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 return d->fontlist_.highestInRange(startpos, endpos, def_size);
1345 Paragraph::value_type
1346 Paragraph::getUChar(BufferParams const & bparams, pos_type pos) const
1348 value_type c = getChar(pos);
1349 if (!lyxrc.rtl_support)
1379 if (uc != c && getFontSettings(bparams, pos).isRightToLeft())
1386 void Paragraph::setFont(pos_type pos, Font const & font)
1388 BOOST_ASSERT(pos <= size());
1390 // First, reduce font against layout/label font
1391 // Update: The setCharFont() routine in text2.cpp already
1392 // reduces font, so we don't need to do that here. (Asger)
1394 d->fontlist_.set(pos, font);
1398 void Paragraph::makeSameLayout(Paragraph const & par)
1400 layout(par.layout());
1402 d->params_ = par.params();
1406 bool Paragraph::stripLeadingSpaces(bool trackChanges)
1408 if (isFreeSpacing())
1414 while (pos < size() && (isNewline(pos) || isLineSeparator(pos))) {
1415 if (eraseChar(pos, trackChanges))
1421 return count > 0 || pos > 0;
1425 bool Paragraph::hasSameLayout(Paragraph const & par) const
1427 return par.layout() == layout() && d->params_.sameLayout(par.params());
1431 depth_type Paragraph::getDepth() const
1433 return params().depth();
1437 depth_type Paragraph::getMaxDepthAfter() const
1439 if (layout()->isEnvironment())
1440 return params().depth() + 1;
1442 return params().depth();
1446 char Paragraph::getAlign() const
1448 if (params().align() == LYX_ALIGN_LAYOUT)
1449 return layout()->align;
1451 return params().align();
1455 docstring const & Paragraph::getLabelstring() const
1457 return params().labelString();
1461 // the next two functions are for the manual labels
1462 docstring const Paragraph::getLabelWidthString() const
1464 if (layout()->margintype == MARGIN_MANUAL)
1465 return params().labelWidthString();
1467 return _("Senseless with this layout!");
1471 void Paragraph::setLabelWidthString(docstring const & s)
1473 params().labelWidthString(s);
1477 docstring const Paragraph::translateIfPossible(docstring const & s,
1478 BufferParams const & bparams) const
1480 if (!support::isAscii(s) || s.empty()) {
1481 // This must be a user defined layout. We cannot translate
1482 // this, since gettext accepts only ascii keys.
1485 // Probably standard layout, try to translate
1486 Messages & m = getMessages(getParLanguage(bparams)->code());
1487 return m.get(to_ascii(s));
1491 docstring Paragraph::expandLabel(LayoutPtr const & layout,
1492 BufferParams const & bparams, bool process_appendix) const
1494 TextClass const & tclass = bparams.getTextClass();
1497 if (process_appendix && params().appendix())
1498 fmt = translateIfPossible(layout->labelstring_appendix(),
1501 fmt = translateIfPossible(layout->labelstring(), bparams);
1503 if (fmt.empty() && layout->labeltype == LABEL_COUNTER
1504 && !layout->counter.empty())
1505 fmt = "\\the" + layout->counter;
1507 // handle 'inherited level parts' in 'fmt',
1508 // i.e. the stuff between '@' in '@Section@.\arabic{subsection}'
1509 size_t const i = fmt.find('@', 0);
1510 if (i != docstring::npos) {
1511 size_t const j = fmt.find('@', i + 1);
1512 if (j != docstring::npos) {
1513 docstring parent(fmt, i + 1, j - i - 1);
1514 docstring label = from_ascii("??");
1515 if (tclass.hasLayout(parent))
1516 docstring label = expandLabel(tclass[parent], bparams,
1518 fmt = docstring(fmt, 0, i) + label
1519 + docstring(fmt, j + 1, docstring::npos);
1523 return tclass.counters().counterLabel(fmt);
1527 void Paragraph::applyLayout(LayoutPtr const & new_layout)
1530 LyXAlignment const oldAlign = params().align();
1532 if (!(oldAlign & layout()->alignpossible)) {
1533 frontend::Alert::warning(_("Alignment not permitted"),
1534 _("The new layout does not permit the alignment previously used.\nSetting to default."));
1535 params().align(LYX_ALIGN_LAYOUT);
1540 pos_type Paragraph::beginOfBody() const
1542 return begin_of_body_;
1546 void Paragraph::setBeginOfBody()
1548 if (layout()->labeltype != LABEL_MANUAL) {
1553 // Unroll the first two cycles of the loop
1554 // and remember the previous character to
1555 // remove unnecessary getChar() calls
1557 pos_type end = size();
1558 if (i < end && !isNewline(i)) {
1560 char_type previous_char = 0;
1563 previous_char = text_[i];
1564 if (!isNewline(i)) {
1566 while (i < end && previous_char != ' ') {
1571 previous_char = temp;
1581 // returns -1 if inset not found
1582 int Paragraph::getPositionOfInset(Inset const * inset) const
1585 InsetList::const_iterator it = d->insetlist_.begin();
1586 InsetList::const_iterator end = d->insetlist_.end();
1587 for (; it != end; ++it)
1588 if (it->inset == inset)
1594 InsetBibitem * Paragraph::bibitem() const
1596 if (!d->insetlist_.empty()) {
1597 Inset * inset = d->insetlist_.begin()->inset;
1598 if (inset->lyxCode() == BIBITEM_CODE)
1599 return static_cast<InsetBibitem *>(inset);
1605 bool Paragraph::forceDefaultParagraphs() const
1607 return inInset() && inInset()->forceDefaultParagraphs(0);
1613 // paragraphs inside floats need different alignment tags to avoid
1616 bool noTrivlistCentering(InsetCode code)
1618 return code == FLOAT_CODE || code == WRAP_CODE;
1622 string correction(string const & orig)
1624 if (orig == "flushleft")
1625 return "raggedright";
1626 if (orig == "flushright")
1627 return "raggedleft";
1628 if (orig == "center")
1634 string const corrected_env(string const & suffix, string const & env,
1637 string output = suffix + "{";
1638 if (noTrivlistCentering(code))
1639 output += correction(env);
1643 if (suffix == "\\begin")
1649 void adjust_row_column(string const & str, TexRow & texrow, int & column)
1651 if (!contains(str, "\n"))
1652 column += str.size();
1656 column = rsplit(str, tmp, '\n').size();
1663 // This could go to ParagraphParameters if we want to
1664 int Paragraph::startTeXParParams(BufferParams const & bparams,
1665 odocstream & os, TexRow & texrow,
1666 bool moving_arg) const
1670 if (params().noindent()) {
1671 os << "\\noindent ";
1675 LyXAlignment const curAlign = params().align();
1677 if (curAlign == layout()->align)
1681 case LYX_ALIGN_NONE:
1682 case LYX_ALIGN_BLOCK:
1683 case LYX_ALIGN_LAYOUT:
1684 case LYX_ALIGN_SPECIAL:
1686 case LYX_ALIGN_LEFT:
1687 case LYX_ALIGN_RIGHT:
1688 case LYX_ALIGN_CENTER:
1697 case LYX_ALIGN_NONE:
1698 case LYX_ALIGN_BLOCK:
1699 case LYX_ALIGN_LAYOUT:
1700 case LYX_ALIGN_SPECIAL:
1702 case LYX_ALIGN_LEFT: {
1704 if (getParLanguage(bparams)->babel() != "hebrew")
1705 output = corrected_env("\\begin", "flushleft", ownerCode());
1707 output = corrected_env("\\begin", "flushright", ownerCode());
1708 os << from_ascii(output);
1709 adjust_row_column(output, texrow, column);
1711 } case LYX_ALIGN_RIGHT: {
1713 if (getParLanguage(bparams)->babel() != "hebrew")
1714 output = corrected_env("\\begin", "flushright", ownerCode());
1716 output = corrected_env("\\begin", "flushleft", ownerCode());
1717 os << from_ascii(output);
1718 adjust_row_column(output, texrow, column);
1720 } case LYX_ALIGN_CENTER: {
1722 output = corrected_env("\\begin", "center", ownerCode());
1723 os << from_ascii(output);
1724 adjust_row_column(output, texrow, column);
1733 // This could go to ParagraphParameters if we want to
1734 int Paragraph::endTeXParParams(BufferParams const & bparams,
1735 odocstream & os, TexRow & texrow,
1736 bool moving_arg) const
1740 switch (params().align()) {
1741 case LYX_ALIGN_NONE:
1742 case LYX_ALIGN_BLOCK:
1743 case LYX_ALIGN_LAYOUT:
1744 case LYX_ALIGN_SPECIAL:
1746 case LYX_ALIGN_LEFT:
1747 case LYX_ALIGN_RIGHT:
1748 case LYX_ALIGN_CENTER:
1756 switch (params().align()) {
1757 case LYX_ALIGN_NONE:
1758 case LYX_ALIGN_BLOCK:
1759 case LYX_ALIGN_LAYOUT:
1760 case LYX_ALIGN_SPECIAL:
1762 case LYX_ALIGN_LEFT: {
1764 if (getParLanguage(bparams)->babel() != "hebrew")
1765 output = corrected_env("\n\\par\\end", "flushleft", ownerCode());
1767 output = corrected_env("\n\\par\\end", "flushright", ownerCode());
1768 os << from_ascii(output);
1769 adjust_row_column(output, texrow, column);
1771 } case LYX_ALIGN_RIGHT: {
1773 if (getParLanguage(bparams)->babel() != "hebrew")
1774 output = corrected_env("\n\\par\\end", "flushright", ownerCode());
1776 output = corrected_env("\n\\par\\end", "flushleft", ownerCode());
1777 os << from_ascii(output);
1778 adjust_row_column(output, texrow, column);
1780 } case LYX_ALIGN_CENTER: {
1782 output = corrected_env("\n\\par\\end", "center", ownerCode());
1783 os << from_ascii(output);
1784 adjust_row_column(output, texrow, column);
1793 // This one spits out the text of the paragraph
1794 bool Paragraph::simpleTeXOnePar(Buffer const & buf,
1795 BufferParams const & bparams,
1796 Font const & outerfont,
1797 odocstream & os, TexRow & texrow,
1798 OutputParams const & runparams) const
1800 LYXERR(Debug::LATEX) << "SimpleTeXOnePar... " << this << endl;
1802 bool return_value = false;
1806 // well we have to check if we are in an inset with unlimited
1807 // length (all in one row) if that is true then we don't allow
1808 // any special options in the paragraph and also we don't allow
1809 // any environment other than the default layout of the text class
1811 bool asdefault = forceDefaultParagraphs();
1814 style = bparams.getTextClass().defaultLayout();
1819 // Current base font for all inherited font changes, without any
1820 // change caused by an individual character, except for the language:
1821 // It is set to the language of the first character.
1822 // As long as we are in the label, this font is the base font of the
1823 // label. Before the first body character it is set to the base font
1827 // Maybe we have to create a optional argument.
1828 pos_type body_pos = beginOfBody();
1829 unsigned int column = 0;
1832 // the optional argument is kept in curly brackets in
1833 // case it contains a ']'
1836 basefont = getLabelFont(bparams, outerfont);
1838 basefont = getLayoutFont(bparams, outerfont);
1841 // Which font is currently active?
1842 Font running_font(basefont);
1843 // Do we have an open font change?
1844 bool open_font = false;
1846 Change runningChange = Change(Change::UNCHANGED);
1848 texrow.start(id(), 0);
1850 // if the paragraph is empty, the loop will not be entered at all
1852 if (style->isCommand()) {
1857 column += startTeXParParams(bparams, os, texrow,
1858 runparams.moving_arg);
1861 for (pos_type i = 0; i < size(); ++i) {
1862 // First char in paragraph or after label?
1863 if (i == body_pos) {
1866 column += running_font.latexWriteEndChanges(
1867 os, bparams, runparams,
1868 basefont, basefont);
1871 basefont = getLayoutFont(bparams, outerfont);
1872 running_font = basefont;
1874 column += Changes::latexMarkChange(os, bparams,
1875 runningChange, Change(Change::UNCHANGED));
1876 runningChange = Change(Change::UNCHANGED);
1881 if (style->isCommand()) {
1887 column += startTeXParParams(bparams, os,
1889 runparams.moving_arg);
1892 Change const & change = runparams.inDeletedInset ? runparams.changeOfDeletedInset
1895 if (bparams.outputChanges && runningChange != change) {
1897 column += running_font.latexWriteEndChanges(
1898 os, bparams, runparams, basefont, basefont);
1901 basefont = getLayoutFont(bparams, outerfont);
1902 running_font = basefont;
1904 column += Changes::latexMarkChange(os, bparams, runningChange, change);
1905 runningChange = change;
1908 // do not output text which is marked deleted
1909 // if change tracking output is disabled
1910 if (!bparams.outputChanges && change.type == Change::DELETED) {
1916 value_type const c = getChar(i);
1918 // Fully instantiated font
1919 Font const font = getFont(bparams, i, outerfont);
1921 Font const last_font = running_font;
1923 // Do we need to close the previous font?
1925 (font != running_font ||
1926 font.language() != running_font.language()))
1928 column += running_font.latexWriteEndChanges(
1929 os, bparams, runparams, basefont,
1930 (i == body_pos-1) ? basefont : font);
1931 running_font = basefont;
1935 // Switch file encoding if necessary
1936 if (runparams.encoding->package() == Encoding::inputenc &&
1937 font.language()->encoding()->package() == Encoding::inputenc) {
1938 std::pair<bool, int> const enc_switch = switchEncoding(os, bparams,
1939 runparams.moving_arg, *(runparams.encoding),
1940 *(font.language()->encoding()));
1941 if (enc_switch.first) {
1942 column += enc_switch.second;
1943 runparams.encoding = font.language()->encoding();
1947 // Do we need to change font?
1948 if ((font != running_font ||
1949 font.language() != running_font.language()) &&
1952 odocstringstream ods;
1953 column += font.latexWriteStartChanges(ods, bparams,
1954 runparams, basefont,
1956 running_font = font;
1958 docstring fontchange = ods.str();
1959 // check if the fontchange ends with a trailing blank
1960 // (like "\small " (see bug 3382)
1961 if (suffixIs(fontchange, ' ') && c == ' ')
1962 os << fontchange.substr(0, fontchange.size() - 1)
1963 << from_ascii("{}");
1969 // Do not print the separation of the optional argument
1970 // if style->pass_thru is false. This works because
1971 // simpleTeXSpecialChars ignores spaces if
1972 // style->pass_thru is false.
1973 if (i != body_pos - 1) {
1974 if (d->simpleTeXBlanks(
1975 *(runparams.encoding), os, texrow,
1976 i, column, font, *style))
1977 // A surrogate pair was output. We
1978 // must not call simpleTeXSpecialChars
1979 // in this iteration, since
1980 // simpleTeXBlanks incremented i, and
1981 // simpleTeXSpecialChars would output
1982 // the combining character again.
1987 OutputParams rp = runparams;
1988 rp.free_spacing = style->free_spacing;
1989 rp.local_font = &font;
1990 rp.intitle = style->intitle;
1991 d->simpleTeXSpecialChars(buf, bparams, os,
1992 texrow, rp, running_font,
1993 basefont, outerfont, open_font,
1994 runningChange, *style, i, column, c);
1996 // Set the encoding to that returned from simpleTeXSpecialChars (see
1997 // comment for encoding member in OutputParams.h)
1998 runparams.encoding = rp.encoding;
2001 // If we have an open font definition, we have to close it
2003 #ifdef FIXED_LANGUAGE_END_DETECTION
2006 .latexWriteEndChanges(os, bparams, runparams,
2008 next_->getFont(bparams, 0, outerfont));
2010 running_font.latexWriteEndChanges(os, bparams,
2011 runparams, basefont, basefont);
2014 //FIXME: For now we ALWAYS have to close the foreign font settings if they are
2015 //FIXME: there as we start another \selectlanguage with the next paragraph if
2016 //FIXME: we are in need of this. This should be fixed sometime (Jug)
2017 running_font.latexWriteEndChanges(os, bparams, runparams,
2018 basefont, basefont);
2022 column += Changes::latexMarkChange(os, bparams, runningChange, Change(Change::UNCHANGED));
2024 // Needed if there is an optional argument but no contents.
2025 if (body_pos > 0 && body_pos == size()) {
2027 return_value = false;
2031 column += endTeXParParams(bparams, os, texrow,
2032 runparams.moving_arg);
2035 LYXERR(Debug::LATEX) << "SimpleTeXOnePar...done " << this << endl;
2036 return return_value;
2053 string tag_name(PAR_TAG const & pt) {
2055 case PAR_NONE: return "!-- --";
2056 case TT: return "tt";
2057 case SF: return "sf";
2058 case BF: return "bf";
2059 case IT: return "it";
2060 case SL: return "sl";
2061 case EM: return "em";
2068 void operator|=(PAR_TAG & p1, PAR_TAG const & p2)
2070 p1 = static_cast<PAR_TAG>(p1 | p2);
2075 void reset(PAR_TAG & p1, PAR_TAG const & p2)
2077 p1 = static_cast<PAR_TAG>(p1 & ~p2);
2083 bool Paragraph::emptyTag() const
2085 for (pos_type i = 0; i < size(); ++i) {
2087 Inset const * inset = getInset(i);
2088 InsetCode lyx_code = inset->lyxCode();
2089 if (lyx_code != TOC_CODE &&
2090 lyx_code != INCLUDE_CODE &&
2091 lyx_code != GRAPHICS_CODE &&
2092 lyx_code != ERT_CODE &&
2093 lyx_code != LISTINGS_CODE &&
2094 lyx_code != FLOAT_CODE &&
2095 lyx_code != TABULAR_CODE) {
2099 value_type c = getChar(i);
2100 if (c != ' ' && c != '\t')
2108 string Paragraph::getID(Buffer const & buf, OutputParams const & runparams) const
2110 for (pos_type i = 0; i < size(); ++i) {
2112 Inset const * inset = getInset(i);
2113 InsetCode lyx_code = inset->lyxCode();
2114 if (lyx_code == LABEL_CODE) {
2115 string const id = static_cast<InsetCommand const *>(inset)->getContents();
2116 return "id='" + to_utf8(sgml::cleanID(buf, runparams, from_utf8(id))) + "'";
2125 pos_type Paragraph::getFirstWord(Buffer const & buf, odocstream & os, OutputParams const & runparams) const
2128 for (i = 0; i < size(); ++i) {
2130 Inset const * inset = getInset(i);
2131 inset->docbook(buf, os, runparams);
2133 value_type c = getChar(i);
2136 os << sgml::escapeChar(c);
2143 bool Paragraph::onlyText(Buffer const & buf, Font const & outerfont, pos_type initial) const
2147 for (pos_type i = initial; i < size(); ++i) {
2148 Font font = getFont(buf.params(), i, outerfont);
2151 if (i != initial && font != font_old)
2160 void Paragraph::simpleDocBookOnePar(Buffer const & buf,
2162 OutputParams const & runparams,
2163 Font const & outerfont,
2164 pos_type initial) const
2166 bool emph_flag = false;
2168 LayoutPtr const & style = layout();
2170 style->labeltype == LABEL_MANUAL ? style->labelfont : style->font;
2172 if (style->pass_thru && !onlyText(buf, outerfont, initial))
2175 // parsing main loop
2176 for (pos_type i = initial; i < size(); ++i) {
2177 Font font = getFont(buf.params(), i, outerfont);
2179 // handle <emphasis> tag
2180 if (font_old.emph() != font.emph()) {
2181 if (font.emph() == Font::ON) {
2184 } else if (i != initial) {
2185 os << "</emphasis>";
2191 Inset const * inset = getInset(i);
2192 inset->docbook(buf, os, runparams);
2194 value_type c = getChar(i);
2196 if (style->pass_thru)
2199 os << sgml::escapeChar(c);
2205 os << "</emphasis>";
2208 if (style->free_spacing)
2210 if (style->pass_thru && !onlyText(buf, outerfont, initial))
2215 bool Paragraph::isHfill(pos_type pos) const
2218 && getInset(pos)->lyxCode() == HFILL_CODE;
2222 bool Paragraph::isNewline(pos_type pos) const
2225 && getInset(pos)->lyxCode() == NEWLINE_CODE;
2229 bool Paragraph::isLineSeparator(pos_type pos) const
2231 value_type const c = getChar(pos);
2232 return isLineSeparatorChar(c)
2233 || (c == Paragraph::META_INSET && getInset(pos) &&
2234 getInset(pos)->isLineSeparator());
2238 /// Used by the spellchecker
2239 bool Paragraph::isLetter(pos_type pos) const
2242 return getInset(pos)->isLetter();
2244 value_type const c = getChar(pos);
2245 return isLetterChar(c) || isDigit(c);
2251 Paragraph::getParLanguage(BufferParams const & bparams) const
2254 return getFirstFontSettings(bparams).language();
2255 // FIXME: we should check the prev par as well (Lgb)
2256 return bparams.language;
2260 bool Paragraph::isRTL(BufferParams const & bparams) const
2262 return lyxrc.rtl_support
2263 && getParLanguage(bparams)->rightToLeft()
2264 && ownerCode() != ERT_CODE
2265 && ownerCode() != LISTINGS_CODE;
2269 void Paragraph::changeLanguage(BufferParams const & bparams,
2270 Language const * from, Language const * to)
2272 // change language including dummy font change at the end
2273 for (pos_type i = 0; i <= size(); ++i) {
2274 Font font = getFontSettings(bparams, i);
2275 if (font.language() == from) {
2276 font.setLanguage(to);
2283 bool Paragraph::isMultiLingual(BufferParams const & bparams) const
2285 Language const * doc_language = bparams.language;
2286 FontList::const_iterator cit = d->fontlist_.begin();
2287 FontList::const_iterator end = d->fontlist_.end();
2289 for (; cit != end; ++cit)
2290 if (cit->font().language() != ignore_language &&
2291 cit->font().language() != latex_language &&
2292 cit->font().language() != doc_language)
2298 // Convert the paragraph to a string.
2299 // Used for building the table of contents
2300 docstring const Paragraph::asString(Buffer const & buffer, bool label) const
2302 return asString(buffer, 0, size(), label);
2306 docstring const Paragraph::asString(Buffer const & buffer,
2307 pos_type beg, pos_type end, bool label) const
2310 odocstringstream os;
2312 if (beg == 0 && label && !params().labelString().empty())
2313 os << params().labelString() << ' ';
2315 for (pos_type i = beg; i < end; ++i) {
2316 value_type const c = getChar(i);
2319 else if (c == META_INSET)
2320 getInset(i)->textString(buffer, os);
2327 void Paragraph::setInsetOwner(Inset * inset)
2329 d->inset_owner_ = inset;
2333 int Paragraph::id() const
2339 LayoutPtr const & Paragraph::layout() const
2345 void Paragraph::layout(LayoutPtr const & new_layout)
2347 layout_ = new_layout;
2351 Inset * Paragraph::inInset() const
2353 return d->inset_owner_;
2357 InsetCode Paragraph::ownerCode() const
2359 return d->inset_owner_ ?
2360 d->inset_owner_->lyxCode() : NO_CODE;
2364 ParagraphParameters & Paragraph::params()
2370 ParagraphParameters const & Paragraph::params() const
2376 bool Paragraph::isFreeSpacing() const
2378 if (layout()->free_spacing)
2381 // for now we just need this, later should we need this in some
2382 // other way we can always add a function to Inset too.
2383 return ownerCode() == ERT_CODE || ownerCode() == LISTINGS_CODE;
2387 bool Paragraph::allowEmpty() const
2389 if (layout()->keepempty)
2391 return ownerCode() == ERT_CODE || ownerCode() == LISTINGS_CODE;
2395 char_type Paragraph::transformChar(char_type c, pos_type pos) const
2397 if (!Encodings::is_arabic(c))
2400 value_type prev_char = ' ';
2401 value_type next_char = ' ';
2403 for (pos_type i = pos - 1; i >= 0; --i) {
2404 value_type const par_char = getChar(i);
2405 if (!Encodings::isComposeChar_arabic(par_char)) {
2406 prev_char = par_char;
2411 for (pos_type i = pos + 1, end = size(); i < end; ++i) {
2412 value_type const par_char = getChar(i);
2413 if (!Encodings::isComposeChar_arabic(par_char)) {
2414 next_char = par_char;
2419 if (Encodings::is_arabic(next_char)) {
2420 if (Encodings::is_arabic(prev_char) &&
2421 !Encodings::is_arabic_special(prev_char))
2422 return Encodings::transformChar(c, Encodings::FORM_MEDIAL);
2424 return Encodings::transformChar(c, Encodings::FORM_INITIAL);
2426 if (Encodings::is_arabic(prev_char) &&
2427 !Encodings::is_arabic_special(prev_char))
2428 return Encodings::transformChar(c, Encodings::FORM_FINAL);
2430 return Encodings::transformChar(c, Encodings::FORM_ISOLATED);
2435 int Paragraph::checkBiblio(bool track_changes)
2438 //This is getting more and more a mess. ...We really should clean
2439 //up this bibitem issue for 1.6. See also bug 2743.
2441 // Add bibitem insets if necessary
2442 if (layout()->labeltype != LABEL_BIBLIO)
2445 bool hasbibitem = !d->insetlist_.empty()
2446 // Insist on it being in pos 0
2447 && getChar(0) == Paragraph::META_INSET
2448 && d->insetlist_.begin()->inset->lyxCode() == BIBITEM_CODE;
2453 // remove a bibitem in pos != 0
2454 // restore it later in pos 0 if necessary
2455 // (e.g. if a user inserts contents _before_ the item)
2456 // we're assuming there's only one of these, which there
2458 int erasedInsetPosition = -1;
2459 InsetList::iterator it = d->insetlist_.begin();
2460 InsetList::iterator end = d->insetlist_.end();
2461 for (; it != end; ++it)
2462 if (it->inset->lyxCode() == BIBITEM_CODE
2464 InsetBibitem * olditem = static_cast<InsetBibitem *>(it->inset);
2465 oldkey = olditem->getParam("key");
2466 oldlabel = olditem->getParam("label");
2467 erasedInsetPosition = it->pos;
2468 eraseChar(erasedInsetPosition, track_changes);
2472 //There was an InsetBibitem at the beginning, and we didn't
2473 //have to erase one.
2474 if (hasbibitem && erasedInsetPosition < 0)
2477 //There was an InsetBibitem at the beginning and we did have to
2478 //erase one. So we give its properties to the beginning inset.
2480 InsetBibitem * inset =
2481 static_cast<InsetBibitem *>(d->insetlist_.begin()->inset);
2482 if (!oldkey.empty())
2483 inset->setParam("key", oldkey);
2484 inset->setParam("label", oldlabel);
2485 return -erasedInsetPosition;
2488 //There was no inset at the beginning, so we need to create one with
2489 //the key and label of the one we erased.
2490 InsetBibitem * inset(new InsetBibitem(InsetCommandParams("bibitem")));
2491 // restore values of previously deleted item in this par.
2492 if (!oldkey.empty())
2493 inset->setParam("key", oldkey);
2494 inset->setParam("label", oldlabel);
2495 insertInset(0, static_cast<Inset *>(inset),
2496 Change(track_changes ? Change::INSERTED : Change::UNCHANGED));
2502 void Paragraph::checkAuthors(AuthorList const & authorList)
2504 d->changes_.checkAuthors(authorList);
2508 bool Paragraph::isUnchanged(pos_type pos) const
2510 return lookupChange(pos).type == Change::UNCHANGED;
2514 bool Paragraph::isInserted(pos_type pos) const
2516 return lookupChange(pos).type == Change::INSERTED;
2520 bool Paragraph::isDeleted(pos_type pos) const
2522 return lookupChange(pos).type == Change::DELETED;
2526 InsetList const & Paragraph::insetList() const
2528 return d->insetlist_;
2532 Inset * Paragraph::releaseInset(pos_type pos)
2534 Inset * inset = d->insetlist_.release(pos);
2535 /// does not honour change tracking!
2536 eraseChar(pos, false);
2541 Inset * Paragraph::getInset(pos_type pos)
2543 return d->insetlist_.get(pos);
2547 Inset const * Paragraph::getInset(pos_type pos) const
2549 return d->insetlist_.get(pos);