about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorMarco Peereboom <marco@conformal.com>2013-01-02 11:33:44 -0600
committerMarco Peereboom <marco@conformal.com>2013-01-02 11:33:44 -0600
commitee114622ce724ea97664c2cf5b7e1cbf54621b1f (patch)
tree2968bf0cc4487dfda7ca424ef7c867db7340da20
parent5f5c997e16964a8ab297a656595c46ea4e5ef32d (diff)
downloadxombrero-ee114622ce724ea97664c2cf5b7e1cbf54621b1f.tar.gz
add urlmod plus and min
-rw-r--r--settings.c2
-rw-r--r--xombrero.c90
-rw-r--r--xombrero.h5
3 files changed, 97 insertions, 0 deletions
diff --git a/settings.c b/settings.c
index 0437577..98fd74c 100644
--- a/settings.c
+++ b/settings.c
@@ -1449,6 +1449,8 @@ struct key_binding	keys[] = {
 	{ "editelement",	CTRL,	1,	GDK_i		},
 	{ "passthrough",	CTRL,	1,	GDK_z		},
 	{ "modurl",		CTRL,	1,	GDK_Return	},
+	{ "urlmod plus",	MOD1,	1,	GDK_a		},
+	{ "urlmod min",		MOD1,	1,	GDK_A		},
 
 	/* search */
 	{ "searchnext",		0,	0,	GDK_n		},
diff --git a/xombrero.c b/xombrero.c
index 153b531..15670ea 100644
--- a/xombrero.c
+++ b/xombrero.c
@@ -3084,6 +3084,91 @@ proxy_cmd(struct tab *t, struct karg *args)
 done:
 	return (XT_CB_PASSTHROUGH);
 }
+
+/*
+ * If you can read this functionthen you are a sick and twisted individual.
+ * I hope we never meet, it'll be violent.
+ */
+gboolean
+eval_cb(const GMatchInfo *info, GString *res, gpointer data)
+{
+	gchar			*match, *num;
+	gint			start = -1, end = -1, i;
+	struct karg		*args = data;
+
+	/*
+	 * match contains the string UP TO the match.
+	 *
+	 * res is what is returned, note that whatever remains in the sent in
+	 * string is appended on the way out.
+	 *
+	 * for example /123/456/789/moo came in
+	 * match contains /123/456/789/
+	 * we assign that to res and replace /789/ with the replacement text
+	 * then g_regex_replace_eval on the way out has /123/456/replacement/moo
+	 *
+	 */
+	match = g_match_info_fetch(info, 0);
+	if (match == NULL)
+		goto done;
+
+	if (g_match_info_fetch_pos(info, 1, &start, &end) == FALSE)
+		goto freeit;
+
+	g_string_assign(res, match);
+
+	i = atoi(&match[start + 1]);
+	if (args->i == XT_URL_PLUS)
+		i++;
+	else
+		i--;
+
+	/* preserve whitespace when likely */
+	num = g_strdup_printf("%0*d",  end - start - 2, i);
+	g_string_overwrite_len(res, start + 1, num, end - start - 2);
+	g_free(num);
+
+freeit:
+	g_free(match);
+done:
+	return (FALSE); /* doesn't matter */
+}
+
+int
+urlmod_cmd(struct tab *t, struct karg *args)
+{
+	const gchar		*uri;
+	GRegex			*reg;
+	gchar			*res;
+
+	if (t == NULL)
+		goto done;
+	if ((uri = gtk_entry_get_text(GTK_ENTRY(t->uri_entry))) == NULL)
+		goto done;
+	if (strlen(uri) == 0)
+		goto done;
+
+	reg = g_regex_new(".*(/[0-9]+/)", 0, 0, NULL);
+	if (reg == NULL)
+		goto done;
+	res = g_regex_replace_eval(reg, uri, -1, 0, 0, eval_cb, args, NULL);
+	if (res == NULL)
+		goto free_reg;
+
+	if (!strcmp(res, uri))
+		goto free_res;
+
+	gtk_entry_set_text(GTK_ENTRY(t->uri_entry), res);
+	activate_uri_entry_cb(t->uri_entry, t);
+
+free_res:
+	g_free(res);
+free_reg:
+	g_regex_unref(reg);
+done:
+	return (XT_CB_PASSTHROUGH);
+}
+
 struct cmd {
 	char		*cmd;
 	int		level;
@@ -3292,6 +3377,11 @@ struct cmd {
 	{ "proxy",		0,	proxy_cmd,		XT_PRXY_SHOW,		0 },
 	{ "show",		1,	proxy_cmd,		XT_PRXY_SHOW,		0 },
 	{ "toggle",		1,	proxy_cmd,		XT_PRXY_TOGGLE,		0 },
+
+	/* url mod */
+	{ "urlmod",		0,	urlmod_cmd,		XT_URL,			0 },
+	{ "plus",		1,	urlmod_cmd,		XT_URL_PLUS,		0 },
+	{ "min",		1,	urlmod_cmd,		XT_URL_MIN,		0 },
 };
 
 struct {
diff --git a/xombrero.h b/xombrero.h
index 8e0e505..381ed95 100644
--- a/xombrero.h
+++ b/xombrero.h
@@ -439,6 +439,11 @@ int			edit_element(struct tab *t, struct karg *a);
 #define XT_PRXY_SHOW		(1<<0)
 #define XT_PRXY_TOGGLE		(1<<1)
 
+/* url modify */
+#define XT_URL			(1<<0)
+#define XT_URL_PLUS		(1<<1)
+#define XT_URL_MIN		(1<<2)
+
 /* inspector */
 #define XT_INS_SHOW		(1<<0)
 #define XT_INS_HIDE		(1<<1)