edit gtkspell.c so it complies a little bit more with main branch coding style
[claws.git] / src / gtkspell.c
index 3b9517ebc5ca3f7f12d813cb0335ae406b836c79..ddceea9f0567e170d8a4a4182e4d8f9624883c62 100644 (file)
@@ -1,4 +1,4 @@
-/* gtkspell - a spell-checking addon for GtkText
+/* gtkpspell - a spell-checking addon for GtkText
  * Copyright (c) 2000 Evan Martin.
  *
  * This library is free software; you can redistribute it and/or
  * License along with this library; if not, write to the Free Software
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
  */
-/*
-    Stuphead: (C) 2000,2001 Grigroy Bakunov, Sergey Pinaev
- */
 
 /*
+ * Stuphead: (C) 2000,2001 Grigroy Bakunov, Sergey Pinaev
  * Adapted for Sylpheed (Claws) (c) 2001 by Hiroyuki Yamamoto & 
  * The Sylpheed Claws Team.
+ * Adapted for pspell (c) 2001 Melvin Hadasht
  */
-
 #if defined(HAVE_CONFIG_H)
 #include "config.h"
 #endif
 
+#if USE_PSPELL
 #include "intl.h"
 
 #include <gtk/gtk.h>
 
 #include "gtkspell.h"
 
+#include <pspell/pspell.h>
+
 /* size of the text buffer used in various word-processing routines. */
 #define BUFSIZE 1024
 
 /* number of suggestions to display on each menu. */
 #define MENUCOUNT 15
 
-/* because we keep only one copy of the spell program running,
- * all ispell-related variables can be static.
- */
-volatile pid_t spell_pid = -1;
-static int sp_fd_write[2], sp_fd_read[2];
-static int signal_set_up = 0;
+/******************************************************************************/
+
+/* Function called from menus */
+
+static void add_word_to_session                (GtkWidget *w, GtkPspell *d);
+static void add_word_to_personal       (GtkWidget *w, GtkPspell *d);
+static void set_sug_mode               (GtkWidget *w, GtkPspell *gtkpspell);
+static void check_all                  (GtkWidget *w, GtkPspell *gtkpspell);
+static void menu_change_dict           (GtkWidget *w, GtkPspell *gtkpspell);
+static void entry_insert_cb            (GtkXText *gtktext, gchar *newtext, 
+                                        guint len, guint *ppos, 
+                                        GtkPspell *gtkpspell);
 
+/* gtkspellconfig - only one config per session */
+GtkPspellConfig * gtkpspellconfig;
+
+/* TODO: configurable */
 static GdkColor highlight = { 0, 255 * 256, 0, 0 };
 
-static void entry_insert_cb(GtkXText *gtktext,
-                            gchar *newtext, guint len, guint *ppos, gpointer d);
-static void set_up_signal();
+/******************************************************************************/
 
-int gtkspell_running() 
+/* gtkspell_init() - run the first pspell_config from which every
+ * new config is cloned 
+ */
+GtkPspellConfig * gtkpspell_init()
 {
-    return (spell_pid > 0);
+       return new_pspell_config();
 }
 
-/* functions to interface with pipe */
-static void writetext(unsigned char *text_ccc) 
+/* gtkspell_finished() - Finish all. No more spelling. Called when the 
+ * program ends 
+ */
+void gtkpspell_finished(GtkPspellConfig *gtkpspellconfig)
 {
-    write(sp_fd_write[1], text_ccc, strlen(text_ccc));
+       if (gtkpspellconfig) {
+               delete_pspell_config(gtkpspellconfig);
+               gtkpspellconfig = NULL;
+       }
 }
 
-static int readpipe(unsigned char *buf, int bufsize) 
+/* gtkspell_running - Test if there is a manager running 
+ */
+int gtkpspell_running(GtkPspell * gtkpspell) 
 {
-    int len;
-    len = read(sp_fd_read[0], buf, bufsize - 1);
-    if (len < 0) {
-        debug_print(_("*** readpipe: read: %s\n"), strerror(errno));
-        return -1;
-    } else if (len == 0) {
-        debug_print(_("*** readpipe: pipe closed.\n"));
-        return -1;
-    } else if (len == bufsize - 1) {
-        debug_print(_("*** readpipe: buffer overflowed?\n"));
-    }
-
-    buf[len] = 0;
-    return len;
+       return (gtkpspell->config!=NULL);
 }
 
-static int readline(unsigned char *buf) 
+/* gtkspell_new() - creates a new session if a gtkpspellconfig exists. The 
+ * settings are defaults.  If no path/dict is set afterwards, the default 
+ * one is used  
+ */
+GtkPspell * gtkpspell_new(GtkPspellConfig *gtkpspellconfig)
 {
-    return readpipe(buf, BUFSIZE);
+       GtkPspell *gtkpspell;
+
+       if (gtkpspellconfig == NULL) {
+               gtkpspellconfig = gtkpspell_init();
+               if (gtkpspellconfig == NULL) {
+                       debug_print(_("Pspell could not be started."));
+                       prefs_common.enable_pspell=FALSE;
+                       return NULL;
+               }
+       }
+
+       gtkpspell               = g_new(GtkPspell ,1);
+       gtkpspell->config       = gtkpspellconfig;
+       gtkpspell->possible_err = new_pspell_manager(gtkpspell->config);
+       gtkpspell->checker      = NULL;
+
+       if (pspell_error_number(gtkpspell->possible_err) != 0) {
+               debug_print(_("Pspell error : %s\n"), pspell_error_message(gtkpspell->possible_err));
+               delete_pspell_can_have_error( gtkpspell->possible_err );
+       }
+       else {
+               gtkpspell->checker = to_pspell_manager(gtkpspell->possible_err);
+       }
+       gtkpspell->dictionary_list = NULL;
+       gtkpspell->path            = NULL;
+       gtkpspell->dict            = NULL;
+       gtkpspell->mode            = PSPELL_FASTMODE;
+       gtkpspell->gtktext         = NULL;
+       return gtkpspell;
 }
 
-static int readresponse(unsigned char *buf) 
+/* gtkspell_new_with_config() - Creates a new session and set path/dic 
+ */
+GtkPspell *gtkpspell_new_with_config(GtkPspellConfig *gtkpspellconfig, 
+                                     guchar *path, guchar *dict, 
+                                     guint mode, guchar *encoding)
 {
-    int len;
-    len = readpipe(buf, BUFSIZE);
+       GtkPspell *gtkpspell;
+
+       if (gtkpspellconfig == NULL) {
+               gtkpspellconfig=gtkpspell_init();
+               if (gtkpspellconfig == NULL) {
+                       debug_print(_("Pspell could not be started."));
+                       prefs_common.enable_pspell = FALSE;
+                       return NULL;
+               }
+       }
 
-    /* all ispell responses of any reasonable length should end in \n\n.
-     * depending on the speed of the spell checker, this may require more
-     * reading. */
-    if (len >= 2 && (buf[len - 1] != '\n' || buf[len - 2] != '\n')) {
-        len += readpipe(buf + len, BUFSIZE - len);
-    }
+       gtkpspell                  = g_new( GtkPspell ,1);
+       gtkpspell->path            = NULL;
+       gtkpspell->dict            = NULL;
+       gtkpspell->dictionary_list = NULL;
+       gtkpspell->gtktext         = NULL;
+  
+       gtkpspell->config          = pspell_config_clone(gtkpspellconfig);
+       gtkpspell->mode            = PSPELL_FASTMODE;
+       
+       if (!set_path_and_dict(gtkpspell, gtkpspell->config, path, dict)) {
+               debug_print(_("Pspell could not be configured."));
+               gtkpspell = gtkpspell_delete(gtkpspell);
+               return gtkpspell;
+       }
+       
+       if (encoding)
+               pspell_config_replace(gtkpspell->config,"encoding",encoding);
 
-    /* now we can remove all of the the trailing newlines. */
-    while (len > 0 && buf[len - 1] == '\n')
-        buf[--len] = 0;
+       gtkpspell->possible_err = new_pspell_manager(gtkpspell->config);
+       gtkpspell->checker      = NULL;
 
-    return len;
+       if (pspell_error_number(gtkpspell->possible_err) != 0) {
+               debug_print(_("Pspell error : %s\n"), pspell_error_message(gtkpspell->possible_err));
+               delete_pspell_can_have_error(gtkpspell->possible_err);
+               gtkpspell = gtkpspell_delete(gtkpspell);
+       }
+       else {
+               gtkpspell->checker = to_pspell_manager( gtkpspell->possible_err );
+       }
+       return gtkpspell;
 }
 
-void gtkspell_stop() 
+/* gtkspell_delete() - Finishes a session 
+ */
+GtkPspell *gtkpspell_delete( GtkPspell *gtkpspell )
 {
-    if (gtkspell_running()) {
-        kill(spell_pid, SIGTERM);
-        debug_print(_("*** Kill pid[%i] returned: %s\n"), spell_pid, strerror(errno));
-        while (spell_pid != -1);
-    }
-}
+       if ( gtkpspell->checker ) {
+               delete_pspell_manager(gtkpspell->checker);
+               gtkpspell->checker      = NULL;
+               gtkpspell->possible_err = NULL; /* checker is a cast from possible_err */
+       }
 
-static int poller(int buffer) 
+       if (gtkpspell->dictionary_list)
+               gtkpspell_free_dictionary_list(gtkpspell->dictionary_list);
+
+       g_free(gtkpspell->path);
+       g_free(gtkpspell->dict);
+       gtkpspell->path = NULL;
+       gtkpspell->dict = NULL;
+
+       g_free(gtkpspell);
+       return NULL;
+}
+  
+int set_path_and_dict(GtkPspell *gtkpspell, PspellConfig *config,
+                     guchar *path, guchar * dict)
 {
-    int len;
-    fd_set rfds;
-    struct timeval tv;
-    int retval;
+       guchar *module   = NULL;
+       guchar *language = NULL;
+       guchar *spelling = NULL;
+       guchar *jargon   = NULL;
+       guchar  buf[BUFSIZE];
+       guchar *end;
+       guchar *temppath;
+       guchar *tempdict;
+
+       /* Change nothing if any of path/dict/config is NULL */
+       g_return_val_if_fail(path, 0);
+       g_return_val_if_fail(dict, 0);
+       g_return_val_if_fail(config, 0);
+       
+       /* This is done, so we can free gtkpspell->path, even if it was
+        * given as an argument of the function */
+       temppath = g_strdup(path);
+       g_free(gtkpspell->path);
+  
+       /* pspell dict name format :                         */
+       /*   <lang>[[-<spelling>[-<jargon>]]-<module>.pwli   */
+       /* Strip off path                                    */
+
+       if (!strrchr(dict, G_DIR_SEPARATOR)) { /* plain dict name */
+               strncpy(buf, dict, BUFSIZE - 1);
+       }
+       else { /* strip path */
+               strncpy(buf, strrchr(dict, G_DIR_SEPARATOR) + 1, BUFSIZE - 1);
+       }
+       g_free(gtkpspell->dict);
+
+       /* Ensure no buffers overflows if the dict is to long */
+       buf[BUFSIZE - 1] = 0x00;
+  
+       language = buf;
+       if ((module = strrchr(buf,'-')) != NULL) {
+               module++;
+               if ((end = strrchr(module,'.')) != NULL)
+                       *end = 0;
+       }
 
-    FD_ZERO(&rfds);
-    FD_SET(buffer, &rfds);
-    memset(&tv, 0, sizeof(tv));
-    tv.tv_sec = 2;
-    tv.tv_usec = 0;
-    return select(buffer + 1, &rfds, NULL, NULL, &tv);
+       /* In tempdict, we have only the dict name, without path nor
+         * extension. Useful in the popup menus */
+
+       tempdict = g_strdup(buf);
+
+       /* Probably I am too paranoied... */
+       if (!( language[0] != 0x00 && language[1] != 0x00))
+               language = NULL;
+       else {
+               spelling = strchr(language,'-');
+               if (spelling!=NULL) {
+                       *spelling = 0;
+                       spelling++;
+               }
+               if (spelling != module) {
+                       if ((end = strchr(spelling, '-' )) != NULL) {
+                               *end = 0;
+                               jargon = end + 1;
+                               if (jargon != module)
+                                       if ((end = strchr(jargon, '-')) != NULL)
+                                               *end = 0;
+                                       else
+                                               jargon = NULL;
+                               else
+                                       jargon = NULL;
+                       }
+                       else
+                               spelling = NULL;
+               }
+               else 
+                       spelling = NULL;
+       }
+
+       debug_print(_("Language : %s\nSpelling: %s\nJargon: %s\nModule: %s\n"),
+                   language, spelling, jargon, module);
+
+       if (language) 
+               pspell_config_replace(config, "language-tag", language);
+       if (spelling) 
+               pspell_config_replace(config, "spelling", spelling);
+       if (jargon)
+               pspell_config_replace(config, "jargon", jargon);
+       if (module)
+               pspell_config_replace(config, "module", module);
+       if (temppath)
+               pspell_config_replace(config, "word-list-path", temppath);
+
+       switch(gtkpspell->mode) {
+       case PSPELL_FASTMODE: 
+               pspell_config_replace(config, "sug_mode", "fast");
+               break;
+       case PSPELL_NORMALMODE: 
+               pspell_config_replace(config, "sug_mode", "normal");
+               break;
+       case PSPELL_BADSPELLERMODE: 
+               pspell_config_replace(config, "sug_mode", "bad-spellers");
+               break;
+       }
+  
+       gtkpspell->path = g_strdup(temppath);
+       gtkpspell->dict = g_strdup(tempdict);
+       g_free(temppath);
+       g_free(tempdict);
+
+       return -1;
 }
 
-int gtkspell_start(unsigned char *path, char * args[]) 
+/* gtkpspell_set_path_and_dict() - Set path and dict. The session is 
+ * resetted. 
+ * 0 on error, -1 on success */
+int gtkpspell_set_path_and_dict(GtkPspell * gtkpspell, guchar * path, 
+                               guchar * dict)
 {
-    int fd_error[2];
-    FILE *sav_stdin, *sav_stdout, *sav_stderr;
-    char buf[BUFSIZE];
-    int retncode;
+       PspellConfig * config2;
 
-    if (gtkspell_running()) {
-        debug_print(_("*** gtkspell_start called while already running.\n"));
-        gtkspell_stop();
-    }
+       /* It seems changing an already running config is not the way to go
+         */
 
-    if (!signal_set_up) {
-        set_up_signal();
-        signal_set_up = 1;
-    }
+       config2 = pspell_config_clone(gtkpspell->config);
 
-    pipe(sp_fd_write);
-    pipe(sp_fd_read);
-    pipe(fd_error);
-
-    spell_pid = fork();
-    if (spell_pid < 0) {
-        debug_print(_("*** fork: %s\n"), strerror(errno));
-        return -1;
-    } else if (spell_pid == 0) {
-        sav_stdin = fdopen(dup(fileno(stdin)), "r");
-        sav_stdout = fdopen(dup(fileno(stdout)), "w");
-        sav_stderr = fdopen(dup(fileno(stderr)), "w");
-        dup2(sp_fd_write[0], 0);
-        dup2(sp_fd_read[1], 1);
-        dup2(fd_error[1], 2);
-        close(sp_fd_read[0]);
-        close(fd_error[0]);
-        close(sp_fd_write[1]);
-
-        if (path == NULL) {
-            if (execvp(args[0], args) < 0)
-                //DONT call debug_print here, because stdout is closed at this moment
-                fprintf(sav_stderr, _("*** execvp('%s'): %s\n"), args[0], strerror(errno));
-        } else {
-            if (execv(path, args) < 0)
-                //DONT call debug_print here, because stdout is closed at this moment
-                fprintf(sav_stderr, _("*** execv('%s'): %s\n"), path, strerror(errno));
-        }
-        /* if we get here, we failed.
-         * send some text on the pipe to indicate status.
-         */
-        write(sp_fd_read[1], "!", 1);
-
-        _exit(0);
-    } else {
-        retncode = poller(sp_fd_read[1]);
-        if (retncode < 0) {
-            debug_print(_("*** Spell comand failed: %s.\n"), strerror(errno));
-            gtkspell_stop();
-            return -1;
-        }
-        readline(buf);
-        /* ispell should print something like this:
-         * @(#) International Ispell Version 3.1.20 10/10/95
-         * if it doesn't, it's an error. */
-        if (buf[0] != '@') {
-            debug_print(_("*** ispell didnt print '@'\n"));
-            gtkspell_stop();
-            return -1;
-        }
-    }
+       if (gtkpspell->checker) 
+               delete_pspell_manager(gtkpspell->checker);
+       
+       gtkpspell->checker      = NULL;
+       gtkpspell->possible_err = NULL;
 
-    /* put ispell into terse mode.
-     * this makes it not respond on correctly spelled words. */
-    sprintf(buf, "!\n");
-    writetext(buf);
-    return 0;
-}
-
-static GList* misspelled_suggest(unsigned char *word) 
-{
-    unsigned char buf[BUFSIZE];
-    unsigned char *newword;
-    GList *l = NULL;
-    int count;
-    sprintf(buf, "^%s\n", word);  /* guard against ispell control chars */
-    writetext(buf);
-    readresponse(buf);
-    switch (buf[0]) { /* first char is ispell command. */
-    case 0:  /* no response: word is ok. */
-        return NULL;
-    case 10:  /* just enter word is ok.  */
-        return NULL;
-    case '&':  /* misspelled, with suggestions */
-        /* & <orig> <count> <ofs>: <miss>, <miss>, <guess>, ... */
-        strtok(buf, " ");  /* & */
-        newword = strtok(NULL, " ");  /* orig */
-        l = g_list_append(l, g_strdup(newword));
-        newword = strtok(NULL, " ");  /* count */
-        count = atoi(newword);
-        strtok(NULL, " ");  /* ofs: */
-
-        while ((newword = strtok(NULL, ",")) != NULL) {
-            int len = strlen(newword);
-            if (newword[len - 1] == ' ' || newword[len - 1] == '\n')
-                newword[len - 1] = 0;
-            if (count == 0) {
-                g_list_append(l, NULL);  /* signal the "suggestions" */
-            }
-            /* add it to the list, skipping the initial space. */
-            l = g_list_append(l,
-                              g_strdup(newword[0] == ' ' ? newword + 1 : newword));
-
-            count--;
-        }
-        return l;
-    case '?':  /* ispell is guessing. */
-    case '#':  /* misspelled, no suggestions */
-        /* # <orig> <ofs> */
-        strtok(buf, " ");  /* & */
-        newword = strtok(NULL, " ");  /* orig */
-        l = g_list_append(l, g_strdup(newword));
-        return l;
-    default:
-        debug_print(_("*** Unsupported spell command '%c'.\n"), buf[0]);
-    }
-    return NULL;
+       if (set_path_and_dict(gtkpspell,config2,path,dict) == 0) {
+               debug_print(_("Pspell set_path_and_dict error."));
+               return 0;
+       }
+  
+       gtkpspell->possible_err = new_pspell_manager(config2);
+
+       delete_pspell_config(config2);
+       config2 = NULL;
+
+       if (pspell_error_number(gtkpspell->possible_err) != 0) {
+               debug_print(_("Pspell path & dict. error %s\n"),
+                           pspell_error_message(gtkpspell->possible_err));
+               delete_pspell_can_have_error(gtkpspell->possible_err);
+               gtkpspell->possible_err = NULL;
+               return 0;
+       }
+
+       gtkpspell->checker=to_pspell_manager(gtkpspell->possible_err);
+
+       return -1;
+}
+  
+/* gtkpspell_get_dict() - What dict are we using ? language-spelling-jargon-module format */
+/* Actually, this function is not used and hence not tested. */
+/* Returns an allocated string */  
+guchar *gtkpspell_get_dict(GtkPspell *gtkpspell)
+{
+       guchar *dict;
+       guchar *language;
+       guchar *spelling;
+       guchar *jargon;
+       guint   len;
+
+       g_return_val_if_fail(gtkpspell->config, NULL);
+  
+       language = g_strdup(pspell_config_retrieve(gtkpspell->config, "language"));
+       spelling = g_strdup(pspell_config_retrieve(gtkpspell->config, "spelling"));
+       jargon   = g_strdup(pspell_config_retrieve(gtkpspell->config, "jargon"  ));
+       len      = strlen(language) + strlen(spelling) + strlen(jargon);
+
+       if (len < BUFSIZE) {
+               dict = g_new(char,len + 4);
+               strcpy(dict, language);
+               if (spelling) {
+                       strcat(dict, "-");
+                       strcat(dict, spelling);
+                       if (jargon) {
+                               strcat(dict, "-");
+                               strcat(dict,jargon);
+                       }
+               }
+       }
+       g_free(language);
+       g_free(spelling);
+       g_free(jargon);
+  
+       return dict;
+}
+  
+/* gtkpspell_get_path() - Return the dict path as an allocated string */
+/* Not used = not tested */
+guchar *gtkpspell_get_path(GtkPspell *gtkpspell)
+{
+       guchar * path;
+
+
+       g_return_val_if_fail(gtkpspell->config, NULL);
+
+       path = g_strdup(pspell_config_retrieve(gtkpspell->config,"word-list-path"));
+
+       return path;
 }
 
-static int misspelled_test(unsigned char *word) 
+/* menu_change_dict() - Menu callback : change dict */
+static void menu_change_dict(GtkWidget *w, GtkPspell *gtkpspell)
 {
-    unsigned char buf[BUFSIZE];
-    sprintf(buf, "^%s\n", word);  /* guard against ispell control chars */
-    writetext(buf);
-    readresponse(buf);
+       guchar *thedict,
+              *thelabel;
+  
+       /* Dict is simply the menu label */
 
-    if (buf[0] == 0) {
-        return 0;
-    } else if (buf[0] == '&' || buf[0] == '#' || buf[0] == '?') {
-        return 1;
-    }
+       gtk_label_get(GTK_LABEL(GTK_BIN(w)->child), (gchar **) &thelabel);
+       thedict = g_strdup(thelabel);
+
+       /* Set path, dict, (and sug_mode ?) */
+       gtkpspell_set_path_and_dict(gtkpspell, gtkpspell->path, thedict);
+       g_free(thedict);
+}
+
+/* set_sug_mode() - Menu callback : Set the suggestion mode */
+static void set_sug_mode(GtkWidget *w, GtkPspell *gtkpspell)
+{
+       unsigned char *themode;
+
+       gtk_label_get(GTK_LABEL(GTK_BIN(w)->child), (gchar **) &themode);
 
-    debug_print(_("*** Unsupported spell command '%c'.\n"), buf[0]);
+       if (!strcmp(themode, _("Fast Mode"))) {
+               gtkpspell_set_sug_mode(gtkpspell, "fast");
+               gtkpspell->mode = PSPELL_FASTMODE;
+       }
+       if (!strcmp(themode,_("Normal Mode"))) {
+               gtkpspell_set_sug_mode(gtkpspell, "normal");
+               gtkpspell->mode = PSPELL_NORMALMODE;
+       }
+       if (!strcmp( themode,_("Bad Spellers Mode"))) {
+               gtkpspell_set_sug_mode(gtkpspell, "bad-spellers");
+               gtkpspell->mode = PSPELL_BADSPELLERMODE;
+       }
+}
+  
+/* gtkpspell_set_sug_mode() - Set the suggestion mode */
+/* Actually, the session is resetted and everything is reset. */
+/* We take the used path/dict pair and create a new config with them */
+int gtkpspell_set_sug_mode(GtkPspell *gtkpspell, gchar *themode)
+{
+       PspellConfig *config2;
+       guchar       *path;
+       guchar       *dict;
+
+       delete_pspell_manager(gtkpspell->checker);
+       gtkpspell->checker = NULL;
+
+       config2 = pspell_config_clone(gtkpspell->config);
 
-    return -1;
+       if (!set_path_and_dict(gtkpspell, config2, gtkpspell->path, gtkpspell->dict)) {
+               debug_print(_("Pspell set_sug_mod could not reset path & dict\n"));
+               return 0;
+       }
+
+       pspell_config_replace(config2, "sug-mode", themode);
+
+       gtkpspell->possible_err = new_pspell_manager(config2);
+       delete_pspell_config(config2);
+       config2 = NULL;
+
+       if (pspell_error_number(gtkpspell->possible_err) != 0) {
+               debug_print(_("Pspell set sug-mode error %s\n"),
+                           pspell_error_message(gtkpspell->possible_err));
+               delete_pspell_can_have_error(gtkpspell->possible_err);
+               gtkpspell->possible_err = NULL;
+               return 0;
+       }
+       gtkpspell->checker = to_pspell_manager(gtkpspell->possible_err);
+       return -1;
+}
+  
+/* misspelled_suggest() - Create a suggestion list for  word  */
+static GList *misspelled_suggest(GtkPspell *gtkpspell, guchar *word) 
+{
+       const guchar          *newword;
+       GList                 *list = NULL;
+       int                    count;
+       const PspellWordList  *suggestions;
+       PspellStringEmulation *elements;
+
+       g_return_val_if_fail(word, NULL);
+
+       if (!pspell_manager_check(gtkpspell->checker, word, -1)) {
+               suggestions = pspell_manager_suggest(gtkpspell->checker, (const char *)word, -1);
+               elements    = pspell_word_list_elements(suggestions);
+               /* First one must be the misspelled (why this ?) */
+               list        = g_list_append(list, g_strdup(word)); 
+               
+               while ((newword = pspell_string_emulation_next(elements)) != NULL)
+                       list = g_list_append(list, g_strdup(newword));
+               return list;
+       }
+       return NULL;
+}
+
+/* misspelled_test() - Just test if word is correctly spelled */  
+static int misspelled_test(GtkPspell *gtkpspell, unsigned char *word) 
+{
+       return pspell_manager_check(gtkpspell->checker, word, -1) ? 0 : 1; 
 }
 
+
 static gboolean iswordsep(unsigned char c) 
 {
-    return !isalpha(c) && c != '\'';
+       return !isalpha(c) && c != '\'';
 }
 
-static guchar get_text_index_whar(GtkXText *gtktext, int pos) 
+static guchar get_text_index_whar(GtkPspell *gtkpspell, int pos) 
 {
-    guchar a;
-    gchar *text;
-    text = gtk_editable_get_chars(GTK_EDITABLE(gtktext), pos, pos + 1);
-    if (text == NULL) return 0;
-    a = (guchar) * text;
-    g_free(text);
-    return a;
+       guchar a;
+       gchar *text;
+       
+       text = gtk_editable_get_chars(GTK_EDITABLE(gtkpspell->gtktext), pos, pos + 1);
+       if (text == NULL) 
+               return 0;
+       a = (guchar) *text;
+       g_free(text);
+       return a;
 }
 
-static gboolean get_word_from_pos(GtkXText *gtktext, int pos, unsigned char* buf,
+static gboolean get_word_from_pos(GtkPspell *gtkpspell, int pos, 
+                                  unsigned char* buf,
                                   int *pstart, int *pend) 
 {
-    gint start, end;
-    if (iswordsep(get_text_index_whar(gtktext, pos))) return FALSE;
+       gint start, end;
+       GtkXText *gtktext;
+       
+       gtktext = gtkpspell->gtktext;
+       if (iswordsep(get_text_index_whar(gtkpspell, pos))) 
+               return FALSE;
 
-    for (start = pos; start >= 0; --start) {
-        if (iswordsep(get_text_index_whar(gtktext, start))) break;
-    }
-    start++;
+       for (start = pos; start >= 0; --start) {
+               if (iswordsep(get_text_index_whar(gtkpspell, start))) 
+                       break;
+       }
+       start++;
 
-    for (end = pos; end < gtk_xtext_get_length(gtktext); end++) {
-        if (iswordsep(get_text_index_whar(gtktext, end))) break;
-    }
+       for (end = pos; end < gtk_xtext_get_length(gtktext); end++) {
+               if (iswordsep(get_text_index_whar(gtkpspell, end))) 
+                       break;
+       }
 
-    if (buf) {
-        for (pos = start; pos < end; pos++) buf[pos - start] = get_text_index_whar(gtktext, pos);
-        buf[pos - start] = 0;
-    }
+       if (buf) {
+               for (pos = start; pos < end; pos++) 
+                       buf[pos - start] = get_text_index_whar(gtkpspell, pos);
+               buf[pos - start] = 0;
+       }
 
-    if (pstart) *pstart = start;
-    if (pend) *pend = end;
+       if (pstart) 
+               *pstart = start;
+       if (pend) 
+               *pend = end;
 
-    return TRUE;
+       return TRUE;
 }
 
-static gboolean get_curword(GtkXText *gtktext, unsigned char* buf,
+static gboolean get_curword(GtkPspell *gtkpspell, unsigned char* buf,
                             int *pstart, int *pend) 
 {
-    int pos = gtk_editable_get_position(GTK_EDITABLE(gtktext));
-    return get_word_from_pos(gtktext, pos, buf, pstart, pend);
+       int pos = gtk_editable_get_position(GTK_EDITABLE(gtkpspell->gtktext));
+       return get_word_from_pos(gtkpspell, pos, buf, pstart, pend);
 }
 
-static void change_color(GtkXText *gtktext, int start, int end, GdkColor *color) 
+static void change_color(GtkPspell * gtkpspell, 
+                        int start, int end, 
+                         GdkColor *color) 
 {
-    char *newtext;
-    if (start >= end) {
-        return ;
-    };
-    gtk_xtext_freeze(gtktext);
-    newtext = gtk_editable_get_chars(GTK_EDITABLE(gtktext), start, end);
-//    if (prefs_common.auto_makeispell) {
-        gtk_signal_handler_block_by_func(GTK_OBJECT(gtktext),
-                                         GTK_SIGNAL_FUNC(entry_insert_cb), NULL);
-//    }
-    gtk_xtext_set_point(gtktext, start);
-    gtk_xtext_forward_delete(gtktext, end - start);
-
-    if (newtext && end - start > 0)
-        gtk_xtext_insert(gtktext, NULL, color, NULL, newtext, end - start);
-  //  if (prefs_common.auto_makeispell) {
-        gtk_signal_handler_unblock_by_func(GTK_OBJECT(gtktext),
-                                           GTK_SIGNAL_FUNC(entry_insert_cb), NULL);
-   // }
-    gtk_xtext_thaw(gtktext);
-}
-
-static gboolean check_at(GtkXText *gtktext, int from_pos) 
-{
-    int start, end;
-    unsigned char buf[BUFSIZE];
-    if (from_pos < 0) return FALSE;
-    if (!get_word_from_pos(gtktext, from_pos, buf, &start, &end)) {
-        return FALSE;
-    };
-    if (misspelled_test(buf)) {
-        if (highlight.pixel == 0) {
-            /* add an entry for the highlight in the color map. */
-            GdkColormap *gc = gtk_widget_get_colormap(GTK_WIDGET(gtktext));
-            gdk_colormap_alloc_color(gc, &highlight, FALSE, TRUE);
-            ;
-        }
-        change_color(gtktext, start, end, &highlight);
-        return TRUE;
-    } else {
-        change_color(gtktext, start, end,
-                     &(GTK_WIDGET(gtktext)->style->fg[0]));
-        return FALSE;
-    }
+       char     *newtext;
+       GtkXText *gtktext;
+
+       g_return_if_fail(start < end);
+    
+       gtktext = gtkpspell->gtktext;
+    
+       gtk_xtext_freeze(gtktext);
+       newtext = gtk_editable_get_chars(GTK_EDITABLE(gtktext), start, end);
+       if (newtext) {
+               gtk_signal_handler_block_by_func(GTK_OBJECT(gtktext),
+                                                GTK_SIGNAL_FUNC(entry_insert_cb), 
+                                                gtkpspell);
+               gtk_xtext_set_point(gtktext, start);
+               gtk_xtext_forward_delete(gtktext, end - start);
+
+               gtk_xtext_insert(gtktext, NULL, color, NULL, newtext, end - start);
+               gtk_signal_handler_unblock_by_func(GTK_OBJECT(gtktext),
+                                                  GTK_SIGNAL_FUNC(entry_insert_cb), 
+                                                  gtkpspell);
+       }
+       gtk_xtext_thaw(gtktext);
 }
 
-void gtkspell_check_all(GtkXText *gtktext
+static gboolean check_at(GtkPspell *gtkpspell, int from_pos
 {
-    guint origpos;
-    guint pos = 0;
-    guint len;
-    float adj_value;
+       int           start, end;
+       unsigned char buf[BUFSIZE];
+       GtkXText     *gtktext;
 
-    if (!gtkspell_running()) return ;
+       g_return_val_if_fail(from_pos >= 0, FALSE);
+    
+       gtktext = gtkpspell->gtktext;
+       if (!get_word_from_pos(gtkpspell, from_pos, buf, &start, &end))
+               return FALSE;
+       strncpy(gtkpspell->theword, buf, BUFSIZE - 1);
+       gtkpspell->theword[BUFSIZE - 1] = 0;
+
+       if (misspelled_test(gtkpspell, buf)) {
+               if (highlight.pixel == 0) {
+                       /* add an entry for the highlight in the color map. */
+                       GdkColormap *gc = gtk_widget_get_colormap(GTK_WIDGET(gtktext));
+                       gdk_colormap_alloc_color(gc, &highlight, FALSE, TRUE);
+               }
+               change_color(gtkpspell, start, end, &highlight);
+               return TRUE;
+       } else {
+               change_color(gtkpspell, start, end,
+                            &(GTK_WIDGET(gtktext)->style->fg[0]));
+               return FALSE;
+       }
+}
 
-    len = gtk_xtext_get_length(gtktext);
 
-    adj_value = gtktext->vadj->value;
-    gtk_xtext_freeze(gtktext);
-    origpos = gtk_editable_get_position(GTK_EDITABLE(gtktext));
-    while (pos < len) {
-        while (pos < len && iswordsep(get_text_index_whar(gtktext, pos)))
-            pos++;
-        while (pos < len && !iswordsep(get_text_index_whar(gtktext, pos)))
-            pos++;
-        if (pos > 0)
-            check_at(gtktext, pos - 1);
-    }
-    gtk_xtext_thaw(gtktext);
-    gtk_editable_set_position(GTK_EDITABLE(gtktext), origpos);
-    gtk_editable_select_region(GTK_EDITABLE(gtktext), origpos, origpos);
+static void check_all(GtkWidget *w, GtkPspell *gtkpspell)
+{
+       gtkpspell_check_all(gtkpspell);
+}
+
 
+void gtkpspell_check_all(GtkPspell *gtkpspell) 
+{
+       guint origpos;
+       guint pos = 0;
+       guint len;
+       float adj_value;
+       GtkXText *gtktext;
+
+       
+       if (!gtkpspell_running(gtkpspell)) return ;
+       gtktext = gtkpspell->gtktext;
+
+       len = gtk_xtext_get_length(gtktext);
+
+       adj_value = gtktext->vadj->value;
+       gtk_xtext_freeze(gtktext);
+       origpos = gtk_editable_get_position(GTK_EDITABLE(gtktext));
+       gtk_editable_set_position(GTK_EDITABLE(gtktext),0);
+       while (pos < len) {
+               while (pos < len && iswordsep(get_text_index_whar(gtkpspell, pos)))
+                       pos++;
+               while (pos < len && !iswordsep(get_text_index_whar(gtkpspell, pos)))
+                       pos++;
+               if (pos > 0)
+                       check_at(gtkpspell, pos - 1);
+       }
+       gtk_xtext_thaw(gtktext);
+       gtk_editable_set_position(GTK_EDITABLE(gtktext), origpos);
+       gtk_editable_select_region(GTK_EDITABLE(gtktext), origpos, origpos);
 }
 
-static void entry_insert_cb(GtkXText *gtktext,
-                            gchar *newtext, guint len, guint *ppos, gpointer d) 
+static void entry_insert_cb(GtkXText *gtktext, gchar *newtext, 
+                           guint len, guint *ppos, 
+                            GtkPspell * gtkpspell) 
 {
-    int origpos;
-    if (!gtkspell_running()) return ;
+       if (!gtkpspell_running(gtkpspell)) 
+               return;
+
+       if (iswordsep(newtext[0])) {
+               /* did we just end a word? */
+               if (*ppos >= 2) 
+                       check_at(gtkpspell, *ppos - 2);
+
+               /* did we just split a word? */
+               if (*ppos < gtk_xtext_get_length(gtktext))
+                       check_at(gtkpspell, *ppos + 1);
+       } else {
+               /* check as they type, *except* if they're typing at the end (the most
+                 * common case.
+                 */
+               if (*ppos < gtk_xtext_get_length(gtktext) 
+               &&  !iswordsep(get_text_index_whar(gtkpspell, *ppos)))
+                       check_at(gtkpspell, *ppos - 1);
+               }
 
-    gtk_signal_handler_block_by_func(GTK_OBJECT(gtktext),
-                                     GTK_SIGNAL_FUNC(entry_insert_cb),
-                                     NULL);
-    gtk_xtext_insert(GTK_XTEXT(gtktext), NULL,
-                    &(GTK_WIDGET(gtktext)->style->fg[0]), NULL, newtext, len);
+}
 
-    gtk_signal_handler_unblock_by_func(GTK_OBJECT(gtktext),
-                                       GTK_SIGNAL_FUNC(entry_insert_cb),
-                                       NULL);
-    gtk_signal_emit_stop_by_name(GTK_OBJECT(gtktext), "insert-text");
-    *ppos += len;
-    origpos = gtk_editable_get_position(GTK_EDITABLE(gtktext));
+static void entry_delete_cb(GtkXText *gtktext, gint start, gint end, 
+                           GtkPspell *gtkpspell) 
+{
+       int origpos;
+    
+       if (!gtkpspell_running(gtkpspell)) 
+               return;
+
+       origpos = gtk_editable_get_position(GTK_EDITABLE(gtktext));
+       check_at(gtkpspell, start - 1);
+       gtk_editable_set_position(GTK_EDITABLE(gtktext), origpos);
+       gtk_editable_select_region(GTK_EDITABLE(gtktext), origpos, origpos);
+       /* this is to *UNDO* the selection, in case they were holding shift
+         * while hitting backspace. */
+}
+
+static void replace_word(GtkWidget *w, GtkPspell *gtkpspell) 
+{
+       int             start, end,oldlen, newlen;
+       guint           origpos;
+       unsigned char  *newword;
+       unsigned char   buf[BUFSIZE];
+       guint           pos;
+       GtkXText       *gtktext;
+    
+       gtktext = gtkpspell->gtktext;
 
-    if (iswordsep(newtext[0])) {
-        /* did we just end a word? */
-        if (*ppos >= 2) check_at(gtktext, *ppos - 2);
+       gtk_xtext_freeze(GTK_XTEXT(gtktext));
+       origpos = gtkpspell->orig_pos;
+       pos     = origpos;
 
-        /* did we just split a word? */
-        if (*ppos < gtk_xtext_get_length(gtktext))
-            check_at(gtktext, *ppos + 1);
-    } else {
-        /* check as they type, *except* if they're typing at the end (the most
-         * common case.
-         */
-        if (*ppos < gtk_xtext_get_length(gtktext) &&
-                !iswordsep(get_text_index_whar(gtktext, *ppos)))
-            check_at(gtktext, *ppos - 1);
+       gtk_label_get(GTK_LABEL(GTK_BIN(w)->child), (gchar**) &newword);
+       newlen = strlen(newword);
+
+       get_curword(gtkpspell, buf, &start, &end);
+       oldlen = end - start;
+
+       gtk_xtext_set_point(GTK_XTEXT(gtktext), end);
+       gtk_xtext_backward_delete(GTK_XTEXT(gtktext), end - start);
+       gtk_xtext_insert(GTK_XTEXT(gtktext), NULL, NULL, NULL, newword, strlen(newword));
+    
+#if 0
+    if(end-start>0 /*&& gtkpspell->learn*/){ 
+      buf[end-start]=0x00; /* Just be sure the buffer is correct... */
+      /* Learn from common misspellings */
+      pspell_manager_store_replacement(gtkpspell->checker,buf,end-start,
+                                      newword,strlen(newword));
     }
+#endif
 
-    gtk_editable_set_position(GTK_EDITABLE(gtktext), origpos);
+       /* Put the point and the position where we clicked with the mouse */
+       /* It seems to be a hack, as I must thaw,freeze,thaw the widget   */
+       /* to let it update correctly the word insertion and then the     */
+       /* point & position position. If not, SEGV after the first replacement */
+       /* If the new word ends before point, put the point at its end*/
+    
+       if (origpos-start <= oldlen && origpos-start >= 0) {
+               /* Original point was in the word. */
+               /* Put the insertion point in the same location */
+               /* with respect to the new length */
+               /* If the original position is still within the word, */
+               /* then keep the original position. If not, move to the */
+               /* end of the word */
+               if (origpos-start > newlen)
+                       pos = start + newlen;
+       }
+       else if (origpos > end) {
+               /* move the position according to the change of length */
+               pos = origpos + newlen - oldlen;
+       }
+       gtk_xtext_thaw(GTK_XTEXT(gtktext));
+       gtk_xtext_freeze(GTK_XTEXT(gtktext));
+       if (GTK_XTEXT(gtktext)->text_len < pos)
+               pos = gtk_xtext_get_length(GTK_XTEXT(gtktext));
+       gtkpspell->orig_pos = pos;
+       gtk_editable_set_position(GTK_EDITABLE(gtktext), gtkpspell->orig_pos);
+       gtk_xtext_set_point(GTK_XTEXT(gtktext), 
+                           gtk_editable_get_position(GTK_EDITABLE(gtktext)));
+       gtk_xtext_thaw(GTK_XTEXT(gtktext));
 }
 
-static void entry_delete_cb(GtkXText *gtktext,
-                            gint start, gint end, gpointer d) 
+/* Accept this word for this session */
+
+static void add_word_to_session(GtkWidget *w, GtkPspell *gtkpspell)
 {
-    int origpos;
-    if (!gtkspell_running()) return ;
+       guint     pos;
+       GtkXText *gtkxtext;
+    
+       gtkxtext = gtkpspell->gtktext;
+       gtk_xtext_freeze(GTK_XTEXT(gtkxtext));
+       pos = gtk_editable_get_position(GTK_EDITABLE(gtkxtext));
+    
+       pspell_manager_add_to_session(gtkpspell->checker,gtkpspell->theword, strlen(gtkpspell->theword));
 
-    origpos = gtk_editable_get_position(GTK_EDITABLE(gtktext));
-    check_at(gtktext, start - 1);
-    gtk_editable_set_position(GTK_EDITABLE(gtktext), origpos);
-    gtk_editable_select_region(GTK_EDITABLE(gtktext), origpos, origpos);
-    /* this is to *UNDO* the selection, in case they were holding shift
-     * while hitting backspace. */
-}
+       check_at(gtkpspell,gtk_editable_get_position(GTK_EDITABLE(gtkxtext)));
 
-static void replace_word(GtkWidget *w, gpointer d) 
-{
-    int start, end;
-    unsigned char *newword;
-    unsigned char buf[BUFSIZE];
-    gtk_xtext_freeze(GTK_XTEXT(d));
+       gtk_xtext_thaw(GTK_XTEXT(gtkxtext));
+       gtk_xtext_freeze(GTK_XTEXT(gtkxtext));
+       gtk_editable_set_position(GTK_EDITABLE(gtkxtext),pos);
+       gtk_xtext_set_point(GTK_XTEXT(gtkxtext), gtk_editable_get_position(GTK_EDITABLE(gtkxtext)));
+       gtk_xtext_thaw(GTK_XTEXT(gtkxtext));
+}
 
-    gtk_label_get(GTK_LABEL(GTK_BIN(w)->child), (gchar**) &newword);
-    get_curword(GTK_XTEXT(d), buf, &start, &end);
+/* add_word_to_personal() - add word to personal dict. Save the list at every addition (may be
+ *  not efficient...) */
 
-    gtk_xtext_set_point(GTK_XTEXT(d), end);
-    gtk_xtext_backward_delete(GTK_XTEXT(d), end - start);
-    gtk_xtext_insert(GTK_XTEXT(d), NULL, NULL, NULL, newword, strlen(newword));
+static void add_word_to_personal(GtkWidget *w, GtkPspell *gtkpspell)
+{
+       guint     pos;
+       GtkXText *gtkxtext;
+    
+       gtkxtext = gtkpspell->gtktext;
 
-    gtk_xtext_thaw(GTK_XTEXT(d));
+       gtk_xtext_freeze(GTK_XTEXT(gtkxtext));
+       pos = gtk_editable_get_position(GTK_EDITABLE(gtkxtext));
+    
+       pspell_manager_add_to_personal(gtkpspell->checker,gtkpspell->theword,strlen(gtkpspell->theword));
+       pspell_manager_save_all_word_lists(gtkpspell->checker);
+    
+       check_at(gtkpspell,gtk_editable_get_position(GTK_EDITABLE(gtkxtext)));
+       gtk_xtext_thaw(GTK_XTEXT(gtkxtext));
+       gtk_xtext_freeze(GTK_XTEXT(gtkxtext));
+       gtk_editable_set_position(GTK_EDITABLE(gtkxtext),pos);
+       gtk_xtext_set_point(GTK_XTEXT(gtkxtext), gtk_editable_get_position(GTK_EDITABLE(gtkxtext)));
+       gtk_xtext_thaw(GTK_XTEXT(gtkxtext));
 }
 
-static GtkMenu *make_menu(GList *l, GtkXText *gtktext) 
+       
+static GtkMenu *make_menu_config(GtkPspell *gtkpspell)
 {
-    GtkWidget *menu, *item;
-    unsigned char *caption;
-    menu = gtk_menu_new(); 
-    
-        caption = g_strdup_printf(_("Not in dictionary: %s"), (unsigned char*)l->data);
-        item = gtk_menu_item_new_with_label(caption);
-        /* I'd like to make it so this item is never selectable, like
-         * the menu titles in the GNOME panel... unfortunately, the GNOME
-         * panel creates their own custom widget to do this! */
+       GtkWidget *menu, *item, *submenu;
+
+  
+       menu = gtk_menu_new();
+
+        item = gtk_menu_item_new_with_label(_("Spell check all"));
+        gtk_widget_show(item);
+        gtk_menu_append(GTK_MENU(menu), item);
+        gtk_signal_connect(GTK_OBJECT(item),"activate",
+                          GTK_SIGNAL_FUNC(check_all), 
+                          gtkpspell);
+
+
+        item = gtk_menu_item_new();
+        gtk_widget_show(item);
+        gtk_menu_append(GTK_MENU(menu),item);
+
+       submenu = gtk_menu_new();
+        item = gtk_menu_item_new_with_label(_("Change dictionary"));
         gtk_widget_show(item);
+        gtk_menu_item_set_submenu(GTK_MENU_ITEM(item),submenu);
         gtk_menu_append(GTK_MENU(menu), item);
 
+       /* Dict list */
+        if (gtkpspell->dictionary_list==NULL)
+               gtkpspell->dictionary_list = gtkpspell_get_dictionary_list(gtkpspell->path);
+       
+        {
+               GtkWidget * curmenu = submenu;
+               int count = 0;
+               Dictionary *dict;
+               GSList *tmp;
+               
+               tmp = gtkpspell->dictionary_list;
+               for (tmp = gtkpspell->dictionary_list; tmp != NULL; tmp =g_slist_next(tmp)) {
+                       dict = (Dictionary *) tmp->data;
+                       item = gtk_check_menu_item_new_with_label(dict->name);
+                       if (strcmp2(dict->name, gtkpspell->dict))
+                               gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), FALSE);
+                       else {
+                               gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), TRUE);
+                               gtk_widget_set_sensitive(GTK_WIDGET(item), FALSE);
+                       }
+                       gtk_menu_append(GTK_MENU(curmenu), item);
+                       gtk_signal_connect(GTK_OBJECT(item), "activate",
+                                          GTK_SIGNAL_FUNC(menu_change_dict),
+                                          gtkpspell);
+                       gtk_widget_show(item);
+                       count++;
+                       if (count == MENUCOUNT) {
+                               GtkWidget *newmenu;
+                               newmenu = gtk_menu_new();
+                               item = gtk_menu_item_new_with_label(_("More..."));
+                               gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), newmenu);
+                               gtk_menu_append(GTK_MENU(curmenu), item);
+                               gtk_widget_show(item);
+                               curmenu = newmenu;
+                               count = 0;
+                       }
+               }
+        }  
+
+        item = gtk_menu_item_new();
+        gtk_widget_show(item);
+        gtk_menu_append(GTK_MENU(menu), item);
+
+        item = gtk_check_menu_item_new_with_label(_("Fast Mode"));
+        gtk_widget_show(item);
+        gtk_menu_append(GTK_MENU(menu), item);
+        if (gtkpspell->mode == PSPELL_FASTMODE) {
+               gtk_widget_set_sensitive(GTK_WIDGET(item),FALSE);
+               gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item),TRUE);
+       }
+        else
+               gtk_signal_connect(GTK_OBJECT(item),"activate",
+                                  GTK_SIGNAL_FUNC(set_sug_mode),
+                                  gtkpspell);
+
+        item = gtk_check_menu_item_new_with_label(_("Normal Mode"));
+        gtk_widget_show(item);
+        gtk_menu_append(GTK_MENU(menu),item);
+        if (gtkpspell->mode == PSPELL_NORMALMODE) {
+               gtk_widget_set_sensitive(GTK_WIDGET(item), FALSE);
+               gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), TRUE);
+       }
+        else
+               gtk_signal_connect(GTK_OBJECT(item), "activate",
+                                  GTK_SIGNAL_FUNC(set_sug_mode),
+                                  gtkpspell);
+        
+        item = gtk_check_menu_item_new_with_label(_("Bad Spellers Mode"));
+       gtk_widget_show(item);
+        gtk_menu_append(GTK_MENU(menu),item);
+        if (gtkpspell->mode==PSPELL_BADSPELLERMODE) {
+               gtk_widget_set_sensitive(GTK_WIDGET(item), FALSE);
+               gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), TRUE);
+       }
+        else
+               gtk_signal_connect(GTK_OBJECT(item),"activate",
+                                  GTK_SIGNAL_FUNC(set_sug_mode),
+                                  gtkpspell);
+
+        return GTK_MENU(menu);
+}
+  
+/* make_menu() - Add menus to accept this word for this session and to add it to 
+ * personal dictionary */
+static GtkMenu *make_menu(GList *l, GtkPspell *gtkpspell) 
+{
+       GtkWidget *menu, *item;
+       unsigned char *caption;
+       GtkXText * gtktext;
+       
+       gtktext = gtkpspell->gtktext;
+
+       menu = gtk_menu_new(); 
+        caption = g_strdup_printf(_("Accept `%s' for this session"), (unsigned char*)l->data);
+       item = gtk_menu_item_new_with_label(caption);
+       g_free(caption);
+       gtk_widget_show(item);
+       gtk_menu_append(GTK_MENU(menu), item);
+
+        gtk_signal_connect(GTK_OBJECT(item), "activate",
+                          GTK_SIGNAL_FUNC(add_word_to_session), 
+                          gtkpspell);
+
+               caption = g_strdup_printf(_("Add `%s' to personal dictionary"), (char*)l->data);
+       item = gtk_menu_item_new_with_label(caption);
+       g_free(caption);
+       gtk_widget_show(item);
+       gtk_menu_append(GTK_MENU(menu), item);
+
+        gtk_signal_connect(GTK_OBJECT(item), "activate",
+                          GTK_SIGNAL_FUNC(add_word_to_personal), 
+                          gtkpspell);
+         
         item = gtk_menu_item_new();
         gtk_widget_show(item);
         gtk_menu_append(GTK_MENU(menu), item);
 
         l = l->next;
         if (l == NULL) {
-            item = gtk_menu_item_new_with_label(_("(no suggestions)"));
-            gtk_widget_show(item);
-            gtk_menu_append(GTK_MENU(menu), item);
+               item = gtk_menu_item_new_with_label(_("(no suggestions)"));
+               gtk_widget_show(item);
+               gtk_menu_append(GTK_MENU(menu), item);
         } else {
-            GtkWidget *curmenu = menu;
-            int count = 0;
-            do {
-                if (l->data == NULL && l->next != NULL) {
-                    count = 0;
-                    curmenu = gtk_menu_new();
-                    item = gtk_menu_item_new_with_label(_("Others..."));
-                    gtk_widget_show(item);
-                    gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), curmenu);
-                    gtk_menu_append(GTK_MENU(curmenu), item);
-                    l = l->next;
-                } else if (count > MENUCOUNT) {
-                    count -= MENUCOUNT;
-                    item = gtk_menu_item_new_with_label(_("More..."));
-                    gtk_widget_show(item);
-                    gtk_menu_append(GTK_MENU(curmenu), item);
-                    curmenu = gtk_menu_new();
-                    gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), curmenu);
-                }
-                item = gtk_menu_item_new_with_label((unsigned char*)l->data);
-                gtk_signal_connect(GTK_OBJECT(item), "activate",
-                                   GTK_SIGNAL_FUNC(replace_word), gtktext);
-                gtk_widget_show(item);
-                gtk_menu_append(GTK_MENU(curmenu), item);
-                count++;
-            } while ((l = l->next) != NULL);
-        }
-    return GTK_MENU(menu);
-}
-
-static void popup_menu(GtkXText *gtktext, GdkEventButton *eb) 
-{
-    unsigned char buf[BUFSIZE];
-    GList *list, *l;
-    if (!get_curword(gtktext, buf, NULL, NULL)) return ;
-    if (buf == NULL) return ;
-    list = misspelled_suggest(buf);
-    if (list != NULL) {
-        gtk_menu_popup(make_menu(list, gtktext), NULL, NULL, NULL, NULL,
-                       eb->button, eb->time);
-        for (l = list; l != NULL; l = l->next)
-            g_free(l->data);
-        g_list_free(list);
-    }
+               GtkWidget *curmenu = menu;
+               int count = 0;
+               
+               do {
+                       if (l->data == NULL && l->next != NULL) {
+                               count = 0;
+                               curmenu = gtk_menu_new();
+                               item = gtk_menu_item_new_with_label(_("Others..."));
+                               gtk_widget_show(item);
+                               gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), curmenu);
+                               gtk_menu_append(GTK_MENU(curmenu), item);
+                               l = l->next;
+                       } else if (count > MENUCOUNT) {
+                               count -= MENUCOUNT;
+                               item = gtk_menu_item_new_with_label(_("More..."));
+                               gtk_widget_show(item);
+                               gtk_menu_append(GTK_MENU(curmenu), item);
+                               curmenu = gtk_menu_new();
+                               gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), curmenu);
+                       }
+                       item = gtk_menu_item_new_with_label((unsigned char*)l->data);
+                       gtk_signal_connect(GTK_OBJECT(item), "activate",
+                                          GTK_SIGNAL_FUNC(replace_word), gtkpspell);
+                       gtk_widget_show(item);
+                       gtk_menu_append(GTK_MENU(curmenu), item);
+                       count++;
+               } while ((l = l->next) != NULL);
+       }
+       return GTK_MENU(menu);
+}
+
+static void popup_menu(GtkPspell *gtkpspell, GdkEventButton *eb) 
+{
+       unsigned char buf[BUFSIZE];
+       GList *list, *l;
+       GtkXText * gtktext;
+       
+       gtktext = gtkpspell->gtktext;
+       gtkpspell->orig_pos = gtk_editable_get_position(GTK_EDITABLE(gtktext));
+
+       if (!(eb->state & GDK_SHIFT_MASK))
+               if (get_curword(gtkpspell, buf, NULL, NULL)) {
+                       if (buf != NULL) {
+                               strncpy(gtkpspell->theword, buf, BUFSIZE - 1);
+                               gtkpspell->theword[BUFSIZE - 1] = 0;
+                               list = misspelled_suggest(gtkpspell, buf);
+                               if (list != NULL) {
+                                       gtk_menu_popup(make_menu(list, gtkpspell), NULL, NULL, NULL, NULL,
+                                                      eb->button, eb->time);
+                                       for (l = list; l != NULL; l = l->next)
+                                               g_free(l->data);
+                                       g_list_free(list);
+                                       return;
+                               }
+                       }
+               }
+       gtk_menu_popup(make_menu_config(gtkpspell),NULL,NULL,NULL,NULL,
+       eb->button,eb->time);
 }
 
 /* ok, this is pretty wacky:
@@ -569,102 +1055,91 @@ static void popup_menu(GtkXText *gtktext, GdkEventButton *eb)
  * so what do we do?  forge rightclicks as leftclicks, then popup the menu.
  * HACK HACK HACK.
  */
-static gint button_press_intercept_cb(GtkXText *gtktext, GdkEvent *e, gpointer d
+static gint button_press_intercept_cb(GtkXText *gtktext, GdkEvent *e, GtkPspell *gtkpspell
 {
-    GdkEventButton *eb;
-    gboolean retval;
-
-    if (!gtkspell_running()) return FALSE;
+       GdkEventButton *eb;
+       gboolean retval;
 
-    if (e->type != GDK_BUTTON_PRESS) return FALSE;
-    eb = (GdkEventButton*) e;
+       if (!gtkpspell_running(gtkpspell)) 
+               return FALSE;
 
-    if (eb->button != 3) return FALSE;
+       if (e->type != GDK_BUTTON_PRESS) 
+               return FALSE;
+       eb = (GdkEventButton*) e;
 
-    /* forge the leftclick */
-    eb->button = 1;
+       if (eb->button != 3) 
+               return FALSE;
 
-    gtk_signal_handler_block_by_func(GTK_OBJECT(gtktext),
-                                     GTK_SIGNAL_FUNC(button_press_intercept_cb), d);
-    gtk_signal_emit_by_name(GTK_OBJECT(gtktext), "button-press-event",
-                            e, &retval);
-    gtk_signal_handler_unblock_by_func(GTK_OBJECT(gtktext),
-                                       GTK_SIGNAL_FUNC(button_press_intercept_cb), d);
-    gtk_signal_emit_stop_by_name(GTK_OBJECT(gtktext), "button-press-event");
+       /* forge the leftclick */
+       eb->button = 1;
 
-    /* now do the menu wackiness */
-    popup_menu(gtktext, eb);
-    return TRUE;
+        gtk_signal_handler_block_by_func(GTK_OBJECT(gtktext),
+                                        GTK_SIGNAL_FUNC(button_press_intercept_cb), 
+                                        gtkpspell);
+       gtk_signal_emit_by_name(GTK_OBJECT(gtktext), "button-press-event",
+                               e, &retval);
+       gtk_signal_handler_unblock_by_func(GTK_OBJECT(gtktext),
+                                          GTK_SIGNAL_FUNC(button_press_intercept_cb), 
+                                          gtkpspell);
+       gtk_signal_emit_stop_by_name(GTK_OBJECT(gtktext), "button-press-event");
+    
+       /* now do the menu wackiness */
+       popup_menu(gtkpspell, eb);
+       gtk_grab_remove(GTK_WIDGET(gtktext));
+       return TRUE;
 }
 
-void gtkspell_uncheck_all(GtkXText *gtktext
+void gtkpspell_uncheck_all(GtkPspell * gtkpspell
 {
-    int origpos;
-    unsigned char *text;
-    float adj_value;
-    adj_value = gtktext->vadj->value;
-    gtk_xtext_freeze(gtktext);
-    origpos = gtk_editable_get_position(GTK_EDITABLE(gtktext));
-    text = gtk_editable_get_chars(GTK_EDITABLE(gtktext), 0, -1);
-    gtk_xtext_set_point(gtktext, 0);
-    gtk_xtext_forward_delete(gtktext, gtk_xtext_get_length(gtktext));
-    gtk_xtext_insert(gtktext, NULL, NULL, NULL, text, strlen(text));
-    gtk_xtext_thaw(gtktext);
-
-    gtk_editable_set_position(GTK_EDITABLE(gtktext), origpos);
-    gtk_adjustment_set_value(gtktext->vadj, adj_value);
+       int origpos;
+       unsigned char *text;
+       float adj_value;
+       GtkXText *gtktext;
+       
+       gtktext=gtkpspell->gtktext;
+
+       adj_value = gtktext->vadj->value;
+       gtk_xtext_freeze(gtktext);
+       origpos = gtk_editable_get_position(GTK_EDITABLE(gtktext));
+       text = gtk_editable_get_chars(GTK_EDITABLE(gtktext), 0, -1);
+       gtk_xtext_set_point(gtktext, 0);
+       gtk_xtext_forward_delete(gtktext, gtk_xtext_get_length(gtktext));
+       gtk_xtext_insert(gtktext, NULL, NULL, NULL, text, strlen(text));
+       gtk_xtext_thaw(gtktext);
+
+       gtk_editable_set_position(GTK_EDITABLE(gtktext), origpos);
+       gtk_adjustment_set_value(gtktext->vadj, adj_value);
 }
 
-void gtkspell_attach(GtkXText *gtktext) 
+void gtkpspell_attach(GtkPspell *gtkpspell, GtkXText *gtktext) 
 {
-//    if (prefs_common.auto_makeispell) {
-        gtk_signal_connect(GTK_OBJECT(gtktext), "insert-text",
-                           GTK_SIGNAL_FUNC(entry_insert_cb), NULL);
-        gtk_signal_connect_after(GTK_OBJECT(gtktext), "delete-text",
-                                 GTK_SIGNAL_FUNC(entry_delete_cb), NULL);
-//    };
-    gtk_signal_connect(GTK_OBJECT(gtktext), "button-press-event",
-                       GTK_SIGNAL_FUNC(button_press_intercept_cb), NULL);
+       gtkpspell->gtktext=gtktext;
+/*    if (prefs_common.auto_makepspell) { */
+       gtk_signal_connect(GTK_OBJECT(gtktext), "insert-text",
+                          GTK_SIGNAL_FUNC(entry_insert_cb), gtkpspell);
+       gtk_signal_connect_after(GTK_OBJECT(gtktext), "delete-text",
+                                GTK_SIGNAL_FUNC(entry_delete_cb), gtkpspell);
+/*    }; */
+       gtk_signal_connect(GTK_OBJECT(gtktext), "button-press-event",
+                          GTK_SIGNAL_FUNC(button_press_intercept_cb), gtkpspell);
 
 }
 
-void gtkspell_detach(GtkXText *gtktext
+void gtkpspell_detach(GtkPspell * gtkpspell
 {
-//    if (prefs_common.auto_makeispell) {
+       GtkXText * gtktext;
+       
+       gtktext =gtkpspell->gtktext;
+/*    if (prefs_common.auto_makepspell) { */
         gtk_signal_disconnect_by_func(GTK_OBJECT(gtktext),
-                                      GTK_SIGNAL_FUNC(entry_insert_cb), NULL);
+                                      GTK_SIGNAL_FUNC(entry_insert_cb), gtkpspell);
         gtk_signal_disconnect_by_func(GTK_OBJECT(gtktext),
-                                      GTK_SIGNAL_FUNC(entry_delete_cb), NULL);
-//    };
-    gtk_signal_disconnect_by_func(GTK_OBJECT(gtktext),
-                                  GTK_SIGNAL_FUNC(button_press_intercept_cb), NULL);
+                                      GTK_SIGNAL_FUNC(entry_delete_cb), gtkpspell);
+/*    }; */
+       gtk_signal_disconnect_by_func(GTK_OBJECT(gtktext),
+                                      GTK_SIGNAL_FUNC(button_press_intercept_cb), gtkpspell);
 
-    gtkspell_uncheck_all(gtktext);
-}
-
-static void sigchld(int param) 
-{
-    int retstat, retval;
-
-    debug_print(_("*** SIGCHLD called. (ispell pid: %i)\n"), spell_pid);
-    while ((retval = waitpid( -1, &retstat, WNOHANG)) > 0) {
-        if (retval == spell_pid) {
-            debug_print(_("*** SIGCHLD called for ispell. (pid: %i)\n"), spell_pid);
-            spell_pid = -1;
-        }
-    }
-}
-
-static void set_up_signal() 
-{
-    /* RETSIGTYPE is found in autoconf's config.h */
-#ifdef RETSIGTYPE
-    typedef RETSIGTYPE (*sighandler)(int);
-    signal(SIGCHLD, (sighandler)sigchld);
-#else
-    /* otherwise, just hope it works */
-    signal(SIGCHLD, sigchld);
-#endif
+       gtkpspell_uncheck_all(gtkpspell);
 }
 
 /*** Sylpheed (Claws) ***/
@@ -676,13 +1151,11 @@ static GSList *create_empty_dictionary_list(void)
        
        dict = g_new0(Dictionary, 1);
        dict->name = g_strdup(_("None"));
-       dict->path = NULL;
        return g_slist_append(list, dict);
 }
 
-/* gtkspell_get_dictionary_list() - returns list of dictionary names and the full 
- * path of file names. */
-GSList *gtkspell_get_dictionary_list(const gchar *ispell_path)
+/* gtkpspell_get_dictionary_list() - returns list of dictionary names */
+GSList *gtkpspell_get_dictionary_list(const gchar *pspell_path)
 {
        GSList *list;
        gchar *dict_path, *tmp, *prevdir;
@@ -693,18 +1166,10 @@ GSList *gtkspell_get_dictionary_list(const gchar *ispell_path)
 
        list = NULL;
 
-       /* ASSUME: ispell_path is full path */
-       dict_path = strstr(ispell_path + 1, G_DIR_SEPARATOR_S);
-       tmp = g_strndup(ispell_path, dict_path - ispell_path);
-       
-       /* ASSUME: ispell dictionaries in PREFIX/lib/ispell */
-       dict_path = g_strconcat(tmp, G_DIR_SEPARATOR_S, "lib", G_DIR_SEPARATOR_S, "ispell", NULL);
-       g_free(tmp);
-
 #ifdef USE_THREADS
 #warning TODO: no directory change
 #endif
-       
+       dict_path=g_strdup(pspell_path);
        prevdir = g_get_current_dir();
        if (chdir(dict_path) <0) {
                FILE_OP_ERROR(dict_path, "chdir");
@@ -717,12 +1182,11 @@ GSList *gtkspell_get_dictionary_list(const gchar *ispell_path)
 
        if (NULL != (dir = opendir("."))) {
                while (NULL != (ent = readdir(dir))) {
-                       /* search for hash table */
-                       if (NULL != (tmp = strstr(ent->d_name, ".hash"))) {
+                       /* search for pwli */
+                       if (NULL != (tmp = strstr(ent->d_name, ".pwli"))) {
                                dict = g_new0(Dictionary, 1);
                                dict->name = g_strndup(ent->d_name, tmp - ent->d_name);
-                               dict->path = g_strconcat(dict_path, G_DIR_SEPARATOR_S, ent->d_name, NULL);
-                               debug_print(_("Found dictionary %s\n"), dict->path);
+                               debug_print(_("Found dictionary %s\n"), dict->name);
                                list = g_slist_append(list, dict);
                        }
                }                       
@@ -733,13 +1197,17 @@ GSList *gtkspell_get_dictionary_list(const gchar *ispell_path)
                debug_print(_("No dictionary found\n"));
                list = create_empty_dictionary_list();
        }
+        if(list==NULL){
+          debug_print(_("No dictionary found"));
+          list = create_empty_dictionary_list();
+        }
        chdir(prevdir);
        g_free(dict_path);
        g_free(prevdir);
        return list;
 }
 
-void gtkspell_free_dictionary_list(GSList *list)
+void gtkpspell_free_dictionary_list(GSList *list)
 {
        Dictionary *dict;
        GSList *walk;
@@ -748,8 +1216,6 @@ void gtkspell_free_dictionary_list(GSList *list)
                        dict = (Dictionary *) walk->data;
                        if (dict->name)
                                g_free(dict->name);
-                       if (dict->path)
-                               g_free(dict->path);
                        g_free(dict);
                }                               
        g_slist_free(list);
@@ -763,14 +1229,14 @@ static void dictionary_option_menu_item_data_destroy(gpointer data)
                g_free(str);
 }
 
-GtkWidget *gtkspell_dictionary_option_menu_new(const gchar *ispell_path)
+GtkWidget *gtkpspell_dictionary_option_menu_new(const gchar *pspell_path)
 {
        GSList *dict_list, *tmp;
        GtkWidget *item;
        GtkWidget *menu;
        Dictionary *dict;
 
-       dict_list = gtkspell_get_dictionary_list(ispell_path);
+       dict_list = gtkpspell_get_dictionary_list(pspell_path);
        g_return_val_if_fail(dict_list, NULL);
 
        menu = gtk_menu_new();
@@ -778,9 +1244,9 @@ GtkWidget *gtkspell_dictionary_option_menu_new(const gchar *ispell_path)
        for (tmp = dict_list; tmp != NULL; tmp = g_slist_next(tmp)) {
                dict = (Dictionary *) tmp->data;
                item = gtk_menu_item_new_with_label(dict->name);
-               if (dict->path)
-                       gtk_object_set_data_full(GTK_OBJECT(item), "full_path",
-                                        g_strdup(dict->path), 
+               if (dict->name)
+                       gtk_object_set_data_full(GTK_OBJECT(item), "dict_name",
+                                        g_strdup(dict->name), 
                                         dictionary_option_menu_item_data_destroy);
                gtk_menu_append(GTK_MENU(menu), item);                                   
                gtk_widget_show(item);
@@ -788,22 +1254,23 @@ GtkWidget *gtkspell_dictionary_option_menu_new(const gchar *ispell_path)
 
        gtk_widget_show(menu);
 
-       gtkspell_free_dictionary_list(dict_list);
+       gtkpspell_free_dictionary_list(dict_list);
 
        return menu;
 }
 
-gchar *gtkspell_get_dictionary_menu_active_item(GtkWidget *menu)
+
+
+gchar *gtkpspell_get_dictionary_menu_active_item(GtkWidget *menu)
 {
        GtkWidget *menuitem;
        gchar *result;
 
        g_return_val_if_fail(GTK_IS_MENU(menu), NULL);
        menuitem = gtk_menu_get_active(GTK_MENU(menu));
-       
-       result = gtk_object_get_data(GTK_OBJECT(menuitem), "full_path");
-       g_return_val_if_fail(result, NULL);
-
-       return g_strdup(result);
+        result = gtk_object_get_data(GTK_OBJECT(menuitem), "dict_name");
+        g_return_val_if_fail(result, NULL);
+        return g_strdup(result);
+  
 }
-
+#endif