1 /* gtkpspell - a spell-checking addon for GtkText
2 * Copyright (c) 2000 Evan Martin.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * Stuphead: (C) 2000,2001 Grigroy Bakunov, Sergey Pinaev
21 * Adapted for Sylpheed (Claws) (c) 2001 by Hiroyuki Yamamoto &
22 * The Sylpheed Claws Team.
23 * Adapted for pspell (c) 2001 Melvin Hadasht
26 #if defined(HAVE_CONFIG_H)
34 #include <sys/types.h>
47 #include <prefs_common.h>
52 #include <gtk/gtkoptionmenu.h>
53 #include <gtk/gtkmenu.h>
54 #include <gtk/gtkmenuitem.h>
60 #include <pspell/pspell.h>
61 /* size of the text buffer used in various word-processing routines. */
64 /* number of suggestions to display on each menu. */
67 /******************************************************************************/
69 /* Function called from menus */
71 static void add_word_to_session (GtkWidget *w, GtkPspell *d);
72 static void add_word_to_personal (GtkWidget *w, GtkPspell *d);
73 static void set_sug_mode (GtkWidget *w, GtkPspell *gtkpspell);
74 static void set_learn_mode (GtkWidget *w, GtkPspell *gtkpspell);
75 static void check_all (GtkWidget *w, GtkPspell *gtkpspell);
76 static void menu_change_dict (GtkWidget *w, GtkPspell *gtkpspell);
77 static void entry_insert_cb (GtkXText *gtktext, gchar *newtext,
78 guint len, guint *ppos,
79 GtkPspell *gtkpspell);
80 static void entry_delete_cb (GtkXText *gtktext, gint start, gint end,
81 GtkPspell *gtkpspell);
82 static gint compare_dict (Dictionary *a, Dictionary *b);
83 guchar *convert_to_pspell_encoding (const guchar *encoding);
85 static void allocate_color(GtkPspell *gtkpspell);
87 /* gtkspellconfig - only one config per session */
88 GtkPspellConfig * gtkpspellconfig;
90 /******************************************************************************/
92 /* gtkspell_init() - run the first pspell_config from which every
93 * new config is cloned
95 GtkPspellConfig * gtkpspell_init()
97 return new_pspell_config();
100 /* gtkspell_finished() - Finish all. No more spelling. Called when the
103 void gtkpspell_finished(GtkPspellConfig *gtkpspellconfig)
105 if (gtkpspellconfig) {
106 delete_pspell_config(gtkpspellconfig);
107 gtkpspellconfig = NULL;
111 /* gtkspell_running - Test if there is a manager running
113 int gtkpspell_running(GtkPspell * gtkpspell)
115 return (gtkpspell->config!=NULL);
118 /* gtkspell_new() - creates a new session if a gtkpspellconfig exists. The
119 * settings are defaults. If no path/dict is set afterwards, the default
122 GtkPspell * gtkpspell_new(GtkPspellConfig *gtkpspellconfig)
124 GtkPspell *gtkpspell;
126 if (gtkpspellconfig == NULL) {
127 gtkpspellconfig = gtkpspell_init();
128 if (gtkpspellconfig == NULL) {
129 debug_print(_("Pspell could not be started."));
130 prefs_common.enable_pspell=FALSE;
135 gtkpspell = g_new(GtkPspell ,1);
136 gtkpspell->config = gtkpspellconfig;
137 gtkpspell->possible_err = new_pspell_manager(gtkpspell->config);
138 gtkpspell->checker = NULL;
140 if (pspell_error_number(gtkpspell->possible_err) != 0) {
141 debug_print(_("Pspell error : %s\n"), pspell_error_message(gtkpspell->possible_err));
142 delete_pspell_can_have_error( gtkpspell->possible_err );
145 gtkpspell->checker = to_pspell_manager(gtkpspell->possible_err);
148 gtkpspell->dictionary_list = NULL;
149 gtkpspell->path = NULL;
150 gtkpspell->dict = NULL;
151 gtkpspell->mode = PSPELL_FASTMODE;
152 gtkpspell->learn = TRUE;
153 gtkpspell->gtktext = NULL;
158 /* gtkspell_new_with_config() - Creates a new session and set path/dic
160 GtkPspell *gtkpspell_new_with_config(GtkPspellConfig *gtkpspellconfig,
161 guchar *path, guchar *dict,
162 guint mode, const guchar *encoding)
164 GtkPspell *gtkpspell;
166 if (gtkpspellconfig == NULL) {
167 gtkpspellconfig=gtkpspell_init();
168 if (gtkpspellconfig == NULL) {
169 debug_print(_("Pspell could not be started."));
170 prefs_common.enable_pspell = FALSE;
175 gtkpspell = g_new( GtkPspell ,1);
176 gtkpspell->path = NULL;
177 gtkpspell->dict = NULL;
178 gtkpspell->dictionary_list = NULL;
179 gtkpspell->gtktext = NULL;
181 gtkpspell->config = pspell_config_clone(gtkpspellconfig);
182 gtkpspell->mode = mode;
183 gtkpspell->learn = TRUE;
185 if (!set_path_and_dict(gtkpspell, gtkpspell->config, path, dict)) {
186 debug_print(_("Pspell could not be configured."));
187 gtkpspell = gtkpspell_delete(gtkpspell);
192 char *pspell_encoding;
193 pspell_encoding = convert_to_pspell_encoding (encoding);
194 debug_print(_("Pspell encoding: asked: %s changed to: %s\n"), encoding, pspell_encoding);
195 pspell_config_replace(gtkpspell->config, "encoding", (const char *)pspell_encoding);
196 if (pspell_config_error_number(gtkpspell->config) !=0 ) {
197 debug_print(_("Pspell encoding error: %s\nSwitching to iso8859-1 (sorry)\n"),
198 pspell_config_error_message(gtkpspell->config));
199 pspell_config_replace(gtkpspell->config, "encoding", "iso8859-1");
201 g_free(pspell_encoding);
204 gtkpspell->possible_err = new_pspell_manager(gtkpspell->config);
205 gtkpspell->checker = NULL;
207 if (pspell_error_number(gtkpspell->possible_err) != 0) {
208 debug_print(_("Pspell error : %s\n"), pspell_error_message(gtkpspell->possible_err));
209 delete_pspell_can_have_error(gtkpspell->possible_err);
210 gtkpspell = gtkpspell_delete(gtkpspell);
213 gtkpspell->checker = to_pspell_manager( gtkpspell->possible_err );
219 /* gtkspell_delete() - Finishes a session
221 GtkPspell *gtkpspell_delete( GtkPspell *gtkpspell )
223 if ( gtkpspell->checker ) {
224 /* First save all word lists */
225 pspell_manager_save_all_word_lists(gtkpspell->checker);
226 delete_pspell_manager(gtkpspell->checker);
227 gtkpspell->checker = NULL;
228 gtkpspell->possible_err = NULL; /* checker is a cast from possible_err */
231 if (gtkpspell->dictionary_list)
232 gtkpspell_free_dictionary_list(gtkpspell->dictionary_list);
234 g_free(gtkpspell->path);
235 g_free(gtkpspell->dict);
236 gtkpspell->path = NULL;
237 gtkpspell->dict = NULL;
243 int set_path_and_dict(GtkPspell *gtkpspell, PspellConfig *config,
244 guchar *path, guchar * dict)
246 guchar *module = NULL;
247 guchar *language = NULL;
248 guchar *spelling = NULL;
249 guchar *jargon = NULL;
255 /* Change nothing if any of path/dict/config is NULL */
256 g_return_val_if_fail(path, 0);
257 g_return_val_if_fail(dict, 0);
258 g_return_val_if_fail(config, 0);
260 /* This is done, so we can free gtkpspell->path, even if it was
261 * given as an argument of the function */
262 temppath = g_strdup(path);
263 g_free(gtkpspell->path);
265 /* pspell dict name format : */
266 /* <lang>[[-<spelling>[-<jargon>]]-<module>.pwli */
269 if (!strrchr(dict,G_DIR_SEPARATOR)) {
270 /* plain dict name */
271 strncpy(buf,dict,BUFSIZE-1);
275 strncpy(buf, strrchr(dict, G_DIR_SEPARATOR)+1, BUFSIZE-1);
278 g_free(gtkpspell->dict);
280 /* Ensure no buffers overflows if the dict is to long */
281 buf[BUFSIZE-1] = 0x00;
284 if ((module = strrchr(buf, '-')) != NULL) {
286 if ((end = strrchr(module, '.')) != NULL)
290 /* In tempdict, we have only the dict name, without path nor
291 extension. Useful in the popup menus */
293 tempdict = g_strdup(buf);
295 /* Probably I am too paranoied... */
296 if (!(language[0] != 0x00 && language[1] != 0x00))
299 spelling = strchr(language, '-');
300 if (spelling != NULL) {
304 if (spelling != module) {
305 if ((end = strchr(spelling, '-')) != NULL) {
308 if (jargon != module)
309 if ((end = strchr(jargon, '-')) != NULL)
323 debug_print(_("Language : %s\nSpelling: %s\nJargon: %s\nModule: %s\n"),
324 language, spelling, jargon, module);
326 if (temppath[strlen(temppath)-1] == G_DIR_SEPARATOR)
327 temppath[strlen(temppath)-1]= 0;
329 pspell_config_replace(config, "add-word-list-path", temppath);
330 debug_print(_("Pspell config: added path %s\n"), pspell_config_retrieve(config, "word-list-path"));
331 if (pspell_config_error_number(config))
332 debug_print(_("Pspell config: %s\n"), pspell_config_error_message(config));
335 pspell_config_replace(config, "language-tag", language);
337 pspell_config_replace(config, "spelling", spelling);
339 pspell_config_replace(config, "jargon", jargon);
341 pspell_config_replace(config, "module", module);
343 switch(gtkpspell->mode) {
344 case PSPELL_FASTMODE:
345 pspell_config_replace(config, "sug-mode", "fast");
347 case PSPELL_NORMALMODE:
348 pspell_config_replace(config, "sug-mode", "normal");
350 case PSPELL_BADSPELLERMODE:
351 pspell_config_replace(config, "sug-mode", "bad-spellers");
355 gtkpspell->path = g_strdup(temppath);
356 gtkpspell->dict = g_strdup(tempdict);
364 /* gtkpspell_set_path_and_dict() - Set path and dict. The session is
366 * FALSE on error, TRUE on success */
367 int gtkpspell_set_path_and_dict(GtkPspell * gtkpspell, guchar * path,
370 PspellConfig * config2;
372 /* It seems changing an already running config is not the way to go
375 config2 = pspell_config_clone(gtkpspell->config);
377 if (gtkpspell->checker) {
378 pspell_manager_save_all_word_lists(gtkpspell->checker);
379 delete_pspell_manager(gtkpspell->checker);
382 gtkpspell->checker = NULL;
383 gtkpspell->possible_err = NULL;
385 if (set_path_and_dict(gtkpspell,config2,path,dict) == 0) {
386 debug_print(_("Pspell set_path_and_dict error."));
390 gtkpspell->possible_err = new_pspell_manager(config2);
392 delete_pspell_config(config2);
395 if (pspell_error_number(gtkpspell->possible_err) != 0) {
396 debug_print(_("Pspell path & dict. error %s\n"),
397 pspell_error_message(gtkpspell->possible_err));
398 delete_pspell_can_have_error(gtkpspell->possible_err);
399 gtkpspell->possible_err = NULL;
403 gtkpspell->checker=to_pspell_manager(gtkpspell->possible_err);
408 /* gtkpspell_get_dict() - What dict are we using ? language-spelling-jargon-module format */
409 /* Actually, this function is not used and hence not tested. */
410 /* Returns an allocated string */
411 guchar *gtkpspell_get_dict(GtkPspell *gtkpspell)
413 /* Number of dashes in a dictionary name + 1 (for trailing null)
414 * needed when rebuilding dictionary name
416 #define DASHES_IN_DICTNAME 3+1
424 g_return_val_if_fail(gtkpspell->config, NULL);
426 language = g_strdup(pspell_config_retrieve(gtkpspell->config, "language"));
427 spelling = g_strdup(pspell_config_retrieve(gtkpspell->config, "spelling"));
428 jargon = g_strdup(pspell_config_retrieve(gtkpspell->config, "jargon" ));
429 len = strlen(language) + strlen(spelling) + strlen(jargon);
431 if (len + DASHES_IN_DICTNAME < BUFSIZE) {
432 dict = g_new(char,len + DASHES_IN_DICTNAME);
433 strcpy(dict, language);
436 strcat(dict, spelling);
450 /* gtkpspell_get_path() - Return the dict path as an allocated string */
451 /* Not used = not tested */
452 guchar *gtkpspell_get_path(GtkPspell *gtkpspell)
457 g_return_val_if_fail(gtkpspell->config, NULL);
459 path = g_strdup(pspell_config_retrieve(gtkpspell->config,"word-list-path"));
464 /* menu_change_dict() - Menu callback : change dict */
465 static void menu_change_dict(GtkWidget *w, GtkPspell *gtkpspell)
470 /* Dict is simply the menu label */
472 gtk_label_get(GTK_LABEL(GTK_BIN(w)->child), (gchar **) &thelabel);
474 if (!strcmp2(thelabel, _("None")))
477 thedict = g_strdup(thelabel);
479 /* Set path, dict, (and sug_mode ?) */
480 if(!gtkpspell_set_path_and_dict(gtkpspell, gtkpspell->path, thedict)) {
481 /* FIXME : try to handle this very special case */
482 debug_print("Pspell: Attempt to change to a non existant dict. I will crash after closing compose window.\n");
483 gtkpspell_detach(gtkpspell);
484 gtkpspell = gtkpspell_delete(gtkpspell);
489 /* set_sug_mode() - Menu callback : Set the suggestion mode */
490 static void set_sug_mode(GtkWidget *w, GtkPspell *gtkpspell)
492 unsigned char *themode;
494 gtk_label_get(GTK_LABEL(GTK_BIN(w)->child), (gchar **) &themode);
496 if (!strcmp(themode, _("Fast Mode"))) {
497 gtkpspell_set_sug_mode(gtkpspell, "fast");
498 gtkpspell->mode = PSPELL_FASTMODE;
500 if (!strcmp(themode,_("Normal Mode"))) {
501 gtkpspell_set_sug_mode(gtkpspell, "normal");
502 gtkpspell->mode = PSPELL_NORMALMODE;
504 if (!strcmp( themode,_("Bad Spellers Mode"))) {
505 gtkpspell_set_sug_mode(gtkpspell, "bad-spellers");
506 gtkpspell->mode = PSPELL_BADSPELLERMODE;
510 /* gtkpspell_set_sug_mode() - Set the suggestion mode */
511 /* Actually, the session is resetted and everything is reset. */
512 /* We take the used path/dict pair and create a new config with them */
513 int gtkpspell_set_sug_mode(GtkPspell *gtkpspell, gchar *themode)
515 PspellConfig *config2;
519 pspell_manager_save_all_word_lists(gtkpspell->checker);
520 delete_pspell_manager(gtkpspell->checker);
522 gtkpspell->checker = NULL;
524 config2 = pspell_config_clone(gtkpspell->config);
526 if (!set_path_and_dict(gtkpspell, config2, gtkpspell->path, gtkpspell->dict)) {
527 debug_print(_("Pspell set_sug_mod could not reset path & dict\n"));
531 pspell_config_replace(config2, "sug-mode", themode);
533 gtkpspell->possible_err = new_pspell_manager(config2);
534 delete_pspell_config(config2);
537 if (pspell_error_number(gtkpspell->possible_err) != 0) {
538 debug_print(_("Pspell set sug-mode error %s\n"),
539 pspell_error_message(gtkpspell->possible_err));
540 delete_pspell_can_have_error(gtkpspell->possible_err);
541 gtkpspell->possible_err = NULL;
544 gtkpspell->checker = to_pspell_manager(gtkpspell->possible_err);
548 /* set_learn_mode() - menu callback to toggle learn mode */
549 static void set_learn_mode (GtkWidget *w, GtkPspell *gtkpspell)
551 gtkpspell->learn = gtkpspell->learn == FALSE ;
554 /* misspelled_suggest() - Create a suggestion list for word */
555 static GList *misspelled_suggest(GtkPspell *gtkpspell, guchar *word)
557 const guchar *newword;
560 const PspellWordList *suggestions;
561 PspellStringEmulation *elements;
563 g_return_val_if_fail(word, NULL);
565 if (!pspell_manager_check(gtkpspell->checker, word, -1)) {
566 suggestions = pspell_manager_suggest(gtkpspell->checker, (const char *)word, -1);
567 elements = pspell_word_list_elements(suggestions);
568 /* First one must be the misspelled (why this ?) */
569 list = g_list_append(list, g_strdup(word));
571 while ((newword = pspell_string_emulation_next(elements)) != NULL)
572 list = g_list_append(list, g_strdup(newword));
578 /* misspelled_test() - Just test if word is correctly spelled */
579 static int misspelled_test(GtkPspell *gtkpspell, unsigned char *word)
581 return pspell_manager_check(gtkpspell->checker, word, -1) ? 0 : 1;
585 static gboolean iswordsep(unsigned char c)
587 return !isalpha(c) && c != '\'';
590 static guchar get_text_index_whar(GtkPspell *gtkpspell, int pos)
595 text = gtk_editable_get_chars(GTK_EDITABLE(gtkpspell->gtktext), pos, pos + 1);
603 /* get_word_from_pos () - return the word pointed to. */
604 /* Handles correctly the quotes. */
605 static gboolean get_word_from_pos(GtkPspell *gtkpspell, int pos,
607 int *pstart, int *pend)
610 /* TODO : when correcting a word into quotes, change the color of */
611 /* the quotes too, as may be they were highlighted before. To do */
612 /* so, we can use two others pointers that points to the whole */
613 /* word including quotes. */
620 gtktext = gtkpspell->gtktext;
621 if (iswordsep(get_text_index_whar(gtkpspell, pos)))
624 /* The apostrophe character is somtimes used for quotes
625 * So include it in the word only if it is not surrounded
626 * by other characters.
629 for (start = pos; start >= 0; --start) {
630 c = get_text_index_whar(gtkpspell, start);
633 if (!isalpha(get_text_index_whar(gtkpspell, start - 1))) {
634 /* start_quote = TRUE; */
639 /* start_quote = TRUE; */
649 for (end = pos; end < gtk_xtext_get_length(gtktext); end++) {
650 c = get_text_index_whar(gtkpspell, end);
652 if (end < gtk_xtext_get_length(gtktext)) {
653 if (!isalpha(get_text_index_whar(gtkpspell, end + 1))) {
654 /* end_quote = TRUE; */
659 /* end_quote = TRUE; */
669 for (pos = start; pos < end; pos++)
670 buf[pos - start] = get_text_index_whar(gtkpspell, pos);
671 buf[pos - start] = 0;
682 static gboolean get_curword(GtkPspell *gtkpspell, unsigned char* buf,
683 int *pstart, int *pend)
685 int pos = gtk_editable_get_position(GTK_EDITABLE(gtkpspell->gtktext));
686 return get_word_from_pos(gtkpspell, pos, buf, pstart, pend);
689 static void change_color(GtkPspell * gtkpspell,
696 g_return_if_fail(start < end);
698 gtktext = gtkpspell->gtktext;
700 gtk_xtext_freeze(gtktext);
701 newtext = gtk_editable_get_chars(GTK_EDITABLE(gtktext), start, end);
703 gtk_xtext_set_point(gtktext, start);
704 gtk_xtext_forward_delete(gtktext, end - start);
706 gtk_xtext_insert(gtktext, NULL, color, NULL, newtext, end - start);
709 gtk_xtext_thaw(gtktext);
712 static gboolean check_at(GtkPspell *gtkpspell, int from_pos)
715 unsigned char buf[BUFSIZE];
718 g_return_val_if_fail(from_pos >= 0, FALSE);
720 gtktext = gtkpspell->gtktext;
722 if (!get_word_from_pos(gtkpspell, from_pos, buf, &start, &end))
725 strncpy(gtkpspell->theword, buf, BUFSIZE - 1);
726 gtkpspell->theword[BUFSIZE - 1] = 0;
728 if (misspelled_test(gtkpspell, buf)) {
729 change_color(gtkpspell, start, end, &(gtkpspell->highlight));
732 change_color(gtkpspell, start, end, NULL);
738 static void check_all(GtkWidget *w, GtkPspell *gtkpspell)
740 gtkpspell_check_all(gtkpspell);
744 void gtkpspell_check_all(GtkPspell *gtkpspell)
753 if (!gtkpspell_running(gtkpspell)) return ;
754 gtktext = gtkpspell->gtktext;
756 len = gtk_xtext_get_length(gtktext);
758 adj_value = gtktext->vadj->value;
759 gtk_xtext_freeze(gtktext);
760 origpos = gtk_editable_get_position(GTK_EDITABLE(gtktext));
761 gtk_editable_set_position(GTK_EDITABLE(gtktext),0);
763 while (pos < len && iswordsep(get_text_index_whar(gtkpspell, pos)))
765 while (pos < len && !iswordsep(get_text_index_whar(gtkpspell, pos)))
768 check_at(gtkpspell, pos - 1);
770 gtk_xtext_thaw(gtktext);
771 gtk_editable_set_position(GTK_EDITABLE(gtktext), origpos);
772 gtk_editable_select_region(GTK_EDITABLE(gtktext), origpos, origpos);
775 static void entry_insert_cb(GtkXText *gtktext, gchar *newtext,
776 guint len, guint *ppos,
777 GtkPspell * gtkpspell)
780 if (!gtkpspell_running(gtkpspell))
783 /* We must insert ourself the character to impose the */
784 /* color of the inserted character to be default */
785 /* Never mess with set_insertion when frozen */
786 gtk_xtext_freeze(gtktext);
787 gtk_xtext_backward_delete(GTK_XTEXT(gtktext), len);
788 gtk_xtext_insert(GTK_XTEXT(gtktext), NULL, NULL, NULL, newtext, len);
789 *ppos = gtk_xtext_get_point(GTK_XTEXT(gtktext));
791 if (iswordsep(newtext[0])) {
792 /* did we just end a word? */
794 check_at(gtkpspell, *ppos - 2);
796 /* did we just split a word? */
797 if (*ppos < gtk_xtext_get_length(gtktext))
798 check_at(gtkpspell, *ppos + 1);
800 /* check as they type, *except* if they're typing at the end (the most
803 if (*ppos < gtk_xtext_get_length(gtktext)
804 && !iswordsep(get_text_index_whar(gtkpspell, *ppos)))
805 check_at(gtkpspell, *ppos - 1);
807 gtk_xtext_thaw(gtktext);
808 gtk_editable_set_position(GTK_EDITABLE(gtktext), *ppos);
811 static void entry_delete_cb(GtkXText *gtktext, gint start, gint end,
812 GtkPspell *gtkpspell)
816 if (!gtkpspell_running(gtkpspell))
819 origpos = gtk_editable_get_position(GTK_EDITABLE(gtktext));
821 check_at(gtkpspell, start - 1);
822 check_at(gtkpspell, start);
825 gtk_editable_set_position(GTK_EDITABLE(gtktext), origpos);
826 gtk_xtext_set_point(gtktext, origpos);
827 gtk_editable_select_region(GTK_EDITABLE(gtktext), origpos, origpos);
828 /* this is to *UNDO* the selection, in case they were holding shift
829 * while hitting backspace. */
832 static void replace_word(GtkWidget *w, GtkPspell *gtkpspell)
834 int start, end,oldlen, newlen;
836 unsigned char *newword;
837 unsigned char buf[BUFSIZE];
841 gtktext = gtkpspell->gtktext;
843 gtk_xtext_freeze(GTK_XTEXT(gtktext));
844 origpos = gtkpspell->orig_pos;
847 gtk_label_get(GTK_LABEL(GTK_BIN(w)->child), (gchar**) &newword);
848 newlen = strlen(newword);
850 get_curword(gtkpspell, buf, &start, &end);
851 oldlen = end - start;
853 gtk_xtext_set_point(GTK_XTEXT(gtktext), end);
854 gtk_signal_handler_block_by_func(GTK_OBJECT(gtktext),
855 GTK_SIGNAL_FUNC(entry_insert_cb),
857 gtk_signal_handler_block_by_func(GTK_OBJECT(gtktext),
858 GTK_SIGNAL_FUNC(entry_delete_cb),
860 gtk_xtext_set_point(gtktext, start);
861 gtk_signal_emit_by_name(GTK_OBJECT(gtktext), "delete-text", start, end);
862 gtk_signal_emit_by_name(GTK_OBJECT(gtktext), "insert-text", newword, strlen(newword), &start);
864 gtk_signal_handler_unblock_by_func(GTK_OBJECT(gtktext),
865 GTK_SIGNAL_FUNC(entry_insert_cb),
867 gtk_signal_handler_unblock_by_func(GTK_OBJECT(gtktext),
868 GTK_SIGNAL_FUNC(entry_delete_cb),
870 if (end-start > 0 && gtkpspell->learn) {
871 /* Just be sure the buffer ends somewhere... */
873 /* Learn from common misspellings */
874 pspell_manager_store_replacement(gtkpspell->checker, buf,
879 /* Put the point and the position where we clicked with the mouse */
880 /* It seems to be a hack, as I must thaw,freeze,thaw the widget */
881 /* to let it update correctly the word insertion and then the */
882 /* point & position position. If not, SEGV after the first replacement */
883 /* If the new word ends before point, put the point at its end*/
885 if (origpos-start <= oldlen && origpos-start >= 0) {
886 /* Original point was in the word. */
887 /* Put the insertion point in the same location */
888 /* with respect to the new length */
889 /* If the original position is still within the word, */
890 /* then keep the original position. If not, move to the */
891 /* end of the word */
892 if (origpos-start > newlen)
893 pos = start + newlen;
895 else if (origpos > end) {
896 /* move the position according to the change of length */
897 pos = origpos + newlen - oldlen;
899 gtk_xtext_thaw(GTK_XTEXT(gtktext));
900 gtk_xtext_freeze(GTK_XTEXT(gtktext));
901 if (GTK_XTEXT(gtktext)->text_len < pos)
902 pos = gtk_xtext_get_length(GTK_XTEXT(gtktext));
903 gtkpspell->orig_pos = pos;
904 gtk_editable_set_position(GTK_EDITABLE(gtktext), gtkpspell->orig_pos);
905 gtk_xtext_set_point(GTK_XTEXT(gtktext),
906 gtk_editable_get_position(GTK_EDITABLE(gtktext)));
907 gtk_xtext_thaw(GTK_XTEXT(gtktext));
910 /* Accept this word for this session */
912 static void add_word_to_session(GtkWidget *w, GtkPspell *gtkpspell)
917 gtkxtext = gtkpspell->gtktext;
918 gtk_xtext_freeze(GTK_XTEXT(gtkxtext));
919 pos = gtk_editable_get_position(GTK_EDITABLE(gtkxtext));
921 pspell_manager_add_to_session(gtkpspell->checker,gtkpspell->theword, strlen(gtkpspell->theword));
923 check_at(gtkpspell,gtk_editable_get_position(GTK_EDITABLE(gtkxtext)));
925 gtk_xtext_thaw(GTK_XTEXT(gtkxtext));
926 gtk_xtext_freeze(GTK_XTEXT(gtkxtext));
927 gtk_editable_set_position(GTK_EDITABLE(gtkxtext),pos);
928 gtk_xtext_set_point(GTK_XTEXT(gtkxtext), gtk_editable_get_position(GTK_EDITABLE(gtkxtext)));
929 gtk_xtext_thaw(GTK_XTEXT(gtkxtext));
932 /* add_word_to_personal() - add word to personal dict. */
934 static void add_word_to_personal(GtkWidget *w, GtkPspell *gtkpspell)
939 gtkxtext = gtkpspell->gtktext;
941 gtk_xtext_freeze(GTK_XTEXT(gtkxtext));
942 pos = gtk_editable_get_position(GTK_EDITABLE(gtkxtext));
944 pspell_manager_add_to_personal(gtkpspell->checker,gtkpspell->theword,strlen(gtkpspell->theword));
946 check_at(gtkpspell,gtk_editable_get_position(GTK_EDITABLE(gtkxtext)));
947 gtk_xtext_thaw(GTK_XTEXT(gtkxtext));
948 gtk_xtext_freeze(GTK_XTEXT(gtkxtext));
949 gtk_editable_set_position(GTK_EDITABLE(gtkxtext),pos);
950 gtk_xtext_set_point(GTK_XTEXT(gtkxtext), gtk_editable_get_position(GTK_EDITABLE(gtkxtext)));
951 gtk_xtext_thaw(GTK_XTEXT(gtkxtext));
955 static GtkMenu *make_menu_config(GtkPspell *gtkpspell)
957 GtkWidget *menu, *item, *submenu;
960 menu = gtk_menu_new();
962 item = gtk_menu_item_new_with_label(_("Spell check all"));
963 gtk_widget_show(item);
964 gtk_menu_append(GTK_MENU(menu), item);
965 gtk_signal_connect(GTK_OBJECT(item),"activate",
966 GTK_SIGNAL_FUNC(check_all),
970 item = gtk_menu_item_new();
971 gtk_widget_show(item);
972 gtk_menu_append(GTK_MENU(menu),item);
974 submenu = gtk_menu_new();
975 item = gtk_menu_item_new_with_label(_("Change dictionary"));
976 gtk_widget_show(item);
977 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item),submenu);
978 gtk_menu_append(GTK_MENU(menu), item);
981 if (gtkpspell->dictionary_list==NULL)
982 gtkpspell->dictionary_list = gtkpspell_get_dictionary_list(gtkpspell->path);
985 GtkWidget * curmenu = submenu;
990 tmp = gtkpspell->dictionary_list;
991 for (tmp = gtkpspell->dictionary_list; tmp != NULL; tmp =g_slist_next(tmp)) {
992 dict = (Dictionary *) tmp->data;
993 item = gtk_check_menu_item_new_with_label(dict->name);
994 if (strcmp2(dict->name, gtkpspell->dict))
995 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), FALSE);
997 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), TRUE);
998 gtk_widget_set_sensitive(GTK_WIDGET(item), FALSE);
1000 gtk_menu_append(GTK_MENU(curmenu), item);
1001 gtk_signal_connect(GTK_OBJECT(item), "activate",
1002 GTK_SIGNAL_FUNC(menu_change_dict),
1004 gtk_widget_show(item);
1006 if (count == MENUCOUNT) {
1008 newmenu = gtk_menu_new();
1009 item = gtk_menu_item_new_with_label(_("More..."));
1010 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), newmenu);
1011 gtk_menu_append(GTK_MENU(curmenu), item);
1012 gtk_widget_show(item);
1019 item = gtk_menu_item_new();
1020 gtk_widget_show(item);
1021 gtk_menu_append(GTK_MENU(menu), item);
1023 item = gtk_check_menu_item_new_with_label(_("Fast Mode"));
1024 gtk_widget_show(item);
1025 gtk_menu_append(GTK_MENU(menu), item);
1026 if (gtkpspell->mode == PSPELL_FASTMODE) {
1027 gtk_widget_set_sensitive(GTK_WIDGET(item),FALSE);
1028 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item),TRUE);
1031 gtk_signal_connect(GTK_OBJECT(item), "activate",
1032 GTK_SIGNAL_FUNC(set_sug_mode),
1035 item = gtk_check_menu_item_new_with_label(_("Normal Mode"));
1036 gtk_widget_show(item);
1037 gtk_menu_append(GTK_MENU(menu),item);
1038 if (gtkpspell->mode == PSPELL_NORMALMODE) {
1039 gtk_widget_set_sensitive(GTK_WIDGET(item), FALSE);
1040 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), TRUE);
1043 gtk_signal_connect(GTK_OBJECT(item), "activate",
1044 GTK_SIGNAL_FUNC(set_sug_mode),
1047 item = gtk_check_menu_item_new_with_label(_("Bad Spellers Mode"));
1048 gtk_widget_show(item);
1049 gtk_menu_append(GTK_MENU(menu), item);
1050 if (gtkpspell->mode==PSPELL_BADSPELLERMODE) {
1051 gtk_widget_set_sensitive(GTK_WIDGET(item), FALSE);
1052 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), TRUE);
1055 gtk_signal_connect(GTK_OBJECT(item), "activate",
1056 GTK_SIGNAL_FUNC(set_sug_mode),
1058 item = gtk_menu_item_new();
1059 gtk_widget_show(item);
1060 gtk_menu_append(GTK_MENU(menu), item);
1062 item = gtk_check_menu_item_new_with_label(_("Learn from mistakes"));
1063 gtk_widget_show(item);
1064 gtk_menu_append(GTK_MENU(menu), item);
1065 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item),
1067 gtk_signal_connect(GTK_OBJECT(item), "activate",
1068 GTK_SIGNAL_FUNC(set_learn_mode), gtkpspell);
1071 return GTK_MENU(menu);
1074 /* make_menu() - Add menus to accept this word for this session and to add it to
1075 * personal dictionary */
1076 static GtkMenu *make_menu(GList *l, GtkPspell *gtkpspell)
1078 GtkWidget *menu, *item;
1079 unsigned char *caption;
1082 gtktext = gtkpspell->gtktext;
1084 menu = gtk_menu_new();
1085 caption = g_strdup_printf(_("Accept `%s' for this session"), (unsigned char*)l->data);
1086 item = gtk_menu_item_new_with_label(caption);
1088 gtk_widget_show(item);
1089 gtk_menu_append(GTK_MENU(menu), item);
1091 gtk_signal_connect(GTK_OBJECT(item), "activate",
1092 GTK_SIGNAL_FUNC(add_word_to_session),
1095 caption = g_strdup_printf(_("Add `%s' to personal dictionary"), (char*)l->data);
1096 item = gtk_menu_item_new_with_label(caption);
1098 gtk_widget_show(item);
1099 gtk_menu_append(GTK_MENU(menu), item);
1101 gtk_signal_connect(GTK_OBJECT(item), "activate",
1102 GTK_SIGNAL_FUNC(add_word_to_personal),
1105 item = gtk_menu_item_new();
1106 gtk_widget_show(item);
1107 gtk_menu_append(GTK_MENU(menu), item);
1111 item = gtk_menu_item_new_with_label(_("(no suggestions)"));
1112 gtk_widget_show(item);
1113 gtk_menu_append(GTK_MENU(menu), item);
1115 GtkWidget *curmenu = menu;
1119 if (l->data == NULL && l->next != NULL) {
1121 curmenu = gtk_menu_new();
1122 item = gtk_menu_item_new_with_label(_("Others..."));
1123 gtk_widget_show(item);
1124 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), curmenu);
1125 gtk_menu_append(GTK_MENU(curmenu), item);
1127 } else if (count > MENUCOUNT) {
1129 item = gtk_menu_item_new_with_label(_("More..."));
1130 gtk_widget_show(item);
1131 gtk_menu_append(GTK_MENU(curmenu), item);
1132 curmenu = gtk_menu_new();
1133 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), curmenu);
1135 item = gtk_menu_item_new_with_label((unsigned char*)l->data);
1136 gtk_signal_connect(GTK_OBJECT(item), "activate",
1137 GTK_SIGNAL_FUNC(replace_word), gtkpspell);
1138 gtk_widget_show(item);
1139 gtk_menu_append(GTK_MENU(curmenu), item);
1141 } while ((l = l->next) != NULL);
1143 return GTK_MENU(menu);
1146 static void popup_menu(GtkPspell *gtkpspell, GdkEventButton *eb)
1148 unsigned char buf[BUFSIZE];
1152 gtktext = gtkpspell->gtktext;
1153 gtkpspell->orig_pos = gtk_editable_get_position(GTK_EDITABLE(gtktext));
1155 if (!(eb->state & GDK_SHIFT_MASK))
1156 if (get_curword(gtkpspell, buf, NULL, NULL)) {
1158 strncpy(gtkpspell->theword, buf, BUFSIZE - 1);
1159 gtkpspell->theword[BUFSIZE - 1] = 0;
1160 list = misspelled_suggest(gtkpspell, buf);
1162 gtk_menu_popup(make_menu(list, gtkpspell), NULL, NULL, NULL, NULL,
1163 eb->button, eb->time);
1164 for (l = list; l != NULL; l = l->next)
1171 gtk_menu_popup(make_menu_config(gtkpspell),NULL,NULL,NULL,NULL,
1172 eb->button,eb->time);
1175 /* ok, this is pretty wacky:
1176 * we need to let the right-mouse-click go through, so it moves the cursor,
1177 * but we *can't* let it go through, because GtkText interprets rightclicks as
1178 * weird selection modifiers.
1180 * so what do we do? forge rightclicks as leftclicks, then popup the menu.
1183 static gint button_press_intercept_cb(GtkXText *gtktext, GdkEvent *e, GtkPspell *gtkpspell)
1188 if (!gtkpspell_running(gtkpspell))
1191 if (e->type != GDK_BUTTON_PRESS)
1193 eb = (GdkEventButton*) e;
1195 if (eb->button != 3)
1198 /* forge the leftclick */
1201 gtk_signal_handler_block_by_func(GTK_OBJECT(gtktext),
1202 GTK_SIGNAL_FUNC(button_press_intercept_cb),
1204 gtk_signal_emit_by_name(GTK_OBJECT(gtktext), "button-press-event",
1206 gtk_signal_handler_unblock_by_func(GTK_OBJECT(gtktext),
1207 GTK_SIGNAL_FUNC(button_press_intercept_cb),
1209 gtk_signal_emit_stop_by_name(GTK_OBJECT(gtktext), "button-press-event");
1211 /* now do the menu wackiness */
1212 popup_menu(gtkpspell, eb);
1213 gtk_grab_remove(GTK_WIDGET(gtktext));
1217 void gtkpspell_uncheck_all(GtkPspell * gtkpspell)
1220 unsigned char *text;
1224 gtktext=gtkpspell->gtktext;
1226 adj_value = gtktext->vadj->value;
1227 gtk_xtext_freeze(gtktext);
1228 origpos = gtk_editable_get_position(GTK_EDITABLE(gtktext));
1229 text = gtk_editable_get_chars(GTK_EDITABLE(gtktext), 0, -1);
1230 gtk_xtext_set_point(gtktext, 0);
1231 gtk_xtext_forward_delete(gtktext, gtk_xtext_get_length(gtktext));
1232 gtk_xtext_insert(gtktext, NULL, NULL, NULL, text, strlen(text));
1233 gtk_xtext_thaw(gtktext);
1235 gtk_editable_set_position(GTK_EDITABLE(gtktext), origpos);
1236 gtk_adjustment_set_value(gtktext->vadj, adj_value);
1239 void gtkpspell_attach(GtkPspell *gtkpspell, GtkXText *gtktext)
1241 gtkpspell->gtktext = gtktext;
1242 gtk_signal_connect_after(GTK_OBJECT(gtktext), "insert-text",
1243 GTK_SIGNAL_FUNC(entry_insert_cb), gtkpspell);
1244 gtk_signal_connect_after(GTK_OBJECT(gtktext), "delete-text",
1245 GTK_SIGNAL_FUNC(entry_delete_cb), gtkpspell);
1246 gtk_signal_connect(GTK_OBJECT(gtktext), "button-press-event",
1247 GTK_SIGNAL_FUNC(button_press_intercept_cb), gtkpspell);
1249 allocate_color(gtkpspell);
1253 void gtkpspell_detach(GtkPspell * gtkpspell)
1257 gtktext =gtkpspell->gtktext;
1258 /* if (prefs_common.auto_makepspell) { */
1259 gtk_signal_disconnect_by_func(GTK_OBJECT(gtktext),
1260 GTK_SIGNAL_FUNC(entry_insert_cb), gtkpspell);
1261 gtk_signal_disconnect_by_func(GTK_OBJECT(gtktext),
1262 GTK_SIGNAL_FUNC(entry_delete_cb), gtkpspell);
1264 gtk_signal_disconnect_by_func(GTK_OBJECT(gtktext),
1265 GTK_SIGNAL_FUNC(button_press_intercept_cb), gtkpspell);
1267 gtkpspell_uncheck_all(gtkpspell);
1270 /*** Sylpheed (Claws) ***/
1272 static GSList *create_empty_dictionary_list(void)
1274 GSList *list = NULL;
1277 dict = g_new0(Dictionary, 1);
1278 dict->name = g_strdup(_("None"));
1279 return g_slist_append(list, dict);
1282 /* gtkpspell_get_dictionary_list() - returns list of dictionary names */
1283 GSList *gtkpspell_get_dictionary_list(const gchar *pspell_path)
1286 gchar *dict_path, *tmp, *prevdir;
1295 #warning TODO: no directory change
1297 dict_path=g_strdup(pspell_path);
1298 prevdir = g_get_current_dir();
1299 if (chdir(dict_path) <0) {
1300 FILE_OP_ERROR(dict_path, "chdir");
1303 return create_empty_dictionary_list();
1306 debug_print(_("Checking for dictionaries in %s\n"), dict_path);
1308 if (NULL != (dir = opendir("."))) {
1309 while (NULL != (ent = readdir(dir))) {
1310 /* search for pwli */
1311 if (NULL != (tmp = strstr(ent->d_name, ".pwli"))) {
1312 dict = g_new0(Dictionary, 1);
1313 dict->name = g_strndup(ent->d_name, tmp - ent->d_name);
1314 debug_print(_("Found dictionary %s\n"), dict->name);
1315 list = g_slist_insert_sorted(list, dict, (GCompareFunc) compare_dict);
1321 FILE_OP_ERROR(dict_path, "opendir");
1322 debug_print(_("No dictionary found\n"));
1323 list = create_empty_dictionary_list();
1326 debug_print(_("No dictionary found\n"));
1327 list = create_empty_dictionary_list();
1335 /* compare_dict () - compare 2 dict names */
1337 static gint compare_dict(Dictionary *a, Dictionary *b)
1339 guchar *alanguage, *blanguage,
1340 *aspelling, *bspelling,
1341 *ajargon , *bjargon ,
1342 *amodule , *bmodule ;
1343 guint aparts = 0, bparts = 0, i;
1345 for (i=0; i < strlen(a->name) ; i++)
1346 if (a->name[i]=='-')
1348 for (i=0; i < strlen(b->name) ; i++)
1349 if (b->name[i]=='-')
1352 if (aparts != bparts)
1353 return (aparts < bparts) ? -1 : +1 ;
1355 return strcmp2(a->name, b->name);
1357 void gtkpspell_free_dictionary_list(GSList *list)
1361 for (walk = list; walk != NULL; walk = g_slist_next(walk))
1363 dict = (Dictionary *) walk->data;
1371 static void dictionary_option_menu_item_data_destroy(gpointer data)
1373 gchar *str = (gchar *) data;
1379 GtkWidget *gtkpspell_dictionary_option_menu_new(const gchar *pspell_path)
1381 GSList *dict_list, *tmp;
1386 dict_list = gtkpspell_get_dictionary_list(pspell_path);
1387 g_return_val_if_fail(dict_list, NULL);
1389 menu = gtk_menu_new();
1391 for (tmp = dict_list; tmp != NULL; tmp = g_slist_next(tmp)) {
1392 dict = (Dictionary *) tmp->data;
1393 item = gtk_menu_item_new_with_label(dict->name);
1395 gtk_object_set_data_full(GTK_OBJECT(item), "dict_name",
1396 g_strdup(dict->name),
1397 dictionary_option_menu_item_data_destroy);
1398 gtk_menu_append(GTK_MENU(menu), item);
1399 gtk_widget_show(item);
1402 gtk_widget_show(menu);
1404 gtkpspell_free_dictionary_list(dict_list);
1411 gchar *gtkpspell_get_dictionary_menu_active_item(GtkWidget *menu)
1413 GtkWidget *menuitem;
1416 g_return_val_if_fail(GTK_IS_MENU(menu), NULL);
1417 menuitem = gtk_menu_get_active(GTK_MENU(menu));
1418 result = gtk_object_get_data(GTK_OBJECT(menuitem), "dict_name");
1419 g_return_val_if_fail(result, NULL);
1420 return g_strdup(result);
1424 GtkWidget *gtkpspell_sugmode_option_menu_new(gint sugmode)
1430 menu = gtk_menu_new();
1431 gtk_widget_show(menu);
1433 item = gtk_menu_item_new_with_label(_("Fast Mode"));
1434 gtk_widget_show(item);
1435 gtk_menu_append(GTK_MENU(menu), item);
1436 gtk_object_set_data(GTK_OBJECT(item), "sugmode", GINT_TO_POINTER(PSPELL_FASTMODE));
1438 item = gtk_menu_item_new_with_label(_("Normal Mode"));
1439 gtk_widget_show(item);
1440 gtk_menu_append(GTK_MENU(menu), item);
1441 gtk_object_set_data(GTK_OBJECT(item), "sugmode", GINT_TO_POINTER(PSPELL_NORMALMODE));
1443 item = gtk_menu_item_new_with_label(_("Bad Spellers Mode"));
1444 gtk_widget_show(item);
1445 gtk_menu_append(GTK_MENU(menu), item);
1446 gtk_object_set_data(GTK_OBJECT(item), "sugmode", GINT_TO_POINTER(PSPELL_BADSPELLERMODE));
1451 void gtkpspell_sugmode_option_menu_set(GtkOptionMenu *optmenu, gint sugmode)
1453 g_return_if_fail(GTK_IS_OPTION_MENU(optmenu));
1455 g_return_if_fail(sugmode == PSPELL_FASTMODE ||
1456 sugmode == PSPELL_NORMALMODE ||
1457 sugmode == PSPELL_BADSPELLERMODE);
1459 gtk_option_menu_set_history(GTK_OPTION_MENU(optmenu), sugmode - 1);
1462 gint gtkpspell_get_sugmode_from_option_menu(GtkOptionMenu *optmenu)
1467 g_return_val_if_fail(GTK_IS_OPTION_MENU(optmenu), -1);
1469 item = gtk_menu_get_active(GTK_MENU(gtk_option_menu_get_menu(optmenu)));
1471 sugmode = GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(item), "sugmode"));
1477 /* convert_to_pspell_encoding () - converts ISO-8859-* strings to iso8859-*
1478 * as needed by pspell. Returns an allocated string.
1481 guchar *convert_to_pspell_encoding (const guchar *encoding)
1483 guchar * pspell_encoding;
1485 if (strstr2(encoding, "ISO-8859-")) {
1486 pspell_encoding = g_strdup_printf("iso8859%s", encoding+8);
1489 if (!strcmp2(encoding, "US-ASCII"))
1490 pspell_encoding = g_strdup("iso8859-1");
1492 pspell_encoding = g_strdup(encoding);
1493 return pspell_encoding;
1497 static void allocate_color(GtkPspell *gtkpspell)
1501 /* Color allocation */
1502 gc = gtk_widget_get_colormap(GTK_WIDGET(gtkpspell->gtktext));
1504 if (gtkpspell->highlight.pixel)
1505 gdk_colormap_free_colors(gc, &(gtkpspell->highlight), 1);
1507 gtkut_convert_int_to_gdk_color(prefs_common.misspelled_col,
1508 &(gtkpspell->highlight));
1510 /* add an entry for the highlight in the color map. */
1511 gdk_colormap_alloc_color(gc, &(gtkpspell->highlight), FALSE, TRUE);