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 // is there a font change in middle of the word?
604 FontList::const_iterator cit = fontlist_.begin();
605 FontList::const_iterator end = fontlist_.end();
606 for (; cit != end; ++cit) {
607 if (cit->pos() >= pos)
610 if (cit != end && pos + len - 1 > cit->pos())
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.
647 case Paragraph::META_INSET: {
648 Inset * inset = owner_->getInset(i);
650 // FIXME: remove this check
654 // FIXME: move this to InsetNewline::latex
655 if (inset->lyxCode() == NEWLINE_CODE) {
656 // newlines are handled differently here than
657 // the default in simpleTeXSpecialChars().
658 if (!style.newline_allowed) {
662 column += running_font.latexWriteEndChanges(
663 os, bparams, runparams,
668 if (running_font.family() == Font::TYPEWRITER_FAMILY)
671 basefont = owner_->getLayoutFont(bparams, outerfont);
672 running_font = basefont;
674 if (runparams.moving_arg)
680 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)
714 // Some insets cannot be inside a font change command.
715 // However, even such insets *can* be placed in \L or \R
716 // or their equivalents (for RTL language switches), so we don't
717 // close the language in those cases.
718 // ArabTeX, though, cannot handle this special behavior, it seems.
719 bool arabtex = basefont.language()->lang() == "arabic_arabtex" ||
720 running_font.language()->lang() == "arabic_arabtex";
721 if (open_font && inset->noFontChange()) {
722 bool closeLanguage = arabtex ||
723 basefont.isRightToLeft() == running_font.isRightToLeft();
724 unsigned int count = running_font.latexWriteEndChanges(
725 os, bparams, runparams,
726 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;
771 // And now for the special cases within each mode
775 os << "\\textbackslash{}";
779 case '|': case '<': case '>':
780 // In T1 encoding, these characters exist
781 if (lyxrc.fontenc == "T1") {
783 //... but we should avoid ligatures
784 if ((c == '>' || c == '<')
786 && getChar(i + 1) == c) {
787 //os << "\\textcompwordmark{}";
789 // Jean-Marc, have a look at
790 // this. I think this works
798 // Typewriter font also has them
799 if (running_font.family() == Font::TYPEWRITER_FAMILY) {
803 // Otherwise, we use what LaTeX
807 os << "\\textless{}";
811 os << "\\textgreater{}";
821 case '-': // "--" in Typewriter mode -> "-{}-"
822 if (i <= size() - 2 &&
823 getChar(i + 1) == '-' &&
824 running_font.family() == Font::TYPEWRITER_FAMILY) {
833 os << "\\char`\\\"{}";
838 case '%': case '#': case '{':
846 os << "\\textasciitilde{}";
851 os << "\\textasciicircum{}";
856 // avoid being mistaken for optional arguments
864 // Blanks are printed before font switching.
865 // Sure? I am not! (try nice-latex)
866 // I am sure it's correct. LyX might be smarter
867 // in the future, but for now, nothing wrong is
873 // I assume this is hack treating typewriter as verbatim
874 // FIXME UNICODE: This can fail if c cannot be encoded
875 // in the current encoding.
876 if (running_font.family() == Font::TYPEWRITER_FAMILY) {
885 // FIXME: if we have "LaTeX" with a font
886 // change in the middle (before the 'T', then
887 // the "TeX" part is still special cased.
888 // Really we should only operate this on
889 // "words" for some definition of word
893 for (; pnr < phrases_nr; ++pnr) {
894 if (isTextAt(special_phrases[pnr].phrase, i)) {
895 os << special_phrases[pnr].macro;
896 i += special_phrases[pnr].phrase.length() - 1;
897 column += special_phrases[pnr].macro.length() - 1;
902 if (pnr == phrases_nr && c != '\0') {
903 Encoding const & encoding = *(runparams.encoding);
904 if (i + 1 < size()) {
905 char_type next = getChar(i + 1);
906 if (Encodings::isCombiningChar(next)) {
907 column += latexSurrogatePair(os, c, next, encoding) - 1;
913 if (Encodings::isKnownLangChar(c, preamble)) {
915 knownLangChars(os, c, preamble,
920 docstring const latex = encoding.latexChar(c);
921 if (latex.length() > 1 &&
922 latex[latex.length() - 1] != '}') {
923 // Prevent eating of a following
924 // space or command corruption by
925 // following characters
926 column += latex.length() + 1;
929 column += latex.length() - 1;
939 void Paragraph::Private::validate(LaTeXFeatures & features,
940 Layout const & layout) const
942 BufferParams const & bparams = features.bufferParams();
945 if (!params_.spacing().isDefault())
946 features.require("setspace");
949 features.useLayout(layout.name());
952 Language const * doc_language = bparams.language;
954 FontList::const_iterator fcit = fontlist_.begin();
955 FontList::const_iterator fend = fontlist_.end();
956 for (; fcit != fend; ++fcit) {
957 if (fcit->font().noun() == Font::ON) {
958 LYXERR(Debug::LATEX) << "font.noun: "
959 << fcit->font().noun()
961 features.require("noun");
962 LYXERR(Debug::LATEX) << "Noun enabled. Font: "
963 << to_utf8(fcit->font().stateText(0))
966 switch (fcit->font().color()) {
970 // probably we should put here all interface colors used for
971 // font displaying! For now I just add this ones I know of (Jug)
976 features.require("color");
977 LYXERR(Debug::LATEX) << "Color enabled. Font: "
978 << to_utf8(fcit->font().stateText(0))
982 Language const * language = fcit->font().language();
983 if (language->babel() != doc_language->babel() &&
984 language != ignore_language &&
985 language != latex_language)
987 features.useLanguage(language);
988 LYXERR(Debug::LATEX) << "Found language "
989 << language->lang() << endl;
993 if (!params_.leftIndent().zero())
994 features.require("ParagraphLeftIndent");
997 InsetList::const_iterator icit = insetlist_.begin();
998 InsetList::const_iterator iend = insetlist_.end();
999 for (; icit != iend; ++icit) {
1001 icit->inset->validate(features);
1002 if (layout.needprotect &&
1003 icit->inset->lyxCode() == FOOT_CODE)
1004 features.require("NeedLyXFootnoteCode");
1008 // then the contents
1009 for (pos_type i = 0; i < size() ; ++i) {
1010 for (size_t pnr = 0; pnr < phrases_nr; ++pnr) {
1011 if (!special_phrases[pnr].builtin
1012 && isTextAt(special_phrases[pnr].phrase, i)) {
1013 features.require(special_phrases[pnr].phrase);
1017 Encodings::validate(getChar(i), features);
1025 /////////////////////////////////////////////////////////////////////
1029 /////////////////////////////////////////////////////////////////////
1033 Paragraph::Paragraph()
1034 : begin_of_body_(0), d(new Paragraph::Private(this))
1041 Paragraph::Paragraph(Paragraph const & par)
1042 : itemdepth(par.itemdepth),
1043 layout_(par.layout_),
1044 text_(par.text_), begin_of_body_(par.begin_of_body_),
1045 d(new Paragraph::Private(*par.d, this))
1050 Paragraph & Paragraph::operator=(Paragraph const & par)
1052 // needed as we will destroy the private part before copying it
1054 itemdepth = par.itemdepth;
1055 layout_ = par.layout();
1057 begin_of_body_ = par.begin_of_body_;
1060 d = new Private(*par.d, this);
1066 Paragraph::~Paragraph()
1072 void Paragraph::write(Buffer const & buf, ostream & os,
1073 BufferParams const & bparams,
1074 depth_type & dth) const
1076 // The beginning or end of a deeper (i.e. nested) area?
1077 if (dth != params().depth()) {
1078 if (params().depth() > dth) {
1079 while (params().depth() > dth) {
1080 os << "\n\\begin_deeper";
1084 while (params().depth() < dth) {
1085 os << "\n\\end_deeper";
1091 // First write the layout
1092 os << "\n\\begin_layout " << to_utf8(layout()->name()) << '\n';
1096 Font font1(Font::ALL_INHERIT, bparams.language);
1098 Change running_change = Change(Change::UNCHANGED);
1101 for (pos_type i = 0; i <= size(); ++i) {
1103 Change change = lookupChange(i);
1104 Changes::lyxMarkChange(os, column, running_change, change);
1105 running_change = change;
1110 // Write font changes
1111 Font font2 = getFontSettings(bparams, i);
1112 if (font2 != font1) {
1113 font2.lyxWriteChanges(font1, os);
1118 value_type const c = getChar(i);
1122 Inset const * inset = getInset(i);
1124 if (inset->directWrite()) {
1125 // international char, let it write
1126 // code directly so it's shorter in
1128 inset->write(buf, os);
1132 os << "\\begin_inset ";
1133 inset->write(buf, os);
1134 os << "\n\\end_inset\n\n";
1140 os << "\n\\backslash\n";
1144 if (i + 1 < size() && getChar(i + 1) == ' ') {
1151 if ((column > 70 && c == ' ')
1156 // this check is to amend a bug. LyX sometimes
1157 // inserts '\0' this could cause problems.
1159 std::vector<char> tmp = ucs4_to_utf8(c);
1160 tmp.push_back('\0');
1163 lyxerr << "ERROR (Paragraph::writeFile):"
1164 " NULL char in structure." << endl;
1170 os << "\n\\end_layout\n";
1174 void Paragraph::validate(LaTeXFeatures & features) const
1176 d->validate(features, *layout());
1180 void Paragraph::insert(pos_type start, docstring const & str,
1181 Font const & font, Change const & change)
1183 for (size_t i = 0, n = str.size(); i != n ; ++i)
1184 insertChar(start + i, str[i], font, change);
1188 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c,
1191 d->insertChar(pos, c, Change(trackChanges ?
1192 Change::INSERTED : Change::UNCHANGED));
1196 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c,
1197 Font const & font, bool trackChanges)
1199 d->insertChar(pos, c, Change(trackChanges ?
1200 Change::INSERTED : Change::UNCHANGED));
1205 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c,
1206 Font const & font, Change const & change)
1208 d->insertChar(pos, c, change);
1213 void Paragraph::insertInset(pos_type pos, Inset * inset,
1214 Font const & font, Change const & change)
1216 insertInset(pos, inset, change);
1217 // Set the font/language of the inset...
1222 bool Paragraph::insetAllowed(InsetCode code)
1224 return !d->inset_owner_ || d->inset_owner_->insetAllowed(code);
1228 // Gets uninstantiated font setting at position.
1229 Font const Paragraph::getFontSettings(BufferParams const & bparams,
1233 lyxerr << " pos: " << pos << " size: " << size() << endl;
1234 BOOST_ASSERT(pos <= size());
1237 FontList::const_iterator cit = d->fontlist_.begin();
1238 FontList::const_iterator end = d->fontlist_.end();
1239 for (; cit != end; ++cit)
1240 if (cit->pos() >= pos)
1246 if (pos == size() && !empty())
1247 return getFontSettings(bparams, pos - 1);
1249 return Font(Font::ALL_INHERIT, getParLanguage(bparams));
1253 FontSpan Paragraph::fontSpan(pos_type pos) const
1255 BOOST_ASSERT(pos <= size());
1258 FontList::const_iterator cit = d->fontlist_.begin();
1259 FontList::const_iterator end = d->fontlist_.end();
1260 for (; cit != end; ++cit) {
1261 if (cit->pos() >= pos) {
1262 if (pos >= beginOfBody())
1263 return FontSpan(std::max(start, beginOfBody()),
1266 return FontSpan(start,
1267 std::min(beginOfBody() - 1,
1270 start = cit->pos() + 1;
1273 // This should not happen, but if so, we take no chances.
1274 //lyxerr << "Paragraph::getEndPosOfFontSpan: This should not happen!"
1276 return FontSpan(pos, pos);
1280 // Gets uninstantiated font setting at position 0
1281 Font const Paragraph::getFirstFontSettings(BufferParams const & bparams) const
1283 if (!empty() && !d->fontlist_.empty())
1284 return d->fontlist_.begin()->font();
1286 return Font(Font::ALL_INHERIT, bparams.language);
1290 // Gets the fully instantiated font at a given position in a paragraph
1291 // This is basically the same function as Text::GetFont() in text2.cpp.
1292 // The difference is that this one is used for generating the LaTeX file,
1293 // and thus cosmetic "improvements" are disallowed: This has to deliver
1294 // the true picture of the buffer. (Asger)
1295 Font const Paragraph::getFont(BufferParams const & bparams, pos_type pos,
1296 Font const & outerfont) const
1298 BOOST_ASSERT(pos >= 0);
1300 Font font = getFontSettings(bparams, pos);
1302 pos_type const body_pos = beginOfBody();
1304 font.realize(layout_->labelfont);
1306 font.realize(layout_->font);
1308 font.realize(outerfont);
1309 font.realize(bparams.getFont());
1315 Font const Paragraph::getLabelFont
1316 (BufferParams const & bparams, Font const & outerfont) const
1318 Font tmpfont = layout()->labelfont;
1319 tmpfont.setLanguage(getParLanguage(bparams));
1320 tmpfont.realize(outerfont);
1321 tmpfont.realize(bparams.getFont());
1326 Font const Paragraph::getLayoutFont
1327 (BufferParams const & bparams, Font const & outerfont) const
1329 Font tmpfont = layout()->font;
1330 tmpfont.setLanguage(getParLanguage(bparams));
1331 tmpfont.realize(outerfont);
1332 tmpfont.realize(bparams.getFont());
1337 /// Returns the height of the highest font in range
1338 Font_size Paragraph::highestFontInRange
1339 (pos_type startpos, pos_type endpos, Font_size def_size) const
1341 if (d->fontlist_.empty())
1344 FontList::const_iterator end_it = d->fontlist_.begin();
1345 FontList::const_iterator const end = d->fontlist_.end();
1346 for (; end_it != end; ++end_it) {
1347 if (end_it->pos() >= endpos)
1354 FontList::const_iterator cit = d->fontlist_.begin();
1355 for (; cit != end; ++cit) {
1356 if (cit->pos() >= startpos)
1360 Font::FONT_SIZE maxsize = Font::SIZE_TINY;
1361 for (; cit != end_it; ++cit) {
1362 Font::FONT_SIZE size = cit->font().size();
1363 if (size == Font::INHERIT_SIZE)
1365 if (size > maxsize && size <= Font::SIZE_HUGER)
1372 Paragraph::value_type
1373 Paragraph::getUChar(BufferParams const & bparams, pos_type pos) const
1375 value_type c = getChar(pos);
1376 if (!lyxrc.rtl_support)
1406 if (uc != c && getFontSettings(bparams, pos).isRightToLeft())
1413 void Paragraph::setFont(pos_type pos, Font const & font)
1415 BOOST_ASSERT(pos <= size());
1417 // First, reduce font against layout/label font
1418 // Update: The setCharFont() routine in text2.cpp already
1419 // reduces font, so we don't need to do that here. (Asger)
1421 d->fontlist_.set(pos, font);
1425 void Paragraph::makeSameLayout(Paragraph const & par)
1427 layout(par.layout());
1429 d->params_ = par.params();
1433 bool Paragraph::stripLeadingSpaces(bool trackChanges)
1435 if (isFreeSpacing())
1441 while (pos < size() && (isNewline(pos) || isLineSeparator(pos))) {
1442 if (eraseChar(pos, trackChanges))
1448 return count > 0 || pos > 0;
1452 bool Paragraph::hasSameLayout(Paragraph const & par) const
1454 return par.layout() == layout() && d->params_.sameLayout(par.params());
1458 depth_type Paragraph::getDepth() const
1460 return params().depth();
1464 depth_type Paragraph::getMaxDepthAfter() const
1466 if (layout()->isEnvironment())
1467 return params().depth() + 1;
1469 return params().depth();
1473 char Paragraph::getAlign() const
1475 if (params().align() == LYX_ALIGN_LAYOUT)
1476 return layout()->align;
1478 return params().align();
1482 docstring const & Paragraph::getLabelstring() const
1484 return params().labelString();
1488 // the next two functions are for the manual labels
1489 docstring const Paragraph::getLabelWidthString() const
1491 if (layout()->margintype == MARGIN_MANUAL)
1492 return params().labelWidthString();
1494 return _("Senseless with this layout!");
1498 void Paragraph::setLabelWidthString(docstring const & s)
1500 params().labelWidthString(s);
1504 docstring const Paragraph::translateIfPossible(docstring const & s,
1505 BufferParams const & bparams) const
1507 if (!support::isAscii(s) || s.empty()) {
1508 // This must be a user defined layout. We cannot translate
1509 // this, since gettext accepts only ascii keys.
1512 // Probably standard layout, try to translate
1513 Messages & m = getMessages(getParLanguage(bparams)->code());
1514 return m.get(to_ascii(s));
1518 docstring Paragraph::expandLabel(LayoutPtr const & layout,
1519 BufferParams const & bparams, bool process_appendix) const
1521 TextClass const & tclass = bparams.getTextClass();
1524 if (process_appendix && params().appendix())
1525 fmt = translateIfPossible(layout->labelstring_appendix(),
1528 fmt = translateIfPossible(layout->labelstring(), bparams);
1530 if (fmt.empty() && layout->labeltype == LABEL_COUNTER
1531 && !layout->counter.empty())
1532 fmt = "\\the" + layout->counter;
1534 // handle 'inherited level parts' in 'fmt',
1535 // i.e. the stuff between '@' in '@Section@.\arabic{subsection}'
1536 size_t const i = fmt.find('@', 0);
1537 if (i != docstring::npos) {
1538 size_t const j = fmt.find('@', i + 1);
1539 if (j != docstring::npos) {
1540 docstring parent(fmt, i + 1, j - i - 1);
1541 docstring label = from_ascii("??");
1542 if (tclass.hasLayout(parent))
1543 docstring label = expandLabel(tclass[parent], bparams,
1545 fmt = docstring(fmt, 0, i) + label
1546 + docstring(fmt, j + 1, docstring::npos);
1550 return tclass.counters().counterLabel(fmt);
1554 void Paragraph::applyLayout(LayoutPtr const & new_layout)
1557 LyXAlignment const oldAlign = params().align();
1559 if (!(oldAlign & layout()->alignpossible)) {
1560 frontend::Alert::warning(_("Alignment not permitted"),
1561 _("The new layout does not permit the alignment previously used.\nSetting to default."));
1562 params().align(LYX_ALIGN_LAYOUT);
1567 pos_type Paragraph::beginOfBody() const
1569 return begin_of_body_;
1573 void Paragraph::setBeginOfBody()
1575 if (layout()->labeltype != LABEL_MANUAL) {
1580 // Unroll the first two cycles of the loop
1581 // and remember the previous character to
1582 // remove unnecessary getChar() calls
1584 pos_type end = size();
1585 if (i < end && !isNewline(i)) {
1587 char_type previous_char = 0;
1590 previous_char = text_[i];
1591 if (!isNewline(i)) {
1593 while (i < end && previous_char != ' ') {
1598 previous_char = temp;
1608 // returns -1 if inset not found
1609 int Paragraph::getPositionOfInset(Inset const * inset) const
1612 InsetList::const_iterator it = d->insetlist_.begin();
1613 InsetList::const_iterator end = d->insetlist_.end();
1614 for (; it != end; ++it)
1615 if (it->inset == inset)
1621 InsetBibitem * Paragraph::bibitem() const
1623 if (!d->insetlist_.empty()) {
1624 Inset * inset = d->insetlist_.begin()->inset;
1625 if (inset->lyxCode() == BIBITEM_CODE)
1626 return static_cast<InsetBibitem *>(inset);
1632 bool Paragraph::forceDefaultParagraphs() const
1634 return inInset() && inInset()->forceDefaultParagraphs(0);
1640 // paragraphs inside floats need different alignment tags to avoid
1643 bool noTrivlistCentering(InsetCode code)
1645 return code == FLOAT_CODE || code == WRAP_CODE;
1649 string correction(string const & orig)
1651 if (orig == "flushleft")
1652 return "raggedright";
1653 if (orig == "flushright")
1654 return "raggedleft";
1655 if (orig == "center")
1661 string const corrected_env(string const & suffix, string const & env,
1664 string output = suffix + "{";
1665 if (noTrivlistCentering(code))
1666 output += correction(env);
1670 if (suffix == "\\begin")
1676 void adjust_row_column(string const & str, TexRow & texrow, int & column)
1678 if (!contains(str, "\n"))
1679 column += str.size();
1683 column = rsplit(str, tmp, '\n').size();
1690 // This could go to ParagraphParameters if we want to
1691 int Paragraph::startTeXParParams(BufferParams const & bparams,
1692 odocstream & os, TexRow & texrow,
1693 bool moving_arg) const
1697 if (params().noindent()) {
1698 os << "\\noindent ";
1702 LyXAlignment const curAlign = params().align();
1704 if (curAlign == layout()->align)
1708 case LYX_ALIGN_NONE:
1709 case LYX_ALIGN_BLOCK:
1710 case LYX_ALIGN_LAYOUT:
1711 case LYX_ALIGN_SPECIAL:
1713 case LYX_ALIGN_LEFT:
1714 case LYX_ALIGN_RIGHT:
1715 case LYX_ALIGN_CENTER:
1724 case LYX_ALIGN_NONE:
1725 case LYX_ALIGN_BLOCK:
1726 case LYX_ALIGN_LAYOUT:
1727 case LYX_ALIGN_SPECIAL:
1729 case LYX_ALIGN_LEFT: {
1731 if (getParLanguage(bparams)->babel() != "hebrew")
1732 output = corrected_env("\\begin", "flushleft", ownerCode());
1734 output = corrected_env("\\begin", "flushright", ownerCode());
1735 os << from_ascii(output);
1736 adjust_row_column(output, texrow, column);
1738 } case LYX_ALIGN_RIGHT: {
1740 if (getParLanguage(bparams)->babel() != "hebrew")
1741 output = corrected_env("\\begin", "flushright", ownerCode());
1743 output = corrected_env("\\begin", "flushleft", ownerCode());
1744 os << from_ascii(output);
1745 adjust_row_column(output, texrow, column);
1747 } case LYX_ALIGN_CENTER: {
1749 output = corrected_env("\\begin", "center", ownerCode());
1750 os << from_ascii(output);
1751 adjust_row_column(output, texrow, column);
1760 // This could go to ParagraphParameters if we want to
1761 int Paragraph::endTeXParParams(BufferParams const & bparams,
1762 odocstream & os, TexRow & texrow,
1763 bool moving_arg) const
1767 switch (params().align()) {
1768 case LYX_ALIGN_NONE:
1769 case LYX_ALIGN_BLOCK:
1770 case LYX_ALIGN_LAYOUT:
1771 case LYX_ALIGN_SPECIAL:
1773 case LYX_ALIGN_LEFT:
1774 case LYX_ALIGN_RIGHT:
1775 case LYX_ALIGN_CENTER:
1783 switch (params().align()) {
1784 case LYX_ALIGN_NONE:
1785 case LYX_ALIGN_BLOCK:
1786 case LYX_ALIGN_LAYOUT:
1787 case LYX_ALIGN_SPECIAL:
1789 case LYX_ALIGN_LEFT: {
1791 if (getParLanguage(bparams)->babel() != "hebrew")
1792 output = corrected_env("\n\\par\\end", "flushleft", ownerCode());
1794 output = corrected_env("\n\\par\\end", "flushright", ownerCode());
1795 os << from_ascii(output);
1796 adjust_row_column(output, texrow, column);
1798 } case LYX_ALIGN_RIGHT: {
1800 if (getParLanguage(bparams)->babel() != "hebrew")
1801 output = corrected_env("\n\\par\\end", "flushright", ownerCode());
1803 output = corrected_env("\n\\par\\end", "flushleft", ownerCode());
1804 os << from_ascii(output);
1805 adjust_row_column(output, texrow, column);
1807 } case LYX_ALIGN_CENTER: {
1809 output = corrected_env("\n\\par\\end", "center", ownerCode());
1810 os << from_ascii(output);
1811 adjust_row_column(output, texrow, column);
1820 // This one spits out the text of the paragraph
1821 bool Paragraph::simpleTeXOnePar(Buffer const & buf,
1822 BufferParams const & bparams,
1823 Font const & outerfont,
1824 odocstream & os, TexRow & texrow,
1825 OutputParams const & runparams) const
1827 LYXERR(Debug::LATEX) << "SimpleTeXOnePar... " << this << endl;
1829 bool return_value = false;
1833 // well we have to check if we are in an inset with unlimited
1834 // length (all in one row) if that is true then we don't allow
1835 // any special options in the paragraph and also we don't allow
1836 // any environment other than the default layout of the text class
1838 bool asdefault = forceDefaultParagraphs();
1841 style = bparams.getTextClass().defaultLayout();
1846 // Current base font for all inherited font changes, without any
1847 // change caused by an individual character, except for the language:
1848 // It is set to the language of the first character.
1849 // As long as we are in the label, this font is the base font of the
1850 // label. Before the first body character it is set to the base font
1854 // Maybe we have to create a optional argument.
1855 pos_type body_pos = beginOfBody();
1856 unsigned int column = 0;
1859 // the optional argument is kept in curly brackets in
1860 // case it contains a ']'
1863 basefont = getLabelFont(bparams, outerfont);
1865 basefont = getLayoutFont(bparams, outerfont);
1868 // Which font is currently active?
1869 Font running_font(basefont);
1870 // Do we have an open font change?
1871 bool open_font = false;
1873 Change runningChange = Change(Change::UNCHANGED);
1875 texrow.start(id(), 0);
1877 // if the paragraph is empty, the loop will not be entered at all
1879 if (style->isCommand()) {
1884 column += startTeXParParams(bparams, os, texrow,
1885 runparams.moving_arg);
1888 for (pos_type i = 0; i < size(); ++i) {
1889 // First char in paragraph or after label?
1890 if (i == body_pos) {
1893 column += running_font.latexWriteEndChanges(
1894 os, bparams, runparams,
1895 basefont, basefont);
1898 basefont = getLayoutFont(bparams, outerfont);
1899 running_font = basefont;
1901 column += Changes::latexMarkChange(os, bparams,
1902 runningChange, Change(Change::UNCHANGED));
1903 runningChange = Change(Change::UNCHANGED);
1908 if (style->isCommand()) {
1914 column += startTeXParParams(bparams, os,
1916 runparams.moving_arg);
1919 Change const & change = runparams.inDeletedInset ? runparams.changeOfDeletedInset
1922 if (bparams.outputChanges && runningChange != change) {
1924 column += running_font.latexWriteEndChanges(
1925 os, bparams, runparams, basefont, basefont);
1928 basefont = getLayoutFont(bparams, outerfont);
1929 running_font = basefont;
1931 column += Changes::latexMarkChange(os, bparams, runningChange, change);
1932 runningChange = change;
1935 // do not output text which is marked deleted
1936 // if change tracking output is disabled
1937 if (!bparams.outputChanges && change.type == Change::DELETED) {
1943 value_type const c = getChar(i);
1945 // Fully instantiated font
1946 Font const font = getFont(bparams, i, outerfont);
1948 Font const last_font = running_font;
1950 // Do we need to close the previous font?
1952 (font != running_font ||
1953 font.language() != running_font.language()))
1955 column += running_font.latexWriteEndChanges(
1956 os, bparams, runparams, basefont,
1957 (i == body_pos-1) ? basefont : font);
1958 running_font = basefont;
1962 // Switch file encoding if necessary
1963 if (runparams.encoding->package() == Encoding::inputenc &&
1964 font.language()->encoding()->package() == Encoding::inputenc) {
1965 std::pair<bool, int> const enc_switch = switchEncoding(os, bparams,
1966 runparams.moving_arg, *(runparams.encoding),
1967 *(font.language()->encoding()));
1968 if (enc_switch.first) {
1969 column += enc_switch.second;
1970 runparams.encoding = font.language()->encoding();
1974 // Do we need to change font?
1975 if ((font != running_font ||
1976 font.language() != running_font.language()) &&
1979 odocstringstream ods;
1980 column += font.latexWriteStartChanges(ods, bparams,
1981 runparams, basefont,
1983 running_font = font;
1985 docstring fontchange = ods.str();
1986 // check if the fontchange ends with a trailing blank
1987 // (like "\small " (see bug 3382)
1988 if (suffixIs(fontchange, ' ') && c == ' ')
1989 os << fontchange.substr(0, fontchange.size() - 1)
1990 << from_ascii("{}");
1996 // Do not print the separation of the optional argument
1997 // if style->pass_thru is false. This works because
1998 // simpleTeXSpecialChars ignores spaces if
1999 // style->pass_thru is false.
2000 if (i != body_pos - 1) {
2001 if (d->simpleTeXBlanks(
2002 *(runparams.encoding), os, texrow,
2003 i, column, font, *style))
2004 // A surrogate pair was output. We
2005 // must not call simpleTeXSpecialChars
2006 // in this iteration, since
2007 // simpleTeXBlanks incremented i, and
2008 // simpleTeXSpecialChars would output
2009 // the combining character again.
2014 OutputParams rp = runparams;
2015 rp.free_spacing = style->free_spacing;
2016 rp.local_font = &font;
2017 rp.intitle = style->intitle;
2018 d->simpleTeXSpecialChars(buf, bparams, os,
2019 texrow, rp, running_font,
2020 basefont, outerfont, open_font,
2021 runningChange, *style, i, column, c);
2023 // Set the encoding to that returned from simpleTeXSpecialChars (see
2024 // comment for encoding member in OutputParams.h)
2025 runparams.encoding = rp.encoding;
2028 // If we have an open font definition, we have to close it
2030 #ifdef FIXED_LANGUAGE_END_DETECTION
2033 .latexWriteEndChanges(os, bparams, runparams,
2035 next_->getFont(bparams, 0, outerfont));
2037 running_font.latexWriteEndChanges(os, bparams,
2038 runparams, basefont, basefont);
2041 //FIXME: For now we ALWAYS have to close the foreign font settings if they are
2042 //FIXME: there as we start another \selectlanguage with the next paragraph if
2043 //FIXME: we are in need of this. This should be fixed sometime (Jug)
2044 running_font.latexWriteEndChanges(os, bparams, runparams,
2045 basefont, basefont);
2049 column += Changes::latexMarkChange(os, bparams, runningChange, Change(Change::UNCHANGED));
2051 // Needed if there is an optional argument but no contents.
2052 if (body_pos > 0 && body_pos == size()) {
2054 return_value = false;
2058 column += endTeXParParams(bparams, os, texrow,
2059 runparams.moving_arg);
2062 LYXERR(Debug::LATEX) << "SimpleTeXOnePar...done " << this << endl;
2063 return return_value;
2080 string tag_name(PAR_TAG const & pt) {
2082 case PAR_NONE: return "!-- --";
2083 case TT: return "tt";
2084 case SF: return "sf";
2085 case BF: return "bf";
2086 case IT: return "it";
2087 case SL: return "sl";
2088 case EM: return "em";
2095 void operator|=(PAR_TAG & p1, PAR_TAG const & p2)
2097 p1 = static_cast<PAR_TAG>(p1 | p2);
2102 void reset(PAR_TAG & p1, PAR_TAG const & p2)
2104 p1 = static_cast<PAR_TAG>(p1 & ~p2);
2110 bool Paragraph::emptyTag() const
2112 for (pos_type i = 0; i < size(); ++i) {
2114 Inset const * inset = getInset(i);
2115 InsetCode lyx_code = inset->lyxCode();
2116 if (lyx_code != TOC_CODE &&
2117 lyx_code != INCLUDE_CODE &&
2118 lyx_code != GRAPHICS_CODE &&
2119 lyx_code != ERT_CODE &&
2120 lyx_code != LISTINGS_CODE &&
2121 lyx_code != FLOAT_CODE &&
2122 lyx_code != TABULAR_CODE) {
2126 value_type c = getChar(i);
2127 if (c != ' ' && c != '\t')
2135 string Paragraph::getID(Buffer const & buf, OutputParams const & runparams) const
2137 for (pos_type i = 0; i < size(); ++i) {
2139 Inset const * inset = getInset(i);
2140 InsetCode lyx_code = inset->lyxCode();
2141 if (lyx_code == LABEL_CODE) {
2142 string const id = static_cast<InsetCommand const *>(inset)->getContents();
2143 return "id='" + to_utf8(sgml::cleanID(buf, runparams, from_utf8(id))) + "'";
2152 pos_type Paragraph::getFirstWord(Buffer const & buf, odocstream & os, OutputParams const & runparams) const
2155 for (i = 0; i < size(); ++i) {
2157 Inset const * inset = getInset(i);
2158 inset->docbook(buf, os, runparams);
2160 value_type c = getChar(i);
2163 os << sgml::escapeChar(c);
2170 bool Paragraph::onlyText(Buffer const & buf, Font const & outerfont, pos_type initial) const
2174 for (pos_type i = initial; i < size(); ++i) {
2175 Font font = getFont(buf.params(), i, outerfont);
2178 if (i != initial && font != font_old)
2187 void Paragraph::simpleDocBookOnePar(Buffer const & buf,
2189 OutputParams const & runparams,
2190 Font const & outerfont,
2191 pos_type initial) const
2193 bool emph_flag = false;
2195 LayoutPtr const & style = layout();
2197 style->labeltype == LABEL_MANUAL ? style->labelfont : style->font;
2199 if (style->pass_thru && !onlyText(buf, outerfont, initial))
2202 // parsing main loop
2203 for (pos_type i = initial; i < size(); ++i) {
2204 Font font = getFont(buf.params(), i, outerfont);
2206 // handle <emphasis> tag
2207 if (font_old.emph() != font.emph()) {
2208 if (font.emph() == Font::ON) {
2211 } else if (i != initial) {
2212 os << "</emphasis>";
2218 Inset const * inset = getInset(i);
2219 inset->docbook(buf, os, runparams);
2221 value_type c = getChar(i);
2223 if (style->pass_thru)
2226 os << sgml::escapeChar(c);
2232 os << "</emphasis>";
2235 if (style->free_spacing)
2237 if (style->pass_thru && !onlyText(buf, outerfont, initial))
2242 bool Paragraph::isHfill(pos_type pos) const
2245 && getInset(pos)->lyxCode() == HFILL_CODE;
2249 bool Paragraph::isNewline(pos_type pos) const
2252 && getInset(pos)->lyxCode() == NEWLINE_CODE;
2256 bool Paragraph::isLineSeparator(pos_type pos) const
2258 value_type const c = getChar(pos);
2259 return isLineSeparatorChar(c)
2260 || (c == Paragraph::META_INSET && getInset(pos) &&
2261 getInset(pos)->isLineSeparator());
2265 /// Used by the spellchecker
2266 bool Paragraph::isLetter(pos_type pos) const
2269 return getInset(pos)->isLetter();
2271 value_type const c = getChar(pos);
2272 return isLetterChar(c) || isDigit(c);
2278 Paragraph::getParLanguage(BufferParams const & bparams) const
2281 return getFirstFontSettings(bparams).language();
2282 // FIXME: we should check the prev par as well (Lgb)
2283 return bparams.language;
2287 bool Paragraph::isRTL(BufferParams const & bparams) const
2289 return lyxrc.rtl_support
2290 && getParLanguage(bparams)->rightToLeft()
2291 && ownerCode() != ERT_CODE
2292 && ownerCode() != LISTINGS_CODE;
2296 void Paragraph::changeLanguage(BufferParams const & bparams,
2297 Language const * from, Language const * to)
2299 // change language including dummy font change at the end
2300 for (pos_type i = 0; i <= size(); ++i) {
2301 Font font = getFontSettings(bparams, i);
2302 if (font.language() == from) {
2303 font.setLanguage(to);
2310 bool Paragraph::isMultiLingual(BufferParams const & bparams) const
2312 Language const * doc_language = bparams.language;
2313 FontList::const_iterator cit = d->fontlist_.begin();
2314 FontList::const_iterator end = d->fontlist_.end();
2316 for (; cit != end; ++cit)
2317 if (cit->font().language() != ignore_language &&
2318 cit->font().language() != latex_language &&
2319 cit->font().language() != doc_language)
2325 // Convert the paragraph to a string.
2326 // Used for building the table of contents
2327 docstring const Paragraph::asString(Buffer const & buffer, bool label) const
2329 return asString(buffer, 0, size(), label);
2333 docstring const Paragraph::asString(Buffer const & buffer,
2334 pos_type beg, pos_type end, bool label) const
2337 odocstringstream os;
2339 if (beg == 0 && label && !params().labelString().empty())
2340 os << params().labelString() << ' ';
2342 for (pos_type i = beg; i < end; ++i) {
2343 value_type const c = getChar(i);
2346 else if (c == META_INSET)
2347 getInset(i)->textString(buffer, os);
2354 void Paragraph::setInsetOwner(Inset * inset)
2356 d->inset_owner_ = inset;
2360 int Paragraph::id() const
2366 LayoutPtr const & Paragraph::layout() const
2372 void Paragraph::layout(LayoutPtr const & new_layout)
2374 layout_ = new_layout;
2378 Inset * Paragraph::inInset() const
2380 return d->inset_owner_;
2384 InsetCode Paragraph::ownerCode() const
2386 return d->inset_owner_ ?
2387 d->inset_owner_->lyxCode() : NO_CODE;
2391 ParagraphParameters & Paragraph::params()
2397 ParagraphParameters const & Paragraph::params() const
2403 bool Paragraph::isFreeSpacing() const
2405 if (layout()->free_spacing)
2408 // for now we just need this, later should we need this in some
2409 // other way we can always add a function to Inset too.
2410 return ownerCode() == ERT_CODE || ownerCode() == LISTINGS_CODE;
2414 bool Paragraph::allowEmpty() const
2416 if (layout()->keepempty)
2418 return ownerCode() == ERT_CODE || ownerCode() == LISTINGS_CODE;
2422 char_type Paragraph::transformChar(char_type c, pos_type pos) const
2424 if (!Encodings::is_arabic(c))
2427 value_type prev_char = ' ';
2428 value_type next_char = ' ';
2430 for (pos_type i = pos - 1; i >= 0; --i) {
2431 value_type const par_char = getChar(i);
2432 if (!Encodings::isComposeChar_arabic(par_char)) {
2433 prev_char = par_char;
2438 for (pos_type i = pos + 1, end = size(); i < end; ++i) {
2439 value_type const par_char = getChar(i);
2440 if (!Encodings::isComposeChar_arabic(par_char)) {
2441 next_char = par_char;
2446 if (Encodings::is_arabic(next_char)) {
2447 if (Encodings::is_arabic(prev_char) &&
2448 !Encodings::is_arabic_special(prev_char))
2449 return Encodings::transformChar(c, Encodings::FORM_MEDIAL);
2451 return Encodings::transformChar(c, Encodings::FORM_INITIAL);
2453 if (Encodings::is_arabic(prev_char) &&
2454 !Encodings::is_arabic_special(prev_char))
2455 return Encodings::transformChar(c, Encodings::FORM_FINAL);
2457 return Encodings::transformChar(c, Encodings::FORM_ISOLATED);
2462 int Paragraph::checkBiblio(bool track_changes)
2465 //This is getting more and more a mess. ...We really should clean
2466 //up this bibitem issue for 1.6. See also bug 2743.
2468 // Add bibitem insets if necessary
2469 if (layout()->labeltype != LABEL_BIBLIO)
2472 bool hasbibitem = !d->insetlist_.empty()
2473 // Insist on it being in pos 0
2474 && getChar(0) == Paragraph::META_INSET
2475 && d->insetlist_.begin()->inset->lyxCode() == BIBITEM_CODE;
2480 // remove a bibitem in pos != 0
2481 // restore it later in pos 0 if necessary
2482 // (e.g. if a user inserts contents _before_ the item)
2483 // we're assuming there's only one of these, which there
2485 int erasedInsetPosition = -1;
2486 InsetList::iterator it = d->insetlist_.begin();
2487 InsetList::iterator end = d->insetlist_.end();
2488 for (; it != end; ++it)
2489 if (it->inset->lyxCode() == BIBITEM_CODE
2491 InsetBibitem * olditem = static_cast<InsetBibitem *>(it->inset);
2492 oldkey = olditem->getParam("key");
2493 oldlabel = olditem->getParam("label");
2494 erasedInsetPosition = it->pos;
2495 eraseChar(erasedInsetPosition, track_changes);
2499 //There was an InsetBibitem at the beginning, and we didn't
2500 //have to erase one.
2501 if (hasbibitem && erasedInsetPosition < 0)
2504 //There was an InsetBibitem at the beginning and we did have to
2505 //erase one. So we give its properties to the beginning inset.
2507 InsetBibitem * inset =
2508 static_cast<InsetBibitem *>(d->insetlist_.begin()->inset);
2509 if (!oldkey.empty())
2510 inset->setParam("key", oldkey);
2511 inset->setParam("label", oldlabel);
2512 return -erasedInsetPosition;
2515 //There was no inset at the beginning, so we need to create one with
2516 //the key and label of the one we erased.
2517 InsetBibitem * inset(new InsetBibitem(InsetCommandParams("bibitem")));
2518 // restore values of previously deleted item in this par.
2519 if (!oldkey.empty())
2520 inset->setParam("key", oldkey);
2521 inset->setParam("label", oldlabel);
2522 insertInset(0, static_cast<Inset *>(inset),
2523 Change(track_changes ? Change::INSERTED : Change::UNCHANGED));
2529 void Paragraph::checkAuthors(AuthorList const & authorList)
2531 d->changes_.checkAuthors(authorList);
2535 bool Paragraph::isUnchanged(pos_type pos) const
2537 return lookupChange(pos).type == Change::UNCHANGED;
2541 bool Paragraph::isInserted(pos_type pos) const
2543 return lookupChange(pos).type == Change::INSERTED;
2547 bool Paragraph::isDeleted(pos_type pos) const
2549 return lookupChange(pos).type == Change::DELETED;
2553 InsetList const & Paragraph::insetList() const
2555 return d->insetlist_;
2559 Inset * Paragraph::releaseInset(pos_type pos)
2561 Inset * inset = d->insetlist_.release(pos);
2562 /// does not honour change tracking!
2563 eraseChar(pos, false);
2568 Inset * Paragraph::getInset(pos_type pos)
2570 return d->insetlist_.get(pos);
2574 Inset const * Paragraph::getInset(pos_type pos) const
2576 return d->insetlist_.get(pos);