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>
36 using std::upper_bound;
37 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 const special_phrases[] = {
53 { "LyX", from_ascii("\\LyX{}"), false },
54 { "TeX", from_ascii("\\TeX{}"), true },
55 { "LaTeX2e", from_ascii("\\LaTeXe{}"), true },
56 { "LaTeX", from_ascii("\\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), changes_(p.changes_), owner_(owner)
75 inset_owner = p.inset_owner;
76 fontlist = p.fontlist;
81 void Paragraph::Pimpl::setContentsFromPar(Paragraph const & par)
83 owner_->text_ = par.text_;
84 // FIXME: change tracking (MG)
85 changes_ = par.pimpl_->changes_;
89 bool Paragraph::Pimpl::isChanged(pos_type start, pos_type end) const
91 // FIXME: change tracking (MG)
94 return changes_.isChanged(start, end);
98 void Paragraph::Pimpl::setChange(Change const & change)
100 // FIXME: change tracking (MG)
104 changes_.set(change, 0, size());
106 if (change.type == Change::UNCHANGED) { // only for UNCHANGED ???
107 for (pos_type i = 0; i < size(); ++i) {
108 if (owner_->isInset(i)) {
109 owner_->getInset(i)->setChange(change);
116 void Paragraph::Pimpl::setChange(pos_type pos, Change const & change)
118 // FIXME: change tracking (MG)
121 changes_.set(change, pos);
125 Change const Paragraph::Pimpl::lookupChange(pos_type pos) const
127 // FIXME: change tracking (MG)
128 return Change(Change::UNCHANGED);
130 return changes_.lookup(pos);
134 void Paragraph::Pimpl::acceptChange(pos_type start, pos_type end)
136 // FIXME: change tracking (MG)
139 // care for empty pars
141 lyxerr[Debug::CHANGES] << "acceptchange" << endl;
144 for (; i < end; ++i) {
145 switch (lookupChange(i).type) {
146 case Change::UNCHANGED:
149 case Change::INSERTED:
150 // FIXME: change tracking (MG)
151 changes_.set(Change(Change::UNCHANGED), i);
154 case Change::DELETED:
155 // Suppress access to nonexistent
156 // "end-of-paragraph char":
166 lyxerr[Debug::CHANGES] << "endacceptchange" << endl;
167 // FIXME: change tracking (MG)
168 // changes_.reset(Change::UNCHANGED);
172 void Paragraph::Pimpl::rejectChange(pos_type start, pos_type end)
174 // FIXME: change tracking (MG)
177 // care for empty pars
181 for (; i < end; ++i) {
182 switch (lookupChange(i).type) {
183 case Change::UNCHANGED:
186 case Change::INSERTED:
194 case Change::DELETED:
195 // FIXME: change tracking (MG)
196 changes_.set(Change(Change::UNCHANGED), i);
197 // No real char at position size():
198 if (i < size() && owner_->isInset(i))
199 // FIXME: change tracking (MG)
200 owner_->getInset(i)->setChange(Change(Change::UNCHANGED));
204 // FIXME: change tracking (MG)
205 // changes_.reset(Change::UNCHANGED);
209 Paragraph::value_type Paragraph::Pimpl::getChar(pos_type pos) const
211 return owner_->getChar(pos);
215 void Paragraph::Pimpl::insertChar(pos_type pos, value_type c, Change const & change)
217 BOOST_ASSERT(pos <= size());
219 // FIXME: change tracking (MG)
221 // changes_.record(change, pos);
224 // This is actually very common when parsing buffers (and
225 // maybe inserting ascii text)
227 // when appending characters, no need to update tables
228 owner_->text_.push_back(c);
232 owner_->text_.insert(owner_->text_.begin() + pos, c);
234 // Update the font table.
235 FontTable search_font(pos, LyXFont());
236 for (FontList::iterator it = lower_bound(fontlist.begin(),
238 search_font, matchFT());
239 it != fontlist.end(); ++it)
241 it->pos(it->pos() + 1);
245 owner_->insetlist.increasePosAfterPos(pos);
249 void Paragraph::Pimpl::insertInset(pos_type pos,
250 InsetBase * inset, Change const & change)
253 BOOST_ASSERT(pos <= size());
255 insertChar(pos, META_INSET, change);
256 BOOST_ASSERT(owner_->text_[pos] == META_INSET);
258 // Add a new entry in the insetlist.
259 owner_->insetlist.insert(inset, pos);
263 void Paragraph::Pimpl::erase(pos_type pos)
265 // FIXME: change tracking (MG)
266 // do something like changes_.erase(i);
267 // in one of the next patches, the two erase functions
268 // will be merged but I don't want to break too many things at the same time :-)
270 // if it is an inset, delete the inset entry
271 if (owner_->text_[pos] == Paragraph::META_INSET) {
272 owner_->insetlist.erase(pos);
275 owner_->text_.erase(owner_->text_.begin() + pos);
277 // Erase entries in the tables.
278 FontTable search_font(pos, LyXFont());
280 FontList::iterator it =
281 lower_bound(fontlist.begin(),
283 search_font, matchFT());
284 if (it != fontlist.end() && it->pos() == pos &&
286 (it != fontlist.begin()
287 && boost::prior(it)->pos() == pos - 1))) {
288 // If it is a multi-character font
289 // entry, we just make it smaller
290 // (see update below), otherwise we
292 unsigned int const i = it - fontlist.begin();
293 fontlist.erase(fontlist.begin() + i);
294 it = fontlist.begin() + i;
295 if (i > 0 && i < fontlist.size() &&
296 fontlist[i - 1].font() == fontlist[i].font()) {
297 fontlist.erase(fontlist.begin() + i - 1);
298 it = fontlist.begin() + i - 1;
302 // Update all other entries
303 FontList::iterator fend = fontlist.end();
304 for (; it != fend; ++it)
305 it->pos(it->pos() - 1);
307 // Update the insetlist
308 owner_->insetlist.decreasePosAfterPos(pos);
312 bool Paragraph::Pimpl::erase(pos_type pos, bool /*trackChanges*/)
314 BOOST_ASSERT(pos <= size());
316 // FIXME: change tracking (MG)
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 // FIXME: change tracking (MG)
325 owner_->getInset(pos)->setChange(Change(Change::DELETED));
330 // Don't physically access nonexistent end-of-paragraph char
340 int Paragraph::Pimpl::erase(pos_type start, pos_type end, bool trackChanges)
343 for (pos_type count = end - start; count; --count) {
344 if (!erase(i, trackChanges))
351 void Paragraph::Pimpl::simpleTeXBlanks(odocstream & os, TexRow & texrow,
353 unsigned int & column,
354 LyXFont const & font,
355 LyXLayout const & style)
360 if (column > lyxrc.ascii_linelen
362 && getChar(i - 1) != ' '
364 // same in FreeSpacing mode
365 && !owner_->isFreeSpacing()
366 // In typewriter mode, we want to avoid
367 // ! . ? : at the end of a line
368 && !(font.family() == LyXFont::TYPEWRITER_FAMILY
369 && (getChar(i - 1) == '.'
370 || getChar(i - 1) == '?'
371 || getChar(i - 1) == ':'
372 || getChar(i - 1) == '!'))) {
375 texrow.start(owner_->id(), i + 1);
377 } else if (style.free_spacing) {
385 bool Paragraph::Pimpl::isTextAt(string const & str, pos_type pos) const
387 pos_type const len = str.length();
389 // is the paragraph large enough?
390 if (pos + len > size())
393 // does the wanted text start at point?
394 for (string::size_type i = 0; i < str.length(); ++i) {
395 if (str[i] != owner_->text_[pos + i])
399 // is there a font change in middle of the word?
400 FontList::const_iterator cit = fontlist.begin();
401 FontList::const_iterator end = fontlist.end();
402 for (; cit != end; ++cit) {
403 if (cit->pos() >= pos)
406 if (cit != end && pos + len - 1 > cit->pos())
413 void Paragraph::Pimpl::simpleTeXSpecialChars(Buffer const & buf,
414 BufferParams const & bparams,
417 OutputParams const & runparams,
419 LyXFont & running_font,
421 LyXFont const & outerfont,
423 Change::Type & running_change,
424 LyXLayout const & style,
426 unsigned int & column,
429 if (style.pass_thru) {
430 if (c != Paragraph::META_INSET) {
434 owner_->getInset(i)->plaintext(buf, os, runparams);
438 // Two major modes: LaTeX or plain
439 // Handle here those cases common to both modes
440 // and then split to handle the two modes separately.
442 case Paragraph::META_INSET: {
443 InsetBase * inset = owner_->getInset(i);
445 // FIXME: remove this check
449 // FIXME: move this to InsetNewline::latex
450 if (inset->lyxCode() == InsetBase::NEWLINE_CODE) {
451 // newlines are handled differently here than
452 // the default in simpleTeXSpecialChars().
453 if (!style.newline_allowed) {
457 column += running_font.latexWriteEndChanges(os, basefont, basefont);
460 basefont = owner_->getLayoutFont(bparams, outerfont);
461 running_font = basefont;
463 if (font.family() == LyXFont::TYPEWRITER_FAMILY)
466 if (runparams.moving_arg)
472 texrow.start(owner_->id(), i + 1);
477 // output change tracking marks only if desired,
478 // if dvipost is installed,
479 // and with dvi/ps (other formats don't work)
480 LaTeXFeatures features(buf, bparams, runparams);
481 bool const output = bparams.outputChanges
482 && runparams.flavor == OutputParams::LATEX
483 && features.isAvailable("dvipost");
485 if (inset->canTrackChanges()) {
486 column += Changes::latexMarkChange(os, running_change,
487 Change::UNCHANGED, output);
488 running_change = Change::UNCHANGED;
492 odocstream::pos_type const len = os.tellp();
494 if ((inset->lyxCode() == InsetBase::GRAPHICS_CODE
495 || inset->lyxCode() == InsetBase::MATH_CODE
496 || inset->lyxCode() == InsetBase::URL_CODE)
497 && running_font.isRightToLeft()) {
503 #warning Bug: we can have an empty font change here!
504 // if there has just been a font change, we are going to close it
505 // right now, which means stupid latex code like \textsf{}. AFAIK,
506 // this does not harm dvi output. A minor bug, thus (JMarc)
508 // some insets cannot be inside a font change command
509 if (open_font && inset->noFontChange()) {
510 column +=running_font.
511 latexWriteEndChanges(os,
515 basefont = owner_->getLayoutFont(bparams, outerfont);
516 running_font = basefont;
519 int tmp = inset->latex(buf, os, runparams);
525 for (int j = 0; j < tmp; ++j) {
528 texrow.start(owner_->id(), i + 1);
531 column += os.tellp() - len;
537 // And now for the special cases within each mode
541 os << "\\textbackslash{}";
545 // The following characters could be written literally in latin1, but they
546 // would be wrongly converted on systems where char is signed, so we give
548 // This also makes us independant from the encoding of this source file.
549 case 0xb1: // ± PLUS-MINUS SIGN
550 case 0xb2: // ² SUPERSCRIPT TWO
551 case 0xb3: // ³ SUPERSCRIPT THREE
552 case 0xd7: // × MULTIPLICATION SIGN
553 case 0xf7: // ÷ DIVISION SIGN
554 case 0xb9: // ¹ SUPERSCRIPT ONE
555 case 0xac: // ¬ NOT SIGN
556 case 0xb5: // µ MICRO SIGN
557 if ((bparams.inputenc == "latin1" ||
558 bparams.inputenc == "latin9") ||
559 (bparams.inputenc == "auto" &&
560 (font.language()->encoding()->latexName()
562 font.language()->encoding()->latexName()
564 os << "\\ensuremath{";
573 case '|': case '<': case '>':
574 // In T1 encoding, these characters exist
575 if (lyxrc.fontenc == "T1") {
577 //... but we should avoid ligatures
578 if ((c == '>' || c == '<')
580 && getChar(i + 1) == c) {
581 //os << "\\textcompwordmark{}";
582 // Jean-Marc, have a look at
583 // this. I think this works
591 // Typewriter font also has them
592 if (font.family() == LyXFont::TYPEWRITER_FAMILY) {
596 // Otherwise, we use what LaTeX
600 os << "\\textless{}";
604 os << "\\textgreater{}";
614 case '-': // "--" in Typewriter mode -> "-{}-"
616 && getChar(i + 1) == '-'
617 && font.family() == LyXFont::TYPEWRITER_FAMILY) {
626 os << "\\char`\\\"{}";
630 case 0xa3: // £ POUND SIGN
631 if (bparams.inputenc == "default") {
640 case '%': case '#': case '{':
648 os << "\\textasciitilde{}";
653 os << "\\textasciicircum{}";
658 // avoid being mistaken for optional arguments
666 // Blanks are printed before font switching.
667 // Sure? I am not! (try nice-latex)
668 // I am sure it's correct. LyX might be smarter
669 // in the future, but for now, nothing wrong is
675 // I assume this is hack treating typewriter as verbatim
676 if (font.family() == LyXFont::TYPEWRITER_FAMILY) {
685 // FIXME: if we have "LaTeX" with a font
686 // change in the middle (before the 'T', then
687 // the "TeX" part is still special cased.
688 // Really we should only operate this on
689 // "words" for some definition of word
693 for (; pnr < phrases_nr; ++pnr) {
694 if (isTextAt(special_phrases[pnr].phrase, i)) {
695 os << special_phrases[pnr].macro;
696 i += special_phrases[pnr].phrase.length() - 1;
697 column += special_phrases[pnr].macro.length() - 1;
702 if (pnr == phrases_nr && c != '\0') {
711 void Paragraph::Pimpl::validate(LaTeXFeatures & features,
712 LyXLayout const & layout) const
714 BufferParams const & bparams = features.bufferParams();
717 if (!params.spacing().isDefault())
718 features.require("setspace");
721 features.useLayout(layout.name());
724 Language const * doc_language = bparams.language;
726 FontList::const_iterator fcit = fontlist.begin();
727 FontList::const_iterator fend = fontlist.end();
728 for (; fcit != fend; ++fcit) {
729 if (fcit->font().noun() == LyXFont::ON) {
730 lyxerr[Debug::LATEX] << "font.noun: "
731 << fcit->font().noun()
733 features.require("noun");
734 lyxerr[Debug::LATEX] << "Noun enabled. Font: "
735 << fcit->font().stateText(0)
738 switch (fcit->font().color()) {
740 case LColor::inherit:
742 // probably we should put here all interface colors used for
743 // font displaying! For now I just add this ones I know of (Jug)
748 features.require("color");
749 lyxerr[Debug::LATEX] << "Color enabled. Font: "
750 << fcit->font().stateText(0)
754 Language const * language = fcit->font().language();
755 if (language->babel() != doc_language->babel() &&
756 language != ignore_language &&
757 language != latex_language)
759 features.useLanguage(language);
760 lyxerr[Debug::LATEX] << "Found language "
761 << language->babel() << endl;
765 if (!params.leftIndent().zero())
766 features.require("ParagraphLeftIndent");
769 InsetList::const_iterator icit = owner_->insetlist.begin();
770 InsetList::const_iterator iend = owner_->insetlist.end();
771 for (; icit != iend; ++icit) {
773 icit->inset->validate(features);
774 if (layout.needprotect &&
775 icit->inset->lyxCode() == InsetBase::FOOT_CODE)
776 features.require("NeedLyXFootnoteCode");
781 for (pos_type i = 0; i < size() ; ++i) {
782 for (size_t pnr = 0; pnr < phrases_nr; ++pnr) {
783 if (!special_phrases[pnr].builtin
784 && isTextAt(special_phrases[pnr].phrase, i)) {
785 features.require(special_phrases[pnr].phrase);