3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Asger Alstrup
7 * \author Lars Gullik Bjønnes
8 * \author Jean-Marc Lasgouttes
9 * \author Angus Leeming
11 * \author André Pönitz
13 * \author Jürgen Vigna
15 * Full author contact details are available in file CREDITS.
20 #include "Paragraph.h"
23 #include "BufferParams.h"
29 #include "InsetList.h"
31 #include "LaTeXFeatures.h"
38 #include "OutputParams.h"
39 #include "output_latex.h"
40 #include "paragraph_funcs.h"
41 #include "ParagraphParameters.h"
46 #include "frontends/alert.h"
47 #include "frontends/FontMetrics.h"
49 #include "insets/InsetBibitem.h"
50 #include "insets/InsetLabel.h"
51 #include "insets/InsetOptArg.h"
53 #include "support/lstrings.h"
54 #include "support/textutils.h"
55 #include "support/convert.h"
56 #include "support/unicode.h"
67 using support::contains;
68 using support::lowercase;
69 using support::prefixIs;
70 using support::suffixIs;
71 using support::rsplit;
72 using support::uppercase;
75 /// Inset identifier (above 0x10ffff, for ucs-4)
76 char_type const META_INSET = 0x200001;
79 /////////////////////////////////////////////////////////////////////
83 /////////////////////////////////////////////////////////////////////
85 class Paragraph::Private
89 Private(Paragraph * owner);
90 /// "Copy constructor"
91 Private(Private const &, Paragraph * owner);
94 void insertChar(pos_type pos, char_type c, Change const & change);
96 /// Output the surrogate pair formed by \p c and \p next to \p os.
97 /// \return the number of characters written.
98 int latexSurrogatePair(odocstream & os, char_type c, char_type next,
101 /// Output a space in appropriate formatting (or a surrogate pair
102 /// if the next character is a combining character).
103 /// \return whether a surrogate pair was output.
104 bool simpleTeXBlanks(OutputParams const &,
105 odocstream &, TexRow & texrow,
107 unsigned int & column,
109 Layout const & style);
111 /// Output consecutive unicode chars, belonging to the same script as
112 /// specified by the latex macro \p ltx, to \p os starting from \p 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)
708 texrow.start(owner_->id(), i + 1);
712 if (owner_->lookupChange(i).type == Change::DELETED) {
713 if( ++runparams.inDeletedInset == 1)
714 runparams.changeOfDeletedInset = owner_->lookupChange(i);
717 if (inset->canTrackChanges()) {
718 column += Changes::latexMarkChange(os, bparams, running_change,
719 Change(Change::UNCHANGED));
720 running_change = Change(Change::UNCHANGED);
724 odocstream::pos_type const len = os.tellp();
726 if ((inset->lyxCode() == GRAPHICS_CODE
727 || inset->lyxCode() == MATH_CODE
728 || inset->lyxCode() == HYPERLINK_CODE)
729 && running_font.isRightToLeft()) {
730 if (running_font.language()->lang() == "farsi")
737 // FIXME: Bug: we can have an empty font change here!
738 // if there has just been a font change, we are going to close it
739 // right now, which means stupid latex code like \textsf{}. AFAIK,
740 // this does not harm dvi output. A minor bug, thus (JMarc)
742 // Some insets cannot be inside a font change command.
743 // However, even such insets *can* be placed in \L or \R
744 // or their equivalents (for RTL language switches), so we don't
745 // close the language in those cases.
746 // ArabTeX, though, cannot handle this special behavior, it seems.
747 bool arabtex = basefont.language()->lang() == "arabic_arabtex"
748 || running_font.language()->lang() == "arabic_arabtex";
749 if (open_font && inset->noFontChange()) {
750 bool closeLanguage = arabtex
751 || basefont.isRightToLeft() == running_font.isRightToLeft();
752 unsigned int count = running_font.latexWriteEndChanges(os,
753 bparams, runparams, basefont, basefont, closeLanguage);
755 // if any font properties were closed, update the running_font,
756 // making sure, however, to leave the language as it was
758 // FIXME: probably a better way to keep track of the old
759 // language, than copying the entire font?
760 Font const copy_font(running_font);
761 basefont = owner_->getLayoutFont(bparams, outerfont);
762 running_font = basefont;
764 running_font.setLanguage(copy_font.language());
765 // leave font open if language is still open
766 open_font = (running_font.language() == basefont.language());
768 runparams.local_font = &basefont;
772 int tmp = inset->latex(buf, os, runparams);
775 if (running_font.language()->lang() == "farsi")
782 for (int j = 0; j < tmp; ++j)
785 texrow.start(owner_->id(), i + 1);
788 column += os.tellp() - len;
791 if (owner_->lookupChange(i).type == Change::DELETED)
792 --runparams.inDeletedInset;
796 void Paragraph::Private::latexSpecialChar(
798 OutputParams & runparams,
800 Change & running_change,
801 Layout const & style,
803 unsigned int & column)
805 char_type const c = text_[i];
807 if (style.pass_thru) {
809 // FIXME UNICODE: This can fail if c cannot
810 // be encoded in the current encoding.
815 if (runparams.verbatim) {
820 if (lyxrc.fontenc == "T1" && latexSpecialT1(c, os, i, column))
823 if (running_font.fontInfo().family() == TYPEWRITER_FAMILY
824 && latexSpecialTypewriter(c, os, i, column))
827 // Otherwise, we use what LaTeX provides us.
830 os << "\\textbackslash{}";
834 os << "\\textless{}";
838 os << "\\textgreater{}";
849 os << "\\char`\\\"{}";
854 case '%': case '#': case '{':
862 os << "\\textasciitilde{}";
867 os << "\\textasciicircum{}";
872 // avoid being mistaken for optional arguments
880 // Blanks are printed before font switching.
881 // Sure? I am not! (try nice-latex)
882 // I am sure it's correct. LyX might be smarter
883 // in the future, but for now, nothing wrong is
890 if (latexSpecialPhrase(os, i, column, runparams))
896 Encoding const & encoding = *(runparams.encoding);
897 if (i + 1 < int(text_.size())) {
898 char_type next = text_[i + 1];
899 if (Encodings::isCombiningChar(next)) {
900 column += latexSurrogatePair(os, c, next, encoding) - 1;
906 docstring const latex = encoding.latexChar(c);
907 if (Encodings::isKnownScriptChar(c, script)
908 && prefixIs(latex, from_ascii("\\" + script)))
909 column += writeScriptChars(os, latex,
910 running_change, encoding, i) - 1;
911 else if (latex.length() > 1 && latex[latex.length() - 1] != '}') {
912 // Prevent eating of a following
913 // space or command corruption by
914 // following characters
915 column += latex.length() + 1;
918 column += latex.length() - 1;
926 bool Paragraph::Private::latexSpecialT1(char_type const c, odocstream & os,
927 pos_type & i, unsigned int & column)
933 // In T1 encoding, these characters exist
934 // but we should avoid ligatures
935 if (i + 1 > int(text_.size()) || text_[i + 1] != c)
940 //os << "\\textcompwordmark{}";
952 bool Paragraph::Private::latexSpecialTypewriter(char_type const c, odocstream & os,
953 pos_type & i, unsigned int & column)
957 if (i + 1 < int(text_.size()) && text_[i + 1] == '-') {
958 // "--" in Typewriter mode -> "-{}-"
965 // I assume this is hack treating typewriter as verbatim
966 // FIXME UNICODE: This can fail if c cannot be encoded
967 // in the current encoding.
972 // Those characters are not directly supported.
976 case '%': case '#': case '{':
985 // With Typewriter font, these characters exist.
992 bool Paragraph::Private::latexSpecialPhrase(odocstream & os, pos_type & i,
993 unsigned int & column, OutputParams & runparams)
995 // FIXME: if we have "LaTeX" with a font
996 // change in the middle (before the 'T', then
997 // the "TeX" part is still special cased.
998 // Really we should only operate this on
999 // "words" for some definition of word
1001 for (size_t pnr = 0; pnr < phrases_nr; ++pnr) {
1002 if (!isTextAt(special_phrases[pnr].phrase, i))
1004 if (runparams.moving_arg)
1006 os << special_phrases[pnr].macro;
1007 i += special_phrases[pnr].phrase.length() - 1;
1008 column += special_phrases[pnr].macro.length() - 1;
1015 void Paragraph::Private::validate(LaTeXFeatures & features,
1016 Layout const & layout) const
1018 // check the params.
1019 if (!params_.spacing().isDefault())
1020 features.require("setspace");
1023 features.useLayout(layout.name());
1026 fontlist_.validate(features);
1028 // then the indentation
1029 if (!params_.leftIndent().zero())
1030 features.require("ParagraphLeftIndent");
1033 InsetList::const_iterator icit = insetlist_.begin();
1034 InsetList::const_iterator iend = insetlist_.end();
1035 for (; icit != iend; ++icit) {
1037 icit->inset->validate(features);
1038 if (layout.needprotect &&
1039 icit->inset->lyxCode() == FOOT_CODE)
1040 features.require("NeedLyXFootnoteCode");
1044 // then the contents
1045 for (pos_type i = 0; i < int(text_.size()) ; ++i) {
1046 for (size_t pnr = 0; pnr < phrases_nr; ++pnr) {
1047 if (!special_phrases[pnr].builtin
1048 && isTextAt(special_phrases[pnr].phrase, i)) {
1049 features.require(special_phrases[pnr].phrase);
1053 Encodings::validate(text_[i], features);
1057 /////////////////////////////////////////////////////////////////////
1061 /////////////////////////////////////////////////////////////////////
1063 Paragraph::Paragraph()
1064 : d(new Paragraph::Private(this))
1071 Paragraph::Paragraph(Paragraph const & par)
1072 : itemdepth(par.itemdepth),
1073 d(new Paragraph::Private(*par.d, this))
1078 Paragraph & Paragraph::operator=(Paragraph const & par)
1080 // needed as we will destroy the private part before copying it
1082 itemdepth = par.itemdepth;
1085 d = new Private(*par.d, this);
1091 Paragraph::~Paragraph()
1097 void Paragraph::write(Buffer const & buf, ostream & os,
1098 BufferParams const & bparams,
1099 depth_type & dth) const
1101 // The beginning or end of a deeper (i.e. nested) area?
1102 if (dth != params().depth()) {
1103 if (params().depth() > dth) {
1104 while (params().depth() > dth) {
1105 os << "\n\\begin_deeper";
1109 while (params().depth() < dth) {
1110 os << "\n\\end_deeper";
1116 // First write the layout
1117 os << "\n\\begin_layout " << to_utf8(layout()->name()) << '\n';
1121 Font font1(inherit_font, bparams.language);
1123 Change running_change = Change(Change::UNCHANGED);
1126 for (pos_type i = 0; i <= size(); ++i) {
1128 Change change = lookupChange(i);
1129 Changes::lyxMarkChange(os, column, running_change, change);
1130 running_change = change;
1135 // Write font changes
1136 Font font2 = getFontSettings(bparams, i);
1137 if (font2 != font1) {
1138 font2.lyxWriteChanges(font1, os);
1143 char_type const c = d->text_[i];
1147 Inset const * inset = getInset(i);
1149 if (inset->directWrite()) {
1150 // international char, let it write
1151 // code directly so it's shorter in
1153 inset->write(buf, os);
1157 os << "\\begin_inset ";
1158 inset->write(buf, os);
1159 os << "\n\\end_inset\n\n";
1165 os << "\n\\backslash\n";
1169 if (i + 1 < size() && d->text_[i + 1] == ' ') {
1176 if ((column > 70 && c == ' ')
1181 // this check is to amend a bug. LyX sometimes
1182 // inserts '\0' this could cause problems.
1184 std::vector<char> tmp = ucs4_to_utf8(c);
1185 tmp.push_back('\0');
1188 lyxerr << "ERROR (Paragraph::writeFile):"
1189 " NULL char in structure." << endl;
1195 os << "\n\\end_layout\n";
1199 void Paragraph::validate(LaTeXFeatures & features) const
1201 d->validate(features, *layout());
1205 void Paragraph::insert(pos_type start, docstring const & str,
1206 Font const & font, Change const & change)
1208 for (size_t i = 0, n = str.size(); i != n ; ++i)
1209 insertChar(start + i, str[i], font, change);
1213 void Paragraph::appendChar(char_type c, Font const & font,
1214 Change const & change)
1217 d->changes_.insert(change, d->text_.size());
1218 // when appending characters, no need to update tables
1219 d->text_.push_back(c);
1220 setFont(d->text_.size() - 1, font);
1224 void Paragraph::appendString(docstring const & s, Font const & font,
1225 Change const & change)
1227 pos_type end = s.size();
1228 size_t oldsize = d->text_.size();
1229 size_t newsize = oldsize + end;
1230 size_t capacity = d->text_.capacity();
1231 if (newsize >= capacity)
1232 d->text_.reserve(std::max(capacity + 100, newsize));
1234 // when appending characters, no need to update tables
1237 // FIXME: Optimize this!
1238 for (pos_type i = 0; i != end; ++i) {
1240 d->changes_.insert(change, i);
1242 d->fontlist_.set(oldsize, font);
1243 d->fontlist_.set(newsize - 1, font);
1247 void Paragraph::insertChar(pos_type pos, char_type c,
1250 d->insertChar(pos, c, Change(trackChanges ?
1251 Change::INSERTED : Change::UNCHANGED));
1255 void Paragraph::insertChar(pos_type pos, char_type c,
1256 Font const & font, bool trackChanges)
1258 d->insertChar(pos, c, Change(trackChanges ?
1259 Change::INSERTED : Change::UNCHANGED));
1264 void Paragraph::insertChar(pos_type pos, char_type c,
1265 Font const & font, Change const & change)
1267 d->insertChar(pos, c, change);
1272 void Paragraph::insertInset(pos_type pos, Inset * inset,
1273 Font const & font, Change const & change)
1275 insertInset(pos, inset, change);
1276 // Set the font/language of the inset...
1281 bool Paragraph::insetAllowed(InsetCode code)
1283 return !d->inset_owner_ || d->inset_owner_->insetAllowed(code);
1287 void Paragraph::resetFonts(Font const & font)
1289 d->fontlist_.clear();
1290 d->fontlist_.set(0, font);
1291 d->fontlist_.set(d->text_.size() - 1, font);
1294 // Gets uninstantiated font setting at position.
1295 Font const Paragraph::getFontSettings(BufferParams const & bparams,
1299 lyxerr << " pos: " << pos << " size: " << size() << endl;
1300 BOOST_ASSERT(pos <= size());
1303 FontList::const_iterator cit = d->fontlist_.fontIterator(pos);
1304 if (cit != d->fontlist_.end())
1307 if (pos == size() && !empty())
1308 return getFontSettings(bparams, pos - 1);
1310 return Font(inherit_font, getParLanguage(bparams));
1314 FontSpan Paragraph::fontSpan(pos_type pos) const
1316 BOOST_ASSERT(pos <= size());
1319 FontList::const_iterator cit = d->fontlist_.begin();
1320 FontList::const_iterator end = d->fontlist_.end();
1321 for (; cit != end; ++cit) {
1322 if (cit->pos() >= pos) {
1323 if (pos >= beginOfBody())
1324 return FontSpan(std::max(start, beginOfBody()),
1327 return FontSpan(start,
1328 std::min(beginOfBody() - 1,
1331 start = cit->pos() + 1;
1334 // This should not happen, but if so, we take no chances.
1335 //lyxerr << "Paragraph::getEndPosOfFontSpan: This should not happen!"
1337 return FontSpan(pos, pos);
1341 // Gets uninstantiated font setting at position 0
1342 Font const Paragraph::getFirstFontSettings(BufferParams const & bparams) const
1344 if (!empty() && !d->fontlist_.empty())
1345 return d->fontlist_.begin()->font();
1347 return Font(inherit_font, bparams.language);
1351 // Gets the fully instantiated font at a given position in a paragraph
1352 // This is basically the same function as Text::GetFont() in text2.cpp.
1353 // The difference is that this one is used for generating the LaTeX file,
1354 // and thus cosmetic "improvements" are disallowed: This has to deliver
1355 // the true picture of the buffer. (Asger)
1356 Font const Paragraph::getFont(BufferParams const & bparams, pos_type pos,
1357 Font const & outerfont) const
1359 BOOST_ASSERT(pos >= 0);
1361 Font font = getFontSettings(bparams, pos);
1363 pos_type const body_pos = beginOfBody();
1365 font.fontInfo().realize(d->layout_->labelfont);
1367 font.fontInfo().realize(d->layout_->font);
1369 font.fontInfo().realize(outerfont.fontInfo());
1370 font.fontInfo().realize(bparams.getFont().fontInfo());
1376 Font const Paragraph::getLabelFont
1377 (BufferParams const & bparams, Font const & outerfont) const
1379 FontInfo tmpfont = layout()->labelfont;
1380 tmpfont.realize(outerfont.fontInfo());
1381 tmpfont.realize(bparams.getFont().fontInfo());
1382 return Font(tmpfont, getParLanguage(bparams));
1386 Font const Paragraph::getLayoutFont
1387 (BufferParams const & bparams, Font const & outerfont) const
1389 FontInfo tmpfont = layout()->font;
1390 tmpfont.realize(outerfont.fontInfo());
1391 tmpfont.realize(bparams.getFont().fontInfo());
1392 return Font(tmpfont, getParLanguage(bparams));
1396 /// Returns the height of the highest font in range
1397 FontSize Paragraph::highestFontInRange
1398 (pos_type startpos, pos_type endpos, FontSize def_size) const
1400 return d->fontlist_.highestInRange(startpos, endpos, def_size);
1405 Paragraph::getUChar(BufferParams const & bparams, pos_type pos) const
1407 char_type c = d->text_[pos];
1408 if (!lyxrc.rtl_support)
1438 if (uc != c && getFontSettings(bparams, pos).isRightToLeft())
1445 void Paragraph::setFont(pos_type pos, Font const & font)
1447 BOOST_ASSERT(pos <= size());
1449 // First, reduce font against layout/label font
1450 // Update: The setCharFont() routine in text2.cpp already
1451 // reduces font, so we don't need to do that here. (Asger)
1453 d->fontlist_.set(pos, font);
1457 void Paragraph::makeSameLayout(Paragraph const & par)
1459 layout(par.layout());
1461 d->params_ = par.params();
1465 bool Paragraph::stripLeadingSpaces(bool trackChanges)
1467 if (isFreeSpacing())
1473 while (pos < size() && (isNewline(pos) || isLineSeparator(pos))) {
1474 if (eraseChar(pos, trackChanges))
1480 return count > 0 || pos > 0;
1484 bool Paragraph::hasSameLayout(Paragraph const & par) const
1486 return par.layout() == layout() && d->params_.sameLayout(par.params());
1490 depth_type Paragraph::getDepth() const
1492 return params().depth();
1496 depth_type Paragraph::getMaxDepthAfter() const
1498 if (layout()->isEnvironment())
1499 return params().depth() + 1;
1501 return params().depth();
1505 char Paragraph::getAlign() const
1507 if (params().align() == LYX_ALIGN_LAYOUT)
1508 return layout()->align;
1510 return params().align();
1514 docstring const & Paragraph::getLabelstring() const
1516 return params().labelString();
1520 // the next two functions are for the manual labels
1521 docstring const Paragraph::getLabelWidthString() const
1523 if (layout()->margintype == MARGIN_MANUAL)
1524 return params().labelWidthString();
1526 return _("Senseless with this layout!");
1530 void Paragraph::setLabelWidthString(docstring const & s)
1532 params().labelWidthString(s);
1536 docstring const Paragraph::translateIfPossible(docstring const & s,
1537 BufferParams const & bparams) const
1539 if (!support::isAscii(s) || s.empty()) {
1540 // This must be a user defined layout. We cannot translate
1541 // this, since gettext accepts only ascii keys.
1544 // Probably standard layout, try to translate
1545 Messages & m = getMessages(getParLanguage(bparams)->code());
1546 return m.get(to_ascii(s));
1550 docstring Paragraph::expandLabel(LayoutPtr const & layout,
1551 BufferParams const & bparams, bool process_appendix) const
1553 TextClass const & tclass = bparams.getTextClass();
1556 if (process_appendix && params().appendix())
1557 fmt = translateIfPossible(layout->labelstring_appendix(),
1560 fmt = translateIfPossible(layout->labelstring(), bparams);
1562 if (fmt.empty() && layout->labeltype == LABEL_COUNTER
1563 && !layout->counter.empty())
1564 fmt = "\\the" + layout->counter;
1566 // handle 'inherited level parts' in 'fmt',
1567 // i.e. the stuff between '@' in '@Section@.\arabic{subsection}'
1568 size_t const i = fmt.find('@', 0);
1569 if (i != docstring::npos) {
1570 size_t const j = fmt.find('@', i + 1);
1571 if (j != docstring::npos) {
1572 docstring parent(fmt, i + 1, j - i - 1);
1573 docstring label = from_ascii("??");
1574 if (tclass.hasLayout(parent))
1575 docstring label = expandLabel(tclass[parent], bparams,
1577 fmt = docstring(fmt, 0, i) + label
1578 + docstring(fmt, j + 1, docstring::npos);
1582 return tclass.counters().counterLabel(fmt);
1586 void Paragraph::applyLayout(LayoutPtr const & new_layout)
1589 LyXAlignment const oldAlign = params().align();
1591 if (!(oldAlign & layout()->alignpossible)) {
1592 frontend::Alert::warning(_("Alignment not permitted"),
1593 _("The new layout does not permit the alignment previously used.\nSetting to default."));
1594 params().align(LYX_ALIGN_LAYOUT);
1599 pos_type Paragraph::beginOfBody() const
1601 return d->begin_of_body_;
1605 void Paragraph::setBeginOfBody()
1607 if (layout()->labeltype != LABEL_MANUAL) {
1608 d->begin_of_body_ = 0;
1612 // Unroll the first two cycles of the loop
1613 // and remember the previous character to
1614 // remove unnecessary getChar() calls
1616 pos_type end = size();
1617 if (i < end && !isNewline(i)) {
1619 char_type previous_char = 0;
1622 previous_char = d->text_[i];
1623 if (!isNewline(i)) {
1625 while (i < end && previous_char != ' ') {
1630 previous_char = temp;
1636 d->begin_of_body_ = i;
1640 bool Paragraph::forceDefaultParagraphs() const
1642 return inInset() && inInset()->forceDefaultParagraphs(0);
1648 // paragraphs inside floats need different alignment tags to avoid
1651 bool noTrivlistCentering(InsetCode code)
1653 return code == FLOAT_CODE || code == WRAP_CODE;
1657 string correction(string const & orig)
1659 if (orig == "flushleft")
1660 return "raggedright";
1661 if (orig == "flushright")
1662 return "raggedleft";
1663 if (orig == "center")
1669 string const corrected_env(string const & suffix, string const & env,
1672 string output = suffix + "{";
1673 if (noTrivlistCentering(code))
1674 output += correction(env);
1678 if (suffix == "\\begin")
1684 void adjust_row_column(string const & str, TexRow & texrow, int & column)
1686 if (!contains(str, "\n"))
1687 column += str.size();
1691 column = rsplit(str, tmp, '\n').size();
1698 int Paragraph::Private::startTeXParParams(BufferParams const & bparams,
1699 odocstream & os, TexRow & texrow,
1700 bool moving_arg) const
1704 if (params_.noindent()) {
1705 os << "\\noindent ";
1709 LyXAlignment const curAlign = params_.align();
1711 if (curAlign == layout_->align)
1715 case LYX_ALIGN_NONE:
1716 case LYX_ALIGN_BLOCK:
1717 case LYX_ALIGN_LAYOUT:
1718 case LYX_ALIGN_SPECIAL:
1720 case LYX_ALIGN_LEFT:
1721 case LYX_ALIGN_RIGHT:
1722 case LYX_ALIGN_CENTER:
1731 case LYX_ALIGN_NONE:
1732 case LYX_ALIGN_BLOCK:
1733 case LYX_ALIGN_LAYOUT:
1734 case LYX_ALIGN_SPECIAL:
1736 case LYX_ALIGN_LEFT: {
1738 if (owner_->getParLanguage(bparams)->babel() != "hebrew")
1739 output = corrected_env("\\begin", "flushleft", owner_->ownerCode());
1741 output = corrected_env("\\begin", "flushright", owner_->ownerCode());
1742 os << from_ascii(output);
1743 adjust_row_column(output, texrow, column);
1745 } case LYX_ALIGN_RIGHT: {
1747 if (owner_->getParLanguage(bparams)->babel() != "hebrew")
1748 output = corrected_env("\\begin", "flushright", owner_->ownerCode());
1750 output = corrected_env("\\begin", "flushleft", owner_->ownerCode());
1751 os << from_ascii(output);
1752 adjust_row_column(output, texrow, column);
1754 } case LYX_ALIGN_CENTER: {
1756 output = corrected_env("\\begin", "center", owner_->ownerCode());
1757 os << from_ascii(output);
1758 adjust_row_column(output, texrow, column);
1767 int Paragraph::Private::endTeXParParams(BufferParams const & bparams,
1768 odocstream & os, TexRow & texrow,
1769 bool moving_arg) const
1773 switch (params_.align()) {
1774 case LYX_ALIGN_NONE:
1775 case LYX_ALIGN_BLOCK:
1776 case LYX_ALIGN_LAYOUT:
1777 case LYX_ALIGN_SPECIAL:
1779 case LYX_ALIGN_LEFT:
1780 case LYX_ALIGN_RIGHT:
1781 case LYX_ALIGN_CENTER:
1789 switch (params_.align()) {
1790 case LYX_ALIGN_NONE:
1791 case LYX_ALIGN_BLOCK:
1792 case LYX_ALIGN_LAYOUT:
1793 case LYX_ALIGN_SPECIAL:
1795 case LYX_ALIGN_LEFT: {
1797 if (owner_->getParLanguage(bparams)->babel() != "hebrew")
1798 output = corrected_env("\n\\par\\end", "flushleft", owner_->ownerCode());
1800 output = corrected_env("\n\\par\\end", "flushright", owner_->ownerCode());
1801 os << from_ascii(output);
1802 adjust_row_column(output, texrow, column);
1804 } case LYX_ALIGN_RIGHT: {
1806 if (owner_->getParLanguage(bparams)->babel() != "hebrew")
1807 output = corrected_env("\n\\par\\end", "flushright", owner_->ownerCode());
1809 output = corrected_env("\n\\par\\end", "flushleft", owner_->ownerCode());
1810 os << from_ascii(output);
1811 adjust_row_column(output, texrow, column);
1813 } case LYX_ALIGN_CENTER: {
1815 output = corrected_env("\n\\par\\end", "center", owner_->ownerCode());
1816 os << from_ascii(output);
1817 adjust_row_column(output, texrow, column);
1826 // This one spits out the text of the paragraph
1827 bool Paragraph::latex(Buffer const & buf,
1828 BufferParams const & bparams,
1829 Font const & outerfont,
1830 odocstream & os, TexRow & texrow,
1831 OutputParams const & runparams) const
1833 LYXERR(Debug::LATEX) << "SimpleTeXOnePar... " << this << endl;
1835 bool return_value = false;
1839 // well we have to check if we are in an inset with unlimited
1840 // length (all in one row) if that is true then we don't allow
1841 // any special options in the paragraph and also we don't allow
1842 // any environment other than the default layout of the text class
1844 bool asdefault = forceDefaultParagraphs();
1847 style = bparams.getTextClass().defaultLayout();
1852 // Current base font for all inherited font changes, without any
1853 // change caused by an individual character, except for the language:
1854 // It is set to the language of the first character.
1855 // As long as we are in the label, this font is the base font of the
1856 // label. Before the first body character it is set to the base font
1860 // Maybe we have to create a optional argument.
1861 pos_type body_pos = beginOfBody();
1862 unsigned int column = 0;
1865 // the optional argument is kept in curly brackets in
1866 // case it contains a ']'
1869 basefont = getLabelFont(bparams, outerfont);
1871 basefont = getLayoutFont(bparams, outerfont);
1874 // Which font is currently active?
1875 Font running_font(basefont);
1876 // Do we have an open font change?
1877 bool open_font = false;
1879 Change runningChange = Change(Change::UNCHANGED);
1881 texrow.start(id(), 0);
1883 // if the paragraph is empty, the loop will not be entered at all
1885 if (style->isCommand()) {
1890 column += d->startTeXParParams(bparams, os, texrow,
1891 runparams.moving_arg);
1894 for (pos_type i = 0; i < size(); ++i) {
1895 // First char in paragraph or after label?
1896 if (i == body_pos) {
1899 column += running_font.latexWriteEndChanges(
1900 os, bparams, runparams,
1901 basefont, basefont);
1904 basefont = getLayoutFont(bparams, outerfont);
1905 running_font = basefont;
1907 column += Changes::latexMarkChange(os, bparams,
1908 runningChange, Change(Change::UNCHANGED));
1909 runningChange = Change(Change::UNCHANGED);
1914 if (style->isCommand()) {
1920 column += d->startTeXParParams(bparams, os,
1922 runparams.moving_arg);
1925 Change const & change = runparams.inDeletedInset ? runparams.changeOfDeletedInset
1928 if (bparams.outputChanges && runningChange != change) {
1930 column += running_font.latexWriteEndChanges(
1931 os, bparams, runparams, basefont, basefont);
1934 basefont = getLayoutFont(bparams, outerfont);
1935 running_font = basefont;
1937 column += Changes::latexMarkChange(os, bparams, runningChange, change);
1938 runningChange = change;
1941 // do not output text which is marked deleted
1942 // if change tracking output is disabled
1943 if (!bparams.outputChanges && change.type == Change::DELETED) {
1949 // Fully instantiated font
1950 Font const font = getFont(bparams, i, outerfont);
1952 Font const last_font = running_font;
1954 // Do we need to close the previous font?
1956 (font != running_font ||
1957 font.language() != running_font.language()))
1959 column += running_font.latexWriteEndChanges(
1960 os, bparams, runparams, basefont,
1961 (i == body_pos-1) ? basefont : font);
1962 running_font = basefont;
1966 // Switch file encoding if necessary (and allowed)
1967 if (!runparams.verbatim &&
1968 runparams.encoding->package() == Encoding::inputenc &&
1969 font.language()->encoding()->package() == Encoding::inputenc) {
1970 std::pair<bool, int> const enc_switch = switchEncoding(os, bparams,
1971 runparams.moving_arg, *(runparams.encoding),
1972 *(font.language()->encoding()));
1973 if (enc_switch.first) {
1974 column += enc_switch.second;
1975 runparams.encoding = font.language()->encoding();
1979 char_type const c = d->text_[i];
1981 // Do we need to change font?
1982 if ((font != running_font ||
1983 font.language() != running_font.language()) &&
1986 odocstringstream ods;
1987 column += font.latexWriteStartChanges(ods, bparams,
1988 runparams, basefont,
1990 running_font = font;
1992 docstring fontchange = ods.str();
1993 // check if the fontchange ends with a trailing blank
1994 // (like "\small " (see bug 3382)
1995 if (suffixIs(fontchange, ' ') && c == ' ')
1996 os << fontchange.substr(0, fontchange.size() - 1)
1997 << from_ascii("{}");
2003 // FIXME: integrate this case in latexSpecialChar
2004 // Do not print the separation of the optional argument
2005 // if style->pass_thru is false. This works because
2006 // latexSpecialChar ignores spaces if
2007 // style->pass_thru is false.
2008 if (i != body_pos - 1) {
2009 if (d->simpleTeXBlanks(
2010 runparams, os, texrow,
2011 i, column, font, *style)) {
2012 // A surrogate pair was output. We
2013 // must not call latexSpecialChar
2014 // in this iteration, since it would output
2015 // the combining character again.
2022 OutputParams rp = runparams;
2023 rp.free_spacing = style->free_spacing;
2024 rp.local_font = &font;
2025 rp.intitle = style->intitle;
2027 // Two major modes: LaTeX or plain
2028 // Handle here those cases common to both modes
2029 // and then split to handle the two modes separately.
2030 if (c == META_INSET)
2031 d->latexInset(buf, bparams, os,
2032 texrow, rp, running_font,
2033 basefont, outerfont, open_font,
2034 runningChange, *style, i, column);
2036 d->latexSpecialChar(os, rp, running_font, runningChange,
2039 // Set the encoding to that returned from simpleTeXSpecialChars (see
2040 // comment for encoding member in OutputParams.h)
2041 runparams.encoding = rp.encoding;
2044 // If we have an open font definition, we have to close it
2046 #ifdef FIXED_LANGUAGE_END_DETECTION
2049 .latexWriteEndChanges(os, bparams, runparams,
2051 next_->getFont(bparams, 0, outerfont));
2053 running_font.latexWriteEndChanges(os, bparams,
2054 runparams, basefont, basefont);
2057 //FIXME: For now we ALWAYS have to close the foreign font settings if they are
2058 //FIXME: there as we start another \selectlanguage with the next paragraph if
2059 //FIXME: we are in need of this. This should be fixed sometime (Jug)
2060 running_font.latexWriteEndChanges(os, bparams, runparams,
2061 basefont, basefont);
2065 column += Changes::latexMarkChange(os, bparams, runningChange, Change(Change::UNCHANGED));
2067 // Needed if there is an optional argument but no contents.
2068 if (body_pos > 0 && body_pos == size()) {
2070 return_value = false;
2074 column += d->endTeXParParams(bparams, os, texrow,
2075 runparams.moving_arg);
2078 LYXERR(Debug::LATEX) << "SimpleTeXOnePar...done " << this << endl;
2079 return return_value;
2083 bool Paragraph::emptyTag() const
2085 for (pos_type i = 0; i < size(); ++i) {
2087 Inset const * inset = getInset(i);
2088 InsetCode lyx_code = inset->lyxCode();
2089 if (lyx_code != TOC_CODE &&
2090 lyx_code != INCLUDE_CODE &&
2091 lyx_code != GRAPHICS_CODE &&
2092 lyx_code != ERT_CODE &&
2093 lyx_code != LISTINGS_CODE &&
2094 lyx_code != FLOAT_CODE &&
2095 lyx_code != TABULAR_CODE) {
2099 char_type c = d->text_[i];
2100 if (c != ' ' && c != '\t')
2108 string Paragraph::getID(Buffer const & buf, OutputParams const & runparams) const
2110 for (pos_type i = 0; i < size(); ++i) {
2112 Inset const * inset = getInset(i);
2113 InsetCode lyx_code = inset->lyxCode();
2114 if (lyx_code == LABEL_CODE) {
2115 InsetLabel const * const il = static_cast<InsetLabel const *>(inset);
2116 docstring const & id = il->getParam("name");
2117 return "id='" + to_utf8(sgml::cleanID(buf, runparams, id)) + "'";
2126 pos_type Paragraph::getFirstWord(Buffer const & buf, odocstream & os, OutputParams const & runparams) const
2129 for (i = 0; i < size(); ++i) {
2131 Inset const * inset = getInset(i);
2132 inset->docbook(buf, os, runparams);
2134 char_type c = d->text_[i];
2137 os << sgml::escapeChar(c);
2144 bool Paragraph::Private::onlyText(Buffer const & buf, Font const & outerfont, pos_type initial) const
2147 pos_type size = text_.size();
2148 for (pos_type i = initial; i < size; ++i) {
2149 Font font = owner_->getFont(buf.params(), i, outerfont);
2150 if (text_[i] == META_INSET)
2152 if (i != initial && font != font_old)
2161 void Paragraph::simpleDocBookOnePar(Buffer const & buf,
2163 OutputParams const & runparams,
2164 Font const & outerfont,
2165 pos_type initial) const
2167 bool emph_flag = false;
2169 LayoutPtr const & style = layout();
2171 style->labeltype == LABEL_MANUAL ? style->labelfont : style->font;
2173 if (style->pass_thru && !d->onlyText(buf, outerfont, initial))
2176 // parsing main loop
2177 for (pos_type i = initial; i < size(); ++i) {
2178 Font font = getFont(buf.params(), i, outerfont);
2180 // handle <emphasis> tag
2181 if (font_old.emph() != font.fontInfo().emph()) {
2182 if (font.fontInfo().emph() == FONT_ON) {
2185 } else if (i != initial) {
2186 os << "</emphasis>";
2192 Inset const * inset = getInset(i);
2193 inset->docbook(buf, os, runparams);
2195 char_type c = d->text_[i];
2197 if (style->pass_thru)
2200 os << sgml::escapeChar(c);
2202 font_old = font.fontInfo();
2206 os << "</emphasis>";
2209 if (style->free_spacing)
2211 if (style->pass_thru && !d->onlyText(buf, outerfont, initial))
2216 bool Paragraph::isHfill(pos_type pos) const
2219 && getInset(pos)->lyxCode() == HFILL_CODE;
2223 bool Paragraph::isNewline(pos_type pos) const
2226 && getInset(pos)->lyxCode() == NEWLINE_CODE;
2230 bool Paragraph::isLineSeparator(pos_type pos) const
2232 char_type const c = d->text_[pos];
2233 return isLineSeparatorChar(c)
2234 || (c == META_INSET && getInset(pos) &&
2235 getInset(pos)->isLineSeparator());
2239 /// Used by the spellchecker
2240 bool Paragraph::isLetter(pos_type pos) const
2243 return getInset(pos)->isLetter();
2245 char_type const c = d->text_[pos];
2246 return isLetterChar(c) || isDigit(c);
2252 Paragraph::getParLanguage(BufferParams const & bparams) const
2255 return getFirstFontSettings(bparams).language();
2256 // FIXME: we should check the prev par as well (Lgb)
2257 return bparams.language;
2261 bool Paragraph::isRTL(BufferParams const & bparams) const
2263 return lyxrc.rtl_support
2264 && getParLanguage(bparams)->rightToLeft()
2265 && ownerCode() != ERT_CODE
2266 && ownerCode() != LISTINGS_CODE;
2270 void Paragraph::changeLanguage(BufferParams const & bparams,
2271 Language const * from, Language const * to)
2273 // change language including dummy font change at the end
2274 for (pos_type i = 0; i <= size(); ++i) {
2275 Font font = getFontSettings(bparams, i);
2276 if (font.language() == from) {
2277 font.setLanguage(to);
2284 bool Paragraph::isMultiLingual(BufferParams const & bparams) const
2286 Language const * doc_language = bparams.language;
2287 FontList::const_iterator cit = d->fontlist_.begin();
2288 FontList::const_iterator end = d->fontlist_.end();
2290 for (; cit != end; ++cit)
2291 if (cit->font().language() != ignore_language &&
2292 cit->font().language() != latex_language &&
2293 cit->font().language() != doc_language)
2299 // Convert the paragraph to a string.
2300 // Used for building the table of contents
2301 docstring const Paragraph::asString(Buffer const & buffer, bool label) const
2303 return asString(buffer, 0, size(), label);
2307 docstring const Paragraph::asString(Buffer const & buffer,
2308 pos_type beg, pos_type end, bool label) const
2311 odocstringstream os;
2313 if (beg == 0 && label && !params().labelString().empty())
2314 os << params().labelString() << ' ';
2316 for (pos_type i = beg; i < end; ++i) {
2317 char_type const c = d->text_[i];
2320 else if (c == META_INSET)
2321 getInset(i)->textString(buffer, os);
2328 void Paragraph::setInsetOwner(Inset * inset)
2330 d->inset_owner_ = inset;
2334 int Paragraph::id() const
2340 LayoutPtr const & Paragraph::layout() const
2346 void Paragraph::layout(LayoutPtr const & new_layout)
2348 d->layout_ = new_layout;
2352 Inset * Paragraph::inInset() const
2354 return d->inset_owner_;
2358 InsetCode Paragraph::ownerCode() const
2360 return d->inset_owner_ ?
2361 d->inset_owner_->lyxCode() : NO_CODE;
2365 ParagraphParameters & Paragraph::params()
2371 ParagraphParameters const & Paragraph::params() const
2377 bool Paragraph::isFreeSpacing() const
2379 if (layout()->free_spacing)
2381 return d->inset_owner_ && d->inset_owner_->isFreeSpacing();
2385 bool Paragraph::allowEmpty() const
2387 if (layout()->keepempty)
2389 return d->inset_owner_ && d->inset_owner_->allowEmpty();
2393 char_type Paragraph::transformChar(char_type c, pos_type pos) const
2395 if (!Encodings::is_arabic(c))
2398 char_type prev_char = ' ';
2399 char_type next_char = ' ';
2401 for (pos_type i = pos - 1; i >= 0; --i) {
2402 char_type const par_char = d->text_[i];
2403 if (!Encodings::isComposeChar_arabic(par_char)) {
2404 prev_char = par_char;
2409 for (pos_type i = pos + 1, end = size(); i < end; ++i) {
2410 char_type const par_char = d->text_[i];
2411 if (!Encodings::isComposeChar_arabic(par_char)) {
2412 next_char = par_char;
2417 if (Encodings::is_arabic(next_char)) {
2418 if (Encodings::is_arabic(prev_char) &&
2419 !Encodings::is_arabic_special(prev_char))
2420 return Encodings::transformChar(c, Encodings::FORM_MEDIAL);
2422 return Encodings::transformChar(c, Encodings::FORM_INITIAL);
2424 if (Encodings::is_arabic(prev_char) &&
2425 !Encodings::is_arabic_special(prev_char))
2426 return Encodings::transformChar(c, Encodings::FORM_FINAL);
2428 return Encodings::transformChar(c, Encodings::FORM_ISOLATED);
2433 int Paragraph::checkBiblio(bool track_changes)
2436 //This is getting more and more a mess. ...We really should clean
2437 //up this bibitem issue for 1.6. See also bug 2743.
2439 // Add bibitem insets if necessary
2440 if (layout()->labeltype != LABEL_BIBLIO)
2443 bool hasbibitem = !d->insetlist_.empty()
2444 // Insist on it being in pos 0
2445 && d->text_[0] == META_INSET
2446 && d->insetlist_.begin()->inset->lyxCode() == BIBITEM_CODE;
2451 // remove a bibitem in pos != 0
2452 // restore it later in pos 0 if necessary
2453 // (e.g. if a user inserts contents _before_ the item)
2454 // we're assuming there's only one of these, which there
2456 int erasedInsetPosition = -1;
2457 InsetList::iterator it = d->insetlist_.begin();
2458 InsetList::iterator end = d->insetlist_.end();
2459 for (; it != end; ++it)
2460 if (it->inset->lyxCode() == BIBITEM_CODE
2462 InsetBibitem * olditem = static_cast<InsetBibitem *>(it->inset);
2463 oldkey = olditem->getParam("key");
2464 oldlabel = olditem->getParam("label");
2465 erasedInsetPosition = it->pos;
2466 eraseChar(erasedInsetPosition, track_changes);
2470 //There was an InsetBibitem at the beginning, and we didn't
2471 //have to erase one.
2472 if (hasbibitem && erasedInsetPosition < 0)
2475 //There was an InsetBibitem at the beginning and we did have to
2476 //erase one. So we give its properties to the beginning inset.
2478 InsetBibitem * inset =
2479 static_cast<InsetBibitem *>(d->insetlist_.begin()->inset);
2480 if (!oldkey.empty())
2481 inset->setParam("key", oldkey);
2482 inset->setParam("label", oldlabel);
2483 return -erasedInsetPosition;
2486 //There was no inset at the beginning, so we need to create one with
2487 //the key and label of the one we erased.
2488 InsetBibitem * inset(new InsetBibitem(InsetCommandParams(BIBITEM_CODE)));
2489 // restore values of previously deleted item in this par.
2490 if (!oldkey.empty())
2491 inset->setParam("key", oldkey);
2492 inset->setParam("label", oldlabel);
2493 insertInset(0, static_cast<Inset *>(inset),
2494 Change(track_changes ? Change::INSERTED : Change::UNCHANGED));
2500 unsigned int Paragraph::macrocontextPosition() const
2502 return d->macrocontext_position_;
2506 void Paragraph::setMacrocontextPosition(unsigned int pos)
2508 d->macrocontext_position_ = pos;
2512 void Paragraph::checkAuthors(AuthorList const & authorList)
2514 d->changes_.checkAuthors(authorList);
2518 bool Paragraph::isUnchanged(pos_type pos) const
2520 return lookupChange(pos).type == Change::UNCHANGED;
2524 bool Paragraph::isInserted(pos_type pos) const
2526 return lookupChange(pos).type == Change::INSERTED;
2530 bool Paragraph::isDeleted(pos_type pos) const
2532 return lookupChange(pos).type == Change::DELETED;
2536 InsetList const & Paragraph::insetList() const
2538 return d->insetlist_;
2542 Inset * Paragraph::releaseInset(pos_type pos)
2544 Inset * inset = d->insetlist_.release(pos);
2545 /// does not honour change tracking!
2546 eraseChar(pos, false);
2551 Inset * Paragraph::getInset(pos_type pos)
2553 return d->insetlist_.get(pos);
2557 Inset const * Paragraph::getInset(pos_type pos) const
2559 return d->insetlist_.get(pos);
2563 void Paragraph::changeCase(BufferParams const & bparams, pos_type pos,
2564 pos_type right, TextCase action)
2566 // process sequences of modified characters; in change
2567 // tracking mode, this approach results in much better
2568 // usability than changing case on a char-by-char basis
2571 bool const trackChanges = bparams.trackChanges;
2573 bool capitalize = true;
2575 for (; pos < right; ++pos) {
2576 char_type oldChar = d->text_[pos];
2577 char_type newChar = oldChar;
2579 // ignore insets and don't play with deleted text!
2580 if (oldChar != META_INSET && !isDeleted(pos)) {
2582 case text_lowercase:
2583 newChar = lowercase(oldChar);
2585 case text_capitalization:
2587 newChar = uppercase(oldChar);
2591 case text_uppercase:
2592 newChar = uppercase(oldChar);
2597 if (!isLetter(pos) || isDeleted(pos)) {
2598 // permit capitalization again
2602 if (oldChar != newChar)
2605 if (oldChar == newChar || pos == right - 1) {
2606 if (oldChar != newChar) {
2607 // step behind the changing area
2610 int erasePos = pos - changes.size();
2611 for (size_t i = 0; i < changes.size(); i++) {
2612 insertChar(pos, changes[i],
2613 getFontSettings(bparams,
2616 if (!eraseChar(erasePos, trackChanges)) {
2619 ++right; // expand selection
2628 bool Paragraph::find(docstring const & str, bool cs, bool mw,
2629 pos_type pos, bool del) const
2631 int const strsize = str.length();
2633 pos_type const parsize = d->text_.size();
2634 for (i = 0; pos + i < parsize; ++i) {
2637 if (cs && str[i] != d->text_[pos + i])
2639 if (!cs && uppercase(str[i]) != uppercase(d->text_[pos + i]))
2641 if (!del && isDeleted(pos + i))
2648 // if necessary, check whether string matches word
2650 if (pos > 0 && isLetter(pos - 1))
2652 if (pos + strsize < parsize
2653 && isLetter(pos + strsize))
2661 char_type Paragraph::getChar(pos_type pos) const
2663 return d->text_[pos];
2667 pos_type Paragraph::size() const
2669 return d->text_.size();
2673 bool Paragraph::empty() const
2675 return d->text_.empty();
2679 bool Paragraph::isInset(pos_type pos) const
2681 return d->text_[pos] == META_INSET;
2685 bool Paragraph::isSeparator(pos_type pos) const
2687 //FIXME: Are we sure this can be the only separator?
2688 return d->text_[pos] == ' ';