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