1 /* $OpenBSD$ */
2
3 /*
4 * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
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 <stdlib.h>
22
23 #include "tmux.h"
24
25 /*
26 * Unbind key from command.
27 */
28
29 static enum cmd_retval cmd_unbind_key_exec(struct cmd *, struct cmdq_item *);
30
31 const struct cmd_entry cmd_unbind_key_entry = {
32 .name = "unbind-key",
33 .alias = "unbind",
34
35 .args = { "anqT:", 0, 1, NULL },
36 .usage = "[-anq] [-T key-table] key",
37
38 .flags = CMD_AFTERHOOK,
39 .exec = cmd_unbind_key_exec
40 };
41
42 static enum cmd_retval
cmd_unbind_key_exec(struct cmd * self,struct cmdq_item * item)43 cmd_unbind_key_exec(struct cmd *self, struct cmdq_item *item)
44 {
45 struct args *args = cmd_get_args(self);
46 key_code key;
47 const char *tablename, *keystr = args_string(args, 0);
48 int quiet = args_has(args, 'q');
49
50 if (args_has(args, 'a')) {
51 if (keystr != NULL) {
52 if (!quiet)
53 cmdq_error(item, "key given with -a");
54 return (CMD_RETURN_ERROR);
55 }
56
57 tablename = args_get(args, 'T');
58 if (tablename == NULL) {
59 if (args_has(args, 'n'))
60 tablename = "root";
61 else
62 tablename = "prefix";
63 }
64 if (key_bindings_get_table(tablename, 0) == NULL) {
65 if (!quiet) {
66 cmdq_error(item, "table %s doesn't exist" ,
67 tablename);
68 }
69 return (CMD_RETURN_ERROR);
70 }
71
72 key_bindings_remove_table(tablename);
73 return (CMD_RETURN_NORMAL);
74 }
75
76 if (keystr == NULL) {
77 if (!quiet)
78 cmdq_error(item, "missing key");
79 return (CMD_RETURN_ERROR);
80 }
81
82 key = key_string_lookup_string(keystr);
83 if (key == KEYC_NONE || key == KEYC_UNKNOWN) {
84 if (!quiet)
85 cmdq_error(item, "unknown key: %s", keystr);
86 return (CMD_RETURN_ERROR);
87 }
88
89 if (args_has(args, 'T')) {
90 tablename = args_get(args, 'T');
91 if (key_bindings_get_table(tablename, 0) == NULL) {
92 if (!quiet) {
93 cmdq_error(item, "table %s doesn't exist" ,
94 tablename);
95 }
96 return (CMD_RETURN_ERROR);
97 }
98 } else if (args_has(args, 'n'))
99 tablename = "root";
100 else
101 tablename = "prefix";
102 key_bindings_remove(tablename, key);
103 return (CMD_RETURN_NORMAL);
104 }
105