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 LyXText * text = bufferview()->getLyXText();
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_);