3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Asger Alstrup
7 * \author Lars Gullik Bjønnes
8 * \author Jean-Marc Lasgouttes
9 * \author Angus Leeming
11 * \author André Pönitz
13 * \author Jürgen Vigna
15 * Full author contact details are available in file CREDITS.
20 #include "Paragraph.h"
23 #include "BufferParams.h"
27 #include "InsetList.h"
29 #include "LaTeXFeatures.h"
35 #include "OutputParams.h"
36 #include "output_latex.h"
37 #include "paragraph_funcs.h"
38 #include "ParagraphParameters.h"
40 #include "TextClass.h"
44 #include "frontends/alert.h"
45 #include "frontends/FontMetrics.h"
47 #include "insets/InsetBibitem.h"
48 #include "insets/InsetLabel.h"
50 #include "support/convert.h"
51 #include "support/debug.h"
52 #include "support/gettext.h"
53 #include "support/lstrings.h"
54 #include "support/Messages.h"
55 #include "support/textutils.h"
56 #include "support/unicode.h"
67 using support::contains;
68 using support::lowercase;
69 using support::prefixIs;
70 using support::suffixIs;
71 using support::rsplit;
72 using support::uppercase;
75 /// Inset identifier (above 0x10ffff, for ucs-4)
76 char_type const META_INSET = 0x200001;
79 /////////////////////////////////////////////////////////////////////
83 /////////////////////////////////////////////////////////////////////
85 class Paragraph::Private
89 Private(Paragraph * owner);
90 /// "Copy constructor"
91 Private(Private const &, Paragraph * owner);
94 void insertChar(pos_type pos, char_type c, Change const & change);
96 /// Output the surrogate pair formed by \p c and \p next to \p os.
97 /// \return the number of characters written.
98 int latexSurrogatePair(odocstream & os, char_type c, char_type next,
101 /// Output a space in appropriate formatting (or a surrogate pair
102 /// if the next character is a combining character).
103 /// \return whether a surrogate pair was output.
104 bool simpleTeXBlanks(OutputParams const &,
105 odocstream &, TexRow & texrow,
107 unsigned int & column,
109 Layout const & style);
111 /// Output consecutive unicode chars, belonging to the same script as
112 /// specified by the latex macro \p ltx, to \p os starting from \p i.
113 /// \return the number of characters written.
114 int writeScriptChars(odocstream & os, docstring const & ltx,
115 Change &, Encoding const &, pos_type & i);
117 /// This could go to ParagraphParameters if we want to.
118 int startTeXParParams(BufferParams const &, odocstream &, TexRow &,
121 /// This could go to ParagraphParameters if we want to.
122 int endTeXParParams(BufferParams const &, odocstream &, TexRow &,
126 void latexInset(Buffer const &, BufferParams const &,
128 TexRow & texrow, OutputParams &,
131 Font const & outerfont,
133 Change & running_change,
134 Layout const & style,
136 unsigned int & column);
139 void latexSpecialChar(
141 OutputParams & runparams,
143 Change & running_change,
144 Layout const & style,
146 unsigned int & column);
153 unsigned int & column);
155 bool latexSpecialTypewriter(
159 unsigned int & column);
161 bool latexSpecialPhrase(
164 unsigned int & column,
165 OutputParams & runparams);
168 void validate(LaTeXFeatures & features,
169 Layout const & layout) const;
171 /// Checks if the paragraph contains only text and no inset or font change.
172 bool onlyText(Buffer const & buf, Font const & outerfont,
173 pos_type initial) const;
175 /// match a string against a particular point in the paragraph
176 bool isTextAt(std::string const & str, pos_type pos) const;
178 /// Which Paragraph owns us?
182 Inset * inset_owner_;
190 static unsigned int paragraph_id;
192 ParagraphParameters params_;
194 /// position of the paragraph in the buffer. Only macros from
195 /// paragraphs strictly smaller are visible in this paragraph
196 unsigned int macrocontext_position_;
198 /// for recording and looking up changes
202 InsetList insetlist_;
208 pos_type begin_of_body_;
210 typedef docstring TextContainer;
219 using std::upper_bound;
220 using std::lower_bound;
224 // Initialization of the counter for the paragraph id's,
225 unsigned int Paragraph::Private::paragraph_id = 0;
229 struct special_phrase {
235 special_phrase const special_phrases[] = {
236 { "LyX", from_ascii("\\LyX{}"), false },
237 { "TeX", from_ascii("\\TeX{}"), true },
238 { "LaTeX2e", from_ascii("\\LaTeXe{}"), true },
239 { "LaTeX", from_ascii("\\LaTeX{}"), true },
242 size_t const phrases_nr = sizeof(special_phrases)/sizeof(special_phrase);
247 Paragraph::Private::Private(Paragraph * owner)
248 : owner_(owner), inset_owner_(0), begin_of_body_(0)
250 id_ = paragraph_id++;
251 macrocontext_position_ = 0;
256 Paragraph::Private::Private(Private const & p, Paragraph * owner)
257 : owner_(owner), inset_owner_(p.inset_owner_), fontlist_(p.fontlist_),
258 params_(p.params_), changes_(p.changes_), insetlist_(p.insetlist_),
259 layout_(p.layout_), begin_of_body_(p.begin_of_body_), text_(p.text_)
261 id_ = paragraph_id++;
265 bool Paragraph::isChanged(pos_type start, pos_type end) const
267 BOOST_ASSERT(start >= 0 && start <= size());
268 BOOST_ASSERT(end > start && end <= size() + 1);
270 return d->changes_.isChanged(start, end);
274 bool Paragraph::isMergedOnEndOfParDeletion(bool trackChanges) const {
275 // keep the logic here in sync with the logic of eraseChars()
281 Change change = d->changes_.lookup(size());
283 return change.type == Change::INSERTED && change.author == 0;
287 void Paragraph::setChange(Change const & change)
289 // beware of the imaginary end-of-par character!
290 d->changes_.set(change, 0, size() + 1);
293 * Propagate the change recursively - but not in case of DELETED!
295 * Imagine that your co-author makes changes in an existing inset. He
296 * sends your document to you and you come to the conclusion that the
297 * inset should go completely. If you erase it, LyX must not delete all
298 * text within the inset. Otherwise, the change tracked insertions of
299 * your co-author get lost and there is no way to restore them later.
301 * Conclusion: An inset's content should remain untouched if you delete it
304 if (change.type != Change::DELETED) {
305 for (pos_type pos = 0; pos < size(); ++pos) {
307 getInset(pos)->setChange(change);
313 void Paragraph::setChange(pos_type pos, Change const & change)
315 BOOST_ASSERT(pos >= 0 && pos <= size());
317 d->changes_.set(change, pos);
319 // see comment in setChange(Change const &) above
321 if (change.type != Change::DELETED &&
322 pos < size() && isInset(pos)) {
323 getInset(pos)->setChange(change);
328 Change const & Paragraph::lookupChange(pos_type pos) const
330 BOOST_ASSERT(pos >= 0 && pos <= size());
332 return d->changes_.lookup(pos);
336 void Paragraph::acceptChanges(BufferParams const & bparams, pos_type start,
339 BOOST_ASSERT(start >= 0 && start <= size());
340 BOOST_ASSERT(end > start && end <= size() + 1);
342 for (pos_type pos = start; pos < end; ++pos) {
343 switch (lookupChange(pos).type) {
344 case Change::UNCHANGED:
345 // accept changes in nested inset
346 if (pos < size() && isInset(pos))
347 getInset(pos)->acceptChanges(bparams);
351 case Change::INSERTED:
352 d->changes_.set(Change(Change::UNCHANGED), pos);
353 // also accept changes in nested inset
354 if (pos < size() && isInset(pos)) {
355 getInset(pos)->acceptChanges(bparams);
359 case Change::DELETED:
360 // Suppress access to non-existent
361 // "end-of-paragraph char"
363 eraseChar(pos, false);
374 void Paragraph::rejectChanges(BufferParams const & bparams,
375 pos_type start, pos_type end)
377 BOOST_ASSERT(start >= 0 && start <= size());
378 BOOST_ASSERT(end > start && end <= size() + 1);
380 for (pos_type pos = start; pos < end; ++pos) {
381 switch (lookupChange(pos).type) {
382 case Change::UNCHANGED:
383 // reject changes in nested inset
384 if (pos < size() && isInset(pos)) {
385 getInset(pos)->rejectChanges(bparams);
389 case Change::INSERTED:
390 // Suppress access to non-existent
391 // "end-of-paragraph char"
393 eraseChar(pos, false);
399 case Change::DELETED:
400 d->changes_.set(Change(Change::UNCHANGED), pos);
402 // Do NOT reject changes within a deleted inset!
403 // There may be insertions of a co-author inside of it!
411 void Paragraph::Private::insertChar(pos_type pos, char_type c,
412 Change const & change)
414 BOOST_ASSERT(pos >= 0 && pos <= int(text_.size()));
417 changes_.insert(change, pos);
419 // This is actually very common when parsing buffers (and
420 // maybe inserting ascii text)
421 if (pos == pos_type(text_.size())) {
422 // when appending characters, no need to update tables
427 text_.insert(text_.begin() + pos, c);
429 // Update the font table.
430 fontlist_.increasePosAfterPos(pos);
433 insetlist_.increasePosAfterPos(pos);
437 void Paragraph::insertInset(pos_type pos, Inset * inset,
438 Change const & change)
441 BOOST_ASSERT(pos >= 0 && pos <= size());
443 d->insertChar(pos, META_INSET, change);
444 BOOST_ASSERT(d->text_[pos] == META_INSET);
446 // Add a new entry in the insetlist_.
447 d->insetlist_.insert(inset, pos);
451 bool Paragraph::eraseChar(pos_type pos, bool trackChanges)
453 BOOST_ASSERT(pos >= 0 && pos <= size());
455 // keep the logic here in sync with the logic of isMergedOnEndOfParDeletion()
458 Change change = d->changes_.lookup(pos);
460 // set the character to DELETED if
461 // a) it was previously unchanged or
462 // b) it was inserted by a co-author
464 if (change.type == Change::UNCHANGED ||
465 (change.type == Change::INSERTED && change.author != 0)) {
466 setChange(pos, Change(Change::DELETED));
470 if (change.type == Change::DELETED)
474 // Don't physically access the imaginary end-of-paragraph character.
475 // eraseChar() can only mark it as DELETED. A physical deletion of
476 // end-of-par must be handled externally.
482 d->changes_.erase(pos);
484 // if it is an inset, delete the inset entry
485 if (d->text_[pos] == META_INSET)
486 d->insetlist_.erase(pos);
488 d->text_.erase(d->text_.begin() + pos);
490 // Update the fontlist_
491 d->fontlist_.erase(pos);
493 // Update the insetlist_
494 d->insetlist_.decreasePosAfterPos(pos);
500 int Paragraph::eraseChars(pos_type start, pos_type end, bool trackChanges)
502 BOOST_ASSERT(start >= 0 && start <= size());
503 BOOST_ASSERT(end >= start && end <= size() + 1);
506 for (pos_type count = end - start; count; --count) {
507 if (!eraseChar(i, trackChanges))
514 int Paragraph::Private::latexSurrogatePair(odocstream & os, char_type c,
515 char_type next, Encoding const & encoding)
517 // Writing next here may circumvent a possible font change between
518 // c and next. Since next is only output if it forms a surrogate pair
519 // with c we can ignore this:
520 // A font change inside a surrogate pair does not make sense and is
521 // hopefully impossible to input.
522 // FIXME: change tracking
523 // Is this correct WRT change tracking?
524 docstring const latex1 = encoding.latexChar(next);
525 docstring const latex2 = encoding.latexChar(c);
526 os << latex1 << '{' << latex2 << '}';
527 return latex1.length() + latex2.length() + 2;
531 bool Paragraph::Private::simpleTeXBlanks(OutputParams const & runparams,
532 odocstream & os, TexRow & texrow,
534 unsigned int & column,
536 Layout const & style)
538 if (style.pass_thru || runparams.verbatim)
541 if (i + 1 < int(text_.size())) {
542 char_type next = text_[i + 1];
543 if (Encodings::isCombiningChar(next)) {
544 Encoding const & encoding = *(runparams.encoding);
545 // This space has an accent, so we must always output it.
546 column += latexSurrogatePair(os, ' ', next, encoding) - 1;
551 if (lyxrc.plaintext_linelen > 0
552 && column > lyxrc.plaintext_linelen
554 && text_[i - 1] != ' '
555 && (i + 1 < int(text_.size()))
556 // same in FreeSpacing mode
557 && !owner_->isFreeSpacing()
558 // In typewriter mode, we want to avoid
559 // ! . ? : at the end of a line
560 && !(font.fontInfo().family() == TYPEWRITER_FAMILY
561 && (text_[i - 1] == '.'
562 || text_[i - 1] == '?'
563 || text_[i - 1] == ':'
564 || text_[i - 1] == '!'))) {
567 texrow.start(owner_->id(), i + 1);
569 } else if (style.free_spacing) {
578 int Paragraph::Private::writeScriptChars(odocstream & os,
579 docstring const & ltx,
580 Change & runningChange,
581 Encoding const & encoding,
584 // FIXME: modifying i here is not very nice...
586 // We only arrive here when a proper language for character text_[i] has
587 // not been specified (i.e., it could not be translated in the current
588 // latex encoding) and it belongs to a known script.
589 // Parameter ltx contains the latex translation of text_[i] as specified in
590 // the unicodesymbols file and is something like "\textXXX{<spec>}".
591 // The latex macro name "textXXX" specifies the script to which text_[i]
592 // belongs and we use it in order to check whether characters from the
593 // same script immediately follow, such that we can collect them in a
594 // single "\textXXX" macro. So, we have to retain "\textXXX{<spec>"
595 // for the first char but only "<spec>" for all subsequent chars.
596 docstring::size_type const brace1 = ltx.find_first_of(from_ascii("{"));
597 docstring::size_type const brace2 = ltx.find_last_of(from_ascii("}"));
598 string script = to_ascii(ltx.substr(1, brace1 - 1));
599 int length = ltx.substr(0, brace2).length();
600 os << ltx.substr(0, brace2);
601 int size = text_.size();
602 while (i + 1 < size) {
603 char_type const next = text_[i + 1];
604 // Stop here if next character belongs to another script
605 // or there is a change in change tracking status.
606 if (!Encodings::isKnownScriptChar(next, script) ||
607 runningChange != owner_->lookupChange(i + 1))
611 FontList::const_iterator cit = fontlist_.begin();
612 FontList::const_iterator end = fontlist_.end();
613 for (; cit != end; ++cit) {
614 if (cit->pos() >= i && !found) {
615 prev_font = cit->font();
618 if (cit->pos() >= i + 1)
621 // Stop here if there is a font attribute or encoding change.
622 if (found && cit != end && prev_font != cit->font())
624 docstring const latex = encoding.latexChar(next);
625 docstring::size_type const b1 =
626 latex.find_first_of(from_ascii("{"));
627 docstring::size_type const b2 =
628 latex.find_last_of(from_ascii("}"));
629 int const len = b2 - b1 - 1;
630 os << latex.substr(b1 + 1, len);
640 bool Paragraph::Private::isTextAt(string const & str, pos_type pos) const
642 pos_type const len = str.length();
644 // is the paragraph large enough?
645 if (pos + len > int(text_.size()))
648 // does the wanted text start at point?
649 for (string::size_type i = 0; i < str.length(); ++i) {
650 // Caution: direct comparison of characters works only
651 // because str is pure ASCII.
652 if (str[i] != text_[pos + i])
656 return fontlist_.hasChangeInRange(pos, len);
660 void Paragraph::Private::latexInset(Buffer const & buf,
661 BufferParams const & bparams,
664 OutputParams & runparams,
667 Font const & outerfont,
669 Change & running_change,
670 Layout const & style,
672 unsigned int & column)
674 Inset * inset = owner_->getInset(i);
677 if (style.pass_thru) {
678 inset->plaintext(buf, os, runparams);
682 // FIXME: move this to InsetNewline::latex
683 if (inset->lyxCode() == NEWLINE_CODE) {
684 // newlines are handled differently here than
685 // the default in simpleTeXSpecialChars().
686 if (!style.newline_allowed) {
690 column += running_font.latexWriteEndChanges(
691 os, bparams, runparams,
696 if (running_font.fontInfo().family() == TYPEWRITER_FAMILY)
699 basefont = owner_->getLayoutFont(bparams, outerfont);
700 running_font = basefont;
702 if (runparams.moving_arg)
707 texrow.start(owner_->id(), i + 1);
711 if (owner_->lookupChange(i).type == Change::DELETED) {
712 if( ++runparams.inDeletedInset == 1)
713 runparams.changeOfDeletedInset = owner_->lookupChange(i);
716 if (inset->canTrackChanges()) {
717 column += Changes::latexMarkChange(os, bparams, running_change,
718 Change(Change::UNCHANGED));
719 running_change = Change(Change::UNCHANGED);
723 odocstream::pos_type const len = os.tellp();
725 if (inset->forceLTR()
726 && running_font.isRightToLeft()
727 // ERT is an exception, it should be output with no decorations at all
728 && inset->lyxCode() != ERT_CODE) {
729 if (running_font.language()->lang() == "farsi")
736 // FIXME: Bug: we can have an empty font change here!
737 // if there has just been a font change, we are going to close it
738 // right now, which means stupid latex code like \textsf{}. AFAIK,
739 // this does not harm dvi output. A minor bug, thus (JMarc)
741 // Some insets cannot be inside a font change command.
742 // However, even such insets *can* be placed in \L or \R
743 // or their equivalents (for RTL language switches), so we don't
744 // close the language in those cases.
745 // ArabTeX, though, cannot handle this special behavior, it seems.
746 bool arabtex = basefont.language()->lang() == "arabic_arabtex"
747 || running_font.language()->lang() == "arabic_arabtex";
748 if (open_font && inset->noFontChange()) {
749 bool closeLanguage = arabtex
750 || basefont.isRightToLeft() == running_font.isRightToLeft();
751 unsigned int count = running_font.latexWriteEndChanges(os,
752 bparams, runparams, basefont, basefont, closeLanguage);
754 // if any font properties were closed, update the running_font,
755 // making sure, however, to leave the language as it was
757 // FIXME: probably a better way to keep track of the old
758 // language, than copying the entire font?
759 Font const copy_font(running_font);
760 basefont = owner_->getLayoutFont(bparams, outerfont);
761 running_font = basefont;
763 running_font.setLanguage(copy_font.language());
764 // leave font open if language is still open
765 open_font = (running_font.language() == basefont.language());
767 runparams.local_font = &basefont;
771 int tmp = inset->latex(buf, os, runparams);
774 if (running_font.language()->lang() == "farsi")
781 for (int j = 0; j < tmp; ++j)
784 texrow.start(owner_->id(), i + 1);
787 column += os.tellp() - len;
790 if (owner_->lookupChange(i).type == Change::DELETED)
791 --runparams.inDeletedInset;
795 void Paragraph::Private::latexSpecialChar(
797 OutputParams & runparams,
799 Change & running_change,
800 Layout const & style,
802 unsigned int & column)
804 char_type const c = text_[i];
806 if (style.pass_thru) {
808 // FIXME UNICODE: This can fail if c cannot
809 // be encoded in the current encoding.
814 if (runparams.verbatim) {
819 if (lyxrc.fontenc == "T1" && latexSpecialT1(c, os, i, column))
822 if (running_font.fontInfo().family() == TYPEWRITER_FAMILY
823 && latexSpecialTypewriter(c, os, i, column))
826 // Otherwise, we use what LaTeX provides us.
829 os << "\\textbackslash{}";
833 os << "\\textless{}";
837 os << "\\textgreater{}";
848 os << "\\char`\\\"{}";
853 case '%': case '#': case '{':
861 os << "\\textasciitilde{}";
866 os << "\\textasciicircum{}";
871 // avoid being mistaken for optional arguments
879 // Blanks are printed before font switching.
880 // Sure? I am not! (try nice-latex)
881 // I am sure it's correct. LyX might be smarter
882 // in the future, but for now, nothing wrong is
889 if (latexSpecialPhrase(os, i, column, runparams))
895 Encoding const & encoding = *(runparams.encoding);
896 if (i + 1 < int(text_.size())) {
897 char_type next = text_[i + 1];
898 if (Encodings::isCombiningChar(next)) {
899 column += latexSurrogatePair(os, c, next, encoding) - 1;
905 docstring const latex = encoding.latexChar(c);
906 if (Encodings::isKnownScriptChar(c, script)
907 && prefixIs(latex, from_ascii("\\" + script)))
908 column += writeScriptChars(os, latex,
909 running_change, encoding, i) - 1;
910 else if (latex.length() > 1 && latex[latex.length() - 1] != '}') {
911 // Prevent eating of a following
912 // space or command corruption by
913 // following characters
914 column += latex.length() + 1;
917 column += latex.length() - 1;
925 bool Paragraph::Private::latexSpecialT1(char_type const c, odocstream & os,
926 pos_type & i, unsigned int & column)
932 // In T1 encoding, these characters exist
933 // but we should avoid ligatures
934 if (i + 1 > int(text_.size()) || text_[i + 1] != c)
939 //os << "\\textcompwordmark{}";
951 bool Paragraph::Private::latexSpecialTypewriter(char_type const c, odocstream & os,
952 pos_type & i, unsigned int & column)
956 if (i + 1 < int(text_.size()) && text_[i + 1] == '-') {
957 // "--" in Typewriter mode -> "-{}-"
964 // I assume this is hack treating typewriter as verbatim
965 // FIXME UNICODE: This can fail if c cannot be encoded
966 // in the current encoding.
971 // Those characters are not directly supported.
975 case '%': case '#': case '{':
984 // With Typewriter font, these characters exist.
991 bool Paragraph::Private::latexSpecialPhrase(odocstream & os, pos_type & i,
992 unsigned int & column, OutputParams & runparams)
994 // FIXME: if we have "LaTeX" with a font
995 // change in the middle (before the 'T', then
996 // the "TeX" part is still special cased.
997 // Really we should only operate this on
998 // "words" for some definition of word
1000 for (size_t pnr = 0; pnr < phrases_nr; ++pnr) {
1001 if (!isTextAt(special_phrases[pnr].phrase, i))
1003 if (runparams.moving_arg)
1005 os << special_phrases[pnr].macro;
1006 i += special_phrases[pnr].phrase.length() - 1;
1007 column += special_phrases[pnr].macro.length() - 1;
1014 void Paragraph::Private::validate(LaTeXFeatures & features,
1015 Layout const & layout) const
1017 // check the params.
1018 if (!params_.spacing().isDefault())
1019 features.require("setspace");
1022 features.useLayout(layout.name());
1025 fontlist_.validate(features);
1027 // then the indentation
1028 if (!params_.leftIndent().zero())
1029 features.require("ParagraphLeftIndent");
1032 InsetList::const_iterator icit = insetlist_.begin();
1033 InsetList::const_iterator iend = insetlist_.end();
1034 for (; icit != iend; ++icit) {
1036 icit->inset->validate(features);
1037 if (layout.needprotect &&
1038 icit->inset->lyxCode() == FOOT_CODE)
1039 features.require("NeedLyXFootnoteCode");
1043 // then the contents
1044 for (pos_type i = 0; i < int(text_.size()) ; ++i) {
1045 for (size_t pnr = 0; pnr < phrases_nr; ++pnr) {
1046 if (!special_phrases[pnr].builtin
1047 && isTextAt(special_phrases[pnr].phrase, i)) {
1048 features.require(special_phrases[pnr].phrase);
1052 Encodings::validate(text_[i], features);
1056 /////////////////////////////////////////////////////////////////////
1060 /////////////////////////////////////////////////////////////////////
1062 Paragraph::Paragraph()
1063 : d(new Paragraph::Private(this))
1070 Paragraph::Paragraph(Paragraph const & par)
1071 : itemdepth(par.itemdepth),
1072 d(new Paragraph::Private(*par.d, this))
1077 Paragraph & Paragraph::operator=(Paragraph const & par)
1079 // needed as we will destroy the private part before copying it
1081 itemdepth = par.itemdepth;
1084 d = new Private(*par.d, this);
1090 Paragraph::~Paragraph()
1096 void Paragraph::write(Buffer const & buf, ostream & os,
1097 BufferParams const & bparams,
1098 depth_type & dth) const
1100 // The beginning or end of a deeper (i.e. nested) area?
1101 if (dth != params().depth()) {
1102 if (params().depth() > dth) {
1103 while (params().depth() > dth) {
1104 os << "\n\\begin_deeper";
1108 while (params().depth() < dth) {
1109 os << "\n\\end_deeper";
1115 // First write the layout
1116 os << "\n\\begin_layout " << to_utf8(layout()->name()) << '\n';
1120 Font font1(inherit_font, bparams.language);
1122 Change running_change = Change(Change::UNCHANGED);
1125 for (pos_type i = 0; i <= size(); ++i) {
1127 Change change = lookupChange(i);
1128 Changes::lyxMarkChange(os, column, running_change, change);
1129 running_change = change;
1134 // Write font changes
1135 Font font2 = getFontSettings(bparams, i);
1136 if (font2 != font1) {
1137 font2.lyxWriteChanges(font1, os);
1142 char_type const c = d->text_[i];
1146 Inset const * inset = getInset(i);
1148 if (inset->directWrite()) {
1149 // international char, let it write
1150 // code directly so it's shorter in
1152 inset->write(buf, os);
1156 os << "\\begin_inset ";
1157 inset->write(buf, os);
1158 os << "\n\\end_inset\n\n";
1164 os << "\n\\backslash\n";
1168 if (i + 1 < size() && d->text_[i + 1] == ' ') {
1175 if ((column > 70 && c == ' ')
1180 // this check is to amend a bug. LyX sometimes
1181 // inserts '\0' this could cause problems.
1183 std::vector<char> tmp = ucs4_to_utf8(c);
1184 tmp.push_back('\0');
1187 lyxerr << "ERROR (Paragraph::writeFile):"
1188 " NULL char in structure." << endl;
1194 os << "\n\\end_layout\n";
1198 void Paragraph::validate(LaTeXFeatures & features) const
1200 d->validate(features, *layout());
1204 void Paragraph::insert(pos_type start, docstring const & str,
1205 Font const & font, Change const & change)
1207 for (size_t i = 0, n = str.size(); i != n ; ++i)
1208 insertChar(start + i, str[i], font, change);
1212 void Paragraph::appendChar(char_type c, Font const & font,
1213 Change const & change)
1216 d->changes_.insert(change, d->text_.size());
1217 // when appending characters, no need to update tables
1218 d->text_.push_back(c);
1219 setFont(d->text_.size() - 1, font);
1223 void Paragraph::appendString(docstring const & s, Font const & font,
1224 Change const & change)
1226 pos_type end = s.size();
1227 size_t oldsize = d->text_.size();
1228 size_t newsize = oldsize + end;
1229 size_t capacity = d->text_.capacity();
1230 if (newsize >= capacity)
1231 d->text_.reserve(std::max(capacity + 100, newsize));
1233 // when appending characters, no need to update tables
1236 // FIXME: Optimize this!
1237 for (pos_type i = 0; i != end; ++i) {
1239 d->changes_.insert(change, i);
1241 d->fontlist_.set(oldsize, font);
1242 d->fontlist_.set(newsize - 1, font);
1246 void Paragraph::insertChar(pos_type pos, char_type c,
1249 d->insertChar(pos, c, Change(trackChanges ?
1250 Change::INSERTED : Change::UNCHANGED));
1254 void Paragraph::insertChar(pos_type pos, char_type c,
1255 Font const & font, bool trackChanges)
1257 d->insertChar(pos, c, Change(trackChanges ?
1258 Change::INSERTED : Change::UNCHANGED));
1263 void Paragraph::insertChar(pos_type pos, char_type c,
1264 Font const & font, Change const & change)
1266 d->insertChar(pos, c, change);
1271 void Paragraph::insertInset(pos_type pos, Inset * inset,
1272 Font const & font, Change const & change)
1274 insertInset(pos, inset, change);
1275 // Set the font/language of the inset...
1280 bool Paragraph::insetAllowed(InsetCode code)
1282 return !d->inset_owner_ || d->inset_owner_->insetAllowed(code);
1286 void Paragraph::resetFonts(Font const & font)
1288 d->fontlist_.clear();
1289 d->fontlist_.set(0, font);
1290 d->fontlist_.set(d->text_.size() - 1, font);
1293 // Gets uninstantiated font setting at position.
1294 Font const Paragraph::getFontSettings(BufferParams const & bparams,
1298 lyxerr << " pos: " << pos << " size: " << size() << endl;
1299 BOOST_ASSERT(pos <= size());
1302 FontList::const_iterator cit = d->fontlist_.fontIterator(pos);
1303 if (cit != d->fontlist_.end())
1306 if (pos == size() && !empty())
1307 return getFontSettings(bparams, pos - 1);
1309 return Font(inherit_font, getParLanguage(bparams));
1313 FontSpan Paragraph::fontSpan(pos_type pos) const
1315 BOOST_ASSERT(pos <= size());
1318 FontList::const_iterator cit = d->fontlist_.begin();
1319 FontList::const_iterator end = d->fontlist_.end();
1320 for (; cit != end; ++cit) {
1321 if (cit->pos() >= pos) {
1322 if (pos >= beginOfBody())
1323 return FontSpan(std::max(start, beginOfBody()),
1326 return FontSpan(start,
1327 std::min(beginOfBody() - 1,
1330 start = cit->pos() + 1;
1333 // This should not happen, but if so, we take no chances.
1334 //lyxerr << "Paragraph::getEndPosOfFontSpan: This should not happen!"
1336 return FontSpan(pos, pos);
1340 // Gets uninstantiated font setting at position 0
1341 Font const Paragraph::getFirstFontSettings(BufferParams const & bparams) const
1343 if (!empty() && !d->fontlist_.empty())
1344 return d->fontlist_.begin()->font();
1346 return Font(inherit_font, bparams.language);
1350 // Gets the fully instantiated font at a given position in a paragraph
1351 // This is basically the same function as Text::GetFont() in text2.cpp.
1352 // The difference is that this one is used for generating the LaTeX file,
1353 // and thus cosmetic "improvements" are disallowed: This has to deliver
1354 // the true picture of the buffer. (Asger)
1355 Font const Paragraph::getFont(BufferParams const & bparams, pos_type pos,
1356 Font const & outerfont) const
1358 BOOST_ASSERT(pos >= 0);
1360 Font font = getFontSettings(bparams, pos);
1362 pos_type const body_pos = beginOfBody();
1364 font.fontInfo().realize(d->layout_->labelfont);
1366 font.fontInfo().realize(d->layout_->font);
1368 font.fontInfo().realize(outerfont.fontInfo());
1369 font.fontInfo().realize(bparams.getFont().fontInfo());
1375 Font const Paragraph::getLabelFont
1376 (BufferParams const & bparams, Font const & outerfont) const
1378 FontInfo tmpfont = layout()->labelfont;
1379 tmpfont.realize(outerfont.fontInfo());
1380 tmpfont.realize(bparams.getFont().fontInfo());
1381 return Font(tmpfont, getParLanguage(bparams));
1385 Font const Paragraph::getLayoutFont
1386 (BufferParams const & bparams, Font const & outerfont) const
1388 FontInfo tmpfont = layout()->font;
1389 tmpfont.realize(outerfont.fontInfo());
1390 tmpfont.realize(bparams.getFont().fontInfo());
1391 return Font(tmpfont, getParLanguage(bparams));
1395 /// Returns the height of the highest font in range
1396 FontSize Paragraph::highestFontInRange
1397 (pos_type startpos, pos_type endpos, FontSize def_size) const
1399 return d->fontlist_.highestInRange(startpos, endpos, def_size);
1404 Paragraph::getUChar(BufferParams const & bparams, pos_type pos) const
1406 char_type c = d->text_[pos];
1407 if (!lyxrc.rtl_support)
1437 if (uc != c && getFontSettings(bparams, pos).isRightToLeft())
1444 void Paragraph::setFont(pos_type pos, Font const & font)
1446 BOOST_ASSERT(pos <= size());
1448 // First, reduce font against layout/label font
1449 // Update: The setCharFont() routine in text2.cpp already
1450 // reduces font, so we don't need to do that here. (Asger)
1452 d->fontlist_.set(pos, font);
1456 void Paragraph::makeSameLayout(Paragraph const & par)
1458 layout(par.layout());
1460 d->params_ = par.params();
1464 bool Paragraph::stripLeadingSpaces(bool trackChanges)
1466 if (isFreeSpacing())
1472 while (pos < size() && (isNewline(pos) || isLineSeparator(pos))) {
1473 if (eraseChar(pos, trackChanges))
1479 return count > 0 || pos > 0;
1483 bool Paragraph::hasSameLayout(Paragraph const & par) const
1485 return par.layout() == layout() && d->params_.sameLayout(par.params());
1489 depth_type Paragraph::getDepth() const
1491 return params().depth();
1495 depth_type Paragraph::getMaxDepthAfter() const
1497 if (layout()->isEnvironment())
1498 return params().depth() + 1;
1500 return params().depth();
1504 char Paragraph::getAlign() const
1506 if (params().align() == LYX_ALIGN_LAYOUT)
1507 return layout()->align;
1509 return params().align();
1513 docstring const & Paragraph::getLabelstring() const
1515 return params().labelString();
1519 // the next two functions are for the manual labels
1520 docstring const Paragraph::getLabelWidthString() const
1522 if (layout()->margintype == MARGIN_MANUAL)
1523 return params().labelWidthString();
1525 return _("Senseless with this layout!");
1529 void Paragraph::setLabelWidthString(docstring const & s)
1531 params().labelWidthString(s);
1535 docstring const Paragraph::translateIfPossible(docstring const & s,
1536 BufferParams const & bparams) const
1538 if (!support::isAscii(s) || s.empty()) {
1539 // This must be a user defined layout. We cannot translate
1540 // this, since gettext accepts only ascii keys.
1543 // Probably standard layout, try to translate
1544 Messages & m = getMessages(getParLanguage(bparams)->code());
1545 return m.get(to_ascii(s));
1549 docstring Paragraph::expandLabel(LayoutPtr const & layout,
1550 BufferParams const & bparams, bool process_appendix) const
1552 TextClass const & tclass = bparams.getTextClass();
1555 if (process_appendix && params().appendix())
1556 fmt = translateIfPossible(layout->labelstring_appendix(),
1559 fmt = translateIfPossible(layout->labelstring(), bparams);
1561 if (fmt.empty() && layout->labeltype == LABEL_COUNTER
1562 && !layout->counter.empty())
1563 fmt = "\\the" + layout->counter;
1565 // handle 'inherited level parts' in 'fmt',
1566 // i.e. the stuff between '@' in '@Section@.\arabic{subsection}'
1567 size_t const i = fmt.find('@', 0);
1568 if (i != docstring::npos) {
1569 size_t const j = fmt.find('@', i + 1);
1570 if (j != docstring::npos) {
1571 docstring parent(fmt, i + 1, j - i - 1);
1572 docstring label = from_ascii("??");
1573 if (tclass.hasLayout(parent))
1574 docstring label = expandLabel(tclass[parent], bparams,
1576 fmt = docstring(fmt, 0, i) + label
1577 + docstring(fmt, j + 1, docstring::npos);
1581 return tclass.counters().counterLabel(fmt);
1585 void Paragraph::applyLayout(LayoutPtr const & new_layout)
1588 LyXAlignment const oldAlign = params().align();
1590 if (!(oldAlign & layout()->alignpossible)) {
1591 frontend::Alert::warning(_("Alignment not permitted"),
1592 _("The new layout does not permit the alignment previously used.\nSetting to default."));
1593 params().align(LYX_ALIGN_LAYOUT);
1598 pos_type Paragraph::beginOfBody() const
1600 return d->begin_of_body_;
1604 void Paragraph::setBeginOfBody()
1606 if (layout()->labeltype != LABEL_MANUAL) {
1607 d->begin_of_body_ = 0;
1611 // Unroll the first two cycles of the loop
1612 // and remember the previous character to
1613 // remove unnecessary getChar() calls
1615 pos_type end = size();
1616 if (i < end && !isNewline(i)) {
1618 char_type previous_char = 0;
1621 previous_char = d->text_[i];
1622 if (!isNewline(i)) {
1624 while (i < end && previous_char != ' ') {
1629 previous_char = temp;
1635 d->begin_of_body_ = i;
1639 bool Paragraph::forceDefaultParagraphs() const
1641 return inInset() && inInset()->forceDefaultParagraphs(0);
1647 // paragraphs inside floats need different alignment tags to avoid
1650 bool noTrivlistCentering(InsetCode code)
1652 return code == FLOAT_CODE || code == WRAP_CODE;
1656 string correction(string const & orig)
1658 if (orig == "flushleft")
1659 return "raggedright";
1660 if (orig == "flushright")
1661 return "raggedleft";
1662 if (orig == "center")
1668 string const corrected_env(string const & suffix, string const & env,
1671 string output = suffix + "{";
1672 if (noTrivlistCentering(code))
1673 output += correction(env);
1677 if (suffix == "\\begin")
1683 void adjust_row_column(string const & str, TexRow & texrow, int & column)
1685 if (!contains(str, "\n"))
1686 column += str.size();
1690 column = rsplit(str, tmp, '\n').size();
1697 int Paragraph::Private::startTeXParParams(BufferParams const & bparams,
1698 odocstream & os, TexRow & texrow,
1699 bool moving_arg) const
1703 if (params_.noindent()) {
1704 os << "\\noindent ";
1708 LyXAlignment const curAlign = params_.align();
1710 if (curAlign == layout_->align)
1714 case LYX_ALIGN_NONE:
1715 case LYX_ALIGN_BLOCK:
1716 case LYX_ALIGN_LAYOUT:
1717 case LYX_ALIGN_SPECIAL:
1719 case LYX_ALIGN_LEFT:
1720 case LYX_ALIGN_RIGHT:
1721 case LYX_ALIGN_CENTER:
1730 case LYX_ALIGN_NONE:
1731 case LYX_ALIGN_BLOCK:
1732 case LYX_ALIGN_LAYOUT:
1733 case LYX_ALIGN_SPECIAL:
1735 case LYX_ALIGN_LEFT: {
1737 if (owner_->getParLanguage(bparams)->babel() != "hebrew")
1738 output = corrected_env("\\begin", "flushleft", owner_->ownerCode());
1740 output = corrected_env("\\begin", "flushright", owner_->ownerCode());
1741 os << from_ascii(output);
1742 adjust_row_column(output, texrow, column);
1744 } case LYX_ALIGN_RIGHT: {
1746 if (owner_->getParLanguage(bparams)->babel() != "hebrew")
1747 output = corrected_env("\\begin", "flushright", owner_->ownerCode());
1749 output = corrected_env("\\begin", "flushleft", owner_->ownerCode());
1750 os << from_ascii(output);
1751 adjust_row_column(output, texrow, column);
1753 } case LYX_ALIGN_CENTER: {
1755 output = corrected_env("\\begin", "center", owner_->ownerCode());
1756 os << from_ascii(output);
1757 adjust_row_column(output, texrow, column);
1766 int Paragraph::Private::endTeXParParams(BufferParams const & bparams,
1767 odocstream & os, TexRow & texrow,
1768 bool moving_arg) const
1772 switch (params_.align()) {
1773 case LYX_ALIGN_NONE:
1774 case LYX_ALIGN_BLOCK:
1775 case LYX_ALIGN_LAYOUT:
1776 case LYX_ALIGN_SPECIAL:
1778 case LYX_ALIGN_LEFT:
1779 case LYX_ALIGN_RIGHT:
1780 case LYX_ALIGN_CENTER:
1788 switch (params_.align()) {
1789 case LYX_ALIGN_NONE:
1790 case LYX_ALIGN_BLOCK:
1791 case LYX_ALIGN_LAYOUT:
1792 case LYX_ALIGN_SPECIAL:
1794 case LYX_ALIGN_LEFT: {
1796 if (owner_->getParLanguage(bparams)->babel() != "hebrew")
1797 output = corrected_env("\n\\par\\end", "flushleft", owner_->ownerCode());
1799 output = corrected_env("\n\\par\\end", "flushright", owner_->ownerCode());
1800 os << from_ascii(output);
1801 adjust_row_column(output, texrow, column);
1803 } case LYX_ALIGN_RIGHT: {
1805 if (owner_->getParLanguage(bparams)->babel() != "hebrew")
1806 output = corrected_env("\n\\par\\end", "flushright", owner_->ownerCode());
1808 output = corrected_env("\n\\par\\end", "flushleft", owner_->ownerCode());
1809 os << from_ascii(output);
1810 adjust_row_column(output, texrow, column);
1812 } case LYX_ALIGN_CENTER: {
1814 output = corrected_env("\n\\par\\end", "center", owner_->ownerCode());
1815 os << from_ascii(output);
1816 adjust_row_column(output, texrow, column);
1825 // This one spits out the text of the paragraph
1826 bool Paragraph::latex(Buffer const & buf,
1827 BufferParams const & bparams,
1828 Font const & outerfont,
1829 odocstream & os, TexRow & texrow,
1830 OutputParams const & runparams) const
1832 LYXERR(Debug::LATEX, "SimpleTeXOnePar... " << this);
1834 bool return_value = false;
1838 // well we have to check if we are in an inset with unlimited
1839 // length (all in one row) if that is true then we don't allow
1840 // any special options in the paragraph and also we don't allow
1841 // any environment other than the default layout of the text class
1843 bool asdefault = forceDefaultParagraphs();
1846 style = bparams.getTextClass().defaultLayout();
1851 // Current base font for all inherited font changes, without any
1852 // change caused by an individual character, except for the language:
1853 // It is set to the language of the first character.
1854 // As long as we are in the label, this font is the base font of the
1855 // label. Before the first body character it is set to the base font
1859 // Maybe we have to create a optional argument.
1860 pos_type body_pos = beginOfBody();
1861 unsigned int column = 0;
1864 // the optional argument is kept in curly brackets in
1865 // case it contains a ']'
1868 basefont = getLabelFont(bparams, outerfont);
1870 basefont = getLayoutFont(bparams, outerfont);
1873 // Which font is currently active?
1874 Font running_font(basefont);
1875 // Do we have an open font change?
1876 bool open_font = false;
1878 Change runningChange = Change(Change::UNCHANGED);
1880 texrow.start(id(), 0);
1882 // if the paragraph is empty, the loop will not be entered at all
1884 if (style->isCommand()) {
1889 column += d->startTeXParParams(bparams, os, texrow,
1890 runparams.moving_arg);
1893 for (pos_type i = 0; i < size(); ++i) {
1894 // First char in paragraph or after label?
1895 if (i == body_pos) {
1898 column += running_font.latexWriteEndChanges(
1899 os, bparams, runparams,
1900 basefont, basefont);
1903 basefont = getLayoutFont(bparams, outerfont);
1904 running_font = basefont;
1906 column += Changes::latexMarkChange(os, bparams,
1907 runningChange, Change(Change::UNCHANGED));
1908 runningChange = Change(Change::UNCHANGED);
1913 if (style->isCommand()) {
1919 column += d->startTeXParParams(bparams, os,
1921 runparams.moving_arg);
1924 Change const & change = runparams.inDeletedInset ? runparams.changeOfDeletedInset
1927 if (bparams.outputChanges && runningChange != change) {
1929 column += running_font.latexWriteEndChanges(
1930 os, bparams, runparams, basefont, basefont);
1933 basefont = getLayoutFont(bparams, outerfont);
1934 running_font = basefont;
1936 column += Changes::latexMarkChange(os, bparams, runningChange, change);
1937 runningChange = change;
1940 // do not output text which is marked deleted
1941 // if change tracking output is disabled
1942 if (!bparams.outputChanges && change.type == Change::DELETED) {
1948 // Fully instantiated font
1949 Font const font = getFont(bparams, i, outerfont);
1951 Font const last_font = running_font;
1953 // Do we need to close the previous font?
1955 (font != running_font ||
1956 font.language() != running_font.language()))
1958 column += running_font.latexWriteEndChanges(
1959 os, bparams, runparams, basefont,
1960 (i == body_pos-1) ? basefont : font);
1961 running_font = basefont;
1965 // Switch file encoding if necessary (and allowed)
1966 if (!runparams.verbatim &&
1967 runparams.encoding->package() == Encoding::inputenc &&
1968 font.language()->encoding()->package() == Encoding::inputenc) {
1969 std::pair<bool, int> const enc_switch = switchEncoding(os, bparams,
1970 runparams.moving_arg, *(runparams.encoding),
1971 *(font.language()->encoding()));
1972 if (enc_switch.first) {
1973 column += enc_switch.second;
1974 runparams.encoding = font.language()->encoding();
1978 char_type const c = d->text_[i];
1980 // Do we need to change font?
1981 if ((font != running_font ||
1982 font.language() != running_font.language()) &&
1985 odocstringstream ods;
1986 column += font.latexWriteStartChanges(ods, bparams,
1987 runparams, basefont,
1989 running_font = font;
1991 docstring fontchange = ods.str();
1992 // check if the fontchange ends with a trailing blank
1993 // (like "\small " (see bug 3382)
1994 if (suffixIs(fontchange, ' ') && c == ' ')
1995 os << fontchange.substr(0, fontchange.size() - 1)
1996 << from_ascii("{}");
2002 // FIXME: integrate this case in latexSpecialChar
2003 // Do not print the separation of the optional argument
2004 // if style->pass_thru is false. This works because
2005 // latexSpecialChar ignores spaces if
2006 // style->pass_thru is false.
2007 if (i != body_pos - 1) {
2008 if (d->simpleTeXBlanks(
2009 runparams, os, texrow,
2010 i, column, font, *style)) {
2011 // A surrogate pair was output. We
2012 // must not call latexSpecialChar
2013 // in this iteration, since it would output
2014 // the combining character again.
2021 OutputParams rp = runparams;
2022 rp.free_spacing = style->free_spacing;
2023 rp.local_font = &font;
2024 rp.intitle = style->intitle;
2026 // Two major modes: LaTeX or plain
2027 // Handle here those cases common to both modes
2028 // and then split to handle the two modes separately.
2029 if (c == META_INSET)
2030 d->latexInset(buf, bparams, os,
2031 texrow, rp, running_font,
2032 basefont, outerfont, open_font,
2033 runningChange, *style, i, column);
2035 d->latexSpecialChar(os, rp, running_font, runningChange,
2038 // Set the encoding to that returned from simpleTeXSpecialChars (see
2039 // comment for encoding member in OutputParams.h)
2040 runparams.encoding = rp.encoding;
2043 // If we have an open font definition, we have to close it
2045 #ifdef FIXED_LANGUAGE_END_DETECTION
2048 .latexWriteEndChanges(os, bparams, runparams,
2050 next_->getFont(bparams, 0, outerfont));
2052 running_font.latexWriteEndChanges(os, bparams,
2053 runparams, basefont, basefont);
2056 //FIXME: For now we ALWAYS have to close the foreign font settings if they are
2057 //FIXME: there as we start another \selectlanguage with the next paragraph if
2058 //FIXME: we are in need of this. This should be fixed sometime (Jug)
2059 running_font.latexWriteEndChanges(os, bparams, runparams,
2060 basefont, basefont);
2064 column += Changes::latexMarkChange(os, bparams, runningChange, Change(Change::UNCHANGED));
2066 // Needed if there is an optional argument but no contents.
2067 if (body_pos > 0 && body_pos == size()) {
2069 return_value = false;
2073 column += d->endTeXParParams(bparams, os, texrow,
2074 runparams.moving_arg);
2077 LYXERR(Debug::LATEX, "SimpleTeXOnePar...done " << this);
2078 return return_value;
2082 bool Paragraph::emptyTag() const
2084 for (pos_type i = 0; i < size(); ++i) {
2086 Inset const * inset = getInset(i);
2087 InsetCode lyx_code = inset->lyxCode();
2088 if (lyx_code != TOC_CODE &&
2089 lyx_code != INCLUDE_CODE &&
2090 lyx_code != GRAPHICS_CODE &&
2091 lyx_code != ERT_CODE &&
2092 lyx_code != LISTINGS_CODE &&
2093 lyx_code != FLOAT_CODE &&
2094 lyx_code != TABULAR_CODE) {
2098 char_type c = d->text_[i];
2099 if (c != ' ' && c != '\t')
2107 string Paragraph::getID(Buffer const & buf, OutputParams const & runparams) const
2109 for (pos_type i = 0; i < size(); ++i) {
2111 Inset const * inset = getInset(i);
2112 InsetCode lyx_code = inset->lyxCode();
2113 if (lyx_code == LABEL_CODE) {
2114 InsetLabel const * const il = static_cast<InsetLabel const *>(inset);
2115 docstring const & id = il->getParam("name");
2116 return "id='" + to_utf8(sgml::cleanID(buf, runparams, id)) + "'";
2125 pos_type Paragraph::getFirstWord(Buffer const & buf, odocstream & os, OutputParams const & runparams) const
2128 for (i = 0; i < size(); ++i) {
2130 Inset const * inset = getInset(i);
2131 inset->docbook(buf, os, runparams);
2133 char_type c = d->text_[i];
2136 os << sgml::escapeChar(c);
2143 bool Paragraph::Private::onlyText(Buffer const & buf, Font const & outerfont, pos_type initial) const
2146 pos_type size = text_.size();
2147 for (pos_type i = initial; i < size; ++i) {
2148 Font font = owner_->getFont(buf.params(), i, outerfont);
2149 if (text_[i] == META_INSET)
2151 if (i != initial && font != font_old)
2160 void Paragraph::simpleDocBookOnePar(Buffer const & buf,
2162 OutputParams const & runparams,
2163 Font const & outerfont,
2164 pos_type initial) const
2166 bool emph_flag = false;
2168 LayoutPtr const & style = layout();
2170 style->labeltype == LABEL_MANUAL ? style->labelfont : style->font;
2172 if (style->pass_thru && !d->onlyText(buf, outerfont, initial))
2175 // parsing main loop
2176 for (pos_type i = initial; i < size(); ++i) {
2177 Font font = getFont(buf.params(), i, outerfont);
2179 // handle <emphasis> tag
2180 if (font_old.emph() != font.fontInfo().emph()) {
2181 if (font.fontInfo().emph() == FONT_ON) {
2184 } else if (i != initial) {
2185 os << "</emphasis>";
2191 Inset const * inset = getInset(i);
2192 inset->docbook(buf, os, runparams);
2194 char_type c = d->text_[i];
2196 if (style->pass_thru)
2199 os << sgml::escapeChar(c);
2201 font_old = font.fontInfo();
2205 os << "</emphasis>";
2208 if (style->free_spacing)
2210 if (style->pass_thru && !d->onlyText(buf, outerfont, initial))
2215 bool Paragraph::isHfill(pos_type pos) const
2218 && getInset(pos)->lyxCode() == HFILL_CODE;
2222 bool Paragraph::isNewline(pos_type pos) const
2225 && getInset(pos)->lyxCode() == NEWLINE_CODE;
2229 bool Paragraph::isLineSeparator(pos_type pos) const
2231 char_type const c = d->text_[pos];
2232 return isLineSeparatorChar(c)
2233 || (c == META_INSET && getInset(pos) &&
2234 getInset(pos)->isLineSeparator());
2238 /// Used by the spellchecker
2239 bool Paragraph::isLetter(pos_type pos) const
2242 return getInset(pos)->isLetter();
2244 char_type const c = d->text_[pos];
2245 return isLetterChar(c) || isDigit(c);
2251 Paragraph::getParLanguage(BufferParams const & bparams) const
2254 return getFirstFontSettings(bparams).language();
2255 // FIXME: we should check the prev par as well (Lgb)
2256 return bparams.language;
2260 bool Paragraph::isRTL(BufferParams const & bparams) const
2262 return lyxrc.rtl_support
2263 && getParLanguage(bparams)->rightToLeft()
2264 && ownerCode() != ERT_CODE
2265 && ownerCode() != LISTINGS_CODE;
2269 void Paragraph::changeLanguage(BufferParams const & bparams,
2270 Language const * from, Language const * to)
2272 // change language including dummy font change at the end
2273 for (pos_type i = 0; i <= size(); ++i) {
2274 Font font = getFontSettings(bparams, i);
2275 if (font.language() == from) {
2276 font.setLanguage(to);
2283 bool Paragraph::isMultiLingual(BufferParams const & bparams) const
2285 Language const * doc_language = bparams.language;
2286 FontList::const_iterator cit = d->fontlist_.begin();
2287 FontList::const_iterator end = d->fontlist_.end();
2289 for (; cit != end; ++cit)
2290 if (cit->font().language() != ignore_language &&
2291 cit->font().language() != latex_language &&
2292 cit->font().language() != doc_language)
2298 // Convert the paragraph to a string.
2299 // Used for building the table of contents
2300 docstring const Paragraph::asString(Buffer const & buffer, bool label) const
2302 return asString(buffer, 0, size(), label);
2306 docstring const Paragraph::asString(Buffer const & buffer,
2307 pos_type beg, pos_type end, bool label) const
2310 odocstringstream os;
2312 if (beg == 0 && label && !params().labelString().empty())
2313 os << params().labelString() << ' ';
2315 for (pos_type i = beg; i < end; ++i) {
2316 char_type const c = d->text_[i];
2319 else if (c == META_INSET)
2320 getInset(i)->textString(buffer, os);
2327 void Paragraph::setInsetOwner(Inset * inset)
2329 d->inset_owner_ = inset;
2333 int Paragraph::id() const
2339 LayoutPtr const & Paragraph::layout() const
2345 void Paragraph::layout(LayoutPtr const & new_layout)
2347 d->layout_ = new_layout;
2351 Inset * Paragraph::inInset() const
2353 return d->inset_owner_;
2357 InsetCode Paragraph::ownerCode() const
2359 return d->inset_owner_ ?
2360 d->inset_owner_->lyxCode() : NO_CODE;
2364 ParagraphParameters & Paragraph::params()
2370 ParagraphParameters const & Paragraph::params() const
2376 bool Paragraph::isFreeSpacing() const
2378 if (layout()->free_spacing)
2380 return d->inset_owner_ && d->inset_owner_->isFreeSpacing();
2384 bool Paragraph::allowEmpty() const
2386 if (layout()->keepempty)
2388 return d->inset_owner_ && d->inset_owner_->allowEmpty();
2392 char_type Paragraph::transformChar(char_type c, pos_type pos) const
2394 if (!Encodings::is_arabic(c))
2397 char_type prev_char = ' ';
2398 char_type next_char = ' ';
2400 for (pos_type i = pos - 1; i >= 0; --i) {
2401 char_type const par_char = d->text_[i];
2402 if (!Encodings::isComposeChar_arabic(par_char)) {
2403 prev_char = par_char;
2408 for (pos_type i = pos + 1, end = size(); i < end; ++i) {
2409 char_type const par_char = d->text_[i];
2410 if (!Encodings::isComposeChar_arabic(par_char)) {
2411 next_char = par_char;
2416 if (Encodings::is_arabic(next_char)) {
2417 if (Encodings::is_arabic(prev_char) &&
2418 !Encodings::is_arabic_special(prev_char))
2419 return Encodings::transformChar(c, Encodings::FORM_MEDIAL);
2421 return Encodings::transformChar(c, Encodings::FORM_INITIAL);
2423 if (Encodings::is_arabic(prev_char) &&
2424 !Encodings::is_arabic_special(prev_char))
2425 return Encodings::transformChar(c, Encodings::FORM_FINAL);
2427 return Encodings::transformChar(c, Encodings::FORM_ISOLATED);
2432 int Paragraph::checkBiblio(bool track_changes)
2435 //This is getting more and more a mess. ...We really should clean
2436 //up this bibitem issue for 1.6. See also bug 2743.
2438 // Add bibitem insets if necessary
2439 if (layout()->labeltype != LABEL_BIBLIO)
2442 bool hasbibitem = !d->insetlist_.empty()
2443 // Insist on it being in pos 0
2444 && d->text_[0] == META_INSET
2445 && d->insetlist_.begin()->inset->lyxCode() == BIBITEM_CODE;
2450 // remove a bibitem in pos != 0
2451 // restore it later in pos 0 if necessary
2452 // (e.g. if a user inserts contents _before_ the item)
2453 // we're assuming there's only one of these, which there
2455 int erasedInsetPosition = -1;
2456 InsetList::iterator it = d->insetlist_.begin();
2457 InsetList::iterator end = d->insetlist_.end();
2458 for (; it != end; ++it)
2459 if (it->inset->lyxCode() == BIBITEM_CODE
2461 InsetBibitem * olditem = static_cast<InsetBibitem *>(it->inset);
2462 oldkey = olditem->getParam("key");
2463 oldlabel = olditem->getParam("label");
2464 erasedInsetPosition = it->pos;
2465 eraseChar(erasedInsetPosition, track_changes);
2469 //There was an InsetBibitem at the beginning, and we didn't
2470 //have to erase one.
2471 if (hasbibitem && erasedInsetPosition < 0)
2474 //There was an InsetBibitem at the beginning and we did have to
2475 //erase one. So we give its properties to the beginning inset.
2477 InsetBibitem * inset =
2478 static_cast<InsetBibitem *>(d->insetlist_.begin()->inset);
2479 if (!oldkey.empty())
2480 inset->setParam("key", oldkey);
2481 inset->setParam("label", oldlabel);
2482 return -erasedInsetPosition;
2485 //There was no inset at the beginning, so we need to create one with
2486 //the key and label of the one we erased.
2487 InsetBibitem * inset(new InsetBibitem(InsetCommandParams(BIBITEM_CODE)));
2488 // restore values of previously deleted item in this par.
2489 if (!oldkey.empty())
2490 inset->setParam("key", oldkey);
2491 inset->setParam("label", oldlabel);
2492 insertInset(0, static_cast<Inset *>(inset),
2493 Change(track_changes ? Change::INSERTED : Change::UNCHANGED));
2499 unsigned int Paragraph::macrocontextPosition() const
2501 return d->macrocontext_position_;
2505 void Paragraph::setMacrocontextPosition(unsigned int pos)
2507 d->macrocontext_position_ = pos;
2511 void Paragraph::checkAuthors(AuthorList const & authorList)
2513 d->changes_.checkAuthors(authorList);
2517 bool Paragraph::isUnchanged(pos_type pos) const
2519 return lookupChange(pos).type == Change::UNCHANGED;
2523 bool Paragraph::isInserted(pos_type pos) const
2525 return lookupChange(pos).type == Change::INSERTED;
2529 bool Paragraph::isDeleted(pos_type pos) const
2531 return lookupChange(pos).type == Change::DELETED;
2535 InsetList const & Paragraph::insetList() const
2537 return d->insetlist_;
2541 Inset * Paragraph::releaseInset(pos_type pos)
2543 Inset * inset = d->insetlist_.release(pos);
2544 /// does not honour change tracking!
2545 eraseChar(pos, false);
2550 Inset * Paragraph::getInset(pos_type pos)
2552 return d->insetlist_.get(pos);
2556 Inset const * Paragraph::getInset(pos_type pos) const
2558 return d->insetlist_.get(pos);
2562 void Paragraph::changeCase(BufferParams const & bparams, pos_type pos,
2563 pos_type right, TextCase action)
2565 // process sequences of modified characters; in change
2566 // tracking mode, this approach results in much better
2567 // usability than changing case on a char-by-char basis
2570 bool const trackChanges = bparams.trackChanges;
2572 bool capitalize = true;
2574 for (; pos < right; ++pos) {
2575 char_type oldChar = d->text_[pos];
2576 char_type newChar = oldChar;
2578 // ignore insets and don't play with deleted text!
2579 if (oldChar != META_INSET && !isDeleted(pos)) {
2581 case text_lowercase:
2582 newChar = lowercase(oldChar);
2584 case text_capitalization:
2586 newChar = uppercase(oldChar);
2590 case text_uppercase:
2591 newChar = uppercase(oldChar);
2596 if (!isLetter(pos) || isDeleted(pos)) {
2597 // permit capitalization again
2601 if (oldChar != newChar)
2604 if (oldChar == newChar || pos == right - 1) {
2605 if (oldChar != newChar) {
2606 // step behind the changing area
2609 int erasePos = pos - changes.size();
2610 for (size_t i = 0; i < changes.size(); i++) {
2611 insertChar(pos, changes[i],
2612 getFontSettings(bparams,
2615 if (!eraseChar(erasePos, trackChanges)) {
2618 ++right; // expand selection
2627 bool Paragraph::find(docstring const & str, bool cs, bool mw,
2628 pos_type pos, bool del) const
2630 int const strsize = str.length();
2632 pos_type const parsize = d->text_.size();
2633 for (i = 0; pos + i < parsize; ++i) {
2636 if (cs && str[i] != d->text_[pos + i])
2638 if (!cs && uppercase(str[i]) != uppercase(d->text_[pos + i]))
2640 if (!del && isDeleted(pos + i))
2647 // if necessary, check whether string matches word
2649 if (pos > 0 && isLetter(pos - 1))
2651 if (pos + strsize < parsize
2652 && isLetter(pos + strsize))
2660 char_type Paragraph::getChar(pos_type pos) const
2662 return d->text_[pos];
2666 pos_type Paragraph::size() const
2668 return d->text_.size();
2672 bool Paragraph::empty() const
2674 return d->text_.empty();
2678 bool Paragraph::isInset(pos_type pos) const
2680 return d->text_[pos] == META_INSET;
2684 bool Paragraph::isSeparator(pos_type pos) const
2686 //FIXME: Are we sure this can be the only separator?
2687 return d->text_[pos] == ' ';