about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorJosh Rickmar <jrick@devio.us>2012-06-14 12:16:15 -0400
committerJosh Rickmar <jrick@devio.us>2012-06-15 12:29:19 -0400
commitc7e96918621b358af82976835b840fc411a7a865 (patch)
tree052a145833f585863b6ddbfcac899d1ac9aca282
parent260f314f514dbd80ba00359a8d8317693553537e (diff)
downloadxombrero-c7e96918621b358af82976835b840fc411a7a865.tar.gz
Add a userstyle setting to change the default stylesheet that is used
instead of the low-contrast one.  Allow the userstyle and
usersyle_global commands to take an optional argument to use a
user-specified stylesheet instead of the default.  Document in the
manpage.
-rw-r--r--settings.c47
-rw-r--r--xombrero.150
-rw-r--r--xombrero.c44
-rw-r--r--xombrero.conf1
-rw-r--r--xombrero.h4
5 files changed, 131 insertions, 15 deletions
diff --git a/settings.c b/settings.c
index eabc2ce..56b8b1c 100644
--- a/settings.c
+++ b/settings.c
@@ -96,6 +96,7 @@ char		*url_regex = NULL;		/* allocated/set at startup */
 char		*encoding = NULL;		/* allocated/set at startup */
 int		autofocus_onload = XT_DS_AUTOFOCUS_ONLOAD;
 int		enable_js_autorun = XT_DS_ENABLE_JS_AUTORUN;
+char		*userstyle = NULL;		/* allocated/set at startup */
 int		userstyle_global = XT_DS_USERSTYLE_GLOBAL;
 int		auto_load_images = XT_DS_AUTO_LOAD_IMAGES;
 int		enable_autoscroll = XT_DS_ENABLE_AUTOSCROLL;
@@ -121,6 +122,7 @@ char		*get_download_mode(struct settings *);
 char		*get_work_dir(struct settings *);
 char		*get_referer(struct settings *);
 char		*get_ssl_ca_file(struct settings *);
+char		*get_userstyle(struct settings *);
 
 int		add_cookie_wl(struct settings *, char *);
 int		add_js_wl(struct settings *, char *);
@@ -184,6 +186,8 @@ int		set_ssl_ca_file_rt(char *);
 int		set_ssl_strict_certs(char *);
 int		set_statusbar_font(char *);
 int		set_url_regex(char *);
+int		set_userstyle(struct settings *, char *);
+int		set_userstyle_rt(char *);
 int		set_userstyle_global(char *);
 int		set_external_editor(char *);
 int		set_xterm_workaround(char *);
@@ -358,6 +362,12 @@ struct special		s_referer = {
 	NULL
 };
 
+struct special		s_userstyle = {
+	set_userstyle,
+	get_userstyle,
+	NULL
+};
+
 struct settings		rs[] = {
 	{ "allow_volatile_cookies",	XT_S_INT, 0,		&allow_volatile_cookies, NULL, NULL, NULL, NULL},
 	{ "autofocus_onload",		XT_S_INT, 0,		&autofocus_onload, NULL, NULL, NULL, set_autofocus_onload },
@@ -409,6 +419,7 @@ struct settings		rs[] = {
 	{ "enable_strict_transport",	XT_S_INT, 0,		&enable_strict_transport, NULL, NULL, NULL, set_enable_strict_transport },
 	{ "statusbar_elems",		XT_S_STR, 0, NULL,	&statusbar_elems, NULL, NULL, NULL },
 	{ "tab_style",			XT_S_STR, 0, NULL, NULL,&s_tab_style, NULL, set_tab_style_rt },
+	{ "userstyle",			XT_S_STR, 0, NULL, NULL,&s_userstyle, NULL, set_userstyle_rt },
 	{ "userstyle_global",		XT_S_INT, 0,		&userstyle_global, NULL, NULL, NULL, set_userstyle_global },
 	{ "url_regex",			XT_S_STR, 0, NULL,	&url_regex, NULL, NULL, set_url_regex },
 	{ "window_height",		XT_S_INT, 0,		&window_height, NULL, NULL, NULL, NULL },
@@ -2120,6 +2131,38 @@ set_url_regex(char *value)
 }
 
 int
+set_userstyle(struct settings *s, char *value)
+{
+	char		script[PATH_MAX] = {'\0'};
+
+	if (userstyle)
+		g_free(userstyle);
+	if (value == NULL || strlen(value) == 0)
+		userstyle = g_strdup_printf("file://%s" PS "style.css",
+		    resource_dir);
+	else {
+		expand_tilde(script, sizeof script, value);
+		userstyle = g_strdup_printf("file://%s", script);
+	}
+	if (stylesheet)
+		g_free(stylesheet);
+	stylesheet = g_strdup(userstyle);
+	return (0);
+}
+
+char *
+get_userstyle(struct settings *s)
+{
+	return (g_strdup(userstyle + strlen("file://")));
+}
+
+int
+set_userstyle_rt(char *value)
+{
+	return (set_userstyle(NULL, value));
+}
+
+int
 set_userstyle_global(char *value)
 {
 	struct karg		args = {0};
@@ -2131,7 +2174,7 @@ set_userstyle_global(char *value)
 			return (0);
 		userstyle_global = 1;
 		args.i = XT_STYLE_GLOBAL;
-		userstyle(get_current_tab(), &args);
+		userstyle_cmd(get_current_tab(), &args);
 	} else {
 		old_style = userstyle_global;
 		tmp = strtonum(value, 0, 1, &errstr);
@@ -2139,7 +2182,7 @@ set_userstyle_global(char *value)
 			return (-1);
 		if (tmp != old_style) {
 			args.i = XT_STYLE_GLOBAL;
-			userstyle(get_current_tab(), &args);
+			userstyle_cmd(get_current_tab(), &args);
 		}
 	}
 	return (0);
diff --git a/xombrero.1 b/xombrero.1
index 1fdc34b..1c73661 100644
--- a/xombrero.1
+++ b/xombrero.1
@@ -405,6 +405,10 @@ Quit
 .Ss Low-Contrast Color Scheme
 These commands toggle the page style between the default CSS and a
 low-contrast color scheme with light grey text on a dark grey background.
+If the
+.Cm userstyle
+setting is changed, that stylesheet will be used instead of the
+low-contrast color scheme.
 .Pp
 .Bl -tag -width Ds -offset indent -compact
 .It Cm s
@@ -669,8 +673,38 @@ permanently adding the top level domain to the persistent whitelist.
 Hide url entry and tool bar.
 .It Cm urlshow , urls
 Show url entry and tool bar.
-.It Cm userstyle
-Toggle between normal and low contrast mode.
+.It Cm userstyle <stylesheet>
+Toggle the current tab between using a custom stylesheet and the
+page's default CSS.
+If there is no stylesheet argument to this command, the stylesheet
+specified by the
+.Cm userstyle
+setting will be used instead.
+This
+.Cm userstyle
+setting is set by default to a low-contrast color scheme.
+If a stylesheet is specified, the current tab will be toggled with
+this stylesheet instead.
+.Pp
+If the
+.Cm userstyle
+setting is left unchanged, this command without any arguments will
+toggle between the default page's CSS and the low-contrast color
+scheme.
+.Pp
+Additional stylesheets may be used in addition to the low-contrast
+color scheme by specifying additonal keybindings.  For example,
+.Pp
+.D1 keybinding = userstyle ~/style.css,v
+.D1 keybinding = userstyle_global ~/style.css,V
+.Pp
+will map toggling between ~/style.css to the v and V keys while
+keeping the low-contrast feature mapped to the s and S keys.
+.It Cm userstyle_global <stylesheet>
+Same as
+.Cm userstyle
+but the change affects all open tabs, and newly created tabs will use
+this style.
 .It Cm w
 Save open tabs to current session.
 The tabs will be restored next time the session is opened.
@@ -1428,8 +1462,18 @@ Set to override the default
 .Nm
 user-agent string. May be specified several times for switching between
 user-agents.
+.It Cm userstyle
+Set the stylesheet to be used as a default for the
+.Cm userstyle
+and
+.Cm userstyle_global
+commands when no extra parameters are given.
+The default is a low-contrast stylesheet.
 .It Cm userstyle_global
-When enabled new tabs will automatically be displayed in low contrast mode.
+When enabled new tabs will automatically be displayed using the custom
+stylesheet specified by the
+.Cm userstyle
+setting.
 .It Cm warn_cert_changes
 When enabled all SSL certificates from HTTPS websites will be
 cached in the
diff --git a/xombrero.c b/xombrero.c
index 743fd3f..5477b93 100644
--- a/xombrero.c
+++ b/xombrero.c
@@ -233,6 +233,7 @@ uint64_t		blocked_cookies = 0;
 char			named_session[PATH_MAX];
 GtkListStore		*completion_model;
 GtkListStore		*buffers_store;
+char			*stylesheet;
 
 char			*qmarks[XT_NOQMARKS];
 int			btn_down;	/* M1 down in any wv */
@@ -1158,32 +1159,56 @@ remove_style(struct tab *t)
 }
 
 int
-userstyle(struct tab *t, struct karg *args)
+userstyle_cmd(struct tab *t, struct karg *args)
 {
+	char			script[PATH_MAX] = {'\0'};
+	char			*script_uri;
 	struct tab		*tt;
 
-	DNPRINTF(XT_D_JS, "userstyle: tab %d\n", t->tab_id);
+	DNPRINTF(XT_D_JS, "userstyle_cmd: tab %d\n", t->tab_id);
+
+	if (args->s != NULL && strlen(args->s)) {
+		expand_tilde(script, sizeof script, args->s);
+		script_uri = g_strdup_printf("file://%s", script);
+	} else
+		script_uri = g_strdup(userstyle);
 
 	switch (args->i) {
 	case XT_STYLE_CURRENT_TAB:
-		if (t->styled)
+		if (t->styled && !strcmp(script_uri, t->stylesheet))
 			remove_style(t);
-		else
+		else {
+			if (t->stylesheet)
+				g_free(t->stylesheet);
+			t->stylesheet = g_strdup(script_uri);
 			apply_style(t);
+		}
 		break;
 	case XT_STYLE_GLOBAL:
-		if (userstyle_global) {
+		if (userstyle_global && !strcmp(script_uri, t->stylesheet)) {
 			userstyle_global = 0;
 			TAILQ_FOREACH(tt, &tabs, entry)
 				remove_style(tt);
 		} else {
 			userstyle_global = 1;
-			TAILQ_FOREACH(tt, &tabs, entry)
+
+			/* need to save this stylesheet for new tabs */
+			if (stylesheet)
+				g_free(stylesheet);
+			stylesheet = g_strdup(script_uri);
+
+			TAILQ_FOREACH(tt, &tabs, entry) {
+				if (tt->stylesheet)
+					g_free(tt->stylesheet);
+				tt->stylesheet = g_strdup(script_uri);
 				apply_style(tt);
+			}
 		}
 		break;
 	}
 
+	g_free(script_uri);
+
 	return (0);
 }
 
@@ -3197,8 +3222,8 @@ struct cmd {
 	{ "hinting_newtab",	0,	hint,			XT_HINT_NEWTAB,		0 },
 
 	/* custom stylesheet */
-	{ "userstyle",		0,	userstyle,		XT_STYLE_CURRENT_TAB,	0 },
-	{ "userstyle_global",	0,	userstyle,		XT_STYLE_GLOBAL,	0 },
+	{ "userstyle",		0,	userstyle_cmd,		XT_STYLE_CURRENT_TAB,	XT_USERARG },
+	{ "userstyle_global",	0,	userstyle_cmd,		XT_STYLE_GLOBAL,	XT_USERARG },
 
 	/* navigation */
 	{ "goback",		0,	navaction,		XT_NAV_BACK,		0 },
@@ -6615,7 +6640,7 @@ create_browser(struct tab *t)
 	} else
 		t->user_agent = g_strdup(user_agent->value);
 
-	t->stylesheet = g_strdup_printf("file://%s/style.css", resource_dir);
+	t->stylesheet = g_strdup(stylesheet);
 	t->load_images = auto_load_images;
 
 	adjustment =
@@ -8104,6 +8129,7 @@ main(int argc, char **argv)
 	spell_check_languages = g_strdup(XT_DS_SPELL_CHECK_LANGUAGES);
 	encoding = g_strdup(XT_DS_ENCODING);
 	spell_check_languages = g_strdup(XT_DS_SPELL_CHECK_LANGUAGES);
+	userstyle = g_strdup_printf("file://%s" PS "style.css", resource_dir);
 
 	/* set statically allocated (struct special) settings */
 	expand_tilde(default_script, sizeof default_script,
diff --git a/xombrero.conf b/xombrero.conf
index 294ed61..c9b81a4 100644
--- a/xombrero.conf
+++ b/xombrero.conf
@@ -56,6 +56,7 @@
 # enable_js_autorun	= 0
 # auto_load_images	= 0
 # enable_localstorage	= 0
+# userstyle		= /usr/local/share/xombrero/style.css
 # userstyle_global	= 1
 # enable_favicon_entry	= 0
 # enable_favicon_tabs	= 1
diff --git a/xombrero.h b/xombrero.h
index 3cfd5df..78e85cb 100644
--- a/xombrero.h
+++ b/xombrero.h
@@ -706,7 +706,7 @@ char		*get_setting_name(int);
 void		statusbar_set_visibility(void);
 int		tabaction(struct tab *, struct karg *);
 int		urlaction(struct tab *, struct karg *);
-int		userstyle(struct tab *, struct karg *);
+int		userstyle_cmd(struct tab *, struct karg *);
 struct tab	*get_current_tab(void);
 int		resizetab(struct tab *, struct karg *);
 int		cert_cmd(struct tab *, struct karg *);
@@ -782,6 +782,8 @@ extern char	*oops_font_name;
 extern char	*statusbar_font_name;
 extern char	*tabbar_font_name;
 extern int	edit_mode;
+extern char	*userstyle;
+extern char	*stylesheet;
 extern int	userstyle_global;
 extern int	auto_load_images;
 extern int	enable_autoscroll;