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(BufferParams const & bparams, 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:
161 // accept changes in nested inset
162 if (pos < size() && owner_->isInset(pos)) {
163 owner_->getInset(pos)->acceptChanges(bparams);
168 case Change::INSERTED:
169 changes_.set(Change(Change::UNCHANGED), pos);
170 // also accept changes in nested inset
171 if (pos < size() && owner_->isInset(pos)) {
172 owner_->getInset(pos)->acceptChanges(bparams);
176 case Change::DELETED:
177 // Suppress access to non-existent
178 // "end-of-paragraph char"
180 eraseChar(pos, false);
191 void Paragraph::Pimpl::rejectChanges(BufferParams const & bparams, pos_type start, pos_type end)
193 BOOST_ASSERT(start >= 0 && start <= size());
194 BOOST_ASSERT(end > start && end <= size() + 1);
196 for (pos_type pos = start; pos < end; ++pos) {
197 switch (lookupChange(pos).type) {
198 case Change::UNCHANGED:
199 // reject changes in nested inset
200 if (pos < size() && owner_->isInset(pos)) {
201 owner_->getInset(pos)->rejectChanges(bparams);
205 case Change::INSERTED:
206 // Suppress access to non-existent
207 // "end-of-paragraph char"
209 eraseChar(pos, false);
215 case Change::DELETED:
216 changes_.set(Change(Change::UNCHANGED), pos);
218 // Do NOT reject changes within a deleted inset!
219 // There may be insertions of a co-author inside of it!
227 Paragraph::value_type Paragraph::Pimpl::getChar(pos_type pos) const
229 BOOST_ASSERT(pos >= 0 && pos <= size());
231 return owner_->getChar(pos);
235 void Paragraph::Pimpl::insertChar(pos_type pos, value_type c, Change const & change)
237 BOOST_ASSERT(pos >= 0 && pos <= size());
240 changes_.insert(change, pos);
242 // This is actually very common when parsing buffers (and
243 // maybe inserting ascii text)
245 // when appending characters, no need to update tables
246 owner_->text_.push_back(c);
250 owner_->text_.insert(owner_->text_.begin() + pos, c);
252 // Update the font table.
253 FontTable search_font(pos, LyXFont());
254 for (FontList::iterator it
255 = lower_bound(fontlist.begin(), fontlist.end(), search_font, matchFT());
256 it != fontlist.end(); ++it)
258 it->pos(it->pos() + 1);
262 owner_->insetlist.increasePosAfterPos(pos);
266 void Paragraph::Pimpl::insertInset(pos_type pos, InsetBase * inset,
267 Change const & change)
270 BOOST_ASSERT(pos >= 0 && pos <= size());
272 insertChar(pos, META_INSET, change);
273 BOOST_ASSERT(owner_->text_[pos] == META_INSET);
275 // Add a new entry in the insetlist.
276 owner_->insetlist.insert(inset, pos);
280 bool Paragraph::Pimpl::eraseChar(pos_type pos, bool trackChanges)
282 BOOST_ASSERT(pos >= 0 && pos <= size());
284 // keep the logic here in sync with the logic of isMergedOnEndOfParDeletion()
287 Change change = changes_.lookup(pos);
289 // set the character to DELETED if
290 // a) it was previously unchanged or
291 // b) it was inserted by a co-author
293 if (change.type == Change::UNCHANGED ||
294 (change.type == Change::INSERTED && change.author != 0)) {
295 setChange(pos, Change(Change::DELETED));
299 if (change.type == Change::DELETED)
303 // Don't physically access the imaginary end-of-paragraph character.
304 // eraseChar() can only mark it as DELETED. A physical deletion of
305 // end-of-par must be handled externally.
313 // if it is an inset, delete the inset entry
314 if (owner_->text_[pos] == Paragraph::META_INSET) {
315 owner_->insetlist.erase(pos);
318 owner_->text_.erase(owner_->text_.begin() + pos);
320 // Erase entries in the tables.
321 FontTable search_font(pos, LyXFont());
323 FontList::iterator it =
324 lower_bound(fontlist.begin(),
326 search_font, matchFT());
327 if (it != fontlist.end() && it->pos() == pos &&
329 (it != fontlist.begin()
330 && boost::prior(it)->pos() == pos - 1))) {
331 // If it is a multi-character font
332 // entry, we just make it smaller
333 // (see update below), otherwise we
335 unsigned int const i = it - fontlist.begin();
336 fontlist.erase(fontlist.begin() + i);
337 it = fontlist.begin() + i;
338 if (i > 0 && i < fontlist.size() &&
339 fontlist[i - 1].font() == fontlist[i].font()) {
340 fontlist.erase(fontlist.begin() + i - 1);
341 it = fontlist.begin() + i - 1;
345 // Update all other entries
346 FontList::iterator fend = fontlist.end();
347 for (; it != fend; ++it)
348 it->pos(it->pos() - 1);
350 // Update the insetlist
351 owner_->insetlist.decreasePosAfterPos(pos);
357 int Paragraph::Pimpl::eraseChars(pos_type start, pos_type end, bool trackChanges)
359 BOOST_ASSERT(start >= 0 && start <= size());
360 BOOST_ASSERT(end >= start && end <= size() + 1);
363 for (pos_type count = end - start; count; --count) {
364 if (!eraseChar(i, trackChanges))
371 int Paragraph::Pimpl::latexSurrogatePair(odocstream & os, value_type c,
372 value_type next, Encoding const & encoding)
374 // Writing next here may circumvent a possible font change between
375 // c and next. Since next is only output if it forms a surrogate pair
376 // with c we can ignore this:
377 // A font change inside a surrogate pair does not make sense and is
378 // hopefully impossible to input.
379 // FIXME: change tracking
380 // Is this correct WRT change tracking?
381 docstring const latex1 = encoding.latexChar(next);
382 docstring const latex2 = encoding.latexChar(c);
383 os << latex1 << '{' << latex2 << '}';
384 return latex1.length() + latex2.length() + 2;
388 bool Paragraph::Pimpl::simpleTeXBlanks(BufferParams const & bparams,
389 Encoding const & encoding,
390 odocstream & os, TexRow & texrow,
392 unsigned int & column,
393 LyXFont const & font,
394 LyXLayout const & style)
399 if (i < size() - 1) {
400 char_type next = getChar(i + 1);
401 if (Encodings::isCombiningChar(next)) {
402 // This space has an accent, so we must always output it.
403 column += latexSurrogatePair(os, ' ', next, encoding) - 1;
409 if (lyxrc.plaintext_linelen > 0
410 && column > lyxrc.plaintext_linelen
412 && getChar(i - 1) != ' '
414 // same in FreeSpacing mode
415 && !owner_->isFreeSpacing()
416 // In typewriter mode, we want to avoid
417 // ! . ? : at the end of a line
418 && !(font.family() == LyXFont::TYPEWRITER_FAMILY
419 && (getChar(i - 1) == '.'
420 || getChar(i - 1) == '?'
421 || getChar(i - 1) == ':'
422 || getChar(i - 1) == '!'))) {
425 texrow.start(owner_->id(), i + 1);
427 } else if (style.free_spacing) {
436 bool Paragraph::Pimpl::isTextAt(string const & str, pos_type pos) const
438 pos_type const len = str.length();
440 // is the paragraph large enough?
441 if (pos + len > size())
444 // does the wanted text start at point?
445 for (string::size_type i = 0; i < str.length(); ++i) {
446 // Caution: direct comparison of characters works only
447 // because str is pure ASCII.
448 if (str[i] != owner_->text_[pos + i])
452 // is there a font change in middle of the word?
453 FontList::const_iterator cit = fontlist.begin();
454 FontList::const_iterator end = fontlist.end();
455 for (; cit != end; ++cit) {
456 if (cit->pos() >= pos)
459 if (cit != end && pos + len - 1 > cit->pos())
466 void Paragraph::Pimpl::simpleTeXSpecialChars(Buffer const & buf,
467 BufferParams const & bparams,
470 OutputParams const & runparams,
471 LyXFont & running_font,
473 LyXFont const & outerfont,
475 Change::Type & running_change,
476 LyXLayout const & style,
478 unsigned int & column,
481 if (style.pass_thru) {
482 if (c != Paragraph::META_INSET) {
484 // FIXME UNICODE: This can fail if c cannot
485 // be encoded in the current encoding.
488 owner_->getInset(i)->plaintext(buf, os, runparams);
492 // Two major modes: LaTeX or plain
493 // Handle here those cases common to both modes
494 // and then split to handle the two modes separately.
496 case Paragraph::META_INSET: {
497 InsetBase * inset = owner_->getInset(i);
499 // FIXME: remove this check
503 // FIXME: move this to InsetNewline::latex
504 if (inset->lyxCode() == InsetBase::NEWLINE_CODE) {
505 // newlines are handled differently here than
506 // the default in simpleTeXSpecialChars().
507 if (!style.newline_allowed) {
511 column += running_font.latexWriteEndChanges(
512 os, basefont, basefont);
516 if (running_font.family() == LyXFont::TYPEWRITER_FAMILY)
519 basefont = owner_->getLayoutFont(bparams, outerfont);
520 running_font = basefont;
522 if (runparams.moving_arg)
528 texrow.start(owner_->id(), i + 1);
533 // output change tracking marks only if desired,
534 // if dvipost is installed,
535 // and with dvi/ps (other formats don't work)
536 LaTeXFeatures features(buf, bparams, runparams);
537 bool const output = bparams.outputChanges
538 && runparams.flavor == OutputParams::LATEX
539 && features.isAvailable("dvipost");
541 if (inset->canTrackChanges()) {
542 column += Changes::latexMarkChange(os, running_change,
543 Change::UNCHANGED, output);
544 running_change = Change::UNCHANGED;
548 odocstream::pos_type const len = os.tellp();
550 if ((inset->lyxCode() == InsetBase::GRAPHICS_CODE
551 || inset->lyxCode() == InsetBase::MATH_CODE
552 || inset->lyxCode() == InsetBase::URL_CODE)
553 && running_font.isRightToLeft()) {
559 #warning Bug: we can have an empty font change here!
560 // if there has just been a font change, we are going to close it
561 // right now, which means stupid latex code like \textsf{}. AFAIK,
562 // this does not harm dvi output. A minor bug, thus (JMarc)
564 // some insets cannot be inside a font change command
565 if (open_font && inset->noFontChange()) {
566 column += running_font.latexWriteEndChanges(
567 os, basefont, basefont);
569 basefont = owner_->getLayoutFont(bparams, outerfont);
570 running_font = basefont;
573 int tmp = inset->latex(buf, os, runparams);
579 for (int j = 0; j < tmp; ++j) {
582 texrow.start(owner_->id(), i + 1);
585 column += os.tellp() - len;
591 // And now for the special cases within each mode
595 os << "\\textbackslash{}";
599 // The following characters could be written literally in latin1, but they
600 // would be wrongly converted on systems where char is signed, so we give
602 // This also makes us independant from the encoding of this source file.
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 (running_font.family() == LyXFont::TYPEWRITER_FAMILY) {
627 // Otherwise, we use what LaTeX
631 os << "\\textless{}";
635 os << "\\textgreater{}";
645 case '-': // "--" in Typewriter mode -> "-{}-"
646 if (i <= size() - 2 &&
647 getChar(i + 1) == '-' &&
648 running_font.family() == LyXFont::TYPEWRITER_FAMILY) {
657 os << "\\char`\\\"{}";
662 case '%': case '#': case '{':
670 os << "\\textasciitilde{}";
675 os << "\\textasciicircum{}";
680 // avoid being mistaken for optional arguments
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 // FIXME UNICODE: This can fail if c cannot be encoded
699 // in the current encoding.
700 if (running_font.family() == LyXFont::TYPEWRITER_FAMILY) {
709 // FIXME: if we have "LaTeX" with a font
710 // change in the middle (before the 'T', then
711 // the "TeX" part is still special cased.
712 // Really we should only operate this on
713 // "words" for some definition of word
717 for (; pnr < phrases_nr; ++pnr) {
718 if (isTextAt(special_phrases[pnr].phrase, i)) {
719 os << special_phrases[pnr].macro;
720 i += special_phrases[pnr].phrase.length() - 1;
721 column += special_phrases[pnr].macro.length() - 1;
726 if (pnr == phrases_nr && c != '\0') {
727 Encoding const & encoding = *(runparams.encoding);
728 if (i < size() - 1) {
729 char_type next = getChar(i + 1);
730 if (Encodings::isCombiningChar(next)) {
731 column += latexSurrogatePair(os, c, next, encoding) - 1;
736 docstring const latex = encoding.latexChar(c);
737 if (latex.length() > 1 &&
738 latex[latex.length() - 1] != '}') {
739 // Prevent eating of a following
740 // space or command corruption by
741 // following characters
742 column += latex.length() + 1;
745 column += latex.length() - 1;
755 void Paragraph::Pimpl::validate(LaTeXFeatures & features,
756 LyXLayout const & layout) const
758 BufferParams const & bparams = features.bufferParams();
761 if (!params.spacing().isDefault())
762 features.require("setspace");
765 features.useLayout(layout.name());
768 Language const * doc_language = bparams.language;
770 FontList::const_iterator fcit = fontlist.begin();
771 FontList::const_iterator fend = fontlist.end();
772 for (; fcit != fend; ++fcit) {
773 if (fcit->font().noun() == LyXFont::ON) {
774 LYXERR(Debug::LATEX) << "font.noun: "
775 << fcit->font().noun()
777 features.require("noun");
778 LYXERR(Debug::LATEX) << "Noun enabled. Font: "
779 << to_utf8(fcit->font().stateText(0))
782 switch (fcit->font().color()) {
784 case LColor::inherit:
786 // probably we should put here all interface colors used for
787 // font displaying! For now I just add this ones I know of (Jug)
792 features.require("color");
793 LYXERR(Debug::LATEX) << "Color enabled. Font: "
794 << to_utf8(fcit->font().stateText(0))
798 Language const * language = fcit->font().language();
799 if (language->babel() != doc_language->babel() &&
800 language != ignore_language &&
801 language != latex_language)
803 features.useLanguage(language);
804 LYXERR(Debug::LATEX) << "Found language "
805 << language->babel() << endl;
809 if (!params.leftIndent().zero())
810 features.require("ParagraphLeftIndent");
813 InsetList::const_iterator icit = owner_->insetlist.begin();
814 InsetList::const_iterator iend = owner_->insetlist.end();
815 for (; icit != iend; ++icit) {
817 icit->inset->validate(features);
818 if (layout.needprotect &&
819 icit->inset->lyxCode() == InsetBase::FOOT_CODE)
820 features.require("NeedLyXFootnoteCode");
825 for (pos_type i = 0; i < size() ; ++i) {
826 for (size_t pnr = 0; pnr < phrases_nr; ++pnr) {
827 if (!special_phrases[pnr].builtin
828 && isTextAt(special_phrases[pnr].phrase, i)) {
829 features.require(special_phrases[pnr].phrase);
833 Encodings::validate(getChar(i), features);