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 if (docstring(1, next) == latex1) {
503 // the encoding supports the combination
504 os << latex2 << latex1;
505 return latex1.length() + latex2.length();
507 os << latex1 << '{' << latex2 << '}';
508 return latex1.length() + latex2.length() + 2;
512 bool Paragraph::Private::simpleTeXBlanks(OutputParams const & runparams,
513 odocstream & os, TexRow & texrow,
515 unsigned int & column,
517 Layout const & style)
519 if (style.pass_thru || runparams.verbatim)
522 if (i + 1 < int(text_.size())) {
523 char_type next = text_[i + 1];
524 if (Encodings::isCombiningChar(next)) {
525 Encoding const & encoding = *(runparams.encoding);
526 // This space has an accent, so we must always output it.
527 column += latexSurrogatePair(os, ' ', next, encoding) - 1;
532 if (lyxrc.plaintext_linelen > 0
533 && column > lyxrc.plaintext_linelen
535 && text_[i - 1] != ' '
536 && (i + 1 < int(text_.size()))
537 // same in FreeSpacing mode
538 && !owner_->isFreeSpacing()
539 // In typewriter mode, we want to avoid
540 // ! . ? : at the end of a line
541 && !(font.fontInfo().family() == TYPEWRITER_FAMILY
542 && (text_[i - 1] == '.'
543 || text_[i - 1] == '?'
544 || text_[i - 1] == ':'
545 || text_[i - 1] == '!'))) {
548 texrow.start(owner_->id(), i + 1);
550 } else if (style.free_spacing) {
559 int Paragraph::Private::writeScriptChars(odocstream & os,
560 docstring const & ltx,
561 Change & runningChange,
562 Encoding const & encoding,
565 // FIXME: modifying i here is not very nice...
567 // We only arrive here when a proper language for character text_[i] has
568 // not been specified (i.e., it could not be translated in the current
569 // latex encoding) and it belongs to a known script.
570 // Parameter ltx contains the latex translation of text_[i] as specified in
571 // the unicodesymbols file and is something like "\textXXX{<spec>}".
572 // The latex macro name "textXXX" specifies the script to which text_[i]
573 // belongs and we use it in order to check whether characters from the
574 // same script immediately follow, such that we can collect them in a
575 // single "\textXXX" macro. So, we have to retain "\textXXX{<spec>"
576 // for the first char but only "<spec>" for all subsequent chars.
577 docstring::size_type const brace1 = ltx.find_first_of(from_ascii("{"));
578 docstring::size_type const brace2 = ltx.find_last_of(from_ascii("}"));
579 string script = to_ascii(ltx.substr(1, brace1 - 1));
580 int length = ltx.substr(0, brace2).length();
581 os << ltx.substr(0, brace2);
582 int size = text_.size();
583 while (i + 1 < size) {
584 char_type const next = text_[i + 1];
585 // Stop here if next character belongs to another script
586 // or there is a change in change tracking status.
587 if (!Encodings::isKnownScriptChar(next, script) ||
588 runningChange != owner_->lookupChange(i + 1))
592 FontList::const_iterator cit = fontlist_.begin();
593 FontList::const_iterator end = fontlist_.end();
594 for (; cit != end; ++cit) {
595 if (cit->pos() >= i && !found) {
596 prev_font = cit->font();
599 if (cit->pos() >= i + 1)
602 // Stop here if there is a font attribute or encoding change.
603 if (found && cit != end && prev_font != cit->font())
605 docstring const latex = encoding.latexChar(next);
606 docstring::size_type const b1 =
607 latex.find_first_of(from_ascii("{"));
608 docstring::size_type const b2 =
609 latex.find_last_of(from_ascii("}"));
610 int const len = b2 - b1 - 1;
611 os << latex.substr(b1 + 1, len);
621 bool Paragraph::Private::isTextAt(string const & str, pos_type pos) const
623 pos_type const len = str.length();
625 // is the paragraph large enough?
626 if (pos + len > int(text_.size()))
629 // does the wanted text start at point?
630 for (string::size_type i = 0; i < str.length(); ++i) {
631 // Caution: direct comparison of characters works only
632 // because str is pure ASCII.
633 if (str[i] != text_[pos + i])
637 return fontlist_.hasChangeInRange(pos, len);
641 void Paragraph::Private::latexInset(
642 BufferParams const & bparams,
645 OutputParams & runparams,
648 Font const & outerfont,
650 Change & running_change,
651 Layout const & style,
653 unsigned int & column)
655 Inset * inset = owner_->getInset(i);
656 LASSERT(inset, /**/);
658 if (style.pass_thru) {
659 inset->plaintext(os, runparams);
663 // FIXME: move this to InsetNewline::latex
664 if (inset->lyxCode() == NEWLINE_CODE) {
665 // newlines are handled differently here than
666 // the default in simpleTeXSpecialChars().
667 if (!style.newline_allowed) {
671 column += running_font.latexWriteEndChanges(
672 os, bparams, runparams,
677 if (running_font.fontInfo().family() == TYPEWRITER_FAMILY)
680 basefont = owner_->getLayoutFont(bparams, outerfont);
681 running_font = basefont;
683 if (runparams.moving_arg)
688 texrow.start(owner_->id(), i + 1);
692 if (owner_->lookupChange(i).type == Change::DELETED) {
693 if( ++runparams.inDeletedInset == 1)
694 runparams.changeOfDeletedInset = owner_->lookupChange(i);
697 if (inset->canTrackChanges()) {
698 column += Changes::latexMarkChange(os, bparams, running_change,
699 Change(Change::UNCHANGED));
700 running_change = Change(Change::UNCHANGED);
704 odocstream::pos_type const len = os.tellp();
706 if (inset->forceLTR()
707 && running_font.isRightToLeft()
708 // ERT is an exception, it should be output with no decorations at all
709 && inset->lyxCode() != ERT_CODE) {
710 if (running_font.language()->lang() == "farsi")
717 // FIXME: Bug: we can have an empty font change here!
718 // if there has just been a font change, we are going to close it
719 // right now, which means stupid latex code like \textsf{}. AFAIK,
720 // this does not harm dvi output. A minor bug, thus (JMarc)
722 // Some insets cannot be inside a font change command.
723 // However, even such insets *can* be placed in \L or \R
724 // or their equivalents (for RTL language switches), so we don't
725 // close the language in those cases.
726 // ArabTeX, though, cannot handle this special behavior, it seems.
727 bool arabtex = basefont.language()->lang() == "arabic_arabtex"
728 || running_font.language()->lang() == "arabic_arabtex";
729 if (open_font && inset->noFontChange()) {
730 bool closeLanguage = arabtex
731 || basefont.isRightToLeft() == running_font.isRightToLeft();
732 unsigned int count = running_font.latexWriteEndChanges(os,
733 bparams, runparams, basefont, basefont, closeLanguage);
735 // if any font properties were closed, update the running_font,
736 // making sure, however, to leave the language as it was
738 // FIXME: probably a better way to keep track of the old
739 // language, than copying the entire font?
740 Font const copy_font(running_font);
741 basefont = owner_->getLayoutFont(bparams, outerfont);
742 running_font = basefont;
744 running_font.setLanguage(copy_font.language());
745 // leave font open if language is still open
746 open_font = (running_font.language() == basefont.language());
748 runparams.local_font = &basefont;
755 tmp = inset->latex(os, runparams);
756 } catch (EncodingException & e) {
757 // add location information and throw again.
764 if (running_font.language()->lang() == "farsi")
771 for (int j = 0; j < tmp; ++j)
774 texrow.start(owner_->id(), i + 1);
777 column += os.tellp() - len;
780 if (owner_->lookupChange(i).type == Change::DELETED)
781 --runparams.inDeletedInset;
785 void Paragraph::Private::latexSpecialChar(
787 OutputParams & runparams,
789 Change & running_change,
790 Layout const & style,
792 unsigned int & column)
794 char_type const c = text_[i];
796 if (style.pass_thru) {
798 // FIXME UNICODE: This can fail if c cannot
799 // be encoded in the current encoding.
804 if (runparams.verbatim) {
809 if (lyxrc.fontenc == "T1" && latexSpecialT1(c, os, i, column))
812 if (running_font.fontInfo().family() == TYPEWRITER_FAMILY
813 && latexSpecialTypewriter(c, os, i, column))
816 // Otherwise, we use what LaTeX provides us.
819 os << "\\textbackslash{}";
823 os << "\\textless{}";
827 os << "\\textgreater{}";
838 os << "\\char`\\\"{}";
843 case '%': case '#': case '{':
851 os << "\\textasciitilde{}";
856 os << "\\textasciicircum{}";
861 // avoid being mistaken for optional arguments
869 // Blanks are printed before font switching.
870 // Sure? I am not! (try nice-latex)
871 // I am sure it's correct. LyX might be smarter
872 // in the future, but for now, nothing wrong is
879 if (latexSpecialPhrase(os, i, column, runparams))
885 Encoding const & encoding = *(runparams.encoding);
886 if (i + 1 < int(text_.size())) {
887 char_type next = text_[i + 1];
888 if (Encodings::isCombiningChar(next)) {
889 column += latexSurrogatePair(os, c, next, encoding) - 1;
895 docstring const latex = encoding.latexChar(c);
896 if (Encodings::isKnownScriptChar(c, script)
897 && prefixIs(latex, from_ascii("\\" + script)))
898 column += writeScriptChars(os, latex,
899 running_change, encoding, i) - 1;
900 else if (latex.length() > 1 && latex[latex.length() - 1] != '}') {
901 // Prevent eating of a following
902 // space or command corruption by
903 // following characters
904 column += latex.length() + 1;
907 column += latex.length() - 1;
915 bool Paragraph::Private::latexSpecialT1(char_type const c, odocstream & os,
916 pos_type & i, unsigned int & column)
922 // In T1 encoding, these characters exist
923 // but we should avoid ligatures
924 if (i + 1 >= int(text_.size()) || text_[i + 1] != c)
926 os << "\\textcompwordmark{}";
938 bool Paragraph::Private::latexSpecialTypewriter(char_type const c, odocstream & os,
939 pos_type & i, unsigned int & column)
943 if (i + 1 < int(text_.size()) && text_[i + 1] == '-') {
944 // "--" in Typewriter mode -> "-{}-"
951 // FIXME I assume this is hack treating typewriter as verbatim
952 // This should be re-evaluated eventually.
957 // // Those characters are not directly supported.
960 // case '$': case '&':
961 // case '%': case '#': case '{':
962 // case '}': case '_':
965 // case '*': case '[':
975 bool Paragraph::Private::latexSpecialPhrase(odocstream & os, pos_type & i,
976 unsigned int & column, OutputParams & runparams)
978 // FIXME: if we have "LaTeX" with a font
979 // change in the middle (before the 'T', then
980 // the "TeX" part is still special cased.
981 // Really we should only operate this on
982 // "words" for some definition of word
984 for (size_t pnr = 0; pnr < phrases_nr; ++pnr) {
985 if (!isTextAt(special_phrases[pnr].phrase, i))
987 if (runparams.moving_arg)
989 os << special_phrases[pnr].macro;
990 i += special_phrases[pnr].phrase.length() - 1;
991 column += special_phrases[pnr].macro.length() - 1;
998 void Paragraph::Private::validate(LaTeXFeatures & features,
999 Layout const & layout) const
1001 // check the params.
1002 if (!params_.spacing().isDefault())
1003 features.require("setspace");
1006 features.useLayout(layout.name());
1009 fontlist_.validate(features);
1011 // then the indentation
1012 if (!params_.leftIndent().zero())
1013 features.require("ParagraphLeftIndent");
1016 InsetList::const_iterator icit = insetlist_.begin();
1017 InsetList::const_iterator iend = insetlist_.end();
1018 for (; icit != iend; ++icit) {
1020 icit->inset->validate(features);
1021 if (layout.needprotect &&
1022 icit->inset->lyxCode() == FOOT_CODE)
1023 features.require("NeedLyXFootnoteCode");
1027 // then the contents
1028 for (pos_type i = 0; i < int(text_.size()) ; ++i) {
1029 for (size_t pnr = 0; pnr < phrases_nr; ++pnr) {
1030 if (!special_phrases[pnr].builtin
1031 && isTextAt(special_phrases[pnr].phrase, i)) {
1032 features.require(special_phrases[pnr].phrase);
1036 Encodings::validate(text_[i], features);
1040 /////////////////////////////////////////////////////////////////////
1044 /////////////////////////////////////////////////////////////////////
1047 Layout const emptyParagraphLayout;
1050 Paragraph::Paragraph()
1051 : d(new Paragraph::Private(this, emptyParagraphLayout))
1058 Paragraph::Paragraph(Paragraph const & par)
1059 : itemdepth(par.itemdepth),
1060 d(new Paragraph::Private(*par.d, this))
1066 Paragraph & Paragraph::operator=(Paragraph const & par)
1068 // needed as we will destroy the private part before copying it
1070 itemdepth = par.itemdepth;
1074 d = new Private(*par.d, this);
1081 Paragraph::~Paragraph()
1088 void Paragraph::write(ostream & os, BufferParams const & bparams,
1089 depth_type & dth) const
1091 // The beginning or end of a deeper (i.e. nested) area?
1092 if (dth != d->params_.depth()) {
1093 if (d->params_.depth() > dth) {
1094 while (d->params_.depth() > dth) {
1095 os << "\n\\begin_deeper";
1099 while (d->params_.depth() < dth) {
1100 os << "\n\\end_deeper";
1106 // First write the layout
1107 os << "\n\\begin_layout " << to_utf8(d->layout_->name()) << '\n';
1109 d->params_.write(os);
1111 Font font1(inherit_font, bparams.language);
1113 Change running_change = Change(Change::UNCHANGED);
1116 for (pos_type i = 0; i <= size(); ++i) {
1118 Change change = lookupChange(i);
1119 Changes::lyxMarkChange(os, column, running_change, change);
1120 running_change = change;
1125 // Write font changes
1126 Font font2 = getFontSettings(bparams, i);
1127 if (font2 != font1) {
1128 font2.lyxWriteChanges(font1, os);
1133 char_type const c = d->text_[i];
1136 if (Inset const * inset = getInset(i)) {
1137 if (inset->directWrite()) {
1138 // international char, let it write
1139 // code directly so it's shorter in
1145 os << "\\begin_inset ";
1147 os << "\n\\end_inset\n\n";
1153 os << "\n\\backslash\n";
1157 if (i + 1 < size() && d->text_[i + 1] == ' ') {
1164 if ((column > 70 && c == ' ')
1169 // this check is to amend a bug. LyX sometimes
1170 // inserts '\0' this could cause problems.
1172 os << to_utf8(docstring(1, c));
1174 LYXERR0("NUL char in structure.");
1180 os << "\n\\end_layout\n";
1184 void Paragraph::validate(LaTeXFeatures & features) const
1186 d->validate(features, *d->layout_);
1190 void Paragraph::insert(pos_type start, docstring const & str,
1191 Font const & font, Change const & change)
1193 for (size_t i = 0, n = str.size(); i != n ; ++i)
1194 insertChar(start + i, str[i], font, change);
1198 void Paragraph::appendChar(char_type c, Font const & font,
1199 Change const & change)
1202 d->changes_.insert(change, d->text_.size());
1203 // when appending characters, no need to update tables
1204 d->text_.push_back(c);
1205 setFont(d->text_.size() - 1, font);
1209 void Paragraph::appendString(docstring const & s, Font const & font,
1210 Change const & change)
1212 pos_type end = s.size();
1213 size_t oldsize = d->text_.size();
1214 size_t newsize = oldsize + end;
1215 size_t capacity = d->text_.capacity();
1216 if (newsize >= capacity)
1217 d->text_.reserve(max(capacity + 100, newsize));
1219 // when appending characters, no need to update tables
1222 // FIXME: Optimize this!
1223 for (pos_type i = oldsize; i != newsize; ++i) {
1225 d->changes_.insert(change, i);
1227 d->fontlist_.set(oldsize, font);
1228 d->fontlist_.set(newsize - 1, font);
1232 void Paragraph::insertChar(pos_type pos, char_type c,
1235 d->insertChar(pos, c, Change(trackChanges ?
1236 Change::INSERTED : Change::UNCHANGED));
1240 void Paragraph::insertChar(pos_type pos, char_type c,
1241 Font const & font, bool trackChanges)
1243 d->insertChar(pos, c, Change(trackChanges ?
1244 Change::INSERTED : Change::UNCHANGED));
1249 void Paragraph::insertChar(pos_type pos, char_type c,
1250 Font const & font, Change const & change)
1252 d->insertChar(pos, c, change);
1257 void Paragraph::insertInset(pos_type pos, Inset * inset,
1258 Font const & font, Change const & change)
1260 insertInset(pos, inset, change);
1261 // Set the font/language of the inset...
1266 bool Paragraph::insetAllowed(InsetCode code)
1268 return !d->inset_owner_ || d->inset_owner_->insetAllowed(code);
1272 void Paragraph::resetFonts(Font const & font)
1274 d->fontlist_.clear();
1275 d->fontlist_.set(0, font);
1276 d->fontlist_.set(d->text_.size() - 1, font);
1279 // Gets uninstantiated font setting at position.
1280 Font const Paragraph::getFontSettings(BufferParams const & bparams,
1284 LYXERR0("pos: " << pos << " size: " << size());
1285 LASSERT(pos <= size(), /**/);
1288 FontList::const_iterator cit = d->fontlist_.fontIterator(pos);
1289 if (cit != d->fontlist_.end())
1292 if (pos == size() && !empty())
1293 return getFontSettings(bparams, pos - 1);
1295 return Font(inherit_font, getParLanguage(bparams));
1299 FontSpan Paragraph::fontSpan(pos_type pos) const
1301 LASSERT(pos <= size(), /**/);
1304 FontList::const_iterator cit = d->fontlist_.begin();
1305 FontList::const_iterator end = d->fontlist_.end();
1306 for (; cit != end; ++cit) {
1307 if (cit->pos() >= pos) {
1308 if (pos >= beginOfBody())
1309 return FontSpan(max(start, beginOfBody()),
1312 return FontSpan(start,
1313 min(beginOfBody() - 1,
1316 start = cit->pos() + 1;
1319 // This should not happen, but if so, we take no chances.
1320 // LYXERR0("Paragraph::getEndPosOfFontSpan: This should not happen!");
1321 return FontSpan(pos, pos);
1325 // Gets uninstantiated font setting at position 0
1326 Font const Paragraph::getFirstFontSettings(BufferParams const & bparams) const
1328 if (!empty() && !d->fontlist_.empty())
1329 return d->fontlist_.begin()->font();
1331 return Font(inherit_font, bparams.language);
1335 // Gets the fully instantiated font at a given position in a paragraph
1336 // This is basically the same function as Text::GetFont() in text2.cpp.
1337 // The difference is that this one is used for generating the LaTeX file,
1338 // and thus cosmetic "improvements" are disallowed: This has to deliver
1339 // the true picture of the buffer. (Asger)
1340 Font const Paragraph::getFont(BufferParams const & bparams, pos_type pos,
1341 Font const & outerfont) const
1343 LASSERT(pos >= 0, /**/);
1345 Font font = getFontSettings(bparams, pos);
1347 pos_type const body_pos = beginOfBody();
1349 font.fontInfo().realize(d->layout_->labelfont);
1351 font.fontInfo().realize(d->layout_->font);
1353 font.fontInfo().realize(outerfont.fontInfo());
1354 font.fontInfo().realize(bparams.getFont().fontInfo());
1360 Font const Paragraph::getLabelFont
1361 (BufferParams const & bparams, Font const & outerfont) const
1363 FontInfo tmpfont = d->layout_->labelfont;
1364 tmpfont.realize(outerfont.fontInfo());
1365 tmpfont.realize(bparams.getFont().fontInfo());
1366 return Font(tmpfont, getParLanguage(bparams));
1370 Font const Paragraph::getLayoutFont
1371 (BufferParams const & bparams, Font const & outerfont) const
1373 FontInfo tmpfont = d->layout_->font;
1374 tmpfont.realize(outerfont.fontInfo());
1375 tmpfont.realize(bparams.getFont().fontInfo());
1376 return Font(tmpfont, getParLanguage(bparams));
1380 /// Returns the height of the highest font in range
1381 FontSize Paragraph::highestFontInRange
1382 (pos_type startpos, pos_type endpos, FontSize def_size) const
1384 return d->fontlist_.highestInRange(startpos, endpos, def_size);
1388 char_type Paragraph::getUChar(BufferParams const & bparams, pos_type pos) const
1390 char_type c = d->text_[pos];
1391 if (!lyxrc.rtl_support)
1421 if (uc != c && getFontSettings(bparams, pos).isRightToLeft())
1427 void Paragraph::setFont(pos_type pos, Font const & font)
1429 LASSERT(pos <= size(), /**/);
1431 // First, reduce font against layout/label font
1432 // Update: The setCharFont() routine in text2.cpp already
1433 // reduces font, so we don't need to do that here. (Asger)
1435 d->fontlist_.set(pos, font);
1439 void Paragraph::makeSameLayout(Paragraph const & par)
1441 d->layout_ = par.d->layout_;
1442 d->params_ = par.d->params_;
1446 bool Paragraph::stripLeadingSpaces(bool trackChanges)
1448 if (isFreeSpacing())
1454 while (pos < size() && (isNewline(pos) || isLineSeparator(pos))) {
1455 if (eraseChar(pos, trackChanges))
1461 return count > 0 || pos > 0;
1465 bool Paragraph::hasSameLayout(Paragraph const & par) const
1467 return par.d->layout_ == d->layout_
1468 && d->params_.sameLayout(par.d->params_);
1472 depth_type Paragraph::getDepth() const
1474 return d->params_.depth();
1478 depth_type Paragraph::getMaxDepthAfter() const
1480 if (d->layout_->isEnvironment())
1481 return d->params_.depth() + 1;
1483 return d->params_.depth();
1487 char Paragraph::getAlign() const
1489 if (d->params_.align() == LYX_ALIGN_LAYOUT)
1490 return d->layout_->align;
1492 return d->params_.align();
1496 docstring const & Paragraph::labelString() const
1498 return d->params_.labelString();
1502 // the next two functions are for the manual labels
1503 docstring const Paragraph::getLabelWidthString() const
1505 if (d->layout_->margintype == MARGIN_MANUAL)
1506 return d->params_.labelWidthString();
1508 return _("Senseless with this layout!");
1512 void Paragraph::setLabelWidthString(docstring const & s)
1514 d->params_.labelWidthString(s);
1518 docstring const Paragraph::translateIfPossible(docstring const & s,
1519 BufferParams const & bparams) const
1521 if (!isAscii(s) || s.empty()) {
1522 // This must be a user defined layout. We cannot translate
1523 // this, since gettext accepts only ascii keys.
1526 // Probably standard layout, try to translate
1527 Messages & m = getMessages(getParLanguage(bparams)->code());
1528 return m.get(to_ascii(s));
1532 docstring Paragraph::expandLabel(Layout const & layout,
1533 BufferParams const & bparams, bool process_appendix) const
1535 DocumentClass const & tclass = bparams.documentClass();
1538 if (process_appendix && d->params_.appendix())
1539 fmt = translateIfPossible(layout.labelstring_appendix(),
1542 fmt = translateIfPossible(layout.labelstring(), bparams);
1544 if (fmt.empty() && layout.labeltype == LABEL_COUNTER
1545 && !layout.counter.empty())
1546 fmt = "\\the" + layout.counter;
1548 // handle 'inherited level parts' in 'fmt',
1549 // i.e. the stuff between '@' in '@Section@.\arabic{subsection}'
1550 size_t const i = fmt.find('@', 0);
1551 if (i != docstring::npos) {
1552 size_t const j = fmt.find('@', i + 1);
1553 if (j != docstring::npos) {
1554 docstring parent(fmt, i + 1, j - i - 1);
1555 docstring label = from_ascii("??");
1556 if (tclass.hasLayout(parent))
1557 docstring label = expandLabel(tclass[parent], bparams,
1559 fmt = docstring(fmt, 0, i) + label
1560 + docstring(fmt, j + 1, docstring::npos);
1564 return tclass.counters().counterLabel(fmt);
1568 void Paragraph::applyLayout(Layout const & new_layout)
1570 d->layout_ = &new_layout;
1571 LyXAlignment const oldAlign = d->params_.align();
1573 if (!(oldAlign & d->layout_->alignpossible)) {
1574 frontend::Alert::warning(_("Alignment not permitted"),
1575 _("The new layout does not permit the alignment previously used.\nSetting to default."));
1576 d->params_.align(LYX_ALIGN_LAYOUT);
1581 pos_type Paragraph::beginOfBody() const
1583 return d->begin_of_body_;
1587 void Paragraph::setBeginOfBody()
1589 if (d->layout_->labeltype != LABEL_MANUAL) {
1590 d->begin_of_body_ = 0;
1594 // Unroll the first two cycles of the loop
1595 // and remember the previous character to
1596 // remove unnecessary getChar() calls
1598 pos_type end = size();
1599 if (i < end && !isNewline(i)) {
1601 char_type previous_char = 0;
1604 previous_char = d->text_[i];
1605 if (!isNewline(i)) {
1607 while (i < end && previous_char != ' ') {
1612 previous_char = temp;
1618 d->begin_of_body_ = i;
1622 bool Paragraph::forceEmptyLayout() const
1624 Inset const * const inset = inInset();
1627 return inset->forceEmptyLayout();
1631 bool Paragraph::allowParagraphCustomization() const
1633 Inset const * const inset = inInset();
1636 return inset->allowParagraphCustomization();
1640 bool Paragraph::useEmptyLayout() const
1642 Inset const * const inset = inInset();
1645 return inset->useEmptyLayout();
1651 // paragraphs inside floats need different alignment tags to avoid
1654 bool noTrivlistCentering(InsetCode code)
1656 return code == FLOAT_CODE || code == WRAP_CODE;
1660 string correction(string const & orig)
1662 if (orig == "flushleft")
1663 return "raggedright";
1664 if (orig == "flushright")
1665 return "raggedleft";
1666 if (orig == "center")
1672 string const corrected_env(string const & suffix, string const & env,
1675 string output = suffix + "{";
1676 if (noTrivlistCentering(code))
1677 output += correction(env);
1681 if (suffix == "\\begin")
1687 void adjust_row_column(string const & str, TexRow & texrow, int & column)
1689 if (!contains(str, "\n"))
1690 column += str.size();
1694 column = rsplit(str, tmp, '\n').size();
1701 int Paragraph::Private::startTeXParParams(BufferParams const & bparams,
1702 odocstream & os, TexRow & texrow,
1703 bool moving_arg) const
1707 if (params_.noindent()) {
1708 os << "\\noindent ";
1712 LyXAlignment const curAlign = params_.align();
1714 if (curAlign == layout_->align)
1718 case LYX_ALIGN_NONE:
1719 case LYX_ALIGN_BLOCK:
1720 case LYX_ALIGN_LAYOUT:
1721 case LYX_ALIGN_SPECIAL:
1723 case LYX_ALIGN_LEFT:
1724 case LYX_ALIGN_RIGHT:
1725 case LYX_ALIGN_CENTER:
1734 case LYX_ALIGN_NONE:
1735 case LYX_ALIGN_BLOCK:
1736 case LYX_ALIGN_LAYOUT:
1737 case LYX_ALIGN_SPECIAL:
1739 case LYX_ALIGN_LEFT: {
1741 if (owner_->getParLanguage(bparams)->babel() != "hebrew")
1742 output = corrected_env("\\begin", "flushleft", owner_->ownerCode());
1744 output = corrected_env("\\begin", "flushright", owner_->ownerCode());
1745 os << from_ascii(output);
1746 adjust_row_column(output, texrow, column);
1748 } case LYX_ALIGN_RIGHT: {
1750 if (owner_->getParLanguage(bparams)->babel() != "hebrew")
1751 output = corrected_env("\\begin", "flushright", owner_->ownerCode());
1753 output = corrected_env("\\begin", "flushleft", owner_->ownerCode());
1754 os << from_ascii(output);
1755 adjust_row_column(output, texrow, column);
1757 } case LYX_ALIGN_CENTER: {
1759 output = corrected_env("\\begin", "center", owner_->ownerCode());
1760 os << from_ascii(output);
1761 adjust_row_column(output, texrow, column);
1770 int Paragraph::Private::endTeXParParams(BufferParams const & bparams,
1771 odocstream & os, TexRow & texrow,
1772 bool moving_arg) const
1776 switch (params_.align()) {
1777 case LYX_ALIGN_NONE:
1778 case LYX_ALIGN_BLOCK:
1779 case LYX_ALIGN_LAYOUT:
1780 case LYX_ALIGN_SPECIAL:
1782 case LYX_ALIGN_LEFT:
1783 case LYX_ALIGN_RIGHT:
1784 case LYX_ALIGN_CENTER:
1792 switch (params_.align()) {
1793 case LYX_ALIGN_NONE:
1794 case LYX_ALIGN_BLOCK:
1795 case LYX_ALIGN_LAYOUT:
1796 case LYX_ALIGN_SPECIAL:
1798 case LYX_ALIGN_LEFT: {
1800 if (owner_->getParLanguage(bparams)->babel() != "hebrew")
1801 output = corrected_env("\n\\par\\end", "flushleft", owner_->ownerCode());
1803 output = corrected_env("\n\\par\\end", "flushright", owner_->ownerCode());
1804 os << from_ascii(output);
1805 adjust_row_column(output, texrow, column);
1807 } case LYX_ALIGN_RIGHT: {
1809 if (owner_->getParLanguage(bparams)->babel() != "hebrew")
1810 output = corrected_env("\n\\par\\end", "flushright", owner_->ownerCode());
1812 output = corrected_env("\n\\par\\end", "flushleft", owner_->ownerCode());
1813 os << from_ascii(output);
1814 adjust_row_column(output, texrow, column);
1816 } case LYX_ALIGN_CENTER: {
1818 output = corrected_env("\n\\par\\end", "center", owner_->ownerCode());
1819 os << from_ascii(output);
1820 adjust_row_column(output, texrow, column);
1829 // This one spits out the text of the paragraph
1830 bool Paragraph::latex(BufferParams const & bparams,
1831 Font const & outerfont,
1832 odocstream & os, TexRow & texrow,
1833 OutputParams const & runparams) const
1835 LYXERR(Debug::LATEX, "SimpleTeXOnePar... " << this);
1837 bool return_value = false;
1839 bool asdefault = forceEmptyLayout();
1841 Layout const & style = asdefault ?
1842 bparams.documentClass().emptyLayout() :
1845 // Current base font for all inherited font changes, without any
1846 // change caused by an individual character, except for the language:
1847 // It is set to the language of the first character.
1848 // As long as we are in the label, this font is the base font of the
1849 // label. Before the first body character it is set to the base font
1853 // Maybe we have to create a optional argument.
1854 pos_type body_pos = beginOfBody();
1855 unsigned int column = 0;
1858 // the optional argument is kept in curly brackets in
1859 // case it contains a ']'
1862 basefont = getLabelFont(bparams, outerfont);
1864 basefont = getLayoutFont(bparams, outerfont);
1867 // Which font is currently active?
1868 Font running_font(basefont);
1869 // Do we have an open font change?
1870 bool open_font = false;
1872 Change runningChange = Change(Change::UNCHANGED);
1874 texrow.start(id(), 0);
1876 // if the paragraph is empty, the loop will not be entered at all
1878 if (style.isCommand()) {
1883 column += d->startTeXParParams(bparams, os, texrow,
1884 runparams.moving_arg);
1887 for (pos_type i = 0; i < size(); ++i) {
1888 // First char in paragraph or after label?
1889 if (i == body_pos) {
1892 column += running_font.latexWriteEndChanges(
1893 os, bparams, runparams,
1894 basefont, basefont);
1897 basefont = getLayoutFont(bparams, outerfont);
1898 running_font = basefont;
1900 column += Changes::latexMarkChange(os, bparams,
1901 runningChange, Change(Change::UNCHANGED));
1902 runningChange = Change(Change::UNCHANGED);
1907 if (style.isCommand()) {
1913 column += d->startTeXParParams(bparams, os,
1915 runparams.moving_arg);
1918 Change const & change = runparams.inDeletedInset ? runparams.changeOfDeletedInset
1921 if (bparams.outputChanges && runningChange != change) {
1923 column += running_font.latexWriteEndChanges(
1924 os, bparams, runparams, basefont, basefont);
1927 basefont = getLayoutFont(bparams, outerfont);
1928 running_font = basefont;
1930 column += Changes::latexMarkChange(os, bparams, runningChange, change);
1931 runningChange = change;
1934 // do not output text which is marked deleted
1935 // if change tracking output is disabled
1936 if (!bparams.outputChanges && change.type == Change::DELETED) {
1942 // Fully instantiated font
1943 Font const font = getFont(bparams, i, outerfont);
1945 Font const last_font = running_font;
1947 // Do we need to close the previous font?
1949 (font != running_font ||
1950 font.language() != running_font.language()))
1952 column += running_font.latexWriteEndChanges(
1953 os, bparams, runparams, basefont,
1954 (i == body_pos-1) ? basefont : font);
1955 running_font = basefont;
1959 // close babel's font environment before opening CJK.
1960 if (!running_font.language()->babel().empty() &&
1961 font.language()->encoding()->package() == Encoding::CJK) {
1962 string end_tag = subst(lyxrc.language_command_end,
1964 running_font.language()->babel());
1965 os << from_ascii(end_tag);
1966 column += end_tag.length();
1969 // Switch file encoding if necessary (and allowed)
1970 if (!runparams.verbatim &&
1971 runparams.encoding->package() != Encoding::none &&
1972 font.language()->encoding()->package() != Encoding::none) {
1973 pair<bool, int> const enc_switch = switchEncoding(os, bparams,
1974 runparams, *(font.language()->encoding()));
1975 if (enc_switch.first) {
1976 column += enc_switch.second;
1977 runparams.encoding = font.language()->encoding();
1981 char_type const c = d->text_[i];
1983 // Do we need to change font?
1984 if ((font != running_font ||
1985 font.language() != running_font.language()) &&
1988 odocstringstream ods;
1989 column += font.latexWriteStartChanges(ods, bparams,
1990 runparams, basefont,
1992 running_font = font;
1994 docstring fontchange = ods.str();
1995 // check if the fontchange ends with a trailing blank
1996 // (like "\small " (see bug 3382)
1997 if (suffixIs(fontchange, ' ') && c == ' ')
1998 os << fontchange.substr(0, fontchange.size() - 1)
1999 << from_ascii("{}");
2005 // FIXME: integrate this case in latexSpecialChar
2006 // Do not print the separation of the optional argument
2007 // if style.pass_thru is false. This works because
2008 // latexSpecialChar ignores spaces if
2009 // style.pass_thru is false.
2010 if (i != body_pos - 1) {
2011 if (d->simpleTeXBlanks(
2012 runparams, os, texrow,
2013 i, column, font, style)) {
2014 // A surrogate pair was output. We
2015 // must not call latexSpecialChar
2016 // in this iteration, since it would output
2017 // the combining character again.
2024 OutputParams rp = runparams;
2025 rp.free_spacing = style.free_spacing;
2026 rp.local_font = &font;
2027 rp.intitle = style.intitle;
2029 // Two major modes: LaTeX or plain
2030 // Handle here those cases common to both modes
2031 // and then split to handle the two modes separately.
2032 if (c == META_INSET)
2033 d->latexInset(bparams, os,
2034 texrow, rp, running_font,
2035 basefont, outerfont, open_font,
2036 runningChange, style, i, column);
2039 d->latexSpecialChar(os, rp, running_font, runningChange,
2041 } catch (EncodingException & e) {
2042 if (runparams.dryrun) {
2043 os << "<" << _("LyX Warning: ")
2044 << _("uncodable character") << " '";
2048 // add location information and throw again.
2056 // Set the encoding to that returned from simpleTeXSpecialChars (see
2057 // comment for encoding member in OutputParams.h)
2058 runparams.encoding = rp.encoding;
2061 // If we have an open font definition, we have to close it
2063 #ifdef FIXED_LANGUAGE_END_DETECTION
2066 .latexWriteEndChanges(os, bparams, runparams,
2068 next_->getFont(bparams, 0, outerfont));
2070 running_font.latexWriteEndChanges(os, bparams,
2071 runparams, basefont, basefont);
2074 //FIXME: For now we ALWAYS have to close the foreign font settings if they are
2075 //FIXME: there as we start another \selectlanguage with the next paragraph if
2076 //FIXME: we are in need of this. This should be fixed sometime (Jug)
2077 running_font.latexWriteEndChanges(os, bparams, runparams,
2078 basefont, basefont);
2082 column += Changes::latexMarkChange(os, bparams, runningChange, Change(Change::UNCHANGED));
2084 // Needed if there is an optional argument but no contents.
2085 if (body_pos > 0 && body_pos == size()) {
2087 return_value = false;
2091 column += d->endTeXParParams(bparams, os, texrow,
2092 runparams.moving_arg);
2095 LYXERR(Debug::LATEX, "SimpleTeXOnePar...done " << this);
2096 return return_value;
2100 bool Paragraph::emptyTag() const
2102 for (pos_type i = 0; i < size(); ++i) {
2103 if (Inset const * inset = getInset(i)) {
2104 InsetCode lyx_code = inset->lyxCode();
2105 if (lyx_code != TOC_CODE &&
2106 lyx_code != INCLUDE_CODE &&
2107 lyx_code != GRAPHICS_CODE &&
2108 lyx_code != ERT_CODE &&
2109 lyx_code != LISTINGS_CODE &&
2110 lyx_code != FLOAT_CODE &&
2111 lyx_code != TABULAR_CODE) {
2115 char_type c = d->text_[i];
2116 if (c != ' ' && c != '\t')
2124 string Paragraph::getID(Buffer const & buf, OutputParams const & runparams)
2127 for (pos_type i = 0; i < size(); ++i) {
2128 if (Inset const * inset = getInset(i)) {
2129 InsetCode lyx_code = inset->lyxCode();
2130 if (lyx_code == LABEL_CODE) {
2131 InsetLabel const * const il = static_cast<InsetLabel const *>(inset);
2132 docstring const & id = il->getParam("name");
2133 return "id='" + to_utf8(sgml::cleanID(buf, runparams, id)) + "'";
2141 pos_type Paragraph::firstWord(odocstream & os, OutputParams const & runparams)
2145 for (i = 0; i < size(); ++i) {
2146 if (Inset const * inset = getInset(i)) {
2147 inset->docbook(os, runparams);
2149 char_type c = d->text_[i];
2152 os << sgml::escapeChar(c);
2159 bool Paragraph::Private::onlyText(Buffer const & buf, Font const & outerfont, pos_type initial) const
2162 pos_type size = text_.size();
2163 for (pos_type i = initial; i < size; ++i) {
2164 Font font = owner_->getFont(buf.params(), i, outerfont);
2165 if (text_[i] == META_INSET)
2167 if (i != initial && font != font_old)
2176 void Paragraph::simpleDocBookOnePar(Buffer const & buf,
2178 OutputParams const & runparams,
2179 Font const & outerfont,
2180 pos_type initial) const
2182 bool emph_flag = false;
2184 Layout const & style = *d->layout_;
2186 style.labeltype == LABEL_MANUAL ? style.labelfont : style.font;
2188 if (style.pass_thru && !d->onlyText(buf, outerfont, initial))
2191 // parsing main loop
2192 for (pos_type i = initial; i < size(); ++i) {
2193 Font font = getFont(buf.params(), i, outerfont);
2195 // handle <emphasis> tag
2196 if (font_old.emph() != font.fontInfo().emph()) {
2197 if (font.fontInfo().emph() == FONT_ON) {
2200 } else if (i != initial) {
2201 os << "</emphasis>";
2206 if (Inset const * inset = getInset(i)) {
2207 inset->docbook(os, runparams);
2209 char_type c = d->text_[i];
2211 if (style.pass_thru)
2214 os << sgml::escapeChar(c);
2216 font_old = font.fontInfo();
2220 os << "</emphasis>";
2223 if (style.free_spacing)
2225 if (style.pass_thru && !d->onlyText(buf, outerfont, initial))
2230 bool Paragraph::isHfill(pos_type pos) const
2232 Inset const * inset = getInset(pos);
2233 return inset && (inset->lyxCode() == SPACE_CODE &&
2234 inset->isStretchableSpace());
2238 bool Paragraph::isNewline(pos_type pos) const
2240 Inset const * inset = getInset(pos);
2241 return inset && inset->lyxCode() == NEWLINE_CODE;
2245 bool Paragraph::isLineSeparator(pos_type pos) const
2247 char_type const c = d->text_[pos];
2248 if (isLineSeparatorChar(c))
2250 Inset const * inset = getInset(pos);
2251 return inset && inset->isLineSeparator();
2255 /// Used by the spellchecker
2256 bool Paragraph::isLetter(pos_type pos) const
2258 if (Inset const * inset = getInset(pos))
2259 return inset->isLetter();
2260 char_type const c = d->text_[pos];
2261 return isLetterChar(c) || isDigit(c);
2265 bool Paragraph::isChar(pos_type pos) const
2267 if (Inset const * inset = getInset(pos))
2268 return inset->isChar();
2269 char_type const c = d->text_[pos];
2270 return !isLetterChar(c) && !isDigit(c) && !lyx::isSpace(c);
2274 bool Paragraph::isSpace(pos_type pos) const
2276 if (Inset const * inset = getInset(pos))
2277 return inset->isSpace();
2278 char_type const c = d->text_[pos];
2279 return lyx::isSpace(c);
2284 Paragraph::getParLanguage(BufferParams const & bparams) const
2287 return getFirstFontSettings(bparams).language();
2288 // FIXME: we should check the prev par as well (Lgb)
2289 return bparams.language;
2293 bool Paragraph::isRTL(BufferParams const & bparams) const
2295 return lyxrc.rtl_support
2296 && getParLanguage(bparams)->rightToLeft()
2297 && ownerCode() != ERT_CODE
2298 && ownerCode() != LISTINGS_CODE;
2302 void Paragraph::changeLanguage(BufferParams const & bparams,
2303 Language const * from, Language const * to)
2305 // change language including dummy font change at the end
2306 for (pos_type i = 0; i <= size(); ++i) {
2307 Font font = getFontSettings(bparams, i);
2308 if (font.language() == from) {
2309 font.setLanguage(to);
2316 bool Paragraph::isMultiLingual(BufferParams const & bparams) const
2318 Language const * doc_language = bparams.language;
2319 FontList::const_iterator cit = d->fontlist_.begin();
2320 FontList::const_iterator end = d->fontlist_.end();
2322 for (; cit != end; ++cit)
2323 if (cit->font().language() != ignore_language &&
2324 cit->font().language() != latex_language &&
2325 cit->font().language() != doc_language)
2331 docstring Paragraph::asString(int options) const
2333 return asString(0, size(), options);
2337 docstring Paragraph::asString(pos_type beg, pos_type end, int options) const
2339 odocstringstream os;
2342 && options & AS_STR_LABEL
2343 && !d->params_.labelString().empty())
2344 os << d->params_.labelString() << ' ';
2346 for (pos_type i = beg; i < end; ++i) {
2347 char_type const c = d->text_[i];
2350 else if (c == META_INSET && options & AS_STR_INSETS)
2351 getInset(i)->textString(os);
2358 void Paragraph::setInsetOwner(Inset * inset)
2360 d->inset_owner_ = inset;
2364 int Paragraph::id() const
2370 Layout const & Paragraph::layout() const
2376 void Paragraph::setLayout(Layout const & layout)
2378 d->layout_ = &layout;
2382 void Paragraph::setEmptyOrDefaultLayout(DocumentClass const & tclass)
2384 if (useEmptyLayout())
2385 setLayout(tclass.emptyLayout());
2387 setLayout(tclass.defaultLayout());
2391 Inset * Paragraph::inInset() const
2393 return d->inset_owner_;
2397 InsetCode Paragraph::ownerCode() const
2399 return d->inset_owner_ ? d->inset_owner_->lyxCode() : NO_CODE;
2403 ParagraphParameters & Paragraph::params()
2409 ParagraphParameters const & Paragraph::params() const
2415 bool Paragraph::isFreeSpacing() const
2417 if (d->layout_->free_spacing)
2419 return d->inset_owner_ && d->inset_owner_->isFreeSpacing();
2423 bool Paragraph::allowEmpty() const
2425 if (d->layout_->keepempty)
2427 return d->inset_owner_ && d->inset_owner_->allowEmpty();
2431 char_type Paragraph::transformChar(char_type c, pos_type pos) const
2433 if (!Encodings::isArabicChar(c))
2436 char_type prev_char = ' ';
2437 char_type next_char = ' ';
2439 for (pos_type i = pos - 1; i >= 0; --i) {
2440 char_type const par_char = d->text_[i];
2441 if (!Encodings::isArabicComposeChar(par_char)) {
2442 prev_char = par_char;
2447 for (pos_type i = pos + 1, end = size(); i < end; ++i) {
2448 char_type const par_char = d->text_[i];
2449 if (!Encodings::isArabicComposeChar(par_char)) {
2450 next_char = par_char;
2455 if (Encodings::isArabicChar(next_char)) {
2456 if (Encodings::isArabicChar(prev_char) &&
2457 !Encodings::isArabicSpecialChar(prev_char))
2458 return Encodings::transformChar(c, Encodings::FORM_MEDIAL);
2460 return Encodings::transformChar(c, Encodings::FORM_INITIAL);
2462 if (Encodings::isArabicChar(prev_char) &&
2463 !Encodings::isArabicSpecialChar(prev_char))
2464 return Encodings::transformChar(c, Encodings::FORM_FINAL);
2466 return Encodings::transformChar(c, Encodings::FORM_ISOLATED);
2471 int Paragraph::checkBiblio(Buffer const & buffer)
2474 // This is getting more and more a mess. ...We really should clean
2475 // up this bibitem issue for 1.6. See also bug 2743.
2477 // Add bibitem insets if necessary
2478 if (d->layout_->labeltype != LABEL_BIBLIO)
2481 bool hasbibitem = !d->insetlist_.empty()
2482 // Insist on it being in pos 0
2483 && d->text_[0] == META_INSET
2484 && d->insetlist_.begin()->inset->lyxCode() == BIBITEM_CODE;
2486 bool track_changes = buffer.params().trackChanges;
2491 // remove a bibitem in pos != 0
2492 // restore it later in pos 0 if necessary
2493 // (e.g. if a user inserts contents _before_ the item)
2494 // we're assuming there's only one of these, which there
2496 int erasedInsetPosition = -1;
2497 InsetList::iterator it = d->insetlist_.begin();
2498 InsetList::iterator end = d->insetlist_.end();
2499 for (; it != end; ++it)
2500 if (it->inset->lyxCode() == BIBITEM_CODE
2502 InsetBibitem * olditem = static_cast<InsetBibitem *>(it->inset);
2503 oldkey = olditem->getParam("key");
2504 oldlabel = olditem->getParam("label");
2505 erasedInsetPosition = it->pos;
2506 eraseChar(erasedInsetPosition, track_changes);
2510 // There was an InsetBibitem at the beginning, and we didn't
2511 // have to erase one.
2512 if (hasbibitem && erasedInsetPosition < 0)
2515 // There was an InsetBibitem at the beginning and we did have to
2516 // erase one. So we give its properties to the beginning inset.
2518 InsetBibitem * inset =
2519 static_cast<InsetBibitem *>(d->insetlist_.begin()->inset);
2520 if (!oldkey.empty())
2521 inset->setParam("key", oldkey);
2522 inset->setParam("label", oldlabel);
2523 return -erasedInsetPosition;
2526 // There was no inset at the beginning, so we need to create one with
2527 // the key and label of the one we erased.
2528 InsetBibitem * inset =
2529 new InsetBibitem(buffer, InsetCommandParams(BIBITEM_CODE));
2530 // restore values of previously deleted item in this par.
2531 if (!oldkey.empty())
2532 inset->setParam("key", oldkey);
2533 inset->setParam("label", oldlabel);
2534 insertInset(0, static_cast<Inset *>(inset),
2535 Change(track_changes ? Change::INSERTED : Change::UNCHANGED));
2541 void Paragraph::checkAuthors(AuthorList const & authorList)
2543 d->changes_.checkAuthors(authorList);
2547 bool Paragraph::isUnchanged(pos_type pos) const
2549 return lookupChange(pos).type == Change::UNCHANGED;
2553 bool Paragraph::isInserted(pos_type pos) const
2555 return lookupChange(pos).type == Change::INSERTED;
2559 bool Paragraph::isDeleted(pos_type pos) const
2561 return lookupChange(pos).type == Change::DELETED;
2565 InsetList const & Paragraph::insetList() const
2567 return d->insetlist_;
2571 Inset * Paragraph::releaseInset(pos_type pos)
2573 Inset * inset = d->insetlist_.release(pos);
2574 /// does not honour change tracking!
2575 eraseChar(pos, false);
2580 Inset * Paragraph::getInset(pos_type pos)
2582 return (pos < pos_type(d->text_.size()) && d->text_[pos] == META_INSET)
2583 ? d->insetlist_.get(pos) : 0;
2587 Inset const * Paragraph::getInset(pos_type pos) const
2589 return (pos < pos_type(d->text_.size()) && d->text_[pos] == META_INSET)
2590 ? d->insetlist_.get(pos) : 0;
2594 void Paragraph::changeCase(BufferParams const & bparams, pos_type pos,
2595 pos_type & right, TextCase action)
2597 // process sequences of modified characters; in change
2598 // tracking mode, this approach results in much better
2599 // usability than changing case on a char-by-char basis
2602 bool const trackChanges = bparams.trackChanges;
2604 bool capitalize = true;
2606 for (; pos < right; ++pos) {
2607 char_type oldChar = d->text_[pos];
2608 char_type newChar = oldChar;
2610 // ignore insets and don't play with deleted text!
2611 if (oldChar != META_INSET && !isDeleted(pos)) {
2613 case text_lowercase:
2614 newChar = lowercase(oldChar);
2616 case text_capitalization:
2618 newChar = uppercase(oldChar);
2622 case text_uppercase:
2623 newChar = uppercase(oldChar);
2628 if (!isLetter(pos) || isDeleted(pos)) {
2629 // permit capitalization again
2633 if (oldChar != newChar)
2636 if (oldChar == newChar || pos == right - 1) {
2637 if (oldChar != newChar) {
2638 // step behind the changing area
2641 int erasePos = pos - changes.size();
2642 for (size_t i = 0; i < changes.size(); i++) {
2643 insertChar(pos, changes[i],
2644 getFontSettings(bparams,
2647 if (!eraseChar(erasePos, trackChanges)) {
2650 ++right; // expand selection
2659 bool Paragraph::find(docstring const & str, bool cs, bool mw,
2660 pos_type pos, bool del) const
2662 int const strsize = str.length();
2664 pos_type const parsize = d->text_.size();
2665 for (i = 0; pos + i < parsize; ++i) {
2668 if (cs && str[i] != d->text_[pos + i])
2670 if (!cs && uppercase(str[i]) != uppercase(d->text_[pos + i]))
2672 if (!del && isDeleted(pos + i))
2679 // if necessary, check whether string matches word
2681 if (pos > 0 && isLetter(pos - 1))
2683 if (pos + strsize < parsize
2684 && isLetter(pos + strsize))
2692 char_type Paragraph::getChar(pos_type pos) const
2694 return d->text_[pos];
2698 pos_type Paragraph::size() const
2700 return d->text_.size();
2704 bool Paragraph::empty() const
2706 return d->text_.empty();
2710 bool Paragraph::isInset(pos_type pos) const
2712 return d->text_[pos] == META_INSET;
2716 bool Paragraph::isSeparator(pos_type pos) const
2718 //FIXME: Are we sure this can be the only separator?
2719 return d->text_[pos] == ' ';
2723 void Paragraph::deregisterWords()
2725 Private::Words::const_iterator it;
2726 WordList & wl = theWordList();
2727 for (it = d->words_.begin(); it != d->words_.end(); ++it)
2733 void Paragraph::collectWords(CursorSlice const & sl)
2736 bool inword = false;
2738 //lyxerr << "Words: ";
2739 pos_type n = size();
2740 for (pos_type pos = 0; pos != n; ++pos) {
2744 if (!isLetter(pos)) {
2753 CursorSlice from = sl;
2754 CursorSlice to = sl;
2757 from.text()->getWord(from, to, WHOLE_WORD);
2758 if (to.pos() - from.pos() < 6)
2760 docstring word = asString(from.pos(), to.pos(), false);
2761 d->words_.insert(word);
2762 //lyxerr << word << " ";
2764 //lyxerr << std::endl;
2768 void Paragraph::registerWords()
2770 Private::Words::const_iterator it;
2771 WordList & wl = theWordList();
2772 for (it = d->words_.begin(); it != d->words_.end(); ++it)
2777 void Paragraph::updateWords(CursorSlice const & sl)
2779 LASSERT(&sl.paragraph() == this, /**/);