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>
35 using std::upper_bound;
36 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 special_phrases[] = {
53 { "LyX", "\\LyX{}", false },
54 { "TeX", "\\TeX{}", true },
55 { "LaTeX2e", "\\LaTeXe{}", true },
56 { "LaTeX", "\\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), owner_(owner)
75 inset_owner = p.inset_owner;
76 fontlist = p.fontlist;
80 changes_.reset(new Changes(*p.changes_.get()));
84 void Paragraph::Pimpl::setContentsFromPar(Paragraph const & par)
86 owner_->text_ = par.text_;
87 if (par.pimpl_->tracking()) {
88 changes_.reset(new Changes(*(par.pimpl_->changes_.get())));
93 void Paragraph::Pimpl::trackChanges(Change::Type type)
96 lyxerr[Debug::CHANGES] << "already tracking for par " << id_ << endl;
100 lyxerr[Debug::CHANGES] << "track changes for par "
101 << id_ << " type " << type << endl;
102 changes_.reset(new Changes(type));
103 changes_->set(type, 0, size() + 1);
107 void Paragraph::Pimpl::untrackChanges()
113 void Paragraph::Pimpl::cleanChanges(Paragraph::ChangeTracking ct)
115 // if the paragraph was not tracked and we don't know the buffer's
116 // change tracking state, we do nothing
117 if ((ct == Paragraph::trackingUnknown) && !tracking())
120 // untrack everything if we are in a buffer where ct is disabled
121 else if (ct == Paragraph::trackingOff) {
126 // in a buffer where ct is enabled, set everything to INSERTED
127 changes_.reset(new Changes(Change::INSERTED));
128 changes_->set(Change::INSERTED, 0, size() + 1);
132 bool Paragraph::Pimpl::isChanged(pos_type start, pos_type end) const
137 return changes_->isChange(start, end);
141 bool Paragraph::Pimpl::isChangeEdited(pos_type start, pos_type end) const
146 return changes_->isChangeEdited(start, end);
150 void Paragraph::Pimpl::setChange(pos_type pos, Change::Type type)
155 changes_->set(type, pos);
159 void Paragraph::Pimpl::setChangeFull(pos_type pos, Change change)
164 changes_->set(change, pos);
167 Change::Type Paragraph::Pimpl::lookupChange(pos_type pos) const
170 return Change::UNCHANGED;
172 return changes_->lookup(pos);
176 Change const Paragraph::Pimpl::lookupChangeFull(pos_type pos) const
179 return Change(Change::UNCHANGED);
181 return changes_->lookupFull(pos);
185 void Paragraph::Pimpl::markErased(bool erased)
187 BOOST_ASSERT(tracking());
192 for (pos_type i = 0; i < size(); ++i) {
193 changes_->set(Change::UNCHANGED, i);
194 if (owner_->isInset(i))
195 owner_->getInset(i)->markErased(false);
201 void Paragraph::Pimpl::acceptChange(pos_type start, pos_type end)
207 changes_.reset(new Changes(Change::UNCHANGED));
211 lyxerr[Debug::CHANGES] << "acceptchange" << endl;
214 for (; i < end; ++i) {
215 switch (lookupChange(i)) {
216 case Change::UNCHANGED:
219 case Change::INSERTED:
220 changes_->set(Change::UNCHANGED, i);
223 case Change::DELETED:
224 // Suppress access to nonexistent
225 // "end-of-paragraph char":
236 lyxerr[Debug::CHANGES] << "endacceptchange" << endl;
237 changes_->reset(Change::UNCHANGED);
241 void Paragraph::Pimpl::rejectChange(pos_type start, pos_type end)
247 changes_.reset(new Changes(Change::UNCHANGED));
253 for (; i < end; ++i) {
254 switch (lookupChange(i)) {
255 case Change::UNCHANGED:
258 case Change::INSERTED:
267 case Change::DELETED:
268 changes_->set(Change::UNCHANGED, i);
269 // No real char at position size():
270 if (i < size() && owner_->isInset(i))
271 owner_->getInset(i)->markErased(false);
275 changes_->reset(Change::UNCHANGED);
279 Paragraph::value_type Paragraph::Pimpl::getChar(pos_type pos) const
281 return owner_->getChar(pos);
285 void Paragraph::Pimpl::insertChar(pos_type pos, value_type c, Change change)
287 BOOST_ASSERT(pos <= size());
290 changes_->record(change, pos);
293 // This is actually very common when parsing buffers (and
294 // maybe inserting ascii text)
296 // when appending characters, no need to update tables
297 owner_->text_.push_back(c);
301 owner_->text_.insert(owner_->text_.begin() + pos, c);
303 // Update the font table.
304 FontTable search_font(pos, LyXFont());
305 for (FontList::iterator it = lower_bound(fontlist.begin(),
307 search_font, matchFT());
308 it != fontlist.end(); ++it)
310 it->pos(it->pos() + 1);
314 owner_->insetlist.increasePosAfterPos(pos);
318 void Paragraph::Pimpl::insertInset(pos_type pos,
319 InsetBase * inset, Change change)
322 BOOST_ASSERT(pos <= size());
324 insertChar(pos, META_INSET, change);
325 BOOST_ASSERT(owner_->text_[pos] == META_INSET);
327 // Add a new entry in the insetlist.
328 owner_->insetlist.insert(inset, pos);
332 void Paragraph::Pimpl::eraseIntern(pos_type pos)
334 // if it is an inset, delete the inset entry
335 if (owner_->text_[pos] == Paragraph::META_INSET) {
336 owner_->insetlist.erase(pos);
339 owner_->text_.erase(owner_->text_.begin() + pos);
341 // Erase entries in the tables.
342 FontTable search_font(pos, LyXFont());
344 FontList::iterator it =
345 lower_bound(fontlist.begin(),
347 search_font, matchFT());
348 if (it != fontlist.end() && it->pos() == pos &&
350 (it != fontlist.begin()
351 && boost::prior(it)->pos() == pos - 1))) {
352 // If it is a multi-character font
353 // entry, we just make it smaller
354 // (see update below), otherwise we
356 unsigned int const i = it - fontlist.begin();
357 fontlist.erase(fontlist.begin() + i);
358 it = fontlist.begin() + i;
359 if (i > 0 && i < fontlist.size() &&
360 fontlist[i - 1].font() == fontlist[i].font()) {
361 fontlist.erase(fontlist.begin() + i - 1);
362 it = fontlist.begin() + i - 1;
366 // Update all other entries.
367 FontList::iterator fend = fontlist.end();
368 for (; it != fend; ++it)
369 it->pos(it->pos() - 1);
371 // Update the insetlist.
372 owner_->insetlist.decreasePosAfterPos(pos);
376 bool Paragraph::Pimpl::erase(pos_type pos)
378 BOOST_ASSERT(pos <= size());
381 Change::Type changetype(changes_->lookup(pos));
382 changes_->record(Change(Change::DELETED), pos);
384 // only allow the actual removal if it was /new/ text
385 if (changetype != Change::INSERTED) {
386 if (pos < size() && owner_->isInset(pos))
387 owner_->getInset(pos)->markErased(true);
392 // Don't physically access nonexistent end-of-paragraph char
402 int Paragraph::Pimpl::erase(pos_type start, pos_type end)
405 for (pos_type count = end - start; count; --count) {
413 void Paragraph::Pimpl::simpleTeXBlanks(ostream & os, TexRow & texrow,
415 unsigned int & column,
416 LyXFont const & font,
417 LyXLayout const & style)
422 if (column > lyxrc.ascii_linelen
424 && getChar(i - 1) != ' '
426 // same in FreeSpacing mode
427 && !owner_->isFreeSpacing()
428 // In typewriter mode, we want to avoid
429 // ! . ? : at the end of a line
430 && !(font.family() == LyXFont::TYPEWRITER_FAMILY
431 && (getChar(i - 1) == '.'
432 || getChar(i - 1) == '?'
433 || getChar(i - 1) == ':'
434 || getChar(i - 1) == '!'))) {
437 texrow.start(owner_->id(), i + 1);
439 } else if (style.free_spacing) {
447 bool Paragraph::Pimpl::isTextAt(string const & str, pos_type pos) const
449 pos_type const len = str.length();
451 // is the paragraph large enough?
452 if (pos + len > size())
455 // does the wanted text start at point?
456 for (string::size_type i = 0; i < str.length(); ++i) {
457 if (str[i] != owner_->text_[pos + i])
461 // is there a font change in middle of the word?
462 FontList::const_iterator cit = fontlist.begin();
463 FontList::const_iterator end = fontlist.end();
464 for (; cit != end; ++cit) {
465 if (cit->pos() >= pos)
468 if (cit != end && pos + len - 1 > cit->pos())
475 void Paragraph::Pimpl::simpleTeXSpecialChars(Buffer const & buf,
476 BufferParams const & bparams,
479 OutputParams const & runparams,
481 LyXFont & running_font,
483 LyXFont const & outerfont,
485 Change::Type & running_change,
486 LyXLayout const & style,
488 unsigned int & column,
491 if (style.pass_thru) {
492 if (c != Paragraph::META_INSET) {
496 owner_->getInset(i)->plaintext(buf, os, runparams);
501 // Two major modes: LaTeX or plain
502 // Handle here those cases common to both modes
503 // and then split to handle the two modes separately.
505 case Paragraph::META_INSET: {
506 InsetBase * inset = owner_->getInset(i);
508 // FIXME: remove this check
512 // FIXME: move this to InsetNewline::latex
513 if (inset->lyxCode() == InsetBase::NEWLINE_CODE) {
514 // newlines are handled differently here than
515 // the default in simpleTeXSpecialChars().
516 if (!style.newline_allowed) {
520 column += running_font.latexWriteEndChanges(os, basefont, basefont);
523 basefont = owner_->getLayoutFont(bparams, outerfont);
524 running_font = basefont;
526 if (font.family() == LyXFont::TYPEWRITER_FAMILY)
529 if (runparams.moving_arg)
535 texrow.start(owner_->id(), i + 1);
540 // output change tracking marks only if desired,
541 // if dvipost is installed,
542 // and with dvi/ps (other formats don't work)
543 LaTeXFeatures features(buf, bparams, runparams);
544 bool const output = bparams.output_changes
545 && runparams.flavor == OutputParams::LATEX
546 && features.isAvailable("dvipost");
548 if (inset->canTrackChanges()) {
549 column += Changes::latexMarkChange(os, running_change,
550 Change::UNCHANGED, output);
551 running_change = Change::UNCHANGED;
555 ostream::pos_type const len = os.tellp();
557 if ((inset->lyxCode() == InsetBase::GRAPHICS_CODE
558 || inset->lyxCode() == InsetBase::MATH_CODE
559 || inset->lyxCode() == InsetBase::URL_CODE)
560 && running_font.isRightToLeft()) {
566 #warning Bug: we can have an empty font change here!
567 // if there has just been a font change, we are going to close it
568 // right now, which means stupid latex code like \textsf{}. AFAIK,
569 // this does not harm dvi output. A minor bug, thus (JMarc)
571 // some insets cannot be inside a font change command
572 if (open_font && inset->noFontChange()) {
573 column +=running_font.
574 latexWriteEndChanges(os,
578 basefont = owner_->getLayoutFont(bparams, outerfont);
579 running_font = basefont;
582 int tmp = inset->latex(buf, os, runparams);
588 for (int j = 0; j < tmp; ++j) {
591 texrow.start(owner_->id(), i + 1);
594 column += os.tellp() - len;
600 // And now for the special cases within each mode
604 os << "\\textbackslash{}";
608 case '±': case '²': case '³':
609 case '×': case '÷': case '¹':
611 if ((bparams.inputenc == "latin1" ||
612 bparams.inputenc == "latin9") ||
613 (bparams.inputenc == "auto" &&
614 (font.language()->encoding()->latexName()
616 font.language()->encoding()->latexName()
618 os << "\\ensuremath{"
627 case '|': case '<': case '>':
628 // In T1 encoding, these characters exist
629 if (lyxrc.fontenc == "T1") {
631 //... but we should avoid ligatures
632 if ((c == '>' || c == '<')
634 && getChar(i + 1) == c) {
635 //os << "\\textcompwordmark{}";
636 // Jean-Marc, have a look at
637 // this. I think this works
645 // Typewriter font also has them
646 if (font.family() == LyXFont::TYPEWRITER_FAMILY) {
650 // Otherwise, we use what LaTeX
654 os << "\\textless{}";
658 os << "\\textgreater{}";
668 case '-': // "--" in Typewriter mode -> "-{}-"
670 && getChar(i + 1) == '-'
671 && font.family() == LyXFont::TYPEWRITER_FAMILY) {
680 os << "\\char`\\\"{}";
685 if (bparams.inputenc == "default") {
694 case '%': case '#': case '{':
701 os << "\\textasciitilde{}";
706 os << "\\textasciicircum{}";
711 // avoid being mistaken for optional arguments
712 os << '{' << c << '}';
717 // Blanks are printed before font switching.
718 // Sure? I am not! (try nice-latex)
719 // I am sure it's correct. LyX might be smarter
720 // in the future, but for now, nothing wrong is
726 // I assume this is hack treating typewriter as verbatim
727 if (font.family() == LyXFont::TYPEWRITER_FAMILY) {
736 // FIXME: if we have "LaTeX" with a font
737 // change in the middle (before the 'T', then
738 // the "TeX" part is still special cased.
739 // Really we should only operate this on
740 // "words" for some definition of word
744 for (; pnr < phrases_nr; ++pnr) {
745 if (isTextAt(special_phrases[pnr].phrase, i)) {
746 os << special_phrases[pnr].macro;
747 i += special_phrases[pnr].phrase.length() - 1;
748 column += special_phrases[pnr].macro.length() - 1;
753 if (pnr == phrases_nr && c != '\0') {
762 void Paragraph::Pimpl::validate(LaTeXFeatures & features,
763 LyXLayout const & layout) const
765 BufferParams const & bparams = features.bufferParams();
768 if (!params.spacing().isDefault())
769 features.require("setspace");
772 features.useLayout(layout.name());
775 Language const * doc_language = bparams.language;
777 FontList::const_iterator fcit = fontlist.begin();
778 FontList::const_iterator fend = fontlist.end();
779 for (; fcit != fend; ++fcit) {
780 if (fcit->font().noun() == LyXFont::ON) {
781 lyxerr[Debug::LATEX] << "font.noun: "
782 << fcit->font().noun()
784 features.require("noun");
785 lyxerr[Debug::LATEX] << "Noun enabled. Font: "
786 << fcit->font().stateText(0)
789 switch (fcit->font().color()) {
791 case LColor::inherit:
793 // probably we should put here all interface colors used for
794 // font displaying! For now I just add this ones I know of (Jug)
799 features.require("color");
800 lyxerr[Debug::LATEX] << "Color enabled. Font: "
801 << fcit->font().stateText(0)
805 Language const * language = fcit->font().language();
806 if (language->babel() != doc_language->babel() &&
807 language != ignore_language &&
808 language != latex_language)
810 features.useLanguage(language);
811 lyxerr[Debug::LATEX] << "Found language "
812 << language->babel() << endl;
816 if (!params.leftIndent().zero())
817 features.require("ParagraphLeftIndent");
820 InsetList::const_iterator icit = owner_->insetlist.begin();
821 InsetList::const_iterator iend = owner_->insetlist.end();
822 for (; icit != iend; ++icit) {
824 icit->inset->validate(features);
825 if (layout.needprotect &&
826 icit->inset->lyxCode() == InsetBase::FOOT_CODE)
827 features.require("NeedLyXFootnoteCode");
832 for (pos_type i = 0; i < size() ; ++i) {
833 for (size_t pnr = 0; pnr < phrases_nr; ++pnr) {
834 if (!special_phrases[pnr].builtin
835 && isTextAt(special_phrases[pnr].phrase, i)) {
836 features.require(special_phrases[pnr].phrase);