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"
38 #include "OutputParams.h"
39 #include "output_latex.h"
40 #include "paragraph_funcs.h"
41 #include "ParagraphParameters.h"
46 #include "frontends/alert.h"
47 #include "frontends/FontMetrics.h"
49 #include "insets/InsetBibitem.h"
50 #include "insets/InsetLabel.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"
67 using support::contains;
68 using support::lowercase;
69 using support::prefixIs;
70 using support::suffixIs;
71 using support::rsplit;
72 using support::uppercase;
75 /// Inset identifier (above 0x10ffff, for ucs-4)
76 char_type const META_INSET = 0x200001;
79 /////////////////////////////////////////////////////////////////////
83 /////////////////////////////////////////////////////////////////////
85 class Paragraph::Private
89 Private(Paragraph * owner);
90 /// "Copy constructor"
91 Private(Private const &, Paragraph * owner);
94 void insertChar(pos_type pos, char_type c, Change const & change);
96 /// Output the surrogate pair formed by \p c and \p next to \p os.
97 /// \return the number of characters written.
98 int latexSurrogatePair(odocstream & os, char_type c, char_type next,
101 /// Output a space in appropriate formatting (or a surrogate pair
102 /// if the next character is a combining character).
103 /// \return whether a surrogate pair was output.
104 bool simpleTeXBlanks(OutputParams const &,
105 odocstream &, TexRow & texrow,
107 unsigned int & column,
109 Layout const & style);
111 /// Output consecutive unicode chars, belonging to the same script as
112 /// specified by the latex macro \p ltx, to \p os starting from \p c.
113 /// \return the number of characters written.
114 int writeScriptChars(odocstream & os, char_type c, docstring const & ltx,
115 Change &, Encoding const &, pos_type &);
117 /// This could go to ParagraphParameters if we want to.
118 int startTeXParParams(BufferParams const &, odocstream &, TexRow &,
121 /// This could go to ParagraphParameters if we want to.
122 int endTeXParParams(BufferParams const &, odocstream &, TexRow &,
126 void latexInset(Buffer const &, BufferParams const &,
128 TexRow & texrow, OutputParams &,
131 Font const & outerfont,
133 Change & running_change,
134 Layout const & style,
136 unsigned int & column);
139 void latexSpecialChar(
141 OutputParams & runparams,
143 Change & running_change,
144 Layout const & style,
146 unsigned int & column);
153 unsigned int & column);
155 bool latexSpecialTypewriter(
159 unsigned int & column);
161 bool latexSpecialPhrase(
164 unsigned int & column);
167 void validate(LaTeXFeatures & features,
168 Layout const & layout) const;
170 /// Checks if the paragraph contains only text and no inset or font change.
171 bool onlyText(Buffer const & buf, Font const & outerfont,
172 pos_type initial) const;
174 /// match a string against a particular point in the paragraph
175 bool isTextAt(std::string const & str, pos_type pos) const;
177 /// Which Paragraph owns us?
181 Inset * inset_owner_;
189 static unsigned int paragraph_id;
191 ParagraphParameters params_;
193 /// for recording and looking up changes
197 InsetList insetlist_;
203 pos_type begin_of_body_;
205 typedef docstring TextContainer;
214 using std::upper_bound;
215 using std::lower_bound;
219 // Initialization of the counter for the paragraph id's,
220 unsigned int Paragraph::Private::paragraph_id = 0;
224 struct special_phrase {
230 special_phrase const special_phrases[] = {
231 { "LyX", from_ascii("\\protect\\LyX{}"), false },
232 { "TeX", from_ascii("\\protect\\TeX{}"), true },
233 { "LaTeX2e", from_ascii("\\protect\\LaTeXe{}"), true },
234 { "LaTeX", from_ascii("\\protect\\LaTeX{}"), true },
237 size_t const phrases_nr = sizeof(special_phrases)/sizeof(special_phrase);
242 Paragraph::Private::Private(Paragraph * owner)
243 : owner_(owner), inset_owner_(0), begin_of_body_(0)
245 id_ = paragraph_id++;
250 Paragraph::Private::Private(Private const & p, Paragraph * owner)
251 : owner_(owner), inset_owner_(p.inset_owner_), fontlist_(p.fontlist_),
252 params_(p.params_), changes_(p.changes_), insetlist_(p.insetlist_),
253 layout_(p.layout_), begin_of_body_(p.begin_of_body_), text_(p.text_)
255 id_ = paragraph_id++;
259 bool Paragraph::isChanged(pos_type start, pos_type end) const
261 BOOST_ASSERT(start >= 0 && start <= size());
262 BOOST_ASSERT(end > start && end <= size() + 1);
264 return d->changes_.isChanged(start, end);
268 bool Paragraph::isMergedOnEndOfParDeletion(bool trackChanges) const {
269 // keep the logic here in sync with the logic of eraseChars()
275 Change change = d->changes_.lookup(size());
277 return change.type == Change::INSERTED && change.author == 0;
281 void Paragraph::setChange(Change const & change)
283 // beware of the imaginary end-of-par character!
284 d->changes_.set(change, 0, size() + 1);
287 * Propagate the change recursively - but not in case of DELETED!
289 * Imagine that your co-author makes changes in an existing inset. He
290 * sends your document to you and you come to the conclusion that the
291 * inset should go completely. If you erase it, LyX must not delete all
292 * text within the inset. Otherwise, the change tracked insertions of
293 * your co-author get lost and there is no way to restore them later.
295 * Conclusion: An inset's content should remain untouched if you delete it
298 if (change.type != Change::DELETED) {
299 for (pos_type pos = 0; pos < size(); ++pos) {
301 getInset(pos)->setChange(change);
307 void Paragraph::setChange(pos_type pos, Change const & change)
309 BOOST_ASSERT(pos >= 0 && pos <= size());
311 d->changes_.set(change, pos);
313 // see comment in setChange(Change const &) above
315 if (change.type != Change::DELETED &&
316 pos < size() && isInset(pos)) {
317 getInset(pos)->setChange(change);
322 Change const & Paragraph::lookupChange(pos_type pos) const
324 BOOST_ASSERT(pos >= 0 && pos <= size());
326 return d->changes_.lookup(pos);
330 void Paragraph::acceptChanges(BufferParams const & bparams, pos_type start,
333 BOOST_ASSERT(start >= 0 && start <= size());
334 BOOST_ASSERT(end > start && end <= size() + 1);
336 for (pos_type pos = start; pos < end; ++pos) {
337 switch (lookupChange(pos).type) {
338 case Change::UNCHANGED:
339 // accept changes in nested inset
340 if (pos < size() && isInset(pos))
341 getInset(pos)->acceptChanges(bparams);
345 case Change::INSERTED:
346 d->changes_.set(Change(Change::UNCHANGED), pos);
347 // also accept changes in nested inset
348 if (pos < size() && isInset(pos)) {
349 getInset(pos)->acceptChanges(bparams);
353 case Change::DELETED:
354 // Suppress access to non-existent
355 // "end-of-paragraph char"
357 eraseChar(pos, false);
368 void Paragraph::rejectChanges(BufferParams const & bparams,
369 pos_type start, pos_type end)
371 BOOST_ASSERT(start >= 0 && start <= size());
372 BOOST_ASSERT(end > start && end <= size() + 1);
374 for (pos_type pos = start; pos < end; ++pos) {
375 switch (lookupChange(pos).type) {
376 case Change::UNCHANGED:
377 // reject changes in nested inset
378 if (pos < size() && isInset(pos)) {
379 getInset(pos)->rejectChanges(bparams);
383 case Change::INSERTED:
384 // Suppress access to non-existent
385 // "end-of-paragraph char"
387 eraseChar(pos, false);
393 case Change::DELETED:
394 d->changes_.set(Change(Change::UNCHANGED), pos);
396 // Do NOT reject changes within a deleted inset!
397 // There may be insertions of a co-author inside of it!
405 void Paragraph::Private::insertChar(pos_type pos, char_type c,
406 Change const & change)
408 BOOST_ASSERT(pos >= 0 && pos <= int(text_.size()));
411 changes_.insert(change, pos);
413 // This is actually very common when parsing buffers (and
414 // maybe inserting ascii text)
415 if (pos == text_.size()) {
416 // when appending characters, no need to update tables
421 text_.insert(text_.begin() + pos, c);
423 // Update the font table.
424 fontlist_.increasePosAfterPos(pos);
427 insetlist_.increasePosAfterPos(pos);
431 void Paragraph::insertInset(pos_type pos, Inset * inset,
432 Change const & change)
435 BOOST_ASSERT(pos >= 0 && pos <= size());
437 d->insertChar(pos, META_INSET, change);
438 BOOST_ASSERT(d->text_[pos] == META_INSET);
440 // Add a new entry in the insetlist_.
441 d->insetlist_.insert(inset, pos);
445 bool Paragraph::eraseChar(pos_type pos, bool trackChanges)
447 BOOST_ASSERT(pos >= 0 && pos <= size());
449 // keep the logic here in sync with the logic of isMergedOnEndOfParDeletion()
452 Change change = d->changes_.lookup(pos);
454 // set the character to DELETED if
455 // a) it was previously unchanged or
456 // b) it was inserted by a co-author
458 if (change.type == Change::UNCHANGED ||
459 (change.type == Change::INSERTED && change.author != 0)) {
460 setChange(pos, Change(Change::DELETED));
464 if (change.type == Change::DELETED)
468 // Don't physically access the imaginary end-of-paragraph character.
469 // eraseChar() can only mark it as DELETED. A physical deletion of
470 // end-of-par must be handled externally.
476 d->changes_.erase(pos);
478 // if it is an inset, delete the inset entry
479 if (d->text_[pos] == META_INSET)
480 d->insetlist_.erase(pos);
482 d->text_.erase(d->text_.begin() + pos);
484 // Update the fontlist_
485 d->fontlist_.erase(pos);
487 // Update the insetlist_
488 d->insetlist_.decreasePosAfterPos(pos);
494 int Paragraph::eraseChars(pos_type start, pos_type end, bool trackChanges)
496 BOOST_ASSERT(start >= 0 && start <= size());
497 BOOST_ASSERT(end >= start && end <= size() + 1);
500 for (pos_type count = end - start; count; --count) {
501 if (!eraseChar(i, trackChanges))
508 int Paragraph::Private::latexSurrogatePair(odocstream & os, char_type c,
509 char_type next, Encoding const & encoding)
511 // Writing next here may circumvent a possible font change between
512 // c and next. Since next is only output if it forms a surrogate pair
513 // with c we can ignore this:
514 // A font change inside a surrogate pair does not make sense and is
515 // hopefully impossible to input.
516 // FIXME: change tracking
517 // Is this correct WRT change tracking?
518 docstring const latex1 = encoding.latexChar(next);
519 docstring const latex2 = encoding.latexChar(c);
520 os << latex1 << '{' << latex2 << '}';
521 return latex1.length() + latex2.length() + 2;
525 bool Paragraph::Private::simpleTeXBlanks(OutputParams const & runparams,
526 odocstream & os, TexRow & texrow,
528 unsigned int & column,
530 Layout const & style)
532 if (style.pass_thru || runparams.verbatim)
535 if (i + 1 < int(text_.size())) {
536 char_type next = text_[i + 1];
537 if (Encodings::isCombiningChar(next)) {
538 Encoding const & encoding = *(runparams.encoding);
539 // This space has an accent, so we must always output it.
540 column += latexSurrogatePair(os, ' ', next, encoding) - 1;
545 if (lyxrc.plaintext_linelen > 0
546 && column > lyxrc.plaintext_linelen
548 && text_[i - 1] != ' '
549 && (i + 1 < int(text_.size()))
550 // same in FreeSpacing mode
551 && !owner_->isFreeSpacing()
552 // In typewriter mode, we want to avoid
553 // ! . ? : at the end of a line
554 && !(font.fontInfo().family() == TYPEWRITER_FAMILY
555 && (text_[i - 1] == '.'
556 || text_[i - 1] == '?'
557 || text_[i - 1] == ':'
558 || text_[i - 1] == '!'))) {
561 texrow.start(owner_->id(), i + 1);
563 } else if (style.free_spacing) {
572 int Paragraph::Private::writeScriptChars(odocstream & os,
574 docstring const & ltx,
575 Change & runningChange,
576 Encoding const & encoding,
579 // We only arrive here when a proper language for character c has not
580 // been specified (i.e., it could not be translated in the current
581 // latex encoding) and it belongs to a known script.
582 // Parameter ltx contains the latex translation of c as specified in
583 // the unicodesymbols file and is something like "\textXXX{<spec>}".
584 // The latex macro name "textXXX" specifies the script to which c
585 // belongs and we use it in order to check whether characters from the
586 // same script immediately follow, such that we can collect them in a
587 // single "\textXXX" macro. So, we have to retain "\textXXX{<spec>"
588 // for the first char but only "<spec>" for all subsequent chars.
589 docstring::size_type const brace1 = ltx.find_first_of(from_ascii("{"));
590 docstring::size_type const brace2 = ltx.find_last_of(from_ascii("}"));
591 string script = to_ascii(ltx.substr(1, brace1 - 1));
592 int length = ltx.substr(0, brace2).length();
593 os << ltx.substr(0, brace2);
594 int size = text_.size();
595 while (i + 1 < size) {
596 char_type const next = text_[i + 1];
597 // Stop here if next character belongs to another script
598 // or there is a change in change tracking status.
599 if (!Encodings::isKnownScriptChar(next, script) ||
600 runningChange != owner_->lookupChange(i + 1))
604 FontList::const_iterator cit = fontlist_.begin();
605 FontList::const_iterator end = fontlist_.end();
606 for (; cit != end; ++cit) {
607 if (cit->pos() >= i && !found) {
608 prev_font = cit->font();
611 if (cit->pos() >= i + 1)
614 // Stop here if there is a font attribute or encoding change.
615 if (found && cit != end && prev_font != cit->font())
617 docstring const latex = encoding.latexChar(next);
618 docstring::size_type const b1 =
619 latex.find_first_of(from_ascii("{"));
620 docstring::size_type const b2 =
621 latex.find_last_of(from_ascii("}"));
622 int const len = b2 - b1 - 1;
623 os << latex.substr(b1 + 1, len);
633 bool Paragraph::Private::isTextAt(string const & str, pos_type pos) const
635 pos_type const len = str.length();
637 // is the paragraph large enough?
638 if (pos + len > int(text_.size()))
641 // does the wanted text start at point?
642 for (string::size_type i = 0; i < str.length(); ++i) {
643 // Caution: direct comparison of characters works only
644 // because str is pure ASCII.
645 if (str[i] != text_[pos + i])
649 return fontlist_.hasChangeInRange(pos, len);
653 void Paragraph::Private::latexInset(Buffer const & buf,
654 BufferParams const & bparams,
657 OutputParams & runparams,
660 Font const & outerfont,
662 Change & running_change,
663 Layout const & style,
665 unsigned int & column)
667 Inset * inset = owner_->getInset(i);
670 if (style.pass_thru) {
671 inset->plaintext(buf, os, runparams);
675 // FIXME: move this to InsetNewline::latex
676 if (inset->lyxCode() == NEWLINE_CODE) {
677 // newlines are handled differently here than
678 // the default in simpleTeXSpecialChars().
679 if (!style.newline_allowed) {
683 column += running_font.latexWriteEndChanges(
684 os, bparams, runparams,
689 if (running_font.fontInfo().family() == TYPEWRITER_FAMILY)
692 basefont = owner_->getLayoutFont(bparams, outerfont);
693 running_font = basefont;
695 if (runparams.moving_arg)
701 texrow.start(owner_->id(), i + 1);
705 if (owner_->lookupChange(i).type == Change::DELETED) {
706 if( ++runparams.inDeletedInset == 1)
707 runparams.changeOfDeletedInset = owner_->lookupChange(i);
710 if (inset->canTrackChanges()) {
711 column += Changes::latexMarkChange(os, bparams, running_change,
712 Change(Change::UNCHANGED));
713 running_change = Change(Change::UNCHANGED);
717 odocstream::pos_type const len = os.tellp();
719 if ((inset->lyxCode() == GRAPHICS_CODE
720 || inset->lyxCode() == MATH_CODE
721 || inset->lyxCode() == HYPERLINK_CODE)
722 && running_font.isRightToLeft()) {
723 if (running_font.language()->lang() == "farsi")
730 // FIXME: Bug: we can have an empty font change here!
731 // if there has just been a font change, we are going to close it
732 // right now, which means stupid latex code like \textsf{}. AFAIK,
733 // this does not harm dvi output. A minor bug, thus (JMarc)
735 // Some insets cannot be inside a font change command.
736 // However, even such insets *can* be placed in \L or \R
737 // or their equivalents (for RTL language switches), so we don't
738 // close the language in those cases.
739 // ArabTeX, though, cannot handle this special behavior, it seems.
740 bool arabtex = basefont.language()->lang() == "arabic_arabtex"
741 || running_font.language()->lang() == "arabic_arabtex";
742 if (open_font && inset->noFontChange()) {
743 bool closeLanguage = arabtex
744 || basefont.isRightToLeft() == running_font.isRightToLeft();
745 unsigned int count = running_font.latexWriteEndChanges(os,
746 bparams, runparams, basefont, basefont, closeLanguage);
748 // if any font properties were closed, update the running_font,
749 // making sure, however, to leave the language as it was
751 // FIXME: probably a better way to keep track of the old
752 // language, than copying the entire font?
753 Font const copy_font(running_font);
754 basefont = owner_->getLayoutFont(bparams, outerfont);
755 running_font = basefont;
757 running_font.setLanguage(copy_font.language());
758 // leave font open if language is still open
759 open_font = (running_font.language() == basefont.language());
761 runparams.local_font = &basefont;
765 int tmp = inset->latex(buf, os, runparams);
768 if (running_font.language()->lang() == "farsi")
775 for (int j = 0; j < tmp; ++j)
778 texrow.start(owner_->id(), i + 1);
781 column += os.tellp() - len;
784 if (owner_->lookupChange(i).type == Change::DELETED)
785 --runparams.inDeletedInset;
789 void Paragraph::Private::latexSpecialChar(
791 OutputParams & runparams,
793 Change & running_change,
794 Layout const & style,
796 unsigned int & column)
798 char_type const c = text_[i];
800 if (style.pass_thru) {
802 // FIXME UNICODE: This can fail if c cannot
803 // be encoded in the current encoding.
808 if (runparams.verbatim) {
813 if (lyxrc.fontenc == "T1" && latexSpecialT1(c, os, i, column))
816 if (running_font.fontInfo().family() == TYPEWRITER_FAMILY
817 && latexSpecialTypewriter(c, os, i, column))
820 // Otherwise, we use what LaTeX provides us.
823 os << "\\textbackslash{}";
827 os << "\\textless{}";
831 os << "\\textgreater{}";
842 os << "\\char`\\\"{}";
847 case '%': case '#': case '{':
855 os << "\\textasciitilde{}";
860 os << "\\textasciicircum{}";
865 // avoid being mistaken for optional arguments
873 // Blanks are printed before font switching.
874 // Sure? I am not! (try nice-latex)
875 // I am sure it's correct. LyX might be smarter
876 // in the future, but for now, nothing wrong is
883 if (latexSpecialPhrase(os, i, column))
889 Encoding const & encoding = *(runparams.encoding);
890 if (i + 1 < int(text_.size())) {
891 char_type next = text_[i + 1];
892 if (Encodings::isCombiningChar(next)) {
893 column += latexSurrogatePair(os, c, next, encoding) - 1;
899 docstring const latex = encoding.latexChar(c);
900 if (Encodings::isKnownScriptChar(c, script)
901 && prefixIs(latex, from_ascii("\\" + script)))
902 column += writeScriptChars(os, c, latex,
903 running_change, encoding, i) - 1;
904 else if (latex.length() > 1 && latex[latex.length() - 1] != '}') {
905 // Prevent eating of a following
906 // space or command corruption by
907 // following characters
908 column += latex.length() + 1;
911 column += latex.length() - 1;
919 bool Paragraph::Private::latexSpecialT1(char_type const c, odocstream & os,
920 pos_type & i, unsigned int & column)
926 // In T1 encoding, these characters exist
927 // but we should avoid ligatures
928 if (i + 1 > int(text_.size()) || text_[i + 1] != c)
933 //os << "\\textcompwordmark{}";
945 bool Paragraph::Private::latexSpecialTypewriter(char_type const c, odocstream & os,
946 pos_type & i, unsigned int & column)
950 if (i + 1 < int(text_.size()) && text_[i + 1] == '-') {
951 // "--" in Typewriter mode -> "-{}-"
958 // I assume this is hack treating typewriter as verbatim
959 // FIXME UNICODE: This can fail if c cannot be encoded
960 // in the current encoding.
965 // Those characters are not directly supported.
969 case '%': case '#': case '{':
978 // With Typewriter font, these characters exist.
985 bool Paragraph::Private::latexSpecialPhrase(odocstream & os, pos_type & i,
986 unsigned int & column)
988 // FIXME: if we have "LaTeX" with a font
989 // change in the middle (before the 'T', then
990 // the "TeX" part is still special cased.
991 // Really we should only operate this on
992 // "words" for some definition of word
994 for (size_t pnr = 0; pnr < phrases_nr; ++pnr) {
995 if (!isTextAt(special_phrases[pnr].phrase, i))
997 os << special_phrases[pnr].macro;
998 i += special_phrases[pnr].phrase.length() - 1;
999 column += special_phrases[pnr].macro.length() - 1;
1006 void Paragraph::Private::validate(LaTeXFeatures & features,
1007 Layout const & layout) const
1009 // check the params.
1010 if (!params_.spacing().isDefault())
1011 features.require("setspace");
1014 features.useLayout(layout.name());
1017 fontlist_.validate(features);
1019 // then the indentation
1020 if (!params_.leftIndent().zero())
1021 features.require("ParagraphLeftIndent");
1024 InsetList::const_iterator icit = insetlist_.begin();
1025 InsetList::const_iterator iend = insetlist_.end();
1026 for (; icit != iend; ++icit) {
1028 icit->inset->validate(features);
1029 if (layout.needprotect &&
1030 icit->inset->lyxCode() == FOOT_CODE)
1031 features.require("NeedLyXFootnoteCode");
1035 // then the contents
1036 for (pos_type i = 0; i < int(text_.size()) ; ++i) {
1037 for (size_t pnr = 0; pnr < phrases_nr; ++pnr) {
1038 if (!special_phrases[pnr].builtin
1039 && isTextAt(special_phrases[pnr].phrase, i)) {
1040 features.require(special_phrases[pnr].phrase);
1044 Encodings::validate(text_[i], features);
1048 /////////////////////////////////////////////////////////////////////
1052 /////////////////////////////////////////////////////////////////////
1054 Paragraph::Paragraph()
1055 : d(new Paragraph::Private(this))
1062 Paragraph::Paragraph(Paragraph const & par)
1063 : itemdepth(par.itemdepth),
1064 d(new Paragraph::Private(*par.d, this))
1069 Paragraph & Paragraph::operator=(Paragraph const & par)
1071 // needed as we will destroy the private part before copying it
1073 itemdepth = par.itemdepth;
1076 d = new Private(*par.d, this);
1082 Paragraph::~Paragraph()
1088 void Paragraph::write(Buffer const & buf, ostream & os,
1089 BufferParams const & bparams,
1090 depth_type & dth) const
1092 // The beginning or end of a deeper (i.e. nested) area?
1093 if (dth != params().depth()) {
1094 if (params().depth() > dth) {
1095 while (params().depth() > dth) {
1096 os << "\n\\begin_deeper";
1100 while (params().depth() < dth) {
1101 os << "\n\\end_deeper";
1107 // First write the layout
1108 os << "\n\\begin_layout " << to_utf8(layout()->name()) << '\n';
1112 Font font1(inherit_font, bparams.language);
1114 Change running_change = Change(Change::UNCHANGED);
1117 for (pos_type i = 0; i <= size(); ++i) {
1119 Change change = lookupChange(i);
1120 Changes::lyxMarkChange(os, column, running_change, change);
1121 running_change = change;
1126 // Write font changes
1127 Font font2 = getFontSettings(bparams, i);
1128 if (font2 != font1) {
1129 font2.lyxWriteChanges(font1, os);
1134 char_type const c = d->text_[i];
1138 Inset const * inset = getInset(i);
1140 if (inset->directWrite()) {
1141 // international char, let it write
1142 // code directly so it's shorter in
1144 inset->write(buf, os);
1148 os << "\\begin_inset ";
1149 inset->write(buf, os);
1150 os << "\n\\end_inset\n\n";
1156 os << "\n\\backslash\n";
1160 if (i + 1 < size() && d->text_[i + 1] == ' ') {
1167 if ((column > 70 && c == ' ')
1172 // this check is to amend a bug. LyX sometimes
1173 // inserts '\0' this could cause problems.
1175 std::vector<char> tmp = ucs4_to_utf8(c);
1176 tmp.push_back('\0');
1179 lyxerr << "ERROR (Paragraph::writeFile):"
1180 " NULL char in structure." << endl;
1186 os << "\n\\end_layout\n";
1190 void Paragraph::validate(LaTeXFeatures & features) const
1192 d->validate(features, *layout());
1196 void Paragraph::insert(pos_type start, docstring const & str,
1197 Font const & font, Change const & change)
1199 for (size_t i = 0, n = str.size(); i != n ; ++i)
1200 insertChar(start + i, str[i], font, change);
1204 void Paragraph::appendChar(char_type c, Font const & font,
1205 Change const & change)
1208 d->changes_.insert(change, d->text_.size());
1209 // when appending characters, no need to update tables
1210 d->text_.push_back(c);
1211 setFont(d->text_.size() - 1, font);
1215 void Paragraph::appendString(docstring const & s, Font const & font,
1216 Change const & change)
1218 size_t end = s.size();
1219 size_t oldsize = d->text_.size();
1220 size_t newsize = oldsize + end;
1221 size_t capacity = d->text_.capacity();
1222 if (newsize >= capacity)
1223 d->text_.reserve(std::max(capacity + 100, newsize));
1225 // when appending characters, no need to update tables
1228 // FIXME: Optimize this!
1229 for (pos_type i = 0; i != end; ++i) {
1231 d->changes_.insert(change, i);
1233 d->fontlist_.setRange(oldsize, newsize, font);
1237 void Paragraph::insertChar(pos_type pos, char_type c,
1240 d->insertChar(pos, c, Change(trackChanges ?
1241 Change::INSERTED : Change::UNCHANGED));
1245 void Paragraph::insertChar(pos_type pos, char_type c,
1246 Font const & font, bool trackChanges)
1248 d->insertChar(pos, c, Change(trackChanges ?
1249 Change::INSERTED : Change::UNCHANGED));
1254 void Paragraph::insertChar(pos_type pos, char_type c,
1255 Font const & font, Change const & change)
1257 d->insertChar(pos, c, change);
1262 void Paragraph::insertInset(pos_type pos, Inset * inset,
1263 Font const & font, Change const & change)
1265 insertInset(pos, inset, change);
1266 // Set the font/language of the inset...
1271 bool Paragraph::insetAllowed(InsetCode code)
1273 return !d->inset_owner_ || d->inset_owner_->insetAllowed(code);
1277 void Paragraph::resetFonts(Font const & font)
1279 d->fontlist_.setRange(0, d->text_.size(), font);
1282 // Gets uninstantiated font setting at position.
1283 Font const Paragraph::getFontSettings(BufferParams const & bparams,
1287 lyxerr << " pos: " << pos << " size: " << size() << endl;
1288 BOOST_ASSERT(pos <= size());
1291 FontList::const_iterator cit = d->fontlist_.fontIterator(pos);
1292 if (cit != d->fontlist_.end())
1295 if (pos == size() && !empty())
1296 return getFontSettings(bparams, pos - 1);
1298 return Font(inherit_font, getParLanguage(bparams));
1302 FontSpan Paragraph::fontSpan(pos_type pos) const
1304 BOOST_ASSERT(pos <= size());
1307 FontList::const_iterator cit = d->fontlist_.begin();
1308 FontList::const_iterator end = d->fontlist_.end();
1309 for (; cit != end; ++cit) {
1310 if (cit->pos() >= pos) {
1311 if (pos >= beginOfBody())
1312 return FontSpan(std::max(start, beginOfBody()),
1315 return FontSpan(start,
1316 std::min(beginOfBody() - 1,
1319 start = cit->pos() + 1;
1322 // This should not happen, but if so, we take no chances.
1323 //lyxerr << "Paragraph::getEndPosOfFontSpan: This should not happen!"
1325 return FontSpan(pos, pos);
1329 // Gets uninstantiated font setting at position 0
1330 Font const Paragraph::getFirstFontSettings(BufferParams const & bparams) const
1332 if (!empty() && !d->fontlist_.empty())
1333 return d->fontlist_.begin()->font();
1335 return Font(inherit_font, bparams.language);
1339 // Gets the fully instantiated font at a given position in a paragraph
1340 // This is basically the same function as Text::GetFont() in text2.cpp.
1341 // The difference is that this one is used for generating the LaTeX file,
1342 // and thus cosmetic "improvements" are disallowed: This has to deliver
1343 // the true picture of the buffer. (Asger)
1344 Font const Paragraph::getFont(BufferParams const & bparams, pos_type pos,
1345 Font const & outerfont) const
1347 BOOST_ASSERT(pos >= 0);
1349 Font font = getFontSettings(bparams, pos);
1351 pos_type const body_pos = beginOfBody();
1353 font.fontInfo().realize(d->layout_->labelfont);
1355 font.fontInfo().realize(d->layout_->font);
1357 font.fontInfo().realize(outerfont.fontInfo());
1358 font.fontInfo().realize(bparams.getFont().fontInfo());
1364 Font const Paragraph::getLabelFont
1365 (BufferParams const & bparams, Font const & outerfont) const
1367 Font tmpfont = layout()->labelfont;
1368 tmpfont.setLanguage(getParLanguage(bparams));
1369 tmpfont.fontInfo().realize(outerfont.fontInfo());
1370 tmpfont.fontInfo().realize(bparams.getFont().fontInfo());
1375 Font const Paragraph::getLayoutFont
1376 (BufferParams const & bparams, Font const & outerfont) const
1378 FontInfo tmpfont = layout()->font;
1379 tmpfont.realize(outerfont.fontInfo());
1380 tmpfont.realize(bparams.getFont().fontInfo());
1381 return Font(tmpfont, getParLanguage(bparams));
1385 /// Returns the height of the highest font in range
1386 FontSize Paragraph::highestFontInRange
1387 (pos_type startpos, pos_type endpos, FontSize def_size) const
1389 return d->fontlist_.highestInRange(startpos, endpos, def_size);
1394 Paragraph::getUChar(BufferParams const & bparams, pos_type pos) const
1396 char_type c = d->text_[pos];
1397 if (!lyxrc.rtl_support)
1427 if (uc != c && getFontSettings(bparams, pos).isRightToLeft())
1434 void Paragraph::setFont(pos_type pos, Font const & font)
1436 BOOST_ASSERT(pos <= size());
1438 // First, reduce font against layout/label font
1439 // Update: The setCharFont() routine in text2.cpp already
1440 // reduces font, so we don't need to do that here. (Asger)
1442 d->fontlist_.set(pos, font);
1446 void Paragraph::makeSameLayout(Paragraph const & par)
1448 layout(par.layout());
1450 d->params_ = par.params();
1454 bool Paragraph::stripLeadingSpaces(bool trackChanges)
1456 if (isFreeSpacing())
1462 while (pos < size() && (isNewline(pos) || isLineSeparator(pos))) {
1463 if (eraseChar(pos, trackChanges))
1469 return count > 0 || pos > 0;
1473 bool Paragraph::hasSameLayout(Paragraph const & par) const
1475 return par.layout() == layout() && d->params_.sameLayout(par.params());
1479 depth_type Paragraph::getDepth() const
1481 return params().depth();
1485 depth_type Paragraph::getMaxDepthAfter() const
1487 if (layout()->isEnvironment())
1488 return params().depth() + 1;
1490 return params().depth();
1494 char Paragraph::getAlign() const
1496 if (params().align() == LYX_ALIGN_LAYOUT)
1497 return layout()->align;
1499 return params().align();
1503 docstring const & Paragraph::getLabelstring() const
1505 return params().labelString();
1509 // the next two functions are for the manual labels
1510 docstring const Paragraph::getLabelWidthString() const
1512 if (layout()->margintype == MARGIN_MANUAL)
1513 return params().labelWidthString();
1515 return _("Senseless with this layout!");
1519 void Paragraph::setLabelWidthString(docstring const & s)
1521 params().labelWidthString(s);
1525 docstring const Paragraph::translateIfPossible(docstring const & s,
1526 BufferParams const & bparams) const
1528 if (!support::isAscii(s) || s.empty()) {
1529 // This must be a user defined layout. We cannot translate
1530 // this, since gettext accepts only ascii keys.
1533 // Probably standard layout, try to translate
1534 Messages & m = getMessages(getParLanguage(bparams)->code());
1535 return m.get(to_ascii(s));
1539 docstring Paragraph::expandLabel(LayoutPtr const & layout,
1540 BufferParams const & bparams, bool process_appendix) const
1542 TextClass const & tclass = bparams.getTextClass();
1545 if (process_appendix && params().appendix())
1546 fmt = translateIfPossible(layout->labelstring_appendix(),
1549 fmt = translateIfPossible(layout->labelstring(), bparams);
1551 if (fmt.empty() && layout->labeltype == LABEL_COUNTER
1552 && !layout->counter.empty())
1553 fmt = "\\the" + layout->counter;
1555 // handle 'inherited level parts' in 'fmt',
1556 // i.e. the stuff between '@' in '@Section@.\arabic{subsection}'
1557 size_t const i = fmt.find('@', 0);
1558 if (i != docstring::npos) {
1559 size_t const j = fmt.find('@', i + 1);
1560 if (j != docstring::npos) {
1561 docstring parent(fmt, i + 1, j - i - 1);
1562 docstring label = from_ascii("??");
1563 if (tclass.hasLayout(parent))
1564 docstring label = expandLabel(tclass[parent], bparams,
1566 fmt = docstring(fmt, 0, i) + label
1567 + docstring(fmt, j + 1, docstring::npos);
1571 return tclass.counters().counterLabel(fmt);
1575 void Paragraph::applyLayout(LayoutPtr const & new_layout)
1578 LyXAlignment const oldAlign = params().align();
1580 if (!(oldAlign & layout()->alignpossible)) {
1581 frontend::Alert::warning(_("Alignment not permitted"),
1582 _("The new layout does not permit the alignment previously used.\nSetting to default."));
1583 params().align(LYX_ALIGN_LAYOUT);
1588 pos_type Paragraph::beginOfBody() const
1590 return d->begin_of_body_;
1594 void Paragraph::setBeginOfBody()
1596 if (layout()->labeltype != LABEL_MANUAL) {
1597 d->begin_of_body_ = 0;
1601 // Unroll the first two cycles of the loop
1602 // and remember the previous character to
1603 // remove unnecessary getChar() calls
1605 pos_type end = size();
1606 if (i < end && !isNewline(i)) {
1608 char_type previous_char = 0;
1611 previous_char = d->text_[i];
1612 if (!isNewline(i)) {
1614 while (i < end && previous_char != ' ') {
1619 previous_char = temp;
1625 d->begin_of_body_ = i;
1629 bool Paragraph::forceDefaultParagraphs() const
1631 return inInset() && inInset()->forceDefaultParagraphs(0);
1637 // paragraphs inside floats need different alignment tags to avoid
1640 bool noTrivlistCentering(InsetCode code)
1642 return code == FLOAT_CODE || code == WRAP_CODE;
1646 string correction(string const & orig)
1648 if (orig == "flushleft")
1649 return "raggedright";
1650 if (orig == "flushright")
1651 return "raggedleft";
1652 if (orig == "center")
1658 string const corrected_env(string const & suffix, string const & env,
1661 string output = suffix + "{";
1662 if (noTrivlistCentering(code))
1663 output += correction(env);
1667 if (suffix == "\\begin")
1673 void adjust_row_column(string const & str, TexRow & texrow, int & column)
1675 if (!contains(str, "\n"))
1676 column += str.size();
1680 column = rsplit(str, tmp, '\n').size();
1687 int Paragraph::Private::startTeXParParams(BufferParams const & bparams,
1688 odocstream & os, TexRow & texrow,
1689 bool moving_arg) const
1693 if (params_.noindent()) {
1694 os << "\\noindent ";
1698 LyXAlignment const curAlign = params_.align();
1700 if (curAlign == layout_->align)
1704 case LYX_ALIGN_NONE:
1705 case LYX_ALIGN_BLOCK:
1706 case LYX_ALIGN_LAYOUT:
1707 case LYX_ALIGN_SPECIAL:
1709 case LYX_ALIGN_LEFT:
1710 case LYX_ALIGN_RIGHT:
1711 case LYX_ALIGN_CENTER:
1720 case LYX_ALIGN_NONE:
1721 case LYX_ALIGN_BLOCK:
1722 case LYX_ALIGN_LAYOUT:
1723 case LYX_ALIGN_SPECIAL:
1725 case LYX_ALIGN_LEFT: {
1727 if (owner_->getParLanguage(bparams)->babel() != "hebrew")
1728 output = corrected_env("\\begin", "flushleft", owner_->ownerCode());
1730 output = corrected_env("\\begin", "flushright", owner_->ownerCode());
1731 os << from_ascii(output);
1732 adjust_row_column(output, texrow, column);
1734 } case LYX_ALIGN_RIGHT: {
1736 if (owner_->getParLanguage(bparams)->babel() != "hebrew")
1737 output = corrected_env("\\begin", "flushright", owner_->ownerCode());
1739 output = corrected_env("\\begin", "flushleft", owner_->ownerCode());
1740 os << from_ascii(output);
1741 adjust_row_column(output, texrow, column);
1743 } case LYX_ALIGN_CENTER: {
1745 output = corrected_env("\\begin", "center", owner_->ownerCode());
1746 os << from_ascii(output);
1747 adjust_row_column(output, texrow, column);
1756 int Paragraph::Private::endTeXParParams(BufferParams const & bparams,
1757 odocstream & os, TexRow & texrow,
1758 bool moving_arg) const
1762 switch (params_.align()) {
1763 case LYX_ALIGN_NONE:
1764 case LYX_ALIGN_BLOCK:
1765 case LYX_ALIGN_LAYOUT:
1766 case LYX_ALIGN_SPECIAL:
1768 case LYX_ALIGN_LEFT:
1769 case LYX_ALIGN_RIGHT:
1770 case LYX_ALIGN_CENTER:
1778 switch (params_.align()) {
1779 case LYX_ALIGN_NONE:
1780 case LYX_ALIGN_BLOCK:
1781 case LYX_ALIGN_LAYOUT:
1782 case LYX_ALIGN_SPECIAL:
1784 case LYX_ALIGN_LEFT: {
1786 if (owner_->getParLanguage(bparams)->babel() != "hebrew")
1787 output = corrected_env("\n\\par\\end", "flushleft", owner_->ownerCode());
1789 output = corrected_env("\n\\par\\end", "flushright", owner_->ownerCode());
1790 os << from_ascii(output);
1791 adjust_row_column(output, texrow, column);
1793 } case LYX_ALIGN_RIGHT: {
1795 if (owner_->getParLanguage(bparams)->babel() != "hebrew")
1796 output = corrected_env("\n\\par\\end", "flushright", owner_->ownerCode());
1798 output = corrected_env("\n\\par\\end", "flushleft", owner_->ownerCode());
1799 os << from_ascii(output);
1800 adjust_row_column(output, texrow, column);
1802 } case LYX_ALIGN_CENTER: {
1804 output = corrected_env("\n\\par\\end", "center", owner_->ownerCode());
1805 os << from_ascii(output);
1806 adjust_row_column(output, texrow, column);
1815 // This one spits out the text of the paragraph
1816 bool Paragraph::latex(Buffer const & buf,
1817 BufferParams const & bparams,
1818 Font const & outerfont,
1819 odocstream & os, TexRow & texrow,
1820 OutputParams const & runparams) const
1822 LYXERR(Debug::LATEX) << "SimpleTeXOnePar... " << this << endl;
1824 bool return_value = false;
1828 // well we have to check if we are in an inset with unlimited
1829 // length (all in one row) if that is true then we don't allow
1830 // any special options in the paragraph and also we don't allow
1831 // any environment other than the default layout of the text class
1833 bool asdefault = forceDefaultParagraphs();
1836 style = bparams.getTextClass().defaultLayout();
1841 // Current base font for all inherited font changes, without any
1842 // change caused by an individual character, except for the language:
1843 // It is set to the language of the first character.
1844 // As long as we are in the label, this font is the base font of the
1845 // label. Before the first body character it is set to the base font
1849 // Maybe we have to create a optional argument.
1850 pos_type body_pos = beginOfBody();
1851 unsigned int column = 0;
1854 // the optional argument is kept in curly brackets in
1855 // case it contains a ']'
1858 basefont = getLabelFont(bparams, outerfont);
1860 basefont = getLayoutFont(bparams, outerfont);
1863 // Which font is currently active?
1864 Font running_font(basefont);
1865 // Do we have an open font change?
1866 bool open_font = false;
1868 Change runningChange = Change(Change::UNCHANGED);
1870 texrow.start(id(), 0);
1872 // if the paragraph is empty, the loop will not be entered at all
1874 if (style->isCommand()) {
1879 column += d->startTeXParParams(bparams, os, texrow,
1880 runparams.moving_arg);
1883 for (pos_type i = 0; i < size(); ++i) {
1884 // First char in paragraph or after label?
1885 if (i == body_pos) {
1888 column += running_font.latexWriteEndChanges(
1889 os, bparams, runparams,
1890 basefont, basefont);
1893 basefont = getLayoutFont(bparams, outerfont);
1894 running_font = basefont;
1896 column += Changes::latexMarkChange(os, bparams,
1897 runningChange, Change(Change::UNCHANGED));
1898 runningChange = Change(Change::UNCHANGED);
1903 if (style->isCommand()) {
1909 column += d->startTeXParParams(bparams, os,
1911 runparams.moving_arg);
1914 Change const & change = runparams.inDeletedInset ? runparams.changeOfDeletedInset
1917 if (bparams.outputChanges && runningChange != change) {
1919 column += running_font.latexWriteEndChanges(
1920 os, bparams, runparams, basefont, basefont);
1923 basefont = getLayoutFont(bparams, outerfont);
1924 running_font = basefont;
1926 column += Changes::latexMarkChange(os, bparams, runningChange, change);
1927 runningChange = change;
1930 // do not output text which is marked deleted
1931 // if change tracking output is disabled
1932 if (!bparams.outputChanges && change.type == Change::DELETED) {
1938 // Fully instantiated font
1939 Font const font = getFont(bparams, i, outerfont);
1941 Font const last_font = running_font;
1943 // Do we need to close the previous font?
1945 (font != running_font ||
1946 font.language() != running_font.language()))
1948 column += running_font.latexWriteEndChanges(
1949 os, bparams, runparams, basefont,
1950 (i == body_pos-1) ? basefont : font);
1951 running_font = basefont;
1955 // Switch file encoding if necessary (and allowed)
1956 if (!runparams.verbatim &&
1957 runparams.encoding->package() == Encoding::inputenc &&
1958 font.language()->encoding()->package() == Encoding::inputenc) {
1959 std::pair<bool, int> const enc_switch = switchEncoding(os, bparams,
1960 runparams.moving_arg, *(runparams.encoding),
1961 *(font.language()->encoding()));
1962 if (enc_switch.first) {
1963 column += enc_switch.second;
1964 runparams.encoding = font.language()->encoding();
1968 char_type const c = d->text_[i];
1970 // Do we need to change font?
1971 if ((font != running_font ||
1972 font.language() != running_font.language()) &&
1975 odocstringstream ods;
1976 column += font.latexWriteStartChanges(ods, bparams,
1977 runparams, basefont,
1979 running_font = font;
1981 docstring fontchange = ods.str();
1982 // check if the fontchange ends with a trailing blank
1983 // (like "\small " (see bug 3382)
1984 if (suffixIs(fontchange, ' ') && c == ' ')
1985 os << fontchange.substr(0, fontchange.size() - 1)
1986 << from_ascii("{}");
1992 // FIXME: integrate this case in latexSpecialChar
1993 // Do not print the separation of the optional argument
1994 // if style->pass_thru is false. This works because
1995 // latexSpecialChar ignores spaces if
1996 // style->pass_thru is false.
1997 if (i != body_pos - 1) {
1998 if (d->simpleTeXBlanks(
1999 runparams, os, texrow,
2000 i, column, font, *style)) {
2001 // A surrogate pair was output. We
2002 // must not call latexSpecialChar
2003 // in this iteration, since it would output
2004 // the combining character again.
2011 OutputParams rp = runparams;
2012 rp.free_spacing = style->free_spacing;
2013 rp.local_font = &font;
2014 rp.intitle = style->intitle;
2016 // Two major modes: LaTeX or plain
2017 // Handle here those cases common to both modes
2018 // and then split to handle the two modes separately.
2019 if (c == META_INSET)
2020 d->latexInset(buf, bparams, os,
2021 texrow, rp, running_font,
2022 basefont, outerfont, open_font,
2023 runningChange, *style, i, column);
2025 d->latexSpecialChar(os, rp, running_font, runningChange,
2028 // Set the encoding to that returned from simpleTeXSpecialChars (see
2029 // comment for encoding member in OutputParams.h)
2030 runparams.encoding = rp.encoding;
2033 // If we have an open font definition, we have to close it
2035 #ifdef FIXED_LANGUAGE_END_DETECTION
2038 .latexWriteEndChanges(os, bparams, runparams,
2040 next_->getFont(bparams, 0, outerfont));
2042 running_font.latexWriteEndChanges(os, bparams,
2043 runparams, basefont, basefont);
2046 //FIXME: For now we ALWAYS have to close the foreign font settings if they are
2047 //FIXME: there as we start another \selectlanguage with the next paragraph if
2048 //FIXME: we are in need of this. This should be fixed sometime (Jug)
2049 running_font.latexWriteEndChanges(os, bparams, runparams,
2050 basefont, basefont);
2054 column += Changes::latexMarkChange(os, bparams, runningChange, Change(Change::UNCHANGED));
2056 // Needed if there is an optional argument but no contents.
2057 if (body_pos > 0 && body_pos == size()) {
2059 return_value = false;
2063 column += d->endTeXParParams(bparams, os, texrow,
2064 runparams.moving_arg);
2067 LYXERR(Debug::LATEX) << "SimpleTeXOnePar...done " << this << endl;
2068 return return_value;
2072 bool Paragraph::emptyTag() const
2074 for (pos_type i = 0; i < size(); ++i) {
2076 Inset const * inset = getInset(i);
2077 InsetCode lyx_code = inset->lyxCode();
2078 if (lyx_code != TOC_CODE &&
2079 lyx_code != INCLUDE_CODE &&
2080 lyx_code != GRAPHICS_CODE &&
2081 lyx_code != ERT_CODE &&
2082 lyx_code != LISTINGS_CODE &&
2083 lyx_code != FLOAT_CODE &&
2084 lyx_code != TABULAR_CODE) {
2088 char_type c = d->text_[i];
2089 if (c != ' ' && c != '\t')
2097 string Paragraph::getID(Buffer const & buf, OutputParams const & runparams) const
2099 for (pos_type i = 0; i < size(); ++i) {
2101 Inset const * inset = getInset(i);
2102 InsetCode lyx_code = inset->lyxCode();
2103 if (lyx_code == LABEL_CODE) {
2104 InsetLabel const * const il = static_cast<InsetLabel const *>(inset);
2105 docstring const & id = il->getParam("name");
2106 return "id='" + to_utf8(sgml::cleanID(buf, runparams, id)) + "'";
2115 pos_type Paragraph::getFirstWord(Buffer const & buf, odocstream & os, OutputParams const & runparams) const
2118 for (i = 0; i < size(); ++i) {
2120 Inset const * inset = getInset(i);
2121 inset->docbook(buf, os, runparams);
2123 char_type c = d->text_[i];
2126 os << sgml::escapeChar(c);
2133 bool Paragraph::Private::onlyText(Buffer const & buf, Font const & outerfont, pos_type initial) const
2136 pos_type size = text_.size();
2137 for (pos_type i = initial; i < size; ++i) {
2138 Font font = owner_->getFont(buf.params(), i, outerfont);
2139 if (text_[i] == META_INSET)
2141 if (i != initial && font != font_old)
2150 void Paragraph::simpleDocBookOnePar(Buffer const & buf,
2152 OutputParams const & runparams,
2153 Font const & outerfont,
2154 pos_type initial) const
2156 bool emph_flag = false;
2158 LayoutPtr const & style = layout();
2160 style->labeltype == LABEL_MANUAL ? style->labelfont : style->font;
2162 if (style->pass_thru && !d->onlyText(buf, outerfont, initial))
2165 // parsing main loop
2166 for (pos_type i = initial; i < size(); ++i) {
2167 Font font = getFont(buf.params(), i, outerfont);
2169 // handle <emphasis> tag
2170 if (font_old.fontInfo().emph() != font.fontInfo().emph()) {
2171 if (font.fontInfo().emph() == FONT_ON) {
2174 } else if (i != initial) {
2175 os << "</emphasis>";
2181 Inset const * inset = getInset(i);
2182 inset->docbook(buf, os, runparams);
2184 char_type c = d->text_[i];
2186 if (style->pass_thru)
2189 os << sgml::escapeChar(c);
2195 os << "</emphasis>";
2198 if (style->free_spacing)
2200 if (style->pass_thru && !d->onlyText(buf, outerfont, initial))
2205 bool Paragraph::isHfill(pos_type pos) const
2208 && getInset(pos)->lyxCode() == HFILL_CODE;
2212 bool Paragraph::isNewline(pos_type pos) const
2215 && getInset(pos)->lyxCode() == NEWLINE_CODE;
2219 bool Paragraph::isLineSeparator(pos_type pos) const
2221 char_type const c = d->text_[pos];
2222 return isLineSeparatorChar(c)
2223 || (c == META_INSET && getInset(pos) &&
2224 getInset(pos)->isLineSeparator());
2228 /// Used by the spellchecker
2229 bool Paragraph::isLetter(pos_type pos) const
2232 return getInset(pos)->isLetter();
2234 char_type const c = d->text_[pos];
2235 return isLetterChar(c) || isDigit(c);
2241 Paragraph::getParLanguage(BufferParams const & bparams) const
2244 return getFirstFontSettings(bparams).language();
2245 // FIXME: we should check the prev par as well (Lgb)
2246 return bparams.language;
2250 bool Paragraph::isRTL(BufferParams const & bparams) const
2252 return lyxrc.rtl_support
2253 && getParLanguage(bparams)->rightToLeft()
2254 && ownerCode() != ERT_CODE
2255 && ownerCode() != LISTINGS_CODE;
2259 void Paragraph::changeLanguage(BufferParams const & bparams,
2260 Language const * from, Language const * to)
2262 // change language including dummy font change at the end
2263 for (pos_type i = 0; i <= size(); ++i) {
2264 Font font = getFontSettings(bparams, i);
2265 if (font.language() == from) {
2266 font.setLanguage(to);
2273 bool Paragraph::isMultiLingual(BufferParams const & bparams) const
2275 Language const * doc_language = bparams.language;
2276 FontList::const_iterator cit = d->fontlist_.begin();
2277 FontList::const_iterator end = d->fontlist_.end();
2279 for (; cit != end; ++cit)
2280 if (cit->font().language() != ignore_language &&
2281 cit->font().language() != latex_language &&
2282 cit->font().language() != doc_language)
2288 // Convert the paragraph to a string.
2289 // Used for building the table of contents
2290 docstring const Paragraph::asString(Buffer const & buffer, bool label) const
2292 return asString(buffer, 0, size(), label);
2296 docstring const Paragraph::asString(Buffer const & buffer,
2297 pos_type beg, pos_type end, bool label) const
2300 odocstringstream os;
2302 if (beg == 0 && label && !params().labelString().empty())
2303 os << params().labelString() << ' ';
2305 for (pos_type i = beg; i < end; ++i) {
2306 char_type const c = d->text_[i];
2309 else if (c == META_INSET)
2310 getInset(i)->textString(buffer, os);
2317 void Paragraph::setInsetOwner(Inset * inset)
2319 d->inset_owner_ = inset;
2323 int Paragraph::id() const
2329 LayoutPtr const & Paragraph::layout() const
2335 void Paragraph::layout(LayoutPtr const & new_layout)
2337 d->layout_ = new_layout;
2341 Inset * Paragraph::inInset() const
2343 return d->inset_owner_;
2347 InsetCode Paragraph::ownerCode() const
2349 return d->inset_owner_ ?
2350 d->inset_owner_->lyxCode() : NO_CODE;
2354 ParagraphParameters & Paragraph::params()
2360 ParagraphParameters const & Paragraph::params() const
2366 bool Paragraph::isFreeSpacing() const
2368 if (layout()->free_spacing)
2370 return d->inset_owner_ && d->inset_owner_->isFreeSpacing();
2374 bool Paragraph::allowEmpty() const
2376 if (layout()->keepempty)
2378 return d->inset_owner_ && d->inset_owner_->allowEmpty();
2382 char_type Paragraph::transformChar(char_type c, pos_type pos) const
2384 if (!Encodings::is_arabic(c))
2387 char_type prev_char = ' ';
2388 char_type next_char = ' ';
2390 for (pos_type i = pos - 1; i >= 0; --i) {
2391 char_type const par_char = d->text_[i];
2392 if (!Encodings::isComposeChar_arabic(par_char)) {
2393 prev_char = par_char;
2398 for (pos_type i = pos + 1, end = size(); i < end; ++i) {
2399 char_type const par_char = d->text_[i];
2400 if (!Encodings::isComposeChar_arabic(par_char)) {
2401 next_char = par_char;
2406 if (Encodings::is_arabic(next_char)) {
2407 if (Encodings::is_arabic(prev_char) &&
2408 !Encodings::is_arabic_special(prev_char))
2409 return Encodings::transformChar(c, Encodings::FORM_MEDIAL);
2411 return Encodings::transformChar(c, Encodings::FORM_INITIAL);
2413 if (Encodings::is_arabic(prev_char) &&
2414 !Encodings::is_arabic_special(prev_char))
2415 return Encodings::transformChar(c, Encodings::FORM_FINAL);
2417 return Encodings::transformChar(c, Encodings::FORM_ISOLATED);
2422 int Paragraph::checkBiblio(bool track_changes)
2425 //This is getting more and more a mess. ...We really should clean
2426 //up this bibitem issue for 1.6. See also bug 2743.
2428 // Add bibitem insets if necessary
2429 if (layout()->labeltype != LABEL_BIBLIO)
2432 bool hasbibitem = !d->insetlist_.empty()
2433 // Insist on it being in pos 0
2434 && d->text_[0] == META_INSET
2435 && d->insetlist_.begin()->inset->lyxCode() == BIBITEM_CODE;
2440 // remove a bibitem in pos != 0
2441 // restore it later in pos 0 if necessary
2442 // (e.g. if a user inserts contents _before_ the item)
2443 // we're assuming there's only one of these, which there
2445 int erasedInsetPosition = -1;
2446 InsetList::iterator it = d->insetlist_.begin();
2447 InsetList::iterator end = d->insetlist_.end();
2448 for (; it != end; ++it)
2449 if (it->inset->lyxCode() == BIBITEM_CODE
2451 InsetBibitem * olditem = static_cast<InsetBibitem *>(it->inset);
2452 oldkey = olditem->getParam("key");
2453 oldlabel = olditem->getParam("label");
2454 erasedInsetPosition = it->pos;
2455 eraseChar(erasedInsetPosition, track_changes);
2459 //There was an InsetBibitem at the beginning, and we didn't
2460 //have to erase one.
2461 if (hasbibitem && erasedInsetPosition < 0)
2464 //There was an InsetBibitem at the beginning and we did have to
2465 //erase one. So we give its properties to the beginning inset.
2467 InsetBibitem * inset =
2468 static_cast<InsetBibitem *>(d->insetlist_.begin()->inset);
2469 if (!oldkey.empty())
2470 inset->setParam("key", oldkey);
2471 inset->setParam("label", oldlabel);
2472 return -erasedInsetPosition;
2475 //There was no inset at the beginning, so we need to create one with
2476 //the key and label of the one we erased.
2477 InsetBibitem * inset(new InsetBibitem(InsetCommandParams(BIBITEM_CODE)));
2478 // restore values of previously deleted item in this par.
2479 if (!oldkey.empty())
2480 inset->setParam("key", oldkey);
2481 inset->setParam("label", oldlabel);
2482 insertInset(0, static_cast<Inset *>(inset),
2483 Change(track_changes ? Change::INSERTED : Change::UNCHANGED));
2489 void Paragraph::checkAuthors(AuthorList const & authorList)
2491 d->changes_.checkAuthors(authorList);
2495 bool Paragraph::isUnchanged(pos_type pos) const
2497 return lookupChange(pos).type == Change::UNCHANGED;
2501 bool Paragraph::isInserted(pos_type pos) const
2503 return lookupChange(pos).type == Change::INSERTED;
2507 bool Paragraph::isDeleted(pos_type pos) const
2509 return lookupChange(pos).type == Change::DELETED;
2513 InsetList const & Paragraph::insetList() const
2515 return d->insetlist_;
2519 Inset * Paragraph::releaseInset(pos_type pos)
2521 Inset * inset = d->insetlist_.release(pos);
2522 /// does not honour change tracking!
2523 eraseChar(pos, false);
2528 Inset * Paragraph::getInset(pos_type pos)
2530 return d->insetlist_.get(pos);
2534 Inset const * Paragraph::getInset(pos_type pos) const
2536 return d->insetlist_.get(pos);
2540 void Paragraph::changeCase(BufferParams const & bparams, pos_type pos,
2541 pos_type right, TextCase action)
2543 // process sequences of modified characters; in change
2544 // tracking mode, this approach results in much better
2545 // usability than changing case on a char-by-char basis
2548 bool const trackChanges = bparams.trackChanges;
2550 bool capitalize = true;
2552 for (; pos < right; ++pos) {
2553 char_type oldChar = d->text_[pos];
2554 char_type newChar = oldChar;
2556 // ignore insets and don't play with deleted text!
2557 if (oldChar != META_INSET && !isDeleted(pos)) {
2559 case text_lowercase:
2560 newChar = lowercase(oldChar);
2562 case text_capitalization:
2564 newChar = uppercase(oldChar);
2568 case text_uppercase:
2569 newChar = uppercase(oldChar);
2574 if (!isLetter(pos) || isDeleted(pos)) {
2575 // permit capitalization again
2579 if (oldChar != newChar)
2582 if (oldChar == newChar || pos == right - 1) {
2583 if (oldChar != newChar) {
2584 // step behind the changing area
2587 int erasePos = pos - changes.size();
2588 for (size_t i = 0; i < changes.size(); i++) {
2589 insertChar(pos, changes[i],
2590 getFontSettings(bparams,
2593 if (!eraseChar(erasePos, trackChanges)) {
2596 ++right; // expand selection
2605 bool Paragraph::find(docstring const & str, bool cs, bool mw,
2606 pos_type pos, bool del) const
2608 int const strsize = str.length();
2610 pos_type const parsize = d->text_.size();
2611 for (i = 0; pos + i < parsize; ++i) {
2614 if (cs && str[i] != d->text_[pos + i])
2616 if (!cs && uppercase(str[i]) != uppercase(d->text_[pos + i]))
2618 if (!del && isDeleted(pos + i))
2625 // if necessary, check whether string matches word
2627 if (pos > 0 && isLetter(pos - 1))
2629 if (pos + strsize < parsize
2630 && isLetter(pos + strsize))
2638 char_type Paragraph::getChar(pos_type pos) const
2640 return d->text_[pos];
2644 pos_type Paragraph::size() const
2646 return d->text_.size();
2650 bool Paragraph::empty() const
2652 return d->text_.empty();
2656 bool Paragraph::isInset(pos_type pos) const
2658 return d->text_[pos] == META_INSET;
2662 bool Paragraph::isSeparator(pos_type pos) const
2664 //FIXME: Are we sure this can be the only separator?
2665 return d->text_[pos] == ' ';