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 return (bparams.inputenc == encoding
66 || (bparams.inputenc == "auto"
67 && font.language()->encoding()->latexName() == encoding));
73 Paragraph::Pimpl::Pimpl(Paragraph * owner)
81 Paragraph::Pimpl::Pimpl(Pimpl const & p, Paragraph * owner)
82 : params(p.params), changes_(p.changes_), owner_(owner)
84 inset_owner = p.inset_owner;
85 fontlist = p.fontlist;
90 bool Paragraph::Pimpl::isChanged(pos_type start, pos_type end) const
92 BOOST_ASSERT(start >= 0 && start <= size());
93 BOOST_ASSERT(end > start && end <= size() + 1);
95 return changes_.isChanged(start, end);
99 bool Paragraph::Pimpl::isMergedOnEndOfParDeletion(bool trackChanges) const {
100 // keep the logic here in sync with the logic of eraseChars()
106 Change change = changes_.lookup(size());
108 return change.type == Change::INSERTED && change.author == 0;
112 void Paragraph::Pimpl::setChange(Change const & change)
114 // beware of the imaginary end-of-par character!
115 changes_.set(change, 0, size() + 1);
118 * Propagate the change recursively - but not in case of DELETED!
120 * Imagine that your co-author makes changes in an existing inset. He
121 * sends your document to you and you come to the conclusion that the
122 * inset should go completely. If you erase it, LyX must not delete all
123 * text within the inset. Otherwise, the change tracked insertions of
124 * your co-author get lost and there is no way to restore them later.
126 * Conclusion: An inset's content should remain untouched if you delete it
129 if (change.type != Change::DELETED) {
130 for (pos_type pos = 0; pos < size(); ++pos) {
131 if (owner_->isInset(pos)) {
132 owner_->getInset(pos)->setChange(change);
139 void Paragraph::Pimpl::setChange(pos_type pos, Change const & change)
141 BOOST_ASSERT(pos >= 0 && pos <= size());
143 changes_.set(change, pos);
145 // see comment in setChange(Change const &) above
147 if (change.type != Change::DELETED &&
148 pos < size() && owner_->isInset(pos)) {
149 owner_->getInset(pos)->setChange(change);
154 Change const Paragraph::Pimpl::lookupChange(pos_type pos) const
156 BOOST_ASSERT(pos >= 0 && pos <= size());
158 return changes_.lookup(pos);
162 void Paragraph::Pimpl::acceptChanges(pos_type start, pos_type end)
164 BOOST_ASSERT(start >= 0 && start <= size());
165 BOOST_ASSERT(end > start && end <= size() + 1);
167 for (pos_type pos = start; pos < end; ++pos) {
168 switch (lookupChange(pos).type) {
169 case Change::UNCHANGED:
170 // accept changes in nested inset
171 if (pos < size() && owner_->isInset(pos)) {
172 owner_->getInset(pos)->acceptChanges();
177 case Change::INSERTED:
178 changes_.set(Change(Change::UNCHANGED), pos);
179 // also accept changes in nested inset
180 if (pos < size() && owner_->isInset(pos)) {
181 owner_->getInset(pos)->acceptChanges();
185 case Change::DELETED:
186 // Suppress access to non-existent
187 // "end-of-paragraph char"
189 eraseChar(pos, false);
200 void Paragraph::Pimpl::rejectChanges(pos_type start, pos_type end)
202 BOOST_ASSERT(start >= 0 && start <= size());
203 BOOST_ASSERT(end > start && end <= size() + 1);
205 for (pos_type pos = start; pos < end; ++pos) {
206 switch (lookupChange(pos).type) {
207 case Change::UNCHANGED:
208 // reject changes in nested inset
209 if (pos < size() && owner_->isInset(pos)) {
210 owner_->getInset(pos)->rejectChanges();
214 case Change::INSERTED:
215 // Suppress access to non-existent
216 // "end-of-paragraph char"
218 eraseChar(pos, false);
224 case Change::DELETED:
225 changes_.set(Change(Change::UNCHANGED), pos);
227 // Do NOT reject changes within a deleted inset!
228 // There may be insertions of a co-author inside of it!
236 Paragraph::value_type Paragraph::Pimpl::getChar(pos_type pos) const
238 BOOST_ASSERT(pos >= 0 && pos <= size());
240 return owner_->getChar(pos);
244 void Paragraph::Pimpl::insertChar(pos_type pos, value_type c, Change const & change)
246 BOOST_ASSERT(pos >= 0 && pos <= size());
249 changes_.insert(change, pos);
251 // This is actually very common when parsing buffers (and
252 // maybe inserting ascii text)
254 // when appending characters, no need to update tables
255 owner_->text_.push_back(c);
259 owner_->text_.insert(owner_->text_.begin() + pos, c);
261 // Update the font table.
262 FontTable search_font(pos, LyXFont());
263 for (FontList::iterator it
264 = lower_bound(fontlist.begin(), fontlist.end(), search_font, matchFT());
265 it != fontlist.end(); ++it)
267 it->pos(it->pos() + 1);
271 owner_->insetlist.increasePosAfterPos(pos);
275 void Paragraph::Pimpl::insertInset(pos_type pos, InsetBase * inset,
276 Change const & change)
279 BOOST_ASSERT(pos >= 0 && pos <= size());
281 insertChar(pos, META_INSET, change);
282 BOOST_ASSERT(owner_->text_[pos] == META_INSET);
284 // Add a new entry in the insetlist.
285 owner_->insetlist.insert(inset, pos);
289 bool Paragraph::Pimpl::eraseChar(pos_type pos, bool trackChanges)
291 BOOST_ASSERT(pos >= 0 && pos <= size());
293 // keep the logic here in sync with the logic of isMergedOnEndOfParDeletion()
296 Change change = changes_.lookup(pos);
298 // set the character to DELETED if
299 // a) it was previously unchanged or
300 // b) it was inserted by a co-author
302 if (change.type == Change::UNCHANGED ||
303 (change.type == Change::INSERTED && change.author != 0)) {
304 setChange(pos, Change(Change::DELETED));
308 if (change.type == Change::DELETED)
312 // Don't physically access the imaginary end-of-paragraph character.
313 // eraseChar() can only mark it as DELETED. A physical deletion of
314 // end-of-par must be handled externally.
322 // if it is an inset, delete the inset entry
323 if (owner_->text_[pos] == Paragraph::META_INSET) {
324 owner_->insetlist.erase(pos);
327 owner_->text_.erase(owner_->text_.begin() + pos);
329 // Erase entries in the tables.
330 FontTable search_font(pos, LyXFont());
332 FontList::iterator it =
333 lower_bound(fontlist.begin(),
335 search_font, matchFT());
336 if (it != fontlist.end() && it->pos() == pos &&
338 (it != fontlist.begin()
339 && boost::prior(it)->pos() == pos - 1))) {
340 // If it is a multi-character font
341 // entry, we just make it smaller
342 // (see update below), otherwise we
344 unsigned int const i = it - fontlist.begin();
345 fontlist.erase(fontlist.begin() + i);
346 it = fontlist.begin() + i;
347 if (i > 0 && i < fontlist.size() &&
348 fontlist[i - 1].font() == fontlist[i].font()) {
349 fontlist.erase(fontlist.begin() + i - 1);
350 it = fontlist.begin() + i - 1;
354 // Update all other entries
355 FontList::iterator fend = fontlist.end();
356 for (; it != fend; ++it)
357 it->pos(it->pos() - 1);
359 // Update the insetlist
360 owner_->insetlist.decreasePosAfterPos(pos);
366 int Paragraph::Pimpl::eraseChars(pos_type start, pos_type end, bool trackChanges)
368 BOOST_ASSERT(start >= 0 && start <= size());
369 BOOST_ASSERT(end >= start && end <= size() + 1);
372 for (pos_type count = end - start; count; --count) {
373 if (!eraseChar(i, trackChanges))
380 void Paragraph::Pimpl::simpleTeXBlanks(odocstream & os, TexRow & texrow,
382 unsigned int & column,
383 LyXFont const & font,
384 LyXLayout const & style)
389 if (column > lyxrc.ascii_linelen
391 && getChar(i - 1) != ' '
393 // same in FreeSpacing mode
394 && !owner_->isFreeSpacing()
395 // In typewriter mode, we want to avoid
396 // ! . ? : at the end of a line
397 && !(font.family() == LyXFont::TYPEWRITER_FAMILY
398 && (getChar(i - 1) == '.'
399 || getChar(i - 1) == '?'
400 || getChar(i - 1) == ':'
401 || getChar(i - 1) == '!'))) {
404 texrow.start(owner_->id(), i + 1);
406 } else if (style.free_spacing) {
414 bool Paragraph::Pimpl::isTextAt(string const & str, pos_type pos) const
416 pos_type const len = str.length();
418 // is the paragraph large enough?
419 if (pos + len > size())
422 // does the wanted text start at point?
423 for (string::size_type i = 0; i < str.length(); ++i) {
424 if (str[i] != owner_->text_[pos + i])
428 // is there a font change in middle of the word?
429 FontList::const_iterator cit = fontlist.begin();
430 FontList::const_iterator end = fontlist.end();
431 for (; cit != end; ++cit) {
432 if (cit->pos() >= pos)
435 if (cit != end && pos + len - 1 > cit->pos())
442 void Paragraph::Pimpl::simpleTeXSpecialChars(Buffer const & buf,
443 BufferParams const & bparams,
446 OutputParams const & runparams,
448 LyXFont & running_font,
450 LyXFont const & outerfont,
452 Change::Type & running_change,
453 LyXLayout const & style,
455 unsigned int & column,
458 if (style.pass_thru) {
459 if (c != Paragraph::META_INSET) {
463 owner_->getInset(i)->plaintext(buf, os, runparams);
467 // Two major modes: LaTeX or plain
468 // Handle here those cases common to both modes
469 // and then split to handle the two modes separately.
471 case Paragraph::META_INSET: {
472 InsetBase * inset = owner_->getInset(i);
474 // FIXME: remove this check
478 // FIXME: move this to InsetNewline::latex
479 if (inset->lyxCode() == InsetBase::NEWLINE_CODE) {
480 // newlines are handled differently here than
481 // the default in simpleTeXSpecialChars().
482 if (!style.newline_allowed) {
486 column += running_font.latexWriteEndChanges(os, basefont, basefont);
489 basefont = owner_->getLayoutFont(bparams, outerfont);
490 running_font = basefont;
492 if (font.family() == LyXFont::TYPEWRITER_FAMILY)
495 if (runparams.moving_arg)
501 texrow.start(owner_->id(), i + 1);
506 // output change tracking marks only if desired,
507 // if dvipost is installed,
508 // and with dvi/ps (other formats don't work)
509 LaTeXFeatures features(buf, bparams, runparams);
510 bool const output = bparams.outputChanges
511 && runparams.flavor == OutputParams::LATEX
512 && features.isAvailable("dvipost");
514 if (inset->canTrackChanges()) {
515 column += Changes::latexMarkChange(os, running_change,
516 Change::UNCHANGED, output);
517 running_change = Change::UNCHANGED;
521 odocstream::pos_type const len = os.tellp();
523 if ((inset->lyxCode() == InsetBase::GRAPHICS_CODE
524 || inset->lyxCode() == InsetBase::MATH_CODE
525 || inset->lyxCode() == InsetBase::URL_CODE)
526 && running_font.isRightToLeft()) {
532 #warning Bug: we can have an empty font change here!
533 // if there has just been a font change, we are going to close it
534 // right now, which means stupid latex code like \textsf{}. AFAIK,
535 // this does not harm dvi output. A minor bug, thus (JMarc)
537 // some insets cannot be inside a font change command
538 if (open_font && inset->noFontChange()) {
539 column +=running_font.
540 latexWriteEndChanges(os,
544 basefont = owner_->getLayoutFont(bparams, outerfont);
545 running_font = basefont;
548 int tmp = inset->latex(buf, os, runparams);
554 for (int j = 0; j < tmp; ++j) {
557 texrow.start(owner_->id(), i + 1);
560 column += os.tellp() - len;
566 // And now for the special cases within each mode
570 os << "\\textbackslash{}";
574 // The following characters could be written literally in latin1, but they
575 // would be wrongly converted on systems where char is signed, so we give
577 // This also makes us independant from the encoding of this source file.
578 case 0xb1: // ± PLUS-MINUS SIGN
579 case 0xb2: // ² SUPERSCRIPT TWO
580 case 0xb3: // ³ SUPERSCRIPT THREE
581 case 0xd7: // × MULTIPLICATION SIGN
582 case 0xf7: // ÷ DIVISION SIGN
583 case 0xb9: // ¹ SUPERSCRIPT ONE
584 case 0xac: // ¬ NOT SIGN
585 case 0xb5: // µ MICRO SIGN
586 if (isEncoding(bparams, font, "latin1")
587 || isEncoding(bparams, font, "latin9")) {
588 os << "\\ensuremath{";
597 case '|': case '<': case '>':
598 // In T1 encoding, these characters exist
599 if (lyxrc.fontenc == "T1") {
601 //... but we should avoid ligatures
602 if ((c == '>' || c == '<')
604 && getChar(i + 1) == c) {
605 //os << "\\textcompwordmark{}";
606 // Jean-Marc, have a look at
607 // this. I think this works
615 // Typewriter font also has them
616 if (font.family() == LyXFont::TYPEWRITER_FAMILY) {
620 // Otherwise, we use what LaTeX
624 os << "\\textless{}";
628 os << "\\textgreater{}";
638 case '-': // "--" in Typewriter mode -> "-{}-"
640 && getChar(i + 1) == '-'
641 && font.family() == LyXFont::TYPEWRITER_FAMILY) {
650 os << "\\char`\\\"{}";
654 case 0xa3: // £ POUND SIGN
655 if (bparams.inputenc == "default") {
663 case 0x20ac: // EURO SIGN
664 if (isEncoding(bparams, font, "latin9")
665 || isEncoding(bparams, font, "cp1251")
666 || isEncoding(bparams, font, "utf8")) {
669 os << "\\texteuro{}";
674 // These characters are covered by latin1, but not
675 // by latin9 (a.o.). We have to support them because
676 // we switched the default of latin1-languages to latin9
677 case 0xa4: // CURRENCY SYMBOL
678 case 0xa6: // BROKEN BAR
679 case 0xa8: // DIAERESIS
680 case 0xb4: // ACUTE ACCENT
681 case 0xb8: // CEDILLA
682 case 0xbd: // 1/2 FRACTION
683 case 0xbc: // 1/4 FRACTION
684 case 0xbe: // 3/4 FRACTION
685 if (isEncoding(bparams, font, "latin1")
686 || isEncoding(bparams, font, "latin5")
687 || isEncoding(bparams, font, "utf8")) {
693 os << "\\textcurrency{}";
697 os << "\\textbrokenbar{}";
701 os << "\\textasciidieresis{}";
705 os << "\\textasciiacute{}";
708 case 0xb8: // from latin1.def:
713 os << "\\textonehalf{}";
717 os << "\\textonequarter{}";
721 os << "\\textthreequarters{}";
729 case '%': case '#': case '{':
737 os << "\\textasciitilde{}";
742 os << "\\textasciicircum{}";
747 // avoid being mistaken for optional arguments
755 // Blanks are printed before font switching.
756 // Sure? I am not! (try nice-latex)
757 // I am sure it's correct. LyX might be smarter
758 // in the future, but for now, nothing wrong is
764 // I assume this is hack treating typewriter as verbatim
765 if (font.family() == LyXFont::TYPEWRITER_FAMILY) {
774 // FIXME: if we have "LaTeX" with a font
775 // change in the middle (before the 'T', then
776 // the "TeX" part is still special cased.
777 // Really we should only operate this on
778 // "words" for some definition of word
782 for (; pnr < phrases_nr; ++pnr) {
783 if (isTextAt(special_phrases[pnr].phrase, i)) {
784 os << special_phrases[pnr].macro;
785 i += special_phrases[pnr].phrase.length() - 1;
786 column += special_phrases[pnr].macro.length() - 1;
791 if (pnr == phrases_nr && c != '\0') {
800 void Paragraph::Pimpl::validate(LaTeXFeatures & features,
801 LyXLayout const & layout) const
803 BufferParams const & bparams = features.bufferParams();
806 if (!params.spacing().isDefault())
807 features.require("setspace");
810 features.useLayout(layout.name());
813 Language const * doc_language = bparams.language;
815 FontList::const_iterator fcit = fontlist.begin();
816 FontList::const_iterator fend = fontlist.end();
817 for (; fcit != fend; ++fcit) {
818 if (fcit->font().noun() == LyXFont::ON) {
819 lyxerr[Debug::LATEX] << "font.noun: "
820 << fcit->font().noun()
822 features.require("noun");
823 lyxerr[Debug::LATEX] << "Noun enabled. Font: "
824 << fcit->font().stateText(0)
827 switch (fcit->font().color()) {
829 case LColor::inherit:
831 // probably we should put here all interface colors used for
832 // font displaying! For now I just add this ones I know of (Jug)
837 features.require("color");
838 lyxerr[Debug::LATEX] << "Color enabled. Font: "
839 << fcit->font().stateText(0)
843 Language const * language = fcit->font().language();
844 if (language->babel() != doc_language->babel() &&
845 language != ignore_language &&
846 language != latex_language)
848 features.useLanguage(language);
849 lyxerr[Debug::LATEX] << "Found language "
850 << language->babel() << endl;
854 if (!params.leftIndent().zero())
855 features.require("ParagraphLeftIndent");
858 InsetList::const_iterator icit = owner_->insetlist.begin();
859 InsetList::const_iterator iend = owner_->insetlist.end();
860 for (; icit != iend; ++icit) {
862 icit->inset->validate(features);
863 if (layout.needprotect &&
864 icit->inset->lyxCode() == InsetBase::FOOT_CODE)
865 features.require("NeedLyXFootnoteCode");
870 for (pos_type i = 0; i < size() ; ++i) {
871 for (size_t pnr = 0; pnr < phrases_nr; ++pnr) {
872 if (!special_phrases[pnr].builtin
873 && isTextAt(special_phrases[pnr].phrase, i)) {
874 features.require(special_phrases[pnr].phrase);
878 // these glyphs require the textcomp package
879 if (getChar(i) == 0x20ac || getChar(i) == 0xa4
880 || getChar(i) == 0xa6 || getChar(i) == 0xa8
881 || getChar(i) == 0xb4 || getChar(i) == 0xbd
882 || getChar(i) == 0xbc || getChar(i) == 0xbe)
883 features.require("textcomp");