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"
15 #include "PersonalWordList.h"
18 #include "WordLangTuple.h"
20 #include "support/lassert.h"
21 #include "support/debug.h"
22 #include "support/docstring_list.h"
24 #include "support/filetools.h"
25 #include "support/Package.h"
26 #include "support/FileName.h"
27 #include "support/Path.h"
35 using namespace lyx::support;
42 AspellConfig * config;
43 AspellCanHaveError * e_speller;
44 docstring_list ignored_words_;
47 typedef std::map<std::string, Speller> Spellers;
48 typedef map<std::string, PersonalWordList *> LangPersonalWordList;
52 struct AspellChecker::Private
58 /// add a speller of the given language and variety
59 AspellSpeller * addSpeller(Language const * lang);
62 AspellSpeller * speller(Language const * lang);
64 bool isValidDictionary(AspellConfig * config,
65 string const & lang, string const & variety);
66 bool checkAspellData(AspellConfig * config,
67 string const & basepath, string const & datapath, string const & dictpath,
68 string const & lang, string const & variety);
69 AspellConfig * getConfig(string const & lang, string const & variety);
71 SpellChecker::Result check(AspellSpeller * m,
72 string const & word) const;
74 void initSessionDictionary(Speller const & speller, PersonalWordList * pd);
75 void insert(WordLangTuple const & word);
76 void remove(WordLangTuple const & word);
77 bool learned(WordLangTuple const & word);
79 void accept(Speller & speller, WordLangTuple const & word);
84 LangPersonalWordList personal_;
86 /// the location below system/user directory
87 /// there the rws files lookup will happen
88 const string dictDirectory(void) { return "dicts"; }
89 /// there the dat+cmap files lookup will happen
90 const string dataDirectory(void) { return "data"; }
91 /// os package directory constants
92 /// macports on Mac OS X or
93 /// aspell rpms on Linux
94 const string osPackageBase(void) {
95 #ifdef USE_MACOSX_PACKAGING
101 const string osPackageDictDirectory(void) {
102 #ifdef USE_MACOSX_PACKAGING
103 return "/share/aspell";
105 return "/lib/aspell-0.60";
108 const string osPackageDataDirectory(void) { return "/lib/aspell-0.60"; }
113 AspellChecker::Private::~Private()
115 Spellers::iterator it = spellers_.begin();
116 Spellers::iterator end = spellers_.end();
118 for (; it != end; ++it) {
119 if (it->second.e_speller) {
120 AspellSpeller * speller = to_aspell_speller(it->second.e_speller);
121 aspell_speller_save_all_word_lists(speller);
122 delete_aspell_can_have_error(it->second.e_speller);
124 delete_aspell_config(it->second.config);
127 LangPersonalWordList::const_iterator pdit = personal_.begin();
128 LangPersonalWordList::const_iterator pdet = personal_.end();
130 for (; pdit != pdet; ++pdit) {
131 if ( 0 == pdit->second)
133 PersonalWordList * pd = pdit->second;
140 bool AspellChecker::Private::isValidDictionary(AspellConfig * config,
141 string const & lang, string const & variety)
144 // code taken from aspell's list-dicts example
145 // the returned pointer should _not_ need to be deleted
146 AspellDictInfoList * dlist = get_aspell_dict_info_list(config);
147 AspellDictInfoEnumeration * dels = aspell_dict_info_list_elements(dlist);
148 const AspellDictInfo * entry;
150 while (0 != (entry = aspell_dict_info_enumeration_next(dels))) {
151 LYXERR(Debug::DEBUG, "aspell dict:"
152 << " name=" << entry->name
153 << ",code=" << entry->code
154 << ",variety=" << entry->jargon);
155 if (entry->code == lang && (variety.empty() || entry->jargon == variety)) {
160 delete_aspell_dict_info_enumeration(dels);
161 LYXERR(Debug::FILES, "aspell dictionary: " << lang << (have ? " yes" : " no"));
166 bool AspellChecker::Private::checkAspellData(AspellConfig * config,
167 string const & basepath, string const & datapath, string const & dictpath,
168 string const & lang, string const & variety)
170 FileName base(basepath);
171 bool have_dict = base.isDirectory() ;
174 FileName data(addPath(base.absFileName(), datapath));
175 FileName dict(addPath(base.absFileName(), dictpath));
176 have_dict = dict.isDirectory() && data.isDirectory();
178 LYXERR(Debug::FILES, "aspell dict-dir: " << dict);
179 LYXERR(Debug::FILES, "aspell data-dir: " << data);
180 aspell_config_replace(config, "dict-dir", dict.absFileName().c_str());
181 aspell_config_replace(config, "data-dir", data.absFileName().c_str());
182 have_dict = isValidDictionary(config, lang, variety);
189 AspellConfig * AspellChecker::Private::getConfig(string const & lang, string const & variety)
191 AspellConfig * config = new_aspell_config();
192 bool have_dict = false;
193 string const sysdir = lyx::support::package().system_support().absFileName() ;
194 string const userdir = lyx::support::package().user_support().absFileName() ;
196 LYXERR(Debug::FILES, "aspell user dir: " << userdir);
197 have_dict = checkAspellData(config, userdir, dataDirectory(), dictDirectory(), lang, variety);
199 LYXERR(Debug::FILES, "aspell sysdir dir: " << sysdir);
200 have_dict = checkAspellData(config, sysdir, dataDirectory(), dictDirectory(), lang, variety);
203 // check for package data of OS installation
204 have_dict = checkAspellData(config, osPackageBase(), osPackageDataDirectory(), osPackageDictDirectory(), lang, variety);
210 void AspellChecker::Private::initSessionDictionary(
211 Speller const & speller,
212 PersonalWordList * pd)
214 AspellSpeller * aspell = to_aspell_speller(speller.e_speller);
215 aspell_speller_clear_session(aspell);
216 docstring_list::const_iterator it = pd->begin();
217 docstring_list::const_iterator et = pd->end();
218 for (; it != et; ++it) {
219 string const word_to_add = to_utf8(*it);
220 aspell_speller_add_to_session(aspell, word_to_add.c_str(), -1);
222 it = speller.ignored_words_.begin();
223 et = speller.ignored_words_.end();
224 for (; it != et; ++it) {
225 string const word_to_add = to_utf8(*it);
226 aspell_speller_add_to_session(aspell, word_to_add.c_str(), -1);
231 AspellSpeller * AspellChecker::Private::addSpeller(Language const * lang)
234 string const code = lang->code();
235 string const variety = lang->variety();
236 m.config = getConfig(code, variety);
237 // Aspell supports both languages and varieties (such as German
238 // old vs. new spelling). The respective naming convention is
239 // lang_REGION-variety (e.g. de_DE-alt).
240 aspell_config_replace(m.config, "lang", code.c_str());
241 if (!variety.empty())
242 aspell_config_replace(m.config, "variety", variety.c_str());
243 // Set the encoding to utf-8.
244 // aspell does also understand "ucs-4", so we would not need a
245 // conversion in theory, but if this is used it expects all
246 // char const * arguments to be a cast from uint const *, and it
247 // seems that this uint is not compatible with our char_type on some
248 // platforms (cygwin, OS X). Therefore we use utf-8, that does
250 aspell_config_replace(m.config, "encoding", "utf-8");
251 if (lyxrc.spellchecker_accept_compound)
252 // Consider run-together words as legal compounds
253 aspell_config_replace(m.config, "run-together", "true");
255 // Report run-together words as errors
256 aspell_config_replace(m.config, "run-together", "false");
258 m.e_speller = new_aspell_speller(m.config);
259 if (aspell_error_number(m.e_speller) != 0) {
260 // FIXME: We should indicate somehow that this language is not supported.
261 LYXERR(Debug::FILES, "aspell error: " << aspell_error_message(m.e_speller));
262 delete_aspell_can_have_error(m.e_speller);
263 delete_aspell_config(m.config);
267 PersonalWordList * pd = new PersonalWordList(lang->lang());
269 personal_[lang->lang()] = pd;
270 initSessionDictionary(m, pd);
273 spellers_[lang->lang()] = m;
274 return m.e_speller ? to_aspell_speller(m.e_speller) : 0;
278 AspellSpeller * AspellChecker::Private::speller(Language const * lang)
280 Spellers::iterator it = spellers_.find(lang->lang());
281 if (it != spellers_.end())
282 return to_aspell_speller(it->second.e_speller);
284 return addSpeller(lang);
288 SpellChecker::Result AspellChecker::Private::check(
289 AspellSpeller * m, string const & word)
292 int const word_ok = aspell_speller_check(m, word.c_str(), -1);
293 LASSERT(word_ok != -1, /**/);
294 return (word_ok) ? WORD_OK : UNKNOWN_WORD;
297 void AspellChecker::Private::accept(Speller & speller, WordLangTuple const & word)
299 speller.ignored_words_.push_back(word.word());
303 /// personal word list interface
304 void AspellChecker::Private::remove(WordLangTuple const & word)
306 PersonalWordList * pd = personal_[word.lang()->lang()];
309 pd->remove(word.word());
310 Spellers::iterator it = spellers_.find(word.lang()->lang());
311 if (it != spellers_.end()) {
312 initSessionDictionary(it->second, pd);
317 void AspellChecker::Private::insert(WordLangTuple const & word)
319 Spellers::iterator it = spellers_.find(word.lang()->lang());
320 if (it != spellers_.end()) {
321 AspellSpeller * speller = to_aspell_speller(it->second.e_speller);
322 aspell_speller_add_to_session(speller, to_utf8(word.word()).c_str(), -1);
323 PersonalWordList * pd = personal_[word.lang()->lang()];
326 pd->insert(word.word());
330 bool AspellChecker::Private::learned(WordLangTuple const & word)
332 PersonalWordList * pd = personal_[word.lang()->lang()];
335 return pd->exists(word.word());
339 AspellChecker::AspellChecker(): d(new Private)
344 AspellChecker::~AspellChecker()
350 SpellChecker::Result AspellChecker::check(WordLangTuple const & word)
353 AspellSpeller * m = d->speller(word.lang());
358 if (word.word().empty())
359 // MSVC compiled Aspell doesn't like it.
362 string const word_str = to_utf8(word.word());
363 SpellChecker::Result rc = d->check(m, word_str);
364 return (rc == WORD_OK && d->learned(word)) ? LEARNED_WORD : rc;
368 void AspellChecker::advanceChangeNumber()
374 void AspellChecker::insert(WordLangTuple const & word)
377 advanceChangeNumber();
381 void AspellChecker::accept(WordLangTuple const & word)
383 Spellers::iterator it = d->spellers_.find(word.lang()->lang());
384 if (it != d->spellers_.end()) {
385 AspellSpeller * speller = to_aspell_speller(it->second.e_speller);
386 aspell_speller_add_to_session(speller, to_utf8(word.word()).c_str(), -1);
387 d->accept(it->second, word);
388 advanceChangeNumber();
393 void AspellChecker::suggest(WordLangTuple const & wl,
394 docstring_list & suggestions)
397 AspellSpeller * m = d->speller(wl.lang());
402 AspellWordList const * sugs =
403 aspell_speller_suggest(m, to_utf8(wl.word()).c_str(), -1);
404 LASSERT(sugs != 0, /**/);
405 AspellStringEnumeration * els = aspell_word_list_elements(sugs);
406 if (!els || aspell_word_list_empty(sugs))
410 char const * str = aspell_string_enumeration_next(els);
413 suggestions.push_back(from_utf8(str));
416 delete_aspell_string_enumeration(els);
419 void AspellChecker::remove(WordLangTuple const & word)
422 advanceChangeNumber();
425 bool AspellChecker::hasDictionary(Language const * lang) const
428 Spellers::iterator it = d->spellers_.begin();
429 Spellers::iterator end = d->spellers_.end();
432 for (; it != end && !have; ++it) {
433 have = it->second.config && d->isValidDictionary(it->second.config, lang->code(), lang->variety());
436 AspellConfig * config = d->getConfig(lang->code(), lang->variety());
437 have = d->isValidDictionary(config, lang->code(), lang->variety());
438 delete_aspell_config(config);
445 docstring const AspellChecker::error()
447 Spellers::iterator it = d->spellers_.begin();
448 Spellers::iterator end = d->spellers_.end();
449 char const * err = 0;
451 for (; it != end && 0 == err; ++it) {
452 if (it->second.e_speller && aspell_error_number(it->second.e_speller) != 0)
453 err = aspell_error_message(it->second.e_speller);
456 // FIXME UNICODE: err is not in UTF8, but probably the locale encoding
457 return (err ? from_utf8(err) : docstring());