4 * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
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.
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.
19 #include <sys/types.h>
26 * Unbind key from command.
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);
32 const struct cmd_entry cmd_unbind_key_entry
= {
33 "unbind-key", "unbind",
35 "[-acn] [-t key-table] key",
42 cmd_unbind_key_exec(struct cmd
*self
, struct cmd_q
*cmdq
)
44 struct args
*args
= self
->args
;
45 struct key_binding
*bd
;
48 if (!args_has(args
, 'a')) {
49 if (args
->argc
!= 1) {
50 cmdq_error(cmdq
, "missing key");
51 return (CMD_RETURN_ERROR
);
53 key
= key_string_lookup_string(args
->argv
[0]);
54 if (key
== KEYC_NONE
) {
55 cmdq_error(cmdq
, "unknown key: %s", args
->argv
[0]);
56 return (CMD_RETURN_ERROR
);
59 if (args
->argc
!= 0) {
60 cmdq_error(cmdq
, "key given with -a");
61 return (CMD_RETURN_ERROR
);
66 if (args_has(args
, 't'))
67 return (cmd_unbind_key_table(self
, cmdq
, key
));
69 if (key
== KEYC_NONE
) {
70 while (!RB_EMPTY(&key_bindings
)) {
71 bd
= RB_ROOT(&key_bindings
);
72 key_bindings_remove(bd
->key
);
74 return (CMD_RETURN_NORMAL
);
77 if (!args_has(args
, 'n'))
79 key_bindings_remove(key
);
80 return (CMD_RETURN_NORMAL
);
84 cmd_unbind_key_table(struct cmd
*self
, struct cmd_q
*cmdq
, int key
)
86 struct args
*args
= self
->args
;
87 const char *tablename
;
88 const struct mode_key_table
*mtab
;
89 struct mode_key_binding
*mbind
, mtmp
;
91 tablename
= args_get(args
, 't');
92 if ((mtab
= mode_key_findtable(tablename
)) == NULL
) {
93 cmdq_error(cmdq
, "unknown key table: %s", tablename
);
94 return (CMD_RETURN_ERROR
);
97 if (key
== KEYC_NONE
) {
98 while (!RB_EMPTY(mtab
->tree
)) {
99 mbind
= RB_ROOT(mtab
->tree
);
100 RB_REMOVE(mode_key_tree
, mtab
->tree
, mbind
);
103 return (CMD_RETURN_NORMAL
);
107 mtmp
.mode
= !!args_has(args
, 'c');
108 if ((mbind
= RB_FIND(mode_key_tree
, mtab
->tree
, &mtmp
)) != NULL
) {
109 RB_REMOVE(mode_key_tree
, mtab
->tree
, mbind
);
112 return (CMD_RETURN_NORMAL
);