Annotation of src/usr.bin/tmux/cmd-bind-key.c, Revision 1.26
1.26 ! nicm 1: /* $OpenBSD: cmd-bind-key.c,v 1.25 2016/01/19 15:59:12 nicm Exp $ */
1.1 nicm 2:
3: /*
1.25 nicm 4: * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
1.1 nicm 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:
1.13 nicm 21: #include <stdlib.h>
1.5 nicm 22: #include <string.h>
23:
1.1 nicm 24: #include "tmux.h"
25:
26: /*
27: * Bind a key to a command, this recurses through cmd_*.
28: */
29:
1.16 nicm 30: enum cmd_retval cmd_bind_key_exec(struct cmd *, struct cmd_q *);
1.1 nicm 31:
1.22 nicm 32: enum cmd_retval cmd_bind_key_mode_table(struct cmd *, struct cmd_q *,
33: key_code);
1.1 nicm 34:
35: const struct cmd_entry cmd_bind_key_entry = {
1.24 nicm 36: .name = "bind-key",
37: .alias = "bind",
38:
1.26 ! nicm 39: .args = { "cnrR:t:T:", 1, -1 },
! 40: .usage = "[-cnr] [-t mode-table] [-R repeat-count] [-T key-table] key "
! 41: "command [arguments]",
1.24 nicm 42:
43: .flags = 0,
44: .exec = cmd_bind_key_exec
1.1 nicm 45: };
46:
1.14 nicm 47: enum cmd_retval
1.17 nicm 48: cmd_bind_key_exec(struct cmd *self, struct cmd_q *cmdq)
1.1 nicm 49: {
1.17 nicm 50: struct args *args = self->args;
51: char *cause;
52: struct cmd_list *cmdlist;
1.22 nicm 53: key_code key;
1.21 nicm 54: const char *tablename;
1.17 nicm 55:
1.10 nicm 56: if (args_has(args, 't')) {
1.17 nicm 57: if (args->argc != 2 && args->argc != 3) {
58: cmdq_error(cmdq, "not enough arguments");
1.14 nicm 59: return (CMD_RETURN_ERROR);
1.17 nicm 60: }
1.5 nicm 61: } else {
1.17 nicm 62: if (args->argc < 2) {
63: cmdq_error(cmdq, "not enough arguments");
1.14 nicm 64: return (CMD_RETURN_ERROR);
1.17 nicm 65: }
1.5 nicm 66: }
1.10 nicm 67:
68: key = key_string_lookup_string(args->argv[0]);
1.23 nicm 69: if (key == KEYC_NONE || key == KEYC_UNKNOWN) {
1.16 nicm 70: cmdq_error(cmdq, "unknown key: %s", args->argv[0]);
1.14 nicm 71: return (CMD_RETURN_ERROR);
1.10 nicm 72: }
1.1 nicm 73:
1.10 nicm 74: if (args_has(args, 't'))
1.18 nicm 75: return (cmd_bind_key_mode_table(self, cmdq, key));
1.1 nicm 76:
1.21 nicm 77: if (args_has(args, 'T'))
78: tablename = args_get(args, 'T');
79: else if (args_has(args, 'n'))
80: tablename = "root";
81: else
82: tablename = "prefix";
83:
1.16 nicm 84: cmdlist = cmd_list_parse(args->argc - 1, args->argv + 1, NULL, 0,
85: &cause);
1.10 nicm 86: if (cmdlist == NULL) {
1.16 nicm 87: cmdq_error(cmdq, "%s", cause);
1.13 nicm 88: free(cause);
1.14 nicm 89: return (CMD_RETURN_ERROR);
1.10 nicm 90: }
1.1 nicm 91:
1.21 nicm 92: key_bindings_add(tablename, key, args_has(args, 'r'), cmdlist);
1.14 nicm 93: return (CMD_RETURN_NORMAL);
1.1 nicm 94: }
95:
1.14 nicm 96: enum cmd_retval
1.22 nicm 97: cmd_bind_key_mode_table(struct cmd *self, struct cmd_q *cmdq, key_code key)
1.5 nicm 98: {
1.10 nicm 99: struct args *args = self->args;
1.26 ! nicm 100: const char *tablename, *arg;
1.5 nicm 101: const struct mode_key_table *mtab;
102: struct mode_key_binding *mbind, mtmp;
103: enum mode_key_cmd cmd;
1.26 ! nicm 104: char *cause;
! 105: u_int repeat;
1.5 nicm 106:
1.10 nicm 107: tablename = args_get(args, 't');
108: if ((mtab = mode_key_findtable(tablename)) == NULL) {
1.16 nicm 109: cmdq_error(cmdq, "unknown key table: %s", tablename);
1.14 nicm 110: return (CMD_RETURN_ERROR);
1.5 nicm 111: }
112:
1.11 nicm 113: cmd = mode_key_fromstring(mtab->cmdstr, args->argv[1]);
1.5 nicm 114: if (cmd == MODEKEY_NONE) {
1.16 nicm 115: cmdq_error(cmdq, "unknown command: %s", args->argv[1]);
1.14 nicm 116: return (CMD_RETURN_ERROR);
1.5 nicm 117: }
1.7 nicm 118:
1.20 nicm 119: switch (cmd) {
120: case MODEKEYCOPY_APPENDSELECTION:
121: case MODEKEYCOPY_COPYSELECTION:
122: case MODEKEYCOPY_STARTNAMEDBUFFER:
123: if (args->argc == 2)
124: arg = NULL;
125: else {
126: arg = args->argv[2];
127: if (strcmp(arg, "-x") != 0) {
128: cmdq_error(cmdq, "unknown argument");
129: return (CMD_RETURN_ERROR);
130: }
1.15 nicm 131: }
1.20 nicm 132: break;
133: case MODEKEYCOPY_COPYPIPE:
1.15 nicm 134: if (args->argc != 3) {
1.16 nicm 135: cmdq_error(cmdq, "no argument given");
1.15 nicm 136: return (CMD_RETURN_ERROR);
137: }
138: arg = args->argv[2];
1.20 nicm 139: break;
140: default:
141: if (args->argc != 2) {
142: cmdq_error(cmdq, "no argument allowed");
143: return (CMD_RETURN_ERROR);
144: }
145: arg = NULL;
146: break;
1.15 nicm 147: }
148:
1.26 ! nicm 149: repeat = 1;
! 150: if (args_has(args, 'R')) {
! 151: repeat = args_strtonum(args, 'R', 1, SHRT_MAX, &cause);
! 152: if (cause != NULL) {
! 153: cmdq_error(cmdq, "repeat count %s", cause);
! 154: free(cause);
! 155: return (CMD_RETURN_ERROR);
! 156: }
! 157: }
! 158:
1.10 nicm 159: mtmp.key = key;
160: mtmp.mode = !!args_has(args, 'c');
1.15 nicm 161: if ((mbind = RB_FIND(mode_key_tree, mtab->tree, &mtmp)) == NULL) {
162: mbind = xmalloc(sizeof *mbind);
163: mbind->key = mtmp.key;
164: mbind->mode = mtmp.mode;
165: RB_INSERT(mode_key_tree, mtab->tree, mbind);
166: }
1.26 ! nicm 167: mbind->repeat = repeat;
1.5 nicm 168: mbind->cmd = cmd;
1.15 nicm 169: mbind->arg = arg != NULL ? xstrdup(arg) : NULL;
1.14 nicm 170: return (CMD_RETURN_NORMAL);
1.1 nicm 171: }