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
|
|
|
*
|
|
|
|
* 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"
|
2001-07-13 11:50:39 +00:00
|
|
|
#include "buffer.h"
|
|
|
|
#include "BufferView.h"
|
|
|
|
#include "gettext.h"
|
|
|
|
#include "language.h"
|
2002-08-14 19:19:47 +00:00
|
|
|
#include "lyxrc.h"
|
|
|
|
#include "lyxtext.h"
|
2003-02-17 18:40:04 +00:00
|
|
|
#include "debug.h"
|
2002-05-29 16:21:03 +00:00
|
|
|
|
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-06-30 23:56:22 +00:00
|
|
|
using namespace lyx::support;
|
|
|
|
|
2003-02-17 18:40:04 +00:00
|
|
|
using std::endl;
|
|
|
|
|
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-02-17 18:40:04 +00:00
|
|
|
newval_(0.0), oldval_(0), newvalue_(0), count_(0)
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
|
|
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 {
|
|
|
|
|
|
|
|
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-03-26 01:20:25 +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
|
|
|
|
newval_ = 0.0;
|
|
|
|
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;
|
|
|
|
|
|
|
|
bufferview()->endOfSpellCheck();
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
2002-02-04 11:30:25 +00:00
|
|
|
// clear any old selection
|
2002-08-12 20:17:41 +00:00
|
|
|
LyXText * text = bufferview()->getLyXText();
|
|
|
|
bufferview()->toggleSelection(true);
|
2003-08-04 09:06:35 +00:00
|
|
|
bufferview()->update();
|
2002-03-21 21:21:28 +00:00
|
|
|
|
2003-08-04 09:06:35 +00:00
|
|
|
while (res == SpellBase::OK || res == SpellBase::IGNORE) {
|
2002-08-12 20:17:41 +00:00
|
|
|
word_ = bufferview()->nextWord(newval_);
|
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
|
2002-08-06 22:38:44 +00:00
|
|
|
newvalue_ = int(100.0 * newval_);
|
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;
|
2003-08-07 10:12:49 +00:00
|
|
|
lyxerr << "Found word \"" << word_.word() << "\"" << endl;
|
2003-02-17 18:40:04 +00:00
|
|
|
|
|
|
|
if (!word_.word().empty()) {
|
2002-08-12 20:17:41 +00:00
|
|
|
bufferview()->selectLastWord();
|
2003-08-07 10:12:49 +00:00
|
|
|
bufferview()->fitCursor();
|
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();
|
|
|
|
}
|