]> git.neil.brown.name Git - edlib.git/commitdiff
Mark all "Close" handlers as being closed_ok.
authorNeilBrown <neil@brown.name>
Wed, 13 Sep 2023 23:44:02 +0000 (09:44 +1000)
committerNeilBrown <neil@brown.name>
Fri, 15 Sep 2023 03:37:49 +0000 (13:37 +1000)
This allows use to remove the strcmp on "Close".
It requires python commands to be able to be marks closed_ok.
This is done with name "handle_close" or any name ending "_closed_ok".

Signed-off-by: NeilBrown <neil@brown.name>
34 files changed:
core-doc.c
core-editor.c
core-keymap.c
core-log.c
core-pane.h
display-ncurses.c
display-x11-xcb.c
doc-dir.c
doc-docs.c
doc-email.c
doc-list.c
doc-multipart.c
doc-text.c
emacs-search.c
lang-python.c
lib-askpass.c
lib-aspell.c
lib-copybuf.c
lib-crop.c
lib-history.c
lib-input.c
lib-keymap.c
lib-popup.c
lib-renderline.c
lib-tile.c
lib-view.c
lib-whitespace.c
lib-wiggle.c
lib-x11selection-gtk.c
lib-x11selection-xcb.c
render-complete.c
render-format.c
render-hex.c
render-lines.c

index 31b4549208c8f74749b92d7700ac6ed1ae3a7aa7..2e325132eca71bfde8915c09335d306eb16efa46 100644 (file)
@@ -737,7 +737,7 @@ DEF_CMD(doc_addview)
        return 1 + ret;
 }
 
-DEF_CMD(doc_close_doc)
+DEF_CMD_CLOSED(doc_close_doc)
 {
        struct doc *d = ci->home->_data;
        doc_free(d, ci->home);
@@ -1059,7 +1059,7 @@ DEF_CMD(doc_clone)
        return 1;
 }
 
-DEF_CMD(doc_close)
+DEF_CMD_CLOSED(doc_close)
 {
        struct doc_data *dd = ci->home->data;
        int i;
index 42bbd9114845206de39eb867cb7e40728b808082..c36eb2d7270fb74a55ba2d5a040633074246353a 100644 (file)
@@ -348,7 +348,7 @@ DEF_EXTERN_CMD(edlib_noop)
        return Efallthrough;
 }
 
-DEF_CMD(editor_close)
+DEF_CMD_CLOSED(editor_close)
 {
        struct ed_info *ei = ci->home->data;
        stat_free();
index 14a5e520aa4f8129ca83e44545f4c09d7921b5bc..703ca2b13a5fa1caceb6b9815a0bada9bc1951f7 100644 (file)
@@ -222,6 +222,11 @@ void key_add(struct map *map safe, const char *k safe, struct command *comm)
 
        if (!comm)
                return;
+       if (strcmp(k, "Close") == 0 &&
+           !comm->closed_ok) {
+               LOG("WARNING: Command %s registered for \"Close\" but not marked closed_ok",
+                   comm->name);
+       }
 
        pos = key_find(map, k);
        /* cases:
index 6e826cc1caf23d78ce3589980a4ac56267145abc..9c0fa08024ddb92744fb71633aef93716bdd476e 100644 (file)
@@ -395,7 +395,7 @@ DEF_CMD(log_view)
        return 1;
 }
 
-DEF_CMD(log_close)
+DEF_CMD_CLOSED(log_close)
 {
        struct log *l = ci->home->doc_data;
 
index 8e54f3c73d2a20fc2694335dd04d36298135e5d0..04afae4b2ed7c83a0685b1b67f6858f52f8b61e2 100644 (file)
@@ -132,13 +132,9 @@ static inline int do_call_val(enum target_type type, struct pane *home,
                if (home)
                        ci.home = home;
                if ((home->damaged & DAMAGED_CLOSED) &&
-                   !home->handle->closed_ok &&
-                   ci.key[0] != 'C' && /* Compile will often optimise
-                                        * the strncmp away
-                                        */
-                   strcmp(ci.key, "Close") != 0)
+                   !home->handle->closed_ok)
                        /* This pane cannot accept anything but
-                        * "Close"
+                        * close_ok commands.
                         */
                        return Efallthrough;
                ci.comm = home->handle;
index 1bf5ba8c7019e773ed467116ce0bea1af4c904df..07c3dab5dd1d52c8038bbc0ba9b4e1fd8380989f 100644 (file)
@@ -858,7 +858,7 @@ DEF_CMD(nc_notify_display)
        return 1;
 }
 
-DEF_CMD(nc_close)
+DEF_CMD_CLOSED(nc_close)
 {
        struct pane *p = ci->home;
        struct display_data *dd = p->data;
index e768e935a36226211d11d0b8f462110036d47ae6..817989b2aa2d7a011c9c7aa31a3dfb7a396522c7 100644 (file)
@@ -442,7 +442,7 @@ DEF_CB(cnt_disp)
        return 1;
 }
 
-DEF_CMD(xcb_close_display)
+DEF_CMD_CLOSED(xcb_close_display)
 {
        /* If this is only display, then refuse to close this one */
        struct call_return cr;
@@ -557,7 +557,7 @@ static void panes_free(struct xcb_data *xd safe)
 
 static void kbd_free(struct xcb_data *xd safe);
 
-DEF_CMD(xcb_close)
+DEF_CMD_CLOSED(xcb_close)
 {
        struct xcb_data *xd = ci->home->data;
 
index 749d1c34a261ee874d1937181f661bb37972b68c..988a87837763a82eed6541fb8bf6e1e6a9e32acf 100644 (file)
--- a/doc-dir.c
+++ b/doc-dir.c
@@ -725,7 +725,7 @@ DEF_CMD(dir_val_marks)
        return Efalse;
 }
 
-DEF_CMD(dir_destroy)
+DEF_CMD_CLOSED(dir_destroy)
 {
        struct directory *dr = ci->home->doc_data;
 
index 0b4165772eb43e306bbff5a8568284dcf4a0e958..74fa73bac2792c4777cd9916c9f560ac499b2abd 100644 (file)
@@ -796,7 +796,7 @@ DEF_CMD(docs_val_marks)
        return Efalse;
 }
 
-DEF_CMD(docs_close)
+DEF_CMD_CLOSED(docs_close)
 {
        struct docs *docs = ci->home->doc_data;
 
index cecd3c7a21c9afe097a196edf6e907d1fb60c95a..6579a1adde16e52d501a6ee61c2121b0a93fef06 100644 (file)
@@ -958,7 +958,7 @@ out:
        return Efail;
 }
 
-DEF_CMD(email_view_close)
+DEF_CMD_CLOSED(email_view_close)
 {
        struct email_view *evi = ci->home->data;
 
index 8fca2b6f73636adaeeb6c4ebae56f9dd36e0980f..61c28196ebeab85d16b0ee6e13cd8ece10de50bf 100644 (file)
@@ -216,7 +216,7 @@ DEF_CMD(list_new)
        return comm_call(ci->comm2, "callback:doc", p);
 }
 
-DEF_CMD(list_close)
+DEF_CMD_CLOSED(list_close)
 {
        struct list *l = ci->home->doc_data;
        struct elmnt *e;
index d21e12f1aa5c453388989409a0c2c32a9edf554e..1f262c5fc90a40e244bd4e0c15a36d04b264efbb 100644 (file)
@@ -218,7 +218,7 @@ static void mp_normalize(struct mp_info *mpi safe, struct mark *m safe,
        }
 }
 
-DEF_CMD(mp_close)
+DEF_CMD_CLOSED(mp_close)
 {
        struct mp_info *mpi = ci->home->doc_data;
        int i;
index 7f4ed19441b04fbf2f019fef8f97a9d6c06a3f2a..8d528f394df2d3ac35f1896a22bb0cf7810b116c 100644 (file)
@@ -2516,7 +2516,7 @@ static void text_cleanout(struct text *t safe)
        }
 }
 
-DEF_CMD(text_destroy)
+DEF_CMD_CLOSED(text_destroy)
 {
        struct text *t = ci->home->doc_data;
 
index 6204f966a0ce3d4a0e36c6edac5812ba78f57e53..22b5b2ba039e149b3d4a7becdda9d830690f33f5 100644 (file)
@@ -295,7 +295,7 @@ DEF_CMD(search_insert_meta)
 
 }
 
-DEF_CMD(search_close)
+DEF_CMD_CLOSED(search_close)
 {
        struct es_info *esi = ci->home->data;
 
@@ -1056,7 +1056,7 @@ DEF_CMD(emacs_search_reposition)
        return Efallthrough;
 }
 
-DEF_CMD(emacs_highlight_close)
+DEF_CMD_CLOSED(emacs_highlight_close)
 {
        /* ci->focus is being closed */
        struct highlight_info *hi = ci->home->data2;
index d236caba2449468e1f7cc9940d0e9e54a830d2db..b37fcb96f1f609668889a057dac408107933a202 100644 (file)
@@ -100,9 +100,18 @@ DEF_CMD(python_pane_call);
 DEF_CMD(python_doc_call);
 static void python_free_command(struct command *c safe);
 
-static struct python_command *export_callable(PyObject *callable safe)
+static struct python_command *export_callable(PyObject *callable safe,
+                                             PyObject *nm)
 {
        struct python_command *c;
+       const char *name = NULL;
+       int len;
+
+       if (nm && PyUnicode_Check(nm))
+               name = PyUnicode_AsUTF8(nm);
+       if (!name)
+               name = "";
+       len = strlen(name);
 
        list_for_each_entry(c, &exported_commands, lst)
                if (c->callable == callable) {
@@ -114,7 +123,12 @@ static struct python_command *export_callable(PyObject *callable safe)
        c->c = python_call;
        c->c.free = python_free_command;
        c->c.refcnt = 0;
-       c->c.closed_ok = 0;
+       if (strcmp(name, "handle_close") == 0 ||
+           (len > 10 &&
+            strcmp(name+len-10, "_closed_ok") == 0))
+               c->c.closed_ok = 1;
+       else
+               c->c.closed_ok = 0;
        command_get(&c->c);
        Py_INCREF(callable);
        c->callable = callable;
@@ -547,6 +561,7 @@ static void do_map_init(Pane *self safe)
                        if (doc && doc != Py_None) {
                                PyObject *tofree = NULL;
                                char *docs = python_as_string(doc, &tofree);
+
                                if (docs &&
                                    strstarts(docs, "handle-range") &&
                                    docs[12]) {
@@ -558,7 +573,7 @@ static void do_map_init(Pane *self safe)
                                                char *b = strndup(s1+1, s2-(s1+1));
 
                                                struct python_command *comm =
-                                                       export_callable(m);
+                                                       export_callable(m, e);
                                                key_add_range(self->map, a, b,
                                                              &comm->c);
                                                free(a); free(b);
@@ -571,7 +586,7 @@ static void do_map_init(Pane *self safe)
                                        char *b = strconcat(self->pane,
                                                            a, "\xFF\xFF\xFF\xFF");
                                        struct python_command *comm =
-                                               export_callable(m);
+                                               export_callable(m, e);
                                        key_add_range(self->map, a, b,
                                                      &comm->c);
                                        command_put(&comm->c);
@@ -589,24 +604,24 @@ static void do_map_init(Pane *self safe)
 
                if (m && PyMethod_Check(m)) {
                        PyObject *doc = PyObject_GetAttrString(m, "__doc__");
-                       if (doc && doc != Py_None) {
-                               PyObject *tofree = NULL;
-                               char *docs = python_as_string(doc, &tofree);
-                               if (docs &&
-                                   strstarts(docs, "handle:")) {
+                       char *docs;
+                       PyObject *tofree = NULL;
+                       if (doc && doc != Py_None &&
+                           (docs = python_as_string(doc, &tofree)) != NULL) {
+
+                               if (strstarts(docs, "handle:")) {
                                        struct python_command *comm =
-                                               export_callable(m);
+                                               export_callable(m, e);
                                        key_add(self->map, docs+7, &comm->c);
                                        command_put(&comm->c);
                                }
-                               if (docs &&
-                                   strstarts(docs, "handle-list") &&
+                               if (strstarts(docs, "handle-list") &&
                                    docs[11]) {
                                        char sep = docs[11];
                                        char *s1 = docs + 12;
                                        while (s1 && *s1 && *s1 != sep) {
                                                struct python_command *comm =
-                                                       export_callable(m);
+                                                       export_callable(m, e);
                                                char *a;
                                                char *s2 = strchr(s1, sep);
                                                if (s2) {
@@ -621,8 +636,8 @@ static void do_map_init(Pane *self safe)
                                                command_put(&comm->c);
                                        }
                                }
-                               Py_XDECREF(tofree);
                        }
+                       Py_XDECREF(tofree);
                        Py_XDECREF(doc);
                }
                Py_XDECREF(m);
@@ -2640,7 +2655,7 @@ static bool get_cmd_info(struct cmd_info *ci safe, PyObject *args safe, PyObject
                                return False;
                        }
                } else if (PyCallable_Check(a)) {
-                       struct python_command *pc = export_callable(a);
+                       struct python_command *pc = export_callable(a, NULL);
 
                        if (ci->comm2 == NULL)
                                ci->comm2 = &pc->c;
@@ -2803,7 +2818,7 @@ static bool get_cmd_info(struct cmd_info *ci safe, PyObject *args safe, PyObject
                                        return False;
                                }
                        } else if (PyCallable_Check(a)) {
-                               struct python_command *pc = export_callable(a);
+                               struct python_command *pc = export_callable(a, NULL);
 
                                ci->comm2 = &pc->c;
                        } else {
index e2de20d539b0869e293d71281ccf10458c42f551..a94c295361f5af0f1d4ff512b2bbbd17c6dd8a9e 100644 (file)
@@ -135,7 +135,7 @@ fail:
        return Efail;
 }
 
-DEF_CMD(askpass_close)
+DEF_CMD_CLOSED(askpass_close)
 {
        struct apinfo *ai = ci->home->data;
 
index fc564989d980600a45afc6e9468c0b3e763937a5..f55ca5b96b69312fd595acfb1fdaf0e64425d33b 100644 (file)
@@ -71,7 +71,7 @@ DEF_CMD(aspell_attach_helper)
        return 1;
 }
 
-DEF_CMD(aspell_close)
+DEF_CMD_CLOSED(aspell_close)
 {
        struct aspell_data *as = ci->home->data;
 
index 352a8ea40c73b0860008f3bb24974739337287e1..25a24ff6ea5de74f30fec9d8dcdc121c27eea41e 100644 (file)
@@ -48,7 +48,7 @@ static void free_txt(struct txt **tp safe)
        free(t);
 }
 
-DEF_CMD(copy_close)
+DEF_CMD_CLOSED(copy_close)
 {
        struct copy_info *cyi = ci->home->data;
 
index c7ebe3e85009e1df430e17d6f401fbbc67604125..4c43e9f4cf958de045d899d24065c62b59949626 100644 (file)
@@ -59,7 +59,7 @@ static bool crop(struct mark *m, struct crop_data *cd safe)
        return True;
 }
 
-DEF_CMD(crop_close)
+DEF_CMD_CLOSED(crop_close)
 {
        struct crop_data *cd = ci->home->data;
 
index a5885b46c36aeb4d3f947a4c5d32297623eb9dae..0c051cbbf6c3fbc17ed035d6c5df218645fb1f36 100644 (file)
@@ -72,7 +72,7 @@ static void free_si(struct si **sip safe)
        }
 }
 
-DEF_CMD(history_close)
+DEF_CMD_CLOSED(history_close)
 {
        struct history_info *hi = ci->home->data;
 
index 4b2200df20ac6e60f86ab2d948e0d419c47373af..f862370ca288176831b8eb3b62b6d49116c70a3a 100644 (file)
@@ -477,7 +477,7 @@ DEF_CMD(close_focus)
        return 1;
 }
 
-DEF_CMD(input_close)
+DEF_CMD_CLOSED(input_close)
 {
        struct input_mode *im = ci->home->data;
        int i;
index 16e38f10376660a8015d01ef4ce2b833c3327490..80fb59e4a846ec28860fc9931c3f10970860dc31 100644 (file)
@@ -21,7 +21,7 @@ struct key_data {
 
 static struct pane *safe do_keymap_attach(struct pane *p safe);
 
-DEF_CMD(keymap_handle)
+DEF_CMD_CLOSED(keymap_handle)
 {
        struct key_data *kd = ci->home->data;
 
@@ -29,6 +29,9 @@ DEF_CMD(keymap_handle)
                command_put(kd->globalcmd);
                return 1;
        }
+       if (ci->home->damaged & DAMAGED_CLOSED)
+               return Efallthrough;
+
        if (strcmp(ci->key, "Clone") == 0) {
                struct pane *p = do_keymap_attach(ci->focus);
                struct key_data *kd_old = ci->home->data;
index eb2683dc8992b6137fabc03902b5211b42a139fd..d7c19423c3cec9586b81b848df0197740b98d351 100644 (file)
@@ -132,7 +132,7 @@ static void popup_resize(struct pane *p safe, const char *style safe,
        pane_resize(p, x, y, w, h);
 }
 
-DEF_CMD(popup_close)
+DEF_CMD_CLOSED(popup_close)
 {
        struct popup_info *ppi = ci->home->data;
 
index 451051dd7057619e325f3867ea7a102bfc228f21..da3c99cb9ecd16e4b107719d650e3f4b2f9d0d99 100644 (file)
@@ -1352,7 +1352,7 @@ DEF_CMD(renderline_set)
        return 1;
 }
 
-DEF_CMD(renderline_close)
+DEF_CMD_CLOSED(renderline_close)
 {
        struct rline_data *rd = ci->home->data;
        struct render_item *ri = rd->content;
index e5c85c7031a12b552db007f138c0cf77b6c7fbe8..868c068f5aafa05030b847ba24e7b0a696129b66 100644 (file)
@@ -61,7 +61,7 @@ static inline bool mine(struct pane *t safe)
        return t->z == 0 && t->handle == &tile_handle.c;
 }
 
-DEF_CMD(tile_close)
+DEF_CMD_CLOSED(tile_close)
 {
        struct tileinfo *ti = ci->home->data;
 
index 47bee0837bc13737f288daf6779492b944a909bd..f64b61277aa2818bf03f49604cc027e88e603d31 100644 (file)
@@ -258,7 +258,7 @@ DEF_CMD(view_refresh)
        return 1;
 }
 
-DEF_CMD(view_close)
+DEF_CMD_CLOSED(view_close)
 {
        struct view_data *vd = ci->home->data;
 
index 7b7ebfa257190fd03f7b426aa82d0d8f7e5fb271..68ef69f2d297800ae2ebc0fa82e50bf29773d0b2 100644 (file)
@@ -243,7 +243,7 @@ DEF_CMD(ws_attrs)
        return Efallthrough;
 }
 
-DEF_CMD(ws_close)
+DEF_CMD_CLOSED(ws_close)
 {
        struct ws_info *ws = ci->home->data;
 
index b4b3a3ac5a4080f14a8aae12ade5c8d627119dfb..fbc2032304a6c6c0cf58029c270fc3c855b2b830 100644 (file)
@@ -218,7 +218,7 @@ DEF_CMD(notify_close)
        return 1;
 }
 
-DEF_CMD(wiggle_close)
+DEF_CMD_CLOSED(wiggle_close)
 {
        struct wiggle_data *wd = ci->home->data;
        int i;
index 852fc37a3eb2a48c4a22b84b5564e36a8f56e155..7b337c8bcc2ea2e7a88017ee2ec375c6ca10ce31 100644 (file)
@@ -264,7 +264,7 @@ DEF_CMD(xs_sel_commit)
        return Efallthrough;
 }
 
-DEF_CMD(xs_close)
+DEF_CMD_CLOSED(xs_close)
 {
        struct xs_info *xsi = ci->home->data;
 
index 89a0c77c541a7d236a1de17326ce29e40578e72b..1ff7e55d4520cb1cebef2f194e0f41c8b88c0f8e 100644 (file)
@@ -266,7 +266,7 @@ DEF_CMD(xcbc_handle_close)
        return 1;
 }
 
-DEF_CMD(xcbc_close)
+DEF_CMD_CLOSED(xcbc_close)
 {
        struct xcbc_info *xci = ci->home->data2;
        char *cn = strconcat(ci->home, "xcb-selection-", xci->display);
index e12f3e4b095cfd81ec09c34f83a46d5e1ebf18a4..c2da85a24032833345e99a0f1a116517be82e71f 100644 (file)
@@ -258,7 +258,7 @@ DEF_CMD(render_complete_line)
        return ret;
 }
 
-DEF_CMD(complete_close)
+DEF_CMD_CLOSED(complete_close)
 {
        struct complete_data *cd = ci->home->data;
        struct stk *stk = cd->stk;
index d3978a9bd4599999d8e19102cdb59d0084266a30..241228f87fbff98ee7d70ffca7a0583615c22b5a 100644 (file)
@@ -235,7 +235,7 @@ DEF_CMD(render_line_prev)
        return 1;
 }
 
-DEF_CMD(format_close)
+DEF_CMD_CLOSED(format_close)
 {
        struct rf_data *rf = ci->home->data;
 
index 3f1adf80ea7a6a6ccc83d9df94237baed8ef4188..28bde9e7a178ae0296ad02df2a27e85b183ec5da 100644 (file)
@@ -31,7 +31,7 @@ static struct pane *do_render_hex_attach(struct pane *parent safe);
 
 DEF_LOOKUP_CMD(render_hex_handle, he_map);
 
-DEF_CMD(render_hex_close)
+DEF_CMD_CLOSED(render_hex_close)
 {
        struct pane *p = ci->home;
        struct he_data *he = p->data;
index 75fa6f6fd160984c1c6130885ca9d36aa4c6af61..b2fbce1248e5bdc36f1d4ba56937588dc997a1be 100644 (file)
@@ -1357,7 +1357,7 @@ DEF_CMD(render_lines_refresh)
        return 1;
 }
 
-DEF_CMD(render_lines_close)
+DEF_CMD_CLOSED(render_lines_close)
 {
        struct rl_data *rl = ci->home->data;