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