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>
26 ASpell::ASpell(BufferParams const &, string const & lang)
27 : els(0), spell_error_object(0)
35 if (spell_error_object) {
36 delete_aspell_can_have_error(spell_error_object);
37 spell_error_object = 0;
41 delete_aspell_string_enumeration(els);
43 Spellers::iterator it = spellers_.begin();
44 Spellers::iterator end = spellers_.end();
46 for (; it != end; ++it) {
47 aspell_speller_save_all_word_lists(it->second.speller);
48 delete_aspell_speller(it->second.speller);
49 delete_aspell_config(it->second.config);
54 void ASpell::addSpeller(string const & lang)
56 AspellConfig * config = new_aspell_config();
57 aspell_config_replace(config, "language-tag", lang.c_str());
58 AspellCanHaveError * err = new_aspell_speller(config);
59 if (spell_error_object)
60 delete_aspell_can_have_error(spell_error_object);
61 spell_error_object = 0;
63 if (aspell_error_number(err) == 0) {
65 m.speller = to_aspell_speller(err);
69 spell_error_object = err;
74 ASpell::Result ASpell::check(WordLangTuple const & word)
76 Result res = UNKNOWN_WORD;
78 Spellers::iterator it = spellers_.find(word.lang_code());
79 if (it == spellers_.end()) {
80 addSpeller(word.lang_code());
81 it = spellers_.find(word.lang_code());
83 if (it == spellers_.end())
87 AspellSpeller * m = it->second.speller;
89 int const word_ok = aspell_speller_check(m, word.word().c_str(), -1);
90 BOOST_ASSERT(word_ok != -1);
95 AspellWordList const * sugs =
96 aspell_speller_suggest(m, word.word().c_str(), -1);
97 BOOST_ASSERT(sugs != 0);
98 els = aspell_word_list_elements(sugs);
99 if (aspell_word_list_empty(sugs))
102 res = SUGGESTED_WORDS;
108 void ASpell::insert(WordLangTuple const & word)
110 Spellers::iterator it = spellers_.find(word.lang_code());
111 if (it != spellers_.end())
112 aspell_speller_add_to_personal(it->second.speller, word.word().c_str(), -1);
116 void ASpell::accept(WordLangTuple const & word)
118 Spellers::iterator it = spellers_.find(word.lang_code());
119 if (it != spellers_.end())
120 aspell_speller_add_to_session(it->second.speller, word.word().c_str(), -1);
124 string const ASpell::nextMiss()
126 char const * str = 0;
129 str = aspell_string_enumeration_next(els);
131 return (str ? str : "");
135 string const ASpell::error()
137 char const * err = 0;
139 if (spell_error_object && aspell_error_number(spell_error_object) != 0) {
140 err = aspell_error_message(spell_error_object);
143 return (err ? err : "");