Added --cmd,-C option to give ex commands on startup #342.
authorDaniel Carl <danielcarl@gmx.de>
Wed, 18 Dec 2019 22:29:29 +0000 (23:29 +0100)
committerDaniel Carl <danielcarl@gmx.de>
Wed, 18 Dec 2019 22:30:45 +0000 (23:30 +0100)
CHANGELOG.md
doc/vimb.1
src/main.c
src/main.h

index 3204cde..5c02c9a 100644 (file)
@@ -18,6 +18,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
   user to permit or disable geolcation requests (Thanks to Alva).
 * Setting `dark-mode` to switch the webview into dark mode, which might be
   picked up by pages media query to setup dark styling (Thanks to Alva).
+* Option `--cmd, -C` to run ex commands on startup.
 ### Changed
 ### Fixed
 ### Removed
index 9b1550a..5d62936 100644 (file)
@@ -25,6 +25,17 @@ If \fIURI\fP is '-', Vimb reads the HTML to display from stdin.
 .P
 Mandatory arguments to long options are mandatory for short options too.
 .TP
+.BI "\-C, \-\-cmd " "CMD"
+Run \fICMD\fP as ex command line right before the first page is loaded.
+If the flag is used more than one time, the commands are called in order they
+are given.
+You could also pass several ex commands in one \fICMD\fP,
+if they are separated by "|".
+.sp
+.EX
+vimb --cmd "set dark-mode=on|set header=Referer,DNT=1"
+.EE
+.TP
 .BI "\-c, \-\-config " "FILE"
 Use custom configuration given as \fIFILE\fP.
 This will also be applied on new spawned instances.
index ff1817b..43b470a 100644 (file)
@@ -118,6 +118,8 @@ static void on_script_message_focus(WebKitUserContentManager *manager,
         WebKitJavascriptResult *res, gpointer data);
 static gboolean profileOptionArgFunc(const gchar *option_name,
         const gchar *value, gpointer data, GError **error);
+static gboolean autocmdOptionArgFunc(const gchar *option_name,
+        const gchar *value, gpointer data, GError **error);
 
 struct Vimb vb;
 
@@ -1029,7 +1031,8 @@ static void spawn_new_instance(const char *uri)
 #endif
         + (vb.incognito ? 1 : 0)
         + (vb.profile ? 2 : 0)
-        + (vb.no_maximize ? 1 : 0),
+        + (vb.no_maximize ? 1 : 0)
+        + g_slist_length(vb.cmdargs) * 2,
         sizeof(char *)
     );
 
@@ -1057,6 +1060,10 @@ static void spawn_new_instance(const char *uri)
     if (vb.no_maximize) {
         cmd[i++] = "--no-maximize";
     }
+    for (GSList *l = vb.cmdargs; l; l = l->next) {
+        cmd[i++] = "-C";
+        cmd[i++] = l->data;
+    }
     cmd[i++] = (char*)uri;
     cmd[i++] = NULL;
 
@@ -1786,6 +1793,8 @@ static void vimb_cleanup(void)
         }
     }
     g_free(vb.profile);
+
+    g_slist_free_full(vb.cmdargs, g_free);
 }
 #endif
 
@@ -2085,6 +2094,13 @@ static gboolean profileOptionArgFunc(const gchar *option_name,
     return TRUE;
 }
 
+static gboolean autocmdOptionArgFunc(const gchar *option_name,
+        const gchar *value, gpointer data, GError **error)
+{
+    vb.cmdargs = g_slist_append(vb.cmdargs, g_strdup(value));
+    return TRUE;
+}
+
 int main(int argc, char* argv[])
 {
     Client *c;
@@ -2093,6 +2109,7 @@ int main(int argc, char* argv[])
     gboolean ver = FALSE, buginfo = FALSE;
 
     GOptionEntry opts[] = {
+        {"cmd", 'C', 0, G_OPTION_ARG_CALLBACK, (GOptionArgFunc*)autocmdOptionArgFunc, "Ex command run before first page is loaded", NULL},
         {"config", 'c', 0, G_OPTION_ARG_FILENAME, &vb.configfile, "Custom configuration file", NULL},
         {"embed", 'e', 0, G_OPTION_ARG_STRING, &winid, "Reparents to window specified by xid", NULL},
         {"incognito", 'i', 0, G_OPTION_ARG_NONE, &vb.incognito, "Run with user data read-only", NULL},
@@ -2164,6 +2181,11 @@ int main(int argc, char* argv[])
 
     c = client_new(NULL);
     client_show(NULL, c);
+
+    /* process the --cmd if this was given */
+    for (GSList *l = vb.cmdargs; l; l = l->next) {
+        ex_run_string(c, l->data, false);
+    }
     if (argc <= 1) {
         vb_load_uri(c, &(Arg){TARGET_CURRENT, NULL});
     } else if (!strcmp(argv[argc - 1], "-")) {
index 6759ca4..dee3f77 100644 (file)
@@ -277,6 +277,7 @@ struct Vimb {
     char        *files[FILES_LAST];
     FileStorage *storage[STORAGE_LAST];
     char        *profile;           /* profile name */
+    GSList      *cmdargs;           /* ex commands given asl --command, -C option */
     struct {
         guint   history_max;
         guint   closed_max;