[BACK]Return to cmd-bind-key.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / tmux

Annotation of src/usr.bin/tmux/cmd-bind-key.c, Revision 1.30

1.30    ! nicm        1: /* $OpenBSD: cmd-bind-key.c,v 1.29 2016/10/12 14:50:14 nicm Exp $ */
1.1       nicm        2:
                      3: /*
1.25      nicm        4:  * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
1.1       nicm        5:  *
                      6:  * Permission to use, copy, modify, and distribute this software for any
                      7:  * purpose with or without fee is hereby granted, provided that the above
                      8:  * copyright notice and this permission notice appear in all copies.
                      9:  *
                     10:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     11:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     12:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     13:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     14:  * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
                     15:  * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
                     16:  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
                     17:  */
                     18:
                     19: #include <sys/types.h>
                     20:
1.13      nicm       21: #include <stdlib.h>
1.5       nicm       22: #include <string.h>
                     23:
1.1       nicm       24: #include "tmux.h"
                     25:
                     26: /*
                     27:  * Bind a key to a command, this recurses through cmd_*.
                     28:  */
                     29:
1.27      nicm       30: static enum cmd_retval  cmd_bind_key_exec(struct cmd *, struct cmd_q *);
1.1       nicm       31:
1.27      nicm       32: static enum cmd_retval  cmd_bind_key_mode_table(struct cmd *, struct cmd_q *,
                     33:                             key_code);
1.1       nicm       34:
                     35: const struct cmd_entry cmd_bind_key_entry = {
1.24      nicm       36:        .name = "bind-key",
                     37:        .alias = "bind",
                     38:
1.28      nicm       39:        .args = { "cnrt:T:", 1, -1 },
                     40:        .usage = "[-cnr] [-t mode-table] [-T key-table] key "
1.26      nicm       41:                 "command [arguments]",
1.24      nicm       42:
1.30    ! nicm       43:        .flags = CMD_AFTERHOOK,
1.24      nicm       44:        .exec = cmd_bind_key_exec
1.1       nicm       45: };
                     46:
1.27      nicm       47: static enum cmd_retval
1.17      nicm       48: cmd_bind_key_exec(struct cmd *self, struct cmd_q *cmdq)
1.1       nicm       49: {
1.17      nicm       50:        struct args     *args = self->args;
                     51:        char            *cause;
                     52:        struct cmd_list *cmdlist;
1.22      nicm       53:        key_code         key;
1.21      nicm       54:        const char      *tablename;
1.17      nicm       55:
1.10      nicm       56:        if (args_has(args, 't')) {
1.17      nicm       57:                if (args->argc != 2 && args->argc != 3) {
                     58:                        cmdq_error(cmdq, "not enough arguments");
1.14      nicm       59:                        return (CMD_RETURN_ERROR);
1.17      nicm       60:                }
1.5       nicm       61:        } else {
1.17      nicm       62:                if (args->argc < 2) {
                     63:                        cmdq_error(cmdq, "not enough arguments");
1.14      nicm       64:                        return (CMD_RETURN_ERROR);
1.17      nicm       65:                }
1.5       nicm       66:        }
1.10      nicm       67:
                     68:        key = key_string_lookup_string(args->argv[0]);
1.23      nicm       69:        if (key == KEYC_NONE || key == KEYC_UNKNOWN) {
1.16      nicm       70:                cmdq_error(cmdq, "unknown key: %s", args->argv[0]);
1.14      nicm       71:                return (CMD_RETURN_ERROR);
1.10      nicm       72:        }
1.1       nicm       73:
1.10      nicm       74:        if (args_has(args, 't'))
1.18      nicm       75:                return (cmd_bind_key_mode_table(self, cmdq, key));
1.1       nicm       76:
1.21      nicm       77:        if (args_has(args, 'T'))
                     78:                tablename = args_get(args, 'T');
                     79:        else if (args_has(args, 'n'))
                     80:                tablename = "root";
                     81:        else
                     82:                tablename = "prefix";
                     83:
1.16      nicm       84:        cmdlist = cmd_list_parse(args->argc - 1, args->argv + 1, NULL, 0,
                     85:            &cause);
1.10      nicm       86:        if (cmdlist == NULL) {
1.16      nicm       87:                cmdq_error(cmdq, "%s", cause);
1.13      nicm       88:                free(cause);
1.14      nicm       89:                return (CMD_RETURN_ERROR);
1.10      nicm       90:        }
1.1       nicm       91:
1.21      nicm       92:        key_bindings_add(tablename, key, args_has(args, 'r'), cmdlist);
1.14      nicm       93:        return (CMD_RETURN_NORMAL);
1.1       nicm       94: }
                     95:
1.27      nicm       96: static enum cmd_retval
1.22      nicm       97: cmd_bind_key_mode_table(struct cmd *self, struct cmd_q *cmdq, key_code key)
1.5       nicm       98: {
1.10      nicm       99:        struct args                     *args = self->args;
1.28      nicm      100:        const char                      *tablename;
1.5       nicm      101:        const struct mode_key_table     *mtab;
                    102:        struct mode_key_binding         *mbind, mtmp;
                    103:        enum mode_key_cmd                cmd;
                    104:
1.10      nicm      105:        tablename = args_get(args, 't');
                    106:        if ((mtab = mode_key_findtable(tablename)) == NULL) {
1.16      nicm      107:                cmdq_error(cmdq, "unknown key table: %s", tablename);
1.14      nicm      108:                return (CMD_RETURN_ERROR);
1.5       nicm      109:        }
                    110:
1.11      nicm      111:        cmd = mode_key_fromstring(mtab->cmdstr, args->argv[1]);
1.5       nicm      112:        if (cmd == MODEKEY_NONE) {
1.16      nicm      113:                cmdq_error(cmdq, "unknown command: %s", args->argv[1]);
1.14      nicm      114:                return (CMD_RETURN_ERROR);
1.5       nicm      115:        }
1.7       nicm      116:
1.28      nicm      117:        if (args->argc != 2) {
                    118:                cmdq_error(cmdq, "no argument allowed");
                    119:                return (CMD_RETURN_ERROR);
1.26      nicm      120:        }
                    121:
1.10      nicm      122:        mtmp.key = key;
1.15      nicm      123:        if ((mbind = RB_FIND(mode_key_tree, mtab->tree, &mtmp)) == NULL) {
                    124:                mbind = xmalloc(sizeof *mbind);
                    125:                mbind->key = mtmp.key;
                    126:                RB_INSERT(mode_key_tree, mtab->tree, mbind);
                    127:        }
1.5       nicm      128:        mbind->cmd = cmd;
1.14      nicm      129:        return (CMD_RETURN_NORMAL);
1.1       nicm      130: }