2 * \file paragraph_pimpl.C
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Lars Gullik Bjønnes
7 * \author Jean-Marc Lasgouttes
11 * Full author contact details are available in file CREDITS.
16 #include "paragraph_pimpl.h"
18 #include "bufferparams.h"
22 #include "LaTeXFeatures.h"
24 #include "lyxlength.h"
26 #include "outputparams.h"
29 #include <boost/next_prior.hpp>
34 using std::upper_bound;
35 using std::lower_bound;
40 // Initialization of the counter for the paragraph id's,
41 unsigned int Paragraph::Pimpl::paragraph_id = 0;
45 struct special_phrase {
51 special_phrase special_phrases[] = {
52 { "LyX", "\\LyX{}", false },
53 { "TeX", "\\TeX{}", true },
54 { "LaTeX2e", "\\LaTeXe{}", true },
55 { "LaTeX", "\\LaTeX{}", true },
58 size_t const phrases_nr = sizeof(special_phrases)/sizeof(special_phrase);
63 Paragraph::Pimpl::Pimpl(Paragraph * owner)
71 Paragraph::Pimpl::Pimpl(Pimpl const & p, Paragraph * owner)
72 : params(p.params), owner_(owner)
74 inset_owner = p.inset_owner;
75 fontlist = p.fontlist;
79 changes_.reset(new Changes(*p.changes_.get()));
83 void Paragraph::Pimpl::setContentsFromPar(Paragraph const & par)
85 owner_->text_ = par.text_;
86 if (par.pimpl_->tracking()) {
87 changes_.reset(new Changes(*(par.pimpl_->changes_.get())));
92 void Paragraph::Pimpl::trackChanges(Change::Type type)
95 lyxerr[Debug::CHANGES] << "already tracking for par " << id_ << endl;
99 lyxerr[Debug::CHANGES] << "track changes for par "
100 << id_ << " type " << type << endl;
101 changes_.reset(new Changes(type));
102 changes_->set(type, 0, size());
106 void Paragraph::Pimpl::untrackChanges()
112 void Paragraph::Pimpl::cleanChanges()
114 // if we're not tracking, we don't want to reset...
118 changes_.reset(new Changes(Change::INSERTED));
119 changes_->set(Change::INSERTED, 0, size());
123 bool Paragraph::Pimpl::isChanged(pos_type start, pos_type end) const
128 return changes_->isChange(start, end);
132 bool Paragraph::Pimpl::isChangeEdited(pos_type start, pos_type end) const
137 return changes_->isChangeEdited(start, end);
141 void Paragraph::Pimpl::setChange(pos_type pos, Change::Type type)
146 changes_->set(type, pos);
150 Change::Type Paragraph::Pimpl::lookupChange(pos_type pos) const
153 return Change::UNCHANGED;
155 return changes_->lookup(pos);
159 Change const Paragraph::Pimpl::lookupChangeFull(pos_type pos) const
162 return Change(Change::UNCHANGED);
164 return changes_->lookupFull(pos);
168 void Paragraph::Pimpl::markErased(bool erased)
170 BOOST_ASSERT(tracking());
175 for (pos_type i = 0; i < size(); ++i) {
176 changes_->set(Change::UNCHANGED, i);
177 if (owner_->isInset(i))
178 owner_->getInset(i)->markErased(false);
184 void Paragraph::Pimpl::acceptChange(pos_type start, pos_type end)
190 changes_.reset(new Changes(Change::UNCHANGED));
194 lyxerr[Debug::CHANGES] << "acceptchange" << endl;
197 for (; i < end; ++i) {
198 switch (lookupChange(i)) {
199 case Change::UNCHANGED:
202 case Change::INSERTED:
203 changes_->set(Change::UNCHANGED, i);
206 case Change::DELETED:
215 lyxerr[Debug::CHANGES] << "endacceptchange" << endl;
216 changes_->reset(Change::UNCHANGED);
220 void Paragraph::Pimpl::rejectChange(pos_type start, pos_type end)
226 changes_.reset(new Changes(Change::UNCHANGED));
232 for (; i < end; ++i) {
233 switch (lookupChange(i)) {
234 case Change::UNCHANGED:
237 case Change::INSERTED:
244 case Change::DELETED:
245 changes_->set(Change::UNCHANGED, i);
246 if (owner_->isInset(i))
247 owner_->getInset(i)->markErased(false);
251 changes_->reset(Change::UNCHANGED);
255 Paragraph::value_type Paragraph::Pimpl::getChar(pos_type pos) const
257 return owner_->getChar(pos);
261 void Paragraph::Pimpl::insertChar(pos_type pos, value_type c, Change change)
263 BOOST_ASSERT(pos <= size());
266 changes_->record(change, pos);
269 // This is actually very common when parsing buffers (and
270 // maybe inserting ascii text)
272 // when appending characters, no need to update tables
273 owner_->text_.push_back(c);
277 owner_->text_.insert(owner_->text_.begin() + pos, c);
279 // Update the font table.
280 FontTable search_font(pos, LyXFont());
281 for (FontList::iterator it = lower_bound(fontlist.begin(),
283 search_font, matchFT());
284 it != fontlist.end(); ++it)
286 it->pos(it->pos() + 1);
290 owner_->insetlist.increasePosAfterPos(pos);
294 void Paragraph::Pimpl::insertInset(pos_type pos,
295 InsetBase * inset, Change change)
298 BOOST_ASSERT(pos <= size());
300 insertChar(pos, META_INSET, change);
301 BOOST_ASSERT(owner_->text_[pos] == META_INSET);
303 // Add a new entry in the insetlist.
304 owner_->insetlist.insert(inset, pos);
308 void Paragraph::Pimpl::eraseIntern(pos_type pos)
310 // if it is an inset, delete the inset entry
311 if (owner_->text_[pos] == Paragraph::META_INSET) {
312 owner_->insetlist.erase(pos);
315 owner_->text_.erase(owner_->text_.begin() + pos);
317 // Erase entries in the tables.
318 FontTable search_font(pos, LyXFont());
320 FontList::iterator it =
321 lower_bound(fontlist.begin(),
323 search_font, matchFT());
324 if (it != fontlist.end() && it->pos() == pos &&
326 (it != fontlist.begin()
327 && boost::prior(it)->pos() == pos - 1))) {
328 // If it is a multi-character font
329 // entry, we just make it smaller
330 // (see update below), otherwise we
332 unsigned int const i = it - fontlist.begin();
333 fontlist.erase(fontlist.begin() + i);
334 it = fontlist.begin() + i;
335 if (i > 0 && i < fontlist.size() &&
336 fontlist[i - 1].font() == fontlist[i].font()) {
337 fontlist.erase(fontlist.begin() + i - 1);
338 it = fontlist.begin() + i - 1;
342 // Update all other entries.
343 FontList::iterator fend = fontlist.end();
344 for (; it != fend; ++it)
345 it->pos(it->pos() - 1);
347 // Update the insetlist.
348 owner_->insetlist.decreasePosAfterPos(pos);
352 bool Paragraph::Pimpl::erase(pos_type pos)
354 BOOST_ASSERT(pos < size());
357 Change::Type changetype(changes_->lookup(pos));
358 changes_->record(Change(Change::DELETED), pos);
360 // only allow the actual removal if it was /new/ text
361 if (changetype != Change::INSERTED) {
362 if (owner_->isInset(pos))
363 owner_->getInset(pos)->markErased(true);
373 int Paragraph::Pimpl::erase(pos_type start, pos_type end)
376 for (pos_type count = end - start; count; --count) {
384 void Paragraph::Pimpl::simpleTeXBlanks(ostream & os, TexRow & texrow,
386 unsigned int & column,
387 LyXFont const & font,
388 LyXLayout const & style)
393 if (column > lyxrc.ascii_linelen
395 && getChar(i - 1) != ' '
397 // same in FreeSpacing mode
398 && !owner_->isFreeSpacing()
399 // In typewriter mode, we want to avoid
400 // ! . ? : at the end of a line
401 && !(font.family() == LyXFont::TYPEWRITER_FAMILY
402 && (getChar(i - 1) == '.'
403 || getChar(i - 1) == '?'
404 || getChar(i - 1) == ':'
405 || getChar(i - 1) == '!'))) {
408 texrow.start(owner_->id(), i + 1);
410 } else if (style.free_spacing) {
418 bool Paragraph::Pimpl::isTextAt(string const & str, pos_type pos) const
420 pos_type const len = str.length();
422 // is the paragraph large enough?
423 if (pos + len > size())
426 // does the wanted text start at point?
427 for (string::size_type i = 0; i < str.length(); ++i) {
428 if (str[i] != owner_->text_[pos + i])
432 // is there a font change in middle of the word?
433 FontList::const_iterator cit = fontlist.begin();
434 FontList::const_iterator end = fontlist.end();
435 for (; cit != end; ++cit) {
436 if (cit->pos() >= pos)
439 if (cit != end && pos + len - 1 > cit->pos())
446 void Paragraph::Pimpl::simpleTeXSpecialChars(Buffer const & buf,
447 BufferParams const & bparams,
450 OutputParams const & runparams,
452 LyXFont & running_font,
454 LyXFont const & outerfont,
456 Change::Type & running_change,
457 LyXLayout const & style,
459 unsigned int & column,
462 if (style.pass_thru) {
463 if (c != Paragraph::META_INSET) {
467 owner_->getInset(i)->plaintext(buf, os, runparams);
472 // Two major modes: LaTeX or plain
473 // Handle here those cases common to both modes
474 // and then split to handle the two modes separately.
476 case Paragraph::META_INSET: {
477 InsetBase * inset = owner_->getInset(i);
479 // FIXME: remove this check
483 // FIXME: move this to InsetNewline::latex
484 if (inset->lyxCode() == InsetBase::NEWLINE_CODE) {
485 // newlines are handled differently here than
486 // the default in simpleTeXSpecialChars().
487 if (!style.newline_allowed) {
491 column += running_font.latexWriteEndChanges(os, basefont, basefont);
494 basefont = owner_->getLayoutFont(bparams, outerfont);
495 running_font = basefont;
497 if (font.family() == LyXFont::TYPEWRITER_FAMILY)
500 if (runparams.moving_arg)
506 texrow.start(owner_->id(), i + 1);
511 // output change tracking marks only if desired,
512 // if dvipost is installed,
513 // and with dvi/ps (other formats don't work)
514 LaTeXFeatures features(buf, bparams, runparams.nice);
515 bool const output = bparams.output_changes
516 && runparams.flavor == OutputParams::LATEX
517 && features.isAvailable("dvipost");
519 if (inset->isTextInset()) {
520 column += Changes::latexMarkChange(os, running_change,
521 Change::UNCHANGED, output);
522 running_change = Change::UNCHANGED;
526 ostream::pos_type const len = os.tellp();
528 if ((inset->lyxCode() == InsetBase::GRAPHICS_CODE
529 || inset->lyxCode() == InsetBase::MATH_CODE
530 || inset->lyxCode() == InsetBase::URL_CODE)
531 && running_font.isRightToLeft()) {
537 #warning Bug: we can have an empty font change here!
538 // if there has just been a font change, we are going to close it
539 // right now, which means stupid latex code like \textsf{}. AFAIK,
540 // this does not harm dvi output. A minor bug, thus (JMarc)
542 // some insets cannot be inside a font change command
543 if (open_font && inset->noFontChange()) {
544 column +=running_font.
545 latexWriteEndChanges(os,
549 basefont = owner_->getLayoutFont(bparams, outerfont);
550 running_font = basefont;
553 int tmp = inset->latex(buf, os, runparams);
559 for (int j = 0; j < tmp; ++j) {
562 texrow.start(owner_->id(), i + 1);
565 column += os.tellp() - len;
571 // And now for the special cases within each mode
575 os << "\\textbackslash{}";
579 case '±': case '²': case '³':
580 case '×': case '÷': case '¹':
582 if ((bparams.inputenc == "latin1" ||
583 bparams.inputenc == "latin9") ||
584 (bparams.inputenc == "auto" &&
585 (font.language()->encoding()->LatexName()
587 font.language()->encoding()->LatexName()
589 os << "\\ensuremath{"
598 case '|': case '<': case '>':
599 // In T1 encoding, these characters exist
600 if (lyxrc.fontenc == "T1") {
602 //... but we should avoid ligatures
603 if ((c == '>' || c == '<')
605 && getChar(i + 1) == c) {
606 //os << "\\textcompwordmark{}";
607 // Jean-Marc, have a look at
608 // this. I think this works
616 // Typewriter font also has them
617 if (font.family() == LyXFont::TYPEWRITER_FAMILY) {
621 // Otherwise, we use what LaTeX
625 os << "\\textless{}";
629 os << "\\textgreater{}";
639 case '-': // "--" in Typewriter mode -> "-{}-"
641 && getChar(i + 1) == '-'
642 && font.family() == LyXFont::TYPEWRITER_FAMILY) {
651 os << "\\char`\\\"{}";
656 if (bparams.inputenc == "default") {
665 case '%': case '#': case '{':
672 os << "\\textasciitilde{}";
677 os << "\\textasciicircum{}";
682 // avoid being mistaken for optional arguments
683 os << '{' << c << '}';
688 // Blanks are printed before font switching.
689 // Sure? I am not! (try nice-latex)
690 // I am sure it's correct. LyX might be smarter
691 // in the future, but for now, nothing wrong is
697 // I assume this is hack treating typewriter as verbatim
698 if (font.family() == LyXFont::TYPEWRITER_FAMILY) {
707 // FIXME: if we have "LaTeX" with a font
708 // change in the middle (before the 'T', then
709 // the "TeX" part is still special cased.
710 // Really we should only operate this on
711 // "words" for some definition of word
715 for (; pnr < phrases_nr; ++pnr) {
716 if (isTextAt(special_phrases[pnr].phrase, i)) {
717 os << special_phrases[pnr].macro;
718 i += special_phrases[pnr].phrase.length() - 1;
719 column += special_phrases[pnr].macro.length() - 1;
724 if (pnr == phrases_nr && c != '\0') {
733 void Paragraph::Pimpl::validate(LaTeXFeatures & features,
734 LyXLayout const & layout) const
736 BufferParams const & bparams = features.bufferParams();
739 if (!params.spacing().isDefault())
740 features.require("setspace");
743 features.useLayout(layout.name());
746 Language const * doc_language = bparams.language;
748 FontList::const_iterator fcit = fontlist.begin();
749 FontList::const_iterator fend = fontlist.end();
750 for (; fcit != fend; ++fcit) {
751 if (fcit->font().noun() == LyXFont::ON) {
752 lyxerr[Debug::LATEX] << "font.noun: "
753 << fcit->font().noun()
755 features.require("noun");
756 lyxerr[Debug::LATEX] << "Noun enabled. Font: "
757 << fcit->font().stateText(0)
760 switch (fcit->font().color()) {
762 case LColor::inherit:
764 // probably we should put here all interface colors used for
765 // font displaying! For now I just add this ones I know of (Jug)
770 features.require("color");
771 lyxerr[Debug::LATEX] << "Color enabled. Font: "
772 << fcit->font().stateText(0)
776 Language const * language = fcit->font().language();
777 if (language->babel() != doc_language->babel() &&
778 language != ignore_language &&
779 language != latex_language)
781 features.useLanguage(language);
782 lyxerr[Debug::LATEX] << "Found language "
783 << language->babel() << endl;
787 if (!params.leftIndent().zero())
788 features.require("ParagraphLeftIndent");
791 InsetList::const_iterator icit = owner_->insetlist.begin();
792 InsetList::const_iterator iend = owner_->insetlist.end();
793 for (; icit != iend; ++icit) {
795 icit->inset->validate(features);
796 if (layout.needprotect &&
797 icit->inset->lyxCode() == InsetBase::FOOT_CODE)
798 features.require("NeedLyXFootnoteCode");
803 for (pos_type i = 0; i < size() ; ++i) {
804 for (size_t pnr = 0; pnr < phrases_nr; ++pnr) {
805 if (!special_phrases[pnr].builtin
806 && isTextAt(special_phrases[pnr].phrase, i)) {
807 features.require(special_phrases[pnr].phrase);