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