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"
25 #include "lyxlength.h"
33 using std::upper_bound;
34 using std::lower_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;
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());
107 void Paragraph::Pimpl::untrackChanges()
113 void Paragraph::Pimpl::cleanChanges()
115 // if we're not tracking, we don't want to reset...
119 changes_.reset(new Changes(Change::INSERTED));
120 changes_->set(Change::INSERTED, 0, size());
124 bool Paragraph::Pimpl::isChanged(pos_type start, pos_type end) const
129 return changes_->isChange(start, end);
133 bool Paragraph::Pimpl::isChangeEdited(pos_type start, pos_type end) const
138 return changes_->isChangeEdited(start, end);
142 void Paragraph::Pimpl::setChange(pos_type pos, Change::Type type)
147 changes_->set(type, pos);
151 Change::Type Paragraph::Pimpl::lookupChange(pos_type pos) const
154 return Change::UNCHANGED;
156 return changes_->lookup(pos);
160 Change const Paragraph::Pimpl::lookupChangeFull(pos_type pos) const
163 return Change(Change::UNCHANGED);
165 return changes_->lookupFull(pos);
169 void Paragraph::Pimpl::markErased()
171 BOOST_ASSERT(tracking());
173 // FIXME: we should actually remove INSERTED chars.
174 // difficult because owning insettexts/tabulars need
175 // to update themselves when rows etc. change
176 changes_->set(Change::DELETED, 0, size());
177 changes_->reset(Change::DELETED);
181 void Paragraph::Pimpl::acceptChange(pos_type start, pos_type end)
187 changes_.reset(new Changes(Change::UNCHANGED));
191 lyxerr << "acceptchange" << endl;
194 for (; i < end; ++i) {
195 switch (lookupChange(i)) {
196 case Change::UNCHANGED:
199 case Change::INSERTED:
200 changes_->set(Change::UNCHANGED, i);
203 case Change::DELETED:
212 lyxerr << "endacceptchange" << endl;
213 changes_->reset(Change::UNCHANGED);
217 void Paragraph::Pimpl::rejectChange(pos_type start, pos_type end)
223 changes_.reset(new Changes(Change::UNCHANGED));
229 for (; i < end; ++i) {
230 switch (lookupChange(i)) {
231 case Change::UNCHANGED:
234 case Change::INSERTED:
241 case Change::DELETED:
242 changes_->set(Change::UNCHANGED, i);
246 changes_->reset(Change::UNCHANGED);
250 Paragraph::value_type Paragraph::Pimpl::getChar(pos_type pos) const
252 return owner_->getChar(pos);
256 void Paragraph::Pimpl::insertChar(pos_type pos, value_type c,
257 LyXFont const & font, Change change)
259 BOOST_ASSERT(pos <= size());
262 changes_->record(change, pos);
265 // This is actually very common when parsing buffers (and
266 // maybe inserting ascii text)
268 // when appending characters, no need to update tables
269 owner_->text_.push_back(c);
270 owner_->setFont(pos, font);
274 owner_->text_.insert(owner_->text_.begin() + pos, c);
276 // Update the font table.
277 FontTable search_font(pos, LyXFont());
278 for (FontList::iterator it = lower_bound(fontlist.begin(),
280 search_font, matchFT());
281 it != fontlist.end(); ++it)
283 it->pos(it->pos() + 1);
287 owner_->insetlist.increasePosAfterPos(pos);
289 owner_->setFont(pos, font);
293 void Paragraph::Pimpl::insertInset(pos_type pos,
294 InsetOld * inset, LyXFont const & font, Change change)
297 BOOST_ASSERT(pos <= size());
299 insertChar(pos, META_INSET, font, change);
300 BOOST_ASSERT(owner_->text_[pos] == META_INSET);
302 // Add a new entry in the insetlist.
303 owner_->insetlist.insert(inset, pos);
306 inset->setOwner(inset_owner);
310 void Paragraph::Pimpl::eraseIntern(pos_type pos)
312 // if it is an inset, delete the inset entry
313 if (owner_->text_[pos] == Paragraph::META_INSET) {
314 owner_->insetlist.erase(pos);
317 owner_->text_.erase(owner_->text_.begin() + pos);
319 // Erase entries in the tables.
320 FontTable search_font(pos, LyXFont());
322 FontList::iterator it =
323 lower_bound(fontlist.begin(),
325 search_font, matchFT());
326 if (it != fontlist.end() && it->pos() == pos &&
328 (it != fontlist.begin()
329 && boost::prior(it)->pos() == pos - 1))) {
330 // If it is a multi-character font
331 // entry, we just make it smaller
332 // (see update below), otherwise we
334 unsigned int const i = it - fontlist.begin();
335 fontlist.erase(fontlist.begin() + i);
336 it = fontlist.begin() + i;
337 if (i > 0 && i < fontlist.size() &&
338 fontlist[i - 1].font() == fontlist[i].font()) {
339 fontlist.erase(fontlist.begin() + i - 1);
340 it = fontlist.begin() + i - 1;
344 // Update all other entries.
345 FontList::iterator fend = fontlist.end();
346 for (; it != fend; ++it)
347 it->pos(it->pos() - 1);
349 // Update the insetlist.
350 owner_->insetlist.decreasePosAfterPos(pos);
354 bool Paragraph::Pimpl::erase(pos_type pos)
356 BOOST_ASSERT(pos < size());
359 Change::Type changetype(changes_->lookup(pos));
360 changes_->record(Change(Change::DELETED), pos);
362 // only allow the actual removal if it was /new/ text
363 if (changetype != Change::INSERTED) {
364 if (owner_->text_[pos] == Paragraph::META_INSET) {
365 InsetOld * i(owner_->getInset(pos));
377 int Paragraph::Pimpl::erase(pos_type start, pos_type end)
380 pos_type count = end - start;
391 void Paragraph::Pimpl::simpleTeXBlanks(ostream & os, TexRow & texrow,
393 unsigned int & column,
394 LyXFont const & font,
395 LyXLayout const & style)
400 if (column > lyxrc.ascii_linelen
402 && getChar(i - 1) != ' '
404 // same in FreeSpacing mode
405 && !owner_->isFreeSpacing()
406 // In typewriter mode, we want to avoid
407 // ! . ? : at the end of a line
408 && !(font.family() == LyXFont::TYPEWRITER_FAMILY
409 && (getChar(i - 1) == '.'
410 || getChar(i - 1) == '?'
411 || getChar(i - 1) == ':'
412 || getChar(i - 1) == '!'))) {
415 texrow.start(owner_->id(), i + 1);
417 } else if (style.free_spacing) {
425 bool Paragraph::Pimpl::isTextAt(string const & str, pos_type pos) const
427 pos_type const len = str.length();
429 // is the paragraph large enough?
430 if (pos + len > size())
433 // does the wanted text start at point?
434 for (string::size_type i = 0; i < str.length(); ++i) {
435 if (str[i] != owner_->text_[pos + i])
439 // is there a font change in middle of the word?
440 FontList::const_iterator cit = fontlist.begin();
441 FontList::const_iterator end = fontlist.end();
442 for (; cit != end; ++cit) {
443 if (cit->pos() >= pos)
446 if (cit != end && pos + len - 1 > cit->pos())
453 void Paragraph::Pimpl::simpleTeXSpecialChars(Buffer const & buf,
454 BufferParams const & bparams,
457 LatexRunParams const & runparams,
459 LyXFont & running_font,
461 LyXFont const & outerfont,
463 Change::Type & running_change,
464 LyXLayout const & style,
466 unsigned int & column,
469 if (style.pass_thru) {
470 if (c != Paragraph::META_INSET) {
474 InsetOld const * inset = owner_->getInset(i);
475 inset->ascii(buf, os, 0);
480 // Two major modes: LaTeX or plain
481 // Handle here those cases common to both modes
482 // and then split to handle the two modes separately.
484 case Paragraph::META_INSET: {
485 InsetOld * inset = owner_->getInset(i);
487 // FIXME: remove this check
491 // FIXME: move this to InsetNewline::latex
492 if (inset->lyxCode() == InsetOld::NEWLINE_CODE) {
493 // newlines are handled differently here than
494 // the default in simpleTeXSpecialChars().
495 if (!style.newline_allowed) {
499 column += running_font.latexWriteEndChanges(os, basefont, basefont);
502 basefont = owner_->getLayoutFont(bparams, outerfont);
503 running_font = basefont;
505 if (font.family() == LyXFont::TYPEWRITER_FAMILY)
508 if (runparams.moving_arg)
514 texrow.start(owner_->id(), i + 1);
519 if (inset->isTextInset()) {
520 column += Changes::latexMarkChange(os, running_change,
522 running_change = Change::UNCHANGED;
526 ostream::pos_type const len = os.tellp();
528 if ((inset->lyxCode() == InsetOld::GRAPHICS_CODE
529 || inset->lyxCode() == InsetOld::MATH_CODE
530 || inset->lyxCode() == InsetOld::URL_CODE)
531 && running_font.isRightToLeft()) {
537 #warning Bug: we can have an empty font change here!
538 // if there has just been a font change, we are going to close it
539 // right now, which means stupid latex code like \textsf{}. AFAIK,
540 // this does not harm dvi output. A minor bug, thus (JMarc)
542 // some insets cannot be inside a font change command
543 if (open_font && inset->noFontChange()) {
544 column +=running_font.
545 latexWriteEndChanges(os,
549 basefont = owner_->getLayoutFont(bparams, outerfont);
550 running_font = basefont;
553 int tmp = inset->latex(buf, os, runparams);
559 for (int j = 0; j < tmp; ++j) {
562 texrow.start(owner_->id(), i + 1);
565 column += os.tellp() - len;
571 // And now for the special cases within each mode
575 os << "\\textbackslash{}";
579 case '±': case '²': case '³':
580 case '×': case '÷': case '¹':
582 if ((bparams.inputenc == "latin1" ||
583 bparams.inputenc == "latin9") ||
584 (bparams.inputenc == "auto" &&
585 (font.language()->encoding()->LatexName()
587 font.language()->encoding()->LatexName()
589 os << "\\ensuremath{"
598 case '|': case '<': case '>':
599 // In T1 encoding, these characters exist
600 if (lyxrc.fontenc == "T1") {
602 //... but we should avoid ligatures
603 if ((c == '>' || c == '<')
605 && getChar(i + 1) == c) {
606 //os << "\\textcompwordmark{}";
607 // Jean-Marc, have a look at
608 // this. I think this works
616 // Typewriter font also has them
617 if (font.family() == LyXFont::TYPEWRITER_FAMILY) {
621 // Otherwise, we use what LaTeX
625 os << "\\textless{}";
629 os << "\\textgreater{}";
639 case '-': // "--" in Typewriter mode -> "-{}-"
641 && getChar(i + 1) == '-'
642 && font.family() == LyXFont::TYPEWRITER_FAMILY) {
651 os << "\\char`\\\"{}";
656 if (bparams.inputenc == "default") {
665 case '%': case '#': case '{':
672 os << "\\textasciitilde{}";
677 os << "\\textasciicircum{}";
681 case '*': case '[': case ']':
682 // avoid being mistaken for optional arguments
683 os << '{' << c << '}';
688 // Blanks are printed before font switching.
689 // Sure? I am not! (try nice-latex)
690 // I am sure it's correct. LyX might be smarter
691 // in the future, but for now, nothing wrong is
697 // I assume this is hack treating typewriter as verbatim
698 if (font.family() == LyXFont::TYPEWRITER_FAMILY) {
707 // FIXME: if we have "LaTeX" with a font
708 // change in the middle (before the 'T', then
709 // the "TeX" part is still special cased.
710 // Really we should only operate this on
711 // "words" for some definition of word
715 for (; pnr < phrases_nr; ++pnr) {
716 if (isTextAt(special_phrases[pnr].phrase, i)) {
717 os << special_phrases[pnr].macro;
718 i += special_phrases[pnr].phrase.length() - 1;
719 column += special_phrases[pnr].macro.length() - 1;
724 if (pnr == phrases_nr && c != '\0') {
733 void Paragraph::Pimpl::validate(LaTeXFeatures & features,
734 LyXLayout const & layout) const
736 BufferParams const & bparams = features.bufferParams();
739 if (params.lineTop() || params.lineBottom())
740 features.require("lyxline");
741 if (!params.spacing().isDefault())
742 features.require("setspace");
745 features.useLayout(layout.name());
748 Language const * doc_language = bparams.language;
750 FontList::const_iterator fcit = fontlist.begin();
751 FontList::const_iterator fend = fontlist.end();
752 for (; fcit != fend; ++fcit) {
753 if (fcit->font().noun() == LyXFont::ON) {
754 lyxerr[Debug::LATEX] << "font.noun: "
755 << fcit->font().noun()
757 features.require("noun");
758 lyxerr[Debug::LATEX] << "Noun enabled. Font: "
759 << fcit->font().stateText(0)
762 switch (fcit->font().color()) {
764 case LColor::inherit:
766 // probably we should put here all interface colors used for
767 // font displaying! For now I just add this ones I know of (Jug)
772 features.require("color");
773 lyxerr[Debug::LATEX] << "Color enabled. Font: "
774 << fcit->font().stateText(0)
778 Language const * language = fcit->font().language();
779 if (language->babel() != doc_language->babel() &&
780 language != ignore_language &&
781 language != latex_language)
783 features.useLanguage(language);
784 lyxerr[Debug::LATEX] << "Found language "
785 << language->babel() << endl;
789 if (!params.leftIndent().zero())
790 features.require("ParagraphLeftIndent");
793 InsetList::iterator icit = owner_->insetlist.begin();
794 InsetList::iterator iend = owner_->insetlist.end();
795 for (; icit != iend; ++icit) {
797 icit->inset->validate(features);
798 if (layout.needprotect &&
799 icit->inset->lyxCode() == InsetOld::FOOT_CODE)
800 features.require("NeedLyXFootnoteCode");
805 for (pos_type i = 0; i < size() ; ++i) {
806 for (size_t pnr = 0; pnr < phrases_nr; ++pnr) {
807 if (!special_phrases[pnr].builtin
808 && isTextAt(special_phrases[pnr].phrase, i)) {
809 features.require(special_phrases[pnr].phrase);