]> git.lyx.org Git - lyx.git/blob - src/AspellChecker.cpp
* src/LaTeXFeatures.cpp: simplify greektext definition. Patch by G. Milde (bug #6458)
[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
95         if ( getPrivateFrameworkPathName(buf, sizeof(buf), "Aspell.framework") ) {
96                 lyx::support::FileName const base(buf);
97                 lyx::support::FileName const data(base.absFilename() + "/lib/aspell-0.60");
98                 lyx::support::FileName const dict(base.absFilename() + "/share/aspell");
99                 LYXERR(Debug::FILES, "aspell bundle path: " << buf);
100                 if (dict.isDirectory() && data.isDirectory()) {
101                         aspell_config_replace(config, "dict-dir", dict.absFilename().c_str());
102                         aspell_config_replace(config, "data-dir", data.absFilename().c_str());
103                         LYXERR(Debug::FILES, "aspell dict: " << dict);
104                 }
105         }
106 #endif
107         return config ;
108 }
109
110
111 AspellSpeller * AspellChecker::Private::addSpeller(string const & lang,
112                                                    string const & variety)
113 {
114         AspellConfig * config = getConfig();
115         // Aspell supports both languages and varieties (such as German
116         // old vs. new spelling). The respective naming convention is
117         // lang_REGION-variety (e.g. de_DE-alt).
118         aspell_config_replace(config, "lang", lang.c_str());
119         if (!variety.empty())
120                 aspell_config_replace(config, "variety", variety.c_str());
121         // Set the encoding to utf-8.
122         // aspell does also understand "ucs-4", so we would not need a
123         // conversion in theory, but if this is used it expects all
124         // char const * arguments to be a cast from  uint const *, and it
125         // seems that this uint is not compatible with our char_type on some
126         // platforms (cygwin, OS X). Therefore we use utf-8, that does
127         // always work.
128         aspell_config_replace(config, "encoding", "utf-8");
129         if (lyxrc.spellchecker_accept_compound)
130                 // Consider run-together words as legal compounds
131                 aspell_config_replace(config, "run-together", "true");
132         else
133                 // Report run-together words as errors
134                 aspell_config_replace(config, "run-together", "false");
135
136         AspellCanHaveError * err = new_aspell_speller(config);
137         if (spell_error_object)
138                 delete_aspell_can_have_error(spell_error_object);
139         spell_error_object = 0;
140
141         if (aspell_error_number(err) != 0) {
142                 // FIXME: We should we indicate somehow that this language is not
143                 // supported.
144                 spell_error_object = err;
145                 LYXERR(Debug::FILES, "aspell error: " << aspell_error_message(err));
146                 return 0;
147         }
148         Speller m;
149         m.speller = to_aspell_speller(err);
150         m.config = config;
151         spellers_[spellerID(lang, variety)] = m;
152         return m.speller;
153 }
154
155
156 AspellSpeller * AspellChecker::Private::speller(string const & lang,
157                                                 string const & variety)
158 {
159         Spellers::iterator it = spellers_.find(spellerID(lang, variety));
160         if (it != spellers_.end())
161                 return it->second.speller;
162         
163         return addSpeller(lang, variety);
164 }
165
166
167 string const AspellChecker::Private::spellerID(string const & lang,
168                                         string const & variety)
169 {
170         if (variety.empty())
171                 return lang;
172         return lang + "-" + variety;
173 }
174
175
176 AspellChecker::AspellChecker(): d(new Private)
177 {
178 }
179
180
181 AspellChecker::~AspellChecker()
182 {
183         delete d;
184 }
185
186
187 SpellChecker::Result AspellChecker::check(WordLangTuple const & word)
188 {
189   
190         AspellSpeller * m =
191                 d->speller(word.lang()->code(), word.lang()->variety());
192
193         if (!m)
194                 return OK;
195
196         if (word.word().empty())
197                 // MSVC compiled Aspell doesn't like it.
198                 return OK;
199
200         int const word_ok = aspell_speller_check(m, to_utf8(word.word()).c_str(), -1);
201         LASSERT(word_ok != -1, /**/);
202
203         return (word_ok) ? OK : UNKNOWN_WORD;
204 }
205
206
207 void AspellChecker::insert(WordLangTuple const & word)
208 {
209         Spellers::iterator it = d->spellers_.find(
210                 d->spellerID(word.lang()->code(), word.lang()->variety()));
211         if (it != d->spellers_.end())
212                 aspell_speller_add_to_personal(it->second.speller, to_utf8(word.word()).c_str(), -1);
213 }
214
215
216 void AspellChecker::accept(WordLangTuple const & word)
217 {
218         Spellers::iterator it = d->spellers_.find(
219                 d->spellerID(word.lang()->code(), word.lang()->variety()));
220         if (it != d->spellers_.end())
221                 aspell_speller_add_to_session(it->second.speller, to_utf8(word.word()).c_str(), -1);
222 }
223
224
225 void AspellChecker::suggest(WordLangTuple const & wl,
226         docstring_list & suggestions)
227 {
228         suggestions.clear();
229         AspellSpeller * m =
230                 d->speller(wl.lang()->code(), wl.lang()->variety());
231
232         if (!m)
233                 return;
234
235         AspellWordList const * sugs =
236                 aspell_speller_suggest(m, to_utf8(wl.word()).c_str(), -1);
237         LASSERT(sugs != 0, /**/);
238         AspellStringEnumeration * els = aspell_word_list_elements(sugs);
239         if (!els || aspell_word_list_empty(sugs))
240                 return;
241
242         for (;;) {
243                 char const * str = aspell_string_enumeration_next(els);
244                 if (!str)
245                         break;
246                 suggestions.push_back(from_utf8(str));
247         }
248
249         delete_aspell_string_enumeration(els);
250 }
251
252
253 bool AspellChecker::hasDictionary(Language const * lang) const
254 {
255         if (!lang)
256                 return false;
257         // code taken from aspell's list-dicts example
258         AspellConfig * config;
259         AspellDictInfoList * dlist;
260         AspellDictInfoEnumeration * dels;
261         const AspellDictInfo * entry;
262
263         config = getConfig();
264
265         /* the returned pointer should _not_ need to be deleted */
266         dlist = get_aspell_dict_info_list(config);
267
268         /* config is no longer needed */
269         delete_aspell_config(config);
270
271         dels = aspell_dict_info_list_elements(dlist);
272
273         bool have = false;
274         while ((entry = aspell_dict_info_enumeration_next(dels)) != 0)
275         {
276                 if (entry->code == lang->code()
277                     && (lang->variety().empty() || entry->jargon == lang->variety())) {
278                         have = true;
279                         break;
280                 }
281         }
282
283         delete_aspell_dict_info_enumeration(dels);
284
285         return have;
286 }
287
288
289 docstring const AspellChecker::error()
290 {
291         char const * err = 0;
292
293         if (d->spell_error_object && aspell_error_number(d->spell_error_object) != 0)
294                 err = aspell_error_message(d->spell_error_object);
295
296         // FIXME UNICODE: err is not in UTF8, but probably the locale encoding
297         return (err ? from_utf8(err) : docstring());
298 }
299
300
301 } // namespace lyx