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
49 AspellSpeller * addSpeller(string const & lang);
52 AspellSpeller * speller(string const & lang);
58 AspellCanHaveError * spell_error_object;
62 AspellChecker::Private::~Private()
64 if (spell_error_object) {
65 delete_aspell_can_have_error(spell_error_object);
66 spell_error_object = 0;
69 Spellers::iterator it = spellers_.begin();
70 Spellers::iterator end = spellers_.end();
72 for (; it != end; ++it) {
73 aspell_speller_save_all_word_lists(it->second.speller);
74 delete_aspell_speller(it->second.speller);
75 delete_aspell_config(it->second.config);
80 AspellSpeller * AspellChecker::Private::addSpeller(string const & lang)
82 AspellConfig * config = new_aspell_config();
83 // FIXME The aspell documentation says to use "lang"
84 aspell_config_replace(config, "language-tag", lang.c_str());
85 // Set the encoding to utf-8.
86 // aspell does also understand "ucs-4", so we would not need a
87 // conversion in theory, but if this is used it expects all
88 // char const * arguments to be a cast from uint const *, and it
89 // seems that this uint is not compatible with our char_type on some
90 // platforms (cygwin, OS X). Therefore we use utf-8, that does
92 aspell_config_replace(config, "encoding", "utf-8");
93 if (lyxrc.spellchecker_accept_compound)
94 // Consider run-together words as legal compounds
95 aspell_config_replace(config, "run-together", "true");
97 // Report run-together words as errors
98 aspell_config_replace(config, "run-together", "false");
99 AspellCanHaveError * err = new_aspell_speller(config);
100 if (spell_error_object)
101 delete_aspell_can_have_error(spell_error_object);
102 spell_error_object = 0;
104 if (aspell_error_number(err) != 0) {
105 // FIXME: We should we indicate somehow that this language is not
107 spell_error_object = err;
111 m.speller = to_aspell_speller(err);
118 AspellSpeller * AspellChecker::Private::speller(string const & lang)
120 Spellers::iterator it = spellers_.find(lang);
121 if (it != spellers_.end())
122 return it->second.speller;
124 return addSpeller(lang);
128 AspellChecker::AspellChecker(): d(new Private)
133 AspellChecker::~AspellChecker()
139 SpellChecker::Result AspellChecker::check(WordLangTuple const & word)
141 AspellSpeller * m = d->speller(word.lang_code());
145 if (word.word().empty())
146 // MSVC compiled Aspell doesn't like it.
149 int const word_ok = aspell_speller_check(m, to_utf8(word.word()).c_str(), -1);
150 LASSERT(word_ok != -1, /**/);
152 return (word_ok) ? OK : UNKNOWN_WORD;
156 void AspellChecker::insert(WordLangTuple const & word)
158 Spellers::iterator it = d->spellers_.find(word.lang_code());
159 if (it != d->spellers_.end())
160 aspell_speller_add_to_personal(it->second.speller, to_utf8(word.word()).c_str(), -1);
164 void AspellChecker::accept(WordLangTuple const & word)
166 Spellers::iterator it = d->spellers_.find(word.lang_code());
167 if (it != d->spellers_.end())
168 aspell_speller_add_to_session(it->second.speller, to_utf8(word.word()).c_str(), -1);
172 void AspellChecker::suggest(WordLangTuple const & wl,
173 docstring_list & suggestions)
176 AspellSpeller * m = d->speller(wl.lang_code());
180 AspellWordList const * sugs =
181 aspell_speller_suggest(m, to_utf8(wl.word()).c_str(), -1);
182 LASSERT(sugs != 0, /**/);
183 AspellStringEnumeration * els = aspell_word_list_elements(sugs);
184 if (!els || aspell_word_list_empty(sugs))
188 char const * str = aspell_string_enumeration_next(els);
191 suggestions.push_back(from_utf8(str));
194 delete_aspell_string_enumeration(els);
198 docstring const AspellChecker::error()
200 char const * err = 0;
202 if (d->spell_error_object && aspell_error_number(d->spell_error_object) != 0)
203 err = aspell_error_message(d->spell_error_object);
205 // FIXME UNICODE: err is not in UTF8, but probably the locale encoding
206 return (err ? from_utf8(err) : docstring());