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 "InsetList.h"
31 #include "LaTeXFeatures.h"
38 #include "OutputParams.h"
39 #include "output_latex.h"
40 #include "paragraph_funcs.h"
41 #include "ParagraphParameters.h"
43 #include "TextClass.h"
47 #include "frontends/alert.h"
48 #include "frontends/FontMetrics.h"
50 #include "insets/InsetBibitem.h"
51 #include "insets/InsetLabel.h"
52 #include "insets/InsetOptArg.h"
54 #include "support/lstrings.h"
55 #include "support/textutils.h"
56 #include "support/convert.h"
57 #include "support/unicode.h"
68 using support::contains;
69 using support::lowercase;
70 using support::prefixIs;
71 using support::suffixIs;
72 using support::rsplit;
73 using support::uppercase;
76 /// Inset identifier (above 0x10ffff, for ucs-4)
77 char_type const META_INSET = 0x200001;
80 /////////////////////////////////////////////////////////////////////
84 /////////////////////////////////////////////////////////////////////
86 class Paragraph::Private
90 Private(Paragraph * owner);
91 /// "Copy constructor"
92 Private(Private const &, Paragraph * owner);
95 void insertChar(pos_type pos, char_type c, Change const & change);
97 /// Output the surrogate pair formed by \p c and \p next to \p os.
98 /// \return the number of characters written.
99 int latexSurrogatePair(odocstream & os, char_type c, char_type next,
102 /// Output a space in appropriate formatting (or a surrogate pair
103 /// if the next character is a combining character).
104 /// \return whether a surrogate pair was output.
105 bool simpleTeXBlanks(OutputParams const &,
106 odocstream &, TexRow & texrow,
108 unsigned int & column,
110 Layout const & style);
112 /// Output consecutive unicode chars, belonging to the same script as
113 /// specified by the latex macro \p ltx, to \p os starting from \p i.
114 /// \return the number of characters written.
115 int writeScriptChars(odocstream & os, docstring const & ltx,
116 Change &, Encoding const &, pos_type & i);
118 /// This could go to ParagraphParameters if we want to.
119 int startTeXParParams(BufferParams const &, odocstream &, TexRow &,
122 /// This could go to ParagraphParameters if we want to.
123 int endTeXParParams(BufferParams const &, odocstream &, TexRow &,
127 void latexInset(Buffer const &, BufferParams const &,
129 TexRow & texrow, OutputParams &,
132 Font const & outerfont,
134 Change & running_change,
135 Layout const & style,
137 unsigned int & column);
140 void latexSpecialChar(
142 OutputParams & runparams,
144 Change & running_change,
145 Layout const & style,
147 unsigned int & column);
154 unsigned int & column);
156 bool latexSpecialTypewriter(
160 unsigned int & column);
162 bool latexSpecialPhrase(
165 unsigned int & column,
166 OutputParams & runparams);
169 void validate(LaTeXFeatures & features,
170 Layout const & layout) const;
172 /// Checks if the paragraph contains only text and no inset or font change.
173 bool onlyText(Buffer const & buf, Font const & outerfont,
174 pos_type initial) const;
176 /// match a string against a particular point in the paragraph
177 bool isTextAt(std::string const & str, pos_type pos) const;
179 /// Which Paragraph owns us?
183 Inset * inset_owner_;
191 static unsigned int paragraph_id;
193 ParagraphParameters params_;
195 /// position of the paragraph in the buffer. Only macros from
196 /// paragraphs strictly smaller are visible in this paragraph
197 unsigned int macrocontext_position_;
199 /// for recording and looking up changes
203 InsetList insetlist_;
209 pos_type begin_of_body_;
211 typedef docstring TextContainer;
220 using std::upper_bound;
221 using std::lower_bound;
225 // Initialization of the counter for the paragraph id's,
226 unsigned int Paragraph::Private::paragraph_id = 0;
230 struct special_phrase {
236 special_phrase const special_phrases[] = {
237 { "LyX", from_ascii("\\LyX{}"), false },
238 { "TeX", from_ascii("\\TeX{}"), true },
239 { "LaTeX2e", from_ascii("\\LaTeXe{}"), true },
240 { "LaTeX", from_ascii("\\LaTeX{}"), true },
243 size_t const phrases_nr = sizeof(special_phrases)/sizeof(special_phrase);
248 Paragraph::Private::Private(Paragraph * owner)
249 : owner_(owner), inset_owner_(0), begin_of_body_(0)
251 id_ = paragraph_id++;
252 macrocontext_position_ = 0;
257 Paragraph::Private::Private(Private const & p, Paragraph * owner)
258 : owner_(owner), inset_owner_(p.inset_owner_), fontlist_(p.fontlist_),
259 params_(p.params_), changes_(p.changes_), insetlist_(p.insetlist_),
260 layout_(p.layout_), begin_of_body_(p.begin_of_body_), text_(p.text_)
262 id_ = paragraph_id++;
266 bool Paragraph::isChanged(pos_type start, pos_type end) const
268 BOOST_ASSERT(start >= 0 && start <= size());
269 BOOST_ASSERT(end > start && end <= size() + 1);
271 return d->changes_.isChanged(start, end);
275 bool Paragraph::isMergedOnEndOfParDeletion(bool trackChanges) const {
276 // keep the logic here in sync with the logic of eraseChars()
282 Change change = d->changes_.lookup(size());
284 return change.type == Change::INSERTED && change.author == 0;
288 void Paragraph::setChange(Change const & change)
290 // beware of the imaginary end-of-par character!
291 d->changes_.set(change, 0, size() + 1);
294 * Propagate the change recursively - but not in case of DELETED!
296 * Imagine that your co-author makes changes in an existing inset. He
297 * sends your document to you and you come to the conclusion that the
298 * inset should go completely. If you erase it, LyX must not delete all
299 * text within the inset. Otherwise, the change tracked insertions of
300 * your co-author get lost and there is no way to restore them later.
302 * Conclusion: An inset's content should remain untouched if you delete it
305 if (change.type != Change::DELETED) {
306 for (pos_type pos = 0; pos < size(); ++pos) {
308 getInset(pos)->setChange(change);
314 void Paragraph::setChange(pos_type pos, Change const & change)
316 BOOST_ASSERT(pos >= 0 && pos <= size());
318 d->changes_.set(change, pos);
320 // see comment in setChange(Change const &) above
322 if (change.type != Change::DELETED &&
323 pos < size() && isInset(pos)) {
324 getInset(pos)->setChange(change);
329 Change const & Paragraph::lookupChange(pos_type pos) const
331 BOOST_ASSERT(pos >= 0 && pos <= size());
333 return d->changes_.lookup(pos);
337 void Paragraph::acceptChanges(BufferParams const & bparams, pos_type start,
340 BOOST_ASSERT(start >= 0 && start <= size());
341 BOOST_ASSERT(end > start && end <= size() + 1);
343 for (pos_type pos = start; pos < end; ++pos) {
344 switch (lookupChange(pos).type) {
345 case Change::UNCHANGED:
346 // accept changes in nested inset
347 if (pos < size() && isInset(pos))
348 getInset(pos)->acceptChanges(bparams);
352 case Change::INSERTED:
353 d->changes_.set(Change(Change::UNCHANGED), pos);
354 // also accept changes in nested inset
355 if (pos < size() && isInset(pos)) {
356 getInset(pos)->acceptChanges(bparams);
360 case Change::DELETED:
361 // Suppress access to non-existent
362 // "end-of-paragraph char"
364 eraseChar(pos, false);
375 void Paragraph::rejectChanges(BufferParams const & bparams,
376 pos_type start, pos_type end)
378 BOOST_ASSERT(start >= 0 && start <= size());
379 BOOST_ASSERT(end > start && end <= size() + 1);
381 for (pos_type pos = start; pos < end; ++pos) {
382 switch (lookupChange(pos).type) {
383 case Change::UNCHANGED:
384 // reject changes in nested inset
385 if (pos < size() && isInset(pos)) {
386 getInset(pos)->rejectChanges(bparams);
390 case Change::INSERTED:
391 // Suppress access to non-existent
392 // "end-of-paragraph char"
394 eraseChar(pos, false);
400 case Change::DELETED:
401 d->changes_.set(Change(Change::UNCHANGED), pos);
403 // Do NOT reject changes within a deleted inset!
404 // There may be insertions of a co-author inside of it!
412 void Paragraph::Private::insertChar(pos_type pos, char_type c,
413 Change const & change)
415 BOOST_ASSERT(pos >= 0 && pos <= int(text_.size()));
418 changes_.insert(change, pos);
420 // This is actually very common when parsing buffers (and
421 // maybe inserting ascii text)
422 if (pos == pos_type(text_.size())) {
423 // when appending characters, no need to update tables
428 text_.insert(text_.begin() + pos, c);
430 // Update the font table.
431 fontlist_.increasePosAfterPos(pos);
434 insetlist_.increasePosAfterPos(pos);
438 void Paragraph::insertInset(pos_type pos, Inset * inset,
439 Change const & change)
442 BOOST_ASSERT(pos >= 0 && pos <= size());
444 d->insertChar(pos, META_INSET, change);
445 BOOST_ASSERT(d->text_[pos] == META_INSET);
447 // Add a new entry in the insetlist_.
448 d->insetlist_.insert(inset, pos);
452 bool Paragraph::eraseChar(pos_type pos, bool trackChanges)
454 BOOST_ASSERT(pos >= 0 && pos <= size());
456 // keep the logic here in sync with the logic of isMergedOnEndOfParDeletion()
459 Change change = d->changes_.lookup(pos);
461 // set the character to DELETED if
462 // a) it was previously unchanged or
463 // b) it was inserted by a co-author
465 if (change.type == Change::UNCHANGED ||
466 (change.type == Change::INSERTED && change.author != 0)) {
467 setChange(pos, Change(Change::DELETED));
471 if (change.type == Change::DELETED)
475 // Don't physically access the imaginary end-of-paragraph character.
476 // eraseChar() can only mark it as DELETED. A physical deletion of
477 // end-of-par must be handled externally.
483 d->changes_.erase(pos);
485 // if it is an inset, delete the inset entry
486 if (d->text_[pos] == META_INSET)
487 d->insetlist_.erase(pos);
489 d->text_.erase(d->text_.begin() + pos);
491 // Update the fontlist_
492 d->fontlist_.erase(pos);
494 // Update the insetlist_
495 d->insetlist_.decreasePosAfterPos(pos);
501 int Paragraph::eraseChars(pos_type start, pos_type end, bool trackChanges)
503 BOOST_ASSERT(start >= 0 && start <= size());
504 BOOST_ASSERT(end >= start && end <= size() + 1);
507 for (pos_type count = end - start; count; --count) {
508 if (!eraseChar(i, trackChanges))
515 int Paragraph::Private::latexSurrogatePair(odocstream & os, char_type c,
516 char_type next, Encoding const & encoding)
518 // Writing next here may circumvent a possible font change between
519 // c and next. Since next is only output if it forms a surrogate pair
520 // with c we can ignore this:
521 // A font change inside a surrogate pair does not make sense and is
522 // hopefully impossible to input.
523 // FIXME: change tracking
524 // Is this correct WRT change tracking?
525 docstring const latex1 = encoding.latexChar(next);
526 docstring const latex2 = encoding.latexChar(c);
527 os << latex1 << '{' << latex2 << '}';
528 return latex1.length() + latex2.length() + 2;
532 bool Paragraph::Private::simpleTeXBlanks(OutputParams const & runparams,
533 odocstream & os, TexRow & texrow,
535 unsigned int & column,
537 Layout const & style)
539 if (style.pass_thru || runparams.verbatim)
542 if (i + 1 < int(text_.size())) {
543 char_type next = text_[i + 1];
544 if (Encodings::isCombiningChar(next)) {
545 Encoding const & encoding = *(runparams.encoding);
546 // This space has an accent, so we must always output it.
547 column += latexSurrogatePair(os, ' ', next, encoding) - 1;
552 if (lyxrc.plaintext_linelen > 0
553 && column > lyxrc.plaintext_linelen
555 && text_[i - 1] != ' '
556 && (i + 1 < int(text_.size()))
557 // same in FreeSpacing mode
558 && !owner_->isFreeSpacing()
559 // In typewriter mode, we want to avoid
560 // ! . ? : at the end of a line
561 && !(font.fontInfo().family() == TYPEWRITER_FAMILY
562 && (text_[i - 1] == '.'
563 || text_[i - 1] == '?'
564 || text_[i - 1] == ':'
565 || text_[i - 1] == '!'))) {
568 texrow.start(owner_->id(), i + 1);
570 } else if (style.free_spacing) {
579 int Paragraph::Private::writeScriptChars(odocstream & os,
580 docstring const & ltx,
581 Change & runningChange,
582 Encoding const & encoding,
585 // FIXME: modifying i here is not very nice...
587 // We only arrive here when a proper language for character text_[i] has
588 // not been specified (i.e., it could not be translated in the current
589 // latex encoding) and it belongs to a known script.
590 // Parameter ltx contains the latex translation of text_[i] as specified in
591 // the unicodesymbols file and is something like "\textXXX{<spec>}".
592 // The latex macro name "textXXX" specifies the script to which text_[i]
593 // belongs and we use it in order to check whether characters from the
594 // same script immediately follow, such that we can collect them in a
595 // single "\textXXX" macro. So, we have to retain "\textXXX{<spec>"
596 // for the first char but only "<spec>" for all subsequent chars.
597 docstring::size_type const brace1 = ltx.find_first_of(from_ascii("{"));
598 docstring::size_type const brace2 = ltx.find_last_of(from_ascii("}"));
599 string script = to_ascii(ltx.substr(1, brace1 - 1));
600 int length = ltx.substr(0, brace2).length();
601 os << ltx.substr(0, brace2);
602 int size = text_.size();
603 while (i + 1 < size) {
604 char_type const next = text_[i + 1];
605 // Stop here if next character belongs to another script
606 // or there is a change in change tracking status.
607 if (!Encodings::isKnownScriptChar(next, script) ||
608 runningChange != owner_->lookupChange(i + 1))
612 FontList::const_iterator cit = fontlist_.begin();
613 FontList::const_iterator end = fontlist_.end();
614 for (; cit != end; ++cit) {
615 if (cit->pos() >= i && !found) {
616 prev_font = cit->font();
619 if (cit->pos() >= i + 1)
622 // Stop here if there is a font attribute or encoding change.
623 if (found && cit != end && prev_font != cit->font())
625 docstring const latex = encoding.latexChar(next);
626 docstring::size_type const b1 =
627 latex.find_first_of(from_ascii("{"));
628 docstring::size_type const b2 =
629 latex.find_last_of(from_ascii("}"));
630 int const len = b2 - b1 - 1;
631 os << latex.substr(b1 + 1, len);
641 bool Paragraph::Private::isTextAt(string const & str, pos_type pos) const
643 pos_type const len = str.length();
645 // is the paragraph large enough?
646 if (pos + len > int(text_.size()))
649 // does the wanted text start at point?
650 for (string::size_type i = 0; i < str.length(); ++i) {
651 // Caution: direct comparison of characters works only
652 // because str is pure ASCII.
653 if (str[i] != text_[pos + i])
657 return fontlist_.hasChangeInRange(pos, len);
661 void Paragraph::Private::latexInset(Buffer const & buf,
662 BufferParams const & bparams,
665 OutputParams & runparams,
668 Font const & outerfont,
670 Change & running_change,
671 Layout const & style,
673 unsigned int & column)
675 Inset * inset = owner_->getInset(i);
678 if (style.pass_thru) {
679 inset->plaintext(buf, os, runparams);
683 // FIXME: move this to InsetNewline::latex
684 if (inset->lyxCode() == NEWLINE_CODE) {
685 // newlines are handled differently here than
686 // the default in simpleTeXSpecialChars().
687 if (!style.newline_allowed) {
691 column += running_font.latexWriteEndChanges(
692 os, bparams, runparams,
697 if (running_font.fontInfo().family() == TYPEWRITER_FAMILY)
700 basefont = owner_->getLayoutFont(bparams, outerfont);
701 running_font = basefont;
703 if (runparams.moving_arg)
709 texrow.start(owner_->id(), i + 1);
713 if (owner_->lookupChange(i).type == Change::DELETED) {
714 if( ++runparams.inDeletedInset == 1)
715 runparams.changeOfDeletedInset = owner_->lookupChange(i);
718 if (inset->canTrackChanges()) {
719 column += Changes::latexMarkChange(os, bparams, running_change,
720 Change(Change::UNCHANGED));
721 running_change = Change(Change::UNCHANGED);
725 odocstream::pos_type const len = os.tellp();
727 if (inset->forceLTR()
728 && running_font.isRightToLeft()
729 // ERT is an exception, it should be output with no decorations at all
730 && inset->lyxCode() != ERT_CODE) {
731 if (running_font.language()->lang() == "farsi")
738 // FIXME: Bug: we can have an empty font change here!
739 // if there has just been a font change, we are going to close it
740 // right now, which means stupid latex code like \textsf{}. AFAIK,
741 // this does not harm dvi output. A minor bug, thus (JMarc)
743 // Some insets cannot be inside a font change command.
744 // However, even such insets *can* be placed in \L or \R
745 // or their equivalents (for RTL language switches), so we don't
746 // close the language in those cases.
747 // ArabTeX, though, cannot handle this special behavior, it seems.
748 bool arabtex = basefont.language()->lang() == "arabic_arabtex"
749 || running_font.language()->lang() == "arabic_arabtex";
750 if (open_font && inset->noFontChange()) {
751 bool closeLanguage = arabtex
752 || basefont.isRightToLeft() == running_font.isRightToLeft();
753 unsigned int count = running_font.latexWriteEndChanges(os,
754 bparams, runparams, basefont, basefont, closeLanguage);
756 // if any font properties were closed, update the running_font,
757 // making sure, however, to leave the language as it was
759 // FIXME: probably a better way to keep track of the old
760 // language, than copying the entire font?
761 Font const copy_font(running_font);
762 basefont = owner_->getLayoutFont(bparams, outerfont);
763 running_font = basefont;
765 running_font.setLanguage(copy_font.language());
766 // leave font open if language is still open
767 open_font = (running_font.language() == basefont.language());
769 runparams.local_font = &basefont;
773 int tmp = inset->latex(buf, os, runparams);
776 if (running_font.language()->lang() == "farsi")
783 for (int j = 0; j < tmp; ++j)
786 texrow.start(owner_->id(), i + 1);
789 column += os.tellp() - len;
792 if (owner_->lookupChange(i).type == Change::DELETED)
793 --runparams.inDeletedInset;
797 void Paragraph::Private::latexSpecialChar(
799 OutputParams & runparams,
801 Change & running_change,
802 Layout const & style,
804 unsigned int & column)
806 char_type const c = text_[i];
808 if (style.pass_thru) {
810 // FIXME UNICODE: This can fail if c cannot
811 // be encoded in the current encoding.
816 if (runparams.verbatim) {
821 if (lyxrc.fontenc == "T1" && latexSpecialT1(c, os, i, column))
824 if (running_font.fontInfo().family() == TYPEWRITER_FAMILY
825 && latexSpecialTypewriter(c, os, i, column))
828 // Otherwise, we use what LaTeX provides us.
831 os << "\\textbackslash{}";
835 os << "\\textless{}";
839 os << "\\textgreater{}";
850 os << "\\char`\\\"{}";
855 case '%': case '#': case '{':
863 os << "\\textasciitilde{}";
868 os << "\\textasciicircum{}";
873 // avoid being mistaken for optional arguments
881 // Blanks are printed before font switching.
882 // Sure? I am not! (try nice-latex)
883 // I am sure it's correct. LyX might be smarter
884 // in the future, but for now, nothing wrong is
891 if (latexSpecialPhrase(os, i, column, runparams))
897 Encoding const & encoding = *(runparams.encoding);
898 if (i + 1 < int(text_.size())) {
899 char_type next = text_[i + 1];
900 if (Encodings::isCombiningChar(next)) {
901 column += latexSurrogatePair(os, c, next, encoding) - 1;
907 docstring const latex = encoding.latexChar(c);
908 if (Encodings::isKnownScriptChar(c, script)
909 && prefixIs(latex, from_ascii("\\" + script)))
910 column += writeScriptChars(os, latex,
911 running_change, encoding, i) - 1;
912 else if (latex.length() > 1 && latex[latex.length() - 1] != '}') {
913 // Prevent eating of a following
914 // space or command corruption by
915 // following characters
916 column += latex.length() + 1;
919 column += latex.length() - 1;
927 bool Paragraph::Private::latexSpecialT1(char_type const c, odocstream & os,
928 pos_type & i, unsigned int & column)
934 // In T1 encoding, these characters exist
935 // but we should avoid ligatures
936 if (i + 1 > int(text_.size()) || text_[i + 1] != c)
941 //os << "\\textcompwordmark{}";
953 bool Paragraph::Private::latexSpecialTypewriter(char_type const c, odocstream & os,
954 pos_type & i, unsigned int & column)
958 if (i + 1 < int(text_.size()) && text_[i + 1] == '-') {
959 // "--" in Typewriter mode -> "-{}-"
966 // I assume this is hack treating typewriter as verbatim
967 // FIXME UNICODE: This can fail if c cannot be encoded
968 // in the current encoding.
973 // Those characters are not directly supported.
977 case '%': case '#': case '{':
986 // With Typewriter font, these characters exist.
993 bool Paragraph::Private::latexSpecialPhrase(odocstream & os, pos_type & i,
994 unsigned int & column, OutputParams & runparams)
996 // FIXME: if we have "LaTeX" with a font
997 // change in the middle (before the 'T', then
998 // the "TeX" part is still special cased.
999 // Really we should only operate this on
1000 // "words" for some definition of word
1002 for (size_t pnr = 0; pnr < phrases_nr; ++pnr) {
1003 if (!isTextAt(special_phrases[pnr].phrase, i))
1005 if (runparams.moving_arg)
1007 os << special_phrases[pnr].macro;
1008 i += special_phrases[pnr].phrase.length() - 1;
1009 column += special_phrases[pnr].macro.length() - 1;
1016 void Paragraph::Private::validate(LaTeXFeatures & features,
1017 Layout const & layout) const
1019 // check the params.
1020 if (!params_.spacing().isDefault())
1021 features.require("setspace");
1024 features.useLayout(layout.name());
1027 fontlist_.validate(features);
1029 // then the indentation
1030 if (!params_.leftIndent().zero())
1031 features.require("ParagraphLeftIndent");
1034 InsetList::const_iterator icit = insetlist_.begin();
1035 InsetList::const_iterator iend = insetlist_.end();
1036 for (; icit != iend; ++icit) {
1038 icit->inset->validate(features);
1039 if (layout.needprotect &&
1040 icit->inset->lyxCode() == FOOT_CODE)
1041 features.require("NeedLyXFootnoteCode");
1045 // then the contents
1046 for (pos_type i = 0; i < int(text_.size()) ; ++i) {
1047 for (size_t pnr = 0; pnr < phrases_nr; ++pnr) {
1048 if (!special_phrases[pnr].builtin
1049 && isTextAt(special_phrases[pnr].phrase, i)) {
1050 features.require(special_phrases[pnr].phrase);
1054 Encodings::validate(text_[i], features);
1058 /////////////////////////////////////////////////////////////////////
1062 /////////////////////////////////////////////////////////////////////
1064 Paragraph::Paragraph()
1065 : d(new Paragraph::Private(this))
1072 Paragraph::Paragraph(Paragraph const & par)
1073 : itemdepth(par.itemdepth),
1074 d(new Paragraph::Private(*par.d, this))
1079 Paragraph & Paragraph::operator=(Paragraph const & par)
1081 // needed as we will destroy the private part before copying it
1083 itemdepth = par.itemdepth;
1086 d = new Private(*par.d, this);
1092 Paragraph::~Paragraph()
1098 void Paragraph::write(Buffer const & buf, ostream & os,
1099 BufferParams const & bparams,
1100 depth_type & dth) const
1102 // The beginning or end of a deeper (i.e. nested) area?
1103 if (dth != params().depth()) {
1104 if (params().depth() > dth) {
1105 while (params().depth() > dth) {
1106 os << "\n\\begin_deeper";
1110 while (params().depth() < dth) {
1111 os << "\n\\end_deeper";
1117 // First write the layout
1118 os << "\n\\begin_layout " << to_utf8(layout()->name()) << '\n';
1122 Font font1(inherit_font, bparams.language);
1124 Change running_change = Change(Change::UNCHANGED);
1127 for (pos_type i = 0; i <= size(); ++i) {
1129 Change change = lookupChange(i);
1130 Changes::lyxMarkChange(os, column, running_change, change);
1131 running_change = change;
1136 // Write font changes
1137 Font font2 = getFontSettings(bparams, i);
1138 if (font2 != font1) {
1139 font2.lyxWriteChanges(font1, os);
1144 char_type const c = d->text_[i];
1148 Inset const * inset = getInset(i);
1150 if (inset->directWrite()) {
1151 // international char, let it write
1152 // code directly so it's shorter in
1154 inset->write(buf, os);
1158 os << "\\begin_inset ";
1159 inset->write(buf, os);
1160 os << "\n\\end_inset\n\n";
1166 os << "\n\\backslash\n";
1170 if (i + 1 < size() && d->text_[i + 1] == ' ') {
1177 if ((column > 70 && c == ' ')
1182 // this check is to amend a bug. LyX sometimes
1183 // inserts '\0' this could cause problems.
1185 std::vector<char> tmp = ucs4_to_utf8(c);
1186 tmp.push_back('\0');
1189 lyxerr << "ERROR (Paragraph::writeFile):"
1190 " NULL char in structure." << endl;
1196 os << "\n\\end_layout\n";
1200 void Paragraph::validate(LaTeXFeatures & features) const
1202 d->validate(features, *layout());
1206 void Paragraph::insert(pos_type start, docstring const & str,
1207 Font const & font, Change const & change)
1209 for (size_t i = 0, n = str.size(); i != n ; ++i)
1210 insertChar(start + i, str[i], font, change);
1214 void Paragraph::appendChar(char_type c, Font const & font,
1215 Change const & change)
1218 d->changes_.insert(change, d->text_.size());
1219 // when appending characters, no need to update tables
1220 d->text_.push_back(c);
1221 setFont(d->text_.size() - 1, font);
1225 void Paragraph::appendString(docstring const & s, Font const & font,
1226 Change const & change)
1228 pos_type end = s.size();
1229 size_t oldsize = d->text_.size();
1230 size_t newsize = oldsize + end;
1231 size_t capacity = d->text_.capacity();
1232 if (newsize >= capacity)
1233 d->text_.reserve(std::max(capacity + 100, newsize));
1235 // when appending characters, no need to update tables
1238 // FIXME: Optimize this!
1239 for (pos_type i = 0; i != end; ++i) {
1241 d->changes_.insert(change, i);
1243 d->fontlist_.set(oldsize, font);
1244 d->fontlist_.set(newsize - 1, font);
1248 void Paragraph::insertChar(pos_type pos, char_type c,
1251 d->insertChar(pos, c, Change(trackChanges ?
1252 Change::INSERTED : Change::UNCHANGED));
1256 void Paragraph::insertChar(pos_type pos, char_type c,
1257 Font const & font, bool trackChanges)
1259 d->insertChar(pos, c, Change(trackChanges ?
1260 Change::INSERTED : Change::UNCHANGED));
1265 void Paragraph::insertChar(pos_type pos, char_type c,
1266 Font const & font, Change const & change)
1268 d->insertChar(pos, c, change);
1273 void Paragraph::insertInset(pos_type pos, Inset * inset,
1274 Font const & font, Change const & change)
1276 insertInset(pos, inset, change);
1277 // Set the font/language of the inset...
1282 bool Paragraph::insetAllowed(InsetCode code)
1284 return !d->inset_owner_ || d->inset_owner_->insetAllowed(code);
1288 void Paragraph::resetFonts(Font const & font)
1290 d->fontlist_.clear();
1291 d->fontlist_.set(0, font);
1292 d->fontlist_.set(d->text_.size() - 1, font);
1295 // Gets uninstantiated font setting at position.
1296 Font const Paragraph::getFontSettings(BufferParams const & bparams,
1300 lyxerr << " pos: " << pos << " size: " << size() << endl;
1301 BOOST_ASSERT(pos <= size());
1304 FontList::const_iterator cit = d->fontlist_.fontIterator(pos);
1305 if (cit != d->fontlist_.end())
1308 if (pos == size() && !empty())
1309 return getFontSettings(bparams, pos - 1);
1311 return Font(inherit_font, getParLanguage(bparams));
1315 FontSpan Paragraph::fontSpan(pos_type pos) const
1317 BOOST_ASSERT(pos <= size());
1320 FontList::const_iterator cit = d->fontlist_.begin();
1321 FontList::const_iterator end = d->fontlist_.end();
1322 for (; cit != end; ++cit) {
1323 if (cit->pos() >= pos) {
1324 if (pos >= beginOfBody())
1325 return FontSpan(std::max(start, beginOfBody()),
1328 return FontSpan(start,
1329 std::min(beginOfBody() - 1,
1332 start = cit->pos() + 1;
1335 // This should not happen, but if so, we take no chances.
1336 //lyxerr << "Paragraph::getEndPosOfFontSpan: This should not happen!"
1338 return FontSpan(pos, pos);
1342 // Gets uninstantiated font setting at position 0
1343 Font const Paragraph::getFirstFontSettings(BufferParams const & bparams) const
1345 if (!empty() && !d->fontlist_.empty())
1346 return d->fontlist_.begin()->font();
1348 return Font(inherit_font, bparams.language);
1352 // Gets the fully instantiated font at a given position in a paragraph
1353 // This is basically the same function as Text::GetFont() in text2.cpp.
1354 // The difference is that this one is used for generating the LaTeX file,
1355 // and thus cosmetic "improvements" are disallowed: This has to deliver
1356 // the true picture of the buffer. (Asger)
1357 Font const Paragraph::getFont(BufferParams const & bparams, pos_type pos,
1358 Font const & outerfont) const
1360 BOOST_ASSERT(pos >= 0);
1362 Font font = getFontSettings(bparams, pos);
1364 pos_type const body_pos = beginOfBody();
1366 font.fontInfo().realize(d->layout_->labelfont);
1368 font.fontInfo().realize(d->layout_->font);
1370 font.fontInfo().realize(outerfont.fontInfo());
1371 font.fontInfo().realize(bparams.getFont().fontInfo());
1377 Font const Paragraph::getLabelFont
1378 (BufferParams const & bparams, Font const & outerfont) const
1380 FontInfo tmpfont = layout()->labelfont;
1381 tmpfont.realize(outerfont.fontInfo());
1382 tmpfont.realize(bparams.getFont().fontInfo());
1383 return Font(tmpfont, getParLanguage(bparams));
1387 Font const Paragraph::getLayoutFont
1388 (BufferParams const & bparams, Font const & outerfont) const
1390 FontInfo tmpfont = layout()->font;
1391 tmpfont.realize(outerfont.fontInfo());
1392 tmpfont.realize(bparams.getFont().fontInfo());
1393 return Font(tmpfont, getParLanguage(bparams));
1397 /// Returns the height of the highest font in range
1398 FontSize Paragraph::highestFontInRange
1399 (pos_type startpos, pos_type endpos, FontSize def_size) const
1401 return d->fontlist_.highestInRange(startpos, endpos, def_size);
1406 Paragraph::getUChar(BufferParams const & bparams, pos_type pos) const
1408 char_type c = d->text_[pos];
1409 if (!lyxrc.rtl_support)
1439 if (uc != c && getFontSettings(bparams, pos).isRightToLeft())
1446 void Paragraph::setFont(pos_type pos, Font const & font)
1448 BOOST_ASSERT(pos <= size());
1450 // First, reduce font against layout/label font
1451 // Update: The setCharFont() routine in text2.cpp already
1452 // reduces font, so we don't need to do that here. (Asger)
1454 d->fontlist_.set(pos, font);
1458 void Paragraph::makeSameLayout(Paragraph const & par)
1460 layout(par.layout());
1462 d->params_ = par.params();
1466 bool Paragraph::stripLeadingSpaces(bool trackChanges)
1468 if (isFreeSpacing())
1474 while (pos < size() && (isNewline(pos) || isLineSeparator(pos))) {
1475 if (eraseChar(pos, trackChanges))
1481 return count > 0 || pos > 0;
1485 bool Paragraph::hasSameLayout(Paragraph const & par) const
1487 return par.layout() == layout() && d->params_.sameLayout(par.params());
1491 depth_type Paragraph::getDepth() const
1493 return params().depth();
1497 depth_type Paragraph::getMaxDepthAfter() const
1499 if (layout()->isEnvironment())
1500 return params().depth() + 1;
1502 return params().depth();
1506 char Paragraph::getAlign() const
1508 if (params().align() == LYX_ALIGN_LAYOUT)
1509 return layout()->align;
1511 return params().align();
1515 docstring const & Paragraph::getLabelstring() const
1517 return params().labelString();
1521 // the next two functions are for the manual labels
1522 docstring const Paragraph::getLabelWidthString() const
1524 if (layout()->margintype == MARGIN_MANUAL)
1525 return params().labelWidthString();
1527 return _("Senseless with this layout!");
1531 void Paragraph::setLabelWidthString(docstring const & s)
1533 params().labelWidthString(s);
1537 docstring const Paragraph::translateIfPossible(docstring const & s,
1538 BufferParams const & bparams) const
1540 if (!support::isAscii(s) || s.empty()) {
1541 // This must be a user defined layout. We cannot translate
1542 // this, since gettext accepts only ascii keys.
1545 // Probably standard layout, try to translate
1546 Messages & m = getMessages(getParLanguage(bparams)->code());
1547 return m.get(to_ascii(s));
1551 docstring Paragraph::expandLabel(LayoutPtr const & layout,
1552 BufferParams const & bparams, bool process_appendix) const
1554 TextClass const & tclass = bparams.getTextClass();
1557 if (process_appendix && params().appendix())
1558 fmt = translateIfPossible(layout->labelstring_appendix(),
1561 fmt = translateIfPossible(layout->labelstring(), bparams);
1563 if (fmt.empty() && layout->labeltype == LABEL_COUNTER
1564 && !layout->counter.empty())
1565 fmt = "\\the" + layout->counter;
1567 // handle 'inherited level parts' in 'fmt',
1568 // i.e. the stuff between '@' in '@Section@.\arabic{subsection}'
1569 size_t const i = fmt.find('@', 0);
1570 if (i != docstring::npos) {
1571 size_t const j = fmt.find('@', i + 1);
1572 if (j != docstring::npos) {
1573 docstring parent(fmt, i + 1, j - i - 1);
1574 docstring label = from_ascii("??");
1575 if (tclass.hasLayout(parent))
1576 docstring label = expandLabel(tclass[parent], bparams,
1578 fmt = docstring(fmt, 0, i) + label
1579 + docstring(fmt, j + 1, docstring::npos);
1583 return tclass.counters().counterLabel(fmt);
1587 void Paragraph::applyLayout(LayoutPtr const & new_layout)
1590 LyXAlignment const oldAlign = params().align();
1592 if (!(oldAlign & layout()->alignpossible)) {
1593 frontend::Alert::warning(_("Alignment not permitted"),
1594 _("The new layout does not permit the alignment previously used.\nSetting to default."));
1595 params().align(LYX_ALIGN_LAYOUT);
1600 pos_type Paragraph::beginOfBody() const
1602 return d->begin_of_body_;
1606 void Paragraph::setBeginOfBody()
1608 if (layout()->labeltype != LABEL_MANUAL) {
1609 d->begin_of_body_ = 0;
1613 // Unroll the first two cycles of the loop
1614 // and remember the previous character to
1615 // remove unnecessary getChar() calls
1617 pos_type end = size();
1618 if (i < end && !isNewline(i)) {
1620 char_type previous_char = 0;
1623 previous_char = d->text_[i];
1624 if (!isNewline(i)) {
1626 while (i < end && previous_char != ' ') {
1631 previous_char = temp;
1637 d->begin_of_body_ = i;
1641 bool Paragraph::forceDefaultParagraphs() const
1643 return inInset() && inInset()->forceDefaultParagraphs(0);
1649 // paragraphs inside floats need different alignment tags to avoid
1652 bool noTrivlistCentering(InsetCode code)
1654 return code == FLOAT_CODE || code == WRAP_CODE;
1658 string correction(string const & orig)
1660 if (orig == "flushleft")
1661 return "raggedright";
1662 if (orig == "flushright")
1663 return "raggedleft";
1664 if (orig == "center")
1670 string const corrected_env(string const & suffix, string const & env,
1673 string output = suffix + "{";
1674 if (noTrivlistCentering(code))
1675 output += correction(env);
1679 if (suffix == "\\begin")
1685 void adjust_row_column(string const & str, TexRow & texrow, int & column)
1687 if (!contains(str, "\n"))
1688 column += str.size();
1692 column = rsplit(str, tmp, '\n').size();
1699 int Paragraph::Private::startTeXParParams(BufferParams const & bparams,
1700 odocstream & os, TexRow & texrow,
1701 bool moving_arg) const
1705 if (params_.noindent()) {
1706 os << "\\noindent ";
1710 LyXAlignment const curAlign = params_.align();
1712 if (curAlign == layout_->align)
1716 case LYX_ALIGN_NONE:
1717 case LYX_ALIGN_BLOCK:
1718 case LYX_ALIGN_LAYOUT:
1719 case LYX_ALIGN_SPECIAL:
1721 case LYX_ALIGN_LEFT:
1722 case LYX_ALIGN_RIGHT:
1723 case LYX_ALIGN_CENTER:
1732 case LYX_ALIGN_NONE:
1733 case LYX_ALIGN_BLOCK:
1734 case LYX_ALIGN_LAYOUT:
1735 case LYX_ALIGN_SPECIAL:
1737 case LYX_ALIGN_LEFT: {
1739 if (owner_->getParLanguage(bparams)->babel() != "hebrew")
1740 output = corrected_env("\\begin", "flushleft", owner_->ownerCode());
1742 output = corrected_env("\\begin", "flushright", owner_->ownerCode());
1743 os << from_ascii(output);
1744 adjust_row_column(output, texrow, column);
1746 } case LYX_ALIGN_RIGHT: {
1748 if (owner_->getParLanguage(bparams)->babel() != "hebrew")
1749 output = corrected_env("\\begin", "flushright", owner_->ownerCode());
1751 output = corrected_env("\\begin", "flushleft", owner_->ownerCode());
1752 os << from_ascii(output);
1753 adjust_row_column(output, texrow, column);
1755 } case LYX_ALIGN_CENTER: {
1757 output = corrected_env("\\begin", "center", owner_->ownerCode());
1758 os << from_ascii(output);
1759 adjust_row_column(output, texrow, column);
1768 int Paragraph::Private::endTeXParParams(BufferParams const & bparams,
1769 odocstream & os, TexRow & texrow,
1770 bool moving_arg) const
1774 switch (params_.align()) {
1775 case LYX_ALIGN_NONE:
1776 case LYX_ALIGN_BLOCK:
1777 case LYX_ALIGN_LAYOUT:
1778 case LYX_ALIGN_SPECIAL:
1780 case LYX_ALIGN_LEFT:
1781 case LYX_ALIGN_RIGHT:
1782 case LYX_ALIGN_CENTER:
1790 switch (params_.align()) {
1791 case LYX_ALIGN_NONE:
1792 case LYX_ALIGN_BLOCK:
1793 case LYX_ALIGN_LAYOUT:
1794 case LYX_ALIGN_SPECIAL:
1796 case LYX_ALIGN_LEFT: {
1798 if (owner_->getParLanguage(bparams)->babel() != "hebrew")
1799 output = corrected_env("\n\\par\\end", "flushleft", owner_->ownerCode());
1801 output = corrected_env("\n\\par\\end", "flushright", owner_->ownerCode());
1802 os << from_ascii(output);
1803 adjust_row_column(output, texrow, column);
1805 } case LYX_ALIGN_RIGHT: {
1807 if (owner_->getParLanguage(bparams)->babel() != "hebrew")
1808 output = corrected_env("\n\\par\\end", "flushright", owner_->ownerCode());
1810 output = corrected_env("\n\\par\\end", "flushleft", owner_->ownerCode());
1811 os << from_ascii(output);
1812 adjust_row_column(output, texrow, column);
1814 } case LYX_ALIGN_CENTER: {
1816 output = corrected_env("\n\\par\\end", "center", owner_->ownerCode());
1817 os << from_ascii(output);
1818 adjust_row_column(output, texrow, column);
1827 // This one spits out the text of the paragraph
1828 bool Paragraph::latex(Buffer const & buf,
1829 BufferParams const & bparams,
1830 Font const & outerfont,
1831 odocstream & os, TexRow & texrow,
1832 OutputParams const & runparams) const
1834 LYXERR(Debug::LATEX, "SimpleTeXOnePar... " << this);
1836 bool return_value = false;
1840 // well we have to check if we are in an inset with unlimited
1841 // length (all in one row) if that is true then we don't allow
1842 // any special options in the paragraph and also we don't allow
1843 // any environment other than the default layout of the text class
1845 bool asdefault = forceDefaultParagraphs();
1848 style = bparams.getTextClass().defaultLayout();
1853 // Current base font for all inherited font changes, without any
1854 // change caused by an individual character, except for the language:
1855 // It is set to the language of the first character.
1856 // As long as we are in the label, this font is the base font of the
1857 // label. Before the first body character it is set to the base font
1861 // Maybe we have to create a optional argument.
1862 pos_type body_pos = beginOfBody();
1863 unsigned int column = 0;
1866 // the optional argument is kept in curly brackets in
1867 // case it contains a ']'
1870 basefont = getLabelFont(bparams, outerfont);
1872 basefont = getLayoutFont(bparams, outerfont);
1875 // Which font is currently active?
1876 Font running_font(basefont);
1877 // Do we have an open font change?
1878 bool open_font = false;
1880 Change runningChange = Change(Change::UNCHANGED);
1882 texrow.start(id(), 0);
1884 // if the paragraph is empty, the loop will not be entered at all
1886 if (style->isCommand()) {
1891 column += d->startTeXParParams(bparams, os, texrow,
1892 runparams.moving_arg);
1895 for (pos_type i = 0; i < size(); ++i) {
1896 // First char in paragraph or after label?
1897 if (i == body_pos) {
1900 column += running_font.latexWriteEndChanges(
1901 os, bparams, runparams,
1902 basefont, basefont);
1905 basefont = getLayoutFont(bparams, outerfont);
1906 running_font = basefont;
1908 column += Changes::latexMarkChange(os, bparams,
1909 runningChange, Change(Change::UNCHANGED));
1910 runningChange = Change(Change::UNCHANGED);
1915 if (style->isCommand()) {
1921 column += d->startTeXParParams(bparams, os,
1923 runparams.moving_arg);
1926 Change const & change = runparams.inDeletedInset ? runparams.changeOfDeletedInset
1929 if (bparams.outputChanges && runningChange != change) {
1931 column += running_font.latexWriteEndChanges(
1932 os, bparams, runparams, basefont, basefont);
1935 basefont = getLayoutFont(bparams, outerfont);
1936 running_font = basefont;
1938 column += Changes::latexMarkChange(os, bparams, runningChange, change);
1939 runningChange = change;
1942 // do not output text which is marked deleted
1943 // if change tracking output is disabled
1944 if (!bparams.outputChanges && change.type == Change::DELETED) {
1950 // Fully instantiated font
1951 Font const font = getFont(bparams, i, outerfont);
1953 Font const last_font = running_font;
1955 // Do we need to close the previous font?
1957 (font != running_font ||
1958 font.language() != running_font.language()))
1960 column += running_font.latexWriteEndChanges(
1961 os, bparams, runparams, basefont,
1962 (i == body_pos-1) ? basefont : font);
1963 running_font = basefont;
1967 // Switch file encoding if necessary (and allowed)
1968 if (!runparams.verbatim &&
1969 runparams.encoding->package() == Encoding::inputenc &&
1970 font.language()->encoding()->package() == Encoding::inputenc) {
1971 std::pair<bool, int> const enc_switch = switchEncoding(os, bparams,
1972 runparams.moving_arg, *(runparams.encoding),
1973 *(font.language()->encoding()));
1974 if (enc_switch.first) {
1975 column += enc_switch.second;
1976 runparams.encoding = font.language()->encoding();
1980 char_type const c = d->text_[i];
1982 // Do we need to change font?
1983 if ((font != running_font ||
1984 font.language() != running_font.language()) &&
1987 odocstringstream ods;
1988 column += font.latexWriteStartChanges(ods, bparams,
1989 runparams, basefont,
1991 running_font = font;
1993 docstring fontchange = ods.str();
1994 // check if the fontchange ends with a trailing blank
1995 // (like "\small " (see bug 3382)
1996 if (suffixIs(fontchange, ' ') && c == ' ')
1997 os << fontchange.substr(0, fontchange.size() - 1)
1998 << from_ascii("{}");
2004 // FIXME: integrate this case in latexSpecialChar
2005 // Do not print the separation of the optional argument
2006 // if style->pass_thru is false. This works because
2007 // latexSpecialChar ignores spaces if
2008 // style->pass_thru is false.
2009 if (i != body_pos - 1) {
2010 if (d->simpleTeXBlanks(
2011 runparams, os, texrow,
2012 i, column, font, *style)) {
2013 // A surrogate pair was output. We
2014 // must not call latexSpecialChar
2015 // in this iteration, since it would output
2016 // the combining character again.
2023 OutputParams rp = runparams;
2024 rp.free_spacing = style->free_spacing;
2025 rp.local_font = &font;
2026 rp.intitle = style->intitle;
2028 // Two major modes: LaTeX or plain
2029 // Handle here those cases common to both modes
2030 // and then split to handle the two modes separately.
2031 if (c == META_INSET)
2032 d->latexInset(buf, bparams, os,
2033 texrow, rp, running_font,
2034 basefont, outerfont, open_font,
2035 runningChange, *style, i, column);
2037 d->latexSpecialChar(os, rp, running_font, runningChange,
2040 // Set the encoding to that returned from simpleTeXSpecialChars (see
2041 // comment for encoding member in OutputParams.h)
2042 runparams.encoding = rp.encoding;
2045 // If we have an open font definition, we have to close it
2047 #ifdef FIXED_LANGUAGE_END_DETECTION
2050 .latexWriteEndChanges(os, bparams, runparams,
2052 next_->getFont(bparams, 0, outerfont));
2054 running_font.latexWriteEndChanges(os, bparams,
2055 runparams, basefont, basefont);
2058 //FIXME: For now we ALWAYS have to close the foreign font settings if they are
2059 //FIXME: there as we start another \selectlanguage with the next paragraph if
2060 //FIXME: we are in need of this. This should be fixed sometime (Jug)
2061 running_font.latexWriteEndChanges(os, bparams, runparams,
2062 basefont, basefont);
2066 column += Changes::latexMarkChange(os, bparams, runningChange, Change(Change::UNCHANGED));
2068 // Needed if there is an optional argument but no contents.
2069 if (body_pos > 0 && body_pos == size()) {
2071 return_value = false;
2075 column += d->endTeXParParams(bparams, os, texrow,
2076 runparams.moving_arg);
2079 LYXERR(Debug::LATEX, "SimpleTeXOnePar...done " << this);
2080 return return_value;
2084 bool Paragraph::emptyTag() const
2086 for (pos_type i = 0; i < size(); ++i) {
2088 Inset const * inset = getInset(i);
2089 InsetCode lyx_code = inset->lyxCode();
2090 if (lyx_code != TOC_CODE &&
2091 lyx_code != INCLUDE_CODE &&
2092 lyx_code != GRAPHICS_CODE &&
2093 lyx_code != ERT_CODE &&
2094 lyx_code != LISTINGS_CODE &&
2095 lyx_code != FLOAT_CODE &&
2096 lyx_code != TABULAR_CODE) {
2100 char_type c = d->text_[i];
2101 if (c != ' ' && c != '\t')
2109 string Paragraph::getID(Buffer const & buf, OutputParams const & runparams) const
2111 for (pos_type i = 0; i < size(); ++i) {
2113 Inset const * inset = getInset(i);
2114 InsetCode lyx_code = inset->lyxCode();
2115 if (lyx_code == LABEL_CODE) {
2116 InsetLabel const * const il = static_cast<InsetLabel const *>(inset);
2117 docstring const & id = il->getParam("name");
2118 return "id='" + to_utf8(sgml::cleanID(buf, runparams, id)) + "'";
2127 pos_type Paragraph::getFirstWord(Buffer const & buf, odocstream & os, OutputParams const & runparams) const
2130 for (i = 0; i < size(); ++i) {
2132 Inset const * inset = getInset(i);
2133 inset->docbook(buf, os, runparams);
2135 char_type c = d->text_[i];
2138 os << sgml::escapeChar(c);
2145 bool Paragraph::Private::onlyText(Buffer const & buf, Font const & outerfont, pos_type initial) const
2148 pos_type size = text_.size();
2149 for (pos_type i = initial; i < size; ++i) {
2150 Font font = owner_->getFont(buf.params(), i, outerfont);
2151 if (text_[i] == META_INSET)
2153 if (i != initial && font != font_old)
2162 void Paragraph::simpleDocBookOnePar(Buffer const & buf,
2164 OutputParams const & runparams,
2165 Font const & outerfont,
2166 pos_type initial) const
2168 bool emph_flag = false;
2170 LayoutPtr const & style = layout();
2172 style->labeltype == LABEL_MANUAL ? style->labelfont : style->font;
2174 if (style->pass_thru && !d->onlyText(buf, outerfont, initial))
2177 // parsing main loop
2178 for (pos_type i = initial; i < size(); ++i) {
2179 Font font = getFont(buf.params(), i, outerfont);
2181 // handle <emphasis> tag
2182 if (font_old.emph() != font.fontInfo().emph()) {
2183 if (font.fontInfo().emph() == FONT_ON) {
2186 } else if (i != initial) {
2187 os << "</emphasis>";
2193 Inset const * inset = getInset(i);
2194 inset->docbook(buf, os, runparams);
2196 char_type c = d->text_[i];
2198 if (style->pass_thru)
2201 os << sgml::escapeChar(c);
2203 font_old = font.fontInfo();
2207 os << "</emphasis>";
2210 if (style->free_spacing)
2212 if (style->pass_thru && !d->onlyText(buf, outerfont, initial))
2217 bool Paragraph::isHfill(pos_type pos) const
2220 && getInset(pos)->lyxCode() == HFILL_CODE;
2224 bool Paragraph::isNewline(pos_type pos) const
2227 && getInset(pos)->lyxCode() == NEWLINE_CODE;
2231 bool Paragraph::isLineSeparator(pos_type pos) const
2233 char_type const c = d->text_[pos];
2234 return isLineSeparatorChar(c)
2235 || (c == META_INSET && getInset(pos) &&
2236 getInset(pos)->isLineSeparator());
2240 /// Used by the spellchecker
2241 bool Paragraph::isLetter(pos_type pos) const
2244 return getInset(pos)->isLetter();
2246 char_type const c = d->text_[pos];
2247 return isLetterChar(c) || isDigit(c);
2253 Paragraph::getParLanguage(BufferParams const & bparams) const
2256 return getFirstFontSettings(bparams).language();
2257 // FIXME: we should check the prev par as well (Lgb)
2258 return bparams.language;
2262 bool Paragraph::isRTL(BufferParams const & bparams) const
2264 return lyxrc.rtl_support
2265 && getParLanguage(bparams)->rightToLeft()
2266 && ownerCode() != ERT_CODE
2267 && ownerCode() != LISTINGS_CODE;
2271 void Paragraph::changeLanguage(BufferParams const & bparams,
2272 Language const * from, Language const * to)
2274 // change language including dummy font change at the end
2275 for (pos_type i = 0; i <= size(); ++i) {
2276 Font font = getFontSettings(bparams, i);
2277 if (font.language() == from) {
2278 font.setLanguage(to);
2285 bool Paragraph::isMultiLingual(BufferParams const & bparams) const
2287 Language const * doc_language = bparams.language;
2288 FontList::const_iterator cit = d->fontlist_.begin();
2289 FontList::const_iterator end = d->fontlist_.end();
2291 for (; cit != end; ++cit)
2292 if (cit->font().language() != ignore_language &&
2293 cit->font().language() != latex_language &&
2294 cit->font().language() != doc_language)
2300 // Convert the paragraph to a string.
2301 // Used for building the table of contents
2302 docstring const Paragraph::asString(Buffer const & buffer, bool label) const
2304 return asString(buffer, 0, size(), label);
2308 docstring const Paragraph::asString(Buffer const & buffer,
2309 pos_type beg, pos_type end, bool label) const
2312 odocstringstream os;
2314 if (beg == 0 && label && !params().labelString().empty())
2315 os << params().labelString() << ' ';
2317 for (pos_type i = beg; i < end; ++i) {
2318 char_type const c = d->text_[i];
2321 else if (c == META_INSET)
2322 getInset(i)->textString(buffer, os);
2329 void Paragraph::setInsetOwner(Inset * inset)
2331 d->inset_owner_ = inset;
2335 int Paragraph::id() const
2341 LayoutPtr const & Paragraph::layout() const
2347 void Paragraph::layout(LayoutPtr const & new_layout)
2349 d->layout_ = new_layout;
2353 Inset * Paragraph::inInset() const
2355 return d->inset_owner_;
2359 InsetCode Paragraph::ownerCode() const
2361 return d->inset_owner_ ?
2362 d->inset_owner_->lyxCode() : NO_CODE;
2366 ParagraphParameters & Paragraph::params()
2372 ParagraphParameters const & Paragraph::params() const
2378 bool Paragraph::isFreeSpacing() const
2380 if (layout()->free_spacing)
2382 return d->inset_owner_ && d->inset_owner_->isFreeSpacing();
2386 bool Paragraph::allowEmpty() const
2388 if (layout()->keepempty)
2390 return d->inset_owner_ && d->inset_owner_->allowEmpty();
2394 char_type Paragraph::transformChar(char_type c, pos_type pos) const
2396 if (!Encodings::is_arabic(c))
2399 char_type prev_char = ' ';
2400 char_type next_char = ' ';
2402 for (pos_type i = pos - 1; i >= 0; --i) {
2403 char_type const par_char = d->text_[i];
2404 if (!Encodings::isComposeChar_arabic(par_char)) {
2405 prev_char = par_char;
2410 for (pos_type i = pos + 1, end = size(); i < end; ++i) {
2411 char_type const par_char = d->text_[i];
2412 if (!Encodings::isComposeChar_arabic(par_char)) {
2413 next_char = par_char;
2418 if (Encodings::is_arabic(next_char)) {
2419 if (Encodings::is_arabic(prev_char) &&
2420 !Encodings::is_arabic_special(prev_char))
2421 return Encodings::transformChar(c, Encodings::FORM_MEDIAL);
2423 return Encodings::transformChar(c, Encodings::FORM_INITIAL);
2425 if (Encodings::is_arabic(prev_char) &&
2426 !Encodings::is_arabic_special(prev_char))
2427 return Encodings::transformChar(c, Encodings::FORM_FINAL);
2429 return Encodings::transformChar(c, Encodings::FORM_ISOLATED);
2434 int Paragraph::checkBiblio(bool track_changes)
2437 //This is getting more and more a mess. ...We really should clean
2438 //up this bibitem issue for 1.6. See also bug 2743.
2440 // Add bibitem insets if necessary
2441 if (layout()->labeltype != LABEL_BIBLIO)
2444 bool hasbibitem = !d->insetlist_.empty()
2445 // Insist on it being in pos 0
2446 && d->text_[0] == META_INSET
2447 && d->insetlist_.begin()->inset->lyxCode() == BIBITEM_CODE;
2452 // remove a bibitem in pos != 0
2453 // restore it later in pos 0 if necessary
2454 // (e.g. if a user inserts contents _before_ the item)
2455 // we're assuming there's only one of these, which there
2457 int erasedInsetPosition = -1;
2458 InsetList::iterator it = d->insetlist_.begin();
2459 InsetList::iterator end = d->insetlist_.end();
2460 for (; it != end; ++it)
2461 if (it->inset->lyxCode() == BIBITEM_CODE
2463 InsetBibitem * olditem = static_cast<InsetBibitem *>(it->inset);
2464 oldkey = olditem->getParam("key");
2465 oldlabel = olditem->getParam("label");
2466 erasedInsetPosition = it->pos;
2467 eraseChar(erasedInsetPosition, track_changes);
2471 //There was an InsetBibitem at the beginning, and we didn't
2472 //have to erase one.
2473 if (hasbibitem && erasedInsetPosition < 0)
2476 //There was an InsetBibitem at the beginning and we did have to
2477 //erase one. So we give its properties to the beginning inset.
2479 InsetBibitem * inset =
2480 static_cast<InsetBibitem *>(d->insetlist_.begin()->inset);
2481 if (!oldkey.empty())
2482 inset->setParam("key", oldkey);
2483 inset->setParam("label", oldlabel);
2484 return -erasedInsetPosition;
2487 //There was no inset at the beginning, so we need to create one with
2488 //the key and label of the one we erased.
2489 InsetBibitem * inset(new InsetBibitem(InsetCommandParams(BIBITEM_CODE)));
2490 // restore values of previously deleted item in this par.
2491 if (!oldkey.empty())
2492 inset->setParam("key", oldkey);
2493 inset->setParam("label", oldlabel);
2494 insertInset(0, static_cast<Inset *>(inset),
2495 Change(track_changes ? Change::INSERTED : Change::UNCHANGED));
2501 unsigned int Paragraph::macrocontextPosition() const
2503 return d->macrocontext_position_;
2507 void Paragraph::setMacrocontextPosition(unsigned int pos)
2509 d->macrocontext_position_ = pos;
2513 void Paragraph::checkAuthors(AuthorList const & authorList)
2515 d->changes_.checkAuthors(authorList);
2519 bool Paragraph::isUnchanged(pos_type pos) const
2521 return lookupChange(pos).type == Change::UNCHANGED;
2525 bool Paragraph::isInserted(pos_type pos) const
2527 return lookupChange(pos).type == Change::INSERTED;
2531 bool Paragraph::isDeleted(pos_type pos) const
2533 return lookupChange(pos).type == Change::DELETED;
2537 InsetList const & Paragraph::insetList() const
2539 return d->insetlist_;
2543 Inset * Paragraph::releaseInset(pos_type pos)
2545 Inset * inset = d->insetlist_.release(pos);
2546 /// does not honour change tracking!
2547 eraseChar(pos, false);
2552 Inset * Paragraph::getInset(pos_type pos)
2554 return d->insetlist_.get(pos);
2558 Inset const * Paragraph::getInset(pos_type pos) const
2560 return d->insetlist_.get(pos);
2564 void Paragraph::changeCase(BufferParams const & bparams, pos_type pos,
2565 pos_type right, TextCase action)
2567 // process sequences of modified characters; in change
2568 // tracking mode, this approach results in much better
2569 // usability than changing case on a char-by-char basis
2572 bool const trackChanges = bparams.trackChanges;
2574 bool capitalize = true;
2576 for (; pos < right; ++pos) {
2577 char_type oldChar = d->text_[pos];
2578 char_type newChar = oldChar;
2580 // ignore insets and don't play with deleted text!
2581 if (oldChar != META_INSET && !isDeleted(pos)) {
2583 case text_lowercase:
2584 newChar = lowercase(oldChar);
2586 case text_capitalization:
2588 newChar = uppercase(oldChar);
2592 case text_uppercase:
2593 newChar = uppercase(oldChar);
2598 if (!isLetter(pos) || isDeleted(pos)) {
2599 // permit capitalization again
2603 if (oldChar != newChar)
2606 if (oldChar == newChar || pos == right - 1) {
2607 if (oldChar != newChar) {
2608 // step behind the changing area
2611 int erasePos = pos - changes.size();
2612 for (size_t i = 0; i < changes.size(); i++) {
2613 insertChar(pos, changes[i],
2614 getFontSettings(bparams,
2617 if (!eraseChar(erasePos, trackChanges)) {
2620 ++right; // expand selection
2629 bool Paragraph::find(docstring const & str, bool cs, bool mw,
2630 pos_type pos, bool del) const
2632 int const strsize = str.length();
2634 pos_type const parsize = d->text_.size();
2635 for (i = 0; pos + i < parsize; ++i) {
2638 if (cs && str[i] != d->text_[pos + i])
2640 if (!cs && uppercase(str[i]) != uppercase(d->text_[pos + i]))
2642 if (!del && isDeleted(pos + i))
2649 // if necessary, check whether string matches word
2651 if (pos > 0 && isLetter(pos - 1))
2653 if (pos + strsize < parsize
2654 && isLetter(pos + strsize))
2662 char_type Paragraph::getChar(pos_type pos) const
2664 return d->text_[pos];
2668 pos_type Paragraph::size() const
2670 return d->text_.size();
2674 bool Paragraph::empty() const
2676 return d->text_.empty();
2680 bool Paragraph::isInset(pos_type pos) const
2682 return d->text_[pos] == META_INSET;
2686 bool Paragraph::isSeparator(pos_type pos) const
2688 //FIXME: Are we sure this can be the only separator?
2689 return d->text_[pos] == ' ';