cmd-bind-key.c revision 1.14
1/* $OpenBSD: cmd-bind-key.c,v 1.14 2012/07/11 07:10:15 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 * Bind a key to a command, this recurses through cmd_*.
28 */
29
30enum cmd_retval	 cmd_bind_key_check(struct args *);
31enum cmd_retval	 cmd_bind_key_exec(struct cmd *, struct cmd_ctx *);
32
33enum cmd_retval	 cmd_bind_key_table(struct cmd *, struct cmd_ctx *, int);
34
35const struct cmd_entry cmd_bind_key_entry = {
36	"bind-key", "bind",
37	"cnrt:", 1, -1,
38	"[-cnr] [-t key-table] key command [arguments]",
39	0,
40	NULL,
41	cmd_bind_key_check,
42	cmd_bind_key_exec
43};
44
45enum cmd_retval
46cmd_bind_key_check(struct args *args)
47{
48	if (args_has(args, 't')) {
49		if (args->argc != 2)
50			return (CMD_RETURN_ERROR);
51	} else {
52		if (args->argc < 2)
53			return (CMD_RETURN_ERROR);
54	}
55	return (CMD_RETURN_NORMAL);
56}
57
58enum cmd_retval
59cmd_bind_key_exec(struct cmd *self, struct cmd_ctx *ctx)
60{
61	struct args	*args = self->args;
62	char		*cause;
63	struct cmd_list	*cmdlist;
64	int		 key;
65
66	key = key_string_lookup_string(args->argv[0]);
67	if (key == KEYC_NONE) {
68		ctx->error(ctx, "unknown key: %s", args->argv[0]);
69		return (CMD_RETURN_ERROR);
70	}
71
72	if (args_has(args, 't'))
73		return (cmd_bind_key_table(self, ctx, key));
74
75	cmdlist = cmd_list_parse(args->argc - 1, args->argv + 1, &cause);
76	if (cmdlist == NULL) {
77		ctx->error(ctx, "%s", cause);
78		free(cause);
79		return (CMD_RETURN_ERROR);
80	}
81
82	if (!args_has(args, 'n'))
83	    key |= KEYC_PREFIX;
84	key_bindings_add(key, args_has(args, 'r'), cmdlist);
85	return (CMD_RETURN_NORMAL);
86}
87
88enum cmd_retval
89cmd_bind_key_table(struct cmd *self, struct cmd_ctx *ctx, int key)
90{
91	struct args			*args = self->args;
92	const char			*tablename;
93	const struct mode_key_table	*mtab;
94	struct mode_key_binding		*mbind, mtmp;
95	enum mode_key_cmd		 cmd;
96
97	tablename = args_get(args, 't');
98	if ((mtab = mode_key_findtable(tablename)) == NULL) {
99		ctx->error(ctx, "unknown key table: %s", tablename);
100		return (CMD_RETURN_ERROR);
101	}
102
103	cmd = mode_key_fromstring(mtab->cmdstr, args->argv[1]);
104	if (cmd == MODEKEY_NONE) {
105		ctx->error(ctx, "unknown command: %s", args->argv[1]);
106		return (CMD_RETURN_ERROR);
107	}
108
109	mtmp.key = key;
110	mtmp.mode = !!args_has(args, 'c');
111	if ((mbind = RB_FIND(mode_key_tree, mtab->tree, &mtmp)) != NULL) {
112		mbind->cmd = cmd;
113		return (CMD_RETURN_NORMAL);
114	}
115	mbind = xmalloc(sizeof *mbind);
116	mbind->key = mtmp.key;
117	mbind->mode = mtmp.mode;
118	mbind->cmd = cmd;
119	RB_INSERT(mode_key_tree, mtab->tree, mbind);
120	return (CMD_RETURN_NORMAL);
121}
122