]> git.lyx.org Git - lyx.git/blob - src/frontends/controllers/ControlSpellchecker.C
fix bug 1950: Spellchecker runs twice
[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
15 #include "buffer.h"
16 #include "bufferparams.h"
17 #include "BufferView.h"
18 #include "cursor.h"
19 #include "CutAndPaste.h"
20 #include "debug.h"
21 #include "gettext.h"
22 #include "language.h"
23 #include "lyxrc.h"
24 #include "paragraph.h"
25
26 #if defined(USE_ASPELL)
27 # include "aspell_local.h"
28 #elif defined(USE_PSPELL)
29 # include "pspell.h"
30 #endif
31
32 #if defined(USE_ISPELL)
33 # include "ispell.h"
34 #else
35 # include "SpellBase.h"
36 #endif
37
38 #include "support/textutils.h"
39 #include "support/convert.h"
40
41 #include "frontends/Alert.h"
42
43 using std::advance;
44 using std::distance;
45 using std::endl;
46 using std::string;
47
48 namespace lyx {
49
50 using support::bformat;
51 using support::contains;
52
53 namespace frontend {
54
55
56 ControlSpellchecker::ControlSpellchecker(Dialog & parent)
57         : Dialog::Controller(parent), exitEarly_(false),
58           oldval_(0), newvalue_(0), count_(0)
59 {}
60
61
62 ControlSpellchecker::~ControlSpellchecker()
63 {}
64
65
66 namespace {
67
68 SpellBase * getSpeller(BufferParams const & bp)
69 {
70         string lang = (lyxrc.isp_use_alt_lang)
71                       ? lyxrc.isp_alt_lang
72                       : bp.language->code();
73
74 #if defined(USE_ASPELL)
75         if (lyxrc.use_spell_lib)
76                 return new ASpell(bp, lang);
77 #elif defined(USE_PSPELL)
78         if (lyxrc.use_spell_lib)
79                 return new PSpell(bp, lang);
80 #endif
81
82 #if defined(USE_ISPELL)
83         lang = (lyxrc.isp_use_alt_lang) ?
84                 lyxrc.isp_alt_lang : bp.language->lang();
85
86         return new ISpell(bp, lang);
87 #else
88         return new SpellBase;
89 #endif
90 }
91
92 } // namespace anon
93
94
95 bool ControlSpellchecker::initialiseParams(std::string const &)
96 {
97         lyxerr[Debug::GUI] << "Spellchecker::initialiseParams" << endl;
98
99         speller_.reset(getSpeller(kernel().buffer().params()));
100         if (!speller_.get())
101                 return false;
102
103         // reset values to initial
104         oldval_ = 0;
105         newvalue_ = 0;
106         count_ = 0;
107
108         bool const success = speller_->error().empty();
109
110         if (!success) {
111                 Alert::error(_("The spell-checker could not be started"),
112                              speller_->error());
113                 speller_.reset(0);
114         }
115
116         return success;
117 }
118
119
120 void ControlSpellchecker::clearParams()
121 {
122         lyxerr[Debug::GUI] << "Spellchecker::clearParams" << endl;
123         speller_.reset(0);
124 }
125
126
127 namespace {
128
129 bool isLetter(DocIterator const & cur)
130 {
131         return cur.inTexted()
132                 && cur.inset().allowSpellCheck()
133                 && cur.pos() != cur.lastpos()
134                 && (cur.paragraph().isLetter(cur.pos())
135                     // We want to pass the ' and escape chars to ispell
136                     || contains(lyxrc.isp_esc_chars + '\'',
137                                 cur.paragraph().getChar(cur.pos())))
138                 && !isDeletedText(cur.paragraph(), cur.pos());
139 }
140
141
142 WordLangTuple nextWord(DocIterator & cur, ptrdiff_t & progress,
143         BufferParams & bp)
144 {
145         bool inword = false;
146         bool ignoreword = false;
147         string word, lang_code;
148
149         while (cur.depth()) {
150                 if (isLetter(cur)) {
151                         if (!inword) {
152                                 inword = true;
153                                 ignoreword = false;
154                                 word.clear();
155                                 lang_code = cur.paragraph().getFontSettings(bp, cur.pos()).language()->code();
156                         }
157                         // Insets like optional hyphens and ligature
158                         // break are part of a word.
159                         if (!cur.paragraph().isInset(cur.pos())) {
160                                 Paragraph::value_type const c =
161                                         cur.paragraph().getChar(cur.pos());
162                                 word += c;
163                                 if (IsDigit(c))
164                                         ignoreword = true;
165                         }
166                 } else { // !isLetter(cur)
167                         if (inword)
168                                 if (!ignoreword)
169                                         return WordLangTuple(word, lang_code);
170                                 else
171                                         inword = false;
172                 }
173
174                 cur.forwardPos();
175                 ++progress;
176         }
177
178         return WordLangTuple(string(), string());
179 }
180
181 } // namespace anon
182
183
184
185 void ControlSpellchecker::check()
186 {
187         lyxerr[Debug::GUI] << "spell check a word" << endl;
188
189         SpellBase::Result res = SpellBase::OK;
190
191         DocIterator cur = kernel().bufferview()->cursor();
192
193         ptrdiff_t start = 0, total = 0;
194         DocIterator it = DocIterator(kernel().buffer().inset());
195         for (start = 0; it != cur; it.forwardPos())
196                 ++start;
197
198         for (total = start; it; it.forwardPos())
199                 ++total;
200
201         for (; cur && cur.pos() && isLetter(cur); cur.forwardPos())
202                 ++start;
203
204         BufferParams & bufferparams = kernel().buffer().params();
205         exitEarly_ = false;
206
207         while (res == SpellBase::OK || res == SpellBase::IGNORED_WORD) {
208                 word_ = nextWord(cur, start, bufferparams);
209
210                 // end of document
211                 if (getWord().empty()) {
212                         showSummary();
213                         exitEarly_ = true;
214                         return;
215                 }
216
217                 ++count_;
218
219                 // Update slider if and only if value has changed
220                 float progress = total ? float(start)/total : 1;
221                 newvalue_ = int(100.0 * progress);
222                 if (newvalue_!= oldval_) {
223                         lyxerr[Debug::GUI] << "Updating spell progress." << endl;
224                         oldval_ = newvalue_;
225                         // set progress bar
226                         dialog().view().partialUpdate(SPELL_PROGRESSED);
227                 }
228
229                 // speller might be dead ...
230                 if (!checkAlive())
231                         return;
232
233                 res = speller_->check(word_);
234
235                 // ... or it might just be reporting an error
236                 if (!checkAlive())
237                         return;
238         }
239
240         lyxerr[Debug::GUI] << "Found word \"" << getWord() << "\"" << endl;
241
242         int const size = getWord().size();
243         cur.pos() -= size;
244         kernel().bufferview()->putSelectionAt(cur, size, false);
245         // if we used a lfun like in find/replace, dispatch would do
246         // that for us
247         kernel().bufferview()->update();
248
249         // set suggestions
250         if (res != SpellBase::OK && res != SpellBase::IGNORED_WORD) {
251                 lyxerr[Debug::GUI] << "Found a word needing checking." << endl;
252                 dialog().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         dialog().CancelButton();
268
269         Alert::error(_("The spell-checker has failed"), message);
270         return false;
271 }
272
273
274 void ControlSpellchecker::showSummary()
275 {
276         if (!checkAlive() || count_ == 0) {
277                 dialog().CancelButton();
278                 return;
279         }
280
281         string message;
282         if (count_ != 1)
283                 message = bformat(_("%1$d words checked."), count_);
284         else
285                 message = _("One word checked.");
286
287         dialog().CancelButton();
288         Alert::information(_("Spell-checking is complete"), message);
289 }
290
291
292 void ControlSpellchecker::replace(string const & replacement)
293 {
294         lyxerr << "ControlSpellchecker::replace("
295                << replacement << ")" << std::endl;
296         BufferView & bufferview = *kernel().bufferview();
297         cap::replaceWord(bufferview.cursor(), replacement);
298         kernel().buffer().markDirty();
299         bufferview.update();
300         // fix up the count
301         --count_;
302         check();
303 }
304
305
306 void ControlSpellchecker::replaceAll(string const & replacement)
307 {
308         // TODO: add to list
309         replace(replacement);
310 }
311
312
313 void ControlSpellchecker::insert()
314 {
315         speller_->insert(word_);
316         check();
317 }
318
319
320 string const ControlSpellchecker::getSuggestion() const
321 {
322         return speller_->nextMiss();
323 }
324
325
326 string const ControlSpellchecker::getWord() const
327 {
328         return word_.word();
329 }
330
331
332 void ControlSpellchecker::ignoreAll()
333 {
334         speller_->accept(word_);
335         check();
336 }
337
338 } // namespace frontend
339 } // namespace lyx