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"
28 #include "BufferEncodings.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 "output_docbook.h"
41 #include "ParagraphParameters.h"
42 #include "SpellChecker.h"
44 #include "texstream.h"
45 #include "TextClass.h"
48 #include "WordLangTuple.h"
51 #include "frontends/alert.h"
53 #include "insets/InsetBibitem.h"
54 #include "insets/InsetLabel.h"
55 #include "insets/InsetSpecialChar.h"
56 #include "insets/InsetText.h"
58 #include "mathed/InsetMathHull.h"
60 #include "support/debug.h"
61 #include "support/docstring_list.h"
62 #include "support/ExceptionMessage.h"
63 #include "support/gettext.h"
64 #include "support/lassert.h"
65 #include "support/lstrings.h"
66 #include "support/textutils.h"
67 #include "output_docbook.h"
74 using namespace lyx::support;
76 // OSX clang, gcc < 4.8.0, and msvc < 2015 do not support C++11 thread_local
77 #if defined(__APPLE__) || (defined(__GNUC__) && __GNUC__ == 4 && __GNUC_MINOR__ < 8)
78 #define THREAD_LOCAL_STATIC static __thread
79 #elif defined(_MSC_VER) && (_MSC_VER < 1900)
80 #define THREAD_LOCAL_STATIC static __declspec(thread)
82 #define THREAD_LOCAL_STATIC thread_local static
87 /////////////////////////////////////////////////////////////////////
91 /////////////////////////////////////////////////////////////////////
93 class SpellResultRange {
95 SpellResultRange(FontSpan range, SpellChecker::Result result)
96 : range_(range), result_(result)
99 FontSpan const & range() const { return range_; }
101 void range(FontSpan const & r) { range_ = r; }
103 SpellChecker::Result result() const { return result_; }
105 void result(SpellChecker::Result r) { result_ = r; }
107 bool contains(pos_type pos) const { return range_.contains(pos); }
109 bool covered(FontSpan const & r) const
111 // 1. first of new range inside current range or
112 // 2. last of new range inside current range or
113 // 3. first of current range inside new range or
114 // 4. last of current range inside new range
115 //FIXME: is this the same as !range_.intersect(r).empty() ?
116 return range_.contains(r.first) || range_.contains(r.last) ||
117 r.contains(range_.first) || r.contains(range_.last);
120 void shift(pos_type pos, int offset)
122 if (range_.first > pos) {
123 range_.first += offset;
124 range_.last += offset;
125 } else if (range_.last >= pos) {
126 range_.last += offset;
131 SpellChecker::Result result_ ;
135 /////////////////////////////////////////////////////////////////////
139 /////////////////////////////////////////////////////////////////////
141 class SpellCheckerState {
145 needs_refresh_ = true;
146 current_change_number_ = 0;
149 void setRange(FontSpan const & fp, SpellChecker::Result state)
152 RangesIterator et = ranges_.end();
153 RangesIterator it = ranges_.begin();
154 for (; it != et; ++it) {
155 if (!it->covered(fp))
156 result.push_back(SpellResultRange(it->range(), it->result()));
157 else if (state == SpellChecker::WORD_OK) {
158 // trim or split the current misspelled range
159 // store misspelled ranges only
160 FontSpan range = it->range();
161 if (fp.first > range.first) {
162 // misspelled area in front of WORD_OK
163 range.last = fp.first - 1;
164 result.push_back(SpellResultRange(range, it->result()));
167 if (fp.last < range.last) {
168 // misspelled area after WORD_OK range
169 range.first = fp.last + 1;
170 result.push_back(SpellResultRange(range, it->result()));
175 if (state != SpellChecker::WORD_OK)
176 ranges_.push_back(SpellResultRange(fp, state));
179 void increasePosAfterPos(pos_type pos)
181 correctRangesAfterPos(pos, 1);
185 void decreasePosAfterPos(pos_type pos)
187 correctRangesAfterPos(pos, -1);
191 void refreshLast(pos_type pos)
193 if (pos < refresh_.last)
197 SpellChecker::Result getState(pos_type pos) const
199 SpellChecker::Result result = SpellChecker::WORD_OK;
200 RangesIterator et = ranges_.end();
201 RangesIterator it = ranges_.begin();
202 for (; it != et; ++it) {
203 if(it->contains(pos)) {
210 FontSpan const & getRange(pos_type pos) const
212 /// empty span to indicate mismatch
213 static FontSpan empty_;
214 RangesIterator et = ranges_.end();
215 RangesIterator it = ranges_.begin();
216 for (; it != et; ++it) {
217 if(it->contains(pos)) {
224 bool needsRefresh() const
226 return needs_refresh_;
229 SpellChecker::ChangeNumber currentChangeNumber() const
231 return current_change_number_;
234 void refreshRange(pos_type & first, pos_type & last) const
236 first = refresh_.first;
237 last = refresh_.last;
240 void needsRefresh(pos_type pos)
242 if (needs_refresh_ && pos != -1) {
243 if (pos < refresh_.first)
244 refresh_.first = pos;
245 if (pos > refresh_.last)
247 } else if (pos != -1) {
248 // init request check for neighbour positions too
249 refresh_.first = pos > 0 ? pos - 1 : 0;
250 // no need for special end of paragraph check
251 refresh_.last = pos + 1;
253 needs_refresh_ = pos != -1;
256 void needsCompleteRefresh(SpellChecker::ChangeNumber change_number)
258 needs_refresh_ = true;
261 current_change_number_ = change_number;
264 typedef vector<SpellResultRange> Ranges;
265 typedef Ranges::const_iterator RangesIterator;
267 /// the area of the paragraph with pending spell check
270 /// spell state cache version number
271 SpellChecker::ChangeNumber current_change_number_;
274 void correctRangesAfterPos(pos_type pos, int offset)
276 RangesIterator et = ranges_.end();
277 Ranges::iterator it = ranges_.begin();
278 for (; it != et; ++it) {
279 it->shift(pos, offset);
285 /////////////////////////////////////////////////////////////////////
287 // Paragraph::Private
289 /////////////////////////////////////////////////////////////////////
291 class Paragraph::Private
293 // Enforce our own "copy" constructor
294 Private(Private const &) = delete;
295 Private & operator=(Private const &) = delete;
296 // Unique ID generator
297 static int make_id();
300 Private(Paragraph * owner, Layout const & layout);
301 /// "Copy constructor"
302 Private(Private const &, Paragraph * owner);
303 /// Copy constructor from \p beg to \p end
304 Private(Private const &, Paragraph * owner, pos_type beg, pos_type end);
307 void insertChar(pos_type pos, char_type c, Change const & change);
309 /// Output the surrogate pair formed by \p c and \p next to \p os.
310 /// \return the number of characters written.
311 int latexSurrogatePair(BufferParams const &, otexstream & os,
312 char_type c, char_type next,
313 OutputParams const &);
315 /// Output a space in appropriate formatting (or a surrogate pair
316 /// if the next character is a combining character).
317 /// \return whether a surrogate pair was output.
318 bool simpleTeXBlanks(BufferParams const &,
319 OutputParams const &,
322 unsigned int & column,
324 Layout const & style);
326 /// This could go to ParagraphParameters if we want to.
327 int startTeXParParams(BufferParams const &, otexstream &,
328 OutputParams const &) const;
330 /// This could go to ParagraphParameters if we want to.
331 bool endTeXParParams(BufferParams const &, otexstream &,
332 OutputParams const &) const;
335 void latexInset(BufferParams const &,
340 Font const & outerfont,
342 Change & running_change,
343 Layout const & style,
345 unsigned int & column);
348 void latexSpecialChar(
350 BufferParams const & bparams,
351 OutputParams const & runparams,
352 Font const & running_font,
353 string & alien_script,
354 Layout const & style,
357 unsigned int & column);
364 unsigned int & column);
370 unsigned int & column);
376 unsigned int & column);
379 void validate(LaTeXFeatures & features) const;
381 /// Checks if the paragraph contains only text and no inset or font change.
382 bool onlyText(Buffer const & buf, Font const & outerfont,
383 pos_type initial) const;
385 /// a vector of speller skip positions
386 typedef vector<FontSpan> SkipPositions;
387 typedef SkipPositions::const_iterator SkipPositionsIterator;
389 void appendSkipPosition(SkipPositions & skips, pos_type const pos) const;
391 Language * getSpellLanguage(pos_type const from) const;
393 Language * locateSpellRange(pos_type & from, pos_type & to,
394 SkipPositions & skips) const;
396 bool hasSpellerChange() const
398 SpellChecker::ChangeNumber speller_change_number = 0;
399 if (theSpellChecker())
400 speller_change_number = theSpellChecker()->changeNumber();
401 return speller_change_number > speller_state_.currentChangeNumber();
404 bool ignoreWord(docstring const & word) const ;
406 void setMisspelled(pos_type from, pos_type to, SpellChecker::Result state)
408 pos_type textsize = owner_->size();
409 // check for sane arguments
410 if (to <= from || from >= textsize)
412 FontSpan fp = FontSpan(from, to - 1);
413 speller_state_.setRange(fp, state);
416 void requestSpellCheck(pos_type pos)
419 speller_state_.needsCompleteRefresh(speller_state_.currentChangeNumber());
421 speller_state_.needsRefresh(pos);
424 void readySpellCheck()
426 speller_state_.needsRefresh(-1);
429 bool needsSpellCheck() const
431 return speller_state_.needsRefresh();
434 void rangeOfSpellCheck(pos_type & first, pos_type & last) const
436 speller_state_.refreshRange(first, last);
438 last = owner_->size();
441 pos_type endpos = last;
442 owner_->locateWord(first, endpos, WHOLE_WORD, true);
445 owner_->locateWord(last, endpos, WHOLE_WORD, true);
450 int countSkips(SkipPositionsIterator & it, SkipPositionsIterator const et,
454 while (it != et && it->first < start) {
455 int skip = it->last - it->first + 1;
463 void markMisspelledWords(pos_type const & first, pos_type const & last,
464 SpellChecker::Result result,
465 docstring const & word,
466 SkipPositions const & skips);
468 InsetCode ownerCode() const
470 return inset_owner_ ? inset_owner_->lyxCode() : NO_CODE;
473 /// Which Paragraph owns us?
477 Inset const * inset_owner_;
486 ParagraphParameters params_;
488 /// for recording and looking up changes
492 InsetList insetlist_;
495 pos_type begin_of_body_;
497 typedef docstring TextContainer;
501 typedef set<docstring> Words;
502 typedef map<string, Words> LangWordsMap;
506 Layout const * layout_;
508 SpellCheckerState speller_state_;
512 Paragraph::Private::Private(Paragraph * owner, Layout const & layout)
513 : owner_(owner), inset_owner_(nullptr), id_(-1), begin_of_body_(0), layout_(&layout)
520 int Paragraph::Private::make_id()
522 // The id is unique per session across buffers because it is used in
523 // LFUN_PARAGRAPH_GOTO to switch to a different buffer, for instance in the
526 static atomic_uint next_id(0);
531 Paragraph::Private::Private(Private const & p, Paragraph * owner)
532 : owner_(owner), inset_owner_(p.inset_owner_), fontlist_(p.fontlist_),
534 params_(p.params_), changes_(p.changes_), insetlist_(p.insetlist_),
535 begin_of_body_(p.begin_of_body_), text_(p.text_), words_(p.words_),
538 requestSpellCheck(p.text_.size());
542 Paragraph::Private::Private(Private const & p, Paragraph * owner,
543 pos_type beg, pos_type end)
544 : owner_(owner), inset_owner_(p.inset_owner_), id_(make_id()),
545 params_(p.params_), changes_(p.changes_),
546 insetlist_(p.insetlist_, beg, end),
547 begin_of_body_(p.begin_of_body_), words_(p.words_),
550 if (beg >= pos_type(p.text_.size()))
552 text_ = p.text_.substr(beg, end - beg);
554 FontList::const_iterator fcit = fontlist_.begin();
555 FontList::const_iterator fend = fontlist_.end();
556 for (; fcit != fend; ++fcit) {
557 if (fcit->pos() < beg)
559 if (fcit->pos() >= end) {
560 // Add last entry in the fontlist_.
561 fontlist_.set(text_.size() - 1, fcit->font());
564 // Add a new entry in the fontlist_.
565 fontlist_.set(fcit->pos() - beg, fcit->font());
567 requestSpellCheck(p.text_.size());
571 void Paragraph::addChangesToToc(DocIterator const & cdit, Buffer const & buf,
572 bool output_active, TocBackend & backend) const
574 d->changes_.addToToc(cdit, buf, output_active, backend);
578 bool Paragraph::isDeleted(pos_type start, pos_type end) const
580 LASSERT(start >= 0 && start <= size(), return false);
581 LASSERT(end > start && end <= size() + 1, return false);
583 return d->changes_.isDeleted(start, end);
587 bool Paragraph::isChanged(pos_type start, pos_type end) const
589 LASSERT(start >= 0 && start <= size(), return false);
590 LASSERT(end > start && end <= size() + 1, return false);
592 return d->changes_.isChanged(start, end);
595 // FIXME: Ideally the diverse isChanged() methods should account for that!
596 bool Paragraph::hasChangedInsets(pos_type start, pos_type end) const
598 LASSERT(start >= 0 && start <= size(), return false);
599 LASSERT(end > start && end <= size() + 1, return false);
601 for (auto const & icit : d->insetlist_) {
602 if (icit.pos < start)
606 if (icit.inset && icit.inset->isChanged())
612 bool Paragraph::isChanged() const
614 return d->changes_.isChanged();
618 bool Paragraph::isMergedOnEndOfParDeletion(bool trackChanges) const
620 // keep the logic here in sync with the logic of eraseChars()
624 Change const & change = d->changes_.lookup(size());
625 return change.inserted() && change.currentAuthor();
628 Change Paragraph::parEndChange() const
630 return d->changes_.lookup(size());
634 void Paragraph::setChange(Change const & change)
636 // beware of the imaginary end-of-par character!
637 d->changes_.set(change, 0, size() + 1);
640 * Propagate the change recursively - but not in case of DELETED!
642 * Imagine that your co-author makes changes in an existing inset. He
643 * sends your document to you and you come to the conclusion that the
644 * inset should go completely. If you erase it, LyX must not delete all
645 * text within the inset. Otherwise, the change tracked insertions of
646 * your co-author get lost and there is no way to restore them later.
648 * Conclusion: An inset's content should remain untouched if you delete it
651 if (!change.deleted()) {
652 for (pos_type pos = 0; pos < size(); ++pos) {
653 if (Inset * inset = getInset(pos))
654 inset->setChange(change);
660 void Paragraph::setChange(pos_type pos, Change const & change)
662 LASSERT(pos >= 0 && pos <= size(), return);
663 d->changes_.set(change, pos);
665 // see comment in setChange(Change const &) above
666 if (!change.deleted() && pos < size())
667 if (Inset * inset = getInset(pos))
668 inset->setChange(change);
672 Change const & Paragraph::lookupChange(pos_type pos) const
674 LBUFERR(pos >= 0 && pos <= size());
675 return d->changes_.lookup(pos);
679 void Paragraph::acceptChanges(pos_type start, pos_type end)
681 LASSERT(start >= 0 && start <= size(), return);
682 LASSERT(end > start && end <= size() + 1, return);
684 for (pos_type pos = start; pos < end; ++pos) {
685 switch (lookupChange(pos).type) {
686 case Change::UNCHANGED:
687 // accept changes in nested inset
688 if (Inset * inset = getInset(pos))
689 inset->acceptChanges();
692 case Change::INSERTED:
693 d->changes_.set(Change(Change::UNCHANGED), pos);
694 // also accept changes in nested inset
695 if (Inset * inset = getInset(pos))
696 inset->acceptChanges();
699 case Change::DELETED:
700 // Suppress access to non-existent
701 // "end-of-paragraph char"
703 eraseChar(pos, false);
714 void Paragraph::rejectChanges(pos_type start, pos_type end)
716 LASSERT(start >= 0 && start <= size(), return);
717 LASSERT(end > start && end <= size() + 1, return);
719 for (pos_type pos = start; pos < end; ++pos) {
720 switch (lookupChange(pos).type) {
721 case Change::UNCHANGED:
722 // reject changes in nested inset
723 if (Inset * inset = getInset(pos))
724 inset->rejectChanges();
727 case Change::INSERTED:
728 // Suppress access to non-existent
729 // "end-of-paragraph char"
731 eraseChar(pos, false);
737 case Change::DELETED:
738 d->changes_.set(Change(Change::UNCHANGED), pos);
740 // Do NOT reject changes within a deleted inset!
741 // There may be insertions of a co-author inside of it!
749 void Paragraph::Private::insertChar(pos_type pos, char_type c,
750 Change const & change)
752 LASSERT(pos >= 0 && pos <= int(text_.size()), return);
755 changes_.insert(change, pos);
757 // This is actually very common when parsing buffers (and
758 // maybe inserting ascii text)
759 if (pos == pos_type(text_.size())) {
760 // when appending characters, no need to update tables
762 // but we want spell checking
763 requestSpellCheck(pos);
767 text_.insert(text_.begin() + pos, c);
769 // Update the font table.
770 fontlist_.increasePosAfterPos(pos);
773 insetlist_.increasePosAfterPos(pos);
775 // Update list of misspelled positions
776 speller_state_.increasePosAfterPos(pos);
780 bool Paragraph::insertInset(pos_type pos, Inset * inset,
781 Font const & font, Change const & change)
783 LASSERT(inset, return false);
784 LASSERT(pos >= 0 && pos <= size(), return false);
786 // Paragraph::insertInset() can be used in cut/copy/paste operation where
787 // d->inset_owner_ is not set yet.
788 if (d->inset_owner_ && !d->inset_owner_->insetAllowed(inset->lyxCode()))
791 d->insertChar(pos, META_INSET, change);
792 LASSERT(d->text_[pos] == META_INSET, return false);
794 // Add a new entry in the insetlist_.
795 d->insetlist_.insert(inset, pos);
797 // Some insets require run of spell checker
798 requestSpellCheck(pos);
804 bool Paragraph::eraseChar(pos_type pos, bool trackChanges)
806 LASSERT(pos >= 0 && pos <= size(), return false);
808 // keep the logic here in sync with the logic of isMergedOnEndOfParDeletion()
811 Change change = d->changes_.lookup(pos);
813 // set the character to DELETED if
814 // a) it was previously unchanged or
815 // b) it was inserted by a co-author
817 if (!change.changed() ||
818 (change.inserted() && !change.currentAuthor())) {
819 setChange(pos, Change(Change::DELETED));
820 // request run of spell checker
821 requestSpellCheck(pos);
825 if (change.deleted())
829 // Don't physically access the imaginary end-of-paragraph character.
830 // eraseChar() can only mark it as DELETED. A physical deletion of
831 // end-of-par must be handled externally.
837 d->changes_.erase(pos);
839 // if it is an inset, delete the inset entry
840 if (d->text_[pos] == META_INSET)
841 d->insetlist_.erase(pos);
843 d->text_.erase(d->text_.begin() + pos);
845 // Update the fontlist_
846 d->fontlist_.erase(pos);
848 // Update the insetlist_
849 d->insetlist_.decreasePosAfterPos(pos);
851 // Update list of misspelled positions
852 d->speller_state_.decreasePosAfterPos(pos);
853 d->speller_state_.refreshLast(size());
859 int Paragraph::eraseChars(pos_type start, pos_type end, bool trackChanges)
861 LASSERT(start >= 0 && start <= size(), return 0);
862 LASSERT(end >= start && end <= size() + 1, return 0);
865 for (pos_type count = end - start; count; --count) {
866 if (!eraseChar(i, trackChanges))
872 // Handle combining characters
873 int Paragraph::Private::latexSurrogatePair(BufferParams const & bparams,
874 otexstream & os, char_type c, char_type next,
875 OutputParams const & runparams)
877 // Writing next here may circumvent a possible font change between
878 // c and next. Since next is only output if it forms a surrogate pair
879 // with c we can ignore this:
880 // A font change inside a surrogate pair does not make sense and is
881 // hopefully impossible to input.
882 // FIXME: change tracking
883 // Is this correct WRT change tracking?
884 Encoding const & encoding = *(runparams.encoding);
885 docstring latex1 = encoding.latexChar(next).first;
886 if (runparams.inIPA) {
887 string const tipashortcut = Encodings::TIPAShortcut(next);
888 if (!tipashortcut.empty()) {
889 latex1 = from_ascii(tipashortcut);
892 docstring latex2 = encoding.latexChar(c).first;
894 if (bparams.useNonTeXFonts || docstring(1, next) == latex1) {
895 // Encoding supports the combination:
896 // output as is (combining char after base char).
897 os << latex2 << latex1;
898 return latex1.length() + latex2.length();
901 os << latex1 << "{" << latex2 << "}";
902 return latex1.length() + latex2.length() + 2;
906 bool Paragraph::Private::simpleTeXBlanks(BufferParams const & bparams,
907 OutputParams const & runparams,
910 unsigned int & column,
912 Layout const & style)
914 if (style.pass_thru || runparams.pass_thru)
917 if (i + 1 < int(text_.size())) {
918 char_type next = text_[i + 1];
919 if (Encodings::isCombiningChar(next)) {
920 // This space has an accent, so we must always output it.
921 column += latexSurrogatePair(bparams, os, ' ', next, runparams) - 1;
926 if (runparams.linelen > 0
927 && column > runparams.linelen
929 && text_[i - 1] != ' '
930 && (i + 1 < int(text_.size()))
931 // same in FreeSpacing mode
932 && !owner_->isFreeSpacing()
933 // In typewriter mode, we want to avoid
934 // ! . ? : at the end of a line
935 && !(font.fontInfo().family() == TYPEWRITER_FAMILY
936 && (text_[i - 1] == '.'
937 || text_[i - 1] == '?'
938 || text_[i - 1] == ':'
939 || text_[i - 1] == '!'))) {
941 os.texrow().start(owner_->id(), i + 1);
943 } else if (style.free_spacing) {
952 void Paragraph::Private::latexInset(BufferParams const & bparams,
954 OutputParams & runparams,
957 Font const & outerfont,
959 Change & running_change,
960 Layout const & style,
962 unsigned int & column)
964 Inset * inset = owner_->getInset(i);
967 if (style.pass_thru) {
968 odocstringstream ods;
969 inset->plaintext(ods, runparams);
974 // FIXME: move this to InsetNewline::latex
975 if (inset->lyxCode() == NEWLINE_CODE || inset->lyxCode() == SEPARATOR_CODE) {
976 // newlines are handled differently here than
977 // the default in simpleTeXSpecialChars().
978 if (!style.newline_allowed) {
982 bool needPar = false;
983 column += running_font.latexWriteEndChanges(
984 os, bparams, runparams,
985 basefont, basefont, needPar);
989 if (running_font.fontInfo().family() == TYPEWRITER_FAMILY)
992 basefont = owner_->getLayoutFont(bparams, outerfont);
993 running_font = basefont;
995 if (runparams.moving_arg)
999 os.texrow().start(owner_->id(), i + 1);
1003 if (owner_->isDeleted(i)) {
1004 if( ++runparams.inDeletedInset == 1)
1005 runparams.changeOfDeletedInset = owner_->lookupChange(i);
1008 if (inset->canTrackChanges()) {
1009 column += Changes::latexMarkChange(os, bparams, running_change,
1010 Change(Change::UNCHANGED), runparams);
1011 running_change = Change(Change::UNCHANGED);
1015 odocstream::pos_type const len = os.os().tellp();
1017 if (inset->forceLTR(runparams)
1018 && running_font.isRightToLeft()
1019 // ERT is an exception, it should be output with no
1020 // decorations at all
1021 && inset->lyxCode() != ERT_CODE) {
1022 if (runparams.use_polyglossia) {
1024 } else if (running_font.language()->lang() == "farsi"
1025 || running_font.language()->lang() == "arabic_arabi")
1026 os << "\\textLR{" << termcmd;
1032 // FIXME: Bug: we can have an empty font change here!
1033 // if there has just been a font change, we are going to close it
1034 // right now, which means stupid latex code like \textsf{}. AFAIK,
1035 // this does not harm dvi output. A minor bug, thus (JMarc)
1037 // Some insets cannot be inside a font change command.
1038 // However, even such insets *can* be placed in \L or \R
1039 // or their equivalents (for RTL language switches), so we don't
1040 // close the language in those cases.
1041 // ArabTeX, though, cannot handle this special behavior, it seems.
1042 bool arabtex = basefont.language()->lang() == "arabic_arabtex"
1043 || running_font.language()->lang() == "arabic_arabtex";
1044 if (open_font && !inset->inheritFont()) {
1045 bool needPar = false;
1046 bool closeLanguage = arabtex
1047 || basefont.isRightToLeft() == running_font.isRightToLeft();
1048 // We pass non_inherit_inset = true here since size switches
1049 // ought not to be terminated here (#8384).
1050 unsigned int count = running_font.latexWriteEndChanges(os,
1051 bparams, runparams, basefont, basefont,
1052 needPar, closeLanguage, true);
1054 // if any font properties were closed, update the running_font,
1055 // making sure, however, to leave the language as it was
1057 // FIXME: probably a better way to keep track of the old
1058 // language, than copying the entire font?
1059 Font const copy_font(running_font);
1060 basefont = owner_->getLayoutFont(bparams, outerfont);
1061 running_font = basefont;
1063 running_font.setLanguage(copy_font.language());
1064 // For these, we use switches, so no need to close
1065 basefont.fontInfo().setSize(copy_font.fontInfo().size());
1066 basefont.fontInfo().setFamily(copy_font.fontInfo().family());
1067 basefont.fontInfo().setSeries(copy_font.fontInfo().series());
1068 // leave font open if language or any of the switches is still open
1069 open_font = (running_font.language() == basefont.language()
1070 || running_font.fontInfo().size() == basefont.fontInfo().size()
1071 || running_font.fontInfo().family() == basefont.fontInfo().family()
1072 || running_font.fontInfo().series() == basefont.fontInfo().series());
1074 runparams.local_font = &basefont;
1078 size_t const previous_row_count = os.texrow().rows();
1081 runparams.lastid = id_;
1082 runparams.lastpos = i;
1083 inset->latex(os, runparams);
1084 } catch (EncodingException & e) {
1085 // add location information and throw again.
1094 if (os.texrow().rows() > previous_row_count) {
1095 os.texrow().start(owner_->id(), i + 1);
1098 column += (unsigned int)(os.os().tellp() - len);
1101 if (owner_->isDeleted(i))
1102 --runparams.inDeletedInset;
1106 void Paragraph::Private::latexSpecialChar(otexstream & os,
1107 BufferParams const & bparams,
1108 OutputParams const & runparams,
1109 Font const & running_font,
1110 string & alien_script,
1111 Layout const & style,
1114 unsigned int & column)
1116 char_type const c = owner_->getUChar(bparams, runparams, i);
1118 if (style.pass_thru || runparams.pass_thru
1119 || contains(style.pass_thru_chars, c)
1120 || contains(runparams.pass_thru_chars, c)) {
1122 Encoding const * const enc = runparams.encoding;
1123 if (enc && !enc->encodable(c))
1124 throw EncodingException(c);
1130 // TIPA uses its own T3 encoding
1131 if (runparams.inIPA && latexSpecialT3(c, os, i, column))
1133 // If T1 font encoding is used, use the special
1134 // characters it provides.
1135 // NOTE: Some languages reset the font encoding internally to a
1136 // non-standard font encoding. If we are using such a language,
1137 // we do not output special T1 chars.
1138 if (!runparams.inIPA && !running_font.language()->internalFontEncoding()
1139 && !runparams.isFullUnicode() && bparams.main_font_encoding() == "T1"
1140 && latexSpecialT1(c, os, i, column))
1142 // NOTE: "fontspec" (non-TeX fonts) sets the font encoding to "TU" (untill 2017 "EU1" or "EU2")
1143 else if (!runparams.inIPA && !running_font.language()->internalFontEncoding()
1144 && runparams.isFullUnicode() && latexSpecialTU(c, os, i, column))
1147 // Otherwise, we use what LaTeX provides us.
1150 os << "\\textbackslash" << termcmd;
1154 os << "\\textless" << termcmd;
1158 os << "\\textgreater" << termcmd;
1162 os << "\\textbar" << termcmd;
1167 if (i + 1 < static_cast<pos_type>(text_.size()) &&
1168 (end_pos == -1 || i + 1 < end_pos) &&
1169 text_[i+1] == '-') {
1170 // Prevent "--" becoming an en dash and "---" an em dash.
1171 // (Within \ttfamily, "---" is merged to en dash + hyphen.)
1177 os << "\\textquotedbl" << termcmd;
1182 case '%': case '#': case '{':
1190 os << "\\textasciitilde" << termcmd;
1195 os << "\\textasciicircum" << termcmd;
1202 // avoid being mistaken for optional arguments
1210 // Blanks are printed before font switching.
1211 // Sure? I am not! (try nice-latex)
1212 // I am sure it's correct. LyX might be smarter
1213 // in the future, but for now, nothing wrong is
1219 // XeTeX's dash behaviour is determined via a global setting
1220 if (bparams.use_dash_ligatures
1221 && owner_->getFontSettings(bparams, i).fontInfo().family() != TYPEWRITER_FAMILY
1223 // TODO #10961: && not in inset Flex Code
1224 // TODO #10961: && not in layout LyXCode
1225 && (!bparams.useNonTeXFonts || runparams.flavor != OutputParams::XETEX)) {
1242 Encoding const & encoding = *(runparams.encoding);
1243 char_type next = '\0';
1244 if (i + 1 < int(text_.size())) {
1245 next = text_[i + 1];
1246 if (Encodings::isCombiningChar(next)) {
1247 column += latexSurrogatePair(bparams, os, c, next, runparams) - 1;
1252 pair<docstring, bool> latex = encoding.latexChar(c);
1253 docstring nextlatex;
1254 bool nexttipas = false;
1255 string nexttipashortcut;
1256 if (next != '\0' && next != META_INSET && !encoding.encodable(next)) {
1257 nextlatex = encoding.latexChar(next).first;
1258 if (runparams.inIPA) {
1259 nexttipashortcut = Encodings::TIPAShortcut(next);
1260 nexttipas = !nexttipashortcut.empty();
1264 if (runparams.inIPA) {
1265 string const tipashortcut = Encodings::TIPAShortcut(c);
1266 if (!tipashortcut.empty()) {
1267 latex.first = from_ascii(tipashortcut);
1268 latex.second = false;
1272 // eventually close "script wrapper" command (see `Paragraph::latex`)
1273 if (!alien_script.empty()
1274 && alien_script != Encodings::isKnownScriptChar(next)) {
1275 column += latex.first.length();
1276 alien_script.clear();
1277 os << latex.first << "}";
1281 && ((!prefixIs(nextlatex, '\\')
1282 && !prefixIs(nextlatex, '{')
1283 && !prefixIs(nextlatex, '}'))
1285 && !prefixIs(from_ascii(nexttipashortcut), '\\')))
1287 // Prevent eating of a following space or command corruption by
1288 // following characters
1289 if (next == ' ' || next == '\0') {
1290 column += latex.first.length() + 1;
1291 os << latex.first << "{}";
1293 column += latex.first.length();
1294 os << latex.first << " ";
1297 column += latex.first.length() - 1;
1305 bool Paragraph::Private::latexSpecialT1(char_type const c, otexstream & os,
1306 pos_type i, unsigned int & column)
1312 // In T1 encoding, these characters exist
1313 // but we should avoid ligatures
1314 if (i + 1 >= int(text_.size()) || text_[i + 1] != c)
1316 os << "\\textcompwordmark" << termcmd;
1323 // soul.sty breaks with \char`\"
1324 os << "\\textquotedbl" << termcmd;
1333 bool Paragraph::Private::latexSpecialTU(char_type const c, otexstream & os,
1334 pos_type i, unsigned int & column)
1336 // TU encoding is currently on par with T1.
1337 return latexSpecialT1(c, os, i, column);
1341 bool Paragraph::Private::latexSpecialT3(char_type const c, otexstream & os,
1342 pos_type /*i*/, unsigned int & column)
1352 os << "\\textvertline" << termcmd;
1361 void Paragraph::Private::validate(LaTeXFeatures & features) const
1363 if (layout_->inpreamble && inset_owner_) {
1364 // FIXME: Using a string stream here circumvents the encoding
1365 // switching machinery of odocstream. Therefore the
1366 // output is wrong if this paragraph contains content
1367 // that needs to switch encoding.
1368 Buffer const & buf = inset_owner_->buffer();
1369 otexstringstream os;
1370 os << layout_->preamble();
1371 size_t const length = os.length();
1372 TeXOnePar(buf, *inset_owner_->getText(int(buf.getParFromID(owner_->id()).idx())),
1373 buf.getParFromID(owner_->id()).pit(), os,
1374 features.runparams(), string(), 0, -1, true);
1375 if (os.length() > length)
1376 features.addPreambleSnippet(os.release(), true);
1379 if (features.runparams().flavor == OutputParams::HTML
1380 && layout_->htmltitle()) {
1381 features.setHTMLTitle(owner_->asString(AS_STR_INSETS | AS_STR_SKIPDELETE));
1384 // check the params.
1385 if (!params_.spacing().isDefault())
1386 features.require("setspace");
1389 features.useLayout(layout_->name());
1392 fontlist_.validate(features);
1394 // then the indentation
1395 if (!params_.leftIndent().zero())
1396 features.require("ParagraphLeftIndent");
1399 InsetList::const_iterator icit = insetlist_.begin();
1400 InsetList::const_iterator iend = insetlist_.end();
1401 for (; icit != iend; ++icit) {
1403 features.inDeletedInset(owner_->isDeleted(icit->pos));
1404 if (icit->inset->lyxCode() == FOOT_CODE) {
1405 // FIXME: an item inset would make things much easier.
1406 if ((layout_->latextype == LATEX_LIST_ENVIRONMENT
1407 || (layout_->latextype == LATEX_ITEM_ENVIRONMENT
1408 && layout_->margintype == MARGIN_FIRST_DYNAMIC))
1409 && (icit->pos < begin_of_body_
1410 || (icit->pos == begin_of_body_
1411 && (icit->pos == 0 || text_[icit->pos - 1] != ' '))))
1412 features.saveNoteEnv("description");
1414 icit->inset->validate(features);
1415 features.inDeletedInset(false);
1416 if (layout_->needprotect &&
1417 icit->inset->lyxCode() == FOOT_CODE)
1418 features.require("NeedLyXFootnoteCode");
1422 // then the contents
1423 BufferParams const bp = features.runparams().is_child
1424 ? features.buffer().masterParams() : features.buffer().params();
1425 for (pos_type i = 0; i < int(text_.size()) ; ++i) {
1426 char_type c = text_[i];
1427 CharInfo const & ci = Encodings::unicodeCharInfo(c);
1429 if (features.runparams().isFullUnicode() && bp.useNonTeXFonts)
1430 features.require("textquotedblp");
1431 else if (bp.main_font_encoding() != "T1"
1432 || ((&owner_->getFontSettings(bp, i))->language()->internalFontEncoding()))
1433 features.require("textquotedbl");
1434 } else if (ci.textfeature() && contains(ci.textpreamble(), '=')) {
1435 // features that depend on the font or input encoding
1436 string feats = ci.textpreamble();
1437 string fontenc = (&owner_->getFontSettings(bp, i))->language()->fontenc(bp);
1438 if (fontenc.empty())
1439 fontenc = features.runparams().main_fontenc;
1440 while (!feats.empty()) {
1442 feats = split(feats, feat, ',');
1443 if (contains(feat, "!=")) {
1444 // a feature that is required except for the spcified
1445 // font or input encodings
1447 string const contexts = ltrim(split(feat, realfeature, '!'), "=");
1448 // multiple encodings are separated by semicolon
1449 vector<string> context = getVectorFromString(contexts, ";");
1450 // require feature if the context matches neither current font
1451 // nor input encoding
1452 if (std::find(context.begin(), context.end(), fontenc) == context.end()
1453 && std::find(context.begin(), context.end(),
1454 features.runparams().encoding->name()) == context.end())
1455 features.require(realfeature);
1456 } else if (contains(feat, '=')) {
1457 // a feature that is required only for the spcified
1458 // font or input encodings
1460 string const contexts = split(feat, realfeature, '=');
1461 // multiple encodings are separated by semicolon
1462 vector<string> context = getVectorFromString(contexts, ";");
1463 // require feature if the context matches either current font
1464 // or input encoding
1465 if (std::find(context.begin(), context.end(), fontenc) != context.end()
1466 || std::find(context.begin(), context.end(),
1467 features.runparams().encoding->name()) != context.end())
1468 features.require(realfeature);
1471 } else if (!bp.use_dash_ligatures
1472 && (c == 0x2013 || c == 0x2014)
1473 && bp.useNonTeXFonts
1474 && features.runparams().flavor == OutputParams::XETEX)
1475 // XeTeX's dash behaviour is determined via a global setting
1476 features.require("xetexdashbreakstate");
1477 BufferEncodings::validate(c, features);
1481 /////////////////////////////////////////////////////////////////////
1485 /////////////////////////////////////////////////////////////////////
1488 Layout const emptyParagraphLayout;
1491 Paragraph::Paragraph()
1492 : d(new Paragraph::Private(this, emptyParagraphLayout))
1499 Paragraph::Paragraph(Paragraph const & par)
1500 : itemdepth(par.itemdepth),
1501 d(new Paragraph::Private(*par.d, this))
1507 Paragraph::Paragraph(Paragraph const & par, pos_type beg, pos_type end)
1508 : itemdepth(par.itemdepth),
1509 d(new Paragraph::Private(*par.d, this, beg, end))
1515 Paragraph & Paragraph::operator=(Paragraph const & par)
1517 // needed as we will destroy the private part before copying it
1519 itemdepth = par.itemdepth;
1523 d = new Private(*par.d, this);
1530 Paragraph::~Paragraph()
1539 // this shall be called just before every "os << ..." action.
1540 void flushString(ostream & os, docstring & s)
1549 void Paragraph::write(ostream & os, BufferParams const & bparams,
1550 depth_type & dth) const
1552 // The beginning or end of a deeper (i.e. nested) area?
1553 if (dth != d->params_.depth()) {
1554 if (d->params_.depth() > dth) {
1555 while (d->params_.depth() > dth) {
1556 os << "\n\\begin_deeper";
1560 while (d->params_.depth() < dth) {
1561 os << "\n\\end_deeper";
1567 // First write the layout
1568 os << "\n\\begin_layout " << to_utf8(d->layout_->name()) << '\n';
1570 d->params_.write(os);
1572 Font font1(inherit_font, bparams.language);
1574 Change running_change = Change(Change::UNCHANGED);
1576 // this string is used as a buffer to avoid repetitive calls
1577 // to to_utf8(), which turn out to be expensive (JMarc)
1578 docstring write_buffer;
1581 for (pos_type i = 0; i <= size(); ++i) {
1583 Change const & change = lookupChange(i);
1584 if (change != running_change)
1585 flushString(os, write_buffer);
1586 Changes::lyxMarkChange(os, bparams, column, running_change, change);
1587 running_change = change;
1592 // Write font changes
1593 Font font2 = getFontSettings(bparams, i);
1594 if (font2 != font1) {
1595 flushString(os, write_buffer);
1596 font2.lyxWriteChanges(font1, os);
1601 char_type const c = d->text_[i];
1604 if (Inset const * inset = getInset(i)) {
1605 flushString(os, write_buffer);
1606 if (inset->directWrite()) {
1607 // international char, let it write
1608 // code directly so it's shorter in
1614 os << "\\begin_inset ";
1616 os << "\n\\end_inset\n\n";
1619 // FIXME This can be removed again once the mystery
1620 // crash has been resolved.
1625 flushString(os, write_buffer);
1626 os << "\n\\backslash\n";
1630 flushString(os, write_buffer);
1631 if (i + 1 < size() && d->text_[i + 1] == ' ') {
1638 if ((column > 70 && c == ' ')
1640 flushString(os, write_buffer);
1644 // this check is to amend a bug. LyX sometimes
1645 // inserts '\0' this could cause problems.
1647 write_buffer.push_back(c);
1649 LYXERR0("NUL char in structure.");
1655 flushString(os, write_buffer);
1656 os << "\n\\end_layout\n";
1657 // FIXME This can be removed again once the mystery
1658 // crash has been resolved.
1663 void Paragraph::validate(LaTeXFeatures & features) const
1665 d->validate(features);
1666 bool fragile = features.runparams().moving_arg;
1667 fragile |= layout().needprotect;
1668 if (inInset().getLayout().isNeedProtect())
1670 if (needsCProtection(fragile))
1671 features.require("cprotect");
1675 void Paragraph::insert(pos_type start, docstring const & str,
1676 Font const & font, Change const & change)
1678 for (size_t i = 0, n = str.size(); i != n ; ++i)
1679 insertChar(start + i, str[i], font, change);
1683 void Paragraph::appendChar(char_type c, Font const & font,
1684 Change const & change)
1687 d->changes_.insert(change, d->text_.size());
1688 // when appending characters, no need to update tables
1689 d->text_.push_back(c);
1690 setFont(d->text_.size() - 1, font);
1691 d->requestSpellCheck(d->text_.size() - 1);
1695 void Paragraph::appendString(docstring const & s, Font const & font,
1696 Change const & change)
1698 pos_type end = s.size();
1699 size_t oldsize = d->text_.size();
1700 size_t newsize = oldsize + end;
1701 size_t capacity = d->text_.capacity();
1702 if (newsize >= capacity)
1703 d->text_.reserve(max(capacity + 100, newsize));
1705 // when appending characters, no need to update tables
1708 // FIXME: Optimize this!
1709 for (size_t i = oldsize; i != newsize; ++i) {
1711 d->changes_.insert(change, i);
1712 d->requestSpellCheck(i);
1714 d->fontlist_.set(oldsize, font);
1715 d->fontlist_.set(newsize - 1, font);
1719 void Paragraph::insertChar(pos_type pos, char_type c,
1722 d->insertChar(pos, c, Change(trackChanges ?
1723 Change::INSERTED : Change::UNCHANGED));
1727 void Paragraph::insertChar(pos_type pos, char_type c,
1728 Font const & font, bool trackChanges)
1730 d->insertChar(pos, c, Change(trackChanges ?
1731 Change::INSERTED : Change::UNCHANGED));
1736 void Paragraph::insertChar(pos_type pos, char_type c,
1737 Font const & font, Change const & change)
1739 d->insertChar(pos, c, change);
1744 void Paragraph::resetFonts(Font const & font)
1746 d->fontlist_.clear();
1747 d->fontlist_.set(0, font);
1748 d->fontlist_.set(d->text_.size() - 1, font);
1751 // Gets uninstantiated font setting at position.
1752 Font const & Paragraph::getFontSettings(BufferParams const & bparams,
1756 LYXERR0("pos: " << pos << " size: " << size());
1760 FontList::const_iterator cit = d->fontlist_.fontIterator(pos);
1761 if (cit != d->fontlist_.end())
1764 if (pos == size() && !empty())
1765 return getFontSettings(bparams, pos - 1);
1767 // Optimisation: avoid a full font instantiation if there is no
1768 // language change from previous call.
1769 static Font previous_font;
1770 static Language const * previous_lang = nullptr;
1771 Language const * lang = getParLanguage(bparams);
1772 if (lang != previous_lang) {
1773 previous_lang = lang;
1774 previous_font = Font(inherit_font, lang);
1776 return previous_font;
1780 FontSpan Paragraph::fontSpan(pos_type pos) const
1782 LBUFERR(pos <= size());
1785 return FontSpan(pos, pos);
1788 FontList::const_iterator cit = d->fontlist_.begin();
1789 FontList::const_iterator end = d->fontlist_.end();
1790 for (; cit != end; ++cit) {
1791 if (cit->pos() >= pos) {
1792 if (pos >= beginOfBody())
1793 return FontSpan(max(start, beginOfBody()),
1796 return FontSpan(start,
1797 min(beginOfBody() - 1,
1800 start = cit->pos() + 1;
1803 // This should not happen, but if so, we take no chances.
1804 LYXERR0("Paragraph::fontSpan: position not found in fontinfo table!");
1805 LASSERT(false, return FontSpan(pos, pos));
1809 // Gets uninstantiated font setting at position 0
1810 Font const & Paragraph::getFirstFontSettings(BufferParams const & bparams) const
1812 if (!empty() && !d->fontlist_.empty())
1813 return d->fontlist_.begin()->font();
1815 // Optimisation: avoid a full font instantiation if there is no
1816 // language change from previous call.
1817 static Font previous_font;
1818 static Language const * previous_lang = nullptr;
1819 if (bparams.language != previous_lang) {
1820 previous_lang = bparams.language;
1821 previous_font = Font(inherit_font, bparams.language);
1824 return previous_font;
1828 // Gets the fully instantiated font at a given position in a paragraph
1829 // This is basically the same function as Text::GetFont() in text2.cpp.
1830 // The difference is that this one is used for generating the LaTeX file,
1831 // and thus cosmetic "improvements" are disallowed: This has to deliver
1832 // the true picture of the buffer. (Asger)
1833 Font const Paragraph::getFont(BufferParams const & bparams, pos_type pos,
1834 Font const & outerfont) const
1838 Font font = getFontSettings(bparams, pos);
1840 pos_type const body_pos = beginOfBody();
1841 FontInfo & fi = font.fontInfo();
1843 fi.realize(d->layout_->labelfont);
1845 fi.realize(d->layout_->font);
1847 fi.realize(outerfont.fontInfo());
1848 fi.realize(bparams.getFont().fontInfo());
1854 Font const Paragraph::getLabelFont
1855 (BufferParams const & bparams, Font const & outerfont) const
1857 FontInfo tmpfont = d->layout_->labelfont;
1858 tmpfont.realize(outerfont.fontInfo());
1859 tmpfont.realize(bparams.getFont().fontInfo());
1860 return Font(tmpfont, getParLanguage(bparams));
1864 Font const Paragraph::getLayoutFont
1865 (BufferParams const & bparams, Font const & outerfont) const
1867 FontInfo tmpfont = d->layout_->font;
1868 tmpfont.realize(outerfont.fontInfo());
1869 tmpfont.realize(bparams.getFont().fontInfo());
1870 return Font(tmpfont, getParLanguage(bparams));
1874 char_type Paragraph::getUChar(BufferParams const & bparams,
1875 OutputParams const & rp,
1878 char_type c = d->text_[pos];
1880 // Return unchanged character in LTR languages
1881 // or if we use poylglossia/bidi (XeTeX).
1882 if (rp.useBidiPackage()
1883 || !getFontSettings(bparams, pos).isRightToLeft())
1886 // Without polyglossia/bidi, we need to account for some special cases.
1887 // FIXME This needs to be audited!
1889 // * The input is as expected for all delimiters
1890 // => checked for Hebrew!
1891 // * The output matches the display in the LyX workarea
1892 // => checked for Hebrew!
1893 // * The special cases below are really necessary
1894 // => checked for Hebrew!
1895 // * In arabic_arabi, brackets are transformed to Arabic
1896 // Ornate Parentheses. Is this is really wanted?
1898 string const & lang = getFontSettings(bparams, pos).language()->lang();
1901 // 1. In the following languages, parentheses need to be reversed.
1902 // Also with polyglodia/luabidi
1903 bool const reverseparens = (lang == "hebrew" || rp.use_polyglossia);
1905 // 2. In the following languages, brackets don't need to be reversed.
1906 bool const reversebrackets = lang != "arabic_arabtex"
1907 && lang != "arabic_arabi"
1910 // Now swap delimiters if needed.
1921 if (reversebrackets)
1925 if (reversebrackets)
1946 void Paragraph::setFont(pos_type pos, Font const & font)
1948 LASSERT(pos <= size(), return);
1950 // First, reduce font against layout/label font
1951 // Update: The setCharFont() routine in text2.cpp already
1952 // reduces font, so we don't need to do that here. (Asger)
1954 d->fontlist_.set(pos, font);
1958 void Paragraph::makeSameLayout(Paragraph const & par)
1960 d->layout_ = par.d->layout_;
1961 d->params_ = par.d->params_;
1965 bool Paragraph::stripLeadingSpaces(bool trackChanges)
1967 if (isFreeSpacing())
1973 while (pos < size() && (isNewline(pos) || isLineSeparator(pos))) {
1974 if (eraseChar(pos, trackChanges))
1980 return count > 0 || pos > 0;
1984 bool Paragraph::hasSameLayout(Paragraph const & par) const
1986 return par.d->layout_ == d->layout_
1987 && d->params_.sameLayout(par.d->params_);
1991 depth_type Paragraph::getDepth() const
1993 return d->params_.depth();
1997 depth_type Paragraph::getMaxDepthAfter() const
1999 if (d->layout_->isEnvironment())
2000 return d->params_.depth() + 1;
2002 return d->params_.depth();
2006 LyXAlignment Paragraph::getAlign(BufferParams const & bparams) const
2008 if (d->params_.align() == LYX_ALIGN_LAYOUT)
2009 return getDefaultAlign(bparams);
2011 return d->params_.align();
2015 LyXAlignment Paragraph::getDefaultAlign(BufferParams const & bparams) const
2017 LyXAlignment res = layout().align;
2018 if (isRTL(bparams)) {
2020 if (res == LYX_ALIGN_LEFT)
2021 res = LYX_ALIGN_RIGHT;
2022 else if (res == LYX_ALIGN_RIGHT)
2023 res = LYX_ALIGN_LEFT;
2029 docstring const & Paragraph::labelString() const
2031 return d->params_.labelString();
2035 // the next two functions are for the manual labels
2036 docstring const Paragraph::getLabelWidthString() const
2038 if (d->layout_->margintype == MARGIN_MANUAL
2039 || d->layout_->latextype == LATEX_BIB_ENVIRONMENT)
2040 return d->params_.labelWidthString();
2042 return _("Senseless with this layout!");
2046 void Paragraph::setLabelWidthString(docstring const & s)
2048 d->params_.labelWidthString(s);
2052 docstring Paragraph::expandLabel(Layout const & layout,
2053 BufferParams const & bparams) const
2055 return expandParagraphLabel(layout, bparams, true);
2059 docstring Paragraph::expandParagraphLabel(Layout const & layout,
2060 BufferParams const & bparams, bool process_appendix) const
2062 DocumentClass const & tclass = bparams.documentClass();
2063 string const & lang = getParLanguage(bparams)->code();
2064 bool const in_appendix = process_appendix && d->params_.appendix();
2065 docstring fmt = translateIfPossible(layout.labelstring(in_appendix), lang);
2067 if (fmt.empty() && !layout.counter.empty())
2068 return tclass.counters().theCounter(layout.counter, lang);
2070 // handle 'inherited level parts' in 'fmt',
2071 // i.e. the stuff between '@' in '@Section@.\arabic{subsection}'
2072 size_t const i = fmt.find('@', 0);
2073 if (i != docstring::npos) {
2074 size_t const j = fmt.find('@', i + 1);
2075 if (j != docstring::npos) {
2076 docstring parent(fmt, i + 1, j - i - 1);
2077 docstring label = from_ascii("??");
2078 if (tclass.hasLayout(parent))
2079 label = expandParagraphLabel(tclass[parent], bparams,
2081 fmt = docstring(fmt, 0, i) + label
2082 + docstring(fmt, j + 1, docstring::npos);
2086 return tclass.counters().counterLabel(fmt, lang);
2090 void Paragraph::applyLayout(Layout const & new_layout)
2092 d->layout_ = &new_layout;
2093 LyXAlignment const oldAlign = d->params_.align();
2095 if (!(oldAlign & d->layout_->alignpossible)) {
2096 frontend::Alert::warning(_("Alignment not permitted"),
2097 _("The new layout does not permit the alignment previously used.\nSetting to default."));
2098 d->params_.align(LYX_ALIGN_LAYOUT);
2103 pos_type Paragraph::beginOfBody() const
2105 return d->begin_of_body_;
2109 void Paragraph::setBeginOfBody()
2111 if (d->layout_->labeltype != LABEL_MANUAL) {
2112 d->begin_of_body_ = 0;
2116 // Unroll the first two cycles of the loop
2117 // and remember the previous character to
2118 // remove unnecessary getChar() calls
2120 pos_type end = size();
2121 bool prev_char_deleted = false;
2122 if (i < end && (!(isNewline(i) || isEnvSeparator(i)) || isDeleted(i))) {
2125 char_type previous_char = d->text_[i];
2126 if (!(isNewline(i) || isEnvSeparator(i))) {
2128 while (i < end && (previous_char != ' ' || prev_char_deleted)) {
2129 char_type temp = d->text_[i];
2130 prev_char_deleted = isDeleted(i);
2131 if (!isDeleted(i) && (isNewline(i) || isEnvSeparator(i)))
2134 previous_char = temp;
2140 d->begin_of_body_ = i;
2144 bool Paragraph::allowParagraphCustomization() const
2146 return inInset().allowParagraphCustomization();
2150 bool Paragraph::usePlainLayout() const
2152 return inInset().usePlainLayout();
2156 bool Paragraph::isPassThru() const
2158 return inInset().isPassThru() || d->layout_->pass_thru;
2163 // paragraphs inside floats need different alignment tags to avoid
2166 bool noTrivlistCentering(InsetCode code)
2168 return code == FLOAT_CODE
2169 || code == WRAP_CODE
2170 || code == CELL_CODE;
2174 string correction(string const & orig)
2176 if (orig == "flushleft")
2177 return "raggedright";
2178 if (orig == "flushright")
2179 return "raggedleft";
2180 if (orig == "center")
2186 bool corrected_env(otexstream & os, string const & suffix, string const & env,
2187 InsetCode code, bool const lastpar, int & col)
2189 string macro = suffix + "{";
2190 if (noTrivlistCentering(code)) {
2192 // the last paragraph in non-trivlist-aligned
2193 // context is special (to avoid unwanted whitespace)
2194 if (suffix == "\\begin") {
2195 macro = "\\" + correction(env) + "{}";
2196 os << from_ascii(macro);
2197 col += macro.size();
2202 macro += correction(env);
2206 if (suffix == "\\par\\end") {
2210 os << from_ascii(macro);
2211 col += macro.size();
2212 if (suffix == "\\begin") {
2222 int Paragraph::Private::startTeXParParams(BufferParams const & bparams,
2223 otexstream & os, OutputParams const & runparams) const
2228 (bparams.paragraph_separation == BufferParams::ParagraphIndentSeparation) ?
2229 (layout_->toggle_indent != ITOGGLE_NEVER) :
2230 (layout_->toggle_indent == ITOGGLE_ALWAYS);
2232 if (canindent && params_.noindent() && !layout_->pass_thru) {
2233 os << "\\noindent ";
2237 LyXAlignment const curAlign = params_.align();
2239 if (curAlign == layout_->align)
2243 case LYX_ALIGN_NONE:
2244 case LYX_ALIGN_BLOCK:
2245 case LYX_ALIGN_LAYOUT:
2246 case LYX_ALIGN_SPECIAL:
2247 case LYX_ALIGN_DECIMAL:
2249 case LYX_ALIGN_LEFT:
2250 case LYX_ALIGN_RIGHT:
2251 case LYX_ALIGN_CENTER:
2252 if (runparams.moving_arg) {
2259 string const begin_tag = "\\begin";
2260 InsetCode code = ownerCode();
2261 bool const lastpar = runparams.isLastPar;
2262 // RTL in classic (PDF)LaTeX (without the Bidi package)
2263 // Luabibdi (used by LuaTeX) behaves like classic
2264 bool const rtl_classic = owner_->getParLanguage(bparams)->rightToLeft()
2265 && !runparams.useBidiPackage();
2268 case LYX_ALIGN_NONE:
2269 case LYX_ALIGN_BLOCK:
2270 case LYX_ALIGN_LAYOUT:
2271 case LYX_ALIGN_SPECIAL:
2272 case LYX_ALIGN_DECIMAL:
2274 case LYX_ALIGN_LEFT: {
2276 // Classic (PDF)LaTeX switches the left/right logic in RTL mode
2277 corrected_env(os, begin_tag, "flushright", code, lastpar, column);
2279 corrected_env(os, begin_tag, "flushleft", code, lastpar, column);
2281 } case LYX_ALIGN_RIGHT: {
2283 // Classic (PDF)LaTeX switches the left/right logic in RTL mode
2284 corrected_env(os, begin_tag, "flushleft", code, lastpar, column);
2286 corrected_env(os, begin_tag, "flushright", code, lastpar, column);
2288 } case LYX_ALIGN_CENTER: {
2289 corrected_env(os, begin_tag, "center", code, lastpar, column);
2298 bool Paragraph::Private::endTeXParParams(BufferParams const & bparams,
2299 otexstream & os, OutputParams const & runparams) const
2301 LyXAlignment const curAlign = params_.align();
2303 if (curAlign == layout_->align)
2307 case LYX_ALIGN_NONE:
2308 case LYX_ALIGN_BLOCK:
2309 case LYX_ALIGN_LAYOUT:
2310 case LYX_ALIGN_SPECIAL:
2311 case LYX_ALIGN_DECIMAL:
2313 case LYX_ALIGN_LEFT:
2314 case LYX_ALIGN_RIGHT:
2315 case LYX_ALIGN_CENTER:
2316 if (runparams.moving_arg)
2321 bool output = false;
2323 string const end_tag = "\\par\\end";
2324 InsetCode code = ownerCode();
2325 bool const lastpar = runparams.isLastPar;
2326 // RTL in classic (PDF)LaTeX (without the Bidi package)
2327 // Luabibdi (used by LuaTeX) behaves like classic
2328 bool const rtl_classic = owner_->getParLanguage(bparams)->rightToLeft()
2329 && !runparams.useBidiPackage();
2332 case LYX_ALIGN_NONE:
2333 case LYX_ALIGN_BLOCK:
2334 case LYX_ALIGN_LAYOUT:
2335 case LYX_ALIGN_SPECIAL:
2336 case LYX_ALIGN_DECIMAL:
2338 case LYX_ALIGN_LEFT: {
2340 // Classic (PDF)LaTeX switches the left/right logic in RTL mode
2341 output = corrected_env(os, end_tag, "flushright", code, lastpar, col);
2343 output = corrected_env(os, end_tag, "flushleft", code, lastpar, col);
2345 } case LYX_ALIGN_RIGHT: {
2347 // Classic (PDF)LaTeX switches the left/right logic in RTL mode
2348 output = corrected_env(os, end_tag, "flushleft", code, lastpar, col);
2350 output = corrected_env(os, end_tag, "flushright", code, lastpar, col);
2352 } case LYX_ALIGN_CENTER: {
2353 corrected_env(os, end_tag, "center", code, lastpar, col);
2358 return output || lastpar;
2362 // This one spits out the text of the paragraph
2363 void Paragraph::latex(BufferParams const & bparams,
2364 Font const & outerfont,
2366 OutputParams const & runparams,
2367 int start_pos, int end_pos, bool force) const
2369 LYXERR(Debug::LATEX, "Paragraph::latex... " << this);
2371 // FIXME This check should not be needed. Perhaps issue an
2372 // error if it triggers.
2373 Layout const & style = inInset().forcePlainLayout() ?
2374 bparams.documentClass().plainLayout() : *d->layout_;
2376 if (!force && style.inpreamble)
2379 bool const allowcust = allowParagraphCustomization();
2381 // Current base font for all inherited font changes, without any
2382 // change caused by an individual character, except for the language:
2383 // It is set to the language of the first character.
2384 // As long as we are in the label, this font is the base font of the
2385 // label. Before the first body character it is set to the base font
2389 // If there is an open font-encoding changing command (script wrapper),
2390 // alien_script is set to its name
2391 string alien_script;
2394 // Maybe we have to create a optional argument.
2395 pos_type body_pos = beginOfBody();
2396 unsigned int column = 0;
2398 // If we are inside an inset, the real outerfont is local_font
2399 Font const real_outerfont = (runparams.local_font != nullptr)
2400 ? Font(runparams.local_font->fontInfo()) : outerfont;
2403 // the optional argument is kept in curly brackets in
2404 // case it contains a ']'
2405 // This is not strictly needed, but if this is changed it
2406 // would be a file format change, and tex2lyx would need
2407 // to be adjusted, since it unconditionally removes the
2408 // braces when it parses \item.
2411 basefont = getLabelFont(bparams, real_outerfont);
2413 basefont = getLayoutFont(bparams, real_outerfont);
2416 // Which font is currently active?
2417 Font running_font(basefont);
2418 // Do we have an open font change?
2419 bool open_font = false;
2421 Change runningChange = Change(Change::UNCHANGED);
2423 Encoding const * const prev_encoding = runparams.encoding;
2425 os.texrow().start(id(), 0);
2427 // if the paragraph is empty, the loop will not be entered at all
2429 // For InTitle commands, we have already opened a group
2430 // in output_latex::TeXOnePar.
2431 if (style.isCommand() && !style.intitle) {
2435 if (!style.leftdelim().empty()) {
2436 os << style.leftdelim();
2437 column += style.leftdelim().size();
2440 column += d->startTeXParParams(bparams, os, runparams);
2443 // Whether a \par can be issued for insets typeset inline with text.
2444 // Yes if greater than 0. This has to be static.
2445 THREAD_LOCAL_STATIC int parInline = 0;
2447 for (pos_type i = 0; i < size(); ++i) {
2448 // First char in paragraph or after label?
2449 if (i == body_pos) {
2452 bool needPar = false;
2453 column += running_font.latexWriteEndChanges(
2454 os, bparams, runparams,
2455 basefont, basefont, needPar);
2458 basefont = getLayoutFont(bparams, real_outerfont);
2459 running_font = basefont;
2461 column += Changes::latexMarkChange(os, bparams,
2462 runningChange, Change(Change::UNCHANGED),
2464 runningChange = Change(Change::UNCHANGED);
2469 // For InTitle commands, we have already opened a group
2470 // in output_latex::TeXOnePar.
2471 if (style.isCommand() && !style.intitle) {
2476 if (!style.leftdelim().empty()) {
2477 os << style.leftdelim();
2478 column += style.leftdelim().size();
2482 column += d->startTeXParParams(bparams, os,
2486 runparams.wasDisplayMath = runparams.inDisplayMath;
2487 runparams.inDisplayMath = false;
2488 bool deleted_display_math = false;
2489 Change const & change = runparams.inDeletedInset
2490 ? runparams.changeOfDeletedInset : lookupChange(i);
2492 char_type const c = d->text_[i];
2494 // Check whether a display math inset follows
2496 && i >= start_pos && (end_pos == -1 || i < end_pos)) {
2498 runparams.ctObject = getInset(i)->CtObject(runparams);
2500 InsetMath const * im = getInset(i)->asInsetMath();
2501 if (im && im->asHullInset()
2502 && im->asHullInset()->outerDisplay()) {
2503 runparams.inDisplayMath = true;
2504 // runparams.inDeletedInset will be set by
2505 // latexInset later, but we need this info
2506 // before it is called. On the other hand, we
2507 // cannot set it here because it is a counter.
2508 deleted_display_math = isDeleted(i);
2510 if (bparams.output_changes && deleted_display_math
2511 && runningChange == change
2512 && change.type == Change::DELETED
2513 && !os.afterParbreak()) {
2514 // A display math in the same paragraph follows.
2515 // We have to close and then reopen \lyxdeleted,
2516 // otherwise the math will be shifted up.
2517 OutputParams rp = runparams;
2519 bool needPar = false;
2520 column += running_font.latexWriteEndChanges(
2521 os, bparams, rp, basefont,
2525 basefont = (body_pos > i) ? getLabelFont(bparams, real_outerfont)
2526 : getLayoutFont(bparams, real_outerfont);
2527 running_font = basefont;
2528 column += Changes::latexMarkChange(os, bparams,
2529 Change(Change::INSERTED), change, rp);
2533 if (bparams.output_changes && runningChange != change) {
2534 if (!alien_script.empty()) {
2537 alien_script.clear();
2540 bool needPar = false;
2541 column += running_font.latexWriteEndChanges(
2542 os, bparams, runparams,
2543 basefont, basefont, needPar);
2546 basefont = (body_pos > i) ? getLabelFont(bparams, real_outerfont)
2547 : getLayoutFont(bparams, real_outerfont);
2548 running_font = basefont;
2549 column += Changes::latexMarkChange(os, bparams, runningChange,
2551 runningChange = change;
2554 // do not output text which is marked deleted
2555 // if change tracking output is disabled
2556 if (!bparams.output_changes && change.deleted()) {
2562 // Fully instantiated font
2563 Font const current_font = getFont(bparams, i, real_outerfont);
2565 Font const last_font = running_font;
2566 bool const in_ct_deletion = (bparams.output_changes
2567 && runningChange == change
2568 && change.type == Change::DELETED
2569 && !os.afterParbreak());
2571 // Do we need to close the previous font?
2573 (current_font != running_font ||
2574 current_font.language() != running_font.language()))
2576 // ensure there is no open script-wrapper
2577 if (!alien_script.empty()) {
2580 alien_script.clear();
2582 bool needPar = false;
2583 if (in_ct_deletion) {
2584 // We have to close and then reopen \lyxdeleted,
2585 // as strikeout needs to be on lowest level.
2589 column += running_font.latexWriteEndChanges(
2590 os, bparams, runparams, basefont,
2591 (i == body_pos-1) ? basefont : current_font,
2593 if (in_ct_deletion) {
2594 // We have to close and then reopen \lyxdeleted,
2595 // as strikeout needs to be on lowest level.
2596 OutputParams rp = runparams;
2597 column += Changes::latexMarkChange(os, bparams,
2598 Change(Change::UNCHANGED), Change(Change::DELETED), rp);
2600 running_font = basefont;
2604 // if necessary, close language environment before opening CJK
2605 string const running_lang = running_font.language()->babel();
2606 string const lang_end_command = lyxrc.language_command_end;
2607 if (!lang_end_command.empty() && !bparams.useNonTeXFonts
2608 && !running_lang.empty()
2609 && running_lang == openLanguageName()
2610 && current_font.language()->encoding()->package() == Encoding::CJK) {
2611 string end_tag = subst(lang_end_command, "$$lang", running_lang);
2612 os << from_ascii(end_tag);
2613 column += end_tag.length();
2617 // Switch file encoding if necessary (and allowed)
2618 if (!runparams.pass_thru && !style.pass_thru &&
2619 runparams.encoding->package() != Encoding::none &&
2620 current_font.language()->encoding()->package() != Encoding::none) {
2621 pair<bool, int> const enc_switch =
2622 switchEncoding(os.os(), bparams, runparams,
2623 *(current_font.language()->encoding()));
2624 if (enc_switch.first) {
2625 column += enc_switch.second;
2626 runparams.encoding = current_font.language()->encoding();
2630 // A display math inset inside an ulem command will be output
2631 // as a box of width \linewidth, so we have to either disable
2632 // indentation if the inset starts a paragraph, or start a new
2633 // line to accommodate such box. This has to be done before
2634 // writing any font changing commands.
2635 if (runparams.inDisplayMath && !deleted_display_math
2636 && runparams.inulemcmd) {
2637 if (os.afterParbreak())
2643 // Do we need to change font?
2644 if ((current_font != running_font ||
2645 current_font.language() != running_font.language())
2646 && i != body_pos - 1)
2648 if (in_ct_deletion) {
2649 // We have to close and then reopen \lyxdeleted,
2650 // as strikeout needs to be on lowest level.
2651 bool needPar = false;
2652 OutputParams rp = runparams;
2653 column += running_font.latexWriteEndChanges(
2654 os, bparams, rp, basefont,
2659 otexstringstream ots;
2660 bool const non_inherit_inset = (c == META_INSET && getInset(i) && !getInset(i)->inheritFont());
2661 column += current_font.latexWriteStartChanges(ots, bparams,
2662 runparams, basefont,
2663 last_font, non_inherit_inset);
2664 // Check again for display math in ulem commands as a
2665 // font change may also occur just before a math inset.
2666 if (runparams.inDisplayMath && !deleted_display_math
2667 && runparams.inulemcmd) {
2668 if (os.afterParbreak())
2673 running_font = current_font;
2675 docstring fontchange = ots.str();
2677 // check whether the fontchange ends with a \\textcolor
2678 // modifier and the text starts with a space. If so we
2679 // need to add } in order to prevent \\textcolor from gobbling
2680 // the space (bug 4473).
2681 docstring const last_modifier = rsplit(fontchange, '\\');
2682 if (prefixIs(last_modifier, from_ascii("textcolor")) && c == ' ')
2683 os << from_ascii("{}");
2684 else if (ots.terminateCommand())
2686 if (in_ct_deletion) {
2687 // We have to close and then reopen \lyxdeleted,
2688 // as strikeout needs to be on lowest level.
2689 OutputParams rp = runparams;
2690 column += Changes::latexMarkChange(os, bparams,
2691 Change(Change::UNCHANGED), change, rp);
2695 // FIXME: think about end_pos implementation...
2696 if (c == ' ' && i >= start_pos && (end_pos == -1 || i < end_pos)) {
2697 // FIXME: integrate this case in latexSpecialChar
2698 // Do not print the separation of the optional argument
2699 // if style.pass_thru is false. This works because
2700 // latexSpecialChar ignores spaces if
2701 // style.pass_thru is false.
2702 if (i != body_pos - 1) {
2703 if (d->simpleTeXBlanks(bparams, runparams, os,
2704 i, column, current_font, style)) {
2705 // A surrogate pair was output. We
2706 // must not call latexSpecialChar
2707 // in this iteration, since it would output
2708 // the combining character again.
2715 OutputParams rp = runparams;
2716 rp.free_spacing = style.free_spacing;
2717 rp.local_font = ¤t_font;
2718 rp.intitle = style.intitle;
2720 // Two major modes: LaTeX or plain
2721 // Handle here those cases common to both modes
2722 // and then split to handle the two modes separately.
2723 if (c == META_INSET) {
2724 if (i >= start_pos && (end_pos == -1 || i < end_pos)) {
2725 // Greyedout notes and, in general, all insets
2726 // with InsetLayout::isDisplay() == false,
2727 // are typeset inline with the text. So, we
2728 // can add a \par to the last paragraph of
2729 // such insets only if nothing else follows.
2730 bool incremented = false;
2731 Inset const * inset = getInset(i);
2732 InsetText const * textinset = inset
2733 ? inset->asInsetText()
2735 if (i + 1 == size() && textinset
2736 && !inset->getLayout().isDisplay()) {
2737 ParagraphList const & pars =
2738 textinset->text().paragraphs();
2739 pit_type const pit = pars.size() - 1;
2740 Font const lastfont =
2741 pit < 0 || pars[pit].empty()
2742 ? pars[pit].getLayoutFont(
2745 : pars[pit].getFont(bparams,
2746 pars[pit].size() - 1,
2748 if (lastfont.fontInfo().size() !=
2749 basefont.fontInfo().size()) {
2754 d->latexInset(bparams, os, rp, running_font,
2755 basefont, real_outerfont, open_font,
2756 runningChange, style, i, column);
2760 if (runparams.ctObject == OutputParams::CT_DISPLAYOBJECT
2761 || runparams.ctObject == OutputParams::CT_UDISPLAYOBJECT) {
2762 // Close \lyx*deleted and force its
2763 // reopening (if needed)
2766 runningChange = Change(Change::UNCHANGED);
2767 runparams.ctObject = OutputParams::CT_NORMAL;
2770 } else if (i >= start_pos && (end_pos == -1 || i < end_pos)) {
2771 if (!bparams.useNonTeXFonts)
2772 script = Encodings::isKnownScriptChar(c);
2773 if (script != alien_script) {
2774 if (!alien_script.empty()) {
2776 alien_script.clear();
2778 string fontenc = running_font.language()->fontenc(bparams);
2780 && !Encodings::fontencSupportsScript(fontenc, script)) {
2781 column += script.length() + 2;
2782 os << "\\" << script << "{";
2783 alien_script = script;
2787 d->latexSpecialChar(os, bparams, rp, running_font,
2788 alien_script, style, i, end_pos, column);
2789 } catch (EncodingException & e) {
2790 if (runparams.dryrun) {
2791 os << "<" << _("LyX Warning: ")
2792 << _("uncodable character") << " '";
2796 // add location information and throw again.
2804 // Set the encoding to that returned from latexSpecialChar (see
2805 // comment for encoding member in OutputParams.h)
2806 runparams.encoding = rp.encoding;
2808 // Also carry on the info on a closed ulem command for insets
2809 // such as Note that do not produce any output, so that no
2810 // command is ever executed but its opening was recorded.
2811 runparams.inulemcmd = rp.inulemcmd;
2813 // These need to be passed upstream as well
2814 runparams.need_maketitle = rp.need_maketitle;
2815 runparams.have_maketitle = rp.have_maketitle;
2817 // And finally, pass the post_macros upstream
2818 runparams.post_macro = rp.post_macro;
2821 // Close wrapper for alien script
2822 if (!alien_script.empty()) {
2824 alien_script.clear();
2827 // If we have an open font definition, we have to close it
2829 // Make sure that \\par is done with the font of the last
2830 // character if this has another size as the default.
2831 // This is necessary because LaTeX (and LyX on the screen)
2832 // calculates the space between the baselines according
2833 // to this font. (Matthias)
2835 // We must not change the font for the last paragraph
2836 // of non-multipar insets, tabular cells or commands,
2837 // since this produces unwanted whitespace.
2839 Font const font = empty()
2840 ? getLayoutFont(bparams, real_outerfont)
2841 : getFont(bparams, size() - 1, real_outerfont);
2843 InsetText const * textinset = inInset().asInsetText();
2845 bool const maintext = textinset
2846 ? textinset->text().isMainText()
2849 size_t const numpars = textinset
2850 ? textinset->text().paragraphs().size()
2853 bool needPar = false;
2855 if (style.resfont.size() != font.fontInfo().size()
2856 && (!runparams.isLastPar || maintext
2857 || (numpars > 1 && d->ownerCode() != CELL_CODE
2858 && (inInset().getLayout().isDisplay()
2860 && !style.isCommand()) {
2863 #ifdef FIXED_LANGUAGE_END_DETECTION
2865 running_font.latexWriteEndChanges(os, bparams,
2866 runparams, basefont,
2867 next_->getFont(bparams, 0, outerfont),
2870 running_font.latexWriteEndChanges(os, bparams,
2871 runparams, basefont, basefont, needPar);
2874 //FIXME: For now we ALWAYS have to close the foreign font settings if they are
2875 //FIXME: there as we start another \selectlanguage with the next paragraph if
2876 //FIXME: we are in need of this. This should be fixed sometime (Jug)
2877 running_font.latexWriteEndChanges(os, bparams, runparams,
2878 basefont, basefont, needPar);
2881 // The \par could not be inserted at the same nesting
2882 // level of the font size change, so do it now.
2883 os << "{\\" << font.latexSize() << "\\par}";
2887 column += Changes::latexMarkChange(os, bparams, runningChange,
2888 Change(Change::UNCHANGED), runparams);
2890 // Needed if there is an optional argument but no contents.
2891 if (body_pos > 0 && body_pos == size()) {
2895 if (!style.rightdelim().empty()) {
2896 os << style.rightdelim();
2897 column += style.rightdelim().size();
2900 if (allowcust && d->endTeXParParams(bparams, os, runparams)
2901 && runparams.encoding != prev_encoding) {
2902 runparams.encoding = prev_encoding;
2903 os << setEncoding(prev_encoding->iconvName());
2906 LYXERR(Debug::LATEX, "Paragraph::latex... done " << this);
2910 bool Paragraph::emptyTag() const
2912 for (pos_type i = 0; i < size(); ++i) {
2913 if (Inset const * inset = getInset(i)) {
2914 InsetCode lyx_code = inset->lyxCode();
2915 // FIXME testing like that is wrong. What is
2917 if (lyx_code != TOC_CODE &&
2918 lyx_code != INCLUDE_CODE &&
2919 lyx_code != GRAPHICS_CODE &&
2920 lyx_code != ERT_CODE &&
2921 lyx_code != LISTINGS_CODE &&
2922 lyx_code != FLOAT_CODE &&
2923 lyx_code != TABULAR_CODE) {
2927 char_type c = d->text_[i];
2928 if (c != ' ' && c != '\t')
2936 string Paragraph::getID(Buffer const &, OutputParams const &)
2939 for (pos_type i = 0; i < size(); ++i) {
2940 if (Inset const * inset = getInset(i)) {
2941 InsetCode lyx_code = inset->lyxCode();
2942 if (lyx_code == LABEL_CODE) {
2943 InsetLabel const * const il = static_cast<InsetLabel const *>(inset);
2944 docstring const & id = il->getParam("name");
2945 return "id='" + to_utf8(xml::cleanID(id)) + "'";
2953 pos_type Paragraph::firstWordDocBook(XMLStream & xs, OutputParams const & runparams) const
2956 for (i = 0; i < size(); ++i) {
2957 if (Inset const * inset = getInset(i)) {
2958 inset->docbook(xs, runparams);
2960 char_type c = d->text_[i];
2970 pos_type Paragraph::firstWordLyXHTML(XMLStream & xs, OutputParams const & runparams)
2974 for (i = 0; i < size(); ++i) {
2975 if (Inset const * inset = getInset(i)) {
2976 inset->xhtml(xs, runparams);
2978 char_type c = d->text_[i];
2988 bool Paragraph::Private::onlyText(Buffer const & buf, Font const & outerfont, pos_type initial) const
2991 pos_type size = text_.size();
2992 for (pos_type i = initial; i < size; ++i) {
2993 Font font = owner_->getFont(buf.params(), i, outerfont);
2994 if (text_[i] == META_INSET)
2996 if (i != initial && font != font_old)
3007 void doFontSwitchDocBook(vector<xml::FontTag> & tagsToOpen,
3008 vector<xml::EndFontTag> & tagsToClose,
3009 bool & flag, FontState curstate, xml::FontTypes type)
3011 if (curstate == FONT_ON) {
3012 tagsToOpen.push_back(docbookStartFontTag(type));
3015 tagsToClose.push_back(docbookEndFontTag(type));
3020 class OptionalFontType {
3025 OptionalFontType(): has_value(false), ft(xml::FT_EMPH) {} // A possible value at random for ft.
3026 OptionalFontType(xml::FontTypes ft): has_value(true), ft(ft) {}
3029 OptionalFontType fontShapeToXml(FontShape fs)
3033 return {xml::FT_ITALIC};
3035 return {xml::FT_SLANTED};
3036 case SMALLCAPS_SHAPE:
3037 return {xml::FT_SMALLCAPS};
3042 // the other tags are for internal use
3048 OptionalFontType fontFamilyToXml(FontFamily fm)
3052 return {xml::FT_ROMAN};
3055 return {xml::FT_SANS};
3057 case TYPEWRITER_FAMILY:
3058 return {xml::FT_TYPE};
3060 case INHERIT_FAMILY:
3063 // the other tags are for internal use
3069 OptionalFontType fontSizeToXml(FontSize fs)
3073 return {xml::FT_SIZE_TINY};
3075 return {xml::FT_SIZE_SCRIPT};
3077 return {xml::FT_SIZE_FOOTNOTE};
3079 return {xml::FT_SIZE_SMALL};
3081 return {xml::FT_SIZE_LARGE};
3083 return {xml::FT_SIZE_LARGER};
3085 return {xml::FT_SIZE_LARGEST};
3087 return {xml::FT_SIZE_HUGE};
3089 return {xml::FT_SIZE_HUGER};
3091 return {xml::FT_SIZE_INCREASE};
3093 return {xml::FT_SIZE_DECREASE};
3098 // the other tags are for internal use
3104 struct DocBookFontState
3106 // track whether we have opened these tags
3107 bool emph_flag = false;
3108 bool bold_flag = false;
3109 bool noun_flag = false;
3110 bool ubar_flag = false;
3111 bool dbar_flag = false;
3112 bool sout_flag = false;
3113 bool xout_flag = false;
3114 bool wave_flag = false;
3116 bool shap_flag = false;
3118 bool faml_flag = false;
3120 bool size_flag = false;
3122 FontShape curr_fs = INHERIT_SHAPE;
3123 FontFamily curr_fam = INHERIT_FAMILY;
3124 FontSize curr_size = INHERIT_SIZE;
3127 std::tuple<vector<xml::FontTag>, vector<xml::EndFontTag>> computeDocBookFontSwitch(FontInfo const & font_old,
3129 std::string const & default_family,
3130 DocBookFontState fs)
3132 vector<xml::FontTag> tagsToOpen;
3133 vector<xml::EndFontTag> tagsToClose;
3136 FontState curstate = font.fontInfo().emph();
3137 if (font_old.emph() != curstate)
3138 doFontSwitchDocBook(tagsToOpen, tagsToClose, fs.emph_flag, curstate, xml::FT_EMPH);
3141 curstate = font.fontInfo().noun();
3142 if (font_old.noun() != curstate)
3143 doFontSwitchDocBook(tagsToOpen, tagsToClose, fs.noun_flag, curstate, xml::FT_NOUN);
3146 curstate = font.fontInfo().underbar();
3147 if (font_old.underbar() != curstate)
3148 doFontSwitchDocBook(tagsToOpen, tagsToClose, fs.ubar_flag, curstate, xml::FT_UBAR);
3151 curstate = font.fontInfo().strikeout();
3152 if (font_old.strikeout() != curstate)
3153 doFontSwitchDocBook(tagsToOpen, tagsToClose, fs.sout_flag, curstate, xml::FT_SOUT);
3156 curstate = font.fontInfo().uuline();
3157 if (font_old.uuline() != curstate)
3158 doFontSwitchDocBook(tagsToOpen, tagsToClose, fs.dbar_flag, curstate, xml::FT_DBAR);
3161 curstate = font.fontInfo().uwave();
3162 if (font_old.uwave() != curstate)
3163 doFontSwitchDocBook(tagsToOpen, tagsToClose, fs.wave_flag, curstate, xml::FT_WAVE);
3166 // a little hackish, but allows us to reuse what we have.
3167 curstate = (font.fontInfo().series() == BOLD_SERIES ? FONT_ON : FONT_OFF);
3168 if (font_old.series() != font.fontInfo().series())
3169 doFontSwitchDocBook(tagsToOpen, tagsToClose, fs.bold_flag, curstate, xml::FT_BOLD);
3172 fs.curr_fs = font.fontInfo().shape();
3173 FontShape old_fs = font_old.shape();
3174 if (old_fs != fs.curr_fs) {
3176 OptionalFontType tag = fontShapeToXml(old_fs);
3177 if (tag.has_value) {
3178 tagsToClose.push_back(docbookEndFontTag(tag.ft));
3180 fs.shap_flag = false;
3183 OptionalFontType tag = fontShapeToXml(fs.curr_fs);
3184 if (tag.has_value) {
3185 tagsToOpen.push_back(docbookStartFontTag(tag.ft));
3190 fs.curr_fam = font.fontInfo().family();
3191 FontFamily old_fam = font_old.family();
3192 if (old_fam != fs.curr_fam) {
3194 OptionalFontType tag = fontFamilyToXml(old_fam);
3195 if (tag.has_value) {
3196 tagsToClose.push_back(docbookEndFontTag(tag.ft));
3198 fs.faml_flag = false;
3200 switch (fs.curr_fam) {
3202 // we will treat a "default" font family as roman, since we have
3203 // no other idea what to do.
3204 if (default_family != "rmdefault" && default_family != "default") {
3205 tagsToOpen.push_back(docbookStartFontTag(xml::FT_ROMAN));
3206 fs.faml_flag = true;
3210 if (default_family != "sfdefault") {
3211 tagsToOpen.push_back(docbookStartFontTag(xml::FT_SANS));
3212 fs.faml_flag = true;
3215 case TYPEWRITER_FAMILY:
3216 if (default_family != "ttdefault") {
3217 tagsToOpen.push_back(docbookStartFontTag(xml::FT_TYPE));
3218 fs.faml_flag = true;
3221 case INHERIT_FAMILY:
3224 // the other tags are for internal use
3231 fs.curr_size = font.fontInfo().size();
3232 FontSize old_size = font_old.size();
3233 if (old_size != fs.curr_size) {
3235 OptionalFontType tag = fontSizeToXml(old_size);
3236 if (tag.has_value) {
3237 tagsToClose.push_back(docbookEndFontTag(tag.ft));
3239 fs.size_flag = false;
3242 OptionalFontType tag = fontSizeToXml(fs.curr_size);
3243 if (tag.has_value) {
3244 tagsToOpen.push_back(docbookStartFontTag(tag.ft));
3245 fs.size_flag = true;
3249 return std::tuple<vector<xml::FontTag>, vector<xml::EndFontTag>>(tagsToOpen, tagsToClose);
3252 }// anonymous namespace
3255 void Paragraph::simpleDocBookOnePar(Buffer const & buf,
3257 OutputParams const & runparams,
3258 Font const & outerfont,
3259 bool start_paragraph, bool close_paragraph,
3260 pos_type initial) const
3262 // track whether we have opened these tags
3263 DocBookFontState fs;
3265 if (start_paragraph)
3266 xs.startDivision(allowEmpty());
3268 Layout const & style = *d->layout_;
3270 style.labeltype == LABEL_MANUAL ? style.labelfont : style.font;
3272 string const default_family =
3273 buf.masterBuffer()->params().fonts_default_family;
3275 vector<xml::FontTag> tagsToOpen;
3276 vector<xml::EndFontTag> tagsToClose;
3278 // parsing main loop
3279 for (pos_type i = initial; i < size(); ++i) {
3280 // let's not show deleted material in the output
3284 Font const font = getFont(buf.masterBuffer()->params(), i, outerfont);
3286 // Determine which tags should be opened or closed.
3287 tie(tagsToOpen, tagsToClose) = computeDocBookFontSwitch(font_old, font, default_family, fs);
3290 // Other such tags? What about the other text ranges?
3292 vector<xml::EndFontTag>::const_iterator cit = tagsToClose.begin();
3293 vector<xml::EndFontTag>::const_iterator cen = tagsToClose.end();
3294 for (; cit != cen; ++cit)
3297 vector<xml::FontTag>::const_iterator sit = tagsToOpen.begin();
3298 vector<xml::FontTag>::const_iterator sen = tagsToOpen.end();
3299 for (; sit != sen; ++sit)
3302 tagsToClose.clear();
3305 if (Inset const * inset = getInset(i)) {
3306 if (!runparams.for_toc || inset->isInToc()) {
3307 OutputParams np = runparams;
3308 np.local_font = &font;
3309 // If the paragraph has size 1, then we are in the "special
3310 // case" where we do not output the containing paragraph info.
3311 // This "special case" is defined in more details in output_docbook.cpp, makeParagraphs. The results
3312 // of that brittle logic is passed to this function through open_par.
3313 if (!inset->getLayout().htmlisblock() && size() != 1) // TODO: htmlisblock here too!
3314 np.docbook_in_par = true;
3315 inset->docbook(xs, np);
3318 char_type c = getUChar(buf.masterBuffer()->params(), runparams, i);
3321 font_old = font.fontInfo();
3324 // FIXME, this code is just imported from XHTML
3325 // I'm worried about what happens if a branch, say, is itself
3326 // wrapped in some font stuff. I think that will not work.
3328 if (runparams.docbook_in_listing)
3330 if (close_paragraph)
3337 void doFontSwitchXHTML(vector<xml::FontTag> & tagsToOpen,
3338 vector<xml::EndFontTag> & tagsToClose,
3339 bool & flag, FontState curstate, xml::FontTypes type)
3341 if (curstate == FONT_ON) {
3342 tagsToOpen.push_back(xhtmlStartFontTag(type));
3345 tagsToClose.push_back(xhtmlEndFontTag(type));
3350 } // anonymous namespace
3353 docstring Paragraph::simpleLyXHTMLOnePar(Buffer const & buf,
3355 OutputParams const & runparams,
3356 Font const & outerfont,
3357 bool start_paragraph, bool close_paragraph,
3358 pos_type initial) const
3362 // track whether we have opened these tags
3363 bool emph_flag = false;
3364 bool bold_flag = false;
3365 bool noun_flag = false;
3366 bool ubar_flag = false;
3367 bool dbar_flag = false;
3368 bool sout_flag = false;
3369 bool xout_flag = false;
3370 bool wave_flag = false;
3372 bool shap_flag = false;
3374 bool faml_flag = false;
3376 bool size_flag = false;
3378 Layout const & style = *d->layout_;
3380 if (start_paragraph)
3381 xs.startDivision(allowEmpty());
3384 style.labeltype == LABEL_MANUAL ? style.labelfont : style.font;
3386 FontShape curr_fs = INHERIT_SHAPE;
3387 FontFamily curr_fam = INHERIT_FAMILY;
3388 FontSize curr_size = INHERIT_SIZE;
3390 string const default_family =
3391 buf.masterBuffer()->params().fonts_default_family;
3393 vector<xml::FontTag> tagsToOpen;
3394 vector<xml::EndFontTag> tagsToClose;
3396 // parsing main loop
3397 for (pos_type i = initial; i < size(); ++i) {
3398 // let's not show deleted material in the output
3402 Font const font = getFont(buf.masterBuffer()->params(), i, outerfont);
3405 FontState curstate = font.fontInfo().emph();
3406 if (font_old.emph() != curstate)
3407 doFontSwitchXHTML(tagsToOpen, tagsToClose, emph_flag, curstate, xml::FT_EMPH);
3410 curstate = font.fontInfo().noun();
3411 if (font_old.noun() != curstate)
3412 doFontSwitchXHTML(tagsToOpen, tagsToClose, noun_flag, curstate, xml::FT_NOUN);
3415 curstate = font.fontInfo().underbar();
3416 if (font_old.underbar() != curstate)
3417 doFontSwitchXHTML(tagsToOpen, tagsToClose, ubar_flag, curstate, xml::FT_UBAR);
3420 curstate = font.fontInfo().strikeout();
3421 if (font_old.strikeout() != curstate)
3422 doFontSwitchXHTML(tagsToOpen, tagsToClose, sout_flag, curstate, xml::FT_SOUT);
3425 curstate = font.fontInfo().xout();
3426 if (font_old.xout() != curstate)
3427 doFontSwitchXHTML(tagsToOpen, tagsToClose, xout_flag, curstate, xml::FT_XOUT);
3430 curstate = font.fontInfo().uuline();
3431 if (font_old.uuline() != curstate)
3432 doFontSwitchXHTML(tagsToOpen, tagsToClose, dbar_flag, curstate, xml::FT_DBAR);
3435 curstate = font.fontInfo().uwave();
3436 if (font_old.uwave() != curstate)
3437 doFontSwitchXHTML(tagsToOpen, tagsToClose, wave_flag, curstate, xml::FT_WAVE);
3440 // a little hackish, but allows us to reuse what we have.
3441 curstate = (font.fontInfo().series() == BOLD_SERIES ? FONT_ON : FONT_OFF);
3442 if (font_old.series() != font.fontInfo().series())
3443 doFontSwitchXHTML(tagsToOpen, tagsToClose, bold_flag, curstate, xml::FT_BOLD);
3446 curr_fs = font.fontInfo().shape();
3447 FontShape old_fs = font_old.shape();
3448 if (old_fs != curr_fs) {
3452 tagsToClose.emplace_back(xhtmlEndFontTag(xml::FT_ITALIC));
3455 tagsToClose.emplace_back(xhtmlEndFontTag(xml::FT_SLANTED));
3457 case SMALLCAPS_SHAPE:
3458 tagsToClose.emplace_back(xhtmlEndFontTag(xml::FT_SMALLCAPS));
3464 // the other tags are for internal use
3472 tagsToOpen.emplace_back(xhtmlStartFontTag(xml::FT_ITALIC));
3476 tagsToOpen.emplace_back(xhtmlStartFontTag(xml::FT_SLANTED));
3479 case SMALLCAPS_SHAPE:
3480 tagsToOpen.emplace_back(xhtmlStartFontTag(xml::FT_SMALLCAPS));
3487 // the other tags are for internal use
3494 curr_fam = font.fontInfo().family();
3495 FontFamily old_fam = font_old.family();
3496 if (old_fam != curr_fam) {
3500 tagsToClose.emplace_back(xhtmlEndFontTag(xml::FT_ROMAN));
3503 tagsToClose.emplace_back(xhtmlEndFontTag(xml::FT_SANS));
3505 case TYPEWRITER_FAMILY:
3506 tagsToClose.emplace_back(xhtmlEndFontTag(xml::FT_TYPE));
3508 case INHERIT_FAMILY:
3511 // the other tags are for internal use
3519 // we will treat a "default" font family as roman, since we have
3520 // no other idea what to do.
3521 if (default_family != "rmdefault" && default_family != "default") {
3522 tagsToOpen.emplace_back(xhtmlStartFontTag(xml::FT_ROMAN));
3527 if (default_family != "sfdefault") {
3528 tagsToOpen.emplace_back(xhtmlStartFontTag(xml::FT_SANS));
3532 case TYPEWRITER_FAMILY:
3533 if (default_family != "ttdefault") {
3534 tagsToOpen.emplace_back(xhtmlStartFontTag(xml::FT_TYPE));
3538 case INHERIT_FAMILY:
3541 // the other tags are for internal use
3548 curr_size = font.fontInfo().size();
3549 FontSize old_size = font_old.size();
3550 if (old_size != curr_size) {
3554 tagsToClose.emplace_back(xhtmlEndFontTag(xml::FT_SIZE_TINY));
3557 tagsToClose.emplace_back(xhtmlEndFontTag(xml::FT_SIZE_SCRIPT));
3560 tagsToClose.emplace_back(xhtmlEndFontTag(xml::FT_SIZE_FOOTNOTE));
3563 tagsToClose.emplace_back(xhtmlEndFontTag(xml::FT_SIZE_SMALL));
3566 tagsToClose.emplace_back(xhtmlEndFontTag(xml::FT_SIZE_LARGE));
3569 tagsToClose.emplace_back(xhtmlEndFontTag(xml::FT_SIZE_LARGER));
3572 tagsToClose.emplace_back(xhtmlEndFontTag(xml::FT_SIZE_LARGEST));
3575 tagsToClose.emplace_back(xhtmlEndFontTag(xml::FT_SIZE_HUGE));
3578 tagsToClose.emplace_back(xhtmlEndFontTag(xml::FT_SIZE_HUGER));
3581 tagsToClose.emplace_back(xhtmlEndFontTag(xml::FT_SIZE_INCREASE));
3584 tagsToClose.emplace_back(xhtmlEndFontTag(xml::FT_SIZE_DECREASE));
3590 // the other tags are for internal use
3596 switch (curr_size) {
3598 tagsToOpen.emplace_back(xhtmlStartFontTag(xml::FT_SIZE_TINY));
3602 tagsToOpen.emplace_back(xhtmlStartFontTag(xml::FT_SIZE_SCRIPT));
3606 tagsToOpen.emplace_back(xhtmlStartFontTag(xml::FT_SIZE_FOOTNOTE));
3610 tagsToOpen.emplace_back(xhtmlStartFontTag(xml::FT_SIZE_SMALL));
3614 tagsToOpen.emplace_back(xhtmlStartFontTag(xml::FT_SIZE_LARGE));
3618 tagsToOpen.emplace_back(xhtmlStartFontTag(xml::FT_SIZE_LARGER));
3622 tagsToOpen.emplace_back(xhtmlStartFontTag(xml::FT_SIZE_LARGEST));
3626 tagsToOpen.emplace_back(xhtmlStartFontTag(xml::FT_SIZE_HUGE));
3630 tagsToOpen.emplace_back(xhtmlStartFontTag(xml::FT_SIZE_HUGER));
3634 tagsToOpen.emplace_back(xhtmlStartFontTag(xml::FT_SIZE_INCREASE));
3638 tagsToOpen.emplace_back(xhtmlStartFontTag(xml::FT_SIZE_DECREASE));
3645 // the other tags are for internal use
3652 // Other such tags? What about the other text ranges?
3654 vector<xml::EndFontTag>::const_iterator cit = tagsToClose.begin();
3655 vector<xml::EndFontTag>::const_iterator cen = tagsToClose.end();
3656 for (; cit != cen; ++cit)
3659 vector<xml::FontTag>::const_iterator sit = tagsToOpen.begin();
3660 vector<xml::FontTag>::const_iterator sen = tagsToOpen.end();
3661 for (; sit != sen; ++sit)
3664 tagsToClose.clear();
3667 Inset const * inset = getInset(i);
3669 if (!runparams.for_toc || inset->isInToc()) {
3670 OutputParams np = runparams;
3671 np.local_font = &font;
3672 // If the paragraph has size 1, then we are in the "special
3673 // case" where we do not output the containing paragraph info
3674 if (!inset->getLayout().htmlisblock() && size() != 1)
3675 np.html_in_par = true;
3676 retval += inset->xhtml(xs, np);
3679 char_type c = getUChar(buf.masterBuffer()->params(),
3681 if (c == ' ' && (style.free_spacing || runparams.free_spacing))
3682 xs << XMLStream::ESCAPE_NONE << " ";
3686 font_old = font.fontInfo();
3690 // I'm worried about what happens if a branch, say, is itself
3691 // wrapped in some font stuff. I think that will not work.
3693 if (close_paragraph)
3700 bool Paragraph::isHfill(pos_type pos) const
3702 Inset const * inset = getInset(pos);
3703 return inset && inset->isHfill();
3707 bool Paragraph::isNewline(pos_type pos) const
3709 // U+2028 LINE SEPARATOR
3710 // U+2029 PARAGRAPH SEPARATOR
3711 char_type const c = d->text_[pos];
3712 if (c == 0x2028 || c == 0x2029)
3714 Inset const * inset = getInset(pos);
3715 return inset && inset->lyxCode() == NEWLINE_CODE;
3719 bool Paragraph::isEnvSeparator(pos_type pos) const
3721 Inset const * inset = getInset(pos);
3722 return inset && inset->lyxCode() == SEPARATOR_CODE;
3726 bool Paragraph::isLineSeparator(pos_type pos) const
3728 char_type const c = d->text_[pos];
3729 if (isLineSeparatorChar(c))
3731 Inset const * inset = getInset(pos);
3732 return inset && inset->isLineSeparator();
3736 bool Paragraph::isWordSeparator(pos_type pos, bool const ignore_deleted) const
3740 if (ignore_deleted && isDeleted(pos))
3742 if (Inset const * inset = getInset(pos))
3743 return !inset->isLetter();
3744 // if we have a hard hyphen (no en- or emdash) or apostrophe
3745 // we pass this to the spell checker
3746 // FIXME: this method is subject to change, visit
3747 // https://bugzilla.mozilla.org/show_bug.cgi?id=355178
3748 // to get an impression how complex this is.
3749 if (isHardHyphenOrApostrophe(pos))
3751 char_type const c = d->text_[pos];
3752 // We want to pass the escape chars to the spellchecker
3753 docstring const escape_chars = from_utf8(lyxrc.spellchecker_esc_chars);
3754 return !isLetterChar(c) && !isDigitASCII(c) && !contains(escape_chars, c);
3758 bool Paragraph::isHardHyphenOrApostrophe(pos_type pos) const
3760 pos_type const psize = size();
3763 char_type const c = d->text_[pos];
3764 if (c != '-' && c != '\'')
3766 int nextpos = pos + 1;
3767 int prevpos = pos > 0 ? pos - 1 : 0;
3768 if ((nextpos == psize || isSpace(nextpos))
3769 && (pos == 0 || isSpace(prevpos)))
3775 bool Paragraph::needsCProtection(bool const fragile) const
3777 // first check the layout of the paragraph, but only in insets
3778 InsetText const * textinset = inInset().asInsetText();
3779 bool const maintext = textinset
3780 ? textinset->text().isMainText()
3783 if (!maintext && layout().needcprotect) {
3784 // Environments need cprotection regardless the content
3785 if (layout().latextype == LATEX_ENVIRONMENT)
3788 // Commands need cprotection if they contain specific chars
3789 int const nchars_escape = 9;
3790 static char_type const chars_escape[nchars_escape] = {
3791 '&', '_', '$', '%', '#', '^', '{', '}', '\\'};
3793 docstring const pars = asString();
3794 for (int k = 0; k < nchars_escape; k++) {
3795 if (contains(pars, chars_escape[k]))
3800 // now check whether we have insets that need cprotection
3801 pos_type size = pos_type(d->text_.size());
3802 for (pos_type i = 0; i < size; ++i) {
3805 Inset const * ins = getInset(i);
3806 if (ins->needsCProtection(maintext, fragile))
3808 if (ins->getLayout().latextype() == InsetLayout::ENVIRONMENT)
3809 // Environments need cprotection regardless the content
3811 // Now check math environments
3812 InsetMath const * im = getInset(i)->asInsetMath();
3813 if (!im || im->cell(0).empty())
3815 switch(im->cell(0)[0]->lyxCode()) {
3816 case MATH_AMSARRAY_CODE:
3817 case MATH_SUBSTACK_CODE:
3819 case MATH_XYMATRIX_CODE:
3820 // these need cprotection
3831 FontSpan const & Paragraph::getSpellRange(pos_type pos) const
3833 return d->speller_state_.getRange(pos);
3837 bool Paragraph::isChar(pos_type pos) const
3839 if (Inset const * inset = getInset(pos))
3840 return inset->isChar();
3841 char_type const c = d->text_[pos];
3842 return !isLetterChar(c) && !isDigitASCII(c) && !lyx::isSpace(c);
3846 bool Paragraph::isSpace(pos_type pos) const
3848 if (Inset const * inset = getInset(pos))
3849 return inset->isSpace();
3850 char_type const c = d->text_[pos];
3851 return lyx::isSpace(c);
3856 Paragraph::getParLanguage(BufferParams const & bparams) const
3859 return getFirstFontSettings(bparams).language();
3860 // FIXME: we should check the prev par as well (Lgb)
3861 return bparams.language;
3865 bool Paragraph::isRTL(BufferParams const & bparams) const
3867 return getParLanguage(bparams)->rightToLeft()
3868 && !inInset().getLayout().forceLTR();
3872 void Paragraph::changeLanguage(BufferParams const & bparams,
3873 Language const * from, Language const * to)
3875 // change language including dummy font change at the end
3876 for (pos_type i = 0; i <= size(); ++i) {
3877 Font font = getFontSettings(bparams, i);
3878 if (font.language() == from) {
3879 font.setLanguage(to);
3881 d->requestSpellCheck(i);
3887 bool Paragraph::isMultiLingual(BufferParams const & bparams) const
3889 Language const * doc_language = bparams.language;
3890 for (auto const & f : d->fontlist_)
3891 if (f.font().language() != ignore_language &&
3892 f.font().language() != latex_language &&
3893 f.font().language() != doc_language)
3899 void Paragraph::getLanguages(std::set<Language const *> & langs) const
3901 for (auto const & f : d->fontlist_) {
3902 Language const * lang = f.font().language();
3903 if (lang != ignore_language &&
3904 lang != latex_language)
3910 docstring Paragraph::asString(int options) const
3912 return asString(0, size(), options);
3916 docstring Paragraph::asString(pos_type beg, pos_type end, int options, const OutputParams *runparams) const
3918 odocstringstream os;
3921 && options & AS_STR_LABEL
3922 && !d->params_.labelString().empty())
3923 os << d->params_.labelString() << ' ';
3925 for (pos_type i = beg; i < end; ++i) {
3926 if ((options & AS_STR_SKIPDELETE) && isDeleted(i))
3928 char_type const c = d->text_[i];
3929 if (isPrintable(c) || c == '\t'
3930 || (c == '\n' && (options & AS_STR_NEWLINES)))
3932 else if (c == META_INSET && (options & AS_STR_INSETS)) {
3933 if (c == META_INSET && (options & AS_STR_PLAINTEXT)) {
3934 LASSERT(runparams != nullptr, return docstring());
3935 getInset(i)->plaintext(os, *runparams);
3937 getInset(i)->toString(os);
3946 void Paragraph::forOutliner(docstring & os, size_t const maxlen,
3947 bool const shorten, bool const label) const
3949 size_t tmplen = shorten ? maxlen + 1 : maxlen;
3950 if (label && !labelString().empty())
3951 os += labelString() + ' ';
3952 if (!layout().isTocCaption())
3954 for (pos_type i = 0; i < size() && os.length() < tmplen; ++i) {
3957 char_type const c = d->text_[i];
3960 else if (c == META_INSET)
3961 getInset(i)->forOutliner(os, tmplen, false);
3964 Text::shortenForOutliner(os, maxlen);
3968 void Paragraph::setInsetOwner(Inset const * inset)
3970 d->inset_owner_ = inset;
3974 int Paragraph::id() const
3980 void Paragraph::setId(int id)
3986 Layout const & Paragraph::layout() const
3992 void Paragraph::setLayout(Layout const & layout)
3994 d->layout_ = &layout;
3998 void Paragraph::setDefaultLayout(DocumentClass const & tc)
4000 setLayout(tc.defaultLayout());
4004 void Paragraph::setPlainLayout(DocumentClass const & tc)
4006 setLayout(tc.plainLayout());
4010 void Paragraph::setPlainOrDefaultLayout(DocumentClass const & tclass)
4012 if (usePlainLayout())
4013 setPlainLayout(tclass);
4015 setDefaultLayout(tclass);
4019 Inset const & Paragraph::inInset() const
4021 LBUFERR(d->inset_owner_);
4022 return *d->inset_owner_;
4026 ParagraphParameters & Paragraph::params()
4032 ParagraphParameters const & Paragraph::params() const
4038 bool Paragraph::isFreeSpacing() const
4040 if (d->layout_->free_spacing)
4042 return d->inset_owner_ && d->inset_owner_->isFreeSpacing();
4046 bool Paragraph::allowEmpty() const
4048 if (d->layout_->keepempty)
4050 return d->inset_owner_ && d->inset_owner_->allowEmpty();
4054 bool Paragraph::brokenBiblio() const
4056 // There is a problem if there is no bibitem at position 0 in
4057 // paragraphs that need one, if there is another bibitem in the
4058 // paragraph or if this paragraph is not supposed to have
4059 // a bibitem inset at all.
4060 return ((d->layout_->labeltype == LABEL_BIBLIO
4061 && (d->insetlist_.find(BIBITEM_CODE) != 0
4062 || d->insetlist_.find(BIBITEM_CODE, 1) > 0))
4063 || (d->layout_->labeltype != LABEL_BIBLIO
4064 && d->insetlist_.find(BIBITEM_CODE) != -1));
4068 int Paragraph::fixBiblio(Buffer const & buffer)
4070 // FIXME: when there was already an inset at 0, the return value is 1,
4071 // which does not tell whether another inset has been remove; the
4072 // cursor cannot be correctly updated.
4074 bool const track_changes = buffer.params().track_changes;
4075 int bibitem_pos = d->insetlist_.find(BIBITEM_CODE);
4077 // The case where paragraph is not BIBLIO
4078 if (d->layout_->labeltype != LABEL_BIBLIO) {
4079 if (bibitem_pos == -1)
4080 // No InsetBibitem => OK
4082 // There is an InsetBibitem: remove it!
4083 d->insetlist_.release(bibitem_pos);
4084 eraseChar(bibitem_pos, track_changes);
4085 return (bibitem_pos == 0) ? -1 : -bibitem_pos;
4088 bool const hasbibitem0 = bibitem_pos == 0;
4090 bibitem_pos = d->insetlist_.find(BIBITEM_CODE, 1);
4091 // There was an InsetBibitem at pos 0,
4092 // and no other one => OK
4093 if (bibitem_pos == -1)
4095 // there is a bibitem at the 0 position, but since
4096 // there is a second one, we copy the second on the
4097 // first. We're assuming there are at most two of
4098 // these, which there should be.
4099 // FIXME: why does it make sense to do that rather
4100 // than keep the first? (JMarc)
4101 Inset * inset = releaseInset(bibitem_pos);
4102 d->insetlist_.begin()->inset = inset;
4103 return -bibitem_pos;
4106 // We need to create an inset at the beginning
4107 Inset * inset = nullptr;
4108 if (bibitem_pos > 0) {
4109 // there was one somewhere in the paragraph, let's move it
4110 inset = d->insetlist_.release(bibitem_pos);
4111 eraseChar(bibitem_pos, track_changes);
4114 inset = new InsetBibitem(const_cast<Buffer *>(&buffer),
4115 InsetCommandParams(BIBITEM_CODE));
4117 Font font(inherit_font, buffer.params().language);
4118 insertInset(0, inset, font, Change(track_changes ? Change::INSERTED
4119 : Change::UNCHANGED));
4121 // This is needed to get the counters right
4122 buffer.updateBuffer();
4127 void Paragraph::checkAuthors(AuthorList const & authorList)
4129 d->changes_.checkAuthors(authorList);
4133 bool Paragraph::isChanged(pos_type pos) const
4135 return lookupChange(pos).changed();
4139 bool Paragraph::isInserted(pos_type pos) const
4141 return lookupChange(pos).inserted();
4145 bool Paragraph::isDeleted(pos_type pos) const
4147 return lookupChange(pos).deleted();
4151 InsetList const & Paragraph::insetList() const
4153 return d->insetlist_;
4157 void Paragraph::setInsetBuffers(Buffer & b)
4159 d->insetlist_.setBuffer(b);
4163 void Paragraph::resetBuffer()
4165 d->insetlist_.resetBuffer();
4169 Inset * Paragraph::releaseInset(pos_type pos)
4171 Inset * inset = d->insetlist_.release(pos);
4172 /// does not honour change tracking!
4173 eraseChar(pos, false);
4178 Inset * Paragraph::getInset(pos_type pos)
4180 return (pos < pos_type(d->text_.size()) && d->text_[pos] == META_INSET)
4181 ? d->insetlist_.get(pos) : nullptr;
4185 Inset const * Paragraph::getInset(pos_type pos) const
4187 return (pos < pos_type(d->text_.size()) && d->text_[pos] == META_INSET)
4188 ? d->insetlist_.get(pos) : nullptr;
4192 void Paragraph::changeCase(BufferParams const & bparams, pos_type pos,
4193 pos_type & right, TextCase action)
4195 // process sequences of modified characters; in change
4196 // tracking mode, this approach results in much better
4197 // usability than changing case on a char-by-char basis
4198 // We also need to track the current font, since font
4199 // changes within sequences can occur.
4200 vector<pair<char_type, Font> > changes;
4202 bool const trackChanges = bparams.track_changes;
4204 bool capitalize = true;
4206 for (; pos < right; ++pos) {
4207 char_type oldChar = d->text_[pos];
4208 char_type newChar = oldChar;
4210 // ignore insets and don't play with deleted text!
4211 if (oldChar != META_INSET && !isDeleted(pos)) {
4213 case text_lowercase:
4214 newChar = lowercase(oldChar);
4216 case text_capitalization:
4218 newChar = uppercase(oldChar);
4222 case text_uppercase:
4223 newChar = uppercase(oldChar);
4228 if (isWordSeparator(pos) || isDeleted(pos)) {
4229 // permit capitalization again
4233 if (oldChar != newChar) {
4234 changes.push_back(make_pair(newChar, getFontSettings(bparams, pos)));
4235 if (pos != right - 1)
4237 // step behind the changing area
4241 int erasePos = pos - changes.size();
4242 for (size_t i = 0; i < changes.size(); i++) {
4243 insertChar(pos, changes[i].first,
4246 if (!eraseChar(erasePos, trackChanges)) {
4249 ++right; // expand selection
4257 int Paragraph::find(docstring const & str, bool cs, bool mw,
4258 pos_type start_pos, bool del) const
4260 pos_type pos = start_pos;
4261 int const strsize = str.length();
4263 pos_type const parsize = d->text_.size();
4264 for (i = 0; i < strsize && pos < parsize; ++i, ++pos) {
4265 // Ignore "invisible" letters such as ligature breaks
4266 // and hyphenation chars while searching
4267 while (pos < parsize - 1 && isInset(pos)) {
4268 odocstringstream os;
4269 getInset(pos)->toString(os);
4270 if (!getInset(pos)->isLetter() || !os.str().empty())
4274 if (cs && str[i] != d->text_[pos])
4276 if (!cs && uppercase(str[i]) != uppercase(d->text_[pos]))
4278 if (!del && isDeleted(pos))
4285 // if necessary, check whether string matches word
4287 if (start_pos > 0 && !isWordSeparator(start_pos - 1))
4290 && !isWordSeparator(pos))
4294 return pos - start_pos;
4298 char_type Paragraph::getChar(pos_type pos) const
4300 return d->text_[pos];
4304 pos_type Paragraph::size() const
4306 return d->text_.size();
4310 bool Paragraph::empty() const
4312 return d->text_.empty();
4316 bool Paragraph::isInset(pos_type pos) const
4318 return d->text_[pos] == META_INSET;
4322 bool Paragraph::isSeparator(pos_type pos) const
4324 //FIXME: Are we sure this can be the only separator?
4325 return d->text_[pos] == ' ';
4329 void Paragraph::deregisterWords()
4331 Private::LangWordsMap::const_iterator itl = d->words_.begin();
4332 Private::LangWordsMap::const_iterator ite = d->words_.end();
4333 for (; itl != ite; ++itl) {
4334 WordList & wl = theWordList(itl->first);
4335 Private::Words::const_iterator it = (itl->second).begin();
4336 Private::Words::const_iterator et = (itl->second).end();
4337 for (; it != et; ++it)
4344 void Paragraph::locateWord(pos_type & from, pos_type & to,
4345 word_location const loc, bool const ignore_deleted) const
4348 case WHOLE_WORD_STRICT:
4349 if (from == 0 || from == size()
4350 || isWordSeparator(from, ignore_deleted)
4351 || isWordSeparator(from - 1, ignore_deleted)) {
4358 // If we are already at the beginning of a word, do nothing
4359 if (!from || isWordSeparator(from - 1, ignore_deleted))
4364 // always move the cursor to the beginning of previous word
4365 while (from && !isWordSeparator(from - 1, ignore_deleted))
4369 LYXERR0("Paragraph::locateWord: NEXT_WORD not implemented yet");
4372 // no need to move the 'from' cursor
4376 while (to < size() && !isWordSeparator(to, ignore_deleted))
4381 void Paragraph::collectWords()
4383 for (pos_type pos = 0; pos < size(); ++pos) {
4384 if (isWordSeparator(pos))
4386 pos_type from = pos;
4387 locateWord(from, pos, WHOLE_WORD);
4388 // Work around MSVC warning: The statement
4389 // if (pos < from + lyxrc.completion_minlength)
4390 // triggers a signed vs. unsigned warning.
4391 // I don't know why this happens, it could be a MSVC bug, or
4392 // related to LLP64 (windows) vs. LP64 (unix) programming
4393 // model, or the C++ standard might be ambigous in the section
4394 // defining the "usual arithmetic conversions". However, using
4395 // a temporary variable is safe and works on all compilers.
4396 pos_type const endpos = from + lyxrc.completion_minlength;
4399 FontList::const_iterator cit = d->fontlist_.fontIterator(from);
4400 if (cit == d->fontlist_.end())
4402 Language const * lang = cit->font().language();
4403 docstring const word = asString(from, pos, AS_STR_NONE);
4404 d->words_[lang->lang()].insert(word);
4409 void Paragraph::registerWords()
4411 Private::LangWordsMap::const_iterator itl = d->words_.begin();
4412 Private::LangWordsMap::const_iterator ite = d->words_.end();
4413 for (; itl != ite; ++itl) {
4414 WordList & wl = theWordList(itl->first);
4415 Private::Words::const_iterator it = (itl->second).begin();
4416 Private::Words::const_iterator et = (itl->second).end();
4417 for (; it != et; ++it)
4423 void Paragraph::updateWords()
4431 void Paragraph::Private::appendSkipPosition(SkipPositions & skips, pos_type const pos) const
4433 SkipPositionsIterator begin = skips.begin();
4434 SkipPositions::iterator end = skips.end();
4435 if (pos > 0 && begin < end) {
4437 if (end->last == pos - 1) {
4442 skips.insert(end, FontSpan(pos, pos));
4446 Language * Paragraph::Private::locateSpellRange(
4447 pos_type & from, pos_type & to,
4448 SkipPositions & skips) const
4450 // skip leading white space
4451 while (from < to && owner_->isWordSeparator(from))
4453 // don't check empty range
4456 // get current language
4457 Language * lang = getSpellLanguage(from);
4458 pos_type last = from;
4459 bool samelang = true;
4460 bool sameinset = true;
4461 while (last < to && samelang && sameinset) {
4462 // hop to end of word
4463 while (last < to && !owner_->isWordSeparator(last)) {
4464 if (owner_->getInset(last)) {
4465 appendSkipPosition(skips, last);
4466 } else if (owner_->isDeleted(last)) {
4467 appendSkipPosition(skips, last);
4471 // hop to next word while checking for insets
4472 while (sameinset && last < to && owner_->isWordSeparator(last)) {
4473 if (Inset const * inset = owner_->getInset(last))
4474 sameinset = inset->isChar() && inset->isLetter();
4475 if (sameinset && owner_->isDeleted(last)) {
4476 appendSkipPosition(skips, last);
4481 if (sameinset && last < to) {
4482 // now check for language change
4483 samelang = lang == getSpellLanguage(last);
4486 // if language change detected backstep is needed
4494 Language * Paragraph::Private::getSpellLanguage(pos_type const from) const
4497 const_cast<Language *>(owner_->getFontSettings(
4498 inset_owner_->buffer().params(), from).language());
4499 if (lang == inset_owner_->buffer().params().language
4500 && !lyxrc.spellchecker_alt_lang.empty()) {
4502 string const lang_variety =
4503 split(lyxrc.spellchecker_alt_lang, lang_code, '-');
4504 lang->setCode(lang_code);
4505 lang->setVariety(lang_variety);
4511 void Paragraph::requestSpellCheck(pos_type pos)
4513 d->requestSpellCheck(pos);
4517 bool Paragraph::needsSpellCheck() const
4519 SpellChecker::ChangeNumber speller_change_number = 0;
4520 if (theSpellChecker())
4521 speller_change_number = theSpellChecker()->changeNumber();
4522 if (speller_change_number > d->speller_state_.currentChangeNumber()) {
4523 d->speller_state_.needsCompleteRefresh(speller_change_number);
4525 return d->needsSpellCheck();
4529 bool Paragraph::Private::ignoreWord(docstring const & word) const
4531 // Ignore words with digits
4532 // FIXME: make this customizable
4533 // (note that some checkers ignore words with digits by default)
4534 docstring::const_iterator cit = word.begin();
4535 docstring::const_iterator const end = word.end();
4536 for (; cit != end; ++cit) {
4537 if (isNumber((*cit)))
4544 SpellChecker::Result Paragraph::spellCheck(pos_type & from, pos_type & to,
4545 WordLangTuple & wl, docstring_list & suggestions,
4546 bool do_suggestion, bool check_learned) const
4548 SpellChecker::Result result = SpellChecker::WORD_OK;
4549 SpellChecker * speller = theSpellChecker();
4553 if (!d->layout_->spellcheck || !inInset().allowSpellCheck())
4556 locateWord(from, to, WHOLE_WORD, true);
4557 if (from == to || from >= size())
4560 docstring word = asString(from, to, AS_STR_INSETS | AS_STR_SKIPDELETE);
4561 Language * lang = d->getSpellLanguage(from);
4563 if (getFontSettings(d->inset_owner_->buffer().params(), from).fontInfo().nospellcheck() == FONT_ON)
4566 wl = WordLangTuple(word, lang);
4571 if (needsSpellCheck() || check_learned) {
4573 if (!d->ignoreWord(word)) {
4574 bool const trailing_dot = to < size() && d->text_[to] == '.';
4575 result = speller->check(wl);
4576 if (SpellChecker::misspelled(result) && trailing_dot) {
4577 wl = WordLangTuple(word.append(from_ascii(".")), lang);
4578 result = speller->check(wl);
4579 if (!SpellChecker::misspelled(result)) {
4580 LYXERR(Debug::GUI, "misspelled word is correct with dot: \"" <<
4582 from << ".." << to << "]");
4584 // spell check with dot appended failed too
4585 // restore original word/lang value
4586 word = asString(from, to, AS_STR_INSETS | AS_STR_SKIPDELETE);
4587 wl = WordLangTuple(word, lang);
4591 if (!SpellChecker::misspelled(result)) {
4592 // area up to the begin of the next word is not misspelled
4593 while (end < size() && isWordSeparator(end))
4596 d->setMisspelled(from, end, result);
4598 result = d->speller_state_.getState(from);
4602 suggestions.clear();
4604 if (SpellChecker::misspelled(result)) {
4605 LYXERR(Debug::GUI, "misspelled word: \"" <<
4607 from << ".." << to << "]");
4609 speller->suggest(wl, suggestions);
4615 void Paragraph::anonymize()
4617 // This is a very crude anonymization for now
4618 for (char_type & c : d->text_)
4619 if (isLetterChar(c) || isNumber(c))
4624 void Paragraph::Private::markMisspelledWords(
4625 pos_type const & first, pos_type const & last,
4626 SpellChecker::Result result,
4627 docstring const & word,
4628 SkipPositions const & skips)
4630 if (!SpellChecker::misspelled(result)) {
4631 setMisspelled(first, last, SpellChecker::WORD_OK);
4635 SpellChecker * speller = theSpellChecker();
4636 // locate and enumerate the error positions
4637 int nerrors = speller->numMisspelledWords();
4639 SkipPositionsIterator it = skips.begin();
4640 SkipPositionsIterator et = skips.end();
4641 for (int index = 0; index < nerrors; ++index) {
4644 speller->misspelledWord(index, wstart, wlen);
4645 /// should not happen if speller supports range checks
4646 if (!wlen) continue;
4647 docstring const misspelled = word.substr(wstart, wlen);
4648 wstart += first + numskipped;
4649 if (snext < wstart) {
4650 /// mark the range of correct spelling
4651 numskipped += countSkips(it, et, wstart);
4652 setMisspelled(snext,
4653 wstart - 1, SpellChecker::WORD_OK);
4655 snext = wstart + wlen;
4656 numskipped += countSkips(it, et, snext);
4657 /// mark the range of misspelling
4658 setMisspelled(wstart, snext, result);
4659 LYXERR(Debug::GUI, "misspelled word: \"" <<
4660 misspelled << "\" [" <<
4661 wstart << ".." << (snext-1) << "]");
4664 if (snext <= last) {
4665 /// mark the range of correct spelling at end
4666 setMisspelled(snext, last, SpellChecker::WORD_OK);
4671 void Paragraph::spellCheck() const
4673 SpellChecker * speller = theSpellChecker();
4674 if (!speller || empty() ||!needsSpellCheck())
4678 d->rangeOfSpellCheck(start, endpos);
4679 if (speller->canCheckParagraph()) {
4680 // loop until we leave the range
4681 for (pos_type first = start; first < endpos; ) {
4682 pos_type last = endpos;
4683 Private::SkipPositions skips;
4684 Language * lang = d->locateSpellRange(first, last, skips);
4685 if (first >= endpos)
4687 // start the spell checker on the unit of meaning
4688 docstring word = asString(first, last, AS_STR_INSETS + AS_STR_SKIPDELETE);
4689 WordLangTuple wl = WordLangTuple(word, lang);
4690 SpellChecker::Result result = word.size() ?
4691 speller->check(wl) : SpellChecker::WORD_OK;
4692 d->markMisspelledWords(first, last, result, word, skips);
4696 static docstring_list suggestions;
4697 pos_type to = endpos;
4698 while (start < endpos) {
4700 spellCheck(start, to, wl, suggestions, false);
4704 d->readySpellCheck();
4708 bool Paragraph::isMisspelled(pos_type pos, bool check_boundary) const
4710 bool result = SpellChecker::misspelled(d->speller_state_.getState(pos));
4711 if (result || pos <= 0 || pos > size())
4713 if (check_boundary && (pos == size() || isWordSeparator(pos)))
4714 result = SpellChecker::misspelled(d->speller_state_.getState(pos - 1));
4719 string Paragraph::magicLabel() const
4722 ss << "magicparlabel-" << id();