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