]> git.lyx.org Git - lyx.git/blob - src/frontends/controllers/ControlSpellchecker.C
obvious stuff
[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 #include "ispell.h"
27 #ifdef USE_PSPELL
28 # include "pspell.h"
29 #else
30 #ifdef USE_ASPELL
31 # include "aspell_local.h"
32 #endif
33 #endif
34
35 #include "support/tostr.h"
36
37 #include "frontends/Alert.h"
38
39 using std::advance;
40 using std::distance;
41 using std::endl;
42 using std::string;
43
44 namespace lyx {
45
46 using support::bformat;
47
48 namespace frontend {
49
50
51 ControlSpellchecker::ControlSpellchecker(Dialog & parent)
52         : Dialog::Controller(parent),
53           oldval_(0), newvalue_(0), count_(0)
54 {}
55
56
57 ControlSpellchecker::~ControlSpellchecker()
58 {}
59
60
61 namespace {
62
63 SpellBase * getSpeller(BufferParams const & bp)
64 {
65         string lang = (lyxrc.isp_use_alt_lang)
66                       ? lyxrc.isp_alt_lang
67                       : bp.language->code();
68
69 #ifdef USE_ASPELL
70         if (lyxrc.use_spell_lib)
71                 return new ASpell(bp, lang);
72 #endif
73 #ifdef USE_PSPELL
74         if (lyxrc.use_spell_lib)
75                 return new PSpell(bp, lang);
76 #endif
77
78         lang = (lyxrc.isp_use_alt_lang) ?
79                 lyxrc.isp_alt_lang : bp.language->lang();
80
81         return new ISpell(bp, lang);
82 }
83
84 } // namespace anon
85
86
87 bool ControlSpellchecker::initialiseParams(std::string const &)
88 {
89         lyxerr[Debug::GUI] << "Spellchecker::initialiseParams" << endl;
90
91         speller_.reset(getSpeller(kernel().buffer().params()));
92
93         // reset values to initial
94         oldval_ = 0;
95         newvalue_ = 0;
96         count_ = 0;
97
98         bool const success = speller_->error().empty();
99
100         if (!success) {
101                 Alert::error(_("The spell-checker could not be started"),
102                              speller_->error());
103                 speller_.reset(0);
104         }
105
106         return success;
107 }
108
109
110 void ControlSpellchecker::clearParams()
111 {
112         lyxerr[Debug::GUI] << "Spellchecker::clearParams" << endl;
113         speller_.reset(0);
114 }
115
116
117 namespace {
118
119 bool isLetter(DocIterator const & cur)
120 {
121         return cur.inTexted()
122                 && cur.inset().allowSpellCheck()
123                 && cur.pos() != cur.lastpos()
124                 && cur.paragraph().isLetter(cur.pos())
125                 && !isDeletedText(cur.paragraph(), cur.pos());
126 }
127
128
129 WordLangTuple nextWord(DocIterator & cur, ptrdiff_t & progress,
130         BufferParams & bp)
131 {
132         // skip until we have real text (will jump paragraphs)
133         for (; cur.size() && !isLetter(cur); cur.forwardPos());
134                 ++progress;
135
136         // hit end
137         if (cur.empty())
138                 return WordLangTuple(string(), string());
139
140         string lang_code = cur.paragraph().
141                 getFontSettings(bp, cur.pos()).language()->code();
142         string str;
143         // and find the end of the word (insets like optional hyphens
144         // and ligature break are part of a word)
145         for (; cur && isLetter(cur); cur.forwardPos(), ++progress) {
146                 if (!cur.paragraph().isInset(cur.pos()))
147                         str += cur.paragraph().getChar(cur.pos());
148         }
149
150         return WordLangTuple(str, lang_code);
151 }
152
153 } // namespace anon
154
155
156
157 void ControlSpellchecker::check()
158 {
159         lyxerr[Debug::GUI] << "spell check a word" << endl;
160
161         SpellBase::Result res = SpellBase::OK;
162
163         DocIterator cur = kernel().bufferview()->cursor();
164
165         ptrdiff_t start = 0, total = 0;
166         DocIterator it = DocIterator(kernel().buffer().inset());
167         for (start = 0; it != cur; it.forwardPos())
168                 ++start;
169
170         for (total = start; it; it.forwardPos())
171                 ++total;
172
173         for (; cur && isLetter(cur); cur.forwardPos())
174                 ++start;
175
176         BufferParams & bufferparams = kernel().buffer().params();
177
178         while (res == SpellBase::OK || res == SpellBase::IGNORE) {
179                 word_ = nextWord(cur, start, bufferparams);
180
181                 // end of document
182                 if (getWord().empty())
183                         break;
184
185                 ++count_;
186
187                 // Update slider if and only if value has changed
188                 float progress = total ? float(start)/total : 1;
189                 newvalue_ = int(100.0 * progress);
190                 if (newvalue_!= oldval_) {
191                         lyxerr[Debug::GUI] << "Updating spell progress." << endl;
192                         oldval_ = newvalue_;
193                         // set progress bar
194                         dialog().view().partialUpdate(SPELL_PROGRESSED);
195                 }
196
197                 // speller might be dead ...
198                 if (!checkAlive())
199                         return;
200
201                 res = speller_->check(word_);
202
203                 // ... or it might just be reporting an error
204                 if (!checkAlive())
205                         return;
206         }
207
208         lyxerr[Debug::GUI] << "Found word \"" << getWord() << "\"" << endl;
209
210         if (getWord().empty()) {
211                 showSummary();
212                 return;
213         }
214
215         int const size = getWord().size();
216         kernel().bufferview()->putSelectionAt(cur, size, true);
217
218         // set suggestions
219         if (res != SpellBase::OK && res != SpellBase::IGNORE) {
220                 lyxerr[Debug::GUI] << "Found a word needing checking." << endl;
221                 dialog().view().partialUpdate(SPELL_FOUND_WORD);
222         }
223 }
224
225
226 bool ControlSpellchecker::checkAlive()
227 {
228         if (speller_->alive() && speller_->error().empty())
229                 return true;
230
231         string message = speller_->error();
232         if (message.empty())
233                 message = _("The spell-checker has died for some reason.\n"
234                          "Maybe it has been killed.");
235
236         dialog().CancelButton();
237
238         Alert::error(_("The spell-checker has failed"), message);
239         return false;
240 }
241
242
243 void ControlSpellchecker::showSummary()
244 {
245         if (!checkAlive() || count_ == 0) {
246                 dialog().CancelButton();
247                 return;
248         }
249
250         string message;
251         if (count_ != 1)
252                 message = bformat(_("%1$s words checked."), tostr(count_));
253         else
254                 message = _("One word checked.");
255
256         dialog().CancelButton();
257         Alert::information(_("Spell-checking is complete"), message);
258 }
259
260
261 void ControlSpellchecker::replace(string const & replacement)
262 {
263         lyxerr << "ControlSpellchecker::replace("
264                << replacement << ")" << std::endl;
265         BufferView & bufferview = *kernel().bufferview();
266         cap::replaceWord(bufferview.cursor(), replacement);
267         kernel().buffer().markDirty();
268         bufferview.update();
269         // fix up the count
270         --count_;
271         check();
272 }
273
274
275 void ControlSpellchecker::replaceAll(string const & replacement)
276 {
277         // TODO: add to list
278         replace(replacement);
279 }
280
281
282 void ControlSpellchecker::insert()
283 {
284         speller_->insert(word_);
285         check();
286 }
287
288
289 string const ControlSpellchecker::getSuggestion() const
290 {
291         return speller_->nextMiss();
292 }
293
294
295 string const ControlSpellchecker::getWord() const
296 {
297         return word_.word();
298 }
299
300
301 void ControlSpellchecker::ignoreAll()
302 {
303         speller_->accept(word_);
304         check();
305 }
306
307 } // namespace frontend
308 } // namespace lyx