From: Daniel Carl Date: Sat, 16 Feb 2013 00:22:59 +0000 (+0100) Subject: Simplified keybind by removing string splitting. X-Git-Url: https://git.owens.tech/assets/static/git-logo.png/assets/static/git-logo.png/git?a=commitdiff_plain;h=bcbb0cd77581cc1c15f096e032cc68203396e686;p=vimb.git Simplified keybind by removing string splitting. The splitting of keybind input string is now done in the command that maps the keybindings. --- diff --git a/src/command.c b/src/command.c index 2289862..11240b7 100644 --- a/src/command.c +++ b/src/command.c @@ -273,9 +273,17 @@ gboolean command_scroll(const Arg* arg) gboolean command_map(const Arg* arg) { + gboolean result; vp_set_mode(VP_MODE_NORMAL, FALSE); - return keybind_add_from_string(arg->s, arg->i); + gchar **string = g_strsplit(arg->s, "=", 2); + if (g_strv_length(string) != 2) { + return FALSE; + } + result = keybind_add_from_string(string[0], string[1], arg->i); + g_strfreev(string); + + return result; } gboolean command_unmap(const Arg* arg) diff --git a/src/keybind.c b/src/keybind.c index 5bd6f6e..90d077b 100644 --- a/src/keybind.c +++ b/src/keybind.c @@ -46,66 +46,49 @@ void keybind_cleanup(void) } } -gboolean keybind_add_from_string(const gchar* str, const Mode mode) +gboolean keybind_add_from_string(gchar* keys, const gchar* command, const Mode mode) { - if (str == NULL || *str == '\0') { + if (keys == NULL || *keys == '\0') { return FALSE; } - gboolean result; - gchar* line = g_strdup(str); - g_strstrip(line); - - /* split into keybinding and command */ - char **string = g_strsplit(line, "=", 2); - - guint len = g_strv_length(string); - if (len == 2) { - /* split the input string into command and parameter part */ - gchar** token = g_strsplit(string[1], " ", 2); - if (!token[0] || !command_exists(token[0])) { - g_strfreev(token); - return FALSE; - } - Keybind* keybind = g_new0(Keybind, 1); - keybind->mode = mode; - keybind->command = g_strdup(token[0]); - keybind->param = g_strdup(token[1]); + /* split the input string into command and parameter part */ + gchar** token = g_strsplit(command, " ", 2); + if (!token[0] || !command_exists(token[0])) { g_strfreev(token); + return FALSE; + } - keybind_str_to_keybind(string[0], keybind); + Keybind* keybind = g_new0(Keybind, 1); + keybind->mode = mode; + keybind->command = g_strdup(token[0]); + keybind->param = g_strdup(token[1]); + g_strfreev(token); - /* add the keybinding to the list */ - vp.behave.keys = g_slist_prepend(vp.behave.keys, keybind); + keybind_str_to_keybind(keys, keybind); - /* save the modkey also in the modkey string if not exists already */ - if (keybind->modkey && strchr(vp.behave.modkeys->str, keybind->modkey) == NULL) { - g_string_append_c(vp.behave.modkeys, keybind->modkey); - } - result = TRUE; - } else { - result = FALSE; - } + /* add the keybinding to the list */ + vp.behave.keys = g_slist_prepend(vp.behave.keys, keybind); - g_strfreev(string); - g_free(line); + /* save the modkey also in the modkey string if not exists already */ + if (keybind->modkey && strchr(vp.behave.modkeys->str, keybind->modkey) == NULL) { + g_string_append_c(vp.behave.modkeys, keybind->modkey); + } - return result; + return TRUE; } -gboolean keybind_remove_from_string(const gchar* str, const Mode mode) +gboolean keybind_remove_from_string(gchar* str, const Mode mode) { - gchar* line = NULL; Keybind keybind = {.mode = mode}; if (str == NULL || *str == '\0') { return FALSE; } - line = g_strdup(str); - g_strstrip(line); + g_strstrip(str); /* fill the keybind with data from given string */ - keybind_str_to_keybind(line, &keybind); + keybind_str_to_keybind(str, &keybind); GSList* link = keybind_find(keybind.mode, keybind.modkey, keybind.modmask, keybind.keyval); if (link) { @@ -167,9 +150,6 @@ static void keybind_str_to_keybind(gchar* str, Keybind* keybind) gchar** string = NULL; guint len = 0; - if (str == NULL || *str == '\0') { - return; - } g_strstrip(str); /* [modkey]keyval diff --git a/src/keybind.h b/src/keybind.h index 0aa0003..79ba6aa 100644 --- a/src/keybind.h +++ b/src/keybind.h @@ -34,7 +34,7 @@ typedef struct { void keybind_init(void); void keybind_cleanup(void); -gboolean keybind_add_from_string(const gchar* str, const Mode mode); -gboolean keybind_remove_from_string(const gchar* str, const Mode mode); +gboolean keybind_add_from_string(gchar* keys, const gchar* command, const Mode mode); +gboolean keybind_remove_from_string(gchar* str, const Mode mode); #endif /* end of include guard: _KEYBIND_H */