2 * \file ControlSpellchecker.C
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
13 #include "ControlSpellchecker.h"
16 #include "BufferView.h"
28 # include "aspell_local.h"
32 #include "support/tostr.h"
34 #include "frontends/Alert.h"
36 using namespace lyx::support;
41 ControlSpellchecker::ControlSpellchecker(LyXView & lv, Dialogs & d)
42 : ControlDialogBD(lv, d),
43 newval_(0.0), oldval_(0), newvalue_(0), count_(0)
47 ControlSpellchecker::~ControlSpellchecker()
51 void ControlSpellchecker::setParams()
53 lyxerr[Debug::GUI] << "spell setParams" << endl;
58 void ControlSpellchecker::clearParams()
60 lyxerr[Debug::GUI] << "spell clearParams" << endl;
67 SpellBase * getSpeller(BufferParams const & bp)
69 string lang = (lyxrc.isp_use_alt_lang)
71 : bp.language->code();
74 if (lyxrc.use_spell_lib)
75 return new ASpell(bp, lang);
78 if (lyxrc.use_spell_lib)
79 return new PSpell(bp, lang);
82 lang = (lyxrc.isp_use_alt_lang) ?
83 lyxrc.isp_alt_lang : bp.language->lang();
85 return new ISpell(bp, lang);
91 void ControlSpellchecker::startSession()
93 lyxerr[Debug::GUI] << "spell startSession" << endl;
96 lyxerr[Debug::GUI] << "startSession: speller exists" << endl;
101 speller_.reset(getSpeller(buffer()->params));
103 // reset values to initial
108 emergency_exit_ = false;
110 // start off the check
111 if (speller_->error().empty()) {
116 emergency_exit_ = true;
117 string message = speller_->error();
119 message = _("The spell-checker could not be started.\n"
120 "Maybe it is mis-configured.");
122 Alert::error(_("The spell-checker has failed"), message);
127 void ControlSpellchecker::endSession()
129 lyxerr[Debug::GUI] << "spell endSession" << endl;
131 bufferview()->endOfSpellCheck();
133 emergency_exit_ = true;
135 if (!speller_.get()) {
136 lyxerr[Debug::GUI] << "endSession with no speller" << endl;
144 void ControlSpellchecker::check()
146 lyxerr[Debug::GUI] << "spell check a word" << endl;
148 SpellBase::Result res = SpellBase::OK;
150 // clear any old selection
151 bufferview()->update();
153 while (res == SpellBase::OK || res == SpellBase::IGNORE) {
154 word_ = bufferview()->nextWord(newval_);
157 if (word_.word().empty())
162 // Update slider if and only if value has changed
163 newvalue_ = int(100.0 * newval_);
164 if (newvalue_!= oldval_) {
165 lyxerr[Debug::GUI] << "Updating spell progress." << endl;
168 view().partialUpdate(SPELL_PROGRESSED);
171 // speller might be dead ...
175 res = speller_->check(word_);
177 // ... or it might just be reporting an error
182 lyxerr[Debug::GUI] << "Found word \"" << word_.word() << "\"" << endl;
183 lyxerr << "Found word \"" << word_.word() << "\"" << endl;
185 if (!word_.word().empty()) {
186 bufferview()->selectLastWord();
187 bufferview()->fitCursor();
195 if (res != SpellBase::OK && res != SpellBase::IGNORE) {
196 lyxerr[Debug::GUI] << "Found a word needing checking." << endl;
197 view().partialUpdate(SPELL_FOUND_WORD);
202 bool ControlSpellchecker::checkAlive()
204 if (speller_->alive() && speller_->error().empty())
207 string message = speller_->error();
209 message = _("The spell-checker has died for some reason.\n"
210 "Maybe it has been killed.");
215 Alert::error(_("The spell-checker has failed"), message);
220 void ControlSpellchecker::showSummary()
222 if (!checkAlive() || count_ == 0) {
229 message = bformat(_("%1$s words checked."), tostr(count_));
231 message = _("One word checked.");
234 Alert::information(_("Spell-checking is complete"), message);
238 void ControlSpellchecker::replace(string const & replacement)
240 bufferview()->replaceWord(replacement);
247 void ControlSpellchecker::replaceAll(string const & replacement)
250 replace(replacement);
254 void ControlSpellchecker::insert()
256 speller_->insert(word_);
261 string const ControlSpellchecker::getSuggestion() const
263 return speller_->nextMiss();
267 string const ControlSpellchecker::getWord() const
273 void ControlSpellchecker::ignoreAll()
275 speller_->accept(word_);