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":
172 lyxerr[Debug::CHANGES] << "endacceptchange" << endl;
173 changes_->reset(Change::UNCHANGED);
177 void Paragraph::Pimpl::rejectChange(pos_type start, pos_type end)
183 changes_.reset(new Changes(Change::UNCHANGED));
189 for (; i < end; ++i) {
190 switch (lookupChange(i).type) {
191 case Change::UNCHANGED:
194 case Change::INSERTED:
202 case Change::DELETED:
203 changes_->set(Change::UNCHANGED, i);
204 // No real char at position size():
205 if (i < size() && owner_->isInset(i))
206 // FIXME: change tracking (MG)
207 owner_->getInset(i)->setChange(Change(Change::UNCHANGED));
211 changes_->reset(Change::UNCHANGED);
215 Paragraph::value_type Paragraph::Pimpl::getChar(pos_type pos) const
217 return owner_->getChar(pos);
221 void Paragraph::Pimpl::insertChar(pos_type pos, value_type c, Change const & change)
223 BOOST_ASSERT(pos <= size());
226 changes_->record(change, pos);
229 // This is actually very common when parsing buffers (and
230 // maybe inserting ascii text)
232 // when appending characters, no need to update tables
233 owner_->text_.push_back(c);
237 owner_->text_.insert(owner_->text_.begin() + pos, c);
239 // Update the font table.
240 FontTable search_font(pos, LyXFont());
241 for (FontList::iterator it = lower_bound(fontlist.begin(),
243 search_font, matchFT());
244 it != fontlist.end(); ++it)
246 it->pos(it->pos() + 1);
250 owner_->insetlist.increasePosAfterPos(pos);
254 void Paragraph::Pimpl::insertInset(pos_type pos,
255 InsetBase * inset, Change const & change)
258 BOOST_ASSERT(pos <= size());
260 insertChar(pos, META_INSET, change);
261 BOOST_ASSERT(owner_->text_[pos] == META_INSET);
263 // Add a new entry in the insetlist.
264 owner_->insetlist.insert(inset, pos);
268 void Paragraph::Pimpl::erase(pos_type pos)
270 // FIXME: change tracking (MG)
271 // do something like changes_->erase(i);
272 // in one of the next patches, the two erase functions
273 // will be merged but I don't want to break too many things at the same time :-)
275 // if it is an inset, delete the inset entry
276 if (owner_->text_[pos] == Paragraph::META_INSET) {
277 owner_->insetlist.erase(pos);
280 owner_->text_.erase(owner_->text_.begin() + pos);
282 // Erase entries in the tables.
283 FontTable search_font(pos, LyXFont());
285 FontList::iterator it =
286 lower_bound(fontlist.begin(),
288 search_font, matchFT());
289 if (it != fontlist.end() && it->pos() == pos &&
291 (it != fontlist.begin()
292 && boost::prior(it)->pos() == pos - 1))) {
293 // If it is a multi-character font
294 // entry, we just make it smaller
295 // (see update below), otherwise we
297 unsigned int const i = it - fontlist.begin();
298 fontlist.erase(fontlist.begin() + i);
299 it = fontlist.begin() + i;
300 if (i > 0 && i < fontlist.size() &&
301 fontlist[i - 1].font() == fontlist[i].font()) {
302 fontlist.erase(fontlist.begin() + i - 1);
303 it = fontlist.begin() + i - 1;
307 // Update all other entries
308 FontList::iterator fend = fontlist.end();
309 for (; it != fend; ++it)
310 it->pos(it->pos() - 1);
312 // Update the insetlist
313 owner_->insetlist.decreasePosAfterPos(pos);
317 bool Paragraph::Pimpl::erase(pos_type pos, bool trackChanges)
319 // FIXME: change tracking (MG)
320 BOOST_ASSERT(pos <= size());
323 Change::Type changetype(changes_->lookup(pos).type);
325 // only allow the actual removal if it was /new/ text
326 if (changetype != Change::INSERTED) {
327 changes_->record(Change(Change::DELETED), pos);
328 if (pos < size() && owner_->isInset(pos))
329 // FIXME: change tracking (MG)
330 owner_->getInset(pos)->setChange(Change(Change::DELETED));
335 // Don't physically access nonexistent end-of-paragraph char
345 int Paragraph::Pimpl::erase(pos_type start, pos_type end, bool trackChanges)
348 for (pos_type count = end - start; count; --count) {
349 if (!erase(i, trackChanges))
356 void Paragraph::Pimpl::simpleTeXBlanks(odocstream & os, TexRow & texrow,
358 unsigned int & column,
359 LyXFont const & font,
360 LyXLayout const & style)
365 if (column > lyxrc.ascii_linelen
367 && getChar(i - 1) != ' '
369 // same in FreeSpacing mode
370 && !owner_->isFreeSpacing()
371 // In typewriter mode, we want to avoid
372 // ! . ? : at the end of a line
373 && !(font.family() == LyXFont::TYPEWRITER_FAMILY
374 && (getChar(i - 1) == '.'
375 || getChar(i - 1) == '?'
376 || getChar(i - 1) == ':'
377 || getChar(i - 1) == '!'))) {
380 texrow.start(owner_->id(), i + 1);
382 } else if (style.free_spacing) {
390 bool Paragraph::Pimpl::isTextAt(string const & str, pos_type pos) const
392 pos_type const len = str.length();
394 // is the paragraph large enough?
395 if (pos + len > size())
398 // does the wanted text start at point?
399 for (string::size_type i = 0; i < str.length(); ++i) {
400 if (str[i] != owner_->text_[pos + i])
404 // is there a font change in middle of the word?
405 FontList::const_iterator cit = fontlist.begin();
406 FontList::const_iterator end = fontlist.end();
407 for (; cit != end; ++cit) {
408 if (cit->pos() >= pos)
411 if (cit != end && pos + len - 1 > cit->pos())
418 void Paragraph::Pimpl::simpleTeXSpecialChars(Buffer const & buf,
419 BufferParams const & bparams,
422 OutputParams const & runparams,
424 LyXFont & running_font,
426 LyXFont const & outerfont,
428 Change::Type & running_change,
429 LyXLayout const & style,
431 unsigned int & column,
434 if (style.pass_thru) {
435 if (c != Paragraph::META_INSET) {
439 owner_->getInset(i)->plaintext(buf, os, runparams);
443 // Two major modes: LaTeX or plain
444 // Handle here those cases common to both modes
445 // and then split to handle the two modes separately.
447 case Paragraph::META_INSET: {
448 InsetBase * inset = owner_->getInset(i);
450 // FIXME: remove this check
454 // FIXME: move this to InsetNewline::latex
455 if (inset->lyxCode() == InsetBase::NEWLINE_CODE) {
456 // newlines are handled differently here than
457 // the default in simpleTeXSpecialChars().
458 if (!style.newline_allowed) {
462 column += running_font.latexWriteEndChanges(os, basefont, basefont);
465 basefont = owner_->getLayoutFont(bparams, outerfont);
466 running_font = basefont;
468 if (font.family() == LyXFont::TYPEWRITER_FAMILY)
471 if (runparams.moving_arg)
477 texrow.start(owner_->id(), i + 1);
482 // output change tracking marks only if desired,
483 // if dvipost is installed,
484 // and with dvi/ps (other formats don't work)
485 LaTeXFeatures features(buf, bparams, runparams);
486 bool const output = bparams.outputChanges
487 && runparams.flavor == OutputParams::LATEX
488 && features.isAvailable("dvipost");
490 if (inset->canTrackChanges()) {
491 column += Changes::latexMarkChange(os, running_change,
492 Change::UNCHANGED, output);
493 running_change = Change::UNCHANGED;
497 odocstream::pos_type const len = os.tellp();
499 if ((inset->lyxCode() == InsetBase::GRAPHICS_CODE
500 || inset->lyxCode() == InsetBase::MATH_CODE
501 || inset->lyxCode() == InsetBase::URL_CODE)
502 && running_font.isRightToLeft()) {
508 #warning Bug: we can have an empty font change here!
509 // if there has just been a font change, we are going to close it
510 // right now, which means stupid latex code like \textsf{}. AFAIK,
511 // this does not harm dvi output. A minor bug, thus (JMarc)
513 // some insets cannot be inside a font change command
514 if (open_font && inset->noFontChange()) {
515 column +=running_font.
516 latexWriteEndChanges(os,
520 basefont = owner_->getLayoutFont(bparams, outerfont);
521 running_font = basefont;
524 int tmp = inset->latex(buf, os, runparams);
530 for (int j = 0; j < tmp; ++j) {
533 texrow.start(owner_->id(), i + 1);
536 column += os.tellp() - len;
542 // And now for the special cases within each mode
546 os << "\\textbackslash{}";
550 case '±': case '²': case '³':
551 case '×': case '÷': case '¹':
553 if ((bparams.inputenc == "latin1" ||
554 bparams.inputenc == "latin9") ||
555 (bparams.inputenc == "auto" &&
556 (font.language()->encoding()->latexName()
558 font.language()->encoding()->latexName()
560 os << "\\ensuremath{";
569 case '|': case '<': case '>':
570 // In T1 encoding, these characters exist
571 if (lyxrc.fontenc == "T1") {
573 //... but we should avoid ligatures
574 if ((c == '>' || c == '<')
576 && getChar(i + 1) == c) {
577 //os << "\\textcompwordmark{}";
578 // Jean-Marc, have a look at
579 // this. I think this works
587 // Typewriter font also has them
588 if (font.family() == LyXFont::TYPEWRITER_FAMILY) {
592 // Otherwise, we use what LaTeX
596 os << "\\textless{}";
600 os << "\\textgreater{}";
610 case '-': // "--" in Typewriter mode -> "-{}-"
612 && getChar(i + 1) == '-'
613 && font.family() == LyXFont::TYPEWRITER_FAMILY) {
622 os << "\\char`\\\"{}";
627 if (bparams.inputenc == "default") {
636 case '%': case '#': case '{':
644 os << "\\textasciitilde{}";
649 os << "\\textasciicircum{}";
654 // avoid being mistaken for optional arguments
662 // Blanks are printed before font switching.
663 // Sure? I am not! (try nice-latex)
664 // I am sure it's correct. LyX might be smarter
665 // in the future, but for now, nothing wrong is
671 // I assume this is hack treating typewriter as verbatim
672 if (font.family() == LyXFont::TYPEWRITER_FAMILY) {
681 // FIXME: if we have "LaTeX" with a font
682 // change in the middle (before the 'T', then
683 // the "TeX" part is still special cased.
684 // Really we should only operate this on
685 // "words" for some definition of word
689 for (; pnr < phrases_nr; ++pnr) {
690 if (isTextAt(special_phrases[pnr].phrase, i)) {
691 os << special_phrases[pnr].macro;
692 i += special_phrases[pnr].phrase.length() - 1;
693 column += special_phrases[pnr].macro.length() - 1;
698 if (pnr == phrases_nr && c != '\0') {
707 void Paragraph::Pimpl::validate(LaTeXFeatures & features,
708 LyXLayout const & layout) const
710 BufferParams const & bparams = features.bufferParams();
713 if (!params.spacing().isDefault())
714 features.require("setspace");
717 features.useLayout(layout.name());
720 Language const * doc_language = bparams.language;
722 FontList::const_iterator fcit = fontlist.begin();
723 FontList::const_iterator fend = fontlist.end();
724 for (; fcit != fend; ++fcit) {
725 if (fcit->font().noun() == LyXFont::ON) {
726 lyxerr[Debug::LATEX] << "font.noun: "
727 << fcit->font().noun()
729 features.require("noun");
730 lyxerr[Debug::LATEX] << "Noun enabled. Font: "
731 << fcit->font().stateText(0)
734 switch (fcit->font().color()) {
736 case LColor::inherit:
738 // probably we should put here all interface colors used for
739 // font displaying! For now I just add this ones I know of (Jug)
744 features.require("color");
745 lyxerr[Debug::LATEX] << "Color enabled. Font: "
746 << fcit->font().stateText(0)
750 Language const * language = fcit->font().language();
751 if (language->babel() != doc_language->babel() &&
752 language != ignore_language &&
753 language != latex_language)
755 features.useLanguage(language);
756 lyxerr[Debug::LATEX] << "Found language "
757 << language->babel() << endl;
761 if (!params.leftIndent().zero())
762 features.require("ParagraphLeftIndent");
765 InsetList::const_iterator icit = owner_->insetlist.begin();
766 InsetList::const_iterator iend = owner_->insetlist.end();
767 for (; icit != iend; ++icit) {
769 icit->inset->validate(features);
770 if (layout.needprotect &&
771 icit->inset->lyxCode() == InsetBase::FOOT_CODE)
772 features.require("NeedLyXFootnoteCode");
777 for (pos_type i = 0; i < size() ; ++i) {
778 for (size_t pnr = 0; pnr < phrases_nr; ++pnr) {
779 if (!special_phrases[pnr].builtin
780 && isTextAt(special_phrases[pnr].phrase, i)) {
781 features.require(special_phrases[pnr].phrase);