summaryrefslogtreecommitdiffstats
path: root/cmd-list-keys.c
diff options
context:
space:
mode:
authornicm <nicm>2015-04-20 15:34:56 +0000
committernicm <nicm>2015-04-20 15:34:56 +0000
commitbded7437064c76dd6cf4e76e558d826859adcc79 (patch)
tree708c5dee6ddf671161b5ffa7208cba05e52eb4c5 /cmd-list-keys.c
parent3497843f0272e573d0a63cb6e94948591ae07667 (diff)
Support for multiple key tables to commands to be bound to sequences of
keys. The default key bindings become the "prefix" table and -n the "root" table. Keys may be bound in new tables with bind -T and switch-client -T used to specify the table in which the next key should be looked up. Based on a diff from Keith Amling.
Diffstat (limited to 'cmd-list-keys.c')
-rw-r--r--cmd-list-keys.c86
1 files changed, 49 insertions, 37 deletions
diff --git a/cmd-list-keys.c b/cmd-list-keys.c
index 0733ee28..e2d5dc52 100644
--- a/cmd-list-keys.c
+++ b/cmd-list-keys.c
@@ -33,8 +33,8 @@ enum cmd_retval cmd_list_keys_commands(struct cmd *, struct cmd_q *);
const struct cmd_entry cmd_list_keys_entry = {
"list-keys", "lsk",
- "t:", 0, 0,
- "[-t key-table]",
+ "t:T:", 0, 0,
+ "[-t mode-table] [-T key-table]",
0,
cmd_list_keys_exec
};
@@ -51,11 +51,12 @@ enum cmd_retval
cmd_list_keys_exec(struct cmd *self, struct cmd_q *cmdq)
{
struct args *args = self->args;
+ struct key_table *table;
struct key_binding *bd;
- const char *key;
- char tmp[BUFSIZ], flags[8];
+ const char *key, *tablename, *r;
+ char tmp[BUFSIZ];
size_t used;
- int width, keywidth;
+ int repeat, width, tablewidth, keywidth;
if (self->entry == &cmd_list_commands_entry)
return (cmd_list_keys_commands(self, cmdq));
@@ -63,46 +64,57 @@ cmd_list_keys_exec(struct cmd *self, struct cmd_q *cmdq)
if (args_has(args, 't'))
return (cmd_list_keys_table(self, cmdq));
- width = 0;
+ tablename = args_get(args, 'T');
+ if (tablename != NULL && key_bindings_get_table(tablename, 0) == NULL) {
+ cmdq_error(cmdq, "table %s doesn't exist", tablename);
+ return (CMD_RETURN_ERROR);
+ }
- RB_FOREACH(bd, key_bindings, &key_bindings) {
- key = key_string_lookup_key(bd->key & ~KEYC_PREFIX);
- if (key == NULL)
+ repeat = 0;
+ tablewidth = keywidth = 0;
+ RB_FOREACH(table, key_tables, &key_tables) {
+ if (tablename != NULL && strcmp(table->name, tablename) != 0)
continue;
+ RB_FOREACH(bd, key_bindings, &table->key_bindings) {
+ key = key_string_lookup_key(bd->key);
+ if (key == NULL)
+ continue;
- keywidth = strlen(key);
- if (!(bd->key & KEYC_PREFIX)) {
if (bd->can_repeat)
- keywidth += 4;
- else
- keywidth += 3;
- } else if (bd->can_repeat)
- keywidth += 3;
- if (keywidth > width)
- width = keywidth;
+ repeat = 1;
+
+ width = strlen(table->name);
+ if (width > tablewidth)
+ tablewidth =width;
+ width = strlen(key);
+ if (width > keywidth)
+ keywidth = width;
+ }
}
- RB_FOREACH(bd, key_bindings, &key_bindings) {
- key = key_string_lookup_key(bd->key & ~KEYC_PREFIX);
- if (key == NULL)
+ RB_FOREACH(table, key_tables, &key_tables) {
+ if (tablename != NULL && strcmp(table->name, tablename) != 0)
continue;
-
- *flags = '\0';
- if (!(bd->key & KEYC_PREFIX)) {
- if (bd->can_repeat)
- xsnprintf(flags, sizeof flags, "-rn ");
+ RB_FOREACH(bd, key_bindings, &table->key_bindings) {
+ key = key_string_lookup_key(bd->key);
+ if (key == NULL)
+ continue;
+
+ if (!repeat)
+ r = "";
+ else if (bd->can_repeat)
+ r = "-r ";
else
- xsnprintf(flags, sizeof flags, "-n ");
- } else if (bd->can_repeat)
- xsnprintf(flags, sizeof flags, "-r ");
-
- used = xsnprintf(tmp, sizeof tmp, "%s%*s ",
- flags, (int) (width - strlen(flags)), key);
- if (used >= sizeof tmp)
- continue;
-
- cmd_list_print(bd->cmdlist, tmp + used, (sizeof tmp) - used);
- cmdq_print(cmdq, "bind-key %s", tmp);
+ r = " ";
+ used = xsnprintf(tmp, sizeof tmp, "%s-T %-*s %-*s ", r,
+ (int)tablewidth, table->name, (int)keywidth, key);
+ if (used < sizeof tmp) {
+ cmd_list_print(bd->cmdlist, tmp + used,
+ (sizeof tmp) - used);
+ }
+
+ cmdq_print(cmdq, "bind-key %s", tmp);
+ }
}
return (CMD_RETURN_NORMAL);