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>
62 /* size of the text buffer used in various word-processing routines. */
65 /* number of suggestions to display on each menu. */
68 /******************************************************************************/
70 /* Function called from menus */
72 static void add_word_to_session (GtkWidget *w, GtkPspell *d);
73 static void add_word_to_personal (GtkWidget *w, GtkPspell *d);
74 static void set_sug_mode (GtkWidget *w, GtkPspell *gtkpspell);
75 static void set_learn_mode (GtkWidget *w, GtkPspell *gtkpspell);
76 static void check_all (GtkWidget *w, GtkPspell *gtkpspell);
77 static void menu_change_dict (GtkWidget *w, GtkPspell *gtkpspell);
78 static void entry_insert_cb (GtkXText *gtktext, gchar *newtext,
79 guint len, guint *ppos,
80 GtkPspell *gtkpspell);
81 static gint compare_dict (Dictionary *a, Dictionary *b);
82 guchar *convert_to_pspell_encoding (const guchar *encoding);
85 /* gtkspellconfig - only one config per session */
86 GtkPspellConfig * gtkpspellconfig;
88 /* TODO: configurable */
89 static GdkColor highlight = { 0, 255 * 256, 0, 0 };
91 /******************************************************************************/
93 /* gtkspell_init() - run the first pspell_config from which every
94 * new config is cloned
96 GtkPspellConfig * gtkpspell_init()
98 return new_pspell_config();
101 /* gtkspell_finished() - Finish all. No more spelling. Called when the
104 void gtkpspell_finished(GtkPspellConfig *gtkpspellconfig)
106 if (gtkpspellconfig) {
107 delete_pspell_config(gtkpspellconfig);
108 gtkpspellconfig = NULL;
112 /* gtkspell_running - Test if there is a manager running
114 int gtkpspell_running(GtkPspell * gtkpspell)
116 return (gtkpspell->config!=NULL);
119 /* gtkspell_new() - creates a new session if a gtkpspellconfig exists. The
120 * settings are defaults. If no path/dict is set afterwards, the default
123 GtkPspell * gtkpspell_new(GtkPspellConfig *gtkpspellconfig)
125 GtkPspell *gtkpspell;
127 if (gtkpspellconfig == NULL) {
128 gtkpspellconfig = gtkpspell_init();
129 if (gtkpspellconfig == NULL) {
130 debug_print(_("Pspell could not be started."));
131 prefs_common.enable_pspell=FALSE;
136 gtkpspell = g_new(GtkPspell ,1);
137 gtkpspell->config = gtkpspellconfig;
138 gtkpspell->possible_err = new_pspell_manager(gtkpspell->config);
139 gtkpspell->checker = NULL;
141 if (pspell_error_number(gtkpspell->possible_err) != 0) {
142 debug_print(_("Pspell error : %s\n"), pspell_error_message(gtkpspell->possible_err));
143 delete_pspell_can_have_error( gtkpspell->possible_err );
146 gtkpspell->checker = to_pspell_manager(gtkpspell->possible_err);
149 gtkpspell->dictionary_list = NULL;
150 gtkpspell->path = NULL;
151 gtkpspell->dict = NULL;
152 gtkpspell->mode = PSPELL_FASTMODE;
153 gtkpspell->learn = TRUE;
154 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 = PSPELL_FASTMODE;
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: %s\n"), 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: %s\n"), pspell_config_error_message(gtkpspell->config));
198 debug_print(_("Pspell encoding: setting error. Switching to iso8859-1 (sorry)\n"));
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 );
218 /* gtkspell_delete() - Finishes a session
220 GtkPspell *gtkpspell_delete( GtkPspell *gtkpspell )
222 if ( gtkpspell->checker ) {
223 /* First save all word lists */
224 pspell_manager_save_all_word_lists(gtkpspell->checker);
225 delete_pspell_manager(gtkpspell->checker);
226 gtkpspell->checker = NULL;
227 gtkpspell->possible_err = NULL; /* checker is a cast from possible_err */
230 if (gtkpspell->dictionary_list)
231 gtkpspell_free_dictionary_list(gtkpspell->dictionary_list);
233 g_free(gtkpspell->path);
234 g_free(gtkpspell->dict);
235 gtkpspell->path = NULL;
236 gtkpspell->dict = NULL;
242 int set_path_and_dict(GtkPspell *gtkpspell, PspellConfig *config,
243 guchar *path, guchar * dict)
245 guchar *module = NULL;
246 guchar *language = NULL;
247 guchar *spelling = NULL;
248 guchar *jargon = NULL;
254 /* Change nothing if any of path/dict/config is NULL */
255 g_return_val_if_fail(path, 0);
256 g_return_val_if_fail(dict, 0);
257 g_return_val_if_fail(config, 0);
259 /* This is done, so we can free gtkpspell->path, even if it was
260 * given as an argument of the function */
261 temppath = g_strdup(path);
262 g_free(gtkpspell->path);
264 /* pspell dict name format : */
265 /* <lang>[[-<spelling>[-<jargon>]]-<module>.pwli */
268 if (!strrchr(dict,G_DIR_SEPARATOR)) {
269 /* plain dict name */
270 strncpy(buf,dict,BUFSIZE-1);
274 strncpy(buf, strrchr(dict, G_DIR_SEPARATOR)+1, BUFSIZE-1);
277 g_free(gtkpspell->dict);
279 /* Ensure no buffers overflows if the dict is to long */
280 buf[BUFSIZE-1] = 0x00;
283 if ((module = strrchr(buf, '-')) != NULL) {
285 if ((end = strrchr(module, '.')) != NULL)
289 /* In tempdict, we have only the dict name, without path nor
290 extension. Useful in the popup menus */
292 tempdict = g_strdup(buf);
294 /* Probably I am too paranoied... */
295 if (!(language[0] != 0x00 && language[1] != 0x00))
298 spelling = strchr(language, '-');
299 if (spelling != NULL) {
303 if (spelling != module) {
304 if ((end = strchr(spelling, '-')) != NULL) {
307 if (jargon != module)
308 if ((end = strchr(jargon, '-')) != NULL)
322 debug_print(_("Language : %s\nSpelling: %s\nJargon: %s\nModule: %s\n"),
323 language, spelling, jargon, module);
326 pspell_config_replace(config, "language-tag", language);
328 pspell_config_replace(config, "spelling", spelling);
330 pspell_config_replace(config, "jargon", jargon);
332 pspell_config_replace(config, "module", module);
334 pspell_config_replace(config, "word-list-path", temppath);
336 switch(gtkpspell->mode) {
337 case PSPELL_FASTMODE:
338 pspell_config_replace(config, "sug_mode", "fast");
340 case PSPELL_NORMALMODE:
341 pspell_config_replace(config, "sug_mode", "normal");
343 case PSPELL_BADSPELLERMODE:
344 pspell_config_replace(config, "sug_mode", "bad-spellers");
348 gtkpspell->path = g_strdup(temppath);
349 gtkpspell->dict = g_strdup(tempdict);
357 /* gtkpspell_set_path_and_dict() - Set path and dict. The session is
359 * FALSE on error, TRUE on success */
360 int gtkpspell_set_path_and_dict(GtkPspell * gtkpspell, guchar * path,
363 PspellConfig * config2;
365 /* It seems changing an already running config is not the way to go
368 config2 = pspell_config_clone(gtkpspell->config);
370 if (gtkpspell->checker) {
371 pspell_manager_save_all_word_lists(gtkpspell->checker);
372 delete_pspell_manager(gtkpspell->checker);
375 gtkpspell->checker = NULL;
376 gtkpspell->possible_err = NULL;
378 if (set_path_and_dict(gtkpspell,config2,path,dict) == 0) {
379 debug_print(_("Pspell set_path_and_dict error."));
383 gtkpspell->possible_err = new_pspell_manager(config2);
385 delete_pspell_config(config2);
388 if (pspell_error_number(gtkpspell->possible_err) != 0) {
389 debug_print(_("Pspell path & dict. error %s\n"),
390 pspell_error_message(gtkpspell->possible_err));
391 delete_pspell_can_have_error(gtkpspell->possible_err);
392 gtkpspell->possible_err = NULL;
396 gtkpspell->checker=to_pspell_manager(gtkpspell->possible_err);
401 /* gtkpspell_get_dict() - What dict are we using ? language-spelling-jargon-module format */
402 /* Actually, this function is not used and hence not tested. */
403 /* Returns an allocated string */
404 guchar *gtkpspell_get_dict(GtkPspell *gtkpspell)
412 g_return_val_if_fail(gtkpspell->config, NULL);
414 language = g_strdup(pspell_config_retrieve(gtkpspell->config, "language"));
415 spelling = g_strdup(pspell_config_retrieve(gtkpspell->config, "spelling"));
416 jargon = g_strdup(pspell_config_retrieve(gtkpspell->config, "jargon" ));
417 len = strlen(language) + strlen(spelling) + strlen(jargon);
420 dict = g_new(char,len + 4);
421 strcpy(dict, language);
424 strcat(dict, spelling);
438 /* gtkpspell_get_path() - Return the dict path as an allocated string */
439 /* Not used = not tested */
440 guchar *gtkpspell_get_path(GtkPspell *gtkpspell)
445 g_return_val_if_fail(gtkpspell->config, NULL);
447 path = g_strdup(pspell_config_retrieve(gtkpspell->config,"word-list-path"));
452 /* menu_change_dict() - Menu callback : change dict */
453 static void menu_change_dict(GtkWidget *w, GtkPspell *gtkpspell)
458 /* Dict is simply the menu label */
460 gtk_label_get(GTK_LABEL(GTK_BIN(w)->child), (gchar **) &thelabel);
461 thedict = g_strdup(thelabel);
463 /* Set path, dict, (and sug_mode ?) */
464 gtkpspell_set_path_and_dict(gtkpspell, gtkpspell->path, thedict);
468 /* set_sug_mode() - Menu callback : Set the suggestion mode */
469 static void set_sug_mode(GtkWidget *w, GtkPspell *gtkpspell)
471 unsigned char *themode;
473 gtk_label_get(GTK_LABEL(GTK_BIN(w)->child), (gchar **) &themode);
475 if (!strcmp(themode, _("Fast Mode"))) {
476 gtkpspell_set_sug_mode(gtkpspell, "fast");
477 gtkpspell->mode = PSPELL_FASTMODE;
479 if (!strcmp(themode,_("Normal Mode"))) {
480 gtkpspell_set_sug_mode(gtkpspell, "normal");
481 gtkpspell->mode = PSPELL_NORMALMODE;
483 if (!strcmp( themode,_("Bad Spellers Mode"))) {
484 gtkpspell_set_sug_mode(gtkpspell, "bad-spellers");
485 gtkpspell->mode = PSPELL_BADSPELLERMODE;
489 /* gtkpspell_set_sug_mode() - Set the suggestion mode */
490 /* Actually, the session is resetted and everything is reset. */
491 /* We take the used path/dict pair and create a new config with them */
492 int gtkpspell_set_sug_mode(GtkPspell *gtkpspell, gchar *themode)
494 PspellConfig *config2;
498 pspell_manager_save_all_word_lists(gtkpspell->checker);
499 delete_pspell_manager(gtkpspell->checker);
501 gtkpspell->checker = NULL;
503 config2 = pspell_config_clone(gtkpspell->config);
505 if (!set_path_and_dict(gtkpspell, config2, gtkpspell->path, gtkpspell->dict)) {
506 debug_print(_("Pspell set_sug_mod could not reset path & dict\n"));
510 pspell_config_replace(config2, "sug-mode", themode);
512 gtkpspell->possible_err = new_pspell_manager(config2);
513 delete_pspell_config(config2);
516 if (pspell_error_number(gtkpspell->possible_err) != 0) {
517 debug_print(_("Pspell set sug-mode error %s\n"),
518 pspell_error_message(gtkpspell->possible_err));
519 delete_pspell_can_have_error(gtkpspell->possible_err);
520 gtkpspell->possible_err = NULL;
523 gtkpspell->checker = to_pspell_manager(gtkpspell->possible_err);
527 /* set_learn_mode() - menu callback to toggle learn mode */
528 static void set_learn_mode (GtkWidget *w, GtkPspell *gtkpspell)
530 gtkpspell->learn = gtkpspell->learn == FALSE ;
533 /* misspelled_suggest() - Create a suggestion list for word */
534 static GList *misspelled_suggest(GtkPspell *gtkpspell, guchar *word)
536 const guchar *newword;
539 const PspellWordList *suggestions;
540 PspellStringEmulation *elements;
542 g_return_val_if_fail(word, NULL);
544 if (!pspell_manager_check(gtkpspell->checker, word, -1)) {
545 suggestions = pspell_manager_suggest(gtkpspell->checker, (const char *)word, -1);
546 elements = pspell_word_list_elements(suggestions);
547 /* First one must be the misspelled (why this ?) */
548 list = g_list_append(list, g_strdup(word));
550 while ((newword = pspell_string_emulation_next(elements)) != NULL)
551 list = g_list_append(list, g_strdup(newword));
557 /* misspelled_test() - Just test if word is correctly spelled */
558 static int misspelled_test(GtkPspell *gtkpspell, unsigned char *word)
560 return pspell_manager_check(gtkpspell->checker, word, -1) ? 0 : 1;
564 static gboolean iswordsep(unsigned char c)
566 return !isalpha(c) && c != '\'';
569 static guchar get_text_index_whar(GtkPspell *gtkpspell, int pos)
574 text = gtk_editable_get_chars(GTK_EDITABLE(gtkpspell->gtktext), pos, pos + 1);
582 /* get_word_from_pos () - return the word pointed to. */
583 /* Handles correctly the quotes. */
584 static gboolean get_word_from_pos(GtkPspell *gtkpspell, int pos,
586 int *pstart, int *pend)
589 /* TODO : when correcting a word into quotes, change the color of */
590 /* the quotes too, as may be they were highlighted before. To do */
591 /* so, we can use two others pointers that points to the whole */
592 /* word including quotes. */
599 gtktext = gtkpspell->gtktext;
600 if (iswordsep(get_text_index_whar(gtkpspell, pos)))
603 /* The apostrophe character is somtimes used for quotes
604 * So include it in the word only if it is not surrounded
605 * by other characters.
608 for (start = pos; start >= 0; --start) {
609 c = get_text_index_whar(gtkpspell, start);
612 if (!isalpha(get_text_index_whar(gtkpspell, start - 1))) {
613 /* start_quote = TRUE; */
618 /* start_quote = TRUE; */
628 for (end = pos; end < gtk_xtext_get_length(gtktext); end++) {
629 c = get_text_index_whar(gtkpspell, end);
631 if (end < gtk_xtext_get_length(gtktext)) {
632 if (!isalpha(get_text_index_whar(gtkpspell, end + 1))) {
633 /* end_quote = TRUE; */
638 /* end_quote = TRUE; */
648 for (pos = start; pos < end; pos++)
649 buf[pos - start] = get_text_index_whar(gtkpspell, pos);
650 buf[pos - start] = 0;
661 static gboolean get_curword(GtkPspell *gtkpspell, unsigned char* buf,
662 int *pstart, int *pend)
664 int pos = gtk_editable_get_position(GTK_EDITABLE(gtkpspell->gtktext));
665 return get_word_from_pos(gtkpspell, pos, buf, pstart, pend);
668 static void change_color(GtkPspell * gtkpspell,
675 g_return_if_fail(start < end);
677 gtktext = gtkpspell->gtktext;
679 gtk_xtext_freeze(gtktext);
680 newtext = gtk_editable_get_chars(GTK_EDITABLE(gtktext), start, end);
682 gtk_signal_handler_block_by_func(GTK_OBJECT(gtktext),
683 GTK_SIGNAL_FUNC(entry_insert_cb),
685 gtk_xtext_set_point(gtktext, start);
686 gtk_xtext_forward_delete(gtktext, end - start);
688 gtk_xtext_insert(gtktext, NULL, color, NULL, newtext, end - start);
689 gtk_signal_handler_unblock_by_func(GTK_OBJECT(gtktext),
690 GTK_SIGNAL_FUNC(entry_insert_cb),
693 gtk_xtext_thaw(gtktext);
696 static gboolean check_at(GtkPspell *gtkpspell, int from_pos)
699 unsigned char buf[BUFSIZE];
702 g_return_val_if_fail(from_pos >= 0, FALSE);
704 gtktext = gtkpspell->gtktext;
706 if (!get_word_from_pos(gtkpspell, from_pos, buf, &start, &end))
709 strncpy(gtkpspell->theword, buf, BUFSIZE - 1);
710 gtkpspell->theword[BUFSIZE - 1] = 0;
712 if (misspelled_test(gtkpspell, buf)) {
713 if (highlight.pixel == 0) {
714 /* add an entry for the highlight in the color map. */
715 GdkColormap *gc = gtk_widget_get_colormap(GTK_WIDGET(gtktext));
716 gdk_colormap_alloc_color(gc, &highlight, FALSE, TRUE);
718 change_color(gtkpspell, start, end, &highlight);
721 change_color(gtkpspell, start, end,
722 &(GTK_WIDGET(gtktext)->style->fg[0]));
728 static void check_all(GtkWidget *w, GtkPspell *gtkpspell)
730 gtkpspell_check_all(gtkpspell);
734 void gtkpspell_check_all(GtkPspell *gtkpspell)
743 if (!gtkpspell_running(gtkpspell)) return ;
744 gtktext = gtkpspell->gtktext;
746 len = gtk_xtext_get_length(gtktext);
748 adj_value = gtktext->vadj->value;
749 gtk_xtext_freeze(gtktext);
750 origpos = gtk_editable_get_position(GTK_EDITABLE(gtktext));
751 gtk_editable_set_position(GTK_EDITABLE(gtktext),0);
753 while (pos < len && iswordsep(get_text_index_whar(gtkpspell, pos)))
755 while (pos < len && !iswordsep(get_text_index_whar(gtkpspell, pos)))
758 check_at(gtkpspell, pos - 1);
760 gtk_xtext_thaw(gtktext);
761 gtk_editable_set_position(GTK_EDITABLE(gtktext), origpos);
762 gtk_editable_select_region(GTK_EDITABLE(gtktext), origpos, origpos);
765 static void entry_insert_cb(GtkXText *gtktext, gchar *newtext,
766 guint len, guint *ppos,
767 GtkPspell * gtkpspell)
770 if (!gtkpspell_running(gtkpspell))
773 /* We must insert ourself the character to impose the */
774 /* color of the inserted character to be default */
775 /* Never mess with set_insertion when frozen */
776 gtk_xtext_freeze(gtktext);
777 gtk_xtext_backward_delete(GTK_XTEXT(gtktext), len);
778 gtk_xtext_insert(GTK_XTEXT(gtktext), NULL,
779 &(GTK_WIDGET(gtktext)->style->fg[0]), NULL, newtext, len);
780 *ppos = gtk_xtext_get_point(GTK_XTEXT(gtktext));
782 if (iswordsep(newtext[0])) {
783 /* did we just end a word? */
785 check_at(gtkpspell, *ppos - 2);
787 /* did we just split a word? */
788 if (*ppos < gtk_xtext_get_length(gtktext))
789 check_at(gtkpspell, *ppos + 1);
791 /* check as they type, *except* if they're typing at the end (the most
794 if (*ppos < gtk_xtext_get_length(gtktext)
795 && !iswordsep(get_text_index_whar(gtkpspell, *ppos)))
796 check_at(gtkpspell, *ppos - 1);
798 gtk_xtext_thaw(gtktext);
799 gtk_editable_set_position(GTK_EDITABLE(gtktext), *ppos);
802 static void entry_delete_cb(GtkXText *gtktext, gint start, gint end,
803 GtkPspell *gtkpspell)
807 if (!gtkpspell_running(gtkpspell))
810 origpos = gtk_editable_get_position(GTK_EDITABLE(gtktext));
812 check_at(gtkpspell, start - 1);
813 gtk_editable_set_position(GTK_EDITABLE(gtktext), origpos);
814 gtk_editable_select_region(GTK_EDITABLE(gtktext), origpos, origpos);
815 /* this is to *UNDO* the selection, in case they were holding shift
816 * while hitting backspace. */
819 static void replace_word(GtkWidget *w, GtkPspell *gtkpspell)
821 int start, end,oldlen, newlen;
823 unsigned char *newword;
824 unsigned char buf[BUFSIZE];
828 gtktext = gtkpspell->gtktext;
830 gtk_xtext_freeze(GTK_XTEXT(gtktext));
831 origpos = gtkpspell->orig_pos;
834 gtk_label_get(GTK_LABEL(GTK_BIN(w)->child), (gchar**) &newword);
835 newlen = strlen(newword);
837 get_curword(gtkpspell, buf, &start, &end);
838 oldlen = end - start;
840 gtk_xtext_set_point(GTK_XTEXT(gtktext), end);
841 gtk_xtext_backward_delete(GTK_XTEXT(gtktext), end - start);
842 gtk_xtext_insert(GTK_XTEXT(gtktext), NULL, NULL, NULL, newword, strlen(newword));
844 if (end-start > 0 && gtkpspell->learn) {
845 /* Just be sure the buffer ends somewhere... */
847 /* Learn from common misspellings */
848 pspell_manager_store_replacement(gtkpspell->checker, buf,
853 /* Put the point and the position where we clicked with the mouse */
854 /* It seems to be a hack, as I must thaw,freeze,thaw the widget */
855 /* to let it update correctly the word insertion and then the */
856 /* point & position position. If not, SEGV after the first replacement */
857 /* If the new word ends before point, put the point at its end*/
859 if (origpos-start <= oldlen && origpos-start >= 0) {
860 /* Original point was in the word. */
861 /* Put the insertion point in the same location */
862 /* with respect to the new length */
863 /* If the original position is still within the word, */
864 /* then keep the original position. If not, move to the */
865 /* end of the word */
866 if (origpos-start > newlen)
867 pos = start + newlen;
869 else if (origpos > end) {
870 /* move the position according to the change of length */
871 pos = origpos + newlen - oldlen;
873 gtk_xtext_thaw(GTK_XTEXT(gtktext));
874 gtk_xtext_freeze(GTK_XTEXT(gtktext));
875 if (GTK_XTEXT(gtktext)->text_len < pos)
876 pos = gtk_xtext_get_length(GTK_XTEXT(gtktext));
877 gtkpspell->orig_pos = pos;
878 gtk_editable_set_position(GTK_EDITABLE(gtktext), gtkpspell->orig_pos);
879 gtk_xtext_set_point(GTK_XTEXT(gtktext),
880 gtk_editable_get_position(GTK_EDITABLE(gtktext)));
881 gtk_xtext_thaw(GTK_XTEXT(gtktext));
884 /* Accept this word for this session */
886 static void add_word_to_session(GtkWidget *w, GtkPspell *gtkpspell)
891 gtkxtext = gtkpspell->gtktext;
892 gtk_xtext_freeze(GTK_XTEXT(gtkxtext));
893 pos = gtk_editable_get_position(GTK_EDITABLE(gtkxtext));
895 pspell_manager_add_to_session(gtkpspell->checker,gtkpspell->theword, strlen(gtkpspell->theword));
897 check_at(gtkpspell,gtk_editable_get_position(GTK_EDITABLE(gtkxtext)));
899 gtk_xtext_thaw(GTK_XTEXT(gtkxtext));
900 gtk_xtext_freeze(GTK_XTEXT(gtkxtext));
901 gtk_editable_set_position(GTK_EDITABLE(gtkxtext),pos);
902 gtk_xtext_set_point(GTK_XTEXT(gtkxtext), gtk_editable_get_position(GTK_EDITABLE(gtkxtext)));
903 gtk_xtext_thaw(GTK_XTEXT(gtkxtext));
906 /* add_word_to_personal() - add word to personal dict. */
908 static void add_word_to_personal(GtkWidget *w, GtkPspell *gtkpspell)
913 gtkxtext = gtkpspell->gtktext;
915 gtk_xtext_freeze(GTK_XTEXT(gtkxtext));
916 pos = gtk_editable_get_position(GTK_EDITABLE(gtkxtext));
918 pspell_manager_add_to_personal(gtkpspell->checker,gtkpspell->theword,strlen(gtkpspell->theword));
920 check_at(gtkpspell,gtk_editable_get_position(GTK_EDITABLE(gtkxtext)));
921 gtk_xtext_thaw(GTK_XTEXT(gtkxtext));
922 gtk_xtext_freeze(GTK_XTEXT(gtkxtext));
923 gtk_editable_set_position(GTK_EDITABLE(gtkxtext),pos);
924 gtk_xtext_set_point(GTK_XTEXT(gtkxtext), gtk_editable_get_position(GTK_EDITABLE(gtkxtext)));
925 gtk_xtext_thaw(GTK_XTEXT(gtkxtext));
929 static GtkMenu *make_menu_config(GtkPspell *gtkpspell)
931 GtkWidget *menu, *item, *submenu;
934 menu = gtk_menu_new();
936 item = gtk_menu_item_new_with_label(_("Spell check all"));
937 gtk_widget_show(item);
938 gtk_menu_append(GTK_MENU(menu), item);
939 gtk_signal_connect(GTK_OBJECT(item),"activate",
940 GTK_SIGNAL_FUNC(check_all),
944 item = gtk_menu_item_new();
945 gtk_widget_show(item);
946 gtk_menu_append(GTK_MENU(menu),item);
948 submenu = gtk_menu_new();
949 item = gtk_menu_item_new_with_label(_("Change dictionary"));
950 gtk_widget_show(item);
951 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item),submenu);
952 gtk_menu_append(GTK_MENU(menu), item);
955 if (gtkpspell->dictionary_list==NULL)
956 gtkpspell->dictionary_list = gtkpspell_get_dictionary_list(gtkpspell->path);
959 GtkWidget * curmenu = submenu;
964 tmp = gtkpspell->dictionary_list;
965 for (tmp = gtkpspell->dictionary_list; tmp != NULL; tmp =g_slist_next(tmp)) {
966 dict = (Dictionary *) tmp->data;
967 item = gtk_check_menu_item_new_with_label(dict->name);
968 if (strcmp2(dict->name, gtkpspell->dict))
969 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), FALSE);
971 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), TRUE);
972 gtk_widget_set_sensitive(GTK_WIDGET(item), FALSE);
974 gtk_menu_append(GTK_MENU(curmenu), item);
975 gtk_signal_connect(GTK_OBJECT(item), "activate",
976 GTK_SIGNAL_FUNC(menu_change_dict),
978 gtk_widget_show(item);
980 if (count == MENUCOUNT) {
982 newmenu = gtk_menu_new();
983 item = gtk_menu_item_new_with_label(_("More..."));
984 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), newmenu);
985 gtk_menu_append(GTK_MENU(curmenu), item);
986 gtk_widget_show(item);
993 item = gtk_menu_item_new();
994 gtk_widget_show(item);
995 gtk_menu_append(GTK_MENU(menu), item);
997 item = gtk_check_menu_item_new_with_label(_("Fast Mode"));
998 gtk_widget_show(item);
999 gtk_menu_append(GTK_MENU(menu), item);
1000 if (gtkpspell->mode == PSPELL_FASTMODE) {
1001 gtk_widget_set_sensitive(GTK_WIDGET(item),FALSE);
1002 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item),TRUE);
1005 gtk_signal_connect(GTK_OBJECT(item), "activate",
1006 GTK_SIGNAL_FUNC(set_sug_mode),
1009 item = gtk_check_menu_item_new_with_label(_("Normal Mode"));
1010 gtk_widget_show(item);
1011 gtk_menu_append(GTK_MENU(menu),item);
1012 if (gtkpspell->mode == PSPELL_NORMALMODE) {
1013 gtk_widget_set_sensitive(GTK_WIDGET(item), FALSE);
1014 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), TRUE);
1017 gtk_signal_connect(GTK_OBJECT(item), "activate",
1018 GTK_SIGNAL_FUNC(set_sug_mode),
1021 item = gtk_check_menu_item_new_with_label(_("Bad Spellers Mode"));
1022 gtk_widget_show(item);
1023 gtk_menu_append(GTK_MENU(menu), item);
1024 if (gtkpspell->mode==PSPELL_BADSPELLERMODE) {
1025 gtk_widget_set_sensitive(GTK_WIDGET(item), FALSE);
1026 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), TRUE);
1029 gtk_signal_connect(GTK_OBJECT(item), "activate",
1030 GTK_SIGNAL_FUNC(set_sug_mode),
1032 item = gtk_menu_item_new();
1033 gtk_widget_show(item);
1034 gtk_menu_append(GTK_MENU(menu), item);
1036 item = gtk_check_menu_item_new_with_label(_("Learn from mistakes"));
1037 gtk_widget_show(item);
1038 gtk_menu_append(GTK_MENU(menu), item);
1039 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item),
1041 gtk_signal_connect(GTK_OBJECT(item), "activate",
1042 GTK_SIGNAL_FUNC(set_learn_mode), gtkpspell);
1045 return GTK_MENU(menu);
1048 /* make_menu() - Add menus to accept this word for this session and to add it to
1049 * personal dictionary */
1050 static GtkMenu *make_menu(GList *l, GtkPspell *gtkpspell)
1052 GtkWidget *menu, *item;
1053 unsigned char *caption;
1056 gtktext = gtkpspell->gtktext;
1058 menu = gtk_menu_new();
1059 caption = g_strdup_printf(_("Accept `%s' for this session"), (unsigned char*)l->data);
1060 item = gtk_menu_item_new_with_label(caption);
1062 gtk_widget_show(item);
1063 gtk_menu_append(GTK_MENU(menu), item);
1065 gtk_signal_connect(GTK_OBJECT(item), "activate",
1066 GTK_SIGNAL_FUNC(add_word_to_session),
1069 caption = g_strdup_printf(_("Add `%s' to personal dictionary"), (char*)l->data);
1070 item = gtk_menu_item_new_with_label(caption);
1072 gtk_widget_show(item);
1073 gtk_menu_append(GTK_MENU(menu), item);
1075 gtk_signal_connect(GTK_OBJECT(item), "activate",
1076 GTK_SIGNAL_FUNC(add_word_to_personal),
1079 item = gtk_menu_item_new();
1080 gtk_widget_show(item);
1081 gtk_menu_append(GTK_MENU(menu), item);
1085 item = gtk_menu_item_new_with_label(_("(no suggestions)"));
1086 gtk_widget_show(item);
1087 gtk_menu_append(GTK_MENU(menu), item);
1089 GtkWidget *curmenu = menu;
1093 if (l->data == NULL && l->next != NULL) {
1095 curmenu = gtk_menu_new();
1096 item = gtk_menu_item_new_with_label(_("Others..."));
1097 gtk_widget_show(item);
1098 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), curmenu);
1099 gtk_menu_append(GTK_MENU(curmenu), item);
1101 } else if (count > MENUCOUNT) {
1103 item = gtk_menu_item_new_with_label(_("More..."));
1104 gtk_widget_show(item);
1105 gtk_menu_append(GTK_MENU(curmenu), item);
1106 curmenu = gtk_menu_new();
1107 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), curmenu);
1109 item = gtk_menu_item_new_with_label((unsigned char*)l->data);
1110 gtk_signal_connect(GTK_OBJECT(item), "activate",
1111 GTK_SIGNAL_FUNC(replace_word), gtkpspell);
1112 gtk_widget_show(item);
1113 gtk_menu_append(GTK_MENU(curmenu), item);
1115 } while ((l = l->next) != NULL);
1117 return GTK_MENU(menu);
1120 static void popup_menu(GtkPspell *gtkpspell, GdkEventButton *eb)
1122 unsigned char buf[BUFSIZE];
1126 gtktext = gtkpspell->gtktext;
1127 gtkpspell->orig_pos = gtk_editable_get_position(GTK_EDITABLE(gtktext));
1129 if (!(eb->state & GDK_SHIFT_MASK))
1130 if (get_curword(gtkpspell, buf, NULL, NULL)) {
1132 strncpy(gtkpspell->theword, buf, BUFSIZE - 1);
1133 gtkpspell->theword[BUFSIZE - 1] = 0;
1134 list = misspelled_suggest(gtkpspell, buf);
1136 gtk_menu_popup(make_menu(list, gtkpspell), NULL, NULL, NULL, NULL,
1137 eb->button, eb->time);
1138 for (l = list; l != NULL; l = l->next)
1145 gtk_menu_popup(make_menu_config(gtkpspell),NULL,NULL,NULL,NULL,
1146 eb->button,eb->time);
1149 /* ok, this is pretty wacky:
1150 * we need to let the right-mouse-click go through, so it moves the cursor,
1151 * but we *can't* let it go through, because GtkText interprets rightclicks as
1152 * weird selection modifiers.
1154 * so what do we do? forge rightclicks as leftclicks, then popup the menu.
1157 static gint button_press_intercept_cb(GtkXText *gtktext, GdkEvent *e, GtkPspell *gtkpspell)
1162 if (!gtkpspell_running(gtkpspell))
1165 if (e->type != GDK_BUTTON_PRESS)
1167 eb = (GdkEventButton*) e;
1169 if (eb->button != 3)
1172 /* forge the leftclick */
1175 gtk_signal_handler_block_by_func(GTK_OBJECT(gtktext),
1176 GTK_SIGNAL_FUNC(button_press_intercept_cb),
1178 gtk_signal_emit_by_name(GTK_OBJECT(gtktext), "button-press-event",
1180 gtk_signal_handler_unblock_by_func(GTK_OBJECT(gtktext),
1181 GTK_SIGNAL_FUNC(button_press_intercept_cb),
1183 gtk_signal_emit_stop_by_name(GTK_OBJECT(gtktext), "button-press-event");
1185 /* now do the menu wackiness */
1186 popup_menu(gtkpspell, eb);
1187 gtk_grab_remove(GTK_WIDGET(gtktext));
1191 void gtkpspell_uncheck_all(GtkPspell * gtkpspell)
1194 unsigned char *text;
1198 gtktext=gtkpspell->gtktext;
1200 adj_value = gtktext->vadj->value;
1201 gtk_xtext_freeze(gtktext);
1202 origpos = gtk_editable_get_position(GTK_EDITABLE(gtktext));
1203 text = gtk_editable_get_chars(GTK_EDITABLE(gtktext), 0, -1);
1204 gtk_xtext_set_point(gtktext, 0);
1205 gtk_xtext_forward_delete(gtktext, gtk_xtext_get_length(gtktext));
1206 gtk_xtext_insert(gtktext, NULL, NULL, NULL, text, strlen(text));
1207 gtk_xtext_thaw(gtktext);
1209 gtk_editable_set_position(GTK_EDITABLE(gtktext), origpos);
1210 gtk_adjustment_set_value(gtktext->vadj, adj_value);
1213 void gtkpspell_attach(GtkPspell *gtkpspell, GtkXText *gtktext)
1215 gtkpspell->gtktext=gtktext;
1216 gtk_signal_connect_after(GTK_OBJECT(gtktext), "insert-text",
1217 GTK_SIGNAL_FUNC(entry_insert_cb), gtkpspell);
1218 gtk_signal_connect_after(GTK_OBJECT(gtktext), "delete-text",
1219 GTK_SIGNAL_FUNC(entry_delete_cb), gtkpspell);
1220 gtk_signal_connect(GTK_OBJECT(gtktext), "button-press-event",
1221 GTK_SIGNAL_FUNC(button_press_intercept_cb), gtkpspell);
1225 void gtkpspell_detach(GtkPspell * gtkpspell)
1229 gtktext =gtkpspell->gtktext;
1230 /* if (prefs_common.auto_makepspell) { */
1231 gtk_signal_disconnect_by_func(GTK_OBJECT(gtktext),
1232 GTK_SIGNAL_FUNC(entry_insert_cb), gtkpspell);
1233 gtk_signal_disconnect_by_func(GTK_OBJECT(gtktext),
1234 GTK_SIGNAL_FUNC(entry_delete_cb), gtkpspell);
1236 gtk_signal_disconnect_by_func(GTK_OBJECT(gtktext),
1237 GTK_SIGNAL_FUNC(button_press_intercept_cb), gtkpspell);
1239 gtkpspell_uncheck_all(gtkpspell);
1242 /*** Sylpheed (Claws) ***/
1244 static GSList *create_empty_dictionary_list(void)
1246 GSList *list = NULL;
1249 dict = g_new0(Dictionary, 1);
1250 dict->name = g_strdup(_("None"));
1251 return g_slist_append(list, dict);
1254 /* gtkpspell_get_dictionary_list() - returns list of dictionary names */
1255 GSList *gtkpspell_get_dictionary_list(const gchar *pspell_path)
1258 gchar *dict_path, *tmp, *prevdir;
1267 #warning TODO: no directory change
1269 dict_path=g_strdup(pspell_path);
1270 prevdir = g_get_current_dir();
1271 if (chdir(dict_path) <0) {
1272 FILE_OP_ERROR(dict_path, "chdir");
1275 return create_empty_dictionary_list();
1278 debug_print(_("Checking for dictionaries in %s\n"), dict_path);
1280 if (NULL != (dir = opendir("."))) {
1281 while (NULL != (ent = readdir(dir))) {
1282 /* search for pwli */
1283 if (NULL != (tmp = strstr(ent->d_name, ".pwli"))) {
1284 dict = g_new0(Dictionary, 1);
1285 dict->name = g_strndup(ent->d_name, tmp - ent->d_name);
1286 debug_print(_("Found dictionary %s\n"), dict->name);
1287 list = g_slist_insert_sorted(list, dict, (GCompareFunc) compare_dict);
1293 FILE_OP_ERROR(dict_path, "opendir");
1294 debug_print(_("No dictionary found\n"));
1295 list = create_empty_dictionary_list();
1298 debug_print(_("No dictionary found"));
1299 list = create_empty_dictionary_list();
1307 /* compare_dict () - compare 2 dict names */
1309 static gint compare_dict(Dictionary *a, Dictionary *b)
1311 guchar *alanguage, *blanguage,
1312 *aspelling, *bspelling,
1313 *ajargon , *bjargon ,
1314 *amodule , *bmodule ;
1315 guint aparts = 0, bparts = 0, i;
1317 for (i=0; i < strlen(a->name) ; i++)
1318 if (a->name[i]=='-')
1320 for (i=0; i < strlen(b->name) ; i++)
1321 if (b->name[i]=='-')
1324 if (aparts != bparts)
1325 return (aparts < bparts) ? -1 : +1 ;
1327 return strcmp2(a->name, b->name);
1329 void gtkpspell_free_dictionary_list(GSList *list)
1333 for (walk = list; walk != NULL; walk = g_slist_next(walk))
1335 dict = (Dictionary *) walk->data;
1343 static void dictionary_option_menu_item_data_destroy(gpointer data)
1345 gchar *str = (gchar *) data;
1351 GtkWidget *gtkpspell_dictionary_option_menu_new(const gchar *pspell_path)
1353 GSList *dict_list, *tmp;
1358 dict_list = gtkpspell_get_dictionary_list(pspell_path);
1359 g_return_val_if_fail(dict_list, NULL);
1361 menu = gtk_menu_new();
1363 for (tmp = dict_list; tmp != NULL; tmp = g_slist_next(tmp)) {
1364 dict = (Dictionary *) tmp->data;
1365 item = gtk_menu_item_new_with_label(dict->name);
1367 gtk_object_set_data_full(GTK_OBJECT(item), "dict_name",
1368 g_strdup(dict->name),
1369 dictionary_option_menu_item_data_destroy);
1370 gtk_menu_append(GTK_MENU(menu), item);
1371 gtk_widget_show(item);
1374 gtk_widget_show(menu);
1376 gtkpspell_free_dictionary_list(dict_list);
1383 gchar *gtkpspell_get_dictionary_menu_active_item(GtkWidget *menu)
1385 GtkWidget *menuitem;
1388 g_return_val_if_fail(GTK_IS_MENU(menu), NULL);
1389 menuitem = gtk_menu_get_active(GTK_MENU(menu));
1390 result = gtk_object_get_data(GTK_OBJECT(menuitem), "dict_name");
1391 g_return_val_if_fail(result, NULL);
1392 return g_strdup(result);
1396 /* convert_to_pspell_encoding () - converts ISO-8859-* strings to iso8859-*
1397 * as needed by pspell. Returns an allocated string.
1400 guchar *convert_to_pspell_encoding (const guchar *encoding)
1402 guchar * pspell_encoding;
1403 /* Beware, strstr2 returns 0 if string is found -1 if not */
1404 if (!strstr2(encoding, "ISO-8859-")) {
1405 pspell_encoding = g_strdup_printf("iso8859%s", encoding+8);
1408 pspell_encoding = g_strdup(encoding);
1409 return pspell_encoding;