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);
64 Paragraph::Pimpl::Pimpl(Paragraph * owner)
72 Paragraph::Pimpl::Pimpl(Pimpl const & p, Paragraph * owner)
73 : params(p.params), changes_(p.changes_), owner_(owner)
75 inset_owner = p.inset_owner;
76 fontlist = p.fontlist;
81 bool Paragraph::Pimpl::isChanged(pos_type start, pos_type end) const
83 BOOST_ASSERT(start >= 0 && start <= size());
84 BOOST_ASSERT(end > start && end <= size() + 1);
86 return changes_.isChanged(start, end);
90 bool Paragraph::Pimpl::isMergedOnEndOfParDeletion(bool trackChanges) const {
91 // keep the logic here in sync with the logic of eraseChars()
97 Change change = changes_.lookup(size());
99 return change.type == Change::INSERTED && change.author == 0;
103 void Paragraph::Pimpl::setChange(Change const & change)
105 // beware of the imaginary end-of-par character!
106 changes_.set(change, 0, size() + 1);
109 * Propagate the change recursively - but not in case of DELETED!
111 * Imagine that your co-author makes changes in an existing inset. He
112 * sends your document to you and you come to the conclusion that the
113 * inset should go completely. If you erase it, LyX must not delete all
114 * text within the inset. Otherwise, the change tracked insertions of
115 * your co-author get lost and there is no way to restore them later.
117 * Conclusion: An inset's content should remain untouched if you delete it
120 if (change.type != Change::DELETED) {
121 for (pos_type pos = 0; pos < size(); ++pos) {
122 if (owner_->isInset(pos)) {
123 owner_->getInset(pos)->setChange(change);
130 void Paragraph::Pimpl::setChange(pos_type pos, Change const & change)
132 BOOST_ASSERT(pos >= 0 && pos <= size());
134 changes_.set(change, pos);
136 // see comment in setChange(Change const &) above
138 if (change.type != Change::DELETED &&
139 pos < size() && owner_->isInset(pos)) {
140 owner_->getInset(pos)->setChange(change);
145 Change const Paragraph::Pimpl::lookupChange(pos_type pos) const
147 BOOST_ASSERT(pos >= 0 && pos <= size());
149 return changes_.lookup(pos);
153 void Paragraph::Pimpl::acceptChanges(pos_type start, pos_type end)
155 BOOST_ASSERT(start >= 0 && start <= size());
156 BOOST_ASSERT(end > start && end <= size() + 1);
158 for (pos_type pos = start; pos < end; ++pos) {
159 switch (lookupChange(pos).type) {
160 case Change::UNCHANGED:
163 case Change::INSERTED:
164 changes_.set(Change(Change::UNCHANGED), pos);
167 case Change::DELETED:
168 // Suppress access to non-existent
169 // "end-of-paragraph char"
171 eraseChar(pos, false);
178 // also accept changes in nested insets
179 if (pos < size() && owner_->isInset(pos)) {
180 owner_->getInset(pos)->acceptChanges();
186 void Paragraph::Pimpl::rejectChanges(pos_type start, pos_type end)
188 BOOST_ASSERT(start >= 0 && start <= size());
189 BOOST_ASSERT(end > start && end <= size() + 1);
191 for (pos_type pos = start; pos < end; ++pos) {
192 switch (lookupChange(pos).type) {
193 case Change::UNCHANGED:
194 // also reject changes inside of insets
195 if (pos < size() && owner_->isInset(pos)) {
196 owner_->getInset(pos)->rejectChanges();
200 case Change::INSERTED:
201 // Suppress access to non-existent
202 // "end-of-paragraph char"
204 eraseChar(pos, false);
210 case Change::DELETED:
211 changes_.set(Change(Change::UNCHANGED), pos);
213 // Do NOT reject changes within a deleted inset!
214 // There may be insertions of a co-author inside of it!
222 Paragraph::value_type Paragraph::Pimpl::getChar(pos_type pos) const
224 BOOST_ASSERT(pos >= 0 && pos <= size());
226 return owner_->getChar(pos);
230 void Paragraph::Pimpl::insertChar(pos_type pos, value_type c, Change const & change)
232 BOOST_ASSERT(pos >= 0 && pos <= size());
235 changes_.insert(change, pos);
237 // This is actually very common when parsing buffers (and
238 // maybe inserting ascii text)
240 // when appending characters, no need to update tables
241 owner_->text_.push_back(c);
245 owner_->text_.insert(owner_->text_.begin() + pos, c);
247 // Update the font table.
248 FontTable search_font(pos, LyXFont());
249 for (FontList::iterator it
250 = lower_bound(fontlist.begin(), fontlist.end(), search_font, matchFT());
251 it != fontlist.end(); ++it)
253 it->pos(it->pos() + 1);
257 owner_->insetlist.increasePosAfterPos(pos);
261 void Paragraph::Pimpl::insertInset(pos_type pos, InsetBase * inset,
262 Change const & change)
265 BOOST_ASSERT(pos >= 0 && pos <= size());
267 insertChar(pos, META_INSET, change);
268 BOOST_ASSERT(owner_->text_[pos] == META_INSET);
270 // Add a new entry in the insetlist.
271 owner_->insetlist.insert(inset, pos);
275 bool Paragraph::Pimpl::eraseChar(pos_type pos, bool trackChanges)
277 BOOST_ASSERT(pos >= 0 && pos <= size());
279 // keep the logic here in sync with the logic of isMergedOnEndOfParDeletion()
282 Change change = changes_.lookup(pos);
284 // set the character to DELETED if
285 // a) it was previously unchanged or
286 // b) it was inserted by a co-author
288 if (change.type == Change::UNCHANGED ||
289 (change.type == Change::INSERTED && change.author != 0)) {
290 setChange(pos, Change(Change::DELETED));
294 if (change.type == Change::DELETED)
298 // Don't physically access the imaginary end-of-paragraph character.
299 // eraseChar() can only mark it as DELETED. A physical deletion of
300 // end-of-par must be handled externally.
308 // if it is an inset, delete the inset entry
309 if (owner_->text_[pos] == Paragraph::META_INSET) {
310 owner_->insetlist.erase(pos);
313 owner_->text_.erase(owner_->text_.begin() + pos);
315 // Erase entries in the tables.
316 FontTable search_font(pos, LyXFont());
318 FontList::iterator it =
319 lower_bound(fontlist.begin(),
321 search_font, matchFT());
322 if (it != fontlist.end() && it->pos() == pos &&
324 (it != fontlist.begin()
325 && boost::prior(it)->pos() == pos - 1))) {
326 // If it is a multi-character font
327 // entry, we just make it smaller
328 // (see update below), otherwise we
330 unsigned int const i = it - fontlist.begin();
331 fontlist.erase(fontlist.begin() + i);
332 it = fontlist.begin() + i;
333 if (i > 0 && i < fontlist.size() &&
334 fontlist[i - 1].font() == fontlist[i].font()) {
335 fontlist.erase(fontlist.begin() + i - 1);
336 it = fontlist.begin() + i - 1;
340 // Update all other entries
341 FontList::iterator fend = fontlist.end();
342 for (; it != fend; ++it)
343 it->pos(it->pos() - 1);
345 // Update the insetlist
346 owner_->insetlist.decreasePosAfterPos(pos);
352 int Paragraph::Pimpl::eraseChars(pos_type start, pos_type end, bool trackChanges)
354 BOOST_ASSERT(start >= 0 && start <= size());
355 BOOST_ASSERT(end >= start && end <= size() + 1);
358 for (pos_type count = end - start; count; --count) {
359 if (!eraseChar(i, trackChanges))
366 void Paragraph::Pimpl::simpleTeXBlanks(odocstream & os, TexRow & texrow,
368 unsigned int & column,
369 LyXFont const & font,
370 LyXLayout const & style)
375 if (column > lyxrc.ascii_linelen
377 && getChar(i - 1) != ' '
379 // same in FreeSpacing mode
380 && !owner_->isFreeSpacing()
381 // In typewriter mode, we want to avoid
382 // ! . ? : at the end of a line
383 && !(font.family() == LyXFont::TYPEWRITER_FAMILY
384 && (getChar(i - 1) == '.'
385 || getChar(i - 1) == '?'
386 || getChar(i - 1) == ':'
387 || getChar(i - 1) == '!'))) {
390 texrow.start(owner_->id(), i + 1);
392 } else if (style.free_spacing) {
400 bool Paragraph::Pimpl::isTextAt(string const & str, pos_type pos) const
402 pos_type const len = str.length();
404 // is the paragraph large enough?
405 if (pos + len > size())
408 // does the wanted text start at point?
409 for (string::size_type i = 0; i < str.length(); ++i) {
410 if (str[i] != owner_->text_[pos + i])
414 // is there a font change in middle of the word?
415 FontList::const_iterator cit = fontlist.begin();
416 FontList::const_iterator end = fontlist.end();
417 for (; cit != end; ++cit) {
418 if (cit->pos() >= pos)
421 if (cit != end && pos + len - 1 > cit->pos())
428 void Paragraph::Pimpl::simpleTeXSpecialChars(Buffer const & buf,
429 BufferParams const & bparams,
432 OutputParams const & runparams,
434 LyXFont & running_font,
436 LyXFont const & outerfont,
438 Change::Type & running_change,
439 LyXLayout const & style,
441 unsigned int & column,
444 if (style.pass_thru) {
445 if (c != Paragraph::META_INSET) {
449 owner_->getInset(i)->plaintext(buf, os, runparams);
453 // Two major modes: LaTeX or plain
454 // Handle here those cases common to both modes
455 // and then split to handle the two modes separately.
457 case Paragraph::META_INSET: {
458 InsetBase * inset = owner_->getInset(i);
460 // FIXME: remove this check
464 // FIXME: move this to InsetNewline::latex
465 if (inset->lyxCode() == InsetBase::NEWLINE_CODE) {
466 // newlines are handled differently here than
467 // the default in simpleTeXSpecialChars().
468 if (!style.newline_allowed) {
472 column += running_font.latexWriteEndChanges(os, basefont, basefont);
475 basefont = owner_->getLayoutFont(bparams, outerfont);
476 running_font = basefont;
478 if (font.family() == LyXFont::TYPEWRITER_FAMILY)
481 if (runparams.moving_arg)
487 texrow.start(owner_->id(), i + 1);
492 // output change tracking marks only if desired,
493 // if dvipost is installed,
494 // and with dvi/ps (other formats don't work)
495 LaTeXFeatures features(buf, bparams, runparams);
496 bool const output = bparams.outputChanges
497 && runparams.flavor == OutputParams::LATEX
498 && features.isAvailable("dvipost");
500 if (inset->canTrackChanges()) {
501 column += Changes::latexMarkChange(os, running_change,
502 Change::UNCHANGED, output);
503 running_change = Change::UNCHANGED;
507 odocstream::pos_type const len = os.tellp();
509 if ((inset->lyxCode() == InsetBase::GRAPHICS_CODE
510 || inset->lyxCode() == InsetBase::MATH_CODE
511 || inset->lyxCode() == InsetBase::URL_CODE)
512 && running_font.isRightToLeft()) {
518 #warning Bug: we can have an empty font change here!
519 // if there has just been a font change, we are going to close it
520 // right now, which means stupid latex code like \textsf{}. AFAIK,
521 // this does not harm dvi output. A minor bug, thus (JMarc)
523 // some insets cannot be inside a font change command
524 if (open_font && inset->noFontChange()) {
525 column +=running_font.
526 latexWriteEndChanges(os,
530 basefont = owner_->getLayoutFont(bparams, outerfont);
531 running_font = basefont;
534 int tmp = inset->latex(buf, os, runparams);
540 for (int j = 0; j < tmp; ++j) {
543 texrow.start(owner_->id(), i + 1);
546 column += os.tellp() - len;
552 // And now for the special cases within each mode
556 os << "\\textbackslash{}";
560 // The following characters could be written literally in latin1, but they
561 // would be wrongly converted on systems where char is signed, so we give
563 // This also makes us independant from the encoding of this source file.
564 case 0xb1: // ± PLUS-MINUS SIGN
565 case 0xb2: // ² SUPERSCRIPT TWO
566 case 0xb3: // ³ SUPERSCRIPT THREE
567 case 0xd7: // × MULTIPLICATION SIGN
568 case 0xf7: // ÷ DIVISION SIGN
569 case 0xb9: // ¹ SUPERSCRIPT ONE
570 case 0xac: // ¬ NOT SIGN
571 case 0xb5: // µ MICRO SIGN
572 if ((bparams.inputenc == "latin1" ||
573 bparams.inputenc == "latin9") ||
574 (bparams.inputenc == "auto" &&
575 (font.language()->encoding()->latexName()
577 font.language()->encoding()->latexName()
579 os << "\\ensuremath{";
588 case '|': case '<': case '>':
589 // In T1 encoding, these characters exist
590 if (lyxrc.fontenc == "T1") {
592 //... but we should avoid ligatures
593 if ((c == '>' || c == '<')
595 && getChar(i + 1) == c) {
596 //os << "\\textcompwordmark{}";
597 // Jean-Marc, have a look at
598 // this. I think this works
606 // Typewriter font also has them
607 if (font.family() == LyXFont::TYPEWRITER_FAMILY) {
611 // Otherwise, we use what LaTeX
615 os << "\\textless{}";
619 os << "\\textgreater{}";
629 case '-': // "--" in Typewriter mode -> "-{}-"
631 && getChar(i + 1) == '-'
632 && font.family() == LyXFont::TYPEWRITER_FAMILY) {
641 os << "\\char`\\\"{}";
645 case 0xa3: // £ POUND SIGN
646 if (bparams.inputenc == "default") {
655 case '%': case '#': case '{':
663 os << "\\textasciitilde{}";
668 os << "\\textasciicircum{}";
673 // avoid being mistaken for optional arguments
681 // Blanks are printed before font switching.
682 // Sure? I am not! (try nice-latex)
683 // I am sure it's correct. LyX might be smarter
684 // in the future, but for now, nothing wrong is
690 // I assume this is hack treating typewriter as verbatim
691 if (font.family() == LyXFont::TYPEWRITER_FAMILY) {
700 // FIXME: if we have "LaTeX" with a font
701 // change in the middle (before the 'T', then
702 // the "TeX" part is still special cased.
703 // Really we should only operate this on
704 // "words" for some definition of word
708 for (; pnr < phrases_nr; ++pnr) {
709 if (isTextAt(special_phrases[pnr].phrase, i)) {
710 os << special_phrases[pnr].macro;
711 i += special_phrases[pnr].phrase.length() - 1;
712 column += special_phrases[pnr].macro.length() - 1;
717 if (pnr == phrases_nr && c != '\0') {
726 void Paragraph::Pimpl::validate(LaTeXFeatures & features,
727 LyXLayout const & layout) const
729 BufferParams const & bparams = features.bufferParams();
732 if (!params.spacing().isDefault())
733 features.require("setspace");
736 features.useLayout(layout.name());
739 Language const * doc_language = bparams.language;
741 FontList::const_iterator fcit = fontlist.begin();
742 FontList::const_iterator fend = fontlist.end();
743 for (; fcit != fend; ++fcit) {
744 if (fcit->font().noun() == LyXFont::ON) {
745 lyxerr[Debug::LATEX] << "font.noun: "
746 << fcit->font().noun()
748 features.require("noun");
749 lyxerr[Debug::LATEX] << "Noun enabled. Font: "
750 << fcit->font().stateText(0)
753 switch (fcit->font().color()) {
755 case LColor::inherit:
757 // probably we should put here all interface colors used for
758 // font displaying! For now I just add this ones I know of (Jug)
763 features.require("color");
764 lyxerr[Debug::LATEX] << "Color enabled. Font: "
765 << fcit->font().stateText(0)
769 Language const * language = fcit->font().language();
770 if (language->babel() != doc_language->babel() &&
771 language != ignore_language &&
772 language != latex_language)
774 features.useLanguage(language);
775 lyxerr[Debug::LATEX] << "Found language "
776 << language->babel() << endl;
780 if (!params.leftIndent().zero())
781 features.require("ParagraphLeftIndent");
784 InsetList::const_iterator icit = owner_->insetlist.begin();
785 InsetList::const_iterator iend = owner_->insetlist.end();
786 for (; icit != iend; ++icit) {
788 icit->inset->validate(features);
789 if (layout.needprotect &&
790 icit->inset->lyxCode() == InsetBase::FOOT_CODE)
791 features.require("NeedLyXFootnoteCode");
796 for (pos_type i = 0; i < size() ; ++i) {
797 for (size_t pnr = 0; pnr < phrases_nr; ++pnr) {
798 if (!special_phrases[pnr].builtin
799 && isTextAt(special_phrases[pnr].phrase, i)) {
800 features.require(special_phrases[pnr].phrase);