]> git.lyx.org Git - lyx.git/blob - src/AspellChecker.cpp
GuiBox.cpp: fix #6721
[lyx.git] / src / AspellChecker.cpp
1 /**
2  * \file AspellChecker.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Kevin Atkinson
7  * \author John Levon
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "AspellChecker.h"
15 #include "LyXRC.h"
16 #include "WordLangTuple.h"
17
18 #include "support/lassert.h"
19 #include "support/debug.h"
20 #include "support/docstring_list.h"
21
22 #include "support/FileName.h"
23 #include "support/Path.h"
24
25 #include <aspell.h>
26
27 #include <map>
28 #include <string>
29
30 using namespace std;
31
32 namespace lyx {
33
34 namespace {
35
36 struct Speller {
37         AspellSpeller * speller;
38         AspellConfig * config;
39 };
40
41 typedef std::map<std::string, Speller> Spellers;
42
43 } // anon namespace
44
45 struct AspellChecker::Private
46 {
47         Private(): spell_error_object(0) {}
48
49         ~Private();
50
51         /// add a speller of the given language and variety
52         AspellSpeller * addSpeller(string const & lang,
53                                    string const & variety = string());
54
55         ///
56         AspellSpeller * speller(string const & lang,
57                                 string const & variety);
58
59         /// create a unique ID from lang code and variety
60         string const spellerID(string const & lang,
61                                string const & variety);
62
63         /// the spellers
64         Spellers spellers_;
65
66         /// FIXME
67         AspellCanHaveError * spell_error_object;
68 };
69
70
71 AspellChecker::Private::~Private()
72 {
73         if (spell_error_object) {
74                 delete_aspell_can_have_error(spell_error_object);
75                 spell_error_object = 0;
76         }
77
78         Spellers::iterator it = spellers_.begin();
79         Spellers::iterator end = spellers_.end();
80
81         for (; it != end; ++it) {
82                 aspell_speller_save_all_word_lists(it->second.speller);
83                 delete_aspell_speller(it->second.speller);
84                 delete_aspell_config(it->second.config);
85         }
86 }
87
88
89 AspellConfig * getConfig()
90 {
91         AspellConfig * config = new_aspell_config();
92 #ifdef __APPLE__
93         char buf[2048] ;
94         bool have_dict = false;
95 #ifdef ASPELL_FRAMEWORK
96         char * framework = ASPELL_FRAMEWORK ;
97
98         if ( strlen(framework) && getPrivateFrameworkPathName(buf, sizeof(buf), framework) ) {
99                 lyx::support::FileName const base(buf);
100                 lyx::support::FileName const data(base.absFileName() + "/Resources/data");
101                 lyx::support::FileName const dict(base.absFileName() + "/Resources/dict");
102                 LYXERR(Debug::FILES, "aspell bundle path: " << buf);
103                 have_dict = dict.isDirectory() && data.isDirectory();
104                 if (have_dict) {
105                         aspell_config_replace(config, "dict-dir", dict.absFileName().c_str());
106                         aspell_config_replace(config, "data-dir", data.absFileName().c_str());
107                         LYXERR(Debug::FILES, "aspell dict: " << dict);
108                 }
109         }
110 #endif
111         if ( !have_dict ) {
112                 lyx::support::FileName const base("/opt/local"); // check for mac-ports data
113                 lyx::support::FileName const data(base.absFileName() + "/lib/aspell-0.60");
114                 lyx::support::FileName const dict(base.absFileName() + "/share/aspell");
115                 have_dict = dict.isDirectory() && data.isDirectory();
116                 if (have_dict) {
117                         aspell_config_replace(config, "dict-dir", dict.absFileName().c_str());
118                         aspell_config_replace(config, "data-dir", data.absFileName().c_str());
119                         LYXERR(Debug::FILES, "aspell dict: " << dict);
120                 }
121         }
122 #endif
123         return config ;
124 }
125
126
127 AspellSpeller * AspellChecker::Private::addSpeller(string const & lang,
128                                                    string const & variety)
129 {
130         AspellConfig * config = getConfig();
131         // Aspell supports both languages and varieties (such as German
132         // old vs. new spelling). The respective naming convention is
133         // lang_REGION-variety (e.g. de_DE-alt).
134         aspell_config_replace(config, "lang", lang.c_str());
135         if (!variety.empty())
136                 aspell_config_replace(config, "variety", variety.c_str());
137         // Set the encoding to utf-8.
138         // aspell does also understand "ucs-4", so we would not need a
139         // conversion in theory, but if this is used it expects all
140         // char const * arguments to be a cast from  uint const *, and it
141         // seems that this uint is not compatible with our char_type on some
142         // platforms (cygwin, OS X). Therefore we use utf-8, that does
143         // always work.
144         aspell_config_replace(config, "encoding", "utf-8");
145         if (lyxrc.spellchecker_accept_compound)
146                 // Consider run-together words as legal compounds
147                 aspell_config_replace(config, "run-together", "true");
148         else
149                 // Report run-together words as errors
150                 aspell_config_replace(config, "run-together", "false");
151
152         AspellCanHaveError * err = new_aspell_speller(config);
153         if (spell_error_object)
154                 delete_aspell_can_have_error(spell_error_object);
155         spell_error_object = 0;
156
157         if (aspell_error_number(err) != 0) {
158                 // FIXME: We should we indicate somehow that this language is not
159                 // supported.
160                 spell_error_object = err;
161                 LYXERR(Debug::FILES, "aspell error: " << aspell_error_message(err));
162                 return 0;
163         }
164         Speller m;
165         m.speller = to_aspell_speller(err);
166         m.config = config;
167         spellers_[spellerID(lang, variety)] = m;
168         return m.speller;
169 }
170
171
172 AspellSpeller * AspellChecker::Private::speller(string const & lang,
173                                                 string const & variety)
174 {
175         Spellers::iterator it = spellers_.find(spellerID(lang, variety));
176         if (it != spellers_.end())
177                 return it->second.speller;
178         
179         return addSpeller(lang, variety);
180 }
181
182
183 string const AspellChecker::Private::spellerID(string const & lang,
184                                         string const & variety)
185 {
186         if (variety.empty())
187                 return lang;
188         return lang + "-" + variety;
189 }
190
191
192 AspellChecker::AspellChecker(): d(new Private)
193 {
194 }
195
196
197 AspellChecker::~AspellChecker()
198 {
199         delete d;
200 }
201
202
203 SpellChecker::Result AspellChecker::check(WordLangTuple const & word)
204 {
205   
206         AspellSpeller * m =
207                 d->speller(word.lang()->code(), word.lang()->variety());
208
209         if (!m)
210                 return OK;
211
212         if (word.word().empty())
213                 // MSVC compiled Aspell doesn't like it.
214                 return OK;
215
216         int const word_ok = aspell_speller_check(m, to_utf8(word.word()).c_str(), -1);
217         LASSERT(word_ok != -1, /**/);
218
219         return (word_ok) ? OK : UNKNOWN_WORD;
220 }
221
222
223 void AspellChecker::insert(WordLangTuple const & word)
224 {
225         Spellers::iterator it = d->spellers_.find(
226                 d->spellerID(word.lang()->code(), word.lang()->variety()));
227         if (it != d->spellers_.end())
228                 aspell_speller_add_to_personal(it->second.speller, to_utf8(word.word()).c_str(), -1);
229 }
230
231
232 void AspellChecker::accept(WordLangTuple const & word)
233 {
234         Spellers::iterator it = d->spellers_.find(
235                 d->spellerID(word.lang()->code(), word.lang()->variety()));
236         if (it != d->spellers_.end())
237                 aspell_speller_add_to_session(it->second.speller, to_utf8(word.word()).c_str(), -1);
238 }
239
240
241 void AspellChecker::suggest(WordLangTuple const & wl,
242         docstring_list & suggestions)
243 {
244         suggestions.clear();
245         AspellSpeller * m =
246                 d->speller(wl.lang()->code(), wl.lang()->variety());
247
248         if (!m)
249                 return;
250
251         AspellWordList const * sugs =
252                 aspell_speller_suggest(m, to_utf8(wl.word()).c_str(), -1);
253         LASSERT(sugs != 0, /**/);
254         AspellStringEnumeration * els = aspell_word_list_elements(sugs);
255         if (!els || aspell_word_list_empty(sugs))
256                 return;
257
258         for (;;) {
259                 char const * str = aspell_string_enumeration_next(els);
260                 if (!str)
261                         break;
262                 suggestions.push_back(from_utf8(str));
263         }
264
265         delete_aspell_string_enumeration(els);
266 }
267
268
269 bool AspellChecker::hasDictionary(Language const * lang) const
270 {
271         if (!lang)
272                 return false;
273         // code taken from aspell's list-dicts example
274         AspellConfig * config;
275         AspellDictInfoList * dlist;
276         AspellDictInfoEnumeration * dels;
277         const AspellDictInfo * entry;
278
279         config = getConfig();
280
281         /* the returned pointer should _not_ need to be deleted */
282         dlist = get_aspell_dict_info_list(config);
283
284         /* config is no longer needed */
285         delete_aspell_config(config);
286
287         dels = aspell_dict_info_list_elements(dlist);
288
289         bool have = false;
290         while ((entry = aspell_dict_info_enumeration_next(dels)) != 0)
291         {
292                 if (entry->code == lang->code()
293                     && (lang->variety().empty() || entry->jargon == lang->variety())) {
294                         have = true;
295                         break;
296                 }
297         }
298
299         delete_aspell_dict_info_enumeration(dels);
300
301         return have;
302 }
303
304
305 docstring const AspellChecker::error()
306 {
307         char const * err = 0;
308
309         if (d->spell_error_object && aspell_error_number(d->spell_error_object) != 0)
310                 err = aspell_error_message(d->spell_error_object);
311
312         // FIXME UNICODE: err is not in UTF8, but probably the locale encoding
313         return (err ? from_utf8(err) : docstring());
314 }
315
316
317 } // namespace lyx