aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/binds.c34
-rw-r--r--src/binds.h2
-rw-r--r--src/commands.c36
-rw-r--r--src/commands.h2
-rw-r--r--src/imv.c21
5 files changed, 55 insertions, 40 deletions
diff --git a/src/binds.c b/src/binds.c
index 1649537..a710278 100644
--- a/src/binds.c
+++ b/src/binds.c
@@ -5,7 +5,7 @@
struct bind_node {
char *key; /* input key to reach this node */
- char *command; /* command to run for this node, or NULL if not leaf node */
+ struct list *commands; /* commands to run for this node, or NULL if not leaf node */
struct list *suffixes; /* list of bind_node* suffixes, or NULL if leaf node */
};
@@ -24,7 +24,7 @@ static int compare_node_key(const void* item, const void *key)
static void init_bind_node(struct bind_node *bn)
{
bn->key = NULL;
- bn->command = NULL;
+ bn->commands = NULL;
bn->suffixes = list_create();
}
@@ -34,7 +34,9 @@ static void destroy_bind_node(struct bind_node *bn)
destroy_bind_node(bn->suffixes->items[i]);
}
free(bn->key);
- free(bn->command);
+ if(bn->commands) {
+ list_deep_free(bn->commands);
+ }
list_deep_free(bn->suffixes);
}
@@ -69,7 +71,7 @@ enum bind_result imv_binds_add(struct imv_binds *binds, const struct list *keys,
struct bind_node *node = &binds->bind_tree;
for(size_t i = 0; i < keys->len; ++i) {
/* If we've reached a node that already has a command, there's a conflict */
- if(node->command) {
+ if(node->commands) {
result = BIND_INVALID_COMMAND;
break;
}
@@ -90,15 +92,14 @@ enum bind_result imv_binds_add(struct imv_binds *binds, const struct list *keys,
/* We've now found the correct node for this key */
/* Check if the node has a command */
- if(next_node->command) {
+ if(next_node->commands) {
if(i + 1 < keys->len) {
/* If we're not at the end, it's a conflict */
result = BIND_CONFLICTS;
break;
} else {
- /* Otherwise we just need to overwrite the existing bind. */
- free(next_node->command);
- next_node->command = strdup(command);
+ /* Otherwise we just need to append a new command to the existing bind. */
+ list_append(next_node->commands, strdup(command));
result = BIND_SUCCESS;
break;
}
@@ -110,7 +111,8 @@ enum bind_result imv_binds_add(struct imv_binds *binds, const struct list *keys,
if(next_node->suffixes->len > 0) {
result = BIND_CONFLICTS;
} else {
- next_node->command = strdup(command);
+ next_node->commands = list_create();
+ list_append(next_node->commands, strdup(command));
}
} else {
/* Otherwise, move down the trie */
@@ -133,7 +135,7 @@ enum lookup_result {
LOOKUP_MATCH,
};
-static enum lookup_result bind_lookup(struct bind_node *node, struct list *keys, const char **out_str)
+static enum lookup_result bind_lookup(struct bind_node *node, struct list *keys, struct list **out_list)
{
for(size_t part = 0; part < keys->len; ++part) {
const char* cur_key = keys->items[part];
@@ -151,8 +153,8 @@ static enum lookup_result bind_lookup(struct bind_node *node, struct list *keys,
}
}
- if(node->command) {
- *out_str = node->command;
+ if(node->commands) {
+ *out_list = node->commands;
return LOOKUP_MATCH;
}
return LOOKUP_PARTIAL;
@@ -216,7 +218,7 @@ void imv_bind_clear_input(struct imv_binds *binds)
binds->keys = list_create();
}
-const char *imv_bind_handle_event(struct imv_binds *binds, const SDL_Event *event)
+struct list *imv_bind_handle_event(struct imv_binds *binds, const SDL_Event *event)
{
if(event->key.keysym.sym == SDLK_ESCAPE) {
imv_bind_clear_input(binds);
@@ -230,13 +232,13 @@ const char *imv_bind_handle_event(struct imv_binds *binds, const SDL_Event *even
}
list_append(binds->keys, strdup(buffer));
- const char *command = NULL;
- enum lookup_result result = bind_lookup(&binds->bind_tree, binds->keys, &command);
+ struct list *commands = NULL;
+ enum lookup_result result = bind_lookup(&binds->bind_tree, binds->keys, &commands);
if(result == LOOKUP_PARTIAL) {
return NULL;
} else if(result == LOOKUP_MATCH) {
imv_bind_clear_input(binds);
- return command;
+ return commands;
} else if(result == LOOKUP_INVALID) {
imv_bind_clear_input(binds);
return NULL;
diff --git a/src/binds.h b/src/binds.h
index 05810d8..a720fe1 100644
--- a/src/binds.h
+++ b/src/binds.h
@@ -32,7 +32,7 @@ const struct list *imv_bind_input_buffer(struct imv_binds *binds);
void imv_bind_clear_input(struct imv_binds *binds);
/* Handle an input event, if a bind is triggered, return its command */
-const char *imv_bind_handle_event(struct imv_binds *binds, const SDL_Event *event);
+struct list *imv_bind_handle_event(struct imv_binds *binds, const SDL_Event *event);
/* Convert a string (such as from a config) to a key list */
struct list *imv_bind_parse_keys(const char *keys);
diff --git a/src/commands.c b/src/commands.c
index 7dfbc2c..2bc33fe 100644
--- a/src/commands.c
+++ b/src/commands.c
@@ -46,29 +46,35 @@ void imv_command_alias(struct imv_commands *cmds, const char *command, const cha
list_append(cmds->command_list, cmd);
}
-int imv_command_exec(struct imv_commands *cmds, const char *command, void *data)
+int imv_command_exec(struct imv_commands *cmds, struct list *commands, void *data)
{
- struct list *args = list_from_string(command, ' ');
int ret = 1;
+ for(size_t i = 0; i < commands->len; ++i) {
+ const char *command = commands->items[i];
+ struct list *args = list_from_string(command, ' ');
- if(args->len > 0) {
- for(size_t i = 0; i < cmds->command_list->len; ++i) {
- struct command *cmd = cmds->command_list->items[i];
- if(!strcmp(cmd->command, args->items[0])) {
- if(cmd->handler) {
- /* argstr = all args as a single string */
- const char *argstr = command + strlen(cmd->command) + 1;
- cmd->handler(args, argstr, data);
- ret = 0;
- } else if(cmd->alias) {
- ret = imv_command_exec(cmds, cmd->alias, data);
+ if(args->len > 0) {
+ for(size_t i = 0; i < cmds->command_list->len; ++i) {
+ struct command *cmd = cmds->command_list->items[i];
+ if(!strcmp(cmd->command, args->items[0])) {
+ if(cmd->handler) {
+ /* argstr = all args as a single string */
+ const char *argstr = command + strlen(cmd->command) + 1;
+ cmd->handler(args, argstr, data);
+ ret = 0;
+ } else if(cmd->alias) {
+ struct list *commands = list_create();
+ list_append(commands, cmd->alias);
+ ret = imv_command_exec(cmds, commands, data);
+ list_free(commands);
+ }
+ break;
}
- break;
}
}
+ list_deep_free(args);
}
- list_deep_free(args);
return ret;
}
diff --git a/src/commands.h b/src/commands.h
index cf3a728..256018e 100644
--- a/src/commands.h
+++ b/src/commands.h
@@ -11,7 +11,7 @@ struct imv_commands *imv_commands_create(void);
void imv_commands_free(struct imv_commands *cmds);
void imv_command_register(struct imv_commands *cmds, const char *command, void (*handler)(struct list*, const char*, void*));
void imv_command_alias(struct imv_commands *cmds, const char *command, const char *alias);
-int imv_command_exec(struct imv_commands *cmds, const char *command, void *data);
+int imv_command_exec(struct imv_commands *cmds, struct list *commands, void *data);
#endif
diff --git a/src/imv.c b/src/imv.c
index dc3dedf..768ae21 100644
--- a/src/imv.c
+++ b/src/imv.c
@@ -760,10 +760,14 @@ static void handle_event(struct imv *imv, SDL_Event *event)
}
switch(event->type) {
- case SDL_QUIT:
- imv_command_exec(imv->commands, "quit", imv);
+ case SDL_QUIT: {
+ /* new scope needed in order to declare variables in a switch statement */
+ struct list *commands = list_create();
+ list_append(commands, "quit");
+ imv_command_exec(imv->commands, commands, imv);
+ list_free(commands);
break;
-
+ }
case SDL_TEXTINPUT:
strncat(imv->input_buffer, event->text.text, command_buffer_len - 1);
imv->need_redraw = true;
@@ -780,8 +784,11 @@ static void handle_event(struct imv *imv, SDL_Event *event)
imv->input_buffer = NULL;
imv->need_redraw = true;
} else if(event->key.keysym.sym == SDLK_RETURN) {
- imv_command_exec(imv->commands, imv->input_buffer, imv);
+ struct list *commands = list_create();
+ list_append(commands, imv->input_buffer);
+ imv_command_exec(imv->commands, commands, imv);
SDL_StopTextInput();
+ list_free(commands);
free(imv->input_buffer);
imv->input_buffer = NULL;
imv->need_redraw = true;
@@ -806,9 +813,9 @@ static void handle_event(struct imv *imv, SDL_Event *event)
}
break;
default: { /* braces to allow const char *cmd definition */
- const char *cmd = imv_bind_handle_event(imv->binds, event);
- if(cmd) {
- imv_command_exec(imv->commands, cmd, imv);
+ struct list *cmds = imv_bind_handle_event(imv->binds, event);
+ if(cmds) {
+ imv_command_exec(imv->commands, cmds, imv);
}
}
}