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>
33 using lyx::odocstream;
37 using std::upper_bound;
38 using std::lower_bound;
42 // Initialization of the counter for the paragraph id's,
43 unsigned int Paragraph::Pimpl::paragraph_id = 0;
47 struct special_phrase {
53 special_phrase const special_phrases[] = {
54 { "LyX", lyx::from_ascii("\\LyX{}"), false },
55 { "TeX", lyx::from_ascii("\\TeX{}"), true },
56 { "LaTeX2e", lyx::from_ascii("\\LaTeXe{}"), true },
57 { "LaTeX", lyx::from_ascii("\\LaTeX{}"), true },
60 size_t const phrases_nr = sizeof(special_phrases)/sizeof(special_phrase);
65 Paragraph::Pimpl::Pimpl(Paragraph * owner)
73 Paragraph::Pimpl::Pimpl(Pimpl const & p, Paragraph * owner)
74 : params(p.params), owner_(owner)
76 inset_owner = p.inset_owner;
77 fontlist = p.fontlist;
81 changes_.reset(new Changes(*p.changes_.get()));
85 void Paragraph::Pimpl::setContentsFromPar(Paragraph const & par)
87 owner_->text_ = par.text_;
88 if (par.pimpl_->tracking()) {
89 changes_.reset(new Changes(*(par.pimpl_->changes_.get())));
94 bool Paragraph::Pimpl::isChanged(pos_type start, pos_type end) const
96 // FIXME: change tracking (MG)
100 return changes_->isChange(start, end);
104 void Paragraph::Pimpl::setChange(Change const & change)
106 // FIXME: change tracking (MG)
107 // changes_.set(change, 0, size());
109 if (change.type == Change::UNCHANGED) { // only for UNCHANGED ???
110 for (pos_type i = 0; i < size(); ++i) {
111 if (owner_->isInset(i)) {
112 // FIXME: change tracking (MG)
113 // owner_->getInset(i)->setChange(change);
120 void Paragraph::Pimpl::setChange(pos_type pos, Change const & 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::acceptChange(pos_type start, pos_type end)
144 changes_.reset(new Changes(Change::UNCHANGED));
148 lyxerr[Debug::CHANGES] << "acceptchange" << endl;
151 for (; i < end; ++i) {
152 switch (lookupChange(i).type) {
153 case Change::UNCHANGED:
156 case Change::INSERTED:
157 changes_->set(Change::UNCHANGED, i);
160 case Change::DELETED:
161 // Suppress access to nonexistent
162 // "end-of-paragraph char":
172 lyxerr[Debug::CHANGES] << "endacceptchange" << endl;
173 changes_->reset(Change::UNCHANGED);
177 void Paragraph::Pimpl::rejectChange(pos_type start, pos_type end)
183 changes_.reset(new Changes(Change::UNCHANGED));
189 for (; i < end; ++i) {
190 switch (lookupChange(i).type) {
191 case Change::UNCHANGED:
194 case Change::INSERTED:
202 case Change::DELETED:
203 changes_->set(Change::UNCHANGED, i);
204 // No real char at position size():
205 if (i < size() && owner_->isInset(i))
206 owner_->getInset(i)->markErased(false);
210 changes_->reset(Change::UNCHANGED);
214 Paragraph::value_type Paragraph::Pimpl::getChar(pos_type pos) const
216 return owner_->getChar(pos);
220 void Paragraph::Pimpl::insertChar(pos_type pos, value_type c, Change const & change)
222 BOOST_ASSERT(pos <= size());
225 changes_->record(change, pos);
228 // This is actually very common when parsing buffers (and
229 // maybe inserting ascii text)
231 // when appending characters, no need to update tables
232 owner_->text_.push_back(c);
236 owner_->text_.insert(owner_->text_.begin() + pos, c);
238 // Update the font table.
239 FontTable search_font(pos, LyXFont());
240 for (FontList::iterator it = lower_bound(fontlist.begin(),
242 search_font, matchFT());
243 it != fontlist.end(); ++it)
245 it->pos(it->pos() + 1);
249 owner_->insetlist.increasePosAfterPos(pos);
253 void Paragraph::Pimpl::insertInset(pos_type pos,
254 InsetBase * inset, Change const & change)
257 BOOST_ASSERT(pos <= size());
259 insertChar(pos, META_INSET, change);
260 BOOST_ASSERT(owner_->text_[pos] == META_INSET);
262 // Add a new entry in the insetlist.
263 owner_->insetlist.insert(inset, pos);
267 void Paragraph::Pimpl::erase(pos_type pos)
269 // FIXME: change tracking (MG)
270 // do something like changes_->erase(i);
271 // in one of the next patches, the two erase functions
272 // will be merged but I don't want to break too many things at the same time :-)
274 // if it is an inset, delete the inset entry
275 if (owner_->text_[pos] == Paragraph::META_INSET) {
276 owner_->insetlist.erase(pos);
279 owner_->text_.erase(owner_->text_.begin() + pos);
281 // Erase entries in the tables.
282 FontTable search_font(pos, LyXFont());
284 FontList::iterator it =
285 lower_bound(fontlist.begin(),
287 search_font, matchFT());
288 if (it != fontlist.end() && it->pos() == pos &&
290 (it != fontlist.begin()
291 && boost::prior(it)->pos() == pos - 1))) {
292 // If it is a multi-character font
293 // entry, we just make it smaller
294 // (see update below), otherwise we
296 unsigned int const i = it - fontlist.begin();
297 fontlist.erase(fontlist.begin() + i);
298 it = fontlist.begin() + i;
299 if (i > 0 && i < fontlist.size() &&
300 fontlist[i - 1].font() == fontlist[i].font()) {
301 fontlist.erase(fontlist.begin() + i - 1);
302 it = fontlist.begin() + i - 1;
306 // Update all other entries
307 FontList::iterator fend = fontlist.end();
308 for (; it != fend; ++it)
309 it->pos(it->pos() - 1);
311 // Update the insetlist
312 owner_->insetlist.decreasePosAfterPos(pos);
316 bool Paragraph::Pimpl::erase(pos_type pos, bool trackChanges)
318 // FIXME: change tracking (MG)
319 BOOST_ASSERT(pos <= size());
322 Change::Type changetype(changes_->lookup(pos).type);
324 // only allow the actual removal if it was /new/ text
325 if (changetype != Change::INSERTED) {
326 changes_->record(Change(Change::DELETED), pos);
327 if (pos < size() && owner_->isInset(pos))
328 owner_->getInset(pos)->markErased(true);
333 // Don't physically access nonexistent end-of-paragraph char
343 int Paragraph::Pimpl::erase(pos_type start, pos_type end, bool trackChanges)
346 for (pos_type count = end - start; count; --count) {
347 if (!erase(i, trackChanges))
354 void Paragraph::Pimpl::simpleTeXBlanks(odocstream & os, TexRow & texrow,
356 unsigned int & column,
357 LyXFont const & font,
358 LyXLayout const & style)
363 if (column > lyxrc.ascii_linelen
365 && getChar(i - 1) != ' '
367 // same in FreeSpacing mode
368 && !owner_->isFreeSpacing()
369 // In typewriter mode, we want to avoid
370 // ! . ? : at the end of a line
371 && !(font.family() == LyXFont::TYPEWRITER_FAMILY
372 && (getChar(i - 1) == '.'
373 || getChar(i - 1) == '?'
374 || getChar(i - 1) == ':'
375 || getChar(i - 1) == '!'))) {
378 texrow.start(owner_->id(), i + 1);
380 } else if (style.free_spacing) {
388 bool Paragraph::Pimpl::isTextAt(string const & str, pos_type pos) const
390 pos_type const len = str.length();
392 // is the paragraph large enough?
393 if (pos + len > size())
396 // does the wanted text start at point?
397 for (string::size_type i = 0; i < str.length(); ++i) {
398 if (str[i] != owner_->text_[pos + i])
402 // is there a font change in middle of the word?
403 FontList::const_iterator cit = fontlist.begin();
404 FontList::const_iterator end = fontlist.end();
405 for (; cit != end; ++cit) {
406 if (cit->pos() >= pos)
409 if (cit != end && pos + len - 1 > cit->pos())
416 void Paragraph::Pimpl::simpleTeXSpecialChars(Buffer const & buf,
417 BufferParams const & bparams,
420 OutputParams const & runparams,
422 LyXFont & running_font,
424 LyXFont const & outerfont,
426 Change::Type & running_change,
427 LyXLayout const & style,
429 unsigned int & column,
432 if (style.pass_thru) {
433 if (c != Paragraph::META_INSET) {
437 owner_->getInset(i)->plaintext(buf, os, runparams);
441 // Two major modes: LaTeX or plain
442 // Handle here those cases common to both modes
443 // and then split to handle the two modes separately.
445 case Paragraph::META_INSET: {
446 InsetBase * inset = owner_->getInset(i);
448 // FIXME: remove this check
452 // FIXME: move this to InsetNewline::latex
453 if (inset->lyxCode() == InsetBase::NEWLINE_CODE) {
454 // newlines are handled differently here than
455 // the default in simpleTeXSpecialChars().
456 if (!style.newline_allowed) {
460 column += running_font.latexWriteEndChanges(os, basefont, basefont);
463 basefont = owner_->getLayoutFont(bparams, outerfont);
464 running_font = basefont;
466 if (font.family() == LyXFont::TYPEWRITER_FAMILY)
469 if (runparams.moving_arg)
475 texrow.start(owner_->id(), i + 1);
480 // output change tracking marks only if desired,
481 // if dvipost is installed,
482 // and with dvi/ps (other formats don't work)
483 LaTeXFeatures features(buf, bparams, runparams);
484 bool const output = bparams.outputChanges
485 && runparams.flavor == OutputParams::LATEX
486 && features.isAvailable("dvipost");
488 if (inset->canTrackChanges()) {
489 column += Changes::latexMarkChange(os, running_change,
490 Change::UNCHANGED, output);
491 running_change = Change::UNCHANGED;
495 odocstream::pos_type const len = os.tellp();
497 if ((inset->lyxCode() == InsetBase::GRAPHICS_CODE
498 || inset->lyxCode() == InsetBase::MATH_CODE
499 || inset->lyxCode() == InsetBase::URL_CODE)
500 && running_font.isRightToLeft()) {
506 #warning Bug: we can have an empty font change here!
507 // if there has just been a font change, we are going to close it
508 // right now, which means stupid latex code like \textsf{}. AFAIK,
509 // this does not harm dvi output. A minor bug, thus (JMarc)
511 // some insets cannot be inside a font change command
512 if (open_font && inset->noFontChange()) {
513 column +=running_font.
514 latexWriteEndChanges(os,
518 basefont = owner_->getLayoutFont(bparams, outerfont);
519 running_font = basefont;
522 int tmp = inset->latex(buf, os, runparams);
528 for (int j = 0; j < tmp; ++j) {
531 texrow.start(owner_->id(), i + 1);
534 column += os.tellp() - len;
540 // And now for the special cases within each mode
544 os << "\\textbackslash{}";
548 case '±': case '²': case '³':
549 case '×': case '÷': case '¹':
551 if ((bparams.inputenc == "latin1" ||
552 bparams.inputenc == "latin9") ||
553 (bparams.inputenc == "auto" &&
554 (font.language()->encoding()->latexName()
556 font.language()->encoding()->latexName()
558 os << "\\ensuremath{"
567 case '|': case '<': case '>':
568 // In T1 encoding, these characters exist
569 if (lyxrc.fontenc == "T1") {
571 //... but we should avoid ligatures
572 if ((c == '>' || c == '<')
574 && getChar(i + 1) == c) {
575 //os << "\\textcompwordmark{}";
576 // Jean-Marc, have a look at
577 // this. I think this works
585 // Typewriter font also has them
586 if (font.family() == LyXFont::TYPEWRITER_FAMILY) {
590 // Otherwise, we use what LaTeX
594 os << "\\textless{}";
598 os << "\\textgreater{}";
608 case '-': // "--" in Typewriter mode -> "-{}-"
610 && getChar(i + 1) == '-'
611 && font.family() == LyXFont::TYPEWRITER_FAMILY) {
620 os << "\\char`\\\"{}";
625 if (bparams.inputenc == "default") {
634 case '%': case '#': case '{':
641 os << "\\textasciitilde{}";
646 os << "\\textasciicircum{}";
651 // avoid being mistaken for optional arguments
652 os << '{' << c << '}';
657 // Blanks are printed before font switching.
658 // Sure? I am not! (try nice-latex)
659 // I am sure it's correct. LyX might be smarter
660 // in the future, but for now, nothing wrong is
666 // I assume this is hack treating typewriter as verbatim
667 if (font.family() == LyXFont::TYPEWRITER_FAMILY) {
676 // FIXME: if we have "LaTeX" with a font
677 // change in the middle (before the 'T', then
678 // the "TeX" part is still special cased.
679 // Really we should only operate this on
680 // "words" for some definition of word
684 for (; pnr < phrases_nr; ++pnr) {
685 if (isTextAt(special_phrases[pnr].phrase, i)) {
686 os << special_phrases[pnr].macro;
687 i += special_phrases[pnr].phrase.length() - 1;
688 column += special_phrases[pnr].macro.length() - 1;
693 if (pnr == phrases_nr && c != '\0') {
702 void Paragraph::Pimpl::validate(LaTeXFeatures & features,
703 LyXLayout const & layout) const
705 BufferParams const & bparams = features.bufferParams();
708 if (!params.spacing().isDefault())
709 features.require("setspace");
712 features.useLayout(layout.name());
715 Language const * doc_language = bparams.language;
717 FontList::const_iterator fcit = fontlist.begin();
718 FontList::const_iterator fend = fontlist.end();
719 for (; fcit != fend; ++fcit) {
720 if (fcit->font().noun() == LyXFont::ON) {
721 lyxerr[Debug::LATEX] << "font.noun: "
722 << fcit->font().noun()
724 features.require("noun");
725 lyxerr[Debug::LATEX] << "Noun enabled. Font: "
726 << fcit->font().stateText(0)
729 switch (fcit->font().color()) {
731 case LColor::inherit:
733 // probably we should put here all interface colors used for
734 // font displaying! For now I just add this ones I know of (Jug)
739 features.require("color");
740 lyxerr[Debug::LATEX] << "Color enabled. Font: "
741 << fcit->font().stateText(0)
745 Language const * language = fcit->font().language();
746 if (language->babel() != doc_language->babel() &&
747 language != ignore_language &&
748 language != latex_language)
750 features.useLanguage(language);
751 lyxerr[Debug::LATEX] << "Found language "
752 << language->babel() << endl;
756 if (!params.leftIndent().zero())
757 features.require("ParagraphLeftIndent");
760 InsetList::const_iterator icit = owner_->insetlist.begin();
761 InsetList::const_iterator iend = owner_->insetlist.end();
762 for (; icit != iend; ++icit) {
764 icit->inset->validate(features);
765 if (layout.needprotect &&
766 icit->inset->lyxCode() == InsetBase::FOOT_CODE)
767 features.require("NeedLyXFootnoteCode");
772 for (pos_type i = 0; i < size() ; ++i) {
773 for (size_t pnr = 0; pnr < phrases_nr; ++pnr) {
774 if (!special_phrases[pnr].builtin
775 && isTextAt(special_phrases[pnr].phrase, i)) {
776 features.require(special_phrases[pnr].phrase);