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