aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHarry Jeffery <harry@exec64.co.uk>2019-08-18 14:54:26 +0100
committerHarry Jeffery <harry@exec64.co.uk>2019-08-18 14:54:26 +0100
commit00ad6b1d8e946069963bc2ab09f4f01c8e81ab03 (patch)
treee3e0cef7a0c4ed26a1c7a4cb29630b0af3c92b48
parent901243b3f97368c05f7ed1c723d4ef5d18f634a1 (diff)
downloadimv-00ad6b1d8e946069963bc2ab09f4f01c8e81ab03.tar.gz
imv: Add bind command
-rw-r--r--doc/imv.1.txt5
-rw-r--r--src/commands.c27
-rw-r--r--src/imv.c14
-rw-r--r--src/list.c27
-rw-r--r--src/list.h2
5 files changed, 49 insertions, 26 deletions
diff --git a/doc/imv.1.txt b/doc/imv.1.txt
index eb00c62..0d6a2b3 100644
--- a/doc/imv.1.txt
+++ b/doc/imv.1.txt
@@ -138,6 +138,11 @@ Commands can be entered by pressing *:*. imv supports the following commands:
Set the background color. 'checks' for a chequerboard pattern, or specify
a 6-digit hexadecimal color code. Aliased to 'bg'.
+*bind* <keys> <commands>::
+ Binds an action to a set of key inputs. Uses the same syntax as the config
+ file, but without an equals sign between the keys and the commands. For
+ more information on syntax, see **imv**(5).
+
Configuration
-------------
diff --git a/src/commands.c b/src/commands.c
index 25ec702..e72b9e0 100644
--- a/src/commands.c
+++ b/src/commands.c
@@ -11,31 +11,6 @@ struct command {
char* alias;
};
-static char *join_str_list(struct list *list, const char *sep, size_t start)
-{
- size_t len = 0;
- size_t cap = 512;
- char *buf = malloc(cap);
- buf[0] = 0;
-
- size_t sep_len = strlen(sep);
- for (size_t i = start; i < list->len; ++i) {
- size_t item_len = strlen(list->items[i]);
- if (len + item_len + sep_len >= cap) {
- cap *= 2;
- buf = realloc(buf, cap);
- assert(buf);
- }
-
- strncat(buf, list->items[i], cap - 1);
- len += item_len;
-
- strncat(buf, sep, cap - 1);
- len += sep_len;
- }
- return buf;
-}
-
struct imv_commands *imv_commands_create(void)
{
struct imv_commands *cmds = malloc(sizeof *cmds);
@@ -90,7 +65,7 @@ int imv_command_exec(struct imv_commands *cmds, const char *command, void *data)
cmd->handler(args, argstr, data);
ret = 0;
} else if(cmd->alias) {
- char *new_args = join_str_list(args, " ", 1);
+ char *new_args = list_to_string(args, " ", 1);
size_t cmd_len = strlen(cmd->alias) + 1 + strlen(new_args) + 1;
char *new_cmd = malloc(cmd_len);
snprintf(new_cmd, cmd_len, "%s %s", cmd->alias, new_args);
diff --git a/src/imv.c b/src/imv.c
index fd2bf1f..d657862 100644
--- a/src/imv.c
+++ b/src/imv.c
@@ -196,6 +196,7 @@ static void command_toggle_playing(struct list *args, const char *argstr, void *
static void command_set_scaling_mode(struct list *args, const char *argstr, void *data);
static void command_set_slideshow_duration(struct list *args, const char *argstr, void *data);
static void command_set_background(struct list *args, const char *argstr, void *data);
+static void command_bind(struct list *args, const char *argstr, void *data);
static bool setup_window(struct imv *imv);
static void consume_internal_event(struct imv *imv, struct internal_event *event);
@@ -542,6 +543,7 @@ struct imv *imv_create(void)
imv_command_register(imv->commands, "scaling", &command_set_scaling_mode);
imv_command_register(imv->commands, "slideshow", &command_set_slideshow_duration);
imv_command_register(imv->commands, "background", &command_set_background);
+ imv_command_register(imv->commands, "bind", &command_bind);
imv_command_alias(imv->commands, "q", "quit");
imv_command_alias(imv->commands, "n", "next");
@@ -1682,6 +1684,18 @@ static void command_set_background(struct list *args, const char *argstr, void *
}
}
+static void command_bind(struct list *args, const char *argstr, void *data)
+{
+ (void)argstr;
+ struct imv *imv = data;
+ if (args->len >= 3) {
+ const char *keys = args->items[1];
+ char *commands = list_to_string(args, " ", 2);
+ add_bind(imv, keys, commands);
+ free(commands);
+ }
+}
+
static void update_env_vars(struct imv *imv)
{
char str[64];
diff --git a/src/list.c b/src/list.c
index ba5ce87..21f2f74 100644
--- a/src/list.c
+++ b/src/list.c
@@ -1,5 +1,7 @@
#include "list.h"
+#include <assert.h>
+
struct list *list_create(void)
{
struct list *list = malloc(sizeof *list);
@@ -107,4 +109,29 @@ int list_find(struct list *list, int (*cmp)(const void *, const void *), const v
return -1;
}
+char *list_to_string(struct list *list, const char *sep, size_t start)
+{
+ size_t len = 0;
+ size_t cap = 512;
+ char *buf = malloc(cap);
+ buf[0] = 0;
+
+ size_t sep_len = strlen(sep);
+ for (size_t i = start; i < list->len; ++i) {
+ size_t item_len = strlen(list->items[i]);
+ if (len + item_len + sep_len >= cap) {
+ cap *= 2;
+ buf = realloc(buf, cap);
+ assert(buf);
+ }
+
+ strncat(buf, list->items[i], cap - 1);
+ len += item_len;
+
+ strncat(buf, sep, cap - 1);
+ len += sep_len;
+ }
+ return buf;
+}
+
/* vim:set ts=2 sts=2 sw=2 et: */
diff --git a/src/list.h b/src/list.h
index abb8b85..069aea1 100644
--- a/src/list.h
+++ b/src/list.h
@@ -34,6 +34,8 @@ int list_find(
const void *key
);
+char *list_to_string(struct list *list, const char *sep, size_t start);
+
#endif
/* vim:set ts=2 sts=2 sw=2 et: */