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