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;
50 ControlSpellchecker::ControlSpellchecker(LyXView & lv, Dialogs & d)
51 : ControlDialogBD(lv, d),
52 oldval_(0), newvalue_(0), count_(0)
56 ControlSpellchecker::~ControlSpellchecker()
60 void ControlSpellchecker::setParams()
62 lyxerr[Debug::GUI] << "spell setParams" << endl;
67 void ControlSpellchecker::clearParams()
69 lyxerr[Debug::GUI] << "spell clearParams" << endl;
77 SpellBase * getSpeller(BufferParams const & bp)
79 string lang = (lyxrc.isp_use_alt_lang)
81 : bp.language->code();
84 if (lyxrc.use_spell_lib)
85 return new ASpell(bp, lang);
88 if (lyxrc.use_spell_lib)
89 return new PSpell(bp, lang);
92 lang = (lyxrc.isp_use_alt_lang) ?
93 lyxrc.isp_alt_lang : bp.language->lang();
95 return new ISpell(bp, lang);
101 void ControlSpellchecker::startSession()
103 lyxerr[Debug::GUI] << "spell startSession" << endl;
105 if (speller_.get()) {
106 lyxerr[Debug::GUI] << "startSession: speller exists" << endl;
111 speller_.reset(getSpeller(buffer()->params()));
113 // reset values to initial
117 emergency_exit_ = false;
119 // start off the check
120 if (speller_->error().empty()) {
125 emergency_exit_ = true;
126 string message = speller_->error();
128 message = _("The spell-checker could not be started.\n"
129 "Maybe it is mis-configured.");
131 Alert::error(_("The spell-checker has failed"), message);
136 void ControlSpellchecker::endSession()
138 lyxerr[Debug::GUI] << "spell endSession" << endl;
140 emergency_exit_ = true;
142 if (!speller_.get()) {
143 lyxerr[Debug::GUI] << "endSession with no speller" << endl;
154 bool isLetter(PosIterator & cur)
157 && cur.pit()->isLetter(cur.pos())
158 && !isDeletedText(*cur.pit(), cur.pos())
159 && (!cur.inset() || cur.inset()->allowSpellCheck());
163 WordLangTuple nextWord(PosIterator & cur, PosIterator const & end,
164 int & progress, BufferParams & bp)
166 // skip until we have real text (will jump paragraphs)
167 for (; cur != end && !isLetter(cur); ++cur, ++progress);
170 return WordLangTuple(string(), string());
172 string lang_code = cur.pit()->getFontSettings(bp, cur.pos()).language()->code();
174 // and find the end of the word (insets like optional hyphens
175 // and ligature break are part of a word)
176 for (; cur != end && isLetter(cur); ++cur, ++progress) {
177 if (!cur.pit()->isInset(cur.pos()))
178 str += cur.pit()->getChar(cur.pos());
181 return WordLangTuple(str, lang_code);
190 void ControlSpellchecker::check()
192 lyxerr[Debug::GUI] << "spell check a word" << endl;
194 SpellBase::Result res = SpellBase::OK;
196 PosIterator cur(*bufferview());
197 PosIterator const beg = buffer()->pos_iterator_begin();
198 PosIterator const end = buffer()->pos_iterator_end();
200 int start = distance(beg, cur);
201 int const total = start + distance(cur, end);
203 if (cur != buffer()->pos_iterator_begin())
204 for (; cur != end && isLetter(cur); ++cur, ++start);
206 while (res == SpellBase::OK || res == SpellBase::IGNORE) {
207 word_ = nextWord(cur, end, start, buffer()->params());
210 if (word_.word().empty())
215 // Update slider if and only if value has changed
216 float progress = total ? float(start)/total : 1;
217 newvalue_ = int(100.0 * progress);
218 if (newvalue_!= oldval_) {
219 lyxerr[Debug::GUI] << "Updating spell progress." << endl;
222 view().partialUpdate(SPELL_PROGRESSED);
225 // speller might be dead ...
229 res = speller_->check(word_);
231 // ... or it might just be reporting an error
236 lyxerr[Debug::GUI] << "Found word \"" << word_.word() << "\"" << endl;
238 if (!word_.word().empty()) {
239 int const size = word_.word().size();
241 bufferview()->putSelectionAt(cur, size, false);
250 if (res != SpellBase::OK && res != SpellBase::IGNORE) {
251 lyxerr[Debug::GUI] << "Found a word needing checking." << endl;
252 view().partialUpdate(SPELL_FOUND_WORD);
257 bool ControlSpellchecker::checkAlive()
259 if (speller_->alive() && speller_->error().empty())
262 string message = speller_->error();
264 message = _("The spell-checker has died for some reason.\n"
265 "Maybe it has been killed.");
270 Alert::error(_("The spell-checker has failed"), message);
275 void ControlSpellchecker::showSummary()
277 if (!checkAlive() || count_ == 0) {
284 message = bformat(_("%1$s words checked."), tostr(count_));
286 message = _("One word checked.");
289 Alert::information(_("Spell-checking is complete"), message);
293 void ControlSpellchecker::replace(string const & replacement)
295 bufferview()->replaceWord(replacement);
302 void ControlSpellchecker::replaceAll(string const & replacement)
305 replace(replacement);
309 void ControlSpellchecker::insert()
311 speller_->insert(word_);
316 string const ControlSpellchecker::getSuggestion() const
318 return speller_->nextMiss();
322 string const ControlSpellchecker::getWord() const
328 void ControlSpellchecker::ignoreAll()
330 speller_->accept(word_);