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 * ====================================================== */
14 #pragma implementation
17 #include "paragraph_pimpl.h"
18 #include "LaTeXFeatures.h"
21 #include "bufferparams.h"
26 #include "support/LAssert.h"
31 using std::upper_bound;
32 using std::lower_bound;
34 // Initialize static member.
35 ShareContainer<LyXFont> Paragraph::Pimpl::FontTable::container;
36 // Initialization of the counter for the paragraph id's,
37 unsigned int Paragraph::Pimpl::paragraph_id = 0;
41 struct special_phrase {
47 special_phrase special_phrases[] = {
48 { "LyX", "\\LyX{}", false },
49 { "TeX", "\\TeX{}", true },
50 { "LaTeX2e", "\\LaTeXe{}", true },
51 { "LaTeX", "\\LaTeX{}", true },
54 size_t const phrases_nr = sizeof(special_phrases)/sizeof(special_phrase);
59 Paragraph::Pimpl::Pimpl(Paragraph * owner)
67 Paragraph::Pimpl::Pimpl(Pimpl const & p, Paragraph * owner, bool same_ids)
68 : params(p.params), owner_(owner)
70 inset_owner = p.inset_owner;
72 fontlist = p.fontlist;
79 changes_.reset(new Changes(*p.changes_.get()));
83 void Paragraph::Pimpl::clear()
90 void Paragraph::Pimpl::setContentsFromPar(Paragraph const * par)
93 text = par->pimpl_->text;
94 if (par->pimpl_->tracking()) {
95 changes_.reset(new Changes(*(par->pimpl_->changes_.get())));
100 void Paragraph::Pimpl::trackChanges(Change::Type type)
103 lyxerr[Debug::CHANGES] << "already tracking for par " << id_ << endl;
107 lyxerr[Debug::CHANGES] << "track changes for par "
108 << id_ << " type " << type << endl;
109 changes_.reset(new Changes(type));
110 changes_->set(type, 0, size());
114 void Paragraph::Pimpl::untrackChanges()
120 void Paragraph::Pimpl::cleanChanges()
122 // if we're not tracking, we don't want to reset...
126 changes_.reset(new Changes(Change::INSERTED));
127 changes_->set(Change::INSERTED, 0, size());
131 bool Paragraph::Pimpl::isChanged(pos_type start, pos_type end) const
136 return changes_->isChange(start, end);
140 bool Paragraph::Pimpl::isChangeEdited(pos_type start, pos_type end) const
145 return changes_->isChangeEdited(start, end);
149 void Paragraph::Pimpl::setChange(pos_type pos, Change::Type type)
154 changes_->set(type, pos);
158 Change::Type Paragraph::Pimpl::lookupChange(pos_type pos) const
161 return Change::UNCHANGED;
163 return changes_->lookup(pos);
167 Change const Paragraph::Pimpl::lookupChangeFull(pos_type pos) const
170 return Change(Change::UNCHANGED);
172 return changes_->lookupFull(pos);
176 void Paragraph::Pimpl::markErased()
178 lyx::Assert(tracking());
180 // FIXME: we should actually remove INSERTED chars.
181 // difficult because owning insettexts/tabulars need
182 // to update themselves when rows etc. change
183 changes_->set(Change::DELETED, 0, size());
184 changes_->reset(Change::DELETED);
188 void Paragraph::Pimpl::acceptChange(pos_type start, pos_type end)
194 changes_.reset(new Changes(Change::UNCHANGED));
198 lyxerr << "acceptchange" << endl;
201 for (; i < end; ++i) {
202 switch (lookupChange(i)) {
203 case Change::UNCHANGED:
206 case Change::INSERTED:
207 changes_->set(Change::UNCHANGED, i);
210 case Change::DELETED:
219 lyxerr << "endacceptchange" << endl;
220 changes_->reset(Change::UNCHANGED);
224 void Paragraph::Pimpl::rejectChange(pos_type start, pos_type end)
230 changes_.reset(new Changes(Change::UNCHANGED));
236 for (; i < end; ++i) {
237 switch (lookupChange(i)) {
238 case Change::UNCHANGED:
241 case Change::INSERTED:
248 case Change::DELETED:
249 changes_->set(Change::UNCHANGED, i);
253 changes_->reset(Change::UNCHANGED);
257 Paragraph::value_type Paragraph::Pimpl::getChar(pos_type pos) const
259 // This is in the critical path for loading!
260 pos_type const siz = size();
261 lyx::Assert(pos <= siz);
262 // This is stronger, and I belive that this is the assertion
263 // that we should really use. (Lgb)
264 //Assert(pos < size());
266 // Then this has no meaning. (Lgb)
267 if (!siz || pos == siz)
274 void Paragraph::Pimpl::setChar(pos_type pos, value_type c)
281 void Paragraph::Pimpl::insertChar(pos_type pos, value_type c,
282 LyXFont const & font, Change change)
284 lyx::Assert(pos <= size());
287 changes_->record(change, pos);
290 // This is actually very common when parsing buffers (and
291 // maybe inserting ascii text)
293 // when appending characters, no need to update tables
295 owner_->setFont(pos, font);
299 text.insert(text.begin() + pos, c);
301 // Update the font table.
302 FontTable search_font(pos, LyXFont());
303 for (FontList::iterator it = lower_bound(fontlist.begin(),
305 search_font, matchFT());
306 it != fontlist.end(); ++it)
308 it->pos(it->pos() + 1);
312 owner_->insetlist.increasePosAfterPos(pos);
314 owner_->setFont(pos, font);
318 void Paragraph::Pimpl::insertInset(pos_type pos,
319 Inset * inset, LyXFont const & font, Change change)
322 lyx::Assert(pos <= size());
324 insertChar(pos, META_INSET, font, change);
325 lyx::Assert(text[pos] == META_INSET);
327 // Add a new entry in the insetlist.
328 owner_->insetlist.insert(inset, pos);
329 inset->parOwner(owner_);
332 inset->setOwner(inset_owner);
336 bool Paragraph::Pimpl::erasePos(pos_type pos)
338 lyx::Assert(pos < size());
341 Change::Type changetype(changes_->lookup(pos));
342 changes_->record(Change(Change::DELETED), pos);
344 // only allow the actual removal if it was /new/ text
345 if (changetype != Change::INSERTED) {
346 if (text[pos] == Paragraph::META_INSET) {
347 Inset * i(owner_->getInset(pos));
359 void Paragraph::Pimpl::eraseIntern(pos_type pos)
361 // if it is an inset, delete the inset entry
362 if (text[pos] == Paragraph::META_INSET) {
363 owner_->insetlist.erase(pos);
366 text.erase(text.begin() + pos);
368 // Erase entries in the tables.
369 FontTable search_font(pos, LyXFont());
371 FontList::iterator it =
372 lower_bound(fontlist.begin(),
374 search_font, matchFT());
375 if (it != fontlist.end() && it->pos() == pos &&
377 (it != fontlist.begin()
378 && boost::prior(it)->pos() == pos - 1))) {
379 // If it is a multi-character font
380 // entry, we just make it smaller
381 // (see update below), otherwise we
383 unsigned int const i = it - fontlist.begin();
384 fontlist.erase(fontlist.begin() + i);
385 it = fontlist.begin() + i;
386 if (i > 0 && i < fontlist.size() &&
387 fontlist[i - 1].font() == fontlist[i].font()) {
388 fontlist.erase(fontlist.begin() + i - 1);
389 it = fontlist.begin() + i - 1;
393 // Update all other entries.
394 FontList::iterator fend = fontlist.end();
395 for (; it != fend; ++it)
396 it->pos(it->pos() - 1);
398 // Update the insetlist.
399 owner_->insetlist.decreasePosAfterPos(pos);
403 void Paragraph::Pimpl::erase(pos_type pos)
409 bool Paragraph::Pimpl::erase(pos_type start, pos_type end)
412 pos_type count = end - start;
413 bool any_erased = false;
427 void Paragraph::Pimpl::simpleTeXBlanks(ostream & os, TexRow & texrow,
429 unsigned int & column,
430 LyXFont const & font,
431 LyXLayout const & style)
433 if (style.pass_thru) return;
434 if (column > lyxrc.ascii_linelen
436 && getChar(i - 1) != ' '
438 // same in FreeSpacing mode
439 && !style.free_spacing
440 && !owner_->isFreeSpacing()
441 // In typewriter mode, we want to avoid
442 // ! . ? : at the end of a line
443 && !(font.family() == LyXFont::TYPEWRITER_FAMILY
444 && (getChar(i - 1) == '.'
445 || getChar(i - 1) == '?'
446 || getChar(i - 1) == ':'
447 || getChar(i - 1) == '!'))) {
450 texrow.start(owner_, i + 1);
452 } else if (style.free_spacing) {
460 bool Paragraph::Pimpl::isTextAt(string const & str, pos_type pos) const
462 pos_type const len = str.length();
464 // is the paragraph large enough?
465 if (pos + len > size())
468 // does the wanted text start at point?
469 for (string::size_type i = 0; i < str.length(); ++i) {
470 if (str[i] != text[pos + i])
474 // is there a font change in middle of the word?
475 FontList::const_iterator cit = fontlist.begin();
476 FontList::const_iterator end = fontlist.end();
477 for (; cit != end; ++cit) {
478 if (cit->pos() >= pos)
481 if (cit != end && pos + len - 1 > cit->pos())
488 void Paragraph::Pimpl::simpleTeXSpecialChars(Buffer const * buf,
489 BufferParams const & bparams,
494 LyXFont & running_font,
497 Change::Type & running_change,
498 LyXLayout const & style,
500 unsigned int & column,
503 if (style.pass_thru) {
504 if (c != '\0') os << c;
507 // Two major modes: LaTeX or plain
508 // Handle here those cases common to both modes
509 // and then split to handle the two modes separately.
511 case Paragraph::META_INSET: {
512 Inset * inset = owner_->getInset(i);
514 // FIXME: remove this check
518 if (inset->isTextInset()) {
519 column += Changes::latexMarkChange(os, running_change,
521 running_change = Change::UNCHANGED;
525 int const len = os.tellp();
526 //ostream::pos_type const len = os.tellp();
527 if ((inset->lyxCode() == Inset::GRAPHICS_CODE
528 || inset->lyxCode() == Inset::MATH_CODE
529 || inset->lyxCode() == Inset::URL_CODE)
530 && running_font.isRightToLeft()) {
536 #warning Bug: we can have an empty font change here!
537 // if there has just been a font change, we are going to close it
538 // right now, which means stupid latex code like \textsf{}. AFAIK,
539 // this does not harm dvi output. A minor bug, thus (JMarc)
541 // some insets cannot be inside a font change command
542 if (open_font && inset->noFontChange()) {
543 column +=running_font.
544 latexWriteEndChanges(os,
548 basefont = owner_->getLayoutFont(bparams);
549 running_font = basefont;
552 int tmp = inset->latex(buf, os, moving_arg,
559 for (int j = 0; j < tmp; ++j) {
562 texrow.start(owner_, i + 1);
565 column += int(os.tellp()) - len;
570 case Paragraph::META_NEWLINE:
572 column += running_font.latexWriteEndChanges(os,
577 basefont = owner_->getLayoutFont(bparams);
578 running_font = basefont;
581 case Paragraph::META_HFILL:
587 // And now for the special cases within each mode
591 os << "\\textbackslash{}";
595 case '±': case '²': case '³':
596 case '×': case '÷': case '¹':
597 case '¬': case 'µ':
598 if ((bparams.inputenc == "latin1" ||
599 bparams.inputenc == "latin9") ||
600 (bparams.inputenc == "auto" &&
601 (font.language()->encoding()->LatexName()
603 font.language()->encoding()->LatexName()
605 os << "\\ensuremath{"
614 case '|': case '<': case '>':
615 // In T1 encoding, these characters exist
616 if (lyxrc.fontenc == "T1") {
618 //... but we should avoid ligatures
619 if ((c == '>' || c == '<')
621 && getChar(i + 1) == c) {
622 //os << "\\textcompwordmark{}";
623 // Jean-Marc, have a look at
624 // this. IÂ think this works
632 // Typewriter font also has them
633 if (font.family() == LyXFont::TYPEWRITER_FAMILY) {
637 // Otherwise, we use what LaTeX
641 os << "\\textless{}";
645 os << "\\textgreater{}";
655 case '-': // "--" in Typewriter mode -> "-{}-"
657 && getChar(i + 1) == '-'
658 && font.family() == LyXFont::TYPEWRITER_FAMILY) {
667 os << "\\char`\\\"{}";
672 if (bparams.inputenc == "default") {
681 case '%': case '#': case '{':
688 os << "\\textasciitilde{}";
693 os << "\\textasciicircum{}";
697 case '*': case '[': case ']':
698 // avoid being mistaken for optional arguments
699 os << '{' << c << '}';
704 // Blanks are printed before font switching.
705 // Sure? I am not! (try nice-latex)
706 // I am sure it's correct. LyX might be smarter
707 // in the future, but for now, nothing wrong is
713 // I assume this is hack treating typewriter as verbatim
714 if (font.family() == LyXFont::TYPEWRITER_FAMILY) {
723 // FIXME: if we have "LaTeX" with a font
724 // change in the middle (before the 'T', then
725 // the "TeX" part is still special cased.
726 // Really we should only operate this on
727 // "words" for some definition of word
731 for (; pnr < phrases_nr; ++pnr) {
732 if (isTextAt(special_phrases[pnr].phrase, i)) {
733 os << special_phrases[pnr].macro;
734 i += special_phrases[pnr].phrase.length() - 1;
735 column += special_phrases[pnr].macro.length() - 1;
740 if (pnr == phrases_nr && c != '\0') {
750 Paragraph * Paragraph::Pimpl::TeXDeeper(Buffer const * buf,
751 BufferParams const & bparams,
752 ostream & os, TexRow & texrow)
754 lyxerr[Debug::LATEX] << "TeXDeeper... " << this << endl;
755 Paragraph * par = owner_;
757 while (par && par->params().depth() == owner_->params().depth()) {
758 if (par->layout()->isEnvironment()) {
759 par = par->TeXEnvironment(buf, bparams,
762 par = par->TeXOnePar(buf, bparams,
766 lyxerr[Debug::LATEX] << "TeXDeeper...done " << par << endl;
772 void Paragraph::Pimpl::validate(LaTeXFeatures & features,
773 LyXLayout const & layout) const
775 BufferParams const & bparams = features.bufferParams();
778 if (params.lineTop() || params.lineBottom())
779 features.require("lyxline");
780 if (!params.spacing().isDefault())
781 features.require("setspace");
784 features.useLayout(layout.name());
787 Language const * doc_language = bparams.language;
789 FontList::const_iterator fcit = fontlist.begin();
790 FontList::const_iterator fend = fontlist.end();
791 for (; fcit != fend; ++fcit) {
792 if (fcit->font().noun() == LyXFont::ON) {
793 lyxerr[Debug::LATEX] << "font.noun: "
794 << fcit->font().noun()
796 features.require("noun");
797 lyxerr[Debug::LATEX] << "Noun enabled. Font: "
798 << fcit->font().stateText(0)
801 switch (fcit->font().color()) {
803 case LColor::inherit:
805 // probably we should put here all interface colors used for
806 // font displaying! For now I just add this ones I know of (Jug)
811 features.require("color");
812 lyxerr[Debug::LATEX] << "Color enabled. Font: "
813 << fcit->font().stateText(0)
817 Language const * language = fcit->font().language();
818 if (language->babel() != doc_language->babel() &&
819 language != ignore_language &&
820 language != latex_language)
822 features.useLanguage(language);
823 lyxerr[Debug::LATEX] << "Found language "
824 << language->babel() << endl;
828 if (!params.leftIndent().zero())
829 features.require("ParagraphLeftIndent");
832 InsetList::iterator icit = owner_->insetlist.begin();
833 InsetList::iterator iend = owner_->insetlist.end();
834 for (; icit != iend; ++icit) {
835 if (icit.getInset()) {
836 icit.getInset()->validate(features);
837 if (layout.needprotect &&
838 icit.getInset()->lyxCode() == Inset::FOOT_CODE)
839 features.require("NeedLyXFootnoteCode");
844 for (pos_type i = 0; i < size() ; ++i) {
845 for (size_t pnr = 0; pnr < phrases_nr; ++pnr) {
846 if (!special_phrases[pnr].builtin
847 && isTextAt(special_phrases[pnr].phrase, i)) {
848 features.require(special_phrases[pnr].phrase);
856 Paragraph * Paragraph::Pimpl::getParFromID(int id) const
858 InsetList::iterator cit = owner_->insetlist.begin();
859 InsetList::iterator lend = owner_->insetlist.end();
861 for (; cit != lend; ++cit) {
862 if ((result = cit.getInset()->getParFromID(id)))
869 LyXFont const Paragraph::Pimpl::realizeFont(LyXFont const & font,
870 BufferParams const & bparams) const
872 LyXFont tmpfont(font);
874 // check for environment font information
875 char par_depth = owner_->getDepth();
876 Paragraph const * par = owner_;
877 LyXTextClass const & tclass = bparams.getLyXTextClass();
879 while (par && par->getDepth() && !tmpfont.resolved()) {
880 par = par->outerHook();
882 tmpfont.realize(par->layout()->font);
883 par_depth = par->getDepth();
887 tmpfont.realize(tclass.defaultfont());