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 // FIXME: change tracking (MG)
158 changes_->set(Change(Change::UNCHANGED), i);
161 case Change::DELETED:
162 // Suppress access to nonexistent
163 // "end-of-paragraph char":
173 lyxerr[Debug::CHANGES] << "endacceptchange" << endl;
174 changes_->reset(Change::UNCHANGED);
178 void Paragraph::Pimpl::rejectChange(pos_type start, pos_type end)
184 changes_.reset(new Changes(Change::UNCHANGED));
190 for (; i < end; ++i) {
191 switch (lookupChange(i).type) {
192 case Change::UNCHANGED:
195 case Change::INSERTED:
203 case Change::DELETED:
204 // FIXME: change tracking (MG)
205 changes_->set(Change(Change::UNCHANGED), i);
206 // No real char at position size():
207 if (i < size() && owner_->isInset(i))
208 // FIXME: change tracking (MG)
209 owner_->getInset(i)->setChange(Change(Change::UNCHANGED));
213 changes_->reset(Change::UNCHANGED);
217 Paragraph::value_type Paragraph::Pimpl::getChar(pos_type pos) const
219 return owner_->getChar(pos);
223 void Paragraph::Pimpl::insertChar(pos_type pos, value_type c, Change const & change)
225 BOOST_ASSERT(pos <= size());
228 changes_->record(change, pos);
231 // This is actually very common when parsing buffers (and
232 // maybe inserting ascii text)
234 // when appending characters, no need to update tables
235 owner_->text_.push_back(c);
239 owner_->text_.insert(owner_->text_.begin() + pos, c);
241 // Update the font table.
242 FontTable search_font(pos, LyXFont());
243 for (FontList::iterator it = lower_bound(fontlist.begin(),
245 search_font, matchFT());
246 it != fontlist.end(); ++it)
248 it->pos(it->pos() + 1);
252 owner_->insetlist.increasePosAfterPos(pos);
256 void Paragraph::Pimpl::insertInset(pos_type pos,
257 InsetBase * inset, Change const & change)
260 BOOST_ASSERT(pos <= size());
262 insertChar(pos, META_INSET, change);
263 BOOST_ASSERT(owner_->text_[pos] == META_INSET);
265 // Add a new entry in the insetlist.
266 owner_->insetlist.insert(inset, pos);
270 void Paragraph::Pimpl::erase(pos_type pos)
272 // FIXME: change tracking (MG)
273 // do something like changes_->erase(i);
274 // in one of the next patches, the two erase functions
275 // will be merged but I don't want to break too many things at the same time :-)
277 // if it is an inset, delete the inset entry
278 if (owner_->text_[pos] == Paragraph::META_INSET) {
279 owner_->insetlist.erase(pos);
282 owner_->text_.erase(owner_->text_.begin() + pos);
284 // Erase entries in the tables.
285 FontTable search_font(pos, LyXFont());
287 FontList::iterator it =
288 lower_bound(fontlist.begin(),
290 search_font, matchFT());
291 if (it != fontlist.end() && it->pos() == pos &&
293 (it != fontlist.begin()
294 && boost::prior(it)->pos() == pos - 1))) {
295 // If it is a multi-character font
296 // entry, we just make it smaller
297 // (see update below), otherwise we
299 unsigned int const i = it - fontlist.begin();
300 fontlist.erase(fontlist.begin() + i);
301 it = fontlist.begin() + i;
302 if (i > 0 && i < fontlist.size() &&
303 fontlist[i - 1].font() == fontlist[i].font()) {
304 fontlist.erase(fontlist.begin() + i - 1);
305 it = fontlist.begin() + i - 1;
309 // Update all other entries
310 FontList::iterator fend = fontlist.end();
311 for (; it != fend; ++it)
312 it->pos(it->pos() - 1);
314 // Update the insetlist
315 owner_->insetlist.decreasePosAfterPos(pos);
319 bool Paragraph::Pimpl::erase(pos_type pos, bool trackChanges)
321 // FIXME: change tracking (MG)
322 BOOST_ASSERT(pos <= size());
325 Change::Type changetype(changes_->lookup(pos).type);
327 // only allow the actual removal if it was /new/ text
328 if (changetype != Change::INSERTED) {
329 changes_->record(Change(Change::DELETED), pos);
330 if (pos < size() && owner_->isInset(pos))
331 // FIXME: change tracking (MG)
332 owner_->getInset(pos)->setChange(Change(Change::DELETED));
337 // Don't physically access nonexistent end-of-paragraph char
347 int Paragraph::Pimpl::erase(pos_type start, pos_type end, bool trackChanges)
350 for (pos_type count = end - start; count; --count) {
351 if (!erase(i, trackChanges))
358 void Paragraph::Pimpl::simpleTeXBlanks(odocstream & os, TexRow & texrow,
360 unsigned int & column,
361 LyXFont const & font,
362 LyXLayout const & style)
367 if (column > lyxrc.ascii_linelen
369 && getChar(i - 1) != ' '
371 // same in FreeSpacing mode
372 && !owner_->isFreeSpacing()
373 // In typewriter mode, we want to avoid
374 // ! . ? : at the end of a line
375 && !(font.family() == LyXFont::TYPEWRITER_FAMILY
376 && (getChar(i - 1) == '.'
377 || getChar(i - 1) == '?'
378 || getChar(i - 1) == ':'
379 || getChar(i - 1) == '!'))) {
382 texrow.start(owner_->id(), i + 1);
384 } else if (style.free_spacing) {
392 bool Paragraph::Pimpl::isTextAt(string const & str, pos_type pos) const
394 pos_type const len = str.length();
396 // is the paragraph large enough?
397 if (pos + len > size())
400 // does the wanted text start at point?
401 for (string::size_type i = 0; i < str.length(); ++i) {
402 if (str[i] != owner_->text_[pos + i])
406 // is there a font change in middle of the word?
407 FontList::const_iterator cit = fontlist.begin();
408 FontList::const_iterator end = fontlist.end();
409 for (; cit != end; ++cit) {
410 if (cit->pos() >= pos)
413 if (cit != end && pos + len - 1 > cit->pos())
420 void Paragraph::Pimpl::simpleTeXSpecialChars(Buffer const & buf,
421 BufferParams const & bparams,
424 OutputParams const & runparams,
426 LyXFont & running_font,
428 LyXFont const & outerfont,
430 Change::Type & running_change,
431 LyXLayout const & style,
433 unsigned int & column,
436 if (style.pass_thru) {
437 if (c != Paragraph::META_INSET) {
441 owner_->getInset(i)->plaintext(buf, os, runparams);
445 // Two major modes: LaTeX or plain
446 // Handle here those cases common to both modes
447 // and then split to handle the two modes separately.
449 case Paragraph::META_INSET: {
450 InsetBase * inset = owner_->getInset(i);
452 // FIXME: remove this check
456 // FIXME: move this to InsetNewline::latex
457 if (inset->lyxCode() == InsetBase::NEWLINE_CODE) {
458 // newlines are handled differently here than
459 // the default in simpleTeXSpecialChars().
460 if (!style.newline_allowed) {
464 column += running_font.latexWriteEndChanges(os, basefont, basefont);
467 basefont = owner_->getLayoutFont(bparams, outerfont);
468 running_font = basefont;
470 if (font.family() == LyXFont::TYPEWRITER_FAMILY)
473 if (runparams.moving_arg)
479 texrow.start(owner_->id(), i + 1);
484 // output change tracking marks only if desired,
485 // if dvipost is installed,
486 // and with dvi/ps (other formats don't work)
487 LaTeXFeatures features(buf, bparams, runparams);
488 bool const output = bparams.outputChanges
489 && runparams.flavor == OutputParams::LATEX
490 && features.isAvailable("dvipost");
492 if (inset->canTrackChanges()) {
493 column += Changes::latexMarkChange(os, running_change,
494 Change::UNCHANGED, output);
495 running_change = Change::UNCHANGED;
499 odocstream::pos_type const len = os.tellp();
501 if ((inset->lyxCode() == InsetBase::GRAPHICS_CODE
502 || inset->lyxCode() == InsetBase::MATH_CODE
503 || inset->lyxCode() == InsetBase::URL_CODE)
504 && running_font.isRightToLeft()) {
510 #warning Bug: we can have an empty font change here!
511 // if there has just been a font change, we are going to close it
512 // right now, which means stupid latex code like \textsf{}. AFAIK,
513 // this does not harm dvi output. A minor bug, thus (JMarc)
515 // some insets cannot be inside a font change command
516 if (open_font && inset->noFontChange()) {
517 column +=running_font.
518 latexWriteEndChanges(os,
522 basefont = owner_->getLayoutFont(bparams, outerfont);
523 running_font = basefont;
526 int tmp = inset->latex(buf, os, runparams);
532 for (int j = 0; j < tmp; ++j) {
535 texrow.start(owner_->id(), i + 1);
538 column += os.tellp() - len;
544 // And now for the special cases within each mode
548 os << "\\textbackslash{}";
552 case '±': case '²': case '³':
553 case '×': case '÷': case '¹':
555 if ((bparams.inputenc == "latin1" ||
556 bparams.inputenc == "latin9") ||
557 (bparams.inputenc == "auto" &&
558 (font.language()->encoding()->latexName()
560 font.language()->encoding()->latexName()
562 os << "\\ensuremath{";
571 case '|': case '<': case '>':
572 // In T1 encoding, these characters exist
573 if (lyxrc.fontenc == "T1") {
575 //... but we should avoid ligatures
576 if ((c == '>' || c == '<')
578 && getChar(i + 1) == c) {
579 //os << "\\textcompwordmark{}";
580 // Jean-Marc, have a look at
581 // this. I think this works
589 // Typewriter font also has them
590 if (font.family() == LyXFont::TYPEWRITER_FAMILY) {
594 // Otherwise, we use what LaTeX
598 os << "\\textless{}";
602 os << "\\textgreater{}";
612 case '-': // "--" in Typewriter mode -> "-{}-"
614 && getChar(i + 1) == '-'
615 && font.family() == LyXFont::TYPEWRITER_FAMILY) {
624 os << "\\char`\\\"{}";
629 if (bparams.inputenc == "default") {
638 case '%': case '#': case '{':
646 os << "\\textasciitilde{}";
651 os << "\\textasciicircum{}";
656 // avoid being mistaken for optional arguments
664 // Blanks are printed before font switching.
665 // Sure? I am not! (try nice-latex)
666 // I am sure it's correct. LyX might be smarter
667 // in the future, but for now, nothing wrong is
673 // I assume this is hack treating typewriter as verbatim
674 if (font.family() == LyXFont::TYPEWRITER_FAMILY) {
683 // FIXME: if we have "LaTeX" with a font
684 // change in the middle (before the 'T', then
685 // the "TeX" part is still special cased.
686 // Really we should only operate this on
687 // "words" for some definition of word
691 for (; pnr < phrases_nr; ++pnr) {
692 if (isTextAt(special_phrases[pnr].phrase, i)) {
693 os << special_phrases[pnr].macro;
694 i += special_phrases[pnr].phrase.length() - 1;
695 column += special_phrases[pnr].macro.length() - 1;
700 if (pnr == phrases_nr && c != '\0') {
709 void Paragraph::Pimpl::validate(LaTeXFeatures & features,
710 LyXLayout const & layout) const
712 BufferParams const & bparams = features.bufferParams();
715 if (!params.spacing().isDefault())
716 features.require("setspace");
719 features.useLayout(layout.name());
722 Language const * doc_language = bparams.language;
724 FontList::const_iterator fcit = fontlist.begin();
725 FontList::const_iterator fend = fontlist.end();
726 for (; fcit != fend; ++fcit) {
727 if (fcit->font().noun() == LyXFont::ON) {
728 lyxerr[Debug::LATEX] << "font.noun: "
729 << fcit->font().noun()
731 features.require("noun");
732 lyxerr[Debug::LATEX] << "Noun enabled. Font: "
733 << fcit->font().stateText(0)
736 switch (fcit->font().color()) {
738 case LColor::inherit:
740 // probably we should put here all interface colors used for
741 // font displaying! For now I just add this ones I know of (Jug)
746 features.require("color");
747 lyxerr[Debug::LATEX] << "Color enabled. Font: "
748 << fcit->font().stateText(0)
752 Language const * language = fcit->font().language();
753 if (language->babel() != doc_language->babel() &&
754 language != ignore_language &&
755 language != latex_language)
757 features.useLanguage(language);
758 lyxerr[Debug::LATEX] << "Found language "
759 << language->babel() << endl;
763 if (!params.leftIndent().zero())
764 features.require("ParagraphLeftIndent");
767 InsetList::const_iterator icit = owner_->insetlist.begin();
768 InsetList::const_iterator iend = owner_->insetlist.end();
769 for (; icit != iend; ++icit) {
771 icit->inset->validate(features);
772 if (layout.needprotect &&
773 icit->inset->lyxCode() == InsetBase::FOOT_CODE)
774 features.require("NeedLyXFootnoteCode");
779 for (pos_type i = 0; i < size() ; ++i) {
780 for (size_t pnr = 0; pnr < phrases_nr; ++pnr) {
781 if (!special_phrases[pnr].builtin
782 && isTextAt(special_phrases[pnr].phrase, i)) {
783 features.require(special_phrases[pnr].phrase);