From 1d81da727b1c6b9531775c2720b1495f9640aed0 Mon Sep 17 00:00:00 2001 From: Daniel Carl Date: Wed, 18 Dec 2019 23:29:29 +0100 Subject: [PATCH] Added --cmd,-C option to give ex commands on startup #342. --- CHANGELOG.md | 1 + doc/vimb.1 | 11 +++++++++++ src/main.c | 24 +++++++++++++++++++++++- src/main.h | 1 + 4 files changed, 36 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3204cde..5c02c9a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/doc/vimb.1 b/doc/vimb.1 index 9b1550a..5d62936 100644 --- a/doc/vimb.1 +++ b/doc/vimb.1 @@ -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. diff --git a/src/main.c b/src/main.c index ff1817b..43b470a 100644 --- a/src/main.c +++ b/src/main.c @@ -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], "-")) { diff --git a/src/main.h b/src/main.h index 6759ca4..dee3f77 100644 --- a/src/main.h +++ b/src/main.h @@ -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; -- 2.20.1