about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorJosh Rickmar <jrick@devio.us>2012-06-01 17:22:42 -0400
committerJosh Rickmar <jrick@devio.us>2012-06-01 17:22:42 -0400
commit861831aad1d1114be754d027683a46bf20e5b664 (patch)
treeab9f2103bb4ace275bc19419f45fe1fca42f5c39
parent1df03fd0b17313e7d953dcb0e14d6849207ca0dc (diff)
downloadxombrero-861831aad1d1114be754d027683a46bf20e5b664.tar.gz
Add a cmd_alias feature to set an alias for a :command, and document
it in the manpage.
-rw-r--r--settings.c66
-rw-r--r--xombrero.113
-rw-r--r--xombrero.c63
-rw-r--r--xombrero.h8
4 files changed, 130 insertions, 20 deletions
diff --git a/settings.c b/settings.c
index 8f33809..bf34475 100644
--- a/settings.c
+++ b/settings.c
@@ -126,6 +126,7 @@ int		add_mime_type(struct settings *, char *);
 int		add_alias(struct settings *, char *);
 int		add_kb(struct settings *, char *);
 int		add_ua(struct settings *, char *);
+int		add_cmd_alias(struct settings *, char *);
 int		add_custom_uri(struct settings *, char *);
 
 int		set_append_next(char *);
@@ -197,6 +198,8 @@ void		walk_kb(struct settings *, void (*)(struct settings *, char *,
 		    void *), void *);
 void		walk_ua(struct settings *, void (*)(struct settings *, char *,
 		    void *), void *);
+void		walk_cmd_alias(struct settings *, void (*)(struct settings *,
+		    char *, void *), void *);
 void		walk_custom_uri(struct settings *, void (*)(struct settings *,
 		    char *, void *), void *);
 
@@ -257,6 +260,12 @@ struct special		s_alias = {
 	walk_alias
 };
 
+struct special		s_cmd_alias = {
+	add_cmd_alias,
+	NULL,
+	walk_cmd_alias
+};
+
 struct special		s_mime = {
 	add_mime_type,
 	NULL,
@@ -424,6 +433,7 @@ struct settings		rs[] = {
 	{ "mime_type",			XT_S_STR, XT_SF_RUNTIME, NULL, NULL, &s_mime, NULL, NULL },
 	{ "pl_wl",			XT_S_STR, XT_SF_RUNTIME, NULL, NULL, &s_pl, NULL, NULL },
 	{ "user_agent",			XT_S_STR, XT_SF_RUNTIME, NULL, NULL, &s_ua, NULL, NULL },
+	{ "cmd_alias",			XT_S_STR, XT_SF_RUNTIME, NULL, NULL, &s_cmd_alias, NULL, NULL },
 	{ "custom_uri",			XT_S_STR, XT_SF_RUNTIME, NULL, NULL, &s_uri, NULL, NULL },
 };
 
@@ -1228,6 +1238,28 @@ keybinding_add(char *cmd, char *key, int use_in_entry)
 }
 
 int
+cmd_alias_add(char *alias, char *cmd)
+{
+	struct cmd_alias	*c;
+
+	/* XXX */
+	TAILQ_FOREACH(c, &cal, entry)
+		if (!strcmp((alias), c->alias)) {
+			TAILQ_REMOVE(&cal, c, entry);
+			g_free(c);
+		}
+
+	c = g_malloc(sizeof (struct cmd_alias));
+	c->alias = g_strchug(g_strdup(alias));
+	c->cmd = g_strchug(g_strdup(cmd));
+
+	DNPRINTF(XT_D_CUSTOM_URI, "cmd_alias_add: %s %s\n", c->alias, c->cmd);
+
+	TAILQ_INSERT_HEAD(&cal, c, entry);
+	return (0);
+}
+
+int
 custom_uri_add(char *uri, char *cmd)
 {
 	struct custom_uri	*u;
@@ -1339,6 +1371,40 @@ walk_ua(struct settings *s,
 }
 
 int
+add_cmd_alias(struct settings *s, char *entry)
+{
+	char			*alias, *cmd;
+
+	DNPRINTF(XT_D_CMD_ALIAS, "add_cmd_alias: %s\n", entry);
+
+	alias = strstr(entry, ",");
+	if (alias == NULL)
+		return (1);
+	*alias = '\0';
+	cmd = alias + 1;
+
+	return (cmd_alias_add(entry, cmd));
+}
+
+void
+walk_cmd_alias(struct settings *s,
+    void (*cb)(struct settings *, char *, void *), void *cb_args)
+{
+	struct cmd_alias	*c;
+	char			buf[1024];
+
+	if (s == NULL || cb == NULL) {
+		show_oops(NULL, "walk_cmd_alias invalid parameters");
+		return;
+	}
+
+	TAILQ_FOREACH(c, &cal, entry) {
+		snprintf(buf, sizeof buf, "%s --> %s", c->alias, c->cmd);
+		cb(s, buf, cb_args);
+	}
+}
+
+int
 set_auto_load_images(char *value)
 {
 	struct tab		*t;
diff --git a/xombrero.1 b/xombrero.1
index 00dfad8..6afb346 100644
--- a/xombrero.1
+++ b/xombrero.1
@@ -984,6 +984,19 @@ This setting must be the first entry in
 .Pa ~/.xombrero.conf
 because it sets advanced settings that can be overridden later in the file.
 See the default config file for more details.
+.It Cm cmd_alias
+This setting is used to define an alias for a command.
+The syntax of this setting is
+.Pa cmd_alias = alias,cmd .
+For example, if you wanted to set an alias for the
+.Pa tabnew
+command to
+.Pa tn ,
+add the line
+.Pa cmd_alias = tn,tabnew
+to your
+.Pa ~/.xombrero.conf .
+This setting may not begin with a number.
 .It Cm cmd_font
 Set the command prompt font.
 E.g.
diff --git a/xombrero.c b/xombrero.c
index 67dac8b..0c7840b 100644
--- a/xombrero.c
+++ b/xombrero.c
@@ -221,6 +221,7 @@ struct undo_tailq	undos;
 struct keybinding_list	kbl;
 struct sp_list		spl;
 struct user_agent_list	ua_list;
+struct cmd_alias_list	cal;
 struct custom_uri_list	cul;
 int			user_agent_count = 0;
 struct command_list	chl;
@@ -5855,32 +5856,54 @@ cmd_complete(struct tab *t, char *str, int dir)
 	g_free(sc);
 }
 
-gboolean
-cmd_execute(struct tab *t, char *str)
+char *
+parse_prefix_and_alias(const char *str, int *prefix)
 {
-	struct cmd		*cmd = NULL;
-	char			*tok, *last = NULL, *s = g_strdup(str), *sc;
-	char			prefixstr[4];
-	int			j, len, c = 0, dep = 0, matchcount = 0;
-	int			prefix = -1, rv = XT_CB_PASSTHROUGH;
-	struct karg		arg = {0, NULL, -1};
+	struct cmd_alias	*c;
+	char			*s = g_strdup(str), *sc;
+	char			hasprefix = 0;
 
+	g_strstrip(s);
 	sc = s;
 
-	/* copy prefix*/
-	for (j = 0; j<3 && isdigit(s[j]); j++)
-		prefixstr[j]=s[j];
+	if (isdigit(s[0])) {
+		hasprefix = 1;
+		sscanf(s, "%d", prefix);
+		while (isdigit(s[0]) || isspace(s[0]))
+			++s;
+	}
 
-	prefixstr[j]='\0';
+	TAILQ_FOREACH(c, &cal, entry) {
+		if (strncmp(s, c->alias, strlen(c->alias)))
+			continue;
 
-	s += j;
-	while (isspace(s[0]))
-		s++;
+		if (strlen(s) == strlen(c->alias)) {
+			g_free(sc);
+			return (g_strdup(c->cmd));
+		}
 
-	if (strlen(s) > 0 && strlen(prefixstr) > 0)
-		prefix = atoi(prefixstr);
-	else
-		s = sc;
+		if (!isspace(s[strlen(c->alias)]))
+			continue;
+
+		s = g_strdup_printf("%s %s", c->cmd, &s[strlen(c->alias) + 1]);
+		g_free(sc);
+		return (s);
+	}
+	s = g_strdup(s);
+	g_free(sc);
+	return (s);
+}
+
+gboolean
+cmd_execute(struct tab *t, char *str)
+{
+	struct cmd		*cmd = NULL;
+	char			*tok, *last = NULL, *s = str;
+	int			j = 0, len, c = 0, dep = 0, matchcount = 0;
+	int			prefix = -1, rv = XT_CB_PASSTHROUGH;
+	struct karg		arg = {0, NULL, -1};
+
+	s = parse_prefix_and_alias(s, &prefix);
 
 	for (tok = strtok_r(s, " ", &last); tok;
 	    tok = strtok_r(NULL, " ", &last)) {
@@ -5952,7 +5975,7 @@ execute_cmd:
 done:
 	if (j > 0)
 		cmd_prefix = 0;
-	g_free(sc);
+	g_free(s);
 	if (arg.s)
 		g_free(arg.s);
 
diff --git a/xombrero.h b/xombrero.h
index d9222b5..0061e34 100644
--- a/xombrero.h
+++ b/xombrero.h
@@ -586,6 +586,13 @@ struct user_agent {
 };
 TAILQ_HEAD(user_agent_list, user_agent);
 
+struct cmd_alias {
+	char			*alias;
+	char			*cmd;
+	TAILQ_ENTRY(cmd_alias)	entry;
+};
+TAILQ_HEAD(cmd_alias_list, cmd_alias);
+
 struct settings {
 	char		*name;
 	int		type;
@@ -736,6 +743,7 @@ extern struct mime_type_list	mtl;
 extern struct keybinding_list	kbl;
 extern struct sp_list		spl;
 extern struct user_agent_list	ua_list;
+extern struct cmd_alias_list	cal;
 extern struct custom_uri_list	cul;
 extern int			user_agent_count;