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>
35 using namespace lyx::support;
36 using namespace lyx::support::os;
42 typedef map<std::string, Hunspell *> Spellers;
46 struct HunspellChecker::Private
52 Hunspell * addSpeller(string const & lang);
53 Hunspell * speller(string const & lang);
60 HunspellChecker::Private::~Private()
62 Spellers::iterator it = spellers_.begin();
63 Spellers::iterator end = spellers_.end();
65 for (; it != end; ++it) {
72 bool haveLanguageFiles(string const & hpath)
74 FileName const affix(hpath + ".aff");
75 FileName const dict(hpath + ".dic");
76 if (!affix.isReadableFile()) {
77 // FIXME: We should indicate somehow that this language is not
79 LYXERR(Debug::FILES, "Hunspell affix file " << affix << " does not exist");
82 if (!dict.isReadableFile()) {
83 LYXERR(Debug::FILES, "Hunspell dictionary file " << dict << " does not exist");
91 Hunspell * HunspellChecker::Private::addSpeller(string const & lang)
93 string hunspell_path = lyxrc.hunspelldir_path;
94 LYXERR(Debug::FILES, "hunspell path: " << external_path(hunspell_path));
95 if (hunspell_path.empty()) {
96 // FIXME We'd like to issue a better error message here, but there seems
97 // to be a problem about thread safety, or something of the sort. If
98 // we issue the message using frontend::Alert, then the code comes
99 // back through here while the box is waiting, and causes some kind
101 static bool warned = false;
104 LYXERR0("Hunspell path not set.");
105 //frontend::Alert::error(_("Hunspell Path Not Found"),
106 // _("You must set the Hunspell dictionary path in Tools>Preferences>Paths."));
111 hunspell_path = external_path(addName(hunspell_path, lang));
112 if (!haveLanguageFiles(hunspell_path)) {
113 // try with '_' replaced by '-'
114 hunspell_path = subst(hunspell_path, '_', '-');
115 if (!haveLanguageFiles(hunspell_path)) {
116 // FIXME: We should indicate somehow that this language is not
117 // supported, probably by popping a warning. But we'll need to
118 // remember which warnings we've issued.
122 FileName const affix(hunspell_path + ".aff");
123 FileName const dict(hunspell_path + ".dic");
124 Hunspell * h = new Hunspell(affix.absFilename().c_str(), dict.absFilename().c_str());
130 Hunspell * HunspellChecker::Private::speller(string const & lang)
132 Spellers::iterator it = spellers_.find(lang);
133 if (it != spellers_.end())
136 return addSpeller(lang);
140 HunspellChecker::HunspellChecker(): d(new Private)
145 HunspellChecker::~HunspellChecker()
151 SpellChecker::Result HunspellChecker::check(WordLangTuple const & wl)
153 string const word_to_check = to_utf8(wl.word());
154 Hunspell * h = d->speller(wl.lang_code());
158 if (h->spell(word_to_check.c_str(), &info))
161 if (info & SPELL_COMPOUND) {
162 // FIXME: What to do with that?
163 LYXERR(Debug::FILES, "Hunspell compound word found " << word_to_check);
165 if (info & SPELL_FORBIDDEN) {
166 // FIXME: What to do with that?
167 LYXERR(Debug::FILES, "Hunspell explicit forbidden word found " << word_to_check);
174 void HunspellChecker::insert(WordLangTuple const & wl)
176 string const word_to_check = to_utf8(wl.word());
177 Hunspell * h = d->speller(wl.lang_code());
180 h->add(word_to_check.c_str());
184 void HunspellChecker::accept(WordLangTuple const &)
186 // FIXME: not implemented!
190 void HunspellChecker::suggest(WordLangTuple const & wl,
191 docstring_list & suggestions)
194 string const word_to_check = to_utf8(wl.word());
195 Hunspell * h = d->speller(wl.lang_code());
198 char ** suggestion_list;
199 int const suggestion_number = h->suggest(&suggestion_list, word_to_check.c_str());
200 if (suggestion_number <= 0)
202 for (int i = 0; i != suggestion_number; ++i)
203 suggestions.push_back(from_utf8(suggestion_list[i]));
204 h->free_list(&suggestion_list, suggestion_number);
208 docstring const HunspellChecker::error()