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 void Paragraph::Pimpl::setChange(Change const & change)
92 // beware of the imaginary end-of-par character!
93 changes_.set(change, 0, size() + 1);
96 * Propagate the change recursively - but not in case of DELETED!
98 * Imagine that your co-author makes changes in an existing inset. He
99 * sends your document to you and you come to the conclusion that the
100 * inset should go completely. If you erase it, LyX must not delete all
101 * text within the inset. Otherwise, the change tracked insertions of
102 * your co-author get lost and there is no way to restore them later.
104 * Conclusion: An inset's content should remain untouched if you delete it
107 if (change.type != Change::DELETED) {
108 for (pos_type pos = 0; pos < size(); ++pos) {
109 if (owner_->isInset(pos)) {
110 owner_->getInset(pos)->setChange(change);
117 void Paragraph::Pimpl::setChange(pos_type pos, Change const & change)
119 BOOST_ASSERT(pos >= 0 && pos <= size());
121 changes_.set(change, pos);
123 // see comment in setChange(Change const &) above
125 if (change.type != Change::DELETED &&
126 pos < size() && owner_->isInset(pos)) {
127 owner_->getInset(pos)->setChange(change);
132 Change const Paragraph::Pimpl::lookupChange(pos_type pos) const
134 BOOST_ASSERT(pos >= 0 && pos <= size());
136 return changes_.lookup(pos);
140 void Paragraph::Pimpl::acceptChanges(pos_type start, pos_type end)
142 BOOST_ASSERT(start >= 0 && start <= size());
143 BOOST_ASSERT(end > start && end <= size() + 1);
145 for (pos_type pos = start; pos < end; ++pos) {
146 switch (lookupChange(pos).type) {
147 case Change::UNCHANGED:
150 case Change::INSERTED:
151 changes_.set(Change(Change::UNCHANGED), pos);
154 case Change::DELETED:
155 // Suppress access to non-existent
156 // "end-of-paragraph char"
158 eraseChar(pos, false);
165 // also accept changes in nested insets
166 if (pos < size() && owner_->isInset(pos)) {
167 owner_->getInset(pos)->acceptChanges();
173 void Paragraph::Pimpl::rejectChanges(pos_type start, pos_type end)
175 BOOST_ASSERT(start >= 0 && start <= size());
176 BOOST_ASSERT(end > start && end <= size() + 1);
178 for (pos_type pos = start; pos < end; ++pos) {
179 switch (lookupChange(pos).type) {
180 case Change::UNCHANGED:
181 // also reject changes inside of insets
182 if (pos < size() && owner_->isInset(pos)) {
183 owner_->getInset(pos)->rejectChanges();
187 case Change::INSERTED:
188 // Suppress access to non-existent
189 // "end-of-paragraph char"
191 eraseChar(pos, false);
197 case Change::DELETED:
198 changes_.set(Change(Change::UNCHANGED), pos);
200 // Do NOT reject changes within a deleted inset!
201 // There may be insertions of a co-author inside of it!
209 Paragraph::value_type Paragraph::Pimpl::getChar(pos_type pos) const
211 BOOST_ASSERT(pos >= 0 && pos <= size());
213 return owner_->getChar(pos);
217 void Paragraph::Pimpl::insertChar(pos_type pos, value_type c, Change const & change)
219 BOOST_ASSERT(pos >= 0 && pos <= size());
222 changes_.insert(change, pos);
224 // This is actually very common when parsing buffers (and
225 // maybe inserting ascii text)
227 // when appending characters, no need to update tables
228 owner_->text_.push_back(c);
232 owner_->text_.insert(owner_->text_.begin() + pos, c);
234 // Update the font table.
235 FontTable search_font(pos, LyXFont());
236 for (FontList::iterator it
237 = lower_bound(fontlist.begin(), fontlist.end(), search_font, matchFT());
238 it != fontlist.end(); ++it)
240 it->pos(it->pos() + 1);
244 owner_->insetlist.increasePosAfterPos(pos);
248 void Paragraph::Pimpl::insertInset(pos_type pos, InsetBase * inset,
249 Change const & change)
252 BOOST_ASSERT(pos >= 0 && pos <= size());
254 insertChar(pos, META_INSET, change);
255 BOOST_ASSERT(owner_->text_[pos] == META_INSET);
257 // Add a new entry in the insetlist.
258 owner_->insetlist.insert(inset, pos);
262 bool Paragraph::Pimpl::eraseChar(pos_type pos, bool trackChanges)
264 BOOST_ASSERT(pos >= 0 && pos <= size());
267 Change change = changes_.lookup(pos);
269 // set the character to DELETED if
270 // a) it was previously unchanged or
271 // b) it was inserted by a co-author
273 if (change.type == Change::UNCHANGED ||
274 (change.type == Change::INSERTED && change.author != 0)) {
275 setChange(pos, Change(Change::DELETED));
279 if (change.type == Change::DELETED)
283 // Don't physically access nonexistent end-of-paragraph char
285 // FIXME: change tracking (MG)
286 // how do we handle end-of-pars previously marked inserted?
293 // if it is an inset, delete the inset entry
294 if (owner_->text_[pos] == Paragraph::META_INSET) {
295 owner_->insetlist.erase(pos);
298 owner_->text_.erase(owner_->text_.begin() + pos);
300 // Erase entries in the tables.
301 FontTable search_font(pos, LyXFont());
303 FontList::iterator it =
304 lower_bound(fontlist.begin(),
306 search_font, matchFT());
307 if (it != fontlist.end() && it->pos() == pos &&
309 (it != fontlist.begin()
310 && boost::prior(it)->pos() == pos - 1))) {
311 // If it is a multi-character font
312 // entry, we just make it smaller
313 // (see update below), otherwise we
315 unsigned int const i = it - fontlist.begin();
316 fontlist.erase(fontlist.begin() + i);
317 it = fontlist.begin() + i;
318 if (i > 0 && i < fontlist.size() &&
319 fontlist[i - 1].font() == fontlist[i].font()) {
320 fontlist.erase(fontlist.begin() + i - 1);
321 it = fontlist.begin() + i - 1;
325 // Update all other entries
326 FontList::iterator fend = fontlist.end();
327 for (; it != fend; ++it)
328 it->pos(it->pos() - 1);
330 // Update the insetlist
331 owner_->insetlist.decreasePosAfterPos(pos);
337 int Paragraph::Pimpl::eraseChars(pos_type start, pos_type end, bool trackChanges)
339 BOOST_ASSERT(start >= 0 && start <= size());
340 BOOST_ASSERT(end > start && end <= size() + 1);
343 for (pos_type count = end - start; count; --count) {
344 if (!eraseChar(i, trackChanges))
351 void Paragraph::Pimpl::simpleTeXBlanks(odocstream & os, TexRow & texrow,
353 unsigned int & column,
354 LyXFont const & font,
355 LyXLayout const & style)
360 if (column > lyxrc.ascii_linelen
362 && getChar(i - 1) != ' '
364 // same in FreeSpacing mode
365 && !owner_->isFreeSpacing()
366 // In typewriter mode, we want to avoid
367 // ! . ? : at the end of a line
368 && !(font.family() == LyXFont::TYPEWRITER_FAMILY
369 && (getChar(i - 1) == '.'
370 || getChar(i - 1) == '?'
371 || getChar(i - 1) == ':'
372 || getChar(i - 1) == '!'))) {
375 texrow.start(owner_->id(), i + 1);
377 } else if (style.free_spacing) {
385 bool Paragraph::Pimpl::isTextAt(string const & str, pos_type pos) const
387 pos_type const len = str.length();
389 // is the paragraph large enough?
390 if (pos + len > size())
393 // does the wanted text start at point?
394 for (string::size_type i = 0; i < str.length(); ++i) {
395 if (str[i] != owner_->text_[pos + i])
399 // is there a font change in middle of the word?
400 FontList::const_iterator cit = fontlist.begin();
401 FontList::const_iterator end = fontlist.end();
402 for (; cit != end; ++cit) {
403 if (cit->pos() >= pos)
406 if (cit != end && pos + len - 1 > cit->pos())
413 void Paragraph::Pimpl::simpleTeXSpecialChars(Buffer const & buf,
414 BufferParams const & bparams,
417 OutputParams const & runparams,
419 LyXFont & running_font,
421 LyXFont const & outerfont,
423 Change::Type & running_change,
424 LyXLayout const & style,
426 unsigned int & column,
429 if (style.pass_thru) {
430 if (c != Paragraph::META_INSET) {
434 owner_->getInset(i)->plaintext(buf, os, runparams);
438 // Two major modes: LaTeX or plain
439 // Handle here those cases common to both modes
440 // and then split to handle the two modes separately.
442 case Paragraph::META_INSET: {
443 InsetBase * inset = owner_->getInset(i);
445 // FIXME: remove this check
449 // FIXME: move this to InsetNewline::latex
450 if (inset->lyxCode() == InsetBase::NEWLINE_CODE) {
451 // newlines are handled differently here than
452 // the default in simpleTeXSpecialChars().
453 if (!style.newline_allowed) {
457 column += running_font.latexWriteEndChanges(os, basefont, basefont);
460 basefont = owner_->getLayoutFont(bparams, outerfont);
461 running_font = basefont;
463 if (font.family() == LyXFont::TYPEWRITER_FAMILY)
466 if (runparams.moving_arg)
472 texrow.start(owner_->id(), i + 1);
477 // output change tracking marks only if desired,
478 // if dvipost is installed,
479 // and with dvi/ps (other formats don't work)
480 LaTeXFeatures features(buf, bparams, runparams);
481 bool const output = bparams.outputChanges
482 && runparams.flavor == OutputParams::LATEX
483 && features.isAvailable("dvipost");
485 if (inset->canTrackChanges()) {
486 column += Changes::latexMarkChange(os, running_change,
487 Change::UNCHANGED, output);
488 running_change = Change::UNCHANGED;
492 odocstream::pos_type const len = os.tellp();
494 if ((inset->lyxCode() == InsetBase::GRAPHICS_CODE
495 || inset->lyxCode() == InsetBase::MATH_CODE
496 || inset->lyxCode() == InsetBase::URL_CODE)
497 && running_font.isRightToLeft()) {
503 #warning Bug: we can have an empty font change here!
504 // if there has just been a font change, we are going to close it
505 // right now, which means stupid latex code like \textsf{}. AFAIK,
506 // this does not harm dvi output. A minor bug, thus (JMarc)
508 // some insets cannot be inside a font change command
509 if (open_font && inset->noFontChange()) {
510 column +=running_font.
511 latexWriteEndChanges(os,
515 basefont = owner_->getLayoutFont(bparams, outerfont);
516 running_font = basefont;
519 int tmp = inset->latex(buf, os, runparams);
525 for (int j = 0; j < tmp; ++j) {
528 texrow.start(owner_->id(), i + 1);
531 column += os.tellp() - len;
537 // And now for the special cases within each mode
541 os << "\\textbackslash{}";
545 // The following characters could be written literally in latin1, but they
546 // would be wrongly converted on systems where char is signed, so we give
548 // This also makes us independant from the encoding of this source file.
549 case 0xb1: // ± PLUS-MINUS SIGN
550 case 0xb2: // ² SUPERSCRIPT TWO
551 case 0xb3: // ³ SUPERSCRIPT THREE
552 case 0xd7: // × MULTIPLICATION SIGN
553 case 0xf7: // ÷ DIVISION SIGN
554 case 0xb9: // ¹ SUPERSCRIPT ONE
555 case 0xac: // ¬ NOT SIGN
556 case 0xb5: // µ MICRO SIGN
557 if ((bparams.inputenc == "latin1" ||
558 bparams.inputenc == "latin9") ||
559 (bparams.inputenc == "auto" &&
560 (font.language()->encoding()->latexName()
562 font.language()->encoding()->latexName()
564 os << "\\ensuremath{";
573 case '|': case '<': case '>':
574 // In T1 encoding, these characters exist
575 if (lyxrc.fontenc == "T1") {
577 //... but we should avoid ligatures
578 if ((c == '>' || c == '<')
580 && getChar(i + 1) == c) {
581 //os << "\\textcompwordmark{}";
582 // Jean-Marc, have a look at
583 // this. I think this works
591 // Typewriter font also has them
592 if (font.family() == LyXFont::TYPEWRITER_FAMILY) {
596 // Otherwise, we use what LaTeX
600 os << "\\textless{}";
604 os << "\\textgreater{}";
614 case '-': // "--" in Typewriter mode -> "-{}-"
616 && getChar(i + 1) == '-'
617 && font.family() == LyXFont::TYPEWRITER_FAMILY) {
626 os << "\\char`\\\"{}";
630 case 0xa3: // £ POUND SIGN
631 if (bparams.inputenc == "default") {
640 case '%': case '#': case '{':
648 os << "\\textasciitilde{}";
653 os << "\\textasciicircum{}";
658 // avoid being mistaken for optional arguments
666 // Blanks are printed before font switching.
667 // Sure? I am not! (try nice-latex)
668 // I am sure it's correct. LyX might be smarter
669 // in the future, but for now, nothing wrong is
675 // I assume this is hack treating typewriter as verbatim
676 if (font.family() == LyXFont::TYPEWRITER_FAMILY) {
685 // FIXME: if we have "LaTeX" with a font
686 // change in the middle (before the 'T', then
687 // the "TeX" part is still special cased.
688 // Really we should only operate this on
689 // "words" for some definition of word
693 for (; pnr < phrases_nr; ++pnr) {
694 if (isTextAt(special_phrases[pnr].phrase, i)) {
695 os << special_phrases[pnr].macro;
696 i += special_phrases[pnr].phrase.length() - 1;
697 column += special_phrases[pnr].macro.length() - 1;
702 if (pnr == phrases_nr && c != '\0') {
711 void Paragraph::Pimpl::validate(LaTeXFeatures & features,
712 LyXLayout const & layout) const
714 BufferParams const & bparams = features.bufferParams();
717 if (!params.spacing().isDefault())
718 features.require("setspace");
721 features.useLayout(layout.name());
724 Language const * doc_language = bparams.language;
726 FontList::const_iterator fcit = fontlist.begin();
727 FontList::const_iterator fend = fontlist.end();
728 for (; fcit != fend; ++fcit) {
729 if (fcit->font().noun() == LyXFont::ON) {
730 lyxerr[Debug::LATEX] << "font.noun: "
731 << fcit->font().noun()
733 features.require("noun");
734 lyxerr[Debug::LATEX] << "Noun enabled. Font: "
735 << fcit->font().stateText(0)
738 switch (fcit->font().color()) {
740 case LColor::inherit:
742 // probably we should put here all interface colors used for
743 // font displaying! For now I just add this ones I know of (Jug)
748 features.require("color");
749 lyxerr[Debug::LATEX] << "Color enabled. Font: "
750 << fcit->font().stateText(0)
754 Language const * language = fcit->font().language();
755 if (language->babel() != doc_language->babel() &&
756 language != ignore_language &&
757 language != latex_language)
759 features.useLanguage(language);
760 lyxerr[Debug::LATEX] << "Found language "
761 << language->babel() << endl;
765 if (!params.leftIndent().zero())
766 features.require("ParagraphLeftIndent");
769 InsetList::const_iterator icit = owner_->insetlist.begin();
770 InsetList::const_iterator iend = owner_->insetlist.end();
771 for (; icit != iend; ++icit) {
773 icit->inset->validate(features);
774 if (layout.needprotect &&
775 icit->inset->lyxCode() == InsetBase::FOOT_CODE)
776 features.require("NeedLyXFootnoteCode");
781 for (pos_type i = 0; i < size() ; ++i) {
782 for (size_t pnr = 0; pnr < phrases_nr; ++pnr) {
783 if (!special_phrases[pnr].builtin
784 && isTextAt(special_phrases[pnr].phrase, i)) {
785 features.require(special_phrases[pnr].phrase);