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 // However, this per-session id is used in LFUN_PARAGRAPH_GOTO to
514 // switch to a different buffer, as used in the outliner for instance.
515 static int paragraph_id = -1;
517 Paragraph::Private::Private(Private const & p, Paragraph * owner)
518 : owner_(owner), inset_owner_(p.inset_owner_), fontlist_(p.fontlist_),
519 params_(p.params_), changes_(p.changes_), insetlist_(p.insetlist_),
520 begin_of_body_(p.begin_of_body_), text_(p.text_), words_(p.words_),
523 id_ = ++paragraph_id;
524 requestSpellCheck(p.text_.size());
528 Paragraph::Private::Private(Private const & p, Paragraph * owner,
529 pos_type beg, pos_type end)
530 : owner_(owner), inset_owner_(p.inset_owner_),
531 params_(p.params_), changes_(p.changes_),
532 insetlist_(p.insetlist_, beg, end),
533 begin_of_body_(p.begin_of_body_), words_(p.words_),
536 id_ = ++paragraph_id;
537 if (beg >= pos_type(p.text_.size()))
539 text_ = p.text_.substr(beg, end - beg);
541 FontList::const_iterator fcit = fontlist_.begin();
542 FontList::const_iterator fend = fontlist_.end();
543 for (; fcit != fend; ++fcit) {
544 if (fcit->pos() < beg)
546 if (fcit->pos() >= end) {
547 // Add last entry in the fontlist_.
548 fontlist_.set(text_.size() - 1, fcit->font());
551 // Add a new entry in the fontlist_.
552 fontlist_.set(fcit->pos() - beg, fcit->font());
554 requestSpellCheck(p.text_.size());
558 void Paragraph::addChangesToToc(DocIterator const & cdit,
559 Buffer const & buf, bool output_active) const
561 d->changes_.addToToc(cdit, buf, output_active);
565 bool Paragraph::isDeleted(pos_type start, pos_type end) const
567 LASSERT(start >= 0 && start <= size(), return false);
568 LASSERT(end > start && end <= size() + 1, return false);
570 return d->changes_.isDeleted(start, end);
574 bool Paragraph::isChanged(pos_type start, pos_type end) const
576 LASSERT(start >= 0 && start <= size(), return false);
577 LASSERT(end > start && end <= size() + 1, return false);
579 return d->changes_.isChanged(start, end);
583 bool Paragraph::isMergedOnEndOfParDeletion(bool trackChanges) const
585 // keep the logic here in sync with the logic of eraseChars()
589 Change const & change = d->changes_.lookup(size());
590 return change.inserted() && change.currentAuthor();
594 void Paragraph::setChange(Change const & change)
596 // beware of the imaginary end-of-par character!
597 d->changes_.set(change, 0, size() + 1);
600 * Propagate the change recursively - but not in case of DELETED!
602 * Imagine that your co-author makes changes in an existing inset. He
603 * sends your document to you and you come to the conclusion that the
604 * inset should go completely. If you erase it, LyX must not delete all
605 * text within the inset. Otherwise, the change tracked insertions of
606 * your co-author get lost and there is no way to restore them later.
608 * Conclusion: An inset's content should remain untouched if you delete it
611 if (!change.deleted()) {
612 for (pos_type pos = 0; pos < size(); ++pos) {
613 if (Inset * inset = getInset(pos))
614 inset->setChange(change);
620 void Paragraph::setChange(pos_type pos, Change const & change)
622 LASSERT(pos >= 0 && pos <= size(), return);
623 d->changes_.set(change, pos);
625 // see comment in setChange(Change const &) above
626 if (!change.deleted() && pos < size())
627 if (Inset * inset = getInset(pos))
628 inset->setChange(change);
632 Change const & Paragraph::lookupChange(pos_type pos) const
634 LBUFERR(pos >= 0 && pos <= size());
635 return d->changes_.lookup(pos);
639 void Paragraph::acceptChanges(pos_type start, pos_type end)
641 LASSERT(start >= 0 && start <= size(), return);
642 LASSERT(end > start && end <= size() + 1, return);
644 for (pos_type pos = start; pos < end; ++pos) {
645 switch (lookupChange(pos).type) {
646 case Change::UNCHANGED:
647 // accept changes in nested inset
648 if (Inset * inset = getInset(pos))
649 inset->acceptChanges();
652 case Change::INSERTED:
653 d->changes_.set(Change(Change::UNCHANGED), pos);
654 // also accept changes in nested inset
655 if (Inset * inset = getInset(pos))
656 inset->acceptChanges();
659 case Change::DELETED:
660 // Suppress access to non-existent
661 // "end-of-paragraph char"
663 eraseChar(pos, false);
674 void Paragraph::rejectChanges(pos_type start, pos_type end)
676 LASSERT(start >= 0 && start <= size(), return);
677 LASSERT(end > start && end <= size() + 1, return);
679 for (pos_type pos = start; pos < end; ++pos) {
680 switch (lookupChange(pos).type) {
681 case Change::UNCHANGED:
682 // reject changes in nested inset
683 if (Inset * inset = getInset(pos))
684 inset->rejectChanges();
687 case Change::INSERTED:
688 // Suppress access to non-existent
689 // "end-of-paragraph char"
691 eraseChar(pos, false);
697 case Change::DELETED:
698 d->changes_.set(Change(Change::UNCHANGED), pos);
700 // Do NOT reject changes within a deleted inset!
701 // There may be insertions of a co-author inside of it!
709 void Paragraph::Private::insertChar(pos_type pos, char_type c,
710 Change const & change)
712 LASSERT(pos >= 0 && pos <= int(text_.size()), return);
715 changes_.insert(change, pos);
717 // This is actually very common when parsing buffers (and
718 // maybe inserting ascii text)
719 if (pos == pos_type(text_.size())) {
720 // when appending characters, no need to update tables
722 // but we want spell checking
723 requestSpellCheck(pos);
727 text_.insert(text_.begin() + pos, c);
729 // Update the font table.
730 fontlist_.increasePosAfterPos(pos);
733 insetlist_.increasePosAfterPos(pos);
735 // Update list of misspelled positions
736 speller_state_.increasePosAfterPos(pos);
740 bool Paragraph::insertInset(pos_type pos, Inset * inset,
741 Font const & font, Change const & change)
743 LASSERT(inset, return false);
744 LASSERT(pos >= 0 && pos <= size(), return false);
746 // Paragraph::insertInset() can be used in cut/copy/paste operation where
747 // d->inset_owner_ is not set yet.
748 if (d->inset_owner_ && !d->inset_owner_->insetAllowed(inset->lyxCode()))
751 d->insertChar(pos, META_INSET, change);
752 LASSERT(d->text_[pos] == META_INSET, return false);
754 // Add a new entry in the insetlist_.
755 d->insetlist_.insert(inset, pos);
757 // Some insets require run of spell checker
758 requestSpellCheck(pos);
764 bool Paragraph::eraseChar(pos_type pos, bool trackChanges)
766 LASSERT(pos >= 0 && pos <= size(), return false);
768 // keep the logic here in sync with the logic of isMergedOnEndOfParDeletion()
771 Change change = d->changes_.lookup(pos);
773 // set the character to DELETED if
774 // a) it was previously unchanged or
775 // b) it was inserted by a co-author
777 if (!change.changed() ||
778 (change.inserted() && !change.currentAuthor())) {
779 setChange(pos, Change(Change::DELETED));
780 // request run of spell checker
781 requestSpellCheck(pos);
785 if (change.deleted())
789 // Don't physically access the imaginary end-of-paragraph character.
790 // eraseChar() can only mark it as DELETED. A physical deletion of
791 // end-of-par must be handled externally.
797 d->changes_.erase(pos);
799 // if it is an inset, delete the inset entry
800 if (d->text_[pos] == META_INSET)
801 d->insetlist_.erase(pos);
803 d->text_.erase(d->text_.begin() + pos);
805 // Update the fontlist_
806 d->fontlist_.erase(pos);
808 // Update the insetlist_
809 d->insetlist_.decreasePosAfterPos(pos);
811 // Update list of misspelled positions
812 d->speller_state_.decreasePosAfterPos(pos);
813 d->speller_state_.refreshLast(size());
819 int Paragraph::eraseChars(pos_type start, pos_type end, bool trackChanges)
821 LASSERT(start >= 0 && start <= size(), return 0);
822 LASSERT(end >= start && end <= size() + 1, return 0);
825 for (pos_type count = end - start; count; --count) {
826 if (!eraseChar(i, trackChanges))
833 int Paragraph::Private::latexSurrogatePair(otexstream & os, char_type c,
834 char_type next, OutputParams const & runparams)
836 // Writing next here may circumvent a possible font change between
837 // c and next. Since next is only output if it forms a surrogate pair
838 // with c we can ignore this:
839 // A font change inside a surrogate pair does not make sense and is
840 // hopefully impossible to input.
841 // FIXME: change tracking
842 // Is this correct WRT change tracking?
843 Encoding const & encoding = *(runparams.encoding);
844 docstring latex1 = encoding.latexChar(next).first;
845 if (runparams.inIPA) {
846 string const tipashortcut = Encodings::TIPAShortcut(next);
847 if (!tipashortcut.empty()) {
848 latex1 = from_ascii(tipashortcut);
851 docstring const latex2 = encoding.latexChar(c).first;
852 if (docstring(1, next) == latex1) {
853 // the encoding supports the combination
854 os << latex2 << latex1;
855 return latex1.length() + latex2.length();
856 } else if (runparams.local_font &&
857 runparams.local_font->language()->lang() == "polutonikogreek") {
858 // polutonikogreek only works without the brackets
859 os << latex1 << latex2;
860 return latex1.length() + latex2.length();
862 os << latex1 << '{' << latex2 << '}';
863 return latex1.length() + latex2.length() + 2;
867 bool Paragraph::Private::simpleTeXBlanks(OutputParams const & runparams,
870 unsigned int & column,
872 Layout const & style)
874 if (style.pass_thru || runparams.pass_thru)
877 if (i + 1 < int(text_.size())) {
878 char_type next = text_[i + 1];
879 if (Encodings::isCombiningChar(next)) {
880 // This space has an accent, so we must always output it.
881 column += latexSurrogatePair(os, ' ', next, runparams) - 1;
886 if (runparams.linelen > 0
887 && column > runparams.linelen
889 && text_[i - 1] != ' '
890 && (i + 1 < int(text_.size()))
891 // same in FreeSpacing mode
892 && !owner_->isFreeSpacing()
893 // In typewriter mode, we want to avoid
894 // ! . ? : at the end of a line
895 && !(font.fontInfo().family() == TYPEWRITER_FAMILY
896 && (text_[i - 1] == '.'
897 || text_[i - 1] == '?'
898 || text_[i - 1] == ':'
899 || text_[i - 1] == '!'))) {
901 os.texrow().start(owner_->id(), i + 1);
903 } else if (style.free_spacing) {
912 int Paragraph::Private::writeScriptChars(otexstream & os,
913 docstring const & ltx,
914 Change const & runningChange,
915 Encoding const & encoding,
918 // FIXME: modifying i here is not very nice...
920 // We only arrive here when character text_[i] could not be translated
921 // into the current latex encoding (or its latex translation has been forced,)
922 // and it belongs to a known script.
923 // TODO: We need \textcyr and \textgreek wrappers also for characters
924 // that can be encoded in the "LaTeX encoding" but not in the
925 // current *font encoding*.
926 // (See #9681 for details and test)
927 // Parameter ltx contains the latex translation of text_[i] as specified
928 // in the unicodesymbols file and is something like "\textXXX{<spec>}".
929 // The latex macro name "textXXX" specifies the script to which text_[i]
930 // belongs and we use it in order to check whether characters from the
931 // same script immediately follow, such that we can collect them in a
932 // single "\textXXX" macro. So, we have to retain "\textXXX{<spec>"
933 // for the first char but only "<spec>" for all subsequent chars.
934 docstring::size_type const brace1 = ltx.find_first_of(from_ascii("{"));
935 docstring::size_type const brace2 = ltx.find_last_of(from_ascii("}"));
936 string script = to_ascii(ltx.substr(1, brace1 - 1));
939 bool closing_brace = true;
940 if (script == "textgreek" && encoding.latexName() == "iso-8859-7") {
941 // Correct encoding is being used, so we can avoid \textgreek.
942 // TODO: wrong test: we need to check the *font encoding*
943 // (i.e. the active language and its FontEncoding tag)
944 // instead of the LaTeX *input encoding*!
945 // See #9637 for details and test-cases.
948 closing_brace = false;
950 os << ltx.substr(pos, length);
951 int size = text_.size();
952 while (i + 1 < size) {
953 char_type const next = text_[i + 1];
954 // Stop here if next character belongs to another script
955 // or there is a change in change tracking status.
956 if (!Encodings::isKnownScriptChar(next, script) ||
957 runningChange != owner_->lookupChange(i + 1))
961 FontList::const_iterator cit = fontlist_.begin();
962 FontList::const_iterator end = fontlist_.end();
963 for (; cit != end; ++cit) {
964 if (cit->pos() >= i && !found) {
965 prev_font = cit->font();
968 if (cit->pos() >= i + 1)
971 // Stop here if there is a font attribute or encoding change.
972 if (found && cit != end && prev_font != cit->font())
974 docstring const latex = encoding.latexChar(next).first;
975 docstring::size_type const b1 =
976 latex.find_first_of(from_ascii("{"));
977 docstring::size_type const b2 =
978 latex.find_last_of(from_ascii("}"));
979 int const len = b2 - b1 - 1;
980 os << latex.substr(b1 + 1, len);
992 void Paragraph::Private::latexInset(BufferParams const & bparams,
994 OutputParams & runparams,
997 Font const & outerfont,
999 Change & running_change,
1000 Layout const & style,
1002 unsigned int & column)
1004 Inset * inset = owner_->getInset(i);
1007 if (style.pass_thru) {
1008 odocstringstream ods;
1009 inset->plaintext(ods, runparams);
1014 // FIXME: move this to InsetNewline::latex
1015 if (inset->lyxCode() == NEWLINE_CODE || inset->lyxCode() == SEPARATOR_CODE) {
1016 // newlines are handled differently here than
1017 // the default in simpleTeXSpecialChars().
1018 if (!style.newline_allowed) {
1022 column += running_font.latexWriteEndChanges(
1023 os, bparams, runparams,
1024 basefont, basefont);
1028 if (running_font.fontInfo().family() == TYPEWRITER_FAMILY)
1031 basefont = owner_->getLayoutFont(bparams, outerfont);
1032 running_font = basefont;
1034 if (runparams.moving_arg)
1038 os.texrow().start(owner_->id(), i + 1);
1042 if (owner_->isDeleted(i)) {
1043 if( ++runparams.inDeletedInset == 1)
1044 runparams.changeOfDeletedInset = owner_->lookupChange(i);
1047 if (inset->canTrackChanges()) {
1048 column += Changes::latexMarkChange(os, bparams, running_change,
1049 Change(Change::UNCHANGED), runparams);
1050 running_change = Change(Change::UNCHANGED);
1054 odocstream::pos_type const len = os.os().tellp();
1056 if (inset->forceLTR()
1057 && !runparams.use_polyglossia
1058 && running_font.isRightToLeft()
1059 // ERT is an exception, it should be output with no
1060 // decorations at all
1061 && inset->lyxCode() != ERT_CODE) {
1062 if (running_font.language()->lang() == "farsi")
1069 // FIXME: Bug: we can have an empty font change here!
1070 // if there has just been a font change, we are going to close it
1071 // right now, which means stupid latex code like \textsf{}. AFAIK,
1072 // this does not harm dvi output. A minor bug, thus (JMarc)
1074 // Some insets cannot be inside a font change command.
1075 // However, even such insets *can* be placed in \L or \R
1076 // or their equivalents (for RTL language switches), so we don't
1077 // close the language in those cases.
1078 // ArabTeX, though, cannot handle this special behavior, it seems.
1079 bool arabtex = basefont.language()->lang() == "arabic_arabtex"
1080 || running_font.language()->lang() == "arabic_arabtex";
1081 if (open_font && !inset->inheritFont()) {
1082 bool closeLanguage = arabtex
1083 || basefont.isRightToLeft() == running_font.isRightToLeft();
1084 unsigned int count = running_font.latexWriteEndChanges(os,
1085 bparams, runparams, basefont, basefont, closeLanguage);
1087 // if any font properties were closed, update the running_font,
1088 // making sure, however, to leave the language as it was
1090 // FIXME: probably a better way to keep track of the old
1091 // language, than copying the entire font?
1092 Font const copy_font(running_font);
1093 basefont = owner_->getLayoutFont(bparams, outerfont);
1094 running_font = basefont;
1096 running_font.setLanguage(copy_font.language());
1097 // leave font open if language is still open
1098 open_font = (running_font.language() == basefont.language());
1100 runparams.local_font = &basefont;
1104 int prev_rows = os.texrow().rows();
1107 runparams.lastid = id_;
1108 runparams.lastpos = i;
1109 inset->latex(os, runparams);
1110 } catch (EncodingException & e) {
1111 // add location information and throw again.
1118 if (running_font.language()->lang() == "farsi")
1124 if (os.texrow().rows() > prev_rows) {
1125 os.texrow().start(owner_->id(), i + 1);
1128 column += (unsigned int)(os.os().tellp() - len);
1131 if (owner_->isDeleted(i))
1132 --runparams.inDeletedInset;
1136 void Paragraph::Private::latexSpecialChar(otexstream & os,
1137 BufferParams const & bparams,
1138 OutputParams const & runparams,
1139 Font const & running_font,
1140 Change const & running_change,
1141 Layout const & style,
1144 unsigned int & column)
1146 // With polyglossia, brackets and stuff need not be reversed
1147 // in RTL scripts (see bug #8251)
1148 char_type const c = (runparams.use_polyglossia) ?
1149 owner_->getUChar(bparams, i) : text_[i];
1151 if (style.pass_thru || runparams.pass_thru
1152 || contains(style.pass_thru_chars, c)
1153 || contains(runparams.pass_thru_chars, c)) {
1155 Encoding const * const enc = runparams.encoding;
1156 if (enc && !enc->encodable(c))
1157 throw EncodingException(c);
1163 // TIPA uses its own T3 encoding
1164 if (runparams.inIPA && latexSpecialT3(c, os, i, column))
1166 // If T1 font encoding is used, use the special
1167 // characters it provides.
1168 // NOTE: Some languages reset the font encoding internally to a
1169 // non-standard font encoding. If we are using such a language,
1170 // we do not output special T1 chars.
1171 if (!runparams.inIPA && !running_font.language()->internalFontEncoding()
1172 && bparams.font_encoding() == "T1" && latexSpecialT1(c, os, i, column))
1175 // Otherwise, we use what LaTeX provides us.
1178 os << "\\textbackslash{}";
1182 os << "\\textless{}";
1186 os << "\\textgreater{}";
1190 os << "\\textbar{}";
1195 if (i + 1 < end_pos && text_[i+1] == '-') {
1196 // Prevent "--" becoming an endash and "---" becoming
1198 // Within \ttfamily, "--" is merged to "-" (no endash)
1199 // so we avoid this rather irritating ligature as well
1205 os << "\\char`\\\"{}";
1210 case '%': case '#': case '{':
1218 os << "\\textasciitilde{}";
1223 os << "\\textasciicircum{}";
1230 // avoid being mistaken for optional arguments
1238 // Blanks are printed before font switching.
1239 // Sure? I am not! (try nice-latex)
1240 // I am sure it's correct. LyX might be smarter
1241 // in the future, but for now, nothing wrong is
1249 Encoding const & encoding = *(runparams.encoding);
1250 char_type next = '\0';
1251 if (i + 1 < int(text_.size())) {
1252 next = text_[i + 1];
1253 if (Encodings::isCombiningChar(next)) {
1254 column += latexSurrogatePair(os, c, next, runparams) - 1;
1260 pair<docstring, bool> latex = encoding.latexChar(c);
1261 docstring nextlatex;
1262 bool nexttipas = false;
1263 string nexttipashortcut;
1264 if (next != '\0' && next != META_INSET && encoding.encodable(next)) {
1265 nextlatex = encoding.latexChar(next).first;
1266 if (runparams.inIPA) {
1267 nexttipashortcut = Encodings::TIPAShortcut(next);
1268 nexttipas = !nexttipashortcut.empty();
1272 if (runparams.inIPA) {
1273 string const tipashortcut = Encodings::TIPAShortcut(c);
1274 if (!tipashortcut.empty()) {
1275 latex.first = from_ascii(tipashortcut);
1276 latex.second = false;
1280 if (Encodings::isKnownScriptChar(c, script)
1281 && prefixIs(latex.first, from_ascii("\\" + script)))
1282 column += writeScriptChars(os, latex.first,
1283 running_change, encoding, i) - 1;
1284 else if (latex.second
1285 && ((!prefixIs(nextlatex, '\\')
1286 && !prefixIs(nextlatex, '{')
1287 && !prefixIs(nextlatex, '}'))
1289 && !prefixIs(from_ascii(nexttipashortcut), '\\')))
1291 // Prevent eating of a following
1292 // space or command corruption by
1293 // following characters
1294 if (next == ' ' || next == '\0') {
1295 column += latex.first.length() + 1;
1296 os << latex.first << "{}";
1298 column += latex.first.length();
1299 os << latex.first << " ";
1302 column += latex.first.length() - 1;
1310 bool Paragraph::Private::latexSpecialT1(char_type const c, otexstream & os,
1311 pos_type i, unsigned int & column)
1317 // In T1 encoding, these characters exist
1318 // but we should avoid ligatures
1319 if (i + 1 >= int(text_.size()) || text_[i + 1] != c)
1321 os << "\\textcompwordmark{}";
1328 // soul.sty breaks with \char`\"
1329 os << "\\textquotedbl{}";
1338 bool Paragraph::Private::latexSpecialT3(char_type const c, otexstream & os,
1339 pos_type /*i*/, unsigned int & column)
1349 os << "\\textvertline{}";
1358 void Paragraph::Private::validate(LaTeXFeatures & features) const
1360 if (layout_->inpreamble && inset_owner_) {
1361 bool const is_command = layout_->latextype == LATEX_COMMAND;
1362 Buffer const & buf = inset_owner_->buffer();
1363 BufferParams const & bp = features.runparams().is_child
1364 ? buf.masterParams() : buf.params();
1367 // Using a string stream here circumvents the encoding
1368 // switching machinery of odocstream. Therefore the
1369 // output is wrong if this paragraph contains content
1370 // that needs to switch encoding.
1371 odocstringstream ods;
1372 otexstream os(ods, texrow);
1374 os << '\\' << from_ascii(layout_->latexname());
1375 // we have to provide all the optional arguments here, even though
1376 // the last one is the only one we care about.
1377 // Separate handling of optional argument inset.
1378 if (!layout_->latexargs().empty()) {
1379 OutputParams rp = features.runparams();
1380 rp.local_font = &owner_->getFirstFontSettings(bp);
1381 latexArgInsets(*owner_, os, rp, layout_->latexargs());
1383 os << from_ascii(layout_->latexparam());
1385 docstring::size_type const length = ods.str().length();
1386 // this will output "{" at the beginning, but not at the end
1387 owner_->latex(bp, f, os, features.runparams(), 0, -1, true);
1388 if (ods.str().length() > length) {
1391 if (!layout_->postcommandargs().empty()) {
1392 OutputParams rp = features.runparams();
1393 rp.local_font = &owner_->getFirstFontSettings(bp);
1394 latexArgInsets(*owner_, os, rp, layout_->postcommandargs(), "post:");
1397 string const snippet = to_utf8(ods.str());
1398 features.addPreambleSnippet(snippet);
1402 if (features.runparams().flavor == OutputParams::HTML
1403 && layout_->htmltitle()) {
1404 features.setHTMLTitle(owner_->asString(AS_STR_INSETS | AS_STR_SKIPDELETE));
1407 // check the params.
1408 if (!params_.spacing().isDefault())
1409 features.require("setspace");
1412 features.useLayout(layout_->name());
1415 fontlist_.validate(features);
1417 // then the indentation
1418 if (!params_.leftIndent().zero())
1419 features.require("ParagraphLeftIndent");
1422 InsetList::const_iterator icit = insetlist_.begin();
1423 InsetList::const_iterator iend = insetlist_.end();
1424 for (; icit != iend; ++icit) {
1426 icit->inset->validate(features);
1427 if (layout_->needprotect &&
1428 icit->inset->lyxCode() == FOOT_CODE)
1429 features.require("NeedLyXFootnoteCode");
1433 // then the contents
1434 for (pos_type i = 0; i < int(text_.size()) ; ++i) {
1435 BufferEncodings::validate(text_[i], features);
1439 /////////////////////////////////////////////////////////////////////
1443 /////////////////////////////////////////////////////////////////////
1446 Layout const emptyParagraphLayout;
1449 Paragraph::Paragraph()
1450 : d(new Paragraph::Private(this, emptyParagraphLayout))
1457 Paragraph::Paragraph(Paragraph const & par)
1458 : itemdepth(par.itemdepth),
1459 d(new Paragraph::Private(*par.d, this))
1465 Paragraph::Paragraph(Paragraph const & par, pos_type beg, pos_type end)
1466 : itemdepth(par.itemdepth),
1467 d(new Paragraph::Private(*par.d, this, beg, end))
1473 Paragraph & Paragraph::operator=(Paragraph const & par)
1475 // needed as we will destroy the private part before copying it
1477 itemdepth = par.itemdepth;
1481 d = new Private(*par.d, this);
1488 Paragraph::~Paragraph()
1497 // this shall be called just before every "os << ..." action.
1498 void flushString(ostream & os, docstring & s)
1507 void Paragraph::write(ostream & os, BufferParams const & bparams,
1508 depth_type & dth) const
1510 // The beginning or end of a deeper (i.e. nested) area?
1511 if (dth != d->params_.depth()) {
1512 if (d->params_.depth() > dth) {
1513 while (d->params_.depth() > dth) {
1514 os << "\n\\begin_deeper";
1518 while (d->params_.depth() < dth) {
1519 os << "\n\\end_deeper";
1525 // First write the layout
1526 os << "\n\\begin_layout " << to_utf8(d->layout_->name()) << '\n';
1528 d->params_.write(os);
1530 Font font1(inherit_font, bparams.language);
1532 Change running_change = Change(Change::UNCHANGED);
1534 // this string is used as a buffer to avoid repetitive calls
1535 // to to_utf8(), which turn out to be expensive (JMarc)
1536 docstring write_buffer;
1539 for (pos_type i = 0; i <= size(); ++i) {
1541 Change const & change = lookupChange(i);
1542 if (change != running_change)
1543 flushString(os, write_buffer);
1544 Changes::lyxMarkChange(os, bparams, column, running_change, change);
1545 running_change = change;
1550 // Write font changes
1551 Font font2 = getFontSettings(bparams, i);
1552 if (font2 != font1) {
1553 flushString(os, write_buffer);
1554 font2.lyxWriteChanges(font1, os);
1559 char_type const c = d->text_[i];
1562 if (Inset const * inset = getInset(i)) {
1563 flushString(os, write_buffer);
1564 if (inset->directWrite()) {
1565 // international char, let it write
1566 // code directly so it's shorter in
1572 os << "\\begin_inset ";
1574 os << "\n\\end_inset\n\n";
1577 // FIXME This can be removed again once the mystery
1578 // crash has been resolved.
1583 flushString(os, write_buffer);
1584 os << "\n\\backslash\n";
1588 flushString(os, write_buffer);
1589 if (i + 1 < size() && d->text_[i + 1] == ' ') {
1596 if ((column > 70 && c == ' ')
1598 flushString(os, write_buffer);
1602 // this check is to amend a bug. LyX sometimes
1603 // inserts '\0' this could cause problems.
1605 write_buffer.push_back(c);
1607 LYXERR0("NUL char in structure.");
1613 flushString(os, write_buffer);
1614 os << "\n\\end_layout\n";
1615 // FIXME This can be removed again once the mystery
1616 // crash has been resolved.
1621 void Paragraph::validate(LaTeXFeatures & features) const
1623 d->validate(features);
1627 void Paragraph::insert(pos_type start, docstring const & str,
1628 Font const & font, Change const & change)
1630 for (size_t i = 0, n = str.size(); i != n ; ++i)
1631 insertChar(start + i, str[i], font, change);
1635 void Paragraph::appendChar(char_type c, Font const & font,
1636 Change const & change)
1639 d->changes_.insert(change, d->text_.size());
1640 // when appending characters, no need to update tables
1641 d->text_.push_back(c);
1642 setFont(d->text_.size() - 1, font);
1643 d->requestSpellCheck(d->text_.size() - 1);
1647 void Paragraph::appendString(docstring const & s, Font const & font,
1648 Change const & change)
1650 pos_type end = s.size();
1651 size_t oldsize = d->text_.size();
1652 size_t newsize = oldsize + end;
1653 size_t capacity = d->text_.capacity();
1654 if (newsize >= capacity)
1655 d->text_.reserve(max(capacity + 100, newsize));
1657 // when appending characters, no need to update tables
1660 // FIXME: Optimize this!
1661 for (size_t i = oldsize; i != newsize; ++i) {
1663 d->changes_.insert(change, i);
1664 d->requestSpellCheck(i);
1666 d->fontlist_.set(oldsize, font);
1667 d->fontlist_.set(newsize - 1, font);
1671 void Paragraph::insertChar(pos_type pos, char_type c,
1674 d->insertChar(pos, c, Change(trackChanges ?
1675 Change::INSERTED : Change::UNCHANGED));
1679 void Paragraph::insertChar(pos_type pos, char_type c,
1680 Font const & font, bool trackChanges)
1682 d->insertChar(pos, c, Change(trackChanges ?
1683 Change::INSERTED : Change::UNCHANGED));
1688 void Paragraph::insertChar(pos_type pos, char_type c,
1689 Font const & font, Change const & change)
1691 d->insertChar(pos, c, change);
1696 void Paragraph::resetFonts(Font const & font)
1698 d->fontlist_.clear();
1699 d->fontlist_.set(0, font);
1700 d->fontlist_.set(d->text_.size() - 1, font);
1703 // Gets uninstantiated font setting at position.
1704 Font const & Paragraph::getFontSettings(BufferParams const & bparams,
1708 LYXERR0("pos: " << pos << " size: " << size());
1712 FontList::const_iterator cit = d->fontlist_.fontIterator(pos);
1713 if (cit != d->fontlist_.end())
1716 if (pos == size() && !empty())
1717 return getFontSettings(bparams, pos - 1);
1719 // Optimisation: avoid a full font instantiation if there is no
1720 // language change from previous call.
1721 static Font previous_font;
1722 static Language const * previous_lang = 0;
1723 Language const * lang = getParLanguage(bparams);
1724 if (lang != previous_lang) {
1725 previous_lang = lang;
1726 previous_font = Font(inherit_font, lang);
1728 return previous_font;
1732 FontSpan Paragraph::fontSpan(pos_type pos) const
1734 LBUFERR(pos <= size());
1737 return FontSpan(pos, pos);
1740 FontList::const_iterator cit = d->fontlist_.begin();
1741 FontList::const_iterator end = d->fontlist_.end();
1742 for (; cit != end; ++cit) {
1743 if (cit->pos() >= pos) {
1744 if (pos >= beginOfBody())
1745 return FontSpan(max(start, beginOfBody()),
1748 return FontSpan(start,
1749 min(beginOfBody() - 1,
1752 start = cit->pos() + 1;
1755 // This should not happen, but if so, we take no chances.
1756 LYXERR0("Paragraph::fontSpan: position not found in fontinfo table!");
1757 LASSERT(false, return FontSpan(pos, pos));
1761 // Gets uninstantiated font setting at position 0
1762 Font const & Paragraph::getFirstFontSettings(BufferParams const & bparams) const
1764 if (!empty() && !d->fontlist_.empty())
1765 return d->fontlist_.begin()->font();
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 = 0;
1771 if (bparams.language != previous_lang) {
1772 previous_lang = bparams.language;
1773 previous_font = Font(inherit_font, bparams.language);
1776 return previous_font;
1780 // Gets the fully instantiated font at a given position in a paragraph
1781 // This is basically the same function as Text::GetFont() in text2.cpp.
1782 // The difference is that this one is used for generating the LaTeX file,
1783 // and thus cosmetic "improvements" are disallowed: This has to deliver
1784 // the true picture of the buffer. (Asger)
1785 Font const Paragraph::getFont(BufferParams const & bparams, pos_type pos,
1786 Font const & outerfont) const
1790 Font font = getFontSettings(bparams, pos);
1792 pos_type const body_pos = beginOfBody();
1793 FontInfo & fi = font.fontInfo();
1795 fi.realize(d->layout_->labelfont);
1797 fi.realize(d->layout_->font);
1799 fi.realize(outerfont.fontInfo());
1800 fi.realize(bparams.getFont().fontInfo());
1806 Font const Paragraph::getLabelFont
1807 (BufferParams const & bparams, Font const & outerfont) const
1809 FontInfo tmpfont = d->layout_->labelfont;
1810 tmpfont.realize(outerfont.fontInfo());
1811 tmpfont.realize(bparams.getFont().fontInfo());
1812 return Font(tmpfont, getParLanguage(bparams));
1816 Font const Paragraph::getLayoutFont
1817 (BufferParams const & bparams, Font const & outerfont) const
1819 FontInfo tmpfont = d->layout_->font;
1820 tmpfont.realize(outerfont.fontInfo());
1821 tmpfont.realize(bparams.getFont().fontInfo());
1822 return Font(tmpfont, getParLanguage(bparams));
1826 /// Returns the height of the highest font in range
1827 FontSize Paragraph::highestFontInRange
1828 (pos_type startpos, pos_type endpos, FontSize def_size) const
1830 return d->fontlist_.highestInRange(startpos, endpos, def_size);
1834 char_type Paragraph::getUChar(BufferParams const & bparams, pos_type pos) const
1836 char_type c = d->text_[pos];
1837 if (!getFontSettings(bparams, pos).isRightToLeft())
1840 // FIXME: The arabic special casing is due to the difference of arabic
1841 // round brackets input introduced in r18599. Check if this should be
1842 // unified with Hebrew or at least if all bracket types should be
1843 // handled the same (file format change in either case).
1844 string const & lang = getFontSettings(bparams, pos).language()->lang();
1845 bool const arabic = lang == "arabic_arabtex" || lang == "arabic_arabi"
1850 uc = arabic ? c : ')';
1853 uc = arabic ? c : '(';
1879 void Paragraph::setFont(pos_type pos, Font const & font)
1881 LASSERT(pos <= size(), return);
1883 // First, reduce font against layout/label font
1884 // Update: The setCharFont() routine in text2.cpp already
1885 // reduces font, so we don't need to do that here. (Asger)
1887 d->fontlist_.set(pos, font);
1891 void Paragraph::makeSameLayout(Paragraph const & par)
1893 d->layout_ = par.d->layout_;
1894 d->params_ = par.d->params_;
1898 bool Paragraph::stripLeadingSpaces(bool trackChanges)
1900 if (isFreeSpacing())
1906 while (pos < size() && (isNewline(pos) || isLineSeparator(pos))) {
1907 if (eraseChar(pos, trackChanges))
1913 return count > 0 || pos > 0;
1917 bool Paragraph::hasSameLayout(Paragraph const & par) const
1919 return par.d->layout_ == d->layout_
1920 && d->params_.sameLayout(par.d->params_);
1924 depth_type Paragraph::getDepth() const
1926 return d->params_.depth();
1930 depth_type Paragraph::getMaxDepthAfter() const
1932 if (d->layout_->isEnvironment())
1933 return d->params_.depth() + 1;
1935 return d->params_.depth();
1939 char Paragraph::getAlign() const
1941 if (d->params_.align() == LYX_ALIGN_LAYOUT)
1942 return d->layout_->align;
1944 return d->params_.align();
1948 docstring const & Paragraph::labelString() const
1950 return d->params_.labelString();
1954 // the next two functions are for the manual labels
1955 docstring const Paragraph::getLabelWidthString() const
1957 if (d->layout_->margintype == MARGIN_MANUAL
1958 || d->layout_->latextype == LATEX_BIB_ENVIRONMENT)
1959 return d->params_.labelWidthString();
1961 return _("Senseless with this layout!");
1965 void Paragraph::setLabelWidthString(docstring const & s)
1967 d->params_.labelWidthString(s);
1971 docstring Paragraph::expandLabel(Layout const & layout,
1972 BufferParams const & bparams) const
1974 return expandParagraphLabel(layout, bparams, true);
1978 docstring Paragraph::expandDocBookLabel(Layout const & layout,
1979 BufferParams const & bparams) const
1981 return expandParagraphLabel(layout, bparams, false);
1985 docstring Paragraph::expandParagraphLabel(Layout const & layout,
1986 BufferParams const & bparams, bool process_appendix) const
1988 DocumentClass const & tclass = bparams.documentClass();
1989 string const & lang = getParLanguage(bparams)->code();
1990 bool const in_appendix = process_appendix && d->params_.appendix();
1991 docstring fmt = translateIfPossible(layout.labelstring(in_appendix), lang);
1993 if (fmt.empty() && !layout.counter.empty())
1994 return tclass.counters().theCounter(layout.counter, lang);
1996 // handle 'inherited level parts' in 'fmt',
1997 // i.e. the stuff between '@' in '@Section@.\arabic{subsection}'
1998 size_t const i = fmt.find('@', 0);
1999 if (i != docstring::npos) {
2000 size_t const j = fmt.find('@', i + 1);
2001 if (j != docstring::npos) {
2002 docstring parent(fmt, i + 1, j - i - 1);
2003 docstring label = from_ascii("??");
2004 if (tclass.hasLayout(parent))
2005 label = expandParagraphLabel(tclass[parent], bparams,
2007 fmt = docstring(fmt, 0, i) + label
2008 + docstring(fmt, j + 1, docstring::npos);
2012 return tclass.counters().counterLabel(fmt, lang);
2016 void Paragraph::applyLayout(Layout const & new_layout)
2018 d->layout_ = &new_layout;
2019 LyXAlignment const oldAlign = d->params_.align();
2021 if (!(oldAlign & d->layout_->alignpossible)) {
2022 frontend::Alert::warning(_("Alignment not permitted"),
2023 _("The new layout does not permit the alignment previously used.\nSetting to default."));
2024 d->params_.align(LYX_ALIGN_LAYOUT);
2029 pos_type Paragraph::beginOfBody() const
2031 return d->begin_of_body_;
2035 void Paragraph::setBeginOfBody()
2037 if (d->layout_->labeltype != LABEL_MANUAL) {
2038 d->begin_of_body_ = 0;
2042 // Unroll the first two cycles of the loop
2043 // and remember the previous character to
2044 // remove unnecessary getChar() calls
2046 pos_type end = size();
2047 if (i < end && !(isNewline(i) || isEnvSeparator(i))) {
2050 char_type previous_char = d->text_[i];
2051 if (!(isNewline(i) || isEnvSeparator(i))) {
2053 while (i < end && previous_char != ' ') {
2054 char_type temp = d->text_[i];
2055 if (isNewline(i) || isEnvSeparator(i))
2058 previous_char = temp;
2064 d->begin_of_body_ = i;
2068 bool Paragraph::allowParagraphCustomization() const
2070 return inInset().allowParagraphCustomization();
2074 bool Paragraph::usePlainLayout() const
2076 return inInset().usePlainLayout();
2080 bool Paragraph::isPassThru() const
2082 return inInset().isPassThru() || d->layout_->pass_thru;
2087 // paragraphs inside floats need different alignment tags to avoid
2090 bool noTrivlistCentering(InsetCode code)
2092 return code == FLOAT_CODE
2093 || code == WRAP_CODE
2094 || code == CELL_CODE;
2098 string correction(string const & orig)
2100 if (orig == "flushleft")
2101 return "raggedright";
2102 if (orig == "flushright")
2103 return "raggedleft";
2104 if (orig == "center")
2110 bool corrected_env(otexstream & os, string const & suffix, string const & env,
2111 InsetCode code, bool const lastpar, int & col)
2113 string macro = suffix + "{";
2114 if (noTrivlistCentering(code)) {
2116 // the last paragraph in non-trivlist-aligned
2117 // context is special (to avoid unwanted whitespace)
2118 if (suffix == "\\begin") {
2119 macro = "\\" + correction(env) + "{}";
2120 os << from_ascii(macro);
2121 col += macro.size();
2126 macro += correction(env);
2130 if (suffix == "\\par\\end") {
2134 os << from_ascii(macro);
2135 col += macro.size();
2136 if (suffix == "\\begin") {
2146 int Paragraph::Private::startTeXParParams(BufferParams const & bparams,
2147 otexstream & os, OutputParams const & runparams) const
2152 (bparams.paragraph_separation == BufferParams::ParagraphIndentSeparation) ?
2153 (layout_->toggle_indent != ITOGGLE_NEVER) :
2154 (layout_->toggle_indent == ITOGGLE_ALWAYS);
2156 if (canindent && params_.noindent() && !layout_->pass_thru) {
2157 os << "\\noindent ";
2161 LyXAlignment const curAlign = params_.align();
2163 if (curAlign == layout_->align)
2167 case LYX_ALIGN_NONE:
2168 case LYX_ALIGN_BLOCK:
2169 case LYX_ALIGN_LAYOUT:
2170 case LYX_ALIGN_SPECIAL:
2171 case LYX_ALIGN_DECIMAL:
2173 case LYX_ALIGN_LEFT:
2174 case LYX_ALIGN_RIGHT:
2175 case LYX_ALIGN_CENTER:
2176 if (runparams.moving_arg) {
2183 string const begin_tag = "\\begin";
2184 InsetCode code = ownerCode();
2185 bool const lastpar = runparams.isLastPar;
2188 case LYX_ALIGN_NONE:
2189 case LYX_ALIGN_BLOCK:
2190 case LYX_ALIGN_LAYOUT:
2191 case LYX_ALIGN_SPECIAL:
2192 case LYX_ALIGN_DECIMAL:
2194 case LYX_ALIGN_LEFT: {
2195 if (owner_->getParLanguage(bparams)->babel() != "hebrew")
2196 corrected_env(os, begin_tag, "flushleft", code, lastpar, column);
2198 corrected_env(os, begin_tag, "flushright", code, lastpar, column);
2200 } case LYX_ALIGN_RIGHT: {
2201 if (owner_->getParLanguage(bparams)->babel() != "hebrew")
2202 corrected_env(os, begin_tag, "flushright", code, lastpar, column);
2204 corrected_env(os, begin_tag, "flushleft", code, lastpar, column);
2206 } case LYX_ALIGN_CENTER: {
2207 corrected_env(os, begin_tag, "center", code, lastpar, column);
2216 bool Paragraph::Private::endTeXParParams(BufferParams const & bparams,
2217 otexstream & os, OutputParams const & runparams) const
2219 LyXAlignment const curAlign = params_.align();
2221 if (curAlign == layout_->align)
2225 case LYX_ALIGN_NONE:
2226 case LYX_ALIGN_BLOCK:
2227 case LYX_ALIGN_LAYOUT:
2228 case LYX_ALIGN_SPECIAL:
2229 case LYX_ALIGN_DECIMAL:
2231 case LYX_ALIGN_LEFT:
2232 case LYX_ALIGN_RIGHT:
2233 case LYX_ALIGN_CENTER:
2234 if (runparams.moving_arg)
2239 bool output = false;
2241 string const end_tag = "\\par\\end";
2242 InsetCode code = ownerCode();
2243 bool const lastpar = runparams.isLastPar;
2246 case LYX_ALIGN_NONE:
2247 case LYX_ALIGN_BLOCK:
2248 case LYX_ALIGN_LAYOUT:
2249 case LYX_ALIGN_SPECIAL:
2250 case LYX_ALIGN_DECIMAL:
2252 case LYX_ALIGN_LEFT: {
2253 if (owner_->getParLanguage(bparams)->babel() != "hebrew")
2254 output = corrected_env(os, end_tag, "flushleft", code, lastpar, col);
2256 output = corrected_env(os, end_tag, "flushright", code, lastpar, col);
2258 } case LYX_ALIGN_RIGHT: {
2259 if (owner_->getParLanguage(bparams)->babel() != "hebrew")
2260 output = corrected_env(os, end_tag, "flushright", code, lastpar, col);
2262 output = corrected_env(os, end_tag, "flushleft", code, lastpar, col);
2264 } case LYX_ALIGN_CENTER: {
2265 corrected_env(os, end_tag, "center", code, lastpar, col);
2270 return output || lastpar;
2274 // This one spits out the text of the paragraph
2275 void Paragraph::latex(BufferParams const & bparams,
2276 Font const & outerfont,
2278 OutputParams const & runparams,
2279 int start_pos, int end_pos, bool force) const
2281 LYXERR(Debug::LATEX, "Paragraph::latex... " << this);
2283 // FIXME This check should not be needed. Perhaps issue an
2284 // error if it triggers.
2285 Layout const & style = inInset().forcePlainLayout() ?
2286 bparams.documentClass().plainLayout() : *d->layout_;
2288 if (!force && style.inpreamble)
2291 bool const allowcust = allowParagraphCustomization();
2293 // Current base font for all inherited font changes, without any
2294 // change caused by an individual character, except for the language:
2295 // It is set to the language of the first character.
2296 // As long as we are in the label, this font is the base font of the
2297 // label. Before the first body character it is set to the base font
2301 // Maybe we have to create a optional argument.
2302 pos_type body_pos = beginOfBody();
2303 unsigned int column = 0;
2306 // the optional argument is kept in curly brackets in
2307 // case it contains a ']'
2308 // This is not strictly needed, but if this is changed it
2309 // would be a file format change, and tex2lyx would need
2310 // to be adjusted, since it unconditionally removes the
2311 // braces when it parses \item.
2314 basefont = getLabelFont(bparams, outerfont);
2316 basefont = getLayoutFont(bparams, outerfont);
2319 // Which font is currently active?
2320 Font running_font(basefont);
2321 // Do we have an open font change?
2322 bool open_font = false;
2324 Change runningChange = Change(Change::UNCHANGED);
2326 Encoding const * const prev_encoding = runparams.encoding;
2328 os.texrow().start(id(), 0);
2330 // if the paragraph is empty, the loop will not be entered at all
2332 if (style.isCommand()) {
2336 if (!style.leftdelim().empty()) {
2337 os << style.leftdelim();
2338 column += style.leftdelim().size();
2341 column += d->startTeXParParams(bparams, os, runparams);
2344 for (pos_type i = 0; i < size(); ++i) {
2345 // First char in paragraph or after label?
2346 if (i == body_pos) {
2349 column += running_font.latexWriteEndChanges(
2350 os, bparams, runparams,
2351 basefont, basefont);
2354 basefont = getLayoutFont(bparams, outerfont);
2355 running_font = basefont;
2357 column += Changes::latexMarkChange(os, bparams,
2358 runningChange, Change(Change::UNCHANGED),
2360 runningChange = Change(Change::UNCHANGED);
2365 if (style.isCommand()) {
2370 if (!style.leftdelim().empty()) {
2371 os << style.leftdelim();
2372 column += style.leftdelim().size();
2376 column += d->startTeXParParams(bparams, os,
2380 Change const & change = runparams.inDeletedInset
2381 ? runparams.changeOfDeletedInset : lookupChange(i);
2383 if (bparams.output_changes && runningChange != change) {
2385 column += running_font.latexWriteEndChanges(
2386 os, bparams, runparams, basefont, basefont);
2389 basefont = getLayoutFont(bparams, outerfont);
2390 running_font = basefont;
2392 column += Changes::latexMarkChange(os, bparams, runningChange,
2394 runningChange = change;
2397 // do not output text which is marked deleted
2398 // if change tracking output is disabled
2399 if (!bparams.output_changes && change.deleted()) {
2405 // Fully instantiated font
2406 Font const font = getFont(bparams, i, outerfont);
2408 Font const last_font = running_font;
2410 // Do we need to close the previous font?
2412 (font != running_font ||
2413 font.language() != running_font.language()))
2415 column += running_font.latexWriteEndChanges(
2416 os, bparams, runparams, basefont,
2417 (i == body_pos-1) ? basefont : font);
2418 running_font = basefont;
2422 string const running_lang = runparams.use_polyglossia ?
2423 running_font.language()->polyglossia() : running_font.language()->babel();
2424 // close babel's font environment before opening CJK.
2425 string const lang_end_command = runparams.use_polyglossia ?
2426 "\\end{$$lang}" : lyxrc.language_command_end;
2427 if (!running_lang.empty() &&
2428 font.language()->encoding()->package() == Encoding::CJK) {
2429 string end_tag = subst(lang_end_command,
2432 os << from_ascii(end_tag);
2433 column += end_tag.length();
2436 // Switch file encoding if necessary (and allowed)
2437 if (!runparams.pass_thru && !style.pass_thru &&
2438 runparams.encoding->package() != Encoding::none &&
2439 font.language()->encoding()->package() != Encoding::none) {
2440 pair<bool, int> const enc_switch =
2441 switchEncoding(os.os(), bparams, runparams,
2442 *(font.language()->encoding()));
2443 if (enc_switch.first) {
2444 column += enc_switch.second;
2445 runparams.encoding = font.language()->encoding();
2449 char_type const c = d->text_[i];
2451 // Do we need to change font?
2452 if ((font != running_font ||
2453 font.language() != running_font.language()) &&
2456 odocstringstream ods;
2457 column += font.latexWriteStartChanges(ods, bparams,
2458 runparams, basefont,
2460 running_font = font;
2462 docstring fontchange = ods.str();
2463 // check whether the fontchange ends with a \\textcolor
2464 // modifier and the text starts with a space (bug 4473)
2465 docstring const last_modifier = rsplit(fontchange, '\\');
2466 if (prefixIs(last_modifier, from_ascii("textcolor")) && c == ' ')
2467 os << fontchange << from_ascii("{}");
2468 // check if the fontchange ends with a trailing blank
2469 // (like "\small " (see bug 3382)
2470 else if (suffixIs(fontchange, ' ') && c == ' ')
2471 os << fontchange.substr(0, fontchange.size() - 1)
2472 << from_ascii("{}");
2477 // FIXME: think about end_pos implementation...
2478 if (c == ' ' && i >= start_pos && (end_pos == -1 || i < end_pos)) {
2479 // FIXME: integrate this case in latexSpecialChar
2480 // Do not print the separation of the optional argument
2481 // if style.pass_thru is false. This works because
2482 // latexSpecialChar ignores spaces if
2483 // style.pass_thru is false.
2484 if (i != body_pos - 1) {
2485 if (d->simpleTeXBlanks(runparams, os,
2486 i, column, font, style)) {
2487 // A surrogate pair was output. We
2488 // must not call latexSpecialChar
2489 // in this iteration, since it would output
2490 // the combining character again.
2497 OutputParams rp = runparams;
2498 rp.free_spacing = style.free_spacing;
2499 rp.local_font = &font;
2500 rp.intitle = style.intitle;
2502 // Two major modes: LaTeX or plain
2503 // Handle here those cases common to both modes
2504 // and then split to handle the two modes separately.
2505 if (c == META_INSET) {
2506 if (i >= start_pos && (end_pos == -1 || i < end_pos)) {
2507 d->latexInset(bparams, os, rp, running_font,
2508 basefont, outerfont, open_font,
2509 runningChange, style, i, column);
2512 if (i >= start_pos && (end_pos == -1 || i < end_pos)) {
2514 d->latexSpecialChar(os, bparams, rp, running_font, runningChange,
2515 style, i, end_pos, column);
2516 } catch (EncodingException & e) {
2517 if (runparams.dryrun) {
2518 os << "<" << _("LyX Warning: ")
2519 << _("uncodable character") << " '";
2523 // add location information and throw again.
2532 // Set the encoding to that returned from latexSpecialChar (see
2533 // comment for encoding member in OutputParams.h)
2534 runparams.encoding = rp.encoding;
2537 // If we have an open font definition, we have to close it
2539 #ifdef FIXED_LANGUAGE_END_DETECTION
2541 running_font.latexWriteEndChanges(os, bparams,
2542 runparams, basefont,
2543 next_->getFont(bparams, 0, outerfont));
2545 running_font.latexWriteEndChanges(os, bparams,
2546 runparams, basefont, basefont);
2549 //FIXME: For now we ALWAYS have to close the foreign font settings if they are
2550 //FIXME: there as we start another \selectlanguage with the next paragraph if
2551 //FIXME: we are in need of this. This should be fixed sometime (Jug)
2552 running_font.latexWriteEndChanges(os, bparams, runparams,
2553 basefont, basefont);
2557 column += Changes::latexMarkChange(os, bparams, runningChange,
2558 Change(Change::UNCHANGED), runparams);
2560 // Needed if there is an optional argument but no contents.
2561 if (body_pos > 0 && body_pos == size()) {
2565 if (!style.rightdelim().empty()) {
2566 os << style.rightdelim();
2567 column += style.rightdelim().size();
2570 if (allowcust && d->endTeXParParams(bparams, os, runparams)
2571 && runparams.encoding != prev_encoding) {
2572 runparams.encoding = prev_encoding;
2573 os << setEncoding(prev_encoding->iconvName());
2576 LYXERR(Debug::LATEX, "Paragraph::latex... done " << this);
2580 bool Paragraph::emptyTag() const
2582 for (pos_type i = 0; i < size(); ++i) {
2583 if (Inset const * inset = getInset(i)) {
2584 InsetCode lyx_code = inset->lyxCode();
2585 // FIXME testing like that is wrong. What is
2587 if (lyx_code != TOC_CODE &&
2588 lyx_code != INCLUDE_CODE &&
2589 lyx_code != GRAPHICS_CODE &&
2590 lyx_code != ERT_CODE &&
2591 lyx_code != LISTINGS_CODE &&
2592 lyx_code != FLOAT_CODE &&
2593 lyx_code != TABULAR_CODE) {
2597 char_type c = d->text_[i];
2598 if (c != ' ' && c != '\t')
2606 string Paragraph::getID(Buffer const & buf, OutputParams const & runparams)
2609 for (pos_type i = 0; i < size(); ++i) {
2610 if (Inset const * inset = getInset(i)) {
2611 InsetCode lyx_code = inset->lyxCode();
2612 if (lyx_code == LABEL_CODE) {
2613 InsetLabel const * const il = static_cast<InsetLabel const *>(inset);
2614 docstring const & id = il->getParam("name");
2615 return "id='" + to_utf8(sgml::cleanID(buf, runparams, id)) + "'";
2623 pos_type Paragraph::firstWordDocBook(odocstream & os, OutputParams const & runparams)
2627 for (i = 0; i < size(); ++i) {
2628 if (Inset const * inset = getInset(i)) {
2629 inset->docbook(os, runparams);
2631 char_type c = d->text_[i];
2634 os << sgml::escapeChar(c);
2641 pos_type Paragraph::firstWordLyXHTML(XHTMLStream & xs, OutputParams const & runparams)
2645 for (i = 0; i < size(); ++i) {
2646 if (Inset const * inset = getInset(i)) {
2647 inset->xhtml(xs, runparams);
2649 char_type c = d->text_[i];
2659 bool Paragraph::Private::onlyText(Buffer const & buf, Font const & outerfont, pos_type initial) const
2662 pos_type size = text_.size();
2663 for (pos_type i = initial; i < size; ++i) {
2664 Font font = owner_->getFont(buf.params(), i, outerfont);
2665 if (text_[i] == META_INSET)
2667 if (i != initial && font != font_old)
2676 void Paragraph::simpleDocBookOnePar(Buffer const & buf,
2678 OutputParams const & runparams,
2679 Font const & outerfont,
2680 pos_type initial) const
2682 bool emph_flag = false;
2684 Layout const & style = *d->layout_;
2686 style.labeltype == LABEL_MANUAL ? style.labelfont : style.font;
2688 if (style.pass_thru && !d->onlyText(buf, outerfont, initial))
2691 // parsing main loop
2692 for (pos_type i = initial; i < size(); ++i) {
2693 Font font = getFont(buf.params(), i, outerfont);
2695 // handle <emphasis> tag
2696 if (font_old.emph() != font.fontInfo().emph()) {
2697 if (font.fontInfo().emph() == FONT_ON) {
2700 } else if (i != initial) {
2701 os << "</emphasis>";
2706 if (Inset const * inset = getInset(i)) {
2707 inset->docbook(os, runparams);
2709 char_type c = d->text_[i];
2711 if (style.pass_thru)
2714 os << sgml::escapeChar(c);
2716 font_old = font.fontInfo();
2720 os << "</emphasis>";
2723 if (style.free_spacing)
2725 if (style.pass_thru && !d->onlyText(buf, outerfont, initial))
2731 void doFontSwitch(vector<html::FontTag> & tagsToOpen,
2732 vector<html::EndFontTag> & tagsToClose,
2733 bool & flag, FontState curstate, html::FontTypes type)
2735 if (curstate == FONT_ON) {
2736 tagsToOpen.push_back(html::FontTag(type));
2739 tagsToClose.push_back(html::EndFontTag(type));
2746 docstring Paragraph::simpleLyXHTMLOnePar(Buffer const & buf,
2748 OutputParams const & runparams,
2749 Font const & outerfont,
2750 pos_type initial) const
2754 // track whether we have opened these tags
2755 bool emph_flag = false;
2756 bool bold_flag = false;
2757 bool noun_flag = false;
2758 bool ubar_flag = false;
2759 bool dbar_flag = false;
2760 bool sout_flag = false;
2761 bool wave_flag = false;
2763 bool shap_flag = false;
2765 bool faml_flag = false;
2767 bool size_flag = false;
2769 Layout const & style = *d->layout_;
2771 xs.startParagraph(allowEmpty());
2774 style.labeltype == LABEL_MANUAL ? style.labelfont : style.font;
2776 FontShape curr_fs = INHERIT_SHAPE;
2777 FontFamily curr_fam = INHERIT_FAMILY;
2778 FontSize curr_size = FONT_SIZE_INHERIT;
2780 string const default_family =
2781 buf.masterBuffer()->params().fonts_default_family;
2783 vector<html::FontTag> tagsToOpen;
2784 vector<html::EndFontTag> tagsToClose;
2786 // parsing main loop
2787 for (pos_type i = initial; i < size(); ++i) {
2788 // let's not show deleted material in the output
2792 Font const font = getFont(buf.masterBuffer()->params(), i, outerfont);
2795 FontState curstate = font.fontInfo().emph();
2796 if (font_old.emph() != curstate)
2797 doFontSwitch(tagsToOpen, tagsToClose, emph_flag, curstate, html::FT_EMPH);
2800 curstate = font.fontInfo().noun();
2801 if (font_old.noun() != curstate)
2802 doFontSwitch(tagsToOpen, tagsToClose, noun_flag, curstate, html::FT_NOUN);
2805 curstate = font.fontInfo().underbar();
2806 if (font_old.underbar() != curstate)
2807 doFontSwitch(tagsToOpen, tagsToClose, ubar_flag, curstate, html::FT_UBAR);
2810 curstate = font.fontInfo().strikeout();
2811 if (font_old.strikeout() != curstate)
2812 doFontSwitch(tagsToOpen, tagsToClose, sout_flag, curstate, html::FT_SOUT);
2815 curstate = font.fontInfo().uuline();
2816 if (font_old.uuline() != curstate)
2817 doFontSwitch(tagsToOpen, tagsToClose, dbar_flag, curstate, html::FT_DBAR);
2820 curstate = font.fontInfo().uwave();
2821 if (font_old.uwave() != curstate)
2822 doFontSwitch(tagsToOpen, tagsToClose, wave_flag, curstate, html::FT_WAVE);
2825 // a little hackish, but allows us to reuse what we have.
2826 curstate = (font.fontInfo().series() == BOLD_SERIES ? FONT_ON : FONT_OFF);
2827 if (font_old.series() != font.fontInfo().series())
2828 doFontSwitch(tagsToOpen, tagsToClose, bold_flag, curstate, html::FT_BOLD);
2831 curr_fs = font.fontInfo().shape();
2832 FontShape old_fs = font_old.shape();
2833 if (old_fs != curr_fs) {
2837 tagsToClose.push_back(html::EndFontTag(html::FT_ITALIC));
2840 tagsToClose.push_back(html::EndFontTag(html::FT_SLANTED));
2842 case SMALLCAPS_SHAPE:
2843 tagsToClose.push_back(html::EndFontTag(html::FT_SMALLCAPS));
2849 // the other tags are for internal use
2857 tagsToOpen.push_back(html::FontTag(html::FT_ITALIC));
2861 tagsToOpen.push_back(html::FontTag(html::FT_SLANTED));
2864 case SMALLCAPS_SHAPE:
2865 tagsToOpen.push_back(html::FontTag(html::FT_SMALLCAPS));
2872 // the other tags are for internal use
2879 curr_fam = font.fontInfo().family();
2880 FontFamily old_fam = font_old.family();
2881 if (old_fam != curr_fam) {
2885 tagsToClose.push_back(html::EndFontTag(html::FT_ROMAN));
2888 tagsToClose.push_back(html::EndFontTag(html::FT_SANS));
2890 case TYPEWRITER_FAMILY:
2891 tagsToClose.push_back(html::EndFontTag(html::FT_TYPE));
2893 case INHERIT_FAMILY:
2896 // the other tags are for internal use
2904 // we will treat a "default" font family as roman, since we have
2905 // no other idea what to do.
2906 if (default_family != "rmdefault" && default_family != "default") {
2907 tagsToOpen.push_back(html::FontTag(html::FT_ROMAN));
2912 if (default_family != "sfdefault") {
2913 tagsToOpen.push_back(html::FontTag(html::FT_SANS));
2917 case TYPEWRITER_FAMILY:
2918 if (default_family != "ttdefault") {
2919 tagsToOpen.push_back(html::FontTag(html::FT_TYPE));
2923 case INHERIT_FAMILY:
2926 // the other tags are for internal use
2933 curr_size = font.fontInfo().size();
2934 FontSize old_size = font_old.size();
2935 if (old_size != curr_size) {
2938 case FONT_SIZE_TINY:
2939 tagsToClose.push_back(html::EndFontTag(html::FT_SIZE_TINY));
2941 case FONT_SIZE_SCRIPT:
2942 tagsToClose.push_back(html::EndFontTag(html::FT_SIZE_SCRIPT));
2944 case FONT_SIZE_FOOTNOTE:
2945 tagsToClose.push_back(html::EndFontTag(html::FT_SIZE_FOOTNOTE));
2947 case FONT_SIZE_SMALL:
2948 tagsToClose.push_back(html::EndFontTag(html::FT_SIZE_SMALL));
2950 case FONT_SIZE_LARGE:
2951 tagsToClose.push_back(html::EndFontTag(html::FT_SIZE_LARGE));
2953 case FONT_SIZE_LARGER:
2954 tagsToClose.push_back(html::EndFontTag(html::FT_SIZE_LARGER));
2956 case FONT_SIZE_LARGEST:
2957 tagsToClose.push_back(html::EndFontTag(html::FT_SIZE_LARGEST));
2959 case FONT_SIZE_HUGE:
2960 tagsToClose.push_back(html::EndFontTag(html::FT_SIZE_HUGE));
2962 case FONT_SIZE_HUGER:
2963 tagsToClose.push_back(html::EndFontTag(html::FT_SIZE_HUGER));
2965 case FONT_SIZE_INCREASE:
2966 tagsToClose.push_back(html::EndFontTag(html::FT_SIZE_INCREASE));
2968 case FONT_SIZE_DECREASE:
2969 tagsToClose.push_back(html::EndFontTag(html::FT_SIZE_DECREASE));
2971 case FONT_SIZE_INHERIT:
2972 case FONT_SIZE_NORMAL:
2975 // the other tags are for internal use
2981 switch (curr_size) {
2982 case FONT_SIZE_TINY:
2983 tagsToOpen.push_back(html::FontTag(html::FT_SIZE_TINY));
2986 case FONT_SIZE_SCRIPT:
2987 tagsToOpen.push_back(html::FontTag(html::FT_SIZE_SCRIPT));
2990 case FONT_SIZE_FOOTNOTE:
2991 tagsToOpen.push_back(html::FontTag(html::FT_SIZE_FOOTNOTE));
2994 case FONT_SIZE_SMALL:
2995 tagsToOpen.push_back(html::FontTag(html::FT_SIZE_SMALL));
2998 case FONT_SIZE_LARGE:
2999 tagsToOpen.push_back(html::FontTag(html::FT_SIZE_LARGE));
3002 case FONT_SIZE_LARGER:
3003 tagsToOpen.push_back(html::FontTag(html::FT_SIZE_LARGER));
3006 case FONT_SIZE_LARGEST:
3007 tagsToOpen.push_back(html::FontTag(html::FT_SIZE_LARGEST));
3010 case FONT_SIZE_HUGE:
3011 tagsToOpen.push_back(html::FontTag(html::FT_SIZE_HUGE));
3014 case FONT_SIZE_HUGER:
3015 tagsToOpen.push_back(html::FontTag(html::FT_SIZE_HUGER));
3018 case FONT_SIZE_INCREASE:
3019 tagsToOpen.push_back(html::FontTag(html::FT_SIZE_INCREASE));
3022 case FONT_SIZE_DECREASE:
3023 tagsToOpen.push_back(html::FontTag(html::FT_SIZE_DECREASE));
3026 case FONT_SIZE_NORMAL:
3027 case FONT_SIZE_INHERIT:
3030 // the other tags are for internal use
3037 // Other such tags? What about the other text ranges?
3039 vector<html::EndFontTag>::const_iterator cit = tagsToClose.begin();
3040 vector<html::EndFontTag>::const_iterator cen = tagsToClose.end();
3041 for (; cit != cen; ++cit)
3044 vector<html::FontTag>::const_iterator sit = tagsToOpen.begin();
3045 vector<html::FontTag>::const_iterator sen = tagsToOpen.end();
3046 for (; sit != sen; ++sit)
3049 tagsToClose.clear();
3052 Inset const * inset = getInset(i);
3054 if (!runparams.for_toc || inset->isInToc()) {
3055 OutputParams np = runparams;
3056 np.local_font = &font;
3057 if (!inset->getLayout().htmlisblock())
3058 np.html_in_par = true;
3059 retval += inset->xhtml(xs, np);
3062 char_type c = getUChar(buf.masterBuffer()->params(), i);
3065 font_old = font.fontInfo();
3074 bool Paragraph::isHfill(pos_type pos) const
3076 Inset const * inset = getInset(pos);
3077 return inset && inset->isHfill();
3081 bool Paragraph::isNewline(pos_type pos) const
3083 Inset const * inset = getInset(pos);
3084 return inset && inset->lyxCode() == NEWLINE_CODE;
3088 bool Paragraph::isEnvSeparator(pos_type pos) const
3090 Inset const * inset = getInset(pos);
3091 return inset && inset->lyxCode() == SEPARATOR_CODE;
3095 bool Paragraph::isLineSeparator(pos_type pos) const
3097 char_type const c = d->text_[pos];
3098 if (isLineSeparatorChar(c))
3100 Inset const * inset = getInset(pos);
3101 return inset && inset->isLineSeparator();
3105 bool Paragraph::isWordSeparator(pos_type pos) const
3109 if (Inset const * inset = getInset(pos))
3110 return !inset->isLetter();
3111 // if we have a hard hyphen (no en- or emdash) or apostrophe
3112 // we pass this to the spell checker
3113 // FIXME: this method is subject to change, visit
3114 // https://bugzilla.mozilla.org/show_bug.cgi?id=355178
3115 // to get an impression how complex this is.
3116 if (isHardHyphenOrApostrophe(pos))
3118 char_type const c = d->text_[pos];
3119 // We want to pass the escape chars to the spellchecker
3120 docstring const escape_chars = from_utf8(lyxrc.spellchecker_esc_chars);
3121 return !isLetterChar(c) && !isDigitASCII(c) && !contains(escape_chars, c);
3125 bool Paragraph::isHardHyphenOrApostrophe(pos_type pos) const
3127 pos_type const psize = size();
3130 char_type const c = d->text_[pos];
3131 if (c != '-' && c != '\'')
3133 int nextpos = pos + 1;
3134 int prevpos = pos > 0 ? pos - 1 : 0;
3135 if ((nextpos == psize || isSpace(nextpos))
3136 && (pos == 0 || isSpace(prevpos)))
3142 FontSpan const & Paragraph::getSpellRange(pos_type pos) const
3144 return d->speller_state_.getRange(pos);
3148 bool Paragraph::isChar(pos_type pos) const
3150 if (Inset const * inset = getInset(pos))
3151 return inset->isChar();
3152 char_type const c = d->text_[pos];
3153 return !isLetterChar(c) && !isDigitASCII(c) && !lyx::isSpace(c);
3157 bool Paragraph::isSpace(pos_type pos) const
3159 if (Inset const * inset = getInset(pos))
3160 return inset->isSpace();
3161 char_type const c = d->text_[pos];
3162 return lyx::isSpace(c);
3167 Paragraph::getParLanguage(BufferParams const & bparams) const
3170 return getFirstFontSettings(bparams).language();
3171 // FIXME: we should check the prev par as well (Lgb)
3172 return bparams.language;
3176 bool Paragraph::isRTL(BufferParams const & bparams) const
3178 return getParLanguage(bparams)->rightToLeft()
3179 && !inInset().getLayout().forceLTR();
3183 void Paragraph::changeLanguage(BufferParams const & bparams,
3184 Language const * from, Language const * to)
3186 // change language including dummy font change at the end
3187 for (pos_type i = 0; i <= size(); ++i) {
3188 Font font = getFontSettings(bparams, i);
3189 if (font.language() == from) {
3190 font.setLanguage(to);
3192 d->requestSpellCheck(i);
3198 bool Paragraph::isMultiLingual(BufferParams const & bparams) const
3200 Language const * doc_language = bparams.language;
3201 FontList::const_iterator cit = d->fontlist_.begin();
3202 FontList::const_iterator end = d->fontlist_.end();
3204 for (; cit != end; ++cit)
3205 if (cit->font().language() != ignore_language &&
3206 cit->font().language() != latex_language &&
3207 cit->font().language() != doc_language)
3213 void Paragraph::getLanguages(std::set<Language const *> & languages) const
3215 FontList::const_iterator cit = d->fontlist_.begin();
3216 FontList::const_iterator end = d->fontlist_.end();
3218 for (; cit != end; ++cit) {
3219 Language const * lang = cit->font().language();
3220 if (lang != ignore_language &&
3221 lang != latex_language)
3222 languages.insert(lang);
3227 docstring Paragraph::asString(int options) const
3229 return asString(0, size(), options);
3233 docstring Paragraph::asString(pos_type beg, pos_type end, int options, const OutputParams *runparams) const
3235 odocstringstream os;
3238 && options & AS_STR_LABEL
3239 && !d->params_.labelString().empty())
3240 os << d->params_.labelString() << ' ';
3242 for (pos_type i = beg; i < end; ++i) {
3243 if ((options & AS_STR_SKIPDELETE) && isDeleted(i))
3245 char_type const c = d->text_[i];
3246 if (isPrintable(c) || c == '\t'
3247 || (c == '\n' && (options & AS_STR_NEWLINES)))
3249 else if (c == META_INSET && (options & AS_STR_INSETS)) {
3250 if (c == META_INSET && (options & AS_STR_PLAINTEXT)) {
3251 LASSERT(runparams != 0, return docstring());
3252 getInset(i)->plaintext(os, *runparams);
3254 getInset(i)->toString(os);
3263 void Paragraph::forOutliner(docstring & os, size_t const maxlen,
3264 bool const shorten) const
3266 size_t tmplen = shorten ? maxlen + 1 : maxlen;
3267 if (!d->params_.labelString().empty())
3268 os += d->params_.labelString() + ' ';
3269 for (pos_type i = 0; i < size() && os.length() < tmplen; ++i) {
3272 char_type const c = d->text_[i];
3275 else if (c == META_INSET)
3276 getInset(i)->forOutliner(os, tmplen, false);
3279 Text::shortenForOutliner(os, maxlen);
3283 void Paragraph::setInsetOwner(Inset const * inset)
3285 d->inset_owner_ = inset;
3289 int Paragraph::id() const
3295 void Paragraph::setId(int id)
3301 Layout const & Paragraph::layout() const
3307 void Paragraph::setLayout(Layout const & layout)
3309 d->layout_ = &layout;
3313 void Paragraph::setDefaultLayout(DocumentClass const & tc)
3315 setLayout(tc.defaultLayout());
3319 void Paragraph::setPlainLayout(DocumentClass const & tc)
3321 setLayout(tc.plainLayout());
3325 void Paragraph::setPlainOrDefaultLayout(DocumentClass const & tclass)
3327 if (usePlainLayout())
3328 setPlainLayout(tclass);
3330 setDefaultLayout(tclass);
3334 Inset const & Paragraph::inInset() const
3336 LBUFERR(d->inset_owner_);
3337 return *d->inset_owner_;
3341 ParagraphParameters & Paragraph::params()
3347 ParagraphParameters const & Paragraph::params() const
3353 bool Paragraph::isFreeSpacing() const
3355 if (d->layout_->free_spacing)
3357 return d->inset_owner_ && d->inset_owner_->isFreeSpacing();
3361 bool Paragraph::allowEmpty() const
3363 if (d->layout_->keepempty)
3365 return d->inset_owner_ && d->inset_owner_->allowEmpty();
3369 bool Paragraph::brokenBiblio() const
3371 // there is a problem if there is no bibitem at position 0 or
3372 // if there is another bibitem in the paragraph.
3373 return d->layout_->labeltype == LABEL_BIBLIO
3374 && (d->insetlist_.find(BIBITEM_CODE) != 0
3375 || d->insetlist_.find(BIBITEM_CODE, 1) > 0);
3379 int Paragraph::fixBiblio(Buffer const & buffer)
3381 // FIXME: What about the case where paragraph is not BIBLIO
3382 // but there is an InsetBibitem?
3383 // FIXME: when there was already an inset at 0, the return value is 1,
3384 // which does not tell whether another inset has been remove; the
3385 // cursor cannot be correctly updated.
3387 if (d->layout_->labeltype != LABEL_BIBLIO)
3390 bool const track_changes = buffer.params().track_changes;
3391 int bibitem_pos = d->insetlist_.find(BIBITEM_CODE);
3392 bool const hasbibitem0 = bibitem_pos == 0;
3395 bibitem_pos = d->insetlist_.find(BIBITEM_CODE, 1);
3396 // There was an InsetBibitem at pos 0, and no other one => OK
3397 if (bibitem_pos == -1)
3399 // there is a bibitem at the 0 position, but since
3400 // there is a second one, we copy the second on the
3401 // first. We're assuming there are at most two of
3402 // these, which there should be.
3403 // FIXME: why does it make sense to do that rather
3404 // than keep the first? (JMarc)
3405 Inset * inset = releaseInset(bibitem_pos);
3406 d->insetlist_.begin()->inset = inset;
3407 return -bibitem_pos;
3410 // We need to create an inset at the beginning
3412 if (bibitem_pos > 0) {
3413 // there was one somewhere in the paragraph, let's move it
3414 inset = d->insetlist_.release(bibitem_pos);
3415 eraseChar(bibitem_pos, track_changes);
3418 inset = new InsetBibitem(const_cast<Buffer *>(&buffer),
3419 InsetCommandParams(BIBITEM_CODE));
3421 Font font(inherit_font, buffer.params().language);
3422 insertInset(0, inset, font, Change(track_changes ? Change::INSERTED
3423 : Change::UNCHANGED));
3429 void Paragraph::checkAuthors(AuthorList const & authorList)
3431 d->changes_.checkAuthors(authorList);
3435 bool Paragraph::isChanged(pos_type pos) const
3437 return lookupChange(pos).changed();
3441 bool Paragraph::isInserted(pos_type pos) const
3443 return lookupChange(pos).inserted();
3447 bool Paragraph::isDeleted(pos_type pos) const
3449 return lookupChange(pos).deleted();
3453 InsetList const & Paragraph::insetList() const
3455 return d->insetlist_;
3459 void Paragraph::setBuffer(Buffer & b)
3461 d->insetlist_.setBuffer(b);
3465 Inset * Paragraph::releaseInset(pos_type pos)
3467 Inset * inset = d->insetlist_.release(pos);
3468 /// does not honour change tracking!
3469 eraseChar(pos, false);
3474 Inset * Paragraph::getInset(pos_type pos)
3476 return (pos < pos_type(d->text_.size()) && d->text_[pos] == META_INSET)
3477 ? d->insetlist_.get(pos) : 0;
3481 Inset const * Paragraph::getInset(pos_type pos) const
3483 return (pos < pos_type(d->text_.size()) && d->text_[pos] == META_INSET)
3484 ? d->insetlist_.get(pos) : 0;
3488 void Paragraph::changeCase(BufferParams const & bparams, pos_type pos,
3489 pos_type & right, TextCase action)
3491 // process sequences of modified characters; in change
3492 // tracking mode, this approach results in much better
3493 // usability than changing case on a char-by-char basis
3494 // We also need to track the current font, since font
3495 // changes within sequences can occur.
3496 vector<pair<char_type, Font> > changes;
3498 bool const trackChanges = bparams.track_changes;
3500 bool capitalize = true;
3502 for (; pos < right; ++pos) {
3503 char_type oldChar = d->text_[pos];
3504 char_type newChar = oldChar;
3506 // ignore insets and don't play with deleted text!
3507 if (oldChar != META_INSET && !isDeleted(pos)) {
3509 case text_lowercase:
3510 newChar = lowercase(oldChar);
3512 case text_capitalization:
3514 newChar = uppercase(oldChar);
3518 case text_uppercase:
3519 newChar = uppercase(oldChar);
3524 if (isWordSeparator(pos) || isDeleted(pos)) {
3525 // permit capitalization again
3529 if (oldChar != newChar) {
3530 changes.push_back(make_pair(newChar, getFontSettings(bparams, pos)));
3531 if (pos != right - 1)
3533 // step behind the changing area
3537 int erasePos = pos - changes.size();
3538 for (size_t i = 0; i < changes.size(); i++) {
3539 insertChar(pos, changes[i].first,
3542 if (!eraseChar(erasePos, trackChanges)) {
3545 ++right; // expand selection
3553 int Paragraph::find(docstring const & str, bool cs, bool mw,
3554 pos_type start_pos, bool del) const
3556 pos_type pos = start_pos;
3557 int const strsize = str.length();
3559 pos_type const parsize = d->text_.size();
3560 for (i = 0; i < strsize && pos < parsize; ++i, ++pos) {
3561 // Ignore "invisible" letters such as ligature breaks
3562 // and hyphenation chars while searching
3563 while (pos < parsize - 1 && isInset(pos)) {
3564 odocstringstream os;
3565 getInset(pos)->toString(os);
3566 if (!getInset(pos)->isLetter() || !os.str().empty())
3570 if (cs && str[i] != d->text_[pos])
3572 if (!cs && uppercase(str[i]) != uppercase(d->text_[pos]))
3574 if (!del && isDeleted(pos))
3581 // if necessary, check whether string matches word
3583 if (start_pos > 0 && !isWordSeparator(start_pos - 1))
3586 && !isWordSeparator(pos))
3590 return pos - start_pos;
3594 char_type Paragraph::getChar(pos_type pos) const
3596 return d->text_[pos];
3600 pos_type Paragraph::size() const
3602 return d->text_.size();
3606 bool Paragraph::empty() const
3608 return d->text_.empty();
3612 bool Paragraph::isInset(pos_type pos) const
3614 return d->text_[pos] == META_INSET;
3618 bool Paragraph::isSeparator(pos_type pos) const
3620 //FIXME: Are we sure this can be the only separator?
3621 return d->text_[pos] == ' ';
3625 void Paragraph::deregisterWords()
3627 Private::LangWordsMap::const_iterator itl = d->words_.begin();
3628 Private::LangWordsMap::const_iterator ite = d->words_.end();
3629 for (; itl != ite; ++itl) {
3630 WordList * wl = theWordList(itl->first);
3631 Private::Words::const_iterator it = (itl->second).begin();
3632 Private::Words::const_iterator et = (itl->second).end();
3633 for (; it != et; ++it)
3640 void Paragraph::locateWord(pos_type & from, pos_type & to,
3641 word_location const loc) const
3644 case WHOLE_WORD_STRICT:
3645 if (from == 0 || from == size()
3646 || isWordSeparator(from)
3647 || isWordSeparator(from - 1)) {
3651 // no break here, we go to the next
3654 // If we are already at the beginning of a word, do nothing
3655 if (!from || isWordSeparator(from - 1))
3657 // no break here, we go to the next
3660 // always move the cursor to the beginning of previous word
3661 while (from && !isWordSeparator(from - 1))
3665 LYXERR0("Paragraph::locateWord: NEXT_WORD not implemented yet");
3668 // no need to move the 'from' cursor
3672 while (to < size() && !isWordSeparator(to))
3677 void Paragraph::collectWords()
3679 for (pos_type pos = 0; pos < size(); ++pos) {
3680 if (isWordSeparator(pos))
3682 pos_type from = pos;
3683 locateWord(from, pos, WHOLE_WORD);
3684 // Work around MSVC warning: The statement
3685 // if (pos < from + lyxrc.completion_minlength)
3686 // triggers a signed vs. unsigned warning.
3687 // I don't know why this happens, it could be a MSVC bug, or
3688 // related to LLP64 (windows) vs. LP64 (unix) programming
3689 // model, or the C++ standard might be ambigous in the section
3690 // defining the "usual arithmetic conversions". However, using
3691 // a temporary variable is safe and works on all compilers.
3692 pos_type const endpos = from + lyxrc.completion_minlength;
3695 FontList::const_iterator cit = d->fontlist_.fontIterator(from);
3696 if (cit == d->fontlist_.end())
3698 Language const * lang = cit->font().language();
3699 docstring const word = asString(from, pos, AS_STR_NONE);
3700 d->words_[lang->lang()].insert(word);
3705 void Paragraph::registerWords()
3707 Private::LangWordsMap::const_iterator itl = d->words_.begin();
3708 Private::LangWordsMap::const_iterator ite = d->words_.end();
3709 for (; itl != ite; ++itl) {
3710 WordList * wl = theWordList(itl->first);
3711 Private::Words::const_iterator it = (itl->second).begin();
3712 Private::Words::const_iterator et = (itl->second).end();
3713 for (; it != et; ++it)
3719 void Paragraph::updateWords()
3727 void Paragraph::Private::appendSkipPosition(SkipPositions & skips, pos_type const pos) const
3729 SkipPositionsIterator begin = skips.begin();
3730 SkipPositions::iterator end = skips.end();
3731 if (pos > 0 && begin < end) {
3733 if (end->last == pos - 1) {
3738 skips.insert(end, FontSpan(pos, pos));
3742 Language * Paragraph::Private::locateSpellRange(
3743 pos_type & from, pos_type & to,
3744 SkipPositions & skips) const
3746 // skip leading white space
3747 while (from < to && owner_->isWordSeparator(from))
3749 // don't check empty range
3752 // get current language
3753 Language * lang = getSpellLanguage(from);
3754 pos_type last = from;
3755 bool samelang = true;
3756 bool sameinset = true;
3757 while (last < to && samelang && sameinset) {
3758 // hop to end of word
3759 while (last < to && !owner_->isWordSeparator(last)) {
3760 if (owner_->getInset(last)) {
3761 appendSkipPosition(skips, last);
3762 } else if (owner_->isDeleted(last)) {
3763 appendSkipPosition(skips, last);
3767 // hop to next word while checking for insets
3768 while (sameinset && last < to && owner_->isWordSeparator(last)) {
3769 if (Inset const * inset = owner_->getInset(last))
3770 sameinset = inset->isChar() && inset->isLetter();
3771 if (sameinset && owner_->isDeleted(last)) {
3772 appendSkipPosition(skips, last);
3777 if (sameinset && last < to) {
3778 // now check for language change
3779 samelang = lang == getSpellLanguage(last);
3782 // if language change detected backstep is needed
3790 Language * Paragraph::Private::getSpellLanguage(pos_type const from) const
3793 const_cast<Language *>(owner_->getFontSettings(
3794 inset_owner_->buffer().params(), from).language());
3795 if (lang == inset_owner_->buffer().params().language
3796 && !lyxrc.spellchecker_alt_lang.empty()) {
3798 string const lang_variety =
3799 split(lyxrc.spellchecker_alt_lang, lang_code, '-');
3800 lang->setCode(lang_code);
3801 lang->setVariety(lang_variety);
3807 void Paragraph::requestSpellCheck(pos_type pos)
3809 d->requestSpellCheck(pos);
3813 bool Paragraph::needsSpellCheck() const
3815 SpellChecker::ChangeNumber speller_change_number = 0;
3816 if (theSpellChecker())
3817 speller_change_number = theSpellChecker()->changeNumber();
3818 if (speller_change_number > d->speller_state_.currentChangeNumber()) {
3819 d->speller_state_.needsCompleteRefresh(speller_change_number);
3821 return d->needsSpellCheck();
3825 bool Paragraph::Private::ignoreWord(docstring const & word) const
3827 // Ignore words with digits
3828 // FIXME: make this customizable
3829 // (note that some checkers ignore words with digits by default)
3830 docstring::const_iterator cit = word.begin();
3831 docstring::const_iterator const end = word.end();
3832 for (; cit != end; ++cit) {
3833 if (isNumber((*cit)))
3840 SpellChecker::Result Paragraph::spellCheck(pos_type & from, pos_type & to,
3841 WordLangTuple & wl, docstring_list & suggestions,
3842 bool do_suggestion, bool check_learned) const
3844 SpellChecker::Result result = SpellChecker::WORD_OK;
3845 SpellChecker * speller = theSpellChecker();
3849 if (!d->layout_->spellcheck || !inInset().allowSpellCheck())
3852 locateWord(from, to, WHOLE_WORD);
3853 if (from == to || from >= size())
3856 docstring word = asString(from, to, AS_STR_INSETS | AS_STR_SKIPDELETE);
3857 Language * lang = d->getSpellLanguage(from);
3859 wl = WordLangTuple(word, lang);
3864 if (needsSpellCheck() || check_learned) {
3866 if (!d->ignoreWord(word)) {
3867 bool const trailing_dot = to < size() && d->text_[to] == '.';
3868 result = speller->check(wl);
3869 if (SpellChecker::misspelled(result) && trailing_dot) {
3870 wl = WordLangTuple(word.append(from_ascii(".")), lang);
3871 result = speller->check(wl);
3872 if (!SpellChecker::misspelled(result)) {
3873 LYXERR(Debug::GUI, "misspelled word is correct with dot: \"" <<
3875 from << ".." << to << "]");
3877 // spell check with dot appended failed too
3878 // restore original word/lang value
3879 word = asString(from, to, AS_STR_INSETS | AS_STR_SKIPDELETE);
3880 wl = WordLangTuple(word, lang);
3884 if (!SpellChecker::misspelled(result)) {
3885 // area up to the begin of the next word is not misspelled
3886 while (end < size() && isWordSeparator(end))
3889 d->setMisspelled(from, end, result);
3891 result = d->speller_state_.getState(from);
3895 suggestions.clear();
3897 if (SpellChecker::misspelled(result)) {
3898 LYXERR(Debug::GUI, "misspelled word: \"" <<
3900 from << ".." << to << "]");
3902 speller->suggest(wl, suggestions);
3908 void Paragraph::Private::markMisspelledWords(
3909 pos_type const & first, pos_type const & last,
3910 SpellChecker::Result result,
3911 docstring const & word,
3912 SkipPositions const & skips)
3914 if (!SpellChecker::misspelled(result)) {
3915 setMisspelled(first, last, SpellChecker::WORD_OK);
3919 SpellChecker * speller = theSpellChecker();
3920 // locate and enumerate the error positions
3921 int nerrors = speller->numMisspelledWords();
3923 SkipPositionsIterator it = skips.begin();
3924 SkipPositionsIterator et = skips.end();
3925 for (int index = 0; index < nerrors; ++index) {
3928 speller->misspelledWord(index, wstart, wlen);
3929 /// should not happen if speller supports range checks
3930 if (!wlen) continue;
3931 docstring const misspelled = word.substr(wstart, wlen);
3932 wstart += first + numskipped;
3933 if (snext < wstart) {
3934 /// mark the range of correct spelling
3935 numskipped += countSkips(it, et, wstart);
3936 setMisspelled(snext,
3937 wstart - 1, SpellChecker::WORD_OK);
3939 snext = wstart + wlen;
3940 numskipped += countSkips(it, et, snext);
3941 /// mark the range of misspelling
3942 setMisspelled(wstart, snext, result);
3943 LYXERR(Debug::GUI, "misspelled word: \"" <<
3944 misspelled << "\" [" <<
3945 wstart << ".." << (snext-1) << "]");
3948 if (snext <= last) {
3949 /// mark the range of correct spelling at end
3950 setMisspelled(snext, last, SpellChecker::WORD_OK);
3955 void Paragraph::spellCheck() const
3957 SpellChecker * speller = theSpellChecker();
3958 if (!speller || empty() ||!needsSpellCheck())
3962 d->rangeOfSpellCheck(start, endpos);
3963 if (speller->canCheckParagraph()) {
3964 // loop until we leave the range
3965 for (pos_type first = start; first < endpos; ) {
3966 pos_type last = endpos;
3967 Private::SkipPositions skips;
3968 Language * lang = d->locateSpellRange(first, last, skips);
3969 if (first >= endpos)
3971 // start the spell checker on the unit of meaning
3972 docstring word = asString(first, last, AS_STR_INSETS + AS_STR_SKIPDELETE);
3973 WordLangTuple wl = WordLangTuple(word, lang);
3974 SpellChecker::Result result = word.size() ?
3975 speller->check(wl) : SpellChecker::WORD_OK;
3976 d->markMisspelledWords(first, last, result, word, skips);
3980 static docstring_list suggestions;
3981 pos_type to = endpos;
3982 while (start < endpos) {
3984 spellCheck(start, to, wl, suggestions, false);
3988 d->readySpellCheck();
3992 bool Paragraph::isMisspelled(pos_type pos, bool check_boundary) const
3994 bool result = SpellChecker::misspelled(d->speller_state_.getState(pos));
3995 if (result || pos <= 0 || pos > size())
3997 if (check_boundary && (pos == size() || isWordSeparator(pos)))
3998 result = SpellChecker::misspelled(d->speller_state_.getState(pos - 1));
4003 string Paragraph::magicLabel() const
4006 ss << "magicparlabel-" << id();