From b84ba2c862be7d9b06b3d136ae9381e1d890d47e Mon Sep 17 00:00:00 2001 From: NeilBrown Date: Sat, 12 Dec 2015 10:47:21 +1100 Subject: [PATCH] Clarify key_handle* key_handle should be used most often. It uses the focus given, but moves 'home' out to a leaf and searches in from there. key_handle_filter should be used only when calling a function with the same name as self. It uses focus and home as given and only searches home towards root key_handle_focus should be used when simulting keyboard input. It moves focus out to a leaf, and then sets home from there. key_handle_xy should be used when simulatiing mouse input. it follows focus out to a root tracking x,y and starts home from there. key_handle_focus and key_handle_xy also pick-up ->point if it is set on any pane on the way out. Signed-off-by: NeilBrown --- core-doc.c | 4 +-- core-keymap.c | 68 +++++++++++++++++++++++------------------------ core-mark.c | 6 ++--- core-pane.c | 2 +- core.h | 27 +++++++++---------- display-ncurses.c | 2 +- doc-text.c | 2 +- lib-input.c | 7 +++-- lib-popup.c | 8 +++--- mode-emacs.c | 12 ++++----- render-complete.c | 10 +++---- render-lines.c | 12 ++++----- 12 files changed, 80 insertions(+), 80 deletions(-) diff --git a/core-doc.c b/core-doc.c index 9493969b..012bb09f 100644 --- a/core-doc.c +++ b/core-doc.c @@ -567,7 +567,7 @@ struct pane *doc_open(struct editor *ed, int fd, char *name) ci2.focus = p; ci2.extra = -1; ci2.misc = &stb; - if (key_handle_focus(&ci2) > 0) + if (key_handle(&ci2) > 0) return p; } @@ -703,7 +703,7 @@ char *doc_getstr(struct pane *from, struct mark *to) cr.c = doc_str_callback; cr.s = NULL; ci.comm2 = &cr.c; - ret = key_handle_focus(&ci); + ret = key_handle(&ci); if (!ret) return NULL; return cr.s; diff --git a/core-keymap.c b/core-keymap.c index 12905640..30ee7abe 100644 --- a/core-keymap.c +++ b/core-keymap.c @@ -292,11 +292,13 @@ int key_lookup_cmd_func(const struct cmd_info *ci) return key_lookup(*l->m, ci); } -int key_handle(const struct cmd_info *ci) +/* key_handle_filter is simplest. It just searches towards root for the pane + * which handles the command. + */ +int key_handle_filter(const struct cmd_info *ci) { struct cmd_info *vci = (struct cmd_info*)ci; struct pane *p; - int ret = 0; if (ci->comm) return ci->comm->func(ci); @@ -308,7 +310,8 @@ int key_handle(const struct cmd_info *ci) if (!p) p = ci->focus; - while (ret == 0 && p) { + while (p) { + int ret = 0; if (p->handle) { vci->home = p; vci->comm = p->handle; @@ -316,53 +319,56 @@ int key_handle(const struct cmd_info *ci) } if (ret) /* 'p' might have been destroyed */ - break; + return ret; p = p->parent; } - return ret; + return 0; } -static int __key_handle_focus(struct cmd_info *ci, int savepoint) +/* key_handle move 'home' out to a leaf and searches from there */ +int key_handle(const struct cmd_info *ci) +{ + struct pane *p; + if (ci->comm) + return ci->comm->func(ci); + p = ci->home; + if (!p) + p = ci->focus; + while (p->focus) + p = p->focus; + ((struct cmd_info*)ci)->home = p; + return key_handle_filter(ci); +} + +int key_handle_focus(struct cmd_info *ci) { - /* Handle this in the focus pane, so x,y are irrelevant */ struct pane *p = ci->home; if (!p) p = ci->focus; if (!p) return -1; + /* Handle this in the focus pane, so x,y are irrelevant */ ci->x = -1; ci->y = -1; while (p->focus) { p = p->focus; - if (savepoint && p->pointer) + if (!ci->mark) ci->mark = p->pointer; } - if (!ci->home || !ci->focus) - ci->focus = p; + ci->focus = p; ci->home = p; ci->comm = NULL; return key_handle(ci); } -int key_handle_focus(struct cmd_info *ci) -{ - return __key_handle_focus(ci, 0); -} - -int key_handle_focus_point(struct cmd_info *ci) -{ - int ret = __key_handle_focus(ci, 1); - if (ret < 0) - call5("Message", ci->focus, 0, NULL, "** Command Failed **", 1); - return ret; -} - -static int __key_handle_xy(struct cmd_info *ci, int savepoint) +int key_handle_xy(struct cmd_info *ci) { /* Handle this in child with x,y co-ords */ - struct pane *p = ci->focus; + struct pane *p = ci->home; int x = ci->x; int y = ci->y; + if (!p) + p = ci->focus; while (1) { struct pane *t, *chld = NULL; @@ -381,21 +387,13 @@ static int __key_handle_xy(struct cmd_info *ci, int savepoint) x -= chld->x; y -= chld->y; p = chld; - if (savepoint && p->pointer) + if (!ci->mark) ci->mark = p->pointer; } ci->x = x; ci->y = y; ci->focus = p; + ci->home = p; ci->comm = NULL; return key_handle(ci); } - -int key_handle_xy(struct cmd_info *ci) -{ - return __key_handle_xy(ci, 0); -} -int key_handle_xy_point(struct cmd_info *ci) -{ - return __key_handle_xy(ci, 1); -} diff --git a/core-mark.c b/core-mark.c index cf81a33e..208038a4 100644 --- a/core-mark.c +++ b/core-mark.c @@ -775,7 +775,7 @@ int mark_same_pane(struct pane *p, struct mark *m1, struct mark *m2, ci->mark = m1; ci->mark2 = m2; ci->focus = p; - return key_handle_focus(ci) == 1; + return key_handle(ci) == 1; } /* A 'vmark' is a mark in a particular view. We can walk around those @@ -876,7 +876,7 @@ static int vmark_get(struct pane *p, int view, ci.extra = 1; else if (new) ci.extra = 2; - if (key_handle_focus(&ci) == 0) + if (key_handle(&ci) == 0) return 0; if (first) *first = cr.m; @@ -926,7 +926,7 @@ struct mark *vmark_at_or_before(struct pane *p, struct mark *m, int view) cr.c = take_marks; cr.m = cr.m2 = NULL; ci.comm2 = &cr.c; - if (key_handle_focus(&ci) == 0) + if (key_handle(&ci) == 0) return NULL; return cr.m2; } diff --git a/core-pane.c b/core-pane.c index 64d42076..e491c2f4 100644 --- a/core-pane.c +++ b/core-pane.c @@ -616,7 +616,7 @@ struct pane *call_pane(char *key, struct pane *focus, int numeric, cr.c = take_pane; cr.p = NULL; ci.comm2 = &cr.c; - if (!key_handle_focus(&ci)) + if (!key_handle(&ci)) return NULL; return cr.p; } diff --git a/core.h b/core.h index f5b5eb56..c7cf6210 100644 --- a/core.h +++ b/core.h @@ -297,11 +297,10 @@ struct cmd_info { struct map *key_alloc(void); void key_free(struct map *m); +int key_handle_filter(const struct cmd_info *ci); int key_handle(const struct cmd_info *ci); int key_handle_focus(struct cmd_info *ci); int key_handle_xy(struct cmd_info *ci); -int key_handle_focus_point(struct cmd_info *ci); -int key_handle_xy_point(struct cmd_info *ci); int key_lookup(struct map *m, const const struct cmd_info *ci); struct command *key_lookup_cmd(struct map *m, char *c); void key_add(struct map *map, char *k, struct command *comm); @@ -412,7 +411,7 @@ static inline void doc_replace(struct pane *p, struct mark *m, ci.str = str; ci.extra = *first; ci.numeric = 1; - ret = key_handle_focus(&ci); + ret = key_handle(&ci); if (ret == 1) *first = 1; } @@ -422,7 +421,7 @@ static inline int doc_undo(struct pane *p, bool redo) ci.focus = p; ci.numeric = redo ? 1 : 0; ci.key = "doc:reundo"; - return key_handle_focus(&ci); + return key_handle(&ci); } static inline int doc_load_file(struct pane *p, int fd, char *name) { @@ -431,7 +430,7 @@ static inline int doc_load_file(struct pane *p, int fd, char *name) ci.extra = fd; ci.str = name; ci.key = "doc:load-file"; - return key_handle_focus(&ci); + return key_handle(&ci); } static inline int doc_set_attr(struct pane *p, struct mark *pt, @@ -444,7 +443,7 @@ static inline int doc_set_attr(struct pane *p, struct mark *pt, ci.mark = pt; ci.str = attr; ci.str2 = val; - return key_handle_focus(&ci); + return key_handle(&ci); } @@ -456,7 +455,7 @@ static inline int doc_add_view(struct pane *p, struct command *c, int size) ci.key = "doc:add-view"; ci.comm2 = c; ci.extra = size; - ret = key_handle_focus(&ci); + ret = key_handle(&ci); if (ret <= 0) return -1; return ret - 1; @@ -468,7 +467,7 @@ static inline void doc_del_view(struct pane *p, struct command *c) ci.focus = p; ci.key = "doc:del-view"; ci.comm2 = c; - key_handle_focus(&ci); + key_handle(&ci); } static inline int doc_find_view(struct pane *p, struct command *c) @@ -479,7 +478,7 @@ static inline int doc_find_view(struct pane *p, struct command *c) ci.focus = p; ci.key = "doc:find-view"; ci.comm2 = c; - ret = key_handle_focus(&ci); + ret = key_handle(&ci); if (ret <= 0) return -1; return ret - 1; @@ -493,7 +492,7 @@ static inline int call3(char *key, struct pane *focus, int numeric, struct mark ci.focus = focus; ci.numeric = numeric; ci.mark = m; - return key_handle_focus(&ci); + return key_handle(&ci); } static inline int call_home(struct pane *home, char *key, struct pane *focus, @@ -507,7 +506,7 @@ static inline int call_home(struct pane *home, char *key, struct pane *focus, ci.numeric = numeric; ci.mark = m; ci.comm2 = comm; - return key_handle_focus(&ci); + return key_handle(&ci); } static inline int call5(char *key, struct pane *focus, int numeric, struct mark *m, @@ -521,7 +520,7 @@ static inline int call5(char *key, struct pane *focus, int numeric, struct mark ci.mark = m; ci.str = str; ci.extra = extra; - return key_handle_focus(&ci); + return key_handle(&ci); } static inline int call7(char *key, struct pane *focus, int numeric, struct mark *m, @@ -537,7 +536,7 @@ static inline int call7(char *key, struct pane *focus, int numeric, struct mark ci.str = str; ci.str2 = str2; ci.extra = extra; - return key_handle_focus(&ci); + return key_handle(&ci); } struct call_return { @@ -560,7 +559,7 @@ static inline int call_comm(char *key, struct pane *focus, int numeric, struct m ci.str = str; ci.extra = extra; ci.comm2 = comm; - return key_handle_focus(&ci); + return key_handle(&ci); } static inline int comm_call(struct command *comm, char *key, struct pane *focus, diff --git a/display-ncurses.c b/display-ncurses.c index d0e95b1a..56fa3ad1 100644 --- a/display-ncurses.c +++ b/display-ncurses.c @@ -341,7 +341,7 @@ static void do_send_mouse(struct pane *p, int x, int y, char *cmd) ci.focus = p; ci.x = x; ci.y = y; - key_handle_xy(&ci); + key_handle(&ci); } static void send_mouse(MEVENT *mev, struct pane *p) diff --git a/doc-text.c b/doc-text.c index 5b3a1a7c..9cd5cdf0 100644 --- a/doc-text.c +++ b/doc-text.c @@ -1108,7 +1108,7 @@ DEF_CMD(text_get_str) char *ret; int l = 0, head, tail; - if (ci->mark) { + if (ci->mark && ci->mark2) { if (mark_ordered(ci->mark2, ci->mark)) { from = ci->mark2; to = ci->mark; diff --git a/lib-input.c b/lib-input.c index e6aa88a3..1b837839 100644 --- a/lib-input.c +++ b/lib-input.c @@ -49,6 +49,7 @@ DEF_CMD(keystroke) struct cmd_info ci2 = {0}; struct input_mode *im = ci->home->data; int l; + int ret; l = strlen(im->mode) + strlen(ci->str) + 1; ci2.key = malloc(l); @@ -60,8 +61,10 @@ DEF_CMD(keystroke) im->mode = ""; im->numeric = NO_NUMERIC; im->extra = 0; - key_handle_focus_point(&ci2); + ret = key_handle_focus(&ci2); free(ci2.key); + if (ret < 0) + call5("Message", ci2.focus, 0, NULL, "** Command Failed **", 1); return 0; } @@ -85,7 +88,7 @@ DEF_CMD(mouse_event) im->numeric = NO_NUMERIC; im->extra = 0; - key_handle_xy_point(&ci2); + key_handle_xy(&ci2); return 0; } diff --git a/lib-popup.c b/lib-popup.c index d1b8e469..628011a7 100644 --- a/lib-popup.c +++ b/lib-popup.c @@ -113,7 +113,7 @@ DEF_CMD(popup_handle) if (ppi->doc) ci2.str = doc_getstr(ppi->popup, NULL); ci2.mark = NULL; - key_handle_focus(&ci2); + key_handle(&ci2); if (ppi->doc) free(ci2.str); d = ppi->doc; ppi->doc = NULL; @@ -142,7 +142,7 @@ DEF_CMD(popup_quote) ci2.numeric = ci->numeric; ci2.extra = ci->extra; ci2.mark = ci->mark; - return key_handle_focus(&ci2); + return key_handle(&ci2); } DEF_CMD(popup_attach) @@ -214,9 +214,9 @@ DEF_CMD(popup_attach) ci2.focus = p; ci2.str = "popup:quote"; ci2.str2 = "Return"; - key_handle_focus(&ci2); + key_handle(&ci2); ci2.str2 = "Abort"; - key_handle_focus(&ci2); + key_handle(&ci2); return comm_call(ci->comm2, "callback:attach", ppi->popup, 0, NULL, NULL, 0); } diff --git a/mode-emacs.c b/mode-emacs.c index bd52b775..da08c9d7 100644 --- a/mode-emacs.c +++ b/mode-emacs.c @@ -188,7 +188,7 @@ REDEF_CMD(emacs_str) ci2.numeric = ci->numeric; ci2.extra = ci->extra; ci2.mark = ci->mark; - return key_handle_focus(&ci2); + return key_handle(&ci2); } DEF_CMD(emacs_insert) @@ -287,7 +287,7 @@ DEF_CMD(emacs_findfile) ci2.focus = p; ci2.str = "emacs:file-complete"; ci2.str2 = "Tab"; - key_handle_focus(&ci2); + key_handle(&ci2); return 1; } @@ -373,7 +373,7 @@ DEF_CMD(emacs_file_complete) cr.c = save_str; cr.s = NULL; ci2.comm2 = &cr.c; - ret = key_handle_focus(&ci2); + ret = key_handle(&ci2); free(d); if (cr.s && (strlen(cr.s) <= strlen(b) && ret-1 > 1)) { /* We need the dropdown */ @@ -420,7 +420,7 @@ DEF_CMD(emacs_finddoc) ci2.focus = p; ci2.str = "emacs:doc-complete"; ci2.str2 = "Tab"; - key_handle_focus(&ci2); + key_handle(&ci2); return 1; } @@ -470,7 +470,7 @@ DEF_CMD(emacs_doc_complete) cr.c = save_str; cr.s = NULL; ci2.comm2= &cr.c; - ret = key_handle_focus(&ci2); + ret = key_handle(&ci2); if (cr.s && (strlen(cr.s) <= strlen(str) && ret - 1 > 1)) { /* We need the dropdown */ pane_damaged(par, DAMAGED_CONTENT); @@ -568,7 +568,7 @@ DEF_CMD(emacs_search) ci2.str = "Search String"; ci2.str2 = ci->str; ci2.focus = ci->home; - key_handle_focus(&ci2); + key_handle(&ci2); memset(&ci2, 0, sizeof(ci2)); ci2.focus = ci->home; diff --git a/render-complete.c b/render-complete.c index f31be0e3..1afd80a1 100644 --- a/render-complete.c +++ b/render-complete.c @@ -88,7 +88,7 @@ DEF_CMD(render_complete_line) cr.i = plen; cr.s = NULL; ci2.comm2 = &cr.c; - if (key_handle(&ci2) == 0) + if (key_handle_filter(&ci2) == 0) return 0; ret = comm_call(ci->comm2, "callback:render", ci->focus, 0, NULL, cr.s, 0); @@ -104,7 +104,7 @@ DEF_CMD(render_complete_line) cb.prefix = cd->prefix; cb.cmp = 0; ci2.comm2 = &cb.c; - key_handle(&ci2); + key_handle_filter(&ci2); if (cb.cmp == 0) break; @@ -151,7 +151,7 @@ DEF_CMD(render_complete_prev) cb.plen = strlen(cb.prefix); ci3.comm2 = &cb.c; while (1) { - ret = key_handle(&ci2); + ret = key_handle_filter(&ci2); if (ret <= 0 || ci->numeric == 0) /* Either hit start-of-file, or have what we need */ break; @@ -164,7 +164,7 @@ DEF_CMD(render_complete_prev) ci3.numeric = NO_NUMERIC; cb.keep = ci2.numeric == 1 && ci->extra == 42; cb.str = NULL; - if (key_handle(&ci3) != 1) { + if (key_handle_filter(&ci3) != 1) { mark_free(ci3.mark); break; } @@ -373,7 +373,7 @@ DEF_CMD(complete_return) ci2.focus = ci->home->parent; ci2.str = cr.s + strlen(cd->prefix); ci2.numeric = NO_NUMERIC; - key_handle(&ci2); + key_handle_filter(&ci2); free(cr.s); return 1; } diff --git a/render-lines.c b/render-lines.c index 988ec6f8..45107ce6 100644 --- a/render-lines.c +++ b/render-lines.c @@ -263,7 +263,7 @@ static struct mark *call_render_line_prev(struct pane *p, ci.mark = m; ci.focus = p; ci.numeric = n; - ret = key_handle_focus(&ci); + ret = key_handle(&ci); if (ret == 0) { mark_free(m); return NULL; @@ -312,7 +312,7 @@ static struct mark *call_render_line(struct pane *p, struct mark *start) * 'used' can be negative if the mark is before the start * of the pane */ - if (key_handle_focus(&ci) == 0) { + if (key_handle(&ci) == 0) { mark_free(ci.mark); return NULL; } @@ -353,7 +353,7 @@ static struct mark *call_render_line_offset(struct pane *p, ci.mark = mark_dup(start, 0); ci.numeric = offset; ci.comm2 = &no_save; - if (key_handle_focus(&ci) == 0) { + if (key_handle(&ci) == 0) { mark_free(ci.mark); return NULL; } @@ -380,7 +380,7 @@ static int call_render_line_to_point(struct pane *p, struct mark *pm, ci.mark = mark_dup(start, 0); ci.numeric = -1; ci.comm2 = &get_len; - len = key_handle_focus(&ci); + len = key_handle(&ci); if (len <= 0) { mark_free(ci.mark); return 0; @@ -828,12 +828,12 @@ DEF_CMD(render_lines_move_line) else ci2.numeric += 1; ci2.mark = ci->mark; - if (!key_handle_focus(&ci2)) + if (!key_handle(&ci2)) return -1; if (RPT_NUM(ci) > 0) { /* at end of target line, move to start */ ci2.numeric = -1; - if (!key_handle_focus(&ci2)) + if (!key_handle(&ci2)) return -1; } -- 2.39.5