xref: /openbsd-src/usr.bin/tmux/cmd-unbind-key.c (revision 68e0a7f271bc1c08e58a23a65844d346c6665089)
1 /* $OpenBSD: cmd-unbind-key.c,v 1.29 2016/10/16 19:04:05 nicm Exp $ */
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 static enum cmd_retval	cmd_unbind_key_mode_table(struct cmd *,
32 			    struct cmdq_item *, key_code);
33 
34 const struct cmd_entry cmd_unbind_key_entry = {
35 	.name = "unbind-key",
36 	.alias = "unbind",
37 
38 	.args = { "ant:T:", 0, 1 },
39 	.usage = "[-an] [-t mode-table] [-T key-table] key",
40 
41 	.flags = CMD_AFTERHOOK,
42 	.exec = cmd_unbind_key_exec
43 };
44 
45 static enum cmd_retval
46 cmd_unbind_key_exec(struct cmd *self, struct cmdq_item *item)
47 {
48 	struct args	*args = self->args;
49 	key_code	 key;
50 	const char	*tablename;
51 
52 	if (!args_has(args, 'a')) {
53 		if (args->argc != 1) {
54 			cmdq_error(item, "missing key");
55 			return (CMD_RETURN_ERROR);
56 		}
57 		key = key_string_lookup_string(args->argv[0]);
58 		if (key == KEYC_NONE || key == KEYC_UNKNOWN) {
59 			cmdq_error(item, "unknown key: %s", args->argv[0]);
60 			return (CMD_RETURN_ERROR);
61 		}
62 	} else {
63 		if (args->argc != 0) {
64 			cmdq_error(item, "key given with -a");
65 			return (CMD_RETURN_ERROR);
66 		}
67 		key = KEYC_UNKNOWN;
68 	}
69 
70 	if (args_has(args, 't'))
71 		return (cmd_unbind_key_mode_table(self, item, key));
72 
73 	if (key == KEYC_UNKNOWN) {
74 		tablename = args_get(args, 'T');
75 		if (tablename == NULL) {
76 			key_bindings_remove_table("root");
77 			key_bindings_remove_table("prefix");
78 			return (CMD_RETURN_NORMAL);
79 		}
80 		if (key_bindings_get_table(tablename, 0) == NULL) {
81 			cmdq_error(item, "table %s doesn't exist", tablename);
82 			return (CMD_RETURN_ERROR);
83 		}
84 		key_bindings_remove_table(tablename);
85 		return (CMD_RETURN_NORMAL);
86 	}
87 
88 	if (args_has(args, 'T')) {
89 		tablename = args_get(args, 'T');
90 		if (key_bindings_get_table(tablename, 0) == NULL) {
91 			cmdq_error(item, "table %s doesn't exist", tablename);
92 			return (CMD_RETURN_ERROR);
93 		}
94 	} else if (args_has(args, 'n'))
95 		tablename = "root";
96 	else
97 		tablename = "prefix";
98 	key_bindings_remove(tablename, key);
99 	return (CMD_RETURN_NORMAL);
100 }
101 
102 static enum cmd_retval
103 cmd_unbind_key_mode_table(struct cmd *self, struct cmdq_item *item,
104     key_code key)
105 {
106 	struct args			*args = self->args;
107 	const char			*tablename;
108 	const struct mode_key_table	*mtab;
109 	struct mode_key_binding		*mbind, mtmp;
110 
111 	tablename = args_get(args, 't');
112 	if ((mtab = mode_key_findtable(tablename)) == NULL) {
113 		cmdq_error(item, "unknown key table: %s", tablename);
114 		return (CMD_RETURN_ERROR);
115 	}
116 
117 	if (key == KEYC_UNKNOWN) {
118 		while (!RB_EMPTY(mtab->tree)) {
119 			mbind = RB_ROOT(mtab->tree);
120 			RB_REMOVE(mode_key_tree, mtab->tree, mbind);
121 			free(mbind);
122 		}
123 		return (CMD_RETURN_NORMAL);
124 	}
125 
126 	mtmp.key = key;
127 	if ((mbind = RB_FIND(mode_key_tree, mtab->tree, &mtmp)) != NULL) {
128 		RB_REMOVE(mode_key_tree, mtab->tree, mbind);
129 		free(mbind);
130 	}
131 	return (CMD_RETURN_NORMAL);
132 }
133