xref: /netbsd/external/bsd/tmux/dist/menu.c (revision ae2d633d)
1 /* $OpenBSD$ */
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
menu_add_items(struct menu * menu,const struct menu_item * items,struct cmdq_item * qitem,struct client * c,struct cmd_find_state * fs)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
menu_add_item(struct menu * menu,const struct menu_item * item,struct cmdq_item * qitem,struct client * c,struct cmd_find_state * fs)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, *trimmed, *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 		 * Add the key if it is shorter than a quarter of the available
93 		 * space or there is space for the entire item text and the
94 		 * key.
95 		 */
96 		if (keylen <= max_width / 4)
97 			max_width -= keylen;
98 		else if (keylen >= max_width || slen >= max_width - keylen)
99 			key = NULL;
100 	}
101 
102 	if (slen > max_width) {
103 		max_width--;
104 		suffix = ">";
105 	}
106 	trimmed = format_trim_right(s, max_width);
107 	if (key != NULL) {
108 		xasprintf(&name, "%s%s#[default] #[align=right](%s)",
109 		    trimmed, suffix, key);
110 	} else
111 		xasprintf(&name, "%s%s", trimmed, suffix);
112 	free(trimmed);
113 
114 	new_item->name = name;
115 	free(s);
116 
117 	cmd = item->command;
118 	if (cmd != NULL) {
119 		if (fs != NULL)
120 			s = format_single_from_state(qitem, cmd, c, fs);
121 		else
122 			s = format_single(qitem, cmd, c, NULL, NULL, NULL);
123 	} else
124 		s = NULL;
125 	new_item->command = s;
126 	new_item->key = item->key;
127 
128 	width = format_width(new_item->name);
129 	if (*new_item->name == '-')
130 		width--;
131 	if (width > menu->width)
132 		menu->width = width;
133 }
134 
135 struct menu *
menu_create(const char * title)136 menu_create(const char *title)
137 {
138 	struct menu	*menu;
139 
140 	menu = xcalloc(1, sizeof *menu);
141 	menu->title = xstrdup(title);
142 	menu->width = format_width(title);
143 
144 	return (menu);
145 }
146 
147 void
menu_free(struct menu * menu)148 menu_free(struct menu *menu)
149 {
150 	u_int	i;
151 
152 	for (i = 0; i < menu->count; i++) {
153 		free(__UNCONST(menu->items[i].name));
154 		free(__UNCONST(menu->items[i].command));
155 	}
156 	free(menu->items);
157 
158 	free(__UNCONST(menu->title));
159 	free(menu);
160 }
161 
162 struct screen *
menu_mode_cb(__unused struct client * c,void * data,__unused u_int * cx,__unused u_int * cy)163 menu_mode_cb(__unused struct client *c, void *data, __unused u_int *cx,
164     __unused u_int *cy)
165 {
166 	struct menu_data	*md = data;
167 
168 	return (&md->s);
169 }
170 
171 /* Return parts of the input range which are not obstructed by the menu. */
172 void
menu_check_cb(__unused struct client * c,void * data,u_int px,u_int py,u_int nx,struct overlay_ranges * r)173 menu_check_cb(__unused struct client *c, void *data, u_int px, u_int py,
174     u_int nx, struct overlay_ranges *r)
175 {
176 	struct menu_data	*md = data;
177 	struct menu		*menu = md->menu;
178 
179 	server_client_overlay_range(md->px, md->py, menu->width + 4,
180 	    menu->count + 2, px, py, nx, r);
181 }
182 
183 void
menu_draw_cb(struct client * c,void * data,__unused struct screen_redraw_ctx * rctx)184 menu_draw_cb(struct client *c, void *data,
185     __unused struct screen_redraw_ctx *rctx)
186 {
187 	struct menu_data	*md = data;
188 	struct tty		*tty = &c->tty;
189 	struct screen		*s = &md->s;
190 	struct menu		*menu = md->menu;
191 	struct screen_write_ctx	 ctx;
192 	u_int			 i, px = md->px, py = md->py;
193 	struct grid_cell	 gc;
194 
195 	style_apply(&gc, c->session->curw->window->options, "mode-style", NULL);
196 
197 	screen_write_start(&ctx, s);
198 	screen_write_clearscreen(&ctx, 8);
199 	screen_write_menu(&ctx, menu, md->choice, &gc);
200 	screen_write_stop(&ctx);
201 
202 	for (i = 0; i < screen_size_y(&md->s); i++) {
203 		tty_draw_line(tty, s, 0, i, menu->width + 4, px, py + i,
204 		    &grid_default_cell, NULL);
205 	}
206 }
207 
208 void
menu_free_cb(__unused struct client * c,void * data)209 menu_free_cb(__unused struct client *c, void *data)
210 {
211 	struct menu_data	*md = data;
212 
213 	if (md->item != NULL)
214 		cmdq_continue(md->item);
215 
216 	if (md->cb != NULL)
217 		md->cb(md->menu, UINT_MAX, KEYC_NONE, md->data);
218 
219 	screen_free(&md->s);
220 	menu_free(md->menu);
221 	free(md);
222 }
223 
224 int
menu_key_cb(struct client * c,void * data,struct key_event * event)225 menu_key_cb(struct client *c, void *data, struct key_event *event)
226 {
227 	struct menu_data		*md = data;
228 	struct menu			*menu = md->menu;
229 	struct mouse_event		*m = &event->m;
230 	u_int				 i;
231 	int				 count = menu->count, old = md->choice;
232 	const char			*name = NULL;
233 	const struct menu_item		*item;
234 	struct cmdq_state		*state;
235 	enum cmd_parse_status		 status;
236 	char				*error;
237 
238 	if (KEYC_IS_MOUSE(event->key)) {
239 		if (md->flags & MENU_NOMOUSE) {
240 			if (MOUSE_BUTTONS(m->b) != MOUSE_BUTTON_1)
241 				return (1);
242 			return (0);
243 		}
244 		if (m->x < md->px ||
245 		    m->x > md->px + 4 + menu->width ||
246 		    m->y < md->py + 1 ||
247 		    m->y > md->py + 1 + count - 1) {
248 			if (~md->flags & MENU_STAYOPEN) {
249 				if (MOUSE_RELEASE(m->b))
250 					return (1);
251 			} else {
252 				if (!MOUSE_RELEASE(m->b) &&
253 				    !MOUSE_WHEEL(m->b) &&
254 				    !MOUSE_DRAG(m->b))
255 					return (1);
256 			}
257 			if (md->choice != -1) {
258 				md->choice = -1;
259 				c->flags |= CLIENT_REDRAWOVERLAY;
260 			}
261 			return (0);
262 		}
263 		if (~md->flags & MENU_STAYOPEN) {
264 			if (MOUSE_RELEASE(m->b))
265 				goto chosen;
266 		} else {
267 			if (!MOUSE_WHEEL(m->b) && !MOUSE_DRAG(m->b))
268 				goto chosen;
269 		}
270 		md->choice = m->y - (md->py + 1);
271 		if (md->choice != old)
272 			c->flags |= CLIENT_REDRAWOVERLAY;
273 		return (0);
274 	}
275 	for (i = 0; i < (u_int)count; i++) {
276 		name = menu->items[i].name;
277 		if (name == NULL || *name == '-')
278 			continue;
279 		if (event->key == menu->items[i].key) {
280 			md->choice = i;
281 			goto chosen;
282 		}
283 	}
284 	switch (event->key & ~KEYC_MASK_FLAGS) {
285 	case KEYC_UP:
286 	case 'k':
287 		if (old == -1)
288 			old = 0;
289 		do {
290 			if (md->choice == -1 || md->choice == 0)
291 				md->choice = count - 1;
292 			else
293 				md->choice--;
294 			name = menu->items[md->choice].name;
295 		} while ((name == NULL || *name == '-') && md->choice != old);
296 		c->flags |= CLIENT_REDRAWOVERLAY;
297 		return (0);
298 	case KEYC_BSPACE:
299 		if (~md->flags & MENU_TAB)
300 			break;
301 		return (1);
302 	case '\011': /* Tab */
303 		if (~md->flags & MENU_TAB)
304 			break;
305 		if (md->choice == count - 1)
306 			return (1);
307 		/* FALLTHROUGH */
308 	case KEYC_DOWN:
309 	case 'j':
310 		if (old == -1)
311 			old = 0;
312 		do {
313 			if (md->choice == -1 || md->choice == count - 1)
314 				md->choice = 0;
315 			else
316 				md->choice++;
317 			name = menu->items[md->choice].name;
318 		} while ((name == NULL || *name == '-') && md->choice != old);
319 		c->flags |= CLIENT_REDRAWOVERLAY;
320 		return (0);
321 	case 'g':
322 	case KEYC_PPAGE:
323 	case '\002': /* C-b */
324 		if (md->choice > 5)
325 			md->choice -= 5;
326 		else
327 			md->choice = 0;
328 		while (md->choice != count && (name == NULL || *name == '-'))
329 			md->choice++;
330 		if (md->choice == count)
331 			md->choice = -1;
332 		c->flags |= CLIENT_REDRAWOVERLAY;
333 		break;
334 	case 'G':
335 	case KEYC_NPAGE:
336 		if (md->choice > count - 6)
337 			md->choice = count - 1;
338 		else
339 			md->choice += 5;
340 		while (md->choice != -1 && (name == NULL || *name == '-'))
341 			md->choice--;
342 		c->flags |= CLIENT_REDRAWOVERLAY;
343 		break;
344 	case '\006': /* C-f */
345 		break;
346 	case '\r':
347 		goto chosen;
348 	case '\033': /* Escape */
349 	case '\003': /* C-c */
350 	case '\007': /* C-g */
351 	case 'q':
352 		return (1);
353 	}
354 	return (0);
355 
356 chosen:
357 	if (md->choice == -1)
358 		return (1);
359 	item = &menu->items[md->choice];
360 	if (item->name == NULL || *item->name == '-') {
361 		if (md->flags & MENU_STAYOPEN)
362 			return (0);
363 		return (1);
364 	}
365 	if (md->cb != NULL) {
366 	    md->cb(md->menu, md->choice, item->key, md->data);
367 	    md->cb = NULL;
368 	    return (1);
369 	}
370 
371 	if (md->item != NULL)
372 		event = cmdq_get_event(md->item);
373 	else
374 		event = NULL;
375 	state = cmdq_new_state(&md->fs, event, 0);
376 
377 	status = cmd_parse_and_append(item->command, NULL, c, state, &error);
378 	if (status == CMD_PARSE_ERROR) {
379 		cmdq_append(c, cmdq_get_error(error));
380 		free(error);
381 	}
382 	cmdq_free_state(state);
383 
384 	return (1);
385 }
386 
387 struct menu_data *
menu_prepare(struct menu * menu,int flags,struct cmdq_item * item,u_int px,u_int py,struct client * c,struct cmd_find_state * fs,menu_choice_cb cb,void * data)388 menu_prepare(struct menu *menu, int flags, struct cmdq_item *item, u_int px,
389     u_int py, struct client *c, struct cmd_find_state *fs, menu_choice_cb cb,
390     void *data)
391 {
392 	struct menu_data	*md;
393 	u_int			 i;
394 	const char		*name;
395 
396 	if (c->tty.sx < menu->width + 4 || c->tty.sy < menu->count + 2)
397 		return (NULL);
398 	if (px + menu->width + 4 > c->tty.sx)
399 		px = c->tty.sx - menu->width - 4;
400 	if (py + menu->count + 2 > c->tty.sy)
401 		py = c->tty.sy - menu->count - 2;
402 
403 	md = xcalloc(1, sizeof *md);
404 	md->item = item;
405 	md->flags = flags;
406 
407 	if (fs != NULL)
408 		cmd_find_copy_state(&md->fs, fs);
409 	screen_init(&md->s, menu->width + 4, menu->count + 2, 0);
410 	if (~md->flags & MENU_NOMOUSE)
411 		md->s.mode |= (MODE_MOUSE_ALL|MODE_MOUSE_BUTTON);
412 	md->s.mode &= ~MODE_CURSOR;
413 
414 	md->px = px;
415 	md->py = py;
416 
417 	md->menu = menu;
418 	if (md->flags & MENU_NOMOUSE) {
419 		for (i = 0; i < menu->count; i++) {
420 			name = menu->items[i].name;
421 			if (name != NULL && *name != '-')
422 				break;
423 		}
424 		if (i != menu->count)
425 			md->choice = i;
426 		else
427 			md->choice = -1;
428 	} else
429 		md->choice = -1;
430 
431 	md->cb = cb;
432 	md->data = data;
433 	return (md);
434 }
435 
436 int
menu_display(struct menu * menu,int flags,struct cmdq_item * item,u_int px,u_int py,struct client * c,struct cmd_find_state * fs,menu_choice_cb cb,void * data)437 menu_display(struct menu *menu, int flags, struct cmdq_item *item, u_int px,
438     u_int py, struct client *c, struct cmd_find_state *fs, menu_choice_cb cb,
439     void *data)
440 {
441 	struct menu_data	*md;
442 
443 	md = menu_prepare(menu, flags, item, px, py, c, fs, cb, data);
444 	if (md == NULL)
445 		return (-1);
446 	server_client_set_overlay(c, 0, NULL, menu_mode_cb, menu_draw_cb,
447 	    menu_key_cb, menu_free_cb, NULL, md);
448 	return (0);
449 }
450