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