diff options
author | nicm <nicm> | 2019-05-25 07:18:20 +0000 |
---|---|---|
committer | nicm <nicm> | 2019-05-25 07:18:20 +0000 |
commit | f8d3d247d884a9dc0b23f2799944c15630cab28f (patch) | |
tree | 4d96106a16701c94bec7475d1fb2e188e207df80 /cmd-bind-key.c | |
parent | 6b0fa14470df848ae0735a3e0bab393c241acb86 (diff) | |
download | rtmux-f8d3d247d884a9dc0b23f2799944c15630cab28f.tar.gz rtmux-f8d3d247d884a9dc0b23f2799944c15630cab28f.tar.bz2 rtmux-f8d3d247d884a9dc0b23f2799944c15630cab28f.zip |
Merge cmd_list_parse into cmd-parse.y so it can use the new alias
processing code.
Diffstat (limited to 'cmd-bind-key.c')
-rw-r--r-- | cmd-bind-key.c | 85 |
1 files changed, 17 insertions, 68 deletions
diff --git a/cmd-bind-key.c b/cmd-bind-key.c index 4650afb1..ed2f69d1 100644 --- a/cmd-bind-key.c +++ b/cmd-bind-key.c @@ -24,20 +24,17 @@ #include "tmux.h" /* - * Bind a key to a command, this recurses through cmd_*. + * Bind a key to a command. */ static enum cmd_retval cmd_bind_key_exec(struct cmd *, struct cmdq_item *); -static enum cmd_retval cmd_bind_key_mode_table(struct cmd *, - struct cmdq_item *, key_code); - const struct cmd_entry cmd_bind_key_entry = { .name = "bind-key", .alias = "bind", - .args = { "cnrt:T:", 1, -1 }, - .usage = "[-cnr] [-t mode-table] [-T key-table] key " + .args = { "cnrT:", 2, -1 }, + .usage = "[-cnr] [-T key-table] key " "command [arguments]", .flags = CMD_AFTERHOOK, @@ -47,23 +44,10 @@ const struct cmd_entry cmd_bind_key_entry = { static enum cmd_retval cmd_bind_key_exec(struct cmd *self, struct cmdq_item *item) { - struct args *args = self->args; - char *cause; - struct cmd_list *cmdlist; - key_code key; - const char *tablename; - - if (args_has(args, 't')) { - if (args->argc != 2 && args->argc != 3) { - cmdq_error(item, "not enough arguments"); - return (CMD_RETURN_ERROR); - } - } else { - if (args->argc < 2) { - cmdq_error(item, "not enough arguments"); - return (CMD_RETURN_ERROR); - } - } + struct args *args = self->args; + key_code key; + const char *tablename; + struct cmd_parse_result *pr; key = key_string_lookup_string(args->argv[0]); if (key == KEYC_NONE || key == KEYC_UNKNOWN) { @@ -71,9 +55,6 @@ cmd_bind_key_exec(struct cmd *self, struct cmdq_item *item) return (CMD_RETURN_ERROR); } - if (args_has(args, 't')) - return (cmd_bind_key_mode_table(self, item, key)); - if (args_has(args, 'T')) tablename = args_get(args, 'T'); else if (args_has(args, 'n')) @@ -81,50 +62,18 @@ cmd_bind_key_exec(struct cmd *self, struct cmdq_item *item) else tablename = "prefix"; - cmdlist = cmd_list_parse(args->argc - 1, args->argv + 1, NULL, 0, - &cause); - if (cmdlist == NULL) { - cmdq_error(item, "%s", cause); - free(cause); - return (CMD_RETURN_ERROR); - } - - key_bindings_add(tablename, key, args_has(args, 'r'), cmdlist); - return (CMD_RETURN_NORMAL); -} - -static enum cmd_retval -cmd_bind_key_mode_table(struct cmd *self, struct cmdq_item *item, key_code key) -{ - struct args *args = self->args; - const char *tablename; - const struct mode_key_table *mtab; - struct mode_key_binding *mbind, mtmp; - enum mode_key_cmd cmd; - - tablename = args_get(args, 't'); - if ((mtab = mode_key_findtable(tablename)) == NULL) { - cmdq_error(item, "unknown key table: %s", tablename); - return (CMD_RETURN_ERROR); - } - - cmd = mode_key_fromstring(mtab->cmdstr, args->argv[1]); - if (cmd == MODEKEY_NONE) { - cmdq_error(item, "unknown command: %s", args->argv[1]); + pr = cmd_parse_from_arguments(args->argc - 1, args->argv + 1, NULL); + switch (pr->status) { + case CMD_PARSE_EMPTY: + cmdq_error(item, "empty command"); return (CMD_RETURN_ERROR); - } - - if (args->argc != 2) { - cmdq_error(item, "no argument allowed"); + case CMD_PARSE_ERROR: + cmdq_error(item, "%s", pr->error); + free(pr->error); return (CMD_RETURN_ERROR); + case CMD_PARSE_SUCCESS: + break; } - - mtmp.key = key; - if ((mbind = RB_FIND(mode_key_tree, mtab->tree, &mtmp)) == NULL) { - mbind = xmalloc(sizeof *mbind); - mbind->key = mtmp.key; - RB_INSERT(mode_key_tree, mtab->tree, mbind); - } - mbind->cmd = cmd; + key_bindings_add(tablename, key, args_has(args, 'r'), pr->cmdlist); return (CMD_RETURN_NORMAL); } |