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)
67 : params(p.params), owner_(owner)
69 inset_owner = p.inset_owner;
71 fontlist = p.fontlist;
75 changes_.reset(new Changes(*p.changes_.get()));
79 void Paragraph::Pimpl::clear()
86 void Paragraph::Pimpl::setContentsFromPar(Paragraph const & par)
88 text = par.pimpl_->text;
89 if (par.pimpl_->tracking()) {
90 changes_.reset(new Changes(*(par.pimpl_->changes_.get())));
95 void Paragraph::Pimpl::trackChanges(Change::Type type)
98 lyxerr[Debug::CHANGES] << "already tracking for par " << id_ << endl;
102 lyxerr[Debug::CHANGES] << "track changes for par "
103 << id_ << " type " << type << endl;
104 changes_.reset(new Changes(type));
105 changes_->set(type, 0, size());
109 void Paragraph::Pimpl::untrackChanges()
115 void Paragraph::Pimpl::cleanChanges()
117 // if we're not tracking, we don't want to reset...
121 changes_.reset(new Changes(Change::INSERTED));
122 changes_->set(Change::INSERTED, 0, size());
126 bool Paragraph::Pimpl::isChanged(pos_type start, pos_type end) const
131 return changes_->isChange(start, end);
135 bool Paragraph::Pimpl::isChangeEdited(pos_type start, pos_type end) const
140 return changes_->isChangeEdited(start, end);
144 void Paragraph::Pimpl::setChange(pos_type pos, Change::Type type)
149 changes_->set(type, pos);
153 Change::Type Paragraph::Pimpl::lookupChange(pos_type pos) const
156 return Change::UNCHANGED;
158 return changes_->lookup(pos);
162 Change const Paragraph::Pimpl::lookupChangeFull(pos_type pos) const
165 return Change(Change::UNCHANGED);
167 return changes_->lookupFull(pos);
171 void Paragraph::Pimpl::markErased()
173 lyx::Assert(tracking());
175 // FIXME: we should actually remove INSERTED chars.
176 // difficult because owning insettexts/tabulars need
177 // to update themselves when rows etc. change
178 changes_->set(Change::DELETED, 0, size());
179 changes_->reset(Change::DELETED);
183 void Paragraph::Pimpl::acceptChange(pos_type start, pos_type end)
189 changes_.reset(new Changes(Change::UNCHANGED));
193 lyxerr << "acceptchange" << endl;
196 for (; i < end; ++i) {
197 switch (lookupChange(i)) {
198 case Change::UNCHANGED:
201 case Change::INSERTED:
202 changes_->set(Change::UNCHANGED, i);
205 case Change::DELETED:
214 lyxerr << "endacceptchange" << endl;
215 changes_->reset(Change::UNCHANGED);
219 void Paragraph::Pimpl::rejectChange(pos_type start, pos_type end)
225 changes_.reset(new Changes(Change::UNCHANGED));
231 for (; i < end; ++i) {
232 switch (lookupChange(i)) {
233 case Change::UNCHANGED:
236 case Change::INSERTED:
243 case Change::DELETED:
244 changes_->set(Change::UNCHANGED, i);
248 changes_->reset(Change::UNCHANGED);
252 Paragraph::value_type Paragraph::Pimpl::getChar(pos_type pos) const
255 // This is in the critical path for loading!
256 pos_type const siz = size();
258 lyx::Assert(pos <= siz);
261 lyxerr << "getChar() on pos " << pos << " in par id "
262 << owner_->id() << " of size " << siz
263 << " is a bit silly !" << endl;
269 lyx::Assert(pos < size());
275 void Paragraph::Pimpl::setChar(pos_type pos, value_type c)
282 void Paragraph::Pimpl::insertChar(pos_type pos, value_type c,
283 LyXFont const & font, Change change)
285 lyx::Assert(pos <= size());
288 changes_->record(change, pos);
291 // This is actually very common when parsing buffers (and
292 // maybe inserting ascii text)
294 // when appending characters, no need to update tables
296 owner_->setFont(pos, font);
300 text.insert(text.begin() + pos, c);
302 // Update the font table.
303 FontTable search_font(pos, LyXFont());
304 for (FontList::iterator it = lower_bound(fontlist.begin(),
306 search_font, matchFT());
307 it != fontlist.end(); ++it)
309 it->pos(it->pos() + 1);
313 owner_->insetlist.increasePosAfterPos(pos);
315 owner_->setFont(pos, font);
319 void Paragraph::Pimpl::insertInset(pos_type pos,
320 Inset * inset, LyXFont const & font, Change change)
323 lyx::Assert(pos <= size());
325 insertChar(pos, META_INSET, font, change);
326 lyx::Assert(text[pos] == META_INSET);
328 // Add a new entry in the insetlist.
329 owner_->insetlist.insert(inset, pos);
330 inset->parOwner(owner_);
333 inset->setOwner(inset_owner);
337 void Paragraph::Pimpl::eraseIntern(pos_type pos)
339 // if it is an inset, delete the inset entry
340 if (text[pos] == Paragraph::META_INSET) {
341 owner_->insetlist.erase(pos);
344 text.erase(text.begin() + pos);
346 // Erase entries in the tables.
347 FontTable search_font(pos, LyXFont());
349 FontList::iterator it =
350 lower_bound(fontlist.begin(),
352 search_font, matchFT());
353 if (it != fontlist.end() && it->pos() == pos &&
355 (it != fontlist.begin()
356 && boost::prior(it)->pos() == pos - 1))) {
357 // If it is a multi-character font
358 // entry, we just make it smaller
359 // (see update below), otherwise we
361 unsigned int const i = it - fontlist.begin();
362 fontlist.erase(fontlist.begin() + i);
363 it = fontlist.begin() + i;
364 if (i > 0 && i < fontlist.size() &&
365 fontlist[i - 1].font() == fontlist[i].font()) {
366 fontlist.erase(fontlist.begin() + i - 1);
367 it = fontlist.begin() + i - 1;
371 // Update all other entries.
372 FontList::iterator fend = fontlist.end();
373 for (; it != fend; ++it)
374 it->pos(it->pos() - 1);
376 // Update the insetlist.
377 owner_->insetlist.decreasePosAfterPos(pos);
381 bool Paragraph::Pimpl::erase(pos_type pos)
383 lyx::Assert(pos < size());
386 Change::Type changetype(changes_->lookup(pos));
387 changes_->record(Change(Change::DELETED), pos);
389 // only allow the actual removal if it was /new/ text
390 if (changetype != Change::INSERTED) {
391 if (text[pos] == Paragraph::META_INSET) {
392 Inset * i(owner_->getInset(pos));
404 int Paragraph::Pimpl::erase(pos_type start, pos_type end)
407 pos_type count = end - start;
418 void Paragraph::Pimpl::simpleTeXBlanks(ostream & os, TexRow & texrow,
420 unsigned int & column,
421 LyXFont const & font,
422 LyXLayout const & style)
427 if (column > lyxrc.ascii_linelen
429 && getChar(i - 1) != ' '
431 // same in FreeSpacing mode
432 && !owner_->isFreeSpacing()
433 // In typewriter mode, we want to avoid
434 // ! . ? : at the end of a line
435 && !(font.family() == LyXFont::TYPEWRITER_FAMILY
436 && (getChar(i - 1) == '.'
437 || getChar(i - 1) == '?'
438 || getChar(i - 1) == ':'
439 || getChar(i - 1) == '!'))) {
442 texrow.start(owner_->id(), i + 1);
444 } else if (style.free_spacing) {
452 bool Paragraph::Pimpl::isTextAt(string const & str, pos_type pos) const
454 pos_type const len = str.length();
456 // is the paragraph large enough?
457 if (pos + len > size())
460 // does the wanted text start at point?
461 for (string::size_type i = 0; i < str.length(); ++i) {
462 if (str[i] != text[pos + i])
466 // is there a font change in middle of the word?
467 FontList::const_iterator cit = fontlist.begin();
468 FontList::const_iterator end = fontlist.end();
469 for (; cit != end; ++cit) {
470 if (cit->pos() >= pos)
473 if (cit != end && pos + len - 1 > cit->pos())
480 void Paragraph::Pimpl::simpleTeXSpecialChars(Buffer const * buf,
481 BufferParams const & bparams,
484 LatexRunParams const & runparams,
486 LyXFont & running_font,
488 LyXFont const & outerfont,
490 Change::Type & running_change,
491 LyXLayout const & style,
493 unsigned int & column,
496 if (style.pass_thru) {
497 if (c != Paragraph::META_INSET) {
501 Inset const * inset = owner_->getInset(i);
502 inset->ascii(buf, os, 0);
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 // FIXME: move this to InsetNewline::latex
519 if (inset->lyxCode() == Inset::NEWLINE_CODE) {
520 // newlines are handled differently here than
521 // the default in simpleTeXSpecialChars().
522 if (!style.newline_allowed) {
526 column += running_font.latexWriteEndChanges(os, basefont, basefont);
529 basefont = owner_->getLayoutFont(bparams, outerfont);
530 running_font = basefont;
532 if (font.family() == LyXFont::TYPEWRITER_FAMILY)
535 if (runparams.moving_arg)
541 texrow.start(owner_->id(), i + 1);
546 if (inset->isTextInset()) {
547 column += Changes::latexMarkChange(os, running_change,
549 running_change = Change::UNCHANGED;
553 int const len = os.tellp();
554 //ostream::pos_type const len = os.tellp();
555 if ((inset->lyxCode() == Inset::GRAPHICS_CODE
556 || inset->lyxCode() == Inset::MATH_CODE
557 || inset->lyxCode() == Inset::URL_CODE)
558 && running_font.isRightToLeft()) {
564 #warning Bug: we can have an empty font change here!
565 // if there has just been a font change, we are going to close it
566 // right now, which means stupid latex code like \textsf{}. AFAIK,
567 // this does not harm dvi output. A minor bug, thus (JMarc)
569 // some insets cannot be inside a font change command
570 if (open_font && inset->noFontChange()) {
571 column +=running_font.
572 latexWriteEndChanges(os,
576 basefont = owner_->getLayoutFont(bparams, outerfont);
577 running_font = basefont;
580 int tmp = inset->latex(buf, os, runparams);
586 for (int j = 0; j < tmp; ++j) {
589 texrow.start(owner_->id(), i + 1);
592 column += int(os.tellp()) - len;
598 // And now for the special cases within each mode
602 os << "\\textbackslash{}";
606 case '±': case '²': case '³':
607 case '×': case '÷': case '¹':
609 if ((bparams.inputenc == "latin1" ||
610 bparams.inputenc == "latin9") ||
611 (bparams.inputenc == "auto" &&
612 (font.language()->encoding()->LatexName()
614 font.language()->encoding()->LatexName()
616 os << "\\ensuremath{"
625 case '|': case '<': case '>':
626 // In T1 encoding, these characters exist
627 if (lyxrc.fontenc == "T1") {
629 //... but we should avoid ligatures
630 if ((c == '>' || c == '<')
632 && getChar(i + 1) == c) {
633 //os << "\\textcompwordmark{}";
634 // Jean-Marc, have a look at
635 // this. I think this works
643 // Typewriter font also has them
644 if (font.family() == LyXFont::TYPEWRITER_FAMILY) {
648 // Otherwise, we use what LaTeX
652 os << "\\textless{}";
656 os << "\\textgreater{}";
666 case '-': // "--" in Typewriter mode -> "-{}-"
668 && getChar(i + 1) == '-'
669 && font.family() == LyXFont::TYPEWRITER_FAMILY) {
678 os << "\\char`\\\"{}";
683 if (bparams.inputenc == "default") {
692 case '%': case '#': case '{':
699 os << "\\textasciitilde{}";
704 os << "\\textasciicircum{}";
708 case '*': case '[': case ']':
709 // avoid being mistaken for optional arguments
710 os << '{' << c << '}';
715 // Blanks are printed before font switching.
716 // Sure? I am not! (try nice-latex)
717 // I am sure it's correct. LyX might be smarter
718 // in the future, but for now, nothing wrong is
724 // I assume this is hack treating typewriter as verbatim
725 if (font.family() == LyXFont::TYPEWRITER_FAMILY) {
734 // FIXME: if we have "LaTeX" with a font
735 // change in the middle (before the 'T', then
736 // the "TeX" part is still special cased.
737 // Really we should only operate this on
738 // "words" for some definition of word
742 for (; pnr < phrases_nr; ++pnr) {
743 if (isTextAt(special_phrases[pnr].phrase, i)) {
744 os << special_phrases[pnr].macro;
745 i += special_phrases[pnr].phrase.length() - 1;
746 column += special_phrases[pnr].macro.length() - 1;
751 if (pnr == phrases_nr && c != '\0') {
760 void Paragraph::Pimpl::validate(LaTeXFeatures & features,
761 LyXLayout const & layout) const
763 BufferParams const & bparams = features.bufferParams();
766 if (params.lineTop() || params.lineBottom())
767 features.require("lyxline");
768 if (!params.spacing().isDefault())
769 features.require("setspace");
772 features.useLayout(layout.name());
775 Language const * doc_language = bparams.language;
777 FontList::const_iterator fcit = fontlist.begin();
778 FontList::const_iterator fend = fontlist.end();
779 for (; fcit != fend; ++fcit) {
780 if (fcit->font().noun() == LyXFont::ON) {
781 lyxerr[Debug::LATEX] << "font.noun: "
782 << fcit->font().noun()
784 features.require("noun");
785 lyxerr[Debug::LATEX] << "Noun enabled. Font: "
786 << fcit->font().stateText(0)
789 switch (fcit->font().color()) {
791 case LColor::inherit:
793 // probably we should put here all interface colors used for
794 // font displaying! For now I just add this ones I know of (Jug)
799 features.require("color");
800 lyxerr[Debug::LATEX] << "Color enabled. Font: "
801 << fcit->font().stateText(0)
805 Language const * language = fcit->font().language();
806 if (language->babel() != doc_language->babel() &&
807 language != ignore_language &&
808 language != latex_language)
810 features.useLanguage(language);
811 lyxerr[Debug::LATEX] << "Found language "
812 << language->babel() << endl;
816 if (!params.leftIndent().zero())
817 features.require("ParagraphLeftIndent");
820 InsetList::iterator icit = owner_->insetlist.begin();
821 InsetList::iterator iend = owner_->insetlist.end();
822 for (; icit != iend; ++icit) {
824 icit->inset->validate(features);
825 if (layout.needprotect &&
826 icit->inset->lyxCode() == Inset::FOOT_CODE)
827 features.require("NeedLyXFootnoteCode");
832 for (pos_type i = 0; i < size() ; ++i) {
833 for (size_t pnr = 0; pnr < phrases_nr; ++pnr) {
834 if (!special_phrases[pnr].builtin
835 && isTextAt(special_phrases[pnr].phrase, i)) {
836 features.require(special_phrases[pnr].phrase);