2009-06-01 22:58:49 +00:00
|
|
|
/* $OpenBSD$ */
|
|
|
|
|
|
|
|
/*
|
2016-01-19 15:59:12 +00:00
|
|
|
* Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
|
2009-06-01 22:58:49 +00:00
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
|
|
|
|
* IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
2015-11-12 12:43:36 +00:00
|
|
|
#include <stdlib.h>
|
2009-07-21 22:41:00 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
2009-06-01 22:58:49 +00:00
|
|
|
#include "tmux.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* List key bindings.
|
|
|
|
*/
|
|
|
|
|
2016-10-16 19:04:05 +00:00
|
|
|
static enum cmd_retval cmd_list_keys_exec(struct cmd *, struct cmdq_item *);
|
2014-10-20 22:44:30 +00:00
|
|
|
|
2016-10-16 19:04:05 +00:00
|
|
|
static enum cmd_retval cmd_list_keys_commands(struct cmd *,
|
|
|
|
struct cmdq_item *);
|
2009-07-28 07:03:32 +00:00
|
|
|
|
2009-06-01 22:58:49 +00:00
|
|
|
const struct cmd_entry cmd_list_keys_entry = {
|
2015-12-13 21:53:57 +00:00
|
|
|
.name = "list-keys",
|
|
|
|
.alias = "lsk",
|
|
|
|
|
2020-02-15 15:08:08 +00:00
|
|
|
.args = { "1aNP:T:", 0, 1 },
|
|
|
|
.usage = "[-1aN] [-P prefix-string] [-T key-table] [key]",
|
2015-12-13 21:53:57 +00:00
|
|
|
|
2016-10-14 22:14:22 +00:00
|
|
|
.flags = CMD_STARTSERVER|CMD_AFTERHOOK,
|
2015-12-13 21:53:57 +00:00
|
|
|
.exec = cmd_list_keys_exec
|
2009-06-01 22:58:49 +00:00
|
|
|
};
|
|
|
|
|
2014-10-20 22:44:30 +00:00
|
|
|
const struct cmd_entry cmd_list_commands_entry = {
|
2015-12-13 21:53:57 +00:00
|
|
|
.name = "list-commands",
|
|
|
|
.alias = "lscm",
|
|
|
|
|
2020-04-05 08:40:31 +00:00
|
|
|
.args = { "F:", 0, 1 },
|
|
|
|
.usage = "[-F format] [command]",
|
2015-12-13 21:53:57 +00:00
|
|
|
|
2016-10-14 22:14:22 +00:00
|
|
|
.flags = CMD_STARTSERVER|CMD_AFTERHOOK,
|
2015-12-13 21:53:57 +00:00
|
|
|
.exec = cmd_list_keys_exec
|
2014-10-20 22:44:30 +00:00
|
|
|
};
|
|
|
|
|
2020-01-27 08:53:13 +00:00
|
|
|
static u_int
|
|
|
|
cmd_list_keys_get_width(const char *tablename, key_code only)
|
|
|
|
{
|
|
|
|
struct key_table *table;
|
|
|
|
struct key_binding *bd;
|
|
|
|
u_int width, keywidth = 0;
|
|
|
|
|
|
|
|
table = key_bindings_get_table(tablename, 0);
|
|
|
|
if (table == NULL)
|
|
|
|
return (0);
|
|
|
|
bd = key_bindings_first(table);
|
|
|
|
while (bd != NULL) {
|
|
|
|
if ((only != KEYC_UNKNOWN && bd->key != only) ||
|
|
|
|
KEYC_IS_MOUSE(bd->key) ||
|
|
|
|
bd->note == NULL) {
|
|
|
|
bd = key_bindings_next(table, bd);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
width = utf8_cstrwidth(key_string_lookup_key(bd->key));
|
|
|
|
if (width > keywidth)
|
|
|
|
keywidth = width;
|
|
|
|
|
|
|
|
bd = key_bindings_next(table, bd);
|
|
|
|
}
|
|
|
|
return (keywidth);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
cmd_list_keys_print_notes(struct cmdq_item *item, struct args *args,
|
|
|
|
const char *tablename, u_int keywidth, key_code only, const char *prefix)
|
|
|
|
{
|
2020-04-13 20:51:57 +00:00
|
|
|
struct client *tc = cmdq_get_target_client(item);
|
2020-01-27 08:53:13 +00:00
|
|
|
struct key_table *table;
|
|
|
|
struct key_binding *bd;
|
|
|
|
const char *key;
|
2020-02-15 15:08:08 +00:00
|
|
|
char *tmp, *note;
|
2020-01-27 08:53:13 +00:00
|
|
|
int found = 0;
|
|
|
|
|
|
|
|
table = key_bindings_get_table(tablename, 0);
|
|
|
|
if (table == NULL)
|
|
|
|
return (0);
|
|
|
|
bd = key_bindings_first(table);
|
|
|
|
while (bd != NULL) {
|
|
|
|
if ((only != KEYC_UNKNOWN && bd->key != only) ||
|
|
|
|
KEYC_IS_MOUSE(bd->key) ||
|
2020-02-15 15:08:08 +00:00
|
|
|
(bd->note == NULL && !args_has(args, 'a'))) {
|
2020-01-27 08:53:13 +00:00
|
|
|
bd = key_bindings_next(table, bd);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
found = 1;
|
|
|
|
key = key_string_lookup_key(bd->key);
|
|
|
|
|
2020-02-15 15:08:08 +00:00
|
|
|
if (bd->note == NULL)
|
|
|
|
note = cmd_list_print(bd->cmdlist, 1);
|
|
|
|
else
|
|
|
|
note = xstrdup(bd->note);
|
2020-01-27 08:53:13 +00:00
|
|
|
tmp = utf8_padcstr(key, keywidth + 1);
|
2020-04-13 20:51:57 +00:00
|
|
|
if (args_has(args, '1') && tc != NULL)
|
|
|
|
status_message_set(tc, "%s%s%s", prefix, tmp, note);
|
2020-01-27 08:53:13 +00:00
|
|
|
else
|
2020-02-15 15:08:08 +00:00
|
|
|
cmdq_print(item, "%s%s%s", prefix, tmp, note);
|
2020-01-27 08:53:13 +00:00
|
|
|
free(tmp);
|
2020-02-15 15:08:08 +00:00
|
|
|
free(note);
|
2020-01-27 08:53:13 +00:00
|
|
|
|
|
|
|
if (args_has(args, '1'))
|
|
|
|
break;
|
|
|
|
bd = key_bindings_next(table, bd);
|
|
|
|
}
|
|
|
|
return (found);
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *
|
|
|
|
cmd_list_keys_get_prefix(struct args *args, key_code *prefix)
|
|
|
|
{
|
|
|
|
char *s;
|
|
|
|
|
|
|
|
*prefix = options_get_number(global_s_options, "prefix");
|
|
|
|
if (!args_has(args, 'P')) {
|
|
|
|
if (*prefix != KEYC_NONE)
|
|
|
|
xasprintf(&s, "%s ", key_string_lookup_key(*prefix));
|
|
|
|
else
|
|
|
|
s = xstrdup("");
|
|
|
|
} else
|
|
|
|
s = xstrdup(args_get(args, 'P'));
|
|
|
|
return (s);
|
|
|
|
}
|
|
|
|
|
2016-06-15 08:54:11 +00:00
|
|
|
static enum cmd_retval
|
2016-10-16 19:04:05 +00:00
|
|
|
cmd_list_keys_exec(struct cmd *self, struct cmdq_item *item)
|
2009-06-01 22:58:49 +00:00
|
|
|
{
|
2020-04-13 08:26:27 +00:00
|
|
|
struct args *args = cmd_get_args(self);
|
2015-04-20 15:34:56 +00:00
|
|
|
struct key_table *table;
|
2009-06-01 22:58:49 +00:00
|
|
|
struct key_binding *bd;
|
2019-05-23 14:03:44 +00:00
|
|
|
const char *tablename, *r;
|
2020-01-27 08:53:13 +00:00
|
|
|
char *key, *cp, *tmp, *start, *empty;
|
|
|
|
key_code prefix, only = KEYC_UNKNOWN;
|
|
|
|
int repeat, width, tablewidth, keywidth, found = 0;
|
2019-10-03 10:39:08 +00:00
|
|
|
size_t tmpsize, tmpused, cplen;
|
2009-07-21 22:41:00 +00:00
|
|
|
|
2020-04-13 08:26:27 +00:00
|
|
|
if (cmd_get_entry(self) == &cmd_list_commands_entry)
|
2016-10-16 19:04:05 +00:00
|
|
|
return (cmd_list_keys_commands(self, item));
|
2014-10-20 22:44:30 +00:00
|
|
|
|
2020-01-27 08:53:13 +00:00
|
|
|
if (args->argc != 0) {
|
|
|
|
only = key_string_lookup_string(args->argv[0]);
|
|
|
|
if (only == KEYC_UNKNOWN) {
|
|
|
|
cmdq_error(item, "invalid key: %s", args->argv[0]);
|
|
|
|
return (CMD_RETURN_ERROR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-20 15:34:56 +00:00
|
|
|
tablename = args_get(args, 'T');
|
|
|
|
if (tablename != NULL && key_bindings_get_table(tablename, 0) == NULL) {
|
2016-10-16 19:04:05 +00:00
|
|
|
cmdq_error(item, "table %s doesn't exist", tablename);
|
2015-04-20 15:34:56 +00:00
|
|
|
return (CMD_RETURN_ERROR);
|
|
|
|
}
|
2011-07-08 15:18:20 +00:00
|
|
|
|
2020-01-27 08:53:13 +00:00
|
|
|
if (args_has(args, 'N')) {
|
|
|
|
if (tablename == NULL) {
|
|
|
|
start = cmd_list_keys_get_prefix(args, &prefix);
|
|
|
|
keywidth = cmd_list_keys_get_width("root", only);
|
|
|
|
if (prefix != KEYC_NONE) {
|
|
|
|
width = cmd_list_keys_get_width("prefix", only);
|
|
|
|
if (width == 0)
|
|
|
|
prefix = KEYC_NONE;
|
|
|
|
else if (width > keywidth)
|
|
|
|
keywidth = width;
|
|
|
|
}
|
|
|
|
empty = utf8_padcstr("", utf8_cstrwidth(start));
|
|
|
|
|
|
|
|
found = cmd_list_keys_print_notes(item, args, "root",
|
|
|
|
keywidth, only, empty);
|
|
|
|
if (prefix != KEYC_NONE) {
|
|
|
|
if (cmd_list_keys_print_notes(item, args,
|
|
|
|
"prefix", keywidth, only, start))
|
|
|
|
found = 1;
|
|
|
|
}
|
|
|
|
free(empty);
|
|
|
|
} else {
|
|
|
|
if (args_has(args, 'P'))
|
|
|
|
start = xstrdup(args_get(args, 'P'));
|
|
|
|
else
|
|
|
|
start = xstrdup("");
|
|
|
|
keywidth = cmd_list_keys_get_width(tablename, only);
|
|
|
|
found = cmd_list_keys_print_notes(item, args, tablename,
|
|
|
|
keywidth, only, start);
|
|
|
|
|
|
|
|
}
|
|
|
|
free(start);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2015-04-20 15:34:56 +00:00
|
|
|
repeat = 0;
|
|
|
|
tablewidth = keywidth = 0;
|
2018-08-02 11:44:07 +00:00
|
|
|
table = key_bindings_first_table ();
|
|
|
|
while (table != NULL) {
|
|
|
|
if (tablename != NULL && strcmp(table->name, tablename) != 0) {
|
|
|
|
table = key_bindings_next_table(table);
|
2009-07-21 22:41:00 +00:00
|
|
|
continue;
|
2018-08-02 11:44:07 +00:00
|
|
|
}
|
|
|
|
bd = key_bindings_first(table);
|
|
|
|
while (bd != NULL) {
|
2020-01-27 08:53:13 +00:00
|
|
|
if (only != KEYC_UNKNOWN && bd->key != only) {
|
|
|
|
bd = key_bindings_next(table, bd);
|
|
|
|
continue;
|
|
|
|
}
|
2019-05-23 14:03:44 +00:00
|
|
|
key = args_escape(key_string_lookup_key(bd->key));
|
2009-07-21 22:41:00 +00:00
|
|
|
|
2017-04-21 14:01:19 +00:00
|
|
|
if (bd->flags & KEY_BINDING_REPEAT)
|
2015-04-20 15:34:56 +00:00
|
|
|
repeat = 1;
|
|
|
|
|
2015-11-12 12:43:36 +00:00
|
|
|
width = utf8_cstrwidth(table->name);
|
2015-04-20 15:34:56 +00:00
|
|
|
if (width > tablewidth)
|
2015-11-12 11:05:34 +00:00
|
|
|
tablewidth = width;
|
|
|
|
width = utf8_cstrwidth(key);
|
2015-04-20 15:34:56 +00:00
|
|
|
if (width > keywidth)
|
|
|
|
keywidth = width;
|
2018-08-02 11:44:07 +00:00
|
|
|
|
2019-05-23 14:03:44 +00:00
|
|
|
free(key);
|
2018-08-02 11:44:07 +00:00
|
|
|
bd = key_bindings_next(table, bd);
|
2015-04-20 15:34:56 +00:00
|
|
|
}
|
2018-08-02 11:44:07 +00:00
|
|
|
table = key_bindings_next_table(table);
|
2009-07-21 22:41:00 +00:00
|
|
|
}
|
|
|
|
|
2019-10-03 10:39:08 +00:00
|
|
|
tmpsize = 256;
|
|
|
|
tmp = xmalloc(tmpsize);
|
|
|
|
|
2018-08-02 11:44:07 +00:00
|
|
|
table = key_bindings_first_table ();
|
|
|
|
while (table != NULL) {
|
|
|
|
if (tablename != NULL && strcmp(table->name, tablename) != 0) {
|
|
|
|
table = key_bindings_next_table(table);
|
2009-06-01 22:58:49 +00:00
|
|
|
continue;
|
2018-08-02 11:44:07 +00:00
|
|
|
}
|
|
|
|
bd = key_bindings_first(table);
|
|
|
|
while (bd != NULL) {
|
2020-01-27 08:53:13 +00:00
|
|
|
if (only != KEYC_UNKNOWN && bd->key != only) {
|
|
|
|
bd = key_bindings_next(table, bd);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
found = 1;
|
2019-05-23 14:03:44 +00:00
|
|
|
key = args_escape(key_string_lookup_key(bd->key));
|
2015-04-20 15:34:56 +00:00
|
|
|
|
|
|
|
if (!repeat)
|
|
|
|
r = "";
|
2017-04-21 14:01:19 +00:00
|
|
|
else if (bd->flags & KEY_BINDING_REPEAT)
|
2015-04-20 15:34:56 +00:00
|
|
|
r = "-r ";
|
2011-07-04 00:31:57 +00:00
|
|
|
else
|
2015-04-20 15:34:56 +00:00
|
|
|
r = " ";
|
2019-10-03 10:39:08 +00:00
|
|
|
tmpused = xsnprintf(tmp, tmpsize, "%s-T ", r);
|
2015-11-12 12:43:36 +00:00
|
|
|
|
|
|
|
cp = utf8_padcstr(table->name, tablewidth);
|
2019-10-03 10:39:08 +00:00
|
|
|
cplen = strlen(cp) + 1;
|
2019-10-14 09:16:48 +00:00
|
|
|
while (tmpused + cplen + 1 >= tmpsize) {
|
2019-10-03 10:39:08 +00:00
|
|
|
tmpsize *= 2;
|
|
|
|
tmp = xrealloc(tmp, tmpsize);
|
|
|
|
}
|
2020-04-09 13:56:46 +00:00
|
|
|
strlcat(tmp, cp, tmpsize);
|
2019-10-03 10:39:08 +00:00
|
|
|
tmpused = strlcat(tmp, " ", tmpsize);
|
2015-11-12 12:43:36 +00:00
|
|
|
free(cp);
|
|
|
|
|
|
|
|
cp = utf8_padcstr(key, keywidth);
|
2019-10-03 10:39:08 +00:00
|
|
|
cplen = strlen(cp) + 1;
|
|
|
|
while (tmpused + cplen + 1 >= tmpsize) {
|
|
|
|
tmpsize *= 2;
|
|
|
|
tmp = xrealloc(tmp, tmpsize);
|
|
|
|
}
|
2020-04-09 13:56:46 +00:00
|
|
|
strlcat(tmp, cp, tmpsize);
|
2019-10-03 10:39:08 +00:00
|
|
|
tmpused = strlcat(tmp, " ", tmpsize);
|
2015-11-12 12:43:36 +00:00
|
|
|
free(cp);
|
|
|
|
|
2019-05-23 14:03:44 +00:00
|
|
|
cp = cmd_list_print(bd->cmdlist, 1);
|
2019-10-03 10:39:08 +00:00
|
|
|
cplen = strlen(cp);
|
|
|
|
while (tmpused + cplen + 1 >= tmpsize) {
|
|
|
|
tmpsize *= 2;
|
|
|
|
tmp = xrealloc(tmp, tmpsize);
|
|
|
|
}
|
|
|
|
strlcat(tmp, cp, tmpsize);
|
2015-11-27 15:06:43 +00:00
|
|
|
free(cp);
|
2015-04-20 15:34:56 +00:00
|
|
|
|
2016-10-16 19:04:05 +00:00
|
|
|
cmdq_print(item, "bind-key %s", tmp);
|
2019-05-23 14:03:44 +00:00
|
|
|
|
|
|
|
free(key);
|
2018-08-02 11:44:07 +00:00
|
|
|
bd = key_bindings_next(table, bd);
|
2015-04-20 15:34:56 +00:00
|
|
|
}
|
2018-08-02 11:44:07 +00:00
|
|
|
table = key_bindings_next_table(table);
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|
|
|
|
|
2019-10-03 10:39:08 +00:00
|
|
|
free(tmp);
|
|
|
|
|
2020-01-27 08:53:13 +00:00
|
|
|
out:
|
|
|
|
if (only != KEYC_UNKNOWN && !found) {
|
|
|
|
cmdq_error(item, "unknown key: %s", args->argv[0]);
|
|
|
|
return (CMD_RETURN_ERROR);
|
|
|
|
}
|
2012-07-11 07:10:15 +00:00
|
|
|
return (CMD_RETURN_NORMAL);
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|
2009-07-28 07:03:32 +00:00
|
|
|
|
2016-06-15 08:54:11 +00:00
|
|
|
static enum cmd_retval
|
2016-10-16 19:04:05 +00:00
|
|
|
cmd_list_keys_commands(struct cmd *self, struct cmdq_item *item)
|
2014-10-20 22:44:30 +00:00
|
|
|
{
|
2020-04-13 08:26:27 +00:00
|
|
|
struct args *args = cmd_get_args(self);
|
2014-10-20 22:44:30 +00:00
|
|
|
const struct cmd_entry **entryp;
|
2014-10-20 23:27:14 +00:00
|
|
|
const struct cmd_entry *entry;
|
2016-06-15 08:54:11 +00:00
|
|
|
struct format_tree *ft;
|
2020-04-05 08:40:31 +00:00
|
|
|
const char *template, *s, *command = NULL;
|
2016-06-15 08:54:11 +00:00
|
|
|
char *line;
|
|
|
|
|
2020-04-05 08:40:31 +00:00
|
|
|
if (args->argc != 0)
|
|
|
|
command = args->argv[0];
|
|
|
|
|
2016-06-15 08:54:11 +00:00
|
|
|
if ((template = args_get(args, 'F')) == NULL) {
|
|
|
|
template = "#{command_list_name}"
|
|
|
|
"#{?command_list_alias, (#{command_list_alias}),} "
|
|
|
|
"#{command_list_usage}";
|
|
|
|
}
|
|
|
|
|
2020-04-13 10:59:58 +00:00
|
|
|
ft = format_create(cmdq_get_client(item), item, FORMAT_NONE, 0);
|
2016-06-15 08:54:11 +00:00
|
|
|
format_defaults(ft, NULL, NULL, NULL, NULL);
|
2014-10-20 22:44:30 +00:00
|
|
|
|
|
|
|
for (entryp = cmd_table; *entryp != NULL; entryp++) {
|
|
|
|
entry = *entryp;
|
2020-04-05 08:40:31 +00:00
|
|
|
if (command != NULL &&
|
|
|
|
(strcmp(entry->name, command) != 0 &&
|
|
|
|
(entry->alias == NULL ||
|
|
|
|
strcmp(entry->alias, command) != 0)))
|
|
|
|
continue;
|
2016-06-15 08:54:11 +00:00
|
|
|
|
|
|
|
format_add(ft, "command_list_name", "%s", entry->name);
|
2016-10-16 19:04:05 +00:00
|
|
|
if (entry->alias != NULL)
|
|
|
|
s = entry->alias;
|
|
|
|
else
|
|
|
|
s = "";
|
|
|
|
format_add(ft, "command_list_alias", "%s", s);
|
|
|
|
if (entry->usage != NULL)
|
|
|
|
s = entry->usage;
|
|
|
|
else
|
|
|
|
s = "";
|
|
|
|
format_add(ft, "command_list_usage", "%s", s);
|
2016-06-15 08:54:11 +00:00
|
|
|
|
|
|
|
line = format_expand(ft, template);
|
|
|
|
if (*line != '\0')
|
2016-10-16 19:04:05 +00:00
|
|
|
cmdq_print(item, "%s", line);
|
2016-06-15 08:54:11 +00:00
|
|
|
free(line);
|
2014-10-20 22:44:30 +00:00
|
|
|
}
|
|
|
|
|
2016-06-15 08:54:11 +00:00
|
|
|
format_free(ft);
|
2014-10-20 22:44:30 +00:00
|
|
|
return (CMD_RETURN_NORMAL);
|
|
|
|
}
|