]> git.lyx.org Git - features.git/blob - src/frontends/controllers/ControlSpellchecker.C
use std::advance and std::distance instead of home-grown versions
[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                 && (!cur.inset() || cur.inset()->allowSpellCheck());
158 }
159
160
161 WordLangTuple nextWord(PosIterator & cur, PosIterator const & end,
162                        int & progress, BufferParams & bp)
163 {
164         // skip until we have real text (will jump paragraphs)
165         for (; cur != end && !isLetter(cur); ++cur, ++progress);
166
167         if (cur == end)
168                 return WordLangTuple(string(), string());
169
170         string lang_code = cur.pit()->getFontSettings(bp, cur.pos()).language()->code();
171         string str;
172         // and find the end of the word (insets like optional hyphens
173         // and ligature break are part of a word)
174         for (; cur != end && isLetter(cur); ++cur, ++progress) {
175                 if (!cur.pit()->isInset(cur.pos()))
176                         str += cur.pit()->getChar(cur.pos());
177         }
178
179         return WordLangTuple(str, lang_code);
180 }
181
182
183 } //namespace anon
184
185
186
187
188 void ControlSpellchecker::check()
189 {
190         lyxerr[Debug::GUI] << "spell check a word" << endl;
191
192         SpellBase::Result res = SpellBase::OK;
193
194         PosIterator cur(*bufferview());
195         PosIterator const beg = buffer()->pos_iterator_begin();
196         PosIterator const end = buffer()->pos_iterator_end();
197
198         int start = std::distance(beg, cur);
199         int const total = start + std::distance(cur, end);
200
201         if (cur != buffer()->pos_iterator_begin())
202                 for (; cur != end && isLetter(cur); ++cur, ++start);
203
204         while (res == SpellBase::OK || res == SpellBase::IGNORE) {
205                 word_ = nextWord(cur, end, start, buffer()->params());
206
207                 // end of document
208                 if (word_.word().empty())
209                         break;
210
211                 ++count_;
212
213                 // Update slider if and only if value has changed
214                 float progress = total ? float(start)/total : 1;
215                 newvalue_ = int(100.0 * progress);
216                 if (newvalue_!= oldval_) {
217                         lyxerr[Debug::GUI] << "Updating spell progress." << endl;
218                         oldval_ = newvalue_;
219                         // set progress bar
220                         view().partialUpdate(SPELL_PROGRESSED);
221                 }
222
223                 // speller might be dead ...
224                 if (!checkAlive())
225                         return;
226
227                 res = speller_->check(word_);
228
229                 // ... or it might just be reporting an error
230                 if (!checkAlive())
231                         return;
232         }
233
234         lyxerr[Debug::GUI] << "Found word \"" << word_.word() << "\"" << endl;
235
236         if (!word_.word().empty()) {
237                 int const size = word_.word().size();
238                 std::advance(cur, -size);
239                 bufferview()->putSelectionAt(cur, size, false);
240                 std::advance(cur, size);
241         } else {
242                 showSummary();
243                 endSession();
244                 return;
245         }
246
247         // set suggestions
248         if (res != SpellBase::OK && res != SpellBase::IGNORE) {
249                 lyxerr[Debug::GUI] << "Found a word needing checking." << endl;
250                 view().partialUpdate(SPELL_FOUND_WORD);
251         }
252 }
253
254
255 bool ControlSpellchecker::checkAlive()
256 {
257         if (speller_->alive() && speller_->error().empty())
258                 return true;
259
260         string message = speller_->error();
261         if (message.empty())
262                 message = _("The spell-checker has died for some reason.\n"
263                          "Maybe it has been killed.");
264
265         view().hide();
266         speller_.reset(0);
267
268         Alert::error(_("The spell-checker has failed"), message);
269         return false;
270 }
271
272
273 void ControlSpellchecker::showSummary()
274 {
275         if (!checkAlive() || count_ == 0) {
276                 view().hide();
277                 return;
278         }
279
280         string message;
281         if (count_ != 1)
282                 message = bformat(_("%1$s words checked."), tostr(count_));
283         else
284                 message = _("One word checked.");
285
286         view().hide();
287         Alert::information(_("Spell-checking is complete"), message);
288 }
289
290
291 void ControlSpellchecker::replace(string const & replacement)
292 {
293         bufferview()->replaceWord(replacement);
294         // fix up the count
295         --count_;
296         check();
297 }
298
299
300 void ControlSpellchecker::replaceAll(string const & replacement)
301 {
302         // TODO: add to list
303         replace(replacement);
304 }
305
306
307 void ControlSpellchecker::insert()
308 {
309         speller_->insert(word_);
310         check();
311 }
312
313
314 string const ControlSpellchecker::getSuggestion() const
315 {
316         return speller_->nextMiss();
317 }
318
319
320 string const ControlSpellchecker::getWord() const
321 {
322         return word_.word();
323 }
324
325
326 void ControlSpellchecker::ignoreAll()
327 {
328         speller_->accept(word_);
329         check();
330 }