]> git.lyx.org Git - lyx.git/blob - src/frontends/controllers/ControlSpellchecker.C
small fix
[lyx.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                 if (!cur.pit()->isInset(cur.pos()))
175                         str += cur.pit()->getChar(cur.pos());
176         }
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 = distance(beg, cur);
199         int const total = start + distance(cur, end);
200
201         if (cur != buffer()->pos_iterator_begin())
202                 for (; cur != end && isLetter(cur); ++cur, ++start);
203
204         
205         while (res == SpellBase::OK || res == SpellBase::IGNORE) {
206                 word_ = nextWord(cur, end, start, buffer()->params());
207
208                 // end of document
209                 if (word_.word().empty())
210                         break;
211
212                 ++count_;
213
214                 // Update slider if and only if value has changed
215                 float progress = total ? float(start)/total : 1;
216                 newvalue_ = int(100.0 * progress);
217                 if (newvalue_!= oldval_) {
218                         lyxerr[Debug::GUI] << "Updating spell progress." << endl;
219                         oldval_ = newvalue_;
220                         // set progress bar
221                         view().partialUpdate(SPELL_PROGRESSED);
222                 }
223
224                 // speller might be dead ...
225                 if (!checkAlive())
226                         return;
227
228                 res = speller_->check(word_);
229
230                 // ... or it might just be reporting an error
231                 if (!checkAlive())
232                         return;
233         }
234
235         lyxerr[Debug::GUI] << "Found word \"" << word_.word() << "\"" << endl;
236
237         if (!word_.word().empty()) {
238                 int const size = word_.word().size();
239                 advance(cur, -size);
240                 bv_funcs::put_selection_at(bufferview(), cur, size, false);
241                 advance(cur, size);
242         } else {
243                 showSummary();
244                 endSession();
245                 return;
246         }
247
248         // set suggestions
249         if (res != SpellBase::OK && res != SpellBase::IGNORE) {
250                 lyxerr[Debug::GUI] << "Found a word needing checking." << endl;
251                 view().partialUpdate(SPELL_FOUND_WORD);
252         }
253 }
254
255
256 bool ControlSpellchecker::checkAlive()
257 {
258         if (speller_->alive() && speller_->error().empty())
259                 return true;
260
261         string message = speller_->error();
262         if (message.empty())
263                 message = _("The spell-checker has died for some reason.\n"
264                          "Maybe it has been killed.");
265
266         view().hide();
267         speller_.reset(0);
268
269         Alert::error(_("The spell-checker has failed"), message);
270         return false;
271 }
272
273
274 void ControlSpellchecker::showSummary()
275 {
276         if (!checkAlive() || count_ == 0) {
277                 view().hide();
278                 return;
279         }
280
281         string message;
282         if (count_ != 1)
283                 message = bformat(_("%1$s words checked."), tostr(count_));
284         else
285                 message = _("One word checked.");
286
287         view().hide();
288         Alert::information(_("Spell-checking is complete"), message);
289 }
290
291
292 void ControlSpellchecker::replace(string const & replacement)
293 {
294         bufferview()->replaceWord(replacement);
295         // fix up the count
296         --count_;
297         check();
298 }
299
300
301 void ControlSpellchecker::replaceAll(string const & replacement)
302 {
303         // TODO: add to list
304         replace(replacement);
305 }
306
307
308 void ControlSpellchecker::insert()
309 {
310         speller_->insert(word_);
311         check();
312 }
313
314
315 string const ControlSpellchecker::getSuggestion() const
316 {
317         return speller_->nextMiss();
318 }
319
320
321 string const ControlSpellchecker::getWord() const
322 {
323         return word_.word();
324 }
325
326
327 void ControlSpellchecker::ignoreAll()
328 {
329         speller_->accept(word_);
330         check();
331 }