3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Asger Alstrup
7 * \author Lars Gullik Bjønnes
8 * \author Richard Heck (XHTML output)
9 * \author Jean-Marc Lasgouttes
10 * \author Angus Leeming
12 * \author André Pönitz
14 * \author Jürgen Vigna
16 * Full author contact details are available in file CREDITS.
21 #include "Paragraph.h"
23 #include "LayoutFile.h"
25 #include "BufferParams.h"
28 #include "BufferEncodings.h"
29 #include "InsetList.h"
31 #include "LaTeXFeatures.h"
37 #include "OutputParams.h"
38 #include "output_latex.h"
39 #include "output_xhtml.h"
40 #include "ParagraphParameters.h"
41 #include "SpellChecker.h"
43 #include "TextClass.h"
46 #include "WordLangTuple.h"
49 #include "frontends/alert.h"
51 #include "insets/InsetBibitem.h"
52 #include "insets/InsetLabel.h"
53 #include "insets/InsetSpecialChar.h"
55 #include "support/debug.h"
56 #include "support/docstring_list.h"
57 #include "support/ExceptionMessage.h"
58 #include "support/gettext.h"
59 #include "support/lassert.h"
60 #include "support/lstrings.h"
61 #include "support/textutils.h"
67 using namespace lyx::support;
73 /// Inset identifier (above 0x10ffff, for ucs-4)
74 char_type const META_INSET = 0x200001;
79 /////////////////////////////////////////////////////////////////////
83 /////////////////////////////////////////////////////////////////////
85 class SpellResultRange {
87 SpellResultRange(FontSpan range, SpellChecker::Result result)
88 : range_(range), result_(result)
91 FontSpan const & range() const { return range_; }
93 void range(FontSpan const & r) { range_ = r; }
95 SpellChecker::Result result() const { return result_; }
97 void result(SpellChecker::Result r) { result_ = r; }
99 bool contains(pos_type pos) const { return range_.contains(pos); }
101 bool covered(FontSpan const & r) const
103 // 1. first of new range inside current range or
104 // 2. last of new range inside current range or
105 // 3. first of current range inside new range or
106 // 4. last of current range inside new range
107 //FIXME: is this the same as !range_.intersect(r).empty() ?
108 return range_.contains(r.first) || range_.contains(r.last) ||
109 r.contains(range_.first) || r.contains(range_.last);
112 void shift(pos_type pos, int offset)
114 if (range_.first > pos) {
115 range_.first += offset;
116 range_.last += offset;
117 } else if (range_.last >= pos) {
118 range_.last += offset;
123 SpellChecker::Result result_ ;
127 /////////////////////////////////////////////////////////////////////
131 /////////////////////////////////////////////////////////////////////
133 class SpellCheckerState {
137 needs_refresh_ = true;
138 current_change_number_ = 0;
141 void setRange(FontSpan const & fp, SpellChecker::Result state)
144 RangesIterator et = ranges_.end();
145 RangesIterator it = ranges_.begin();
146 for (; it != et; ++it) {
147 if (!it->covered(fp))
148 result.push_back(SpellResultRange(it->range(), it->result()));
149 else if (state == SpellChecker::WORD_OK) {
150 // trim or split the current misspelled range
151 // store misspelled ranges only
152 FontSpan range = it->range();
153 if (fp.first > range.first) {
154 // misspelled area in front of WORD_OK
155 range.last = fp.first - 1;
156 result.push_back(SpellResultRange(range, it->result()));
159 if (fp.last < range.last) {
160 // misspelled area after WORD_OK range
161 range.first = fp.last + 1;
162 result.push_back(SpellResultRange(range, it->result()));
167 if (state != SpellChecker::WORD_OK)
168 ranges_.push_back(SpellResultRange(fp, state));
171 void increasePosAfterPos(pos_type pos)
173 correctRangesAfterPos(pos, 1);
177 void decreasePosAfterPos(pos_type pos)
179 correctRangesAfterPos(pos, -1);
183 void refreshLast(pos_type pos)
185 if (pos < refresh_.last)
189 SpellChecker::Result getState(pos_type pos) const
191 SpellChecker::Result result = SpellChecker::WORD_OK;
192 RangesIterator et = ranges_.end();
193 RangesIterator it = ranges_.begin();
194 for (; it != et; ++it) {
195 if(it->contains(pos)) {
202 FontSpan const & getRange(pos_type pos) const
204 /// empty span to indicate mismatch
205 static FontSpan empty_;
206 RangesIterator et = ranges_.end();
207 RangesIterator it = ranges_.begin();
208 for (; it != et; ++it) {
209 if(it->contains(pos)) {
216 bool needsRefresh() const
218 return needs_refresh_;
221 SpellChecker::ChangeNumber currentChangeNumber() const
223 return current_change_number_;
226 void refreshRange(pos_type & first, pos_type & last) const
228 first = refresh_.first;
229 last = refresh_.last;
232 void needsRefresh(pos_type pos)
234 if (needs_refresh_ && pos != -1) {
235 if (pos < refresh_.first)
236 refresh_.first = pos;
237 if (pos > refresh_.last)
239 } else if (pos != -1) {
240 // init request check for neighbour positions too
241 refresh_.first = pos > 0 ? pos - 1 : 0;
242 // no need for special end of paragraph check
243 refresh_.last = pos + 1;
245 needs_refresh_ = pos != -1;
248 void needsCompleteRefresh(SpellChecker::ChangeNumber change_number)
250 needs_refresh_ = true;
253 current_change_number_ = change_number;
256 typedef vector<SpellResultRange> Ranges;
257 typedef Ranges::const_iterator RangesIterator;
259 /// the area of the paragraph with pending spell check
262 /// spell state cache version number
263 SpellChecker::ChangeNumber current_change_number_;
266 void correctRangesAfterPos(pos_type pos, int offset)
268 RangesIterator et = ranges_.end();
269 Ranges::iterator it = ranges_.begin();
270 for (; it != et; ++it) {
271 it->shift(pos, offset);
277 /////////////////////////////////////////////////////////////////////
279 // Paragraph::Private
281 /////////////////////////////////////////////////////////////////////
283 class Paragraph::Private
285 // Enforce our own "copy" constructor by declaring the standard one and
286 // the assignment operator private without implementing them.
287 Private(Private const &);
288 Private & operator=(Private const &);
291 Private(Paragraph * owner, Layout const & layout);
292 /// "Copy constructor"
293 Private(Private const &, Paragraph * owner);
294 /// Copy constructor from \p beg to \p end
295 Private(Private const &, Paragraph * owner, pos_type beg, pos_type end);
298 void insertChar(pos_type pos, char_type c, Change const & change);
300 /// Output the surrogate pair formed by \p c and \p next to \p os.
301 /// \return the number of characters written.
302 int latexSurrogatePair(otexstream & os, char_type c, char_type next,
303 OutputParams const &);
305 /// Output a space in appropriate formatting (or a surrogate pair
306 /// if the next character is a combining character).
307 /// \return whether a surrogate pair was output.
308 bool simpleTeXBlanks(OutputParams const &,
311 unsigned int & column,
313 Layout const & style);
315 /// Output consecutive unicode chars, belonging to the same script as
316 /// specified by the latex macro \p ltx, to \p os starting from \p i.
317 /// \return the number of characters written.
318 int writeScriptChars(otexstream & os, docstring const & ltx,
319 Change const &, Encoding const &, pos_type & i);
321 /// This could go to ParagraphParameters if we want to.
322 int startTeXParParams(BufferParams const &, otexstream &,
323 OutputParams const &) const;
325 /// This could go to ParagraphParameters if we want to.
326 bool endTeXParParams(BufferParams const &, otexstream &,
327 OutputParams const &) const;
330 void latexInset(BufferParams const &,
335 Font const & outerfont,
337 Change & running_change,
338 Layout const & style,
340 unsigned int & column);
343 void latexSpecialChar(
345 BufferParams const & bparams,
346 OutputParams const & runparams,
347 Font const & running_font,
348 Change const & running_change,
349 Layout const & style,
352 unsigned int & column);
359 unsigned int & column);
365 unsigned int & column);
368 void validate(LaTeXFeatures & features) const;
370 /// Checks if the paragraph contains only text and no inset or font change.
371 bool onlyText(Buffer const & buf, Font const & outerfont,
372 pos_type initial) const;
374 /// a vector of speller skip positions
375 typedef vector<FontSpan> SkipPositions;
376 typedef SkipPositions::const_iterator SkipPositionsIterator;
378 void appendSkipPosition(SkipPositions & skips, pos_type const pos) const;
380 Language * getSpellLanguage(pos_type const from) const;
382 Language * locateSpellRange(pos_type & from, pos_type & to,
383 SkipPositions & skips) const;
385 bool hasSpellerChange() const
387 SpellChecker::ChangeNumber speller_change_number = 0;
388 if (theSpellChecker())
389 speller_change_number = theSpellChecker()->changeNumber();
390 return speller_change_number > speller_state_.currentChangeNumber();
393 bool ignoreWord(docstring const & word) const ;
395 void setMisspelled(pos_type from, pos_type to, SpellChecker::Result state)
397 pos_type textsize = owner_->size();
398 // check for sane arguments
399 if (to <= from || from >= textsize)
401 FontSpan fp = FontSpan(from, to - 1);
402 speller_state_.setRange(fp, state);
405 void requestSpellCheck(pos_type pos)
408 speller_state_.needsCompleteRefresh(speller_state_.currentChangeNumber());
410 speller_state_.needsRefresh(pos);
413 void readySpellCheck()
415 speller_state_.needsRefresh(-1);
418 bool needsSpellCheck() const
420 return speller_state_.needsRefresh();
423 void rangeOfSpellCheck(pos_type & first, pos_type & last) const
425 speller_state_.refreshRange(first, last);
427 last = owner_->size();
430 pos_type endpos = last;
431 owner_->locateWord(first, endpos, WHOLE_WORD);
434 owner_->locateWord(last, endpos, WHOLE_WORD);
439 int countSkips(SkipPositionsIterator & it, SkipPositionsIterator const et,
443 while (it != et && it->first < start) {
444 int skip = it->last - it->first + 1;
452 void markMisspelledWords(pos_type const & first, pos_type const & last,
453 SpellChecker::Result result,
454 docstring const & word,
455 SkipPositions const & skips);
457 InsetCode ownerCode() const
459 return inset_owner_ ? inset_owner_->lyxCode() : NO_CODE;
462 /// Which Paragraph owns us?
466 Inset const * inset_owner_;
475 ParagraphParameters params_;
477 /// for recording and looking up changes
481 InsetList insetlist_;
484 pos_type begin_of_body_;
486 typedef docstring TextContainer;
490 typedef set<docstring> Words;
491 typedef map<string, Words> LangWordsMap;
495 Layout const * layout_;
497 SpellCheckerState speller_state_;
501 Paragraph::Private::Private(Paragraph * owner, Layout const & layout)
502 : owner_(owner), inset_owner_(0), id_(-1), begin_of_body_(0), layout_(&layout)
508 // Initialization of the counter for the paragraph id's,
510 // FIXME: There should be a more intelligent way to generate and use the
511 // paragraph ids per buffer instead a global static counter for all InsetText
512 // in the running program.
513 static int paragraph_id = -1;
515 Paragraph::Private::Private(Private const & p, Paragraph * owner)
516 : owner_(owner), inset_owner_(p.inset_owner_), fontlist_(p.fontlist_),
517 params_(p.params_), changes_(p.changes_), insetlist_(p.insetlist_),
518 begin_of_body_(p.begin_of_body_), text_(p.text_), words_(p.words_),
521 id_ = ++paragraph_id;
522 requestSpellCheck(p.text_.size());
526 Paragraph::Private::Private(Private const & p, Paragraph * owner,
527 pos_type beg, pos_type end)
528 : owner_(owner), inset_owner_(p.inset_owner_),
529 params_(p.params_), changes_(p.changes_),
530 insetlist_(p.insetlist_, beg, end),
531 begin_of_body_(p.begin_of_body_), words_(p.words_),
534 id_ = ++paragraph_id;
535 if (beg >= pos_type(p.text_.size()))
537 text_ = p.text_.substr(beg, end - beg);
539 FontList::const_iterator fcit = fontlist_.begin();
540 FontList::const_iterator fend = fontlist_.end();
541 for (; fcit != fend; ++fcit) {
542 if (fcit->pos() < beg)
544 if (fcit->pos() >= end) {
545 // Add last entry in the fontlist_.
546 fontlist_.set(text_.size() - 1, fcit->font());
549 // Add a new entry in the fontlist_.
550 fontlist_.set(fcit->pos() - beg, fcit->font());
552 requestSpellCheck(p.text_.size());
556 void Paragraph::addChangesToToc(DocIterator const & cdit,
557 Buffer const & buf, bool output_active) const
559 d->changes_.addToToc(cdit, buf, output_active);
563 bool Paragraph::isDeleted(pos_type start, pos_type end) const
565 LASSERT(start >= 0 && start <= size(), return false);
566 LASSERT(end > start && end <= size() + 1, return false);
568 return d->changes_.isDeleted(start, end);
572 bool Paragraph::isChanged(pos_type start, pos_type end) const
574 LASSERT(start >= 0 && start <= size(), return false);
575 LASSERT(end > start && end <= size() + 1, return false);
577 return d->changes_.isChanged(start, end);
581 bool Paragraph::isMergedOnEndOfParDeletion(bool trackChanges) const
583 // keep the logic here in sync with the logic of eraseChars()
587 Change const & change = d->changes_.lookup(size());
588 return change.inserted() && change.currentAuthor();
592 void Paragraph::setChange(Change const & change)
594 // beware of the imaginary end-of-par character!
595 d->changes_.set(change, 0, size() + 1);
598 * Propagate the change recursively - but not in case of DELETED!
600 * Imagine that your co-author makes changes in an existing inset. He
601 * sends your document to you and you come to the conclusion that the
602 * inset should go completely. If you erase it, LyX must not delete all
603 * text within the inset. Otherwise, the change tracked insertions of
604 * your co-author get lost and there is no way to restore them later.
606 * Conclusion: An inset's content should remain untouched if you delete it
609 if (!change.deleted()) {
610 for (pos_type pos = 0; pos < size(); ++pos) {
611 if (Inset * inset = getInset(pos))
612 inset->setChange(change);
618 void Paragraph::setChange(pos_type pos, Change const & change)
620 LASSERT(pos >= 0 && pos <= size(), return);
621 d->changes_.set(change, pos);
623 // see comment in setChange(Change const &) above
624 if (!change.deleted() && pos < size())
625 if (Inset * inset = getInset(pos))
626 inset->setChange(change);
630 Change const & Paragraph::lookupChange(pos_type pos) const
632 LBUFERR(pos >= 0 && pos <= size());
633 return d->changes_.lookup(pos);
637 void Paragraph::acceptChanges(pos_type start, pos_type end)
639 LASSERT(start >= 0 && start <= size(), return);
640 LASSERT(end > start && end <= size() + 1, return);
642 for (pos_type pos = start; pos < end; ++pos) {
643 switch (lookupChange(pos).type) {
644 case Change::UNCHANGED:
645 // accept changes in nested inset
646 if (Inset * inset = getInset(pos))
647 inset->acceptChanges();
650 case Change::INSERTED:
651 d->changes_.set(Change(Change::UNCHANGED), pos);
652 // also accept changes in nested inset
653 if (Inset * inset = getInset(pos))
654 inset->acceptChanges();
657 case Change::DELETED:
658 // Suppress access to non-existent
659 // "end-of-paragraph char"
661 eraseChar(pos, false);
672 void Paragraph::rejectChanges(pos_type start, pos_type end)
674 LASSERT(start >= 0 && start <= size(), return);
675 LASSERT(end > start && end <= size() + 1, return);
677 for (pos_type pos = start; pos < end; ++pos) {
678 switch (lookupChange(pos).type) {
679 case Change::UNCHANGED:
680 // reject changes in nested inset
681 if (Inset * inset = getInset(pos))
682 inset->rejectChanges();
685 case Change::INSERTED:
686 // Suppress access to non-existent
687 // "end-of-paragraph char"
689 eraseChar(pos, false);
695 case Change::DELETED:
696 d->changes_.set(Change(Change::UNCHANGED), pos);
698 // Do NOT reject changes within a deleted inset!
699 // There may be insertions of a co-author inside of it!
707 void Paragraph::Private::insertChar(pos_type pos, char_type c,
708 Change const & change)
710 LASSERT(pos >= 0 && pos <= int(text_.size()), return);
713 changes_.insert(change, pos);
715 // This is actually very common when parsing buffers (and
716 // maybe inserting ascii text)
717 if (pos == pos_type(text_.size())) {
718 // when appending characters, no need to update tables
720 // but we want spell checking
721 requestSpellCheck(pos);
725 text_.insert(text_.begin() + pos, c);
727 // Update the font table.
728 fontlist_.increasePosAfterPos(pos);
731 insetlist_.increasePosAfterPos(pos);
733 // Update list of misspelled positions
734 speller_state_.increasePosAfterPos(pos);
738 bool Paragraph::insertInset(pos_type pos, Inset * inset,
739 Font const & font, Change const & change)
741 LASSERT(inset, return false);
742 LASSERT(pos >= 0 && pos <= size(), return false);
744 // Paragraph::insertInset() can be used in cut/copy/paste operation where
745 // d->inset_owner_ is not set yet.
746 if (d->inset_owner_ && !d->inset_owner_->insetAllowed(inset->lyxCode()))
749 d->insertChar(pos, META_INSET, change);
750 LASSERT(d->text_[pos] == META_INSET, return false);
752 // Add a new entry in the insetlist_.
753 d->insetlist_.insert(inset, pos);
755 // Some insets require run of spell checker
756 requestSpellCheck(pos);
762 bool Paragraph::eraseChar(pos_type pos, bool trackChanges)
764 LASSERT(pos >= 0 && pos <= size(), return false);
766 // keep the logic here in sync with the logic of isMergedOnEndOfParDeletion()
769 Change change = d->changes_.lookup(pos);
771 // set the character to DELETED if
772 // a) it was previously unchanged or
773 // b) it was inserted by a co-author
775 if (!change.changed() ||
776 (change.inserted() && !change.currentAuthor())) {
777 setChange(pos, Change(Change::DELETED));
778 // request run of spell checker
779 requestSpellCheck(pos);
783 if (change.deleted())
787 // Don't physically access the imaginary end-of-paragraph character.
788 // eraseChar() can only mark it as DELETED. A physical deletion of
789 // end-of-par must be handled externally.
795 d->changes_.erase(pos);
797 // if it is an inset, delete the inset entry
798 if (d->text_[pos] == META_INSET)
799 d->insetlist_.erase(pos);
801 d->text_.erase(d->text_.begin() + pos);
803 // Update the fontlist_
804 d->fontlist_.erase(pos);
806 // Update the insetlist_
807 d->insetlist_.decreasePosAfterPos(pos);
809 // Update list of misspelled positions
810 d->speller_state_.decreasePosAfterPos(pos);
811 d->speller_state_.refreshLast(size());
817 int Paragraph::eraseChars(pos_type start, pos_type end, bool trackChanges)
819 LASSERT(start >= 0 && start <= size(), return 0);
820 LASSERT(end >= start && end <= size() + 1, return 0);
823 for (pos_type count = end - start; count; --count) {
824 if (!eraseChar(i, trackChanges))
831 int Paragraph::Private::latexSurrogatePair(otexstream & os, char_type c,
832 char_type next, OutputParams const & runparams)
834 // Writing next here may circumvent a possible font change between
835 // c and next. Since next is only output if it forms a surrogate pair
836 // with c we can ignore this:
837 // A font change inside a surrogate pair does not make sense and is
838 // hopefully impossible to input.
839 // FIXME: change tracking
840 // Is this correct WRT change tracking?
841 Encoding const & encoding = *(runparams.encoding);
842 docstring latex1 = encoding.latexChar(next).first;
843 if (runparams.inIPA) {
844 string const tipashortcut = Encodings::TIPAShortcut(next);
845 if (!tipashortcut.empty()) {
846 latex1 = from_ascii(tipashortcut);
849 docstring const latex2 = encoding.latexChar(c).first;
850 if (docstring(1, next) == latex1) {
851 // the encoding supports the combination
852 os << latex2 << latex1;
853 return latex1.length() + latex2.length();
854 } else if (runparams.local_font &&
855 runparams.local_font->language()->lang() == "polutonikogreek") {
856 // polutonikogreek only works without the brackets
857 os << latex1 << latex2;
858 return latex1.length() + latex2.length();
860 os << latex1 << '{' << latex2 << '}';
861 return latex1.length() + latex2.length() + 2;
865 bool Paragraph::Private::simpleTeXBlanks(OutputParams const & runparams,
868 unsigned int & column,
870 Layout const & style)
872 if (style.pass_thru || runparams.pass_thru)
875 if (i + 1 < int(text_.size())) {
876 char_type next = text_[i + 1];
877 if (Encodings::isCombiningChar(next)) {
878 // This space has an accent, so we must always output it.
879 column += latexSurrogatePair(os, ' ', next, runparams) - 1;
884 if (runparams.linelen > 0
885 && column > runparams.linelen
887 && text_[i - 1] != ' '
888 && (i + 1 < int(text_.size()))
889 // same in FreeSpacing mode
890 && !owner_->isFreeSpacing()
891 // In typewriter mode, we want to avoid
892 // ! . ? : at the end of a line
893 && !(font.fontInfo().family() == TYPEWRITER_FAMILY
894 && (text_[i - 1] == '.'
895 || text_[i - 1] == '?'
896 || text_[i - 1] == ':'
897 || text_[i - 1] == '!'))) {
899 os.texrow().start(owner_->id(), i + 1);
901 } else if (style.free_spacing) {
910 int Paragraph::Private::writeScriptChars(otexstream & os,
911 docstring const & ltx,
912 Change const & runningChange,
913 Encoding const & encoding,
916 // FIXME: modifying i here is not very nice...
918 // We only arrive here when a proper language for character text_[i] has
919 // not been specified (i.e., it could not be translated in the current
920 // latex encoding) or its latex translation has been forced, and it
921 // belongs to a known script.
922 // Parameter ltx contains the latex translation of text_[i] as specified
923 // in the unicodesymbols file and is something like "\textXXX{<spec>}".
924 // The latex macro name "textXXX" specifies the script to which text_[i]
925 // belongs and we use it in order to check whether characters from the
926 // same script immediately follow, such that we can collect them in a
927 // single "\textXXX" macro. So, we have to retain "\textXXX{<spec>"
928 // for the first char but only "<spec>" for all subsequent chars.
929 docstring::size_type const brace1 = ltx.find_first_of(from_ascii("{"));
930 docstring::size_type const brace2 = ltx.find_last_of(from_ascii("}"));
931 string script = to_ascii(ltx.substr(1, brace1 - 1));
934 bool closing_brace = true;
935 if (script == "textgreek" && encoding.latexName() == "iso-8859-7") {
936 // Correct encoding is being used, so we can avoid \textgreek.
939 closing_brace = false;
941 os << ltx.substr(pos, length);
942 int size = text_.size();
943 while (i + 1 < size) {
944 char_type const next = text_[i + 1];
945 // Stop here if next character belongs to another script
946 // or there is a change in change tracking status.
947 if (!Encodings::isKnownScriptChar(next, script) ||
948 runningChange != owner_->lookupChange(i + 1))
952 FontList::const_iterator cit = fontlist_.begin();
953 FontList::const_iterator end = fontlist_.end();
954 for (; cit != end; ++cit) {
955 if (cit->pos() >= i && !found) {
956 prev_font = cit->font();
959 if (cit->pos() >= i + 1)
962 // Stop here if there is a font attribute or encoding change.
963 if (found && cit != end && prev_font != cit->font())
965 docstring const latex = encoding.latexChar(next).first;
966 docstring::size_type const b1 =
967 latex.find_first_of(from_ascii("{"));
968 docstring::size_type const b2 =
969 latex.find_last_of(from_ascii("}"));
970 int const len = b2 - b1 - 1;
971 os << latex.substr(b1 + 1, len);
983 void Paragraph::Private::latexInset(BufferParams const & bparams,
985 OutputParams & runparams,
988 Font const & outerfont,
990 Change & running_change,
991 Layout const & style,
993 unsigned int & column)
995 Inset * inset = owner_->getInset(i);
998 if (style.pass_thru) {
999 odocstringstream ods;
1000 inset->plaintext(ods, runparams);
1005 // FIXME: move this to InsetNewline::latex
1006 if (inset->lyxCode() == NEWLINE_CODE || inset->lyxCode() == SEPARATOR_CODE) {
1007 // newlines are handled differently here than
1008 // the default in simpleTeXSpecialChars().
1009 if (!style.newline_allowed) {
1013 column += running_font.latexWriteEndChanges(
1014 os, bparams, runparams,
1015 basefont, basefont);
1019 if (running_font.fontInfo().family() == TYPEWRITER_FAMILY)
1022 basefont = owner_->getLayoutFont(bparams, outerfont);
1023 running_font = basefont;
1025 if (runparams.moving_arg)
1029 os.texrow().start(owner_->id(), i + 1);
1033 if (owner_->isDeleted(i)) {
1034 if( ++runparams.inDeletedInset == 1)
1035 runparams.changeOfDeletedInset = owner_->lookupChange(i);
1038 if (inset->canTrackChanges()) {
1039 column += Changes::latexMarkChange(os, bparams, running_change,
1040 Change(Change::UNCHANGED), runparams);
1041 running_change = Change(Change::UNCHANGED);
1045 odocstream::pos_type const len = os.os().tellp();
1047 if (inset->forceLTR()
1048 && !runparams.use_polyglossia
1049 && running_font.isRightToLeft()
1050 // ERT is an exception, it should be output with no
1051 // decorations at all
1052 && inset->lyxCode() != ERT_CODE) {
1053 if (running_font.language()->lang() == "farsi")
1060 // FIXME: Bug: we can have an empty font change here!
1061 // if there has just been a font change, we are going to close it
1062 // right now, which means stupid latex code like \textsf{}. AFAIK,
1063 // this does not harm dvi output. A minor bug, thus (JMarc)
1065 // Some insets cannot be inside a font change command.
1066 // However, even such insets *can* be placed in \L or \R
1067 // or their equivalents (for RTL language switches), so we don't
1068 // close the language in those cases.
1069 // ArabTeX, though, cannot handle this special behavior, it seems.
1070 bool arabtex = basefont.language()->lang() == "arabic_arabtex"
1071 || running_font.language()->lang() == "arabic_arabtex";
1072 if (open_font && !inset->inheritFont()) {
1073 bool closeLanguage = arabtex
1074 || basefont.isRightToLeft() == running_font.isRightToLeft();
1075 unsigned int count = running_font.latexWriteEndChanges(os,
1076 bparams, runparams, basefont, basefont, closeLanguage);
1078 // if any font properties were closed, update the running_font,
1079 // making sure, however, to leave the language as it was
1081 // FIXME: probably a better way to keep track of the old
1082 // language, than copying the entire font?
1083 Font const copy_font(running_font);
1084 basefont = owner_->getLayoutFont(bparams, outerfont);
1085 running_font = basefont;
1087 running_font.setLanguage(copy_font.language());
1088 // leave font open if language is still open
1089 open_font = (running_font.language() == basefont.language());
1091 runparams.local_font = &basefont;
1095 int prev_rows = os.texrow().rows();
1098 runparams.lastid = id_;
1099 runparams.lastpos = i;
1100 inset->latex(os, runparams);
1101 } catch (EncodingException & e) {
1102 // add location information and throw again.
1109 if (running_font.language()->lang() == "farsi")
1115 if (os.texrow().rows() > prev_rows) {
1116 os.texrow().start(owner_->id(), i + 1);
1119 column += (unsigned int)(os.os().tellp() - len);
1122 if (owner_->isDeleted(i))
1123 --runparams.inDeletedInset;
1127 void Paragraph::Private::latexSpecialChar(otexstream & os,
1128 BufferParams const & bparams,
1129 OutputParams const & runparams,
1130 Font const & running_font,
1131 Change const & running_change,
1132 Layout const & style,
1135 unsigned int & column)
1137 // With polyglossia, brackets and stuff need not be reversed
1138 // in RTL scripts (see bug #8251)
1139 char_type const c = (runparams.use_polyglossia) ?
1140 owner_->getUChar(bparams, i) : text_[i];
1142 if (style.pass_thru || runparams.pass_thru) {
1144 Encoding const * const enc = runparams.encoding;
1145 if (enc && !enc->encodable(c))
1146 throw EncodingException(c);
1152 // TIPA uses its own T3 encoding
1153 if (runparams.inIPA && latexSpecialT3(c, os, i, column))
1155 // If T1 font encoding is used, use the special
1156 // characters it provides.
1157 // NOTE: some languages reset the font encoding
1159 if (!runparams.inIPA && !running_font.language()->internalFontEncoding()
1160 && lyxrc.fontenc == "T1" && latexSpecialT1(c, os, i, column))
1163 // Otherwise, we use what LaTeX provides us.
1166 os << "\\textbackslash{}";
1170 os << "\\textless{}";
1174 os << "\\textgreater{}";
1178 os << "\\textbar{}";
1183 if (i + 1 < end_pos && text_[i+1] == '-') {
1184 // Prevent "--" becoming an endash and "---" becoming
1186 // Within \ttfamily, "--" is merged to "-" (no endash)
1187 // so we avoid this rather irritating ligature as well
1193 os << "\\char`\\\"{}";
1198 case '%': case '#': case '{':
1206 os << "\\textasciitilde{}";
1211 os << "\\textasciicircum{}";
1218 // avoid being mistaken for optional arguments
1226 // Blanks are printed before font switching.
1227 // Sure? I am not! (try nice-latex)
1228 // I am sure it's correct. LyX might be smarter
1229 // in the future, but for now, nothing wrong is
1237 Encoding const & encoding = *(runparams.encoding);
1238 char_type next = '\0';
1239 if (i + 1 < int(text_.size())) {
1240 next = text_[i + 1];
1241 if (Encodings::isCombiningChar(next)) {
1242 column += latexSurrogatePair(os, c, next, runparams) - 1;
1248 pair<docstring, bool> latex = encoding.latexChar(c);
1249 docstring nextlatex;
1250 bool nexttipas = false;
1251 string nexttipashortcut;
1252 if (next != '\0' && next != META_INSET && encoding.encodable(next)) {
1253 nextlatex = encoding.latexChar(next).first;
1254 if (runparams.inIPA) {
1255 nexttipashortcut = Encodings::TIPAShortcut(next);
1256 nexttipas = !nexttipashortcut.empty();
1260 if (runparams.inIPA) {
1261 string const tipashortcut = Encodings::TIPAShortcut(c);
1262 if (!tipashortcut.empty()) {
1263 latex.first = from_ascii(tipashortcut);
1264 latex.second = false;
1268 if (Encodings::isKnownScriptChar(c, script)
1269 && prefixIs(latex.first, from_ascii("\\" + script)))
1270 column += writeScriptChars(os, latex.first,
1271 running_change, encoding, i) - 1;
1272 else if (latex.second
1273 && ((!prefixIs(nextlatex, '\\')
1274 && !prefixIs(nextlatex, '{')
1275 && !prefixIs(nextlatex, '}'))
1277 && !prefixIs(from_ascii(nexttipashortcut), '\\')))
1279 // Prevent eating of a following
1280 // space or command corruption by
1281 // following characters
1282 if (next == ' ' || next == '\0') {
1283 column += latex.first.length() + 1;
1284 os << latex.first << "{}";
1286 column += latex.first.length();
1287 os << latex.first << " ";
1290 column += latex.first.length() - 1;
1298 bool Paragraph::Private::latexSpecialT1(char_type const c, otexstream & os,
1299 pos_type i, unsigned int & column)
1305 // In T1 encoding, these characters exist
1306 // but we should avoid ligatures
1307 if (i + 1 >= int(text_.size()) || text_[i + 1] != c)
1309 os << "\\textcompwordmark{}";
1316 // soul.sty breaks with \char`\"
1317 os << "\\textquotedbl{}";
1326 bool Paragraph::Private::latexSpecialT3(char_type const c, otexstream & os,
1327 pos_type /*i*/, unsigned int & column)
1337 os << "\\textvertline{}";
1346 void Paragraph::Private::validate(LaTeXFeatures & features) const
1348 if (layout_->inpreamble && inset_owner_) {
1349 bool const is_command = layout_->latextype == LATEX_COMMAND;
1350 Buffer const & buf = inset_owner_->buffer();
1351 BufferParams const & bp = features.runparams().is_child
1352 ? buf.masterParams() : buf.params();
1355 // Using a string stream here circumvents the encoding
1356 // switching machinery of odocstream. Therefore the
1357 // output is wrong if this paragraph contains content
1358 // that needs to switch encoding.
1359 odocstringstream ods;
1360 otexstream os(ods, texrow);
1362 os << '\\' << from_ascii(layout_->latexname());
1363 // we have to provide all the optional arguments here, even though
1364 // the last one is the only one we care about.
1365 // Separate handling of optional argument inset.
1366 if (!layout_->latexargs().empty()) {
1367 OutputParams rp = features.runparams();
1368 rp.local_font = &owner_->getFirstFontSettings(bp);
1369 latexArgInsets(*owner_, os, rp, layout_->latexargs());
1371 os << from_ascii(layout_->latexparam());
1373 docstring::size_type const length = ods.str().length();
1374 // this will output "{" at the beginning, but not at the end
1375 owner_->latex(bp, f, os, features.runparams(), 0, -1, true);
1376 if (ods.str().length() > length) {
1379 if (!layout_->postcommandargs().empty()) {
1380 OutputParams rp = features.runparams();
1381 rp.local_font = &owner_->getFirstFontSettings(bp);
1382 latexArgInsets(*owner_, os, rp, layout_->postcommandargs(), "post:");
1385 string const snippet = to_utf8(ods.str());
1386 features.addPreambleSnippet(snippet);
1390 if (features.runparams().flavor == OutputParams::HTML
1391 && layout_->htmltitle()) {
1392 features.setHTMLTitle(owner_->asString(AS_STR_INSETS | AS_STR_SKIPDELETE));
1395 // check the params.
1396 if (!params_.spacing().isDefault())
1397 features.require("setspace");
1400 features.useLayout(layout_->name());
1403 fontlist_.validate(features);
1405 // then the indentation
1406 if (!params_.leftIndent().zero())
1407 features.require("ParagraphLeftIndent");
1410 InsetList::const_iterator icit = insetlist_.begin();
1411 InsetList::const_iterator iend = insetlist_.end();
1412 for (; icit != iend; ++icit) {
1414 icit->inset->validate(features);
1415 if (layout_->needprotect &&
1416 icit->inset->lyxCode() == FOOT_CODE)
1417 features.require("footmisc");
1421 // then the contents
1422 for (pos_type i = 0; i < int(text_.size()) ; ++i) {
1423 BufferEncodings::validate(text_[i], features);
1427 /////////////////////////////////////////////////////////////////////
1431 /////////////////////////////////////////////////////////////////////
1434 Layout const emptyParagraphLayout;
1437 Paragraph::Paragraph()
1438 : d(new Paragraph::Private(this, emptyParagraphLayout))
1445 Paragraph::Paragraph(Paragraph const & par)
1446 : itemdepth(par.itemdepth),
1447 d(new Paragraph::Private(*par.d, this))
1453 Paragraph::Paragraph(Paragraph const & par, pos_type beg, pos_type end)
1454 : itemdepth(par.itemdepth),
1455 d(new Paragraph::Private(*par.d, this, beg, end))
1461 Paragraph & Paragraph::operator=(Paragraph const & par)
1463 // needed as we will destroy the private part before copying it
1465 itemdepth = par.itemdepth;
1469 d = new Private(*par.d, this);
1476 Paragraph::~Paragraph()
1485 // this shall be called just before every "os << ..." action.
1486 void flushString(ostream & os, docstring & s)
1495 void Paragraph::write(ostream & os, BufferParams const & bparams,
1496 depth_type & dth) const
1498 // The beginning or end of a deeper (i.e. nested) area?
1499 if (dth != d->params_.depth()) {
1500 if (d->params_.depth() > dth) {
1501 while (d->params_.depth() > dth) {
1502 os << "\n\\begin_deeper";
1506 while (d->params_.depth() < dth) {
1507 os << "\n\\end_deeper";
1513 // First write the layout
1514 os << "\n\\begin_layout " << to_utf8(d->layout_->name()) << '\n';
1516 d->params_.write(os);
1518 Font font1(inherit_font, bparams.language);
1520 Change running_change = Change(Change::UNCHANGED);
1522 // this string is used as a buffer to avoid repetitive calls
1523 // to to_utf8(), which turn out to be expensive (JMarc)
1524 docstring write_buffer;
1527 for (pos_type i = 0; i <= size(); ++i) {
1529 Change const & change = lookupChange(i);
1530 if (change != running_change)
1531 flushString(os, write_buffer);
1532 Changes::lyxMarkChange(os, bparams, column, running_change, change);
1533 running_change = change;
1538 // Write font changes
1539 Font font2 = getFontSettings(bparams, i);
1540 if (font2 != font1) {
1541 flushString(os, write_buffer);
1542 font2.lyxWriteChanges(font1, os);
1547 char_type const c = d->text_[i];
1550 if (Inset const * inset = getInset(i)) {
1551 flushString(os, write_buffer);
1552 if (inset->directWrite()) {
1553 // international char, let it write
1554 // code directly so it's shorter in
1560 os << "\\begin_inset ";
1562 os << "\n\\end_inset\n\n";
1565 // FIXME This can be removed again once the mystery
1566 // crash has been resolved.
1571 flushString(os, write_buffer);
1572 os << "\n\\backslash\n";
1576 flushString(os, write_buffer);
1577 if (i + 1 < size() && d->text_[i + 1] == ' ') {
1584 if ((column > 70 && c == ' ')
1586 flushString(os, write_buffer);
1590 // this check is to amend a bug. LyX sometimes
1591 // inserts '\0' this could cause problems.
1593 write_buffer.push_back(c);
1595 LYXERR0("NUL char in structure.");
1601 flushString(os, write_buffer);
1602 os << "\n\\end_layout\n";
1603 // FIXME This can be removed again once the mystery
1604 // crash has been resolved.
1609 void Paragraph::validate(LaTeXFeatures & features) const
1611 d->validate(features);
1615 void Paragraph::insert(pos_type start, docstring const & str,
1616 Font const & font, Change const & change)
1618 for (size_t i = 0, n = str.size(); i != n ; ++i)
1619 insertChar(start + i, str[i], font, change);
1623 void Paragraph::appendChar(char_type c, Font const & font,
1624 Change const & change)
1627 d->changes_.insert(change, d->text_.size());
1628 // when appending characters, no need to update tables
1629 d->text_.push_back(c);
1630 setFont(d->text_.size() - 1, font);
1631 d->requestSpellCheck(d->text_.size() - 1);
1635 void Paragraph::appendString(docstring const & s, Font const & font,
1636 Change const & change)
1638 pos_type end = s.size();
1639 size_t oldsize = d->text_.size();
1640 size_t newsize = oldsize + end;
1641 size_t capacity = d->text_.capacity();
1642 if (newsize >= capacity)
1643 d->text_.reserve(max(capacity + 100, newsize));
1645 // when appending characters, no need to update tables
1648 // FIXME: Optimize this!
1649 for (size_t i = oldsize; i != newsize; ++i) {
1651 d->changes_.insert(change, i);
1652 d->requestSpellCheck(i);
1654 d->fontlist_.set(oldsize, font);
1655 d->fontlist_.set(newsize - 1, font);
1659 void Paragraph::insertChar(pos_type pos, char_type c,
1662 d->insertChar(pos, c, Change(trackChanges ?
1663 Change::INSERTED : Change::UNCHANGED));
1667 void Paragraph::insertChar(pos_type pos, char_type c,
1668 Font const & font, bool trackChanges)
1670 d->insertChar(pos, c, Change(trackChanges ?
1671 Change::INSERTED : Change::UNCHANGED));
1676 void Paragraph::insertChar(pos_type pos, char_type c,
1677 Font const & font, Change const & change)
1679 d->insertChar(pos, c, change);
1684 void Paragraph::resetFonts(Font const & font)
1686 d->fontlist_.clear();
1687 d->fontlist_.set(0, font);
1688 d->fontlist_.set(d->text_.size() - 1, font);
1691 // Gets uninstantiated font setting at position.
1692 Font const & Paragraph::getFontSettings(BufferParams const & bparams,
1696 LYXERR0("pos: " << pos << " size: " << size());
1700 FontList::const_iterator cit = d->fontlist_.fontIterator(pos);
1701 if (cit != d->fontlist_.end())
1704 if (pos == size() && !empty())
1705 return getFontSettings(bparams, pos - 1);
1707 // Optimisation: avoid a full font instantiation if there is no
1708 // language change from previous call.
1709 static Font previous_font;
1710 static Language const * previous_lang = 0;
1711 Language const * lang = getParLanguage(bparams);
1712 if (lang != previous_lang) {
1713 previous_lang = lang;
1714 previous_font = Font(inherit_font, lang);
1716 return previous_font;
1720 FontSpan Paragraph::fontSpan(pos_type pos) const
1722 LBUFERR(pos < size());
1725 FontList::const_iterator cit = d->fontlist_.begin();
1726 FontList::const_iterator end = d->fontlist_.end();
1727 for (; cit != end; ++cit) {
1728 if (cit->pos() >= pos) {
1729 if (pos >= beginOfBody())
1730 return FontSpan(max(start, beginOfBody()),
1733 return FontSpan(start,
1734 min(beginOfBody() - 1,
1737 start = cit->pos() + 1;
1740 // This should not happen, but if so, we take no chances.
1741 LYXERR0("Paragraph::fontSpan: position not found in fontinfo table!");
1742 LASSERT(false, return FontSpan(pos, pos));
1746 // Gets uninstantiated font setting at position 0
1747 Font const & Paragraph::getFirstFontSettings(BufferParams const & bparams) const
1749 if (!empty() && !d->fontlist_.empty())
1750 return d->fontlist_.begin()->font();
1752 // Optimisation: avoid a full font instantiation if there is no
1753 // language change from previous call.
1754 static Font previous_font;
1755 static Language const * previous_lang = 0;
1756 if (bparams.language != previous_lang) {
1757 previous_lang = bparams.language;
1758 previous_font = Font(inherit_font, bparams.language);
1761 return previous_font;
1765 // Gets the fully instantiated font at a given position in a paragraph
1766 // This is basically the same function as Text::GetFont() in text2.cpp.
1767 // The difference is that this one is used for generating the LaTeX file,
1768 // and thus cosmetic "improvements" are disallowed: This has to deliver
1769 // the true picture of the buffer. (Asger)
1770 Font const Paragraph::getFont(BufferParams const & bparams, pos_type pos,
1771 Font const & outerfont) const
1775 Font font = getFontSettings(bparams, pos);
1777 pos_type const body_pos = beginOfBody();
1778 FontInfo & fi = font.fontInfo();
1780 fi.realize(d->layout_->labelfont);
1782 fi.realize(d->layout_->font);
1784 fi.realize(outerfont.fontInfo());
1785 fi.realize(bparams.getFont().fontInfo());
1791 Font const Paragraph::getLabelFont
1792 (BufferParams const & bparams, Font const & outerfont) const
1794 FontInfo tmpfont = d->layout_->labelfont;
1795 tmpfont.realize(outerfont.fontInfo());
1796 tmpfont.realize(bparams.getFont().fontInfo());
1797 return Font(tmpfont, getParLanguage(bparams));
1801 Font const Paragraph::getLayoutFont
1802 (BufferParams const & bparams, Font const & outerfont) const
1804 FontInfo tmpfont = d->layout_->font;
1805 tmpfont.realize(outerfont.fontInfo());
1806 tmpfont.realize(bparams.getFont().fontInfo());
1807 return Font(tmpfont, getParLanguage(bparams));
1811 /// Returns the height of the highest font in range
1812 FontSize Paragraph::highestFontInRange
1813 (pos_type startpos, pos_type endpos, FontSize def_size) const
1815 return d->fontlist_.highestInRange(startpos, endpos, def_size);
1819 char_type Paragraph::getUChar(BufferParams const & bparams, pos_type pos) const
1821 char_type c = d->text_[pos];
1822 if (!getFontSettings(bparams, pos).isRightToLeft())
1825 // FIXME: The arabic special casing is due to the difference of arabic
1826 // round brackets input introduced in r18599. Check if this should be
1827 // unified with Hebrew or at least if all bracket types should be
1828 // handled the same (file format change in either case).
1829 string const & lang = getFontSettings(bparams, pos).language()->lang();
1830 bool const arabic = lang == "arabic_arabtex" || lang == "arabic_arabi"
1835 uc = arabic ? c : ')';
1838 uc = arabic ? c : '(';
1864 void Paragraph::setFont(pos_type pos, Font const & font)
1866 LASSERT(pos <= size(), return);
1868 // First, reduce font against layout/label font
1869 // Update: The setCharFont() routine in text2.cpp already
1870 // reduces font, so we don't need to do that here. (Asger)
1872 d->fontlist_.set(pos, font);
1876 void Paragraph::makeSameLayout(Paragraph const & par)
1878 d->layout_ = par.d->layout_;
1879 d->params_ = par.d->params_;
1883 bool Paragraph::stripLeadingSpaces(bool trackChanges)
1885 if (isFreeSpacing())
1891 while (pos < size() && (isNewline(pos) || isLineSeparator(pos))) {
1892 if (eraseChar(pos, trackChanges))
1898 return count > 0 || pos > 0;
1902 bool Paragraph::hasSameLayout(Paragraph const & par) const
1904 return par.d->layout_ == d->layout_
1905 && d->params_.sameLayout(par.d->params_);
1909 depth_type Paragraph::getDepth() const
1911 return d->params_.depth();
1915 depth_type Paragraph::getMaxDepthAfter() const
1917 if (d->layout_->isEnvironment())
1918 return d->params_.depth() + 1;
1920 return d->params_.depth();
1924 char Paragraph::getAlign() const
1926 if (d->params_.align() == LYX_ALIGN_LAYOUT)
1927 return d->layout_->align;
1929 return d->params_.align();
1933 docstring const & Paragraph::labelString() const
1935 return d->params_.labelString();
1939 // the next two functions are for the manual labels
1940 docstring const Paragraph::getLabelWidthString() const
1942 if (d->layout_->margintype == MARGIN_MANUAL
1943 || d->layout_->latextype == LATEX_BIB_ENVIRONMENT)
1944 return d->params_.labelWidthString();
1946 return _("Senseless with this layout!");
1950 void Paragraph::setLabelWidthString(docstring const & s)
1952 d->params_.labelWidthString(s);
1956 docstring Paragraph::expandLabel(Layout const & layout,
1957 BufferParams const & bparams) const
1959 return expandParagraphLabel(layout, bparams, true);
1963 docstring Paragraph::expandDocBookLabel(Layout const & layout,
1964 BufferParams const & bparams) const
1966 return expandParagraphLabel(layout, bparams, false);
1970 docstring Paragraph::expandParagraphLabel(Layout const & layout,
1971 BufferParams const & bparams, bool process_appendix) const
1973 DocumentClass const & tclass = bparams.documentClass();
1974 string const & lang = getParLanguage(bparams)->code();
1975 bool const in_appendix = process_appendix && d->params_.appendix();
1976 docstring fmt = translateIfPossible(layout.labelstring(in_appendix), lang);
1978 if (fmt.empty() && !layout.counter.empty())
1979 return tclass.counters().theCounter(layout.counter, lang);
1981 // handle 'inherited level parts' in 'fmt',
1982 // i.e. the stuff between '@' in '@Section@.\arabic{subsection}'
1983 size_t const i = fmt.find('@', 0);
1984 if (i != docstring::npos) {
1985 size_t const j = fmt.find('@', i + 1);
1986 if (j != docstring::npos) {
1987 docstring parent(fmt, i + 1, j - i - 1);
1988 docstring label = from_ascii("??");
1989 if (tclass.hasLayout(parent))
1990 docstring label = expandParagraphLabel(tclass[parent], bparams,
1992 fmt = docstring(fmt, 0, i) + label
1993 + docstring(fmt, j + 1, docstring::npos);
1997 return tclass.counters().counterLabel(fmt, lang);
2001 void Paragraph::applyLayout(Layout const & new_layout)
2003 d->layout_ = &new_layout;
2004 LyXAlignment const oldAlign = d->params_.align();
2006 if (!(oldAlign & d->layout_->alignpossible)) {
2007 frontend::Alert::warning(_("Alignment not permitted"),
2008 _("The new layout does not permit the alignment previously used.\nSetting to default."));
2009 d->params_.align(LYX_ALIGN_LAYOUT);
2014 pos_type Paragraph::beginOfBody() const
2016 return d->begin_of_body_;
2020 void Paragraph::setBeginOfBody()
2022 if (d->layout_->labeltype != LABEL_MANUAL) {
2023 d->begin_of_body_ = 0;
2027 // Unroll the first two cycles of the loop
2028 // and remember the previous character to
2029 // remove unnecessary getChar() calls
2031 pos_type end = size();
2032 if (i < end && !(isNewline(i) || isEnvSeparator(i))) {
2034 char_type previous_char = 0;
2037 previous_char = d->text_[i];
2038 if (!(isNewline(i) || isEnvSeparator(i))) {
2040 while (i < end && previous_char != ' ') {
2042 if (isNewline(i) || isEnvSeparator(i))
2045 previous_char = temp;
2051 d->begin_of_body_ = i;
2055 bool Paragraph::allowParagraphCustomization() const
2057 return inInset().allowParagraphCustomization();
2061 bool Paragraph::usePlainLayout() const
2063 return inInset().usePlainLayout();
2067 bool Paragraph::isPassThru() const
2069 return inInset().isPassThru() || d->layout_->pass_thru;
2074 // paragraphs inside floats need different alignment tags to avoid
2077 bool noTrivlistCentering(InsetCode code)
2079 return code == FLOAT_CODE
2080 || code == WRAP_CODE
2081 || code == CELL_CODE;
2085 string correction(string const & orig)
2087 if (orig == "flushleft")
2088 return "raggedright";
2089 if (orig == "flushright")
2090 return "raggedleft";
2091 if (orig == "center")
2097 bool corrected_env(otexstream & os, string const & suffix, string const & env,
2098 InsetCode code, bool const lastpar, int & col)
2100 string macro = suffix + "{";
2101 if (noTrivlistCentering(code)) {
2103 // the last paragraph in non-trivlist-aligned
2104 // context is special (to avoid unwanted whitespace)
2105 if (suffix == "\\begin") {
2106 macro = "\\" + correction(env) + "{}";
2107 os << from_ascii(macro);
2108 col += macro.size();
2113 macro += correction(env);
2117 if (suffix == "\\par\\end") {
2121 os << from_ascii(macro);
2122 col += macro.size();
2123 if (suffix == "\\begin") {
2133 int Paragraph::Private::startTeXParParams(BufferParams const & bparams,
2134 otexstream & os, OutputParams const & runparams) const
2138 if (params_.noindent() && !layout_->pass_thru
2139 && (layout_->toggle_indent != ITOGGLE_NEVER)) {
2140 os << "\\noindent ";
2144 LyXAlignment const curAlign = params_.align();
2146 if (curAlign == layout_->align)
2150 case LYX_ALIGN_NONE:
2151 case LYX_ALIGN_BLOCK:
2152 case LYX_ALIGN_LAYOUT:
2153 case LYX_ALIGN_SPECIAL:
2154 case LYX_ALIGN_DECIMAL:
2156 case LYX_ALIGN_LEFT:
2157 case LYX_ALIGN_RIGHT:
2158 case LYX_ALIGN_CENTER:
2159 if (runparams.moving_arg) {
2166 string const begin_tag = "\\begin";
2167 InsetCode code = ownerCode();
2168 bool const lastpar = runparams.isLastPar;
2171 case LYX_ALIGN_NONE:
2172 case LYX_ALIGN_BLOCK:
2173 case LYX_ALIGN_LAYOUT:
2174 case LYX_ALIGN_SPECIAL:
2175 case LYX_ALIGN_DECIMAL:
2177 case LYX_ALIGN_LEFT: {
2178 if (owner_->getParLanguage(bparams)->babel() != "hebrew")
2179 corrected_env(os, begin_tag, "flushleft", code, lastpar, column);
2181 corrected_env(os, begin_tag, "flushright", code, lastpar, column);
2183 } case LYX_ALIGN_RIGHT: {
2185 if (owner_->getParLanguage(bparams)->babel() != "hebrew")
2186 corrected_env(os, begin_tag, "flushright", code, lastpar, column);
2188 corrected_env(os, begin_tag, "flushleft", code, lastpar, column);
2190 } case LYX_ALIGN_CENTER: {
2191 corrected_env(os, begin_tag, "center", code, lastpar, column);
2200 bool Paragraph::Private::endTeXParParams(BufferParams const & bparams,
2201 otexstream & os, OutputParams const & runparams) const
2203 LyXAlignment const curAlign = params_.align();
2205 if (curAlign == layout_->align)
2209 case LYX_ALIGN_NONE:
2210 case LYX_ALIGN_BLOCK:
2211 case LYX_ALIGN_LAYOUT:
2212 case LYX_ALIGN_SPECIAL:
2213 case LYX_ALIGN_DECIMAL:
2215 case LYX_ALIGN_LEFT:
2216 case LYX_ALIGN_RIGHT:
2217 case LYX_ALIGN_CENTER:
2218 if (runparams.moving_arg)
2223 bool output = false;
2225 string const end_tag = "\\par\\end";
2226 InsetCode code = ownerCode();
2227 bool const lastpar = runparams.isLastPar;
2230 case LYX_ALIGN_NONE:
2231 case LYX_ALIGN_BLOCK:
2232 case LYX_ALIGN_LAYOUT:
2233 case LYX_ALIGN_SPECIAL:
2234 case LYX_ALIGN_DECIMAL:
2236 case LYX_ALIGN_LEFT: {
2237 if (owner_->getParLanguage(bparams)->babel() != "hebrew")
2238 output = corrected_env(os, end_tag, "flushleft", code, lastpar, col);
2240 output = corrected_env(os, end_tag, "flushright", code, lastpar, col);
2242 } case LYX_ALIGN_RIGHT: {
2243 if (owner_->getParLanguage(bparams)->babel() != "hebrew")
2244 output = corrected_env(os, end_tag, "flushright", code, lastpar, col);
2246 output = corrected_env(os, end_tag, "flushleft", code, lastpar, col);
2248 } case LYX_ALIGN_CENTER: {
2249 corrected_env(os, end_tag, "center", code, lastpar, col);
2254 return output || lastpar;
2258 // This one spits out the text of the paragraph
2259 void Paragraph::latex(BufferParams const & bparams,
2260 Font const & outerfont,
2262 OutputParams const & runparams,
2263 int start_pos, int end_pos, bool force) const
2265 LYXERR(Debug::LATEX, "Paragraph::latex... " << this);
2267 // FIXME This check should not be needed. Perhaps issue an
2268 // error if it triggers.
2269 Layout const & style = inInset().forcePlainLayout() ?
2270 bparams.documentClass().plainLayout() : *d->layout_;
2272 if (!force && style.inpreamble)
2275 bool const allowcust = allowParagraphCustomization();
2277 // Current base font for all inherited font changes, without any
2278 // change caused by an individual character, except for the language:
2279 // It is set to the language of the first character.
2280 // As long as we are in the label, this font is the base font of the
2281 // label. Before the first body character it is set to the base font
2285 // Maybe we have to create a optional argument.
2286 pos_type body_pos = beginOfBody();
2287 unsigned int column = 0;
2290 // the optional argument is kept in curly brackets in
2291 // case it contains a ']'
2292 // This is not strictly needed, but if this is changed it
2293 // would be a file format change, and tex2lyx would need
2294 // to be adjusted, since it unconditionally removes the
2295 // braces when it parses \item.
2298 basefont = getLabelFont(bparams, outerfont);
2300 basefont = getLayoutFont(bparams, outerfont);
2303 // Which font is currently active?
2304 Font running_font(basefont);
2305 // Do we have an open font change?
2306 bool open_font = false;
2308 Change runningChange = Change(Change::UNCHANGED);
2310 Encoding const * const prev_encoding = runparams.encoding;
2312 os.texrow().start(id(), 0);
2314 // if the paragraph is empty, the loop will not be entered at all
2316 if (style.isCommand()) {
2320 if (!style.leftdelim().empty()) {
2321 os << style.leftdelim();
2322 column += style.leftdelim().size();
2325 column += d->startTeXParParams(bparams, os, runparams);
2328 for (pos_type i = 0; i < size(); ++i) {
2329 // First char in paragraph or after label?
2330 if (i == body_pos) {
2333 column += running_font.latexWriteEndChanges(
2334 os, bparams, runparams,
2335 basefont, basefont);
2338 basefont = getLayoutFont(bparams, outerfont);
2339 running_font = basefont;
2341 column += Changes::latexMarkChange(os, bparams,
2342 runningChange, Change(Change::UNCHANGED),
2344 runningChange = Change(Change::UNCHANGED);
2349 if (style.isCommand()) {
2354 if (!style.leftdelim().empty()) {
2355 os << style.leftdelim();
2356 column += style.leftdelim().size();
2360 column += d->startTeXParParams(bparams, os,
2364 Change const & change = runparams.inDeletedInset
2365 ? runparams.changeOfDeletedInset : lookupChange(i);
2367 if (bparams.output_changes && runningChange != change) {
2369 column += running_font.latexWriteEndChanges(
2370 os, bparams, runparams, basefont, basefont);
2373 basefont = getLayoutFont(bparams, outerfont);
2374 running_font = basefont;
2376 column += Changes::latexMarkChange(os, bparams, runningChange,
2378 runningChange = change;
2381 // do not output text which is marked deleted
2382 // if change tracking output is disabled
2383 if (!bparams.output_changes && change.deleted()) {
2389 // Fully instantiated font
2390 Font const font = getFont(bparams, i, outerfont);
2392 Font const last_font = running_font;
2394 // Do we need to close the previous font?
2396 (font != running_font ||
2397 font.language() != running_font.language()))
2399 column += running_font.latexWriteEndChanges(
2400 os, bparams, runparams, basefont,
2401 (i == body_pos-1) ? basefont : font);
2402 running_font = basefont;
2406 string const running_lang = runparams.use_polyglossia ?
2407 running_font.language()->polyglossia() : running_font.language()->babel();
2408 // close babel's font environment before opening CJK.
2409 string const lang_end_command = runparams.use_polyglossia ?
2410 "\\end{$$lang}" : lyxrc.language_command_end;
2411 if (!running_lang.empty() &&
2412 font.language()->encoding()->package() == Encoding::CJK) {
2413 string end_tag = subst(lang_end_command,
2416 os << from_ascii(end_tag);
2417 column += end_tag.length();
2420 // Switch file encoding if necessary (and allowed)
2421 if (!runparams.pass_thru && !style.pass_thru &&
2422 runparams.encoding->package() != Encoding::none &&
2423 font.language()->encoding()->package() != Encoding::none) {
2424 pair<bool, int> const enc_switch =
2425 switchEncoding(os.os(), bparams, runparams,
2426 *(font.language()->encoding()));
2427 if (enc_switch.first) {
2428 column += enc_switch.second;
2429 runparams.encoding = font.language()->encoding();
2433 char_type const c = d->text_[i];
2435 // Do we need to change font?
2436 if ((font != running_font ||
2437 font.language() != running_font.language()) &&
2440 odocstringstream ods;
2441 column += font.latexWriteStartChanges(ods, bparams,
2442 runparams, basefont,
2444 running_font = font;
2446 docstring fontchange = ods.str();
2447 // check whether the fontchange ends with a \\textcolor
2448 // modifier and the text starts with a space (bug 4473)
2449 docstring const last_modifier = rsplit(fontchange, '\\');
2450 if (prefixIs(last_modifier, from_ascii("textcolor")) && c == ' ')
2451 os << fontchange << from_ascii("{}");
2452 // check if the fontchange ends with a trailing blank
2453 // (like "\small " (see bug 3382)
2454 else if (suffixIs(fontchange, ' ') && c == ' ')
2455 os << fontchange.substr(0, fontchange.size() - 1)
2456 << from_ascii("{}");
2461 // FIXME: think about end_pos implementation...
2462 if (c == ' ' && i >= start_pos && (end_pos == -1 || i < end_pos)) {
2463 // FIXME: integrate this case in latexSpecialChar
2464 // Do not print the separation of the optional argument
2465 // if style.pass_thru is false. This works because
2466 // latexSpecialChar ignores spaces if
2467 // style.pass_thru is false.
2468 if (i != body_pos - 1) {
2469 if (d->simpleTeXBlanks(runparams, os,
2470 i, column, font, style)) {
2471 // A surrogate pair was output. We
2472 // must not call latexSpecialChar
2473 // in this iteration, since it would output
2474 // the combining character again.
2481 OutputParams rp = runparams;
2482 rp.free_spacing = style.free_spacing;
2483 rp.local_font = &font;
2484 rp.intitle = style.intitle;
2486 // Two major modes: LaTeX or plain
2487 // Handle here those cases common to both modes
2488 // and then split to handle the two modes separately.
2489 if (c == META_INSET) {
2490 if (i >= start_pos && (end_pos == -1 || i < end_pos)) {
2491 d->latexInset(bparams, os, rp, running_font,
2492 basefont, outerfont, open_font,
2493 runningChange, style, i, column);
2496 if (i >= start_pos && (end_pos == -1 || i < end_pos)) {
2498 d->latexSpecialChar(os, bparams, rp, running_font, runningChange,
2499 style, i, end_pos, column);
2500 } catch (EncodingException & e) {
2501 if (runparams.dryrun) {
2502 os << "<" << _("LyX Warning: ")
2503 << _("uncodable character") << " '";
2507 // add location information and throw again.
2516 // Set the encoding to that returned from latexSpecialChar (see
2517 // comment for encoding member in OutputParams.h)
2518 runparams.encoding = rp.encoding;
2521 // If we have an open font definition, we have to close it
2523 #ifdef FIXED_LANGUAGE_END_DETECTION
2525 running_font.latexWriteEndChanges(os, bparams,
2526 runparams, basefont,
2527 next_->getFont(bparams, 0, outerfont));
2529 running_font.latexWriteEndChanges(os, bparams,
2530 runparams, basefont, basefont);
2533 //FIXME: For now we ALWAYS have to close the foreign font settings if they are
2534 //FIXME: there as we start another \selectlanguage with the next paragraph if
2535 //FIXME: we are in need of this. This should be fixed sometime (Jug)
2536 running_font.latexWriteEndChanges(os, bparams, runparams,
2537 basefont, basefont);
2541 column += Changes::latexMarkChange(os, bparams, runningChange,
2542 Change(Change::UNCHANGED), runparams);
2544 // Needed if there is an optional argument but no contents.
2545 if (body_pos > 0 && body_pos == size()) {
2549 if (!style.rightdelim().empty()) {
2550 os << style.rightdelim();
2551 column += style.rightdelim().size();
2554 if (allowcust && d->endTeXParParams(bparams, os, runparams)
2555 && runparams.encoding != prev_encoding) {
2556 runparams.encoding = prev_encoding;
2557 if (!runparams.isFullUnicode())
2558 os << setEncoding(prev_encoding->iconvName());
2561 LYXERR(Debug::LATEX, "Paragraph::latex... done " << this);
2565 bool Paragraph::emptyTag() const
2567 for (pos_type i = 0; i < size(); ++i) {
2568 if (Inset const * inset = getInset(i)) {
2569 InsetCode lyx_code = inset->lyxCode();
2570 // FIXME testing like that is wrong. What is
2572 if (lyx_code != TOC_CODE &&
2573 lyx_code != INCLUDE_CODE &&
2574 lyx_code != GRAPHICS_CODE &&
2575 lyx_code != ERT_CODE &&
2576 lyx_code != LISTINGS_CODE &&
2577 lyx_code != FLOAT_CODE &&
2578 lyx_code != TABULAR_CODE) {
2582 char_type c = d->text_[i];
2583 if (c != ' ' && c != '\t')
2591 string Paragraph::getID(Buffer const & buf, OutputParams const & runparams)
2594 for (pos_type i = 0; i < size(); ++i) {
2595 if (Inset const * inset = getInset(i)) {
2596 InsetCode lyx_code = inset->lyxCode();
2597 if (lyx_code == LABEL_CODE) {
2598 InsetLabel const * const il = static_cast<InsetLabel const *>(inset);
2599 docstring const & id = il->getParam("name");
2600 return "id='" + to_utf8(sgml::cleanID(buf, runparams, id)) + "'";
2608 pos_type Paragraph::firstWordDocBook(odocstream & os, OutputParams const & runparams)
2612 for (i = 0; i < size(); ++i) {
2613 if (Inset const * inset = getInset(i)) {
2614 inset->docbook(os, runparams);
2616 char_type c = d->text_[i];
2619 os << sgml::escapeChar(c);
2626 pos_type Paragraph::firstWordLyXHTML(XHTMLStream & xs, OutputParams const & runparams)
2630 for (i = 0; i < size(); ++i) {
2631 if (Inset const * inset = getInset(i)) {
2632 inset->xhtml(xs, runparams);
2634 char_type c = d->text_[i];
2644 bool Paragraph::Private::onlyText(Buffer const & buf, Font const & outerfont, pos_type initial) const
2647 pos_type size = text_.size();
2648 for (pos_type i = initial; i < size; ++i) {
2649 Font font = owner_->getFont(buf.params(), i, outerfont);
2650 if (text_[i] == META_INSET)
2652 if (i != initial && font != font_old)
2661 void Paragraph::simpleDocBookOnePar(Buffer const & buf,
2663 OutputParams const & runparams,
2664 Font const & outerfont,
2665 pos_type initial) const
2667 bool emph_flag = false;
2669 Layout const & style = *d->layout_;
2671 style.labeltype == LABEL_MANUAL ? style.labelfont : style.font;
2673 if (style.pass_thru && !d->onlyText(buf, outerfont, initial))
2676 // parsing main loop
2677 for (pos_type i = initial; i < size(); ++i) {
2678 Font font = getFont(buf.params(), i, outerfont);
2680 // handle <emphasis> tag
2681 if (font_old.emph() != font.fontInfo().emph()) {
2682 if (font.fontInfo().emph() == FONT_ON) {
2685 } else if (i != initial) {
2686 os << "</emphasis>";
2691 if (Inset const * inset = getInset(i)) {
2692 inset->docbook(os, runparams);
2694 char_type c = d->text_[i];
2696 if (style.pass_thru)
2699 os << sgml::escapeChar(c);
2701 font_old = font.fontInfo();
2705 os << "</emphasis>";
2708 if (style.free_spacing)
2710 if (style.pass_thru && !d->onlyText(buf, outerfont, initial))
2716 void doFontSwitch(vector<html::FontTag> & tagsToOpen,
2717 vector<html::EndFontTag> & tagsToClose,
2718 bool & flag, FontState curstate, html::FontTypes type)
2720 if (curstate == FONT_ON) {
2721 tagsToOpen.push_back(html::FontTag(type));
2724 tagsToClose.push_back(html::EndFontTag(type));
2731 docstring Paragraph::simpleLyXHTMLOnePar(Buffer const & buf,
2733 OutputParams const & runparams,
2734 Font const & outerfont,
2735 pos_type initial) const
2739 // track whether we have opened these tags
2740 bool emph_flag = false;
2741 bool bold_flag = false;
2742 bool noun_flag = false;
2743 bool ubar_flag = false;
2744 bool dbar_flag = false;
2745 bool sout_flag = false;
2746 bool wave_flag = false;
2748 bool shap_flag = false;
2750 bool faml_flag = false;
2752 bool size_flag = false;
2754 Layout const & style = *d->layout_;
2756 xs.startParagraph(allowEmpty());
2759 style.labeltype == LABEL_MANUAL ? style.labelfont : style.font;
2761 FontShape curr_fs = INHERIT_SHAPE;
2762 FontFamily curr_fam = INHERIT_FAMILY;
2763 FontSize curr_size = FONT_SIZE_INHERIT;
2765 string const default_family =
2766 buf.masterBuffer()->params().fonts_default_family;
2768 vector<html::FontTag> tagsToOpen;
2769 vector<html::EndFontTag> tagsToClose;
2771 // parsing main loop
2772 for (pos_type i = initial; i < size(); ++i) {
2773 // let's not show deleted material in the output
2777 Font const font = getFont(buf.masterBuffer()->params(), i, outerfont);
2780 FontState curstate = font.fontInfo().emph();
2781 if (font_old.emph() != curstate)
2782 doFontSwitch(tagsToOpen, tagsToClose, emph_flag, curstate, html::FT_EMPH);
2785 curstate = font.fontInfo().noun();
2786 if (font_old.noun() != curstate)
2787 doFontSwitch(tagsToOpen, tagsToClose, noun_flag, curstate, html::FT_NOUN);
2790 curstate = font.fontInfo().underbar();
2791 if (font_old.underbar() != curstate)
2792 doFontSwitch(tagsToOpen, tagsToClose, ubar_flag, curstate, html::FT_UBAR);
2795 curstate = font.fontInfo().strikeout();
2796 if (font_old.strikeout() != curstate)
2797 doFontSwitch(tagsToOpen, tagsToClose, sout_flag, curstate, html::FT_SOUT);
2800 curstate = font.fontInfo().uuline();
2801 if (font_old.uuline() != curstate)
2802 doFontSwitch(tagsToOpen, tagsToClose, dbar_flag, curstate, html::FT_DBAR);
2805 curstate = font.fontInfo().uwave();
2806 if (font_old.uwave() != curstate)
2807 doFontSwitch(tagsToOpen, tagsToClose, wave_flag, curstate, html::FT_WAVE);
2810 // a little hackish, but allows us to reuse what we have.
2811 curstate = (font.fontInfo().series() == BOLD_SERIES ? FONT_ON : FONT_OFF);
2812 if (font_old.series() != font.fontInfo().series())
2813 doFontSwitch(tagsToOpen, tagsToClose, bold_flag, curstate, html::FT_BOLD);
2816 curr_fs = font.fontInfo().shape();
2817 FontShape old_fs = font_old.shape();
2818 if (old_fs != curr_fs) {
2822 tagsToClose.push_back(html::EndFontTag(html::FT_ITALIC));
2825 tagsToClose.push_back(html::EndFontTag(html::FT_SLANTED));
2827 case SMALLCAPS_SHAPE:
2828 tagsToClose.push_back(html::EndFontTag(html::FT_SMALLCAPS));
2834 // the other tags are for internal use
2842 tagsToOpen.push_back(html::FontTag(html::FT_ITALIC));
2846 tagsToOpen.push_back(html::FontTag(html::FT_SLANTED));
2849 case SMALLCAPS_SHAPE:
2850 tagsToOpen.push_back(html::FontTag(html::FT_SMALLCAPS));
2857 // the other tags are for internal use
2864 curr_fam = font.fontInfo().family();
2865 FontFamily old_fam = font_old.family();
2866 if (old_fam != curr_fam) {
2870 tagsToClose.push_back(html::EndFontTag(html::FT_ROMAN));
2873 tagsToClose.push_back(html::EndFontTag(html::FT_SANS));
2875 case TYPEWRITER_FAMILY:
2876 tagsToClose.push_back(html::EndFontTag(html::FT_TYPE));
2878 case INHERIT_FAMILY:
2881 // the other tags are for internal use
2889 // we will treat a "default" font family as roman, since we have
2890 // no other idea what to do.
2891 if (default_family != "rmdefault" && default_family != "default") {
2892 tagsToOpen.push_back(html::FontTag(html::FT_ROMAN));
2897 if (default_family != "sfdefault") {
2898 tagsToOpen.push_back(html::FontTag(html::FT_SANS));
2902 case TYPEWRITER_FAMILY:
2903 if (default_family != "ttdefault") {
2904 tagsToOpen.push_back(html::FontTag(html::FT_TYPE));
2908 case INHERIT_FAMILY:
2911 // the other tags are for internal use
2918 curr_size = font.fontInfo().size();
2919 FontSize old_size = font_old.size();
2920 if (old_size != curr_size) {
2923 case FONT_SIZE_TINY:
2924 tagsToClose.push_back(html::EndFontTag(html::FT_SIZE_TINY));
2926 case FONT_SIZE_SCRIPT:
2927 tagsToClose.push_back(html::EndFontTag(html::FT_SIZE_SCRIPT));
2929 case FONT_SIZE_FOOTNOTE:
2930 tagsToClose.push_back(html::EndFontTag(html::FT_SIZE_FOOTNOTE));
2932 case FONT_SIZE_SMALL:
2933 tagsToClose.push_back(html::EndFontTag(html::FT_SIZE_SMALL));
2935 case FONT_SIZE_LARGE:
2936 tagsToClose.push_back(html::EndFontTag(html::FT_SIZE_LARGE));
2938 case FONT_SIZE_LARGER:
2939 tagsToClose.push_back(html::EndFontTag(html::FT_SIZE_LARGER));
2941 case FONT_SIZE_LARGEST:
2942 tagsToClose.push_back(html::EndFontTag(html::FT_SIZE_LARGEST));
2944 case FONT_SIZE_HUGE:
2945 tagsToClose.push_back(html::EndFontTag(html::FT_SIZE_HUGE));
2947 case FONT_SIZE_HUGER:
2948 tagsToClose.push_back(html::EndFontTag(html::FT_SIZE_HUGER));
2950 case FONT_SIZE_INCREASE:
2951 tagsToClose.push_back(html::EndFontTag(html::FT_SIZE_INCREASE));
2953 case FONT_SIZE_DECREASE:
2954 tagsToClose.push_back(html::EndFontTag(html::FT_SIZE_DECREASE));
2956 case FONT_SIZE_INHERIT:
2957 case FONT_SIZE_NORMAL:
2960 // the other tags are for internal use
2966 switch (curr_size) {
2967 case FONT_SIZE_TINY:
2968 tagsToOpen.push_back(html::FontTag(html::FT_SIZE_TINY));
2971 case FONT_SIZE_SCRIPT:
2972 tagsToOpen.push_back(html::FontTag(html::FT_SIZE_SCRIPT));
2975 case FONT_SIZE_FOOTNOTE:
2976 tagsToOpen.push_back(html::FontTag(html::FT_SIZE_FOOTNOTE));
2979 case FONT_SIZE_SMALL:
2980 tagsToOpen.push_back(html::FontTag(html::FT_SIZE_SMALL));
2983 case FONT_SIZE_LARGE:
2984 tagsToOpen.push_back(html::FontTag(html::FT_SIZE_LARGE));
2987 case FONT_SIZE_LARGER:
2988 tagsToOpen.push_back(html::FontTag(html::FT_SIZE_LARGER));
2991 case FONT_SIZE_LARGEST:
2992 tagsToOpen.push_back(html::FontTag(html::FT_SIZE_LARGEST));
2995 case FONT_SIZE_HUGE:
2996 tagsToOpen.push_back(html::FontTag(html::FT_SIZE_HUGE));
2999 case FONT_SIZE_HUGER:
3000 tagsToOpen.push_back(html::FontTag(html::FT_SIZE_HUGER));
3003 case FONT_SIZE_INCREASE:
3004 tagsToOpen.push_back(html::FontTag(html::FT_SIZE_INCREASE));
3007 case FONT_SIZE_DECREASE:
3008 tagsToOpen.push_back(html::FontTag(html::FT_SIZE_DECREASE));
3011 case FONT_SIZE_NORMAL:
3012 case FONT_SIZE_INHERIT:
3015 // the other tags are for internal use
3022 // Other such tags? What about the other text ranges?
3024 vector<html::EndFontTag>::const_iterator cit = tagsToClose.begin();
3025 vector<html::EndFontTag>::const_iterator cen = tagsToClose.end();
3026 for (; cit != cen; ++cit)
3029 vector<html::FontTag>::const_iterator sit = tagsToOpen.begin();
3030 vector<html::FontTag>::const_iterator sen = tagsToOpen.end();
3031 for (; sit != sen; ++sit)
3034 tagsToClose.clear();
3037 Inset const * inset = getInset(i);
3039 if (!runparams.for_toc || inset->isInToc()) {
3040 OutputParams np = runparams;
3041 np.local_font = &font;
3042 if (!inset->getLayout().htmlisblock())
3043 np.html_in_par = true;
3044 retval += inset->xhtml(xs, np);
3047 char_type c = getUChar(buf.masterBuffer()->params(), i);
3050 font_old = font.fontInfo();
3059 bool Paragraph::isHfill(pos_type pos) const
3061 Inset const * inset = getInset(pos);
3062 return inset && inset->isHfill();
3066 bool Paragraph::isNewline(pos_type pos) const
3068 Inset const * inset = getInset(pos);
3069 return inset && inset->lyxCode() == NEWLINE_CODE;
3073 bool Paragraph::isEnvSeparator(pos_type pos) const
3075 Inset const * inset = getInset(pos);
3076 return inset && inset->lyxCode() == SEPARATOR_CODE;
3080 bool Paragraph::isLineSeparator(pos_type pos) const
3082 char_type const c = d->text_[pos];
3083 if (isLineSeparatorChar(c))
3085 Inset const * inset = getInset(pos);
3086 return inset && inset->isLineSeparator();
3090 bool Paragraph::isWordSeparator(pos_type pos) const
3094 if (Inset const * inset = getInset(pos))
3095 return !inset->isLetter();
3096 // if we have a hard hyphen (no en- or emdash) or apostrophe
3097 // we pass this to the spell checker
3098 // FIXME: this method is subject to change, visit
3099 // https://bugzilla.mozilla.org/show_bug.cgi?id=355178
3100 // to get an impression how complex this is.
3101 if (isHardHyphenOrApostrophe(pos))
3103 char_type const c = d->text_[pos];
3104 // We want to pass the escape chars to the spellchecker
3105 docstring const escape_chars = from_utf8(lyxrc.spellchecker_esc_chars);
3106 return !isLetterChar(c) && !isDigitASCII(c) && !contains(escape_chars, c);
3110 bool Paragraph::isHardHyphenOrApostrophe(pos_type pos) const
3112 pos_type const psize = size();
3115 char_type const c = d->text_[pos];
3116 if (c != '-' && c != '\'')
3118 int nextpos = pos + 1;
3119 int prevpos = pos > 0 ? pos - 1 : 0;
3120 if ((nextpos == psize || isSpace(nextpos))
3121 && (pos == 0 || isSpace(prevpos)))
3127 bool Paragraph::isSameSpellRange(pos_type pos1, pos_type pos2) const
3130 || d->speller_state_.getRange(pos1) == d->speller_state_.getRange(pos2);
3134 bool Paragraph::isChar(pos_type pos) const
3136 if (Inset const * inset = getInset(pos))
3137 return inset->isChar();
3138 char_type const c = d->text_[pos];
3139 return !isLetterChar(c) && !isDigitASCII(c) && !lyx::isSpace(c);
3143 bool Paragraph::isSpace(pos_type pos) const
3145 if (Inset const * inset = getInset(pos))
3146 return inset->isSpace();
3147 char_type const c = d->text_[pos];
3148 return lyx::isSpace(c);
3153 Paragraph::getParLanguage(BufferParams const & bparams) const
3156 return getFirstFontSettings(bparams).language();
3157 // FIXME: we should check the prev par as well (Lgb)
3158 return bparams.language;
3162 bool Paragraph::isRTL(BufferParams const & bparams) const
3164 return getParLanguage(bparams)->rightToLeft()
3165 && !inInset().getLayout().forceLTR();
3169 void Paragraph::changeLanguage(BufferParams const & bparams,
3170 Language const * from, Language const * to)
3172 // change language including dummy font change at the end
3173 for (pos_type i = 0; i <= size(); ++i) {
3174 Font font = getFontSettings(bparams, i);
3175 if (font.language() == from) {
3176 font.setLanguage(to);
3178 d->requestSpellCheck(i);
3184 bool Paragraph::isMultiLingual(BufferParams const & bparams) const
3186 Language const * doc_language = bparams.language;
3187 FontList::const_iterator cit = d->fontlist_.begin();
3188 FontList::const_iterator end = d->fontlist_.end();
3190 for (; cit != end; ++cit)
3191 if (cit->font().language() != ignore_language &&
3192 cit->font().language() != latex_language &&
3193 cit->font().language() != doc_language)
3199 void Paragraph::getLanguages(std::set<Language const *> & languages) const
3201 FontList::const_iterator cit = d->fontlist_.begin();
3202 FontList::const_iterator end = d->fontlist_.end();
3204 for (; cit != end; ++cit) {
3205 Language const * lang = cit->font().language();
3206 if (lang != ignore_language &&
3207 lang != latex_language)
3208 languages.insert(lang);
3213 docstring Paragraph::asString(int options) const
3215 return asString(0, size(), options);
3219 docstring Paragraph::asString(pos_type beg, pos_type end, int options, const OutputParams *runparams) const
3221 odocstringstream os;
3224 && options & AS_STR_LABEL
3225 && !d->params_.labelString().empty())
3226 os << d->params_.labelString() << ' ';
3228 for (pos_type i = beg; i < end; ++i) {
3229 if ((options & AS_STR_SKIPDELETE) && isDeleted(i))
3231 char_type const c = d->text_[i];
3232 if (isPrintable(c) || c == '\t'
3233 || (c == '\n' && (options & AS_STR_NEWLINES)))
3235 else if (c == META_INSET && (options & AS_STR_INSETS)) {
3236 if (c == META_INSET && (options & AS_STR_PLAINTEXT)) {
3237 LASSERT(runparams != 0, return docstring());
3238 getInset(i)->plaintext(os, *runparams);
3240 getInset(i)->toString(os);
3249 void Paragraph::forOutliner(docstring & os, size_t maxlen) const
3251 if (!d->params_.labelString().empty())
3252 os += d->params_.labelString() + ' ';
3253 for (pos_type i = 0; i < size() && os.length() < maxlen; ++i) {
3256 char_type const c = d->text_[i];
3259 else if (c == '\t' || c == '\n')
3261 else if (c == META_INSET)
3262 getInset(i)->forOutliner(os, maxlen);
3267 void Paragraph::setInsetOwner(Inset const * inset)
3269 d->inset_owner_ = inset;
3273 int Paragraph::id() const
3279 void Paragraph::setId(int id)
3285 Layout const & Paragraph::layout() const
3291 void Paragraph::setLayout(Layout const & layout)
3293 d->layout_ = &layout;
3297 void Paragraph::setDefaultLayout(DocumentClass const & tc)
3299 setLayout(tc.defaultLayout());
3303 void Paragraph::setPlainLayout(DocumentClass const & tc)
3305 setLayout(tc.plainLayout());
3309 void Paragraph::setPlainOrDefaultLayout(DocumentClass const & tclass)
3311 if (usePlainLayout())
3312 setPlainLayout(tclass);
3314 setDefaultLayout(tclass);
3318 Inset const & Paragraph::inInset() const
3320 LBUFERR(d->inset_owner_);
3321 return *d->inset_owner_;
3325 ParagraphParameters & Paragraph::params()
3331 ParagraphParameters const & Paragraph::params() const
3337 bool Paragraph::isFreeSpacing() const
3339 if (d->layout_->free_spacing)
3341 return d->inset_owner_ && d->inset_owner_->isFreeSpacing();
3345 bool Paragraph::allowEmpty() const
3347 if (d->layout_->keepempty)
3349 return d->inset_owner_ && d->inset_owner_->allowEmpty();
3353 bool Paragraph::brokenBiblio() const
3355 // there is a problem if there is no bibitem at position 0 or
3356 // if there is another bibitem in the paragraph.
3357 return d->layout_->labeltype == LABEL_BIBLIO
3358 && (d->insetlist_.find(BIBITEM_CODE) != 0
3359 || d->insetlist_.find(BIBITEM_CODE, 1) > 0);
3363 int Paragraph::fixBiblio(Buffer const & buffer)
3365 // FIXME: What about the case where paragraph is not BIBLIO
3366 // but there is an InsetBibitem?
3367 // FIXME: when there was already an inset at 0, the return value is 1,
3368 // which does not tell whether another inset has been remove; the
3369 // cursor cannot be correctly updated.
3371 if (d->layout_->labeltype != LABEL_BIBLIO)
3374 bool const track_changes = buffer.params().track_changes;
3375 int bibitem_pos = d->insetlist_.find(BIBITEM_CODE);
3376 bool const hasbibitem0 = bibitem_pos == 0;
3379 bibitem_pos = d->insetlist_.find(BIBITEM_CODE, 1);
3380 // There was an InsetBibitem at pos 0, and no other one => OK
3381 if (bibitem_pos == -1)
3383 // there is a bibitem at the 0 position, but since
3384 // there is a second one, we copy the second on the
3385 // first. We're assuming there are at most two of
3386 // these, which there should be.
3387 // FIXME: why does it make sense to do that rather
3388 // than keep the first? (JMarc)
3389 Inset * inset = releaseInset(bibitem_pos);
3390 d->insetlist_.begin()->inset = inset;
3391 return -bibitem_pos;
3394 // We need to create an inset at the beginning
3396 if (bibitem_pos > 0) {
3397 // there was one somewhere in the paragraph, let's move it
3398 inset = d->insetlist_.release(bibitem_pos);
3399 eraseChar(bibitem_pos, track_changes);
3402 inset = new InsetBibitem(const_cast<Buffer *>(&buffer),
3403 InsetCommandParams(BIBITEM_CODE));
3405 Font font(inherit_font, buffer.params().language);
3406 insertInset(0, inset, font, Change(track_changes ? Change::INSERTED
3407 : Change::UNCHANGED));
3413 void Paragraph::checkAuthors(AuthorList const & authorList)
3415 d->changes_.checkAuthors(authorList);
3419 bool Paragraph::isChanged(pos_type pos) const
3421 return lookupChange(pos).changed();
3425 bool Paragraph::isInserted(pos_type pos) const
3427 return lookupChange(pos).inserted();
3431 bool Paragraph::isDeleted(pos_type pos) const
3433 return lookupChange(pos).deleted();
3437 InsetList const & Paragraph::insetList() const
3439 return d->insetlist_;
3443 void Paragraph::setBuffer(Buffer & b)
3445 d->insetlist_.setBuffer(b);
3449 Inset * Paragraph::releaseInset(pos_type pos)
3451 Inset * inset = d->insetlist_.release(pos);
3452 /// does not honour change tracking!
3453 eraseChar(pos, false);
3458 Inset * Paragraph::getInset(pos_type pos)
3460 return (pos < pos_type(d->text_.size()) && d->text_[pos] == META_INSET)
3461 ? d->insetlist_.get(pos) : 0;
3465 Inset const * Paragraph::getInset(pos_type pos) const
3467 return (pos < pos_type(d->text_.size()) && d->text_[pos] == META_INSET)
3468 ? d->insetlist_.get(pos) : 0;
3472 void Paragraph::changeCase(BufferParams const & bparams, pos_type pos,
3473 pos_type & right, TextCase action)
3475 // process sequences of modified characters; in change
3476 // tracking mode, this approach results in much better
3477 // usability than changing case on a char-by-char basis
3478 // We also need to track the current font, since font
3479 // changes within sequences can occur.
3480 vector<pair<char_type, Font> > changes;
3482 bool const trackChanges = bparams.track_changes;
3484 bool capitalize = true;
3486 for (; pos < right; ++pos) {
3487 char_type oldChar = d->text_[pos];
3488 char_type newChar = oldChar;
3490 // ignore insets and don't play with deleted text!
3491 if (oldChar != META_INSET && !isDeleted(pos)) {
3493 case text_lowercase:
3494 newChar = lowercase(oldChar);
3496 case text_capitalization:
3498 newChar = uppercase(oldChar);
3502 case text_uppercase:
3503 newChar = uppercase(oldChar);
3508 if (isWordSeparator(pos) || isDeleted(pos)) {
3509 // permit capitalization again
3513 if (oldChar != newChar) {
3514 changes.push_back(make_pair(newChar, getFontSettings(bparams, pos)));
3515 if (pos != right - 1)
3517 // step behind the changing area
3521 int erasePos = pos - changes.size();
3522 for (size_t i = 0; i < changes.size(); i++) {
3523 insertChar(pos, changes[i].first,
3526 if (!eraseChar(erasePos, trackChanges)) {
3529 ++right; // expand selection
3537 int Paragraph::find(docstring const & str, bool cs, bool mw,
3538 pos_type start_pos, bool del) const
3540 pos_type pos = start_pos;
3541 int const strsize = str.length();
3543 pos_type const parsize = d->text_.size();
3544 for (i = 0; i < strsize && pos < parsize; ++i, ++pos) {
3545 // Ignore "invisible" letters such as ligature breaks
3546 // and hyphenation chars while searching
3547 while (pos < parsize - 1 && isInset(pos)) {
3548 odocstringstream os;
3549 getInset(pos)->toString(os);
3550 if (!getInset(pos)->isLetter() || !os.str().empty())
3554 if (cs && str[i] != d->text_[pos])
3556 if (!cs && uppercase(str[i]) != uppercase(d->text_[pos]))
3558 if (!del && isDeleted(pos))
3565 // if necessary, check whether string matches word
3567 if (start_pos > 0 && !isWordSeparator(start_pos - 1))
3570 && !isWordSeparator(pos))
3574 return pos - start_pos;
3578 char_type Paragraph::getChar(pos_type pos) const
3580 return d->text_[pos];
3584 pos_type Paragraph::size() const
3586 return d->text_.size();
3590 bool Paragraph::empty() const
3592 return d->text_.empty();
3596 bool Paragraph::isInset(pos_type pos) const
3598 return d->text_[pos] == META_INSET;
3602 bool Paragraph::isSeparator(pos_type pos) const
3604 //FIXME: Are we sure this can be the only separator?
3605 return d->text_[pos] == ' ';
3609 void Paragraph::deregisterWords()
3611 Private::LangWordsMap::const_iterator itl = d->words_.begin();
3612 Private::LangWordsMap::const_iterator ite = d->words_.end();
3613 for (; itl != ite; ++itl) {
3614 WordList * wl = theWordList(itl->first);
3615 Private::Words::const_iterator it = (itl->second).begin();
3616 Private::Words::const_iterator et = (itl->second).end();
3617 for (; it != et; ++it)
3624 void Paragraph::locateWord(pos_type & from, pos_type & to,
3625 word_location const loc) const
3628 case WHOLE_WORD_STRICT:
3629 if (from == 0 || from == size()
3630 || isWordSeparator(from)
3631 || isWordSeparator(from - 1)) {
3635 // no break here, we go to the next
3638 // If we are already at the beginning of a word, do nothing
3639 if (!from || isWordSeparator(from - 1))
3641 // no break here, we go to the next
3644 // always move the cursor to the beginning of previous word
3645 while (from && !isWordSeparator(from - 1))
3649 LYXERR0("Paragraph::locateWord: NEXT_WORD not implemented yet");
3652 // no need to move the 'from' cursor
3656 while (to < size() && !isWordSeparator(to))
3661 void Paragraph::collectWords()
3663 for (pos_type pos = 0; pos < size(); ++pos) {
3664 if (isWordSeparator(pos))
3666 pos_type from = pos;
3667 locateWord(from, pos, WHOLE_WORD);
3668 // Work around MSVC warning: The statement
3669 // if (pos < from + lyxrc.completion_minlength)
3670 // triggers a signed vs. unsigned warning.
3671 // I don't know why this happens, it could be a MSVC bug, or
3672 // related to LLP64 (windows) vs. LP64 (unix) programming
3673 // model, or the C++ standard might be ambigous in the section
3674 // defining the "usual arithmetic conversions". However, using
3675 // a temporary variable is safe and works on all compilers.
3676 pos_type const endpos = from + lyxrc.completion_minlength;
3679 FontList::const_iterator cit = d->fontlist_.fontIterator(from);
3680 if (cit == d->fontlist_.end())
3682 Language const * lang = cit->font().language();
3683 docstring const word = asString(from, pos, AS_STR_NONE);
3684 d->words_[lang->lang()].insert(word);
3689 void Paragraph::registerWords()
3691 Private::LangWordsMap::const_iterator itl = d->words_.begin();
3692 Private::LangWordsMap::const_iterator ite = d->words_.end();
3693 for (; itl != ite; ++itl) {
3694 WordList * wl = theWordList(itl->first);
3695 Private::Words::const_iterator it = (itl->second).begin();
3696 Private::Words::const_iterator et = (itl->second).end();
3697 for (; it != et; ++it)
3703 void Paragraph::updateWords()
3711 void Paragraph::Private::appendSkipPosition(SkipPositions & skips, pos_type const pos) const
3713 SkipPositionsIterator begin = skips.begin();
3714 SkipPositions::iterator end = skips.end();
3715 if (pos > 0 && begin < end) {
3717 if (end->last == pos - 1) {
3722 skips.insert(end, FontSpan(pos, pos));
3726 Language * Paragraph::Private::locateSpellRange(
3727 pos_type & from, pos_type & to,
3728 SkipPositions & skips) const
3730 // skip leading white space
3731 while (from < to && owner_->isWordSeparator(from))
3733 // don't check empty range
3736 // get current language
3737 Language * lang = getSpellLanguage(from);
3738 pos_type last = from;
3739 bool samelang = true;
3740 bool sameinset = true;
3741 while (last < to && samelang && sameinset) {
3742 // hop to end of word
3743 while (last < to && !owner_->isWordSeparator(last)) {
3744 if (owner_->getInset(last)) {
3745 appendSkipPosition(skips, last);
3746 } else if (owner_->isDeleted(last)) {
3747 appendSkipPosition(skips, last);
3751 // hop to next word while checking for insets
3752 while (sameinset && last < to && owner_->isWordSeparator(last)) {
3753 if (Inset const * inset = owner_->getInset(last))
3754 sameinset = inset->isChar() && inset->isLetter();
3755 if (sameinset && owner_->isDeleted(last)) {
3756 appendSkipPosition(skips, last);
3761 if (sameinset && last < to) {
3762 // now check for language change
3763 samelang = lang == getSpellLanguage(last);
3766 // if language change detected backstep is needed
3774 Language * Paragraph::Private::getSpellLanguage(pos_type const from) const
3777 const_cast<Language *>(owner_->getFontSettings(
3778 inset_owner_->buffer().params(), from).language());
3779 if (lang == inset_owner_->buffer().params().language
3780 && !lyxrc.spellchecker_alt_lang.empty()) {
3782 string const lang_variety =
3783 split(lyxrc.spellchecker_alt_lang, lang_code, '-');
3784 lang->setCode(lang_code);
3785 lang->setVariety(lang_variety);
3791 void Paragraph::requestSpellCheck(pos_type pos)
3793 d->requestSpellCheck(pos);
3797 bool Paragraph::needsSpellCheck() const
3799 SpellChecker::ChangeNumber speller_change_number = 0;
3800 if (theSpellChecker())
3801 speller_change_number = theSpellChecker()->changeNumber();
3802 if (speller_change_number > d->speller_state_.currentChangeNumber()) {
3803 d->speller_state_.needsCompleteRefresh(speller_change_number);
3805 return d->needsSpellCheck();
3809 bool Paragraph::Private::ignoreWord(docstring const & word) const
3811 // Ignore words with digits
3812 // FIXME: make this customizable
3813 // (note that some checkers ignore words with digits by default)
3814 docstring::const_iterator cit = word.begin();
3815 docstring::const_iterator const end = word.end();
3816 for (; cit != end; ++cit) {
3817 if (isNumber((*cit)))
3824 SpellChecker::Result Paragraph::spellCheck(pos_type & from, pos_type & to,
3825 WordLangTuple & wl, docstring_list & suggestions,
3826 bool do_suggestion, bool check_learned) const
3828 SpellChecker::Result result = SpellChecker::WORD_OK;
3829 SpellChecker * speller = theSpellChecker();
3833 if (!d->layout_->spellcheck || !inInset().allowSpellCheck())
3836 locateWord(from, to, WHOLE_WORD);
3837 if (from == to || from >= size())
3840 docstring word = asString(from, to, AS_STR_INSETS | AS_STR_SKIPDELETE);
3841 Language * lang = d->getSpellLanguage(from);
3843 wl = WordLangTuple(word, lang);
3848 if (needsSpellCheck() || check_learned) {
3850 if (!d->ignoreWord(word)) {
3851 bool const trailing_dot = to < size() && d->text_[to] == '.';
3852 result = speller->check(wl);
3853 if (SpellChecker::misspelled(result) && trailing_dot) {
3854 wl = WordLangTuple(word.append(from_ascii(".")), lang);
3855 result = speller->check(wl);
3856 if (!SpellChecker::misspelled(result)) {
3857 LYXERR(Debug::GUI, "misspelled word is correct with dot: \"" <<
3859 from << ".." << to << "]");
3861 // spell check with dot appended failed too
3862 // restore original word/lang value
3863 word = asString(from, to, AS_STR_INSETS | AS_STR_SKIPDELETE);
3864 wl = WordLangTuple(word, lang);
3868 if (!SpellChecker::misspelled(result)) {
3869 // area up to the begin of the next word is not misspelled
3870 while (end < size() && isWordSeparator(end))
3873 d->setMisspelled(from, end, result);
3875 result = d->speller_state_.getState(from);
3879 suggestions.clear();
3881 if (SpellChecker::misspelled(result)) {
3882 LYXERR(Debug::GUI, "misspelled word: \"" <<
3884 from << ".." << to << "]");
3886 speller->suggest(wl, suggestions);
3892 void Paragraph::Private::markMisspelledWords(
3893 pos_type const & first, pos_type const & last,
3894 SpellChecker::Result result,
3895 docstring const & word,
3896 SkipPositions const & skips)
3898 if (!SpellChecker::misspelled(result)) {
3899 setMisspelled(first, last, SpellChecker::WORD_OK);
3903 SpellChecker * speller = theSpellChecker();
3904 // locate and enumerate the error positions
3905 int nerrors = speller->numMisspelledWords();
3907 SkipPositionsIterator it = skips.begin();
3908 SkipPositionsIterator et = skips.end();
3909 for (int index = 0; index < nerrors; ++index) {
3912 speller->misspelledWord(index, wstart, wlen);
3913 /// should not happen if speller supports range checks
3914 if (!wlen) continue;
3915 docstring const misspelled = word.substr(wstart, wlen);
3916 wstart += first + numskipped;
3917 if (snext < wstart) {
3918 /// mark the range of correct spelling
3919 numskipped += countSkips(it, et, wstart);
3920 setMisspelled(snext,
3921 wstart - 1, SpellChecker::WORD_OK);
3923 snext = wstart + wlen;
3924 numskipped += countSkips(it, et, snext);
3925 /// mark the range of misspelling
3926 setMisspelled(wstart, snext, result);
3927 LYXERR(Debug::GUI, "misspelled word: \"" <<
3928 misspelled << "\" [" <<
3929 wstart << ".." << (snext-1) << "]");
3932 if (snext <= last) {
3933 /// mark the range of correct spelling at end
3934 setMisspelled(snext, last, SpellChecker::WORD_OK);
3939 void Paragraph::spellCheck() const
3941 SpellChecker * speller = theSpellChecker();
3942 if (!speller || empty() ||!needsSpellCheck())
3946 d->rangeOfSpellCheck(start, endpos);
3947 if (speller->canCheckParagraph()) {
3948 // loop until we leave the range
3949 for (pos_type first = start; first < endpos; ) {
3950 pos_type last = endpos;
3951 Private::SkipPositions skips;
3952 Language * lang = d->locateSpellRange(first, last, skips);
3953 if (first >= endpos)
3955 // start the spell checker on the unit of meaning
3956 docstring word = asString(first, last, AS_STR_INSETS + AS_STR_SKIPDELETE);
3957 WordLangTuple wl = WordLangTuple(word, lang);
3958 SpellChecker::Result result = word.size() ?
3959 speller->check(wl) : SpellChecker::WORD_OK;
3960 d->markMisspelledWords(first, last, result, word, skips);
3964 static docstring_list suggestions;
3965 pos_type to = endpos;
3966 while (start < endpos) {
3968 spellCheck(start, to, wl, suggestions, false);
3972 d->readySpellCheck();
3976 bool Paragraph::isMisspelled(pos_type pos, bool check_boundary) const
3978 bool result = SpellChecker::misspelled(d->speller_state_.getState(pos));
3979 if (result || pos <= 0 || pos > size())
3981 if (check_boundary && (pos == size() || isWordSeparator(pos)))
3982 result = SpellChecker::misspelled(d->speller_state_.getState(pos - 1));
3987 string Paragraph::magicLabel() const
3990 ss << "magicparlabel-" << id();