From: Daniel Carl Date: Fri, 29 Mar 2013 23:49:56 +0000 (+0100) Subject: Allow to run multiple command together. X-Git-Url: https://git.owens.tech/assets/static/git-favicon.png/assets/static/git-favicon.png/git?a=commitdiff_plain;h=59a46f7bbf5f5e571f6abe5876d3b8d9a25d197c;p=vimb.git Allow to run multiple command together. For example following will work :run set input-bg-normal=#000 | set input-fg-normal=#fff | 5pagedown. --- diff --git a/doc/vimb.1.txt b/doc/vimb.1.txt index 797e4ce..eaa8f6c 100644 --- a/doc/vimb.1.txt +++ b/doc/vimb.1.txt @@ -2,12 +2,12 @@ .\" groff -man -Tascii vimb.1 .TH PROJECT 1 "23/03/2013" "PROJECT/VERSION" "Vimb Manual" .SH NAME -PROJECT \- A modal web browser based on webkit thats inspired by vim the great editor. +PROJECT \- Vim Browser - A modal web browser based on webkit thats inspired by +vim the great editor. .SH SYNOPSIS .BI "PROJECT [" "OPTION" "] [" "URI" "]" .SH DESCRIPTION -.B PROJECT -is a webkit based web browser that behaves like the vimperator +PROJECT is a webkit based web browser that behaves like the vimperator plugin for the firefox and usage paradigms from the great editor vim. The goal of PROJECT is to build a completely keyboard-driven, efficient and pleasurable browsing-experience. @@ -62,6 +62,7 @@ bound to the keybinding. For example the command "hint-focus-next" is only available in hinting mode. To call it, it has to be bound to a key like "hmap =hint-focus-next" to be used. + .SS Open .TP .BI "open [" URI ] @@ -296,6 +297,20 @@ history items. A command is not a internal command, but every string entered into inputbox that begins with \fI[:/?]\fP. So the history contains real commands and search queries. .SS Misc +.TP +.BI run " [COMMAND LIST]" +Run is a command, that was introduced to have the ability to run multiple +other commands with a single call. Everything after the `run' is interpreted +as a `|' seperated list of commands and parameters. +":run [count]command[ param[=value]]|[count]command[ param[=value]]|..." + +The run command allows to use fancy keybindings that set several config +settings with only on keypress. + +Example: + +:run set input-bg-normal=#ff0 | set input-fg-normal=#f0f | 5pagedown + .TP .BI [ N "]search-forward, [" N "]search-backward" Search in current page forward or backward. diff --git a/src/command.c b/src/command.c index d46e6ea..8a1cf97 100644 --- a/src/command.c +++ b/src/command.c @@ -96,6 +96,7 @@ static CommandInfo cmd_list[] = { {"zoomreset", command_zoom, {COMMAND_ZOOM_RESET}}, {"hist-next", command_history, {0}}, {"hist-prev", command_history, {1}}, + {"run", command_run_multi, {0}}, }; @@ -141,6 +142,74 @@ gboolean command_run(const char *name, const char *param) return result; } +/** + * Runs a single command form string containing the command an possible + * parameters. + */ +gboolean command_run_string(const char *input) +{ + gboolean success; + char *command = NULL, *str, **token; + + if (!input || *input == '\0') { + return FALSE; + } + + str =g_strdup(input); + g_strstrip(str); + + /* get a possible command count */ + vb.state.count = g_ascii_strtoll(str, &command, 10); + + /* split the input string into command and parameter part */ + token = g_strsplit(command, " ", 2); + g_free(str); + + if (!token[0]) { + g_strfreev(token); + return FALSE; + } + success = command_run(token[0], token[1] ? token[1] : NULL); + g_strfreev(token); + + return success; +} + +/** + * Runs multiple commands that are seperated by |. + */ +gboolean command_run_multi(const Arg *arg) +{ + gboolean result = TRUE; + char **commands, *input; + unsigned int len, i; + + if (!arg->s || *(arg->s) == '\0') { + return FALSE; + } + + input = g_strdup(arg->s); + g_strstrip(input); + + /* splits the commands */ + commands = g_strsplit(input, "|", 0); + g_free(input); + + len = g_strv_length(commands); + if (!len) { + g_strfreev(commands); + return FALSE; + } + + for (i = 0; i < len; i++) { + /* run the single commands */ + result = (result && command_run_string(commands[i])); + } + g_strfreev(commands); + + return result; +} + gboolean command_open(const Arg *arg) { return vb_load_uri(arg); diff --git a/src/command.h b/src/command.h index d952cd8..9081299 100644 --- a/src/command.h +++ b/src/command.h @@ -47,7 +47,9 @@ void command_init(void); void command_cleanup(void); gboolean command_exists(const char *name); gboolean command_run(const char *name, const char *param); +gboolean command_run_string(const char *input); +gboolean command_run_multi(const Arg *arg); gboolean command_open(const Arg *arg); gboolean command_open_home(const Arg *arg); gboolean command_open_closed(const Arg *arg); diff --git a/src/main.c b/src/main.c index 53d0d55..ceb1fe3 100644 --- a/src/main.c +++ b/src/main.c @@ -64,7 +64,6 @@ static void vb_request_start_cb(WebKitWebView *webview, WebKitWebFrame *frame, WebKitNetworkResponse *response); /* functions */ -static gboolean vb_process_input(const char *input); static void vb_run_user_script(WebKitWebFrame *frame); static char *vb_jsref_to_string(JSContextRef context, JSValueRef ref); static void vb_init_core(void); @@ -474,7 +473,7 @@ static void vb_inputbox_activate_cb(GtkEntry *entry) case ':': completion_clean(); - vb_process_input((command + 1)); + command_run_string((command + 1)); history_add(HISTORY_COMMAND, command + 1); break; } @@ -565,35 +564,6 @@ static void vb_inspector_finished(WebKitWebInspector *inspector) g_free(vb.gui.inspector); } -/** - * Processed input from input box without trailing : or ? /, input from config - * file and default config. - */ -static gboolean vb_process_input(const char *input) -{ - gboolean success; - char *command = NULL, **token; - - if (!input || !strlen(input)) { - return FALSE; - } - - /* get a possible command count */ - vb.state.count = g_ascii_strtoll(input, &command, 10); - - /* split the input string into command and parameter part */ - token = g_strsplit(command, " ", 2); - - if (!token[0]) { - g_strfreev(token); - return FALSE; - } - success = command_run(token[0], token[1] ? token[1] : NULL); - g_strfreev(token); - - return success; -} - #ifdef FEATURE_COOKIE static void vb_set_cookie(SoupCookie *cookie) { @@ -774,7 +744,7 @@ static void vb_read_config(void) /* load default config */ for (guint i = 0; default_config[i].command != NULL; i++) { - if (!vb_process_input(default_config[i].command)) { + if (!command_run_string(default_config[i].command)) { fprintf(stderr, "Invalid default config: %s\n", default_config[i].command); } } @@ -791,7 +761,7 @@ static void vb_read_config(void) if (!g_ascii_isalpha(line[0])) { continue; } - if (!vb_process_input(line)) { + if (!command_run_string(line)) { fprintf(stderr, "Invalid config: %s\n", line); } }