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"
34 AspellSpeller * speller;
35 AspellConfig * config;
38 typedef std::map<std::string, Speller> Spellers;
42 struct AspellChecker::Private
44 Private(): spell_error_object(0) {}
48 /// add a speller of the given language and variety
49 AspellSpeller * addSpeller(string const & lang,
50 string const & variety = string());
53 AspellSpeller * speller(string const & lang,
54 string const & variety);
56 /// create a unique ID from lang code and variety
57 string const spellerID(string const & lang,
58 string const & variety);
64 AspellCanHaveError * spell_error_object;
68 AspellChecker::Private::~Private()
70 if (spell_error_object) {
71 delete_aspell_can_have_error(spell_error_object);
72 spell_error_object = 0;
75 Spellers::iterator it = spellers_.begin();
76 Spellers::iterator end = spellers_.end();
78 for (; it != end; ++it) {
79 aspell_speller_save_all_word_lists(it->second.speller);
80 delete_aspell_speller(it->second.speller);
81 delete_aspell_config(it->second.config);
86 AspellSpeller * AspellChecker::Private::addSpeller(string const & lang,
87 string const & variety)
89 AspellConfig * config = new_aspell_config();
90 // Aspell supports both languages and varieties (such as German
91 // old vs. new spelling). The respective naming convention is
92 // lang_REGION-variety (e.g. de_DE-alt).
93 aspell_config_replace(config, "lang", lang.c_str());
95 aspell_config_replace(config, "variety", variety.c_str());
96 // Set the encoding to utf-8.
97 // aspell does also understand "ucs-4", so we would not need a
98 // conversion in theory, but if this is used it expects all
99 // char const * arguments to be a cast from uint const *, and it
100 // seems that this uint is not compatible with our char_type on some
101 // platforms (cygwin, OS X). Therefore we use utf-8, that does
103 aspell_config_replace(config, "encoding", "utf-8");
104 if (lyxrc.spellchecker_accept_compound)
105 // Consider run-together words as legal compounds
106 aspell_config_replace(config, "run-together", "true");
108 // Report run-together words as errors
109 aspell_config_replace(config, "run-together", "false");
110 AspellCanHaveError * err = new_aspell_speller(config);
111 if (spell_error_object)
112 delete_aspell_can_have_error(spell_error_object);
113 spell_error_object = 0;
115 if (aspell_error_number(err) != 0) {
116 // FIXME: We should we indicate somehow that this language is not
118 spell_error_object = err;
122 m.speller = to_aspell_speller(err);
124 spellers_[spellerID(lang, variety)] = m;
129 AspellSpeller * AspellChecker::Private::speller(string const & lang,
130 string const & variety)
132 Spellers::iterator it = spellers_.find(spellerID(lang, variety));
133 if (it != spellers_.end())
134 return it->second.speller;
136 return addSpeller(lang, variety);
140 string const AspellChecker::Private::spellerID(string const & lang,
141 string const & variety)
145 return lang + "-" + variety;
149 AspellChecker::AspellChecker(): d(new Private)
154 AspellChecker::~AspellChecker()
160 SpellChecker::Result AspellChecker::check(WordLangTuple const & word)
164 d->speller(word.lang()->code(), word.lang()->variety());
169 if (word.word().empty())
170 // MSVC compiled Aspell doesn't like it.
173 int const word_ok = aspell_speller_check(m, to_utf8(word.word()).c_str(), -1);
174 LASSERT(word_ok != -1, /**/);
176 return (word_ok) ? OK : UNKNOWN_WORD;
180 void AspellChecker::insert(WordLangTuple const & word)
182 Spellers::iterator it = d->spellers_.find(
183 d->spellerID(word.lang()->code(), word.lang()->variety()));
184 if (it != d->spellers_.end())
185 aspell_speller_add_to_personal(it->second.speller, to_utf8(word.word()).c_str(), -1);
189 void AspellChecker::accept(WordLangTuple const & word)
191 Spellers::iterator it = d->spellers_.find(
192 d->spellerID(word.lang()->code(), word.lang()->variety()));
193 if (it != d->spellers_.end())
194 aspell_speller_add_to_session(it->second.speller, to_utf8(word.word()).c_str(), -1);
198 void AspellChecker::suggest(WordLangTuple const & wl,
199 docstring_list & suggestions)
203 d->speller(wl.lang()->code(), wl.lang()->variety());
208 AspellWordList const * sugs =
209 aspell_speller_suggest(m, to_utf8(wl.word()).c_str(), -1);
210 LASSERT(sugs != 0, /**/);
211 AspellStringEnumeration * els = aspell_word_list_elements(sugs);
212 if (!els || aspell_word_list_empty(sugs))
216 char const * str = aspell_string_enumeration_next(els);
219 suggestions.push_back(from_utf8(str));
222 delete_aspell_string_enumeration(els);
226 bool AspellChecker::hasDictionary(Language const * lang) const
230 // code taken from aspell's list-dicts example
231 AspellConfig * config;
232 AspellDictInfoList * dlist;
233 AspellDictInfoEnumeration * dels;
234 const AspellDictInfo * entry;
236 config = new_aspell_config();
238 /* the returned pointer should _not_ need to be deleted */
239 dlist = get_aspell_dict_info_list(config);
241 /* config is no longer needed */
242 delete_aspell_config(config);
244 dels = aspell_dict_info_list_elements(dlist);
247 while ((entry = aspell_dict_info_enumeration_next(dels)) != 0)
249 if (entry->code == lang->code()
250 && (lang->variety().empty() || entry->jargon == lang->variety())) {
256 delete_aspell_dict_info_enumeration(dels);
262 docstring const AspellChecker::error()
264 char const * err = 0;
266 if (d->spell_error_object && aspell_error_number(d->spell_error_object) != 0)
267 err = aspell_error_message(d->spell_error_object);
269 // FIXME UNICODE: err is not in UTF8, but probably the locale encoding
270 return (err ? from_utf8(err) : docstring());