1 /* gtkaspell - a spell-checking addon for GtkText
2 * Copyright (c) 2000 Evan Martin (original code for ispell).
3 * Copyright (c) 2002 Melvin Hadasht.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; If not, write to the Free Software
17 * 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-2002 by Hiroyuki Yamamoto &
22 * The Sylpheed Claws Team.
23 * Adapted for pspell (c) 2001-2002 Melvin Hadasht
24 * Adapted for GNU/aspell (c) 2002 Melvin Hadasht
36 #include <sys/types.h>
48 #include <glib/gi18n.h>
52 #include <gtk/gtkoptionmenu.h>
53 #include <gtk/gtkmenu.h>
54 #include <gtk/gtkmenuitem.h>
55 #include <gdk/gdkkeysyms.h>
61 #include "alertpanel.h"
62 #include "gtkaspell.h"
63 #include "gtk/gtkutils.h"
65 #define ASPELL_FASTMODE 1
66 #define ASPELL_NORMALMODE 2
67 #define ASPELL_BADSPELLERMODE 3
69 #define GTKASPELLWORDSIZE 1024
71 /* size of the text buffer used in various word-processing routines. */
74 /* number of suggestions to display on each menu. */
77 /* 'config' must be defined as a 'AspellConfig *' */
78 #define RETURN_FALSE_IF_CONFIG_ERROR() \
80 if (aspell_config_error_number(config) != 0) { \
81 gtkaspellcheckers->error_message = g_strdup(aspell_config_error_message(config)); \
86 #define CONFIG_REPLACE_RETURN_FALSE_IF_FAIL(option, value) { \
87 aspell_config_replace(config, option, value); \
88 RETURN_FALSE_IF_CONFIG_ERROR(); \
91 typedef struct _GtkAspellCheckers {
93 GSList *dictionary_list;
97 typedef struct _Dictionary {
103 typedef struct _GtkAspeller {
104 Dictionary *dictionary;
106 AspellConfig *config;
107 AspellSpeller *checker;
110 typedef void (*ContCheckFunc) (gpointer *gtkaspell);
114 GtkAspeller *gtkaspeller;
115 GtkAspeller *alternate_speller;
116 gchar *dictionary_path;
117 gchar theword[GTKASPELLWORDSIZE];
123 gboolean check_while_typing;
124 gboolean use_alternate;
126 ContCheckFunc continue_check;
128 GtkWidget *replace_entry;
129 GtkWidget *parent_window;
131 gint default_sug_mode;
133 GList *suggestions_list;
135 GtkTextView *gtktext;
137 GtkAccelGroup *accel_group;
140 typedef AspellConfig GtkAspellConfig;
142 /******************************************************************************/
144 static GtkAspellCheckers *gtkaspellcheckers;
146 /* Error message storage */
147 static void gtkaspell_checkers_error_message (gchar *message);
150 static void entry_insert_cb (GtkTextBuffer *textbuf,
154 GtkAspell *gtkaspell);
155 static void entry_delete_cb (GtkTextBuffer *textbuf,
156 GtkTextIter *startiter,
157 GtkTextIter *enditer,
158 GtkAspell *gtkaspell);
159 /*static gint button_press_intercept_cb (GtkTextView *gtktext,
161 GtkAspell *gtkaspell);
163 static void button_press_intercept_cb(GtkTextView *gtktext,
164 GtkMenu *menu, GtkAspell *gtkaspell);
166 /* Checker creation */
167 static GtkAspeller* gtkaspeller_new (Dictionary *dict);
168 static GtkAspeller* gtkaspeller_real_new (Dictionary *dict);
169 static GtkAspeller* gtkaspeller_delete (GtkAspeller *gtkaspeller);
170 static GtkAspeller* gtkaspeller_real_delete (GtkAspeller *gtkaspeller);
172 /* Checker configuration */
173 static gint set_dictionary (AspellConfig *config,
175 static void set_sug_mode_cb (GtkMenuItem *w,
176 GtkAspell *gtkaspell);
177 static void set_real_sug_mode (GtkAspell *gtkaspell,
178 const char *themode);
180 /* Checker actions */
181 static gboolean check_at (GtkAspell *gtkaspell,
183 static gboolean check_next_prev (GtkAspell *gtkaspell,
185 static GList* misspelled_suggest (GtkAspell *gtkaspell,
187 static void add_word_to_session_cb (GtkWidget *w,
189 static void add_word_to_personal_cb (GtkWidget *w,
191 static void replace_with_create_dialog_cb (GtkWidget *w,
193 static void replace_with_supplied_word_cb (GtkWidget *w,
194 GtkAspell *gtkaspell);
195 static void replace_word_cb (GtkWidget *w,
197 static void replace_real_word (GtkAspell *gtkaspell,
199 static void check_with_alternate_cb (GtkWidget *w,
201 static void use_alternate_dict (GtkAspell *gtkaspell);
202 static void toggle_check_while_typing_cb (GtkWidget *w,
206 static GSList* make_sug_menu (GtkAspell *gtkaspell);
207 static GSList * populate_submenu (GtkAspell *gtkaspell);
208 static GSList* make_config_menu (GtkAspell *gtkaspell);
209 static void set_menu_pos (GtkMenu *menu,
214 /* Other menu callbacks */
215 static gboolean aspell_key_pressed (GtkWidget *widget,
217 GtkAspell *gtkaspell);
218 static void change_dict_cb (GtkWidget *w,
219 GtkAspell *gtkaspell);
220 static void switch_to_alternate_cb (GtkWidget *w,
223 /* Misc. helper functions */
224 static void set_point_continue (GtkAspell *gtkaspell);
225 static void continue_check (gpointer *gtkaspell);
226 static gboolean iswordsep (unsigned char c);
227 static guchar get_text_index_whar (GtkAspell *gtkaspell,
229 static gboolean get_word_from_pos (GtkAspell *gtkaspell,
235 static void allocate_color (GtkAspell *gtkaspell,
237 static void change_color (GtkAspell *gtkaspell,
242 static guchar* convert_to_aspell_encoding (const guchar *encoding);
243 static gint compare_dict (Dictionary *a,
245 static void dictionary_delete (Dictionary *dict);
246 static Dictionary * dictionary_dup (const Dictionary *dict);
247 static void free_suggestions_list (GtkAspell *gtkaspell);
248 static void reset_theword_data (GtkAspell *gtkaspell);
249 static void free_checkers (gpointer elt,
251 static gint find_gtkaspeller (gconstpointer aa,
253 /* gtkspellconfig - only one config per session */
254 GtkAspellConfig * gtkaspellconfig;
255 static void destroy_menu(GtkWidget *widget, gpointer user_data);
257 /******************************************************************************/
258 static gint get_textview_buffer_charcount(GtkTextView *view);
260 static gint get_textview_buffer_charcount(GtkTextView *view)
262 GtkTextBuffer *buffer;
264 g_return_val_if_fail(view, 0);
266 buffer = gtk_text_view_get_buffer(view);
267 g_return_val_if_fail(buffer, 0);
269 return gtk_text_buffer_get_char_count(buffer);
271 static gint get_textview_buffer_offset(GtkTextView *view)
273 GtkTextBuffer * buffer;
277 g_return_val_if_fail(view, 0);
279 buffer = gtk_text_view_get_buffer(view);
280 g_return_val_if_fail(buffer, 0);
282 mark = gtk_text_buffer_get_insert(buffer);
283 g_return_val_if_fail(mark, 0);
285 gtk_text_buffer_get_iter_at_mark(buffer, &iter, mark);
287 return gtk_text_iter_get_offset(&iter);
289 static void set_textview_buffer_offset(GtkTextView *view, gint offset)
291 GtkTextBuffer *buffer;
294 g_return_if_fail(view);
296 buffer = gtk_text_view_get_buffer(view);
297 g_return_if_fail(buffer);
299 gtk_text_buffer_get_iter_at_offset(buffer, &iter, offset);
300 gtk_text_buffer_place_cursor(buffer, &iter);
302 /******************************************************************************/
304 void gtkaspell_checkers_init(void)
306 gtkaspellcheckers = g_new(GtkAspellCheckers, 1);
307 gtkaspellcheckers->checkers = NULL;
308 gtkaspellcheckers->dictionary_list = NULL;
309 gtkaspellcheckers->error_message = NULL;
312 void gtkaspell_checkers_quit(void)
317 if (gtkaspellcheckers == NULL)
320 if ((checkers = gtkaspellcheckers->checkers)) {
321 debug_print("Aspell: number of running checkers to delete %d\n",
322 g_slist_length(checkers));
324 g_slist_foreach(checkers, free_checkers, NULL);
325 g_slist_free(checkers);
326 gtkaspellcheckers->checkers = NULL;
329 if ((dict_list = gtkaspellcheckers->dictionary_list)) {
330 debug_print("Aspell: number of dictionaries to delete %d\n",
331 g_slist_length(dict_list));
333 gtkaspell_free_dictionary_list(dict_list);
334 gtkaspellcheckers->dictionary_list = NULL;
337 g_free(gtkaspellcheckers->error_message);
338 gtkaspellcheckers->error_message = NULL;
342 static void gtkaspell_checkers_error_message (gchar *message)
345 if (gtkaspellcheckers->error_message) {
346 tmp = g_strdup_printf("%s\n%s",
347 gtkaspellcheckers->error_message,
350 g_free(gtkaspellcheckers->error_message);
351 gtkaspellcheckers->error_message = tmp;
353 gtkaspellcheckers->error_message = message;
356 const char *gtkaspell_checkers_strerror(void)
358 g_return_val_if_fail(gtkaspellcheckers, "");
359 return gtkaspellcheckers->error_message;
362 void gtkaspell_checkers_reset_error(void)
364 g_return_if_fail(gtkaspellcheckers);
366 g_free(gtkaspellcheckers->error_message);
368 gtkaspellcheckers->error_message = NULL;
371 GtkAspell *gtkaspell_new(const gchar *dictionary_path,
372 const gchar *dictionary,
373 const gchar *encoding,
374 gint misspelled_color,
375 gboolean check_while_typing,
376 gboolean use_alternate,
377 GtkTextView *gtktext,
378 GtkWindow *parent_win)
381 GtkAspell *gtkaspell;
382 GtkAspeller *gtkaspeller;
383 GtkTextBuffer *buffer;
385 g_return_val_if_fail(gtktext, NULL);
386 buffer = gtk_text_view_get_buffer(gtktext);
388 dict = g_new0(Dictionary, 1);
389 dict->fullname = g_strdup(dictionary);
390 dict->encoding = g_strdup(encoding);
392 gtkaspeller = gtkaspeller_new(dict);
393 dictionary_delete(dict);
398 gtkaspell = g_new0(GtkAspell, 1);
400 gtkaspell->dictionary_path = g_strdup(dictionary_path);
402 gtkaspell->gtkaspeller = gtkaspeller;
403 gtkaspell->alternate_speller = NULL;
404 gtkaspell->theword[0] = 0x00;
405 gtkaspell->start_pos = 0;
406 gtkaspell->end_pos = 0;
407 gtkaspell->orig_pos = -1;
408 gtkaspell->end_check_pos = -1;
409 gtkaspell->misspelled = -1;
410 gtkaspell->check_while_typing = check_while_typing;
411 gtkaspell->continue_check = NULL;
412 gtkaspell->replace_entry = NULL;
413 gtkaspell->gtktext = gtktext;
414 gtkaspell->default_sug_mode = ASPELL_FASTMODE;
415 gtkaspell->max_sug = -1;
416 gtkaspell->suggestions_list = NULL;
417 gtkaspell->use_alternate = use_alternate;
418 gtkaspell->parent_window = GTK_WIDGET(parent_win);
420 allocate_color(gtkaspell, misspelled_color);
422 g_signal_connect_after(G_OBJECT(buffer), "insert-text",
423 G_CALLBACK(entry_insert_cb), gtkaspell);
424 g_signal_connect_after(G_OBJECT(buffer), "delete-range",
425 G_CALLBACK(entry_delete_cb), gtkaspell);
426 /*g_signal_connect(G_OBJECT(gtktext), "button-press-event",
427 G_CALLBACK(button_press_intercept_cb),
429 g_signal_connect(G_OBJECT(gtktext), "populate-popup",
430 G_CALLBACK(button_press_intercept_cb), gtkaspell);
432 debug_print("Aspell: created gtkaspell %0x\n", (guint) gtkaspell);
437 void gtkaspell_delete(GtkAspell *gtkaspell)
439 GtkTextView *gtktext = gtkaspell->gtktext;
441 g_signal_handlers_disconnect_by_func(G_OBJECT(gtktext),
442 G_CALLBACK(entry_insert_cb),
444 g_signal_handlers_disconnect_by_func(G_OBJECT(gtktext),
445 G_CALLBACK(entry_delete_cb),
447 g_signal_handlers_disconnect_by_func(G_OBJECT(gtktext),
448 G_CALLBACK(button_press_intercept_cb),
451 gtkaspell_uncheck_all(gtkaspell);
453 gtkaspeller_delete(gtkaspell->gtkaspeller);
455 if (gtkaspell->use_alternate && gtkaspell->alternate_speller)
456 gtkaspeller_delete(gtkaspell->alternate_speller);
458 if (gtkaspell->suggestions_list)
459 free_suggestions_list(gtkaspell);
461 g_free((gchar *)gtkaspell->dictionary_path);
462 gtkaspell->dictionary_path = NULL;
464 debug_print("Aspell: deleting gtkaspell %0x\n", (guint) gtkaspell);
471 static void entry_insert_cb(GtkTextBuffer *textbuf,
475 GtkAspell *gtkaspell)
479 g_return_if_fail(gtkaspell->gtkaspeller->checker);
481 if (!gtkaspell->check_while_typing)
484 pos = gtk_text_iter_get_offset(iter);
486 if (iswordsep(newtext[0])) {
487 /* did we just end a word? */
489 check_at(gtkaspell, pos - 2);
491 /* did we just split a word? */
492 if (pos < gtk_text_buffer_get_char_count(textbuf))
493 check_at(gtkaspell, pos + 1);
495 /* check as they type, *except* if they're typing at the end (the most
498 if (pos < gtk_text_buffer_get_char_count(textbuf) &&
499 !iswordsep(get_text_index_whar(gtkaspell, pos))) {
500 check_at(gtkaspell, pos - 1);
505 static void entry_delete_cb(GtkTextBuffer *textbuf,
506 GtkTextIter *startiter,
507 GtkTextIter *enditer,
508 GtkAspell *gtkaspell)
513 g_return_if_fail(gtkaspell->gtkaspeller->checker);
515 if (!gtkaspell->check_while_typing)
518 start = gtk_text_iter_get_offset(startiter);
519 end = gtk_text_iter_get_offset(enditer);
520 origpos = get_textview_buffer_offset(gtkaspell->gtktext);
522 check_at(gtkaspell, start - 1);
523 check_at(gtkaspell, start);
526 set_textview_buffer_offset(gtkaspell->gtktext, origpos);
527 /* this is to *UNDO* the selection, in case they were holding shift
528 * while hitting backspace. */
529 /* needed with textview ??? */
530 /* gtk_editable_select_region(GTK_EDITABLE(gtktext), origpos, origpos); */
533 static void button_press_intercept_cb(GtkTextView *gtktext,
534 GtkMenu *menu, GtkAspell *gtkaspell)
536 GtkMenuItem *menuitem;
537 GSList *spell_menu = NULL;
539 gboolean suggest = FALSE;
541 menuitem = GTK_MENU_ITEM(gtk_separator_menu_item_new());
542 gtk_menu_shell_prepend(GTK_MENU_SHELL(menu), GTK_WIDGET(menuitem));
543 gtk_widget_show(GTK_WIDGET(menuitem));
545 gtktext = gtkaspell->gtktext;
547 gtkaspell->orig_pos = get_textview_buffer_offset(gtktext);
549 if (check_at(gtkaspell, gtkaspell->orig_pos)) {
551 set_textview_buffer_offset(gtktext, gtkaspell->orig_pos);
553 if (misspelled_suggest(gtkaspell, gtkaspell->theword)) {
554 spell_menu = make_sug_menu(gtkaspell);
558 set_textview_buffer_offset(gtktext, gtkaspell->orig_pos);
561 spell_menu = make_config_menu(gtkaspell);
563 spell_menu = g_slist_reverse(spell_menu);
564 for (items = spell_menu;
565 items; items = items->next) {
566 menuitem = GTK_MENU_ITEM(items->data);
567 gtk_menu_shell_prepend(GTK_MENU_SHELL(menu), GTK_WIDGET(menuitem));
568 gtk_widget_show(GTK_WIDGET(menuitem));
570 g_slist_free(spell_menu);
572 g_signal_connect(G_OBJECT(menu), "deactivate",
573 G_CALLBACK(destroy_menu),
576 g_signal_connect(G_OBJECT(menu),
578 G_CALLBACK(aspell_key_pressed), gtkaspell);
582 /* Checker creation */
583 static GtkAspeller *gtkaspeller_new(Dictionary *dictionary)
586 GtkAspeller *gtkaspeller = NULL;
590 g_return_val_if_fail(gtkaspellcheckers, NULL);
592 g_return_val_if_fail(dictionary, NULL);
594 if (dictionary->fullname == NULL)
595 gtkaspell_checkers_error_message(
596 g_strdup(_("No dictionary selected.")));
598 g_return_val_if_fail(dictionary->fullname, NULL);
600 if (dictionary->dictname == NULL) {
603 tmp = strrchr(dictionary->fullname, G_DIR_SEPARATOR);
606 dictionary->dictname = dictionary->fullname;
608 dictionary->dictname = tmp + 1;
611 dict = dictionary_dup(dictionary);
613 tmp = g_new0(GtkAspeller, 1);
614 tmp->dictionary = dict;
616 exist = g_slist_find_custom(gtkaspellcheckers->checkers, tmp,
621 if ((gtkaspeller = gtkaspeller_real_new(dict)) != NULL) {
622 gtkaspellcheckers->checkers = g_slist_append(
623 gtkaspellcheckers->checkers,
626 debug_print("Aspell: Created a new gtkaspeller %0x\n",
629 dictionary_delete(dict);
631 debug_print("Aspell: Could not create spell checker.\n");
634 debug_print("Aspell: number of existing checkers %d\n",
635 g_slist_length(gtkaspellcheckers->checkers));
640 static GtkAspeller *gtkaspeller_real_new(Dictionary *dict)
642 GtkAspeller *gtkaspeller;
643 AspellConfig *config;
644 AspellCanHaveError *ret;
646 g_return_val_if_fail(gtkaspellcheckers, NULL);
647 g_return_val_if_fail(dict, NULL);
649 gtkaspeller = g_new(GtkAspeller, 1);
651 gtkaspeller->dictionary = dict;
652 gtkaspeller->sug_mode = ASPELL_FASTMODE;
654 config = new_aspell_config();
656 if (!set_dictionary(config, dict))
659 ret = new_aspell_speller(config);
660 delete_aspell_config(config);
662 if (aspell_error_number(ret) != 0) {
663 gtkaspellcheckers->error_message
664 = g_strdup(aspell_error_message(ret));
666 delete_aspell_can_have_error(ret);
671 gtkaspeller->checker = to_aspell_speller(ret);
672 gtkaspeller->config = aspell_speller_config(gtkaspeller->checker);
677 static GtkAspeller *gtkaspeller_delete(GtkAspeller *gtkaspeller)
679 g_return_val_if_fail(gtkaspellcheckers, NULL);
681 gtkaspellcheckers->checkers =
682 g_slist_remove(gtkaspellcheckers->checkers,
685 debug_print("Aspell: Deleting gtkaspeller %0x.\n",
688 gtkaspeller_real_delete(gtkaspeller);
690 debug_print("Aspell: number of existing checkers %d\n",
691 g_slist_length(gtkaspellcheckers->checkers));
696 static GtkAspeller *gtkaspeller_real_delete(GtkAspeller *gtkaspeller)
698 g_return_val_if_fail(gtkaspeller, NULL);
699 g_return_val_if_fail(gtkaspeller->checker, NULL);
701 aspell_speller_save_all_word_lists(gtkaspeller->checker);
703 delete_aspell_speller(gtkaspeller->checker);
705 dictionary_delete(gtkaspeller->dictionary);
707 debug_print("Aspell: gtkaspeller %0x deleted.\n",
715 /*****************************************************************************/
716 /* Checker configuration */
718 static gboolean set_dictionary(AspellConfig *config, Dictionary *dict)
720 gchar *language = NULL;
721 gchar *jargon = NULL;
725 g_return_val_if_fail(config, FALSE);
726 g_return_val_if_fail(dict, FALSE);
728 strncpy(buf, dict->fullname, BUFSIZE-1);
729 buf[BUFSIZE-1] = 0x00;
731 buf[dict->dictname - dict->fullname] = 0x00;
733 CONFIG_REPLACE_RETURN_FALSE_IF_FAIL("dict-dir", buf);
734 debug_print("Aspell: looking for dictionaries in path %s.\n", buf);
736 strncpy(buf, dict->dictname, BUFSIZE-1);
739 if ((size = strrchr(buf, '-')) && isdigit((int) size[1]))
744 if ((jargon = strchr(language, '-')) != NULL)
747 if (size != NULL && jargon == size)
750 debug_print("Aspell: language: %s, jargon: %s, size: %s\n",
751 language, jargon ? jargon : "",
755 CONFIG_REPLACE_RETURN_FALSE_IF_FAIL("lang", language);
757 CONFIG_REPLACE_RETURN_FALSE_IF_FAIL("jargon", jargon);
759 CONFIG_REPLACE_RETURN_FALSE_IF_FAIL("size", size);
760 if (dict->encoding) {
763 aspell_enc = convert_to_aspell_encoding (dict->encoding);
764 aspell_config_replace(config, "encoding",
765 (const char *) aspell_enc);
768 RETURN_FALSE_IF_CONFIG_ERROR();
774 guchar *gtkaspell_get_dict(GtkAspell *gtkaspell)
777 g_return_val_if_fail(gtkaspell->gtkaspeller->config, NULL);
778 g_return_val_if_fail(gtkaspell->gtkaspeller->dictionary, NULL);
780 return g_strdup(gtkaspell->gtkaspeller->dictionary->dictname);
783 guchar *gtkaspell_get_path(GtkAspell *gtkaspell)
788 g_return_val_if_fail(gtkaspell->gtkaspeller->config, NULL);
789 g_return_val_if_fail(gtkaspell->gtkaspeller->dictionary, NULL);
791 dict = gtkaspell->gtkaspeller->dictionary;
792 path = g_strndup(dict->fullname, dict->dictname - dict->fullname);
797 /* set_sug_mode_cb() - Menu callback: Set the suggestion mode */
798 static void set_sug_mode_cb(GtkMenuItem *w, GtkAspell *gtkaspell)
802 themode = (char *) gtk_label_get_text(GTK_LABEL(GTK_BIN(w)->child));
803 themode = g_strdup(themode);
805 set_real_sug_mode(gtkaspell, themode);
809 static void set_real_sug_mode(GtkAspell *gtkaspell, const char *themode)
812 gint mode = ASPELL_FASTMODE;
813 g_return_if_fail(gtkaspell);
814 g_return_if_fail(gtkaspell->gtkaspeller);
815 g_return_if_fail(themode);
817 if (!strcmp(themode,_("Normal Mode")))
818 mode = ASPELL_NORMALMODE;
819 else if (!strcmp( themode,_("Bad Spellers Mode")))
820 mode = ASPELL_BADSPELLERMODE;
822 result = gtkaspell_set_sug_mode(gtkaspell, mode);
825 debug_print("Aspell: error while changing suggestion mode:%s\n",
826 gtkaspellcheckers->error_message);
827 gtkaspell_checkers_reset_error();
831 /* gtkaspell_set_sug_mode() - Set the suggestion mode */
832 gboolean gtkaspell_set_sug_mode(GtkAspell *gtkaspell, gint themode)
834 AspellConfig *config;
836 g_return_val_if_fail(gtkaspell, FALSE);
837 g_return_val_if_fail(gtkaspell->gtkaspeller, FALSE);
838 g_return_val_if_fail(gtkaspell->gtkaspeller->config, FALSE);
840 debug_print("Aspell: setting sug mode of gtkaspeller %0x to %d\n",
841 (guint) gtkaspell->gtkaspeller, themode);
843 config = gtkaspell->gtkaspeller->config;
846 case ASPELL_FASTMODE:
847 CONFIG_REPLACE_RETURN_FALSE_IF_FAIL("sug-mode", "fast");
849 case ASPELL_NORMALMODE:
850 CONFIG_REPLACE_RETURN_FALSE_IF_FAIL("sug-mode", "normal");
852 case ASPELL_BADSPELLERMODE:
853 CONFIG_REPLACE_RETURN_FALSE_IF_FAIL("sug-mode",
857 gtkaspellcheckers->error_message =
858 g_strdup(_("Unknown suggestion mode."));
862 gtkaspell->gtkaspeller->sug_mode = themode;
863 gtkaspell->default_sug_mode = themode;
868 /* misspelled_suggest() - Create a suggestion list for word */
869 static GList *misspelled_suggest(GtkAspell *gtkaspell, guchar *word)
871 const guchar *newword;
873 const AspellWordList *suggestions;
874 AspellStringEnumeration *elements;
876 g_return_val_if_fail(word, NULL);
878 if (!aspell_speller_check(gtkaspell->gtkaspeller->checker, word, -1)) {
879 free_suggestions_list(gtkaspell);
881 suggestions = aspell_speller_suggest(
882 gtkaspell->gtkaspeller->checker,
883 (const char *)word, -1);
884 elements = aspell_word_list_elements(suggestions);
885 list = g_list_append(list, g_strdup(word));
887 while ((newword = aspell_string_enumeration_next(elements)) != NULL)
888 list = g_list_append(list, g_strdup(newword));
890 gtkaspell->max_sug = g_list_length(list) - 1;
891 gtkaspell->suggestions_list = list;
896 free_suggestions_list(gtkaspell);
901 /* misspelled_test() - Just test if word is correctly spelled */
902 static int misspelled_test(GtkAspell *gtkaspell, unsigned char *word)
904 return aspell_speller_check(gtkaspell->gtkaspeller->checker, word, -1)
909 static gboolean iswordsep(unsigned char c)
911 return (isspace(c) || ispunct(c)) && c != '\'';
914 static guchar get_text_index_whar(GtkAspell *gtkaspell, int pos)
916 GtkTextView *view = gtkaspell->gtktext;
917 GtkTextBuffer *buffer = gtk_text_view_get_buffer(view);
918 GtkTextIter start, end;
919 const gchar *utf8chars;
922 gtk_text_buffer_get_iter_at_offset(buffer, &start, pos);
923 gtk_text_buffer_get_iter_at_offset(buffer, &end, pos+1);
925 utf8chars = gtk_text_iter_get_text(&start, &end);
926 if (is_ascii_str(utf8chars)) {
927 a = utf8chars ? utf8chars[0] : '\0' ;
929 gchar *tr = conv_iconv_strdup(utf8chars, CS_UTF_8,
930 gtkaspell->gtkaspeller->dictionary->encoding);
940 /* get_word_from_pos () - return the word pointed to. */
941 /* Handles correctly the quotes. */
942 static gboolean get_word_from_pos(GtkAspell *gtkaspell, gint pos,
943 unsigned char* buf, gint buflen,
944 gint *pstart, gint *pend)
947 /* TODO : when correcting a word into quotes, change the color of */
948 /* the quotes too, as may be they were highlighted before. To do */
949 /* this, we can use two others pointers that points to the whole */
950 /* word including quotes. */
956 GtkTextView *gtktext;
958 gtktext = gtkaspell->gtktext;
959 if (iswordsep(get_text_index_whar(gtkaspell, pos)))
962 /* The apostrophe character is somtimes used for quotes
963 * So include it in the word only if it is not surrounded
964 * by other characters.
967 for (start = pos; start >= 0; --start) {
968 c = get_text_index_whar(gtkaspell, start);
971 if (isspace(get_text_index_whar(gtkaspell,
973 || ispunct(get_text_index_whar(gtkaspell,
975 || isdigit(get_text_index_whar(gtkaspell,
977 /* start_quote = TRUE; */
982 /* start_quote = TRUE; */
986 else if (isspace(c) || ispunct(c) || isdigit(c))
992 for (end = pos; end < get_textview_buffer_charcount(gtktext); end++) {
993 c = get_text_index_whar(gtkaspell, end);
995 if (end < get_textview_buffer_charcount(gtktext)) {
996 if (isspace(get_text_index_whar(gtkaspell,
998 || ispunct(get_text_index_whar(gtkaspell,
1000 || isdigit(get_text_index_whar(gtkaspell,
1002 /* end_quote = TRUE; */
1007 /* end_quote = TRUE; */
1011 else if (isspace(c) || ispunct(c) || isdigit(c))
1021 if (end - start < buflen) {
1022 GtkTextIter iterstart, iterend;
1024 GtkTextBuffer *buffer = gtk_text_view_get_buffer(gtktext);
1025 gtk_text_buffer_get_iter_at_offset(buffer, &iterstart, start);
1026 gtk_text_buffer_get_iter_at_offset(buffer, &iterend, end);
1027 tmp = gtk_text_buffer_get_text(buffer, &iterstart, &iterend, FALSE);
1028 conv = conv_iconv_strdup(tmp, CS_UTF_8,
1029 gtkaspell->gtkaspeller->dictionary->encoding);
1031 strncpy(buf, conv, buflen-1);
1041 static gboolean check_at(GtkAspell *gtkaspell, gint from_pos)
1044 unsigned char buf[GTKASPELLWORDSIZE];
1045 GtkTextView *gtktext;
1047 g_return_val_if_fail(from_pos >= 0, FALSE);
1049 gtktext = gtkaspell->gtktext;
1051 if (!get_word_from_pos(gtkaspell, from_pos, buf, sizeof(buf),
1055 if (misspelled_test(gtkaspell, buf)) {
1056 strncpy(gtkaspell->theword, buf, GTKASPELLWORDSIZE - 1);
1057 gtkaspell->theword[GTKASPELLWORDSIZE - 1] = 0;
1058 gtkaspell->start_pos = start;
1059 gtkaspell->end_pos = end;
1060 free_suggestions_list(gtkaspell);
1062 change_color(gtkaspell, start, end, buf, &(gtkaspell->highlight));
1065 change_color(gtkaspell, start, end, buf, NULL);
1070 static gboolean check_next_prev(GtkAspell *gtkaspell, gboolean forward)
1076 gboolean misspelled;
1079 maxpos = gtkaspell->end_check_pos;
1087 pos = get_textview_buffer_offset(gtkaspell->gtktext);
1088 gtkaspell->orig_pos = pos;
1089 while (iswordsep(get_text_index_whar(gtkaspell, pos)) &&
1090 pos > minpos && pos <= maxpos)
1092 while (!(misspelled = check_at(gtkaspell, pos)) &&
1093 pos > minpos && pos <= maxpos) {
1095 while (!iswordsep(get_text_index_whar(gtkaspell, pos)) &&
1096 pos > minpos && pos <= maxpos)
1099 while (iswordsep(get_text_index_whar(gtkaspell, pos)) &&
1100 pos > minpos && pos <= maxpos)
1106 misspelled_suggest(gtkaspell, gtkaspell->theword);
1109 gtkaspell->orig_pos = gtkaspell->end_pos;
1111 set_textview_buffer_offset(gtkaspell->gtktext,
1112 gtkaspell->end_pos);
1113 /* scroll line to window center */
1114 gtk_text_view_scroll_to_mark(gtkaspell->gtktext,
1115 gtk_text_buffer_get_insert(
1116 gtk_text_view_get_buffer(gtkaspell->gtktext)),
1117 0.0, TRUE, 0.0, 0.5);
1118 /* let textview recalculate coordinates (set_menu_pos) */
1119 while (gtk_events_pending ())
1120 gtk_main_iteration ();
1122 list = make_sug_menu(gtkaspell);
1123 menu = gtk_menu_new();
1124 for (cur = list; cur; cur = cur->next)
1125 gtk_menu_append(menu, GTK_WIDGET(cur->data));
1127 gtk_menu_popup(GTK_MENU(menu), NULL, NULL,
1128 set_menu_pos, gtkaspell, 0, GDK_CURRENT_TIME);
1129 g_signal_connect(G_OBJECT(menu), "deactivate",
1130 G_CALLBACK(destroy_menu),
1132 g_signal_connect(G_OBJECT(menu),
1134 G_CALLBACK(aspell_key_pressed), gtkaspell);
1138 reset_theword_data(gtkaspell);
1140 alertpanel_notice(_("No misspelled word found."));
1141 set_textview_buffer_offset(gtkaspell->gtktext,
1142 gtkaspell->orig_pos);
1148 void gtkaspell_check_backwards(GtkAspell *gtkaspell)
1150 gtkaspell->continue_check = NULL;
1151 gtkaspell->end_check_pos =
1152 get_textview_buffer_charcount(gtkaspell->gtktext);
1153 check_next_prev(gtkaspell, FALSE);
1156 void gtkaspell_check_forwards_go(GtkAspell *gtkaspell)
1159 gtkaspell->continue_check = NULL;
1160 gtkaspell->end_check_pos =
1161 get_textview_buffer_charcount(gtkaspell->gtktext);
1162 check_next_prev(gtkaspell, TRUE);
1165 void gtkaspell_check_all(GtkAspell *gtkaspell)
1167 GtkTextView *gtktext;
1169 GtkTextBuffer *buffer;
1170 GtkTextIter startiter, enditer;
1172 g_return_if_fail(gtkaspell);
1173 g_return_if_fail(gtkaspell->gtktext);
1175 gtktext = gtkaspell->gtktext;
1176 buffer = gtk_text_view_get_buffer(gtktext);
1177 gtk_text_buffer_get_selection_bounds(buffer, &startiter, &enditer);
1178 start = gtk_text_iter_get_offset(&startiter);
1179 end = gtk_text_iter_get_offset(&enditer);
1183 end = gtk_text_buffer_get_char_count(buffer);
1184 } else if (start > end) {
1192 set_textview_buffer_offset(gtktext, start);
1194 gtkaspell->continue_check = continue_check;
1195 gtkaspell->end_check_pos = end;
1197 gtkaspell->misspelled = check_next_prev(gtkaspell, TRUE);
1200 static void continue_check(gpointer *data)
1202 GtkAspell *gtkaspell = (GtkAspell *) data;
1203 gint pos = get_textview_buffer_offset(gtkaspell->gtktext);
1204 if (pos < gtkaspell->end_check_pos && gtkaspell->misspelled)
1205 gtkaspell->misspelled = check_next_prev(gtkaspell, TRUE);
1207 gtkaspell->continue_check = NULL;
1210 void gtkaspell_highlight_all(GtkAspell *gtkaspell)
1215 GtkTextView *gtktext;
1217 g_return_if_fail(gtkaspell->gtkaspeller->checker);
1219 gtktext = gtkaspell->gtktext;
1221 len = get_textview_buffer_charcount(gtktext);
1223 origpos = get_textview_buffer_offset(gtktext);
1227 iswordsep(get_text_index_whar(gtkaspell, pos)))
1230 !iswordsep(get_text_index_whar(gtkaspell, pos)))
1233 check_at(gtkaspell, pos - 1);
1235 set_textview_buffer_offset(gtktext, origpos);
1238 static void replace_with_supplied_word_cb(GtkWidget *w, GtkAspell *gtkaspell)
1240 unsigned char *newword;
1241 GdkEvent *e= (GdkEvent *) gtk_get_current_event();
1243 newword = gtk_editable_get_chars(GTK_EDITABLE(gtkaspell->replace_entry),
1246 if (strcmp(newword, gtkaspell->theword)) {
1247 replace_real_word(gtkaspell, newword);
1249 if ((e->type == GDK_KEY_PRESS &&
1250 ((GdkEventKey *) e)->state & GDK_CONTROL_MASK)) {
1251 aspell_speller_store_replacement(
1252 gtkaspell->gtkaspeller->checker,
1253 gtkaspell->theword, -1,
1256 gtkaspell->replace_entry = NULL;
1261 if (w && GTK_IS_DIALOG(w)) {
1262 gtk_widget_destroy(w);
1265 set_point_continue(gtkaspell);
1269 static void replace_word_cb(GtkWidget *w, gpointer data)
1271 unsigned char *newword;
1272 GtkAspell *gtkaspell = (GtkAspell *) data;
1273 GdkEvent *e= (GdkEvent *) gtk_get_current_event();
1275 newword = (unsigned char *) gtk_label_get_text(GTK_LABEL(GTK_BIN(w)->child));
1276 newword = g_strdup(newword);
1278 replace_real_word(gtkaspell, newword);
1280 if ((e->type == GDK_KEY_PRESS &&
1281 ((GdkEventKey *) e)->state & GDK_CONTROL_MASK) ||
1282 (e->type == GDK_BUTTON_RELEASE &&
1283 ((GdkEventButton *) e)->state & GDK_CONTROL_MASK)) {
1284 aspell_speller_store_replacement(
1285 gtkaspell->gtkaspeller->checker,
1286 gtkaspell->theword, -1,
1290 gtk_menu_shell_deactivate(GTK_MENU_SHELL(w->parent));
1292 set_point_continue(gtkaspell);
1296 static void replace_real_word(GtkAspell *gtkaspell, gchar *newword)
1298 int oldlen, newlen, wordlen;
1301 GtkTextView *gtktext;
1302 GtkTextBuffer *textbuf;
1303 GtkTextIter startiter, enditer;
1305 if (!newword) return;
1307 gtktext = gtkaspell->gtktext;
1308 textbuf = gtk_text_view_get_buffer(gtktext);
1310 origpos = gtkaspell->orig_pos;
1312 oldlen = gtkaspell->end_pos - gtkaspell->start_pos;
1313 wordlen = strlen(gtkaspell->theword);
1315 newlen = strlen(newword); /* FIXME: multybyte characters? */
1317 g_signal_handlers_block_by_func(G_OBJECT(gtktext),
1318 G_CALLBACK(entry_insert_cb),
1320 g_signal_handlers_block_by_func(G_OBJECT(gtktext),
1321 G_CALLBACK(entry_delete_cb),
1324 gtk_text_buffer_get_iter_at_offset(textbuf, &startiter,
1325 gtkaspell->start_pos);
1326 gtk_text_buffer_get_iter_at_offset(textbuf, &enditer,
1327 gtkaspell->end_pos);
1328 g_signal_emit_by_name(G_OBJECT(textbuf), "delete-range",
1329 &startiter, &enditer, gtkaspell);
1330 g_signal_emit_by_name(G_OBJECT(textbuf), "insert-text",
1331 &startiter, newword, newlen, gtkaspell);
1333 g_signal_handlers_unblock_by_func(G_OBJECT(gtktext),
1334 G_CALLBACK(entry_insert_cb),
1336 g_signal_handlers_unblock_by_func(G_OBJECT(gtktext),
1337 G_CALLBACK(entry_delete_cb),
1340 /* Put the point and the position where we clicked with the mouse
1341 * It seems to be a hack, as I must thaw,freeze,thaw the widget
1342 * to let it update correctly the word insertion and then the
1343 * point & position position. If not, SEGV after the first replacement
1344 * If the new word ends before point, put the point at its end.
1347 if (origpos - gtkaspell->start_pos < oldlen &&
1348 origpos - gtkaspell->start_pos >= 0) {
1349 /* Original point was in the word.
1350 * Let it there unless point is going to be outside of the word
1352 if (origpos - gtkaspell->start_pos >= newlen) {
1353 pos = gtkaspell->start_pos + newlen;
1356 else if (origpos >= gtkaspell->end_pos) {
1357 /* move the position according to the change of length */
1358 pos = origpos + newlen - oldlen;
1361 gtkaspell->end_pos = gtkaspell->start_pos + strlen(newword); /* FIXME: multibyte characters? */
1363 if (get_textview_buffer_charcount(gtktext) < pos)
1364 pos = get_textview_buffer_charcount(gtktext);
1365 gtkaspell->orig_pos = pos;
1367 set_textview_buffer_offset(gtktext, gtkaspell->orig_pos);
1370 /* Accept this word for this session */
1371 static void add_word_to_session_cb(GtkWidget *w, gpointer data)
1374 GtkTextView *gtktext;
1375 GtkAspell *gtkaspell = (GtkAspell *) data;
1376 gtktext = gtkaspell->gtktext;
1378 pos = get_textview_buffer_offset(gtktext);
1380 aspell_speller_add_to_session(gtkaspell->gtkaspeller->checker,
1382 strlen(gtkaspell->theword));
1384 check_at(gtkaspell, gtkaspell->start_pos);
1386 gtk_menu_shell_deactivate(GTK_MENU_SHELL(GTK_WIDGET(w)->parent));
1388 set_point_continue(gtkaspell);
1391 /* add_word_to_personal_cb() - add word to personal dict. */
1392 static void add_word_to_personal_cb(GtkWidget *w, gpointer data)
1394 GtkAspell *gtkaspell = (GtkAspell *) data;
1396 aspell_speller_add_to_personal(gtkaspell->gtkaspeller->checker,
1398 strlen(gtkaspell->theword));
1400 check_at(gtkaspell, gtkaspell->start_pos);
1402 gtk_menu_shell_deactivate(GTK_MENU_SHELL(GTK_WIDGET(w)->parent));
1403 set_point_continue(gtkaspell);
1406 static void check_with_alternate_cb(GtkWidget *w, gpointer data)
1408 GtkAspell *gtkaspell = (GtkAspell *) data;
1411 gtk_menu_shell_deactivate(GTK_MENU_SHELL(GTK_WIDGET(w)->parent));
1413 use_alternate_dict(gtkaspell);
1414 misspelled = check_at(gtkaspell, gtkaspell->start_pos);
1416 if (!gtkaspell->continue_check) {
1418 gtkaspell->misspelled = misspelled;
1420 if (gtkaspell->misspelled) {
1423 misspelled_suggest(gtkaspell, gtkaspell->theword);
1425 set_textview_buffer_offset(gtkaspell->gtktext,
1426 gtkaspell->end_pos);
1428 list = make_sug_menu(gtkaspell);
1429 menu = gtk_menu_new();
1430 for (cur = list; cur; cur = cur->next)
1431 gtk_menu_append(menu, GTK_WIDGET(cur->data));
1433 gtk_menu_popup(GTK_MENU(menu), NULL, NULL,
1434 set_menu_pos, gtkaspell, 0,
1436 g_signal_connect(G_OBJECT(menu), "deactivate",
1437 G_CALLBACK(destroy_menu),
1439 g_signal_connect(G_OBJECT(menu),
1441 G_CALLBACK(aspell_key_pressed), gtkaspell);
1445 gtkaspell->orig_pos = gtkaspell->start_pos;
1447 set_point_continue(gtkaspell);
1450 static gboolean replace_key_pressed(GtkWidget *widget,
1452 GtkAspell *gtkaspell)
1454 if (event && event->keyval == GDK_Escape) {
1455 gtk_widget_destroy(widget);
1457 } else if (event && event->keyval == GDK_Return) {
1458 replace_with_supplied_word_cb(widget, gtkaspell);
1464 static void replace_with_create_dialog_cb(GtkWidget *w, gpointer data)
1466 static PangoFontDescription *font_desc;
1473 GtkWidget *ok_button;
1474 GtkWidget *cancel_button;
1475 GtkWidget *confirm_area;
1479 GtkAspell *gtkaspell = (GtkAspell *) data;
1481 gdk_window_get_origin((GTK_WIDGET(w)->parent)->window, &xx, &yy);
1483 gtk_menu_shell_deactivate(GTK_MENU_SHELL(GTK_WIDGET(w)->parent));
1485 dialog = gtk_dialog_new();
1487 gtk_window_set_resizable(GTK_WINDOW(dialog), FALSE);
1488 gtk_window_set_title(GTK_WINDOW(dialog),_("Replace unknown word"));
1489 gtk_dialog_set_has_separator (GTK_DIALOG (dialog), FALSE);
1490 gtk_widget_set_uposition(dialog, xx, yy);
1492 g_signal_connect_swapped(G_OBJECT(dialog), "destroy",
1493 G_CALLBACK(gtk_widget_destroy),
1496 gtk_box_set_spacing (GTK_BOX (GTK_DIALOG (dialog)->vbox), 14);
1497 hbox = gtk_hbox_new (FALSE, 12);
1498 gtk_container_set_border_width (GTK_CONTAINER (hbox), 5);
1499 gtk_widget_show (hbox);
1500 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox), hbox,
1503 thelabel = g_strdup_printf(_("<span weight=\"bold\" "
1504 "size=\"larger\">Replace \"%s\" with: </span>"),
1505 gtkaspell->theword);
1506 /* for title label */
1507 w_hbox = gtk_hbox_new(FALSE, 0);
1509 icon = gtk_image_new_from_stock(GTK_STOCK_DIALOG_QUESTION,
1510 GTK_ICON_SIZE_DIALOG);
1511 gtk_misc_set_alignment (GTK_MISC (icon), 0.5, 0.0);
1512 gtk_box_pack_start (GTK_BOX (hbox), icon, FALSE, FALSE, 0);
1514 vbox = gtk_vbox_new (FALSE, 12);
1515 gtk_box_pack_start (GTK_BOX (hbox), vbox, TRUE, TRUE, 0);
1516 gtk_widget_show (vbox);
1518 label = gtk_label_new(thelabel);
1519 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
1520 gtk_label_set_justify(GTK_LABEL(label), GTK_JUSTIFY_LEFT);
1521 gtk_label_set_use_markup (GTK_LABEL (label), TRUE);
1522 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
1523 gtk_label_set_line_wrap(GTK_LABEL(label), TRUE);
1527 size = pango_font_description_get_size
1528 (label->style->font_desc);
1529 font_desc = pango_font_description_new();
1530 pango_font_description_set_weight
1531 (font_desc, PANGO_WEIGHT_BOLD);
1532 pango_font_description_set_size
1533 (font_desc, size * PANGO_SCALE_LARGE);
1536 gtk_widget_modify_font(label, font_desc);
1539 entry = gtk_entry_new();
1540 gtkaspell->replace_entry = entry;
1541 gtk_entry_set_text(GTK_ENTRY(entry), gtkaspell->theword);
1542 gtk_editable_select_region(GTK_EDITABLE(entry), 0, -1);
1543 g_signal_connect(G_OBJECT(dialog),
1545 G_CALLBACK(replace_key_pressed), gtkaspell);
1546 gtk_box_pack_start(GTK_BOX(vbox), entry, FALSE, FALSE, 0);
1548 label = gtk_label_new(_("Holding down Control key while pressing "
1549 "Enter\nwill learn from mistake.\n"));
1550 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
1551 gtk_label_set_justify(GTK_LABEL(label), GTK_JUSTIFY_LEFT);
1552 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
1553 gtk_widget_show(label);
1555 hbox = gtk_hbox_new(TRUE, 0);
1557 gtkut_stock_button_set_create(&confirm_area,
1558 &ok_button, GTK_STOCK_OK,
1559 &cancel_button, GTK_STOCK_CANCEL,
1562 gtk_box_pack_end(GTK_BOX(GTK_DIALOG(dialog)->action_area),
1563 confirm_area, FALSE, FALSE, 0);
1564 gtk_container_set_border_width(GTK_CONTAINER(confirm_area), 5);
1566 g_signal_connect(G_OBJECT(ok_button), "clicked",
1567 G_CALLBACK(replace_with_supplied_word_cb),
1569 g_signal_connect_swapped(G_OBJECT(ok_button), "clicked",
1570 G_CALLBACK(gtk_widget_destroy),
1573 g_signal_connect_swapped(G_OBJECT(cancel_button), "clicked",
1574 G_CALLBACK(gtk_widget_destroy),
1577 gtk_widget_grab_focus(entry);
1579 gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
1581 gtk_widget_show_all(dialog);
1584 void gtkaspell_uncheck_all(GtkAspell * gtkaspell)
1586 GtkTextView *gtktext;
1587 GtkTextBuffer *buffer;
1588 GtkTextIter startiter, enditer;
1590 gtktext = gtkaspell->gtktext;
1592 buffer = gtk_text_view_get_buffer(gtktext);
1593 gtk_text_buffer_get_iter_at_offset(buffer, &startiter, 0);
1594 gtk_text_buffer_get_iter_at_offset(buffer, &enditer,
1595 get_textview_buffer_charcount(gtktext)-1);
1596 gtk_text_buffer_remove_tag_by_name(buffer, "misspelled",
1597 &startiter, &enditer);
1600 static void toggle_check_while_typing_cb(GtkWidget *w, gpointer data)
1602 GtkAspell *gtkaspell = (GtkAspell *) data;
1604 gtkaspell->check_while_typing = gtkaspell->check_while_typing == FALSE;
1606 if (!gtkaspell->check_while_typing)
1607 gtkaspell_uncheck_all(gtkaspell);
1610 static GSList *create_empty_dictionary_list(void)
1612 GSList *list = NULL;
1615 dict = g_new0(Dictionary, 1);
1616 dict->fullname = g_strdup(_("None"));
1617 dict->dictname = dict->fullname;
1618 dict->encoding = NULL;
1620 return g_slist_append(list, dict);
1623 /* gtkaspell_get_dictionary_list() - returns list of dictionary names */
1624 GSList *gtkaspell_get_dictionary_list(const gchar *aspell_path, gint refresh)
1628 AspellConfig *config;
1629 AspellDictInfoList *dlist;
1630 AspellDictInfoEnumeration *dels;
1631 const AspellDictInfo *entry;
1633 if (!gtkaspellcheckers)
1634 gtkaspell_checkers_init();
1636 if (gtkaspellcheckers->dictionary_list && !refresh)
1637 return gtkaspellcheckers->dictionary_list;
1639 gtkaspell_free_dictionary_list(
1640 gtkaspellcheckers->dictionary_list);
1643 config = new_aspell_config();
1645 aspell_config_replace(config, "dict-dir", aspell_path);
1646 if (aspell_config_error_number(config) != 0) {
1647 gtkaspellcheckers->error_message = g_strdup(
1648 aspell_config_error_message(config));
1649 gtkaspellcheckers->dictionary_list =
1650 create_empty_dictionary_list();
1652 return gtkaspellcheckers->dictionary_list;
1655 dlist = get_aspell_dict_info_list(config);
1656 delete_aspell_config(config);
1658 debug_print("Aspell: checking for dictionaries in %s\n", aspell_path);
1659 dels = aspell_dict_info_list_elements(dlist);
1660 while ( (entry = aspell_dict_info_enumeration_next(dels)) != 0)
1662 dict = g_new0(Dictionary, 1);
1663 dict->fullname = g_strdup_printf("%s%s", aspell_path,
1665 dict->dictname = dict->fullname + strlen(aspell_path);
1666 dict->encoding = g_strdup(entry->code);
1668 if (g_slist_find_custom(list, dict,
1669 (GCompareFunc) compare_dict) != NULL) {
1670 dictionary_delete(dict);
1674 debug_print("Aspell: found dictionary %s %s %s\n", dict->fullname,
1675 dict->dictname, dict->encoding);
1676 list = g_slist_insert_sorted(list, dict,
1677 (GCompareFunc) compare_dict);
1680 delete_aspell_dict_info_enumeration(dels);
1684 debug_print("Aspell: error when searching for dictionaries: "
1685 "No dictionary found.\n");
1686 list = create_empty_dictionary_list();
1689 gtkaspellcheckers->dictionary_list = list;
1694 void gtkaspell_free_dictionary_list(GSList *list)
1698 for (walk = list; walk != NULL; walk = g_slist_next(walk))
1700 dict = (Dictionary *) walk->data;
1701 dictionary_delete(dict);
1707 GtkWidget *gtkaspell_dictionary_option_menu_new(const gchar *aspell_path)
1709 GSList *dict_list, *tmp;
1714 dict_list = gtkaspell_get_dictionary_list(aspell_path, TRUE);
1715 g_return_val_if_fail(dict_list, NULL);
1717 menu = gtk_menu_new();
1719 for (tmp = dict_list; tmp != NULL; tmp = g_slist_next(tmp)) {
1720 dict = (Dictionary *) tmp->data;
1721 item = gtk_menu_item_new_with_label(dict->dictname);
1722 g_object_set_data(G_OBJECT(item), "dict_name",
1725 gtk_menu_append(GTK_MENU(menu), item);
1726 gtk_widget_show(item);
1729 gtk_widget_show(menu);
1734 gchar *gtkaspell_get_dictionary_menu_active_item(GtkWidget *menu)
1736 GtkWidget *menuitem;
1737 gchar *dict_fullname;
1740 g_return_val_if_fail(GTK_IS_MENU(menu), NULL);
1742 menuitem = gtk_menu_get_active(GTK_MENU(menu));
1743 dict_fullname = (gchar *) g_object_get_data(G_OBJECT(menuitem),
1745 g_return_val_if_fail(dict_fullname, NULL);
1747 label = g_strdup(dict_fullname);
1753 gint gtkaspell_set_dictionary_menu_active_item(GtkWidget *menu,
1754 const gchar *dictionary)
1759 g_return_val_if_fail(menu != NULL, 0);
1760 g_return_val_if_fail(dictionary != NULL, 0);
1761 g_return_val_if_fail(GTK_IS_OPTION_MENU(menu), 0);
1764 for (cur = GTK_MENU_SHELL(gtk_option_menu_get_menu(
1765 GTK_OPTION_MENU(menu)))->children;
1766 cur != NULL; cur = cur->next) {
1767 GtkWidget *menuitem;
1770 menuitem = GTK_WIDGET(cur->data);
1771 dict_name = g_object_get_data(G_OBJECT(menuitem),
1773 if ((dict_name != NULL) && !strcmp2(dict_name, dictionary)) {
1774 gtk_option_menu_set_history(GTK_OPTION_MENU(menu), n);
1785 GtkWidget *gtkaspell_sugmode_option_menu_new(gint sugmode)
1790 menu = gtk_menu_new();
1791 gtk_widget_show(menu);
1793 item = gtk_menu_item_new_with_label(_("Fast Mode"));
1794 gtk_widget_show(item);
1795 gtk_menu_append(GTK_MENU(menu), item);
1796 g_object_set_data(G_OBJECT(item), "sugmode",
1797 GINT_TO_POINTER(ASPELL_FASTMODE));
1799 item = gtk_menu_item_new_with_label(_("Normal Mode"));
1800 gtk_widget_show(item);
1801 gtk_menu_append(GTK_MENU(menu), item);
1802 g_object_set_data(G_OBJECT(item), "sugmode",
1803 GINT_TO_POINTER(ASPELL_NORMALMODE));
1805 item = gtk_menu_item_new_with_label(_("Bad Spellers Mode"));
1806 gtk_widget_show(item);
1807 gtk_menu_append(GTK_MENU(menu), item);
1808 g_object_set_data(G_OBJECT(item), "sugmode",
1809 GINT_TO_POINTER(ASPELL_BADSPELLERMODE));
1814 void gtkaspell_sugmode_option_menu_set(GtkOptionMenu *optmenu, gint sugmode)
1816 g_return_if_fail(GTK_IS_OPTION_MENU(optmenu));
1818 g_return_if_fail(sugmode == ASPELL_FASTMODE ||
1819 sugmode == ASPELL_NORMALMODE ||
1820 sugmode == ASPELL_BADSPELLERMODE);
1822 gtk_option_menu_set_history(GTK_OPTION_MENU(optmenu), sugmode - 1);
1825 gint gtkaspell_get_sugmode_from_option_menu(GtkOptionMenu *optmenu)
1830 g_return_val_if_fail(GTK_IS_OPTION_MENU(optmenu), -1);
1832 item = gtk_menu_get_active(GTK_MENU(gtk_option_menu_get_menu(optmenu)));
1834 sugmode = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(item),
1840 static void use_alternate_dict(GtkAspell *gtkaspell)
1844 tmp = gtkaspell->gtkaspeller;
1845 gtkaspell->gtkaspeller = gtkaspell->alternate_speller;
1846 gtkaspell->alternate_speller = tmp;
1849 static void destroy_menu(GtkWidget *widget,
1850 gpointer user_data) {
1852 GtkAspell *gtkaspell = (GtkAspell *)user_data;
1854 if (gtkaspell->accel_group) {
1855 gtk_window_remove_accel_group(GTK_WINDOW(gtkaspell->parent_window),
1856 gtkaspell->accel_group);
1857 gtkaspell->accel_group = NULL;
1861 static gboolean aspell_key_pressed(GtkWidget *widget,
1863 GtkAspell *gtkaspell)
1865 if (event && (isascii(event->keyval) || event->keyval == GDK_Return)) {
1866 gtk_accel_groups_activate(
1867 G_OBJECT(gtkaspell->parent_window),
1868 event->keyval, event->state);
1869 } else if (event && event->keyval == GDK_Escape) {
1870 destroy_menu(NULL, gtkaspell);
1875 /* make_sug_menu() - Add menus to accept this word for this session
1876 * and to add it to personal dictionary
1878 static GSList *make_sug_menu(GtkAspell *gtkaspell)
1881 unsigned char *caption;
1882 GtkTextView *gtktext;
1883 GtkAccelGroup *accel;
1884 GList *l = gtkaspell->suggestions_list;
1886 GSList *list = NULL;
1887 gtktext = gtkaspell->gtktext;
1889 accel = gtk_accel_group_new();
1891 if (gtkaspell->accel_group) {
1892 gtk_window_remove_accel_group(GTK_WINDOW(gtkaspell->parent_window),
1893 gtkaspell->accel_group);
1894 gtkaspell->accel_group = NULL;
1897 utf8buf = conv_codeset_strdup((unsigned char*)l->data,
1898 conv_get_locale_charset_str(),
1900 caption = g_strdup_printf(_("\"%s\" unknown in %s"),
1902 gtkaspell->gtkaspeller->dictionary->dictname);
1903 item = gtk_menu_item_new_with_label(caption);
1905 gtk_widget_show(item);
1906 list = g_slist_append(list, item);
1907 gtk_misc_set_alignment(GTK_MISC(GTK_BIN(item)->child), 0.5, 0.5);
1910 item = gtk_menu_item_new();
1911 gtk_widget_show(item);
1912 list = g_slist_append(list, item);
1914 item = gtk_menu_item_new_with_label(_("Accept in this session"));
1915 gtk_widget_show(item);
1916 list = g_slist_append(list, item);
1917 g_signal_connect(G_OBJECT(item), "activate",
1918 G_CALLBACK(add_word_to_session_cb),
1920 gtk_widget_add_accelerator(item, "activate", accel, GDK_space,
1922 GTK_ACCEL_LOCKED | GTK_ACCEL_VISIBLE);
1924 item = gtk_menu_item_new_with_label(_("Add to personal dictionary"));
1925 gtk_widget_show(item);
1926 list = g_slist_append(list, item);
1927 g_signal_connect(G_OBJECT(item), "activate",
1928 G_CALLBACK(add_word_to_personal_cb),
1930 gtk_widget_add_accelerator(item, "activate", accel, GDK_Return,
1932 GTK_ACCEL_LOCKED | GTK_ACCEL_VISIBLE);
1934 item = gtk_menu_item_new_with_label(_("Replace with..."));
1935 gtk_widget_show(item);
1936 list = g_slist_append(list, item);
1937 g_signal_connect(G_OBJECT(item), "activate",
1938 G_CALLBACK(replace_with_create_dialog_cb),
1940 gtk_widget_add_accelerator(item, "activate", accel, GDK_R, 0,
1941 GTK_ACCEL_LOCKED | GTK_ACCEL_VISIBLE);
1942 gtk_widget_add_accelerator(item, "activate", accel, GDK_R,
1946 if (gtkaspell->use_alternate && gtkaspell->alternate_speller) {
1947 caption = g_strdup_printf(_("Check with %s"),
1948 gtkaspell->alternate_speller->dictionary->dictname);
1949 item = gtk_menu_item_new_with_label(caption);
1951 gtk_widget_show(item);
1952 list = g_slist_append(list, item);
1953 g_signal_connect(G_OBJECT(item), "activate",
1954 G_CALLBACK(check_with_alternate_cb),
1956 gtk_widget_add_accelerator(item, "activate", accel, GDK_X, 0,
1957 GTK_ACCEL_LOCKED | GTK_ACCEL_VISIBLE);
1958 gtk_widget_add_accelerator(item, "activate", accel, GDK_X,
1963 item = gtk_menu_item_new();
1964 gtk_widget_show(item);
1965 list = g_slist_append(list, item);
1969 item = gtk_menu_item_new_with_label(_("(no suggestions)"));
1970 gtk_widget_show(item);
1971 list = g_slist_append(list, item);
1973 GtkWidget *curmenu = NULL;
1977 if (count == MENUCOUNT) {
1980 item = gtk_menu_item_new_with_label(_("More..."));
1981 gtk_widget_show(item);
1983 gtk_menu_append(GTK_MENU(curmenu), item);
1985 list = g_slist_append(list, item);
1987 curmenu = gtk_menu_new();
1988 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item),
1992 utf8buf = conv_codeset_strdup((unsigned char*)l->data,
1993 conv_get_locale_charset_str(),
1995 item = gtk_menu_item_new_with_label(utf8buf);
1997 gtk_widget_show(item);
1998 if (curmenu == NULL) {
1999 list = g_slist_append(list, item);
2001 gtk_menu_append(GTK_MENU(curmenu), item);
2003 g_signal_connect(G_OBJECT(item), "activate",
2004 G_CALLBACK(replace_word_cb),
2007 if (curmenu == NULL && count < MENUCOUNT) {
2008 gtk_widget_add_accelerator(item, "activate",
2013 gtk_widget_add_accelerator(item, "activate",
2022 } while ((l = l->next) != NULL);
2025 gtk_window_add_accel_group
2026 (GTK_WINDOW(gtkaspell->parent_window),
2028 gtkaspell->accel_group = accel;
2033 static GSList *populate_submenu(GtkAspell *gtkaspell)
2035 GtkWidget *item, *submenu;
2037 GtkAspeller *gtkaspeller = gtkaspell->gtkaspeller;
2038 GSList *list = NULL;
2040 dictname = g_strdup_printf(_("Dictionary: %s"),
2041 gtkaspeller->dictionary->dictname);
2042 item = gtk_menu_item_new_with_label(dictname);
2043 gtk_misc_set_alignment(GTK_MISC(GTK_BIN(item)->child), 0.5, 0.5);
2045 gtk_widget_show(item);
2046 list = g_slist_append(list, item);
2048 item = gtk_menu_item_new();
2049 gtk_widget_show(item);
2050 list = g_slist_append(list, item);
2052 if (gtkaspell->use_alternate && gtkaspell->alternate_speller) {
2053 dictname = g_strdup_printf(_("Use alternate (%s)"),
2054 gtkaspell->alternate_speller->dictionary->dictname);
2055 item = gtk_menu_item_new_with_label(dictname);
2057 g_signal_connect(G_OBJECT(item), "activate",
2058 G_CALLBACK(switch_to_alternate_cb),
2060 gtk_widget_show(item);
2061 list = g_slist_append(list, item);
2064 item = gtk_check_menu_item_new_with_label(_("Fast Mode"));
2065 if (gtkaspell->gtkaspeller->sug_mode == ASPELL_FASTMODE) {
2066 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item),TRUE);
2067 gtk_widget_set_sensitive(GTK_WIDGET(item),FALSE);
2069 g_signal_connect(G_OBJECT(item), "activate",
2070 G_CALLBACK(set_sug_mode_cb),
2072 gtk_widget_show(item);
2073 list = g_slist_append(list, item);
2075 item = gtk_check_menu_item_new_with_label(_("Normal Mode"));
2076 if (gtkaspell->gtkaspeller->sug_mode == ASPELL_NORMALMODE) {
2077 gtk_widget_set_sensitive(GTK_WIDGET(item), FALSE);
2078 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), TRUE);
2080 g_signal_connect(G_OBJECT(item), "activate",
2081 G_CALLBACK(set_sug_mode_cb),
2083 gtk_widget_show(item);
2084 list = g_slist_append(list, item);
2086 item = gtk_check_menu_item_new_with_label(_("Bad Spellers Mode"));
2087 if (gtkaspell->gtkaspeller->sug_mode == ASPELL_BADSPELLERMODE) {
2088 gtk_widget_set_sensitive(GTK_WIDGET(item), FALSE);
2089 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), TRUE);
2091 g_signal_connect(G_OBJECT(item), "activate",
2092 G_CALLBACK(set_sug_mode_cb),
2094 gtk_widget_show(item);
2095 list = g_slist_append(list, item);
2097 item = gtk_menu_item_new();
2098 gtk_widget_show(item);
2099 list = g_slist_append(list, item);
2101 item = gtk_check_menu_item_new_with_label(_("Check while typing"));
2102 if (gtkaspell->check_while_typing)
2103 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), TRUE);
2105 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), FALSE);
2106 g_signal_connect(G_OBJECT(item), "activate",
2107 G_CALLBACK(toggle_check_while_typing_cb),
2109 gtk_widget_show(item);
2110 list = g_slist_append(list, item);
2112 item = gtk_menu_item_new();
2113 gtk_widget_show(item);
2114 list = g_slist_append(list, item);
2116 submenu = gtk_menu_new();
2117 item = gtk_menu_item_new_with_label(_("Change dictionary"));
2118 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item),submenu);
2119 gtk_widget_show(item);
2120 list = g_slist_append(list, item);
2123 if (gtkaspellcheckers->dictionary_list == NULL)
2124 gtkaspell_get_dictionary_list(gtkaspell->dictionary_path, FALSE);
2126 GtkWidget * curmenu = submenu;
2130 tmp = gtkaspellcheckers->dictionary_list;
2132 for (tmp = gtkaspellcheckers->dictionary_list; tmp != NULL;
2133 tmp = g_slist_next(tmp)) {
2134 if (count == MENUCOUNT) {
2137 newmenu = gtk_menu_new();
2138 item = gtk_menu_item_new_with_label(_("More..."));
2139 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item),
2142 gtk_menu_append(GTK_MENU(curmenu), item);
2143 gtk_widget_show(item);
2147 dict = (Dictionary *) tmp->data;
2148 item = gtk_check_menu_item_new_with_label(dict->dictname);
2149 g_object_set_data(G_OBJECT(item), "dict_name",
2151 if (strcmp2(dict->fullname,
2152 gtkaspell->gtkaspeller->dictionary->fullname))
2153 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), FALSE);
2155 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), TRUE);
2156 gtk_widget_set_sensitive(GTK_WIDGET(item),
2159 g_signal_connect(G_OBJECT(item), "activate",
2160 G_CALLBACK(change_dict_cb),
2162 gtk_widget_show(item);
2163 gtk_menu_append(GTK_MENU(curmenu), item);
2171 static GSList *make_config_menu(GtkAspell *gtkaspell)
2173 return populate_submenu(gtkaspell);
2176 static void set_menu_pos(GtkMenu *menu, gint *x, gint *y,
2177 gboolean *push_in, gpointer data)
2179 GtkAspell *gtkaspell = (GtkAspell *) data;
2180 gint xx = 0, yy = 0;
2183 GtkTextView *text = GTK_TEXT_VIEW(gtkaspell->gtktext);
2184 GtkTextBuffer *textbuf;
2189 textbuf = gtk_text_view_get_buffer(gtkaspell->gtktext);
2190 gtk_text_buffer_get_iter_at_mark(textbuf, &iter,
2191 gtk_text_buffer_get_insert(textbuf));
2192 gtk_text_view_get_iter_location(gtkaspell->gtktext, &iter, &rect);
2193 gtk_text_view_buffer_to_window_coords(text, GTK_TEXT_WINDOW_TEXT,
2197 gdk_window_get_origin(GTK_WIDGET(gtkaspell->gtktext)->window, &xx, &yy);
2199 sx = gdk_screen_width();
2200 sy = gdk_screen_height();
2202 gtk_widget_get_child_requisition(GTK_WIDGET(menu), &r);
2208 gdk_char_width(gtk_style_get_font(GTK_WIDGET(text)->style), ' ');
2210 *y = rect.y + rect.height + yy;
2216 gdk_string_height(gtk_style_get_font(
2217 GTK_WIDGET(text)->style),
2218 gtkaspell->theword);
2221 /* Menu call backs */
2223 gboolean gtkaspell_change_dict(GtkAspell *gtkaspell, const gchar *dictionary)
2226 GtkAspeller *gtkaspeller;
2229 g_return_val_if_fail(gtkaspell, FALSE);
2230 g_return_val_if_fail(dictionary, FALSE);
2232 sug_mode = gtkaspell->default_sug_mode;
2234 dict = g_new0(Dictionary, 1);
2235 dict->fullname = g_strdup(dictionary);
2236 dict->encoding = g_strdup(gtkaspell->gtkaspeller->dictionary->encoding);
2238 if (gtkaspell->use_alternate && gtkaspell->alternate_speller &&
2239 dict == gtkaspell->alternate_speller->dictionary) {
2240 use_alternate_dict(gtkaspell);
2241 dictionary_delete(dict);
2242 gtkaspell->alternate_speller->dictionary = NULL;
2246 gtkaspeller = gtkaspeller_new(dict);
2250 message = g_strdup_printf(_("The spell checker could not change dictionary.\n%s"),
2251 gtkaspellcheckers->error_message);
2253 alertpanel_warning(message);
2256 if (gtkaspell->use_alternate) {
2257 if (gtkaspell->alternate_speller)
2258 gtkaspeller_delete(gtkaspell->alternate_speller);
2259 gtkaspell->alternate_speller = gtkaspell->gtkaspeller;
2261 gtkaspeller_delete(gtkaspell->gtkaspeller);
2263 gtkaspell->gtkaspeller = gtkaspeller;
2264 gtkaspell_set_sug_mode(gtkaspell, sug_mode);
2267 dictionary_delete(dict);
2272 /* change_dict_cb() - Menu callback : change dict */
2273 static void change_dict_cb(GtkWidget *w, GtkAspell *gtkaspell)
2277 fullname = (gchar *) g_object_get_data(G_OBJECT(w), "dict_name");
2279 if (!strcmp2(fullname, _("None")))
2282 gtkaspell_change_dict(gtkaspell, fullname);
2285 static void switch_to_alternate_cb(GtkWidget *w,
2288 GtkAspell *gtkaspell = (GtkAspell *) data;
2289 use_alternate_dict(gtkaspell);
2292 /* Misc. helper functions */
2294 static void set_point_continue(GtkAspell *gtkaspell)
2296 GtkTextView *gtktext;
2298 gtktext = gtkaspell->gtktext;
2300 set_textview_buffer_offset(gtktext, gtkaspell->orig_pos);
2302 if (gtkaspell->continue_check)
2303 gtkaspell->continue_check((gpointer *) gtkaspell);
2306 static void allocate_color(GtkAspell *gtkaspell, gint rgbvalue)
2308 GtkTextBuffer *buffer = gtk_text_view_get_buffer(gtkaspell->gtktext);
2309 GdkColor *color = &(gtkaspell->highlight);
2311 /* Shameless copy from Sylpheed's gtkutils.c */
2313 color->red = (int) (((gdouble)((rgbvalue & 0xff0000) >> 16) / 255.0)
2315 color->green = (int) (((gdouble)((rgbvalue & 0x00ff00) >> 8) / 255.0)
2317 color->blue = (int) (((gdouble) (rgbvalue & 0x0000ff) / 255.0)
2321 gtk_text_buffer_create_tag(buffer, "misspelled",
2322 "foreground-gdk", color, NULL);
2324 gtk_text_buffer_create_tag(buffer, "misspelled",
2325 "underline", PANGO_UNDERLINE_ERROR, NULL);
2329 static void change_color(GtkAspell * gtkaspell,
2330 gint start, gint end,
2334 GtkTextView *gtktext;
2335 GtkTextBuffer *buffer;
2336 GtkTextIter startiter, enditer;
2341 gtktext = gtkaspell->gtktext;
2343 buffer = gtk_text_view_get_buffer(gtktext);
2344 gtk_text_buffer_get_iter_at_offset(buffer, &startiter, start);
2345 gtk_text_buffer_get_iter_at_offset(buffer, &enditer, end);
2347 gtk_text_buffer_apply_tag_by_name(buffer, "misspelled",
2348 &startiter, &enditer);
2350 gtk_text_iter_forward_char(&enditer);
2351 gtk_text_buffer_remove_tag_by_name(buffer, "misspelled",
2352 &startiter, &enditer);
2356 /* convert_to_aspell_encoding () - converts ISO-8859-* strings to iso8859-*
2357 * as needed by aspell. Returns an allocated string.
2360 static guchar *convert_to_aspell_encoding (const guchar *encoding)
2362 guchar * aspell_encoding;
2364 if (strstr2(encoding, "ISO-8859-")) {
2365 aspell_encoding = g_strdup_printf("iso8859%s", encoding+8);
2368 if (!strcmp2(encoding, "US-ASCII"))
2369 aspell_encoding = g_strdup("iso8859-1");
2371 aspell_encoding = g_strdup(encoding);
2374 return aspell_encoding;
2377 /* compare_dict () - compare 2 dict names */
2378 static gint compare_dict(Dictionary *a, Dictionary *b)
2380 guint aparts = 0, bparts = 0;
2383 for (i=0; i < strlen(a->dictname); i++)
2384 if (a->dictname[i] == '-')
2386 for (i=0; i < strlen(b->dictname); i++)
2387 if (b->dictname[i] == '-')
2390 if (aparts != bparts)
2391 return (aparts < bparts) ? -1 : +1;
2394 compare = strcmp2(a->dictname, b->dictname);
2396 compare = strcmp2(a->fullname, b->fullname);
2401 static void dictionary_delete(Dictionary *dict)
2403 g_free(dict->fullname);
2404 g_free(dict->encoding);
2408 static Dictionary *dictionary_dup(const Dictionary *dict)
2412 dict2 = g_new(Dictionary, 1);
2414 dict2->fullname = g_strdup(dict->fullname);
2415 dict2->dictname = dict->dictname - dict->fullname + dict2->fullname;
2416 dict2->encoding = g_strdup(dict->encoding);
2421 static void free_suggestions_list(GtkAspell *gtkaspell)
2425 for (list = gtkaspell->suggestions_list; list != NULL;
2429 g_list_free(gtkaspell->suggestions_list);
2431 gtkaspell->max_sug = -1;
2432 gtkaspell->suggestions_list = NULL;
2435 static void reset_theword_data(GtkAspell *gtkaspell)
2437 gtkaspell->start_pos = 0;
2438 gtkaspell->end_pos = 0;
2439 gtkaspell->theword[0] = 0;
2440 gtkaspell->max_sug = -1;
2442 free_suggestions_list(gtkaspell);
2445 static void free_checkers(gpointer elt, gpointer data)
2447 GtkAspeller *gtkaspeller = elt;
2449 g_return_if_fail(gtkaspeller);
2451 gtkaspeller_real_delete(gtkaspeller);
2454 static gint find_gtkaspeller(gconstpointer aa, gconstpointer bb)
2456 Dictionary *a = ((GtkAspeller *) aa)->dictionary;
2457 Dictionary *b = ((GtkAspeller *) bb)->dictionary;
2459 if (a && b && a->fullname && b->fullname &&
2460 strcmp(a->fullname, b->fullname) == 0 &&
2461 a->encoding && b->encoding)
2462 return strcmp(a->encoding, b->encoding);