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"
31 # include "aspell_local.h"
35 #include "support/textutils.h"
36 #include "support/convert.h"
38 #include "frontends/Alert.h"
47 using support::bformat;
48 using support::contains;
53 ControlSpellchecker::ControlSpellchecker(Dialog & parent)
54 : Dialog::Controller(parent),
55 oldval_(0), newvalue_(0), count_(0)
59 ControlSpellchecker::~ControlSpellchecker()
65 SpellBase * getSpeller(BufferParams const & bp)
67 string lang = (lyxrc.isp_use_alt_lang)
69 : bp.language->code();
72 if (lyxrc.use_spell_lib)
73 return new ASpell(bp, lang);
76 if (lyxrc.use_spell_lib)
77 return new PSpell(bp, lang);
80 lang = (lyxrc.isp_use_alt_lang) ?
81 lyxrc.isp_alt_lang : bp.language->lang();
83 return new ISpell(bp, lang);
89 bool ControlSpellchecker::initialiseParams(std::string const &)
91 lyxerr[Debug::GUI] << "Spellchecker::initialiseParams" << endl;
93 speller_.reset(getSpeller(kernel().buffer().params()));
95 // reset values to initial
100 bool const success = speller_->error().empty();
103 Alert::error(_("The spell-checker could not be started"),
112 void ControlSpellchecker::clearParams()
114 lyxerr[Debug::GUI] << "Spellchecker::clearParams" << endl;
121 bool isLetter(DocIterator const & cur)
123 return cur.inTexted()
124 && cur.inset().allowSpellCheck()
125 && cur.pos() != cur.lastpos()
126 && (cur.paragraph().isLetter(cur.pos())
127 // We want to pass the ' and escape chars to ispell
128 || contains(lyxrc.isp_esc_chars + '\'',
129 cur.paragraph().getChar(cur.pos())))
130 && !isDeletedText(cur.paragraph(), cur.pos());
134 WordLangTuple nextWord(DocIterator & cur, ptrdiff_t & progress,
138 bool ignoreword = false;
139 string word, lang_code;
147 lang_code = cur.paragraph().getFontSettings(bp, cur.pos()).language()->code();
149 // Insets like optional hyphens and ligature
150 // break are part of a word.
151 if (!cur.paragraph().isInset(cur.pos())) {
152 Paragraph::value_type const c =
153 cur.paragraph().getChar(cur.pos());
158 } else { // !isLetter(cur)
161 return WordLangTuple(word, lang_code);
170 return WordLangTuple(string(), string());
177 void ControlSpellchecker::check()
179 lyxerr[Debug::GUI] << "spell check a word" << endl;
181 SpellBase::Result res = SpellBase::OK;
183 DocIterator cur = kernel().bufferview()->cursor();
185 ptrdiff_t start = 0, total = 0;
186 DocIterator it = DocIterator(kernel().buffer().inset());
187 for (start = 0; it != cur; it.forwardPos())
190 for (total = start; it; it.forwardPos())
193 for (; cur && isLetter(cur); cur.forwardPos())
196 BufferParams & bufferparams = kernel().buffer().params();
198 while (res == SpellBase::OK || res == SpellBase::IGNORED_WORD) {
199 word_ = nextWord(cur, start, bufferparams);
202 if (getWord().empty()) {
209 // Update slider if and only if value has changed
210 float progress = total ? float(start)/total : 1;
211 newvalue_ = int(100.0 * progress);
212 if (newvalue_!= oldval_) {
213 lyxerr[Debug::GUI] << "Updating spell progress." << endl;
216 dialog().view().partialUpdate(SPELL_PROGRESSED);
219 // speller might be dead ...
223 res = speller_->check(word_);
225 // ... or it might just be reporting an error
230 lyxerr[Debug::GUI] << "Found word \"" << getWord() << "\"" << endl;
232 int const size = getWord().size();
234 kernel().bufferview()->putSelectionAt(cur, size, false);
235 // if we used a lfun like in find/replace, dispatch would do
237 kernel().bufferview()->update();
240 if (res != SpellBase::OK && res != SpellBase::IGNORED_WORD) {
241 lyxerr[Debug::GUI] << "Found a word needing checking." << endl;
242 dialog().view().partialUpdate(SPELL_FOUND_WORD);
247 bool ControlSpellchecker::checkAlive()
249 if (speller_->alive() && speller_->error().empty())
252 string message = speller_->error();
254 message = _("The spell-checker has died for some reason.\n"
255 "Maybe it has been killed.");
257 dialog().CancelButton();
259 Alert::error(_("The spell-checker has failed"), message);
264 void ControlSpellchecker::showSummary()
266 if (!checkAlive() || count_ == 0) {
267 dialog().CancelButton();
273 message = bformat(_("%1$d words checked."), count_);
275 message = _("One word checked.");
277 dialog().CancelButton();
278 Alert::information(_("Spell-checking is complete"), message);
282 void ControlSpellchecker::replace(string const & replacement)
284 lyxerr << "ControlSpellchecker::replace("
285 << replacement << ")" << std::endl;
286 BufferView & bufferview = *kernel().bufferview();
287 cap::replaceWord(bufferview.cursor(), replacement);
288 kernel().buffer().markDirty();
296 void ControlSpellchecker::replaceAll(string const & replacement)
299 replace(replacement);
303 void ControlSpellchecker::insert()
305 speller_->insert(word_);
310 string const ControlSpellchecker::getSuggestion() const
312 return speller_->nextMiss();
316 string const ControlSpellchecker::getWord() const
322 void ControlSpellchecker::ignoreAll()
324 speller_->accept(word_);
328 } // namespace frontend