]> git.lyx.org Git - lyx.git/blob - src/frontends/controllers/ControlSpellchecker.C
remove toggleSelection
[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 #include "buffer.h"
16 #include "BufferView.h"
17 #include "gettext.h"
18 #include "language.h"
19 #include "lyxrc.h"
20 #include "lyxtext.h"
21 #include "debug.h"
22
23 #include "ispell.h"
24 #ifdef USE_PSPELL
25 # include "pspell.h"
26 #else
27 #ifdef USE_ASPELL
28 # include "aspell_local.h"
29 #endif
30 #endif
31
32 #include "support/tostr.h"
33
34 #include "frontends/Alert.h"
35
36 using namespace lyx::support;
37
38 using std::endl;
39
40
41 ControlSpellchecker::ControlSpellchecker(LyXView & lv, Dialogs & d)
42         : ControlDialogBD(lv, d),
43           newval_(0.0), oldval_(0), newvalue_(0), count_(0)
44 {}
45
46
47 ControlSpellchecker::~ControlSpellchecker()
48 {}
49
50
51 void ControlSpellchecker::setParams()
52 {
53         lyxerr[Debug::GUI] << "spell setParams" << endl;
54         startSession();
55 }
56
57
58 void ControlSpellchecker::clearParams()
59 {
60         lyxerr[Debug::GUI] << "spell clearParams" << endl;
61         endSession();
62 }
63
64
65 namespace {
66
67 SpellBase * getSpeller(BufferParams const & bp)
68 {
69         string lang = (lyxrc.isp_use_alt_lang)
70                       ? lyxrc.isp_alt_lang
71                       : bp.language->code();
72
73 #ifdef USE_ASPELL
74         if (lyxrc.use_spell_lib)
75                 return new ASpell(bp, lang);
76 #endif
77 #ifdef USE_PSPELL
78         if (lyxrc.use_spell_lib)
79                 return new PSpell(bp, lang);
80 #endif
81
82         lang = (lyxrc.isp_use_alt_lang) ?
83                 lyxrc.isp_alt_lang : bp.language->lang();
84
85         return new ISpell(bp, lang);
86 }
87
88 }
89
90
91 void ControlSpellchecker::startSession()
92 {
93         lyxerr[Debug::GUI] << "spell startSession" << endl;
94
95         if (speller_.get()) {
96                 lyxerr[Debug::GUI] << "startSession: speller exists" << endl;
97                 speller_.reset(0);
98                 return;
99         }
100
101         speller_.reset(getSpeller(buffer()->params));
102
103         // reset values to initial
104         newval_ = 0.0;
105         oldval_ = 0;
106         newvalue_ = 0;
107         count_ = 0;
108         emergency_exit_ = false;
109
110         // start off the check
111         if (speller_->error().empty()) {
112                 check();
113                 return;
114         }
115
116         emergency_exit_ = true;
117         string message = speller_->error();
118         if (message.empty())
119                 message = _("The spell-checker could not be started.\n"
120                          "Maybe it is mis-configured.");
121
122         Alert::error(_("The spell-checker has failed"), message);
123         speller_.reset(0);
124 }
125
126
127 void ControlSpellchecker::endSession()
128 {
129         lyxerr[Debug::GUI] << "spell endSession" << endl;
130
131         bufferview()->endOfSpellCheck();
132
133         emergency_exit_ = true;
134
135         if (!speller_.get()) {
136                 lyxerr[Debug::GUI] << "endSession with no speller" << endl;
137                 return;
138         }
139
140         speller_.reset(0);
141 }
142
143
144 void ControlSpellchecker::check()
145 {
146         lyxerr[Debug::GUI] << "spell check a word" << endl;
147
148         SpellBase::Result res = SpellBase::OK;
149
150         // clear any old selection
151         LyXText * text = bufferview()->getLyXText();
152         bufferview()->update();
153
154         while (res == SpellBase::OK || res == SpellBase::IGNORE) {
155                 word_ = bufferview()->nextWord(newval_);
156
157                 // end of document
158                 if (word_.word().empty())
159                         break;
160
161                 ++count_;
162
163                 // Update slider if and only if value has changed
164                 newvalue_ = int(100.0 * newval_);
165                 if (newvalue_!= oldval_) {
166                         lyxerr[Debug::GUI] << "Updating spell progress." << endl;
167                         oldval_ = newvalue_;
168                         // set progress bar
169                         view().partialUpdate(SPELL_PROGRESSED);
170                 }
171
172                 // speller might be dead ...
173                 if (!checkAlive())
174                         return;
175
176                 res = speller_->check(word_);
177
178                 // ... or it might just be reporting an error
179                 if (!checkAlive())
180                         return;
181         }
182
183         lyxerr[Debug::GUI] << "Found word \"" << word_.word() << "\"" << endl;
184         lyxerr << "Found word \"" << word_.word() << "\"" << endl;
185
186         if (!word_.word().empty()) {
187                 bufferview()->selectLastWord();
188                 bufferview()->fitCursor();
189         } else {
190                 showSummary();
191                 endSession();
192                 return;
193         }
194
195         // set suggestions
196         if (res != SpellBase::OK && res != SpellBase::IGNORE) {
197                 lyxerr[Debug::GUI] << "Found a word needing checking." << endl;
198                 view().partialUpdate(SPELL_FOUND_WORD);
199         }
200 }
201
202
203 bool ControlSpellchecker::checkAlive()
204 {
205         if (speller_->alive() && speller_->error().empty())
206                 return true;
207
208         string message = speller_->error();
209         if (message.empty())
210                 message = _("The spell-checker has died for some reason.\n"
211                          "Maybe it has been killed.");
212
213         view().hide();
214         speller_.reset(0);
215
216         Alert::error(_("The spell-checker has failed"), message);
217         return false;
218 }
219
220
221 void ControlSpellchecker::showSummary()
222 {
223         if (!checkAlive() || count_ == 0) {
224                 view().hide();
225                 return;
226         }
227
228         string message;
229         if (count_ != 1)
230                 message = bformat(_("%1$s words checked."), tostr(count_));
231         else
232                 message = _("One word checked.");
233
234         view().hide();
235         Alert::information(_("Spell-checking is complete"), message);
236 }
237
238
239 void ControlSpellchecker::replace(string const & replacement)
240 {
241         bufferview()->replaceWord(replacement);
242         // fix up the count
243         --count_;
244         check();
245 }
246
247
248 void ControlSpellchecker::replaceAll(string const & replacement)
249 {
250         // TODO: add to list
251         replace(replacement);
252 }
253
254
255 void ControlSpellchecker::insert()
256 {
257         speller_->insert(word_);
258         check();
259 }
260
261
262 string const ControlSpellchecker::getSuggestion() const
263 {
264         return speller_->nextMiss();
265 }
266
267
268 string const ControlSpellchecker::getWord() const
269 {
270         return word_.word();
271 }
272
273
274 void ControlSpellchecker::ignoreAll()
275 {
276         speller_->accept(word_);
277         check();
278 }