From: Daniel Carl Date: Tue, 16 Oct 2012 19:48:18 +0000 (+0200) Subject: Added function to delete previous set keybindings. X-Git-Url: https://git.owens.tech/assets/favicon.png/assets/favicon.png/git?a=commitdiff_plain;h=a1b65fe6ed31aee5934daacc0c458df537baf33f;p=vimb.git Added function to delete previous set keybindings. --- diff --git a/src/keybind.c b/src/keybind.c index 2d02485..270d66b 100644 --- a/src/keybind.c +++ b/src/keybind.c @@ -5,6 +5,7 @@ static GSList* keys = NULL; static GString* modkeys = NULL; +static GSList* keybind_find(int mode, guint modkey, guint modmask, guint keyval); static gboolean keybind_keypress_callback(WebKitWebView* webview, GdkEventKey* event); @@ -16,7 +17,7 @@ void keybind_init(void) void keybind_add(int mode, guint modkey, guint modmask, guint keyval, const gchar* command) { - struct _keybind_key* keybind = g_new0(struct _keybind_key, 1); + Keybind* keybind = g_new0(Keybind, 1); keybind->mode = mode; keybind->modkey = modkey; @@ -32,9 +33,34 @@ void keybind_add(int mode, guint modkey, guint modmask, guint keyval, const gcha } } +void keybind_remove(int mode, guint modkey, guint modmask, guint keyval) +{ + GSList* link = keybind_find(mode, modkey, modmask, keyval); + if (link) { + keys = g_slist_delete_link(keys, link); + } + /* TODO remove eventually no more used modkeys */ +} + +GSList* keybind_find(int mode, guint modkey, guint modmask, guint keyval) +{ + GSList* link; + for (link = keys; link != NULL; link = link->next) { + Keybind* keybind = (Keybind*)link->data; + + if (keybind->mode == mode + && keybind->modmask == modmask + && keybind->modkey == modkey + && keybind->keyval == keyval) { + return link; + } + } + + return NULL; +} + static gboolean keybind_keypress_callback(WebKitWebView* webview, GdkEventKey* event) { - GSList* tmp; GdkModifierType irrelevant; guint keyval; static GdkKeymap *keymap; @@ -73,25 +99,20 @@ static gboolean keybind_keypress_callback(WebKitWebView* webview, GdkEventKey* e } } } - /* TODO move to own function */ + /* check for keybinding */ - for (tmp = keys; tmp != NULL; tmp = tmp->next) { - struct _keybind_key* keybind = (struct _keybind_key*)tmp->data; + GSList* link = keybind_find(vp.state.mode, vp.state.modkey, + (CLEAN(event->state) & ~irrelevant), keyval); - /* handle key presses */ - if (keybind->mode == vp.state.mode - && keybind->modmask == (CLEAN(event->state) & ~irrelevant) - && keybind->modkey == vp.state.modkey - && keybind->keyval == keyval - && keybind->command) { - command_run(keybind->command); + if (link) { + Keybind* keybind = (Keybind*)link->data; + command_run(keybind->command); - /* if key binding used, remove the modkey */ - vp.state.modkey = vp.state.count = 0; - vp_update_statusbar(); + /* if key binding used, remove the modkey */ + vp.state.modkey = vp.state.count = 0; + vp_update_statusbar(); - return TRUE; - } + return TRUE; } return FALSE; diff --git a/src/keybind.h b/src/keybind.h index 1be98de..6866dab 100644 --- a/src/keybind.h +++ b/src/keybind.h @@ -12,15 +12,16 @@ #define IS_ESCAPE(event) (IS_ESCAPE_KEY(CLEAN(event->state), event->keyval)) #define IS_ESCAPE_KEY(s, k) ((s == 0 && k == GDK_Escape) || (s == GDK_CONTROL_MASK && k == GDK_c)) -struct _keybind_key { +typedef struct { int mode; /* mode maks for allowed browser modes */ guint modkey; guint modmask; /* modemask for the kayval */ guint keyval; gchar* command; /* command to run */ -}; +} Keybind; void keybind_init(void); void keybind_add(int mode, guint modkey, guint modmask, guint keyval, const gchar* command); +void keybind_remove(int mode, guint modkey, guint modmask, guint keyval); #endif /* end of include guard: KEYBIND_H */ diff --git a/src/main.c b/src/main.c index 2116c1f..9b05ee1 100644 --- a/src/main.c +++ b/src/main.c @@ -136,8 +136,8 @@ gboolean vp_load_uri(const Arg* arg) g_free(u); /* change state to normal mode */ - vp.state.mode = VP_MODE_NORMAL; - vp_update_statusbar(); + Arg a = {VP_MODE_NORMAL}; + vp_set_mode(&a); return TRUE; } @@ -324,6 +324,7 @@ static void vp_init(void) /* initialize the keybindings */ keybind_init(); + /* TODO read the key bindings from config file */ keybind_add(VP_MODE_NORMAL, GDK_g, 0, GDK_f, "source"); keybind_add(VP_MODE_NORMAL, 0, 0, GDK_colon, "input"); keybind_add(VP_MODE_NORMAL, 0, 0, GDK_d, "quit");