Mercurial > ~mikael > mcabber > hg
comparison mcabber/src/screen.c @ 1590:8d1bcc83ae32
Add support for spellchecking with Enchant (caolan)
Patch from the issue tracker:
http://bitbucket.org/McKael/mcabber-crew/issue/36/patch-to-spell-checking-using-enchant
author | Mikael Berthe <mikael@lilotux.net> |
---|---|
date | Sun, 04 Oct 2009 19:33:38 +0200 |
parents | a2dd83167bc9 |
children | a087125d8fc8 |
comparison
equal
deleted
inserted
replaced
1589:23c08d4f1d1e | 1590:8d1bcc83ae32 |
---|---|
39 # include <localcharset.h> | 39 # include <localcharset.h> |
40 #else | 40 #else |
41 # include <langinfo.h> | 41 # include <langinfo.h> |
42 #endif | 42 #endif |
43 | 43 |
44 #ifdef HAVE_ASPELL_H | 44 #ifdef WITH_ENCHANT |
45 # include <enchant.h> | |
46 #endif | |
47 | |
48 #ifdef WITH_ASPELL | |
45 # include <aspell.h> | 49 # include <aspell.h> |
46 #endif | 50 #endif |
47 | 51 |
48 #include "screen.h" | 52 #include "screen.h" |
49 #include "utf8.h" | 53 #include "utf8.h" |
71 static void scr_cancel_current_completion(void); | 75 static void scr_cancel_current_completion(void); |
72 static void scr_end_current_completion(void); | 76 static void scr_end_current_completion(void); |
73 static void scr_insert_text(const char*); | 77 static void scr_insert_text(const char*); |
74 static void scr_handle_tab(void); | 78 static void scr_handle_tab(void); |
75 | 79 |
76 #ifdef HAVE_ASPELL_H | 80 #if defined(WITH_ENCHANT) || defined(WITH_ASPELL) |
77 static void spellcheck(char *, char *); | 81 static void spellcheck(char *, char *); |
78 #endif | 82 #endif |
79 | 83 |
80 static GHashTable *winbufhash; | 84 static GHashTable *winbufhash; |
81 | 85 |
118 static bool log_win_on_top; | 122 static bool log_win_on_top; |
119 static bool roster_win_on_right; | 123 static bool roster_win_on_right; |
120 static time_t LastActivity; | 124 static time_t LastActivity; |
121 | 125 |
122 static char inputLine[INPUTLINE_LENGTH+1]; | 126 static char inputLine[INPUTLINE_LENGTH+1]; |
123 #ifdef HAVE_ASPELL_H | 127 #if defined(WITH_ENCHANT) || defined(WITH_ASPELL) |
124 static char maskLine[INPUTLINE_LENGTH+1]; | 128 static char maskLine[INPUTLINE_LENGTH+1]; |
125 #endif | 129 #endif |
126 static char *ptr_inputline; | 130 static char *ptr_inputline; |
127 static short int inputline_offset; | 131 static short int inputline_offset; |
128 static int completion_started; | 132 static int completion_started; |
159 time_t timestamp, guint prefix_flags, | 163 time_t timestamp, guint prefix_flags, |
160 unsigned mucnicklen); | 164 unsigned mucnicklen); |
161 inline void scr_UpdateBuddyWindow(void); | 165 inline void scr_UpdateBuddyWindow(void); |
162 inline void scr_set_chatmode(int enable); | 166 inline void scr_set_chatmode(int enable); |
163 | 167 |
164 #ifdef HAVE_ASPELL_H | 168 #define SPELLBADCHAR 5 |
165 #define ASPELLBADCHAR 5 | 169 |
170 #ifdef WITH_ENCHANT | |
171 EnchantBroker *spell_broker; | |
172 EnchantDict *spell_checker; | |
173 #endif | |
174 | |
175 #ifdef WITH_ASPELL | |
166 AspellConfig *spell_config; | 176 AspellConfig *spell_config; |
167 AspellSpeller *spell_checker; | 177 AspellSpeller *spell_checker; |
168 #endif | 178 #endif |
169 | 179 |
170 typedef struct { | 180 typedef struct { |
3578 } | 3588 } |
3579 } | 3589 } |
3580 inputline_offset = c - inputLine; | 3590 inputline_offset = c - inputLine; |
3581 } | 3591 } |
3582 | 3592 |
3583 #ifdef HAVE_ASPELL_H | 3593 #if defined(WITH_ENCHANT) || defined(WITH_ASPELL) |
3584 // prints inputLine with underlined words when misspelled | 3594 // prints inputLine with underlined words when misspelled |
3585 static inline void print_checked_line(void) | 3595 static inline void print_checked_line(void) |
3586 { | 3596 { |
3587 char *wprint_char_fmt = "%c"; | 3597 char *wprint_char_fmt = "%c"; |
3588 int point; | 3598 int point; |
3608 } | 3618 } |
3609 #endif | 3619 #endif |
3610 | 3620 |
3611 static inline void refresh_inputline(void) | 3621 static inline void refresh_inputline(void) |
3612 { | 3622 { |
3613 #ifdef HAVE_ASPELL_H | 3623 #if defined(WITH_ENCHANT) || defined(WITH_ASPELL) |
3614 if (settings_opt_get_int("aspell_enable")) { | 3624 if (settings_opt_get_int("spell_enable")) { |
3615 memset(maskLine, 0, INPUTLINE_LENGTH+1); | 3625 memset(maskLine, 0, INPUTLINE_LENGTH+1); |
3616 spellcheck(inputLine, maskLine); | 3626 spellcheck(inputLine, maskLine); |
3617 } | 3627 } |
3618 print_checked_line(); | 3628 print_checked_line(); |
3619 wclrtoeol(inputWnd); | 3629 wclrtoeol(inputWnd); |
3975 time(&chatstate_timestamp); | 3985 time(&chatstate_timestamp); |
3976 } | 3986 } |
3977 return; | 3987 return; |
3978 } | 3988 } |
3979 | 3989 |
3980 #ifdef HAVE_ASPELL_H | 3990 #if defined(WITH_ENCHANT) || defined(WITH_ASPELL) |
3981 // Aspell initialization | 3991 // initialization |
3982 void spellcheck_init(void) | 3992 void spellcheck_init(void) |
3983 { | 3993 { |
3984 int aspell_enable = settings_opt_get_int("aspell_enable"); | 3994 int spell_enable = settings_opt_get_int("spell_enable"); |
3985 const char *aspell_lang = settings_opt_get("aspell_lang"); | 3995 const char *spell_lang = settings_opt_get("spell_lang"); |
3986 const char *aspell_encoding = settings_opt_get("aspell_encoding"); | 3996 #ifdef WITH_ASPELL |
3997 const char *spell_encoding = settings_opt_get("spell_encoding"); | |
3987 AspellCanHaveError *possible_err; | 3998 AspellCanHaveError *possible_err; |
3988 | 3999 #endif |
3989 if (!aspell_enable) | 4000 |
4001 if (!spell_enable) | |
3990 return; | 4002 return; |
3991 | 4003 |
4004 #ifdef WITH_ENCHANT | |
4005 if (spell_checker) { | |
4006 enchant_broker_free_dict(spell_broker, spell_checker); | |
4007 enchant_broker_free(spell_broker); | |
4008 spell_checker = NULL; | |
4009 spell_broker = NULL; | |
4010 } | |
4011 | |
4012 spell_broker = enchant_broker_init(); | |
4013 spell_checker = enchant_broker_request_dict(spell_broker, spell_lang); | |
4014 #endif | |
4015 #ifdef WITH_ASPELL | |
3992 if (spell_checker) { | 4016 if (spell_checker) { |
3993 delete_aspell_speller(spell_checker); | 4017 delete_aspell_speller(spell_checker); |
3994 delete_aspell_config(spell_config); | 4018 delete_aspell_config(spell_config); |
3995 spell_checker = NULL; | 4019 spell_checker = NULL; |
3996 spell_config = NULL; | 4020 spell_config = NULL; |
3997 } | 4021 } |
3998 | 4022 |
3999 spell_config = new_aspell_config(); | 4023 spell_config = new_aspell_config(); |
4000 aspell_config_replace(spell_config, "encoding", aspell_encoding); | 4024 aspell_config_replace(spell_config, "encoding", spell_encoding); |
4001 aspell_config_replace(spell_config, "lang", aspell_lang); | 4025 aspell_config_replace(spell_config, "lang", spell_lang); |
4002 possible_err = new_aspell_speller(spell_config); | 4026 possible_err = new_aspell_speller(spell_config); |
4003 | 4027 |
4004 if (aspell_error_number(possible_err) != 0) { | 4028 if (aspell_error_number(possible_err) != 0) { |
4005 spell_checker = NULL; | 4029 spell_checker = NULL; |
4006 delete_aspell_config(spell_config); | 4030 delete_aspell_config(spell_config); |
4007 spell_config = NULL; | 4031 spell_config = NULL; |
4008 } else { | 4032 } else { |
4009 spell_checker = to_aspell_speller(possible_err); | 4033 spell_checker = to_aspell_speller(possible_err); |
4010 } | 4034 } |
4011 } | 4035 #endif |
4012 | 4036 } |
4013 // Deinitialization of Aspell spellchecker | 4037 |
4038 // Deinitialization of spellchecker | |
4014 void spellcheck_deinit(void) | 4039 void spellcheck_deinit(void) |
4015 { | 4040 { |
4016 if (spell_checker) { | 4041 if (spell_checker) { |
4042 #ifdef WITH_ENCHANT | |
4043 enchant_broker_free_dict(spell_broker, spell_checker); | |
4044 #endif | |
4045 #ifdef WITH_ASPELL | |
4017 delete_aspell_speller(spell_checker); | 4046 delete_aspell_speller(spell_checker); |
4047 #endif | |
4018 spell_checker = NULL; | 4048 spell_checker = NULL; |
4019 } | 4049 } |
4020 | 4050 |
4051 #ifdef WITH_ENCHANT | |
4052 if (spell_broker) { | |
4053 enchant_broker_free(spell_broker); | |
4054 spell_broker = NULL; | |
4055 } | |
4056 #endif | |
4057 #ifdef WITH_ASPELL | |
4021 if (spell_config) { | 4058 if (spell_config) { |
4022 delete_aspell_config(spell_config); | 4059 delete_aspell_config(spell_config); |
4023 spell_config = NULL; | 4060 spell_config = NULL; |
4024 } | 4061 } |
4025 } | 4062 #endif |
4026 | 4063 } |
4027 #define aspell_isalpha(c) (utf8_mode ? iswalpha(get_char(c)) : isalpha(*c)) | 4064 |
4065 #define spell_isalpha(c) (utf8_mode ? iswalpha(get_char(c)) : isalpha(*c)) | |
4028 | 4066 |
4029 // Spell checking function | 4067 // Spell checking function |
4030 static void spellcheck(char *line, char *checked) | 4068 static void spellcheck(char *line, char *checked) |
4031 { | 4069 { |
4032 const char *start, *line_start; | 4070 const char *start, *line_start; |
4036 | 4074 |
4037 line_start = line; | 4075 line_start = line; |
4038 | 4076 |
4039 while (*line) { | 4077 while (*line) { |
4040 | 4078 |
4041 if (!aspell_isalpha(line)) { | 4079 if (!spell_isalpha(line)) { |
4042 line = next_char(line); | 4080 line = next_char(line); |
4043 continue; | 4081 continue; |
4044 } | 4082 } |
4045 | 4083 |
4046 if (!strncmp(line, "http://", 7)) { | 4084 if (!strncmp(line, "http://", 7)) { |
4061 continue; | 4099 continue; |
4062 } | 4100 } |
4063 | 4101 |
4064 start = line; | 4102 start = line; |
4065 | 4103 |
4066 while (aspell_isalpha(line)) | 4104 while (spell_isalpha(line)) |
4067 line = next_char(line); | 4105 line = next_char(line); |
4068 | 4106 |
4069 if (spell_checker && | 4107 if (spell_checker && |
4070 aspell_speller_check(spell_checker, start, line - start) == 0) | 4108 #ifdef WITH_ENCHANT |
4071 memset(&checked[start - line_start], ASPELLBADCHAR, line - start); | 4109 enchant_dict_check(spell_checker, start, line - start) != 0 |
4072 } | |
4073 } | |
4074 #endif | 4110 #endif |
4111 #ifdef WITH_ASPELL | |
4112 aspell_speller_check(spell_checker, start, line - start) == 0 | |
4113 #endif | |
4114 ) | |
4115 memset(&checked[start - line_start], SPELLBADCHAR, line - start); | |
4116 } | |
4117 } | |
4118 #endif | |
4075 | 4119 |
4076 /* vim: set expandtab cindent cinoptions=>2\:2(0: For Vim users... */ | 4120 /* vim: set expandtab cindent cinoptions=>2\:2(0: For Vim users... */ |