1 /* $OpenBSD: cmd-list-keys.c,v 1.46 2019/05/23 14:03:44 nicm Exp $ */ 2 3 /* 4 * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@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 *tablename, *r; 64 char *key, *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 = args_escape(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 free(key); 99 bd = key_bindings_next(table, bd); 100 } 101 table = key_bindings_next_table(table); 102 } 103 104 table = key_bindings_first_table (); 105 while (table != NULL) { 106 if (tablename != NULL && strcmp(table->name, tablename) != 0) { 107 table = key_bindings_next_table(table); 108 continue; 109 } 110 bd = key_bindings_first(table); 111 while (bd != NULL) { 112 key = args_escape(key_string_lookup_key(bd->key)); 113 114 if (!repeat) 115 r = ""; 116 else if (bd->flags & KEY_BINDING_REPEAT) 117 r = "-r "; 118 else 119 r = " "; 120 xsnprintf(tmp, sizeof tmp, "%s-T ", r); 121 122 cp = utf8_padcstr(table->name, tablewidth); 123 strlcat(tmp, cp, sizeof tmp); 124 strlcat(tmp, " ", sizeof tmp); 125 free(cp); 126 127 cp = utf8_padcstr(key, keywidth); 128 strlcat(tmp, cp, sizeof tmp); 129 strlcat(tmp, " ", sizeof tmp); 130 free(cp); 131 132 cp = cmd_list_print(bd->cmdlist, 1); 133 strlcat(tmp, cp, sizeof tmp); 134 free(cp); 135 136 cmdq_print(item, "bind-key %s", tmp); 137 138 free(key); 139 bd = key_bindings_next(table, bd); 140 } 141 table = key_bindings_next_table(table); 142 } 143 144 return (CMD_RETURN_NORMAL); 145 } 146 147 static enum cmd_retval 148 cmd_list_keys_commands(struct cmd *self, struct cmdq_item *item) 149 { 150 struct args *args = self->args; 151 const struct cmd_entry **entryp; 152 const struct cmd_entry *entry; 153 struct format_tree *ft; 154 const char *template, *s; 155 char *line; 156 157 if ((template = args_get(args, 'F')) == NULL) { 158 template = "#{command_list_name}" 159 "#{?command_list_alias, (#{command_list_alias}),} " 160 "#{command_list_usage}"; 161 } 162 163 ft = format_create(item->client, item, FORMAT_NONE, 0); 164 format_defaults(ft, NULL, NULL, NULL, NULL); 165 166 for (entryp = cmd_table; *entryp != NULL; entryp++) { 167 entry = *entryp; 168 169 format_add(ft, "command_list_name", "%s", entry->name); 170 if (entry->alias != NULL) 171 s = entry->alias; 172 else 173 s = ""; 174 format_add(ft, "command_list_alias", "%s", s); 175 if (entry->usage != NULL) 176 s = entry->usage; 177 else 178 s = ""; 179 format_add(ft, "command_list_usage", "%s", s); 180 181 line = format_expand(ft, template); 182 if (*line != '\0') 183 cmdq_print(item, "%s", line); 184 free(line); 185 } 186 187 format_free(ft); 188 return (CMD_RETURN_NORMAL); 189 } 190