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();
113 Alert::error(_("Spellchecker error"),
114 _("The spellchecker could not be started\n")
115 + speller_->error());
123 void ControlSpellchecker::clearParams()
125 lyxerr[Debug::GUI] << "Spellchecker::clearParams" << endl;
132 bool isLetter(DocIterator const & dit)
134 return dit.inTexted()
135 && dit.inset().allowSpellCheck()
136 && dit.pos() != dit.lastpos()
137 && (dit.paragraph().isLetter(dit.pos())
138 // We want to pass the ' and escape chars to ispell
139 || contains(lyx::from_utf8(lyxrc.isp_esc_chars + '\''),
140 dit.paragraph().getChar(dit.pos())))
141 && !dit.paragraph().isDeleted(dit.pos());
145 WordLangTuple nextWord(LCursor & cur, ptrdiff_t & progress)
147 BufferParams const & bp = cur.bv().buffer()->params();
149 bool ignoreword = false;
154 while (cur.depth()) {
161 lang_code = cur.paragraph().getFontSettings(bp, cur.pos()).language()->code();
163 // Insets like optional hyphens and ligature
164 // break are part of a word.
165 if (!cur.paragraph().isInset(cur.pos())) {
166 Paragraph::value_type const c =
167 cur.paragraph().getChar(cur.pos());
172 } else { // !isLetter(cur)
174 if (!word.empty() && !ignoreword) {
176 return WordLangTuple(lyx::to_utf8(word), lang_code);
185 return WordLangTuple(string(), string());
192 void ControlSpellchecker::check()
194 lyxerr[Debug::GUI] << "Check the spelling of a word" << endl;
196 SpellBase::Result res = SpellBase::OK;
198 LCursor cur = kernel().bufferview()->cursor();
199 while (cur && cur.pos() && isLetter(cur)) {
203 ptrdiff_t start = 0, total = 0;
204 DocIterator it = DocIterator(kernel().buffer().inset());
205 for (start = 0; it != cur; it.forwardPos())
208 for (total = start; it; it.forwardPos())
213 while (res == SpellBase::OK || res == SpellBase::IGNORED_WORD) {
214 word_ = nextWord(cur, start);
217 if (getWord().empty()) {
225 // Update slider if and only if value has changed
226 float progress = total ? float(start)/total : 1;
227 newvalue_ = int(100.0 * progress);
228 if (newvalue_!= oldval_) {
229 lyxerr[Debug::GUI] << "Updating spell progress." << endl;
232 dialog().view().partialUpdate(SPELL_PROGRESSED);
235 // speller might be dead ...
239 res = speller_->check(word_);
241 // ... or it might just be reporting an error
246 lyxerr[Debug::GUI] << "Found word \"" << getWord() << "\"" << endl;
248 int const size = cur.selEnd().pos() - cur.selBegin().pos();
250 kernel().bufferview()->putSelectionAt(cur, size, false);
251 // if we used a lfun like in find/replace, dispatch would do
253 kernel().bufferview()->update();
256 if (res != SpellBase::OK && res != SpellBase::IGNORED_WORD) {
257 lyxerr[Debug::GUI] << "Found a word needing checking." << endl;
258 dialog().view().partialUpdate(SPELL_FOUND_WORD);
263 bool ControlSpellchecker::checkAlive()
265 if (speller_->alive() && speller_->error().empty())
269 if (speller_->error().empty())
270 message = _("The spellchecker has died for some reason.\n"
271 "Maybe it has been killed.");
273 message = _("The spellchecker has failed.\n") + speller_->error();
275 dialog().CancelButton();
277 Alert::error(_("The spellchecker has failed"), message);
282 void ControlSpellchecker::showSummary()
284 if (!checkAlive() || count_ == 0) {
285 dialog().CancelButton();
291 message = bformat(_("%1$d words checked."), count_);
293 message = _("One word checked.");
295 dialog().CancelButton();
296 Alert::information(_("Spelling check completed"), message);
300 void ControlSpellchecker::replace(string const & replacement)
302 lyxerr[Debug::GUI] << "ControlSpellchecker::replace("
303 << replacement << ")" << std::endl;
304 BufferView & bufferview = *kernel().bufferview();
305 cap::replaceSelectionWithString(bufferview.cursor(), replacement, true);
306 kernel().buffer().markDirty();
307 // If we used an LFUN, we would not need that
315 void ControlSpellchecker::replaceAll(string const & replacement)
318 replace(replacement);
322 void ControlSpellchecker::insert()
324 speller_->insert(word_);
329 string const ControlSpellchecker::getSuggestion() const
331 return speller_->nextMiss();
335 string const ControlSpellchecker::getWord() const
341 void ControlSpellchecker::ignoreAll()
343 speller_->accept(word_);
347 } // namespace frontend