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)
155 && cur.paragraph().isLetter(cur.pos())
156 && !isDeletedText(cur.paragraph(), cur.pos());
157 //&& (!cur.nextInset() || cur.nextInset()->allowSpellCheck());
161 WordLangTuple nextWord(DocumentIterator & cur, ptrdiff_t & progress,
164 // skip until we have real text (will jump paragraphs)
165 for (; cur.size() && !isLetter(cur); cur.forwardChar());
170 return WordLangTuple(string(), string());
172 string lang_code = cur.paragraph().
173 getFontSettings(bp, cur.pos()).language()->code();
175 // and find the end of the word (insets like optional hyphens
176 // and ligature break are part of a word)
177 for (; cur.size() && isLetter(cur); cur.forwardChar(), ++progress) {
178 if (!cur.paragraph().isInset(cur.pos()))
179 str += cur.paragraph().getChar(cur.pos());
182 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 DocumentIterator cur = bufferview()->cursor();
197 // a rough estimate should be sufficient:
198 //DocumentIterator::difference_type start = distance(beg, cur);
199 //DocumentIterator::difference_type const total = start + distance(cur, end);
201 ptrdiff_t start = 0, total = 0;
202 DocumentIterator it = DocumentIterator(buffer()->inset());
203 for (start = 0; it != cur; it.forwardPos())
206 for (total = start; it.size(); it.forwardPos())
209 for (; cur.size() && isLetter(cur); cur.forwardPos())
212 while (res == SpellBase::OK || res == SpellBase::IGNORE) {
213 word_ = nextWord(cur, start, buffer()->params());
216 if (word_.word().empty())
221 // Update slider if and only if value has changed
222 float progress = total ? float(start)/total : 1;
223 newvalue_ = int(100.0 * progress);
224 if (newvalue_!= oldval_) {
225 lyxerr[Debug::GUI] << "Updating spell progress." << endl;
228 view().partialUpdate(SPELL_PROGRESSED);
231 // speller might be dead ...
235 res = speller_->check(word_);
237 // ... or it might just be reporting an error
242 lyxerr[Debug::GUI] << "Found word \"" << word_.word() << "\"" << endl;
244 if (word_.word().empty()) {
250 int const size = word_.word().size();
253 bufferview()->putSelectionAt(cur, size, false);
256 bufferview()->putSelectionAt(cur, size, true);
260 if (res != SpellBase::OK && res != SpellBase::IGNORE) {
261 lyxerr[Debug::GUI] << "Found a word needing checking." << endl;
262 view().partialUpdate(SPELL_FOUND_WORD);
267 bool ControlSpellchecker::checkAlive()
269 if (speller_->alive() && speller_->error().empty())
272 string message = speller_->error();
274 message = _("The spell-checker has died for some reason.\n"
275 "Maybe it has been killed.");
280 Alert::error(_("The spell-checker has failed"), message);
285 void ControlSpellchecker::showSummary()
287 if (!checkAlive() || count_ == 0) {
294 message = bformat(_("%1$s words checked."), tostr(count_));
296 message = _("One word checked.");
299 Alert::information(_("Spell-checking is complete"), message);
303 void ControlSpellchecker::replace(string const & replacement)
305 lyx::cap::replaceWord(bufferview()->cursor(), replacement);
306 bufferview()->buffer()->markDirty();
307 bufferview()->update();
314 void ControlSpellchecker::replaceAll(string const & replacement)
317 replace(replacement);
321 void ControlSpellchecker::insert()
323 speller_->insert(word_);
328 string const ControlSpellchecker::getSuggestion() const
330 return speller_->nextMiss();
334 string const ControlSpellchecker::getWord() const
340 void ControlSpellchecker::ignoreAll()
342 speller_->accept(word_);