summaryrefslogtreecommitdiff
path: root/x11-misc/dmenu/files/04_vi-mode.diff
blob: 9fda39ec2c6230f09730b099cbd709d7e0e80fcf (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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
diff --git a/config.def.h b/config.def.h
index a7bf4863b3..5ed8497550 100644
--- a/config.def.h
+++ b/config.def.h
@@ -16,6 +16,7 @@
 	[SchemeNorm] = { "#bbbbbb", "#222222" },
 	[SchemeSel] = { "#eeeeee", "#005577" },
 	[SchemeOut] = { "#000000", "#00ffff" },
+	[SchemeCursor] = { "#222222", "#bbbbbb"},
 };
 /* -l option; if nonzero, dmenu uses vertical list with given number of lines */
 static unsigned int lines      = 0;
@@ -25,6 +26,16 @@
  * for example: " /?\"&[]"
  */
 static const char worddelimiters[] = " ";
-
 /* Size of the window border */
 static unsigned int border_width = 0;
+/*
+ * -vi option; if nonzero, vi mode is always enabled and can be
+ * accessed with the global_esc keysym + mod mask
+ */
+static unsigned int vi_mode = 1;
+static unsigned int start_mode = 1;			/* mode to use when -vi is passed. 0 = insert mode, 1 = normal mode */
+static Key global_esc = { XK_n, Mod1Mask };	/* escape key when vi mode is not enabled explicitly */
+static Key quit_keys[] = {
+	/* keysym	modifier */
+	{ XK_q,		0 }
+};
diff --git a/dmenu.c b/dmenu.c
index 9f9dfb6e27..948f0b08df 100644
--- a/dmenu.c
+++ b/dmenu.c
@@ -25,7 +25,7 @@
 #define TEXTW(X)              (drw_fontset_getwidth(drw, (X)) + lrpad)
 
 /* enums */
-enum { SchemeNorm, SchemeSel, SchemeOut, SchemeLast }; /* color schemes */
+enum { SchemeNorm, SchemeSel, SchemeOut, SchemeCursor, SchemeLast }; /* color schemes */
 
 struct item {
 	char *text;
@@ -34,6 +34,11 @@
 	int out;
 };
 
+typedef struct {
+	KeySym ksym;
+	unsigned int state;
+} Key;
+
 static char text[BUFSIZ] = "";
 static char *embed;
 static int bh, mw, mh;
@@ -44,6 +49,7 @@
 static struct item *matches, *matchend;
 static struct item *prev, *curr, *next, *sel;
 static int mon = -1, screen;
+static unsigned int using_vi_mode = 0;
 
 static Atom clip, utf8;
 static Display *dpy;
@@ -173,7 +179,15 @@
 		drw_text(drw, x, 0, w, bh, lrpad / 2, text, 0);
 
 		curpos = TEXTW(text) - TEXTW(&text[cursor]);
-		if ((curpos += lrpad / 2 - 1) < w) {
+		curpos += lrpad / 2 - 1;
+		if (using_vi_mode && text[0] != '\0') {
+			drw_setscheme(drw, scheme[SchemeCursor]);
+			char vi_char[] = {text[cursor], '\0'};
+			drw_text(drw, x + curpos, 0, TEXTW(vi_char) - lrpad, bh, 0, vi_char, 0);
+		} else if (using_vi_mode) {
+			drw_setscheme(drw, scheme[SchemeNorm]);
+			drw_rect(drw, x + curpos, 2, lrpad / 2, bh - 4, 1, 0);
+		} else if (curpos < w) {
 			drw_setscheme(drw, scheme[SchemeNorm]);
 			drw_rect(drw, x + curpos, 2, 2, bh - 4, 1, 0);
 		}
@@ -333,6 +347,181 @@
 }
 
 static void
+vi_keypress(KeySym ksym, const XKeyEvent *ev)
+{
+	static const size_t quit_len = LENGTH(quit_keys);
+	if (ev->state & ControlMask) {
+		switch(ksym) {
+		/* movement */
+		case XK_d: /* fallthrough */
+			if (next) {
+				sel = curr = next;
+				calcoffsets();
+				goto draw;
+			} else
+				ksym = XK_G;
+			break;
+		case XK_u:
+			if (prev) {
+				sel = curr = prev;
+				calcoffsets();
+				goto draw;
+			} else
+				ksym = XK_g;
+			break;
+		case XK_p: /* fallthrough */
+		case XK_P: break;
+		case XK_c:
+			cleanup();
+			exit(1);
+		case XK_Return: /* fallthrough */
+		case XK_KP_Enter: break;
+		default: return;
+		}
+	}
+
+	switch(ksym) {
+	/* movement */
+	case XK_0:
+		cursor = 0;
+		break;
+	case XK_dollar:
+		if (text[cursor + 1] != '\0') {
+			cursor = strlen(text) - 1;
+			break;
+		}
+		break;
+	case XK_b:
+		movewordedge(-1);
+		break;
+	case XK_e:
+		cursor = nextrune(+1);
+		movewordedge(+1);
+		if (text[cursor] == '\0')
+			--cursor;
+		else
+			cursor = nextrune(-1);
+		break;
+	case XK_g:
+		if (sel == matches) {
+			break;
+		}
+		sel = curr = matches;
+		calcoffsets();
+		break;
+	case XK_G:
+		if (next) {
+			/* jump to end of list and position items in reverse */
+			curr = matchend;
+			calcoffsets();
+			curr = prev;
+			calcoffsets();
+			while (next && (curr = curr->right))
+				calcoffsets();
+		}
+		sel = matchend;
+		break;
+	case XK_h:
+		if (cursor)
+			cursor = nextrune(-1);
+		break;
+	case XK_j:
+		if (sel && sel->right && (sel = sel->right) == next) {
+			curr = next;
+			calcoffsets();
+		}
+		break;
+	case XK_k:
+		if (sel && sel->left && (sel = sel->left)->right == curr) {
+			curr = prev;
+			calcoffsets();
+		}
+		break;
+	case XK_l:
+		if (text[cursor] != '\0' && text[cursor + 1] != '\0')
+			cursor = nextrune(+1);
+		else if (text[cursor] == '\0' && cursor)
+			--cursor;
+		break;
+	case XK_w:
+		movewordedge(+1);
+		if (text[cursor] != '\0' && text[cursor + 1] != '\0')
+			cursor = nextrune(+1);
+		else if (cursor)
+			--cursor;
+		break;
+	/* insertion */
+	case XK_a:
+		cursor = nextrune(+1);
+		/* fallthrough */
+	case XK_i:
+		using_vi_mode = 0;
+		break;
+	case XK_A:
+		if (text[cursor] != '\0')
+			cursor = strlen(text);
+		using_vi_mode = 0;
+		break;
+	case XK_I:
+		cursor = using_vi_mode = 0;
+		break;
+	case XK_p:
+		if (text[cursor] != '\0')
+			cursor = nextrune(+1);
+		XConvertSelection(dpy, (ev->state & ControlMask) ? clip : XA_PRIMARY,
+							utf8, utf8, win, CurrentTime);
+		return;
+	case XK_P:
+		XConvertSelection(dpy, (ev->state & ControlMask) ? clip : XA_PRIMARY,
+							utf8, utf8, win, CurrentTime);
+		return;
+	/* deletion */
+	case XK_D:
+		text[cursor] = '\0';
+		if (cursor)
+			cursor = nextrune(-1);
+		match();
+		break;
+	case XK_x:
+		cursor = nextrune(+1);
+		insert(NULL, nextrune(-1) - cursor);
+		if (text[cursor] == '\0' && text[0] != '\0')
+			--cursor;
+		match();
+		break;
+	/* misc. */
+	case XK_Return:
+	case XK_KP_Enter:
+		puts((sel && !(ev->state & ShiftMask)) ? sel->text : text);
+		if (!(ev->state & ControlMask)) {
+			cleanup();
+			exit(0);
+		}
+		if (sel)
+			sel->out = 1;
+		break;
+	case XK_Tab:
+		if (!sel)
+			return;
+		strncpy(text, sel->text, sizeof text - 1);
+		text[sizeof text - 1] = '\0';
+		cursor = strlen(text) - 1;
+		match();
+		break;
+	default:
+		for (size_t i = 0; i < quit_len; ++i)
+			if (quit_keys[i].ksym == ksym &&
+				(quit_keys[i].state & ev->state) == quit_keys[i].state) {
+				cleanup();
+				exit(1);
+			}
+	}
+
+draw:
+	drawmenu();
+}
+
+static void
 keypress(XKeyEvent *ev)
 {
 	char buf[64];
@@ -351,6 +540,18 @@
 		break;
 	}
 
+	if (using_vi_mode) {
+		vi_keypress(ksym, ev);
+		return;
+	} else if (vi_mode &&
+			   (ksym == global_esc.ksym &&
+				(ev->state & global_esc.state) == global_esc.state)) {
+		using_vi_mode = 1;
+		if (cursor)
+			cursor = nextrune(-1);
+		goto draw;
+	}
+
 	if (ev->state & ControlMask) {
 		switch(ksym) {
 		case XK_a: ksym = XK_Home;      break;
@@ -557,6 +758,8 @@
 		insert(p, (q = strchr(p, '\n')) ? q - p : (ssize_t)strlen(p));
 		XFree(p);
 	}
+	if (using_vi_mode && text[cursor] == '\0')
+		--cursor;
 	drawmenu();
 }
 
@@ -773,6 +976,11 @@
 		else if (!strcmp(argv[i], "-i")) { /* case-insensitive item matching */
 			fstrncmp = strncasecmp;
 			fstrstr = cistrstr;
+		} else if (!strcmp(argv[i], "-vi")) {
+			vi_mode = 1;
+			using_vi_mode = start_mode;
+			global_esc.ksym = XK_Escape;
+			global_esc.state = 0;
 		} else if (i + 1 == argc)
 			usage();
 		/* these options take one argument */