Home | History | Annotate | Line # | Download | only in dist
cmd-list-keys.c revision 1.1.1.14
      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.14       wiz 	.args = { "1aNP:T:", 0, 1, NULL },
     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.14       wiz 	.args = { "F:", 0, 1, NULL },
     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.12  christos 		    bd->note == NULL ||
     72  1.1.1.12  christos 		    *bd->note == '\0') {
     73  1.1.1.11  christos 			bd = key_bindings_next(table, bd);
     74  1.1.1.11  christos 			continue;
     75  1.1.1.11  christos 		}
     76  1.1.1.12  christos 		width = utf8_cstrwidth(key_string_lookup_key(bd->key, 0));
     77  1.1.1.11  christos 		if (width > keywidth)
     78  1.1.1.11  christos 			keywidth = width;
     79  1.1.1.11  christos 
     80  1.1.1.11  christos 		bd = key_bindings_next(table, bd);
     81  1.1.1.11  christos 	}
     82  1.1.1.11  christos 	return (keywidth);
     83  1.1.1.11  christos }
     84  1.1.1.11  christos 
     85  1.1.1.11  christos static int
     86  1.1.1.11  christos cmd_list_keys_print_notes(struct cmdq_item *item, struct args *args,
     87  1.1.1.11  christos     const char *tablename, u_int keywidth, key_code only, const char *prefix)
     88  1.1.1.11  christos {
     89  1.1.1.12  christos 	struct client		*tc = cmdq_get_target_client(item);
     90  1.1.1.11  christos 	struct key_table	*table;
     91  1.1.1.11  christos 	struct key_binding	*bd;
     92  1.1.1.11  christos 	const char		*key;
     93  1.1.1.11  christos 	char			*tmp, *note;
     94  1.1.1.11  christos 	int	                 found = 0;
     95  1.1.1.11  christos 
     96  1.1.1.11  christos 	table = key_bindings_get_table(tablename, 0);
     97  1.1.1.11  christos 	if (table == NULL)
     98  1.1.1.11  christos 		return (0);
     99  1.1.1.11  christos 	bd = key_bindings_first(table);
    100  1.1.1.11  christos 	while (bd != NULL) {
    101  1.1.1.11  christos 		if ((only != KEYC_UNKNOWN && bd->key != only) ||
    102  1.1.1.11  christos 		    KEYC_IS_MOUSE(bd->key) ||
    103  1.1.1.12  christos 		    ((bd->note == NULL || *bd->note == '\0') &&
    104  1.1.1.12  christos 		    !args_has(args, 'a'))) {
    105  1.1.1.11  christos 			bd = key_bindings_next(table, bd);
    106  1.1.1.11  christos 			continue;
    107  1.1.1.11  christos 		}
    108  1.1.1.11  christos 		found = 1;
    109  1.1.1.12  christos 		key = key_string_lookup_key(bd->key, 0);
    110  1.1.1.11  christos 
    111  1.1.1.12  christos 		if (bd->note == NULL || *bd->note == '\0')
    112  1.1.1.11  christos 			note = cmd_list_print(bd->cmdlist, 1);
    113  1.1.1.11  christos 		else
    114  1.1.1.11  christos 			note = xstrdup(bd->note);
    115  1.1.1.11  christos 		tmp = utf8_padcstr(key, keywidth + 1);
    116  1.1.1.12  christos 		if (args_has(args, '1') && tc != NULL) {
    117  1.1.1.12  christos 			status_message_set(tc, -1, 1, 0, "%s%s%s", prefix, tmp,
    118  1.1.1.12  christos 			    note);
    119  1.1.1.12  christos 		} else
    120  1.1.1.11  christos 			cmdq_print(item, "%s%s%s", prefix, tmp, note);
    121  1.1.1.11  christos 		free(tmp);
    122  1.1.1.11  christos 		free(note);
    123  1.1.1.11  christos 
    124  1.1.1.11  christos 		if (args_has(args, '1'))
    125  1.1.1.11  christos 			break;
    126  1.1.1.11  christos 		bd = key_bindings_next(table, bd);
    127  1.1.1.11  christos 	}
    128  1.1.1.11  christos 	return (found);
    129  1.1.1.11  christos }
    130  1.1.1.11  christos 
    131  1.1.1.11  christos static char *
    132  1.1.1.11  christos cmd_list_keys_get_prefix(struct args *args, key_code *prefix)
    133  1.1.1.11  christos {
    134  1.1.1.11  christos 	char	*s;
    135  1.1.1.11  christos 
    136  1.1.1.11  christos 	*prefix = options_get_number(global_s_options, "prefix");
    137  1.1.1.11  christos 	if (!args_has(args, 'P')) {
    138  1.1.1.11  christos 		if (*prefix != KEYC_NONE)
    139  1.1.1.12  christos 			xasprintf(&s, "%s ", key_string_lookup_key(*prefix, 0));
    140  1.1.1.11  christos 		else
    141  1.1.1.11  christos 			s = xstrdup("");
    142  1.1.1.11  christos 	} else
    143  1.1.1.11  christos 		s = xstrdup(args_get(args, 'P'));
    144  1.1.1.11  christos 	return (s);
    145  1.1.1.11  christos }
    146  1.1.1.11  christos 
    147   1.1.1.7  christos static enum cmd_retval
    148   1.1.1.7  christos cmd_list_keys_exec(struct cmd *self, struct cmdq_item *item)
    149       1.1      jmmv {
    150  1.1.1.12  christos 	struct args		*args = cmd_get_args(self);
    151   1.1.1.5  christos 	struct key_table	*table;
    152       1.1      jmmv 	struct key_binding	*bd;
    153  1.1.1.14       wiz 	const char		*tablename, *r, *keystr;
    154  1.1.1.11  christos 	char			*key, *cp, *tmp, *start, *empty;
    155  1.1.1.11  christos 	key_code		 prefix, only = KEYC_UNKNOWN;
    156  1.1.1.11  christos 	int			 repeat, width, tablewidth, keywidth, found = 0;
    157  1.1.1.10  christos 	size_t			 tmpsize, tmpused, cplen;
    158   1.1.1.5  christos 
    159  1.1.1.12  christos 	if (cmd_get_entry(self) == &cmd_list_commands_entry)
    160   1.1.1.7  christos 		return (cmd_list_keys_commands(self, item));
    161       1.1      jmmv 
    162  1.1.1.14       wiz 	if ((keystr = args_string(args, 0)) != NULL) {
    163  1.1.1.14       wiz 		only = key_string_lookup_string(keystr);
    164  1.1.1.11  christos 		if (only == KEYC_UNKNOWN) {
    165  1.1.1.14       wiz 			cmdq_error(item, "invalid key: %s", keystr);
    166  1.1.1.11  christos 			return (CMD_RETURN_ERROR);
    167  1.1.1.11  christos 		}
    168  1.1.1.13  christos 		only &= (KEYC_MASK_KEY|KEYC_MASK_MODIFIERS);
    169  1.1.1.11  christos 	}
    170  1.1.1.11  christos 
    171   1.1.1.5  christos 	tablename = args_get(args, 'T');
    172   1.1.1.5  christos 	if (tablename != NULL && key_bindings_get_table(tablename, 0) == NULL) {
    173   1.1.1.7  christos 		cmdq_error(item, "table %s doesn't exist", tablename);
    174   1.1.1.5  christos 		return (CMD_RETURN_ERROR);
    175   1.1.1.5  christos 	}
    176   1.1.1.2      jmmv 
    177  1.1.1.11  christos 	if (args_has(args, 'N')) {
    178  1.1.1.11  christos 		if (tablename == NULL) {
    179  1.1.1.11  christos 			start = cmd_list_keys_get_prefix(args, &prefix);
    180  1.1.1.11  christos 			keywidth = cmd_list_keys_get_width("root", only);
    181  1.1.1.11  christos 			if (prefix != KEYC_NONE) {
    182  1.1.1.11  christos 				width = cmd_list_keys_get_width("prefix", only);
    183  1.1.1.11  christos 				if (width == 0)
    184  1.1.1.11  christos 					prefix = KEYC_NONE;
    185  1.1.1.11  christos 				else if (width > keywidth)
    186  1.1.1.11  christos 					keywidth = width;
    187  1.1.1.11  christos 			}
    188  1.1.1.11  christos 			empty = utf8_padcstr("", utf8_cstrwidth(start));
    189  1.1.1.11  christos 
    190  1.1.1.11  christos 			found = cmd_list_keys_print_notes(item, args, "root",
    191  1.1.1.11  christos 			    keywidth, only, empty);
    192  1.1.1.11  christos 			if (prefix != KEYC_NONE) {
    193  1.1.1.11  christos 				if (cmd_list_keys_print_notes(item, args,
    194  1.1.1.11  christos 				    "prefix", keywidth, only, start))
    195  1.1.1.11  christos 					found = 1;
    196  1.1.1.11  christos 			}
    197  1.1.1.11  christos 			free(empty);
    198  1.1.1.11  christos 		} else {
    199  1.1.1.11  christos 			if (args_has(args, 'P'))
    200  1.1.1.11  christos 				start = xstrdup(args_get(args, 'P'));
    201  1.1.1.11  christos 			else
    202  1.1.1.11  christos 				start = xstrdup("");
    203  1.1.1.11  christos 			keywidth = cmd_list_keys_get_width(tablename, only);
    204  1.1.1.11  christos 			found = cmd_list_keys_print_notes(item, args, tablename,
    205  1.1.1.11  christos 			    keywidth, only, start);
    206  1.1.1.11  christos 
    207  1.1.1.11  christos 		}
    208  1.1.1.11  christos 		free(start);
    209  1.1.1.11  christos 		goto out;
    210  1.1.1.11  christos 	}
    211  1.1.1.11  christos 
    212   1.1.1.5  christos 	repeat = 0;
    213   1.1.1.5  christos 	tablewidth = keywidth = 0;
    214  1.1.1.14       wiz 	table = key_bindings_first_table();
    215   1.1.1.9  christos 	while (table != NULL) {
    216   1.1.1.9  christos 		if (tablename != NULL && strcmp(table->name, tablename) != 0) {
    217   1.1.1.9  christos 			table = key_bindings_next_table(table);
    218       1.1      jmmv 			continue;
    219   1.1.1.9  christos 		}
    220   1.1.1.9  christos 		bd = key_bindings_first(table);
    221   1.1.1.9  christos 		while (bd != NULL) {
    222  1.1.1.11  christos 			if (only != KEYC_UNKNOWN && bd->key != only) {
    223  1.1.1.11  christos 				bd = key_bindings_next(table, bd);
    224  1.1.1.11  christos 				continue;
    225  1.1.1.11  christos 			}
    226  1.1.1.12  christos 			key = args_escape(key_string_lookup_key(bd->key, 0));
    227       1.1      jmmv 
    228   1.1.1.8  christos 			if (bd->flags & KEY_BINDING_REPEAT)
    229   1.1.1.5  christos 				repeat = 1;
    230   1.1.1.5  christos 
    231   1.1.1.6  christos 			width = utf8_cstrwidth(table->name);
    232   1.1.1.5  christos 			if (width > tablewidth)
    233   1.1.1.6  christos 				tablewidth = width;
    234   1.1.1.6  christos 			width = utf8_cstrwidth(key);
    235   1.1.1.5  christos 			if (width > keywidth)
    236   1.1.1.5  christos 				keywidth = width;
    237   1.1.1.9  christos 
    238  1.1.1.10  christos 			free(key);
    239   1.1.1.9  christos 			bd = key_bindings_next(table, bd);
    240   1.1.1.5  christos 		}
    241   1.1.1.9  christos 		table = key_bindings_next_table(table);
    242       1.1      jmmv 	}
    243       1.1      jmmv 
    244  1.1.1.10  christos 	tmpsize = 256;
    245  1.1.1.10  christos 	tmp = xmalloc(tmpsize);
    246  1.1.1.10  christos 
    247  1.1.1.14       wiz 	table = key_bindings_first_table();
    248   1.1.1.9  christos 	while (table != NULL) {
    249   1.1.1.9  christos 		if (tablename != NULL && strcmp(table->name, tablename) != 0) {
    250   1.1.1.9  christos 			table = key_bindings_next_table(table);
    251       1.1      jmmv 			continue;
    252   1.1.1.9  christos 		}
    253   1.1.1.9  christos 		bd = key_bindings_first(table);
    254   1.1.1.9  christos 		while (bd != NULL) {
    255  1.1.1.11  christos 			if (only != KEYC_UNKNOWN && bd->key != only) {
    256  1.1.1.11  christos 				bd = key_bindings_next(table, bd);
    257  1.1.1.11  christos 				continue;
    258  1.1.1.11  christos 			}
    259  1.1.1.11  christos 			found = 1;
    260  1.1.1.12  christos 			key = args_escape(key_string_lookup_key(bd->key, 0));
    261   1.1.1.5  christos 
    262   1.1.1.5  christos 			if (!repeat)
    263   1.1.1.5  christos 				r = "";
    264   1.1.1.8  christos 			else if (bd->flags & KEY_BINDING_REPEAT)
    265   1.1.1.5  christos 				r = "-r ";
    266   1.1.1.2      jmmv 			else
    267   1.1.1.5  christos 				r = "   ";
    268  1.1.1.10  christos 			tmpused = xsnprintf(tmp, tmpsize, "%s-T ", r);
    269   1.1.1.6  christos 
    270   1.1.1.6  christos 			cp = utf8_padcstr(table->name, tablewidth);
    271  1.1.1.10  christos 			cplen = strlen(cp) + 1;
    272  1.1.1.10  christos 			while (tmpused + cplen + 1 >= tmpsize) {
    273  1.1.1.10  christos 				tmpsize *= 2;
    274  1.1.1.10  christos 				tmp = xrealloc(tmp, tmpsize);
    275  1.1.1.10  christos 			}
    276  1.1.1.12  christos 			strlcat(tmp, cp, tmpsize);
    277  1.1.1.10  christos 			tmpused = strlcat(tmp, " ", tmpsize);
    278   1.1.1.6  christos 			free(cp);
    279   1.1.1.6  christos 
    280   1.1.1.6  christos 			cp = utf8_padcstr(key, keywidth);
    281  1.1.1.10  christos 			cplen = strlen(cp) + 1;
    282  1.1.1.10  christos 			while (tmpused + cplen + 1 >= tmpsize) {
    283  1.1.1.10  christos 				tmpsize *= 2;
    284  1.1.1.10  christos 				tmp = xrealloc(tmp, tmpsize);
    285  1.1.1.10  christos 			}
    286  1.1.1.12  christos 			strlcat(tmp, cp, tmpsize);
    287  1.1.1.10  christos 			tmpused = strlcat(tmp, " ", tmpsize);
    288   1.1.1.6  christos 			free(cp);
    289   1.1.1.6  christos 
    290  1.1.1.10  christos 			cp = cmd_list_print(bd->cmdlist, 1);
    291  1.1.1.10  christos 			cplen = strlen(cp);
    292  1.1.1.10  christos 			while (tmpused + cplen + 1 >= tmpsize) {
    293  1.1.1.10  christos 				tmpsize *= 2;
    294  1.1.1.10  christos 				tmp = xrealloc(tmp, tmpsize);
    295  1.1.1.10  christos 			}
    296  1.1.1.10  christos 			strlcat(tmp, cp, tmpsize);
    297   1.1.1.6  christos 			free(cp);
    298       1.1      jmmv 
    299   1.1.1.7  christos 			cmdq_print(item, "bind-key %s", tmp);
    300  1.1.1.10  christos 
    301  1.1.1.10  christos 			free(key);
    302   1.1.1.9  christos 			bd = key_bindings_next(table, bd);
    303   1.1.1.2      jmmv 		}
    304   1.1.1.9  christos 		table = key_bindings_next_table(table);
    305       1.1      jmmv 	}
    306       1.1      jmmv 
    307  1.1.1.10  christos 	free(tmp);
    308  1.1.1.10  christos 
    309  1.1.1.11  christos out:
    310  1.1.1.11  christos 	if (only != KEYC_UNKNOWN && !found) {
    311  1.1.1.14       wiz 		cmdq_error(item, "unknown key: %s", args_string(args, 0));
    312  1.1.1.11  christos 		return (CMD_RETURN_ERROR);
    313  1.1.1.11  christos 	}
    314   1.1.1.3  christos 	return (CMD_RETURN_NORMAL);
    315       1.1      jmmv }
    316   1.1.1.5  christos 
    317   1.1.1.7  christos static enum cmd_retval
    318   1.1.1.7  christos cmd_list_keys_commands(struct cmd *self, struct cmdq_item *item)
    319   1.1.1.5  christos {
    320  1.1.1.12  christos 	struct args		 *args = cmd_get_args(self);
    321   1.1.1.5  christos 	const struct cmd_entry	**entryp;
    322   1.1.1.5  christos 	const struct cmd_entry	 *entry;
    323   1.1.1.7  christos 	struct format_tree	 *ft;
    324  1.1.1.14       wiz 	const char		 *template, *s, *command;
    325   1.1.1.7  christos 	char			 *line;
    326   1.1.1.7  christos 
    327   1.1.1.7  christos 	if ((template = args_get(args, 'F')) == NULL) {
    328   1.1.1.7  christos 		template = "#{command_list_name}"
    329   1.1.1.7  christos 		    "#{?command_list_alias, (#{command_list_alias}),} "
    330   1.1.1.7  christos 		    "#{command_list_usage}";
    331   1.1.1.7  christos 	}
    332   1.1.1.7  christos 
    333  1.1.1.12  christos 	ft = format_create(cmdq_get_client(item), item, FORMAT_NONE, 0);
    334   1.1.1.7  christos 	format_defaults(ft, NULL, NULL, NULL, NULL);
    335   1.1.1.5  christos 
    336  1.1.1.14       wiz 	command = args_string(args, 0);
    337   1.1.1.5  christos 	for (entryp = cmd_table; *entryp != NULL; entryp++) {
    338   1.1.1.5  christos 		entry = *entryp;
    339  1.1.1.11  christos 		if (command != NULL &&
    340  1.1.1.11  christos 		    (strcmp(entry->name, command) != 0 &&
    341  1.1.1.11  christos 		    (entry->alias == NULL ||
    342  1.1.1.11  christos 		    strcmp(entry->alias, command) != 0)))
    343  1.1.1.11  christos 		    continue;
    344   1.1.1.7  christos 
    345   1.1.1.7  christos 		format_add(ft, "command_list_name", "%s", entry->name);
    346   1.1.1.7  christos 		if (entry->alias != NULL)
    347   1.1.1.7  christos 			s = entry->alias;
    348   1.1.1.7  christos 		else
    349   1.1.1.7  christos 			s = "";
    350   1.1.1.7  christos 		format_add(ft, "command_list_alias", "%s", s);
    351   1.1.1.7  christos 		if (entry->usage != NULL)
    352   1.1.1.7  christos 			s = entry->usage;
    353   1.1.1.7  christos 		else
    354   1.1.1.7  christos 			s = "";
    355   1.1.1.7  christos 		format_add(ft, "command_list_usage", "%s", s);
    356   1.1.1.7  christos 
    357   1.1.1.7  christos 		line = format_expand(ft, template);
    358   1.1.1.7  christos 		if (*line != '\0')
    359   1.1.1.7  christos 			cmdq_print(item, "%s", line);
    360   1.1.1.7  christos 		free(line);
    361   1.1.1.5  christos 	}
    362   1.1.1.5  christos 
    363   1.1.1.7  christos 	format_free(ft);
    364   1.1.1.5  christos 	return (CMD_RETURN_NORMAL);
    365   1.1.1.5  christos }
    366