summaryrefslogtreecommitdiffstats
path: root/key-bindings.c
diff options
context:
space:
mode:
authornicm <nicm>2020-01-05 20:39:25 +0000
committernicm <nicm>2020-01-05 20:39:25 +0000
commit73b8c2ef3ca011fd7e7e46549c9640e9e123d59a (patch)
tree2294dca309b54ffc53ff357885911def6561c672 /key-bindings.c
parent7c6c66cc3cc40b663fc2ba8573f26a6aa4cb291e (diff)
Common function to free key bindings.
Diffstat (limited to 'key-bindings.c')
-rw-r--r--key-bindings.c37
1 files changed, 17 insertions, 20 deletions
diff --git a/key-bindings.c b/key-bindings.c
index 7cd834a2..94110a49 100644
--- a/key-bindings.c
+++ b/key-bindings.c
@@ -85,6 +85,14 @@ key_bindings_cmp(struct key_binding *bd1, struct key_binding *bd2)
return (0);
}
+static void
+key_bindings_free(struct key_table *table, struct key_binding *bd)
+{
+ RB_REMOVE(key_bindings, &table->key_bindings, bd);
+ cmd_list_free(bd->cmdlist);
+ free(bd);
+}
+
struct key_table *
key_bindings_get_table(const char *name, int create)
{
@@ -126,11 +134,8 @@ key_bindings_unref_table(struct key_table *table)
if (--table->references != 0)
return;
- RB_FOREACH_SAFE(bd, key_bindings, &table->key_bindings, bd1) {
- RB_REMOVE(key_bindings, &table->key_bindings, bd);
- cmd_list_free(bd->cmdlist);
- free(bd);
- }
+ RB_FOREACH_SAFE(bd, key_bindings, &table->key_bindings, bd1)
+ key_bindings_free(table, bd);
free((void *)table->name);
free(table);
@@ -162,17 +167,13 @@ key_bindings_add(const char *name, key_code key, int repeat,
struct cmd_list *cmdlist)
{
struct key_table *table;
- struct key_binding bd_find, *bd;
+ struct key_binding *bd;
table = key_bindings_get_table(name, 1);
- bd_find.key = (key & ~KEYC_XTERM);
- bd = RB_FIND(key_bindings, &table->key_bindings, &bd_find);
- if (bd != NULL) {
- RB_REMOVE(key_bindings, &table->key_bindings, bd);
- cmd_list_free(bd->cmdlist);
- free(bd);
- }
+ bd = key_bindings_get(table, key & ~KEYC_XTERM);
+ if (bd != NULL)
+ key_bindings_free(table, bd);
bd = xcalloc(1, sizeof *bd);
bd->key = key;
@@ -187,20 +188,16 @@ void
key_bindings_remove(const char *name, key_code key)
{
struct key_table *table;
- struct key_binding bd_find, *bd;
+ struct key_binding *bd;
table = key_bindings_get_table(name, 0);
if (table == NULL)
return;
- bd_find.key = (key & ~KEYC_XTERM);
- bd = RB_FIND(key_bindings, &table->key_bindings, &bd_find);
+ bd = key_bindings_get(table, key & ~KEYC_XTERM);
if (bd == NULL)
return;
-
- RB_REMOVE(key_bindings, &table->key_bindings, bd);
- cmd_list_free(bd->cmdlist);
- free(bd);
+ key_bindings_free(table, bd);
if (RB_EMPTY(&table->key_bindings)) {
RB_REMOVE(key_tables, &key_tables, table);