xref: /openbsd-src/usr.bin/tmux/menu.c (revision 4e1ee0786f11cc571bd0be17d38e46f635c719fc)
1 /* $OpenBSD: menu.c,v 1.40 2021/10/22 17:12:50 nicm Exp $ */
2 
3 /*
4  * Copyright (c) 2019 Nicholas Marriott <nicholas.marriott@gmail.com>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15  * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #include <sys/types.h>
20 
21 #include <stdlib.h>
22 #include <string.h>
23 
24 #include "tmux.h"
25 
26 struct menu_data {
27 	struct cmdq_item	*item;
28 	int			 flags;
29 
30 	struct cmd_find_state	 fs;
31 	struct screen		 s;
32 
33 	u_int			 px;
34 	u_int			 py;
35 
36 	struct menu		*menu;
37 	int			 choice;
38 
39 	menu_choice_cb		 cb;
40 	void			*data;
41 };
42 
43 void
44 menu_add_items(struct menu *menu, const struct menu_item *items,
45     struct cmdq_item *qitem, struct client *c, struct cmd_find_state *fs)
46 {
47 	const struct menu_item	*loop;
48 
49 	for (loop = items; loop->name != NULL; loop++)
50 		menu_add_item(menu, loop, qitem, c, fs);
51 }
52 
53 void
54 menu_add_item(struct menu *menu, const struct menu_item *item,
55     struct cmdq_item *qitem, struct client *c, struct cmd_find_state *fs)
56 {
57 	struct menu_item	*new_item;
58 	const char		*key = NULL, *cmd, *suffix = "";
59 	char			*s, *name;
60 	u_int			 width, max_width;
61 	int			 line;
62 	size_t			 keylen, slen;
63 
64 	line = (item == NULL || item->name == NULL || *item->name == '\0');
65 	if (line && menu->count == 0)
66 		return;
67 
68 	menu->items = xreallocarray(menu->items, menu->count + 1,
69 	    sizeof *menu->items);
70 	new_item = &menu->items[menu->count++];
71 	memset(new_item, 0, sizeof *new_item);
72 
73 	if (line)
74 		return;
75 
76 	if (fs != NULL)
77 		s = format_single_from_state(qitem, item->name, c, fs);
78 	else
79 		s = format_single(qitem, item->name, c, NULL, NULL, NULL);
80 	if (*s == '\0') { /* no item if empty after format expanded */
81 		menu->count--;
82 		return;
83 	}
84 	max_width = c->tty.sx - 4;
85 
86 	slen = strlen(s);
87 	if (*s != '-' && item->key != KEYC_UNKNOWN && item->key != KEYC_NONE) {
88 		key = key_string_lookup_key(item->key, 0);
89 		keylen = strlen(key) + 3; /* 3 = space and two brackets */
90 
91 		/*
92 		 * Only add the key if there is space for the entire item text
93 		 * and the key.
94 		 */
95 		if (keylen >= max_width || slen >= max_width - keylen)
96 			key = NULL;
97 	}
98 
99 	if (key != NULL)
100 		xasprintf(&name, "%s#[default] #[align=right](%s)", s, key);
101 	else {
102 		if (slen > max_width) {
103 			max_width--;
104 			suffix = ">";
105 		}
106 		xasprintf(&name, "%.*s%s", (int)max_width, s, suffix);
107 	}
108 	new_item->name = name;
109 	free(s);
110 
111 	cmd = item->command;
112 	if (cmd != NULL) {
113 		if (fs != NULL)
114 			s = format_single_from_state(qitem, cmd, c, fs);
115 		else
116 			s = format_single(qitem, cmd, c, NULL, NULL, NULL);
117 	} else
118 		s = NULL;
119 	new_item->command = s;
120 	new_item->key = item->key;
121 
122 	width = format_width(new_item->name);
123 	if (*new_item->name == '-')
124 		width--;
125 	if (width > menu->width)
126 		menu->width = width;
127 }
128 
129 struct menu *
130 menu_create(const char *title)
131 {
132 	struct menu	*menu;
133 
134 	menu = xcalloc(1, sizeof *menu);
135 	menu->title = xstrdup(title);
136 	menu->width = format_width(title);
137 
138 	return (menu);
139 }
140 
141 void
142 menu_free(struct menu *menu)
143 {
144 	u_int	i;
145 
146 	for (i = 0; i < menu->count; i++) {
147 		free((void *)menu->items[i].name);
148 		free((void *)menu->items[i].command);
149 	}
150 	free(menu->items);
151 
152 	free((void *)menu->title);
153 	free(menu);
154 }
155 
156 struct screen *
157 menu_mode_cb(__unused struct client *c, void *data, __unused u_int *cx,
158     __unused u_int *cy)
159 {
160 	struct menu_data	*md = data;
161 
162 	return (&md->s);
163 }
164 
165 /* Return parts of the input range which are not obstructed by the menu. */
166 void
167 menu_check_cb(__unused struct client *c, void *data, u_int px, u_int py,
168     u_int nx, struct overlay_ranges *r)
169 {
170 	struct menu_data	*md = data;
171 	struct menu		*menu = md->menu;
172 
173 	server_client_overlay_range(md->px, md->py, menu->width + 4,
174 	    menu->count + 2, px, py, nx, r);
175 }
176 
177 void
178 menu_draw_cb(struct client *c, void *data,
179     __unused struct screen_redraw_ctx *rctx)
180 {
181 	struct menu_data	*md = data;
182 	struct tty		*tty = &c->tty;
183 	struct screen		*s = &md->s;
184 	struct menu		*menu = md->menu;
185 	struct screen_write_ctx	 ctx;
186 	u_int			 i, px = md->px, py = md->py;
187 	struct grid_cell	 gc;
188 
189 	style_apply(&gc, c->session->curw->window->options, "mode-style", NULL);
190 
191 	screen_write_start(&ctx, s);
192 	screen_write_clearscreen(&ctx, 8);
193 	screen_write_menu(&ctx, menu, md->choice, &gc);
194 	screen_write_stop(&ctx);
195 
196 	for (i = 0; i < screen_size_y(&md->s); i++) {
197 		tty_draw_line(tty, s, 0, i, menu->width + 4, px, py + i,
198 		    &grid_default_cell, NULL);
199 	}
200 }
201 
202 void
203 menu_free_cb(__unused struct client *c, void *data)
204 {
205 	struct menu_data	*md = data;
206 
207 	if (md->item != NULL)
208 		cmdq_continue(md->item);
209 
210 	if (md->cb != NULL)
211 		md->cb(md->menu, UINT_MAX, KEYC_NONE, md->data);
212 
213 	screen_free(&md->s);
214 	menu_free(md->menu);
215 	free(md);
216 }
217 
218 int
219 menu_key_cb(struct client *c, void *data, struct key_event *event)
220 {
221 	struct menu_data		*md = data;
222 	struct menu			*menu = md->menu;
223 	struct mouse_event		*m = &event->m;
224 	u_int				 i;
225 	int				 count = menu->count, old = md->choice;
226 	const char			*name = NULL;
227 	const struct menu_item		*item;
228 	struct cmdq_state		*state;
229 	enum cmd_parse_status		 status;
230 	char				*error;
231 
232 	if (KEYC_IS_MOUSE(event->key)) {
233 		if (md->flags & MENU_NOMOUSE) {
234 			if (MOUSE_BUTTONS(m->b) != 0)
235 				return (1);
236 			return (0);
237 		}
238 		if (m->x < md->px ||
239 		    m->x > md->px + 4 + menu->width ||
240 		    m->y < md->py + 1 ||
241 		    m->y > md->py + 1 + count - 1) {
242 			if (~md->flags & MENU_STAYOPEN) {
243 				if (MOUSE_RELEASE(m->b))
244 					return (1);
245 			} else {
246 				if (!MOUSE_RELEASE(m->b) &&
247 				    MOUSE_WHEEL(m->b) == 0 &&
248 				    !MOUSE_DRAG(m->b))
249 					return (1);
250 			}
251 			if (md->choice != -1) {
252 				md->choice = -1;
253 				c->flags |= CLIENT_REDRAWOVERLAY;
254 			}
255 			return (0);
256 		}
257 		if (~md->flags & MENU_STAYOPEN) {
258 			if (MOUSE_RELEASE(m->b))
259 				goto chosen;
260 		} else {
261 			if (MOUSE_WHEEL(m->b) == 0 && !MOUSE_DRAG(m->b))
262 				goto chosen;
263 		}
264 		md->choice = m->y - (md->py + 1);
265 		if (md->choice != old)
266 			c->flags |= CLIENT_REDRAWOVERLAY;
267 		return (0);
268 	}
269 	for (i = 0; i < (u_int)count; i++) {
270 		name = menu->items[i].name;
271 		if (name == NULL || *name == '-')
272 			continue;
273 		if (event->key == menu->items[i].key) {
274 			md->choice = i;
275 			goto chosen;
276 		}
277 	}
278 	switch (event->key & ~KEYC_MASK_FLAGS) {
279 	case KEYC_UP:
280 	case 'k':
281 		if (old == -1)
282 			old = 0;
283 		do {
284 			if (md->choice == -1 || md->choice == 0)
285 				md->choice = count - 1;
286 			else
287 				md->choice--;
288 			name = menu->items[md->choice].name;
289 		} while ((name == NULL || *name == '-') && md->choice != old);
290 		c->flags |= CLIENT_REDRAWOVERLAY;
291 		return (0);
292 	case KEYC_BSPACE:
293 		if (~md->flags & MENU_TAB)
294 			break;
295 		return (1);
296 	case '\011': /* Tab */
297 		if (~md->flags & MENU_TAB)
298 			break;
299 		if (md->choice == count - 1)
300 			return (1);
301 		/* FALLTHROUGH */
302 	case KEYC_DOWN:
303 	case 'j':
304 		if (old == -1)
305 			old = 0;
306 		do {
307 			if (md->choice == -1 || md->choice == count - 1)
308 				md->choice = 0;
309 			else
310 				md->choice++;
311 			name = menu->items[md->choice].name;
312 		} while ((name == NULL || *name == '-') && md->choice != old);
313 		c->flags |= CLIENT_REDRAWOVERLAY;
314 		return (0);
315 	case 'g':
316 	case KEYC_PPAGE:
317 	case '\002': /* C-b */
318 		if (md->choice > 5)
319 			md->choice -= 5;
320 		else
321 			md->choice = 0;
322 		while (md->choice != count && (name == NULL || *name == '-'))
323 			md->choice++;
324 		if (md->choice == count)
325 			md->choice = -1;
326 		c->flags |= CLIENT_REDRAWOVERLAY;
327 		break;
328 	case 'G':
329 	case KEYC_NPAGE:
330 		if (md->choice > count - 6)
331 			md->choice = count - 1;
332 		else
333 			md->choice += 5;
334 		while (md->choice != -1 && (name == NULL || *name == '-'))
335 			md->choice--;
336 		c->flags |= CLIENT_REDRAWOVERLAY;
337 		break;
338 	case '\006': /* C-f */
339 		break;
340 	case '\r':
341 		goto chosen;
342 	case '\033': /* Escape */
343 	case '\003': /* C-c */
344 	case '\007': /* C-g */
345 	case 'q':
346 		return (1);
347 	}
348 	return (0);
349 
350 chosen:
351 	if (md->choice == -1)
352 		return (1);
353 	item = &menu->items[md->choice];
354 	if (item->name == NULL || *item->name == '-') {
355 		if (md->flags & MENU_STAYOPEN)
356 			return (0);
357 		return (1);
358 	}
359 	if (md->cb != NULL) {
360 	    md->cb(md->menu, md->choice, item->key, md->data);
361 	    md->cb = NULL;
362 	    return (1);
363 	}
364 
365 	if (md->item != NULL)
366 		event = cmdq_get_event(md->item);
367 	else
368 		event = NULL;
369 	state = cmdq_new_state(&md->fs, event, 0);
370 
371 	status = cmd_parse_and_append(item->command, NULL, c, state, &error);
372 	if (status == CMD_PARSE_ERROR) {
373 		cmdq_append(c, cmdq_get_error(error));
374 		free(error);
375 	}
376 	cmdq_free_state(state);
377 
378 	return (1);
379 }
380 
381 struct menu_data *
382 menu_prepare(struct menu *menu, int flags, struct cmdq_item *item, u_int px,
383     u_int py, struct client *c, struct cmd_find_state *fs, menu_choice_cb cb,
384     void *data)
385 {
386 	struct menu_data	*md;
387 	u_int			 i;
388 	const char		*name;
389 
390 	if (c->tty.sx < menu->width + 4 || c->tty.sy < menu->count + 2)
391 		return (NULL);
392 	if (px + menu->width + 4 > c->tty.sx)
393 		px = c->tty.sx - menu->width - 4;
394 	if (py + menu->count + 2 > c->tty.sy)
395 		py = c->tty.sy - menu->count - 2;
396 
397 	md = xcalloc(1, sizeof *md);
398 	md->item = item;
399 	md->flags = flags;
400 
401 	if (fs != NULL)
402 		cmd_find_copy_state(&md->fs, fs);
403 	screen_init(&md->s, menu->width + 4, menu->count + 2, 0);
404 	if (~md->flags & MENU_NOMOUSE)
405 		md->s.mode |= (MODE_MOUSE_ALL|MODE_MOUSE_BUTTON);
406 	md->s.mode &= ~MODE_CURSOR;
407 
408 	md->px = px;
409 	md->py = py;
410 
411 	md->menu = menu;
412 	if (md->flags & MENU_NOMOUSE) {
413 		for (i = 0; i < menu->count; i++) {
414 			name = menu->items[i].name;
415 			if (name != NULL && *name != '-')
416 				break;
417 		}
418 		if (i != menu->count)
419 			md->choice = i;
420 		else
421 			md->choice = -1;
422 	} else
423 		md->choice = -1;
424 
425 	md->cb = cb;
426 	md->data = data;
427 	return (md);
428 }
429 
430 int
431 menu_display(struct menu *menu, int flags, struct cmdq_item *item, u_int px,
432     u_int py, struct client *c, struct cmd_find_state *fs, menu_choice_cb cb,
433     void *data)
434 {
435 	struct menu_data	*md;
436 
437 	md = menu_prepare(menu, flags, item, px, py, c, fs, cb, data);
438 	if (md == NULL)
439 		return (-1);
440 	server_client_set_overlay(c, 0, NULL, menu_mode_cb, menu_draw_cb,
441 	    menu_key_cb, menu_free_cb, NULL, md);
442 	return (0);
443 }
444