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 bool Paragraph::Pimpl::isChanged(pos_type start, pos_type end) const
98 return changes_->isChange(start, end);
102 bool Paragraph::Pimpl::isChangeEdited(pos_type start, pos_type end) const
107 return changes_->isChangeEdited(start, end);
111 void Paragraph::Pimpl::setChangeType(pos_type pos, Change::Type type)
116 changes_->set(type, pos);
120 void Paragraph::Pimpl::setChange(pos_type pos, Change change)
125 changes_->set(change, pos);
129 Change const Paragraph::Pimpl::lookupChange(pos_type pos) const
132 return Change(Change::UNCHANGED);
134 return changes_->lookup(pos);
138 void Paragraph::Pimpl::markErased(bool erased)
140 BOOST_ASSERT(tracking());
145 for (pos_type i = 0; i < size(); ++i) {
146 changes_->set(Change::UNCHANGED, i);
147 if (owner_->isInset(i))
148 owner_->getInset(i)->markErased(false);
154 void Paragraph::Pimpl::acceptChange(pos_type start, pos_type end)
160 changes_.reset(new Changes(Change::UNCHANGED));
164 lyxerr[Debug::CHANGES] << "acceptchange" << endl;
167 for (; i < end; ++i) {
168 switch (lookupChange(i).type) {
169 case Change::UNCHANGED:
172 case Change::INSERTED:
173 changes_->set(Change::UNCHANGED, i);
176 case Change::DELETED:
177 // Suppress access to nonexistent
178 // "end-of-paragraph char":
189 lyxerr[Debug::CHANGES] << "endacceptchange" << endl;
190 changes_->reset(Change::UNCHANGED);
194 void Paragraph::Pimpl::rejectChange(pos_type start, pos_type end)
200 changes_.reset(new Changes(Change::UNCHANGED));
206 for (; i < end; ++i) {
207 switch (lookupChange(i).type) {
208 case Change::UNCHANGED:
211 case Change::INSERTED:
220 case Change::DELETED:
221 changes_->set(Change::UNCHANGED, i);
222 // No real char at position size():
223 if (i < size() && owner_->isInset(i))
224 owner_->getInset(i)->markErased(false);
228 changes_->reset(Change::UNCHANGED);
232 Paragraph::value_type Paragraph::Pimpl::getChar(pos_type pos) const
234 return owner_->getChar(pos);
238 void Paragraph::Pimpl::insertChar(pos_type pos, value_type c, Change change)
240 BOOST_ASSERT(pos <= size());
243 changes_->record(change, pos);
246 // This is actually very common when parsing buffers (and
247 // maybe inserting ascii text)
249 // when appending characters, no need to update tables
250 owner_->text_.push_back(c);
254 owner_->text_.insert(owner_->text_.begin() + pos, c);
256 // Update the font table.
257 FontTable search_font(pos, LyXFont());
258 for (FontList::iterator it = lower_bound(fontlist.begin(),
260 search_font, matchFT());
261 it != fontlist.end(); ++it)
263 it->pos(it->pos() + 1);
267 owner_->insetlist.increasePosAfterPos(pos);
271 void Paragraph::Pimpl::insertInset(pos_type pos,
272 InsetBase * inset, Change change)
275 BOOST_ASSERT(pos <= size());
277 insertChar(pos, META_INSET, change);
278 BOOST_ASSERT(owner_->text_[pos] == META_INSET);
280 // Add a new entry in the insetlist.
281 owner_->insetlist.insert(inset, pos);
285 void Paragraph::Pimpl::eraseIntern(pos_type pos)
287 // if it is an inset, delete the inset entry
288 if (owner_->text_[pos] == Paragraph::META_INSET) {
289 owner_->insetlist.erase(pos);
292 owner_->text_.erase(owner_->text_.begin() + pos);
294 // Erase entries in the tables.
295 FontTable search_font(pos, LyXFont());
297 FontList::iterator it =
298 lower_bound(fontlist.begin(),
300 search_font, matchFT());
301 if (it != fontlist.end() && it->pos() == pos &&
303 (it != fontlist.begin()
304 && boost::prior(it)->pos() == pos - 1))) {
305 // If it is a multi-character font
306 // entry, we just make it smaller
307 // (see update below), otherwise we
309 unsigned int const i = it - fontlist.begin();
310 fontlist.erase(fontlist.begin() + i);
311 it = fontlist.begin() + i;
312 if (i > 0 && i < fontlist.size() &&
313 fontlist[i - 1].font() == fontlist[i].font()) {
314 fontlist.erase(fontlist.begin() + i - 1);
315 it = fontlist.begin() + i - 1;
319 // Update all other entries.
320 FontList::iterator fend = fontlist.end();
321 for (; it != fend; ++it)
322 it->pos(it->pos() - 1);
324 // Update the insetlist.
325 owner_->insetlist.decreasePosAfterPos(pos);
329 bool Paragraph::Pimpl::erase(pos_type pos)
331 BOOST_ASSERT(pos <= size());
334 Change::Type changetype(changes_->lookup(pos).type);
336 // only allow the actual removal if it was /new/ text
337 if (changetype != Change::INSERTED) {
338 changes_->record(Change(Change::DELETED), pos);
339 if (pos < size() && owner_->isInset(pos))
340 owner_->getInset(pos)->markErased(true);
345 // Don't physically access nonexistent end-of-paragraph char
355 int Paragraph::Pimpl::erase(pos_type start, pos_type end)
358 for (pos_type count = end - start; count; --count) {
366 void Paragraph::Pimpl::simpleTeXBlanks(ostream & os, TexRow & texrow,
368 unsigned int & column,
369 LyXFont const & font,
370 LyXLayout const & style)
375 if (column > lyxrc.ascii_linelen
377 && getChar(i - 1) != ' '
379 // same in FreeSpacing mode
380 && !owner_->isFreeSpacing()
381 // In typewriter mode, we want to avoid
382 // ! . ? : at the end of a line
383 && !(font.family() == LyXFont::TYPEWRITER_FAMILY
384 && (getChar(i - 1) == '.'
385 || getChar(i - 1) == '?'
386 || getChar(i - 1) == ':'
387 || getChar(i - 1) == '!'))) {
390 texrow.start(owner_->id(), i + 1);
392 } else if (style.free_spacing) {
400 bool Paragraph::Pimpl::isTextAt(string const & str, pos_type pos) const
402 pos_type const len = str.length();
404 // is the paragraph large enough?
405 if (pos + len > size())
408 // does the wanted text start at point?
409 for (string::size_type i = 0; i < str.length(); ++i) {
410 if (str[i] != owner_->text_[pos + i])
414 // is there a font change in middle of the word?
415 FontList::const_iterator cit = fontlist.begin();
416 FontList::const_iterator end = fontlist.end();
417 for (; cit != end; ++cit) {
418 if (cit->pos() >= pos)
421 if (cit != end && pos + len - 1 > cit->pos())
428 void Paragraph::Pimpl::simpleTeXSpecialChars(Buffer const & buf,
429 BufferParams const & bparams,
432 OutputParams const & runparams,
434 LyXFont & running_font,
436 LyXFont const & outerfont,
438 Change::Type & running_change,
439 LyXLayout const & style,
441 unsigned int & column,
444 if (style.pass_thru) {
445 if (c != Paragraph::META_INSET) {
449 owner_->getInset(i)->plaintext(buf, os, runparams);
454 // Two major modes: LaTeX or plain
455 // Handle here those cases common to both modes
456 // and then split to handle the two modes separately.
458 case Paragraph::META_INSET: {
459 InsetBase * inset = owner_->getInset(i);
461 // FIXME: remove this check
465 // FIXME: move this to InsetNewline::latex
466 if (inset->lyxCode() == InsetBase::NEWLINE_CODE) {
467 // newlines are handled differently here than
468 // the default in simpleTeXSpecialChars().
469 if (!style.newline_allowed) {
473 column += running_font.latexWriteEndChanges(os, basefont, basefont);
476 basefont = owner_->getLayoutFont(bparams, outerfont);
477 running_font = basefont;
479 if (font.family() == LyXFont::TYPEWRITER_FAMILY)
482 if (runparams.moving_arg)
488 texrow.start(owner_->id(), i + 1);
493 // output change tracking marks only if desired,
494 // if dvipost is installed,
495 // and with dvi/ps (other formats don't work)
496 LaTeXFeatures features(buf, bparams, runparams);
497 bool const output = bparams.outputChanges
498 && runparams.flavor == OutputParams::LATEX
499 && features.isAvailable("dvipost");
501 if (inset->canTrackChanges()) {
502 column += Changes::latexMarkChange(os, running_change,
503 Change::UNCHANGED, output);
504 running_change = Change::UNCHANGED;
508 ostream::pos_type const len = os.tellp();
510 if ((inset->lyxCode() == InsetBase::GRAPHICS_CODE
511 || inset->lyxCode() == InsetBase::MATH_CODE
512 || inset->lyxCode() == InsetBase::URL_CODE)
513 && running_font.isRightToLeft()) {
519 #warning Bug: we can have an empty font change here!
520 // if there has just been a font change, we are going to close it
521 // right now, which means stupid latex code like \textsf{}. AFAIK,
522 // this does not harm dvi output. A minor bug, thus (JMarc)
524 // some insets cannot be inside a font change command
525 if (open_font && inset->noFontChange()) {
526 column +=running_font.
527 latexWriteEndChanges(os,
531 basefont = owner_->getLayoutFont(bparams, outerfont);
532 running_font = basefont;
535 int tmp = inset->latex(buf, os, runparams);
541 for (int j = 0; j < tmp; ++j) {
544 texrow.start(owner_->id(), i + 1);
547 column += os.tellp() - len;
553 // And now for the special cases within each mode
557 os << "\\textbackslash{}";
561 case '±': case '²': case '³':
562 case '×': case '÷': case '¹':
564 if ((bparams.inputenc == "latin1" ||
565 bparams.inputenc == "latin9") ||
566 (bparams.inputenc == "auto" &&
567 (font.language()->encoding()->latexName()
569 font.language()->encoding()->latexName()
571 os << "\\ensuremath{"
580 case '|': case '<': case '>':
581 // In T1 encoding, these characters exist
582 if (lyxrc.fontenc == "T1") {
584 //... but we should avoid ligatures
585 if ((c == '>' || c == '<')
587 && getChar(i + 1) == c) {
588 //os << "\\textcompwordmark{}";
589 // Jean-Marc, have a look at
590 // this. I think this works
598 // Typewriter font also has them
599 if (font.family() == LyXFont::TYPEWRITER_FAMILY) {
603 // Otherwise, we use what LaTeX
607 os << "\\textless{}";
611 os << "\\textgreater{}";
621 case '-': // "--" in Typewriter mode -> "-{}-"
623 && getChar(i + 1) == '-'
624 && font.family() == LyXFont::TYPEWRITER_FAMILY) {
633 os << "\\char`\\\"{}";
638 if (bparams.inputenc == "default") {
647 case '%': case '#': case '{':
654 os << "\\textasciitilde{}";
659 os << "\\textasciicircum{}";
664 // avoid being mistaken for optional arguments
665 os << '{' << c << '}';
670 // Blanks are printed before font switching.
671 // Sure? I am not! (try nice-latex)
672 // I am sure it's correct. LyX might be smarter
673 // in the future, but for now, nothing wrong is
679 // I assume this is hack treating typewriter as verbatim
680 if (font.family() == LyXFont::TYPEWRITER_FAMILY) {
689 // FIXME: if we have "LaTeX" with a font
690 // change in the middle (before the 'T', then
691 // the "TeX" part is still special cased.
692 // Really we should only operate this on
693 // "words" for some definition of word
697 for (; pnr < phrases_nr; ++pnr) {
698 if (isTextAt(special_phrases[pnr].phrase, i)) {
699 os << special_phrases[pnr].macro;
700 i += special_phrases[pnr].phrase.length() - 1;
701 column += special_phrases[pnr].macro.length() - 1;
706 if (pnr == phrases_nr && c != '\0') {
715 void Paragraph::Pimpl::validate(LaTeXFeatures & features,
716 LyXLayout const & layout) const
718 BufferParams const & bparams = features.bufferParams();
721 if (!params.spacing().isDefault())
722 features.require("setspace");
725 features.useLayout(layout.name());
728 Language const * doc_language = bparams.language;
730 FontList::const_iterator fcit = fontlist.begin();
731 FontList::const_iterator fend = fontlist.end();
732 for (; fcit != fend; ++fcit) {
733 if (fcit->font().noun() == LyXFont::ON) {
734 lyxerr[Debug::LATEX] << "font.noun: "
735 << fcit->font().noun()
737 features.require("noun");
738 lyxerr[Debug::LATEX] << "Noun enabled. Font: "
739 << fcit->font().stateText(0)
742 switch (fcit->font().color()) {
744 case LColor::inherit:
746 // probably we should put here all interface colors used for
747 // font displaying! For now I just add this ones I know of (Jug)
752 features.require("color");
753 lyxerr[Debug::LATEX] << "Color enabled. Font: "
754 << fcit->font().stateText(0)
758 Language const * language = fcit->font().language();
759 if (language->babel() != doc_language->babel() &&
760 language != ignore_language &&
761 language != latex_language)
763 features.useLanguage(language);
764 lyxerr[Debug::LATEX] << "Found language "
765 << language->babel() << endl;
769 if (!params.leftIndent().zero())
770 features.require("ParagraphLeftIndent");
773 InsetList::const_iterator icit = owner_->insetlist.begin();
774 InsetList::const_iterator iend = owner_->insetlist.end();
775 for (; icit != iend; ++icit) {
777 icit->inset->validate(features);
778 if (layout.needprotect &&
779 icit->inset->lyxCode() == InsetBase::FOOT_CODE)
780 features.require("NeedLyXFootnoteCode");
785 for (pos_type i = 0; i < size() ; ++i) {
786 for (size_t pnr = 0; pnr < phrases_nr; ++pnr) {
787 if (!special_phrases[pnr].builtin
788 && isTextAt(special_phrases[pnr].phrase, i)) {
789 features.require(special_phrases[pnr].phrase);