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 // output change tracking marks only if desired,
507 // if dvipost is installed,
508 // and with dvi/ps (other formats don't work)
509 LaTeXFeatures features(buf, bparams, runparams.nice);
510 bool const output = bparams.output_changes
511 && runparams.flavor == OutputParams::LATEX
512 && features.isAvailable("dvipost");
514 if (inset->isTextInset()) {
515 column += Changes::latexMarkChange(os, running_change,
516 Change::UNCHANGED, output);
517 running_change = Change::UNCHANGED;
521 ostream::pos_type const len = os.tellp();
523 if ((inset->lyxCode() == InsetBase::GRAPHICS_CODE
524 || inset->lyxCode() == InsetBase::MATH_CODE
525 || inset->lyxCode() == InsetBase::URL_CODE)
526 && running_font.isRightToLeft()) {
532 #warning Bug: we can have an empty font change here!
533 // if there has just been a font change, we are going to close it
534 // right now, which means stupid latex code like \textsf{}. AFAIK,
535 // this does not harm dvi output. A minor bug, thus (JMarc)
537 // some insets cannot be inside a font change command
538 if (open_font && inset->noFontChange()) {
539 column +=running_font.
540 latexWriteEndChanges(os,
544 basefont = owner_->getLayoutFont(bparams, outerfont);
545 running_font = basefont;
548 int tmp = inset->latex(buf, os, runparams);
554 for (int j = 0; j < tmp; ++j) {
557 texrow.start(owner_->id(), i + 1);
560 column += os.tellp() - len;
566 // And now for the special cases within each mode
570 os << "\\textbackslash{}";
574 case '±': case '²': case '³':
575 case '×': case '÷': case '¹':
577 if ((bparams.inputenc == "latin1" ||
578 bparams.inputenc == "latin9") ||
579 (bparams.inputenc == "auto" &&
580 (font.language()->encoding()->LatexName()
582 font.language()->encoding()->LatexName()
584 os << "\\ensuremath{"
593 case '|': case '<': case '>':
594 // In T1 encoding, these characters exist
595 if (lyxrc.fontenc == "T1") {
597 //... but we should avoid ligatures
598 if ((c == '>' || c == '<')
600 && getChar(i + 1) == c) {
601 //os << "\\textcompwordmark{}";
602 // Jean-Marc, have a look at
603 // this. I think this works
611 // Typewriter font also has them
612 if (font.family() == LyXFont::TYPEWRITER_FAMILY) {
616 // Otherwise, we use what LaTeX
620 os << "\\textless{}";
624 os << "\\textgreater{}";
634 case '-': // "--" in Typewriter mode -> "-{}-"
636 && getChar(i + 1) == '-'
637 && font.family() == LyXFont::TYPEWRITER_FAMILY) {
646 os << "\\char`\\\"{}";
651 if (bparams.inputenc == "default") {
660 case '%': case '#': case '{':
667 os << "\\textasciitilde{}";
672 os << "\\textasciicircum{}";
677 // avoid being mistaken for optional arguments
678 os << '{' << c << '}';
683 // Blanks are printed before font switching.
684 // Sure? I am not! (try nice-latex)
685 // I am sure it's correct. LyX might be smarter
686 // in the future, but for now, nothing wrong is
692 // I assume this is hack treating typewriter as verbatim
693 if (font.family() == LyXFont::TYPEWRITER_FAMILY) {
702 // FIXME: if we have "LaTeX" with a font
703 // change in the middle (before the 'T', then
704 // the "TeX" part is still special cased.
705 // Really we should only operate this on
706 // "words" for some definition of word
710 for (; pnr < phrases_nr; ++pnr) {
711 if (isTextAt(special_phrases[pnr].phrase, i)) {
712 os << special_phrases[pnr].macro;
713 i += special_phrases[pnr].phrase.length() - 1;
714 column += special_phrases[pnr].macro.length() - 1;
719 if (pnr == phrases_nr && c != '\0') {
728 void Paragraph::Pimpl::validate(LaTeXFeatures & features,
729 LyXLayout const & layout) const
731 BufferParams const & bparams = features.bufferParams();
734 if (!params.spacing().isDefault())
735 features.require("setspace");
738 features.useLayout(layout.name());
741 Language const * doc_language = bparams.language;
743 FontList::const_iterator fcit = fontlist.begin();
744 FontList::const_iterator fend = fontlist.end();
745 for (; fcit != fend; ++fcit) {
746 if (fcit->font().noun() == LyXFont::ON) {
747 lyxerr[Debug::LATEX] << "font.noun: "
748 << fcit->font().noun()
750 features.require("noun");
751 lyxerr[Debug::LATEX] << "Noun enabled. Font: "
752 << fcit->font().stateText(0)
755 switch (fcit->font().color()) {
757 case LColor::inherit:
759 // probably we should put here all interface colors used for
760 // font displaying! For now I just add this ones I know of (Jug)
765 features.require("color");
766 lyxerr[Debug::LATEX] << "Color enabled. Font: "
767 << fcit->font().stateText(0)
771 Language const * language = fcit->font().language();
772 if (language->babel() != doc_language->babel() &&
773 language != ignore_language &&
774 language != latex_language)
776 features.useLanguage(language);
777 lyxerr[Debug::LATEX] << "Found language "
778 << language->babel() << endl;
782 if (!params.leftIndent().zero())
783 features.require("ParagraphLeftIndent");
786 InsetList::const_iterator icit = owner_->insetlist.begin();
787 InsetList::const_iterator iend = owner_->insetlist.end();
788 for (; icit != iend; ++icit) {
790 icit->inset->validate(features);
791 if (layout.needprotect &&
792 icit->inset->lyxCode() == InsetBase::FOOT_CODE)
793 features.require("NeedLyXFootnoteCode");
798 for (pos_type i = 0; i < size() ; ++i) {
799 for (size_t pnr = 0; pnr < phrases_nr; ++pnr) {
800 if (!special_phrases[pnr].builtin
801 && isTextAt(special_phrases[pnr].phrase, i)) {
802 features.require(special_phrases[pnr].phrase);