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 & dit)
132 return dit.inTexted()
133 && dit.inset().allowSpellCheck()
134 && dit.pos() != dit.lastpos()
135 && (dit.paragraph().isLetter(dit.pos())
136 // We want to pass the ' and escape chars to ispell
137 || contains(lyx::from_utf8(lyxrc.isp_esc_chars + '\''),
138 dit.paragraph().getChar(dit.pos())))
139 && !isDeletedText(dit.paragraph(), dit.pos());
143 WordLangTuple nextWord(LCursor & cur, ptrdiff_t & progress)
145 BufferParams const & bp = cur.bv().buffer()->params();
147 bool ignoreword = false;
152 while (cur.depth()) {
159 lang_code = cur.paragraph().getFontSettings(bp, cur.pos()).language()->code();
161 // Insets like optional hyphens and ligature
162 // break are part of a word.
163 if (!cur.paragraph().isInset(cur.pos())) {
164 Paragraph::value_type const c =
165 cur.paragraph().getChar(cur.pos());
170 } else { // !isLetter(cur)
172 if (!word.empty() && !ignoreword) {
174 return WordLangTuple(lyx::to_utf8(word), lang_code);
183 return WordLangTuple(string(), string());
190 void ControlSpellchecker::check()
192 lyxerr[Debug::GUI] << "Check the spelling of a word" << endl;
194 SpellBase::Result res = SpellBase::OK;
196 LCursor cur = kernel().bufferview()->cursor();
197 while (cur && cur.pos() && isLetter(cur)) {
201 ptrdiff_t start = 0, total = 0;
202 DocIterator it = DocIterator(kernel().buffer().inset());
203 for (start = 0; it != cur; it.forwardPos())
206 for (total = start; it; it.forwardPos())
211 while (res == SpellBase::OK || res == SpellBase::IGNORED_WORD) {
212 word_ = nextWord(cur, start);
215 if (getWord().empty()) {
223 // Update slider if and only if value has changed
224 float progress = total ? float(start)/total : 1;
225 newvalue_ = int(100.0 * progress);
226 if (newvalue_!= oldval_) {
227 lyxerr[Debug::GUI] << "Updating spell progress." << endl;
230 dialog().view().partialUpdate(SPELL_PROGRESSED);
233 // speller might be dead ...
237 res = speller_->check(word_);
239 // ... or it might just be reporting an error
244 lyxerr[Debug::GUI] << "Found word \"" << getWord() << "\"" << endl;
246 int const size = cur.selEnd().pos() - cur.selBegin().pos();
248 kernel().bufferview()->putSelectionAt(cur, size, false);
249 // if we used a lfun like in find/replace, dispatch would do
251 kernel().bufferview()->update();
254 if (res != SpellBase::OK && res != SpellBase::IGNORED_WORD) {
255 lyxerr[Debug::GUI] << "Found a word needing checking." << endl;
256 dialog().view().partialUpdate(SPELL_FOUND_WORD);
261 bool ControlSpellchecker::checkAlive()
263 if (speller_->alive() && speller_->error().empty())
267 if (speller_->error().empty())
268 message = _("The spellchecker has died for some reason.\n"
269 "Maybe it has been killed.");
271 message = _("The spellchecker has failed.\n")
274 dialog().CancelButton();
276 Alert::error(_("The spellchecker has failed"), message);
281 void ControlSpellchecker::showSummary()
283 if (!checkAlive() || count_ == 0) {
284 dialog().CancelButton();
290 message = bformat(_("%1$d words checked."), count_);
292 message = _("One word checked.");
294 dialog().CancelButton();
295 Alert::information(_("Spelling check completed"), message);
299 void ControlSpellchecker::replace(string const & replacement)
301 lyxerr[Debug::GUI] << "ControlSpellchecker::replace("
302 << replacement << ")" << std::endl;
303 BufferView & bufferview = *kernel().bufferview();
304 cap::replaceSelectionWithString(bufferview.cursor(), replacement, true);
305 kernel().buffer().markDirty();
313 void ControlSpellchecker::replaceAll(string const & replacement)
316 replace(replacement);
320 void ControlSpellchecker::insert()
322 speller_->insert(word_);
327 string const ControlSpellchecker::getSuggestion() const
329 return speller_->nextMiss();
333 string const ControlSpellchecker::getWord() const
339 void ControlSpellchecker::ignoreAll()
341 speller_->accept(word_);
345 } // namespace frontend