Annotation of src/usr.bin/tmux/cmd-unbind-key.c, Revision 1.7
1.7 ! nicm 1: /* $OpenBSD: cmd-unbind-key.c,v 1.6 2009/11/13 19:53:29 nicm Exp $ */
1.1 nicm 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 "tmux.h"
22:
23: /*
24: * Unbind key from command.
25: */
26:
27: int cmd_unbind_key_parse(struct cmd *, int, char **, char **);
28: int cmd_unbind_key_exec(struct cmd *, struct cmd_ctx *);
29: void cmd_unbind_key_free(struct cmd *);
30:
1.5 nicm 31: int cmd_unbind_key_table(struct cmd *, struct cmd_ctx *);
32:
1.1 nicm 33: struct cmd_unbind_key_data {
34: int key;
1.5 nicm 35:
36: int command_key;
37: char *tablename;
1.1 nicm 38: };
39:
40: const struct cmd_entry cmd_unbind_key_entry = {
41: "unbind-key", "unbind",
1.5 nicm 42: "[-cn] [-t key-table] key",
1.6 nicm 43: 0, "",
1.1 nicm 44: NULL,
45: cmd_unbind_key_parse,
46: cmd_unbind_key_exec,
47: cmd_unbind_key_free,
48: NULL
49: };
50:
51: int
52: cmd_unbind_key_parse(struct cmd *self, int argc, char **argv, char **cause)
53: {
54: struct cmd_unbind_key_data *data;
1.3 nicm 55: int opt, no_prefix = 0;
1.1 nicm 56:
57: self->data = data = xmalloc(sizeof *data);
1.5 nicm 58: data->command_key = 0;
59: data->tablename = NULL;
1.1 nicm 60:
1.5 nicm 61: while ((opt = getopt(argc, argv, "cnt:")) != -1) {
1.1 nicm 62: switch (opt) {
1.5 nicm 63: case 'c':
64: data->command_key = 1;
65: break;
1.3 nicm 66: case 'n':
67: no_prefix = 1;
68: break;
1.5 nicm 69: case 't':
1.7 ! nicm 70: if (data->tablename == NULL)
! 71: data->tablename = xstrdup(optarg);
1.5 nicm 72: break;
1.1 nicm 73: default:
74: goto usage;
75: }
76: }
77: argc -= optind;
78: argv += optind;
79: if (argc != 1)
80: goto usage;
81:
82: if ((data->key = key_string_lookup_string(argv[0])) == KEYC_NONE) {
83: xasprintf(cause, "unknown key: %s", argv[0]);
84: goto error;
85: }
1.3 nicm 86: if (!no_prefix)
87: data->key |= KEYC_PREFIX;
1.1 nicm 88:
89: return (0);
90:
91: usage:
92: xasprintf(cause, "usage: %s %s", self->entry->name, self->entry->usage);
93:
94: error:
95: xfree(data);
96: return (-1);
97: }
98:
99: int
100: cmd_unbind_key_exec(struct cmd *self, unused struct cmd_ctx *ctx)
101: {
102: struct cmd_unbind_key_data *data = self->data;
103:
104: if (data == NULL)
105: return (0);
1.5 nicm 106: if (data->tablename != NULL)
107: return (cmd_unbind_key_table(self, ctx));
1.1 nicm 108:
109: key_bindings_remove(data->key);
110:
111: return (0);
112: }
113:
1.5 nicm 114: int
115: cmd_unbind_key_table(struct cmd *self, struct cmd_ctx *ctx)
116: {
117: struct cmd_unbind_key_data *data = self->data;
118: const struct mode_key_table *mtab;
119: struct mode_key_binding *mbind, mtmp;
120:
121: if ((mtab = mode_key_findtable(data->tablename)) == NULL) {
122: ctx->error(ctx, "unknown key table: %s", data->tablename);
123: return (-1);
124: }
125:
126: mtmp.key = data->key & ~KEYC_PREFIX;
127: mtmp.mode = data->command_key ? 1 : 0;
128: if ((mbind = SPLAY_FIND(mode_key_tree, mtab->tree, &mtmp)) != NULL) {
129: SPLAY_REMOVE(mode_key_tree, mtab->tree, mbind);
130: xfree(mbind);
131: }
132: return (0);
133: }
134:
1.1 nicm 135: void
136: cmd_unbind_key_free(struct cmd *self)
137: {
138: struct cmd_unbind_key_data *data = self->data;
139:
1.5 nicm 140: if (data->tablename != NULL)
141: xfree(data->tablename);
1.1 nicm 142: xfree(data);
143: }