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