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>
26 * Bind a key to a command, this recurses through cmd_*.
29 int cmd_bind_key_parse(struct cmd
*, int, char **, char **);
30 int cmd_bind_key_exec(struct cmd
*, struct cmd_ctx
*);
31 void cmd_bind_key_free(struct cmd
*);
32 size_t cmd_bind_key_print(struct cmd
*, char *, size_t);
34 int cmd_bind_key_table(struct cmd
*, struct cmd_ctx
*);
36 struct cmd_bind_key_data
{
39 struct cmd_list
*cmdlist
;
46 const struct cmd_entry cmd_bind_key_entry
= {
48 "[-cnr] [-t key-table] key command [arguments]",
58 cmd_bind_key_parse(struct cmd
*self
, int argc
, char **argv
, char **cause
)
60 struct cmd_bind_key_data
*data
;
61 int opt
, no_prefix
= 0;
63 self
->data
= data
= xmalloc(sizeof *data
);
66 data
->command_key
= 0;
67 data
->tablename
= NULL
;
70 while ((opt
= getopt(argc
, argv
, "cnrt:")) != -1) {
73 data
->command_key
= 1;
82 if (data
->tablename
== NULL
)
83 data
->tablename
= xstrdup(optarg
);
94 if ((data
->key
= key_string_lookup_string(argv
[0])) == KEYC_NONE
) {
95 xasprintf(cause
, "unknown key: %s", argv
[0]);
99 data
->key
|= KEYC_PREFIX
;
103 if (data
->tablename
!= NULL
) {
106 data
->modecmd
= xstrdup(argv
[0]);
108 if ((data
->cmdlist
= cmd_list_parse(argc
, argv
, cause
)) == NULL
)
115 xasprintf(cause
, "usage: %s %s", self
->entry
->name
, self
->entry
->usage
);
118 self
->entry
->free(self
);
123 cmd_bind_key_exec(struct cmd
*self
, unused
struct cmd_ctx
*ctx
)
125 struct cmd_bind_key_data
*data
= self
->data
;
129 if (data
->tablename
!= NULL
)
130 return (cmd_bind_key_table(self
, ctx
));
132 key_bindings_add(data
->key
, data
->can_repeat
, data
->cmdlist
);
133 data
->cmdlist
->references
++;
139 cmd_bind_key_table(struct cmd
*self
, struct cmd_ctx
*ctx
)
141 struct cmd_bind_key_data
*data
= self
->data
;
142 const struct mode_key_table
*mtab
;
143 struct mode_key_binding
*mbind
, mtmp
;
144 enum mode_key_cmd cmd
;
146 if ((mtab
= mode_key_findtable(data
->tablename
)) == NULL
) {
147 ctx
->error(ctx
, "unknown key table: %s", data
->tablename
);
151 cmd
= mode_key_fromstring(mtab
->cmdstr
, data
->modecmd
);
152 if (cmd
== MODEKEY_NONE
) {
153 ctx
->error(ctx
, "unknown command: %s", data
->modecmd
);
157 mtmp
.key
= data
->key
& ~KEYC_PREFIX
;
158 mtmp
.mode
= data
->command_key
? 1 : 0;
159 if ((mbind
= SPLAY_FIND(mode_key_tree
, mtab
->tree
, &mtmp
)) != NULL
) {
163 mbind
= xmalloc(sizeof *mbind
);
164 mbind
->key
= mtmp
.key
;
165 mbind
->mode
= mtmp
.mode
;
167 SPLAY_INSERT(mode_key_tree
, mtab
->tree
, mbind
);
172 cmd_bind_key_free(struct cmd
*self
)
174 struct cmd_bind_key_data
*data
= self
->data
;
176 if (data
->cmdlist
!= NULL
)
177 cmd_list_free(data
->cmdlist
);
178 if (data
->tablename
!= NULL
)
179 xfree(data
->tablename
);
180 if (data
->modecmd
!= NULL
)
181 xfree(data
->modecmd
);
186 cmd_bind_key_print(struct cmd
*self
, char *buf
, size_t len
)
188 struct cmd_bind_key_data
*data
= self
->data
;
192 off
+= xsnprintf(buf
, len
, "%s", self
->entry
->name
);
196 if (off
< len
&& data
->command_key
)
197 off
+= xsnprintf(buf
+ off
, len
- off
, " -c");
198 if (off
< len
&& !(data
->key
& KEYC_PREFIX
))
199 off
+= xsnprintf(buf
+ off
, len
- off
, " -n");
200 if (off
< len
&& data
->can_repeat
)
201 off
+= xsnprintf(buf
+ off
, len
- off
, " -r");
202 if (off
< len
&& data
->tablename
!= NULL
)
203 off
+= cmd_prarg(buf
+ off
, len
- off
, " -t ", data
->tablename
);
205 skey
= key_string_lookup_key(data
->key
& ~KEYC_PREFIX
);
206 off
+= xsnprintf(buf
+ off
, len
- off
, " %s ", skey
);
209 off
+= cmd_list_print(data
->cmdlist
, buf
+ off
, len
- off
);