Annotation of src/usr.bin/tmux/cmd-unbind-key.c, Revision 1.17
1.17 ! nicm 1: /* $OpenBSD: cmd-unbind-key.c,v 1.16 2013/03/24 09:54:10 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 *);
30: enum cmd_retval cmd_unbind_key_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.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_exec
1.1 nicm 39: };
40:
1.15 nicm 41: enum cmd_retval
1.16 nicm 42: cmd_unbind_key_exec(struct cmd *self, struct cmd_q *cmdq)
1.1 nicm 43: {
1.9 nicm 44: struct args *args = self->args;
45: struct key_binding *bd;
46: int key;
1.1 nicm 47:
1.13 nicm 48: if (!args_has(args, 'a')) {
1.17 ! nicm 49: if (args->argc != 1) {
! 50: cmdq_error(cmdq, "missing key");
! 51: return (CMD_RETURN_ERROR);
! 52: }
! 53: return (CMD_RETURN_ERROR);
1.13 nicm 54: key = key_string_lookup_string(args->argv[0]);
55: if (key == KEYC_NONE) {
1.16 nicm 56: cmdq_error(cmdq, "unknown key: %s", args->argv[0]);
1.15 nicm 57: return (CMD_RETURN_ERROR);
1.13 nicm 58: }
1.17 ! nicm 59: } else {
! 60: if (args->argc != 0) {
! 61: cmdq_error(cmdq, "key given with -a");
! 62: return (CMD_RETURN_ERROR);
! 63: }
1.13 nicm 64: key = KEYC_NONE;
1.17 ! nicm 65: }
1.13 nicm 66:
67: if (args_has(args, 't'))
1.16 nicm 68: return (cmd_unbind_key_table(self, cmdq, key));
1.13 nicm 69:
70: if (key == KEYC_NONE) {
1.12 nicm 71: while (!RB_EMPTY(&key_bindings)) {
72: bd = RB_ROOT(&key_bindings);
1.11 nicm 73: key_bindings_remove(bd->key);
1.8 nicm 74: }
1.15 nicm 75: return (CMD_RETURN_NORMAL);
1.9 nicm 76: }
1.1 nicm 77:
1.9 nicm 78: if (!args_has(args, 'n'))
79: key |= KEYC_PREFIX;
80: key_bindings_remove(key);
1.15 nicm 81: return (CMD_RETURN_NORMAL);
1.1 nicm 82: }
83:
1.15 nicm 84: enum cmd_retval
1.16 nicm 85: cmd_unbind_key_table(struct cmd *self, struct cmd_q *cmdq, int key)
1.5 nicm 86: {
1.9 nicm 87: struct args *args = self->args;
88: const char *tablename;
1.5 nicm 89: const struct mode_key_table *mtab;
90: struct mode_key_binding *mbind, mtmp;
91:
1.9 nicm 92: tablename = args_get(args, 't');
93: if ((mtab = mode_key_findtable(tablename)) == NULL) {
1.16 nicm 94: cmdq_error(cmdq, "unknown key table: %s", tablename);
1.15 nicm 95: return (CMD_RETURN_ERROR);
1.13 nicm 96: }
97:
98: if (key == KEYC_NONE) {
99: while (!RB_EMPTY(mtab->tree)) {
100: mbind = RB_ROOT(mtab->tree);
101: RB_REMOVE(mode_key_tree, mtab->tree, mbind);
1.14 nicm 102: free(mbind);
1.13 nicm 103: }
1.15 nicm 104: return (CMD_RETURN_NORMAL);
1.5 nicm 105: }
106:
1.9 nicm 107: mtmp.key = key;
108: mtmp.mode = !!args_has(args, 'c');
1.12 nicm 109: if ((mbind = RB_FIND(mode_key_tree, mtab->tree, &mtmp)) != NULL) {
110: RB_REMOVE(mode_key_tree, mtab->tree, mbind);
1.14 nicm 111: free(mbind);
1.5 nicm 112: }
1.15 nicm 113: return (CMD_RETURN_NORMAL);
1.1 nicm 114: }