Sync OpenBSD patchset 930:

Change the list-keys format so that it shows the keys using actual tmux
commands which should be able to be directly copied into the config
file. From Markus Schnalke, tweaked by me.
This commit is contained in:
Tiago Cunha 2011-07-04 08:26:41 +00:00
parent 8144e5cb7e
commit bc8d09ef84

View File

@ -1,4 +1,4 @@
/* $Id: cmd-list-keys.c,v 1.26 2011-01-07 14:45:34 tcunha Exp $ */ /* $Id$ */
/* /*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net> * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@ -46,7 +46,7 @@ cmd_list_keys_exec(struct cmd *self, struct cmd_ctx *ctx)
struct args *args = self->args; struct args *args = self->args;
struct key_binding *bd; struct key_binding *bd;
const char *key; const char *key;
char tmp[BUFSIZ]; char tmp[BUFSIZ], flags[8];
size_t used; size_t used;
int width, keywidth; int width, keywidth;
@ -59,9 +59,14 @@ cmd_list_keys_exec(struct cmd *self, struct cmd_ctx *ctx)
if (key == NULL) if (key == NULL)
continue; continue;
keywidth = strlen(key) + 1; keywidth = strlen(key);
if (!(bd->key & KEYC_PREFIX)) if (!(bd->key & KEYC_PREFIX)) {
keywidth += 2; if (bd->can_repeat)
keywidth += 4;
else
keywidth += 3;
} else if (bd->can_repeat)
keywidth += 3;
if (keywidth > width) if (keywidth > width)
width = keywidth; width = keywidth;
} }
@ -70,22 +75,22 @@ cmd_list_keys_exec(struct cmd *self, struct cmd_ctx *ctx)
key = key_string_lookup_key(bd->key & ~KEYC_PREFIX); key = key_string_lookup_key(bd->key & ~KEYC_PREFIX);
if (key == NULL) if (key == NULL)
continue; continue;
used = xsnprintf(tmp, sizeof tmp, "%*s: ", width, key);
if (!(bd->key & KEYC_PREFIX)) {
if (bd->can_repeat)
xsnprintf(flags, sizeof flags, "-rn ");
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) if (used >= sizeof tmp)
continue; continue;
if (!(bd->key & KEYC_PREFIX)) {
used = strlcat(tmp, "(no prefix) ", sizeof tmp);
if (used >= sizeof tmp)
continue;
}
if (bd->can_repeat) {
used = strlcat(tmp, "(repeat) ", sizeof tmp);
if (used >= sizeof tmp)
continue;
}
cmd_list_print(bd->cmdlist, tmp + used, (sizeof tmp) - used); cmd_list_print(bd->cmdlist, tmp + used, (sizeof tmp) - used);
ctx->print(ctx, "%s", tmp); ctx->print(ctx, "bind-key %s", tmp);
} }
return (0); return (0);
@ -99,7 +104,7 @@ cmd_list_keys_table(struct cmd *self, struct cmd_ctx *ctx)
const struct mode_key_table *mtab; const struct mode_key_table *mtab;
struct mode_key_binding *mbind; struct mode_key_binding *mbind;
const char *key, *cmdstr, *mode; const char *key, *cmdstr, *mode;
int width, keywidth; int width, keywidth, any_mode;
tablename = args_get(args, 't'); tablename = args_get(args, 't');
if ((mtab = mode_key_findtable(tablename)) == NULL) { if ((mtab = mode_key_findtable(tablename)) == NULL) {
@ -108,12 +113,16 @@ cmd_list_keys_table(struct cmd *self, struct cmd_ctx *ctx)
} }
width = 0; width = 0;
any_mode = 0;
SPLAY_FOREACH(mbind, mode_key_tree, mtab->tree) { SPLAY_FOREACH(mbind, mode_key_tree, mtab->tree) {
key = key_string_lookup_key(mbind->key); key = key_string_lookup_key(mbind->key);
if (key == NULL) if (key == NULL)
continue; continue;
keywidth = strlen(key) + 1; if (mbind->mode != 0)
any_mode = 1;
keywidth = strlen(key);
if (keywidth > width) if (keywidth > width)
width = keywidth; width = keywidth;
} }
@ -125,10 +134,13 @@ cmd_list_keys_table(struct cmd *self, struct cmd_ctx *ctx)
mode = ""; mode = "";
if (mbind->mode != 0) if (mbind->mode != 0)
mode = "(command mode) "; mode = "c";
cmdstr = mode_key_tostring(mtab->cmdstr, mbind->cmd); cmdstr = mode_key_tostring(mtab->cmdstr, mbind->cmd);
if (cmdstr != NULL) if (cmdstr != NULL) {
ctx->print(ctx, "%*s: %s%s", width, key, mode, cmdstr); ctx->print(ctx, "bind-key -%st %s%s %*s %s",
mode, any_mode && *mode == '\0' ? " " : "",
mtab->name, (int) width, key, cmdstr);
}
} }
return (0); return (0);