]> git.neil.brown.name Git - edlib.git/commitdiff
Clarify key_handle*
authorNeilBrown <neil@brown.name>
Fri, 11 Dec 2015 23:47:21 +0000 (10:47 +1100)
committerNeilBrown <neil@brown.name>
Fri, 11 Dec 2015 23:47:21 +0000 (10:47 +1100)
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 <neil@brown.name>
12 files changed:
core-doc.c
core-keymap.c
core-mark.c
core-pane.c
core.h
display-ncurses.c
doc-text.c
lib-input.c
lib-popup.c
mode-emacs.c
render-complete.c
render-lines.c

index 9493969b984f2f5b6dca08df0b516f2cfdba8386..012bb09f3a8da549975338d4bd173e7af06c3383 100644 (file)
@@ -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;
index 12905640d1c757a8b4d22f2b9ab7632f772d0742..30ee7abe9f4a9c95d159d169e0465d92f42fa4ab 100644 (file)
@@ -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);
-}
index cf81a33ee6e6ddf48e4b4cf9f97fdbd53df52a35..208038a48e1abe5c018a3eadf1ecb78fe5eb716c 100644 (file)
@@ -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;
 }
index 64d420767f722e509c9fdd88ec51debdb3d1f633..e491c2f4318fe97fbd73dc5572bda94d87bb76bb 100644 (file)
@@ -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 f5b5eb5620c502d71a2ff1ea1b08bfd4b46e1c47..c7cf6210fd813dfdfff0e6785aa4f0bf7072fefd 100644 (file)
--- 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,
index d0e95b1a3ecaa6124b39694dfb6f280e849d95cb..56fa3ad1a6fb40fe1bb974c102adb088e8ee8249 100644 (file)
@@ -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)
index 5b3a1a7c9abd4315fc8543a5e230fe803b311f81..9cd5cdf074cffe0f87b3fbceb78fd8b967533c3a 100644 (file)
@@ -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;
index e6aa88a39c755769b5041e70f1d1d64ffce86251..1b8378391e5e98de02663d3f5dc3c6dda56d01bc 100644 (file)
@@ -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;
 }
 
index d1b8e46902b7f2d1eadb5902a5faaaa400e42a37..628011a722329bba8129ec49873e9b388acdd414 100644 (file)
@@ -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);
 }
index bd52b775e70c53fedeeba2321057026edc92bc29..da08c9d737a5c6e952f07c0b7a08192e54163f44 100644 (file)
@@ -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;
index f31be0e309211a75e9ff83e3a279629144a0c6ea..1afd80a18fb332dfc9f9dcb0aa86bb0c24751e7d 100644 (file)
@@ -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;
 }
index 988ec6f888f1e01f952aafe97b510efcd2f12771..45107ce6d296852e2638a0cb9cbacac6de75712c 100644 (file)
@@ -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;
        }