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 Richard Heck (XHTML output)
9 * \author Jean-Marc Lasgouttes
10 * \author Angus Leeming
12 * \author André Pönitz
14 * \author Jürgen Vigna
16 * Full author contact details are available in file CREDITS.
21 #include "Paragraph.h"
23 #include "LayoutFile.h"
25 #include "BufferParams.h"
29 #include "InsetList.h"
31 #include "LaTeXFeatures.h"
37 #include "OutputParams.h"
38 #include "output_latex.h"
39 #include "output_xhtml.h"
40 #include "ParagraphParameters.h"
41 #include "SpellChecker.h"
43 #include "TextClass.h"
47 #include "WordLangTuple.h"
50 #include "frontends/alert.h"
52 #include "insets/InsetBibitem.h"
53 #include "insets/InsetLabel.h"
55 #include "support/debug.h"
56 #include "support/docstring_list.h"
57 #include "support/ExceptionMessage.h"
58 #include "support/gettext.h"
59 #include "support/lassert.h"
60 #include "support/lstrings.h"
61 #include "support/textutils.h"
67 using namespace lyx::support;
72 /// Inset identifier (above 0x10ffff, for ucs-4)
73 char_type const META_INSET = 0x200001;
76 /////////////////////////////////////////////////////////////////////
80 /////////////////////////////////////////////////////////////////////
82 class Paragraph::Private
86 Private(Paragraph * owner, Layout const & layout);
87 /// "Copy constructor"
88 Private(Private const &, Paragraph * owner);
89 /// Copy constructor from \p beg to \p end
90 Private(Private const &, Paragraph * owner, pos_type beg, pos_type end);
93 void insertChar(pos_type pos, char_type c, Change const & change);
95 /// Output the surrogate pair formed by \p c and \p next to \p os.
96 /// \return the number of characters written.
97 int latexSurrogatePair(odocstream & os, char_type c, char_type next,
98 OutputParams const &);
100 /// Output a space in appropriate formatting (or a surrogate pair
101 /// if the next character is a combining character).
102 /// \return whether a surrogate pair was output.
103 bool simpleTeXBlanks(OutputParams const &,
104 odocstream &, TexRow & texrow,
106 unsigned int & column,
108 Layout const & style);
110 /// Output consecutive unicode chars, belonging to the same script as
111 /// specified by the latex macro \p ltx, to \p os starting from \p i.
112 /// \return the number of characters written.
113 int writeScriptChars(odocstream & os, docstring const & ltx,
114 Change const &, Encoding const &, pos_type & i);
116 /// This could go to ParagraphParameters if we want to.
117 int startTeXParParams(BufferParams const &, odocstream &, TexRow &,
118 OutputParams const &) const;
120 /// This could go to ParagraphParameters if we want to.
121 int endTeXParParams(BufferParams const &, odocstream &, TexRow &,
122 OutputParams const &) const;
125 void latexInset(BufferParams const &,
127 TexRow & texrow, OutputParams &,
130 Font const & outerfont,
132 Change & running_change,
133 Layout const & style,
135 unsigned int & column);
138 void latexSpecialChar(
140 OutputParams const & runparams,
141 Font const & running_font,
142 Change const & running_change,
143 Layout const & style,
145 unsigned int & column);
152 unsigned int & column);
154 bool latexSpecialTypewriter(
158 unsigned int & column);
160 bool latexSpecialPhrase(
163 unsigned int & column,
164 OutputParams const & runparams);
167 void validate(LaTeXFeatures & features) const;
169 /// Checks if the paragraph contains only text and no inset or font change.
170 bool onlyText(Buffer const & buf, Font const & outerfont,
171 pos_type initial) const;
173 /// match a string against a particular point in the paragraph
174 bool isTextAt(string const & str, pos_type pos) const;
176 void setMisspelled(pos_type from, pos_type to, bool misspelled)
178 pos_type textsize = owner_->size();
179 // check for sane arguments
180 if (to < from || from >= textsize) return;
181 // don't mark end of paragraph
184 fontlist_.setMisspelled(from, to, misspelled);
188 InsetCode ownerCode() const
190 return inset_owner_ ? inset_owner_->lyxCode() : NO_CODE;
193 /// Which Paragraph owns us?
197 Inset const * inset_owner_;
206 ParagraphParameters params_;
208 /// for recording and looking up changes
212 InsetList insetlist_;
215 pos_type begin_of_body_;
217 typedef docstring TextContainer;
221 typedef set<docstring> Words;
222 typedef map<Language, Words> LangWordsMap;
226 Layout const * layout_;
232 struct special_phrase {
238 special_phrase const special_phrases[] = {
239 { "LyX", from_ascii("\\LyX{}"), false },
240 { "TeX", from_ascii("\\TeX{}"), true },
241 { "LaTeX2e", from_ascii("\\LaTeXe{}"), true },
242 { "LaTeX", from_ascii("\\LaTeX{}"), true },
245 size_t const phrases_nr = sizeof(special_phrases)/sizeof(special_phrase);
250 Paragraph::Private::Private(Paragraph * owner, Layout const & layout)
251 : owner_(owner), inset_owner_(0), id_(-1), begin_of_body_(0), layout_(&layout)
257 // Initialization of the counter for the paragraph id's,
259 // FIXME: There should be a more intelligent way to generate and use the
260 // paragraph ids per buffer instead a global static counter for all InsetText
261 // in the running program.
262 static int paragraph_id = -1;
264 Paragraph::Private::Private(Private const & p, Paragraph * owner)
265 : owner_(owner), inset_owner_(p.inset_owner_), fontlist_(p.fontlist_),
266 params_(p.params_), changes_(p.changes_), insetlist_(p.insetlist_),
267 begin_of_body_(p.begin_of_body_), text_(p.text_), words_(p.words_),
270 id_ = ++paragraph_id;
274 Paragraph::Private::Private(Private const & p, Paragraph * owner,
275 pos_type beg, pos_type end)
276 : owner_(owner), inset_owner_(p.inset_owner_),
277 params_(p.params_), changes_(p.changes_),
278 insetlist_(p.insetlist_, beg, end),
279 begin_of_body_(p.begin_of_body_), words_(p.words_),
282 id_ = ++paragraph_id;
283 if (beg >= pos_type(p.text_.size()))
285 text_ = p.text_.substr(beg, end - beg);
287 FontList::const_iterator fcit = fontlist_.begin();
288 FontList::const_iterator fend = fontlist_.end();
289 for (; fcit != fend; ++fcit) {
290 if (fcit->pos() < beg)
292 if (fcit->pos() >= end) {
293 // Add last entry in the fontlist_.
294 fontlist_.set(text_.size() - 1, fcit->font());
297 // Add a new entry in the fontlist_.
298 fontlist_.set(fcit->pos() - beg, fcit->font());
303 void Paragraph::addChangesToToc(DocIterator const & cdit,
304 Buffer const & buf) const
306 d->changes_.addToToc(cdit, buf);
310 bool Paragraph::isDeleted(pos_type start, pos_type end) const
312 LASSERT(start >= 0 && start <= size(), /**/);
313 LASSERT(end > start && end <= size() + 1, /**/);
315 return d->changes_.isDeleted(start, end);
319 bool Paragraph::isChanged(pos_type start, pos_type end) const
321 LASSERT(start >= 0 && start <= size(), /**/);
322 LASSERT(end > start && end <= size() + 1, /**/);
324 return d->changes_.isChanged(start, end);
328 bool Paragraph::isMergedOnEndOfParDeletion(bool trackChanges) const
330 // keep the logic here in sync with the logic of eraseChars()
334 Change const change = d->changes_.lookup(size());
335 return change.inserted() && change.currentAuthor();
339 void Paragraph::setChange(Change const & change)
341 // beware of the imaginary end-of-par character!
342 d->changes_.set(change, 0, size() + 1);
345 * Propagate the change recursively - but not in case of DELETED!
347 * Imagine that your co-author makes changes in an existing inset. He
348 * sends your document to you and you come to the conclusion that the
349 * inset should go completely. If you erase it, LyX must not delete all
350 * text within the inset. Otherwise, the change tracked insertions of
351 * your co-author get lost and there is no way to restore them later.
353 * Conclusion: An inset's content should remain untouched if you delete it
356 if (!change.deleted()) {
357 for (pos_type pos = 0; pos < size(); ++pos) {
358 if (Inset * inset = getInset(pos))
359 inset->setChange(change);
365 void Paragraph::setChange(pos_type pos, Change const & change)
367 LASSERT(pos >= 0 && pos <= size(), /**/);
368 d->changes_.set(change, pos);
370 // see comment in setChange(Change const &) above
371 if (!change.deleted() && pos < size())
372 if (Inset * inset = getInset(pos))
373 inset->setChange(change);
377 Change const & Paragraph::lookupChange(pos_type pos) const
379 LASSERT(pos >= 0 && pos <= size(), /**/);
380 return d->changes_.lookup(pos);
384 void Paragraph::acceptChanges(pos_type start, pos_type end)
386 LASSERT(start >= 0 && start <= size(), /**/);
387 LASSERT(end > start && end <= size() + 1, /**/);
389 for (pos_type pos = start; pos < end; ++pos) {
390 switch (lookupChange(pos).type) {
391 case Change::UNCHANGED:
392 // accept changes in nested inset
393 if (Inset * inset = getInset(pos))
394 inset->acceptChanges();
397 case Change::INSERTED:
398 d->changes_.set(Change(Change::UNCHANGED), pos);
399 // also accept changes in nested inset
400 if (Inset * inset = getInset(pos))
401 inset->acceptChanges();
404 case Change::DELETED:
405 // Suppress access to non-existent
406 // "end-of-paragraph char"
408 eraseChar(pos, false);
419 void Paragraph::rejectChanges(pos_type start, pos_type end)
421 LASSERT(start >= 0 && start <= size(), /**/);
422 LASSERT(end > start && end <= size() + 1, /**/);
424 for (pos_type pos = start; pos < end; ++pos) {
425 switch (lookupChange(pos).type) {
426 case Change::UNCHANGED:
427 // reject changes in nested inset
428 if (Inset * inset = getInset(pos))
429 inset->rejectChanges();
432 case Change::INSERTED:
433 // Suppress access to non-existent
434 // "end-of-paragraph char"
436 eraseChar(pos, false);
442 case Change::DELETED:
443 d->changes_.set(Change(Change::UNCHANGED), pos);
445 // Do NOT reject changes within a deleted inset!
446 // There may be insertions of a co-author inside of it!
454 void Paragraph::Private::insertChar(pos_type pos, char_type c,
455 Change const & change)
457 LASSERT(pos >= 0 && pos <= int(text_.size()), /**/);
460 changes_.insert(change, pos);
462 // This is actually very common when parsing buffers (and
463 // maybe inserting ascii text)
464 if (pos == pos_type(text_.size())) {
465 // when appending characters, no need to update tables
470 text_.insert(text_.begin() + pos, c);
472 // Update the font table.
473 fontlist_.increasePosAfterPos(pos);
476 insetlist_.increasePosAfterPos(pos);
480 bool Paragraph::insertInset(pos_type pos, Inset * inset,
481 Change const & change)
483 LASSERT(inset, /**/);
484 LASSERT(pos >= 0 && pos <= size(), /**/);
486 // Paragraph::insertInset() can be used in cut/copy/paste operation where
487 // d->inset_owner_ is not set yet.
488 if (d->inset_owner_ && !d->inset_owner_->insetAllowed(inset->lyxCode()))
491 d->insertChar(pos, META_INSET, change);
492 LASSERT(d->text_[pos] == META_INSET, /**/);
494 // Add a new entry in the insetlist_.
495 d->insetlist_.insert(inset, pos);
500 bool Paragraph::eraseChar(pos_type pos, bool trackChanges)
502 LASSERT(pos >= 0 && pos <= size(), return false);
504 // keep the logic here in sync with the logic of isMergedOnEndOfParDeletion()
507 Change change = d->changes_.lookup(pos);
509 // set the character to DELETED if
510 // a) it was previously unchanged or
511 // b) it was inserted by a co-author
513 if (!change.changed() ||
514 (change.inserted() && !change.currentAuthor())) {
515 setChange(pos, Change(Change::DELETED));
519 if (change.deleted())
523 // Don't physically access the imaginary end-of-paragraph character.
524 // eraseChar() can only mark it as DELETED. A physical deletion of
525 // end-of-par must be handled externally.
531 d->changes_.erase(pos);
533 // if it is an inset, delete the inset entry
534 if (d->text_[pos] == META_INSET)
535 d->insetlist_.erase(pos);
537 d->text_.erase(d->text_.begin() + pos);
539 // Update the fontlist_
540 d->fontlist_.erase(pos);
542 // Update the insetlist_
543 d->insetlist_.decreasePosAfterPos(pos);
549 int Paragraph::eraseChars(pos_type start, pos_type end, bool trackChanges)
551 LASSERT(start >= 0 && start <= size(), /**/);
552 LASSERT(end >= start && end <= size() + 1, /**/);
555 for (pos_type count = end - start; count; --count) {
556 if (!eraseChar(i, trackChanges))
563 int Paragraph::Private::latexSurrogatePair(odocstream & os, char_type c,
564 char_type next, OutputParams const & runparams)
566 // Writing next here may circumvent a possible font change between
567 // c and next. Since next is only output if it forms a surrogate pair
568 // with c we can ignore this:
569 // A font change inside a surrogate pair does not make sense and is
570 // hopefully impossible to input.
571 // FIXME: change tracking
572 // Is this correct WRT change tracking?
573 Encoding const & encoding = *(runparams.encoding);
574 docstring const latex1 = encoding.latexChar(next);
575 docstring const latex2 = encoding.latexChar(c);
576 if (docstring(1, next) == latex1) {
577 // the encoding supports the combination
578 os << latex2 << latex1;
579 return latex1.length() + latex2.length();
580 } else if (runparams.local_font &&
581 runparams.local_font->language()->lang() == "polutonikogreek") {
582 // polutonikogreek only works without the brackets
583 os << latex1 << latex2;
584 return latex1.length() + latex2.length();
586 os << latex1 << '{' << latex2 << '}';
587 return latex1.length() + latex2.length() + 2;
591 bool Paragraph::Private::simpleTeXBlanks(OutputParams const & runparams,
592 odocstream & os, TexRow & texrow,
594 unsigned int & column,
596 Layout const & style)
598 if (style.pass_thru || runparams.pass_thru)
601 if (i + 1 < int(text_.size())) {
602 char_type next = text_[i + 1];
603 if (Encodings::isCombiningChar(next)) {
604 // This space has an accent, so we must always output it.
605 column += latexSurrogatePair(os, ' ', next, runparams) - 1;
610 if (runparams.linelen > 0
611 && column > runparams.linelen
613 && text_[i - 1] != ' '
614 && (i + 1 < int(text_.size()))
615 // same in FreeSpacing mode
616 && !owner_->isFreeSpacing()
617 // In typewriter mode, we want to avoid
618 // ! . ? : at the end of a line
619 && !(font.fontInfo().family() == TYPEWRITER_FAMILY
620 && (text_[i - 1] == '.'
621 || text_[i - 1] == '?'
622 || text_[i - 1] == ':'
623 || text_[i - 1] == '!'))) {
626 texrow.start(owner_->id(), i + 1);
628 } else if (style.free_spacing) {
637 int Paragraph::Private::writeScriptChars(odocstream & os,
638 docstring const & ltx,
639 Change const & runningChange,
640 Encoding const & encoding,
643 // FIXME: modifying i here is not very nice...
645 // We only arrive here when a proper language for character text_[i] has
646 // not been specified (i.e., it could not be translated in the current
647 // latex encoding) or its latex translation has been forced, and it
648 // belongs to a known script.
649 // Parameter ltx contains the latex translation of text_[i] as specified
650 // in the unicodesymbols file and is something like "\textXXX{<spec>}".
651 // The latex macro name "textXXX" specifies the script to which text_[i]
652 // belongs and we use it in order to check whether characters from the
653 // same script immediately follow, such that we can collect them in a
654 // single "\textXXX" macro. So, we have to retain "\textXXX{<spec>"
655 // for the first char but only "<spec>" for all subsequent chars.
656 docstring::size_type const brace1 = ltx.find_first_of(from_ascii("{"));
657 docstring::size_type const brace2 = ltx.find_last_of(from_ascii("}"));
658 string script = to_ascii(ltx.substr(1, brace1 - 1));
661 bool closing_brace = true;
662 if (script == "textgreek" && encoding.latexName() == "iso-8859-7") {
663 // Correct encoding is being used, so we can avoid \textgreek.
666 closing_brace = false;
668 os << ltx.substr(pos, length);
669 int size = text_.size();
670 while (i + 1 < size) {
671 char_type const next = text_[i + 1];
672 // Stop here if next character belongs to another script
673 // or there is a change in change tracking status.
674 if (!Encodings::isKnownScriptChar(next, script) ||
675 runningChange != owner_->lookupChange(i + 1))
679 FontList::const_iterator cit = fontlist_.begin();
680 FontList::const_iterator end = fontlist_.end();
681 for (; cit != end; ++cit) {
682 if (cit->pos() >= i && !found) {
683 prev_font = cit->font();
686 if (cit->pos() >= i + 1)
689 // Stop here if there is a font attribute or encoding change.
690 if (found && cit != end && prev_font != cit->font())
692 docstring const latex = encoding.latexChar(next);
693 docstring::size_type const b1 =
694 latex.find_first_of(from_ascii("{"));
695 docstring::size_type const b2 =
696 latex.find_last_of(from_ascii("}"));
697 int const len = b2 - b1 - 1;
698 os << latex.substr(b1 + 1, len);
710 bool Paragraph::Private::isTextAt(string const & str, pos_type pos) const
712 pos_type const len = str.length();
714 // is the paragraph large enough?
715 if (pos + len > int(text_.size()))
718 // does the wanted text start at point?
719 for (string::size_type i = 0; i < str.length(); ++i) {
720 // Caution: direct comparison of characters works only
721 // because str is pure ASCII.
722 if (str[i] != text_[pos + i])
726 return fontlist_.hasChangeInRange(pos, len);
730 void Paragraph::Private::latexInset(BufferParams const & bparams,
733 OutputParams & runparams,
736 Font const & outerfont,
738 Change & running_change,
739 Layout const & style,
741 unsigned int & column)
743 Inset * inset = owner_->getInset(i);
744 LASSERT(inset, /**/);
746 if (style.pass_thru) {
747 inset->plaintext(os, runparams);
751 // FIXME: move this to InsetNewline::latex
752 if (inset->lyxCode() == NEWLINE_CODE) {
753 // newlines are handled differently here than
754 // the default in simpleTeXSpecialChars().
755 if (!style.newline_allowed) {
759 column += running_font.latexWriteEndChanges(
760 os, bparams, runparams,
765 if (running_font.fontInfo().family() == TYPEWRITER_FAMILY)
768 basefont = owner_->getLayoutFont(bparams, outerfont);
769 running_font = basefont;
771 if (runparams.moving_arg)
776 texrow.start(owner_->id(), i + 1);
780 if (owner_->isDeleted(i)) {
781 if( ++runparams.inDeletedInset == 1)
782 runparams.changeOfDeletedInset = owner_->lookupChange(i);
785 if (inset->canTrackChanges()) {
786 column += Changes::latexMarkChange(os, bparams, running_change,
787 Change(Change::UNCHANGED), runparams);
788 running_change = Change(Change::UNCHANGED);
792 odocstream::pos_type const len = os.tellp();
794 if (inset->forceLTR()
795 && running_font.isRightToLeft()
796 // ERT is an exception, it should be output with no
797 // decorations at all
798 && inset->lyxCode() != ERT_CODE) {
799 if (running_font.language()->lang() == "farsi")
806 // FIXME: Bug: we can have an empty font change here!
807 // if there has just been a font change, we are going to close it
808 // right now, which means stupid latex code like \textsf{}. AFAIK,
809 // this does not harm dvi output. A minor bug, thus (JMarc)
811 // Some insets cannot be inside a font change command.
812 // However, even such insets *can* be placed in \L or \R
813 // or their equivalents (for RTL language switches), so we don't
814 // close the language in those cases.
815 // ArabTeX, though, cannot handle this special behavior, it seems.
816 bool arabtex = basefont.language()->lang() == "arabic_arabtex"
817 || running_font.language()->lang() == "arabic_arabtex";
818 if (open_font && inset->noFontChange()) {
819 bool closeLanguage = arabtex
820 || basefont.isRightToLeft() == running_font.isRightToLeft();
821 unsigned int count = running_font.latexWriteEndChanges(os,
822 bparams, runparams, basefont, basefont, closeLanguage);
824 // if any font properties were closed, update the running_font,
825 // making sure, however, to leave the language as it was
827 // FIXME: probably a better way to keep track of the old
828 // language, than copying the entire font?
829 Font const copy_font(running_font);
830 basefont = owner_->getLayoutFont(bparams, outerfont);
831 running_font = basefont;
833 running_font.setLanguage(copy_font.language());
834 // leave font open if language is still open
835 open_font = (running_font.language() == basefont.language());
837 runparams.local_font = &basefont;
844 tmp = inset->latex(os, runparams);
845 } catch (EncodingException & e) {
846 // add location information and throw again.
853 if (running_font.language()->lang() == "farsi")
860 texrow.newlines(tmp);
861 texrow.start(owner_->id(), i + 1);
864 column += os.tellp() - len;
867 if (owner_->isDeleted(i))
868 --runparams.inDeletedInset;
872 void Paragraph::Private::latexSpecialChar(
874 OutputParams const & runparams,
875 Font const & running_font,
876 Change const & running_change,
877 Layout const & style,
879 unsigned int & column)
881 char_type const c = text_[i];
883 if (style.pass_thru || runparams.pass_thru) {
885 // FIXME UNICODE: This can fail if c cannot
886 // be encoded in the current encoding.
891 // If T1 font encoding is used, use the special
892 // characters it provides.
893 // NOTE: some languages reset the font encoding
895 if (!running_font.language()->internalFontEncoding()
896 && lyxrc.fontenc == "T1" && latexSpecialT1(c, os, i, column))
899 // \tt font needs special treatment
900 if (running_font.fontInfo().family() == TYPEWRITER_FAMILY
901 && latexSpecialTypewriter(c, os, i, column))
904 // Otherwise, we use what LaTeX provides us.
907 os << "\\textbackslash{}";
911 os << "\\textless{}";
915 os << "\\textgreater{}";
926 os << "\\char`\\\"{}";
931 case '%': case '#': case '{':
939 os << "\\textasciitilde{}";
944 os << "\\textasciicircum{}";
951 // avoid being mistaken for optional arguments
959 // Blanks are printed before font switching.
960 // Sure? I am not! (try nice-latex)
961 // I am sure it's correct. LyX might be smarter
962 // in the future, but for now, nothing wrong is
968 if (latexSpecialPhrase(os, i, column, runparams))
974 Encoding const & encoding = *(runparams.encoding);
975 if (i + 1 < int(text_.size())) {
976 char_type next = text_[i + 1];
977 if (Encodings::isCombiningChar(next)) {
978 column += latexSurrogatePair(os, c, next, runparams) - 1;
984 docstring const latex = encoding.latexChar(c);
985 if (Encodings::isKnownScriptChar(c, script)
986 && prefixIs(latex, from_ascii("\\" + script)))
987 column += writeScriptChars(os, latex,
988 running_change, encoding, i) - 1;
989 else if (latex.length() > 1 && latex[latex.length() - 1] != '}') {
990 // Prevent eating of a following
991 // space or command corruption by
992 // following characters
993 column += latex.length() + 1;
996 column += latex.length() - 1;
1004 bool Paragraph::Private::latexSpecialT1(char_type const c, odocstream & os,
1005 pos_type i, unsigned int & column)
1011 // In T1 encoding, these characters exist
1012 // but we should avoid ligatures
1013 if (i + 1 >= int(text_.size()) || text_[i + 1] != c)
1015 os << "\\textcompwordmark{}";
1022 // soul.sty breaks with \char`\"
1023 os << "\\textquotedbl{}";
1032 bool Paragraph::Private::latexSpecialTypewriter(char_type const c, odocstream & os,
1033 pos_type i, unsigned int & column)
1037 // within \ttfamily, "--" is merged to "-" (no endash)
1038 // so we avoid this rather irritating ligature
1039 if (i + 1 < int(text_.size()) && text_[i + 1] == '-') {
1046 // everything else has to be checked separately
1047 // (depending on the encoding)
1054 bool Paragraph::Private::latexSpecialPhrase(odocstream & os, pos_type & i,
1055 unsigned int & column, OutputParams const & runparams)
1057 // FIXME: if we have "LaTeX" with a font
1058 // change in the middle (before the 'T', then
1059 // the "TeX" part is still special cased.
1060 // Really we should only operate this on
1061 // "words" for some definition of word
1063 for (size_t pnr = 0; pnr < phrases_nr; ++pnr) {
1064 if (!isTextAt(special_phrases[pnr].phrase, i))
1066 if (runparams.moving_arg)
1068 os << special_phrases[pnr].macro;
1069 i += special_phrases[pnr].phrase.length() - 1;
1070 column += special_phrases[pnr].macro.length() - 1;
1077 void Paragraph::Private::validate(LaTeXFeatures & features) const
1079 if (layout_->inpreamble && inset_owner_) {
1080 Buffer const & buf = inset_owner_->buffer();
1081 BufferParams const & bp = buf.params();
1084 odocstringstream ods;
1085 // we have to provide all the optional arguments here, even though
1086 // the last one is the only one we care about.
1087 owner_->latex(bp, f, ods, tr, features.runparams(), 0, -1, true);
1088 docstring const d = ods.str();
1090 // this will have "{" at the beginning, but not at the end
1091 string const content = to_utf8(d);
1092 string const cmd = layout_->latexname();
1093 features.addPreambleSnippet("\\" + cmd + content + "}");
1097 if (features.runparams().flavor == OutputParams::HTML
1098 && layout_->htmltitle()) {
1099 features.setHTMLTitle(owner_->asString(AS_STR_INSETS));
1102 // check the params.
1103 if (!params_.spacing().isDefault())
1104 features.require("setspace");
1107 features.useLayout(layout_->name());
1110 fontlist_.validate(features);
1112 // then the indentation
1113 if (!params_.leftIndent().zero())
1114 features.require("ParagraphLeftIndent");
1117 InsetList::const_iterator icit = insetlist_.begin();
1118 InsetList::const_iterator iend = insetlist_.end();
1119 for (; icit != iend; ++icit) {
1121 icit->inset->validate(features);
1122 if (layout_->needprotect &&
1123 icit->inset->lyxCode() == FOOT_CODE)
1124 features.require("NeedLyXFootnoteCode");
1128 // then the contents
1129 for (pos_type i = 0; i < int(text_.size()) ; ++i) {
1130 for (size_t pnr = 0; pnr < phrases_nr; ++pnr) {
1131 if (!special_phrases[pnr].builtin
1132 && isTextAt(special_phrases[pnr].phrase, i)) {
1133 features.require(special_phrases[pnr].phrase);
1137 Encodings::validate(text_[i], features);
1141 /////////////////////////////////////////////////////////////////////
1145 /////////////////////////////////////////////////////////////////////
1148 Layout const emptyParagraphLayout;
1151 Paragraph::Paragraph()
1152 : d(new Paragraph::Private(this, emptyParagraphLayout))
1159 Paragraph::Paragraph(Paragraph const & par)
1160 : itemdepth(par.itemdepth),
1161 d(new Paragraph::Private(*par.d, this))
1167 Paragraph::Paragraph(Paragraph const & par, pos_type beg, pos_type end)
1168 : itemdepth(par.itemdepth),
1169 d(new Paragraph::Private(*par.d, this, beg, end))
1175 Paragraph & Paragraph::operator=(Paragraph const & par)
1177 // needed as we will destroy the private part before copying it
1179 itemdepth = par.itemdepth;
1183 d = new Private(*par.d, this);
1190 Paragraph::~Paragraph()
1199 // this shall be called just before every "os << ..." action.
1200 void flushString(ostream & os, docstring & s)
1209 void Paragraph::write(ostream & os, BufferParams const & bparams,
1210 depth_type & dth) const
1212 // The beginning or end of a deeper (i.e. nested) area?
1213 if (dth != d->params_.depth()) {
1214 if (d->params_.depth() > dth) {
1215 while (d->params_.depth() > dth) {
1216 os << "\n\\begin_deeper";
1220 while (d->params_.depth() < dth) {
1221 os << "\n\\end_deeper";
1227 // First write the layout
1228 os << "\n\\begin_layout " << to_utf8(d->layout_->name()) << '\n';
1230 d->params_.write(os);
1232 Font font1(inherit_font, bparams.language);
1234 Change running_change = Change(Change::UNCHANGED);
1236 // this string is used as a buffer to avoid repetitive calls
1237 // to to_utf8(), which turn out to be expensive (JMarc)
1238 docstring write_buffer;
1241 for (pos_type i = 0; i <= size(); ++i) {
1243 Change const change = lookupChange(i);
1244 if (change != running_change)
1245 flushString(os, write_buffer);
1246 Changes::lyxMarkChange(os, bparams, column, running_change, change);
1247 running_change = change;
1252 // Write font changes (ignore spelling markers)
1253 Font font2 = getFontSettings(bparams, i);
1254 font2.setMisspelled(false);
1255 if (font2 != font1) {
1256 flushString(os, write_buffer);
1257 font2.lyxWriteChanges(font1, os);
1262 char_type const c = d->text_[i];
1265 if (Inset const * inset = getInset(i)) {
1266 flushString(os, write_buffer);
1267 if (inset->directWrite()) {
1268 // international char, let it write
1269 // code directly so it's shorter in
1275 os << "\\begin_inset ";
1277 os << "\n\\end_inset\n\n";
1283 flushString(os, write_buffer);
1284 os << "\n\\backslash\n";
1288 flushString(os, write_buffer);
1289 if (i + 1 < size() && d->text_[i + 1] == ' ') {
1296 if ((column > 70 && c == ' ')
1298 flushString(os, write_buffer);
1302 // this check is to amend a bug. LyX sometimes
1303 // inserts '\0' this could cause problems.
1305 write_buffer.push_back(c);
1307 LYXERR0("NUL char in structure.");
1313 flushString(os, write_buffer);
1314 os << "\n\\end_layout\n";
1318 void Paragraph::validate(LaTeXFeatures & features) const
1320 d->validate(features);
1324 void Paragraph::insert(pos_type start, docstring const & str,
1325 Font const & font, Change const & change)
1327 for (size_t i = 0, n = str.size(); i != n ; ++i)
1328 insertChar(start + i, str[i], font, change);
1332 void Paragraph::appendChar(char_type c, Font const & font,
1333 Change const & change)
1336 d->changes_.insert(change, d->text_.size());
1337 // when appending characters, no need to update tables
1338 d->text_.push_back(c);
1339 setFont(d->text_.size() - 1, font);
1343 void Paragraph::appendString(docstring const & s, Font const & font,
1344 Change const & change)
1346 pos_type end = s.size();
1347 size_t oldsize = d->text_.size();
1348 size_t newsize = oldsize + end;
1349 size_t capacity = d->text_.capacity();
1350 if (newsize >= capacity)
1351 d->text_.reserve(max(capacity + 100, newsize));
1353 // when appending characters, no need to update tables
1356 // FIXME: Optimize this!
1357 for (size_t i = oldsize; i != newsize; ++i) {
1359 d->changes_.insert(change, i);
1361 d->fontlist_.set(oldsize, font);
1362 d->fontlist_.set(newsize - 1, font);
1366 void Paragraph::insertChar(pos_type pos, char_type c,
1369 d->insertChar(pos, c, Change(trackChanges ?
1370 Change::INSERTED : Change::UNCHANGED));
1374 void Paragraph::insertChar(pos_type pos, char_type c,
1375 Font const & font, bool trackChanges)
1377 d->insertChar(pos, c, Change(trackChanges ?
1378 Change::INSERTED : Change::UNCHANGED));
1383 void Paragraph::insertChar(pos_type pos, char_type c,
1384 Font const & font, Change const & change)
1386 d->insertChar(pos, c, change);
1391 bool Paragraph::insertInset(pos_type pos, Inset * inset,
1392 Font const & font, Change const & change)
1394 bool const success = insertInset(pos, inset, change);
1395 // Set the font/language of the inset...
1401 void Paragraph::resetFonts(Font const & font)
1403 d->fontlist_.clear();
1404 d->fontlist_.set(0, font);
1405 d->fontlist_.set(d->text_.size() - 1, font);
1408 // Gets uninstantiated font setting at position.
1409 Font const & Paragraph::getFontSettings(BufferParams const & bparams,
1413 LYXERR0("pos: " << pos << " size: " << size());
1414 LASSERT(pos <= size(), /**/);
1417 FontList::const_iterator cit = d->fontlist_.fontIterator(pos);
1418 if (cit != d->fontlist_.end())
1421 if (pos == size() && !empty())
1422 return getFontSettings(bparams, pos - 1);
1424 // Optimisation: avoid a full font instantiation if there is no
1425 // language change from previous call.
1426 static Font previous_font;
1427 static Language const * previous_lang = 0;
1428 Language const * lang = getParLanguage(bparams);
1429 if (lang != previous_lang) {
1430 previous_lang = lang;
1431 previous_font = Font(inherit_font, lang);
1433 return previous_font;
1437 FontSpan Paragraph::fontSpan(pos_type pos) const
1439 LASSERT(pos <= size(), /**/);
1442 FontList::const_iterator cit = d->fontlist_.begin();
1443 FontList::const_iterator end = d->fontlist_.end();
1444 for (; cit != end; ++cit) {
1445 if (cit->pos() >= pos) {
1446 if (pos >= beginOfBody())
1447 return FontSpan(max(start, beginOfBody()),
1450 return FontSpan(start,
1451 min(beginOfBody() - 1,
1454 start = cit->pos() + 1;
1457 // This should not happen, but if so, we take no chances.
1458 // LYXERR0("Paragraph::getEndPosOfFontSpan: This should not happen!");
1459 return FontSpan(pos, pos);
1463 // Gets uninstantiated font setting at position 0
1464 Font const & Paragraph::getFirstFontSettings(BufferParams const & bparams) const
1466 if (!empty() && !d->fontlist_.empty())
1467 return d->fontlist_.begin()->font();
1469 // Optimisation: avoid a full font instantiation if there is no
1470 // language change from previous call.
1471 static Font previous_font;
1472 static Language const * previous_lang = 0;
1473 if (bparams.language != previous_lang) {
1474 previous_lang = bparams.language;
1475 previous_font = Font(inherit_font, bparams.language);
1478 return previous_font;
1482 // Gets the fully instantiated font at a given position in a paragraph
1483 // This is basically the same function as Text::GetFont() in text2.cpp.
1484 // The difference is that this one is used for generating the LaTeX file,
1485 // and thus cosmetic "improvements" are disallowed: This has to deliver
1486 // the true picture of the buffer. (Asger)
1487 Font const Paragraph::getFont(BufferParams const & bparams, pos_type pos,
1488 Font const & outerfont) const
1490 LASSERT(pos >= 0, /**/);
1492 Font font = getFontSettings(bparams, pos);
1494 pos_type const body_pos = beginOfBody();
1495 FontInfo & fi = font.fontInfo();
1497 fi.realize(d->layout_->labelfont);
1499 fi.realize(d->layout_->font);
1501 fi.realize(outerfont.fontInfo());
1502 fi.realize(bparams.getFont().fontInfo());
1508 Font const Paragraph::getLabelFont
1509 (BufferParams const & bparams, Font const & outerfont) const
1511 FontInfo tmpfont = d->layout_->labelfont;
1512 tmpfont.realize(outerfont.fontInfo());
1513 tmpfont.realize(bparams.getFont().fontInfo());
1514 return Font(tmpfont, getParLanguage(bparams));
1518 Font const Paragraph::getLayoutFont
1519 (BufferParams const & bparams, Font const & outerfont) const
1521 FontInfo tmpfont = d->layout_->font;
1522 tmpfont.realize(outerfont.fontInfo());
1523 tmpfont.realize(bparams.getFont().fontInfo());
1524 return Font(tmpfont, getParLanguage(bparams));
1528 /// Returns the height of the highest font in range
1529 FontSize Paragraph::highestFontInRange
1530 (pos_type startpos, pos_type endpos, FontSize def_size) const
1532 return d->fontlist_.highestInRange(startpos, endpos, def_size);
1536 char_type Paragraph::getUChar(BufferParams const & bparams, pos_type pos) const
1538 char_type c = d->text_[pos];
1539 if (!lyxrc.rtl_support)
1569 if (uc != c && getFontSettings(bparams, pos).isRightToLeft())
1575 void Paragraph::setFont(pos_type pos, Font const & font)
1577 LASSERT(pos <= size(), /**/);
1579 // First, reduce font against layout/label font
1580 // Update: The setCharFont() routine in text2.cpp already
1581 // reduces font, so we don't need to do that here. (Asger)
1583 d->fontlist_.set(pos, font);
1587 void Paragraph::makeSameLayout(Paragraph const & par)
1589 d->layout_ = par.d->layout_;
1590 d->params_ = par.d->params_;
1594 bool Paragraph::stripLeadingSpaces(bool trackChanges)
1596 if (isFreeSpacing())
1602 while (pos < size() && (isNewline(pos) || isLineSeparator(pos))) {
1603 if (eraseChar(pos, trackChanges))
1609 return count > 0 || pos > 0;
1613 bool Paragraph::hasSameLayout(Paragraph const & par) const
1615 return par.d->layout_ == d->layout_
1616 && d->params_.sameLayout(par.d->params_);
1620 depth_type Paragraph::getDepth() const
1622 return d->params_.depth();
1626 depth_type Paragraph::getMaxDepthAfter() const
1628 if (d->layout_->isEnvironment())
1629 return d->params_.depth() + 1;
1631 return d->params_.depth();
1635 char Paragraph::getAlign() const
1637 if (d->params_.align() == LYX_ALIGN_LAYOUT)
1638 return d->layout_->align;
1640 return d->params_.align();
1644 docstring const & Paragraph::labelString() const
1646 return d->params_.labelString();
1650 // the next two functions are for the manual labels
1651 docstring const Paragraph::getLabelWidthString() const
1653 if (d->layout_->margintype == MARGIN_MANUAL
1654 || d->layout_->latextype == LATEX_BIB_ENVIRONMENT)
1655 return d->params_.labelWidthString();
1657 return _("Senseless with this layout!");
1661 void Paragraph::setLabelWidthString(docstring const & s)
1663 d->params_.labelWidthString(s);
1667 docstring Paragraph::expandLabel(Layout const & layout,
1668 BufferParams const & bparams) const
1670 return expandParagraphLabel(layout, bparams, true);
1674 docstring Paragraph::expandDocBookLabel(Layout const & layout,
1675 BufferParams const & bparams) const
1677 return expandParagraphLabel(layout, bparams, false);
1681 docstring Paragraph::expandParagraphLabel(Layout const & layout,
1682 BufferParams const & bparams, bool process_appendix) const
1684 DocumentClass const & tclass = bparams.documentClass();
1685 string const & lang = getParLanguage(bparams)->code();
1686 bool const in_appendix = process_appendix && d->params_.appendix();
1687 docstring fmt = translateIfPossible(layout.labelstring(in_appendix), lang);
1689 if (fmt.empty() && layout.labeltype == LABEL_COUNTER
1690 && !layout.counter.empty())
1691 return tclass.counters().theCounter(layout.counter, lang);
1693 // handle 'inherited level parts' in 'fmt',
1694 // i.e. the stuff between '@' in '@Section@.\arabic{subsection}'
1695 size_t const i = fmt.find('@', 0);
1696 if (i != docstring::npos) {
1697 size_t const j = fmt.find('@', i + 1);
1698 if (j != docstring::npos) {
1699 docstring parent(fmt, i + 1, j - i - 1);
1700 docstring label = from_ascii("??");
1701 if (tclass.hasLayout(parent))
1702 docstring label = expandParagraphLabel(tclass[parent], bparams,
1704 fmt = docstring(fmt, 0, i) + label
1705 + docstring(fmt, j + 1, docstring::npos);
1709 return tclass.counters().counterLabel(fmt, lang);
1713 void Paragraph::applyLayout(Layout const & new_layout)
1715 d->layout_ = &new_layout;
1716 LyXAlignment const oldAlign = d->params_.align();
1718 if (!(oldAlign & d->layout_->alignpossible)) {
1719 frontend::Alert::warning(_("Alignment not permitted"),
1720 _("The new layout does not permit the alignment previously used.\nSetting to default."));
1721 d->params_.align(LYX_ALIGN_LAYOUT);
1726 pos_type Paragraph::beginOfBody() const
1728 return d->begin_of_body_;
1732 void Paragraph::setBeginOfBody()
1734 if (d->layout_->labeltype != LABEL_MANUAL) {
1735 d->begin_of_body_ = 0;
1739 // Unroll the first two cycles of the loop
1740 // and remember the previous character to
1741 // remove unnecessary getChar() calls
1743 pos_type end = size();
1744 if (i < end && !isNewline(i)) {
1746 char_type previous_char = 0;
1749 previous_char = d->text_[i];
1750 if (!isNewline(i)) {
1752 while (i < end && previous_char != ' ') {
1757 previous_char = temp;
1763 d->begin_of_body_ = i;
1767 bool Paragraph::allowParagraphCustomization() const
1769 return inInset().allowParagraphCustomization();
1773 bool Paragraph::usePlainLayout() const
1775 return inInset().usePlainLayout();
1781 // paragraphs inside floats need different alignment tags to avoid
1784 bool noTrivlistCentering(InsetCode code)
1786 return code == FLOAT_CODE
1787 || code == WRAP_CODE
1788 || code == CELL_CODE;
1792 string correction(string const & orig)
1794 if (orig == "flushleft")
1795 return "raggedright";
1796 if (orig == "flushright")
1797 return "raggedleft";
1798 if (orig == "center")
1804 string const corrected_env(string const & suffix, string const & env,
1805 InsetCode code, bool const lastpar)
1807 string output = suffix + "{";
1808 if (noTrivlistCentering(code)) {
1810 // the last paragraph in non-trivlist-aligned
1811 // context is special (to avoid unwanted whitespace)
1812 if (suffix == "\\begin")
1813 return "\\" + correction(env) + "{}";
1816 output += correction(env);
1820 if (suffix == "\\begin")
1826 void adjust_row_column(string const & str, TexRow & texrow, int & column)
1828 if (!contains(str, "\n"))
1829 column += str.size();
1833 column = rsplit(str, tmp, '\n').size();
1840 int Paragraph::Private::startTeXParParams(BufferParams const & bparams,
1841 odocstream & os, TexRow & texrow,
1842 OutputParams const & runparams) const
1846 if (params_.noindent()) {
1847 os << "\\noindent ";
1851 LyXAlignment const curAlign = params_.align();
1853 if (curAlign == layout_->align)
1857 case LYX_ALIGN_NONE:
1858 case LYX_ALIGN_BLOCK:
1859 case LYX_ALIGN_LAYOUT:
1860 case LYX_ALIGN_SPECIAL:
1861 case LYX_ALIGN_DECIMAL:
1863 case LYX_ALIGN_LEFT:
1864 case LYX_ALIGN_RIGHT:
1865 case LYX_ALIGN_CENTER:
1866 if (runparams.moving_arg) {
1873 string const begin_tag = "\\begin";
1874 InsetCode code = ownerCode();
1875 bool const lastpar = runparams.isLastPar;
1878 case LYX_ALIGN_NONE:
1879 case LYX_ALIGN_BLOCK:
1880 case LYX_ALIGN_LAYOUT:
1881 case LYX_ALIGN_SPECIAL:
1882 case LYX_ALIGN_DECIMAL:
1884 case LYX_ALIGN_LEFT: {
1886 if (owner_->getParLanguage(bparams)->babel() != "hebrew")
1887 output = corrected_env(begin_tag, "flushleft", code, lastpar);
1889 output = corrected_env(begin_tag, "flushright", code, lastpar);
1890 os << from_ascii(output);
1891 adjust_row_column(output, texrow, column);
1893 } case LYX_ALIGN_RIGHT: {
1895 if (owner_->getParLanguage(bparams)->babel() != "hebrew")
1896 output = corrected_env(begin_tag, "flushright", code, lastpar);
1898 output = corrected_env(begin_tag, "flushleft", code, lastpar);
1899 os << from_ascii(output);
1900 adjust_row_column(output, texrow, column);
1902 } case LYX_ALIGN_CENTER: {
1904 output = corrected_env(begin_tag, "center", code, lastpar);
1905 os << from_ascii(output);
1906 adjust_row_column(output, texrow, column);
1915 int Paragraph::Private::endTeXParParams(BufferParams const & bparams,
1916 odocstream & os, TexRow & texrow,
1917 OutputParams const & runparams) const
1921 LyXAlignment const curAlign = params_.align();
1923 if (curAlign == layout_->align)
1927 case LYX_ALIGN_NONE:
1928 case LYX_ALIGN_BLOCK:
1929 case LYX_ALIGN_LAYOUT:
1930 case LYX_ALIGN_SPECIAL:
1931 case LYX_ALIGN_DECIMAL:
1933 case LYX_ALIGN_LEFT:
1934 case LYX_ALIGN_RIGHT:
1935 case LYX_ALIGN_CENTER:
1936 if (runparams.moving_arg) {
1943 string const end_tag = "\n\\par\\end";
1944 InsetCode code = ownerCode();
1945 bool const lastpar = runparams.isLastPar;
1948 case LYX_ALIGN_NONE:
1949 case LYX_ALIGN_BLOCK:
1950 case LYX_ALIGN_LAYOUT:
1951 case LYX_ALIGN_SPECIAL:
1952 case LYX_ALIGN_DECIMAL:
1954 case LYX_ALIGN_LEFT: {
1956 if (owner_->getParLanguage(bparams)->babel() != "hebrew")
1957 output = corrected_env(end_tag, "flushleft", code, lastpar);
1959 output = corrected_env(end_tag, "flushright", code, lastpar);
1960 os << from_ascii(output);
1961 adjust_row_column(output, texrow, column);
1963 } case LYX_ALIGN_RIGHT: {
1965 if (owner_->getParLanguage(bparams)->babel() != "hebrew")
1966 output = corrected_env(end_tag, "flushright", code, lastpar);
1968 output = corrected_env(end_tag, "flushleft", code, lastpar);
1969 os << from_ascii(output);
1970 adjust_row_column(output, texrow, column);
1972 } case LYX_ALIGN_CENTER: {
1974 output = corrected_env(end_tag, "center", code, lastpar);
1975 os << from_ascii(output);
1976 adjust_row_column(output, texrow, column);
1985 // This one spits out the text of the paragraph
1986 void Paragraph::latex(BufferParams const & bparams,
1987 Font const & outerfont,
1988 odocstream & os, TexRow & texrow,
1989 OutputParams const & runparams,
1990 int start_pos, int end_pos, bool force) const
1992 LYXERR(Debug::LATEX, "Paragraph::latex... " << this);
1994 // FIXME This check should not be needed. Perhaps issue an
1995 // error if it triggers.
1996 Layout const & style = inInset().forcePlainLayout() ?
1997 bparams.documentClass().plainLayout() : *d->layout_;
1999 if (!force && style.inpreamble)
2002 bool const allowcust = allowParagraphCustomization();
2004 // Current base font for all inherited font changes, without any
2005 // change caused by an individual character, except for the language:
2006 // It is set to the language of the first character.
2007 // As long as we are in the label, this font is the base font of the
2008 // label. Before the first body character it is set to the base font
2012 // Maybe we have to create a optional argument.
2013 pos_type body_pos = beginOfBody();
2014 unsigned int column = 0;
2017 // the optional argument is kept in curly brackets in
2018 // case it contains a ']'
2021 basefont = getLabelFont(bparams, outerfont);
2023 basefont = getLayoutFont(bparams, outerfont);
2026 // Which font is currently active?
2027 Font running_font(basefont);
2028 // Do we have an open font change?
2029 bool open_font = false;
2031 Change runningChange = Change(Change::UNCHANGED);
2033 Encoding const * const prev_encoding = runparams.encoding;
2035 texrow.start(id(), 0);
2037 // if the paragraph is empty, the loop will not be entered at all
2039 if (style.isCommand()) {
2044 column += d->startTeXParParams(bparams, os, texrow,
2048 for (pos_type i = 0; i < size(); ++i) {
2049 // First char in paragraph or after label?
2050 if (i == body_pos) {
2053 column += running_font.latexWriteEndChanges(
2054 os, bparams, runparams,
2055 basefont, basefont);
2058 basefont = getLayoutFont(bparams, outerfont);
2059 running_font = basefont;
2061 column += Changes::latexMarkChange(os, bparams,
2062 runningChange, Change(Change::UNCHANGED),
2064 runningChange = Change(Change::UNCHANGED);
2069 if (style.isCommand()) {
2075 column += d->startTeXParParams(bparams, os,
2080 Change const & change = runparams.inDeletedInset ? runparams.changeOfDeletedInset
2083 if (bparams.outputChanges && runningChange != change) {
2085 column += running_font.latexWriteEndChanges(
2086 os, bparams, runparams, basefont, basefont);
2089 basefont = getLayoutFont(bparams, outerfont);
2090 running_font = basefont;
2092 column += Changes::latexMarkChange(os, bparams, runningChange,
2094 runningChange = change;
2097 // do not output text which is marked deleted
2098 // if change tracking output is disabled
2099 if (!bparams.outputChanges && change.deleted()) {
2105 // Fully instantiated font
2106 Font const font = getFont(bparams, i, outerfont);
2108 Font const last_font = running_font;
2110 // Do we need to close the previous font?
2112 (font != running_font ||
2113 font.language() != running_font.language()))
2115 column += running_font.latexWriteEndChanges(
2116 os, bparams, runparams, basefont,
2117 (i == body_pos-1) ? basefont : font);
2118 running_font = basefont;
2122 // close babel's font environment before opening CJK.
2123 if (!running_font.language()->babel().empty() &&
2124 font.language()->encoding()->package() == Encoding::CJK) {
2125 string end_tag = subst(lyxrc.language_command_end,
2127 running_font.language()->babel());
2128 os << from_ascii(end_tag);
2129 column += end_tag.length();
2132 // Switch file encoding if necessary (and allowed)
2133 if (!runparams.pass_thru && !style.pass_thru &&
2134 runparams.encoding->package() != Encoding::none &&
2135 font.language()->encoding()->package() != Encoding::none) {
2136 pair<bool, int> const enc_switch = switchEncoding(os, bparams,
2137 runparams, *(font.language()->encoding()));
2138 if (enc_switch.first) {
2139 column += enc_switch.second;
2140 runparams.encoding = font.language()->encoding();
2144 char_type const c = d->text_[i];
2146 // Do we need to change font?
2147 if ((font != running_font ||
2148 font.language() != running_font.language()) &&
2151 odocstringstream ods;
2152 column += font.latexWriteStartChanges(ods, bparams,
2153 runparams, basefont,
2155 running_font = font;
2157 docstring fontchange = ods.str();
2158 // check whether the fontchange ends with a \\textcolor
2159 // modifier and the text starts with a space (bug 4473)
2160 docstring const last_modifier = rsplit(fontchange, '\\');
2161 if (prefixIs(last_modifier, from_ascii("textcolor")) && c == ' ')
2162 os << fontchange << from_ascii("{}");
2163 // check if the fontchange ends with a trailing blank
2164 // (like "\small " (see bug 3382)
2165 else if (suffixIs(fontchange, ' ') && c == ' ')
2166 os << fontchange.substr(0, fontchange.size() - 1)
2167 << from_ascii("{}");
2172 // FIXME: think about end_pos implementation...
2173 if (c == ' ' && i >= start_pos && (end_pos == -1 || i < end_pos)) {
2174 // FIXME: integrate this case in latexSpecialChar
2175 // Do not print the separation of the optional argument
2176 // if style.pass_thru is false. This works because
2177 // latexSpecialChar ignores spaces if
2178 // style.pass_thru is false.
2179 if (i != body_pos - 1) {
2180 if (d->simpleTeXBlanks(
2181 runparams, os, texrow,
2182 i, column, font, style)) {
2183 // A surrogate pair was output. We
2184 // must not call latexSpecialChar
2185 // in this iteration, since it would output
2186 // the combining character again.
2193 OutputParams rp = runparams;
2194 rp.free_spacing = style.free_spacing;
2195 rp.local_font = &font;
2196 rp.intitle = style.intitle;
2198 // Two major modes: LaTeX or plain
2199 // Handle here those cases common to both modes
2200 // and then split to handle the two modes separately.
2201 if (c == META_INSET) {
2202 if (i >= start_pos && (end_pos == -1 || i < end_pos)) {
2203 d->latexInset(bparams, os,
2204 texrow, rp, running_font,
2205 basefont, outerfont, open_font,
2206 runningChange, style, i, column);
2209 if (i >= start_pos && (end_pos == -1 || i < end_pos)) {
2211 d->latexSpecialChar(os, rp, running_font, runningChange,
2213 } catch (EncodingException & e) {
2214 if (runparams.dryrun) {
2215 os << "<" << _("LyX Warning: ")
2216 << _("uncodable character") << " '";
2220 // add location information and throw again.
2229 // Set the encoding to that returned from latexSpecialChar (see
2230 // comment for encoding member in OutputParams.h)
2231 runparams.encoding = rp.encoding;
2234 // If we have an open font definition, we have to close it
2236 #ifdef FIXED_LANGUAGE_END_DETECTION
2238 running_font.latexWriteEndChanges(os, bparams,
2239 runparams, basefont,
2240 next_->getFont(bparams, 0, outerfont));
2242 running_font.latexWriteEndChanges(os, bparams,
2243 runparams, basefont, basefont);
2246 //FIXME: For now we ALWAYS have to close the foreign font settings if they are
2247 //FIXME: there as we start another \selectlanguage with the next paragraph if
2248 //FIXME: we are in need of this. This should be fixed sometime (Jug)
2249 running_font.latexWriteEndChanges(os, bparams, runparams,
2250 basefont, basefont);
2254 column += Changes::latexMarkChange(os, bparams, runningChange,
2255 Change(Change::UNCHANGED), runparams);
2257 // Needed if there is an optional argument but no contents.
2258 if (body_pos > 0 && body_pos == size()) {
2262 if (allowcust && d->endTeXParParams(bparams, os, texrow, runparams)
2263 && runparams.encoding != prev_encoding) {
2264 runparams.encoding = prev_encoding;
2265 if (!bparams.useXetex)
2266 os << setEncoding(prev_encoding->iconvName());
2269 LYXERR(Debug::LATEX, "Paragraph::latex... done " << this);
2273 bool Paragraph::emptyTag() const
2275 for (pos_type i = 0; i < size(); ++i) {
2276 if (Inset const * inset = getInset(i)) {
2277 InsetCode lyx_code = inset->lyxCode();
2278 // FIXME testing like that is wrong. What is
2280 if (lyx_code != TOC_CODE &&
2281 lyx_code != INCLUDE_CODE &&
2282 lyx_code != GRAPHICS_CODE &&
2283 lyx_code != ERT_CODE &&
2284 lyx_code != LISTINGS_CODE &&
2285 lyx_code != FLOAT_CODE &&
2286 lyx_code != TABULAR_CODE) {
2290 char_type c = d->text_[i];
2291 if (c != ' ' && c != '\t')
2299 string Paragraph::getID(Buffer const & buf, OutputParams const & runparams)
2302 for (pos_type i = 0; i < size(); ++i) {
2303 if (Inset const * inset = getInset(i)) {
2304 InsetCode lyx_code = inset->lyxCode();
2305 if (lyx_code == LABEL_CODE) {
2306 InsetLabel const * const il = static_cast<InsetLabel const *>(inset);
2307 docstring const & id = il->getParam("name");
2308 return "id='" + to_utf8(sgml::cleanID(buf, runparams, id)) + "'";
2316 pos_type Paragraph::firstWordDocBook(odocstream & os, OutputParams const & runparams)
2320 for (i = 0; i < size(); ++i) {
2321 if (Inset const * inset = getInset(i)) {
2322 inset->docbook(os, runparams);
2324 char_type c = d->text_[i];
2327 os << sgml::escapeChar(c);
2334 pos_type Paragraph::firstWordLyXHTML(XHTMLStream & xs, OutputParams const & runparams)
2338 for (i = 0; i < size(); ++i) {
2339 if (Inset const * inset = getInset(i)) {
2340 inset->xhtml(xs, runparams);
2342 char_type c = d->text_[i];
2352 bool Paragraph::Private::onlyText(Buffer const & buf, Font const & outerfont, pos_type initial) const
2355 pos_type size = text_.size();
2356 for (pos_type i = initial; i < size; ++i) {
2357 Font font = owner_->getFont(buf.params(), i, outerfont);
2358 if (text_[i] == META_INSET)
2360 if (i != initial && font != font_old)
2369 void Paragraph::simpleDocBookOnePar(Buffer const & buf,
2371 OutputParams const & runparams,
2372 Font const & outerfont,
2373 pos_type initial) const
2375 bool emph_flag = false;
2377 Layout const & style = *d->layout_;
2379 style.labeltype == LABEL_MANUAL ? style.labelfont : style.font;
2381 if (style.pass_thru && !d->onlyText(buf, outerfont, initial))
2384 // parsing main loop
2385 for (pos_type i = initial; i < size(); ++i) {
2386 Font font = getFont(buf.params(), i, outerfont);
2388 // handle <emphasis> tag
2389 if (font_old.emph() != font.fontInfo().emph()) {
2390 if (font.fontInfo().emph() == FONT_ON) {
2393 } else if (i != initial) {
2394 os << "</emphasis>";
2399 if (Inset const * inset = getInset(i)) {
2400 inset->docbook(os, runparams);
2402 char_type c = d->text_[i];
2404 if (style.pass_thru)
2407 os << sgml::escapeChar(c);
2409 font_old = font.fontInfo();
2413 os << "</emphasis>";
2416 if (style.free_spacing)
2418 if (style.pass_thru && !d->onlyText(buf, outerfont, initial))
2423 docstring Paragraph::simpleLyXHTMLOnePar(Buffer const & buf,
2425 OutputParams const & runparams,
2426 Font const & outerfont,
2427 pos_type initial) const
2431 bool emph_flag = false;
2432 bool bold_flag = false;
2435 Layout const & style = *d->layout_;
2437 if (!runparams.for_toc && runparams.html_make_pars) {
2438 // generate a magic label for this paragraph
2439 string const attr = "id='" + magicLabel() + "'";
2440 xs << html::CompTag("a", attr);
2444 style.labeltype == LABEL_MANUAL ? style.labelfont : style.font;
2446 // parsing main loop
2447 for (pos_type i = initial; i < size(); ++i) {
2448 // let's not show deleted material in the output
2452 Font font = getFont(buf.params(), i, outerfont);
2455 if (font_old.emph() != font.fontInfo().emph()) {
2456 if (font.fontInfo().emph() == FONT_ON) {
2457 xs << html::StartTag("em");
2459 } else if (emph_flag && i != initial) {
2460 xs << html::EndTag("em");
2465 if (font_old.series() != font.fontInfo().series()) {
2466 if (font.fontInfo().series() == BOLD_SERIES) {
2467 xs << html::StartTag("strong");
2469 } else if (bold_flag && i != initial) {
2470 xs << html::EndTag("strong");
2475 // Other such tags? What about the other text ranges?
2477 Inset const * inset = getInset(i);
2479 InsetCommand const * ic = inset->asInsetCommand();
2480 InsetLayout const & il = inset->getLayout();
2481 InsetMath const * im = inset->asInsetMath();
2482 if (!runparams.for_toc
2483 || im || il.isInToc() || (ic && ic->isInToc())) {
2484 OutputParams np = runparams;
2485 if (!il.htmlisblock())
2486 np.html_in_par = true;
2487 retval += inset->xhtml(xs, np);
2490 char_type c = d->text_[i];
2492 if (style.pass_thru)
2494 else if (c == '-') {
2497 if (j < size() && d->text_[j] == '-') {
2499 if (j < size() && d->text_[j] == '-') {
2500 str += from_ascii("—");
2503 str += from_ascii("–");
2509 // We don't want to escape the entities. Note that
2510 // it is safe to do this, since str can otherwise
2511 // only be "-". E.g., it can't be "<".
2512 xs << XHTMLStream::NextRaw() << str;
2516 font_old = font.fontInfo();
2524 bool Paragraph::isHfill(pos_type pos) const
2526 Inset const * inset = getInset(pos);
2527 return inset && (inset->lyxCode() == SPACE_CODE &&
2528 inset->isStretchableSpace());
2532 bool Paragraph::isNewline(pos_type pos) const
2534 Inset const * inset = getInset(pos);
2535 return inset && inset->lyxCode() == NEWLINE_CODE;
2539 bool Paragraph::isLineSeparator(pos_type pos) const
2541 char_type const c = d->text_[pos];
2542 if (isLineSeparatorChar(c))
2544 Inset const * inset = getInset(pos);
2545 return inset && inset->isLineSeparator();
2549 bool Paragraph::isWordSeparator(pos_type pos) const
2551 if (Inset const * inset = getInset(pos))
2552 return !inset->isLetter();
2553 char_type const c = d->text_[pos];
2554 // We want to pass the ' and escape chars to the spellchecker
2555 static docstring const quote = from_utf8(lyxrc.spellchecker_esc_chars + '\'');
2556 return (!isLetterChar(c) && !isDigit(c) && !contains(quote, c))
2561 bool Paragraph::isChar(pos_type pos) const
2563 if (Inset const * inset = getInset(pos))
2564 return inset->isChar();
2565 char_type const c = d->text_[pos];
2566 return !isLetterChar(c) && !isDigit(c) && !lyx::isSpace(c);
2570 bool Paragraph::isSpace(pos_type pos) const
2572 if (Inset const * inset = getInset(pos))
2573 return inset->isSpace();
2574 char_type const c = d->text_[pos];
2575 return lyx::isSpace(c);
2580 Paragraph::getParLanguage(BufferParams const & bparams) const
2583 return getFirstFontSettings(bparams).language();
2584 // FIXME: we should check the prev par as well (Lgb)
2585 return bparams.language;
2589 bool Paragraph::isRTL(BufferParams const & bparams) const
2591 return lyxrc.rtl_support
2592 && getParLanguage(bparams)->rightToLeft()
2593 && !inInset().getLayout().forceLTR();
2597 void Paragraph::changeLanguage(BufferParams const & bparams,
2598 Language const * from, Language const * to)
2600 // change language including dummy font change at the end
2601 for (pos_type i = 0; i <= size(); ++i) {
2602 Font font = getFontSettings(bparams, i);
2603 if (font.language() == from) {
2604 font.setLanguage(to);
2611 bool Paragraph::isMultiLingual(BufferParams const & bparams) const
2613 Language const * doc_language = bparams.language;
2614 FontList::const_iterator cit = d->fontlist_.begin();
2615 FontList::const_iterator end = d->fontlist_.end();
2617 for (; cit != end; ++cit)
2618 if (cit->font().language() != ignore_language &&
2619 cit->font().language() != latex_language &&
2620 cit->font().language() != doc_language)
2626 void Paragraph::getLanguages(std::set<Language const *> & languages) const
2628 FontList::const_iterator cit = d->fontlist_.begin();
2629 FontList::const_iterator end = d->fontlist_.end();
2631 for (; cit != end; ++cit) {
2632 Language const * lang = cit->font().language();
2633 if (lang != ignore_language &&
2634 lang != latex_language)
2635 languages.insert(lang);
2640 docstring Paragraph::asString(int options) const
2642 return asString(0, size(), options);
2646 docstring Paragraph::asString(pos_type beg, pos_type end, int options) const
2648 odocstringstream os;
2651 && options & AS_STR_LABEL
2652 && !d->params_.labelString().empty())
2653 os << d->params_.labelString() << ' ';
2655 for (pos_type i = beg; i < end; ++i) {
2656 char_type const c = d->text_[i];
2657 if (isPrintable(c) || c == '\t'
2658 || (c == '\n' && (options & AS_STR_NEWLINES)))
2660 else if (c == META_INSET && (options & AS_STR_INSETS)) {
2661 getInset(i)->tocString(os);
2662 if (getInset(i)->asInsetMath())
2671 docstring Paragraph::stringify(pos_type beg, pos_type end, int options, OutputParams & runparams) const
2673 odocstringstream os;
2676 && options & AS_STR_LABEL
2677 && !d->params_.labelString().empty())
2678 os << d->params_.labelString() << ' ';
2680 for (pos_type i = beg; i < end; ++i) {
2681 char_type const c = d->text_[i];
2682 if (isPrintable(c) || c == '\t'
2683 || (c == '\n' && (options & AS_STR_NEWLINES)))
2685 else if (c == META_INSET && (options & AS_STR_INSETS)) {
2686 getInset(i)->plaintext(os, runparams);
2694 void Paragraph::setInsetOwner(Inset const * inset)
2696 d->inset_owner_ = inset;
2700 int Paragraph::id() const
2706 void Paragraph::setId(int id)
2712 Layout const & Paragraph::layout() const
2718 void Paragraph::setLayout(Layout const & layout)
2720 d->layout_ = &layout;
2724 void Paragraph::setDefaultLayout(DocumentClass const & tc)
2726 setLayout(tc.defaultLayout());
2730 void Paragraph::setPlainLayout(DocumentClass const & tc)
2732 setLayout(tc.plainLayout());
2736 void Paragraph::setPlainOrDefaultLayout(DocumentClass const & tclass)
2738 if (usePlainLayout())
2739 setPlainLayout(tclass);
2741 setDefaultLayout(tclass);
2745 Inset const & Paragraph::inInset() const
2747 LASSERT(d->inset_owner_, throw ExceptionMessage(BufferException,
2748 _("Memory problem"), _("Paragraph not properly initialized")));
2749 return *d->inset_owner_;
2753 ParagraphParameters & Paragraph::params()
2759 ParagraphParameters const & Paragraph::params() const
2765 bool Paragraph::isFreeSpacing() const
2767 if (d->layout_->free_spacing)
2769 return d->inset_owner_ && d->inset_owner_->isFreeSpacing();
2773 bool Paragraph::allowEmpty() const
2775 if (d->layout_->keepempty)
2777 return d->inset_owner_ && d->inset_owner_->allowEmpty();
2781 char_type Paragraph::transformChar(char_type c, pos_type pos) const
2783 if (!Encodings::isArabicChar(c))
2786 char_type prev_char = ' ';
2787 char_type next_char = ' ';
2789 for (pos_type i = pos - 1; i >= 0; --i) {
2790 char_type const par_char = d->text_[i];
2791 if (!Encodings::isArabicComposeChar(par_char)) {
2792 prev_char = par_char;
2797 for (pos_type i = pos + 1, end = size(); i < end; ++i) {
2798 char_type const par_char = d->text_[i];
2799 if (!Encodings::isArabicComposeChar(par_char)) {
2800 next_char = par_char;
2805 if (Encodings::isArabicChar(next_char)) {
2806 if (Encodings::isArabicChar(prev_char) &&
2807 !Encodings::isArabicSpecialChar(prev_char))
2808 return Encodings::transformChar(c, Encodings::FORM_MEDIAL);
2810 return Encodings::transformChar(c, Encodings::FORM_INITIAL);
2812 if (Encodings::isArabicChar(prev_char) &&
2813 !Encodings::isArabicSpecialChar(prev_char))
2814 return Encodings::transformChar(c, Encodings::FORM_FINAL);
2816 return Encodings::transformChar(c, Encodings::FORM_ISOLATED);
2821 int Paragraph::checkBiblio(Buffer const & buffer)
2824 // This is getting more and more a mess. ...We really should clean
2825 // up this bibitem issue for 1.6. See also bug 2743.
2827 // Add bibitem insets if necessary
2828 if (d->layout_->labeltype != LABEL_BIBLIO)
2831 bool hasbibitem = !d->insetlist_.empty()
2832 // Insist on it being in pos 0
2833 && d->text_[0] == META_INSET
2834 && d->insetlist_.begin()->inset->lyxCode() == BIBITEM_CODE;
2836 bool track_changes = buffer.params().trackChanges;
2841 // remove a bibitem in pos != 0
2842 // restore it later in pos 0 if necessary
2843 // (e.g. if a user inserts contents _before_ the item)
2844 // we're assuming there's only one of these, which there
2846 int erasedInsetPosition = -1;
2847 InsetList::iterator it = d->insetlist_.begin();
2848 InsetList::iterator end = d->insetlist_.end();
2849 for (; it != end; ++it)
2850 if (it->inset->lyxCode() == BIBITEM_CODE
2852 InsetBibitem * olditem = static_cast<InsetBibitem *>(it->inset);
2853 oldkey = olditem->getParam("key");
2854 oldlabel = olditem->getParam("label");
2855 erasedInsetPosition = it->pos;
2856 eraseChar(erasedInsetPosition, track_changes);
2860 // There was an InsetBibitem at the beginning, and we didn't
2861 // have to erase one.
2862 if (hasbibitem && erasedInsetPosition < 0)
2865 // There was an InsetBibitem at the beginning and we did have to
2866 // erase one. So we give its properties to the beginning inset.
2868 InsetBibitem * inset =
2869 static_cast<InsetBibitem *>(d->insetlist_.begin()->inset);
2870 if (!oldkey.empty())
2871 inset->setParam("key", oldkey);
2872 inset->setParam("label", oldlabel);
2873 return -erasedInsetPosition;
2876 // There was no inset at the beginning, so we need to create one with
2877 // the key and label of the one we erased.
2878 InsetBibitem * inset =
2879 new InsetBibitem(const_cast<Buffer *>(&buffer), InsetCommandParams(BIBITEM_CODE));
2880 // restore values of previously deleted item in this par.
2881 if (!oldkey.empty())
2882 inset->setParam("key", oldkey);
2883 inset->setParam("label", oldlabel);
2884 insertInset(0, static_cast<Inset *>(inset),
2885 Change(track_changes ? Change::INSERTED : Change::UNCHANGED));
2891 void Paragraph::checkAuthors(AuthorList const & authorList)
2893 d->changes_.checkAuthors(authorList);
2897 bool Paragraph::isChanged(pos_type pos) const
2899 return lookupChange(pos).changed();
2903 bool Paragraph::isInserted(pos_type pos) const
2905 return lookupChange(pos).inserted();
2909 bool Paragraph::isDeleted(pos_type pos) const
2911 return lookupChange(pos).deleted();
2915 InsetList const & Paragraph::insetList() const
2917 return d->insetlist_;
2921 void Paragraph::setBuffer(Buffer & b)
2923 d->insetlist_.setBuffer(b);
2927 Inset * Paragraph::releaseInset(pos_type pos)
2929 Inset * inset = d->insetlist_.release(pos);
2930 /// does not honour change tracking!
2931 eraseChar(pos, false);
2936 Inset * Paragraph::getInset(pos_type pos)
2938 return (pos < pos_type(d->text_.size()) && d->text_[pos] == META_INSET)
2939 ? d->insetlist_.get(pos) : 0;
2943 Inset const * Paragraph::getInset(pos_type pos) const
2945 return (pos < pos_type(d->text_.size()) && d->text_[pos] == META_INSET)
2946 ? d->insetlist_.get(pos) : 0;
2950 void Paragraph::changeCase(BufferParams const & bparams, pos_type pos,
2951 pos_type & right, TextCase action)
2953 // process sequences of modified characters; in change
2954 // tracking mode, this approach results in much better
2955 // usability than changing case on a char-by-char basis
2958 bool const trackChanges = bparams.trackChanges;
2960 bool capitalize = true;
2962 for (; pos < right; ++pos) {
2963 char_type oldChar = d->text_[pos];
2964 char_type newChar = oldChar;
2966 // ignore insets and don't play with deleted text!
2967 if (oldChar != META_INSET && !isDeleted(pos)) {
2969 case text_lowercase:
2970 newChar = lowercase(oldChar);
2972 case text_capitalization:
2974 newChar = uppercase(oldChar);
2978 case text_uppercase:
2979 newChar = uppercase(oldChar);
2984 if (isWordSeparator(pos) || isDeleted(pos)) {
2985 // permit capitalization again
2989 if (oldChar != newChar) {
2991 if (pos != right - 1)
2993 // step behind the changing area
2997 int erasePos = pos - changes.size();
2998 for (size_t i = 0; i < changes.size(); i++) {
2999 insertChar(pos, changes[i],
3000 getFontSettings(bparams,
3003 if (!eraseChar(erasePos, trackChanges)) {
3006 ++right; // expand selection
3014 bool Paragraph::find(docstring const & str, bool cs, bool mw,
3015 pos_type pos, bool del) const
3017 int const strsize = str.length();
3019 pos_type const parsize = d->text_.size();
3020 for (i = 0; pos + i < parsize; ++i) {
3023 if (cs && str[i] != d->text_[pos + i])
3025 if (!cs && uppercase(str[i]) != uppercase(d->text_[pos + i]))
3027 if (!del && isDeleted(pos + i))
3034 // if necessary, check whether string matches word
3036 if (pos > 0 && !isWordSeparator(pos - 1))
3038 if (pos + strsize < parsize
3039 && !isWordSeparator(pos + strsize))
3047 char_type Paragraph::getChar(pos_type pos) const
3049 return d->text_[pos];
3053 pos_type Paragraph::size() const
3055 return d->text_.size();
3059 bool Paragraph::empty() const
3061 return d->text_.empty();
3065 bool Paragraph::isInset(pos_type pos) const
3067 return d->text_[pos] == META_INSET;
3071 bool Paragraph::isSeparator(pos_type pos) const
3073 //FIXME: Are we sure this can be the only separator?
3074 return d->text_[pos] == ' ';
3078 void Paragraph::deregisterWords()
3080 Private::LangWordsMap::const_iterator itl = d->words_.begin();
3081 Private::LangWordsMap::const_iterator ite = d->words_.end();
3082 for (; itl != ite; ++itl) {
3083 WordList * wl = theWordList(itl->first);
3084 Private::Words::const_iterator it = (itl->second).begin();
3085 Private::Words::const_iterator et = (itl->second).end();
3086 for (; it != et; ++it)
3093 void Paragraph::locateWord(pos_type & from, pos_type & to,
3094 word_location const loc) const
3097 case WHOLE_WORD_STRICT:
3098 if (from == 0 || from == size()
3099 || isWordSeparator(from)
3100 || isWordSeparator(from - 1)) {
3104 // no break here, we go to the next
3107 // If we are already at the beginning of a word, do nothing
3108 if (!from || isWordSeparator(from - 1))
3110 // no break here, we go to the next
3113 // always move the cursor to the beginning of previous word
3114 while (from && !isWordSeparator(from - 1))
3118 LYXERR0("Paragraph::locateWord: NEXT_WORD not implemented yet");
3121 // no need to move the 'from' cursor
3125 while (to < size() && !isWordSeparator(to))
3130 void Paragraph::collectWords()
3132 // This is the value that needs to be exposed in the preferences
3133 // to resolve bug #6760.
3134 static int minlength = 6;
3135 pos_type n = size();
3136 for (pos_type pos = 0; pos < n; ++pos) {
3137 if (isWordSeparator(pos))
3139 pos_type from = pos;
3140 locateWord(from, pos, WHOLE_WORD);
3141 if (pos - from >= minlength) {
3142 docstring word = asString(from, pos, AS_STR_NONE);
3143 FontList::const_iterator cit = d->fontlist_.fontIterator(pos);
3144 if (cit == d->fontlist_.end())
3146 Language const * lang = cit->font().language();
3147 d->words_[*lang].insert(word);
3153 void Paragraph::registerWords()
3155 Private::LangWordsMap::const_iterator itl = d->words_.begin();
3156 Private::LangWordsMap::const_iterator ite = d->words_.end();
3157 for (; itl != ite; ++itl) {
3158 WordList * wl = theWordList(itl->first);
3159 Private::Words::const_iterator it = (itl->second).begin();
3160 Private::Words::const_iterator et = (itl->second).end();
3161 for (; it != et; ++it)
3167 void Paragraph::updateWords()
3175 SpellChecker::Result Paragraph::spellCheck(pos_type & from, pos_type & to, WordLangTuple & wl,
3176 docstring_list & suggestions, bool do_suggestion) const
3178 SpellChecker * speller = theSpellChecker();
3180 return SpellChecker::WORD_OK;
3182 if (!d->layout_->spellcheck || !inInset().allowSpellCheck())
3183 return SpellChecker::WORD_OK;
3185 locateWord(from, to, WHOLE_WORD);
3186 if (from == to || from >= pos_type(d->text_.size()))
3187 return SpellChecker::WORD_OK;
3189 docstring word = asString(from, to, AS_STR_INSETS);
3190 // Ignore words with digits
3191 // FIXME: make this customizable
3192 // (note that hunspell ignores words with digits by default)
3193 bool const ignored = hasDigit(word);
3194 Language * lang = const_cast<Language *>(getFontSettings(
3195 d->inset_owner_->buffer().params(), from).language());
3196 if (lang == d->inset_owner_->buffer().params().language
3197 && !lyxrc.spellchecker_alt_lang.empty()) {
3199 string const lang_variety =
3200 split(lyxrc.spellchecker_alt_lang, lang_code, '-');
3201 lang->setCode(lang_code);
3202 lang->setVariety(lang_variety);
3204 wl = WordLangTuple(word, lang);
3205 SpellChecker::Result res = ignored ?
3206 SpellChecker::WORD_OK : speller->check(wl);
3208 bool const misspelled_ = SpellChecker::misspelled(res) ;
3210 if (lyxrc.spellcheck_continuously)
3211 d->setMisspelled(from, to, misspelled_);
3213 if (misspelled_ && do_suggestion)
3214 speller->suggest(wl, suggestions);
3216 suggestions.clear();
3222 bool Paragraph::isMisspelled(pos_type pos) const
3224 pos_type from = pos;
3227 docstring_list suggestions;
3228 SpellChecker::Result res = spellCheck(from, to, wl, suggestions, false);
3229 return SpellChecker::misspelled(res) ;
3233 string Paragraph::magicLabel() const
3236 ss << "magicparlabel-" << id();