cmd-list-keys.c revision 1.1.1.6 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.3 christos enum cmd_retval cmd_list_keys_exec(struct cmd *, struct cmd_q *);
31 1.1.1.5 christos
32 1.1.1.3 christos enum cmd_retval cmd_list_keys_table(struct cmd *, struct cmd_q *);
33 1.1.1.6 christos enum cmd_retval cmd_list_keys_commands(struct cmd_q *);
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.6 christos .args = { "t:T:", 0, 0 },
40 1.1.1.6 christos .usage = "[-t mode-table] [-T key-table]",
41 1.1.1.6 christos
42 1.1.1.6 christos .flags = CMD_STARTSERVER,
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.6 christos .args = { "", 0, 0 },
51 1.1.1.6 christos .usage = "",
52 1.1.1.6 christos
53 1.1.1.6 christos .flags = CMD_STARTSERVER,
54 1.1.1.6 christos .exec = cmd_list_keys_exec
55 1.1 jmmv };
56 1.1 jmmv
57 1.1.1.3 christos enum cmd_retval
58 1.1.1.3 christos cmd_list_keys_exec(struct cmd *self, struct cmd_q *cmdq)
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.5 christos const char *key, *tablename, *r;
64 1.1.1.6 christos char *cp, tmp[BUFSIZ];
65 1.1.1.5 christos int repeat, width, tablewidth, keywidth;
66 1.1.1.5 christos
67 1.1.1.5 christos if (self->entry == &cmd_list_commands_entry)
68 1.1.1.6 christos return (cmd_list_keys_commands(cmdq));
69 1.1 jmmv
70 1.1.1.2 jmmv if (args_has(args, 't'))
71 1.1.1.3 christos return (cmd_list_keys_table(self, cmdq));
72 1.1 jmmv
73 1.1.1.5 christos tablename = args_get(args, 'T');
74 1.1.1.5 christos if (tablename != NULL && key_bindings_get_table(tablename, 0) == NULL) {
75 1.1.1.5 christos cmdq_error(cmdq, "table %s doesn't exist", tablename);
76 1.1.1.5 christos return (CMD_RETURN_ERROR);
77 1.1.1.5 christos }
78 1.1.1.2 jmmv
79 1.1.1.5 christos repeat = 0;
80 1.1.1.5 christos tablewidth = keywidth = 0;
81 1.1.1.5 christos RB_FOREACH(table, key_tables, &key_tables) {
82 1.1.1.5 christos if (tablename != NULL && strcmp(table->name, tablename) != 0)
83 1.1 jmmv continue;
84 1.1.1.5 christos RB_FOREACH(bd, key_bindings, &table->key_bindings) {
85 1.1.1.5 christos key = key_string_lookup_key(bd->key);
86 1.1 jmmv
87 1.1.1.2 jmmv if (bd->can_repeat)
88 1.1.1.5 christos repeat = 1;
89 1.1.1.5 christos
90 1.1.1.6 christos width = utf8_cstrwidth(table->name);
91 1.1.1.5 christos if (width > tablewidth)
92 1.1.1.6 christos tablewidth = width;
93 1.1.1.6 christos width = utf8_cstrwidth(key);
94 1.1.1.5 christos if (width > keywidth)
95 1.1.1.5 christos keywidth = width;
96 1.1.1.5 christos }
97 1.1 jmmv }
98 1.1 jmmv
99 1.1.1.5 christos RB_FOREACH(table, key_tables, &key_tables) {
100 1.1.1.5 christos if (tablename != NULL && strcmp(table->name, tablename) != 0)
101 1.1 jmmv continue;
102 1.1.1.5 christos RB_FOREACH(bd, key_bindings, &table->key_bindings) {
103 1.1.1.5 christos key = key_string_lookup_key(bd->key);
104 1.1.1.5 christos
105 1.1.1.5 christos if (!repeat)
106 1.1.1.5 christos r = "";
107 1.1.1.5 christos else if (bd->can_repeat)
108 1.1.1.5 christos r = "-r ";
109 1.1.1.2 jmmv else
110 1.1.1.5 christos r = " ";
111 1.1.1.6 christos xsnprintf(tmp, sizeof tmp, "%s-T ", r);
112 1.1.1.6 christos
113 1.1.1.6 christos cp = utf8_padcstr(table->name, tablewidth);
114 1.1.1.6 christos strlcat(tmp, cp, sizeof tmp);
115 1.1.1.6 christos strlcat(tmp, " ", sizeof tmp);
116 1.1.1.6 christos free(cp);
117 1.1.1.6 christos
118 1.1.1.6 christos cp = utf8_padcstr(key, keywidth);
119 1.1.1.6 christos strlcat(tmp, cp, sizeof tmp);
120 1.1.1.6 christos strlcat(tmp, " ", sizeof tmp);
121 1.1.1.6 christos free(cp);
122 1.1.1.6 christos
123 1.1.1.6 christos cp = cmd_list_print(bd->cmdlist);
124 1.1.1.6 christos strlcat(tmp, cp, sizeof tmp);
125 1.1.1.6 christos free(cp);
126 1.1 jmmv
127 1.1.1.5 christos cmdq_print(cmdq, "bind-key %s", tmp);
128 1.1.1.5 christos }
129 1.1 jmmv }
130 1.1 jmmv
131 1.1.1.3 christos return (CMD_RETURN_NORMAL);
132 1.1 jmmv }
133 1.1 jmmv
134 1.1.1.3 christos enum cmd_retval
135 1.1.1.3 christos cmd_list_keys_table(struct cmd *self, struct cmd_q *cmdq)
136 1.1 jmmv {
137 1.1.1.2 jmmv struct args *args = self->args;
138 1.1.1.2 jmmv const char *tablename;
139 1.1 jmmv const struct mode_key_table *mtab;
140 1.1 jmmv struct mode_key_binding *mbind;
141 1.1 jmmv const char *key, *cmdstr, *mode;
142 1.1.1.2 jmmv int width, keywidth, any_mode;
143 1.1 jmmv
144 1.1.1.2 jmmv tablename = args_get(args, 't');
145 1.1.1.2 jmmv if ((mtab = mode_key_findtable(tablename)) == NULL) {
146 1.1.1.3 christos cmdq_error(cmdq, "unknown key table: %s", tablename);
147 1.1.1.3 christos return (CMD_RETURN_ERROR);
148 1.1 jmmv }
149 1.1 jmmv
150 1.1 jmmv width = 0;
151 1.1.1.2 jmmv any_mode = 0;
152 1.1.1.3 christos RB_FOREACH(mbind, mode_key_tree, mtab->tree) {
153 1.1 jmmv key = key_string_lookup_key(mbind->key);
154 1.1 jmmv
155 1.1.1.2 jmmv if (mbind->mode != 0)
156 1.1.1.2 jmmv any_mode = 1;
157 1.1.1.2 jmmv
158 1.1.1.2 jmmv keywidth = strlen(key);
159 1.1 jmmv if (keywidth > width)
160 1.1 jmmv width = keywidth;
161 1.1 jmmv }
162 1.1 jmmv
163 1.1.1.3 christos RB_FOREACH(mbind, mode_key_tree, mtab->tree) {
164 1.1 jmmv key = key_string_lookup_key(mbind->key);
165 1.1 jmmv
166 1.1 jmmv mode = "";
167 1.1 jmmv if (mbind->mode != 0)
168 1.1.1.2 jmmv mode = "c";
169 1.1 jmmv cmdstr = mode_key_tostring(mtab->cmdstr, mbind->cmd);
170 1.1.1.2 jmmv if (cmdstr != NULL) {
171 1.1.1.3 christos cmdq_print(cmdq, "bind-key -%st %s%s %*s %s%s%s%s",
172 1.1.1.2 jmmv mode, any_mode && *mode == '\0' ? " " : "",
173 1.1.1.3 christos mtab->name, (int) width, key, cmdstr,
174 1.1.1.3 christos mbind->arg != NULL ? " \"" : "",
175 1.1.1.3 christos mbind->arg != NULL ? mbind->arg : "",
176 1.1.1.3 christos mbind->arg != NULL ? "\"": "");
177 1.1.1.2 jmmv }
178 1.1 jmmv }
179 1.1 jmmv
180 1.1.1.3 christos return (CMD_RETURN_NORMAL);
181 1.1 jmmv }
182 1.1.1.5 christos
183 1.1.1.5 christos enum cmd_retval
184 1.1.1.6 christos cmd_list_keys_commands(struct cmd_q *cmdq)
185 1.1.1.5 christos {
186 1.1.1.5 christos const struct cmd_entry **entryp;
187 1.1.1.5 christos const struct cmd_entry *entry;
188 1.1.1.5 christos
189 1.1.1.5 christos for (entryp = cmd_table; *entryp != NULL; entryp++) {
190 1.1.1.5 christos entry = *entryp;
191 1.1.1.5 christos if (entry->alias == NULL) {
192 1.1.1.5 christos cmdq_print(cmdq, "%s %s", entry->name, entry->usage);
193 1.1.1.5 christos continue;
194 1.1.1.5 christos }
195 1.1.1.5 christos cmdq_print(cmdq, "%s (%s) %s", entry->name, entry->alias,
196 1.1.1.5 christos entry->usage);
197 1.1.1.5 christos }
198 1.1.1.5 christos
199 1.1.1.5 christos return (CMD_RETURN_NORMAL);
200 1.1.1.5 christos }
201