]> git.lyx.org Git - features.git/blob - src/frontends/controllers/ControlSpellchecker.C
spellcheck cleanup
[features.git] / src / frontends / controllers / ControlSpellchecker.C
1 /**
2  * \file ControlSpellchecker.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Edwin Leuven
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "ControlSpellchecker.h"
14 #include "ViewBase.h"
15
16 #include "buffer.h"
17 #include "bufferparams.h"
18 #include "BufferView.h"
19 #include "bufferview_funcs.h"
20 #include "debug.h"
21 #include "gettext.h"
22 #include "language.h"
23 #include "lyxrc.h"
24
25 #include "PosIterator.h"
26 #include "paragraph.h"
27
28 #include "ispell.h"
29 #ifdef USE_PSPELL
30 # include "pspell.h"
31 #else
32 #ifdef USE_ASPELL
33 # include "aspell_local.h"
34 #endif
35 #endif
36
37 #include "support/tostr.h"
38
39 #include "frontends/Alert.h"
40
41
42 using lyx::support::bformat;
43
44 using std::endl;
45 using std::string;
46
47
48 ControlSpellchecker::ControlSpellchecker(LyXView & lv, Dialogs & d)
49         : ControlDialogBD(lv, d),
50           oldval_(0), newvalue_(0), count_(0)
51 {}
52
53
54 ControlSpellchecker::~ControlSpellchecker()
55 {}
56
57
58 void ControlSpellchecker::setParams()
59 {
60         lyxerr[Debug::GUI] << "spell setParams" << endl;
61         startSession();
62 }
63
64
65 void ControlSpellchecker::clearParams()
66 {
67         lyxerr[Debug::GUI] << "spell clearParams" << endl;
68         endSession();
69 }
70
71
72 namespace {
73
74
75 SpellBase * getSpeller(BufferParams const & bp)
76 {
77         string lang = (lyxrc.isp_use_alt_lang)
78                       ? lyxrc.isp_alt_lang
79                       : bp.language->code();
80
81 #ifdef USE_ASPELL
82         if (lyxrc.use_spell_lib)
83                 return new ASpell(bp, lang);
84 #endif
85 #ifdef USE_PSPELL
86         if (lyxrc.use_spell_lib)
87                 return new PSpell(bp, lang);
88 #endif
89
90         lang = (lyxrc.isp_use_alt_lang) ?
91                 lyxrc.isp_alt_lang : bp.language->lang();
92
93         return new ISpell(bp, lang);
94 }
95
96 }
97
98
99 void ControlSpellchecker::startSession()
100 {
101         lyxerr[Debug::GUI] << "spell startSession" << endl;
102
103         if (speller_.get()) {
104                 lyxerr[Debug::GUI] << "startSession: speller exists" << endl;
105                 speller_.reset(0);
106                 return;
107         }
108
109         speller_.reset(getSpeller(buffer()->params()));
110
111         // reset values to initial
112         oldval_ = 0;
113         newvalue_ = 0;
114         count_ = 0;
115         emergency_exit_ = false;
116
117         // start off the check
118         if (speller_->error().empty()) {
119                 check();
120                 return;
121         }
122
123         emergency_exit_ = true;
124         string message = speller_->error();
125         if (message.empty())
126                 message = _("The spell-checker could not be started.\n"
127                          "Maybe it is mis-configured.");
128
129         Alert::error(_("The spell-checker has failed"), message);
130         speller_.reset(0);
131 }
132
133
134 void ControlSpellchecker::endSession()
135 {
136         lyxerr[Debug::GUI] << "spell endSession" << endl;
137
138         emergency_exit_ = true;
139
140         if (!speller_.get()) {
141                 lyxerr[Debug::GUI] << "endSession with no speller" << endl;
142                 return;
143         }
144
145         speller_.reset(0);
146 }
147
148
149 namespace {
150
151
152 bool isLetter(PosIterator & cur)
153 {
154         return !cur.at_end()
155                 && cur.pit()->isLetter(cur.pos())
156                 && !isDeletedText(*cur.pit(), cur.pos());
157 }
158
159
160 WordLangTuple nextWord(PosIterator & cur, PosIterator const & end,
161                        int & progress, BufferParams & bp)
162 {
163         // skip until we have real text (will jump paragraphs)
164         for (; cur != end && !isLetter(cur); ++cur, ++progress);
165         
166         if (cur == end)
167                 return WordLangTuple(string(), string());
168
169         string lang_code = cur.pit()->getFontSettings(bp, cur.pos()).language()->code();
170         string str;
171         // and find the end of the word (insets like optional hyphens
172         // and ligature break are part of a word)
173         for (; cur != end && isLetter(cur); ++cur, ++progress)
174                 str += cur.pit()->getChar(cur.pos());
175
176         return WordLangTuple(str, lang_code);
177 }
178
179
180 } //namespace anon
181
182
183
184
185 void ControlSpellchecker::check()
186 {
187         lyxerr[Debug::GUI] << "spell check a word" << endl;
188
189         SpellBase::Result res = SpellBase::OK;
190
191         PosIterator cur(*bufferview());
192         PosIterator const beg = buffer()->pos_iterator_begin();
193         PosIterator const end = buffer()->pos_iterator_end();
194
195         int start = distance(beg, cur);
196         int const total = start + distance(cur, end);
197
198         if (cur != buffer()->pos_iterator_begin())
199                 for (; cur != end && isLetter(cur); ++cur, ++start);
200
201         
202         while (res == SpellBase::OK || res == SpellBase::IGNORE) {
203                 word_ = nextWord(cur, end, start, buffer()->params());
204
205                 // end of document
206                 if (word_.word().empty())
207                         break;
208
209                 ++count_;
210
211                 // Update slider if and only if value has changed
212                 float progress = total ? float(start)/total : 1;
213                 newvalue_ = int(100.0 * progress);
214                 if (newvalue_!= oldval_) {
215                         lyxerr[Debug::GUI] << "Updating spell progress." << endl;
216                         oldval_ = newvalue_;
217                         // set progress bar
218                         view().partialUpdate(SPELL_PROGRESSED);
219                 }
220
221                 // speller might be dead ...
222                 if (!checkAlive())
223                         return;
224
225                 res = speller_->check(word_);
226
227                 // ... or it might just be reporting an error
228                 if (!checkAlive())
229                         return;
230         }
231
232         lyxerr[Debug::GUI] << "Found word \"" << word_.word() << "\"" << endl;
233
234         if (!word_.word().empty()) {
235                 int const size = word_.word().size();
236                 advance(cur, -size);
237                 bv_funcs::put_selection_at(bufferview(), cur, size, false);
238                 advance(cur, size);
239         } else {
240                 showSummary();
241                 endSession();
242                 return;
243         }
244
245         // set suggestions
246         if (res != SpellBase::OK && res != SpellBase::IGNORE) {
247                 lyxerr[Debug::GUI] << "Found a word needing checking." << endl;
248                 view().partialUpdate(SPELL_FOUND_WORD);
249         }
250 }
251
252
253 bool ControlSpellchecker::checkAlive()
254 {
255         if (speller_->alive() && speller_->error().empty())
256                 return true;
257
258         string message = speller_->error();
259         if (message.empty())
260                 message = _("The spell-checker has died for some reason.\n"
261                          "Maybe it has been killed.");
262
263         view().hide();
264         speller_.reset(0);
265
266         Alert::error(_("The spell-checker has failed"), message);
267         return false;
268 }
269
270
271 void ControlSpellchecker::showSummary()
272 {
273         if (!checkAlive() || count_ == 0) {
274                 view().hide();
275                 return;
276         }
277
278         string message;
279         if (count_ != 1)
280                 message = bformat(_("%1$s words checked."), tostr(count_));
281         else
282                 message = _("One word checked.");
283
284         view().hide();
285         Alert::information(_("Spell-checking is complete"), message);
286 }
287
288
289 void ControlSpellchecker::replace(string const & replacement)
290 {
291         bufferview()->replaceWord(replacement);
292         // fix up the count
293         --count_;
294         check();
295 }
296
297
298 void ControlSpellchecker::replaceAll(string const & replacement)
299 {
300         // TODO: add to list
301         replace(replacement);
302 }
303
304
305 void ControlSpellchecker::insert()
306 {
307         speller_->insert(word_);
308         check();
309 }
310
311
312 string const ControlSpellchecker::getSuggestion() const
313 {
314         return speller_->nextMiss();
315 }
316
317
318 string const ControlSpellchecker::getWord() const
319 {
320         return word_.word();
321 }
322
323
324 void ControlSpellchecker::ignoreAll()
325 {
326         speller_->accept(word_);
327         check();
328 }