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(string const & lang, string & hpath);
61 bool haveDictionary(string const & lang);
62 Hunspell * addSpeller(string 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 "dict"; }
81 int maxLookupSelector(void) const { return 3; }
85 HunspellChecker::Private::Private()
90 HunspellChecker::Private::~Private()
92 Spellers::iterator it = spellers_.begin();
93 Spellers::iterator end = spellers_.end();
95 for (; it != end; ++it) {
96 if ( 0 != it->second) delete it->second;
99 LangPersonalWordList::const_iterator pdit = personal_.begin();
100 LangPersonalWordList::const_iterator pdet = personal_.end();
102 for (; pdit != pdet; ++pdit) {
103 if ( 0 == pdit->second)
105 PersonalWordList * pd = pdit->second;
112 bool HunspellChecker::Private::haveLanguageFiles(string const & hpath)
114 FileName const affix(hpath + ".aff");
115 FileName const dict(hpath + ".dic");
116 return affix.isReadableFile() && dict.isReadableFile();
120 const string HunspellChecker::Private::dictPath(int selector)
124 return addName(package().system_support().absFileName(),dictDirectory());
127 return addName(package().user_support().absFileName(),dictDirectory());
130 return lyxrc.hunspelldir_path;
135 bool HunspellChecker::Private::haveDictionary(string const & lang, string & hpath)
141 LYXERR(Debug::FILES, "check hunspell path: " << hpath << " for language " << lang);
142 string h_path = addName(hpath, lang);
143 if (!haveLanguageFiles(h_path)) {
144 // try with '_' replaced by '-'
145 h_path = addName(hpath, subst(lang, '_', '-'));
146 if (!haveLanguageFiles(h_path)) {
147 // FIXME: We should indicate somehow that this language is not
148 // supported, probably by popping a warning. But we'll need to
149 // remember which warnings we've issued.
158 bool HunspellChecker::Private::haveDictionary(string const & lang)
161 for ( int p = 0; !result && p < maxLookupSelector(); p++ ) {
162 string lpath = dictPath(p);
163 result = haveDictionary(lang, lpath);
169 Hunspell * HunspellChecker::Private::speller(Language const * lang)
171 Spellers::iterator it = spellers_.find(lang->code());
172 if (it != spellers_.end())
175 return addSpeller(lang);
179 Hunspell * HunspellChecker::Private::addSpeller(string const & lang,string & path)
181 if (!haveDictionary(lang, path)) {
186 FileName const affix(path + ".aff");
187 FileName const dict(path + ".dic");
188 Hunspell * h = new Hunspell(affix.absFileName().c_str(), dict.absFileName().c_str());
189 LYXERR(Debug::FILES, "Hunspell speller for langage " << lang << " at " << dict << " found");
195 Hunspell * HunspellChecker::Private::addSpeller(Language const * lang)
198 for ( int p = 0; p < maxLookupSelector() && 0 == h; p++ ) {
199 string lpath = dictPath(p);
200 h = addSpeller(lang->code(), lpath);
203 string const encoding = h->get_dic_encoding();
204 PersonalWordList * pd = new PersonalWordList(lang->lang());
206 personal_[lang->lang()] = pd;
207 docstring_list::const_iterator it = pd->begin();
208 docstring_list::const_iterator et = pd->end();
209 for (; it != et; ++it) {
210 string const word_to_add = to_iconv_encoding(*it, encoding);
211 h->add(word_to_add.c_str());
218 bool HunspellChecker::Private::isIgnored(WordLangTuple const & wl) const
220 IgnoreList::const_iterator it = ignored_.begin();
221 for (; it != ignored_.end(); ++it) {
222 if ((*it).lang()->code() != wl.lang()->code())
224 if ((*it).word() == wl.word())
230 /// personal word list interface
231 void HunspellChecker::Private::remove(WordLangTuple const & wl)
233 Hunspell * h = speller(wl.lang());
236 string const encoding = h->get_dic_encoding();
237 string const word_to_check = to_iconv_encoding(wl.word(), encoding);
238 h->remove(word_to_check.c_str());
239 PersonalWordList * pd = personal_[wl.lang()->lang()];
242 pd->remove(wl.word());
246 void HunspellChecker::Private::insert(WordLangTuple const & wl)
248 Hunspell * h = speller(wl.lang());
251 string const encoding = h->get_dic_encoding();
252 string const word_to_check = to_iconv_encoding(wl.word(), encoding);
253 h->add(word_to_check.c_str());
254 PersonalWordList * pd = personal_[wl.lang()->lang()];
257 pd->insert(wl.word());
261 bool HunspellChecker::Private::learned(WordLangTuple const & wl)
263 PersonalWordList * pd = personal_[wl.lang()->lang()];
266 return pd->exists(wl.word());
270 HunspellChecker::HunspellChecker(): d(new Private)
275 HunspellChecker::~HunspellChecker()
281 SpellChecker::Result HunspellChecker::check(WordLangTuple const & wl)
283 if (d->isIgnored(wl))
286 Hunspell * h = d->speller(wl.lang());
291 string const encoding = h->get_dic_encoding();
292 string const word_to_check = to_iconv_encoding(wl.word(), encoding);
294 if (h->spell(word_to_check.c_str(), &info))
295 return d->learned(wl) ? LEARNED_WORD : WORD_OK;
297 if (info & SPELL_COMPOUND) {
298 // FIXME: What to do with that?
299 LYXERR(Debug::FILES, "Hunspell compound word found " << word_to_check);
301 if (info & SPELL_FORBIDDEN) {
302 // This was removed from personal dictionary
303 LYXERR(Debug::FILES, "Hunspell explicit forbidden word found " << word_to_check);
310 void HunspellChecker::advanceChangeNumber()
316 void HunspellChecker::insert(WordLangTuple const & wl)
319 LYXERR(Debug::GUI, "learn word: \"" << wl.word() << "\"") ;
320 advanceChangeNumber();
324 void HunspellChecker::remove(WordLangTuple const & wl)
327 LYXERR(Debug::GUI, "unlearn word: \"" << wl.word() << "\"") ;
328 advanceChangeNumber();
332 void HunspellChecker::accept(WordLangTuple const & wl)
334 d->ignored_.push_back(wl);
335 LYXERR(Debug::GUI, "ignore word: \"" << wl.word() << "\"") ;
336 advanceChangeNumber();
340 void HunspellChecker::suggest(WordLangTuple const & wl,
341 docstring_list & suggestions)
344 Hunspell * h = d->speller(wl.lang());
347 string const encoding = h->get_dic_encoding();
348 string const word_to_check = to_iconv_encoding(wl.word(), encoding);
349 char ** suggestion_list;
350 int const suggestion_number = h->suggest(&suggestion_list, word_to_check.c_str());
351 if (suggestion_number <= 0)
353 for (int i = 0; i != suggestion_number; ++i)
354 suggestions.push_back(from_iconv_encoding(suggestion_list[i], encoding));
355 h->free_list(&suggestion_list, suggestion_number);
359 bool HunspellChecker::hasDictionary(Language const * lang) const
363 return (d->haveDictionary(lang->code()));
367 docstring const HunspellChecker::error()