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>
28 ASpell::ASpell(BufferParams const &, string const & lang)
29 : els(0), spell_error_object(0)
37 if (spell_error_object) {
38 delete_aspell_can_have_error(spell_error_object);
39 spell_error_object = 0;
43 delete_aspell_string_enumeration(els);
45 Spellers::iterator it = spellers_.begin();
46 Spellers::iterator end = spellers_.end();
48 for (; it != end; ++it) {
49 aspell_speller_save_all_word_lists(it->second.speller);
50 delete_aspell_speller(it->second.speller);
51 delete_aspell_config(it->second.config);
56 void ASpell::addSpeller(string const & lang)
58 AspellConfig * config = new_aspell_config();
59 aspell_config_replace(config, "language-tag", lang.c_str());
60 AspellCanHaveError * err = new_aspell_speller(config);
61 if (spell_error_object)
62 delete_aspell_can_have_error(spell_error_object);
63 spell_error_object = 0;
65 if (aspell_error_number(err) == 0) {
67 m.speller = to_aspell_speller(err);
71 spell_error_object = err;
76 ASpell::Result ASpell::check(WordLangTuple const & word)
78 Result res = UNKNOWN_WORD;
80 Spellers::iterator it = spellers_.find(word.lang_code());
81 if (it == spellers_.end()) {
82 addSpeller(word.lang_code());
83 it = spellers_.find(word.lang_code());
85 if (it == spellers_.end())
89 AspellSpeller * m = it->second.speller;
91 int const word_ok = aspell_speller_check(m, word.word().c_str(), -1);
92 BOOST_ASSERT(word_ok != -1);
97 AspellWordList const * sugs =
98 aspell_speller_suggest(m, word.word().c_str(), -1);
99 BOOST_ASSERT(sugs != 0);
100 els = aspell_word_list_elements(sugs);
101 if (aspell_word_list_empty(sugs))
104 res = SUGGESTED_WORDS;
110 void ASpell::insert(WordLangTuple const & word)
112 Spellers::iterator it = spellers_.find(word.lang_code());
113 if (it != spellers_.end())
114 aspell_speller_add_to_personal(it->second.speller, word.word().c_str(), -1);
118 void ASpell::accept(WordLangTuple const & word)
120 Spellers::iterator it = spellers_.find(word.lang_code());
121 if (it != spellers_.end())
122 aspell_speller_add_to_session(it->second.speller, word.word().c_str(), -1);
126 string const ASpell::nextMiss()
128 char const * str = 0;
131 str = aspell_string_enumeration_next(els);
133 return (str ? str : "");
137 docstring const ASpell::error()
139 char const * err = 0;
141 if (spell_error_object && aspell_error_number(spell_error_object) != 0) {
142 err = aspell_error_message(spell_error_object);
145 return (err ? lyx::from_utf8(err) : docstring());