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 int const word_ok = aspell_speller_check(m, word.word().c_str(), -1);
91 BOOST_ASSERT(word_ok != -1);
96 AspellWordList const * sugs =
97 aspell_speller_suggest(m, word.word().c_str(), -1);
98 BOOST_ASSERT(sugs != 0);
99 els = aspell_word_list_elements(sugs);
100 if (aspell_word_list_empty(sugs))
103 res = SUGGESTED_WORDS;
109 void ASpell::insert(WordLangTuple const & word)
111 Spellers::iterator it = spellers_.find(word.lang_code());
112 if (it != spellers_.end())
113 aspell_speller_add_to_personal(it->second.speller, word.word().c_str(), -1);
117 void ASpell::accept(WordLangTuple const & word)
119 Spellers::iterator it = spellers_.find(word.lang_code());
120 if (it != spellers_.end())
121 aspell_speller_add_to_session(it->second.speller, word.word().c_str(), -1);
125 string const ASpell::nextMiss()
127 char const * str = 0;
130 str = aspell_string_enumeration_next(els);
132 return (str ? str : "");
136 docstring const ASpell::error()
138 char const * err = 0;
140 if (spell_error_object && aspell_error_number(spell_error_object) != 0) {
141 err = aspell_error_message(spell_error_object);
144 return (err ? from_utf8(err) : docstring());