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),
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(_("The spell-checker could not be started"),
120 void ControlSpellchecker::clearParams()
122 lyxerr[Debug::GUI] << "Spellchecker::clearParams" << endl;
129 bool isLetter(DocIterator const & cur)
131 return cur.inTexted()
132 && cur.inset().allowSpellCheck()
133 && cur.pos() != cur.lastpos()
134 && (cur.paragraph().isLetter(cur.pos())
135 // We want to pass the ' and escape chars to ispell
136 || contains(lyxrc.isp_esc_chars + '\'',
137 cur.paragraph().getChar(cur.pos())))
138 && !isDeletedText(cur.paragraph(), cur.pos());
142 WordLangTuple nextWord(DocIterator & cur, ptrdiff_t & progress,
146 bool ignoreword = false;
147 string word, lang_code;
149 while (cur.depth()) {
155 lang_code = cur.paragraph().getFontSettings(bp, cur.pos()).language()->code();
157 // Insets like optional hyphens and ligature
158 // break are part of a word.
159 if (!cur.paragraph().isInset(cur.pos())) {
160 Paragraph::value_type const c =
161 cur.paragraph().getChar(cur.pos());
166 } else { // !isLetter(cur)
169 return WordLangTuple(word, lang_code);
178 return WordLangTuple(string(), string());
185 void ControlSpellchecker::check()
187 lyxerr[Debug::GUI] << "spell check a word" << endl;
189 SpellBase::Result res = SpellBase::OK;
191 DocIterator cur = kernel().bufferview()->cursor();
193 ptrdiff_t start = 0, total = 0;
194 DocIterator it = DocIterator(kernel().buffer().inset());
195 for (start = 0; it != cur; it.forwardPos())
198 for (total = start; it; it.forwardPos())
201 for (; cur && isLetter(cur); cur.forwardPos())
204 BufferParams & bufferparams = kernel().buffer().params();
206 while (res == SpellBase::OK || res == SpellBase::IGNORED_WORD) {
207 word_ = nextWord(cur, start, bufferparams);
210 if (getWord().empty()) {
217 // Update slider if and only if value has changed
218 float progress = total ? float(start)/total : 1;
219 newvalue_ = int(100.0 * progress);
220 if (newvalue_!= oldval_) {
221 lyxerr[Debug::GUI] << "Updating spell progress." << endl;
224 dialog().view().partialUpdate(SPELL_PROGRESSED);
227 // speller might be dead ...
231 res = speller_->check(word_);
233 // ... or it might just be reporting an error
238 lyxerr[Debug::GUI] << "Found word \"" << getWord() << "\"" << endl;
240 int const size = getWord().size();
242 kernel().bufferview()->putSelectionAt(cur, size, false);
243 // if we used a lfun like in find/replace, dispatch would do
245 kernel().bufferview()->update();
248 if (res != SpellBase::OK && res != SpellBase::IGNORED_WORD) {
249 lyxerr[Debug::GUI] << "Found a word needing checking." << endl;
250 dialog().view().partialUpdate(SPELL_FOUND_WORD);
255 bool ControlSpellchecker::checkAlive()
257 if (speller_->alive() && speller_->error().empty())
260 string message = speller_->error();
262 message = _("The spell-checker has died for some reason.\n"
263 "Maybe it has been killed.");
265 dialog().CancelButton();
267 Alert::error(_("The spell-checker has failed"), message);
272 void ControlSpellchecker::showSummary()
274 if (!checkAlive() || count_ == 0) {
275 dialog().CancelButton();
281 message = bformat(_("%1$d words checked."), count_);
283 message = _("One word checked.");
285 dialog().CancelButton();
286 Alert::information(_("Spell-checking is complete"), message);
290 void ControlSpellchecker::replace(string const & replacement)
292 lyxerr << "ControlSpellchecker::replace("
293 << replacement << ")" << std::endl;
294 BufferView & bufferview = *kernel().bufferview();
295 cap::replaceWord(bufferview.cursor(), replacement);
296 kernel().buffer().markDirty();
304 void ControlSpellchecker::replaceAll(string const & replacement)
307 replace(replacement);
311 void ControlSpellchecker::insert()
313 speller_->insert(word_);
318 string const ControlSpellchecker::getSuggestion() const
320 return speller_->nextMiss();
324 string const ControlSpellchecker::getWord() const
330 void ControlSpellchecker::ignoreAll()
332 speller_->accept(word_);
336 } // namespace frontend