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 Private::simpleTeXSpecialChar(
128 OutputParams & runparams,
130 Change & running_change,
132 unsigned int & column,
136 void validate(LaTeXFeatures & features,
137 Layout const & layout) const;
140 pos_type size() const { return owner_->size(); }
142 /// match a string against a particular point in the paragraph
143 bool isTextAt(std::string const & str, pos_type pos) const;
145 /// Which Paragraph owns us?
149 Inset * inset_owner_;
157 static unsigned int paragraph_id;
159 ParagraphParameters params_;
161 /// for recording and looking up changes
165 InsetList insetlist_;
172 using std::upper_bound;
173 using std::lower_bound;
177 // Initialization of the counter for the paragraph id's,
178 unsigned int Paragraph::Private::paragraph_id = 0;
182 struct special_phrase {
188 special_phrase const special_phrases[] = {
189 { "LyX", from_ascii("\\LyX{}"), false },
190 { "TeX", from_ascii("\\TeX{}"), true },
191 { "LaTeX2e", from_ascii("\\LaTeXe{}"), true },
192 { "LaTeX", from_ascii("\\LaTeX{}"), true },
195 size_t const phrases_nr = sizeof(special_phrases)/sizeof(special_phrase);
200 Paragraph::Private::Private(Paragraph * owner)
204 id_ = paragraph_id++;
208 Paragraph::Private::Private(Private const & p, Paragraph * owner)
209 : owner_(owner), inset_owner_(p.inset_owner_), fontlist_(p.fontlist_),
210 params_(p.params_), changes_(p.changes_), insetlist_(p.insetlist_)
212 id_ = paragraph_id++;
216 bool Paragraph::isChanged(pos_type start, pos_type end) const
218 BOOST_ASSERT(start >= 0 && start <= size());
219 BOOST_ASSERT(end > start && end <= size() + 1);
221 return d->changes_.isChanged(start, end);
225 bool Paragraph::isMergedOnEndOfParDeletion(bool trackChanges) const {
226 // keep the logic here in sync with the logic of eraseChars()
232 Change change = d->changes_.lookup(size());
234 return change.type == Change::INSERTED && change.author == 0;
238 void Paragraph::setChange(Change const & change)
240 // beware of the imaginary end-of-par character!
241 d->changes_.set(change, 0, size() + 1);
244 * Propagate the change recursively - but not in case of DELETED!
246 * Imagine that your co-author makes changes in an existing inset. He
247 * sends your document to you and you come to the conclusion that the
248 * inset should go completely. If you erase it, LyX must not delete all
249 * text within the inset. Otherwise, the change tracked insertions of
250 * your co-author get lost and there is no way to restore them later.
252 * Conclusion: An inset's content should remain untouched if you delete it
255 if (change.type != Change::DELETED) {
256 for (pos_type pos = 0; pos < size(); ++pos) {
258 getInset(pos)->setChange(change);
264 void Paragraph::setChange(pos_type pos, Change const & change)
266 BOOST_ASSERT(pos >= 0 && pos <= size());
268 d->changes_.set(change, pos);
270 // see comment in setChange(Change const &) above
272 if (change.type != Change::DELETED &&
273 pos < size() && isInset(pos)) {
274 getInset(pos)->setChange(change);
279 Change const & Paragraph::lookupChange(pos_type pos) const
281 BOOST_ASSERT(pos >= 0 && pos <= size());
283 return d->changes_.lookup(pos);
287 void Paragraph::acceptChanges(BufferParams const & bparams, pos_type start,
290 BOOST_ASSERT(start >= 0 && start <= size());
291 BOOST_ASSERT(end > start && end <= size() + 1);
293 for (pos_type pos = start; pos < end; ++pos) {
294 switch (lookupChange(pos).type) {
295 case Change::UNCHANGED:
296 // accept changes in nested inset
297 if (pos < size() && isInset(pos))
298 getInset(pos)->acceptChanges(bparams);
302 case Change::INSERTED:
303 d->changes_.set(Change(Change::UNCHANGED), pos);
304 // also accept changes in nested inset
305 if (pos < size() && isInset(pos)) {
306 getInset(pos)->acceptChanges(bparams);
310 case Change::DELETED:
311 // Suppress access to non-existent
312 // "end-of-paragraph char"
314 eraseChar(pos, false);
325 void Paragraph::rejectChanges(BufferParams const & bparams,
326 pos_type start, pos_type end)
328 BOOST_ASSERT(start >= 0 && start <= size());
329 BOOST_ASSERT(end > start && end <= size() + 1);
331 for (pos_type pos = start; pos < end; ++pos) {
332 switch (lookupChange(pos).type) {
333 case Change::UNCHANGED:
334 // reject changes in nested inset
335 if (pos < size() && isInset(pos)) {
336 getInset(pos)->rejectChanges(bparams);
340 case Change::INSERTED:
341 // Suppress access to non-existent
342 // "end-of-paragraph char"
344 eraseChar(pos, false);
350 case Change::DELETED:
351 d->changes_.set(Change(Change::UNCHANGED), pos);
353 // Do NOT reject changes within a deleted inset!
354 // There may be insertions of a co-author inside of it!
362 Paragraph::value_type Paragraph::Private::getChar(pos_type pos) const
364 BOOST_ASSERT(pos >= 0 && pos <= size());
366 return owner_->getChar(pos);
370 void Paragraph::Private::insertChar(pos_type pos, value_type c,
371 Change const & change)
373 BOOST_ASSERT(pos >= 0 && pos <= size());
376 changes_.insert(change, pos);
378 // This is actually very common when parsing buffers (and
379 // maybe inserting ascii text)
381 // when appending characters, no need to update tables
382 owner_->text_.push_back(c);
386 owner_->text_.insert(owner_->text_.begin() + pos, c);
388 // Update the font table.
389 fontlist_.increasePosAfterPos(pos);
392 insetlist_.increasePosAfterPos(pos);
396 void Paragraph::insertInset(pos_type pos, Inset * inset,
397 Change const & change)
400 BOOST_ASSERT(pos >= 0 && pos <= size());
402 d->insertChar(pos, META_INSET, change);
403 BOOST_ASSERT(text_[pos] == META_INSET);
405 // Add a new entry in the insetlist_.
406 d->insetlist_.insert(inset, pos);
410 bool Paragraph::eraseChar(pos_type pos, bool trackChanges)
412 BOOST_ASSERT(pos >= 0 && pos <= size());
414 // keep the logic here in sync with the logic of isMergedOnEndOfParDeletion()
417 Change change = d->changes_.lookup(pos);
419 // set the character to DELETED if
420 // a) it was previously unchanged or
421 // b) it was inserted by a co-author
423 if (change.type == Change::UNCHANGED ||
424 (change.type == Change::INSERTED && change.author != 0)) {
425 setChange(pos, Change(Change::DELETED));
429 if (change.type == Change::DELETED)
433 // Don't physically access the imaginary end-of-paragraph character.
434 // eraseChar() can only mark it as DELETED. A physical deletion of
435 // end-of-par must be handled externally.
441 d->changes_.erase(pos);
443 // if it is an inset, delete the inset entry
444 if (text_[pos] == Paragraph::META_INSET)
445 d->insetlist_.erase(pos);
447 text_.erase(text_.begin() + pos);
449 // Update the fontlist_
450 d->fontlist_.erase(pos);
452 // Update the insetlist_
453 d->insetlist_.decreasePosAfterPos(pos);
459 int Paragraph::eraseChars(pos_type start, pos_type end, bool trackChanges)
461 BOOST_ASSERT(start >= 0 && start <= size());
462 BOOST_ASSERT(end >= start && end <= size() + 1);
465 for (pos_type count = end - start; count; --count) {
466 if (!eraseChar(i, trackChanges))
473 int Paragraph::Private::latexSurrogatePair(odocstream & os, value_type c,
474 value_type next, Encoding const & encoding)
476 // Writing next here may circumvent a possible font change between
477 // c and next. Since next is only output if it forms a surrogate pair
478 // with c we can ignore this:
479 // A font change inside a surrogate pair does not make sense and is
480 // hopefully impossible to input.
481 // FIXME: change tracking
482 // Is this correct WRT change tracking?
483 docstring const latex1 = encoding.latexChar(next);
484 docstring const latex2 = encoding.latexChar(c);
485 os << latex1 << '{' << latex2 << '}';
486 return latex1.length() + latex2.length() + 2;
490 bool Paragraph::Private::simpleTeXBlanks(Encoding const & encoding,
491 odocstream & os, TexRow & texrow,
493 unsigned int & column,
495 Layout const & style)
500 if (i + 1 < size()) {
501 char_type next = getChar(i + 1);
502 if (Encodings::isCombiningChar(next)) {
503 // This space has an accent, so we must always output it.
504 column += latexSurrogatePair(os, ' ', next, encoding) - 1;
510 if (lyxrc.plaintext_linelen > 0
511 && column > lyxrc.plaintext_linelen
513 && getChar(i - 1) != ' '
515 // same in FreeSpacing mode
516 && !owner_->isFreeSpacing()
517 // In typewriter mode, we want to avoid
518 // ! . ? : at the end of a line
519 && !(font.family() == Font::TYPEWRITER_FAMILY
520 && (getChar(i - 1) == '.'
521 || getChar(i - 1) == '?'
522 || getChar(i - 1) == ':'
523 || getChar(i - 1) == '!'))) {
526 texrow.start(owner_->id(), i + 1);
528 } else if (style.free_spacing) {
537 int Paragraph::Private::knownLangChars(odocstream & os,
540 Change & runningChange,
541 Encoding const & encoding,
544 // When the character is marked by the proper language, we simply
545 // get its code point in some encoding, otherwise we get the
546 // translation specified in the unicodesymbols file, which is
547 // something like "\textLANG{<spec>}". So, we have to retain
548 // "\textLANG{<spec>" for the first char but only "<spec>" for
549 // all subsequent chars.
550 docstring const latex1 = rtrim(encoding.latexChar(c), "}");
551 int length = latex1.length();
553 while (i + 1 < size()) {
554 char_type next = getChar(i + 1);
555 // Stop here if next character belongs to another
556 // language or there is a change tracking status.
557 if (!Encodings::isKnownLangChar(next, preamble) ||
558 runningChange != owner_->lookupChange(i + 1))
562 FontList::const_iterator cit = fontlist_.begin();
563 FontList::const_iterator end = fontlist_.end();
564 for (; cit != end; ++cit) {
565 if (cit->pos() >= i && !found) {
566 prev_font = cit->font();
569 if (cit->pos() >= i + 1)
572 // Stop here if there is a font attribute change.
573 if (found && cit != end && prev_font != cit->font())
575 docstring const latex = rtrim(encoding.latexChar(next), "}");
576 docstring::size_type const j =
577 latex.find_first_of(from_ascii("{"));
578 if (j == docstring::npos) {
580 length += latex.length();
582 os << latex.substr(j + 1);
583 length += latex.substr(j + 1).length();
587 // When the proper language is set, we are simply passed a code
588 // point, so we should not try to close the \textLANG command.
589 if (prefixIs(latex1, from_ascii("\\" + preamble))) {
597 bool Paragraph::Private::isTextAt(string const & str, pos_type pos) const
599 pos_type const len = str.length();
601 // is the paragraph large enough?
602 if (pos + len > size())
605 // does the wanted text start at point?
606 for (string::size_type i = 0; i < str.length(); ++i) {
607 // Caution: direct comparison of characters works only
608 // because str is pure ASCII.
609 if (str[i] != owner_->text_[pos + i])
613 return fontlist_.hasChangeInRange(pos, len);
617 void Paragraph::Private::simpleTeXSpecialChars(Buffer const & buf,
618 BufferParams const & bparams,
621 OutputParams & runparams,
624 Font const & outerfont,
626 Change & running_change,
627 Layout const & style,
629 unsigned int & column,
632 if (style.pass_thru) {
633 if (c != Paragraph::META_INSET) {
635 // FIXME UNICODE: This can fail if c cannot
636 // be encoded in the current encoding.
639 owner_->getInset(i)->plaintext(buf, os, runparams);
643 // Two major modes: LaTeX or plain
644 // Handle here those cases common to both modes
645 // and then split to handle the two modes separately.
646 if (c != Paragraph::META_INSET) {
647 simpleTeXSpecialChar(os, runparams, running_font, running_change,
652 Inset * inset = owner_->getInset(i);
655 // FIXME: move this to InsetNewline::latex
656 if (inset->lyxCode() == NEWLINE_CODE) {
657 // newlines are handled differently here than
658 // the default in simpleTeXSpecialChars().
659 if (!style.newline_allowed) {
663 column += running_font.latexWriteEndChanges(
664 os, bparams, runparams,
669 if (running_font.family() == Font::TYPEWRITER_FAMILY)
672 basefont = owner_->getLayoutFont(bparams, outerfont);
673 running_font = basefont;
675 if (runparams.moving_arg)
681 texrow.start(owner_->id(), i + 1);
685 if (owner_->lookupChange(i).type == Change::DELETED) {
686 if( ++runparams.inDeletedInset == 1)
687 runparams.changeOfDeletedInset = owner_->lookupChange(i);
690 if (inset->canTrackChanges()) {
691 column += Changes::latexMarkChange(os, bparams, running_change,
692 Change(Change::UNCHANGED));
693 running_change = Change(Change::UNCHANGED);
697 odocstream::pos_type const len = os.tellp();
699 if ((inset->lyxCode() == GRAPHICS_CODE
700 || inset->lyxCode() == MATH_CODE
701 || inset->lyxCode() == HYPERLINK_CODE)
702 && running_font.isRightToLeft()) {
703 if (running_font.language()->lang() == "farsi")
710 // FIXME: Bug: we can have an empty font change here!
711 // if there has just been a font change, we are going to close it
712 // right now, which means stupid latex code like \textsf{}. AFAIK,
713 // this does not harm dvi output. A minor bug, thus (JMarc)
715 // Some insets cannot be inside a font change command.
716 // However, even such insets *can* be placed in \L or \R
717 // or their equivalents (for RTL language switches), so we don't
718 // close the language in those cases.
719 // ArabTeX, though, cannot handle this special behavior, it seems.
720 bool arabtex = basefont.language()->lang() == "arabic_arabtex"
721 || running_font.language()->lang() == "arabic_arabtex";
722 if (open_font && inset->noFontChange()) {
723 bool closeLanguage = arabtex
724 || basefont.isRightToLeft() == running_font.isRightToLeft();
725 unsigned int count = running_font.latexWriteEndChanges(os,
726 bparams, runparams, basefont, basefont, closeLanguage);
728 // if any font properties were closed, update the running_font,
729 // making sure, however, to leave the language as it was
731 // FIXME: probably a better way to keep track of the old
732 // language, than copying the entire font?
733 Font const copy_font(running_font);
734 basefont = owner_->getLayoutFont(bparams, outerfont);
735 running_font = basefont;
737 running_font.setLanguage(copy_font.language());
738 // leave font open if language is still open
739 open_font = (running_font.language() == basefont.language());
741 runparams.local_font = &basefont;
745 int tmp = inset->latex(buf, os, runparams);
748 if (running_font.language()->lang() == "farsi")
755 for (int j = 0; j < tmp; ++j)
758 texrow.start(owner_->id(), i + 1);
761 column += os.tellp() - len;
764 if (owner_->lookupChange(i).type == Change::DELETED)
765 --runparams.inDeletedInset;
769 void Paragraph::Private::simpleTeXSpecialChar(
771 OutputParams & runparams,
773 Change & running_change,
775 unsigned int & column,
780 os << "\\textbackslash{}";
787 // In T1 encoding, these characters exist
788 if (lyxrc.fontenc == "T1") {
790 //... but we should avoid ligatures
791 if ((c == '>' || c == '<')
793 && getChar(i + 1) == c) {
794 //os << "\\textcompwordmark{}";
796 // Jean-Marc, have a look at
797 // this. I think this works
805 // Typewriter font also has them
806 if (running_font.family() == Font::TYPEWRITER_FAMILY) {
810 // Otherwise, we use what LaTeX
814 os << "\\textless{}";
818 os << "\\textgreater{}";
828 case '-': // "--" in Typewriter mode -> "-{}-"
829 if (i <= size() - 2 &&
830 getChar(i + 1) == '-' &&
831 running_font.family() == Font::TYPEWRITER_FAMILY) {
840 os << "\\char`\\\"{}";
845 case '%': case '#': case '{':
853 os << "\\textasciitilde{}";
858 os << "\\textasciicircum{}";
863 // avoid being mistaken for optional arguments
871 // Blanks are printed before font switching.
872 // Sure? I am not! (try nice-latex)
873 // I am sure it's correct. LyX might be smarter
874 // in the future, but for now, nothing wrong is
879 // I assume this is hack treating typewriter as verbatim
880 // FIXME UNICODE: This can fail if c cannot be encoded
881 // in the current encoding.
882 if (running_font.family() == Font::TYPEWRITER_FAMILY) {
890 // FIXME: if we have "LaTeX" with a font
891 // change in the middle (before the 'T', then
892 // the "TeX" part is still special cased.
893 // Really we should only operate this on
894 // "words" for some definition of word
898 for (; pnr < phrases_nr; ++pnr) {
899 if (isTextAt(special_phrases[pnr].phrase, i)) {
900 os << special_phrases[pnr].macro;
901 i += special_phrases[pnr].phrase.length() - 1;
902 column += special_phrases[pnr].macro.length() - 1;
907 if (pnr == phrases_nr && c != '\0') {
908 Encoding const & encoding = *(runparams.encoding);
909 if (i + 1 < size()) {
910 char_type next = getChar(i + 1);
911 if (Encodings::isCombiningChar(next)) {
912 column += latexSurrogatePair(os, c, next, encoding) - 1;
918 if (Encodings::isKnownLangChar(c, preamble)) {
919 column += knownLangChars(os, c, preamble, running_change,
923 docstring const latex = encoding.latexChar(c);
924 if (latex.length() > 1 && latex[latex.length() - 1] != '}') {
925 // Prevent eating of a following
926 // space or command corruption by
927 // following characters
928 column += latex.length() + 1;
931 column += latex.length() - 1;
940 void Paragraph::Private::validate(LaTeXFeatures & features,
941 Layout const & layout) const
944 if (!params_.spacing().isDefault())
945 features.require("setspace");
948 features.useLayout(layout.name());
951 fontlist_.validate(features);
953 // then the indentation
954 if (!params_.leftIndent().zero())
955 features.require("ParagraphLeftIndent");
958 InsetList::const_iterator icit = insetlist_.begin();
959 InsetList::const_iterator iend = insetlist_.end();
960 for (; icit != iend; ++icit) {
962 icit->inset->validate(features);
963 if (layout.needprotect &&
964 icit->inset->lyxCode() == FOOT_CODE)
965 features.require("NeedLyXFootnoteCode");
970 for (pos_type i = 0; i < size() ; ++i) {
971 for (size_t pnr = 0; pnr < phrases_nr; ++pnr) {
972 if (!special_phrases[pnr].builtin
973 && isTextAt(special_phrases[pnr].phrase, i)) {
974 features.require(special_phrases[pnr].phrase);
978 Encodings::validate(getChar(i), features);
982 /////////////////////////////////////////////////////////////////////
986 /////////////////////////////////////////////////////////////////////
988 Paragraph::Paragraph()
989 : begin_of_body_(0), d(new Paragraph::Private(this))
996 Paragraph::Paragraph(Paragraph const & par)
997 : itemdepth(par.itemdepth),
998 layout_(par.layout_),
999 text_(par.text_), begin_of_body_(par.begin_of_body_),
1000 d(new Paragraph::Private(*par.d, this))
1005 Paragraph & Paragraph::operator=(Paragraph const & par)
1007 // needed as we will destroy the private part before copying it
1009 itemdepth = par.itemdepth;
1010 layout_ = par.layout();
1012 begin_of_body_ = par.begin_of_body_;
1015 d = new Private(*par.d, this);
1021 Paragraph::~Paragraph()
1027 void Paragraph::write(Buffer const & buf, ostream & os,
1028 BufferParams const & bparams,
1029 depth_type & dth) const
1031 // The beginning or end of a deeper (i.e. nested) area?
1032 if (dth != params().depth()) {
1033 if (params().depth() > dth) {
1034 while (params().depth() > dth) {
1035 os << "\n\\begin_deeper";
1039 while (params().depth() < dth) {
1040 os << "\n\\end_deeper";
1046 // First write the layout
1047 os << "\n\\begin_layout " << to_utf8(layout()->name()) << '\n';
1051 Font font1(Font::ALL_INHERIT, bparams.language);
1053 Change running_change = Change(Change::UNCHANGED);
1056 for (pos_type i = 0; i <= size(); ++i) {
1058 Change change = lookupChange(i);
1059 Changes::lyxMarkChange(os, column, running_change, change);
1060 running_change = change;
1065 // Write font changes
1066 Font font2 = getFontSettings(bparams, i);
1067 if (font2 != font1) {
1068 font2.lyxWriteChanges(font1, os);
1073 value_type const c = getChar(i);
1077 Inset const * inset = getInset(i);
1079 if (inset->directWrite()) {
1080 // international char, let it write
1081 // code directly so it's shorter in
1083 inset->write(buf, os);
1087 os << "\\begin_inset ";
1088 inset->write(buf, os);
1089 os << "\n\\end_inset\n\n";
1095 os << "\n\\backslash\n";
1099 if (i + 1 < size() && getChar(i + 1) == ' ') {
1106 if ((column > 70 && c == ' ')
1111 // this check is to amend a bug. LyX sometimes
1112 // inserts '\0' this could cause problems.
1114 std::vector<char> tmp = ucs4_to_utf8(c);
1115 tmp.push_back('\0');
1118 lyxerr << "ERROR (Paragraph::writeFile):"
1119 " NULL char in structure." << endl;
1125 os << "\n\\end_layout\n";
1129 void Paragraph::validate(LaTeXFeatures & features) const
1131 d->validate(features, *layout());
1135 void Paragraph::insert(pos_type start, docstring const & str,
1136 Font const & font, Change const & change)
1138 for (size_t i = 0, n = str.size(); i != n ; ++i)
1139 insertChar(start + i, str[i], font, change);
1143 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c,
1146 d->insertChar(pos, c, Change(trackChanges ?
1147 Change::INSERTED : Change::UNCHANGED));
1151 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c,
1152 Font const & font, bool trackChanges)
1154 d->insertChar(pos, c, Change(trackChanges ?
1155 Change::INSERTED : Change::UNCHANGED));
1160 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c,
1161 Font const & font, Change const & change)
1163 d->insertChar(pos, c, change);
1168 void Paragraph::insertInset(pos_type pos, Inset * inset,
1169 Font const & font, Change const & change)
1171 insertInset(pos, inset, change);
1172 // Set the font/language of the inset...
1177 bool Paragraph::insetAllowed(InsetCode code)
1179 return !d->inset_owner_ || d->inset_owner_->insetAllowed(code);
1183 // Gets uninstantiated font setting at position.
1184 Font const Paragraph::getFontSettings(BufferParams const & bparams,
1188 lyxerr << " pos: " << pos << " size: " << size() << endl;
1189 BOOST_ASSERT(pos <= size());
1192 FontList::const_iterator cit = d->fontlist_.fontIterator(pos);
1193 if (cit != d->fontlist_.end())
1196 if (pos == size() && !empty())
1197 return getFontSettings(bparams, pos - 1);
1199 return Font(Font::ALL_INHERIT, getParLanguage(bparams));
1203 FontSpan Paragraph::fontSpan(pos_type pos) const
1205 BOOST_ASSERT(pos <= size());
1208 FontList::const_iterator cit = d->fontlist_.begin();
1209 FontList::const_iterator end = d->fontlist_.end();
1210 for (; cit != end; ++cit) {
1211 if (cit->pos() >= pos) {
1212 if (pos >= beginOfBody())
1213 return FontSpan(std::max(start, beginOfBody()),
1216 return FontSpan(start,
1217 std::min(beginOfBody() - 1,
1220 start = cit->pos() + 1;
1223 // This should not happen, but if so, we take no chances.
1224 //lyxerr << "Paragraph::getEndPosOfFontSpan: This should not happen!"
1226 return FontSpan(pos, pos);
1230 // Gets uninstantiated font setting at position 0
1231 Font const Paragraph::getFirstFontSettings(BufferParams const & bparams) const
1233 if (!empty() && !d->fontlist_.empty())
1234 return d->fontlist_.begin()->font();
1236 return Font(Font::ALL_INHERIT, bparams.language);
1240 // Gets the fully instantiated font at a given position in a paragraph
1241 // This is basically the same function as Text::GetFont() in text2.cpp.
1242 // The difference is that this one is used for generating the LaTeX file,
1243 // and thus cosmetic "improvements" are disallowed: This has to deliver
1244 // the true picture of the buffer. (Asger)
1245 Font const Paragraph::getFont(BufferParams const & bparams, pos_type pos,
1246 Font const & outerfont) const
1248 BOOST_ASSERT(pos >= 0);
1250 Font font = getFontSettings(bparams, pos);
1252 pos_type const body_pos = beginOfBody();
1254 font.realize(layout_->labelfont);
1256 font.realize(layout_->font);
1258 font.realize(outerfont);
1259 font.realize(bparams.getFont());
1265 Font const Paragraph::getLabelFont
1266 (BufferParams const & bparams, Font const & outerfont) const
1268 Font tmpfont = layout()->labelfont;
1269 tmpfont.setLanguage(getParLanguage(bparams));
1270 tmpfont.realize(outerfont);
1271 tmpfont.realize(bparams.getFont());
1276 Font const Paragraph::getLayoutFont
1277 (BufferParams const & bparams, Font const & outerfont) const
1279 Font tmpfont = layout()->font;
1280 tmpfont.setLanguage(getParLanguage(bparams));
1281 tmpfont.realize(outerfont);
1282 tmpfont.realize(bparams.getFont());
1287 /// Returns the height of the highest font in range
1288 Font_size Paragraph::highestFontInRange
1289 (pos_type startpos, pos_type endpos, Font_size def_size) const
1291 return d->fontlist_.highestInRange(startpos, endpos, def_size);
1295 Paragraph::value_type
1296 Paragraph::getUChar(BufferParams const & bparams, pos_type pos) const
1298 value_type c = getChar(pos);
1299 if (!lyxrc.rtl_support)
1329 if (uc != c && getFontSettings(bparams, pos).isRightToLeft())
1336 void Paragraph::setFont(pos_type pos, Font const & font)
1338 BOOST_ASSERT(pos <= size());
1340 // First, reduce font against layout/label font
1341 // Update: The setCharFont() routine in text2.cpp already
1342 // reduces font, so we don't need to do that here. (Asger)
1344 d->fontlist_.set(pos, font);
1348 void Paragraph::makeSameLayout(Paragraph const & par)
1350 layout(par.layout());
1352 d->params_ = par.params();
1356 bool Paragraph::stripLeadingSpaces(bool trackChanges)
1358 if (isFreeSpacing())
1364 while (pos < size() && (isNewline(pos) || isLineSeparator(pos))) {
1365 if (eraseChar(pos, trackChanges))
1371 return count > 0 || pos > 0;
1375 bool Paragraph::hasSameLayout(Paragraph const & par) const
1377 return par.layout() == layout() && d->params_.sameLayout(par.params());
1381 depth_type Paragraph::getDepth() const
1383 return params().depth();
1387 depth_type Paragraph::getMaxDepthAfter() const
1389 if (layout()->isEnvironment())
1390 return params().depth() + 1;
1392 return params().depth();
1396 char Paragraph::getAlign() const
1398 if (params().align() == LYX_ALIGN_LAYOUT)
1399 return layout()->align;
1401 return params().align();
1405 docstring const & Paragraph::getLabelstring() const
1407 return params().labelString();
1411 // the next two functions are for the manual labels
1412 docstring const Paragraph::getLabelWidthString() const
1414 if (layout()->margintype == MARGIN_MANUAL)
1415 return params().labelWidthString();
1417 return _("Senseless with this layout!");
1421 void Paragraph::setLabelWidthString(docstring const & s)
1423 params().labelWidthString(s);
1427 docstring const Paragraph::translateIfPossible(docstring const & s,
1428 BufferParams const & bparams) const
1430 if (!support::isAscii(s) || s.empty()) {
1431 // This must be a user defined layout. We cannot translate
1432 // this, since gettext accepts only ascii keys.
1435 // Probably standard layout, try to translate
1436 Messages & m = getMessages(getParLanguage(bparams)->code());
1437 return m.get(to_ascii(s));
1441 docstring Paragraph::expandLabel(LayoutPtr const & layout,
1442 BufferParams const & bparams, bool process_appendix) const
1444 TextClass const & tclass = bparams.getTextClass();
1447 if (process_appendix && params().appendix())
1448 fmt = translateIfPossible(layout->labelstring_appendix(),
1451 fmt = translateIfPossible(layout->labelstring(), bparams);
1453 if (fmt.empty() && layout->labeltype == LABEL_COUNTER
1454 && !layout->counter.empty())
1455 fmt = "\\the" + layout->counter;
1457 // handle 'inherited level parts' in 'fmt',
1458 // i.e. the stuff between '@' in '@Section@.\arabic{subsection}'
1459 size_t const i = fmt.find('@', 0);
1460 if (i != docstring::npos) {
1461 size_t const j = fmt.find('@', i + 1);
1462 if (j != docstring::npos) {
1463 docstring parent(fmt, i + 1, j - i - 1);
1464 docstring label = from_ascii("??");
1465 if (tclass.hasLayout(parent))
1466 docstring label = expandLabel(tclass[parent], bparams,
1468 fmt = docstring(fmt, 0, i) + label
1469 + docstring(fmt, j + 1, docstring::npos);
1473 return tclass.counters().counterLabel(fmt);
1477 void Paragraph::applyLayout(LayoutPtr const & new_layout)
1480 LyXAlignment const oldAlign = params().align();
1482 if (!(oldAlign & layout()->alignpossible)) {
1483 frontend::Alert::warning(_("Alignment not permitted"),
1484 _("The new layout does not permit the alignment previously used.\nSetting to default."));
1485 params().align(LYX_ALIGN_LAYOUT);
1490 pos_type Paragraph::beginOfBody() const
1492 return begin_of_body_;
1496 void Paragraph::setBeginOfBody()
1498 if (layout()->labeltype != LABEL_MANUAL) {
1503 // Unroll the first two cycles of the loop
1504 // and remember the previous character to
1505 // remove unnecessary getChar() calls
1507 pos_type end = size();
1508 if (i < end && !isNewline(i)) {
1510 char_type previous_char = 0;
1513 previous_char = text_[i];
1514 if (!isNewline(i)) {
1516 while (i < end && previous_char != ' ') {
1521 previous_char = temp;
1531 // returns -1 if inset not found
1532 int Paragraph::getPositionOfInset(Inset const * inset) const
1535 InsetList::const_iterator it = d->insetlist_.begin();
1536 InsetList::const_iterator end = d->insetlist_.end();
1537 for (; it != end; ++it)
1538 if (it->inset == inset)
1544 InsetBibitem * Paragraph::bibitem() const
1546 if (!d->insetlist_.empty()) {
1547 Inset * inset = d->insetlist_.begin()->inset;
1548 if (inset->lyxCode() == BIBITEM_CODE)
1549 return static_cast<InsetBibitem *>(inset);
1555 bool Paragraph::forceDefaultParagraphs() const
1557 return inInset() && inInset()->forceDefaultParagraphs(0);
1563 // paragraphs inside floats need different alignment tags to avoid
1566 bool noTrivlistCentering(InsetCode code)
1568 return code == FLOAT_CODE || code == WRAP_CODE;
1572 string correction(string const & orig)
1574 if (orig == "flushleft")
1575 return "raggedright";
1576 if (orig == "flushright")
1577 return "raggedleft";
1578 if (orig == "center")
1584 string const corrected_env(string const & suffix, string const & env,
1587 string output = suffix + "{";
1588 if (noTrivlistCentering(code))
1589 output += correction(env);
1593 if (suffix == "\\begin")
1599 void adjust_row_column(string const & str, TexRow & texrow, int & column)
1601 if (!contains(str, "\n"))
1602 column += str.size();
1606 column = rsplit(str, tmp, '\n').size();
1613 // This could go to ParagraphParameters if we want to
1614 int Paragraph::startTeXParParams(BufferParams const & bparams,
1615 odocstream & os, TexRow & texrow,
1616 bool moving_arg) const
1620 if (params().noindent()) {
1621 os << "\\noindent ";
1625 LyXAlignment const curAlign = params().align();
1627 if (curAlign == layout()->align)
1631 case LYX_ALIGN_NONE:
1632 case LYX_ALIGN_BLOCK:
1633 case LYX_ALIGN_LAYOUT:
1634 case LYX_ALIGN_SPECIAL:
1636 case LYX_ALIGN_LEFT:
1637 case LYX_ALIGN_RIGHT:
1638 case LYX_ALIGN_CENTER:
1647 case LYX_ALIGN_NONE:
1648 case LYX_ALIGN_BLOCK:
1649 case LYX_ALIGN_LAYOUT:
1650 case LYX_ALIGN_SPECIAL:
1652 case LYX_ALIGN_LEFT: {
1654 if (getParLanguage(bparams)->babel() != "hebrew")
1655 output = corrected_env("\\begin", "flushleft", ownerCode());
1657 output = corrected_env("\\begin", "flushright", ownerCode());
1658 os << from_ascii(output);
1659 adjust_row_column(output, texrow, column);
1661 } case LYX_ALIGN_RIGHT: {
1663 if (getParLanguage(bparams)->babel() != "hebrew")
1664 output = corrected_env("\\begin", "flushright", ownerCode());
1666 output = corrected_env("\\begin", "flushleft", ownerCode());
1667 os << from_ascii(output);
1668 adjust_row_column(output, texrow, column);
1670 } case LYX_ALIGN_CENTER: {
1672 output = corrected_env("\\begin", "center", ownerCode());
1673 os << from_ascii(output);
1674 adjust_row_column(output, texrow, column);
1683 // This could go to ParagraphParameters if we want to
1684 int Paragraph::endTeXParParams(BufferParams const & bparams,
1685 odocstream & os, TexRow & texrow,
1686 bool moving_arg) const
1690 switch (params().align()) {
1691 case LYX_ALIGN_NONE:
1692 case LYX_ALIGN_BLOCK:
1693 case LYX_ALIGN_LAYOUT:
1694 case LYX_ALIGN_SPECIAL:
1696 case LYX_ALIGN_LEFT:
1697 case LYX_ALIGN_RIGHT:
1698 case LYX_ALIGN_CENTER:
1706 switch (params().align()) {
1707 case LYX_ALIGN_NONE:
1708 case LYX_ALIGN_BLOCK:
1709 case LYX_ALIGN_LAYOUT:
1710 case LYX_ALIGN_SPECIAL:
1712 case LYX_ALIGN_LEFT: {
1714 if (getParLanguage(bparams)->babel() != "hebrew")
1715 output = corrected_env("\n\\par\\end", "flushleft", ownerCode());
1717 output = corrected_env("\n\\par\\end", "flushright", ownerCode());
1718 os << from_ascii(output);
1719 adjust_row_column(output, texrow, column);
1721 } case LYX_ALIGN_RIGHT: {
1723 if (getParLanguage(bparams)->babel() != "hebrew")
1724 output = corrected_env("\n\\par\\end", "flushright", ownerCode());
1726 output = corrected_env("\n\\par\\end", "flushleft", ownerCode());
1727 os << from_ascii(output);
1728 adjust_row_column(output, texrow, column);
1730 } case LYX_ALIGN_CENTER: {
1732 output = corrected_env("\n\\par\\end", "center", ownerCode());
1733 os << from_ascii(output);
1734 adjust_row_column(output, texrow, column);
1743 // This one spits out the text of the paragraph
1744 bool Paragraph::simpleTeXOnePar(Buffer const & buf,
1745 BufferParams const & bparams,
1746 Font const & outerfont,
1747 odocstream & os, TexRow & texrow,
1748 OutputParams const & runparams) const
1750 LYXERR(Debug::LATEX) << "SimpleTeXOnePar... " << this << endl;
1752 bool return_value = false;
1756 // well we have to check if we are in an inset with unlimited
1757 // length (all in one row) if that is true then we don't allow
1758 // any special options in the paragraph and also we don't allow
1759 // any environment other than the default layout of the text class
1761 bool asdefault = forceDefaultParagraphs();
1764 style = bparams.getTextClass().defaultLayout();
1769 // Current base font for all inherited font changes, without any
1770 // change caused by an individual character, except for the language:
1771 // It is set to the language of the first character.
1772 // As long as we are in the label, this font is the base font of the
1773 // label. Before the first body character it is set to the base font
1777 // Maybe we have to create a optional argument.
1778 pos_type body_pos = beginOfBody();
1779 unsigned int column = 0;
1782 // the optional argument is kept in curly brackets in
1783 // case it contains a ']'
1786 basefont = getLabelFont(bparams, outerfont);
1788 basefont = getLayoutFont(bparams, outerfont);
1791 // Which font is currently active?
1792 Font running_font(basefont);
1793 // Do we have an open font change?
1794 bool open_font = false;
1796 Change runningChange = Change(Change::UNCHANGED);
1798 texrow.start(id(), 0);
1800 // if the paragraph is empty, the loop will not be entered at all
1802 if (style->isCommand()) {
1807 column += startTeXParParams(bparams, os, texrow,
1808 runparams.moving_arg);
1811 for (pos_type i = 0; i < size(); ++i) {
1812 // First char in paragraph or after label?
1813 if (i == body_pos) {
1816 column += running_font.latexWriteEndChanges(
1817 os, bparams, runparams,
1818 basefont, basefont);
1821 basefont = getLayoutFont(bparams, outerfont);
1822 running_font = basefont;
1824 column += Changes::latexMarkChange(os, bparams,
1825 runningChange, Change(Change::UNCHANGED));
1826 runningChange = Change(Change::UNCHANGED);
1831 if (style->isCommand()) {
1837 column += startTeXParParams(bparams, os,
1839 runparams.moving_arg);
1842 Change const & change = runparams.inDeletedInset ? runparams.changeOfDeletedInset
1845 if (bparams.outputChanges && runningChange != change) {
1847 column += running_font.latexWriteEndChanges(
1848 os, bparams, runparams, basefont, basefont);
1851 basefont = getLayoutFont(bparams, outerfont);
1852 running_font = basefont;
1854 column += Changes::latexMarkChange(os, bparams, runningChange, change);
1855 runningChange = change;
1858 // do not output text which is marked deleted
1859 // if change tracking output is disabled
1860 if (!bparams.outputChanges && change.type == Change::DELETED) {
1866 value_type const c = getChar(i);
1868 // Fully instantiated font
1869 Font const font = getFont(bparams, i, outerfont);
1871 Font const last_font = running_font;
1873 // Do we need to close the previous font?
1875 (font != running_font ||
1876 font.language() != running_font.language()))
1878 column += running_font.latexWriteEndChanges(
1879 os, bparams, runparams, basefont,
1880 (i == body_pos-1) ? basefont : font);
1881 running_font = basefont;
1885 // Switch file encoding if necessary
1886 if (runparams.encoding->package() == Encoding::inputenc &&
1887 font.language()->encoding()->package() == Encoding::inputenc) {
1888 std::pair<bool, int> const enc_switch = switchEncoding(os, bparams,
1889 runparams.moving_arg, *(runparams.encoding),
1890 *(font.language()->encoding()));
1891 if (enc_switch.first) {
1892 column += enc_switch.second;
1893 runparams.encoding = font.language()->encoding();
1897 // Do we need to change font?
1898 if ((font != running_font ||
1899 font.language() != running_font.language()) &&
1902 odocstringstream ods;
1903 column += font.latexWriteStartChanges(ods, bparams,
1904 runparams, basefont,
1906 running_font = font;
1908 docstring fontchange = ods.str();
1909 // check if the fontchange ends with a trailing blank
1910 // (like "\small " (see bug 3382)
1911 if (suffixIs(fontchange, ' ') && c == ' ')
1912 os << fontchange.substr(0, fontchange.size() - 1)
1913 << from_ascii("{}");
1919 // Do not print the separation of the optional argument
1920 // if style->pass_thru is false. This works because
1921 // simpleTeXSpecialChars ignores spaces if
1922 // style->pass_thru is false.
1923 if (i != body_pos - 1) {
1924 if (d->simpleTeXBlanks(
1925 *(runparams.encoding), os, texrow,
1926 i, column, font, *style))
1927 // A surrogate pair was output. We
1928 // must not call simpleTeXSpecialChars
1929 // in this iteration, since
1930 // simpleTeXBlanks incremented i, and
1931 // simpleTeXSpecialChars would output
1932 // the combining character again.
1937 OutputParams rp = runparams;
1938 rp.free_spacing = style->free_spacing;
1939 rp.local_font = &font;
1940 rp.intitle = style->intitle;
1941 d->simpleTeXSpecialChars(buf, bparams, os,
1942 texrow, rp, running_font,
1943 basefont, outerfont, open_font,
1944 runningChange, *style, i, column, c);
1946 // Set the encoding to that returned from simpleTeXSpecialChars (see
1947 // comment for encoding member in OutputParams.h)
1948 runparams.encoding = rp.encoding;
1951 // If we have an open font definition, we have to close it
1953 #ifdef FIXED_LANGUAGE_END_DETECTION
1956 .latexWriteEndChanges(os, bparams, runparams,
1958 next_->getFont(bparams, 0, outerfont));
1960 running_font.latexWriteEndChanges(os, bparams,
1961 runparams, basefont, basefont);
1964 //FIXME: For now we ALWAYS have to close the foreign font settings if they are
1965 //FIXME: there as we start another \selectlanguage with the next paragraph if
1966 //FIXME: we are in need of this. This should be fixed sometime (Jug)
1967 running_font.latexWriteEndChanges(os, bparams, runparams,
1968 basefont, basefont);
1972 column += Changes::latexMarkChange(os, bparams, runningChange, Change(Change::UNCHANGED));
1974 // Needed if there is an optional argument but no contents.
1975 if (body_pos > 0 && body_pos == size()) {
1977 return_value = false;
1981 column += endTeXParParams(bparams, os, texrow,
1982 runparams.moving_arg);
1985 LYXERR(Debug::LATEX) << "SimpleTeXOnePar...done " << this << endl;
1986 return return_value;
2003 string tag_name(PAR_TAG const & pt) {
2005 case PAR_NONE: return "!-- --";
2006 case TT: return "tt";
2007 case SF: return "sf";
2008 case BF: return "bf";
2009 case IT: return "it";
2010 case SL: return "sl";
2011 case EM: return "em";
2018 void operator|=(PAR_TAG & p1, PAR_TAG const & p2)
2020 p1 = static_cast<PAR_TAG>(p1 | p2);
2025 void reset(PAR_TAG & p1, PAR_TAG const & p2)
2027 p1 = static_cast<PAR_TAG>(p1 & ~p2);
2033 bool Paragraph::emptyTag() const
2035 for (pos_type i = 0; i < size(); ++i) {
2037 Inset const * inset = getInset(i);
2038 InsetCode lyx_code = inset->lyxCode();
2039 if (lyx_code != TOC_CODE &&
2040 lyx_code != INCLUDE_CODE &&
2041 lyx_code != GRAPHICS_CODE &&
2042 lyx_code != ERT_CODE &&
2043 lyx_code != LISTINGS_CODE &&
2044 lyx_code != FLOAT_CODE &&
2045 lyx_code != TABULAR_CODE) {
2049 value_type c = getChar(i);
2050 if (c != ' ' && c != '\t')
2058 string Paragraph::getID(Buffer const & buf, OutputParams const & runparams) const
2060 for (pos_type i = 0; i < size(); ++i) {
2062 Inset const * inset = getInset(i);
2063 InsetCode lyx_code = inset->lyxCode();
2064 if (lyx_code == LABEL_CODE) {
2065 string const id = static_cast<InsetCommand const *>(inset)->getContents();
2066 return "id='" + to_utf8(sgml::cleanID(buf, runparams, from_utf8(id))) + "'";
2075 pos_type Paragraph::getFirstWord(Buffer const & buf, odocstream & os, OutputParams const & runparams) const
2078 for (i = 0; i < size(); ++i) {
2080 Inset const * inset = getInset(i);
2081 inset->docbook(buf, os, runparams);
2083 value_type c = getChar(i);
2086 os << sgml::escapeChar(c);
2093 bool Paragraph::onlyText(Buffer const & buf, Font const & outerfont, pos_type initial) const
2097 for (pos_type i = initial; i < size(); ++i) {
2098 Font font = getFont(buf.params(), i, outerfont);
2101 if (i != initial && font != font_old)
2110 void Paragraph::simpleDocBookOnePar(Buffer const & buf,
2112 OutputParams const & runparams,
2113 Font const & outerfont,
2114 pos_type initial) const
2116 bool emph_flag = false;
2118 LayoutPtr const & style = layout();
2120 style->labeltype == LABEL_MANUAL ? style->labelfont : style->font;
2122 if (style->pass_thru && !onlyText(buf, outerfont, initial))
2125 // parsing main loop
2126 for (pos_type i = initial; i < size(); ++i) {
2127 Font font = getFont(buf.params(), i, outerfont);
2129 // handle <emphasis> tag
2130 if (font_old.emph() != font.emph()) {
2131 if (font.emph() == Font::ON) {
2134 } else if (i != initial) {
2135 os << "</emphasis>";
2141 Inset const * inset = getInset(i);
2142 inset->docbook(buf, os, runparams);
2144 value_type c = getChar(i);
2146 if (style->pass_thru)
2149 os << sgml::escapeChar(c);
2155 os << "</emphasis>";
2158 if (style->free_spacing)
2160 if (style->pass_thru && !onlyText(buf, outerfont, initial))
2165 bool Paragraph::isHfill(pos_type pos) const
2168 && getInset(pos)->lyxCode() == HFILL_CODE;
2172 bool Paragraph::isNewline(pos_type pos) const
2175 && getInset(pos)->lyxCode() == NEWLINE_CODE;
2179 bool Paragraph::isLineSeparator(pos_type pos) const
2181 value_type const c = getChar(pos);
2182 return isLineSeparatorChar(c)
2183 || (c == Paragraph::META_INSET && getInset(pos) &&
2184 getInset(pos)->isLineSeparator());
2188 /// Used by the spellchecker
2189 bool Paragraph::isLetter(pos_type pos) const
2192 return getInset(pos)->isLetter();
2194 value_type const c = getChar(pos);
2195 return isLetterChar(c) || isDigit(c);
2201 Paragraph::getParLanguage(BufferParams const & bparams) const
2204 return getFirstFontSettings(bparams).language();
2205 // FIXME: we should check the prev par as well (Lgb)
2206 return bparams.language;
2210 bool Paragraph::isRTL(BufferParams const & bparams) const
2212 return lyxrc.rtl_support
2213 && getParLanguage(bparams)->rightToLeft()
2214 && ownerCode() != ERT_CODE
2215 && ownerCode() != LISTINGS_CODE;
2219 void Paragraph::changeLanguage(BufferParams const & bparams,
2220 Language const * from, Language const * to)
2222 // change language including dummy font change at the end
2223 for (pos_type i = 0; i <= size(); ++i) {
2224 Font font = getFontSettings(bparams, i);
2225 if (font.language() == from) {
2226 font.setLanguage(to);
2233 bool Paragraph::isMultiLingual(BufferParams const & bparams) const
2235 Language const * doc_language = bparams.language;
2236 FontList::const_iterator cit = d->fontlist_.begin();
2237 FontList::const_iterator end = d->fontlist_.end();
2239 for (; cit != end; ++cit)
2240 if (cit->font().language() != ignore_language &&
2241 cit->font().language() != latex_language &&
2242 cit->font().language() != doc_language)
2248 // Convert the paragraph to a string.
2249 // Used for building the table of contents
2250 docstring const Paragraph::asString(Buffer const & buffer, bool label) const
2252 return asString(buffer, 0, size(), label);
2256 docstring const Paragraph::asString(Buffer const & buffer,
2257 pos_type beg, pos_type end, bool label) const
2260 odocstringstream os;
2262 if (beg == 0 && label && !params().labelString().empty())
2263 os << params().labelString() << ' ';
2265 for (pos_type i = beg; i < end; ++i) {
2266 value_type const c = getChar(i);
2269 else if (c == META_INSET)
2270 getInset(i)->textString(buffer, os);
2277 void Paragraph::setInsetOwner(Inset * inset)
2279 d->inset_owner_ = inset;
2283 int Paragraph::id() const
2289 LayoutPtr const & Paragraph::layout() const
2295 void Paragraph::layout(LayoutPtr const & new_layout)
2297 layout_ = new_layout;
2301 Inset * Paragraph::inInset() const
2303 return d->inset_owner_;
2307 InsetCode Paragraph::ownerCode() const
2309 return d->inset_owner_ ?
2310 d->inset_owner_->lyxCode() : NO_CODE;
2314 ParagraphParameters & Paragraph::params()
2320 ParagraphParameters const & Paragraph::params() const
2326 bool Paragraph::isFreeSpacing() const
2328 if (layout()->free_spacing)
2331 // for now we just need this, later should we need this in some
2332 // other way we can always add a function to Inset too.
2333 return ownerCode() == ERT_CODE || ownerCode() == LISTINGS_CODE;
2337 bool Paragraph::allowEmpty() const
2339 if (layout()->keepempty)
2341 return ownerCode() == ERT_CODE || ownerCode() == LISTINGS_CODE;
2345 char_type Paragraph::transformChar(char_type c, pos_type pos) const
2347 if (!Encodings::is_arabic(c))
2350 value_type prev_char = ' ';
2351 value_type next_char = ' ';
2353 for (pos_type i = pos - 1; i >= 0; --i) {
2354 value_type const par_char = getChar(i);
2355 if (!Encodings::isComposeChar_arabic(par_char)) {
2356 prev_char = par_char;
2361 for (pos_type i = pos + 1, end = size(); i < end; ++i) {
2362 value_type const par_char = getChar(i);
2363 if (!Encodings::isComposeChar_arabic(par_char)) {
2364 next_char = par_char;
2369 if (Encodings::is_arabic(next_char)) {
2370 if (Encodings::is_arabic(prev_char) &&
2371 !Encodings::is_arabic_special(prev_char))
2372 return Encodings::transformChar(c, Encodings::FORM_MEDIAL);
2374 return Encodings::transformChar(c, Encodings::FORM_INITIAL);
2376 if (Encodings::is_arabic(prev_char) &&
2377 !Encodings::is_arabic_special(prev_char))
2378 return Encodings::transformChar(c, Encodings::FORM_FINAL);
2380 return Encodings::transformChar(c, Encodings::FORM_ISOLATED);
2385 int Paragraph::checkBiblio(bool track_changes)
2388 //This is getting more and more a mess. ...We really should clean
2389 //up this bibitem issue for 1.6. See also bug 2743.
2391 // Add bibitem insets if necessary
2392 if (layout()->labeltype != LABEL_BIBLIO)
2395 bool hasbibitem = !d->insetlist_.empty()
2396 // Insist on it being in pos 0
2397 && getChar(0) == Paragraph::META_INSET
2398 && d->insetlist_.begin()->inset->lyxCode() == BIBITEM_CODE;
2403 // remove a bibitem in pos != 0
2404 // restore it later in pos 0 if necessary
2405 // (e.g. if a user inserts contents _before_ the item)
2406 // we're assuming there's only one of these, which there
2408 int erasedInsetPosition = -1;
2409 InsetList::iterator it = d->insetlist_.begin();
2410 InsetList::iterator end = d->insetlist_.end();
2411 for (; it != end; ++it)
2412 if (it->inset->lyxCode() == BIBITEM_CODE
2414 InsetBibitem * olditem = static_cast<InsetBibitem *>(it->inset);
2415 oldkey = olditem->getParam("key");
2416 oldlabel = olditem->getParam("label");
2417 erasedInsetPosition = it->pos;
2418 eraseChar(erasedInsetPosition, track_changes);
2422 //There was an InsetBibitem at the beginning, and we didn't
2423 //have to erase one.
2424 if (hasbibitem && erasedInsetPosition < 0)
2427 //There was an InsetBibitem at the beginning and we did have to
2428 //erase one. So we give its properties to the beginning inset.
2430 InsetBibitem * inset =
2431 static_cast<InsetBibitem *>(d->insetlist_.begin()->inset);
2432 if (!oldkey.empty())
2433 inset->setParam("key", oldkey);
2434 inset->setParam("label", oldlabel);
2435 return -erasedInsetPosition;
2438 //There was no inset at the beginning, so we need to create one with
2439 //the key and label of the one we erased.
2440 InsetBibitem * inset(new InsetBibitem(InsetCommandParams(BIBITEM_CODE)));
2441 // restore values of previously deleted item in this par.
2442 if (!oldkey.empty())
2443 inset->setParam("key", oldkey);
2444 inset->setParam("label", oldlabel);
2445 insertInset(0, static_cast<Inset *>(inset),
2446 Change(track_changes ? Change::INSERTED : Change::UNCHANGED));
2452 void Paragraph::checkAuthors(AuthorList const & authorList)
2454 d->changes_.checkAuthors(authorList);
2458 bool Paragraph::isUnchanged(pos_type pos) const
2460 return lookupChange(pos).type == Change::UNCHANGED;
2464 bool Paragraph::isInserted(pos_type pos) const
2466 return lookupChange(pos).type == Change::INSERTED;
2470 bool Paragraph::isDeleted(pos_type pos) const
2472 return lookupChange(pos).type == Change::DELETED;
2476 InsetList const & Paragraph::insetList() const
2478 return d->insetlist_;
2482 Inset * Paragraph::releaseInset(pos_type pos)
2484 Inset * inset = d->insetlist_.release(pos);
2485 /// does not honour change tracking!
2486 eraseChar(pos, false);
2491 Inset * Paragraph::getInset(pos_type pos)
2493 return d->insetlist_.get(pos);
2497 Inset const * Paragraph::getInset(pos_type pos) const
2499 return d->insetlist_.get(pos);