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), changes_(p.changes_), owner_(owner)
75 inset_owner = p.inset_owner;
76 fontlist = p.fontlist;
81 void Paragraph::Pimpl::setContentsFromPar(Paragraph const & par)
83 owner_->text_ = par.text_;
84 // FIXME: change tracking (MG)
85 // check whether this method is really needed
86 changes_ = par.pimpl_->changes_;
90 bool Paragraph::Pimpl::isChanged(pos_type start, pos_type end) const
92 return changes_.isChanged(start, end);
96 void Paragraph::Pimpl::setChange(Change const & change)
98 // FIXME: change tracking (MG)
99 // how about end-of-line? size()+1?
100 changes_.set(change, 0, size());
102 if (change.type == Change::UNCHANGED) { // only for UNCHANGED ???
103 for (pos_type i = 0; i < size(); ++i) {
104 if (owner_->isInset(i)) {
105 owner_->getInset(i)->setChange(change);
112 void Paragraph::Pimpl::setChange(pos_type pos, Change const & change)
114 changes_.set(change, pos);
116 // FIXME: change tracking (MG)
117 // do we have to set the change recursively?
118 if (pos < size() && owner_->isInset(pos)) {
119 owner_->getInset(pos)->setChange(change);
124 Change const Paragraph::Pimpl::lookupChange(pos_type pos) const
126 return changes_.lookup(pos);
130 void Paragraph::Pimpl::acceptChange(pos_type start, pos_type end)
132 // FIXME: change tracking (MG)
135 // care for empty pars
137 lyxerr[Debug::CHANGES] << "acceptchange" << endl;
140 for (; i < end; ++i) {
141 switch (lookupChange(i).type) {
142 case Change::UNCHANGED:
145 case Change::INSERTED:
146 // FIXME: change tracking (MG)
147 changes_.set(Change(Change::UNCHANGED), i);
150 case Change::DELETED:
151 // Suppress access to nonexistent
152 // "end-of-paragraph char":
162 lyxerr[Debug::CHANGES] << "endacceptchange" << endl;
163 // FIXME: change tracking (MG)
164 // changes_.reset(Change::UNCHANGED);
168 void Paragraph::Pimpl::rejectChange(pos_type start, pos_type end)
170 // FIXME: change tracking (MG)
173 // care for empty pars
177 for (; i < end; ++i) {
178 switch (lookupChange(i).type) {
179 case Change::UNCHANGED:
182 case Change::INSERTED:
190 case Change::DELETED:
191 // FIXME: change tracking (MG)
192 changes_.set(Change(Change::UNCHANGED), i);
193 // No real char at position size():
194 if (i < size() && owner_->isInset(i))
195 // FIXME: change tracking (MG)
196 owner_->getInset(i)->setChange(Change(Change::UNCHANGED));
200 // FIXME: change tracking (MG)
201 // changes_.reset(Change::UNCHANGED);
205 Paragraph::value_type Paragraph::Pimpl::getChar(pos_type pos) const
207 return owner_->getChar(pos);
211 void Paragraph::Pimpl::insertChar(pos_type pos, value_type c, Change const & change)
213 BOOST_ASSERT(pos <= size());
216 changes_.insert(change, pos);
218 // This is actually very common when parsing buffers (and
219 // maybe inserting ascii text)
221 // when appending characters, no need to update tables
222 owner_->text_.push_back(c);
226 owner_->text_.insert(owner_->text_.begin() + pos, c);
228 // Update the font table.
229 FontTable search_font(pos, LyXFont());
230 for (FontList::iterator it
231 = lower_bound(fontlist.begin(), fontlist.end(), search_font, matchFT());
232 it != fontlist.end(); ++it)
234 it->pos(it->pos() + 1);
238 owner_->insetlist.increasePosAfterPos(pos);
242 void Paragraph::Pimpl::insertInset(pos_type pos, InsetBase * inset,
243 Change const & change)
246 BOOST_ASSERT(pos <= size());
248 insertChar(pos, META_INSET, change);
249 BOOST_ASSERT(owner_->text_[pos] == META_INSET);
251 // Add a new entry in the insetlist.
252 owner_->insetlist.insert(inset, pos);
256 bool Paragraph::Pimpl::eraseChar(pos_type pos, bool trackChanges)
258 BOOST_ASSERT(pos <= size());
261 Change::Type changetype(changes_.lookup(pos).type);
263 if (changetype == Change::UNCHANGED) {
264 setChange(pos, Change(Change::DELETED));
268 if (changetype == Change::DELETED)
272 // Don't physically access nonexistent end-of-paragraph char
274 // FIXME: change tracking (MG)
275 // how do we handle end-of-pars previously marked inserted?
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);
326 int Paragraph::Pimpl::eraseChars(pos_type start, pos_type end, bool trackChanges)
329 for (pos_type count = end - start; count; --count) {
330 if (!eraseChar(i, trackChanges))
337 void Paragraph::Pimpl::simpleTeXBlanks(odocstream & os, TexRow & texrow,
339 unsigned int & column,
340 LyXFont const & font,
341 LyXLayout const & style)
346 if (column > lyxrc.ascii_linelen
348 && getChar(i - 1) != ' '
350 // same in FreeSpacing mode
351 && !owner_->isFreeSpacing()
352 // In typewriter mode, we want to avoid
353 // ! . ? : at the end of a line
354 && !(font.family() == LyXFont::TYPEWRITER_FAMILY
355 && (getChar(i - 1) == '.'
356 || getChar(i - 1) == '?'
357 || getChar(i - 1) == ':'
358 || getChar(i - 1) == '!'))) {
361 texrow.start(owner_->id(), i + 1);
363 } else if (style.free_spacing) {
371 bool Paragraph::Pimpl::isTextAt(string const & str, pos_type pos) const
373 pos_type const len = str.length();
375 // is the paragraph large enough?
376 if (pos + len > size())
379 // does the wanted text start at point?
380 for (string::size_type i = 0; i < str.length(); ++i) {
381 if (str[i] != owner_->text_[pos + i])
385 // is there a font change in middle of the word?
386 FontList::const_iterator cit = fontlist.begin();
387 FontList::const_iterator end = fontlist.end();
388 for (; cit != end; ++cit) {
389 if (cit->pos() >= pos)
392 if (cit != end && pos + len - 1 > cit->pos())
399 void Paragraph::Pimpl::simpleTeXSpecialChars(Buffer const & buf,
400 BufferParams const & bparams,
403 OutputParams const & runparams,
405 LyXFont & running_font,
407 LyXFont const & outerfont,
409 Change::Type & running_change,
410 LyXLayout const & style,
412 unsigned int & column,
415 if (style.pass_thru) {
416 if (c != Paragraph::META_INSET) {
420 owner_->getInset(i)->plaintext(buf, os, runparams);
424 // Two major modes: LaTeX or plain
425 // Handle here those cases common to both modes
426 // and then split to handle the two modes separately.
428 case Paragraph::META_INSET: {
429 InsetBase * inset = owner_->getInset(i);
431 // FIXME: remove this check
435 // FIXME: move this to InsetNewline::latex
436 if (inset->lyxCode() == InsetBase::NEWLINE_CODE) {
437 // newlines are handled differently here than
438 // the default in simpleTeXSpecialChars().
439 if (!style.newline_allowed) {
443 column += running_font.latexWriteEndChanges(os, basefont, basefont);
446 basefont = owner_->getLayoutFont(bparams, outerfont);
447 running_font = basefont;
449 if (font.family() == LyXFont::TYPEWRITER_FAMILY)
452 if (runparams.moving_arg)
458 texrow.start(owner_->id(), i + 1);
463 // output change tracking marks only if desired,
464 // if dvipost is installed,
465 // and with dvi/ps (other formats don't work)
466 LaTeXFeatures features(buf, bparams, runparams);
467 bool const output = bparams.outputChanges
468 && runparams.flavor == OutputParams::LATEX
469 && features.isAvailable("dvipost");
471 if (inset->canTrackChanges()) {
472 column += Changes::latexMarkChange(os, running_change,
473 Change::UNCHANGED, output);
474 running_change = Change::UNCHANGED;
478 odocstream::pos_type const len = os.tellp();
480 if ((inset->lyxCode() == InsetBase::GRAPHICS_CODE
481 || inset->lyxCode() == InsetBase::MATH_CODE
482 || inset->lyxCode() == InsetBase::URL_CODE)
483 && running_font.isRightToLeft()) {
489 #warning Bug: we can have an empty font change here!
490 // if there has just been a font change, we are going to close it
491 // right now, which means stupid latex code like \textsf{}. AFAIK,
492 // this does not harm dvi output. A minor bug, thus (JMarc)
494 // some insets cannot be inside a font change command
495 if (open_font && inset->noFontChange()) {
496 column +=running_font.
497 latexWriteEndChanges(os,
501 basefont = owner_->getLayoutFont(bparams, outerfont);
502 running_font = basefont;
505 int tmp = inset->latex(buf, os, runparams);
511 for (int j = 0; j < tmp; ++j) {
514 texrow.start(owner_->id(), i + 1);
517 column += os.tellp() - len;
523 // And now for the special cases within each mode
527 os << "\\textbackslash{}";
531 // The following characters could be written literally in latin1, but they
532 // would be wrongly converted on systems where char is signed, so we give
534 // This also makes us independant from the encoding of this source file.
535 case 0xb1: // ± PLUS-MINUS SIGN
536 case 0xb2: // ² SUPERSCRIPT TWO
537 case 0xb3: // ³ SUPERSCRIPT THREE
538 case 0xd7: // × MULTIPLICATION SIGN
539 case 0xf7: // ÷ DIVISION SIGN
540 case 0xb9: // ¹ SUPERSCRIPT ONE
541 case 0xac: // ¬ NOT SIGN
542 case 0xb5: // µ MICRO SIGN
543 if ((bparams.inputenc == "latin1" ||
544 bparams.inputenc == "latin9") ||
545 (bparams.inputenc == "auto" &&
546 (font.language()->encoding()->latexName()
548 font.language()->encoding()->latexName()
550 os << "\\ensuremath{";
559 case '|': case '<': case '>':
560 // In T1 encoding, these characters exist
561 if (lyxrc.fontenc == "T1") {
563 //... but we should avoid ligatures
564 if ((c == '>' || c == '<')
566 && getChar(i + 1) == c) {
567 //os << "\\textcompwordmark{}";
568 // Jean-Marc, have a look at
569 // this. I think this works
577 // Typewriter font also has them
578 if (font.family() == LyXFont::TYPEWRITER_FAMILY) {
582 // Otherwise, we use what LaTeX
586 os << "\\textless{}";
590 os << "\\textgreater{}";
600 case '-': // "--" in Typewriter mode -> "-{}-"
602 && getChar(i + 1) == '-'
603 && font.family() == LyXFont::TYPEWRITER_FAMILY) {
612 os << "\\char`\\\"{}";
616 case 0xa3: // £ POUND SIGN
617 if (bparams.inputenc == "default") {
626 case '%': case '#': case '{':
634 os << "\\textasciitilde{}";
639 os << "\\textasciicircum{}";
644 // avoid being mistaken for optional arguments
652 // Blanks are printed before font switching.
653 // Sure? I am not! (try nice-latex)
654 // I am sure it's correct. LyX might be smarter
655 // in the future, but for now, nothing wrong is
661 // I assume this is hack treating typewriter as verbatim
662 if (font.family() == LyXFont::TYPEWRITER_FAMILY) {
671 // FIXME: if we have "LaTeX" with a font
672 // change in the middle (before the 'T', then
673 // the "TeX" part is still special cased.
674 // Really we should only operate this on
675 // "words" for some definition of word
679 for (; pnr < phrases_nr; ++pnr) {
680 if (isTextAt(special_phrases[pnr].phrase, i)) {
681 os << special_phrases[pnr].macro;
682 i += special_phrases[pnr].phrase.length() - 1;
683 column += special_phrases[pnr].macro.length() - 1;
688 if (pnr == phrases_nr && c != '\0') {
697 void Paragraph::Pimpl::validate(LaTeXFeatures & features,
698 LyXLayout const & layout) const
700 BufferParams const & bparams = features.bufferParams();
703 if (!params.spacing().isDefault())
704 features.require("setspace");
707 features.useLayout(layout.name());
710 Language const * doc_language = bparams.language;
712 FontList::const_iterator fcit = fontlist.begin();
713 FontList::const_iterator fend = fontlist.end();
714 for (; fcit != fend; ++fcit) {
715 if (fcit->font().noun() == LyXFont::ON) {
716 lyxerr[Debug::LATEX] << "font.noun: "
717 << fcit->font().noun()
719 features.require("noun");
720 lyxerr[Debug::LATEX] << "Noun enabled. Font: "
721 << fcit->font().stateText(0)
724 switch (fcit->font().color()) {
726 case LColor::inherit:
728 // probably we should put here all interface colors used for
729 // font displaying! For now I just add this ones I know of (Jug)
734 features.require("color");
735 lyxerr[Debug::LATEX] << "Color enabled. Font: "
736 << fcit->font().stateText(0)
740 Language const * language = fcit->font().language();
741 if (language->babel() != doc_language->babel() &&
742 language != ignore_language &&
743 language != latex_language)
745 features.useLanguage(language);
746 lyxerr[Debug::LATEX] << "Found language "
747 << language->babel() << endl;
751 if (!params.leftIndent().zero())
752 features.require("ParagraphLeftIndent");
755 InsetList::const_iterator icit = owner_->insetlist.begin();
756 InsetList::const_iterator iend = owner_->insetlist.end();
757 for (; icit != iend; ++icit) {
759 icit->inset->validate(features);
760 if (layout.needprotect &&
761 icit->inset->lyxCode() == InsetBase::FOOT_CODE)
762 features.require("NeedLyXFootnoteCode");
767 for (pos_type i = 0; i < size() ; ++i) {
768 for (size_t pnr = 0; pnr < phrases_nr; ++pnr) {
769 if (!special_phrases[pnr].builtin
770 && isTextAt(special_phrases[pnr].phrase, i)) {
771 features.require(special_phrases[pnr].phrase);