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 void Paragraph::Pimpl::setContentsFromPar(Paragraph const & par)
83 owner_->text_ = par.text_;
84 // FIXME: change tracking (MG)
85 // check whether this method is really needed
86 changes_ = par.pimpl_->changes_;
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 void Paragraph::Pimpl::setChange(Change const & change)
101 // FIXME: change tracking (MG)
102 // how about end-of-line? size()+1?
103 changes_.set(change, 0, size());
105 if (change.type == Change::UNCHANGED) { // only for UNCHANGED ???
106 for (pos_type i = 0; i < size(); ++i) {
107 if (owner_->isInset(i)) {
108 owner_->getInset(i)->setChange(change);
115 void Paragraph::Pimpl::setChange(pos_type pos, Change const & change)
117 BOOST_ASSERT(pos >= 0 && pos <= size());
119 changes_.set(change, pos);
121 // FIXME: change tracking (MG)
122 // do we have to set the change recursively?
123 if (pos < size() && owner_->isInset(pos)) {
124 owner_->getInset(pos)->setChange(change);
129 Change const Paragraph::Pimpl::lookupChange(pos_type pos) const
131 BOOST_ASSERT(pos >= 0 && pos <= size());
133 return changes_.lookup(pos);
137 void Paragraph::Pimpl::acceptChanges(pos_type start, pos_type end)
139 BOOST_ASSERT(start >= 0 && start <= size());
140 BOOST_ASSERT(end > start && end <= size() + 1);
142 for (pos_type pos = start; pos < end; ++pos) {
143 switch (lookupChange(pos).type) {
144 case Change::UNCHANGED:
147 case Change::INSERTED:
148 changes_.set(Change(Change::UNCHANGED), pos);
151 case Change::DELETED:
152 // Suppress access to non-existent
153 // "end-of-paragraph char"
155 eraseChar(pos, false);
162 // also accept changes in nested insets
163 if (pos < size() && owner_->isInset(pos)) {
164 owner_->getInset(pos)->acceptChanges();
170 void Paragraph::Pimpl::rejectChanges(pos_type start, pos_type end)
172 BOOST_ASSERT(start >= 0 && start <= size());
173 BOOST_ASSERT(end > start && end <= size() + 1);
175 for (pos_type pos = start; pos < end; ++pos) {
176 switch (lookupChange(pos).type) {
177 case Change::UNCHANGED:
180 case Change::INSERTED:
181 // Suppress access to non-existent
182 // "end-of-paragraph char"
184 eraseChar(pos, false);
190 case Change::DELETED:
191 changes_.set(Change(Change::UNCHANGED), pos);
195 // also reject changes in nested insets
196 if (pos < size() && owner_->isInset(pos)) {
197 owner_->getInset(pos)->rejectChanges();
203 Paragraph::value_type Paragraph::Pimpl::getChar(pos_type pos) const
205 BOOST_ASSERT(pos >= 0 && pos <= size());
207 return owner_->getChar(pos);
211 void Paragraph::Pimpl::insertChar(pos_type pos, value_type c, Change const & change)
213 BOOST_ASSERT(pos >= 0 && pos <= size());
216 changes_.insert(change, pos);
218 // This is actually very common when parsing buffers (and
219 // maybe inserting ascii text)
221 // when appending characters, no need to update tables
222 owner_->text_.push_back(c);
226 owner_->text_.insert(owner_->text_.begin() + pos, c);
228 // Update the font table.
229 FontTable search_font(pos, LyXFont());
230 for (FontList::iterator it
231 = lower_bound(fontlist.begin(), fontlist.end(), search_font, matchFT());
232 it != fontlist.end(); ++it)
234 it->pos(it->pos() + 1);
238 owner_->insetlist.increasePosAfterPos(pos);
242 void Paragraph::Pimpl::insertInset(pos_type pos, InsetBase * inset,
243 Change const & change)
246 BOOST_ASSERT(pos >= 0 && pos <= size());
248 insertChar(pos, META_INSET, change);
249 BOOST_ASSERT(owner_->text_[pos] == META_INSET);
251 // Add a new entry in the insetlist.
252 owner_->insetlist.insert(inset, pos);
256 bool Paragraph::Pimpl::eraseChar(pos_type pos, bool trackChanges)
258 BOOST_ASSERT(pos >= 0 && pos <= size());
261 Change::Type changetype(changes_.lookup(pos).type);
263 if (changetype == Change::UNCHANGED) {
264 setChange(pos, Change(Change::DELETED));
268 if (changetype == Change::DELETED)
272 // Don't physically access nonexistent end-of-paragraph char
274 // FIXME: change tracking (MG)
275 // how do we handle end-of-pars previously marked inserted?
282 // if it is an inset, delete the inset entry
283 if (owner_->text_[pos] == Paragraph::META_INSET) {
284 owner_->insetlist.erase(pos);
287 owner_->text_.erase(owner_->text_.begin() + pos);
289 // Erase entries in the tables.
290 FontTable search_font(pos, LyXFont());
292 FontList::iterator it =
293 lower_bound(fontlist.begin(),
295 search_font, matchFT());
296 if (it != fontlist.end() && it->pos() == pos &&
298 (it != fontlist.begin()
299 && boost::prior(it)->pos() == pos - 1))) {
300 // If it is a multi-character font
301 // entry, we just make it smaller
302 // (see update below), otherwise we
304 unsigned int const i = it - fontlist.begin();
305 fontlist.erase(fontlist.begin() + i);
306 it = fontlist.begin() + i;
307 if (i > 0 && i < fontlist.size() &&
308 fontlist[i - 1].font() == fontlist[i].font()) {
309 fontlist.erase(fontlist.begin() + i - 1);
310 it = fontlist.begin() + i - 1;
314 // Update all other entries
315 FontList::iterator fend = fontlist.end();
316 for (; it != fend; ++it)
317 it->pos(it->pos() - 1);
319 // Update the insetlist
320 owner_->insetlist.decreasePosAfterPos(pos);
326 int Paragraph::Pimpl::eraseChars(pos_type start, pos_type end, bool trackChanges)
328 BOOST_ASSERT(start >= 0 && start <= size());
329 BOOST_ASSERT(end > start && end <= size() + 1);
332 for (pos_type count = end - start; count; --count) {
333 if (!eraseChar(i, trackChanges))
340 void Paragraph::Pimpl::simpleTeXBlanks(odocstream & os, TexRow & texrow,
342 unsigned int & column,
343 LyXFont const & font,
344 LyXLayout const & style)
349 if (column > lyxrc.ascii_linelen
351 && getChar(i - 1) != ' '
353 // same in FreeSpacing mode
354 && !owner_->isFreeSpacing()
355 // In typewriter mode, we want to avoid
356 // ! . ? : at the end of a line
357 && !(font.family() == LyXFont::TYPEWRITER_FAMILY
358 && (getChar(i - 1) == '.'
359 || getChar(i - 1) == '?'
360 || getChar(i - 1) == ':'
361 || getChar(i - 1) == '!'))) {
364 texrow.start(owner_->id(), i + 1);
366 } else if (style.free_spacing) {
374 bool Paragraph::Pimpl::isTextAt(string const & str, pos_type pos) const
376 pos_type const len = str.length();
378 // is the paragraph large enough?
379 if (pos + len > size())
382 // does the wanted text start at point?
383 for (string::size_type i = 0; i < str.length(); ++i) {
384 if (str[i] != owner_->text_[pos + i])
388 // is there a font change in middle of the word?
389 FontList::const_iterator cit = fontlist.begin();
390 FontList::const_iterator end = fontlist.end();
391 for (; cit != end; ++cit) {
392 if (cit->pos() >= pos)
395 if (cit != end && pos + len - 1 > cit->pos())
402 void Paragraph::Pimpl::simpleTeXSpecialChars(Buffer const & buf,
403 BufferParams const & bparams,
406 OutputParams const & runparams,
408 LyXFont & running_font,
410 LyXFont const & outerfont,
412 Change::Type & running_change,
413 LyXLayout const & style,
415 unsigned int & column,
418 if (style.pass_thru) {
419 if (c != Paragraph::META_INSET) {
423 owner_->getInset(i)->plaintext(buf, os, runparams);
427 // Two major modes: LaTeX or plain
428 // Handle here those cases common to both modes
429 // and then split to handle the two modes separately.
431 case Paragraph::META_INSET: {
432 InsetBase * inset = owner_->getInset(i);
434 // FIXME: remove this check
438 // FIXME: move this to InsetNewline::latex
439 if (inset->lyxCode() == InsetBase::NEWLINE_CODE) {
440 // newlines are handled differently here than
441 // the default in simpleTeXSpecialChars().
442 if (!style.newline_allowed) {
446 column += running_font.latexWriteEndChanges(os, basefont, basefont);
449 basefont = owner_->getLayoutFont(bparams, outerfont);
450 running_font = basefont;
452 if (font.family() == LyXFont::TYPEWRITER_FAMILY)
455 if (runparams.moving_arg)
461 texrow.start(owner_->id(), i + 1);
466 // output change tracking marks only if desired,
467 // if dvipost is installed,
468 // and with dvi/ps (other formats don't work)
469 LaTeXFeatures features(buf, bparams, runparams);
470 bool const output = bparams.outputChanges
471 && runparams.flavor == OutputParams::LATEX
472 && features.isAvailable("dvipost");
474 if (inset->canTrackChanges()) {
475 column += Changes::latexMarkChange(os, running_change,
476 Change::UNCHANGED, output);
477 running_change = Change::UNCHANGED;
481 odocstream::pos_type const len = os.tellp();
483 if ((inset->lyxCode() == InsetBase::GRAPHICS_CODE
484 || inset->lyxCode() == InsetBase::MATH_CODE
485 || inset->lyxCode() == InsetBase::URL_CODE)
486 && running_font.isRightToLeft()) {
492 #warning Bug: we can have an empty font change here!
493 // if there has just been a font change, we are going to close it
494 // right now, which means stupid latex code like \textsf{}. AFAIK,
495 // this does not harm dvi output. A minor bug, thus (JMarc)
497 // some insets cannot be inside a font change command
498 if (open_font && inset->noFontChange()) {
499 column +=running_font.
500 latexWriteEndChanges(os,
504 basefont = owner_->getLayoutFont(bparams, outerfont);
505 running_font = basefont;
508 int tmp = inset->latex(buf, os, runparams);
514 for (int j = 0; j < tmp; ++j) {
517 texrow.start(owner_->id(), i + 1);
520 column += os.tellp() - len;
526 // And now for the special cases within each mode
530 os << "\\textbackslash{}";
534 // The following characters could be written literally in latin1, but they
535 // would be wrongly converted on systems where char is signed, so we give
537 // This also makes us independant from the encoding of this source file.
538 case 0xb1: // ± PLUS-MINUS SIGN
539 case 0xb2: // ² SUPERSCRIPT TWO
540 case 0xb3: // ³ SUPERSCRIPT THREE
541 case 0xd7: // × MULTIPLICATION SIGN
542 case 0xf7: // ÷ DIVISION SIGN
543 case 0xb9: // ¹ SUPERSCRIPT ONE
544 case 0xac: // ¬ NOT SIGN
545 case 0xb5: // µ MICRO SIGN
546 if ((bparams.inputenc == "latin1" ||
547 bparams.inputenc == "latin9") ||
548 (bparams.inputenc == "auto" &&
549 (font.language()->encoding()->latexName()
551 font.language()->encoding()->latexName()
553 os << "\\ensuremath{";
562 case '|': case '<': case '>':
563 // In T1 encoding, these characters exist
564 if (lyxrc.fontenc == "T1") {
566 //... but we should avoid ligatures
567 if ((c == '>' || c == '<')
569 && getChar(i + 1) == c) {
570 //os << "\\textcompwordmark{}";
571 // Jean-Marc, have a look at
572 // this. I think this works
580 // Typewriter font also has them
581 if (font.family() == LyXFont::TYPEWRITER_FAMILY) {
585 // Otherwise, we use what LaTeX
589 os << "\\textless{}";
593 os << "\\textgreater{}";
603 case '-': // "--" in Typewriter mode -> "-{}-"
605 && getChar(i + 1) == '-'
606 && font.family() == LyXFont::TYPEWRITER_FAMILY) {
615 os << "\\char`\\\"{}";
619 case 0xa3: // £ POUND SIGN
620 if (bparams.inputenc == "default") {
629 case '%': case '#': case '{':
637 os << "\\textasciitilde{}";
642 os << "\\textasciicircum{}";
647 // avoid being mistaken for optional arguments
655 // Blanks are printed before font switching.
656 // Sure? I am not! (try nice-latex)
657 // I am sure it's correct. LyX might be smarter
658 // in the future, but for now, nothing wrong is
664 // I assume this is hack treating typewriter as verbatim
665 if (font.family() == LyXFont::TYPEWRITER_FAMILY) {
674 // FIXME: if we have "LaTeX" with a font
675 // change in the middle (before the 'T', then
676 // the "TeX" part is still special cased.
677 // Really we should only operate this on
678 // "words" for some definition of word
682 for (; pnr < phrases_nr; ++pnr) {
683 if (isTextAt(special_phrases[pnr].phrase, i)) {
684 os << special_phrases[pnr].macro;
685 i += special_phrases[pnr].phrase.length() - 1;
686 column += special_phrases[pnr].macro.length() - 1;
691 if (pnr == phrases_nr && c != '\0') {
700 void Paragraph::Pimpl::validate(LaTeXFeatures & features,
701 LyXLayout const & layout) const
703 BufferParams const & bparams = features.bufferParams();
706 if (!params.spacing().isDefault())
707 features.require("setspace");
710 features.useLayout(layout.name());
713 Language const * doc_language = bparams.language;
715 FontList::const_iterator fcit = fontlist.begin();
716 FontList::const_iterator fend = fontlist.end();
717 for (; fcit != fend; ++fcit) {
718 if (fcit->font().noun() == LyXFont::ON) {
719 lyxerr[Debug::LATEX] << "font.noun: "
720 << fcit->font().noun()
722 features.require("noun");
723 lyxerr[Debug::LATEX] << "Noun enabled. Font: "
724 << fcit->font().stateText(0)
727 switch (fcit->font().color()) {
729 case LColor::inherit:
731 // probably we should put here all interface colors used for
732 // font displaying! For now I just add this ones I know of (Jug)
737 features.require("color");
738 lyxerr[Debug::LATEX] << "Color enabled. Font: "
739 << fcit->font().stateText(0)
743 Language const * language = fcit->font().language();
744 if (language->babel() != doc_language->babel() &&
745 language != ignore_language &&
746 language != latex_language)
748 features.useLanguage(language);
749 lyxerr[Debug::LATEX] << "Found language "
750 << language->babel() << endl;
754 if (!params.leftIndent().zero())
755 features.require("ParagraphLeftIndent");
758 InsetList::const_iterator icit = owner_->insetlist.begin();
759 InsetList::const_iterator iend = owner_->insetlist.end();
760 for (; icit != iend; ++icit) {
762 icit->inset->validate(features);
763 if (layout.needprotect &&
764 icit->inset->lyxCode() == InsetBase::FOOT_CODE)
765 features.require("NeedLyXFootnoteCode");
770 for (pos_type i = 0; i < size() ; ++i) {
771 for (size_t pnr = 0; pnr < phrases_nr; ++pnr) {
772 if (!special_phrases[pnr].builtin
773 && isTextAt(special_phrases[pnr].phrase, i)) {
774 features.require(special_phrases[pnr].phrase);