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"
24 #include "PosIterator.h"
25 #include "paragraph.h"
32 # include "aspell_local.h"
36 #include "support/tostr.h"
38 #include "frontends/Alert.h"
41 using lyx::support::bformat;
49 ControlSpellchecker::ControlSpellchecker(LyXView & lv, Dialogs & d)
50 : ControlDialogBD(lv, d),
51 oldval_(0), newvalue_(0), count_(0)
55 ControlSpellchecker::~ControlSpellchecker()
59 void ControlSpellchecker::setParams()
61 lyxerr[Debug::GUI] << "spell setParams" << endl;
66 void ControlSpellchecker::clearParams()
68 lyxerr[Debug::GUI] << "spell clearParams" << endl;
76 SpellBase * getSpeller(BufferParams const & bp)
78 string lang = (lyxrc.isp_use_alt_lang)
80 : bp.language->code();
83 if (lyxrc.use_spell_lib)
84 return new ASpell(bp, lang);
87 if (lyxrc.use_spell_lib)
88 return new PSpell(bp, lang);
91 lang = (lyxrc.isp_use_alt_lang) ?
92 lyxrc.isp_alt_lang : bp.language->lang();
94 return new ISpell(bp, lang);
100 void ControlSpellchecker::startSession()
102 lyxerr[Debug::GUI] << "spell startSession" << endl;
104 if (speller_.get()) {
105 lyxerr[Debug::GUI] << "startSession: speller exists" << endl;
110 speller_.reset(getSpeller(buffer()->params()));
112 // reset values to initial
116 emergency_exit_ = false;
118 // start off the check
119 if (speller_->error().empty()) {
124 emergency_exit_ = true;
125 string message = speller_->error();
127 message = _("The spell-checker could not be started.\n"
128 "Maybe it is mis-configured.");
130 Alert::error(_("The spell-checker has failed"), message);
135 void ControlSpellchecker::endSession()
137 lyxerr[Debug::GUI] << "spell endSession" << endl;
139 emergency_exit_ = true;
141 if (!speller_.get()) {
142 lyxerr[Debug::GUI] << "endSession with no speller" << endl;
153 bool isLetter(PosIterator & cur)
156 && cur.pit()->isLetter(cur.pos())
157 && !isDeletedText(*cur.pit(), cur.pos())
158 && (!cur.inset() || cur.inset()->allowSpellCheck());
162 WordLangTuple nextWord(PosIterator & cur, PosIterator const & end,
163 int & progress, BufferParams & bp)
165 // skip until we have real text (will jump paragraphs)
166 for (; cur != end && !isLetter(cur); ++cur, ++progress);
169 return WordLangTuple(string(), string());
171 string lang_code = cur.pit()->getFontSettings(bp, cur.pos()).language()->code();
173 // and find the end of the word (insets like optional hyphens
174 // and ligature break are part of a word)
175 for (; cur != end && isLetter(cur); ++cur, ++progress) {
176 if (!cur.pit()->isInset(cur.pos()))
177 str += cur.pit()->getChar(cur.pos());
180 return WordLangTuple(str, lang_code);
189 void ControlSpellchecker::check()
191 lyxerr[Debug::GUI] << "spell check a word" << endl;
193 SpellBase::Result res = SpellBase::OK;
195 PosIterator cur(*bufferview());
196 PosIterator const beg = buffer()->pos_iterator_begin();
197 PosIterator const end = buffer()->pos_iterator_end();
199 PosIterator::difference_type start = distance(beg, cur);
200 PosIterator::difference_type const total = start + distance(cur, end);
202 if (cur != buffer()->pos_iterator_begin())
203 for (; cur != end && isLetter(cur); ++cur, ++start);
205 while (res == SpellBase::OK || res == SpellBase::IGNORE) {
206 word_ = nextWord(cur, end, start, buffer()->params());
209 if (word_.word().empty())
214 // Update slider if and only if value has changed
215 float progress = total ? float(start)/total : 1;
216 newvalue_ = int(100.0 * progress);
217 if (newvalue_!= oldval_) {
218 lyxerr[Debug::GUI] << "Updating spell progress." << endl;
221 view().partialUpdate(SPELL_PROGRESSED);
224 // speller might be dead ...
228 res = speller_->check(word_);
230 // ... or it might just be reporting an error
235 lyxerr[Debug::GUI] << "Found word \"" << word_.word() << "\"" << endl;
237 if (word_.word().empty()) {
243 int const size = word_.word().size();
245 bufferview()->putSelectionAt(cur, size, false);
249 if (res != SpellBase::OK && res != SpellBase::IGNORE) {
250 lyxerr[Debug::GUI] << "Found a word needing checking." << endl;
251 view().partialUpdate(SPELL_FOUND_WORD);
256 bool ControlSpellchecker::checkAlive()
258 if (speller_->alive() && speller_->error().empty())
261 string message = speller_->error();
263 message = _("The spell-checker has died for some reason.\n"
264 "Maybe it has been killed.");
269 Alert::error(_("The spell-checker has failed"), message);
274 void ControlSpellchecker::showSummary()
276 if (!checkAlive() || count_ == 0) {
283 message = bformat(_("%1$s words checked."), tostr(count_));
285 message = _("One word checked.");
288 Alert::information(_("Spell-checking is complete"), message);
292 void ControlSpellchecker::replace(string const & replacement)
294 bufferview()->cursor().replaceWord(replacement);
295 bufferview()->buffer()->markDirty();
296 bufferview()->update();
303 void ControlSpellchecker::replaceAll(string const & replacement)
306 replace(replacement);
310 void ControlSpellchecker::insert()
312 speller_->insert(word_);
317 string const ControlSpellchecker::getSuggestion() const
319 return speller_->nextMiss();
323 string const ControlSpellchecker::getWord() const
329 void ControlSpellchecker::ignoreAll()
331 speller_->accept(word_);