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.textClass();
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.textClass().defaultLayout();
1825 // Current base font for all inherited font changes, without any
1826 // change caused by an individual character, except for the language:
1827 // It is set to the language of the first character.
1828 // As long as we are in the label, this font is the base font of the
1829 // label. Before the first body character it is set to the base font
1833 // Maybe we have to create a optional argument.
1834 pos_type body_pos = beginOfBody();
1835 unsigned int column = 0;
1838 // the optional argument is kept in curly brackets in
1839 // case it contains a ']'
1842 basefont = getLabelFont(bparams, outerfont);
1844 basefont = getLayoutFont(bparams, outerfont);
1847 // Which font is currently active?
1848 Font running_font(basefont);
1849 // Do we have an open font change?
1850 bool open_font = false;
1852 Change runningChange = Change(Change::UNCHANGED);
1854 texrow.start(id(), 0);
1856 // if the paragraph is empty, the loop will not be entered at all
1858 if (style->isCommand()) {
1863 column += d->startTeXParParams(bparams, os, texrow,
1864 runparams.moving_arg);
1867 for (pos_type i = 0; i < size(); ++i) {
1868 // First char in paragraph or after label?
1869 if (i == body_pos) {
1872 column += running_font.latexWriteEndChanges(
1873 os, bparams, runparams,
1874 basefont, basefont);
1877 basefont = getLayoutFont(bparams, outerfont);
1878 running_font = basefont;
1880 column += Changes::latexMarkChange(os, bparams,
1881 runningChange, Change(Change::UNCHANGED));
1882 runningChange = Change(Change::UNCHANGED);
1887 if (style->isCommand()) {
1893 column += d->startTeXParParams(bparams, os,
1895 runparams.moving_arg);
1898 Change const & change = runparams.inDeletedInset ? runparams.changeOfDeletedInset
1901 if (bparams.outputChanges && runningChange != change) {
1903 column += running_font.latexWriteEndChanges(
1904 os, bparams, runparams, basefont, basefont);
1907 basefont = getLayoutFont(bparams, outerfont);
1908 running_font = basefont;
1910 column += Changes::latexMarkChange(os, bparams, runningChange, change);
1911 runningChange = change;
1914 // do not output text which is marked deleted
1915 // if change tracking output is disabled
1916 if (!bparams.outputChanges && change.type == Change::DELETED) {
1922 // Fully instantiated font
1923 Font const font = getFont(bparams, i, outerfont);
1925 Font const last_font = running_font;
1927 // Do we need to close the previous font?
1929 (font != running_font ||
1930 font.language() != running_font.language()))
1932 column += running_font.latexWriteEndChanges(
1933 os, bparams, runparams, basefont,
1934 (i == body_pos-1) ? basefont : font);
1935 running_font = basefont;
1939 // close babel's font environment before opening CJK.
1940 if (!running_font.language()->babel().empty() &&
1941 font.language()->encoding()->package() == Encoding::CJK) {
1942 string end_tag = subst(lyxrc.language_command_end,
1944 running_font.language()->babel());
1945 os << from_ascii(end_tag);
1946 column += end_tag.length();
1949 // Switch file encoding if necessary (and allowed)
1950 if (!runparams.verbatim &&
1951 runparams.encoding->package() == Encoding::none &&
1952 font.language()->encoding()->package() == Encoding::none) {
1953 pair<bool, int> const enc_switch = switchEncoding(os, bparams,
1954 runparams, *(font.language()->encoding()));
1955 if (enc_switch.first) {
1956 column += enc_switch.second;
1957 runparams.encoding = font.language()->encoding();
1961 char_type const c = d->text_[i];
1963 // Do we need to change font?
1964 if ((font != running_font ||
1965 font.language() != running_font.language()) &&
1968 odocstringstream ods;
1969 column += font.latexWriteStartChanges(ods, bparams,
1970 runparams, basefont,
1972 running_font = font;
1974 docstring fontchange = ods.str();
1975 // check if the fontchange ends with a trailing blank
1976 // (like "\small " (see bug 3382)
1977 if (suffixIs(fontchange, ' ') && c == ' ')
1978 os << fontchange.substr(0, fontchange.size() - 1)
1979 << from_ascii("{}");
1985 // FIXME: integrate this case in latexSpecialChar
1986 // Do not print the separation of the optional argument
1987 // if style->pass_thru is false. This works because
1988 // latexSpecialChar ignores spaces if
1989 // style->pass_thru is false.
1990 if (i != body_pos - 1) {
1991 if (d->simpleTeXBlanks(
1992 runparams, os, texrow,
1993 i, column, font, *style)) {
1994 // A surrogate pair was output. We
1995 // must not call latexSpecialChar
1996 // in this iteration, since it would output
1997 // the combining character again.
2004 OutputParams rp = runparams;
2005 rp.free_spacing = style->free_spacing;
2006 rp.local_font = &font;
2007 rp.intitle = style->intitle;
2009 // Two major modes: LaTeX or plain
2010 // Handle here those cases common to both modes
2011 // and then split to handle the two modes separately.
2012 if (c == META_INSET)
2013 d->latexInset(buf, bparams, os,
2014 texrow, rp, running_font,
2015 basefont, outerfont, open_font,
2016 runningChange, *style, i, column);
2019 d->latexSpecialChar(os, rp, running_font, runningChange,
2021 } catch (EncodingException & e) {
2022 if (runparams.dryrun) {
2023 os << "<" << _("LyX Warning: ")
2024 << _("uncodable character") << " '";
2028 // add location information and throw again.
2036 // Set the encoding to that returned from simpleTeXSpecialChars (see
2037 // comment for encoding member in OutputParams.h)
2038 runparams.encoding = rp.encoding;
2041 // If we have an open font definition, we have to close it
2043 #ifdef FIXED_LANGUAGE_END_DETECTION
2046 .latexWriteEndChanges(os, bparams, runparams,
2048 next_->getFont(bparams, 0, outerfont));
2050 running_font.latexWriteEndChanges(os, bparams,
2051 runparams, basefont, basefont);
2054 //FIXME: For now we ALWAYS have to close the foreign font settings if they are
2055 //FIXME: there as we start another \selectlanguage with the next paragraph if
2056 //FIXME: we are in need of this. This should be fixed sometime (Jug)
2057 running_font.latexWriteEndChanges(os, bparams, runparams,
2058 basefont, basefont);
2062 column += Changes::latexMarkChange(os, bparams, runningChange, Change(Change::UNCHANGED));
2064 // Needed if there is an optional argument but no contents.
2065 if (body_pos > 0 && body_pos == size()) {
2067 return_value = false;
2071 column += d->endTeXParParams(bparams, os, texrow,
2072 runparams.moving_arg);
2075 LYXERR(Debug::LATEX, "SimpleTeXOnePar...done " << this);
2076 return return_value;
2080 bool Paragraph::emptyTag() const
2082 for (pos_type i = 0; i < size(); ++i) {
2083 if (Inset const * inset = getInset(i)) {
2084 InsetCode lyx_code = inset->lyxCode();
2085 if (lyx_code != TOC_CODE &&
2086 lyx_code != INCLUDE_CODE &&
2087 lyx_code != GRAPHICS_CODE &&
2088 lyx_code != ERT_CODE &&
2089 lyx_code != LISTINGS_CODE &&
2090 lyx_code != FLOAT_CODE &&
2091 lyx_code != TABULAR_CODE) {
2095 char_type c = d->text_[i];
2096 if (c != ' ' && c != '\t')
2104 string Paragraph::getID(Buffer const & buf, OutputParams const & runparams) const
2106 for (pos_type i = 0; i < size(); ++i) {
2107 if (Inset const * inset = getInset(i)) {
2108 InsetCode lyx_code = inset->lyxCode();
2109 if (lyx_code == LABEL_CODE) {
2110 InsetLabel const * const il = static_cast<InsetLabel const *>(inset);
2111 docstring const & id = il->getParam("name");
2112 return "id='" + to_utf8(sgml::cleanID(buf, runparams, id)) + "'";
2120 pos_type Paragraph::getFirstWord(Buffer const & buf, odocstream & os, OutputParams const & runparams) const
2123 for (i = 0; i < size(); ++i) {
2124 if (Inset const * inset = getInset(i)) {
2125 inset->docbook(buf, os, runparams);
2127 char_type c = d->text_[i];
2130 os << sgml::escapeChar(c);
2137 bool Paragraph::Private::onlyText(Buffer const & buf, Font const & outerfont, pos_type initial) const
2140 pos_type size = text_.size();
2141 for (pos_type i = initial; i < size; ++i) {
2142 Font font = owner_->getFont(buf.params(), i, outerfont);
2143 if (text_[i] == META_INSET)
2145 if (i != initial && font != font_old)
2154 void Paragraph::simpleDocBookOnePar(Buffer const & buf,
2156 OutputParams const & runparams,
2157 Font const & outerfont,
2158 pos_type initial) const
2160 bool emph_flag = false;
2162 LayoutPtr const & style = d->layout_;
2164 style->labeltype == LABEL_MANUAL ? style->labelfont : style->font;
2166 if (style->pass_thru && !d->onlyText(buf, outerfont, initial))
2169 // parsing main loop
2170 for (pos_type i = initial; i < size(); ++i) {
2171 Font font = getFont(buf.params(), i, outerfont);
2173 // handle <emphasis> tag
2174 if (font_old.emph() != font.fontInfo().emph()) {
2175 if (font.fontInfo().emph() == FONT_ON) {
2178 } else if (i != initial) {
2179 os << "</emphasis>";
2184 if (Inset const * inset = getInset(i)) {
2185 inset->docbook(buf, os, runparams);
2187 char_type c = d->text_[i];
2189 if (style->pass_thru)
2192 os << sgml::escapeChar(c);
2194 font_old = font.fontInfo();
2198 os << "</emphasis>";
2201 if (style->free_spacing)
2203 if (style->pass_thru && !d->onlyText(buf, outerfont, initial))
2208 bool Paragraph::isHfill(pos_type pos) const
2210 Inset const * inset = getInset(pos);
2211 return inset && inset->lyxCode() == HFILL_CODE;
2215 bool Paragraph::isNewline(pos_type pos) const
2217 Inset const * inset = getInset(pos);
2218 return inset && inset->lyxCode() == NEWLINE_CODE;
2222 bool Paragraph::isLineSeparator(pos_type pos) const
2224 char_type const c = d->text_[pos];
2225 if (isLineSeparatorChar(c))
2227 Inset const * inset = getInset(pos);
2228 return inset && inset->isLineSeparator();
2232 /// Used by the spellchecker
2233 bool Paragraph::isLetter(pos_type pos) const
2235 if (Inset const * inset = getInset(pos))
2236 return inset->isLetter();
2237 char_type const c = d->text_[pos];
2238 return isLetterChar(c) || isDigit(c);
2243 Paragraph::getParLanguage(BufferParams const & bparams) const
2246 return getFirstFontSettings(bparams).language();
2247 // FIXME: we should check the prev par as well (Lgb)
2248 return bparams.language;
2252 bool Paragraph::isRTL(BufferParams const & bparams) const
2254 return lyxrc.rtl_support
2255 && getParLanguage(bparams)->rightToLeft()
2256 && ownerCode() != ERT_CODE
2257 && ownerCode() != LISTINGS_CODE;
2261 void Paragraph::changeLanguage(BufferParams const & bparams,
2262 Language const * from, Language const * to)
2264 // change language including dummy font change at the end
2265 for (pos_type i = 0; i <= size(); ++i) {
2266 Font font = getFontSettings(bparams, i);
2267 if (font.language() == from) {
2268 font.setLanguage(to);
2275 bool Paragraph::isMultiLingual(BufferParams const & bparams) const
2277 Language const * doc_language = bparams.language;
2278 FontList::const_iterator cit = d->fontlist_.begin();
2279 FontList::const_iterator end = d->fontlist_.end();
2281 for (; cit != end; ++cit)
2282 if (cit->font().language() != ignore_language &&
2283 cit->font().language() != latex_language &&
2284 cit->font().language() != doc_language)
2290 // Convert the paragraph to a string.
2291 // Used for building the table of contents
2292 docstring const Paragraph::asString(Buffer const & buffer, bool label) const
2294 return asString(buffer, 0, size(), label);
2298 docstring const Paragraph::asString(Buffer const & buffer,
2299 pos_type beg, pos_type end, bool label) const
2302 odocstringstream os;
2304 if (beg == 0 && label && !d->params_.labelString().empty())
2305 os << d->params_.labelString() << ' ';
2307 for (pos_type i = beg; i < end; ++i) {
2308 char_type const c = d->text_[i];
2311 else if (c == META_INSET)
2312 getInset(i)->textString(buffer, os);
2319 void Paragraph::setInsetOwner(Inset * inset)
2321 d->inset_owner_ = inset;
2325 int Paragraph::id() const
2331 LayoutPtr const & Paragraph::layout() const
2337 void Paragraph::layout(LayoutPtr const & new_layout)
2339 d->layout_ = new_layout;
2343 Inset * Paragraph::inInset() const
2345 return d->inset_owner_;
2349 InsetCode Paragraph::ownerCode() const
2351 return d->inset_owner_ ? d->inset_owner_->lyxCode() : NO_CODE;
2355 ParagraphParameters & Paragraph::params()
2361 ParagraphParameters const & Paragraph::params() const
2367 bool Paragraph::isFreeSpacing() const
2369 if (d->layout_->free_spacing)
2371 return d->inset_owner_ && d->inset_owner_->isFreeSpacing();
2375 bool Paragraph::allowEmpty() const
2377 if (d->layout_->keepempty)
2379 return d->inset_owner_ && d->inset_owner_->allowEmpty();
2383 char_type Paragraph::transformChar(char_type c, pos_type pos) const
2385 if (!Encodings::is_arabic(c))
2388 char_type prev_char = ' ';
2389 char_type next_char = ' ';
2391 for (pos_type i = pos - 1; i >= 0; --i) {
2392 char_type const par_char = d->text_[i];
2393 if (!Encodings::isComposeChar_arabic(par_char)) {
2394 prev_char = par_char;
2399 for (pos_type i = pos + 1, end = size(); i < end; ++i) {
2400 char_type const par_char = d->text_[i];
2401 if (!Encodings::isComposeChar_arabic(par_char)) {
2402 next_char = par_char;
2407 if (Encodings::is_arabic(next_char)) {
2408 if (Encodings::is_arabic(prev_char) &&
2409 !Encodings::is_arabic_special(prev_char))
2410 return Encodings::transformChar(c, Encodings::FORM_MEDIAL);
2412 return Encodings::transformChar(c, Encodings::FORM_INITIAL);
2414 if (Encodings::is_arabic(prev_char) &&
2415 !Encodings::is_arabic_special(prev_char))
2416 return Encodings::transformChar(c, Encodings::FORM_FINAL);
2418 return Encodings::transformChar(c, Encodings::FORM_ISOLATED);
2423 int Paragraph::checkBiblio(bool track_changes)
2426 //This is getting more and more a mess. ...We really should clean
2427 //up this bibitem issue for 1.6. See also bug 2743.
2429 // Add bibitem insets if necessary
2430 if (d->layout_->labeltype != LABEL_BIBLIO)
2433 bool hasbibitem = !d->insetlist_.empty()
2434 // Insist on it being in pos 0
2435 && d->text_[0] == META_INSET
2436 && d->insetlist_.begin()->inset->lyxCode() == BIBITEM_CODE;
2441 // remove a bibitem in pos != 0
2442 // restore it later in pos 0 if necessary
2443 // (e.g. if a user inserts contents _before_ the item)
2444 // we're assuming there's only one of these, which there
2446 int erasedInsetPosition = -1;
2447 InsetList::iterator it = d->insetlist_.begin();
2448 InsetList::iterator end = d->insetlist_.end();
2449 for (; it != end; ++it)
2450 if (it->inset->lyxCode() == BIBITEM_CODE
2452 InsetBibitem * olditem = static_cast<InsetBibitem *>(it->inset);
2453 oldkey = olditem->getParam("key");
2454 oldlabel = olditem->getParam("label");
2455 erasedInsetPosition = it->pos;
2456 eraseChar(erasedInsetPosition, track_changes);
2460 //There was an InsetBibitem at the beginning, and we didn't
2461 //have to erase one.
2462 if (hasbibitem && erasedInsetPosition < 0)
2465 //There was an InsetBibitem at the beginning and we did have to
2466 //erase one. So we give its properties to the beginning inset.
2468 InsetBibitem * inset =
2469 static_cast<InsetBibitem *>(d->insetlist_.begin()->inset);
2470 if (!oldkey.empty())
2471 inset->setParam("key", oldkey);
2472 inset->setParam("label", oldlabel);
2473 return -erasedInsetPosition;
2476 //There was no inset at the beginning, so we need to create one with
2477 //the key and label of the one we erased.
2478 InsetBibitem * inset(new InsetBibitem(InsetCommandParams(BIBITEM_CODE)));
2479 // restore values of previously deleted item in this par.
2480 if (!oldkey.empty())
2481 inset->setParam("key", oldkey);
2482 inset->setParam("label", oldlabel);
2483 insertInset(0, static_cast<Inset *>(inset),
2484 Change(track_changes ? Change::INSERTED : Change::UNCHANGED));
2490 void Paragraph::checkAuthors(AuthorList const & authorList)
2492 d->changes_.checkAuthors(authorList);
2496 bool Paragraph::isUnchanged(pos_type pos) const
2498 return lookupChange(pos).type == Change::UNCHANGED;
2502 bool Paragraph::isInserted(pos_type pos) const
2504 return lookupChange(pos).type == Change::INSERTED;
2508 bool Paragraph::isDeleted(pos_type pos) const
2510 return lookupChange(pos).type == Change::DELETED;
2514 InsetList const & Paragraph::insetList() const
2516 return d->insetlist_;
2520 Inset * Paragraph::releaseInset(pos_type pos)
2522 Inset * inset = d->insetlist_.release(pos);
2523 /// does not honour change tracking!
2524 eraseChar(pos, false);
2529 Inset * Paragraph::getInset(pos_type pos)
2531 return (pos < pos_type(d->text_.size()) && d->text_[pos] == META_INSET)
2532 ? d->insetlist_.get(pos) : 0;
2536 Inset const * Paragraph::getInset(pos_type pos) const
2538 return (pos < pos_type(d->text_.size()) && d->text_[pos] == META_INSET)
2539 ? d->insetlist_.get(pos) : 0;
2543 void Paragraph::changeCase(BufferParams const & bparams, pos_type pos,
2544 pos_type & right, TextCase action)
2546 // process sequences of modified characters; in change
2547 // tracking mode, this approach results in much better
2548 // usability than changing case on a char-by-char basis
2551 bool const trackChanges = bparams.trackChanges;
2553 bool capitalize = true;
2555 for (; pos < right; ++pos) {
2556 char_type oldChar = d->text_[pos];
2557 char_type newChar = oldChar;
2559 // ignore insets and don't play with deleted text!
2560 if (oldChar != META_INSET && !isDeleted(pos)) {
2562 case text_lowercase:
2563 newChar = lowercase(oldChar);
2565 case text_capitalization:
2567 newChar = uppercase(oldChar);
2571 case text_uppercase:
2572 newChar = uppercase(oldChar);
2577 if (!isLetter(pos) || isDeleted(pos)) {
2578 // permit capitalization again
2582 if (oldChar != newChar)
2585 if (oldChar == newChar || pos == right - 1) {
2586 if (oldChar != newChar) {
2587 // step behind the changing area
2590 int erasePos = pos - changes.size();
2591 for (size_t i = 0; i < changes.size(); i++) {
2592 insertChar(pos, changes[i],
2593 getFontSettings(bparams,
2596 if (!eraseChar(erasePos, trackChanges)) {
2599 ++right; // expand selection
2608 bool Paragraph::find(docstring const & str, bool cs, bool mw,
2609 pos_type pos, bool del) const
2611 int const strsize = str.length();
2613 pos_type const parsize = d->text_.size();
2614 for (i = 0; pos + i < parsize; ++i) {
2617 if (cs && str[i] != d->text_[pos + i])
2619 if (!cs && uppercase(str[i]) != uppercase(d->text_[pos + i]))
2621 if (!del && isDeleted(pos + i))
2628 // if necessary, check whether string matches word
2630 if (pos > 0 && isLetter(pos - 1))
2632 if (pos + strsize < parsize
2633 && isLetter(pos + strsize))
2641 char_type Paragraph::getChar(pos_type pos) const
2643 return d->text_[pos];
2647 pos_type Paragraph::size() const
2649 return d->text_.size();
2653 bool Paragraph::empty() const
2655 return d->text_.empty();
2659 bool Paragraph::isInset(pos_type pos) const
2661 return d->text_[pos] == META_INSET;
2665 bool Paragraph::isSeparator(pos_type pos) const
2667 //FIXME: Are we sure this can be the only separator?
2668 return d->text_[pos] == ' ';