From: Daniel Carl Date: Tue, 16 Oct 2012 22:35:42 +0000 (+0200) Subject: Added command to open url. X-Git-Url: https://git.owens.tech/assets/favicon.png/assets/favicon.png/git?a=commitdiff_plain;h=8c41281a17a6f0c031f7d3c9fef5c46090f893a2;p=vimb.git Added command to open url. --- diff --git a/src/command.c b/src/command.c index 2dbbb77..50f7c27 100644 --- a/src/command.c +++ b/src/command.c @@ -2,28 +2,31 @@ #include "main.h" static CommandInfo cmd_list[] = { - /* command function arg */ - {"normal", vp_set_mode, {VP_MODE_NORMAL, ""}}, - {"input", vp_input, {0, ":"}}, - {"quit", vp_close_browser, {0}}, - {"source", vp_view_source, {0}}, - {"back", vp_navigate, {VP_NAVIG_BACK}}, - {"forward", vp_navigate, {VP_NAVIG_FORWARD}}, - {"reload", vp_navigate, {VP_NAVIG_RELOAD}}, - {"reload!", vp_navigate, {VP_NAVIG_RELOAD_FORCE}}, - {"stop", vp_navigate, {VP_NAVIG_STOP_LOADING}}, - {"jumpleft", vp_scroll, {VP_SCROLL_TYPE_JUMP | VP_SCROLL_DIRECTION_LEFT}}, - {"jumpright", vp_scroll, {VP_SCROLL_TYPE_JUMP | VP_SCROLL_DIRECTION_RIGHT}}, - {"jumptop", vp_scroll, {VP_SCROLL_TYPE_JUMP | VP_SCROLL_DIRECTION_TOP}}, - {"jumpbottom", vp_scroll, {VP_SCROLL_TYPE_JUMP | VP_SCROLL_DIRECTION_DOWN}}, - {"pageup", vp_scroll, {VP_SCROLL_TYPE_SCROLL | VP_SCROLL_DIRECTION_TOP | VP_SCROLL_UNIT_PAGE}}, - {"pagedown", vp_scroll, {VP_SCROLL_TYPE_SCROLL | VP_SCROLL_DIRECTION_DOWN | VP_SCROLL_UNIT_PAGE}}, - {"halfpageup", vp_scroll, {VP_SCROLL_TYPE_SCROLL | VP_SCROLL_DIRECTION_TOP | VP_SCROLL_UNIT_HALFPAGE}}, - {"halfpagedown", vp_scroll, {VP_SCROLL_TYPE_SCROLL | VP_SCROLL_DIRECTION_DOWN | VP_SCROLL_UNIT_HALFPAGE}}, - {"scrollleft", vp_scroll, {VP_SCROLL_TYPE_SCROLL | VP_SCROLL_DIRECTION_LEFT | VP_SCROLL_UNIT_LINE}}, - {"scrollright", vp_scroll, {VP_SCROLL_TYPE_SCROLL | VP_SCROLL_DIRECTION_RIGHT | VP_SCROLL_UNIT_LINE}}, - {"scrollup", vp_scroll, {VP_SCROLL_TYPE_SCROLL | VP_SCROLL_DIRECTION_TOP | VP_SCROLL_UNIT_LINE}}, - {"scrolldown", vp_scroll, {VP_SCROLL_TYPE_SCROLL | VP_SCROLL_DIRECTION_DOWN | VP_SCROLL_UNIT_LINE}}, + /* command function arg */ + {"normal", vp_set_mode, {VP_MODE_NORMAL, ""}}, + {"open", vp_open, {VP_MODE_NORMAL, ""}}, + {"input", vp_input, {0, ":"}}, + {"inputopen", vp_input, {0, ":open "}}, + {"inputopencurrent", vp_input, {VP_INPUT_CURRENT_URI, ":open "}}, + {"quit", vp_close_browser, {0}}, + {"source", vp_view_source, {0}}, + {"back", vp_navigate, {VP_NAVIG_BACK}}, + {"forward", vp_navigate, {VP_NAVIG_FORWARD}}, + {"reload", vp_navigate, {VP_NAVIG_RELOAD}}, + {"reload!", vp_navigate, {VP_NAVIG_RELOAD_FORCE}}, + {"stop", vp_navigate, {VP_NAVIG_STOP_LOADING}}, + {"jumpleft", vp_scroll, {VP_SCROLL_TYPE_JUMP | VP_SCROLL_DIRECTION_LEFT}}, + {"jumpright", vp_scroll, {VP_SCROLL_TYPE_JUMP | VP_SCROLL_DIRECTION_RIGHT}}, + {"jumptop", vp_scroll, {VP_SCROLL_TYPE_JUMP | VP_SCROLL_DIRECTION_TOP}}, + {"jumpbottom", vp_scroll, {VP_SCROLL_TYPE_JUMP | VP_SCROLL_DIRECTION_DOWN}}, + {"pageup", vp_scroll, {VP_SCROLL_TYPE_SCROLL | VP_SCROLL_DIRECTION_TOP | VP_SCROLL_UNIT_PAGE}}, + {"pagedown", vp_scroll, {VP_SCROLL_TYPE_SCROLL | VP_SCROLL_DIRECTION_DOWN | VP_SCROLL_UNIT_PAGE}}, + {"halfpageup", vp_scroll, {VP_SCROLL_TYPE_SCROLL | VP_SCROLL_DIRECTION_TOP | VP_SCROLL_UNIT_HALFPAGE}}, + {"halfpagedown", vp_scroll, {VP_SCROLL_TYPE_SCROLL | VP_SCROLL_DIRECTION_DOWN | VP_SCROLL_UNIT_HALFPAGE}}, + {"scrollleft", vp_scroll, {VP_SCROLL_TYPE_SCROLL | VP_SCROLL_DIRECTION_LEFT | VP_SCROLL_UNIT_LINE}}, + {"scrollright", vp_scroll, {VP_SCROLL_TYPE_SCROLL | VP_SCROLL_DIRECTION_RIGHT | VP_SCROLL_UNIT_LINE}}, + {"scrollup", vp_scroll, {VP_SCROLL_TYPE_SCROLL | VP_SCROLL_DIRECTION_TOP | VP_SCROLL_UNIT_LINE}}, + {"scrolldown", vp_scroll, {VP_SCROLL_TYPE_SCROLL | VP_SCROLL_DIRECTION_DOWN | VP_SCROLL_UNIT_LINE}}, }; void command_init() @@ -36,7 +39,7 @@ void command_init() } } -gboolean command_run(const gchar* name) +gboolean command_run(const gchar* name, const gchar* param) { CommandInfo* c = NULL; Arg a; @@ -45,7 +48,7 @@ gboolean command_run(const gchar* name) return FALSE; } a.i = c->arg.i; - a.s = g_strdup(c->arg.s); + a.s = g_strdup(param ? param : c->arg.s); c->function(&a); g_free(a.s); diff --git a/src/command.h b/src/command.h index f4bd15b..5cd3942 100644 --- a/src/command.h +++ b/src/command.h @@ -14,6 +14,6 @@ typedef struct { void command_init(void); -gboolean command_run(const gchar* name); +gboolean command_run(const gchar* name, const gchar* param); #endif /* end of include guard: COMMAND_H */ diff --git a/src/keybind.c b/src/keybind.c index 5b52b4c..3aa00e4 100644 --- a/src/keybind.c +++ b/src/keybind.c @@ -106,7 +106,7 @@ static gboolean keybind_keypress_callback(WebKitWebView* webview, GdkEventKey* e if (link) { Keybind* keybind = (Keybind*)link->data; - command_run(keybind->command); + command_run(keybind->command, NULL); /* if key binding used, remove the modkey */ vp.state.modkey = vp.state.count = 0; diff --git a/src/main.c b/src/main.c index 9b05ee1..b875a91 100644 --- a/src/main.c +++ b/src/main.c @@ -23,6 +23,7 @@ static void vp_init_gui(void); static void vp_set_widget_font(GtkWidget* widget, const gchar* font_definition, const gchar* bg_color, const gchar* fg_color); static void vp_setup_settings(void); static void vp_setup_signals(void); +static gboolean vp_load_uri(const Arg* arg); static void vp_webview_load_status_cb(WebKitWebView* view, GParamSpec* pspec, gpointer user_data) { @@ -107,33 +108,48 @@ static gboolean vp_inputbox_keyrelease_cb(GtkEntry *entry, GdkEventKey *event) static gboolean vp_process_input(const char* input) { gboolean success; - gchar* command = g_strdup(input); + gchar* line = g_strdup(input); + gchar** token; - g_strstrip(command); - size_t length = strlen(command); - if (0 == length) { + if (!input || !strlen(input)) { return FALSE; } - success = command_run(command); - g_free(command); + g_strstrip(line); + + /* split the input string into command and parameter part */ + token = g_strsplit(line, " ", 2); + g_free(line); + + if (!token[0]) { + g_strfreev(token); + return FALSE; + } + success = command_run(token[0], token[1] ? token[1] : NULL); + g_strfreev(token); return success; } -gboolean vp_load_uri(const Arg* arg) +static gboolean vp_load_uri(const Arg* arg) { - char* u; - const char* uri = arg->s; + char* uri; + char* line = arg->s; + + if (!line) { + return FALSE; + } - if (strcmp(uri, "") == 0) { + g_strstrip(line); + if (!strlen(line)) { return FALSE; } - u = g_strrstr(uri, "://") ? g_strdup(uri) : g_strdup_printf("http://%s", uri); + + uri = g_strrstr(line, "://") ? g_strdup(line) : g_strdup_printf("http://%s", line); /* Load a web page into the browser instance */ - webkit_web_view_load_uri(vp.gui.webview, u); - g_free(u); + webkit_web_view_load_uri(vp.gui.webview, uri); + g_free(uri); /* change state to normal mode */ Arg a = {VP_MODE_NORMAL}; @@ -242,6 +258,7 @@ void vp_set_mode(const Arg* arg) void vp_input(const Arg* arg) { gint pos = 0; + const gchar* url; /* reset the colors and fonts to defalts */ vp_set_widget_font(vp.gui.inputbox, inputbox_font[0], inputbox_bg[0], inputbox_fg[0]); @@ -249,14 +266,26 @@ void vp_input(const Arg* arg) /* remove content from input box */ gtk_entry_set_text(GTK_ENTRY(vp.gui.inputbox), ""); - /* insert text */ + /* insert string from arg */ gtk_editable_insert_text(GTK_EDITABLE(vp.gui.inputbox), arg->s, -1, &pos); + + /* add current url if requested */ + if (VP_INPUT_CURRENT_URI == arg->i + && (url = webkit_web_view_get_uri(vp.gui.webview))) { + gtk_editable_insert_text(GTK_EDITABLE(vp.gui.inputbox), url, -1, &pos); + } + gtk_editable_set_position(GTK_EDITABLE(vp.gui.inputbox), -1); Arg a = {VP_MODE_COMMAND}; vp_set_mode(&a); } +void vp_open(const Arg* arg) +{ + vp_load_uri(arg); +} + void vp_update_urlbar(const gchar* uri) { gchar* markup; @@ -309,7 +338,6 @@ void vp_echo(const MessageType type, const gchar *message) gtk_entry_set_text(GTK_ENTRY(vp.gui.inputbox), message); } - static void vp_print_version(void) { fprintf(stderr, "%s/%s (build %s %s)\n", VERSION, PROJECT, __DATE__, __TIME__); @@ -327,6 +355,8 @@ static void vp_init(void) /* 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_o, "inputopen"); + keybind_add(VP_MODE_NORMAL, 0, 0, GDK_O, "inputopencurrent"); keybind_add(VP_MODE_NORMAL, 0, 0, GDK_d, "quit"); keybind_add(VP_MODE_NORMAL, 0, GDK_CONTROL_MASK, GDK_o, "back"); keybind_add(VP_MODE_NORMAL, 0, GDK_CONTROL_MASK, GDK_i, "forward"); diff --git a/src/main.h b/src/main.h index 68e913d..5aebf64 100644 --- a/src/main.h +++ b/src/main.h @@ -40,6 +40,10 @@ enum { VP_NAVIG_STOP_LOADING }; +enum { + VP_INPUT_CURRENT_URI = 1 +}; + /* 1 << 0: 0 = jump, 1 = scroll 1 << 1: 0 = vertical, 1 = horizontal @@ -129,12 +133,12 @@ extern VpCore vp; void vp_update_statusbar(void); void vp_update_urlbar(const gchar* uri); void vp_echo(const MessageType type, const gchar *message); -gboolean vp_load_uri(const Arg* arg); void vp_navigate(const Arg* arg); void vp_scroll(const Arg* arg); void vp_close_browser(const Arg* arg); void vp_view_source(const Arg* arg); void vp_set_mode(const Arg* arg); void vp_input(const Arg* arg); +void vp_open(const Arg* arg); #endif /* end of include guard: MAIN_H */