3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Asger Alstrup
7 * \author Lars Gullik Bjønnes
8 * \author Jean-Marc Lasgouttes
9 * \author Angus Leeming
11 * \author André Pönitz
13 * \author Jürgen Vigna
15 * Full author contact details are available in file CREDITS.
20 #include "Paragraph.h"
23 #include "BufferParams.h"
27 #include "InsetList.h"
29 #include "LaTeXFeatures.h"
35 #include "OutputParams.h"
36 #include "output_latex.h"
37 #include "paragraph_funcs.h"
38 #include "ParagraphParameters.h"
40 #include "TextClass.h"
44 #include "frontends/alert.h"
46 #include "insets/InsetBibitem.h"
47 #include "insets/InsetLabel.h"
49 #include "support/convert.h"
50 #include "support/debug.h"
51 #include "support/gettext.h"
52 #include "support/lstrings.h"
53 #include "support/Messages.h"
54 #include "support/textutils.h"
60 using namespace lyx::support;
65 /// Inset identifier (above 0x10ffff, for ucs-4)
66 char_type const META_INSET = 0x200001;
69 /////////////////////////////////////////////////////////////////////
73 /////////////////////////////////////////////////////////////////////
75 class Paragraph::Private
79 Private(Paragraph * owner);
80 /// "Copy constructor"
81 Private(Private const &, Paragraph * owner);
84 void insertChar(pos_type pos, char_type c, Change const & change);
86 /// Output the surrogate pair formed by \p c and \p next to \p os.
87 /// \return the number of characters written.
88 int latexSurrogatePair(odocstream & os, char_type c, char_type next,
91 /// Output a space in appropriate formatting (or a surrogate pair
92 /// if the next character is a combining character).
93 /// \return whether a surrogate pair was output.
94 bool simpleTeXBlanks(OutputParams const &,
95 odocstream &, TexRow & texrow,
97 unsigned int & column,
99 Layout const & style);
101 /// Output consecutive unicode chars, belonging to the same script as
102 /// specified by the latex macro \p ltx, to \p os starting from \p i.
103 /// \return the number of characters written.
104 int writeScriptChars(odocstream & os, docstring const & ltx,
105 Change &, Encoding const &, pos_type & i);
107 /// This could go to ParagraphParameters if we want to.
108 int startTeXParParams(BufferParams const &, odocstream &, TexRow &,
111 /// This could go to ParagraphParameters if we want to.
112 int endTeXParParams(BufferParams const &, odocstream &, TexRow &,
116 void latexInset(Buffer const &, BufferParams const &,
118 TexRow & texrow, OutputParams &,
121 Font const & outerfont,
123 Change & running_change,
124 Layout const & style,
126 unsigned int & column);
129 void latexSpecialChar(
131 OutputParams & runparams,
133 Change & running_change,
134 Layout const & style,
136 unsigned int & column);
143 unsigned int & column);
145 bool latexSpecialTypewriter(
149 unsigned int & column);
151 bool latexSpecialPhrase(
154 unsigned int & column,
155 OutputParams & runparams);
158 void validate(LaTeXFeatures & features,
159 Layout const & layout) const;
161 /// Checks if the paragraph contains only text and no inset or font change.
162 bool onlyText(Buffer const & buf, Font const & outerfont,
163 pos_type initial) const;
165 /// match a string against a particular point in the paragraph
166 bool isTextAt(string const & str, pos_type pos) const;
168 /// Which Paragraph owns us?
172 Inset * inset_owner_;
180 static unsigned int paragraph_id;
182 ParagraphParameters params_;
184 /// for recording and looking up changes
188 InsetList insetlist_;
194 pos_type begin_of_body_;
196 typedef docstring TextContainer;
202 // Initialization of the counter for the paragraph id's,
203 unsigned int Paragraph::Private::paragraph_id = 0;
207 struct special_phrase {
213 special_phrase const special_phrases[] = {
214 { "LyX", from_ascii("\\LyX{}"), false },
215 { "TeX", from_ascii("\\TeX{}"), true },
216 { "LaTeX2e", from_ascii("\\LaTeXe{}"), true },
217 { "LaTeX", from_ascii("\\LaTeX{}"), true },
220 size_t const phrases_nr = sizeof(special_phrases)/sizeof(special_phrase);
225 Paragraph::Private::Private(Paragraph * owner)
226 : owner_(owner), inset_owner_(0), begin_of_body_(0)
228 id_ = paragraph_id++;
233 Paragraph::Private::Private(Private const & p, Paragraph * owner)
234 : owner_(owner), inset_owner_(p.inset_owner_), fontlist_(p.fontlist_),
235 params_(p.params_), changes_(p.changes_), insetlist_(p.insetlist_),
236 layout_(p.layout_), begin_of_body_(p.begin_of_body_), text_(p.text_)
238 id_ = paragraph_id++;
242 bool Paragraph::isChanged(pos_type start, pos_type end) const
244 BOOST_ASSERT(start >= 0 && start <= size());
245 BOOST_ASSERT(end > start && end <= size() + 1);
247 return d->changes_.isChanged(start, end);
251 bool Paragraph::isMergedOnEndOfParDeletion(bool trackChanges) const
253 // keep the logic here in sync with the logic of eraseChars()
257 Change const change = d->changes_.lookup(size());
258 return change.type == Change::INSERTED && change.author == 0;
262 void Paragraph::setChange(Change const & change)
264 // beware of the imaginary end-of-par character!
265 d->changes_.set(change, 0, size() + 1);
268 * Propagate the change recursively - but not in case of DELETED!
270 * Imagine that your co-author makes changes in an existing inset. He
271 * sends your document to you and you come to the conclusion that the
272 * inset should go completely. If you erase it, LyX must not delete all
273 * text within the inset. Otherwise, the change tracked insertions of
274 * your co-author get lost and there is no way to restore them later.
276 * Conclusion: An inset's content should remain untouched if you delete it
279 if (change.type != Change::DELETED) {
280 for (pos_type pos = 0; pos < size(); ++pos) {
281 if (Inset * inset = getInset(pos))
282 inset->setChange(change);
288 void Paragraph::setChange(pos_type pos, Change const & change)
290 BOOST_ASSERT(pos >= 0 && pos <= size());
291 d->changes_.set(change, pos);
293 // see comment in setChange(Change const &) above
294 if (change.type != Change::DELETED && pos < size())
295 if (Inset * inset = getInset(pos))
296 inset->setChange(change);
300 Change const & Paragraph::lookupChange(pos_type pos) const
302 BOOST_ASSERT(pos >= 0 && pos <= size());
303 return d->changes_.lookup(pos);
307 void Paragraph::acceptChanges(BufferParams const & bparams, pos_type start,
310 BOOST_ASSERT(start >= 0 && start <= size());
311 BOOST_ASSERT(end > start && end <= size() + 1);
313 for (pos_type pos = start; pos < end; ++pos) {
314 switch (lookupChange(pos).type) {
315 case Change::UNCHANGED:
316 // accept changes in nested inset
317 if (Inset * inset = getInset(pos))
318 inset->acceptChanges(bparams);
321 case Change::INSERTED:
322 d->changes_.set(Change(Change::UNCHANGED), pos);
323 // also accept changes in nested inset
324 if (Inset * inset = getInset(pos))
325 inset->acceptChanges(bparams);
328 case Change::DELETED:
329 // Suppress access to non-existent
330 // "end-of-paragraph char"
332 eraseChar(pos, false);
343 void Paragraph::rejectChanges(BufferParams const & bparams,
344 pos_type start, pos_type end)
346 BOOST_ASSERT(start >= 0 && start <= size());
347 BOOST_ASSERT(end > start && end <= size() + 1);
349 for (pos_type pos = start; pos < end; ++pos) {
350 switch (lookupChange(pos).type) {
351 case Change::UNCHANGED:
352 // reject changes in nested inset
353 if (Inset * inset = getInset(pos))
354 inset->rejectChanges(bparams);
357 case Change::INSERTED:
358 // Suppress access to non-existent
359 // "end-of-paragraph char"
361 eraseChar(pos, false);
367 case Change::DELETED:
368 d->changes_.set(Change(Change::UNCHANGED), pos);
370 // Do NOT reject changes within a deleted inset!
371 // There may be insertions of a co-author inside of it!
379 void Paragraph::Private::insertChar(pos_type pos, char_type c,
380 Change const & change)
382 BOOST_ASSERT(pos >= 0 && pos <= int(text_.size()));
385 changes_.insert(change, pos);
387 // This is actually very common when parsing buffers (and
388 // maybe inserting ascii text)
389 if (pos == pos_type(text_.size())) {
390 // when appending characters, no need to update tables
395 text_.insert(text_.begin() + pos, c);
397 // Update the font table.
398 fontlist_.increasePosAfterPos(pos);
401 insetlist_.increasePosAfterPos(pos);
405 void Paragraph::insertInset(pos_type pos, Inset * inset,
406 Change const & change)
409 BOOST_ASSERT(pos >= 0 && pos <= size());
411 d->insertChar(pos, META_INSET, change);
412 BOOST_ASSERT(d->text_[pos] == META_INSET);
414 // Add a new entry in the insetlist_.
415 d->insetlist_.insert(inset, pos);
419 bool Paragraph::eraseChar(pos_type pos, bool trackChanges)
421 BOOST_ASSERT(pos >= 0 && pos <= size());
423 // keep the logic here in sync with the logic of isMergedOnEndOfParDeletion()
426 Change change = d->changes_.lookup(pos);
428 // set the character to DELETED if
429 // a) it was previously unchanged or
430 // b) it was inserted by a co-author
432 if (change.type == Change::UNCHANGED ||
433 (change.type == Change::INSERTED && change.author != 0)) {
434 setChange(pos, Change(Change::DELETED));
438 if (change.type == Change::DELETED)
442 // Don't physically access the imaginary end-of-paragraph character.
443 // eraseChar() can only mark it as DELETED. A physical deletion of
444 // end-of-par must be handled externally.
450 d->changes_.erase(pos);
452 // if it is an inset, delete the inset entry
453 if (d->text_[pos] == META_INSET)
454 d->insetlist_.erase(pos);
456 d->text_.erase(d->text_.begin() + pos);
458 // Update the fontlist_
459 d->fontlist_.erase(pos);
461 // Update the insetlist_
462 d->insetlist_.decreasePosAfterPos(pos);
468 int Paragraph::eraseChars(pos_type start, pos_type end, bool trackChanges)
470 BOOST_ASSERT(start >= 0 && start <= size());
471 BOOST_ASSERT(end >= start && end <= size() + 1);
474 for (pos_type count = end - start; count; --count) {
475 if (!eraseChar(i, trackChanges))
482 int Paragraph::Private::latexSurrogatePair(odocstream & os, char_type c,
483 char_type next, Encoding const & encoding)
485 // Writing next here may circumvent a possible font change between
486 // c and next. Since next is only output if it forms a surrogate pair
487 // with c we can ignore this:
488 // A font change inside a surrogate pair does not make sense and is
489 // hopefully impossible to input.
490 // FIXME: change tracking
491 // Is this correct WRT change tracking?
492 docstring const latex1 = encoding.latexChar(next);
493 docstring const latex2 = encoding.latexChar(c);
494 os << latex1 << '{' << latex2 << '}';
495 return latex1.length() + latex2.length() + 2;
499 bool Paragraph::Private::simpleTeXBlanks(OutputParams const & runparams,
500 odocstream & os, TexRow & texrow,
502 unsigned int & column,
504 Layout const & style)
506 if (style.pass_thru || runparams.verbatim)
509 if (i + 1 < int(text_.size())) {
510 char_type next = text_[i + 1];
511 if (Encodings::isCombiningChar(next)) {
512 Encoding const & encoding = *(runparams.encoding);
513 // This space has an accent, so we must always output it.
514 column += latexSurrogatePair(os, ' ', next, encoding) - 1;
519 if (lyxrc.plaintext_linelen > 0
520 && column > lyxrc.plaintext_linelen
522 && text_[i - 1] != ' '
523 && (i + 1 < int(text_.size()))
524 // same in FreeSpacing mode
525 && !owner_->isFreeSpacing()
526 // In typewriter mode, we want to avoid
527 // ! . ? : at the end of a line
528 && !(font.fontInfo().family() == TYPEWRITER_FAMILY
529 && (text_[i - 1] == '.'
530 || text_[i - 1] == '?'
531 || text_[i - 1] == ':'
532 || text_[i - 1] == '!'))) {
535 texrow.start(owner_->id(), i + 1);
537 } else if (style.free_spacing) {
546 int Paragraph::Private::writeScriptChars(odocstream & os,
547 docstring const & ltx,
548 Change & runningChange,
549 Encoding const & encoding,
552 // FIXME: modifying i here is not very nice...
554 // We only arrive here when a proper language for character text_[i] has
555 // not been specified (i.e., it could not be translated in the current
556 // latex encoding) and it belongs to a known script.
557 // Parameter ltx contains the latex translation of text_[i] as specified in
558 // the unicodesymbols file and is something like "\textXXX{<spec>}".
559 // The latex macro name "textXXX" specifies the script to which text_[i]
560 // belongs and we use it in order to check whether characters from the
561 // same script immediately follow, such that we can collect them in a
562 // single "\textXXX" macro. So, we have to retain "\textXXX{<spec>"
563 // for the first char but only "<spec>" for all subsequent chars.
564 docstring::size_type const brace1 = ltx.find_first_of(from_ascii("{"));
565 docstring::size_type const brace2 = ltx.find_last_of(from_ascii("}"));
566 string script = to_ascii(ltx.substr(1, brace1 - 1));
567 int length = ltx.substr(0, brace2).length();
568 os << ltx.substr(0, brace2);
569 int size = text_.size();
570 while (i + 1 < size) {
571 char_type const next = text_[i + 1];
572 // Stop here if next character belongs to another script
573 // or there is a change in change tracking status.
574 if (!Encodings::isKnownScriptChar(next, script) ||
575 runningChange != owner_->lookupChange(i + 1))
579 FontList::const_iterator cit = fontlist_.begin();
580 FontList::const_iterator end = fontlist_.end();
581 for (; cit != end; ++cit) {
582 if (cit->pos() >= i && !found) {
583 prev_font = cit->font();
586 if (cit->pos() >= i + 1)
589 // Stop here if there is a font attribute or encoding change.
590 if (found && cit != end && prev_font != cit->font())
592 docstring const latex = encoding.latexChar(next);
593 docstring::size_type const b1 =
594 latex.find_first_of(from_ascii("{"));
595 docstring::size_type const b2 =
596 latex.find_last_of(from_ascii("}"));
597 int const len = b2 - b1 - 1;
598 os << latex.substr(b1 + 1, len);
608 bool Paragraph::Private::isTextAt(string const & str, pos_type pos) const
610 pos_type const len = str.length();
612 // is the paragraph large enough?
613 if (pos + len > int(text_.size()))
616 // does the wanted text start at point?
617 for (string::size_type i = 0; i < str.length(); ++i) {
618 // Caution: direct comparison of characters works only
619 // because str is pure ASCII.
620 if (str[i] != text_[pos + i])
624 return fontlist_.hasChangeInRange(pos, len);
628 void Paragraph::Private::latexInset(Buffer const & buf,
629 BufferParams const & bparams,
632 OutputParams & runparams,
635 Font const & outerfont,
637 Change & running_change,
638 Layout const & style,
640 unsigned int & column)
642 Inset * inset = owner_->getInset(i);
645 if (style.pass_thru) {
646 inset->plaintext(buf, os, runparams);
650 // FIXME: move this to InsetNewline::latex
651 if (inset->lyxCode() == NEWLINE_CODE) {
652 // newlines are handled differently here than
653 // the default in simpleTeXSpecialChars().
654 if (!style.newline_allowed) {
658 column += running_font.latexWriteEndChanges(
659 os, bparams, runparams,
664 if (running_font.fontInfo().family() == TYPEWRITER_FAMILY)
667 basefont = owner_->getLayoutFont(bparams, outerfont);
668 running_font = basefont;
670 if (runparams.moving_arg)
675 texrow.start(owner_->id(), i + 1);
679 if (owner_->lookupChange(i).type == Change::DELETED) {
680 if( ++runparams.inDeletedInset == 1)
681 runparams.changeOfDeletedInset = owner_->lookupChange(i);
684 if (inset->canTrackChanges()) {
685 column += Changes::latexMarkChange(os, bparams, running_change,
686 Change(Change::UNCHANGED));
687 running_change = Change(Change::UNCHANGED);
691 odocstream::pos_type const len = os.tellp();
693 if (inset->forceLTR()
694 && running_font.isRightToLeft()
695 // ERT is an exception, it should be output with no decorations at all
696 && inset->lyxCode() != ERT_CODE) {
697 if (running_font.language()->lang() == "farsi")
704 // FIXME: Bug: we can have an empty font change here!
705 // if there has just been a font change, we are going to close it
706 // right now, which means stupid latex code like \textsf{}. AFAIK,
707 // this does not harm dvi output. A minor bug, thus (JMarc)
709 // Some insets cannot be inside a font change command.
710 // However, even such insets *can* be placed in \L or \R
711 // or their equivalents (for RTL language switches), so we don't
712 // close the language in those cases.
713 // ArabTeX, though, cannot handle this special behavior, it seems.
714 bool arabtex = basefont.language()->lang() == "arabic_arabtex"
715 || running_font.language()->lang() == "arabic_arabtex";
716 if (open_font && inset->noFontChange()) {
717 bool closeLanguage = arabtex
718 || basefont.isRightToLeft() == running_font.isRightToLeft();
719 unsigned int count = running_font.latexWriteEndChanges(os,
720 bparams, runparams, basefont, basefont, closeLanguage);
722 // if any font properties were closed, update the running_font,
723 // making sure, however, to leave the language as it was
725 // FIXME: probably a better way to keep track of the old
726 // language, than copying the entire font?
727 Font const copy_font(running_font);
728 basefont = owner_->getLayoutFont(bparams, outerfont);
729 running_font = basefont;
731 running_font.setLanguage(copy_font.language());
732 // leave font open if language is still open
733 open_font = (running_font.language() == basefont.language());
735 runparams.local_font = &basefont;
739 int tmp = inset->latex(buf, os, runparams);
742 if (running_font.language()->lang() == "farsi")
749 for (int j = 0; j < tmp; ++j)
752 texrow.start(owner_->id(), i + 1);
755 column += os.tellp() - len;
758 if (owner_->lookupChange(i).type == Change::DELETED)
759 --runparams.inDeletedInset;
763 void Paragraph::Private::latexSpecialChar(
765 OutputParams & runparams,
767 Change & running_change,
768 Layout const & style,
770 unsigned int & column)
772 char_type const c = text_[i];
774 if (style.pass_thru) {
776 // FIXME UNICODE: This can fail if c cannot
777 // be encoded in the current encoding.
782 if (runparams.verbatim) {
787 if (lyxrc.fontenc == "T1" && latexSpecialT1(c, os, i, column))
790 if (running_font.fontInfo().family() == TYPEWRITER_FAMILY
791 && latexSpecialTypewriter(c, os, i, column))
794 // Otherwise, we use what LaTeX provides us.
797 os << "\\textbackslash{}";
801 os << "\\textless{}";
805 os << "\\textgreater{}";
816 os << "\\char`\\\"{}";
821 case '%': case '#': case '{':
829 os << "\\textasciitilde{}";
834 os << "\\textasciicircum{}";
839 // avoid being mistaken for optional arguments
847 // Blanks are printed before font switching.
848 // Sure? I am not! (try nice-latex)
849 // I am sure it's correct. LyX might be smarter
850 // in the future, but for now, nothing wrong is
857 if (latexSpecialPhrase(os, i, column, runparams))
863 Encoding const & encoding = *(runparams.encoding);
864 if (i + 1 < int(text_.size())) {
865 char_type next = text_[i + 1];
866 if (Encodings::isCombiningChar(next)) {
867 column += latexSurrogatePair(os, c, next, encoding) - 1;
873 docstring const latex = encoding.latexChar(c);
874 if (Encodings::isKnownScriptChar(c, script)
875 && prefixIs(latex, from_ascii("\\" + script)))
876 column += writeScriptChars(os, latex,
877 running_change, encoding, i) - 1;
878 else if (latex.length() > 1 && latex[latex.length() - 1] != '}') {
879 // Prevent eating of a following
880 // space or command corruption by
881 // following characters
882 column += latex.length() + 1;
885 column += latex.length() - 1;
893 bool Paragraph::Private::latexSpecialT1(char_type const c, odocstream & os,
894 pos_type & i, unsigned int & column)
900 // In T1 encoding, these characters exist
901 // but we should avoid ligatures
902 if (i + 1 >= int(text_.size()) || text_[i + 1] != c)
907 //os << "\\textcompwordmark{}";
919 bool Paragraph::Private::latexSpecialTypewriter(char_type const c, odocstream & os,
920 pos_type & i, unsigned int & column)
924 if (i + 1 < int(text_.size()) && text_[i + 1] == '-') {
925 // "--" in Typewriter mode -> "-{}-"
932 // I assume this is hack treating typewriter as verbatim
933 // FIXME UNICODE: This can fail if c cannot be encoded
934 // in the current encoding.
939 // Those characters are not directly supported.
943 case '%': case '#': case '{':
952 // With Typewriter font, these characters exist.
959 bool Paragraph::Private::latexSpecialPhrase(odocstream & os, pos_type & i,
960 unsigned int & column, OutputParams & runparams)
962 // FIXME: if we have "LaTeX" with a font
963 // change in the middle (before the 'T', then
964 // the "TeX" part is still special cased.
965 // Really we should only operate this on
966 // "words" for some definition of word
968 for (size_t pnr = 0; pnr < phrases_nr; ++pnr) {
969 if (!isTextAt(special_phrases[pnr].phrase, i))
971 if (runparams.moving_arg)
973 os << special_phrases[pnr].macro;
974 i += special_phrases[pnr].phrase.length() - 1;
975 column += special_phrases[pnr].macro.length() - 1;
982 void Paragraph::Private::validate(LaTeXFeatures & features,
983 Layout const & layout) const
986 if (!params_.spacing().isDefault())
987 features.require("setspace");
990 features.useLayout(layout.name());
993 fontlist_.validate(features);
995 // then the indentation
996 if (!params_.leftIndent().zero())
997 features.require("ParagraphLeftIndent");
1000 InsetList::const_iterator icit = insetlist_.begin();
1001 InsetList::const_iterator iend = insetlist_.end();
1002 for (; icit != iend; ++icit) {
1004 icit->inset->validate(features);
1005 if (layout.needprotect &&
1006 icit->inset->lyxCode() == FOOT_CODE)
1007 features.require("NeedLyXFootnoteCode");
1011 // then the contents
1012 for (pos_type i = 0; i < int(text_.size()) ; ++i) {
1013 for (size_t pnr = 0; pnr < phrases_nr; ++pnr) {
1014 if (!special_phrases[pnr].builtin
1015 && isTextAt(special_phrases[pnr].phrase, i)) {
1016 features.require(special_phrases[pnr].phrase);
1020 Encodings::validate(text_[i], features);
1024 /////////////////////////////////////////////////////////////////////
1028 /////////////////////////////////////////////////////////////////////
1030 Paragraph::Paragraph()
1031 : d(new Paragraph::Private(this))
1038 Paragraph::Paragraph(Paragraph const & par)
1039 : itemdepth(par.itemdepth),
1040 d(new Paragraph::Private(*par.d, this))
1045 Paragraph & Paragraph::operator=(Paragraph const & par)
1047 // needed as we will destroy the private part before copying it
1049 itemdepth = par.itemdepth;
1052 d = new Private(*par.d, this);
1058 Paragraph::~Paragraph()
1064 void Paragraph::write(Buffer const & buf, ostream & os,
1065 BufferParams const & bparams,
1066 depth_type & dth) const
1068 // The beginning or end of a deeper (i.e. nested) area?
1069 if (dth != d->params_.depth()) {
1070 if (d->params_.depth() > dth) {
1071 while (d->params_.depth() > dth) {
1072 os << "\n\\begin_deeper";
1076 while (d->params_.depth() < dth) {
1077 os << "\n\\end_deeper";
1083 // First write the layout
1084 os << "\n\\begin_layout " << to_utf8(d->layout_->name()) << '\n';
1086 d->params_.write(os);
1088 Font font1(inherit_font, bparams.language);
1090 Change running_change = Change(Change::UNCHANGED);
1093 for (pos_type i = 0; i <= size(); ++i) {
1095 Change change = lookupChange(i);
1096 Changes::lyxMarkChange(os, column, running_change, change);
1097 running_change = change;
1102 // Write font changes
1103 Font font2 = getFontSettings(bparams, i);
1104 if (font2 != font1) {
1105 font2.lyxWriteChanges(font1, os);
1110 char_type const c = d->text_[i];
1113 if (Inset const * inset = getInset(i)) {
1114 if (inset->directWrite()) {
1115 // international char, let it write
1116 // code directly so it's shorter in
1118 inset->write(buf, os);
1122 os << "\\begin_inset ";
1123 inset->write(buf, os);
1124 os << "\n\\end_inset\n\n";
1130 os << "\n\\backslash\n";
1134 if (i + 1 < size() && d->text_[i + 1] == ' ') {
1141 if ((column > 70 && c == ' ')
1146 // this check is to amend a bug. LyX sometimes
1147 // inserts '\0' this could cause problems.
1149 os << to_utf8(docstring(1, c));
1151 lyxerr << "ERROR (Paragraph::writeFile):"
1152 " NULL char in structure." << endl;
1158 os << "\n\\end_layout\n";
1162 void Paragraph::validate(LaTeXFeatures & features) const
1164 d->validate(features, *d->layout_);
1168 void Paragraph::insert(pos_type start, docstring const & str,
1169 Font const & font, Change const & change)
1171 for (size_t i = 0, n = str.size(); i != n ; ++i)
1172 insertChar(start + i, str[i], font, change);
1176 void Paragraph::appendChar(char_type c, Font const & font,
1177 Change const & change)
1180 d->changes_.insert(change, d->text_.size());
1181 // when appending characters, no need to update tables
1182 d->text_.push_back(c);
1183 setFont(d->text_.size() - 1, font);
1187 void Paragraph::appendString(docstring const & s, Font const & font,
1188 Change const & change)
1190 pos_type end = s.size();
1191 size_t oldsize = d->text_.size();
1192 size_t newsize = oldsize + end;
1193 size_t capacity = d->text_.capacity();
1194 if (newsize >= capacity)
1195 d->text_.reserve(max(capacity + 100, newsize));
1197 // when appending characters, no need to update tables
1200 // FIXME: Optimize this!
1201 for (pos_type i = 0; i != end; ++i) {
1203 d->changes_.insert(change, i);
1205 d->fontlist_.set(oldsize, font);
1206 d->fontlist_.set(newsize - 1, font);
1210 void Paragraph::insertChar(pos_type pos, char_type c,
1213 d->insertChar(pos, c, Change(trackChanges ?
1214 Change::INSERTED : Change::UNCHANGED));
1218 void Paragraph::insertChar(pos_type pos, char_type c,
1219 Font const & font, bool trackChanges)
1221 d->insertChar(pos, c, Change(trackChanges ?
1222 Change::INSERTED : Change::UNCHANGED));
1227 void Paragraph::insertChar(pos_type pos, char_type c,
1228 Font const & font, Change const & change)
1230 d->insertChar(pos, c, change);
1235 void Paragraph::insertInset(pos_type pos, Inset * inset,
1236 Font const & font, Change const & change)
1238 insertInset(pos, inset, change);
1239 // Set the font/language of the inset...
1244 bool Paragraph::insetAllowed(InsetCode code)
1246 return !d->inset_owner_ || d->inset_owner_->insetAllowed(code);
1250 void Paragraph::resetFonts(Font const & font)
1252 d->fontlist_.clear();
1253 d->fontlist_.set(0, font);
1254 d->fontlist_.set(d->text_.size() - 1, font);
1257 // Gets uninstantiated font setting at position.
1258 Font const Paragraph::getFontSettings(BufferParams const & bparams,
1262 lyxerr << " pos: " << pos << " size: " << size() << endl;
1263 BOOST_ASSERT(pos <= size());
1266 FontList::const_iterator cit = d->fontlist_.fontIterator(pos);
1267 if (cit != d->fontlist_.end())
1270 if (pos == size() && !empty())
1271 return getFontSettings(bparams, pos - 1);
1273 return Font(inherit_font, getParLanguage(bparams));
1277 FontSpan Paragraph::fontSpan(pos_type pos) const
1279 BOOST_ASSERT(pos <= size());
1282 FontList::const_iterator cit = d->fontlist_.begin();
1283 FontList::const_iterator end = d->fontlist_.end();
1284 for (; cit != end; ++cit) {
1285 if (cit->pos() >= pos) {
1286 if (pos >= beginOfBody())
1287 return FontSpan(max(start, beginOfBody()),
1290 return FontSpan(start,
1291 min(beginOfBody() - 1,
1294 start = cit->pos() + 1;
1297 // This should not happen, but if so, we take no chances.
1298 //lyxerr << "Paragraph::getEndPosOfFontSpan: This should not happen!"
1300 return FontSpan(pos, pos);
1304 // Gets uninstantiated font setting at position 0
1305 Font const Paragraph::getFirstFontSettings(BufferParams const & bparams) const
1307 if (!empty() && !d->fontlist_.empty())
1308 return d->fontlist_.begin()->font();
1310 return Font(inherit_font, bparams.language);
1314 // Gets the fully instantiated font at a given position in a paragraph
1315 // This is basically the same function as Text::GetFont() in text2.cpp.
1316 // The difference is that this one is used for generating the LaTeX file,
1317 // and thus cosmetic "improvements" are disallowed: This has to deliver
1318 // the true picture of the buffer. (Asger)
1319 Font const Paragraph::getFont(BufferParams const & bparams, pos_type pos,
1320 Font const & outerfont) const
1322 BOOST_ASSERT(pos >= 0);
1324 Font font = getFontSettings(bparams, pos);
1326 pos_type const body_pos = beginOfBody();
1328 font.fontInfo().realize(d->layout_->labelfont);
1330 font.fontInfo().realize(d->layout_->font);
1332 font.fontInfo().realize(outerfont.fontInfo());
1333 font.fontInfo().realize(bparams.getFont().fontInfo());
1339 Font const Paragraph::getLabelFont
1340 (BufferParams const & bparams, Font const & outerfont) const
1342 FontInfo tmpfont = d->layout_->labelfont;
1343 tmpfont.realize(outerfont.fontInfo());
1344 tmpfont.realize(bparams.getFont().fontInfo());
1345 return Font(tmpfont, getParLanguage(bparams));
1349 Font const Paragraph::getLayoutFont
1350 (BufferParams const & bparams, Font const & outerfont) const
1352 FontInfo tmpfont = d->layout_->font;
1353 tmpfont.realize(outerfont.fontInfo());
1354 tmpfont.realize(bparams.getFont().fontInfo());
1355 return Font(tmpfont, getParLanguage(bparams));
1359 /// Returns the height of the highest font in range
1360 FontSize Paragraph::highestFontInRange
1361 (pos_type startpos, pos_type endpos, FontSize def_size) const
1363 return d->fontlist_.highestInRange(startpos, endpos, def_size);
1368 Paragraph::getUChar(BufferParams const & bparams, pos_type pos) const
1370 char_type c = d->text_[pos];
1371 if (!lyxrc.rtl_support)
1401 if (uc != c && getFontSettings(bparams, pos).isRightToLeft())
1408 void Paragraph::setFont(pos_type pos, Font const & font)
1410 BOOST_ASSERT(pos <= size());
1412 // First, reduce font against layout/label font
1413 // Update: The setCharFont() routine in text2.cpp already
1414 // reduces font, so we don't need to do that here. (Asger)
1416 d->fontlist_.set(pos, font);
1420 void Paragraph::makeSameLayout(Paragraph const & par)
1422 d->layout_ = par.d->layout_;
1423 d->params_ = par.d->params_;
1427 bool Paragraph::stripLeadingSpaces(bool trackChanges)
1429 if (isFreeSpacing())
1435 while (pos < size() && (isNewline(pos) || isLineSeparator(pos))) {
1436 if (eraseChar(pos, trackChanges))
1442 return count > 0 || pos > 0;
1446 bool Paragraph::hasSameLayout(Paragraph const & par) const
1448 return par.d->layout_ == d->layout_ && d->params_.sameLayout(par.d->params_);
1452 depth_type Paragraph::getDepth() const
1454 return d->params_.depth();
1458 depth_type Paragraph::getMaxDepthAfter() const
1460 if (d->layout_->isEnvironment())
1461 return d->params_.depth() + 1;
1463 return d->params_.depth();
1467 char Paragraph::getAlign() const
1469 if (d->params_.align() == LYX_ALIGN_LAYOUT)
1470 return d->layout_->align;
1472 return d->params_.align();
1476 docstring const & Paragraph::getLabelstring() const
1478 return d->params_.labelString();
1482 // the next two functions are for the manual labels
1483 docstring const Paragraph::getLabelWidthString() const
1485 if (d->layout_->margintype == MARGIN_MANUAL)
1486 return d->params_.labelWidthString();
1488 return _("Senseless with this layout!");
1492 void Paragraph::setLabelWidthString(docstring const & s)
1494 d->params_.labelWidthString(s);
1498 docstring const Paragraph::translateIfPossible(docstring const & s,
1499 BufferParams const & bparams) const
1501 if (!isAscii(s) || s.empty()) {
1502 // This must be a user defined layout. We cannot translate
1503 // this, since gettext accepts only ascii keys.
1506 // Probably standard layout, try to translate
1507 Messages & m = getMessages(getParLanguage(bparams)->code());
1508 return m.get(to_ascii(s));
1512 docstring Paragraph::expandLabel(LayoutPtr const & layout,
1513 BufferParams const & bparams, bool process_appendix) const
1515 TextClass const & tclass = bparams.getTextClass();
1518 if (process_appendix && d->params_.appendix())
1519 fmt = translateIfPossible(layout->labelstring_appendix(),
1522 fmt = translateIfPossible(layout->labelstring(), bparams);
1524 if (fmt.empty() && layout->labeltype == LABEL_COUNTER
1525 && !layout->counter.empty())
1526 fmt = "\\the" + layout->counter;
1528 // handle 'inherited level parts' in 'fmt',
1529 // i.e. the stuff between '@' in '@Section@.\arabic{subsection}'
1530 size_t const i = fmt.find('@', 0);
1531 if (i != docstring::npos) {
1532 size_t const j = fmt.find('@', i + 1);
1533 if (j != docstring::npos) {
1534 docstring parent(fmt, i + 1, j - i - 1);
1535 docstring label = from_ascii("??");
1536 if (tclass.hasLayout(parent))
1537 docstring label = expandLabel(tclass[parent], bparams,
1539 fmt = docstring(fmt, 0, i) + label
1540 + docstring(fmt, j + 1, docstring::npos);
1544 return tclass.counters().counterLabel(fmt);
1548 void Paragraph::applyLayout(LayoutPtr const & new_layout)
1550 d->layout_ = new_layout;
1551 LyXAlignment const oldAlign = d->params_.align();
1553 if (!(oldAlign & d->layout_->alignpossible)) {
1554 frontend::Alert::warning(_("Alignment not permitted"),
1555 _("The new layout does not permit the alignment previously used.\nSetting to default."));
1556 d->params_.align(LYX_ALIGN_LAYOUT);
1561 pos_type Paragraph::beginOfBody() const
1563 return d->begin_of_body_;
1567 void Paragraph::setBeginOfBody()
1569 if (d->layout_->labeltype != LABEL_MANUAL) {
1570 d->begin_of_body_ = 0;
1574 // Unroll the first two cycles of the loop
1575 // and remember the previous character to
1576 // remove unnecessary getChar() calls
1578 pos_type end = size();
1579 if (i < end && !isNewline(i)) {
1581 char_type previous_char = 0;
1584 previous_char = d->text_[i];
1585 if (!isNewline(i)) {
1587 while (i < end && previous_char != ' ') {
1592 previous_char = temp;
1598 d->begin_of_body_ = i;
1602 bool Paragraph::forceEmptyLayout() const
1604 return inInset() && inInset()->forceEmptyLayout();
1608 bool Paragraph::allowParagraphCustomization() const
1610 return inInset() && inInset()->allowParagraphCustomization(0);
1614 bool Paragraph::useEmptyLayout() const
1616 return inInset() && inInset()->useEmptyLayout();
1622 // paragraphs inside floats need different alignment tags to avoid
1625 bool noTrivlistCentering(InsetCode code)
1627 return code == FLOAT_CODE || code == WRAP_CODE;
1631 string correction(string const & orig)
1633 if (orig == "flushleft")
1634 return "raggedright";
1635 if (orig == "flushright")
1636 return "raggedleft";
1637 if (orig == "center")
1643 string const corrected_env(string const & suffix, string const & env,
1646 string output = suffix + "{";
1647 if (noTrivlistCentering(code))
1648 output += correction(env);
1652 if (suffix == "\\begin")
1658 void adjust_row_column(string const & str, TexRow & texrow, int & column)
1660 if (!contains(str, "\n"))
1661 column += str.size();
1665 column = rsplit(str, tmp, '\n').size();
1672 int Paragraph::Private::startTeXParParams(BufferParams const & bparams,
1673 odocstream & os, TexRow & texrow,
1674 bool moving_arg) const
1678 if (params_.noindent()) {
1679 os << "\\noindent ";
1683 LyXAlignment const curAlign = params_.align();
1685 if (curAlign == layout_->align)
1689 case LYX_ALIGN_NONE:
1690 case LYX_ALIGN_BLOCK:
1691 case LYX_ALIGN_LAYOUT:
1692 case LYX_ALIGN_SPECIAL:
1694 case LYX_ALIGN_LEFT:
1695 case LYX_ALIGN_RIGHT:
1696 case LYX_ALIGN_CENTER:
1705 case LYX_ALIGN_NONE:
1706 case LYX_ALIGN_BLOCK:
1707 case LYX_ALIGN_LAYOUT:
1708 case LYX_ALIGN_SPECIAL:
1710 case LYX_ALIGN_LEFT: {
1712 if (owner_->getParLanguage(bparams)->babel() != "hebrew")
1713 output = corrected_env("\\begin", "flushleft", owner_->ownerCode());
1715 output = corrected_env("\\begin", "flushright", owner_->ownerCode());
1716 os << from_ascii(output);
1717 adjust_row_column(output, texrow, column);
1719 } case LYX_ALIGN_RIGHT: {
1721 if (owner_->getParLanguage(bparams)->babel() != "hebrew")
1722 output = corrected_env("\\begin", "flushright", owner_->ownerCode());
1724 output = corrected_env("\\begin", "flushleft", owner_->ownerCode());
1725 os << from_ascii(output);
1726 adjust_row_column(output, texrow, column);
1728 } case LYX_ALIGN_CENTER: {
1730 output = corrected_env("\\begin", "center", owner_->ownerCode());
1731 os << from_ascii(output);
1732 adjust_row_column(output, texrow, column);
1741 int Paragraph::Private::endTeXParParams(BufferParams const & bparams,
1742 odocstream & os, TexRow & texrow,
1743 bool moving_arg) const
1747 switch (params_.align()) {
1748 case LYX_ALIGN_NONE:
1749 case LYX_ALIGN_BLOCK:
1750 case LYX_ALIGN_LAYOUT:
1751 case LYX_ALIGN_SPECIAL:
1753 case LYX_ALIGN_LEFT:
1754 case LYX_ALIGN_RIGHT:
1755 case LYX_ALIGN_CENTER:
1763 switch (params_.align()) {
1764 case LYX_ALIGN_NONE:
1765 case LYX_ALIGN_BLOCK:
1766 case LYX_ALIGN_LAYOUT:
1767 case LYX_ALIGN_SPECIAL:
1769 case LYX_ALIGN_LEFT: {
1771 if (owner_->getParLanguage(bparams)->babel() != "hebrew")
1772 output = corrected_env("\n\\par\\end", "flushleft", owner_->ownerCode());
1774 output = corrected_env("\n\\par\\end", "flushright", owner_->ownerCode());
1775 os << from_ascii(output);
1776 adjust_row_column(output, texrow, column);
1778 } case LYX_ALIGN_RIGHT: {
1780 if (owner_->getParLanguage(bparams)->babel() != "hebrew")
1781 output = corrected_env("\n\\par\\end", "flushright", owner_->ownerCode());
1783 output = corrected_env("\n\\par\\end", "flushleft", owner_->ownerCode());
1784 os << from_ascii(output);
1785 adjust_row_column(output, texrow, column);
1787 } case LYX_ALIGN_CENTER: {
1789 output = corrected_env("\n\\par\\end", "center", owner_->ownerCode());
1790 os << from_ascii(output);
1791 adjust_row_column(output, texrow, column);
1800 // This one spits out the text of the paragraph
1801 bool Paragraph::latex(Buffer const & buf,
1802 BufferParams const & bparams,
1803 Font const & outerfont,
1804 odocstream & os, TexRow & texrow,
1805 OutputParams const & runparams) const
1807 LYXERR(Debug::LATEX, "SimpleTeXOnePar... " << this);
1809 bool return_value = false;
1813 // well we have to check if we are in an inset with unlimited
1814 // length (all in one row) if that is true then we don't allow
1815 // any special options in the paragraph and also we don't allow
1816 // any environment other than the default layout of the text class
1818 bool asdefault = forceEmptyLayout();
1821 style = bparams.getTextClass().defaultLayout();
1826 // Current base font for all inherited font changes, without any
1827 // change caused by an individual character, except for the language:
1828 // It is set to the language of the first character.
1829 // As long as we are in the label, this font is the base font of the
1830 // label. Before the first body character it is set to the base font
1834 // Maybe we have to create a optional argument.
1835 pos_type body_pos = beginOfBody();
1836 unsigned int column = 0;
1839 // the optional argument is kept in curly brackets in
1840 // case it contains a ']'
1843 basefont = getLabelFont(bparams, outerfont);
1845 basefont = getLayoutFont(bparams, outerfont);
1848 // Which font is currently active?
1849 Font running_font(basefont);
1850 // Do we have an open font change?
1851 bool open_font = false;
1853 Change runningChange = Change(Change::UNCHANGED);
1855 texrow.start(id(), 0);
1857 // if the paragraph is empty, the loop will not be entered at all
1859 if (style->isCommand()) {
1864 column += d->startTeXParParams(bparams, os, texrow,
1865 runparams.moving_arg);
1868 for (pos_type i = 0; i < size(); ++i) {
1869 // First char in paragraph or after label?
1870 if (i == body_pos) {
1873 column += running_font.latexWriteEndChanges(
1874 os, bparams, runparams,
1875 basefont, basefont);
1878 basefont = getLayoutFont(bparams, outerfont);
1879 running_font = basefont;
1881 column += Changes::latexMarkChange(os, bparams,
1882 runningChange, Change(Change::UNCHANGED));
1883 runningChange = Change(Change::UNCHANGED);
1888 if (style->isCommand()) {
1894 column += d->startTeXParParams(bparams, os,
1896 runparams.moving_arg);
1899 Change const & change = runparams.inDeletedInset ? runparams.changeOfDeletedInset
1902 if (bparams.outputChanges && runningChange != change) {
1904 column += running_font.latexWriteEndChanges(
1905 os, bparams, runparams, basefont, basefont);
1908 basefont = getLayoutFont(bparams, outerfont);
1909 running_font = basefont;
1911 column += Changes::latexMarkChange(os, bparams, runningChange, change);
1912 runningChange = change;
1915 // do not output text which is marked deleted
1916 // if change tracking output is disabled
1917 if (!bparams.outputChanges && change.type == Change::DELETED) {
1923 // Fully instantiated font
1924 Font const font = getFont(bparams, i, outerfont);
1926 Font const last_font = running_font;
1928 // Do we need to close the previous font?
1930 (font != running_font ||
1931 font.language() != running_font.language()))
1933 column += running_font.latexWriteEndChanges(
1934 os, bparams, runparams, basefont,
1935 (i == body_pos-1) ? basefont : font);
1936 running_font = basefont;
1940 // close babel's font environment before opening CJK.
1941 if (!running_font.language()->babel().empty() &&
1942 font.language()->encoding()->package() == Encoding::CJK) {
1943 string end_tag = subst(lyxrc.language_command_end,
1945 running_font.language()->babel());
1946 os << from_ascii(end_tag);
1947 column += end_tag.length();
1950 // Switch file encoding if necessary (and allowed)
1951 if (!runparams.verbatim &&
1952 runparams.encoding->package() == Encoding::none &&
1953 font.language()->encoding()->package() == Encoding::none) {
1954 pair<bool, int> const enc_switch = switchEncoding(os, bparams,
1955 runparams, *(font.language()->encoding()));
1956 if (enc_switch.first) {
1957 column += enc_switch.second;
1958 runparams.encoding = font.language()->encoding();
1962 char_type const c = d->text_[i];
1964 // Do we need to change font?
1965 if ((font != running_font ||
1966 font.language() != running_font.language()) &&
1969 odocstringstream ods;
1970 column += font.latexWriteStartChanges(ods, bparams,
1971 runparams, basefont,
1973 running_font = font;
1975 docstring fontchange = ods.str();
1976 // check if the fontchange ends with a trailing blank
1977 // (like "\small " (see bug 3382)
1978 if (suffixIs(fontchange, ' ') && c == ' ')
1979 os << fontchange.substr(0, fontchange.size() - 1)
1980 << from_ascii("{}");
1986 // FIXME: integrate this case in latexSpecialChar
1987 // Do not print the separation of the optional argument
1988 // if style->pass_thru is false. This works because
1989 // latexSpecialChar ignores spaces if
1990 // style->pass_thru is false.
1991 if (i != body_pos - 1) {
1992 if (d->simpleTeXBlanks(
1993 runparams, os, texrow,
1994 i, column, font, *style)) {
1995 // A surrogate pair was output. We
1996 // must not call latexSpecialChar
1997 // in this iteration, since it would output
1998 // the combining character again.
2005 OutputParams rp = runparams;
2006 rp.free_spacing = style->free_spacing;
2007 rp.local_font = &font;
2008 rp.intitle = style->intitle;
2010 // Two major modes: LaTeX or plain
2011 // Handle here those cases common to both modes
2012 // and then split to handle the two modes separately.
2013 if (c == META_INSET)
2014 d->latexInset(buf, bparams, os,
2015 texrow, rp, running_font,
2016 basefont, outerfont, open_font,
2017 runningChange, *style, i, column);
2020 d->latexSpecialChar(os, rp, running_font, runningChange,
2022 } catch (EncodingException & e) {
2023 if (runparams.dryrun) {
2024 os << "<" << _("LyX Warning: ")
2025 << _("uncodable character") << " '";
2029 // add location information and throw again.
2037 // Set the encoding to that returned from simpleTeXSpecialChars (see
2038 // comment for encoding member in OutputParams.h)
2039 runparams.encoding = rp.encoding;
2042 // If we have an open font definition, we have to close it
2044 #ifdef FIXED_LANGUAGE_END_DETECTION
2047 .latexWriteEndChanges(os, bparams, runparams,
2049 next_->getFont(bparams, 0, outerfont));
2051 running_font.latexWriteEndChanges(os, bparams,
2052 runparams, basefont, basefont);
2055 //FIXME: For now we ALWAYS have to close the foreign font settings if they are
2056 //FIXME: there as we start another \selectlanguage with the next paragraph if
2057 //FIXME: we are in need of this. This should be fixed sometime (Jug)
2058 running_font.latexWriteEndChanges(os, bparams, runparams,
2059 basefont, basefont);
2063 column += Changes::latexMarkChange(os, bparams, runningChange, Change(Change::UNCHANGED));
2065 // Needed if there is an optional argument but no contents.
2066 if (body_pos > 0 && body_pos == size()) {
2068 return_value = false;
2072 column += d->endTeXParParams(bparams, os, texrow,
2073 runparams.moving_arg);
2076 LYXERR(Debug::LATEX, "SimpleTeXOnePar...done " << this);
2077 return return_value;
2081 bool Paragraph::emptyTag() const
2083 for (pos_type i = 0; i < size(); ++i) {
2084 if (Inset const * inset = getInset(i)) {
2085 InsetCode lyx_code = inset->lyxCode();
2086 if (lyx_code != TOC_CODE &&
2087 lyx_code != INCLUDE_CODE &&
2088 lyx_code != GRAPHICS_CODE &&
2089 lyx_code != ERT_CODE &&
2090 lyx_code != LISTINGS_CODE &&
2091 lyx_code != FLOAT_CODE &&
2092 lyx_code != TABULAR_CODE) {
2096 char_type c = d->text_[i];
2097 if (c != ' ' && c != '\t')
2105 string Paragraph::getID(Buffer const & buf, OutputParams const & runparams) const
2107 for (pos_type i = 0; i < size(); ++i) {
2108 if (Inset const * inset = getInset(i)) {
2109 InsetCode lyx_code = inset->lyxCode();
2110 if (lyx_code == LABEL_CODE) {
2111 InsetLabel const * const il = static_cast<InsetLabel const *>(inset);
2112 docstring const & id = il->getParam("name");
2113 return "id='" + to_utf8(sgml::cleanID(buf, runparams, id)) + "'";
2121 pos_type Paragraph::getFirstWord(Buffer const & buf, odocstream & os, OutputParams const & runparams) const
2124 for (i = 0; i < size(); ++i) {
2125 if (Inset const * inset = getInset(i)) {
2126 inset->docbook(buf, os, runparams);
2128 char_type c = d->text_[i];
2131 os << sgml::escapeChar(c);
2138 bool Paragraph::Private::onlyText(Buffer const & buf, Font const & outerfont, pos_type initial) const
2141 pos_type size = text_.size();
2142 for (pos_type i = initial; i < size; ++i) {
2143 Font font = owner_->getFont(buf.params(), i, outerfont);
2144 if (text_[i] == META_INSET)
2146 if (i != initial && font != font_old)
2155 void Paragraph::simpleDocBookOnePar(Buffer const & buf,
2157 OutputParams const & runparams,
2158 Font const & outerfont,
2159 pos_type initial) const
2161 bool emph_flag = false;
2163 LayoutPtr const & style = d->layout_;
2165 style->labeltype == LABEL_MANUAL ? style->labelfont : style->font;
2167 if (style->pass_thru && !d->onlyText(buf, outerfont, initial))
2170 // parsing main loop
2171 for (pos_type i = initial; i < size(); ++i) {
2172 Font font = getFont(buf.params(), i, outerfont);
2174 // handle <emphasis> tag
2175 if (font_old.emph() != font.fontInfo().emph()) {
2176 if (font.fontInfo().emph() == FONT_ON) {
2179 } else if (i != initial) {
2180 os << "</emphasis>";
2185 if (Inset const * inset = getInset(i)) {
2186 inset->docbook(buf, os, runparams);
2188 char_type c = d->text_[i];
2190 if (style->pass_thru)
2193 os << sgml::escapeChar(c);
2195 font_old = font.fontInfo();
2199 os << "</emphasis>";
2202 if (style->free_spacing)
2204 if (style->pass_thru && !d->onlyText(buf, outerfont, initial))
2209 bool Paragraph::isHfill(pos_type pos) const
2211 Inset const * inset = getInset(pos);
2212 return inset && inset->lyxCode() == HFILL_CODE;
2216 bool Paragraph::isNewline(pos_type pos) const
2218 Inset const * inset = getInset(pos);
2219 return inset && inset->lyxCode() == NEWLINE_CODE;
2223 bool Paragraph::isLineSeparator(pos_type pos) const
2225 char_type const c = d->text_[pos];
2226 if (isLineSeparatorChar(c))
2228 Inset const * inset = getInset(pos);
2229 return inset && inset->isLineSeparator();
2233 /// Used by the spellchecker
2234 bool Paragraph::isLetter(pos_type pos) const
2236 if (Inset const * inset = getInset(pos))
2237 return inset->isLetter();
2238 char_type const c = d->text_[pos];
2239 return isLetterChar(c) || isDigit(c);
2244 Paragraph::getParLanguage(BufferParams const & bparams) const
2247 return getFirstFontSettings(bparams).language();
2248 // FIXME: we should check the prev par as well (Lgb)
2249 return bparams.language;
2253 bool Paragraph::isRTL(BufferParams const & bparams) const
2255 return lyxrc.rtl_support
2256 && getParLanguage(bparams)->rightToLeft()
2257 && ownerCode() != ERT_CODE
2258 && ownerCode() != LISTINGS_CODE;
2262 void Paragraph::changeLanguage(BufferParams const & bparams,
2263 Language const * from, Language const * to)
2265 // change language including dummy font change at the end
2266 for (pos_type i = 0; i <= size(); ++i) {
2267 Font font = getFontSettings(bparams, i);
2268 if (font.language() == from) {
2269 font.setLanguage(to);
2276 bool Paragraph::isMultiLingual(BufferParams const & bparams) const
2278 Language const * doc_language = bparams.language;
2279 FontList::const_iterator cit = d->fontlist_.begin();
2280 FontList::const_iterator end = d->fontlist_.end();
2282 for (; cit != end; ++cit)
2283 if (cit->font().language() != ignore_language &&
2284 cit->font().language() != latex_language &&
2285 cit->font().language() != doc_language)
2291 // Convert the paragraph to a string.
2292 // Used for building the table of contents
2293 docstring const Paragraph::asString(Buffer const & buffer, bool label) const
2295 return asString(buffer, 0, size(), label);
2299 docstring const Paragraph::asString(Buffer const & buffer,
2300 pos_type beg, pos_type end, bool label) const
2303 odocstringstream os;
2305 if (beg == 0 && label && !d->params_.labelString().empty())
2306 os << d->params_.labelString() << ' ';
2308 for (pos_type i = beg; i < end; ++i) {
2309 char_type const c = d->text_[i];
2312 else if (c == META_INSET)
2313 getInset(i)->textString(buffer, os);
2320 void Paragraph::setInsetOwner(Inset * inset)
2322 d->inset_owner_ = inset;
2326 int Paragraph::id() const
2332 LayoutPtr const & Paragraph::layout() const
2338 void Paragraph::layout(LayoutPtr const & new_layout)
2340 d->layout_ = new_layout;
2344 Inset * Paragraph::inInset() const
2346 return d->inset_owner_;
2350 InsetCode Paragraph::ownerCode() const
2352 return d->inset_owner_ ? d->inset_owner_->lyxCode() : NO_CODE;
2356 ParagraphParameters & Paragraph::params()
2362 ParagraphParameters const & Paragraph::params() const
2368 bool Paragraph::isFreeSpacing() const
2370 if (d->layout_->free_spacing)
2372 return d->inset_owner_ && d->inset_owner_->isFreeSpacing();
2376 bool Paragraph::allowEmpty() const
2378 if (d->layout_->keepempty)
2380 return d->inset_owner_ && d->inset_owner_->allowEmpty();
2384 char_type Paragraph::transformChar(char_type c, pos_type pos) const
2386 if (!Encodings::is_arabic(c))
2389 char_type prev_char = ' ';
2390 char_type next_char = ' ';
2392 for (pos_type i = pos - 1; i >= 0; --i) {
2393 char_type const par_char = d->text_[i];
2394 if (!Encodings::isComposeChar_arabic(par_char)) {
2395 prev_char = par_char;
2400 for (pos_type i = pos + 1, end = size(); i < end; ++i) {
2401 char_type const par_char = d->text_[i];
2402 if (!Encodings::isComposeChar_arabic(par_char)) {
2403 next_char = par_char;
2408 if (Encodings::is_arabic(next_char)) {
2409 if (Encodings::is_arabic(prev_char) &&
2410 !Encodings::is_arabic_special(prev_char))
2411 return Encodings::transformChar(c, Encodings::FORM_MEDIAL);
2413 return Encodings::transformChar(c, Encodings::FORM_INITIAL);
2415 if (Encodings::is_arabic(prev_char) &&
2416 !Encodings::is_arabic_special(prev_char))
2417 return Encodings::transformChar(c, Encodings::FORM_FINAL);
2419 return Encodings::transformChar(c, Encodings::FORM_ISOLATED);
2424 int Paragraph::checkBiblio(bool track_changes)
2427 //This is getting more and more a mess. ...We really should clean
2428 //up this bibitem issue for 1.6. See also bug 2743.
2430 // Add bibitem insets if necessary
2431 if (d->layout_->labeltype != LABEL_BIBLIO)
2434 bool hasbibitem = !d->insetlist_.empty()
2435 // Insist on it being in pos 0
2436 && d->text_[0] == META_INSET
2437 && d->insetlist_.begin()->inset->lyxCode() == BIBITEM_CODE;
2442 // remove a bibitem in pos != 0
2443 // restore it later in pos 0 if necessary
2444 // (e.g. if a user inserts contents _before_ the item)
2445 // we're assuming there's only one of these, which there
2447 int erasedInsetPosition = -1;
2448 InsetList::iterator it = d->insetlist_.begin();
2449 InsetList::iterator end = d->insetlist_.end();
2450 for (; it != end; ++it)
2451 if (it->inset->lyxCode() == BIBITEM_CODE
2453 InsetBibitem * olditem = static_cast<InsetBibitem *>(it->inset);
2454 oldkey = olditem->getParam("key");
2455 oldlabel = olditem->getParam("label");
2456 erasedInsetPosition = it->pos;
2457 eraseChar(erasedInsetPosition, track_changes);
2461 //There was an InsetBibitem at the beginning, and we didn't
2462 //have to erase one.
2463 if (hasbibitem && erasedInsetPosition < 0)
2466 //There was an InsetBibitem at the beginning and we did have to
2467 //erase one. So we give its properties to the beginning inset.
2469 InsetBibitem * inset =
2470 static_cast<InsetBibitem *>(d->insetlist_.begin()->inset);
2471 if (!oldkey.empty())
2472 inset->setParam("key", oldkey);
2473 inset->setParam("label", oldlabel);
2474 return -erasedInsetPosition;
2477 //There was no inset at the beginning, so we need to create one with
2478 //the key and label of the one we erased.
2479 InsetBibitem * inset(new InsetBibitem(InsetCommandParams(BIBITEM_CODE)));
2480 // restore values of previously deleted item in this par.
2481 if (!oldkey.empty())
2482 inset->setParam("key", oldkey);
2483 inset->setParam("label", oldlabel);
2484 insertInset(0, static_cast<Inset *>(inset),
2485 Change(track_changes ? Change::INSERTED : Change::UNCHANGED));
2491 void Paragraph::checkAuthors(AuthorList const & authorList)
2493 d->changes_.checkAuthors(authorList);
2497 bool Paragraph::isUnchanged(pos_type pos) const
2499 return lookupChange(pos).type == Change::UNCHANGED;
2503 bool Paragraph::isInserted(pos_type pos) const
2505 return lookupChange(pos).type == Change::INSERTED;
2509 bool Paragraph::isDeleted(pos_type pos) const
2511 return lookupChange(pos).type == Change::DELETED;
2515 InsetList const & Paragraph::insetList() const
2517 return d->insetlist_;
2521 Inset * Paragraph::releaseInset(pos_type pos)
2523 Inset * inset = d->insetlist_.release(pos);
2524 /// does not honour change tracking!
2525 eraseChar(pos, false);
2530 Inset * Paragraph::getInset(pos_type pos)
2532 return (pos < pos_type(d->text_.size()) && d->text_[pos] == META_INSET)
2533 ? d->insetlist_.get(pos) : 0;
2537 Inset const * Paragraph::getInset(pos_type pos) const
2539 return (pos < pos_type(d->text_.size()) && d->text_[pos] == META_INSET)
2540 ? d->insetlist_.get(pos) : 0;
2544 void Paragraph::changeCase(BufferParams const & bparams, pos_type pos,
2545 pos_type & right, TextCase action)
2547 // process sequences of modified characters; in change
2548 // tracking mode, this approach results in much better
2549 // usability than changing case on a char-by-char basis
2552 bool const trackChanges = bparams.trackChanges;
2554 bool capitalize = true;
2556 for (; pos < right; ++pos) {
2557 char_type oldChar = d->text_[pos];
2558 char_type newChar = oldChar;
2560 // ignore insets and don't play with deleted text!
2561 if (oldChar != META_INSET && !isDeleted(pos)) {
2563 case text_lowercase:
2564 newChar = lowercase(oldChar);
2566 case text_capitalization:
2568 newChar = uppercase(oldChar);
2572 case text_uppercase:
2573 newChar = uppercase(oldChar);
2578 if (!isLetter(pos) || isDeleted(pos)) {
2579 // permit capitalization again
2583 if (oldChar != newChar)
2586 if (oldChar == newChar || pos == right - 1) {
2587 if (oldChar != newChar) {
2588 // step behind the changing area
2591 int erasePos = pos - changes.size();
2592 for (size_t i = 0; i < changes.size(); i++) {
2593 insertChar(pos, changes[i],
2594 getFontSettings(bparams,
2597 if (!eraseChar(erasePos, trackChanges)) {
2600 ++right; // expand selection
2609 bool Paragraph::find(docstring const & str, bool cs, bool mw,
2610 pos_type pos, bool del) const
2612 int const strsize = str.length();
2614 pos_type const parsize = d->text_.size();
2615 for (i = 0; pos + i < parsize; ++i) {
2618 if (cs && str[i] != d->text_[pos + i])
2620 if (!cs && uppercase(str[i]) != uppercase(d->text_[pos + i]))
2622 if (!del && isDeleted(pos + i))
2629 // if necessary, check whether string matches word
2631 if (pos > 0 && isLetter(pos - 1))
2633 if (pos + strsize < parsize
2634 && isLetter(pos + strsize))
2642 char_type Paragraph::getChar(pos_type pos) const
2644 return d->text_[pos];
2648 pos_type Paragraph::size() const
2650 return d->text_.size();
2654 bool Paragraph::empty() const
2656 return d->text_.empty();
2660 bool Paragraph::isInset(pos_type pos) const
2662 return d->text_[pos] == META_INSET;
2666 bool Paragraph::isSeparator(pos_type pos) const
2668 //FIXME: Are we sure this can be the only separator?
2669 return d->text_[pos] == ' ';