xref: /netbsd-src/external/bsd/tmux/dist/cmd-unbind-key.c (revision deb6f0161a9109e7de9b519dc8dfb9478668dcdd)
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 = { "anT:", 0, 1 },
36 	.usage = "[-an] [-T key-table] key",
37 
38 	.flags = CMD_AFTERHOOK,
39 	.exec = cmd_unbind_key_exec
40 };
41 
42 static enum cmd_retval
43 cmd_unbind_key_exec(struct cmd *self, struct cmdq_item *item)
44 {
45 	struct args	*args = self->args;
46 	key_code	 key;
47 	const char	*tablename;
48 
49 	if (!args_has(args, 'a')) {
50 		if (args->argc != 1) {
51 			cmdq_error(item, "missing key");
52 			return (CMD_RETURN_ERROR);
53 		}
54 		key = key_string_lookup_string(args->argv[0]);
55 		if (key == KEYC_NONE || key == KEYC_UNKNOWN) {
56 			cmdq_error(item, "unknown key: %s", args->argv[0]);
57 			return (CMD_RETURN_ERROR);
58 		}
59 	} else {
60 		if (args->argc != 0) {
61 			cmdq_error(item, "key given with -a");
62 			return (CMD_RETURN_ERROR);
63 		}
64 		key = KEYC_UNKNOWN;
65 	}
66 
67 	if (key == KEYC_UNKNOWN) {
68 		tablename = args_get(args, 'T');
69 		if (tablename == NULL) {
70 			key_bindings_remove_table("root");
71 			key_bindings_remove_table("prefix");
72 			return (CMD_RETURN_NORMAL);
73 		}
74 		if (key_bindings_get_table(tablename, 0) == NULL) {
75 			cmdq_error(item, "table %s doesn't exist", tablename);
76 			return (CMD_RETURN_ERROR);
77 		}
78 		key_bindings_remove_table(tablename);
79 		return (CMD_RETURN_NORMAL);
80 	}
81 
82 	if (args_has(args, 'T')) {
83 		tablename = args_get(args, 'T');
84 		if (key_bindings_get_table(tablename, 0) == NULL) {
85 			cmdq_error(item, "table %s doesn't exist", tablename);
86 			return (CMD_RETURN_ERROR);
87 		}
88 	} else if (args_has(args, 'n'))
89 		tablename = "root";
90 	else
91 		tablename = "prefix";
92 	key_bindings_remove(tablename, key);
93 	return (CMD_RETURN_NORMAL);
94 }
95