diff options
author | Thomas Adam <thomas@xteddy.org> | 2020-05-13 01:15:31 +0100 |
---|---|---|
committer | Thomas Adam <thomas@xteddy.org> | 2020-05-13 01:15:31 +0100 |
commit | 9d44df9da95452d985f6e5937dd3c4311bf33548 (patch) | |
tree | f25a55a68c42ad5296fed0c4b87ea6a23c3afd75 /key-bindings.c | |
parent | 212cf53ea92acdc4fb752efd6ecd9fce99dccaec (diff) | |
parent | ba20e46bdc6d4624f54920c0efe753671c5184bb (diff) | |
download | rtmux-9d44df9da95452d985f6e5937dd3c4311bf33548.tar.gz rtmux-9d44df9da95452d985f6e5937dd3c4311bf33548.tar.bz2 rtmux-9d44df9da95452d985f6e5937dd3c4311bf33548.zip |
Merge branch 'master' of github.com:tmux/tmux
Diffstat (limited to 'key-bindings.c')
-rw-r--r-- | key-bindings.c | 59 |
1 files changed, 51 insertions, 8 deletions
diff --git a/key-bindings.c b/key-bindings.c index d6a72297..fc083db4 100644 --- a/key-bindings.c +++ b/key-bindings.c @@ -89,9 +89,8 @@ key_bindings_cmp(struct key_binding *bd1, struct key_binding *bd2) } static void -key_bindings_free(struct key_table *table, struct key_binding *bd) +key_bindings_free(struct key_binding *bd) { - RB_REMOVE(key_bindings, &table->key_bindings, bd); cmd_list_free(bd->cmdlist); free((void *)bd->note); free(bd); @@ -110,6 +109,7 @@ key_bindings_get_table(const char *name, int create) table = xmalloc(sizeof *table); table->name = xstrdup(name); RB_INIT(&table->key_bindings); + RB_INIT(&table->default_key_bindings); table->references = 1; /* one reference in key_tables */ RB_INSERT(key_tables, &key_tables, table); @@ -138,8 +138,14 @@ key_bindings_unref_table(struct key_table *table) if (--table->references != 0) return; - RB_FOREACH_SAFE(bd, key_bindings, &table->key_bindings, bd1) - key_bindings_free(table, bd); + RB_FOREACH_SAFE(bd, key_bindings, &table->key_bindings, bd1) { + RB_REMOVE(key_bindings, &table->key_bindings, bd); + key_bindings_free(bd); + } + RB_FOREACH_SAFE(bd, key_bindings, &table->default_key_bindings, bd1) { + RB_REMOVE(key_bindings, &table->default_key_bindings, bd); + key_bindings_free(bd); + } free((void *)table->name); free(table); @@ -155,6 +161,15 @@ key_bindings_get(struct key_table *table, key_code key) } struct key_binding * +key_bindings_get_default(struct key_table *table, key_code key) +{ + struct key_binding bd; + + bd.key = key; + return (RB_FIND(key_bindings, &table->default_key_bindings, &bd)); +} + +struct key_binding * key_bindings_first(struct key_table *table) { return (RB_MIN(key_bindings, &table->key_bindings)); @@ -176,8 +191,10 @@ key_bindings_add(const char *name, key_code key, const char *note, int repeat, table = key_bindings_get_table(name, 1); bd = key_bindings_get(table, key & ~KEYC_XTERM); - if (bd != NULL) - key_bindings_free(table, bd); + if (bd != NULL) { + RB_REMOVE(key_bindings, &table->key_bindings, bd); + key_bindings_free(bd); + } bd = xcalloc(1, sizeof *bd); bd->key = key; @@ -203,9 +220,12 @@ key_bindings_remove(const char *name, key_code key) bd = key_bindings_get(table, key & ~KEYC_XTERM); if (bd == NULL) return; - key_bindings_free(table, bd); - if (RB_EMPTY(&table->key_bindings)) { + RB_REMOVE(key_bindings, &table->key_bindings, bd); + key_bindings_free(bd); + + if (RB_EMPTY(&table->key_bindings) && + RB_EMPTY(&table->default_key_bindings)) { RB_REMOVE(key_tables, &key_tables, table); key_bindings_unref_table(table); } @@ -228,6 +248,28 @@ key_bindings_remove_table(const char *name) } } +static enum cmd_retval +key_bindings_init_done(__unused struct cmdq_item *item, __unused void *data) +{ + struct key_table *table; + struct key_binding *bd, *new_bd; + + RB_FOREACH(table, key_tables, &key_tables) { + RB_FOREACH(bd, key_bindings, &table->key_bindings) { + new_bd = xcalloc(1, sizeof *bd); + new_bd->key = bd->key; + if (bd->note != NULL) + new_bd->note = xstrdup(bd->note); + new_bd->flags = bd->flags; + new_bd->cmdlist = bd->cmdlist; + new_bd->cmdlist->references++; + RB_INSERT(key_bindings, &table->default_key_bindings, + new_bd); + } + } + return (CMD_RETURN_NORMAL); +} + void key_bindings_init(void) { @@ -525,6 +567,7 @@ key_bindings_init(void) cmdq_append(NULL, cmdq_get_command(pr->cmdlist, NULL)); cmd_list_free(pr->cmdlist); } + cmdq_append(NULL, cmdq_get_callback(key_bindings_init_done, NULL)); } static enum cmd_retval |