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