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;
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);
89 void ControlSpellchecker::startSession()
91 lyxerr[Debug::GUI] << "spell startSession" << endl;
94 lyxerr[Debug::GUI] << "startSession: speller exists" << endl;
99 speller_.reset(getSpeller(buffer()->params));
101 // reset values to initial
106 emergency_exit_ = false;
108 // start off the check
109 if (speller_->error().empty()) {
114 emergency_exit_ = true;
115 string message = speller_->error();
117 message = _("The spell-checker could not be started.\n"
118 "Maybe it is mis-configured.");
120 Alert::error(_("The spell-checker has failed"), message);
125 void ControlSpellchecker::endSession()
127 lyxerr[Debug::GUI] << "spell endSession" << endl;
129 bufferview()->endOfSpellCheck();
131 emergency_exit_ = true;
133 if (!speller_.get()) {
134 lyxerr[Debug::GUI] << "endSession with no speller" << endl;
142 void ControlSpellchecker::check()
144 lyxerr[Debug::GUI] << "spell check a word" << endl;
146 SpellBase::Result res = SpellBase::OK;
148 // clear any old selection
149 LyXText * text = bufferview()->getLyXText();
150 bufferview()->toggleSelection(true);
151 bufferview()->update(text, BufferView::SELECT);
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;
184 if (!word_.word().empty()) {
185 bufferview()->selectLastWord();
193 if (res != SpellBase::OK && res != SpellBase::IGNORE) {
194 lyxerr[Debug::GUI] << "Found a word needing checking." << endl;
195 view().partialUpdate(SPELL_FOUND_WORD);
200 bool ControlSpellchecker::checkAlive()
202 if (speller_->alive() && speller_->error().empty())
205 string message = speller_->error();
207 message = _("The spell-checker has died for some reason.\n"
208 "Maybe it has been killed.");
213 Alert::error(_("The spell-checker has failed"), message);
218 void ControlSpellchecker::showSummary()
220 if (!checkAlive() || count_ == 0) {
227 message = bformat(_("%1$s words checked."), tostr(count_));
229 message = _("One word checked.");
232 Alert::information(_("Spell-checking is complete"), message);
236 void ControlSpellchecker::replace(string const & replacement)
238 bufferview()->replaceWord(replacement);
245 void ControlSpellchecker::replaceAll(string const & replacement)
248 replace(replacement);
252 void ControlSpellchecker::insert()
254 speller_->insert(word_);
259 string const ControlSpellchecker::getSuggestion() const
261 return speller_->nextMiss();
265 string const ControlSpellchecker::getWord() const
271 void ControlSpellchecker::ignoreAll()
273 speller_->accept(word_);