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"
40 #include "support/docstring.h"
42 #include "frontends/Alert.h"
51 using support::bformat;
52 using support::contains;
57 ControlSpellchecker::ControlSpellchecker(Dialog & parent)
58 : Dialog::Controller(parent), exitEarly_(false),
59 oldval_(0), newvalue_(0), count_(0)
64 ControlSpellchecker::~ControlSpellchecker()
70 SpellBase * getSpeller(BufferParams const & bp)
72 string lang = (lyxrc.isp_use_alt_lang)
74 : bp.language->code();
76 #if defined(USE_ASPELL)
77 if (lyxrc.use_spell_lib)
78 return new ASpell(bp, lang);
79 #elif defined(USE_PSPELL)
80 if (lyxrc.use_spell_lib)
81 return new PSpell(bp, lang);
84 #if defined(USE_ISPELL)
85 lang = (lyxrc.isp_use_alt_lang) ?
86 lyxrc.isp_alt_lang : bp.language->lang();
88 return new ISpell(bp, lang);
97 bool ControlSpellchecker::initialiseParams(std::string const &)
99 lyxerr[Debug::GUI] << "Spellchecker::initialiseParams" << endl;
101 speller_.reset(getSpeller(kernel().buffer().params()));
105 // reset values to initial
110 bool const success = speller_->error().empty();
114 Alert::error(_("Spellchecker error"),
115 _("The spellchecker could not be started\n")
116 + speller_->error());
124 void ControlSpellchecker::clearParams()
126 lyxerr[Debug::GUI] << "Spellchecker::clearParams" << endl;
133 bool isLetter(DocIterator const & dit)
135 return dit.inTexted()
136 && dit.inset().allowSpellCheck()
137 && dit.pos() != dit.lastpos()
138 && (dit.paragraph().isLetter(dit.pos())
139 // We want to pass the ' and escape chars to ispell
140 || contains(lyx::from_utf8(lyxrc.isp_esc_chars + '\''),
141 dit.paragraph().getChar(dit.pos())))
142 && !isDeletedText(dit.paragraph(), dit.pos());
146 WordLangTuple nextWord(LCursor & cur, ptrdiff_t & progress)
148 BufferParams const & bp = cur.bv().buffer()->params();
150 bool ignoreword = false;
155 while (cur.depth()) {
162 lang_code = cur.paragraph().getFontSettings(bp, cur.pos()).language()->code();
164 // Insets like optional hyphens and ligature
165 // break are part of a word.
166 if (!cur.paragraph().isInset(cur.pos())) {
167 Paragraph::value_type const c =
168 cur.paragraph().getChar(cur.pos());
173 } else { // !isLetter(cur)
175 if (!word.empty() && !ignoreword) {
177 return WordLangTuple(lyx::to_utf8(word), lang_code);
186 return WordLangTuple(string(), string());
193 void ControlSpellchecker::check()
195 lyxerr[Debug::GUI] << "Check the spelling of a word" << endl;
197 SpellBase::Result res = SpellBase::OK;
199 LCursor cur = kernel().bufferview()->cursor();
200 while (cur && cur.pos() && isLetter(cur)) {
204 ptrdiff_t start = 0, total = 0;
205 DocIterator it = DocIterator(kernel().buffer().inset());
206 for (start = 0; it != cur; it.forwardPos())
209 for (total = start; it; it.forwardPos())
214 while (res == SpellBase::OK || res == SpellBase::IGNORED_WORD) {
215 word_ = nextWord(cur, start);
218 if (getWord().empty()) {
226 // Update slider if and only if value has changed
227 float progress = total ? float(start)/total : 1;
228 newvalue_ = int(100.0 * progress);
229 if (newvalue_!= oldval_) {
230 lyxerr[Debug::GUI] << "Updating spell progress." << endl;
233 dialog().view().partialUpdate(SPELL_PROGRESSED);
236 // speller might be dead ...
240 res = speller_->check(word_);
242 // ... or it might just be reporting an error
247 lyxerr[Debug::GUI] << "Found word \"" << getWord() << "\"" << endl;
249 int const size = cur.selEnd().pos() - cur.selBegin().pos();
251 kernel().bufferview()->putSelectionAt(cur, size, false);
252 // if we used a lfun like in find/replace, dispatch would do
254 kernel().bufferview()->update();
257 if (res != SpellBase::OK && res != SpellBase::IGNORED_WORD) {
258 lyxerr[Debug::GUI] << "Found a word needing checking." << endl;
259 dialog().view().partialUpdate(SPELL_FOUND_WORD);
264 bool ControlSpellchecker::checkAlive()
266 if (speller_->alive() && speller_->error().empty())
270 if (speller_->error().empty())
271 message = _("The spellchecker has died for some reason.\n"
272 "Maybe it has been killed.");
274 message = _("The spellchecker has failed.\n") + speller_->error();
276 dialog().CancelButton();
278 Alert::error(_("The spellchecker has failed"), message);
283 void ControlSpellchecker::showSummary()
285 if (!checkAlive() || count_ == 0) {
286 dialog().CancelButton();
293 message = bformat(_("%1$d words checked."), count_);
295 message = _("One word checked.");
297 dialog().CancelButton();
298 Alert::information(_("Spelling check completed"), message);
302 void ControlSpellchecker::replace(string const & replacement)
304 lyxerr[Debug::GUI] << "ControlSpellchecker::replace("
305 << replacement << ")" << std::endl;
306 BufferView & bufferview = *kernel().bufferview();
307 cap::replaceSelectionWithString(bufferview.cursor(), replacement, true);
308 kernel().buffer().markDirty();
316 void ControlSpellchecker::replaceAll(string const & replacement)
319 replace(replacement);
323 void ControlSpellchecker::insert()
325 speller_->insert(word_);
330 string const ControlSpellchecker::getSuggestion() const
332 return speller_->nextMiss();
336 string const ControlSpellchecker::getWord() const
342 void ControlSpellchecker::ignoreAll()
344 speller_->accept(word_);
348 } // namespace frontend