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;
38 // Initialize static member.
39 ShareContainer<LyXFont> Paragraph::Pimpl::FontTable::container;
40 // Initialization of the counter for the paragraph id's,
41 unsigned int Paragraph::Pimpl::paragraph_id = 0;
45 struct special_phrase {
51 special_phrase special_phrases[] = {
52 { "LyX", "\\LyX{}", false },
53 { "TeX", "\\TeX{}", true },
54 { "LaTeX2e", "\\LaTeXe{}", true },
55 { "LaTeX", "\\LaTeX{}", true },
58 size_t const phrases_nr = sizeof(special_phrases)/sizeof(special_phrase);
63 Paragraph::Pimpl::Pimpl(Paragraph * owner)
71 Paragraph::Pimpl::Pimpl(Pimpl const & p, Paragraph * owner)
72 : params(p.params), owner_(owner)
74 inset_owner = p.inset_owner;
75 fontlist = p.fontlist;
79 changes_.reset(new Changes(*p.changes_.get()));
83 void Paragraph::Pimpl::setContentsFromPar(Paragraph const & par)
85 owner_->text_ = par.text_;
86 if (par.pimpl_->tracking()) {
87 changes_.reset(new Changes(*(par.pimpl_->changes_.get())));
92 void Paragraph::Pimpl::trackChanges(Change::Type type)
95 lyxerr[Debug::CHANGES] << "already tracking for par " << id_ << endl;
99 lyxerr[Debug::CHANGES] << "track changes for par "
100 << id_ << " type " << type << endl;
101 changes_.reset(new Changes(type));
102 changes_->set(type, 0, size());
106 void Paragraph::Pimpl::untrackChanges()
112 void Paragraph::Pimpl::cleanChanges()
114 // if we're not tracking, we don't want to reset...
118 changes_.reset(new Changes(Change::INSERTED));
119 changes_->set(Change::INSERTED, 0, size());
123 bool Paragraph::Pimpl::isChanged(pos_type start, pos_type end) const
128 return changes_->isChange(start, end);
132 bool Paragraph::Pimpl::isChangeEdited(pos_type start, pos_type end) const
137 return changes_->isChangeEdited(start, end);
141 void Paragraph::Pimpl::setChange(pos_type pos, Change::Type type)
146 changes_->set(type, pos);
150 Change::Type Paragraph::Pimpl::lookupChange(pos_type pos) const
153 return Change::UNCHANGED;
155 return changes_->lookup(pos);
159 Change const Paragraph::Pimpl::lookupChangeFull(pos_type pos) const
162 return Change(Change::UNCHANGED);
164 return changes_->lookupFull(pos);
168 void Paragraph::Pimpl::markErased()
170 BOOST_ASSERT(tracking());
172 // FIXME: we should actually remove INSERTED chars.
173 // difficult because owning insettexts/tabulars need
174 // to update themselves when rows etc. change
175 changes_->set(Change::DELETED, 0, size());
176 changes_->reset(Change::DELETED);
180 void Paragraph::Pimpl::acceptChange(pos_type start, pos_type end)
186 changes_.reset(new Changes(Change::UNCHANGED));
190 lyxerr << "acceptchange" << endl;
193 for (; i < end; ++i) {
194 switch (lookupChange(i)) {
195 case Change::UNCHANGED:
198 case Change::INSERTED:
199 changes_->set(Change::UNCHANGED, i);
202 case Change::DELETED:
211 lyxerr << "endacceptchange" << endl;
212 changes_->reset(Change::UNCHANGED);
216 void Paragraph::Pimpl::rejectChange(pos_type start, pos_type end)
222 changes_.reset(new Changes(Change::UNCHANGED));
228 for (; i < end; ++i) {
229 switch (lookupChange(i)) {
230 case Change::UNCHANGED:
233 case Change::INSERTED:
240 case Change::DELETED:
241 changes_->set(Change::UNCHANGED, i);
245 changes_->reset(Change::UNCHANGED);
249 Paragraph::value_type Paragraph::Pimpl::getChar(pos_type pos) const
251 return owner_->getChar(pos);
255 void Paragraph::Pimpl::insertChar(pos_type pos, value_type c,
256 LyXFont const & font, Change change)
258 BOOST_ASSERT(pos <= size());
261 changes_->record(change, pos);
264 // This is actually very common when parsing buffers (and
265 // maybe inserting ascii text)
267 // when appending characters, no need to update tables
268 owner_->text_.push_back(c);
269 owner_->setFont(pos, font);
273 owner_->text_.insert(owner_->text_.begin() + pos, c);
275 // Update the font table.
276 FontTable search_font(pos, LyXFont());
277 for (FontList::iterator it = lower_bound(fontlist.begin(),
279 search_font, matchFT());
280 it != fontlist.end(); ++it)
282 it->pos(it->pos() + 1);
286 owner_->insetlist.increasePosAfterPos(pos);
288 owner_->setFont(pos, font);
292 void Paragraph::Pimpl::insertInset(pos_type pos,
293 InsetOld * inset, LyXFont const & font, Change change)
296 BOOST_ASSERT(pos <= size());
298 insertChar(pos, META_INSET, font, change);
299 BOOST_ASSERT(owner_->text_[pos] == META_INSET);
301 // Add a new entry in the insetlist.
302 owner_->insetlist.insert(inset, pos);
305 inset->setOwner(inset_owner);
309 void Paragraph::Pimpl::eraseIntern(pos_type pos)
311 // if it is an inset, delete the inset entry
312 if (owner_->text_[pos] == Paragraph::META_INSET) {
313 owner_->insetlist.erase(pos);
316 owner_->text_.erase(owner_->text_.begin() + pos);
318 // Erase entries in the tables.
319 FontTable search_font(pos, LyXFont());
321 FontList::iterator it =
322 lower_bound(fontlist.begin(),
324 search_font, matchFT());
325 if (it != fontlist.end() && it->pos() == pos &&
327 (it != fontlist.begin()
328 && boost::prior(it)->pos() == pos - 1))) {
329 // If it is a multi-character font
330 // entry, we just make it smaller
331 // (see update below), otherwise we
333 unsigned int const i = it - fontlist.begin();
334 fontlist.erase(fontlist.begin() + i);
335 it = fontlist.begin() + i;
336 if (i > 0 && i < fontlist.size() &&
337 fontlist[i - 1].font() == fontlist[i].font()) {
338 fontlist.erase(fontlist.begin() + i - 1);
339 it = fontlist.begin() + i - 1;
343 // Update all other entries.
344 FontList::iterator fend = fontlist.end();
345 for (; it != fend; ++it)
346 it->pos(it->pos() - 1);
348 // Update the insetlist.
349 owner_->insetlist.decreasePosAfterPos(pos);
353 bool Paragraph::Pimpl::erase(pos_type pos)
355 BOOST_ASSERT(pos < size());
358 Change::Type changetype(changes_->lookup(pos));
359 changes_->record(Change(Change::DELETED), pos);
361 // only allow the actual removal if it was /new/ text
362 if (changetype != Change::INSERTED) {
363 if (owner_->text_[pos] == Paragraph::META_INSET) {
364 InsetOld * i(owner_->getInset(pos));
376 int Paragraph::Pimpl::erase(pos_type start, pos_type end)
379 pos_type count = end - start;
390 void Paragraph::Pimpl::simpleTeXBlanks(ostream & os, TexRow & texrow,
392 unsigned int & column,
393 LyXFont const & font,
394 LyXLayout const & style)
399 if (column > lyxrc.ascii_linelen
401 && getChar(i - 1) != ' '
403 // same in FreeSpacing mode
404 && !owner_->isFreeSpacing()
405 // In typewriter mode, we want to avoid
406 // ! . ? : at the end of a line
407 && !(font.family() == LyXFont::TYPEWRITER_FAMILY
408 && (getChar(i - 1) == '.'
409 || getChar(i - 1) == '?'
410 || getChar(i - 1) == ':'
411 || getChar(i - 1) == '!'))) {
414 texrow.start(owner_->id(), i + 1);
416 } else if (style.free_spacing) {
424 bool Paragraph::Pimpl::isTextAt(string const & str, pos_type pos) const
426 pos_type const len = str.length();
428 // is the paragraph large enough?
429 if (pos + len > size())
432 // does the wanted text start at point?
433 for (string::size_type i = 0; i < str.length(); ++i) {
434 if (str[i] != owner_->text_[pos + i])
438 // is there a font change in middle of the word?
439 FontList::const_iterator cit = fontlist.begin();
440 FontList::const_iterator end = fontlist.end();
441 for (; cit != end; ++cit) {
442 if (cit->pos() >= pos)
445 if (cit != end && pos + len - 1 > cit->pos())
452 void Paragraph::Pimpl::simpleTeXSpecialChars(Buffer const & buf,
453 BufferParams const & bparams,
456 LatexRunParams const & runparams,
458 LyXFont & running_font,
460 LyXFont const & outerfont,
462 Change::Type & running_change,
463 LyXLayout const & style,
465 unsigned int & column,
468 if (style.pass_thru) {
469 if (c != Paragraph::META_INSET) {
473 InsetOld const * inset = owner_->getInset(i);
474 inset->ascii(buf, os, 0);
479 // Two major modes: LaTeX or plain
480 // Handle here those cases common to both modes
481 // and then split to handle the two modes separately.
483 case Paragraph::META_INSET: {
484 InsetOld * inset = owner_->getInset(i);
486 // FIXME: remove this check
490 // FIXME: move this to InsetNewline::latex
491 if (inset->lyxCode() == InsetOld::NEWLINE_CODE) {
492 // newlines are handled differently here than
493 // the default in simpleTeXSpecialChars().
494 if (!style.newline_allowed) {
498 column += running_font.latexWriteEndChanges(os, basefont, basefont);
501 basefont = owner_->getLayoutFont(bparams, outerfont);
502 running_font = basefont;
504 if (font.family() == LyXFont::TYPEWRITER_FAMILY)
507 if (runparams.moving_arg)
513 texrow.start(owner_->id(), i + 1);
518 if (inset->isTextInset()) {
519 column += Changes::latexMarkChange(os, running_change,
521 running_change = Change::UNCHANGED;
525 int const len = os.tellp();
526 //ostream::pos_type const len = os.tellp();
527 if ((inset->lyxCode() == InsetOld::GRAPHICS_CODE
528 || inset->lyxCode() == InsetOld::MATH_CODE
529 || inset->lyxCode() == InsetOld::URL_CODE)
530 && running_font.isRightToLeft()) {
536 #warning Bug: we can have an empty font change here!
537 // if there has just been a font change, we are going to close it
538 // right now, which means stupid latex code like \textsf{}. AFAIK,
539 // this does not harm dvi output. A minor bug, thus (JMarc)
541 // some insets cannot be inside a font change command
542 if (open_font && inset->noFontChange()) {
543 column +=running_font.
544 latexWriteEndChanges(os,
548 basefont = owner_->getLayoutFont(bparams, outerfont);
549 running_font = basefont;
552 int tmp = inset->latex(buf, os, runparams);
558 for (int j = 0; j < tmp; ++j) {
561 texrow.start(owner_->id(), i + 1);
564 column += int(os.tellp()) - len;
570 // And now for the special cases within each mode
574 os << "\\textbackslash{}";
578 case '±': case '²': case '³':
579 case '×': case '÷': case '¹':
581 if ((bparams.inputenc == "latin1" ||
582 bparams.inputenc == "latin9") ||
583 (bparams.inputenc == "auto" &&
584 (font.language()->encoding()->LatexName()
586 font.language()->encoding()->LatexName()
588 os << "\\ensuremath{"
597 case '|': case '<': case '>':
598 // In T1 encoding, these characters exist
599 if (lyxrc.fontenc == "T1") {
601 //... but we should avoid ligatures
602 if ((c == '>' || c == '<')
604 && getChar(i + 1) == c) {
605 //os << "\\textcompwordmark{}";
606 // Jean-Marc, have a look at
607 // this. I think this works
615 // Typewriter font also has them
616 if (font.family() == LyXFont::TYPEWRITER_FAMILY) {
620 // Otherwise, we use what LaTeX
624 os << "\\textless{}";
628 os << "\\textgreater{}";
638 case '-': // "--" in Typewriter mode -> "-{}-"
640 && getChar(i + 1) == '-'
641 && font.family() == LyXFont::TYPEWRITER_FAMILY) {
650 os << "\\char`\\\"{}";
655 if (bparams.inputenc == "default") {
664 case '%': case '#': case '{':
671 os << "\\textasciitilde{}";
676 os << "\\textasciicircum{}";
680 case '*': case '[': case ']':
681 // avoid being mistaken for optional arguments
682 os << '{' << c << '}';
687 // Blanks are printed before font switching.
688 // Sure? I am not! (try nice-latex)
689 // I am sure it's correct. LyX might be smarter
690 // in the future, but for now, nothing wrong is
696 // I assume this is hack treating typewriter as verbatim
697 if (font.family() == LyXFont::TYPEWRITER_FAMILY) {
706 // FIXME: if we have "LaTeX" with a font
707 // change in the middle (before the 'T', then
708 // the "TeX" part is still special cased.
709 // Really we should only operate this on
710 // "words" for some definition of word
714 for (; pnr < phrases_nr; ++pnr) {
715 if (isTextAt(special_phrases[pnr].phrase, i)) {
716 os << special_phrases[pnr].macro;
717 i += special_phrases[pnr].phrase.length() - 1;
718 column += special_phrases[pnr].macro.length() - 1;
723 if (pnr == phrases_nr && c != '\0') {
732 void Paragraph::Pimpl::validate(LaTeXFeatures & features,
733 LyXLayout const & layout) const
735 BufferParams const & bparams = features.bufferParams();
738 if (params.lineTop() || params.lineBottom())
739 features.require("lyxline");
740 if (!params.spacing().isDefault())
741 features.require("setspace");
744 features.useLayout(layout.name());
747 Language const * doc_language = bparams.language;
749 FontList::const_iterator fcit = fontlist.begin();
750 FontList::const_iterator fend = fontlist.end();
751 for (; fcit != fend; ++fcit) {
752 if (fcit->font().noun() == LyXFont::ON) {
753 lyxerr[Debug::LATEX] << "font.noun: "
754 << fcit->font().noun()
756 features.require("noun");
757 lyxerr[Debug::LATEX] << "Noun enabled. Font: "
758 << fcit->font().stateText(0)
761 switch (fcit->font().color()) {
763 case LColor::inherit:
765 // probably we should put here all interface colors used for
766 // font displaying! For now I just add this ones I know of (Jug)
771 features.require("color");
772 lyxerr[Debug::LATEX] << "Color enabled. Font: "
773 << fcit->font().stateText(0)
777 Language const * language = fcit->font().language();
778 if (language->babel() != doc_language->babel() &&
779 language != ignore_language &&
780 language != latex_language)
782 features.useLanguage(language);
783 lyxerr[Debug::LATEX] << "Found language "
784 << language->babel() << endl;
788 if (!params.leftIndent().zero())
789 features.require("ParagraphLeftIndent");
792 InsetList::iterator icit = owner_->insetlist.begin();
793 InsetList::iterator iend = owner_->insetlist.end();
794 for (; icit != iend; ++icit) {
796 icit->inset->validate(features);
797 if (layout.needprotect &&
798 icit->inset->lyxCode() == InsetOld::FOOT_CODE)
799 features.require("NeedLyXFootnoteCode");
804 for (pos_type i = 0; i < size() ; ++i) {
805 for (size_t pnr = 0; pnr < phrases_nr; ++pnr) {
806 if (!special_phrases[pnr].builtin
807 && isTextAt(special_phrases[pnr].phrase, i)) {
808 features.require(special_phrases[pnr].phrase);