about summary refs log tree commit diff stats
path: root/xombrero.c
diff options
context:
space:
mode:
authorJosh Rickmar <jrick@devio.us>2012-06-01 16:55:52 -0400
committerJosh Rickmar <jrick@devio.us>2012-06-01 16:55:52 -0400
commit59e5b689070af33f9597ee85f9d046e850aa26e8 (patch)
tree02619dc24d7338b23d8406977a1d7664c3652098 /xombrero.c
parent108f505da68aeaa8ff1674fe1596c3c3b872d85b (diff)
downloadxombrero-59e5b689070af33f9597ee85f9d046e850aa26e8.tar.gz
Parse keybound commands for if they begin with a ':'. If they do,
don't try to execute them, but instead open a prompt with that current
command.  Can be used to create custom prompts, and should fix FS#233.

This change also adds the ability to replace strings in keybound
prompts.  So far the only string replacement is <uri> which is
replaced by the current tab's uri.  This also kills the old prompt*
commands as they only existed so they could be bound to a key.
However with the addition of the <uri> substitution, these are no
longer necessary.  Document these changes in the manpage.
Diffstat (limited to 'xombrero.c')
-rw-r--r--xombrero.c88
1 files changed, 46 insertions, 42 deletions
diff --git a/xombrero.c b/xombrero.c
index 92c9d9d..4b16a8d 100644
--- a/xombrero.c
+++ b/xombrero.c
@@ -167,11 +167,6 @@ TAILQ_HEAD(command_list, command_entry);
 #define XT_ZOOM_OUT		(-2)
 #define XT_ZOOM_NORMAL		(100)
 
-#define XT_CMD_OPEN		(0)
-#define XT_CMD_OPEN_CURRENT	(1)
-#define XT_CMD_TABNEW		(2)
-#define XT_CMD_TABNEW_CURRENT	(3)
-
 #define XT_STATUS_NOTHING	(0)
 #define XT_STATUS_LINK		(1)
 #define XT_STATUS_URI		(2)
@@ -2615,14 +2610,21 @@ movetab(struct tab *t, struct karg *args)
 
 int cmd_prefix = 0;
 
+struct prompt_sub {
+	const char		*s;
+	const char		*(*f)(struct tab *);
+} subs[] = {
+	{ "<uri>",	get_uri },
+};
 
 int
 command(struct tab *t, struct karg *args)
 {
-	char			*s = NULL, *ss = NULL;
-	gchar			*text, *base;
-	const gchar		*uri;
 	struct karg		a;
+	int			i;
+	char			*s = NULL, *sp = NULL, *sl = NULL;
+	gchar			**sv;
+	gchar			*text, *base;
 
 	if (t == NULL || args == NULL) {
 		show_oops(NULL, "command invalid parameters");
@@ -2637,13 +2639,31 @@ command(struct tab *t, struct karg *args)
 		s = "?";
 		break;
 	case ':':
-		if (cmd_prefix == 0)
-			s = ":";
-		else {
+		if (cmd_prefix == 0) {
+			if (args->s != NULL && strlen(args->s) != 0) {
+				ss = g_strdup_printf(":%s", args->s);
+				s = sp;
+			} else
+				s = ":";
+		} else {
 			ss = g_strdup_printf(":%d", cmd_prefix);
-			s = ss;
+			s = sp;
 			cmd_prefix = 0;
 		}
+		sl = g_strdup(s);
+		if (sp) {
+			g_free(sp);
+			sp = NULL;
+		}
+		s = sl;
+		for (i = 0; i < sizeof subs / sizeof (struct prompt_sub); ++i) {
+			sv = g_strsplit(sl, subs[i].s, -1);
+			if (sl)
+				g_free(sl);
+			sl = g_strjoinv(subs[i].f(t), sv);
+			g_strfreev(sv);
+			s = sl;
+		}
 		break;
 	case '.':
 		t->mode = XT_MODE_HINT;
@@ -2659,23 +2679,6 @@ command(struct tab *t, struct karg *args)
 		hint(t, &a);
 		s = ",";
 		break;
-	case XT_CMD_OPEN:
-		s = ":open ";
-		break;
-	case XT_CMD_TABNEW:
-		s = ":tabnew ";
-		break;
-	case XT_CMD_OPEN_CURRENT:
-		s = ":open ";
-		/* FALL THROUGH */
-	case XT_CMD_TABNEW_CURRENT:
-		if (!s) /* FALL THROUGH? */
-			s = ":tabnew ";
-		if ((uri = get_uri(t)) != NULL) {
-			ss = g_strdup_printf("%s%s", s, uri);
-			s = ss;
-		}
-		break;
 	default:
 		show_oops(t, "command: invalid opcode %d", args->i);
 		return (XT_CB_PASSTHROUGH);
@@ -2695,8 +2698,10 @@ command(struct tab *t, struct karg *args)
 	gtk_widget_grab_focus(GTK_WIDGET(t->cmd));
 	gtk_editable_set_position(GTK_EDITABLE(t->cmd), -1);
 
-	if (ss)
-		g_free(ss);
+	if (sp)
+		g_free(sp);
+	if (sl)
+		g_free(sl);
 
 	return (XT_CB_HANDLED);
 }
@@ -3193,12 +3198,6 @@ struct cmd {
 	{ "encoding",		0,	set_encoding,		0,			XT_USERARG },
 	{ "loadimages",		0,	tabaction,		XT_TAB_LOAD_IMAGES,	0 },
 
-	/* command aliases (handy when -S flag is used) */
-	{ "promptopen",		0,	command,		XT_CMD_OPEN,		0 },
-	{ "promptopencurrent",	0,	command,		XT_CMD_OPEN_CURRENT,	0 },
-	{ "prompttabnew",	0,	command,		XT_CMD_TABNEW,		0 },
-	{ "prompttabnewcurrent",0,	command,		XT_CMD_TABNEW_CURRENT,	0 },
-
 	/* settings */
 	{ "set",		0,	set,			0,			XT_SETARG },
 
@@ -5417,6 +5416,7 @@ done:
 gboolean
 handle_keypress(struct tab *t, GdkEventKey *e, int entry)
 {
+	struct karg		args;
 	struct key_binding	*k;
 
 	/* handle keybindings if buffercmd is empty.
@@ -5431,11 +5431,15 @@ handle_keypress(struct tab *t, GdkEventKey *e, int entry)
 				    (e->state & CTRL || e->state & MOD1))
 					return (XT_CB_PASSTHROUGH);
 
-				if (k->mask == 0) {
-					if ((e->state & (CTRL | MOD1)) == 0)
+				if ((k->mask == 0 &&
+				    (e->state & (CTRL | MOD1)) == 0) ||
+				    (e->state & k->mask) == k->mask) {
+					if (k->cmd[0] == ':') {
+						args.i = ':';
+						args.s = &k->cmd[1];
+						return command(t, &args);
+					} else
 						return (cmd_execute(t, k->cmd));
-				} else if ((e->state & k->mask) == k->mask) {
-					return (cmd_execute(t, k->cmd));
 				}
 			}
 
@@ -7500,7 +7504,7 @@ socket_watcher(GIOChannel *source, GIOCondition condition, gpointer data)
 	if (n <= 0)
 		return (TRUE);
 
-	tt = get_current_tab();
+	tt = TAILQ_LAST(&tabs, tab_list);
 	cmd_execute(tt, str);
 	return (TRUE);
 }