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
98 return changes_->isChange(start, end);
102 bool Paragraph::Pimpl::isChangeEdited(pos_type start, pos_type end) const
107 return changes_->isChangeEdited(start, end);
111 void Paragraph::Pimpl::setChangeType(pos_type pos, Change::Type type)
116 changes_->set(type, pos);
120 void Paragraph::Pimpl::setChange(pos_type pos, Change 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::markErased(bool erased)
140 BOOST_ASSERT(tracking());
145 for (pos_type i = 0; i < size(); ++i) {
146 changes_->set(Change::UNCHANGED, i);
147 if (owner_->isInset(i))
148 owner_->getInset(i)->markErased(false);
154 void Paragraph::Pimpl::acceptChange(pos_type start, pos_type end)
160 changes_.reset(new Changes(Change::UNCHANGED));
164 lyxerr[Debug::CHANGES] << "acceptchange" << endl;
167 for (; i < end; ++i) {
168 switch (lookupChange(i).type) {
169 case Change::UNCHANGED:
172 case Change::INSERTED:
173 changes_->set(Change::UNCHANGED, i);
176 case Change::DELETED:
177 // Suppress access to nonexistent
178 // "end-of-paragraph char":
189 lyxerr[Debug::CHANGES] << "endacceptchange" << endl;
190 changes_->reset(Change::UNCHANGED);
194 void Paragraph::Pimpl::rejectChange(pos_type start, pos_type end)
200 changes_.reset(new Changes(Change::UNCHANGED));
206 for (; i < end; ++i) {
207 switch (lookupChange(i).type) {
208 case Change::UNCHANGED:
211 case Change::INSERTED:
220 case Change::DELETED:
221 changes_->set(Change::UNCHANGED, i);
222 // No real char at position size():
223 if (i < size() && owner_->isInset(i))
224 owner_->getInset(i)->markErased(false);
228 changes_->reset(Change::UNCHANGED);
232 Paragraph::value_type Paragraph::Pimpl::getChar(pos_type pos) const
234 return owner_->getChar(pos);
238 void Paragraph::Pimpl::insertChar(pos_type pos, value_type c, Change change)
240 BOOST_ASSERT(pos <= size());
243 changes_->record(change, pos);
246 // This is actually very common when parsing buffers (and
247 // maybe inserting ascii text)
249 // when appending characters, no need to update tables
250 owner_->text_.push_back(c);
254 owner_->text_.insert(owner_->text_.begin() + pos, c);
256 // Update the font table.
257 FontTable search_font(pos, LyXFont());
258 for (FontList::iterator it = lower_bound(fontlist.begin(),
260 search_font, matchFT());
261 it != fontlist.end(); ++it)
263 it->pos(it->pos() + 1);
267 owner_->insetlist.increasePosAfterPos(pos);
271 void Paragraph::Pimpl::insertInset(pos_type pos,
272 InsetBase * inset, Change change)
275 BOOST_ASSERT(pos <= size());
277 insertChar(pos, META_INSET, change);
278 BOOST_ASSERT(owner_->text_[pos] == META_INSET);
280 // Add a new entry in the insetlist.
281 owner_->insetlist.insert(inset, pos);
285 void Paragraph::Pimpl::eraseIntern(pos_type pos)
287 // if it is an inset, delete the inset entry
288 if (owner_->text_[pos] == Paragraph::META_INSET) {
289 owner_->insetlist.erase(pos);
292 owner_->text_.erase(owner_->text_.begin() + pos);
294 // Erase entries in the tables.
295 FontTable search_font(pos, LyXFont());
297 FontList::iterator it =
298 lower_bound(fontlist.begin(),
300 search_font, matchFT());
301 if (it != fontlist.end() && it->pos() == pos &&
303 (it != fontlist.begin()
304 && boost::prior(it)->pos() == pos - 1))) {
305 // If it is a multi-character font
306 // entry, we just make it smaller
307 // (see update below), otherwise we
309 unsigned int const i = it - fontlist.begin();
310 fontlist.erase(fontlist.begin() + i);
311 it = fontlist.begin() + i;
312 if (i > 0 && i < fontlist.size() &&
313 fontlist[i - 1].font() == fontlist[i].font()) {
314 fontlist.erase(fontlist.begin() + i - 1);
315 it = fontlist.begin() + i - 1;
319 // Update all other entries.
320 FontList::iterator fend = fontlist.end();
321 for (; it != fend; ++it)
322 it->pos(it->pos() - 1);
324 // Update the insetlist.
325 owner_->insetlist.decreasePosAfterPos(pos);
329 bool Paragraph::Pimpl::erase(pos_type pos)
331 BOOST_ASSERT(pos <= size());
334 Change::Type changetype(changes_->lookup(pos).type);
336 // only allow the actual removal if it was /new/ text
337 if (changetype != Change::INSERTED) {
338 changes_->record(Change(Change::DELETED), pos);
339 if (pos < size() && owner_->isInset(pos))
340 owner_->getInset(pos)->markErased(true);
345 // Don't physically access nonexistent end-of-paragraph char
355 int Paragraph::Pimpl::erase(pos_type start, pos_type end)
358 for (pos_type count = end - start; count; --count) {
366 void Paragraph::Pimpl::simpleTeXBlanks(ostream & os, TexRow & texrow,
368 unsigned int & column,
369 LyXFont const & font,
370 LyXLayout const & style)
375 if (column > lyxrc.ascii_linelen
377 && getChar(i - 1) != ' '
379 // same in FreeSpacing mode
380 && !owner_->isFreeSpacing()
381 // In typewriter mode, we want to avoid
382 // ! . ? : at the end of a line
383 && !(font.family() == LyXFont::TYPEWRITER_FAMILY
384 && (getChar(i - 1) == '.'
385 || getChar(i - 1) == '?'
386 || getChar(i - 1) == ':'
387 || getChar(i - 1) == '!'))) {
390 texrow.start(owner_->id(), i + 1);
392 } else if (style.free_spacing) {
400 bool Paragraph::Pimpl::isTextAt(string const & str, pos_type pos) const
402 pos_type const len = str.length();
404 // is the paragraph large enough?
405 if (pos + len > size())
408 // does the wanted text start at point?
409 for (string::size_type i = 0; i < str.length(); ++i) {
410 if (str[i] != owner_->text_[pos + i])
414 // is there a font change in middle of the word?
415 FontList::const_iterator cit = fontlist.begin();
416 FontList::const_iterator end = fontlist.end();
417 for (; cit != end; ++cit) {
418 if (cit->pos() >= pos)
421 if (cit != end && pos + len - 1 > cit->pos())
428 void Paragraph::Pimpl::simpleTeXSpecialChars(Buffer const & buf,
429 BufferParams const & bparams,
432 OutputParams const & runparams,
434 LyXFont & running_font,
436 LyXFont const & outerfont,
438 Change::Type & running_change,
439 LyXLayout const & style,
441 unsigned int & column,
444 if (style.pass_thru) {
445 if (c != Paragraph::META_INSET) {
449 lyx::odocstringstream oss;
450 owner_->getInset(i)->plaintext(buf, oss, runparams);
452 os << lyx::to_utf8(oss.str());
457 // Two major modes: LaTeX or plain
458 // Handle here those cases common to both modes
459 // and then split to handle the two modes separately.
461 case Paragraph::META_INSET: {
462 InsetBase * inset = owner_->getInset(i);
464 // FIXME: remove this check
468 // FIXME: move this to InsetNewline::latex
469 if (inset->lyxCode() == InsetBase::NEWLINE_CODE) {
470 // newlines are handled differently here than
471 // the default in simpleTeXSpecialChars().
472 if (!style.newline_allowed) {
476 column += running_font.latexWriteEndChanges(os, basefont, basefont);
479 basefont = owner_->getLayoutFont(bparams, outerfont);
480 running_font = basefont;
482 if (font.family() == LyXFont::TYPEWRITER_FAMILY)
485 if (runparams.moving_arg)
491 texrow.start(owner_->id(), i + 1);
496 // output change tracking marks only if desired,
497 // if dvipost is installed,
498 // and with dvi/ps (other formats don't work)
499 LaTeXFeatures features(buf, bparams, runparams);
500 bool const output = bparams.outputChanges
501 && runparams.flavor == OutputParams::LATEX
502 && features.isAvailable("dvipost");
504 if (inset->canTrackChanges()) {
505 column += Changes::latexMarkChange(os, running_change,
506 Change::UNCHANGED, output);
507 running_change = Change::UNCHANGED;
511 ostream::pos_type const len = os.tellp();
513 if ((inset->lyxCode() == InsetBase::GRAPHICS_CODE
514 || inset->lyxCode() == InsetBase::MATH_CODE
515 || inset->lyxCode() == InsetBase::URL_CODE)
516 && running_font.isRightToLeft()) {
522 #warning Bug: we can have an empty font change here!
523 // if there has just been a font change, we are going to close it
524 // right now, which means stupid latex code like \textsf{}. AFAIK,
525 // this does not harm dvi output. A minor bug, thus (JMarc)
527 // some insets cannot be inside a font change command
528 if (open_font && inset->noFontChange()) {
529 column +=running_font.
530 latexWriteEndChanges(os,
534 basefont = owner_->getLayoutFont(bparams, outerfont);
535 running_font = basefont;
538 int tmp = inset->latex(buf, os, runparams);
544 for (int j = 0; j < tmp; ++j) {
547 texrow.start(owner_->id(), i + 1);
550 column += os.tellp() - len;
556 // And now for the special cases within each mode
560 os << "\\textbackslash{}";
564 case '±': case '²': case '³':
565 case '×': case '÷': case '¹':
567 if ((bparams.inputenc == "latin1" ||
568 bparams.inputenc == "latin9") ||
569 (bparams.inputenc == "auto" &&
570 (font.language()->encoding()->latexName()
572 font.language()->encoding()->latexName()
574 os << "\\ensuremath{"
583 case '|': case '<': case '>':
584 // In T1 encoding, these characters exist
585 if (lyxrc.fontenc == "T1") {
587 //... but we should avoid ligatures
588 if ((c == '>' || c == '<')
590 && getChar(i + 1) == c) {
591 //os << "\\textcompwordmark{}";
592 // Jean-Marc, have a look at
593 // this. I think this works
601 // Typewriter font also has them
602 if (font.family() == LyXFont::TYPEWRITER_FAMILY) {
606 // Otherwise, we use what LaTeX
610 os << "\\textless{}";
614 os << "\\textgreater{}";
624 case '-': // "--" in Typewriter mode -> "-{}-"
626 && getChar(i + 1) == '-'
627 && font.family() == LyXFont::TYPEWRITER_FAMILY) {
636 os << "\\char`\\\"{}";
641 if (bparams.inputenc == "default") {
650 case '%': case '#': case '{':
657 os << "\\textasciitilde{}";
662 os << "\\textasciicircum{}";
667 // avoid being mistaken for optional arguments
668 os << '{' << c << '}';
673 // Blanks are printed before font switching.
674 // Sure? I am not! (try nice-latex)
675 // I am sure it's correct. LyX might be smarter
676 // in the future, but for now, nothing wrong is
682 // I assume this is hack treating typewriter as verbatim
683 if (font.family() == LyXFont::TYPEWRITER_FAMILY) {
692 // FIXME: if we have "LaTeX" with a font
693 // change in the middle (before the 'T', then
694 // the "TeX" part is still special cased.
695 // Really we should only operate this on
696 // "words" for some definition of word
700 for (; pnr < phrases_nr; ++pnr) {
701 if (isTextAt(special_phrases[pnr].phrase, i)) {
702 os << special_phrases[pnr].macro;
703 i += special_phrases[pnr].phrase.length() - 1;
704 column += special_phrases[pnr].macro.length() - 1;
709 if (pnr == phrases_nr && c != '\0') {
718 void Paragraph::Pimpl::validate(LaTeXFeatures & features,
719 LyXLayout const & layout) const
721 BufferParams const & bparams = features.bufferParams();
724 if (!params.spacing().isDefault())
725 features.require("setspace");
728 features.useLayout(layout.name());
731 Language const * doc_language = bparams.language;
733 FontList::const_iterator fcit = fontlist.begin();
734 FontList::const_iterator fend = fontlist.end();
735 for (; fcit != fend; ++fcit) {
736 if (fcit->font().noun() == LyXFont::ON) {
737 lyxerr[Debug::LATEX] << "font.noun: "
738 << fcit->font().noun()
740 features.require("noun");
741 lyxerr[Debug::LATEX] << "Noun enabled. Font: "
742 << fcit->font().stateText(0)
745 switch (fcit->font().color()) {
747 case LColor::inherit:
749 // probably we should put here all interface colors used for
750 // font displaying! For now I just add this ones I know of (Jug)
755 features.require("color");
756 lyxerr[Debug::LATEX] << "Color enabled. Font: "
757 << fcit->font().stateText(0)
761 Language const * language = fcit->font().language();
762 if (language->babel() != doc_language->babel() &&
763 language != ignore_language &&
764 language != latex_language)
766 features.useLanguage(language);
767 lyxerr[Debug::LATEX] << "Found language "
768 << language->babel() << endl;
772 if (!params.leftIndent().zero())
773 features.require("ParagraphLeftIndent");
776 InsetList::const_iterator icit = owner_->insetlist.begin();
777 InsetList::const_iterator iend = owner_->insetlist.end();
778 for (; icit != iend; ++icit) {
780 icit->inset->validate(features);
781 if (layout.needprotect &&
782 icit->inset->lyxCode() == InsetBase::FOOT_CODE)
783 features.require("NeedLyXFootnoteCode");
788 for (pos_type i = 0; i < size() ; ++i) {
789 for (size_t pnr = 0; pnr < phrases_nr; ++pnr) {
790 if (!special_phrases[pnr].builtin
791 && isTextAt(special_phrases[pnr].phrase, i)) {
792 features.require(special_phrases[pnr].phrase);