Add a missing command and some missing Ic, from Tiago Cunha.
[tmux-openbsd.git] / cmd-unbind-key.c
blobc5a97301807a0726434006914d9ce7467f969fab
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_parse(struct cmd *, int, char **, char **);
28 int cmd_unbind_key_exec(struct cmd *, struct cmd_ctx *);
29 void cmd_unbind_key_free(struct cmd *);
31 int cmd_unbind_key_table(struct cmd *, struct cmd_ctx *);
33 struct cmd_unbind_key_data {
34 int key;
36 int command_key;
37 char *tablename;
40 const struct cmd_entry cmd_unbind_key_entry = {
41 "unbind-key", "unbind",
42 "[-cn] [-t key-table] key",
43 0, "",
44 NULL,
45 cmd_unbind_key_parse,
46 cmd_unbind_key_exec,
47 cmd_unbind_key_free,
48 NULL
51 int
52 cmd_unbind_key_parse(struct cmd *self, int argc, char **argv, char **cause)
54 struct cmd_unbind_key_data *data;
55 int opt, no_prefix = 0;
57 self->data = data = xmalloc(sizeof *data);
58 data->command_key = 0;
59 data->tablename = NULL;
61 while ((opt = getopt(argc, argv, "cnt:")) != -1) {
62 switch (opt) {
63 case 'c':
64 data->command_key = 1;
65 break;
66 case 'n':
67 no_prefix = 1;
68 break;
69 case 't':
70 if (data->tablename == NULL)
71 data->tablename = xstrdup(optarg);
72 break;
73 default:
74 goto usage;
77 argc -= optind;
78 argv += optind;
79 if (argc != 1)
80 goto usage;
82 if ((data->key = key_string_lookup_string(argv[0])) == KEYC_NONE) {
83 xasprintf(cause, "unknown key: %s", argv[0]);
84 goto error;
86 if (!no_prefix)
87 data->key |= KEYC_PREFIX;
89 return (0);
91 usage:
92 xasprintf(cause, "usage: %s %s", self->entry->name, self->entry->usage);
94 error:
95 xfree(data);
96 return (-1);
99 int
100 cmd_unbind_key_exec(struct cmd *self, unused struct cmd_ctx *ctx)
102 struct cmd_unbind_key_data *data = self->data;
104 if (data == NULL)
105 return (0);
106 if (data->tablename != NULL)
107 return (cmd_unbind_key_table(self, ctx));
109 key_bindings_remove(data->key);
111 return (0);
115 cmd_unbind_key_table(struct cmd *self, struct cmd_ctx *ctx)
117 struct cmd_unbind_key_data *data = self->data;
118 const struct mode_key_table *mtab;
119 struct mode_key_binding *mbind, mtmp;
121 if ((mtab = mode_key_findtable(data->tablename)) == NULL) {
122 ctx->error(ctx, "unknown key table: %s", data->tablename);
123 return (-1);
126 mtmp.key = data->key & ~KEYC_PREFIX;
127 mtmp.mode = data->command_key ? 1 : 0;
128 if ((mbind = SPLAY_FIND(mode_key_tree, mtab->tree, &mtmp)) != NULL) {
129 SPLAY_REMOVE(mode_key_tree, mtab->tree, mbind);
130 xfree(mbind);
132 return (0);
135 void
136 cmd_unbind_key_free(struct cmd *self)
138 struct cmd_unbind_key_data *data = self->data;
140 if (data->tablename != NULL)
141 xfree(data->tablename);
142 xfree(data);