Home | History | Annotate | Line # | Download | only in dist
cmd-list-keys.c revision 1.1.1.9
      1 /* $OpenBSD$ */
      2 
      3 /*
      4  * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott (at) gmail.com>
      5  *
      6  * Permission to use, copy, modify, and distribute this software for any
      7  * purpose with or without fee is hereby granted, provided that the above
      8  * copyright notice and this permission notice appear in all copies.
      9  *
     10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
     11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     14  * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
     15  * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
     16  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     17  */
     18 
     19 #include <sys/types.h>
     20 
     21 #include <stdlib.h>
     22 #include <string.h>
     23 
     24 #include "tmux.h"
     25 
     26 /*
     27  * List key bindings.
     28  */
     29 
     30 static enum cmd_retval	cmd_list_keys_exec(struct cmd *, struct cmdq_item *);
     31 
     32 static enum cmd_retval	cmd_list_keys_commands(struct cmd *,
     33 			    struct cmdq_item *);
     34 
     35 const struct cmd_entry cmd_list_keys_entry = {
     36 	.name = "list-keys",
     37 	.alias = "lsk",
     38 
     39 	.args = { "T:", 0, 0 },
     40 	.usage = "[-T key-table]",
     41 
     42 	.flags = CMD_STARTSERVER|CMD_AFTERHOOK,
     43 	.exec = cmd_list_keys_exec
     44 };
     45 
     46 const struct cmd_entry cmd_list_commands_entry = {
     47 	.name = "list-commands",
     48 	.alias = "lscm",
     49 
     50 	.args = { "F:", 0, 0 },
     51 	.usage = "[-F format]",
     52 
     53 	.flags = CMD_STARTSERVER|CMD_AFTERHOOK,
     54 	.exec = cmd_list_keys_exec
     55 };
     56 
     57 static enum cmd_retval
     58 cmd_list_keys_exec(struct cmd *self, struct cmdq_item *item)
     59 {
     60 	struct args		*args = self->args;
     61 	struct key_table	*table;
     62 	struct key_binding	*bd;
     63 	const char		*key, *tablename, *r;
     64 	char			*cp, tmp[BUFSIZ];
     65 	int			 repeat, width, tablewidth, keywidth;
     66 
     67 	if (self->entry == &cmd_list_commands_entry)
     68 		return (cmd_list_keys_commands(self, item));
     69 
     70 	tablename = args_get(args, 'T');
     71 	if (tablename != NULL && key_bindings_get_table(tablename, 0) == NULL) {
     72 		cmdq_error(item, "table %s doesn't exist", tablename);
     73 		return (CMD_RETURN_ERROR);
     74 	}
     75 
     76 	repeat = 0;
     77 	tablewidth = keywidth = 0;
     78 	table = key_bindings_first_table ();
     79 	while (table != NULL) {
     80 		if (tablename != NULL && strcmp(table->name, tablename) != 0) {
     81 			table = key_bindings_next_table(table);
     82 			continue;
     83 		}
     84 		bd = key_bindings_first(table);
     85 		while (bd != NULL) {
     86 			key = key_string_lookup_key(bd->key);
     87 
     88 			if (bd->flags & KEY_BINDING_REPEAT)
     89 				repeat = 1;
     90 
     91 			width = utf8_cstrwidth(table->name);
     92 			if (width > tablewidth)
     93 				tablewidth = width;
     94 			width = utf8_cstrwidth(key);
     95 			if (width > keywidth)
     96 				keywidth = width;
     97 
     98 			bd = key_bindings_next(table, bd);
     99 		}
    100 		table = key_bindings_next_table(table);
    101 	}
    102 
    103 	table = key_bindings_first_table ();
    104 	while (table != NULL) {
    105 		if (tablename != NULL && strcmp(table->name, tablename) != 0) {
    106 			table = key_bindings_next_table(table);
    107 			continue;
    108 		}
    109 		bd = key_bindings_first(table);
    110 		while (bd != NULL) {
    111 			key = key_string_lookup_key(bd->key);
    112 
    113 			if (!repeat)
    114 				r = "";
    115 			else if (bd->flags & KEY_BINDING_REPEAT)
    116 				r = "-r ";
    117 			else
    118 				r = "   ";
    119 			xsnprintf(tmp, sizeof tmp, "%s-T ", r);
    120 
    121 			cp = utf8_padcstr(table->name, tablewidth);
    122 			strlcat(tmp, cp, sizeof tmp);
    123 			strlcat(tmp, " ", sizeof tmp);
    124 			free(cp);
    125 
    126 			cp = utf8_padcstr(key, keywidth);
    127 			strlcat(tmp, cp, sizeof tmp);
    128 			strlcat(tmp, " ", sizeof tmp);
    129 			free(cp);
    130 
    131 			cp = cmd_list_print(bd->cmdlist);
    132 			strlcat(tmp, cp, sizeof tmp);
    133 			free(cp);
    134 
    135 			cmdq_print(item, "bind-key %s", tmp);
    136 			bd = key_bindings_next(table, bd);
    137 		}
    138 		table = key_bindings_next_table(table);
    139 	}
    140 
    141 	return (CMD_RETURN_NORMAL);
    142 }
    143 
    144 static enum cmd_retval
    145 cmd_list_keys_commands(struct cmd *self, struct cmdq_item *item)
    146 {
    147 	struct args		*args = self->args;
    148 	const struct cmd_entry	**entryp;
    149 	const struct cmd_entry	 *entry;
    150 	struct format_tree	 *ft;
    151 	const char		 *template, *s;
    152 	char			 *line;
    153 
    154 	if ((template = args_get(args, 'F')) == NULL) {
    155 		template = "#{command_list_name}"
    156 		    "#{?command_list_alias, (#{command_list_alias}),} "
    157 		    "#{command_list_usage}";
    158 	}
    159 
    160 	ft = format_create(item->client, item, FORMAT_NONE, 0);
    161 	format_defaults(ft, NULL, NULL, NULL, NULL);
    162 
    163 	for (entryp = cmd_table; *entryp != NULL; entryp++) {
    164 		entry = *entryp;
    165 
    166 		format_add(ft, "command_list_name", "%s", entry->name);
    167 		if (entry->alias != NULL)
    168 			s = entry->alias;
    169 		else
    170 			s = "";
    171 		format_add(ft, "command_list_alias", "%s", s);
    172 		if (entry->usage != NULL)
    173 			s = entry->usage;
    174 		else
    175 			s = "";
    176 		format_add(ft, "command_list_usage", "%s", s);
    177 
    178 		line = format_expand(ft, template);
    179 		if (*line != '\0')
    180 			cmdq_print(item, "%s", line);
    181 		free(line);
    182 	}
    183 
    184 	format_free(ft);
    185 	return (CMD_RETURN_NORMAL);
    186 }
    187