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

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