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 return changes_.isChanged(start, end);
96 void Paragraph::Pimpl::setChange(Change const & change)
98 // FIXME: change tracking (MG)
99 // how about end-of-line? size()+1?
100 changes_.set(change, 0, size());
102 if (change.type == Change::UNCHANGED) { // only for UNCHANGED ???
103 for (pos_type i = 0; i < size(); ++i) {
104 if (owner_->isInset(i)) {
105 owner_->getInset(i)->setChange(change);
112 void Paragraph::Pimpl::setChange(pos_type pos, Change const & change)
114 changes_.set(change, pos);
116 // FIXME: change tracking (MG)
117 // do we have to set the change recursively?
118 if (pos < size() && owner_->isInset(pos)) {
119 owner_->getInset(pos)->setChange(change);
124 Change const Paragraph::Pimpl::lookupChange(pos_type pos) const
126 return changes_.lookup(pos);
130 void Paragraph::Pimpl::acceptChanges(pos_type start, pos_type end)
132 for (pos_type pos = start; pos < end; ++pos) {
133 switch (lookupChange(pos).type) {
134 case Change::UNCHANGED:
137 case Change::INSERTED:
138 changes_.set(Change(Change::UNCHANGED), pos);
141 case Change::DELETED:
142 // Suppress access to nonexistent
143 // "end-of-paragraph char":
145 eraseChar(pos, false);
152 // also accept changes in nested insets
153 if (pos < size() && owner_->isInset(pos)) {
154 owner_->getInset(pos)->acceptChanges();
160 void Paragraph::Pimpl::rejectChange(pos_type start, pos_type end)
162 // FIXME: change tracking (MG)
165 // care for empty pars
169 for (; i < end; ++i) {
170 switch (lookupChange(i).type) {
171 case Change::UNCHANGED:
174 case Change::INSERTED:
182 case Change::DELETED:
183 // FIXME: change tracking (MG)
184 changes_.set(Change(Change::UNCHANGED), i);
185 // No real char at position size():
186 if (i < size() && owner_->isInset(i))
187 // FIXME: change tracking (MG)
188 owner_->getInset(i)->setChange(Change(Change::UNCHANGED));
192 // FIXME: change tracking (MG)
193 // changes_.reset(Change::UNCHANGED);
197 Paragraph::value_type Paragraph::Pimpl::getChar(pos_type pos) const
199 return owner_->getChar(pos);
203 void Paragraph::Pimpl::insertChar(pos_type pos, value_type c, Change const & change)
205 BOOST_ASSERT(pos <= size());
208 changes_.insert(change, pos);
210 // This is actually very common when parsing buffers (and
211 // maybe inserting ascii text)
213 // when appending characters, no need to update tables
214 owner_->text_.push_back(c);
218 owner_->text_.insert(owner_->text_.begin() + pos, c);
220 // Update the font table.
221 FontTable search_font(pos, LyXFont());
222 for (FontList::iterator it
223 = lower_bound(fontlist.begin(), fontlist.end(), search_font, matchFT());
224 it != fontlist.end(); ++it)
226 it->pos(it->pos() + 1);
230 owner_->insetlist.increasePosAfterPos(pos);
234 void Paragraph::Pimpl::insertInset(pos_type pos, InsetBase * inset,
235 Change const & change)
238 BOOST_ASSERT(pos <= size());
240 insertChar(pos, META_INSET, change);
241 BOOST_ASSERT(owner_->text_[pos] == META_INSET);
243 // Add a new entry in the insetlist.
244 owner_->insetlist.insert(inset, pos);
248 bool Paragraph::Pimpl::eraseChar(pos_type pos, bool trackChanges)
250 BOOST_ASSERT(pos <= size());
253 Change::Type changetype(changes_.lookup(pos).type);
255 if (changetype == Change::UNCHANGED) {
256 setChange(pos, Change(Change::DELETED));
260 if (changetype == Change::DELETED)
264 // Don't physically access nonexistent end-of-paragraph char
266 // FIXME: change tracking (MG)
267 // how do we handle end-of-pars previously marked inserted?
274 // if it is an inset, delete the inset entry
275 if (owner_->text_[pos] == Paragraph::META_INSET) {
276 owner_->insetlist.erase(pos);
279 owner_->text_.erase(owner_->text_.begin() + pos);
281 // Erase entries in the tables.
282 FontTable search_font(pos, LyXFont());
284 FontList::iterator it =
285 lower_bound(fontlist.begin(),
287 search_font, matchFT());
288 if (it != fontlist.end() && it->pos() == pos &&
290 (it != fontlist.begin()
291 && boost::prior(it)->pos() == pos - 1))) {
292 // If it is a multi-character font
293 // entry, we just make it smaller
294 // (see update below), otherwise we
296 unsigned int const i = it - fontlist.begin();
297 fontlist.erase(fontlist.begin() + i);
298 it = fontlist.begin() + i;
299 if (i > 0 && i < fontlist.size() &&
300 fontlist[i - 1].font() == fontlist[i].font()) {
301 fontlist.erase(fontlist.begin() + i - 1);
302 it = fontlist.begin() + i - 1;
306 // Update all other entries
307 FontList::iterator fend = fontlist.end();
308 for (; it != fend; ++it)
309 it->pos(it->pos() - 1);
311 // Update the insetlist
312 owner_->insetlist.decreasePosAfterPos(pos);
318 int Paragraph::Pimpl::eraseChars(pos_type start, pos_type end, bool trackChanges)
321 for (pos_type count = end - start; count; --count) {
322 if (!eraseChar(i, trackChanges))
329 void Paragraph::Pimpl::simpleTeXBlanks(odocstream & os, TexRow & texrow,
331 unsigned int & column,
332 LyXFont const & font,
333 LyXLayout const & style)
338 if (column > lyxrc.ascii_linelen
340 && getChar(i - 1) != ' '
342 // same in FreeSpacing mode
343 && !owner_->isFreeSpacing()
344 // In typewriter mode, we want to avoid
345 // ! . ? : at the end of a line
346 && !(font.family() == LyXFont::TYPEWRITER_FAMILY
347 && (getChar(i - 1) == '.'
348 || getChar(i - 1) == '?'
349 || getChar(i - 1) == ':'
350 || getChar(i - 1) == '!'))) {
353 texrow.start(owner_->id(), i + 1);
355 } else if (style.free_spacing) {
363 bool Paragraph::Pimpl::isTextAt(string const & str, pos_type pos) const
365 pos_type const len = str.length();
367 // is the paragraph large enough?
368 if (pos + len > size())
371 // does the wanted text start at point?
372 for (string::size_type i = 0; i < str.length(); ++i) {
373 if (str[i] != owner_->text_[pos + i])
377 // is there a font change in middle of the word?
378 FontList::const_iterator cit = fontlist.begin();
379 FontList::const_iterator end = fontlist.end();
380 for (; cit != end; ++cit) {
381 if (cit->pos() >= pos)
384 if (cit != end && pos + len - 1 > cit->pos())
391 void Paragraph::Pimpl::simpleTeXSpecialChars(Buffer const & buf,
392 BufferParams const & bparams,
395 OutputParams const & runparams,
397 LyXFont & running_font,
399 LyXFont const & outerfont,
401 Change::Type & running_change,
402 LyXLayout const & style,
404 unsigned int & column,
407 if (style.pass_thru) {
408 if (c != Paragraph::META_INSET) {
412 owner_->getInset(i)->plaintext(buf, os, runparams);
416 // Two major modes: LaTeX or plain
417 // Handle here those cases common to both modes
418 // and then split to handle the two modes separately.
420 case Paragraph::META_INSET: {
421 InsetBase * inset = owner_->getInset(i);
423 // FIXME: remove this check
427 // FIXME: move this to InsetNewline::latex
428 if (inset->lyxCode() == InsetBase::NEWLINE_CODE) {
429 // newlines are handled differently here than
430 // the default in simpleTeXSpecialChars().
431 if (!style.newline_allowed) {
435 column += running_font.latexWriteEndChanges(os, basefont, basefont);
438 basefont = owner_->getLayoutFont(bparams, outerfont);
439 running_font = basefont;
441 if (font.family() == LyXFont::TYPEWRITER_FAMILY)
444 if (runparams.moving_arg)
450 texrow.start(owner_->id(), i + 1);
455 // output change tracking marks only if desired,
456 // if dvipost is installed,
457 // and with dvi/ps (other formats don't work)
458 LaTeXFeatures features(buf, bparams, runparams);
459 bool const output = bparams.outputChanges
460 && runparams.flavor == OutputParams::LATEX
461 && features.isAvailable("dvipost");
463 if (inset->canTrackChanges()) {
464 column += Changes::latexMarkChange(os, running_change,
465 Change::UNCHANGED, output);
466 running_change = Change::UNCHANGED;
470 odocstream::pos_type const len = os.tellp();
472 if ((inset->lyxCode() == InsetBase::GRAPHICS_CODE
473 || inset->lyxCode() == InsetBase::MATH_CODE
474 || inset->lyxCode() == InsetBase::URL_CODE)
475 && running_font.isRightToLeft()) {
481 #warning Bug: we can have an empty font change here!
482 // if there has just been a font change, we are going to close it
483 // right now, which means stupid latex code like \textsf{}. AFAIK,
484 // this does not harm dvi output. A minor bug, thus (JMarc)
486 // some insets cannot be inside a font change command
487 if (open_font && inset->noFontChange()) {
488 column +=running_font.
489 latexWriteEndChanges(os,
493 basefont = owner_->getLayoutFont(bparams, outerfont);
494 running_font = basefont;
497 int tmp = inset->latex(buf, os, runparams);
503 for (int j = 0; j < tmp; ++j) {
506 texrow.start(owner_->id(), i + 1);
509 column += os.tellp() - len;
515 // And now for the special cases within each mode
519 os << "\\textbackslash{}";
523 // The following characters could be written literally in latin1, but they
524 // would be wrongly converted on systems where char is signed, so we give
526 // This also makes us independant from the encoding of this source file.
527 case 0xb1: // ± PLUS-MINUS SIGN
528 case 0xb2: // ² SUPERSCRIPT TWO
529 case 0xb3: // ³ SUPERSCRIPT THREE
530 case 0xd7: // × MULTIPLICATION SIGN
531 case 0xf7: // ÷ DIVISION SIGN
532 case 0xb9: // ¹ SUPERSCRIPT ONE
533 case 0xac: // ¬ NOT SIGN
534 case 0xb5: // µ MICRO SIGN
535 if ((bparams.inputenc == "latin1" ||
536 bparams.inputenc == "latin9") ||
537 (bparams.inputenc == "auto" &&
538 (font.language()->encoding()->latexName()
540 font.language()->encoding()->latexName()
542 os << "\\ensuremath{";
551 case '|': case '<': case '>':
552 // In T1 encoding, these characters exist
553 if (lyxrc.fontenc == "T1") {
555 //... but we should avoid ligatures
556 if ((c == '>' || c == '<')
558 && getChar(i + 1) == c) {
559 //os << "\\textcompwordmark{}";
560 // Jean-Marc, have a look at
561 // this. I think this works
569 // Typewriter font also has them
570 if (font.family() == LyXFont::TYPEWRITER_FAMILY) {
574 // Otherwise, we use what LaTeX
578 os << "\\textless{}";
582 os << "\\textgreater{}";
592 case '-': // "--" in Typewriter mode -> "-{}-"
594 && getChar(i + 1) == '-'
595 && font.family() == LyXFont::TYPEWRITER_FAMILY) {
604 os << "\\char`\\\"{}";
608 case 0xa3: // £ POUND SIGN
609 if (bparams.inputenc == "default") {
618 case '%': case '#': case '{':
626 os << "\\textasciitilde{}";
631 os << "\\textasciicircum{}";
636 // avoid being mistaken for optional arguments
644 // Blanks are printed before font switching.
645 // Sure? I am not! (try nice-latex)
646 // I am sure it's correct. LyX might be smarter
647 // in the future, but for now, nothing wrong is
653 // I assume this is hack treating typewriter as verbatim
654 if (font.family() == LyXFont::TYPEWRITER_FAMILY) {
663 // FIXME: if we have "LaTeX" with a font
664 // change in the middle (before the 'T', then
665 // the "TeX" part is still special cased.
666 // Really we should only operate this on
667 // "words" for some definition of word
671 for (; pnr < phrases_nr; ++pnr) {
672 if (isTextAt(special_phrases[pnr].phrase, i)) {
673 os << special_phrases[pnr].macro;
674 i += special_phrases[pnr].phrase.length() - 1;
675 column += special_phrases[pnr].macro.length() - 1;
680 if (pnr == phrases_nr && c != '\0') {
689 void Paragraph::Pimpl::validate(LaTeXFeatures & features,
690 LyXLayout const & layout) const
692 BufferParams const & bparams = features.bufferParams();
695 if (!params.spacing().isDefault())
696 features.require("setspace");
699 features.useLayout(layout.name());
702 Language const * doc_language = bparams.language;
704 FontList::const_iterator fcit = fontlist.begin();
705 FontList::const_iterator fend = fontlist.end();
706 for (; fcit != fend; ++fcit) {
707 if (fcit->font().noun() == LyXFont::ON) {
708 lyxerr[Debug::LATEX] << "font.noun: "
709 << fcit->font().noun()
711 features.require("noun");
712 lyxerr[Debug::LATEX] << "Noun enabled. Font: "
713 << fcit->font().stateText(0)
716 switch (fcit->font().color()) {
718 case LColor::inherit:
720 // probably we should put here all interface colors used for
721 // font displaying! For now I just add this ones I know of (Jug)
726 features.require("color");
727 lyxerr[Debug::LATEX] << "Color enabled. Font: "
728 << fcit->font().stateText(0)
732 Language const * language = fcit->font().language();
733 if (language->babel() != doc_language->babel() &&
734 language != ignore_language &&
735 language != latex_language)
737 features.useLanguage(language);
738 lyxerr[Debug::LATEX] << "Found language "
739 << language->babel() << endl;
743 if (!params.leftIndent().zero())
744 features.require("ParagraphLeftIndent");
747 InsetList::const_iterator icit = owner_->insetlist.begin();
748 InsetList::const_iterator iend = owner_->insetlist.end();
749 for (; icit != iend; ++icit) {
751 icit->inset->validate(features);
752 if (layout.needprotect &&
753 icit->inset->lyxCode() == InsetBase::FOOT_CODE)
754 features.require("NeedLyXFootnoteCode");
759 for (pos_type i = 0; i < size() ; ++i) {
760 for (size_t pnr = 0; pnr < phrases_nr; ++pnr) {
761 if (!special_phrases[pnr].builtin
762 && isTextAt(special_phrases[pnr].phrase, i)) {
763 features.require(special_phrases[pnr].phrase);