]> git.lyx.org Git - lyx.git/blob - src/AspellChecker.cpp
Generate the magic label always. We'll need it other times, when we do
[lyx.git] / src / AspellChecker.cpp
1 /**
2  * \file AspellChecker.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Kevin Atkinson
7  * \author John Levon
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "AspellChecker.h"
15 #include "LyXRC.h"
16 #include "WordLangTuple.h"
17
18 #include "support/lassert.h"
19 #include "support/debug.h"
20 #include "support/docstring_list.h"
21
22 #include <aspell.h>
23
24 #include <map>
25 #include <string>
26
27 using namespace std;
28
29 namespace lyx {
30
31 namespace {
32
33 struct Speller {
34         AspellSpeller * speller;
35         AspellConfig * config;
36 };
37
38 typedef std::map<std::string, Speller> Spellers;
39
40 } // anon namespace
41
42 struct AspellChecker::Private
43 {
44         Private(): spell_error_object(0) {}
45
46         ~Private();
47
48         /// add a speller of the given language
49         AspellSpeller * addSpeller(string const & lang);
50
51         ///
52         AspellSpeller * speller(string const & lang);
53
54         /// the spellers
55         Spellers spellers_;
56
57         /// FIXME
58         AspellCanHaveError * spell_error_object;
59 };
60
61
62 AspellChecker::Private::~Private()
63 {
64         if (spell_error_object) {
65                 delete_aspell_can_have_error(spell_error_object);
66                 spell_error_object = 0;
67         }
68
69         Spellers::iterator it = spellers_.begin();
70         Spellers::iterator end = spellers_.end();
71
72         for (; it != end; ++it) {
73                 aspell_speller_save_all_word_lists(it->second.speller);
74                 delete_aspell_speller(it->second.speller);
75                 delete_aspell_config(it->second.config);
76         }
77 }
78
79
80 AspellSpeller * AspellChecker::Private::addSpeller(string const & lang)
81 {
82         AspellConfig * config = new_aspell_config();
83         // FIXME The aspell documentation says to use "lang"
84         aspell_config_replace(config, "language-tag", lang.c_str());
85         // Set the encoding to utf-8.
86         // aspell does also understand "ucs-4", so we would not need a
87         // conversion in theory, but if this is used it expects all
88         // char const * arguments to be a cast from  uint const *, and it
89         // seems that this uint is not compatible with our char_type on some
90         // platforms (cygwin, OS X). Therefore we use utf-8, that does
91         // always work.
92         aspell_config_replace(config, "encoding", "utf-8");
93         if (lyxrc.spellchecker_accept_compound)
94                 // Consider run-together words as legal compounds
95                 aspell_config_replace(config, "run-together", "true");
96         else
97                 // Report run-together words as errors
98                 aspell_config_replace(config, "run-together", "false");
99         AspellCanHaveError * err = new_aspell_speller(config);
100         if (spell_error_object)
101                 delete_aspell_can_have_error(spell_error_object);
102         spell_error_object = 0;
103
104         if (aspell_error_number(err) != 0) {
105                 // FIXME: We should we indicate somehow that this language is not
106                 // supported.
107                 spell_error_object = err;
108                 return 0;
109         }
110         Speller m;
111         m.speller = to_aspell_speller(err);
112         m.config = config;
113         spellers_[lang] = m;
114         return m.speller;
115 }
116
117
118 AspellSpeller * AspellChecker::Private::speller(string const & lang)
119 {
120         Spellers::iterator it = spellers_.find(lang);
121         if (it != spellers_.end())
122                 return it->second.speller;
123         
124         return addSpeller(lang);
125 }
126
127
128 AspellChecker::AspellChecker(): d(new Private)
129 {
130 }
131
132
133 AspellChecker::~AspellChecker()
134 {
135         delete d;
136 }
137
138
139 SpellChecker::Result AspellChecker::check(WordLangTuple const & word)
140 {
141         AspellSpeller * m = d->speller(word.lang_code());
142         if (!m)
143                 return OK;
144
145         if (word.word().empty())
146                 // MSVC compiled Aspell doesn't like it.
147                 return OK;
148
149         int const word_ok = aspell_speller_check(m, to_utf8(word.word()).c_str(), -1);
150         LASSERT(word_ok != -1, /**/);
151
152         return (word_ok) ? OK : UNKNOWN_WORD;
153 }
154
155
156 void AspellChecker::insert(WordLangTuple const & word)
157 {
158         Spellers::iterator it = d->spellers_.find(word.lang_code());
159         if (it != d->spellers_.end())
160                 aspell_speller_add_to_personal(it->second.speller, to_utf8(word.word()).c_str(), -1);
161 }
162
163
164 void AspellChecker::accept(WordLangTuple const & word)
165 {
166         Spellers::iterator it = d->spellers_.find(word.lang_code());
167         if (it != d->spellers_.end())
168                 aspell_speller_add_to_session(it->second.speller, to_utf8(word.word()).c_str(), -1);
169 }
170
171
172 void AspellChecker::suggest(WordLangTuple const & wl,
173         docstring_list & suggestions)
174 {
175         suggestions.clear();
176         AspellSpeller * m = d->speller(wl.lang_code());
177         if (!m)
178                 return;
179
180         AspellWordList const * sugs =
181                 aspell_speller_suggest(m, to_utf8(wl.word()).c_str(), -1);
182         LASSERT(sugs != 0, /**/);
183         AspellStringEnumeration * els = aspell_word_list_elements(sugs);
184         if (!els || aspell_word_list_empty(sugs))
185                 return;
186
187         for (;;) {
188                 char const * str = aspell_string_enumeration_next(els);
189                 if (!str)
190                         break;
191                 suggestions.push_back(from_utf8(str));
192         }
193
194         delete_aspell_string_enumeration(els);
195 }
196
197
198 docstring const AspellChecker::error()
199 {
200         char const * err = 0;
201
202         if (d->spell_error_object && aspell_error_number(d->spell_error_object) != 0)
203                 err = aspell_error_message(d->spell_error_object);
204
205         // FIXME UNICODE: err is not in UTF8, but probably the locale encoding
206         return (err ? from_utf8(err) : docstring());
207 }
208
209
210 } // namespace lyx