Home | History | Annotate | Line # | Download | only in dist
cmd-list-keys.c revision 1.1.1.11
      1   1.1.1.5  christos /* $OpenBSD$ */
      2       1.1      jmmv 
      3       1.1      jmmv /*
      4   1.1.1.6  christos  * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott (at) gmail.com>
      5       1.1      jmmv  *
      6       1.1      jmmv  * Permission to use, copy, modify, and distribute this software for any
      7       1.1      jmmv  * purpose with or without fee is hereby granted, provided that the above
      8       1.1      jmmv  * copyright notice and this permission notice appear in all copies.
      9       1.1      jmmv  *
     10       1.1      jmmv  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
     11       1.1      jmmv  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     12       1.1      jmmv  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     13       1.1      jmmv  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     14       1.1      jmmv  * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
     15       1.1      jmmv  * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
     16       1.1      jmmv  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     17       1.1      jmmv  */
     18       1.1      jmmv 
     19       1.1      jmmv #include <sys/types.h>
     20       1.1      jmmv 
     21   1.1.1.6  christos #include <stdlib.h>
     22       1.1      jmmv #include <string.h>
     23       1.1      jmmv 
     24       1.1      jmmv #include "tmux.h"
     25       1.1      jmmv 
     26       1.1      jmmv /*
     27       1.1      jmmv  * List key bindings.
     28       1.1      jmmv  */
     29       1.1      jmmv 
     30   1.1.1.7  christos static enum cmd_retval	cmd_list_keys_exec(struct cmd *, struct cmdq_item *);
     31   1.1.1.5  christos 
     32   1.1.1.7  christos static enum cmd_retval	cmd_list_keys_commands(struct cmd *,
     33   1.1.1.7  christos 			    struct cmdq_item *);
     34       1.1      jmmv 
     35       1.1      jmmv const struct cmd_entry cmd_list_keys_entry = {
     36   1.1.1.6  christos 	.name = "list-keys",
     37   1.1.1.6  christos 	.alias = "lsk",
     38   1.1.1.6  christos 
     39  1.1.1.11  christos 	.args = { "1aNP:T:", 0, 1 },
     40  1.1.1.11  christos 	.usage = "[-1aN] [-P prefix-string] [-T key-table] [key]",
     41   1.1.1.6  christos 
     42   1.1.1.7  christos 	.flags = CMD_STARTSERVER|CMD_AFTERHOOK,
     43   1.1.1.6  christos 	.exec = cmd_list_keys_exec
     44   1.1.1.5  christos };
     45   1.1.1.5  christos 
     46   1.1.1.5  christos const struct cmd_entry cmd_list_commands_entry = {
     47   1.1.1.6  christos 	.name = "list-commands",
     48   1.1.1.6  christos 	.alias = "lscm",
     49   1.1.1.6  christos 
     50  1.1.1.11  christos 	.args = { "F:", 0, 1 },
     51  1.1.1.11  christos 	.usage = "[-F format] [command]",
     52   1.1.1.6  christos 
     53   1.1.1.7  christos 	.flags = CMD_STARTSERVER|CMD_AFTERHOOK,
     54   1.1.1.6  christos 	.exec = cmd_list_keys_exec
     55       1.1      jmmv };
     56       1.1      jmmv 
     57  1.1.1.11  christos static u_int
     58  1.1.1.11  christos cmd_list_keys_get_width(const char *tablename, key_code only)
     59  1.1.1.11  christos {
     60  1.1.1.11  christos 	struct key_table	*table;
     61  1.1.1.11  christos 	struct key_binding	*bd;
     62  1.1.1.11  christos 	u_int			 width, keywidth = 0;
     63  1.1.1.11  christos 
     64  1.1.1.11  christos 	table = key_bindings_get_table(tablename, 0);
     65  1.1.1.11  christos 	if (table == NULL)
     66  1.1.1.11  christos 		return (0);
     67  1.1.1.11  christos 	bd = key_bindings_first(table);
     68  1.1.1.11  christos 	while (bd != NULL) {
     69  1.1.1.11  christos 		if ((only != KEYC_UNKNOWN && bd->key != only) ||
     70  1.1.1.11  christos 		    KEYC_IS_MOUSE(bd->key) ||
     71  1.1.1.11  christos 		    bd->note == NULL) {
     72  1.1.1.11  christos 			bd = key_bindings_next(table, bd);
     73  1.1.1.11  christos 			continue;
     74  1.1.1.11  christos 		}
     75  1.1.1.11  christos 		width = utf8_cstrwidth(key_string_lookup_key(bd->key));
     76  1.1.1.11  christos 		if (width > keywidth)
     77  1.1.1.11  christos 			keywidth = width;
     78  1.1.1.11  christos 
     79  1.1.1.11  christos 		bd = key_bindings_next(table, bd);
     80  1.1.1.11  christos 	}
     81  1.1.1.11  christos 	return (keywidth);
     82  1.1.1.11  christos }
     83  1.1.1.11  christos 
     84  1.1.1.11  christos static int
     85  1.1.1.11  christos cmd_list_keys_print_notes(struct cmdq_item *item, struct args *args,
     86  1.1.1.11  christos     const char *tablename, u_int keywidth, key_code only, const char *prefix)
     87  1.1.1.11  christos {
     88  1.1.1.11  christos 	struct client		*c = cmd_find_client(item, NULL, 1);
     89  1.1.1.11  christos 	struct key_table	*table;
     90  1.1.1.11  christos 	struct key_binding	*bd;
     91  1.1.1.11  christos 	const char		*key;
     92  1.1.1.11  christos 	char			*tmp, *note;
     93  1.1.1.11  christos 	int	                 found = 0;
     94  1.1.1.11  christos 
     95  1.1.1.11  christos 	table = key_bindings_get_table(tablename, 0);
     96  1.1.1.11  christos 	if (table == NULL)
     97  1.1.1.11  christos 		return (0);
     98  1.1.1.11  christos 	bd = key_bindings_first(table);
     99  1.1.1.11  christos 	while (bd != NULL) {
    100  1.1.1.11  christos 		if ((only != KEYC_UNKNOWN && bd->key != only) ||
    101  1.1.1.11  christos 		    KEYC_IS_MOUSE(bd->key) ||
    102  1.1.1.11  christos 		    (bd->note == NULL && !args_has(args, 'a'))) {
    103  1.1.1.11  christos 			bd = key_bindings_next(table, bd);
    104  1.1.1.11  christos 			continue;
    105  1.1.1.11  christos 		}
    106  1.1.1.11  christos 		found = 1;
    107  1.1.1.11  christos 		key = key_string_lookup_key(bd->key);
    108  1.1.1.11  christos 
    109  1.1.1.11  christos 		if (bd->note == NULL)
    110  1.1.1.11  christos 			note = cmd_list_print(bd->cmdlist, 1);
    111  1.1.1.11  christos 		else
    112  1.1.1.11  christos 			note = xstrdup(bd->note);
    113  1.1.1.11  christos 		tmp = utf8_padcstr(key, keywidth + 1);
    114  1.1.1.11  christos 		if (args_has(args, '1') && c != NULL)
    115  1.1.1.11  christos 			status_message_set(c, "%s%s%s", prefix, tmp, note);
    116  1.1.1.11  christos 		else
    117  1.1.1.11  christos 			cmdq_print(item, "%s%s%s", prefix, tmp, note);
    118  1.1.1.11  christos 		free(tmp);
    119  1.1.1.11  christos 		free(note);
    120  1.1.1.11  christos 
    121  1.1.1.11  christos 		if (args_has(args, '1'))
    122  1.1.1.11  christos 			break;
    123  1.1.1.11  christos 		bd = key_bindings_next(table, bd);
    124  1.1.1.11  christos 	}
    125  1.1.1.11  christos 	return (found);
    126  1.1.1.11  christos }
    127  1.1.1.11  christos 
    128  1.1.1.11  christos static char *
    129  1.1.1.11  christos cmd_list_keys_get_prefix(struct args *args, key_code *prefix)
    130  1.1.1.11  christos {
    131  1.1.1.11  christos 	char	*s;
    132  1.1.1.11  christos 
    133  1.1.1.11  christos 	*prefix = options_get_number(global_s_options, "prefix");
    134  1.1.1.11  christos 	if (!args_has(args, 'P')) {
    135  1.1.1.11  christos 		if (*prefix != KEYC_NONE)
    136  1.1.1.11  christos 			xasprintf(&s, "%s ", key_string_lookup_key(*prefix));
    137  1.1.1.11  christos 		else
    138  1.1.1.11  christos 			s = xstrdup("");
    139  1.1.1.11  christos 	} else
    140  1.1.1.11  christos 		s = xstrdup(args_get(args, 'P'));
    141  1.1.1.11  christos 	return (s);
    142  1.1.1.11  christos }
    143  1.1.1.11  christos 
    144   1.1.1.7  christos static enum cmd_retval
    145   1.1.1.7  christos cmd_list_keys_exec(struct cmd *self, struct cmdq_item *item)
    146       1.1      jmmv {
    147   1.1.1.2      jmmv 	struct args		*args = self->args;
    148   1.1.1.5  christos 	struct key_table	*table;
    149       1.1      jmmv 	struct key_binding	*bd;
    150  1.1.1.10  christos 	const char		*tablename, *r;
    151  1.1.1.11  christos 	char			*key, *cp, *tmp, *start, *empty;
    152  1.1.1.11  christos 	key_code		 prefix, only = KEYC_UNKNOWN;
    153  1.1.1.11  christos 	int			 repeat, width, tablewidth, keywidth, found = 0;
    154  1.1.1.10  christos 	size_t			 tmpsize, tmpused, cplen;
    155   1.1.1.5  christos 
    156   1.1.1.5  christos 	if (self->entry == &cmd_list_commands_entry)
    157   1.1.1.7  christos 		return (cmd_list_keys_commands(self, item));
    158       1.1      jmmv 
    159  1.1.1.11  christos 	if (args->argc != 0) {
    160  1.1.1.11  christos 		only = key_string_lookup_string(args->argv[0]);
    161  1.1.1.11  christos 		if (only == KEYC_UNKNOWN) {
    162  1.1.1.11  christos 			cmdq_error(item, "invalid key: %s", args->argv[0]);
    163  1.1.1.11  christos 			return (CMD_RETURN_ERROR);
    164  1.1.1.11  christos 		}
    165  1.1.1.11  christos 	}
    166  1.1.1.11  christos 
    167   1.1.1.5  christos 	tablename = args_get(args, 'T');
    168   1.1.1.5  christos 	if (tablename != NULL && key_bindings_get_table(tablename, 0) == NULL) {
    169   1.1.1.7  christos 		cmdq_error(item, "table %s doesn't exist", tablename);
    170   1.1.1.5  christos 		return (CMD_RETURN_ERROR);
    171   1.1.1.5  christos 	}
    172   1.1.1.2      jmmv 
    173  1.1.1.11  christos 	if (args_has(args, 'N')) {
    174  1.1.1.11  christos 		if (tablename == NULL) {
    175  1.1.1.11  christos 			start = cmd_list_keys_get_prefix(args, &prefix);
    176  1.1.1.11  christos 			keywidth = cmd_list_keys_get_width("root", only);
    177  1.1.1.11  christos 			if (prefix != KEYC_NONE) {
    178  1.1.1.11  christos 				width = cmd_list_keys_get_width("prefix", only);
    179  1.1.1.11  christos 				if (width == 0)
    180  1.1.1.11  christos 					prefix = KEYC_NONE;
    181  1.1.1.11  christos 				else if (width > keywidth)
    182  1.1.1.11  christos 					keywidth = width;
    183  1.1.1.11  christos 			}
    184  1.1.1.11  christos 			empty = utf8_padcstr("", utf8_cstrwidth(start));
    185  1.1.1.11  christos 
    186  1.1.1.11  christos 			found = cmd_list_keys_print_notes(item, args, "root",
    187  1.1.1.11  christos 			    keywidth, only, empty);
    188  1.1.1.11  christos 			if (prefix != KEYC_NONE) {
    189  1.1.1.11  christos 				if (cmd_list_keys_print_notes(item, args,
    190  1.1.1.11  christos 				    "prefix", keywidth, only, start))
    191  1.1.1.11  christos 					found = 1;
    192  1.1.1.11  christos 			}
    193  1.1.1.11  christos 			free(empty);
    194  1.1.1.11  christos 		} else {
    195  1.1.1.11  christos 			if (args_has(args, 'P'))
    196  1.1.1.11  christos 				start = xstrdup(args_get(args, 'P'));
    197  1.1.1.11  christos 			else
    198  1.1.1.11  christos 				start = xstrdup("");
    199  1.1.1.11  christos 			keywidth = cmd_list_keys_get_width(tablename, only);
    200  1.1.1.11  christos 			found = cmd_list_keys_print_notes(item, args, tablename,
    201  1.1.1.11  christos 			    keywidth, only, start);
    202  1.1.1.11  christos 
    203  1.1.1.11  christos 		}
    204  1.1.1.11  christos 		free(start);
    205  1.1.1.11  christos 		goto out;
    206  1.1.1.11  christos 	}
    207  1.1.1.11  christos 
    208   1.1.1.5  christos 	repeat = 0;
    209   1.1.1.5  christos 	tablewidth = keywidth = 0;
    210   1.1.1.9  christos 	table = key_bindings_first_table ();
    211   1.1.1.9  christos 	while (table != NULL) {
    212   1.1.1.9  christos 		if (tablename != NULL && strcmp(table->name, tablename) != 0) {
    213   1.1.1.9  christos 			table = key_bindings_next_table(table);
    214       1.1      jmmv 			continue;
    215   1.1.1.9  christos 		}
    216   1.1.1.9  christos 		bd = key_bindings_first(table);
    217   1.1.1.9  christos 		while (bd != NULL) {
    218  1.1.1.11  christos 			if (only != KEYC_UNKNOWN && bd->key != only) {
    219  1.1.1.11  christos 				bd = key_bindings_next(table, bd);
    220  1.1.1.11  christos 				continue;
    221  1.1.1.11  christos 			}
    222  1.1.1.10  christos 			key = args_escape(key_string_lookup_key(bd->key));
    223       1.1      jmmv 
    224   1.1.1.8  christos 			if (bd->flags & KEY_BINDING_REPEAT)
    225   1.1.1.5  christos 				repeat = 1;
    226   1.1.1.5  christos 
    227   1.1.1.6  christos 			width = utf8_cstrwidth(table->name);
    228   1.1.1.5  christos 			if (width > tablewidth)
    229   1.1.1.6  christos 				tablewidth = width;
    230   1.1.1.6  christos 			width = utf8_cstrwidth(key);
    231   1.1.1.5  christos 			if (width > keywidth)
    232   1.1.1.5  christos 				keywidth = width;
    233   1.1.1.9  christos 
    234  1.1.1.10  christos 			free(key);
    235   1.1.1.9  christos 			bd = key_bindings_next(table, bd);
    236   1.1.1.5  christos 		}
    237   1.1.1.9  christos 		table = key_bindings_next_table(table);
    238       1.1      jmmv 	}
    239       1.1      jmmv 
    240  1.1.1.10  christos 	tmpsize = 256;
    241  1.1.1.10  christos 	tmp = xmalloc(tmpsize);
    242  1.1.1.10  christos 
    243   1.1.1.9  christos 	table = key_bindings_first_table ();
    244   1.1.1.9  christos 	while (table != NULL) {
    245   1.1.1.9  christos 		if (tablename != NULL && strcmp(table->name, tablename) != 0) {
    246   1.1.1.9  christos 			table = key_bindings_next_table(table);
    247       1.1      jmmv 			continue;
    248   1.1.1.9  christos 		}
    249   1.1.1.9  christos 		bd = key_bindings_first(table);
    250   1.1.1.9  christos 		while (bd != NULL) {
    251  1.1.1.11  christos 			if (only != KEYC_UNKNOWN && bd->key != only) {
    252  1.1.1.11  christos 				bd = key_bindings_next(table, bd);
    253  1.1.1.11  christos 				continue;
    254  1.1.1.11  christos 			}
    255  1.1.1.11  christos 			found = 1;
    256  1.1.1.10  christos 			key = args_escape(key_string_lookup_key(bd->key));
    257   1.1.1.5  christos 
    258   1.1.1.5  christos 			if (!repeat)
    259   1.1.1.5  christos 				r = "";
    260   1.1.1.8  christos 			else if (bd->flags & KEY_BINDING_REPEAT)
    261   1.1.1.5  christos 				r = "-r ";
    262   1.1.1.2      jmmv 			else
    263   1.1.1.5  christos 				r = "   ";
    264  1.1.1.10  christos 			tmpused = xsnprintf(tmp, tmpsize, "%s-T ", r);
    265   1.1.1.6  christos 
    266   1.1.1.6  christos 			cp = utf8_padcstr(table->name, tablewidth);
    267  1.1.1.10  christos 			cplen = strlen(cp) + 1;
    268  1.1.1.10  christos 			while (tmpused + cplen + 1 >= tmpsize) {
    269  1.1.1.10  christos 				tmpsize *= 2;
    270  1.1.1.10  christos 				tmp = xrealloc(tmp, tmpsize);
    271  1.1.1.10  christos 			}
    272  1.1.1.10  christos 			tmpused = strlcat(tmp, cp, tmpsize);
    273  1.1.1.10  christos 			tmpused = strlcat(tmp, " ", tmpsize);
    274   1.1.1.6  christos 			free(cp);
    275   1.1.1.6  christos 
    276   1.1.1.6  christos 			cp = utf8_padcstr(key, keywidth);
    277  1.1.1.10  christos 			cplen = strlen(cp) + 1;
    278  1.1.1.10  christos 			while (tmpused + cplen + 1 >= tmpsize) {
    279  1.1.1.10  christos 				tmpsize *= 2;
    280  1.1.1.10  christos 				tmp = xrealloc(tmp, tmpsize);
    281  1.1.1.10  christos 			}
    282  1.1.1.10  christos 			tmpused = strlcat(tmp, cp, tmpsize);
    283  1.1.1.10  christos 			tmpused = strlcat(tmp, " ", tmpsize);
    284   1.1.1.6  christos 			free(cp);
    285   1.1.1.6  christos 
    286  1.1.1.10  christos 			cp = cmd_list_print(bd->cmdlist, 1);
    287  1.1.1.10  christos 			cplen = strlen(cp);
    288  1.1.1.10  christos 			while (tmpused + cplen + 1 >= tmpsize) {
    289  1.1.1.10  christos 				tmpsize *= 2;
    290  1.1.1.10  christos 				tmp = xrealloc(tmp, tmpsize);
    291  1.1.1.10  christos 			}
    292  1.1.1.10  christos 			strlcat(tmp, cp, tmpsize);
    293   1.1.1.6  christos 			free(cp);
    294       1.1      jmmv 
    295   1.1.1.7  christos 			cmdq_print(item, "bind-key %s", tmp);
    296  1.1.1.10  christos 
    297  1.1.1.10  christos 			free(key);
    298   1.1.1.9  christos 			bd = key_bindings_next(table, bd);
    299   1.1.1.2      jmmv 		}
    300   1.1.1.9  christos 		table = key_bindings_next_table(table);
    301       1.1      jmmv 	}
    302       1.1      jmmv 
    303  1.1.1.10  christos 	free(tmp);
    304  1.1.1.10  christos 
    305  1.1.1.11  christos out:
    306  1.1.1.11  christos 	if (only != KEYC_UNKNOWN && !found) {
    307  1.1.1.11  christos 		cmdq_error(item, "unknown key: %s", args->argv[0]);
    308  1.1.1.11  christos 		return (CMD_RETURN_ERROR);
    309  1.1.1.11  christos 	}
    310   1.1.1.3  christos 	return (CMD_RETURN_NORMAL);
    311       1.1      jmmv }
    312   1.1.1.5  christos 
    313   1.1.1.7  christos static enum cmd_retval
    314   1.1.1.7  christos cmd_list_keys_commands(struct cmd *self, struct cmdq_item *item)
    315   1.1.1.5  christos {
    316  1.1.1.11  christos 	struct args		 *args = self->args;
    317   1.1.1.5  christos 	const struct cmd_entry	**entryp;
    318   1.1.1.5  christos 	const struct cmd_entry	 *entry;
    319   1.1.1.7  christos 	struct format_tree	 *ft;
    320  1.1.1.11  christos 	const char		 *template, *s, *command = NULL;
    321   1.1.1.7  christos 	char			 *line;
    322   1.1.1.7  christos 
    323  1.1.1.11  christos 	if (args->argc != 0)
    324  1.1.1.11  christos 		command = args->argv[0];
    325  1.1.1.11  christos 
    326   1.1.1.7  christos 	if ((template = args_get(args, 'F')) == NULL) {
    327   1.1.1.7  christos 		template = "#{command_list_name}"
    328   1.1.1.7  christos 		    "#{?command_list_alias, (#{command_list_alias}),} "
    329   1.1.1.7  christos 		    "#{command_list_usage}";
    330   1.1.1.7  christos 	}
    331   1.1.1.7  christos 
    332   1.1.1.8  christos 	ft = format_create(item->client, item, FORMAT_NONE, 0);
    333   1.1.1.7  christos 	format_defaults(ft, NULL, NULL, NULL, NULL);
    334   1.1.1.5  christos 
    335   1.1.1.5  christos 	for (entryp = cmd_table; *entryp != NULL; entryp++) {
    336   1.1.1.5  christos 		entry = *entryp;
    337  1.1.1.11  christos 		if (command != NULL &&
    338  1.1.1.11  christos 		    (strcmp(entry->name, command) != 0 &&
    339  1.1.1.11  christos 		    (entry->alias == NULL ||
    340  1.1.1.11  christos 		    strcmp(entry->alias, command) != 0)))
    341  1.1.1.11  christos 		    continue;
    342   1.1.1.7  christos 
    343   1.1.1.7  christos 		format_add(ft, "command_list_name", "%s", entry->name);
    344   1.1.1.7  christos 		if (entry->alias != NULL)
    345   1.1.1.7  christos 			s = entry->alias;
    346   1.1.1.7  christos 		else
    347   1.1.1.7  christos 			s = "";
    348   1.1.1.7  christos 		format_add(ft, "command_list_alias", "%s", s);
    349   1.1.1.7  christos 		if (entry->usage != NULL)
    350   1.1.1.7  christos 			s = entry->usage;
    351   1.1.1.7  christos 		else
    352   1.1.1.7  christos 			s = "";
    353   1.1.1.7  christos 		format_add(ft, "command_list_usage", "%s", s);
    354   1.1.1.7  christos 
    355   1.1.1.7  christos 		line = format_expand(ft, template);
    356   1.1.1.7  christos 		if (*line != '\0')
    357   1.1.1.7  christos 			cmdq_print(item, "%s", line);
    358   1.1.1.7  christos 		free(line);
    359   1.1.1.5  christos 	}
    360   1.1.1.5  christos 
    361   1.1.1.7  christos 	format_free(ft);
    362   1.1.1.5  christos 	return (CMD_RETURN_NORMAL);
    363   1.1.1.5  christos }
    364