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);
88 void insertChar(pos_type pos, char_type c, Change const & change);
90 /// Output the surrogate pair formed by \p c and \p next to \p os.
91 /// \return the number of characters written.
92 int latexSurrogatePair(odocstream & os, char_type c, char_type next,
95 /// Output a space in appropriate formatting (or a surrogate pair
96 /// if the next character is a combining character).
97 /// \return whether a surrogate pair was output.
98 bool simpleTeXBlanks(OutputParams const &,
99 odocstream &, TexRow & texrow,
101 unsigned int & column,
103 Layout const & style);
105 /// Output consecutive unicode chars, belonging to the same script as
106 /// specified by the latex macro \p ltx, to \p os starting from \p i.
107 /// \return the number of characters written.
108 int writeScriptChars(odocstream & os, docstring const & ltx,
109 Change &, Encoding const &, pos_type & i);
111 /// This could go to ParagraphParameters if we want to.
112 int startTeXParParams(BufferParams const &, odocstream &, TexRow &,
115 /// This could go to ParagraphParameters if we want to.
116 int endTeXParParams(BufferParams const &, odocstream &, TexRow &,
120 void latexInset(BufferParams const &,
122 TexRow & texrow, OutputParams &,
125 Font const & outerfont,
127 Change & running_change,
128 Layout const & style,
130 unsigned int & column);
133 void latexSpecialChar(
135 OutputParams & runparams,
137 Change & running_change,
138 Layout const & style,
140 unsigned int & column);
147 unsigned int & column);
149 bool latexSpecialTypewriter(
153 unsigned int & column);
155 bool latexSpecialPhrase(
158 unsigned int & column,
159 OutputParams & runparams);
162 void validate(LaTeXFeatures & features,
163 Layout const & layout) const;
165 /// Checks if the paragraph contains only text and no inset or font change.
166 bool onlyText(Buffer const & buf, Font const & outerfont,
167 pos_type initial) const;
169 /// match a string against a particular point in the paragraph
170 bool isTextAt(string const & str, pos_type pos) const;
172 /// Which Paragraph owns us?
176 Inset * inset_owner_;
184 static unsigned int paragraph_id;
186 ParagraphParameters params_;
188 /// for recording and looking up changes
192 InsetList insetlist_;
195 pos_type begin_of_body_;
197 typedef docstring TextContainer;
201 typedef std::set<docstring> Words;
205 Layout const * layout_;
209 // Initialization of the counter for the paragraph id's,
210 unsigned int Paragraph::Private::paragraph_id = 0;
214 struct special_phrase {
220 special_phrase const special_phrases[] = {
221 { "LyX", from_ascii("\\LyX{}"), false },
222 { "TeX", from_ascii("\\TeX{}"), true },
223 { "LaTeX2e", from_ascii("\\LaTeXe{}"), true },
224 { "LaTeX", from_ascii("\\LaTeX{}"), true },
227 size_t const phrases_nr = sizeof(special_phrases)/sizeof(special_phrase);
232 Paragraph::Private::Private(Paragraph * owner, Layout const & layout)
233 : owner_(owner), inset_owner_(0), begin_of_body_(0), layout_(&layout)
235 id_ = paragraph_id++;
240 Paragraph::Private::Private(Private const & p, Paragraph * owner)
241 : owner_(owner), inset_owner_(p.inset_owner_), fontlist_(p.fontlist_),
242 params_(p.params_), changes_(p.changes_), insetlist_(p.insetlist_),
243 begin_of_body_(p.begin_of_body_), text_(p.text_), words_(p.words_),
246 id_ = paragraph_id++;
250 bool Paragraph::isChanged(pos_type start, pos_type end) const
252 LASSERT(start >= 0 && start <= size(), /**/);
253 LASSERT(end > start && end <= size() + 1, /**/);
255 return d->changes_.isChanged(start, end);
259 bool Paragraph::isMergedOnEndOfParDeletion(bool trackChanges) const
261 // keep the logic here in sync with the logic of eraseChars()
265 Change const change = d->changes_.lookup(size());
266 return change.type == Change::INSERTED && change.author == 0;
270 void Paragraph::setChange(Change const & change)
272 // beware of the imaginary end-of-par character!
273 d->changes_.set(change, 0, size() + 1);
276 * Propagate the change recursively - but not in case of DELETED!
278 * Imagine that your co-author makes changes in an existing inset. He
279 * sends your document to you and you come to the conclusion that the
280 * inset should go completely. If you erase it, LyX must not delete all
281 * text within the inset. Otherwise, the change tracked insertions of
282 * your co-author get lost and there is no way to restore them later.
284 * Conclusion: An inset's content should remain untouched if you delete it
287 if (change.type != Change::DELETED) {
288 for (pos_type pos = 0; pos < size(); ++pos) {
289 if (Inset * inset = getInset(pos))
290 inset->setChange(change);
296 void Paragraph::setChange(pos_type pos, Change const & change)
298 LASSERT(pos >= 0 && pos <= size(), /**/);
299 d->changes_.set(change, pos);
301 // see comment in setChange(Change const &) above
302 if (change.type != Change::DELETED && pos < size())
303 if (Inset * inset = getInset(pos))
304 inset->setChange(change);
308 Change const & Paragraph::lookupChange(pos_type pos) const
310 LASSERT(pos >= 0 && pos <= size(), /**/);
311 return d->changes_.lookup(pos);
315 void Paragraph::acceptChanges(BufferParams const & bparams, pos_type start,
318 LASSERT(start >= 0 && start <= size(), /**/);
319 LASSERT(end > start && end <= size() + 1, /**/);
321 for (pos_type pos = start; pos < end; ++pos) {
322 switch (lookupChange(pos).type) {
323 case Change::UNCHANGED:
324 // accept changes in nested inset
325 if (Inset * inset = getInset(pos))
326 inset->acceptChanges(bparams);
329 case Change::INSERTED:
330 d->changes_.set(Change(Change::UNCHANGED), pos);
331 // also accept changes in nested inset
332 if (Inset * inset = getInset(pos))
333 inset->acceptChanges(bparams);
336 case Change::DELETED:
337 // Suppress access to non-existent
338 // "end-of-paragraph char"
340 eraseChar(pos, false);
351 void Paragraph::rejectChanges(BufferParams const & bparams,
352 pos_type start, pos_type end)
354 LASSERT(start >= 0 && start <= size(), /**/);
355 LASSERT(end > start && end <= size() + 1, /**/);
357 for (pos_type pos = start; pos < end; ++pos) {
358 switch (lookupChange(pos).type) {
359 case Change::UNCHANGED:
360 // reject changes in nested inset
361 if (Inset * inset = getInset(pos))
362 inset->rejectChanges(bparams);
365 case Change::INSERTED:
366 // Suppress access to non-existent
367 // "end-of-paragraph char"
369 eraseChar(pos, false);
375 case Change::DELETED:
376 d->changes_.set(Change(Change::UNCHANGED), pos);
378 // Do NOT reject changes within a deleted inset!
379 // There may be insertions of a co-author inside of it!
387 void Paragraph::Private::insertChar(pos_type pos, char_type c,
388 Change const & change)
390 LASSERT(pos >= 0 && pos <= int(text_.size()), /**/);
393 changes_.insert(change, pos);
395 // This is actually very common when parsing buffers (and
396 // maybe inserting ascii text)
397 if (pos == pos_type(text_.size())) {
398 // when appending characters, no need to update tables
403 text_.insert(text_.begin() + pos, c);
405 // Update the font table.
406 fontlist_.increasePosAfterPos(pos);
409 insetlist_.increasePosAfterPos(pos);
413 void Paragraph::insertInset(pos_type pos, Inset * inset,
414 Change const & change)
416 LASSERT(inset, /**/);
417 LASSERT(pos >= 0 && pos <= size(), /**/);
419 d->insertChar(pos, META_INSET, change);
420 LASSERT(d->text_[pos] == META_INSET, /**/);
422 // Add a new entry in the insetlist_.
423 d->insetlist_.insert(inset, pos);
427 bool Paragraph::eraseChar(pos_type pos, bool trackChanges)
429 LASSERT(pos >= 0 && pos <= size(), /**/);
431 // keep the logic here in sync with the logic of isMergedOnEndOfParDeletion()
434 Change change = d->changes_.lookup(pos);
436 // set the character to DELETED if
437 // a) it was previously unchanged or
438 // b) it was inserted by a co-author
440 if (change.type == Change::UNCHANGED ||
441 (change.type == Change::INSERTED && change.author != 0)) {
442 setChange(pos, Change(Change::DELETED));
446 if (change.type == Change::DELETED)
450 // Don't physically access the imaginary end-of-paragraph character.
451 // eraseChar() can only mark it as DELETED. A physical deletion of
452 // end-of-par must be handled externally.
458 d->changes_.erase(pos);
460 // if it is an inset, delete the inset entry
461 if (d->text_[pos] == META_INSET)
462 d->insetlist_.erase(pos);
464 d->text_.erase(d->text_.begin() + pos);
466 // Update the fontlist_
467 d->fontlist_.erase(pos);
469 // Update the insetlist_
470 d->insetlist_.decreasePosAfterPos(pos);
476 int Paragraph::eraseChars(pos_type start, pos_type end, bool trackChanges)
478 LASSERT(start >= 0 && start <= size(), /**/);
479 LASSERT(end >= start && end <= size() + 1, /**/);
482 for (pos_type count = end - start; count; --count) {
483 if (!eraseChar(i, trackChanges))
490 int Paragraph::Private::latexSurrogatePair(odocstream & os, char_type c,
491 char_type next, Encoding const & encoding)
493 // Writing next here may circumvent a possible font change between
494 // c and next. Since next is only output if it forms a surrogate pair
495 // with c we can ignore this:
496 // A font change inside a surrogate pair does not make sense and is
497 // hopefully impossible to input.
498 // FIXME: change tracking
499 // Is this correct WRT change tracking?
500 docstring const latex1 = encoding.latexChar(next);
501 docstring const latex2 = encoding.latexChar(c);
502 os << latex1 << '{' << latex2 << '}';
503 return latex1.length() + latex2.length() + 2;
507 bool Paragraph::Private::simpleTeXBlanks(OutputParams const & runparams,
508 odocstream & os, TexRow & texrow,
510 unsigned int & column,
512 Layout const & style)
514 if (style.pass_thru || runparams.verbatim)
517 if (i + 1 < int(text_.size())) {
518 char_type next = text_[i + 1];
519 if (Encodings::isCombiningChar(next)) {
520 Encoding const & encoding = *(runparams.encoding);
521 // This space has an accent, so we must always output it.
522 column += latexSurrogatePair(os, ' ', next, encoding) - 1;
527 if (lyxrc.plaintext_linelen > 0
528 && column > lyxrc.plaintext_linelen
530 && text_[i - 1] != ' '
531 && (i + 1 < int(text_.size()))
532 // same in FreeSpacing mode
533 && !owner_->isFreeSpacing()
534 // In typewriter mode, we want to avoid
535 // ! . ? : at the end of a line
536 && !(font.fontInfo().family() == TYPEWRITER_FAMILY
537 && (text_[i - 1] == '.'
538 || text_[i - 1] == '?'
539 || text_[i - 1] == ':'
540 || text_[i - 1] == '!'))) {
543 texrow.start(owner_->id(), i + 1);
545 } else if (style.free_spacing) {
554 int Paragraph::Private::writeScriptChars(odocstream & os,
555 docstring const & ltx,
556 Change & runningChange,
557 Encoding const & encoding,
560 // FIXME: modifying i here is not very nice...
562 // We only arrive here when a proper language for character text_[i] has
563 // not been specified (i.e., it could not be translated in the current
564 // latex encoding) and it belongs to a known script.
565 // Parameter ltx contains the latex translation of text_[i] as specified in
566 // the unicodesymbols file and is something like "\textXXX{<spec>}".
567 // The latex macro name "textXXX" specifies the script to which text_[i]
568 // belongs and we use it in order to check whether characters from the
569 // same script immediately follow, such that we can collect them in a
570 // single "\textXXX" macro. So, we have to retain "\textXXX{<spec>"
571 // for the first char but only "<spec>" for all subsequent chars.
572 docstring::size_type const brace1 = ltx.find_first_of(from_ascii("{"));
573 docstring::size_type const brace2 = ltx.find_last_of(from_ascii("}"));
574 string script = to_ascii(ltx.substr(1, brace1 - 1));
575 int length = ltx.substr(0, brace2).length();
576 os << ltx.substr(0, brace2);
577 int size = text_.size();
578 while (i + 1 < size) {
579 char_type const next = text_[i + 1];
580 // Stop here if next character belongs to another script
581 // or there is a change in change tracking status.
582 if (!Encodings::isKnownScriptChar(next, script) ||
583 runningChange != owner_->lookupChange(i + 1))
587 FontList::const_iterator cit = fontlist_.begin();
588 FontList::const_iterator end = fontlist_.end();
589 for (; cit != end; ++cit) {
590 if (cit->pos() >= i && !found) {
591 prev_font = cit->font();
594 if (cit->pos() >= i + 1)
597 // Stop here if there is a font attribute or encoding change.
598 if (found && cit != end && prev_font != cit->font())
600 docstring const latex = encoding.latexChar(next);
601 docstring::size_type const b1 =
602 latex.find_first_of(from_ascii("{"));
603 docstring::size_type const b2 =
604 latex.find_last_of(from_ascii("}"));
605 int const len = b2 - b1 - 1;
606 os << latex.substr(b1 + 1, len);
616 bool Paragraph::Private::isTextAt(string const & str, pos_type pos) const
618 pos_type const len = str.length();
620 // is the paragraph large enough?
621 if (pos + len > int(text_.size()))
624 // does the wanted text start at point?
625 for (string::size_type i = 0; i < str.length(); ++i) {
626 // Caution: direct comparison of characters works only
627 // because str is pure ASCII.
628 if (str[i] != text_[pos + i])
632 return fontlist_.hasChangeInRange(pos, len);
636 void Paragraph::Private::latexInset(
637 BufferParams const & bparams,
640 OutputParams & runparams,
643 Font const & outerfont,
645 Change & running_change,
646 Layout const & style,
648 unsigned int & column)
650 Inset * inset = owner_->getInset(i);
651 LASSERT(inset, /**/);
653 if (style.pass_thru) {
654 inset->plaintext(os, runparams);
658 // FIXME: move this to InsetNewline::latex
659 if (inset->lyxCode() == NEWLINE_CODE) {
660 // newlines are handled differently here than
661 // the default in simpleTeXSpecialChars().
662 if (!style.newline_allowed) {
666 column += running_font.latexWriteEndChanges(
667 os, bparams, runparams,
672 if (running_font.fontInfo().family() == TYPEWRITER_FAMILY)
675 basefont = owner_->getLayoutFont(bparams, outerfont);
676 running_font = basefont;
678 if (runparams.moving_arg)
683 texrow.start(owner_->id(), i + 1);
687 if (owner_->lookupChange(i).type == Change::DELETED) {
688 if( ++runparams.inDeletedInset == 1)
689 runparams.changeOfDeletedInset = owner_->lookupChange(i);
692 if (inset->canTrackChanges()) {
693 column += Changes::latexMarkChange(os, bparams, running_change,
694 Change(Change::UNCHANGED));
695 running_change = Change(Change::UNCHANGED);
699 odocstream::pos_type const len = os.tellp();
701 if (inset->forceLTR()
702 && running_font.isRightToLeft()
703 // ERT is an exception, it should be output with no decorations at all
704 && inset->lyxCode() != ERT_CODE) {
705 if (running_font.language()->lang() == "farsi")
712 // FIXME: Bug: we can have an empty font change here!
713 // if there has just been a font change, we are going to close it
714 // right now, which means stupid latex code like \textsf{}. AFAIK,
715 // this does not harm dvi output. A minor bug, thus (JMarc)
717 // Some insets cannot be inside a font change command.
718 // However, even such insets *can* be placed in \L or \R
719 // or their equivalents (for RTL language switches), so we don't
720 // close the language in those cases.
721 // ArabTeX, though, cannot handle this special behavior, it seems.
722 bool arabtex = basefont.language()->lang() == "arabic_arabtex"
723 || running_font.language()->lang() == "arabic_arabtex";
724 if (open_font && inset->noFontChange()) {
725 bool closeLanguage = arabtex
726 || basefont.isRightToLeft() == running_font.isRightToLeft();
727 unsigned int count = running_font.latexWriteEndChanges(os,
728 bparams, runparams, basefont, basefont, closeLanguage);
730 // if any font properties were closed, update the running_font,
731 // making sure, however, to leave the language as it was
733 // FIXME: probably a better way to keep track of the old
734 // language, than copying the entire font?
735 Font const copy_font(running_font);
736 basefont = owner_->getLayoutFont(bparams, outerfont);
737 running_font = basefont;
739 running_font.setLanguage(copy_font.language());
740 // leave font open if language is still open
741 open_font = (running_font.language() == basefont.language());
743 runparams.local_font = &basefont;
750 tmp = inset->latex(os, runparams);
751 } catch (EncodingException & e) {
752 // add location information and throw again.
759 if (running_font.language()->lang() == "farsi")
766 for (int j = 0; j < tmp; ++j)
769 texrow.start(owner_->id(), i + 1);
772 column += os.tellp() - len;
775 if (owner_->lookupChange(i).type == Change::DELETED)
776 --runparams.inDeletedInset;
780 void Paragraph::Private::latexSpecialChar(
782 OutputParams & runparams,
784 Change & running_change,
785 Layout const & style,
787 unsigned int & column)
789 char_type const c = text_[i];
791 if (style.pass_thru) {
793 // FIXME UNICODE: This can fail if c cannot
794 // be encoded in the current encoding.
799 if (runparams.verbatim) {
804 if (lyxrc.fontenc == "T1" && latexSpecialT1(c, os, i, column))
807 if (running_font.fontInfo().family() == TYPEWRITER_FAMILY
808 && latexSpecialTypewriter(c, os, i, column))
811 // Otherwise, we use what LaTeX provides us.
814 os << "\\textbackslash{}";
818 os << "\\textless{}";
822 os << "\\textgreater{}";
833 os << "\\char`\\\"{}";
838 case '%': case '#': case '{':
846 os << "\\textasciitilde{}";
851 os << "\\textasciicircum{}";
856 // avoid being mistaken for optional arguments
864 // Blanks are printed before font switching.
865 // Sure? I am not! (try nice-latex)
866 // I am sure it's correct. LyX might be smarter
867 // in the future, but for now, nothing wrong is
874 if (latexSpecialPhrase(os, i, column, runparams))
880 Encoding const & encoding = *(runparams.encoding);
881 if (i + 1 < int(text_.size())) {
882 char_type next = text_[i + 1];
883 if (Encodings::isCombiningChar(next)) {
884 column += latexSurrogatePair(os, c, next, encoding) - 1;
890 docstring const latex = encoding.latexChar(c);
891 if (Encodings::isKnownScriptChar(c, script)
892 && prefixIs(latex, from_ascii("\\" + script)))
893 column += writeScriptChars(os, latex,
894 running_change, encoding, i) - 1;
895 else if (latex.length() > 1 && latex[latex.length() - 1] != '}') {
896 // Prevent eating of a following
897 // space or command corruption by
898 // following characters
899 column += latex.length() + 1;
902 column += latex.length() - 1;
910 bool Paragraph::Private::latexSpecialT1(char_type const c, odocstream & os,
911 pos_type & i, unsigned int & column)
917 // In T1 encoding, these characters exist
918 // but we should avoid ligatures
919 if (i + 1 >= int(text_.size()) || text_[i + 1] != c)
924 //os << "\\textcompwordmark{}";
936 bool Paragraph::Private::latexSpecialTypewriter(char_type const c, odocstream & os,
937 pos_type & i, unsigned int & column)
941 if (i + 1 < int(text_.size()) && text_[i + 1] == '-') {
942 // "--" in Typewriter mode -> "-{}-"
949 // I assume this is hack treating typewriter as verbatim
950 // FIXME UNICODE: This can fail if c cannot be encoded
951 // in the current encoding.
956 // Those characters are not directly supported.
960 case '%': case '#': case '{':
969 // With Typewriter font, these characters exist.
976 bool Paragraph::Private::latexSpecialPhrase(odocstream & os, pos_type & i,
977 unsigned int & column, OutputParams & runparams)
979 // FIXME: if we have "LaTeX" with a font
980 // change in the middle (before the 'T', then
981 // the "TeX" part is still special cased.
982 // Really we should only operate this on
983 // "words" for some definition of word
985 for (size_t pnr = 0; pnr < phrases_nr; ++pnr) {
986 if (!isTextAt(special_phrases[pnr].phrase, i))
988 if (runparams.moving_arg)
990 os << special_phrases[pnr].macro;
991 i += special_phrases[pnr].phrase.length() - 1;
992 column += special_phrases[pnr].macro.length() - 1;
999 void Paragraph::Private::validate(LaTeXFeatures & features,
1000 Layout const & layout) const
1002 // check the params.
1003 if (!params_.spacing().isDefault())
1004 features.require("setspace");
1007 features.useLayout(layout.name());
1010 fontlist_.validate(features);
1012 // then the indentation
1013 if (!params_.leftIndent().zero())
1014 features.require("ParagraphLeftIndent");
1017 InsetList::const_iterator icit = insetlist_.begin();
1018 InsetList::const_iterator iend = insetlist_.end();
1019 for (; icit != iend; ++icit) {
1021 icit->inset->validate(features);
1022 if (layout.needprotect &&
1023 icit->inset->lyxCode() == FOOT_CODE)
1024 features.require("NeedLyXFootnoteCode");
1028 // then the contents
1029 for (pos_type i = 0; i < int(text_.size()) ; ++i) {
1030 for (size_t pnr = 0; pnr < phrases_nr; ++pnr) {
1031 if (!special_phrases[pnr].builtin
1032 && isTextAt(special_phrases[pnr].phrase, i)) {
1033 features.require(special_phrases[pnr].phrase);
1037 Encodings::validate(text_[i], features);
1041 /////////////////////////////////////////////////////////////////////
1045 /////////////////////////////////////////////////////////////////////
1048 Layout const emptyParagraphLayout;
1051 Paragraph::Paragraph()
1052 : d(new Paragraph::Private(this, emptyParagraphLayout))
1059 Paragraph::Paragraph(Paragraph const & par)
1060 : itemdepth(par.itemdepth),
1061 d(new Paragraph::Private(*par.d, this))
1067 Paragraph & Paragraph::operator=(Paragraph const & par)
1069 // needed as we will destroy the private part before copying it
1071 itemdepth = par.itemdepth;
1075 d = new Private(*par.d, this);
1082 Paragraph::~Paragraph()
1089 void Paragraph::write(ostream & os, BufferParams const & bparams,
1090 depth_type & dth) const
1092 // The beginning or end of a deeper (i.e. nested) area?
1093 if (dth != d->params_.depth()) {
1094 if (d->params_.depth() > dth) {
1095 while (d->params_.depth() > dth) {
1096 os << "\n\\begin_deeper";
1100 while (d->params_.depth() < dth) {
1101 os << "\n\\end_deeper";
1107 // First write the layout
1108 os << "\n\\begin_layout " << to_utf8(d->layout_->name()) << '\n';
1110 d->params_.write(os);
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];
1137 if (Inset const * inset = getInset(i)) {
1138 if (inset->directWrite()) {
1139 // international char, let it write
1140 // code directly so it's shorter in
1146 os << "\\begin_inset ";
1148 os << "\n\\end_inset\n\n";
1154 os << "\n\\backslash\n";
1158 if (i + 1 < size() && d->text_[i + 1] == ' ') {
1165 if ((column > 70 && c == ' ')
1170 // this check is to amend a bug. LyX sometimes
1171 // inserts '\0' this could cause problems.
1173 os << to_utf8(docstring(1, c));
1175 LYXERR0("NUL char in structure.");
1181 os << "\n\\end_layout\n";
1185 void Paragraph::validate(LaTeXFeatures & features) const
1187 d->validate(features, *d->layout_);
1191 void Paragraph::insert(pos_type start, docstring const & str,
1192 Font const & font, Change const & change)
1194 for (size_t i = 0, n = str.size(); i != n ; ++i)
1195 insertChar(start + i, str[i], font, change);
1199 void Paragraph::appendChar(char_type c, Font const & font,
1200 Change const & change)
1203 d->changes_.insert(change, d->text_.size());
1204 // when appending characters, no need to update tables
1205 d->text_.push_back(c);
1206 setFont(d->text_.size() - 1, font);
1210 void Paragraph::appendString(docstring const & s, Font const & font,
1211 Change const & change)
1213 pos_type end = s.size();
1214 size_t oldsize = d->text_.size();
1215 size_t newsize = oldsize + end;
1216 size_t capacity = d->text_.capacity();
1217 if (newsize >= capacity)
1218 d->text_.reserve(max(capacity + 100, newsize));
1220 // when appending characters, no need to update tables
1223 // FIXME: Optimize this!
1224 for (pos_type i = oldsize; i != newsize; ++i) {
1226 d->changes_.insert(change, i);
1228 d->fontlist_.set(oldsize, font);
1229 d->fontlist_.set(newsize - 1, font);
1233 void Paragraph::insertChar(pos_type pos, char_type c,
1236 d->insertChar(pos, c, Change(trackChanges ?
1237 Change::INSERTED : Change::UNCHANGED));
1241 void Paragraph::insertChar(pos_type pos, char_type c,
1242 Font const & font, bool trackChanges)
1244 d->insertChar(pos, c, Change(trackChanges ?
1245 Change::INSERTED : Change::UNCHANGED));
1250 void Paragraph::insertChar(pos_type pos, char_type c,
1251 Font const & font, Change const & change)
1253 d->insertChar(pos, c, change);
1258 void Paragraph::insertInset(pos_type pos, Inset * inset,
1259 Font const & font, Change const & change)
1261 insertInset(pos, inset, change);
1262 // Set the font/language of the inset...
1267 bool Paragraph::insetAllowed(InsetCode code)
1269 return !d->inset_owner_ || d->inset_owner_->insetAllowed(code);
1273 void Paragraph::resetFonts(Font const & font)
1275 d->fontlist_.clear();
1276 d->fontlist_.set(0, font);
1277 d->fontlist_.set(d->text_.size() - 1, font);
1280 // Gets uninstantiated font setting at position.
1281 Font const Paragraph::getFontSettings(BufferParams const & bparams,
1285 LYXERR0("pos: " << pos << " size: " << size());
1286 LASSERT(pos <= size(), /**/);
1289 FontList::const_iterator cit = d->fontlist_.fontIterator(pos);
1290 if (cit != d->fontlist_.end())
1293 if (pos == size() && !empty())
1294 return getFontSettings(bparams, pos - 1);
1296 return Font(inherit_font, getParLanguage(bparams));
1300 FontSpan Paragraph::fontSpan(pos_type pos) const
1302 LASSERT(pos <= size(), /**/);
1305 FontList::const_iterator cit = d->fontlist_.begin();
1306 FontList::const_iterator end = d->fontlist_.end();
1307 for (; cit != end; ++cit) {
1308 if (cit->pos() >= pos) {
1309 if (pos >= beginOfBody())
1310 return FontSpan(max(start, beginOfBody()),
1313 return FontSpan(start,
1314 min(beginOfBody() - 1,
1317 start = cit->pos() + 1;
1320 // This should not happen, but if so, we take no chances.
1321 // LYXERR0("Paragraph::getEndPosOfFontSpan: This should not happen!");
1322 return FontSpan(pos, pos);
1326 // Gets uninstantiated font setting at position 0
1327 Font const Paragraph::getFirstFontSettings(BufferParams const & bparams) const
1329 if (!empty() && !d->fontlist_.empty())
1330 return d->fontlist_.begin()->font();
1332 return Font(inherit_font, bparams.language);
1336 // Gets the fully instantiated font at a given position in a paragraph
1337 // This is basically the same function as Text::GetFont() in text2.cpp.
1338 // The difference is that this one is used for generating the LaTeX file,
1339 // and thus cosmetic "improvements" are disallowed: This has to deliver
1340 // the true picture of the buffer. (Asger)
1341 Font const Paragraph::getFont(BufferParams const & bparams, pos_type pos,
1342 Font const & outerfont) const
1344 LASSERT(pos >= 0, /**/);
1346 Font font = getFontSettings(bparams, pos);
1348 pos_type const body_pos = beginOfBody();
1350 font.fontInfo().realize(d->layout_->labelfont);
1352 font.fontInfo().realize(d->layout_->font);
1354 font.fontInfo().realize(outerfont.fontInfo());
1355 font.fontInfo().realize(bparams.getFont().fontInfo());
1361 Font const Paragraph::getLabelFont
1362 (BufferParams const & bparams, Font const & outerfont) const
1364 FontInfo tmpfont = d->layout_->labelfont;
1365 tmpfont.realize(outerfont.fontInfo());
1366 tmpfont.realize(bparams.getFont().fontInfo());
1367 return Font(tmpfont, getParLanguage(bparams));
1371 Font const Paragraph::getLayoutFont
1372 (BufferParams const & bparams, Font const & outerfont) const
1374 FontInfo tmpfont = d->layout_->font;
1375 tmpfont.realize(outerfont.fontInfo());
1376 tmpfont.realize(bparams.getFont().fontInfo());
1377 return Font(tmpfont, getParLanguage(bparams));
1381 /// Returns the height of the highest font in range
1382 FontSize Paragraph::highestFontInRange
1383 (pos_type startpos, pos_type endpos, FontSize def_size) const
1385 return d->fontlist_.highestInRange(startpos, endpos, def_size);
1389 char_type Paragraph::getUChar(BufferParams const & bparams, pos_type pos) const
1391 char_type c = d->text_[pos];
1392 if (!lyxrc.rtl_support)
1422 if (uc != c && getFontSettings(bparams, pos).isRightToLeft())
1428 void Paragraph::setFont(pos_type pos, Font const & font)
1430 LASSERT(pos <= size(), /**/);
1432 // First, reduce font against layout/label font
1433 // Update: The setCharFont() routine in text2.cpp already
1434 // reduces font, so we don't need to do that here. (Asger)
1436 d->fontlist_.set(pos, font);
1440 void Paragraph::makeSameLayout(Paragraph const & par)
1442 d->layout_ = par.d->layout_;
1443 d->params_ = par.d->params_;
1447 bool Paragraph::stripLeadingSpaces(bool trackChanges)
1449 if (isFreeSpacing())
1455 while (pos < size() && (isNewline(pos) || isLineSeparator(pos))) {
1456 if (eraseChar(pos, trackChanges))
1462 return count > 0 || pos > 0;
1466 bool Paragraph::hasSameLayout(Paragraph const & par) const
1468 return par.d->layout_ == d->layout_
1469 && d->params_.sameLayout(par.d->params_);
1473 depth_type Paragraph::getDepth() const
1475 return d->params_.depth();
1479 depth_type Paragraph::getMaxDepthAfter() const
1481 if (d->layout_->isEnvironment())
1482 return d->params_.depth() + 1;
1484 return d->params_.depth();
1488 char Paragraph::getAlign() const
1490 if (d->params_.align() == LYX_ALIGN_LAYOUT)
1491 return d->layout_->align;
1493 return d->params_.align();
1497 docstring const & Paragraph::labelString() const
1499 return d->params_.labelString();
1503 // the next two functions are for the manual labels
1504 docstring const Paragraph::getLabelWidthString() const
1506 if (d->layout_->margintype == MARGIN_MANUAL)
1507 return d->params_.labelWidthString();
1509 return _("Senseless with this layout!");
1513 void Paragraph::setLabelWidthString(docstring const & s)
1515 d->params_.labelWidthString(s);
1519 docstring const Paragraph::translateIfPossible(docstring const & s,
1520 BufferParams const & bparams) const
1522 if (!isAscii(s) || s.empty()) {
1523 // This must be a user defined layout. We cannot translate
1524 // this, since gettext accepts only ascii keys.
1527 // Probably standard layout, try to translate
1528 Messages & m = getMessages(getParLanguage(bparams)->code());
1529 return m.get(to_ascii(s));
1533 docstring Paragraph::expandLabel(Layout const & layout,
1534 BufferParams const & bparams, bool process_appendix) const
1536 DocumentClass const & tclass = bparams.documentClass();
1539 if (process_appendix && d->params_.appendix())
1540 fmt = translateIfPossible(layout.labelstring_appendix(),
1543 fmt = translateIfPossible(layout.labelstring(), bparams);
1545 if (fmt.empty() && layout.labeltype == LABEL_COUNTER
1546 && !layout.counter.empty())
1547 fmt = "\\the" + layout.counter;
1549 // handle 'inherited level parts' in 'fmt',
1550 // i.e. the stuff between '@' in '@Section@.\arabic{subsection}'
1551 size_t const i = fmt.find('@', 0);
1552 if (i != docstring::npos) {
1553 size_t const j = fmt.find('@', i + 1);
1554 if (j != docstring::npos) {
1555 docstring parent(fmt, i + 1, j - i - 1);
1556 docstring label = from_ascii("??");
1557 if (tclass.hasLayout(parent))
1558 docstring label = expandLabel(tclass[parent], bparams,
1560 fmt = docstring(fmt, 0, i) + label
1561 + docstring(fmt, j + 1, docstring::npos);
1565 return tclass.counters().counterLabel(fmt);
1569 void Paragraph::applyLayout(Layout const & new_layout)
1571 d->layout_ = &new_layout;
1572 LyXAlignment const oldAlign = d->params_.align();
1574 if (!(oldAlign & d->layout_->alignpossible)) {
1575 frontend::Alert::warning(_("Alignment not permitted"),
1576 _("The new layout does not permit the alignment previously used.\nSetting to default."));
1577 d->params_.align(LYX_ALIGN_LAYOUT);
1582 pos_type Paragraph::beginOfBody() const
1584 return d->begin_of_body_;
1588 void Paragraph::setBeginOfBody()
1590 if (d->layout_->labeltype != LABEL_MANUAL) {
1591 d->begin_of_body_ = 0;
1595 // Unroll the first two cycles of the loop
1596 // and remember the previous character to
1597 // remove unnecessary getChar() calls
1599 pos_type end = size();
1600 if (i < end && !isNewline(i)) {
1602 char_type previous_char = 0;
1605 previous_char = d->text_[i];
1606 if (!isNewline(i)) {
1608 while (i < end && previous_char != ' ') {
1613 previous_char = temp;
1619 d->begin_of_body_ = i;
1623 bool Paragraph::forceEmptyLayout() const
1625 Inset const * const inset = inInset();
1628 return inset->forceEmptyLayout();
1632 bool Paragraph::allowParagraphCustomization() const
1634 Inset const * const inset = inInset();
1637 return inset->allowParagraphCustomization();
1641 bool Paragraph::useEmptyLayout() const
1643 Inset const * const inset = inInset();
1646 return inset->useEmptyLayout();
1652 // paragraphs inside floats need different alignment tags to avoid
1655 bool noTrivlistCentering(InsetCode code)
1657 return code == FLOAT_CODE || code == WRAP_CODE;
1661 string correction(string const & orig)
1663 if (orig == "flushleft")
1664 return "raggedright";
1665 if (orig == "flushright")
1666 return "raggedleft";
1667 if (orig == "center")
1673 string const corrected_env(string const & suffix, string const & env,
1676 string output = suffix + "{";
1677 if (noTrivlistCentering(code))
1678 output += correction(env);
1682 if (suffix == "\\begin")
1688 void adjust_row_column(string const & str, TexRow & texrow, int & column)
1690 if (!contains(str, "\n"))
1691 column += str.size();
1695 column = rsplit(str, tmp, '\n').size();
1702 int Paragraph::Private::startTeXParParams(BufferParams const & bparams,
1703 odocstream & os, TexRow & texrow,
1704 bool moving_arg) const
1708 if (params_.noindent()) {
1709 os << "\\noindent ";
1713 LyXAlignment const curAlign = params_.align();
1715 if (curAlign == layout_->align)
1719 case LYX_ALIGN_NONE:
1720 case LYX_ALIGN_BLOCK:
1721 case LYX_ALIGN_LAYOUT:
1722 case LYX_ALIGN_SPECIAL:
1724 case LYX_ALIGN_LEFT:
1725 case LYX_ALIGN_RIGHT:
1726 case LYX_ALIGN_CENTER:
1735 case LYX_ALIGN_NONE:
1736 case LYX_ALIGN_BLOCK:
1737 case LYX_ALIGN_LAYOUT:
1738 case LYX_ALIGN_SPECIAL:
1740 case LYX_ALIGN_LEFT: {
1742 if (owner_->getParLanguage(bparams)->babel() != "hebrew")
1743 output = corrected_env("\\begin", "flushleft", owner_->ownerCode());
1745 output = corrected_env("\\begin", "flushright", owner_->ownerCode());
1746 os << from_ascii(output);
1747 adjust_row_column(output, texrow, column);
1749 } case LYX_ALIGN_RIGHT: {
1751 if (owner_->getParLanguage(bparams)->babel() != "hebrew")
1752 output = corrected_env("\\begin", "flushright", owner_->ownerCode());
1754 output = corrected_env("\\begin", "flushleft", owner_->ownerCode());
1755 os << from_ascii(output);
1756 adjust_row_column(output, texrow, column);
1758 } case LYX_ALIGN_CENTER: {
1760 output = corrected_env("\\begin", "center", owner_->ownerCode());
1761 os << from_ascii(output);
1762 adjust_row_column(output, texrow, column);
1771 int Paragraph::Private::endTeXParParams(BufferParams const & bparams,
1772 odocstream & os, TexRow & texrow,
1773 bool moving_arg) const
1777 switch (params_.align()) {
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:
1784 case LYX_ALIGN_RIGHT:
1785 case LYX_ALIGN_CENTER:
1793 switch (params_.align()) {
1794 case LYX_ALIGN_NONE:
1795 case LYX_ALIGN_BLOCK:
1796 case LYX_ALIGN_LAYOUT:
1797 case LYX_ALIGN_SPECIAL:
1799 case LYX_ALIGN_LEFT: {
1801 if (owner_->getParLanguage(bparams)->babel() != "hebrew")
1802 output = corrected_env("\n\\par\\end", "flushleft", owner_->ownerCode());
1804 output = corrected_env("\n\\par\\end", "flushright", owner_->ownerCode());
1805 os << from_ascii(output);
1806 adjust_row_column(output, texrow, column);
1808 } case LYX_ALIGN_RIGHT: {
1810 if (owner_->getParLanguage(bparams)->babel() != "hebrew")
1811 output = corrected_env("\n\\par\\end", "flushright", owner_->ownerCode());
1813 output = corrected_env("\n\\par\\end", "flushleft", owner_->ownerCode());
1814 os << from_ascii(output);
1815 adjust_row_column(output, texrow, column);
1817 } case LYX_ALIGN_CENTER: {
1819 output = corrected_env("\n\\par\\end", "center", owner_->ownerCode());
1820 os << from_ascii(output);
1821 adjust_row_column(output, texrow, column);
1830 // This one spits out the text of the paragraph
1831 bool Paragraph::latex(BufferParams const & bparams,
1832 Font const & outerfont,
1833 odocstream & os, TexRow & texrow,
1834 OutputParams const & runparams) const
1836 LYXERR(Debug::LATEX, "SimpleTeXOnePar... " << this);
1838 bool return_value = false;
1840 bool asdefault = forceEmptyLayout();
1842 Layout const & style = asdefault ?
1843 bparams.documentClass().emptyLayout() :
1846 // Current base font for all inherited font changes, without any
1847 // change caused by an individual character, except for the language:
1848 // It is set to the language of the first character.
1849 // As long as we are in the label, this font is the base font of the
1850 // label. Before the first body character it is set to the base font
1854 // Maybe we have to create a optional argument.
1855 pos_type body_pos = beginOfBody();
1856 unsigned int column = 0;
1859 // the optional argument is kept in curly brackets in
1860 // case it contains a ']'
1863 basefont = getLabelFont(bparams, outerfont);
1865 basefont = getLayoutFont(bparams, outerfont);
1868 // Which font is currently active?
1869 Font running_font(basefont);
1870 // Do we have an open font change?
1871 bool open_font = false;
1873 Change runningChange = Change(Change::UNCHANGED);
1875 texrow.start(id(), 0);
1877 // if the paragraph is empty, the loop will not be entered at all
1879 if (style.isCommand()) {
1884 column += d->startTeXParParams(bparams, os, texrow,
1885 runparams.moving_arg);
1888 for (pos_type i = 0; i < size(); ++i) {
1889 // First char in paragraph or after label?
1890 if (i == body_pos) {
1893 column += running_font.latexWriteEndChanges(
1894 os, bparams, runparams,
1895 basefont, basefont);
1898 basefont = getLayoutFont(bparams, outerfont);
1899 running_font = basefont;
1901 column += Changes::latexMarkChange(os, bparams,
1902 runningChange, Change(Change::UNCHANGED));
1903 runningChange = Change(Change::UNCHANGED);
1908 if (style.isCommand()) {
1914 column += d->startTeXParParams(bparams, os,
1916 runparams.moving_arg);
1919 Change const & change = runparams.inDeletedInset ? runparams.changeOfDeletedInset
1922 if (bparams.outputChanges && runningChange != change) {
1924 column += running_font.latexWriteEndChanges(
1925 os, bparams, runparams, basefont, basefont);
1928 basefont = getLayoutFont(bparams, outerfont);
1929 running_font = basefont;
1931 column += Changes::latexMarkChange(os, bparams, runningChange, change);
1932 runningChange = change;
1935 // do not output text which is marked deleted
1936 // if change tracking output is disabled
1937 if (!bparams.outputChanges && change.type == Change::DELETED) {
1943 // Fully instantiated font
1944 Font const font = getFont(bparams, i, outerfont);
1946 Font const last_font = running_font;
1948 // Do we need to close the previous font?
1950 (font != running_font ||
1951 font.language() != running_font.language()))
1953 column += running_font.latexWriteEndChanges(
1954 os, bparams, runparams, basefont,
1955 (i == body_pos-1) ? basefont : font);
1956 running_font = basefont;
1960 // close babel's font environment before opening CJK.
1961 if (!running_font.language()->babel().empty() &&
1962 font.language()->encoding()->package() == Encoding::CJK) {
1963 string end_tag = subst(lyxrc.language_command_end,
1965 running_font.language()->babel());
1966 os << from_ascii(end_tag);
1967 column += end_tag.length();
1970 // Switch file encoding if necessary (and allowed)
1971 if (!runparams.verbatim &&
1972 runparams.encoding->package() == Encoding::none &&
1973 font.language()->encoding()->package() == Encoding::none) {
1974 pair<bool, int> const enc_switch = switchEncoding(os, bparams,
1975 runparams, *(font.language()->encoding()));
1976 if (enc_switch.first) {
1977 column += enc_switch.second;
1978 runparams.encoding = font.language()->encoding();
1982 char_type const c = d->text_[i];
1984 // Do we need to change font?
1985 if ((font != running_font ||
1986 font.language() != running_font.language()) &&
1989 odocstringstream ods;
1990 column += font.latexWriteStartChanges(ods, bparams,
1991 runparams, basefont,
1993 running_font = font;
1995 docstring fontchange = ods.str();
1996 // check if the fontchange ends with a trailing blank
1997 // (like "\small " (see bug 3382)
1998 if (suffixIs(fontchange, ' ') && c == ' ')
1999 os << fontchange.substr(0, fontchange.size() - 1)
2000 << from_ascii("{}");
2006 // FIXME: integrate this case in latexSpecialChar
2007 // Do not print the separation of the optional argument
2008 // if style.pass_thru is false. This works because
2009 // latexSpecialChar ignores spaces if
2010 // style.pass_thru is false.
2011 if (i != body_pos - 1) {
2012 if (d->simpleTeXBlanks(
2013 runparams, os, texrow,
2014 i, column, font, style)) {
2015 // A surrogate pair was output. We
2016 // must not call latexSpecialChar
2017 // in this iteration, since it would output
2018 // the combining character again.
2025 OutputParams rp = runparams;
2026 rp.free_spacing = style.free_spacing;
2027 rp.local_font = &font;
2028 rp.intitle = style.intitle;
2030 // Two major modes: LaTeX or plain
2031 // Handle here those cases common to both modes
2032 // and then split to handle the two modes separately.
2033 if (c == META_INSET)
2034 d->latexInset(bparams, os,
2035 texrow, rp, running_font,
2036 basefont, outerfont, open_font,
2037 runningChange, style, i, column);
2040 d->latexSpecialChar(os, rp, running_font, runningChange,
2042 } catch (EncodingException & e) {
2043 if (runparams.dryrun) {
2044 os << "<" << _("LyX Warning: ")
2045 << _("uncodable character") << " '";
2049 // add location information and throw again.
2057 // Set the encoding to that returned from simpleTeXSpecialChars (see
2058 // comment for encoding member in OutputParams.h)
2059 runparams.encoding = rp.encoding;
2062 // If we have an open font definition, we have to close it
2064 #ifdef FIXED_LANGUAGE_END_DETECTION
2067 .latexWriteEndChanges(os, bparams, runparams,
2069 next_->getFont(bparams, 0, outerfont));
2071 running_font.latexWriteEndChanges(os, bparams,
2072 runparams, basefont, basefont);
2075 //FIXME: For now we ALWAYS have to close the foreign font settings if they are
2076 //FIXME: there as we start another \selectlanguage with the next paragraph if
2077 //FIXME: we are in need of this. This should be fixed sometime (Jug)
2078 running_font.latexWriteEndChanges(os, bparams, runparams,
2079 basefont, basefont);
2083 column += Changes::latexMarkChange(os, bparams, runningChange, Change(Change::UNCHANGED));
2085 // Needed if there is an optional argument but no contents.
2086 if (body_pos > 0 && body_pos == size()) {
2088 return_value = false;
2092 column += d->endTeXParParams(bparams, os, texrow,
2093 runparams.moving_arg);
2096 LYXERR(Debug::LATEX, "SimpleTeXOnePar...done " << this);
2097 return return_value;
2101 bool Paragraph::emptyTag() const
2103 for (pos_type i = 0; i < size(); ++i) {
2104 if (Inset const * inset = getInset(i)) {
2105 InsetCode lyx_code = inset->lyxCode();
2106 if (lyx_code != TOC_CODE &&
2107 lyx_code != INCLUDE_CODE &&
2108 lyx_code != GRAPHICS_CODE &&
2109 lyx_code != ERT_CODE &&
2110 lyx_code != LISTINGS_CODE &&
2111 lyx_code != FLOAT_CODE &&
2112 lyx_code != TABULAR_CODE) {
2116 char_type c = d->text_[i];
2117 if (c != ' ' && c != '\t')
2125 string Paragraph::getID(Buffer const & buf, OutputParams const & runparams)
2128 for (pos_type i = 0; i < size(); ++i) {
2129 if (Inset const * inset = getInset(i)) {
2130 InsetCode lyx_code = inset->lyxCode();
2131 if (lyx_code == LABEL_CODE) {
2132 InsetLabel const * const il = static_cast<InsetLabel const *>(inset);
2133 docstring const & id = il->getParam("name");
2134 return "id='" + to_utf8(sgml::cleanID(buf, runparams, id)) + "'";
2142 pos_type Paragraph::firstWord(odocstream & os, OutputParams const & runparams)
2146 for (i = 0; i < size(); ++i) {
2147 if (Inset const * inset = getInset(i)) {
2148 inset->docbook(os, runparams);
2150 char_type c = d->text_[i];
2153 os << sgml::escapeChar(c);
2160 bool Paragraph::Private::onlyText(Buffer const & buf, Font const & outerfont, pos_type initial) const
2163 pos_type size = text_.size();
2164 for (pos_type i = initial; i < size; ++i) {
2165 Font font = owner_->getFont(buf.params(), i, outerfont);
2166 if (text_[i] == META_INSET)
2168 if (i != initial && font != font_old)
2177 void Paragraph::simpleDocBookOnePar(Buffer const & buf,
2179 OutputParams const & runparams,
2180 Font const & outerfont,
2181 pos_type initial) const
2183 bool emph_flag = false;
2185 Layout const & style = *d->layout_;
2187 style.labeltype == LABEL_MANUAL ? style.labelfont : style.font;
2189 if (style.pass_thru && !d->onlyText(buf, outerfont, initial))
2192 // parsing main loop
2193 for (pos_type i = initial; i < size(); ++i) {
2194 Font font = getFont(buf.params(), i, outerfont);
2196 // handle <emphasis> tag
2197 if (font_old.emph() != font.fontInfo().emph()) {
2198 if (font.fontInfo().emph() == FONT_ON) {
2201 } else if (i != initial) {
2202 os << "</emphasis>";
2207 if (Inset const * inset = getInset(i)) {
2208 inset->docbook(os, runparams);
2210 char_type c = d->text_[i];
2212 if (style.pass_thru)
2215 os << sgml::escapeChar(c);
2217 font_old = font.fontInfo();
2221 os << "</emphasis>";
2224 if (style.free_spacing)
2226 if (style.pass_thru && !d->onlyText(buf, outerfont, initial))
2231 bool Paragraph::isHfill(pos_type pos) const
2233 Inset const * inset = getInset(pos);
2234 return inset && (inset->lyxCode() == SPACE_CODE &&
2235 inset->isStretchableSpace());
2239 bool Paragraph::isNewline(pos_type pos) const
2241 Inset const * inset = getInset(pos);
2242 return inset && inset->lyxCode() == NEWLINE_CODE;
2246 bool Paragraph::isLineSeparator(pos_type pos) const
2248 char_type const c = d->text_[pos];
2249 if (isLineSeparatorChar(c))
2251 Inset const * inset = getInset(pos);
2252 return inset && inset->isLineSeparator();
2256 /// Used by the spellchecker
2257 bool Paragraph::isLetter(pos_type pos) const
2259 if (Inset const * inset = getInset(pos))
2260 return inset->isLetter();
2261 char_type const c = d->text_[pos];
2262 return isLetterChar(c) || isDigit(c);
2267 Paragraph::getParLanguage(BufferParams const & bparams) const
2270 return getFirstFontSettings(bparams).language();
2271 // FIXME: we should check the prev par as well (Lgb)
2272 return bparams.language;
2276 bool Paragraph::isRTL(BufferParams const & bparams) const
2278 return lyxrc.rtl_support
2279 && getParLanguage(bparams)->rightToLeft()
2280 && ownerCode() != ERT_CODE
2281 && ownerCode() != LISTINGS_CODE;
2285 void Paragraph::changeLanguage(BufferParams const & bparams,
2286 Language const * from, Language const * to)
2288 // change language including dummy font change at the end
2289 for (pos_type i = 0; i <= size(); ++i) {
2290 Font font = getFontSettings(bparams, i);
2291 if (font.language() == from) {
2292 font.setLanguage(to);
2299 bool Paragraph::isMultiLingual(BufferParams const & bparams) const
2301 Language const * doc_language = bparams.language;
2302 FontList::const_iterator cit = d->fontlist_.begin();
2303 FontList::const_iterator end = d->fontlist_.end();
2305 for (; cit != end; ++cit)
2306 if (cit->font().language() != ignore_language &&
2307 cit->font().language() != latex_language &&
2308 cit->font().language() != doc_language)
2314 docstring Paragraph::asString(int options) const
2316 return asString(0, size(), options);
2320 docstring Paragraph::asString(pos_type beg, pos_type end, int options) const
2322 odocstringstream os;
2325 && options & AS_STR_LABEL
2326 && !d->params_.labelString().empty())
2327 os << d->params_.labelString() << ' ';
2329 for (pos_type i = beg; i < end; ++i) {
2330 char_type const c = d->text_[i];
2333 else if (c == META_INSET && options & AS_STR_INSETS)
2334 getInset(i)->textString(os);
2341 void Paragraph::setInsetOwner(Inset * inset)
2343 d->inset_owner_ = inset;
2347 int Paragraph::id() const
2353 Layout const & Paragraph::layout() const
2359 void Paragraph::setLayout(Layout const & layout)
2361 d->layout_ = &layout;
2365 void Paragraph::setEmptyOrDefaultLayout(DocumentClass const & tclass)
2367 if (useEmptyLayout())
2368 setLayout(tclass.emptyLayout());
2370 setLayout(tclass.defaultLayout());
2374 Inset * Paragraph::inInset() const
2376 return d->inset_owner_;
2380 InsetCode Paragraph::ownerCode() const
2382 return d->inset_owner_ ? d->inset_owner_->lyxCode() : NO_CODE;
2386 ParagraphParameters & Paragraph::params()
2392 ParagraphParameters const & Paragraph::params() const
2398 bool Paragraph::isFreeSpacing() const
2400 if (d->layout_->free_spacing)
2402 return d->inset_owner_ && d->inset_owner_->isFreeSpacing();
2406 bool Paragraph::allowEmpty() const
2408 if (d->layout_->keepempty)
2410 return d->inset_owner_ && d->inset_owner_->allowEmpty();
2414 char_type Paragraph::transformChar(char_type c, pos_type pos) const
2416 if (!Encodings::isArabicChar(c))
2419 char_type prev_char = ' ';
2420 char_type next_char = ' ';
2422 for (pos_type i = pos - 1; i >= 0; --i) {
2423 char_type const par_char = d->text_[i];
2424 if (!Encodings::isArabicComposeChar(par_char)) {
2425 prev_char = par_char;
2430 for (pos_type i = pos + 1, end = size(); i < end; ++i) {
2431 char_type const par_char = d->text_[i];
2432 if (!Encodings::isArabicComposeChar(par_char)) {
2433 next_char = par_char;
2438 if (Encodings::isArabicChar(next_char)) {
2439 if (Encodings::isArabicChar(prev_char) &&
2440 !Encodings::isArabicSpecialChar(prev_char))
2441 return Encodings::transformChar(c, Encodings::FORM_MEDIAL);
2443 return Encodings::transformChar(c, Encodings::FORM_INITIAL);
2445 if (Encodings::isArabicChar(prev_char) &&
2446 !Encodings::isArabicSpecialChar(prev_char))
2447 return Encodings::transformChar(c, Encodings::FORM_FINAL);
2449 return Encodings::transformChar(c, Encodings::FORM_ISOLATED);
2454 int Paragraph::checkBiblio(Buffer const & buffer)
2457 // This is getting more and more a mess. ...We really should clean
2458 // up this bibitem issue for 1.6. See also bug 2743.
2460 // Add bibitem insets if necessary
2461 if (d->layout_->labeltype != LABEL_BIBLIO)
2464 bool hasbibitem = !d->insetlist_.empty()
2465 // Insist on it being in pos 0
2466 && d->text_[0] == META_INSET
2467 && d->insetlist_.begin()->inset->lyxCode() == BIBITEM_CODE;
2469 bool track_changes = buffer.params().trackChanges;
2474 // remove a bibitem in pos != 0
2475 // restore it later in pos 0 if necessary
2476 // (e.g. if a user inserts contents _before_ the item)
2477 // we're assuming there's only one of these, which there
2479 int erasedInsetPosition = -1;
2480 InsetList::iterator it = d->insetlist_.begin();
2481 InsetList::iterator end = d->insetlist_.end();
2482 for (; it != end; ++it)
2483 if (it->inset->lyxCode() == BIBITEM_CODE
2485 InsetBibitem * olditem = static_cast<InsetBibitem *>(it->inset);
2486 oldkey = olditem->getParam("key");
2487 oldlabel = olditem->getParam("label");
2488 erasedInsetPosition = it->pos;
2489 eraseChar(erasedInsetPosition, track_changes);
2493 // There was an InsetBibitem at the beginning, and we didn't
2494 // have to erase one.
2495 if (hasbibitem && erasedInsetPosition < 0)
2498 // There was an InsetBibitem at the beginning and we did have to
2499 // erase one. So we give its properties to the beginning inset.
2501 InsetBibitem * inset =
2502 static_cast<InsetBibitem *>(d->insetlist_.begin()->inset);
2503 if (!oldkey.empty())
2504 inset->setParam("key", oldkey);
2505 inset->setParam("label", oldlabel);
2506 return -erasedInsetPosition;
2509 // There was no inset at the beginning, so we need to create one with
2510 // the key and label of the one we erased.
2511 InsetBibitem * inset = new InsetBibitem(InsetCommandParams(BIBITEM_CODE));
2512 inset->setBuffer(const_cast<Buffer &>(buffer));
2513 // restore values of previously deleted item in this par.
2514 if (!oldkey.empty())
2515 inset->setParam("key", oldkey);
2516 inset->setParam("label", oldlabel);
2517 insertInset(0, static_cast<Inset *>(inset),
2518 Change(track_changes ? Change::INSERTED : Change::UNCHANGED));
2524 void Paragraph::checkAuthors(AuthorList const & authorList)
2526 d->changes_.checkAuthors(authorList);
2530 bool Paragraph::isUnchanged(pos_type pos) const
2532 return lookupChange(pos).type == Change::UNCHANGED;
2536 bool Paragraph::isInserted(pos_type pos) const
2538 return lookupChange(pos).type == Change::INSERTED;
2542 bool Paragraph::isDeleted(pos_type pos) const
2544 return lookupChange(pos).type == Change::DELETED;
2548 InsetList const & Paragraph::insetList() const
2550 return d->insetlist_;
2554 Inset * Paragraph::releaseInset(pos_type pos)
2556 Inset * inset = d->insetlist_.release(pos);
2557 /// does not honour change tracking!
2558 eraseChar(pos, false);
2563 Inset * Paragraph::getInset(pos_type pos)
2565 return (pos < pos_type(d->text_.size()) && d->text_[pos] == META_INSET)
2566 ? d->insetlist_.get(pos) : 0;
2570 Inset const * Paragraph::getInset(pos_type pos) const
2572 return (pos < pos_type(d->text_.size()) && d->text_[pos] == META_INSET)
2573 ? d->insetlist_.get(pos) : 0;
2577 void Paragraph::changeCase(BufferParams const & bparams, pos_type pos,
2578 pos_type & right, TextCase action)
2580 // process sequences of modified characters; in change
2581 // tracking mode, this approach results in much better
2582 // usability than changing case on a char-by-char basis
2585 bool const trackChanges = bparams.trackChanges;
2587 bool capitalize = true;
2589 for (; pos < right; ++pos) {
2590 char_type oldChar = d->text_[pos];
2591 char_type newChar = oldChar;
2593 // ignore insets and don't play with deleted text!
2594 if (oldChar != META_INSET && !isDeleted(pos)) {
2596 case text_lowercase:
2597 newChar = lowercase(oldChar);
2599 case text_capitalization:
2601 newChar = uppercase(oldChar);
2605 case text_uppercase:
2606 newChar = uppercase(oldChar);
2611 if (!isLetter(pos) || isDeleted(pos)) {
2612 // permit capitalization again
2616 if (oldChar != newChar)
2619 if (oldChar == newChar || pos == right - 1) {
2620 if (oldChar != newChar) {
2621 // step behind the changing area
2624 int erasePos = pos - changes.size();
2625 for (size_t i = 0; i < changes.size(); i++) {
2626 insertChar(pos, changes[i],
2627 getFontSettings(bparams,
2630 if (!eraseChar(erasePos, trackChanges)) {
2633 ++right; // expand selection
2642 bool Paragraph::find(docstring const & str, bool cs, bool mw,
2643 pos_type pos, bool del) const
2645 int const strsize = str.length();
2647 pos_type const parsize = d->text_.size();
2648 for (i = 0; pos + i < parsize; ++i) {
2651 if (cs && str[i] != d->text_[pos + i])
2653 if (!cs && uppercase(str[i]) != uppercase(d->text_[pos + i]))
2655 if (!del && isDeleted(pos + i))
2662 // if necessary, check whether string matches word
2664 if (pos > 0 && isLetter(pos - 1))
2666 if (pos + strsize < parsize
2667 && isLetter(pos + strsize))
2675 char_type Paragraph::getChar(pos_type pos) const
2677 return d->text_[pos];
2681 pos_type Paragraph::size() const
2683 return d->text_.size();
2687 bool Paragraph::empty() const
2689 return d->text_.empty();
2693 bool Paragraph::isInset(pos_type pos) const
2695 return d->text_[pos] == META_INSET;
2699 bool Paragraph::isSeparator(pos_type pos) const
2701 //FIXME: Are we sure this can be the only separator?
2702 return d->text_[pos] == ' ';
2706 void Paragraph::deregisterWords()
2708 Private::Words::const_iterator it;
2709 WordList & wl = theWordList();
2710 for (it = d->words_.begin(); it != d->words_.end(); ++it)
2716 void Paragraph::collectWords(CursorSlice const & sl)
2719 bool inword = false;
2721 //lyxerr << "Words: ";
2722 pos_type n = size();
2723 for (pos_type pos = 0; pos != n; ++pos) {
2727 if (!isLetter(pos)) {
2736 CursorSlice from = sl;
2737 CursorSlice to = sl;
2740 from.text()->getWord(from, to, WHOLE_WORD);
2741 if (to.pos() - from.pos() < 6)
2743 docstring word = asString(from.pos(), to.pos(), false);
2744 d->words_.insert(word);
2745 //lyxerr << word << " ";
2747 //lyxerr << std::endl;
2751 void Paragraph::registerWords()
2753 Private::Words::const_iterator it;
2754 WordList & wl = theWordList();
2755 for (it = d->words_.begin(); it != d->words_.end(); ++it)
2760 void Paragraph::updateWords(CursorSlice const & sl)
2762 LASSERT(&sl.paragraph() == this, /**/);