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"
14 #include "PersonalWordList.h"
17 #include "WordLangTuple.h"
19 #include "frontends/alert.h"
21 #include "support/debug.h"
22 #include "support/docstring_list.h"
23 #include "support/filetools.h"
24 #include "support/Package.h"
25 #include "support/FileName.h"
26 #include "support/gettext.h"
27 #include "support/lassert.h"
28 #include "support/lstrings.h"
29 #include "support/os.h"
31 #include <hunspell/hunspell.hxx>
38 using namespace lyx::support;
39 using namespace lyx::support::os;
45 typedef map<std::string, Hunspell *> Spellers;
46 typedef map<std::string, PersonalWordList *> LangPersonalWordList;
48 typedef vector<WordLangTuple> IgnoreList;
53 struct HunspellChecker::Private
58 const string dictPath(int selector);
59 bool haveLanguageFiles(string const & hpath);
60 bool haveDictionary(Language const * lang, string & hpath);
61 bool haveDictionary(Language const * lang);
62 Hunspell * addSpeller(Language const * lang, string & hpath);
63 Hunspell * addSpeller(Language const * lang);
64 Hunspell * speller(Language const * lang);
66 bool isIgnored(WordLangTuple const & wl) const;
67 /// personal word list interface
68 void remove(WordLangTuple const & wl);
69 void insert(WordLangTuple const & wl);
70 bool learned(WordLangTuple const & wl);
76 LangPersonalWordList personal_;
78 /// the location below system/user directory
79 /// there the aff+dic files lookup will happen
80 const string dictDirectory(void) const { return "dicts"; }
81 int maxLookupSelector(void) const { return 3; }
82 const string HunspellDictionaryName(Language const * lang) {
83 return lang->variety().empty()
85 : lang->code() + "-" + lang->variety();
90 HunspellChecker::Private::Private()
95 HunspellChecker::Private::~Private()
97 Spellers::iterator it = spellers_.begin();
98 Spellers::iterator end = spellers_.end();
100 for (; it != end; ++it) {
101 if ( 0 != it->second) delete it->second;
104 LangPersonalWordList::const_iterator pdit = personal_.begin();
105 LangPersonalWordList::const_iterator pdet = personal_.end();
107 for (; pdit != pdet; ++pdit) {
108 if ( 0 == pdit->second)
110 PersonalWordList * pd = pdit->second;
117 bool HunspellChecker::Private::haveLanguageFiles(string const & hpath)
119 FileName const affix(hpath + ".aff");
120 FileName const dict(hpath + ".dic");
121 return affix.isReadableFile() && dict.isReadableFile();
125 const string HunspellChecker::Private::dictPath(int selector)
129 return addName(package().system_support().absFileName(),dictDirectory());
132 return addName(package().user_support().absFileName(),dictDirectory());
135 return lyxrc.hunspelldir_path;
140 bool HunspellChecker::Private::haveDictionary(Language const * lang, string & hpath)
146 LYXERR(Debug::FILES, "check hunspell path: " << hpath
147 << " for language " << (lang ? lang->lang() : "NULL" ));
149 string h_path = addName(hpath, HunspellDictionaryName(lang));
150 // first we try lang code+variety
151 if (haveLanguageFiles(h_path)) {
152 LYXERR(Debug::FILES, " found " << h_path);
156 // another try with code, '_' replaced by '-'
157 h_path = addName(hpath, subst(lang->code(), '_', '-'));
158 if (!haveLanguageFiles(h_path)) {
161 LYXERR(Debug::FILES, " found " << h_path);
167 bool HunspellChecker::Private::haveDictionary(Language const * lang)
170 for ( int p = 0; !result && p < maxLookupSelector(); p++ ) {
171 string lpath = dictPath(p);
172 result = haveDictionary(lang, lpath);
174 // FIXME: if result is false...
175 // we should indicate somehow that this language is not
176 // supported, probably by popping a warning. But we'll need to
177 // remember which warnings we've issued.
182 Hunspell * HunspellChecker::Private::speller(Language const * lang)
184 Spellers::iterator it = spellers_.find(lang->lang());
185 if (it != spellers_.end())
188 return addSpeller(lang);
192 Hunspell * HunspellChecker::Private::addSpeller(Language const * lang,string & path)
194 if (!haveDictionary(lang, path)) {
195 spellers_[lang->lang()] = 0;
199 FileName const affix(path + ".aff");
200 FileName const dict(path + ".dic");
201 Hunspell * h = new Hunspell(affix.absFileName().c_str(), dict.absFileName().c_str());
202 LYXERR(Debug::FILES, "Hunspell speller for langage " << lang << " at " << dict << " found");
203 spellers_[lang->lang()] = h;
208 Hunspell * HunspellChecker::Private::addSpeller(Language const * lang)
211 for ( int p = 0; p < maxLookupSelector() && 0 == h; p++ ) {
212 string lpath = dictPath(p);
213 h = addSpeller(lang, lpath);
216 string const encoding = h->get_dic_encoding();
217 PersonalWordList * pd = new PersonalWordList(lang->lang());
219 personal_[lang->lang()] = pd;
220 docstring_list::const_iterator it = pd->begin();
221 docstring_list::const_iterator et = pd->end();
222 for (; it != et; ++it) {
223 string const word_to_add = to_iconv_encoding(*it, encoding);
224 h->add(word_to_add.c_str());
231 bool HunspellChecker::Private::isIgnored(WordLangTuple const & wl) const
233 IgnoreList::const_iterator it = ignored_.begin();
234 for (; it != ignored_.end(); ++it) {
235 if ((*it).lang()->code() != wl.lang()->code())
237 if ((*it).word() == wl.word())
243 /// personal word list interface
244 void HunspellChecker::Private::remove(WordLangTuple const & wl)
246 Hunspell * h = speller(wl.lang());
249 string const encoding = h->get_dic_encoding();
250 string const word_to_check = to_iconv_encoding(wl.word(), encoding);
251 h->remove(word_to_check.c_str());
252 PersonalWordList * pd = personal_[wl.lang()->lang()];
255 pd->remove(wl.word());
259 void HunspellChecker::Private::insert(WordLangTuple const & wl)
261 Hunspell * h = speller(wl.lang());
264 string const encoding = h->get_dic_encoding();
265 string const word_to_check = to_iconv_encoding(wl.word(), encoding);
266 h->add(word_to_check.c_str());
267 PersonalWordList * pd = personal_[wl.lang()->lang()];
270 pd->insert(wl.word());
274 bool HunspellChecker::Private::learned(WordLangTuple const & wl)
276 PersonalWordList * pd = personal_[wl.lang()->lang()];
279 return pd->exists(wl.word());
283 HunspellChecker::HunspellChecker(): d(new Private)
288 HunspellChecker::~HunspellChecker()
294 SpellChecker::Result HunspellChecker::check(WordLangTuple const & wl)
296 if (d->isIgnored(wl))
299 Hunspell * h = d->speller(wl.lang());
304 string const encoding = h->get_dic_encoding();
305 string const word_to_check = to_iconv_encoding(wl.word(), encoding);
307 if (h->spell(word_to_check.c_str(), &info))
308 return d->learned(wl) ? LEARNED_WORD : WORD_OK;
310 if (info & SPELL_COMPOUND) {
311 // FIXME: What to do with that?
312 LYXERR(Debug::FILES, "Hunspell compound word found " << word_to_check);
314 if (info & SPELL_FORBIDDEN) {
315 // This was removed from personal dictionary
316 LYXERR(Debug::FILES, "Hunspell explicit forbidden word found " << word_to_check);
323 void HunspellChecker::advanceChangeNumber()
329 void HunspellChecker::insert(WordLangTuple const & wl)
332 LYXERR(Debug::GUI, "learn word: \"" << wl.word() << "\"") ;
333 advanceChangeNumber();
337 void HunspellChecker::remove(WordLangTuple const & wl)
340 LYXERR(Debug::GUI, "unlearn word: \"" << wl.word() << "\"") ;
341 advanceChangeNumber();
345 void HunspellChecker::accept(WordLangTuple const & wl)
347 d->ignored_.push_back(wl);
348 LYXERR(Debug::GUI, "ignore word: \"" << wl.word() << "\"") ;
349 advanceChangeNumber();
353 void HunspellChecker::suggest(WordLangTuple const & wl,
354 docstring_list & suggestions)
357 Hunspell * h = d->speller(wl.lang());
360 string const encoding = h->get_dic_encoding();
361 string const word_to_check = to_iconv_encoding(wl.word(), encoding);
362 char ** suggestion_list;
363 int const suggestion_number = h->suggest(&suggestion_list, word_to_check.c_str());
364 if (suggestion_number <= 0)
366 for (int i = 0; i != suggestion_number; ++i)
367 suggestions.push_back(from_iconv_encoding(suggestion_list[i], encoding));
368 h->free_list(&suggestion_list, suggestion_number);
372 bool HunspellChecker::hasDictionary(Language const * lang) const
376 return (d->haveDictionary(lang));
380 docstring const HunspellChecker::error()