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"
14 #include "LaTeXFeatures.h"
17 #include "bufferparams.h"
21 #include "paragraph_funcs.h"
23 #include "support/LAssert.h"
28 using std::upper_bound;
29 using std::lower_bound;
31 // Initialize static member.
32 ShareContainer<LyXFont> Paragraph::Pimpl::FontTable::container;
33 // Initialization of the counter for the paragraph id's,
34 unsigned int Paragraph::Pimpl::paragraph_id = 0;
38 struct special_phrase {
44 special_phrase special_phrases[] = {
45 { "LyX", "\\LyX{}", false },
46 { "TeX", "\\TeX{}", true },
47 { "LaTeX2e", "\\LaTeXe{}", true },
48 { "LaTeX", "\\LaTeX{}", true },
51 size_t const phrases_nr = sizeof(special_phrases)/sizeof(special_phrase);
56 Paragraph::Pimpl::Pimpl(Paragraph * owner)
64 Paragraph::Pimpl::Pimpl(Pimpl const & p, Paragraph * owner, bool same_ids)
65 : params(p.params), owner_(owner)
67 inset_owner = p.inset_owner;
69 fontlist = p.fontlist;
76 changes_.reset(new Changes(*p.changes_.get()));
80 void Paragraph::Pimpl::clear()
87 void Paragraph::Pimpl::setContentsFromPar(Paragraph const * par)
90 text = par->pimpl_->text;
91 if (par->pimpl_->tracking()) {
92 changes_.reset(new Changes(*(par->pimpl_->changes_.get())));
97 void Paragraph::Pimpl::trackChanges(Change::Type type)
100 lyxerr[Debug::CHANGES] << "already tracking for par " << id_ << endl;
104 lyxerr[Debug::CHANGES] << "track changes for par "
105 << id_ << " type " << type << endl;
106 changes_.reset(new Changes(type));
107 changes_->set(type, 0, size());
111 void Paragraph::Pimpl::untrackChanges()
117 void Paragraph::Pimpl::cleanChanges()
119 // if we're not tracking, we don't want to reset...
123 changes_.reset(new Changes(Change::INSERTED));
124 changes_->set(Change::INSERTED, 0, size());
128 bool Paragraph::Pimpl::isChanged(pos_type start, pos_type end) const
133 return changes_->isChange(start, end);
137 bool Paragraph::Pimpl::isChangeEdited(pos_type start, pos_type end) const
142 return changes_->isChangeEdited(start, end);
146 void Paragraph::Pimpl::setChange(pos_type pos, Change::Type type)
151 changes_->set(type, pos);
155 Change::Type Paragraph::Pimpl::lookupChange(pos_type pos) const
158 return Change::UNCHANGED;
160 return changes_->lookup(pos);
164 Change const Paragraph::Pimpl::lookupChangeFull(pos_type pos) const
167 return Change(Change::UNCHANGED);
169 return changes_->lookupFull(pos);
173 void Paragraph::Pimpl::markErased()
175 lyx::Assert(tracking());
177 // FIXME: we should actually remove INSERTED chars.
178 // difficult because owning insettexts/tabulars need
179 // to update themselves when rows etc. change
180 changes_->set(Change::DELETED, 0, size());
181 changes_->reset(Change::DELETED);
185 void Paragraph::Pimpl::acceptChange(pos_type start, pos_type end)
191 changes_.reset(new Changes(Change::UNCHANGED));
195 lyxerr << "acceptchange" << endl;
198 for (; i < end; ++i) {
199 switch (lookupChange(i)) {
200 case Change::UNCHANGED:
203 case Change::INSERTED:
204 changes_->set(Change::UNCHANGED, i);
207 case Change::DELETED:
216 lyxerr << "endacceptchange" << endl;
217 changes_->reset(Change::UNCHANGED);
221 void Paragraph::Pimpl::rejectChange(pos_type start, pos_type end)
227 changes_.reset(new Changes(Change::UNCHANGED));
233 for (; i < end; ++i) {
234 switch (lookupChange(i)) {
235 case Change::UNCHANGED:
238 case Change::INSERTED:
245 case Change::DELETED:
246 changes_->set(Change::UNCHANGED, i);
250 changes_->reset(Change::UNCHANGED);
254 Paragraph::value_type Paragraph::Pimpl::getChar(pos_type pos) const
257 // This is in the critical path for loading!
258 pos_type const siz = size();
260 lyx::Assert(pos <= siz);
263 lyxerr << "getChar() on pos " << pos << " in par id "
264 << owner_->id() << " of size " << siz
265 << " is a bit silly !" << endl;
271 lyx::Assert(pos < size());
277 void Paragraph::Pimpl::setChar(pos_type pos, value_type c)
284 void Paragraph::Pimpl::insertChar(pos_type pos, value_type c,
285 LyXFont const & font, Change change)
287 lyx::Assert(pos <= size());
290 changes_->record(change, pos);
293 // This is actually very common when parsing buffers (and
294 // maybe inserting ascii text)
296 // when appending characters, no need to update tables
298 owner_->setFont(pos, font);
302 text.insert(text.begin() + pos, c);
304 // Update the font table.
305 FontTable search_font(pos, LyXFont());
306 for (FontList::iterator it = lower_bound(fontlist.begin(),
308 search_font, matchFT());
309 it != fontlist.end(); ++it)
311 it->pos(it->pos() + 1);
315 owner_->insetlist.increasePosAfterPos(pos);
317 owner_->setFont(pos, font);
321 void Paragraph::Pimpl::insertInset(pos_type pos,
322 Inset * inset, LyXFont const & font, Change change)
325 lyx::Assert(pos <= size());
327 insertChar(pos, META_INSET, font, change);
328 lyx::Assert(text[pos] == META_INSET);
330 // Add a new entry in the insetlist.
331 owner_->insetlist.insert(inset, pos);
332 inset->parOwner(owner_);
335 inset->setOwner(inset_owner);
339 bool Paragraph::Pimpl::erasePos(pos_type pos)
341 lyx::Assert(pos < size());
344 Change::Type changetype(changes_->lookup(pos));
345 changes_->record(Change(Change::DELETED), pos);
347 // only allow the actual removal if it was /new/ text
348 if (changetype != Change::INSERTED) {
349 if (text[pos] == Paragraph::META_INSET) {
350 Inset * i(owner_->getInset(pos));
362 void Paragraph::Pimpl::eraseIntern(pos_type pos)
364 // if it is an inset, delete the inset entry
365 if (text[pos] == Paragraph::META_INSET) {
366 owner_->insetlist.erase(pos);
369 text.erase(text.begin() + pos);
371 // Erase entries in the tables.
372 FontTable search_font(pos, LyXFont());
374 FontList::iterator it =
375 lower_bound(fontlist.begin(),
377 search_font, matchFT());
378 if (it != fontlist.end() && it->pos() == pos &&
380 (it != fontlist.begin()
381 && boost::prior(it)->pos() == pos - 1))) {
382 // If it is a multi-character font
383 // entry, we just make it smaller
384 // (see update below), otherwise we
386 unsigned int const i = it - fontlist.begin();
387 fontlist.erase(fontlist.begin() + i);
388 it = fontlist.begin() + i;
389 if (i > 0 && i < fontlist.size() &&
390 fontlist[i - 1].font() == fontlist[i].font()) {
391 fontlist.erase(fontlist.begin() + i - 1);
392 it = fontlist.begin() + i - 1;
396 // Update all other entries.
397 FontList::iterator fend = fontlist.end();
398 for (; it != fend; ++it)
399 it->pos(it->pos() - 1);
401 // Update the insetlist.
402 owner_->insetlist.decreasePosAfterPos(pos);
406 void Paragraph::Pimpl::erase(pos_type pos)
412 bool Paragraph::Pimpl::erase(pos_type start, pos_type end)
415 pos_type count = end - start;
416 bool any_erased = false;
430 void Paragraph::Pimpl::simpleTeXBlanks(ostream & os, TexRow & texrow,
432 unsigned int & column,
433 LyXFont const & font,
434 LyXLayout const & style)
439 if (column > lyxrc.ascii_linelen
441 && getChar(i - 1) != ' '
443 // same in FreeSpacing mode
444 && !style.free_spacing
445 && !owner_->isFreeSpacing()
446 // In typewriter mode, we want to avoid
447 // ! . ? : at the end of a line
448 && !(font.family() == LyXFont::TYPEWRITER_FAMILY
449 && (getChar(i - 1) == '.'
450 || getChar(i - 1) == '?'
451 || getChar(i - 1) == ':'
452 || getChar(i - 1) == '!'))) {
455 texrow.start(owner_, i + 1);
457 } else if (style.free_spacing) {
465 bool Paragraph::Pimpl::isTextAt(string const & str, pos_type pos) const
467 pos_type const len = str.length();
469 // is the paragraph large enough?
470 if (pos + len > size())
473 // does the wanted text start at point?
474 for (string::size_type i = 0; i < str.length(); ++i) {
475 if (str[i] != text[pos + i])
479 // is there a font change in middle of the word?
480 FontList::const_iterator cit = fontlist.begin();
481 FontList::const_iterator end = fontlist.end();
482 for (; cit != end; ++cit) {
483 if (cit->pos() >= pos)
486 if (cit != end && pos + len - 1 > cit->pos())
493 void Paragraph::Pimpl::simpleTeXSpecialChars(Buffer const * buf,
494 BufferParams const & bparams,
499 LyXFont & running_font,
501 LyXFont const & outerfont,
503 Change::Type & running_change,
504 LyXLayout const & style,
506 unsigned int & column,
509 if (style.pass_thru) {
510 if (c != Paragraph::META_INSET) {
514 Inset const * inset = owner_->getInset(i);
515 inset->ascii(buf, os, 0);
520 // Two major modes: LaTeX or plain
521 // Handle here those cases common to both modes
522 // and then split to handle the two modes separately.
524 case Paragraph::META_INSET: {
525 Inset * inset = owner_->getInset(i);
527 // FIXME: remove this check
531 // FIXME: move this to InsetNewline::latex
532 if (inset->lyxCode() == Inset::NEWLINE_CODE) {
533 // newlines are handled differently here than
534 // the default in simpleTeXSpecialChars().
535 if (!style.newline_allowed) {
539 column += running_font.latexWriteEndChanges(os, basefont, basefont);
542 basefont = owner_->getLayoutFont(bparams, outerfont);
543 running_font = basefont;
545 if (font.family() == LyXFont::TYPEWRITER_FAMILY)
554 texrow.start(owner_, i + 1);
559 if (inset->isTextInset()) {
560 column += Changes::latexMarkChange(os, running_change,
562 running_change = Change::UNCHANGED;
566 int const len = os.tellp();
567 //ostream::pos_type const len = os.tellp();
568 if ((inset->lyxCode() == Inset::GRAPHICS_CODE
569 || inset->lyxCode() == Inset::MATH_CODE
570 || inset->lyxCode() == Inset::URL_CODE)
571 && running_font.isRightToLeft()) {
577 #warning Bug: we can have an empty font change here!
578 // if there has just been a font change, we are going to close it
579 // right now, which means stupid latex code like \textsf{}. AFAIK,
580 // this does not harm dvi output. A minor bug, thus (JMarc)
582 // some insets cannot be inside a font change command
583 if (open_font && inset->noFontChange()) {
584 column +=running_font.
585 latexWriteEndChanges(os,
589 basefont = owner_->getLayoutFont(bparams, outerfont);
590 running_font = basefont;
593 int tmp = inset->latex(buf, os, moving_arg,
600 for (int j = 0; j < tmp; ++j) {
603 texrow.start(owner_, i + 1);
606 column += int(os.tellp()) - len;
612 // And now for the special cases within each mode
616 os << "\\textbackslash{}";
620 case '±': case '²': case '³':
621 case '×': case '÷': case '¹':
623 if ((bparams.inputenc == "latin1" ||
624 bparams.inputenc == "latin9") ||
625 (bparams.inputenc == "auto" &&
626 (font.language()->encoding()->LatexName()
628 font.language()->encoding()->LatexName()
630 os << "\\ensuremath{"
639 case '|': case '<': case '>':
640 // In T1 encoding, these characters exist
641 if (lyxrc.fontenc == "T1") {
643 //... but we should avoid ligatures
644 if ((c == '>' || c == '<')
646 && getChar(i + 1) == c) {
647 //os << "\\textcompwordmark{}";
648 // Jean-Marc, have a look at
649 // this. I think this works
657 // Typewriter font also has them
658 if (font.family() == LyXFont::TYPEWRITER_FAMILY) {
662 // Otherwise, we use what LaTeX
666 os << "\\textless{}";
670 os << "\\textgreater{}";
680 case '-': // "--" in Typewriter mode -> "-{}-"
682 && getChar(i + 1) == '-'
683 && font.family() == LyXFont::TYPEWRITER_FAMILY) {
692 os << "\\char`\\\"{}";
697 if (bparams.inputenc == "default") {
706 case '%': case '#': case '{':
713 os << "\\textasciitilde{}";
718 os << "\\textasciicircum{}";
722 case '*': case '[': case ']':
723 // avoid being mistaken for optional arguments
724 os << '{' << c << '}';
729 // Blanks are printed before font switching.
730 // Sure? I am not! (try nice-latex)
731 // I am sure it's correct. LyX might be smarter
732 // in the future, but for now, nothing wrong is
738 // I assume this is hack treating typewriter as verbatim
739 if (font.family() == LyXFont::TYPEWRITER_FAMILY) {
748 // FIXME: if we have "LaTeX" with a font
749 // change in the middle (before the 'T', then
750 // the "TeX" part is still special cased.
751 // Really we should only operate this on
752 // "words" for some definition of word
756 for (; pnr < phrases_nr; ++pnr) {
757 if (isTextAt(special_phrases[pnr].phrase, i)) {
758 os << special_phrases[pnr].macro;
759 i += special_phrases[pnr].phrase.length() - 1;
760 column += special_phrases[pnr].macro.length() - 1;
765 if (pnr == phrases_nr && c != '\0') {
774 void Paragraph::Pimpl::validate(LaTeXFeatures & features,
775 LyXLayout const & layout) const
777 BufferParams const & bparams = features.bufferParams();
780 if (params.lineTop() || params.lineBottom())
781 features.require("lyxline");
782 if (!params.spacing().isDefault())
783 features.require("setspace");
786 features.useLayout(layout.name());
789 Language const * doc_language = bparams.language;
791 FontList::const_iterator fcit = fontlist.begin();
792 FontList::const_iterator fend = fontlist.end();
793 for (; fcit != fend; ++fcit) {
794 if (fcit->font().noun() == LyXFont::ON) {
795 lyxerr[Debug::LATEX] << "font.noun: "
796 << fcit->font().noun()
798 features.require("noun");
799 lyxerr[Debug::LATEX] << "Noun enabled. Font: "
800 << fcit->font().stateText(0)
803 switch (fcit->font().color()) {
805 case LColor::inherit:
807 // probably we should put here all interface colors used for
808 // font displaying! For now I just add this ones I know of (Jug)
813 features.require("color");
814 lyxerr[Debug::LATEX] << "Color enabled. Font: "
815 << fcit->font().stateText(0)
819 Language const * language = fcit->font().language();
820 if (language->babel() != doc_language->babel() &&
821 language != ignore_language &&
822 language != latex_language)
824 features.useLanguage(language);
825 lyxerr[Debug::LATEX] << "Found language "
826 << language->babel() << endl;
830 if (!params.leftIndent().zero())
831 features.require("ParagraphLeftIndent");
834 InsetList::iterator icit = owner_->insetlist.begin();
835 InsetList::iterator iend = owner_->insetlist.end();
836 for (; icit != iend; ++icit) {
837 if (icit.getInset()) {
838 icit.getInset()->validate(features);
839 if (layout.needprotect &&
840 icit.getInset()->lyxCode() == Inset::FOOT_CODE)
841 features.require("NeedLyXFootnoteCode");
846 for (pos_type i = 0; i < size() ; ++i) {
847 for (size_t pnr = 0; pnr < phrases_nr; ++pnr) {
848 if (!special_phrases[pnr].builtin
849 && isTextAt(special_phrases[pnr].phrase, i)) {
850 features.require(special_phrases[pnr].phrase);