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

Annotation of src/usr.bin/tmux/cmd-unbind-key.c, Revision 1.13

1.13    ! nicm        1: /* $OpenBSD: cmd-unbind-key.c,v 1.12 2012/01/21 11:12:13 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:
                     21: #include "tmux.h"
                     22:
                     23: /*
                     24:  * Unbind key from command.
                     25:  */
                     26:
1.9       nicm       27: int    cmd_unbind_key_check(struct args *);
1.1       nicm       28: int    cmd_unbind_key_exec(struct cmd *, struct cmd_ctx *);
                     29:
1.9       nicm       30: int    cmd_unbind_key_table(struct cmd *, struct cmd_ctx *, int);
1.1       nicm       31:
                     32: const struct cmd_entry cmd_unbind_key_entry = {
                     33:        "unbind-key", "unbind",
1.10      nicm       34:        "acnt:", 0, 1,
1.8       nicm       35:        "[-acn] [-t key-table] key",
1.9       nicm       36:        0,
1.1       nicm       37:        NULL,
1.9       nicm       38:        cmd_unbind_key_check,
                     39:        cmd_unbind_key_exec
1.1       nicm       40: };
                     41:
                     42: int
1.9       nicm       43: cmd_unbind_key_check(struct args *args)
1.1       nicm       44: {
1.13    ! nicm       45:        if (args_has(args, 'a') && args->argc != 0)
        !            46:                return (-1);
1.10      nicm       47:        if (!args_has(args, 'a') && args->argc != 1)
1.9       nicm       48:                return (-1);
1.1       nicm       49:        return (0);
                     50: }
                     51:
                     52: int
                     53: cmd_unbind_key_exec(struct cmd *self, unused struct cmd_ctx *ctx)
                     54: {
1.9       nicm       55:        struct args             *args = self->args;
                     56:        struct key_binding      *bd;
                     57:        int                      key;
1.1       nicm       58:
1.13    ! nicm       59:        if (!args_has(args, 'a')) {
        !            60:                key = key_string_lookup_string(args->argv[0]);
        !            61:                if (key == KEYC_NONE) {
        !            62:                        ctx->error(ctx, "unknown key: %s", args->argv[0]);
        !            63:                        return (-1);
        !            64:                }
        !            65:        } else
        !            66:                key = KEYC_NONE;
        !            67:
        !            68:        if (args_has(args, 't'))
        !            69:                return (cmd_unbind_key_table(self, ctx, key));
        !            70:
        !            71:        if (key == KEYC_NONE) {
1.12      nicm       72:                while (!RB_EMPTY(&key_bindings)) {
                     73:                        bd = RB_ROOT(&key_bindings);
1.11      nicm       74:                        key_bindings_remove(bd->key);
1.8       nicm       75:                }
1.9       nicm       76:                return (0);
                     77:        }
1.1       nicm       78:
1.9       nicm       79:        if (!args_has(args, 'n'))
                     80:                key |= KEYC_PREFIX;
                     81:        key_bindings_remove(key);
1.1       nicm       82:        return (0);
                     83: }
                     84:
1.5       nicm       85: int
1.9       nicm       86: cmd_unbind_key_table(struct cmd *self, struct cmd_ctx *ctx, int key)
1.5       nicm       87: {
1.9       nicm       88:        struct args                     *args = self->args;
                     89:        const char                      *tablename;
1.5       nicm       90:        const struct mode_key_table     *mtab;
                     91:        struct mode_key_binding         *mbind, mtmp;
                     92:
1.9       nicm       93:        tablename = args_get(args, 't');
                     94:        if ((mtab = mode_key_findtable(tablename)) == NULL) {
                     95:                ctx->error(ctx, "unknown key table: %s", tablename);
1.5       nicm       96:                return (-1);
1.13    ! nicm       97:        }
        !            98:
        !            99:        if (key == KEYC_NONE) {
        !           100:                while (!RB_EMPTY(mtab->tree)) {
        !           101:                        mbind = RB_ROOT(mtab->tree);
        !           102:                        RB_REMOVE(mode_key_tree, mtab->tree, mbind);
        !           103:                        xfree(mbind);
        !           104:                }
        !           105:                return (0);
1.5       nicm      106:        }
                    107:
1.9       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) {
                    111:                RB_REMOVE(mode_key_tree, mtab->tree, mbind);
1.5       nicm      112:                xfree(mbind);
                    113:        }
                    114:        return (0);
1.1       nicm      115: }