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 int Paragraph::Pimpl::erase(pos_type start, pos_type end)
415 pos_type count = end - start;
426 void Paragraph::Pimpl::simpleTeXBlanks(ostream & os, TexRow & texrow,
428 unsigned int & column,
429 LyXFont const & font,
430 LyXLayout const & style)
435 if (column > lyxrc.ascii_linelen
437 && getChar(i - 1) != ' '
439 // same in FreeSpacing mode
440 && !style.free_spacing
441 && !owner_->isFreeSpacing()
442 // In typewriter mode, we want to avoid
443 // ! . ? : at the end of a line
444 && !(font.family() == LyXFont::TYPEWRITER_FAMILY
445 && (getChar(i - 1) == '.'
446 || getChar(i - 1) == '?'
447 || getChar(i - 1) == ':'
448 || getChar(i - 1) == '!'))) {
451 texrow.start(owner_, i + 1);
453 } else if (style.free_spacing) {
461 bool Paragraph::Pimpl::isTextAt(string const & str, pos_type pos) const
463 pos_type const len = str.length();
465 // is the paragraph large enough?
466 if (pos + len > size())
469 // does the wanted text start at point?
470 for (string::size_type i = 0; i < str.length(); ++i) {
471 if (str[i] != text[pos + i])
475 // is there a font change in middle of the word?
476 FontList::const_iterator cit = fontlist.begin();
477 FontList::const_iterator end = fontlist.end();
478 for (; cit != end; ++cit) {
479 if (cit->pos() >= pos)
482 if (cit != end && pos + len - 1 > cit->pos())
489 void Paragraph::Pimpl::simpleTeXSpecialChars(Buffer const * buf,
490 BufferParams const & bparams,
495 LyXFont & running_font,
497 LyXFont const & outerfont,
499 Change::Type & running_change,
500 LyXLayout const & style,
502 unsigned int & column,
505 if (style.pass_thru) {
506 if (c != Paragraph::META_INSET) {
510 Inset const * inset = owner_->getInset(i);
511 inset->ascii(buf, os, 0);
516 // Two major modes: LaTeX or plain
517 // Handle here those cases common to both modes
518 // and then split to handle the two modes separately.
520 case Paragraph::META_INSET: {
521 Inset * inset = owner_->getInset(i);
523 // FIXME: remove this check
527 // FIXME: move this to InsetNewline::latex
528 if (inset->lyxCode() == Inset::NEWLINE_CODE) {
529 // newlines are handled differently here than
530 // the default in simpleTeXSpecialChars().
531 if (!style.newline_allowed) {
535 column += running_font.latexWriteEndChanges(os, basefont, basefont);
538 basefont = owner_->getLayoutFont(bparams, outerfont);
539 running_font = basefont;
541 if (font.family() == LyXFont::TYPEWRITER_FAMILY)
550 texrow.start(owner_, i + 1);
555 if (inset->isTextInset()) {
556 column += Changes::latexMarkChange(os, running_change,
558 running_change = Change::UNCHANGED;
562 int const len = os.tellp();
563 //ostream::pos_type const len = os.tellp();
564 if ((inset->lyxCode() == Inset::GRAPHICS_CODE
565 || inset->lyxCode() == Inset::MATH_CODE
566 || inset->lyxCode() == Inset::URL_CODE)
567 && running_font.isRightToLeft()) {
573 #warning Bug: we can have an empty font change here!
574 // if there has just been a font change, we are going to close it
575 // right now, which means stupid latex code like \textsf{}. AFAIK,
576 // this does not harm dvi output. A minor bug, thus (JMarc)
578 // some insets cannot be inside a font change command
579 if (open_font && inset->noFontChange()) {
580 column +=running_font.
581 latexWriteEndChanges(os,
585 basefont = owner_->getLayoutFont(bparams, outerfont);
586 running_font = basefont;
589 int tmp = inset->latex(buf, os, moving_arg,
596 for (int j = 0; j < tmp; ++j) {
599 texrow.start(owner_, i + 1);
602 column += int(os.tellp()) - len;
608 // And now for the special cases within each mode
612 os << "\\textbackslash{}";
616 case '±': case '²': case '³':
617 case '×': case '÷': case '¹':
619 if ((bparams.inputenc == "latin1" ||
620 bparams.inputenc == "latin9") ||
621 (bparams.inputenc == "auto" &&
622 (font.language()->encoding()->LatexName()
624 font.language()->encoding()->LatexName()
626 os << "\\ensuremath{"
635 case '|': case '<': case '>':
636 // In T1 encoding, these characters exist
637 if (lyxrc.fontenc == "T1") {
639 //... but we should avoid ligatures
640 if ((c == '>' || c == '<')
642 && getChar(i + 1) == c) {
643 //os << "\\textcompwordmark{}";
644 // Jean-Marc, have a look at
645 // this. I think this works
653 // Typewriter font also has them
654 if (font.family() == LyXFont::TYPEWRITER_FAMILY) {
658 // Otherwise, we use what LaTeX
662 os << "\\textless{}";
666 os << "\\textgreater{}";
676 case '-': // "--" in Typewriter mode -> "-{}-"
678 && getChar(i + 1) == '-'
679 && font.family() == LyXFont::TYPEWRITER_FAMILY) {
688 os << "\\char`\\\"{}";
693 if (bparams.inputenc == "default") {
702 case '%': case '#': case '{':
709 os << "\\textasciitilde{}";
714 os << "\\textasciicircum{}";
718 case '*': case '[': case ']':
719 // avoid being mistaken for optional arguments
720 os << '{' << c << '}';
725 // Blanks are printed before font switching.
726 // Sure? I am not! (try nice-latex)
727 // I am sure it's correct. LyX might be smarter
728 // in the future, but for now, nothing wrong is
734 // I assume this is hack treating typewriter as verbatim
735 if (font.family() == LyXFont::TYPEWRITER_FAMILY) {
744 // FIXME: if we have "LaTeX" with a font
745 // change in the middle (before the 'T', then
746 // the "TeX" part is still special cased.
747 // Really we should only operate this on
748 // "words" for some definition of word
752 for (; pnr < phrases_nr; ++pnr) {
753 if (isTextAt(special_phrases[pnr].phrase, i)) {
754 os << special_phrases[pnr].macro;
755 i += special_phrases[pnr].phrase.length() - 1;
756 column += special_phrases[pnr].macro.length() - 1;
761 if (pnr == phrases_nr && c != '\0') {
770 void Paragraph::Pimpl::validate(LaTeXFeatures & features,
771 LyXLayout const & layout) const
773 BufferParams const & bparams = features.bufferParams();
776 if (params.lineTop() || params.lineBottom())
777 features.require("lyxline");
778 if (!params.spacing().isDefault())
779 features.require("setspace");
782 features.useLayout(layout.name());
785 Language const * doc_language = bparams.language;
787 FontList::const_iterator fcit = fontlist.begin();
788 FontList::const_iterator fend = fontlist.end();
789 for (; fcit != fend; ++fcit) {
790 if (fcit->font().noun() == LyXFont::ON) {
791 lyxerr[Debug::LATEX] << "font.noun: "
792 << fcit->font().noun()
794 features.require("noun");
795 lyxerr[Debug::LATEX] << "Noun enabled. Font: "
796 << fcit->font().stateText(0)
799 switch (fcit->font().color()) {
801 case LColor::inherit:
803 // probably we should put here all interface colors used for
804 // font displaying! For now I just add this ones I know of (Jug)
809 features.require("color");
810 lyxerr[Debug::LATEX] << "Color enabled. Font: "
811 << fcit->font().stateText(0)
815 Language const * language = fcit->font().language();
816 if (language->babel() != doc_language->babel() &&
817 language != ignore_language &&
818 language != latex_language)
820 features.useLanguage(language);
821 lyxerr[Debug::LATEX] << "Found language "
822 << language->babel() << endl;
826 if (!params.leftIndent().zero())
827 features.require("ParagraphLeftIndent");
830 InsetList::iterator icit = owner_->insetlist.begin();
831 InsetList::iterator iend = owner_->insetlist.end();
832 for (; icit != iend; ++icit) {
833 if (icit.getInset()) {
834 icit.getInset()->validate(features);
835 if (layout.needprotect &&
836 icit.getInset()->lyxCode() == Inset::FOOT_CODE)
837 features.require("NeedLyXFootnoteCode");
842 for (pos_type i = 0; i < size() ; ++i) {
843 for (size_t pnr = 0; pnr < phrases_nr; ++pnr) {
844 if (!special_phrases[pnr].builtin
845 && isTextAt(special_phrases[pnr].phrase, i)) {
846 features.require(special_phrases[pnr].phrase);