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":
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:
204 case Change::DELETED:
205 changes_->set(Change::UNCHANGED, i);
206 // No real char at position size():
207 if (i < size() && owner_->isInset(i))
208 owner_->getInset(i)->markErased(false);
212 changes_->reset(Change::UNCHANGED);
216 Paragraph::value_type Paragraph::Pimpl::getChar(pos_type pos) const
218 return owner_->getChar(pos);
222 void Paragraph::Pimpl::insertChar(pos_type pos, value_type c, Change const & change)
224 BOOST_ASSERT(pos <= size());
227 changes_->record(change, pos);
230 // This is actually very common when parsing buffers (and
231 // maybe inserting ascii text)
233 // when appending characters, no need to update tables
234 owner_->text_.push_back(c);
238 owner_->text_.insert(owner_->text_.begin() + pos, c);
240 // Update the font table.
241 FontTable search_font(pos, LyXFont());
242 for (FontList::iterator it = lower_bound(fontlist.begin(),
244 search_font, matchFT());
245 it != fontlist.end(); ++it)
247 it->pos(it->pos() + 1);
251 owner_->insetlist.increasePosAfterPos(pos);
255 void Paragraph::Pimpl::insertInset(pos_type pos,
256 InsetBase * inset, Change const & change)
259 BOOST_ASSERT(pos <= size());
261 insertChar(pos, META_INSET, change);
262 BOOST_ASSERT(owner_->text_[pos] == META_INSET);
264 // Add a new entry in the insetlist.
265 owner_->insetlist.insert(inset, pos);
269 void Paragraph::Pimpl::eraseIntern(pos_type pos)
271 // if it is an inset, delete the inset entry
272 if (owner_->text_[pos] == Paragraph::META_INSET) {
273 owner_->insetlist.erase(pos);
276 owner_->text_.erase(owner_->text_.begin() + pos);
278 // Erase entries in the tables.
279 FontTable search_font(pos, LyXFont());
281 FontList::iterator it =
282 lower_bound(fontlist.begin(),
284 search_font, matchFT());
285 if (it != fontlist.end() && it->pos() == pos &&
287 (it != fontlist.begin()
288 && boost::prior(it)->pos() == pos - 1))) {
289 // If it is a multi-character font
290 // entry, we just make it smaller
291 // (see update below), otherwise we
293 unsigned int const i = it - fontlist.begin();
294 fontlist.erase(fontlist.begin() + i);
295 it = fontlist.begin() + i;
296 if (i > 0 && i < fontlist.size() &&
297 fontlist[i - 1].font() == fontlist[i].font()) {
298 fontlist.erase(fontlist.begin() + i - 1);
299 it = fontlist.begin() + i - 1;
303 // Update all other entries.
304 FontList::iterator fend = fontlist.end();
305 for (; it != fend; ++it)
306 it->pos(it->pos() - 1);
308 // Update the insetlist.
309 owner_->insetlist.decreasePosAfterPos(pos);
313 bool Paragraph::Pimpl::erase(pos_type pos)
315 BOOST_ASSERT(pos <= size());
318 Change::Type changetype(changes_->lookup(pos).type);
320 // only allow the actual removal if it was /new/ text
321 if (changetype != Change::INSERTED) {
322 changes_->record(Change(Change::DELETED), pos);
323 if (pos < size() && owner_->isInset(pos))
324 owner_->getInset(pos)->markErased(true);
329 // Don't physically access nonexistent end-of-paragraph char
339 int Paragraph::Pimpl::erase(pos_type start, pos_type end)
342 for (pos_type count = end - start; count; --count) {
350 void Paragraph::Pimpl::simpleTeXBlanks(odocstream & os, TexRow & texrow,
352 unsigned int & column,
353 LyXFont const & font,
354 LyXLayout const & style)
359 if (column > lyxrc.ascii_linelen
361 && getChar(i - 1) != ' '
363 // same in FreeSpacing mode
364 && !owner_->isFreeSpacing()
365 // In typewriter mode, we want to avoid
366 // ! . ? : at the end of a line
367 && !(font.family() == LyXFont::TYPEWRITER_FAMILY
368 && (getChar(i - 1) == '.'
369 || getChar(i - 1) == '?'
370 || getChar(i - 1) == ':'
371 || getChar(i - 1) == '!'))) {
374 texrow.start(owner_->id(), i + 1);
376 } else if (style.free_spacing) {
384 bool Paragraph::Pimpl::isTextAt(string const & str, pos_type pos) const
386 pos_type const len = str.length();
388 // is the paragraph large enough?
389 if (pos + len > size())
392 // does the wanted text start at point?
393 for (string::size_type i = 0; i < str.length(); ++i) {
394 if (str[i] != owner_->text_[pos + i])
398 // is there a font change in middle of the word?
399 FontList::const_iterator cit = fontlist.begin();
400 FontList::const_iterator end = fontlist.end();
401 for (; cit != end; ++cit) {
402 if (cit->pos() >= pos)
405 if (cit != end && pos + len - 1 > cit->pos())
412 void Paragraph::Pimpl::simpleTeXSpecialChars(Buffer const & buf,
413 BufferParams const & bparams,
416 OutputParams const & runparams,
418 LyXFont & running_font,
420 LyXFont const & outerfont,
422 Change::Type & running_change,
423 LyXLayout const & style,
425 unsigned int & column,
428 if (style.pass_thru) {
429 if (c != Paragraph::META_INSET) {
433 owner_->getInset(i)->plaintext(buf, os, runparams);
437 // Two major modes: LaTeX or plain
438 // Handle here those cases common to both modes
439 // and then split to handle the two modes separately.
441 case Paragraph::META_INSET: {
442 InsetBase * inset = owner_->getInset(i);
444 // FIXME: remove this check
448 // FIXME: move this to InsetNewline::latex
449 if (inset->lyxCode() == InsetBase::NEWLINE_CODE) {
450 // newlines are handled differently here than
451 // the default in simpleTeXSpecialChars().
452 if (!style.newline_allowed) {
456 column += running_font.latexWriteEndChanges(os, basefont, basefont);
459 basefont = owner_->getLayoutFont(bparams, outerfont);
460 running_font = basefont;
462 if (font.family() == LyXFont::TYPEWRITER_FAMILY)
465 if (runparams.moving_arg)
471 texrow.start(owner_->id(), i + 1);
476 // output change tracking marks only if desired,
477 // if dvipost is installed,
478 // and with dvi/ps (other formats don't work)
479 LaTeXFeatures features(buf, bparams, runparams);
480 bool const output = bparams.outputChanges
481 && runparams.flavor == OutputParams::LATEX
482 && features.isAvailable("dvipost");
484 if (inset->canTrackChanges()) {
485 column += Changes::latexMarkChange(os, running_change,
486 Change::UNCHANGED, output);
487 running_change = Change::UNCHANGED;
491 odocstream::pos_type const len = os.tellp();
493 if ((inset->lyxCode() == InsetBase::GRAPHICS_CODE
494 || inset->lyxCode() == InsetBase::MATH_CODE
495 || inset->lyxCode() == InsetBase::URL_CODE)
496 && running_font.isRightToLeft()) {
502 #warning Bug: we can have an empty font change here!
503 // if there has just been a font change, we are going to close it
504 // right now, which means stupid latex code like \textsf{}. AFAIK,
505 // this does not harm dvi output. A minor bug, thus (JMarc)
507 // some insets cannot be inside a font change command
508 if (open_font && inset->noFontChange()) {
509 column +=running_font.
510 latexWriteEndChanges(os,
514 basefont = owner_->getLayoutFont(bparams, outerfont);
515 running_font = basefont;
518 int tmp = inset->latex(buf, os, runparams);
524 for (int j = 0; j < tmp; ++j) {
527 texrow.start(owner_->id(), i + 1);
530 column += os.tellp() - len;
536 // And now for the special cases within each mode
540 os << "\\textbackslash{}";
544 case '±': case '²': case '³':
545 case '×': case '÷': case '¹':
547 if ((bparams.inputenc == "latin1" ||
548 bparams.inputenc == "latin9") ||
549 (bparams.inputenc == "auto" &&
550 (font.language()->encoding()->latexName()
552 font.language()->encoding()->latexName()
554 os << "\\ensuremath{"
563 case '|': case '<': case '>':
564 // In T1 encoding, these characters exist
565 if (lyxrc.fontenc == "T1") {
567 //... but we should avoid ligatures
568 if ((c == '>' || c == '<')
570 && getChar(i + 1) == c) {
571 //os << "\\textcompwordmark{}";
572 // Jean-Marc, have a look at
573 // this. I think this works
581 // Typewriter font also has them
582 if (font.family() == LyXFont::TYPEWRITER_FAMILY) {
586 // Otherwise, we use what LaTeX
590 os << "\\textless{}";
594 os << "\\textgreater{}";
604 case '-': // "--" in Typewriter mode -> "-{}-"
606 && getChar(i + 1) == '-'
607 && font.family() == LyXFont::TYPEWRITER_FAMILY) {
616 os << "\\char`\\\"{}";
621 if (bparams.inputenc == "default") {
630 case '%': case '#': case '{':
637 os << "\\textasciitilde{}";
642 os << "\\textasciicircum{}";
647 // avoid being mistaken for optional arguments
648 os << '{' << c << '}';
653 // Blanks are printed before font switching.
654 // Sure? I am not! (try nice-latex)
655 // I am sure it's correct. LyX might be smarter
656 // in the future, but for now, nothing wrong is
662 // I assume this is hack treating typewriter as verbatim
663 if (font.family() == LyXFont::TYPEWRITER_FAMILY) {
672 // FIXME: if we have "LaTeX" with a font
673 // change in the middle (before the 'T', then
674 // the "TeX" part is still special cased.
675 // Really we should only operate this on
676 // "words" for some definition of word
680 for (; pnr < phrases_nr; ++pnr) {
681 if (isTextAt(special_phrases[pnr].phrase, i)) {
682 os << special_phrases[pnr].macro;
683 i += special_phrases[pnr].phrase.length() - 1;
684 column += special_phrases[pnr].macro.length() - 1;
689 if (pnr == phrases_nr && c != '\0') {
698 void Paragraph::Pimpl::validate(LaTeXFeatures & features,
699 LyXLayout const & layout) const
701 BufferParams const & bparams = features.bufferParams();
704 if (!params.spacing().isDefault())
705 features.require("setspace");
708 features.useLayout(layout.name());
711 Language const * doc_language = bparams.language;
713 FontList::const_iterator fcit = fontlist.begin();
714 FontList::const_iterator fend = fontlist.end();
715 for (; fcit != fend; ++fcit) {
716 if (fcit->font().noun() == LyXFont::ON) {
717 lyxerr[Debug::LATEX] << "font.noun: "
718 << fcit->font().noun()
720 features.require("noun");
721 lyxerr[Debug::LATEX] << "Noun enabled. Font: "
722 << fcit->font().stateText(0)
725 switch (fcit->font().color()) {
727 case LColor::inherit:
729 // probably we should put here all interface colors used for
730 // font displaying! For now I just add this ones I know of (Jug)
735 features.require("color");
736 lyxerr[Debug::LATEX] << "Color enabled. Font: "
737 << fcit->font().stateText(0)
741 Language const * language = fcit->font().language();
742 if (language->babel() != doc_language->babel() &&
743 language != ignore_language &&
744 language != latex_language)
746 features.useLanguage(language);
747 lyxerr[Debug::LATEX] << "Found language "
748 << language->babel() << endl;
752 if (!params.leftIndent().zero())
753 features.require("ParagraphLeftIndent");
756 InsetList::const_iterator icit = owner_->insetlist.begin();
757 InsetList::const_iterator iend = owner_->insetlist.end();
758 for (; icit != iend; ++icit) {
760 icit->inset->validate(features);
761 if (layout.needprotect &&
762 icit->inset->lyxCode() == InsetBase::FOOT_CODE)
763 features.require("NeedLyXFootnoteCode");
768 for (pos_type i = 0; i < size() ; ++i) {
769 for (size_t pnr = 0; pnr < phrases_nr; ++pnr) {
770 if (!special_phrases[pnr].builtin
771 && isTextAt(special_phrases[pnr].phrase, i)) {
772 features.require(special_phrases[pnr].phrase);