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>
36 using std::upper_bound;
37 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 const special_phrases[] = {
53 { "LyX", from_ascii("\\LyX{}"), false },
54 { "TeX", from_ascii("\\TeX{}"), true },
55 { "LaTeX2e", from_ascii("\\LaTeXe{}"), true },
56 { "LaTeX", from_ascii("\\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;
79 // FIXME: change tracking (MG)
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 // FIXME: change tracking (MG)
90 // changes_.reset(new Changes(*(par.pimpl_->changes_.get())));
95 bool Paragraph::Pimpl::isChanged(pos_type start, pos_type end) const
97 // FIXME: change tracking (MG)
101 return changes_->isChanged(start, end);
105 void Paragraph::Pimpl::setChange(Change const & change)
107 // FIXME: change tracking (MG)
108 // changes_.set(change, 0, size());
110 if (change.type == Change::UNCHANGED) { // only for UNCHANGED ???
111 for (pos_type i = 0; i < size(); ++i) {
112 if (owner_->isInset(i)) {
113 // FIXME: change tracking (MG)
114 // owner_->getInset(i)->setChange(change);
121 void Paragraph::Pimpl::setChange(pos_type pos, Change const & change)
126 changes_->set(change, pos);
130 Change const Paragraph::Pimpl::lookupChange(pos_type pos) const
133 return Change(Change::UNCHANGED);
135 return changes_->lookup(pos);
139 void Paragraph::Pimpl::acceptChange(pos_type start, pos_type end)
145 // FIXME: change tracking (MG)
146 // changes_.reset(new Changes(Change::UNCHANGED));
150 lyxerr[Debug::CHANGES] << "acceptchange" << endl;
153 for (; i < end; ++i) {
154 switch (lookupChange(i).type) {
155 case Change::UNCHANGED:
158 case Change::INSERTED:
159 // FIXME: change tracking (MG)
160 changes_->set(Change(Change::UNCHANGED), i);
163 case Change::DELETED:
164 // Suppress access to nonexistent
165 // "end-of-paragraph char":
175 lyxerr[Debug::CHANGES] << "endacceptchange" << endl;
176 // FIXME: change tracking (MG)
177 // changes_->reset(Change::UNCHANGED);
181 void Paragraph::Pimpl::rejectChange(pos_type start, pos_type end)
187 // FIXME: change tracking (MG)
188 // changes_.reset(new Changes(Change::UNCHANGED));
194 for (; i < end; ++i) {
195 switch (lookupChange(i).type) {
196 case Change::UNCHANGED:
199 case Change::INSERTED:
207 case Change::DELETED:
208 // FIXME: change tracking (MG)
209 changes_->set(Change(Change::UNCHANGED), i);
210 // No real char at position size():
211 if (i < size() && owner_->isInset(i))
212 // FIXME: change tracking (MG)
213 owner_->getInset(i)->setChange(Change(Change::UNCHANGED));
217 // FIXME: change tracking (MG)
218 // changes_->reset(Change::UNCHANGED);
222 Paragraph::value_type Paragraph::Pimpl::getChar(pos_type pos) const
224 return owner_->getChar(pos);
228 void Paragraph::Pimpl::insertChar(pos_type pos, value_type c, Change const & change)
230 BOOST_ASSERT(pos <= size());
233 changes_->record(change, pos);
236 // This is actually very common when parsing buffers (and
237 // maybe inserting ascii text)
239 // when appending characters, no need to update tables
240 owner_->text_.push_back(c);
244 owner_->text_.insert(owner_->text_.begin() + pos, c);
246 // Update the font table.
247 FontTable search_font(pos, LyXFont());
248 for (FontList::iterator it = lower_bound(fontlist.begin(),
250 search_font, matchFT());
251 it != fontlist.end(); ++it)
253 it->pos(it->pos() + 1);
257 owner_->insetlist.increasePosAfterPos(pos);
261 void Paragraph::Pimpl::insertInset(pos_type pos,
262 InsetBase * inset, Change const & change)
265 BOOST_ASSERT(pos <= size());
267 insertChar(pos, META_INSET, change);
268 BOOST_ASSERT(owner_->text_[pos] == META_INSET);
270 // Add a new entry in the insetlist.
271 owner_->insetlist.insert(inset, pos);
275 void Paragraph::Pimpl::erase(pos_type pos)
277 // FIXME: change tracking (MG)
278 // do something like changes_->erase(i);
279 // in one of the next patches, the two erase functions
280 // will be merged but I don't want to break too many things at the same time :-)
282 // if it is an inset, delete the inset entry
283 if (owner_->text_[pos] == Paragraph::META_INSET) {
284 owner_->insetlist.erase(pos);
287 owner_->text_.erase(owner_->text_.begin() + pos);
289 // Erase entries in the tables.
290 FontTable search_font(pos, LyXFont());
292 FontList::iterator it =
293 lower_bound(fontlist.begin(),
295 search_font, matchFT());
296 if (it != fontlist.end() && it->pos() == pos &&
298 (it != fontlist.begin()
299 && boost::prior(it)->pos() == pos - 1))) {
300 // If it is a multi-character font
301 // entry, we just make it smaller
302 // (see update below), otherwise we
304 unsigned int const i = it - fontlist.begin();
305 fontlist.erase(fontlist.begin() + i);
306 it = fontlist.begin() + i;
307 if (i > 0 && i < fontlist.size() &&
308 fontlist[i - 1].font() == fontlist[i].font()) {
309 fontlist.erase(fontlist.begin() + i - 1);
310 it = fontlist.begin() + i - 1;
314 // Update all other entries
315 FontList::iterator fend = fontlist.end();
316 for (; it != fend; ++it)
317 it->pos(it->pos() - 1);
319 // Update the insetlist
320 owner_->insetlist.decreasePosAfterPos(pos);
324 bool Paragraph::Pimpl::erase(pos_type pos, bool /*trackChanges*/)
326 // FIXME: change tracking (MG)
327 BOOST_ASSERT(pos <= size());
330 Change::Type changetype(changes_->lookup(pos).type);
332 // only allow the actual removal if it was /new/ text
333 if (changetype != Change::INSERTED) {
334 changes_->record(Change(Change::DELETED), pos);
335 if (pos < size() && owner_->isInset(pos))
336 // FIXME: change tracking (MG)
337 owner_->getInset(pos)->setChange(Change(Change::DELETED));
342 // Don't physically access nonexistent end-of-paragraph char
352 int Paragraph::Pimpl::erase(pos_type start, pos_type end, bool trackChanges)
355 for (pos_type count = end - start; count; --count) {
356 if (!erase(i, trackChanges))
363 void Paragraph::Pimpl::simpleTeXBlanks(odocstream & os, TexRow & texrow,
365 unsigned int & column,
366 LyXFont const & font,
367 LyXLayout const & style)
372 if (column > lyxrc.ascii_linelen
374 && getChar(i - 1) != ' '
376 // same in FreeSpacing mode
377 && !owner_->isFreeSpacing()
378 // In typewriter mode, we want to avoid
379 // ! . ? : at the end of a line
380 && !(font.family() == LyXFont::TYPEWRITER_FAMILY
381 && (getChar(i - 1) == '.'
382 || getChar(i - 1) == '?'
383 || getChar(i - 1) == ':'
384 || getChar(i - 1) == '!'))) {
387 texrow.start(owner_->id(), i + 1);
389 } else if (style.free_spacing) {
397 bool Paragraph::Pimpl::isTextAt(string const & str, pos_type pos) const
399 pos_type const len = str.length();
401 // is the paragraph large enough?
402 if (pos + len > size())
405 // does the wanted text start at point?
406 for (string::size_type i = 0; i < str.length(); ++i) {
407 if (str[i] != owner_->text_[pos + i])
411 // is there a font change in middle of the word?
412 FontList::const_iterator cit = fontlist.begin();
413 FontList::const_iterator end = fontlist.end();
414 for (; cit != end; ++cit) {
415 if (cit->pos() >= pos)
418 if (cit != end && pos + len - 1 > cit->pos())
425 void Paragraph::Pimpl::simpleTeXSpecialChars(Buffer const & buf,
426 BufferParams const & bparams,
429 OutputParams const & runparams,
431 LyXFont & running_font,
433 LyXFont const & outerfont,
435 Change::Type & running_change,
436 LyXLayout const & style,
438 unsigned int & column,
441 if (style.pass_thru) {
442 if (c != Paragraph::META_INSET) {
446 owner_->getInset(i)->plaintext(buf, os, runparams);
450 // Two major modes: LaTeX or plain
451 // Handle here those cases common to both modes
452 // and then split to handle the two modes separately.
454 case Paragraph::META_INSET: {
455 InsetBase * inset = owner_->getInset(i);
457 // FIXME: remove this check
461 // FIXME: move this to InsetNewline::latex
462 if (inset->lyxCode() == InsetBase::NEWLINE_CODE) {
463 // newlines are handled differently here than
464 // the default in simpleTeXSpecialChars().
465 if (!style.newline_allowed) {
469 column += running_font.latexWriteEndChanges(os, basefont, basefont);
472 basefont = owner_->getLayoutFont(bparams, outerfont);
473 running_font = basefont;
475 if (font.family() == LyXFont::TYPEWRITER_FAMILY)
478 if (runparams.moving_arg)
484 texrow.start(owner_->id(), i + 1);
489 // output change tracking marks only if desired,
490 // if dvipost is installed,
491 // and with dvi/ps (other formats don't work)
492 LaTeXFeatures features(buf, bparams, runparams);
493 bool const output = bparams.outputChanges
494 && runparams.flavor == OutputParams::LATEX
495 && features.isAvailable("dvipost");
497 if (inset->canTrackChanges()) {
498 column += Changes::latexMarkChange(os, running_change,
499 Change::UNCHANGED, output);
500 running_change = Change::UNCHANGED;
504 odocstream::pos_type const len = os.tellp();
506 if ((inset->lyxCode() == InsetBase::GRAPHICS_CODE
507 || inset->lyxCode() == InsetBase::MATH_CODE
508 || inset->lyxCode() == InsetBase::URL_CODE)
509 && running_font.isRightToLeft()) {
515 #warning Bug: we can have an empty font change here!
516 // if there has just been a font change, we are going to close it
517 // right now, which means stupid latex code like \textsf{}. AFAIK,
518 // this does not harm dvi output. A minor bug, thus (JMarc)
520 // some insets cannot be inside a font change command
521 if (open_font && inset->noFontChange()) {
522 column +=running_font.
523 latexWriteEndChanges(os,
527 basefont = owner_->getLayoutFont(bparams, outerfont);
528 running_font = basefont;
531 int tmp = inset->latex(buf, os, runparams);
537 for (int j = 0; j < tmp; ++j) {
540 texrow.start(owner_->id(), i + 1);
543 column += os.tellp() - len;
549 // And now for the special cases within each mode
553 os << "\\textbackslash{}";
557 case '±': case '²': case '³':
558 case '×': case '÷': case '¹':
560 if ((bparams.inputenc == "latin1" ||
561 bparams.inputenc == "latin9") ||
562 (bparams.inputenc == "auto" &&
563 (font.language()->encoding()->latexName()
565 font.language()->encoding()->latexName()
567 os << "\\ensuremath{";
576 case '|': case '<': case '>':
577 // In T1 encoding, these characters exist
578 if (lyxrc.fontenc == "T1") {
580 //... but we should avoid ligatures
581 if ((c == '>' || c == '<')
583 && getChar(i + 1) == c) {
584 //os << "\\textcompwordmark{}";
585 // Jean-Marc, have a look at
586 // this. I think this works
594 // Typewriter font also has them
595 if (font.family() == LyXFont::TYPEWRITER_FAMILY) {
599 // Otherwise, we use what LaTeX
603 os << "\\textless{}";
607 os << "\\textgreater{}";
617 case '-': // "--" in Typewriter mode -> "-{}-"
619 && getChar(i + 1) == '-'
620 && font.family() == LyXFont::TYPEWRITER_FAMILY) {
629 os << "\\char`\\\"{}";
634 if (bparams.inputenc == "default") {
643 case '%': case '#': case '{':
651 os << "\\textasciitilde{}";
656 os << "\\textasciicircum{}";
661 // avoid being mistaken for optional arguments
669 // Blanks are printed before font switching.
670 // Sure? I am not! (try nice-latex)
671 // I am sure it's correct. LyX might be smarter
672 // in the future, but for now, nothing wrong is
678 // I assume this is hack treating typewriter as verbatim
679 if (font.family() == LyXFont::TYPEWRITER_FAMILY) {
688 // FIXME: if we have "LaTeX" with a font
689 // change in the middle (before the 'T', then
690 // the "TeX" part is still special cased.
691 // Really we should only operate this on
692 // "words" for some definition of word
696 for (; pnr < phrases_nr; ++pnr) {
697 if (isTextAt(special_phrases[pnr].phrase, i)) {
698 os << special_phrases[pnr].macro;
699 i += special_phrases[pnr].phrase.length() - 1;
700 column += special_phrases[pnr].macro.length() - 1;
705 if (pnr == phrases_nr && c != '\0') {
714 void Paragraph::Pimpl::validate(LaTeXFeatures & features,
715 LyXLayout const & layout) const
717 BufferParams const & bparams = features.bufferParams();
720 if (!params.spacing().isDefault())
721 features.require("setspace");
724 features.useLayout(layout.name());
727 Language const * doc_language = bparams.language;
729 FontList::const_iterator fcit = fontlist.begin();
730 FontList::const_iterator fend = fontlist.end();
731 for (; fcit != fend; ++fcit) {
732 if (fcit->font().noun() == LyXFont::ON) {
733 lyxerr[Debug::LATEX] << "font.noun: "
734 << fcit->font().noun()
736 features.require("noun");
737 lyxerr[Debug::LATEX] << "Noun enabled. Font: "
738 << fcit->font().stateText(0)
741 switch (fcit->font().color()) {
743 case LColor::inherit:
745 // probably we should put here all interface colors used for
746 // font displaying! For now I just add this ones I know of (Jug)
751 features.require("color");
752 lyxerr[Debug::LATEX] << "Color enabled. Font: "
753 << fcit->font().stateText(0)
757 Language const * language = fcit->font().language();
758 if (language->babel() != doc_language->babel() &&
759 language != ignore_language &&
760 language != latex_language)
762 features.useLanguage(language);
763 lyxerr[Debug::LATEX] << "Found language "
764 << language->babel() << endl;
768 if (!params.leftIndent().zero())
769 features.require("ParagraphLeftIndent");
772 InsetList::const_iterator icit = owner_->insetlist.begin();
773 InsetList::const_iterator iend = owner_->insetlist.end();
774 for (; icit != iend; ++icit) {
776 icit->inset->validate(features);
777 if (layout.needprotect &&
778 icit->inset->lyxCode() == InsetBase::FOOT_CODE)
779 features.require("NeedLyXFootnoteCode");
784 for (pos_type i = 0; i < size() ; ++i) {
785 for (size_t pnr = 0; pnr < phrases_nr; ++pnr) {
786 if (!special_phrases[pnr].builtin
787 && isTextAt(special_phrases[pnr].phrase, i)) {
788 features.require(special_phrases[pnr].phrase);