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 character text_[i] could not be translated
919 // into the current latex encoding (or its latex translation has been forced,)
920 // and it belongs to a known script.
921 // TODO: We need \textcyr and \textgreek wrappers also for characters
922 // that can be encoded in the "LaTeX encoding" but not in the
923 // current *font encoding*.
924 // (See #9681 for details and test)
925 // Parameter ltx contains the latex translation of text_[i] as specified
926 // in the unicodesymbols file and is something like "\textXXX{<spec>}".
927 // The latex macro name "textXXX" specifies the script to which text_[i]
928 // belongs and we use it in order to check whether characters from the
929 // same script immediately follow, such that we can collect them in a
930 // single "\textXXX" macro. So, we have to retain "\textXXX{<spec>"
931 // for the first char but only "<spec>" for all subsequent chars.
932 docstring::size_type const brace1 = ltx.find_first_of(from_ascii("{"));
933 docstring::size_type const brace2 = ltx.find_last_of(from_ascii("}"));
934 string script = to_ascii(ltx.substr(1, brace1 - 1));
937 bool closing_brace = true;
938 if (script == "textgreek" && encoding.latexName() == "iso-8859-7") {
939 // Correct encoding is being used, so we can avoid \textgreek.
940 // TODO: wrong test: we need to check the *font encoding*
941 // (i.e. the active language and its FontEncoding tag)
942 // instead of the LaTeX *input encoding*!
943 // See #9637 for details and test-cases.
946 closing_brace = false;
948 os << ltx.substr(pos, length);
949 int size = text_.size();
950 while (i + 1 < size) {
951 char_type const next = text_[i + 1];
952 // Stop here if next character belongs to another script
953 // or there is a change in change tracking status.
954 if (!Encodings::isKnownScriptChar(next, script) ||
955 runningChange != owner_->lookupChange(i + 1))
959 FontList::const_iterator cit = fontlist_.begin();
960 FontList::const_iterator end = fontlist_.end();
961 for (; cit != end; ++cit) {
962 if (cit->pos() >= i && !found) {
963 prev_font = cit->font();
966 if (cit->pos() >= i + 1)
969 // Stop here if there is a font attribute or encoding change.
970 if (found && cit != end && prev_font != cit->font())
972 docstring const latex = encoding.latexChar(next).first;
973 docstring::size_type const b1 =
974 latex.find_first_of(from_ascii("{"));
975 docstring::size_type const b2 =
976 latex.find_last_of(from_ascii("}"));
977 int const len = b2 - b1 - 1;
978 os << latex.substr(b1 + 1, len);
990 void Paragraph::Private::latexInset(BufferParams const & bparams,
992 OutputParams & runparams,
995 Font const & outerfont,
997 Change & running_change,
998 Layout const & style,
1000 unsigned int & column)
1002 Inset * inset = owner_->getInset(i);
1005 if (style.pass_thru) {
1006 odocstringstream ods;
1007 inset->plaintext(ods, runparams);
1012 // FIXME: move this to InsetNewline::latex
1013 if (inset->lyxCode() == NEWLINE_CODE || inset->lyxCode() == SEPARATOR_CODE) {
1014 // newlines are handled differently here than
1015 // the default in simpleTeXSpecialChars().
1016 if (!style.newline_allowed) {
1020 column += running_font.latexWriteEndChanges(
1021 os, bparams, runparams,
1022 basefont, basefont);
1026 if (running_font.fontInfo().family() == TYPEWRITER_FAMILY)
1029 basefont = owner_->getLayoutFont(bparams, outerfont);
1030 running_font = basefont;
1032 if (runparams.moving_arg)
1036 os.texrow().start(owner_->id(), i + 1);
1040 if (owner_->isDeleted(i)) {
1041 if( ++runparams.inDeletedInset == 1)
1042 runparams.changeOfDeletedInset = owner_->lookupChange(i);
1045 if (inset->canTrackChanges()) {
1046 column += Changes::latexMarkChange(os, bparams, running_change,
1047 Change(Change::UNCHANGED), runparams);
1048 running_change = Change(Change::UNCHANGED);
1052 odocstream::pos_type const len = os.os().tellp();
1054 if (inset->forceLTR()
1055 && !runparams.use_polyglossia
1056 && running_font.isRightToLeft()
1057 // ERT is an exception, it should be output with no
1058 // decorations at all
1059 && inset->lyxCode() != ERT_CODE) {
1060 if (running_font.language()->lang() == "farsi")
1067 // FIXME: Bug: we can have an empty font change here!
1068 // if there has just been a font change, we are going to close it
1069 // right now, which means stupid latex code like \textsf{}. AFAIK,
1070 // this does not harm dvi output. A minor bug, thus (JMarc)
1072 // Some insets cannot be inside a font change command.
1073 // However, even such insets *can* be placed in \L or \R
1074 // or their equivalents (for RTL language switches), so we don't
1075 // close the language in those cases.
1076 // ArabTeX, though, cannot handle this special behavior, it seems.
1077 bool arabtex = basefont.language()->lang() == "arabic_arabtex"
1078 || running_font.language()->lang() == "arabic_arabtex";
1079 if (open_font && !inset->inheritFont()) {
1080 bool closeLanguage = arabtex
1081 || basefont.isRightToLeft() == running_font.isRightToLeft();
1082 unsigned int count = running_font.latexWriteEndChanges(os,
1083 bparams, runparams, basefont, basefont, closeLanguage);
1085 // if any font properties were closed, update the running_font,
1086 // making sure, however, to leave the language as it was
1088 // FIXME: probably a better way to keep track of the old
1089 // language, than copying the entire font?
1090 Font const copy_font(running_font);
1091 basefont = owner_->getLayoutFont(bparams, outerfont);
1092 running_font = basefont;
1094 running_font.setLanguage(copy_font.language());
1095 // leave font open if language is still open
1096 open_font = (running_font.language() == basefont.language());
1098 runparams.local_font = &basefont;
1102 int prev_rows = os.texrow().rows();
1105 runparams.lastid = id_;
1106 runparams.lastpos = i;
1107 inset->latex(os, runparams);
1108 } catch (EncodingException & e) {
1109 // add location information and throw again.
1116 if (running_font.language()->lang() == "farsi")
1122 if (os.texrow().rows() > prev_rows) {
1123 os.texrow().start(owner_->id(), i + 1);
1126 column += (unsigned int)(os.os().tellp() - len);
1129 if (owner_->isDeleted(i))
1130 --runparams.inDeletedInset;
1134 void Paragraph::Private::latexSpecialChar(otexstream & os,
1135 BufferParams const & bparams,
1136 OutputParams const & runparams,
1137 Font const & running_font,
1138 Change const & running_change,
1139 Layout const & style,
1142 unsigned int & column)
1144 // With polyglossia, brackets and stuff need not be reversed
1145 // in RTL scripts (see bug #8251)
1146 char_type const c = (runparams.use_polyglossia) ?
1147 owner_->getUChar(bparams, i) : text_[i];
1149 if (style.pass_thru || runparams.pass_thru
1150 || contains(style.pass_thru_chars, c)
1151 || contains(runparams.pass_thru_chars, c)) {
1153 Encoding const * const enc = runparams.encoding;
1154 if (enc && !enc->encodable(c))
1155 throw EncodingException(c);
1161 // TIPA uses its own T3 encoding
1162 if (runparams.inIPA && latexSpecialT3(c, os, i, column))
1164 // If T1 font encoding is used, use the special
1165 // characters it provides.
1166 // NOTE: Some languages reset the font encoding internally to a
1167 // non-standard font encoding. If we are using such a language,
1168 // we do not output special T1 chars.
1169 if (!runparams.inIPA && !running_font.language()->internalFontEncoding()
1170 && bparams.font_encoding() == "T1" && latexSpecialT1(c, os, i, column))
1173 // Otherwise, we use what LaTeX provides us.
1176 os << "\\textbackslash{}";
1180 os << "\\textless{}";
1184 os << "\\textgreater{}";
1188 os << "\\textbar{}";
1193 if (i + 1 < end_pos && text_[i+1] == '-') {
1194 // Prevent "--" becoming an endash and "---" becoming
1196 // Within \ttfamily, "--" is merged to "-" (no endash)
1197 // so we avoid this rather irritating ligature as well
1203 os << "\\char`\\\"{}";
1208 case '%': case '#': case '{':
1216 os << "\\textasciitilde{}";
1221 os << "\\textasciicircum{}";
1228 // avoid being mistaken for optional arguments
1236 // Blanks are printed before font switching.
1237 // Sure? I am not! (try nice-latex)
1238 // I am sure it's correct. LyX might be smarter
1239 // in the future, but for now, nothing wrong is
1247 Encoding const & encoding = *(runparams.encoding);
1248 char_type next = '\0';
1249 if (i + 1 < int(text_.size())) {
1250 next = text_[i + 1];
1251 if (Encodings::isCombiningChar(next)) {
1252 column += latexSurrogatePair(os, c, next, runparams) - 1;
1258 pair<docstring, bool> latex = encoding.latexChar(c);
1259 docstring nextlatex;
1260 bool nexttipas = false;
1261 string nexttipashortcut;
1262 if (next != '\0' && next != META_INSET && encoding.encodable(next)) {
1263 nextlatex = encoding.latexChar(next).first;
1264 if (runparams.inIPA) {
1265 nexttipashortcut = Encodings::TIPAShortcut(next);
1266 nexttipas = !nexttipashortcut.empty();
1270 if (runparams.inIPA) {
1271 string const tipashortcut = Encodings::TIPAShortcut(c);
1272 if (!tipashortcut.empty()) {
1273 latex.first = from_ascii(tipashortcut);
1274 latex.second = false;
1278 if (Encodings::isKnownScriptChar(c, script)
1279 && prefixIs(latex.first, from_ascii("\\" + script)))
1280 column += writeScriptChars(os, latex.first,
1281 running_change, encoding, i) - 1;
1282 else if (latex.second
1283 && ((!prefixIs(nextlatex, '\\')
1284 && !prefixIs(nextlatex, '{')
1285 && !prefixIs(nextlatex, '}'))
1287 && !prefixIs(from_ascii(nexttipashortcut), '\\')))
1289 // Prevent eating of a following
1290 // space or command corruption by
1291 // following characters
1292 if (next == ' ' || next == '\0') {
1293 column += latex.first.length() + 1;
1294 os << latex.first << "{}";
1296 column += latex.first.length();
1297 os << latex.first << " ";
1300 column += latex.first.length() - 1;
1308 bool Paragraph::Private::latexSpecialT1(char_type const c, otexstream & os,
1309 pos_type i, unsigned int & column)
1315 // In T1 encoding, these characters exist
1316 // but we should avoid ligatures
1317 if (i + 1 >= int(text_.size()) || text_[i + 1] != c)
1319 os << "\\textcompwordmark{}";
1326 // soul.sty breaks with \char`\"
1327 os << "\\textquotedbl{}";
1336 bool Paragraph::Private::latexSpecialT3(char_type const c, otexstream & os,
1337 pos_type /*i*/, unsigned int & column)
1347 os << "\\textvertline{}";
1356 void Paragraph::Private::validate(LaTeXFeatures & features) const
1358 if (layout_->inpreamble && inset_owner_) {
1359 bool const is_command = layout_->latextype == LATEX_COMMAND;
1360 Buffer const & buf = inset_owner_->buffer();
1361 BufferParams const & bp = features.runparams().is_child
1362 ? buf.masterParams() : buf.params();
1365 // Using a string stream here circumvents the encoding
1366 // switching machinery of odocstream. Therefore the
1367 // output is wrong if this paragraph contains content
1368 // that needs to switch encoding.
1369 odocstringstream ods;
1370 otexstream os(ods, texrow);
1372 os << '\\' << from_ascii(layout_->latexname());
1373 // we have to provide all the optional arguments here, even though
1374 // the last one is the only one we care about.
1375 // Separate handling of optional argument inset.
1376 if (!layout_->latexargs().empty()) {
1377 OutputParams rp = features.runparams();
1378 rp.local_font = &owner_->getFirstFontSettings(bp);
1379 latexArgInsets(*owner_, os, rp, layout_->latexargs());
1381 os << from_ascii(layout_->latexparam());
1383 docstring::size_type const length = ods.str().length();
1384 // this will output "{" at the beginning, but not at the end
1385 owner_->latex(bp, f, os, features.runparams(), 0, -1, true);
1386 if (ods.str().length() > length) {
1389 if (!layout_->postcommandargs().empty()) {
1390 OutputParams rp = features.runparams();
1391 rp.local_font = &owner_->getFirstFontSettings(bp);
1392 latexArgInsets(*owner_, os, rp, layout_->postcommandargs(), "post:");
1395 string const snippet = to_utf8(ods.str());
1396 features.addPreambleSnippet(snippet);
1400 if (features.runparams().flavor == OutputParams::HTML
1401 && layout_->htmltitle()) {
1402 features.setHTMLTitle(owner_->asString(AS_STR_INSETS | AS_STR_SKIPDELETE));
1405 // check the params.
1406 if (!params_.spacing().isDefault())
1407 features.require("setspace");
1410 features.useLayout(layout_->name());
1413 fontlist_.validate(features);
1415 // then the indentation
1416 if (!params_.leftIndent().zero())
1417 features.require("ParagraphLeftIndent");
1420 InsetList::const_iterator icit = insetlist_.begin();
1421 InsetList::const_iterator iend = insetlist_.end();
1422 for (; icit != iend; ++icit) {
1424 icit->inset->validate(features);
1425 if (layout_->needprotect &&
1426 icit->inset->lyxCode() == FOOT_CODE)
1427 features.require("footmisc");
1431 // then the contents
1432 for (pos_type i = 0; i < int(text_.size()) ; ++i) {
1433 BufferEncodings::validate(text_[i], features);
1437 /////////////////////////////////////////////////////////////////////
1441 /////////////////////////////////////////////////////////////////////
1444 Layout const emptyParagraphLayout;
1447 Paragraph::Paragraph()
1448 : d(new Paragraph::Private(this, emptyParagraphLayout))
1455 Paragraph::Paragraph(Paragraph const & par)
1456 : itemdepth(par.itemdepth),
1457 d(new Paragraph::Private(*par.d, this))
1463 Paragraph::Paragraph(Paragraph const & par, pos_type beg, pos_type end)
1464 : itemdepth(par.itemdepth),
1465 d(new Paragraph::Private(*par.d, this, beg, end))
1471 Paragraph & Paragraph::operator=(Paragraph const & par)
1473 // needed as we will destroy the private part before copying it
1475 itemdepth = par.itemdepth;
1479 d = new Private(*par.d, this);
1486 Paragraph::~Paragraph()
1495 // this shall be called just before every "os << ..." action.
1496 void flushString(ostream & os, docstring & s)
1505 void Paragraph::write(ostream & os, BufferParams const & bparams,
1506 depth_type & dth) const
1508 // The beginning or end of a deeper (i.e. nested) area?
1509 if (dth != d->params_.depth()) {
1510 if (d->params_.depth() > dth) {
1511 while (d->params_.depth() > dth) {
1512 os << "\n\\begin_deeper";
1516 while (d->params_.depth() < dth) {
1517 os << "\n\\end_deeper";
1523 // First write the layout
1524 os << "\n\\begin_layout " << to_utf8(d->layout_->name()) << '\n';
1526 d->params_.write(os);
1528 Font font1(inherit_font, bparams.language);
1530 Change running_change = Change(Change::UNCHANGED);
1532 // this string is used as a buffer to avoid repetitive calls
1533 // to to_utf8(), which turn out to be expensive (JMarc)
1534 docstring write_buffer;
1537 for (pos_type i = 0; i <= size(); ++i) {
1539 Change const & change = lookupChange(i);
1540 if (change != running_change)
1541 flushString(os, write_buffer);
1542 Changes::lyxMarkChange(os, bparams, column, running_change, change);
1543 running_change = change;
1548 // Write font changes
1549 Font font2 = getFontSettings(bparams, i);
1550 if (font2 != font1) {
1551 flushString(os, write_buffer);
1552 font2.lyxWriteChanges(font1, os);
1557 char_type const c = d->text_[i];
1560 if (Inset const * inset = getInset(i)) {
1561 flushString(os, write_buffer);
1562 if (inset->directWrite()) {
1563 // international char, let it write
1564 // code directly so it's shorter in
1570 os << "\\begin_inset ";
1572 os << "\n\\end_inset\n\n";
1575 // FIXME This can be removed again once the mystery
1576 // crash has been resolved.
1581 flushString(os, write_buffer);
1582 os << "\n\\backslash\n";
1586 flushString(os, write_buffer);
1587 if (i + 1 < size() && d->text_[i + 1] == ' ') {
1594 if ((column > 70 && c == ' ')
1596 flushString(os, write_buffer);
1600 // this check is to amend a bug. LyX sometimes
1601 // inserts '\0' this could cause problems.
1603 write_buffer.push_back(c);
1605 LYXERR0("NUL char in structure.");
1611 flushString(os, write_buffer);
1612 os << "\n\\end_layout\n";
1613 // FIXME This can be removed again once the mystery
1614 // crash has been resolved.
1619 void Paragraph::validate(LaTeXFeatures & features) const
1621 d->validate(features);
1625 void Paragraph::insert(pos_type start, docstring const & str,
1626 Font const & font, Change const & change)
1628 for (size_t i = 0, n = str.size(); i != n ; ++i)
1629 insertChar(start + i, str[i], font, change);
1633 void Paragraph::appendChar(char_type c, Font const & font,
1634 Change const & change)
1637 d->changes_.insert(change, d->text_.size());
1638 // when appending characters, no need to update tables
1639 d->text_.push_back(c);
1640 setFont(d->text_.size() - 1, font);
1641 d->requestSpellCheck(d->text_.size() - 1);
1645 void Paragraph::appendString(docstring const & s, Font const & font,
1646 Change const & change)
1648 pos_type end = s.size();
1649 size_t oldsize = d->text_.size();
1650 size_t newsize = oldsize + end;
1651 size_t capacity = d->text_.capacity();
1652 if (newsize >= capacity)
1653 d->text_.reserve(max(capacity + 100, newsize));
1655 // when appending characters, no need to update tables
1658 // FIXME: Optimize this!
1659 for (size_t i = oldsize; i != newsize; ++i) {
1661 d->changes_.insert(change, i);
1662 d->requestSpellCheck(i);
1664 d->fontlist_.set(oldsize, font);
1665 d->fontlist_.set(newsize - 1, font);
1669 void Paragraph::insertChar(pos_type pos, char_type c,
1672 d->insertChar(pos, c, Change(trackChanges ?
1673 Change::INSERTED : Change::UNCHANGED));
1677 void Paragraph::insertChar(pos_type pos, char_type c,
1678 Font const & font, bool trackChanges)
1680 d->insertChar(pos, c, Change(trackChanges ?
1681 Change::INSERTED : Change::UNCHANGED));
1686 void Paragraph::insertChar(pos_type pos, char_type c,
1687 Font const & font, Change const & change)
1689 d->insertChar(pos, c, change);
1694 void Paragraph::resetFonts(Font const & font)
1696 d->fontlist_.clear();
1697 d->fontlist_.set(0, font);
1698 d->fontlist_.set(d->text_.size() - 1, font);
1701 // Gets uninstantiated font setting at position.
1702 Font const & Paragraph::getFontSettings(BufferParams const & bparams,
1706 LYXERR0("pos: " << pos << " size: " << size());
1710 FontList::const_iterator cit = d->fontlist_.fontIterator(pos);
1711 if (cit != d->fontlist_.end())
1714 if (pos == size() && !empty())
1715 return getFontSettings(bparams, pos - 1);
1717 // Optimisation: avoid a full font instantiation if there is no
1718 // language change from previous call.
1719 static Font previous_font;
1720 static Language const * previous_lang = 0;
1721 Language const * lang = getParLanguage(bparams);
1722 if (lang != previous_lang) {
1723 previous_lang = lang;
1724 previous_font = Font(inherit_font, lang);
1726 return previous_font;
1730 FontSpan Paragraph::fontSpan(pos_type pos) const
1732 LBUFERR(pos < size());
1735 FontList::const_iterator cit = d->fontlist_.begin();
1736 FontList::const_iterator end = d->fontlist_.end();
1737 for (; cit != end; ++cit) {
1738 if (cit->pos() >= pos) {
1739 if (pos >= beginOfBody())
1740 return FontSpan(max(start, beginOfBody()),
1743 return FontSpan(start,
1744 min(beginOfBody() - 1,
1747 start = cit->pos() + 1;
1750 // This should not happen, but if so, we take no chances.
1751 LYXERR0("Paragraph::fontSpan: position not found in fontinfo table!");
1752 LASSERT(false, return FontSpan(pos, pos));
1756 // Gets uninstantiated font setting at position 0
1757 Font const & Paragraph::getFirstFontSettings(BufferParams const & bparams) const
1759 if (!empty() && !d->fontlist_.empty())
1760 return d->fontlist_.begin()->font();
1762 // Optimisation: avoid a full font instantiation if there is no
1763 // language change from previous call.
1764 static Font previous_font;
1765 static Language const * previous_lang = 0;
1766 if (bparams.language != previous_lang) {
1767 previous_lang = bparams.language;
1768 previous_font = Font(inherit_font, bparams.language);
1771 return previous_font;
1775 // Gets the fully instantiated font at a given position in a paragraph
1776 // This is basically the same function as Text::GetFont() in text2.cpp.
1777 // The difference is that this one is used for generating the LaTeX file,
1778 // and thus cosmetic "improvements" are disallowed: This has to deliver
1779 // the true picture of the buffer. (Asger)
1780 Font const Paragraph::getFont(BufferParams const & bparams, pos_type pos,
1781 Font const & outerfont) const
1785 Font font = getFontSettings(bparams, pos);
1787 pos_type const body_pos = beginOfBody();
1788 FontInfo & fi = font.fontInfo();
1790 fi.realize(d->layout_->labelfont);
1792 fi.realize(d->layout_->font);
1794 fi.realize(outerfont.fontInfo());
1795 fi.realize(bparams.getFont().fontInfo());
1801 Font const Paragraph::getLabelFont
1802 (BufferParams const & bparams, Font const & outerfont) const
1804 FontInfo tmpfont = d->layout_->labelfont;
1805 tmpfont.realize(outerfont.fontInfo());
1806 tmpfont.realize(bparams.getFont().fontInfo());
1807 return Font(tmpfont, getParLanguage(bparams));
1811 Font const Paragraph::getLayoutFont
1812 (BufferParams const & bparams, Font const & outerfont) const
1814 FontInfo tmpfont = d->layout_->font;
1815 tmpfont.realize(outerfont.fontInfo());
1816 tmpfont.realize(bparams.getFont().fontInfo());
1817 return Font(tmpfont, getParLanguage(bparams));
1821 /// Returns the height of the highest font in range
1822 FontSize Paragraph::highestFontInRange
1823 (pos_type startpos, pos_type endpos, FontSize def_size) const
1825 return d->fontlist_.highestInRange(startpos, endpos, def_size);
1829 char_type Paragraph::getUChar(BufferParams const & bparams, pos_type pos) const
1831 char_type c = d->text_[pos];
1832 if (!getFontSettings(bparams, pos).isRightToLeft())
1835 // FIXME: The arabic special casing is due to the difference of arabic
1836 // round brackets input introduced in r18599. Check if this should be
1837 // unified with Hebrew or at least if all bracket types should be
1838 // handled the same (file format change in either case).
1839 string const & lang = getFontSettings(bparams, pos).language()->lang();
1840 bool const arabic = lang == "arabic_arabtex" || lang == "arabic_arabi"
1845 uc = arabic ? c : ')';
1848 uc = arabic ? c : '(';
1874 void Paragraph::setFont(pos_type pos, Font const & font)
1876 LASSERT(pos <= size(), return);
1878 // First, reduce font against layout/label font
1879 // Update: The setCharFont() routine in text2.cpp already
1880 // reduces font, so we don't need to do that here. (Asger)
1882 d->fontlist_.set(pos, font);
1886 void Paragraph::makeSameLayout(Paragraph const & par)
1888 d->layout_ = par.d->layout_;
1889 d->params_ = par.d->params_;
1893 bool Paragraph::stripLeadingSpaces(bool trackChanges)
1895 if (isFreeSpacing())
1901 while (pos < size() && (isNewline(pos) || isLineSeparator(pos))) {
1902 if (eraseChar(pos, trackChanges))
1908 return count > 0 || pos > 0;
1912 bool Paragraph::hasSameLayout(Paragraph const & par) const
1914 return par.d->layout_ == d->layout_
1915 && d->params_.sameLayout(par.d->params_);
1919 depth_type Paragraph::getDepth() const
1921 return d->params_.depth();
1925 depth_type Paragraph::getMaxDepthAfter() const
1927 if (d->layout_->isEnvironment())
1928 return d->params_.depth() + 1;
1930 return d->params_.depth();
1934 char Paragraph::getAlign() const
1936 if (d->params_.align() == LYX_ALIGN_LAYOUT)
1937 return d->layout_->align;
1939 return d->params_.align();
1943 docstring const & Paragraph::labelString() const
1945 return d->params_.labelString();
1949 // the next two functions are for the manual labels
1950 docstring const Paragraph::getLabelWidthString() const
1952 if (d->layout_->margintype == MARGIN_MANUAL
1953 || d->layout_->latextype == LATEX_BIB_ENVIRONMENT)
1954 return d->params_.labelWidthString();
1956 return _("Senseless with this layout!");
1960 void Paragraph::setLabelWidthString(docstring const & s)
1962 d->params_.labelWidthString(s);
1966 docstring Paragraph::expandLabel(Layout const & layout,
1967 BufferParams const & bparams) const
1969 return expandParagraphLabel(layout, bparams, true);
1973 docstring Paragraph::expandDocBookLabel(Layout const & layout,
1974 BufferParams const & bparams) const
1976 return expandParagraphLabel(layout, bparams, false);
1980 docstring Paragraph::expandParagraphLabel(Layout const & layout,
1981 BufferParams const & bparams, bool process_appendix) const
1983 DocumentClass const & tclass = bparams.documentClass();
1984 string const & lang = getParLanguage(bparams)->code();
1985 bool const in_appendix = process_appendix && d->params_.appendix();
1986 docstring fmt = translateIfPossible(layout.labelstring(in_appendix), lang);
1988 if (fmt.empty() && !layout.counter.empty())
1989 return tclass.counters().theCounter(layout.counter, lang);
1991 // handle 'inherited level parts' in 'fmt',
1992 // i.e. the stuff between '@' in '@Section@.\arabic{subsection}'
1993 size_t const i = fmt.find('@', 0);
1994 if (i != docstring::npos) {
1995 size_t const j = fmt.find('@', i + 1);
1996 if (j != docstring::npos) {
1997 docstring parent(fmt, i + 1, j - i - 1);
1998 docstring label = from_ascii("??");
1999 if (tclass.hasLayout(parent))
2000 docstring label = expandParagraphLabel(tclass[parent], bparams,
2002 fmt = docstring(fmt, 0, i) + label
2003 + docstring(fmt, j + 1, docstring::npos);
2007 return tclass.counters().counterLabel(fmt, lang);
2011 void Paragraph::applyLayout(Layout const & new_layout)
2013 d->layout_ = &new_layout;
2014 LyXAlignment const oldAlign = d->params_.align();
2016 if (!(oldAlign & d->layout_->alignpossible)) {
2017 frontend::Alert::warning(_("Alignment not permitted"),
2018 _("The new layout does not permit the alignment previously used.\nSetting to default."));
2019 d->params_.align(LYX_ALIGN_LAYOUT);
2024 pos_type Paragraph::beginOfBody() const
2026 return d->begin_of_body_;
2030 void Paragraph::setBeginOfBody()
2032 if (d->layout_->labeltype != LABEL_MANUAL) {
2033 d->begin_of_body_ = 0;
2037 // Unroll the first two cycles of the loop
2038 // and remember the previous character to
2039 // remove unnecessary getChar() calls
2041 pos_type end = size();
2042 if (i < end && !(isNewline(i) || isEnvSeparator(i))) {
2044 char_type previous_char = 0;
2047 previous_char = d->text_[i];
2048 if (!(isNewline(i) || isEnvSeparator(i))) {
2050 while (i < end && previous_char != ' ') {
2052 if (isNewline(i) || isEnvSeparator(i))
2055 previous_char = temp;
2061 d->begin_of_body_ = i;
2065 bool Paragraph::allowParagraphCustomization() const
2067 return inInset().allowParagraphCustomization();
2071 bool Paragraph::usePlainLayout() const
2073 return inInset().usePlainLayout();
2077 bool Paragraph::isPassThru() const
2079 return inInset().isPassThru() || d->layout_->pass_thru;
2084 // paragraphs inside floats need different alignment tags to avoid
2087 bool noTrivlistCentering(InsetCode code)
2089 return code == FLOAT_CODE
2090 || code == WRAP_CODE
2091 || code == CELL_CODE;
2095 string correction(string const & orig)
2097 if (orig == "flushleft")
2098 return "raggedright";
2099 if (orig == "flushright")
2100 return "raggedleft";
2101 if (orig == "center")
2107 bool corrected_env(otexstream & os, string const & suffix, string const & env,
2108 InsetCode code, bool const lastpar, int & col)
2110 string macro = suffix + "{";
2111 if (noTrivlistCentering(code)) {
2113 // the last paragraph in non-trivlist-aligned
2114 // context is special (to avoid unwanted whitespace)
2115 if (suffix == "\\begin") {
2116 macro = "\\" + correction(env) + "{}";
2117 os << from_ascii(macro);
2118 col += macro.size();
2123 macro += correction(env);
2127 if (suffix == "\\par\\end") {
2131 os << from_ascii(macro);
2132 col += macro.size();
2133 if (suffix == "\\begin") {
2143 int Paragraph::Private::startTeXParParams(BufferParams const & bparams,
2144 otexstream & os, OutputParams const & runparams) const
2149 (bparams.paragraph_separation == BufferParams::ParagraphIndentSeparation) ?
2150 (layout_->toggle_indent != ITOGGLE_NEVER) :
2151 (layout_->toggle_indent == ITOGGLE_ALWAYS);
2153 if (canindent && params_.noindent() && !layout_->pass_thru) {
2154 os << "\\noindent ";
2158 LyXAlignment const curAlign = params_.align();
2160 if (curAlign == layout_->align)
2164 case LYX_ALIGN_NONE:
2165 case LYX_ALIGN_BLOCK:
2166 case LYX_ALIGN_LAYOUT:
2167 case LYX_ALIGN_SPECIAL:
2168 case LYX_ALIGN_DECIMAL:
2170 case LYX_ALIGN_LEFT:
2171 case LYX_ALIGN_RIGHT:
2172 case LYX_ALIGN_CENTER:
2173 if (runparams.moving_arg) {
2180 string const begin_tag = "\\begin";
2181 InsetCode code = ownerCode();
2182 bool const lastpar = runparams.isLastPar;
2185 case LYX_ALIGN_NONE:
2186 case LYX_ALIGN_BLOCK:
2187 case LYX_ALIGN_LAYOUT:
2188 case LYX_ALIGN_SPECIAL:
2189 case LYX_ALIGN_DECIMAL:
2191 case LYX_ALIGN_LEFT: {
2192 if (owner_->getParLanguage(bparams)->babel() != "hebrew")
2193 corrected_env(os, begin_tag, "flushleft", code, lastpar, column);
2195 corrected_env(os, begin_tag, "flushright", code, lastpar, column);
2197 } case LYX_ALIGN_RIGHT: {
2199 if (owner_->getParLanguage(bparams)->babel() != "hebrew")
2200 corrected_env(os, begin_tag, "flushright", code, lastpar, column);
2202 corrected_env(os, begin_tag, "flushleft", code, lastpar, column);
2204 } case LYX_ALIGN_CENTER: {
2205 corrected_env(os, begin_tag, "center", code, lastpar, column);
2214 bool Paragraph::Private::endTeXParParams(BufferParams const & bparams,
2215 otexstream & os, OutputParams const & runparams) const
2217 LyXAlignment const curAlign = params_.align();
2219 if (curAlign == layout_->align)
2223 case LYX_ALIGN_NONE:
2224 case LYX_ALIGN_BLOCK:
2225 case LYX_ALIGN_LAYOUT:
2226 case LYX_ALIGN_SPECIAL:
2227 case LYX_ALIGN_DECIMAL:
2229 case LYX_ALIGN_LEFT:
2230 case LYX_ALIGN_RIGHT:
2231 case LYX_ALIGN_CENTER:
2232 if (runparams.moving_arg)
2237 bool output = false;
2239 string const end_tag = "\\par\\end";
2240 InsetCode code = ownerCode();
2241 bool const lastpar = runparams.isLastPar;
2244 case LYX_ALIGN_NONE:
2245 case LYX_ALIGN_BLOCK:
2246 case LYX_ALIGN_LAYOUT:
2247 case LYX_ALIGN_SPECIAL:
2248 case LYX_ALIGN_DECIMAL:
2250 case LYX_ALIGN_LEFT: {
2251 if (owner_->getParLanguage(bparams)->babel() != "hebrew")
2252 output = corrected_env(os, end_tag, "flushleft", code, lastpar, col);
2254 output = corrected_env(os, end_tag, "flushright", code, lastpar, col);
2256 } case LYX_ALIGN_RIGHT: {
2257 if (owner_->getParLanguage(bparams)->babel() != "hebrew")
2258 output = corrected_env(os, end_tag, "flushright", code, lastpar, col);
2260 output = corrected_env(os, end_tag, "flushleft", code, lastpar, col);
2262 } case LYX_ALIGN_CENTER: {
2263 corrected_env(os, end_tag, "center", code, lastpar, col);
2268 return output || lastpar;
2272 // This one spits out the text of the paragraph
2273 void Paragraph::latex(BufferParams const & bparams,
2274 Font const & outerfont,
2276 OutputParams const & runparams,
2277 int start_pos, int end_pos, bool force) const
2279 LYXERR(Debug::LATEX, "Paragraph::latex... " << this);
2281 // FIXME This check should not be needed. Perhaps issue an
2282 // error if it triggers.
2283 Layout const & style = inInset().forcePlainLayout() ?
2284 bparams.documentClass().plainLayout() : *d->layout_;
2286 if (!force && style.inpreamble)
2289 bool const allowcust = allowParagraphCustomization();
2291 // Current base font for all inherited font changes, without any
2292 // change caused by an individual character, except for the language:
2293 // It is set to the language of the first character.
2294 // As long as we are in the label, this font is the base font of the
2295 // label. Before the first body character it is set to the base font
2299 // Maybe we have to create a optional argument.
2300 pos_type body_pos = beginOfBody();
2301 unsigned int column = 0;
2304 // the optional argument is kept in curly brackets in
2305 // case it contains a ']'
2306 // This is not strictly needed, but if this is changed it
2307 // would be a file format change, and tex2lyx would need
2308 // to be adjusted, since it unconditionally removes the
2309 // braces when it parses \item.
2312 basefont = getLabelFont(bparams, outerfont);
2314 basefont = getLayoutFont(bparams, outerfont);
2317 // Which font is currently active?
2318 Font running_font(basefont);
2319 // Do we have an open font change?
2320 bool open_font = false;
2322 Change runningChange = Change(Change::UNCHANGED);
2324 Encoding const * const prev_encoding = runparams.encoding;
2326 os.texrow().start(id(), 0);
2328 // if the paragraph is empty, the loop will not be entered at all
2330 if (style.isCommand()) {
2334 if (!style.leftdelim().empty()) {
2335 os << style.leftdelim();
2336 column += style.leftdelim().size();
2339 column += d->startTeXParParams(bparams, os, runparams);
2342 for (pos_type i = 0; i < size(); ++i) {
2343 // First char in paragraph or after label?
2344 if (i == body_pos) {
2347 column += running_font.latexWriteEndChanges(
2348 os, bparams, runparams,
2349 basefont, basefont);
2352 basefont = getLayoutFont(bparams, outerfont);
2353 running_font = basefont;
2355 column += Changes::latexMarkChange(os, bparams,
2356 runningChange, Change(Change::UNCHANGED),
2358 runningChange = Change(Change::UNCHANGED);
2363 if (style.isCommand()) {
2368 if (!style.leftdelim().empty()) {
2369 os << style.leftdelim();
2370 column += style.leftdelim().size();
2374 column += d->startTeXParParams(bparams, os,
2378 Change const & change = runparams.inDeletedInset
2379 ? runparams.changeOfDeletedInset : lookupChange(i);
2381 if (bparams.output_changes && runningChange != change) {
2383 column += running_font.latexWriteEndChanges(
2384 os, bparams, runparams, basefont, basefont);
2387 basefont = getLayoutFont(bparams, outerfont);
2388 running_font = basefont;
2390 column += Changes::latexMarkChange(os, bparams, runningChange,
2392 runningChange = change;
2395 // do not output text which is marked deleted
2396 // if change tracking output is disabled
2397 if (!bparams.output_changes && change.deleted()) {
2403 // Fully instantiated font
2404 Font const font = getFont(bparams, i, outerfont);
2406 Font const last_font = running_font;
2408 // Do we need to close the previous font?
2410 (font != running_font ||
2411 font.language() != running_font.language()))
2413 column += running_font.latexWriteEndChanges(
2414 os, bparams, runparams, basefont,
2415 (i == body_pos-1) ? basefont : font);
2416 running_font = basefont;
2420 string const running_lang = runparams.use_polyglossia ?
2421 running_font.language()->polyglossia() : running_font.language()->babel();
2422 // close babel's font environment before opening CJK.
2423 string const lang_end_command = runparams.use_polyglossia ?
2424 "\\end{$$lang}" : lyxrc.language_command_end;
2425 if (!running_lang.empty() &&
2426 font.language()->encoding()->package() == Encoding::CJK) {
2427 string end_tag = subst(lang_end_command,
2430 os << from_ascii(end_tag);
2431 column += end_tag.length();
2434 // Switch file encoding if necessary (and allowed)
2435 if (!runparams.pass_thru && !style.pass_thru &&
2436 runparams.encoding->package() != Encoding::none &&
2437 font.language()->encoding()->package() != Encoding::none) {
2438 pair<bool, int> const enc_switch =
2439 switchEncoding(os.os(), bparams, runparams,
2440 *(font.language()->encoding()));
2441 if (enc_switch.first) {
2442 column += enc_switch.second;
2443 runparams.encoding = font.language()->encoding();
2447 char_type const c = d->text_[i];
2449 // Do we need to change font?
2450 if ((font != running_font ||
2451 font.language() != running_font.language()) &&
2454 odocstringstream ods;
2455 column += font.latexWriteStartChanges(ods, bparams,
2456 runparams, basefont,
2458 running_font = font;
2460 docstring fontchange = ods.str();
2461 // check whether the fontchange ends with a \\textcolor
2462 // modifier and the text starts with a space (bug 4473)
2463 docstring const last_modifier = rsplit(fontchange, '\\');
2464 if (prefixIs(last_modifier, from_ascii("textcolor")) && c == ' ')
2465 os << fontchange << from_ascii("{}");
2466 // check if the fontchange ends with a trailing blank
2467 // (like "\small " (see bug 3382)
2468 else if (suffixIs(fontchange, ' ') && c == ' ')
2469 os << fontchange.substr(0, fontchange.size() - 1)
2470 << from_ascii("{}");
2475 // FIXME: think about end_pos implementation...
2476 if (c == ' ' && i >= start_pos && (end_pos == -1 || i < end_pos)) {
2477 // FIXME: integrate this case in latexSpecialChar
2478 // Do not print the separation of the optional argument
2479 // if style.pass_thru is false. This works because
2480 // latexSpecialChar ignores spaces if
2481 // style.pass_thru is false.
2482 if (i != body_pos - 1) {
2483 if (d->simpleTeXBlanks(runparams, os,
2484 i, column, font, style)) {
2485 // A surrogate pair was output. We
2486 // must not call latexSpecialChar
2487 // in this iteration, since it would output
2488 // the combining character again.
2495 OutputParams rp = runparams;
2496 rp.free_spacing = style.free_spacing;
2497 rp.local_font = &font;
2498 rp.intitle = style.intitle;
2500 // Two major modes: LaTeX or plain
2501 // Handle here those cases common to both modes
2502 // and then split to handle the two modes separately.
2503 if (c == META_INSET) {
2504 if (i >= start_pos && (end_pos == -1 || i < end_pos)) {
2505 d->latexInset(bparams, os, rp, running_font,
2506 basefont, outerfont, open_font,
2507 runningChange, style, i, column);
2510 if (i >= start_pos && (end_pos == -1 || i < end_pos)) {
2512 d->latexSpecialChar(os, bparams, rp, running_font, runningChange,
2513 style, i, end_pos, column);
2514 } catch (EncodingException & e) {
2515 if (runparams.dryrun) {
2516 os << "<" << _("LyX Warning: ")
2517 << _("uncodable character") << " '";
2521 // add location information and throw again.
2530 // Set the encoding to that returned from latexSpecialChar (see
2531 // comment for encoding member in OutputParams.h)
2532 runparams.encoding = rp.encoding;
2535 // If we have an open font definition, we have to close it
2537 #ifdef FIXED_LANGUAGE_END_DETECTION
2539 running_font.latexWriteEndChanges(os, bparams,
2540 runparams, basefont,
2541 next_->getFont(bparams, 0, outerfont));
2543 running_font.latexWriteEndChanges(os, bparams,
2544 runparams, basefont, basefont);
2547 //FIXME: For now we ALWAYS have to close the foreign font settings if they are
2548 //FIXME: there as we start another \selectlanguage with the next paragraph if
2549 //FIXME: we are in need of this. This should be fixed sometime (Jug)
2550 running_font.latexWriteEndChanges(os, bparams, runparams,
2551 basefont, basefont);
2555 column += Changes::latexMarkChange(os, bparams, runningChange,
2556 Change(Change::UNCHANGED), runparams);
2558 // Needed if there is an optional argument but no contents.
2559 if (body_pos > 0 && body_pos == size()) {
2563 if (!style.rightdelim().empty()) {
2564 os << style.rightdelim();
2565 column += style.rightdelim().size();
2568 if (allowcust && d->endTeXParParams(bparams, os, runparams)
2569 && runparams.encoding != prev_encoding) {
2570 runparams.encoding = prev_encoding;
2571 if (!runparams.isFullUnicode())
2572 os << setEncoding(prev_encoding->iconvName());
2575 LYXERR(Debug::LATEX, "Paragraph::latex... done " << this);
2579 bool Paragraph::emptyTag() const
2581 for (pos_type i = 0; i < size(); ++i) {
2582 if (Inset const * inset = getInset(i)) {
2583 InsetCode lyx_code = inset->lyxCode();
2584 // FIXME testing like that is wrong. What is
2586 if (lyx_code != TOC_CODE &&
2587 lyx_code != INCLUDE_CODE &&
2588 lyx_code != GRAPHICS_CODE &&
2589 lyx_code != ERT_CODE &&
2590 lyx_code != LISTINGS_CODE &&
2591 lyx_code != FLOAT_CODE &&
2592 lyx_code != TABULAR_CODE) {
2596 char_type c = d->text_[i];
2597 if (c != ' ' && c != '\t')
2605 string Paragraph::getID(Buffer const & buf, OutputParams const & runparams)
2608 for (pos_type i = 0; i < size(); ++i) {
2609 if (Inset const * inset = getInset(i)) {
2610 InsetCode lyx_code = inset->lyxCode();
2611 if (lyx_code == LABEL_CODE) {
2612 InsetLabel const * const il = static_cast<InsetLabel const *>(inset);
2613 docstring const & id = il->getParam("name");
2614 return "id='" + to_utf8(sgml::cleanID(buf, runparams, id)) + "'";
2622 pos_type Paragraph::firstWordDocBook(odocstream & os, OutputParams const & runparams)
2626 for (i = 0; i < size(); ++i) {
2627 if (Inset const * inset = getInset(i)) {
2628 inset->docbook(os, runparams);
2630 char_type c = d->text_[i];
2633 os << sgml::escapeChar(c);
2640 pos_type Paragraph::firstWordLyXHTML(XHTMLStream & xs, OutputParams const & runparams)
2644 for (i = 0; i < size(); ++i) {
2645 if (Inset const * inset = getInset(i)) {
2646 inset->xhtml(xs, runparams);
2648 char_type c = d->text_[i];
2658 bool Paragraph::Private::onlyText(Buffer const & buf, Font const & outerfont, pos_type initial) const
2661 pos_type size = text_.size();
2662 for (pos_type i = initial; i < size; ++i) {
2663 Font font = owner_->getFont(buf.params(), i, outerfont);
2664 if (text_[i] == META_INSET)
2666 if (i != initial && font != font_old)
2675 void Paragraph::simpleDocBookOnePar(Buffer const & buf,
2677 OutputParams const & runparams,
2678 Font const & outerfont,
2679 pos_type initial) const
2681 bool emph_flag = false;
2683 Layout const & style = *d->layout_;
2685 style.labeltype == LABEL_MANUAL ? style.labelfont : style.font;
2687 if (style.pass_thru && !d->onlyText(buf, outerfont, initial))
2690 // parsing main loop
2691 for (pos_type i = initial; i < size(); ++i) {
2692 Font font = getFont(buf.params(), i, outerfont);
2694 // handle <emphasis> tag
2695 if (font_old.emph() != font.fontInfo().emph()) {
2696 if (font.fontInfo().emph() == FONT_ON) {
2699 } else if (i != initial) {
2700 os << "</emphasis>";
2705 if (Inset const * inset = getInset(i)) {
2706 inset->docbook(os, runparams);
2708 char_type c = d->text_[i];
2710 if (style.pass_thru)
2713 os << sgml::escapeChar(c);
2715 font_old = font.fontInfo();
2719 os << "</emphasis>";
2722 if (style.free_spacing)
2724 if (style.pass_thru && !d->onlyText(buf, outerfont, initial))
2730 void doFontSwitch(vector<html::FontTag> & tagsToOpen,
2731 vector<html::EndFontTag> & tagsToClose,
2732 bool & flag, FontState curstate, html::FontTypes type)
2734 if (curstate == FONT_ON) {
2735 tagsToOpen.push_back(html::FontTag(type));
2738 tagsToClose.push_back(html::EndFontTag(type));
2745 docstring Paragraph::simpleLyXHTMLOnePar(Buffer const & buf,
2747 OutputParams const & runparams,
2748 Font const & outerfont,
2749 pos_type initial) const
2753 // track whether we have opened these tags
2754 bool emph_flag = false;
2755 bool bold_flag = false;
2756 bool noun_flag = false;
2757 bool ubar_flag = false;
2758 bool dbar_flag = false;
2759 bool sout_flag = false;
2760 bool wave_flag = false;
2762 bool shap_flag = false;
2764 bool faml_flag = false;
2766 bool size_flag = false;
2768 Layout const & style = *d->layout_;
2770 xs.startParagraph(allowEmpty());
2773 style.labeltype == LABEL_MANUAL ? style.labelfont : style.font;
2775 FontShape curr_fs = INHERIT_SHAPE;
2776 FontFamily curr_fam = INHERIT_FAMILY;
2777 FontSize curr_size = FONT_SIZE_INHERIT;
2779 string const default_family =
2780 buf.masterBuffer()->params().fonts_default_family;
2782 vector<html::FontTag> tagsToOpen;
2783 vector<html::EndFontTag> tagsToClose;
2785 // parsing main loop
2786 for (pos_type i = initial; i < size(); ++i) {
2787 // let's not show deleted material in the output
2791 Font const font = getFont(buf.masterBuffer()->params(), i, outerfont);
2794 FontState curstate = font.fontInfo().emph();
2795 if (font_old.emph() != curstate)
2796 doFontSwitch(tagsToOpen, tagsToClose, emph_flag, curstate, html::FT_EMPH);
2799 curstate = font.fontInfo().noun();
2800 if (font_old.noun() != curstate)
2801 doFontSwitch(tagsToOpen, tagsToClose, noun_flag, curstate, html::FT_NOUN);
2804 curstate = font.fontInfo().underbar();
2805 if (font_old.underbar() != curstate)
2806 doFontSwitch(tagsToOpen, tagsToClose, ubar_flag, curstate, html::FT_UBAR);
2809 curstate = font.fontInfo().strikeout();
2810 if (font_old.strikeout() != curstate)
2811 doFontSwitch(tagsToOpen, tagsToClose, sout_flag, curstate, html::FT_SOUT);
2814 curstate = font.fontInfo().uuline();
2815 if (font_old.uuline() != curstate)
2816 doFontSwitch(tagsToOpen, tagsToClose, dbar_flag, curstate, html::FT_DBAR);
2819 curstate = font.fontInfo().uwave();
2820 if (font_old.uwave() != curstate)
2821 doFontSwitch(tagsToOpen, tagsToClose, wave_flag, curstate, html::FT_WAVE);
2824 // a little hackish, but allows us to reuse what we have.
2825 curstate = (font.fontInfo().series() == BOLD_SERIES ? FONT_ON : FONT_OFF);
2826 if (font_old.series() != font.fontInfo().series())
2827 doFontSwitch(tagsToOpen, tagsToClose, bold_flag, curstate, html::FT_BOLD);
2830 curr_fs = font.fontInfo().shape();
2831 FontShape old_fs = font_old.shape();
2832 if (old_fs != curr_fs) {
2836 tagsToClose.push_back(html::EndFontTag(html::FT_ITALIC));
2839 tagsToClose.push_back(html::EndFontTag(html::FT_SLANTED));
2841 case SMALLCAPS_SHAPE:
2842 tagsToClose.push_back(html::EndFontTag(html::FT_SMALLCAPS));
2848 // the other tags are for internal use
2856 tagsToOpen.push_back(html::FontTag(html::FT_ITALIC));
2860 tagsToOpen.push_back(html::FontTag(html::FT_SLANTED));
2863 case SMALLCAPS_SHAPE:
2864 tagsToOpen.push_back(html::FontTag(html::FT_SMALLCAPS));
2871 // the other tags are for internal use
2878 curr_fam = font.fontInfo().family();
2879 FontFamily old_fam = font_old.family();
2880 if (old_fam != curr_fam) {
2884 tagsToClose.push_back(html::EndFontTag(html::FT_ROMAN));
2887 tagsToClose.push_back(html::EndFontTag(html::FT_SANS));
2889 case TYPEWRITER_FAMILY:
2890 tagsToClose.push_back(html::EndFontTag(html::FT_TYPE));
2892 case INHERIT_FAMILY:
2895 // the other tags are for internal use
2903 // we will treat a "default" font family as roman, since we have
2904 // no other idea what to do.
2905 if (default_family != "rmdefault" && default_family != "default") {
2906 tagsToOpen.push_back(html::FontTag(html::FT_ROMAN));
2911 if (default_family != "sfdefault") {
2912 tagsToOpen.push_back(html::FontTag(html::FT_SANS));
2916 case TYPEWRITER_FAMILY:
2917 if (default_family != "ttdefault") {
2918 tagsToOpen.push_back(html::FontTag(html::FT_TYPE));
2922 case INHERIT_FAMILY:
2925 // the other tags are for internal use
2932 curr_size = font.fontInfo().size();
2933 FontSize old_size = font_old.size();
2934 if (old_size != curr_size) {
2937 case FONT_SIZE_TINY:
2938 tagsToClose.push_back(html::EndFontTag(html::FT_SIZE_TINY));
2940 case FONT_SIZE_SCRIPT:
2941 tagsToClose.push_back(html::EndFontTag(html::FT_SIZE_SCRIPT));
2943 case FONT_SIZE_FOOTNOTE:
2944 tagsToClose.push_back(html::EndFontTag(html::FT_SIZE_FOOTNOTE));
2946 case FONT_SIZE_SMALL:
2947 tagsToClose.push_back(html::EndFontTag(html::FT_SIZE_SMALL));
2949 case FONT_SIZE_LARGE:
2950 tagsToClose.push_back(html::EndFontTag(html::FT_SIZE_LARGE));
2952 case FONT_SIZE_LARGER:
2953 tagsToClose.push_back(html::EndFontTag(html::FT_SIZE_LARGER));
2955 case FONT_SIZE_LARGEST:
2956 tagsToClose.push_back(html::EndFontTag(html::FT_SIZE_LARGEST));
2958 case FONT_SIZE_HUGE:
2959 tagsToClose.push_back(html::EndFontTag(html::FT_SIZE_HUGE));
2961 case FONT_SIZE_HUGER:
2962 tagsToClose.push_back(html::EndFontTag(html::FT_SIZE_HUGER));
2964 case FONT_SIZE_INCREASE:
2965 tagsToClose.push_back(html::EndFontTag(html::FT_SIZE_INCREASE));
2967 case FONT_SIZE_DECREASE:
2968 tagsToClose.push_back(html::EndFontTag(html::FT_SIZE_DECREASE));
2970 case FONT_SIZE_INHERIT:
2971 case FONT_SIZE_NORMAL:
2974 // the other tags are for internal use
2980 switch (curr_size) {
2981 case FONT_SIZE_TINY:
2982 tagsToOpen.push_back(html::FontTag(html::FT_SIZE_TINY));
2985 case FONT_SIZE_SCRIPT:
2986 tagsToOpen.push_back(html::FontTag(html::FT_SIZE_SCRIPT));
2989 case FONT_SIZE_FOOTNOTE:
2990 tagsToOpen.push_back(html::FontTag(html::FT_SIZE_FOOTNOTE));
2993 case FONT_SIZE_SMALL:
2994 tagsToOpen.push_back(html::FontTag(html::FT_SIZE_SMALL));
2997 case FONT_SIZE_LARGE:
2998 tagsToOpen.push_back(html::FontTag(html::FT_SIZE_LARGE));
3001 case FONT_SIZE_LARGER:
3002 tagsToOpen.push_back(html::FontTag(html::FT_SIZE_LARGER));
3005 case FONT_SIZE_LARGEST:
3006 tagsToOpen.push_back(html::FontTag(html::FT_SIZE_LARGEST));
3009 case FONT_SIZE_HUGE:
3010 tagsToOpen.push_back(html::FontTag(html::FT_SIZE_HUGE));
3013 case FONT_SIZE_HUGER:
3014 tagsToOpen.push_back(html::FontTag(html::FT_SIZE_HUGER));
3017 case FONT_SIZE_INCREASE:
3018 tagsToOpen.push_back(html::FontTag(html::FT_SIZE_INCREASE));
3021 case FONT_SIZE_DECREASE:
3022 tagsToOpen.push_back(html::FontTag(html::FT_SIZE_DECREASE));
3025 case FONT_SIZE_NORMAL:
3026 case FONT_SIZE_INHERIT:
3029 // the other tags are for internal use
3036 // Other such tags? What about the other text ranges?
3038 vector<html::EndFontTag>::const_iterator cit = tagsToClose.begin();
3039 vector<html::EndFontTag>::const_iterator cen = tagsToClose.end();
3040 for (; cit != cen; ++cit)
3043 vector<html::FontTag>::const_iterator sit = tagsToOpen.begin();
3044 vector<html::FontTag>::const_iterator sen = tagsToOpen.end();
3045 for (; sit != sen; ++sit)
3048 tagsToClose.clear();
3051 Inset const * inset = getInset(i);
3053 if (!runparams.for_toc || inset->isInToc()) {
3054 OutputParams np = runparams;
3055 np.local_font = &font;
3056 if (!inset->getLayout().htmlisblock())
3057 np.html_in_par = true;
3058 retval += inset->xhtml(xs, np);
3061 char_type c = getUChar(buf.masterBuffer()->params(), i);
3064 font_old = font.fontInfo();
3073 bool Paragraph::isHfill(pos_type pos) const
3075 Inset const * inset = getInset(pos);
3076 return inset && inset->isHfill();
3080 bool Paragraph::isNewline(pos_type pos) const
3082 Inset const * inset = getInset(pos);
3083 return inset && inset->lyxCode() == NEWLINE_CODE;
3087 bool Paragraph::isEnvSeparator(pos_type pos) const
3089 Inset const * inset = getInset(pos);
3090 return inset && inset->lyxCode() == SEPARATOR_CODE;
3094 bool Paragraph::isLineSeparator(pos_type pos) const
3096 char_type const c = d->text_[pos];
3097 if (isLineSeparatorChar(c))
3099 Inset const * inset = getInset(pos);
3100 return inset && inset->isLineSeparator();
3104 bool Paragraph::isWordSeparator(pos_type pos) const
3108 if (Inset const * inset = getInset(pos))
3109 return !inset->isLetter();
3110 // if we have a hard hyphen (no en- or emdash) or apostrophe
3111 // we pass this to the spell checker
3112 // FIXME: this method is subject to change, visit
3113 // https://bugzilla.mozilla.org/show_bug.cgi?id=355178
3114 // to get an impression how complex this is.
3115 if (isHardHyphenOrApostrophe(pos))
3117 char_type const c = d->text_[pos];
3118 // We want to pass the escape chars to the spellchecker
3119 docstring const escape_chars = from_utf8(lyxrc.spellchecker_esc_chars);
3120 return !isLetterChar(c) && !isDigitASCII(c) && !contains(escape_chars, c);
3124 bool Paragraph::isHardHyphenOrApostrophe(pos_type pos) const
3126 pos_type const psize = size();
3129 char_type const c = d->text_[pos];
3130 if (c != '-' && c != '\'')
3132 int nextpos = pos + 1;
3133 int prevpos = pos > 0 ? pos - 1 : 0;
3134 if ((nextpos == psize || isSpace(nextpos))
3135 && (pos == 0 || isSpace(prevpos)))
3141 FontSpan const & Paragraph::getSpellRange(pos_type pos) const
3143 return d->speller_state_.getRange(pos);
3147 bool Paragraph::isChar(pos_type pos) const
3149 if (Inset const * inset = getInset(pos))
3150 return inset->isChar();
3151 char_type const c = d->text_[pos];
3152 return !isLetterChar(c) && !isDigitASCII(c) && !lyx::isSpace(c);
3156 bool Paragraph::isSpace(pos_type pos) const
3158 if (Inset const * inset = getInset(pos))
3159 return inset->isSpace();
3160 char_type const c = d->text_[pos];
3161 return lyx::isSpace(c);
3166 Paragraph::getParLanguage(BufferParams const & bparams) const
3169 return getFirstFontSettings(bparams).language();
3170 // FIXME: we should check the prev par as well (Lgb)
3171 return bparams.language;
3175 bool Paragraph::isRTL(BufferParams const & bparams) const
3177 return getParLanguage(bparams)->rightToLeft()
3178 && !inInset().getLayout().forceLTR();
3182 void Paragraph::changeLanguage(BufferParams const & bparams,
3183 Language const * from, Language const * to)
3185 // change language including dummy font change at the end
3186 for (pos_type i = 0; i <= size(); ++i) {
3187 Font font = getFontSettings(bparams, i);
3188 if (font.language() == from) {
3189 font.setLanguage(to);
3191 d->requestSpellCheck(i);
3197 bool Paragraph::isMultiLingual(BufferParams const & bparams) const
3199 Language const * doc_language = bparams.language;
3200 FontList::const_iterator cit = d->fontlist_.begin();
3201 FontList::const_iterator end = d->fontlist_.end();
3203 for (; cit != end; ++cit)
3204 if (cit->font().language() != ignore_language &&
3205 cit->font().language() != latex_language &&
3206 cit->font().language() != doc_language)
3212 void Paragraph::getLanguages(std::set<Language const *> & languages) const
3214 FontList::const_iterator cit = d->fontlist_.begin();
3215 FontList::const_iterator end = d->fontlist_.end();
3217 for (; cit != end; ++cit) {
3218 Language const * lang = cit->font().language();
3219 if (lang != ignore_language &&
3220 lang != latex_language)
3221 languages.insert(lang);
3226 docstring Paragraph::asString(int options) const
3228 return asString(0, size(), options);
3232 docstring Paragraph::asString(pos_type beg, pos_type end, int options, const OutputParams *runparams) const
3234 odocstringstream os;
3237 && options & AS_STR_LABEL
3238 && !d->params_.labelString().empty())
3239 os << d->params_.labelString() << ' ';
3241 for (pos_type i = beg; i < end; ++i) {
3242 if ((options & AS_STR_SKIPDELETE) && isDeleted(i))
3244 char_type const c = d->text_[i];
3245 if (isPrintable(c) || c == '\t'
3246 || (c == '\n' && (options & AS_STR_NEWLINES)))
3248 else if (c == META_INSET && (options & AS_STR_INSETS)) {
3249 if (c == META_INSET && (options & AS_STR_PLAINTEXT)) {
3250 LASSERT(runparams != 0, return docstring());
3251 getInset(i)->plaintext(os, *runparams);
3253 getInset(i)->toString(os);
3262 void Paragraph::forOutliner(docstring & os, size_t maxlen) const
3264 if (!d->params_.labelString().empty())
3265 os += d->params_.labelString() + ' ';
3266 for (pos_type i = 0; i < size() && os.length() < maxlen; ++i) {
3269 char_type const c = d->text_[i];
3272 else if (c == '\t' || c == '\n')
3274 else if (c == META_INSET)
3275 getInset(i)->forOutliner(os, maxlen);
3280 void Paragraph::setInsetOwner(Inset const * inset)
3282 d->inset_owner_ = inset;
3286 int Paragraph::id() const
3292 void Paragraph::setId(int id)
3298 Layout const & Paragraph::layout() const
3304 void Paragraph::setLayout(Layout const & layout)
3306 d->layout_ = &layout;
3310 void Paragraph::setDefaultLayout(DocumentClass const & tc)
3312 setLayout(tc.defaultLayout());
3316 void Paragraph::setPlainLayout(DocumentClass const & tc)
3318 setLayout(tc.plainLayout());
3322 void Paragraph::setPlainOrDefaultLayout(DocumentClass const & tclass)
3324 if (usePlainLayout())
3325 setPlainLayout(tclass);
3327 setDefaultLayout(tclass);
3331 Inset const & Paragraph::inInset() const
3333 LBUFERR(d->inset_owner_);
3334 return *d->inset_owner_;
3338 ParagraphParameters & Paragraph::params()
3344 ParagraphParameters const & Paragraph::params() const
3350 bool Paragraph::isFreeSpacing() const
3352 if (d->layout_->free_spacing)
3354 return d->inset_owner_ && d->inset_owner_->isFreeSpacing();
3358 bool Paragraph::allowEmpty() const
3360 if (d->layout_->keepempty)
3362 return d->inset_owner_ && d->inset_owner_->allowEmpty();
3366 bool Paragraph::brokenBiblio() const
3368 // there is a problem if there is no bibitem at position 0 or
3369 // if there is another bibitem in the paragraph.
3370 return d->layout_->labeltype == LABEL_BIBLIO
3371 && (d->insetlist_.find(BIBITEM_CODE) != 0
3372 || d->insetlist_.find(BIBITEM_CODE, 1) > 0);
3376 int Paragraph::fixBiblio(Buffer const & buffer)
3378 // FIXME: What about the case where paragraph is not BIBLIO
3379 // but there is an InsetBibitem?
3380 // FIXME: when there was already an inset at 0, the return value is 1,
3381 // which does not tell whether another inset has been remove; the
3382 // cursor cannot be correctly updated.
3384 if (d->layout_->labeltype != LABEL_BIBLIO)
3387 bool const track_changes = buffer.params().track_changes;
3388 int bibitem_pos = d->insetlist_.find(BIBITEM_CODE);
3389 bool const hasbibitem0 = bibitem_pos == 0;
3392 bibitem_pos = d->insetlist_.find(BIBITEM_CODE, 1);
3393 // There was an InsetBibitem at pos 0, and no other one => OK
3394 if (bibitem_pos == -1)
3396 // there is a bibitem at the 0 position, but since
3397 // there is a second one, we copy the second on the
3398 // first. We're assuming there are at most two of
3399 // these, which there should be.
3400 // FIXME: why does it make sense to do that rather
3401 // than keep the first? (JMarc)
3402 Inset * inset = releaseInset(bibitem_pos);
3403 d->insetlist_.begin()->inset = inset;
3404 return -bibitem_pos;
3407 // We need to create an inset at the beginning
3409 if (bibitem_pos > 0) {
3410 // there was one somewhere in the paragraph, let's move it
3411 inset = d->insetlist_.release(bibitem_pos);
3412 eraseChar(bibitem_pos, track_changes);
3415 inset = new InsetBibitem(const_cast<Buffer *>(&buffer),
3416 InsetCommandParams(BIBITEM_CODE));
3418 Font font(inherit_font, buffer.params().language);
3419 insertInset(0, inset, font, Change(track_changes ? Change::INSERTED
3420 : Change::UNCHANGED));
3426 void Paragraph::checkAuthors(AuthorList const & authorList)
3428 d->changes_.checkAuthors(authorList);
3432 bool Paragraph::isChanged(pos_type pos) const
3434 return lookupChange(pos).changed();
3438 bool Paragraph::isInserted(pos_type pos) const
3440 return lookupChange(pos).inserted();
3444 bool Paragraph::isDeleted(pos_type pos) const
3446 return lookupChange(pos).deleted();
3450 InsetList const & Paragraph::insetList() const
3452 return d->insetlist_;
3456 void Paragraph::setBuffer(Buffer & b)
3458 d->insetlist_.setBuffer(b);
3462 Inset * Paragraph::releaseInset(pos_type pos)
3464 Inset * inset = d->insetlist_.release(pos);
3465 /// does not honour change tracking!
3466 eraseChar(pos, false);
3471 Inset * Paragraph::getInset(pos_type pos)
3473 return (pos < pos_type(d->text_.size()) && d->text_[pos] == META_INSET)
3474 ? d->insetlist_.get(pos) : 0;
3478 Inset const * Paragraph::getInset(pos_type pos) const
3480 return (pos < pos_type(d->text_.size()) && d->text_[pos] == META_INSET)
3481 ? d->insetlist_.get(pos) : 0;
3485 void Paragraph::changeCase(BufferParams const & bparams, pos_type pos,
3486 pos_type & right, TextCase action)
3488 // process sequences of modified characters; in change
3489 // tracking mode, this approach results in much better
3490 // usability than changing case on a char-by-char basis
3491 // We also need to track the current font, since font
3492 // changes within sequences can occur.
3493 vector<pair<char_type, Font> > changes;
3495 bool const trackChanges = bparams.track_changes;
3497 bool capitalize = true;
3499 for (; pos < right; ++pos) {
3500 char_type oldChar = d->text_[pos];
3501 char_type newChar = oldChar;
3503 // ignore insets and don't play with deleted text!
3504 if (oldChar != META_INSET && !isDeleted(pos)) {
3506 case text_lowercase:
3507 newChar = lowercase(oldChar);
3509 case text_capitalization:
3511 newChar = uppercase(oldChar);
3515 case text_uppercase:
3516 newChar = uppercase(oldChar);
3521 if (isWordSeparator(pos) || isDeleted(pos)) {
3522 // permit capitalization again
3526 if (oldChar != newChar) {
3527 changes.push_back(make_pair(newChar, getFontSettings(bparams, pos)));
3528 if (pos != right - 1)
3530 // step behind the changing area
3534 int erasePos = pos - changes.size();
3535 for (size_t i = 0; i < changes.size(); i++) {
3536 insertChar(pos, changes[i].first,
3539 if (!eraseChar(erasePos, trackChanges)) {
3542 ++right; // expand selection
3550 int Paragraph::find(docstring const & str, bool cs, bool mw,
3551 pos_type start_pos, bool del) const
3553 pos_type pos = start_pos;
3554 int const strsize = str.length();
3556 pos_type const parsize = d->text_.size();
3557 for (i = 0; i < strsize && pos < parsize; ++i, ++pos) {
3558 // Ignore "invisible" letters such as ligature breaks
3559 // and hyphenation chars while searching
3560 while (pos < parsize - 1 && isInset(pos)) {
3561 odocstringstream os;
3562 getInset(pos)->toString(os);
3563 if (!getInset(pos)->isLetter() || !os.str().empty())
3567 if (cs && str[i] != d->text_[pos])
3569 if (!cs && uppercase(str[i]) != uppercase(d->text_[pos]))
3571 if (!del && isDeleted(pos))
3578 // if necessary, check whether string matches word
3580 if (start_pos > 0 && !isWordSeparator(start_pos - 1))
3583 && !isWordSeparator(pos))
3587 return pos - start_pos;
3591 char_type Paragraph::getChar(pos_type pos) const
3593 return d->text_[pos];
3597 pos_type Paragraph::size() const
3599 return d->text_.size();
3603 bool Paragraph::empty() const
3605 return d->text_.empty();
3609 bool Paragraph::isInset(pos_type pos) const
3611 return d->text_[pos] == META_INSET;
3615 bool Paragraph::isSeparator(pos_type pos) const
3617 //FIXME: Are we sure this can be the only separator?
3618 return d->text_[pos] == ' ';
3622 void Paragraph::deregisterWords()
3624 Private::LangWordsMap::const_iterator itl = d->words_.begin();
3625 Private::LangWordsMap::const_iterator ite = d->words_.end();
3626 for (; itl != ite; ++itl) {
3627 WordList * wl = theWordList(itl->first);
3628 Private::Words::const_iterator it = (itl->second).begin();
3629 Private::Words::const_iterator et = (itl->second).end();
3630 for (; it != et; ++it)
3637 void Paragraph::locateWord(pos_type & from, pos_type & to,
3638 word_location const loc) const
3641 case WHOLE_WORD_STRICT:
3642 if (from == 0 || from == size()
3643 || isWordSeparator(from)
3644 || isWordSeparator(from - 1)) {
3648 // no break here, we go to the next
3651 // If we are already at the beginning of a word, do nothing
3652 if (!from || isWordSeparator(from - 1))
3654 // no break here, we go to the next
3657 // always move the cursor to the beginning of previous word
3658 while (from && !isWordSeparator(from - 1))
3662 LYXERR0("Paragraph::locateWord: NEXT_WORD not implemented yet");
3665 // no need to move the 'from' cursor
3669 while (to < size() && !isWordSeparator(to))
3674 void Paragraph::collectWords()
3676 for (pos_type pos = 0; pos < size(); ++pos) {
3677 if (isWordSeparator(pos))
3679 pos_type from = pos;
3680 locateWord(from, pos, WHOLE_WORD);
3681 // Work around MSVC warning: The statement
3682 // if (pos < from + lyxrc.completion_minlength)
3683 // triggers a signed vs. unsigned warning.
3684 // I don't know why this happens, it could be a MSVC bug, or
3685 // related to LLP64 (windows) vs. LP64 (unix) programming
3686 // model, or the C++ standard might be ambigous in the section
3687 // defining the "usual arithmetic conversions". However, using
3688 // a temporary variable is safe and works on all compilers.
3689 pos_type const endpos = from + lyxrc.completion_minlength;
3692 FontList::const_iterator cit = d->fontlist_.fontIterator(from);
3693 if (cit == d->fontlist_.end())
3695 Language const * lang = cit->font().language();
3696 docstring const word = asString(from, pos, AS_STR_NONE);
3697 d->words_[lang->lang()].insert(word);
3702 void Paragraph::registerWords()
3704 Private::LangWordsMap::const_iterator itl = d->words_.begin();
3705 Private::LangWordsMap::const_iterator ite = d->words_.end();
3706 for (; itl != ite; ++itl) {
3707 WordList * wl = theWordList(itl->first);
3708 Private::Words::const_iterator it = (itl->second).begin();
3709 Private::Words::const_iterator et = (itl->second).end();
3710 for (; it != et; ++it)
3716 void Paragraph::updateWords()
3724 void Paragraph::Private::appendSkipPosition(SkipPositions & skips, pos_type const pos) const
3726 SkipPositionsIterator begin = skips.begin();
3727 SkipPositions::iterator end = skips.end();
3728 if (pos > 0 && begin < end) {
3730 if (end->last == pos - 1) {
3735 skips.insert(end, FontSpan(pos, pos));
3739 Language * Paragraph::Private::locateSpellRange(
3740 pos_type & from, pos_type & to,
3741 SkipPositions & skips) const
3743 // skip leading white space
3744 while (from < to && owner_->isWordSeparator(from))
3746 // don't check empty range
3749 // get current language
3750 Language * lang = getSpellLanguage(from);
3751 pos_type last = from;
3752 bool samelang = true;
3753 bool sameinset = true;
3754 while (last < to && samelang && sameinset) {
3755 // hop to end of word
3756 while (last < to && !owner_->isWordSeparator(last)) {
3757 if (owner_->getInset(last)) {
3758 appendSkipPosition(skips, last);
3759 } else if (owner_->isDeleted(last)) {
3760 appendSkipPosition(skips, last);
3764 // hop to next word while checking for insets
3765 while (sameinset && last < to && owner_->isWordSeparator(last)) {
3766 if (Inset const * inset = owner_->getInset(last))
3767 sameinset = inset->isChar() && inset->isLetter();
3768 if (sameinset && owner_->isDeleted(last)) {
3769 appendSkipPosition(skips, last);
3774 if (sameinset && last < to) {
3775 // now check for language change
3776 samelang = lang == getSpellLanguage(last);
3779 // if language change detected backstep is needed
3787 Language * Paragraph::Private::getSpellLanguage(pos_type const from) const
3790 const_cast<Language *>(owner_->getFontSettings(
3791 inset_owner_->buffer().params(), from).language());
3792 if (lang == inset_owner_->buffer().params().language
3793 && !lyxrc.spellchecker_alt_lang.empty()) {
3795 string const lang_variety =
3796 split(lyxrc.spellchecker_alt_lang, lang_code, '-');
3797 lang->setCode(lang_code);
3798 lang->setVariety(lang_variety);
3804 void Paragraph::requestSpellCheck(pos_type pos)
3806 d->requestSpellCheck(pos);
3810 bool Paragraph::needsSpellCheck() const
3812 SpellChecker::ChangeNumber speller_change_number = 0;
3813 if (theSpellChecker())
3814 speller_change_number = theSpellChecker()->changeNumber();
3815 if (speller_change_number > d->speller_state_.currentChangeNumber()) {
3816 d->speller_state_.needsCompleteRefresh(speller_change_number);
3818 return d->needsSpellCheck();
3822 bool Paragraph::Private::ignoreWord(docstring const & word) const
3824 // Ignore words with digits
3825 // FIXME: make this customizable
3826 // (note that some checkers ignore words with digits by default)
3827 docstring::const_iterator cit = word.begin();
3828 docstring::const_iterator const end = word.end();
3829 for (; cit != end; ++cit) {
3830 if (isNumber((*cit)))
3837 SpellChecker::Result Paragraph::spellCheck(pos_type & from, pos_type & to,
3838 WordLangTuple & wl, docstring_list & suggestions,
3839 bool do_suggestion, bool check_learned) const
3841 SpellChecker::Result result = SpellChecker::WORD_OK;
3842 SpellChecker * speller = theSpellChecker();
3846 if (!d->layout_->spellcheck || !inInset().allowSpellCheck())
3849 locateWord(from, to, WHOLE_WORD);
3850 if (from == to || from >= size())
3853 docstring word = asString(from, to, AS_STR_INSETS | AS_STR_SKIPDELETE);
3854 Language * lang = d->getSpellLanguage(from);
3856 wl = WordLangTuple(word, lang);
3861 if (needsSpellCheck() || check_learned) {
3863 if (!d->ignoreWord(word)) {
3864 bool const trailing_dot = to < size() && d->text_[to] == '.';
3865 result = speller->check(wl);
3866 if (SpellChecker::misspelled(result) && trailing_dot) {
3867 wl = WordLangTuple(word.append(from_ascii(".")), lang);
3868 result = speller->check(wl);
3869 if (!SpellChecker::misspelled(result)) {
3870 LYXERR(Debug::GUI, "misspelled word is correct with dot: \"" <<
3872 from << ".." << to << "]");
3874 // spell check with dot appended failed too
3875 // restore original word/lang value
3876 word = asString(from, to, AS_STR_INSETS | AS_STR_SKIPDELETE);
3877 wl = WordLangTuple(word, lang);
3881 if (!SpellChecker::misspelled(result)) {
3882 // area up to the begin of the next word is not misspelled
3883 while (end < size() && isWordSeparator(end))
3886 d->setMisspelled(from, end, result);
3888 result = d->speller_state_.getState(from);
3892 suggestions.clear();
3894 if (SpellChecker::misspelled(result)) {
3895 LYXERR(Debug::GUI, "misspelled word: \"" <<
3897 from << ".." << to << "]");
3899 speller->suggest(wl, suggestions);
3905 void Paragraph::Private::markMisspelledWords(
3906 pos_type const & first, pos_type const & last,
3907 SpellChecker::Result result,
3908 docstring const & word,
3909 SkipPositions const & skips)
3911 if (!SpellChecker::misspelled(result)) {
3912 setMisspelled(first, last, SpellChecker::WORD_OK);
3916 SpellChecker * speller = theSpellChecker();
3917 // locate and enumerate the error positions
3918 int nerrors = speller->numMisspelledWords();
3920 SkipPositionsIterator it = skips.begin();
3921 SkipPositionsIterator et = skips.end();
3922 for (int index = 0; index < nerrors; ++index) {
3925 speller->misspelledWord(index, wstart, wlen);
3926 /// should not happen if speller supports range checks
3927 if (!wlen) continue;
3928 docstring const misspelled = word.substr(wstart, wlen);
3929 wstart += first + numskipped;
3930 if (snext < wstart) {
3931 /// mark the range of correct spelling
3932 numskipped += countSkips(it, et, wstart);
3933 setMisspelled(snext,
3934 wstart - 1, SpellChecker::WORD_OK);
3936 snext = wstart + wlen;
3937 numskipped += countSkips(it, et, snext);
3938 /// mark the range of misspelling
3939 setMisspelled(wstart, snext, result);
3940 LYXERR(Debug::GUI, "misspelled word: \"" <<
3941 misspelled << "\" [" <<
3942 wstart << ".." << (snext-1) << "]");
3945 if (snext <= last) {
3946 /// mark the range of correct spelling at end
3947 setMisspelled(snext, last, SpellChecker::WORD_OK);
3952 void Paragraph::spellCheck() const
3954 SpellChecker * speller = theSpellChecker();
3955 if (!speller || empty() ||!needsSpellCheck())
3959 d->rangeOfSpellCheck(start, endpos);
3960 if (speller->canCheckParagraph()) {
3961 // loop until we leave the range
3962 for (pos_type first = start; first < endpos; ) {
3963 pos_type last = endpos;
3964 Private::SkipPositions skips;
3965 Language * lang = d->locateSpellRange(first, last, skips);
3966 if (first >= endpos)
3968 // start the spell checker on the unit of meaning
3969 docstring word = asString(first, last, AS_STR_INSETS + AS_STR_SKIPDELETE);
3970 WordLangTuple wl = WordLangTuple(word, lang);
3971 SpellChecker::Result result = word.size() ?
3972 speller->check(wl) : SpellChecker::WORD_OK;
3973 d->markMisspelledWords(first, last, result, word, skips);
3977 static docstring_list suggestions;
3978 pos_type to = endpos;
3979 while (start < endpos) {
3981 spellCheck(start, to, wl, suggestions, false);
3985 d->readySpellCheck();
3989 bool Paragraph::isMisspelled(pos_type pos, bool check_boundary) const
3991 bool result = SpellChecker::misspelled(d->speller_state_.getState(pos));
3992 if (result || pos <= 0 || pos > size())
3994 if (check_boundary && (pos == size() || isWordSeparator(pos)))
3995 result = SpellChecker::misspelled(d->speller_state_.getState(pos - 1));
4000 string Paragraph::magicLabel() const
4003 ss << "magicparlabel-" << id();