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>
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
{
40 const struct cmd_entry cmd_unbind_key_entry
= {
41 "unbind-key", "unbind",
42 "[-cn] [-t key-table] key",
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) {
64 data
->command_key
= 1;
70 if (data
->tablename
== NULL
)
71 data
->tablename
= xstrdup(optarg
);
82 if ((data
->key
= key_string_lookup_string(argv
[0])) == KEYC_NONE
) {
83 xasprintf(cause
, "unknown key: %s", argv
[0]);
87 data
->key
|= KEYC_PREFIX
;
92 xasprintf(cause
, "usage: %s %s", self
->entry
->name
, self
->entry
->usage
);
100 cmd_unbind_key_exec(struct cmd
*self
, unused
struct cmd_ctx
*ctx
)
102 struct cmd_unbind_key_data
*data
= self
->data
;
106 if (data
->tablename
!= NULL
)
107 return (cmd_unbind_key_table(self
, ctx
));
109 key_bindings_remove(data
->key
);
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
);
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
);
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
);