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"
22 #include "LayoutFile.h"
24 #include "BufferParams.h"
28 #include "InsetList.h"
30 #include "LaTeXFeatures.h"
36 #include "OutputParams.h"
37 #include "output_latex.h"
38 #include "paragraph_funcs.h"
39 #include "ParagraphParameters.h"
41 #include "TextClass.h"
47 #include "frontends/alert.h"
49 #include "insets/InsetBibitem.h"
50 #include "insets/InsetLabel.h"
52 #include "support/lassert.h"
53 #include "support/convert.h"
54 #include "support/debug.h"
55 #include "support/gettext.h"
56 #include "support/lstrings.h"
57 #include "support/Messages.h"
58 #include "support/textutils.h"
64 using namespace lyx::support;
69 /// Inset identifier (above 0x10ffff, for ucs-4)
70 char_type const META_INSET = 0x200001;
73 /////////////////////////////////////////////////////////////////////
77 /////////////////////////////////////////////////////////////////////
79 class Paragraph::Private
83 Private(Paragraph * owner, Layout const & layout);
84 /// "Copy constructor"
85 Private(Private const &, Paragraph * owner);
86 /// Copy constructor from \p beg to \p end
87 Private(Private const &, Paragraph * owner, pos_type beg, pos_type end);
90 void insertChar(pos_type pos, char_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, char_type c, char_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(OutputParams const &,
101 odocstream &, TexRow & texrow,
103 unsigned int & column,
105 Layout const & style);
107 /// Output consecutive unicode chars, belonging to the same script as
108 /// specified by the latex macro \p ltx, to \p os starting from \p i.
109 /// \return the number of characters written.
110 int writeScriptChars(odocstream & os, docstring const & ltx,
111 Change &, Encoding const &, pos_type & i);
113 /// This could go to ParagraphParameters if we want to.
114 int startTeXParParams(BufferParams const &, odocstream &, TexRow &,
117 /// This could go to ParagraphParameters if we want to.
118 int endTeXParParams(BufferParams const &, odocstream &, TexRow &,
122 void latexInset(BufferParams const &,
124 TexRow & texrow, OutputParams &,
127 Font const & outerfont,
129 Change & running_change,
130 Layout const & style,
132 unsigned int & column);
135 void latexSpecialChar(
137 OutputParams & runparams,
139 Change & running_change,
140 Layout const & style,
142 unsigned int & column);
149 unsigned int & column);
151 bool latexSpecialTypewriter(
155 unsigned int & column);
157 bool latexSpecialPhrase(
160 unsigned int & column,
161 OutputParams & runparams);
164 void validate(LaTeXFeatures & features,
165 Layout const & layout) const;
167 /// Checks if the paragraph contains only text and no inset or font change.
168 bool onlyText(Buffer const & buf, Font const & outerfont,
169 pos_type initial) const;
171 /// match a string against a particular point in the paragraph
172 bool isTextAt(string const & str, pos_type pos) const;
174 /// Which Paragraph owns us?
178 Inset * inset_owner_;
186 static unsigned int paragraph_id;
188 ParagraphParameters params_;
190 /// for recording and looking up changes
194 InsetList insetlist_;
197 pos_type begin_of_body_;
199 typedef docstring TextContainer;
203 typedef std::set<docstring> Words;
207 Layout const * layout_;
211 // Initialization of the counter for the paragraph id's,
212 unsigned int Paragraph::Private::paragraph_id = 0;
216 struct special_phrase {
222 special_phrase const special_phrases[] = {
223 { "LyX", from_ascii("\\LyX{}"), false },
224 { "TeX", from_ascii("\\TeX{}"), true },
225 { "LaTeX2e", from_ascii("\\LaTeXe{}"), true },
226 { "LaTeX", from_ascii("\\LaTeX{}"), true },
229 size_t const phrases_nr = sizeof(special_phrases)/sizeof(special_phrase);
234 Paragraph::Private::Private(Paragraph * owner, Layout const & layout)
235 : owner_(owner), inset_owner_(0), begin_of_body_(0), layout_(&layout)
237 id_ = paragraph_id++;
242 Paragraph::Private::Private(Private const & p, Paragraph * owner)
243 : owner_(owner), inset_owner_(p.inset_owner_), fontlist_(p.fontlist_),
244 params_(p.params_), changes_(p.changes_), insetlist_(p.insetlist_),
245 begin_of_body_(p.begin_of_body_), text_(p.text_), words_(p.words_),
248 id_ = paragraph_id++;
252 Paragraph::Private::Private(Private const & p, Paragraph * owner,
253 pos_type beg, pos_type end)
254 : owner_(owner), inset_owner_(p.inset_owner_), fontlist_(p.fontlist_),
255 params_(p.params_), changes_(p.changes_),
256 begin_of_body_(p.begin_of_body_), words_(p.words_),
259 id_ = paragraph_id++;
260 if (beg >= pos_type(p.text_.size()))
262 text_ = p.text_.substr(beg, end - beg);
263 InsetList::const_iterator icit = p.insetlist_.begin();
264 InsetList::const_iterator iend = p.insetlist_.end();
265 for (; icit != iend; ++icit) {
268 if (icit->pos >= end)
270 // Add a new entry in the insetlist_.
271 insetlist_.insert(icit->inset, icit->pos - beg);
273 FontList::const_iterator fcit = fontlist_.begin();
274 FontList::const_iterator fend = fontlist_.end();
275 for (; fcit != fend; ++fcit) {
276 if (fcit->pos() < beg)
278 if (fcit->pos() >= end) {
279 // Add last entry in the fontlist_.
280 fontlist_.set(text_.size() - 1, fcit->font());
283 // Add a new entry in the fontlist_.
284 fontlist_.set(fcit->pos() - beg, fcit->font());
289 bool Paragraph::isChanged(pos_type start, pos_type end) const
291 LASSERT(start >= 0 && start <= size(), /**/);
292 LASSERT(end > start && end <= size() + 1, /**/);
294 return d->changes_.isChanged(start, end);
298 bool Paragraph::isMergedOnEndOfParDeletion(bool trackChanges) const
300 // keep the logic here in sync with the logic of eraseChars()
304 Change const change = d->changes_.lookup(size());
305 return change.type == Change::INSERTED && change.author == 0;
309 void Paragraph::setChange(Change const & change)
311 // beware of the imaginary end-of-par character!
312 d->changes_.set(change, 0, size() + 1);
315 * Propagate the change recursively - but not in case of DELETED!
317 * Imagine that your co-author makes changes in an existing inset. He
318 * sends your document to you and you come to the conclusion that the
319 * inset should go completely. If you erase it, LyX must not delete all
320 * text within the inset. Otherwise, the change tracked insertions of
321 * your co-author get lost and there is no way to restore them later.
323 * Conclusion: An inset's content should remain untouched if you delete it
326 if (change.type != Change::DELETED) {
327 for (pos_type pos = 0; pos < size(); ++pos) {
328 if (Inset * inset = getInset(pos))
329 inset->setChange(change);
335 void Paragraph::setChange(pos_type pos, Change const & change)
337 LASSERT(pos >= 0 && pos <= size(), /**/);
338 d->changes_.set(change, pos);
340 // see comment in setChange(Change const &) above
341 if (change.type != Change::DELETED && pos < size())
342 if (Inset * inset = getInset(pos))
343 inset->setChange(change);
347 Change const & Paragraph::lookupChange(pos_type pos) const
349 LASSERT(pos >= 0 && pos <= size(), /**/);
350 return d->changes_.lookup(pos);
354 void Paragraph::acceptChanges(BufferParams const & bparams, pos_type start,
357 LASSERT(start >= 0 && start <= size(), /**/);
358 LASSERT(end > start && end <= size() + 1, /**/);
360 for (pos_type pos = start; pos < end; ++pos) {
361 switch (lookupChange(pos).type) {
362 case Change::UNCHANGED:
363 // accept changes in nested inset
364 if (Inset * inset = getInset(pos))
365 inset->acceptChanges(bparams);
368 case Change::INSERTED:
369 d->changes_.set(Change(Change::UNCHANGED), pos);
370 // also accept changes in nested inset
371 if (Inset * inset = getInset(pos))
372 inset->acceptChanges(bparams);
375 case Change::DELETED:
376 // Suppress access to non-existent
377 // "end-of-paragraph char"
379 eraseChar(pos, false);
390 void Paragraph::rejectChanges(BufferParams const & bparams,
391 pos_type start, pos_type end)
393 LASSERT(start >= 0 && start <= size(), /**/);
394 LASSERT(end > start && end <= size() + 1, /**/);
396 for (pos_type pos = start; pos < end; ++pos) {
397 switch (lookupChange(pos).type) {
398 case Change::UNCHANGED:
399 // reject changes in nested inset
400 if (Inset * inset = getInset(pos))
401 inset->rejectChanges(bparams);
404 case Change::INSERTED:
405 // Suppress access to non-existent
406 // "end-of-paragraph char"
408 eraseChar(pos, false);
414 case Change::DELETED:
415 d->changes_.set(Change(Change::UNCHANGED), pos);
417 // Do NOT reject changes within a deleted inset!
418 // There may be insertions of a co-author inside of it!
426 void Paragraph::Private::insertChar(pos_type pos, char_type c,
427 Change const & change)
429 LASSERT(pos >= 0 && pos <= int(text_.size()), /**/);
432 changes_.insert(change, pos);
434 // This is actually very common when parsing buffers (and
435 // maybe inserting ascii text)
436 if (pos == pos_type(text_.size())) {
437 // when appending characters, no need to update tables
442 text_.insert(text_.begin() + pos, c);
444 // Update the font table.
445 fontlist_.increasePosAfterPos(pos);
448 insetlist_.increasePosAfterPos(pos);
452 void Paragraph::insertInset(pos_type pos, Inset * inset,
453 Change const & change)
455 LASSERT(inset, /**/);
456 LASSERT(pos >= 0 && pos <= size(), /**/);
458 d->insertChar(pos, META_INSET, change);
459 LASSERT(d->text_[pos] == META_INSET, /**/);
461 // Add a new entry in the insetlist_.
462 d->insetlist_.insert(inset, pos);
466 bool Paragraph::eraseChar(pos_type pos, bool trackChanges)
468 LASSERT(pos >= 0 && pos <= size(), /**/);
470 // keep the logic here in sync with the logic of isMergedOnEndOfParDeletion()
473 Change change = d->changes_.lookup(pos);
475 // set the character to DELETED if
476 // a) it was previously unchanged or
477 // b) it was inserted by a co-author
479 if (change.type == Change::UNCHANGED ||
480 (change.type == Change::INSERTED && change.author != 0)) {
481 setChange(pos, Change(Change::DELETED));
485 if (change.type == Change::DELETED)
489 // Don't physically access the imaginary end-of-paragraph character.
490 // eraseChar() can only mark it as DELETED. A physical deletion of
491 // end-of-par must be handled externally.
497 d->changes_.erase(pos);
499 // if it is an inset, delete the inset entry
500 if (d->text_[pos] == META_INSET)
501 d->insetlist_.erase(pos);
503 d->text_.erase(d->text_.begin() + pos);
505 // Update the fontlist_
506 d->fontlist_.erase(pos);
508 // Update the insetlist_
509 d->insetlist_.decreasePosAfterPos(pos);
515 int Paragraph::eraseChars(pos_type start, pos_type end, bool trackChanges)
517 LASSERT(start >= 0 && start <= size(), /**/);
518 LASSERT(end >= start && end <= size() + 1, /**/);
521 for (pos_type count = end - start; count; --count) {
522 if (!eraseChar(i, trackChanges))
529 int Paragraph::Private::latexSurrogatePair(odocstream & os, char_type c,
530 char_type next, Encoding const & encoding)
532 // Writing next here may circumvent a possible font change between
533 // c and next. Since next is only output if it forms a surrogate pair
534 // with c we can ignore this:
535 // A font change inside a surrogate pair does not make sense and is
536 // hopefully impossible to input.
537 // FIXME: change tracking
538 // Is this correct WRT change tracking?
539 docstring const latex1 = encoding.latexChar(next);
540 docstring const latex2 = encoding.latexChar(c);
541 if (docstring(1, next) == latex1) {
542 // the encoding supports the combination
543 os << latex2 << latex1;
544 return latex1.length() + latex2.length();
546 os << latex1 << '{' << latex2 << '}';
547 return latex1.length() + latex2.length() + 2;
551 bool Paragraph::Private::simpleTeXBlanks(OutputParams const & runparams,
552 odocstream & os, TexRow & texrow,
554 unsigned int & column,
556 Layout const & style)
558 if (style.pass_thru || runparams.verbatim)
561 if (i + 1 < int(text_.size())) {
562 char_type next = text_[i + 1];
563 if (Encodings::isCombiningChar(next)) {
564 Encoding const & encoding = *(runparams.encoding);
565 // This space has an accent, so we must always output it.
566 column += latexSurrogatePair(os, ' ', next, encoding) - 1;
571 if (lyxrc.plaintext_linelen > 0
572 && column > lyxrc.plaintext_linelen
574 && text_[i - 1] != ' '
575 && (i + 1 < int(text_.size()))
576 // same in FreeSpacing mode
577 && !owner_->isFreeSpacing()
578 // In typewriter mode, we want to avoid
579 // ! . ? : at the end of a line
580 && !(font.fontInfo().family() == TYPEWRITER_FAMILY
581 && (text_[i - 1] == '.'
582 || text_[i - 1] == '?'
583 || text_[i - 1] == ':'
584 || text_[i - 1] == '!'))) {
587 texrow.start(owner_->id(), i + 1);
589 } else if (style.free_spacing) {
598 int Paragraph::Private::writeScriptChars(odocstream & os,
599 docstring const & ltx,
600 Change & runningChange,
601 Encoding const & encoding,
604 // FIXME: modifying i here is not very nice...
606 // We only arrive here when a proper language for character text_[i] has
607 // not been specified (i.e., it could not be translated in the current
608 // latex encoding) or its latex translation has been forced, and it
609 // belongs to a known script.
610 // Parameter ltx contains the latex translation of text_[i] as specified
611 // in the unicodesymbols file and is something like "\textXXX{<spec>}".
612 // The latex macro name "textXXX" specifies the script to which text_[i]
613 // belongs and we use it in order to check whether characters from the
614 // same script immediately follow, such that we can collect them in a
615 // single "\textXXX" macro. So, we have to retain "\textXXX{<spec>"
616 // for the first char but only "<spec>" for all subsequent chars.
617 docstring::size_type const brace1 = ltx.find_first_of(from_ascii("{"));
618 docstring::size_type const brace2 = ltx.find_last_of(from_ascii("}"));
619 string script = to_ascii(ltx.substr(1, brace1 - 1));
622 bool closing_brace = true;
623 if (script == "textgreek" && encoding.latexName() == "iso-8859-7") {
624 // Correct encoding is being used, so we can avoid \textgreek.
627 closing_brace = false;
629 os << ltx.substr(pos, length);
630 int size = text_.size();
631 while (i + 1 < size) {
632 char_type const next = text_[i + 1];
633 // Stop here if next character belongs to another script
634 // or there is a change in change tracking status.
635 if (!Encodings::isKnownScriptChar(next, script) ||
636 runningChange != owner_->lookupChange(i + 1))
640 FontList::const_iterator cit = fontlist_.begin();
641 FontList::const_iterator end = fontlist_.end();
642 for (; cit != end; ++cit) {
643 if (cit->pos() >= i && !found) {
644 prev_font = cit->font();
647 if (cit->pos() >= i + 1)
650 // Stop here if there is a font attribute or encoding change.
651 if (found && cit != end && prev_font != cit->font())
653 docstring const latex = encoding.latexChar(next);
654 docstring::size_type const b1 =
655 latex.find_first_of(from_ascii("{"));
656 docstring::size_type const b2 =
657 latex.find_last_of(from_ascii("}"));
658 int const len = b2 - b1 - 1;
659 os << latex.substr(b1 + 1, len);
671 bool Paragraph::Private::isTextAt(string const & str, pos_type pos) const
673 pos_type const len = str.length();
675 // is the paragraph large enough?
676 if (pos + len > int(text_.size()))
679 // does the wanted text start at point?
680 for (string::size_type i = 0; i < str.length(); ++i) {
681 // Caution: direct comparison of characters works only
682 // because str is pure ASCII.
683 if (str[i] != text_[pos + i])
687 return fontlist_.hasChangeInRange(pos, len);
691 void Paragraph::Private::latexInset(
692 BufferParams const & bparams,
695 OutputParams & runparams,
698 Font const & outerfont,
700 Change & running_change,
701 Layout const & style,
703 unsigned int & column)
705 Inset * inset = owner_->getInset(i);
706 LASSERT(inset, /**/);
708 if (style.pass_thru) {
709 inset->plaintext(os, runparams);
713 // FIXME: move this to InsetNewline::latex
714 if (inset->lyxCode() == NEWLINE_CODE) {
715 // newlines are handled differently here than
716 // the default in simpleTeXSpecialChars().
717 if (!style.newline_allowed) {
721 column += running_font.latexWriteEndChanges(
722 os, bparams, runparams,
727 if (running_font.fontInfo().family() == TYPEWRITER_FAMILY)
730 basefont = owner_->getLayoutFont(bparams, outerfont);
731 running_font = basefont;
733 if (runparams.moving_arg)
738 texrow.start(owner_->id(), i + 1);
742 if (owner_->lookupChange(i).type == Change::DELETED) {
743 if( ++runparams.inDeletedInset == 1)
744 runparams.changeOfDeletedInset = owner_->lookupChange(i);
747 if (inset->canTrackChanges()) {
748 column += Changes::latexMarkChange(os, bparams, running_change,
749 Change(Change::UNCHANGED));
750 running_change = Change(Change::UNCHANGED);
754 odocstream::pos_type const len = os.tellp();
756 if (inset->forceLTR()
757 && running_font.isRightToLeft()
758 // ERT is an exception, it should be output with no decorations at all
759 && inset->lyxCode() != ERT_CODE) {
760 if (running_font.language()->lang() == "farsi")
767 // FIXME: Bug: we can have an empty font change here!
768 // if there has just been a font change, we are going to close it
769 // right now, which means stupid latex code like \textsf{}. AFAIK,
770 // this does not harm dvi output. A minor bug, thus (JMarc)
772 // Some insets cannot be inside a font change command.
773 // However, even such insets *can* be placed in \L or \R
774 // or their equivalents (for RTL language switches), so we don't
775 // close the language in those cases.
776 // ArabTeX, though, cannot handle this special behavior, it seems.
777 bool arabtex = basefont.language()->lang() == "arabic_arabtex"
778 || running_font.language()->lang() == "arabic_arabtex";
779 if (open_font && inset->noFontChange()) {
780 bool closeLanguage = arabtex
781 || basefont.isRightToLeft() == running_font.isRightToLeft();
782 unsigned int count = running_font.latexWriteEndChanges(os,
783 bparams, runparams, basefont, basefont, closeLanguage);
785 // if any font properties were closed, update the running_font,
786 // making sure, however, to leave the language as it was
788 // FIXME: probably a better way to keep track of the old
789 // language, than copying the entire font?
790 Font const copy_font(running_font);
791 basefont = owner_->getLayoutFont(bparams, outerfont);
792 running_font = basefont;
794 running_font.setLanguage(copy_font.language());
795 // leave font open if language is still open
796 open_font = (running_font.language() == basefont.language());
798 runparams.local_font = &basefont;
805 tmp = inset->latex(os, runparams);
806 } catch (EncodingException & e) {
807 // add location information and throw again.
814 if (running_font.language()->lang() == "farsi")
821 for (int j = 0; j < tmp; ++j)
824 texrow.start(owner_->id(), i + 1);
827 column += os.tellp() - len;
830 if (owner_->lookupChange(i).type == Change::DELETED)
831 --runparams.inDeletedInset;
835 void Paragraph::Private::latexSpecialChar(
837 OutputParams & runparams,
839 Change & running_change,
840 Layout const & style,
842 unsigned int & column)
844 char_type const c = text_[i];
846 if (style.pass_thru) {
848 // FIXME UNICODE: This can fail if c cannot
849 // be encoded in the current encoding.
854 if (runparams.verbatim) {
855 // FIXME UNICODE: This can fail if c cannot
856 // be encoded in the current encoding.
861 if (lyxrc.fontenc == "T1" && latexSpecialT1(c, os, i, column))
864 if (running_font.fontInfo().family() == TYPEWRITER_FAMILY
865 && latexSpecialTypewriter(c, os, i, column))
868 // Otherwise, we use what LaTeX provides us.
871 os << "\\textbackslash{}";
875 os << "\\textless{}";
879 os << "\\textgreater{}";
890 os << "\\char`\\\"{}";
895 case '%': case '#': case '{':
903 os << "\\textasciitilde{}";
908 os << "\\textasciicircum{}";
913 // avoid being mistaken for optional arguments
921 // Blanks are printed before font switching.
922 // Sure? I am not! (try nice-latex)
923 // I am sure it's correct. LyX might be smarter
924 // in the future, but for now, nothing wrong is
930 if (latexSpecialPhrase(os, i, column, runparams))
936 Encoding const & encoding = *(runparams.encoding);
937 if (i + 1 < int(text_.size())) {
938 char_type next = text_[i + 1];
939 if (Encodings::isCombiningChar(next)) {
940 column += latexSurrogatePair(os, c, next, encoding) - 1;
946 docstring const latex = encoding.latexChar(c);
947 if (Encodings::isKnownScriptChar(c, script)
948 && prefixIs(latex, from_ascii("\\" + script)))
949 column += writeScriptChars(os, latex,
950 running_change, encoding, i) - 1;
951 else if (latex.length() > 1 && latex[latex.length() - 1] != '}') {
952 // Prevent eating of a following
953 // space or command corruption by
954 // following characters
955 column += latex.length() + 1;
958 column += latex.length() - 1;
966 bool Paragraph::Private::latexSpecialT1(char_type const c, odocstream & os,
967 pos_type & i, unsigned int & column)
973 // In T1 encoding, these characters exist
974 // but we should avoid ligatures
975 if (i + 1 >= int(text_.size()) || text_[i + 1] != c)
977 os << "\\textcompwordmark{}";
989 bool Paragraph::Private::latexSpecialTypewriter(char_type const c, odocstream & os,
990 pos_type & i, unsigned int & column)
994 // within \ttfamily, "--" is merged to "-" (no endash)
995 // so we avoid this rather irritating ligature
996 if (i + 1 < int(text_.size()) && text_[i + 1] == '-') {
1003 // everything else has to be checked separately
1004 // (depending on the encoding)
1011 bool Paragraph::Private::latexSpecialPhrase(odocstream & os, pos_type & i,
1012 unsigned int & column, OutputParams & runparams)
1014 // FIXME: if we have "LaTeX" with a font
1015 // change in the middle (before the 'T', then
1016 // the "TeX" part is still special cased.
1017 // Really we should only operate this on
1018 // "words" for some definition of word
1020 for (size_t pnr = 0; pnr < phrases_nr; ++pnr) {
1021 if (!isTextAt(special_phrases[pnr].phrase, i))
1023 if (runparams.moving_arg)
1025 os << special_phrases[pnr].macro;
1026 i += special_phrases[pnr].phrase.length() - 1;
1027 column += special_phrases[pnr].macro.length() - 1;
1034 void Paragraph::Private::validate(LaTeXFeatures & features,
1035 Layout const & layout) const
1037 // check the params.
1038 if (!params_.spacing().isDefault())
1039 features.require("setspace");
1042 features.useLayout(layout.name());
1045 fontlist_.validate(features);
1047 // then the indentation
1048 if (!params_.leftIndent().zero())
1049 features.require("ParagraphLeftIndent");
1052 InsetList::const_iterator icit = insetlist_.begin();
1053 InsetList::const_iterator iend = insetlist_.end();
1054 for (; icit != iend; ++icit) {
1056 icit->inset->validate(features);
1057 if (layout.needprotect &&
1058 icit->inset->lyxCode() == FOOT_CODE)
1059 features.require("NeedLyXFootnoteCode");
1063 // then the contents
1064 for (pos_type i = 0; i < int(text_.size()) ; ++i) {
1065 for (size_t pnr = 0; pnr < phrases_nr; ++pnr) {
1066 if (!special_phrases[pnr].builtin
1067 && isTextAt(special_phrases[pnr].phrase, i)) {
1068 features.require(special_phrases[pnr].phrase);
1072 Encodings::validate(text_[i], features);
1076 /////////////////////////////////////////////////////////////////////
1080 /////////////////////////////////////////////////////////////////////
1083 Layout const emptyParagraphLayout;
1086 Paragraph::Paragraph()
1087 : d(new Paragraph::Private(this, emptyParagraphLayout))
1094 Paragraph::Paragraph(Paragraph const & par)
1095 : itemdepth(par.itemdepth),
1096 d(new Paragraph::Private(*par.d, this))
1102 Paragraph::Paragraph(Paragraph const & par, pos_type beg, pos_type end)
1103 : itemdepth(par.itemdepth),
1104 d(new Paragraph::Private(*par.d, this, beg, end))
1110 Paragraph & Paragraph::operator=(Paragraph const & par)
1112 // needed as we will destroy the private part before copying it
1114 itemdepth = par.itemdepth;
1118 d = new Private(*par.d, this);
1125 Paragraph::~Paragraph()
1132 void Paragraph::write(ostream & os, BufferParams const & bparams,
1133 depth_type & dth) const
1135 // The beginning or end of a deeper (i.e. nested) area?
1136 if (dth != d->params_.depth()) {
1137 if (d->params_.depth() > dth) {
1138 while (d->params_.depth() > dth) {
1139 os << "\n\\begin_deeper";
1143 while (d->params_.depth() < dth) {
1144 os << "\n\\end_deeper";
1150 // First write the layout
1151 os << "\n\\begin_layout " << to_utf8(d->layout_->name()) << '\n';
1153 d->params_.write(os);
1155 Font font1(inherit_font, bparams.language);
1157 Change running_change = Change(Change::UNCHANGED);
1160 for (pos_type i = 0; i <= size(); ++i) {
1162 Change change = lookupChange(i);
1163 Changes::lyxMarkChange(os, column, running_change, change);
1164 running_change = change;
1169 // Write font changes
1170 Font font2 = getFontSettings(bparams, i);
1171 if (font2 != font1) {
1172 font2.lyxWriteChanges(font1, os);
1177 char_type const c = d->text_[i];
1180 if (Inset const * inset = getInset(i)) {
1181 if (inset->directWrite()) {
1182 // international char, let it write
1183 // code directly so it's shorter in
1189 os << "\\begin_inset ";
1191 os << "\n\\end_inset\n\n";
1197 os << "\n\\backslash\n";
1201 if (i + 1 < size() && d->text_[i + 1] == ' ') {
1208 if ((column > 70 && c == ' ')
1213 // this check is to amend a bug. LyX sometimes
1214 // inserts '\0' this could cause problems.
1216 os << to_utf8(docstring(1, c));
1218 LYXERR0("NUL char in structure.");
1224 os << "\n\\end_layout\n";
1228 void Paragraph::validate(LaTeXFeatures & features) const
1230 d->validate(features, *d->layout_);
1234 void Paragraph::insert(pos_type start, docstring const & str,
1235 Font const & font, Change const & change)
1237 for (size_t i = 0, n = str.size(); i != n ; ++i)
1238 insertChar(start + i, str[i], font, change);
1242 void Paragraph::appendChar(char_type c, Font const & font,
1243 Change const & change)
1246 d->changes_.insert(change, d->text_.size());
1247 // when appending characters, no need to update tables
1248 d->text_.push_back(c);
1249 setFont(d->text_.size() - 1, font);
1253 void Paragraph::appendString(docstring const & s, Font const & font,
1254 Change const & change)
1256 pos_type end = s.size();
1257 size_t oldsize = d->text_.size();
1258 size_t newsize = oldsize + end;
1259 size_t capacity = d->text_.capacity();
1260 if (newsize >= capacity)
1261 d->text_.reserve(max(capacity + 100, newsize));
1263 // when appending characters, no need to update tables
1266 // FIXME: Optimize this!
1267 for (pos_type i = oldsize; i != newsize; ++i) {
1269 d->changes_.insert(change, i);
1271 d->fontlist_.set(oldsize, font);
1272 d->fontlist_.set(newsize - 1, font);
1276 void Paragraph::insertChar(pos_type pos, char_type c,
1279 d->insertChar(pos, c, Change(trackChanges ?
1280 Change::INSERTED : Change::UNCHANGED));
1284 void Paragraph::insertChar(pos_type pos, char_type c,
1285 Font const & font, bool trackChanges)
1287 d->insertChar(pos, c, Change(trackChanges ?
1288 Change::INSERTED : Change::UNCHANGED));
1293 void Paragraph::insertChar(pos_type pos, char_type c,
1294 Font const & font, Change const & change)
1296 d->insertChar(pos, c, change);
1301 void Paragraph::insertInset(pos_type pos, Inset * inset,
1302 Font const & font, Change const & change)
1304 insertInset(pos, inset, change);
1305 // Set the font/language of the inset...
1310 bool Paragraph::insetAllowed(InsetCode code)
1312 return !d->inset_owner_ || d->inset_owner_->insetAllowed(code);
1316 void Paragraph::resetFonts(Font const & font)
1318 d->fontlist_.clear();
1319 d->fontlist_.set(0, font);
1320 d->fontlist_.set(d->text_.size() - 1, font);
1323 // Gets uninstantiated font setting at position.
1324 Font const Paragraph::getFontSettings(BufferParams const & bparams,
1328 LYXERR0("pos: " << pos << " size: " << size());
1329 LASSERT(pos <= size(), /**/);
1332 FontList::const_iterator cit = d->fontlist_.fontIterator(pos);
1333 if (cit != d->fontlist_.end())
1336 if (pos == size() && !empty())
1337 return getFontSettings(bparams, pos - 1);
1339 return Font(inherit_font, getParLanguage(bparams));
1343 FontSpan Paragraph::fontSpan(pos_type pos) const
1345 LASSERT(pos <= size(), /**/);
1348 FontList::const_iterator cit = d->fontlist_.begin();
1349 FontList::const_iterator end = d->fontlist_.end();
1350 for (; cit != end; ++cit) {
1351 if (cit->pos() >= pos) {
1352 if (pos >= beginOfBody())
1353 return FontSpan(max(start, beginOfBody()),
1356 return FontSpan(start,
1357 min(beginOfBody() - 1,
1360 start = cit->pos() + 1;
1363 // This should not happen, but if so, we take no chances.
1364 // LYXERR0("Paragraph::getEndPosOfFontSpan: This should not happen!");
1365 return FontSpan(pos, pos);
1369 // Gets uninstantiated font setting at position 0
1370 Font const Paragraph::getFirstFontSettings(BufferParams const & bparams) const
1372 if (!empty() && !d->fontlist_.empty())
1373 return d->fontlist_.begin()->font();
1375 return Font(inherit_font, bparams.language);
1379 // Gets the fully instantiated font at a given position in a paragraph
1380 // This is basically the same function as Text::GetFont() in text2.cpp.
1381 // The difference is that this one is used for generating the LaTeX file,
1382 // and thus cosmetic "improvements" are disallowed: This has to deliver
1383 // the true picture of the buffer. (Asger)
1384 Font const Paragraph::getFont(BufferParams const & bparams, pos_type pos,
1385 Font const & outerfont) const
1387 LASSERT(pos >= 0, /**/);
1389 Font font = getFontSettings(bparams, pos);
1391 pos_type const body_pos = beginOfBody();
1393 font.fontInfo().realize(d->layout_->labelfont);
1395 font.fontInfo().realize(d->layout_->font);
1397 font.fontInfo().realize(outerfont.fontInfo());
1398 font.fontInfo().realize(bparams.getFont().fontInfo());
1404 Font const Paragraph::getLabelFont
1405 (BufferParams const & bparams, Font const & outerfont) const
1407 FontInfo tmpfont = d->layout_->labelfont;
1408 tmpfont.realize(outerfont.fontInfo());
1409 tmpfont.realize(bparams.getFont().fontInfo());
1410 return Font(tmpfont, getParLanguage(bparams));
1414 Font const Paragraph::getLayoutFont
1415 (BufferParams const & bparams, Font const & outerfont) const
1417 FontInfo tmpfont = d->layout_->font;
1418 tmpfont.realize(outerfont.fontInfo());
1419 tmpfont.realize(bparams.getFont().fontInfo());
1420 return Font(tmpfont, getParLanguage(bparams));
1424 /// Returns the height of the highest font in range
1425 FontSize Paragraph::highestFontInRange
1426 (pos_type startpos, pos_type endpos, FontSize def_size) const
1428 return d->fontlist_.highestInRange(startpos, endpos, def_size);
1432 char_type Paragraph::getUChar(BufferParams const & bparams, pos_type pos) const
1434 char_type c = d->text_[pos];
1435 if (!lyxrc.rtl_support)
1465 if (uc != c && getFontSettings(bparams, pos).isRightToLeft())
1471 void Paragraph::setFont(pos_type pos, Font const & font)
1473 LASSERT(pos <= size(), /**/);
1475 // First, reduce font against layout/label font
1476 // Update: The setCharFont() routine in text2.cpp already
1477 // reduces font, so we don't need to do that here. (Asger)
1479 d->fontlist_.set(pos, font);
1483 void Paragraph::makeSameLayout(Paragraph const & par)
1485 d->layout_ = par.d->layout_;
1486 d->params_ = par.d->params_;
1490 bool Paragraph::stripLeadingSpaces(bool trackChanges)
1492 if (isFreeSpacing())
1498 while (pos < size() && (isNewline(pos) || isLineSeparator(pos))) {
1499 if (eraseChar(pos, trackChanges))
1505 return count > 0 || pos > 0;
1509 bool Paragraph::hasSameLayout(Paragraph const & par) const
1511 return par.d->layout_ == d->layout_
1512 && d->params_.sameLayout(par.d->params_);
1516 depth_type Paragraph::getDepth() const
1518 return d->params_.depth();
1522 depth_type Paragraph::getMaxDepthAfter() const
1524 if (d->layout_->isEnvironment())
1525 return d->params_.depth() + 1;
1527 return d->params_.depth();
1531 char Paragraph::getAlign() const
1533 if (d->params_.align() == LYX_ALIGN_LAYOUT)
1534 return d->layout_->align;
1536 return d->params_.align();
1540 docstring const & Paragraph::labelString() const
1542 return d->params_.labelString();
1546 // the next two functions are for the manual labels
1547 docstring const Paragraph::getLabelWidthString() const
1549 if (d->layout_->margintype == MARGIN_MANUAL)
1550 return d->params_.labelWidthString();
1552 return _("Senseless with this layout!");
1556 void Paragraph::setLabelWidthString(docstring const & s)
1558 d->params_.labelWidthString(s);
1562 docstring const Paragraph::translateIfPossible(docstring const & s,
1563 BufferParams const & bparams) const
1565 if (!isAscii(s) || s.empty()) {
1566 // This must be a user defined layout. We cannot translate
1567 // this, since gettext accepts only ascii keys.
1570 // Probably standard layout, try to translate
1571 Messages & m = getMessages(getParLanguage(bparams)->code());
1572 return m.get(to_ascii(s));
1576 docstring Paragraph::expandLabel(Layout const & layout,
1577 BufferParams const & bparams, bool process_appendix) const
1579 DocumentClass const & tclass = bparams.documentClass();
1582 if (process_appendix && d->params_.appendix())
1583 fmt = translateIfPossible(layout.labelstring_appendix(),
1586 fmt = translateIfPossible(layout.labelstring(), bparams);
1588 if (fmt.empty() && layout.labeltype == LABEL_COUNTER
1589 && !layout.counter.empty())
1590 fmt = "\\the" + layout.counter;
1592 // handle 'inherited level parts' in 'fmt',
1593 // i.e. the stuff between '@' in '@Section@.\arabic{subsection}'
1594 size_t const i = fmt.find('@', 0);
1595 if (i != docstring::npos) {
1596 size_t const j = fmt.find('@', i + 1);
1597 if (j != docstring::npos) {
1598 docstring parent(fmt, i + 1, j - i - 1);
1599 docstring label = from_ascii("??");
1600 if (tclass.hasLayout(parent))
1601 docstring label = expandLabel(tclass[parent], bparams,
1603 fmt = docstring(fmt, 0, i) + label
1604 + docstring(fmt, j + 1, docstring::npos);
1608 return tclass.counters().counterLabel(fmt);
1612 void Paragraph::applyLayout(Layout const & new_layout)
1614 d->layout_ = &new_layout;
1615 LyXAlignment const oldAlign = d->params_.align();
1617 if (!(oldAlign & d->layout_->alignpossible)) {
1618 frontend::Alert::warning(_("Alignment not permitted"),
1619 _("The new layout does not permit the alignment previously used.\nSetting to default."));
1620 d->params_.align(LYX_ALIGN_LAYOUT);
1625 pos_type Paragraph::beginOfBody() const
1627 return d->begin_of_body_;
1631 void Paragraph::setBeginOfBody()
1633 if (d->layout_->labeltype != LABEL_MANUAL) {
1634 d->begin_of_body_ = 0;
1638 // Unroll the first two cycles of the loop
1639 // and remember the previous character to
1640 // remove unnecessary getChar() calls
1642 pos_type end = size();
1643 if (i < end && !isNewline(i)) {
1645 char_type previous_char = 0;
1648 previous_char = d->text_[i];
1649 if (!isNewline(i)) {
1651 while (i < end && previous_char != ' ') {
1656 previous_char = temp;
1662 d->begin_of_body_ = i;
1666 bool Paragraph::forcePlainLayout() const
1668 Inset const * const inset = inInset();
1671 return inset->forcePlainLayout();
1675 bool Paragraph::allowParagraphCustomization() const
1677 Inset const * const inset = inInset();
1680 return inset->allowParagraphCustomization();
1684 bool Paragraph::usePlainLayout() const
1686 Inset const * const inset = inInset();
1689 return inset->usePlainLayout();
1695 // paragraphs inside floats need different alignment tags to avoid
1698 bool noTrivlistCentering(InsetCode code)
1700 return code == FLOAT_CODE || code == WRAP_CODE;
1704 string correction(string const & orig)
1706 if (orig == "flushleft")
1707 return "raggedright";
1708 if (orig == "flushright")
1709 return "raggedleft";
1710 if (orig == "center")
1716 string const corrected_env(string const & suffix, string const & env,
1719 string output = suffix + "{";
1720 if (noTrivlistCentering(code))
1721 output += correction(env);
1725 if (suffix == "\\begin")
1731 void adjust_row_column(string const & str, TexRow & texrow, int & column)
1733 if (!contains(str, "\n"))
1734 column += str.size();
1738 column = rsplit(str, tmp, '\n').size();
1745 int Paragraph::Private::startTeXParParams(BufferParams const & bparams,
1746 odocstream & os, TexRow & texrow,
1747 bool moving_arg) const
1751 if (params_.noindent()) {
1752 os << "\\noindent ";
1756 LyXAlignment const curAlign = params_.align();
1758 if (curAlign == layout_->align)
1762 case LYX_ALIGN_NONE:
1763 case LYX_ALIGN_BLOCK:
1764 case LYX_ALIGN_LAYOUT:
1765 case LYX_ALIGN_SPECIAL:
1767 case LYX_ALIGN_LEFT:
1768 case LYX_ALIGN_RIGHT:
1769 case LYX_ALIGN_CENTER:
1778 case LYX_ALIGN_NONE:
1779 case LYX_ALIGN_BLOCK:
1780 case LYX_ALIGN_LAYOUT:
1781 case LYX_ALIGN_SPECIAL:
1783 case LYX_ALIGN_LEFT: {
1785 if (owner_->getParLanguage(bparams)->babel() != "hebrew")
1786 output = corrected_env("\\begin", "flushleft", owner_->ownerCode());
1788 output = corrected_env("\\begin", "flushright", owner_->ownerCode());
1789 os << from_ascii(output);
1790 adjust_row_column(output, texrow, column);
1792 } case LYX_ALIGN_RIGHT: {
1794 if (owner_->getParLanguage(bparams)->babel() != "hebrew")
1795 output = corrected_env("\\begin", "flushright", owner_->ownerCode());
1797 output = corrected_env("\\begin", "flushleft", owner_->ownerCode());
1798 os << from_ascii(output);
1799 adjust_row_column(output, texrow, column);
1801 } case LYX_ALIGN_CENTER: {
1803 output = corrected_env("\\begin", "center", owner_->ownerCode());
1804 os << from_ascii(output);
1805 adjust_row_column(output, texrow, column);
1814 int Paragraph::Private::endTeXParParams(BufferParams const & bparams,
1815 odocstream & os, TexRow & texrow,
1816 bool moving_arg) const
1820 switch (params_.align()) {
1821 case LYX_ALIGN_NONE:
1822 case LYX_ALIGN_BLOCK:
1823 case LYX_ALIGN_LAYOUT:
1824 case LYX_ALIGN_SPECIAL:
1826 case LYX_ALIGN_LEFT:
1827 case LYX_ALIGN_RIGHT:
1828 case LYX_ALIGN_CENTER:
1836 switch (params_.align()) {
1837 case LYX_ALIGN_NONE:
1838 case LYX_ALIGN_BLOCK:
1839 case LYX_ALIGN_LAYOUT:
1840 case LYX_ALIGN_SPECIAL:
1842 case LYX_ALIGN_LEFT: {
1844 if (owner_->getParLanguage(bparams)->babel() != "hebrew")
1845 output = corrected_env("\n\\par\\end", "flushleft", owner_->ownerCode());
1847 output = corrected_env("\n\\par\\end", "flushright", owner_->ownerCode());
1848 os << from_ascii(output);
1849 adjust_row_column(output, texrow, column);
1851 } case LYX_ALIGN_RIGHT: {
1853 if (owner_->getParLanguage(bparams)->babel() != "hebrew")
1854 output = corrected_env("\n\\par\\end", "flushright", owner_->ownerCode());
1856 output = corrected_env("\n\\par\\end", "flushleft", owner_->ownerCode());
1857 os << from_ascii(output);
1858 adjust_row_column(output, texrow, column);
1860 } case LYX_ALIGN_CENTER: {
1862 output = corrected_env("\n\\par\\end", "center", owner_->ownerCode());
1863 os << from_ascii(output);
1864 adjust_row_column(output, texrow, column);
1873 // This one spits out the text of the paragraph
1874 bool Paragraph::latex(BufferParams const & bparams,
1875 Font const & outerfont,
1876 odocstream & os, TexRow & texrow,
1877 OutputParams const & runparams) const
1879 LYXERR(Debug::LATEX, "SimpleTeXOnePar... " << this);
1881 bool return_value = false;
1883 bool asdefault = forcePlainLayout();
1885 Layout const & style = asdefault ?
1886 bparams.documentClass().plainLayout() :
1889 // Current base font for all inherited font changes, without any
1890 // change caused by an individual character, except for the language:
1891 // It is set to the language of the first character.
1892 // As long as we are in the label, this font is the base font of the
1893 // label. Before the first body character it is set to the base font
1897 // Maybe we have to create a optional argument.
1898 pos_type body_pos = beginOfBody();
1899 unsigned int column = 0;
1902 // the optional argument is kept in curly brackets in
1903 // case it contains a ']'
1906 basefont = getLabelFont(bparams, outerfont);
1908 basefont = getLayoutFont(bparams, outerfont);
1911 // Which font is currently active?
1912 Font running_font(basefont);
1913 // Do we have an open font change?
1914 bool open_font = false;
1916 Change runningChange = Change(Change::UNCHANGED);
1918 texrow.start(id(), 0);
1920 // if the paragraph is empty, the loop will not be entered at all
1922 if (style.isCommand()) {
1927 column += d->startTeXParParams(bparams, os, texrow,
1928 runparams.moving_arg);
1931 for (pos_type i = 0; i < size(); ++i) {
1932 // First char in paragraph or after label?
1933 if (i == body_pos) {
1936 column += running_font.latexWriteEndChanges(
1937 os, bparams, runparams,
1938 basefont, basefont);
1941 basefont = getLayoutFont(bparams, outerfont);
1942 running_font = basefont;
1944 column += Changes::latexMarkChange(os, bparams,
1945 runningChange, Change(Change::UNCHANGED));
1946 runningChange = Change(Change::UNCHANGED);
1951 if (style.isCommand()) {
1957 column += d->startTeXParParams(bparams, os,
1959 runparams.moving_arg);
1962 Change const & change = runparams.inDeletedInset ? runparams.changeOfDeletedInset
1965 if (bparams.outputChanges && runningChange != change) {
1967 column += running_font.latexWriteEndChanges(
1968 os, bparams, runparams, basefont, basefont);
1971 basefont = getLayoutFont(bparams, outerfont);
1972 running_font = basefont;
1974 column += Changes::latexMarkChange(os, bparams, runningChange, change);
1975 runningChange = change;
1978 // do not output text which is marked deleted
1979 // if change tracking output is disabled
1980 if (!bparams.outputChanges && change.type == Change::DELETED) {
1986 // Fully instantiated font
1987 Font const font = getFont(bparams, i, outerfont);
1989 Font const last_font = running_font;
1991 // Do we need to close the previous font?
1993 (font != running_font ||
1994 font.language() != running_font.language()))
1996 column += running_font.latexWriteEndChanges(
1997 os, bparams, runparams, basefont,
1998 (i == body_pos-1) ? basefont : font);
1999 running_font = basefont;
2003 // close babel's font environment before opening CJK.
2004 if (!running_font.language()->babel().empty() &&
2005 font.language()->encoding()->package() == Encoding::CJK) {
2006 string end_tag = subst(lyxrc.language_command_end,
2008 running_font.language()->babel());
2009 os << from_ascii(end_tag);
2010 column += end_tag.length();
2013 // Switch file encoding if necessary (and allowed)
2014 if (!runparams.verbatim &&
2015 runparams.encoding->package() != Encoding::none &&
2016 font.language()->encoding()->package() != Encoding::none) {
2017 pair<bool, int> const enc_switch = switchEncoding(os, bparams,
2018 runparams, *(font.language()->encoding()));
2019 if (enc_switch.first) {
2020 column += enc_switch.second;
2021 runparams.encoding = font.language()->encoding();
2025 char_type const c = d->text_[i];
2027 // Do we need to change font?
2028 if ((font != running_font ||
2029 font.language() != running_font.language()) &&
2032 odocstringstream ods;
2033 column += font.latexWriteStartChanges(ods, bparams,
2034 runparams, basefont,
2036 running_font = font;
2038 docstring fontchange = ods.str();
2039 // check if the fontchange ends with a trailing blank
2040 // (like "\small " (see bug 3382)
2041 if (suffixIs(fontchange, ' ') && c == ' ')
2042 os << fontchange.substr(0, fontchange.size() - 1)
2043 << from_ascii("{}");
2049 // FIXME: integrate this case in latexSpecialChar
2050 // Do not print the separation of the optional argument
2051 // if style.pass_thru is false. This works because
2052 // latexSpecialChar ignores spaces if
2053 // style.pass_thru is false.
2054 if (i != body_pos - 1) {
2055 if (d->simpleTeXBlanks(
2056 runparams, os, texrow,
2057 i, column, font, style)) {
2058 // A surrogate pair was output. We
2059 // must not call latexSpecialChar
2060 // in this iteration, since it would output
2061 // the combining character again.
2068 OutputParams rp = runparams;
2069 rp.free_spacing = style.free_spacing;
2070 rp.local_font = &font;
2071 rp.intitle = style.intitle;
2073 // Two major modes: LaTeX or plain
2074 // Handle here those cases common to both modes
2075 // and then split to handle the two modes separately.
2076 if (c == META_INSET)
2077 d->latexInset(bparams, os,
2078 texrow, rp, running_font,
2079 basefont, outerfont, open_font,
2080 runningChange, style, i, column);
2083 d->latexSpecialChar(os, rp, running_font, runningChange,
2085 } catch (EncodingException & e) {
2086 if (runparams.dryrun) {
2087 os << "<" << _("LyX Warning: ")
2088 << _("uncodable character") << " '";
2092 // add location information and throw again.
2100 // Set the encoding to that returned from latexSpecialChar (see
2101 // comment for encoding member in OutputParams.h)
2102 runparams.encoding = rp.encoding;
2105 // If we have an open font definition, we have to close it
2107 #ifdef FIXED_LANGUAGE_END_DETECTION
2110 .latexWriteEndChanges(os, bparams, runparams,
2112 next_->getFont(bparams, 0, outerfont));
2114 running_font.latexWriteEndChanges(os, bparams,
2115 runparams, basefont, basefont);
2118 //FIXME: For now we ALWAYS have to close the foreign font settings if they are
2119 //FIXME: there as we start another \selectlanguage with the next paragraph if
2120 //FIXME: we are in need of this. This should be fixed sometime (Jug)
2121 running_font.latexWriteEndChanges(os, bparams, runparams,
2122 basefont, basefont);
2126 column += Changes::latexMarkChange(os, bparams, runningChange, Change(Change::UNCHANGED));
2128 // Needed if there is an optional argument but no contents.
2129 if (body_pos > 0 && body_pos == size()) {
2131 return_value = false;
2135 column += d->endTeXParParams(bparams, os, texrow,
2136 runparams.moving_arg);
2139 LYXERR(Debug::LATEX, "SimpleTeXOnePar...done " << this);
2140 return return_value;
2144 bool Paragraph::emptyTag() const
2146 for (pos_type i = 0; i < size(); ++i) {
2147 if (Inset const * inset = getInset(i)) {
2148 InsetCode lyx_code = inset->lyxCode();
2149 if (lyx_code != TOC_CODE &&
2150 lyx_code != INCLUDE_CODE &&
2151 lyx_code != GRAPHICS_CODE &&
2152 lyx_code != ERT_CODE &&
2153 lyx_code != LISTINGS_CODE &&
2154 lyx_code != FLOAT_CODE &&
2155 lyx_code != TABULAR_CODE) {
2159 char_type c = d->text_[i];
2160 if (c != ' ' && c != '\t')
2168 string Paragraph::getID(Buffer const & buf, OutputParams const & runparams)
2171 for (pos_type i = 0; i < size(); ++i) {
2172 if (Inset const * inset = getInset(i)) {
2173 InsetCode lyx_code = inset->lyxCode();
2174 if (lyx_code == LABEL_CODE) {
2175 InsetLabel const * const il = static_cast<InsetLabel const *>(inset);
2176 docstring const & id = il->getParam("name");
2177 return "id='" + to_utf8(sgml::cleanID(buf, runparams, id)) + "'";
2185 pos_type Paragraph::firstWord(odocstream & os, OutputParams const & runparams)
2189 for (i = 0; i < size(); ++i) {
2190 if (Inset const * inset = getInset(i)) {
2191 inset->docbook(os, runparams);
2193 char_type c = d->text_[i];
2196 os << sgml::escapeChar(c);
2203 bool Paragraph::Private::onlyText(Buffer const & buf, Font const & outerfont, pos_type initial) const
2206 pos_type size = text_.size();
2207 for (pos_type i = initial; i < size; ++i) {
2208 Font font = owner_->getFont(buf.params(), i, outerfont);
2209 if (text_[i] == META_INSET)
2211 if (i != initial && font != font_old)
2220 void Paragraph::simpleDocBookOnePar(Buffer const & buf,
2222 OutputParams const & runparams,
2223 Font const & outerfont,
2224 pos_type initial) const
2226 bool emph_flag = false;
2228 Layout const & style = *d->layout_;
2230 style.labeltype == LABEL_MANUAL ? style.labelfont : style.font;
2232 if (style.pass_thru && !d->onlyText(buf, outerfont, initial))
2235 // parsing main loop
2236 for (pos_type i = initial; i < size(); ++i) {
2237 Font font = getFont(buf.params(), i, outerfont);
2239 // handle <emphasis> tag
2240 if (font_old.emph() != font.fontInfo().emph()) {
2241 if (font.fontInfo().emph() == FONT_ON) {
2244 } else if (i != initial) {
2245 os << "</emphasis>";
2250 if (Inset const * inset = getInset(i)) {
2251 inset->docbook(os, runparams);
2253 char_type c = d->text_[i];
2255 if (style.pass_thru)
2258 os << sgml::escapeChar(c);
2260 font_old = font.fontInfo();
2264 os << "</emphasis>";
2267 if (style.free_spacing)
2269 if (style.pass_thru && !d->onlyText(buf, outerfont, initial))
2274 bool Paragraph::isHfill(pos_type pos) const
2276 Inset const * inset = getInset(pos);
2277 return inset && (inset->lyxCode() == SPACE_CODE &&
2278 inset->isStretchableSpace());
2282 bool Paragraph::isNewline(pos_type pos) const
2284 Inset const * inset = getInset(pos);
2285 return inset && inset->lyxCode() == NEWLINE_CODE;
2289 bool Paragraph::isLineSeparator(pos_type pos) const
2291 char_type const c = d->text_[pos];
2292 if (isLineSeparatorChar(c))
2294 Inset const * inset = getInset(pos);
2295 return inset && inset->isLineSeparator();
2299 /// Used by the spellchecker
2300 bool Paragraph::isLetter(pos_type pos) const
2302 if (Inset const * inset = getInset(pos))
2303 return inset->isLetter();
2304 char_type const c = d->text_[pos];
2305 return isLetterChar(c) || isDigit(c);
2309 bool Paragraph::isChar(pos_type pos) const
2311 if (Inset const * inset = getInset(pos))
2312 return inset->isChar();
2313 char_type const c = d->text_[pos];
2314 return !isLetterChar(c) && !isDigit(c) && !lyx::isSpace(c);
2318 bool Paragraph::isSpace(pos_type pos) const
2320 if (Inset const * inset = getInset(pos))
2321 return inset->isSpace();
2322 char_type const c = d->text_[pos];
2323 return lyx::isSpace(c);
2328 Paragraph::getParLanguage(BufferParams const & bparams) const
2331 return getFirstFontSettings(bparams).language();
2332 // FIXME: we should check the prev par as well (Lgb)
2333 return bparams.language;
2337 bool Paragraph::isRTL(BufferParams const & bparams) const
2339 return lyxrc.rtl_support
2340 && getParLanguage(bparams)->rightToLeft()
2341 && ownerCode() != ERT_CODE
2342 && ownerCode() != LISTINGS_CODE;
2346 void Paragraph::changeLanguage(BufferParams const & bparams,
2347 Language const * from, Language const * to)
2349 // change language including dummy font change at the end
2350 for (pos_type i = 0; i <= size(); ++i) {
2351 Font font = getFontSettings(bparams, i);
2352 if (font.language() == from) {
2353 font.setLanguage(to);
2360 bool Paragraph::isMultiLingual(BufferParams const & bparams) const
2362 Language const * doc_language = bparams.language;
2363 FontList::const_iterator cit = d->fontlist_.begin();
2364 FontList::const_iterator end = d->fontlist_.end();
2366 for (; cit != end; ++cit)
2367 if (cit->font().language() != ignore_language &&
2368 cit->font().language() != latex_language &&
2369 cit->font().language() != doc_language)
2375 docstring Paragraph::asString(int options) const
2377 return asString(0, size(), options);
2381 docstring Paragraph::asString(pos_type beg, pos_type end, int options) const
2383 odocstringstream os;
2386 && options & AS_STR_LABEL
2387 && !d->params_.labelString().empty())
2388 os << d->params_.labelString() << ' ';
2390 for (pos_type i = beg; i < end; ++i) {
2391 char_type const c = d->text_[i];
2394 else if (c == META_INSET && options & AS_STR_INSETS)
2395 getInset(i)->textString(os);
2402 void Paragraph::setInsetOwner(Inset * inset)
2404 d->inset_owner_ = inset;
2408 int Paragraph::id() const
2414 Layout const & Paragraph::layout() const
2420 void Paragraph::setLayout(Layout const & layout)
2422 d->layout_ = &layout;
2426 void Paragraph::setPlainOrDefaultLayout(DocumentClass const & tclass)
2428 if (usePlainLayout())
2429 setLayout(tclass.plainLayout());
2431 setLayout(tclass.defaultLayout());
2435 Inset * Paragraph::inInset() const
2437 return d->inset_owner_;
2441 InsetCode Paragraph::ownerCode() const
2443 return d->inset_owner_ ? d->inset_owner_->lyxCode() : NO_CODE;
2447 ParagraphParameters & Paragraph::params()
2453 ParagraphParameters const & Paragraph::params() const
2459 bool Paragraph::isFreeSpacing() const
2461 if (d->layout_->free_spacing)
2463 return d->inset_owner_ && d->inset_owner_->isFreeSpacing();
2467 bool Paragraph::allowEmpty() const
2469 if (d->layout_->keepempty)
2471 return d->inset_owner_ && d->inset_owner_->allowEmpty();
2475 char_type Paragraph::transformChar(char_type c, pos_type pos) const
2477 if (!Encodings::isArabicChar(c))
2480 char_type prev_char = ' ';
2481 char_type next_char = ' ';
2483 for (pos_type i = pos - 1; i >= 0; --i) {
2484 char_type const par_char = d->text_[i];
2485 if (!Encodings::isArabicComposeChar(par_char)) {
2486 prev_char = par_char;
2491 for (pos_type i = pos + 1, end = size(); i < end; ++i) {
2492 char_type const par_char = d->text_[i];
2493 if (!Encodings::isArabicComposeChar(par_char)) {
2494 next_char = par_char;
2499 if (Encodings::isArabicChar(next_char)) {
2500 if (Encodings::isArabicChar(prev_char) &&
2501 !Encodings::isArabicSpecialChar(prev_char))
2502 return Encodings::transformChar(c, Encodings::FORM_MEDIAL);
2504 return Encodings::transformChar(c, Encodings::FORM_INITIAL);
2506 if (Encodings::isArabicChar(prev_char) &&
2507 !Encodings::isArabicSpecialChar(prev_char))
2508 return Encodings::transformChar(c, Encodings::FORM_FINAL);
2510 return Encodings::transformChar(c, Encodings::FORM_ISOLATED);
2515 int Paragraph::checkBiblio(Buffer const & buffer)
2518 // This is getting more and more a mess. ...We really should clean
2519 // up this bibitem issue for 1.6. See also bug 2743.
2521 // Add bibitem insets if necessary
2522 if (d->layout_->labeltype != LABEL_BIBLIO)
2525 bool hasbibitem = !d->insetlist_.empty()
2526 // Insist on it being in pos 0
2527 && d->text_[0] == META_INSET
2528 && d->insetlist_.begin()->inset->lyxCode() == BIBITEM_CODE;
2530 bool track_changes = buffer.params().trackChanges;
2535 // remove a bibitem in pos != 0
2536 // restore it later in pos 0 if necessary
2537 // (e.g. if a user inserts contents _before_ the item)
2538 // we're assuming there's only one of these, which there
2540 int erasedInsetPosition = -1;
2541 InsetList::iterator it = d->insetlist_.begin();
2542 InsetList::iterator end = d->insetlist_.end();
2543 for (; it != end; ++it)
2544 if (it->inset->lyxCode() == BIBITEM_CODE
2546 InsetBibitem * olditem = static_cast<InsetBibitem *>(it->inset);
2547 oldkey = olditem->getParam("key");
2548 oldlabel = olditem->getParam("label");
2549 erasedInsetPosition = it->pos;
2550 eraseChar(erasedInsetPosition, track_changes);
2554 // There was an InsetBibitem at the beginning, and we didn't
2555 // have to erase one.
2556 if (hasbibitem && erasedInsetPosition < 0)
2559 // There was an InsetBibitem at the beginning and we did have to
2560 // erase one. So we give its properties to the beginning inset.
2562 InsetBibitem * inset =
2563 static_cast<InsetBibitem *>(d->insetlist_.begin()->inset);
2564 if (!oldkey.empty())
2565 inset->setParam("key", oldkey);
2566 inset->setParam("label", oldlabel);
2567 return -erasedInsetPosition;
2570 // There was no inset at the beginning, so we need to create one with
2571 // the key and label of the one we erased.
2572 InsetBibitem * inset =
2573 new InsetBibitem(buffer, InsetCommandParams(BIBITEM_CODE));
2574 // restore values of previously deleted item in this par.
2575 if (!oldkey.empty())
2576 inset->setParam("key", oldkey);
2577 inset->setParam("label", oldlabel);
2578 insertInset(0, static_cast<Inset *>(inset),
2579 Change(track_changes ? Change::INSERTED : Change::UNCHANGED));
2585 void Paragraph::checkAuthors(AuthorList const & authorList)
2587 d->changes_.checkAuthors(authorList);
2591 bool Paragraph::isUnchanged(pos_type pos) const
2593 return lookupChange(pos).type == Change::UNCHANGED;
2597 bool Paragraph::isInserted(pos_type pos) const
2599 return lookupChange(pos).type == Change::INSERTED;
2603 bool Paragraph::isDeleted(pos_type pos) const
2605 return lookupChange(pos).type == Change::DELETED;
2609 InsetList const & Paragraph::insetList() const
2611 return d->insetlist_;
2615 void Paragraph::setBuffer(Buffer & b)
2617 d->insetlist_.setBuffer(b);
2621 Inset * Paragraph::releaseInset(pos_type pos)
2623 Inset * inset = d->insetlist_.release(pos);
2624 /// does not honour change tracking!
2625 eraseChar(pos, false);
2630 Inset * Paragraph::getInset(pos_type pos)
2632 return (pos < pos_type(d->text_.size()) && d->text_[pos] == META_INSET)
2633 ? d->insetlist_.get(pos) : 0;
2637 Inset const * Paragraph::getInset(pos_type pos) const
2639 return (pos < pos_type(d->text_.size()) && d->text_[pos] == META_INSET)
2640 ? d->insetlist_.get(pos) : 0;
2644 void Paragraph::changeCase(BufferParams const & bparams, pos_type pos,
2645 pos_type & right, TextCase action)
2647 // process sequences of modified characters; in change
2648 // tracking mode, this approach results in much better
2649 // usability than changing case on a char-by-char basis
2652 bool const trackChanges = bparams.trackChanges;
2654 bool capitalize = true;
2656 for (; pos < right; ++pos) {
2657 char_type oldChar = d->text_[pos];
2658 char_type newChar = oldChar;
2660 // ignore insets and don't play with deleted text!
2661 if (oldChar != META_INSET && !isDeleted(pos)) {
2663 case text_lowercase:
2664 newChar = lowercase(oldChar);
2666 case text_capitalization:
2668 newChar = uppercase(oldChar);
2672 case text_uppercase:
2673 newChar = uppercase(oldChar);
2678 if (!isLetter(pos) || isDeleted(pos)) {
2679 // permit capitalization again
2683 if (oldChar != newChar)
2686 if (oldChar == newChar || pos == right - 1) {
2687 if (oldChar != newChar) {
2688 // step behind the changing area
2691 int erasePos = pos - changes.size();
2692 for (size_t i = 0; i < changes.size(); i++) {
2693 insertChar(pos, changes[i],
2694 getFontSettings(bparams,
2697 if (!eraseChar(erasePos, trackChanges)) {
2700 ++right; // expand selection
2709 bool Paragraph::find(docstring const & str, bool cs, bool mw,
2710 pos_type pos, bool del) const
2712 int const strsize = str.length();
2714 pos_type const parsize = d->text_.size();
2715 for (i = 0; pos + i < parsize; ++i) {
2718 if (cs && str[i] != d->text_[pos + i])
2720 if (!cs && uppercase(str[i]) != uppercase(d->text_[pos + i]))
2722 if (!del && isDeleted(pos + i))
2729 // if necessary, check whether string matches word
2731 if (pos > 0 && isLetter(pos - 1))
2733 if (pos + strsize < parsize
2734 && isLetter(pos + strsize))
2742 char_type Paragraph::getChar(pos_type pos) const
2744 return d->text_[pos];
2748 pos_type Paragraph::size() const
2750 return d->text_.size();
2754 bool Paragraph::empty() const
2756 return d->text_.empty();
2760 bool Paragraph::isInset(pos_type pos) const
2762 return d->text_[pos] == META_INSET;
2766 bool Paragraph::isSeparator(pos_type pos) const
2768 //FIXME: Are we sure this can be the only separator?
2769 return d->text_[pos] == ' ';
2773 void Paragraph::deregisterWords()
2775 Private::Words::const_iterator it;
2776 WordList & wl = theWordList();
2777 for (it = d->words_.begin(); it != d->words_.end(); ++it)
2783 void Paragraph::collectWords(CursorSlice const & sl)
2786 bool inword = false;
2788 //lyxerr << "Words: ";
2789 pos_type n = size();
2790 for (pos_type pos = 0; pos != n; ++pos) {
2794 if (!isLetter(pos)) {
2803 CursorSlice from = sl;
2804 CursorSlice to = sl;
2807 from.text()->getWord(from, to, WHOLE_WORD);
2808 if (to.pos() - from.pos() < 6)
2810 docstring word = asString(from.pos(), to.pos(), false);
2811 d->words_.insert(word);
2812 //lyxerr << word << " ";
2814 //lyxerr << std::endl;
2818 void Paragraph::registerWords()
2820 Private::Words::const_iterator it;
2821 WordList & wl = theWordList();
2822 for (it = d->words_.begin(); it != d->words_.end(); ++it)
2827 void Paragraph::updateWords(CursorSlice const & sl)
2829 LASSERT(&sl.paragraph() == this, /**/);