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"
17 #include "bufferparams.h"
18 #include "BufferView.h"
29 # include "aspell_local.h"
33 #include "support/tostr.h"
35 #include "frontends/Alert.h"
37 using lyx::support::bformat;
42 ControlSpellchecker::ControlSpellchecker(LyXView & lv, Dialogs & d)
43 : ControlDialogBD(lv, d),
44 newval_(0.0), oldval_(0), newvalue_(0), count_(0)
48 ControlSpellchecker::~ControlSpellchecker()
52 void ControlSpellchecker::setParams()
54 lyxerr[Debug::GUI] << "spell setParams" << endl;
59 void ControlSpellchecker::clearParams()
61 lyxerr[Debug::GUI] << "spell clearParams" << endl;
68 SpellBase * getSpeller(BufferParams const & bp)
70 string lang = (lyxrc.isp_use_alt_lang)
72 : bp.language->code();
75 if (lyxrc.use_spell_lib)
76 return new ASpell(bp, lang);
79 if (lyxrc.use_spell_lib)
80 return new PSpell(bp, lang);
83 lang = (lyxrc.isp_use_alt_lang) ?
84 lyxrc.isp_alt_lang : bp.language->lang();
86 return new ISpell(bp, lang);
92 void ControlSpellchecker::startSession()
94 lyxerr[Debug::GUI] << "spell startSession" << endl;
97 lyxerr[Debug::GUI] << "startSession: speller exists" << endl;
102 speller_.reset(getSpeller(buffer()->params()));
104 // reset values to initial
109 emergency_exit_ = false;
111 // start off the check
112 if (speller_->error().empty()) {
117 emergency_exit_ = true;
118 string message = speller_->error();
120 message = _("The spell-checker could not be started.\n"
121 "Maybe it is mis-configured.");
123 Alert::error(_("The spell-checker has failed"), message);
128 void ControlSpellchecker::endSession()
130 lyxerr[Debug::GUI] << "spell endSession" << endl;
132 bufferview()->endOfSpellCheck();
134 emergency_exit_ = true;
136 if (!speller_.get()) {
137 lyxerr[Debug::GUI] << "endSession with no speller" << endl;
145 void ControlSpellchecker::check()
147 lyxerr[Debug::GUI] << "spell check a word" << endl;
149 SpellBase::Result res = SpellBase::OK;
151 // clear any old selection
152 bufferview()->update();
154 while (res == SpellBase::OK || res == SpellBase::IGNORE) {
155 word_ = bufferview()->nextWord(newval_);
158 if (word_.word().empty())
163 // Update slider if and only if value has changed
164 newvalue_ = int(100.0 * newval_);
165 if (newvalue_!= oldval_) {
166 lyxerr[Debug::GUI] << "Updating spell progress." << endl;
169 view().partialUpdate(SPELL_PROGRESSED);
172 // speller might be dead ...
176 res = speller_->check(word_);
178 // ... or it might just be reporting an error
183 lyxerr[Debug::GUI] << "Found word \"" << word_.word() << "\"" << endl;
184 lyxerr << "Found word \"" << word_.word() << "\"" << endl;
186 if (!word_.word().empty()) {
187 bufferview()->selectLastWord();
188 bufferview()->fitCursor();
196 if (res != SpellBase::OK && res != SpellBase::IGNORE) {
197 lyxerr[Debug::GUI] << "Found a word needing checking." << endl;
198 view().partialUpdate(SPELL_FOUND_WORD);
203 bool ControlSpellchecker::checkAlive()
205 if (speller_->alive() && speller_->error().empty())
208 string message = speller_->error();
210 message = _("The spell-checker has died for some reason.\n"
211 "Maybe it has been killed.");
216 Alert::error(_("The spell-checker has failed"), message);
221 void ControlSpellchecker::showSummary()
223 if (!checkAlive() || count_ == 0) {
230 message = bformat(_("%1$s words checked."), tostr(count_));
232 message = _("One word checked.");
235 Alert::information(_("Spell-checking is complete"), message);
239 void ControlSpellchecker::replace(string const & replacement)
241 bufferview()->replaceWord(replacement);
248 void ControlSpellchecker::replaceAll(string const & replacement)
251 replace(replacement);
255 void ControlSpellchecker::insert()
257 speller_->insert(word_);
262 string const ControlSpellchecker::getSuggestion() const
264 return speller_->nextMiss();
268 string const ControlSpellchecker::getWord() const
274 void ControlSpellchecker::ignoreAll()
276 speller_->accept(word_);