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"
39 #include "SpellBase.h"
41 #include "frontends/alert.h"
44 using namespace lyx::support;
50 GuiSpellchecker::GuiSpellchecker(GuiView & lv)
51 : GuiDialog(lv, "spellchecker", qt_("Spellchecker")), exitEarly_(false),
52 oldval_(0), newvalue_(0), count_(0), speller_(0)
56 connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
57 connect(replacePB, SIGNAL(clicked()), this, SLOT(replace()));
58 connect(ignorePB, SIGNAL(clicked()), this, SLOT(ignore()));
59 connect(replacePB_3, SIGNAL(clicked()), this, SLOT(accept()));
60 connect(addPB, SIGNAL(clicked()), this, SLOT(add()));
62 connect(replaceCO, SIGNAL(highlighted(QString)),
63 this, SLOT(replaceChanged(QString)));
64 connect(suggestionsLW, SIGNAL(itemDoubleClicked(QListWidgetItem*)),
65 this, SLOT(replace()));
66 connect(suggestionsLW, SIGNAL(itemClicked(QListWidgetItem*)),
67 this, SLOT(suggestionChanged(QListWidgetItem*)));
69 wordED->setReadOnly(true);
71 bc().setPolicy(ButtonPolicy::NoRepeatedApplyReadOnlyPolicy);
72 bc().setCancel(closePB);
76 GuiSpellchecker::~GuiSpellchecker()
82 void GuiSpellchecker::suggestionChanged(QListWidgetItem * item)
84 if (replaceCO->count() != 0)
85 replaceCO->setItemText(0, item->text());
87 replaceCO->addItem(item->text());
89 replaceCO->setCurrentIndex(0);
93 void GuiSpellchecker::replaceChanged(const QString & str)
95 if (suggestionsLW->currentItem()
96 && suggestionsLW->currentItem()->text() == str)
99 for (int i = 0; i != suggestionsLW->count(); ++i) {
100 if (suggestionsLW->item(i)->text() == str) {
101 suggestionsLW->setCurrentRow(i);
108 void GuiSpellchecker::reject()
115 void GuiSpellchecker::updateContents()
117 // The clauses below are needed because the spellchecker
118 // has many flaws (see bugs 1950, 2218).
119 // Basically, we have to distinguish the case where a
120 // spellcheck has already been performed for the whole
121 // document (exitEarly() == true, isVisible() == false)
122 // from the rest (exitEarly() == false, isVisible() == true).
123 // FIXME: rewrite the whole beast!
124 static bool check_after_early_exit;
126 // a spellcheck has already been performed,
128 check_after_early_exit = true;
130 else if (isVisible()) {
131 // the above check triggers a second update,
132 // and isVisible() is true then. Prevent a
133 // second check that skips the first word
134 if (check_after_early_exit)
135 // don't check, but reset the bool.
136 // business as usual after this.
137 check_after_early_exit = false;
139 // perform spellcheck (default case)
145 void GuiSpellchecker::accept()
151 void GuiSpellchecker::add()
157 void GuiSpellchecker::ignore()
163 void GuiSpellchecker::replace()
165 replace(qstring_to_ucs4(replaceCO->currentText()));
169 void GuiSpellchecker::partialUpdate(int state)
172 case SPELL_PROGRESSED:
173 spellcheckPR->setValue(getProgress());
176 case SPELL_FOUND_WORD: {
177 wordED->setText(toqstr(getWord()));
178 suggestionsLW->clear();
181 while (!(w = getSuggestion()).empty())
182 suggestionsLW->addItem(toqstr(w));
184 if (suggestionsLW->count() == 0)
185 suggestionChanged(new QListWidgetItem(wordED->text()));
187 suggestionChanged(suggestionsLW->item(0));
189 suggestionsLW->setCurrentRow(0);
196 static SpellBase * createSpeller(BufferParams const & bp)
198 string lang = lyxrc.spellchecker_use_alt_lang
199 ? lyxrc.spellchecker_alt_lang
200 : bp.language->code();
202 #if defined(USE_ASPELL)
203 return new ASpell(bp, lang);
205 return new SpellBase;
209 bool GuiSpellchecker::initialiseParams(string const &)
211 LYXERR(Debug::GUI, "Spellchecker::initialiseParams");
213 speller_ = createSpeller(buffer().params());
217 // reset values to initial
222 bool const success = speller_->error().empty();
225 Alert::error(_("Spellchecker error"),
226 _("The spellchecker could not be started\n")
227 + speller_->error());
236 void GuiSpellchecker::clearParams()
238 LYXERR(Debug::GUI, "Spellchecker::clearParams");
244 static bool isLetter(DocIterator const & dit)
246 return dit.inTexted()
247 && dit.inset().allowSpellCheck()
248 && dit.pos() != dit.lastpos()
249 && (dit.paragraph().isLetter(dit.pos())
250 // We want to pass the ' and escape chars to ispell
251 || contains(from_utf8(lyxrc.spellchecker_esc_chars + '\''),
252 dit.paragraph().getChar(dit.pos())))
253 && !dit.paragraph().isDeleted(dit.pos());
257 static WordLangTuple nextWord(Cursor & cur, ptrdiff_t & progress)
259 BufferParams const & bp = cur.bv().buffer().params();
261 bool ignoreword = false;
266 while (cur.depth()) {
273 lang_code = cur.paragraph().getFontSettings(bp, cur.pos()).language()->code();
275 // Insets like optional hyphens and ligature
276 // break are part of a word.
277 if (!cur.paragraph().isInset(cur.pos())) {
278 char_type const c = cur.paragraph().getChar(cur.pos());
283 } else { // !isLetter(cur)
285 if (!word.empty() && !ignoreword) {
287 return WordLangTuple(word, lang_code);
296 return WordLangTuple(docstring(), string());
300 void GuiSpellchecker::check()
302 LYXERR(Debug::GUI, "Check the spelling of a word");
304 SpellBase::Result res = SpellBase::OK;
306 Cursor cur = bufferview()->cursor();
307 while (cur && cur.pos() && isLetter(cur))
312 DocIterator it = doc_iterator_begin(&buffer());
313 for (start = 1; it != cur; it.forwardPos())
316 for (total = start; it; it.forwardPos())
321 while (res == SpellBase::OK || res == SpellBase::IGNORED_WORD) {
322 word_ = nextWord(cur, start);
325 if (getWord().empty()) {
333 // Update slider if and only if value has changed
334 float progress = total ? float(start)/total : 1;
335 newvalue_ = int(100.0 * progress);
336 if (newvalue_!= oldval_) {
337 LYXERR(Debug::GUI, "Updating spell progress.");
340 partialUpdate(SPELL_PROGRESSED);
343 // speller might be dead ...
347 res = speller_->check(word_);
349 // ... or it might just be reporting an error
354 LYXERR(Debug::GUI, "Found word \"" << to_utf8(getWord()) << "\"");
356 int const size = cur.selEnd().pos() - cur.selBegin().pos();
358 BufferView * bv = const_cast<BufferView *>(bufferview());
359 bv->putSelectionAt(cur, size, false);
360 // FIXME: if we used a lfun like in find/replace, dispatch would do
362 // FIXME: this Controller is very badly designed...
363 bv->processUpdateFlags(Update::Force | Update::FitCursor);
366 if (res != SpellBase::OK && res != SpellBase::IGNORED_WORD) {
367 LYXERR(Debug::GUI, "Found a word needing checking.");
368 partialUpdate(SPELL_FOUND_WORD);
373 bool GuiSpellchecker::checkAlive()
375 if (speller_->alive() && speller_->error().empty())
379 if (speller_->error().empty())
380 message = _("The spellchecker has died for some reason.\n"
381 "Maybe it has been killed.");
383 message = _("The spellchecker has failed.\n") + speller_->error();
387 Alert::error(_("The spellchecker has failed"), message);
392 void GuiSpellchecker::showSummary()
394 if (!checkAlive() || count_ == 0) {
401 message = bformat(_("%1$d words checked."), count_);
403 message = _("One word checked.");
406 Alert::information(_("Spelling check completed"), message);
410 void GuiSpellchecker::replace(docstring const & replacement)
412 LYXERR(Debug::GUI, "GuiSpellchecker::replace("
413 << to_utf8(replacement) << ")");
414 BufferView * bv = const_cast<BufferView *>(bufferview());
415 cap::replaceSelectionWithString(bv->cursor(), replacement, true);
416 bv->buffer().markDirty();
417 // If we used an LFUN, we would not need that
418 bv->processUpdateFlags(Update::Force | Update::FitCursor);
425 void GuiSpellchecker::replaceAll(docstring const & replacement)
428 replace(replacement);
432 void GuiSpellchecker::insert()
434 speller_->insert(word_);
439 docstring GuiSpellchecker::getSuggestion() const
441 return speller_->nextMiss();
445 docstring GuiSpellchecker::getWord() const
451 void GuiSpellchecker::ignoreAll()
453 speller_->accept(word_);
458 Dialog * createGuiSpellchecker(GuiView & lv) { return new GuiSpellchecker(lv); }
460 } // namespace frontend
463 #include "moc_GuiSpellchecker.cpp"