summary refs log tree commit diff
diff options
context:
space:
mode:
authorokan <okan>2014-01-20 18:58:03 +0000
committerokan <okan>2014-01-20 18:58:03 +0000
commit720b5452aa13bf27740a2b923617188fde8814ed (patch)
tree92a1a0020533747078662aea1050a906e0715398
parent43ccf4eae0f0f5c50495834e29f93671468a7cfe (diff)
downloadcwm-720b5452aa13bf27740a2b923617188fde8814ed.tar.gz
cwm-720b5452aa13bf27740a2b923617188fde8814ed.tar.xz
cwm-720b5452aa13bf27740a2b923617188fde8814ed.zip
Add a function that adds an entry to a menuq, normalizing a common code
path; from Tiago Cunha.
-rw-r--r--calmwm.h1
-rw-r--r--group.c11
-rw-r--r--kbfunc.c30
-rw-r--r--menu.c17
-rw-r--r--mousefunc.c13
-rw-r--r--search.c11
6 files changed, 34 insertions, 49 deletions
diff --git a/calmwm.h b/calmwm.h
index 5f4c45f..4c90239 100644
--- a/calmwm.h
+++ b/calmwm.h
@@ -508,6 +508,7 @@ struct menu  		*menu_filter(struct screen_ctx *, struct menu_q *,
 			     char *, char *, int,
 			     void (*)(struct menu_q *, struct menu_q *, char *),
 			     void (*)(struct menu *, int));
+void			 menuq_add(struct menu_q *, void *, const char *, ...);
 void			 menuq_clear(struct menu_q *);
 
 int			 parse_config(const char *, struct conf *);
diff --git a/group.c b/group.c
index 94d881a..929f9c2 100644
--- a/group.c
+++ b/group.c
@@ -324,15 +324,8 @@ group_menu(struct screen_ctx *sc)
 		if (TAILQ_EMPTY(&gc->clients))
 			continue;
 
-		mi = xcalloc(1, sizeof(*mi));
-		if (gc->hidden)
-			(void)snprintf(mi->text, sizeof(mi->text), "%d: [%s]",
-			    gc->shortcut, sc->group_names[i]);
-		else
-			(void)snprintf(mi->text, sizeof(mi->text), "%d: %s",
-			    gc->shortcut, sc->group_names[i]);
-		mi->ctx = gc;
-		TAILQ_INSERT_TAIL(&menuq, mi, entry);
+		menuq_add(&menuq, gc, gc->hidden ? "%d: [%s]" : "%d: %s",
+		    gc->shortcut, sc->group_names[i]);
 	}
 
 	if (TAILQ_EMPTY(&menuq))
diff --git a/kbfunc.c b/kbfunc.c
index f915d9b..5f73129 100644
--- a/kbfunc.c
+++ b/kbfunc.c
@@ -151,13 +151,8 @@ kbfunc_client_search(struct client_ctx *cc, union arg *arg)
 	old_cc = client_current();
 
 	TAILQ_INIT(&menuq);
-
-	TAILQ_FOREACH(cc, &Clientq, entry) {
-		mi = xcalloc(1, sizeof(*mi));
-		(void)strlcpy(mi->text, cc->name, sizeof(mi->text));
-		mi->ctx = cc;
-		TAILQ_INSERT_TAIL(&menuq, mi, entry);
-	}
+	TAILQ_FOREACH(cc, &Clientq, entry)
+		menuq_add(&menuq, cc, "%s", cc->name);
 
 	if ((mi = menu_filter(sc, &menuq, "window", NULL, 0,
 	    search_match_client, search_print_client)) != NULL) {
@@ -182,13 +177,8 @@ kbfunc_menu_search(struct client_ctx *cc, union arg *arg)
 	struct menu_q		 menuq;
 
 	TAILQ_INIT(&menuq);
-
-	TAILQ_FOREACH(cmd, &Conf.cmdq, entry) {
-		mi = xcalloc(1, sizeof(*mi));
-		(void)strlcpy(mi->text, cmd->label, sizeof(mi->text));
-		mi->ctx = cmd;
-		TAILQ_INSERT_TAIL(&menuq, mi, entry);
-	}
+	TAILQ_FOREACH(cmd, &Conf.cmdq, entry)
+		menuq_add(&menuq, cmd, "%s", cmd->label);
 
 	if ((mi = menu_filter(sc, &menuq, "application", NULL, 0,
 	    search_match_text, NULL)) != NULL)
@@ -284,12 +274,8 @@ kbfunc_exec(struct client_ctx *cc, union arg *arg)
 			/* check for truncation etc */
 			if (l == -1 || l >= (int)sizeof(tpath))
 				continue;
-			if (access(tpath, X_OK) == 0) {
-				mi = xcalloc(1, sizeof(*mi));
-				(void)strlcpy(mi->text,
-				    dp->d_name, sizeof(mi->text));
-				TAILQ_INSERT_TAIL(&menuq, mi, entry);
-			}
+			if (access(tpath, X_OK) == 0)
+				menuq_add(&menuq, NULL, "%s", dp->d_name);
 		}
 		(void)closedir(dirp);
 	}
@@ -360,9 +346,7 @@ kbfunc_ssh(struct client_ctx *cc, union arg *arg)
 		if (p - buf + 1 > sizeof(hostbuf))
 			continue;
 		(void)strlcpy(hostbuf, buf, p - buf + 1);
-		mi = xcalloc(1, sizeof(*mi));
-		(void)strlcpy(mi->text, hostbuf, sizeof(mi->text));
-		TAILQ_INSERT_TAIL(&menuq, mi, entry);
+		menuq_add(&menuq, NULL, hostbuf);
 	}
 	free(lbuf);
 	(void)fclose(fp);
diff --git a/menu.c b/menu.c
index 7495eb1..526b37b 100644
--- a/menu.c
+++ b/menu.c
@@ -25,6 +25,7 @@
 #include <ctype.h>
 #include <err.h>
 #include <errno.h>
+#include <stdarg.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -605,6 +606,22 @@ menu_keycode(XKeyEvent *ev, enum ctltype *ctl, char *chr)
 }
 
 void
+menuq_add(struct menu_q *mq, void *ctx, const char *fmt, ...)
+{
+	va_list		 ap;
+	struct menu	*mi;
+
+	mi = xcalloc(1, sizeof(*mi));
+	mi->ctx = ctx;
+
+	va_start(ap, fmt);
+	(void)vsnprintf(mi->text, sizeof(mi->text), fmt, ap);
+	va_end(ap);
+
+	TAILQ_INSERT_TAIL(mq, mi, entry);
+}
+
+void
 menuq_clear(struct menu_q *mq)
 {
 	struct menu	*mi;
diff --git a/mousefunc.c b/mousefunc.c
index 3bc7f1d..c65daf3 100644
--- a/mousefunc.c
+++ b/mousefunc.c
@@ -234,11 +234,8 @@ mousefunc_menu_unhide(struct client_ctx *cc, union arg *arg)
 			if (wname == NULL)
 				continue;
 
-			mi = xcalloc(1, sizeof(*mi));
-			(void)snprintf(mi->text, sizeof(mi->text), "(%d) %s",
+			menuq_add(&menuq, cc, "(%d) %s",
 			    cc->group ? cc->group->shortcut : 0, wname);
-			mi->ctx = cc;
-			TAILQ_INSERT_TAIL(&menuq, mi, entry);
 		}
 
 	if (TAILQ_EMPTY(&menuq))
@@ -267,12 +264,8 @@ mousefunc_menu_cmd(struct client_ctx *cc, union arg *arg)
 
 	TAILQ_INIT(&menuq);
 
-	TAILQ_FOREACH(cmd, &Conf.cmdq, entry) {
-		mi = xcalloc(1, sizeof(*mi));
-		(void)strlcpy(mi->text, cmd->label, sizeof(mi->text));
-		mi->ctx = cmd;
-		TAILQ_INSERT_TAIL(&menuq, mi, entry);
-	}
+	TAILQ_FOREACH(cmd, &Conf.cmdq, entry)
+		menuq_add(&menuq, cmd, "%s", cmd->label);
 	if (TAILQ_EMPTY(&menuq))
 		return;
 
diff --git a/search.c b/search.c
index 4633769..1346199 100644
--- a/search.c
+++ b/search.c
@@ -172,10 +172,9 @@ search_print_client(struct menu *mi, int list)
 static void
 search_match_path(struct menu_q *menuq, struct menu_q *resultq, char *search, int flag)
 {
-	struct menu	*mi;
-	char 		 pattern[MAXPATHLEN];
-	glob_t		 g;
-	int		 i;
+	char 	 pattern[MAXPATHLEN];
+	glob_t	 g;
+	int	 i;
 
 	TAILQ_INIT(resultq);
 
@@ -187,9 +186,7 @@ search_match_path(struct menu_q *menuq, struct menu_q *resultq, char *search, in
 	for (i = 0; i < g.gl_pathc; i++) {
 		if ((flag & PATH_EXEC) && access(g.gl_pathv[i], X_OK))
 			continue;
-		mi = xcalloc(1, sizeof(*mi));
-		(void)strlcpy(mi->text, g.gl_pathv[i], sizeof(mi->text));
-		TAILQ_INSERT_TAIL(resultq, mi, resultentry);
+		menuq_add(resultq, NULL, "%s", g.gl_pathv[i]);
 	}
 	globfree(&g);
 }