Clear window->flags when clearing winlinks
[tmux-openbsd.git] / cmd-unbind-key.c
blob88c81270bfeafb331d9d6d467a7c07ceecf5da24
1 /* $OpenBSD$ */
3 /*
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>
21 #include <stdlib.h>
23 #include "tmux.h"
26 * Unbind key from command.
29 enum cmd_retval cmd_unbind_key_check(struct args *);
30 enum cmd_retval cmd_unbind_key_exec(struct cmd *, struct cmd_q *);
31 enum cmd_retval cmd_unbind_key_table(struct cmd *, struct cmd_q *, int);
33 const struct cmd_entry cmd_unbind_key_entry = {
34 "unbind-key", "unbind",
35 "acnt:", 0, 1,
36 "[-acn] [-t key-table] key",
38 NULL,
39 cmd_unbind_key_check,
40 cmd_unbind_key_exec
43 enum cmd_retval
44 cmd_unbind_key_check(struct args *args)
46 if (args_has(args, 'a') && args->argc != 0)
47 return (CMD_RETURN_ERROR);
48 if (!args_has(args, 'a') && args->argc != 1)
49 return (CMD_RETURN_ERROR);
50 return (CMD_RETURN_NORMAL);
53 enum cmd_retval
54 cmd_unbind_key_exec(struct cmd *self, struct cmd_q *cmdq)
56 struct args *args = self->args;
57 struct key_binding *bd;
58 int key;
60 if (!args_has(args, 'a')) {
61 key = key_string_lookup_string(args->argv[0]);
62 if (key == KEYC_NONE) {
63 cmdq_error(cmdq, "unknown key: %s", args->argv[0]);
64 return (CMD_RETURN_ERROR);
66 } else
67 key = KEYC_NONE;
69 if (args_has(args, 't'))
70 return (cmd_unbind_key_table(self, cmdq, key));
72 if (key == KEYC_NONE) {
73 while (!RB_EMPTY(&key_bindings)) {
74 bd = RB_ROOT(&key_bindings);
75 key_bindings_remove(bd->key);
77 return (CMD_RETURN_NORMAL);
80 if (!args_has(args, 'n'))
81 key |= KEYC_PREFIX;
82 key_bindings_remove(key);
83 return (CMD_RETURN_NORMAL);
86 enum cmd_retval
87 cmd_unbind_key_table(struct cmd *self, struct cmd_q *cmdq, int key)
89 struct args *args = self->args;
90 const char *tablename;
91 const struct mode_key_table *mtab;
92 struct mode_key_binding *mbind, mtmp;
94 tablename = args_get(args, 't');
95 if ((mtab = mode_key_findtable(tablename)) == NULL) {
96 cmdq_error(cmdq, "unknown key table: %s", tablename);
97 return (CMD_RETURN_ERROR);
100 if (key == KEYC_NONE) {
101 while (!RB_EMPTY(mtab->tree)) {
102 mbind = RB_ROOT(mtab->tree);
103 RB_REMOVE(mode_key_tree, mtab->tree, mbind);
104 free(mbind);
106 return (CMD_RETURN_NORMAL);
109 mtmp.key = key;
110 mtmp.mode = !!args_has(args, 'c');
111 if ((mbind = RB_FIND(mode_key_tree, mtab->tree, &mtmp)) != NULL) {
112 RB_REMOVE(mode_key_tree, mtab->tree, mbind);
113 free(mbind);
115 return (CMD_RETURN_NORMAL);