]> git.lyx.org Git - lyx.git/blobdiff - src/Paragraph.cpp
Help avoiding shortcut clashes by discriminating strings.
[lyx.git] / src / Paragraph.cpp
index 87eb41897ecbe3bc3d39b0d4aaaf4b5229a6b689..8d7371ff5fa5ecc0dbd884cd849ec4be85dd85ec 100644 (file)
@@ -137,7 +137,30 @@ public:
 
        void setRange(FontSpan const fp, SpellChecker::Result state)
        {
-               eraseCoveredRanges(fp);
+               Ranges result;
+               RangesIterator et = ranges_.end();
+               RangesIterator it = ranges_.begin();
+               for (; it != et; ++it) {
+                       if (!it->covered(fp))
+                               result.push_back(SpellResultRange(it->range(), it->result()));
+                       else if (state == SpellChecker::WORD_OK) {
+                               // trim or split the current misspelled range
+                               // store misspelled ranges only
+                               FontSpan range = it->range();
+                               if (fp.first > range.first) {
+                                       // misspelled area in front of WORD_OK
+                                       range.last = fp.first - 1;
+                                       result.push_back(SpellResultRange(range, it->result()));
+                                       range = it->range();
+                               }
+                               if (fp.last < range.last) {
+                                       // misspelled area after WORD_OK range
+                                       range.first = fp.last + 1;
+                                       result.push_back(SpellResultRange(range, it->result()));
+                               }
+                       }
+               }
+               ranges_ = result;
                if (state != SpellChecker::WORD_OK)
                        ranges_.push_back(SpellResultRange(fp, state));
        }
@@ -175,6 +198,8 @@ public:
 
        FontSpan const & getRange(pos_type pos) const
        {
+               /// empty span to indicate mismatch
+               static FontSpan empty_;
                RangesIterator et = ranges_.end();
                RangesIterator it = ranges_.begin();
                for (; it != et; ++it) {
@@ -205,8 +230,10 @@ public:
                        if (pos > refresh_.last)
                                refresh_.last = pos;
                } else if (pos != -1) {
-                       refresh_.first = pos;
-                       refresh_.last = pos;
+                       // init request check for neighbour positions too
+                       refresh_.first = pos > 0 ? pos - 1 : 0;
+                       // no need for special end of paragraph check
+                       refresh_.last = pos + 1;
                }
                needs_refresh_ = pos != -1;
        }
@@ -227,22 +254,8 @@ private:
        bool needs_refresh_;
        /// spell state cache version number
        SpellChecker::ChangeNumber current_change_number_;
-       /// empty span to indicate mismatch for getRange()
-       FontSpan empty_;
 
 
-       void eraseCoveredRanges(FontSpan const fp)
-       {
-               Ranges result;
-               RangesIterator et = ranges_.end();
-               RangesIterator it = ranges_.begin();
-               for (; it != et; ++it) {
-                       if (!it->covered(fp))
-                               result.push_back(SpellResultRange(it->range(), it->result()));
-               }
-               ranges_ = result;
-       }
-
        void correctRangesAfterPos(pos_type pos, int offset)
        {
                RangesIterator et = ranges_.end();
@@ -275,14 +288,14 @@ public:
 
        /// Output the surrogate pair formed by \p c and \p next to \p os.
        /// \return the number of characters written.
-       int latexSurrogatePair(odocstream & os, char_type c, char_type next,
+       int latexSurrogatePair(otexstream & os, char_type c, char_type next,
                               OutputParams const &);
 
        /// Output a space in appropriate formatting (or a surrogate pair
        /// if the next character is a combining character).
        /// \return whether a surrogate pair was output.
        bool simpleTeXBlanks(OutputParams const &,
-                            odocstream &, TexRow & texrow,
+                            otexstream &, TexRow & texrow,
                             pos_type i,
                             unsigned int & column,
                             Font const & font,
@@ -291,20 +304,20 @@ public:
        /// Output consecutive unicode chars, belonging to the same script as
        /// specified by the latex macro \p ltx, to \p os starting from \p i.
        /// \return the number of characters written.
-       int writeScriptChars(odocstream & os, docstring const & ltx,
+       int writeScriptChars(otexstream & os, docstring const & ltx,
                           Change const &, Encoding const &, pos_type & i);
 
        /// This could go to ParagraphParameters if we want to.
-       int startTeXParParams(BufferParams const &, odocstream &, TexRow &,
+       int startTeXParParams(BufferParams const &, otexstream &, TexRow &,
                              OutputParams const &) const;
 
        /// This could go to ParagraphParameters if we want to.
-       int endTeXParParams(BufferParams const &, odocstream &, TexRow &,
+       int endTeXParParams(BufferParams const &, otexstream &, TexRow &,
                            OutputParams const &) const;
 
        ///
        void latexInset(BufferParams const &,
-                                  odocstream &,
+                                  otexstream &,
                                   TexRow & texrow, OutputParams &,
                                   Font & running_font,
                                   Font & basefont,
@@ -317,7 +330,7 @@ public:
 
        ///
        void latexSpecialChar(
-                                  odocstream & os,
+                                  otexstream & os,
                                   OutputParams const & runparams,
                                   Font const & running_font,
                                   Change const & running_change,
@@ -328,18 +341,18 @@ public:
        ///
        bool latexSpecialT1(
                char_type const c,
-               odocstream & os,
+               otexstream & os,
                pos_type i,
                unsigned int & column);
        ///
        bool latexSpecialTypewriter(
                char_type const c,
-               odocstream & os,
+               otexstream & os,
                pos_type i,
                unsigned int & column);
        ///
        bool latexSpecialPhrase(
-               odocstream & os,
+               otexstream & os,
                pos_type & i,
                unsigned int & column,
                OutputParams const & runparams);
@@ -378,12 +391,9 @@ public:
        {
                pos_type textsize = owner_->size();
                // check for sane arguments
-               if (to < from || from >= textsize)
+               if (to <= from || from >= textsize)
                        return;
-               FontSpan fp = FontSpan(from, to);
-               // don't mark end of paragraph
-               if (fp.last >= textsize)
-                       fp.last = textsize - 1;
+               FontSpan fp = FontSpan(from, to - 1);
                speller_state_.setRange(fp, state);
        }
 
@@ -827,7 +837,7 @@ int Paragraph::eraseChars(pos_type start, pos_type end, bool trackChanges)
 }
 
 
-int Paragraph::Private::latexSurrogatePair(odocstream & os, char_type c,
+int Paragraph::Private::latexSurrogatePair(otexstream & os, char_type c,
                char_type next, OutputParams const & runparams)
 {
        // Writing next here may circumvent a possible font change between
@@ -856,7 +866,7 @@ int Paragraph::Private::latexSurrogatePair(odocstream & os, char_type c,
 
 
 bool Paragraph::Private::simpleTeXBlanks(OutputParams const & runparams,
-                                      odocstream & os, TexRow & texrow,
+                                      otexstream & os, TexRow & texrow,
                                       pos_type i,
                                       unsigned int & column,
                                       Font const & font,
@@ -901,7 +911,7 @@ bool Paragraph::Private::simpleTeXBlanks(OutputParams const & runparams,
 }
 
 
-int Paragraph::Private::writeScriptChars(odocstream & os,
+int Paragraph::Private::writeScriptChars(otexstream & os,
                                         docstring const & ltx,
                                         Change const & runningChange,
                                         Encoding const & encoding,
@@ -995,7 +1005,7 @@ bool Paragraph::Private::isTextAt(string const & str, pos_type pos) const
 
 
 void Paragraph::Private::latexInset(BufferParams const & bparams,
-                                   odocstream & os,
+                                   otexstream & os,
                                    TexRow & texrow,
                                    OutputParams & runparams,
                                    Font & running_font,
@@ -1011,7 +1021,7 @@ void Paragraph::Private::latexInset(BufferParams const & bparams,
        LASSERT(inset, /**/);
 
        if (style.pass_thru) {
-               inset->plaintext(os, runparams);
+               inset->plaintext(os.os(), runparams);
                return;
        }
 
@@ -1056,7 +1066,7 @@ void Paragraph::Private::latexInset(BufferParams const & bparams,
        }
 
        bool close = false;
-       odocstream::pos_type const len = os.tellp();
+       odocstream::pos_type const len = os.os().tellp();
 
        if (inset->forceLTR()
            && running_font.isRightToLeft()
@@ -1128,7 +1138,7 @@ void Paragraph::Private::latexInset(BufferParams const & bparams,
                texrow.start(owner_->id(), i + 1);
                column = 0;
        } else {
-               column += (unsigned int)(os.tellp() - len);
+               column += (unsigned int)(os.os().tellp() - len);
        }
 
        if (owner_->isDeleted(i))
@@ -1136,14 +1146,13 @@ void Paragraph::Private::latexInset(BufferParams const & bparams,
 }
 
 
-void Paragraph::Private::latexSpecialChar(
-                                            odocstream & os,
-                                            OutputParams const & runparams,
-                                            Font const & running_font,
-                                            Change const & running_change,
-                                            Layout const & style,
-                                            pos_type & i,
-                                            unsigned int & column)
+void Paragraph::Private::latexSpecialChar(otexstream & os,
+                                         OutputParams const & runparams,
+                                         Font const & running_font,
+                                         Change const & running_change,
+                                         Layout const & style,
+                                         pos_type & i,
+                                         unsigned int & column)
 {
        char_type const c = text_[i];
 
@@ -1268,7 +1277,7 @@ void Paragraph::Private::latexSpecialChar(
 }
 
 
-bool Paragraph::Private::latexSpecialT1(char_type const c, odocstream & os,
+bool Paragraph::Private::latexSpecialT1(char_type const c, otexstream & os,
        pos_type i, unsigned int & column)
 {
        switch (c) {
@@ -1296,7 +1305,7 @@ bool Paragraph::Private::latexSpecialT1(char_type const c, odocstream & os,
 }
 
 
-bool Paragraph::Private::latexSpecialTypewriter(char_type const c, odocstream & os,
+bool Paragraph::Private::latexSpecialTypewriter(char_type const c, otexstream & os,
        pos_type i, unsigned int & column)
 {
        switch (c) {
@@ -1318,7 +1327,7 @@ bool Paragraph::Private::latexSpecialTypewriter(char_type const c, odocstream &
 }
 
 
-bool Paragraph::Private::latexSpecialPhrase(odocstream & os, pos_type & i,
+bool Paragraph::Private::latexSpecialPhrase(otexstream & os, pos_type & i,
        unsigned int & column, OutputParams const & runparams)
 {
        // FIXME: if we have "LaTeX" with a font
@@ -1354,20 +1363,21 @@ void Paragraph::Private::validate(LaTeXFeatures & features) const
                // output is wrong if this paragraph contains content
                // that needs to switch encoding.
                odocstringstream ods;
+               otexstream os(ods);
                if (is_command) {
                        ods << '\\' << from_ascii(layout_->latexname());
                        // we have to provide all the optional arguments here, even though
                        // the last one is the only one we care about.
                        // Separate handling of optional argument inset.
                        if (layout_->optargs != 0 || layout_->reqargs != 0)
-                               latexArgInsets(*owner_, ods, features.runparams(),
-                                                                                        layout_->reqargs, layout_->optargs);
+                               latexArgInsets(*owner_, os, features.runparams(),
+                                       layout_->reqargs, layout_->optargs);
                        else
-                               ods << from_ascii(layout_->latexparam());
+                               os << from_ascii(layout_->latexparam());
                }
                docstring::size_type const length = ods.str().length();
                // this will output "{" at the beginning, but not at the end
-               owner_->latex(bp, f, ods, tr, features.runparams(), 0, -1, true);
+               owner_->latex(bp, f, os, tr, features.runparams(), 0, -1, true);
                if (ods.str().length() > length) {
                        if (is_command)
                                ods << '}';
@@ -2057,6 +2067,11 @@ bool Paragraph::usePlainLayout() const
 }
 
 
+bool Paragraph::isPassThru() const
+{
+       return inInset().getLayout().isPassThru() || d->layout_->pass_thru;
+}
+
 namespace {
 
 // paragraphs inside floats need different alignment tags to avoid
@@ -2119,12 +2134,12 @@ void adjust_row_column(string const & str, TexRow & texrow, int & column)
 
 
 int Paragraph::Private::startTeXParParams(BufferParams const & bparams,
-                                odocstream & os, TexRow & texrow,
+                                otexstream & os, TexRow & texrow,
                                 OutputParams const & runparams) const
 {
        int column = 0;
 
-       if (params_.noindent()) {
+       if (params_.noindent() && !layout_->pass_thru) {
                os << "\\noindent ";
                column += 10;
        }
@@ -2194,7 +2209,7 @@ int Paragraph::Private::startTeXParParams(BufferParams const & bparams,
 
 
 int Paragraph::Private::endTeXParParams(BufferParams const & bparams,
-                              odocstream & os, TexRow & texrow,
+                              otexstream & os, TexRow & texrow,
                               OutputParams const & runparams) const
 {
        int column = 0;
@@ -2266,7 +2281,7 @@ int Paragraph::Private::endTeXParParams(BufferParams const & bparams,
 // This one spits out the text of the paragraph
 void Paragraph::latex(BufferParams const & bparams,
        Font const & outerfont,
-       odocstream & os, TexRow & texrow,
+       otexstream & os, TexRow & texrow,
        OutputParams const & runparams,
        int start_pos, int end_pos, bool force) const
 {
@@ -2418,8 +2433,9 @@ void Paragraph::latex(BufferParams const & bparams,
                if (!runparams.pass_thru && !style.pass_thru &&
                    runparams.encoding->package() != Encoding::none &&
                    font.language()->encoding()->package() != Encoding::none) {
-                       pair<bool, int> const enc_switch = switchEncoding(os, bparams,
-                                       runparams, *(font.language()->encoding()));
+                       pair<bool, int> const enc_switch =
+                               switchEncoding(os.os(), bparams, runparams,
+                                       *(font.language()->encoding()));
                        if (enc_switch.first) {
                                column += enc_switch.second;
                                runparams.encoding = font.language()->encoding();
@@ -2831,11 +2847,12 @@ bool Paragraph::isWordSeparator(pos_type pos) const
 {
        if (Inset const * inset = getInset(pos))
                return !inset->isLetter();
+       if (pos == size())
+               return true;
        char_type const c = d->text_[pos];
        // We want to pass the ' and escape chars to the spellchecker
        static docstring const quote = from_utf8(lyxrc.spellchecker_esc_chars + '\'');
-       return (!isLetterChar(c) && !isDigitASCII(c) && !contains(quote, c))
-               || pos == size();
+       return (!isLetterChar(c) && !isDigitASCII(c) && !contains(quote, c));
 }
 
 
@@ -2961,6 +2978,8 @@ docstring Paragraph::asString(pos_type beg, pos_type end, int options) const
 
 void Paragraph::forToc(docstring & os, size_t maxlen) const
 {
+       if (!d->params_.labelString().empty())
+               os += d->params_.labelString() + ' ';
        for (pos_type i = 0; i < size() && os.length() < maxlen; ++i) {
                if (isDeleted(i))
                        continue;
@@ -3634,6 +3653,11 @@ SpellChecker::Result Paragraph::spellCheck(pos_type & from, pos_type & to,
                                        LYXERR(Debug::GUI, "misspelled word is correct with dot: \"" <<
                                           word << "\" [" <<
                                           from << ".." << to << "]");
+                               } else {
+                                       // spell check with dot appended failed
+                                       // restore original word/lang value
+                                       word = asString(from, to, AS_STR_INSETS | AS_STR_SKIPDELETE);
+                                       wl = WordLangTuple(word, lang);
                                }
                        }
                }
@@ -3740,9 +3764,14 @@ void Paragraph::spellCheck() const
 }
 
 
-bool Paragraph::isMisspelled(pos_type pos) const
+bool Paragraph::isMisspelled(pos_type pos, bool check_boundary) const
 {
-       return SpellChecker::misspelled(d->speller_state_.getState(pos));
+       bool result = SpellChecker::misspelled(d->speller_state_.getState(pos));
+       if (result || pos <= 0 || pos >= size())
+               return result;
+       if (check_boundary && isWordSeparator(pos))
+               result = SpellChecker::misspelled(d->speller_state_.getState(pos - 1));
+       return result;
 }