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/lstrings.h"
23 #include "support/docstring_list.h"
25 #include "support/filetools.h"
26 #include "support/Package.h"
27 #include "support/FileName.h"
28 #include "support/Path.h"
36 using namespace lyx::support;
43 AspellConfig * config;
44 AspellCanHaveError * e_speller;
46 docstring_list ignored_words_;
49 typedef std::map<std::string, Speller> Spellers;
50 typedef map<std::string, PersonalWordList *> LangPersonalWordList;
54 struct AspellChecker::Private
60 /// add a speller of the given language and variety
61 AspellSpeller * addSpeller(Language const * lang);
64 AspellSpeller * speller(Language const * lang);
66 bool isValidDictionary(AspellConfig * config,
67 string const & lang, string const & variety);
68 int numDictionaries() const;
69 bool checkAspellData(AspellConfig * config,
70 string const & basepath, string const & datapath, string const & dictpath,
71 string const & lang, string const & variety);
72 AspellConfig * getConfig(string const & lang, string const & variety);
74 string toAspellWord(docstring const & word) const;
76 SpellChecker::Result check(AspellSpeller * m,
77 WordLangTuple const & word) const;
79 void initSessionDictionary(Speller const & speller, PersonalWordList * pd);
80 void addToSession(AspellCanHaveError * speller, docstring const & word);
81 void insert(WordLangTuple const & word);
82 void remove(WordLangTuple const & word);
83 bool learned(WordLangTuple const & word);
85 void accept(Speller & speller, WordLangTuple const & word);
90 LangPersonalWordList personal_;
92 /// the location below system/user directory
93 /// there the rws files lookup will happen
94 const string dictDirectory(void) { return "dicts"; }
95 /// there the dat+cmap files lookup will happen
96 const string dataDirectory(void) { return "data"; }
97 /// os package directory constants
98 /// macports on Mac OS X or
99 /// aspell rpms on Linux
100 const string osPackageBase(void) {
101 #ifdef USE_MACOSX_PACKAGING
107 const string osPackageDictDirectory(void) {
108 #ifdef USE_MACOSX_PACKAGING
109 return "/share/aspell";
111 return "/lib/aspell-0.60";
114 const string osPackageDataDirectory(void) { return "/lib/aspell-0.60"; }
119 AspellChecker::Private::~Private()
121 Spellers::iterator it = spellers_.begin();
122 Spellers::iterator end = spellers_.end();
124 for (; it != end; ++it) {
125 if (it->second.e_speller) {
126 AspellSpeller * speller = to_aspell_speller(it->second.e_speller);
127 aspell_speller_save_all_word_lists(speller);
128 delete_aspell_can_have_error(it->second.e_speller);
130 delete_aspell_config(it->second.config);
133 LangPersonalWordList::const_iterator pdit = personal_.begin();
134 LangPersonalWordList::const_iterator pdet = personal_.end();
136 for (; pdit != pdet; ++pdit) {
137 if ( 0 == pdit->second)
139 PersonalWordList * pd = pdit->second;
146 bool AspellChecker::Private::isValidDictionary(AspellConfig * config,
147 string const & lang, string const & variety)
150 // code taken from aspell's list-dicts example
151 // the returned pointer should _not_ need to be deleted
152 AspellDictInfoList * dlist = get_aspell_dict_info_list(config);
153 AspellDictInfoEnumeration * dels = aspell_dict_info_list_elements(dlist);
154 const AspellDictInfo * entry;
156 while (0 != (entry = aspell_dict_info_enumeration_next(dels))) {
157 LYXERR(Debug::DEBUG, "aspell dict:"
158 << " name=" << entry->name
159 << ",code=" << entry->code
160 << ",variety=" << entry->jargon);
161 if (entry->code == lang && (variety.empty() || entry->jargon == variety)) {
166 delete_aspell_dict_info_enumeration(dels);
167 LYXERR(Debug::FILES, "aspell dictionary: " << lang << (have ? " yes" : " no"));
172 bool AspellChecker::Private::checkAspellData(AspellConfig * config,
173 string const & basepath, string const & datapath, string const & dictpath,
174 string const & lang, string const & variety)
176 FileName base(basepath);
177 bool have_dict = base.isDirectory() ;
180 FileName data(addPath(base.absFileName(), datapath));
181 FileName dict(addPath(base.absFileName(), dictpath));
182 have_dict = dict.isDirectory() && data.isDirectory();
184 LYXERR(Debug::FILES, "aspell dict-dir: " << dict);
185 LYXERR(Debug::FILES, "aspell data-dir: " << data);
186 aspell_config_replace(config, "dict-dir", dict.absFileName().c_str());
187 aspell_config_replace(config, "data-dir", data.absFileName().c_str());
188 have_dict = isValidDictionary(config, lang, variety);
195 AspellConfig * AspellChecker::Private::getConfig(string const & lang, string const & variety)
197 AspellConfig * config = new_aspell_config();
198 bool have_dict = false;
199 string const sysdir = lyx::support::package().system_support().absFileName() ;
200 string const userdir = lyx::support::package().user_support().absFileName() ;
202 LYXERR(Debug::FILES, "aspell user dir: " << userdir);
203 have_dict = checkAspellData(config, userdir, dataDirectory(), dictDirectory(), lang, variety);
205 LYXERR(Debug::FILES, "aspell sysdir dir: " << sysdir);
206 have_dict = checkAspellData(config, sysdir, dataDirectory(), dictDirectory(), lang, variety);
209 // check for package data of OS installation
210 have_dict = checkAspellData(config, osPackageBase(), osPackageDataDirectory(), osPackageDictDirectory(), lang, variety);
216 void AspellChecker::Private::addToSession(AspellCanHaveError * speller, docstring const & word)
218 string const word_to_add = toAspellWord(word);
219 if(1 != aspell_speller_add_to_session(to_aspell_speller(speller), word_to_add.c_str(), -1))
220 LYXERR(Debug::GUI, "aspell add to session: " << aspell_error_message(speller));
224 void AspellChecker::Private::initSessionDictionary(
225 Speller const & speller,
226 PersonalWordList * pd)
228 AspellSpeller * aspell = to_aspell_speller(speller.e_speller);
229 aspell_speller_clear_session(aspell);
230 docstring_list::const_iterator it = pd->begin();
231 docstring_list::const_iterator et = pd->end();
232 for (; it != et; ++it) {
233 addToSession(speller.e_speller, *it);
235 it = speller.ignored_words_.begin();
236 et = speller.ignored_words_.end();
237 for (; it != et; ++it) {
238 addToSession(speller.e_speller, *it);
243 AspellSpeller * AspellChecker::Private::addSpeller(Language const * lang)
246 string const code = lang->code();
247 string const variety = lang->variety();
248 m.config = getConfig(code, variety);
249 // Aspell supports both languages and varieties (such as German
250 // old vs. new spelling). The respective naming convention is
251 // lang_REGION-variety (e.g. de_DE-alt).
252 aspell_config_replace(m.config, "lang", code.c_str());
253 if (!variety.empty())
254 aspell_config_replace(m.config, "variety", variety.c_str());
255 // Set the encoding to utf-8.
256 // aspell does also understand "ucs-4", so we would not need a
257 // conversion in theory, but if this is used it expects all
258 // char const * arguments to be a cast from uint const *, and it
259 // seems that this uint is not compatible with our char_type on some
260 // platforms (cygwin, OS X). Therefore we use utf-8, that does
262 aspell_config_replace(m.config, "encoding", "utf-8");
263 if (lyxrc.spellchecker_accept_compound)
264 // Consider run-together words as legal compounds
265 aspell_config_replace(m.config, "run-together", "true");
267 // Report run-together words as errors
268 aspell_config_replace(m.config, "run-together", "false");
270 m.accept_compound = lyxrc.spellchecker_accept_compound;
271 m.e_speller = new_aspell_speller(m.config);
272 if (aspell_error_number(m.e_speller) != 0) {
273 // FIXME: We should indicate somehow that this language is not supported.
274 LYXERR(Debug::FILES, "aspell error: " << aspell_error_message(m.e_speller));
275 delete_aspell_can_have_error(m.e_speller);
276 delete_aspell_config(m.config);
280 PersonalWordList * pd = new PersonalWordList(lang->lang());
282 personal_[lang->lang()] = pd;
283 initSessionDictionary(m, pd);
286 spellers_[lang->lang()] = m;
287 return m.e_speller ? to_aspell_speller(m.e_speller) : 0;
291 AspellSpeller * AspellChecker::Private::speller(Language const * lang)
293 Spellers::iterator it = spellers_.find(lang->lang());
294 if (it != spellers_.end()) {
295 Speller aspell = it->second;
296 if (lyxrc.spellchecker_accept_compound != aspell.accept_compound) {
297 // spell checker setting changed... adjust run-together
298 aspell.accept_compound = lyxrc.spellchecker_accept_compound;
299 if (aspell.accept_compound)
300 // Consider run-together words as legal compounds
301 aspell_config_replace(aspell.config, "run-together", "true");
303 // Report run-together words as errors
304 aspell_config_replace(aspell.config, "run-together", "false");
305 AspellCanHaveError * e_speller = aspell.e_speller;
306 aspell.e_speller = new_aspell_speller(aspell.config);
307 delete_aspell_speller(to_aspell_speller(e_speller));
308 spellers_[lang->lang()] = aspell;
310 return to_aspell_speller(aspell.e_speller);
313 return addSpeller(lang);
317 int AspellChecker::Private::numDictionaries() const
320 Spellers::const_iterator it = spellers_.begin();
321 Spellers::const_iterator et = spellers_.end();
323 for (; it != et; ++it) {
324 Speller aspell = it->second;
325 result += aspell.e_speller != 0;
331 string AspellChecker::Private::toAspellWord(docstring const & word) const
334 string word_str = to_utf8(word);
335 while ((mpos = word_str.find('-')) != word_str.npos) {
336 word_str.erase(mpos, 1);
342 SpellChecker::Result AspellChecker::Private::check(
343 AspellSpeller * m, WordLangTuple const & word)
346 SpellChecker::Result result = WORD_OK;
348 docstring rest = split(word.word(), w1, '-');
349 for (; result == WORD_OK;) {
350 string const word_str = toAspellWord(w1);
351 int const word_ok = aspell_speller_check(m, word_str.c_str(), -1);
352 LASSERT(word_ok != -1, /**/);
353 result = (word_ok) ? WORD_OK : UNKNOWN_WORD;
356 rest = split(rest,w1,'-');
358 if (result == WORD_OK)
360 string const word_str = toAspellWord(word.word());
361 int const word_ok = aspell_speller_check(m, word_str.c_str(), -1);
362 LASSERT(word_ok != -1, /**/);
363 return (word_ok) ? WORD_OK : UNKNOWN_WORD;
366 void AspellChecker::Private::accept(Speller & speller, WordLangTuple const & word)
368 speller.ignored_words_.push_back(word.word());
372 /// personal word list interface
373 void AspellChecker::Private::remove(WordLangTuple const & word)
375 PersonalWordList * pd = personal_[word.lang()->lang()];
378 pd->remove(word.word());
379 Spellers::iterator it = spellers_.find(word.lang()->lang());
380 if (it != spellers_.end()) {
381 initSessionDictionary(it->second, pd);
386 void AspellChecker::Private::insert(WordLangTuple const & word)
388 Spellers::iterator it = spellers_.find(word.lang()->lang());
389 if (it != spellers_.end()) {
390 addToSession(it->second.e_speller, word.word());
391 PersonalWordList * pd = personal_[word.lang()->lang()];
394 pd->insert(word.word());
398 bool AspellChecker::Private::learned(WordLangTuple const & word)
400 PersonalWordList * pd = personal_[word.lang()->lang()];
403 return pd->exists(word.word());
407 AspellChecker::AspellChecker(): d(new Private)
412 AspellChecker::~AspellChecker()
418 SpellChecker::Result AspellChecker::check(WordLangTuple const & word)
421 AspellSpeller * m = d->speller(word.lang());
424 return NO_DICTIONARY;
426 if (word.word().empty())
427 // MSVC compiled Aspell doesn't like it.
430 SpellChecker::Result rc = d->check(m, word);
431 return (rc == WORD_OK && d->learned(word)) ? LEARNED_WORD : rc;
435 void AspellChecker::advanceChangeNumber()
441 void AspellChecker::insert(WordLangTuple const & word)
444 advanceChangeNumber();
448 void AspellChecker::accept(WordLangTuple const & word)
450 Spellers::iterator it = d->spellers_.find(word.lang()->lang());
451 if (it != d->spellers_.end()) {
452 d->addToSession(it->second.e_speller, word.word());
453 d->accept(it->second, word);
454 advanceChangeNumber();
459 void AspellChecker::suggest(WordLangTuple const & wl,
460 docstring_list & suggestions)
463 AspellSpeller * m = d->speller(wl.lang());
468 string const word = d->toAspellWord(wl.word());
469 AspellWordList const * sugs =
470 aspell_speller_suggest(m, word.c_str(), -1);
471 LASSERT(sugs != 0, /**/);
472 AspellStringEnumeration * els = aspell_word_list_elements(sugs);
473 if (!els || aspell_word_list_empty(sugs))
477 char const * str = aspell_string_enumeration_next(els);
480 suggestions.push_back(from_utf8(str));
483 delete_aspell_string_enumeration(els);
486 void AspellChecker::remove(WordLangTuple const & word)
489 advanceChangeNumber();
492 bool AspellChecker::hasDictionary(Language const * lang) const
495 Spellers::iterator it = d->spellers_.begin();
496 Spellers::iterator end = d->spellers_.end();
499 for (; it != end && !have; ++it) {
500 have = it->second.config && d->isValidDictionary(it->second.config, lang->code(), lang->variety());
503 AspellConfig * config = d->getConfig(lang->code(), lang->variety());
504 have = d->isValidDictionary(config, lang->code(), lang->variety());
505 delete_aspell_config(config);
512 int AspellChecker::numDictionaries() const
514 return d->numDictionaries();
518 docstring const AspellChecker::error()
520 Spellers::iterator it = d->spellers_.begin();
521 Spellers::iterator end = d->spellers_.end();
522 char const * err = 0;
524 for (; it != end && 0 == err; ++it) {
525 if (it->second.e_speller && aspell_error_number(it->second.e_speller) != 0)
526 err = aspell_error_message(it->second.e_speller);
529 // FIXME UNICODE: err is not in UTF8, but probably the locale encoding
530 return (err ? from_utf8(err) : docstring());