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/tostr.h"
37 #include "frontends/Alert.h"
39 using lyx::support::bformat;
47 ControlSpellchecker::ControlSpellchecker(Dialog & parent)
48 : Dialog::Controller(parent),
49 oldval_(0), newvalue_(0), count_(0)
53 ControlSpellchecker::~ControlSpellchecker()
59 SpellBase * getSpeller(BufferParams const & bp)
61 string lang = (lyxrc.isp_use_alt_lang)
63 : bp.language->code();
66 if (lyxrc.use_spell_lib)
67 return new ASpell(bp, lang);
70 if (lyxrc.use_spell_lib)
71 return new PSpell(bp, lang);
74 lang = (lyxrc.isp_use_alt_lang) ?
75 lyxrc.isp_alt_lang : bp.language->lang();
77 return new ISpell(bp, lang);
83 bool ControlSpellchecker::initialiseParams(std::string const &)
85 lyxerr[Debug::GUI] << "Spellchecker::initialiseParams" << endl;
87 speller_.reset(getSpeller(kernel().buffer().params()));
89 // reset values to initial
94 bool const success = speller_->error().empty();
97 Alert::error(_("The spell-checker could not be started"),
106 void ControlSpellchecker::clearParams()
108 lyxerr[Debug::GUI] << "Spellchecker::clearParams" << endl;
115 bool isLetter(DocIterator const & cur)
117 return cur.inTexted()
118 && cur.inset().allowSpellCheck()
119 && cur.pos() != cur.lastpos()
120 && cur.paragraph().isLetter(cur.pos())
121 && !isDeletedText(cur.paragraph(), cur.pos());
125 WordLangTuple nextWord(DocIterator & cur, ptrdiff_t & progress,
128 // skip until we have real text (will jump paragraphs)
129 for (; cur.size() && !isLetter(cur); cur.forwardPos());
134 return WordLangTuple(string(), string());
136 string lang_code = cur.paragraph().
137 getFontSettings(bp, cur.pos()).language()->code();
139 // and find the end of the word (insets like optional hyphens
140 // and ligature break are part of a word)
141 for (; cur && isLetter(cur); cur.forwardPos(), ++progress) {
142 if (!cur.paragraph().isInset(cur.pos()))
143 str += cur.paragraph().getChar(cur.pos());
146 return WordLangTuple(str, lang_code);
153 void ControlSpellchecker::check()
155 lyxerr[Debug::GUI] << "spell check a word" << endl;
157 SpellBase::Result res = SpellBase::OK;
159 DocIterator cur = kernel().bufferview()->cursor();
161 ptrdiff_t start = 0, total = 0;
162 DocIterator it = DocIterator(kernel().buffer().inset());
163 for (start = 0; it != cur; it.forwardPos())
166 for (total = start; it; it.forwardPos())
169 for (; cur && isLetter(cur); cur.forwardPos())
172 BufferParams & bufferparams = kernel().buffer().params();
174 while (res == SpellBase::OK || res == SpellBase::IGNORE) {
175 word_ = nextWord(cur, start, bufferparams);
178 if (getWord().empty())
183 // Update slider if and only if value has changed
184 float progress = total ? float(start)/total : 1;
185 newvalue_ = int(100.0 * progress);
186 if (newvalue_!= oldval_) {
187 lyxerr[Debug::GUI] << "Updating spell progress." << endl;
190 dialog().view().partialUpdate(SPELL_PROGRESSED);
193 // speller might be dead ...
197 res = speller_->check(word_);
199 // ... or it might just be reporting an error
204 lyxerr[Debug::GUI] << "Found word \"" << getWord() << "\"" << endl;
206 if (getWord().empty()) {
211 int const size = getWord().size();
212 kernel().bufferview()->putSelectionAt(cur, size, true);
215 if (res != SpellBase::OK && res != SpellBase::IGNORE) {
216 lyxerr[Debug::GUI] << "Found a word needing checking." << endl;
217 dialog().view().partialUpdate(SPELL_FOUND_WORD);
222 bool ControlSpellchecker::checkAlive()
224 if (speller_->alive() && speller_->error().empty())
227 string message = speller_->error();
229 message = _("The spell-checker has died for some reason.\n"
230 "Maybe it has been killed.");
232 dialog().CancelButton();
234 Alert::error(_("The spell-checker has failed"), message);
239 void ControlSpellchecker::showSummary()
241 if (!checkAlive() || count_ == 0) {
242 dialog().CancelButton();
248 message = bformat(_("%1$s words checked."), tostr(count_));
250 message = _("One word checked.");
252 dialog().CancelButton();
253 Alert::information(_("Spell-checking is complete"), message);
257 void ControlSpellchecker::replace(string const & replacement)
259 lyxerr << "ControlSpellchecker::replace("
260 << replacement << ")" << std::endl;
261 BufferView & bufferview = *kernel().bufferview();
262 lyx::cap::replaceWord(bufferview.cursor(), replacement);
263 kernel().buffer().markDirty();
271 void ControlSpellchecker::replaceAll(string const & replacement)
274 replace(replacement);
278 void ControlSpellchecker::insert()
280 speller_->insert(word_);
285 string const ControlSpellchecker::getSuggestion() const
287 return speller_->nextMiss();
291 string const ControlSpellchecker::getWord() const
297 void ControlSpellchecker::ignoreAll()
299 speller_->accept(word_);