2 * \file GuiSpellchecker.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
9 * Full author contact details are available in file CREDITS.
14 #include "GuiSpellchecker.h"
16 #include "qt_helpers.h"
19 #include "BufferParams.h"
20 #include "BufferView.h"
22 #include "CutAndPaste.h"
25 #include "Paragraph.h"
27 #include "support/debug.h"
28 #include "support/docstring.h"
29 #include "support/gettext.h"
30 #include "support/lstrings.h"
31 #include "support/textutils.h"
33 #include <QListWidgetItem>
35 #if defined(USE_ASPELL)
36 # include "ASpell_local.h"
37 #elif defined(USE_PSPELL)
41 #if defined(USE_ISPELL)
44 # include "SpellBase.h"
47 #include "frontends/alert.h"
50 using namespace lyx::support;
56 GuiSpellchecker::GuiSpellchecker(GuiView & lv)
57 : GuiDialog(lv, "spellchecker", qt_("Spellchecker")), exitEarly_(false),
58 oldval_(0), newvalue_(0), count_(0), speller_(0)
62 connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
63 connect(replacePB, SIGNAL(clicked()), this, SLOT(replace()));
64 connect(ignorePB, SIGNAL(clicked()), this, SLOT(ignore()));
65 connect(replacePB_3, SIGNAL(clicked()), this, SLOT(accept()));
66 connect(addPB, SIGNAL(clicked()), this, SLOT(add()));
68 connect(replaceCO, SIGNAL(highlighted(QString)),
69 this, SLOT(replaceChanged(QString)));
70 connect(suggestionsLW, SIGNAL(itemDoubleClicked(QListWidgetItem*)),
71 this, SLOT(replace()));
72 connect(suggestionsLW, SIGNAL(itemClicked(QListWidgetItem*)),
73 this, SLOT(suggestionChanged(QListWidgetItem*)));
75 wordED->setReadOnly(true);
77 bc().setPolicy(ButtonPolicy::NoRepeatedApplyReadOnlyPolicy);
78 bc().setCancel(closePB);
82 GuiSpellchecker::~GuiSpellchecker()
88 void GuiSpellchecker::suggestionChanged(QListWidgetItem * item)
90 if (replaceCO->count() != 0)
91 replaceCO->setItemText(0, item->text());
93 replaceCO->addItem(item->text());
95 replaceCO->setCurrentIndex(0);
99 void GuiSpellchecker::replaceChanged(const QString & str)
101 if (suggestionsLW->currentItem()->text() == str)
104 for (int i = 0; i != suggestionsLW->count(); ++i) {
105 if (suggestionsLW->item(i)->text() == str) {
106 suggestionsLW->setCurrentRow(i);
113 void GuiSpellchecker::reject()
120 void GuiSpellchecker::updateContents()
122 // The clauses below are needed because the spellchecker
123 // has many flaws (see bugs 1950, 2218).
124 // Basically, we have to distinguish the case where a
125 // spellcheck has already been performed for the whole
126 // document (exitEarly() == true, isVisible() == false)
127 // from the rest (exitEarly() == false, isVisible() == true).
128 // FIXME: rewrite the whole beast!
129 static bool check_after_early_exit;
131 // a spellcheck has already been performed,
133 check_after_early_exit = true;
135 else if (isVisible()) {
136 // the above check triggers a second update,
137 // and isVisible() is true then. Prevent a
138 // second check that skips the first word
139 if (check_after_early_exit)
140 // don't check, but reset the bool.
141 // business as usual after this.
142 check_after_early_exit = false;
144 // perform spellcheck (default case)
150 void GuiSpellchecker::accept()
156 void GuiSpellchecker::add()
162 void GuiSpellchecker::ignore()
168 void GuiSpellchecker::replace()
170 replace(qstring_to_ucs4(replaceCO->currentText()));
174 void GuiSpellchecker::partialUpdate(int state)
177 case SPELL_PROGRESSED:
178 spellcheckPR->setValue(getProgress());
181 case SPELL_FOUND_WORD: {
182 wordED->setText(toqstr(getWord()));
183 suggestionsLW->clear();
186 while (!(w = getSuggestion()).empty())
187 suggestionsLW->addItem(toqstr(w));
189 if (suggestionsLW->count() == 0)
190 suggestionChanged(new QListWidgetItem(wordED->text()));
192 suggestionChanged(suggestionsLW->item(0));
194 suggestionsLW->setCurrentRow(0);
201 static SpellBase * createSpeller(BufferParams const & bp)
203 string lang = (lyxrc.isp_use_alt_lang)
205 : bp.language->code();
207 #if defined(USE_ASPELL)
208 if (lyxrc.use_spell_lib)
209 return new ASpell(bp, lang);
210 #elif defined(USE_PSPELL)
211 if (lyxrc.use_spell_lib)
212 return new PSpell(bp, lang);
215 #if defined(USE_ISPELL)
216 lang = lyxrc.isp_use_alt_lang ?
217 lyxrc.isp_alt_lang : bp.language->lang();
219 return new ISpell(bp, lang);
221 return new SpellBase;
226 bool GuiSpellchecker::initialiseParams(string const &)
228 LYXERR(Debug::GUI, "Spellchecker::initialiseParams");
230 speller_ = createSpeller(buffer().params());
234 // reset values to initial
239 bool const success = speller_->error().empty();
242 Alert::error(_("Spellchecker error"),
243 _("The spellchecker could not be started\n")
244 + speller_->error());
253 void GuiSpellchecker::clearParams()
255 LYXERR(Debug::GUI, "Spellchecker::clearParams");
261 static bool isLetter(DocIterator const & dit)
263 return dit.inTexted()
264 && dit.inset().allowSpellCheck()
265 && dit.pos() != dit.lastpos()
266 && (dit.paragraph().isLetter(dit.pos())
267 // We want to pass the ' and escape chars to ispell
268 || contains(from_utf8(lyxrc.isp_esc_chars + '\''),
269 dit.paragraph().getChar(dit.pos())))
270 && !dit.paragraph().isDeleted(dit.pos());
274 static WordLangTuple nextWord(Cursor & cur, ptrdiff_t & progress)
276 BufferParams const & bp = cur.bv().buffer().params();
278 bool ignoreword = false;
283 while (cur.depth()) {
290 lang_code = cur.paragraph().getFontSettings(bp, cur.pos()).language()->code();
292 // Insets like optional hyphens and ligature
293 // break are part of a word.
294 if (!cur.paragraph().isInset(cur.pos())) {
295 char_type const c = cur.paragraph().getChar(cur.pos());
300 } else { // !isLetter(cur)
302 if (!word.empty() && !ignoreword) {
304 return WordLangTuple(word, lang_code);
313 return WordLangTuple(docstring(), string());
317 void GuiSpellchecker::check()
319 LYXERR(Debug::GUI, "Check the spelling of a word");
321 SpellBase::Result res = SpellBase::OK;
323 Cursor cur = bufferview()->cursor();
324 while (cur && cur.pos() && isLetter(cur))
329 DocIterator it = doc_iterator_begin(buffer().inset());
330 for (start = 1; it != cur; it.forwardPos())
333 for (total = start; it; it.forwardPos())
338 while (res == SpellBase::OK || res == SpellBase::IGNORED_WORD) {
339 word_ = nextWord(cur, start);
342 if (getWord().empty()) {
350 // Update slider if and only if value has changed
351 float progress = total ? float(start)/total : 1;
352 newvalue_ = int(100.0 * progress);
353 if (newvalue_!= oldval_) {
354 LYXERR(Debug::GUI, "Updating spell progress.");
357 partialUpdate(SPELL_PROGRESSED);
360 // speller might be dead ...
364 res = speller_->check(word_);
366 // ... or it might just be reporting an error
371 LYXERR(Debug::GUI, "Found word \"" << to_utf8(getWord()) << "\"");
373 int const size = cur.selEnd().pos() - cur.selBegin().pos();
375 bufferview()->putSelectionAt(cur, size, false);
376 // FIXME: if we used a lfun like in find/replace, dispatch would do
378 // FIXME: this Controller is very badly designed...
379 bufferview()->processUpdateFlags(Update::Force | Update::FitCursor);
382 if (res != SpellBase::OK && res != SpellBase::IGNORED_WORD) {
383 LYXERR(Debug::GUI, "Found a word needing checking.");
384 partialUpdate(SPELL_FOUND_WORD);
389 bool GuiSpellchecker::checkAlive()
391 if (speller_->alive() && speller_->error().empty())
395 if (speller_->error().empty())
396 message = _("The spellchecker has died for some reason.\n"
397 "Maybe it has been killed.");
399 message = _("The spellchecker has failed.\n") + speller_->error();
403 Alert::error(_("The spellchecker has failed"), message);
408 void GuiSpellchecker::showSummary()
410 if (!checkAlive() || count_ == 0) {
417 message = bformat(_("%1$d words checked."), count_);
419 message = _("One word checked.");
422 Alert::information(_("Spelling check completed"), message);
426 void GuiSpellchecker::replace(docstring const & replacement)
428 LYXERR(Debug::GUI, "GuiSpellchecker::replace("
429 << to_utf8(replacement) << ")");
430 cap::replaceSelectionWithString(bufferview()->cursor(), replacement, true);
431 buffer().markDirty();
432 // If we used an LFUN, we would not need that
433 bufferview()->processUpdateFlags(Update::Force | Update::FitCursor);
440 void GuiSpellchecker::replaceAll(docstring const & replacement)
443 replace(replacement);
447 void GuiSpellchecker::insert()
449 speller_->insert(word_);
454 docstring GuiSpellchecker::getSuggestion() const
456 return speller_->nextMiss();
460 docstring GuiSpellchecker::getWord() const
466 void GuiSpellchecker::ignoreAll()
468 speller_->accept(word_);
473 Dialog * createGuiSpellchecker(GuiView & lv) { return new GuiSpellchecker(lv); }
475 } // namespace frontend
478 #include "GuiSpellchecker_moc.cpp"