]> git.lyx.org Git - lyx.git/blob - src/AspellChecker.cpp
Make the fake sequence for braces highly unlikely (addressing #6478).
[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 and variety
49         AspellSpeller * addSpeller(string const & lang,
50                                    string const & variety = string());
51
52         ///
53         AspellSpeller * speller(string const & lang,
54                                 string const & variety);
55
56         /// create a unique ID from lang code and variety
57         string const spellerID(string const & lang,
58                                string const & variety);
59
60         /// the spellers
61         Spellers spellers_;
62
63         /// FIXME
64         AspellCanHaveError * spell_error_object;
65 };
66
67
68 AspellChecker::Private::~Private()
69 {
70         if (spell_error_object) {
71                 delete_aspell_can_have_error(spell_error_object);
72                 spell_error_object = 0;
73         }
74
75         Spellers::iterator it = spellers_.begin();
76         Spellers::iterator end = spellers_.end();
77
78         for (; it != end; ++it) {
79                 aspell_speller_save_all_word_lists(it->second.speller);
80                 delete_aspell_speller(it->second.speller);
81                 delete_aspell_config(it->second.config);
82         }
83 }
84
85
86 AspellSpeller * AspellChecker::Private::addSpeller(string const & lang,
87                                                    string const & variety)
88 {
89         AspellConfig * config = new_aspell_config();
90         // Aspell supports both languages and varieties (such as German
91         // old vs. new spelling). The respective naming convention is
92         // lang_REGION-variety (e.g. de_DE-alt).
93         aspell_config_replace(config, "lang", lang.c_str());
94         if (!variety.empty())
95                 aspell_config_replace(config, "variety", variety.c_str());
96         // Set the encoding to utf-8.
97         // aspell does also understand "ucs-4", so we would not need a
98         // conversion in theory, but if this is used it expects all
99         // char const * arguments to be a cast from  uint const *, and it
100         // seems that this uint is not compatible with our char_type on some
101         // platforms (cygwin, OS X). Therefore we use utf-8, that does
102         // always work.
103         aspell_config_replace(config, "encoding", "utf-8");
104         if (lyxrc.spellchecker_accept_compound)
105                 // Consider run-together words as legal compounds
106                 aspell_config_replace(config, "run-together", "true");
107         else
108                 // Report run-together words as errors
109                 aspell_config_replace(config, "run-together", "false");
110         AspellCanHaveError * err = new_aspell_speller(config);
111         if (spell_error_object)
112                 delete_aspell_can_have_error(spell_error_object);
113         spell_error_object = 0;
114
115         if (aspell_error_number(err) != 0) {
116                 // FIXME: We should we indicate somehow that this language is not
117                 // supported.
118                 spell_error_object = err;
119                 return 0;
120         }
121         Speller m;
122         m.speller = to_aspell_speller(err);
123         m.config = config;
124         spellers_[spellerID(lang, variety)] = m;
125         return m.speller;
126 }
127
128
129 AspellSpeller * AspellChecker::Private::speller(string const & lang,
130                                                 string const & variety)
131 {
132         Spellers::iterator it = spellers_.find(spellerID(lang, variety));
133         if (it != spellers_.end())
134                 return it->second.speller;
135         
136         return addSpeller(lang, variety);
137 }
138
139
140 string const AspellChecker::Private::spellerID(string const & lang,
141                                         string const & variety)
142 {
143         if (variety.empty())
144                 return lang;
145         return lang + "-" + variety;
146 }
147
148
149 AspellChecker::AspellChecker(): d(new Private)
150 {
151 }
152
153
154 AspellChecker::~AspellChecker()
155 {
156         delete d;
157 }
158
159
160 SpellChecker::Result AspellChecker::check(WordLangTuple const & word)
161 {
162   
163         AspellSpeller * m =
164                 d->speller(word.lang_code(), word.lang_variety());
165
166         if (!m)
167                 return OK;
168
169         if (word.word().empty())
170                 // MSVC compiled Aspell doesn't like it.
171                 return OK;
172
173         int const word_ok = aspell_speller_check(m, to_utf8(word.word()).c_str(), -1);
174         LASSERT(word_ok != -1, /**/);
175
176         return (word_ok) ? OK : UNKNOWN_WORD;
177 }
178
179
180 void AspellChecker::insert(WordLangTuple const & word)
181 {
182         Spellers::iterator it = d->spellers_.find(
183                 d->spellerID(word.lang_code(), word.lang_variety()));
184         if (it != d->spellers_.end())
185                 aspell_speller_add_to_personal(it->second.speller, to_utf8(word.word()).c_str(), -1);
186 }
187
188
189 void AspellChecker::accept(WordLangTuple const & word)
190 {
191         Spellers::iterator it = d->spellers_.find(
192                 d->spellerID(word.lang_code(), word.lang_variety()));
193         if (it != d->spellers_.end())
194                 aspell_speller_add_to_session(it->second.speller, to_utf8(word.word()).c_str(), -1);
195 }
196
197
198 void AspellChecker::suggest(WordLangTuple const & wl,
199         docstring_list & suggestions)
200 {
201         suggestions.clear();
202         AspellSpeller * m =
203                 d->speller(wl.lang_code(), wl.lang_variety());
204
205         if (!m)
206                 return;
207
208         AspellWordList const * sugs =
209                 aspell_speller_suggest(m, to_utf8(wl.word()).c_str(), -1);
210         LASSERT(sugs != 0, /**/);
211         AspellStringEnumeration * els = aspell_word_list_elements(sugs);
212         if (!els || aspell_word_list_empty(sugs))
213                 return;
214
215         for (;;) {
216                 char const * str = aspell_string_enumeration_next(els);
217                 if (!str)
218                         break;
219                 suggestions.push_back(from_utf8(str));
220         }
221
222         delete_aspell_string_enumeration(els);
223 }
224
225
226 docstring const AspellChecker::error()
227 {
228         char const * err = 0;
229
230         if (d->spell_error_object && aspell_error_number(d->spell_error_object) != 0)
231                 err = aspell_error_message(d->spell_error_object);
232
233         // FIXME UNICODE: err is not in UTF8, but probably the locale encoding
234         return (err ? from_utf8(err) : docstring());
235 }
236
237
238 } // namespace lyx