]> git.lyx.org Git - features.git/blob - src/frontends/controllers/ControlSpellchecker.C
2aa74e0173bfd7fb8ab45c4e272962b1a42cbd3f
[features.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(_("Spellchecker error"),
112                              _("The spellchecker could not be started\n")
113                              + speller_->error());
114                 speller_.reset(0);
115         }
116
117         return success;
118 }
119
120
121 void ControlSpellchecker::clearParams()
122 {
123         lyxerr[Debug::GUI] << "Spellchecker::clearParams" << endl;
124         speller_.reset(0);
125 }
126
127
128 namespace {
129
130 bool isLetter(DocIterator const & dit)
131 {
132         return dit.inTexted()
133                 && dit.inset().allowSpellCheck()
134                 && dit.pos() != dit.lastpos()
135                 && (dit.paragraph().isLetter(dit.pos())
136                     // We want to pass the ' and escape chars to ispell
137                     || contains(lyxrc.isp_esc_chars + '\'',
138                                 dit.paragraph().getChar(dit.pos())))
139                 && !isDeletedText(dit.paragraph(), dit.pos());
140 }
141
142
143 WordLangTuple nextWord(LCursor & cur, ptrdiff_t & progress)
144 {
145         BufferParams const & bp = cur.bv().buffer()->params();
146         bool inword = false;
147         bool ignoreword = false;
148         cur.resetAnchor();
149         string word, lang_code;
150
151         while (cur.depth()) {
152                 if (isLetter(cur)) {
153                         if (!inword) {
154                                 inword = true;
155                                 ignoreword = false;
156                                 cur.resetAnchor();
157                                 word.clear();
158                                 lang_code = cur.paragraph().getFontSettings(bp, cur.pos()).language()->code();
159                         }
160                         // Insets like optional hyphens and ligature
161                         // break are part of a word.
162                         if (!cur.paragraph().isInset(cur.pos())) {
163                                 Paragraph::value_type const c =
164                                         cur.paragraph().getChar(cur.pos());
165                                 word += c;
166                                 if (isDigit(c))
167                                         ignoreword = true;
168                         }
169                 } else { // !isLetter(cur)
170                         if (inword)
171                                 if (!word.empty() && !ignoreword) {
172                                         cur.setSelection();
173                                         return WordLangTuple(word, lang_code);
174                                 } else
175                                         inword = false;
176                 }
177
178                 cur.forwardPos();
179                 ++progress;
180         }
181
182         return WordLangTuple(string(), string());
183 }
184
185 } // namespace anon
186
187
188
189 void ControlSpellchecker::check()
190 {
191         lyxerr[Debug::GUI] << "Check the spelling of a word" << endl;
192
193         SpellBase::Result res = SpellBase::OK;
194
195         LCursor cur = kernel().bufferview()->cursor();
196         while (cur && cur.pos() && isLetter(cur)) {
197                 cur.backwardPos();
198         }
199
200         ptrdiff_t start = 0, total = 0;
201         DocIterator it = DocIterator(kernel().buffer().inset());
202         for (start = 0; it != cur; it.forwardPos())
203                 ++start;
204
205         for (total = start; it; it.forwardPos())
206                 ++total;
207
208         exitEarly_ = false;
209
210         while (res == SpellBase::OK || res == SpellBase::IGNORED_WORD) {
211                 word_ = nextWord(cur, start);
212
213                 // end of document
214                 if (getWord().empty()) {
215                         showSummary();
216                         exitEarly_ = true;
217                         return;
218                 }
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                         dialog().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 \"" << getWord() << "\"" << endl;
244
245         int const size = cur.selEnd().pos() - cur.selBegin().pos();
246         cur.pos() -= size;
247         kernel().bufferview()->putSelectionAt(cur, size, false);
248         // if we used a lfun like in find/replace, dispatch would do
249         // that for us
250         kernel().bufferview()->update();
251
252         // set suggestions
253         if (res != SpellBase::OK && res != SpellBase::IGNORED_WORD) {
254                 lyxerr[Debug::GUI] << "Found a word needing checking." << endl;
255                 dialog().view().partialUpdate(SPELL_FOUND_WORD);
256         }
257 }
258
259
260 bool ControlSpellchecker::checkAlive()
261 {
262         if (speller_->alive() && speller_->error().empty())
263                 return true;
264
265         string message;
266         if (speller_->error().empty())
267                 message = _("The spellchecker has died for some reason.\n"
268                             "Maybe it has been killed.");
269         else
270                 message = _("The spellchecker has failed.\n")
271                         + speller_->error();
272
273         dialog().CancelButton();
274
275         Alert::error(_("The spellchecker has failed"), message);
276         return false;
277 }
278
279
280 void ControlSpellchecker::showSummary()
281 {
282         if (!checkAlive() || count_ == 0) {
283                 dialog().CancelButton();
284                 return;
285         }
286
287         string message;
288         if (count_ != 1)
289                 message = bformat(_("%1$d words checked."), count_);
290         else
291                 message = _("One word checked.");
292
293         dialog().CancelButton();
294         Alert::information(_("Spelling check completed"), message);
295 }
296
297
298 void ControlSpellchecker::replace(string const & replacement)
299 {
300         lyxerr[Debug::GUI] << "ControlSpellchecker::replace("
301                            << replacement << ")" << std::endl;
302         BufferView & bufferview = *kernel().bufferview();
303         cap::replaceSelectionWithString(bufferview.cursor(), replacement, true);
304         kernel().buffer().markDirty();
305         bufferview.update();
306         // fix up the count
307         --count_;
308         check();
309 }
310
311
312 void ControlSpellchecker::replaceAll(string const & replacement)
313 {
314         // TODO: add to list
315         replace(replacement);
316 }
317
318
319 void ControlSpellchecker::insert()
320 {
321         speller_->insert(word_);
322         check();
323 }
324
325
326 string const ControlSpellchecker::getSuggestion() const
327 {
328         return speller_->nextMiss();
329 }
330
331
332 string const ControlSpellchecker::getWord() const
333 {
334         return word_.word();
335 }
336
337
338 void ControlSpellchecker::ignoreAll()
339 {
340         speller_->accept(word_);
341         check();
342 }
343
344 } // namespace frontend
345 } // namespace lyx