From: Samuel Archer Date: Wed, 21 May 2014 22:15:20 +0000 (-0400) Subject: Added URI handling on newly spawned windows X-Git-Url: https://git.owens.tech/projects.html/projects.html/git?a=commitdiff_plain;h=f325b3f9815a15697f130b13614461b868052763;p=vimb.git Added URI handling on newly spawned windows --- diff --git a/src/ex.c b/src/ex.c index e4c8306..08e9797 100644 --- a/src/ex.c +++ b/src/ex.c @@ -36,6 +36,7 @@ #include "util.h" #include "bookmark.h" #include "shortcut.h" +#include "handlers.h" #include "map.h" #include "js.h" @@ -46,6 +47,8 @@ typedef enum { EX_HARDCOPY, EX_CMAP, EX_CNOREMAP, + EX_HANDADD, + EX_HANDREM, EX_IMAP, EX_NMAP, EX_NNOREMAP, @@ -123,6 +126,7 @@ static gboolean ex_save(const ExArg *arg); static gboolean ex_set(const ExArg *arg); static gboolean ex_shellcmd(const ExArg *arg); static gboolean ex_shortcut(const ExArg *arg); +static gboolean ex_handlers(const ExArg *arg); static gboolean complete(short direction); static void completion_select(char *match); @@ -142,6 +146,8 @@ static ExInfo commands[] = { {"cmap", EX_CMAP, ex_map, EX_FLAG_LHS|EX_FLAG_RHS}, {"cnoremap", EX_CNOREMAP, ex_map, EX_FLAG_LHS|EX_FLAG_RHS}, {"cunmap", EX_CUNMAP, ex_unmap, EX_FLAG_LHS}, + {"handler-add", EX_HANDADD, ex_handlers, EX_FLAG_RHS}, + {"handler-remove", EX_HANDREM, ex_handlers, EX_FLAG_RHS}, {"hardcopy", EX_HARDCOPY, ex_hardcopy, EX_FLAG_NONE}, {"eval", EX_EVAL, ex_eval, EX_FLAG_RHS}, {"imap", EX_IMAP, ex_map, EX_FLAG_LHS|EX_FLAG_RHS}, @@ -875,6 +881,26 @@ static gboolean ex_shellcmd(const ExArg *arg) return success; } +static gboolean ex_handlers(const ExArg *arg) +{ + char *p; + + switch (arg->code) { + case EX_HANDADD: + if (arg->rhs->len && (p = strchr(arg->rhs->str, '='))) { + *p++ = '\0'; + return handler_add(arg->rhs->str, p); + } + return false; + + case EX_HANDREM: + return handler_remove(arg->rhs->str); + + default: + return false; + } +} + static gboolean ex_shortcut(const ExArg *arg) { char *p; diff --git a/src/handlers.c b/src/handlers.c new file mode 100644 index 0000000..0ecd0b6 --- /dev/null +++ b/src/handlers.c @@ -0,0 +1,84 @@ +/** + * vimb - a webkit based vim like browser. + * + * Copyright (C) 2012-2014 Daniel Carl + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://www.gnu.org/licenses/. + */ + +#include "main.h" +#include "handlers.h" +#include "util.h" + +extern VbCore vb; + +static GHashTable *handlers = NULL; + +static char *handler_lookup(const char *uri); + + +void handlers_init(void) +{ + handlers = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free); +} + +void handlers_cleanup(void) +{ + if (handlers) { + g_hash_table_destroy(handlers); + } +} + +gboolean handler_add(const char *key, const char *uri) +{ + g_hash_table_insert(handlers, g_strdup(key), g_strdup(uri)); + + return true; +} + +gboolean handler_remove(const char *key) +{ + return g_hash_table_remove(handlers, key); +} + +gboolean handle_uri(const char *uri) +{ + char *handler = NULL; + + if (!(handler = handler_lookup(uri))) { + return false; + } + + GError *error = NULL; + char *cmd = g_strdup_printf(handler, uri); + if (!g_spawn_command_line_async(cmd, &error)) { + return false; + } + + return true; +} + +static char *handler_lookup(const char *uri) +{ + char *p, *handler = NULL; + + if ((p = strchr(uri, ':'))) { + *p = '\0'; + handler = g_hash_table_lookup(handlers, uri); + *p = ':'; + } + + return handler; +} + diff --git a/src/handlers.h b/src/handlers.h new file mode 100644 index 0000000..df71b98 --- /dev/null +++ b/src/handlers.h @@ -0,0 +1,29 @@ +/** + * vimb - a webkit based vim like browser. + * + * Copyright (C) 2012-2014 Daniel Carl + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://www.gnu.org/licenses/. + */ + +#ifndef _HANDLERS_H +#define _HANDLERS_H + +void handlers_init(void); +void handlers_cleanup(void); +gboolean handler_add(const char *key, const char *uri); +gboolean handler_remove(const char *key); +gboolean handle_uri(const char *uri); + +#endif /* end of include guard: _HANDLERS_H */ diff --git a/src/main.c b/src/main.c index 3b99f3b..3d1bbd0 100644 --- a/src/main.c +++ b/src/main.c @@ -28,6 +28,7 @@ #include "dom.h" #include "hints.h" #include "shortcut.h" +#include "handlers.h" #include "history.h" #include "session.h" #include "mode.h" @@ -170,6 +171,10 @@ gboolean vb_load_uri(const Arg *arg) path = vb.config.home_page; } + if (handle_uri(path)) { + return true; + } + if (strstr(path, "://") || !strncmp(path, "about:", 6)) { uri = g_strdup(path); } else if (stat(path, &st) == 0) { diff --git a/src/setting.c b/src/setting.c index 682d9e4..75b8fff 100644 --- a/src/setting.c +++ b/src/setting.c @@ -20,6 +20,7 @@ #include "config.h" #include "setting.h" #include "shortcut.h" +#include "handlers.h" #include "util.h" #include "completion.h" #include "js.h" @@ -148,6 +149,10 @@ void setting_init(void) shortcut_add("dl", "https://duckduckgo.com/html/?q=$0"); shortcut_add("dd", "https://duckduckgo.com/?q=$0"); shortcut_set_default("dl"); + + /* initialize the handlers */ + handlers_init(); + handler_add("magnet", "xdg-open %s"); } void setting_cleanup(void) @@ -156,6 +161,7 @@ void setting_cleanup(void) g_hash_table_destroy(settings); } shortcut_cleanup(); + handlers_cleanup(); } gboolean setting_run(char *name, const char *param)