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(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();
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();
189 case Change::DELETED:
190 // Suppress access to non-existent
191 // "end-of-paragraph char"
193 eraseChar(pos, false);
204 void Paragraph::Pimpl::rejectChanges(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();
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 (column > lyxrc.plaintext_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);
471 // Two major modes: LaTeX or plain
472 // Handle here those cases common to both modes
473 // and then split to handle the two modes separately.
475 case Paragraph::META_INSET: {
476 InsetBase * inset = owner_->getInset(i);
478 // FIXME: remove this check
482 // FIXME: move this to InsetNewline::latex
483 if (inset->lyxCode() == InsetBase::NEWLINE_CODE) {
484 // newlines are handled differently here than
485 // the default in simpleTeXSpecialChars().
486 if (!style.newline_allowed) {
490 column += running_font.latexWriteEndChanges(
491 os, basefont, basefont, bparams);
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);
515 bool const output = bparams.outputChanges
516 && runparams.flavor == OutputParams::LATEX
517 && features.isAvailable("dvipost");
519 if (inset->canTrackChanges()) {
520 column += Changes::latexMarkChange(os, running_change,
521 Change::UNCHANGED, output);
522 running_change = Change::UNCHANGED;
526 odocstream::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.latexWriteEndChanges(
545 os, basefont, basefont, bparams);
547 basefont = owner_->getLayoutFont(bparams, outerfont);
548 running_font = basefont;
551 int tmp = inset->latex(buf, os, runparams);
557 for (int j = 0; j < tmp; ++j) {
560 texrow.start(owner_->id(), i + 1);
563 column += os.tellp() - len;
569 // And now for the special cases within each mode
573 os << "\\textbackslash{}";
577 // The following characters could be written literally in latin1, but they
578 // would be wrongly converted on systems where char is signed, so we give
580 // This also makes us independant from the encoding of this source file.
581 case 0xb1: // ± PLUS-MINUS SIGN
582 case 0xb2: // ² SUPERSCRIPT TWO
583 case 0xb3: // ³ SUPERSCRIPT THREE
584 case 0xd7: // × MULTIPLICATION SIGN
585 case 0xf7: // ÷ DIVISION SIGN
586 case 0xb9: // ¹ SUPERSCRIPT ONE
587 case 0xac: // ¬ NOT SIGN
588 case 0xb5: // µ MICRO SIGN
589 if (isEncoding(bparams, font, "latin1")
590 || isEncoding(bparams, font, "latin9")) {
591 os << "\\ensuremath{";
600 case '|': case '<': case '>':
601 // In T1 encoding, these characters exist
602 if (lyxrc.fontenc == "T1") {
604 //... but we should avoid ligatures
605 if ((c == '>' || c == '<')
607 && getChar(i + 1) == c) {
608 //os << "\\textcompwordmark{}";
609 // Jean-Marc, have a look at
610 // this. I think this works
618 // Typewriter font also has them
619 if (font.family() == LyXFont::TYPEWRITER_FAMILY) {
623 // Otherwise, we use what LaTeX
627 os << "\\textless{}";
631 os << "\\textgreater{}";
641 case '-': // "--" in Typewriter mode -> "-{}-"
643 && getChar(i + 1) == '-'
644 && font.family() == LyXFont::TYPEWRITER_FAMILY) {
653 os << "\\char`\\\"{}";
657 case 0xa3: // £ POUND SIGN
658 if (bparams.inputenc == "default") {
666 case 0x20ac: // EURO SIGN
667 if (isEncoding(bparams, font, "latin9")
668 || isEncoding(bparams, font, "cp1251")
669 || isEncoding(bparams, font, "utf8")
670 || isEncoding(bparams, font, "latin10")
671 || isEncoding(bparams, font, "cp858")) {
674 os << "\\texteuro{}";
679 // These characters are covered by latin1, but not
680 // by latin9 (a.o.). We have to support them because
681 // we switched the default of latin1-languages to latin9
682 case 0xa4: // CURRENCY SYMBOL
683 case 0xa6: // BROKEN BAR
684 case 0xa8: // DIAERESIS
685 case 0xb4: // ACUTE ACCENT
686 case 0xb8: // CEDILLA
687 case 0xbd: // 1/2 FRACTION
688 case 0xbc: // 1/4 FRACTION
689 case 0xbe: // 3/4 FRACTION
690 if (isEncoding(bparams, font, "latin1")
691 || isEncoding(bparams, font, "latin5")
692 || isEncoding(bparams, font, "utf8")) {
698 os << "\\textcurrency{}";
702 os << "\\textbrokenbar{}";
706 os << "\\textasciidieresis{}";
710 os << "\\textasciiacute{}";
713 case 0xb8: // from latin1.def:
718 os << "\\textonehalf{}";
722 os << "\\textonequarter{}";
726 os << "\\textthreequarters{}";
734 case '%': case '#': case '{':
742 os << "\\textasciitilde{}";
747 os << "\\textasciicircum{}";
752 // avoid being mistaken for optional arguments
760 // Blanks are printed before font switching.
761 // Sure? I am not! (try nice-latex)
762 // I am sure it's correct. LyX might be smarter
763 // in the future, but for now, nothing wrong is
769 // I assume this is hack treating typewriter as verbatim
770 if (font.family() == LyXFont::TYPEWRITER_FAMILY) {
779 // FIXME: if we have "LaTeX" with a font
780 // change in the middle (before the 'T', then
781 // the "TeX" part is still special cased.
782 // Really we should only operate this on
783 // "words" for some definition of word
787 for (; pnr < phrases_nr; ++pnr) {
788 if (isTextAt(special_phrases[pnr].phrase, i)) {
789 os << special_phrases[pnr].macro;
790 i += special_phrases[pnr].phrase.length() - 1;
791 column += special_phrases[pnr].macro.length() - 1;
796 if (pnr == phrases_nr && c != '\0') {
805 void Paragraph::Pimpl::validate(LaTeXFeatures & features,
806 LyXLayout const & layout) const
808 BufferParams const & bparams = features.bufferParams();
811 if (!params.spacing().isDefault())
812 features.require("setspace");
815 features.useLayout(layout.name());
818 Language const * doc_language = bparams.language;
820 FontList::const_iterator fcit = fontlist.begin();
821 FontList::const_iterator fend = fontlist.end();
822 for (; fcit != fend; ++fcit) {
823 if (fcit->font().noun() == LyXFont::ON) {
824 lyxerr[Debug::LATEX] << "font.noun: "
825 << fcit->font().noun()
827 features.require("noun");
828 lyxerr[Debug::LATEX] << "Noun enabled. Font: "
829 << to_utf8(fcit->font().stateText(0))
832 switch (fcit->font().color()) {
834 case LColor::inherit:
836 // probably we should put here all interface colors used for
837 // font displaying! For now I just add this ones I know of (Jug)
842 features.require("color");
843 lyxerr[Debug::LATEX] << "Color enabled. Font: "
844 << to_utf8(fcit->font().stateText(0))
848 Language const * language = fcit->font().language();
849 if (language->babel() != doc_language->babel() &&
850 language != ignore_language &&
851 language != latex_language)
853 features.useLanguage(language);
854 lyxerr[Debug::LATEX] << "Found language "
855 << language->babel() << endl;
859 if (!params.leftIndent().zero())
860 features.require("ParagraphLeftIndent");
863 InsetList::const_iterator icit = owner_->insetlist.begin();
864 InsetList::const_iterator iend = owner_->insetlist.end();
865 for (; icit != iend; ++icit) {
867 icit->inset->validate(features);
868 if (layout.needprotect &&
869 icit->inset->lyxCode() == InsetBase::FOOT_CODE)
870 features.require("NeedLyXFootnoteCode");
875 for (pos_type i = 0; i < size() ; ++i) {
876 for (size_t pnr = 0; pnr < phrases_nr; ++pnr) {
877 if (!special_phrases[pnr].builtin
878 && isTextAt(special_phrases[pnr].phrase, i)) {
879 features.require(special_phrases[pnr].phrase);
883 // these glyphs require the textcomp package
884 if (getChar(i) == 0x20ac || getChar(i) == 0xa4
885 || getChar(i) == 0xa6 || getChar(i) == 0xa8
886 || getChar(i) == 0xb4 || getChar(i) == 0xbd
887 || getChar(i) == 0xbc || getChar(i) == 0xbe)
888 features.require("textcomp");