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.
14 #include "support/debug.h"
18 #include "ASpell_local.h"
20 #include "WordLangTuple.h"
22 #include "support/lassert.h"
28 ASpell::ASpell(): 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 // FIXME The aspell documentation says to use "lang"
58 aspell_config_replace(config, "language-tag", lang.c_str());
59 // Set the encoding to utf-8.
60 // aspell does also understand "ucs-4", so we would not need a
61 // conversion in theory, but if this is used it expects all
62 // char const * arguments to be a cast from uint const *, and it
63 // seems that this uint is not compatible with our char_type on some
64 // platforms (cygwin, OS X). Therefore we use utf-8, that does
66 aspell_config_replace(config, "encoding", "utf-8");
67 if (lyxrc.spellchecker_accept_compound)
68 // Consider run-together words as legal compounds
69 aspell_config_replace(config, "run-together", "true");
71 // Report run-together words as errors
72 aspell_config_replace(config, "run-together", "false");
73 AspellCanHaveError * err = new_aspell_speller(config);
74 if (spell_error_object)
75 delete_aspell_can_have_error(spell_error_object);
76 spell_error_object = 0;
78 if (aspell_error_number(err) == 0) {
80 m.speller = to_aspell_speller(err);
84 spell_error_object = err;
89 ASpell::Result ASpell::check(WordLangTuple const & word)
91 Result res = UNKNOWN_WORD;
93 Spellers::iterator it = spellers_.find(word.lang_code());
94 if (it == spellers_.end()) {
95 addSpeller(word.lang_code());
96 it = spellers_.find(word.lang_code());
98 if (it == spellers_.end())
102 AspellSpeller * m = it->second.speller;
104 int const word_ok = aspell_speller_check(m, to_utf8(word.word()).c_str(), -1);
105 LASSERT(word_ok != -1, /**/);
110 AspellWordList const * sugs =
111 aspell_speller_suggest(m, to_utf8(word.word()).c_str(), -1);
112 LASSERT(sugs != 0, /**/);
113 els = aspell_word_list_elements(sugs);
114 if (aspell_word_list_empty(sugs))
117 res = SUGGESTED_WORDS;
123 void ASpell::insert(WordLangTuple const & word)
125 Spellers::iterator it = spellers_.find(word.lang_code());
126 if (it != spellers_.end())
127 aspell_speller_add_to_personal(it->second.speller, to_utf8(word.word()).c_str(), -1);
131 void ASpell::accept(WordLangTuple const & word)
133 Spellers::iterator it = spellers_.find(word.lang_code());
134 if (it != spellers_.end())
135 aspell_speller_add_to_session(it->second.speller, to_utf8(word.word()).c_str(), -1);
139 docstring const ASpell::nextMiss()
141 char const * str = 0;
144 str = aspell_string_enumeration_next(els);
146 return (str ? from_utf8(str) : docstring());
150 docstring const ASpell::error()
152 char const * err = 0;
154 if (spell_error_object && aspell_error_number(spell_error_object) != 0)
155 err = aspell_error_message(spell_error_object);
157 // FIXME UNICODE: err is not in UTF8, but probably the locale encoding
158 return (err ? from_utf8(err) : docstring());