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"
16 #include "bufferparams.h"
17 #include "BufferView.h"
19 #include "CutAndPaste.h"
24 #include "paragraph.h"
31 # include "aspell_local.h"
35 #include "support/tostr.h"
37 #include "frontends/Alert.h"
46 using support::bformat;
51 ControlSpellchecker::ControlSpellchecker(Dialog & parent)
52 : Dialog::Controller(parent),
53 oldval_(0), newvalue_(0), count_(0)
57 ControlSpellchecker::~ControlSpellchecker()
63 SpellBase * getSpeller(BufferParams const & bp)
65 string lang = (lyxrc.isp_use_alt_lang)
67 : bp.language->code();
70 if (lyxrc.use_spell_lib)
71 return new ASpell(bp, lang);
74 if (lyxrc.use_spell_lib)
75 return new PSpell(bp, lang);
78 lang = (lyxrc.isp_use_alt_lang) ?
79 lyxrc.isp_alt_lang : bp.language->lang();
81 return new ISpell(bp, lang);
87 bool ControlSpellchecker::initialiseParams(std::string const &)
89 lyxerr[Debug::GUI] << "Spellchecker::initialiseParams" << endl;
91 speller_.reset(getSpeller(kernel().buffer().params()));
93 // reset values to initial
98 bool const success = speller_->error().empty();
101 Alert::error(_("The spell-checker could not be started"),
110 void ControlSpellchecker::clearParams()
112 lyxerr[Debug::GUI] << "Spellchecker::clearParams" << endl;
119 bool isLetter(DocIterator const & cur)
121 return cur.inTexted()
122 && cur.inset().allowSpellCheck()
123 && cur.pos() != cur.lastpos()
124 && cur.paragraph().isLetter(cur.pos())
125 && !isDeletedText(cur.paragraph(), cur.pos());
129 WordLangTuple nextWord(DocIterator & cur, ptrdiff_t & progress,
132 // skip until we have real text (will jump paragraphs)
133 for (; cur.size() && !isLetter(cur); cur.forwardPos());
138 return WordLangTuple(string(), string());
140 string lang_code = cur.paragraph().
141 getFontSettings(bp, cur.pos()).language()->code();
143 // and find the end of the word (insets like optional hyphens
144 // and ligature break are part of a word)
145 for (; cur && isLetter(cur); cur.forwardPos(), ++progress) {
146 if (!cur.paragraph().isInset(cur.pos()))
147 str += cur.paragraph().getChar(cur.pos());
150 return WordLangTuple(str, lang_code);
157 void ControlSpellchecker::check()
159 lyxerr[Debug::GUI] << "spell check a word" << endl;
161 SpellBase::Result res = SpellBase::OK;
163 DocIterator cur = kernel().bufferview()->cursor();
165 ptrdiff_t start = 0, total = 0;
166 DocIterator it = DocIterator(kernel().buffer().inset());
167 for (start = 0; it != cur; it.forwardPos())
170 for (total = start; it; it.forwardPos())
173 for (; cur && isLetter(cur); cur.forwardPos())
176 BufferParams & bufferparams = kernel().buffer().params();
178 while (res == SpellBase::OK || res == SpellBase::IGNORE) {
179 word_ = nextWord(cur, start, bufferparams);
182 if (getWord().empty())
187 // Update slider if and only if value has changed
188 float progress = total ? float(start)/total : 1;
189 newvalue_ = int(100.0 * progress);
190 if (newvalue_!= oldval_) {
191 lyxerr[Debug::GUI] << "Updating spell progress." << endl;
194 dialog().view().partialUpdate(SPELL_PROGRESSED);
197 // speller might be dead ...
201 res = speller_->check(word_);
203 // ... or it might just be reporting an error
208 lyxerr[Debug::GUI] << "Found word \"" << getWord() << "\"" << endl;
210 if (getWord().empty()) {
215 int const size = getWord().size();
216 kernel().bufferview()->putSelectionAt(cur, size, true);
219 if (res != SpellBase::OK && res != SpellBase::IGNORE) {
220 lyxerr[Debug::GUI] << "Found a word needing checking." << endl;
221 dialog().view().partialUpdate(SPELL_FOUND_WORD);
226 bool ControlSpellchecker::checkAlive()
228 if (speller_->alive() && speller_->error().empty())
231 string message = speller_->error();
233 message = _("The spell-checker has died for some reason.\n"
234 "Maybe it has been killed.");
236 dialog().CancelButton();
238 Alert::error(_("The spell-checker has failed"), message);
243 void ControlSpellchecker::showSummary()
245 if (!checkAlive() || count_ == 0) {
246 dialog().CancelButton();
252 message = bformat(_("%1$s words checked."), tostr(count_));
254 message = _("One word checked.");
256 dialog().CancelButton();
257 Alert::information(_("Spell-checking is complete"), message);
261 void ControlSpellchecker::replace(string const & replacement)
263 lyxerr << "ControlSpellchecker::replace("
264 << replacement << ")" << std::endl;
265 BufferView & bufferview = *kernel().bufferview();
266 cap::replaceWord(bufferview.cursor(), replacement);
267 kernel().buffer().markDirty();
275 void ControlSpellchecker::replaceAll(string const & replacement)
278 replace(replacement);
282 void ControlSpellchecker::insert()
284 speller_->insert(word_);
289 string const ControlSpellchecker::getSuggestion() const
291 return speller_->nextMiss();
295 string const ControlSpellchecker::getWord() const
301 void ControlSpellchecker::ignoreAll()
303 speller_->accept(word_);
307 } // namespace frontend