2 * \file HunspellChecker.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Abdelrazak Younes
8 * Full author contact details are available in file CREDITS.
13 #include "HunspellChecker.h"
16 #include "WordLangTuple.h"
18 #include "frontends/alert.h"
20 #include "support/debug.h"
21 #include "support/docstring_list.h"
22 #include "support/filetools.h"
23 #include "support/FileName.h"
24 #include "support/gettext.h"
25 #include "support/lassert.h"
26 #include "support/lstrings.h"
27 #include "support/os.h"
29 #include <hunspell/hunspell.hxx>
36 using namespace lyx::support;
37 using namespace lyx::support::os;
43 typedef map<std::string, Hunspell *> Spellers;
44 typedef vector<WordLangTuple> IgnoreList;
48 struct HunspellChecker::Private
54 bool haveDictionary(string const & lang) const;
55 Hunspell * addSpeller(string const & lang);
56 Hunspell * speller(string const & lang);
58 bool isIgnored(WordLangTuple const & wl) const;
67 HunspellChecker::Private::~Private()
69 Spellers::iterator it = spellers_.begin();
70 Spellers::iterator end = spellers_.end();
72 for (; it != end; ++it) {
79 bool haveLanguageFiles(string const & hpath)
81 FileName const affix(hpath + ".aff");
82 FileName const dict(hpath + ".dic");
83 if (!affix.isReadableFile()) {
84 // FIXME: We should indicate somehow that this language is not
86 LYXERR(Debug::FILES, "Hunspell affix file " << affix << " does not exist");
89 if (!dict.isReadableFile()) {
90 LYXERR(Debug::FILES, "Hunspell dictionary file " << dict << " does not exist");
98 bool HunspellChecker::Private::haveDictionary(string const & lang) const
100 string hunspell_path = lyxrc.hunspelldir_path;
101 LYXERR(Debug::FILES, "hunspell path: " << external_path(hunspell_path));
102 if (hunspell_path.empty()) {
103 // FIXME We'd like to issue a better error message here, but there seems
104 // to be a problem about thread safety, or something of the sort. If
105 // we issue the message using frontend::Alert, then the code comes
106 // back through here while the box is waiting, and causes some kind
108 static bool warned = false;
111 LYXERR0("Hunspell path not set.");
112 //frontend::Alert::error(_("Hunspell Path Not Found"),
113 // _("You must set the Hunspell dictionary path in Tools>Preferences>Paths."));
118 hunspell_path = external_path(addName(hunspell_path, lang));
119 if (!haveLanguageFiles(hunspell_path)) {
120 // try with '_' replaced by '-'
121 hunspell_path = subst(hunspell_path, '_', '-');
122 if (!haveLanguageFiles(hunspell_path)) {
123 // FIXME: We should indicate somehow that this language is not
124 // supported, probably by popping a warning. But we'll need to
125 // remember which warnings we've issued.
133 Hunspell * HunspellChecker::Private::addSpeller(string const & lang)
135 string hunspell_path = lyxrc.hunspelldir_path;
137 if (!haveDictionary(lang))
140 FileName const affix(hunspell_path + ".aff");
141 FileName const dict(hunspell_path + ".dic");
142 Hunspell * h = new Hunspell(affix.absFilename().c_str(), dict.absFilename().c_str());
148 Hunspell * HunspellChecker::Private::speller(string const & lang)
150 Spellers::iterator it = spellers_.find(lang);
151 if (it != spellers_.end())
154 return addSpeller(lang);
158 bool HunspellChecker::Private::isIgnored(WordLangTuple const & wl) const
160 IgnoreList::const_iterator it = ignored_.begin();
161 for (; it != ignored_.end(); ++it) {
162 if ((*it).lang()->code() != wl.lang()->code())
164 if ((*it).word() == wl.word())
171 HunspellChecker::HunspellChecker(): d(new Private)
176 HunspellChecker::~HunspellChecker()
182 SpellChecker::Result HunspellChecker::check(WordLangTuple const & wl)
184 if (d->isIgnored(wl))
187 string const word_to_check = to_utf8(wl.word());
188 Hunspell * h = d->speller(wl.lang()->code());
192 if (h->spell(word_to_check.c_str(), &info))
195 if (info & SPELL_COMPOUND) {
196 // FIXME: What to do with that?
197 LYXERR(Debug::FILES, "Hunspell compound word found " << word_to_check);
199 if (info & SPELL_FORBIDDEN) {
200 // FIXME: What to do with that?
201 LYXERR(Debug::FILES, "Hunspell explicit forbidden word found " << word_to_check);
208 void HunspellChecker::insert(WordLangTuple const & wl)
210 string const word_to_check = to_utf8(wl.word());
211 Hunspell * h = d->speller(wl.lang()->code());
214 h->add(word_to_check.c_str());
218 void HunspellChecker::accept(WordLangTuple const & wl)
220 d->ignored_.push_back(wl);
224 void HunspellChecker::suggest(WordLangTuple const & wl,
225 docstring_list & suggestions)
228 string const word_to_check = to_utf8(wl.word());
229 Hunspell * h = d->speller(wl.lang()->code());
232 char ** suggestion_list;
233 int const suggestion_number = h->suggest(&suggestion_list, word_to_check.c_str());
234 if (suggestion_number <= 0)
236 for (int i = 0; i != suggestion_number; ++i)
237 suggestions.push_back(from_utf8(suggestion_list[i]));
238 h->free_list(&suggestion_list, suggestion_number);
242 bool HunspellChecker::hasDictionary(Language const * lang) const
246 return (d->haveDictionary(lang->code()));
250 docstring const HunspellChecker::error()