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/FileName.h"
23 #include "support/gettext.h"
24 #include "support/lassert.h"
25 #include "support/lstrings.h"
26 #include "support/os.h"
28 #include <hunspell/hunspell.hxx>
34 using namespace lyx::support;
35 using namespace lyx::support::os;
41 typedef map<std::string, Hunspell *> Spellers;
45 struct HunspellChecker::Private
51 Hunspell * addSpeller(string const & lang);
52 Hunspell * speller(string const & lang);
59 HunspellChecker::Private::~Private()
61 Spellers::iterator it = spellers_.begin();
62 Spellers::iterator end = spellers_.end();
64 for (; it != end; ++it) {
71 bool haveLanguageFiles(string const & hpath)
73 FileName const affix(hpath + ".aff");
74 FileName const dict(hpath + ".dic");
75 if (!affix.isReadableFile()) {
76 // FIXME: We should indicate somehow that this language is not
78 LYXERR(Debug::FILES, "Hunspell affix file " << affix << " does not exist");
81 if (!dict.isReadableFile()) {
82 LYXERR(Debug::FILES, "Hunspell dictionary file " << dict << " does not exist");
90 Hunspell * HunspellChecker::Private::addSpeller(string const & lang)
92 string hunspell_path = external_path(lyxrc.hunspelldir_path);
93 LYXERR(Debug::FILES, "hunspell path: " << hunspell_path);
94 if (hunspell_path.empty()) {
95 // FIXME We'd like to issue a better error message here, but there seems
96 // to be a problem about thread safety, or something of the sort. If
97 // we issue the message using frontend::Alert, then the code comes
98 // back through here while the box is waiting, and causes some kind
100 static bool warned = false;
103 LYXERR0("Hunspell path not set.");
104 //frontend::Alert::error(_("Hunspell Path Not Found"),
105 // _("You must set the Hunspell dictionary path in Tools>Preferences>Paths."));
110 hunspell_path += "/" + lang;
111 if (!haveLanguageFiles(hunspell_path)) {
112 // try with '_' replaced by '-'
113 hunspell_path = subst(hunspell_path, '_', '-');
114 if (!haveLanguageFiles(hunspell_path)) {
115 // FIXME: We should indicate somehow that this language is not
116 // supported, probably by popping a warning. But we'll need to
117 // remember which warnings we've issued.
121 FileName const affix(hunspell_path + ".aff");
122 FileName const dict(hunspell_path + ".dic");
123 Hunspell * h = new Hunspell(affix.absFilename().c_str(), dict.absFilename().c_str());
129 Hunspell * HunspellChecker::Private::speller(string const & lang)
131 Spellers::iterator it = spellers_.find(lang);
132 if (it != spellers_.end())
135 return addSpeller(lang);
139 HunspellChecker::HunspellChecker(): d(new Private)
144 HunspellChecker::~HunspellChecker()
150 SpellChecker::Result HunspellChecker::check(WordLangTuple const & wl)
152 string const word_to_check = to_utf8(wl.word());
153 Hunspell * h = d->speller(wl.lang_code());
157 if (h->spell(word_to_check.c_str(), &info))
160 if (info & SPELL_COMPOUND) {
161 // FIXME: What to do with that?
162 LYXERR(Debug::FILES, "Hunspell compound word found " << word_to_check);
164 if (info & SPELL_FORBIDDEN) {
165 // FIXME: What to do with that?
166 LYXERR(Debug::FILES, "Hunspell explicit forbidden word found " << word_to_check);
173 void HunspellChecker::insert(WordLangTuple const & wl)
175 string const word_to_check = to_utf8(wl.word());
176 Hunspell * h = d->speller(wl.lang_code());
179 h->add(word_to_check.c_str());
183 void HunspellChecker::accept(WordLangTuple const &)
185 // FIXME: not implemented!
189 void HunspellChecker::suggest(WordLangTuple const & wl,
190 docstring_list & suggestions)
193 string const word_to_check = to_utf8(wl.word());
194 Hunspell * h = d->speller(wl.lang_code());
197 char ** suggestion_list;
198 int const suggestion_number = h->suggest(&suggestion_list, word_to_check.c_str());
199 if (suggestion_number <= 0)
201 for (int i = 0; i != suggestion_number; ++i)
202 suggestions.push_back(from_utf8(suggestion_list[i]));
203 h->free_list(&suggestion_list, suggestion_number);
207 docstring const HunspellChecker::error()