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;
45 docstring_list ignored_words_;
48 typedef std::map<std::string, Speller> Spellers;
49 typedef map<std::string, PersonalWordList *> LangPersonalWordList;
53 struct AspellChecker::Private
59 /// add a speller of the given language and variety
60 AspellSpeller * addSpeller(Language const * lang);
63 AspellSpeller * speller(Language const * lang);
65 bool isValidDictionary(AspellConfig * config,
66 string const & lang, string const & variety);
67 bool checkAspellData(AspellConfig * config,
68 string const & basepath, string const & datapath, string const & dictpath,
69 string const & lang, string const & variety);
70 AspellConfig * getConfig(string const & lang, string const & variety);
72 string toAspellWord(docstring const & word) const;
74 SpellChecker::Result check(AspellSpeller * m,
75 WordLangTuple const & word) const;
77 void initSessionDictionary(Speller const & speller, PersonalWordList * pd);
78 void addToSession(AspellCanHaveError * speller, docstring const & word);
79 void insert(WordLangTuple const & word);
80 void remove(WordLangTuple const & word);
81 bool learned(WordLangTuple const & word);
83 void accept(Speller & speller, WordLangTuple const & word);
88 LangPersonalWordList personal_;
90 /// the location below system/user directory
91 /// there the rws files lookup will happen
92 const string dictDirectory(void) { return "dicts"; }
93 /// there the dat+cmap files lookup will happen
94 const string dataDirectory(void) { return "data"; }
95 /// os package directory constants
96 /// macports on Mac OS X or
97 /// aspell rpms on Linux
98 const string osPackageBase(void) {
99 #ifdef USE_MACOSX_PACKAGING
105 const string osPackageDictDirectory(void) {
106 #ifdef USE_MACOSX_PACKAGING
107 return "/share/aspell";
109 return "/lib/aspell-0.60";
112 const string osPackageDataDirectory(void) { return "/lib/aspell-0.60"; }
117 AspellChecker::Private::~Private()
119 Spellers::iterator it = spellers_.begin();
120 Spellers::iterator end = spellers_.end();
122 for (; it != end; ++it) {
123 if (it->second.e_speller) {
124 AspellSpeller * speller = to_aspell_speller(it->second.e_speller);
125 aspell_speller_save_all_word_lists(speller);
126 delete_aspell_can_have_error(it->second.e_speller);
128 delete_aspell_config(it->second.config);
131 LangPersonalWordList::const_iterator pdit = personal_.begin();
132 LangPersonalWordList::const_iterator pdet = personal_.end();
134 for (; pdit != pdet; ++pdit) {
135 if ( 0 == pdit->second)
137 PersonalWordList * pd = pdit->second;
144 bool AspellChecker::Private::isValidDictionary(AspellConfig * config,
145 string const & lang, string const & variety)
148 // code taken from aspell's list-dicts example
149 // the returned pointer should _not_ need to be deleted
150 AspellDictInfoList * dlist = get_aspell_dict_info_list(config);
151 AspellDictInfoEnumeration * dels = aspell_dict_info_list_elements(dlist);
152 const AspellDictInfo * entry;
154 while (0 != (entry = aspell_dict_info_enumeration_next(dels))) {
155 LYXERR(Debug::DEBUG, "aspell dict:"
156 << " name=" << entry->name
157 << ",code=" << entry->code
158 << ",variety=" << entry->jargon);
159 if (entry->code == lang && (variety.empty() || entry->jargon == variety)) {
164 delete_aspell_dict_info_enumeration(dels);
165 LYXERR(Debug::FILES, "aspell dictionary: " << lang << (have ? " yes" : " no"));
170 bool AspellChecker::Private::checkAspellData(AspellConfig * config,
171 string const & basepath, string const & datapath, string const & dictpath,
172 string const & lang, string const & variety)
174 FileName base(basepath);
175 bool have_dict = base.isDirectory() ;
178 FileName data(addPath(base.absFileName(), datapath));
179 FileName dict(addPath(base.absFileName(), dictpath));
180 have_dict = dict.isDirectory() && data.isDirectory();
182 LYXERR(Debug::FILES, "aspell dict-dir: " << dict);
183 LYXERR(Debug::FILES, "aspell data-dir: " << data);
184 aspell_config_replace(config, "dict-dir", dict.absFileName().c_str());
185 aspell_config_replace(config, "data-dir", data.absFileName().c_str());
186 have_dict = isValidDictionary(config, lang, variety);
193 AspellConfig * AspellChecker::Private::getConfig(string const & lang, string const & variety)
195 AspellConfig * config = new_aspell_config();
196 bool have_dict = false;
197 string const sysdir = lyx::support::package().system_support().absFileName() ;
198 string const userdir = lyx::support::package().user_support().absFileName() ;
200 LYXERR(Debug::FILES, "aspell user dir: " << userdir);
201 have_dict = checkAspellData(config, userdir, dataDirectory(), dictDirectory(), lang, variety);
203 LYXERR(Debug::FILES, "aspell sysdir dir: " << sysdir);
204 have_dict = checkAspellData(config, sysdir, dataDirectory(), dictDirectory(), lang, variety);
207 // check for package data of OS installation
208 have_dict = checkAspellData(config, osPackageBase(), osPackageDataDirectory(), osPackageDictDirectory(), lang, variety);
214 void AspellChecker::Private::addToSession(AspellCanHaveError * speller, docstring const & word)
216 string const word_to_add = toAspellWord(word);
217 if(1 != aspell_speller_add_to_session(to_aspell_speller(speller), word_to_add.c_str(), -1))
218 LYXERR(Debug::GUI, "aspell add to session: " << aspell_error_message(speller));
222 void AspellChecker::Private::initSessionDictionary(
223 Speller const & speller,
224 PersonalWordList * pd)
226 AspellSpeller * aspell = to_aspell_speller(speller.e_speller);
227 aspell_speller_clear_session(aspell);
228 docstring_list::const_iterator it = pd->begin();
229 docstring_list::const_iterator et = pd->end();
230 for (; it != et; ++it) {
231 addToSession(speller.e_speller, *it);
233 it = speller.ignored_words_.begin();
234 et = speller.ignored_words_.end();
235 for (; it != et; ++it) {
236 addToSession(speller.e_speller, *it);
241 AspellSpeller * AspellChecker::Private::addSpeller(Language const * lang)
244 string const code = lang->code();
245 string const variety = lang->variety();
246 m.config = getConfig(code, variety);
247 // Aspell supports both languages and varieties (such as German
248 // old vs. new spelling). The respective naming convention is
249 // lang_REGION-variety (e.g. de_DE-alt).
250 aspell_config_replace(m.config, "lang", code.c_str());
251 if (!variety.empty())
252 aspell_config_replace(m.config, "variety", variety.c_str());
253 // Set the encoding to utf-8.
254 // aspell does also understand "ucs-4", so we would not need a
255 // conversion in theory, but if this is used it expects all
256 // char const * arguments to be a cast from uint const *, and it
257 // seems that this uint is not compatible with our char_type on some
258 // platforms (cygwin, OS X). Therefore we use utf-8, that does
260 aspell_config_replace(m.config, "encoding", "utf-8");
261 if (lyxrc.spellchecker_accept_compound)
262 // Consider run-together words as legal compounds
263 aspell_config_replace(m.config, "run-together", "true");
265 // Report run-together words as errors
266 aspell_config_replace(m.config, "run-together", "false");
268 m.accept_compound = lyxrc.spellchecker_accept_compound;
269 m.e_speller = new_aspell_speller(m.config);
270 if (aspell_error_number(m.e_speller) != 0) {
271 // FIXME: We should indicate somehow that this language is not supported.
272 LYXERR(Debug::FILES, "aspell error: " << aspell_error_message(m.e_speller));
273 delete_aspell_can_have_error(m.e_speller);
274 delete_aspell_config(m.config);
278 PersonalWordList * pd = new PersonalWordList(lang->lang());
280 personal_[lang->lang()] = pd;
281 initSessionDictionary(m, pd);
284 spellers_[lang->lang()] = m;
285 return m.e_speller ? to_aspell_speller(m.e_speller) : 0;
289 AspellSpeller * AspellChecker::Private::speller(Language const * lang)
291 Spellers::iterator it = spellers_.find(lang->lang());
292 if (it != spellers_.end()) {
293 Speller aspell = it->second;
294 if (lyxrc.spellchecker_accept_compound != aspell.accept_compound) {
295 // spell checker setting changed... adjust run-together
296 aspell.accept_compound = lyxrc.spellchecker_accept_compound;
297 if (aspell.accept_compound)
298 // Consider run-together words as legal compounds
299 aspell_config_replace(aspell.config, "run-together", "true");
301 // Report run-together words as errors
302 aspell_config_replace(aspell.config, "run-together", "false");
303 AspellCanHaveError * e_speller = aspell.e_speller;
304 aspell.e_speller = new_aspell_speller(aspell.config);
305 delete_aspell_speller(to_aspell_speller(e_speller));
306 spellers_[lang->lang()] = aspell;
308 return to_aspell_speller(aspell.e_speller);
311 return addSpeller(lang);
315 string AspellChecker::Private::toAspellWord(docstring const & word) const
317 return to_utf8(word);
321 SpellChecker::Result AspellChecker::Private::check(
322 AspellSpeller * m, WordLangTuple const & word)
325 string const word_str = toAspellWord(word.word());
326 int const word_ok = aspell_speller_check(m, word_str.c_str(), -1);
327 LASSERT(word_ok != -1, /**/);
328 return (word_ok) ? WORD_OK : UNKNOWN_WORD;
331 void AspellChecker::Private::accept(Speller & speller, WordLangTuple const & word)
333 speller.ignored_words_.push_back(word.word());
337 /// personal word list interface
338 void AspellChecker::Private::remove(WordLangTuple const & word)
340 PersonalWordList * pd = personal_[word.lang()->lang()];
343 pd->remove(word.word());
344 Spellers::iterator it = spellers_.find(word.lang()->lang());
345 if (it != spellers_.end()) {
346 initSessionDictionary(it->second, pd);
351 void AspellChecker::Private::insert(WordLangTuple const & word)
353 Spellers::iterator it = spellers_.find(word.lang()->lang());
354 if (it != spellers_.end()) {
355 addToSession(it->second.e_speller, word.word());
356 PersonalWordList * pd = personal_[word.lang()->lang()];
359 pd->insert(word.word());
363 bool AspellChecker::Private::learned(WordLangTuple const & word)
365 PersonalWordList * pd = personal_[word.lang()->lang()];
368 return pd->exists(word.word());
372 AspellChecker::AspellChecker(): d(new Private)
377 AspellChecker::~AspellChecker()
383 SpellChecker::Result AspellChecker::check(WordLangTuple const & word)
386 AspellSpeller * m = d->speller(word.lang());
391 if (word.word().empty())
392 // MSVC compiled Aspell doesn't like it.
395 SpellChecker::Result rc = d->check(m, word);
396 return (rc == WORD_OK && d->learned(word)) ? LEARNED_WORD : rc;
400 void AspellChecker::advanceChangeNumber()
406 void AspellChecker::insert(WordLangTuple const & word)
409 advanceChangeNumber();
413 void AspellChecker::accept(WordLangTuple const & word)
415 Spellers::iterator it = d->spellers_.find(word.lang()->lang());
416 if (it != d->spellers_.end()) {
417 d->addToSession(it->second.e_speller, word.word());
418 d->accept(it->second, word);
419 advanceChangeNumber();
424 void AspellChecker::suggest(WordLangTuple const & wl,
425 docstring_list & suggestions)
428 AspellSpeller * m = d->speller(wl.lang());
433 string const word = d->toAspellWord(wl.word());
434 AspellWordList const * sugs =
435 aspell_speller_suggest(m, word.c_str(), -1);
436 LASSERT(sugs != 0, /**/);
437 AspellStringEnumeration * els = aspell_word_list_elements(sugs);
438 if (!els || aspell_word_list_empty(sugs))
442 char const * str = aspell_string_enumeration_next(els);
445 suggestions.push_back(from_utf8(str));
448 delete_aspell_string_enumeration(els);
451 void AspellChecker::remove(WordLangTuple const & word)
454 advanceChangeNumber();
457 bool AspellChecker::hasDictionary(Language const * lang) const
460 Spellers::iterator it = d->spellers_.begin();
461 Spellers::iterator end = d->spellers_.end();
464 for (; it != end && !have; ++it) {
465 have = it->second.config && d->isValidDictionary(it->second.config, lang->code(), lang->variety());
468 AspellConfig * config = d->getConfig(lang->code(), lang->variety());
469 have = d->isValidDictionary(config, lang->code(), lang->variety());
470 delete_aspell_config(config);
477 docstring const AspellChecker::error()
479 Spellers::iterator it = d->spellers_.begin();
480 Spellers::iterator end = d->spellers_.end();
481 char const * err = 0;
483 for (; it != end && 0 == err; ++it) {
484 if (it->second.e_speller && aspell_error_number(it->second.e_speller) != 0)
485 err = aspell_error_message(it->second.e_speller);
488 // FIXME UNICODE: err is not in UTF8, but probably the locale encoding
489 return (err ? from_utf8(err) : docstring());