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"
|
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"
|
2004-02-13 07:30:59 +00:00
|
|
|
#include "cursor.h"
|
2004-03-25 09:16:36 +00:00
|
|
|
#include "CutAndPaste.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"
|
2003-11-04 00:26:50 +00:00
|
|
|
#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-09-09 22:13:45 +00:00
|
|
|
using lyx::support::bformat;
|
2003-06-30 23:56:22 +00:00
|
|
|
|
2004-01-28 16:21:29 +00:00
|
|
|
using std::advance;
|
|
|
|
using std::distance;
|
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
|
|
|
|
2004-03-31 19:51:55 +00:00
|
|
|
ControlSpellchecker::ControlSpellchecker(Dialog & parent)
|
|
|
|
: Dialog::Controller(parent),
|
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
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2004-03-31 19:51:55 +00:00
|
|
|
} // namespace anon
|
2003-02-21 15:36:29 +00:00
|
|
|
|
2003-08-04 09:06:35 +00:00
|
|
|
|
2004-03-31 19:51:55 +00:00
|
|
|
bool ControlSpellchecker::initialiseParams(std::string const &)
|
2003-02-17 18:40:04 +00:00
|
|
|
{
|
2004-03-31 19:51:55 +00:00
|
|
|
lyxerr[Debug::GUI] << "Spellchecker::initialiseParams" << endl;
|
2003-02-17 18:40:04 +00:00
|
|
|
|
2004-03-31 19:51:55 +00:00
|
|
|
speller_.reset(getSpeller(kernel().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;
|
|
|
|
|
2004-03-31 19:51:55 +00:00
|
|
|
bool const success = speller_->error().empty();
|
2003-02-17 18:40:04 +00:00
|
|
|
|
2004-03-31 19:51:55 +00:00
|
|
|
if (!success) {
|
|
|
|
Alert::error(_("The spell-checker could not be started"),
|
|
|
|
speller_->error());
|
|
|
|
speller_.reset(0);
|
|
|
|
}
|
2003-02-17 18:40:04 +00:00
|
|
|
|
2004-03-31 19:51:55 +00:00
|
|
|
return success;
|
2003-02-17 18:40:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-03-31 19:51:55 +00:00
|
|
|
void ControlSpellchecker::clearParams()
|
2003-02-17 18:40:04 +00:00
|
|
|
{
|
2004-03-31 19:51:55 +00:00
|
|
|
lyxerr[Debug::GUI] << "Spellchecker::clearParams" << endl;
|
2003-02-17 18:40:04 +00:00
|
|
|
speller_.reset(0);
|
2001-07-13 11:50:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-11-04 00:26:50 +00:00
|
|
|
namespace {
|
|
|
|
|
2004-03-31 19:11:56 +00:00
|
|
|
bool isLetter(DocIterator const & cur)
|
2003-11-04 00:26:50 +00:00
|
|
|
{
|
2004-03-31 17:58:11 +00:00
|
|
|
return cur.inTexted()
|
|
|
|
&& cur.inset().allowSpellCheck()
|
|
|
|
&& cur.pos() != cur.lastpos()
|
2004-03-25 09:16:36 +00:00
|
|
|
&& cur.paragraph().isLetter(cur.pos())
|
|
|
|
&& !isDeletedText(cur.paragraph(), cur.pos());
|
2003-11-04 00:26:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-03-31 19:11:56 +00:00
|
|
|
WordLangTuple nextWord(DocIterator & cur, ptrdiff_t & progress,
|
2004-03-25 09:16:36 +00:00
|
|
|
BufferParams & bp)
|
2003-11-04 00:26:50 +00:00
|
|
|
{
|
|
|
|
// skip until we have real text (will jump paragraphs)
|
2004-03-31 17:58:11 +00:00
|
|
|
for (; cur.size() && !isLetter(cur); cur.forwardPos());
|
2004-03-25 09:16:36 +00:00
|
|
|
++progress;
|
2004-01-26 17:00:09 +00:00
|
|
|
|
2004-03-25 09:16:36 +00:00
|
|
|
// hit end
|
|
|
|
if (cur.empty())
|
2003-11-04 00:26:50 +00:00
|
|
|
return WordLangTuple(string(), string());
|
|
|
|
|
2004-03-25 09:16:36 +00:00
|
|
|
string lang_code = cur.paragraph().
|
|
|
|
getFontSettings(bp, cur.pos()).language()->code();
|
2003-11-04 00:26:50 +00:00
|
|
|
string str;
|
|
|
|
// and find the end of the word (insets like optional hyphens
|
|
|
|
// and ligature break are part of a word)
|
2004-03-31 17:58:11 +00:00
|
|
|
for (; cur && isLetter(cur); cur.forwardPos(), ++progress) {
|
2004-03-25 09:16:36 +00:00
|
|
|
if (!cur.paragraph().isInset(cur.pos()))
|
|
|
|
str += cur.paragraph().getChar(cur.pos());
|
2003-11-04 10:30:36 +00:00
|
|
|
}
|
2003-11-04 00:26:50 +00:00
|
|
|
|
|
|
|
return WordLangTuple(str, lang_code);
|
|
|
|
}
|
|
|
|
|
2004-03-25 09:16:36 +00:00
|
|
|
} // namespace anon
|
2003-11-04 00:26:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
2004-03-31 19:51:55 +00:00
|
|
|
DocIterator cur = kernel().bufferview()->cursor();
|
2003-11-04 00:26:50 +00:00
|
|
|
|
2004-03-25 09:16:36 +00:00
|
|
|
ptrdiff_t start = 0, total = 0;
|
2004-03-31 19:51:55 +00:00
|
|
|
DocIterator it = DocIterator(kernel().buffer().inset());
|
2004-03-25 09:16:36 +00:00
|
|
|
for (start = 0; it != cur; it.forwardPos())
|
2004-04-03 08:37:12 +00:00
|
|
|
++start;
|
2003-11-04 00:26:50 +00:00
|
|
|
|
2004-03-31 17:58:11 +00:00
|
|
|
for (total = start; it; it.forwardPos())
|
2004-04-03 08:37:12 +00:00
|
|
|
++total;
|
2004-03-25 09:16:36 +00:00
|
|
|
|
2004-03-31 17:58:11 +00:00
|
|
|
for (; cur && isLetter(cur); cur.forwardPos())
|
2004-03-25 09:16:36 +00:00
|
|
|
++start;
|
2002-03-21 21:21:28 +00:00
|
|
|
|
2004-03-31 19:51:55 +00:00
|
|
|
BufferParams & bufferparams = kernel().buffer().params();
|
|
|
|
|
2003-08-04 09:06:35 +00:00
|
|
|
while (res == SpellBase::OK || res == SpellBase::IGNORE) {
|
2004-03-31 19:51:55 +00:00
|
|
|
word_ = nextWord(cur, start, bufferparams);
|
2002-03-21 21:21:28 +00:00
|
|
|
|
2003-02-17 18:40:04 +00:00
|
|
|
// end of document
|
2004-03-31 19:51:55 +00:00
|
|
|
if (getWord().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
|
2004-03-31 19:51:55 +00:00
|
|
|
dialog().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
|
|
|
|
2004-03-31 19:51:55 +00:00
|
|
|
lyxerr[Debug::GUI] << "Found word \"" << getWord() << "\"" << endl;
|
2003-02-17 18:40:04 +00:00
|
|
|
|
2004-03-31 19:51:55 +00:00
|
|
|
if (getWord().empty()) {
|
2003-02-17 18:40:04 +00:00
|
|
|
showSummary();
|
|
|
|
return;
|
|
|
|
}
|
2001-07-13 11:50:39 +00:00
|
|
|
|
2004-03-31 19:51:55 +00:00
|
|
|
int const size = getWord().size();
|
|
|
|
kernel().bufferview()->putSelectionAt(cur, size, true);
|
2004-02-13 07:30:59 +00:00
|
|
|
|
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;
|
2004-03-31 19:51:55 +00:00
|
|
|
dialog().view().partialUpdate(SPELL_FOUND_WORD);
|
2003-02-17 18:40:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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.");
|
|
|
|
|
2004-03-31 19:51:55 +00:00
|
|
|
dialog().CancelButton();
|
2003-02-17 18:40:04 +00:00
|
|
|
|
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) {
|
2004-03-31 19:51:55 +00:00
|
|
|
dialog().CancelButton();
|
2003-02-17 18:40:04 +00:00
|
|
|
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.");
|
|
|
|
|
2004-03-31 19:51:55 +00:00
|
|
|
dialog().CancelButton();
|
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)
|
|
|
|
{
|
2004-03-31 19:51:55 +00:00
|
|
|
lyxerr << "ControlSpellchecker::replace("
|
|
|
|
<< replacement << ")" << std::endl;
|
|
|
|
BufferView & bufferview = *kernel().bufferview();
|
|
|
|
lyx::cap::replaceWord(bufferview.cursor(), replacement);
|
|
|
|
kernel().buffer().markDirty();
|
|
|
|
bufferview.update();
|
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();
|
|
|
|
}
|