*
* \author Edwin Leuven
*
- * Full author contact details are available in file CREDITS
+ * Full author contact details are available in file CREDITS.
*/
#include <config.h>
-
#include "ControlSpellchecker.h"
#include "ViewBase.h"
+
#include "buffer.h"
+#include "bufferparams.h"
#include "BufferView.h"
+#include "bufferview_funcs.h"
+#include "debug.h"
#include "gettext.h"
#include "language.h"
#include "lyxrc.h"
-#include "lyxtext.h"
-#include "debug.h"
+
+#include "PosIterator.h"
+#include "paragraph.h"
#include "ispell.h"
#ifdef USE_PSPELL
# include "pspell.h"
+#else
+#ifdef USE_ASPELL
+# include "aspell_local.h"
#endif
+#endif
+
+#include "support/tostr.h"
#include "frontends/Alert.h"
-#include "BoostFormat.h"
+
+using lyx::support::bformat;
using std::endl;
+using std::string;
+
ControlSpellchecker::ControlSpellchecker(LyXView & lv, Dialogs & d)
: ControlDialogBD(lv, d),
- newval_(0.0), oldval_(0), newvalue_(0), count_(0)
+ oldval_(0), newvalue_(0), count_(0)
{}
}
+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);
+}
+
+}
+
+
void ControlSpellchecker::startSession()
{
lyxerr[Debug::GUI] << "spell startSession" << endl;
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_.reset(new PSpell(buffer()->params, tmp));
- } else {
-#endif
- tmp = (lyxrc.isp_use_alt_lang) ?
- lyxrc.isp_alt_lang : buffer()->params.language->lang();
-
- speller_.reset(new ISpell(buffer()->params, tmp));
-#ifdef USE_PSPELL
- }
-#endif
+ speller_.reset(getSpeller(buffer()->params()));
// reset values to initial
- newval_ = 0.0;
oldval_ = 0;
newvalue_ = 0;
count_ = 0;
message = _("The spell-checker could not be started.\n"
"Maybe it is mis-configured.");
- Alert::alert(_("The spell-checker has failed"), message);
+ Alert::error(_("The spell-checker has failed"), message);
speller_.reset(0);
}
{
lyxerr[Debug::GUI] << "spell endSession" << endl;
- bufferview()->endOfSpellCheck();
-
emergency_exit_ = true;
if (!speller_.get()) {
}
+namespace {
+
+
+bool isLetter(PosIterator & cur)
+{
+ return !cur.at_end()
+ && cur.pit()->isLetter(cur.pos())
+ && !isDeletedText(*cur.pit(), cur.pos())
+ && (!cur.inset() || cur.inset()->allowSpellCheck());
+}
+
+
+WordLangTuple nextWord(PosIterator & cur, PosIterator const & end,
+ int & progress, BufferParams & bp)
+{
+ // skip until we have real text (will jump paragraphs)
+ for (; cur != end && !isLetter(cur); ++cur, ++progress);
+
+ if (cur == end)
+ return WordLangTuple(string(), string());
+
+ string lang_code = cur.pit()->getFontSettings(bp, cur.pos()).language()->code();
+ string str;
+ // and find the end of the word (insets like optional hyphens
+ // and ligature break are part of a word)
+ for (; cur != end && isLetter(cur); ++cur, ++progress) {
+ if (!cur.pit()->isInset(cur.pos()))
+ str += cur.pit()->getChar(cur.pos());
+ }
+
+ return WordLangTuple(str, lang_code);
+}
+
+
+} //namespace anon
+
+
+
+
void ControlSpellchecker::check()
{
lyxerr[Debug::GUI] << "spell check a word" << endl;
SpellBase::Result res = SpellBase::OK;
- // clear any old selection
- LyXText * text = bufferview()->getLyXText();
- bufferview()->toggleSelection(true);
- bufferview()->update(text, BufferView::SELECT);
+ PosIterator cur(*bufferview());
+ PosIterator const beg = buffer()->pos_iterator_begin();
+ PosIterator const end = buffer()->pos_iterator_end();
- while ((res == SpellBase::OK || res == SpellBase::IGNORE)) {
- word_ = bufferview()->nextWord(newval_);
+ int start = std::distance(beg, cur);
+ int const total = start + std::distance(cur, end);
+
+ if (cur != buffer()->pos_iterator_begin())
+ for (; cur != end && isLetter(cur); ++cur, ++start);
+
+ while (res == SpellBase::OK || res == SpellBase::IGNORE) {
+ word_ = nextWord(cur, end, start, buffer()->params());
// end of document
if (word_.word().empty())
++count_;
// Update slider if and only if value has changed
- newvalue_ = int(100.0 * newval_);
+ float progress = total ? float(start)/total : 1;
+ newvalue_ = int(100.0 * progress);
if (newvalue_!= oldval_) {
lyxerr[Debug::GUI] << "Updating spell progress." << endl;
oldval_ = newvalue_;
lyxerr[Debug::GUI] << "Found word \"" << word_.word() << "\"" << endl;
if (!word_.word().empty()) {
- bufferview()->selectLastWord();
+ int const size = word_.word().size();
+ std::advance(cur, -size);
+ bufferview()->putSelectionAt(cur, size, false);
+ std::advance(cur, size);
} else {
showSummary();
endSession();
view().hide();
speller_.reset(0);
- Alert::alert(_("The spell-checker has failed"), message);
+ Alert::error(_("The spell-checker has failed"), message);
return false;
}
}
string message;
-
-#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 {
+ if (count_ != 1)
+ message = bformat(_("%1$s words checked."), tostr(count_));
+ else
message = _("One word checked.");
- }
-#endif
view().hide();
- Alert::alert(_("Spell-checking is complete"), message);
+ Alert::information(_("Spell-checking is complete"), message);
}
speller_->accept(word_);
check();
}
-
-