]> git.lyx.org Git - lyx.git/blob - src/HunspellChecker.cpp
Fix bug #6649 - fix texrow structure generated by InsetIndex
[lyx.git] / src / HunspellChecker.cpp
1 /**
2  * \file HunspellChecker.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Abdelrazak Younes
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "HunspellChecker.h"
14
15 #include "LyXRC.h"
16 #include "WordLangTuple.h"
17
18 #include "frontends/alert.h"
19
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"
28
29 #include <hunspell/hunspell.hxx>
30
31 #include <map>
32 #include <string>
33 #include <vector>
34
35 using namespace std;
36 using namespace lyx::support;
37 using namespace lyx::support::os;
38
39 namespace lyx {
40
41 namespace {
42
43 typedef map<std::string, Hunspell *> Spellers;
44 typedef vector<WordLangTuple> IgnoreList;
45
46 } // anon namespace
47
48 struct HunspellChecker::Private
49 {
50         Private() {}
51
52         ~Private();
53
54         bool haveDictionary(string const & lang, string & hpath);
55         Hunspell * addSpeller(string const & lang);
56         Hunspell * speller(string const & lang);
57         /// ignored words
58         bool isIgnored(WordLangTuple const & wl) const;
59
60         /// the spellers
61         Spellers spellers_;
62         ///
63         IgnoreList ignored_;
64 };
65
66
67 HunspellChecker::Private::~Private()
68 {
69         Spellers::iterator it = spellers_.begin();
70         Spellers::iterator end = spellers_.end();
71
72         for (; it != end; ++it) {
73                 delete it->second;
74         }
75 }
76
77
78 namespace {
79 bool haveLanguageFiles(string const & hpath)
80 {
81         FileName const affix(hpath + ".aff");
82         FileName const dict(hpath + ".dic");
83         if (!affix.isReadableFile()) {
84                 // FIXME: We should indicate somehow that this language is not
85                 // supported.
86                 LYXERR(Debug::FILES, "Hunspell affix file " << affix << " does not exist");
87                 return false;
88         }
89         if (!dict.isReadableFile()) {
90                 LYXERR(Debug::FILES, "Hunspell dictionary file " << dict << " does not exist");
91                 return false;
92         }
93         return true;
94 }
95 }
96
97
98 bool HunspellChecker::Private::haveDictionary(string const & lang, string & hunspell_path)
99 {
100         LYXERR(Debug::FILES, "hunspell path: " << external_path(hunspell_path));
101         if (hunspell_path.empty()) {
102                 // FIXME We'd like to issue a better error message here, but there seems
103                 // to be a problem about thread safety, or something of the sort. If
104                 // we issue the message using frontend::Alert, then the code comes
105                 // back through here while the box is waiting, and causes some kind
106                 // of crash. 
107                 static bool warned = false;
108                 if (!warned) {
109                         warned = true;
110                         LYXERR0("Hunspell path not set.");
111                         //frontend::Alert::error(_("Hunspell Path Not Found"), 
112                         //              _("You must set the Hunspell dictionary path in Tools>Preferences>Paths."));
113                 }
114                 return false;
115         }
116
117         hunspell_path = external_path(addName(hunspell_path, lang));
118         if (!haveLanguageFiles(hunspell_path)) {
119                 // try with '_' replaced by '-'
120                 hunspell_path = subst(hunspell_path, '_', '-');
121                 if (!haveLanguageFiles(hunspell_path)) {
122                         // FIXME: We should indicate somehow that this language is not
123                         // supported, probably by popping a warning. But we'll need to
124                         // remember which warnings we've issued.
125                         return false;
126                 }
127         }
128         return true;
129 }
130
131
132 Hunspell * HunspellChecker::Private::addSpeller(string const & lang)
133 {
134         string hunspell_path = lyxrc.hunspelldir_path;
135
136         if (!haveDictionary(lang, hunspell_path))
137                 return 0;
138
139         FileName const affix(hunspell_path + ".aff");
140         FileName const dict(hunspell_path + ".dic");
141         Hunspell * h = new Hunspell(affix.absFilename().c_str(), dict.absFilename().c_str());
142         spellers_[lang] = h;
143         return h;
144 }
145
146
147 Hunspell * HunspellChecker::Private::speller(string const & lang)
148 {
149         Spellers::iterator it = spellers_.find(lang);
150         if (it != spellers_.end())
151                 return it->second;
152         
153         return addSpeller(lang);
154 }
155
156
157 bool HunspellChecker::Private::isIgnored(WordLangTuple const & wl) const
158 {
159         IgnoreList::const_iterator it = ignored_.begin();
160         for (; it != ignored_.end(); ++it) {
161                 if ((*it).lang()->code() != wl.lang()->code())
162                         continue;
163                 if ((*it).word() == wl.word())
164                         return true;
165         }
166         return false;
167 }
168
169
170 HunspellChecker::HunspellChecker(): d(new Private)
171 {
172 }
173
174
175 HunspellChecker::~HunspellChecker()
176 {
177         delete d;
178 }
179
180
181 SpellChecker::Result HunspellChecker::check(WordLangTuple const & wl)
182 {
183         if (d->isIgnored(wl))
184                 return OK;
185
186         Hunspell * h = d->speller(wl.lang()->code());
187         if (!h)
188                 return OK;
189         int info;
190
191         string const encoding = h->get_dic_encoding();
192         string const word_to_check = to_iconv_encoding(wl.word(), encoding);
193         
194         if (h->spell(word_to_check.c_str(), &info))
195                 return OK;
196
197         if (info & SPELL_COMPOUND) {
198                 // FIXME: What to do with that?
199                 LYXERR(Debug::FILES, "Hunspell compound word found " << word_to_check);
200         }
201         if (info & SPELL_FORBIDDEN) {
202                 // FIXME: What to do with that?
203                 LYXERR(Debug::FILES, "Hunspell explicit forbidden word found " << word_to_check);
204         }
205
206         return UNKNOWN_WORD;
207 }
208
209
210 void HunspellChecker::insert(WordLangTuple const & wl)
211 {
212         string const word_to_check = to_utf8(wl.word());
213         Hunspell * h = d->speller(wl.lang()->code());
214         if (!h)
215                 return;
216         h->add(word_to_check.c_str());
217 }
218
219
220 void HunspellChecker::accept(WordLangTuple const & wl)
221 {
222         d->ignored_.push_back(wl);
223 }
224
225
226 void HunspellChecker::suggest(WordLangTuple const & wl,
227         docstring_list & suggestions)
228 {
229         suggestions.clear();
230         Hunspell * h = d->speller(wl.lang()->code());
231         if (!h)
232                 return;
233         string const encoding = h->get_dic_encoding();
234         string const word_to_check = to_iconv_encoding(wl.word(), encoding);
235         char ** suggestion_list;
236         int const suggestion_number = h->suggest(&suggestion_list, word_to_check.c_str());
237         if (suggestion_number <= 0)
238                 return;
239         for (int i = 0; i != suggestion_number; ++i)
240                 suggestions.push_back(from_iconv_encoding(suggestion_list[i], encoding));
241         h->free_list(&suggestion_list, suggestion_number);
242 }
243
244
245 bool HunspellChecker::hasDictionary(Language const * lang) const
246 {
247         if (!lang)
248                 return false;
249         string hunspell_path = lyxrc.hunspelldir_path;
250         return (d->haveDictionary(lang->code(), hunspell_path));
251 }
252
253
254 docstring const HunspellChecker::error()
255 {
256         return docstring();
257 }
258
259
260 } // namespace lyx