]> git.lyx.org Git - lyx.git/blobdiff - src/EnchantChecker.cpp
Avoid full metrics computation with Update:FitCursor
[lyx.git] / src / EnchantChecker.cpp
index 8cc37812b4c7e558c57331bde91b63c98611b11b..5b42f33fde7e5f575a418d32e18c9ff476f63ffa 100644 (file)
@@ -30,17 +30,29 @@ namespace lyx {
 
 namespace {
 
+enchant::Broker & broker()
+{
+#ifdef HAVE_ENCHANT2
+       static enchant::Broker thebroker;
+       return thebroker;
+#else
+       return *enchant::Broker::instance();
+#endif
+}
+
+
 struct Speller {
        enchant::Dict * speller;
 };
 
 typedef map<string, Speller> Spellers;
-  
-} // anon namespace
+
+} // namespace
 
 struct EnchantChecker::Private
 {
-       Private() {}
+       Private()
+       {}
 
        ~Private();
 
@@ -60,30 +72,28 @@ EnchantChecker::Private::~Private()
        Spellers::iterator it = spellers_.begin();
        Spellers::iterator end = spellers_.end();
 
-       for (; it != end; ++it) {
+       for (; it != end; ++it)
                delete it->second.speller;
-       }
 }
 
 
 enchant::Dict * EnchantChecker::Private::addSpeller(string const & lang)
 {
-       enchant::Broker * instance = enchant::Broker::instance();
+       Speller m;
 
-       if (!instance->dict_exists(lang))
+       try {
+               LYXERR(Debug::FILES, "request enchant speller for language " << lang);
+               m.speller = broker().request_dict(lang);
+       }
+       catch (enchant::Exception & e) {
                // FIXME error handling?
-               return 0;
-
-       enchant::Dict * dict = instance->request_dict(lang);
-
-       if (dict) {
-               Speller m;
-               m.speller = dict;
-               spellers_[lang] = m;
-               return m.speller;
+               const char * what = e.what();
+               LYXERR(Debug::FILES, "cannot add enchant speller: " <<
+                          ((what && *what) ? what : "unspecified enchant exception in request_dict()"));
+               m.speller = nullptr;
        }
-       // FIXME error handling?
-       return 0;
+       spellers_[lang] = m;
+       return m.speller;
 }
 
 
@@ -92,14 +102,14 @@ enchant::Dict * EnchantChecker::Private::speller(string const & lang)
        Spellers::iterator it = spellers_.find(lang);
        if (it != spellers_.end())
                return it->second.speller;
-       
+
        return addSpeller(lang);
 }
 
 
-EnchantChecker::EnchantChecker(): d(new Private)
-{
-}
+EnchantChecker::EnchantChecker()
+       : d(new Private)
+{}
 
 
 EnchantChecker::~EnchantChecker()
@@ -108,11 +118,15 @@ EnchantChecker::~EnchantChecker()
 }
 
 
-SpellChecker::Result EnchantChecker::check(WordLangTuple const & word)
+SpellChecker::Result EnchantChecker::check(WordLangTuple const & word,
+        std::vector<WordLangTuple> const & docdict)
 {
        enchant::Dict * m = d->speller(word.lang()->code());
 
-       if (!m || word.word().empty())
+       if (!m)
+               return NO_DICTIONARY;
+
+       if (word.word().empty())
                return WORD_OK;
 
        string utf8word = to_utf8(word.word());
@@ -120,6 +134,14 @@ SpellChecker::Result EnchantChecker::check(WordLangTuple const & word)
        if (m->check(utf8word))
                return WORD_OK;
 
+       vector<WordLangTuple>::const_iterator it = docdict.begin();
+       for (; it != docdict.end(); ++it) {
+               if (it->lang()->code() != word.lang()->code())
+                       continue;
+               if (it->word() == word.word())
+                       return DOCUMENT_LEARNED_WORD;
+       }
+
        return UNKNOWN_WORD;
 }
 
@@ -132,9 +154,19 @@ void EnchantChecker::advanceChangeNumber()
 
 void EnchantChecker::insert(WordLangTuple const & word)
 {
-       Spellers::iterator it = d->spellers_.find(word.lang()->code());
-       if (it != d->spellers_.end()) {
-               it->second.speller->add(to_utf8(word.word()));
+       enchant::Dict * m = d->speller(word.lang()->code());
+       if (m) {
+               m->add(to_utf8(word.word()));
+               advanceChangeNumber();
+       }
+}
+
+
+void EnchantChecker::remove(WordLangTuple const & word)
+{
+       enchant::Dict * m = d->speller(word.lang()->code());
+       if (m) {
+               m->remove(to_utf8(word.word()));
                advanceChangeNumber();
        }
 }
@@ -142,9 +174,9 @@ void EnchantChecker::insert(WordLangTuple const & word)
 
 void EnchantChecker::accept(WordLangTuple const & word)
 {
-       Spellers::iterator it = d->spellers_.find(word.lang()->code());
-       if (it != d->spellers_.end()) {
-               it->second.speller->add_to_session(to_utf8(word.word()));
+       enchant::Dict * m = d->speller(word.lang()->code());
+       if (m) {
+               m->add_to_session(to_utf8(word.word()));
                advanceChangeNumber();
        }
 }
@@ -163,7 +195,7 @@ void EnchantChecker::suggest(WordLangTuple const & wl,
 
        vector<string> suggs = m->suggest(utf8word);
        vector<string>::const_iterator it = suggs.begin();
-       
+
        for (; it != suggs.end(); ++it)
                suggestions.push_back(from_utf8(*it));
 }
@@ -173,8 +205,13 @@ bool EnchantChecker::hasDictionary(Language const * lang) const
 {
        if (!lang)
                return false;
-       enchant::Broker * instance = enchant::Broker::instance();
-       return (instance->dict_exists(lang->code()));
+       return broker().dict_exists(lang->code());
+}
+
+
+int EnchantChecker::numDictionaries() const
+{
+       return d->spellers_.size();
 }