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.16 wiz 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.16 wiz status_message_set(tc, -1, 1, 0, 0, "%s%s%s", prefix, 118 1.1.1.16 wiz tmp, 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.15 wiz struct client *tc = cmdq_get_target_client(item); 152 1.1.1.5 christos struct key_table *table; 153 1.1 jmmv struct key_binding *bd; 154 1.1.1.14 wiz const char *tablename, *r, *keystr; 155 1.1.1.11 christos char *key, *cp, *tmp, *start, *empty; 156 1.1.1.11 christos key_code prefix, only = KEYC_UNKNOWN; 157 1.1.1.11 christos int repeat, width, tablewidth, keywidth, found = 0; 158 1.1.1.10 christos size_t tmpsize, tmpused, cplen; 159 1.1.1.5 christos 160 1.1.1.12 christos if (cmd_get_entry(self) == &cmd_list_commands_entry) 161 1.1.1.7 christos return (cmd_list_keys_commands(self, item)); 162 1.1 jmmv 163 1.1.1.14 wiz if ((keystr = args_string(args, 0)) != NULL) { 164 1.1.1.14 wiz only = key_string_lookup_string(keystr); 165 1.1.1.11 christos if (only == KEYC_UNKNOWN) { 166 1.1.1.14 wiz cmdq_error(item, "invalid key: %s", keystr); 167 1.1.1.11 christos return (CMD_RETURN_ERROR); 168 1.1.1.11 christos } 169 1.1.1.13 christos only &= (KEYC_MASK_KEY|KEYC_MASK_MODIFIERS); 170 1.1.1.11 christos } 171 1.1.1.11 christos 172 1.1.1.5 christos tablename = args_get(args, 'T'); 173 1.1.1.5 christos if (tablename != NULL && key_bindings_get_table(tablename, 0) == NULL) { 174 1.1.1.7 christos cmdq_error(item, "table %s doesn't exist", tablename); 175 1.1.1.5 christos return (CMD_RETURN_ERROR); 176 1.1.1.5 christos } 177 1.1.1.2 jmmv 178 1.1.1.11 christos if (args_has(args, 'N')) { 179 1.1.1.11 christos if (tablename == NULL) { 180 1.1.1.11 christos start = cmd_list_keys_get_prefix(args, &prefix); 181 1.1.1.11 christos keywidth = cmd_list_keys_get_width("root", only); 182 1.1.1.11 christos if (prefix != KEYC_NONE) { 183 1.1.1.11 christos width = cmd_list_keys_get_width("prefix", only); 184 1.1.1.11 christos if (width == 0) 185 1.1.1.11 christos prefix = KEYC_NONE; 186 1.1.1.11 christos else if (width > keywidth) 187 1.1.1.11 christos keywidth = width; 188 1.1.1.11 christos } 189 1.1.1.11 christos empty = utf8_padcstr("", utf8_cstrwidth(start)); 190 1.1.1.11 christos 191 1.1.1.11 christos found = cmd_list_keys_print_notes(item, args, "root", 192 1.1.1.11 christos keywidth, only, empty); 193 1.1.1.11 christos if (prefix != KEYC_NONE) { 194 1.1.1.11 christos if (cmd_list_keys_print_notes(item, args, 195 1.1.1.11 christos "prefix", keywidth, only, start)) 196 1.1.1.11 christos found = 1; 197 1.1.1.11 christos } 198 1.1.1.11 christos free(empty); 199 1.1.1.11 christos } else { 200 1.1.1.11 christos if (args_has(args, 'P')) 201 1.1.1.11 christos start = xstrdup(args_get(args, 'P')); 202 1.1.1.11 christos else 203 1.1.1.11 christos start = xstrdup(""); 204 1.1.1.11 christos keywidth = cmd_list_keys_get_width(tablename, only); 205 1.1.1.11 christos found = cmd_list_keys_print_notes(item, args, tablename, 206 1.1.1.11 christos keywidth, only, start); 207 1.1.1.11 christos 208 1.1.1.11 christos } 209 1.1.1.11 christos free(start); 210 1.1.1.11 christos goto out; 211 1.1.1.11 christos } 212 1.1.1.11 christos 213 1.1.1.5 christos repeat = 0; 214 1.1.1.5 christos tablewidth = keywidth = 0; 215 1.1.1.14 wiz table = key_bindings_first_table(); 216 1.1.1.9 christos while (table != NULL) { 217 1.1.1.9 christos if (tablename != NULL && strcmp(table->name, tablename) != 0) { 218 1.1.1.9 christos table = key_bindings_next_table(table); 219 1.1 jmmv continue; 220 1.1.1.9 christos } 221 1.1.1.9 christos bd = key_bindings_first(table); 222 1.1.1.9 christos while (bd != NULL) { 223 1.1.1.11 christos if (only != KEYC_UNKNOWN && bd->key != only) { 224 1.1.1.11 christos bd = key_bindings_next(table, bd); 225 1.1.1.11 christos continue; 226 1.1.1.11 christos } 227 1.1.1.12 christos key = args_escape(key_string_lookup_key(bd->key, 0)); 228 1.1 jmmv 229 1.1.1.8 christos if (bd->flags & KEY_BINDING_REPEAT) 230 1.1.1.5 christos repeat = 1; 231 1.1.1.5 christos 232 1.1.1.6 christos width = utf8_cstrwidth(table->name); 233 1.1.1.5 christos if (width > tablewidth) 234 1.1.1.6 christos tablewidth = width; 235 1.1.1.6 christos width = utf8_cstrwidth(key); 236 1.1.1.5 christos if (width > keywidth) 237 1.1.1.5 christos keywidth = width; 238 1.1.1.9 christos 239 1.1.1.10 christos free(key); 240 1.1.1.9 christos bd = key_bindings_next(table, bd); 241 1.1.1.5 christos } 242 1.1.1.9 christos table = key_bindings_next_table(table); 243 1.1 jmmv } 244 1.1 jmmv 245 1.1.1.10 christos tmpsize = 256; 246 1.1.1.10 christos tmp = xmalloc(tmpsize); 247 1.1.1.10 christos 248 1.1.1.14 wiz table = key_bindings_first_table(); 249 1.1.1.9 christos while (table != NULL) { 250 1.1.1.9 christos if (tablename != NULL && strcmp(table->name, tablename) != 0) { 251 1.1.1.9 christos table = key_bindings_next_table(table); 252 1.1 jmmv continue; 253 1.1.1.9 christos } 254 1.1.1.9 christos bd = key_bindings_first(table); 255 1.1.1.9 christos while (bd != NULL) { 256 1.1.1.11 christos if (only != KEYC_UNKNOWN && bd->key != only) { 257 1.1.1.11 christos bd = key_bindings_next(table, bd); 258 1.1.1.11 christos continue; 259 1.1.1.11 christos } 260 1.1.1.11 christos found = 1; 261 1.1.1.12 christos key = args_escape(key_string_lookup_key(bd->key, 0)); 262 1.1.1.5 christos 263 1.1.1.5 christos if (!repeat) 264 1.1.1.5 christos r = ""; 265 1.1.1.8 christos else if (bd->flags & KEY_BINDING_REPEAT) 266 1.1.1.5 christos r = "-r "; 267 1.1.1.2 jmmv else 268 1.1.1.5 christos r = " "; 269 1.1.1.10 christos tmpused = xsnprintf(tmp, tmpsize, "%s-T ", r); 270 1.1.1.6 christos 271 1.1.1.6 christos cp = utf8_padcstr(table->name, tablewidth); 272 1.1.1.10 christos cplen = strlen(cp) + 1; 273 1.1.1.10 christos while (tmpused + cplen + 1 >= tmpsize) { 274 1.1.1.10 christos tmpsize *= 2; 275 1.1.1.10 christos tmp = xrealloc(tmp, tmpsize); 276 1.1.1.10 christos } 277 1.1.1.12 christos strlcat(tmp, cp, tmpsize); 278 1.1.1.10 christos tmpused = strlcat(tmp, " ", tmpsize); 279 1.1.1.6 christos free(cp); 280 1.1.1.6 christos 281 1.1.1.6 christos cp = utf8_padcstr(key, keywidth); 282 1.1.1.10 christos cplen = strlen(cp) + 1; 283 1.1.1.10 christos while (tmpused + cplen + 1 >= tmpsize) { 284 1.1.1.10 christos tmpsize *= 2; 285 1.1.1.10 christos tmp = xrealloc(tmp, tmpsize); 286 1.1.1.10 christos } 287 1.1.1.12 christos strlcat(tmp, cp, tmpsize); 288 1.1.1.10 christos tmpused = strlcat(tmp, " ", tmpsize); 289 1.1.1.6 christos free(cp); 290 1.1.1.6 christos 291 1.1.1.10 christos cp = cmd_list_print(bd->cmdlist, 1); 292 1.1.1.10 christos cplen = strlen(cp); 293 1.1.1.10 christos while (tmpused + cplen + 1 >= tmpsize) { 294 1.1.1.10 christos tmpsize *= 2; 295 1.1.1.10 christos tmp = xrealloc(tmp, tmpsize); 296 1.1.1.10 christos } 297 1.1.1.10 christos strlcat(tmp, cp, tmpsize); 298 1.1.1.6 christos free(cp); 299 1.1 jmmv 300 1.1.1.15 wiz if (args_has(args, '1') && tc != NULL) { 301 1.1.1.16 wiz status_message_set(tc, -1, 1, 0, 0, 302 1.1.1.16 wiz "bind-key %s", tmp); 303 1.1.1.15 wiz } else 304 1.1.1.15 wiz cmdq_print(item, "bind-key %s", tmp); 305 1.1.1.10 christos free(key); 306 1.1.1.15 wiz 307 1.1.1.15 wiz if (args_has(args, '1')) 308 1.1.1.15 wiz break; 309 1.1.1.9 christos bd = key_bindings_next(table, bd); 310 1.1.1.2 jmmv } 311 1.1.1.9 christos table = key_bindings_next_table(table); 312 1.1 jmmv } 313 1.1 jmmv 314 1.1.1.10 christos free(tmp); 315 1.1.1.10 christos 316 1.1.1.11 christos out: 317 1.1.1.11 christos if (only != KEYC_UNKNOWN && !found) { 318 1.1.1.14 wiz cmdq_error(item, "unknown key: %s", args_string(args, 0)); 319 1.1.1.11 christos return (CMD_RETURN_ERROR); 320 1.1.1.11 christos } 321 1.1.1.3 christos return (CMD_RETURN_NORMAL); 322 1.1 jmmv } 323 1.1.1.5 christos 324 1.1.1.16 wiz static void 325 1.1.1.16 wiz cmd_list_single_command(const struct cmd_entry *entry, struct format_tree *ft, 326 1.1.1.16 wiz const char *template, struct cmdq_item *item) 327 1.1.1.16 wiz { 328 1.1.1.16 wiz const char *s; 329 1.1.1.16 wiz char *line; 330 1.1.1.16 wiz 331 1.1.1.16 wiz format_add(ft, "command_list_name", "%s", entry->name); 332 1.1.1.16 wiz if (entry->alias != NULL) 333 1.1.1.16 wiz s = entry->alias; 334 1.1.1.16 wiz else 335 1.1.1.16 wiz s = ""; 336 1.1.1.16 wiz format_add(ft, "command_list_alias", "%s", s); 337 1.1.1.16 wiz if (entry->usage != NULL) 338 1.1.1.16 wiz s = entry->usage; 339 1.1.1.16 wiz else 340 1.1.1.16 wiz s = ""; 341 1.1.1.16 wiz format_add(ft, "command_list_usage", "%s", s); 342 1.1.1.16 wiz 343 1.1.1.16 wiz line = format_expand(ft, template); 344 1.1.1.16 wiz if (*line != '\0') 345 1.1.1.16 wiz cmdq_print(item, "%s", line); 346 1.1.1.16 wiz free(line); 347 1.1.1.16 wiz } 348 1.1.1.16 wiz 349 1.1.1.7 christos static enum cmd_retval 350 1.1.1.7 christos cmd_list_keys_commands(struct cmd *self, struct cmdq_item *item) 351 1.1.1.5 christos { 352 1.1.1.12 christos struct args *args = cmd_get_args(self); 353 1.1.1.5 christos const struct cmd_entry **entryp; 354 1.1.1.5 christos const struct cmd_entry *entry; 355 1.1.1.7 christos struct format_tree *ft; 356 1.1.1.16 wiz const char *template, *command; 357 1.1.1.16 wiz char *cause; 358 1.1.1.7 christos 359 1.1.1.7 christos if ((template = args_get(args, 'F')) == NULL) { 360 1.1.1.7 christos template = "#{command_list_name}" 361 1.1.1.7 christos "#{?command_list_alias, (#{command_list_alias}),} " 362 1.1.1.7 christos "#{command_list_usage}"; 363 1.1.1.7 christos } 364 1.1.1.7 christos 365 1.1.1.12 christos ft = format_create(cmdq_get_client(item), item, FORMAT_NONE, 0); 366 1.1.1.7 christos format_defaults(ft, NULL, NULL, NULL, NULL); 367 1.1.1.5 christos 368 1.1.1.14 wiz command = args_string(args, 0); 369 1.1.1.16 wiz if (command == NULL) { 370 1.1.1.16 wiz for (entryp = cmd_table; *entryp != NULL; entryp++) 371 1.1.1.16 wiz cmd_list_single_command(*entryp, ft, template, item); 372 1.1.1.16 wiz } else { 373 1.1.1.16 wiz entry = cmd_find(command, &cause); 374 1.1.1.16 wiz if (entry != NULL) 375 1.1.1.16 wiz cmd_list_single_command(entry, ft, template, item); 376 1.1.1.16 wiz else { 377 1.1.1.16 wiz cmdq_error(item, "%s", cause); 378 1.1.1.16 wiz free(cause); 379 1.1.1.16 wiz format_free(ft); 380 1.1.1.16 wiz return (CMD_RETURN_ERROR); 381 1.1.1.16 wiz } 382 1.1.1.5 christos } 383 1.1.1.5 christos 384 1.1.1.7 christos format_free(ft); 385 1.1.1.5 christos return (CMD_RETURN_NORMAL); 386 1.1.1.5 christos } 387