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 // FIXME The aspell documentation says to use "lang"
59 aspell_config_replace(config, "language-tag", lang.c_str());
60 // Set the encoding to utf-8.
61 // aspell does also understand "ucs-4", so we would not need a
62 // conversion in theory, but if this is used it expects all
63 // char const * arguments to be a cast from uint const *, and it
64 // seems that this uint is not compatible with our char_type on some
65 // platforms (cygwin, OS X). Therefore we use utf-8, that does
67 aspell_config_replace(config, "encoding", "utf-8");
68 AspellCanHaveError * err = new_aspell_speller(config);
69 if (spell_error_object)
70 delete_aspell_can_have_error(spell_error_object);
71 spell_error_object = 0;
73 if (aspell_error_number(err) == 0) {
75 m.speller = to_aspell_speller(err);
79 spell_error_object = err;
84 ASpell::Result ASpell::check(WordLangTuple const & word)
86 Result res = UNKNOWN_WORD;
88 Spellers::iterator it = spellers_.find(word.lang_code());
89 if (it == spellers_.end()) {
90 addSpeller(word.lang_code());
91 it = spellers_.find(word.lang_code());
93 if (it == spellers_.end())
97 AspellSpeller * m = it->second.speller;
99 int const word_ok = aspell_speller_check(m, to_utf8(word.word()).c_str(), -1);
100 BOOST_ASSERT(word_ok != -1);
105 AspellWordList const * sugs =
106 aspell_speller_suggest(m, to_utf8(word.word()).c_str(), -1);
107 BOOST_ASSERT(sugs != 0);
108 els = aspell_word_list_elements(sugs);
109 if (aspell_word_list_empty(sugs))
112 res = SUGGESTED_WORDS;
118 void ASpell::insert(WordLangTuple const & word)
120 Spellers::iterator it = spellers_.find(word.lang_code());
121 if (it != spellers_.end())
122 aspell_speller_add_to_personal(it->second.speller, to_utf8(word.word()).c_str(), -1);
126 void ASpell::accept(WordLangTuple const & word)
128 Spellers::iterator it = spellers_.find(word.lang_code());
129 if (it != spellers_.end())
130 aspell_speller_add_to_session(it->second.speller, to_utf8(word.word()).c_str(), -1);
134 docstring const ASpell::nextMiss()
136 char const * str = 0;
139 str = aspell_string_enumeration_next(els);
141 return (str ? from_utf8(str) : docstring());
145 docstring const ASpell::error()
147 char const * err = 0;
149 if (spell_error_object && aspell_error_number(spell_error_object) != 0) {
150 err = aspell_error_message(spell_error_object);
153 // FIXME UNICODE: err is not in UTF8, but probably the locale encoding
154 return (err ? from_utf8(err) : docstring());