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 "frontends/Alert.h"
37 ControlSpellchecker::ControlSpellchecker(LyXView & lv, Dialogs & d)
38 : ControlDialogBD(lv, d),
39 newval_(0.0), oldval_(0), newvalue_(0), count_(0)
43 ControlSpellchecker::~ControlSpellchecker()
47 void ControlSpellchecker::setParams()
49 lyxerr[Debug::GUI] << "spell setParams" << endl;
54 void ControlSpellchecker::clearParams()
56 lyxerr[Debug::GUI] << "spell clearParams" << endl;
62 SpellBase * getSpeller(BufferParams const & bp)
64 string lang = (lyxrc.isp_use_alt_lang)
66 : bp.language->code();
69 if (lyxrc.use_spell_lib)
70 return new ASpell(bp, lang);
73 if (lyxrc.use_spell_lib)
74 return new PSpell(bp, lang);
77 lang = (lyxrc.isp_use_alt_lang) ?
78 lyxrc.isp_alt_lang : bp.language->lang();
80 return new ISpell(bp, lang);
85 void ControlSpellchecker::startSession()
87 lyxerr[Debug::GUI] << "spell startSession" << endl;
90 lyxerr[Debug::GUI] << "startSession: speller exists" << endl;
95 speller_.reset(getSpeller(buffer()->params));
97 // reset values to initial
102 emergency_exit_ = false;
104 // start off the check
105 if (speller_->error().empty()) {
110 emergency_exit_ = true;
111 string message = speller_->error();
113 message = _("The spell-checker could not be started.\n"
114 "Maybe it is mis-configured.");
116 Alert::error(_("The spell-checker has failed"), message);
121 void ControlSpellchecker::endSession()
123 lyxerr[Debug::GUI] << "spell endSession" << endl;
125 bufferview()->endOfSpellCheck();
127 emergency_exit_ = true;
129 if (!speller_.get()) {
130 lyxerr[Debug::GUI] << "endSession with no speller" << endl;
138 void ControlSpellchecker::check()
140 lyxerr[Debug::GUI] << "spell check a word" << endl;
142 SpellBase::Result res = SpellBase::OK;
144 // clear any old selection
145 LyXText * text = bufferview()->getLyXText();
146 bufferview()->toggleSelection(true);
147 bufferview()->update(text, BufferView::SELECT);
149 while ((res == SpellBase::OK || res == SpellBase::IGNORE)) {
150 word_ = bufferview()->nextWord(newval_);
153 if (word_.word().empty())
158 // Update slider if and only if value has changed
159 newvalue_ = int(100.0 * newval_);
160 if (newvalue_!= oldval_) {
161 lyxerr[Debug::GUI] << "Updating spell progress." << endl;
164 view().partialUpdate(SPELL_PROGRESSED);
167 // speller might be dead ...
171 res = speller_->check(word_);
173 // ... or it might just be reporting an error
178 lyxerr[Debug::GUI] << "Found word \"" << word_.word() << "\"" << endl;
180 if (!word_.word().empty()) {
181 bufferview()->selectLastWord();
189 if (res != SpellBase::OK && res != SpellBase::IGNORE) {
190 lyxerr[Debug::GUI] << "Found a word needing checking." << endl;
191 view().partialUpdate(SPELL_FOUND_WORD);
196 bool ControlSpellchecker::checkAlive()
198 if (speller_->alive() && speller_->error().empty())
201 string message = speller_->error();
203 message = _("The spell-checker has died for some reason.\n"
204 "Maybe it has been killed.");
209 Alert::error(_("The spell-checker has failed"), message);
214 void ControlSpellchecker::showSummary()
216 if (!checkAlive() || count_ == 0) {
223 message = bformat(_("%1$s words checked."), tostr(count_));
225 message = _("One word checked.");
228 Alert::information(_("Spell-checking is complete"), message);
232 void ControlSpellchecker::replace(string const & replacement)
234 bufferview()->replaceWord(replacement);
241 void ControlSpellchecker::replaceAll(string const & replacement)
244 replace(replacement);
248 void ControlSpellchecker::insert()
250 speller_->insert(word_);
255 string const ControlSpellchecker::getSuggestion() const
257 return speller_->nextMiss();
261 string const ControlSpellchecker::getWord() const
267 void ControlSpellchecker::ignoreAll()
269 speller_->accept(word_);