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"
27 using namespace lyx::support;
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)
68 : params(p.params), owner_(owner)
70 inset_owner = p.inset_owner;
72 fontlist = p.fontlist;
76 changes_.reset(new Changes(*p.changes_.get()));
80 void Paragraph::Pimpl::clear()
87 void Paragraph::Pimpl::setContentsFromPar(Paragraph const & par)
89 text = par.pimpl_->text;
90 if (par.pimpl_->tracking()) {
91 changes_.reset(new Changes(*(par.pimpl_->changes_.get())));
96 void Paragraph::Pimpl::trackChanges(Change::Type type)
99 lyxerr[Debug::CHANGES] << "already tracking for par " << id_ << endl;
103 lyxerr[Debug::CHANGES] << "track changes for par "
104 << id_ << " type " << type << endl;
105 changes_.reset(new Changes(type));
106 changes_->set(type, 0, size());
110 void Paragraph::Pimpl::untrackChanges()
116 void Paragraph::Pimpl::cleanChanges()
118 // if we're not tracking, we don't want to reset...
122 changes_.reset(new Changes(Change::INSERTED));
123 changes_->set(Change::INSERTED, 0, size());
127 bool Paragraph::Pimpl::isChanged(pos_type start, pos_type end) const
132 return changes_->isChange(start, end);
136 bool Paragraph::Pimpl::isChangeEdited(pos_type start, pos_type end) const
141 return changes_->isChangeEdited(start, end);
145 void Paragraph::Pimpl::setChange(pos_type pos, Change::Type type)
150 changes_->set(type, pos);
154 Change::Type Paragraph::Pimpl::lookupChange(pos_type pos) const
157 return Change::UNCHANGED;
159 return changes_->lookup(pos);
163 Change const Paragraph::Pimpl::lookupChangeFull(pos_type pos) const
166 return Change(Change::UNCHANGED);
168 return changes_->lookupFull(pos);
172 void Paragraph::Pimpl::markErased()
176 // FIXME: we should actually remove INSERTED chars.
177 // difficult because owning insettexts/tabulars need
178 // to update themselves when rows etc. change
179 changes_->set(Change::DELETED, 0, size());
180 changes_->reset(Change::DELETED);
184 void Paragraph::Pimpl::acceptChange(pos_type start, pos_type end)
190 changes_.reset(new Changes(Change::UNCHANGED));
194 lyxerr << "acceptchange" << endl;
197 for (; i < end; ++i) {
198 switch (lookupChange(i)) {
199 case Change::UNCHANGED:
202 case Change::INSERTED:
203 changes_->set(Change::UNCHANGED, i);
206 case Change::DELETED:
215 lyxerr << "endacceptchange" << endl;
216 changes_->reset(Change::UNCHANGED);
220 void Paragraph::Pimpl::rejectChange(pos_type start, pos_type end)
226 changes_.reset(new Changes(Change::UNCHANGED));
232 for (; i < end; ++i) {
233 switch (lookupChange(i)) {
234 case Change::UNCHANGED:
237 case Change::INSERTED:
244 case Change::DELETED:
245 changes_->set(Change::UNCHANGED, i);
249 changes_->reset(Change::UNCHANGED);
253 Paragraph::value_type Paragraph::Pimpl::getChar(pos_type pos) const
256 // This is in the critical path for loading!
257 pos_type const siz = size();
262 lyxerr << "getChar() on pos " << pos << " in par id "
263 << owner_->id() << " of size " << siz
264 << " is a bit silly !" << endl;
270 Assert(pos < size());
276 void Paragraph::Pimpl::setChar(pos_type pos, value_type c)
283 void Paragraph::Pimpl::insertChar(pos_type pos, value_type c,
284 LyXFont const & font, Change change)
286 Assert(pos <= size());
289 changes_->record(change, pos);
292 // This is actually very common when parsing buffers (and
293 // maybe inserting ascii text)
295 // when appending characters, no need to update tables
297 owner_->setFont(pos, font);
301 text.insert(text.begin() + pos, c);
303 // Update the font table.
304 FontTable search_font(pos, LyXFont());
305 for (FontList::iterator it = lower_bound(fontlist.begin(),
307 search_font, matchFT());
308 it != fontlist.end(); ++it)
310 it->pos(it->pos() + 1);
314 owner_->insetlist.increasePosAfterPos(pos);
316 owner_->setFont(pos, font);
320 void Paragraph::Pimpl::insertInset(pos_type pos,
321 Inset * inset, LyXFont const & font, Change change)
324 Assert(pos <= size());
326 insertChar(pos, META_INSET, font, change);
327 Assert(text[pos] == META_INSET);
329 // Add a new entry in the insetlist.
330 owner_->insetlist.insert(inset, pos);
331 inset->parOwner(owner_);
334 inset->setOwner(inset_owner);
338 void Paragraph::Pimpl::eraseIntern(pos_type pos)
340 // if it is an inset, delete the inset entry
341 if (text[pos] == Paragraph::META_INSET) {
342 owner_->insetlist.erase(pos);
345 text.erase(text.begin() + pos);
347 // Erase entries in the tables.
348 FontTable search_font(pos, LyXFont());
350 FontList::iterator it =
351 lower_bound(fontlist.begin(),
353 search_font, matchFT());
354 if (it != fontlist.end() && it->pos() == pos &&
356 (it != fontlist.begin()
357 && boost::prior(it)->pos() == pos - 1))) {
358 // If it is a multi-character font
359 // entry, we just make it smaller
360 // (see update below), otherwise we
362 unsigned int const i = it - fontlist.begin();
363 fontlist.erase(fontlist.begin() + i);
364 it = fontlist.begin() + i;
365 if (i > 0 && i < fontlist.size() &&
366 fontlist[i - 1].font() == fontlist[i].font()) {
367 fontlist.erase(fontlist.begin() + i - 1);
368 it = fontlist.begin() + i - 1;
372 // Update all other entries.
373 FontList::iterator fend = fontlist.end();
374 for (; it != fend; ++it)
375 it->pos(it->pos() - 1);
377 // Update the insetlist.
378 owner_->insetlist.decreasePosAfterPos(pos);
382 bool Paragraph::Pimpl::erase(pos_type pos)
384 Assert(pos < size());
387 Change::Type changetype(changes_->lookup(pos));
388 changes_->record(Change(Change::DELETED), pos);
390 // only allow the actual removal if it was /new/ text
391 if (changetype != Change::INSERTED) {
392 if (text[pos] == Paragraph::META_INSET) {
393 Inset * i(owner_->getInset(pos));
405 int Paragraph::Pimpl::erase(pos_type start, pos_type end)
408 pos_type count = end - start;
419 void Paragraph::Pimpl::simpleTeXBlanks(ostream & os, TexRow & texrow,
421 unsigned int & column,
422 LyXFont const & font,
423 LyXLayout const & style)
428 if (column > lyxrc.ascii_linelen
430 && getChar(i - 1) != ' '
432 // same in FreeSpacing mode
433 && !owner_->isFreeSpacing()
434 // In typewriter mode, we want to avoid
435 // ! . ? : at the end of a line
436 && !(font.family() == LyXFont::TYPEWRITER_FAMILY
437 && (getChar(i - 1) == '.'
438 || getChar(i - 1) == '?'
439 || getChar(i - 1) == ':'
440 || getChar(i - 1) == '!'))) {
443 texrow.start(owner_->id(), i + 1);
445 } else if (style.free_spacing) {
453 bool Paragraph::Pimpl::isTextAt(string const & str, pos_type pos) const
455 pos_type const len = str.length();
457 // is the paragraph large enough?
458 if (pos + len > size())
461 // does the wanted text start at point?
462 for (string::size_type i = 0; i < str.length(); ++i) {
463 if (str[i] != text[pos + i])
467 // is there a font change in middle of the word?
468 FontList::const_iterator cit = fontlist.begin();
469 FontList::const_iterator end = fontlist.end();
470 for (; cit != end; ++cit) {
471 if (cit->pos() >= pos)
474 if (cit != end && pos + len - 1 > cit->pos())
481 void Paragraph::Pimpl::simpleTeXSpecialChars(Buffer const * buf,
482 BufferParams const & bparams,
485 LatexRunParams const & runparams,
487 LyXFont & running_font,
489 LyXFont const & outerfont,
491 Change::Type & running_change,
492 LyXLayout const & style,
494 unsigned int & column,
497 if (style.pass_thru) {
498 if (c != Paragraph::META_INSET) {
502 Inset const * inset = owner_->getInset(i);
503 inset->ascii(buf, os, 0);
508 // Two major modes: LaTeX or plain
509 // Handle here those cases common to both modes
510 // and then split to handle the two modes separately.
512 case Paragraph::META_INSET: {
513 Inset * inset = owner_->getInset(i);
515 // FIXME: remove this check
519 // FIXME: move this to InsetNewline::latex
520 if (inset->lyxCode() == Inset::NEWLINE_CODE) {
521 // newlines are handled differently here than
522 // the default in simpleTeXSpecialChars().
523 if (!style.newline_allowed) {
527 column += running_font.latexWriteEndChanges(os, basefont, basefont);
530 basefont = owner_->getLayoutFont(bparams, outerfont);
531 running_font = basefont;
533 if (font.family() == LyXFont::TYPEWRITER_FAMILY)
536 if (runparams.moving_arg)
542 texrow.start(owner_->id(), i + 1);
547 if (inset->isTextInset()) {
548 column += Changes::latexMarkChange(os, running_change,
550 running_change = Change::UNCHANGED;
554 int const len = os.tellp();
555 //ostream::pos_type const len = os.tellp();
556 if ((inset->lyxCode() == Inset::GRAPHICS_CODE
557 || inset->lyxCode() == Inset::MATH_CODE
558 || inset->lyxCode() == Inset::URL_CODE)
559 && running_font.isRightToLeft()) {
565 #warning Bug: we can have an empty font change here!
566 // if there has just been a font change, we are going to close it
567 // right now, which means stupid latex code like \textsf{}. AFAIK,
568 // this does not harm dvi output. A minor bug, thus (JMarc)
570 // some insets cannot be inside a font change command
571 if (open_font && inset->noFontChange()) {
572 column +=running_font.
573 latexWriteEndChanges(os,
577 basefont = owner_->getLayoutFont(bparams, outerfont);
578 running_font = basefont;
581 int tmp = inset->latex(buf, os, runparams);
587 for (int j = 0; j < tmp; ++j) {
590 texrow.start(owner_->id(), i + 1);
593 column += int(os.tellp()) - len;
599 // And now for the special cases within each mode
603 os << "\\textbackslash{}";
607 case '±': case '²': case '³':
608 case '×': case '÷': case '¹':
610 if ((bparams.inputenc == "latin1" ||
611 bparams.inputenc == "latin9") ||
612 (bparams.inputenc == "auto" &&
613 (font.language()->encoding()->LatexName()
615 font.language()->encoding()->LatexName()
617 os << "\\ensuremath{"
626 case '|': case '<': case '>':
627 // In T1 encoding, these characters exist
628 if (lyxrc.fontenc == "T1") {
630 //... but we should avoid ligatures
631 if ((c == '>' || c == '<')
633 && getChar(i + 1) == c) {
634 //os << "\\textcompwordmark{}";
635 // Jean-Marc, have a look at
636 // this. I think this works
644 // Typewriter font also has them
645 if (font.family() == LyXFont::TYPEWRITER_FAMILY) {
649 // Otherwise, we use what LaTeX
653 os << "\\textless{}";
657 os << "\\textgreater{}";
667 case '-': // "--" in Typewriter mode -> "-{}-"
669 && getChar(i + 1) == '-'
670 && font.family() == LyXFont::TYPEWRITER_FAMILY) {
679 os << "\\char`\\\"{}";
684 if (bparams.inputenc == "default") {
693 case '%': case '#': case '{':
700 os << "\\textasciitilde{}";
705 os << "\\textasciicircum{}";
709 case '*': case '[': case ']':
710 // avoid being mistaken for optional arguments
711 os << '{' << c << '}';
716 // Blanks are printed before font switching.
717 // Sure? I am not! (try nice-latex)
718 // I am sure it's correct. LyX might be smarter
719 // in the future, but for now, nothing wrong is
725 // I assume this is hack treating typewriter as verbatim
726 if (font.family() == LyXFont::TYPEWRITER_FAMILY) {
735 // FIXME: if we have "LaTeX" with a font
736 // change in the middle (before the 'T', then
737 // the "TeX" part is still special cased.
738 // Really we should only operate this on
739 // "words" for some definition of word
743 for (; pnr < phrases_nr; ++pnr) {
744 if (isTextAt(special_phrases[pnr].phrase, i)) {
745 os << special_phrases[pnr].macro;
746 i += special_phrases[pnr].phrase.length() - 1;
747 column += special_phrases[pnr].macro.length() - 1;
752 if (pnr == phrases_nr && c != '\0') {
761 void Paragraph::Pimpl::validate(LaTeXFeatures & features,
762 LyXLayout const & layout) const
764 BufferParams const & bparams = features.bufferParams();
767 if (params.lineTop() || params.lineBottom())
768 features.require("lyxline");
769 if (!params.spacing().isDefault())
770 features.require("setspace");
773 features.useLayout(layout.name());
776 Language const * doc_language = bparams.language;
778 FontList::const_iterator fcit = fontlist.begin();
779 FontList::const_iterator fend = fontlist.end();
780 for (; fcit != fend; ++fcit) {
781 if (fcit->font().noun() == LyXFont::ON) {
782 lyxerr[Debug::LATEX] << "font.noun: "
783 << fcit->font().noun()
785 features.require("noun");
786 lyxerr[Debug::LATEX] << "Noun enabled. Font: "
787 << fcit->font().stateText(0)
790 switch (fcit->font().color()) {
792 case LColor::inherit:
794 // probably we should put here all interface colors used for
795 // font displaying! For now I just add this ones I know of (Jug)
800 features.require("color");
801 lyxerr[Debug::LATEX] << "Color enabled. Font: "
802 << fcit->font().stateText(0)
806 Language const * language = fcit->font().language();
807 if (language->babel() != doc_language->babel() &&
808 language != ignore_language &&
809 language != latex_language)
811 features.useLanguage(language);
812 lyxerr[Debug::LATEX] << "Found language "
813 << language->babel() << endl;
817 if (!params.leftIndent().zero())
818 features.require("ParagraphLeftIndent");
821 InsetList::iterator icit = owner_->insetlist.begin();
822 InsetList::iterator iend = owner_->insetlist.end();
823 for (; icit != iend; ++icit) {
825 icit->inset->validate(features);
826 if (layout.needprotect &&
827 icit->inset->lyxCode() == Inset::FOOT_CODE)
828 features.require("NeedLyXFootnoteCode");
833 for (pos_type i = 0; i < size() ; ++i) {
834 for (size_t pnr = 0; pnr < phrases_nr; ++pnr) {
835 if (!special_phrases[pnr].builtin
836 && isTextAt(special_phrases[pnr].phrase, i)) {
837 features.require(special_phrases[pnr].phrase);