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
14 #include "ControlSpellchecker.h"
17 #include "BufferView.h"
29 #include "frontends/Alert.h"
31 #include "BoostFormat.h"
35 ControlSpellchecker::ControlSpellchecker(LyXView & lv, Dialogs & d)
36 : ControlDialogBD(lv, d),
37 newval_(0.0), oldval_(0), newvalue_(0), count_(0)
41 ControlSpellchecker::~ControlSpellchecker()
45 void ControlSpellchecker::setParams()
47 lyxerr[Debug::GUI] << "spell setParams" << endl;
52 void ControlSpellchecker::clearParams()
54 lyxerr[Debug::GUI] << "spell clearParams" << endl;
59 void ControlSpellchecker::startSession()
61 lyxerr[Debug::GUI] << "spell startSession" << endl;
64 lyxerr[Debug::GUI] << "startSession: speller exists" << endl;
69 // create spell object
72 if (lyxrc.use_pspell) {
73 tmp = (lyxrc.isp_use_alt_lang) ?
74 lyxrc.isp_alt_lang : buffer()->params.language->code();
76 speller_.reset(new PSpell(buffer()->params, tmp));
79 tmp = (lyxrc.isp_use_alt_lang) ?
80 lyxrc.isp_alt_lang : buffer()->params.language->lang();
82 speller_.reset(new ISpell(buffer()->params, tmp));
87 // reset values to initial
92 emergency_exit_ = false;
94 // start off the check
95 if (speller_->error().empty()) {
100 emergency_exit_ = true;
101 string message = speller_->error();
103 message = _("The spell-checker could not be started.\n"
104 "Maybe it is mis-configured.");
106 Alert::alert(_("The spell-checker has failed"), message);
111 void ControlSpellchecker::endSession()
113 lyxerr[Debug::GUI] << "spell endSession" << endl;
115 bufferview()->endOfSpellCheck();
117 emergency_exit_ = true;
119 if (!speller_.get()) {
120 lyxerr[Debug::GUI] << "endSession with no speller" << endl;
128 void ControlSpellchecker::check()
130 lyxerr[Debug::GUI] << "spell check a word" << endl;
132 SpellBase::Result res = SpellBase::OK;
134 // clear any old selection
135 LyXText * text = bufferview()->getLyXText();
136 bufferview()->toggleSelection(true);
137 bufferview()->update(text, BufferView::SELECT);
139 while ((res == SpellBase::OK || res == SpellBase::IGNORE)) {
140 word_ = bufferview()->nextWord(newval_);
143 if (word_.word().empty())
148 // Update slider if and only if value has changed
149 newvalue_ = int(100.0 * newval_);
150 if (newvalue_!= oldval_) {
151 lyxerr[Debug::GUI] << "Updating spell progress." << endl;
154 view().partialUpdate(SPELL_PROGRESSED);
157 // speller might be dead ...
161 res = speller_->check(word_);
163 // ... or it might just be reporting an error
168 lyxerr[Debug::GUI] << "Found word \"" << word_.word() << "\"" << endl;
170 if (!word_.word().empty()) {
171 bufferview()->selectLastWord();
179 if (res != SpellBase::OK && res != SpellBase::IGNORE) {
180 lyxerr[Debug::GUI] << "Found a word needing checking." << endl;
181 view().partialUpdate(SPELL_FOUND_WORD);
186 bool ControlSpellchecker::checkAlive()
188 if (speller_->alive() && speller_->error().empty())
191 string message = speller_->error();
193 message = _("The spell-checker has died for some reason.\n"
194 "Maybe it has been killed.");
199 Alert::alert(_("The spell-checker has failed"), message);
204 void ControlSpellchecker::showSummary()
206 if (!checkAlive() || count_ == 0) {
215 boost::format fmter("%1$d words checked.");
217 message += fmter.str();
219 message += _("One word checked.");
223 message += tostr(count_) + " words checked";
225 message = _("One word checked.");
230 Alert::alert(_("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_);