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"
38 using lyx::support::bformat;
44 ControlSpellchecker::ControlSpellchecker(LyXView & lv, Dialogs & d)
45 : ControlDialogBD(lv, d),
46 newval_(0.0), oldval_(0), newvalue_(0), count_(0)
50 ControlSpellchecker::~ControlSpellchecker()
54 void ControlSpellchecker::setParams()
56 lyxerr[Debug::GUI] << "spell setParams" << endl;
61 void ControlSpellchecker::clearParams()
63 lyxerr[Debug::GUI] << "spell clearParams" << endl;
70 SpellBase * getSpeller(BufferParams const & bp)
72 string lang = (lyxrc.isp_use_alt_lang)
74 : bp.language->code();
77 if (lyxrc.use_spell_lib)
78 return new ASpell(bp, lang);
81 if (lyxrc.use_spell_lib)
82 return new PSpell(bp, lang);
85 lang = (lyxrc.isp_use_alt_lang) ?
86 lyxrc.isp_alt_lang : bp.language->lang();
88 return new ISpell(bp, lang);
94 void ControlSpellchecker::startSession()
96 lyxerr[Debug::GUI] << "spell startSession" << endl;
99 lyxerr[Debug::GUI] << "startSession: speller exists" << endl;
104 speller_.reset(getSpeller(buffer()->params()));
106 // reset values to initial
111 emergency_exit_ = false;
113 // start off the check
114 if (speller_->error().empty()) {
119 emergency_exit_ = true;
120 string message = speller_->error();
122 message = _("The spell-checker could not be started.\n"
123 "Maybe it is mis-configured.");
125 Alert::error(_("The spell-checker has failed"), message);
130 void ControlSpellchecker::endSession()
132 lyxerr[Debug::GUI] << "spell endSession" << endl;
134 bufferview()->endOfSpellCheck();
136 emergency_exit_ = true;
138 if (!speller_.get()) {
139 lyxerr[Debug::GUI] << "endSession with no speller" << endl;
147 void ControlSpellchecker::check()
149 lyxerr[Debug::GUI] << "spell check a word" << endl;
151 SpellBase::Result res = SpellBase::OK;
153 // clear any old selection
154 bufferview()->update();
156 while (res == SpellBase::OK || res == SpellBase::IGNORE) {
157 word_ = bufferview()->nextWord(newval_);
160 if (word_.word().empty())
165 // Update slider if and only if value has changed
166 newvalue_ = int(100.0 * newval_);
167 if (newvalue_!= oldval_) {
168 lyxerr[Debug::GUI] << "Updating spell progress." << endl;
171 view().partialUpdate(SPELL_PROGRESSED);
174 // speller might be dead ...
178 res = speller_->check(word_);
180 // ... or it might just be reporting an error
185 lyxerr[Debug::GUI] << "Found word \"" << word_.word() << "\"" << endl;
186 lyxerr << "Found word \"" << word_.word() << "\"" << endl;
188 if (!word_.word().empty()) {
189 bufferview()->selectLastWord();
190 bufferview()->fitCursor();
198 if (res != SpellBase::OK && res != SpellBase::IGNORE) {
199 lyxerr[Debug::GUI] << "Found a word needing checking." << endl;
200 view().partialUpdate(SPELL_FOUND_WORD);
205 bool ControlSpellchecker::checkAlive()
207 if (speller_->alive() && speller_->error().empty())
210 string message = speller_->error();
212 message = _("The spell-checker has died for some reason.\n"
213 "Maybe it has been killed.");
218 Alert::error(_("The spell-checker has failed"), message);
223 void ControlSpellchecker::showSummary()
225 if (!checkAlive() || count_ == 0) {
232 message = bformat(_("%1$s words checked."), tostr(count_));
234 message = _("One word checked.");
237 Alert::information(_("Spell-checking is complete"), message);
241 void ControlSpellchecker::replace(string const & replacement)
243 bufferview()->replaceWord(replacement);
250 void ControlSpellchecker::replaceAll(string const & replacement)
253 replace(replacement);
257 void ControlSpellchecker::insert()
259 speller_->insert(word_);
264 string const ControlSpellchecker::getSuggestion() const
266 return speller_->nextMiss();
270 string const ControlSpellchecker::getWord() const
276 void ControlSpellchecker::ignoreAll()
278 speller_->accept(word_);