]> git.neil.brown.name Git - edlib.git/blobdiff - lib-aspell.c
TODO: clean out done items.
[edlib.git] / lib-aspell.c
index da1e3d20f59bc3b0d1256f62a8cf86269abcd6c6..f55ca5b96b69312fd595acfb1fdaf0e64425d33b 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright Neil Brown ©2020-2021 <neil@brown.name>
+ * Copyright Neil Brown ©2020-2023 <neil@brown.name>
  * May be distributed under terms of GPLv2 - see file:COPYING
  *
  * aspell: edlib interface for aspell library.
@@ -8,13 +8,16 @@
 
 #include <aspell.h>
 #include <wctype.h>
+#define PANE_DATA_TYPE struct aspell_data
 #include "core.h"
 
-static AspellConfig *spell_config;
-
 struct aspell_data {
        AspellSpeller *speller safe;
+       bool need_save;
 };
+#include "core-pane.h"
+
+static AspellConfig *spell_config;
 
 static int trim(const char *safe *wordp safe)
 {
@@ -53,22 +56,27 @@ DEF_CMD(aspell_attach_helper)
                LOG("Cannot create speller: %s", aspell_error_message(ret));
                return Efail;
        }
-       alloc(as, pane);
+       p = pane_register(ci->focus, 0, &aspell_handle.c);
+       if (!p)
+               return Efail;
+       as = p->data;
        as->speller = safe_cast to_aspell_speller(ret);
-       p = pane_register(ci->focus, 0, &aspell_handle.c, as);
-       if (p) {
-               call("doc:request:aspell:check", p);
-               call("doc:request:aspell:suggest", p);
-               call("doc:request:aspell:set-dict", p);
-               call("doc:request:aspell:add-word", p);
-       }
+
+       call("doc:request:aspell:check", p);
+       call("doc:request:aspell:suggest", p);
+       call("doc:request:aspell:set-dict", p);
+       call("doc:request:aspell:add-word", p);
+       call("doc:request:aspell:save", p);
+
        return 1;
 }
 
-DEF_CMD(aspell_close)
+DEF_CMD_CLOSED(aspell_close)
 {
        struct aspell_data *as = ci->home->data;
 
+       if (as->need_save)
+               aspell_speller_save_all_word_lists(as->speller);
        delete_aspell_speller(as->speller);
        return 1;
 }
@@ -96,7 +104,7 @@ DEF_CMD(spell_check)
 
        if (rv != Efallthrough)
                return rv;
-       call_comm("doc:attach-helper", ci->focus, &aspell_attach_helper);
+       call_comm("doc:get-doc", ci->focus, &aspell_attach_helper);
        return call("doc:notify:aspell:check", ci->focus, 0, NULL, ci->str);
 }
 
@@ -130,11 +138,33 @@ DEF_CMD(spell_suggest)
 
        if (rv != Efallthrough)
                return rv;
-       call_comm("doc:attach-helper", ci->focus, &aspell_attach_helper);
+       call_comm("doc:get-doc", ci->focus, &aspell_attach_helper);
        return call_comm("doc:notify:aspell:suggest", ci->focus, ci->comm2,
                         0, NULL, ci->str);
 }
 
+DEF_CMD(aspell_save)
+{
+       struct aspell_data *as = ci->home->data;
+       if (as->need_save) {
+               as->need_save = False;
+               aspell_speller_save_all_word_lists(as->speller);
+       }
+       return Efalse;
+}
+
+DEF_CMD(aspell_do_save)
+{
+       aspell_save_func(ci);
+       return 1;
+}
+
+DEF_CMD(spell_save)
+{
+       return call_comm("doc:notify:aspell:save", ci->focus, ci->comm2,
+                        ci->num, NULL, ci->str);
+}
+
 DEF_CMD(aspell_add)
 {
        struct aspell_data *as = ci->home->data;
@@ -147,9 +177,13 @@ DEF_CMD(aspell_add)
        if (!len)
                return Efail;
 
-       if (ci->num == 1)
+       if (ci->num == 1) {
                aspell_speller_add_to_personal(as->speller, word, len);
-       else
+               if (as->need_save)
+                       call_comm("event:free", ci->home, &aspell_save);
+               as->need_save = True;
+               call_comm("event:timer", ci->home, &aspell_save, 30*1000);
+       } else
                aspell_speller_add_to_session(as->speller, word, len);
        call("doc:notify:spell:dict-changed", ci->home);
        return 1;
@@ -158,13 +192,13 @@ DEF_CMD(aspell_add)
 DEF_CMD(spell_add)
 {
        int rv = call_comm("doc:notify:aspell:add-word", ci->focus, ci->comm2,
-                          0, NULL, ci->str);
+                          ci->num, NULL, ci->str);
 
        if (rv != Efallthrough)
                return rv;
-       call_comm("doc:attach-helper", ci->focus, &aspell_attach_helper);
+       call_comm("doc:get-doc", ci->focus, &aspell_attach_helper);
        return call_comm("doc:notify:aspell:add-word", ci->focus, ci->comm2,
-                        0, NULL, ci->str);
+                        ci->num, NULL, ci->str);
 }
 
 DEF_CMD(aspell_set_dict)
@@ -176,7 +210,6 @@ DEF_CMD(aspell_set_dict)
 
        if (!lang)
                return Enoarg;
-       LOG("lang = %s", lang);
        conf2 = aspell_config_clone(spell_config);
        aspell_config_replace(conf2, "lang", lang);
        ret = new_aspell_speller(conf2);
@@ -196,7 +229,7 @@ DEF_CMD(spell_dict)
                   ksuffix(ci, "interactive-cmd-dict-"));
        if (ret != Efallthrough)
                return ret;
-       call_comm("doc:attach-helper", ci->focus, &aspell_attach_helper);
+       call_comm("doc:get-doc", ci->focus, &aspell_attach_helper);
        return call("doc:notify:aspell:set-dict", ci->focus, 0, NULL,
                    ksuffix(ci, "interactive-cmd-dict-"));
 }
@@ -301,16 +334,17 @@ void edlib_init(struct pane *ed safe)
                  0, NULL, "Spell:NextWord");
        call_comm("global-set-command", ed, &spell_add,
                  0, NULL, "Spell:AddWord");
+       call_comm("global-set-command", ed, &spell_save,
+                 0, NULL, "Spell:Save");
 
-       call_comm("global-set-command", ed, &spell_dict,
-                 0, NULL, "interactive-cmd-dict-",
-                 0, NULL, "interactive-cmd-dict-~");
+       call_comm("global-set-command-prefix", ed, &spell_dict,
+                 0, NULL, "interactive-cmd-dict-");
 
        aspell_map = key_alloc();
        key_add(aspell_map, "Close", &aspell_close);
-       key_add(aspell_map, "Free", &edlib_do_free);
        key_add(aspell_map, "aspell:check", &aspell_check);
        key_add(aspell_map, "aspell:suggest", &aspell_suggest);
        key_add(aspell_map, "aspell:set-dict", &aspell_set_dict);
        key_add(aspell_map, "aspell:add-word", &aspell_add);
+       key_add(aspell_map, "aspell:save", &aspell_do_save);
 }