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"
19 #include "bufferview_funcs.h"
25 #include "PosIterator.h"
26 #include "paragraph.h"
33 # include "aspell_local.h"
37 #include "support/tostr.h"
39 #include "frontends/Alert.h"
42 using lyx::support::bformat;
48 ControlSpellchecker::ControlSpellchecker(LyXView & lv, Dialogs & d)
49 : ControlDialogBD(lv, d),
50 oldval_(0), newvalue_(0), count_(0)
54 ControlSpellchecker::~ControlSpellchecker()
58 void ControlSpellchecker::setParams()
60 lyxerr[Debug::GUI] << "spell setParams" << endl;
65 void ControlSpellchecker::clearParams()
67 lyxerr[Debug::GUI] << "spell clearParams" << endl;
75 SpellBase * getSpeller(BufferParams const & bp)
77 string lang = (lyxrc.isp_use_alt_lang)
79 : bp.language->code();
82 if (lyxrc.use_spell_lib)
83 return new ASpell(bp, lang);
86 if (lyxrc.use_spell_lib)
87 return new PSpell(bp, lang);
90 lang = (lyxrc.isp_use_alt_lang) ?
91 lyxrc.isp_alt_lang : bp.language->lang();
93 return new ISpell(bp, lang);
99 void ControlSpellchecker::startSession()
101 lyxerr[Debug::GUI] << "spell startSession" << endl;
103 if (speller_.get()) {
104 lyxerr[Debug::GUI] << "startSession: speller exists" << endl;
109 speller_.reset(getSpeller(buffer()->params()));
111 // reset values to initial
115 emergency_exit_ = false;
117 // start off the check
118 if (speller_->error().empty()) {
123 emergency_exit_ = true;
124 string message = speller_->error();
126 message = _("The spell-checker could not be started.\n"
127 "Maybe it is mis-configured.");
129 Alert::error(_("The spell-checker has failed"), message);
134 void ControlSpellchecker::endSession()
136 lyxerr[Debug::GUI] << "spell endSession" << endl;
138 emergency_exit_ = true;
140 if (!speller_.get()) {
141 lyxerr[Debug::GUI] << "endSession with no speller" << endl;
152 bool isLetter(PosIterator & cur)
155 && cur.pit()->isLetter(cur.pos())
156 && !isDeletedText(*cur.pit(), cur.pos())
157 && (!cur.inset() || cur.inset()->allowSpellCheck());
161 WordLangTuple nextWord(PosIterator & cur, PosIterator const & end,
162 int & progress, BufferParams & bp)
164 // skip until we have real text (will jump paragraphs)
165 for (; cur != end && !isLetter(cur); ++cur, ++progress);
168 return WordLangTuple(string(), string());
170 string lang_code = cur.pit()->getFontSettings(bp, cur.pos()).language()->code();
172 // and find the end of the word (insets like optional hyphens
173 // and ligature break are part of a word)
174 for (; cur != end && isLetter(cur); ++cur, ++progress) {
175 if (!cur.pit()->isInset(cur.pos()))
176 str += cur.pit()->getChar(cur.pos());
179 return WordLangTuple(str, lang_code);
188 void ControlSpellchecker::check()
190 lyxerr[Debug::GUI] << "spell check a word" << endl;
192 SpellBase::Result res = SpellBase::OK;
194 PosIterator cur(*bufferview());
195 PosIterator const beg = buffer()->pos_iterator_begin();
196 PosIterator const end = buffer()->pos_iterator_end();
198 int start = distance(beg, cur);
199 int const total = start + distance(cur, end);
201 if (cur != buffer()->pos_iterator_begin())
202 for (; cur != end && isLetter(cur); ++cur, ++start);
204 while (res == SpellBase::OK || res == SpellBase::IGNORE) {
205 word_ = nextWord(cur, end, start, buffer()->params());
208 if (word_.word().empty())
213 // Update slider if and only if value has changed
214 float progress = total ? float(start)/total : 1;
215 newvalue_ = int(100.0 * progress);
216 if (newvalue_!= oldval_) {
217 lyxerr[Debug::GUI] << "Updating spell progress." << endl;
220 view().partialUpdate(SPELL_PROGRESSED);
223 // speller might be dead ...
227 res = speller_->check(word_);
229 // ... or it might just be reporting an error
234 lyxerr[Debug::GUI] << "Found word \"" << word_.word() << "\"" << endl;
236 if (!word_.word().empty()) {
237 int const size = word_.word().size();
239 bv_funcs::put_selection_at(bufferview(), cur, size, false);
248 if (res != SpellBase::OK && res != SpellBase::IGNORE) {
249 lyxerr[Debug::GUI] << "Found a word needing checking." << endl;
250 view().partialUpdate(SPELL_FOUND_WORD);
255 bool ControlSpellchecker::checkAlive()
257 if (speller_->alive() && speller_->error().empty())
260 string message = speller_->error();
262 message = _("The spell-checker has died for some reason.\n"
263 "Maybe it has been killed.");
268 Alert::error(_("The spell-checker has failed"), message);
273 void ControlSpellchecker::showSummary()
275 if (!checkAlive() || count_ == 0) {
282 message = bformat(_("%1$s words checked."), tostr(count_));
284 message = _("One word checked.");
287 Alert::information(_("Spell-checking is complete"), message);
291 void ControlSpellchecker::replace(string const & replacement)
293 bufferview()->replaceWord(replacement);
300 void ControlSpellchecker::replaceAll(string const & replacement)
303 replace(replacement);
307 void ControlSpellchecker::insert()
309 speller_->insert(word_);
314 string const ControlSpellchecker::getSuggestion() const
316 return speller_->nextMiss();
320 string const ControlSpellchecker::getWord() const
326 void ControlSpellchecker::ignoreAll()
328 speller_->accept(word_);