1 /* $OpenBSD: cmd-list-keys.c,v 1.31 2015/11/27 15:06:43 nicm Exp $ */ 2 3 /* 4 * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net> 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 enum cmd_retval cmd_list_keys_exec(struct cmd *, struct cmd_q *); 31 32 enum cmd_retval cmd_list_keys_table(struct cmd *, struct cmd_q *); 33 enum cmd_retval cmd_list_keys_commands(struct cmd_q *); 34 35 const struct cmd_entry cmd_list_keys_entry = { 36 "list-keys", "lsk", 37 "t:T:", 0, 0, 38 "[-t mode-table] [-T key-table]", 39 0, 40 cmd_list_keys_exec 41 }; 42 43 const struct cmd_entry cmd_list_commands_entry = { 44 "list-commands", "lscm", 45 "", 0, 0, 46 "", 47 0, 48 cmd_list_keys_exec 49 }; 50 51 enum cmd_retval 52 cmd_list_keys_exec(struct cmd *self, struct cmd_q *cmdq) 53 { 54 struct args *args = self->args; 55 struct key_table *table; 56 struct key_binding *bd; 57 const char *key, *tablename, *r; 58 char *cp, tmp[BUFSIZ]; 59 int repeat, width, tablewidth, keywidth; 60 61 if (self->entry == &cmd_list_commands_entry) 62 return (cmd_list_keys_commands(cmdq)); 63 64 if (args_has(args, 't')) 65 return (cmd_list_keys_table(self, cmdq)); 66 67 tablename = args_get(args, 'T'); 68 if (tablename != NULL && key_bindings_get_table(tablename, 0) == NULL) { 69 cmdq_error(cmdq, "table %s doesn't exist", tablename); 70 return (CMD_RETURN_ERROR); 71 } 72 73 repeat = 0; 74 tablewidth = keywidth = 0; 75 RB_FOREACH(table, key_tables, &key_tables) { 76 if (tablename != NULL && strcmp(table->name, tablename) != 0) 77 continue; 78 RB_FOREACH(bd, key_bindings, &table->key_bindings) { 79 key = key_string_lookup_key(bd->key); 80 81 if (bd->can_repeat) 82 repeat = 1; 83 84 width = utf8_cstrwidth(table->name); 85 if (width > tablewidth) 86 tablewidth = width; 87 width = utf8_cstrwidth(key); 88 if (width > keywidth) 89 keywidth = width; 90 } 91 } 92 93 RB_FOREACH(table, key_tables, &key_tables) { 94 if (tablename != NULL && strcmp(table->name, tablename) != 0) 95 continue; 96 RB_FOREACH(bd, key_bindings, &table->key_bindings) { 97 key = key_string_lookup_key(bd->key); 98 99 if (!repeat) 100 r = ""; 101 else if (bd->can_repeat) 102 r = "-r "; 103 else 104 r = " "; 105 xsnprintf(tmp, sizeof tmp, "%s-T ", r); 106 107 cp = utf8_padcstr(table->name, tablewidth); 108 strlcat(tmp, cp, sizeof tmp); 109 strlcat(tmp, " ", sizeof tmp); 110 free(cp); 111 112 cp = utf8_padcstr(key, keywidth); 113 strlcat(tmp, cp, sizeof tmp); 114 strlcat(tmp, " ", sizeof tmp); 115 free(cp); 116 117 cp = cmd_list_print(bd->cmdlist); 118 strlcat(tmp, cp, sizeof tmp); 119 free(cp); 120 121 cmdq_print(cmdq, "bind-key %s", tmp); 122 } 123 } 124 125 return (CMD_RETURN_NORMAL); 126 } 127 128 enum cmd_retval 129 cmd_list_keys_table(struct cmd *self, struct cmd_q *cmdq) 130 { 131 struct args *args = self->args; 132 const char *tablename; 133 const struct mode_key_table *mtab; 134 struct mode_key_binding *mbind; 135 const char *key, *cmdstr, *mode; 136 int width, keywidth, any_mode; 137 138 tablename = args_get(args, 't'); 139 if ((mtab = mode_key_findtable(tablename)) == NULL) { 140 cmdq_error(cmdq, "unknown key table: %s", tablename); 141 return (CMD_RETURN_ERROR); 142 } 143 144 width = 0; 145 any_mode = 0; 146 RB_FOREACH(mbind, mode_key_tree, mtab->tree) { 147 key = key_string_lookup_key(mbind->key); 148 149 if (mbind->mode != 0) 150 any_mode = 1; 151 152 keywidth = strlen(key); 153 if (keywidth > width) 154 width = keywidth; 155 } 156 157 RB_FOREACH(mbind, mode_key_tree, mtab->tree) { 158 key = key_string_lookup_key(mbind->key); 159 160 mode = ""; 161 if (mbind->mode != 0) 162 mode = "c"; 163 cmdstr = mode_key_tostring(mtab->cmdstr, mbind->cmd); 164 if (cmdstr != NULL) { 165 cmdq_print(cmdq, "bind-key -%st %s%s %*s %s%s%s%s", 166 mode, any_mode && *mode == '\0' ? " " : "", 167 mtab->name, (int) width, key, cmdstr, 168 mbind->arg != NULL ? " \"" : "", 169 mbind->arg != NULL ? mbind->arg : "", 170 mbind->arg != NULL ? "\"": ""); 171 } 172 } 173 174 return (CMD_RETURN_NORMAL); 175 } 176 177 enum cmd_retval 178 cmd_list_keys_commands(struct cmd_q *cmdq) 179 { 180 const struct cmd_entry **entryp; 181 const struct cmd_entry *entry; 182 183 for (entryp = cmd_table; *entryp != NULL; entryp++) { 184 entry = *entryp; 185 if (entry->alias == NULL) { 186 cmdq_print(cmdq, "%s %s", entry->name, entry->usage); 187 continue; 188 } 189 cmdq_print(cmdq, "%s (%s) %s", entry->name, entry->alias, 190 entry->usage); 191 } 192 193 return (CMD_RETURN_NORMAL); 194 } 195