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"
20 #include "CutAndPaste.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;
152 bool isLetter(DocumentIterator const & cur)
154 return cur.inTexted()
155 && cur.inset().allowSpellCheck()
156 && cur.pos() != cur.lastpos()
157 && cur.paragraph().isLetter(cur.pos())
158 && !isDeletedText(cur.paragraph(), cur.pos());
162 WordLangTuple nextWord(DocumentIterator & cur, ptrdiff_t & progress,
165 // skip until we have real text (will jump paragraphs)
166 for (; cur.size() && !isLetter(cur); cur.forwardPos());
171 return WordLangTuple(string(), string());
173 string lang_code = cur.paragraph().
174 getFontSettings(bp, cur.pos()).language()->code();
176 // and find the end of the word (insets like optional hyphens
177 // and ligature break are part of a word)
178 for (; cur && isLetter(cur); cur.forwardPos(), ++progress) {
179 if (!cur.paragraph().isInset(cur.pos()))
180 str += cur.paragraph().getChar(cur.pos());
183 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 DocumentIterator cur = bufferview()->cursor();
198 // a rough estimate should be sufficient:
199 //DocumentIterator::difference_type start = distance(beg, cur);
200 //DocumentIterator::difference_type const total = start + distance(cur, end);
202 ptrdiff_t start = 0, total = 0;
203 DocumentIterator it = DocumentIterator(buffer()->inset());
204 for (start = 0; it != cur; it.forwardPos())
207 for (total = start; it; it.forwardPos())
210 for (; cur && isLetter(cur); cur.forwardPos())
213 while (res == SpellBase::OK || res == SpellBase::IGNORE) {
214 word_ = nextWord(cur, start, buffer()->params());
217 if (word_.word().empty())
222 // Update slider if and only if value has changed
223 float progress = total ? float(start)/total : 1;
224 newvalue_ = int(100.0 * progress);
225 if (newvalue_!= oldval_) {
226 lyxerr[Debug::GUI] << "Updating spell progress." << endl;
229 view().partialUpdate(SPELL_PROGRESSED);
232 // speller might be dead ...
236 res = speller_->check(word_);
238 // ... or it might just be reporting an error
243 lyxerr[Debug::GUI] << "Found word \"" << word_.word() << "\"" << endl;
245 if (word_.word().empty()) {
251 int const size = word_.word().size();
254 bufferview()->putSelectionAt(cur, size, false);
257 bufferview()->putSelectionAt(cur, size, true);
261 if (res != SpellBase::OK && res != SpellBase::IGNORE) {
262 lyxerr[Debug::GUI] << "Found a word needing checking." << endl;
263 view().partialUpdate(SPELL_FOUND_WORD);
268 bool ControlSpellchecker::checkAlive()
270 if (speller_->alive() && speller_->error().empty())
273 string message = speller_->error();
275 message = _("The spell-checker has died for some reason.\n"
276 "Maybe it has been killed.");
281 Alert::error(_("The spell-checker has failed"), message);
286 void ControlSpellchecker::showSummary()
288 if (!checkAlive() || count_ == 0) {
295 message = bformat(_("%1$s words checked."), tostr(count_));
297 message = _("One word checked.");
300 Alert::information(_("Spell-checking is complete"), message);
304 void ControlSpellchecker::replace(string const & replacement)
306 lyx::cap::replaceWord(bufferview()->cursor(), replacement);
307 bufferview()->buffer()->markDirty();
308 bufferview()->update();
315 void ControlSpellchecker::replaceAll(string const & replacement)
318 replace(replacement);
322 void ControlSpellchecker::insert()
324 speller_->insert(word_);
329 string const ControlSpellchecker::getSuggestion() const
331 return speller_->nextMiss();
335 string const ControlSpellchecker::getWord() const
341 void ControlSpellchecker::ignoreAll()
343 speller_->accept(word_);