*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:
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:
#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)
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)
#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;
}
* 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;
}
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);
* 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;
}
#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 */