/**
* \file ControlSpellchecker.C
- * Copyright 2001 the LyX Team
- * Read the file COPYING
+ * This file is part of LyX, the document processor.
+ * Licence details can be found in the file COPYING.
*
- * \author Edwin Leuven <leuven@fee.uva.nl>
+ * \author Edwin Leuven
+ *
+ * Full author contact details are available in file CREDITS.
*/
#include <config.h>
-#ifdef __GNUG__
-#pragma implementation
-#endif
-
#include "ControlSpellchecker.h"
+#include "ViewBase.h"
+
#include "buffer.h"
-#include "lyxrc.h"
+#include "bufferparams.h"
#include "BufferView.h"
-#include "lyxtext.h"
+#include "bufferview_funcs.h"
+#include "debug.h"
#include "gettext.h"
#include "language.h"
+#include "lyxrc.h"
-#include "ViewBase.h"
-#include "Dialogs.h"
-#include "Liason.h"
-
-#include "frontends/LyXView.h"
-#include "frontends/Alert.h"
-
-#include "support/lstrings.h"
+#include "PosIterator.h"
+#include "paragraph.h"
#include "ispell.h"
#ifdef USE_PSPELL
# include "pspell.h"
+#else
+#ifdef USE_ASPELL
+# include "aspell_local.h"
#endif
+#endif
+
+#include "support/tostr.h"
+
+#include "frontends/Alert.h"
+
+
+using lyx::support::bformat;
+
+using std::endl;
+using std::string;
+
ControlSpellchecker::ControlSpellchecker(LyXView & lv, Dialogs & d)
: ControlDialogBD(lv, d),
- rtl_(false), newval_(0.0), oldval_(0), newvalue_(0), count_(0),
- stop_(false), speller_(0)
+ oldval_(0), newvalue_(0), count_(0)
+{}
+
+
+ControlSpellchecker::~ControlSpellchecker()
{}
void ControlSpellchecker::setParams()
{
- 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();
+ lyxerr[Debug::GUI] << "spell setParams" << endl;
+ startSession();
+}
- speller_ = new PSpell(lv_.view()->buffer()->params, tmp);
- } else {
-#endif
- tmp = (lyxrc.isp_use_alt_lang) ?
- lyxrc.isp_alt_lang : lv_.buffer()->params.language->lang();
- speller_ = new ISpell(lv_.view()->buffer()->params, tmp);
+void ControlSpellchecker::clearParams()
+{
+ lyxerr[Debug::GUI] << "spell clearParams" << endl;
+ endSession();
+}
+
+
+namespace {
+
+
+SpellBase * getSpeller(BufferParams const & bp)
+{
+ string lang = (lyxrc.isp_use_alt_lang)
+ ? lyxrc.isp_alt_lang
+ : bp.language->code();
+
+#ifdef USE_ASPELL
+ if (lyxrc.use_spell_lib)
+ return new ASpell(bp, lang);
+#endif
#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();
- }
+ lang = (lyxrc.isp_use_alt_lang) ?
+ lyxrc.isp_alt_lang : bp.language->lang();
- if (!speller_->error().empty()) {
- emergency_exit_ = true;
- Alert::alert("The spellchecker has failed", speller_->error());
- clearParams();
- return;
- }
+ 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;
+ }
+
+ speller_.reset(getSpeller(buffer()->params()));
+
+ // reset values to initial
+ 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::endSession()
+{
+ lyxerr[Debug::GUI] << "spell endSession" << endl;
+
+ emergency_exit_ = true;
+
+ if (!speller_.get()) {
+ lyxerr[Debug::GUI] << "endSession with no speller" << endl;
+ return;
+ }
+
+ speller_.reset(0);
+}
+
+
+namespace {
+
+
+bool isLetter(PosIterator & cur)
+{
+ return !cur.at_end()
+ && cur.pit()->isLetter(cur.pos())
+ && !isDeletedText(*cur.pit(), cur.pos())
+ && (!cur.inset() || cur.inset()->allowSpellCheck());
+}
+
+
+WordLangTuple nextWord(PosIterator & cur, PosIterator const & end,
+ int & progress, BufferParams & bp)
+{
+ // skip until we have real text (will jump paragraphs)
+ for (; cur != end && !isLetter(cur); ++cur, ++progress);
+
+ if (cur == end)
+ return WordLangTuple(string(), string());
+
+ string lang_code = cur.pit()->getFontSettings(bp, cur.pos()).language()->code();
+ string str;
+ // and find the end of the word (insets like optional hyphens
+ // and ligature break are part of a word)
+ for (; cur != end && isLetter(cur); ++cur, ++progress) {
+ if (!cur.pit()->isInset(cur.pos()))
+ str += cur.pit()->getChar(cur.pos());
+ }
+
+ return WordLangTuple(str, lang_code);
+}
+
+
+} //namespace anon
+
+
+
+
void ControlSpellchecker::check()
{
+ lyxerr[Debug::GUI] << "spell check a word" << endl;
+
SpellBase::Result res = SpellBase::OK;
- stop_ = false;
- // clear any old selection
- LyXText * text = lv_.view()->getLyXText();
- lv_.view()->toggleSelection(true);
- lv_.view()->update(text, BufferView::SELECT);
+ PosIterator cur(*bufferview());
+ PosIterator const beg = buffer()->pos_iterator_begin();
+ PosIterator const end = buffer()->pos_iterator_end();
+
+ int start = std::distance(beg, cur);
+ int const total = start + std::distance(cur, end);
- while ((res == SpellBase::OK || res == SpellBase::IGNORE) && !stop_) {
- word_ = lv_.view()->nextWord(newval_);
+ if (cur != buffer()->pos_iterator_begin())
+ for (; cur != end && isLetter(cur); ++cur, ++start);
- if (word_.word().empty()) {
- clearParams();
+ while (res == SpellBase::OK || res == SpellBase::IGNORE) {
+ word_ = nextWord(cur, end, start, buffer()->params());
+
+ // end of document
+ if (word_.word().empty())
break;
- }
++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(0);
+ view().partialUpdate(SPELL_PROGRESSED);
}
- if (!speller_->alive()) {
- clearParams();
- stop();
+ // speller might be dead ...
+ if (!checkAlive())
return;
- }
res = speller_->check(word_);
+
+ // ... or it might just be reporting an error
+ if (!checkAlive())
+ return;
}
- if (!stop_ && !word_.word().empty())
- lv_.view()->selectLastWord();
+ lyxerr[Debug::GUI] << "Found word \"" << word_.word() << "\"" << endl;
+
+ if (!word_.word().empty()) {
+ int const size = word_.word().size();
+ std::advance(cur, -size);
+ bufferview()->putSelectionAt(cur, size, false);
+ std::advance(cur, size);
+ } else {
+ showSummary();
+ endSession();
+ return;
+ }
// set suggestions
if (res != SpellBase::OK && res != SpellBase::IGNORE) {
- view().partialUpdate(1);
+ lyxerr[Debug::GUI] << "Found a word needing checking." << endl;
+ view().partialUpdate(SPELL_FOUND_WORD);
}
}
+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.");
+
+ view().hide();
+ speller_.reset(0);
+
+ Alert::error(_("The spell-checker has failed"), message);
+ return false;
+}
+
+
+void ControlSpellchecker::showSummary()
+{
+ if (!checkAlive() || count_ == 0) {
+ view().hide();
+ return;
+ }
+
+ string message;
+ if (count_ != 1)
+ message = bformat(_("%1$s words checked."), tostr(count_));
+ else
+ message = _("One word checked.");
+
+ view().hide();
+ Alert::information(_("Spell-checking is complete"), message);
+}
+
+
void ControlSpellchecker::replace(string const & replacement)
{
- lv_.view()->replaceWord(replacement);
+ bufferview()->replaceWord(replacement);
// fix up the count
- --count_;
+ --count_;
check();
}
}
-string ControlSpellchecker::getSuggestion()
+string const ControlSpellchecker::getSuggestion() const
{
- string miss(speller_->nextMiss());
-
- if (rtl_)
- std::reverse(miss.begin(), miss.end());
-
- return miss;
+ return speller_->nextMiss();
}
-string ControlSpellchecker::getWord()
+string const ControlSpellchecker::getWord() const
{
- string tmp = word_.word();
- if (rtl_)
- std::reverse(tmp.begin(), tmp.end());
- return tmp;
+ return word_.word();
}
speller_->accept(word_);
check();
}
-
-
-void ControlSpellchecker::stop()
-{
- stop_ = true;
- lv_.view()->endOfSpellCheck();
-}
-
-
-void ControlSpellchecker::clearParams()
-{
- if (!speller_)
- return;
-
- 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 {
- message_ = speller_->error();
- speller_->cleanUp();
- if (message_.empty())
- message_ = _("The spell checker has died for some reason.\n"
- "Maybe it has been killed.");
-
- // make sure that the dialog is not launched
- emergency_exit_ = true;
- Alert::alert("The spellchecker has failed", message_);
- }
-
- delete speller_;
-
- lv_.view()->endOfSpellCheck();
-
- // show closing message if any words were checked.
- if (count_ > 0)
- view().partialUpdate(2);
-
- // reset values to initial
- rtl_ = false;
- newval_ = 0.0;
- oldval_ = 0;
- newvalue_ = 0;
- count_ = 0;
- message_.erase();
- stop_ = false;
- speller_ = 0;
-}