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 void latexInset(Buffer const &, BufferParams const &,
115 TexRow & texrow, OutputParams &,
118 Font const & outerfont,
120 Change & running_change,
121 Layout const & style,
123 unsigned int & column);
126 void latexSpecialChar(
128 OutputParams & runparams,
130 Change & running_change,
131 Layout const & style,
133 unsigned int & column);
140 unsigned int & column);
142 bool latexSpecialTypewriter(
146 unsigned int & column);
148 bool latexSpecialPhrase(
151 unsigned int & column);
154 void validate(LaTeXFeatures & features,
155 Layout const & layout) const;
158 pos_type size() const { return owner_->size(); }
160 /// match a string against a particular point in the paragraph
161 bool isTextAt(std::string const & str, pos_type pos) const;
163 /// Which Paragraph owns us?
167 Inset * inset_owner_;
175 static unsigned int paragraph_id;
177 ParagraphParameters params_;
179 /// for recording and looking up changes
183 InsetList insetlist_;
190 using std::upper_bound;
191 using std::lower_bound;
195 // Initialization of the counter for the paragraph id's,
196 unsigned int Paragraph::Private::paragraph_id = 0;
200 struct special_phrase {
206 special_phrase const special_phrases[] = {
207 { "LyX", from_ascii("\\protect\\LyX{}"), false },
208 { "TeX", from_ascii("\\protect\\TeX{}"), true },
209 { "LaTeX2e", from_ascii("\\protect\\LaTeXe{}"), true },
210 { "LaTeX", from_ascii("\\protect\\LaTeX{}"), true },
213 size_t const phrases_nr = sizeof(special_phrases)/sizeof(special_phrase);
218 Paragraph::Private::Private(Paragraph * owner)
222 id_ = paragraph_id++;
226 Paragraph::Private::Private(Private const & p, Paragraph * owner)
227 : owner_(owner), inset_owner_(p.inset_owner_), fontlist_(p.fontlist_),
228 params_(p.params_), changes_(p.changes_), insetlist_(p.insetlist_)
230 id_ = paragraph_id++;
234 bool Paragraph::isChanged(pos_type start, pos_type end) const
236 BOOST_ASSERT(start >= 0 && start <= size());
237 BOOST_ASSERT(end > start && end <= size() + 1);
239 return d->changes_.isChanged(start, end);
243 bool Paragraph::isMergedOnEndOfParDeletion(bool trackChanges) const {
244 // keep the logic here in sync with the logic of eraseChars()
250 Change change = d->changes_.lookup(size());
252 return change.type == Change::INSERTED && change.author == 0;
256 void Paragraph::setChange(Change const & change)
258 // beware of the imaginary end-of-par character!
259 d->changes_.set(change, 0, size() + 1);
262 * Propagate the change recursively - but not in case of DELETED!
264 * Imagine that your co-author makes changes in an existing inset. He
265 * sends your document to you and you come to the conclusion that the
266 * inset should go completely. If you erase it, LyX must not delete all
267 * text within the inset. Otherwise, the change tracked insertions of
268 * your co-author get lost and there is no way to restore them later.
270 * Conclusion: An inset's content should remain untouched if you delete it
273 if (change.type != Change::DELETED) {
274 for (pos_type pos = 0; pos < size(); ++pos) {
276 getInset(pos)->setChange(change);
282 void Paragraph::setChange(pos_type pos, Change const & change)
284 BOOST_ASSERT(pos >= 0 && pos <= size());
286 d->changes_.set(change, pos);
288 // see comment in setChange(Change const &) above
290 if (change.type != Change::DELETED &&
291 pos < size() && isInset(pos)) {
292 getInset(pos)->setChange(change);
297 Change const & Paragraph::lookupChange(pos_type pos) const
299 BOOST_ASSERT(pos >= 0 && pos <= size());
301 return d->changes_.lookup(pos);
305 void Paragraph::acceptChanges(BufferParams const & bparams, pos_type start,
308 BOOST_ASSERT(start >= 0 && start <= size());
309 BOOST_ASSERT(end > start && end <= size() + 1);
311 for (pos_type pos = start; pos < end; ++pos) {
312 switch (lookupChange(pos).type) {
313 case Change::UNCHANGED:
314 // accept changes in nested inset
315 if (pos < size() && isInset(pos))
316 getInset(pos)->acceptChanges(bparams);
320 case Change::INSERTED:
321 d->changes_.set(Change(Change::UNCHANGED), pos);
322 // also accept changes in nested inset
323 if (pos < size() && isInset(pos)) {
324 getInset(pos)->acceptChanges(bparams);
328 case Change::DELETED:
329 // Suppress access to non-existent
330 // "end-of-paragraph char"
332 eraseChar(pos, false);
343 void Paragraph::rejectChanges(BufferParams const & bparams,
344 pos_type start, pos_type end)
346 BOOST_ASSERT(start >= 0 && start <= size());
347 BOOST_ASSERT(end > start && end <= size() + 1);
349 for (pos_type pos = start; pos < end; ++pos) {
350 switch (lookupChange(pos).type) {
351 case Change::UNCHANGED:
352 // reject changes in nested inset
353 if (pos < size() && isInset(pos)) {
354 getInset(pos)->rejectChanges(bparams);
358 case Change::INSERTED:
359 // Suppress access to non-existent
360 // "end-of-paragraph char"
362 eraseChar(pos, false);
368 case Change::DELETED:
369 d->changes_.set(Change(Change::UNCHANGED), pos);
371 // Do NOT reject changes within a deleted inset!
372 // There may be insertions of a co-author inside of it!
380 Paragraph::value_type Paragraph::Private::getChar(pos_type pos) const
382 BOOST_ASSERT(pos >= 0 && pos <= size());
384 return owner_->getChar(pos);
388 void Paragraph::Private::insertChar(pos_type pos, value_type c,
389 Change const & change)
391 BOOST_ASSERT(pos >= 0 && pos <= size());
394 changes_.insert(change, pos);
396 // This is actually very common when parsing buffers (and
397 // maybe inserting ascii text)
399 // when appending characters, no need to update tables
400 owner_->text_.push_back(c);
404 owner_->text_.insert(owner_->text_.begin() + pos, c);
406 // Update the font table.
407 fontlist_.increasePosAfterPos(pos);
410 insetlist_.increasePosAfterPos(pos);
414 void Paragraph::insertInset(pos_type pos, Inset * inset,
415 Change const & change)
418 BOOST_ASSERT(pos >= 0 && pos <= size());
420 d->insertChar(pos, META_INSET, change);
421 BOOST_ASSERT(text_[pos] == META_INSET);
423 // Add a new entry in the insetlist_.
424 d->insetlist_.insert(inset, pos);
428 bool Paragraph::eraseChar(pos_type pos, bool trackChanges)
430 BOOST_ASSERT(pos >= 0 && pos <= size());
432 // keep the logic here in sync with the logic of isMergedOnEndOfParDeletion()
435 Change change = d->changes_.lookup(pos);
437 // set the character to DELETED if
438 // a) it was previously unchanged or
439 // b) it was inserted by a co-author
441 if (change.type == Change::UNCHANGED ||
442 (change.type == Change::INSERTED && change.author != 0)) {
443 setChange(pos, Change(Change::DELETED));
447 if (change.type == Change::DELETED)
451 // Don't physically access the imaginary end-of-paragraph character.
452 // eraseChar() can only mark it as DELETED. A physical deletion of
453 // end-of-par must be handled externally.
459 d->changes_.erase(pos);
461 // if it is an inset, delete the inset entry
462 if (text_[pos] == Paragraph::META_INSET)
463 d->insetlist_.erase(pos);
465 text_.erase(text_.begin() + pos);
467 // Update the fontlist_
468 d->fontlist_.erase(pos);
470 // Update the insetlist_
471 d->insetlist_.decreasePosAfterPos(pos);
477 int Paragraph::eraseChars(pos_type start, pos_type end, bool trackChanges)
479 BOOST_ASSERT(start >= 0 && start <= size());
480 BOOST_ASSERT(end >= start && end <= size() + 1);
483 for (pos_type count = end - start; count; --count) {
484 if (!eraseChar(i, trackChanges))
491 int Paragraph::Private::latexSurrogatePair(odocstream & os, value_type c,
492 value_type next, Encoding const & encoding)
494 // Writing next here may circumvent a possible font change between
495 // c and next. Since next is only output if it forms a surrogate pair
496 // with c we can ignore this:
497 // A font change inside a surrogate pair does not make sense and is
498 // hopefully impossible to input.
499 // FIXME: change tracking
500 // Is this correct WRT change tracking?
501 docstring const latex1 = encoding.latexChar(next);
502 docstring const latex2 = encoding.latexChar(c);
503 os << latex1 << '{' << latex2 << '}';
504 return latex1.length() + latex2.length() + 2;
508 bool Paragraph::Private::simpleTeXBlanks(Encoding const & encoding,
509 odocstream & os, TexRow & texrow,
511 unsigned int & column,
513 Layout const & style)
518 if (i + 1 < size()) {
519 char_type next = getChar(i + 1);
520 if (Encodings::isCombiningChar(next)) {
521 // This space has an accent, so we must always output it.
522 column += latexSurrogatePair(os, ' ', next, encoding) - 1;
527 if (lyxrc.plaintext_linelen > 0
528 && column > lyxrc.plaintext_linelen
530 && getChar(i - 1) != ' '
532 // same in FreeSpacing mode
533 && !owner_->isFreeSpacing()
534 // In typewriter mode, we want to avoid
535 // ! . ? : at the end of a line
536 && !(font.family() == Font::TYPEWRITER_FAMILY
537 && (getChar(i - 1) == '.'
538 || getChar(i - 1) == '?'
539 || getChar(i - 1) == ':'
540 || getChar(i - 1) == '!'))) {
543 texrow.start(owner_->id(), i + 1);
545 } else if (style.free_spacing) {
554 int Paragraph::Private::knownLangChars(odocstream & os,
557 Change & runningChange,
558 Encoding const & encoding,
561 // When the character is marked by the proper language, we simply
562 // get its code point in some encoding, otherwise we get the
563 // translation specified in the unicodesymbols file, which is
564 // something like "\textLANG{<spec>}". So, we have to retain
565 // "\textLANG{<spec>" for the first char but only "<spec>" for
566 // all subsequent chars.
567 docstring const latex1 = rtrim(encoding.latexChar(c), "}");
568 int length = latex1.length();
570 while (i + 1 < size()) {
571 char_type next = getChar(i + 1);
572 // Stop here if next character belongs to another
573 // language or there is a change tracking status.
574 if (!Encodings::isKnownLangChar(next, preamble) ||
575 runningChange != owner_->lookupChange(i + 1))
579 FontList::const_iterator cit = fontlist_.begin();
580 FontList::const_iterator end = fontlist_.end();
581 for (; cit != end; ++cit) {
582 if (cit->pos() >= i && !found) {
583 prev_font = cit->font();
586 if (cit->pos() >= i + 1)
589 // Stop here if there is a font attribute change.
590 if (found && cit != end && prev_font != cit->font())
592 docstring const latex = rtrim(encoding.latexChar(next), "}");
593 docstring::size_type const j =
594 latex.find_first_of(from_ascii("{"));
595 if (j == docstring::npos) {
597 length += latex.length();
599 os << latex.substr(j + 1);
600 length += latex.substr(j + 1).length();
604 // When the proper language is set, we are simply passed a code
605 // point, so we should not try to close the \textLANG command.
606 if (prefixIs(latex1, from_ascii("\\" + preamble))) {
614 bool Paragraph::Private::isTextAt(string const & str, pos_type pos) const
616 pos_type const len = str.length();
618 // is the paragraph large enough?
619 if (pos + len > size())
622 // does the wanted text start at point?
623 for (string::size_type i = 0; i < str.length(); ++i) {
624 // Caution: direct comparison of characters works only
625 // because str is pure ASCII.
626 if (str[i] != owner_->text_[pos + i])
630 return fontlist_.hasChangeInRange(pos, len);
634 void Paragraph::Private::latexInset(Buffer const & buf,
635 BufferParams const & bparams,
638 OutputParams & runparams,
641 Font const & outerfont,
643 Change & running_change,
644 Layout const & style,
646 unsigned int & column)
648 Inset * inset = owner_->getInset(i);
651 if (style.pass_thru) {
652 inset->plaintext(buf, os, runparams);
656 // FIXME: move this to InsetNewline::latex
657 if (inset->lyxCode() == NEWLINE_CODE) {
658 // newlines are handled differently here than
659 // the default in simpleTeXSpecialChars().
660 if (!style.newline_allowed) {
664 column += running_font.latexWriteEndChanges(
665 os, bparams, runparams,
670 if (running_font.family() == Font::TYPEWRITER_FAMILY)
673 basefont = owner_->getLayoutFont(bparams, outerfont);
674 running_font = basefont;
676 if (runparams.moving_arg)
682 texrow.start(owner_->id(), i + 1);
686 if (owner_->lookupChange(i).type == Change::DELETED) {
687 if( ++runparams.inDeletedInset == 1)
688 runparams.changeOfDeletedInset = owner_->lookupChange(i);
691 if (inset->canTrackChanges()) {
692 column += Changes::latexMarkChange(os, bparams, running_change,
693 Change(Change::UNCHANGED));
694 running_change = Change(Change::UNCHANGED);
698 odocstream::pos_type const len = os.tellp();
700 if ((inset->lyxCode() == GRAPHICS_CODE
701 || inset->lyxCode() == MATH_CODE
702 || inset->lyxCode() == HYPERLINK_CODE)
703 && running_font.isRightToLeft()) {
704 if (running_font.language()->lang() == "farsi")
711 // FIXME: Bug: we can have an empty font change here!
712 // if there has just been a font change, we are going to close it
713 // right now, which means stupid latex code like \textsf{}. AFAIK,
714 // this does not harm dvi output. A minor bug, thus (JMarc)
716 // Some insets cannot be inside a font change command.
717 // However, even such insets *can* be placed in \L or \R
718 // or their equivalents (for RTL language switches), so we don't
719 // close the language in those cases.
720 // ArabTeX, though, cannot handle this special behavior, it seems.
721 bool arabtex = basefont.language()->lang() == "arabic_arabtex"
722 || running_font.language()->lang() == "arabic_arabtex";
723 if (open_font && inset->noFontChange()) {
724 bool closeLanguage = arabtex
725 || basefont.isRightToLeft() == running_font.isRightToLeft();
726 unsigned int count = running_font.latexWriteEndChanges(os,
727 bparams, runparams, basefont, basefont, closeLanguage);
729 // if any font properties were closed, update the running_font,
730 // making sure, however, to leave the language as it was
732 // FIXME: probably a better way to keep track of the old
733 // language, than copying the entire font?
734 Font const copy_font(running_font);
735 basefont = owner_->getLayoutFont(bparams, outerfont);
736 running_font = basefont;
738 running_font.setLanguage(copy_font.language());
739 // leave font open if language is still open
740 open_font = (running_font.language() == basefont.language());
742 runparams.local_font = &basefont;
746 int tmp = inset->latex(buf, os, runparams);
749 if (running_font.language()->lang() == "farsi")
756 for (int j = 0; j < tmp; ++j)
759 texrow.start(owner_->id(), i + 1);
762 column += os.tellp() - len;
765 if (owner_->lookupChange(i).type == Change::DELETED)
766 --runparams.inDeletedInset;
770 void Paragraph::Private::latexSpecialChar(
772 OutputParams & runparams,
774 Change & running_change,
775 Layout const & style,
777 unsigned int & column)
779 value_type const c = getChar(i);
781 if (style.pass_thru) {
783 // FIXME UNICODE: This can fail if c cannot
784 // be encoded in the current encoding.
789 if (runparams.verbatim) {
794 if (lyxrc.fontenc == "T1" && latexSpecialT1(c, os, i, column))
797 if (running_font.family() == Font::TYPEWRITER_FAMILY
798 && latexSpecialTypewriter(c, os, i, column))
801 // Otherwise, we use what LaTeX provides us.
804 os << "\\textbackslash{}";
808 os << "\\textless{}";
812 os << "\\textgreater{}";
823 os << "\\char`\\\"{}";
828 case '%': case '#': case '{':
836 os << "\\textasciitilde{}";
841 os << "\\textasciicircum{}";
846 // avoid being mistaken for optional arguments
854 // Blanks are printed before font switching.
855 // Sure? I am not! (try nice-latex)
856 // I am sure it's correct. LyX might be smarter
857 // in the future, but for now, nothing wrong is
864 if (latexSpecialPhrase(os, i, column))
870 Encoding const & encoding = *(runparams.encoding);
871 if (i + 1 < size()) {
872 char_type next = getChar(i + 1);
873 if (Encodings::isCombiningChar(next)) {
874 column += latexSurrogatePair(os, c, next, encoding) - 1;
880 if (Encodings::isKnownLangChar(c, preamble)) {
881 column += knownLangChars(os, c, preamble, running_change,
885 docstring const latex = encoding.latexChar(c);
886 if (latex.length() > 1 && latex[latex.length() - 1] != '}') {
887 // Prevent eating of a following
888 // space or command corruption by
889 // following characters
890 column += latex.length() + 1;
893 column += latex.length() - 1;
901 bool Paragraph::Private::latexSpecialT1(char_type const c, odocstream & os,
902 pos_type & i, unsigned int & column)
908 // In T1 encoding, these characters exist
909 // but we should avoid ligatures
910 if (i + 1 > size() || getChar(i + 1) != c)
915 //os << "\\textcompwordmark{}";
927 bool Paragraph::Private::latexSpecialTypewriter(char_type const c, odocstream & os,
928 pos_type & i, unsigned int & column)
932 if (i + 1 < size() && getChar(i + 1) == '-') {
933 // "--" in Typewriter mode -> "-{}-"
940 // I assume this is hack treating typewriter as verbatim
941 // FIXME UNICODE: This can fail if c cannot be encoded
942 // in the current encoding.
947 // Those characters are not directly supported.
951 case '%': case '#': case '{':
960 // With Typewriter font, these characters exist.
967 bool Paragraph::Private::latexSpecialPhrase(odocstream & os, pos_type & i,
968 unsigned int & column)
970 // FIXME: if we have "LaTeX" with a font
971 // change in the middle (before the 'T', then
972 // the "TeX" part is still special cased.
973 // Really we should only operate this on
974 // "words" for some definition of word
976 for (size_t pnr = 0; pnr < phrases_nr; ++pnr) {
977 if (!isTextAt(special_phrases[pnr].phrase, i))
979 os << special_phrases[pnr].macro;
980 i += special_phrases[pnr].phrase.length() - 1;
981 column += special_phrases[pnr].macro.length() - 1;
988 void Paragraph::Private::validate(LaTeXFeatures & features,
989 Layout const & layout) const
992 if (!params_.spacing().isDefault())
993 features.require("setspace");
996 features.useLayout(layout.name());
999 fontlist_.validate(features);
1001 // then the indentation
1002 if (!params_.leftIndent().zero())
1003 features.require("ParagraphLeftIndent");
1006 InsetList::const_iterator icit = insetlist_.begin();
1007 InsetList::const_iterator iend = insetlist_.end();
1008 for (; icit != iend; ++icit) {
1010 icit->inset->validate(features);
1011 if (layout.needprotect &&
1012 icit->inset->lyxCode() == FOOT_CODE)
1013 features.require("NeedLyXFootnoteCode");
1017 // then the contents
1018 for (pos_type i = 0; i < size() ; ++i) {
1019 for (size_t pnr = 0; pnr < phrases_nr; ++pnr) {
1020 if (!special_phrases[pnr].builtin
1021 && isTextAt(special_phrases[pnr].phrase, i)) {
1022 features.require(special_phrases[pnr].phrase);
1026 Encodings::validate(getChar(i), features);
1030 /////////////////////////////////////////////////////////////////////
1034 /////////////////////////////////////////////////////////////////////
1036 Paragraph::Paragraph()
1037 : begin_of_body_(0), d(new Paragraph::Private(this))
1045 Paragraph::Paragraph(Paragraph const & par)
1046 : itemdepth(par.itemdepth),
1047 layout_(par.layout_),
1048 text_(par.text_), begin_of_body_(par.begin_of_body_),
1049 d(new Paragraph::Private(*par.d, this))
1054 Paragraph & Paragraph::operator=(Paragraph const & par)
1056 // needed as we will destroy the private part before copying it
1058 itemdepth = par.itemdepth;
1059 layout_ = par.layout();
1061 begin_of_body_ = par.begin_of_body_;
1064 d = new Private(*par.d, this);
1070 Paragraph::~Paragraph()
1076 void Paragraph::write(Buffer const & buf, ostream & os,
1077 BufferParams const & bparams,
1078 depth_type & dth) const
1080 // The beginning or end of a deeper (i.e. nested) area?
1081 if (dth != params().depth()) {
1082 if (params().depth() > dth) {
1083 while (params().depth() > dth) {
1084 os << "\n\\begin_deeper";
1088 while (params().depth() < dth) {
1089 os << "\n\\end_deeper";
1095 // First write the layout
1096 os << "\n\\begin_layout " << to_utf8(layout()->name()) << '\n';
1100 Font font1(Font::ALL_INHERIT, bparams.language);
1102 Change running_change = Change(Change::UNCHANGED);
1105 for (pos_type i = 0; i <= size(); ++i) {
1107 Change change = lookupChange(i);
1108 Changes::lyxMarkChange(os, column, running_change, change);
1109 running_change = change;
1114 // Write font changes
1115 Font font2 = getFontSettings(bparams, i);
1116 if (font2 != font1) {
1117 font2.lyxWriteChanges(font1, os);
1122 value_type const c = getChar(i);
1126 Inset const * inset = getInset(i);
1128 if (inset->directWrite()) {
1129 // international char, let it write
1130 // code directly so it's shorter in
1132 inset->write(buf, os);
1136 os << "\\begin_inset ";
1137 inset->write(buf, os);
1138 os << "\n\\end_inset\n\n";
1144 os << "\n\\backslash\n";
1148 if (i + 1 < size() && getChar(i + 1) == ' ') {
1155 if ((column > 70 && c == ' ')
1160 // this check is to amend a bug. LyX sometimes
1161 // inserts '\0' this could cause problems.
1163 std::vector<char> tmp = ucs4_to_utf8(c);
1164 tmp.push_back('\0');
1167 lyxerr << "ERROR (Paragraph::writeFile):"
1168 " NULL char in structure." << endl;
1174 os << "\n\\end_layout\n";
1178 void Paragraph::validate(LaTeXFeatures & features) const
1180 d->validate(features, *layout());
1184 void Paragraph::insert(pos_type start, docstring const & str,
1185 Font const & font, Change const & change)
1187 for (size_t i = 0, n = str.size(); i != n ; ++i)
1188 insertChar(start + i, str[i], font, change);
1192 void Paragraph::appendChar(value_type c, Font const & font,
1193 Change const & change)
1196 d->changes_.insert(change, text_.size());
1197 // when appending characters, no need to update tables
1199 setFont(text_.size() - 1, font);
1203 void Paragraph::appendString(docstring const & s, Font const & font,
1204 Change const & change)
1206 size_t end = s.size();
1207 size_t oldsize = text_.size();
1208 size_t newsize = oldsize + end;
1209 size_t capacity = text_.capacity();
1210 if (newsize >= capacity)
1211 text_.reserve(std::max(capacity + 100, newsize));
1213 // FIXME: Optimize this!
1214 for (pos_type i = 0; i != end; ++i) {
1216 d->changes_.insert(change, i);
1217 // when appending characters, no need to update tables
1218 text_.push_back(s[i]);
1220 d->fontlist_.setRange(oldsize, newsize, font);
1224 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c,
1227 d->insertChar(pos, c, Change(trackChanges ?
1228 Change::INSERTED : Change::UNCHANGED));
1232 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c,
1233 Font const & font, bool trackChanges)
1235 d->insertChar(pos, c, Change(trackChanges ?
1236 Change::INSERTED : Change::UNCHANGED));
1241 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c,
1242 Font const & font, Change const & change)
1244 d->insertChar(pos, c, change);
1249 void Paragraph::insertInset(pos_type pos, Inset * inset,
1250 Font const & font, Change const & change)
1252 insertInset(pos, inset, change);
1253 // Set the font/language of the inset...
1258 bool Paragraph::insetAllowed(InsetCode code)
1260 return !d->inset_owner_ || d->inset_owner_->insetAllowed(code);
1264 // Gets uninstantiated font setting at position.
1265 Font const Paragraph::getFontSettings(BufferParams const & bparams,
1269 lyxerr << " pos: " << pos << " size: " << size() << endl;
1270 BOOST_ASSERT(pos <= size());
1273 FontList::const_iterator cit = d->fontlist_.fontIterator(pos);
1274 if (cit != d->fontlist_.end())
1277 if (pos == size() && !empty())
1278 return getFontSettings(bparams, pos - 1);
1280 return Font(Font::ALL_INHERIT, getParLanguage(bparams));
1284 FontSpan Paragraph::fontSpan(pos_type pos) const
1286 BOOST_ASSERT(pos <= size());
1289 FontList::const_iterator cit = d->fontlist_.begin();
1290 FontList::const_iterator end = d->fontlist_.end();
1291 for (; cit != end; ++cit) {
1292 if (cit->pos() >= pos) {
1293 if (pos >= beginOfBody())
1294 return FontSpan(std::max(start, beginOfBody()),
1297 return FontSpan(start,
1298 std::min(beginOfBody() - 1,
1301 start = cit->pos() + 1;
1304 // This should not happen, but if so, we take no chances.
1305 //lyxerr << "Paragraph::getEndPosOfFontSpan: This should not happen!"
1307 return FontSpan(pos, pos);
1311 // Gets uninstantiated font setting at position 0
1312 Font const Paragraph::getFirstFontSettings(BufferParams const & bparams) const
1314 if (!empty() && !d->fontlist_.empty())
1315 return d->fontlist_.begin()->font();
1317 return Font(Font::ALL_INHERIT, bparams.language);
1321 // Gets the fully instantiated font at a given position in a paragraph
1322 // This is basically the same function as Text::GetFont() in text2.cpp.
1323 // The difference is that this one is used for generating the LaTeX file,
1324 // and thus cosmetic "improvements" are disallowed: This has to deliver
1325 // the true picture of the buffer. (Asger)
1326 Font const Paragraph::getFont(BufferParams const & bparams, pos_type pos,
1327 Font const & outerfont) const
1329 BOOST_ASSERT(pos >= 0);
1331 Font font = getFontSettings(bparams, pos);
1333 pos_type const body_pos = beginOfBody();
1335 font.realize(layout_->labelfont);
1337 font.realize(layout_->font);
1339 font.realize(outerfont);
1340 font.realize(bparams.getFont());
1346 Font const Paragraph::getLabelFont
1347 (BufferParams const & bparams, Font const & outerfont) const
1349 Font tmpfont = layout()->labelfont;
1350 tmpfont.setLanguage(getParLanguage(bparams));
1351 tmpfont.realize(outerfont);
1352 tmpfont.realize(bparams.getFont());
1357 Font const Paragraph::getLayoutFont
1358 (BufferParams const & bparams, Font const & outerfont) const
1360 Font tmpfont = layout()->font;
1361 tmpfont.setLanguage(getParLanguage(bparams));
1362 tmpfont.realize(outerfont);
1363 tmpfont.realize(bparams.getFont());
1368 /// Returns the height of the highest font in range
1369 Font_size Paragraph::highestFontInRange
1370 (pos_type startpos, pos_type endpos, Font_size def_size) const
1372 return d->fontlist_.highestInRange(startpos, endpos, def_size);
1376 Paragraph::value_type
1377 Paragraph::getUChar(BufferParams const & bparams, pos_type pos) const
1379 value_type c = getChar(pos);
1380 if (!lyxrc.rtl_support)
1410 if (uc != c && getFontSettings(bparams, pos).isRightToLeft())
1417 void Paragraph::setFont(pos_type pos, Font const & font)
1419 BOOST_ASSERT(pos <= size());
1421 // First, reduce font against layout/label font
1422 // Update: The setCharFont() routine in text2.cpp already
1423 // reduces font, so we don't need to do that here. (Asger)
1425 d->fontlist_.set(pos, font);
1429 void Paragraph::makeSameLayout(Paragraph const & par)
1431 layout(par.layout());
1433 d->params_ = par.params();
1437 bool Paragraph::stripLeadingSpaces(bool trackChanges)
1439 if (isFreeSpacing())
1445 while (pos < size() && (isNewline(pos) || isLineSeparator(pos))) {
1446 if (eraseChar(pos, trackChanges))
1452 return count > 0 || pos > 0;
1456 bool Paragraph::hasSameLayout(Paragraph const & par) const
1458 return par.layout() == layout() && d->params_.sameLayout(par.params());
1462 depth_type Paragraph::getDepth() const
1464 return params().depth();
1468 depth_type Paragraph::getMaxDepthAfter() const
1470 if (layout()->isEnvironment())
1471 return params().depth() + 1;
1473 return params().depth();
1477 char Paragraph::getAlign() const
1479 if (params().align() == LYX_ALIGN_LAYOUT)
1480 return layout()->align;
1482 return params().align();
1486 docstring const & Paragraph::getLabelstring() const
1488 return params().labelString();
1492 // the next two functions are for the manual labels
1493 docstring const Paragraph::getLabelWidthString() const
1495 if (layout()->margintype == MARGIN_MANUAL)
1496 return params().labelWidthString();
1498 return _("Senseless with this layout!");
1502 void Paragraph::setLabelWidthString(docstring const & s)
1504 params().labelWidthString(s);
1508 docstring const Paragraph::translateIfPossible(docstring const & s,
1509 BufferParams const & bparams) const
1511 if (!support::isAscii(s) || s.empty()) {
1512 // This must be a user defined layout. We cannot translate
1513 // this, since gettext accepts only ascii keys.
1516 // Probably standard layout, try to translate
1517 Messages & m = getMessages(getParLanguage(bparams)->code());
1518 return m.get(to_ascii(s));
1522 docstring Paragraph::expandLabel(LayoutPtr const & layout,
1523 BufferParams const & bparams, bool process_appendix) const
1525 TextClass const & tclass = bparams.getTextClass();
1528 if (process_appendix && params().appendix())
1529 fmt = translateIfPossible(layout->labelstring_appendix(),
1532 fmt = translateIfPossible(layout->labelstring(), bparams);
1534 if (fmt.empty() && layout->labeltype == LABEL_COUNTER
1535 && !layout->counter.empty())
1536 fmt = "\\the" + layout->counter;
1538 // handle 'inherited level parts' in 'fmt',
1539 // i.e. the stuff between '@' in '@Section@.\arabic{subsection}'
1540 size_t const i = fmt.find('@', 0);
1541 if (i != docstring::npos) {
1542 size_t const j = fmt.find('@', i + 1);
1543 if (j != docstring::npos) {
1544 docstring parent(fmt, i + 1, j - i - 1);
1545 docstring label = from_ascii("??");
1546 if (tclass.hasLayout(parent))
1547 docstring label = expandLabel(tclass[parent], bparams,
1549 fmt = docstring(fmt, 0, i) + label
1550 + docstring(fmt, j + 1, docstring::npos);
1554 return tclass.counters().counterLabel(fmt);
1558 void Paragraph::applyLayout(LayoutPtr const & new_layout)
1561 LyXAlignment const oldAlign = params().align();
1563 if (!(oldAlign & layout()->alignpossible)) {
1564 frontend::Alert::warning(_("Alignment not permitted"),
1565 _("The new layout does not permit the alignment previously used.\nSetting to default."));
1566 params().align(LYX_ALIGN_LAYOUT);
1571 pos_type Paragraph::beginOfBody() const
1573 return begin_of_body_;
1577 void Paragraph::setBeginOfBody()
1579 if (layout()->labeltype != LABEL_MANUAL) {
1584 // Unroll the first two cycles of the loop
1585 // and remember the previous character to
1586 // remove unnecessary getChar() calls
1588 pos_type end = size();
1589 if (i < end && !isNewline(i)) {
1591 char_type previous_char = 0;
1594 previous_char = text_[i];
1595 if (!isNewline(i)) {
1597 while (i < end && previous_char != ' ') {
1602 previous_char = temp;
1612 // returns -1 if inset not found
1613 int Paragraph::getPositionOfInset(Inset const * inset) const
1616 InsetList::const_iterator it = d->insetlist_.begin();
1617 InsetList::const_iterator end = d->insetlist_.end();
1618 for (; it != end; ++it)
1619 if (it->inset == inset)
1625 InsetBibitem * Paragraph::bibitem() const
1627 if (!d->insetlist_.empty()) {
1628 Inset * inset = d->insetlist_.begin()->inset;
1629 if (inset->lyxCode() == BIBITEM_CODE)
1630 return static_cast<InsetBibitem *>(inset);
1636 bool Paragraph::forceDefaultParagraphs() const
1638 return inInset() && inInset()->forceDefaultParagraphs(0);
1644 // paragraphs inside floats need different alignment tags to avoid
1647 bool noTrivlistCentering(InsetCode code)
1649 return code == FLOAT_CODE || code == WRAP_CODE;
1653 string correction(string const & orig)
1655 if (orig == "flushleft")
1656 return "raggedright";
1657 if (orig == "flushright")
1658 return "raggedleft";
1659 if (orig == "center")
1665 string const corrected_env(string const & suffix, string const & env,
1668 string output = suffix + "{";
1669 if (noTrivlistCentering(code))
1670 output += correction(env);
1674 if (suffix == "\\begin")
1680 void adjust_row_column(string const & str, TexRow & texrow, int & column)
1682 if (!contains(str, "\n"))
1683 column += str.size();
1687 column = rsplit(str, tmp, '\n').size();
1694 // This could go to ParagraphParameters if we want to
1695 int Paragraph::startTeXParParams(BufferParams const & bparams,
1696 odocstream & os, TexRow & texrow,
1697 bool moving_arg) const
1701 if (params().noindent()) {
1702 os << "\\noindent ";
1706 LyXAlignment const curAlign = params().align();
1708 if (curAlign == layout()->align)
1712 case LYX_ALIGN_NONE:
1713 case LYX_ALIGN_BLOCK:
1714 case LYX_ALIGN_LAYOUT:
1715 case LYX_ALIGN_SPECIAL:
1717 case LYX_ALIGN_LEFT:
1718 case LYX_ALIGN_RIGHT:
1719 case LYX_ALIGN_CENTER:
1728 case LYX_ALIGN_NONE:
1729 case LYX_ALIGN_BLOCK:
1730 case LYX_ALIGN_LAYOUT:
1731 case LYX_ALIGN_SPECIAL:
1733 case LYX_ALIGN_LEFT: {
1735 if (getParLanguage(bparams)->babel() != "hebrew")
1736 output = corrected_env("\\begin", "flushleft", ownerCode());
1738 output = corrected_env("\\begin", "flushright", ownerCode());
1739 os << from_ascii(output);
1740 adjust_row_column(output, texrow, column);
1742 } case LYX_ALIGN_RIGHT: {
1744 if (getParLanguage(bparams)->babel() != "hebrew")
1745 output = corrected_env("\\begin", "flushright", ownerCode());
1747 output = corrected_env("\\begin", "flushleft", ownerCode());
1748 os << from_ascii(output);
1749 adjust_row_column(output, texrow, column);
1751 } case LYX_ALIGN_CENTER: {
1753 output = corrected_env("\\begin", "center", ownerCode());
1754 os << from_ascii(output);
1755 adjust_row_column(output, texrow, column);
1764 // This could go to ParagraphParameters if we want to
1765 int Paragraph::endTeXParParams(BufferParams const & bparams,
1766 odocstream & os, TexRow & texrow,
1767 bool moving_arg) const
1771 switch (params().align()) {
1772 case LYX_ALIGN_NONE:
1773 case LYX_ALIGN_BLOCK:
1774 case LYX_ALIGN_LAYOUT:
1775 case LYX_ALIGN_SPECIAL:
1777 case LYX_ALIGN_LEFT:
1778 case LYX_ALIGN_RIGHT:
1779 case LYX_ALIGN_CENTER:
1787 switch (params().align()) {
1788 case LYX_ALIGN_NONE:
1789 case LYX_ALIGN_BLOCK:
1790 case LYX_ALIGN_LAYOUT:
1791 case LYX_ALIGN_SPECIAL:
1793 case LYX_ALIGN_LEFT: {
1795 if (getParLanguage(bparams)->babel() != "hebrew")
1796 output = corrected_env("\n\\par\\end", "flushleft", ownerCode());
1798 output = corrected_env("\n\\par\\end", "flushright", ownerCode());
1799 os << from_ascii(output);
1800 adjust_row_column(output, texrow, column);
1802 } case LYX_ALIGN_RIGHT: {
1804 if (getParLanguage(bparams)->babel() != "hebrew")
1805 output = corrected_env("\n\\par\\end", "flushright", ownerCode());
1807 output = corrected_env("\n\\par\\end", "flushleft", ownerCode());
1808 os << from_ascii(output);
1809 adjust_row_column(output, texrow, column);
1811 } case LYX_ALIGN_CENTER: {
1813 output = corrected_env("\n\\par\\end", "center", ownerCode());
1814 os << from_ascii(output);
1815 adjust_row_column(output, texrow, column);
1824 // This one spits out the text of the paragraph
1825 bool Paragraph::latex(Buffer const & buf,
1826 BufferParams const & bparams,
1827 Font const & outerfont,
1828 odocstream & os, TexRow & texrow,
1829 OutputParams const & runparams) const
1831 LYXERR(Debug::LATEX) << "SimpleTeXOnePar... " << this << endl;
1833 bool return_value = false;
1837 // well we have to check if we are in an inset with unlimited
1838 // length (all in one row) if that is true then we don't allow
1839 // any special options in the paragraph and also we don't allow
1840 // any environment other than the default layout of the text class
1842 bool asdefault = forceDefaultParagraphs();
1845 style = bparams.getTextClass().defaultLayout();
1850 // Current base font for all inherited font changes, without any
1851 // change caused by an individual character, except for the language:
1852 // It is set to the language of the first character.
1853 // As long as we are in the label, this font is the base font of the
1854 // label. Before the first body character it is set to the base font
1858 // Maybe we have to create a optional argument.
1859 pos_type body_pos = beginOfBody();
1860 unsigned int column = 0;
1863 // the optional argument is kept in curly brackets in
1864 // case it contains a ']'
1867 basefont = getLabelFont(bparams, outerfont);
1869 basefont = getLayoutFont(bparams, outerfont);
1872 // Which font is currently active?
1873 Font running_font(basefont);
1874 // Do we have an open font change?
1875 bool open_font = false;
1877 Change runningChange = Change(Change::UNCHANGED);
1879 texrow.start(id(), 0);
1881 // if the paragraph is empty, the loop will not be entered at all
1883 if (style->isCommand()) {
1888 column += startTeXParParams(bparams, os, texrow,
1889 runparams.moving_arg);
1892 for (pos_type i = 0; i < size(); ++i) {
1893 // First char in paragraph or after label?
1894 if (i == body_pos) {
1897 column += running_font.latexWriteEndChanges(
1898 os, bparams, runparams,
1899 basefont, basefont);
1902 basefont = getLayoutFont(bparams, outerfont);
1903 running_font = basefont;
1905 column += Changes::latexMarkChange(os, bparams,
1906 runningChange, Change(Change::UNCHANGED));
1907 runningChange = Change(Change::UNCHANGED);
1912 if (style->isCommand()) {
1918 column += startTeXParParams(bparams, os,
1920 runparams.moving_arg);
1923 Change const & change = runparams.inDeletedInset ? runparams.changeOfDeletedInset
1926 if (bparams.outputChanges && runningChange != change) {
1928 column += running_font.latexWriteEndChanges(
1929 os, bparams, runparams, basefont, basefont);
1932 basefont = getLayoutFont(bparams, outerfont);
1933 running_font = basefont;
1935 column += Changes::latexMarkChange(os, bparams, runningChange, change);
1936 runningChange = change;
1939 // do not output text which is marked deleted
1940 // if change tracking output is disabled
1941 if (!bparams.outputChanges && change.type == Change::DELETED) {
1947 // Fully instantiated font
1948 Font const font = getFont(bparams, i, outerfont);
1950 Font const last_font = running_font;
1952 // Do we need to close the previous font?
1954 (font != running_font ||
1955 font.language() != running_font.language()))
1957 column += running_font.latexWriteEndChanges(
1958 os, bparams, runparams, basefont,
1959 (i == body_pos-1) ? basefont : font);
1960 running_font = basefont;
1964 // Switch file encoding if necessary
1965 if (runparams.encoding->package() == Encoding::inputenc &&
1966 font.language()->encoding()->package() == Encoding::inputenc) {
1967 std::pair<bool, int> const enc_switch = switchEncoding(os, bparams,
1968 runparams.moving_arg, *(runparams.encoding),
1969 *(font.language()->encoding()));
1970 if (enc_switch.first) {
1971 column += enc_switch.second;
1972 runparams.encoding = font.language()->encoding();
1976 value_type const c = getChar(i);
1978 // Do we need to change font?
1979 if ((font != running_font ||
1980 font.language() != running_font.language()) &&
1983 odocstringstream ods;
1984 column += font.latexWriteStartChanges(ods, bparams,
1985 runparams, basefont,
1987 running_font = font;
1989 docstring fontchange = ods.str();
1990 // check if the fontchange ends with a trailing blank
1991 // (like "\small " (see bug 3382)
1992 if (suffixIs(fontchange, ' ') && c == ' ')
1993 os << fontchange.substr(0, fontchange.size() - 1)
1994 << from_ascii("{}");
2000 // FIXME: integrate this case in latexSpecialChar
2001 // Do not print the separation of the optional argument
2002 // if style->pass_thru is false. This works because
2003 // latexSpecialChar ignores spaces if
2004 // style->pass_thru is false.
2005 if (i != body_pos - 1) {
2006 if (d->simpleTeXBlanks(
2007 *(runparams.encoding), os, texrow,
2008 i, column, font, *style)) {
2009 // A surrogate pair was output. We
2010 // must not call latexSpecialChar
2011 // in this iteration, since it would output
2012 // the combining character again.
2019 OutputParams rp = runparams;
2020 rp.free_spacing = style->free_spacing;
2021 rp.local_font = &font;
2022 rp.intitle = style->intitle;
2024 // Two major modes: LaTeX or plain
2025 // Handle here those cases common to both modes
2026 // and then split to handle the two modes separately.
2027 if (c == Paragraph::META_INSET)
2028 d->latexInset(buf, bparams, os,
2029 texrow, rp, running_font,
2030 basefont, outerfont, open_font,
2031 runningChange, *style, i, column);
2033 d->latexSpecialChar(os, rp, running_font, runningChange,
2036 // Set the encoding to that returned from simpleTeXSpecialChars (see
2037 // comment for encoding member in OutputParams.h)
2038 runparams.encoding = rp.encoding;
2041 // If we have an open font definition, we have to close it
2043 #ifdef FIXED_LANGUAGE_END_DETECTION
2046 .latexWriteEndChanges(os, bparams, runparams,
2048 next_->getFont(bparams, 0, outerfont));
2050 running_font.latexWriteEndChanges(os, bparams,
2051 runparams, basefont, basefont);
2054 //FIXME: For now we ALWAYS have to close the foreign font settings if they are
2055 //FIXME: there as we start another \selectlanguage with the next paragraph if
2056 //FIXME: we are in need of this. This should be fixed sometime (Jug)
2057 running_font.latexWriteEndChanges(os, bparams, runparams,
2058 basefont, basefont);
2062 column += Changes::latexMarkChange(os, bparams, runningChange, Change(Change::UNCHANGED));
2064 // Needed if there is an optional argument but no contents.
2065 if (body_pos > 0 && body_pos == size()) {
2067 return_value = false;
2071 column += endTeXParParams(bparams, os, texrow,
2072 runparams.moving_arg);
2075 LYXERR(Debug::LATEX) << "SimpleTeXOnePar...done " << this << endl;
2076 return return_value;
2093 string tag_name(PAR_TAG const & pt) {
2095 case PAR_NONE: return "!-- --";
2096 case TT: return "tt";
2097 case SF: return "sf";
2098 case BF: return "bf";
2099 case IT: return "it";
2100 case SL: return "sl";
2101 case EM: return "em";
2108 void operator|=(PAR_TAG & p1, PAR_TAG const & p2)
2110 p1 = static_cast<PAR_TAG>(p1 | p2);
2115 void reset(PAR_TAG & p1, PAR_TAG const & p2)
2117 p1 = static_cast<PAR_TAG>(p1 & ~p2);
2123 bool Paragraph::emptyTag() const
2125 for (pos_type i = 0; i < size(); ++i) {
2127 Inset const * inset = getInset(i);
2128 InsetCode lyx_code = inset->lyxCode();
2129 if (lyx_code != TOC_CODE &&
2130 lyx_code != INCLUDE_CODE &&
2131 lyx_code != GRAPHICS_CODE &&
2132 lyx_code != ERT_CODE &&
2133 lyx_code != LISTINGS_CODE &&
2134 lyx_code != FLOAT_CODE &&
2135 lyx_code != TABULAR_CODE) {
2139 value_type c = getChar(i);
2140 if (c != ' ' && c != '\t')
2148 string Paragraph::getID(Buffer const & buf, OutputParams const & runparams) const
2150 for (pos_type i = 0; i < size(); ++i) {
2152 Inset const * inset = getInset(i);
2153 InsetCode lyx_code = inset->lyxCode();
2154 if (lyx_code == LABEL_CODE) {
2155 string const id = static_cast<InsetCommand const *>(inset)->getContents();
2156 return "id='" + to_utf8(sgml::cleanID(buf, runparams, from_utf8(id))) + "'";
2165 pos_type Paragraph::getFirstWord(Buffer const & buf, odocstream & os, OutputParams const & runparams) const
2168 for (i = 0; i < size(); ++i) {
2170 Inset const * inset = getInset(i);
2171 inset->docbook(buf, os, runparams);
2173 value_type c = getChar(i);
2176 os << sgml::escapeChar(c);
2183 bool Paragraph::onlyText(Buffer const & buf, Font const & outerfont, pos_type initial) const
2187 for (pos_type i = initial; i < size(); ++i) {
2188 Font font = getFont(buf.params(), i, outerfont);
2191 if (i != initial && font != font_old)
2200 void Paragraph::simpleDocBookOnePar(Buffer const & buf,
2202 OutputParams const & runparams,
2203 Font const & outerfont,
2204 pos_type initial) const
2206 bool emph_flag = false;
2208 LayoutPtr const & style = layout();
2210 style->labeltype == LABEL_MANUAL ? style->labelfont : style->font;
2212 if (style->pass_thru && !onlyText(buf, outerfont, initial))
2215 // parsing main loop
2216 for (pos_type i = initial; i < size(); ++i) {
2217 Font font = getFont(buf.params(), i, outerfont);
2219 // handle <emphasis> tag
2220 if (font_old.emph() != font.emph()) {
2221 if (font.emph() == Font::ON) {
2224 } else if (i != initial) {
2225 os << "</emphasis>";
2231 Inset const * inset = getInset(i);
2232 inset->docbook(buf, os, runparams);
2234 value_type c = getChar(i);
2236 if (style->pass_thru)
2239 os << sgml::escapeChar(c);
2245 os << "</emphasis>";
2248 if (style->free_spacing)
2250 if (style->pass_thru && !onlyText(buf, outerfont, initial))
2255 bool Paragraph::isHfill(pos_type pos) const
2258 && getInset(pos)->lyxCode() == HFILL_CODE;
2262 bool Paragraph::isNewline(pos_type pos) const
2265 && getInset(pos)->lyxCode() == NEWLINE_CODE;
2269 bool Paragraph::isLineSeparator(pos_type pos) const
2271 value_type const c = getChar(pos);
2272 return isLineSeparatorChar(c)
2273 || (c == Paragraph::META_INSET && getInset(pos) &&
2274 getInset(pos)->isLineSeparator());
2278 /// Used by the spellchecker
2279 bool Paragraph::isLetter(pos_type pos) const
2282 return getInset(pos)->isLetter();
2284 value_type const c = getChar(pos);
2285 return isLetterChar(c) || isDigit(c);
2291 Paragraph::getParLanguage(BufferParams const & bparams) const
2294 return getFirstFontSettings(bparams).language();
2295 // FIXME: we should check the prev par as well (Lgb)
2296 return bparams.language;
2300 bool Paragraph::isRTL(BufferParams const & bparams) const
2302 return lyxrc.rtl_support
2303 && getParLanguage(bparams)->rightToLeft()
2304 && ownerCode() != ERT_CODE
2305 && ownerCode() != LISTINGS_CODE;
2309 void Paragraph::changeLanguage(BufferParams const & bparams,
2310 Language const * from, Language const * to)
2312 // change language including dummy font change at the end
2313 for (pos_type i = 0; i <= size(); ++i) {
2314 Font font = getFontSettings(bparams, i);
2315 if (font.language() == from) {
2316 font.setLanguage(to);
2323 bool Paragraph::isMultiLingual(BufferParams const & bparams) const
2325 Language const * doc_language = bparams.language;
2326 FontList::const_iterator cit = d->fontlist_.begin();
2327 FontList::const_iterator end = d->fontlist_.end();
2329 for (; cit != end; ++cit)
2330 if (cit->font().language() != ignore_language &&
2331 cit->font().language() != latex_language &&
2332 cit->font().language() != doc_language)
2338 // Convert the paragraph to a string.
2339 // Used for building the table of contents
2340 docstring const Paragraph::asString(Buffer const & buffer, bool label) const
2342 return asString(buffer, 0, size(), label);
2346 docstring const Paragraph::asString(Buffer const & buffer,
2347 pos_type beg, pos_type end, bool label) const
2350 odocstringstream os;
2352 if (beg == 0 && label && !params().labelString().empty())
2353 os << params().labelString() << ' ';
2355 for (pos_type i = beg; i < end; ++i) {
2356 value_type const c = getChar(i);
2359 else if (c == META_INSET)
2360 getInset(i)->textString(buffer, os);
2367 void Paragraph::setInsetOwner(Inset * inset)
2369 d->inset_owner_ = inset;
2373 int Paragraph::id() const
2379 LayoutPtr const & Paragraph::layout() const
2385 void Paragraph::layout(LayoutPtr const & new_layout)
2387 layout_ = new_layout;
2391 Inset * Paragraph::inInset() const
2393 return d->inset_owner_;
2397 InsetCode Paragraph::ownerCode() const
2399 return d->inset_owner_ ?
2400 d->inset_owner_->lyxCode() : NO_CODE;
2404 ParagraphParameters & Paragraph::params()
2410 ParagraphParameters const & Paragraph::params() const
2416 bool Paragraph::isFreeSpacing() const
2418 if (layout()->free_spacing)
2421 // for now we just need this, later should we need this in some
2422 // other way we can always add a function to Inset too.
2423 return ownerCode() == ERT_CODE || ownerCode() == LISTINGS_CODE;
2427 bool Paragraph::allowEmpty() const
2429 if (layout()->keepempty)
2431 return ownerCode() == ERT_CODE || ownerCode() == LISTINGS_CODE;
2435 char_type Paragraph::transformChar(char_type c, pos_type pos) const
2437 if (!Encodings::is_arabic(c))
2440 value_type prev_char = ' ';
2441 value_type next_char = ' ';
2443 for (pos_type i = pos - 1; i >= 0; --i) {
2444 value_type const par_char = getChar(i);
2445 if (!Encodings::isComposeChar_arabic(par_char)) {
2446 prev_char = par_char;
2451 for (pos_type i = pos + 1, end = size(); i < end; ++i) {
2452 value_type const par_char = getChar(i);
2453 if (!Encodings::isComposeChar_arabic(par_char)) {
2454 next_char = par_char;
2459 if (Encodings::is_arabic(next_char)) {
2460 if (Encodings::is_arabic(prev_char) &&
2461 !Encodings::is_arabic_special(prev_char))
2462 return Encodings::transformChar(c, Encodings::FORM_MEDIAL);
2464 return Encodings::transformChar(c, Encodings::FORM_INITIAL);
2466 if (Encodings::is_arabic(prev_char) &&
2467 !Encodings::is_arabic_special(prev_char))
2468 return Encodings::transformChar(c, Encodings::FORM_FINAL);
2470 return Encodings::transformChar(c, Encodings::FORM_ISOLATED);
2475 int Paragraph::checkBiblio(bool track_changes)
2478 //This is getting more and more a mess. ...We really should clean
2479 //up this bibitem issue for 1.6. See also bug 2743.
2481 // Add bibitem insets if necessary
2482 if (layout()->labeltype != LABEL_BIBLIO)
2485 bool hasbibitem = !d->insetlist_.empty()
2486 // Insist on it being in pos 0
2487 && getChar(0) == Paragraph::META_INSET
2488 && d->insetlist_.begin()->inset->lyxCode() == BIBITEM_CODE;
2493 // remove a bibitem in pos != 0
2494 // restore it later in pos 0 if necessary
2495 // (e.g. if a user inserts contents _before_ the item)
2496 // we're assuming there's only one of these, which there
2498 int erasedInsetPosition = -1;
2499 InsetList::iterator it = d->insetlist_.begin();
2500 InsetList::iterator end = d->insetlist_.end();
2501 for (; it != end; ++it)
2502 if (it->inset->lyxCode() == BIBITEM_CODE
2504 InsetBibitem * olditem = static_cast<InsetBibitem *>(it->inset);
2505 oldkey = olditem->getParam("key");
2506 oldlabel = olditem->getParam("label");
2507 erasedInsetPosition = it->pos;
2508 eraseChar(erasedInsetPosition, track_changes);
2512 //There was an InsetBibitem at the beginning, and we didn't
2513 //have to erase one.
2514 if (hasbibitem && erasedInsetPosition < 0)
2517 //There was an InsetBibitem at the beginning and we did have to
2518 //erase one. So we give its properties to the beginning inset.
2520 InsetBibitem * inset =
2521 static_cast<InsetBibitem *>(d->insetlist_.begin()->inset);
2522 if (!oldkey.empty())
2523 inset->setParam("key", oldkey);
2524 inset->setParam("label", oldlabel);
2525 return -erasedInsetPosition;
2528 //There was no inset at the beginning, so we need to create one with
2529 //the key and label of the one we erased.
2530 InsetBibitem * inset(new InsetBibitem(InsetCommandParams(BIBITEM_CODE)));
2531 // restore values of previously deleted item in this par.
2532 if (!oldkey.empty())
2533 inset->setParam("key", oldkey);
2534 inset->setParam("label", oldlabel);
2535 insertInset(0, static_cast<Inset *>(inset),
2536 Change(track_changes ? Change::INSERTED : Change::UNCHANGED));
2542 void Paragraph::checkAuthors(AuthorList const & authorList)
2544 d->changes_.checkAuthors(authorList);
2548 bool Paragraph::isUnchanged(pos_type pos) const
2550 return lookupChange(pos).type == Change::UNCHANGED;
2554 bool Paragraph::isInserted(pos_type pos) const
2556 return lookupChange(pos).type == Change::INSERTED;
2560 bool Paragraph::isDeleted(pos_type pos) const
2562 return lookupChange(pos).type == Change::DELETED;
2566 InsetList const & Paragraph::insetList() const
2568 return d->insetlist_;
2572 Inset * Paragraph::releaseInset(pos_type pos)
2574 Inset * inset = d->insetlist_.release(pos);
2575 /// does not honour change tracking!
2576 eraseChar(pos, false);
2581 Inset * Paragraph::getInset(pos_type pos)
2583 return d->insetlist_.get(pos);
2587 Inset const * Paragraph::getInset(pos_type pos) const
2589 return d->insetlist_.get(pos);
2593 int Paragraph::numberOfOptArgs() const
2596 InsetList::const_iterator it = insetList().begin();
2597 InsetList::const_iterator end = insetList().end();
2598 for (; it != end ; ++it) {
2599 if (it->inset->lyxCode() == OPTARG_CODE)