Annotation of src/usr.bin/tmux/cmd-display-menu.c, Revision 1.37
1.37 ! nicm 1: /* $OpenBSD: cmd-display-menu.c,v 1.36 2021/10/20 09:50:40 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:
1.23 nicm 21: #include <paths.h>
1.1 nicm 22: #include <stdlib.h>
23: #include <string.h>
24:
25: #include "tmux.h"
26:
27: /*
28: * Display a menu on a client.
29: */
30:
1.32 nicm 31: static enum args_parse_type cmd_display_menu_args_parse(struct args *,
32: u_int, char **);
33: static enum cmd_retval cmd_display_menu_exec(struct cmd *,
34: struct cmdq_item *);
35: static enum cmd_retval cmd_display_popup_exec(struct cmd *,
36: struct cmdq_item *);
1.1 nicm 37:
38: const struct cmd_entry cmd_display_menu_entry = {
39: .name = "display-menu",
40: .alias = "menu",
41:
1.32 nicm 42: .args = { "c:t:OT:x:y:", 1, -1, cmd_display_menu_args_parse },
1.20 nicm 43: .usage = "[-O] [-c target-client] " CMD_TARGET_PANE_USAGE " [-T title] "
1.4 nicm 44: "[-x position] [-y position] name key command ...",
1.1 nicm 45:
46: .target = { 't', CMD_FIND_PANE, 0 },
47:
1.18 nicm 48: .flags = CMD_AFTERHOOK|CMD_CLIENT_CFLAG,
1.1 nicm 49: .exec = cmd_display_menu_exec
50: };
51:
1.7 nicm 52: const struct cmd_entry cmd_display_popup_entry = {
53: .name = "display-popup",
54: .alias = "popup",
55:
1.37 ! nicm 56: .args = { "Bb:Cc:d:e:Eh:s:S:t:T:w:x:y:", 0, -1, NULL },
1.35 nicm 57: .usage = "[-BCE] [-b border-lines] [-c target-client] "
58: "[-d start-directory] [-e environment] [-h height] "
1.37 ! nicm 59: "[-s style] [-S border-style] " CMD_TARGET_PANE_USAGE
! 60: "[-T title] [-w width] [-x position] [-y position] "
! 61: "[shell-command]",
1.7 nicm 62:
63: .target = { 't', CMD_FIND_PANE, 0 },
64:
1.18 nicm 65: .flags = CMD_AFTERHOOK|CMD_CLIENT_CFLAG,
1.7 nicm 66: .exec = cmd_display_popup_exec
67: };
1.32 nicm 68:
69: static enum args_parse_type
70: cmd_display_menu_args_parse(struct args *args, u_int idx, __unused char **cause)
71: {
72: u_int i = 0;
73: enum args_parse_type type = ARGS_PARSE_STRING;
74:
75: for (;;) {
76: type = ARGS_PARSE_STRING;
77: if (i == idx)
78: break;
79: if (*args_string(args, i++) == '\0')
80: continue;
81:
82: type = ARGS_PARSE_STRING;
83: if (i++ == idx)
84: break;
85:
86: type = ARGS_PARSE_COMMANDS_OR_STRING;
87: if (i++ == idx)
88: break;
89: }
90: return (type);
91: }
1.7 nicm 92:
1.21 nicm 93: static int
1.18 nicm 94: cmd_display_menu_get_position(struct client *tc, struct cmdq_item *item,
1.5 nicm 95: struct args *args, u_int *px, u_int *py, u_int w, u_int h)
96: {
1.18 nicm 97: struct tty *tty = &tc->tty;
1.14 nicm 98: struct cmd_find_state *target = cmdq_get_target(item);
1.17 nicm 99: struct key_event *event = cmdq_get_event(item);
1.18 nicm 100: struct session *s = tc->session;
1.14 nicm 101: struct winlink *wl = target->wl;
102: struct window_pane *wp = target->wp;
1.21 nicm 103: struct style_ranges *ranges = NULL;
104: struct style_range *sr = NULL;
1.5 nicm 105: const char *xp, *yp;
1.21 nicm 106: char *p;
107: int top;
108: u_int line, ox, oy, sx, sy, lines, position;
109: long n;
110: struct format_tree *ft;
111:
112: /*
113: * Work out the position from the -x and -y arguments. This is the
114: * bottom-left position.
115: */
116:
117: /* If the popup is too big, stop now. */
118: if (w > tty->sx || h > tty->sy)
119: return (0);
120:
121: /* Create format with mouse position if any. */
122: ft = format_create_from_target(item);
123: if (event->m.valid) {
124: format_add(ft, "popup_mouse_x", "%u", event->m.x);
125: format_add(ft, "popup_mouse_y", "%u", event->m.y);
1.11 nicm 126: }
1.5 nicm 127:
1.21 nicm 128: /*
129: * If there are any status lines, add this window position and the
130: * status line position.
131: */
132: top = status_at_line(tc);
133: if (top != -1) {
134: lines = status_line_size(tc);
135: if (top == 0)
136: top = lines;
1.5 nicm 137: else
1.21 nicm 138: top = 0;
139: position = options_get_number(s->options, "status-position");
140:
141: for (line = 0; line < lines; line++) {
142: ranges = &tc->status.entries[line].ranges;
1.11 nicm 143: TAILQ_FOREACH(sr, ranges, entry) {
1.5 nicm 144: if (sr->type != STYLE_RANGE_WINDOW)
145: continue;
146: if (sr->argument == (u_int)wl->idx)
147: break;
148: }
149: if (sr != NULL)
1.21 nicm 150: break;
151: }
152:
153: if (sr != NULL) {
154: format_add(ft, "popup_window_status_line_x", "%u",
155: sr->start);
156: if (position == 0) {
157: format_add(ft, "popup_window_status_line_y",
158: "%u", line + 1 + h);
159: } else {
160: format_add(ft, "popup_window_status_line_y",
161: "%u", tty->sy - lines + line);
162: }
163: }
164:
165: if (position == 0)
166: format_add(ft, "popup_status_line_y", "%u", lines + h);
167: else {
168: format_add(ft, "popup_status_line_y", "%u",
169: tty->sy - lines);
1.5 nicm 170: }
171: } else
1.21 nicm 172: top = 0;
1.5 nicm 173:
1.21 nicm 174: /* Popup width and height. */
175: format_add(ft, "popup_width", "%u", w);
176: format_add(ft, "popup_height", "%u", h);
177:
178: /* Position so popup is in the centre. */
179: n = (long)(tty->sx - 1) / 2 - w / 2;
180: if (n < 0)
181: format_add(ft, "popup_centre_x", "%u", 0);
182: else
183: format_add(ft, "popup_centre_x", "%ld", n);
184: n = (tty->sy - 1) / 2 + h / 2;
1.22 nicm 185: if (n >= tty->sy)
1.21 nicm 186: format_add(ft, "popup_centre_y", "%u", tty->sy - h);
187: else
188: format_add(ft, "popup_centre_y", "%ld", n);
189:
190: /* Position of popup relative to mouse. */
191: if (event->m.valid) {
192: n = (long)event->m.x - w / 2;
193: if (n < 0)
194: format_add(ft, "popup_mouse_centre_x", "%u", 0);
195: else
196: format_add(ft, "popup_mouse_centre_x", "%ld", n);
197: n = event->m.y - h / 2;
198: if (n + h >= tty->sy) {
199: format_add(ft, "popup_mouse_centre_y", "%u",
200: tty->sy - h);
201: } else
202: format_add(ft, "popup_mouse_centre_y", "%ld", n);
203: n = (long)event->m.y + h;
1.26 nicm 204: if (n >= tty->sy)
1.25 nicm 205: format_add(ft, "popup_mouse_top", "%u", tty->sy - 1);
1.21 nicm 206: else
207: format_add(ft, "popup_mouse_top", "%ld", n);
208: n = event->m.y - h;
209: if (n < 0)
210: format_add(ft, "popup_mouse_bottom", "%u", 0);
211: else
212: format_add(ft, "popup_mouse_bottom", "%ld", n);
213: }
214:
215: /* Position in pane. */
216: tty_window_offset(&tc->tty, &ox, &oy, &sx, &sy);
217: n = top + wp->yoff - oy + h;
218: if (n >= tty->sy)
219: format_add(ft, "popup_pane_top", "%u", tty->sy - h);
220: else
221: format_add(ft, "popup_pane_top", "%ld", n);
222: format_add(ft, "popup_pane_bottom", "%u", top + wp->yoff + wp->sy - oy);
223: format_add(ft, "popup_pane_left", "%u", wp->xoff - ox);
224: n = (long)wp->xoff + wp->sx - ox - w;
225: if (n < 0)
226: format_add(ft, "popup_pane_right", "%u", 0);
227: else
228: format_add(ft, "popup_pane_right", "%ld", n);
229:
230: /* Expand horizontal position. */
231: xp = args_get(args, 'x');
232: if (xp == NULL || strcmp(xp, "C") == 0)
233: xp = "#{popup_centre_x}";
234: else if (strcmp(xp, "R") == 0)
1.24 nicm 235: xp = "#{popup_pane_right}";
1.21 nicm 236: else if (strcmp(xp, "P") == 0)
237: xp = "#{popup_pane_left}";
238: else if (strcmp(xp, "M") == 0)
239: xp = "#{popup_mouse_centre_x}";
240: else if (strcmp(xp, "W") == 0)
241: xp = "#{popup_window_status_line_x}";
242: p = format_expand(ft, xp);
243: n = strtol(p, NULL, 10);
244: if (n + w >= tty->sx)
245: n = tty->sx - w;
246: else if (n < 0)
247: n = 0;
248: *px = n;
1.31 nicm 249: log_debug("%s: -x: %s = %s = %u (-w %u)", __func__, xp, p, *px, w);
1.21 nicm 250: free(p);
251:
252: /* Expand vertical position */
1.5 nicm 253: yp = args_get(args, 'y');
1.10 nicm 254: if (yp == NULL || strcmp(yp, "C") == 0)
1.21 nicm 255: yp = "#{popup_centre_y}";
256: else if (strcmp(yp, "P") == 0)
257: yp = "#{popup_pane_bottom}";
258: else if (strcmp(yp, "M") == 0)
259: yp = "#{popup_mouse_top}";
260: else if (strcmp(yp, "S") == 0)
261: yp = "#{popup_status_line_y}";
262: else if (strcmp(yp, "W") == 0)
263: yp = "#{popup_window_status_line_y}";
264: p = format_expand(ft, yp);
265: n = strtol(p, NULL, 10);
266: if (n < h)
267: n = 0;
1.5 nicm 268: else
1.21 nicm 269: n -= h;
270: if (n + h >= tty->sy)
271: n = tty->sy - h;
272: else if (n < 0)
273: n = 0;
274: *py = n;
1.31 nicm 275: log_debug("%s: -y: %s = %s = %u (-h %u)", __func__, yp, p, *py, h);
1.21 nicm 276: free(p);
277:
278: return (1);
1.5 nicm 279: }
280:
1.1 nicm 281: static enum cmd_retval
282: cmd_display_menu_exec(struct cmd *self, struct cmdq_item *item)
283: {
1.13 nicm 284: struct args *args = cmd_get_args(self);
1.14 nicm 285: struct cmd_find_state *target = cmdq_get_target(item);
1.17 nicm 286: struct key_event *event = cmdq_get_event(item);
1.18 nicm 287: struct client *tc = cmdq_get_target_client(item);
1.1 nicm 288: struct menu *menu = NULL;
1.4 nicm 289: struct menu_item menu_item;
1.28 nicm 290: const char *key, *name;
291: char *title;
292: int flags = 0;
293: u_int px, py, i, count = args_count(args);
1.1 nicm 294:
1.18 nicm 295: if (tc->overlay_draw != NULL)
1.1 nicm 296: return (CMD_RETURN_NORMAL);
297:
298: if (args_has(args, 'T'))
1.18 nicm 299: title = format_single_from_target(item, args_get(args, 'T'));
1.1 nicm 300: else
301: title = xstrdup("");
1.4 nicm 302: menu = menu_create(title);
303:
1.28 nicm 304: for (i = 0; i != count; /* nothing */) {
305: name = args_string(args, i++);
1.4 nicm 306: if (*name == '\0') {
1.18 nicm 307: menu_add_item(menu, NULL, item, tc, target);
1.4 nicm 308: continue;
309: }
310:
1.28 nicm 311: if (count - i < 2) {
1.4 nicm 312: cmdq_error(item, "not enough arguments");
313: free(title);
314: menu_free(menu);
315: return (CMD_RETURN_ERROR);
316: }
1.28 nicm 317: key = args_string(args, i++);
1.4 nicm 318:
319: menu_item.name = name;
320: menu_item.key = key_string_lookup_string(key);
1.28 nicm 321: menu_item.command = args_string(args, i++);
1.4 nicm 322:
1.18 nicm 323: menu_add_item(menu, &menu_item, item, tc, target);
1.4 nicm 324: }
1.1 nicm 325: free(title);
326: if (menu == NULL) {
1.4 nicm 327: cmdq_error(item, "invalid menu arguments");
1.1 nicm 328: return (CMD_RETURN_ERROR);
329: }
330: if (menu->count == 0) {
331: menu_free(menu);
332: return (CMD_RETURN_NORMAL);
333: }
1.21 nicm 334: if (!cmd_display_menu_get_position(tc, item, args, &px, &py,
335: menu->width + 4, menu->count + 2)) {
336: menu_free(menu);
337: return (CMD_RETURN_NORMAL);
338: }
1.1 nicm 339:
1.20 nicm 340: if (args_has(args, 'O'))
341: flags |= MENU_STAYOPEN;
1.17 nicm 342: if (!event->m.valid)
1.1 nicm 343: flags |= MENU_NOMOUSE;
1.18 nicm 344: if (menu_display(menu, flags, item, px, py, tc, target, NULL,
345: NULL) != 0)
1.7 nicm 346: return (CMD_RETURN_NORMAL);
347: return (CMD_RETURN_WAIT);
348: }
349:
350: static enum cmd_retval
351: cmd_display_popup_exec(struct cmd *self, struct cmdq_item *item)
352: {
1.13 nicm 353: struct args *args = cmd_get_args(self);
1.14 nicm 354: struct cmd_find_state *target = cmdq_get_target(item);
1.23 nicm 355: struct session *s = target->s;
1.18 nicm 356: struct client *tc = cmdq_get_target_client(item);
357: struct tty *tty = &tc->tty;
1.28 nicm 358: const char *value, *shell, *shellcmd = NULL;
1.37 ! nicm 359: const char *style = args_get(args, 's');
! 360: const char *border_style = args_get(args, 'S');
1.36 nicm 361: char *cwd, *cause = NULL, **argv = NULL, *title;
1.28 nicm 362: int flags = 0, argc = 0;
1.35 nicm 363: enum box_lines lines = BOX_LINES_DEFAULT;
1.28 nicm 364: u_int px, py, w, h, count = args_count(args);
1.34 nicm 365: struct args_value *av;
366: struct environ *env = NULL;
1.35 nicm 367: struct options *o = s->curw->window->options;
368: struct options_entry *oe;
1.7 nicm 369:
370: if (args_has(args, 'C')) {
1.18 nicm 371: server_client_clear_overlay(tc);
1.7 nicm 372: return (CMD_RETURN_NORMAL);
373: }
1.18 nicm 374: if (tc->overlay_draw != NULL)
1.7 nicm 375: return (CMD_RETURN_NORMAL);
376:
1.23 nicm 377: h = tty->sy / 2;
1.7 nicm 378: if (args_has(args, 'h')) {
1.18 nicm 379: h = args_percentage(args, 'h', 1, tty->sy, tty->sy, &cause);
1.7 nicm 380: if (cause != NULL) {
381: cmdq_error(item, "height %s", cause);
382: free(cause);
383: return (CMD_RETURN_ERROR);
384: }
385: }
386:
1.23 nicm 387: w = tty->sx / 2;
1.7 nicm 388: if (args_has(args, 'w')) {
1.18 nicm 389: w = args_percentage(args, 'w', 1, tty->sx, tty->sx, &cause);
1.7 nicm 390: if (cause != NULL) {
391: cmdq_error(item, "width %s", cause);
392: free(cause);
393: return (CMD_RETURN_ERROR);
394: }
395: }
396:
1.31 nicm 397: if (w > tty->sx)
398: w = tty->sx;
399: if (h > tty->sy)
400: h = tty->sy;
1.21 nicm 401: if (!cmd_display_menu_get_position(tc, item, args, &px, &py, w, h))
402: return (CMD_RETURN_NORMAL);
1.7 nicm 403:
1.35 nicm 404: value = args_get(args, 'b');
405: if (args_has(args, 'B'))
406: lines = BOX_LINES_NONE;
407: else if (value != NULL) {
408: oe = options_get(o, "popup-border-lines");
409: lines = options_find_choice(options_table_entry(oe), value,
410: &cause);
411: if (cause != NULL) {
412: cmdq_error(item, "popup-border-lines %s", cause);
413: free(cause);
414: return (CMD_RETURN_ERROR);
415: }
416: }
417:
1.7 nicm 418: value = args_get(args, 'd');
419: if (value != NULL)
1.18 nicm 420: cwd = format_single_from_target(item, value);
1.7 nicm 421: else
1.23 nicm 422: cwd = xstrdup(server_client_get_cwd(tc, s));
1.28 nicm 423: if (count == 0)
1.23 nicm 424: shellcmd = options_get_string(s->options, "default-command");
1.28 nicm 425: else if (count == 1)
426: shellcmd = args_string(args, 0);
427: if (count <= 1 && (shellcmd == NULL || *shellcmd == '\0')) {
1.23 nicm 428: shellcmd = NULL;
1.28 nicm 429: shell = options_get_string(s->options, "default-shell");
430: if (!checkshell(shell))
431: shell = _PATH_BSHELL;
432: cmd_append_argv(&argc, &argv, shell);
433: } else
1.33 nicm 434: args_to_vector(args, &argc, &argv);
1.7 nicm 435:
1.34 nicm 436: if (args_has(args, 'e') >= 1) {
437: env = environ_create();
438: av = args_first_value(args, 'e');
439: while (av != NULL) {
440: environ_put(env, av->string, 0);
441: av = args_next_value(av);
442: }
443: }
444:
1.36 nicm 445: if (args_has(args, 'T'))
446: title = format_single_from_target(item, args_get(args, 'T'));
447: else
448: title = xstrdup("");
1.9 nicm 449: if (args_has(args, 'E') > 1)
450: flags |= POPUP_CLOSEEXITZERO;
451: else if (args_has(args, 'E'))
1.7 nicm 452: flags |= POPUP_CLOSEEXIT;
1.35 nicm 453: if (popup_display(flags, lines, item, px, py, w, h, env, shellcmd, argc,
1.37 ! nicm 454: argv, cwd, title, tc, s, style, border_style, NULL, NULL) != 0) {
1.28 nicm 455: cmd_free_argv(argc, argv);
1.34 nicm 456: if (env != NULL)
457: environ_free(env);
1.36 nicm 458: free(title);
1.1 nicm 459: return (CMD_RETURN_NORMAL);
1.28 nicm 460: }
1.34 nicm 461: if (env != NULL)
462: environ_free(env);
1.36 nicm 463: free(title);
1.28 nicm 464: cmd_free_argv(argc, argv);
1.1 nicm 465: return (CMD_RETURN_WAIT);
466: }