Home | History | Annotate | Line # | Download | only in dist
cmd-list-keys.c revision 1.1.1.9
      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.7  christos 	.args = { "T:", 0, 0 },
     40  1.1.1.7  christos 	.usage = "[-T key-table]",
     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.7  christos 	.args = { "F:", 0, 0 },
     51  1.1.1.7  christos 	.usage = "[-F format]",
     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.7  christos static enum cmd_retval
     58  1.1.1.7  christos cmd_list_keys_exec(struct cmd *self, struct cmdq_item *item)
     59      1.1      jmmv {
     60  1.1.1.2      jmmv 	struct args		*args = self->args;
     61  1.1.1.5  christos 	struct key_table	*table;
     62      1.1      jmmv 	struct key_binding	*bd;
     63  1.1.1.5  christos 	const char		*key, *tablename, *r;
     64  1.1.1.6  christos 	char			*cp, tmp[BUFSIZ];
     65  1.1.1.5  christos 	int			 repeat, width, tablewidth, keywidth;
     66  1.1.1.5  christos 
     67  1.1.1.5  christos 	if (self->entry == &cmd_list_commands_entry)
     68  1.1.1.7  christos 		return (cmd_list_keys_commands(self, item));
     69      1.1      jmmv 
     70  1.1.1.5  christos 	tablename = args_get(args, 'T');
     71  1.1.1.5  christos 	if (tablename != NULL && key_bindings_get_table(tablename, 0) == NULL) {
     72  1.1.1.7  christos 		cmdq_error(item, "table %s doesn't exist", tablename);
     73  1.1.1.5  christos 		return (CMD_RETURN_ERROR);
     74  1.1.1.5  christos 	}
     75  1.1.1.2      jmmv 
     76  1.1.1.5  christos 	repeat = 0;
     77  1.1.1.5  christos 	tablewidth = keywidth = 0;
     78  1.1.1.9  christos 	table = key_bindings_first_table ();
     79  1.1.1.9  christos 	while (table != NULL) {
     80  1.1.1.9  christos 		if (tablename != NULL && strcmp(table->name, tablename) != 0) {
     81  1.1.1.9  christos 			table = key_bindings_next_table(table);
     82      1.1      jmmv 			continue;
     83  1.1.1.9  christos 		}
     84  1.1.1.9  christos 		bd = key_bindings_first(table);
     85  1.1.1.9  christos 		while (bd != NULL) {
     86  1.1.1.5  christos 			key = key_string_lookup_key(bd->key);
     87      1.1      jmmv 
     88  1.1.1.8  christos 			if (bd->flags & KEY_BINDING_REPEAT)
     89  1.1.1.5  christos 				repeat = 1;
     90  1.1.1.5  christos 
     91  1.1.1.6  christos 			width = utf8_cstrwidth(table->name);
     92  1.1.1.5  christos 			if (width > tablewidth)
     93  1.1.1.6  christos 				tablewidth = width;
     94  1.1.1.6  christos 			width = utf8_cstrwidth(key);
     95  1.1.1.5  christos 			if (width > keywidth)
     96  1.1.1.5  christos 				keywidth = width;
     97  1.1.1.9  christos 
     98  1.1.1.9  christos 			bd = key_bindings_next(table, bd);
     99  1.1.1.5  christos 		}
    100  1.1.1.9  christos 		table = key_bindings_next_table(table);
    101      1.1      jmmv 	}
    102      1.1      jmmv 
    103  1.1.1.9  christos 	table = key_bindings_first_table ();
    104  1.1.1.9  christos 	while (table != NULL) {
    105  1.1.1.9  christos 		if (tablename != NULL && strcmp(table->name, tablename) != 0) {
    106  1.1.1.9  christos 			table = key_bindings_next_table(table);
    107      1.1      jmmv 			continue;
    108  1.1.1.9  christos 		}
    109  1.1.1.9  christos 		bd = key_bindings_first(table);
    110  1.1.1.9  christos 		while (bd != NULL) {
    111  1.1.1.5  christos 			key = key_string_lookup_key(bd->key);
    112  1.1.1.5  christos 
    113  1.1.1.5  christos 			if (!repeat)
    114  1.1.1.5  christos 				r = "";
    115  1.1.1.8  christos 			else if (bd->flags & KEY_BINDING_REPEAT)
    116  1.1.1.5  christos 				r = "-r ";
    117  1.1.1.2      jmmv 			else
    118  1.1.1.5  christos 				r = "   ";
    119  1.1.1.6  christos 			xsnprintf(tmp, sizeof tmp, "%s-T ", r);
    120  1.1.1.6  christos 
    121  1.1.1.6  christos 			cp = utf8_padcstr(table->name, tablewidth);
    122  1.1.1.6  christos 			strlcat(tmp, cp, sizeof tmp);
    123  1.1.1.6  christos 			strlcat(tmp, " ", sizeof tmp);
    124  1.1.1.6  christos 			free(cp);
    125  1.1.1.6  christos 
    126  1.1.1.6  christos 			cp = utf8_padcstr(key, keywidth);
    127  1.1.1.6  christos 			strlcat(tmp, cp, sizeof tmp);
    128  1.1.1.6  christos 			strlcat(tmp, " ", sizeof tmp);
    129  1.1.1.6  christos 			free(cp);
    130  1.1.1.6  christos 
    131  1.1.1.6  christos 			cp = cmd_list_print(bd->cmdlist);
    132  1.1.1.6  christos 			strlcat(tmp, cp, sizeof tmp);
    133  1.1.1.6  christos 			free(cp);
    134      1.1      jmmv 
    135  1.1.1.7  christos 			cmdq_print(item, "bind-key %s", tmp);
    136  1.1.1.9  christos 			bd = key_bindings_next(table, bd);
    137  1.1.1.2      jmmv 		}
    138  1.1.1.9  christos 		table = key_bindings_next_table(table);
    139      1.1      jmmv 	}
    140      1.1      jmmv 
    141  1.1.1.3  christos 	return (CMD_RETURN_NORMAL);
    142      1.1      jmmv }
    143  1.1.1.5  christos 
    144  1.1.1.7  christos static enum cmd_retval
    145  1.1.1.7  christos cmd_list_keys_commands(struct cmd *self, struct cmdq_item *item)
    146  1.1.1.5  christos {
    147  1.1.1.7  christos 	struct args		*args = self->args;
    148  1.1.1.5  christos 	const struct cmd_entry	**entryp;
    149  1.1.1.5  christos 	const struct cmd_entry	 *entry;
    150  1.1.1.7  christos 	struct format_tree	 *ft;
    151  1.1.1.7  christos 	const char		 *template, *s;
    152  1.1.1.7  christos 	char			 *line;
    153  1.1.1.7  christos 
    154  1.1.1.7  christos 	if ((template = args_get(args, 'F')) == NULL) {
    155  1.1.1.7  christos 		template = "#{command_list_name}"
    156  1.1.1.7  christos 		    "#{?command_list_alias, (#{command_list_alias}),} "
    157  1.1.1.7  christos 		    "#{command_list_usage}";
    158  1.1.1.7  christos 	}
    159  1.1.1.7  christos 
    160  1.1.1.8  christos 	ft = format_create(item->client, item, FORMAT_NONE, 0);
    161  1.1.1.7  christos 	format_defaults(ft, NULL, NULL, NULL, NULL);
    162  1.1.1.5  christos 
    163  1.1.1.5  christos 	for (entryp = cmd_table; *entryp != NULL; entryp++) {
    164  1.1.1.5  christos 		entry = *entryp;
    165  1.1.1.7  christos 
    166  1.1.1.7  christos 		format_add(ft, "command_list_name", "%s", entry->name);
    167  1.1.1.7  christos 		if (entry->alias != NULL)
    168  1.1.1.7  christos 			s = entry->alias;
    169  1.1.1.7  christos 		else
    170  1.1.1.7  christos 			s = "";
    171  1.1.1.7  christos 		format_add(ft, "command_list_alias", "%s", s);
    172  1.1.1.7  christos 		if (entry->usage != NULL)
    173  1.1.1.7  christos 			s = entry->usage;
    174  1.1.1.7  christos 		else
    175  1.1.1.7  christos 			s = "";
    176  1.1.1.7  christos 		format_add(ft, "command_list_usage", "%s", s);
    177  1.1.1.7  christos 
    178  1.1.1.7  christos 		line = format_expand(ft, template);
    179  1.1.1.7  christos 		if (*line != '\0')
    180  1.1.1.7  christos 			cmdq_print(item, "%s", line);
    181  1.1.1.7  christos 		free(line);
    182  1.1.1.5  christos 	}
    183  1.1.1.5  christos 
    184  1.1.1.7  christos 	format_free(ft);
    185  1.1.1.5  christos 	return (CMD_RETURN_NORMAL);
    186  1.1.1.5  christos }
    187