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
95 // FIXME: change tracking (MG)
99 return changes_->isChange(start, end);
103 void Paragraph::Pimpl::setChange(Change const & change)
105 // FIXME: change tracking (MG)
106 // changes_.set(change, 0, size());
108 if (change.type == Change::UNCHANGED) { // only for UNCHANGED ???
109 for (pos_type i = 0; i < size(); ++i) {
110 if (owner_->isInset(i)) {
111 // FIXME: change tracking (MG)
112 // owner_->getInset(i)->setChange(change);
119 void Paragraph::Pimpl::setChange(pos_type pos, Change const & change)
124 changes_->set(change, pos);
128 Change const Paragraph::Pimpl::lookupChange(pos_type pos) const
131 return Change(Change::UNCHANGED);
133 return changes_->lookup(pos);
137 void Paragraph::Pimpl::acceptChange(pos_type start, pos_type end)
143 changes_.reset(new Changes(Change::UNCHANGED));
147 lyxerr[Debug::CHANGES] << "acceptchange" << endl;
150 for (; i < end; ++i) {
151 switch (lookupChange(i).type) {
152 case Change::UNCHANGED:
155 case Change::INSERTED:
156 changes_->set(Change::UNCHANGED, i);
159 case Change::DELETED:
160 // Suppress access to nonexistent
161 // "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:
203 case Change::DELETED:
204 changes_->set(Change::UNCHANGED, i);
205 // No real char at position size():
206 if (i < size() && owner_->isInset(i))
207 owner_->getInset(i)->markErased(false);
211 changes_->reset(Change::UNCHANGED);
215 Paragraph::value_type Paragraph::Pimpl::getChar(pos_type pos) const
217 return owner_->getChar(pos);
221 void Paragraph::Pimpl::insertChar(pos_type pos, value_type c, Change const & change)
223 BOOST_ASSERT(pos <= size());
226 changes_->record(change, pos);
229 // This is actually very common when parsing buffers (and
230 // maybe inserting ascii text)
232 // when appending characters, no need to update tables
233 owner_->text_.push_back(c);
237 owner_->text_.insert(owner_->text_.begin() + pos, c);
239 // Update the font table.
240 FontTable search_font(pos, LyXFont());
241 for (FontList::iterator it = lower_bound(fontlist.begin(),
243 search_font, matchFT());
244 it != fontlist.end(); ++it)
246 it->pos(it->pos() + 1);
250 owner_->insetlist.increasePosAfterPos(pos);
254 void Paragraph::Pimpl::insertInset(pos_type pos,
255 InsetBase * inset, Change const & change)
258 BOOST_ASSERT(pos <= size());
260 insertChar(pos, META_INSET, change);
261 BOOST_ASSERT(owner_->text_[pos] == META_INSET);
263 // Add a new entry in the insetlist.
264 owner_->insetlist.insert(inset, pos);
268 void Paragraph::Pimpl::eraseIntern(pos_type pos)
270 // if it is an inset, delete the inset entry
271 if (owner_->text_[pos] == Paragraph::META_INSET) {
272 owner_->insetlist.erase(pos);
275 owner_->text_.erase(owner_->text_.begin() + pos);
277 // Erase entries in the tables.
278 FontTable search_font(pos, LyXFont());
280 FontList::iterator it =
281 lower_bound(fontlist.begin(),
283 search_font, matchFT());
284 if (it != fontlist.end() && it->pos() == pos &&
286 (it != fontlist.begin()
287 && boost::prior(it)->pos() == pos - 1))) {
288 // If it is a multi-character font
289 // entry, we just make it smaller
290 // (see update below), otherwise we
292 unsigned int const i = it - fontlist.begin();
293 fontlist.erase(fontlist.begin() + i);
294 it = fontlist.begin() + i;
295 if (i > 0 && i < fontlist.size() &&
296 fontlist[i - 1].font() == fontlist[i].font()) {
297 fontlist.erase(fontlist.begin() + i - 1);
298 it = fontlist.begin() + i - 1;
302 // Update all other entries.
303 FontList::iterator fend = fontlist.end();
304 for (; it != fend; ++it)
305 it->pos(it->pos() - 1);
307 // Update the insetlist.
308 owner_->insetlist.decreasePosAfterPos(pos);
312 bool Paragraph::Pimpl::erase(pos_type pos)
314 BOOST_ASSERT(pos <= size());
317 Change::Type changetype(changes_->lookup(pos).type);
319 // only allow the actual removal if it was /new/ text
320 if (changetype != Change::INSERTED) {
321 changes_->record(Change(Change::DELETED), pos);
322 if (pos < size() && owner_->isInset(pos))
323 owner_->getInset(pos)->markErased(true);
328 // Don't physically access nonexistent end-of-paragraph char
338 int Paragraph::Pimpl::erase(pos_type start, pos_type end)
341 for (pos_type count = end - start; count; --count) {
349 void Paragraph::Pimpl::simpleTeXBlanks(ostream & os, TexRow & texrow,
351 unsigned int & column,
352 LyXFont const & font,
353 LyXLayout const & style)
358 if (column > lyxrc.ascii_linelen
360 && getChar(i - 1) != ' '
362 // same in FreeSpacing mode
363 && !owner_->isFreeSpacing()
364 // In typewriter mode, we want to avoid
365 // ! . ? : at the end of a line
366 && !(font.family() == LyXFont::TYPEWRITER_FAMILY
367 && (getChar(i - 1) == '.'
368 || getChar(i - 1) == '?'
369 || getChar(i - 1) == ':'
370 || getChar(i - 1) == '!'))) {
373 texrow.start(owner_->id(), i + 1);
375 } else if (style.free_spacing) {
383 bool Paragraph::Pimpl::isTextAt(string const & str, pos_type pos) const
385 pos_type const len = str.length();
387 // is the paragraph large enough?
388 if (pos + len > size())
391 // does the wanted text start at point?
392 for (string::size_type i = 0; i < str.length(); ++i) {
393 if (str[i] != owner_->text_[pos + i])
397 // is there a font change in middle of the word?
398 FontList::const_iterator cit = fontlist.begin();
399 FontList::const_iterator end = fontlist.end();
400 for (; cit != end; ++cit) {
401 if (cit->pos() >= pos)
404 if (cit != end && pos + len - 1 > cit->pos())
411 void Paragraph::Pimpl::simpleTeXSpecialChars(Buffer const & buf,
412 BufferParams const & bparams,
415 OutputParams const & runparams,
417 LyXFont & running_font,
419 LyXFont const & outerfont,
421 Change::Type & running_change,
422 LyXLayout const & style,
424 unsigned int & column,
427 if (style.pass_thru) {
428 if (c != Paragraph::META_INSET) {
432 lyx::odocstringstream oss;
433 owner_->getInset(i)->plaintext(buf, oss, runparams);
435 os << lyx::to_utf8(oss.str());
440 // Two major modes: LaTeX or plain
441 // Handle here those cases common to both modes
442 // and then split to handle the two modes separately.
444 case Paragraph::META_INSET: {
445 InsetBase * inset = owner_->getInset(i);
447 // FIXME: remove this check
451 // FIXME: move this to InsetNewline::latex
452 if (inset->lyxCode() == InsetBase::NEWLINE_CODE) {
453 // newlines are handled differently here than
454 // the default in simpleTeXSpecialChars().
455 if (!style.newline_allowed) {
459 column += running_font.latexWriteEndChanges(os, basefont, basefont);
462 basefont = owner_->getLayoutFont(bparams, outerfont);
463 running_font = basefont;
465 if (font.family() == LyXFont::TYPEWRITER_FAMILY)
468 if (runparams.moving_arg)
474 texrow.start(owner_->id(), i + 1);
479 // output change tracking marks only if desired,
480 // if dvipost is installed,
481 // and with dvi/ps (other formats don't work)
482 LaTeXFeatures features(buf, bparams, runparams);
483 bool const output = bparams.outputChanges
484 && runparams.flavor == OutputParams::LATEX
485 && features.isAvailable("dvipost");
487 if (inset->canTrackChanges()) {
488 column += Changes::latexMarkChange(os, running_change,
489 Change::UNCHANGED, output);
490 running_change = Change::UNCHANGED;
494 ostream::pos_type const len = os.tellp();
496 if ((inset->lyxCode() == InsetBase::GRAPHICS_CODE
497 || inset->lyxCode() == InsetBase::MATH_CODE
498 || inset->lyxCode() == InsetBase::URL_CODE)
499 && running_font.isRightToLeft()) {
505 #warning Bug: we can have an empty font change here!
506 // if there has just been a font change, we are going to close it
507 // right now, which means stupid latex code like \textsf{}. AFAIK,
508 // this does not harm dvi output. A minor bug, thus (JMarc)
510 // some insets cannot be inside a font change command
511 if (open_font && inset->noFontChange()) {
512 column +=running_font.
513 latexWriteEndChanges(os,
517 basefont = owner_->getLayoutFont(bparams, outerfont);
518 running_font = basefont;
521 int tmp = inset->latex(buf, os, runparams);
527 for (int j = 0; j < tmp; ++j) {
530 texrow.start(owner_->id(), i + 1);
533 column += os.tellp() - len;
539 // And now for the special cases within each mode
543 os << "\\textbackslash{}";
547 case '±': case '²': case '³':
548 case '×': case '÷': case '¹':
550 if ((bparams.inputenc == "latin1" ||
551 bparams.inputenc == "latin9") ||
552 (bparams.inputenc == "auto" &&
553 (font.language()->encoding()->latexName()
555 font.language()->encoding()->latexName()
557 os << "\\ensuremath{"
566 case '|': case '<': case '>':
567 // In T1 encoding, these characters exist
568 if (lyxrc.fontenc == "T1") {
570 //... but we should avoid ligatures
571 if ((c == '>' || c == '<')
573 && getChar(i + 1) == c) {
574 //os << "\\textcompwordmark{}";
575 // Jean-Marc, have a look at
576 // this. I think this works
584 // Typewriter font also has them
585 if (font.family() == LyXFont::TYPEWRITER_FAMILY) {
589 // Otherwise, we use what LaTeX
593 os << "\\textless{}";
597 os << "\\textgreater{}";
607 case '-': // "--" in Typewriter mode -> "-{}-"
609 && getChar(i + 1) == '-'
610 && font.family() == LyXFont::TYPEWRITER_FAMILY) {
619 os << "\\char`\\\"{}";
624 if (bparams.inputenc == "default") {
633 case '%': case '#': case '{':
640 os << "\\textasciitilde{}";
645 os << "\\textasciicircum{}";
650 // avoid being mistaken for optional arguments
651 os << '{' << c << '}';
656 // Blanks are printed before font switching.
657 // Sure? I am not! (try nice-latex)
658 // I am sure it's correct. LyX might be smarter
659 // in the future, but for now, nothing wrong is
665 // I assume this is hack treating typewriter as verbatim
666 if (font.family() == LyXFont::TYPEWRITER_FAMILY) {
675 // FIXME: if we have "LaTeX" with a font
676 // change in the middle (before the 'T', then
677 // the "TeX" part is still special cased.
678 // Really we should only operate this on
679 // "words" for some definition of word
683 for (; pnr < phrases_nr; ++pnr) {
684 if (isTextAt(special_phrases[pnr].phrase, i)) {
685 os << special_phrases[pnr].macro;
686 i += special_phrases[pnr].phrase.length() - 1;
687 column += special_phrases[pnr].macro.length() - 1;
692 if (pnr == phrases_nr && c != '\0') {
701 void Paragraph::Pimpl::validate(LaTeXFeatures & features,
702 LyXLayout const & layout) const
704 BufferParams const & bparams = features.bufferParams();
707 if (!params.spacing().isDefault())
708 features.require("setspace");
711 features.useLayout(layout.name());
714 Language const * doc_language = bparams.language;
716 FontList::const_iterator fcit = fontlist.begin();
717 FontList::const_iterator fend = fontlist.end();
718 for (; fcit != fend; ++fcit) {
719 if (fcit->font().noun() == LyXFont::ON) {
720 lyxerr[Debug::LATEX] << "font.noun: "
721 << fcit->font().noun()
723 features.require("noun");
724 lyxerr[Debug::LATEX] << "Noun enabled. Font: "
725 << fcit->font().stateText(0)
728 switch (fcit->font().color()) {
730 case LColor::inherit:
732 // probably we should put here all interface colors used for
733 // font displaying! For now I just add this ones I know of (Jug)
738 features.require("color");
739 lyxerr[Debug::LATEX] << "Color enabled. Font: "
740 << fcit->font().stateText(0)
744 Language const * language = fcit->font().language();
745 if (language->babel() != doc_language->babel() &&
746 language != ignore_language &&
747 language != latex_language)
749 features.useLanguage(language);
750 lyxerr[Debug::LATEX] << "Found language "
751 << language->babel() << endl;
755 if (!params.leftIndent().zero())
756 features.require("ParagraphLeftIndent");
759 InsetList::const_iterator icit = owner_->insetlist.begin();
760 InsetList::const_iterator iend = owner_->insetlist.end();
761 for (; icit != iend; ++icit) {
763 icit->inset->validate(features);
764 if (layout.needprotect &&
765 icit->inset->lyxCode() == InsetBase::FOOT_CODE)
766 features.require("NeedLyXFootnoteCode");
771 for (pos_type i = 0; i < size() ; ++i) {
772 for (size_t pnr = 0; pnr < phrases_nr; ++pnr) {
773 if (!special_phrases[pnr].builtin
774 && isTextAt(special_phrases[pnr].phrase, i)) {
775 features.require(special_phrases[pnr].phrase);