summaryrefslogtreecommitdiffstats
path: root/key-bindings.c
diff options
context:
space:
mode:
authorNicholas Marriott <nicholas.marriott@gmail.com>2009-01-06 14:10:32 +0000
committerNicholas Marriott <nicholas.marriott@gmail.com>2009-01-06 14:10:32 +0000
commitca1ee217023b431bac179023c2b70e13427c4ab0 (patch)
tree8fbc261392ff2e39f7a5d19594da2c2b0c248fee /key-bindings.c
parentac3fe6512f47e72ba108e04392d1f2e548774af9 (diff)
Switch to splay tree for key bindings.
Diffstat (limited to 'key-bindings.c')
-rw-r--r--key-bindings.c73
1 files changed, 32 insertions, 41 deletions
diff --git a/key-bindings.c b/key-bindings.c
index 039b855a..40f3a901 100644
--- a/key-bindings.c
+++ b/key-bindings.c
@@ -1,4 +1,4 @@
-/* $Id: key-bindings.c,v 1.38 2008-12-17 08:08:09 nicm Exp $ */
+/* $Id: key-bindings.c,v 1.39 2009-01-06 14:10:32 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@@ -24,23 +24,33 @@
#include "tmux.h"
-struct bindings key_bindings;
+SPLAY_GENERATE(key_bindings, key_binding, entry, key_bindings_cmp);
+
+struct key_bindings key_bindings;
+
+int
+key_bindings_cmp(struct key_binding *bd1, struct key_binding *bd2)
+{
+ return (bd1->key - bd2->key);
+}
+
+struct key_binding *
+key_bindings_lookup(int key)
+{
+ struct key_binding bd;
+
+ bd.key = key;
+ return (SPLAY_FIND(key_bindings, &key_bindings, &bd));
+}
void
key_bindings_add(int key, struct cmd *cmd)
{
- struct binding *bd;
- u_int i;
+ struct key_binding *bd;
- bd = NULL;
- for (i = 0; i < ARRAY_LENGTH(&key_bindings); i++) {
- bd = ARRAY_ITEM(&key_bindings, i);
- if (bd->key == key)
- break;
- }
- if (i == ARRAY_LENGTH(&key_bindings)) {
+ if ((bd = key_bindings_lookup(key)) == NULL) {
bd = xmalloc(sizeof *bd);
- ARRAY_ADD(&key_bindings, bd);
+ SPLAY_INSERT(key_bindings, &key_bindings, bd);
} else
cmd_free(bd->cmd);
@@ -51,19 +61,11 @@ key_bindings_add(int key, struct cmd *cmd)
void
key_bindings_remove(int key)
{
- struct binding *bd;
- u_int i;
+ struct key_binding *bd;
- bd = NULL;
- for (i = 0; i < ARRAY_LENGTH(&key_bindings); i++) {
- bd = ARRAY_ITEM(&key_bindings, i);
- if (bd->key == key)
- break;
- }
- if (i == ARRAY_LENGTH(&key_bindings))
+ if ((bd = key_bindings_lookup(key)) == NULL)
return;
-
- ARRAY_REMOVE(&key_bindings, i);
+ SPLAY_REMOVE(key_bindings, &key_bindings, bd);
cmd_free(bd->cmd);
xfree(bd);
@@ -108,7 +110,7 @@ key_bindings_init(void)
u_int i;
struct cmd *cmd;
- ARRAY_INIT(&key_bindings);
+ SPLAY_INIT(&key_bindings);
for (i = 0; i < (sizeof table / sizeof table[0]); i++) {
cmd = xmalloc(sizeof *cmd);
@@ -123,17 +125,13 @@ key_bindings_init(void)
void
key_bindings_free(void)
{
- struct binding *bd;
- u_int i;
-
- for (i = 0; i < ARRAY_LENGTH(&key_bindings); i++) {
- bd = ARRAY_ITEM(&key_bindings, i);
+ struct key_binding *bd;
+ while (!SPLAY_EMPTY(&key_bindings)) {
+ bd = SPLAY_ROOT(&key_bindings);
cmd_free(bd->cmd);
xfree(bd);
}
-
- ARRAY_FREE(&key_bindings);
}
void printflike2
@@ -185,17 +183,10 @@ key_bindings_info(struct cmd_ctx *ctx, const char *fmt, ...)
void
key_bindings_dispatch(int key, struct client *c)
{
- struct cmd_ctx ctx;
- struct binding *bd;
- u_int i;
+ struct cmd_ctx ctx;
+ struct key_binding *bd;
- bd = NULL;
- for (i = 0; i < ARRAY_LENGTH(&key_bindings); i++) {
- bd = ARRAY_ITEM(&key_bindings, i);
- if (bd->key == key)
- break;
- }
- if (i == ARRAY_LENGTH(&key_bindings))
+ if ((bd = key_bindings_lookup(key)) == NULL)
return;
ctx.msgdata = NULL;