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::setChangeType(pos_type pos, Change::Type type)
155 changes_->set(type, pos);
159 void Paragraph::Pimpl::setChange(pos_type pos, Change change)
164 changes_->set(change, pos);
168 Change const Paragraph::Pimpl::lookupChange(pos_type pos) const
171 return Change(Change::UNCHANGED);
173 return changes_->lookup(pos);
177 void Paragraph::Pimpl::markErased(bool erased)
179 BOOST_ASSERT(tracking());
184 for (pos_type i = 0; i < size(); ++i) {
185 changes_->set(Change::UNCHANGED, i);
186 if (owner_->isInset(i))
187 owner_->getInset(i)->markErased(false);
193 void Paragraph::Pimpl::acceptChange(pos_type start, pos_type end)
199 changes_.reset(new Changes(Change::UNCHANGED));
203 lyxerr[Debug::CHANGES] << "acceptchange" << endl;
206 for (; i < end; ++i) {
207 switch (lookupChange(i).type) {
208 case Change::UNCHANGED:
211 case Change::INSERTED:
212 changes_->set(Change::UNCHANGED, i);
215 case Change::DELETED:
216 // Suppress access to nonexistent
217 // "end-of-paragraph char":
228 lyxerr[Debug::CHANGES] << "endacceptchange" << endl;
229 changes_->reset(Change::UNCHANGED);
233 void Paragraph::Pimpl::rejectChange(pos_type start, pos_type end)
239 changes_.reset(new Changes(Change::UNCHANGED));
245 for (; i < end; ++i) {
246 switch (lookupChange(i).type) {
247 case Change::UNCHANGED:
250 case Change::INSERTED:
259 case Change::DELETED:
260 changes_->set(Change::UNCHANGED, i);
261 // No real char at position size():
262 if (i < size() && owner_->isInset(i))
263 owner_->getInset(i)->markErased(false);
267 changes_->reset(Change::UNCHANGED);
271 Paragraph::value_type Paragraph::Pimpl::getChar(pos_type pos) const
273 return owner_->getChar(pos);
277 void Paragraph::Pimpl::insertChar(pos_type pos, value_type c, Change change)
279 BOOST_ASSERT(pos <= size());
282 changes_->record(change, pos);
285 // This is actually very common when parsing buffers (and
286 // maybe inserting ascii text)
288 // when appending characters, no need to update tables
289 owner_->text_.push_back(c);
293 owner_->text_.insert(owner_->text_.begin() + pos, c);
295 // Update the font table.
296 FontTable search_font(pos, LyXFont());
297 for (FontList::iterator it = lower_bound(fontlist.begin(),
299 search_font, matchFT());
300 it != fontlist.end(); ++it)
302 it->pos(it->pos() + 1);
306 owner_->insetlist.increasePosAfterPos(pos);
310 void Paragraph::Pimpl::insertInset(pos_type pos,
311 InsetBase * inset, Change change)
314 BOOST_ASSERT(pos <= size());
316 insertChar(pos, META_INSET, change);
317 BOOST_ASSERT(owner_->text_[pos] == META_INSET);
319 // Add a new entry in the insetlist.
320 owner_->insetlist.insert(inset, pos);
324 void Paragraph::Pimpl::eraseIntern(pos_type pos)
326 // if it is an inset, delete the inset entry
327 if (owner_->text_[pos] == Paragraph::META_INSET) {
328 owner_->insetlist.erase(pos);
331 owner_->text_.erase(owner_->text_.begin() + pos);
333 // Erase entries in the tables.
334 FontTable search_font(pos, LyXFont());
336 FontList::iterator it =
337 lower_bound(fontlist.begin(),
339 search_font, matchFT());
340 if (it != fontlist.end() && it->pos() == pos &&
342 (it != fontlist.begin()
343 && boost::prior(it)->pos() == pos - 1))) {
344 // If it is a multi-character font
345 // entry, we just make it smaller
346 // (see update below), otherwise we
348 unsigned int const i = it - fontlist.begin();
349 fontlist.erase(fontlist.begin() + i);
350 it = fontlist.begin() + i;
351 if (i > 0 && i < fontlist.size() &&
352 fontlist[i - 1].font() == fontlist[i].font()) {
353 fontlist.erase(fontlist.begin() + i - 1);
354 it = fontlist.begin() + i - 1;
358 // Update all other entries.
359 FontList::iterator fend = fontlist.end();
360 for (; it != fend; ++it)
361 it->pos(it->pos() - 1);
363 // Update the insetlist.
364 owner_->insetlist.decreasePosAfterPos(pos);
368 bool Paragraph::Pimpl::erase(pos_type pos)
370 BOOST_ASSERT(pos <= size());
373 Change::Type changetype(changes_->lookup(pos).type);
375 // only allow the actual removal if it was /new/ text
376 if (changetype != Change::INSERTED) {
377 changes_->record(Change(Change::DELETED), pos);
378 if (pos < size() && owner_->isInset(pos))
379 owner_->getInset(pos)->markErased(true);
384 // Don't physically access nonexistent end-of-paragraph char
394 int Paragraph::Pimpl::erase(pos_type start, pos_type end)
397 for (pos_type count = end - start; count; --count) {
405 void Paragraph::Pimpl::simpleTeXBlanks(ostream & os, TexRow & texrow,
407 unsigned int & column,
408 LyXFont const & font,
409 LyXLayout const & style)
414 if (column > lyxrc.ascii_linelen
416 && getChar(i - 1) != ' '
418 // same in FreeSpacing mode
419 && !owner_->isFreeSpacing()
420 // In typewriter mode, we want to avoid
421 // ! . ? : at the end of a line
422 && !(font.family() == LyXFont::TYPEWRITER_FAMILY
423 && (getChar(i - 1) == '.'
424 || getChar(i - 1) == '?'
425 || getChar(i - 1) == ':'
426 || getChar(i - 1) == '!'))) {
429 texrow.start(owner_->id(), i + 1);
431 } else if (style.free_spacing) {
439 bool Paragraph::Pimpl::isTextAt(string const & str, pos_type pos) const
441 pos_type const len = str.length();
443 // is the paragraph large enough?
444 if (pos + len > size())
447 // does the wanted text start at point?
448 for (string::size_type i = 0; i < str.length(); ++i) {
449 if (str[i] != owner_->text_[pos + i])
453 // is there a font change in middle of the word?
454 FontList::const_iterator cit = fontlist.begin();
455 FontList::const_iterator end = fontlist.end();
456 for (; cit != end; ++cit) {
457 if (cit->pos() >= pos)
460 if (cit != end && pos + len - 1 > cit->pos())
467 void Paragraph::Pimpl::simpleTeXSpecialChars(Buffer const & buf,
468 BufferParams const & bparams,
471 OutputParams const & runparams,
473 LyXFont & running_font,
475 LyXFont const & outerfont,
477 Change::Type & running_change,
478 LyXLayout const & style,
480 unsigned int & column,
483 if (style.pass_thru) {
484 if (c != Paragraph::META_INSET) {
488 owner_->getInset(i)->plaintext(buf, os, runparams);
493 // Two major modes: LaTeX or plain
494 // Handle here those cases common to both modes
495 // and then split to handle the two modes separately.
497 case Paragraph::META_INSET: {
498 InsetBase * inset = owner_->getInset(i);
500 // FIXME: remove this check
504 // FIXME: move this to InsetNewline::latex
505 if (inset->lyxCode() == InsetBase::NEWLINE_CODE) {
506 // newlines are handled differently here than
507 // the default in simpleTeXSpecialChars().
508 if (!style.newline_allowed) {
512 column += running_font.latexWriteEndChanges(os, basefont, basefont);
515 basefont = owner_->getLayoutFont(bparams, outerfont);
516 running_font = basefont;
518 if (font.family() == LyXFont::TYPEWRITER_FAMILY)
521 if (runparams.moving_arg)
527 texrow.start(owner_->id(), i + 1);
532 // output change tracking marks only if desired,
533 // if dvipost is installed,
534 // and with dvi/ps (other formats don't work)
535 LaTeXFeatures features(buf, bparams, runparams);
536 bool const output = bparams.output_changes
537 && runparams.flavor == OutputParams::LATEX
538 && features.isAvailable("dvipost");
540 if (inset->canTrackChanges()) {
541 column += Changes::latexMarkChange(os, running_change,
542 Change::UNCHANGED, output);
543 running_change = Change::UNCHANGED;
547 ostream::pos_type const len = os.tellp();
549 if ((inset->lyxCode() == InsetBase::GRAPHICS_CODE
550 || inset->lyxCode() == InsetBase::MATH_CODE
551 || inset->lyxCode() == InsetBase::URL_CODE)
552 && running_font.isRightToLeft()) {
558 #warning Bug: we can have an empty font change here!
559 // if there has just been a font change, we are going to close it
560 // right now, which means stupid latex code like \textsf{}. AFAIK,
561 // this does not harm dvi output. A minor bug, thus (JMarc)
563 // some insets cannot be inside a font change command
564 if (open_font && inset->noFontChange()) {
565 column +=running_font.
566 latexWriteEndChanges(os,
570 basefont = owner_->getLayoutFont(bparams, outerfont);
571 running_font = basefont;
574 int tmp = inset->latex(buf, os, runparams);
580 for (int j = 0; j < tmp; ++j) {
583 texrow.start(owner_->id(), i + 1);
586 column += os.tellp() - len;
592 // And now for the special cases within each mode
596 os << "\\textbackslash{}";
600 case '±': case '²': case '³':
601 case '×': case '÷': case '¹':
603 if ((bparams.inputenc == "latin1" ||
604 bparams.inputenc == "latin9") ||
605 (bparams.inputenc == "auto" &&
606 (font.language()->encoding()->latexName()
608 font.language()->encoding()->latexName()
610 os << "\\ensuremath{"
619 case '|': case '<': case '>':
620 // In T1 encoding, these characters exist
621 if (lyxrc.fontenc == "T1") {
623 //... but we should avoid ligatures
624 if ((c == '>' || c == '<')
626 && getChar(i + 1) == c) {
627 //os << "\\textcompwordmark{}";
628 // Jean-Marc, have a look at
629 // this. I think this works
637 // Typewriter font also has them
638 if (font.family() == LyXFont::TYPEWRITER_FAMILY) {
642 // Otherwise, we use what LaTeX
646 os << "\\textless{}";
650 os << "\\textgreater{}";
660 case '-': // "--" in Typewriter mode -> "-{}-"
662 && getChar(i + 1) == '-'
663 && font.family() == LyXFont::TYPEWRITER_FAMILY) {
672 os << "\\char`\\\"{}";
677 if (bparams.inputenc == "default") {
686 case '%': case '#': case '{':
693 os << "\\textasciitilde{}";
698 os << "\\textasciicircum{}";
703 // avoid being mistaken for optional arguments
704 os << '{' << c << '}';
709 // Blanks are printed before font switching.
710 // Sure? I am not! (try nice-latex)
711 // I am sure it's correct. LyX might be smarter
712 // in the future, but for now, nothing wrong is
718 // I assume this is hack treating typewriter as verbatim
719 if (font.family() == LyXFont::TYPEWRITER_FAMILY) {
728 // FIXME: if we have "LaTeX" with a font
729 // change in the middle (before the 'T', then
730 // the "TeX" part is still special cased.
731 // Really we should only operate this on
732 // "words" for some definition of word
736 for (; pnr < phrases_nr; ++pnr) {
737 if (isTextAt(special_phrases[pnr].phrase, i)) {
738 os << special_phrases[pnr].macro;
739 i += special_phrases[pnr].phrase.length() - 1;
740 column += special_phrases[pnr].macro.length() - 1;
745 if (pnr == phrases_nr && c != '\0') {
754 void Paragraph::Pimpl::validate(LaTeXFeatures & features,
755 LyXLayout const & layout) const
757 BufferParams const & bparams = features.bufferParams();
760 if (!params.spacing().isDefault())
761 features.require("setspace");
764 features.useLayout(layout.name());
767 Language const * doc_language = bparams.language;
769 FontList::const_iterator fcit = fontlist.begin();
770 FontList::const_iterator fend = fontlist.end();
771 for (; fcit != fend; ++fcit) {
772 if (fcit->font().noun() == LyXFont::ON) {
773 lyxerr[Debug::LATEX] << "font.noun: "
774 << fcit->font().noun()
776 features.require("noun");
777 lyxerr[Debug::LATEX] << "Noun enabled. Font: "
778 << fcit->font().stateText(0)
781 switch (fcit->font().color()) {
783 case LColor::inherit:
785 // probably we should put here all interface colors used for
786 // font displaying! For now I just add this ones I know of (Jug)
791 features.require("color");
792 lyxerr[Debug::LATEX] << "Color enabled. Font: "
793 << fcit->font().stateText(0)
797 Language const * language = fcit->font().language();
798 if (language->babel() != doc_language->babel() &&
799 language != ignore_language &&
800 language != latex_language)
802 features.useLanguage(language);
803 lyxerr[Debug::LATEX] << "Found language "
804 << language->babel() << endl;
808 if (!params.leftIndent().zero())
809 features.require("ParagraphLeftIndent");
812 InsetList::const_iterator icit = owner_->insetlist.begin();
813 InsetList::const_iterator iend = owner_->insetlist.end();
814 for (; icit != iend; ++icit) {
816 icit->inset->validate(features);
817 if (layout.needprotect &&
818 icit->inset->lyxCode() == InsetBase::FOOT_CODE)
819 features.require("NeedLyXFootnoteCode");
824 for (pos_type i = 0; i < size() ; ++i) {
825 for (size_t pnr = 0; pnr < phrases_nr; ++pnr) {
826 if (!special_phrases[pnr].builtin
827 && isTextAt(special_phrases[pnr].phrase, i)) {
828 features.require(special_phrases[pnr].phrase);