]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/controllers/ControlSpellchecker.C
fix crash due to invalidated iterator
[lyx.git] / src / frontends / controllers / ControlSpellchecker.C
index afa264cb06df53e1dfcb7bafc518ad7efda99904..7e0c88d281d247a926c71ca8c48e638e65ac2387 100644 (file)
 #include <config.h>
 
 #include "ControlSpellchecker.h"
-#include "ViewBase.h"
 
 #include "buffer.h"
 #include "bufferparams.h"
 #include "BufferView.h"
+#include "cursor.h"
+#include "CutAndPaste.h"
 #include "debug.h"
 #include "gettext.h"
 #include "language.h"
 #include "lyxrc.h"
+#include "paragraph.h"
 
-#include "ispell.h"
-#ifdef USE_PSPELL
-# include "pspell.h"
-#else
-#ifdef USE_ASPELL
+#if defined(USE_ASPELL)
 # include "aspell_local.h"
+#elif defined(USE_PSPELL)
+# include "pspell.h"
 #endif
+
+#if defined(USE_ISPELL)
+# include "ispell.h"
+#else
+# include "SpellBase.h"
 #endif
 
-#include "support/tostr.h"
+#include "support/textutils.h"
+#include "support/convert.h"
 
 #include "frontends/Alert.h"
 
-using lyx::support::bformat;
-
+using std::advance;
+using std::distance;
 using std::endl;
+using std::string;
 
+namespace lyx {
 
-ControlSpellchecker::ControlSpellchecker(LyXView & lv, Dialogs & d)
-       : ControlDialogBD(lv, d),
-         newval_(0.0), oldval_(0), newvalue_(0), count_(0)
-{}
-
+using support::bformat;
+using support::contains;
 
-ControlSpellchecker::~ControlSpellchecker()
-{}
+namespace frontend {
 
 
-void ControlSpellchecker::setParams()
-{
-       lyxerr[Debug::GUI] << "spell setParams" << endl;
-       startSession();
-}
+ControlSpellchecker::ControlSpellchecker(Dialog & parent)
+       : Dialog::Controller(parent), exitEarly_(false),
+         oldval_(0), newvalue_(0), count_(0)
+{}
 
 
-void ControlSpellchecker::clearParams()
-{
-       lyxerr[Debug::GUI] << "spell clearParams" << endl;
-       endSession();
-}
+ControlSpellchecker::~ControlSpellchecker()
+{}
 
 
 namespace {
@@ -71,102 +71,160 @@ SpellBase * getSpeller(BufferParams const & bp)
                      ? lyxrc.isp_alt_lang
                      : bp.language->code();
 
-#ifdef USE_ASPELL
+#if defined(USE_ASPELL)
        if (lyxrc.use_spell_lib)
                return new ASpell(bp, lang);
-#endif
-#ifdef USE_PSPELL
+#elif defined(USE_PSPELL)
        if (lyxrc.use_spell_lib)
                return new PSpell(bp, lang);
 #endif
 
+#if defined(USE_ISPELL)
        lang = (lyxrc.isp_use_alt_lang) ?
                lyxrc.isp_alt_lang : bp.language->lang();
 
        return new ISpell(bp, lang);
+#else
+       return new SpellBase;
+#endif
 }
 
-}
+} // namespace anon
 
 
-void ControlSpellchecker::startSession()
+bool ControlSpellchecker::initialiseParams(std::string const &)
 {
-       lyxerr[Debug::GUI] << "spell startSession" << endl;
-
-       if (speller_.get()) {
-               lyxerr[Debug::GUI] << "startSession: speller exists" << endl;
-               speller_.reset(0);
-               return;
-       }
+       lyxerr[Debug::GUI] << "Spellchecker::initialiseParams" << endl;
 
-       speller_.reset(getSpeller(buffer()->params()));
+       speller_.reset(getSpeller(kernel().buffer().params()));
+       if (!speller_.get())
+               return false;
 
        // reset values to initial
-       newval_ = 0.0;
        oldval_ = 0;
        newvalue_ = 0;
        count_ = 0;
-       emergency_exit_ = false;
 
-       // start off the check
-       if (speller_->error().empty()) {
-               check();
-               return;
+       bool const success = speller_->error().empty();
+
+       if (!success) {
+               Alert::error(_("Spellchecker error"),
+                            _("The spellchecker could not be started\n")
+                            + speller_->error());
+               speller_.reset(0);
        }
 
-       emergency_exit_ = true;
-       string message = speller_->error();
-       if (message.empty())
-               message = _("The spell-checker could not be started.\n"
-                        "Maybe it is mis-configured.");
+       return success;
+}
+
 
-       Alert::error(_("The spell-checker has failed"), message);
+void ControlSpellchecker::clearParams()
+{
+       lyxerr[Debug::GUI] << "Spellchecker::clearParams" << endl;
        speller_.reset(0);
 }
 
 
-void ControlSpellchecker::endSession()
+namespace {
+
+bool isLetter(DocIterator const & cur)
 {
-       lyxerr[Debug::GUI] << "spell endSession" << endl;
+       return cur.inTexted()
+               && cur.inset().allowSpellCheck()
+               && cur.pos() != cur.lastpos()
+               && (cur.paragraph().isLetter(cur.pos())
+                   // We want to pass the ' and escape chars to ispell
+                   || contains(lyxrc.isp_esc_chars + '\'',
+                               cur.paragraph().getChar(cur.pos())))
+               && !isDeletedText(cur.paragraph(), cur.pos());
+}
 
-       bufferview()->endOfSpellCheck();
 
-       emergency_exit_ = true;
+WordLangTuple nextWord(DocIterator & cur, ptrdiff_t & progress,
+       BufferParams & bp)
+{
+       bool inword = false;
+       bool ignoreword = false;
+       string word, lang_code;
+
+       while (cur.depth()) {
+               if (isLetter(cur)) {
+                       if (!inword) {
+                               inword = true;
+                               ignoreword = false;
+                               word.clear();
+                               lang_code = cur.paragraph().getFontSettings(bp, cur.pos()).language()->code();
+                       }
+                       // Insets like optional hyphens and ligature
+                       // break are part of a word.
+                       if (!cur.paragraph().isInset(cur.pos())) {
+                               Paragraph::value_type const c =
+                                       cur.paragraph().getChar(cur.pos());
+                               word += c;
+                               if (IsDigit(c))
+                                       ignoreword = true;
+                       }
+               } else { // !isLetter(cur)
+                       if (inword)
+                               if (!word.empty() && !ignoreword)
+                                       return WordLangTuple(word, lang_code);
+                               else
+                                       inword = false;
+               }
 
-       if (!speller_.get()) {
-               lyxerr[Debug::GUI] << "endSession with no speller" << endl;
-               return;
+               cur.forwardPos();
+               ++progress;
        }
 
-       speller_.reset(0);
+       return WordLangTuple(string(), string());
 }
 
+} // namespace anon
+
+
 
 void ControlSpellchecker::check()
 {
-       lyxerr[Debug::GUI] << "spell check a word" << endl;
+       lyxerr[Debug::GUI] << "Check the spelling of a word" << endl;
 
        SpellBase::Result res = SpellBase::OK;
 
-       // clear any old selection
-       bufferview()->update();
+       DocIterator cur = kernel().bufferview()->cursor();
+       while (cur && cur.pos() && isLetter(cur)) {
+               cur.backwardPos();
+       }
+
+       ptrdiff_t start = 0, total = 0;
+       DocIterator it = DocIterator(kernel().buffer().inset());
+       for (start = 0; it != cur; it.forwardPos())
+               ++start;
+
+       for (total = start; it; it.forwardPos())
+               ++total;
 
-       while (res == SpellBase::OK || res == SpellBase::IGNORE) {
-               word_ = bufferview()->nextWord(newval_);
+       BufferParams & bufferparams = kernel().buffer().params();
+       exitEarly_ = false;
+
+       while (res == SpellBase::OK || res == SpellBase::IGNORED_WORD) {
+               word_ = nextWord(cur, start, bufferparams);
 
                // end of document
-               if (word_.word().empty())
-                       break;
+               if (getWord().empty()) {
+                       showSummary();
+                       exitEarly_ = true;
+                       return;
+               }
 
                ++count_;
 
                // Update slider if and only if value has changed
-               newvalue_ = int(100.0 * newval_);
+               float progress = total ? float(start)/total : 1;
+               newvalue_ = int(100.0 * progress);
                if (newvalue_!= oldval_) {
                        lyxerr[Debug::GUI] << "Updating spell progress." << endl;
                        oldval_ = newvalue_;
                        // set progress bar
-                       view().partialUpdate(SPELL_PROGRESSED);
+                       dialog().view().partialUpdate(SPELL_PROGRESSED);
                }
 
                // speller might be dead ...
@@ -180,22 +238,19 @@ void ControlSpellchecker::check()
                        return;
        }
 
-       lyxerr[Debug::GUI] << "Found word \"" << word_.word() << "\"" << endl;
-       lyxerr << "Found word \"" << word_.word() << "\"" << endl;
+       lyxerr[Debug::GUI] << "Found word \"" << getWord() << "\"" << endl;
 
-       if (!word_.word().empty()) {
-               bufferview()->selectLastWord();
-               bufferview()->fitCursor();
-       } else {
-               showSummary();
-               endSession();
-               return;
-       }
+       int const size = getWord().size();
+       cur.pos() -= size;
+       kernel().bufferview()->putSelectionAt(cur, size, false);
+       // if we used a lfun like in find/replace, dispatch would do
+       // that for us
+       kernel().bufferview()->update();
 
        // set suggestions
-       if (res != SpellBase::OK && res != SpellBase::IGNORE) {
+       if (res != SpellBase::OK && res != SpellBase::IGNORED_WORD) {
                lyxerr[Debug::GUI] << "Found a word needing checking." << endl;
-               view().partialUpdate(SPELL_FOUND_WORD);
+               dialog().view().partialUpdate(SPELL_FOUND_WORD);
        }
 }
 
@@ -205,15 +260,17 @@ bool ControlSpellchecker::checkAlive()
        if (speller_->alive() && speller_->error().empty())
                return true;
 
-       string message = speller_->error();
-       if (message.empty())
-               message = _("The spell-checker has died for some reason.\n"
-                        "Maybe it has been killed.");
+       string message;
+       if (speller_->error().empty())
+               message = _("The spellchecker has died for some reason.\n"
+                           "Maybe it has been killed.");
+       else
+               message = _("The spellchecker has failed.\n") 
+                       + speller_->error();
 
-       view().hide();
-       speller_.reset(0);
+       dialog().CancelButton();
 
-       Alert::error(_("The spell-checker has failed"), message);
+       Alert::error(_("The spellchecker has failed"), message);
        return false;
 }
 
@@ -221,24 +278,29 @@ bool ControlSpellchecker::checkAlive()
 void ControlSpellchecker::showSummary()
 {
        if (!checkAlive() || count_ == 0) {
-               view().hide();
+               dialog().CancelButton();
                return;
        }
 
        string message;
        if (count_ != 1)
-               message = bformat(_("%1$s words checked."), tostr(count_));
+               message = bformat(_("%1$d words checked."), count_);
        else
                message = _("One word checked.");
 
-       view().hide();
-       Alert::information(_("Spell-checking is complete"), message);
+       dialog().CancelButton();
+       Alert::information(_("Spelling check completed"), message);
 }
 
 
 void ControlSpellchecker::replace(string const & replacement)
 {
-       bufferview()->replaceWord(replacement);
+       lyxerr << "ControlSpellchecker::replace("
+              << replacement << ")" << std::endl;
+       BufferView & bufferview = *kernel().bufferview();
+       cap::replaceWord(bufferview.cursor(), replacement);
+       kernel().buffer().markDirty();
+       bufferview.update();
        // fix up the count
        --count_;
        check();
@@ -276,3 +338,6 @@ void ControlSpellchecker::ignoreAll()
        speller_->accept(word_);
        check();
 }
+
+} // namespace frontend
+} // namespace lyx