]> git.lyx.org Git - lyx.git/blob - src/frontends/controllers/ControlSpellchecker.C
64 bit compile fixes.
[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 "cursor.h"
20 #include "debug.h"
21 #include "gettext.h"
22 #include "language.h"
23 #include "lyxrc.h"
24 #include "PosIterator.h"
25 #include "paragraph.h"
26
27 #include "ispell.h"
28 #ifdef USE_PSPELL
29 # include "pspell.h"
30 #else
31 #ifdef USE_ASPELL
32 # include "aspell_local.h"
33 #endif
34 #endif
35
36 #include "support/tostr.h"
37
38 #include "frontends/Alert.h"
39
40
41 using lyx::support::bformat;
42
43 using std::advance;
44 using std::distance;
45 using std::endl;
46 using std::string;
47
48
49 ControlSpellchecker::ControlSpellchecker(LyXView & lv, Dialogs & d)
50         : ControlDialogBD(lv, d),
51           oldval_(0), newvalue_(0), count_(0)
52 {}
53
54
55 ControlSpellchecker::~ControlSpellchecker()
56 {}
57
58
59 void ControlSpellchecker::setParams()
60 {
61         lyxerr[Debug::GUI] << "spell setParams" << endl;
62         startSession();
63 }
64
65
66 void ControlSpellchecker::clearParams()
67 {
68         lyxerr[Debug::GUI] << "spell clearParams" << endl;
69         endSession();
70 }
71
72
73 namespace {
74
75
76 SpellBase * getSpeller(BufferParams const & bp)
77 {
78         string lang = (lyxrc.isp_use_alt_lang)
79                       ? lyxrc.isp_alt_lang
80                       : bp.language->code();
81
82 #ifdef USE_ASPELL
83         if (lyxrc.use_spell_lib)
84                 return new ASpell(bp, lang);
85 #endif
86 #ifdef USE_PSPELL
87         if (lyxrc.use_spell_lib)
88                 return new PSpell(bp, lang);
89 #endif
90
91         lang = (lyxrc.isp_use_alt_lang) ?
92                 lyxrc.isp_alt_lang : bp.language->lang();
93
94         return new ISpell(bp, lang);
95 }
96
97 }
98
99
100 void ControlSpellchecker::startSession()
101 {
102         lyxerr[Debug::GUI] << "spell startSession" << endl;
103
104         if (speller_.get()) {
105                 lyxerr[Debug::GUI] << "startSession: speller exists" << endl;
106                 speller_.reset(0);
107                 return;
108         }
109
110         speller_.reset(getSpeller(buffer()->params()));
111
112         // reset values to initial
113         oldval_ = 0;
114         newvalue_ = 0;
115         count_ = 0;
116         emergency_exit_ = false;
117
118         // start off the check
119         if (speller_->error().empty()) {
120                 check();
121                 return;
122         }
123
124         emergency_exit_ = true;
125         string message = speller_->error();
126         if (message.empty())
127                 message = _("The spell-checker could not be started.\n"
128                          "Maybe it is mis-configured.");
129
130         Alert::error(_("The spell-checker has failed"), message);
131         speller_.reset(0);
132 }
133
134
135 void ControlSpellchecker::endSession()
136 {
137         lyxerr[Debug::GUI] << "spell endSession" << endl;
138
139         emergency_exit_ = true;
140
141         if (!speller_.get()) {
142                 lyxerr[Debug::GUI] << "endSession with no speller" << endl;
143                 return;
144         }
145
146         speller_.reset(0);
147 }
148
149
150 namespace {
151
152
153 bool isLetter(PosIterator & cur)
154 {
155         return !cur.at_end()
156                 && cur.pit()->isLetter(cur.pos())
157                 && !isDeletedText(*cur.pit(), cur.pos())
158                 && (!cur.inset() || cur.inset()->allowSpellCheck());
159 }
160
161
162 WordLangTuple nextWord(PosIterator & cur, PosIterator const & end,
163                        PosIterator::difference_type & progress, 
164                        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         PosIterator::difference_type start = distance(beg, cur);
201         PosIterator::difference_type 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                 showSummary();
240                 endSession();
241                 return;
242         }
243
244         int const size = word_.word().size();
245         advance(cur, -size);
246         bufferview()->putSelectionAt(cur, size, false);
247         advance(cur, size);
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()->cursor().replaceWord(replacement);
296         bufferview()->buffer()->markDirty();
297         bufferview()->update();
298         // fix up the count
299         --count_;
300         check();
301 }
302
303
304 void ControlSpellchecker::replaceAll(string const & replacement)
305 {
306         // TODO: add to list
307         replace(replacement);
308 }
309
310
311 void ControlSpellchecker::insert()
312 {
313         speller_->insert(word_);
314         check();
315 }
316
317
318 string const ControlSpellchecker::getSuggestion() const
319 {
320         return speller_->nextMiss();
321 }
322
323
324 string const ControlSpellchecker::getWord() const
325 {
326         return word_.word();
327 }
328
329
330 void ControlSpellchecker::ignoreAll()
331 {
332         speller_->accept(word_);
333         check();
334 }