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

1.21    ! nicm        1: /* $OpenBSD: cmd-unbind-key.c,v 1.20 2014/10/20 22:29:25 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.16      nicm       29: enum cmd_retval         cmd_unbind_key_exec(struct cmd *, struct cmd_q *);
1.19      nicm       30: enum cmd_retval         cmd_unbind_key_mode_table(struct cmd *, struct cmd_q *, int);
1.1       nicm       31:
                     32: const struct cmd_entry cmd_unbind_key_entry = {
                     33:        "unbind-key", "unbind",
1.21    ! nicm       34:        "acnt:T:", 0, 1,
        !            35:        "[-acn] [-t mode-table] [-T key-table] key",
1.9       nicm       36:        0,
                     37:        cmd_unbind_key_exec
1.1       nicm       38: };
                     39:
1.15      nicm       40: enum cmd_retval
1.16      nicm       41: cmd_unbind_key_exec(struct cmd *self, struct cmd_q *cmdq)
1.1       nicm       42: {
1.21    ! nicm       43:        struct args     *args = self->args;
        !            44:        int              key;
        !            45:        const char      *tablename;
1.1       nicm       46:
1.13      nicm       47:        if (!args_has(args, 'a')) {
1.17      nicm       48:                if (args->argc != 1) {
                     49:                        cmdq_error(cmdq, "missing key");
                     50:                        return (CMD_RETURN_ERROR);
                     51:                }
1.13      nicm       52:                key = key_string_lookup_string(args->argv[0]);
                     53:                if (key == KEYC_NONE) {
1.16      nicm       54:                        cmdq_error(cmdq, "unknown key: %s", args->argv[0]);
1.15      nicm       55:                        return (CMD_RETURN_ERROR);
1.13      nicm       56:                }
1.17      nicm       57:        } else {
                     58:                if (args->argc != 0) {
                     59:                        cmdq_error(cmdq, "key given with -a");
                     60:                        return (CMD_RETURN_ERROR);
                     61:                }
1.13      nicm       62:                key = KEYC_NONE;
1.17      nicm       63:        }
1.13      nicm       64:
                     65:        if (args_has(args, 't'))
1.19      nicm       66:                return (cmd_unbind_key_mode_table(self, cmdq, key));
1.13      nicm       67:
                     68:        if (key == KEYC_NONE) {
1.21    ! nicm       69:                tablename = args_get(args, 'T');
        !            70:                if (tablename == NULL) {
        !            71:                        key_bindings_remove_table("root");
        !            72:                        key_bindings_remove_table("prefix");
        !            73:                        return (CMD_RETURN_NORMAL);
1.8       nicm       74:                }
1.21    ! nicm       75:                if (key_bindings_get_table(tablename, 0) == NULL) {
        !            76:                        cmdq_error(cmdq, "table %s doesn't exist", tablename);
        !            77:                        return (CMD_RETURN_ERROR);
        !            78:                }
        !            79:                key_bindings_remove_table(tablename);
1.15      nicm       80:                return (CMD_RETURN_NORMAL);
1.9       nicm       81:        }
1.1       nicm       82:
1.21    ! nicm       83:        if (args_has(args, 'T')) {
        !            84:                tablename = args_get(args, 'T');
        !            85:                if (key_bindings_get_table(tablename, 0) == NULL) {
        !            86:                        cmdq_error(cmdq, "table %s doesn't exist", tablename);
        !            87:                        return (CMD_RETURN_ERROR);
        !            88:                }
        !            89:        } else if (args_has(args, 'n'))
        !            90:                tablename = "root";
        !            91:        else
        !            92:                tablename = "prefix";
        !            93:        key_bindings_remove(tablename, key);
1.15      nicm       94:        return (CMD_RETURN_NORMAL);
1.1       nicm       95: }
                     96:
1.15      nicm       97: enum cmd_retval
1.19      nicm       98: cmd_unbind_key_mode_table(struct cmd *self, struct cmd_q *cmdq, int key)
1.5       nicm       99: {
1.9       nicm      100:        struct args                     *args = self->args;
                    101:        const char                      *tablename;
1.5       nicm      102:        const struct mode_key_table     *mtab;
                    103:        struct mode_key_binding         *mbind, mtmp;
                    104:
1.9       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.15      nicm      108:                return (CMD_RETURN_ERROR);
1.13      nicm      109:        }
                    110:
                    111:        if (key == KEYC_NONE) {
                    112:                while (!RB_EMPTY(mtab->tree)) {
                    113:                        mbind = RB_ROOT(mtab->tree);
                    114:                        RB_REMOVE(mode_key_tree, mtab->tree, mbind);
1.14      nicm      115:                        free(mbind);
1.13      nicm      116:                }
1.15      nicm      117:                return (CMD_RETURN_NORMAL);
1.5       nicm      118:        }
                    119:
1.9       nicm      120:        mtmp.key = key;
                    121:        mtmp.mode = !!args_has(args, 'c');
1.12      nicm      122:        if ((mbind = RB_FIND(mode_key_tree, mtab->tree, &mtmp)) != NULL) {
                    123:                RB_REMOVE(mode_key_tree, mtab->tree, mbind);
1.14      nicm      124:                free(mbind);
1.5       nicm      125:        }
1.15      nicm      126:        return (CMD_RETURN_NORMAL);
1.1       nicm      127: }