From: NeilBrown Date: Tue, 1 Dec 2015 22:11:32 +0000 (+1100) Subject: Introduce some 'call*' functions to call command. X-Git-Tag: lca2016~146 X-Git-Url: http://git.neil.brown.name/?a=commitdiff_plain;h=9396e27ec83f9551a0014b8482c37c1aab6ad1a8;p=edlib.git Introduce some 'call*' functions to call command. This simplifies lots of code. Signed-off-by: NeilBrown --- diff --git a/core-doc.c b/core-doc.c index bd5a16dd..966233f2 100644 --- a/core-doc.c +++ b/core-doc.c @@ -563,7 +563,6 @@ struct pane *doc_from_text(struct pane *parent, char *name, char *text) bool first = 1; struct pane *p; struct doc *d; - struct cmd_info ci = {0}; d = doc_new(pane2ed(parent), "text"); if (!d) @@ -575,10 +574,7 @@ struct pane *doc_from_text(struct pane *parent, char *name, char *text) return p; } doc_replace(p, NULL, text, &first); - ci.key = "Move-File"; - ci.numeric = -1; - ci.focus = p; - key_handle_focus(&ci); + call3("Move-File", p, -1, NULL); return p; } @@ -754,11 +750,9 @@ DEF_CMD(docs_open) renderer = "hex"; if (strcmp(ci->key, "Chr-o") == 0) { - struct cmd_info ci2 = {0}; - ci2.key = "OtherPane"; - ci2.focus = ci->focus; - if (key_handle_focus(&ci2)) { - par = ci2.focus; + struct pane *p2 = call_pane("OtherPane", ci->focus, 0, NULL, 0); + if (p2) { + par = p2; p = pane_child(par); } } @@ -859,7 +853,6 @@ int doc_destroy(struct doc *d) * the documents list and destroy it. */ int i; - struct cmd_info ci2 = {0}; d->deleting = 1; if (d == d->ed->docs) @@ -877,9 +870,7 @@ int doc_destroy(struct doc *d) docs_release(d); - ci2.key = "doc:destroy"; - ci2.focus = d->home; - key_handle_focus(&ci2); + call3("doc:destroy", d->home, 0, 0); pane_close(d->home); free(d->views); diff --git a/core-mark.c b/core-mark.c index 3601b679..09ddc77a 100644 --- a/core-mark.c +++ b/core-mark.c @@ -161,14 +161,7 @@ struct mark *do_mark_at_point(struct doc *d, struct mark *pt, int view) struct mark *mark_at_point(struct pane *p, struct mark *pm, int view) { - struct cmd_info ci = {0}; - ci.key = "doc:dup-point"; - ci.extra = view; - ci.mark = pm; - ci.focus = p; - if (key_handle_focus(&ci) == 0) - return NULL; - return ci.mark; + return call_mark("doc:dup-point", p, 0, pm, view); } struct mark *point_dup(struct mark *p) @@ -259,7 +252,6 @@ struct mark *mark_dup(struct mark *m, int notype) void __mark_reset(struct doc *d, struct mark *m, int new, int end) { int i; - struct cmd_info ci = {0}; int seq = 0; struct point_links *lnk; @@ -281,11 +273,7 @@ void __mark_reset(struct doc *d, struct mark *m, int new, int end) hlist_add_head(&m->all, &d->marks); assign_seq(m, seq); - ci.key = "doc:set-ref"; - ci.mark = m; - ci.numeric = !end; /* start */ - ci.focus = d->home; - key_handle_focus(&ci); + call3("doc:set-ref", d->home, !end, m); if (m->viewnum == MARK_UNGROUPED) return; @@ -508,13 +496,7 @@ void mark_backward_over(struct mark *m, struct mark *mp) wint_t mark_step(struct doc *d, struct mark *m, int forward, int move, struct cmd_info *ci) { - ci->key = "doc:step"; - ci->focus = d->home; - ci->mark = m; - ci->numeric = forward; - ci->extra = move; - key_handle_focus(ci); - return ci->extra; + return call_extra("doc:step", d->home, forward, m, move); } wint_t mark_step2(struct doc *d, struct mark *m, int forward, int move) diff --git a/core.h b/core.h index d349e22f..4e0b9723 100644 --- a/core.h +++ b/core.h @@ -470,3 +470,87 @@ static inline struct doc *doc_from_pane(struct pane *p) return ci.misc; } +static inline int call3(char *key, struct pane *focus, int numeric, struct mark *m) +{ + struct cmd_info ci = {0}; + + ci.key = key; + ci.focus = focus; + ci.numeric = numeric; + ci.mark = m; + return key_handle_focus(&ci); +} + +static inline int call5(char *key, struct pane *focus, int numeric, struct mark *m, + char *str, int extra) +{ + struct cmd_info ci = {0}; + + ci.key = key; + ci.focus = focus; + ci.numeric = numeric; + ci.mark = m; + ci.str = str; + ci.extra = extra; + return key_handle_focus(&ci); +} + +static inline struct mark *call_mark(char *key, struct pane *focus, int numeric, + struct mark *m, int extra) +{ + struct cmd_info ci = {0}; + + ci.key = key; + ci.focus = focus; + ci.numeric = numeric; + ci.extra = extra; + ci.mark = m; + if (!key_handle_focus(&ci)) + return NULL; + return ci.mark; +} + +static inline struct pane *call_pane(char *key, struct pane *focus, int numeric, + struct mark *m, int extra) +{ + struct cmd_info ci = {0}; + + ci.key = key; + ci.focus = focus; + ci.numeric = numeric; + ci.extra = extra; + ci.mark = m; + if (!key_handle_focus(&ci)) + return NULL; + return ci.focus; +} + +static inline int call_extra(char *key, struct pane *focus, int numeric, struct mark *m, + int extra) +{ + struct cmd_info ci = {0}; + + ci.key = key; + ci.focus = focus; + ci.numeric = numeric; + ci.extra = extra; + ci.mark = m; + if (!key_handle_focus(&ci)) + return 0; + return ci.extra; +} + +static inline char *call_str(char *key, struct pane *focus, int numeric, struct mark *m, + char *str) +{ + struct cmd_info ci = {0}; + + ci.key = key; + ci.focus = focus; + ci.numeric = numeric; + ci.str = str; + ci.mark = m; + if (!key_handle_focus(&ci)) + return NULL; + return ci.str; +} diff --git a/doc-dir.c b/doc-dir.c index 34cd12b9..31ea8855 100644 --- a/doc-dir.c +++ b/doc-dir.c @@ -518,11 +518,9 @@ DEF_CMD(dir_open) asprintf(&fname, "%s/%s", dr->fname, de->name); fd = open(fname, O_RDONLY); if (strcmp(ci->key, "Chr-o") == 0) { - struct cmd_info ci2 = {0}; - ci2.key = "OtherPane"; - ci2.focus = ci->focus; - if (key_handle_focus(&ci2)) { - par = ci2.focus; + struct pane *p2 = call_pane("OtherPane", ci->focus, 0, NULL, 0); + if (p2) { + par = p2; p = pane_child(par); } } diff --git a/edlib.c b/edlib.c index cc1f76bb..6a872d03 100644 --- a/edlib.c +++ b/edlib.c @@ -70,10 +70,8 @@ int main(int argc, char *argv[]) global = pane_attach(root, "global-keymap", NULL, NULL); editor_load_module(ed, "mode-emacs"); - ci.focus = global; - ci.key = "global-set-keymap"; - ci.str = "mode-emacs"; - key_handle_focus(&ci); + call5("global-set-keymap", global, 0, NULL, "mode-emacs", 0); + b = pane_attach(global, "tile", NULL, NULL); if (b) p = doc_from_text(b, "*Welcome*", WelcomeText); diff --git a/emacs-search.c b/emacs-search.c index 4ecef747..afd08a1e 100644 --- a/emacs-search.c +++ b/emacs-search.c @@ -104,7 +104,6 @@ DEF_CMD(search_add) char b[5]; mbstate_t ps = {0}; int l; - struct cmd_info ci2 = {0}; if (!d) return -1; @@ -129,12 +128,7 @@ DEF_CMD(search_add) } else l = wcrtomb(b, wch, &ps); b[l] = 0; - ci2.key = "Replace"; - ci2.str = b; - ci2.numeric = 1; - ci2.mark = NULL; - ci2.focus = ci->focus; - key_handle_focus(&ci2); + call5("Replace", ci->focus, 1, NULL, b, 0); } while (strcmp(ci->key, "C-Chr-C") != 0 && wch != ' '); return 1; } @@ -198,10 +192,7 @@ REDEF_CMD(search_again) point_to_mark(esi->end, m); /* TEMP HACK - please fix */ doc_set_attr(esi->target, esi->end, "highlight","fg:red,inverse"); - ci2.key = "Move-View-Pos"; - ci2.focus = esi->target; - ci2.mark = esi->end; - key_handle_focus(&ci2); + call3("Move-View-Pos", esi->target, 0, esi->end); esi->matched = 1; pfx = "Search: "; } else { @@ -238,28 +229,23 @@ DEF_CMD(emacs_search) { struct pane *p; struct es_info *esi; - struct cmd_info ci2 = {0}; + struct mark *m; if (!es_map) emacs_search_init_map(); - ci2.key = "popup:get-target"; - ci2.focus = ci->focus; - if (key_handle_focus(&ci2) == 0) - return 0; + p = call_pane("popup:get-target", ci->focus, 0, 0, 0); + if (!p) + return -1; esi = malloc(sizeof(*esi)); - esi->target = ci2.focus; - memset(&ci2, 0, sizeof(ci2)); - ci2.key = "doc:dup-point"; - ci2.extra = MARK_POINT; - ci2.focus = esi->target; - key_handle_focus(&ci2); - if (!ci2.mark) { + esi->target = p; + m = call_mark("doc:dup-point", p, 0, NULL, MARK_POINT); + if (!m) { free(esi); return -1; } - esi->end = ci2.mark; + esi->end = m; - esi->start = mark_dup(ci2.mark, 1); + esi->start = mark_dup(m, 1); esi->s = NULL; esi->matched = 0; esi->search = ci->focus; diff --git a/lib-popup.c b/lib-popup.c index 4ea0d718..40d018c8 100644 --- a/lib-popup.c +++ b/lib-popup.c @@ -140,14 +140,11 @@ DEF_CMD(popup_attach) if (strchr(style, 'D')) { int x = 0, y = 0; pane_to_root(ci->focus, &x, &y, &z, NULL, NULL); - ci2.key = "global-key-root"; + root = call_pane("global-key-root", ci->focus, 0, NULL, 0); } else - ci2.key = "ThisPane"; - ci2.focus = ci->focus; - - if (!key_handle_focus(&ci2)) + root = call_pane("ThisPane", ci->focus, 0, NULL, 0); + if (!root) return 0; - root = ci2.focus; ppi->target = ci->focus; ppi->popup = pane_register(root, z, &popup_handle, ppi, NULL); @@ -172,7 +169,7 @@ DEF_CMD(popup_attach) p = doc_attach_view(ppi->popup, d->home, NULL); } pane_focus(p); - memset(&ci2, 0, sizeof(ci2)); + ci2.key = "local-set-key"; ci2.focus = p; ci2.str = "popup:quote"; diff --git a/lib-view.c b/lib-view.c index 8735d54e..11970399 100644 --- a/lib-view.c +++ b/lib-view.c @@ -251,7 +251,8 @@ DEF_CMD(view_click) struct pane *p = ci->home; struct view_data *vd = p->data; int mid = vd->scroll_bar_y; - struct cmd_info ci2 = {0}; + char *key; + int num; if (ci->hx != 0) return 0; @@ -259,25 +260,24 @@ DEF_CMD(view_click) return 0; p = pane_child(p); - ci2.focus = p; - ci2.key = "Move-View-Small"; - ci2.numeric = RPT_NUM(ci); - ci2.mark = ci->mark; + + key = "Move-View-Small"; + num = RPT_NUM(ci); if (ci->hy == mid-1) { /* scroll up */ - ci2.numeric = -ci2.numeric; + num = -num; } else if (ci->hy < mid-1) { /* big scroll up */ - ci2.numeric = -ci2.numeric; - ci2.key = "Move-View-Large"; + num = -num; + key = "Move-View-Large"; } else if (ci->hy == mid+1) { /* scroll down */ } else if (ci->hy > mid+1 && ci->hy < p->h-1) { - ci2.key = "Move-View-Large"; + key = "Move-View-Large"; } else return 0; - return key_handle_focus(&ci2); + return call3(key, p, num, NULL); } void edlib_init(struct editor *ed) diff --git a/mode-emacs.c b/mode-emacs.c index a1b7e6e0..61a7ff86 100644 --- a/mode-emacs.c +++ b/mode-emacs.c @@ -78,12 +78,7 @@ REDEF_CMD(emacs_move) return 0; old_x = cursor_pane->cx; - ci2.focus = ci->focus; - ci2.key = mv->type; - ci2.numeric = mv->direction * RPT_NUM(ci); - ci2.mark = ci->mark; - ret = key_handle_focus(&ci2); - + ret = call3(mv->type, ci->focus, mv->direction * RPT_NUM(ci), ci->mark); if (!ret) return 0; @@ -95,6 +90,7 @@ REDEF_CMD(emacs_move) ci2.key = "Move-CursorXY"; ci2.numeric = 1; ci2.x = old_x; + ci2.mark = ci->mark; if (mv->direction == 1) ci2.y = 0; else @@ -110,31 +106,24 @@ REDEF_CMD(emacs_move) REDEF_CMD(emacs_delete) { struct move_command *mv = container_of(ci->comm, struct move_command, cmd); - struct cmd_info ci2 = {0}; int ret = 0; struct mark *m; struct doc *d = doc_from_pane(ci->home); m = mark_dup(ci->mark, 1); - ci2.focus = ci->focus; - ci2.key = mv->type; - ci2.numeric = mv->direction * RPT_NUM(ci); - if (strcmp(mv->type, "Move-EOL") == 0 && ci2.numeric == 1 && + + if (strcmp(mv->type, "Move-EOL") == 0 && + mv->direction == 1 && RPT_NUM(ci) == 1 && doc_following(d, m) == '\n') - ci2.key = "Move-Char"; - ci2.mark = m; - ret = key_handle_focus(&ci2); + ret = call3("Move-Char", ci->focus, mv->direction * RPT_NUM(ci), m); + else + ret = call3(mv->type, ci->focus, mv->direction * RPT_NUM(ci), m); + if (!ret) { mark_free(m); return 0; } - ci2.focus = ci->focus; - ci2.key = "Replace"; - ci2.numeric = 1; - ci2.extra = ci->extra; - ci2.mark = m; - ci2.str = NULL; - ret = key_handle_focus(&ci2); + ret = call5("Replace", ci->focus, 1, m, NULL, ci->extra); mark_free(m); pane_set_extra(ci->home, 1); @@ -179,19 +168,12 @@ REDEF_CMD(emacs_str) DEF_CMD(emacs_insert) { - char str[5]; - struct cmd_info ci2 = {0}; int ret; + char *str; - ci2.focus = ci->focus; - ci2.key = "Replace"; - ci2.numeric = 1; - ci2.extra = ci->extra; - ci2.mark = ci->mark; - strncpy(str,ci->key+4, sizeof(str)); - str[4] = 0; - ci2.str = str; - ret = key_handle_focus(&ci2); + /* Key is "Chr-X" - skip 4 bytes to get X */ + str = ci->key + 4; + ret = call5("Replace", ci->focus, 1, ci->mark, str, ci->extra); pane_set_extra(ci->home, 1); return ret; @@ -209,25 +191,18 @@ static struct { DEF_CMD(emacs_insert_other) { - struct pane *p = ci->home; - struct cmd_info ci2 = {0}; int ret; int i; - ci2.focus = ci->focus; - ci2.key = "Replace"; - ci2.numeric = 1; - ci2.extra = ci->extra; - ci2.mark = ci->mark; for (i = 0; other_inserts[i].key; i++) if (strcmp(other_inserts[i].key, ci->key) == 0) break; if (other_inserts[i].key == NULL) return 0; - ci2.str = other_inserts[i].insert; - ret = key_handle_focus(&ci2); - pane_set_extra(p, 0); /* A newline starts a new undo */ + ret = call5("Replace", ci->focus, 1, ci->mark, other_inserts[i].insert, + ci->extra); + pane_set_extra(ci->home, 0); /* A newline starts a new undo */ return ret; } @@ -279,18 +254,10 @@ DEF_CMD(emacs_findfile) attr_set_str(&p->attrs, "prefix", "Find File: ", -1); attr_set_str(&p->attrs, "done-key", "File Found", -1); } - ci2.key = "doc:set-name"; - ci2.focus = p; - ci2.str = "Find File"; - key_handle_focus(&ci2); - if (path) { - memset(&ci2, 0, sizeof(ci2)); - ci2.key = "Replace"; - ci2.focus = p; - ci2.str = path; - key_handle_focus(&ci2); - } - memset(&ci2, 0, sizeof(ci2)); + call5("doc:set-name", p, 0, NULL, "Find File", 0); + if (path) + call5("Replace", p, 0, NULL, path, 0); + ci2.key = "local-set-key"; ci2.focus = p; ci2.str = "emacs:file-complete"; @@ -300,13 +267,12 @@ DEF_CMD(emacs_findfile) } if (strcmp(ci->key, "File Found Other Window") == 0) - ci2.key = "OtherPane"; + p = call_pane("OtherPane", ci->focus, 0, NULL, 0); else - ci2.key = "ThisPane"; - ci2.focus = ci->focus; - if (key_handle_focus(&ci2) == 0) + p = call_pane("ThisPane", ci->focus, 0, NULL, 0); + + if (!p) return -1; - p = ci2.focus; par = p; /* par is the tile */ @@ -379,13 +345,8 @@ DEF_CMD(emacs_file_complete) if (ci2.str) { /* add the extra chars from ci2.str */ char *c = ci2.str + strlen(b); - struct cmd_info ci3 = {0}; - ci3.key = "Replace"; - ci3.mark = ci->mark; - ci3.numeric = 1; - ci3.focus = ci->focus; - ci3.str = c; - key_handle_focus(&ci3); + + call5("Replace", ci->focus, 1, ci->mark, c, 0); } /* Now need to close the popup */ pane_close(pop); @@ -412,12 +373,8 @@ DEF_CMD(emacs_finddoc) attr_set_str(&p->attrs, "prefix", "Find Document: ", -1); attr_set_str(&p->attrs, "done-key", "Doc Found", -1); } - ci2.key = "doc:set-name"; - ci2.focus = p; - ci2.str = "Find Document"; - key_handle_focus(&ci2); + call5("doc:set-name", p, 0, NULL, "Find Document", 0); - memset(&ci2, 0, sizeof(ci2)); ci2.key = "local-set-key"; ci2.focus = p; ci2.str = "emacs:doc-complete"; @@ -427,13 +384,11 @@ DEF_CMD(emacs_finddoc) } if (strcmp(ci->key, "Doc Found Other Window") == 0) - ci2.key = "OtherPane"; + p = call_pane("OtherPane", ci->focus, 0, NULL, 0); else - ci2.key = "ThisPane"; - ci2.focus = ci->focus; - if (key_handle_focus(&ci2) == 0) + p = call_pane("ThisPane", ci->focus, 0, NULL, 0); + if (!p) return -1; - p = ci2.focus; par = p; /* par is the tile */ @@ -477,13 +432,8 @@ DEF_CMD(emacs_doc_complete) if (ci2.str) { /* add the extra chars from ci2.str */ char *c = ci2.str + strlen(str); - struct cmd_info ci3 = {0}; - ci3.key = "Replace"; - ci3.mark = ci->mark; - ci3.numeric = 1; - ci3.focus = ci->focus; - ci3.str = c; - key_handle_focus(&ci3); + + call5("Replace", ci->focus, 1, ci->mark, c, 0); } /* Now need to close the popup */ pane_close(pop); @@ -494,13 +444,10 @@ DEF_CMD(emacs_viewdocs) { struct pane *p, *par; struct doc *d; - struct cmd_info ci2 = {0}; - ci2.key = "ThisPane"; - ci2.focus = ci->focus; - if (key_handle_focus(&ci2) == 0) + par = call_pane("ThisPane", ci->focus, 0, NULL, 0); + if (!par) return -1; - par = ci2.focus; /* par is the tile */ d = pane2ed(par)->docs; @@ -557,10 +504,7 @@ DEF_CMD(emacs_search) attr_set_str(&p->attrs, "prefix", "Search: ", -1); attr_set_str(&p->attrs, "done-key", "Search String", -1); - ci2.key = "doc:set-name"; - ci2.focus = p; - ci2.str = "Search"; - key_handle_focus(&ci2); + call5("doc:set-name", p, 0, NULL, "Search", 0); p = pane_final_child(p); pane_attach(p, "emacs-search", NULL, NULL); @@ -570,11 +514,7 @@ DEF_CMD(emacs_search) if (!ci->str || !ci->str[0]) return -1; - ci2.key = "doc:dup-point"; - ci2.focus = ci->home; - ci2.extra = MARK_UNGROUPED; - key_handle_focus(&ci2); - m = ci2.mark; + m = call_mark("doc:dup-point", ci->home, 0, NULL, MARK_UNGROUPED); memset(&ci2, 0, sizeof(ci2)); ci2.focus = ci->home; @@ -583,13 +523,9 @@ DEF_CMD(emacs_search) ci2.key = "text-search"; if (!key_lookup(pane2ed(ci->focus)->commands, &ci2)) ci2.extra = -1; - if (ci2.extra > 0) { - memset(&ci2, 0, sizeof(ci2)); - ci2.key = "Move-to"; - ci2.mark = m; - ci2.focus = ci->focus; - key_handle_focus(&ci2); - } + if (ci2.extra > 0) + call3("Move-to", ci->focus, 0, m); + mark_free(m); return 1; } diff --git a/render-complete.c b/render-complete.c index fb2aba54..c27c2068 100644 --- a/render-complete.c +++ b/render-complete.c @@ -216,21 +216,9 @@ DEF_CMD(complete_set_prefix) free(cd->prefix); cd->prefix = strdup(ci->str); - ci2.key = "doc:dup-point"; - ci2.focus = ci->home; - ci2.extra = MARK_UNGROUPED; - key_handle_focus(&ci2); - m = ci2.mark; - - memset(&ci2, 0, sizeof(ci2)); - - ci2.key = "Move-File"; - ci2.focus = ci->home; - ci2.numeric = 1; - ci2.mark = m; - key_handle_focus(&ci2); + m = call_mark("doc:dup-point", ci->home, 0, NULL, MARK_UNGROUPED); + call3("Move-File", ci->home, 1, m); - memset(&ci2, 0, sizeof(ci2)); ci2.key = "render-line-prev"; ci2.numeric = 1; ci2.mark = m; @@ -250,16 +238,9 @@ DEF_CMD(complete_set_prefix) } ci->extra = cnt; ci->str = common; - memset(&ci2, 0, sizeof(ci2)); - ci2.key = "Move-to"; - ci2.mark = m; - ci2.focus = ci->home; - key_handle_focus(&ci2); + call3("Move-to", ci->home, 0, m); mark_free(m); - memset(&ci2, 0, sizeof(ci2)); - ci2.key = "render-lines:redraw"; - ci2.focus = ci->focus; - key_handle_focus(&ci2); + call3("render-lines:redraw", ci->focus, 0, NULL); return 1; }