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 simpleTeXSpecialChars(Buffer const &, BufferParams const &,
115 TexRow & texrow, OutputParams &,
118 Font const & outerfont,
120 Change & running_change,
121 Layout const & style,
123 unsigned int & column, value_type const c);
126 void validate(LaTeXFeatures & features,
127 Layout const & layout) const;
130 pos_type size() const { return owner_->size(); }
132 /// match a string against a particular point in the paragraph
133 bool isTextAt(std::string const & str, pos_type pos) const;
135 /// Which Paragraph owns us?
139 Inset * inset_owner_;
147 static unsigned int paragraph_id;
149 ParagraphParameters params_;
151 /// for recording and looking up changes
155 InsetList insetlist_;
162 using std::upper_bound;
163 using std::lower_bound;
167 // Initialization of the counter for the paragraph id's,
168 unsigned int Paragraph::Private::paragraph_id = 0;
172 struct special_phrase {
178 special_phrase const special_phrases[] = {
179 { "LyX", from_ascii("\\LyX{}"), false },
180 { "TeX", from_ascii("\\TeX{}"), true },
181 { "LaTeX2e", from_ascii("\\LaTeXe{}"), true },
182 { "LaTeX", from_ascii("\\LaTeX{}"), true },
185 size_t const phrases_nr = sizeof(special_phrases)/sizeof(special_phrase);
190 Paragraph::Private::Private(Paragraph * owner)
194 id_ = paragraph_id++;
198 Paragraph::Private::Private(Private const & p, Paragraph * owner)
199 : owner_(owner), inset_owner_(p.inset_owner_), fontlist_(p.fontlist_),
200 params_(p.params_), changes_(p.changes_), insetlist_(p.insetlist_)
202 id_ = paragraph_id++;
206 bool Paragraph::isChanged(pos_type start, pos_type end) const
208 BOOST_ASSERT(start >= 0 && start <= size());
209 BOOST_ASSERT(end > start && end <= size() + 1);
211 return d->changes_.isChanged(start, end);
215 bool Paragraph::isMergedOnEndOfParDeletion(bool trackChanges) const {
216 // keep the logic here in sync with the logic of eraseChars()
222 Change change = d->changes_.lookup(size());
224 return change.type == Change::INSERTED && change.author == 0;
228 void Paragraph::setChange(Change const & change)
230 // beware of the imaginary end-of-par character!
231 d->changes_.set(change, 0, size() + 1);
234 * Propagate the change recursively - but not in case of DELETED!
236 * Imagine that your co-author makes changes in an existing inset. He
237 * sends your document to you and you come to the conclusion that the
238 * inset should go completely. If you erase it, LyX must not delete all
239 * text within the inset. Otherwise, the change tracked insertions of
240 * your co-author get lost and there is no way to restore them later.
242 * Conclusion: An inset's content should remain untouched if you delete it
245 if (change.type != Change::DELETED) {
246 for (pos_type pos = 0; pos < size(); ++pos) {
248 getInset(pos)->setChange(change);
254 void Paragraph::setChange(pos_type pos, Change const & change)
256 BOOST_ASSERT(pos >= 0 && pos <= size());
258 d->changes_.set(change, pos);
260 // see comment in setChange(Change const &) above
262 if (change.type != Change::DELETED &&
263 pos < size() && isInset(pos)) {
264 getInset(pos)->setChange(change);
269 Change const & Paragraph::lookupChange(pos_type pos) const
271 BOOST_ASSERT(pos >= 0 && pos <= size());
273 return d->changes_.lookup(pos);
277 void Paragraph::acceptChanges(BufferParams const & bparams, pos_type start,
280 BOOST_ASSERT(start >= 0 && start <= size());
281 BOOST_ASSERT(end > start && end <= size() + 1);
283 for (pos_type pos = start; pos < end; ++pos) {
284 switch (lookupChange(pos).type) {
285 case Change::UNCHANGED:
286 // accept changes in nested inset
287 if (pos < size() && isInset(pos))
288 getInset(pos)->acceptChanges(bparams);
292 case Change::INSERTED:
293 d->changes_.set(Change(Change::UNCHANGED), pos);
294 // also accept changes in nested inset
295 if (pos < size() && isInset(pos)) {
296 getInset(pos)->acceptChanges(bparams);
300 case Change::DELETED:
301 // Suppress access to non-existent
302 // "end-of-paragraph char"
304 eraseChar(pos, false);
315 void Paragraph::rejectChanges(BufferParams const & bparams,
316 pos_type start, pos_type end)
318 BOOST_ASSERT(start >= 0 && start <= size());
319 BOOST_ASSERT(end > start && end <= size() + 1);
321 for (pos_type pos = start; pos < end; ++pos) {
322 switch (lookupChange(pos).type) {
323 case Change::UNCHANGED:
324 // reject changes in nested inset
325 if (pos < size() && isInset(pos)) {
326 getInset(pos)->rejectChanges(bparams);
330 case Change::INSERTED:
331 // Suppress access to non-existent
332 // "end-of-paragraph char"
334 eraseChar(pos, false);
340 case Change::DELETED:
341 d->changes_.set(Change(Change::UNCHANGED), pos);
343 // Do NOT reject changes within a deleted inset!
344 // There may be insertions of a co-author inside of it!
352 Paragraph::value_type Paragraph::Private::getChar(pos_type pos) const
354 BOOST_ASSERT(pos >= 0 && pos <= size());
356 return owner_->getChar(pos);
360 void Paragraph::Private::insertChar(pos_type pos, value_type c,
361 Change const & change)
363 BOOST_ASSERT(pos >= 0 && pos <= size());
366 changes_.insert(change, pos);
368 // This is actually very common when parsing buffers (and
369 // maybe inserting ascii text)
371 // when appending characters, no need to update tables
372 owner_->text_.push_back(c);
376 owner_->text_.insert(owner_->text_.begin() + pos, c);
378 // Update the font table.
379 fontlist_.increasePosAfterPos(pos);
382 insetlist_.increasePosAfterPos(pos);
386 void Paragraph::insertInset(pos_type pos, Inset * inset,
387 Change const & change)
390 BOOST_ASSERT(pos >= 0 && pos <= size());
392 d->insertChar(pos, META_INSET, change);
393 BOOST_ASSERT(text_[pos] == META_INSET);
395 // Add a new entry in the insetlist_.
396 d->insetlist_.insert(inset, pos);
400 bool Paragraph::eraseChar(pos_type pos, bool trackChanges)
402 BOOST_ASSERT(pos >= 0 && pos <= size());
404 // keep the logic here in sync with the logic of isMergedOnEndOfParDeletion()
407 Change change = d->changes_.lookup(pos);
409 // set the character to DELETED if
410 // a) it was previously unchanged or
411 // b) it was inserted by a co-author
413 if (change.type == Change::UNCHANGED ||
414 (change.type == Change::INSERTED && change.author != 0)) {
415 setChange(pos, Change(Change::DELETED));
419 if (change.type == Change::DELETED)
423 // Don't physically access the imaginary end-of-paragraph character.
424 // eraseChar() can only mark it as DELETED. A physical deletion of
425 // end-of-par must be handled externally.
431 d->changes_.erase(pos);
433 // if it is an inset, delete the inset entry
434 if (text_[pos] == Paragraph::META_INSET)
435 d->insetlist_.erase(pos);
437 text_.erase(text_.begin() + pos);
439 // Update the fontlist_
440 d->fontlist_.erase(pos);
442 // Update the insetlist_
443 d->insetlist_.decreasePosAfterPos(pos);
449 int Paragraph::eraseChars(pos_type start, pos_type end, bool trackChanges)
451 BOOST_ASSERT(start >= 0 && start <= size());
452 BOOST_ASSERT(end >= start && end <= size() + 1);
455 for (pos_type count = end - start; count; --count) {
456 if (!eraseChar(i, trackChanges))
463 int Paragraph::Private::latexSurrogatePair(odocstream & os, value_type c,
464 value_type next, Encoding const & encoding)
466 // Writing next here may circumvent a possible font change between
467 // c and next. Since next is only output if it forms a surrogate pair
468 // with c we can ignore this:
469 // A font change inside a surrogate pair does not make sense and is
470 // hopefully impossible to input.
471 // FIXME: change tracking
472 // Is this correct WRT change tracking?
473 docstring const latex1 = encoding.latexChar(next);
474 docstring const latex2 = encoding.latexChar(c);
475 os << latex1 << '{' << latex2 << '}';
476 return latex1.length() + latex2.length() + 2;
480 bool Paragraph::Private::simpleTeXBlanks(Encoding const & encoding,
481 odocstream & os, TexRow & texrow,
483 unsigned int & column,
485 Layout const & style)
490 if (i + 1 < size()) {
491 char_type next = getChar(i + 1);
492 if (Encodings::isCombiningChar(next)) {
493 // This space has an accent, so we must always output it.
494 column += latexSurrogatePair(os, ' ', next, encoding) - 1;
500 if (lyxrc.plaintext_linelen > 0
501 && column > lyxrc.plaintext_linelen
503 && getChar(i - 1) != ' '
505 // same in FreeSpacing mode
506 && !owner_->isFreeSpacing()
507 // In typewriter mode, we want to avoid
508 // ! . ? : at the end of a line
509 && !(font.family() == Font::TYPEWRITER_FAMILY
510 && (getChar(i - 1) == '.'
511 || getChar(i - 1) == '?'
512 || getChar(i - 1) == ':'
513 || getChar(i - 1) == '!'))) {
516 texrow.start(owner_->id(), i + 1);
518 } else if (style.free_spacing) {
527 int Paragraph::Private::knownLangChars(odocstream & os,
530 Change & runningChange,
531 Encoding const & encoding,
534 // When the character is marked by the proper language, we simply
535 // get its code point in some encoding, otherwise we get the
536 // translation specified in the unicodesymbols file, which is
537 // something like "\textLANG{<spec>}". So, we have to retain
538 // "\textLANG{<spec>" for the first char but only "<spec>" for
539 // all subsequent chars.
540 docstring const latex1 = rtrim(encoding.latexChar(c), "}");
541 int length = latex1.length();
543 while (i + 1 < size()) {
544 char_type next = getChar(i + 1);
545 // Stop here if next character belongs to another
546 // language or there is a change tracking status.
547 if (!Encodings::isKnownLangChar(next, preamble) ||
548 runningChange != owner_->lookupChange(i + 1))
552 FontList::const_iterator cit = fontlist_.begin();
553 FontList::const_iterator end = fontlist_.end();
554 for (; cit != end; ++cit) {
555 if (cit->pos() >= i && !found) {
556 prev_font = cit->font();
559 if (cit->pos() >= i + 1)
562 // Stop here if there is a font attribute change.
563 if (found && cit != end && prev_font != cit->font())
565 docstring const latex = rtrim(encoding.latexChar(next), "}");
566 docstring::size_type const j =
567 latex.find_first_of(from_ascii("{"));
568 if (j == docstring::npos) {
570 length += latex.length();
572 os << latex.substr(j + 1);
573 length += latex.substr(j + 1).length();
577 // When the proper language is set, we are simply passed a code
578 // point, so we should not try to close the \textLANG command.
579 if (prefixIs(latex1, from_ascii("\\" + preamble))) {
587 bool Paragraph::Private::isTextAt(string const & str, pos_type pos) const
589 pos_type const len = str.length();
591 // is the paragraph large enough?
592 if (pos + len > size())
595 // does the wanted text start at point?
596 for (string::size_type i = 0; i < str.length(); ++i) {
597 // Caution: direct comparison of characters works only
598 // because str is pure ASCII.
599 if (str[i] != owner_->text_[pos + i])
603 return fontlist_.hasChangeInRange(pos, len);
607 void Paragraph::Private::simpleTeXSpecialChars(Buffer const & buf,
608 BufferParams const & bparams,
611 OutputParams & runparams,
614 Font const & outerfont,
616 Change & running_change,
617 Layout const & style,
619 unsigned int & column,
622 if (style.pass_thru) {
623 if (c != Paragraph::META_INSET) {
625 // FIXME UNICODE: This can fail if c cannot
626 // be encoded in the current encoding.
629 owner_->getInset(i)->plaintext(buf, os, runparams);
633 // Two major modes: LaTeX or plain
634 // Handle here those cases common to both modes
635 // and then split to handle the two modes separately.
637 case Paragraph::META_INSET: {
638 Inset * inset = owner_->getInset(i);
640 // FIXME: remove this check
644 // FIXME: move this to InsetNewline::latex
645 if (inset->lyxCode() == NEWLINE_CODE) {
646 // newlines are handled differently here than
647 // the default in simpleTeXSpecialChars().
648 if (!style.newline_allowed) {
652 column += running_font.latexWriteEndChanges(
653 os, bparams, runparams,
658 if (running_font.family() == Font::TYPEWRITER_FAMILY)
661 basefont = owner_->getLayoutFont(bparams, outerfont);
662 running_font = basefont;
664 if (runparams.moving_arg)
670 texrow.start(owner_->id(), i + 1);
675 if (owner_->lookupChange(i).type == Change::DELETED) {
676 if( ++runparams.inDeletedInset == 1)
677 runparams.changeOfDeletedInset = owner_->lookupChange(i);
680 if (inset->canTrackChanges()) {
681 column += Changes::latexMarkChange(os, bparams, running_change,
682 Change(Change::UNCHANGED));
683 running_change = Change(Change::UNCHANGED);
687 odocstream::pos_type const len = os.tellp();
689 if ((inset->lyxCode() == GRAPHICS_CODE
690 || inset->lyxCode() == MATH_CODE
691 || inset->lyxCode() == HYPERLINK_CODE)
692 && running_font.isRightToLeft()) {
693 if (running_font.language()->lang() == "farsi")
700 // FIXME: Bug: we can have an empty font change here!
701 // if there has just been a font change, we are going to close it
702 // right now, which means stupid latex code like \textsf{}. AFAIK,
703 // this does not harm dvi output. A minor bug, thus (JMarc)
704 // Some insets cannot be inside a font change command.
705 // However, even such insets *can* be placed in \L or \R
706 // or their equivalents (for RTL language switches), so we don't
707 // close the language in those cases.
708 // ArabTeX, though, cannot handle this special behavior, it seems.
709 bool arabtex = basefont.language()->lang() == "arabic_arabtex" ||
710 running_font.language()->lang() == "arabic_arabtex";
711 if (open_font && inset->noFontChange()) {
712 bool closeLanguage = arabtex ||
713 basefont.isRightToLeft() == running_font.isRightToLeft();
714 unsigned int count = running_font.latexWriteEndChanges(
715 os, bparams, runparams,
716 basefont, basefont, closeLanguage);
718 // if any font properties were closed, update the running_font,
719 // making sure, however, to leave the language as it was
721 // FIXME: probably a better way to keep track of the old
722 // language, than copying the entire font?
723 Font const copy_font(running_font);
724 basefont = owner_->getLayoutFont(bparams, outerfont);
725 running_font = basefont;
727 running_font.setLanguage(copy_font.language());
728 // leave font open if language is still open
729 open_font = (running_font.language() == basefont.language());
731 runparams.local_font = &basefont;
735 int tmp = inset->latex(buf, os, runparams);
738 if (running_font.language()->lang() == "farsi")
745 for (int j = 0; j < tmp; ++j) {
748 texrow.start(owner_->id(), i + 1);
751 column += os.tellp() - len;
754 if (owner_->lookupChange(i).type == Change::DELETED) {
755 --runparams.inDeletedInset;
761 // And now for the special cases within each mode
765 os << "\\textbackslash{}";
769 case '|': case '<': case '>':
770 // In T1 encoding, these characters exist
771 if (lyxrc.fontenc == "T1") {
773 //... but we should avoid ligatures
774 if ((c == '>' || c == '<')
776 && getChar(i + 1) == c) {
777 //os << "\\textcompwordmark{}";
779 // Jean-Marc, have a look at
780 // this. I think this works
788 // Typewriter font also has them
789 if (running_font.family() == Font::TYPEWRITER_FAMILY) {
793 // Otherwise, we use what LaTeX
797 os << "\\textless{}";
801 os << "\\textgreater{}";
811 case '-': // "--" in Typewriter mode -> "-{}-"
812 if (i <= size() - 2 &&
813 getChar(i + 1) == '-' &&
814 running_font.family() == Font::TYPEWRITER_FAMILY) {
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
863 // I assume this is hack treating typewriter as verbatim
864 // FIXME UNICODE: This can fail if c cannot be encoded
865 // in the current encoding.
866 if (running_font.family() == Font::TYPEWRITER_FAMILY) {
875 // FIXME: if we have "LaTeX" with a font
876 // change in the middle (before the 'T', then
877 // the "TeX" part is still special cased.
878 // Really we should only operate this on
879 // "words" for some definition of word
883 for (; pnr < phrases_nr; ++pnr) {
884 if (isTextAt(special_phrases[pnr].phrase, i)) {
885 os << special_phrases[pnr].macro;
886 i += special_phrases[pnr].phrase.length() - 1;
887 column += special_phrases[pnr].macro.length() - 1;
892 if (pnr == phrases_nr && c != '\0') {
893 Encoding const & encoding = *(runparams.encoding);
894 if (i + 1 < size()) {
895 char_type next = getChar(i + 1);
896 if (Encodings::isCombiningChar(next)) {
897 column += latexSurrogatePair(os, c, next, encoding) - 1;
903 if (Encodings::isKnownLangChar(c, preamble)) {
905 knownLangChars(os, c, preamble,
910 docstring const latex = encoding.latexChar(c);
911 if (latex.length() > 1 &&
912 latex[latex.length() - 1] != '}') {
913 // Prevent eating of a following
914 // space or command corruption by
915 // following characters
916 column += latex.length() + 1;
919 column += latex.length() - 1;
929 void Paragraph::Private::validate(LaTeXFeatures & features,
930 Layout const & layout) const
933 if (!params_.spacing().isDefault())
934 features.require("setspace");
937 features.useLayout(layout.name());
940 fontlist_.validate(features);
942 // then the indentation
943 if (!params_.leftIndent().zero())
944 features.require("ParagraphLeftIndent");
947 InsetList::const_iterator icit = insetlist_.begin();
948 InsetList::const_iterator iend = insetlist_.end();
949 for (; icit != iend; ++icit) {
951 icit->inset->validate(features);
952 if (layout.needprotect &&
953 icit->inset->lyxCode() == FOOT_CODE)
954 features.require("NeedLyXFootnoteCode");
959 for (pos_type i = 0; i < size() ; ++i) {
960 for (size_t pnr = 0; pnr < phrases_nr; ++pnr) {
961 if (!special_phrases[pnr].builtin
962 && isTextAt(special_phrases[pnr].phrase, i)) {
963 features.require(special_phrases[pnr].phrase);
967 Encodings::validate(getChar(i), features);
975 /////////////////////////////////////////////////////////////////////
979 /////////////////////////////////////////////////////////////////////
983 Paragraph::Paragraph()
984 : begin_of_body_(0), d(new Paragraph::Private(this))
991 Paragraph::Paragraph(Paragraph const & par)
992 : itemdepth(par.itemdepth),
993 layout_(par.layout_),
994 text_(par.text_), begin_of_body_(par.begin_of_body_),
995 d(new Paragraph::Private(*par.d, this))
1000 Paragraph & Paragraph::operator=(Paragraph const & par)
1002 // needed as we will destroy the private part before copying it
1004 itemdepth = par.itemdepth;
1005 layout_ = par.layout();
1007 begin_of_body_ = par.begin_of_body_;
1010 d = new Private(*par.d, this);
1016 Paragraph::~Paragraph()
1022 void Paragraph::write(Buffer const & buf, ostream & os,
1023 BufferParams const & bparams,
1024 depth_type & dth) const
1026 // The beginning or end of a deeper (i.e. nested) area?
1027 if (dth != params().depth()) {
1028 if (params().depth() > dth) {
1029 while (params().depth() > dth) {
1030 os << "\n\\begin_deeper";
1034 while (params().depth() < dth) {
1035 os << "\n\\end_deeper";
1041 // First write the layout
1042 os << "\n\\begin_layout " << to_utf8(layout()->name()) << '\n';
1046 Font font1(Font::ALL_INHERIT, bparams.language);
1048 Change running_change = Change(Change::UNCHANGED);
1051 for (pos_type i = 0; i <= size(); ++i) {
1053 Change change = lookupChange(i);
1054 Changes::lyxMarkChange(os, column, running_change, change);
1055 running_change = change;
1060 // Write font changes
1061 Font font2 = getFontSettings(bparams, i);
1062 if (font2 != font1) {
1063 font2.lyxWriteChanges(font1, os);
1068 value_type const c = getChar(i);
1072 Inset const * inset = getInset(i);
1074 if (inset->directWrite()) {
1075 // international char, let it write
1076 // code directly so it's shorter in
1078 inset->write(buf, os);
1082 os << "\\begin_inset ";
1083 inset->write(buf, os);
1084 os << "\n\\end_inset\n\n";
1090 os << "\n\\backslash\n";
1094 if (i + 1 < size() && getChar(i + 1) == ' ') {
1101 if ((column > 70 && c == ' ')
1106 // this check is to amend a bug. LyX sometimes
1107 // inserts '\0' this could cause problems.
1109 std::vector<char> tmp = ucs4_to_utf8(c);
1110 tmp.push_back('\0');
1113 lyxerr << "ERROR (Paragraph::writeFile):"
1114 " NULL char in structure." << endl;
1120 os << "\n\\end_layout\n";
1124 void Paragraph::validate(LaTeXFeatures & features) const
1126 d->validate(features, *layout());
1130 void Paragraph::insert(pos_type start, docstring const & str,
1131 Font const & font, Change const & change)
1133 for (size_t i = 0, n = str.size(); i != n ; ++i)
1134 insertChar(start + i, str[i], font, change);
1138 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c,
1141 d->insertChar(pos, c, Change(trackChanges ?
1142 Change::INSERTED : Change::UNCHANGED));
1146 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c,
1147 Font const & font, bool trackChanges)
1149 d->insertChar(pos, c, Change(trackChanges ?
1150 Change::INSERTED : Change::UNCHANGED));
1155 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c,
1156 Font const & font, Change const & change)
1158 d->insertChar(pos, c, change);
1163 void Paragraph::insertInset(pos_type pos, Inset * inset,
1164 Font const & font, Change const & change)
1166 insertInset(pos, inset, change);
1167 // Set the font/language of the inset...
1172 bool Paragraph::insetAllowed(InsetCode code)
1174 return !d->inset_owner_ || d->inset_owner_->insetAllowed(code);
1178 // Gets uninstantiated font setting at position.
1179 Font const Paragraph::getFontSettings(BufferParams const & bparams,
1183 lyxerr << " pos: " << pos << " size: " << size() << endl;
1184 BOOST_ASSERT(pos <= size());
1187 FontList::const_iterator cit = d->fontlist_.fontIterator(pos);
1188 if (cit != d->fontlist_.end())
1191 if (pos == size() && !empty())
1192 return getFontSettings(bparams, pos - 1);
1194 return Font(Font::ALL_INHERIT, getParLanguage(bparams));
1198 FontSpan Paragraph::fontSpan(pos_type pos) const
1200 BOOST_ASSERT(pos <= size());
1203 FontList::const_iterator cit = d->fontlist_.begin();
1204 FontList::const_iterator end = d->fontlist_.end();
1205 for (; cit != end; ++cit) {
1206 if (cit->pos() >= pos) {
1207 if (pos >= beginOfBody())
1208 return FontSpan(std::max(start, beginOfBody()),
1211 return FontSpan(start,
1212 std::min(beginOfBody() - 1,
1215 start = cit->pos() + 1;
1218 // This should not happen, but if so, we take no chances.
1219 //lyxerr << "Paragraph::getEndPosOfFontSpan: This should not happen!"
1221 return FontSpan(pos, pos);
1225 // Gets uninstantiated font setting at position 0
1226 Font const Paragraph::getFirstFontSettings(BufferParams const & bparams) const
1228 if (!empty() && !d->fontlist_.empty())
1229 return d->fontlist_.begin()->font();
1231 return Font(Font::ALL_INHERIT, bparams.language);
1235 // Gets the fully instantiated font at a given position in a paragraph
1236 // This is basically the same function as Text::GetFont() in text2.cpp.
1237 // The difference is that this one is used for generating the LaTeX file,
1238 // and thus cosmetic "improvements" are disallowed: This has to deliver
1239 // the true picture of the buffer. (Asger)
1240 Font const Paragraph::getFont(BufferParams const & bparams, pos_type pos,
1241 Font const & outerfont) const
1243 BOOST_ASSERT(pos >= 0);
1245 Font font = getFontSettings(bparams, pos);
1247 pos_type const body_pos = beginOfBody();
1249 font.realize(layout_->labelfont);
1251 font.realize(layout_->font);
1253 font.realize(outerfont);
1254 font.realize(bparams.getFont());
1260 Font const Paragraph::getLabelFont
1261 (BufferParams const & bparams, Font const & outerfont) const
1263 Font tmpfont = layout()->labelfont;
1264 tmpfont.setLanguage(getParLanguage(bparams));
1265 tmpfont.realize(outerfont);
1266 tmpfont.realize(bparams.getFont());
1271 Font const Paragraph::getLayoutFont
1272 (BufferParams const & bparams, Font const & outerfont) const
1274 Font tmpfont = layout()->font;
1275 tmpfont.setLanguage(getParLanguage(bparams));
1276 tmpfont.realize(outerfont);
1277 tmpfont.realize(bparams.getFont());
1282 /// Returns the height of the highest font in range
1283 Font_size Paragraph::highestFontInRange
1284 (pos_type startpos, pos_type endpos, Font_size def_size) const
1286 return d->fontlist_.highestInRange(startpos, endpos, def_size);
1290 Paragraph::value_type
1291 Paragraph::getUChar(BufferParams const & bparams, pos_type pos) const
1293 value_type c = getChar(pos);
1294 if (!lyxrc.rtl_support)
1324 if (uc != c && getFontSettings(bparams, pos).isRightToLeft())
1331 void Paragraph::setFont(pos_type pos, Font const & font)
1333 BOOST_ASSERT(pos <= size());
1335 // First, reduce font against layout/label font
1336 // Update: The setCharFont() routine in text2.cpp already
1337 // reduces font, so we don't need to do that here. (Asger)
1339 d->fontlist_.set(pos, font);
1343 void Paragraph::makeSameLayout(Paragraph const & par)
1345 layout(par.layout());
1347 d->params_ = par.params();
1351 bool Paragraph::stripLeadingSpaces(bool trackChanges)
1353 if (isFreeSpacing())
1359 while (pos < size() && (isNewline(pos) || isLineSeparator(pos))) {
1360 if (eraseChar(pos, trackChanges))
1366 return count > 0 || pos > 0;
1370 bool Paragraph::hasSameLayout(Paragraph const & par) const
1372 return par.layout() == layout() && d->params_.sameLayout(par.params());
1376 depth_type Paragraph::getDepth() const
1378 return params().depth();
1382 depth_type Paragraph::getMaxDepthAfter() const
1384 if (layout()->isEnvironment())
1385 return params().depth() + 1;
1387 return params().depth();
1391 char Paragraph::getAlign() const
1393 if (params().align() == LYX_ALIGN_LAYOUT)
1394 return layout()->align;
1396 return params().align();
1400 docstring const & Paragraph::getLabelstring() const
1402 return params().labelString();
1406 // the next two functions are for the manual labels
1407 docstring const Paragraph::getLabelWidthString() const
1409 if (layout()->margintype == MARGIN_MANUAL)
1410 return params().labelWidthString();
1412 return _("Senseless with this layout!");
1416 void Paragraph::setLabelWidthString(docstring const & s)
1418 params().labelWidthString(s);
1422 docstring const Paragraph::translateIfPossible(docstring const & s,
1423 BufferParams const & bparams) const
1425 if (!support::isAscii(s) || s.empty()) {
1426 // This must be a user defined layout. We cannot translate
1427 // this, since gettext accepts only ascii keys.
1430 // Probably standard layout, try to translate
1431 Messages & m = getMessages(getParLanguage(bparams)->code());
1432 return m.get(to_ascii(s));
1436 docstring Paragraph::expandLabel(LayoutPtr const & layout,
1437 BufferParams const & bparams, bool process_appendix) const
1439 TextClass const & tclass = bparams.getTextClass();
1442 if (process_appendix && params().appendix())
1443 fmt = translateIfPossible(layout->labelstring_appendix(),
1446 fmt = translateIfPossible(layout->labelstring(), bparams);
1448 if (fmt.empty() && layout->labeltype == LABEL_COUNTER
1449 && !layout->counter.empty())
1450 fmt = "\\the" + layout->counter;
1452 // handle 'inherited level parts' in 'fmt',
1453 // i.e. the stuff between '@' in '@Section@.\arabic{subsection}'
1454 size_t const i = fmt.find('@', 0);
1455 if (i != docstring::npos) {
1456 size_t const j = fmt.find('@', i + 1);
1457 if (j != docstring::npos) {
1458 docstring parent(fmt, i + 1, j - i - 1);
1459 docstring label = from_ascii("??");
1460 if (tclass.hasLayout(parent))
1461 docstring label = expandLabel(tclass[parent], bparams,
1463 fmt = docstring(fmt, 0, i) + label
1464 + docstring(fmt, j + 1, docstring::npos);
1468 return tclass.counters().counterLabel(fmt);
1472 void Paragraph::applyLayout(LayoutPtr const & new_layout)
1475 LyXAlignment const oldAlign = params().align();
1477 if (!(oldAlign & layout()->alignpossible)) {
1478 frontend::Alert::warning(_("Alignment not permitted"),
1479 _("The new layout does not permit the alignment previously used.\nSetting to default."));
1480 params().align(LYX_ALIGN_LAYOUT);
1485 pos_type Paragraph::beginOfBody() const
1487 return begin_of_body_;
1491 void Paragraph::setBeginOfBody()
1493 if (layout()->labeltype != LABEL_MANUAL) {
1498 // Unroll the first two cycles of the loop
1499 // and remember the previous character to
1500 // remove unnecessary getChar() calls
1502 pos_type end = size();
1503 if (i < end && !isNewline(i)) {
1505 char_type previous_char = 0;
1508 previous_char = text_[i];
1509 if (!isNewline(i)) {
1511 while (i < end && previous_char != ' ') {
1516 previous_char = temp;
1526 // returns -1 if inset not found
1527 int Paragraph::getPositionOfInset(Inset const * inset) const
1530 InsetList::const_iterator it = d->insetlist_.begin();
1531 InsetList::const_iterator end = d->insetlist_.end();
1532 for (; it != end; ++it)
1533 if (it->inset == inset)
1539 InsetBibitem * Paragraph::bibitem() const
1541 if (!d->insetlist_.empty()) {
1542 Inset * inset = d->insetlist_.begin()->inset;
1543 if (inset->lyxCode() == BIBITEM_CODE)
1544 return static_cast<InsetBibitem *>(inset);
1550 bool Paragraph::forceDefaultParagraphs() const
1552 return inInset() && inInset()->forceDefaultParagraphs(0);
1558 // paragraphs inside floats need different alignment tags to avoid
1561 bool noTrivlistCentering(InsetCode code)
1563 return code == FLOAT_CODE || code == WRAP_CODE;
1567 string correction(string const & orig)
1569 if (orig == "flushleft")
1570 return "raggedright";
1571 if (orig == "flushright")
1572 return "raggedleft";
1573 if (orig == "center")
1579 string const corrected_env(string const & suffix, string const & env,
1582 string output = suffix + "{";
1583 if (noTrivlistCentering(code))
1584 output += correction(env);
1588 if (suffix == "\\begin")
1594 void adjust_row_column(string const & str, TexRow & texrow, int & column)
1596 if (!contains(str, "\n"))
1597 column += str.size();
1601 column = rsplit(str, tmp, '\n').size();
1608 // This could go to ParagraphParameters if we want to
1609 int Paragraph::startTeXParParams(BufferParams const & bparams,
1610 odocstream & os, TexRow & texrow,
1611 bool moving_arg) const
1615 if (params().noindent()) {
1616 os << "\\noindent ";
1620 LyXAlignment const curAlign = params().align();
1622 if (curAlign == layout()->align)
1626 case LYX_ALIGN_NONE:
1627 case LYX_ALIGN_BLOCK:
1628 case LYX_ALIGN_LAYOUT:
1629 case LYX_ALIGN_SPECIAL:
1631 case LYX_ALIGN_LEFT:
1632 case LYX_ALIGN_RIGHT:
1633 case LYX_ALIGN_CENTER:
1642 case LYX_ALIGN_NONE:
1643 case LYX_ALIGN_BLOCK:
1644 case LYX_ALIGN_LAYOUT:
1645 case LYX_ALIGN_SPECIAL:
1647 case LYX_ALIGN_LEFT: {
1649 if (getParLanguage(bparams)->babel() != "hebrew")
1650 output = corrected_env("\\begin", "flushleft", ownerCode());
1652 output = corrected_env("\\begin", "flushright", ownerCode());
1653 os << from_ascii(output);
1654 adjust_row_column(output, texrow, column);
1656 } case LYX_ALIGN_RIGHT: {
1658 if (getParLanguage(bparams)->babel() != "hebrew")
1659 output = corrected_env("\\begin", "flushright", ownerCode());
1661 output = corrected_env("\\begin", "flushleft", ownerCode());
1662 os << from_ascii(output);
1663 adjust_row_column(output, texrow, column);
1665 } case LYX_ALIGN_CENTER: {
1667 output = corrected_env("\\begin", "center", ownerCode());
1668 os << from_ascii(output);
1669 adjust_row_column(output, texrow, column);
1678 // This could go to ParagraphParameters if we want to
1679 int Paragraph::endTeXParParams(BufferParams const & bparams,
1680 odocstream & os, TexRow & texrow,
1681 bool moving_arg) const
1685 switch (params().align()) {
1686 case LYX_ALIGN_NONE:
1687 case LYX_ALIGN_BLOCK:
1688 case LYX_ALIGN_LAYOUT:
1689 case LYX_ALIGN_SPECIAL:
1691 case LYX_ALIGN_LEFT:
1692 case LYX_ALIGN_RIGHT:
1693 case LYX_ALIGN_CENTER:
1701 switch (params().align()) {
1702 case LYX_ALIGN_NONE:
1703 case LYX_ALIGN_BLOCK:
1704 case LYX_ALIGN_LAYOUT:
1705 case LYX_ALIGN_SPECIAL:
1707 case LYX_ALIGN_LEFT: {
1709 if (getParLanguage(bparams)->babel() != "hebrew")
1710 output = corrected_env("\n\\par\\end", "flushleft", ownerCode());
1712 output = corrected_env("\n\\par\\end", "flushright", ownerCode());
1713 os << from_ascii(output);
1714 adjust_row_column(output, texrow, column);
1716 } case LYX_ALIGN_RIGHT: {
1718 if (getParLanguage(bparams)->babel() != "hebrew")
1719 output = corrected_env("\n\\par\\end", "flushright", ownerCode());
1721 output = corrected_env("\n\\par\\end", "flushleft", ownerCode());
1722 os << from_ascii(output);
1723 adjust_row_column(output, texrow, column);
1725 } case LYX_ALIGN_CENTER: {
1727 output = corrected_env("\n\\par\\end", "center", ownerCode());
1728 os << from_ascii(output);
1729 adjust_row_column(output, texrow, column);
1738 // This one spits out the text of the paragraph
1739 bool Paragraph::simpleTeXOnePar(Buffer const & buf,
1740 BufferParams const & bparams,
1741 Font const & outerfont,
1742 odocstream & os, TexRow & texrow,
1743 OutputParams const & runparams) const
1745 LYXERR(Debug::LATEX) << "SimpleTeXOnePar... " << this << endl;
1747 bool return_value = false;
1751 // well we have to check if we are in an inset with unlimited
1752 // length (all in one row) if that is true then we don't allow
1753 // any special options in the paragraph and also we don't allow
1754 // any environment other than the default layout of the text class
1756 bool asdefault = forceDefaultParagraphs();
1759 style = bparams.getTextClass().defaultLayout();
1764 // Current base font for all inherited font changes, without any
1765 // change caused by an individual character, except for the language:
1766 // It is set to the language of the first character.
1767 // As long as we are in the label, this font is the base font of the
1768 // label. Before the first body character it is set to the base font
1772 // Maybe we have to create a optional argument.
1773 pos_type body_pos = beginOfBody();
1774 unsigned int column = 0;
1777 // the optional argument is kept in curly brackets in
1778 // case it contains a ']'
1781 basefont = getLabelFont(bparams, outerfont);
1783 basefont = getLayoutFont(bparams, outerfont);
1786 // Which font is currently active?
1787 Font running_font(basefont);
1788 // Do we have an open font change?
1789 bool open_font = false;
1791 Change runningChange = Change(Change::UNCHANGED);
1793 texrow.start(id(), 0);
1795 // if the paragraph is empty, the loop will not be entered at all
1797 if (style->isCommand()) {
1802 column += startTeXParParams(bparams, os, texrow,
1803 runparams.moving_arg);
1806 for (pos_type i = 0; i < size(); ++i) {
1807 // First char in paragraph or after label?
1808 if (i == body_pos) {
1811 column += running_font.latexWriteEndChanges(
1812 os, bparams, runparams,
1813 basefont, basefont);
1816 basefont = getLayoutFont(bparams, outerfont);
1817 running_font = basefont;
1819 column += Changes::latexMarkChange(os, bparams,
1820 runningChange, Change(Change::UNCHANGED));
1821 runningChange = Change(Change::UNCHANGED);
1826 if (style->isCommand()) {
1832 column += startTeXParParams(bparams, os,
1834 runparams.moving_arg);
1837 Change const & change = runparams.inDeletedInset ? runparams.changeOfDeletedInset
1840 if (bparams.outputChanges && runningChange != change) {
1842 column += running_font.latexWriteEndChanges(
1843 os, bparams, runparams, basefont, basefont);
1846 basefont = getLayoutFont(bparams, outerfont);
1847 running_font = basefont;
1849 column += Changes::latexMarkChange(os, bparams, runningChange, change);
1850 runningChange = change;
1853 // do not output text which is marked deleted
1854 // if change tracking output is disabled
1855 if (!bparams.outputChanges && change.type == Change::DELETED) {
1861 value_type const c = getChar(i);
1863 // Fully instantiated font
1864 Font const font = getFont(bparams, i, outerfont);
1866 Font const last_font = running_font;
1868 // Do we need to close the previous font?
1870 (font != running_font ||
1871 font.language() != running_font.language()))
1873 column += running_font.latexWriteEndChanges(
1874 os, bparams, runparams, basefont,
1875 (i == body_pos-1) ? basefont : font);
1876 running_font = basefont;
1880 // Switch file encoding if necessary
1881 if (runparams.encoding->package() == Encoding::inputenc &&
1882 font.language()->encoding()->package() == Encoding::inputenc) {
1883 std::pair<bool, int> const enc_switch = switchEncoding(os, bparams,
1884 runparams.moving_arg, *(runparams.encoding),
1885 *(font.language()->encoding()));
1886 if (enc_switch.first) {
1887 column += enc_switch.second;
1888 runparams.encoding = font.language()->encoding();
1892 // Do we need to change font?
1893 if ((font != running_font ||
1894 font.language() != running_font.language()) &&
1897 odocstringstream ods;
1898 column += font.latexWriteStartChanges(ods, bparams,
1899 runparams, basefont,
1901 running_font = font;
1903 docstring fontchange = ods.str();
1904 // check if the fontchange ends with a trailing blank
1905 // (like "\small " (see bug 3382)
1906 if (suffixIs(fontchange, ' ') && c == ' ')
1907 os << fontchange.substr(0, fontchange.size() - 1)
1908 << from_ascii("{}");
1914 // Do not print the separation of the optional argument
1915 // if style->pass_thru is false. This works because
1916 // simpleTeXSpecialChars ignores spaces if
1917 // style->pass_thru is false.
1918 if (i != body_pos - 1) {
1919 if (d->simpleTeXBlanks(
1920 *(runparams.encoding), os, texrow,
1921 i, column, font, *style))
1922 // A surrogate pair was output. We
1923 // must not call simpleTeXSpecialChars
1924 // in this iteration, since
1925 // simpleTeXBlanks incremented i, and
1926 // simpleTeXSpecialChars would output
1927 // the combining character again.
1932 OutputParams rp = runparams;
1933 rp.free_spacing = style->free_spacing;
1934 rp.local_font = &font;
1935 rp.intitle = style->intitle;
1936 d->simpleTeXSpecialChars(buf, bparams, os,
1937 texrow, rp, running_font,
1938 basefont, outerfont, open_font,
1939 runningChange, *style, i, column, c);
1941 // Set the encoding to that returned from simpleTeXSpecialChars (see
1942 // comment for encoding member in OutputParams.h)
1943 runparams.encoding = rp.encoding;
1946 // If we have an open font definition, we have to close it
1948 #ifdef FIXED_LANGUAGE_END_DETECTION
1951 .latexWriteEndChanges(os, bparams, runparams,
1953 next_->getFont(bparams, 0, outerfont));
1955 running_font.latexWriteEndChanges(os, bparams,
1956 runparams, basefont, basefont);
1959 //FIXME: For now we ALWAYS have to close the foreign font settings if they are
1960 //FIXME: there as we start another \selectlanguage with the next paragraph if
1961 //FIXME: we are in need of this. This should be fixed sometime (Jug)
1962 running_font.latexWriteEndChanges(os, bparams, runparams,
1963 basefont, basefont);
1967 column += Changes::latexMarkChange(os, bparams, runningChange, Change(Change::UNCHANGED));
1969 // Needed if there is an optional argument but no contents.
1970 if (body_pos > 0 && body_pos == size()) {
1972 return_value = false;
1976 column += endTeXParParams(bparams, os, texrow,
1977 runparams.moving_arg);
1980 LYXERR(Debug::LATEX) << "SimpleTeXOnePar...done " << this << endl;
1981 return return_value;
1998 string tag_name(PAR_TAG const & pt) {
2000 case PAR_NONE: return "!-- --";
2001 case TT: return "tt";
2002 case SF: return "sf";
2003 case BF: return "bf";
2004 case IT: return "it";
2005 case SL: return "sl";
2006 case EM: return "em";
2013 void operator|=(PAR_TAG & p1, PAR_TAG const & p2)
2015 p1 = static_cast<PAR_TAG>(p1 | p2);
2020 void reset(PAR_TAG & p1, PAR_TAG const & p2)
2022 p1 = static_cast<PAR_TAG>(p1 & ~p2);
2028 bool Paragraph::emptyTag() const
2030 for (pos_type i = 0; i < size(); ++i) {
2032 Inset const * inset = getInset(i);
2033 InsetCode lyx_code = inset->lyxCode();
2034 if (lyx_code != TOC_CODE &&
2035 lyx_code != INCLUDE_CODE &&
2036 lyx_code != GRAPHICS_CODE &&
2037 lyx_code != ERT_CODE &&
2038 lyx_code != LISTINGS_CODE &&
2039 lyx_code != FLOAT_CODE &&
2040 lyx_code != TABULAR_CODE) {
2044 value_type c = getChar(i);
2045 if (c != ' ' && c != '\t')
2053 string Paragraph::getID(Buffer const & buf, OutputParams const & runparams) const
2055 for (pos_type i = 0; i < size(); ++i) {
2057 Inset const * inset = getInset(i);
2058 InsetCode lyx_code = inset->lyxCode();
2059 if (lyx_code == LABEL_CODE) {
2060 string const id = static_cast<InsetCommand const *>(inset)->getContents();
2061 return "id='" + to_utf8(sgml::cleanID(buf, runparams, from_utf8(id))) + "'";
2070 pos_type Paragraph::getFirstWord(Buffer const & buf, odocstream & os, OutputParams const & runparams) const
2073 for (i = 0; i < size(); ++i) {
2075 Inset const * inset = getInset(i);
2076 inset->docbook(buf, os, runparams);
2078 value_type c = getChar(i);
2081 os << sgml::escapeChar(c);
2088 bool Paragraph::onlyText(Buffer const & buf, Font const & outerfont, pos_type initial) const
2092 for (pos_type i = initial; i < size(); ++i) {
2093 Font font = getFont(buf.params(), i, outerfont);
2096 if (i != initial && font != font_old)
2105 void Paragraph::simpleDocBookOnePar(Buffer const & buf,
2107 OutputParams const & runparams,
2108 Font const & outerfont,
2109 pos_type initial) const
2111 bool emph_flag = false;
2113 LayoutPtr const & style = layout();
2115 style->labeltype == LABEL_MANUAL ? style->labelfont : style->font;
2117 if (style->pass_thru && !onlyText(buf, outerfont, initial))
2120 // parsing main loop
2121 for (pos_type i = initial; i < size(); ++i) {
2122 Font font = getFont(buf.params(), i, outerfont);
2124 // handle <emphasis> tag
2125 if (font_old.emph() != font.emph()) {
2126 if (font.emph() == Font::ON) {
2129 } else if (i != initial) {
2130 os << "</emphasis>";
2136 Inset const * inset = getInset(i);
2137 inset->docbook(buf, os, runparams);
2139 value_type c = getChar(i);
2141 if (style->pass_thru)
2144 os << sgml::escapeChar(c);
2150 os << "</emphasis>";
2153 if (style->free_spacing)
2155 if (style->pass_thru && !onlyText(buf, outerfont, initial))
2160 bool Paragraph::isHfill(pos_type pos) const
2163 && getInset(pos)->lyxCode() == HFILL_CODE;
2167 bool Paragraph::isNewline(pos_type pos) const
2170 && getInset(pos)->lyxCode() == NEWLINE_CODE;
2174 bool Paragraph::isLineSeparator(pos_type pos) const
2176 value_type const c = getChar(pos);
2177 return isLineSeparatorChar(c)
2178 || (c == Paragraph::META_INSET && getInset(pos) &&
2179 getInset(pos)->isLineSeparator());
2183 /// Used by the spellchecker
2184 bool Paragraph::isLetter(pos_type pos) const
2187 return getInset(pos)->isLetter();
2189 value_type const c = getChar(pos);
2190 return isLetterChar(c) || isDigit(c);
2196 Paragraph::getParLanguage(BufferParams const & bparams) const
2199 return getFirstFontSettings(bparams).language();
2200 // FIXME: we should check the prev par as well (Lgb)
2201 return bparams.language;
2205 bool Paragraph::isRTL(BufferParams const & bparams) const
2207 return lyxrc.rtl_support
2208 && getParLanguage(bparams)->rightToLeft()
2209 && ownerCode() != ERT_CODE
2210 && ownerCode() != LISTINGS_CODE;
2214 void Paragraph::changeLanguage(BufferParams const & bparams,
2215 Language const * from, Language const * to)
2217 // change language including dummy font change at the end
2218 for (pos_type i = 0; i <= size(); ++i) {
2219 Font font = getFontSettings(bparams, i);
2220 if (font.language() == from) {
2221 font.setLanguage(to);
2228 bool Paragraph::isMultiLingual(BufferParams const & bparams) const
2230 Language const * doc_language = bparams.language;
2231 FontList::const_iterator cit = d->fontlist_.begin();
2232 FontList::const_iterator end = d->fontlist_.end();
2234 for (; cit != end; ++cit)
2235 if (cit->font().language() != ignore_language &&
2236 cit->font().language() != latex_language &&
2237 cit->font().language() != doc_language)
2243 // Convert the paragraph to a string.
2244 // Used for building the table of contents
2245 docstring const Paragraph::asString(Buffer const & buffer, bool label) const
2247 return asString(buffer, 0, size(), label);
2251 docstring const Paragraph::asString(Buffer const & buffer,
2252 pos_type beg, pos_type end, bool label) const
2255 odocstringstream os;
2257 if (beg == 0 && label && !params().labelString().empty())
2258 os << params().labelString() << ' ';
2260 for (pos_type i = beg; i < end; ++i) {
2261 value_type const c = getChar(i);
2264 else if (c == META_INSET)
2265 getInset(i)->textString(buffer, os);
2272 void Paragraph::setInsetOwner(Inset * inset)
2274 d->inset_owner_ = inset;
2278 int Paragraph::id() const
2284 LayoutPtr const & Paragraph::layout() const
2290 void Paragraph::layout(LayoutPtr const & new_layout)
2292 layout_ = new_layout;
2296 Inset * Paragraph::inInset() const
2298 return d->inset_owner_;
2302 InsetCode Paragraph::ownerCode() const
2304 return d->inset_owner_ ?
2305 d->inset_owner_->lyxCode() : NO_CODE;
2309 ParagraphParameters & Paragraph::params()
2315 ParagraphParameters const & Paragraph::params() const
2321 bool Paragraph::isFreeSpacing() const
2323 if (layout()->free_spacing)
2326 // for now we just need this, later should we need this in some
2327 // other way we can always add a function to Inset too.
2328 return ownerCode() == ERT_CODE || ownerCode() == LISTINGS_CODE;
2332 bool Paragraph::allowEmpty() const
2334 if (layout()->keepempty)
2336 return ownerCode() == ERT_CODE || ownerCode() == LISTINGS_CODE;
2340 char_type Paragraph::transformChar(char_type c, pos_type pos) const
2342 if (!Encodings::is_arabic(c))
2345 value_type prev_char = ' ';
2346 value_type next_char = ' ';
2348 for (pos_type i = pos - 1; i >= 0; --i) {
2349 value_type const par_char = getChar(i);
2350 if (!Encodings::isComposeChar_arabic(par_char)) {
2351 prev_char = par_char;
2356 for (pos_type i = pos + 1, end = size(); i < end; ++i) {
2357 value_type const par_char = getChar(i);
2358 if (!Encodings::isComposeChar_arabic(par_char)) {
2359 next_char = par_char;
2364 if (Encodings::is_arabic(next_char)) {
2365 if (Encodings::is_arabic(prev_char) &&
2366 !Encodings::is_arabic_special(prev_char))
2367 return Encodings::transformChar(c, Encodings::FORM_MEDIAL);
2369 return Encodings::transformChar(c, Encodings::FORM_INITIAL);
2371 if (Encodings::is_arabic(prev_char) &&
2372 !Encodings::is_arabic_special(prev_char))
2373 return Encodings::transformChar(c, Encodings::FORM_FINAL);
2375 return Encodings::transformChar(c, Encodings::FORM_ISOLATED);
2380 int Paragraph::checkBiblio(bool track_changes)
2383 //This is getting more and more a mess. ...We really should clean
2384 //up this bibitem issue for 1.6. See also bug 2743.
2386 // Add bibitem insets if necessary
2387 if (layout()->labeltype != LABEL_BIBLIO)
2390 bool hasbibitem = !d->insetlist_.empty()
2391 // Insist on it being in pos 0
2392 && getChar(0) == Paragraph::META_INSET
2393 && d->insetlist_.begin()->inset->lyxCode() == BIBITEM_CODE;
2398 // remove a bibitem in pos != 0
2399 // restore it later in pos 0 if necessary
2400 // (e.g. if a user inserts contents _before_ the item)
2401 // we're assuming there's only one of these, which there
2403 int erasedInsetPosition = -1;
2404 InsetList::iterator it = d->insetlist_.begin();
2405 InsetList::iterator end = d->insetlist_.end();
2406 for (; it != end; ++it)
2407 if (it->inset->lyxCode() == BIBITEM_CODE
2409 InsetBibitem * olditem = static_cast<InsetBibitem *>(it->inset);
2410 oldkey = olditem->getParam("key");
2411 oldlabel = olditem->getParam("label");
2412 erasedInsetPosition = it->pos;
2413 eraseChar(erasedInsetPosition, track_changes);
2417 //There was an InsetBibitem at the beginning, and we didn't
2418 //have to erase one.
2419 if (hasbibitem && erasedInsetPosition < 0)
2422 //There was an InsetBibitem at the beginning and we did have to
2423 //erase one. So we give its properties to the beginning inset.
2425 InsetBibitem * inset =
2426 static_cast<InsetBibitem *>(d->insetlist_.begin()->inset);
2427 if (!oldkey.empty())
2428 inset->setParam("key", oldkey);
2429 inset->setParam("label", oldlabel);
2430 return -erasedInsetPosition;
2433 //There was no inset at the beginning, so we need to create one with
2434 //the key and label of the one we erased.
2435 InsetBibitem * inset(new InsetBibitem(InsetCommandParams("bibitem")));
2436 // restore values of previously deleted item in this par.
2437 if (!oldkey.empty())
2438 inset->setParam("key", oldkey);
2439 inset->setParam("label", oldlabel);
2440 insertInset(0, static_cast<Inset *>(inset),
2441 Change(track_changes ? Change::INSERTED : Change::UNCHANGED));
2447 void Paragraph::checkAuthors(AuthorList const & authorList)
2449 d->changes_.checkAuthors(authorList);
2453 bool Paragraph::isUnchanged(pos_type pos) const
2455 return lookupChange(pos).type == Change::UNCHANGED;
2459 bool Paragraph::isInserted(pos_type pos) const
2461 return lookupChange(pos).type == Change::INSERTED;
2465 bool Paragraph::isDeleted(pos_type pos) const
2467 return lookupChange(pos).type == Change::DELETED;
2471 InsetList const & Paragraph::insetList() const
2473 return d->insetlist_;
2477 Inset * Paragraph::releaseInset(pos_type pos)
2479 Inset * inset = d->insetlist_.release(pos);
2480 /// does not honour change tracking!
2481 eraseChar(pos, false);
2486 Inset * Paragraph::getInset(pos_type pos)
2488 return d->insetlist_.get(pos);
2492 Inset const * Paragraph::getInset(pos_type pos) const
2494 return d->insetlist_.get(pos);