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"
18 #include "bufferparams.h"
22 #include "LaTeXFeatures.h"
23 #include "latexrunparams.h"
27 #include "support/LAssert.h"
29 using namespace lyx::support;
34 using std::upper_bound;
39 // Initialize static member.
40 ShareContainer<LyXFont> Paragraph::Pimpl::FontTable::container;
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;
77 fontlist = p.fontlist;
81 changes_.reset(new Changes(*p.changes_.get()));
85 void Paragraph::Pimpl::clear()
92 void Paragraph::Pimpl::setContentsFromPar(Paragraph const & par)
94 text = par.pimpl_->text;
95 if (par.pimpl_->tracking()) {
96 changes_.reset(new Changes(*(par.pimpl_->changes_.get())));
101 void Paragraph::Pimpl::trackChanges(Change::Type type)
104 lyxerr[Debug::CHANGES] << "already tracking for par " << id_ << endl;
108 lyxerr[Debug::CHANGES] << "track changes for par "
109 << id_ << " type " << type << endl;
110 changes_.reset(new Changes(type));
111 changes_->set(type, 0, size());
115 void Paragraph::Pimpl::untrackChanges()
121 void Paragraph::Pimpl::cleanChanges()
123 // if we're not tracking, we don't want to reset...
127 changes_.reset(new Changes(Change::INSERTED));
128 changes_->set(Change::INSERTED, 0, size());
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 Change::Type Paragraph::Pimpl::lookupChange(pos_type pos) const
162 return Change::UNCHANGED;
164 return changes_->lookup(pos);
168 Change const Paragraph::Pimpl::lookupChangeFull(pos_type pos) const
171 return Change(Change::UNCHANGED);
173 return changes_->lookupFull(pos);
177 void Paragraph::Pimpl::markErased()
181 // FIXME: we should actually remove INSERTED chars.
182 // difficult because owning insettexts/tabulars need
183 // to update themselves when rows etc. change
184 changes_->set(Change::DELETED, 0, size());
185 changes_->reset(Change::DELETED);
189 void Paragraph::Pimpl::acceptChange(pos_type start, pos_type end)
195 changes_.reset(new Changes(Change::UNCHANGED));
199 lyxerr << "acceptchange" << endl;
202 for (; i < end; ++i) {
203 switch (lookupChange(i)) {
204 case Change::UNCHANGED:
207 case Change::INSERTED:
208 changes_->set(Change::UNCHANGED, i);
211 case Change::DELETED:
220 lyxerr << "endacceptchange" << endl;
221 changes_->reset(Change::UNCHANGED);
225 void Paragraph::Pimpl::rejectChange(pos_type start, pos_type end)
231 changes_.reset(new Changes(Change::UNCHANGED));
237 for (; i < end; ++i) {
238 switch (lookupChange(i)) {
239 case Change::UNCHANGED:
242 case Change::INSERTED:
249 case Change::DELETED:
250 changes_->set(Change::UNCHANGED, i);
254 changes_->reset(Change::UNCHANGED);
258 Paragraph::value_type Paragraph::Pimpl::getChar(pos_type pos) const
261 // This is in the critical path for loading!
262 pos_type const siz = size();
267 lyxerr << "getChar() on pos " << pos << " in par id "
268 << owner_->id() << " of size " << siz
269 << " is a bit silly !" << endl;
275 Assert(pos < size());
281 void Paragraph::Pimpl::setChar(pos_type pos, value_type c)
288 void Paragraph::Pimpl::insertChar(pos_type pos, value_type c,
289 LyXFont const & font, Change change)
291 Assert(pos <= size());
294 changes_->record(change, pos);
297 // This is actually very common when parsing buffers (and
298 // maybe inserting ascii text)
300 // when appending characters, no need to update tables
302 owner_->setFont(pos, font);
306 text.insert(text.begin() + pos, c);
308 // Update the font table.
309 FontTable search_font(pos, LyXFont());
310 for (FontList::iterator it = lower_bound(fontlist.begin(),
312 search_font, matchFT());
313 it != fontlist.end(); ++it)
315 it->pos(it->pos() + 1);
319 owner_->insetlist.increasePosAfterPos(pos);
321 owner_->setFont(pos, font);
325 void Paragraph::Pimpl::insertInset(pos_type pos,
326 InsetOld * inset, LyXFont const & font, Change change)
329 Assert(pos <= size());
331 insertChar(pos, META_INSET, font, change);
332 Assert(text[pos] == META_INSET);
334 // Add a new entry in the insetlist.
335 owner_->insetlist.insert(inset, pos);
338 inset->setOwner(inset_owner);
342 void Paragraph::Pimpl::eraseIntern(pos_type pos)
344 // if it is an inset, delete the inset entry
345 if (text[pos] == Paragraph::META_INSET) {
346 owner_->insetlist.erase(pos);
349 text.erase(text.begin() + pos);
351 // Erase entries in the tables.
352 FontTable search_font(pos, LyXFont());
354 FontList::iterator it =
355 lower_bound(fontlist.begin(),
357 search_font, matchFT());
358 if (it != fontlist.end() && it->pos() == pos &&
360 (it != fontlist.begin()
361 && boost::prior(it)->pos() == pos - 1))) {
362 // If it is a multi-character font
363 // entry, we just make it smaller
364 // (see update below), otherwise we
366 unsigned int const i = it - fontlist.begin();
367 fontlist.erase(fontlist.begin() + i);
368 it = fontlist.begin() + i;
369 if (i > 0 && i < fontlist.size() &&
370 fontlist[i - 1].font() == fontlist[i].font()) {
371 fontlist.erase(fontlist.begin() + i - 1);
372 it = fontlist.begin() + i - 1;
376 // Update all other entries.
377 FontList::iterator fend = fontlist.end();
378 for (; it != fend; ++it)
379 it->pos(it->pos() - 1);
381 // Update the insetlist.
382 owner_->insetlist.decreasePosAfterPos(pos);
386 bool Paragraph::Pimpl::erase(pos_type pos)
388 Assert(pos < size());
391 Change::Type changetype(changes_->lookup(pos));
392 changes_->record(Change(Change::DELETED), pos);
394 // only allow the actual removal if it was /new/ text
395 if (changetype != Change::INSERTED) {
396 if (text[pos] == Paragraph::META_INSET) {
397 InsetOld * i(owner_->getInset(pos));
409 int Paragraph::Pimpl::erase(pos_type start, pos_type end)
412 pos_type count = end - start;
423 void Paragraph::Pimpl::simpleTeXBlanks(ostream & os, TexRow & texrow,
425 unsigned int & column,
426 LyXFont const & font,
427 LyXLayout const & style)
432 if (column > lyxrc.ascii_linelen
434 && getChar(i - 1) != ' '
436 // same in FreeSpacing mode
437 && !owner_->isFreeSpacing()
438 // In typewriter mode, we want to avoid
439 // ! . ? : at the end of a line
440 && !(font.family() == LyXFont::TYPEWRITER_FAMILY
441 && (getChar(i - 1) == '.'
442 || getChar(i - 1) == '?'
443 || getChar(i - 1) == ':'
444 || getChar(i - 1) == '!'))) {
447 texrow.start(owner_->id(), i + 1);
449 } else if (style.free_spacing) {
457 bool Paragraph::Pimpl::isTextAt(string const & str, pos_type pos) const
459 pos_type const len = str.length();
461 // is the paragraph large enough?
462 if (pos + len > size())
465 // does the wanted text start at point?
466 for (string::size_type i = 0; i < str.length(); ++i) {
467 if (str[i] != text[pos + i])
471 // is there a font change in middle of the word?
472 FontList::const_iterator cit = fontlist.begin();
473 FontList::const_iterator end = fontlist.end();
474 for (; cit != end; ++cit) {
475 if (cit->pos() >= pos)
478 if (cit != end && pos + len - 1 > cit->pos())
485 void Paragraph::Pimpl::simpleTeXSpecialChars(Buffer const & buf,
486 BufferParams const & bparams,
489 LatexRunParams const & runparams,
491 LyXFont & running_font,
493 LyXFont const & outerfont,
495 Change::Type & running_change,
496 LyXLayout const & style,
498 unsigned int & column,
501 if (style.pass_thru) {
502 if (c != Paragraph::META_INSET) {
506 InsetOld const * inset = owner_->getInset(i);
507 inset->ascii(buf, os, 0);
512 // Two major modes: LaTeX or plain
513 // Handle here those cases common to both modes
514 // and then split to handle the two modes separately.
516 case Paragraph::META_INSET: {
517 InsetOld * inset = owner_->getInset(i);
519 // FIXME: remove this check
523 // FIXME: move this to InsetNewline::latex
524 if (inset->lyxCode() == InsetOld::NEWLINE_CODE) {
525 // newlines are handled differently here than
526 // the default in simpleTeXSpecialChars().
527 if (!style.newline_allowed) {
531 column += running_font.latexWriteEndChanges(os, basefont, basefont);
534 basefont = owner_->getLayoutFont(bparams, outerfont);
535 running_font = basefont;
537 if (font.family() == LyXFont::TYPEWRITER_FAMILY)
540 if (runparams.moving_arg)
546 texrow.start(owner_->id(), i + 1);
551 if (inset->isTextInset()) {
552 column += Changes::latexMarkChange(os, running_change,
554 running_change = Change::UNCHANGED;
558 int const len = os.tellp();
559 //ostream::pos_type const len = os.tellp();
560 if ((inset->lyxCode() == InsetOld::GRAPHICS_CODE
561 || inset->lyxCode() == InsetOld::MATH_CODE
562 || inset->lyxCode() == InsetOld::URL_CODE)
563 && running_font.isRightToLeft()) {
569 #warning Bug: we can have an empty font change here!
570 // if there has just been a font change, we are going to close it
571 // right now, which means stupid latex code like \textsf{}. AFAIK,
572 // this does not harm dvi output. A minor bug, thus (JMarc)
574 // some insets cannot be inside a font change command
575 if (open_font && inset->noFontChange()) {
576 column +=running_font.
577 latexWriteEndChanges(os,
581 basefont = owner_->getLayoutFont(bparams, outerfont);
582 running_font = basefont;
585 int tmp = inset->latex(buf, os, runparams);
591 for (int j = 0; j < tmp; ++j) {
594 texrow.start(owner_->id(), i + 1);
597 column += int(os.tellp()) - len;
603 // And now for the special cases within each mode
607 os << "\\textbackslash{}";
611 case '±': case '²': case '³':
612 case '×': case '÷': case '¹':
614 if ((bparams.inputenc == "latin1" ||
615 bparams.inputenc == "latin9") ||
616 (bparams.inputenc == "auto" &&
617 (font.language()->encoding()->LatexName()
619 font.language()->encoding()->LatexName()
621 os << "\\ensuremath{"
630 case '|': case '<': case '>':
631 // In T1 encoding, these characters exist
632 if (lyxrc.fontenc == "T1") {
634 //... but we should avoid ligatures
635 if ((c == '>' || c == '<')
637 && getChar(i + 1) == c) {
638 //os << "\\textcompwordmark{}";
639 // Jean-Marc, have a look at
640 // this. I think this works
648 // Typewriter font also has them
649 if (font.family() == LyXFont::TYPEWRITER_FAMILY) {
653 // Otherwise, we use what LaTeX
657 os << "\\textless{}";
661 os << "\\textgreater{}";
671 case '-': // "--" in Typewriter mode -> "-{}-"
673 && getChar(i + 1) == '-'
674 && font.family() == LyXFont::TYPEWRITER_FAMILY) {
683 os << "\\char`\\\"{}";
688 if (bparams.inputenc == "default") {
697 case '%': case '#': case '{':
704 os << "\\textasciitilde{}";
709 os << "\\textasciicircum{}";
713 case '*': case '[': case ']':
714 // avoid being mistaken for optional arguments
715 os << '{' << c << '}';
720 // Blanks are printed before font switching.
721 // Sure? I am not! (try nice-latex)
722 // I am sure it's correct. LyX might be smarter
723 // in the future, but for now, nothing wrong is
729 // I assume this is hack treating typewriter as verbatim
730 if (font.family() == LyXFont::TYPEWRITER_FAMILY) {
739 // FIXME: if we have "LaTeX" with a font
740 // change in the middle (before the 'T', then
741 // the "TeX" part is still special cased.
742 // Really we should only operate this on
743 // "words" for some definition of word
747 for (; pnr < phrases_nr; ++pnr) {
748 if (isTextAt(special_phrases[pnr].phrase, i)) {
749 os << special_phrases[pnr].macro;
750 i += special_phrases[pnr].phrase.length() - 1;
751 column += special_phrases[pnr].macro.length() - 1;
756 if (pnr == phrases_nr && c != '\0') {
765 void Paragraph::Pimpl::validate(LaTeXFeatures & features,
766 LyXLayout const & layout) const
768 BufferParams const & bparams = features.bufferParams();
771 if (params.lineTop() || params.lineBottom())
772 features.require("lyxline");
773 if (!params.spacing().isDefault())
774 features.require("setspace");
777 features.useLayout(layout.name());
780 Language const * doc_language = bparams.language;
782 FontList::const_iterator fcit = fontlist.begin();
783 FontList::const_iterator fend = fontlist.end();
784 for (; fcit != fend; ++fcit) {
785 if (fcit->font().noun() == LyXFont::ON) {
786 lyxerr[Debug::LATEX] << "font.noun: "
787 << fcit->font().noun()
789 features.require("noun");
790 lyxerr[Debug::LATEX] << "Noun enabled. Font: "
791 << fcit->font().stateText(0)
794 switch (fcit->font().color()) {
796 case LColor::inherit:
798 // probably we should put here all interface colors used for
799 // font displaying! For now I just add this ones I know of (Jug)
804 features.require("color");
805 lyxerr[Debug::LATEX] << "Color enabled. Font: "
806 << fcit->font().stateText(0)
810 Language const * language = fcit->font().language();
811 if (language->babel() != doc_language->babel() &&
812 language != ignore_language &&
813 language != latex_language)
815 features.useLanguage(language);
816 lyxerr[Debug::LATEX] << "Found language "
817 << language->babel() << endl;
821 if (!params.leftIndent().zero())
822 features.require("ParagraphLeftIndent");
825 InsetList::iterator icit = owner_->insetlist.begin();
826 InsetList::iterator iend = owner_->insetlist.end();
827 for (; icit != iend; ++icit) {
829 icit->inset->validate(features);
830 if (layout.needprotect &&
831 icit->inset->lyxCode() == InsetOld::FOOT_CODE)
832 features.require("NeedLyXFootnoteCode");
837 for (pos_type i = 0; i < size() ; ++i) {
838 for (size_t pnr = 0; pnr < phrases_nr; ++pnr) {
839 if (!special_phrases[pnr].builtin
840 && isTextAt(special_phrases[pnr].phrase, i)) {
841 features.require(special_phrases[pnr].phrase);