]> git.lyx.org Git - lyx.git/blobdiff - src/HunspellChecker.cpp
More requires --> required, for C++2a.
[lyx.git] / src / HunspellChecker.cpp
index 67c7e27cc422fc93fde4e1b8aaf639f8590333d4..1c2612e78e0fdc973e3e3a6302fc6e213626741e 100644 (file)
@@ -47,7 +47,7 @@ typedef map<std::string, PersonalWordList *> LangPersonalWordList;
 
 typedef vector<WordLangTuple> IgnoreList;
 
-} // anon namespace
+} // namespace
 
 
 struct HunspellChecker::Private
@@ -65,6 +65,7 @@ struct HunspellChecker::Private
        Hunspell * addSpeller(Language const * lang, string & hpath);
        Hunspell * addSpeller(Language const * lang);
        Hunspell * speller(Language const * lang);
+       Hunspell * lookup(Language const * lang);
        /// ignored words
        bool isIgnored(WordLangTuple const & wl) const;
        /// personal word list interface
@@ -85,7 +86,7 @@ struct HunspellChecker::Private
        const string dictDirectory(void) const { return "dicts"; }
        int maxLookupSelector(void) const { return 5; }
        const string HunspellDictionaryName(Language const * lang) {
-               return lang->variety().empty() 
+               return lang->variety().empty()
                        ? lang->code()
                        : lang->code() + "-" + lang->variety();
        }
@@ -154,9 +155,9 @@ const string HunspellChecker::Private::dictPath(int selector)
 {
        switch (selector) {
        case 4:
-               return addName(hunspellPackageDictDirectory(),dictDirectory());
+               return hunspellPackageDictDirectory();
        case 3:
-               return addName(myspellPackageDictDirectory(),dictDirectory());
+               return myspellPackageDictDirectory();
        case 2:
                return addName(package().system_support().absFileName(),dictDirectory());
        case 1:
@@ -169,13 +170,17 @@ const string HunspellChecker::Private::dictPath(int selector)
 
 bool HunspellChecker::Private::haveDictionary(Language const * lang, string & hpath)
 {
-       if (hpath.empty())
+       if (hpath.empty() || !lang)
                return false;
 
+       if (lookup(lang)) return true;
+
+       string d_name = HunspellDictionaryName(lang);
+
        LYXERR(Debug::FILES, "check hunspell path: " << hpath
-                               << " for language " << (lang ? lang->lang() : "NULL" ));
+               << " for language " << lang->lang() << " with name " << d_name);
 
-       string h_path = addName(hpath, HunspellDictionaryName(lang));
+       string h_path = addName(hpath, d_name);
        // first we try lang code+variety
        if (haveLanguageFiles(h_path)) {
                LYXERR(Debug::FILES, "  found " << h_path);
@@ -201,24 +206,27 @@ bool HunspellChecker::Private::haveDictionary(Language const * lang)
                string lpath = dictPath(p);
                result = haveDictionary(lang, lpath);
        }
-       // FIXME: if result is false... 
-       // we should indicate somehow that this language is not
-       // supported, probably by popping a warning. But we'll need to
-       // remember which warnings we've issued.
        return result;
 }
 
 
 Hunspell * HunspellChecker::Private::speller(Language const * lang)
 {
+       Hunspell * h = lookup(lang);
+       if (h) return h;
+
        setUserPath(lyxrc.hunspelldir_path);
-       Spellers::iterator it = spellers_.find(lang->lang());
-       if (it != spellers_.end())
-               return it->second;
        return addSpeller(lang);
 }
 
 
+Hunspell * HunspellChecker::Private::lookup(Language const * lang)
+{
+       Spellers::iterator it = spellers_.find(lang->lang());
+       return it != spellers_.end() ? it->second : 0;
+}
+
+
 Hunspell * HunspellChecker::Private::addSpeller(Language const * lang,string & path)
 {
        if (!haveDictionary(lang, path)) {
@@ -229,7 +237,7 @@ Hunspell * HunspellChecker::Private::addSpeller(Language const * lang,string & p
        FileName const affix(path + ".aff");
        FileName const dict(path + ".dic");
        Hunspell * h = new Hunspell(affix.absFileName().c_str(), dict.absFileName().c_str());
-       LYXERR(Debug::FILES, "Hunspell speller for langage " << lang << " at " << dict << " found");
+       LYXERR(Debug::FILES, "Hunspell speller for langage " << lang << " at " << dict << " added.");
        spellers_[lang->lang()] = h;
        return h;
 }
@@ -348,7 +356,11 @@ SpellChecker::Result HunspellChecker::check(WordLangTuple const & wl)
 
        LYXERR(Debug::GUI, "spellCheck: \"" <<
                   wl.word() << "\", lang = " << wl.lang()->lang()) ;
+#ifdef HAVE_HUNSPELL_CXXABI
+       if (h->spell(word_to_check, &info))
+#else
        if (h->spell(word_to_check.c_str(), &info))
+#endif
                return d->learned(wl) ? LEARNED_WORD : WORD_OK;
 
        if (info & SPELL_COMPOUND) {
@@ -403,6 +415,11 @@ void HunspellChecker::suggest(WordLangTuple const & wl,
                return;
        string const encoding = h->get_dic_encoding();
        string const word_to_check = to_iconv_encoding(wl.word(), encoding);
+#ifdef HAVE_HUNSPELL_CXXABI
+       vector<string> wlst = h->suggest(word_to_check);
+       for (auto const s : wlst)
+               suggestions.push_back(from_iconv_encoding(s, encoding));
+#else
        char ** suggestion_list;
        int const suggestion_number = h->suggest(&suggestion_list, word_to_check.c_str());
        if (suggestion_number <= 0)
@@ -410,6 +427,7 @@ void HunspellChecker::suggest(WordLangTuple const & wl,
        for (int i = 0; i != suggestion_number; ++i)
                suggestions.push_back(from_iconv_encoding(suggestion_list[i], encoding));
        h->free_list(&suggestion_list, suggestion_number);
+#endif
 }
 
 
@@ -422,6 +440,11 @@ void HunspellChecker::stem(WordLangTuple const & wl,
                return;
        string const encoding = h->get_dic_encoding();
        string const word_to_check = to_iconv_encoding(wl.word(), encoding);
+#ifdef HAVE_HUNSPELL_CXXABI
+       vector<string> wlst = h->stem(word_to_check);
+       for (auto const s : wlst)
+               suggestions.push_back(from_iconv_encoding(s, encoding));
+#else
        char ** suggestion_list;
        int const suggestion_number = h->stem(&suggestion_list, word_to_check.c_str());
        if (suggestion_number <= 0)
@@ -429,6 +452,7 @@ void HunspellChecker::stem(WordLangTuple const & wl,
        for (int i = 0; i != suggestion_number; ++i)
                suggestions.push_back(from_iconv_encoding(suggestion_list[i], encoding));
        h->free_list(&suggestion_list, suggestion_number);
+#endif
 }