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/InsetOptArg.h"
53 #include "support/lstrings.h"
54 #include "support/textutils.h"
55 #include "support/convert.h"
56 #include "support/unicode.h"
66 using support::contains;
67 using support::prefixIs;
68 using support::suffixIs;
69 using support::rsplit;
73 /////////////////////////////////////////////////////////////////////
77 /////////////////////////////////////////////////////////////////////
79 class Paragraph::Private
83 Private(Paragraph * owner);
84 /// "Copy constructor"
85 Private(Private const &, Paragraph * owner);
88 value_type getChar(pos_type pos) const;
90 void insertChar(pos_type pos, value_type c, Change const & change);
92 /// Output the surrogate pair formed by \p c and \p next to \p os.
93 /// \return the number of characters written.
94 int latexSurrogatePair(odocstream & os, value_type c, value_type next,
97 /// Output a space in appropriate formatting (or a surrogate pair
98 /// if the next character is a combining character).
99 /// \return whether a surrogate pair was output.
100 bool simpleTeXBlanks(Encoding const &,
101 odocstream &, TexRow & texrow,
103 unsigned int & column,
105 Layout const & style);
107 /// Output consecutive known unicode chars, belonging to the same
108 /// language as specified by \p preamble, to \p os starting from \p c.
109 /// \return the number of characters written.
110 int knownLangChars(odocstream & os, value_type c, string & preamble,
111 Change &, Encoding const &, pos_type &);
113 /// This could go to ParagraphParameters if we want to.
114 int startTeXParParams(BufferParams const &, odocstream &, TexRow &,
117 /// This could go to ParagraphParameters if we want to.
118 int endTeXParParams(BufferParams const &, odocstream &, TexRow &,
122 void latexInset(Buffer const &, BufferParams const &,
124 TexRow & texrow, OutputParams &,
127 Font const & outerfont,
129 Change & running_change,
130 Layout const & style,
132 unsigned int & column);
135 void latexSpecialChar(
137 OutputParams & runparams,
139 Change & running_change,
140 Layout const & style,
142 unsigned int & column);
149 unsigned int & column);
151 bool latexSpecialTypewriter(
155 unsigned int & column);
157 bool latexSpecialPhrase(
160 unsigned int & column);
163 void validate(LaTeXFeatures & features,
164 Layout const & layout) const;
167 pos_type size() const { return owner_->size(); }
169 /// match a string against a particular point in the paragraph
170 bool isTextAt(std::string const & str, pos_type pos) const;
172 /// Which Paragraph owns us?
176 Inset * inset_owner_;
184 static unsigned int paragraph_id;
186 ParagraphParameters params_;
188 /// for recording and looking up changes
192 InsetList insetlist_;
198 pos_type begin_of_body_;
205 using std::upper_bound;
206 using std::lower_bound;
210 // Initialization of the counter for the paragraph id's,
211 unsigned int Paragraph::Private::paragraph_id = 0;
215 struct special_phrase {
221 special_phrase const special_phrases[] = {
222 { "LyX", from_ascii("\\protect\\LyX{}"), false },
223 { "TeX", from_ascii("\\protect\\TeX{}"), true },
224 { "LaTeX2e", from_ascii("\\protect\\LaTeXe{}"), true },
225 { "LaTeX", from_ascii("\\protect\\LaTeX{}"), true },
228 size_t const phrases_nr = sizeof(special_phrases)/sizeof(special_phrase);
233 Paragraph::Private::Private(Paragraph * owner)
234 : owner_(owner), inset_owner_(0), begin_of_body_(0)
236 id_ = paragraph_id++;
240 Paragraph::Private::Private(Private const & p, Paragraph * owner)
241 : owner_(owner), inset_owner_(p.inset_owner_), fontlist_(p.fontlist_),
242 params_(p.params_), changes_(p.changes_), insetlist_(p.insetlist_),
243 layout_(p.layout_), begin_of_body_(p.begin_of_body_)
245 id_ = paragraph_id++;
249 bool Paragraph::isChanged(pos_type start, pos_type end) const
251 BOOST_ASSERT(start >= 0 && start <= size());
252 BOOST_ASSERT(end > start && end <= size() + 1);
254 return d->changes_.isChanged(start, end);
258 bool Paragraph::isMergedOnEndOfParDeletion(bool trackChanges) const {
259 // keep the logic here in sync with the logic of eraseChars()
265 Change change = d->changes_.lookup(size());
267 return change.type == Change::INSERTED && change.author == 0;
271 void Paragraph::setChange(Change const & change)
273 // beware of the imaginary end-of-par character!
274 d->changes_.set(change, 0, size() + 1);
277 * Propagate the change recursively - but not in case of DELETED!
279 * Imagine that your co-author makes changes in an existing inset. He
280 * sends your document to you and you come to the conclusion that the
281 * inset should go completely. If you erase it, LyX must not delete all
282 * text within the inset. Otherwise, the change tracked insertions of
283 * your co-author get lost and there is no way to restore them later.
285 * Conclusion: An inset's content should remain untouched if you delete it
288 if (change.type != Change::DELETED) {
289 for (pos_type pos = 0; pos < size(); ++pos) {
291 getInset(pos)->setChange(change);
297 void Paragraph::setChange(pos_type pos, Change const & change)
299 BOOST_ASSERT(pos >= 0 && pos <= size());
301 d->changes_.set(change, pos);
303 // see comment in setChange(Change const &) above
305 if (change.type != Change::DELETED &&
306 pos < size() && isInset(pos)) {
307 getInset(pos)->setChange(change);
312 Change const & Paragraph::lookupChange(pos_type pos) const
314 BOOST_ASSERT(pos >= 0 && pos <= size());
316 return d->changes_.lookup(pos);
320 void Paragraph::acceptChanges(BufferParams const & bparams, pos_type start,
323 BOOST_ASSERT(start >= 0 && start <= size());
324 BOOST_ASSERT(end > start && end <= size() + 1);
326 for (pos_type pos = start; pos < end; ++pos) {
327 switch (lookupChange(pos).type) {
328 case Change::UNCHANGED:
329 // accept changes in nested inset
330 if (pos < size() && isInset(pos))
331 getInset(pos)->acceptChanges(bparams);
335 case Change::INSERTED:
336 d->changes_.set(Change(Change::UNCHANGED), pos);
337 // also accept changes in nested inset
338 if (pos < size() && isInset(pos)) {
339 getInset(pos)->acceptChanges(bparams);
343 case Change::DELETED:
344 // Suppress access to non-existent
345 // "end-of-paragraph char"
347 eraseChar(pos, false);
358 void Paragraph::rejectChanges(BufferParams const & bparams,
359 pos_type start, pos_type end)
361 BOOST_ASSERT(start >= 0 && start <= size());
362 BOOST_ASSERT(end > start && end <= size() + 1);
364 for (pos_type pos = start; pos < end; ++pos) {
365 switch (lookupChange(pos).type) {
366 case Change::UNCHANGED:
367 // reject changes in nested inset
368 if (pos < size() && isInset(pos)) {
369 getInset(pos)->rejectChanges(bparams);
373 case Change::INSERTED:
374 // Suppress access to non-existent
375 // "end-of-paragraph char"
377 eraseChar(pos, false);
383 case Change::DELETED:
384 d->changes_.set(Change(Change::UNCHANGED), pos);
386 // Do NOT reject changes within a deleted inset!
387 // There may be insertions of a co-author inside of it!
395 Paragraph::value_type Paragraph::Private::getChar(pos_type pos) const
397 BOOST_ASSERT(pos >= 0 && pos <= size());
399 return owner_->getChar(pos);
403 void Paragraph::Private::insertChar(pos_type pos, value_type c,
404 Change const & change)
406 BOOST_ASSERT(pos >= 0 && pos <= size());
409 changes_.insert(change, pos);
411 // This is actually very common when parsing buffers (and
412 // maybe inserting ascii text)
414 // when appending characters, no need to update tables
415 owner_->text_.push_back(c);
419 owner_->text_.insert(owner_->text_.begin() + pos, c);
421 // Update the font table.
422 fontlist_.increasePosAfterPos(pos);
425 insetlist_.increasePosAfterPos(pos);
429 void Paragraph::insertInset(pos_type pos, Inset * inset,
430 Change const & change)
433 BOOST_ASSERT(pos >= 0 && pos <= size());
435 d->insertChar(pos, META_INSET, change);
436 BOOST_ASSERT(text_[pos] == META_INSET);
438 // Add a new entry in the insetlist_.
439 d->insetlist_.insert(inset, pos);
443 bool Paragraph::eraseChar(pos_type pos, bool trackChanges)
445 BOOST_ASSERT(pos >= 0 && pos <= size());
447 // keep the logic here in sync with the logic of isMergedOnEndOfParDeletion()
450 Change change = d->changes_.lookup(pos);
452 // set the character to DELETED if
453 // a) it was previously unchanged or
454 // b) it was inserted by a co-author
456 if (change.type == Change::UNCHANGED ||
457 (change.type == Change::INSERTED && change.author != 0)) {
458 setChange(pos, Change(Change::DELETED));
462 if (change.type == Change::DELETED)
466 // Don't physically access the imaginary end-of-paragraph character.
467 // eraseChar() can only mark it as DELETED. A physical deletion of
468 // end-of-par must be handled externally.
474 d->changes_.erase(pos);
476 // if it is an inset, delete the inset entry
477 if (text_[pos] == Paragraph::META_INSET)
478 d->insetlist_.erase(pos);
480 text_.erase(text_.begin() + pos);
482 // Update the fontlist_
483 d->fontlist_.erase(pos);
485 // Update the insetlist_
486 d->insetlist_.decreasePosAfterPos(pos);
492 int Paragraph::eraseChars(pos_type start, pos_type end, bool trackChanges)
494 BOOST_ASSERT(start >= 0 && start <= size());
495 BOOST_ASSERT(end >= start && end <= size() + 1);
498 for (pos_type count = end - start; count; --count) {
499 if (!eraseChar(i, trackChanges))
506 int Paragraph::Private::latexSurrogatePair(odocstream & os, value_type c,
507 value_type next, Encoding const & encoding)
509 // Writing next here may circumvent a possible font change between
510 // c and next. Since next is only output if it forms a surrogate pair
511 // with c we can ignore this:
512 // A font change inside a surrogate pair does not make sense and is
513 // hopefully impossible to input.
514 // FIXME: change tracking
515 // Is this correct WRT change tracking?
516 docstring const latex1 = encoding.latexChar(next);
517 docstring const latex2 = encoding.latexChar(c);
518 os << latex1 << '{' << latex2 << '}';
519 return latex1.length() + latex2.length() + 2;
523 bool Paragraph::Private::simpleTeXBlanks(Encoding const & encoding,
524 odocstream & os, TexRow & texrow,
526 unsigned int & column,
528 Layout const & style)
533 if (i + 1 < size()) {
534 char_type next = getChar(i + 1);
535 if (Encodings::isCombiningChar(next)) {
536 // This space has an accent, so we must always output it.
537 column += latexSurrogatePair(os, ' ', next, encoding) - 1;
542 if (lyxrc.plaintext_linelen > 0
543 && column > lyxrc.plaintext_linelen
545 && getChar(i - 1) != ' '
547 // same in FreeSpacing mode
548 && !owner_->isFreeSpacing()
549 // In typewriter mode, we want to avoid
550 // ! . ? : at the end of a line
551 && !(font.family() == Font::TYPEWRITER_FAMILY
552 && (getChar(i - 1) == '.'
553 || getChar(i - 1) == '?'
554 || getChar(i - 1) == ':'
555 || getChar(i - 1) == '!'))) {
558 texrow.start(owner_->id(), i + 1);
560 } else if (style.free_spacing) {
569 int Paragraph::Private::knownLangChars(odocstream & os,
572 Change & runningChange,
573 Encoding const & encoding,
576 // When the character is marked by the proper language, we simply
577 // get its code point in some encoding, otherwise we get the
578 // translation specified in the unicodesymbols file, which is
579 // something like "\textLANG{<spec>}". So, we have to retain
580 // "\textLANG{<spec>" for the first char but only "<spec>" for
581 // all subsequent chars.
582 docstring const latex1 = rtrim(encoding.latexChar(c), "}");
583 int length = latex1.length();
585 while (i + 1 < size()) {
586 char_type next = getChar(i + 1);
587 // Stop here if next character belongs to another
588 // language or there is a change tracking status.
589 if (!Encodings::isKnownLangChar(next, preamble) ||
590 runningChange != owner_->lookupChange(i + 1))
594 FontList::const_iterator cit = fontlist_.begin();
595 FontList::const_iterator end = fontlist_.end();
596 for (; cit != end; ++cit) {
597 if (cit->pos() >= i && !found) {
598 prev_font = cit->font();
601 if (cit->pos() >= i + 1)
604 // Stop here if there is a font attribute change.
605 if (found && cit != end && prev_font != cit->font())
607 docstring const latex = rtrim(encoding.latexChar(next), "}");
608 docstring::size_type const j =
609 latex.find_first_of(from_ascii("{"));
610 if (j == docstring::npos) {
612 length += latex.length();
614 os << latex.substr(j + 1);
615 length += latex.substr(j + 1).length();
619 // When the proper language is set, we are simply passed a code
620 // point, so we should not try to close the \textLANG command.
621 if (prefixIs(latex1, from_ascii("\\" + preamble))) {
629 bool Paragraph::Private::isTextAt(string const & str, pos_type pos) const
631 pos_type const len = str.length();
633 // is the paragraph large enough?
634 if (pos + len > size())
637 // does the wanted text start at point?
638 for (string::size_type i = 0; i < str.length(); ++i) {
639 // Caution: direct comparison of characters works only
640 // because str is pure ASCII.
641 if (str[i] != owner_->text_[pos + i])
645 return fontlist_.hasChangeInRange(pos, len);
649 void Paragraph::Private::latexInset(Buffer const & buf,
650 BufferParams const & bparams,
653 OutputParams & runparams,
656 Font const & outerfont,
658 Change & running_change,
659 Layout const & style,
661 unsigned int & column)
663 Inset * inset = owner_->getInset(i);
666 if (style.pass_thru) {
667 inset->plaintext(buf, os, runparams);
671 // FIXME: move this to InsetNewline::latex
672 if (inset->lyxCode() == NEWLINE_CODE) {
673 // newlines are handled differently here than
674 // the default in simpleTeXSpecialChars().
675 if (!style.newline_allowed) {
679 column += running_font.latexWriteEndChanges(
680 os, bparams, runparams,
685 if (running_font.family() == Font::TYPEWRITER_FAMILY)
688 basefont = owner_->getLayoutFont(bparams, outerfont);
689 running_font = basefont;
691 if (runparams.moving_arg)
697 texrow.start(owner_->id(), i + 1);
701 if (owner_->lookupChange(i).type == Change::DELETED) {
702 if( ++runparams.inDeletedInset == 1)
703 runparams.changeOfDeletedInset = owner_->lookupChange(i);
706 if (inset->canTrackChanges()) {
707 column += Changes::latexMarkChange(os, bparams, running_change,
708 Change(Change::UNCHANGED));
709 running_change = Change(Change::UNCHANGED);
713 odocstream::pos_type const len = os.tellp();
715 if ((inset->lyxCode() == GRAPHICS_CODE
716 || inset->lyxCode() == MATH_CODE
717 || inset->lyxCode() == HYPERLINK_CODE)
718 && running_font.isRightToLeft()) {
719 if (running_font.language()->lang() == "farsi")
726 // FIXME: Bug: we can have an empty font change here!
727 // if there has just been a font change, we are going to close it
728 // right now, which means stupid latex code like \textsf{}. AFAIK,
729 // this does not harm dvi output. A minor bug, thus (JMarc)
731 // Some insets cannot be inside a font change command.
732 // However, even such insets *can* be placed in \L or \R
733 // or their equivalents (for RTL language switches), so we don't
734 // close the language in those cases.
735 // ArabTeX, though, cannot handle this special behavior, it seems.
736 bool arabtex = basefont.language()->lang() == "arabic_arabtex"
737 || running_font.language()->lang() == "arabic_arabtex";
738 if (open_font && inset->noFontChange()) {
739 bool closeLanguage = arabtex
740 || basefont.isRightToLeft() == running_font.isRightToLeft();
741 unsigned int count = running_font.latexWriteEndChanges(os,
742 bparams, runparams, basefont, basefont, closeLanguage);
744 // if any font properties were closed, update the running_font,
745 // making sure, however, to leave the language as it was
747 // FIXME: probably a better way to keep track of the old
748 // language, than copying the entire font?
749 Font const copy_font(running_font);
750 basefont = owner_->getLayoutFont(bparams, outerfont);
751 running_font = basefont;
753 running_font.setLanguage(copy_font.language());
754 // leave font open if language is still open
755 open_font = (running_font.language() == basefont.language());
757 runparams.local_font = &basefont;
761 int tmp = inset->latex(buf, os, runparams);
764 if (running_font.language()->lang() == "farsi")
771 for (int j = 0; j < tmp; ++j)
774 texrow.start(owner_->id(), i + 1);
777 column += os.tellp() - len;
780 if (owner_->lookupChange(i).type == Change::DELETED)
781 --runparams.inDeletedInset;
785 void Paragraph::Private::latexSpecialChar(
787 OutputParams & runparams,
789 Change & running_change,
790 Layout const & style,
792 unsigned int & column)
794 value_type const c = getChar(i);
796 if (style.pass_thru) {
798 // FIXME UNICODE: This can fail if c cannot
799 // be encoded in the current encoding.
804 if (runparams.verbatim) {
809 if (lyxrc.fontenc == "T1" && latexSpecialT1(c, os, i, column))
812 if (running_font.family() == Font::TYPEWRITER_FAMILY
813 && latexSpecialTypewriter(c, os, i, column))
816 // Otherwise, we use what LaTeX provides us.
819 os << "\\textbackslash{}";
823 os << "\\textless{}";
827 os << "\\textgreater{}";
838 os << "\\char`\\\"{}";
843 case '%': case '#': case '{':
851 os << "\\textasciitilde{}";
856 os << "\\textasciicircum{}";
861 // avoid being mistaken for optional arguments
869 // Blanks are printed before font switching.
870 // Sure? I am not! (try nice-latex)
871 // I am sure it's correct. LyX might be smarter
872 // in the future, but for now, nothing wrong is
879 if (latexSpecialPhrase(os, i, column))
885 Encoding const & encoding = *(runparams.encoding);
886 if (i + 1 < size()) {
887 char_type next = getChar(i + 1);
888 if (Encodings::isCombiningChar(next)) {
889 column += latexSurrogatePair(os, c, next, encoding) - 1;
895 if (Encodings::isKnownLangChar(c, preamble)) {
896 column += knownLangChars(os, c, preamble, running_change,
900 docstring const latex = encoding.latexChar(c);
901 if (latex.length() > 1 && latex[latex.length() - 1] != '}') {
902 // Prevent eating of a following
903 // space or command corruption by
904 // following characters
905 column += latex.length() + 1;
908 column += latex.length() - 1;
916 bool Paragraph::Private::latexSpecialT1(char_type const c, odocstream & os,
917 pos_type & i, unsigned int & column)
923 // In T1 encoding, these characters exist
924 // but we should avoid ligatures
925 if (i + 1 > size() || getChar(i + 1) != c)
930 //os << "\\textcompwordmark{}";
942 bool Paragraph::Private::latexSpecialTypewriter(char_type const c, odocstream & os,
943 pos_type & i, unsigned int & column)
947 if (i + 1 < size() && getChar(i + 1) == '-') {
948 // "--" in Typewriter mode -> "-{}-"
955 // I assume this is hack treating typewriter as verbatim
956 // FIXME UNICODE: This can fail if c cannot be encoded
957 // in the current encoding.
962 // Those characters are not directly supported.
966 case '%': case '#': case '{':
975 // With Typewriter font, these characters exist.
982 bool Paragraph::Private::latexSpecialPhrase(odocstream & os, pos_type & i,
983 unsigned int & column)
985 // FIXME: if we have "LaTeX" with a font
986 // change in the middle (before the 'T', then
987 // the "TeX" part is still special cased.
988 // Really we should only operate this on
989 // "words" for some definition of word
991 for (size_t pnr = 0; pnr < phrases_nr; ++pnr) {
992 if (!isTextAt(special_phrases[pnr].phrase, i))
994 os << special_phrases[pnr].macro;
995 i += special_phrases[pnr].phrase.length() - 1;
996 column += special_phrases[pnr].macro.length() - 1;
1003 void Paragraph::Private::validate(LaTeXFeatures & features,
1004 Layout const & layout) const
1006 // check the params.
1007 if (!params_.spacing().isDefault())
1008 features.require("setspace");
1011 features.useLayout(layout.name());
1014 fontlist_.validate(features);
1016 // then the indentation
1017 if (!params_.leftIndent().zero())
1018 features.require("ParagraphLeftIndent");
1021 InsetList::const_iterator icit = insetlist_.begin();
1022 InsetList::const_iterator iend = insetlist_.end();
1023 for (; icit != iend; ++icit) {
1025 icit->inset->validate(features);
1026 if (layout.needprotect &&
1027 icit->inset->lyxCode() == FOOT_CODE)
1028 features.require("NeedLyXFootnoteCode");
1032 // then the contents
1033 for (pos_type i = 0; i < size() ; ++i) {
1034 for (size_t pnr = 0; pnr < phrases_nr; ++pnr) {
1035 if (!special_phrases[pnr].builtin
1036 && isTextAt(special_phrases[pnr].phrase, i)) {
1037 features.require(special_phrases[pnr].phrase);
1041 Encodings::validate(getChar(i), features);
1045 /////////////////////////////////////////////////////////////////////
1049 /////////////////////////////////////////////////////////////////////
1051 Paragraph::Paragraph()
1052 : d(new Paragraph::Private(this))
1060 Paragraph::Paragraph(Paragraph const & par)
1061 : itemdepth(par.itemdepth), text_(par.text_),
1062 d(new Paragraph::Private(*par.d, this))
1067 Paragraph & Paragraph::operator=(Paragraph const & par)
1069 // needed as we will destroy the private part before copying it
1071 itemdepth = par.itemdepth;
1075 d = new Private(*par.d, this);
1081 Paragraph::~Paragraph()
1087 void Paragraph::write(Buffer const & buf, ostream & os,
1088 BufferParams const & bparams,
1089 depth_type & dth) const
1091 // The beginning or end of a deeper (i.e. nested) area?
1092 if (dth != params().depth()) {
1093 if (params().depth() > dth) {
1094 while (params().depth() > dth) {
1095 os << "\n\\begin_deeper";
1099 while (params().depth() < dth) {
1100 os << "\n\\end_deeper";
1106 // First write the layout
1107 os << "\n\\begin_layout " << to_utf8(layout()->name()) << '\n';
1111 Font font1(Font::ALL_INHERIT, bparams.language);
1113 Change running_change = Change(Change::UNCHANGED);
1116 for (pos_type i = 0; i <= size(); ++i) {
1118 Change change = lookupChange(i);
1119 Changes::lyxMarkChange(os, column, running_change, change);
1120 running_change = change;
1125 // Write font changes
1126 Font font2 = getFontSettings(bparams, i);
1127 if (font2 != font1) {
1128 font2.lyxWriteChanges(font1, os);
1133 value_type const c = getChar(i);
1137 Inset const * inset = getInset(i);
1139 if (inset->directWrite()) {
1140 // international char, let it write
1141 // code directly so it's shorter in
1143 inset->write(buf, os);
1147 os << "\\begin_inset ";
1148 inset->write(buf, os);
1149 os << "\n\\end_inset\n\n";
1155 os << "\n\\backslash\n";
1159 if (i + 1 < size() && getChar(i + 1) == ' ') {
1166 if ((column > 70 && c == ' ')
1171 // this check is to amend a bug. LyX sometimes
1172 // inserts '\0' this could cause problems.
1174 std::vector<char> tmp = ucs4_to_utf8(c);
1175 tmp.push_back('\0');
1178 lyxerr << "ERROR (Paragraph::writeFile):"
1179 " NULL char in structure." << endl;
1185 os << "\n\\end_layout\n";
1189 void Paragraph::validate(LaTeXFeatures & features) const
1191 d->validate(features, *layout());
1195 void Paragraph::insert(pos_type start, docstring const & str,
1196 Font const & font, Change const & change)
1198 for (size_t i = 0, n = str.size(); i != n ; ++i)
1199 insertChar(start + i, str[i], font, change);
1203 void Paragraph::appendChar(value_type c, Font const & font,
1204 Change const & change)
1207 d->changes_.insert(change, text_.size());
1208 // when appending characters, no need to update tables
1210 setFont(text_.size() - 1, font);
1214 void Paragraph::appendString(docstring const & s, Font const & font,
1215 Change const & change)
1217 size_t end = s.size();
1218 size_t oldsize = text_.size();
1219 size_t newsize = oldsize + end;
1220 size_t capacity = text_.capacity();
1221 if (newsize >= capacity)
1222 text_.reserve(std::max(capacity + 100, newsize));
1224 // FIXME: Optimize this!
1225 for (pos_type i = 0; i != end; ++i) {
1227 d->changes_.insert(change, i);
1228 // when appending characters, no need to update tables
1229 text_.push_back(s[i]);
1231 d->fontlist_.setRange(oldsize, newsize, font);
1235 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c,
1238 d->insertChar(pos, c, Change(trackChanges ?
1239 Change::INSERTED : Change::UNCHANGED));
1243 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c,
1244 Font const & font, bool trackChanges)
1246 d->insertChar(pos, c, Change(trackChanges ?
1247 Change::INSERTED : Change::UNCHANGED));
1252 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c,
1253 Font const & font, Change const & change)
1255 d->insertChar(pos, c, change);
1260 void Paragraph::insertInset(pos_type pos, Inset * inset,
1261 Font const & font, Change const & change)
1263 insertInset(pos, inset, change);
1264 // Set the font/language of the inset...
1269 bool Paragraph::insetAllowed(InsetCode code)
1271 return !d->inset_owner_ || d->inset_owner_->insetAllowed(code);
1275 // Gets uninstantiated font setting at position.
1276 Font const Paragraph::getFontSettings(BufferParams const & bparams,
1280 lyxerr << " pos: " << pos << " size: " << size() << endl;
1281 BOOST_ASSERT(pos <= size());
1284 FontList::const_iterator cit = d->fontlist_.fontIterator(pos);
1285 if (cit != d->fontlist_.end())
1288 if (pos == size() && !empty())
1289 return getFontSettings(bparams, pos - 1);
1291 return Font(Font::ALL_INHERIT, getParLanguage(bparams));
1295 FontSpan Paragraph::fontSpan(pos_type pos) const
1297 BOOST_ASSERT(pos <= size());
1300 FontList::const_iterator cit = d->fontlist_.begin();
1301 FontList::const_iterator end = d->fontlist_.end();
1302 for (; cit != end; ++cit) {
1303 if (cit->pos() >= pos) {
1304 if (pos >= beginOfBody())
1305 return FontSpan(std::max(start, beginOfBody()),
1308 return FontSpan(start,
1309 std::min(beginOfBody() - 1,
1312 start = cit->pos() + 1;
1315 // This should not happen, but if so, we take no chances.
1316 //lyxerr << "Paragraph::getEndPosOfFontSpan: This should not happen!"
1318 return FontSpan(pos, pos);
1322 // Gets uninstantiated font setting at position 0
1323 Font const Paragraph::getFirstFontSettings(BufferParams const & bparams) const
1325 if (!empty() && !d->fontlist_.empty())
1326 return d->fontlist_.begin()->font();
1328 return Font(Font::ALL_INHERIT, bparams.language);
1332 // Gets the fully instantiated font at a given position in a paragraph
1333 // This is basically the same function as Text::GetFont() in text2.cpp.
1334 // The difference is that this one is used for generating the LaTeX file,
1335 // and thus cosmetic "improvements" are disallowed: This has to deliver
1336 // the true picture of the buffer. (Asger)
1337 Font const Paragraph::getFont(BufferParams const & bparams, pos_type pos,
1338 Font const & outerfont) const
1340 BOOST_ASSERT(pos >= 0);
1342 Font font = getFontSettings(bparams, pos);
1344 pos_type const body_pos = beginOfBody();
1346 font.realize(d->layout_->labelfont);
1348 font.realize(d->layout_->font);
1350 font.realize(outerfont);
1351 font.realize(bparams.getFont());
1357 Font const Paragraph::getLabelFont
1358 (BufferParams const & bparams, Font const & outerfont) const
1360 Font tmpfont = layout()->labelfont;
1361 tmpfont.setLanguage(getParLanguage(bparams));
1362 tmpfont.realize(outerfont);
1363 tmpfont.realize(bparams.getFont());
1368 Font const Paragraph::getLayoutFont
1369 (BufferParams const & bparams, Font const & outerfont) const
1371 Font tmpfont = layout()->font;
1372 tmpfont.setLanguage(getParLanguage(bparams));
1373 tmpfont.realize(outerfont);
1374 tmpfont.realize(bparams.getFont());
1379 /// Returns the height of the highest font in range
1380 Font_size Paragraph::highestFontInRange
1381 (pos_type startpos, pos_type endpos, Font_size def_size) const
1383 return d->fontlist_.highestInRange(startpos, endpos, def_size);
1387 Paragraph::value_type
1388 Paragraph::getUChar(BufferParams const & bparams, pos_type pos) const
1390 value_type c = getChar(pos);
1391 if (!lyxrc.rtl_support)
1421 if (uc != c && getFontSettings(bparams, pos).isRightToLeft())
1428 void Paragraph::setFont(pos_type pos, Font const & font)
1430 BOOST_ASSERT(pos <= size());
1432 // First, reduce font against layout/label font
1433 // Update: The setCharFont() routine in text2.cpp already
1434 // reduces font, so we don't need to do that here. (Asger)
1436 d->fontlist_.set(pos, font);
1440 void Paragraph::makeSameLayout(Paragraph const & par)
1442 layout(par.layout());
1444 d->params_ = par.params();
1448 bool Paragraph::stripLeadingSpaces(bool trackChanges)
1450 if (isFreeSpacing())
1456 while (pos < size() && (isNewline(pos) || isLineSeparator(pos))) {
1457 if (eraseChar(pos, trackChanges))
1463 return count > 0 || pos > 0;
1467 bool Paragraph::hasSameLayout(Paragraph const & par) const
1469 return par.layout() == layout() && d->params_.sameLayout(par.params());
1473 depth_type Paragraph::getDepth() const
1475 return params().depth();
1479 depth_type Paragraph::getMaxDepthAfter() const
1481 if (layout()->isEnvironment())
1482 return params().depth() + 1;
1484 return params().depth();
1488 char Paragraph::getAlign() const
1490 if (params().align() == LYX_ALIGN_LAYOUT)
1491 return layout()->align;
1493 return params().align();
1497 docstring const & Paragraph::getLabelstring() const
1499 return params().labelString();
1503 // the next two functions are for the manual labels
1504 docstring const Paragraph::getLabelWidthString() const
1506 if (layout()->margintype == MARGIN_MANUAL)
1507 return params().labelWidthString();
1509 return _("Senseless with this layout!");
1513 void Paragraph::setLabelWidthString(docstring const & s)
1515 params().labelWidthString(s);
1519 docstring const Paragraph::translateIfPossible(docstring const & s,
1520 BufferParams const & bparams) const
1522 if (!support::isAscii(s) || s.empty()) {
1523 // This must be a user defined layout. We cannot translate
1524 // this, since gettext accepts only ascii keys.
1527 // Probably standard layout, try to translate
1528 Messages & m = getMessages(getParLanguage(bparams)->code());
1529 return m.get(to_ascii(s));
1533 docstring Paragraph::expandLabel(LayoutPtr const & layout,
1534 BufferParams const & bparams, bool process_appendix) const
1536 TextClass const & tclass = bparams.getTextClass();
1539 if (process_appendix && params().appendix())
1540 fmt = translateIfPossible(layout->labelstring_appendix(),
1543 fmt = translateIfPossible(layout->labelstring(), bparams);
1545 if (fmt.empty() && layout->labeltype == LABEL_COUNTER
1546 && !layout->counter.empty())
1547 fmt = "\\the" + layout->counter;
1549 // handle 'inherited level parts' in 'fmt',
1550 // i.e. the stuff between '@' in '@Section@.\arabic{subsection}'
1551 size_t const i = fmt.find('@', 0);
1552 if (i != docstring::npos) {
1553 size_t const j = fmt.find('@', i + 1);
1554 if (j != docstring::npos) {
1555 docstring parent(fmt, i + 1, j - i - 1);
1556 docstring label = from_ascii("??");
1557 if (tclass.hasLayout(parent))
1558 docstring label = expandLabel(tclass[parent], bparams,
1560 fmt = docstring(fmt, 0, i) + label
1561 + docstring(fmt, j + 1, docstring::npos);
1565 return tclass.counters().counterLabel(fmt);
1569 void Paragraph::applyLayout(LayoutPtr const & new_layout)
1572 LyXAlignment const oldAlign = params().align();
1574 if (!(oldAlign & layout()->alignpossible)) {
1575 frontend::Alert::warning(_("Alignment not permitted"),
1576 _("The new layout does not permit the alignment previously used.\nSetting to default."));
1577 params().align(LYX_ALIGN_LAYOUT);
1582 pos_type Paragraph::beginOfBody() const
1584 return d->begin_of_body_;
1588 void Paragraph::setBeginOfBody()
1590 if (layout()->labeltype != LABEL_MANUAL) {
1591 d->begin_of_body_ = 0;
1595 // Unroll the first two cycles of the loop
1596 // and remember the previous character to
1597 // remove unnecessary getChar() calls
1599 pos_type end = size();
1600 if (i < end && !isNewline(i)) {
1602 char_type previous_char = 0;
1605 previous_char = text_[i];
1606 if (!isNewline(i)) {
1608 while (i < end && previous_char != ' ') {
1613 previous_char = temp;
1619 d->begin_of_body_ = i;
1623 InsetBibitem * Paragraph::bibitem() const
1625 if (!d->insetlist_.empty()) {
1626 Inset * inset = d->insetlist_.begin()->inset;
1627 if (inset->lyxCode() == BIBITEM_CODE)
1628 return static_cast<InsetBibitem *>(inset);
1634 bool Paragraph::forceDefaultParagraphs() const
1636 return inInset() && inInset()->forceDefaultParagraphs(0);
1642 // paragraphs inside floats need different alignment tags to avoid
1645 bool noTrivlistCentering(InsetCode code)
1647 return code == FLOAT_CODE || code == WRAP_CODE;
1651 string correction(string const & orig)
1653 if (orig == "flushleft")
1654 return "raggedright";
1655 if (orig == "flushright")
1656 return "raggedleft";
1657 if (orig == "center")
1663 string const corrected_env(string const & suffix, string const & env,
1666 string output = suffix + "{";
1667 if (noTrivlistCentering(code))
1668 output += correction(env);
1672 if (suffix == "\\begin")
1678 void adjust_row_column(string const & str, TexRow & texrow, int & column)
1680 if (!contains(str, "\n"))
1681 column += str.size();
1685 column = rsplit(str, tmp, '\n').size();
1692 int Paragraph::Private::startTeXParParams(BufferParams const & bparams,
1693 odocstream & os, TexRow & texrow,
1694 bool moving_arg) const
1698 if (params_.noindent()) {
1699 os << "\\noindent ";
1703 LyXAlignment const curAlign = params_.align();
1705 if (curAlign == layout_->align)
1709 case LYX_ALIGN_NONE:
1710 case LYX_ALIGN_BLOCK:
1711 case LYX_ALIGN_LAYOUT:
1712 case LYX_ALIGN_SPECIAL:
1714 case LYX_ALIGN_LEFT:
1715 case LYX_ALIGN_RIGHT:
1716 case LYX_ALIGN_CENTER:
1725 case LYX_ALIGN_NONE:
1726 case LYX_ALIGN_BLOCK:
1727 case LYX_ALIGN_LAYOUT:
1728 case LYX_ALIGN_SPECIAL:
1730 case LYX_ALIGN_LEFT: {
1732 if (owner_->getParLanguage(bparams)->babel() != "hebrew")
1733 output = corrected_env("\\begin", "flushleft", owner_->ownerCode());
1735 output = corrected_env("\\begin", "flushright", owner_->ownerCode());
1736 os << from_ascii(output);
1737 adjust_row_column(output, texrow, column);
1739 } case LYX_ALIGN_RIGHT: {
1741 if (owner_->getParLanguage(bparams)->babel() != "hebrew")
1742 output = corrected_env("\\begin", "flushright", owner_->ownerCode());
1744 output = corrected_env("\\begin", "flushleft", owner_->ownerCode());
1745 os << from_ascii(output);
1746 adjust_row_column(output, texrow, column);
1748 } case LYX_ALIGN_CENTER: {
1750 output = corrected_env("\\begin", "center", owner_->ownerCode());
1751 os << from_ascii(output);
1752 adjust_row_column(output, texrow, column);
1761 int Paragraph::Private::endTeXParParams(BufferParams const & bparams,
1762 odocstream & os, TexRow & texrow,
1763 bool moving_arg) const
1767 switch (params_.align()) {
1768 case LYX_ALIGN_NONE:
1769 case LYX_ALIGN_BLOCK:
1770 case LYX_ALIGN_LAYOUT:
1771 case LYX_ALIGN_SPECIAL:
1773 case LYX_ALIGN_LEFT:
1774 case LYX_ALIGN_RIGHT:
1775 case LYX_ALIGN_CENTER:
1783 switch (params_.align()) {
1784 case LYX_ALIGN_NONE:
1785 case LYX_ALIGN_BLOCK:
1786 case LYX_ALIGN_LAYOUT:
1787 case LYX_ALIGN_SPECIAL:
1789 case LYX_ALIGN_LEFT: {
1791 if (owner_->getParLanguage(bparams)->babel() != "hebrew")
1792 output = corrected_env("\n\\par\\end", "flushleft", owner_->ownerCode());
1794 output = corrected_env("\n\\par\\end", "flushright", owner_->ownerCode());
1795 os << from_ascii(output);
1796 adjust_row_column(output, texrow, column);
1798 } case LYX_ALIGN_RIGHT: {
1800 if (owner_->getParLanguage(bparams)->babel() != "hebrew")
1801 output = corrected_env("\n\\par\\end", "flushright", owner_->ownerCode());
1803 output = corrected_env("\n\\par\\end", "flushleft", owner_->ownerCode());
1804 os << from_ascii(output);
1805 adjust_row_column(output, texrow, column);
1807 } case LYX_ALIGN_CENTER: {
1809 output = corrected_env("\n\\par\\end", "center", owner_->ownerCode());
1810 os << from_ascii(output);
1811 adjust_row_column(output, texrow, column);
1820 // This one spits out the text of the paragraph
1821 bool Paragraph::latex(Buffer const & buf,
1822 BufferParams const & bparams,
1823 Font const & outerfont,
1824 odocstream & os, TexRow & texrow,
1825 OutputParams const & runparams) const
1827 LYXERR(Debug::LATEX) << "SimpleTeXOnePar... " << this << endl;
1829 bool return_value = false;
1833 // well we have to check if we are in an inset with unlimited
1834 // length (all in one row) if that is true then we don't allow
1835 // any special options in the paragraph and also we don't allow
1836 // any environment other than the default layout of the text class
1838 bool asdefault = forceDefaultParagraphs();
1841 style = bparams.getTextClass().defaultLayout();
1846 // Current base font for all inherited font changes, without any
1847 // change caused by an individual character, except for the language:
1848 // It is set to the language of the first character.
1849 // As long as we are in the label, this font is the base font of the
1850 // label. Before the first body character it is set to the base font
1854 // Maybe we have to create a optional argument.
1855 pos_type body_pos = beginOfBody();
1856 unsigned int column = 0;
1859 // the optional argument is kept in curly brackets in
1860 // case it contains a ']'
1863 basefont = getLabelFont(bparams, outerfont);
1865 basefont = getLayoutFont(bparams, outerfont);
1868 // Which font is currently active?
1869 Font running_font(basefont);
1870 // Do we have an open font change?
1871 bool open_font = false;
1873 Change runningChange = Change(Change::UNCHANGED);
1875 texrow.start(id(), 0);
1877 // if the paragraph is empty, the loop will not be entered at all
1879 if (style->isCommand()) {
1884 column += d->startTeXParParams(bparams, os, texrow,
1885 runparams.moving_arg);
1888 for (pos_type i = 0; i < size(); ++i) {
1889 // First char in paragraph or after label?
1890 if (i == body_pos) {
1893 column += running_font.latexWriteEndChanges(
1894 os, bparams, runparams,
1895 basefont, basefont);
1898 basefont = getLayoutFont(bparams, outerfont);
1899 running_font = basefont;
1901 column += Changes::latexMarkChange(os, bparams,
1902 runningChange, Change(Change::UNCHANGED));
1903 runningChange = Change(Change::UNCHANGED);
1908 if (style->isCommand()) {
1914 column += d->startTeXParParams(bparams, os,
1916 runparams.moving_arg);
1919 Change const & change = runparams.inDeletedInset ? runparams.changeOfDeletedInset
1922 if (bparams.outputChanges && runningChange != change) {
1924 column += running_font.latexWriteEndChanges(
1925 os, bparams, runparams, basefont, basefont);
1928 basefont = getLayoutFont(bparams, outerfont);
1929 running_font = basefont;
1931 column += Changes::latexMarkChange(os, bparams, runningChange, change);
1932 runningChange = change;
1935 // do not output text which is marked deleted
1936 // if change tracking output is disabled
1937 if (!bparams.outputChanges && change.type == Change::DELETED) {
1943 // Fully instantiated font
1944 Font const font = getFont(bparams, i, outerfont);
1946 Font const last_font = running_font;
1948 // Do we need to close the previous font?
1950 (font != running_font ||
1951 font.language() != running_font.language()))
1953 column += running_font.latexWriteEndChanges(
1954 os, bparams, runparams, basefont,
1955 (i == body_pos-1) ? basefont : font);
1956 running_font = basefont;
1960 // Switch file encoding if necessary
1961 if (runparams.encoding->package() == Encoding::inputenc &&
1962 font.language()->encoding()->package() == Encoding::inputenc) {
1963 std::pair<bool, int> const enc_switch = switchEncoding(os, bparams,
1964 runparams.moving_arg, *(runparams.encoding),
1965 *(font.language()->encoding()));
1966 if (enc_switch.first) {
1967 column += enc_switch.second;
1968 runparams.encoding = font.language()->encoding();
1972 value_type const c = getChar(i);
1974 // Do we need to change font?
1975 if ((font != running_font ||
1976 font.language() != running_font.language()) &&
1979 odocstringstream ods;
1980 column += font.latexWriteStartChanges(ods, bparams,
1981 runparams, basefont,
1983 running_font = font;
1985 docstring fontchange = ods.str();
1986 // check if the fontchange ends with a trailing blank
1987 // (like "\small " (see bug 3382)
1988 if (suffixIs(fontchange, ' ') && c == ' ')
1989 os << fontchange.substr(0, fontchange.size() - 1)
1990 << from_ascii("{}");
1996 // FIXME: integrate this case in latexSpecialChar
1997 // Do not print the separation of the optional argument
1998 // if style->pass_thru is false. This works because
1999 // latexSpecialChar ignores spaces if
2000 // style->pass_thru is false.
2001 if (i != body_pos - 1) {
2002 if (d->simpleTeXBlanks(
2003 *(runparams.encoding), os, texrow,
2004 i, column, font, *style)) {
2005 // A surrogate pair was output. We
2006 // must not call latexSpecialChar
2007 // in this iteration, since it would output
2008 // the combining character again.
2015 OutputParams rp = runparams;
2016 rp.free_spacing = style->free_spacing;
2017 rp.local_font = &font;
2018 rp.intitle = style->intitle;
2020 // Two major modes: LaTeX or plain
2021 // Handle here those cases common to both modes
2022 // and then split to handle the two modes separately.
2023 if (c == Paragraph::META_INSET)
2024 d->latexInset(buf, bparams, os,
2025 texrow, rp, running_font,
2026 basefont, outerfont, open_font,
2027 runningChange, *style, i, column);
2029 d->latexSpecialChar(os, rp, running_font, runningChange,
2032 // Set the encoding to that returned from simpleTeXSpecialChars (see
2033 // comment for encoding member in OutputParams.h)
2034 runparams.encoding = rp.encoding;
2037 // If we have an open font definition, we have to close it
2039 #ifdef FIXED_LANGUAGE_END_DETECTION
2042 .latexWriteEndChanges(os, bparams, runparams,
2044 next_->getFont(bparams, 0, outerfont));
2046 running_font.latexWriteEndChanges(os, bparams,
2047 runparams, basefont, basefont);
2050 //FIXME: For now we ALWAYS have to close the foreign font settings if they are
2051 //FIXME: there as we start another \selectlanguage with the next paragraph if
2052 //FIXME: we are in need of this. This should be fixed sometime (Jug)
2053 running_font.latexWriteEndChanges(os, bparams, runparams,
2054 basefont, basefont);
2058 column += Changes::latexMarkChange(os, bparams, runningChange, Change(Change::UNCHANGED));
2060 // Needed if there is an optional argument but no contents.
2061 if (body_pos > 0 && body_pos == size()) {
2063 return_value = false;
2067 column += d->endTeXParParams(bparams, os, texrow,
2068 runparams.moving_arg);
2071 LYXERR(Debug::LATEX) << "SimpleTeXOnePar...done " << this << endl;
2072 return return_value;
2089 string tag_name(PAR_TAG const & pt) {
2091 case PAR_NONE: return "!-- --";
2092 case TT: return "tt";
2093 case SF: return "sf";
2094 case BF: return "bf";
2095 case IT: return "it";
2096 case SL: return "sl";
2097 case EM: return "em";
2104 void operator|=(PAR_TAG & p1, PAR_TAG const & p2)
2106 p1 = static_cast<PAR_TAG>(p1 | p2);
2111 void reset(PAR_TAG & p1, PAR_TAG const & p2)
2113 p1 = static_cast<PAR_TAG>(p1 & ~p2);
2119 bool Paragraph::emptyTag() const
2121 for (pos_type i = 0; i < size(); ++i) {
2123 Inset const * inset = getInset(i);
2124 InsetCode lyx_code = inset->lyxCode();
2125 if (lyx_code != TOC_CODE &&
2126 lyx_code != INCLUDE_CODE &&
2127 lyx_code != GRAPHICS_CODE &&
2128 lyx_code != ERT_CODE &&
2129 lyx_code != LISTINGS_CODE &&
2130 lyx_code != FLOAT_CODE &&
2131 lyx_code != TABULAR_CODE) {
2135 value_type c = getChar(i);
2136 if (c != ' ' && c != '\t')
2144 string Paragraph::getID(Buffer const & buf, OutputParams const & runparams) const
2146 for (pos_type i = 0; i < size(); ++i) {
2148 Inset const * inset = getInset(i);
2149 InsetCode lyx_code = inset->lyxCode();
2150 if (lyx_code == LABEL_CODE) {
2151 string const id = static_cast<InsetCommand const *>(inset)->getContents();
2152 return "id='" + to_utf8(sgml::cleanID(buf, runparams, from_utf8(id))) + "'";
2161 pos_type Paragraph::getFirstWord(Buffer const & buf, odocstream & os, OutputParams const & runparams) const
2164 for (i = 0; i < size(); ++i) {
2166 Inset const * inset = getInset(i);
2167 inset->docbook(buf, os, runparams);
2169 value_type c = getChar(i);
2172 os << sgml::escapeChar(c);
2179 bool Paragraph::onlyText(Buffer const & buf, Font const & outerfont, pos_type initial) const
2183 for (pos_type i = initial; i < size(); ++i) {
2184 Font font = getFont(buf.params(), i, outerfont);
2187 if (i != initial && font != font_old)
2196 void Paragraph::simpleDocBookOnePar(Buffer const & buf,
2198 OutputParams const & runparams,
2199 Font const & outerfont,
2200 pos_type initial) const
2202 bool emph_flag = false;
2204 LayoutPtr const & style = layout();
2206 style->labeltype == LABEL_MANUAL ? style->labelfont : style->font;
2208 if (style->pass_thru && !onlyText(buf, outerfont, initial))
2211 // parsing main loop
2212 for (pos_type i = initial; i < size(); ++i) {
2213 Font font = getFont(buf.params(), i, outerfont);
2215 // handle <emphasis> tag
2216 if (font_old.emph() != font.emph()) {
2217 if (font.emph() == Font::ON) {
2220 } else if (i != initial) {
2221 os << "</emphasis>";
2227 Inset const * inset = getInset(i);
2228 inset->docbook(buf, os, runparams);
2230 value_type c = getChar(i);
2232 if (style->pass_thru)
2235 os << sgml::escapeChar(c);
2241 os << "</emphasis>";
2244 if (style->free_spacing)
2246 if (style->pass_thru && !onlyText(buf, outerfont, initial))
2251 bool Paragraph::isHfill(pos_type pos) const
2254 && getInset(pos)->lyxCode() == HFILL_CODE;
2258 bool Paragraph::isNewline(pos_type pos) const
2261 && getInset(pos)->lyxCode() == NEWLINE_CODE;
2265 bool Paragraph::isLineSeparator(pos_type pos) const
2267 value_type const c = getChar(pos);
2268 return isLineSeparatorChar(c)
2269 || (c == Paragraph::META_INSET && getInset(pos) &&
2270 getInset(pos)->isLineSeparator());
2274 /// Used by the spellchecker
2275 bool Paragraph::isLetter(pos_type pos) const
2278 return getInset(pos)->isLetter();
2280 value_type const c = getChar(pos);
2281 return isLetterChar(c) || isDigit(c);
2287 Paragraph::getParLanguage(BufferParams const & bparams) const
2290 return getFirstFontSettings(bparams).language();
2291 // FIXME: we should check the prev par as well (Lgb)
2292 return bparams.language;
2296 bool Paragraph::isRTL(BufferParams const & bparams) const
2298 return lyxrc.rtl_support
2299 && getParLanguage(bparams)->rightToLeft()
2300 && ownerCode() != ERT_CODE
2301 && ownerCode() != LISTINGS_CODE;
2305 void Paragraph::changeLanguage(BufferParams const & bparams,
2306 Language const * from, Language const * to)
2308 // change language including dummy font change at the end
2309 for (pos_type i = 0; i <= size(); ++i) {
2310 Font font = getFontSettings(bparams, i);
2311 if (font.language() == from) {
2312 font.setLanguage(to);
2319 bool Paragraph::isMultiLingual(BufferParams const & bparams) const
2321 Language const * doc_language = bparams.language;
2322 FontList::const_iterator cit = d->fontlist_.begin();
2323 FontList::const_iterator end = d->fontlist_.end();
2325 for (; cit != end; ++cit)
2326 if (cit->font().language() != ignore_language &&
2327 cit->font().language() != latex_language &&
2328 cit->font().language() != doc_language)
2334 // Convert the paragraph to a string.
2335 // Used for building the table of contents
2336 docstring const Paragraph::asString(Buffer const & buffer, bool label) const
2338 return asString(buffer, 0, size(), label);
2342 docstring const Paragraph::asString(Buffer const & buffer,
2343 pos_type beg, pos_type end, bool label) const
2346 odocstringstream os;
2348 if (beg == 0 && label && !params().labelString().empty())
2349 os << params().labelString() << ' ';
2351 for (pos_type i = beg; i < end; ++i) {
2352 value_type const c = getChar(i);
2355 else if (c == META_INSET)
2356 getInset(i)->textString(buffer, os);
2363 void Paragraph::setInsetOwner(Inset * inset)
2365 d->inset_owner_ = inset;
2369 int Paragraph::id() const
2375 LayoutPtr const & Paragraph::layout() const
2381 void Paragraph::layout(LayoutPtr const & new_layout)
2383 d->layout_ = new_layout;
2387 Inset * Paragraph::inInset() const
2389 return d->inset_owner_;
2393 InsetCode Paragraph::ownerCode() const
2395 return d->inset_owner_ ?
2396 d->inset_owner_->lyxCode() : NO_CODE;
2400 ParagraphParameters & Paragraph::params()
2406 ParagraphParameters const & Paragraph::params() const
2412 bool Paragraph::isFreeSpacing() const
2414 if (layout()->free_spacing)
2417 // for now we just need this, later should we need this in some
2418 // other way we can always add a function to Inset too.
2419 return ownerCode() == ERT_CODE || ownerCode() == LISTINGS_CODE;
2423 bool Paragraph::allowEmpty() const
2425 if (layout()->keepempty)
2427 return ownerCode() == ERT_CODE || ownerCode() == LISTINGS_CODE;
2431 char_type Paragraph::transformChar(char_type c, pos_type pos) const
2433 if (!Encodings::is_arabic(c))
2436 value_type prev_char = ' ';
2437 value_type next_char = ' ';
2439 for (pos_type i = pos - 1; i >= 0; --i) {
2440 value_type const par_char = getChar(i);
2441 if (!Encodings::isComposeChar_arabic(par_char)) {
2442 prev_char = par_char;
2447 for (pos_type i = pos + 1, end = size(); i < end; ++i) {
2448 value_type const par_char = getChar(i);
2449 if (!Encodings::isComposeChar_arabic(par_char)) {
2450 next_char = par_char;
2455 if (Encodings::is_arabic(next_char)) {
2456 if (Encodings::is_arabic(prev_char) &&
2457 !Encodings::is_arabic_special(prev_char))
2458 return Encodings::transformChar(c, Encodings::FORM_MEDIAL);
2460 return Encodings::transformChar(c, Encodings::FORM_INITIAL);
2462 if (Encodings::is_arabic(prev_char) &&
2463 !Encodings::is_arabic_special(prev_char))
2464 return Encodings::transformChar(c, Encodings::FORM_FINAL);
2466 return Encodings::transformChar(c, Encodings::FORM_ISOLATED);
2471 int Paragraph::checkBiblio(bool track_changes)
2474 //This is getting more and more a mess. ...We really should clean
2475 //up this bibitem issue for 1.6. See also bug 2743.
2477 // Add bibitem insets if necessary
2478 if (layout()->labeltype != LABEL_BIBLIO)
2481 bool hasbibitem = !d->insetlist_.empty()
2482 // Insist on it being in pos 0
2483 && getChar(0) == Paragraph::META_INSET
2484 && d->insetlist_.begin()->inset->lyxCode() == BIBITEM_CODE;
2489 // remove a bibitem in pos != 0
2490 // restore it later in pos 0 if necessary
2491 // (e.g. if a user inserts contents _before_ the item)
2492 // we're assuming there's only one of these, which there
2494 int erasedInsetPosition = -1;
2495 InsetList::iterator it = d->insetlist_.begin();
2496 InsetList::iterator end = d->insetlist_.end();
2497 for (; it != end; ++it)
2498 if (it->inset->lyxCode() == BIBITEM_CODE
2500 InsetBibitem * olditem = static_cast<InsetBibitem *>(it->inset);
2501 oldkey = olditem->getParam("key");
2502 oldlabel = olditem->getParam("label");
2503 erasedInsetPosition = it->pos;
2504 eraseChar(erasedInsetPosition, track_changes);
2508 //There was an InsetBibitem at the beginning, and we didn't
2509 //have to erase one.
2510 if (hasbibitem && erasedInsetPosition < 0)
2513 //There was an InsetBibitem at the beginning and we did have to
2514 //erase one. So we give its properties to the beginning inset.
2516 InsetBibitem * inset =
2517 static_cast<InsetBibitem *>(d->insetlist_.begin()->inset);
2518 if (!oldkey.empty())
2519 inset->setParam("key", oldkey);
2520 inset->setParam("label", oldlabel);
2521 return -erasedInsetPosition;
2524 //There was no inset at the beginning, so we need to create one with
2525 //the key and label of the one we erased.
2526 InsetBibitem * inset(new InsetBibitem(InsetCommandParams(BIBITEM_CODE)));
2527 // restore values of previously deleted item in this par.
2528 if (!oldkey.empty())
2529 inset->setParam("key", oldkey);
2530 inset->setParam("label", oldlabel);
2531 insertInset(0, static_cast<Inset *>(inset),
2532 Change(track_changes ? Change::INSERTED : Change::UNCHANGED));
2538 void Paragraph::checkAuthors(AuthorList const & authorList)
2540 d->changes_.checkAuthors(authorList);
2544 bool Paragraph::isUnchanged(pos_type pos) const
2546 return lookupChange(pos).type == Change::UNCHANGED;
2550 bool Paragraph::isInserted(pos_type pos) const
2552 return lookupChange(pos).type == Change::INSERTED;
2556 bool Paragraph::isDeleted(pos_type pos) const
2558 return lookupChange(pos).type == Change::DELETED;
2562 InsetList const & Paragraph::insetList() const
2564 return d->insetlist_;
2568 Inset * Paragraph::releaseInset(pos_type pos)
2570 Inset * inset = d->insetlist_.release(pos);
2571 /// does not honour change tracking!
2572 eraseChar(pos, false);
2577 Inset * Paragraph::getInset(pos_type pos)
2579 return d->insetlist_.get(pos);
2583 Inset const * Paragraph::getInset(pos_type pos) const
2585 return d->insetlist_.get(pos);
2589 int Paragraph::numberOfOptArgs() const
2592 InsetList::const_iterator it = insetList().begin();
2593 InsetList::const_iterator end = insetList().end();
2594 for (; it != end ; ++it) {
2595 if (it->inset->lyxCode() == OPTARG_CODE)