Didn't really think the else behaviour through - requiring argv to
[tmux-openbsd.git] / cmd-unbind-key.c
blob7e8c183cd167f34a99ed0bf5d23d4cafc1ef4734
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 "tmux.h"
24 * Unbind key from command.
27 int cmd_unbind_key_check(struct args *);
28 int cmd_unbind_key_exec(struct cmd *, struct cmd_ctx *);
30 int cmd_unbind_key_table(struct cmd *, struct cmd_ctx *, int);
32 const struct cmd_entry cmd_unbind_key_entry = {
33 "unbind-key", "unbind",
34 "acnt:", 0, 1,
35 "[-acn] [-t key-table] key",
37 NULL,
38 cmd_unbind_key_check,
39 cmd_unbind_key_exec
42 int
43 cmd_unbind_key_check(struct args *args)
45 if (args_has(args, 'a') && (args->argc != 0 || args_has(args, 't')))
46 return (-1);
47 if (!args_has(args, 'a') && args->argc != 1)
48 return (-1);
49 return (0);
52 int
53 cmd_unbind_key_exec(struct cmd *self, unused struct cmd_ctx *ctx)
55 struct args *args = self->args;
56 struct key_binding *bd;
57 int key;
59 if (args_has(args, 'a')) {
60 while (!SPLAY_EMPTY(&key_bindings)) {
61 bd = SPLAY_ROOT(&key_bindings);
62 key_bindings_remove(bd->key);
64 return (0);
67 key = key_string_lookup_string(args->argv[0]);
68 if (key == KEYC_NONE) {
69 ctx->error(ctx, "unknown key: %s", args->argv[0]);
70 return (-1);
73 if (args_has(args, 't'))
74 return (cmd_unbind_key_table(self, ctx, key));
76 if (!args_has(args, 'n'))
77 key |= KEYC_PREFIX;
78 key_bindings_remove(key);
79 return (0);
82 int
83 cmd_unbind_key_table(struct cmd *self, struct cmd_ctx *ctx, int key)
85 struct args *args = self->args;
86 const char *tablename;
87 const struct mode_key_table *mtab;
88 struct mode_key_binding *mbind, mtmp;
90 tablename = args_get(args, 't');
91 if ((mtab = mode_key_findtable(tablename)) == NULL) {
92 ctx->error(ctx, "unknown key table: %s", tablename);
93 return (-1);
96 mtmp.key = key;
97 mtmp.mode = !!args_has(args, 'c');
98 if ((mbind = SPLAY_FIND(mode_key_tree, mtab->tree, &mtmp)) != NULL) {
99 SPLAY_REMOVE(mode_key_tree, mtab->tree, mbind);
100 xfree(mbind);
102 return (0);