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