2 * \file AspellChecker.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Kevin Atkinson
9 * Full author contact details are available in file CREDITS.
14 #include "AspellChecker.h"
16 #include "WordLangTuple.h"
18 #include "support/lassert.h"
19 #include "support/debug.h"
20 #include "support/docstring_list.h"
22 #include "support/Package.h"
23 #include "support/FileName.h"
24 #include "support/Path.h"
33 # ifndef ASPELL_FRAMEWORK
34 # define ASPELL_FRAMEWORK "Aspell.framework"
36 # ifndef ASPELL_FRAMEWORK_DATA
37 # define ASPELL_FRAMEWORK_DATA "/Resources/data"
39 # ifndef ASPELL_FRAMEWORK_DICT
40 # define ASPELL_FRAMEWORK_DICT "/Resources/dict"
43 # ifndef ASPELL_MACPORTS
44 # define ASPELL_MACPORTS "/opt/local"
46 # ifndef ASPELL_MACPORTS_DATA
47 # define ASPELL_MACPORTS_DATA "/lib/aspell-0.60"
49 # ifndef ASPELL_MACPORTS_DICT
50 # define ASPELL_MACPORTS_DICT "/share/aspell"
53 #endif /* __APPLE__ */
56 using namespace lyx::support;
63 ///AspellSpeller * speller;
64 AspellConfig * config;
65 AspellCanHaveError * e_speller;
68 typedef std::map<std::string, Speller> Spellers;
72 struct AspellChecker::Private
78 /// add a speller of the given language and variety
79 AspellSpeller * addSpeller(string const & lang,
80 string const & variety = string());
83 AspellSpeller * speller(string const & lang,
84 string const & variety);
86 /// create a unique ID from lang code and variety
87 string const spellerID(string const & lang,
88 string const & variety);
96 AspellChecker::Private::~Private()
98 Spellers::iterator it = spellers_.begin();
99 Spellers::iterator end = spellers_.end();
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);
107 delete_aspell_config(it->second.config);
112 bool isValidDictionary(AspellConfig * config,
113 string const & lang, string const & variety)
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;
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)) {
132 delete_aspell_dict_info_enumeration(dels);
133 LYXERR(Debug::FILES, "aspell dictionary: " << lang << (have ? " yes" : " no"));
138 bool checkAspellData(AspellConfig * config,
139 char const * basepath, char const * datapath, char const * dictpath,
140 string const & lang, string const & variety)
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();
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);
157 AspellConfig * getConfig(string const & lang,
158 string const & variety)
160 AspellConfig * config = new_aspell_config();
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 ;
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);
176 // check for macports data
177 have_dict = checkAspellData(config, ASPELL_MACPORTS, ASPELL_MACPORTS_DATA, ASPELL_MACPORTS_DICT, lang, variety);
184 AspellSpeller * AspellChecker::Private::addSpeller(string const & lang,
185 string const & variety)
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
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");
208 // Report run-together words as errors
209 aspell_config_replace(m.config, "run-together", "false");
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));
217 spellers_[spellerID(lang, variety)] = m;
218 return to_aspell_speller(m.e_speller);
222 AspellSpeller * AspellChecker::Private::speller(string const & lang,
223 string const & variety)
225 Spellers::iterator it = spellers_.find(spellerID(lang, variety));
226 if (it != spellers_.end())
227 return to_aspell_speller(it->second.e_speller);
229 return addSpeller(lang, variety);
233 string const AspellChecker::Private::spellerID(string const & lang,
234 string const & variety)
238 return lang + "-" + variety;
242 AspellChecker::AspellChecker(): d(new Private)
247 AspellChecker::~AspellChecker()
253 SpellChecker::Result AspellChecker::check(WordLangTuple const & word)
257 d->speller(word.lang()->code(), word.lang()->variety());
262 if (word.word().empty())
263 // MSVC compiled Aspell doesn't like it.
266 int const word_ok = aspell_speller_check(m, to_utf8(word.word()).c_str(), -1);
267 LASSERT(word_ok != -1, /**/);
269 return (word_ok) ? OK : UNKNOWN_WORD;
273 void AspellChecker::insert(WordLangTuple const & word)
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);
284 void AspellChecker::accept(WordLangTuple const & word)
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);
295 void AspellChecker::suggest(WordLangTuple const & wl,
296 docstring_list & suggestions)
300 d->speller(wl.lang()->code(), wl.lang()->variety());
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))
313 char const * str = aspell_string_enumeration_next(els);
316 suggestions.push_back(from_utf8(str));
319 delete_aspell_string_enumeration(els);
323 bool AspellChecker::hasDictionary(Language const * lang) const
326 Spellers::iterator it = d->spellers_.begin();
327 Spellers::iterator end = d->spellers_.end();
330 for (; it != end && !have; ++it) {
331 have = isValidDictionary(it->second.config, lang->code(), lang->variety());
334 AspellConfig * config = getConfig(lang->code(), lang->variety());
335 have = isValidDictionary(config, lang->code(), lang->variety());
336 delete_aspell_config(config);
343 docstring const AspellChecker::error()
345 Spellers::iterator it = d->spellers_.begin();
346 Spellers::iterator end = d->spellers_.end();
347 char const * err = 0;
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);
354 // FIXME UNICODE: err is not in UTF8, but probably the locale encoding
355 return (err ? from_utf8(err) : docstring());