Add a function that adds an entry to a menuq, normalizing a common code

path; from Tiago Cunha.
This commit is contained in:
okan 2014-01-20 18:58:03 +00:00
parent 43ccf4eae0
commit 720b5452aa
6 changed files with 34 additions and 49 deletions

View File

@ -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 *);

11
group.c
View File

@ -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))

View File

@ -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);

17
menu.c
View File

@ -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>
@ -604,6 +605,22 @@ menu_keycode(XKeyEvent *ev, enum ctltype *ctl, char *chr)
return (0);
}
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)
{

View File

@ -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;

View File

@ -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);
}