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