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"
46 #include "frontends/alert.h"
48 #include "insets/InsetBibitem.h"
49 #include "insets/InsetLabel.h"
51 #include "support/convert.h"
52 #include "support/debug.h"
53 #include "support/gettext.h"
54 #include "support/lstrings.h"
55 #include "support/Messages.h"
56 #include "support/textutils.h"
62 using namespace lyx::support;
67 /// Inset identifier (above 0x10ffff, for ucs-4)
68 char_type const META_INSET = 0x200001;
71 /////////////////////////////////////////////////////////////////////
75 /////////////////////////////////////////////////////////////////////
77 class Paragraph::Private
81 Private(Paragraph * owner);
82 /// "Copy constructor"
83 Private(Private const &, Paragraph * owner);
86 void insertChar(pos_type pos, char_type c, Change const & change);
88 /// Output the surrogate pair formed by \p c and \p next to \p os.
89 /// \return the number of characters written.
90 int latexSurrogatePair(odocstream & os, char_type c, char_type next,
93 /// Output a space in appropriate formatting (or a surrogate pair
94 /// if the next character is a combining character).
95 /// \return whether a surrogate pair was output.
96 bool simpleTeXBlanks(OutputParams const &,
97 odocstream &, TexRow & texrow,
99 unsigned int & column,
101 Layout const & style);
103 /// Output consecutive unicode chars, belonging to the same script as
104 /// specified by the latex macro \p ltx, to \p os starting from \p i.
105 /// \return the number of characters written.
106 int writeScriptChars(odocstream & os, docstring const & ltx,
107 Change &, Encoding const &, pos_type & i);
109 /// This could go to ParagraphParameters if we want to.
110 int startTeXParParams(BufferParams const &, odocstream &, TexRow &,
113 /// This could go to ParagraphParameters if we want to.
114 int endTeXParParams(BufferParams const &, odocstream &, TexRow &,
118 void latexInset(Buffer const &, BufferParams const &,
120 TexRow & texrow, OutputParams &,
123 Font const & outerfont,
125 Change & running_change,
126 Layout const & style,
128 unsigned int & column);
131 void latexSpecialChar(
133 OutputParams & runparams,
135 Change & running_change,
136 Layout const & style,
138 unsigned int & column);
145 unsigned int & column);
147 bool latexSpecialTypewriter(
151 unsigned int & column);
153 bool latexSpecialPhrase(
156 unsigned int & column,
157 OutputParams & runparams);
160 void validate(LaTeXFeatures & features,
161 Layout const & layout) const;
163 /// Checks if the paragraph contains only text and no inset or font change.
164 bool onlyText(Buffer const & buf, Font const & outerfont,
165 pos_type initial) const;
167 /// match a string against a particular point in the paragraph
168 bool isTextAt(string const & str, pos_type pos) const;
170 /// Which Paragraph owns us?
174 Inset * inset_owner_;
182 static unsigned int paragraph_id;
184 ParagraphParameters params_;
186 /// for recording and looking up changes
190 InsetList insetlist_;
196 pos_type begin_of_body_;
198 typedef docstring TextContainer;
202 typedef std::set<docstring> Words;
208 // Initialization of the counter for the paragraph id's,
209 unsigned int Paragraph::Private::paragraph_id = 0;
213 struct special_phrase {
219 special_phrase const special_phrases[] = {
220 { "LyX", from_ascii("\\LyX{}"), false },
221 { "TeX", from_ascii("\\TeX{}"), true },
222 { "LaTeX2e", from_ascii("\\LaTeXe{}"), true },
223 { "LaTeX", from_ascii("\\LaTeX{}"), true },
226 size_t const phrases_nr = sizeof(special_phrases)/sizeof(special_phrase);
231 Paragraph::Private::Private(Paragraph * owner)
232 : owner_(owner), inset_owner_(0), begin_of_body_(0)
234 id_ = paragraph_id++;
239 Paragraph::Private::Private(Private const & p, Paragraph * owner)
240 : owner_(owner), inset_owner_(p.inset_owner_), fontlist_(p.fontlist_),
241 params_(p.params_), changes_(p.changes_), insetlist_(p.insetlist_),
242 layout_(p.layout_), begin_of_body_(p.begin_of_body_), text_(p.text_)
244 id_ = paragraph_id++;
248 bool Paragraph::isChanged(pos_type start, pos_type end) const
250 BOOST_ASSERT(start >= 0 && start <= size());
251 BOOST_ASSERT(end > start && end <= size() + 1);
253 return d->changes_.isChanged(start, end);
257 bool Paragraph::isMergedOnEndOfParDeletion(bool trackChanges) const
259 // keep the logic here in sync with the logic of eraseChars()
263 Change const change = d->changes_.lookup(size());
264 return change.type == Change::INSERTED && change.author == 0;
268 void Paragraph::setChange(Change const & change)
270 // beware of the imaginary end-of-par character!
271 d->changes_.set(change, 0, size() + 1);
274 * Propagate the change recursively - but not in case of DELETED!
276 * Imagine that your co-author makes changes in an existing inset. He
277 * sends your document to you and you come to the conclusion that the
278 * inset should go completely. If you erase it, LyX must not delete all
279 * text within the inset. Otherwise, the change tracked insertions of
280 * your co-author get lost and there is no way to restore them later.
282 * Conclusion: An inset's content should remain untouched if you delete it
285 if (change.type != Change::DELETED) {
286 for (pos_type pos = 0; pos < size(); ++pos) {
287 if (Inset * inset = getInset(pos))
288 inset->setChange(change);
294 void Paragraph::setChange(pos_type pos, Change const & change)
296 BOOST_ASSERT(pos >= 0 && pos <= size());
297 d->changes_.set(change, pos);
299 // see comment in setChange(Change const &) above
300 if (change.type != Change::DELETED && pos < size())
301 if (Inset * inset = getInset(pos))
302 inset->setChange(change);
306 Change const & Paragraph::lookupChange(pos_type pos) const
308 BOOST_ASSERT(pos >= 0 && pos <= size());
309 return d->changes_.lookup(pos);
313 void Paragraph::acceptChanges(BufferParams const & bparams, pos_type start,
316 BOOST_ASSERT(start >= 0 && start <= size());
317 BOOST_ASSERT(end > start && end <= size() + 1);
319 for (pos_type pos = start; pos < end; ++pos) {
320 switch (lookupChange(pos).type) {
321 case Change::UNCHANGED:
322 // accept changes in nested inset
323 if (Inset * inset = getInset(pos))
324 inset->acceptChanges(bparams);
327 case Change::INSERTED:
328 d->changes_.set(Change(Change::UNCHANGED), pos);
329 // also accept changes in nested inset
330 if (Inset * inset = getInset(pos))
331 inset->acceptChanges(bparams);
334 case Change::DELETED:
335 // Suppress access to non-existent
336 // "end-of-paragraph char"
338 eraseChar(pos, false);
349 void Paragraph::rejectChanges(BufferParams const & bparams,
350 pos_type start, pos_type end)
352 BOOST_ASSERT(start >= 0 && start <= size());
353 BOOST_ASSERT(end > start && end <= size() + 1);
355 for (pos_type pos = start; pos < end; ++pos) {
356 switch (lookupChange(pos).type) {
357 case Change::UNCHANGED:
358 // reject changes in nested inset
359 if (Inset * inset = getInset(pos))
360 inset->rejectChanges(bparams);
363 case Change::INSERTED:
364 // Suppress access to non-existent
365 // "end-of-paragraph char"
367 eraseChar(pos, false);
373 case Change::DELETED:
374 d->changes_.set(Change(Change::UNCHANGED), pos);
376 // Do NOT reject changes within a deleted inset!
377 // There may be insertions of a co-author inside of it!
385 void Paragraph::Private::insertChar(pos_type pos, char_type c,
386 Change const & change)
388 BOOST_ASSERT(pos >= 0 && pos <= int(text_.size()));
391 changes_.insert(change, pos);
393 // This is actually very common when parsing buffers (and
394 // maybe inserting ascii text)
395 if (pos == pos_type(text_.size())) {
396 // when appending characters, no need to update tables
401 text_.insert(text_.begin() + pos, c);
403 // Update the font table.
404 fontlist_.increasePosAfterPos(pos);
407 insetlist_.increasePosAfterPos(pos);
411 void Paragraph::insertInset(pos_type pos, Inset * inset,
412 Change const & change)
415 BOOST_ASSERT(pos >= 0 && pos <= size());
417 d->insertChar(pos, META_INSET, change);
418 BOOST_ASSERT(d->text_[pos] == META_INSET);
420 // Add a new entry in the insetlist_.
421 d->insetlist_.insert(inset, pos);
425 bool Paragraph::eraseChar(pos_type pos, bool trackChanges)
427 BOOST_ASSERT(pos >= 0 && pos <= size());
429 // keep the logic here in sync with the logic of isMergedOnEndOfParDeletion()
432 Change change = d->changes_.lookup(pos);
434 // set the character to DELETED if
435 // a) it was previously unchanged or
436 // b) it was inserted by a co-author
438 if (change.type == Change::UNCHANGED ||
439 (change.type == Change::INSERTED && change.author != 0)) {
440 setChange(pos, Change(Change::DELETED));
444 if (change.type == Change::DELETED)
448 // Don't physically access the imaginary end-of-paragraph character.
449 // eraseChar() can only mark it as DELETED. A physical deletion of
450 // end-of-par must be handled externally.
456 d->changes_.erase(pos);
458 // if it is an inset, delete the inset entry
459 if (d->text_[pos] == META_INSET)
460 d->insetlist_.erase(pos);
462 d->text_.erase(d->text_.begin() + pos);
464 // Update the fontlist_
465 d->fontlist_.erase(pos);
467 // Update the insetlist_
468 d->insetlist_.decreasePosAfterPos(pos);
474 int Paragraph::eraseChars(pos_type start, pos_type end, bool trackChanges)
476 BOOST_ASSERT(start >= 0 && start <= size());
477 BOOST_ASSERT(end >= start && end <= size() + 1);
480 for (pos_type count = end - start; count; --count) {
481 if (!eraseChar(i, trackChanges))
488 int Paragraph::Private::latexSurrogatePair(odocstream & os, char_type c,
489 char_type next, Encoding const & encoding)
491 // Writing next here may circumvent a possible font change between
492 // c and next. Since next is only output if it forms a surrogate pair
493 // with c we can ignore this:
494 // A font change inside a surrogate pair does not make sense and is
495 // hopefully impossible to input.
496 // FIXME: change tracking
497 // Is this correct WRT change tracking?
498 docstring const latex1 = encoding.latexChar(next);
499 docstring const latex2 = encoding.latexChar(c);
500 os << latex1 << '{' << latex2 << '}';
501 return latex1.length() + latex2.length() + 2;
505 bool Paragraph::Private::simpleTeXBlanks(OutputParams const & runparams,
506 odocstream & os, TexRow & texrow,
508 unsigned int & column,
510 Layout const & style)
512 if (style.pass_thru || runparams.verbatim)
515 if (i + 1 < int(text_.size())) {
516 char_type next = text_[i + 1];
517 if (Encodings::isCombiningChar(next)) {
518 Encoding const & encoding = *(runparams.encoding);
519 // This space has an accent, so we must always output it.
520 column += latexSurrogatePair(os, ' ', next, encoding) - 1;
525 if (lyxrc.plaintext_linelen > 0
526 && column > lyxrc.plaintext_linelen
528 && text_[i - 1] != ' '
529 && (i + 1 < int(text_.size()))
530 // same in FreeSpacing mode
531 && !owner_->isFreeSpacing()
532 // In typewriter mode, we want to avoid
533 // ! . ? : at the end of a line
534 && !(font.fontInfo().family() == TYPEWRITER_FAMILY
535 && (text_[i - 1] == '.'
536 || text_[i - 1] == '?'
537 || text_[i - 1] == ':'
538 || text_[i - 1] == '!'))) {
541 texrow.start(owner_->id(), i + 1);
543 } else if (style.free_spacing) {
552 int Paragraph::Private::writeScriptChars(odocstream & os,
553 docstring const & ltx,
554 Change & runningChange,
555 Encoding const & encoding,
558 // FIXME: modifying i here is not very nice...
560 // We only arrive here when a proper language for character text_[i] has
561 // not been specified (i.e., it could not be translated in the current
562 // latex encoding) and it belongs to a known script.
563 // Parameter ltx contains the latex translation of text_[i] as specified in
564 // the unicodesymbols file and is something like "\textXXX{<spec>}".
565 // The latex macro name "textXXX" specifies the script to which text_[i]
566 // belongs and we use it in order to check whether characters from the
567 // same script immediately follow, such that we can collect them in a
568 // single "\textXXX" macro. So, we have to retain "\textXXX{<spec>"
569 // for the first char but only "<spec>" for all subsequent chars.
570 docstring::size_type const brace1 = ltx.find_first_of(from_ascii("{"));
571 docstring::size_type const brace2 = ltx.find_last_of(from_ascii("}"));
572 string script = to_ascii(ltx.substr(1, brace1 - 1));
573 int length = ltx.substr(0, brace2).length();
574 os << ltx.substr(0, brace2);
575 int size = text_.size();
576 while (i + 1 < size) {
577 char_type const next = text_[i + 1];
578 // Stop here if next character belongs to another script
579 // or there is a change in change tracking status.
580 if (!Encodings::isKnownScriptChar(next, script) ||
581 runningChange != owner_->lookupChange(i + 1))
585 FontList::const_iterator cit = fontlist_.begin();
586 FontList::const_iterator end = fontlist_.end();
587 for (; cit != end; ++cit) {
588 if (cit->pos() >= i && !found) {
589 prev_font = cit->font();
592 if (cit->pos() >= i + 1)
595 // Stop here if there is a font attribute or encoding change.
596 if (found && cit != end && prev_font != cit->font())
598 docstring const latex = encoding.latexChar(next);
599 docstring::size_type const b1 =
600 latex.find_first_of(from_ascii("{"));
601 docstring::size_type const b2 =
602 latex.find_last_of(from_ascii("}"));
603 int const len = b2 - b1 - 1;
604 os << latex.substr(b1 + 1, len);
614 bool Paragraph::Private::isTextAt(string const & str, pos_type pos) const
616 pos_type const len = str.length();
618 // is the paragraph large enough?
619 if (pos + len > int(text_.size()))
622 // does the wanted text start at point?
623 for (string::size_type i = 0; i < str.length(); ++i) {
624 // Caution: direct comparison of characters works only
625 // because str is pure ASCII.
626 if (str[i] != text_[pos + i])
630 return fontlist_.hasChangeInRange(pos, len);
634 void Paragraph::Private::latexInset(Buffer const & buf,
635 BufferParams const & bparams,
638 OutputParams & runparams,
641 Font const & outerfont,
643 Change & running_change,
644 Layout const & style,
646 unsigned int & column)
648 Inset * inset = owner_->getInset(i);
651 if (style.pass_thru) {
652 inset->plaintext(buf, os, runparams);
656 // FIXME: move this to InsetNewline::latex
657 if (inset->lyxCode() == NEWLINE_CODE) {
658 // newlines are handled differently here than
659 // the default in simpleTeXSpecialChars().
660 if (!style.newline_allowed) {
664 column += running_font.latexWriteEndChanges(
665 os, bparams, runparams,
670 if (running_font.fontInfo().family() == TYPEWRITER_FAMILY)
673 basefont = owner_->getLayoutFont(bparams, outerfont);
674 running_font = basefont;
676 if (runparams.moving_arg)
681 texrow.start(owner_->id(), i + 1);
685 if (owner_->lookupChange(i).type == Change::DELETED) {
686 if( ++runparams.inDeletedInset == 1)
687 runparams.changeOfDeletedInset = owner_->lookupChange(i);
690 if (inset->canTrackChanges()) {
691 column += Changes::latexMarkChange(os, bparams, running_change,
692 Change(Change::UNCHANGED));
693 running_change = Change(Change::UNCHANGED);
697 odocstream::pos_type const len = os.tellp();
699 if (inset->forceLTR()
700 && running_font.isRightToLeft()
701 // ERT is an exception, it should be output with no decorations at all
702 && inset->lyxCode() != ERT_CODE) {
703 if (running_font.language()->lang() == "farsi")
710 // FIXME: Bug: we can have an empty font change here!
711 // if there has just been a font change, we are going to close it
712 // right now, which means stupid latex code like \textsf{}. AFAIK,
713 // this does not harm dvi output. A minor bug, thus (JMarc)
715 // Some insets cannot be inside a font change command.
716 // However, even such insets *can* be placed in \L or \R
717 // or their equivalents (for RTL language switches), so we don't
718 // close the language in those cases.
719 // ArabTeX, though, cannot handle this special behavior, it seems.
720 bool arabtex = basefont.language()->lang() == "arabic_arabtex"
721 || running_font.language()->lang() == "arabic_arabtex";
722 if (open_font && inset->noFontChange()) {
723 bool closeLanguage = arabtex
724 || basefont.isRightToLeft() == running_font.isRightToLeft();
725 unsigned int count = running_font.latexWriteEndChanges(os,
726 bparams, runparams, basefont, basefont, closeLanguage);
728 // if any font properties were closed, update the running_font,
729 // making sure, however, to leave the language as it was
731 // FIXME: probably a better way to keep track of the old
732 // language, than copying the entire font?
733 Font const copy_font(running_font);
734 basefont = owner_->getLayoutFont(bparams, outerfont);
735 running_font = basefont;
737 running_font.setLanguage(copy_font.language());
738 // leave font open if language is still open
739 open_font = (running_font.language() == basefont.language());
741 runparams.local_font = &basefont;
745 int tmp = inset->latex(buf, os, runparams);
748 if (running_font.language()->lang() == "farsi")
755 for (int j = 0; j < tmp; ++j)
758 texrow.start(owner_->id(), i + 1);
761 column += os.tellp() - len;
764 if (owner_->lookupChange(i).type == Change::DELETED)
765 --runparams.inDeletedInset;
769 void Paragraph::Private::latexSpecialChar(
771 OutputParams & runparams,
773 Change & running_change,
774 Layout const & style,
776 unsigned int & column)
778 char_type const c = text_[i];
780 if (style.pass_thru) {
782 // FIXME UNICODE: This can fail if c cannot
783 // be encoded in the current encoding.
788 if (runparams.verbatim) {
793 if (lyxrc.fontenc == "T1" && latexSpecialT1(c, os, i, column))
796 if (running_font.fontInfo().family() == TYPEWRITER_FAMILY
797 && latexSpecialTypewriter(c, os, i, column))
800 // Otherwise, we use what LaTeX provides us.
803 os << "\\textbackslash{}";
807 os << "\\textless{}";
811 os << "\\textgreater{}";
822 os << "\\char`\\\"{}";
827 case '%': case '#': case '{':
835 os << "\\textasciitilde{}";
840 os << "\\textasciicircum{}";
845 // avoid being mistaken for optional arguments
853 // Blanks are printed before font switching.
854 // Sure? I am not! (try nice-latex)
855 // I am sure it's correct. LyX might be smarter
856 // in the future, but for now, nothing wrong is
863 if (latexSpecialPhrase(os, i, column, runparams))
869 Encoding const & encoding = *(runparams.encoding);
870 if (i + 1 < int(text_.size())) {
871 char_type next = text_[i + 1];
872 if (Encodings::isCombiningChar(next)) {
873 column += latexSurrogatePair(os, c, next, encoding) - 1;
879 docstring const latex = encoding.latexChar(c);
880 if (Encodings::isKnownScriptChar(c, script)
881 && prefixIs(latex, from_ascii("\\" + script)))
882 column += writeScriptChars(os, latex,
883 running_change, encoding, i) - 1;
884 else if (latex.length() > 1 && latex[latex.length() - 1] != '}') {
885 // Prevent eating of a following
886 // space or command corruption by
887 // following characters
888 column += latex.length() + 1;
891 column += latex.length() - 1;
899 bool Paragraph::Private::latexSpecialT1(char_type const c, odocstream & os,
900 pos_type & i, unsigned int & column)
906 // In T1 encoding, these characters exist
907 // but we should avoid ligatures
908 if (i + 1 >= int(text_.size()) || text_[i + 1] != c)
913 //os << "\\textcompwordmark{}";
925 bool Paragraph::Private::latexSpecialTypewriter(char_type const c, odocstream & os,
926 pos_type & i, unsigned int & column)
930 if (i + 1 < int(text_.size()) && text_[i + 1] == '-') {
931 // "--" in Typewriter mode -> "-{}-"
938 // I assume this is hack treating typewriter as verbatim
939 // FIXME UNICODE: This can fail if c cannot be encoded
940 // in the current encoding.
945 // Those characters are not directly supported.
949 case '%': case '#': case '{':
958 // With Typewriter font, these characters exist.
965 bool Paragraph::Private::latexSpecialPhrase(odocstream & os, pos_type & i,
966 unsigned int & column, OutputParams & runparams)
968 // FIXME: if we have "LaTeX" with a font
969 // change in the middle (before the 'T', then
970 // the "TeX" part is still special cased.
971 // Really we should only operate this on
972 // "words" for some definition of word
974 for (size_t pnr = 0; pnr < phrases_nr; ++pnr) {
975 if (!isTextAt(special_phrases[pnr].phrase, i))
977 if (runparams.moving_arg)
979 os << special_phrases[pnr].macro;
980 i += special_phrases[pnr].phrase.length() - 1;
981 column += special_phrases[pnr].macro.length() - 1;
988 void Paragraph::Private::validate(LaTeXFeatures & features,
989 Layout const & layout) const
992 if (!params_.spacing().isDefault())
993 features.require("setspace");
996 features.useLayout(layout.name());
999 fontlist_.validate(features);
1001 // then the indentation
1002 if (!params_.leftIndent().zero())
1003 features.require("ParagraphLeftIndent");
1006 InsetList::const_iterator icit = insetlist_.begin();
1007 InsetList::const_iterator iend = insetlist_.end();
1008 for (; icit != iend; ++icit) {
1010 icit->inset->validate(features);
1011 if (layout.needprotect &&
1012 icit->inset->lyxCode() == FOOT_CODE)
1013 features.require("NeedLyXFootnoteCode");
1017 // then the contents
1018 for (pos_type i = 0; i < int(text_.size()) ; ++i) {
1019 for (size_t pnr = 0; pnr < phrases_nr; ++pnr) {
1020 if (!special_phrases[pnr].builtin
1021 && isTextAt(special_phrases[pnr].phrase, i)) {
1022 features.require(special_phrases[pnr].phrase);
1026 Encodings::validate(text_[i], features);
1030 /////////////////////////////////////////////////////////////////////
1034 /////////////////////////////////////////////////////////////////////
1036 Paragraph::Paragraph()
1037 : d(new Paragraph::Private(this))
1044 Paragraph::Paragraph(Paragraph const & par)
1045 : itemdepth(par.itemdepth),
1046 d(new Paragraph::Private(*par.d, this))
1051 Paragraph & Paragraph::operator=(Paragraph const & par)
1053 // needed as we will destroy the private part before copying it
1055 itemdepth = par.itemdepth;
1058 d = new Private(*par.d, this);
1064 Paragraph::~Paragraph()
1071 void Paragraph::write(Buffer const & buf, ostream & os,
1072 BufferParams const & bparams,
1073 depth_type & dth) const
1075 // The beginning or end of a deeper (i.e. nested) area?
1076 if (dth != d->params_.depth()) {
1077 if (d->params_.depth() > dth) {
1078 while (d->params_.depth() > dth) {
1079 os << "\n\\begin_deeper";
1083 while (d->params_.depth() < dth) {
1084 os << "\n\\end_deeper";
1090 // First write the layout
1091 os << "\n\\begin_layout " << to_utf8(d->layout_->name()) << '\n';
1093 d->params_.write(os);
1095 Font font1(inherit_font, bparams.language);
1097 Change running_change = Change(Change::UNCHANGED);
1100 for (pos_type i = 0; i <= size(); ++i) {
1102 Change change = lookupChange(i);
1103 Changes::lyxMarkChange(os, column, running_change, change);
1104 running_change = change;
1109 // Write font changes
1110 Font font2 = getFontSettings(bparams, i);
1111 if (font2 != font1) {
1112 font2.lyxWriteChanges(font1, os);
1117 char_type const c = d->text_[i];
1120 if (Inset const * inset = getInset(i)) {
1121 if (inset->directWrite()) {
1122 // international char, let it write
1123 // code directly so it's shorter in
1125 inset->write(buf, os);
1129 os << "\\begin_inset ";
1130 inset->write(buf, os);
1131 os << "\n\\end_inset\n\n";
1137 os << "\n\\backslash\n";
1141 if (i + 1 < size() && d->text_[i + 1] == ' ') {
1148 if ((column > 70 && c == ' ')
1153 // this check is to amend a bug. LyX sometimes
1154 // inserts '\0' this could cause problems.
1156 os << to_utf8(docstring(1, c));
1158 lyxerr << "ERROR (Paragraph::writeFile):"
1159 " NULL char in structure." << endl;
1165 os << "\n\\end_layout\n";
1169 void Paragraph::validate(LaTeXFeatures & features) const
1171 d->validate(features, *d->layout_);
1175 void Paragraph::insert(pos_type start, docstring const & str,
1176 Font const & font, Change const & change)
1178 for (size_t i = 0, n = str.size(); i != n ; ++i)
1179 insertChar(start + i, str[i], font, change);
1183 void Paragraph::appendChar(char_type c, Font const & font,
1184 Change const & change)
1187 d->changes_.insert(change, d->text_.size());
1188 // when appending characters, no need to update tables
1189 d->text_.push_back(c);
1190 setFont(d->text_.size() - 1, font);
1194 void Paragraph::appendString(docstring const & s, Font const & font,
1195 Change const & change)
1197 pos_type end = s.size();
1198 size_t oldsize = d->text_.size();
1199 size_t newsize = oldsize + end;
1200 size_t capacity = d->text_.capacity();
1201 if (newsize >= capacity)
1202 d->text_.reserve(max(capacity + 100, newsize));
1204 // when appending characters, no need to update tables
1207 // FIXME: Optimize this!
1208 for (pos_type i = 0; i != end; ++i) {
1210 d->changes_.insert(change, i);
1212 d->fontlist_.set(oldsize, font);
1213 d->fontlist_.set(newsize - 1, font);
1217 void Paragraph::insertChar(pos_type pos, char_type c,
1220 d->insertChar(pos, c, Change(trackChanges ?
1221 Change::INSERTED : Change::UNCHANGED));
1225 void Paragraph::insertChar(pos_type pos, char_type c,
1226 Font const & font, bool trackChanges)
1228 d->insertChar(pos, c, Change(trackChanges ?
1229 Change::INSERTED : Change::UNCHANGED));
1234 void Paragraph::insertChar(pos_type pos, char_type c,
1235 Font const & font, Change const & change)
1237 d->insertChar(pos, c, change);
1242 void Paragraph::insertInset(pos_type pos, Inset * inset,
1243 Font const & font, Change const & change)
1245 insertInset(pos, inset, change);
1246 // Set the font/language of the inset...
1251 bool Paragraph::insetAllowed(InsetCode code)
1253 return !d->inset_owner_ || d->inset_owner_->insetAllowed(code);
1257 void Paragraph::resetFonts(Font const & font)
1259 d->fontlist_.clear();
1260 d->fontlist_.set(0, font);
1261 d->fontlist_.set(d->text_.size() - 1, font);
1264 // Gets uninstantiated font setting at position.
1265 Font const Paragraph::getFontSettings(BufferParams const & bparams,
1269 lyxerr << " pos: " << pos << " size: " << size() << endl;
1270 BOOST_ASSERT(pos <= size());
1273 FontList::const_iterator cit = d->fontlist_.fontIterator(pos);
1274 if (cit != d->fontlist_.end())
1277 if (pos == size() && !empty())
1278 return getFontSettings(bparams, pos - 1);
1280 return Font(inherit_font, getParLanguage(bparams));
1284 FontSpan Paragraph::fontSpan(pos_type pos) const
1286 BOOST_ASSERT(pos <= size());
1289 FontList::const_iterator cit = d->fontlist_.begin();
1290 FontList::const_iterator end = d->fontlist_.end();
1291 for (; cit != end; ++cit) {
1292 if (cit->pos() >= pos) {
1293 if (pos >= beginOfBody())
1294 return FontSpan(max(start, beginOfBody()),
1297 return FontSpan(start,
1298 min(beginOfBody() - 1,
1301 start = cit->pos() + 1;
1304 // This should not happen, but if so, we take no chances.
1305 //lyxerr << "Paragraph::getEndPosOfFontSpan: This should not happen!"
1307 return FontSpan(pos, pos);
1311 // Gets uninstantiated font setting at position 0
1312 Font const Paragraph::getFirstFontSettings(BufferParams const & bparams) const
1314 if (!empty() && !d->fontlist_.empty())
1315 return d->fontlist_.begin()->font();
1317 return Font(inherit_font, bparams.language);
1321 // Gets the fully instantiated font at a given position in a paragraph
1322 // This is basically the same function as Text::GetFont() in text2.cpp.
1323 // The difference is that this one is used for generating the LaTeX file,
1324 // and thus cosmetic "improvements" are disallowed: This has to deliver
1325 // the true picture of the buffer. (Asger)
1326 Font const Paragraph::getFont(BufferParams const & bparams, pos_type pos,
1327 Font const & outerfont) const
1329 BOOST_ASSERT(pos >= 0);
1331 Font font = getFontSettings(bparams, pos);
1333 pos_type const body_pos = beginOfBody();
1335 font.fontInfo().realize(d->layout_->labelfont);
1337 font.fontInfo().realize(d->layout_->font);
1339 font.fontInfo().realize(outerfont.fontInfo());
1340 font.fontInfo().realize(bparams.getFont().fontInfo());
1346 Font const Paragraph::getLabelFont
1347 (BufferParams const & bparams, Font const & outerfont) const
1349 FontInfo tmpfont = d->layout_->labelfont;
1350 tmpfont.realize(outerfont.fontInfo());
1351 tmpfont.realize(bparams.getFont().fontInfo());
1352 return Font(tmpfont, getParLanguage(bparams));
1356 Font const Paragraph::getLayoutFont
1357 (BufferParams const & bparams, Font const & outerfont) const
1359 FontInfo tmpfont = d->layout_->font;
1360 tmpfont.realize(outerfont.fontInfo());
1361 tmpfont.realize(bparams.getFont().fontInfo());
1362 return Font(tmpfont, getParLanguage(bparams));
1366 /// Returns the height of the highest font in range
1367 FontSize Paragraph::highestFontInRange
1368 (pos_type startpos, pos_type endpos, FontSize def_size) const
1370 return d->fontlist_.highestInRange(startpos, endpos, def_size);
1375 Paragraph::getUChar(BufferParams const & bparams, pos_type pos) const
1377 char_type c = d->text_[pos];
1378 if (!lyxrc.rtl_support)
1408 if (uc != c && getFontSettings(bparams, pos).isRightToLeft())
1415 void Paragraph::setFont(pos_type pos, Font const & font)
1417 BOOST_ASSERT(pos <= size());
1419 // First, reduce font against layout/label font
1420 // Update: The setCharFont() routine in text2.cpp already
1421 // reduces font, so we don't need to do that here. (Asger)
1423 d->fontlist_.set(pos, font);
1427 void Paragraph::makeSameLayout(Paragraph const & par)
1429 d->layout_ = par.d->layout_;
1430 d->params_ = par.d->params_;
1434 bool Paragraph::stripLeadingSpaces(bool trackChanges)
1436 if (isFreeSpacing())
1442 while (pos < size() && (isNewline(pos) || isLineSeparator(pos))) {
1443 if (eraseChar(pos, trackChanges))
1449 return count > 0 || pos > 0;
1453 bool Paragraph::hasSameLayout(Paragraph const & par) const
1455 return par.d->layout_ == d->layout_ && d->params_.sameLayout(par.d->params_);
1459 depth_type Paragraph::getDepth() const
1461 return d->params_.depth();
1465 depth_type Paragraph::getMaxDepthAfter() const
1467 if (d->layout_->isEnvironment())
1468 return d->params_.depth() + 1;
1470 return d->params_.depth();
1474 char Paragraph::getAlign() const
1476 if (d->params_.align() == LYX_ALIGN_LAYOUT)
1477 return d->layout_->align;
1479 return d->params_.align();
1483 docstring const & Paragraph::labelString() const
1485 return d->params_.labelString();
1489 // the next two functions are for the manual labels
1490 docstring const Paragraph::getLabelWidthString() const
1492 if (d->layout_->margintype == MARGIN_MANUAL)
1493 return d->params_.labelWidthString();
1495 return _("Senseless with this layout!");
1499 void Paragraph::setLabelWidthString(docstring const & s)
1501 d->params_.labelWidthString(s);
1505 docstring const Paragraph::translateIfPossible(docstring const & s,
1506 BufferParams const & bparams) const
1508 if (!isAscii(s) || s.empty()) {
1509 // This must be a user defined layout. We cannot translate
1510 // this, since gettext accepts only ascii keys.
1513 // Probably standard layout, try to translate
1514 Messages & m = getMessages(getParLanguage(bparams)->code());
1515 return m.get(to_ascii(s));
1519 docstring Paragraph::expandLabel(LayoutPtr const & layout,
1520 BufferParams const & bparams, bool process_appendix) const
1522 TextClass const & tclass = bparams.textClass();
1525 if (process_appendix && d->params_.appendix())
1526 fmt = translateIfPossible(layout->labelstring_appendix(),
1529 fmt = translateIfPossible(layout->labelstring(), bparams);
1531 if (fmt.empty() && layout->labeltype == LABEL_COUNTER
1532 && !layout->counter.empty())
1533 fmt = "\\the" + layout->counter;
1535 // handle 'inherited level parts' in 'fmt',
1536 // i.e. the stuff between '@' in '@Section@.\arabic{subsection}'
1537 size_t const i = fmt.find('@', 0);
1538 if (i != docstring::npos) {
1539 size_t const j = fmt.find('@', i + 1);
1540 if (j != docstring::npos) {
1541 docstring parent(fmt, i + 1, j - i - 1);
1542 docstring label = from_ascii("??");
1543 if (tclass.hasLayout(parent))
1544 docstring label = expandLabel(tclass[parent], bparams,
1546 fmt = docstring(fmt, 0, i) + label
1547 + docstring(fmt, j + 1, docstring::npos);
1551 return tclass.counters().counterLabel(fmt);
1555 void Paragraph::applyLayout(LayoutPtr const & new_layout)
1557 d->layout_ = new_layout;
1558 LyXAlignment const oldAlign = d->params_.align();
1560 if (!(oldAlign & d->layout_->alignpossible)) {
1561 frontend::Alert::warning(_("Alignment not permitted"),
1562 _("The new layout does not permit the alignment previously used.\nSetting to default."));
1563 d->params_.align(LYX_ALIGN_LAYOUT);
1568 pos_type Paragraph::beginOfBody() const
1570 return d->begin_of_body_;
1574 void Paragraph::setBeginOfBody()
1576 if (d->layout_->labeltype != LABEL_MANUAL) {
1577 d->begin_of_body_ = 0;
1581 // Unroll the first two cycles of the loop
1582 // and remember the previous character to
1583 // remove unnecessary getChar() calls
1585 pos_type end = size();
1586 if (i < end && !isNewline(i)) {
1588 char_type previous_char = 0;
1591 previous_char = d->text_[i];
1592 if (!isNewline(i)) {
1594 while (i < end && previous_char != ' ') {
1599 previous_char = temp;
1605 d->begin_of_body_ = i;
1609 bool Paragraph::forceEmptyLayout() const
1611 return inInset() && inInset()->forceEmptyLayout();
1615 bool Paragraph::allowParagraphCustomization() const
1617 return inInset() && inInset()->allowParagraphCustomization(0);
1621 bool Paragraph::useEmptyLayout() const
1623 return inInset() && inInset()->useEmptyLayout();
1629 // paragraphs inside floats need different alignment tags to avoid
1632 bool noTrivlistCentering(InsetCode code)
1634 return code == FLOAT_CODE || code == WRAP_CODE;
1638 string correction(string const & orig)
1640 if (orig == "flushleft")
1641 return "raggedright";
1642 if (orig == "flushright")
1643 return "raggedleft";
1644 if (orig == "center")
1650 string const corrected_env(string const & suffix, string const & env,
1653 string output = suffix + "{";
1654 if (noTrivlistCentering(code))
1655 output += correction(env);
1659 if (suffix == "\\begin")
1665 void adjust_row_column(string const & str, TexRow & texrow, int & column)
1667 if (!contains(str, "\n"))
1668 column += str.size();
1672 column = rsplit(str, tmp, '\n').size();
1679 int Paragraph::Private::startTeXParParams(BufferParams const & bparams,
1680 odocstream & os, TexRow & texrow,
1681 bool moving_arg) const
1685 if (params_.noindent()) {
1686 os << "\\noindent ";
1690 LyXAlignment const curAlign = params_.align();
1692 if (curAlign == layout_->align)
1696 case LYX_ALIGN_NONE:
1697 case LYX_ALIGN_BLOCK:
1698 case LYX_ALIGN_LAYOUT:
1699 case LYX_ALIGN_SPECIAL:
1701 case LYX_ALIGN_LEFT:
1702 case LYX_ALIGN_RIGHT:
1703 case LYX_ALIGN_CENTER:
1712 case LYX_ALIGN_NONE:
1713 case LYX_ALIGN_BLOCK:
1714 case LYX_ALIGN_LAYOUT:
1715 case LYX_ALIGN_SPECIAL:
1717 case LYX_ALIGN_LEFT: {
1719 if (owner_->getParLanguage(bparams)->babel() != "hebrew")
1720 output = corrected_env("\\begin", "flushleft", owner_->ownerCode());
1722 output = corrected_env("\\begin", "flushright", owner_->ownerCode());
1723 os << from_ascii(output);
1724 adjust_row_column(output, texrow, column);
1726 } case LYX_ALIGN_RIGHT: {
1728 if (owner_->getParLanguage(bparams)->babel() != "hebrew")
1729 output = corrected_env("\\begin", "flushright", owner_->ownerCode());
1731 output = corrected_env("\\begin", "flushleft", owner_->ownerCode());
1732 os << from_ascii(output);
1733 adjust_row_column(output, texrow, column);
1735 } case LYX_ALIGN_CENTER: {
1737 output = corrected_env("\\begin", "center", owner_->ownerCode());
1738 os << from_ascii(output);
1739 adjust_row_column(output, texrow, column);
1748 int Paragraph::Private::endTeXParParams(BufferParams const & bparams,
1749 odocstream & os, TexRow & texrow,
1750 bool moving_arg) const
1754 switch (params_.align()) {
1755 case LYX_ALIGN_NONE:
1756 case LYX_ALIGN_BLOCK:
1757 case LYX_ALIGN_LAYOUT:
1758 case LYX_ALIGN_SPECIAL:
1760 case LYX_ALIGN_LEFT:
1761 case LYX_ALIGN_RIGHT:
1762 case LYX_ALIGN_CENTER:
1770 switch (params_.align()) {
1771 case LYX_ALIGN_NONE:
1772 case LYX_ALIGN_BLOCK:
1773 case LYX_ALIGN_LAYOUT:
1774 case LYX_ALIGN_SPECIAL:
1776 case LYX_ALIGN_LEFT: {
1778 if (owner_->getParLanguage(bparams)->babel() != "hebrew")
1779 output = corrected_env("\n\\par\\end", "flushleft", owner_->ownerCode());
1781 output = corrected_env("\n\\par\\end", "flushright", owner_->ownerCode());
1782 os << from_ascii(output);
1783 adjust_row_column(output, texrow, column);
1785 } case LYX_ALIGN_RIGHT: {
1787 if (owner_->getParLanguage(bparams)->babel() != "hebrew")
1788 output = corrected_env("\n\\par\\end", "flushright", owner_->ownerCode());
1790 output = corrected_env("\n\\par\\end", "flushleft", owner_->ownerCode());
1791 os << from_ascii(output);
1792 adjust_row_column(output, texrow, column);
1794 } case LYX_ALIGN_CENTER: {
1796 output = corrected_env("\n\\par\\end", "center", owner_->ownerCode());
1797 os << from_ascii(output);
1798 adjust_row_column(output, texrow, column);
1807 // This one spits out the text of the paragraph
1808 bool Paragraph::latex(Buffer const & buf,
1809 BufferParams const & bparams,
1810 Font const & outerfont,
1811 odocstream & os, TexRow & texrow,
1812 OutputParams const & runparams) const
1814 LYXERR(Debug::LATEX, "SimpleTeXOnePar... " << this);
1816 bool return_value = false;
1820 // well we have to check if we are in an inset with unlimited
1821 // length (all in one row) if that is true then we don't allow
1822 // any special options in the paragraph and also we don't allow
1823 // any environment other than the default layout of the text class
1825 bool asdefault = forceEmptyLayout();
1828 style = bparams.textClass().defaultLayout();
1832 // Current base font for all inherited font changes, without any
1833 // change caused by an individual character, except for the language:
1834 // It is set to the language of the first character.
1835 // As long as we are in the label, this font is the base font of the
1836 // label. Before the first body character it is set to the base font
1840 // Maybe we have to create a optional argument.
1841 pos_type body_pos = beginOfBody();
1842 unsigned int column = 0;
1845 // the optional argument is kept in curly brackets in
1846 // case it contains a ']'
1849 basefont = getLabelFont(bparams, outerfont);
1851 basefont = getLayoutFont(bparams, outerfont);
1854 // Which font is currently active?
1855 Font running_font(basefont);
1856 // Do we have an open font change?
1857 bool open_font = false;
1859 Change runningChange = Change(Change::UNCHANGED);
1861 texrow.start(id(), 0);
1863 // if the paragraph is empty, the loop will not be entered at all
1865 if (style->isCommand()) {
1870 column += d->startTeXParParams(bparams, os, texrow,
1871 runparams.moving_arg);
1874 for (pos_type i = 0; i < size(); ++i) {
1875 // First char in paragraph or after label?
1876 if (i == body_pos) {
1879 column += running_font.latexWriteEndChanges(
1880 os, bparams, runparams,
1881 basefont, basefont);
1884 basefont = getLayoutFont(bparams, outerfont);
1885 running_font = basefont;
1887 column += Changes::latexMarkChange(os, bparams,
1888 runningChange, Change(Change::UNCHANGED));
1889 runningChange = Change(Change::UNCHANGED);
1894 if (style->isCommand()) {
1900 column += d->startTeXParParams(bparams, os,
1902 runparams.moving_arg);
1905 Change const & change = runparams.inDeletedInset ? runparams.changeOfDeletedInset
1908 if (bparams.outputChanges && runningChange != change) {
1910 column += running_font.latexWriteEndChanges(
1911 os, bparams, runparams, basefont, basefont);
1914 basefont = getLayoutFont(bparams, outerfont);
1915 running_font = basefont;
1917 column += Changes::latexMarkChange(os, bparams, runningChange, change);
1918 runningChange = change;
1921 // do not output text which is marked deleted
1922 // if change tracking output is disabled
1923 if (!bparams.outputChanges && change.type == Change::DELETED) {
1929 // Fully instantiated font
1930 Font const font = getFont(bparams, i, outerfont);
1932 Font const last_font = running_font;
1934 // Do we need to close the previous font?
1936 (font != running_font ||
1937 font.language() != running_font.language()))
1939 column += running_font.latexWriteEndChanges(
1940 os, bparams, runparams, basefont,
1941 (i == body_pos-1) ? basefont : font);
1942 running_font = basefont;
1946 // close babel's font environment before opening CJK.
1947 if (!running_font.language()->babel().empty() &&
1948 font.language()->encoding()->package() == Encoding::CJK) {
1949 string end_tag = subst(lyxrc.language_command_end,
1951 running_font.language()->babel());
1952 os << from_ascii(end_tag);
1953 column += end_tag.length();
1956 // Switch file encoding if necessary (and allowed)
1957 if (!runparams.verbatim &&
1958 runparams.encoding->package() == Encoding::none &&
1959 font.language()->encoding()->package() == Encoding::none) {
1960 pair<bool, int> const enc_switch = switchEncoding(os, bparams,
1961 runparams, *(font.language()->encoding()));
1962 if (enc_switch.first) {
1963 column += enc_switch.second;
1964 runparams.encoding = font.language()->encoding();
1968 char_type const c = d->text_[i];
1970 // Do we need to change font?
1971 if ((font != running_font ||
1972 font.language() != running_font.language()) &&
1975 odocstringstream ods;
1976 column += font.latexWriteStartChanges(ods, bparams,
1977 runparams, basefont,
1979 running_font = font;
1981 docstring fontchange = ods.str();
1982 // check if the fontchange ends with a trailing blank
1983 // (like "\small " (see bug 3382)
1984 if (suffixIs(fontchange, ' ') && c == ' ')
1985 os << fontchange.substr(0, fontchange.size() - 1)
1986 << from_ascii("{}");
1992 // FIXME: integrate this case in latexSpecialChar
1993 // Do not print the separation of the optional argument
1994 // if style->pass_thru is false. This works because
1995 // latexSpecialChar ignores spaces if
1996 // style->pass_thru is false.
1997 if (i != body_pos - 1) {
1998 if (d->simpleTeXBlanks(
1999 runparams, os, texrow,
2000 i, column, font, *style)) {
2001 // A surrogate pair was output. We
2002 // must not call latexSpecialChar
2003 // in this iteration, since it would output
2004 // the combining character again.
2011 OutputParams rp = runparams;
2012 rp.free_spacing = style->free_spacing;
2013 rp.local_font = &font;
2014 rp.intitle = style->intitle;
2016 // Two major modes: LaTeX or plain
2017 // Handle here those cases common to both modes
2018 // and then split to handle the two modes separately.
2019 if (c == META_INSET)
2020 d->latexInset(buf, bparams, os,
2021 texrow, rp, running_font,
2022 basefont, outerfont, open_font,
2023 runningChange, *style, i, column);
2026 d->latexSpecialChar(os, rp, running_font, runningChange,
2028 } catch (EncodingException & e) {
2029 if (runparams.dryrun) {
2030 os << "<" << _("LyX Warning: ")
2031 << _("uncodable character") << " '";
2035 // add location information and throw again.
2043 // Set the encoding to that returned from simpleTeXSpecialChars (see
2044 // comment for encoding member in OutputParams.h)
2045 runparams.encoding = rp.encoding;
2048 // If we have an open font definition, we have to close it
2050 #ifdef FIXED_LANGUAGE_END_DETECTION
2053 .latexWriteEndChanges(os, bparams, runparams,
2055 next_->getFont(bparams, 0, outerfont));
2057 running_font.latexWriteEndChanges(os, bparams,
2058 runparams, basefont, basefont);
2061 //FIXME: For now we ALWAYS have to close the foreign font settings if they are
2062 //FIXME: there as we start another \selectlanguage with the next paragraph if
2063 //FIXME: we are in need of this. This should be fixed sometime (Jug)
2064 running_font.latexWriteEndChanges(os, bparams, runparams,
2065 basefont, basefont);
2069 column += Changes::latexMarkChange(os, bparams, runningChange, Change(Change::UNCHANGED));
2071 // Needed if there is an optional argument but no contents.
2072 if (body_pos > 0 && body_pos == size()) {
2074 return_value = false;
2078 column += d->endTeXParParams(bparams, os, texrow,
2079 runparams.moving_arg);
2082 LYXERR(Debug::LATEX, "SimpleTeXOnePar...done " << this);
2083 return return_value;
2087 bool Paragraph::emptyTag() const
2089 for (pos_type i = 0; i < size(); ++i) {
2090 if (Inset const * inset = getInset(i)) {
2091 InsetCode lyx_code = inset->lyxCode();
2092 if (lyx_code != TOC_CODE &&
2093 lyx_code != INCLUDE_CODE &&
2094 lyx_code != GRAPHICS_CODE &&
2095 lyx_code != ERT_CODE &&
2096 lyx_code != LISTINGS_CODE &&
2097 lyx_code != FLOAT_CODE &&
2098 lyx_code != TABULAR_CODE) {
2102 char_type c = d->text_[i];
2103 if (c != ' ' && c != '\t')
2111 string Paragraph::getID(Buffer const & buf, OutputParams const & runparams) const
2113 for (pos_type i = 0; i < size(); ++i) {
2114 if (Inset const * inset = getInset(i)) {
2115 InsetCode lyx_code = inset->lyxCode();
2116 if (lyx_code == LABEL_CODE) {
2117 InsetLabel const * const il = static_cast<InsetLabel const *>(inset);
2118 docstring const & id = il->getParam("name");
2119 return "id='" + to_utf8(sgml::cleanID(buf, runparams, id)) + "'";
2127 pos_type Paragraph::getFirstWord(Buffer const & buf, odocstream & os, OutputParams const & runparams) const
2130 for (i = 0; i < size(); ++i) {
2131 if (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 = d->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>";
2191 if (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
2217 Inset const * inset = getInset(pos);
2218 return inset && inset->lyxCode() == HFILL_CODE;
2222 bool Paragraph::isNewline(pos_type pos) const
2224 Inset const * inset = getInset(pos);
2225 return inset && inset->lyxCode() == NEWLINE_CODE;
2229 bool Paragraph::isLineSeparator(pos_type pos) const
2231 char_type const c = d->text_[pos];
2232 if (isLineSeparatorChar(c))
2234 Inset const * inset = getInset(pos);
2235 return inset && inset->isLineSeparator();
2239 /// Used by the spellchecker
2240 bool Paragraph::isLetter(pos_type pos) const
2242 if (Inset const * inset = getInset(pos))
2243 return inset->isLetter();
2244 char_type const c = d->text_[pos];
2245 return isLetterChar(c) || isDigit(c);
2250 Paragraph::getParLanguage(BufferParams const & bparams) const
2253 return getFirstFontSettings(bparams).language();
2254 // FIXME: we should check the prev par as well (Lgb)
2255 return bparams.language;
2259 bool Paragraph::isRTL(BufferParams const & bparams) const
2261 return lyxrc.rtl_support
2262 && getParLanguage(bparams)->rightToLeft()
2263 && ownerCode() != ERT_CODE
2264 && ownerCode() != LISTINGS_CODE;
2268 void Paragraph::changeLanguage(BufferParams const & bparams,
2269 Language const * from, Language const * to)
2271 // change language including dummy font change at the end
2272 for (pos_type i = 0; i <= size(); ++i) {
2273 Font font = getFontSettings(bparams, i);
2274 if (font.language() == from) {
2275 font.setLanguage(to);
2282 bool Paragraph::isMultiLingual(BufferParams const & bparams) const
2284 Language const * doc_language = bparams.language;
2285 FontList::const_iterator cit = d->fontlist_.begin();
2286 FontList::const_iterator end = d->fontlist_.end();
2288 for (; cit != end; ++cit)
2289 if (cit->font().language() != ignore_language &&
2290 cit->font().language() != latex_language &&
2291 cit->font().language() != doc_language)
2297 // Convert the paragraph to a string.
2298 // Used for building the table of contents
2299 docstring const Paragraph::asString(Buffer const & buffer, bool label) const
2301 return asString(buffer, 0, size(), label);
2305 docstring const Paragraph::asString(Buffer const & buffer,
2306 pos_type beg, pos_type end, bool label) const
2309 odocstringstream os;
2311 if (beg == 0 && label && !d->params_.labelString().empty())
2312 os << d->params_.labelString() << ' ';
2314 for (pos_type i = beg; i < end; ++i) {
2315 char_type const c = d->text_[i];
2318 else if (c == META_INSET)
2319 getInset(i)->textString(buffer, os);
2326 void Paragraph::setInsetOwner(Inset * inset)
2328 d->inset_owner_ = inset;
2332 int Paragraph::id() const
2338 LayoutPtr const & Paragraph::layout() const
2344 void Paragraph::setLayout(LayoutPtr const & layout)
2346 d->layout_ = layout;
2350 void Paragraph::setEmptyOrDefaultLayout(TextClass const & tclass)
2352 if (useEmptyLayout())
2353 setLayout(tclass.emptyLayout());
2355 setLayout(tclass.defaultLayout());
2359 Inset * Paragraph::inInset() const
2361 return d->inset_owner_;
2365 InsetCode Paragraph::ownerCode() const
2367 return d->inset_owner_ ? d->inset_owner_->lyxCode() : NO_CODE;
2371 ParagraphParameters & Paragraph::params()
2377 ParagraphParameters const & Paragraph::params() const
2383 bool Paragraph::isFreeSpacing() const
2385 if (d->layout_->free_spacing)
2387 return d->inset_owner_ && d->inset_owner_->isFreeSpacing();
2391 bool Paragraph::allowEmpty() const
2393 if (d->layout_->keepempty)
2395 return d->inset_owner_ && d->inset_owner_->allowEmpty();
2399 char_type Paragraph::transformChar(char_type c, pos_type pos) const
2401 if (!Encodings::is_arabic(c))
2404 char_type prev_char = ' ';
2405 char_type next_char = ' ';
2407 for (pos_type i = pos - 1; i >= 0; --i) {
2408 char_type const par_char = d->text_[i];
2409 if (!Encodings::isComposeChar_arabic(par_char)) {
2410 prev_char = par_char;
2415 for (pos_type i = pos + 1, end = size(); i < end; ++i) {
2416 char_type const par_char = d->text_[i];
2417 if (!Encodings::isComposeChar_arabic(par_char)) {
2418 next_char = par_char;
2423 if (Encodings::is_arabic(next_char)) {
2424 if (Encodings::is_arabic(prev_char) &&
2425 !Encodings::is_arabic_special(prev_char))
2426 return Encodings::transformChar(c, Encodings::FORM_MEDIAL);
2428 return Encodings::transformChar(c, Encodings::FORM_INITIAL);
2430 if (Encodings::is_arabic(prev_char) &&
2431 !Encodings::is_arabic_special(prev_char))
2432 return Encodings::transformChar(c, Encodings::FORM_FINAL);
2434 return Encodings::transformChar(c, Encodings::FORM_ISOLATED);
2439 int Paragraph::checkBiblio(bool track_changes)
2442 //This is getting more and more a mess. ...We really should clean
2443 //up this bibitem issue for 1.6. See also bug 2743.
2445 // Add bibitem insets if necessary
2446 if (d->layout_->labeltype != LABEL_BIBLIO)
2449 bool hasbibitem = !d->insetlist_.empty()
2450 // Insist on it being in pos 0
2451 && d->text_[0] == META_INSET
2452 && d->insetlist_.begin()->inset->lyxCode() == BIBITEM_CODE;
2457 // remove a bibitem in pos != 0
2458 // restore it later in pos 0 if necessary
2459 // (e.g. if a user inserts contents _before_ the item)
2460 // we're assuming there's only one of these, which there
2462 int erasedInsetPosition = -1;
2463 InsetList::iterator it = d->insetlist_.begin();
2464 InsetList::iterator end = d->insetlist_.end();
2465 for (; it != end; ++it)
2466 if (it->inset->lyxCode() == BIBITEM_CODE
2468 InsetBibitem * olditem = static_cast<InsetBibitem *>(it->inset);
2469 oldkey = olditem->getParam("key");
2470 oldlabel = olditem->getParam("label");
2471 erasedInsetPosition = it->pos;
2472 eraseChar(erasedInsetPosition, track_changes);
2476 //There was an InsetBibitem at the beginning, and we didn't
2477 //have to erase one.
2478 if (hasbibitem && erasedInsetPosition < 0)
2481 //There was an InsetBibitem at the beginning and we did have to
2482 //erase one. So we give its properties to the beginning inset.
2484 InsetBibitem * inset =
2485 static_cast<InsetBibitem *>(d->insetlist_.begin()->inset);
2486 if (!oldkey.empty())
2487 inset->setParam("key", oldkey);
2488 inset->setParam("label", oldlabel);
2489 return -erasedInsetPosition;
2492 //There was no inset at the beginning, so we need to create one with
2493 //the key and label of the one we erased.
2494 InsetBibitem * inset(new InsetBibitem(InsetCommandParams(BIBITEM_CODE)));
2495 // restore values of previously deleted item in this par.
2496 if (!oldkey.empty())
2497 inset->setParam("key", oldkey);
2498 inset->setParam("label", oldlabel);
2499 insertInset(0, static_cast<Inset *>(inset),
2500 Change(track_changes ? Change::INSERTED : Change::UNCHANGED));
2506 void Paragraph::checkAuthors(AuthorList const & authorList)
2508 d->changes_.checkAuthors(authorList);
2512 bool Paragraph::isUnchanged(pos_type pos) const
2514 return lookupChange(pos).type == Change::UNCHANGED;
2518 bool Paragraph::isInserted(pos_type pos) const
2520 return lookupChange(pos).type == Change::INSERTED;
2524 bool Paragraph::isDeleted(pos_type pos) const
2526 return lookupChange(pos).type == Change::DELETED;
2530 InsetList const & Paragraph::insetList() const
2532 return d->insetlist_;
2536 Inset * Paragraph::releaseInset(pos_type pos)
2538 Inset * inset = d->insetlist_.release(pos);
2539 /// does not honour change tracking!
2540 eraseChar(pos, false);
2545 Inset * Paragraph::getInset(pos_type pos)
2547 return (pos < pos_type(d->text_.size()) && d->text_[pos] == META_INSET)
2548 ? d->insetlist_.get(pos) : 0;
2552 Inset const * Paragraph::getInset(pos_type pos) const
2554 return (pos < pos_type(d->text_.size()) && d->text_[pos] == META_INSET)
2555 ? d->insetlist_.get(pos) : 0;
2559 void Paragraph::changeCase(BufferParams const & bparams, pos_type pos,
2560 pos_type & right, TextCase action)
2562 // process sequences of modified characters; in change
2563 // tracking mode, this approach results in much better
2564 // usability than changing case on a char-by-char basis
2567 bool const trackChanges = bparams.trackChanges;
2569 bool capitalize = true;
2571 for (; pos < right; ++pos) {
2572 char_type oldChar = d->text_[pos];
2573 char_type newChar = oldChar;
2575 // ignore insets and don't play with deleted text!
2576 if (oldChar != META_INSET && !isDeleted(pos)) {
2578 case text_lowercase:
2579 newChar = lowercase(oldChar);
2581 case text_capitalization:
2583 newChar = uppercase(oldChar);
2587 case text_uppercase:
2588 newChar = uppercase(oldChar);
2593 if (!isLetter(pos) || isDeleted(pos)) {
2594 // permit capitalization again
2598 if (oldChar != newChar)
2601 if (oldChar == newChar || pos == right - 1) {
2602 if (oldChar != newChar) {
2603 // step behind the changing area
2606 int erasePos = pos - changes.size();
2607 for (size_t i = 0; i < changes.size(); i++) {
2608 insertChar(pos, changes[i],
2609 getFontSettings(bparams,
2612 if (!eraseChar(erasePos, trackChanges)) {
2615 ++right; // expand selection
2624 bool Paragraph::find(docstring const & str, bool cs, bool mw,
2625 pos_type pos, bool del) const
2627 int const strsize = str.length();
2629 pos_type const parsize = d->text_.size();
2630 for (i = 0; pos + i < parsize; ++i) {
2633 if (cs && str[i] != d->text_[pos + i])
2635 if (!cs && uppercase(str[i]) != uppercase(d->text_[pos + i]))
2637 if (!del && isDeleted(pos + i))
2644 // if necessary, check whether string matches word
2646 if (pos > 0 && isLetter(pos - 1))
2648 if (pos + strsize < parsize
2649 && isLetter(pos + strsize))
2657 char_type Paragraph::getChar(pos_type pos) const
2659 return d->text_[pos];
2663 pos_type Paragraph::size() const
2665 return d->text_.size();
2669 bool Paragraph::empty() const
2671 return d->text_.empty();
2675 bool Paragraph::isInset(pos_type pos) const
2677 return d->text_[pos] == META_INSET;
2681 bool Paragraph::isSeparator(pos_type pos) const
2683 //FIXME: Are we sure this can be the only separator?
2684 return d->text_[pos] == ' ';
2688 void Paragraph::deregisterWords()
2691 Private::Words::const_iterator it;
2692 WordList & wl = theWordList();
2693 for (it = d->words_.begin(); it != d->words_.end(); ++it)
2699 void Paragraph::registerWords(Cursor const & cur)
2702 bool inword = false;
2704 lyxerr << "Words: ";
2705 pos_type n = size();
2706 for (pos_type pos = 0; pos != n; ++pos) {
2710 if (!isLetter(pos)) {
2719 CursorSlice from = cur.top();
2720 CursorSlice to = cur.top();
2723 from.text()->getWord(from, to, NEXT_WORD);
2724 if (to.pos() - from.pos() < 6)
2727 = asString(cur.buffer(), from.pos(), to.pos(), false);
2728 d->words_.insert(word);
2729 lyxerr << word << " ";
2731 lyxerr << std::endl;
2734 Private::Words::const_iterator it;
2735 WordList & wl = theWordList();
2736 for (it = d->words_.begin(); it != d->words_.end(); ++it)
2741 void Paragraph::updateWords(Cursor const & cur)