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()
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, Change change)
257 BOOST_ASSERT(pos <= size());
260 changes_->record(change, pos);
263 // This is actually very common when parsing buffers (and
264 // maybe inserting ascii text)
266 // when appending characters, no need to update tables
267 owner_->text_.push_back(c);
271 owner_->text_.insert(owner_->text_.begin() + pos, c);
273 // Update the font table.
274 FontTable search_font(pos, LyXFont());
275 for (FontList::iterator it = lower_bound(fontlist.begin(),
277 search_font, matchFT());
278 it != fontlist.end(); ++it)
280 it->pos(it->pos() + 1);
284 owner_->insetlist.increasePosAfterPos(pos);
288 void Paragraph::Pimpl::insertInset(pos_type pos,
289 InsetBase * inset, Change change)
292 BOOST_ASSERT(pos <= size());
294 insertChar(pos, META_INSET, change);
295 BOOST_ASSERT(owner_->text_[pos] == META_INSET);
297 // Add a new entry in the insetlist.
298 owner_->insetlist.insert(inset, pos);
302 void Paragraph::Pimpl::eraseIntern(pos_type pos)
304 // if it is an inset, delete the inset entry
305 if (owner_->text_[pos] == Paragraph::META_INSET) {
306 owner_->insetlist.erase(pos);
309 owner_->text_.erase(owner_->text_.begin() + pos);
311 // Erase entries in the tables.
312 FontTable search_font(pos, LyXFont());
314 FontList::iterator it =
315 lower_bound(fontlist.begin(),
317 search_font, matchFT());
318 if (it != fontlist.end() && it->pos() == pos &&
320 (it != fontlist.begin()
321 && boost::prior(it)->pos() == pos - 1))) {
322 // If it is a multi-character font
323 // entry, we just make it smaller
324 // (see update below), otherwise we
326 unsigned int const i = it - fontlist.begin();
327 fontlist.erase(fontlist.begin() + i);
328 it = fontlist.begin() + i;
329 if (i > 0 && i < fontlist.size() &&
330 fontlist[i - 1].font() == fontlist[i].font()) {
331 fontlist.erase(fontlist.begin() + i - 1);
332 it = fontlist.begin() + i - 1;
336 // Update all other entries.
337 FontList::iterator fend = fontlist.end();
338 for (; it != fend; ++it)
339 it->pos(it->pos() - 1);
341 // Update the insetlist.
342 owner_->insetlist.decreasePosAfterPos(pos);
346 bool Paragraph::Pimpl::erase(pos_type pos)
348 BOOST_ASSERT(pos < size());
351 Change::Type changetype(changes_->lookup(pos));
352 changes_->record(Change(Change::DELETED), pos);
354 // only allow the actual removal if it was /new/ text
355 if (changetype != Change::INSERTED) {
356 if (owner_->text_[pos] == Paragraph::META_INSET) {
357 owner_->getInset(pos)->markErased();
368 int Paragraph::Pimpl::erase(pos_type start, pos_type end)
371 for (pos_type count = end - start; count; --count) {
379 void Paragraph::Pimpl::simpleTeXBlanks(ostream & os, TexRow & texrow,
381 unsigned int & column,
382 LyXFont const & font,
383 LyXLayout const & style)
388 if (column > lyxrc.ascii_linelen
390 && getChar(i - 1) != ' '
392 // same in FreeSpacing mode
393 && !owner_->isFreeSpacing()
394 // In typewriter mode, we want to avoid
395 // ! . ? : at the end of a line
396 && !(font.family() == LyXFont::TYPEWRITER_FAMILY
397 && (getChar(i - 1) == '.'
398 || getChar(i - 1) == '?'
399 || getChar(i - 1) == ':'
400 || getChar(i - 1) == '!'))) {
403 texrow.start(owner_->id(), i + 1);
405 } else if (style.free_spacing) {
413 bool Paragraph::Pimpl::isTextAt(string const & str, pos_type pos) const
415 pos_type const len = str.length();
417 // is the paragraph large enough?
418 if (pos + len > size())
421 // does the wanted text start at point?
422 for (string::size_type i = 0; i < str.length(); ++i) {
423 if (str[i] != owner_->text_[pos + i])
427 // is there a font change in middle of the word?
428 FontList::const_iterator cit = fontlist.begin();
429 FontList::const_iterator end = fontlist.end();
430 for (; cit != end; ++cit) {
431 if (cit->pos() >= pos)
434 if (cit != end && pos + len - 1 > cit->pos())
441 void Paragraph::Pimpl::simpleTeXSpecialChars(Buffer const & buf,
442 BufferParams const & bparams,
445 OutputParams const & runparams,
447 LyXFont & running_font,
449 LyXFont const & outerfont,
451 Change::Type & running_change,
452 LyXLayout const & style,
454 unsigned int & column,
457 if (style.pass_thru) {
458 if (c != Paragraph::META_INSET) {
462 owner_->getInset(i)->plaintext(buf, os, runparams);
467 // Two major modes: LaTeX or plain
468 // Handle here those cases common to both modes
469 // and then split to handle the two modes separately.
471 case Paragraph::META_INSET: {
472 InsetBase * inset = owner_->getInset(i);
474 // FIXME: remove this check
478 // FIXME: move this to InsetNewline::latex
479 if (inset->lyxCode() == InsetBase::NEWLINE_CODE) {
480 // newlines are handled differently here than
481 // the default in simpleTeXSpecialChars().
482 if (!style.newline_allowed) {
486 column += running_font.latexWriteEndChanges(os, basefont, basefont);
489 basefont = owner_->getLayoutFont(bparams, outerfont);
490 running_font = basefont;
492 if (font.family() == LyXFont::TYPEWRITER_FAMILY)
495 if (runparams.moving_arg)
501 texrow.start(owner_->id(), i + 1);
506 if (inset->isTextInset()) {
507 column += Changes::latexMarkChange(os, running_change,
509 running_change = Change::UNCHANGED;
513 ostream::pos_type const len = os.tellp();
515 if ((inset->lyxCode() == InsetBase::GRAPHICS_CODE
516 || inset->lyxCode() == InsetBase::MATH_CODE
517 || inset->lyxCode() == InsetBase::URL_CODE)
518 && running_font.isRightToLeft()) {
524 #warning Bug: we can have an empty font change here!
525 // if there has just been a font change, we are going to close it
526 // right now, which means stupid latex code like \textsf{}. AFAIK,
527 // this does not harm dvi output. A minor bug, thus (JMarc)
529 // some insets cannot be inside a font change command
530 if (open_font && inset->noFontChange()) {
531 column +=running_font.
532 latexWriteEndChanges(os,
536 basefont = owner_->getLayoutFont(bparams, outerfont);
537 running_font = basefont;
540 int tmp = inset->latex(buf, os, runparams);
546 for (int j = 0; j < tmp; ++j) {
549 texrow.start(owner_->id(), i + 1);
552 column += os.tellp() - len;
558 // And now for the special cases within each mode
562 os << "\\textbackslash{}";
566 case '±': case '²': case '³':
567 case '×': case '÷': case '¹':
569 if ((bparams.inputenc == "latin1" ||
570 bparams.inputenc == "latin9") ||
571 (bparams.inputenc == "auto" &&
572 (font.language()->encoding()->LatexName()
574 font.language()->encoding()->LatexName()
576 os << "\\ensuremath{"
585 case '|': case '<': case '>':
586 // In T1 encoding, these characters exist
587 if (lyxrc.fontenc == "T1") {
589 //... but we should avoid ligatures
590 if ((c == '>' || c == '<')
592 && getChar(i + 1) == c) {
593 //os << "\\textcompwordmark{}";
594 // Jean-Marc, have a look at
595 // this. I think this works
603 // Typewriter font also has them
604 if (font.family() == LyXFont::TYPEWRITER_FAMILY) {
608 // Otherwise, we use what LaTeX
612 os << "\\textless{}";
616 os << "\\textgreater{}";
626 case '-': // "--" in Typewriter mode -> "-{}-"
628 && getChar(i + 1) == '-'
629 && font.family() == LyXFont::TYPEWRITER_FAMILY) {
638 os << "\\char`\\\"{}";
643 if (bparams.inputenc == "default") {
652 case '%': case '#': case '{':
659 os << "\\textasciitilde{}";
664 os << "\\textasciicircum{}";
669 // avoid being mistaken for optional arguments
670 os << '{' << c << '}';
675 // Blanks are printed before font switching.
676 // Sure? I am not! (try nice-latex)
677 // I am sure it's correct. LyX might be smarter
678 // in the future, but for now, nothing wrong is
684 // I assume this is hack treating typewriter as verbatim
685 if (font.family() == LyXFont::TYPEWRITER_FAMILY) {
694 // FIXME: if we have "LaTeX" with a font
695 // change in the middle (before the 'T', then
696 // the "TeX" part is still special cased.
697 // Really we should only operate this on
698 // "words" for some definition of word
702 for (; pnr < phrases_nr; ++pnr) {
703 if (isTextAt(special_phrases[pnr].phrase, i)) {
704 os << special_phrases[pnr].macro;
705 i += special_phrases[pnr].phrase.length() - 1;
706 column += special_phrases[pnr].macro.length() - 1;
711 if (pnr == phrases_nr && c != '\0') {
720 void Paragraph::Pimpl::validate(LaTeXFeatures & features,
721 LyXLayout const & layout) const
723 BufferParams const & bparams = features.bufferParams();
726 if (!params.spacing().isDefault())
727 features.require("setspace");
730 features.useLayout(layout.name());
733 Language const * doc_language = bparams.language;
735 FontList::const_iterator fcit = fontlist.begin();
736 FontList::const_iterator fend = fontlist.end();
737 for (; fcit != fend; ++fcit) {
738 if (fcit->font().noun() == LyXFont::ON) {
739 lyxerr[Debug::LATEX] << "font.noun: "
740 << fcit->font().noun()
742 features.require("noun");
743 lyxerr[Debug::LATEX] << "Noun enabled. Font: "
744 << fcit->font().stateText(0)
747 switch (fcit->font().color()) {
749 case LColor::inherit:
751 // probably we should put here all interface colors used for
752 // font displaying! For now I just add this ones I know of (Jug)
757 features.require("color");
758 lyxerr[Debug::LATEX] << "Color enabled. Font: "
759 << fcit->font().stateText(0)
763 Language const * language = fcit->font().language();
764 if (language->babel() != doc_language->babel() &&
765 language != ignore_language &&
766 language != latex_language)
768 features.useLanguage(language);
769 lyxerr[Debug::LATEX] << "Found language "
770 << language->babel() << endl;
774 if (!params.leftIndent().zero())
775 features.require("ParagraphLeftIndent");
778 InsetList::const_iterator icit = owner_->insetlist.begin();
779 InsetList::const_iterator iend = owner_->insetlist.end();
780 for (; icit != iend; ++icit) {
782 icit->inset->validate(features);
783 if (layout.needprotect &&
784 icit->inset->lyxCode() == InsetBase::FOOT_CODE)
785 features.require("NeedLyXFootnoteCode");
790 for (pos_type i = 0; i < size() ; ++i) {
791 for (size_t pnr = 0; pnr < phrases_nr; ++pnr) {
792 if (!special_phrases[pnr].builtin
793 && isTextAt(special_phrases[pnr].phrase, i)) {
794 features.require(special_phrases[pnr].phrase);