]> git.lyx.org Git - lyx.git/blob - src/frontends/controllers/ControlSpellchecker.C
s/DocumentIterator/DocIterator/g a.k.a showing off my sed abilities. Sorry for the...
[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 "CutAndPaste.h"
21 #include "debug.h"
22 #include "gettext.h"
23 #include "language.h"
24 #include "lyxrc.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 bool isLetter(DocIterator const & cur)
153 {
154         return cur.inTexted()
155                 && cur.inset().allowSpellCheck()
156                 && cur.pos() != cur.lastpos()
157                 && cur.paragraph().isLetter(cur.pos())
158                 && !isDeletedText(cur.paragraph(), cur.pos());
159 }
160
161
162 WordLangTuple nextWord(DocIterator & cur, ptrdiff_t & progress,
163         BufferParams & bp)
164 {
165         // skip until we have real text (will jump paragraphs)
166         for (; cur.size() && !isLetter(cur); cur.forwardPos());
167                 ++progress;
168
169         // hit end
170         if (cur.empty())
171                 return WordLangTuple(string(), string());
172
173         string lang_code = cur.paragraph().
174                 getFontSettings(bp, cur.pos()).language()->code();
175         string str;
176         // and find the end of the word (insets like optional hyphens
177         // and ligature break are part of a word)
178         for (; cur && isLetter(cur); cur.forwardPos(), ++progress) {
179                 if (!cur.paragraph().isInset(cur.pos()))
180                         str += cur.paragraph().getChar(cur.pos());
181         }
182
183         return WordLangTuple(str, lang_code);
184 }
185
186 } // namespace anon
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         DocIterator cur = bufferview()->cursor();
197
198         // a rough estimate should be sufficient:
199         //DocIterator::difference_type start = distance(beg, cur);
200         //DocIterator::difference_type const total = start + distance(cur, end);
201
202         ptrdiff_t start = 0, total = 0;
203         DocIterator it = DocIterator(buffer()->inset());
204         for (start = 0; it != cur; it.forwardPos())
205                 ++start;        
206
207         for (total = start; it; it.forwardPos())
208                 ++total;        
209
210         for (; cur && isLetter(cur); cur.forwardPos())
211                 ++start;
212
213         while (res == SpellBase::OK || res == SpellBase::IGNORE) {
214                 word_ = nextWord(cur, start, buffer()->params());
215
216                 // end of document
217                 if (word_.word().empty())
218                         break;
219
220                 ++count_;
221
222                 // Update slider if and only if value has changed
223                 float progress = total ? float(start)/total : 1;
224                 newvalue_ = int(100.0 * progress);
225                 if (newvalue_!= oldval_) {
226                         lyxerr[Debug::GUI] << "Updating spell progress." << endl;
227                         oldval_ = newvalue_;
228                         // set progress bar
229                         view().partialUpdate(SPELL_PROGRESSED);
230                 }
231
232                 // speller might be dead ...
233                 if (!checkAlive())
234                         return;
235
236                 res = speller_->check(word_);
237
238                 // ... or it might just be reporting an error
239                 if (!checkAlive())
240                         return;
241         }
242
243         lyxerr[Debug::GUI] << "Found word \"" << word_.word() << "\"" << endl;
244
245         if (word_.word().empty()) {
246                 showSummary();
247                 endSession();
248                 return;
249         }
250
251         int const size = word_.word().size();
252 #if 0
253         advance(cur, -size);
254         bufferview()->putSelectionAt(cur, size, false);
255         advance(cur, size);
256 #else
257         bufferview()->putSelectionAt(cur, size, true);
258 #endif
259
260         // set suggestions
261         if (res != SpellBase::OK && res != SpellBase::IGNORE) {
262                 lyxerr[Debug::GUI] << "Found a word needing checking." << endl;
263                 view().partialUpdate(SPELL_FOUND_WORD);
264         }
265 }
266
267
268 bool ControlSpellchecker::checkAlive()
269 {
270         if (speller_->alive() && speller_->error().empty())
271                 return true;
272
273         string message = speller_->error();
274         if (message.empty())
275                 message = _("The spell-checker has died for some reason.\n"
276                          "Maybe it has been killed.");
277
278         view().hide();
279         speller_.reset(0);
280
281         Alert::error(_("The spell-checker has failed"), message);
282         return false;
283 }
284
285
286 void ControlSpellchecker::showSummary()
287 {
288         if (!checkAlive() || count_ == 0) {
289                 view().hide();
290                 return;
291         }
292
293         string message;
294         if (count_ != 1)
295                 message = bformat(_("%1$s words checked."), tostr(count_));
296         else
297                 message = _("One word checked.");
298
299         view().hide();
300         Alert::information(_("Spell-checking is complete"), message);
301 }
302
303
304 void ControlSpellchecker::replace(string const & replacement)
305 {
306         lyx::cap::replaceWord(bufferview()->cursor(), replacement);
307         bufferview()->buffer()->markDirty();
308         bufferview()->update();
309         // fix up the count
310         --count_;
311         check();
312 }
313
314
315 void ControlSpellchecker::replaceAll(string const & replacement)
316 {
317         // TODO: add to list
318         replace(replacement);
319 }
320
321
322 void ControlSpellchecker::insert()
323 {
324         speller_->insert(word_);
325         check();
326 }
327
328
329 string const ControlSpellchecker::getSuggestion() const
330 {
331         return speller_->nextMiss();
332 }
333
334
335 string const ControlSpellchecker::getWord() const
336 {
337         return word_.word();
338 }
339
340
341 void ControlSpellchecker::ignoreAll()
342 {
343         speller_->accept(word_);
344         check();
345 }