From: NeilBrown Date: Wed, 13 Sep 2023 23:44:02 +0000 (+1000) Subject: Mark all "Close" handlers as being closed_ok. X-Git-Url: http://git.neil.brown.name/?a=commitdiff_plain;h=e4d89e9743cdb9e2cacd16eb4f2c5df58c726595;p=edlib.git Mark all "Close" handlers as being closed_ok. 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 --- diff --git a/core-doc.c b/core-doc.c index 31b45492..2e325132 100644 --- a/core-doc.c +++ b/core-doc.c @@ -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; diff --git a/core-editor.c b/core-editor.c index 42bbd911..c36eb2d7 100644 --- a/core-editor.c +++ b/core-editor.c @@ -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(); diff --git a/core-keymap.c b/core-keymap.c index 14a5e520..703ca2b1 100644 --- a/core-keymap.c +++ b/core-keymap.c @@ -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: diff --git a/core-log.c b/core-log.c index 6e826cc1..9c0fa080 100644 --- a/core-log.c +++ b/core-log.c @@ -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; diff --git a/core-pane.h b/core-pane.h index 8e54f3c7..04afae4b 100644 --- a/core-pane.h +++ b/core-pane.h @@ -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; diff --git a/display-ncurses.c b/display-ncurses.c index 1bf5ba8c..07c3dab5 100644 --- a/display-ncurses.c +++ b/display-ncurses.c @@ -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; diff --git a/display-x11-xcb.c b/display-x11-xcb.c index e768e935..817989b2 100644 --- a/display-x11-xcb.c +++ b/display-x11-xcb.c @@ -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; diff --git a/doc-dir.c b/doc-dir.c index 749d1c34..988a8783 100644 --- 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; diff --git a/doc-docs.c b/doc-docs.c index 0b416577..74fa73ba 100644 --- a/doc-docs.c +++ b/doc-docs.c @@ -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; diff --git a/doc-email.c b/doc-email.c index cecd3c7a..6579a1ad 100644 --- a/doc-email.c +++ b/doc-email.c @@ -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; diff --git a/doc-list.c b/doc-list.c index 8fca2b6f..61c28196 100644 --- a/doc-list.c +++ b/doc-list.c @@ -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; diff --git a/doc-multipart.c b/doc-multipart.c index d21e12f1..1f262c5f 100644 --- a/doc-multipart.c +++ b/doc-multipart.c @@ -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; diff --git a/doc-text.c b/doc-text.c index 7f4ed194..8d528f39 100644 --- a/doc-text.c +++ b/doc-text.c @@ -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; diff --git a/emacs-search.c b/emacs-search.c index 6204f966..22b5b2ba 100644 --- a/emacs-search.c +++ b/emacs-search.c @@ -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; diff --git a/lang-python.c b/lang-python.c index d236caba..b37fcb96 100644 --- a/lang-python.c +++ b/lang-python.c @@ -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 { diff --git a/lib-askpass.c b/lib-askpass.c index e2de20d5..a94c2953 100644 --- a/lib-askpass.c +++ b/lib-askpass.c @@ -135,7 +135,7 @@ fail: return Efail; } -DEF_CMD(askpass_close) +DEF_CMD_CLOSED(askpass_close) { struct apinfo *ai = ci->home->data; diff --git a/lib-aspell.c b/lib-aspell.c index fc564989..f55ca5b9 100644 --- a/lib-aspell.c +++ b/lib-aspell.c @@ -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; diff --git a/lib-copybuf.c b/lib-copybuf.c index 352a8ea4..25a24ff6 100644 --- a/lib-copybuf.c +++ b/lib-copybuf.c @@ -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; diff --git a/lib-crop.c b/lib-crop.c index c7ebe3e8..4c43e9f4 100644 --- a/lib-crop.c +++ b/lib-crop.c @@ -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; diff --git a/lib-history.c b/lib-history.c index a5885b46..0c051cbb 100644 --- a/lib-history.c +++ b/lib-history.c @@ -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; diff --git a/lib-input.c b/lib-input.c index 4b2200df..f862370c 100644 --- a/lib-input.c +++ b/lib-input.c @@ -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; diff --git a/lib-keymap.c b/lib-keymap.c index 16e38f10..80fb59e4 100644 --- a/lib-keymap.c +++ b/lib-keymap.c @@ -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; diff --git a/lib-popup.c b/lib-popup.c index eb2683dc..d7c19423 100644 --- a/lib-popup.c +++ b/lib-popup.c @@ -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; diff --git a/lib-renderline.c b/lib-renderline.c index 451051dd..da3c99cb 100644 --- a/lib-renderline.c +++ b/lib-renderline.c @@ -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; diff --git a/lib-tile.c b/lib-tile.c index e5c85c70..868c068f 100644 --- a/lib-tile.c +++ b/lib-tile.c @@ -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; diff --git a/lib-view.c b/lib-view.c index 47bee083..f64b6127 100644 --- a/lib-view.c +++ b/lib-view.c @@ -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; diff --git a/lib-whitespace.c b/lib-whitespace.c index 7b7ebfa2..68ef69f2 100644 --- a/lib-whitespace.c +++ b/lib-whitespace.c @@ -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; diff --git a/lib-wiggle.c b/lib-wiggle.c index b4b3a3ac..fbc20323 100644 --- a/lib-wiggle.c +++ b/lib-wiggle.c @@ -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; diff --git a/lib-x11selection-gtk.c b/lib-x11selection-gtk.c index 852fc37a..7b337c8b 100644 --- a/lib-x11selection-gtk.c +++ b/lib-x11selection-gtk.c @@ -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; diff --git a/lib-x11selection-xcb.c b/lib-x11selection-xcb.c index 89a0c77c..1ff7e55d 100644 --- a/lib-x11selection-xcb.c +++ b/lib-x11selection-xcb.c @@ -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); diff --git a/render-complete.c b/render-complete.c index e12f3e4b..c2da85a2 100644 --- a/render-complete.c +++ b/render-complete.c @@ -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; diff --git a/render-format.c b/render-format.c index d3978a9b..241228f8 100644 --- a/render-format.c +++ b/render-format.c @@ -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; diff --git a/render-hex.c b/render-hex.c index 3f1adf80..28bde9e7 100644 --- a/render-hex.c +++ b/render-hex.c @@ -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; diff --git a/render-lines.c b/render-lines.c index 75fa6f6f..b2fbce12 100644 --- a/render-lines.c +++ b/render-lines.c @@ -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;