xref: /openbsd-src/usr.bin/tmux/cmd-unbind-key.c (revision e5157e49389faebcb42b7237d55fbf096d9c2523)
1 /* $OpenBSD: cmd-unbind-key.c,v 1.20 2014/10/20 22:29:25 nicm Exp $ */
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 
21 #include <stdlib.h>
22 
23 #include "tmux.h"
24 
25 /*
26  * Unbind key from command.
27  */
28 
29 enum cmd_retval	 cmd_unbind_key_exec(struct cmd *, struct cmd_q *);
30 enum cmd_retval	 cmd_unbind_key_mode_table(struct cmd *, struct cmd_q *, int);
31 
32 const struct cmd_entry cmd_unbind_key_entry = {
33 	"unbind-key", "unbind",
34 	"acnt:", 0, 1,
35 	"[-acn] [-t mode-table] key",
36 	0,
37 	cmd_unbind_key_exec
38 };
39 
40 enum cmd_retval
41 cmd_unbind_key_exec(struct cmd *self, struct cmd_q *cmdq)
42 {
43 	struct args		*args = self->args;
44 	struct key_binding	*bd;
45 	int			 key;
46 
47 	if (!args_has(args, 'a')) {
48 		if (args->argc != 1) {
49 			cmdq_error(cmdq, "missing key");
50 			return (CMD_RETURN_ERROR);
51 		}
52 		key = key_string_lookup_string(args->argv[0]);
53 		if (key == KEYC_NONE) {
54 			cmdq_error(cmdq, "unknown key: %s", args->argv[0]);
55 			return (CMD_RETURN_ERROR);
56 		}
57 	} else {
58 		if (args->argc != 0) {
59 			cmdq_error(cmdq, "key given with -a");
60 			return (CMD_RETURN_ERROR);
61 		}
62 		key = KEYC_NONE;
63 	}
64 
65 	if (args_has(args, 't'))
66 		return (cmd_unbind_key_mode_table(self, cmdq, key));
67 
68 	if (key == KEYC_NONE) {
69 		while (!RB_EMPTY(&key_bindings)) {
70 			bd = RB_ROOT(&key_bindings);
71 			key_bindings_remove(bd->key);
72 		}
73 		return (CMD_RETURN_NORMAL);
74 	}
75 
76 	if (!args_has(args, 'n'))
77 		key |= KEYC_PREFIX;
78 	key_bindings_remove(key);
79 	return (CMD_RETURN_NORMAL);
80 }
81 
82 enum cmd_retval
83 cmd_unbind_key_mode_table(struct cmd *self, struct cmd_q *cmdq, int key)
84 {
85 	struct args			*args = self->args;
86 	const char			*tablename;
87 	const struct mode_key_table	*mtab;
88 	struct mode_key_binding		*mbind, mtmp;
89 
90 	tablename = args_get(args, 't');
91 	if ((mtab = mode_key_findtable(tablename)) == NULL) {
92 		cmdq_error(cmdq, "unknown key table: %s", tablename);
93 		return (CMD_RETURN_ERROR);
94 	}
95 
96 	if (key == KEYC_NONE) {
97 		while (!RB_EMPTY(mtab->tree)) {
98 			mbind = RB_ROOT(mtab->tree);
99 			RB_REMOVE(mode_key_tree, mtab->tree, mbind);
100 			free(mbind);
101 		}
102 		return (CMD_RETURN_NORMAL);
103 	}
104 
105 	mtmp.key = key;
106 	mtmp.mode = !!args_has(args, 'c');
107 	if ((mbind = RB_FIND(mode_key_tree, mtab->tree, &mtmp)) != NULL) {
108 		RB_REMOVE(mode_key_tree, mtab->tree, mbind);
109 		free(mbind);
110 	}
111 	return (CMD_RETURN_NORMAL);
112 }
113