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