Switch to splay tree for key bindings.

pull/1/head
Nicholas Marriott 2009-01-06 14:10:32 +00:00
parent ac3fe6512f
commit ca1ee21702
5 changed files with 51 additions and 55 deletions

View File

@ -1,3 +1,7 @@
06 January 2009
* Use a splay tree for key bindings.
22 December 2008 22 December 2008
* Use the right keys for home and end. * Use the right keys for home and end.
@ -792,7 +796,7 @@
(including mutt, emacs). No status bar yet and no key remapping or other (including mutt, emacs). No status bar yet and no key remapping or other
customisation. customisation.
$Id: CHANGES,v 1.176 2008-12-22 17:26:51 nicm Exp $ $Id: CHANGES,v 1.177 2009-01-06 14:10:32 nicm Exp $
LocalWords: showw utf UTF fulvio ciriaco joshe OSC APC gettime abc DEF OA clr LocalWords: showw utf UTF fulvio ciriaco joshe OSC APC gettime abc DEF OA clr
LocalWords: rivo nurges lscm Erdely eol smysession mysession ek dstname RB LocalWords: rivo nurges lscm Erdely eol smysession mysession ek dstname RB

2
TODO
View File

@ -43,14 +43,12 @@
- activity/bell should be per-window not per-link? what if it is cur win in - activity/bell should be per-window not per-link? what if it is cur win in
session not being watched? session not being watched?
- tidy up window modes - tidy up window modes
- list-keys should be sorted
- problems with force-width when wrapping line in emacs? - problems with force-width when wrapping line in emacs?
- command history for command-prompt. better tab completion (use options too) - command history for command-prompt. better tab completion (use options too)
- window options should be done similarly to standard options - window options should be done similarly to standard options
- next prev word etc in command prompt - next prev word etc in command prompt
- many more info() displays for various things - many more info() displays for various things
- vi half page scroll - vi half page scroll
- why do home/end work in emacs outside tmux but not inside?
- document status line options, title bits - document status line options, title bits
- document mode-fg/mode-bg/message-fg/message-bg - document mode-fg/mode-bg/message-fg/message-bg
- document window options changes - document window options changes

View File

@ -1,4 +1,4 @@
/* $Id: cmd-list-keys.c,v 1.10 2008-06-05 21:25:00 nicm Exp $ */ /* $Id: cmd-list-keys.c,v 1.11 2009-01-06 14:10:32 nicm Exp $ */
/* /*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net> * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@ -42,13 +42,11 @@ const struct cmd_entry cmd_list_keys_entry = {
void void
cmd_list_keys_exec(unused struct cmd *self, struct cmd_ctx *ctx) cmd_list_keys_exec(unused struct cmd *self, struct cmd_ctx *ctx)
{ {
struct binding *bd; struct key_binding *bd;
const char *key; const char *key;
char s[BUFSIZ]; char s[BUFSIZ];
u_int i;
for (i = 0; i < ARRAY_LENGTH(&key_bindings); i++) { SPLAY_FOREACH(bd, key_bindings, &key_bindings) {
bd = ARRAY_ITEM(&key_bindings, i);
if ((key = key_string_lookup_key(bd->key)) == NULL) if ((key = key_string_lookup_key(bd->key)) == NULL)
continue; continue;
if (bd->cmd->entry->print == NULL) { if (bd->cmd->entry->print == NULL) {

View File

@ -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> * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@ -24,23 +24,33 @@
#include "tmux.h" #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 void
key_bindings_add(int key, struct cmd *cmd) key_bindings_add(int key, struct cmd *cmd)
{ {
struct binding *bd; struct key_binding *bd;
u_int i;
bd = NULL; if ((bd = key_bindings_lookup(key)) == 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)) {
bd = xmalloc(sizeof *bd); bd = xmalloc(sizeof *bd);
ARRAY_ADD(&key_bindings, bd); SPLAY_INSERT(key_bindings, &key_bindings, bd);
} else } else
cmd_free(bd->cmd); cmd_free(bd->cmd);
@ -51,19 +61,11 @@ key_bindings_add(int key, struct cmd *cmd)
void void
key_bindings_remove(int key) key_bindings_remove(int key)
{ {
struct binding *bd; struct key_binding *bd;
u_int i;
bd = NULL; if ((bd = key_bindings_lookup(key)) == 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))
return; return;
SPLAY_REMOVE(key_bindings, &key_bindings, bd);
ARRAY_REMOVE(&key_bindings, i);
cmd_free(bd->cmd); cmd_free(bd->cmd);
xfree(bd); xfree(bd);
@ -108,7 +110,7 @@ key_bindings_init(void)
u_int i; u_int i;
struct cmd *cmd; struct cmd *cmd;
ARRAY_INIT(&key_bindings); SPLAY_INIT(&key_bindings);
for (i = 0; i < (sizeof table / sizeof table[0]); i++) { for (i = 0; i < (sizeof table / sizeof table[0]); i++) {
cmd = xmalloc(sizeof *cmd); cmd = xmalloc(sizeof *cmd);
@ -123,17 +125,13 @@ key_bindings_init(void)
void void
key_bindings_free(void) key_bindings_free(void)
{ {
struct binding *bd; struct key_binding *bd;
u_int i;
for (i = 0; i < ARRAY_LENGTH(&key_bindings); i++) {
bd = ARRAY_ITEM(&key_bindings, i);
while (!SPLAY_EMPTY(&key_bindings)) {
bd = SPLAY_ROOT(&key_bindings);
cmd_free(bd->cmd); cmd_free(bd->cmd);
xfree(bd); xfree(bd);
} }
ARRAY_FREE(&key_bindings);
} }
void printflike2 void printflike2
@ -185,17 +183,10 @@ key_bindings_info(struct cmd_ctx *ctx, const char *fmt, ...)
void void
key_bindings_dispatch(int key, struct client *c) key_bindings_dispatch(int key, struct client *c)
{ {
struct cmd_ctx ctx; struct cmd_ctx ctx;
struct binding *bd; struct key_binding *bd;
u_int i;
bd = NULL; if ((bd = key_bindings_lookup(key)) == 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))
return; return;
ctx.msgdata = NULL; ctx.msgdata = NULL;

13
tmux.h
View File

@ -1,4 +1,4 @@
/* $Id: tmux.h,v 1.204 2008-12-15 21:21:56 nicm Exp $ */ /* $Id: tmux.h,v 1.205 2009-01-06 14:10:32 nicm Exp $ */
/* /*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net> * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@ -831,11 +831,13 @@ struct cmd_option_data {
}; };
/* Key binding. */ /* Key binding. */
struct binding { struct key_binding {
int key; int key;
struct cmd *cmd; struct cmd *cmd;
SPLAY_ENTRY(key_binding) entry;
}; };
ARRAY_DECL(bindings, struct binding *); SPLAY_HEAD(key_bindings, key_binding);
/* Set/display option data. */ /* Set/display option data. */
struct set_option_entry { struct set_option_entry {
@ -1122,7 +1124,10 @@ void client_write_server2(
void client_fill_session(struct msg_command_data *); void client_fill_session(struct msg_command_data *);
/* key-bindings.c */ /* key-bindings.c */
extern struct bindings key_bindings; extern struct key_bindings key_bindings;
int key_bindings_cmp(struct key_binding *, struct key_binding *);
SPLAY_PROTOTYPE(key_bindings, key_binding, entry, key_bindings_cmp);
struct key_binding *key_bindings_lookup(int);
void key_bindings_add(int, struct cmd *); void key_bindings_add(int, struct cmd *);
void key_bindings_remove(int); void key_bindings_remove(int);
void key_bindings_init(void); void key_bindings_init(void);