mirror of
https://github.com/leahneukirchen/cwm.git
synced 2023-08-10 21:13:12 +03:00
Add a function that adds an entry to a menuq, normalizing a common code
path; from Tiago Cunha.
This commit is contained in:
parent
43ccf4eae0
commit
720b5452aa
1
calmwm.h
1
calmwm.h
@ -508,6 +508,7 @@ struct menu *menu_filter(struct screen_ctx *, struct menu_q *,
|
|||||||
char *, char *, int,
|
char *, char *, int,
|
||||||
void (*)(struct menu_q *, struct menu_q *, char *),
|
void (*)(struct menu_q *, struct menu_q *, char *),
|
||||||
void (*)(struct menu *, int));
|
void (*)(struct menu *, int));
|
||||||
|
void menuq_add(struct menu_q *, void *, const char *, ...);
|
||||||
void menuq_clear(struct menu_q *);
|
void menuq_clear(struct menu_q *);
|
||||||
|
|
||||||
int parse_config(const char *, struct conf *);
|
int parse_config(const char *, struct conf *);
|
||||||
|
9
group.c
9
group.c
@ -324,15 +324,8 @@ group_menu(struct screen_ctx *sc)
|
|||||||
if (TAILQ_EMPTY(&gc->clients))
|
if (TAILQ_EMPTY(&gc->clients))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
mi = xcalloc(1, sizeof(*mi));
|
menuq_add(&menuq, gc, gc->hidden ? "%d: [%s]" : "%d: %s",
|
||||||
if (gc->hidden)
|
|
||||||
(void)snprintf(mi->text, sizeof(mi->text), "%d: [%s]",
|
|
||||||
gc->shortcut, sc->group_names[i]);
|
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (TAILQ_EMPTY(&menuq))
|
if (TAILQ_EMPTY(&menuq))
|
||||||
|
30
kbfunc.c
30
kbfunc.c
@ -151,13 +151,8 @@ kbfunc_client_search(struct client_ctx *cc, union arg *arg)
|
|||||||
old_cc = client_current();
|
old_cc = client_current();
|
||||||
|
|
||||||
TAILQ_INIT(&menuq);
|
TAILQ_INIT(&menuq);
|
||||||
|
TAILQ_FOREACH(cc, &Clientq, entry)
|
||||||
TAILQ_FOREACH(cc, &Clientq, entry) {
|
menuq_add(&menuq, cc, "%s", cc->name);
|
||||||
mi = xcalloc(1, sizeof(*mi));
|
|
||||||
(void)strlcpy(mi->text, cc->name, sizeof(mi->text));
|
|
||||||
mi->ctx = cc;
|
|
||||||
TAILQ_INSERT_TAIL(&menuq, mi, entry);
|
|
||||||
}
|
|
||||||
|
|
||||||
if ((mi = menu_filter(sc, &menuq, "window", NULL, 0,
|
if ((mi = menu_filter(sc, &menuq, "window", NULL, 0,
|
||||||
search_match_client, search_print_client)) != NULL) {
|
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;
|
struct menu_q menuq;
|
||||||
|
|
||||||
TAILQ_INIT(&menuq);
|
TAILQ_INIT(&menuq);
|
||||||
|
TAILQ_FOREACH(cmd, &Conf.cmdq, entry)
|
||||||
TAILQ_FOREACH(cmd, &Conf.cmdq, entry) {
|
menuq_add(&menuq, cmd, "%s", cmd->label);
|
||||||
mi = xcalloc(1, sizeof(*mi));
|
|
||||||
(void)strlcpy(mi->text, cmd->label, sizeof(mi->text));
|
|
||||||
mi->ctx = cmd;
|
|
||||||
TAILQ_INSERT_TAIL(&menuq, mi, entry);
|
|
||||||
}
|
|
||||||
|
|
||||||
if ((mi = menu_filter(sc, &menuq, "application", NULL, 0,
|
if ((mi = menu_filter(sc, &menuq, "application", NULL, 0,
|
||||||
search_match_text, NULL)) != NULL)
|
search_match_text, NULL)) != NULL)
|
||||||
@ -284,12 +274,8 @@ kbfunc_exec(struct client_ctx *cc, union arg *arg)
|
|||||||
/* check for truncation etc */
|
/* check for truncation etc */
|
||||||
if (l == -1 || l >= (int)sizeof(tpath))
|
if (l == -1 || l >= (int)sizeof(tpath))
|
||||||
continue;
|
continue;
|
||||||
if (access(tpath, X_OK) == 0) {
|
if (access(tpath, X_OK) == 0)
|
||||||
mi = xcalloc(1, sizeof(*mi));
|
menuq_add(&menuq, NULL, "%s", dp->d_name);
|
||||||
(void)strlcpy(mi->text,
|
|
||||||
dp->d_name, sizeof(mi->text));
|
|
||||||
TAILQ_INSERT_TAIL(&menuq, mi, entry);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
(void)closedir(dirp);
|
(void)closedir(dirp);
|
||||||
}
|
}
|
||||||
@ -360,9 +346,7 @@ kbfunc_ssh(struct client_ctx *cc, union arg *arg)
|
|||||||
if (p - buf + 1 > sizeof(hostbuf))
|
if (p - buf + 1 > sizeof(hostbuf))
|
||||||
continue;
|
continue;
|
||||||
(void)strlcpy(hostbuf, buf, p - buf + 1);
|
(void)strlcpy(hostbuf, buf, p - buf + 1);
|
||||||
mi = xcalloc(1, sizeof(*mi));
|
menuq_add(&menuq, NULL, hostbuf);
|
||||||
(void)strlcpy(mi->text, hostbuf, sizeof(mi->text));
|
|
||||||
TAILQ_INSERT_TAIL(&menuq, mi, entry);
|
|
||||||
}
|
}
|
||||||
free(lbuf);
|
free(lbuf);
|
||||||
(void)fclose(fp);
|
(void)fclose(fp);
|
||||||
|
17
menu.c
17
menu.c
@ -25,6 +25,7 @@
|
|||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
#include <err.h>
|
#include <err.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
#include <stdarg.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
@ -604,6 +605,22 @@ menu_keycode(XKeyEvent *ev, enum ctltype *ctl, char *chr)
|
|||||||
return (0);
|
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
|
void
|
||||||
menuq_clear(struct menu_q *mq)
|
menuq_clear(struct menu_q *mq)
|
||||||
{
|
{
|
||||||
|
13
mousefunc.c
13
mousefunc.c
@ -234,11 +234,8 @@ mousefunc_menu_unhide(struct client_ctx *cc, union arg *arg)
|
|||||||
if (wname == NULL)
|
if (wname == NULL)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
mi = xcalloc(1, sizeof(*mi));
|
menuq_add(&menuq, cc, "(%d) %s",
|
||||||
(void)snprintf(mi->text, sizeof(mi->text), "(%d) %s",
|
|
||||||
cc->group ? cc->group->shortcut : 0, wname);
|
cc->group ? cc->group->shortcut : 0, wname);
|
||||||
mi->ctx = cc;
|
|
||||||
TAILQ_INSERT_TAIL(&menuq, mi, entry);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (TAILQ_EMPTY(&menuq))
|
if (TAILQ_EMPTY(&menuq))
|
||||||
@ -267,12 +264,8 @@ mousefunc_menu_cmd(struct client_ctx *cc, union arg *arg)
|
|||||||
|
|
||||||
TAILQ_INIT(&menuq);
|
TAILQ_INIT(&menuq);
|
||||||
|
|
||||||
TAILQ_FOREACH(cmd, &Conf.cmdq, entry) {
|
TAILQ_FOREACH(cmd, &Conf.cmdq, entry)
|
||||||
mi = xcalloc(1, sizeof(*mi));
|
menuq_add(&menuq, cmd, "%s", cmd->label);
|
||||||
(void)strlcpy(mi->text, cmd->label, sizeof(mi->text));
|
|
||||||
mi->ctx = cmd;
|
|
||||||
TAILQ_INSERT_TAIL(&menuq, mi, entry);
|
|
||||||
}
|
|
||||||
if (TAILQ_EMPTY(&menuq))
|
if (TAILQ_EMPTY(&menuq))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
5
search.c
5
search.c
@ -172,7 +172,6 @@ search_print_client(struct menu *mi, int list)
|
|||||||
static void
|
static void
|
||||||
search_match_path(struct menu_q *menuq, struct menu_q *resultq, char *search, int flag)
|
search_match_path(struct menu_q *menuq, struct menu_q *resultq, char *search, int flag)
|
||||||
{
|
{
|
||||||
struct menu *mi;
|
|
||||||
char pattern[MAXPATHLEN];
|
char pattern[MAXPATHLEN];
|
||||||
glob_t g;
|
glob_t g;
|
||||||
int i;
|
int i;
|
||||||
@ -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++) {
|
for (i = 0; i < g.gl_pathc; i++) {
|
||||||
if ((flag & PATH_EXEC) && access(g.gl_pathv[i], X_OK))
|
if ((flag & PATH_EXEC) && access(g.gl_pathv[i], X_OK))
|
||||||
continue;
|
continue;
|
||||||
mi = xcalloc(1, sizeof(*mi));
|
menuq_add(resultq, NULL, "%s", g.gl_pathv[i]);
|
||||||
(void)strlcpy(mi->text, g.gl_pathv[i], sizeof(mi->text));
|
|
||||||
TAILQ_INSERT_TAIL(resultq, mi, resultentry);
|
|
||||||
}
|
}
|
||||||
globfree(&g);
|
globfree(&g);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user