From 1b85f0d2617cb838a354831b0450d4b01d67348c Mon Sep 17 00:00:00 2001 From: Daniel Carl Date: Sun, 15 Apr 2018 00:11:00 +0200 Subject: [PATCH] Do not propagate the shortcut struct. --- src/ex.c | 8 ++++---- src/main.c | 4 +++- src/main.h | 6 ++---- src/setting.c | 8 +++----- src/shortcut.c | 54 +++++++++++++++++++++++++++++--------------------- src/shortcut.h | 16 ++++++++------- 6 files changed, 52 insertions(+), 44 deletions(-) diff --git a/src/ex.c b/src/ex.c index 3520b20..94d3d7d 100644 --- a/src/ex.c +++ b/src/ex.c @@ -1099,18 +1099,18 @@ static VbCmdResult ex_shortcut(Client *c, const ExArg *arg) *uri++ = '\0'; /* devide key and uri */ g_strstrip(arg->rhs->str); g_strstrip(uri); - success = shortcut_add(c, arg->rhs->str, uri); + success = shortcut_add(c->config.shortcuts, arg->rhs->str, uri); } break; case EX_SCR: g_strstrip(arg->rhs->str); - success = shortcut_remove(c, arg->rhs->str); + success = shortcut_remove(c->config.shortcuts, arg->rhs->str); break; case EX_SCD: g_strstrip(arg->rhs->str); - success = shortcut_set_default(c, arg->rhs->str); + success = shortcut_set_default(c->config.shortcuts, arg->rhs->str); break; default: @@ -1236,7 +1236,7 @@ static gboolean complete(Client *c, short direction) case EX_SCR: /* Fallthrough */ case EX_SCD: - found = shortcut_fill_completion(c, store, token); + found = shortcut_fill_completion(c->config.shortcuts, store, token); break; case EX_HANDREM: diff --git a/src/main.c b/src/main.c index aaddbef..84c9c9f 100644 --- a/src/main.c +++ b/src/main.c @@ -381,7 +381,7 @@ gboolean vb_load_uri(Client *c, const Arg *arg) free(rp); } else if (strchr(path, ' ') || !strchr(path, '.')) { /* use a shortcut if path contains spaces or no dot */ - uri = shortcut_get_uri(c, path); + uri = shortcut_get_uri(c->config.shortcuts, path); } if (!uri) { @@ -667,6 +667,7 @@ static void client_destroy(Client *c) #ifdef FEATURE_AUTOCMD autocmd_cleanup(c); #endif + shortcut_free(c->config.shortcuts); g_slice_free(Client, c); @@ -693,6 +694,7 @@ static Client *client_new(WebKitWebView *webview) vb.clients = c; c->state.progress = 100; + c->config.shortcuts = shortcut_new(); completion_init(c); map_init(c); diff --git a/src/main.h b/src/main.h index ac23d0d..b282712 100644 --- a/src/main.h +++ b/src/main.h @@ -24,6 +24,7 @@ #include #include #include +#include "shortcut.h" #include "config.h" @@ -238,6 +239,7 @@ struct Client { GdkRGBA comp_bg[COMP_LAST]; PangoFontDescription *comp_font; guint default_zoom; /* default zoom level in percent */ + Shortcut *shortcuts; } config; struct { GSList *list; @@ -248,10 +250,6 @@ struct Client { char showcmd[SHOWCMD_LEN + 1]; /* buffer to show ambiguous key sequence */ guint timeoutlen; /* timeout for ambiguous mappings */ } map; - struct { - GHashTable *table; - char *fallback; /* default shortcut to use if none given in request */ - } shortcut; struct { GHashTable *table; /* holds the protocol handlers */ } handlers; diff --git a/src/setting.c b/src/setting.c index 70e84a2..dfdbed3 100644 --- a/src/setting.c +++ b/src/setting.c @@ -192,10 +192,9 @@ void setting_init(Client *c) #endif /* FEATURE_GUI_STYLE_VIMB2_COMPAT */ /* initialize the shortcuts and set the default shortcuts */ - shortcut_init(c); - shortcut_add(c, "dl", "https://duckduckgo.com/html/?q=$0"); - shortcut_add(c, "dd", "https://duckduckgo.com/?q=$0"); - shortcut_set_default(c, "dl"); + shortcut_add(c->config.shortcuts, "dl", "https://duckduckgo.com/html/?q=$0"); + shortcut_add(c->config.shortcuts, "dd", "https://duckduckgo.com/?q=$0"); + shortcut_set_default(c->config.shortcuts, "dl"); } VbCmdResult setting_run(Client *c, char *name, const char *param) @@ -301,7 +300,6 @@ void setting_cleanup(Client *c) g_hash_table_destroy(c->config.settings); c->config.settings = NULL; } - shortcut_cleanup(c); } static int setting_set_value(Client *c, Setting *prop, void *value, SettingType type) diff --git a/src/shortcut.c b/src/shortcut.c index 2f3de66..356c08d 100644 --- a/src/shortcut.c +++ b/src/shortcut.c @@ -25,42 +25,50 @@ #include "shortcut.h" #include "util.h" +struct shortcut { + GHashTable *table; + char *fallback; /* default shortcut to use if none given in request */ +}; + extern struct Vimb vb; static int get_max_placeholder(const char *str); -static const char *shortcut_lookup(Client *c, const char *string, const char **query); - +static const char *shortcut_lookup(Shortcut *sc, const char *string, const char **query); -void shortcut_init(Client *c) +Shortcut *shortcut_new() { - c->shortcut.table = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free); - c->shortcut.fallback = NULL; + Shortcut *sc = g_slice_new(Shortcut); + sc->table = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free); + sc->fallback = NULL; + + return sc; } -void shortcut_cleanup(Client *c) +void shortcut_free(Shortcut *sc) { - if (c->shortcut.table) { - g_hash_table_destroy(c->shortcut.table); + if (sc->table) { + g_hash_table_destroy(sc->table); } + g_slice_free(Shortcut, sc); } -gboolean shortcut_add(Client *c, const char *key, const char *uri) +gboolean shortcut_add(Shortcut *sc, const char *key, const char *uri) { - g_hash_table_insert(c->shortcut.table, g_strdup(key), g_strdup(uri)); + g_hash_table_insert(sc->table, g_strdup(key), g_strdup(uri)); return TRUE; } -gboolean shortcut_remove(Client *c, const char *key) +gboolean shortcut_remove(Shortcut *sc, const char *key) { - return g_hash_table_remove(c->shortcut.table, key); + return g_hash_table_remove(sc->table, key); } -gboolean shortcut_set_default(Client *c, const char *key) +gboolean shortcut_set_default(Shortcut *sc, const char *key) { /* do not check if the shortcut exists to be able to set the default * before defining the shortcut */ - OVERWRITE_STRING(c->shortcut.fallback, key); + OVERWRITE_STRING(sc->fallback, key); return TRUE; } @@ -69,14 +77,14 @@ gboolean shortcut_set_default(Client *c, const char *key) * Retrieves the uri for given query string. Not that the memory of the * returned uri must be freed. */ -char *shortcut_get_uri(Client *c, const char *string) +char *shortcut_get_uri(Shortcut *sc, const char *string) { const char *tmpl, *query = NULL; char *uri, *quoted_param; int max_num, current_num; GString *token; - tmpl = shortcut_lookup(c, string, &query); + tmpl = shortcut_lookup(sc, string, &query); if (!tmpl) { return NULL; } @@ -157,9 +165,9 @@ char *shortcut_get_uri(Client *c, const char *string) return uri; } -gboolean shortcut_fill_completion(Client *c, GtkListStore *store, const char *input) +gboolean shortcut_fill_completion(Shortcut *sc, GtkListStore *store, const char *input) { - GList *src = g_hash_table_get_keys(c->shortcut.table); + GList *src = g_hash_table_get_keys(sc->table); gboolean found = util_fill_completion(store, input, src); g_list_free(src); @@ -191,23 +199,23 @@ static int get_max_placeholder(const char *str) * pointer with the query part of the given string (everything except of the * shortcut identifier). */ -static const char *shortcut_lookup(Client *c, const char *string, const char **query) +static const char *shortcut_lookup(Shortcut *sc, const char *string, const char **query) { char *p, *uri = NULL; if ((p = strchr(string, ' '))) { char *key = g_strndup(string, p - string); /* is the first word might be a shortcut */ - if ((uri = g_hash_table_lookup(c->shortcut.table, key))) { + if ((uri = g_hash_table_lookup(sc->table, key))) { *query = p + 1; } g_free(key); } else { - uri = g_hash_table_lookup(c->shortcut.table, string); + uri = g_hash_table_lookup(sc->table, string); } - if (!uri && c->shortcut.fallback - && (uri = g_hash_table_lookup(c->shortcut.table, c->shortcut.fallback))) { + if (!uri && sc->fallback + && (uri = g_hash_table_lookup(sc->table, sc->fallback))) { *query = string; } diff --git a/src/shortcut.h b/src/shortcut.h index d3438b4..10de427 100644 --- a/src/shortcut.h +++ b/src/shortcut.h @@ -20,13 +20,15 @@ #ifndef _SHORTCUT_H #define _SHORTCUT_H -void shortcut_init(Client *c); -void shortcut_cleanup(Client *c); -gboolean shortcut_add(Client *c, const char *key, const char *uri); -gboolean shortcut_remove(Client *c, const char *key); -gboolean shortcut_set_default(Client *c, const char *key); -char *shortcut_get_uri(Client *c, const char *key); -gboolean shortcut_fill_completion(Client *c, GtkListStore *store, const char *input); +typedef struct shortcut Shortcut; + +Shortcut *shortcut_new(); +void shortcut_free(Shortcut *sc); +gboolean shortcut_add(Shortcut *sc, const char *key, const char *uri); +gboolean shortcut_remove(Shortcut *sc, const char *key); +gboolean shortcut_set_default(Shortcut *sc, const char *key); +char *shortcut_get_uri(Shortcut *sc, const char *key); +gboolean shortcut_fill_completion(Shortcut *c, GtkListStore *store, const char *input); #endif /* end of include guard: _SHORTCUT_H */ -- 2.20.1