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"
24 #include "lyxlength.h"
26 #include "outputparams.h"
29 #include <boost/next_prior.hpp>
34 using std::upper_bound;
35 using std::lower_bound;
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(bool erased)
170 BOOST_ASSERT(tracking());
177 for (pos_type i = 0; i < size(); ++i) {
178 changes_->set(Change::UNCHANGED, i);
179 if (owner_->isInset(i))
180 owner_->getInset(i)->markErased(false);
186 void Paragraph::Pimpl::acceptChange(pos_type start, pos_type end)
192 changes_.reset(new Changes(Change::UNCHANGED));
196 lyxerr[Debug::CHANGES] << "acceptchange" << endl;
199 for (; i < end; ++i) {
200 switch (lookupChange(i)) {
201 case Change::UNCHANGED:
204 case Change::INSERTED:
205 changes_->set(Change::UNCHANGED, i);
208 case Change::DELETED:
217 lyxerr[Debug::CHANGES] << "endacceptchange" << endl;
218 changes_->reset(Change::UNCHANGED);
222 void Paragraph::Pimpl::rejectChange(pos_type start, pos_type end)
228 changes_.reset(new Changes(Change::UNCHANGED));
234 for (; i < end; ++i) {
235 switch (lookupChange(i)) {
236 case Change::UNCHANGED:
239 case Change::INSERTED:
246 case Change::DELETED:
247 changes_->set(Change::UNCHANGED, i);
248 if (owner_->isInset(i))
249 owner_->getInset(i)->markErased(false);
253 changes_->reset(Change::UNCHANGED);
257 Paragraph::value_type Paragraph::Pimpl::getChar(pos_type pos) const
259 return owner_->getChar(pos);
263 void Paragraph::Pimpl::insertChar(pos_type pos, value_type c, Change change)
265 BOOST_ASSERT(pos <= size());
268 changes_->record(change, pos);
271 // This is actually very common when parsing buffers (and
272 // maybe inserting ascii text)
274 // when appending characters, no need to update tables
275 owner_->text_.push_back(c);
279 owner_->text_.insert(owner_->text_.begin() + pos, c);
281 // Update the font table.
282 FontTable search_font(pos, LyXFont());
283 for (FontList::iterator it = lower_bound(fontlist.begin(),
285 search_font, matchFT());
286 it != fontlist.end(); ++it)
288 it->pos(it->pos() + 1);
292 owner_->insetlist.increasePosAfterPos(pos);
296 void Paragraph::Pimpl::insertInset(pos_type pos,
297 InsetBase * inset, Change change)
300 BOOST_ASSERT(pos <= size());
302 insertChar(pos, META_INSET, change);
303 BOOST_ASSERT(owner_->text_[pos] == META_INSET);
305 // Add a new entry in the insetlist.
306 owner_->insetlist.insert(inset, pos);
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_->isInset(pos))
365 owner_->getInset(pos)->markErased(true);
375 int Paragraph::Pimpl::erase(pos_type start, pos_type end)
378 for (pos_type count = end - start; count; --count) {
386 void Paragraph::Pimpl::simpleTeXBlanks(ostream & os, TexRow & texrow,
388 unsigned int & column,
389 LyXFont const & font,
390 LyXLayout const & style)
395 if (column > lyxrc.ascii_linelen
397 && getChar(i - 1) != ' '
399 // same in FreeSpacing mode
400 && !owner_->isFreeSpacing()
401 // In typewriter mode, we want to avoid
402 // ! . ? : at the end of a line
403 && !(font.family() == LyXFont::TYPEWRITER_FAMILY
404 && (getChar(i - 1) == '.'
405 || getChar(i - 1) == '?'
406 || getChar(i - 1) == ':'
407 || getChar(i - 1) == '!'))) {
410 texrow.start(owner_->id(), i + 1);
412 } else if (style.free_spacing) {
420 bool Paragraph::Pimpl::isTextAt(string const & str, pos_type pos) const
422 pos_type const len = str.length();
424 // is the paragraph large enough?
425 if (pos + len > size())
428 // does the wanted text start at point?
429 for (string::size_type i = 0; i < str.length(); ++i) {
430 if (str[i] != owner_->text_[pos + i])
434 // is there a font change in middle of the word?
435 FontList::const_iterator cit = fontlist.begin();
436 FontList::const_iterator end = fontlist.end();
437 for (; cit != end; ++cit) {
438 if (cit->pos() >= pos)
441 if (cit != end && pos + len - 1 > cit->pos())
448 void Paragraph::Pimpl::simpleTeXSpecialChars(Buffer const & buf,
449 BufferParams const & bparams,
452 OutputParams const & runparams,
454 LyXFont & running_font,
456 LyXFont const & outerfont,
458 Change::Type & running_change,
459 LyXLayout const & style,
461 unsigned int & column,
464 if (style.pass_thru) {
465 if (c != Paragraph::META_INSET) {
469 owner_->getInset(i)->plaintext(buf, os, runparams);
474 // Two major modes: LaTeX or plain
475 // Handle here those cases common to both modes
476 // and then split to handle the two modes separately.
478 case Paragraph::META_INSET: {
479 InsetBase * inset = owner_->getInset(i);
481 // FIXME: remove this check
485 // FIXME: move this to InsetNewline::latex
486 if (inset->lyxCode() == InsetBase::NEWLINE_CODE) {
487 // newlines are handled differently here than
488 // the default in simpleTeXSpecialChars().
489 if (!style.newline_allowed) {
493 column += running_font.latexWriteEndChanges(os, basefont, basefont);
496 basefont = owner_->getLayoutFont(bparams, outerfont);
497 running_font = basefont;
499 if (font.family() == LyXFont::TYPEWRITER_FAMILY)
502 if (runparams.moving_arg)
508 texrow.start(owner_->id(), i + 1);
513 // output change tracking marks only if desired,
514 // if dvipost is installed,
515 // and with dvi/ps (other formats don't work)
516 LaTeXFeatures features(buf, bparams, runparams.nice);
517 bool const output = bparams.output_changes
518 && runparams.flavor == OutputParams::LATEX
519 && features.isAvailable("dvipost");
521 if (inset->isTextInset()) {
522 column += Changes::latexMarkChange(os, running_change,
523 Change::UNCHANGED, output);
524 running_change = Change::UNCHANGED;
528 ostream::pos_type const len = os.tellp();
530 if ((inset->lyxCode() == InsetBase::GRAPHICS_CODE
531 || inset->lyxCode() == InsetBase::MATH_CODE
532 || inset->lyxCode() == InsetBase::URL_CODE)
533 && running_font.isRightToLeft()) {
539 #warning Bug: we can have an empty font change here!
540 // if there has just been a font change, we are going to close it
541 // right now, which means stupid latex code like \textsf{}. AFAIK,
542 // this does not harm dvi output. A minor bug, thus (JMarc)
544 // some insets cannot be inside a font change command
545 if (open_font && inset->noFontChange()) {
546 column +=running_font.
547 latexWriteEndChanges(os,
551 basefont = owner_->getLayoutFont(bparams, outerfont);
552 running_font = basefont;
555 int tmp = inset->latex(buf, os, runparams);
561 for (int j = 0; j < tmp; ++j) {
564 texrow.start(owner_->id(), i + 1);
567 column += os.tellp() - len;
573 // And now for the special cases within each mode
577 os << "\\textbackslash{}";
581 case '±': case '²': case '³':
582 case '×': case '÷': case '¹':
584 if ((bparams.inputenc == "latin1" ||
585 bparams.inputenc == "latin9") ||
586 (bparams.inputenc == "auto" &&
587 (font.language()->encoding()->LatexName()
589 font.language()->encoding()->LatexName()
591 os << "\\ensuremath{"
600 case '|': case '<': case '>':
601 // In T1 encoding, these characters exist
602 if (lyxrc.fontenc == "T1") {
604 //... but we should avoid ligatures
605 if ((c == '>' || c == '<')
607 && getChar(i + 1) == c) {
608 //os << "\\textcompwordmark{}";
609 // Jean-Marc, have a look at
610 // this. I think this works
618 // Typewriter font also has them
619 if (font.family() == LyXFont::TYPEWRITER_FAMILY) {
623 // Otherwise, we use what LaTeX
627 os << "\\textless{}";
631 os << "\\textgreater{}";
641 case '-': // "--" in Typewriter mode -> "-{}-"
643 && getChar(i + 1) == '-'
644 && font.family() == LyXFont::TYPEWRITER_FAMILY) {
653 os << "\\char`\\\"{}";
658 if (bparams.inputenc == "default") {
667 case '%': case '#': case '{':
674 os << "\\textasciitilde{}";
679 os << "\\textasciicircum{}";
684 // avoid being mistaken for optional arguments
685 os << '{' << c << '}';
690 // Blanks are printed before font switching.
691 // Sure? I am not! (try nice-latex)
692 // I am sure it's correct. LyX might be smarter
693 // in the future, but for now, nothing wrong is
699 // I assume this is hack treating typewriter as verbatim
700 if (font.family() == LyXFont::TYPEWRITER_FAMILY) {
709 // FIXME: if we have "LaTeX" with a font
710 // change in the middle (before the 'T', then
711 // the "TeX" part is still special cased.
712 // Really we should only operate this on
713 // "words" for some definition of word
717 for (; pnr < phrases_nr; ++pnr) {
718 if (isTextAt(special_phrases[pnr].phrase, i)) {
719 os << special_phrases[pnr].macro;
720 i += special_phrases[pnr].phrase.length() - 1;
721 column += special_phrases[pnr].macro.length() - 1;
726 if (pnr == phrases_nr && c != '\0') {
735 void Paragraph::Pimpl::validate(LaTeXFeatures & features,
736 LyXLayout const & layout) const
738 BufferParams const & bparams = features.bufferParams();
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::const_iterator icit = owner_->insetlist.begin();
794 InsetList::const_iterator iend = owner_->insetlist.end();
795 for (; icit != iend; ++icit) {
797 icit->inset->validate(features);
798 if (layout.needprotect &&
799 icit->inset->lyxCode() == InsetBase::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);