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>
31 ASpell::ASpell(BufferParams const &, string const & lang)
32 : els(0), spell_error_object(0)
40 if (spell_error_object) {
41 delete_aspell_can_have_error(spell_error_object);
42 spell_error_object = 0;
46 delete_aspell_string_enumeration(els);
48 Spellers::iterator it = spellers_.begin();
49 Spellers::iterator end = spellers_.end();
51 for (; it != end; ++it) {
52 aspell_speller_save_all_word_lists(it->second.speller);
53 delete_aspell_speller(it->second.speller);
54 delete_aspell_config(it->second.config);
59 void ASpell::addSpeller(string const & lang)
61 AspellConfig * config = new_aspell_config();
62 aspell_config_replace(config, "language-tag", lang.c_str());
63 AspellCanHaveError * err = new_aspell_speller(config);
64 if (spell_error_object)
65 delete_aspell_can_have_error(spell_error_object);
66 spell_error_object = 0;
68 if (aspell_error_number(err) == 0) {
70 m.speller = to_aspell_speller(err);
74 spell_error_object = err;
79 ASpell::Result ASpell::check(WordLangTuple const & word)
81 Result res = UNKNOWN_WORD;
83 Spellers::iterator it = spellers_.find(word.lang_code());
84 if (it == spellers_.end()) {
85 addSpeller(word.lang_code());
86 it = spellers_.find(word.lang_code());
88 if (it == spellers_.end())
92 AspellSpeller * m = it->second.speller;
94 int const word_ok = aspell_speller_check(m, word.word().c_str(), -1);
95 BOOST_ASSERT(word_ok != -1);
100 AspellWordList const * sugs =
101 aspell_speller_suggest(m, word.word().c_str(), -1);
102 BOOST_ASSERT(sugs != 0);
103 els = aspell_word_list_elements(sugs);
104 if (aspell_word_list_empty(sugs))
107 res = SUGGESTED_WORDS;
113 void ASpell::insert(WordLangTuple const & word)
115 Spellers::iterator it = spellers_.find(word.lang_code());
116 if (it != spellers_.end())
117 aspell_speller_add_to_personal(it->second.speller, word.word().c_str(), -1);
121 void ASpell::accept(WordLangTuple const & word)
123 Spellers::iterator it = spellers_.find(word.lang_code());
124 if (it != spellers_.end())
125 aspell_speller_add_to_session(it->second.speller, word.word().c_str(), -1);
129 string const ASpell::nextMiss()
131 char const * str = 0;
134 str = aspell_string_enumeration_next(els);
136 return (str ? str : "");
140 docstring const ASpell::error()
142 char const * err = 0;
144 if (spell_error_object && aspell_error_number(spell_error_object) != 0) {
145 err = aspell_error_message(spell_error_object);
148 return (err ? from_utf8(err) : docstring());