1 /* $OpenBSD: cmd-unbind-key.c,v 1.14 2012/07/10 11:53:01 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 23 #include "tmux.h" 24 25 /* 26 * Unbind key from command. 27 */ 28 29 int cmd_unbind_key_check(struct args *); 30 int cmd_unbind_key_exec(struct cmd *, struct cmd_ctx *); 31 32 int cmd_unbind_key_table(struct cmd *, struct cmd_ctx *, int); 33 34 const struct cmd_entry cmd_unbind_key_entry = { 35 "unbind-key", "unbind", 36 "acnt:", 0, 1, 37 "[-acn] [-t key-table] key", 38 0, 39 NULL, 40 cmd_unbind_key_check, 41 cmd_unbind_key_exec 42 }; 43 44 int 45 cmd_unbind_key_check(struct args *args) 46 { 47 if (args_has(args, 'a') && args->argc != 0) 48 return (-1); 49 if (!args_has(args, 'a') && args->argc != 1) 50 return (-1); 51 return (0); 52 } 53 54 int 55 cmd_unbind_key_exec(struct cmd *self, unused struct cmd_ctx *ctx) 56 { 57 struct args *args = self->args; 58 struct key_binding *bd; 59 int key; 60 61 if (!args_has(args, 'a')) { 62 key = key_string_lookup_string(args->argv[0]); 63 if (key == KEYC_NONE) { 64 ctx->error(ctx, "unknown key: %s", args->argv[0]); 65 return (-1); 66 } 67 } else 68 key = KEYC_NONE; 69 70 if (args_has(args, 't')) 71 return (cmd_unbind_key_table(self, ctx, key)); 72 73 if (key == KEYC_NONE) { 74 while (!RB_EMPTY(&key_bindings)) { 75 bd = RB_ROOT(&key_bindings); 76 key_bindings_remove(bd->key); 77 } 78 return (0); 79 } 80 81 if (!args_has(args, 'n')) 82 key |= KEYC_PREFIX; 83 key_bindings_remove(key); 84 return (0); 85 } 86 87 int 88 cmd_unbind_key_table(struct cmd *self, struct cmd_ctx *ctx, int key) 89 { 90 struct args *args = self->args; 91 const char *tablename; 92 const struct mode_key_table *mtab; 93 struct mode_key_binding *mbind, mtmp; 94 95 tablename = args_get(args, 't'); 96 if ((mtab = mode_key_findtable(tablename)) == NULL) { 97 ctx->error(ctx, "unknown key table: %s", tablename); 98 return (-1); 99 } 100 101 if (key == KEYC_NONE) { 102 while (!RB_EMPTY(mtab->tree)) { 103 mbind = RB_ROOT(mtab->tree); 104 RB_REMOVE(mode_key_tree, mtab->tree, mbind); 105 free(mbind); 106 } 107 return (0); 108 } 109 110 mtmp.key = key; 111 mtmp.mode = !!args_has(args, 'c'); 112 if ((mbind = RB_FIND(mode_key_tree, mtab->tree, &mtmp)) != NULL) { 113 RB_REMOVE(mode_key_tree, mtab->tree, mbind); 114 free(mbind); 115 } 116 return (0); 117 } 118