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"
39 ControlSpellchecker::ControlSpellchecker(LyXView & lv, Dialogs & d)
40 : ControlDialogBD(lv, d),
41 newval_(0.0), oldval_(0), newvalue_(0), count_(0)
45 ControlSpellchecker::~ControlSpellchecker()
49 void ControlSpellchecker::setParams()
51 lyxerr[Debug::GUI] << "spell setParams" << endl;
56 void ControlSpellchecker::clearParams()
58 lyxerr[Debug::GUI] << "spell clearParams" << endl;
64 SpellBase * getSpeller(BufferParams const & bp)
66 string lang = (lyxrc.isp_use_alt_lang)
68 : bp.language->code();
71 if (lyxrc.use_spell_lib)
72 return new ASpell(bp, lang);
75 if (lyxrc.use_spell_lib)
76 return new PSpell(bp, lang);
79 lang = (lyxrc.isp_use_alt_lang) ?
80 lyxrc.isp_alt_lang : bp.language->lang();
82 return new ISpell(bp, lang);
87 void ControlSpellchecker::startSession()
89 lyxerr[Debug::GUI] << "spell startSession" << endl;
92 lyxerr[Debug::GUI] << "startSession: speller exists" << endl;
97 speller_.reset(getSpeller(buffer()->params));
99 // reset values to initial
104 emergency_exit_ = false;
106 // start off the check
107 if (speller_->error().empty()) {
112 emergency_exit_ = true;
113 string message = speller_->error();
115 message = _("The spell-checker could not be started.\n"
116 "Maybe it is mis-configured.");
118 Alert::error(_("The spell-checker has failed"), message);
123 void ControlSpellchecker::endSession()
125 lyxerr[Debug::GUI] << "spell endSession" << endl;
127 bufferview()->endOfSpellCheck();
129 emergency_exit_ = true;
131 if (!speller_.get()) {
132 lyxerr[Debug::GUI] << "endSession with no speller" << endl;
140 void ControlSpellchecker::check()
142 lyxerr[Debug::GUI] << "spell check a word" << endl;
144 SpellBase::Result res = SpellBase::OK;
146 // clear any old selection
147 LyXText * text = bufferview()->getLyXText();
148 bufferview()->toggleSelection(true);
149 bufferview()->update(text, BufferView::SELECT);
151 while ((res == SpellBase::OK || res == SpellBase::IGNORE)) {
152 word_ = bufferview()->nextWord(newval_);
155 if (word_.word().empty())
160 // Update slider if and only if value has changed
161 newvalue_ = int(100.0 * newval_);
162 if (newvalue_!= oldval_) {
163 lyxerr[Debug::GUI] << "Updating spell progress." << endl;
166 view().partialUpdate(SPELL_PROGRESSED);
169 // speller might be dead ...
173 res = speller_->check(word_);
175 // ... or it might just be reporting an error
180 lyxerr[Debug::GUI] << "Found word \"" << word_.word() << "\"" << endl;
182 if (!word_.word().empty()) {
183 bufferview()->selectLastWord();
191 if (res != SpellBase::OK && res != SpellBase::IGNORE) {
192 lyxerr[Debug::GUI] << "Found a word needing checking." << endl;
193 view().partialUpdate(SPELL_FOUND_WORD);
198 bool ControlSpellchecker::checkAlive()
200 if (speller_->alive() && speller_->error().empty())
203 string message = speller_->error();
205 message = _("The spell-checker has died for some reason.\n"
206 "Maybe it has been killed.");
211 Alert::error(_("The spell-checker has failed"), message);
216 void ControlSpellchecker::showSummary()
218 if (!checkAlive() || count_ == 0) {
225 message = bformat(_("%1$s words checked."), tostr(count_));
227 message = _("One word checked.");
230 Alert::information(_("Spell-checking is complete"), message);
234 void ControlSpellchecker::replace(string const & replacement)
236 bufferview()->replaceWord(replacement);
243 void ControlSpellchecker::replaceAll(string const & replacement)
246 replace(replacement);
250 void ControlSpellchecker::insert()
252 speller_->insert(word_);
257 string const ControlSpellchecker::getSuggestion() const
259 return speller_->nextMiss();
263 string const ControlSpellchecker::getWord() const
269 void ControlSpellchecker::ignoreAll()
271 speller_->accept(word_);