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/FileName.h"
23 #include "support/Path.h"
37 AspellSpeller * speller;
38 AspellConfig * config;
41 typedef std::map<std::string, Speller> Spellers;
45 struct AspellChecker::Private
47 Private(): spell_error_object(0) {}
51 /// add a speller of the given language and variety
52 AspellSpeller * addSpeller(string const & lang,
53 string const & variety = string());
56 AspellSpeller * speller(string const & lang,
57 string const & variety);
59 /// create a unique ID from lang code and variety
60 string const spellerID(string const & lang,
61 string const & variety);
67 AspellCanHaveError * spell_error_object;
71 AspellChecker::Private::~Private()
73 if (spell_error_object) {
74 delete_aspell_can_have_error(spell_error_object);
75 spell_error_object = 0;
78 Spellers::iterator it = spellers_.begin();
79 Spellers::iterator end = spellers_.end();
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);
89 AspellConfig * getConfig()
91 AspellConfig * config = new_aspell_config();
94 bool have_dict = false;
95 #ifdef ASPELL_FRAMEWORK
96 char * framework = ASPELL_FRAMEWORK ;
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();
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);
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();
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);
127 AspellSpeller * AspellChecker::Private::addSpeller(string const & lang,
128 string const & variety)
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
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");
149 // Report run-together words as errors
150 aspell_config_replace(config, "run-together", "false");
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;
157 if (aspell_error_number(err) != 0) {
158 // FIXME: We should we indicate somehow that this language is not
160 spell_error_object = err;
161 LYXERR(Debug::FILES, "aspell error: " << aspell_error_message(err));
165 m.speller = to_aspell_speller(err);
167 spellers_[spellerID(lang, variety)] = m;
172 AspellSpeller * AspellChecker::Private::speller(string const & lang,
173 string const & variety)
175 Spellers::iterator it = spellers_.find(spellerID(lang, variety));
176 if (it != spellers_.end())
177 return it->second.speller;
179 return addSpeller(lang, variety);
183 string const AspellChecker::Private::spellerID(string const & lang,
184 string const & variety)
188 return lang + "-" + variety;
192 AspellChecker::AspellChecker(): d(new Private)
197 AspellChecker::~AspellChecker()
203 SpellChecker::Result AspellChecker::check(WordLangTuple const & word)
207 d->speller(word.lang()->code(), word.lang()->variety());
212 if (word.word().empty())
213 // MSVC compiled Aspell doesn't like it.
216 int const word_ok = aspell_speller_check(m, to_utf8(word.word()).c_str(), -1);
217 LASSERT(word_ok != -1, /**/);
219 return (word_ok) ? OK : UNKNOWN_WORD;
223 void AspellChecker::insert(WordLangTuple const & word)
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);
232 void AspellChecker::accept(WordLangTuple const & word)
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);
241 void AspellChecker::suggest(WordLangTuple const & wl,
242 docstring_list & suggestions)
246 d->speller(wl.lang()->code(), wl.lang()->variety());
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))
259 char const * str = aspell_string_enumeration_next(els);
262 suggestions.push_back(from_utf8(str));
265 delete_aspell_string_enumeration(els);
269 bool AspellChecker::hasDictionary(Language const * lang) const
273 // code taken from aspell's list-dicts example
274 AspellConfig * config;
275 AspellDictInfoList * dlist;
276 AspellDictInfoEnumeration * dels;
277 const AspellDictInfo * entry;
279 config = getConfig();
281 /* the returned pointer should _not_ need to be deleted */
282 dlist = get_aspell_dict_info_list(config);
284 /* config is no longer needed */
285 delete_aspell_config(config);
287 dels = aspell_dict_info_list_elements(dlist);
290 while ((entry = aspell_dict_info_enumeration_next(dels)) != 0)
292 if (entry->code == lang->code()
293 && (lang->variety().empty() || entry->jargon == lang->variety())) {
299 delete_aspell_dict_info_enumeration(dels);
305 docstring const AspellChecker::error()
307 char const * err = 0;
309 if (d->spell_error_object && aspell_error_number(d->spell_error_object) != 0)
310 err = aspell_error_message(d->spell_error_object);
312 // FIXME UNICODE: err is not in UTF8, but probably the locale encoding
313 return (err ? from_utf8(err) : docstring());