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"
|
2002-05-29 16:21:03 +00:00
|
|
|
#endif
|
2001-07-13 11:50:39 +00:00
|
|
|
|
2002-11-21 18:33:09 +00:00
|
|
|
#include "frontends/Alert.h"
|
|
|
|
|
|
|
|
#include "BoostFormat.h"
|
2002-08-20 13:00:25 +00:00
|
|
|
|
2003-02-17 18:40:04 +00:00
|
|
|
using std::endl;
|
|
|
|
|
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-02-21 15:36:29 +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
|
|
|
|
2002-10-21 17:38:09 +00:00
|
|
|
// create spell object
|
|
|
|
string tmp;
|
2001-07-13 11:50:39 +00:00
|
|
|
#ifdef USE_PSPELL
|
2002-10-21 17:38:09 +00:00
|
|
|
if (lyxrc.use_pspell) {
|
|
|
|
tmp = (lyxrc.isp_use_alt_lang) ?
|
|
|
|
lyxrc.isp_alt_lang : buffer()->params.language->code();
|
2002-11-21 18:33:09 +00:00
|
|
|
|
2003-02-17 18:40:04 +00:00
|
|
|
speller_.reset(new PSpell(buffer()->params, tmp));
|
2002-10-21 17:38:09 +00:00
|
|
|
} else {
|
2001-07-16 10:03:38 +00:00
|
|
|
#endif
|
2002-10-21 17:38:09 +00:00
|
|
|
tmp = (lyxrc.isp_use_alt_lang) ?
|
|
|
|
lyxrc.isp_alt_lang : buffer()->params.language->lang();
|
2002-11-21 18:33:09 +00:00
|
|
|
|
2003-02-17 18:40:04 +00:00
|
|
|
speller_.reset(new ISpell(buffer()->params, tmp));
|
2001-07-16 10:03:38 +00:00
|
|
|
#ifdef USE_PSPELL
|
2002-10-21 17:38:09 +00:00
|
|
|
}
|
2001-07-13 11:50:39 +00:00
|
|
|
#endif
|
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.");
|
|
|
|
|
|
|
|
Alert::alert(_("The spell-checker has failed"), message);
|
|
|
|
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);
|
|
|
|
bufferview()->update(text, BufferView::SELECT);
|
2002-03-21 21:21:28 +00:00
|
|
|
|
2003-02-17 18:40:04 +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;
|
|
|
|
|
|
|
|
if (!word_.word().empty()) {
|
2002-08-12 20:17:41 +00:00
|
|
|
bufferview()->selectLastWord();
|
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);
|
|
|
|
|
|
|
|
Alert::alert(_("The spell-checker has failed"), message);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ControlSpellchecker::showSummary()
|
|
|
|
{
|
|
|
|
if (!checkAlive() || count_ == 0) {
|
|
|
|
view().hide();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
string message;
|
|
|
|
|
|
|
|
#if USE_BOOST_FORMAT
|
|
|
|
if (count_ != 1) {
|
2003-03-23 18:40:32 +00:00
|
|
|
boost::format fmter(_("%1$d words checked."));
|
2003-02-17 18:40:04 +00:00
|
|
|
fmter % count_;
|
|
|
|
message += fmter.str();
|
|
|
|
} else {
|
|
|
|
message += _("One word checked.");
|
2001-07-13 11:50:39 +00:00
|
|
|
}
|
2003-02-17 18:40:04 +00:00
|
|
|
#else
|
|
|
|
if (count_ != 1) {
|
2003-03-23 18:40:32 +00:00
|
|
|
message += tostr(count_) + _(" words checked.");
|
2003-02-17 18:40:04 +00:00
|
|
|
} else {
|
|
|
|
message = _("One word checked.");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
view().hide();
|
|
|
|
Alert::alert(_("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();
|
|
|
|
}
|
|
|
|
|
|
|
|
|