#include "util.h"
#include "bookmark.h"
#include "shortcut.h"
+#include "handlers.h"
#include "map.h"
#include "js.h"
EX_HARDCOPY,
EX_CMAP,
EX_CNOREMAP,
+ EX_HANDADD,
+ EX_HANDREM,
EX_IMAP,
EX_NMAP,
EX_NNOREMAP,
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);
{"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},
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;
--- /dev/null
+/**
+ * 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;
+}
+
--- /dev/null
+/**
+ * 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 */
#include "dom.h"
#include "hints.h"
#include "shortcut.h"
+#include "handlers.h"
#include "history.h"
#include "session.h"
#include "mode.h"
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) {
#include "config.h"
#include "setting.h"
#include "shortcut.h"
+#include "handlers.h"
#include "util.h"
#include "completion.h"
#include "js.h"
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)
g_hash_table_destroy(settings);
}
shortcut_cleanup();
+ handlers_cleanup();
}
gboolean setting_run(char *name, const char *param)