Allow to run multiple command together.
authorDaniel Carl <danielcarl@gmx.de>
Fri, 29 Mar 2013 23:49:56 +0000 (00:49 +0100)
committerDaniel Carl <danielcarl@gmx.de>
Sat, 30 Mar 2013 13:55:11 +0000 (14:55 +0100)
For example following will work
:run set input-bg-normal=#000 | set input-fg-normal=#fff | 5pagedown.

doc/vimb.1.txt
src/command.c
src/command.h
src/main.c

index 797e4ce..eaa8f6c 100644 (file)
@@ -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 <tab>=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.
index d46e6ea..8a1cf97 100644 (file)
@@ -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);
index d952cd8..9081299 100644 (file)
@@ -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);
index 53d0d55..ceb1fe3 100644 (file)
@@ -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);
             }
         }