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::setChangeType(pos_type pos, Change::Type type)
124 changes_->set(type, pos);
128 void Paragraph::Pimpl::setChange(pos_type pos, Change const & change)
133 changes_->set(change, pos);
137 Change const Paragraph::Pimpl::lookupChange(pos_type pos) const
140 return Change(Change::UNCHANGED);
142 return changes_->lookup(pos);
146 void Paragraph::Pimpl::acceptChange(pos_type start, pos_type end)
152 changes_.reset(new Changes(Change::UNCHANGED));
156 lyxerr[Debug::CHANGES] << "acceptchange" << endl;
159 for (; i < end; ++i) {
160 switch (lookupChange(i).type) {
161 case Change::UNCHANGED:
164 case Change::INSERTED:
165 changes_->set(Change::UNCHANGED, i);
168 case Change::DELETED:
169 // Suppress access to nonexistent
170 // "end-of-paragraph char":
181 lyxerr[Debug::CHANGES] << "endacceptchange" << endl;
182 changes_->reset(Change::UNCHANGED);
186 void Paragraph::Pimpl::rejectChange(pos_type start, pos_type end)
192 changes_.reset(new Changes(Change::UNCHANGED));
198 for (; i < end; ++i) {
199 switch (lookupChange(i).type) {
200 case Change::UNCHANGED:
203 case Change::INSERTED:
212 case Change::DELETED:
213 changes_->set(Change::UNCHANGED, i);
214 // No real char at position size():
215 if (i < size() && owner_->isInset(i))
216 owner_->getInset(i)->markErased(false);
220 changes_->reset(Change::UNCHANGED);
224 Paragraph::value_type Paragraph::Pimpl::getChar(pos_type pos) const
226 return owner_->getChar(pos);
230 void Paragraph::Pimpl::insertChar(pos_type pos, value_type c, Change const & change)
232 BOOST_ASSERT(pos <= size());
235 changes_->record(change, pos);
238 // This is actually very common when parsing buffers (and
239 // maybe inserting ascii text)
241 // when appending characters, no need to update tables
242 owner_->text_.push_back(c);
246 owner_->text_.insert(owner_->text_.begin() + pos, c);
248 // Update the font table.
249 FontTable search_font(pos, LyXFont());
250 for (FontList::iterator it = lower_bound(fontlist.begin(),
252 search_font, matchFT());
253 it != fontlist.end(); ++it)
255 it->pos(it->pos() + 1);
259 owner_->insetlist.increasePosAfterPos(pos);
263 void Paragraph::Pimpl::insertInset(pos_type pos,
264 InsetBase * inset, Change const & change)
267 BOOST_ASSERT(pos <= size());
269 insertChar(pos, META_INSET, change);
270 BOOST_ASSERT(owner_->text_[pos] == META_INSET);
272 // Add a new entry in the insetlist.
273 owner_->insetlist.insert(inset, pos);
277 void Paragraph::Pimpl::eraseIntern(pos_type pos)
279 // if it is an inset, delete the inset entry
280 if (owner_->text_[pos] == Paragraph::META_INSET) {
281 owner_->insetlist.erase(pos);
284 owner_->text_.erase(owner_->text_.begin() + pos);
286 // Erase entries in the tables.
287 FontTable search_font(pos, LyXFont());
289 FontList::iterator it =
290 lower_bound(fontlist.begin(),
292 search_font, matchFT());
293 if (it != fontlist.end() && it->pos() == pos &&
295 (it != fontlist.begin()
296 && boost::prior(it)->pos() == pos - 1))) {
297 // If it is a multi-character font
298 // entry, we just make it smaller
299 // (see update below), otherwise we
301 unsigned int const i = it - fontlist.begin();
302 fontlist.erase(fontlist.begin() + i);
303 it = fontlist.begin() + i;
304 if (i > 0 && i < fontlist.size() &&
305 fontlist[i - 1].font() == fontlist[i].font()) {
306 fontlist.erase(fontlist.begin() + i - 1);
307 it = fontlist.begin() + i - 1;
311 // Update all other entries.
312 FontList::iterator fend = fontlist.end();
313 for (; it != fend; ++it)
314 it->pos(it->pos() - 1);
316 // Update the insetlist.
317 owner_->insetlist.decreasePosAfterPos(pos);
321 bool Paragraph::Pimpl::erase(pos_type pos)
323 BOOST_ASSERT(pos <= size());
326 Change::Type changetype(changes_->lookup(pos).type);
328 // only allow the actual removal if it was /new/ text
329 if (changetype != Change::INSERTED) {
330 changes_->record(Change(Change::DELETED), pos);
331 if (pos < size() && owner_->isInset(pos))
332 owner_->getInset(pos)->markErased(true);
337 // Don't physically access nonexistent end-of-paragraph char
347 int Paragraph::Pimpl::erase(pos_type start, pos_type end)
350 for (pos_type count = end - start; count; --count) {
358 void Paragraph::Pimpl::simpleTeXBlanks(ostream & 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 lyx::odocstringstream oss;
442 owner_->getInset(i)->plaintext(buf, oss, runparams);
444 os << lyx::to_utf8(oss.str());
449 // Two major modes: LaTeX or plain
450 // Handle here those cases common to both modes
451 // and then split to handle the two modes separately.
453 case Paragraph::META_INSET: {
454 InsetBase * inset = owner_->getInset(i);
456 // FIXME: remove this check
460 // FIXME: move this to InsetNewline::latex
461 if (inset->lyxCode() == InsetBase::NEWLINE_CODE) {
462 // newlines are handled differently here than
463 // the default in simpleTeXSpecialChars().
464 if (!style.newline_allowed) {
468 column += running_font.latexWriteEndChanges(os, basefont, basefont);
471 basefont = owner_->getLayoutFont(bparams, outerfont);
472 running_font = basefont;
474 if (font.family() == LyXFont::TYPEWRITER_FAMILY)
477 if (runparams.moving_arg)
483 texrow.start(owner_->id(), i + 1);
488 // output change tracking marks only if desired,
489 // if dvipost is installed,
490 // and with dvi/ps (other formats don't work)
491 LaTeXFeatures features(buf, bparams, runparams);
492 bool const output = bparams.outputChanges
493 && runparams.flavor == OutputParams::LATEX
494 && features.isAvailable("dvipost");
496 if (inset->canTrackChanges()) {
497 column += Changes::latexMarkChange(os, running_change,
498 Change::UNCHANGED, output);
499 running_change = Change::UNCHANGED;
503 ostream::pos_type const len = os.tellp();
505 if ((inset->lyxCode() == InsetBase::GRAPHICS_CODE
506 || inset->lyxCode() == InsetBase::MATH_CODE
507 || inset->lyxCode() == InsetBase::URL_CODE)
508 && running_font.isRightToLeft()) {
514 #warning Bug: we can have an empty font change here!
515 // if there has just been a font change, we are going to close it
516 // right now, which means stupid latex code like \textsf{}. AFAIK,
517 // this does not harm dvi output. A minor bug, thus (JMarc)
519 // some insets cannot be inside a font change command
520 if (open_font && inset->noFontChange()) {
521 column +=running_font.
522 latexWriteEndChanges(os,
526 basefont = owner_->getLayoutFont(bparams, outerfont);
527 running_font = basefont;
530 int tmp = inset->latex(buf, os, runparams);
536 for (int j = 0; j < tmp; ++j) {
539 texrow.start(owner_->id(), i + 1);
542 column += os.tellp() - len;
548 // And now for the special cases within each mode
552 os << "\\textbackslash{}";
556 case '±': case '²': case '³':
557 case '×': case '÷': case '¹':
559 if ((bparams.inputenc == "latin1" ||
560 bparams.inputenc == "latin9") ||
561 (bparams.inputenc == "auto" &&
562 (font.language()->encoding()->latexName()
564 font.language()->encoding()->latexName()
566 os << "\\ensuremath{"
575 case '|': case '<': case '>':
576 // In T1 encoding, these characters exist
577 if (lyxrc.fontenc == "T1") {
579 //... but we should avoid ligatures
580 if ((c == '>' || c == '<')
582 && getChar(i + 1) == c) {
583 //os << "\\textcompwordmark{}";
584 // Jean-Marc, have a look at
585 // this. I think this works
593 // Typewriter font also has them
594 if (font.family() == LyXFont::TYPEWRITER_FAMILY) {
598 // Otherwise, we use what LaTeX
602 os << "\\textless{}";
606 os << "\\textgreater{}";
616 case '-': // "--" in Typewriter mode -> "-{}-"
618 && getChar(i + 1) == '-'
619 && font.family() == LyXFont::TYPEWRITER_FAMILY) {
628 os << "\\char`\\\"{}";
633 if (bparams.inputenc == "default") {
642 case '%': case '#': case '{':
649 os << "\\textasciitilde{}";
654 os << "\\textasciicircum{}";
659 // avoid being mistaken for optional arguments
660 os << '{' << c << '}';
665 // Blanks are printed before font switching.
666 // Sure? I am not! (try nice-latex)
667 // I am sure it's correct. LyX might be smarter
668 // in the future, but for now, nothing wrong is
674 // I assume this is hack treating typewriter as verbatim
675 if (font.family() == LyXFont::TYPEWRITER_FAMILY) {
684 // FIXME: if we have "LaTeX" with a font
685 // change in the middle (before the 'T', then
686 // the "TeX" part is still special cased.
687 // Really we should only operate this on
688 // "words" for some definition of word
692 for (; pnr < phrases_nr; ++pnr) {
693 if (isTextAt(special_phrases[pnr].phrase, i)) {
694 os << special_phrases[pnr].macro;
695 i += special_phrases[pnr].phrase.length() - 1;
696 column += special_phrases[pnr].macro.length() - 1;
701 if (pnr == phrases_nr && c != '\0') {
710 void Paragraph::Pimpl::validate(LaTeXFeatures & features,
711 LyXLayout const & layout) const
713 BufferParams const & bparams = features.bufferParams();
716 if (!params.spacing().isDefault())
717 features.require("setspace");
720 features.useLayout(layout.name());
723 Language const * doc_language = bparams.language;
725 FontList::const_iterator fcit = fontlist.begin();
726 FontList::const_iterator fend = fontlist.end();
727 for (; fcit != fend; ++fcit) {
728 if (fcit->font().noun() == LyXFont::ON) {
729 lyxerr[Debug::LATEX] << "font.noun: "
730 << fcit->font().noun()
732 features.require("noun");
733 lyxerr[Debug::LATEX] << "Noun enabled. Font: "
734 << fcit->font().stateText(0)
737 switch (fcit->font().color()) {
739 case LColor::inherit:
741 // probably we should put here all interface colors used for
742 // font displaying! For now I just add this ones I know of (Jug)
747 features.require("color");
748 lyxerr[Debug::LATEX] << "Color enabled. Font: "
749 << fcit->font().stateText(0)
753 Language const * language = fcit->font().language();
754 if (language->babel() != doc_language->babel() &&
755 language != ignore_language &&
756 language != latex_language)
758 features.useLanguage(language);
759 lyxerr[Debug::LATEX] << "Found language "
760 << language->babel() << endl;
764 if (!params.leftIndent().zero())
765 features.require("ParagraphLeftIndent");
768 InsetList::const_iterator icit = owner_->insetlist.begin();
769 InsetList::const_iterator iend = owner_->insetlist.end();
770 for (; icit != iend; ++icit) {
772 icit->inset->validate(features);
773 if (layout.needprotect &&
774 icit->inset->lyxCode() == InsetBase::FOOT_CODE)
775 features.require("NeedLyXFootnoteCode");
780 for (pos_type i = 0; i < size() ; ++i) {
781 for (size_t pnr = 0; pnr < phrases_nr; ++pnr) {
782 if (!special_phrases[pnr].builtin
783 && isTextAt(special_phrases[pnr].phrase, i)) {
784 features.require(special_phrases[pnr].phrase);