summaryrefslogtreecommitdiff
path: root/dmenu/patches/dmenu-navhistory-5.0.diff
blob: 7038b19797f35fbfec579de8d4101dad45de6b66 (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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
From a4a08baf35edb6b50ed14f76e99d0c6fe790759d Mon Sep 17 00:00:00 2001
From: Max Schillinger <maxschillinger@web.de>
Date: Fri, 9 Jul 2021 17:17:36 +0200
Subject: [PATCH] Bug fix: Writing first entry to history file was skipped

---
 config.def.h |   2 +
 dmenu.1      |   5 ++
 dmenu.c      | 144 ++++++++++++++++++++++++++++++++++++++++++++++++++-
 dmenu_run    |   2 +-
 4 files changed, 151 insertions(+), 2 deletions(-)

diff --git a/config.def.h b/config.def.h
index 1edb647..e3e1b53 100644
--- a/config.def.h
+++ b/config.def.h
@@ -15,6 +15,8 @@ static const char *colors[SchemeLast][2] = {
 };
 /* -l option; if nonzero, dmenu uses vertical list with given number of lines */
 static unsigned int lines      = 0;
+static unsigned int maxhist    = 64;
+static int histnodup           = 1;	/* if 0, record repeated histories */

 /*
  * Characters not considered part of a word while deleting words
diff --git a/dmenu.1 b/dmenu.1
index 323f93c..ff496dd 100644
--- a/dmenu.1
+++ b/dmenu.1
@@ -22,6 +22,8 @@ dmenu \- dynamic menu
 .IR color ]
 .RB [ \-w
 .IR windowid ]
+.RB [ \-H
+.IR histfile ]
 .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 \-H " histfile"
+save input in histfile and use it for history navigation.
 .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..5023257 100644
--- a/dmenu.c
+++ b/dmenu.c
@@ -53,6 +53,10 @@ static XIC xic;
 static Drw *drw;
 static Clr *scheme[SchemeLast];

+static char *histfile;
+static char **history;
+static size_t histsz, histpos;
+
 #include "config.h"

 static int (*fstrncmp)(const char *, const char *, size_t) = strncmp;
@@ -304,6 +308,129 @@ movewordedge(int dir)
 	}
 }

+static void
+loadhistory(void)
+{
+	FILE *fp = NULL;
+	static size_t cap = 0;
+	size_t llen;
+	char *line;
+
+	if (!histfile) {
+		return;
+	}
+
+	fp = fopen(histfile, "r");
+	if (!fp) {
+		return;
+	}
+
+	for (;;) {
+		line = NULL;
+		llen = 0;
+		if (-1 == getline(&line, &llen, fp)) {
+			if (ferror(fp)) {
+				die("failed to read history");
+			}
+			free(line);
+			break;
+		}
+
+		if (cap == histsz) {
+			cap += 64 * sizeof(char*);
+			history = realloc(history, cap);
+			if (!history) {
+				die("failed to realloc memory");
+			}
+		}
+		strtok(line, "\n");
+		history[histsz] = line;
+		histsz++;
+	}
+	histpos = histsz;
+
+	if (fclose(fp)) {
+		die("failed to close file %s", histfile);
+	}
+}
+
+static void
+navhistory(int dir)
+{
+	static char def[BUFSIZ];
+	char *p = NULL;
+	size_t len = 0;
+
+	if (!history || histpos + 1 == 0)
+		return;
+
+	if (histsz == histpos) {
+		strncpy(def, text, sizeof(def));
+	}
+
+	switch(dir) {
+	case 1:
+		if (histpos < histsz - 1) {
+			p = history[++histpos];
+		} else if (histpos == histsz - 1) {
+			p = def;
+			histpos++;
+		}
+		break;
+	case -1:
+		if (histpos > 0) {
+			p = history[--histpos];
+		}
+		break;
+	}
+	if (p == NULL) {
+		return;
+	}
+
+	len = MIN(strlen(p), BUFSIZ - 1);
+	strncpy(text, p, len);
+	text[len] = '\0';
+	cursor = len;
+	match();
+}
+
+static void
+savehistory(char *input)
+{
+	unsigned int i;
+	FILE *fp;
+
+	if (!histfile ||
+	    0 == maxhist ||
+	    0 == strlen(input)) {
+		goto out;
+	}
+
+	fp = fopen(histfile, "w");
+	if (!fp) {
+		die("failed to open %s", histfile);
+	}
+	for (i = histsz < maxhist ? 0 : histsz - maxhist; i < histsz; i++) {
+		if (0 >= fprintf(fp, "%s\n", history[i])) {
+			die("failed to write to %s", histfile);
+		}
+	}
+	if (histsz == 0 || !histnodup || (histsz > 0 && strcmp(input, history[histsz-1]) != 0)) { /* TODO */
+		if (0 >= fputs(input, fp)) {
+			die("failed to write to %s", histfile);
+		}
+	}
+	if (fclose(fp)) {
+		die("failed to close file %s", histfile);
+	}
+
+out:
+	for (i = 0; i < histsz; i++) {
+		free(history[i]);
+	}
+	free(history);
+}
+
 static void
 keypress(XKeyEvent *ev)
 {
@@ -388,6 +515,14 @@ keypress(XKeyEvent *ev)
 		case XK_j: ksym = XK_Next;  break;
 		case XK_k: ksym = XK_Prior; break;
 		case XK_l: ksym = XK_Down;  break;
+		case XK_p:
+			navhistory(-1);
+			buf[0]=0;
+			break;
+		case XK_n:
+			navhistory(1);
+			buf[0]=0;
+			break;
 		default:
 			return;
 		}
@@ -466,6 +601,8 @@ insert:
 	case XK_KP_Enter:
 		puts((sel && !(ev->state & ShiftMask)) ? sel->text : text);
 		if (!(ev->state & ControlMask)) {
+			savehistory((sel && !(ev->state & ShiftMask))
+				    ? sel->text : text);
 			cleanup();
 			exit(0);
 		}
@@ -690,7 +827,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"
+	      "             [-H histfile]", stderr);
 	exit(1);
 }

@@ -715,6 +853,8 @@ main(int argc, char *argv[])
 		} else if (i + 1 == argc)
 			usage();
 		/* these options take one argument */
+		else if (!strcmp(argv[i], "-H"))
+			histfile = argv[++i];
 		else if (!strcmp(argv[i], "-l"))   /* number of lines in vertical list */
 			lines = atoi(argv[++i]);
 		else if (!strcmp(argv[i], "-m"))
@@ -757,6 +897,8 @@ main(int argc, char *argv[])
 		die("pledge");
 #endif

+	loadhistory();
+
 	if (fast && !isatty(0)) {
 		grabkeyboard();
 		readstdin();
diff --git a/dmenu_run b/dmenu_run
index 834ede5..59ec622 100755
--- a/dmenu_run
+++ b/dmenu_run
@@ -1,2 +1,2 @@
 #!/bin/sh
-dmenu_path | dmenu "$@" | ${SHELL:-"/bin/sh"} &
+dmenu_path | dmenu -H "${XDG_CACHE_HOME:-$HOME/.cache/}/dmenu_run.hist" "$@" | ${SHELL:-"/bin/sh"} &
--
2.25.1