[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.12

1.12    ! nicm        1: /* $OpenBSD: cmd-bind-key.c,v 1.11 2011/01/15 20:14:41 nicm Exp $ */
1.1       nicm        2:
                      3: /*
                      4:  * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
                      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.5       nicm       21: #include <string.h>
                     22:
1.1       nicm       23: #include "tmux.h"
                     24:
                     25: /*
                     26:  * Bind a key to a command, this recurses through cmd_*.
                     27:  */
                     28:
1.10      nicm       29: int    cmd_bind_key_check(struct args *);
1.1       nicm       30: int    cmd_bind_key_exec(struct cmd *, struct cmd_ctx *);
                     31:
1.10      nicm       32: int    cmd_bind_key_table(struct cmd *, struct cmd_ctx *, int);
1.1       nicm       33:
                     34: const struct cmd_entry cmd_bind_key_entry = {
                     35:        "bind-key", "bind",
1.10      nicm       36:        "cnrt:", 1, -1,
1.5       nicm       37:        "[-cnr] [-t key-table] key command [arguments]",
1.10      nicm       38:        0,
1.1       nicm       39:        NULL,
1.10      nicm       40:        cmd_bind_key_check,
                     41:        cmd_bind_key_exec
1.1       nicm       42: };
                     43:
                     44: int
1.10      nicm       45: cmd_bind_key_check(struct args *args)
1.1       nicm       46: {
1.10      nicm       47:        if (args_has(args, 't')) {
1.11      nicm       48:                if (args->argc != 2)
1.10      nicm       49:                        return (-1);
1.5       nicm       50:        } else {
1.10      nicm       51:                if (args->argc < 2)
                     52:                        return (-1);
1.5       nicm       53:        }
1.1       nicm       54:        return (0);
                     55: }
                     56:
                     57: int
1.10      nicm       58: cmd_bind_key_exec(struct cmd *self, struct cmd_ctx *ctx)
1.1       nicm       59: {
1.10      nicm       60:        struct args     *args = self->args;
                     61:        char            *cause;
                     62:        struct cmd_list *cmdlist;
                     63:        int              key;
                     64:
                     65:        key = key_string_lookup_string(args->argv[0]);
                     66:        if (key == KEYC_NONE) {
                     67:                ctx->error(ctx, "unknown key: %s", args->argv[0]);
                     68:                return (-1);
                     69:        }
1.1       nicm       70:
1.10      nicm       71:        if (args_has(args, 't'))
                     72:                return (cmd_bind_key_table(self, ctx, key));
1.1       nicm       73:
1.10      nicm       74:        cmdlist = cmd_list_parse(args->argc - 1, args->argv + 1, &cause);
                     75:        if (cmdlist == NULL) {
                     76:                ctx->error(ctx, "%s", cause);
                     77:                xfree(cause);
                     78:                return (-1);
                     79:        }
1.1       nicm       80:
1.10      nicm       81:        if (!args_has(args, 'n'))
                     82:            key |= KEYC_PREFIX;
                     83:        key_bindings_add(key, args_has(args, 'r'), cmdlist);
1.1       nicm       84:        return (0);
                     85: }
                     86:
1.5       nicm       87: int
1.10      nicm       88: cmd_bind_key_table(struct cmd *self, struct cmd_ctx *ctx, int key)
1.5       nicm       89: {
1.10      nicm       90:        struct args                     *args = self->args;
                     91:        const char                      *tablename;
1.5       nicm       92:        const struct mode_key_table     *mtab;
                     93:        struct mode_key_binding         *mbind, mtmp;
                     94:        enum mode_key_cmd                cmd;
                     95:
1.10      nicm       96:        tablename = args_get(args, 't');
                     97:        if ((mtab = mode_key_findtable(tablename)) == NULL) {
                     98:                ctx->error(ctx, "unknown key table: %s", tablename);
1.5       nicm       99:                return (-1);
                    100:        }
                    101:
1.11      nicm      102:        cmd = mode_key_fromstring(mtab->cmdstr, args->argv[1]);
1.5       nicm      103:        if (cmd == MODEKEY_NONE) {
1.11      nicm      104:                ctx->error(ctx, "unknown command: %s", args->argv[1]);
1.5       nicm      105:                return (-1);
                    106:        }
1.7       nicm      107:
1.10      nicm      108:        mtmp.key = key;
                    109:        mtmp.mode = !!args_has(args, 'c');
1.12    ! nicm      110:        if ((mbind = RB_FIND(mode_key_tree, mtab->tree, &mtmp)) != NULL) {
1.5       nicm      111:                mbind->cmd = cmd;
                    112:                return (0);
                    113:        }
                    114:        mbind = xmalloc(sizeof *mbind);
                    115:        mbind->key = mtmp.key;
                    116:        mbind->mode = mtmp.mode;
                    117:        mbind->cmd = cmd;
1.12    ! nicm      118:        RB_INSERT(mode_key_tree, mtab->tree, mbind);
1.5       nicm      119:        return (0);
1.1       nicm      120: }