lyx_mirror/src/frontends/controllers/ControlSpellchecker.C

221 lines
4.0 KiB
C++
Raw Normal View History

/**
* \file ControlSpellchecker.C
* This file is part of LyX, the document processor.
* Licence details can be found in the file COPYING.
*
* \author Edwin Leuven
*
* Full author contact details are available in file CREDITS
*/
#include <config.h>
#include "ControlSpellchecker.h"
#include "ViewBase.h"
#include "buffer.h"
#include "BufferView.h"
#include "gettext.h"
#include "language.h"
#include "lyxrc.h"
#include "lyxtext.h"
#include "ispell.h"
#ifdef USE_PSPELL
# include "pspell.h"
#endif
#include "frontends/Alert.h"
#include "BoostFormat.h"
ControlSpellchecker::ControlSpellchecker(LyXView & lv, Dialogs & d)
: ControlDialogBD(lv, d),
newval_(0.0), oldval_(0), newvalue_(0), count_(0),
stop_(false), speller_(0)
{}
void ControlSpellchecker::setParams()
{
if (speller_)
return;
// create spell object
string tmp;
#ifdef USE_PSPELL
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 {
#endif
tmp = (lyxrc.isp_use_alt_lang) ?
lyxrc.isp_alt_lang : buffer()->params.language->lang();
speller_ = new ISpell(buffer()->params, tmp);
#ifdef USE_PSPELL
}
#endif
if (!speller_->error().empty()) {
emergency_exit_ = true;
Alert::alert("The spellchecker has failed", speller_->error());
clearParams();
return;
}
}
void ControlSpellchecker::check()
{
SpellBase::Result res = SpellBase::OK;
stop_ = false;
// clear any old selection
LyXText * text = bufferview()->getLyXText();
bufferview()->toggleSelection(true);
bufferview()->update(text, BufferView::SELECT);
while ((res == SpellBase::OK || res == SpellBase::IGNORE) && !stop_) {
word_ = bufferview()->nextWord(newval_);
if (word_.word().empty()) {
clearParams();
break;
}
++count_;
// Update slider if and only if value has changed
newvalue_ = int(100.0 * newval_);
if (newvalue_!= oldval_) {
oldval_ = newvalue_;
// set progress bar
view().partialUpdate(0);
}
if (!speller_ || !speller_->alive()) {
clearParams();
stop();
return;
}
res = speller_->check(word_);
}
if (!stop_ && !word_.word().empty())
bufferview()->selectLastWord();
// set suggestions
if (res != SpellBase::OK && res != SpellBase::IGNORE) {
view().partialUpdate(1);
}
}
void ControlSpellchecker::replace(string const & replacement)
{
bufferview()->replaceWord(replacement);
// fix up the count
--count_;
check();
}
void ControlSpellchecker::replaceAll(string const & replacement)
{
// TODO: add to list
replace(replacement);
}
void ControlSpellchecker::insert()
{
speller_->insert(word_);
check();
}
string const ControlSpellchecker::getSuggestion() const
{
return speller_->nextMiss();
}
string const ControlSpellchecker::getWord() const
{
return word_.word();
}
void ControlSpellchecker::ignoreAll()
{
speller_->accept(word_);
check();
}
void ControlSpellchecker::stop()
{
stop_ = true;
bufferview()->endOfSpellCheck();
}
void ControlSpellchecker::clearParams()
{
if (!speller_)
return;
if (speller_->alive()) {
speller_->close();
message_ = string(_("Spellchecking completed!")) + '\n';
#if USE_BOOST_FORMAT
if (count_ != 1) {
boost::format fmter("%1$d words checked.");
fmter % count_;
message_ += fmter.str();
} else {
message_ += _("One word checked.");
}
#else
if (count_ != 1) {
message_ += tostr(count_) + " words checked";
} else {
message_ = _("One word checked.");
}
#endif
} else {
message_ = speller_->error();
speller_->cleanUp();
if (message_.empty())
message_ = _("The spell checker has died for some reason.\n"
"Maybe it has been killed.");
// make sure that the dialog is not launched
emergency_exit_ = true;
Alert::alert("The spellchecker has failed", message_);
}
delete speller_;
bufferview()->endOfSpellCheck();
// show closing message if any words were checked.
if (count_ > 0)
view().partialUpdate(2);
// reset values to initial
newval_ = 0.0;
oldval_ = 0;
newvalue_ = 0;
count_ = 0;
message_.erase();
stop_ = false;
speller_ = 0;
}