summaryrefslogtreecommitdiff
path: root/dmenu/patches/dmenu-dynamicoptions-5.0.diff
blob: e26f3bd88ffb24d651cdb210a1ca9caef78229e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
From f70735c476c25da46f9e44b835ac967e0dfa4d85 Mon Sep 17 00:00:00 2001
From: Ziad EL KHOURY HANNA <me@ziadkh.ovh>
Date: Mon, 29 Nov 2021 17:36:35 +0100
Subject: [PATCH] add `-dy` flag for dynamic menu updating

`-dy` flag makes dmenu run the command given to it whenever input is
changed with the current input as the last argument and update the
option list according to the output of that command.

Based on dynamic options patch by ttmx <tiago.sequeira.teles@gmail.com>.
Adds proper quoting of the given command.
Adds option to man file documentation.
---
 config.def.h |  1 +
 dmenu.1      |  5 ++++
 dmenu.c      | 70 +++++++++++++++++++++++++++++++++++++++++++++++-----
 3 files changed, 70 insertions(+), 6 deletions(-)

diff --git a/config.def.h b/config.def.h
index 1edb647..035b877 100644
--- a/config.def.h
+++ b/config.def.h
@@ -7,6 +7,7 @@ static const char *fonts[] = {
 	"monospace:size=10"
 };
 static const char *prompt      = NULL;      /* -p  option; prompt to the left of input field */
+static const char *dynamic     = NULL;      /* -dy option; dynamic command to run on input change */
 static const char *colors[SchemeLast][2] = {
 	/*     fg         bg       */
 	[SchemeNorm] = { "#bbbbbb", "#222222" },
diff --git a/dmenu.1 b/dmenu.1
index 323f93c..1ae3fe3 100644
--- a/dmenu.1
+++ b/dmenu.1
@@ -22,6 +22,8 @@ dmenu \- dynamic menu
 .IR color ]
 .RB [ \-w
 .IR windowid ]
+.RB [ \-dy
+.IR command ]
 .P
 .BR dmenu_run " ..."
 .SH DESCRIPTION
@@ -80,6 +82,9 @@ prints version information to stdout, then exits.
 .TP
 .BI \-w " windowid"
 embed into windowid.
+.TP
+.BI \-dy " command"
+runs command whenever input changes to update menu items.
 .SH USAGE
 dmenu is completely controlled by the keyboard.  Items are selected using the
 arrow keys, page up, page down, home, and end.
diff --git a/dmenu.c b/dmenu.c
index 65f25ce..6780122 100644
--- a/dmenu.c
+++ b/dmenu.c
@@ -44,6 +44,7 @@ static struct item *items = NULL;
 static struct item *matches, *matchend;
 static struct item *prev, *curr, *next, *sel;
 static int mon = -1, screen;
+static unsigned int max_lines = 0;
 
 static Atom clip, utf8;
 static Display *dpy;
@@ -210,6 +211,47 @@ grabkeyboard(void)
 	die("cannot grab keyboard");
 }
 
+static void readstdin(FILE* stream);
+
+static void
+refreshoptions()
+{
+	int dynlen = strlen(dynamic);
+	int cmdlen = dynlen + 4;
+	char *cmd;
+	char *c;
+	char *t = text;
+	while (*t)
+		cmdlen += *t++ == '\'' ? 4 : 1;
+	cmd = malloc(cmdlen);
+	if (cmd == NULL)
+		die("cannot malloc %u bytes:", cmdlen);
+	strcpy(cmd, dynamic);
+	t = text;
+	c = cmd + dynlen;
+	*(c++) = ' ';
+	*(c++) = '\'';
+	while (*t) {
+		// prefix ' with '\'
+		if (*t == '\'') {
+			*(c++) = '\'';
+			*(c++) = '\\';
+			*(c++) = '\'';
+		}
+		*(c++) = *(t++);
+	}
+	*(c++) = '\'';
+	*(c++) = 0;
+	FILE *stream = popen(cmd, "r");
+	if (!stream)
+		die("could not popen dynamic command (%s):", cmd);
+	readstdin(stream);
+	int r = pclose(stream);
+	if (r == -1)
+		die("could not pclose dynamic command");
+	free(cmd);
+}
+
 static void
 match(void)
 {
@@ -221,6 +263,16 @@ match(void)
 	size_t len, textsize;
 	struct item *item, *lprefix, *lsubstr, *prefixend, *substrend;
 
+	if (dynamic) {
+		refreshoptions();
+		matches = matchend = NULL;
+		for (item = items; item && item->text; item++)
+			appenditem(item, &matches, &matchend);
+		curr = sel = matches;
+		calcoffsets();
+		return;
+	}
+
 	strcpy(buf, text);
 	/* separate input text into tokens to be matched individually */
 	for (s = strtok(buf, " "); s; tokv[tokc - 1] = s, s = strtok(NULL, " "))
@@ -519,14 +571,14 @@ paste(void)
 }
 
 static void
-readstdin(void)
+readstdin(FILE* stream)
 {
 	char buf[sizeof text], *p;
 	size_t i, imax = 0, size = 0;
 	unsigned int tmpmax = 0;
 
 	/* read each line from stdin and add it to the item list */
-	for (i = 0; fgets(buf, sizeof buf, stdin); i++) {
+	for (i = 0; fgets(buf, sizeof buf, stream); i++) {
 		if (i + 1 >= size / sizeof *items)
 			if (!(items = realloc(items, (size += BUFSIZ))))
 				die("cannot realloc %u bytes:", size);
@@ -544,7 +596,7 @@ readstdin(void)
 	if (items)
 		items[i].text = NULL;
 	inputw = items ? TEXTW(items[imax].text) : 0;
-	lines = MIN(lines, i);
+	lines = MIN(max_lines, i);
 }
 
 static void
@@ -690,7 +742,8 @@ static void
 usage(void)
 {
 	fputs("usage: dmenu [-bfiv] [-l lines] [-p prompt] [-fn font] [-m monitor]\n"
-	      "             [-nb color] [-nf color] [-sb color] [-sf color] [-w windowid]\n", stderr);
+	      "             [-nb color] [-nf color] [-sb color] [-sf color] [-w windowid]\n"
+	      "             [-dy command]\n", stderr);
 	exit(1);
 }
 
@@ -733,6 +786,8 @@ main(int argc, char *argv[])
 			colors[SchemeSel][ColFg] = argv[++i];
 		else if (!strcmp(argv[i], "-w"))   /* embedding window id */
 			embed = argv[++i];
+		else if (!strcmp(argv[i], "-dy"))  /* dynamic command to run */
+			dynamic = argv[++i] && *argv[i] ? argv[i] : NULL;
 		else
 			usage();
 
@@ -757,11 +812,14 @@ main(int argc, char *argv[])
 		die("pledge");
 #endif
 
+	max_lines = lines;
 	if (fast && !isatty(0)) {
 		grabkeyboard();
-		readstdin();
+		if (!dynamic)
+			readstdin(stdin);
 	} else {
-		readstdin();
+		if (!dynamic)
+			readstdin(stdin);
 		grabkeyboard();
 	}
 	setup();
-- 
2.34.1