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"
39 #include "OutputParams.h"
40 #include "output_latex.h"
41 #include "paragraph_funcs.h"
42 #include "ParagraphParameters.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;
74 using support::uppercase;
77 /// Inset identifier (above 0x10ffff, for ucs-4)
78 char_type const META_INSET = 0x200001;
81 /////////////////////////////////////////////////////////////////////
85 /////////////////////////////////////////////////////////////////////
87 class Paragraph::Private
91 Private(Paragraph * owner);
92 /// "Copy constructor"
93 Private(Private const &, Paragraph * owner);
96 void insertChar(pos_type pos, char_type c, Change const & change);
98 /// Output the surrogate pair formed by \p c and \p next to \p os.
99 /// \return the number of characters written.
100 int latexSurrogatePair(odocstream & os, char_type c, char_type next,
103 /// Output a space in appropriate formatting (or a surrogate pair
104 /// if the next character is a combining character).
105 /// \return whether a surrogate pair was output.
106 bool simpleTeXBlanks(Encoding const &,
107 odocstream &, TexRow & texrow,
109 unsigned int & column,
111 Layout const & style);
113 /// Output consecutive known unicode chars, belonging to the same
114 /// language as specified by \p preamble, to \p os starting from \p c.
115 /// \return the number of characters written.
116 int knownLangChars(odocstream & os, char_type c, string & preamble,
117 Change &, Encoding const &, pos_type &);
119 /// This could go to ParagraphParameters if we want to.
120 int startTeXParParams(BufferParams const &, odocstream &, TexRow &,
123 /// This could go to ParagraphParameters if we want to.
124 int endTeXParParams(BufferParams const &, odocstream &, TexRow &,
128 void latexInset(Buffer const &, BufferParams const &,
130 TexRow & texrow, OutputParams &,
133 Font const & outerfont,
135 Change & running_change,
136 Layout const & style,
138 unsigned int & column);
141 void latexSpecialChar(
143 OutputParams & runparams,
145 Change & running_change,
146 Layout const & style,
148 unsigned int & column);
155 unsigned int & column);
157 bool latexSpecialTypewriter(
161 unsigned int & column);
163 bool latexSpecialPhrase(
166 unsigned int & column);
169 void validate(LaTeXFeatures & features,
170 Layout const & layout) const;
173 pos_type size() const { return owner_->size(); }
175 /// match a string against a particular point in the paragraph
176 bool isTextAt(std::string const & str, pos_type pos) const;
178 /// Which Paragraph owns us?
182 Inset * inset_owner_;
190 static unsigned int paragraph_id;
192 ParagraphParameters params_;
194 /// for recording and looking up changes
198 InsetList insetlist_;
204 pos_type begin_of_body_;
206 typedef std::vector<char_type> TextContainer;
215 using std::upper_bound;
216 using std::lower_bound;
220 // Initialization of the counter for the paragraph id's,
221 unsigned int Paragraph::Private::paragraph_id = 0;
225 struct special_phrase {
231 special_phrase const special_phrases[] = {
232 { "LyX", from_ascii("\\protect\\LyX{}"), false },
233 { "TeX", from_ascii("\\protect\\TeX{}"), true },
234 { "LaTeX2e", from_ascii("\\protect\\LaTeXe{}"), true },
235 { "LaTeX", from_ascii("\\protect\\LaTeX{}"), true },
238 size_t const phrases_nr = sizeof(special_phrases)/sizeof(special_phrase);
243 Paragraph::Private::Private(Paragraph * owner)
244 : owner_(owner), inset_owner_(0), begin_of_body_(0)
246 id_ = paragraph_id++;
251 Paragraph::Private::Private(Private const & p, Paragraph * owner)
252 : owner_(owner), inset_owner_(p.inset_owner_), fontlist_(p.fontlist_),
253 params_(p.params_), changes_(p.changes_), insetlist_(p.insetlist_),
254 layout_(p.layout_), begin_of_body_(p.begin_of_body_), text_(p.text_)
256 id_ = paragraph_id++;
260 bool Paragraph::isChanged(pos_type start, pos_type end) const
262 BOOST_ASSERT(start >= 0 && start <= size());
263 BOOST_ASSERT(end > start && end <= size() + 1);
265 return d->changes_.isChanged(start, end);
269 bool Paragraph::isMergedOnEndOfParDeletion(bool trackChanges) const {
270 // keep the logic here in sync with the logic of eraseChars()
276 Change change = d->changes_.lookup(size());
278 return change.type == Change::INSERTED && change.author == 0;
282 void Paragraph::setChange(Change const & change)
284 // beware of the imaginary end-of-par character!
285 d->changes_.set(change, 0, size() + 1);
288 * Propagate the change recursively - but not in case of DELETED!
290 * Imagine that your co-author makes changes in an existing inset. He
291 * sends your document to you and you come to the conclusion that the
292 * inset should go completely. If you erase it, LyX must not delete all
293 * text within the inset. Otherwise, the change tracked insertions of
294 * your co-author get lost and there is no way to restore them later.
296 * Conclusion: An inset's content should remain untouched if you delete it
299 if (change.type != Change::DELETED) {
300 for (pos_type pos = 0; pos < size(); ++pos) {
302 getInset(pos)->setChange(change);
308 void Paragraph::setChange(pos_type pos, Change const & change)
310 BOOST_ASSERT(pos >= 0 && pos <= size());
312 d->changes_.set(change, pos);
314 // see comment in setChange(Change const &) above
316 if (change.type != Change::DELETED &&
317 pos < size() && isInset(pos)) {
318 getInset(pos)->setChange(change);
323 Change const & Paragraph::lookupChange(pos_type pos) const
325 BOOST_ASSERT(pos >= 0 && pos <= size());
327 return d->changes_.lookup(pos);
331 void Paragraph::acceptChanges(BufferParams const & bparams, pos_type start,
334 BOOST_ASSERT(start >= 0 && start <= size());
335 BOOST_ASSERT(end > start && end <= size() + 1);
337 for (pos_type pos = start; pos < end; ++pos) {
338 switch (lookupChange(pos).type) {
339 case Change::UNCHANGED:
340 // accept changes in nested inset
341 if (pos < size() && isInset(pos))
342 getInset(pos)->acceptChanges(bparams);
346 case Change::INSERTED:
347 d->changes_.set(Change(Change::UNCHANGED), pos);
348 // also accept changes in nested inset
349 if (pos < size() && isInset(pos)) {
350 getInset(pos)->acceptChanges(bparams);
354 case Change::DELETED:
355 // Suppress access to non-existent
356 // "end-of-paragraph char"
358 eraseChar(pos, false);
369 void Paragraph::rejectChanges(BufferParams const & bparams,
370 pos_type start, pos_type end)
372 BOOST_ASSERT(start >= 0 && start <= size());
373 BOOST_ASSERT(end > start && end <= size() + 1);
375 for (pos_type pos = start; pos < end; ++pos) {
376 switch (lookupChange(pos).type) {
377 case Change::UNCHANGED:
378 // reject changes in nested inset
379 if (pos < size() && isInset(pos)) {
380 getInset(pos)->rejectChanges(bparams);
384 case Change::INSERTED:
385 // Suppress access to non-existent
386 // "end-of-paragraph char"
388 eraseChar(pos, false);
394 case Change::DELETED:
395 d->changes_.set(Change(Change::UNCHANGED), pos);
397 // Do NOT reject changes within a deleted inset!
398 // There may be insertions of a co-author inside of it!
406 void Paragraph::Private::insertChar(pos_type pos, char_type c,
407 Change const & change)
409 BOOST_ASSERT(pos >= 0 && pos <= size());
412 changes_.insert(change, pos);
414 // This is actually very common when parsing buffers (and
415 // maybe inserting ascii text)
417 // when appending characters, no need to update tables
422 text_.insert(text_.begin() + pos, c);
424 // Update the font table.
425 fontlist_.increasePosAfterPos(pos);
428 insetlist_.increasePosAfterPos(pos);
432 void Paragraph::insertInset(pos_type pos, Inset * inset,
433 Change const & change)
436 BOOST_ASSERT(pos >= 0 && pos <= size());
438 d->insertChar(pos, META_INSET, change);
439 BOOST_ASSERT(d->text_[pos] == META_INSET);
441 // Add a new entry in the insetlist_.
442 d->insetlist_.insert(inset, pos);
446 bool Paragraph::eraseChar(pos_type pos, bool trackChanges)
448 BOOST_ASSERT(pos >= 0 && pos <= size());
450 // keep the logic here in sync with the logic of isMergedOnEndOfParDeletion()
453 Change change = d->changes_.lookup(pos);
455 // set the character to DELETED if
456 // a) it was previously unchanged or
457 // b) it was inserted by a co-author
459 if (change.type == Change::UNCHANGED ||
460 (change.type == Change::INSERTED && change.author != 0)) {
461 setChange(pos, Change(Change::DELETED));
465 if (change.type == Change::DELETED)
469 // Don't physically access the imaginary end-of-paragraph character.
470 // eraseChar() can only mark it as DELETED. A physical deletion of
471 // end-of-par must be handled externally.
477 d->changes_.erase(pos);
479 // if it is an inset, delete the inset entry
480 if (d->text_[pos] == META_INSET)
481 d->insetlist_.erase(pos);
483 d->text_.erase(d->text_.begin() + pos);
485 // Update the fontlist_
486 d->fontlist_.erase(pos);
488 // Update the insetlist_
489 d->insetlist_.decreasePosAfterPos(pos);
495 int Paragraph::eraseChars(pos_type start, pos_type end, bool trackChanges)
497 BOOST_ASSERT(start >= 0 && start <= size());
498 BOOST_ASSERT(end >= start && end <= size() + 1);
501 for (pos_type count = end - start; count; --count) {
502 if (!eraseChar(i, trackChanges))
509 int Paragraph::Private::latexSurrogatePair(odocstream & os, char_type c,
510 char_type next, Encoding const & encoding)
512 // Writing next here may circumvent a possible font change between
513 // c and next. Since next is only output if it forms a surrogate pair
514 // with c we can ignore this:
515 // A font change inside a surrogate pair does not make sense and is
516 // hopefully impossible to input.
517 // FIXME: change tracking
518 // Is this correct WRT change tracking?
519 docstring const latex1 = encoding.latexChar(next);
520 docstring const latex2 = encoding.latexChar(c);
521 os << latex1 << '{' << latex2 << '}';
522 return latex1.length() + latex2.length() + 2;
526 bool Paragraph::Private::simpleTeXBlanks(Encoding const & encoding,
527 odocstream & os, TexRow & texrow,
529 unsigned int & column,
531 Layout const & style)
536 if (i + 1 < size()) {
537 char_type next = text_[i + 1];
538 if (Encodings::isCombiningChar(next)) {
539 // This space has an accent, so we must always output it.
540 column += latexSurrogatePair(os, ' ', next, encoding) - 1;
545 if (lyxrc.plaintext_linelen > 0
546 && column > lyxrc.plaintext_linelen
548 && text_[i - 1] != ' '
550 // same in FreeSpacing mode
551 && !owner_->isFreeSpacing()
552 // In typewriter mode, we want to avoid
553 // ! . ? : at the end of a line
554 && !(font.family() == Font::TYPEWRITER_FAMILY
555 && (text_[i - 1] == '.'
556 || text_[i - 1] == '?'
557 || text_[i - 1] == ':'
558 || text_[i - 1] == '!'))) {
561 texrow.start(owner_->id(), i + 1);
563 } else if (style.free_spacing) {
572 int Paragraph::Private::knownLangChars(odocstream & os,
575 Change & runningChange,
576 Encoding const & encoding,
579 // When the character is marked by the proper language, we simply
580 // get its code point in some encoding, otherwise we get the
581 // translation specified in the unicodesymbols file, which is
582 // something like "\textLANG{<spec>}". So, we have to retain
583 // "\textLANG{<spec>" for the first char but only "<spec>" for
584 // all subsequent chars.
585 docstring const latex1 = rtrim(encoding.latexChar(c), "}");
586 int length = latex1.length();
588 while (i + 1 < size()) {
589 char_type next = text_[i + 1];
590 // Stop here if next character belongs to another
591 // language or there is a change tracking status.
592 if (!Encodings::isKnownLangChar(next, preamble) ||
593 runningChange != owner_->lookupChange(i + 1))
597 FontList::const_iterator cit = fontlist_.begin();
598 FontList::const_iterator end = fontlist_.end();
599 for (; cit != end; ++cit) {
600 if (cit->pos() >= i && !found) {
601 prev_font = cit->font();
604 if (cit->pos() >= i + 1)
607 // Stop here if there is a font attribute change.
608 if (found && cit != end && prev_font != cit->font())
610 docstring const latex = rtrim(encoding.latexChar(next), "}");
611 docstring::size_type const j =
612 latex.find_first_of(from_ascii("{"));
613 if (j == docstring::npos) {
615 length += latex.length();
617 os << latex.substr(j + 1);
618 length += latex.substr(j + 1).length();
622 // When the proper language is set, we are simply passed a code
623 // point, so we should not try to close the \textLANG command.
624 if (prefixIs(latex1, from_ascii("\\" + preamble))) {
632 bool Paragraph::Private::isTextAt(string const & str, pos_type pos) const
634 pos_type const len = str.length();
636 // is the paragraph large enough?
637 if (pos + len > size())
640 // does the wanted text start at point?
641 for (string::size_type i = 0; i < str.length(); ++i) {
642 // Caution: direct comparison of characters works only
643 // because str is pure ASCII.
644 if (str[i] != text_[pos + i])
648 return fontlist_.hasChangeInRange(pos, len);
652 void Paragraph::Private::latexInset(Buffer const & buf,
653 BufferParams const & bparams,
656 OutputParams & runparams,
659 Font const & outerfont,
661 Change & running_change,
662 Layout const & style,
664 unsigned int & column)
666 Inset * inset = owner_->getInset(i);
669 if (style.pass_thru) {
670 inset->plaintext(buf, os, runparams);
674 // FIXME: move this to InsetNewline::latex
675 if (inset->lyxCode() == NEWLINE_CODE) {
676 // newlines are handled differently here than
677 // the default in simpleTeXSpecialChars().
678 if (!style.newline_allowed) {
682 column += running_font.latexWriteEndChanges(
683 os, bparams, runparams,
688 if (running_font.family() == Font::TYPEWRITER_FAMILY)
691 basefont = owner_->getLayoutFont(bparams, outerfont);
692 running_font = basefont;
694 if (runparams.moving_arg)
700 texrow.start(owner_->id(), i + 1);
704 if (owner_->lookupChange(i).type == Change::DELETED) {
705 if( ++runparams.inDeletedInset == 1)
706 runparams.changeOfDeletedInset = owner_->lookupChange(i);
709 if (inset->canTrackChanges()) {
710 column += Changes::latexMarkChange(os, bparams, running_change,
711 Change(Change::UNCHANGED));
712 running_change = Change(Change::UNCHANGED);
716 odocstream::pos_type const len = os.tellp();
718 if ((inset->lyxCode() == GRAPHICS_CODE
719 || inset->lyxCode() == MATH_CODE
720 || inset->lyxCode() == HYPERLINK_CODE)
721 && running_font.isRightToLeft()) {
722 if (running_font.language()->lang() == "farsi")
729 // FIXME: Bug: we can have an empty font change here!
730 // if there has just been a font change, we are going to close it
731 // right now, which means stupid latex code like \textsf{}. AFAIK,
732 // this does not harm dvi output. A minor bug, thus (JMarc)
734 // Some insets cannot be inside a font change command.
735 // However, even such insets *can* be placed in \L or \R
736 // or their equivalents (for RTL language switches), so we don't
737 // close the language in those cases.
738 // ArabTeX, though, cannot handle this special behavior, it seems.
739 bool arabtex = basefont.language()->lang() == "arabic_arabtex"
740 || running_font.language()->lang() == "arabic_arabtex";
741 if (open_font && inset->noFontChange()) {
742 bool closeLanguage = arabtex
743 || basefont.isRightToLeft() == running_font.isRightToLeft();
744 unsigned int count = running_font.latexWriteEndChanges(os,
745 bparams, runparams, basefont, basefont, closeLanguage);
747 // if any font properties were closed, update the running_font,
748 // making sure, however, to leave the language as it was
750 // FIXME: probably a better way to keep track of the old
751 // language, than copying the entire font?
752 Font const copy_font(running_font);
753 basefont = owner_->getLayoutFont(bparams, outerfont);
754 running_font = basefont;
756 running_font.setLanguage(copy_font.language());
757 // leave font open if language is still open
758 open_font = (running_font.language() == basefont.language());
760 runparams.local_font = &basefont;
764 int tmp = inset->latex(buf, os, runparams);
767 if (running_font.language()->lang() == "farsi")
774 for (int j = 0; j < tmp; ++j)
777 texrow.start(owner_->id(), i + 1);
780 column += os.tellp() - len;
783 if (owner_->lookupChange(i).type == Change::DELETED)
784 --runparams.inDeletedInset;
788 void Paragraph::Private::latexSpecialChar(
790 OutputParams & runparams,
792 Change & running_change,
793 Layout const & style,
795 unsigned int & column)
797 char_type const c = text_[i];
799 if (style.pass_thru) {
801 // FIXME UNICODE: This can fail if c cannot
802 // be encoded in the current encoding.
807 if (runparams.verbatim) {
812 if (lyxrc.fontenc == "T1" && latexSpecialT1(c, os, i, column))
815 if (running_font.family() == Font::TYPEWRITER_FAMILY
816 && latexSpecialTypewriter(c, os, i, column))
819 // Otherwise, we use what LaTeX provides us.
822 os << "\\textbackslash{}";
826 os << "\\textless{}";
830 os << "\\textgreater{}";
841 os << "\\char`\\\"{}";
846 case '%': case '#': case '{':
854 os << "\\textasciitilde{}";
859 os << "\\textasciicircum{}";
864 // avoid being mistaken for optional arguments
872 // Blanks are printed before font switching.
873 // Sure? I am not! (try nice-latex)
874 // I am sure it's correct. LyX might be smarter
875 // in the future, but for now, nothing wrong is
882 if (latexSpecialPhrase(os, i, column))
888 Encoding const & encoding = *(runparams.encoding);
889 if (i + 1 < size()) {
890 char_type next = text_[i + 1];
891 if (Encodings::isCombiningChar(next)) {
892 column += latexSurrogatePair(os, c, next, encoding) - 1;
898 if (Encodings::isKnownLangChar(c, preamble)) {
899 column += knownLangChars(os, c, preamble, running_change,
903 docstring const latex = encoding.latexChar(c);
904 if (latex.length() > 1 && latex[latex.length() - 1] != '}') {
905 // Prevent eating of a following
906 // space or command corruption by
907 // following characters
908 column += latex.length() + 1;
911 column += latex.length() - 1;
919 bool Paragraph::Private::latexSpecialT1(char_type const c, odocstream & os,
920 pos_type & i, unsigned int & column)
926 // In T1 encoding, these characters exist
927 // but we should avoid ligatures
928 if (i + 1 > size() || text_[i + 1] != c)
933 //os << "\\textcompwordmark{}";
945 bool Paragraph::Private::latexSpecialTypewriter(char_type const c, odocstream & os,
946 pos_type & i, unsigned int & column)
950 if (i + 1 < size() && text_[i + 1] == '-') {
951 // "--" in Typewriter mode -> "-{}-"
958 // I assume this is hack treating typewriter as verbatim
959 // FIXME UNICODE: This can fail if c cannot be encoded
960 // in the current encoding.
965 // Those characters are not directly supported.
969 case '%': case '#': case '{':
978 // With Typewriter font, these characters exist.
985 bool Paragraph::Private::latexSpecialPhrase(odocstream & os, pos_type & i,
986 unsigned int & column)
988 // FIXME: if we have "LaTeX" with a font
989 // change in the middle (before the 'T', then
990 // the "TeX" part is still special cased.
991 // Really we should only operate this on
992 // "words" for some definition of word
994 for (size_t pnr = 0; pnr < phrases_nr; ++pnr) {
995 if (!isTextAt(special_phrases[pnr].phrase, i))
997 os << special_phrases[pnr].macro;
998 i += special_phrases[pnr].phrase.length() - 1;
999 column += special_phrases[pnr].macro.length() - 1;
1006 void Paragraph::Private::validate(LaTeXFeatures & features,
1007 Layout const & layout) const
1009 // check the params.
1010 if (!params_.spacing().isDefault())
1011 features.require("setspace");
1014 features.useLayout(layout.name());
1017 fontlist_.validate(features);
1019 // then the indentation
1020 if (!params_.leftIndent().zero())
1021 features.require("ParagraphLeftIndent");
1024 InsetList::const_iterator icit = insetlist_.begin();
1025 InsetList::const_iterator iend = insetlist_.end();
1026 for (; icit != iend; ++icit) {
1028 icit->inset->validate(features);
1029 if (layout.needprotect &&
1030 icit->inset->lyxCode() == FOOT_CODE)
1031 features.require("NeedLyXFootnoteCode");
1035 // then the contents
1036 for (pos_type i = 0; i < size() ; ++i) {
1037 for (size_t pnr = 0; pnr < phrases_nr; ++pnr) {
1038 if (!special_phrases[pnr].builtin
1039 && isTextAt(special_phrases[pnr].phrase, i)) {
1040 features.require(special_phrases[pnr].phrase);
1044 Encodings::validate(text_[i], features);
1048 /////////////////////////////////////////////////////////////////////
1052 /////////////////////////////////////////////////////////////////////
1054 Paragraph::Paragraph()
1055 : d(new Paragraph::Private(this))
1062 Paragraph::Paragraph(Paragraph const & par)
1063 : itemdepth(par.itemdepth),
1064 d(new Paragraph::Private(*par.d, this))
1069 Paragraph & Paragraph::operator=(Paragraph const & par)
1071 // needed as we will destroy the private part before copying it
1073 itemdepth = par.itemdepth;
1076 d = new Private(*par.d, this);
1082 Paragraph::~Paragraph()
1088 void Paragraph::write(Buffer const & buf, ostream & os,
1089 BufferParams const & bparams,
1090 depth_type & dth) const
1092 // The beginning or end of a deeper (i.e. nested) area?
1093 if (dth != params().depth()) {
1094 if (params().depth() > dth) {
1095 while (params().depth() > dth) {
1096 os << "\n\\begin_deeper";
1100 while (params().depth() < dth) {
1101 os << "\n\\end_deeper";
1107 // First write the layout
1108 os << "\n\\begin_layout " << to_utf8(layout()->name()) << '\n';
1112 Font font1(Font::ALL_INHERIT, bparams.language);
1114 Change running_change = Change(Change::UNCHANGED);
1117 for (pos_type i = 0; i <= size(); ++i) {
1119 Change change = lookupChange(i);
1120 Changes::lyxMarkChange(os, column, running_change, change);
1121 running_change = change;
1126 // Write font changes
1127 Font font2 = getFontSettings(bparams, i);
1128 if (font2 != font1) {
1129 font2.lyxWriteChanges(font1, os);
1134 char_type const c = d->text_[i];
1138 Inset const * inset = getInset(i);
1140 if (inset->directWrite()) {
1141 // international char, let it write
1142 // code directly so it's shorter in
1144 inset->write(buf, os);
1148 os << "\\begin_inset ";
1149 inset->write(buf, os);
1150 os << "\n\\end_inset\n\n";
1156 os << "\n\\backslash\n";
1160 if (i + 1 < size() && d->text_[i + 1] == ' ') {
1167 if ((column > 70 && c == ' ')
1172 // this check is to amend a bug. LyX sometimes
1173 // inserts '\0' this could cause problems.
1175 std::vector<char> tmp = ucs4_to_utf8(c);
1176 tmp.push_back('\0');
1179 lyxerr << "ERROR (Paragraph::writeFile):"
1180 " NULL char in structure." << endl;
1186 os << "\n\\end_layout\n";
1190 void Paragraph::validate(LaTeXFeatures & features) const
1192 d->validate(features, *layout());
1196 void Paragraph::insert(pos_type start, docstring const & str,
1197 Font const & font, Change const & change)
1199 for (size_t i = 0, n = str.size(); i != n ; ++i)
1200 insertChar(start + i, str[i], font, change);
1204 void Paragraph::appendChar(char_type c, Font const & font,
1205 Change const & change)
1208 d->changes_.insert(change, d->text_.size());
1209 // when appending characters, no need to update tables
1210 d->text_.push_back(c);
1211 setFont(d->text_.size() - 1, font);
1215 void Paragraph::appendString(docstring const & s, Font const & font,
1216 Change const & change)
1218 size_t end = s.size();
1219 size_t oldsize = d->text_.size();
1220 size_t newsize = oldsize + end;
1221 size_t capacity = d->text_.capacity();
1222 if (newsize >= capacity)
1223 d->text_.reserve(std::max(capacity + 100, newsize));
1225 // FIXME: Optimize this!
1226 for (pos_type i = 0; i != end; ++i) {
1228 d->changes_.insert(change, i);
1229 // when appending characters, no need to update tables
1230 d->text_.push_back(s[i]);
1232 d->fontlist_.setRange(oldsize, newsize, font);
1236 void Paragraph::insertChar(pos_type pos, char_type c,
1239 d->insertChar(pos, c, Change(trackChanges ?
1240 Change::INSERTED : Change::UNCHANGED));
1244 void Paragraph::insertChar(pos_type pos, char_type c,
1245 Font const & font, bool trackChanges)
1247 d->insertChar(pos, c, Change(trackChanges ?
1248 Change::INSERTED : Change::UNCHANGED));
1253 void Paragraph::insertChar(pos_type pos, char_type c,
1254 Font const & font, Change const & change)
1256 d->insertChar(pos, c, change);
1261 void Paragraph::insertInset(pos_type pos, Inset * inset,
1262 Font const & font, Change const & change)
1264 insertInset(pos, inset, change);
1265 // Set the font/language of the inset...
1270 bool Paragraph::insetAllowed(InsetCode code)
1272 return !d->inset_owner_ || d->inset_owner_->insetAllowed(code);
1276 // Gets uninstantiated font setting at position.
1277 Font const Paragraph::getFontSettings(BufferParams const & bparams,
1281 lyxerr << " pos: " << pos << " size: " << size() << endl;
1282 BOOST_ASSERT(pos <= size());
1285 FontList::const_iterator cit = d->fontlist_.fontIterator(pos);
1286 if (cit != d->fontlist_.end())
1289 if (pos == size() && !empty())
1290 return getFontSettings(bparams, pos - 1);
1292 return Font(Font::ALL_INHERIT, getParLanguage(bparams));
1296 FontSpan Paragraph::fontSpan(pos_type pos) const
1298 BOOST_ASSERT(pos <= size());
1301 FontList::const_iterator cit = d->fontlist_.begin();
1302 FontList::const_iterator end = d->fontlist_.end();
1303 for (; cit != end; ++cit) {
1304 if (cit->pos() >= pos) {
1305 if (pos >= beginOfBody())
1306 return FontSpan(std::max(start, beginOfBody()),
1309 return FontSpan(start,
1310 std::min(beginOfBody() - 1,
1313 start = cit->pos() + 1;
1316 // This should not happen, but if so, we take no chances.
1317 //lyxerr << "Paragraph::getEndPosOfFontSpan: This should not happen!"
1319 return FontSpan(pos, pos);
1323 // Gets uninstantiated font setting at position 0
1324 Font const Paragraph::getFirstFontSettings(BufferParams const & bparams) const
1326 if (!empty() && !d->fontlist_.empty())
1327 return d->fontlist_.begin()->font();
1329 return Font(Font::ALL_INHERIT, bparams.language);
1333 // Gets the fully instantiated font at a given position in a paragraph
1334 // This is basically the same function as Text::GetFont() in text2.cpp.
1335 // The difference is that this one is used for generating the LaTeX file,
1336 // and thus cosmetic "improvements" are disallowed: This has to deliver
1337 // the true picture of the buffer. (Asger)
1338 Font const Paragraph::getFont(BufferParams const & bparams, pos_type pos,
1339 Font const & outerfont) const
1341 BOOST_ASSERT(pos >= 0);
1343 Font font = getFontSettings(bparams, pos);
1345 pos_type const body_pos = beginOfBody();
1347 font.realize(d->layout_->labelfont);
1349 font.realize(d->layout_->font);
1351 font.realize(outerfont);
1352 font.realize(bparams.getFont());
1358 Font const Paragraph::getLabelFont
1359 (BufferParams const & bparams, Font const & outerfont) const
1361 Font tmpfont = layout()->labelfont;
1362 tmpfont.setLanguage(getParLanguage(bparams));
1363 tmpfont.realize(outerfont);
1364 tmpfont.realize(bparams.getFont());
1369 Font const Paragraph::getLayoutFont
1370 (BufferParams const & bparams, Font const & outerfont) const
1372 Font tmpfont = layout()->font;
1373 tmpfont.setLanguage(getParLanguage(bparams));
1374 tmpfont.realize(outerfont);
1375 tmpfont.realize(bparams.getFont());
1380 /// Returns the height of the highest font in range
1381 Font_size Paragraph::highestFontInRange
1382 (pos_type startpos, pos_type endpos, Font_size def_size) const
1384 return d->fontlist_.highestInRange(startpos, endpos, def_size);
1389 Paragraph::getUChar(BufferParams const & bparams, pos_type pos) const
1391 char_type c = d->text_[pos];
1392 if (!lyxrc.rtl_support)
1422 if (uc != c && getFontSettings(bparams, pos).isRightToLeft())
1429 void Paragraph::setFont(pos_type pos, Font const & font)
1431 BOOST_ASSERT(pos <= size());
1433 // First, reduce font against layout/label font
1434 // Update: The setCharFont() routine in text2.cpp already
1435 // reduces font, so we don't need to do that here. (Asger)
1437 d->fontlist_.set(pos, font);
1441 void Paragraph::makeSameLayout(Paragraph const & par)
1443 layout(par.layout());
1445 d->params_ = par.params();
1449 bool Paragraph::stripLeadingSpaces(bool trackChanges)
1451 if (isFreeSpacing())
1457 while (pos < size() && (isNewline(pos) || isLineSeparator(pos))) {
1458 if (eraseChar(pos, trackChanges))
1464 return count > 0 || pos > 0;
1468 bool Paragraph::hasSameLayout(Paragraph const & par) const
1470 return par.layout() == layout() && d->params_.sameLayout(par.params());
1474 depth_type Paragraph::getDepth() const
1476 return params().depth();
1480 depth_type Paragraph::getMaxDepthAfter() const
1482 if (layout()->isEnvironment())
1483 return params().depth() + 1;
1485 return params().depth();
1489 char Paragraph::getAlign() const
1491 if (params().align() == LYX_ALIGN_LAYOUT)
1492 return layout()->align;
1494 return params().align();
1498 docstring const & Paragraph::getLabelstring() const
1500 return params().labelString();
1504 // the next two functions are for the manual labels
1505 docstring const Paragraph::getLabelWidthString() const
1507 if (layout()->margintype == MARGIN_MANUAL)
1508 return params().labelWidthString();
1510 return _("Senseless with this layout!");
1514 void Paragraph::setLabelWidthString(docstring const & s)
1516 params().labelWidthString(s);
1520 docstring const Paragraph::translateIfPossible(docstring const & s,
1521 BufferParams const & bparams) const
1523 if (!support::isAscii(s) || s.empty()) {
1524 // This must be a user defined layout. We cannot translate
1525 // this, since gettext accepts only ascii keys.
1528 // Probably standard layout, try to translate
1529 Messages & m = getMessages(getParLanguage(bparams)->code());
1530 return m.get(to_ascii(s));
1534 docstring Paragraph::expandLabel(LayoutPtr const & layout,
1535 BufferParams const & bparams, bool process_appendix) const
1537 TextClass const & tclass = bparams.getTextClass();
1540 if (process_appendix && params().appendix())
1541 fmt = translateIfPossible(layout->labelstring_appendix(),
1544 fmt = translateIfPossible(layout->labelstring(), bparams);
1546 if (fmt.empty() && layout->labeltype == LABEL_COUNTER
1547 && !layout->counter.empty())
1548 fmt = "\\the" + layout->counter;
1550 // handle 'inherited level parts' in 'fmt',
1551 // i.e. the stuff between '@' in '@Section@.\arabic{subsection}'
1552 size_t const i = fmt.find('@', 0);
1553 if (i != docstring::npos) {
1554 size_t const j = fmt.find('@', i + 1);
1555 if (j != docstring::npos) {
1556 docstring parent(fmt, i + 1, j - i - 1);
1557 docstring label = from_ascii("??");
1558 if (tclass.hasLayout(parent))
1559 docstring label = expandLabel(tclass[parent], bparams,
1561 fmt = docstring(fmt, 0, i) + label
1562 + docstring(fmt, j + 1, docstring::npos);
1566 return tclass.counters().counterLabel(fmt);
1570 void Paragraph::applyLayout(LayoutPtr const & new_layout)
1573 LyXAlignment const oldAlign = params().align();
1575 if (!(oldAlign & layout()->alignpossible)) {
1576 frontend::Alert::warning(_("Alignment not permitted"),
1577 _("The new layout does not permit the alignment previously used.\nSetting to default."));
1578 params().align(LYX_ALIGN_LAYOUT);
1583 pos_type Paragraph::beginOfBody() const
1585 return d->begin_of_body_;
1589 void Paragraph::setBeginOfBody()
1591 if (layout()->labeltype != LABEL_MANUAL) {
1592 d->begin_of_body_ = 0;
1596 // Unroll the first two cycles of the loop
1597 // and remember the previous character to
1598 // remove unnecessary getChar() calls
1600 pos_type end = size();
1601 if (i < end && !isNewline(i)) {
1603 char_type previous_char = 0;
1606 previous_char = d->text_[i];
1607 if (!isNewline(i)) {
1609 while (i < end && previous_char != ' ') {
1614 previous_char = temp;
1620 d->begin_of_body_ = i;
1624 InsetBibitem * Paragraph::bibitem() const
1626 if (!d->insetlist_.empty()) {
1627 Inset * inset = d->insetlist_.begin()->inset;
1628 if (inset->lyxCode() == BIBITEM_CODE)
1629 return static_cast<InsetBibitem *>(inset);
1635 bool Paragraph::forceDefaultParagraphs() const
1637 return inInset() && inInset()->forceDefaultParagraphs(0);
1643 // paragraphs inside floats need different alignment tags to avoid
1646 bool noTrivlistCentering(InsetCode code)
1648 return code == FLOAT_CODE || code == WRAP_CODE;
1652 string correction(string const & orig)
1654 if (orig == "flushleft")
1655 return "raggedright";
1656 if (orig == "flushright")
1657 return "raggedleft";
1658 if (orig == "center")
1664 string const corrected_env(string const & suffix, string const & env,
1667 string output = suffix + "{";
1668 if (noTrivlistCentering(code))
1669 output += correction(env);
1673 if (suffix == "\\begin")
1679 void adjust_row_column(string const & str, TexRow & texrow, int & column)
1681 if (!contains(str, "\n"))
1682 column += str.size();
1686 column = rsplit(str, tmp, '\n').size();
1693 int Paragraph::Private::startTeXParParams(BufferParams const & bparams,
1694 odocstream & os, TexRow & texrow,
1695 bool moving_arg) const
1699 if (params_.noindent()) {
1700 os << "\\noindent ";
1704 LyXAlignment const curAlign = params_.align();
1706 if (curAlign == layout_->align)
1710 case LYX_ALIGN_NONE:
1711 case LYX_ALIGN_BLOCK:
1712 case LYX_ALIGN_LAYOUT:
1713 case LYX_ALIGN_SPECIAL:
1715 case LYX_ALIGN_LEFT:
1716 case LYX_ALIGN_RIGHT:
1717 case LYX_ALIGN_CENTER:
1726 case LYX_ALIGN_NONE:
1727 case LYX_ALIGN_BLOCK:
1728 case LYX_ALIGN_LAYOUT:
1729 case LYX_ALIGN_SPECIAL:
1731 case LYX_ALIGN_LEFT: {
1733 if (owner_->getParLanguage(bparams)->babel() != "hebrew")
1734 output = corrected_env("\\begin", "flushleft", owner_->ownerCode());
1736 output = corrected_env("\\begin", "flushright", owner_->ownerCode());
1737 os << from_ascii(output);
1738 adjust_row_column(output, texrow, column);
1740 } case LYX_ALIGN_RIGHT: {
1742 if (owner_->getParLanguage(bparams)->babel() != "hebrew")
1743 output = corrected_env("\\begin", "flushright", owner_->ownerCode());
1745 output = corrected_env("\\begin", "flushleft", owner_->ownerCode());
1746 os << from_ascii(output);
1747 adjust_row_column(output, texrow, column);
1749 } case LYX_ALIGN_CENTER: {
1751 output = corrected_env("\\begin", "center", owner_->ownerCode());
1752 os << from_ascii(output);
1753 adjust_row_column(output, texrow, column);
1762 int Paragraph::Private::endTeXParParams(BufferParams const & bparams,
1763 odocstream & os, TexRow & texrow,
1764 bool moving_arg) const
1768 switch (params_.align()) {
1769 case LYX_ALIGN_NONE:
1770 case LYX_ALIGN_BLOCK:
1771 case LYX_ALIGN_LAYOUT:
1772 case LYX_ALIGN_SPECIAL:
1774 case LYX_ALIGN_LEFT:
1775 case LYX_ALIGN_RIGHT:
1776 case LYX_ALIGN_CENTER:
1784 switch (params_.align()) {
1785 case LYX_ALIGN_NONE:
1786 case LYX_ALIGN_BLOCK:
1787 case LYX_ALIGN_LAYOUT:
1788 case LYX_ALIGN_SPECIAL:
1790 case LYX_ALIGN_LEFT: {
1792 if (owner_->getParLanguage(bparams)->babel() != "hebrew")
1793 output = corrected_env("\n\\par\\end", "flushleft", owner_->ownerCode());
1795 output = corrected_env("\n\\par\\end", "flushright", owner_->ownerCode());
1796 os << from_ascii(output);
1797 adjust_row_column(output, texrow, column);
1799 } case LYX_ALIGN_RIGHT: {
1801 if (owner_->getParLanguage(bparams)->babel() != "hebrew")
1802 output = corrected_env("\n\\par\\end", "flushright", owner_->ownerCode());
1804 output = corrected_env("\n\\par\\end", "flushleft", owner_->ownerCode());
1805 os << from_ascii(output);
1806 adjust_row_column(output, texrow, column);
1808 } case LYX_ALIGN_CENTER: {
1810 output = corrected_env("\n\\par\\end", "center", owner_->ownerCode());
1811 os << from_ascii(output);
1812 adjust_row_column(output, texrow, column);
1821 // This one spits out the text of the paragraph
1822 bool Paragraph::latex(Buffer const & buf,
1823 BufferParams const & bparams,
1824 Font const & outerfont,
1825 odocstream & os, TexRow & texrow,
1826 OutputParams const & runparams) const
1828 LYXERR(Debug::LATEX) << "SimpleTeXOnePar... " << this << endl;
1830 bool return_value = false;
1834 // well we have to check if we are in an inset with unlimited
1835 // length (all in one row) if that is true then we don't allow
1836 // any special options in the paragraph and also we don't allow
1837 // any environment other than the default layout of the text class
1839 bool asdefault = forceDefaultParagraphs();
1842 style = bparams.getTextClass().defaultLayout();
1847 // Current base font for all inherited font changes, without any
1848 // change caused by an individual character, except for the language:
1849 // It is set to the language of the first character.
1850 // As long as we are in the label, this font is the base font of the
1851 // label. Before the first body character it is set to the base font
1855 // Maybe we have to create a optional argument.
1856 pos_type body_pos = beginOfBody();
1857 unsigned int column = 0;
1860 // the optional argument is kept in curly brackets in
1861 // case it contains a ']'
1864 basefont = getLabelFont(bparams, outerfont);
1866 basefont = getLayoutFont(bparams, outerfont);
1869 // Which font is currently active?
1870 Font running_font(basefont);
1871 // Do we have an open font change?
1872 bool open_font = false;
1874 Change runningChange = Change(Change::UNCHANGED);
1876 texrow.start(id(), 0);
1878 // if the paragraph is empty, the loop will not be entered at all
1880 if (style->isCommand()) {
1885 column += d->startTeXParParams(bparams, os, texrow,
1886 runparams.moving_arg);
1889 for (pos_type i = 0; i < size(); ++i) {
1890 // First char in paragraph or after label?
1891 if (i == body_pos) {
1894 column += running_font.latexWriteEndChanges(
1895 os, bparams, runparams,
1896 basefont, basefont);
1899 basefont = getLayoutFont(bparams, outerfont);
1900 running_font = basefont;
1902 column += Changes::latexMarkChange(os, bparams,
1903 runningChange, Change(Change::UNCHANGED));
1904 runningChange = Change(Change::UNCHANGED);
1909 if (style->isCommand()) {
1915 column += d->startTeXParParams(bparams, os,
1917 runparams.moving_arg);
1920 Change const & change = runparams.inDeletedInset ? runparams.changeOfDeletedInset
1923 if (bparams.outputChanges && runningChange != change) {
1925 column += running_font.latexWriteEndChanges(
1926 os, bparams, runparams, basefont, basefont);
1929 basefont = getLayoutFont(bparams, outerfont);
1930 running_font = basefont;
1932 column += Changes::latexMarkChange(os, bparams, runningChange, change);
1933 runningChange = change;
1936 // do not output text which is marked deleted
1937 // if change tracking output is disabled
1938 if (!bparams.outputChanges && change.type == Change::DELETED) {
1944 // Fully instantiated font
1945 Font const font = getFont(bparams, i, outerfont);
1947 Font const last_font = running_font;
1949 // Do we need to close the previous font?
1951 (font != running_font ||
1952 font.language() != running_font.language()))
1954 column += running_font.latexWriteEndChanges(
1955 os, bparams, runparams, basefont,
1956 (i == body_pos-1) ? basefont : font);
1957 running_font = basefont;
1961 // Switch file encoding if necessary (and allowed)
1962 if (!runparams.verbatim &&
1963 runparams.encoding->package() == Encoding::inputenc &&
1964 font.language()->encoding()->package() == Encoding::inputenc) {
1965 std::pair<bool, int> const enc_switch = switchEncoding(os, bparams,
1966 runparams.moving_arg, *(runparams.encoding),
1967 *(font.language()->encoding()));
1968 if (enc_switch.first) {
1969 column += enc_switch.second;
1970 runparams.encoding = font.language()->encoding();
1974 char_type const c = d->text_[i];
1976 // Do we need to change font?
1977 if ((font != running_font ||
1978 font.language() != running_font.language()) &&
1981 odocstringstream ods;
1982 column += font.latexWriteStartChanges(ods, bparams,
1983 runparams, basefont,
1985 running_font = font;
1987 docstring fontchange = ods.str();
1988 // check if the fontchange ends with a trailing blank
1989 // (like "\small " (see bug 3382)
1990 if (suffixIs(fontchange, ' ') && c == ' ')
1991 os << fontchange.substr(0, fontchange.size() - 1)
1992 << from_ascii("{}");
1998 // FIXME: integrate this case in latexSpecialChar
1999 // Do not print the separation of the optional argument
2000 // if style->pass_thru is false. This works because
2001 // latexSpecialChar ignores spaces if
2002 // style->pass_thru is false.
2003 if (i != body_pos - 1) {
2004 if (d->simpleTeXBlanks(
2005 *(runparams.encoding), os, texrow,
2006 i, column, font, *style)) {
2007 // A surrogate pair was output. We
2008 // must not call latexSpecialChar
2009 // in this iteration, since it would output
2010 // the combining character again.
2017 OutputParams rp = runparams;
2018 rp.free_spacing = style->free_spacing;
2019 rp.local_font = &font;
2020 rp.intitle = style->intitle;
2022 // Two major modes: LaTeX or plain
2023 // Handle here those cases common to both modes
2024 // and then split to handle the two modes separately.
2025 if (c == META_INSET)
2026 d->latexInset(buf, bparams, os,
2027 texrow, rp, running_font,
2028 basefont, outerfont, open_font,
2029 runningChange, *style, i, column);
2031 d->latexSpecialChar(os, rp, running_font, runningChange,
2034 // Set the encoding to that returned from simpleTeXSpecialChars (see
2035 // comment for encoding member in OutputParams.h)
2036 runparams.encoding = rp.encoding;
2039 // If we have an open font definition, we have to close it
2041 #ifdef FIXED_LANGUAGE_END_DETECTION
2044 .latexWriteEndChanges(os, bparams, runparams,
2046 next_->getFont(bparams, 0, outerfont));
2048 running_font.latexWriteEndChanges(os, bparams,
2049 runparams, basefont, basefont);
2052 //FIXME: For now we ALWAYS have to close the foreign font settings if they are
2053 //FIXME: there as we start another \selectlanguage with the next paragraph if
2054 //FIXME: we are in need of this. This should be fixed sometime (Jug)
2055 running_font.latexWriteEndChanges(os, bparams, runparams,
2056 basefont, basefont);
2060 column += Changes::latexMarkChange(os, bparams, runningChange, Change(Change::UNCHANGED));
2062 // Needed if there is an optional argument but no contents.
2063 if (body_pos > 0 && body_pos == size()) {
2065 return_value = false;
2069 column += d->endTeXParParams(bparams, os, texrow,
2070 runparams.moving_arg);
2073 LYXERR(Debug::LATEX) << "SimpleTeXOnePar...done " << this << endl;
2074 return return_value;
2091 string tag_name(PAR_TAG const & pt) {
2093 case PAR_NONE: return "!-- --";
2094 case TT: return "tt";
2095 case SF: return "sf";
2096 case BF: return "bf";
2097 case IT: return "it";
2098 case SL: return "sl";
2099 case EM: return "em";
2106 void operator|=(PAR_TAG & p1, PAR_TAG const & p2)
2108 p1 = static_cast<PAR_TAG>(p1 | p2);
2113 void reset(PAR_TAG & p1, PAR_TAG const & p2)
2115 p1 = static_cast<PAR_TAG>(p1 & ~p2);
2121 bool Paragraph::emptyTag() const
2123 for (pos_type i = 0; i < size(); ++i) {
2125 Inset const * inset = getInset(i);
2126 InsetCode lyx_code = inset->lyxCode();
2127 if (lyx_code != TOC_CODE &&
2128 lyx_code != INCLUDE_CODE &&
2129 lyx_code != GRAPHICS_CODE &&
2130 lyx_code != ERT_CODE &&
2131 lyx_code != LISTINGS_CODE &&
2132 lyx_code != FLOAT_CODE &&
2133 lyx_code != TABULAR_CODE) {
2137 char_type c = d->text_[i];
2138 if (c != ' ' && c != '\t')
2146 string Paragraph::getID(Buffer const & buf, OutputParams const & runparams) const
2148 for (pos_type i = 0; i < size(); ++i) {
2150 Inset const * inset = getInset(i);
2151 InsetCode lyx_code = inset->lyxCode();
2152 if (lyx_code == LABEL_CODE) {
2153 InsetLabel const * const il = static_cast<InsetLabel const *>(inset);
2154 docstring const & id = il->getParam("name");
2155 return "id='" + to_utf8(sgml::cleanID(buf, runparams, id)) + "'";
2164 pos_type Paragraph::getFirstWord(Buffer const & buf, odocstream & os, OutputParams const & runparams) const
2167 for (i = 0; i < size(); ++i) {
2169 Inset const * inset = getInset(i);
2170 inset->docbook(buf, os, runparams);
2172 char_type c = d->text_[i];
2175 os << sgml::escapeChar(c);
2182 bool Paragraph::onlyText(Buffer const & buf, Font const & outerfont, pos_type initial) const
2186 for (pos_type i = initial; i < size(); ++i) {
2187 Font font = getFont(buf.params(), i, outerfont);
2190 if (i != initial && font != font_old)
2199 void Paragraph::simpleDocBookOnePar(Buffer const & buf,
2201 OutputParams const & runparams,
2202 Font const & outerfont,
2203 pos_type initial) const
2205 bool emph_flag = false;
2207 LayoutPtr const & style = layout();
2209 style->labeltype == LABEL_MANUAL ? style->labelfont : style->font;
2211 if (style->pass_thru && !onlyText(buf, outerfont, initial))
2214 // parsing main loop
2215 for (pos_type i = initial; i < size(); ++i) {
2216 Font font = getFont(buf.params(), i, outerfont);
2218 // handle <emphasis> tag
2219 if (font_old.emph() != font.emph()) {
2220 if (font.emph() == Font::ON) {
2223 } else if (i != initial) {
2224 os << "</emphasis>";
2230 Inset const * inset = getInset(i);
2231 inset->docbook(buf, os, runparams);
2233 char_type c = d->text_[i];
2235 if (style->pass_thru)
2238 os << sgml::escapeChar(c);
2244 os << "</emphasis>";
2247 if (style->free_spacing)
2249 if (style->pass_thru && !onlyText(buf, outerfont, initial))
2254 bool Paragraph::isHfill(pos_type pos) const
2257 && getInset(pos)->lyxCode() == HFILL_CODE;
2261 bool Paragraph::isNewline(pos_type pos) const
2264 && getInset(pos)->lyxCode() == NEWLINE_CODE;
2268 bool Paragraph::isLineSeparator(pos_type pos) const
2270 char_type const c = d->text_[pos];
2271 return isLineSeparatorChar(c)
2272 || (c == META_INSET && getInset(pos) &&
2273 getInset(pos)->isLineSeparator());
2277 /// Used by the spellchecker
2278 bool Paragraph::isLetter(pos_type pos) const
2281 return getInset(pos)->isLetter();
2283 char_type const c = d->text_[pos];
2284 return isLetterChar(c) || isDigit(c);
2290 Paragraph::getParLanguage(BufferParams const & bparams) const
2293 return getFirstFontSettings(bparams).language();
2294 // FIXME: we should check the prev par as well (Lgb)
2295 return bparams.language;
2299 bool Paragraph::isRTL(BufferParams const & bparams) const
2301 return lyxrc.rtl_support
2302 && getParLanguage(bparams)->rightToLeft()
2303 && ownerCode() != ERT_CODE
2304 && ownerCode() != LISTINGS_CODE;
2308 void Paragraph::changeLanguage(BufferParams const & bparams,
2309 Language const * from, Language const * to)
2311 // change language including dummy font change at the end
2312 for (pos_type i = 0; i <= size(); ++i) {
2313 Font font = getFontSettings(bparams, i);
2314 if (font.language() == from) {
2315 font.setLanguage(to);
2322 bool Paragraph::isMultiLingual(BufferParams const & bparams) const
2324 Language const * doc_language = bparams.language;
2325 FontList::const_iterator cit = d->fontlist_.begin();
2326 FontList::const_iterator end = d->fontlist_.end();
2328 for (; cit != end; ++cit)
2329 if (cit->font().language() != ignore_language &&
2330 cit->font().language() != latex_language &&
2331 cit->font().language() != doc_language)
2337 // Convert the paragraph to a string.
2338 // Used for building the table of contents
2339 docstring const Paragraph::asString(Buffer const & buffer, bool label) const
2341 return asString(buffer, 0, size(), label);
2345 docstring const Paragraph::asString(Buffer const & buffer,
2346 pos_type beg, pos_type end, bool label) const
2349 odocstringstream os;
2351 if (beg == 0 && label && !params().labelString().empty())
2352 os << params().labelString() << ' ';
2354 for (pos_type i = beg; i < end; ++i) {
2355 char_type const c = d->text_[i];
2358 else if (c == META_INSET)
2359 getInset(i)->textString(buffer, os);
2366 void Paragraph::setInsetOwner(Inset * inset)
2368 d->inset_owner_ = inset;
2372 int Paragraph::id() const
2378 LayoutPtr const & Paragraph::layout() const
2384 void Paragraph::layout(LayoutPtr const & new_layout)
2386 d->layout_ = new_layout;
2390 Inset * Paragraph::inInset() const
2392 return d->inset_owner_;
2396 InsetCode Paragraph::ownerCode() const
2398 return d->inset_owner_ ?
2399 d->inset_owner_->lyxCode() : NO_CODE;
2403 ParagraphParameters & Paragraph::params()
2409 ParagraphParameters const & Paragraph::params() const
2415 bool Paragraph::isFreeSpacing() const
2417 if (layout()->free_spacing)
2420 // for now we just need this, later should we need this in some
2421 // other way we can always add a function to Inset too.
2422 return ownerCode() == ERT_CODE || ownerCode() == LISTINGS_CODE;
2426 bool Paragraph::allowEmpty() const
2428 if (layout()->keepempty)
2430 return ownerCode() == ERT_CODE || ownerCode() == LISTINGS_CODE;
2434 char_type Paragraph::transformChar(char_type c, pos_type pos) const
2436 if (!Encodings::is_arabic(c))
2439 char_type prev_char = ' ';
2440 char_type next_char = ' ';
2442 for (pos_type i = pos - 1; i >= 0; --i) {
2443 char_type const par_char = d->text_[i];
2444 if (!Encodings::isComposeChar_arabic(par_char)) {
2445 prev_char = par_char;
2450 for (pos_type i = pos + 1, end = size(); i < end; ++i) {
2451 char_type const par_char = d->text_[i];
2452 if (!Encodings::isComposeChar_arabic(par_char)) {
2453 next_char = par_char;
2458 if (Encodings::is_arabic(next_char)) {
2459 if (Encodings::is_arabic(prev_char) &&
2460 !Encodings::is_arabic_special(prev_char))
2461 return Encodings::transformChar(c, Encodings::FORM_MEDIAL);
2463 return Encodings::transformChar(c, Encodings::FORM_INITIAL);
2465 if (Encodings::is_arabic(prev_char) &&
2466 !Encodings::is_arabic_special(prev_char))
2467 return Encodings::transformChar(c, Encodings::FORM_FINAL);
2469 return Encodings::transformChar(c, Encodings::FORM_ISOLATED);
2474 int Paragraph::checkBiblio(bool track_changes)
2477 //This is getting more and more a mess. ...We really should clean
2478 //up this bibitem issue for 1.6. See also bug 2743.
2480 // Add bibitem insets if necessary
2481 if (layout()->labeltype != LABEL_BIBLIO)
2484 bool hasbibitem = !d->insetlist_.empty()
2485 // Insist on it being in pos 0
2486 && d->text_[0] == META_INSET
2487 && d->insetlist_.begin()->inset->lyxCode() == BIBITEM_CODE;
2492 // remove a bibitem in pos != 0
2493 // restore it later in pos 0 if necessary
2494 // (e.g. if a user inserts contents _before_ the item)
2495 // we're assuming there's only one of these, which there
2497 int erasedInsetPosition = -1;
2498 InsetList::iterator it = d->insetlist_.begin();
2499 InsetList::iterator end = d->insetlist_.end();
2500 for (; it != end; ++it)
2501 if (it->inset->lyxCode() == BIBITEM_CODE
2503 InsetBibitem * olditem = static_cast<InsetBibitem *>(it->inset);
2504 oldkey = olditem->getParam("key");
2505 oldlabel = olditem->getParam("label");
2506 erasedInsetPosition = it->pos;
2507 eraseChar(erasedInsetPosition, track_changes);
2511 //There was an InsetBibitem at the beginning, and we didn't
2512 //have to erase one.
2513 if (hasbibitem && erasedInsetPosition < 0)
2516 //There was an InsetBibitem at the beginning and we did have to
2517 //erase one. So we give its properties to the beginning inset.
2519 InsetBibitem * inset =
2520 static_cast<InsetBibitem *>(d->insetlist_.begin()->inset);
2521 if (!oldkey.empty())
2522 inset->setParam("key", oldkey);
2523 inset->setParam("label", oldlabel);
2524 return -erasedInsetPosition;
2527 //There was no inset at the beginning, so we need to create one with
2528 //the key and label of the one we erased.
2529 InsetBibitem * inset(new InsetBibitem(InsetCommandParams(BIBITEM_CODE)));
2530 // restore values of previously deleted item in this par.
2531 if (!oldkey.empty())
2532 inset->setParam("key", oldkey);
2533 inset->setParam("label", oldlabel);
2534 insertInset(0, static_cast<Inset *>(inset),
2535 Change(track_changes ? Change::INSERTED : Change::UNCHANGED));
2541 void Paragraph::checkAuthors(AuthorList const & authorList)
2543 d->changes_.checkAuthors(authorList);
2547 bool Paragraph::isUnchanged(pos_type pos) const
2549 return lookupChange(pos).type == Change::UNCHANGED;
2553 bool Paragraph::isInserted(pos_type pos) const
2555 return lookupChange(pos).type == Change::INSERTED;
2559 bool Paragraph::isDeleted(pos_type pos) const
2561 return lookupChange(pos).type == Change::DELETED;
2565 InsetList const & Paragraph::insetList() const
2567 return d->insetlist_;
2571 Inset * Paragraph::releaseInset(pos_type pos)
2573 Inset * inset = d->insetlist_.release(pos);
2574 /// does not honour change tracking!
2575 eraseChar(pos, false);
2580 Inset * Paragraph::getInset(pos_type pos)
2582 return d->insetlist_.get(pos);
2586 Inset const * Paragraph::getInset(pos_type pos) const
2588 return d->insetlist_.get(pos);
2592 int Paragraph::numberOfOptArgs() const
2595 InsetList::const_iterator it = insetList().begin();
2596 InsetList::const_iterator end = insetList().end();
2597 for (; it != end ; ++it) {
2598 if (it->inset->lyxCode() == OPTARG_CODE)
2605 void Paragraph::changeCase(BufferParams const & bparams, pos_type pos,
2606 pos_type right, TextCase action)
2608 // process sequences of modified characters; in change
2609 // tracking mode, this approach results in much better
2610 // usability than changing case on a char-by-char basis
2613 bool const trackChanges = bparams.trackChanges;
2615 bool capitalize = true;
2617 for (; pos < right; ++pos) {
2618 char_type oldChar = d->text_[pos];
2619 char_type newChar = oldChar;
2621 // ignore insets and don't play with deleted text!
2622 if (isInset(pos) && !isDeleted(pos)) {
2624 case text_lowercase:
2625 newChar = lowercase(oldChar);
2627 case text_capitalization:
2629 newChar = uppercase(oldChar);
2633 case text_uppercase:
2634 newChar = uppercase(oldChar);
2639 if (!isLetter(pos) || isDeleted(pos)) {
2640 // permit capitalization again
2644 if (oldChar != newChar)
2647 if (oldChar == newChar || pos == right - 1) {
2648 if (oldChar != newChar) {
2649 // step behind the changing area
2652 int erasePos = pos - changes.size();
2653 for (size_t i = 0; i < changes.size(); i++) {
2654 insertChar(pos, changes[i],
2655 getFontSettings(bparams,
2658 if (!eraseChar(erasePos, trackChanges)) {
2661 ++right; // expand selection
2670 bool Paragraph::find(docstring const & str, bool cs, bool mw,
2671 pos_type pos, bool del) const
2673 int const strsize = str.length();
2675 pos_type const parsize = d->text_.size();
2676 for (i = 0; pos + i < parsize; ++i) {
2679 if (cs && str[i] != d->text_[pos + i])
2681 if (!cs && uppercase(str[i]) != uppercase(d->text_[pos + i]))
2683 if (!del && isDeleted(pos + i))
2690 // if necessary, check whether string matches word
2692 if (pos > 0 && isLetter(pos - 1))
2694 if (pos + strsize < parsize
2695 && isLetter(pos + strsize))
2703 char_type Paragraph::getChar(pos_type pos) const
2705 return d->text_[pos];
2709 pos_type Paragraph::size() const
2711 return d->text_.size();
2715 bool Paragraph::empty() const
2717 return d->text_.empty();
2721 bool Paragraph::isInset(pos_type pos) const
2723 return d->text_[pos] == META_INSET;
2727 bool Paragraph::isSeparator(pos_type pos) const
2729 //FIXME: Are we sure this can be the only separator?
2730 return d->text_[pos] == ' ';