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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
|
/*
* weechat-aspell-speller.c - speller management for aspell plugin
*
* Copyright (C) 2006 Emmanuel Bouthenot <kolter@openics.org>
* Copyright (C) 2006-2018 Sébastien Helleu <flashcode@flashtux.org>
*
* This file is part of WeeChat, the extensible chat client.
*
* WeeChat is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* WeeChat is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with WeeChat. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <string.h>
#include "../weechat-plugin.h"
#include "weechat-aspell.h"
#include "weechat-aspell-speller.h"
#include "weechat-aspell-config.h"
/*
* spellers: one by dictionary (key is name of dictionary (eg: "fr"), value is
* pointer on AspellSpeller)
*/
struct t_hashtable *weechat_aspell_spellers = NULL;
/*
* spellers by buffer (key is buffer pointer, value is pointer on
* struct t_aspell_speller_buffer)
*/
struct t_hashtable *weechat_aspell_speller_buffer = NULL;
/*
* Checks if an aspell dictionary is supported (installed on system).
*
* Returns:
* 1: aspell dict is supported
* 0: aspell dict is NOT supported
*/
int
weechat_aspell_speller_dict_supported (const char *lang)
{
#ifdef USE_ENCHANT
return enchant_broker_dict_exists (broker, lang);
#else
struct AspellConfig *config;
AspellDictInfoList *list;
AspellDictInfoEnumeration *elements;
const AspellDictInfo *dict;
int rc;
rc = 0;
config = new_aspell_config ();
list = get_aspell_dict_info_list (config);
elements = aspell_dict_info_list_elements (list);
while ((dict = aspell_dict_info_enumeration_next (elements)) != NULL)
{
if (strcmp (dict->name, lang) == 0)
{
rc = 1;
break;
}
}
delete_aspell_dict_info_enumeration (elements);
delete_aspell_config (config);
return rc;
#endif /* USE_ENCHANT */
}
/*
* Checks if dictionaries are valid (called when user creates/changes
* dictionaries for a buffer).
*
* An error is displayed for each invalid dictionary found.
*/
void
weechat_aspell_speller_check_dictionaries (const char *dict_list)
{
char **argv;
int argc, i;
if (dict_list)
{
argv = weechat_string_split (dict_list, ",", 0, 0, &argc);
if (argv)
{
for (i = 0; i < argc; i++)
{
if (!weechat_aspell_speller_dict_supported (argv[i]))
{
weechat_printf (NULL,
_("%s: warning: dictionary \"%s\" is not "
"available on your system"),
ASPELL_PLUGIN_NAME, argv[i]);
}
}
weechat_string_free_split (argv);
}
}
}
/*
* Creates and adds a new speller instance in the hashtable.
*
* Returns pointer to new aspell speller, NULL if error.
*/
#ifdef USE_ENCHANT
EnchantDict *
#else
AspellSpeller *
#endif /* USE_ENCHANT */
weechat_aspell_speller_new (const char *lang)
{
#ifdef USE_ENCHANT
EnchantDict *new_speller;
#else
AspellConfig *config;
AspellCanHaveError *ret;
AspellSpeller *new_speller;
#endif /* USE_ENCHANT */
struct t_infolist *infolist;
if (!lang)
return NULL;
if (weechat_aspell_plugin->debug)
{
weechat_printf (NULL,
"%s: creating new speller for lang \"%s\"",
ASPELL_PLUGIN_NAME, lang);
}
#ifdef USE_ENCHANT
new_speller = enchant_broker_request_dict (broker, lang);
if (!new_speller)
{
weechat_printf (NULL,
_("%s%s: error: unable to create speller for lang \"%s\""),
weechat_prefix ("error"), ASPELL_PLUGIN_NAME,
lang);
return NULL;
}
#else
/* create a speller instance for the newly created cell */
config = new_aspell_config ();
aspell_config_replace (config, "lang", lang);
#endif /* USE_ENCHANT */
/* apply all options */
infolist = weechat_infolist_get ("option", NULL, "aspell.option.*");
if (infolist)
{
while (weechat_infolist_next (infolist))
{
#ifdef USE_ENCHANT
/* TODO: set option with enchant */
#else
aspell_config_replace (config,
weechat_infolist_string (infolist, "option_name"),
weechat_infolist_string (infolist, "value"));
#endif /* USE_ENCHANT */
}
weechat_infolist_free (infolist);
}
#ifndef USE_ENCHANT
ret = new_aspell_speller (config);
if (aspell_error (ret) != 0)
{
weechat_printf (NULL,
"%s%s: error: %s",
weechat_prefix ("error"), ASPELL_PLUGIN_NAME,
aspell_error_message (ret));
delete_aspell_config (config);
delete_aspell_can_have_error (ret);
return NULL;
}
new_speller = to_aspell_speller (ret);
#endif /* USE_ENCHANT */
weechat_hashtable_set (weechat_aspell_spellers, lang, new_speller);
#ifndef USE_ENCHANT
/* free configuration */
delete_aspell_config (config);
#endif /* USE_ENCHANT */
return new_speller;
}
void
weechat_aspell_speller_add_dicts_to_hash (struct t_hashtable *hashtable,
const char *dict)
{
char **dicts;
int num_dicts, i;
if (!dict || !dict[0])
return;
dicts = weechat_string_split (dict, ",", 0, 0, &num_dicts);
if (dicts)
{
for (i = 0; i < num_dicts; i++)
{
weechat_hashtable_set (hashtable, dicts[i], NULL);
}
weechat_string_free_split (dicts);
}
}
/*
* Removes a speller if it is NOT in hashtable "used_spellers".
*/
void
weechat_aspell_speller_remove_unused_cb (void *data,
struct t_hashtable *hashtable,
const void *key, const void *value)
{
struct t_hashtable *used_spellers;
/* make C compiler happy */
(void) value;
used_spellers = (struct t_hashtable *)data;
/* if speller is not in "used_spellers", remove it (not used any more) */
if (!weechat_hashtable_has_key (used_spellers, key))
weechat_hashtable_remove (hashtable, key);
}
/*
* Removes unused spellers from hashtable "weechat_aspell_spellers".
*/
void
weechat_aspell_speller_remove_unused ()
{
struct t_hashtable *used_spellers;
struct t_infolist *infolist;
if (weechat_aspell_plugin->debug)
{
weechat_printf (NULL,
"%s: removing unused spellers",
ASPELL_PLUGIN_NAME);
}
/* create a hashtable that will contain all used spellers */
used_spellers = weechat_hashtable_new (32,
WEECHAT_HASHTABLE_STRING,
WEECHAT_HASHTABLE_STRING,
NULL, NULL);
if (!used_spellers)
return;
/* collect used spellers and store them in hashtable "used_spellers" */
weechat_aspell_speller_add_dicts_to_hash (used_spellers,
weechat_config_string (weechat_aspell_config_check_default_dict));
infolist = weechat_infolist_get ("option", NULL, "aspell.dict.*");
if (infolist)
{
while (weechat_infolist_next (infolist))
{
weechat_aspell_speller_add_dicts_to_hash (used_spellers,
weechat_infolist_string (infolist, "value"));
}
weechat_infolist_free (infolist);
}
/*
* look at current spellers, and remove spellers that are not in hashtable
* "used_spellers"
*/
weechat_hashtable_map (weechat_aspell_spellers,
&weechat_aspell_speller_remove_unused_cb,
used_spellers);
weechat_hashtable_free (used_spellers);
}
/*
* Callback called when a key is removed in hashtable "weechat_aspell_spellers".
*/
void
weechat_aspell_speller_free_value_cb (struct t_hashtable *hashtable,
const void *key, void *value)
{
#ifdef USE_ENCHANT
EnchantDict *ptr_speller;
#else
AspellSpeller *ptr_speller;
#endif /* USE_ENCHANT */
/* make C compiler happy */
(void) hashtable;
if (weechat_aspell_plugin->debug)
{
weechat_printf (NULL,
"%s: removing speller for lang \"%s\"",
ASPELL_PLUGIN_NAME, (const char *)key);
}
/* free speller */
#ifdef USE_ENCHANT
ptr_speller = (EnchantDict *)value;
enchant_broker_free_dict (broker, ptr_speller);
#else
ptr_speller = (AspellSpeller *)value;
aspell_speller_save_all_word_lists (ptr_speller);
delete_aspell_speller (ptr_speller);
#endif /* USE_ENCHANT */
}
/*
* Creates a structure for buffer speller info in hashtable
* "weechat_aspell_buffer_spellers".
*/
struct t_aspell_speller_buffer *
weechat_aspell_speller_buffer_new (struct t_gui_buffer *buffer)
{
const char *buffer_dicts;
char **dicts;
int num_dicts, i;
struct t_aspell_speller_buffer *new_speller_buffer;
#ifdef USE_ENCHANT
EnchantDict *ptr_speller;
#else
AspellSpeller *ptr_speller;
#endif /* USE_ENCHANT */
if (!buffer)
return NULL;
weechat_hashtable_remove (weechat_aspell_speller_buffer, buffer);
new_speller_buffer = malloc (sizeof (*new_speller_buffer));
if (!new_speller_buffer)
return NULL;
new_speller_buffer->spellers = NULL;
new_speller_buffer->modifier_string = NULL;
new_speller_buffer->input_pos = -1;
new_speller_buffer->modifier_result = NULL;
buffer_dicts = weechat_aspell_get_dict (buffer);
if (buffer_dicts)
{
dicts = weechat_string_split (buffer_dicts, ",", 0, 0, &num_dicts);
if (dicts && (num_dicts > 0))
{
new_speller_buffer->spellers =
#ifdef USE_ENCHANT
malloc ((num_dicts + 1) * sizeof (EnchantDict *));
#else
malloc ((num_dicts + 1) * sizeof (AspellSpeller *));
#endif /* USE_ENCHANT */
if (new_speller_buffer->spellers)
{
for (i = 0; i < num_dicts; i++)
{
ptr_speller = weechat_hashtable_get (weechat_aspell_spellers,
dicts[i]);
if (!ptr_speller)
ptr_speller = weechat_aspell_speller_new (dicts[i]);
new_speller_buffer->spellers[i] = ptr_speller;
}
new_speller_buffer->spellers[num_dicts] = NULL;
}
}
if (dicts)
weechat_string_free_split (dicts);
}
weechat_hashtable_set (weechat_aspell_speller_buffer,
buffer,
new_speller_buffer);
weechat_bar_item_update ("aspell_dict");
return new_speller_buffer;
}
/*
* Callback called when a key is removed in hashtable
* "weechat_aspell_speller_buffer".
*/
void
weechat_aspell_speller_buffer_free_value_cb (struct t_hashtable *hashtable,
const void *key, void *value)
{
struct t_aspell_speller_buffer *ptr_speller_buffer;
/* make C compiler happy */
(void) hashtable;
(void) key;
ptr_speller_buffer = (struct t_aspell_speller_buffer *)value;
if (ptr_speller_buffer->spellers)
free (ptr_speller_buffer->spellers);
if (ptr_speller_buffer->modifier_string)
free (ptr_speller_buffer->modifier_string);
if (ptr_speller_buffer->modifier_result)
free (ptr_speller_buffer->modifier_result);
free (ptr_speller_buffer);
}
/*
* Initializes spellers (creates hashtables).
*
* Returns:
* 1: OK (hashtables created)
* 0: error (not enough memory)
*/
int
weechat_aspell_speller_init ()
{
weechat_aspell_spellers = weechat_hashtable_new (32,
WEECHAT_HASHTABLE_STRING,
WEECHAT_HASHTABLE_POINTER,
NULL, NULL);
if (!weechat_aspell_spellers)
return 0;
weechat_hashtable_set_pointer (weechat_aspell_spellers,
"callback_free_value",
&weechat_aspell_speller_free_value_cb);
weechat_aspell_speller_buffer = weechat_hashtable_new (32,
WEECHAT_HASHTABLE_POINTER,
WEECHAT_HASHTABLE_POINTER,
NULL, NULL);
if (!weechat_aspell_speller_buffer)
{
weechat_hashtable_free (weechat_aspell_spellers);
return 0;
}
weechat_hashtable_set_pointer (weechat_aspell_speller_buffer,
"callback_free_value",
&weechat_aspell_speller_buffer_free_value_cb);
return 1;
}
/*
* Ends spellers (removes hashtables).
*/
void
weechat_aspell_speller_end ()
{
weechat_hashtable_free (weechat_aspell_spellers);
weechat_hashtable_free (weechat_aspell_speller_buffer);
}
|