Annotation of src/usr.bin/tmux/menu.c, Revision 1.48
1.48 ! nicm 1: /* $OpenBSD: menu.c,v 1.47 2022/08/04 12:06:09 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.45 nicm 59: char *s, *trimmed, *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: /*
1.41 nicm 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.
1.40 nicm 95: */
1.41 nicm 96: if (keylen <= max_width / 4)
97: max_width -= keylen;
98: else if (keylen >= max_width || slen >= max_width - keylen)
1.40 nicm 99: key = NULL;
100: }
101:
1.41 nicm 102: if (slen > max_width) {
103: max_width--;
104: suffix = ">";
105: }
1.45 nicm 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);
1.41 nicm 113:
1.8 nicm 114: new_item->name = name;
115: free(s);
116:
117: cmd = item->command;
118: if (cmd != NULL) {
119: if (fs != NULL)
1.24 nicm 120: s = format_single_from_state(qitem, cmd, c, fs);
1.8 nicm 121: else
122: s = format_single(qitem, cmd, c, NULL, NULL, NULL);
1.6 nicm 123: } else
1.8 nicm 124: s = NULL;
125: new_item->command = s;
1.1 nicm 126: new_item->key = item->key;
127:
128: width = format_width(new_item->name);
1.39 nicm 129: if (*new_item->name == '-')
130: width--;
1.1 nicm 131: if (width > menu->width)
132: menu->width = width;
133: }
134:
135: struct menu *
1.8 nicm 136: menu_create(const char *title)
1.1 nicm 137: {
138: struct menu *menu;
139:
140: menu = xcalloc(1, sizeof *menu);
141: menu->title = xstrdup(title);
1.30 nicm 142: menu->width = format_width(title);
1.1 nicm 143:
144: return (menu);
145: }
146:
147: void
148: menu_free(struct menu *menu)
149: {
150: u_int i;
151:
152: for (i = 0; i < menu->count; i++) {
1.8 nicm 153: free((void *)menu->items[i].name);
154: free((void *)menu->items[i].command);
1.1 nicm 155: }
156: free(menu->items);
157:
1.8 nicm 158: free((void *)menu->title);
1.1 nicm 159: free(menu);
160: }
161:
1.35 nicm 162: struct screen *
1.46 nicm 163: menu_mode_cb(__unused struct client *c, void *data, u_int *cx, u_int *cy)
1.15 nicm 164: {
1.35 nicm 165: struct menu_data *md = data;
1.46 nicm 166:
167: *cx = md->px + 2;
168: if (md->choice == -1)
169: *cy = md->py;
170: else
171: *cy = md->py + 1 + md->choice;
1.15 nicm 172:
1.27 nicm 173: return (&md->s);
1.15 nicm 174: }
175:
1.38 nicm 176: /* Return parts of the input range which are not obstructed by the menu. */
177: void
178: menu_check_cb(__unused struct client *c, void *data, u_int px, u_int py,
179: u_int nx, struct overlay_ranges *r)
1.35 nicm 180: {
181: struct menu_data *md = data;
182: struct menu *menu = md->menu;
183:
1.38 nicm 184: server_client_overlay_range(md->px, md->py, menu->width + 4,
185: menu->count + 2, px, py, nx, r);
1.35 nicm 186: }
187:
188: void
189: menu_draw_cb(struct client *c, void *data,
190: __unused struct screen_redraw_ctx *rctx)
1.1 nicm 191: {
1.35 nicm 192: struct menu_data *md = data;
1.1 nicm 193: struct tty *tty = &c->tty;
194: struct screen *s = &md->s;
195: struct menu *menu = md->menu;
196: struct screen_write_ctx ctx;
1.13 nicm 197: u_int i, px = md->px, py = md->py;
1.22 nicm 198: struct grid_cell gc;
199:
1.25 nicm 200: style_apply(&gc, c->session->curw->window->options, "mode-style", NULL);
1.1 nicm 201:
1.27 nicm 202: screen_write_start(&ctx, s);
1.1 nicm 203: screen_write_clearscreen(&ctx, 8);
1.22 nicm 204: screen_write_menu(&ctx, menu, md->choice, &gc);
1.1 nicm 205: screen_write_stop(&ctx);
206:
1.27 nicm 207: for (i = 0; i < screen_size_y(&md->s); i++) {
208: tty_draw_line(tty, s, 0, i, menu->width + 4, px, py + i,
209: &grid_default_cell, NULL);
210: }
1.1 nicm 211: }
212:
1.35 nicm 213: void
214: menu_free_cb(__unused struct client *c, void *data)
1.1 nicm 215: {
1.35 nicm 216: struct menu_data *md = data;
1.1 nicm 217:
218: if (md->item != NULL)
1.10 nicm 219: cmdq_continue(md->item);
1.1 nicm 220:
1.3 nicm 221: if (md->cb != NULL)
222: md->cb(md->menu, UINT_MAX, KEYC_NONE, md->data);
223:
1.1 nicm 224: screen_free(&md->s);
225: menu_free(md->menu);
226: free(md);
227: }
228:
1.35 nicm 229: int
230: menu_key_cb(struct client *c, void *data, struct key_event *event)
1.1 nicm 231: {
1.35 nicm 232: struct menu_data *md = data;
1.1 nicm 233: struct menu *menu = md->menu;
234: struct mouse_event *m = &event->m;
235: u_int i;
236: int count = menu->count, old = md->choice;
1.31 nicm 237: const char *name = NULL;
1.1 nicm 238: const struct menu_item *item;
1.21 nicm 239: struct cmdq_state *state;
240: enum cmd_parse_status status;
241: char *error;
1.1 nicm 242:
243: if (KEYC_IS_MOUSE(event->key)) {
1.12 nicm 244: if (md->flags & MENU_NOMOUSE) {
1.44 nicm 245: if (MOUSE_BUTTONS(m->b) != MOUSE_BUTTON_1)
1.12 nicm 246: return (1);
1.1 nicm 247: return (0);
1.12 nicm 248: }
1.1 nicm 249: if (m->x < md->px ||
250: m->x > md->px + 4 + menu->width ||
251: m->y < md->py + 1 ||
252: m->y > md->py + 1 + count - 1) {
1.32 nicm 253: if (~md->flags & MENU_STAYOPEN) {
254: if (MOUSE_RELEASE(m->b))
255: return (1);
256: } else {
257: if (!MOUSE_RELEASE(m->b) &&
1.44 nicm 258: !MOUSE_WHEEL(m->b) &&
1.32 nicm 259: !MOUSE_DRAG(m->b))
260: return (1);
261: }
1.1 nicm 262: if (md->choice != -1) {
263: md->choice = -1;
264: c->flags |= CLIENT_REDRAWOVERLAY;
265: }
266: return (0);
267: }
1.32 nicm 268: if (~md->flags & MENU_STAYOPEN) {
269: if (MOUSE_RELEASE(m->b))
270: goto chosen;
271: } else {
1.44 nicm 272: if (!MOUSE_WHEEL(m->b) && !MOUSE_DRAG(m->b))
1.32 nicm 273: goto chosen;
274: }
1.7 nicm 275: md->choice = m->y - (md->py + 1);
1.1 nicm 276: if (md->choice != old)
277: c->flags |= CLIENT_REDRAWOVERLAY;
278: return (0);
279: }
1.11 nicm 280: for (i = 0; i < (u_int)count; i++) {
281: name = menu->items[i].name;
282: if (name == NULL || *name == '-')
283: continue;
284: if (event->key == menu->items[i].key) {
285: md->choice = i;
286: goto chosen;
287: }
288: }
1.29 nicm 289: switch (event->key & ~KEYC_MASK_FLAGS) {
1.1 nicm 290: case KEYC_UP:
1.11 nicm 291: case 'k':
1.9 nicm 292: if (old == -1)
293: old = 0;
1.1 nicm 294: do {
295: if (md->choice == -1 || md->choice == 0)
296: md->choice = count - 1;
297: else
298: md->choice--;
1.9 nicm 299: name = menu->items[md->choice].name;
300: } while ((name == NULL || *name == '-') && md->choice != old);
1.1 nicm 301: c->flags |= CLIENT_REDRAWOVERLAY;
302: return (0);
1.26 nicm 303: case KEYC_BSPACE:
304: if (~md->flags & MENU_TAB)
305: break;
306: return (1);
307: case '\011': /* Tab */
308: if (~md->flags & MENU_TAB)
309: break;
310: if (md->choice == count - 1)
311: return (1);
312: /* FALLTHROUGH */
1.1 nicm 313: case KEYC_DOWN:
1.11 nicm 314: case 'j':
1.9 nicm 315: if (old == -1)
316: old = 0;
1.1 nicm 317: do {
318: if (md->choice == -1 || md->choice == count - 1)
319: md->choice = 0;
1.9 nicm 320: else
321: md->choice++;
322: name = menu->items[md->choice].name;
323: } while ((name == NULL || *name == '-') && md->choice != old);
1.1 nicm 324: c->flags |= CLIENT_REDRAWOVERLAY;
325: return (0);
1.26 nicm 326: case KEYC_PPAGE:
327: case '\002': /* C-b */
1.47 nicm 328: if (md->choice < 6)
1.26 nicm 329: md->choice = 0;
1.47 nicm 330: else {
331: i = 5;
332: while (i > 0) {
333: md->choice--;
334: name = menu->items[md->choice].name;
335: if (md->choice != 0 &&
336: (name != NULL && *name != '-'))
337: i--;
338: else if (md->choice == 0)
339: break;
340: }
341: }
342: c->flags |= CLIENT_REDRAWOVERLAY;
343: break;
344: case KEYC_NPAGE:
345: if (md->choice > count - 6) {
346: md->choice = count - 1;
347: name = menu->items[md->choice].name;
348: } else {
349: i = 5;
350: while (i > 0) {
351: md->choice++;
352: name = menu->items[md->choice].name;
353: if (md->choice != count - 1 &&
354: (name != NULL && *name != '-'))
355: i++;
356: else if (md->choice == count - 1)
357: break;
358: }
359: }
360: while (name == NULL || *name == '-') {
361: md->choice--;
362: name = menu->items[md->choice].name;
363: }
364: c->flags |= CLIENT_REDRAWOVERLAY;
365: break;
366: case 'g':
367: case KEYC_HOME:
368: md->choice = 0;
369: name = menu->items[md->choice].name;
370: while (name == NULL || *name == '-') {
1.26 nicm 371: md->choice++;
1.47 nicm 372: name = menu->items[md->choice].name;
373: }
1.26 nicm 374: c->flags |= CLIENT_REDRAWOVERLAY;
375: break;
376: case 'G':
1.47 nicm 377: case KEYC_END:
378: md->choice = count - 1;
379: name = menu->items[md->choice].name;
380: while (name == NULL || *name == '-') {
1.26 nicm 381: md->choice--;
1.47 nicm 382: name = menu->items[md->choice].name;
383: }
1.26 nicm 384: c->flags |= CLIENT_REDRAWOVERLAY;
385: break;
386: case '\006': /* C-f */
387: break;
1.1 nicm 388: case '\r':
389: goto chosen;
390: case '\033': /* Escape */
391: case '\003': /* C-c */
392: case '\007': /* C-g */
393: case 'q':
394: return (1);
395: }
396: return (0);
397:
398: chosen:
399: if (md->choice == -1)
400: return (1);
401: item = &menu->items[md->choice];
1.33 nicm 402: if (item->name == NULL || *item->name == '-') {
403: if (md->flags & MENU_STAYOPEN)
404: return (0);
1.1 nicm 405: return (1);
1.33 nicm 406: }
1.1 nicm 407: if (md->cb != NULL) {
408: md->cb(md->menu, md->choice, item->key, md->data);
1.3 nicm 409: md->cb = NULL;
1.1 nicm 410: return (1);
411: }
1.5 nicm 412:
1.21 nicm 413: if (md->item != NULL)
414: event = cmdq_get_event(md->item);
415: else
416: event = NULL;
417: state = cmdq_new_state(&md->fs, event, 0);
418:
419: status = cmd_parse_and_append(item->command, NULL, c, state, &error);
420: if (status == CMD_PARSE_ERROR) {
421: cmdq_append(c, cmdq_get_error(error));
422: free(error);
1.1 nicm 423: }
1.21 nicm 424: cmdq_free_state(state);
425:
1.1 nicm 426: return (1);
427: }
428:
1.35 nicm 429: struct menu_data *
1.48 ! nicm 430: menu_prepare(struct menu *menu, int flags, int starting_choice,
! 431: struct cmdq_item *item, u_int px, u_int py, struct client *c,
! 432: struct cmd_find_state *fs, menu_choice_cb cb, void *data)
1.1 nicm 433: {
434: struct menu_data *md;
1.48 ! nicm 435: int choice;
1.23 nicm 436: const char *name;
1.1 nicm 437:
438: if (c->tty.sx < menu->width + 4 || c->tty.sy < menu->count + 2)
1.35 nicm 439: return (NULL);
1.14 nicm 440: if (px + menu->width + 4 > c->tty.sx)
441: px = c->tty.sx - menu->width - 4;
442: if (py + menu->count + 2 > c->tty.sy)
443: py = c->tty.sy - menu->count - 2;
1.1 nicm 444:
445: md = xcalloc(1, sizeof *md);
446: md->item = item;
447: md->flags = flags;
448:
1.3 nicm 449: if (fs != NULL)
450: cmd_find_copy_state(&md->fs, fs);
1.1 nicm 451: screen_init(&md->s, menu->width + 4, menu->count + 2, 0);
1.27 nicm 452: if (~md->flags & MENU_NOMOUSE)
1.37 nicm 453: md->s.mode |= (MODE_MOUSE_ALL|MODE_MOUSE_BUTTON);
1.28 nicm 454: md->s.mode &= ~MODE_CURSOR;
1.1 nicm 455:
456: md->px = px;
457: md->py = py;
458:
459: md->menu = menu;
1.48 ! nicm 460: md->choice = -1;
! 461:
1.23 nicm 462: if (md->flags & MENU_NOMOUSE) {
1.48 ! nicm 463: if (starting_choice >= (int)menu->count) {
! 464: starting_choice = menu->count - 1;
! 465: choice = starting_choice + 1;
! 466: for (;;) {
! 467: name = menu->items[choice - 1].name;
! 468: if (name != NULL && *name != '-') {
! 469: md->choice = choice - 1;
! 470: break;
! 471: }
! 472: if (--choice == 0)
! 473: choice = menu->count;
! 474: if (choice == starting_choice + 1)
! 475: break;
! 476: }
! 477: } else if (starting_choice >= 0) {
! 478: choice = starting_choice;
! 479: for (;;) {
! 480: name = menu->items[choice].name;
! 481: if (name != NULL && *name != '-') {
! 482: md->choice = choice;
! 483: break;
! 484: }
! 485: if (++choice == (int)menu->count)
! 486: choice = 0;
! 487: if (choice == starting_choice)
! 488: break;
! 489: }
1.23 nicm 490: }
1.48 ! nicm 491: }
1.1 nicm 492:
493: md->cb = cb;
494: md->data = data;
1.35 nicm 495: return (md);
496: }
497:
498: int
1.48 ! nicm 499: menu_display(struct menu *menu, int flags, int starting_choice,
! 500: struct cmdq_item *item, u_int px, u_int py, struct client *c,
! 501: struct cmd_find_state *fs, menu_choice_cb cb, void *data)
1.35 nicm 502: {
503: struct menu_data *md;
1.1 nicm 504:
1.48 ! nicm 505: md = menu_prepare(menu, flags, starting_choice, item, px, py, c, fs, cb,
! 506: data);
1.35 nicm 507: if (md == NULL)
508: return (-1);
1.15 nicm 509: server_client_set_overlay(c, 0, NULL, menu_mode_cb, menu_draw_cb,
1.34 nicm 510: menu_key_cb, menu_free_cb, NULL, md);
1.1 nicm 511: return (0);
512: }