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.
18 #include "aspell_local.h"
19 #include "WordLangTuple.h"
21 #include <boost/assert.hpp>
27 ASpell::ASpell(BufferParams const &, string const & lang)
28 : els(0), spell_error_object(0)
36 if (spell_error_object) {
37 delete_aspell_can_have_error(spell_error_object);
38 spell_error_object = 0;
42 delete_aspell_string_enumeration(els);
44 Spellers::iterator it = spellers_.begin();
45 Spellers::iterator end = spellers_.end();
47 for (; it != end; ++it) {
48 aspell_speller_save_all_word_lists(it->second.speller);
49 delete_aspell_speller(it->second.speller);
50 delete_aspell_config(it->second.config);
55 void ASpell::addSpeller(string const & lang)
57 AspellConfig * config = new_aspell_config();
58 aspell_config_replace(config, "language-tag", lang.c_str());
59 AspellCanHaveError * err = new_aspell_speller(config);
60 if (spell_error_object)
61 delete_aspell_can_have_error(spell_error_object);
62 spell_error_object = 0;
64 if (aspell_error_number(err) == 0) {
66 m.speller = to_aspell_speller(err);
70 spell_error_object = err;
75 ASpell::Result ASpell::check(WordLangTuple const & word)
77 Result res = UNKNOWN_WORD;
79 Spellers::iterator it = spellers_.find(word.lang_code());
80 if (it == spellers_.end()) {
81 addSpeller(word.lang_code());
82 it = spellers_.find(word.lang_code());
84 if (it == spellers_.end())
88 AspellSpeller * m = it->second.speller;
90 // FIXME UNICODE: we don't need to convert to UTF8, but probably to the locale encoding
91 int const word_ok = aspell_speller_check(m, to_utf8(word.word()).c_str(), -1);
92 BOOST_ASSERT(word_ok != -1);
97 // FIXME UNICODE: we don't need to convert to UTF8, but probably to the locale encoding
98 AspellWordList const * sugs =
99 aspell_speller_suggest(m, to_utf8(word.word()).c_str(), -1);
100 BOOST_ASSERT(sugs != 0);
101 els = aspell_word_list_elements(sugs);
102 if (aspell_word_list_empty(sugs))
105 res = SUGGESTED_WORDS;
111 void ASpell::insert(WordLangTuple const & word)
113 Spellers::iterator it = spellers_.find(word.lang_code());
114 if (it != spellers_.end())
115 // FIXME UNICODE: we don't need to convert to UTF8, but probably to the locale encoding
116 aspell_speller_add_to_personal(it->second.speller, to_utf8(word.word()).c_str(), -1);
120 void ASpell::accept(WordLangTuple const & word)
122 Spellers::iterator it = spellers_.find(word.lang_code());
123 if (it != spellers_.end())
124 // FIXME UNICODE: we don't need to convert to UTF8, but probably to the locale encoding
125 aspell_speller_add_to_session(it->second.speller, to_utf8(word.word()).c_str(), -1);
129 docstring const ASpell::nextMiss()
131 char const * str = 0;
134 str = aspell_string_enumeration_next(els);
136 // FIXME UNICODE: str is not in UTF8, but probably the locale encoding
137 return (str ? from_utf8(str) : docstring());
141 docstring const ASpell::error()
143 char const * err = 0;
145 if (spell_error_object && aspell_error_number(spell_error_object) != 0) {
146 err = aspell_error_message(spell_error_object);
149 // FIXME UNICODE: err is not in UTF8, but probably the locale encoding
150 return (err ? from_utf8(err) : docstring());