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