]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/controllers/ControlSpellchecker.C
Add a buffer_path arg to InsetGraphicsMailer's params2string, string2params.
[lyx.git] / src / frontends / controllers / ControlSpellchecker.C
index 595ef6f1bcd5c85334254c461e07fe750e114ad3..19c1acf035b26061234b2db86803c1b817fae122 100644 (file)
-/* This file is part of
- * ====================================================== 
- *
- *           LyX, The Document Processor
- *
- *           Copyright 2001 The LyX Team.
+/**
+ * \file ControlSpellchecker.C
+ * This file is part of LyX, the document processor.
+ * Licence details can be found in the file COPYING.
  *
- * ======================================================
+ * \author Edwin Leuven
  *
- * \file ControlSpellchecker.C
- * \author Edwin Leuven <leuven@fee.uva.nl>
+ * Full author contact details are available in file CREDITS
  */
 
 #include <config.h>
 
-#ifdef __GNUG__
-#pragma implementation
-#endif
-
-#include FORMS_H_LOCATION
-
-#ifdef HAVE_SYS_SELECT_H
-# ifdef HAVE_STRINGS_H
-   // <strings.h> is needed at least on AIX because FD_ZERO uses bzero().
-   // BUT we cannot include both string.h and strings.h on Irix 6.5 :(
-#  ifdef _AIX
-#   include <strings.h>
-#  endif
-# endif
-#include <sys/select.h>
-#endif
-
+#include "ControlSpellchecker.h"
+#include "ViewBase.h"
 #include "buffer.h"
-#include "lyxrc.h"
 #include "BufferView.h"
-#include "LyXView.h" 
 #include "gettext.h"
-#include "support/lstrings.h"
 #include "language.h"
+#include "lyxrc.h"
+#include "lyxtext.h"
+#include "debug.h"
 
-#include "ViewBase.h"
-#include "ButtonControllerBase.h"
-#include "ControlSpellchecker.h"
-#include "Dialogs.h"
-#include "Liason.h"
-
-# include "sp_ispell.h"
+#include "ispell.h"
 #ifdef USE_PSPELL
-# include "sp_pspell.h"
+# include "pspell.h"
+#else
+#ifdef USE_ASPELL
+# include "aspell_local.h"
+#endif
 #endif
 
-#include "debug.h"
+#include "support/tostr.h"
+
+#include "frontends/Alert.h"
+
+using namespace lyx::support;
+
+using std::endl;
 
-using SigC::slot;
 
 ControlSpellchecker::ControlSpellchecker(LyXView & lv, Dialogs & d)
-       : ControlDialog<ControlConnectBD>(lv, d)
-{
-       d_.showSpellchecker.connect(SigC::slot(this, &ControlSpellchecker::show));
+       : ControlDialogBD(lv, d),
+         newval_(0.0), oldval_(0), newvalue_(0), count_(0)
+{}
 
-       rtl_ = false;
-       word_ = "";
-       newval_ = 0.0;
-       oldval_ = 0;
-       newvalue_ = 0;
-       count_ = 0;
-       message_ = "";
-       stop_ = false;
-       result_ = SpellBase::ISP_UNKNOWN;
-       speller_ = 0;
-       
+
+ControlSpellchecker::~ControlSpellchecker()
+{}
+
+
+void ControlSpellchecker::setParams()
+{
+       lyxerr[Debug::GUI] << "spell setParams" << endl;
+       startSession();
 }
 
 
-ControlSpellchecker::~ControlSpellchecker()
+void ControlSpellchecker::clearParams()
 {
+       lyxerr[Debug::GUI] << "spell clearParams" << endl;
+       endSession();
 }
 
+namespace {
 
-void ControlSpellchecker::show()
+SpellBase * getSpeller(BufferParams const & bp)
 {
-       if (isBufferDependent() && !lv_.view()->available())
-               return;
+       string lang = (lyxrc.isp_use_alt_lang)
+                     ? lyxrc.isp_alt_lang
+                     : bp.language->code();
 
-       if (!speller_) {
-               // create spell object
-               string tmp;
-#ifdef USE_PSPELL
-               if (lyxrc.use_pspell) {
-                       tmp = (lyxrc.isp_use_alt_lang) ?
-                               lyxrc.isp_alt_lang : lv_.buffer()->params.language->code();
-                       
-                       speller_ = new PSpell(lv_.view()->buffer()->params, tmp);
-               } else {
+#ifdef USE_ASPELL
+       if (lyxrc.use_spell_lib)
+               return new ASpell(bp, lang);
 #endif
-                       tmp = (lyxrc.isp_use_alt_lang) ?
-                               lyxrc.isp_alt_lang : lv_.buffer()->params.language->lang();
-                       
-                       speller_ = new ISpell(lv_.view()->buffer()->params, tmp);
 #ifdef USE_PSPELL
-               }
+       if (lyxrc.use_spell_lib)
+               return new PSpell(bp, lang);
 #endif
-       
-               if (lyxrc.isp_use_alt_lang) {
-                       Language const * lang = languages.getLanguage(tmp);
-                       if (lang)
-                               rtl_ = lang->RightToLeft();
-               } else {
-                       rtl_ = lv_.buffer()->params.language->RightToLeft();
-               }
-               
-               if (speller_->error() != 0) {
-                       message_ = speller_->error();
-                       view().partialUpdate(2);
-                       hide();
-                       return;
-               }
+
+       lang = (lyxrc.isp_use_alt_lang) ?
+               lyxrc.isp_alt_lang : bp.language->lang();
+
+       return new ISpell(bp, lang);
+}
+
+}
+
+void ControlSpellchecker::startSession()
+{
+       lyxerr[Debug::GUI] << "spell startSession" << endl;
+
+       if (speller_.get()) {
+               lyxerr[Debug::GUI] << "startSession: speller exists" << endl;
+               speller_.reset(0);
+               return;
        }
 
-       bc().readOnly(isReadonly());
-       view().show();
+       speller_.reset(getSpeller(buffer()->params));
+
+       // 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;
+       }
+
+       emergency_exit_ = true;
+       string message = speller_->error();
+       if (message.empty())
+               message = _("The spell-checker could not be started.\n"
+                        "Maybe it is mis-configured.");
+
+       Alert::error(_("The spell-checker has failed"), message);
+       speller_.reset(0);
 }
 
 
-void ControlSpellchecker::hide()
+void ControlSpellchecker::endSession()
 {
-       delete speller_;
-       speller_ = 0;
+       lyxerr[Debug::GUI] << "spell endSession" << endl;
 
-       disconnect();
-       view().hide();
+       bufferview()->endOfSpellCheck();
+
+       emergency_exit_ = true;
+
+       if (!speller_.get()) {
+               lyxerr[Debug::GUI] << "endSession with no speller" << endl;
+               return;
+       }
+
+       speller_.reset(0);
 }
 
 
 void ControlSpellchecker::check()
 {
-       result_ = SpellBase::ISP_UNKNOWN;
-       stop_ = false;
-       
-       while (result_!=SpellBase::ISP_MISSED && !stop_) {
-               word_ = lv_.view()->nextWord(newval_);
-               if (word_.empty()) {
-                       quit();
+       lyxerr[Debug::GUI] << "spell check a word" << endl;
+
+       SpellBase::Result res = SpellBase::OK;
+
+       // clear any old selection
+       LyXText * text = bufferview()->getLyXText();
+       bufferview()->toggleSelection(true);
+       bufferview()->update(text, BufferView::SELECT);
+
+       while ((res == SpellBase::OK || res == SpellBase::IGNORE)) {
+               word_ = bufferview()->nextWord(newval_);
+
+               // end of document
+               if (word_.word().empty())
                        break;
-               }
-               
+
                ++count_;
-               
+
                // Update slider if and only if value has changed
-               newvalue_ = int(100.0*newval_);
+               newvalue_ = int(100.0 * newval_);
                if (newvalue_!= oldval_) {
+                       lyxerr[Debug::GUI] << "Updating spell progress." << endl;
                        oldval_ = newvalue_;
                        // set progress bar
-                       view().partialUpdate(0);
+                       view().partialUpdate(SPELL_PROGRESSED);
                }
-               
-               if (!speller_->alive()) quit();
-               
-               result_ = speller_->check(word_);
+
+               // speller might be dead ...
+               if (!checkAlive())
+                       return;
+
+               res = speller_->check(word_);
+
+               // ... or it might just be reporting an error
+               if (!checkAlive())
+                       return;
+       }
+
+       lyxerr[Debug::GUI] << "Found word \"" << word_.word() << "\"" << endl;
+
+       if (!word_.word().empty()) {
+               bufferview()->selectLastWord();
+       } else {
+               showSummary();
+               endSession();
+               return;
        }
-       
-       if (!stop_ && !word_.empty())
-               lv_.view()->selectLastWord();
 
        // set suggestions
-       if (result_==SpellBase::ISP_MISSED) {
-               view().partialUpdate(1);
+       if (res != SpellBase::OK && res != SpellBase::IGNORE) {
+               lyxerr[Debug::GUI] << "Found a word needing checking." << endl;
+               view().partialUpdate(SPELL_FOUND_WORD);
        }
 }
 
 
-void ControlSpellchecker::replace(string const & replacement)
+bool ControlSpellchecker::checkAlive()
 {
-       lv_.view()->replaceWord(replacement);
-       check();
-}
+       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.");
 
-void ControlSpellchecker::replaceAll(string const & replacement)
-{
-       // TODO: add to list
-       replace(replacement);
-}
-
+       view().hide();
+       speller_.reset(0);
 
-void ControlSpellchecker::insert()
-{
-       speller_->insert(word_);
+       Alert::error(_("The spell-checker has failed"), message);
+       return false;
 }
 
 
-string ControlSpellchecker::getSuggestion()
+void ControlSpellchecker::showSummary()
 {
-       // this is needed because string tmp = nextmiss()
-       // segfaults when nextMiss is 0
-       string tmp;
-       char const * w = speller_->nextMiss();
-       
-       if (w!=0) {
-               tmp = w;
-               if (rtl_) std::reverse(tmp.begin(), tmp.end());
+       if (!checkAlive() || count_ == 0) {
+               view().hide();
+               return;
        }
-       
-       return tmp;
-}
 
+       string message;
+       if (count_ != 1)
+               message = bformat(_("%1$s words checked."), tostr(count_));
+       else
+               message = _("One word checked.");
 
-string ControlSpellchecker::getWord()
-{
-       string tmp = word_;
-       if (rtl_) std::reverse(tmp.begin(), tmp.end());
-       return tmp;
+       view().hide();
+       Alert::information(_("Spell-checking is complete"), message);
 }
 
 
-void ControlSpellchecker::ignoreAll()
+void ControlSpellchecker::replace(string const & replacement)
 {
-       speller_->accept(word_);
+       bufferview()->replaceWord(replacement);
+       // fix up the count
+       --count_;
        check();
 }
 
 
-void ControlSpellchecker::stop()
+void ControlSpellchecker::replaceAll(string const & replacement)
 {
-       stop_ = true;
+       // TODO: add to list
+       replace(replacement);
 }
 
 
-void ControlSpellchecker::quit()
+void ControlSpellchecker::insert()
 {
-       if (speller_->alive()) {
-               speller_->close();
-               message_ = tostr(count_);
-               if (count_ != 1) {
-                       message_ += _(" words checked.");
-               } else {
-                       message_ += _(" word checked.");
-               }
-               message_ = "\n" + message_;
-               message_ = _("Spellchecking completed! ") + message_;
-       } else {
-               speller_->cleanUp();
-               message_ = _("The spell checker has died for some reason.\n"
-                                 "Maybe it has been killed.");
-       }
-
-       lv_.view()->endOfSpellCheck();
+       speller_->insert(word_);
+       check();
+}
 
-       // hide dialog, disconnect and delete speller
-       hide();
-       
-       // show closing message
-       view().partialUpdate(2);
 
-       // reset values to initial
-       rtl_ = false;
-       word_ = "";
-       newval_ = 0.0;
-       oldval_ = 0;
-       newvalue_ = 0;
-       count_ = 0;
-       message_ = "";
-       stop_ = false;
-       result_ = SpellBase::ISP_UNKNOWN;
+string const ControlSpellchecker::getSuggestion() const
+{
+       return speller_->nextMiss();
 }
 
 
-void ControlSpellchecker::options()
+string const ControlSpellchecker::getWord() const
 {
-       lv_.getDialogs()->showSpellcheckerPreferences();
+       return word_.word();
 }
 
 
+void ControlSpellchecker::ignoreAll()
+{
+       speller_->accept(word_);
+       check();
+}