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>
|
|
|
|
|
|
|
|
#ifdef __GNUG__
|
|
|
|
#pragma implementation
|
|
|
|
#endif
|
|
|
|
|
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"
|
2002-08-06 01:04:34 +00:00
|
|
|
#include "frontends/Alert.h"
|
2002-05-29 16:21:03 +00:00
|
|
|
#include "support/lstrings.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"
|
2002-05-29 16:21:03 +00:00
|
|
|
#endif
|
2001-07-13 11:50:39 +00:00
|
|
|
|
2002-08-20 13:00:25 +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),
|
2001-07-17 09:00:17 +00:00
|
|
|
rtl_(false), newval_(0.0), oldval_(0), newvalue_(0), count_(0),
|
2002-08-04 23:11:50 +00:00
|
|
|
stop_(false), speller_(0)
|
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
|
|
|
{
|
2002-10-21 17:38:09 +00:00
|
|
|
if (speller_)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// 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();
|
|
|
|
|
|
|
|
speller_ = new PSpell(buffer()->params, tmp);
|
|
|
|
} 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();
|
|
|
|
|
|
|
|
speller_ = 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-10-21 17:38:09 +00:00
|
|
|
|
|
|
|
if (lyxrc.isp_use_alt_lang) {
|
|
|
|
Language const * lang = languages.getLanguage(tmp);
|
|
|
|
if (lang)
|
|
|
|
rtl_ = lang->RightToLeft();
|
|
|
|
} else {
|
|
|
|
rtl_ = buffer()->params.language->RightToLeft();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!speller_->error().empty()) {
|
|
|
|
emergency_exit_ = true;
|
|
|
|
Alert::alert("The spellchecker has failed", speller_->error());
|
|
|
|
clearParams();
|
|
|
|
return;
|
2001-07-13 11:50:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ControlSpellchecker::check()
|
|
|
|
{
|
2002-08-04 23:11:50 +00:00
|
|
|
SpellBase::Result res = SpellBase::OK;
|
2001-07-13 11:50:39 +00:00
|
|
|
stop_ = false;
|
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
|
|
|
|
2002-08-04 23:11:50 +00:00
|
|
|
while ((res == SpellBase::OK || res == SpellBase::IGNORE) && !stop_) {
|
2002-08-12 20:17:41 +00:00
|
|
|
word_ = bufferview()->nextWord(newval_);
|
2002-03-21 21:21:28 +00:00
|
|
|
|
2002-08-06 22:38:44 +00:00
|
|
|
if (word_.word().empty()) {
|
2001-07-17 09:00:17 +00:00
|
|
|
clearParams();
|
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_) {
|
|
|
|
oldval_ = newvalue_;
|
|
|
|
// set progress bar
|
|
|
|
view().partialUpdate(0);
|
|
|
|
}
|
2002-03-21 21:21:28 +00:00
|
|
|
|
2002-11-12 13:37:34 +00:00
|
|
|
if (!speller_ || !speller_->alive()) {
|
2002-08-06 22:38:44 +00:00
|
|
|
clearParams();
|
|
|
|
stop();
|
|
|
|
return;
|
|
|
|
}
|
2002-03-21 21:21:28 +00:00
|
|
|
|
2002-08-04 23:11:50 +00:00
|
|
|
res = speller_->check(word_);
|
2001-07-13 11:50:39 +00:00
|
|
|
}
|
2002-03-21 21:21:28 +00:00
|
|
|
|
2002-08-06 22:38:44 +00:00
|
|
|
if (!stop_ && !word_.word().empty())
|
2002-08-12 20:17:41 +00:00
|
|
|
bufferview()->selectLastWord();
|
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) {
|
2001-07-13 11:50:39 +00:00
|
|
|
view().partialUpdate(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
{
|
2002-08-04 23:11:50 +00:00
|
|
|
string miss(speller_->nextMiss());
|
2002-03-21 21:21:28 +00:00
|
|
|
|
2002-08-04 23:11:50 +00:00
|
|
|
if (rtl_)
|
|
|
|
std::reverse(miss.begin(), miss.end());
|
|
|
|
|
|
|
|
return miss;
|
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
|
|
|
{
|
2002-08-06 22:38:44 +00:00
|
|
|
string tmp = word_.word();
|
2002-08-04 23:11:50 +00:00
|
|
|
if (rtl_)
|
|
|
|
std::reverse(tmp.begin(), tmp.end());
|
2001-07-13 11:50:39 +00:00
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ControlSpellchecker::ignoreAll()
|
|
|
|
{
|
|
|
|
speller_->accept(word_);
|
|
|
|
check();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ControlSpellchecker::stop()
|
|
|
|
{
|
|
|
|
stop_ = true;
|
2002-08-12 20:17:41 +00:00
|
|
|
bufferview()->endOfSpellCheck();
|
2001-07-13 11:50:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-07-17 09:00:17 +00:00
|
|
|
void ControlSpellchecker::clearParams()
|
2001-07-13 11:50:39 +00:00
|
|
|
{
|
2002-08-06 23:16:27 +00:00
|
|
|
if (!speller_)
|
|
|
|
return;
|
2002-03-21 21:21:28 +00:00
|
|
|
|
2001-07-13 11:50:39 +00:00
|
|
|
if (speller_->alive()) {
|
|
|
|
speller_->close();
|
|
|
|
message_ = tostr(count_);
|
|
|
|
if (count_ != 1) {
|
|
|
|
message_ += _(" words checked.");
|
|
|
|
} else {
|
|
|
|
message_ += _(" word checked.");
|
|
|
|
}
|
|
|
|
message_ = "\n" + message_;
|
|
|
|
message_ = _("Spellchecking completed! ") + message_;
|
2001-07-17 09:00:17 +00:00
|
|
|
|
2001-07-13 11:50:39 +00:00
|
|
|
} else {
|
2001-10-22 21:17:06 +00:00
|
|
|
message_ = speller_->error();
|
2001-07-13 11:50:39 +00:00
|
|
|
speller_->cleanUp();
|
2001-10-22 21:17:06 +00:00
|
|
|
if (message_.empty())
|
|
|
|
message_ = _("The spell checker has died for some reason.\n"
|
|
|
|
"Maybe it has been killed.");
|
2001-09-27 13:10:28 +00:00
|
|
|
|
|
|
|
// make sure that the dialog is not launched
|
|
|
|
emergency_exit_ = true;
|
2002-08-06 01:04:34 +00:00
|
|
|
Alert::alert("The spellchecker has failed", message_);
|
2001-07-13 11:50:39 +00:00
|
|
|
}
|
2002-03-21 21:21:28 +00:00
|
|
|
|
2001-07-17 09:00:17 +00:00
|
|
|
delete speller_;
|
2002-03-21 21:21:28 +00:00
|
|
|
|
2002-08-12 20:17:41 +00:00
|
|
|
bufferview()->endOfSpellCheck();
|
2001-07-13 11:50:39 +00:00
|
|
|
|
2002-01-07 17:57:21 +00:00
|
|
|
// show closing message if any words were checked.
|
|
|
|
if (count_ > 0)
|
|
|
|
view().partialUpdate(2);
|
2001-07-13 11:50:39 +00:00
|
|
|
|
|
|
|
// reset values to initial
|
|
|
|
rtl_ = false;
|
|
|
|
newval_ = 0.0;
|
|
|
|
oldval_ = 0;
|
|
|
|
newvalue_ = 0;
|
|
|
|
count_ = 0;
|
2002-01-07 17:57:21 +00:00
|
|
|
message_.erase();
|
2001-07-13 11:50:39 +00:00
|
|
|
stop_ = false;
|
2001-07-17 09:00:17 +00:00
|
|
|
speller_ = 0;
|
2001-07-13 11:50:39 +00:00
|
|
|
}
|