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>
33 using lyx::odocstream;
37 using std::upper_bound;
38 using std::lower_bound;
42 // Initialization of the counter for the paragraph id's,
43 unsigned int Paragraph::Pimpl::paragraph_id = 0;
47 struct special_phrase {
53 special_phrase const special_phrases[] = {
54 { "LyX", lyx::from_ascii("\\LyX{}"), false },
55 { "TeX", lyx::from_ascii("\\TeX{}"), true },
56 { "LaTeX2e", lyx::from_ascii("\\LaTeXe{}"), true },
57 { "LaTeX", lyx::from_ascii("\\LaTeX{}"), true },
60 size_t const phrases_nr = sizeof(special_phrases)/sizeof(special_phrase);
65 Paragraph::Pimpl::Pimpl(Paragraph * owner)
73 Paragraph::Pimpl::Pimpl(Pimpl const & p, Paragraph * owner)
74 : params(p.params), owner_(owner)
76 inset_owner = p.inset_owner;
77 fontlist = p.fontlist;
80 // FIXME: change tracking (MG)
82 // changes_.reset(new Changes(*p.changes_.get()));
86 void Paragraph::Pimpl::setContentsFromPar(Paragraph const & par)
88 owner_->text_ = par.text_;
89 if (par.pimpl_->tracking()) {
90 // FIXME: change tracking (MG)
91 // changes_.reset(new Changes(*(par.pimpl_->changes_.get())));
96 bool Paragraph::Pimpl::isChanged(pos_type start, pos_type end) const
98 // FIXME: change tracking (MG)
102 return changes_->isChanged(start, end);
106 void Paragraph::Pimpl::setChange(Change const & change)
108 // FIXME: change tracking (MG)
109 // changes_.set(change, 0, size());
111 if (change.type == Change::UNCHANGED) { // only for UNCHANGED ???
112 for (pos_type i = 0; i < size(); ++i) {
113 if (owner_->isInset(i)) {
114 // FIXME: change tracking (MG)
115 // owner_->getInset(i)->setChange(change);
122 void Paragraph::Pimpl::setChange(pos_type pos, Change const & change)
127 changes_->set(change, pos);
131 Change const Paragraph::Pimpl::lookupChange(pos_type pos) const
134 return Change(Change::UNCHANGED);
136 return changes_->lookup(pos);
140 void Paragraph::Pimpl::acceptChange(pos_type start, pos_type end)
146 // FIXME: change tracking (MG)
147 // changes_.reset(new Changes(Change::UNCHANGED));
151 lyxerr[Debug::CHANGES] << "acceptchange" << endl;
154 for (; i < end; ++i) {
155 switch (lookupChange(i).type) {
156 case Change::UNCHANGED:
159 case Change::INSERTED:
160 // FIXME: change tracking (MG)
161 changes_->set(Change(Change::UNCHANGED), i);
164 case Change::DELETED:
165 // Suppress access to nonexistent
166 // "end-of-paragraph char":
176 lyxerr[Debug::CHANGES] << "endacceptchange" << endl;
177 // FIXME: change tracking (MG)
178 // changes_->reset(Change::UNCHANGED);
182 void Paragraph::Pimpl::rejectChange(pos_type start, pos_type end)
188 // FIXME: change tracking (MG)
189 // changes_.reset(new Changes(Change::UNCHANGED));
195 for (; i < end; ++i) {
196 switch (lookupChange(i).type) {
197 case Change::UNCHANGED:
200 case Change::INSERTED:
208 case Change::DELETED:
209 // FIXME: change tracking (MG)
210 changes_->set(Change(Change::UNCHANGED), i);
211 // No real char at position size():
212 if (i < size() && owner_->isInset(i))
213 // FIXME: change tracking (MG)
214 owner_->getInset(i)->setChange(Change(Change::UNCHANGED));
218 // FIXME: change tracking (MG)
219 // changes_->reset(Change::UNCHANGED);
223 Paragraph::value_type Paragraph::Pimpl::getChar(pos_type pos) const
225 return owner_->getChar(pos);
229 void Paragraph::Pimpl::insertChar(pos_type pos, value_type c, Change const & change)
231 BOOST_ASSERT(pos <= size());
234 changes_->record(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 = lower_bound(fontlist.begin(),
251 search_font, matchFT());
252 it != fontlist.end(); ++it)
254 it->pos(it->pos() + 1);
258 owner_->insetlist.increasePosAfterPos(pos);
262 void Paragraph::Pimpl::insertInset(pos_type pos,
263 InsetBase * inset, Change const & change)
266 BOOST_ASSERT(pos <= size());
268 insertChar(pos, META_INSET, change);
269 BOOST_ASSERT(owner_->text_[pos] == META_INSET);
271 // Add a new entry in the insetlist.
272 owner_->insetlist.insert(inset, pos);
276 void Paragraph::Pimpl::erase(pos_type pos)
278 // FIXME: change tracking (MG)
279 // do something like changes_->erase(i);
280 // in one of the next patches, the two erase functions
281 // will be merged but I don't want to break too many things at the same time :-)
283 // if it is an inset, delete the inset entry
284 if (owner_->text_[pos] == Paragraph::META_INSET) {
285 owner_->insetlist.erase(pos);
288 owner_->text_.erase(owner_->text_.begin() + pos);
290 // Erase entries in the tables.
291 FontTable search_font(pos, LyXFont());
293 FontList::iterator it =
294 lower_bound(fontlist.begin(),
296 search_font, matchFT());
297 if (it != fontlist.end() && it->pos() == pos &&
299 (it != fontlist.begin()
300 && boost::prior(it)->pos() == pos - 1))) {
301 // If it is a multi-character font
302 // entry, we just make it smaller
303 // (see update below), otherwise we
305 unsigned int const i = it - fontlist.begin();
306 fontlist.erase(fontlist.begin() + i);
307 it = fontlist.begin() + i;
308 if (i > 0 && i < fontlist.size() &&
309 fontlist[i - 1].font() == fontlist[i].font()) {
310 fontlist.erase(fontlist.begin() + i - 1);
311 it = fontlist.begin() + i - 1;
315 // Update all other entries
316 FontList::iterator fend = fontlist.end();
317 for (; it != fend; ++it)
318 it->pos(it->pos() - 1);
320 // Update the insetlist
321 owner_->insetlist.decreasePosAfterPos(pos);
325 bool Paragraph::Pimpl::erase(pos_type pos, bool trackChanges)
327 // FIXME: change tracking (MG)
328 BOOST_ASSERT(pos <= size());
331 Change::Type changetype(changes_->lookup(pos).type);
333 // only allow the actual removal if it was /new/ text
334 if (changetype != Change::INSERTED) {
335 changes_->record(Change(Change::DELETED), pos);
336 if (pos < size() && owner_->isInset(pos))
337 // FIXME: change tracking (MG)
338 owner_->getInset(pos)->setChange(Change(Change::DELETED));
343 // Don't physically access nonexistent end-of-paragraph char
353 int Paragraph::Pimpl::erase(pos_type start, pos_type end, bool trackChanges)
356 for (pos_type count = end - start; count; --count) {
357 if (!erase(i, trackChanges))
364 void Paragraph::Pimpl::simpleTeXBlanks(odocstream & os, TexRow & texrow,
366 unsigned int & column,
367 LyXFont const & font,
368 LyXLayout const & style)
373 if (column > lyxrc.ascii_linelen
375 && getChar(i - 1) != ' '
377 // same in FreeSpacing mode
378 && !owner_->isFreeSpacing()
379 // In typewriter mode, we want to avoid
380 // ! . ? : at the end of a line
381 && !(font.family() == LyXFont::TYPEWRITER_FAMILY
382 && (getChar(i - 1) == '.'
383 || getChar(i - 1) == '?'
384 || getChar(i - 1) == ':'
385 || getChar(i - 1) == '!'))) {
388 texrow.start(owner_->id(), i + 1);
390 } else if (style.free_spacing) {
398 bool Paragraph::Pimpl::isTextAt(string const & str, pos_type pos) const
400 pos_type const len = str.length();
402 // is the paragraph large enough?
403 if (pos + len > size())
406 // does the wanted text start at point?
407 for (string::size_type i = 0; i < str.length(); ++i) {
408 if (str[i] != owner_->text_[pos + i])
412 // is there a font change in middle of the word?
413 FontList::const_iterator cit = fontlist.begin();
414 FontList::const_iterator end = fontlist.end();
415 for (; cit != end; ++cit) {
416 if (cit->pos() >= pos)
419 if (cit != end && pos + len - 1 > cit->pos())
426 void Paragraph::Pimpl::simpleTeXSpecialChars(Buffer const & buf,
427 BufferParams const & bparams,
430 OutputParams const & runparams,
432 LyXFont & running_font,
434 LyXFont const & outerfont,
436 Change::Type & running_change,
437 LyXLayout const & style,
439 unsigned int & column,
442 if (style.pass_thru) {
443 if (c != Paragraph::META_INSET) {
447 owner_->getInset(i)->plaintext(buf, os, runparams);
451 // Two major modes: LaTeX or plain
452 // Handle here those cases common to both modes
453 // and then split to handle the two modes separately.
455 case Paragraph::META_INSET: {
456 InsetBase * inset = owner_->getInset(i);
458 // FIXME: remove this check
462 // FIXME: move this to InsetNewline::latex
463 if (inset->lyxCode() == InsetBase::NEWLINE_CODE) {
464 // newlines are handled differently here than
465 // the default in simpleTeXSpecialChars().
466 if (!style.newline_allowed) {
470 column += running_font.latexWriteEndChanges(os, basefont, basefont);
473 basefont = owner_->getLayoutFont(bparams, outerfont);
474 running_font = basefont;
476 if (font.family() == LyXFont::TYPEWRITER_FAMILY)
479 if (runparams.moving_arg)
485 texrow.start(owner_->id(), i + 1);
490 // output change tracking marks only if desired,
491 // if dvipost is installed,
492 // and with dvi/ps (other formats don't work)
493 LaTeXFeatures features(buf, bparams, runparams);
494 bool const output = bparams.outputChanges
495 && runparams.flavor == OutputParams::LATEX
496 && features.isAvailable("dvipost");
498 if (inset->canTrackChanges()) {
499 column += Changes::latexMarkChange(os, running_change,
500 Change::UNCHANGED, output);
501 running_change = Change::UNCHANGED;
505 odocstream::pos_type const len = os.tellp();
507 if ((inset->lyxCode() == InsetBase::GRAPHICS_CODE
508 || inset->lyxCode() == InsetBase::MATH_CODE
509 || inset->lyxCode() == InsetBase::URL_CODE)
510 && running_font.isRightToLeft()) {
516 #warning Bug: we can have an empty font change here!
517 // if there has just been a font change, we are going to close it
518 // right now, which means stupid latex code like \textsf{}. AFAIK,
519 // this does not harm dvi output. A minor bug, thus (JMarc)
521 // some insets cannot be inside a font change command
522 if (open_font && inset->noFontChange()) {
523 column +=running_font.
524 latexWriteEndChanges(os,
528 basefont = owner_->getLayoutFont(bparams, outerfont);
529 running_font = basefont;
532 int tmp = inset->latex(buf, os, runparams);
538 for (int j = 0; j < tmp; ++j) {
541 texrow.start(owner_->id(), i + 1);
544 column += os.tellp() - len;
550 // And now for the special cases within each mode
554 os << "\\textbackslash{}";
558 case '±': case '²': case '³':
559 case '×': case '÷': case '¹':
561 if ((bparams.inputenc == "latin1" ||
562 bparams.inputenc == "latin9") ||
563 (bparams.inputenc == "auto" &&
564 (font.language()->encoding()->latexName()
566 font.language()->encoding()->latexName()
568 os << "\\ensuremath{";
577 case '|': case '<': case '>':
578 // In T1 encoding, these characters exist
579 if (lyxrc.fontenc == "T1") {
581 //... but we should avoid ligatures
582 if ((c == '>' || c == '<')
584 && getChar(i + 1) == c) {
585 //os << "\\textcompwordmark{}";
586 // Jean-Marc, have a look at
587 // this. I think this works
595 // Typewriter font also has them
596 if (font.family() == LyXFont::TYPEWRITER_FAMILY) {
600 // Otherwise, we use what LaTeX
604 os << "\\textless{}";
608 os << "\\textgreater{}";
618 case '-': // "--" in Typewriter mode -> "-{}-"
620 && getChar(i + 1) == '-'
621 && font.family() == LyXFont::TYPEWRITER_FAMILY) {
630 os << "\\char`\\\"{}";
635 if (bparams.inputenc == "default") {
644 case '%': case '#': case '{':
652 os << "\\textasciitilde{}";
657 os << "\\textasciicircum{}";
662 // avoid being mistaken for optional arguments
670 // Blanks are printed before font switching.
671 // Sure? I am not! (try nice-latex)
672 // I am sure it's correct. LyX might be smarter
673 // in the future, but for now, nothing wrong is
679 // I assume this is hack treating typewriter as verbatim
680 if (font.family() == LyXFont::TYPEWRITER_FAMILY) {
689 // FIXME: if we have "LaTeX" with a font
690 // change in the middle (before the 'T', then
691 // the "TeX" part is still special cased.
692 // Really we should only operate this on
693 // "words" for some definition of word
697 for (; pnr < phrases_nr; ++pnr) {
698 if (isTextAt(special_phrases[pnr].phrase, i)) {
699 os << special_phrases[pnr].macro;
700 i += special_phrases[pnr].phrase.length() - 1;
701 column += special_phrases[pnr].macro.length() - 1;
706 if (pnr == phrases_nr && c != '\0') {
715 void Paragraph::Pimpl::validate(LaTeXFeatures & features,
716 LyXLayout const & layout) const
718 BufferParams const & bparams = features.bufferParams();
721 if (!params.spacing().isDefault())
722 features.require("setspace");
725 features.useLayout(layout.name());
728 Language const * doc_language = bparams.language;
730 FontList::const_iterator fcit = fontlist.begin();
731 FontList::const_iterator fend = fontlist.end();
732 for (; fcit != fend; ++fcit) {
733 if (fcit->font().noun() == LyXFont::ON) {
734 lyxerr[Debug::LATEX] << "font.noun: "
735 << fcit->font().noun()
737 features.require("noun");
738 lyxerr[Debug::LATEX] << "Noun enabled. Font: "
739 << fcit->font().stateText(0)
742 switch (fcit->font().color()) {
744 case LColor::inherit:
746 // probably we should put here all interface colors used for
747 // font displaying! For now I just add this ones I know of (Jug)
752 features.require("color");
753 lyxerr[Debug::LATEX] << "Color enabled. Font: "
754 << fcit->font().stateText(0)
758 Language const * language = fcit->font().language();
759 if (language->babel() != doc_language->babel() &&
760 language != ignore_language &&
761 language != latex_language)
763 features.useLanguage(language);
764 lyxerr[Debug::LATEX] << "Found language "
765 << language->babel() << endl;
769 if (!params.leftIndent().zero())
770 features.require("ParagraphLeftIndent");
773 InsetList::const_iterator icit = owner_->insetlist.begin();
774 InsetList::const_iterator iend = owner_->insetlist.end();
775 for (; icit != iend; ++icit) {
777 icit->inset->validate(features);
778 if (layout.needprotect &&
779 icit->inset->lyxCode() == InsetBase::FOOT_CODE)
780 features.require("NeedLyXFootnoteCode");
785 for (pos_type i = 0; i < size() ; ++i) {
786 for (size_t pnr = 0; pnr < phrases_nr; ++pnr) {
787 if (!special_phrases[pnr].builtin
788 && isTextAt(special_phrases[pnr].phrase, i)) {
789 features.require(special_phrases[pnr].phrase);