1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich
7 * Copyright 1995-2001 The LyX Team.
9 * ====================================================== */
13 #include "paragraph_pimpl.h"
15 #include "bufferparams.h"
19 #include "LaTeXFeatures.h"
20 #include "latexrunparams.h"
22 #include "paragraph_funcs.h"
25 #include "support/LAssert.h"
30 using std::upper_bound;
31 using std::lower_bound;
33 // Initialize static member.
34 ShareContainer<LyXFont> Paragraph::Pimpl::FontTable::container;
35 // Initialization of the counter for the paragraph id's,
36 unsigned int Paragraph::Pimpl::paragraph_id = 0;
40 struct special_phrase {
46 special_phrase special_phrases[] = {
47 { "LyX", "\\LyX{}", false },
48 { "TeX", "\\TeX{}", true },
49 { "LaTeX2e", "\\LaTeXe{}", true },
50 { "LaTeX", "\\LaTeX{}", true },
53 size_t const phrases_nr = sizeof(special_phrases)/sizeof(special_phrase);
58 Paragraph::Pimpl::Pimpl(Paragraph * owner)
66 Paragraph::Pimpl::Pimpl(Pimpl const & p, Paragraph * owner, bool same_ids)
67 : params(p.params), owner_(owner)
69 inset_owner = p.inset_owner;
71 fontlist = p.fontlist;
78 changes_.reset(new Changes(*p.changes_.get()));
82 void Paragraph::Pimpl::clear()
89 void Paragraph::Pimpl::setContentsFromPar(Paragraph const & par)
91 text = par.pimpl_->text;
92 if (par.pimpl_->tracking()) {
93 changes_.reset(new Changes(*(par.pimpl_->changes_.get())));
98 void Paragraph::Pimpl::trackChanges(Change::Type type)
101 lyxerr[Debug::CHANGES] << "already tracking for par " << id_ << endl;
105 lyxerr[Debug::CHANGES] << "track changes for par "
106 << id_ << " type " << type << endl;
107 changes_.reset(new Changes(type));
108 changes_->set(type, 0, size());
112 void Paragraph::Pimpl::untrackChanges()
118 void Paragraph::Pimpl::cleanChanges()
120 // if we're not tracking, we don't want to reset...
124 changes_.reset(new Changes(Change::INSERTED));
125 changes_->set(Change::INSERTED, 0, size());
129 bool Paragraph::Pimpl::isChanged(pos_type start, pos_type end) const
134 return changes_->isChange(start, end);
138 bool Paragraph::Pimpl::isChangeEdited(pos_type start, pos_type end) const
143 return changes_->isChangeEdited(start, end);
147 void Paragraph::Pimpl::setChange(pos_type pos, Change::Type type)
152 changes_->set(type, pos);
156 Change::Type Paragraph::Pimpl::lookupChange(pos_type pos) const
159 return Change::UNCHANGED;
161 return changes_->lookup(pos);
165 Change const Paragraph::Pimpl::lookupChangeFull(pos_type pos) const
168 return Change(Change::UNCHANGED);
170 return changes_->lookupFull(pos);
174 void Paragraph::Pimpl::markErased()
176 lyx::Assert(tracking());
178 // FIXME: we should actually remove INSERTED chars.
179 // difficult because owning insettexts/tabulars need
180 // to update themselves when rows etc. change
181 changes_->set(Change::DELETED, 0, size());
182 changes_->reset(Change::DELETED);
186 void Paragraph::Pimpl::acceptChange(pos_type start, pos_type end)
192 changes_.reset(new Changes(Change::UNCHANGED));
196 lyxerr << "acceptchange" << endl;
199 for (; i < end; ++i) {
200 switch (lookupChange(i)) {
201 case Change::UNCHANGED:
204 case Change::INSERTED:
205 changes_->set(Change::UNCHANGED, i);
208 case Change::DELETED:
217 lyxerr << "endacceptchange" << endl;
218 changes_->reset(Change::UNCHANGED);
222 void Paragraph::Pimpl::rejectChange(pos_type start, pos_type end)
228 changes_.reset(new Changes(Change::UNCHANGED));
234 for (; i < end; ++i) {
235 switch (lookupChange(i)) {
236 case Change::UNCHANGED:
239 case Change::INSERTED:
246 case Change::DELETED:
247 changes_->set(Change::UNCHANGED, i);
251 changes_->reset(Change::UNCHANGED);
255 Paragraph::value_type Paragraph::Pimpl::getChar(pos_type pos) const
258 // This is in the critical path for loading!
259 pos_type const siz = size();
261 lyx::Assert(pos <= siz);
264 lyxerr << "getChar() on pos " << pos << " in par id "
265 << owner_->id() << " of size " << siz
266 << " is a bit silly !" << endl;
272 lyx::Assert(pos < size());
278 void Paragraph::Pimpl::setChar(pos_type pos, value_type c)
285 void Paragraph::Pimpl::insertChar(pos_type pos, value_type c,
286 LyXFont const & font, Change change)
288 lyx::Assert(pos <= size());
291 changes_->record(change, pos);
294 // This is actually very common when parsing buffers (and
295 // maybe inserting ascii text)
297 // when appending characters, no need to update tables
299 owner_->setFont(pos, font);
303 text.insert(text.begin() + pos, c);
305 // Update the font table.
306 FontTable search_font(pos, LyXFont());
307 for (FontList::iterator it = lower_bound(fontlist.begin(),
309 search_font, matchFT());
310 it != fontlist.end(); ++it)
312 it->pos(it->pos() + 1);
316 owner_->insetlist.increasePosAfterPos(pos);
318 owner_->setFont(pos, font);
322 void Paragraph::Pimpl::insertInset(pos_type pos,
323 Inset * inset, LyXFont const & font, Change change)
326 lyx::Assert(pos <= size());
328 insertChar(pos, META_INSET, font, change);
329 lyx::Assert(text[pos] == META_INSET);
331 // Add a new entry in the insetlist.
332 owner_->insetlist.insert(inset, pos);
333 inset->parOwner(owner_);
336 inset->setOwner(inset_owner);
340 void Paragraph::Pimpl::eraseIntern(pos_type pos)
342 // if it is an inset, delete the inset entry
343 if (text[pos] == Paragraph::META_INSET) {
344 owner_->insetlist.erase(pos);
347 text.erase(text.begin() + pos);
349 // Erase entries in the tables.
350 FontTable search_font(pos, LyXFont());
352 FontList::iterator it =
353 lower_bound(fontlist.begin(),
355 search_font, matchFT());
356 if (it != fontlist.end() && it->pos() == pos &&
358 (it != fontlist.begin()
359 && boost::prior(it)->pos() == pos - 1))) {
360 // If it is a multi-character font
361 // entry, we just make it smaller
362 // (see update below), otherwise we
364 unsigned int const i = it - fontlist.begin();
365 fontlist.erase(fontlist.begin() + i);
366 it = fontlist.begin() + i;
367 if (i > 0 && i < fontlist.size() &&
368 fontlist[i - 1].font() == fontlist[i].font()) {
369 fontlist.erase(fontlist.begin() + i - 1);
370 it = fontlist.begin() + i - 1;
374 // Update all other entries.
375 FontList::iterator fend = fontlist.end();
376 for (; it != fend; ++it)
377 it->pos(it->pos() - 1);
379 // Update the insetlist.
380 owner_->insetlist.decreasePosAfterPos(pos);
384 bool Paragraph::Pimpl::erase(pos_type pos)
386 lyx::Assert(pos < size());
389 Change::Type changetype(changes_->lookup(pos));
390 changes_->record(Change(Change::DELETED), pos);
392 // only allow the actual removal if it was /new/ text
393 if (changetype != Change::INSERTED) {
394 if (text[pos] == Paragraph::META_INSET) {
395 Inset * i(owner_->getInset(pos));
407 int Paragraph::Pimpl::erase(pos_type start, pos_type end)
410 pos_type count = end - start;
421 void Paragraph::Pimpl::simpleTeXBlanks(ostream & os, TexRow & texrow,
423 unsigned int & column,
424 LyXFont const & font,
425 LyXLayout const & style)
430 if (column > lyxrc.ascii_linelen
432 && getChar(i - 1) != ' '
434 // same in FreeSpacing mode
435 && !style.free_spacing
436 && !owner_->isFreeSpacing()
437 // In typewriter mode, we want to avoid
438 // ! . ? : at the end of a line
439 && !(font.family() == LyXFont::TYPEWRITER_FAMILY
440 && (getChar(i - 1) == '.'
441 || getChar(i - 1) == '?'
442 || getChar(i - 1) == ':'
443 || getChar(i - 1) == '!'))) {
446 texrow.start(owner_->id(), i + 1);
448 } else if (style.free_spacing) {
456 bool Paragraph::Pimpl::isTextAt(string const & str, pos_type pos) const
458 pos_type const len = str.length();
460 // is the paragraph large enough?
461 if (pos + len > size())
464 // does the wanted text start at point?
465 for (string::size_type i = 0; i < str.length(); ++i) {
466 if (str[i] != text[pos + i])
470 // is there a font change in middle of the word?
471 FontList::const_iterator cit = fontlist.begin();
472 FontList::const_iterator end = fontlist.end();
473 for (; cit != end; ++cit) {
474 if (cit->pos() >= pos)
477 if (cit != end && pos + len - 1 > cit->pos())
484 void Paragraph::Pimpl::simpleTeXSpecialChars(Buffer const * buf,
485 BufferParams const & bparams,
488 LatexRunParams const & runparams,
490 LyXFont & running_font,
492 LyXFont const & outerfont,
494 Change::Type & running_change,
495 LyXLayout const & style,
497 unsigned int & column,
500 if (style.pass_thru) {
501 if (c != Paragraph::META_INSET) {
505 Inset const * inset = owner_->getInset(i);
506 inset->ascii(buf, os, 0);
511 // Two major modes: LaTeX or plain
512 // Handle here those cases common to both modes
513 // and then split to handle the two modes separately.
515 case Paragraph::META_INSET: {
516 Inset * inset = owner_->getInset(i);
518 // FIXME: remove this check
522 // FIXME: move this to InsetNewline::latex
523 if (inset->lyxCode() == Inset::NEWLINE_CODE) {
524 // newlines are handled differently here than
525 // the default in simpleTeXSpecialChars().
526 if (!style.newline_allowed) {
530 column += running_font.latexWriteEndChanges(os, basefont, basefont);
533 basefont = owner_->getLayoutFont(bparams, outerfont);
534 running_font = basefont;
536 if (font.family() == LyXFont::TYPEWRITER_FAMILY)
539 if (runparams.moving_arg)
545 texrow.start(owner_->id(), i + 1);
550 if (inset->isTextInset()) {
551 column += Changes::latexMarkChange(os, running_change,
553 running_change = Change::UNCHANGED;
557 int const len = os.tellp();
558 //ostream::pos_type const len = os.tellp();
559 if ((inset->lyxCode() == Inset::GRAPHICS_CODE
560 || inset->lyxCode() == Inset::MATH_CODE
561 || inset->lyxCode() == Inset::URL_CODE)
562 && running_font.isRightToLeft()) {
568 #warning Bug: we can have an empty font change here!
569 // if there has just been a font change, we are going to close it
570 // right now, which means stupid latex code like \textsf{}. AFAIK,
571 // this does not harm dvi output. A minor bug, thus (JMarc)
573 // some insets cannot be inside a font change command
574 if (open_font && inset->noFontChange()) {
575 column +=running_font.
576 latexWriteEndChanges(os,
580 basefont = owner_->getLayoutFont(bparams, outerfont);
581 running_font = basefont;
584 int tmp = inset->latex(buf, os, runparams);
590 for (int j = 0; j < tmp; ++j) {
593 texrow.start(owner_->id(), i + 1);
596 column += int(os.tellp()) - len;
602 // And now for the special cases within each mode
606 os << "\\textbackslash{}";
610 case '±': case '²': case '³':
611 case '×': case '÷': case '¹':
613 if ((bparams.inputenc == "latin1" ||
614 bparams.inputenc == "latin9") ||
615 (bparams.inputenc == "auto" &&
616 (font.language()->encoding()->LatexName()
618 font.language()->encoding()->LatexName()
620 os << "\\ensuremath{"
629 case '|': case '<': case '>':
630 // In T1 encoding, these characters exist
631 if (lyxrc.fontenc == "T1") {
633 //... but we should avoid ligatures
634 if ((c == '>' || c == '<')
636 && getChar(i + 1) == c) {
637 //os << "\\textcompwordmark{}";
638 // Jean-Marc, have a look at
639 // this. I think this works
647 // Typewriter font also has them
648 if (font.family() == LyXFont::TYPEWRITER_FAMILY) {
652 // Otherwise, we use what LaTeX
656 os << "\\textless{}";
660 os << "\\textgreater{}";
670 case '-': // "--" in Typewriter mode -> "-{}-"
672 && getChar(i + 1) == '-'
673 && font.family() == LyXFont::TYPEWRITER_FAMILY) {
682 os << "\\char`\\\"{}";
687 if (bparams.inputenc == "default") {
696 case '%': case '#': case '{':
703 os << "\\textasciitilde{}";
708 os << "\\textasciicircum{}";
712 case '*': case '[': case ']':
713 // avoid being mistaken for optional arguments
714 os << '{' << c << '}';
719 // Blanks are printed before font switching.
720 // Sure? I am not! (try nice-latex)
721 // I am sure it's correct. LyX might be smarter
722 // in the future, but for now, nothing wrong is
728 // I assume this is hack treating typewriter as verbatim
729 if (font.family() == LyXFont::TYPEWRITER_FAMILY) {
738 // FIXME: if we have "LaTeX" with a font
739 // change in the middle (before the 'T', then
740 // the "TeX" part is still special cased.
741 // Really we should only operate this on
742 // "words" for some definition of word
746 for (; pnr < phrases_nr; ++pnr) {
747 if (isTextAt(special_phrases[pnr].phrase, i)) {
748 os << special_phrases[pnr].macro;
749 i += special_phrases[pnr].phrase.length() - 1;
750 column += special_phrases[pnr].macro.length() - 1;
755 if (pnr == phrases_nr && c != '\0') {
764 void Paragraph::Pimpl::validate(LaTeXFeatures & features,
765 LyXLayout const & layout) const
767 BufferParams const & bparams = features.bufferParams();
770 if (params.lineTop() || params.lineBottom())
771 features.require("lyxline");
772 if (!params.spacing().isDefault())
773 features.require("setspace");
776 features.useLayout(layout.name());
779 Language const * doc_language = bparams.language;
781 FontList::const_iterator fcit = fontlist.begin();
782 FontList::const_iterator fend = fontlist.end();
783 for (; fcit != fend; ++fcit) {
784 if (fcit->font().noun() == LyXFont::ON) {
785 lyxerr[Debug::LATEX] << "font.noun: "
786 << fcit->font().noun()
788 features.require("noun");
789 lyxerr[Debug::LATEX] << "Noun enabled. Font: "
790 << fcit->font().stateText(0)
793 switch (fcit->font().color()) {
795 case LColor::inherit:
797 // probably we should put here all interface colors used for
798 // font displaying! For now I just add this ones I know of (Jug)
803 features.require("color");
804 lyxerr[Debug::LATEX] << "Color enabled. Font: "
805 << fcit->font().stateText(0)
809 Language const * language = fcit->font().language();
810 if (language->babel() != doc_language->babel() &&
811 language != ignore_language &&
812 language != latex_language)
814 features.useLanguage(language);
815 lyxerr[Debug::LATEX] << "Found language "
816 << language->babel() << endl;
820 if (!params.leftIndent().zero())
821 features.require("ParagraphLeftIndent");
824 InsetList::iterator icit = owner_->insetlist.begin();
825 InsetList::iterator iend = owner_->insetlist.end();
826 for (; icit != iend; ++icit) {
827 if (icit.getInset()) {
828 icit.getInset()->validate(features);
829 if (layout.needprotect &&
830 icit.getInset()->lyxCode() == Inset::FOOT_CODE)
831 features.require("NeedLyXFootnoteCode");
836 for (pos_type i = 0; i < size() ; ++i) {
837 for (size_t pnr = 0; pnr < phrases_nr; ++pnr) {
838 if (!special_phrases[pnr].builtin
839 && isTextAt(special_phrases[pnr].phrase, i)) {
840 features.require(special_phrases[pnr].phrase);