xref: /openbsd-src/usr.bin/tmux/cmd-list-keys.c (revision 43003dfe3ad45d1698bed8a37f2b0f5b14f20d4f)
1 /* $OpenBSD: cmd-list-keys.c,v 1.7 2009/07/28 17:05:10 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 <string.h>
22 
23 #include "tmux.h"
24 
25 /*
26  * List key bindings.
27  */
28 
29 int	cmd_list_keys_exec(struct cmd *, struct cmd_ctx *);
30 
31 int	cmd_list_keys_table(struct cmd *, struct cmd_ctx *);
32 
33 const struct cmd_entry cmd_list_keys_entry = {
34 	"list-keys", "lsk",
35 	"[-t key-table]",
36 	0, 0,
37 	cmd_target_init,
38 	cmd_target_parse,
39 	cmd_list_keys_exec,
40 	cmd_target_free,
41 	cmd_target_print
42 };
43 
44 int
45 cmd_list_keys_exec(struct cmd *self, struct cmd_ctx *ctx)
46 {
47 	struct cmd_target_data	*data = self->data;
48 	struct key_binding	*bd;
49 	const char		*key;
50 	char			 tmp[BUFSIZ], keytmp[64];
51 	int			 width, keywidth;
52 
53 	if (data->target != NULL)
54 		return (cmd_list_keys_table(self, ctx));
55 
56 	width = 0;
57 	SPLAY_FOREACH(bd, key_bindings, &key_bindings) {
58 		key = key_string_lookup_key(bd->key & ~KEYC_PREFIX);
59 		if (key == NULL)
60 			continue;
61 
62 		keywidth = strlen(key) + 1;
63 		if (!(bd->key & KEYC_PREFIX))
64 			keywidth += 2;
65 		if (keywidth > width)
66 			width = keywidth;
67 	}
68 
69 	SPLAY_FOREACH(bd, key_bindings, &key_bindings) {
70 		key = key_string_lookup_key(bd->key & ~KEYC_PREFIX);
71 		if (key == NULL)
72 			continue;
73 
74 		*tmp = '\0';
75 		cmd_list_print(bd->cmdlist, tmp, sizeof tmp);
76 		if (!(bd->key & KEYC_PREFIX)) {
77 			xsnprintf(keytmp, sizeof keytmp, "[%s]", key);
78 			key = keytmp;
79 		}
80 		ctx->print(ctx, "%*s: %s", width, key, tmp);
81 	}
82 
83 	return (0);
84 }
85 
86 int
87 cmd_list_keys_table(struct cmd *self, struct cmd_ctx *ctx)
88 {
89 	struct cmd_target_data		*data = self->data;
90 	const struct mode_key_table	*mtab;
91 	struct mode_key_binding		*mbind;
92 	const char			*key, *cmdstr, *mode;
93 	int			 	 width, keywidth;
94 
95 	if ((mtab = mode_key_findtable(data->target)) == NULL) {
96 		ctx->error(ctx, "unknown key table: %s", data->target);
97 		return (-1);
98 	}
99 
100 	width = 0;
101 	SPLAY_FOREACH(mbind, mode_key_tree, mtab->tree) {
102 		key = key_string_lookup_key(mbind->key);
103 		if (key == NULL)
104 			continue;
105 
106 		keywidth = strlen(key) + 1;
107 		if (keywidth > width)
108 			width = keywidth;
109 	}
110 
111 	SPLAY_FOREACH(mbind, mode_key_tree, mtab->tree) {
112 		key = key_string_lookup_key(mbind->key);
113 		if (key == NULL)
114 			continue;
115 
116 		mode = "";
117 		if (mbind->mode != 0)
118 			mode = "(command mode) ";
119 		cmdstr = mode_key_tostring(mtab->cmdstr, mbind->cmd);
120 		if (cmdstr != NULL)
121 			ctx->print(ctx, "%*s: %s%s", width, key, mode, cmdstr);
122 	}
123 
124 	return (0);
125 }
126