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"
27 # include "aspell_local.h"
31 #include "support/tostr.h"
33 #include "frontends/Alert.h"
35 using namespace lyx::support;
40 ControlSpellchecker::ControlSpellchecker(LyXView & lv, Dialogs & d)
41 : ControlDialogBD(lv, d),
42 newval_(0.0), oldval_(0), newvalue_(0), count_(0)
46 ControlSpellchecker::~ControlSpellchecker()
50 void ControlSpellchecker::setParams()
52 lyxerr[Debug::GUI] << "spell setParams" << endl;
57 void ControlSpellchecker::clearParams()
59 lyxerr[Debug::GUI] << "spell clearParams" << endl;
66 SpellBase * getSpeller(BufferParams const & bp)
68 string lang = (lyxrc.isp_use_alt_lang)
70 : bp.language->code();
73 if (lyxrc.use_spell_lib)
74 return new ASpell(bp, lang);
77 if (lyxrc.use_spell_lib)
78 return new PSpell(bp, lang);
81 lang = (lyxrc.isp_use_alt_lang) ?
82 lyxrc.isp_alt_lang : bp.language->lang();
84 return new ISpell(bp, lang);
90 void ControlSpellchecker::startSession()
92 lyxerr[Debug::GUI] << "spell startSession" << endl;
95 lyxerr[Debug::GUI] << "startSession: speller exists" << endl;
100 speller_.reset(getSpeller(buffer()->params));
102 // reset values to initial
107 emergency_exit_ = false;
109 // start off the check
110 if (speller_->error().empty()) {
115 emergency_exit_ = true;
116 string message = speller_->error();
118 message = _("The spell-checker could not be started.\n"
119 "Maybe it is mis-configured.");
121 Alert::error(_("The spell-checker has failed"), message);
126 void ControlSpellchecker::endSession()
128 lyxerr[Debug::GUI] << "spell endSession" << endl;
130 bufferview()->endOfSpellCheck();
132 emergency_exit_ = true;
134 if (!speller_.get()) {
135 lyxerr[Debug::GUI] << "endSession with no speller" << endl;
143 void ControlSpellchecker::check()
145 lyxerr[Debug::GUI] << "spell check a word" << endl;
147 SpellBase::Result res = SpellBase::OK;
149 // clear any old selection
150 bufferview()->update();
152 while (res == SpellBase::OK || res == SpellBase::IGNORE) {
153 word_ = bufferview()->nextWord(newval_);
156 if (word_.word().empty())
161 // Update slider if and only if value has changed
162 newvalue_ = int(100.0 * newval_);
163 if (newvalue_!= oldval_) {
164 lyxerr[Debug::GUI] << "Updating spell progress." << endl;
167 view().partialUpdate(SPELL_PROGRESSED);
170 // speller might be dead ...
174 res = speller_->check(word_);
176 // ... or it might just be reporting an error
181 lyxerr[Debug::GUI] << "Found word \"" << word_.word() << "\"" << endl;
182 lyxerr << "Found word \"" << word_.word() << "\"" << endl;
184 if (!word_.word().empty()) {
185 bufferview()->selectLastWord();
186 bufferview()->fitCursor();
194 if (res != SpellBase::OK && res != SpellBase::IGNORE) {
195 lyxerr[Debug::GUI] << "Found a word needing checking." << endl;
196 view().partialUpdate(SPELL_FOUND_WORD);
201 bool ControlSpellchecker::checkAlive()
203 if (speller_->alive() && speller_->error().empty())
206 string message = speller_->error();
208 message = _("The spell-checker has died for some reason.\n"
209 "Maybe it has been killed.");
214 Alert::error(_("The spell-checker has failed"), message);
219 void ControlSpellchecker::showSummary()
221 if (!checkAlive() || count_ == 0) {
228 message = bformat(_("%1$s words checked."), tostr(count_));
230 message = _("One word checked.");
233 Alert::information(_("Spell-checking is complete"), message);
237 void ControlSpellchecker::replace(string const & replacement)
239 bufferview()->replaceWord(replacement);
246 void ControlSpellchecker::replaceAll(string const & replacement)
249 replace(replacement);
253 void ControlSpellchecker::insert()
255 speller_->insert(word_);
260 string const ControlSpellchecker::getSuggestion() const
262 return speller_->nextMiss();
266 string const ControlSpellchecker::getWord() const
272 void ControlSpellchecker::ignoreAll()
274 speller_->accept(word_);