Annotation of src/usr.bin/tmux/menu.c, Revision 1.40
1.40 ! nicm 1: /* $OpenBSD: menu.c,v 1.39 2021/10/18 09:48:35 nicm Exp $ */
1.1 nicm 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:
1.8 nicm 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,
1.6 nicm 55: struct cmdq_item *qitem, struct client *c, struct cmd_find_state *fs)
1.1 nicm 56: {
57: struct menu_item *new_item;
1.40 ! nicm 58: const char *key = NULL, *cmd, *suffix = "";
1.8 nicm 59: char *s, *name;
1.40 ! nicm 60: u_int width, max_width;
1.8 nicm 61: int line;
1.40 ! nicm 62: size_t keylen, slen;
1.8 nicm 63:
64: line = (item == NULL || item->name == NULL || *item->name == '\0');
65: if (line && menu->count == 0)
66: return;
1.1 nicm 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:
1.8 nicm 73: if (line)
1.1 nicm 74: return;
1.8 nicm 75:
76: if (fs != NULL)
1.24 nicm 77: s = format_single_from_state(qitem, item->name, c, fs);
1.8 nicm 78: else
79: s = format_single(qitem, item->name, c, NULL, NULL, NULL);
80: if (*s == '\0') { /* no item if empty after format expanded */
1.1 nicm 81: menu->count--;
82: return;
83: }
1.40 ! nicm 84: max_width = c->tty.sx - 4;
! 85:
! 86: slen = strlen(s);
1.9 nicm 87: if (*s != '-' && item->key != KEYC_UNKNOWN && item->key != KEYC_NONE) {
1.29 nicm 88: key = key_string_lookup_key(item->key, 0);
1.40 ! nicm 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)
1.8 nicm 100: xasprintf(&name, "%s#[default] #[align=right](%s)", s, key);
1.40 ! nicm 101: else {
! 102: if (slen > max_width) {
! 103: max_width--;
! 104: suffix = ">";
! 105: }
! 106: xasprintf(&name, "%.*s%s", (int)max_width, s, suffix);
! 107: }
1.8 nicm 108: new_item->name = name;
109: free(s);
110:
111: cmd = item->command;
112: if (cmd != NULL) {
113: if (fs != NULL)
1.24 nicm 114: s = format_single_from_state(qitem, cmd, c, fs);
1.8 nicm 115: else
116: s = format_single(qitem, cmd, c, NULL, NULL, NULL);
1.6 nicm 117: } else
1.8 nicm 118: s = NULL;
119: new_item->command = s;
1.1 nicm 120: new_item->key = item->key;
121:
122: width = format_width(new_item->name);
1.39 nicm 123: if (*new_item->name == '-')
124: width--;
1.1 nicm 125: if (width > menu->width)
126: menu->width = width;
127: }
128:
129: struct menu *
1.8 nicm 130: menu_create(const char *title)
1.1 nicm 131: {
132: struct menu *menu;
133:
134: menu = xcalloc(1, sizeof *menu);
135: menu->title = xstrdup(title);
1.30 nicm 136: menu->width = format_width(title);
1.1 nicm 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++) {
1.8 nicm 147: free((void *)menu->items[i].name);
148: free((void *)menu->items[i].command);
1.1 nicm 149: }
150: free(menu->items);
151:
1.8 nicm 152: free((void *)menu->title);
1.1 nicm 153: free(menu);
154: }
155:
1.35 nicm 156: struct screen *
157: menu_mode_cb(__unused struct client *c, void *data, __unused u_int *cx,
158: __unused u_int *cy)
1.15 nicm 159: {
1.35 nicm 160: struct menu_data *md = data;
1.15 nicm 161:
1.27 nicm 162: return (&md->s);
1.15 nicm 163: }
164:
1.38 nicm 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)
1.35 nicm 169: {
170: struct menu_data *md = data;
171: struct menu *menu = md->menu;
172:
1.38 nicm 173: server_client_overlay_range(md->px, md->py, menu->width + 4,
174: menu->count + 2, px, py, nx, r);
1.35 nicm 175: }
176:
177: void
178: menu_draw_cb(struct client *c, void *data,
179: __unused struct screen_redraw_ctx *rctx)
1.1 nicm 180: {
1.35 nicm 181: struct menu_data *md = data;
1.1 nicm 182: struct tty *tty = &c->tty;
183: struct screen *s = &md->s;
184: struct menu *menu = md->menu;
185: struct screen_write_ctx ctx;
1.13 nicm 186: u_int i, px = md->px, py = md->py;
1.22 nicm 187: struct grid_cell gc;
188:
1.25 nicm 189: style_apply(&gc, c->session->curw->window->options, "mode-style", NULL);
1.1 nicm 190:
1.27 nicm 191: screen_write_start(&ctx, s);
1.1 nicm 192: screen_write_clearscreen(&ctx, 8);
1.22 nicm 193: screen_write_menu(&ctx, menu, md->choice, &gc);
1.1 nicm 194: screen_write_stop(&ctx);
195:
1.27 nicm 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: }
1.1 nicm 200: }
201:
1.35 nicm 202: void
203: menu_free_cb(__unused struct client *c, void *data)
1.1 nicm 204: {
1.35 nicm 205: struct menu_data *md = data;
1.1 nicm 206:
207: if (md->item != NULL)
1.10 nicm 208: cmdq_continue(md->item);
1.1 nicm 209:
1.3 nicm 210: if (md->cb != NULL)
211: md->cb(md->menu, UINT_MAX, KEYC_NONE, md->data);
212:
1.1 nicm 213: screen_free(&md->s);
214: menu_free(md->menu);
215: free(md);
216: }
217:
1.35 nicm 218: int
219: menu_key_cb(struct client *c, void *data, struct key_event *event)
1.1 nicm 220: {
1.35 nicm 221: struct menu_data *md = data;
1.1 nicm 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;
1.31 nicm 226: const char *name = NULL;
1.1 nicm 227: const struct menu_item *item;
1.21 nicm 228: struct cmdq_state *state;
229: enum cmd_parse_status status;
230: char *error;
1.1 nicm 231:
232: if (KEYC_IS_MOUSE(event->key)) {
1.12 nicm 233: if (md->flags & MENU_NOMOUSE) {
234: if (MOUSE_BUTTONS(m->b) != 0)
235: return (1);
1.1 nicm 236: return (0);
1.12 nicm 237: }
1.1 nicm 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) {
1.32 nicm 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: }
1.1 nicm 251: if (md->choice != -1) {
252: md->choice = -1;
253: c->flags |= CLIENT_REDRAWOVERLAY;
254: }
255: return (0);
256: }
1.32 nicm 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: }
1.7 nicm 264: md->choice = m->y - (md->py + 1);
1.1 nicm 265: if (md->choice != old)
266: c->flags |= CLIENT_REDRAWOVERLAY;
267: return (0);
268: }
1.11 nicm 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: }
1.29 nicm 278: switch (event->key & ~KEYC_MASK_FLAGS) {
1.1 nicm 279: case KEYC_UP:
1.11 nicm 280: case 'k':
1.9 nicm 281: if (old == -1)
282: old = 0;
1.1 nicm 283: do {
284: if (md->choice == -1 || md->choice == 0)
285: md->choice = count - 1;
286: else
287: md->choice--;
1.9 nicm 288: name = menu->items[md->choice].name;
289: } while ((name == NULL || *name == '-') && md->choice != old);
1.1 nicm 290: c->flags |= CLIENT_REDRAWOVERLAY;
291: return (0);
1.26 nicm 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 */
1.1 nicm 302: case KEYC_DOWN:
1.11 nicm 303: case 'j':
1.9 nicm 304: if (old == -1)
305: old = 0;
1.1 nicm 306: do {
307: if (md->choice == -1 || md->choice == count - 1)
308: md->choice = 0;
1.9 nicm 309: else
310: md->choice++;
311: name = menu->items[md->choice].name;
312: } while ((name == NULL || *name == '-') && md->choice != old);
1.1 nicm 313: c->flags |= CLIENT_REDRAWOVERLAY;
314: return (0);
1.26 nicm 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;
1.1 nicm 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];
1.33 nicm 354: if (item->name == NULL || *item->name == '-') {
355: if (md->flags & MENU_STAYOPEN)
356: return (0);
1.1 nicm 357: return (1);
1.33 nicm 358: }
1.1 nicm 359: if (md->cb != NULL) {
360: md->cb(md->menu, md->choice, item->key, md->data);
1.3 nicm 361: md->cb = NULL;
1.1 nicm 362: return (1);
363: }
1.5 nicm 364:
1.21 nicm 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);
1.1 nicm 375: }
1.21 nicm 376: cmdq_free_state(state);
377:
1.1 nicm 378: return (1);
379: }
380:
1.35 nicm 381: struct menu_data *
382: menu_prepare(struct menu *menu, int flags, struct cmdq_item *item, u_int px,
1.1 nicm 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;
1.23 nicm 387: u_int i;
388: const char *name;
1.1 nicm 389:
390: if (c->tty.sx < menu->width + 4 || c->tty.sy < menu->count + 2)
1.35 nicm 391: return (NULL);
1.14 nicm 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;
1.1 nicm 396:
397: md = xcalloc(1, sizeof *md);
398: md->item = item;
399: md->flags = flags;
400:
1.3 nicm 401: if (fs != NULL)
402: cmd_find_copy_state(&md->fs, fs);
1.1 nicm 403: screen_init(&md->s, menu->width + 4, menu->count + 2, 0);
1.27 nicm 404: if (~md->flags & MENU_NOMOUSE)
1.37 nicm 405: md->s.mode |= (MODE_MOUSE_ALL|MODE_MOUSE_BUTTON);
1.28 nicm 406: md->s.mode &= ~MODE_CURSOR;
1.1 nicm 407:
408: md->px = px;
409: md->py = py;
410:
411: md->menu = menu;
1.23 nicm 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;
1.1 nicm 424:
425: md->cb = cb;
426: md->data = data;
1.35 nicm 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;
1.1 nicm 436:
1.35 nicm 437: md = menu_prepare(menu, flags, item, px, py, c, fs, cb, data);
438: if (md == NULL)
439: return (-1);
1.15 nicm 440: server_client_set_overlay(c, 0, NULL, menu_mode_cb, menu_draw_cb,
1.34 nicm 441: menu_key_cb, menu_free_cb, NULL, md);
1.1 nicm 442: return (0);
443: }