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 "ParagraphParameters.h"
41 #include "SpellChecker.h"
43 #include "TextClass.h"
46 #include "WordLangTuple.h"
49 #include "frontends/alert.h"
51 #include "insets/InsetBibitem.h"
52 #include "insets/InsetLabel.h"
53 #include "insets/InsetSpecialChar.h"
55 #include "support/debug.h"
56 #include "support/docstring_list.h"
57 #include "support/ExceptionMessage.h"
58 #include "support/gettext.h"
59 #include "support/lassert.h"
60 #include "support/lstrings.h"
61 #include "support/textutils.h"
67 using namespace lyx::support;
73 /// Inset identifier (above 0x10ffff, for ucs-4)
74 char_type const META_INSET = 0x200001;
79 /////////////////////////////////////////////////////////////////////
83 /////////////////////////////////////////////////////////////////////
85 class SpellResultRange {
87 SpellResultRange(FontSpan range, SpellChecker::Result result)
88 : range_(range), result_(result)
91 FontSpan const & range() const { return range_; }
93 void range(FontSpan const & r) { range_ = r; }
95 SpellChecker::Result result() const { return result_; }
97 void result(SpellChecker::Result r) { result_ = r; }
99 bool contains(pos_type pos) const { return range_.contains(pos); }
101 bool covered(FontSpan const & r) const
103 // 1. first of new range inside current range or
104 // 2. last of new range inside current range or
105 // 3. first of current range inside new range or
106 // 4. last of current range inside new range
107 //FIXME: is this the same as !range_.intersect(r).empty() ?
108 return range_.contains(r.first) || range_.contains(r.last) ||
109 r.contains(range_.first) || r.contains(range_.last);
112 void shift(pos_type pos, int offset)
114 if (range_.first > pos) {
115 range_.first += offset;
116 range_.last += offset;
117 } else if (range_.last >= pos) {
118 range_.last += offset;
123 SpellChecker::Result result_ ;
127 /////////////////////////////////////////////////////////////////////
131 /////////////////////////////////////////////////////////////////////
133 class SpellCheckerState {
137 needs_refresh_ = true;
138 current_change_number_ = 0;
141 void setRange(FontSpan const & fp, SpellChecker::Result state)
144 RangesIterator et = ranges_.end();
145 RangesIterator it = ranges_.begin();
146 for (; it != et; ++it) {
147 if (!it->covered(fp))
148 result.push_back(SpellResultRange(it->range(), it->result()));
149 else if (state == SpellChecker::WORD_OK) {
150 // trim or split the current misspelled range
151 // store misspelled ranges only
152 FontSpan range = it->range();
153 if (fp.first > range.first) {
154 // misspelled area in front of WORD_OK
155 range.last = fp.first - 1;
156 result.push_back(SpellResultRange(range, it->result()));
159 if (fp.last < range.last) {
160 // misspelled area after WORD_OK range
161 range.first = fp.last + 1;
162 result.push_back(SpellResultRange(range, it->result()));
167 if (state != SpellChecker::WORD_OK)
168 ranges_.push_back(SpellResultRange(fp, state));
171 void increasePosAfterPos(pos_type pos)
173 correctRangesAfterPos(pos, 1);
177 void decreasePosAfterPos(pos_type pos)
179 correctRangesAfterPos(pos, -1);
183 void refreshLast(pos_type pos)
185 if (pos < refresh_.last)
189 SpellChecker::Result getState(pos_type pos) const
191 SpellChecker::Result result = SpellChecker::WORD_OK;
192 RangesIterator et = ranges_.end();
193 RangesIterator it = ranges_.begin();
194 for (; it != et; ++it) {
195 if(it->contains(pos)) {
202 FontSpan const & getRange(pos_type pos) const
204 /// empty span to indicate mismatch
205 static FontSpan empty_;
206 RangesIterator et = ranges_.end();
207 RangesIterator it = ranges_.begin();
208 for (; it != et; ++it) {
209 if(it->contains(pos)) {
216 bool needsRefresh() const
218 return needs_refresh_;
221 SpellChecker::ChangeNumber currentChangeNumber() const
223 return current_change_number_;
226 void refreshRange(pos_type & first, pos_type & last) const
228 first = refresh_.first;
229 last = refresh_.last;
232 void needsRefresh(pos_type pos)
234 if (needs_refresh_ && pos != -1) {
235 if (pos < refresh_.first)
236 refresh_.first = pos;
237 if (pos > refresh_.last)
239 } else if (pos != -1) {
240 // init request check for neighbour positions too
241 refresh_.first = pos > 0 ? pos - 1 : 0;
242 // no need for special end of paragraph check
243 refresh_.last = pos + 1;
245 needs_refresh_ = pos != -1;
248 void needsCompleteRefresh(SpellChecker::ChangeNumber change_number)
250 needs_refresh_ = true;
253 current_change_number_ = change_number;
256 typedef vector<SpellResultRange> Ranges;
257 typedef Ranges::const_iterator RangesIterator;
259 /// the area of the paragraph with pending spell check
262 /// spell state cache version number
263 SpellChecker::ChangeNumber current_change_number_;
266 void correctRangesAfterPos(pos_type pos, int offset)
268 RangesIterator et = ranges_.end();
269 Ranges::iterator it = ranges_.begin();
270 for (; it != et; ++it) {
271 it->shift(pos, offset);
277 /////////////////////////////////////////////////////////////////////
279 // Paragraph::Private
281 /////////////////////////////////////////////////////////////////////
283 class Paragraph::Private
285 // Enforce our own "copy" constructor by declaring the standard one and
286 // the assignment operator private without implementing them.
287 Private(Private const &);
288 Private & operator=(Private const &);
291 Private(Paragraph * owner, Layout const & layout);
292 /// "Copy constructor"
293 Private(Private const &, Paragraph * owner);
294 /// Copy constructor from \p beg to \p end
295 Private(Private const &, Paragraph * owner, pos_type beg, pos_type end);
298 void insertChar(pos_type pos, char_type c, Change const & change);
300 /// Output the surrogate pair formed by \p c and \p next to \p os.
301 /// \return the number of characters written.
302 int latexSurrogatePair(otexstream & os, char_type c, char_type next,
303 OutputParams const &);
305 /// Output a space in appropriate formatting (or a surrogate pair
306 /// if the next character is a combining character).
307 /// \return whether a surrogate pair was output.
308 bool simpleTeXBlanks(OutputParams const &,
311 unsigned int & column,
313 Layout const & style);
315 /// Output consecutive unicode chars, belonging to the same script as
316 /// specified by the latex macro \p ltx, to \p os starting from \p i.
317 /// \return the number of characters written.
318 int writeScriptChars(otexstream & os, docstring const & ltx,
319 Change const &, Encoding const &, pos_type & i);
321 /// This could go to ParagraphParameters if we want to.
322 int startTeXParParams(BufferParams const &, otexstream &,
323 OutputParams const &) const;
325 /// This could go to ParagraphParameters if we want to.
326 bool endTeXParParams(BufferParams const &, otexstream &,
327 OutputParams const &) const;
330 void latexInset(BufferParams const &,
335 Font const & outerfont,
337 Change & running_change,
338 Layout const & style,
340 unsigned int & column);
343 void latexSpecialChar(
345 BufferParams const & bparams,
346 OutputParams const & runparams,
347 Font const & running_font,
348 Change const & running_change,
349 Layout const & style,
352 unsigned int & column);
359 unsigned int & column);
365 unsigned int & column);
368 void validate(LaTeXFeatures & features) const;
370 /// Checks if the paragraph contains only text and no inset or font change.
371 bool onlyText(Buffer const & buf, Font const & outerfont,
372 pos_type initial) const;
374 /// a vector of speller skip positions
375 typedef vector<FontSpan> SkipPositions;
376 typedef SkipPositions::const_iterator SkipPositionsIterator;
378 void appendSkipPosition(SkipPositions & skips, pos_type const pos) const;
380 Language * getSpellLanguage(pos_type const from) const;
382 Language * locateSpellRange(pos_type & from, pos_type & to,
383 SkipPositions & skips) const;
385 bool hasSpellerChange() const
387 SpellChecker::ChangeNumber speller_change_number = 0;
388 if (theSpellChecker())
389 speller_change_number = theSpellChecker()->changeNumber();
390 return speller_change_number > speller_state_.currentChangeNumber();
393 bool ignoreWord(docstring const & word) const ;
395 void setMisspelled(pos_type from, pos_type to, SpellChecker::Result state)
397 pos_type textsize = owner_->size();
398 // check for sane arguments
399 if (to <= from || from >= textsize)
401 FontSpan fp = FontSpan(from, to - 1);
402 speller_state_.setRange(fp, state);
405 void requestSpellCheck(pos_type pos)
408 speller_state_.needsCompleteRefresh(speller_state_.currentChangeNumber());
410 speller_state_.needsRefresh(pos);
413 void readySpellCheck()
415 speller_state_.needsRefresh(-1);
418 bool needsSpellCheck() const
420 return speller_state_.needsRefresh();
423 void rangeOfSpellCheck(pos_type & first, pos_type & last) const
425 speller_state_.refreshRange(first, last);
427 last = owner_->size();
430 pos_type endpos = last;
431 owner_->locateWord(first, endpos, WHOLE_WORD);
434 owner_->locateWord(last, endpos, WHOLE_WORD);
439 int countSkips(SkipPositionsIterator & it, SkipPositionsIterator const et,
443 while (it != et && it->first < start) {
444 int skip = it->last - it->first + 1;
452 void markMisspelledWords(pos_type const & first, pos_type const & last,
453 SpellChecker::Result result,
454 docstring const & word,
455 SkipPositions const & skips);
457 InsetCode ownerCode() const
459 return inset_owner_ ? inset_owner_->lyxCode() : NO_CODE;
462 /// Which Paragraph owns us?
466 Inset const * inset_owner_;
475 ParagraphParameters params_;
477 /// for recording and looking up changes
481 InsetList insetlist_;
484 pos_type begin_of_body_;
486 typedef docstring TextContainer;
490 typedef set<docstring> Words;
491 typedef map<string, Words> LangWordsMap;
495 Layout const * layout_;
497 SpellCheckerState speller_state_;
501 Paragraph::Private::Private(Paragraph * owner, Layout const & layout)
502 : owner_(owner), inset_owner_(0), id_(-1), begin_of_body_(0), layout_(&layout)
508 // Initialization of the counter for the paragraph id's,
510 // FIXME: There should be a more intelligent way to generate and use the
511 // paragraph ids per buffer instead a global static counter for all InsetText
512 // in the running program.
513 static int paragraph_id = -1;
515 Paragraph::Private::Private(Private const & p, Paragraph * owner)
516 : owner_(owner), inset_owner_(p.inset_owner_), fontlist_(p.fontlist_),
517 params_(p.params_), changes_(p.changes_), insetlist_(p.insetlist_),
518 begin_of_body_(p.begin_of_body_), text_(p.text_), words_(p.words_),
521 id_ = ++paragraph_id;
522 requestSpellCheck(p.text_.size());
526 Paragraph::Private::Private(Private const & p, Paragraph * owner,
527 pos_type beg, pos_type end)
528 : owner_(owner), inset_owner_(p.inset_owner_),
529 params_(p.params_), changes_(p.changes_),
530 insetlist_(p.insetlist_, beg, end),
531 begin_of_body_(p.begin_of_body_), words_(p.words_),
534 id_ = ++paragraph_id;
535 if (beg >= pos_type(p.text_.size()))
537 text_ = p.text_.substr(beg, end - beg);
539 FontList::const_iterator fcit = fontlist_.begin();
540 FontList::const_iterator fend = fontlist_.end();
541 for (; fcit != fend; ++fcit) {
542 if (fcit->pos() < beg)
544 if (fcit->pos() >= end) {
545 // Add last entry in the fontlist_.
546 fontlist_.set(text_.size() - 1, fcit->font());
549 // Add a new entry in the fontlist_.
550 fontlist_.set(fcit->pos() - beg, fcit->font());
552 requestSpellCheck(p.text_.size());
556 void Paragraph::addChangesToToc(DocIterator const & cdit,
557 Buffer const & buf, bool output_active) const
559 d->changes_.addToToc(cdit, buf, output_active);
563 bool Paragraph::isDeleted(pos_type start, pos_type end) const
565 LASSERT(start >= 0 && start <= size(), return false);
566 LASSERT(end > start && end <= size() + 1, return false);
568 return d->changes_.isDeleted(start, end);
572 bool Paragraph::isChanged(pos_type start, pos_type end) const
574 LASSERT(start >= 0 && start <= size(), return false);
575 LASSERT(end > start && end <= size() + 1, return false);
577 return d->changes_.isChanged(start, end);
581 bool Paragraph::isMergedOnEndOfParDeletion(bool trackChanges) const
583 // keep the logic here in sync with the logic of eraseChars()
587 Change const & change = d->changes_.lookup(size());
588 return change.inserted() && change.currentAuthor();
592 void Paragraph::setChange(Change const & change)
594 // beware of the imaginary end-of-par character!
595 d->changes_.set(change, 0, size() + 1);
598 * Propagate the change recursively - but not in case of DELETED!
600 * Imagine that your co-author makes changes in an existing inset. He
601 * sends your document to you and you come to the conclusion that the
602 * inset should go completely. If you erase it, LyX must not delete all
603 * text within the inset. Otherwise, the change tracked insertions of
604 * your co-author get lost and there is no way to restore them later.
606 * Conclusion: An inset's content should remain untouched if you delete it
609 if (!change.deleted()) {
610 for (pos_type pos = 0; pos < size(); ++pos) {
611 if (Inset * inset = getInset(pos))
612 inset->setChange(change);
618 void Paragraph::setChange(pos_type pos, Change const & change)
620 LASSERT(pos >= 0 && pos <= size(), return);
621 d->changes_.set(change, pos);
623 // see comment in setChange(Change const &) above
624 if (!change.deleted() && pos < size())
625 if (Inset * inset = getInset(pos))
626 inset->setChange(change);
630 Change const & Paragraph::lookupChange(pos_type pos) const
632 LBUFERR(pos >= 0 && pos <= size());
633 return d->changes_.lookup(pos);
637 void Paragraph::acceptChanges(pos_type start, pos_type end)
639 LASSERT(start >= 0 && start <= size(), return);
640 LASSERT(end > start && end <= size() + 1, return);
642 for (pos_type pos = start; pos < end; ++pos) {
643 switch (lookupChange(pos).type) {
644 case Change::UNCHANGED:
645 // accept changes in nested inset
646 if (Inset * inset = getInset(pos))
647 inset->acceptChanges();
650 case Change::INSERTED:
651 d->changes_.set(Change(Change::UNCHANGED), pos);
652 // also accept changes in nested inset
653 if (Inset * inset = getInset(pos))
654 inset->acceptChanges();
657 case Change::DELETED:
658 // Suppress access to non-existent
659 // "end-of-paragraph char"
661 eraseChar(pos, false);
672 void Paragraph::rejectChanges(pos_type start, pos_type end)
674 LASSERT(start >= 0 && start <= size(), return);
675 LASSERT(end > start && end <= size() + 1, return);
677 for (pos_type pos = start; pos < end; ++pos) {
678 switch (lookupChange(pos).type) {
679 case Change::UNCHANGED:
680 // reject changes in nested inset
681 if (Inset * inset = getInset(pos))
682 inset->rejectChanges();
685 case Change::INSERTED:
686 // Suppress access to non-existent
687 // "end-of-paragraph char"
689 eraseChar(pos, false);
695 case Change::DELETED:
696 d->changes_.set(Change(Change::UNCHANGED), pos);
698 // Do NOT reject changes within a deleted inset!
699 // There may be insertions of a co-author inside of it!
707 void Paragraph::Private::insertChar(pos_type pos, char_type c,
708 Change const & change)
710 LASSERT(pos >= 0 && pos <= int(text_.size()), return);
713 changes_.insert(change, pos);
715 // This is actually very common when parsing buffers (and
716 // maybe inserting ascii text)
717 if (pos == pos_type(text_.size())) {
718 // when appending characters, no need to update tables
720 // but we want spell checking
721 requestSpellCheck(pos);
725 text_.insert(text_.begin() + pos, c);
727 // Update the font table.
728 fontlist_.increasePosAfterPos(pos);
731 insetlist_.increasePosAfterPos(pos);
733 // Update list of misspelled positions
734 speller_state_.increasePosAfterPos(pos);
738 bool Paragraph::insertInset(pos_type pos, Inset * inset,
739 Font const & font, Change const & change)
741 LASSERT(inset, return false);
742 LASSERT(pos >= 0 && pos <= size(), return false);
744 // Paragraph::insertInset() can be used in cut/copy/paste operation where
745 // d->inset_owner_ is not set yet.
746 if (d->inset_owner_ && !d->inset_owner_->insetAllowed(inset->lyxCode()))
749 d->insertChar(pos, META_INSET, change);
750 LASSERT(d->text_[pos] == META_INSET, return false);
752 // Add a new entry in the insetlist_.
753 d->insetlist_.insert(inset, pos);
755 // Some insets require run of spell checker
756 requestSpellCheck(pos);
762 bool Paragraph::eraseChar(pos_type pos, bool trackChanges)
764 LASSERT(pos >= 0 && pos <= size(), return false);
766 // keep the logic here in sync with the logic of isMergedOnEndOfParDeletion()
769 Change change = d->changes_.lookup(pos);
771 // set the character to DELETED if
772 // a) it was previously unchanged or
773 // b) it was inserted by a co-author
775 if (!change.changed() ||
776 (change.inserted() && !change.currentAuthor())) {
777 setChange(pos, Change(Change::DELETED));
778 // request run of spell checker
779 requestSpellCheck(pos);
783 if (change.deleted())
787 // Don't physically access the imaginary end-of-paragraph character.
788 // eraseChar() can only mark it as DELETED. A physical deletion of
789 // end-of-par must be handled externally.
795 d->changes_.erase(pos);
797 // if it is an inset, delete the inset entry
798 if (d->text_[pos] == META_INSET)
799 d->insetlist_.erase(pos);
801 d->text_.erase(d->text_.begin() + pos);
803 // Update the fontlist_
804 d->fontlist_.erase(pos);
806 // Update the insetlist_
807 d->insetlist_.decreasePosAfterPos(pos);
809 // Update list of misspelled positions
810 d->speller_state_.decreasePosAfterPos(pos);
811 d->speller_state_.refreshLast(size());
817 int Paragraph::eraseChars(pos_type start, pos_type end, bool trackChanges)
819 LASSERT(start >= 0 && start <= size(), return 0);
820 LASSERT(end >= start && end <= size() + 1, return 0);
823 for (pos_type count = end - start; count; --count) {
824 if (!eraseChar(i, trackChanges))
831 int Paragraph::Private::latexSurrogatePair(otexstream & os, char_type c,
832 char_type next, OutputParams const & runparams)
834 // Writing next here may circumvent a possible font change between
835 // c and next. Since next is only output if it forms a surrogate pair
836 // with c we can ignore this:
837 // A font change inside a surrogate pair does not make sense and is
838 // hopefully impossible to input.
839 // FIXME: change tracking
840 // Is this correct WRT change tracking?
841 Encoding const & encoding = *(runparams.encoding);
842 docstring latex1 = encoding.latexChar(next).first;
843 if (runparams.inIPA) {
844 string const tipashortcut = Encodings::TIPAShortcut(next);
845 if (!tipashortcut.empty()) {
846 latex1 = from_ascii(tipashortcut);
849 docstring const latex2 = encoding.latexChar(c).first;
850 if (docstring(1, next) == latex1) {
851 // the encoding supports the combination
852 os << latex2 << latex1;
853 return latex1.length() + latex2.length();
854 } else if (runparams.local_font &&
855 runparams.local_font->language()->lang() == "polutonikogreek") {
856 // polutonikogreek only works without the brackets
857 os << latex1 << latex2;
858 return latex1.length() + latex2.length();
860 os << latex1 << '{' << latex2 << '}';
861 return latex1.length() + latex2.length() + 2;
865 bool Paragraph::Private::simpleTeXBlanks(OutputParams const & runparams,
868 unsigned int & column,
870 Layout const & style)
872 if (style.pass_thru || runparams.pass_thru)
875 if (i + 1 < int(text_.size())) {
876 char_type next = text_[i + 1];
877 if (Encodings::isCombiningChar(next)) {
878 // This space has an accent, so we must always output it.
879 column += latexSurrogatePair(os, ' ', next, runparams) - 1;
884 if (runparams.linelen > 0
885 && column > runparams.linelen
887 && text_[i - 1] != ' '
888 && (i + 1 < int(text_.size()))
889 // same in FreeSpacing mode
890 && !owner_->isFreeSpacing()
891 // In typewriter mode, we want to avoid
892 // ! . ? : at the end of a line
893 && !(font.fontInfo().family() == TYPEWRITER_FAMILY
894 && (text_[i - 1] == '.'
895 || text_[i - 1] == '?'
896 || text_[i - 1] == ':'
897 || text_[i - 1] == '!'))) {
899 os.texrow().start(owner_->id(), i + 1);
901 } else if (style.free_spacing) {
910 int Paragraph::Private::writeScriptChars(otexstream & os,
911 docstring const & ltx,
912 Change const & runningChange,
913 Encoding const & encoding,
916 // FIXME: modifying i here is not very nice...
918 // We only arrive here when a proper language for character text_[i] has
919 // not been specified (i.e., it could not be translated in the current
920 // latex encoding) or its latex translation has been forced, and it
921 // belongs to a known script.
922 // Parameter ltx contains the latex translation of text_[i] as specified
923 // in the unicodesymbols file and is something like "\textXXX{<spec>}".
924 // The latex macro name "textXXX" specifies the script to which text_[i]
925 // belongs and we use it in order to check whether characters from the
926 // same script immediately follow, such that we can collect them in a
927 // single "\textXXX" macro. So, we have to retain "\textXXX{<spec>"
928 // for the first char but only "<spec>" for all subsequent chars.
929 docstring::size_type const brace1 = ltx.find_first_of(from_ascii("{"));
930 docstring::size_type const brace2 = ltx.find_last_of(from_ascii("}"));
931 string script = to_ascii(ltx.substr(1, brace1 - 1));
934 bool closing_brace = true;
935 if (script == "textgreek" && encoding.latexName() == "iso-8859-7") {
936 // Correct encoding is being used, so we can avoid \textgreek.
939 closing_brace = false;
941 os << ltx.substr(pos, length);
942 int size = text_.size();
943 while (i + 1 < size) {
944 char_type const next = text_[i + 1];
945 // Stop here if next character belongs to another script
946 // or there is a change in change tracking status.
947 if (!Encodings::isKnownScriptChar(next, script) ||
948 runningChange != owner_->lookupChange(i + 1))
952 FontList::const_iterator cit = fontlist_.begin();
953 FontList::const_iterator end = fontlist_.end();
954 for (; cit != end; ++cit) {
955 if (cit->pos() >= i && !found) {
956 prev_font = cit->font();
959 if (cit->pos() >= i + 1)
962 // Stop here if there is a font attribute or encoding change.
963 if (found && cit != end && prev_font != cit->font())
965 docstring const latex = encoding.latexChar(next).first;
966 docstring::size_type const b1 =
967 latex.find_first_of(from_ascii("{"));
968 docstring::size_type const b2 =
969 latex.find_last_of(from_ascii("}"));
970 int const len = b2 - b1 - 1;
971 os << latex.substr(b1 + 1, len);
983 void Paragraph::Private::latexInset(BufferParams const & bparams,
985 OutputParams & runparams,
988 Font const & outerfont,
990 Change & running_change,
991 Layout const & style,
993 unsigned int & column)
995 Inset * inset = owner_->getInset(i);
998 if (style.pass_thru) {
999 odocstringstream ods;
1000 inset->plaintext(ods, runparams);
1005 // FIXME: move this to InsetNewline::latex
1006 if (inset->lyxCode() == NEWLINE_CODE || inset->lyxCode() == SEPARATOR_CODE) {
1007 // newlines are handled differently here than
1008 // the default in simpleTeXSpecialChars().
1009 if (!style.newline_allowed) {
1013 column += running_font.latexWriteEndChanges(
1014 os, bparams, runparams,
1015 basefont, basefont);
1019 if (running_font.fontInfo().family() == TYPEWRITER_FAMILY)
1022 basefont = owner_->getLayoutFont(bparams, outerfont);
1023 running_font = basefont;
1025 if (runparams.moving_arg)
1029 os.texrow().start(owner_->id(), i + 1);
1033 if (owner_->isDeleted(i)) {
1034 if( ++runparams.inDeletedInset == 1)
1035 runparams.changeOfDeletedInset = owner_->lookupChange(i);
1038 if (inset->canTrackChanges()) {
1039 column += Changes::latexMarkChange(os, bparams, running_change,
1040 Change(Change::UNCHANGED), runparams);
1041 running_change = Change(Change::UNCHANGED);
1045 odocstream::pos_type const len = os.os().tellp();
1047 if (inset->forceLTR()
1048 && !runparams.use_polyglossia
1049 && running_font.isRightToLeft()
1050 // ERT is an exception, it should be output with no
1051 // decorations at all
1052 && inset->lyxCode() != ERT_CODE) {
1053 if (running_font.language()->lang() == "farsi")
1060 // FIXME: Bug: we can have an empty font change here!
1061 // if there has just been a font change, we are going to close it
1062 // right now, which means stupid latex code like \textsf{}. AFAIK,
1063 // this does not harm dvi output. A minor bug, thus (JMarc)
1065 // Some insets cannot be inside a font change command.
1066 // However, even such insets *can* be placed in \L or \R
1067 // or their equivalents (for RTL language switches), so we don't
1068 // close the language in those cases.
1069 // ArabTeX, though, cannot handle this special behavior, it seems.
1070 bool arabtex = basefont.language()->lang() == "arabic_arabtex"
1071 || running_font.language()->lang() == "arabic_arabtex";
1072 if (open_font && !inset->inheritFont()) {
1073 bool closeLanguage = arabtex
1074 || basefont.isRightToLeft() == running_font.isRightToLeft();
1075 unsigned int count = running_font.latexWriteEndChanges(os,
1076 bparams, runparams, basefont, basefont, closeLanguage);
1078 // if any font properties were closed, update the running_font,
1079 // making sure, however, to leave the language as it was
1081 // FIXME: probably a better way to keep track of the old
1082 // language, than copying the entire font?
1083 Font const copy_font(running_font);
1084 basefont = owner_->getLayoutFont(bparams, outerfont);
1085 running_font = basefont;
1087 running_font.setLanguage(copy_font.language());
1088 // leave font open if language is still open
1089 open_font = (running_font.language() == basefont.language());
1091 runparams.local_font = &basefont;
1095 int prev_rows = os.texrow().rows();
1098 runparams.lastid = id_;
1099 runparams.lastpos = i;
1100 inset->latex(os, runparams);
1101 } catch (EncodingException & e) {
1102 // add location information and throw again.
1109 if (running_font.language()->lang() == "farsi")
1115 if (os.texrow().rows() > prev_rows) {
1116 os.texrow().start(owner_->id(), i + 1);
1119 column += (unsigned int)(os.os().tellp() - len);
1122 if (owner_->isDeleted(i))
1123 --runparams.inDeletedInset;
1127 void Paragraph::Private::latexSpecialChar(otexstream & os,
1128 BufferParams const & bparams,
1129 OutputParams const & runparams,
1130 Font const & running_font,
1131 Change const & running_change,
1132 Layout const & style,
1135 unsigned int & column)
1137 // With polyglossia, brackets and stuff need not be reversed
1138 // in RTL scripts (see bug #8251)
1139 char_type const c = (runparams.use_polyglossia) ?
1140 owner_->getUChar(bparams, i) : text_[i];
1142 if (style.pass_thru || runparams.pass_thru
1143 || contains(style.pass_thru_chars, c)
1144 || contains(runparams.pass_thru_chars, c)) {
1146 Encoding const * const enc = runparams.encoding;
1147 if (enc && !enc->encodable(c))
1148 throw EncodingException(c);
1154 // TIPA uses its own T3 encoding
1155 if (runparams.inIPA && latexSpecialT3(c, os, i, column))
1157 // If T1 font encoding is used, use the special
1158 // characters it provides.
1159 // NOTE: Some languages reset the font encoding internally.
1160 // If we are using such a language, we do not output
1161 // special T1 chars.
1162 if (!runparams.inIPA && !running_font.language()->internalFontEncoding()
1163 && bparams.font_encoding() == "T1" && latexSpecialT1(c, os, i, column))
1166 // Otherwise, we use what LaTeX provides us.
1169 os << "\\textbackslash{}";
1173 os << "\\textless{}";
1177 os << "\\textgreater{}";
1181 os << "\\textbar{}";
1186 if (i + 1 < end_pos && text_[i+1] == '-') {
1187 // Prevent "--" becoming an endash and "---" becoming
1189 // Within \ttfamily, "--" is merged to "-" (no endash)
1190 // so we avoid this rather irritating ligature as well
1196 os << "\\char`\\\"{}";
1201 case '%': case '#': case '{':
1209 os << "\\textasciitilde{}";
1214 os << "\\textasciicircum{}";
1221 // avoid being mistaken for optional arguments
1229 // Blanks are printed before font switching.
1230 // Sure? I am not! (try nice-latex)
1231 // I am sure it's correct. LyX might be smarter
1232 // in the future, but for now, nothing wrong is
1240 Encoding const & encoding = *(runparams.encoding);
1241 char_type next = '\0';
1242 if (i + 1 < int(text_.size())) {
1243 next = text_[i + 1];
1244 if (Encodings::isCombiningChar(next)) {
1245 column += latexSurrogatePair(os, c, next, runparams) - 1;
1251 pair<docstring, bool> latex = encoding.latexChar(c);
1252 docstring nextlatex;
1253 bool nexttipas = false;
1254 string nexttipashortcut;
1255 if (next != '\0' && next != META_INSET && encoding.encodable(next)) {
1256 nextlatex = encoding.latexChar(next).first;
1257 if (runparams.inIPA) {
1258 nexttipashortcut = Encodings::TIPAShortcut(next);
1259 nexttipas = !nexttipashortcut.empty();
1263 if (runparams.inIPA) {
1264 string const tipashortcut = Encodings::TIPAShortcut(c);
1265 if (!tipashortcut.empty()) {
1266 latex.first = from_ascii(tipashortcut);
1267 latex.second = false;
1271 if (Encodings::isKnownScriptChar(c, script)
1272 && prefixIs(latex.first, from_ascii("\\" + script)))
1273 column += writeScriptChars(os, latex.first,
1274 running_change, encoding, i) - 1;
1275 else if (latex.second
1276 && ((!prefixIs(nextlatex, '\\')
1277 && !prefixIs(nextlatex, '{')
1278 && !prefixIs(nextlatex, '}'))
1280 && !prefixIs(from_ascii(nexttipashortcut), '\\')))
1282 // Prevent eating of a following
1283 // space or command corruption by
1284 // following characters
1285 if (next == ' ' || next == '\0') {
1286 column += latex.first.length() + 1;
1287 os << latex.first << "{}";
1289 column += latex.first.length();
1290 os << latex.first << " ";
1293 column += latex.first.length() - 1;
1301 bool Paragraph::Private::latexSpecialT1(char_type const c, otexstream & os,
1302 pos_type i, unsigned int & column)
1308 // In T1 encoding, these characters exist
1309 // but we should avoid ligatures
1310 if (i + 1 >= int(text_.size()) || text_[i + 1] != c)
1312 os << "\\textcompwordmark{}";
1319 // soul.sty breaks with \char`\"
1320 os << "\\textquotedbl{}";
1329 bool Paragraph::Private::latexSpecialT3(char_type const c, otexstream & os,
1330 pos_type /*i*/, unsigned int & column)
1340 os << "\\textvertline{}";
1349 void Paragraph::Private::validate(LaTeXFeatures & features) const
1351 if (layout_->inpreamble && inset_owner_) {
1352 bool const is_command = layout_->latextype == LATEX_COMMAND;
1353 Buffer const & buf = inset_owner_->buffer();
1354 BufferParams const & bp = features.runparams().is_child
1355 ? buf.masterParams() : buf.params();
1358 // Using a string stream here circumvents the encoding
1359 // switching machinery of odocstream. Therefore the
1360 // output is wrong if this paragraph contains content
1361 // that needs to switch encoding.
1362 odocstringstream ods;
1363 otexstream os(ods, texrow);
1365 os << '\\' << from_ascii(layout_->latexname());
1366 // we have to provide all the optional arguments here, even though
1367 // the last one is the only one we care about.
1368 // Separate handling of optional argument inset.
1369 if (!layout_->latexargs().empty()) {
1370 OutputParams rp = features.runparams();
1371 rp.local_font = &owner_->getFirstFontSettings(bp);
1372 latexArgInsets(*owner_, os, rp, layout_->latexargs());
1374 os << from_ascii(layout_->latexparam());
1376 docstring::size_type const length = ods.str().length();
1377 // this will output "{" at the beginning, but not at the end
1378 owner_->latex(bp, f, os, features.runparams(), 0, -1, true);
1379 if (ods.str().length() > length) {
1382 if (!layout_->postcommandargs().empty()) {
1383 OutputParams rp = features.runparams();
1384 rp.local_font = &owner_->getFirstFontSettings(bp);
1385 latexArgInsets(*owner_, os, rp, layout_->postcommandargs(), "post:");
1388 string const snippet = to_utf8(ods.str());
1389 features.addPreambleSnippet(snippet);
1393 if (features.runparams().flavor == OutputParams::HTML
1394 && layout_->htmltitle()) {
1395 features.setHTMLTitle(owner_->asString(AS_STR_INSETS | AS_STR_SKIPDELETE));
1398 // check the params.
1399 if (!params_.spacing().isDefault())
1400 features.require("setspace");
1403 features.useLayout(layout_->name());
1406 fontlist_.validate(features);
1408 // then the indentation
1409 if (!params_.leftIndent().zero())
1410 features.require("ParagraphLeftIndent");
1413 InsetList::const_iterator icit = insetlist_.begin();
1414 InsetList::const_iterator iend = insetlist_.end();
1415 for (; icit != iend; ++icit) {
1417 icit->inset->validate(features);
1418 if (layout_->needprotect &&
1419 icit->inset->lyxCode() == FOOT_CODE)
1420 features.require("footmisc");
1424 // then the contents
1425 for (pos_type i = 0; i < int(text_.size()) ; ++i) {
1426 BufferEncodings::validate(text_[i], features);
1430 /////////////////////////////////////////////////////////////////////
1434 /////////////////////////////////////////////////////////////////////
1437 Layout const emptyParagraphLayout;
1440 Paragraph::Paragraph()
1441 : d(new Paragraph::Private(this, emptyParagraphLayout))
1448 Paragraph::Paragraph(Paragraph const & par)
1449 : itemdepth(par.itemdepth),
1450 d(new Paragraph::Private(*par.d, this))
1456 Paragraph::Paragraph(Paragraph const & par, pos_type beg, pos_type end)
1457 : itemdepth(par.itemdepth),
1458 d(new Paragraph::Private(*par.d, this, beg, end))
1464 Paragraph & Paragraph::operator=(Paragraph const & par)
1466 // needed as we will destroy the private part before copying it
1468 itemdepth = par.itemdepth;
1472 d = new Private(*par.d, this);
1479 Paragraph::~Paragraph()
1488 // this shall be called just before every "os << ..." action.
1489 void flushString(ostream & os, docstring & s)
1498 void Paragraph::write(ostream & os, BufferParams const & bparams,
1499 depth_type & dth) const
1501 // The beginning or end of a deeper (i.e. nested) area?
1502 if (dth != d->params_.depth()) {
1503 if (d->params_.depth() > dth) {
1504 while (d->params_.depth() > dth) {
1505 os << "\n\\begin_deeper";
1509 while (d->params_.depth() < dth) {
1510 os << "\n\\end_deeper";
1516 // First write the layout
1517 os << "\n\\begin_layout " << to_utf8(d->layout_->name()) << '\n';
1519 d->params_.write(os);
1521 Font font1(inherit_font, bparams.language);
1523 Change running_change = Change(Change::UNCHANGED);
1525 // this string is used as a buffer to avoid repetitive calls
1526 // to to_utf8(), which turn out to be expensive (JMarc)
1527 docstring write_buffer;
1530 for (pos_type i = 0; i <= size(); ++i) {
1532 Change const & change = lookupChange(i);
1533 if (change != running_change)
1534 flushString(os, write_buffer);
1535 Changes::lyxMarkChange(os, bparams, column, running_change, change);
1536 running_change = change;
1541 // Write font changes
1542 Font font2 = getFontSettings(bparams, i);
1543 if (font2 != font1) {
1544 flushString(os, write_buffer);
1545 font2.lyxWriteChanges(font1, os);
1550 char_type const c = d->text_[i];
1553 if (Inset const * inset = getInset(i)) {
1554 flushString(os, write_buffer);
1555 if (inset->directWrite()) {
1556 // international char, let it write
1557 // code directly so it's shorter in
1563 os << "\\begin_inset ";
1565 os << "\n\\end_inset\n\n";
1568 // FIXME This can be removed again once the mystery
1569 // crash has been resolved.
1574 flushString(os, write_buffer);
1575 os << "\n\\backslash\n";
1579 flushString(os, write_buffer);
1580 if (i + 1 < size() && d->text_[i + 1] == ' ') {
1587 if ((column > 70 && c == ' ')
1589 flushString(os, write_buffer);
1593 // this check is to amend a bug. LyX sometimes
1594 // inserts '\0' this could cause problems.
1596 write_buffer.push_back(c);
1598 LYXERR0("NUL char in structure.");
1604 flushString(os, write_buffer);
1605 os << "\n\\end_layout\n";
1606 // FIXME This can be removed again once the mystery
1607 // crash has been resolved.
1612 void Paragraph::validate(LaTeXFeatures & features) const
1614 d->validate(features);
1618 void Paragraph::insert(pos_type start, docstring const & str,
1619 Font const & font, Change const & change)
1621 for (size_t i = 0, n = str.size(); i != n ; ++i)
1622 insertChar(start + i, str[i], font, change);
1626 void Paragraph::appendChar(char_type c, Font const & font,
1627 Change const & change)
1630 d->changes_.insert(change, d->text_.size());
1631 // when appending characters, no need to update tables
1632 d->text_.push_back(c);
1633 setFont(d->text_.size() - 1, font);
1634 d->requestSpellCheck(d->text_.size() - 1);
1638 void Paragraph::appendString(docstring const & s, Font const & font,
1639 Change const & change)
1641 pos_type end = s.size();
1642 size_t oldsize = d->text_.size();
1643 size_t newsize = oldsize + end;
1644 size_t capacity = d->text_.capacity();
1645 if (newsize >= capacity)
1646 d->text_.reserve(max(capacity + 100, newsize));
1648 // when appending characters, no need to update tables
1651 // FIXME: Optimize this!
1652 for (size_t i = oldsize; i != newsize; ++i) {
1654 d->changes_.insert(change, i);
1655 d->requestSpellCheck(i);
1657 d->fontlist_.set(oldsize, font);
1658 d->fontlist_.set(newsize - 1, font);
1662 void Paragraph::insertChar(pos_type pos, char_type c,
1665 d->insertChar(pos, c, Change(trackChanges ?
1666 Change::INSERTED : Change::UNCHANGED));
1670 void Paragraph::insertChar(pos_type pos, char_type c,
1671 Font const & font, bool trackChanges)
1673 d->insertChar(pos, c, Change(trackChanges ?
1674 Change::INSERTED : Change::UNCHANGED));
1679 void Paragraph::insertChar(pos_type pos, char_type c,
1680 Font const & font, Change const & change)
1682 d->insertChar(pos, c, change);
1687 void Paragraph::resetFonts(Font const & font)
1689 d->fontlist_.clear();
1690 d->fontlist_.set(0, font);
1691 d->fontlist_.set(d->text_.size() - 1, font);
1694 // Gets uninstantiated font setting at position.
1695 Font const & Paragraph::getFontSettings(BufferParams const & bparams,
1699 LYXERR0("pos: " << pos << " size: " << size());
1703 FontList::const_iterator cit = d->fontlist_.fontIterator(pos);
1704 if (cit != d->fontlist_.end())
1707 if (pos == size() && !empty())
1708 return getFontSettings(bparams, pos - 1);
1710 // Optimisation: avoid a full font instantiation if there is no
1711 // language change from previous call.
1712 static Font previous_font;
1713 static Language const * previous_lang = 0;
1714 Language const * lang = getParLanguage(bparams);
1715 if (lang != previous_lang) {
1716 previous_lang = lang;
1717 previous_font = Font(inherit_font, lang);
1719 return previous_font;
1723 FontSpan Paragraph::fontSpan(pos_type pos) const
1725 LBUFERR(pos < size());
1728 FontList::const_iterator cit = d->fontlist_.begin();
1729 FontList::const_iterator end = d->fontlist_.end();
1730 for (; cit != end; ++cit) {
1731 if (cit->pos() >= pos) {
1732 if (pos >= beginOfBody())
1733 return FontSpan(max(start, beginOfBody()),
1736 return FontSpan(start,
1737 min(beginOfBody() - 1,
1740 start = cit->pos() + 1;
1743 // This should not happen, but if so, we take no chances.
1744 LYXERR0("Paragraph::fontSpan: position not found in fontinfo table!");
1745 LASSERT(false, return FontSpan(pos, pos));
1749 // Gets uninstantiated font setting at position 0
1750 Font const & Paragraph::getFirstFontSettings(BufferParams const & bparams) const
1752 if (!empty() && !d->fontlist_.empty())
1753 return d->fontlist_.begin()->font();
1755 // Optimisation: avoid a full font instantiation if there is no
1756 // language change from previous call.
1757 static Font previous_font;
1758 static Language const * previous_lang = 0;
1759 if (bparams.language != previous_lang) {
1760 previous_lang = bparams.language;
1761 previous_font = Font(inherit_font, bparams.language);
1764 return previous_font;
1768 // Gets the fully instantiated font at a given position in a paragraph
1769 // This is basically the same function as Text::GetFont() in text2.cpp.
1770 // The difference is that this one is used for generating the LaTeX file,
1771 // and thus cosmetic "improvements" are disallowed: This has to deliver
1772 // the true picture of the buffer. (Asger)
1773 Font const Paragraph::getFont(BufferParams const & bparams, pos_type pos,
1774 Font const & outerfont) const
1778 Font font = getFontSettings(bparams, pos);
1780 pos_type const body_pos = beginOfBody();
1781 FontInfo & fi = font.fontInfo();
1783 fi.realize(d->layout_->labelfont);
1785 fi.realize(d->layout_->font);
1787 fi.realize(outerfont.fontInfo());
1788 fi.realize(bparams.getFont().fontInfo());
1794 Font const Paragraph::getLabelFont
1795 (BufferParams const & bparams, Font const & outerfont) const
1797 FontInfo tmpfont = d->layout_->labelfont;
1798 tmpfont.realize(outerfont.fontInfo());
1799 tmpfont.realize(bparams.getFont().fontInfo());
1800 return Font(tmpfont, getParLanguage(bparams));
1804 Font const Paragraph::getLayoutFont
1805 (BufferParams const & bparams, Font const & outerfont) const
1807 FontInfo tmpfont = d->layout_->font;
1808 tmpfont.realize(outerfont.fontInfo());
1809 tmpfont.realize(bparams.getFont().fontInfo());
1810 return Font(tmpfont, getParLanguage(bparams));
1814 /// Returns the height of the highest font in range
1815 FontSize Paragraph::highestFontInRange
1816 (pos_type startpos, pos_type endpos, FontSize def_size) const
1818 return d->fontlist_.highestInRange(startpos, endpos, def_size);
1822 char_type Paragraph::getUChar(BufferParams const & bparams, pos_type pos) const
1824 char_type c = d->text_[pos];
1825 if (!getFontSettings(bparams, pos).isRightToLeft())
1828 // FIXME: The arabic special casing is due to the difference of arabic
1829 // round brackets input introduced in r18599. Check if this should be
1830 // unified with Hebrew or at least if all bracket types should be
1831 // handled the same (file format change in either case).
1832 string const & lang = getFontSettings(bparams, pos).language()->lang();
1833 bool const arabic = lang == "arabic_arabtex" || lang == "arabic_arabi"
1838 uc = arabic ? c : ')';
1841 uc = arabic ? c : '(';
1867 void Paragraph::setFont(pos_type pos, Font const & font)
1869 LASSERT(pos <= size(), return);
1871 // First, reduce font against layout/label font
1872 // Update: The setCharFont() routine in text2.cpp already
1873 // reduces font, so we don't need to do that here. (Asger)
1875 d->fontlist_.set(pos, font);
1879 void Paragraph::makeSameLayout(Paragraph const & par)
1881 d->layout_ = par.d->layout_;
1882 d->params_ = par.d->params_;
1886 bool Paragraph::stripLeadingSpaces(bool trackChanges)
1888 if (isFreeSpacing())
1894 while (pos < size() && (isNewline(pos) || isLineSeparator(pos))) {
1895 if (eraseChar(pos, trackChanges))
1901 return count > 0 || pos > 0;
1905 bool Paragraph::hasSameLayout(Paragraph const & par) const
1907 return par.d->layout_ == d->layout_
1908 && d->params_.sameLayout(par.d->params_);
1912 depth_type Paragraph::getDepth() const
1914 return d->params_.depth();
1918 depth_type Paragraph::getMaxDepthAfter() const
1920 if (d->layout_->isEnvironment())
1921 return d->params_.depth() + 1;
1923 return d->params_.depth();
1927 char Paragraph::getAlign() const
1929 if (d->params_.align() == LYX_ALIGN_LAYOUT)
1930 return d->layout_->align;
1932 return d->params_.align();
1936 docstring const & Paragraph::labelString() const
1938 return d->params_.labelString();
1942 // the next two functions are for the manual labels
1943 docstring const Paragraph::getLabelWidthString() const
1945 if (d->layout_->margintype == MARGIN_MANUAL
1946 || d->layout_->latextype == LATEX_BIB_ENVIRONMENT)
1947 return d->params_.labelWidthString();
1949 return _("Senseless with this layout!");
1953 void Paragraph::setLabelWidthString(docstring const & s)
1955 d->params_.labelWidthString(s);
1959 docstring Paragraph::expandLabel(Layout const & layout,
1960 BufferParams const & bparams) const
1962 return expandParagraphLabel(layout, bparams, true);
1966 docstring Paragraph::expandDocBookLabel(Layout const & layout,
1967 BufferParams const & bparams) const
1969 return expandParagraphLabel(layout, bparams, false);
1973 docstring Paragraph::expandParagraphLabel(Layout const & layout,
1974 BufferParams const & bparams, bool process_appendix) const
1976 DocumentClass const & tclass = bparams.documentClass();
1977 string const & lang = getParLanguage(bparams)->code();
1978 bool const in_appendix = process_appendix && d->params_.appendix();
1979 docstring fmt = translateIfPossible(layout.labelstring(in_appendix), lang);
1981 if (fmt.empty() && !layout.counter.empty())
1982 return tclass.counters().theCounter(layout.counter, lang);
1984 // handle 'inherited level parts' in 'fmt',
1985 // i.e. the stuff between '@' in '@Section@.\arabic{subsection}'
1986 size_t const i = fmt.find('@', 0);
1987 if (i != docstring::npos) {
1988 size_t const j = fmt.find('@', i + 1);
1989 if (j != docstring::npos) {
1990 docstring parent(fmt, i + 1, j - i - 1);
1991 docstring label = from_ascii("??");
1992 if (tclass.hasLayout(parent))
1993 docstring label = expandParagraphLabel(tclass[parent], bparams,
1995 fmt = docstring(fmt, 0, i) + label
1996 + docstring(fmt, j + 1, docstring::npos);
2000 return tclass.counters().counterLabel(fmt, lang);
2004 void Paragraph::applyLayout(Layout const & new_layout)
2006 d->layout_ = &new_layout;
2007 LyXAlignment const oldAlign = d->params_.align();
2009 if (!(oldAlign & d->layout_->alignpossible)) {
2010 frontend::Alert::warning(_("Alignment not permitted"),
2011 _("The new layout does not permit the alignment previously used.\nSetting to default."));
2012 d->params_.align(LYX_ALIGN_LAYOUT);
2017 pos_type Paragraph::beginOfBody() const
2019 return d->begin_of_body_;
2023 void Paragraph::setBeginOfBody()
2025 if (d->layout_->labeltype != LABEL_MANUAL) {
2026 d->begin_of_body_ = 0;
2030 // Unroll the first two cycles of the loop
2031 // and remember the previous character to
2032 // remove unnecessary getChar() calls
2034 pos_type end = size();
2035 if (i < end && !(isNewline(i) || isEnvSeparator(i))) {
2037 char_type previous_char = 0;
2040 previous_char = d->text_[i];
2041 if (!(isNewline(i) || isEnvSeparator(i))) {
2043 while (i < end && previous_char != ' ') {
2045 if (isNewline(i) || isEnvSeparator(i))
2048 previous_char = temp;
2054 d->begin_of_body_ = i;
2058 bool Paragraph::allowParagraphCustomization() const
2060 return inInset().allowParagraphCustomization();
2064 bool Paragraph::usePlainLayout() const
2066 return inInset().usePlainLayout();
2070 bool Paragraph::isPassThru() const
2072 return inInset().isPassThru() || d->layout_->pass_thru;
2077 // paragraphs inside floats need different alignment tags to avoid
2080 bool noTrivlistCentering(InsetCode code)
2082 return code == FLOAT_CODE
2083 || code == WRAP_CODE
2084 || code == CELL_CODE;
2088 string correction(string const & orig)
2090 if (orig == "flushleft")
2091 return "raggedright";
2092 if (orig == "flushright")
2093 return "raggedleft";
2094 if (orig == "center")
2100 bool corrected_env(otexstream & os, string const & suffix, string const & env,
2101 InsetCode code, bool const lastpar, int & col)
2103 string macro = suffix + "{";
2104 if (noTrivlistCentering(code)) {
2106 // the last paragraph in non-trivlist-aligned
2107 // context is special (to avoid unwanted whitespace)
2108 if (suffix == "\\begin") {
2109 macro = "\\" + correction(env) + "{}";
2110 os << from_ascii(macro);
2111 col += macro.size();
2116 macro += correction(env);
2120 if (suffix == "\\par\\end") {
2124 os << from_ascii(macro);
2125 col += macro.size();
2126 if (suffix == "\\begin") {
2136 int Paragraph::Private::startTeXParParams(BufferParams const & bparams,
2137 otexstream & os, OutputParams const & runparams) const
2141 if (params_.noindent() && !layout_->pass_thru
2142 && (layout_->toggle_indent != ITOGGLE_NEVER)) {
2143 os << "\\noindent ";
2147 LyXAlignment const curAlign = params_.align();
2149 if (curAlign == layout_->align)
2153 case LYX_ALIGN_NONE:
2154 case LYX_ALIGN_BLOCK:
2155 case LYX_ALIGN_LAYOUT:
2156 case LYX_ALIGN_SPECIAL:
2157 case LYX_ALIGN_DECIMAL:
2159 case LYX_ALIGN_LEFT:
2160 case LYX_ALIGN_RIGHT:
2161 case LYX_ALIGN_CENTER:
2162 if (runparams.moving_arg) {
2169 string const begin_tag = "\\begin";
2170 InsetCode code = ownerCode();
2171 bool const lastpar = runparams.isLastPar;
2174 case LYX_ALIGN_NONE:
2175 case LYX_ALIGN_BLOCK:
2176 case LYX_ALIGN_LAYOUT:
2177 case LYX_ALIGN_SPECIAL:
2178 case LYX_ALIGN_DECIMAL:
2180 case LYX_ALIGN_LEFT: {
2181 if (owner_->getParLanguage(bparams)->babel() != "hebrew")
2182 corrected_env(os, begin_tag, "flushleft", code, lastpar, column);
2184 corrected_env(os, begin_tag, "flushright", code, lastpar, column);
2186 } case LYX_ALIGN_RIGHT: {
2188 if (owner_->getParLanguage(bparams)->babel() != "hebrew")
2189 corrected_env(os, begin_tag, "flushright", code, lastpar, column);
2191 corrected_env(os, begin_tag, "flushleft", code, lastpar, column);
2193 } case LYX_ALIGN_CENTER: {
2194 corrected_env(os, begin_tag, "center", code, lastpar, column);
2203 bool Paragraph::Private::endTeXParParams(BufferParams const & bparams,
2204 otexstream & os, OutputParams const & runparams) const
2206 LyXAlignment const curAlign = params_.align();
2208 if (curAlign == layout_->align)
2212 case LYX_ALIGN_NONE:
2213 case LYX_ALIGN_BLOCK:
2214 case LYX_ALIGN_LAYOUT:
2215 case LYX_ALIGN_SPECIAL:
2216 case LYX_ALIGN_DECIMAL:
2218 case LYX_ALIGN_LEFT:
2219 case LYX_ALIGN_RIGHT:
2220 case LYX_ALIGN_CENTER:
2221 if (runparams.moving_arg)
2226 bool output = false;
2228 string const end_tag = "\\par\\end";
2229 InsetCode code = ownerCode();
2230 bool const lastpar = runparams.isLastPar;
2233 case LYX_ALIGN_NONE:
2234 case LYX_ALIGN_BLOCK:
2235 case LYX_ALIGN_LAYOUT:
2236 case LYX_ALIGN_SPECIAL:
2237 case LYX_ALIGN_DECIMAL:
2239 case LYX_ALIGN_LEFT: {
2240 if (owner_->getParLanguage(bparams)->babel() != "hebrew")
2241 output = corrected_env(os, end_tag, "flushleft", code, lastpar, col);
2243 output = corrected_env(os, end_tag, "flushright", code, lastpar, col);
2245 } case LYX_ALIGN_RIGHT: {
2246 if (owner_->getParLanguage(bparams)->babel() != "hebrew")
2247 output = corrected_env(os, end_tag, "flushright", code, lastpar, col);
2249 output = corrected_env(os, end_tag, "flushleft", code, lastpar, col);
2251 } case LYX_ALIGN_CENTER: {
2252 corrected_env(os, end_tag, "center", code, lastpar, col);
2257 return output || lastpar;
2261 // This one spits out the text of the paragraph
2262 void Paragraph::latex(BufferParams const & bparams,
2263 Font const & outerfont,
2265 OutputParams const & runparams,
2266 int start_pos, int end_pos, bool force) const
2268 LYXERR(Debug::LATEX, "Paragraph::latex... " << this);
2270 // FIXME This check should not be needed. Perhaps issue an
2271 // error if it triggers.
2272 Layout const & style = inInset().forcePlainLayout() ?
2273 bparams.documentClass().plainLayout() : *d->layout_;
2275 if (!force && style.inpreamble)
2278 bool const allowcust = allowParagraphCustomization();
2280 // Current base font for all inherited font changes, without any
2281 // change caused by an individual character, except for the language:
2282 // It is set to the language of the first character.
2283 // As long as we are in the label, this font is the base font of the
2284 // label. Before the first body character it is set to the base font
2288 // Maybe we have to create a optional argument.
2289 pos_type body_pos = beginOfBody();
2290 unsigned int column = 0;
2293 // the optional argument is kept in curly brackets in
2294 // case it contains a ']'
2295 // This is not strictly needed, but if this is changed it
2296 // would be a file format change, and tex2lyx would need
2297 // to be adjusted, since it unconditionally removes the
2298 // braces when it parses \item.
2301 basefont = getLabelFont(bparams, outerfont);
2303 basefont = getLayoutFont(bparams, outerfont);
2306 // Which font is currently active?
2307 Font running_font(basefont);
2308 // Do we have an open font change?
2309 bool open_font = false;
2311 Change runningChange = Change(Change::UNCHANGED);
2313 Encoding const * const prev_encoding = runparams.encoding;
2315 os.texrow().start(id(), 0);
2317 // if the paragraph is empty, the loop will not be entered at all
2319 if (style.isCommand()) {
2323 if (!style.leftdelim().empty()) {
2324 os << style.leftdelim();
2325 column += style.leftdelim().size();
2328 column += d->startTeXParParams(bparams, os, runparams);
2331 for (pos_type i = 0; i < size(); ++i) {
2332 // First char in paragraph or after label?
2333 if (i == body_pos) {
2336 column += running_font.latexWriteEndChanges(
2337 os, bparams, runparams,
2338 basefont, basefont);
2341 basefont = getLayoutFont(bparams, outerfont);
2342 running_font = basefont;
2344 column += Changes::latexMarkChange(os, bparams,
2345 runningChange, Change(Change::UNCHANGED),
2347 runningChange = Change(Change::UNCHANGED);
2352 if (style.isCommand()) {
2357 if (!style.leftdelim().empty()) {
2358 os << style.leftdelim();
2359 column += style.leftdelim().size();
2363 column += d->startTeXParParams(bparams, os,
2367 Change const & change = runparams.inDeletedInset
2368 ? runparams.changeOfDeletedInset : lookupChange(i);
2370 if (bparams.output_changes && runningChange != change) {
2372 column += running_font.latexWriteEndChanges(
2373 os, bparams, runparams, basefont, basefont);
2376 basefont = getLayoutFont(bparams, outerfont);
2377 running_font = basefont;
2379 column += Changes::latexMarkChange(os, bparams, runningChange,
2381 runningChange = change;
2384 // do not output text which is marked deleted
2385 // if change tracking output is disabled
2386 if (!bparams.output_changes && change.deleted()) {
2392 // Fully instantiated font
2393 Font const font = getFont(bparams, i, outerfont);
2395 Font const last_font = running_font;
2397 // Do we need to close the previous font?
2399 (font != running_font ||
2400 font.language() != running_font.language()))
2402 column += running_font.latexWriteEndChanges(
2403 os, bparams, runparams, basefont,
2404 (i == body_pos-1) ? basefont : font);
2405 running_font = basefont;
2409 string const running_lang = runparams.use_polyglossia ?
2410 running_font.language()->polyglossia() : running_font.language()->babel();
2411 // close babel's font environment before opening CJK.
2412 string const lang_end_command = runparams.use_polyglossia ?
2413 "\\end{$$lang}" : lyxrc.language_command_end;
2414 if (!running_lang.empty() &&
2415 font.language()->encoding()->package() == Encoding::CJK) {
2416 string end_tag = subst(lang_end_command,
2419 os << from_ascii(end_tag);
2420 column += end_tag.length();
2423 // Switch file encoding if necessary (and allowed)
2424 if (!runparams.pass_thru && !style.pass_thru &&
2425 runparams.encoding->package() != Encoding::none &&
2426 font.language()->encoding()->package() != Encoding::none) {
2427 pair<bool, int> const enc_switch =
2428 switchEncoding(os.os(), bparams, runparams,
2429 *(font.language()->encoding()));
2430 if (enc_switch.first) {
2431 column += enc_switch.second;
2432 runparams.encoding = font.language()->encoding();
2436 char_type const c = d->text_[i];
2438 // Do we need to change font?
2439 if ((font != running_font ||
2440 font.language() != running_font.language()) &&
2443 odocstringstream ods;
2444 column += font.latexWriteStartChanges(ods, bparams,
2445 runparams, basefont,
2447 running_font = font;
2449 docstring fontchange = ods.str();
2450 // check whether the fontchange ends with a \\textcolor
2451 // modifier and the text starts with a space (bug 4473)
2452 docstring const last_modifier = rsplit(fontchange, '\\');
2453 if (prefixIs(last_modifier, from_ascii("textcolor")) && c == ' ')
2454 os << fontchange << from_ascii("{}");
2455 // check if the fontchange ends with a trailing blank
2456 // (like "\small " (see bug 3382)
2457 else if (suffixIs(fontchange, ' ') && c == ' ')
2458 os << fontchange.substr(0, fontchange.size() - 1)
2459 << from_ascii("{}");
2464 // FIXME: think about end_pos implementation...
2465 if (c == ' ' && i >= start_pos && (end_pos == -1 || i < end_pos)) {
2466 // FIXME: integrate this case in latexSpecialChar
2467 // Do not print the separation of the optional argument
2468 // if style.pass_thru is false. This works because
2469 // latexSpecialChar ignores spaces if
2470 // style.pass_thru is false.
2471 if (i != body_pos - 1) {
2472 if (d->simpleTeXBlanks(runparams, os,
2473 i, column, font, style)) {
2474 // A surrogate pair was output. We
2475 // must not call latexSpecialChar
2476 // in this iteration, since it would output
2477 // the combining character again.
2484 OutputParams rp = runparams;
2485 rp.free_spacing = style.free_spacing;
2486 rp.local_font = &font;
2487 rp.intitle = style.intitle;
2489 // Two major modes: LaTeX or plain
2490 // Handle here those cases common to both modes
2491 // and then split to handle the two modes separately.
2492 if (c == META_INSET) {
2493 if (i >= start_pos && (end_pos == -1 || i < end_pos)) {
2494 d->latexInset(bparams, os, rp, running_font,
2495 basefont, outerfont, open_font,
2496 runningChange, style, i, column);
2499 if (i >= start_pos && (end_pos == -1 || i < end_pos)) {
2501 d->latexSpecialChar(os, bparams, rp, running_font, runningChange,
2502 style, i, end_pos, column);
2503 } catch (EncodingException & e) {
2504 if (runparams.dryrun) {
2505 os << "<" << _("LyX Warning: ")
2506 << _("uncodable character") << " '";
2510 // add location information and throw again.
2519 // Set the encoding to that returned from latexSpecialChar (see
2520 // comment for encoding member in OutputParams.h)
2521 runparams.encoding = rp.encoding;
2524 // If we have an open font definition, we have to close it
2526 #ifdef FIXED_LANGUAGE_END_DETECTION
2528 running_font.latexWriteEndChanges(os, bparams,
2529 runparams, basefont,
2530 next_->getFont(bparams, 0, outerfont));
2532 running_font.latexWriteEndChanges(os, bparams,
2533 runparams, basefont, basefont);
2536 //FIXME: For now we ALWAYS have to close the foreign font settings if they are
2537 //FIXME: there as we start another \selectlanguage with the next paragraph if
2538 //FIXME: we are in need of this. This should be fixed sometime (Jug)
2539 running_font.latexWriteEndChanges(os, bparams, runparams,
2540 basefont, basefont);
2544 column += Changes::latexMarkChange(os, bparams, runningChange,
2545 Change(Change::UNCHANGED), runparams);
2547 // Needed if there is an optional argument but no contents.
2548 if (body_pos > 0 && body_pos == size()) {
2552 if (!style.rightdelim().empty()) {
2553 os << style.rightdelim();
2554 column += style.rightdelim().size();
2557 if (allowcust && d->endTeXParParams(bparams, os, runparams)
2558 && runparams.encoding != prev_encoding) {
2559 runparams.encoding = prev_encoding;
2560 if (!runparams.isFullUnicode())
2561 os << setEncoding(prev_encoding->iconvName());
2564 LYXERR(Debug::LATEX, "Paragraph::latex... done " << this);
2568 bool Paragraph::emptyTag() const
2570 for (pos_type i = 0; i < size(); ++i) {
2571 if (Inset const * inset = getInset(i)) {
2572 InsetCode lyx_code = inset->lyxCode();
2573 // FIXME testing like that is wrong. What is
2575 if (lyx_code != TOC_CODE &&
2576 lyx_code != INCLUDE_CODE &&
2577 lyx_code != GRAPHICS_CODE &&
2578 lyx_code != ERT_CODE &&
2579 lyx_code != LISTINGS_CODE &&
2580 lyx_code != FLOAT_CODE &&
2581 lyx_code != TABULAR_CODE) {
2585 char_type c = d->text_[i];
2586 if (c != ' ' && c != '\t')
2594 string Paragraph::getID(Buffer const & buf, OutputParams const & runparams)
2597 for (pos_type i = 0; i < size(); ++i) {
2598 if (Inset const * inset = getInset(i)) {
2599 InsetCode lyx_code = inset->lyxCode();
2600 if (lyx_code == LABEL_CODE) {
2601 InsetLabel const * const il = static_cast<InsetLabel const *>(inset);
2602 docstring const & id = il->getParam("name");
2603 return "id='" + to_utf8(sgml::cleanID(buf, runparams, id)) + "'";
2611 pos_type Paragraph::firstWordDocBook(odocstream & os, OutputParams const & runparams)
2615 for (i = 0; i < size(); ++i) {
2616 if (Inset const * inset = getInset(i)) {
2617 inset->docbook(os, runparams);
2619 char_type c = d->text_[i];
2622 os << sgml::escapeChar(c);
2629 pos_type Paragraph::firstWordLyXHTML(XHTMLStream & xs, OutputParams const & runparams)
2633 for (i = 0; i < size(); ++i) {
2634 if (Inset const * inset = getInset(i)) {
2635 inset->xhtml(xs, runparams);
2637 char_type c = d->text_[i];
2647 bool Paragraph::Private::onlyText(Buffer const & buf, Font const & outerfont, pos_type initial) const
2650 pos_type size = text_.size();
2651 for (pos_type i = initial; i < size; ++i) {
2652 Font font = owner_->getFont(buf.params(), i, outerfont);
2653 if (text_[i] == META_INSET)
2655 if (i != initial && font != font_old)
2664 void Paragraph::simpleDocBookOnePar(Buffer const & buf,
2666 OutputParams const & runparams,
2667 Font const & outerfont,
2668 pos_type initial) const
2670 bool emph_flag = false;
2672 Layout const & style = *d->layout_;
2674 style.labeltype == LABEL_MANUAL ? style.labelfont : style.font;
2676 if (style.pass_thru && !d->onlyText(buf, outerfont, initial))
2679 // parsing main loop
2680 for (pos_type i = initial; i < size(); ++i) {
2681 Font font = getFont(buf.params(), i, outerfont);
2683 // handle <emphasis> tag
2684 if (font_old.emph() != font.fontInfo().emph()) {
2685 if (font.fontInfo().emph() == FONT_ON) {
2688 } else if (i != initial) {
2689 os << "</emphasis>";
2694 if (Inset const * inset = getInset(i)) {
2695 inset->docbook(os, runparams);
2697 char_type c = d->text_[i];
2699 if (style.pass_thru)
2702 os << sgml::escapeChar(c);
2704 font_old = font.fontInfo();
2708 os << "</emphasis>";
2711 if (style.free_spacing)
2713 if (style.pass_thru && !d->onlyText(buf, outerfont, initial))
2719 void doFontSwitch(vector<html::FontTag> & tagsToOpen,
2720 vector<html::EndFontTag> & tagsToClose,
2721 bool & flag, FontState curstate, html::FontTypes type)
2723 if (curstate == FONT_ON) {
2724 tagsToOpen.push_back(html::FontTag(type));
2727 tagsToClose.push_back(html::EndFontTag(type));
2734 docstring Paragraph::simpleLyXHTMLOnePar(Buffer const & buf,
2736 OutputParams const & runparams,
2737 Font const & outerfont,
2738 pos_type initial) const
2742 // track whether we have opened these tags
2743 bool emph_flag = false;
2744 bool bold_flag = false;
2745 bool noun_flag = false;
2746 bool ubar_flag = false;
2747 bool dbar_flag = false;
2748 bool sout_flag = false;
2749 bool wave_flag = false;
2751 bool shap_flag = false;
2753 bool faml_flag = false;
2755 bool size_flag = false;
2757 Layout const & style = *d->layout_;
2759 xs.startParagraph(allowEmpty());
2762 style.labeltype == LABEL_MANUAL ? style.labelfont : style.font;
2764 FontShape curr_fs = INHERIT_SHAPE;
2765 FontFamily curr_fam = INHERIT_FAMILY;
2766 FontSize curr_size = FONT_SIZE_INHERIT;
2768 string const default_family =
2769 buf.masterBuffer()->params().fonts_default_family;
2771 vector<html::FontTag> tagsToOpen;
2772 vector<html::EndFontTag> tagsToClose;
2774 // parsing main loop
2775 for (pos_type i = initial; i < size(); ++i) {
2776 // let's not show deleted material in the output
2780 Font const font = getFont(buf.masterBuffer()->params(), i, outerfont);
2783 FontState curstate = font.fontInfo().emph();
2784 if (font_old.emph() != curstate)
2785 doFontSwitch(tagsToOpen, tagsToClose, emph_flag, curstate, html::FT_EMPH);
2788 curstate = font.fontInfo().noun();
2789 if (font_old.noun() != curstate)
2790 doFontSwitch(tagsToOpen, tagsToClose, noun_flag, curstate, html::FT_NOUN);
2793 curstate = font.fontInfo().underbar();
2794 if (font_old.underbar() != curstate)
2795 doFontSwitch(tagsToOpen, tagsToClose, ubar_flag, curstate, html::FT_UBAR);
2798 curstate = font.fontInfo().strikeout();
2799 if (font_old.strikeout() != curstate)
2800 doFontSwitch(tagsToOpen, tagsToClose, sout_flag, curstate, html::FT_SOUT);
2803 curstate = font.fontInfo().uuline();
2804 if (font_old.uuline() != curstate)
2805 doFontSwitch(tagsToOpen, tagsToClose, dbar_flag, curstate, html::FT_DBAR);
2808 curstate = font.fontInfo().uwave();
2809 if (font_old.uwave() != curstate)
2810 doFontSwitch(tagsToOpen, tagsToClose, wave_flag, curstate, html::FT_WAVE);
2813 // a little hackish, but allows us to reuse what we have.
2814 curstate = (font.fontInfo().series() == BOLD_SERIES ? FONT_ON : FONT_OFF);
2815 if (font_old.series() != font.fontInfo().series())
2816 doFontSwitch(tagsToOpen, tagsToClose, bold_flag, curstate, html::FT_BOLD);
2819 curr_fs = font.fontInfo().shape();
2820 FontShape old_fs = font_old.shape();
2821 if (old_fs != curr_fs) {
2825 tagsToClose.push_back(html::EndFontTag(html::FT_ITALIC));
2828 tagsToClose.push_back(html::EndFontTag(html::FT_SLANTED));
2830 case SMALLCAPS_SHAPE:
2831 tagsToClose.push_back(html::EndFontTag(html::FT_SMALLCAPS));
2837 // the other tags are for internal use
2845 tagsToOpen.push_back(html::FontTag(html::FT_ITALIC));
2849 tagsToOpen.push_back(html::FontTag(html::FT_SLANTED));
2852 case SMALLCAPS_SHAPE:
2853 tagsToOpen.push_back(html::FontTag(html::FT_SMALLCAPS));
2860 // the other tags are for internal use
2867 curr_fam = font.fontInfo().family();
2868 FontFamily old_fam = font_old.family();
2869 if (old_fam != curr_fam) {
2873 tagsToClose.push_back(html::EndFontTag(html::FT_ROMAN));
2876 tagsToClose.push_back(html::EndFontTag(html::FT_SANS));
2878 case TYPEWRITER_FAMILY:
2879 tagsToClose.push_back(html::EndFontTag(html::FT_TYPE));
2881 case INHERIT_FAMILY:
2884 // the other tags are for internal use
2892 // we will treat a "default" font family as roman, since we have
2893 // no other idea what to do.
2894 if (default_family != "rmdefault" && default_family != "default") {
2895 tagsToOpen.push_back(html::FontTag(html::FT_ROMAN));
2900 if (default_family != "sfdefault") {
2901 tagsToOpen.push_back(html::FontTag(html::FT_SANS));
2905 case TYPEWRITER_FAMILY:
2906 if (default_family != "ttdefault") {
2907 tagsToOpen.push_back(html::FontTag(html::FT_TYPE));
2911 case INHERIT_FAMILY:
2914 // the other tags are for internal use
2921 curr_size = font.fontInfo().size();
2922 FontSize old_size = font_old.size();
2923 if (old_size != curr_size) {
2926 case FONT_SIZE_TINY:
2927 tagsToClose.push_back(html::EndFontTag(html::FT_SIZE_TINY));
2929 case FONT_SIZE_SCRIPT:
2930 tagsToClose.push_back(html::EndFontTag(html::FT_SIZE_SCRIPT));
2932 case FONT_SIZE_FOOTNOTE:
2933 tagsToClose.push_back(html::EndFontTag(html::FT_SIZE_FOOTNOTE));
2935 case FONT_SIZE_SMALL:
2936 tagsToClose.push_back(html::EndFontTag(html::FT_SIZE_SMALL));
2938 case FONT_SIZE_LARGE:
2939 tagsToClose.push_back(html::EndFontTag(html::FT_SIZE_LARGE));
2941 case FONT_SIZE_LARGER:
2942 tagsToClose.push_back(html::EndFontTag(html::FT_SIZE_LARGER));
2944 case FONT_SIZE_LARGEST:
2945 tagsToClose.push_back(html::EndFontTag(html::FT_SIZE_LARGEST));
2947 case FONT_SIZE_HUGE:
2948 tagsToClose.push_back(html::EndFontTag(html::FT_SIZE_HUGE));
2950 case FONT_SIZE_HUGER:
2951 tagsToClose.push_back(html::EndFontTag(html::FT_SIZE_HUGER));
2953 case FONT_SIZE_INCREASE:
2954 tagsToClose.push_back(html::EndFontTag(html::FT_SIZE_INCREASE));
2956 case FONT_SIZE_DECREASE:
2957 tagsToClose.push_back(html::EndFontTag(html::FT_SIZE_DECREASE));
2959 case FONT_SIZE_INHERIT:
2960 case FONT_SIZE_NORMAL:
2963 // the other tags are for internal use
2969 switch (curr_size) {
2970 case FONT_SIZE_TINY:
2971 tagsToOpen.push_back(html::FontTag(html::FT_SIZE_TINY));
2974 case FONT_SIZE_SCRIPT:
2975 tagsToOpen.push_back(html::FontTag(html::FT_SIZE_SCRIPT));
2978 case FONT_SIZE_FOOTNOTE:
2979 tagsToOpen.push_back(html::FontTag(html::FT_SIZE_FOOTNOTE));
2982 case FONT_SIZE_SMALL:
2983 tagsToOpen.push_back(html::FontTag(html::FT_SIZE_SMALL));
2986 case FONT_SIZE_LARGE:
2987 tagsToOpen.push_back(html::FontTag(html::FT_SIZE_LARGE));
2990 case FONT_SIZE_LARGER:
2991 tagsToOpen.push_back(html::FontTag(html::FT_SIZE_LARGER));
2994 case FONT_SIZE_LARGEST:
2995 tagsToOpen.push_back(html::FontTag(html::FT_SIZE_LARGEST));
2998 case FONT_SIZE_HUGE:
2999 tagsToOpen.push_back(html::FontTag(html::FT_SIZE_HUGE));
3002 case FONT_SIZE_HUGER:
3003 tagsToOpen.push_back(html::FontTag(html::FT_SIZE_HUGER));
3006 case FONT_SIZE_INCREASE:
3007 tagsToOpen.push_back(html::FontTag(html::FT_SIZE_INCREASE));
3010 case FONT_SIZE_DECREASE:
3011 tagsToOpen.push_back(html::FontTag(html::FT_SIZE_DECREASE));
3014 case FONT_SIZE_NORMAL:
3015 case FONT_SIZE_INHERIT:
3018 // the other tags are for internal use
3025 // Other such tags? What about the other text ranges?
3027 vector<html::EndFontTag>::const_iterator cit = tagsToClose.begin();
3028 vector<html::EndFontTag>::const_iterator cen = tagsToClose.end();
3029 for (; cit != cen; ++cit)
3032 vector<html::FontTag>::const_iterator sit = tagsToOpen.begin();
3033 vector<html::FontTag>::const_iterator sen = tagsToOpen.end();
3034 for (; sit != sen; ++sit)
3037 tagsToClose.clear();
3040 Inset const * inset = getInset(i);
3042 if (!runparams.for_toc || inset->isInToc()) {
3043 OutputParams np = runparams;
3044 np.local_font = &font;
3045 if (!inset->getLayout().htmlisblock())
3046 np.html_in_par = true;
3047 retval += inset->xhtml(xs, np);
3050 char_type c = getUChar(buf.masterBuffer()->params(), i);
3053 font_old = font.fontInfo();
3062 bool Paragraph::isHfill(pos_type pos) const
3064 Inset const * inset = getInset(pos);
3065 return inset && inset->isHfill();
3069 bool Paragraph::isNewline(pos_type pos) const
3071 Inset const * inset = getInset(pos);
3072 return inset && inset->lyxCode() == NEWLINE_CODE;
3076 bool Paragraph::isEnvSeparator(pos_type pos) const
3078 Inset const * inset = getInset(pos);
3079 return inset && inset->lyxCode() == SEPARATOR_CODE;
3083 bool Paragraph::isLineSeparator(pos_type pos) const
3085 char_type const c = d->text_[pos];
3086 if (isLineSeparatorChar(c))
3088 Inset const * inset = getInset(pos);
3089 return inset && inset->isLineSeparator();
3093 bool Paragraph::isWordSeparator(pos_type pos) const
3097 if (Inset const * inset = getInset(pos))
3098 return !inset->isLetter();
3099 // if we have a hard hyphen (no en- or emdash) or apostrophe
3100 // we pass this to the spell checker
3101 // FIXME: this method is subject to change, visit
3102 // https://bugzilla.mozilla.org/show_bug.cgi?id=355178
3103 // to get an impression how complex this is.
3104 if (isHardHyphenOrApostrophe(pos))
3106 char_type const c = d->text_[pos];
3107 // We want to pass the escape chars to the spellchecker
3108 docstring const escape_chars = from_utf8(lyxrc.spellchecker_esc_chars);
3109 return !isLetterChar(c) && !isDigitASCII(c) && !contains(escape_chars, c);
3113 bool Paragraph::isHardHyphenOrApostrophe(pos_type pos) const
3115 pos_type const psize = size();
3118 char_type const c = d->text_[pos];
3119 if (c != '-' && c != '\'')
3121 int nextpos = pos + 1;
3122 int prevpos = pos > 0 ? pos - 1 : 0;
3123 if ((nextpos == psize || isSpace(nextpos))
3124 && (pos == 0 || isSpace(prevpos)))
3130 bool Paragraph::isSameSpellRange(pos_type pos1, pos_type pos2) const
3133 || d->speller_state_.getRange(pos1) == d->speller_state_.getRange(pos2);
3137 bool Paragraph::isChar(pos_type pos) const
3139 if (Inset const * inset = getInset(pos))
3140 return inset->isChar();
3141 char_type const c = d->text_[pos];
3142 return !isLetterChar(c) && !isDigitASCII(c) && !lyx::isSpace(c);
3146 bool Paragraph::isSpace(pos_type pos) const
3148 if (Inset const * inset = getInset(pos))
3149 return inset->isSpace();
3150 char_type const c = d->text_[pos];
3151 return lyx::isSpace(c);
3156 Paragraph::getParLanguage(BufferParams const & bparams) const
3159 return getFirstFontSettings(bparams).language();
3160 // FIXME: we should check the prev par as well (Lgb)
3161 return bparams.language;
3165 bool Paragraph::isRTL(BufferParams const & bparams) const
3167 return getParLanguage(bparams)->rightToLeft()
3168 && !inInset().getLayout().forceLTR();
3172 void Paragraph::changeLanguage(BufferParams const & bparams,
3173 Language const * from, Language const * to)
3175 // change language including dummy font change at the end
3176 for (pos_type i = 0; i <= size(); ++i) {
3177 Font font = getFontSettings(bparams, i);
3178 if (font.language() == from) {
3179 font.setLanguage(to);
3181 d->requestSpellCheck(i);
3187 bool Paragraph::isMultiLingual(BufferParams const & bparams) const
3189 Language const * doc_language = bparams.language;
3190 FontList::const_iterator cit = d->fontlist_.begin();
3191 FontList::const_iterator end = d->fontlist_.end();
3193 for (; cit != end; ++cit)
3194 if (cit->font().language() != ignore_language &&
3195 cit->font().language() != latex_language &&
3196 cit->font().language() != doc_language)
3202 void Paragraph::getLanguages(std::set<Language const *> & languages) const
3204 FontList::const_iterator cit = d->fontlist_.begin();
3205 FontList::const_iterator end = d->fontlist_.end();
3207 for (; cit != end; ++cit) {
3208 Language const * lang = cit->font().language();
3209 if (lang != ignore_language &&
3210 lang != latex_language)
3211 languages.insert(lang);
3216 docstring Paragraph::asString(int options) const
3218 return asString(0, size(), options);
3222 docstring Paragraph::asString(pos_type beg, pos_type end, int options, const OutputParams *runparams) const
3224 odocstringstream os;
3227 && options & AS_STR_LABEL
3228 && !d->params_.labelString().empty())
3229 os << d->params_.labelString() << ' ';
3231 for (pos_type i = beg; i < end; ++i) {
3232 if ((options & AS_STR_SKIPDELETE) && isDeleted(i))
3234 char_type const c = d->text_[i];
3235 if (isPrintable(c) || c == '\t'
3236 || (c == '\n' && (options & AS_STR_NEWLINES)))
3238 else if (c == META_INSET && (options & AS_STR_INSETS)) {
3239 if (c == META_INSET && (options & AS_STR_PLAINTEXT)) {
3240 LASSERT(runparams != 0, return docstring());
3241 getInset(i)->plaintext(os, *runparams);
3243 getInset(i)->toString(os);
3252 void Paragraph::forOutliner(docstring & os, size_t maxlen) const
3254 if (!d->params_.labelString().empty())
3255 os += d->params_.labelString() + ' ';
3256 for (pos_type i = 0; i < size() && os.length() < maxlen; ++i) {
3259 char_type const c = d->text_[i];
3262 else if (c == '\t' || c == '\n')
3264 else if (c == META_INSET)
3265 getInset(i)->forOutliner(os, maxlen);
3270 void Paragraph::setInsetOwner(Inset const * inset)
3272 d->inset_owner_ = inset;
3276 int Paragraph::id() const
3282 void Paragraph::setId(int id)
3288 Layout const & Paragraph::layout() const
3294 void Paragraph::setLayout(Layout const & layout)
3296 d->layout_ = &layout;
3300 void Paragraph::setDefaultLayout(DocumentClass const & tc)
3302 setLayout(tc.defaultLayout());
3306 void Paragraph::setPlainLayout(DocumentClass const & tc)
3308 setLayout(tc.plainLayout());
3312 void Paragraph::setPlainOrDefaultLayout(DocumentClass const & tclass)
3314 if (usePlainLayout())
3315 setPlainLayout(tclass);
3317 setDefaultLayout(tclass);
3321 Inset const & Paragraph::inInset() const
3323 LBUFERR(d->inset_owner_);
3324 return *d->inset_owner_;
3328 ParagraphParameters & Paragraph::params()
3334 ParagraphParameters const & Paragraph::params() const
3340 bool Paragraph::isFreeSpacing() const
3342 if (d->layout_->free_spacing)
3344 return d->inset_owner_ && d->inset_owner_->isFreeSpacing();
3348 bool Paragraph::allowEmpty() const
3350 if (d->layout_->keepempty)
3352 return d->inset_owner_ && d->inset_owner_->allowEmpty();
3356 bool Paragraph::brokenBiblio() const
3358 // there is a problem if there is no bibitem at position 0 or
3359 // if there is another bibitem in the paragraph.
3360 return d->layout_->labeltype == LABEL_BIBLIO
3361 && (d->insetlist_.find(BIBITEM_CODE) != 0
3362 || d->insetlist_.find(BIBITEM_CODE, 1) > 0);
3366 int Paragraph::fixBiblio(Buffer const & buffer)
3368 // FIXME: What about the case where paragraph is not BIBLIO
3369 // but there is an InsetBibitem?
3370 // FIXME: when there was already an inset at 0, the return value is 1,
3371 // which does not tell whether another inset has been remove; the
3372 // cursor cannot be correctly updated.
3374 if (d->layout_->labeltype != LABEL_BIBLIO)
3377 bool const track_changes = buffer.params().track_changes;
3378 int bibitem_pos = d->insetlist_.find(BIBITEM_CODE);
3379 bool const hasbibitem0 = bibitem_pos == 0;
3382 bibitem_pos = d->insetlist_.find(BIBITEM_CODE, 1);
3383 // There was an InsetBibitem at pos 0, and no other one => OK
3384 if (bibitem_pos == -1)
3386 // there is a bibitem at the 0 position, but since
3387 // there is a second one, we copy the second on the
3388 // first. We're assuming there are at most two of
3389 // these, which there should be.
3390 // FIXME: why does it make sense to do that rather
3391 // than keep the first? (JMarc)
3392 Inset * inset = releaseInset(bibitem_pos);
3393 d->insetlist_.begin()->inset = inset;
3394 return -bibitem_pos;
3397 // We need to create an inset at the beginning
3399 if (bibitem_pos > 0) {
3400 // there was one somewhere in the paragraph, let's move it
3401 inset = d->insetlist_.release(bibitem_pos);
3402 eraseChar(bibitem_pos, track_changes);
3405 inset = new InsetBibitem(const_cast<Buffer *>(&buffer),
3406 InsetCommandParams(BIBITEM_CODE));
3408 Font font(inherit_font, buffer.params().language);
3409 insertInset(0, inset, font, Change(track_changes ? Change::INSERTED
3410 : Change::UNCHANGED));
3416 void Paragraph::checkAuthors(AuthorList const & authorList)
3418 d->changes_.checkAuthors(authorList);
3422 bool Paragraph::isChanged(pos_type pos) const
3424 return lookupChange(pos).changed();
3428 bool Paragraph::isInserted(pos_type pos) const
3430 return lookupChange(pos).inserted();
3434 bool Paragraph::isDeleted(pos_type pos) const
3436 return lookupChange(pos).deleted();
3440 InsetList const & Paragraph::insetList() const
3442 return d->insetlist_;
3446 void Paragraph::setBuffer(Buffer & b)
3448 d->insetlist_.setBuffer(b);
3452 Inset * Paragraph::releaseInset(pos_type pos)
3454 Inset * inset = d->insetlist_.release(pos);
3455 /// does not honour change tracking!
3456 eraseChar(pos, false);
3461 Inset * Paragraph::getInset(pos_type pos)
3463 return (pos < pos_type(d->text_.size()) && d->text_[pos] == META_INSET)
3464 ? d->insetlist_.get(pos) : 0;
3468 Inset const * Paragraph::getInset(pos_type pos) const
3470 return (pos < pos_type(d->text_.size()) && d->text_[pos] == META_INSET)
3471 ? d->insetlist_.get(pos) : 0;
3475 void Paragraph::changeCase(BufferParams const & bparams, pos_type pos,
3476 pos_type & right, TextCase action)
3478 // process sequences of modified characters; in change
3479 // tracking mode, this approach results in much better
3480 // usability than changing case on a char-by-char basis
3481 // We also need to track the current font, since font
3482 // changes within sequences can occur.
3483 vector<pair<char_type, Font> > changes;
3485 bool const trackChanges = bparams.track_changes;
3487 bool capitalize = true;
3489 for (; pos < right; ++pos) {
3490 char_type oldChar = d->text_[pos];
3491 char_type newChar = oldChar;
3493 // ignore insets and don't play with deleted text!
3494 if (oldChar != META_INSET && !isDeleted(pos)) {
3496 case text_lowercase:
3497 newChar = lowercase(oldChar);
3499 case text_capitalization:
3501 newChar = uppercase(oldChar);
3505 case text_uppercase:
3506 newChar = uppercase(oldChar);
3511 if (isWordSeparator(pos) || isDeleted(pos)) {
3512 // permit capitalization again
3516 if (oldChar != newChar) {
3517 changes.push_back(make_pair(newChar, getFontSettings(bparams, pos)));
3518 if (pos != right - 1)
3520 // step behind the changing area
3524 int erasePos = pos - changes.size();
3525 for (size_t i = 0; i < changes.size(); i++) {
3526 insertChar(pos, changes[i].first,
3529 if (!eraseChar(erasePos, trackChanges)) {
3532 ++right; // expand selection
3540 int Paragraph::find(docstring const & str, bool cs, bool mw,
3541 pos_type start_pos, bool del) const
3543 pos_type pos = start_pos;
3544 int const strsize = str.length();
3546 pos_type const parsize = d->text_.size();
3547 for (i = 0; i < strsize && pos < parsize; ++i, ++pos) {
3548 // Ignore "invisible" letters such as ligature breaks
3549 // and hyphenation chars while searching
3550 while (pos < parsize - 1 && isInset(pos)) {
3551 odocstringstream os;
3552 getInset(pos)->toString(os);
3553 if (!getInset(pos)->isLetter() || !os.str().empty())
3557 if (cs && str[i] != d->text_[pos])
3559 if (!cs && uppercase(str[i]) != uppercase(d->text_[pos]))
3561 if (!del && isDeleted(pos))
3568 // if necessary, check whether string matches word
3570 if (start_pos > 0 && !isWordSeparator(start_pos - 1))
3573 && !isWordSeparator(pos))
3577 return pos - start_pos;
3581 char_type Paragraph::getChar(pos_type pos) const
3583 return d->text_[pos];
3587 pos_type Paragraph::size() const
3589 return d->text_.size();
3593 bool Paragraph::empty() const
3595 return d->text_.empty();
3599 bool Paragraph::isInset(pos_type pos) const
3601 return d->text_[pos] == META_INSET;
3605 bool Paragraph::isSeparator(pos_type pos) const
3607 //FIXME: Are we sure this can be the only separator?
3608 return d->text_[pos] == ' ';
3612 void Paragraph::deregisterWords()
3614 Private::LangWordsMap::const_iterator itl = d->words_.begin();
3615 Private::LangWordsMap::const_iterator ite = d->words_.end();
3616 for (; itl != ite; ++itl) {
3617 WordList * wl = theWordList(itl->first);
3618 Private::Words::const_iterator it = (itl->second).begin();
3619 Private::Words::const_iterator et = (itl->second).end();
3620 for (; it != et; ++it)
3627 void Paragraph::locateWord(pos_type & from, pos_type & to,
3628 word_location const loc) const
3631 case WHOLE_WORD_STRICT:
3632 if (from == 0 || from == size()
3633 || isWordSeparator(from)
3634 || isWordSeparator(from - 1)) {
3638 // no break here, we go to the next
3641 // If we are already at the beginning of a word, do nothing
3642 if (!from || isWordSeparator(from - 1))
3644 // no break here, we go to the next
3647 // always move the cursor to the beginning of previous word
3648 while (from && !isWordSeparator(from - 1))
3652 LYXERR0("Paragraph::locateWord: NEXT_WORD not implemented yet");
3655 // no need to move the 'from' cursor
3659 while (to < size() && !isWordSeparator(to))
3664 void Paragraph::collectWords()
3666 for (pos_type pos = 0; pos < size(); ++pos) {
3667 if (isWordSeparator(pos))
3669 pos_type from = pos;
3670 locateWord(from, pos, WHOLE_WORD);
3671 // Work around MSVC warning: The statement
3672 // if (pos < from + lyxrc.completion_minlength)
3673 // triggers a signed vs. unsigned warning.
3674 // I don't know why this happens, it could be a MSVC bug, or
3675 // related to LLP64 (windows) vs. LP64 (unix) programming
3676 // model, or the C++ standard might be ambigous in the section
3677 // defining the "usual arithmetic conversions". However, using
3678 // a temporary variable is safe and works on all compilers.
3679 pos_type const endpos = from + lyxrc.completion_minlength;
3682 FontList::const_iterator cit = d->fontlist_.fontIterator(from);
3683 if (cit == d->fontlist_.end())
3685 Language const * lang = cit->font().language();
3686 docstring const word = asString(from, pos, AS_STR_NONE);
3687 d->words_[lang->lang()].insert(word);
3692 void Paragraph::registerWords()
3694 Private::LangWordsMap::const_iterator itl = d->words_.begin();
3695 Private::LangWordsMap::const_iterator ite = d->words_.end();
3696 for (; itl != ite; ++itl) {
3697 WordList * wl = theWordList(itl->first);
3698 Private::Words::const_iterator it = (itl->second).begin();
3699 Private::Words::const_iterator et = (itl->second).end();
3700 for (; it != et; ++it)
3706 void Paragraph::updateWords()
3714 void Paragraph::Private::appendSkipPosition(SkipPositions & skips, pos_type const pos) const
3716 SkipPositionsIterator begin = skips.begin();
3717 SkipPositions::iterator end = skips.end();
3718 if (pos > 0 && begin < end) {
3720 if (end->last == pos - 1) {
3725 skips.insert(end, FontSpan(pos, pos));
3729 Language * Paragraph::Private::locateSpellRange(
3730 pos_type & from, pos_type & to,
3731 SkipPositions & skips) const
3733 // skip leading white space
3734 while (from < to && owner_->isWordSeparator(from))
3736 // don't check empty range
3739 // get current language
3740 Language * lang = getSpellLanguage(from);
3741 pos_type last = from;
3742 bool samelang = true;
3743 bool sameinset = true;
3744 while (last < to && samelang && sameinset) {
3745 // hop to end of word
3746 while (last < to && !owner_->isWordSeparator(last)) {
3747 if (owner_->getInset(last)) {
3748 appendSkipPosition(skips, last);
3749 } else if (owner_->isDeleted(last)) {
3750 appendSkipPosition(skips, last);
3754 // hop to next word while checking for insets
3755 while (sameinset && last < to && owner_->isWordSeparator(last)) {
3756 if (Inset const * inset = owner_->getInset(last))
3757 sameinset = inset->isChar() && inset->isLetter();
3758 if (sameinset && owner_->isDeleted(last)) {
3759 appendSkipPosition(skips, last);
3764 if (sameinset && last < to) {
3765 // now check for language change
3766 samelang = lang == getSpellLanguage(last);
3769 // if language change detected backstep is needed
3777 Language * Paragraph::Private::getSpellLanguage(pos_type const from) const
3780 const_cast<Language *>(owner_->getFontSettings(
3781 inset_owner_->buffer().params(), from).language());
3782 if (lang == inset_owner_->buffer().params().language
3783 && !lyxrc.spellchecker_alt_lang.empty()) {
3785 string const lang_variety =
3786 split(lyxrc.spellchecker_alt_lang, lang_code, '-');
3787 lang->setCode(lang_code);
3788 lang->setVariety(lang_variety);
3794 void Paragraph::requestSpellCheck(pos_type pos)
3796 d->requestSpellCheck(pos);
3800 bool Paragraph::needsSpellCheck() const
3802 SpellChecker::ChangeNumber speller_change_number = 0;
3803 if (theSpellChecker())
3804 speller_change_number = theSpellChecker()->changeNumber();
3805 if (speller_change_number > d->speller_state_.currentChangeNumber()) {
3806 d->speller_state_.needsCompleteRefresh(speller_change_number);
3808 return d->needsSpellCheck();
3812 bool Paragraph::Private::ignoreWord(docstring const & word) const
3814 // Ignore words with digits
3815 // FIXME: make this customizable
3816 // (note that some checkers ignore words with digits by default)
3817 docstring::const_iterator cit = word.begin();
3818 docstring::const_iterator const end = word.end();
3819 for (; cit != end; ++cit) {
3820 if (isNumber((*cit)))
3827 SpellChecker::Result Paragraph::spellCheck(pos_type & from, pos_type & to,
3828 WordLangTuple & wl, docstring_list & suggestions,
3829 bool do_suggestion, bool check_learned) const
3831 SpellChecker::Result result = SpellChecker::WORD_OK;
3832 SpellChecker * speller = theSpellChecker();
3836 if (!d->layout_->spellcheck || !inInset().allowSpellCheck())
3839 locateWord(from, to, WHOLE_WORD);
3840 if (from == to || from >= size())
3843 docstring word = asString(from, to, AS_STR_INSETS | AS_STR_SKIPDELETE);
3844 Language * lang = d->getSpellLanguage(from);
3846 wl = WordLangTuple(word, lang);
3851 if (needsSpellCheck() || check_learned) {
3853 if (!d->ignoreWord(word)) {
3854 bool const trailing_dot = to < size() && d->text_[to] == '.';
3855 result = speller->check(wl);
3856 if (SpellChecker::misspelled(result) && trailing_dot) {
3857 wl = WordLangTuple(word.append(from_ascii(".")), lang);
3858 result = speller->check(wl);
3859 if (!SpellChecker::misspelled(result)) {
3860 LYXERR(Debug::GUI, "misspelled word is correct with dot: \"" <<
3862 from << ".." << to << "]");
3864 // spell check with dot appended failed too
3865 // restore original word/lang value
3866 word = asString(from, to, AS_STR_INSETS | AS_STR_SKIPDELETE);
3867 wl = WordLangTuple(word, lang);
3871 if (!SpellChecker::misspelled(result)) {
3872 // area up to the begin of the next word is not misspelled
3873 while (end < size() && isWordSeparator(end))
3876 d->setMisspelled(from, end, result);
3878 result = d->speller_state_.getState(from);
3882 suggestions.clear();
3884 if (SpellChecker::misspelled(result)) {
3885 LYXERR(Debug::GUI, "misspelled word: \"" <<
3887 from << ".." << to << "]");
3889 speller->suggest(wl, suggestions);
3895 void Paragraph::Private::markMisspelledWords(
3896 pos_type const & first, pos_type const & last,
3897 SpellChecker::Result result,
3898 docstring const & word,
3899 SkipPositions const & skips)
3901 if (!SpellChecker::misspelled(result)) {
3902 setMisspelled(first, last, SpellChecker::WORD_OK);
3906 SpellChecker * speller = theSpellChecker();
3907 // locate and enumerate the error positions
3908 int nerrors = speller->numMisspelledWords();
3910 SkipPositionsIterator it = skips.begin();
3911 SkipPositionsIterator et = skips.end();
3912 for (int index = 0; index < nerrors; ++index) {
3915 speller->misspelledWord(index, wstart, wlen);
3916 /// should not happen if speller supports range checks
3917 if (!wlen) continue;
3918 docstring const misspelled = word.substr(wstart, wlen);
3919 wstart += first + numskipped;
3920 if (snext < wstart) {
3921 /// mark the range of correct spelling
3922 numskipped += countSkips(it, et, wstart);
3923 setMisspelled(snext,
3924 wstart - 1, SpellChecker::WORD_OK);
3926 snext = wstart + wlen;
3927 numskipped += countSkips(it, et, snext);
3928 /// mark the range of misspelling
3929 setMisspelled(wstart, snext, result);
3930 LYXERR(Debug::GUI, "misspelled word: \"" <<
3931 misspelled << "\" [" <<
3932 wstart << ".." << (snext-1) << "]");
3935 if (snext <= last) {
3936 /// mark the range of correct spelling at end
3937 setMisspelled(snext, last, SpellChecker::WORD_OK);
3942 void Paragraph::spellCheck() const
3944 SpellChecker * speller = theSpellChecker();
3945 if (!speller || empty() ||!needsSpellCheck())
3949 d->rangeOfSpellCheck(start, endpos);
3950 if (speller->canCheckParagraph()) {
3951 // loop until we leave the range
3952 for (pos_type first = start; first < endpos; ) {
3953 pos_type last = endpos;
3954 Private::SkipPositions skips;
3955 Language * lang = d->locateSpellRange(first, last, skips);
3956 if (first >= endpos)
3958 // start the spell checker on the unit of meaning
3959 docstring word = asString(first, last, AS_STR_INSETS + AS_STR_SKIPDELETE);
3960 WordLangTuple wl = WordLangTuple(word, lang);
3961 SpellChecker::Result result = word.size() ?
3962 speller->check(wl) : SpellChecker::WORD_OK;
3963 d->markMisspelledWords(first, last, result, word, skips);
3967 static docstring_list suggestions;
3968 pos_type to = endpos;
3969 while (start < endpos) {
3971 spellCheck(start, to, wl, suggestions, false);
3975 d->readySpellCheck();
3979 bool Paragraph::isMisspelled(pos_type pos, bool check_boundary) const
3981 bool result = SpellChecker::misspelled(d->speller_state_.getState(pos));
3982 if (result || pos <= 0 || pos > size())
3984 if (check_boundary && (pos == size() || isWordSeparator(pos)))
3985 result = SpellChecker::misspelled(d->speller_state_.getState(pos - 1));
3990 string Paragraph::magicLabel() const
3993 ss << "magicparlabel-" << id();