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"
24 #include "lyxlength.h"
28 #include "support/LAssert.h"
30 using namespace lyx::support;
35 using std::upper_bound;
40 // Initialize static member.
41 ShareContainer<LyXFont> Paragraph::Pimpl::FontTable::container;
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 special_phrases[] = {
54 { "LyX", "\\LyX{}", false },
55 { "TeX", "\\TeX{}", true },
56 { "LaTeX2e", "\\LaTeXe{}", true },
57 { "LaTeX", "\\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;
78 fontlist = p.fontlist;
82 changes_.reset(new Changes(*p.changes_.get()));
86 void Paragraph::Pimpl::clear()
93 void Paragraph::Pimpl::setContentsFromPar(Paragraph const & par)
95 text = par.pimpl_->text;
96 if (par.pimpl_->tracking()) {
97 changes_.reset(new Changes(*(par.pimpl_->changes_.get())));
102 void Paragraph::Pimpl::trackChanges(Change::Type type)
105 lyxerr[Debug::CHANGES] << "already tracking for par " << id_ << endl;
109 lyxerr[Debug::CHANGES] << "track changes for par "
110 << id_ << " type " << type << endl;
111 changes_.reset(new Changes(type));
112 changes_->set(type, 0, size());
116 void Paragraph::Pimpl::untrackChanges()
122 void Paragraph::Pimpl::cleanChanges()
124 // if we're not tracking, we don't want to reset...
128 changes_.reset(new Changes(Change::INSERTED));
129 changes_->set(Change::INSERTED, 0, size());
133 bool Paragraph::Pimpl::isChanged(pos_type start, pos_type end) const
138 return changes_->isChange(start, end);
142 bool Paragraph::Pimpl::isChangeEdited(pos_type start, pos_type end) const
147 return changes_->isChangeEdited(start, end);
151 void Paragraph::Pimpl::setChange(pos_type pos, Change::Type type)
156 changes_->set(type, pos);
160 Change::Type Paragraph::Pimpl::lookupChange(pos_type pos) const
163 return Change::UNCHANGED;
165 return changes_->lookup(pos);
169 Change const Paragraph::Pimpl::lookupChangeFull(pos_type pos) const
172 return Change(Change::UNCHANGED);
174 return changes_->lookupFull(pos);
178 void Paragraph::Pimpl::markErased()
182 // FIXME: we should actually remove INSERTED chars.
183 // difficult because owning insettexts/tabulars need
184 // to update themselves when rows etc. change
185 changes_->set(Change::DELETED, 0, size());
186 changes_->reset(Change::DELETED);
190 void Paragraph::Pimpl::acceptChange(pos_type start, pos_type end)
196 changes_.reset(new Changes(Change::UNCHANGED));
200 lyxerr << "acceptchange" << endl;
203 for (; i < end; ++i) {
204 switch (lookupChange(i)) {
205 case Change::UNCHANGED:
208 case Change::INSERTED:
209 changes_->set(Change::UNCHANGED, i);
212 case Change::DELETED:
221 lyxerr << "endacceptchange" << endl;
222 changes_->reset(Change::UNCHANGED);
226 void Paragraph::Pimpl::rejectChange(pos_type start, pos_type end)
232 changes_.reset(new Changes(Change::UNCHANGED));
238 for (; i < end; ++i) {
239 switch (lookupChange(i)) {
240 case Change::UNCHANGED:
243 case Change::INSERTED:
250 case Change::DELETED:
251 changes_->set(Change::UNCHANGED, i);
255 changes_->reset(Change::UNCHANGED);
259 Paragraph::value_type Paragraph::Pimpl::getChar(pos_type pos) const
262 // This is in the critical path for loading!
263 pos_type const siz = size();
268 lyxerr << "getChar() on pos " << pos << " in par id "
269 << owner_->id() << " of size " << siz
270 << " is a bit silly !" << endl;
276 Assert(pos < size());
282 void Paragraph::Pimpl::setChar(pos_type pos, value_type c)
289 void Paragraph::Pimpl::insertChar(pos_type pos, value_type c,
290 LyXFont const & font, Change change)
292 Assert(pos <= size());
295 changes_->record(change, pos);
298 // This is actually very common when parsing buffers (and
299 // maybe inserting ascii text)
301 // when appending characters, no need to update tables
303 owner_->setFont(pos, font);
307 text.insert(text.begin() + pos, c);
309 // Update the font table.
310 FontTable search_font(pos, LyXFont());
311 for (FontList::iterator it = lower_bound(fontlist.begin(),
313 search_font, matchFT());
314 it != fontlist.end(); ++it)
316 it->pos(it->pos() + 1);
320 owner_->insetlist.increasePosAfterPos(pos);
322 owner_->setFont(pos, font);
326 void Paragraph::Pimpl::insertInset(pos_type pos,
327 InsetOld * inset, LyXFont const & font, Change change)
330 Assert(pos <= size());
332 insertChar(pos, META_INSET, font, change);
333 Assert(text[pos] == META_INSET);
335 // Add a new entry in the insetlist.
336 owner_->insetlist.insert(inset, pos);
339 inset->setOwner(inset_owner);
343 void Paragraph::Pimpl::eraseIntern(pos_type pos)
345 // if it is an inset, delete the inset entry
346 if (text[pos] == Paragraph::META_INSET) {
347 owner_->insetlist.erase(pos);
350 text.erase(text.begin() + pos);
352 // Erase entries in the tables.
353 FontTable search_font(pos, LyXFont());
355 FontList::iterator it =
356 lower_bound(fontlist.begin(),
358 search_font, matchFT());
359 if (it != fontlist.end() && it->pos() == pos &&
361 (it != fontlist.begin()
362 && boost::prior(it)->pos() == pos - 1))) {
363 // If it is a multi-character font
364 // entry, we just make it smaller
365 // (see update below), otherwise we
367 unsigned int const i = it - fontlist.begin();
368 fontlist.erase(fontlist.begin() + i);
369 it = fontlist.begin() + i;
370 if (i > 0 && i < fontlist.size() &&
371 fontlist[i - 1].font() == fontlist[i].font()) {
372 fontlist.erase(fontlist.begin() + i - 1);
373 it = fontlist.begin() + i - 1;
377 // Update all other entries.
378 FontList::iterator fend = fontlist.end();
379 for (; it != fend; ++it)
380 it->pos(it->pos() - 1);
382 // Update the insetlist.
383 owner_->insetlist.decreasePosAfterPos(pos);
387 bool Paragraph::Pimpl::erase(pos_type pos)
389 Assert(pos < size());
392 Change::Type changetype(changes_->lookup(pos));
393 changes_->record(Change(Change::DELETED), pos);
395 // only allow the actual removal if it was /new/ text
396 if (changetype != Change::INSERTED) {
397 if (text[pos] == Paragraph::META_INSET) {
398 InsetOld * i(owner_->getInset(pos));
410 int Paragraph::Pimpl::erase(pos_type start, pos_type end)
413 pos_type count = end - start;
424 void Paragraph::Pimpl::simpleTeXBlanks(ostream & os, TexRow & texrow,
426 unsigned int & column,
427 LyXFont const & font,
428 LyXLayout const & style)
433 if (column > lyxrc.ascii_linelen
435 && getChar(i - 1) != ' '
437 // same in FreeSpacing mode
438 && !owner_->isFreeSpacing()
439 // In typewriter mode, we want to avoid
440 // ! . ? : at the end of a line
441 && !(font.family() == LyXFont::TYPEWRITER_FAMILY
442 && (getChar(i - 1) == '.'
443 || getChar(i - 1) == '?'
444 || getChar(i - 1) == ':'
445 || getChar(i - 1) == '!'))) {
448 texrow.start(owner_->id(), i + 1);
450 } else if (style.free_spacing) {
458 bool Paragraph::Pimpl::isTextAt(string const & str, pos_type pos) const
460 pos_type const len = str.length();
462 // is the paragraph large enough?
463 if (pos + len > size())
466 // does the wanted text start at point?
467 for (string::size_type i = 0; i < str.length(); ++i) {
468 if (str[i] != text[pos + i])
472 // is there a font change in middle of the word?
473 FontList::const_iterator cit = fontlist.begin();
474 FontList::const_iterator end = fontlist.end();
475 for (; cit != end; ++cit) {
476 if (cit->pos() >= pos)
479 if (cit != end && pos + len - 1 > cit->pos())
486 void Paragraph::Pimpl::simpleTeXSpecialChars(Buffer const & buf,
487 BufferParams const & bparams,
490 LatexRunParams const & runparams,
492 LyXFont & running_font,
494 LyXFont const & outerfont,
496 Change::Type & running_change,
497 LyXLayout const & style,
499 unsigned int & column,
502 if (style.pass_thru) {
503 if (c != Paragraph::META_INSET) {
507 InsetOld const * inset = owner_->getInset(i);
508 inset->ascii(buf, os, 0);
513 // Two major modes: LaTeX or plain
514 // Handle here those cases common to both modes
515 // and then split to handle the two modes separately.
517 case Paragraph::META_INSET: {
518 InsetOld * inset = owner_->getInset(i);
520 // FIXME: remove this check
524 // FIXME: move this to InsetNewline::latex
525 if (inset->lyxCode() == InsetOld::NEWLINE_CODE) {
526 // newlines are handled differently here than
527 // the default in simpleTeXSpecialChars().
528 if (!style.newline_allowed) {
532 column += running_font.latexWriteEndChanges(os, basefont, basefont);
535 basefont = owner_->getLayoutFont(bparams, outerfont);
536 running_font = basefont;
538 if (font.family() == LyXFont::TYPEWRITER_FAMILY)
541 if (runparams.moving_arg)
547 texrow.start(owner_->id(), i + 1);
552 if (inset->isTextInset()) {
553 column += Changes::latexMarkChange(os, running_change,
555 running_change = Change::UNCHANGED;
559 int const len = os.tellp();
560 //ostream::pos_type const len = os.tellp();
561 if ((inset->lyxCode() == InsetOld::GRAPHICS_CODE
562 || inset->lyxCode() == InsetOld::MATH_CODE
563 || inset->lyxCode() == InsetOld::URL_CODE)
564 && running_font.isRightToLeft()) {
570 #warning Bug: we can have an empty font change here!
571 // if there has just been a font change, we are going to close it
572 // right now, which means stupid latex code like \textsf{}. AFAIK,
573 // this does not harm dvi output. A minor bug, thus (JMarc)
575 // some insets cannot be inside a font change command
576 if (open_font && inset->noFontChange()) {
577 column +=running_font.
578 latexWriteEndChanges(os,
582 basefont = owner_->getLayoutFont(bparams, outerfont);
583 running_font = basefont;
586 int tmp = inset->latex(buf, os, runparams);
592 for (int j = 0; j < tmp; ++j) {
595 texrow.start(owner_->id(), i + 1);
598 column += int(os.tellp()) - len;
604 // And now for the special cases within each mode
608 os << "\\textbackslash{}";
612 case '±': case '²': case '³':
613 case '×': case '÷': case '¹':
615 if ((bparams.inputenc == "latin1" ||
616 bparams.inputenc == "latin9") ||
617 (bparams.inputenc == "auto" &&
618 (font.language()->encoding()->LatexName()
620 font.language()->encoding()->LatexName()
622 os << "\\ensuremath{"
631 case '|': case '<': case '>':
632 // In T1 encoding, these characters exist
633 if (lyxrc.fontenc == "T1") {
635 //... but we should avoid ligatures
636 if ((c == '>' || c == '<')
638 && getChar(i + 1) == c) {
639 //os << "\\textcompwordmark{}";
640 // Jean-Marc, have a look at
641 // this. I think this works
649 // Typewriter font also has them
650 if (font.family() == LyXFont::TYPEWRITER_FAMILY) {
654 // Otherwise, we use what LaTeX
658 os << "\\textless{}";
662 os << "\\textgreater{}";
672 case '-': // "--" in Typewriter mode -> "-{}-"
674 && getChar(i + 1) == '-'
675 && font.family() == LyXFont::TYPEWRITER_FAMILY) {
684 os << "\\char`\\\"{}";
689 if (bparams.inputenc == "default") {
698 case '%': case '#': case '{':
705 os << "\\textasciitilde{}";
710 os << "\\textasciicircum{}";
714 case '*': case '[': case ']':
715 // avoid being mistaken for optional arguments
716 os << '{' << c << '}';
721 // Blanks are printed before font switching.
722 // Sure? I am not! (try nice-latex)
723 // I am sure it's correct. LyX might be smarter
724 // in the future, but for now, nothing wrong is
730 // I assume this is hack treating typewriter as verbatim
731 if (font.family() == LyXFont::TYPEWRITER_FAMILY) {
740 // FIXME: if we have "LaTeX" with a font
741 // change in the middle (before the 'T', then
742 // the "TeX" part is still special cased.
743 // Really we should only operate this on
744 // "words" for some definition of word
748 for (; pnr < phrases_nr; ++pnr) {
749 if (isTextAt(special_phrases[pnr].phrase, i)) {
750 os << special_phrases[pnr].macro;
751 i += special_phrases[pnr].phrase.length() - 1;
752 column += special_phrases[pnr].macro.length() - 1;
757 if (pnr == phrases_nr && c != '\0') {
766 void Paragraph::Pimpl::validate(LaTeXFeatures & features,
767 LyXLayout const & layout) const
769 BufferParams const & bparams = features.bufferParams();
772 if (params.lineTop() || params.lineBottom())
773 features.require("lyxline");
774 if (!params.spacing().isDefault())
775 features.require("setspace");
778 features.useLayout(layout.name());
781 Language const * doc_language = bparams.language;
783 FontList::const_iterator fcit = fontlist.begin();
784 FontList::const_iterator fend = fontlist.end();
785 for (; fcit != fend; ++fcit) {
786 if (fcit->font().noun() == LyXFont::ON) {
787 lyxerr[Debug::LATEX] << "font.noun: "
788 << fcit->font().noun()
790 features.require("noun");
791 lyxerr[Debug::LATEX] << "Noun enabled. Font: "
792 << fcit->font().stateText(0)
795 switch (fcit->font().color()) {
797 case LColor::inherit:
799 // probably we should put here all interface colors used for
800 // font displaying! For now I just add this ones I know of (Jug)
805 features.require("color");
806 lyxerr[Debug::LATEX] << "Color enabled. Font: "
807 << fcit->font().stateText(0)
811 Language const * language = fcit->font().language();
812 if (language->babel() != doc_language->babel() &&
813 language != ignore_language &&
814 language != latex_language)
816 features.useLanguage(language);
817 lyxerr[Debug::LATEX] << "Found language "
818 << language->babel() << endl;
822 if (!params.leftIndent().zero())
823 features.require("ParagraphLeftIndent");
826 InsetList::iterator icit = owner_->insetlist.begin();
827 InsetList::iterator iend = owner_->insetlist.end();
828 for (; icit != iend; ++icit) {
830 icit->inset->validate(features);
831 if (layout.needprotect &&
832 icit->inset->lyxCode() == InsetOld::FOOT_CODE)
833 features.require("NeedLyXFootnoteCode");
838 for (pos_type i = 0; i < size() ; ++i) {
839 for (size_t pnr = 0; pnr < phrases_nr; ++pnr) {
840 if (!special_phrases[pnr].builtin
841 && isTextAt(special_phrases[pnr].phrase, i)) {
842 features.require(special_phrases[pnr].phrase);