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() + 1);
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() + 1);
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 void Paragraph::Pimpl::setChangeFull(pos_type pos, Change change)
155 changes_->set(change, pos);
158 Change::Type Paragraph::Pimpl::lookupChange(pos_type pos) const
161 return Change::UNCHANGED;
163 return changes_->lookup(pos);
167 Change const Paragraph::Pimpl::lookupChangeFull(pos_type pos) const
170 return Change(Change::UNCHANGED);
172 return changes_->lookupFull(pos);
176 void Paragraph::Pimpl::markErased(bool erased)
178 BOOST_ASSERT(tracking());
183 for (pos_type i = 0; i < size(); ++i) {
184 changes_->set(Change::UNCHANGED, i);
185 if (owner_->isInset(i))
186 owner_->getInset(i)->markErased(false);
192 void Paragraph::Pimpl::acceptChange(pos_type start, pos_type end)
198 changes_.reset(new Changes(Change::UNCHANGED));
202 lyxerr[Debug::CHANGES] << "acceptchange" << endl;
205 for (; i < end; ++i) {
206 switch (lookupChange(i)) {
207 case Change::UNCHANGED:
210 case Change::INSERTED:
211 changes_->set(Change::UNCHANGED, i);
214 case Change::DELETED:
215 // Suppress access to nonexistent
216 // "end-of-paragraph char":
227 lyxerr[Debug::CHANGES] << "endacceptchange" << endl;
228 changes_->reset(Change::UNCHANGED);
232 void Paragraph::Pimpl::rejectChange(pos_type start, pos_type end)
238 changes_.reset(new Changes(Change::UNCHANGED));
244 for (; i < end; ++i) {
245 switch (lookupChange(i)) {
246 case Change::UNCHANGED:
249 case Change::INSERTED:
258 case Change::DELETED:
259 changes_->set(Change::UNCHANGED, i);
260 // No real char at position size():
261 if (i < size() && owner_->isInset(i))
262 owner_->getInset(i)->markErased(false);
266 changes_->reset(Change::UNCHANGED);
270 Paragraph::value_type Paragraph::Pimpl::getChar(pos_type pos) const
272 return owner_->getChar(pos);
276 void Paragraph::Pimpl::insertChar(pos_type pos, value_type c, Change change)
278 BOOST_ASSERT(pos <= size());
281 changes_->record(change, pos);
284 // This is actually very common when parsing buffers (and
285 // maybe inserting ascii text)
287 // when appending characters, no need to update tables
288 owner_->text_.push_back(c);
292 owner_->text_.insert(owner_->text_.begin() + pos, c);
294 // Update the font table.
295 FontTable search_font(pos, LyXFont());
296 for (FontList::iterator it = lower_bound(fontlist.begin(),
298 search_font, matchFT());
299 it != fontlist.end(); ++it)
301 it->pos(it->pos() + 1);
305 owner_->insetlist.increasePosAfterPos(pos);
309 void Paragraph::Pimpl::insertInset(pos_type pos,
310 InsetBase * inset, Change change)
313 BOOST_ASSERT(pos <= size());
315 insertChar(pos, META_INSET, change);
316 BOOST_ASSERT(owner_->text_[pos] == META_INSET);
318 // Add a new entry in the insetlist.
319 owner_->insetlist.insert(inset, pos);
323 void Paragraph::Pimpl::eraseIntern(pos_type pos)
325 // if it is an inset, delete the inset entry
326 if (owner_->text_[pos] == Paragraph::META_INSET) {
327 owner_->insetlist.erase(pos);
330 owner_->text_.erase(owner_->text_.begin() + pos);
332 // Erase entries in the tables.
333 FontTable search_font(pos, LyXFont());
335 FontList::iterator it =
336 lower_bound(fontlist.begin(),
338 search_font, matchFT());
339 if (it != fontlist.end() && it->pos() == pos &&
341 (it != fontlist.begin()
342 && boost::prior(it)->pos() == pos - 1))) {
343 // If it is a multi-character font
344 // entry, we just make it smaller
345 // (see update below), otherwise we
347 unsigned int const i = it - fontlist.begin();
348 fontlist.erase(fontlist.begin() + i);
349 it = fontlist.begin() + i;
350 if (i > 0 && i < fontlist.size() &&
351 fontlist[i - 1].font() == fontlist[i].font()) {
352 fontlist.erase(fontlist.begin() + i - 1);
353 it = fontlist.begin() + i - 1;
357 // Update all other entries.
358 FontList::iterator fend = fontlist.end();
359 for (; it != fend; ++it)
360 it->pos(it->pos() - 1);
362 // Update the insetlist.
363 owner_->insetlist.decreasePosAfterPos(pos);
367 bool Paragraph::Pimpl::erase(pos_type pos)
369 BOOST_ASSERT(pos <= size());
372 Change::Type changetype(changes_->lookup(pos));
373 changes_->record(Change(Change::DELETED), pos);
375 // only allow the actual removal if it was /new/ text
376 if (changetype != Change::INSERTED) {
377 if (pos < size() && owner_->isInset(pos))
378 owner_->getInset(pos)->markErased(true);
383 // Don't physically access nonexistent end-of-paragraph char
393 int Paragraph::Pimpl::erase(pos_type start, pos_type end)
396 for (pos_type count = end - start; count; --count) {
404 void Paragraph::Pimpl::simpleTeXBlanks(ostream & os, TexRow & texrow,
406 unsigned int & column,
407 LyXFont const & font,
408 LyXLayout const & style)
413 if (column > lyxrc.ascii_linelen
415 && getChar(i - 1) != ' '
417 // same in FreeSpacing mode
418 && !owner_->isFreeSpacing()
419 // In typewriter mode, we want to avoid
420 // ! . ? : at the end of a line
421 && !(font.family() == LyXFont::TYPEWRITER_FAMILY
422 && (getChar(i - 1) == '.'
423 || getChar(i - 1) == '?'
424 || getChar(i - 1) == ':'
425 || getChar(i - 1) == '!'))) {
428 texrow.start(owner_->id(), i + 1);
430 } else if (style.free_spacing) {
438 bool Paragraph::Pimpl::isTextAt(string const & str, pos_type pos) const
440 pos_type const len = str.length();
442 // is the paragraph large enough?
443 if (pos + len > size())
446 // does the wanted text start at point?
447 for (string::size_type i = 0; i < str.length(); ++i) {
448 if (str[i] != owner_->text_[pos + i])
452 // is there a font change in middle of the word?
453 FontList::const_iterator cit = fontlist.begin();
454 FontList::const_iterator end = fontlist.end();
455 for (; cit != end; ++cit) {
456 if (cit->pos() >= pos)
459 if (cit != end && pos + len - 1 > cit->pos())
466 void Paragraph::Pimpl::simpleTeXSpecialChars(Buffer const & buf,
467 BufferParams const & bparams,
470 OutputParams const & runparams,
472 LyXFont & running_font,
474 LyXFont const & outerfont,
476 Change::Type & running_change,
477 LyXLayout const & style,
479 unsigned int & column,
482 if (style.pass_thru) {
483 if (c != Paragraph::META_INSET) {
487 owner_->getInset(i)->plaintext(buf, os, runparams);
492 // Two major modes: LaTeX or plain
493 // Handle here those cases common to both modes
494 // and then split to handle the two modes separately.
496 case Paragraph::META_INSET: {
497 InsetBase * inset = owner_->getInset(i);
499 // FIXME: remove this check
503 // FIXME: move this to InsetNewline::latex
504 if (inset->lyxCode() == InsetBase::NEWLINE_CODE) {
505 // newlines are handled differently here than
506 // the default in simpleTeXSpecialChars().
507 if (!style.newline_allowed) {
511 column += running_font.latexWriteEndChanges(os, basefont, basefont);
514 basefont = owner_->getLayoutFont(bparams, outerfont);
515 running_font = basefont;
517 if (font.family() == LyXFont::TYPEWRITER_FAMILY)
520 if (runparams.moving_arg)
526 texrow.start(owner_->id(), i + 1);
531 // output change tracking marks only if desired,
532 // if dvipost is installed,
533 // and with dvi/ps (other formats don't work)
534 LaTeXFeatures features(buf, bparams, runparams);
535 bool const output = bparams.output_changes
536 && runparams.flavor == OutputParams::LATEX
537 && features.isAvailable("dvipost");
539 if (inset->canTrackChanges()) {
540 column += Changes::latexMarkChange(os, running_change,
541 Change::UNCHANGED, output);
542 running_change = Change::UNCHANGED;
546 ostream::pos_type const len = os.tellp();
548 if ((inset->lyxCode() == InsetBase::GRAPHICS_CODE
549 || inset->lyxCode() == InsetBase::MATH_CODE
550 || inset->lyxCode() == InsetBase::URL_CODE)
551 && running_font.isRightToLeft()) {
557 #warning Bug: we can have an empty font change here!
558 // if there has just been a font change, we are going to close it
559 // right now, which means stupid latex code like \textsf{}. AFAIK,
560 // this does not harm dvi output. A minor bug, thus (JMarc)
562 // some insets cannot be inside a font change command
563 if (open_font && inset->noFontChange()) {
564 column +=running_font.
565 latexWriteEndChanges(os,
569 basefont = owner_->getLayoutFont(bparams, outerfont);
570 running_font = basefont;
573 int tmp = inset->latex(buf, os, runparams);
579 for (int j = 0; j < tmp; ++j) {
582 texrow.start(owner_->id(), i + 1);
585 column += os.tellp() - len;
591 // And now for the special cases within each mode
595 os << "\\textbackslash{}";
599 case '±': case '²': case '³':
600 case '×': case '÷': case '¹':
602 if ((bparams.inputenc == "latin1" ||
603 bparams.inputenc == "latin9") ||
604 (bparams.inputenc == "auto" &&
605 (font.language()->encoding()->LatexName()
607 font.language()->encoding()->LatexName()
609 os << "\\ensuremath{"
618 case '|': case '<': case '>':
619 // In T1 encoding, these characters exist
620 if (lyxrc.fontenc == "T1") {
622 //... but we should avoid ligatures
623 if ((c == '>' || c == '<')
625 && getChar(i + 1) == c) {
626 //os << "\\textcompwordmark{}";
627 // Jean-Marc, have a look at
628 // this. I think this works
636 // Typewriter font also has them
637 if (font.family() == LyXFont::TYPEWRITER_FAMILY) {
641 // Otherwise, we use what LaTeX
645 os << "\\textless{}";
649 os << "\\textgreater{}";
659 case '-': // "--" in Typewriter mode -> "-{}-"
661 && getChar(i + 1) == '-'
662 && font.family() == LyXFont::TYPEWRITER_FAMILY) {
671 os << "\\char`\\\"{}";
676 if (bparams.inputenc == "default") {
685 case '%': case '#': case '{':
692 os << "\\textasciitilde{}";
697 os << "\\textasciicircum{}";
702 // avoid being mistaken for optional arguments
703 os << '{' << c << '}';
708 // Blanks are printed before font switching.
709 // Sure? I am not! (try nice-latex)
710 // I am sure it's correct. LyX might be smarter
711 // in the future, but for now, nothing wrong is
717 // I assume this is hack treating typewriter as verbatim
718 if (font.family() == LyXFont::TYPEWRITER_FAMILY) {
727 // FIXME: if we have "LaTeX" with a font
728 // change in the middle (before the 'T', then
729 // the "TeX" part is still special cased.
730 // Really we should only operate this on
731 // "words" for some definition of word
735 for (; pnr < phrases_nr; ++pnr) {
736 if (isTextAt(special_phrases[pnr].phrase, i)) {
737 os << special_phrases[pnr].macro;
738 i += special_phrases[pnr].phrase.length() - 1;
739 column += special_phrases[pnr].macro.length() - 1;
744 if (pnr == phrases_nr && c != '\0') {
753 void Paragraph::Pimpl::validate(LaTeXFeatures & features,
754 LyXLayout const & layout) const
756 BufferParams const & bparams = features.bufferParams();
759 if (!params.spacing().isDefault())
760 features.require("setspace");
763 features.useLayout(layout.name());
766 Language const * doc_language = bparams.language;
768 FontList::const_iterator fcit = fontlist.begin();
769 FontList::const_iterator fend = fontlist.end();
770 for (; fcit != fend; ++fcit) {
771 if (fcit->font().noun() == LyXFont::ON) {
772 lyxerr[Debug::LATEX] << "font.noun: "
773 << fcit->font().noun()
775 features.require("noun");
776 lyxerr[Debug::LATEX] << "Noun enabled. Font: "
777 << fcit->font().stateText(0)
780 switch (fcit->font().color()) {
782 case LColor::inherit:
784 // probably we should put here all interface colors used for
785 // font displaying! For now I just add this ones I know of (Jug)
790 features.require("color");
791 lyxerr[Debug::LATEX] << "Color enabled. Font: "
792 << fcit->font().stateText(0)
796 Language const * language = fcit->font().language();
797 if (language->babel() != doc_language->babel() &&
798 language != ignore_language &&
799 language != latex_language)
801 features.useLanguage(language);
802 lyxerr[Debug::LATEX] << "Found language "
803 << language->babel() << endl;
807 if (!params.leftIndent().zero())
808 features.require("ParagraphLeftIndent");
811 InsetList::const_iterator icit = owner_->insetlist.begin();
812 InsetList::const_iterator iend = owner_->insetlist.end();
813 for (; icit != iend; ++icit) {
815 icit->inset->validate(features);
816 if (layout.needprotect &&
817 icit->inset->lyxCode() == InsetBase::FOOT_CODE)
818 features.require("NeedLyXFootnoteCode");
823 for (pos_type i = 0; i < size() ; ++i) {
824 for (size_t pnr = 0; pnr < phrases_nr; ++pnr) {
825 if (!special_phrases[pnr].builtin
826 && isTextAt(special_phrases[pnr].phrase, i)) {
827 features.require(special_phrases[pnr].phrase);