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