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 << " for language " << lang);
147 string h_path = addName(hpath, HunspellDictionaryName(lang));
148 // first we try lang code+variety
149 if (haveLanguageFiles(h_path)) {
153 // another try with code, '_' replaced by '-'
154 h_path = addName(hpath, subst(lang->code(), '_', '-'));
155 if (!haveLanguageFiles(h_path)) {
163 bool HunspellChecker::Private::haveDictionary(Language const * lang)
166 for ( int p = 0; !result && p < maxLookupSelector(); p++ ) {
167 string lpath = dictPath(p);
168 result = haveDictionary(lang, lpath);
170 // FIXME: if result is false...
171 // we should indicate somehow that this language is not
172 // supported, probably by popping a warning. But we'll need to
173 // remember which warnings we've issued.
178 Hunspell * HunspellChecker::Private::speller(Language const * lang)
180 Spellers::iterator it = spellers_.find(lang->lang());
181 if (it != spellers_.end())
184 return addSpeller(lang);
188 Hunspell * HunspellChecker::Private::addSpeller(Language const * lang,string & path)
190 if (!haveDictionary(lang, path)) {
191 spellers_[lang->lang()] = 0;
195 FileName const affix(path + ".aff");
196 FileName const dict(path + ".dic");
197 Hunspell * h = new Hunspell(affix.absFileName().c_str(), dict.absFileName().c_str());
198 LYXERR(Debug::FILES, "Hunspell speller for langage " << lang << " at " << dict << " found");
199 spellers_[lang->lang()] = h;
204 Hunspell * HunspellChecker::Private::addSpeller(Language const * lang)
207 for ( int p = 0; p < maxLookupSelector() && 0 == h; p++ ) {
208 string lpath = dictPath(p);
209 h = addSpeller(lang, lpath);
212 string const encoding = h->get_dic_encoding();
213 PersonalWordList * pd = new PersonalWordList(lang->lang());
215 personal_[lang->lang()] = pd;
216 docstring_list::const_iterator it = pd->begin();
217 docstring_list::const_iterator et = pd->end();
218 for (; it != et; ++it) {
219 string const word_to_add = to_iconv_encoding(*it, encoding);
220 h->add(word_to_add.c_str());
227 bool HunspellChecker::Private::isIgnored(WordLangTuple const & wl) const
229 IgnoreList::const_iterator it = ignored_.begin();
230 for (; it != ignored_.end(); ++it) {
231 if ((*it).lang()->code() != wl.lang()->code())
233 if ((*it).word() == wl.word())
239 /// personal word list interface
240 void HunspellChecker::Private::remove(WordLangTuple const & wl)
242 Hunspell * h = speller(wl.lang());
245 string const encoding = h->get_dic_encoding();
246 string const word_to_check = to_iconv_encoding(wl.word(), encoding);
247 h->remove(word_to_check.c_str());
248 PersonalWordList * pd = personal_[wl.lang()->lang()];
251 pd->remove(wl.word());
255 void HunspellChecker::Private::insert(WordLangTuple const & wl)
257 Hunspell * h = speller(wl.lang());
260 string const encoding = h->get_dic_encoding();
261 string const word_to_check = to_iconv_encoding(wl.word(), encoding);
262 h->add(word_to_check.c_str());
263 PersonalWordList * pd = personal_[wl.lang()->lang()];
266 pd->insert(wl.word());
270 bool HunspellChecker::Private::learned(WordLangTuple const & wl)
272 PersonalWordList * pd = personal_[wl.lang()->lang()];
275 return pd->exists(wl.word());
279 HunspellChecker::HunspellChecker(): d(new Private)
284 HunspellChecker::~HunspellChecker()
290 SpellChecker::Result HunspellChecker::check(WordLangTuple const & wl)
292 if (d->isIgnored(wl))
295 Hunspell * h = d->speller(wl.lang());
300 string const encoding = h->get_dic_encoding();
301 string const word_to_check = to_iconv_encoding(wl.word(), encoding);
303 if (h->spell(word_to_check.c_str(), &info))
304 return d->learned(wl) ? LEARNED_WORD : WORD_OK;
306 if (info & SPELL_COMPOUND) {
307 // FIXME: What to do with that?
308 LYXERR(Debug::FILES, "Hunspell compound word found " << word_to_check);
310 if (info & SPELL_FORBIDDEN) {
311 // This was removed from personal dictionary
312 LYXERR(Debug::FILES, "Hunspell explicit forbidden word found " << word_to_check);
319 void HunspellChecker::advanceChangeNumber()
325 void HunspellChecker::insert(WordLangTuple const & wl)
328 LYXERR(Debug::GUI, "learn word: \"" << wl.word() << "\"") ;
329 advanceChangeNumber();
333 void HunspellChecker::remove(WordLangTuple const & wl)
336 LYXERR(Debug::GUI, "unlearn word: \"" << wl.word() << "\"") ;
337 advanceChangeNumber();
341 void HunspellChecker::accept(WordLangTuple const & wl)
343 d->ignored_.push_back(wl);
344 LYXERR(Debug::GUI, "ignore word: \"" << wl.word() << "\"") ;
345 advanceChangeNumber();
349 void HunspellChecker::suggest(WordLangTuple const & wl,
350 docstring_list & suggestions)
353 Hunspell * h = d->speller(wl.lang());
356 string const encoding = h->get_dic_encoding();
357 string const word_to_check = to_iconv_encoding(wl.word(), encoding);
358 char ** suggestion_list;
359 int const suggestion_number = h->suggest(&suggestion_list, word_to_check.c_str());
360 if (suggestion_number <= 0)
362 for (int i = 0; i != suggestion_number; ++i)
363 suggestions.push_back(from_iconv_encoding(suggestion_list[i], encoding));
364 h->free_list(&suggestion_list, suggestion_number);
368 bool HunspellChecker::hasDictionary(Language const * lang) const
372 return (d->haveDictionary(lang));
376 docstring const HunspellChecker::error()