2002-08-04 23:11:50 +00:00
|
|
|
/**
|
2001-07-13 11:50:39 +00:00
|
|
|
* \file ControlSpellchecker.C
|
2002-09-05 15:14:23 +00:00
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
* Licence details can be found in the file COPYING.
|
2002-08-04 23:11:50 +00:00
|
|
|
*
|
2002-10-21 17:38:09 +00:00
|
|
|
* \author Edwin Leuven
|
2002-09-05 14:10:50 +00:00
|
|
|
*
|
2003-08-23 00:17:00 +00:00
|
|
|
* Full author contact details are available in file CREDITS.
|
2001-07-13 11:50:39 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
2002-05-29 16:21:03 +00:00
|
|
|
#include "ControlSpellchecker.h"
|
2002-08-14 19:19:47 +00:00
|
|
|
#include "ViewBase.h"
|
2003-09-09 11:24:33 +00:00
|
|
|
|
2001-07-13 11:50:39 +00:00
|
|
|
#include "buffer.h"
|
2003-09-09 11:24:33 +00:00
|
|
|
#include "bufferparams.h"
|
2001-07-13 11:50:39 +00:00
|
|
|
#include "BufferView.h"
|
2003-11-04 00:26:50 +00:00
|
|
|
#include "bufferview_funcs.h"
|
2003-09-09 11:24:33 +00:00
|
|
|
#include "debug.h"
|
2001-07-13 11:50:39 +00:00
|
|
|
#include "gettext.h"
|
|
|
|
#include "language.h"
|
2002-08-14 19:19:47 +00:00
|
|
|
#include "lyxrc.h"
|
2002-05-29 16:21:03 +00:00
|
|
|
|
2003-11-04 00:26:50 +00:00
|
|
|
#include "PosIterator.h"
|
|
|
|
#include "paragraph.h"
|
|
|
|
|
2002-08-04 23:11:50 +00:00
|
|
|
#include "ispell.h"
|
2001-07-13 11:50:39 +00:00
|
|
|
#ifdef USE_PSPELL
|
2002-08-04 23:11:50 +00:00
|
|
|
# include "pspell.h"
|
2003-03-26 01:20:25 +00:00
|
|
|
#else
|
|
|
|
#ifdef USE_ASPELL
|
|
|
|
# include "aspell_local.h"
|
|
|
|
#endif
|
2002-05-29 16:21:03 +00:00
|
|
|
#endif
|
2001-07-13 11:50:39 +00:00
|
|
|
|
2003-05-13 14:36:24 +00:00
|
|
|
#include "support/tostr.h"
|
|
|
|
|
2002-11-21 18:33:09 +00:00
|
|
|
#include "frontends/Alert.h"
|
|
|
|
|
2003-10-06 15:43:21 +00:00
|
|
|
|
2003-09-09 22:13:45 +00:00
|
|
|
using lyx::support::bformat;
|
2003-06-30 23:56:22 +00:00
|
|
|
|
2003-02-17 18:40:04 +00:00
|
|
|
using std::endl;
|
2003-10-06 15:43:21 +00:00
|
|
|
using std::string;
|
2003-02-17 18:40:04 +00:00
|
|
|
|
2003-05-13 09:48:57 +00:00
|
|
|
|
2001-07-13 11:50:39 +00:00
|
|
|
ControlSpellchecker::ControlSpellchecker(LyXView & lv, Dialogs & d)
|
2002-01-16 14:47:58 +00:00
|
|
|
: ControlDialogBD(lv, d),
|
2003-11-04 00:26:50 +00:00
|
|
|
oldval_(0), newvalue_(0), count_(0)
|
2003-02-17 18:40:04 +00:00
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
|
|
ControlSpellchecker::~ControlSpellchecker()
|
2002-06-18 15:44:30 +00:00
|
|
|
{}
|
2001-07-13 11:50:39 +00:00
|
|
|
|
|
|
|
|
2001-07-17 09:00:17 +00:00
|
|
|
void ControlSpellchecker::setParams()
|
2001-07-13 11:50:39 +00:00
|
|
|
{
|
2003-02-17 18:40:04 +00:00
|
|
|
lyxerr[Debug::GUI] << "spell setParams" << endl;
|
|
|
|
startSession();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ControlSpellchecker::clearParams()
|
|
|
|
{
|
|
|
|
lyxerr[Debug::GUI] << "spell clearParams" << endl;
|
|
|
|
endSession();
|
|
|
|
}
|
|
|
|
|
2003-08-04 09:06:35 +00:00
|
|
|
|
2003-03-26 01:20:25 +00:00
|
|
|
namespace {
|
|
|
|
|
2003-11-04 00:26:50 +00:00
|
|
|
|
2003-03-26 01:20:25 +00:00
|
|
|
SpellBase * getSpeller(BufferParams const & bp)
|
|
|
|
{
|
|
|
|
string lang = (lyxrc.isp_use_alt_lang)
|
|
|
|
? lyxrc.isp_alt_lang
|
|
|
|
: bp.language->code();
|
|
|
|
|
|
|
|
#ifdef USE_ASPELL
|
|
|
|
if (lyxrc.use_spell_lib)
|
|
|
|
return new ASpell(bp, lang);
|
|
|
|
#endif
|
|
|
|
#ifdef USE_PSPELL
|
|
|
|
if (lyxrc.use_spell_lib)
|
|
|
|
return new PSpell(bp, lang);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
lang = (lyxrc.isp_use_alt_lang) ?
|
|
|
|
lyxrc.isp_alt_lang : bp.language->lang();
|
|
|
|
|
|
|
|
return new ISpell(bp, lang);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2003-02-21 15:36:29 +00:00
|
|
|
|
2003-08-04 09:06:35 +00:00
|
|
|
|
2003-02-17 18:40:04 +00:00
|
|
|
void ControlSpellchecker::startSession()
|
|
|
|
{
|
|
|
|
lyxerr[Debug::GUI] << "spell startSession" << endl;
|
|
|
|
|
|
|
|
if (speller_.get()) {
|
|
|
|
lyxerr[Debug::GUI] << "startSession: speller exists" << endl;
|
|
|
|
speller_.reset(0);
|
2002-10-21 17:38:09 +00:00
|
|
|
return;
|
2003-02-17 18:40:04 +00:00
|
|
|
}
|
2002-11-21 18:33:09 +00:00
|
|
|
|
2003-09-09 09:47:59 +00:00
|
|
|
speller_.reset(getSpeller(buffer()->params()));
|
2002-11-21 18:33:09 +00:00
|
|
|
|
2003-02-17 18:40:04 +00:00
|
|
|
// reset values to initial
|
|
|
|
oldval_ = 0;
|
|
|
|
newvalue_ = 0;
|
|
|
|
count_ = 0;
|
|
|
|
emergency_exit_ = false;
|
|
|
|
|
|
|
|
// start off the check
|
|
|
|
if (speller_->error().empty()) {
|
|
|
|
check();
|
2002-10-21 17:38:09 +00:00
|
|
|
return;
|
2001-07-13 11:50:39 +00:00
|
|
|
}
|
2003-02-17 18:40:04 +00:00
|
|
|
|
|
|
|
emergency_exit_ = true;
|
|
|
|
string message = speller_->error();
|
|
|
|
if (message.empty())
|
|
|
|
message = _("The spell-checker could not be started.\n"
|
|
|
|
"Maybe it is mis-configured.");
|
|
|
|
|
2003-03-29 11:34:53 +00:00
|
|
|
Alert::error(_("The spell-checker has failed"), message);
|
2003-02-17 18:40:04 +00:00
|
|
|
speller_.reset(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ControlSpellchecker::endSession()
|
|
|
|
{
|
|
|
|
lyxerr[Debug::GUI] << "spell endSession" << endl;
|
|
|
|
|
|
|
|
emergency_exit_ = true;
|
|
|
|
|
|
|
|
if (!speller_.get()) {
|
|
|
|
lyxerr[Debug::GUI] << "endSession with no speller" << endl;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
speller_.reset(0);
|
2001-07-13 11:50:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-11-04 00:26:50 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
|
|
|
|
bool isLetter(PosIterator & cur)
|
|
|
|
{
|
|
|
|
return !cur.at_end()
|
|
|
|
&& cur.pit()->isLetter(cur.pos())
|
2003-11-07 09:40:49 +00:00
|
|
|
&& !isDeletedText(*cur.pit(), cur.pos())
|
|
|
|
&& (!cur.inset() || cur.inset()->allowSpellCheck());
|
2003-11-04 00:26:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
WordLangTuple nextWord(PosIterator & cur, PosIterator const & end,
|
|
|
|
int & progress, BufferParams & bp)
|
|
|
|
{
|
|
|
|
// skip until we have real text (will jump paragraphs)
|
|
|
|
for (; cur != end && !isLetter(cur); ++cur, ++progress);
|
|
|
|
|
|
|
|
if (cur == end)
|
|
|
|
return WordLangTuple(string(), string());
|
|
|
|
|
|
|
|
string lang_code = cur.pit()->getFontSettings(bp, cur.pos()).language()->code();
|
|
|
|
string str;
|
|
|
|
// and find the end of the word (insets like optional hyphens
|
|
|
|
// and ligature break are part of a word)
|
2003-11-04 10:30:36 +00:00
|
|
|
for (; cur != end && isLetter(cur); ++cur, ++progress) {
|
|
|
|
if (!cur.pit()->isInset(cur.pos()))
|
|
|
|
str += cur.pit()->getChar(cur.pos());
|
|
|
|
}
|
2003-11-04 00:26:50 +00:00
|
|
|
|
|
|
|
return WordLangTuple(str, lang_code);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} //namespace anon
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2001-07-13 11:50:39 +00:00
|
|
|
void ControlSpellchecker::check()
|
|
|
|
{
|
2003-02-17 18:40:04 +00:00
|
|
|
lyxerr[Debug::GUI] << "spell check a word" << endl;
|
|
|
|
|
2002-08-04 23:11:50 +00:00
|
|
|
SpellBase::Result res = SpellBase::OK;
|
2002-03-21 21:21:28 +00:00
|
|
|
|
2003-11-04 00:26:50 +00:00
|
|
|
PosIterator cur(*bufferview());
|
|
|
|
PosIterator const beg = buffer()->pos_iterator_begin();
|
|
|
|
PosIterator const end = buffer()->pos_iterator_end();
|
|
|
|
|
|
|
|
int start = distance(beg, cur);
|
|
|
|
int const total = start + distance(cur, end);
|
|
|
|
|
|
|
|
if (cur != buffer()->pos_iterator_begin())
|
|
|
|
for (; cur != end && isLetter(cur); ++cur, ++start);
|
2002-03-21 21:21:28 +00:00
|
|
|
|
2003-08-04 09:06:35 +00:00
|
|
|
while (res == SpellBase::OK || res == SpellBase::IGNORE) {
|
2003-11-04 00:26:50 +00:00
|
|
|
word_ = nextWord(cur, end, start, buffer()->params());
|
2002-03-21 21:21:28 +00:00
|
|
|
|
2003-02-17 18:40:04 +00:00
|
|
|
// end of document
|
|
|
|
if (word_.word().empty())
|
2001-07-13 11:50:39 +00:00
|
|
|
break;
|
2002-03-21 21:21:28 +00:00
|
|
|
|
2001-07-13 11:50:39 +00:00
|
|
|
++count_;
|
2001-07-17 09:00:17 +00:00
|
|
|
|
2001-07-13 11:50:39 +00:00
|
|
|
// Update slider if and only if value has changed
|
2003-11-04 00:26:50 +00:00
|
|
|
float progress = total ? float(start)/total : 1;
|
|
|
|
newvalue_ = int(100.0 * progress);
|
2001-07-13 11:50:39 +00:00
|
|
|
if (newvalue_!= oldval_) {
|
2003-02-17 18:40:04 +00:00
|
|
|
lyxerr[Debug::GUI] << "Updating spell progress." << endl;
|
2001-07-13 11:50:39 +00:00
|
|
|
oldval_ = newvalue_;
|
|
|
|
// set progress bar
|
2003-02-17 18:40:04 +00:00
|
|
|
view().partialUpdate(SPELL_PROGRESSED);
|
2001-07-13 11:50:39 +00:00
|
|
|
}
|
2002-03-21 21:21:28 +00:00
|
|
|
|
2003-02-17 18:40:04 +00:00
|
|
|
// speller might be dead ...
|
|
|
|
if (!checkAlive())
|
2002-08-06 22:38:44 +00:00
|
|
|
return;
|
2002-03-21 21:21:28 +00:00
|
|
|
|
2002-08-04 23:11:50 +00:00
|
|
|
res = speller_->check(word_);
|
2003-02-17 18:40:04 +00:00
|
|
|
|
|
|
|
// ... or it might just be reporting an error
|
|
|
|
if (!checkAlive())
|
|
|
|
return;
|
2001-07-13 11:50:39 +00:00
|
|
|
}
|
2002-03-21 21:21:28 +00:00
|
|
|
|
2003-02-17 18:40:04 +00:00
|
|
|
lyxerr[Debug::GUI] << "Found word \"" << word_.word() << "\"" << endl;
|
|
|
|
|
|
|
|
if (!word_.word().empty()) {
|
2003-11-04 00:26:50 +00:00
|
|
|
int const size = word_.word().size();
|
|
|
|
advance(cur, -size);
|
2004-01-14 17:21:39 +00:00
|
|
|
bufferview()->putSelectionAt(cur, size, false);
|
2003-11-04 00:26:50 +00:00
|
|
|
advance(cur, size);
|
2003-02-17 18:40:04 +00:00
|
|
|
} else {
|
|
|
|
showSummary();
|
|
|
|
endSession();
|
|
|
|
return;
|
|
|
|
}
|
2001-07-13 11:50:39 +00:00
|
|
|
|
|
|
|
// set suggestions
|
2002-08-04 23:11:50 +00:00
|
|
|
if (res != SpellBase::OK && res != SpellBase::IGNORE) {
|
2003-02-17 18:40:04 +00:00
|
|
|
lyxerr[Debug::GUI] << "Found a word needing checking." << endl;
|
|
|
|
view().partialUpdate(SPELL_FOUND_WORD);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool ControlSpellchecker::checkAlive()
|
|
|
|
{
|
|
|
|
if (speller_->alive() && speller_->error().empty())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
string message = speller_->error();
|
|
|
|
if (message.empty())
|
|
|
|
message = _("The spell-checker has died for some reason.\n"
|
|
|
|
"Maybe it has been killed.");
|
|
|
|
|
|
|
|
view().hide();
|
|
|
|
speller_.reset(0);
|
|
|
|
|
2003-03-29 11:34:53 +00:00
|
|
|
Alert::error(_("The spell-checker has failed"), message);
|
2003-02-17 18:40:04 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ControlSpellchecker::showSummary()
|
|
|
|
{
|
|
|
|
if (!checkAlive() || count_ == 0) {
|
|
|
|
view().hide();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
string message;
|
2003-05-13 09:48:57 +00:00
|
|
|
if (count_ != 1)
|
|
|
|
message = bformat(_("%1$s words checked."), tostr(count_));
|
|
|
|
else
|
2003-02-17 18:40:04 +00:00
|
|
|
message = _("One word checked.");
|
|
|
|
|
|
|
|
view().hide();
|
2003-03-29 11:34:53 +00:00
|
|
|
Alert::information(_("Spell-checking is complete"), message);
|
2001-07-13 11:50:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ControlSpellchecker::replace(string const & replacement)
|
|
|
|
{
|
2002-08-12 20:17:41 +00:00
|
|
|
bufferview()->replaceWord(replacement);
|
2002-08-06 22:38:44 +00:00
|
|
|
// fix up the count
|
2002-10-21 17:38:09 +00:00
|
|
|
--count_;
|
2001-07-13 11:50:39 +00:00
|
|
|
check();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ControlSpellchecker::replaceAll(string const & replacement)
|
|
|
|
{
|
|
|
|
// TODO: add to list
|
|
|
|
replace(replacement);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ControlSpellchecker::insert()
|
|
|
|
{
|
|
|
|
speller_->insert(word_);
|
2001-08-07 15:07:36 +00:00
|
|
|
check();
|
2001-07-13 11:50:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-10-25 09:16:22 +00:00
|
|
|
string const ControlSpellchecker::getSuggestion() const
|
2001-07-13 11:50:39 +00:00
|
|
|
{
|
2003-01-15 14:23:21 +00:00
|
|
|
return speller_->nextMiss();
|
2001-07-13 11:50:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-10-25 09:16:22 +00:00
|
|
|
string const ControlSpellchecker::getWord() const
|
2001-07-13 11:50:39 +00:00
|
|
|
{
|
2003-01-15 14:23:21 +00:00
|
|
|
return word_.word();
|
2001-07-13 11:50:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ControlSpellchecker::ignoreAll()
|
|
|
|
{
|
|
|
|
speller_->accept(word_);
|
|
|
|
check();
|
|
|
|
}
|