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"
17 #include "paragraph.h"
19 #include "bufferparams.h"
23 #include "LaTeXFeatures.h"
25 #include "lyxlength.h"
27 #include "outputparams.h"
30 #include <boost/next_prior.hpp>
36 using std::upper_bound;
37 using std::lower_bound;
41 // Initialization of the counter for the paragraph id's,
42 unsigned int Paragraph::Pimpl::paragraph_id = 0;
46 struct special_phrase {
52 special_phrase const special_phrases[] = {
53 { "LyX", from_ascii("\\LyX{}"), false },
54 { "TeX", from_ascii("\\TeX{}"), true },
55 { "LaTeX2e", from_ascii("\\LaTeXe{}"), true },
56 { "LaTeX", from_ascii("\\LaTeX{}"), true },
59 size_t const phrases_nr = sizeof(special_phrases)/sizeof(special_phrase);
62 bool isEncoding(BufferParams const & bparams, LyXFont const & font,
63 string const & encoding)
65 // We do ignore bparams.inputenc == "default" here because characters
66 // in this encoding could be treated by TeX as something different,
67 // e.g. if they are inside a CJK environment. See also
68 // http://bugzilla.lyx.org/show_bug.cgi?id=3043.
69 return (bparams.inputenc == encoding
70 || (bparams.inputenc == "auto"
71 && font.language()->encoding()->latexName() == encoding));
77 Paragraph::Pimpl::Pimpl(Paragraph * owner)
85 Paragraph::Pimpl::Pimpl(Pimpl const & p, Paragraph * owner)
86 : params(p.params), changes_(p.changes_), owner_(owner)
88 inset_owner = p.inset_owner;
89 fontlist = p.fontlist;
94 bool Paragraph::Pimpl::isChanged(pos_type start, pos_type end) const
96 BOOST_ASSERT(start >= 0 && start <= size());
97 BOOST_ASSERT(end > start && end <= size() + 1);
99 return changes_.isChanged(start, end);
103 bool Paragraph::Pimpl::isMergedOnEndOfParDeletion(bool trackChanges) const {
104 // keep the logic here in sync with the logic of eraseChars()
110 Change change = changes_.lookup(size());
112 return change.type == Change::INSERTED && change.author == 0;
116 void Paragraph::Pimpl::setChange(Change const & change)
118 // beware of the imaginary end-of-par character!
119 changes_.set(change, 0, size() + 1);
122 * Propagate the change recursively - but not in case of DELETED!
124 * Imagine that your co-author makes changes in an existing inset. He
125 * sends your document to you and you come to the conclusion that the
126 * inset should go completely. If you erase it, LyX must not delete all
127 * text within the inset. Otherwise, the change tracked insertions of
128 * your co-author get lost and there is no way to restore them later.
130 * Conclusion: An inset's content should remain untouched if you delete it
133 if (change.type != Change::DELETED) {
134 for (pos_type pos = 0; pos < size(); ++pos) {
135 if (owner_->isInset(pos)) {
136 owner_->getInset(pos)->setChange(change);
143 void Paragraph::Pimpl::setChange(pos_type pos, Change const & change)
145 BOOST_ASSERT(pos >= 0 && pos <= size());
147 changes_.set(change, pos);
149 // see comment in setChange(Change const &) above
151 if (change.type != Change::DELETED &&
152 pos < size() && owner_->isInset(pos)) {
153 owner_->getInset(pos)->setChange(change);
158 Change const & Paragraph::Pimpl::lookupChange(pos_type pos) const
160 BOOST_ASSERT(pos >= 0 && pos <= size());
162 return changes_.lookup(pos);
166 void Paragraph::Pimpl::acceptChanges(BufferParams const & bparams, pos_type start, pos_type end)
168 BOOST_ASSERT(start >= 0 && start <= size());
169 BOOST_ASSERT(end > start && end <= size() + 1);
171 for (pos_type pos = start; pos < end; ++pos) {
172 switch (lookupChange(pos).type) {
173 case Change::UNCHANGED:
174 // accept changes in nested inset
175 if (pos < size() && owner_->isInset(pos)) {
176 owner_->getInset(pos)->acceptChanges(bparams);
181 case Change::INSERTED:
182 changes_.set(Change(Change::UNCHANGED), pos);
183 // also accept changes in nested inset
184 if (pos < size() && owner_->isInset(pos)) {
185 owner_->getInset(pos)->acceptChanges(bparams);
189 case Change::DELETED:
190 // Suppress access to non-existent
191 // "end-of-paragraph char"
193 eraseChar(pos, false);
204 void Paragraph::Pimpl::rejectChanges(BufferParams const & bparams, pos_type start, pos_type end)
206 BOOST_ASSERT(start >= 0 && start <= size());
207 BOOST_ASSERT(end > start && end <= size() + 1);
209 for (pos_type pos = start; pos < end; ++pos) {
210 switch (lookupChange(pos).type) {
211 case Change::UNCHANGED:
212 // reject changes in nested inset
213 if (pos < size() && owner_->isInset(pos)) {
214 owner_->getInset(pos)->rejectChanges(bparams);
218 case Change::INSERTED:
219 // Suppress access to non-existent
220 // "end-of-paragraph char"
222 eraseChar(pos, false);
228 case Change::DELETED:
229 changes_.set(Change(Change::UNCHANGED), pos);
231 // Do NOT reject changes within a deleted inset!
232 // There may be insertions of a co-author inside of it!
240 Paragraph::value_type Paragraph::Pimpl::getChar(pos_type pos) const
242 BOOST_ASSERT(pos >= 0 && pos <= size());
244 return owner_->getChar(pos);
248 void Paragraph::Pimpl::insertChar(pos_type pos, value_type c, Change const & change)
250 BOOST_ASSERT(pos >= 0 && pos <= size());
253 changes_.insert(change, pos);
255 // This is actually very common when parsing buffers (and
256 // maybe inserting ascii text)
258 // when appending characters, no need to update tables
259 owner_->text_.push_back(c);
263 owner_->text_.insert(owner_->text_.begin() + pos, c);
265 // Update the font table.
266 FontTable search_font(pos, LyXFont());
267 for (FontList::iterator it
268 = lower_bound(fontlist.begin(), fontlist.end(), search_font, matchFT());
269 it != fontlist.end(); ++it)
271 it->pos(it->pos() + 1);
275 owner_->insetlist.increasePosAfterPos(pos);
279 void Paragraph::Pimpl::insertInset(pos_type pos, InsetBase * inset,
280 Change const & change)
283 BOOST_ASSERT(pos >= 0 && pos <= size());
285 insertChar(pos, META_INSET, change);
286 BOOST_ASSERT(owner_->text_[pos] == META_INSET);
288 // Add a new entry in the insetlist.
289 owner_->insetlist.insert(inset, pos);
293 bool Paragraph::Pimpl::eraseChar(pos_type pos, bool trackChanges)
295 BOOST_ASSERT(pos >= 0 && pos <= size());
297 // keep the logic here in sync with the logic of isMergedOnEndOfParDeletion()
300 Change change = changes_.lookup(pos);
302 // set the character to DELETED if
303 // a) it was previously unchanged or
304 // b) it was inserted by a co-author
306 if (change.type == Change::UNCHANGED ||
307 (change.type == Change::INSERTED && change.author != 0)) {
308 setChange(pos, Change(Change::DELETED));
312 if (change.type == Change::DELETED)
316 // Don't physically access the imaginary end-of-paragraph character.
317 // eraseChar() can only mark it as DELETED. A physical deletion of
318 // end-of-par must be handled externally.
326 // if it is an inset, delete the inset entry
327 if (owner_->text_[pos] == Paragraph::META_INSET) {
328 owner_->insetlist.erase(pos);
331 owner_->text_.erase(owner_->text_.begin() + pos);
333 // Erase entries in the tables.
334 FontTable search_font(pos, LyXFont());
336 FontList::iterator it =
337 lower_bound(fontlist.begin(),
339 search_font, matchFT());
340 if (it != fontlist.end() && it->pos() == pos &&
342 (it != fontlist.begin()
343 && boost::prior(it)->pos() == pos - 1))) {
344 // If it is a multi-character font
345 // entry, we just make it smaller
346 // (see update below), otherwise we
348 unsigned int const i = it - fontlist.begin();
349 fontlist.erase(fontlist.begin() + i);
350 it = fontlist.begin() + i;
351 if (i > 0 && i < fontlist.size() &&
352 fontlist[i - 1].font() == fontlist[i].font()) {
353 fontlist.erase(fontlist.begin() + i - 1);
354 it = fontlist.begin() + i - 1;
358 // Update all other entries
359 FontList::iterator fend = fontlist.end();
360 for (; it != fend; ++it)
361 it->pos(it->pos() - 1);
363 // Update the insetlist
364 owner_->insetlist.decreasePosAfterPos(pos);
370 int Paragraph::Pimpl::eraseChars(pos_type start, pos_type end, bool trackChanges)
372 BOOST_ASSERT(start >= 0 && start <= size());
373 BOOST_ASSERT(end >= start && end <= size() + 1);
376 for (pos_type count = end - start; count; --count) {
377 if (!eraseChar(i, trackChanges))
384 void Paragraph::Pimpl::simpleTeXBlanks(odocstream & os, TexRow & texrow,
386 unsigned int & column,
387 LyXFont const & font,
388 LyXLayout const & style)
393 if (lyxrc.plaintext_linelen > 0
394 && column > lyxrc.plaintext_linelen
396 && getChar(i - 1) != ' '
398 // same in FreeSpacing mode
399 && !owner_->isFreeSpacing()
400 // In typewriter mode, we want to avoid
401 // ! . ? : at the end of a line
402 && !(font.family() == LyXFont::TYPEWRITER_FAMILY
403 && (getChar(i - 1) == '.'
404 || getChar(i - 1) == '?'
405 || getChar(i - 1) == ':'
406 || getChar(i - 1) == '!'))) {
409 texrow.start(owner_->id(), i + 1);
411 } else if (style.free_spacing) {
419 bool Paragraph::Pimpl::isTextAt(string const & str, pos_type pos) const
421 pos_type const len = str.length();
423 // is the paragraph large enough?
424 if (pos + len > size())
427 // does the wanted text start at point?
428 for (string::size_type i = 0; i < str.length(); ++i) {
429 // Caution: direct comparison of characters works only
430 // because str is pure ASCII.
431 if (str[i] != owner_->text_[pos + i])
435 // is there a font change in middle of the word?
436 FontList::const_iterator cit = fontlist.begin();
437 FontList::const_iterator end = fontlist.end();
438 for (; cit != end; ++cit) {
439 if (cit->pos() >= pos)
442 if (cit != end && pos + len - 1 > cit->pos())
449 void Paragraph::Pimpl::simpleTeXSpecialChars(Buffer const & buf,
450 BufferParams const & bparams,
453 OutputParams const & runparams,
455 LyXFont & running_font,
457 LyXFont const & outerfont,
459 Change::Type & running_change,
460 LyXLayout const & style,
462 unsigned int & column,
465 if (style.pass_thru) {
466 if (c != Paragraph::META_INSET) {
470 owner_->getInset(i)->plaintext(buf, os, runparams);
474 // Two major modes: LaTeX or plain
475 // Handle here those cases common to both modes
476 // and then split to handle the two modes separately.
478 case Paragraph::META_INSET: {
479 InsetBase * inset = owner_->getInset(i);
481 // FIXME: remove this check
485 // FIXME: move this to InsetNewline::latex
486 if (inset->lyxCode() == InsetBase::NEWLINE_CODE) {
487 // newlines are handled differently here than
488 // the default in simpleTeXSpecialChars().
489 if (!style.newline_allowed) {
493 column += running_font.latexWriteEndChanges(
494 os, basefont, basefont, bparams);
497 basefont = owner_->getLayoutFont(bparams, outerfont);
498 running_font = basefont;
500 if (font.family() == LyXFont::TYPEWRITER_FAMILY)
503 if (runparams.moving_arg)
509 texrow.start(owner_->id(), i + 1);
514 // output change tracking marks only if desired,
515 // if dvipost is installed,
516 // and with dvi/ps (other formats don't work)
517 LaTeXFeatures features(buf, bparams, runparams);
518 bool const output = bparams.outputChanges
519 && runparams.flavor == OutputParams::LATEX
520 && features.isAvailable("dvipost");
522 if (inset->canTrackChanges()) {
523 column += Changes::latexMarkChange(os, running_change,
524 Change::UNCHANGED, output);
525 running_change = Change::UNCHANGED;
529 odocstream::pos_type const len = os.tellp();
531 if ((inset->lyxCode() == InsetBase::GRAPHICS_CODE
532 || inset->lyxCode() == InsetBase::MATH_CODE
533 || inset->lyxCode() == InsetBase::URL_CODE)
534 && running_font.isRightToLeft()) {
540 #warning Bug: we can have an empty font change here!
541 // if there has just been a font change, we are going to close it
542 // right now, which means stupid latex code like \textsf{}. AFAIK,
543 // this does not harm dvi output. A minor bug, thus (JMarc)
545 // some insets cannot be inside a font change command
546 if (open_font && inset->noFontChange()) {
547 column += running_font.latexWriteEndChanges(
548 os, basefont, basefont, bparams);
550 basefont = owner_->getLayoutFont(bparams, outerfont);
551 running_font = basefont;
554 int tmp = inset->latex(buf, os, runparams);
560 for (int j = 0; j < tmp; ++j) {
563 texrow.start(owner_->id(), i + 1);
566 column += os.tellp() - len;
572 // And now for the special cases within each mode
576 os << "\\textbackslash{}";
580 // The following characters could be written literally in latin1, but they
581 // would be wrongly converted on systems where char is signed, so we give
583 // This also makes us independant from the encoding of this source file.
584 case 0xb1: // ± PLUS-MINUS SIGN
585 case 0xb2: // ² SUPERSCRIPT TWO
586 case 0xb3: // ³ SUPERSCRIPT THREE
587 case 0xd7: // × MULTIPLICATION SIGN
588 case 0xf7: // ÷ DIVISION SIGN
589 case 0xb9: // ¹ SUPERSCRIPT ONE
590 case 0xac: // ¬ NOT SIGN
591 case 0xb5: // µ MICRO SIGN
592 if (isEncoding(bparams, font, "latin1")
593 || isEncoding(bparams, font, "latin9")) {
594 os << "\\ensuremath{";
603 case '|': case '<': case '>':
604 // In T1 encoding, these characters exist
605 if (lyxrc.fontenc == "T1") {
607 //... but we should avoid ligatures
608 if ((c == '>' || c == '<')
610 && getChar(i + 1) == c) {
611 //os << "\\textcompwordmark{}";
613 // Jean-Marc, have a look at
614 // this. I think this works
622 // Typewriter font also has them
623 if (font.family() == LyXFont::TYPEWRITER_FAMILY) {
627 // Otherwise, we use what LaTeX
631 os << "\\textless{}";
635 os << "\\textgreater{}";
645 case '-': // "--" in Typewriter mode -> "-{}-"
647 && getChar(i + 1) == '-'
648 && font.family() == LyXFont::TYPEWRITER_FAMILY) {
657 os << "\\char`\\\"{}";
661 case 0xa3: // £ POUND SIGN
662 if (bparams.inputenc == "default") {
670 case 0x20ac: // EURO SIGN
671 if (isEncoding(bparams, font, "latin9")
672 || isEncoding(bparams, font, "cp1251")
673 || isEncoding(bparams, font, "utf8")
674 || isEncoding(bparams, font, "latin10")
675 || isEncoding(bparams, font, "cp858")) {
678 os << "\\texteuro{}";
683 // These characters are covered by latin1, but not
684 // by latin9 (a.o.). We have to support them because
685 // we switched the default of latin1-languages to latin9
686 case 0xa4: // CURRENCY SYMBOL
687 case 0xa6: // BROKEN BAR
688 case 0xa8: // DIAERESIS
689 case 0xb4: // ACUTE ACCENT
690 case 0xb8: // CEDILLA
691 case 0xbd: // 1/2 FRACTION
692 case 0xbc: // 1/4 FRACTION
693 case 0xbe: // 3/4 FRACTION
694 if (isEncoding(bparams, font, "latin1")
695 || isEncoding(bparams, font, "latin5")
696 || isEncoding(bparams, font, "utf8")) {
702 os << "\\textcurrency{}";
706 os << "\\textbrokenbar{}";
710 os << "\\textasciidieresis{}";
714 os << "\\textasciiacute{}";
717 case 0xb8: // from latin1.def:
722 os << "\\textonehalf{}";
726 os << "\\textonequarter{}";
730 os << "\\textthreequarters{}";
738 case '%': case '#': case '{':
746 os << "\\textasciitilde{}";
751 os << "\\textasciicircum{}";
756 // avoid being mistaken for optional arguments
764 // Blanks are printed before font switching.
765 // Sure? I am not! (try nice-latex)
766 // I am sure it's correct. LyX might be smarter
767 // in the future, but for now, nothing wrong is
773 // I assume this is hack treating typewriter as verbatim
774 if (font.family() == LyXFont::TYPEWRITER_FAMILY) {
783 // FIXME: if we have "LaTeX" with a font
784 // change in the middle (before the 'T', then
785 // the "TeX" part is still special cased.
786 // Really we should only operate this on
787 // "words" for some definition of word
791 for (; pnr < phrases_nr; ++pnr) {
792 if (isTextAt(special_phrases[pnr].phrase, i)) {
793 os << special_phrases[pnr].macro;
794 i += special_phrases[pnr].phrase.length() - 1;
795 column += special_phrases[pnr].macro.length() - 1;
800 if (pnr == phrases_nr && c != '\0') {
809 void Paragraph::Pimpl::validate(LaTeXFeatures & features,
810 LyXLayout const & layout) const
812 BufferParams const & bparams = features.bufferParams();
815 if (!params.spacing().isDefault())
816 features.require("setspace");
819 features.useLayout(layout.name());
822 Language const * doc_language = bparams.language;
824 FontList::const_iterator fcit = fontlist.begin();
825 FontList::const_iterator fend = fontlist.end();
826 for (; fcit != fend; ++fcit) {
827 if (fcit->font().noun() == LyXFont::ON) {
828 lyxerr[Debug::LATEX] << "font.noun: "
829 << fcit->font().noun()
831 features.require("noun");
832 lyxerr[Debug::LATEX] << "Noun enabled. Font: "
833 << to_utf8(fcit->font().stateText(0))
836 switch (fcit->font().color()) {
838 case LColor::inherit:
840 // probably we should put here all interface colors used for
841 // font displaying! For now I just add this ones I know of (Jug)
846 features.require("color");
847 lyxerr[Debug::LATEX] << "Color enabled. Font: "
848 << to_utf8(fcit->font().stateText(0))
852 Language const * language = fcit->font().language();
853 if (language->babel() != doc_language->babel() &&
854 language != ignore_language &&
855 language != latex_language)
857 features.useLanguage(language);
858 lyxerr[Debug::LATEX] << "Found language "
859 << language->babel() << endl;
863 if (!params.leftIndent().zero())
864 features.require("ParagraphLeftIndent");
867 InsetList::const_iterator icit = owner_->insetlist.begin();
868 InsetList::const_iterator iend = owner_->insetlist.end();
869 for (; icit != iend; ++icit) {
871 icit->inset->validate(features);
872 if (layout.needprotect &&
873 icit->inset->lyxCode() == InsetBase::FOOT_CODE)
874 features.require("NeedLyXFootnoteCode");
879 for (pos_type i = 0; i < size() ; ++i) {
880 for (size_t pnr = 0; pnr < phrases_nr; ++pnr) {
881 if (!special_phrases[pnr].builtin
882 && isTextAt(special_phrases[pnr].phrase, i)) {
883 features.require(special_phrases[pnr].phrase);
887 // these glyphs require the textcomp package
888 if (getChar(i) == 0x20ac || getChar(i) == 0xa4
889 || getChar(i) == 0xa6 || getChar(i) == 0xa8
890 || getChar(i) == 0xb4 || getChar(i) == 0xbd
891 || getChar(i) == 0xbc || getChar(i) == 0xbe)
892 features.require("textcomp");