3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Asger Alstrup
7 * \author Lars Gullik Bjønnes
8 * \author Jean-Marc Lasgouttes
9 * \author Angus Leeming
11 * \author André Pönitz
13 * \author Jürgen Vigna
15 * Full author contact details are available in file CREDITS.
20 #include "Paragraph.h"
23 #include "BufferParams.h"
29 #include "LaTeXFeatures.h"
35 #include "OutputParams.h"
36 #include "output_latex.h"
37 #include "paragraph_funcs.h"
38 #include "ParagraphParameters.h"
43 #include "frontends/alert.h"
44 #include "frontends/FontMetrics.h"
46 #include "insets/InsetBibitem.h"
47 #include "insets/InsetOptArg.h"
49 #include "support/lstrings.h"
50 #include "support/textutils.h"
51 #include "support/convert.h"
52 #include "support/unicode.h"
54 #include <boost/bind.hpp>
55 #include <boost/next_prior.hpp>
67 using support::contains;
68 using support::suffixIs;
69 using support::rsplit;
72 /////////////////////////////////////////////////////////////////////
76 /////////////////////////////////////////////////////////////////////
82 class Paragraph::Pimpl {
85 Pimpl(Paragraph * owner);
86 /// "Copy constructor"
87 Pimpl(Pimpl const &, Paragraph * owner);
92 /// look up change at given pos
93 Change const & lookupChange(pos_type pos) const;
94 /// is there a change within the given range ?
95 bool isChanged(pos_type start, pos_type end) const;
96 /// will the paragraph be physically merged with the next
97 /// one if the imaginary end-of-par character is logically deleted?
98 bool isMergedOnEndOfParDeletion(bool trackChanges) const;
99 /// set change for the entire par
100 void setChange(Change const & change);
101 /// set change at given pos
102 void setChange(pos_type pos, Change const & change);
103 /// accept changes within the given range
104 void acceptChanges(BufferParams const & bparams, pos_type start, pos_type end);
105 /// reject changes within the given range
106 void rejectChanges(BufferParams const & bparams, pos_type start, pos_type end);
109 value_type getChar(pos_type pos) const;
111 void insertChar(pos_type pos, value_type c, Change const & change);
113 void insertInset(pos_type pos, Inset * inset, Change const & change);
114 /// (logically) erase the char at pos; return true if it was actually erased
115 bool eraseChar(pos_type pos, bool trackChanges);
116 /// (logically) erase the given range; return the number of chars actually erased
117 int eraseChars(pos_type start, pos_type end, bool trackChanges);
121 /** A font entry covers a range of positions. Notice that the
122 entries in the list are inserted in random order.
123 I don't think it's worth the effort to implement a more effective
124 datastructure, because the number of different fonts in a paragraph
126 Nevertheless, I decided to store fontlist using a sorted vector:
127 fontlist = { {pos_1,font_1} , {pos_2,font_2} , ... } where
128 pos_1 < pos_2 < ..., font_{i-1} != font_i for all i,
129 and font_i covers the chars in positions pos_{i-1}+1,...,pos_i
130 (font_1 covers the chars 0,...,pos_1) (Dekel)
135 FontTable(pos_type p, Font const & f)
139 pos_type pos() const { return pos_; }
141 void pos(pos_type p) { pos_ = p; }
143 Font const & font() const { return font_; }
145 void font(Font const & f) { font_ = f;}
147 /// End position of paragraph this font attribute covers
149 /** Font. Interpretation of the font values:
150 If a value is Font::INHERIT_*, it means that the font
151 attribute is inherited from either the layout of this
152 paragraph or, in the case of nested paragraphs, from the
153 layout in the environment one level up until completely
155 The values Font::IGNORE_* and Font::TOGGLE are NOT
156 allowed in these font tables.
161 friend class matchFT;
165 /// used by lower_bound and upper_bound
166 int operator()(FontTable const & a, FontTable const & b) const {
167 return a.pos() < b.pos();
172 typedef std::vector<FontTable> FontList;
176 /// Output the surrogate pair formed by \p c and \p next to \p os.
177 /// \return the number of characters written.
178 int latexSurrogatePair(odocstream & os, value_type c, value_type next,
180 /// Output a space in appropriate formatting (or a surrogate pair
181 /// if the next character is a combining character).
182 /// \return whether a surrogate pair was output.
183 bool simpleTeXBlanks(Encoding const &,
184 odocstream &, TexRow & texrow,
186 unsigned int & column,
188 Layout const & style);
190 void simpleTeXSpecialChars(Buffer const &, BufferParams const &,
192 TexRow & texrow, OutputParams &,
195 Font const & outerfont,
197 Change & running_change,
198 Layout const & style,
200 unsigned int & column, value_type const c);
203 void validate(LaTeXFeatures & features,
204 Layout const & layout) const;
209 static unsigned int paragraph_id;
211 ParagraphParameters params;
215 pos_type size() const { return owner_->size(); }
216 /// match a string against a particular point in the paragraph
217 bool isTextAt(std::string const & str, pos_type pos) const;
219 /// for recording and looking up changes
230 using std::upper_bound;
231 using std::lower_bound;
235 // Initialization of the counter for the paragraph id's,
236 unsigned int Paragraph::Pimpl::paragraph_id = 0;
240 struct special_phrase {
246 special_phrase const special_phrases[] = {
247 { "LyX", from_ascii("\\LyX{}"), false },
248 { "TeX", from_ascii("\\TeX{}"), true },
249 { "LaTeX2e", from_ascii("\\LaTeXe{}"), true },
250 { "LaTeX", from_ascii("\\LaTeX{}"), true },
253 size_t const phrases_nr = sizeof(special_phrases)/sizeof(special_phrase);
258 Paragraph::Pimpl::Pimpl(Paragraph * owner)
262 id_ = paragraph_id++;
266 Paragraph::Pimpl::Pimpl(Pimpl const & p, Paragraph * owner)
267 : params(p.params), changes_(p.changes_), owner_(owner)
269 inset_owner = p.inset_owner;
270 fontlist = p.fontlist;
271 id_ = paragraph_id++;
275 bool Paragraph::Pimpl::isChanged(pos_type start, pos_type end) const
277 BOOST_ASSERT(start >= 0 && start <= size());
278 BOOST_ASSERT(end > start && end <= size() + 1);
280 return changes_.isChanged(start, end);
284 bool Paragraph::Pimpl::isMergedOnEndOfParDeletion(bool trackChanges) const {
285 // keep the logic here in sync with the logic of eraseChars()
291 Change change = changes_.lookup(size());
293 return change.type == Change::INSERTED && change.author == 0;
297 void Paragraph::Pimpl::setChange(Change const & change)
299 // beware of the imaginary end-of-par character!
300 changes_.set(change, 0, size() + 1);
303 * Propagate the change recursively - but not in case of DELETED!
305 * Imagine that your co-author makes changes in an existing inset. He
306 * sends your document to you and you come to the conclusion that the
307 * inset should go completely. If you erase it, LyX must not delete all
308 * text within the inset. Otherwise, the change tracked insertions of
309 * your co-author get lost and there is no way to restore them later.
311 * Conclusion: An inset's content should remain untouched if you delete it
314 if (change.type != Change::DELETED) {
315 for (pos_type pos = 0; pos < size(); ++pos) {
316 if (owner_->isInset(pos)) {
317 owner_->getInset(pos)->setChange(change);
324 void Paragraph::Pimpl::setChange(pos_type pos, Change const & change)
326 BOOST_ASSERT(pos >= 0 && pos <= size());
328 changes_.set(change, pos);
330 // see comment in setChange(Change const &) above
332 if (change.type != Change::DELETED &&
333 pos < size() && owner_->isInset(pos)) {
334 owner_->getInset(pos)->setChange(change);
339 Change const & Paragraph::Pimpl::lookupChange(pos_type pos) const
341 BOOST_ASSERT(pos >= 0 && pos <= size());
343 return changes_.lookup(pos);
347 void Paragraph::Pimpl::acceptChanges(BufferParams const & bparams, pos_type start, pos_type end)
349 BOOST_ASSERT(start >= 0 && start <= size());
350 BOOST_ASSERT(end > start && end <= size() + 1);
352 for (pos_type pos = start; pos < end; ++pos) {
353 switch (lookupChange(pos).type) {
354 case Change::UNCHANGED:
355 // accept changes in nested inset
356 if (pos < size() && owner_->isInset(pos)) {
357 owner_->getInset(pos)->acceptChanges(bparams);
362 case Change::INSERTED:
363 changes_.set(Change(Change::UNCHANGED), pos);
364 // also accept changes in nested inset
365 if (pos < size() && owner_->isInset(pos)) {
366 owner_->getInset(pos)->acceptChanges(bparams);
370 case Change::DELETED:
371 // Suppress access to non-existent
372 // "end-of-paragraph char"
374 eraseChar(pos, false);
385 void Paragraph::Pimpl::rejectChanges(BufferParams const & bparams, pos_type start, pos_type end)
387 BOOST_ASSERT(start >= 0 && start <= size());
388 BOOST_ASSERT(end > start && end <= size() + 1);
390 for (pos_type pos = start; pos < end; ++pos) {
391 switch (lookupChange(pos).type) {
392 case Change::UNCHANGED:
393 // reject changes in nested inset
394 if (pos < size() && owner_->isInset(pos)) {
395 owner_->getInset(pos)->rejectChanges(bparams);
399 case Change::INSERTED:
400 // Suppress access to non-existent
401 // "end-of-paragraph char"
403 eraseChar(pos, false);
409 case Change::DELETED:
410 changes_.set(Change(Change::UNCHANGED), pos);
412 // Do NOT reject changes within a deleted inset!
413 // There may be insertions of a co-author inside of it!
421 Paragraph::value_type Paragraph::Pimpl::getChar(pos_type pos) const
423 BOOST_ASSERT(pos >= 0 && pos <= size());
425 return owner_->getChar(pos);
429 void Paragraph::Pimpl::insertChar(pos_type pos, value_type c, Change const & change)
431 BOOST_ASSERT(pos >= 0 && pos <= size());
434 changes_.insert(change, pos);
436 // This is actually very common when parsing buffers (and
437 // maybe inserting ascii text)
439 // when appending characters, no need to update tables
440 owner_->text_.push_back(c);
444 owner_->text_.insert(owner_->text_.begin() + pos, c);
446 // Update the font table.
447 FontTable search_font(pos, Font());
448 for (FontList::iterator it
449 = lower_bound(fontlist.begin(), fontlist.end(), search_font, matchFT());
450 it != fontlist.end(); ++it)
452 it->pos(it->pos() + 1);
456 owner_->insetlist.increasePosAfterPos(pos);
460 void Paragraph::Pimpl::insertInset(pos_type pos, Inset * inset,
461 Change const & change)
464 BOOST_ASSERT(pos >= 0 && pos <= size());
466 insertChar(pos, META_INSET, change);
467 BOOST_ASSERT(owner_->text_[pos] == META_INSET);
469 // Add a new entry in the insetlist.
470 owner_->insetlist.insert(inset, pos);
474 bool Paragraph::Pimpl::eraseChar(pos_type pos, bool trackChanges)
476 BOOST_ASSERT(pos >= 0 && pos <= size());
478 // keep the logic here in sync with the logic of isMergedOnEndOfParDeletion()
481 Change change = changes_.lookup(pos);
483 // set the character to DELETED if
484 // a) it was previously unchanged or
485 // b) it was inserted by a co-author
487 if (change.type == Change::UNCHANGED ||
488 (change.type == Change::INSERTED && change.author != 0)) {
489 setChange(pos, Change(Change::DELETED));
493 if (change.type == Change::DELETED)
497 // Don't physically access the imaginary end-of-paragraph character.
498 // eraseChar() can only mark it as DELETED. A physical deletion of
499 // end-of-par must be handled externally.
507 // if it is an inset, delete the inset entry
508 if (owner_->text_[pos] == Paragraph::META_INSET) {
509 owner_->insetlist.erase(pos);
512 owner_->text_.erase(owner_->text_.begin() + pos);
514 // Erase entries in the tables.
515 FontTable search_font(pos, Font());
517 FontList::iterator it =
518 lower_bound(fontlist.begin(),
520 search_font, matchFT());
521 if (it != fontlist.end() && it->pos() == pos &&
523 (it != fontlist.begin()
524 && boost::prior(it)->pos() == pos - 1))) {
525 // If it is a multi-character font
526 // entry, we just make it smaller
527 // (see update below), otherwise we
529 unsigned int const i = it - fontlist.begin();
530 fontlist.erase(fontlist.begin() + i);
531 it = fontlist.begin() + i;
532 if (i > 0 && i < fontlist.size() &&
533 fontlist[i - 1].font() == fontlist[i].font()) {
534 fontlist.erase(fontlist.begin() + i - 1);
535 it = fontlist.begin() + i - 1;
539 // Update all other entries
540 FontList::iterator fend = fontlist.end();
541 for (; it != fend; ++it)
542 it->pos(it->pos() - 1);
544 // Update the insetlist
545 owner_->insetlist.decreasePosAfterPos(pos);
551 int Paragraph::Pimpl::eraseChars(pos_type start, pos_type end, bool trackChanges)
553 BOOST_ASSERT(start >= 0 && start <= size());
554 BOOST_ASSERT(end >= start && end <= size() + 1);
557 for (pos_type count = end - start; count; --count) {
558 if (!eraseChar(i, trackChanges))
565 int Paragraph::Pimpl::latexSurrogatePair(odocstream & os, value_type c,
566 value_type next, Encoding const & encoding)
568 // Writing next here may circumvent a possible font change between
569 // c and next. Since next is only output if it forms a surrogate pair
570 // with c we can ignore this:
571 // A font change inside a surrogate pair does not make sense and is
572 // hopefully impossible to input.
573 // FIXME: change tracking
574 // Is this correct WRT change tracking?
575 docstring const latex1 = encoding.latexChar(next);
576 docstring const latex2 = encoding.latexChar(c);
577 os << latex1 << '{' << latex2 << '}';
578 return latex1.length() + latex2.length() + 2;
582 bool Paragraph::Pimpl::simpleTeXBlanks(Encoding const & encoding,
583 odocstream & os, TexRow & texrow,
585 unsigned int & column,
587 Layout const & style)
592 if (i < size() - 1) {
593 char_type next = getChar(i + 1);
594 if (Encodings::isCombiningChar(next)) {
595 // This space has an accent, so we must always output it.
596 column += latexSurrogatePair(os, ' ', next, encoding) - 1;
602 if (lyxrc.plaintext_linelen > 0
603 && column > lyxrc.plaintext_linelen
605 && getChar(i - 1) != ' '
607 // same in FreeSpacing mode
608 && !owner_->isFreeSpacing()
609 // In typewriter mode, we want to avoid
610 // ! . ? : at the end of a line
611 && !(font.family() == Font::TYPEWRITER_FAMILY
612 && (getChar(i - 1) == '.'
613 || getChar(i - 1) == '?'
614 || getChar(i - 1) == ':'
615 || getChar(i - 1) == '!'))) {
618 texrow.start(owner_->id(), i + 1);
620 } else if (style.free_spacing) {
629 bool Paragraph::Pimpl::isTextAt(string const & str, pos_type pos) const
631 pos_type const len = str.length();
633 // is the paragraph large enough?
634 if (pos + len > size())
637 // does the wanted text start at point?
638 for (string::size_type i = 0; i < str.length(); ++i) {
639 // Caution: direct comparison of characters works only
640 // because str is pure ASCII.
641 if (str[i] != owner_->text_[pos + i])
645 // is there a font change in middle of the word?
646 FontList::const_iterator cit = fontlist.begin();
647 FontList::const_iterator end = fontlist.end();
648 for (; cit != end; ++cit) {
649 if (cit->pos() >= pos)
652 if (cit != end && pos + len - 1 > cit->pos())
659 void Paragraph::Pimpl::simpleTeXSpecialChars(Buffer const & buf,
660 BufferParams const & bparams,
663 OutputParams & runparams,
666 Font const & outerfont,
668 Change & running_change,
669 Layout const & style,
671 unsigned int & column,
674 if (style.pass_thru) {
675 if (c != Paragraph::META_INSET) {
677 // FIXME UNICODE: This can fail if c cannot
678 // be encoded in the current encoding.
681 owner_->getInset(i)->plaintext(buf, os, runparams);
685 // Two major modes: LaTeX or plain
686 // Handle here those cases common to both modes
687 // and then split to handle the two modes separately.
689 case Paragraph::META_INSET: {
690 Inset * inset = owner_->getInset(i);
692 // FIXME: remove this check
696 // FIXME: move this to InsetNewline::latex
697 if (inset->lyxCode() == Inset::NEWLINE_CODE) {
698 // newlines are handled differently here than
699 // the default in simpleTeXSpecialChars().
700 if (!style.newline_allowed) {
704 column += running_font.latexWriteEndChanges(
705 os, bparams, runparams,
710 if (running_font.family() == Font::TYPEWRITER_FAMILY)
713 basefont = owner_->getLayoutFont(bparams, outerfont);
714 running_font = basefont;
716 if (runparams.moving_arg)
722 texrow.start(owner_->id(), i + 1);
727 if (lookupChange(i).type == Change::DELETED) {
728 if( ++runparams.inDeletedInset == 1)
729 runparams.changeOfDeletedInset = lookupChange(i);
732 if (inset->canTrackChanges()) {
733 column += Changes::latexMarkChange(os, bparams, running_change,
734 Change(Change::UNCHANGED));
735 running_change = Change(Change::UNCHANGED);
739 odocstream::pos_type const len = os.tellp();
741 if ((inset->lyxCode() == Inset::GRAPHICS_CODE
742 || inset->lyxCode() == Inset::MATH_CODE
743 || inset->lyxCode() == Inset::URL_CODE)
744 && running_font.isRightToLeft()) {
745 if (running_font.language()->lang() == "farsi")
752 // FIXME: Bug: we can have an empty font change here!
753 // if there has just been a font change, we are going to close it
754 // right now, which means stupid latex code like \textsf{}. AFAIK,
755 // this does not harm dvi output. A minor bug, thus (JMarc)
756 // Some insets cannot be inside a font change command.
757 // However, even such insets *can* be placed in \L or \R
758 // or their equivalents (for RTL language switches), so we don't
759 // close the language in those cases.
760 // ArabTeX, though, cannot handle this special behavior, it seems.
761 bool arabtex = basefont.language()->lang() == "arabic_arabtex" ||
762 running_font.language()->lang() == "arabic_arabtex";
763 if (open_font && inset->noFontChange()) {
764 bool closeLanguage = arabtex ||
765 basefont.isRightToLeft() == running_font.isRightToLeft();
766 unsigned int count = running_font.latexWriteEndChanges(
767 os, bparams, runparams,
768 basefont, basefont, closeLanguage);
770 // if any font properties were closed, update the running_font,
771 // making sure, however, to leave the language as it was
773 // FIXME: probably a better way to keep track of the old
774 // language, than copying the entire font?
775 Font const copy_font(running_font);
776 basefont = owner_->getLayoutFont(bparams, outerfont);
777 running_font = basefont;
779 running_font.setLanguage(copy_font.language());
780 // leave font open if language is still open
781 open_font = (running_font.language() == basefont.language());
783 runparams.local_font = &basefont;
787 int tmp = inset->latex(buf, os, runparams);
790 if (running_font.language()->lang() == "farsi")
797 for (int j = 0; j < tmp; ++j) {
800 texrow.start(owner_->id(), i + 1);
803 column += os.tellp() - len;
806 if (lookupChange(i).type == Change::DELETED) {
807 --runparams.inDeletedInset;
813 // And now for the special cases within each mode
817 os << "\\textbackslash{}";
821 case '|': case '<': case '>':
822 // In T1 encoding, these characters exist
823 if (lyxrc.fontenc == "T1") {
825 //... but we should avoid ligatures
826 if ((c == '>' || c == '<')
828 && getChar(i + 1) == c) {
829 //os << "\\textcompwordmark{}";
831 // Jean-Marc, have a look at
832 // this. I think this works
840 // Typewriter font also has them
841 if (running_font.family() == Font::TYPEWRITER_FAMILY) {
845 // Otherwise, we use what LaTeX
849 os << "\\textless{}";
853 os << "\\textgreater{}";
863 case '-': // "--" in Typewriter mode -> "-{}-"
864 if (i <= size() - 2 &&
865 getChar(i + 1) == '-' &&
866 running_font.family() == Font::TYPEWRITER_FAMILY) {
875 os << "\\char`\\\"{}";
880 case '%': case '#': case '{':
888 os << "\\textasciitilde{}";
893 os << "\\textasciicircum{}";
898 // avoid being mistaken for optional arguments
906 // Blanks are printed before font switching.
907 // Sure? I am not! (try nice-latex)
908 // I am sure it's correct. LyX might be smarter
909 // in the future, but for now, nothing wrong is
915 // I assume this is hack treating typewriter as verbatim
916 // FIXME UNICODE: This can fail if c cannot be encoded
917 // in the current encoding.
918 if (running_font.family() == Font::TYPEWRITER_FAMILY) {
927 // FIXME: if we have "LaTeX" with a font
928 // change in the middle (before the 'T', then
929 // the "TeX" part is still special cased.
930 // Really we should only operate this on
931 // "words" for some definition of word
935 for (; pnr < phrases_nr; ++pnr) {
936 if (isTextAt(special_phrases[pnr].phrase, i)) {
937 os << special_phrases[pnr].macro;
938 i += special_phrases[pnr].phrase.length() - 1;
939 column += special_phrases[pnr].macro.length() - 1;
944 if (pnr == phrases_nr && c != '\0') {
945 Encoding const & encoding = *(runparams.encoding);
946 if (i < size() - 1) {
947 char_type next = getChar(i + 1);
948 if (Encodings::isCombiningChar(next)) {
949 column += latexSurrogatePair(os, c, next, encoding) - 1;
954 docstring const latex = encoding.latexChar(c);
955 if (latex.length() > 1 &&
956 latex[latex.length() - 1] != '}') {
957 // Prevent eating of a following
958 // space or command corruption by
959 // following characters
960 column += latex.length() + 1;
963 column += latex.length() - 1;
973 void Paragraph::Pimpl::validate(LaTeXFeatures & features,
974 Layout const & layout) const
976 BufferParams const & bparams = features.bufferParams();
979 if (!params.spacing().isDefault())
980 features.require("setspace");
983 features.useLayout(layout.name());
986 Language const * doc_language = bparams.language;
988 FontList::const_iterator fcit = fontlist.begin();
989 FontList::const_iterator fend = fontlist.end();
990 for (; fcit != fend; ++fcit) {
991 if (fcit->font().noun() == Font::ON) {
992 LYXERR(Debug::LATEX) << "font.noun: "
993 << fcit->font().noun()
995 features.require("noun");
996 LYXERR(Debug::LATEX) << "Noun enabled. Font: "
997 << to_utf8(fcit->font().stateText(0))
1000 switch (fcit->font().color()) {
1002 case Color::inherit:
1004 // probably we should put here all interface colors used for
1005 // font displaying! For now I just add this ones I know of (Jug)
1010 features.require("color");
1011 LYXERR(Debug::LATEX) << "Color enabled. Font: "
1012 << to_utf8(fcit->font().stateText(0))
1016 Language const * language = fcit->font().language();
1017 if (language->babel() != doc_language->babel() &&
1018 language != ignore_language &&
1019 language != latex_language)
1021 features.useLanguage(language);
1022 LYXERR(Debug::LATEX) << "Found language "
1023 << language->lang() << endl;
1027 if (!params.leftIndent().zero())
1028 features.require("ParagraphLeftIndent");
1031 InsetList::const_iterator icit = owner_->insetlist.begin();
1032 InsetList::const_iterator iend = owner_->insetlist.end();
1033 for (; icit != iend; ++icit) {
1035 icit->inset->validate(features);
1036 if (layout.needprotect &&
1037 icit->inset->lyxCode() == Inset::FOOT_CODE)
1038 features.require("NeedLyXFootnoteCode");
1042 // then the contents
1043 for (pos_type i = 0; i < size() ; ++i) {
1044 for (size_t pnr = 0; pnr < phrases_nr; ++pnr) {
1045 if (!special_phrases[pnr].builtin
1046 && isTextAt(special_phrases[pnr].phrase, i)) {
1047 features.require(special_phrases[pnr].phrase);
1051 Encodings::validate(getChar(i), features);
1059 /////////////////////////////////////////////////////////////////////
1063 /////////////////////////////////////////////////////////////////////
1067 Paragraph::Paragraph()
1068 : begin_of_body_(0), pimpl_(new Paragraph::Pimpl(this))
1075 Paragraph::Paragraph(Paragraph const & par)
1076 : itemdepth(par.itemdepth), insetlist(par.insetlist),
1077 layout_(par.layout_),
1078 text_(par.text_), begin_of_body_(par.begin_of_body_),
1079 pimpl_(new Paragraph::Pimpl(*par.pimpl_, this))
1081 //lyxerr << "Paragraph::Paragraph(Paragraph const&)" << endl;
1082 InsetList::iterator it = insetlist.begin();
1083 InsetList::iterator end = insetlist.end();
1084 for (; it != end; ++it)
1085 it->inset = it->inset->clone();
1089 Paragraph & Paragraph::operator=(Paragraph const & par)
1091 // needed as we will destroy the pimpl_ before copying it
1093 itemdepth = par.itemdepth;
1095 insetlist = par.insetlist;
1096 InsetList::iterator it = insetlist.begin();
1097 InsetList::iterator end = insetlist.end();
1098 for (; it != end; ++it)
1099 it->inset = it->inset->clone();
1101 layout_ = par.layout();
1103 begin_of_body_ = par.begin_of_body_;
1106 pimpl_ = new Pimpl(*par.pimpl_, this);
1112 Paragraph::~Paragraph()
1116 //lyxerr << "Paragraph::paragraph_id = "
1117 // << Paragraph::paragraph_id << endl;
1121 void Paragraph::write(Buffer const & buf, ostream & os,
1122 BufferParams const & bparams,
1123 depth_type & dth) const
1125 // The beginning or end of a deeper (i.e. nested) area?
1126 if (dth != params().depth()) {
1127 if (params().depth() > dth) {
1128 while (params().depth() > dth) {
1129 os << "\n\\begin_deeper";
1133 while (params().depth() < dth) {
1134 os << "\n\\end_deeper";
1140 // First write the layout
1141 os << "\n\\begin_layout " << to_utf8(layout()->name()) << '\n';
1145 Font font1(Font::ALL_INHERIT, bparams.language);
1147 Change running_change = Change(Change::UNCHANGED);
1150 for (pos_type i = 0; i <= size(); ++i) {
1152 Change change = pimpl_->lookupChange(i);
1153 Changes::lyxMarkChange(os, column, running_change, change);
1154 running_change = change;
1159 // Write font changes
1160 Font font2 = getFontSettings(bparams, i);
1161 if (font2 != font1) {
1162 font2.lyxWriteChanges(font1, os);
1167 value_type const c = getChar(i);
1171 Inset const * inset = getInset(i);
1173 if (inset->directWrite()) {
1174 // international char, let it write
1175 // code directly so it's shorter in
1177 inset->write(buf, os);
1181 os << "\\begin_inset ";
1182 inset->write(buf, os);
1183 os << "\n\\end_inset\n\n";
1189 os << "\n\\backslash\n";
1193 if (i + 1 < size() && getChar(i + 1) == ' ') {
1200 if ((column > 70 && c == ' ')
1205 // this check is to amend a bug. LyX sometimes
1206 // inserts '\0' this could cause problems.
1208 std::vector<char> tmp = ucs4_to_utf8(c);
1209 tmp.push_back('\0');
1212 lyxerr << "ERROR (Paragraph::writeFile):"
1213 " NULL char in structure." << endl;
1219 os << "\n\\end_layout\n";
1223 void Paragraph::validate(LaTeXFeatures & features) const
1225 pimpl_->validate(features, *layout());
1229 bool Paragraph::eraseChar(pos_type pos, bool trackChanges)
1231 return pimpl_->eraseChar(pos, trackChanges);
1235 int Paragraph::eraseChars(pos_type start, pos_type end, bool trackChanges)
1237 return pimpl_->eraseChars(start, end, trackChanges);
1241 void Paragraph::insert(pos_type start, docstring const & str,
1242 Font const & font, Change const & change)
1244 for (size_t i = 0, n = str.size(); i != n ; ++i)
1245 insertChar(start + i, str[i], font, change);
1249 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c,
1252 pimpl_->insertChar(pos, c, Change(trackChanges ?
1253 Change::INSERTED : Change::UNCHANGED));
1257 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c,
1258 Font const & font, bool trackChanges)
1260 pimpl_->insertChar(pos, c, Change(trackChanges ?
1261 Change::INSERTED : Change::UNCHANGED));
1266 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c,
1267 Font const & font, Change const & change)
1269 pimpl_->insertChar(pos, c, change);
1274 void Paragraph::insertInset(pos_type pos, Inset * inset,
1275 Change const & change)
1277 pimpl_->insertInset(pos, inset, change);
1281 void Paragraph::insertInset(pos_type pos, Inset * inset,
1282 Font const & font, Change const & change)
1284 pimpl_->insertInset(pos, inset, change);
1285 // Set the font/language of the inset...
1287 // ... as well as the font/language of the text inside the inset
1288 // FIXME: This is far from perfect. It basically overrides work being done
1289 // in the InsetText constructor. Also, it doesn't work for Tables
1290 // (precisely because each cell's font/language is set in the Table's
1291 // constructor, so by now it's too late). The long-term solution should
1292 // be moving current_font into Cursor, and getting rid of all this...
1293 // (see http://thread.gmane.org/gmane.editors.lyx.devel/88869/focus=88944)
1294 if (inset->asTextInset()) {
1295 inset->asTextInset()->text_.current_font = font;
1296 inset->asTextInset()->text_.real_current_font = font;
1301 bool Paragraph::insetAllowed(Inset_code code)
1303 return !pimpl_->inset_owner || pimpl_->inset_owner->insetAllowed(code);
1307 // Gets uninstantiated font setting at position.
1308 Font const Paragraph::getFontSettings(BufferParams const & bparams,
1312 lyxerr << " pos: " << pos << " size: " << size() << endl;
1313 BOOST_ASSERT(pos <= size());
1316 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
1317 Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
1318 for (; cit != end; ++cit)
1319 if (cit->pos() >= pos)
1325 if (pos == size() && !empty())
1326 return getFontSettings(bparams, pos - 1);
1328 return Font(Font::ALL_INHERIT, getParLanguage(bparams));
1332 FontSpan Paragraph::fontSpan(pos_type pos) const
1334 BOOST_ASSERT(pos <= size());
1337 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
1338 Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
1339 for (; cit != end; ++cit) {
1340 if (cit->pos() >= pos) {
1341 if (pos >= beginOfBody())
1342 return FontSpan(std::max(start, beginOfBody()),
1345 return FontSpan(start,
1346 std::min(beginOfBody() - 1,
1349 start = cit->pos() + 1;
1352 // This should not happen, but if so, we take no chances.
1353 //lyxerr << "Paragraph::getEndPosOfFontSpan: This should not happen!"
1355 return FontSpan(pos, pos);
1359 // Gets uninstantiated font setting at position 0
1360 Font const Paragraph::getFirstFontSettings(BufferParams const & bparams) const
1362 if (!empty() && !pimpl_->fontlist.empty())
1363 return pimpl_->fontlist[0].font();
1365 return Font(Font::ALL_INHERIT, bparams.language);
1369 // Gets the fully instantiated font at a given position in a paragraph
1370 // This is basically the same function as Text::GetFont() in text2.cpp.
1371 // The difference is that this one is used for generating the LaTeX file,
1372 // and thus cosmetic "improvements" are disallowed: This has to deliver
1373 // the true picture of the buffer. (Asger)
1374 Font const Paragraph::getFont(BufferParams const & bparams, pos_type pos,
1375 Font const & outerfont) const
1377 BOOST_ASSERT(pos >= 0);
1379 Font font = getFontSettings(bparams, pos);
1381 pos_type const body_pos = beginOfBody();
1383 font.realize(layout_->labelfont);
1385 font.realize(layout_->font);
1387 font.realize(outerfont);
1388 font.realize(bparams.getFont());
1394 Font const Paragraph::getLabelFont
1395 (BufferParams const & bparams, Font const & outerfont) const
1397 Font tmpfont = layout()->labelfont;
1398 tmpfont.setLanguage(getParLanguage(bparams));
1399 tmpfont.realize(outerfont);
1400 tmpfont.realize(bparams.getFont());
1405 Font const Paragraph::getLayoutFont
1406 (BufferParams const & bparams, Font const & outerfont) const
1408 Font tmpfont = layout()->font;
1409 tmpfont.setLanguage(getParLanguage(bparams));
1410 tmpfont.realize(outerfont);
1411 tmpfont.realize(bparams.getFont());
1416 /// Returns the height of the highest font in range
1417 Font_size Paragraph::highestFontInRange
1418 (pos_type startpos, pos_type endpos, Font_size def_size) const
1420 if (pimpl_->fontlist.empty())
1423 Pimpl::FontList::const_iterator end_it = pimpl_->fontlist.begin();
1424 Pimpl::FontList::const_iterator const end = pimpl_->fontlist.end();
1425 for (; end_it != end; ++end_it) {
1426 if (end_it->pos() >= endpos)
1433 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
1434 for (; cit != end; ++cit) {
1435 if (cit->pos() >= startpos)
1439 Font::FONT_SIZE maxsize = Font::SIZE_TINY;
1440 for (; cit != end_it; ++cit) {
1441 Font::FONT_SIZE size = cit->font().size();
1442 if (size == Font::INHERIT_SIZE)
1444 if (size > maxsize && size <= Font::SIZE_HUGER)
1451 Paragraph::value_type
1452 Paragraph::getUChar(BufferParams const & bparams, pos_type pos) const
1454 value_type c = getChar(pos);
1455 if (!lyxrc.rtl_support)
1485 if (uc != c && getFontSettings(bparams, pos).isRightToLeft())
1492 void Paragraph::setFont(pos_type pos, Font const & font)
1494 BOOST_ASSERT(pos <= size());
1496 // First, reduce font against layout/label font
1497 // Update: The setCharFont() routine in text2.cpp already
1498 // reduces font, so we don't need to do that here. (Asger)
1499 // No need to simplify this because it will disappear
1500 // in a new kernel. (Asger)
1501 // Next search font table
1503 Pimpl::FontList::iterator beg = pimpl_->fontlist.begin();
1504 Pimpl::FontList::iterator it = beg;
1505 Pimpl::FontList::iterator endit = pimpl_->fontlist.end();
1506 for (; it != endit; ++it) {
1507 if (it->pos() >= pos)
1510 size_t const i = distance(beg, it);
1511 bool notfound = (it == endit);
1513 if (!notfound && pimpl_->fontlist[i].font() == font)
1516 bool begin = pos == 0 || notfound ||
1517 (i > 0 && pimpl_->fontlist[i - 1].pos() == pos - 1);
1518 // Is position pos is a beginning of a font block?
1519 bool end = !notfound && pimpl_->fontlist[i].pos() == pos;
1520 // Is position pos is the end of a font block?
1521 if (begin && end) { // A single char block
1522 if (i + 1 < pimpl_->fontlist.size() &&
1523 pimpl_->fontlist[i + 1].font() == font) {
1524 // Merge the singleton block with the next block
1525 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
1526 if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
1527 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i - 1);
1528 } else if (i > 0 && pimpl_->fontlist[i - 1].font() == font) {
1529 // Merge the singleton block with the previous block
1530 pimpl_->fontlist[i - 1].pos(pos);
1531 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
1533 pimpl_->fontlist[i].font(font);
1535 if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
1536 pimpl_->fontlist[i - 1].pos(pos);
1538 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
1539 Pimpl::FontTable(pos, font));
1541 pimpl_->fontlist[i].pos(pos - 1);
1542 if (!(i + 1 < pimpl_->fontlist.size() &&
1543 pimpl_->fontlist[i + 1].font() == font))
1544 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
1545 Pimpl::FontTable(pos, font));
1546 } else { // The general case. The block is splitted into 3 blocks
1547 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
1548 Pimpl::FontTable(pos - 1, pimpl_->fontlist[i].font()));
1549 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
1550 Pimpl::FontTable(pos, font));
1555 void Paragraph::makeSameLayout(Paragraph const & par)
1557 layout(par.layout());
1559 params() = par.params();
1563 bool Paragraph::stripLeadingSpaces(bool trackChanges)
1565 if (isFreeSpacing())
1571 while (pos < size() && (isNewline(pos) || isLineSeparator(pos))) {
1572 if (eraseChar(pos, trackChanges))
1578 return count > 0 || pos > 0;
1582 bool Paragraph::hasSameLayout(Paragraph const & par) const
1584 return par.layout() == layout() && params().sameLayout(par.params());
1588 depth_type Paragraph::getDepth() const
1590 return params().depth();
1594 depth_type Paragraph::getMaxDepthAfter() const
1596 if (layout()->isEnvironment())
1597 return params().depth() + 1;
1599 return params().depth();
1603 char Paragraph::getAlign() const
1605 if (params().align() == LYX_ALIGN_LAYOUT)
1606 return layout()->align;
1608 return params().align();
1612 docstring const & Paragraph::getLabelstring() const
1614 return params().labelString();
1618 // the next two functions are for the manual labels
1619 docstring const Paragraph::getLabelWidthString() const
1621 if (!params().labelWidthString().empty())
1622 return params().labelWidthString();
1624 return _("Senseless with this layout!");
1628 void Paragraph::setLabelWidthString(docstring const & s)
1630 params().labelWidthString(s);
1634 docstring const Paragraph::translateIfPossible(docstring const & s,
1635 BufferParams const & bparams) const
1637 if (!support::isAscii(s) || s.empty()) {
1638 // This must be a user defined layout. We cannot translate
1639 // this, since gettext accepts only ascii keys.
1642 // Probably standard layout, try to translate
1643 Messages & m = getMessages(getParLanguage(bparams)->code());
1644 return m.get(to_ascii(s));
1648 docstring Paragraph::expandLabel(LayoutPtr const & layout,
1649 BufferParams const & bparams, bool process_appendix) const
1651 TextClass const & tclass = bparams.getTextClass();
1654 if (process_appendix && params().appendix())
1655 fmt = translateIfPossible(layout->labelstring_appendix(),
1658 fmt = translateIfPossible(layout->labelstring(), bparams);
1660 if (fmt.empty() && layout->labeltype == LABEL_COUNTER
1661 && !layout->counter.empty())
1662 fmt = "\\the" + layout->counter;
1664 // handle 'inherited level parts' in 'fmt',
1665 // i.e. the stuff between '@' in '@Section@.\arabic{subsection}'
1666 size_t const i = fmt.find('@', 0);
1667 if (i != docstring::npos) {
1668 size_t const j = fmt.find('@', i + 1);
1669 if (j != docstring::npos) {
1670 docstring parent(fmt, i + 1, j - i - 1);
1671 docstring label = expandLabel(tclass[parent], bparams,
1673 fmt = docstring(fmt, 0, i) + label
1674 + docstring(fmt, j + 1, docstring::npos);
1678 return tclass.counters().counterLabel(fmt);
1682 void Paragraph::applyLayout(LayoutPtr const & new_layout)
1685 LyXAlignment const oldAlign = params().align();
1687 if (!(oldAlign & layout()->alignpossible)) {
1688 frontend::Alert::warning(_("Alignment not permitted"),
1689 _("The new layout does not permit the alignment previously used.\nSetting to default."));
1690 params().align(LYX_ALIGN_LAYOUT);
1695 pos_type Paragraph::beginOfBody() const
1697 return begin_of_body_;
1701 void Paragraph::setBeginOfBody()
1703 if (layout()->labeltype != LABEL_MANUAL) {
1708 // Unroll the first two cycles of the loop
1709 // and remember the previous character to
1710 // remove unnecessary getChar() calls
1712 pos_type end = size();
1713 if (i < end && !isNewline(i)) {
1715 char_type previous_char = 0;
1718 previous_char = text_[i];
1719 if (!isNewline(i)) {
1721 while (i < end && previous_char != ' ') {
1726 previous_char = temp;
1736 // returns -1 if inset not found
1737 int Paragraph::getPositionOfInset(Inset const * inset) const
1740 InsetList::const_iterator it = insetlist.begin();
1741 InsetList::const_iterator end = insetlist.end();
1742 for (; it != end; ++it)
1743 if (it->inset == inset)
1749 InsetBibitem * Paragraph::bibitem() const
1751 if (!insetlist.empty()) {
1752 Inset * inset = insetlist.begin()->inset;
1753 if (inset->lyxCode() == Inset::BIBITEM_CODE)
1754 return static_cast<InsetBibitem *>(inset);
1760 bool Paragraph::forceDefaultParagraphs() const
1762 return inInset() && inInset()->forceDefaultParagraphs(0);
1768 // paragraphs inside floats need different alignment tags to avoid
1771 bool noTrivlistCentering(Inset::Code code)
1773 return code == Inset::FLOAT_CODE || code == Inset::WRAP_CODE;
1777 string correction(string const & orig)
1779 if (orig == "flushleft")
1780 return "raggedright";
1781 if (orig == "flushright")
1782 return "raggedleft";
1783 if (orig == "center")
1789 string const corrected_env(string const & suffix, string const & env,
1792 string output = suffix + "{";
1793 if (noTrivlistCentering(code))
1794 output += correction(env);
1798 if (suffix == "\\begin")
1804 void adjust_row_column(string const & str, TexRow & texrow, int & column)
1806 if (!contains(str, "\n"))
1807 column += str.size();
1811 column = rsplit(str, tmp, '\n').size();
1818 // This could go to ParagraphParameters if we want to
1819 int Paragraph::startTeXParParams(BufferParams const & bparams,
1820 odocstream & os, TexRow & texrow,
1821 bool moving_arg) const
1825 if (params().noindent()) {
1826 os << "\\noindent ";
1830 LyXAlignment const curAlign = params().align();
1832 if (curAlign == layout()->align)
1836 case LYX_ALIGN_NONE:
1837 case LYX_ALIGN_BLOCK:
1838 case LYX_ALIGN_LAYOUT:
1839 case LYX_ALIGN_SPECIAL:
1841 case LYX_ALIGN_LEFT:
1842 case LYX_ALIGN_RIGHT:
1843 case LYX_ALIGN_CENTER:
1852 case LYX_ALIGN_NONE:
1853 case LYX_ALIGN_BLOCK:
1854 case LYX_ALIGN_LAYOUT:
1855 case LYX_ALIGN_SPECIAL:
1857 case LYX_ALIGN_LEFT: {
1859 if (getParLanguage(bparams)->babel() != "hebrew")
1860 output = corrected_env("\\begin", "flushleft", ownerCode());
1862 output = corrected_env("\\begin", "flushright", ownerCode());
1863 os << from_ascii(output);
1864 adjust_row_column(output, texrow, column);
1866 } case LYX_ALIGN_RIGHT: {
1868 if (getParLanguage(bparams)->babel() != "hebrew")
1869 output = corrected_env("\\begin", "flushright", ownerCode());
1871 output = corrected_env("\\begin", "flushleft", ownerCode());
1872 os << from_ascii(output);
1873 adjust_row_column(output, texrow, column);
1875 } case LYX_ALIGN_CENTER: {
1877 output = corrected_env("\\begin", "center", ownerCode());
1878 os << from_ascii(output);
1879 adjust_row_column(output, texrow, column);
1888 // This could go to ParagraphParameters if we want to
1889 int Paragraph::endTeXParParams(BufferParams const & bparams,
1890 odocstream & os, TexRow & texrow,
1891 bool moving_arg) const
1895 switch (params().align()) {
1896 case LYX_ALIGN_NONE:
1897 case LYX_ALIGN_BLOCK:
1898 case LYX_ALIGN_LAYOUT:
1899 case LYX_ALIGN_SPECIAL:
1901 case LYX_ALIGN_LEFT:
1902 case LYX_ALIGN_RIGHT:
1903 case LYX_ALIGN_CENTER:
1911 switch (params().align()) {
1912 case LYX_ALIGN_NONE:
1913 case LYX_ALIGN_BLOCK:
1914 case LYX_ALIGN_LAYOUT:
1915 case LYX_ALIGN_SPECIAL:
1917 case LYX_ALIGN_LEFT: {
1919 if (getParLanguage(bparams)->babel() != "hebrew")
1920 output = corrected_env("\n\\par\\end", "flushleft", ownerCode());
1922 output = corrected_env("\n\\par\\end", "flushright", ownerCode());
1923 os << from_ascii(output);
1924 adjust_row_column(output, texrow, column);
1926 } case LYX_ALIGN_RIGHT: {
1928 if (getParLanguage(bparams)->babel() != "hebrew")
1929 output = corrected_env("\n\\par\\end", "flushright", ownerCode());
1931 output = corrected_env("\n\\par\\end", "flushleft", ownerCode());
1932 os << from_ascii(output);
1933 adjust_row_column(output, texrow, column);
1935 } case LYX_ALIGN_CENTER: {
1937 output = corrected_env("\n\\par\\end", "center", ownerCode());
1938 os << from_ascii(output);
1939 adjust_row_column(output, texrow, column);
1948 // This one spits out the text of the paragraph
1949 bool Paragraph::simpleTeXOnePar(Buffer const & buf,
1950 BufferParams const & bparams,
1951 Font const & outerfont,
1952 odocstream & os, TexRow & texrow,
1953 OutputParams const & runparams) const
1955 LYXERR(Debug::LATEX) << "SimpleTeXOnePar... " << this << endl;
1957 bool return_value = false;
1961 // well we have to check if we are in an inset with unlimited
1962 // length (all in one row) if that is true then we don't allow
1963 // any special options in the paragraph and also we don't allow
1964 // any environment other than the default layout of the text class
1966 bool asdefault = forceDefaultParagraphs();
1969 style = bparams.getTextClass().defaultLayout();
1974 // Current base font for all inherited font changes, without any
1975 // change caused by an individual character, except for the language:
1976 // It is set to the language of the first character.
1977 // As long as we are in the label, this font is the base font of the
1978 // label. Before the first body character it is set to the base font
1982 // Maybe we have to create a optional argument.
1983 pos_type body_pos = beginOfBody();
1984 unsigned int column = 0;
1987 // the optional argument is kept in curly brackets in
1988 // case it contains a ']'
1991 basefont = getLabelFont(bparams, outerfont);
1993 basefont = getLayoutFont(bparams, outerfont);
1996 // Which font is currently active?
1997 Font running_font(basefont);
1998 // Do we have an open font change?
1999 bool open_font = false;
2001 Change runningChange = Change(Change::UNCHANGED);
2003 texrow.start(id(), 0);
2005 // if the paragraph is empty, the loop will not be entered at all
2007 if (style->isCommand()) {
2012 column += startTeXParParams(bparams, os, texrow,
2013 runparams.moving_arg);
2016 for (pos_type i = 0; i < size(); ++i) {
2017 // First char in paragraph or after label?
2018 if (i == body_pos) {
2021 column += running_font.latexWriteEndChanges(
2022 os, bparams, runparams,
2023 basefont, basefont);
2026 basefont = getLayoutFont(bparams, outerfont);
2027 running_font = basefont;
2029 column += Changes::latexMarkChange(os, bparams,
2030 runningChange, Change(Change::UNCHANGED));
2031 runningChange = Change(Change::UNCHANGED);
2036 if (style->isCommand()) {
2042 column += startTeXParParams(bparams, os,
2044 runparams.moving_arg);
2047 Change const & change = runparams.inDeletedInset ? runparams.changeOfDeletedInset
2048 : pimpl_->lookupChange(i);
2050 if (bparams.outputChanges && runningChange != change) {
2052 column += running_font.latexWriteEndChanges(
2053 os, bparams, runparams, basefont, basefont);
2056 basefont = getLayoutFont(bparams, outerfont);
2057 running_font = basefont;
2059 column += Changes::latexMarkChange(os, bparams, runningChange, change);
2060 runningChange = change;
2063 // do not output text which is marked deleted
2064 // if change tracking output is disabled
2065 if (!bparams.outputChanges && change.type == Change::DELETED) {
2071 value_type const c = getChar(i);
2073 // Fully instantiated font
2074 Font const font = getFont(bparams, i, outerfont);
2076 Font const last_font = running_font;
2078 // Do we need to close the previous font?
2080 (font != running_font ||
2081 font.language() != running_font.language()))
2083 column += running_font.latexWriteEndChanges(
2084 os, bparams, runparams, basefont,
2085 (i == body_pos-1) ? basefont : font);
2086 running_font = basefont;
2090 // Switch file encoding if necessary
2091 if (runparams.encoding->package() == Encoding::inputenc &&
2092 font.language()->encoding()->package() == Encoding::inputenc) {
2093 std::pair<bool, int> const enc_switch = switchEncoding(os, bparams,
2094 runparams.moving_arg, *(runparams.encoding),
2095 *(font.language()->encoding()));
2096 if (enc_switch.first) {
2097 column += enc_switch.second;
2098 runparams.encoding = font.language()->encoding();
2102 // Do we need to change font?
2103 if ((font != running_font ||
2104 font.language() != running_font.language()) &&
2107 odocstringstream ods;
2108 column += font.latexWriteStartChanges(ods, bparams,
2109 runparams, basefont,
2111 running_font = font;
2113 docstring fontchange = ods.str();
2114 // check if the fontchange ends with a trailing blank
2115 // (like "\small " (see bug 3382)
2116 if (suffixIs(fontchange, ' ') && c == ' ')
2117 os << fontchange.substr(0, fontchange.size() - 1)
2118 << from_ascii("{}");
2124 // Do not print the separation of the optional argument
2125 // if style->pass_thru is false. This works because
2126 // simpleTeXSpecialChars ignores spaces if
2127 // style->pass_thru is false.
2128 if (i != body_pos - 1) {
2129 if (pimpl_->simpleTeXBlanks(
2130 *(runparams.encoding), os, texrow,
2131 i, column, font, *style))
2132 // A surrogate pair was output. We
2133 // must not call simpleTeXSpecialChars
2134 // in this iteration, since
2135 // simpleTeXBlanks incremented i, and
2136 // simpleTeXSpecialChars would output
2137 // the combining character again.
2142 OutputParams rp = runparams;
2143 rp.free_spacing = style->free_spacing;
2144 rp.local_font = &font;
2145 rp.intitle = style->intitle;
2146 pimpl_->simpleTeXSpecialChars(buf, bparams, os,
2147 texrow, rp, running_font,
2148 basefont, outerfont, open_font,
2149 runningChange, *style, i, column, c);
2151 // Set the encoding to that returned from simpleTeXSpecialChars (see
2152 // comment for encoding member in OutputParams.h)
2153 runparams.encoding = rp.encoding;
2156 // If we have an open font definition, we have to close it
2158 #ifdef FIXED_LANGUAGE_END_DETECTION
2161 .latexWriteEndChanges(os, bparams, runparams,
2163 next_->getFont(bparams, 0, outerfont));
2165 running_font.latexWriteEndChanges(os, bparams,
2166 runparams, basefont, basefont);
2169 //FIXME: For now we ALWAYS have to close the foreign font settings if they are
2170 //FIXME: there as we start another \selectlanguage with the next paragraph if
2171 //FIXME: we are in need of this. This should be fixed sometime (Jug)
2172 running_font.latexWriteEndChanges(os, bparams, runparams,
2173 basefont, basefont);
2177 column += Changes::latexMarkChange(os, bparams, runningChange, Change(Change::UNCHANGED));
2179 // Needed if there is an optional argument but no contents.
2180 if (body_pos > 0 && body_pos == size()) {
2182 return_value = false;
2186 column += endTeXParParams(bparams, os, texrow,
2187 runparams.moving_arg);
2190 LYXERR(Debug::LATEX) << "SimpleTeXOnePar...done " << this << endl;
2191 return return_value;
2208 string tag_name(PAR_TAG const & pt) {
2210 case PAR_NONE: return "!-- --";
2211 case TT: return "tt";
2212 case SF: return "sf";
2213 case BF: return "bf";
2214 case IT: return "it";
2215 case SL: return "sl";
2216 case EM: return "em";
2223 void operator|=(PAR_TAG & p1, PAR_TAG const & p2)
2225 p1 = static_cast<PAR_TAG>(p1 | p2);
2230 void reset(PAR_TAG & p1, PAR_TAG const & p2)
2232 p1 = static_cast<PAR_TAG>(p1 & ~p2);
2238 bool Paragraph::emptyTag() const
2240 for (pos_type i = 0; i < size(); ++i) {
2242 Inset const * inset = getInset(i);
2243 Inset::Code lyx_code = inset->lyxCode();
2244 if (lyx_code != Inset::TOC_CODE &&
2245 lyx_code != Inset::INCLUDE_CODE &&
2246 lyx_code != Inset::GRAPHICS_CODE &&
2247 lyx_code != Inset::ERT_CODE &&
2248 lyx_code != Inset::LISTINGS_CODE &&
2249 lyx_code != Inset::FLOAT_CODE &&
2250 lyx_code != Inset::TABULAR_CODE) {
2254 value_type c = getChar(i);
2255 if (c != ' ' && c != '\t')
2263 string Paragraph::getID(Buffer const & buf, OutputParams const & runparams) const
2265 for (pos_type i = 0; i < size(); ++i) {
2267 Inset const * inset = getInset(i);
2268 Inset::Code lyx_code = inset->lyxCode();
2269 if (lyx_code == Inset::LABEL_CODE) {
2270 string const id = static_cast<InsetCommand const *>(inset)->getContents();
2271 return "id='" + to_utf8(sgml::cleanID(buf, runparams, from_utf8(id))) + "'";
2280 pos_type Paragraph::getFirstWord(Buffer const & buf, odocstream & os, OutputParams const & runparams) const
2283 for (i = 0; i < size(); ++i) {
2285 Inset const * inset = getInset(i);
2286 inset->docbook(buf, os, runparams);
2288 value_type c = getChar(i);
2291 os << sgml::escapeChar(c);
2298 bool Paragraph::onlyText(Buffer const & buf, Font const & outerfont, pos_type initial) const
2302 for (pos_type i = initial; i < size(); ++i) {
2303 Font font = getFont(buf.params(), i, outerfont);
2306 if (i != initial && font != font_old)
2315 void Paragraph::simpleDocBookOnePar(Buffer const & buf,
2317 OutputParams const & runparams,
2318 Font const & outerfont,
2319 pos_type initial) const
2321 bool emph_flag = false;
2323 LayoutPtr const & style = layout();
2325 style->labeltype == LABEL_MANUAL ? style->labelfont : style->font;
2327 if (style->pass_thru && !onlyText(buf, outerfont, initial))
2330 // parsing main loop
2331 for (pos_type i = initial; i < size(); ++i) {
2332 Font font = getFont(buf.params(), i, outerfont);
2334 // handle <emphasis> tag
2335 if (font_old.emph() != font.emph()) {
2336 if (font.emph() == Font::ON) {
2339 } else if (i != initial) {
2340 os << "</emphasis>";
2346 Inset const * inset = getInset(i);
2347 inset->docbook(buf, os, runparams);
2349 value_type c = getChar(i);
2351 if (style->pass_thru)
2354 os << sgml::escapeChar(c);
2360 os << "</emphasis>";
2363 if (style->free_spacing)
2365 if (style->pass_thru && !onlyText(buf, outerfont, initial))
2370 bool Paragraph::isNewline(pos_type pos) const
2373 && getInset(pos)->lyxCode() == Inset::NEWLINE_CODE;
2377 bool Paragraph::isLineSeparator(pos_type pos) const
2379 value_type const c = getChar(pos);
2380 return isLineSeparatorChar(c)
2381 || (c == Paragraph::META_INSET && getInset(pos) &&
2382 getInset(pos)->isLineSeparator());
2386 /// Used by the spellchecker
2387 bool Paragraph::isLetter(pos_type pos) const
2390 return getInset(pos)->isLetter();
2392 value_type const c = getChar(pos);
2393 return isLetterChar(c) || isDigit(c);
2399 Paragraph::getParLanguage(BufferParams const & bparams) const
2402 return getFirstFontSettings(bparams).language();
2403 // FIXME: we should check the prev par as well (Lgb)
2404 return bparams.language;
2408 bool Paragraph::isRightToLeftPar(BufferParams const & bparams) const
2410 return lyxrc.rtl_support
2411 && getParLanguage(bparams)->rightToLeft()
2412 && ownerCode() != Inset::ERT_CODE
2413 && ownerCode() != Inset::LISTINGS_CODE;
2417 void Paragraph::changeLanguage(BufferParams const & bparams,
2418 Language const * from, Language const * to)
2420 // change language including dummy font change at the end
2421 for (pos_type i = 0; i <= size(); ++i) {
2422 Font font = getFontSettings(bparams, i);
2423 if (font.language() == from) {
2424 font.setLanguage(to);
2431 bool Paragraph::isMultiLingual(BufferParams const & bparams) const
2433 Language const * doc_language = bparams.language;
2434 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
2435 Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
2437 for (; cit != end; ++cit)
2438 if (cit->font().language() != ignore_language &&
2439 cit->font().language() != latex_language &&
2440 cit->font().language() != doc_language)
2446 // Convert the paragraph to a string.
2447 // Used for building the table of contents
2448 docstring const Paragraph::asString(Buffer const & buffer, bool label) const
2450 return asString(buffer, 0, size(), label);
2454 docstring const Paragraph::asString(Buffer const & buffer,
2455 pos_type beg, pos_type end, bool label) const
2458 odocstringstream os;
2460 if (beg == 0 && label && !params().labelString().empty())
2461 os << params().labelString() << ' ';
2463 for (pos_type i = beg; i < end; ++i) {
2464 value_type const c = getChar(i);
2467 else if (c == META_INSET)
2468 getInset(i)->textString(buffer, os);
2475 void Paragraph::setInsetOwner(Inset * inset)
2477 pimpl_->inset_owner = inset;
2481 Change const & Paragraph::lookupChange(pos_type pos) const
2483 BOOST_ASSERT(pos <= size());
2484 return pimpl_->lookupChange(pos);
2488 bool Paragraph::isChanged(pos_type start, pos_type end) const
2490 return pimpl_->isChanged(start, end);
2494 bool Paragraph::isMergedOnEndOfParDeletion(bool trackChanges) const
2496 return pimpl_->isMergedOnEndOfParDeletion(trackChanges);
2500 void Paragraph::setChange(Change const & change)
2502 pimpl_->setChange(change);
2506 void Paragraph::setChange(pos_type pos, Change const & change)
2508 pimpl_->setChange(pos, change);
2512 void Paragraph::acceptChanges(BufferParams const & bparams, pos_type start, pos_type end)
2514 return pimpl_->acceptChanges(bparams, start, end);
2518 void Paragraph::rejectChanges(BufferParams const & bparams, pos_type start, pos_type end)
2520 return pimpl_->rejectChanges(bparams, start, end);
2524 int Paragraph::id() const
2530 LayoutPtr const & Paragraph::layout() const
2536 void Paragraph::layout(LayoutPtr const & new_layout)
2538 layout_ = new_layout;
2542 Inset * Paragraph::inInset() const
2544 return pimpl_->inset_owner;
2548 Inset::Code Paragraph::ownerCode() const
2550 return pimpl_->inset_owner
2551 ? pimpl_->inset_owner->lyxCode() : Inset::NO_CODE;
2555 ParagraphParameters & Paragraph::params()
2557 return pimpl_->params;
2561 ParagraphParameters const & Paragraph::params() const
2563 return pimpl_->params;
2567 bool Paragraph::isFreeSpacing() const
2569 if (layout()->free_spacing)
2572 // for now we just need this, later should we need this in some
2573 // other way we can always add a function to Inset too.
2574 return ownerCode() == Inset::ERT_CODE || ownerCode() == Inset::LISTINGS_CODE;
2578 bool Paragraph::allowEmpty() const
2580 if (layout()->keepempty)
2582 return ownerCode() == Inset::ERT_CODE || ownerCode() == Inset::LISTINGS_CODE;
2586 char_type Paragraph::transformChar(char_type c, pos_type pos) const
2588 if (!Encodings::is_arabic(c))
2591 value_type prev_char = ' ';
2592 value_type next_char = ' ';
2594 for (pos_type i = pos - 1; i >= 0; --i) {
2595 value_type const par_char = getChar(i);
2596 if (!Encodings::isComposeChar_arabic(par_char)) {
2597 prev_char = par_char;
2602 for (pos_type i = pos + 1, end = size(); i < end; ++i) {
2603 value_type const par_char = getChar(i);
2604 if (!Encodings::isComposeChar_arabic(par_char)) {
2605 next_char = par_char;
2610 if (Encodings::is_arabic(next_char)) {
2611 if (Encodings::is_arabic(prev_char) &&
2612 !Encodings::is_arabic_special(prev_char))
2613 return Encodings::transformChar(c, Encodings::FORM_MEDIAL);
2615 return Encodings::transformChar(c, Encodings::FORM_INITIAL);
2617 if (Encodings::is_arabic(prev_char) &&
2618 !Encodings::is_arabic_special(prev_char))
2619 return Encodings::transformChar(c, Encodings::FORM_FINAL);
2621 return Encodings::transformChar(c, Encodings::FORM_ISOLATED);
2626 int Paragraph::checkBiblio(bool track_changes)
2629 //This is getting more and more a mess. ...We really should clean
2630 //up this bibitem issue for 1.6. See also bug 2743.
2632 // Add bibitem insets if necessary
2633 if (layout()->labeltype != LABEL_BIBLIO)
2636 bool hasbibitem = !insetlist.empty()
2637 // Insist on it being in pos 0
2638 && getChar(0) == Paragraph::META_INSET
2639 && insetlist.begin()->inset->lyxCode() == Inset::BIBITEM_CODE;
2644 // remove a bibitem in pos != 0
2645 // restore it later in pos 0 if necessary
2646 // (e.g. if a user inserts contents _before_ the item)
2647 // we're assuming there's only one of these, which there
2649 int erasedInsetPosition = -1;
2650 InsetList::iterator it = insetlist.begin();
2651 InsetList::iterator end = insetlist.end();
2652 for (; it != end; ++it)
2653 if (it->inset->lyxCode() == Inset::BIBITEM_CODE
2655 InsetBibitem * olditem = static_cast<InsetBibitem *>(it->inset);
2656 oldkey = olditem->getParam("key");
2657 oldlabel = olditem->getParam("label");
2658 erasedInsetPosition = it->pos;
2659 eraseChar(erasedInsetPosition, track_changes);
2663 //There was an InsetBibitem at the beginning, and we didn't
2664 //have to erase one.
2665 if (hasbibitem && erasedInsetPosition < 0)
2668 //There was an InsetBibitem at the beginning and we did have to
2669 //erase one. So we give its properties to the beginning inset.
2671 InsetBibitem * inset =
2672 static_cast<InsetBibitem *>(insetlist.begin()->inset);
2673 if (!oldkey.empty())
2674 inset->setParam("key", oldkey);
2675 inset->setParam("label", oldlabel);
2676 return -erasedInsetPosition;
2679 //There was no inset at the beginning, so we need to create one with
2680 //the key and label of the one we erased.
2681 InsetBibitem * inset(new InsetBibitem(InsetCommandParams("bibitem")));
2682 // restore values of previously deleted item in this par.
2683 if (!oldkey.empty())
2684 inset->setParam("key", oldkey);
2685 inset->setParam("label", oldlabel);
2686 insertInset(0, static_cast<Inset *>(inset),
2687 Change(track_changes ? Change::INSERTED : Change::UNCHANGED));
2693 void Paragraph::checkAuthors(AuthorList const & authorList)
2695 pimpl_->changes_.checkAuthors(authorList);