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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
|
/*
* weechat-aspell.c - aspell plugin for WeeChat: color for misspelled words
*
* Copyright (C) 2006 Emmanuel Bouthenot <kolter@openics.org>
* Copyright (C) 2006-2015 Sébastien Helleu <flashcode@flashtux.org>
* Copyright (C) 2012 Nils Görs <weechatter@arcor.de>
*
* 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/>.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <wctype.h>
#include "../weechat-plugin.h"
#include "weechat-aspell.h"
#include "weechat-aspell-bar-item.h"
#include "weechat-aspell-command.h"
#include "weechat-aspell-completion.h"
#include "weechat-aspell-config.h"
#include "weechat-aspell-info.h"
#include "weechat-aspell-speller.h"
WEECHAT_PLUGIN_NAME(ASPELL_PLUGIN_NAME);
WEECHAT_PLUGIN_DESCRIPTION(N_("Spell checker for input (with Aspell)"));
WEECHAT_PLUGIN_AUTHOR("Sébastien Helleu <flashcode@flashtux.org>");
WEECHAT_PLUGIN_VERSION(WEECHAT_VERSION);
WEECHAT_PLUGIN_LICENSE(WEECHAT_LICENSE);
WEECHAT_PLUGIN_PRIORITY(9000);
struct t_weechat_plugin *weechat_aspell_plugin = NULL;
int aspell_enabled = 0;
#ifdef USE_ENCHANT
EnchantBroker *broker = NULL;
#endif /* USE_ENCHANT */
/*
* aspell supported languages, updated on 2012-07-05
* URL: ftp://ftp.gnu.org/gnu/aspell/dict/0index.html
*/
struct t_aspell_code aspell_langs[] =
{
{ "af", "Afrikaans" },
{ "am", "Amharic" },
{ "ar", "Arabic" },
{ "ast", "Asturian" },
{ "az", "Azerbaijani" },
{ "be", "Belarusian" },
{ "bg", "Bulgarian" },
{ "bn", "Bengali" },
{ "br", "Breton" },
{ "ca", "Catalan" },
{ "cs", "Czech" },
{ "csb", "Kashubian" },
{ "cy", "Welsh" },
{ "da", "Danish" },
{ "de", "German" },
{ "de-alt", "German - Old Spelling" },
{ "el", "Greek" },
{ "en", "English" },
{ "eo", "Esperanto" },
{ "es", "Spanish" },
{ "et", "Estonian" },
{ "fa", "Persian" },
{ "fi", "Finnish" },
{ "fo", "Faroese" },
{ "fr", "French" },
{ "fy", "Frisian" },
{ "ga", "Irish" },
{ "gd", "Scottish Gaelic" },
{ "gl", "Galician" },
{ "grc", "Ancient Greek" },
{ "gu", "Gujarati" },
{ "gv", "Manx Gaelic" },
{ "he", "Hebrew" },
{ "hi", "Hindi" },
{ "hil", "Hiligaynon" },
{ "hr", "Croatian" },
{ "hsb", "Upper Sorbian" },
{ "hu", "Hungarian" },
{ "hus", "Huastec" },
{ "hy", "Armenian" },
{ "ia", "Interlingua" },
{ "id", "Indonesian" },
{ "is", "Icelandic" },
{ "it", "Italian" },
{ "kn", "Kannada" },
{ "ku", "Kurdi" },
{ "ky", "Kirghiz" },
{ "la", "Latin" },
{ "lt", "Lithuanian" },
{ "lv", "Latvian" },
{ "mg", "Malagasy" },
{ "mi", "Maori" },
{ "mk", "Macedonian" },
{ "ml", "Malayalam" },
{ "mn", "Mongolian" },
{ "mr", "Marathi" },
{ "ms", "Malay" },
{ "mt", "Maltese" },
{ "nb", "Norwegian Bokmal" },
{ "nds", "Low Saxon" },
{ "nl", "Dutch" },
{ "nn", "Norwegian Nynorsk" },
{ "ny", "Chichewa" },
{ "or", "Oriya" },
{ "pa", "Punjabi" },
{ "pl", "Polish" },
{ "pt_BR", "Brazilian Portuguese" },
{ "pt_PT", "Portuguese" },
{ "qu", "Quechua" },
{ "ro", "Romanian" },
{ "ru", "Russian" },
{ "rw", "Kinyarwanda" },
{ "sc", "Sardinian" },
{ "sk", "Slovak" },
{ "sl", "Slovenian" },
{ "sr", "Serbian" },
{ "sv", "Swedish" },
{ "sw", "Swahili" },
{ "ta", "Tamil" },
{ "te", "Telugu" },
{ "tet", "Tetum" },
{ "tk", "Turkmen" },
{ "tl", "Tagalog" },
{ "tn", "Setswana" },
{ "tr", "Turkish" },
{ "uk", "Ukrainian" },
{ "uz", "Uzbek" },
{ "vi", "Vietnamese" },
{ "wa", "Walloon" },
{ "yi", "Yiddish" },
{ "zu", "Zulu" },
{ NULL, NULL}
};
struct t_aspell_code aspell_countries[] =
{
{ "AT", "Austria" },
{ "BR", "Brazil" },
{ "CA", "Canada" },
{ "CH", "Switzerland" },
{ "DE", "Germany" },
{ "FR", "France" },
{ "GB", "Great Britain" },
{ "PT", "Portugal" },
{ "SK", "Slovakia" },
{ "US", "United States of America" },
{ NULL, NULL}
};
char *aspell_url_prefix[] =
{ "http:", "https:", "ftp:", "tftp:", "ftps:", "ssh:", "fish:", "dict:",
"ldap:", "file:", "telnet:", "gopher:", "irc:", "ircs:", "irc6:", "irc6s:",
"cvs:", "svn:", "svn+ssh:", "git:", NULL };
/*
* Builds full name of buffer.
*
* Note: result must be freed after use.
*/
char *
weechat_aspell_build_option_name (struct t_gui_buffer *buffer)
{
const char *plugin_name, *name;
char *option_name;
int length;
if (!buffer)
return NULL;
plugin_name = weechat_buffer_get_string (buffer, "plugin");
name = weechat_buffer_get_string (buffer, "name");
length = strlen (plugin_name) + 1 + strlen (name) + 1;
option_name = malloc (length);
if (!option_name)
return NULL;
snprintf (option_name, length, "%s.%s", plugin_name, name);
return option_name;
}
/*
* Gets dictionary list for a name of buffer.
*
* First tries with all arguments, then removes one by one to find dict (from
* specific to general dict).
*/
const char *
weechat_aspell_get_dict_with_buffer_name (const char *name)
{
char *option_name, *ptr_end;
struct t_config_option *ptr_option;
if (!name)
return NULL;
option_name = strdup (name);
if (option_name)
{
ptr_end = option_name + strlen (option_name);
while (ptr_end >= option_name)
{
ptr_option = weechat_aspell_config_get_dict (option_name);
if (ptr_option)
{
free (option_name);
return weechat_config_string (ptr_option);
}
ptr_end--;
while ((ptr_end >= option_name) && (ptr_end[0] != '.'))
{
ptr_end--;
}
if ((ptr_end >= option_name) && (ptr_end[0] == '.'))
ptr_end[0] = '\0';
}
ptr_option = weechat_aspell_config_get_dict (option_name);
free (option_name);
if (ptr_option)
return weechat_config_string (ptr_option);
}
/* nothing found => return default dictionary (if set) */
if (weechat_config_string (weechat_aspell_config_check_default_dict)
&& weechat_config_string (weechat_aspell_config_check_default_dict)[0])
{
return weechat_config_string (weechat_aspell_config_check_default_dict);
}
/* no default dictionary set */
return NULL;
}
/*
* Gets dictionary list for a buffer.
*
* First tries with all arguments, then removes one by one to find dict (from
* specific to general dict).
*/
const char *
weechat_aspell_get_dict (struct t_gui_buffer *buffer)
{
char *name;
const char *dict;
name = weechat_aspell_build_option_name (buffer);
if (!name)
return NULL;
dict = weechat_aspell_get_dict_with_buffer_name (name);
free (name);
return dict;
}
/*
* Checks if command is authorized for spell checking.
*
* Returns:
* 1: command authorized
* 0: command not authorized
*/
int
weechat_aspell_command_authorized (const char *command)
{
int length_command, i;
if (!command)
return 1;
length_command = strlen (command);
for (i = 0; i < weechat_aspell_count_commands_to_check; i++)
{
if ((weechat_aspell_length_commands_to_check[i] == length_command)
&& (weechat_strcasecmp (command,
weechat_aspell_commands_to_check[i]) == 0))
{
/* command is authorized */
return 1;
}
}
/* command is not authorized */
return 0;
}
/*
* Checks if a word is an URL.
*
* Returns:
* 1: word is an URL
* 0: word is not an URL
*/
int
weechat_aspell_string_is_url (const char *word)
{
int i;
for (i = 0; aspell_url_prefix[i]; i++)
{
if (weechat_strncasecmp (word, aspell_url_prefix[i],
strlen (aspell_url_prefix[i])) == 0)
return 1;
}
/* word is not an URL */
return 0;
}
/*
* Checks if a word is a nick of nicklist.
*
* Returns:
* 1: word is a nick of nicklist
* 0: word is not a nick of nicklist
*/
int
weechat_aspell_string_is_nick (struct t_gui_buffer *buffer, const char *word)
{
char *pos, *pos_nick_completer, *pos_space, saved_char;
const char *nick_completer, *buffer_type, *buffer_nick, *buffer_channel;
int rc, len_completer;
nick_completer = weechat_config_string (
weechat_config_get ("weechat.completion.nick_completer"));
len_completer = (nick_completer) ? strlen (nick_completer) : 0;
pos_nick_completer = (nick_completer) ?
strstr (word, nick_completer) : NULL;
pos_space = strchr (word, ' ');
pos = NULL;
if (pos_nick_completer && pos_space)
{
if ((pos_nick_completer < pos_space)
&& (pos_nick_completer + len_completer == pos_space))
{
pos = pos_nick_completer;
}
else
pos = pos_space;
}
else
{
pos = (pos_nick_completer && !pos_nick_completer[len_completer]) ?
pos_nick_completer : pos_space;
}
if (pos)
{
saved_char = pos[0];
pos[0] = '\0';
}
rc = (weechat_nicklist_search_nick (buffer, NULL, word)) ? 1 : 0;
if (!rc)
{
/* for "private" buffers, check if word is self or remote nick */
buffer_type = weechat_buffer_get_string (buffer, "localvar_type");
if (buffer_type && (strcmp (buffer_type, "private") == 0))
{
/* check self nick */
buffer_nick = weechat_buffer_get_string (buffer, "localvar_nick");
if (buffer_nick && (weechat_strcasecmp (buffer_nick, word) == 0))
{
rc = 1;
}
else
{
/* check remote nick */
buffer_channel = weechat_buffer_get_string (buffer,
"localvar_channel");
if (buffer_channel
&& (weechat_strcasecmp (buffer_channel, word) == 0))
{
rc = 1;
}
}
}
}
if (pos)
pos[0] = saved_char;
return rc;
}
/*
* Checks if a word is made of digits and punctuation.
*
* Returns:
* 1: word has only digits and punctuation
* 0: word has some other chars (not digits neither punctuation)
*/
int
weechat_aspell_string_is_simili_number (const char *word)
{
int code_point;
if (!word || !word[0])
return 0;
while (word && word[0])
{
code_point = weechat_utf8_char_int (word);
if (!iswpunct (code_point) && !iswdigit (code_point))
return 0;
word = weechat_utf8_next_char (word);
}
/* there are only digits or punctuation */
return 1;
}
/*
* Spell checks a word.
*
* Returns:
* 1: word is OK
* 0: word is misspelled
*/
int
weechat_aspell_check_word (struct t_aspell_speller_buffer *speller_buffer,
const char *word)
{
int i;
/* word too small? then do not check word */
if ((weechat_config_integer (weechat_aspell_config_check_word_min_length) > 0)
&& ((int)strlen (word) < weechat_config_integer (weechat_aspell_config_check_word_min_length)))
return 1;
/* word is a number? then do not check word */
if (weechat_aspell_string_is_simili_number (word))
return 1;
/* check word with all spellers (order is important) */
if (speller_buffer->spellers)
{
for (i = 0; speller_buffer->spellers[i]; i++)
{
#ifdef USE_ENCHANT
if (enchant_dict_check (speller_buffer->spellers[i], word, strlen (word)) == 0)
#else
if (aspell_speller_check (speller_buffer->spellers[i], word, -1) == 1)
#endif /* USE_ENCHANT */
return 1;
}
}
/* misspelled word! */
return 0;
}
/*
* Gets suggestions for a word.
*
* Returns a string with format: "suggest1,suggest2,suggest3".
*
* Note: result must be freed after use (if not NULL).
*/
char *
weechat_aspell_get_suggestions (struct t_aspell_speller_buffer *speller_buffer,
const char *word)
{
int i, size, max_suggestions, num_suggestions;
char *suggestions, *suggestions2;
const char *ptr_word;
#ifdef USE_ENCHANT
char **elements;
size_t num_elements;
#else
const AspellWordList *list;
AspellStringEnumeration *elements;
#endif /* USE_ENCHANT */
max_suggestions = weechat_config_integer (weechat_aspell_config_check_suggestions);
if (max_suggestions < 0)
return NULL;
size = 1;
suggestions = malloc (size);
if (!suggestions)
return NULL;
suggestions[0] = '\0';
if (speller_buffer->spellers)
{
for (i = 0; speller_buffer->spellers[i]; i++)
{
#ifdef USE_ENCHANT
elements = enchant_dict_suggest (speller_buffer->spellers[i], word,
-1, &num_elements);
if (elements)
{
if (num_elements > 0)
{
num_suggestions = 0;
while ((ptr_word = elements[num_suggestions]) != NULL)
{
size += strlen (ptr_word) + ((suggestions[0]) ? 1 : 0);
suggestions2 = realloc (suggestions, size);
if (!suggestions2)
{
free (suggestions);
enchant_dict_free_string_list (speller_buffer->spellers[i],
elements);
return NULL;
}
suggestions = suggestions2;
if (suggestions[0])
strcat (suggestions, (num_suggestions == 0) ? "/" : ",");
strcat (suggestions, ptr_word);
num_suggestions++;
if ((max_suggestions >= 0) && (num_suggestions == max_suggestions))
break;
}
}
enchant_dict_free_string_list (speller_buffer->spellers[i], elements);
}
#else
list = aspell_speller_suggest (speller_buffer->spellers[i], word, -1);
if (list)
{
elements = aspell_word_list_elements (list);
num_suggestions = 0;
while ((ptr_word = aspell_string_enumeration_next (elements)) != NULL)
{
size += strlen (ptr_word) + ((suggestions[0]) ? 1 : 0);
suggestions2 = realloc (suggestions, size);
if (!suggestions2)
{
free (suggestions);
delete_aspell_string_enumeration (elements);
return NULL;
}
suggestions = suggestions2;
if (suggestions[0])
strcat (suggestions, (num_suggestions == 0) ? "/" : ",");
strcat (suggestions, ptr_word);
num_suggestions++;
if ((max_suggestions >= 0) && (num_suggestions == max_suggestions))
break;
}
delete_aspell_string_enumeration (elements);
}
#endif /* USE_ENCHANT */
}
}
/* no suggestions found */
if (!suggestions[0])
{
free (suggestions);
return NULL;
}
return suggestions;
}
/*
* Updates input text by adding color for misspelled words.
*/
char *
weechat_aspell_modifier_cb (void *data, const char *modifier,
const char *modifier_data, const char *string)
{
long unsigned int value;
struct t_gui_buffer *buffer;
struct t_aspell_speller_buffer *ptr_speller_buffer;
char *result, *ptr_string, *ptr_string_orig, *pos_space;
char *ptr_end, *ptr_end_valid, save_end;
char *word_for_suggestions, *old_suggestions, *suggestions;
char *word_and_suggestions;
const char *color_normal, *color_error, *ptr_suggestions;
int code_point, char_size;
int length, index_result, length_word, word_ok;
int length_color_normal, length_color_error, rc;
int input_pos, current_pos, word_start_pos, word_end_pos, word_end_pos_valid;
/* make C compiler happy */
(void) data;
(void) modifier;
if (!aspell_enabled)
return NULL;
if (!string)
return NULL;
rc = sscanf (modifier_data, "%lx", &value);
if ((rc == EOF) || (rc == 0))
return NULL;
buffer = (struct t_gui_buffer *)value;
/* check text during search only if option is enabled */
if (weechat_buffer_get_integer (buffer, "text_search")
&& !weechat_config_boolean (weechat_aspell_config_check_during_search))
return NULL;
/* get structure with speller info for buffer */
ptr_speller_buffer = weechat_hashtable_get (weechat_aspell_speller_buffer,
buffer);
if (!ptr_speller_buffer)
{
ptr_speller_buffer = weechat_aspell_speller_buffer_new (buffer);
if (!ptr_speller_buffer)
return NULL;
}
if (!ptr_speller_buffer->spellers)
return NULL;
/*
* for performance: return last string built if input string is the
* same (and cursor position is the same, if suggestions are enabled)
*/
input_pos = weechat_buffer_get_integer (buffer, "input_pos");
if (ptr_speller_buffer->modifier_string
&& (strcmp (string, ptr_speller_buffer->modifier_string) == 0)
&& ((weechat_config_integer (weechat_aspell_config_check_suggestions) < 0)
|| (input_pos == ptr_speller_buffer->input_pos)))
{
return (ptr_speller_buffer->modifier_result) ?
strdup (ptr_speller_buffer->modifier_result) : NULL;
}
/* free last modifier string and result */
if (ptr_speller_buffer->modifier_string)
{
free (ptr_speller_buffer->modifier_string);
ptr_speller_buffer->modifier_string = NULL;
}
if (ptr_speller_buffer->modifier_result)
{
free (ptr_speller_buffer->modifier_result);
ptr_speller_buffer->modifier_result = NULL;
}
word_for_suggestions = NULL;
/* save last modifier string received */
ptr_speller_buffer->modifier_string = strdup (string);
ptr_speller_buffer->input_pos = input_pos;
color_normal = weechat_color ("bar_fg");
length_color_normal = strlen (color_normal);
color_error = weechat_color (weechat_config_string (weechat_aspell_config_color_misspelled));
length_color_error = strlen (color_error);
length = strlen (string);
result = malloc (length + (length * length_color_error) + 1);
if (result)
{
result[0] = '\0';
ptr_string = ptr_speller_buffer->modifier_string;
index_result = 0;
/* check if string is a command */
if (!weechat_string_input_for_buffer (ptr_string))
{
char_size = weechat_utf8_char_size (ptr_string);
ptr_string += char_size;
pos_space = ptr_string;
while (pos_space && pos_space[0] && (pos_space[0] != ' '))
{
pos_space = (char *)weechat_utf8_next_char (pos_space);
}
if (!pos_space || !pos_space[0])
{
free (result);
return NULL;
}
pos_space[0] = '\0';
/* exit if command is not authorized for spell checking */
if (!weechat_aspell_command_authorized (ptr_string))
{
free (result);
return NULL;
}
memcpy (result + index_result,
ptr_speller_buffer->modifier_string,
char_size);
index_result += char_size;
strcpy (result + index_result, ptr_string);
index_result += strlen (ptr_string);
pos_space[0] = ' ';
ptr_string = pos_space;
}
current_pos = 0;
while (ptr_string[0])
{
ptr_string_orig = NULL;
/* find start of word: it must start with an alphanumeric char */
code_point = weechat_utf8_char_int (ptr_string);
while ((!iswalnum (code_point)) || iswspace (code_point))
{
if (!ptr_string_orig && !iswspace (code_point))
ptr_string_orig = ptr_string;
char_size = weechat_utf8_char_size (ptr_string);
memcpy (result + index_result, ptr_string, char_size);
index_result += char_size;
ptr_string += char_size;
current_pos++;
if (!ptr_string[0])
break;
code_point = weechat_utf8_char_int (ptr_string);
}
if (!ptr_string[0])
break;
if (!ptr_string_orig)
ptr_string_orig = ptr_string;
word_start_pos = current_pos;
word_end_pos = current_pos;
word_end_pos_valid = current_pos;
/* find end of word: ' and - allowed in word, but not at the end */
ptr_end_valid = ptr_string;
ptr_end = (char *)weechat_utf8_next_char (ptr_string);
code_point = weechat_utf8_char_int (ptr_end);
while (iswalnum (code_point) || (code_point == '\'')
|| (code_point == '-'))
{
word_end_pos++;
if (iswalnum (code_point))
{
/* pointer to last alphanumeric char in the word */
ptr_end_valid = ptr_end;
word_end_pos_valid = word_end_pos;
}
ptr_end = (char *)weechat_utf8_next_char (ptr_end);
if (!ptr_end[0])
break;
code_point = weechat_utf8_char_int (ptr_end);
}
ptr_end = (char *)weechat_utf8_next_char (ptr_end_valid);
word_end_pos = word_end_pos_valid;
word_ok = 0;
if (weechat_aspell_string_is_url (ptr_string)
|| weechat_aspell_string_is_nick (buffer, ptr_string_orig))
{
/*
* word is an URL or a nick, then it is OK: search for next
* space (will be end of word)
*/
word_ok = 1;
if (ptr_end[0])
{
code_point = weechat_utf8_char_int (ptr_end);
while (!iswspace (code_point))
{
ptr_end = (char *)weechat_utf8_next_char (ptr_end);
if (!ptr_end[0])
break;
code_point = weechat_utf8_char_int (ptr_end);
}
}
}
save_end = ptr_end[0];
ptr_end[0] = '\0';
length_word = ptr_end - ptr_string;
if (!word_ok)
{
if ((save_end != '\0')
|| (weechat_config_integer (weechat_aspell_config_check_real_time)))
{
word_ok = weechat_aspell_check_word (ptr_speller_buffer,
ptr_string);
if (!word_ok && (input_pos >= word_start_pos))
{
/*
* if word is misspelled and that cursor is after
* the beginning of this word, save the word (we will
* look for suggestions after this loop)
*/
if (word_for_suggestions)
free (word_for_suggestions);
word_for_suggestions = strdup (ptr_string);
}
}
else
word_ok = 1;
}
/* add error color */
if (!word_ok)
{
strcpy (result + index_result, color_error);
index_result += length_color_error;
}
/* add word */
strcpy (result + index_result, ptr_string);
index_result += length_word;
/* add normal color (after misspelled word) */
if (!word_ok)
{
strcpy (result + index_result, color_normal);
index_result += length_color_normal;
}
if (save_end == '\0')
break;
ptr_end[0] = save_end;
ptr_string = ptr_end;
current_pos = word_end_pos + 1;
}
result[index_result] = '\0';
}
/* save old suggestions in buffer */
ptr_suggestions = weechat_buffer_get_string (buffer,
"localvar_aspell_suggest");
old_suggestions = (ptr_suggestions) ? strdup (ptr_suggestions) : NULL;
/* if there is a misspelled word, get suggestions and set them in buffer */
if (word_for_suggestions)
{
suggestions = weechat_aspell_get_suggestions (ptr_speller_buffer,
word_for_suggestions);
if (suggestions)
{
length = strlen (word_for_suggestions) + 1 /* ":" */
+ strlen (suggestions) + 1;
word_and_suggestions = malloc (length);
if (word_and_suggestions)
{
snprintf (word_and_suggestions, length, "%s:%s",
word_for_suggestions, suggestions);
weechat_buffer_set (buffer, "localvar_set_aspell_suggest",
word_and_suggestions);
free (word_and_suggestions);
}
else
{
weechat_buffer_set (buffer, "localvar_del_aspell_suggest", "");
}
free (suggestions);
}
else
{
weechat_buffer_set (buffer, "localvar_del_aspell_suggest", "");
}
free (word_for_suggestions);
}
else
{
weechat_buffer_set (buffer, "localvar_del_aspell_suggest", "");
}
/*
* if suggestions have changed, update the bar item
* and send signal "aspell_suggest"
*/
ptr_suggestions = weechat_buffer_get_string (buffer,
"localvar_aspell_suggest");
if ((old_suggestions && !ptr_suggestions)
|| (!old_suggestions && ptr_suggestions)
|| (old_suggestions && ptr_suggestions
&& (strcmp (old_suggestions, ptr_suggestions) != 0)))
{
weechat_bar_item_update ("aspell_suggest");
(void) weechat_hook_signal_send ("aspell_suggest",
WEECHAT_HOOK_SIGNAL_POINTER, buffer);
}
if (old_suggestions)
free (old_suggestions);
if (!result)
return NULL;
ptr_speller_buffer->modifier_result = strdup (result);
return result;
}
/*
* Refreshes bar items on signal "buffer_switch".
*/
int
weechat_aspell_buffer_switch_cb (void *data, const char *signal,
const char *type_data, void *signal_data)
{
/* make C compiler happy */
(void) data;
(void) signal;
(void) type_data;
(void) signal_data;
/* refresh bar items (for root bars) */
weechat_bar_item_update ("aspell_dict");
weechat_bar_item_update ("aspell_suggest");
return WEECHAT_RC_OK;
}
/*
* Refreshes bar items on signal "window_switch".
*/
int
weechat_aspell_window_switch_cb (void *data, const char *signal,
const char *type_data, void *signal_data)
{
/* make C compiler happy */
(void) data;
(void) signal;
(void) type_data;
(void) signal_data;
/* refresh bar items (for root bars) */
weechat_bar_item_update ("aspell_dict");
weechat_bar_item_update ("aspell_suggest");
return WEECHAT_RC_OK;
}
/*
* Removes struct for buffer in hashtable "weechat_aspell_speller_buffer" on
* signal "buffer_closed".
*/
int
weechat_aspell_buffer_closed_cb (void *data, const char *signal,
const char *type_data, void *signal_data)
{
/* make C compiler happy */
(void) data;
(void) signal;
(void) type_data;
weechat_hashtable_remove (weechat_aspell_speller_buffer, signal_data);
return WEECHAT_RC_OK;
}
/*
* Display infos about external libraries used.
*/
int
weechat_aspell_debug_libs_cb (void *data, const char *signal,
const char *type_data, void *signal_data)
{
/* make C compiler happy */
(void) data;
(void) signal;
(void) type_data;
(void) signal_data;
#ifdef USE_ENCHANT
#ifdef HAVE_ENCHANT_GET_VERSION
weechat_printf (NULL, " %s: enchant %s",
ASPELL_PLUGIN_NAME, enchant_get_version ());
#else
weechat_printf (NULL, " %s: enchant (?)", ASPELL_PLUGIN_NAME);
#endif /* HAVE_ENCHANT_GET_VERSION */
#else
#ifdef HAVE_ASPELL_VERSION_STRING
weechat_printf (NULL, " %s: aspell %s",
ASPELL_PLUGIN_NAME, aspell_version_string ());
#else
weechat_printf (NULL, " %s: aspell (?)", ASPELL_PLUGIN_NAME);
#endif /* HAVE_ASPELL_VERSION_STRING */
#endif /* USE_ENCHANT */
return WEECHAT_RC_OK;
}
/*
* Initializes aspell plugin.
*/
int
weechat_plugin_init (struct t_weechat_plugin *plugin, int argc, char *argv[])
{
/* make C compiler happy */
(void) argc;
(void) argv;
weechat_plugin = plugin;
#ifdef USE_ENCHANT
/* acquire enchant broker */
broker = enchant_broker_init ();
if (!broker)
return WEECHAT_RC_ERROR;
#endif /* USE_ENCHANT */
if (!weechat_aspell_speller_init ())
return WEECHAT_RC_ERROR;
if (!weechat_aspell_config_init ())
return WEECHAT_RC_ERROR;
weechat_aspell_config_read ();
weechat_aspell_command_init ();
weechat_aspell_completion_init ();
/*
* callback for spell checking input text
* we use a low priority here, so that other modifiers "input_text_display"
* (from other plugins) will be called before this one
*/
weechat_hook_modifier ("500|input_text_display",
&weechat_aspell_modifier_cb, NULL);
weechat_aspell_bar_item_init ();
weechat_aspell_info_init ();
weechat_hook_signal ("buffer_switch",
&weechat_aspell_buffer_switch_cb, NULL);
weechat_hook_signal ("window_switch",
&weechat_aspell_window_switch_cb, NULL);
weechat_hook_signal ("buffer_closed",
&weechat_aspell_buffer_closed_cb, NULL);
weechat_hook_signal ("debug_libs",
&weechat_aspell_debug_libs_cb, NULL);
return WEECHAT_RC_OK;
}
/*
* Ends aspell plugin.
*/
int
weechat_plugin_end (struct t_weechat_plugin *plugin)
{
/* make C compiler happy */
(void) plugin;
weechat_aspell_config_write ();
weechat_aspell_config_free ();
weechat_aspell_speller_end ();
#ifdef USE_ENCHANT
/* release enchant broker */
enchant_broker_free (broker);
#endif /* USE_ENCHANT */
return WEECHAT_RC_OK;
}
|