-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib-aspell.c
350 lines (304 loc) · 8.19 KB
/
lib-aspell.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/*
* Copyright Neil Brown ©2020-2023 <[email protected]>
* May be distributed under terms of GPLv2 - see file:COPYING
*
* aspell: edlib interface for aspell library.
*
*/
#include <aspell.h>
#include <wctype.h>
#define PANE_DATA_TYPE struct aspell_data
#include "core.h"
struct aspell_data {
AspellSpeller *speller safe;
bool need_save;
};
#include "core-pane.h"
static AspellConfig *spell_config;
static int trim(const char *safe *wordp safe)
{
const char *wp = *wordp;
const char *start;
int len;
wint_t ch;
start = wp;
while (*wp && (ch = get_utf8(&wp, NULL)) < WERR &&
!iswalpha(ch))
start = wp;
if (!*start)
return 0;
len = 1;
/* start is the first alphabetic, wp points beyond it */
while (*wp && (ch = get_utf8(&wp, NULL)) < WERR) {
if (iswalpha(ch))
len = wp - start;
}
*wordp = start;
return len;
}
static struct map *aspell_map safe;
DEF_LOOKUP_CMD(aspell_handle, aspell_map);
DEF_CMD(aspell_attach_helper)
{
struct aspell_data *as;
AspellCanHaveError *ret;
struct pane *p;
ret = new_aspell_speller(spell_config);
if (aspell_error_number(ret)) {
LOG("Cannot create speller: %s", aspell_error_message(ret));
return Efail;
}
p = pane_register(ci->focus, 0, &aspell_handle.c);
if (!p)
return Efail;
as = p->data;
as->speller = safe_cast to_aspell_speller(ret);
call("doc:request:aspell:check", p);
call("doc:request:aspell:suggest", p);
call("doc:request:aspell:set-dict", p);
call("doc:request:aspell:add-word", p);
call("doc:request:aspell:save", p);
return 1;
}
DEF_CMD_CLOSED(aspell_close)
{
struct aspell_data *as = ci->home->data;
if (as->need_save)
aspell_speller_save_all_word_lists(as->speller);
delete_aspell_speller(as->speller);
return 1;
}
DEF_CMD(aspell_check)
{
struct aspell_data *as = ci->home->data;
int correct;
const char *word = ci->str;
int len;
if (!word)
return Enoarg;
len = trim(&word);
if (!len)
return Efail;
correct = aspell_speller_check(as->speller, word, len);
return correct ? 1 : Efalse;
}
DEF_CMD(spell_check)
{
int rv = call("doc:notify:aspell:check", ci->focus, 0, NULL, ci->str);
if (rv != Efallthrough)
return rv;
call_comm("doc:get-doc", ci->focus, &aspell_attach_helper);
return call("doc:notify:aspell:check", ci->focus, 0, NULL, ci->str);
}
DEF_CMD(aspell_suggest)
{
struct aspell_data *as = ci->home->data;
const AspellWordList *l;
AspellStringEnumeration *el;
const char *w;
const char *word = ci->str;
int len;
if (!word)
return Enoarg;
len = trim(&word);
if (!len)
return Efail;
l = aspell_speller_suggest(as->speller, word, len);
el = aspell_word_list_elements(l);
while ((w = aspell_string_enumeration_next(el)) != NULL)
comm_call(ci->comm2, "suggest", ci->focus, 0, NULL, w);
delete_aspell_string_enumeration(el);
return 1;
}
DEF_CMD(spell_suggest)
{
int rv = call_comm("doc:notify:aspell:suggest", ci->focus, ci->comm2,
0, NULL, ci->str);
if (rv != Efallthrough)
return rv;
call_comm("doc:get-doc", ci->focus, &aspell_attach_helper);
return call_comm("doc:notify:aspell:suggest", ci->focus, ci->comm2,
0, NULL, ci->str);
}
DEF_CMD(aspell_save)
{
struct aspell_data *as = ci->home->data;
if (as->need_save) {
as->need_save = False;
aspell_speller_save_all_word_lists(as->speller);
}
return Efalse;
}
DEF_CMD(aspell_do_save)
{
aspell_save_func(ci);
return 1;
}
DEF_CMD(spell_save)
{
return call_comm("doc:notify:aspell:save", ci->focus, ci->comm2,
ci->num, NULL, ci->str);
}
DEF_CMD(aspell_add)
{
struct aspell_data *as = ci->home->data;
const char *word = ci->str;
int len;
if (!word)
return Enoarg;
len = trim(&word);
if (!len)
return Efail;
if (ci->num == 1) {
aspell_speller_add_to_personal(as->speller, word, len);
if (as->need_save)
call_comm("event:free", ci->home, &aspell_save);
as->need_save = True;
call_comm("event:timer", ci->home, &aspell_save, 30*1000);
} else
aspell_speller_add_to_session(as->speller, word, len);
call("doc:notify:spell:dict-changed", ci->home);
return 1;
}
DEF_CMD(spell_add)
{
int rv = call_comm("doc:notify:aspell:add-word", ci->focus, ci->comm2,
ci->num, NULL, ci->str);
if (rv != Efallthrough)
return rv;
call_comm("doc:get-doc", ci->focus, &aspell_attach_helper);
return call_comm("doc:notify:aspell:add-word", ci->focus, ci->comm2,
ci->num, NULL, ci->str);
}
DEF_CMD(aspell_set_dict)
{
struct aspell_data *as = ci->home->data;
const char *lang = ci->str;
AspellConfig *conf2;
AspellCanHaveError *ret;
if (!lang)
return Enoarg;
conf2 = aspell_config_clone(spell_config);
aspell_config_replace(conf2, "lang", lang);
ret = new_aspell_speller(conf2);
if (!aspell_error_number(ret)) {
delete_aspell_speller(as->speller);
as->speller = safe_cast to_aspell_speller(ret);
call("doc:notify:spell:dict-changed", ci->focus);
}
delete_aspell_config(conf2);
return 1;
}
DEF_CMD(spell_dict)
{
int ret;
ret = call("doc:notify:aspell:set-dict", ci->focus, 0, NULL,
ksuffix(ci, "interactive-cmd-dict-"));
if (ret != Efallthrough)
return ret;
call_comm("doc:get-doc", ci->focus, &aspell_attach_helper);
return call("doc:notify:aspell:set-dict", ci->focus, 0, NULL,
ksuffix(ci, "interactive-cmd-dict-"));
}
static inline bool is_word_body(wint_t ch)
{
/* alphabetics, appostrophies */
return iswalpha(ch) || ch == '\'';
}
static inline bool is_word_initial(wint_t ch)
{
/* Word must start with an alphabetic */
return iswalpha(ch);
}
static inline bool is_word_final(wint_t ch)
{
/* Word must end with an alphabetic */
return iswalpha(ch);
}
DEF_CMD(spell_this)
{
/* Find a word "here" to spell. It must include the first
* permitted character after '->mark'.
* '->mark' is moved to the end and if ->mark2 is available, it
* is moved to the start.
* If ->comm2 is available, the word is returned as a string.
* This command should ignore any view-specific or doc-specific rules
* about where words are allowed, but should honour any rules about
* what characters can constitute a word.
*/
wint_t ch;
struct mark *m2;
if (!ci->mark)
return Enoarg;
while ((ch = doc_next(ci->focus, ci->mark)) != WEOF &&
!is_word_initial(ch))
;
if (ch == WEOF)
return Efalse;
if (ci->mark2) {
m2 = ci->mark2;
mark_to_mark(m2, ci->mark);
} else
m2 = mark_dup(ci->mark);
while ((ch = doc_following(ci->focus, ci->mark)) != WEOF &&
is_word_body(ch))
doc_next(ci->focus, ci->mark);
while ((ch = doc_prior(ci->focus, ci->mark)) != WEOF &&
!is_word_final(ch))
doc_prev(ci->focus, ci->mark);
while ((ch = doc_prior(ci->focus, m2)) != WEOF &&
is_word_body(ch))
doc_prev(ci->focus, m2);
while ((ch = doc_following(ci->focus, m2)) != WEOF &&
!is_word_initial(ch))
doc_next(ci->focus, m2);
if (ci->comm2)
call_comm("doc:get-str", ci->focus, ci->comm2,
0, m2, NULL, 0, ci->mark);
if (m2 != ci->mark2)
mark_free(m2);
return 1;
}
DEF_CMD(spell_next)
{
/* Find the next word-start after ->mark.
* A view or doc might over-ride this to skip over
* content that shouldn't be spell-checked.
*/
wint_t ch;
if (!ci->mark)
return Enoarg;
while ((ch = doc_next(ci->focus, ci->mark)) != WEOF &&
!is_word_initial(ch))
;
if (ch == WEOF)
return Efalse;
doc_prev(ci->focus, ci->mark);
return 1;
}
void edlib_init(struct pane *ed safe)
{
spell_config = new_aspell_config();
aspell_config_replace(spell_config, "lang", "en_AU");
call_comm("global-set-command", ed, &spell_check,
0, NULL, "Spell:Check");
call_comm("global-set-command", ed, &spell_suggest,
0, NULL, "Spell:Suggest");
call_comm("global-set-command", ed, &spell_this,
0, NULL, "Spell:ThisWord");
call_comm("global-set-command", ed, &spell_next,
0, NULL, "Spell:NextWord");
call_comm("global-set-command", ed, &spell_add,
0, NULL, "Spell:AddWord");
call_comm("global-set-command", ed, &spell_save,
0, NULL, "Spell:Save");
call_comm("global-set-command-prefix", ed, &spell_dict,
0, NULL, "interactive-cmd-dict-");
aspell_map = key_alloc();
key_add(aspell_map, "Close", &aspell_close);
key_add(aspell_map, "aspell:check", &aspell_check);
key_add(aspell_map, "aspell:suggest", &aspell_suggest);
key_add(aspell_map, "aspell:set-dict", &aspell_set_dict);
key_add(aspell_map, "aspell:add-word", &aspell_add);
key_add(aspell_map, "aspell:save", &aspell_do_save);
}