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"
26 #if defined(USE_ASPELL)
27 # include "aspell_local.h"
28 #elif defined(USE_PSPELL)
32 #if defined(USE_ISPELL)
35 # include "SpellBase.h"
38 #include "support/textutils.h"
39 #include "support/convert.h"
41 #include "frontends/Alert.h"
50 using support::bformat;
51 using support::contains;
56 ControlSpellchecker::ControlSpellchecker(Dialog & parent)
57 : Dialog::Controller(parent), exitEarly_(false),
58 oldval_(0), newvalue_(0), count_(0)
62 ControlSpellchecker::~ControlSpellchecker()
68 SpellBase * getSpeller(BufferParams const & bp)
70 string lang = (lyxrc.isp_use_alt_lang)
72 : bp.language->code();
74 #if defined(USE_ASPELL)
75 if (lyxrc.use_spell_lib)
76 return new ASpell(bp, lang);
77 #elif defined(USE_PSPELL)
78 if (lyxrc.use_spell_lib)
79 return new PSpell(bp, lang);
82 #if defined(USE_ISPELL)
83 lang = (lyxrc.isp_use_alt_lang) ?
84 lyxrc.isp_alt_lang : bp.language->lang();
86 return new ISpell(bp, lang);
95 bool ControlSpellchecker::initialiseParams(std::string const &)
97 lyxerr[Debug::GUI] << "Spellchecker::initialiseParams" << endl;
99 speller_.reset(getSpeller(kernel().buffer().params()));
103 // reset values to initial
108 bool const success = speller_->error().empty();
111 Alert::error(_("Spellchecker error"),
112 _("The spellchecker could not be started\n")
113 + speller_->error());
121 void ControlSpellchecker::clearParams()
123 lyxerr[Debug::GUI] << "Spellchecker::clearParams" << endl;
130 bool isLetter(DocIterator const & cur)
132 return cur.inTexted()
133 && cur.inset().allowSpellCheck()
134 && cur.pos() != cur.lastpos()
135 && (cur.paragraph().isLetter(cur.pos())
136 // We want to pass the ' and escape chars to ispell
137 || contains(lyxrc.isp_esc_chars + '\'',
138 cur.paragraph().getChar(cur.pos())))
139 && !isDeletedText(cur.paragraph(), cur.pos());
143 WordLangTuple nextWord(DocIterator & cur, ptrdiff_t & progress,
147 bool ignoreword = false;
148 string word, lang_code;
150 while (cur.depth()) {
156 lang_code = cur.paragraph().getFontSettings(bp, cur.pos()).language()->code();
158 // Insets like optional hyphens and ligature
159 // break are part of a word.
160 if (!cur.paragraph().isInset(cur.pos())) {
161 Paragraph::value_type const c =
162 cur.paragraph().getChar(cur.pos());
167 } else { // !isLetter(cur)
169 if (!word.empty() && !ignoreword)
170 return WordLangTuple(word, lang_code);
179 return WordLangTuple(string(), string());
186 void ControlSpellchecker::check()
188 lyxerr[Debug::GUI] << "Check the spelling of a word" << endl;
190 SpellBase::Result res = SpellBase::OK;
192 DocIterator cur = kernel().bufferview()->cursor();
193 while (cur && cur.pos() && isLetter(cur)) {
197 ptrdiff_t start = 0, total = 0;
198 DocIterator it = DocIterator(kernel().buffer().inset());
199 for (start = 0; it != cur; it.forwardPos())
202 for (total = start; it; it.forwardPos())
205 BufferParams & bufferparams = kernel().buffer().params();
208 while (res == SpellBase::OK || res == SpellBase::IGNORED_WORD) {
209 word_ = nextWord(cur, start, bufferparams);
212 if (getWord().empty()) {
220 // Update slider if and only if value has changed
221 float progress = total ? float(start)/total : 1;
222 newvalue_ = int(100.0 * progress);
223 if (newvalue_!= oldval_) {
224 lyxerr[Debug::GUI] << "Updating spell progress." << endl;
227 dialog().view().partialUpdate(SPELL_PROGRESSED);
230 // speller might be dead ...
234 res = speller_->check(word_);
236 // ... or it might just be reporting an error
241 lyxerr[Debug::GUI] << "Found word \"" << getWord() << "\"" << endl;
243 int const size = getWord().size();
245 kernel().bufferview()->putSelectionAt(cur, size, false);
246 // if we used a lfun like in find/replace, dispatch would do
248 kernel().bufferview()->update();
251 if (res != SpellBase::OK && res != SpellBase::IGNORED_WORD) {
252 lyxerr[Debug::GUI] << "Found a word needing checking." << endl;
253 dialog().view().partialUpdate(SPELL_FOUND_WORD);
258 bool ControlSpellchecker::checkAlive()
260 if (speller_->alive() && speller_->error().empty())
264 if (speller_->error().empty())
265 message = _("The spellchecker has died for some reason.\n"
266 "Maybe it has been killed.");
268 message = _("The spellchecker has failed.\n")
271 dialog().CancelButton();
273 Alert::error(_("The spellchecker has failed"), message);
278 void ControlSpellchecker::showSummary()
280 if (!checkAlive() || count_ == 0) {
281 dialog().CancelButton();
287 message = bformat(_("%1$d words checked."), count_);
289 message = _("One word checked.");
291 dialog().CancelButton();
292 Alert::information(_("Spelling check completed"), message);
296 void ControlSpellchecker::replace(string const & replacement)
298 lyxerr << "ControlSpellchecker::replace("
299 << replacement << ")" << std::endl;
300 BufferView & bufferview = *kernel().bufferview();
301 cap::replaceWord(bufferview.cursor(), replacement);
302 kernel().buffer().markDirty();
310 void ControlSpellchecker::replaceAll(string const & replacement)
313 replace(replacement);
317 void ControlSpellchecker::insert()
319 speller_->insert(word_);
324 string const ControlSpellchecker::getSuggestion() const
326 return speller_->nextMiss();
330 string const ControlSpellchecker::getWord() const
336 void ControlSpellchecker::ignoreAll()
338 speller_->accept(word_);
342 } // namespace frontend