Annotation of src/usr.bin/tmux/cmd-display-menu.c, Revision 1.10
1.10 ! nicm 1: /* $OpenBSD: cmd-display-menu.c,v 1.9 2020/03/28 09:51:12 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: /*
27: * Display a menu on a client.
28: */
29:
30: static enum cmd_retval cmd_display_menu_exec(struct cmd *,
31: struct cmdq_item *);
1.7 nicm 32: static enum cmd_retval cmd_display_popup_exec(struct cmd *,
33: struct cmdq_item *);
1.1 nicm 34:
35: const struct cmd_entry cmd_display_menu_entry = {
36: .name = "display-menu",
37: .alias = "menu",
38:
1.4 nicm 39: .args = { "c:t:T:x:y:", 1, -1 },
40: .usage = "[-c target-client] " CMD_TARGET_PANE_USAGE " [-T title] "
41: "[-x position] [-y position] name key command ...",
1.1 nicm 42:
43: .target = { 't', CMD_FIND_PANE, 0 },
44:
45: .flags = CMD_AFTERHOOK,
46: .exec = cmd_display_menu_exec
47: };
48:
1.7 nicm 49: const struct cmd_entry cmd_display_popup_entry = {
50: .name = "display-popup",
51: .alias = "popup",
52:
53: .args = { "CEKc:d:h:R:t:w:x:y:", 0, -1 },
54: .usage = "[-CEK] [-c target-client] [-d start-directory] [-h height] "
55: "[-R shell-command] " CMD_TARGET_PANE_USAGE " [-w width] "
56: "[-x position] [-y position] [command line ...]",
57:
58: .target = { 't', CMD_FIND_PANE, 0 },
59:
60: .flags = CMD_AFTERHOOK,
61: .exec = cmd_display_popup_exec
62: };
63:
1.5 nicm 64: static void
65: cmd_display_menu_get_position(struct client *c, struct cmdq_item *item,
66: struct args *args, u_int *px, u_int *py, u_int w, u_int h)
67: {
68: struct winlink *wl = item->target.wl;
69: struct window_pane *wp = item->target.wp;
70: struct style_range *sr;
71: const char *xp, *yp;
72: int at = status_at_line(c);
73: u_int ox, oy, sx, sy;
74:
75: xp = args_get(args, 'x');
1.10 ! nicm 76: if (xp == NULL || strcmp(xp, "C") == 0)
! 77: *px = (c->tty.sx - 1) / 2 - w / 2;
1.5 nicm 78: else if (strcmp(xp, "R") == 0)
79: *px = c->tty.sx - 1;
80: else if (strcmp(xp, "P") == 0) {
81: tty_window_offset(&c->tty, &ox, &oy, &sx, &sy);
82: if (wp->xoff >= ox)
83: *px = wp->xoff - ox;
84: else
85: *px = 0;
86: } else if (strcmp(xp, "M") == 0 && item->shared->mouse.valid) {
87: if (item->shared->mouse.x > w / 2)
88: *px = item->shared->mouse.x - w / 2;
89: else
90: *px = 0;
91: } else if (strcmp(xp, "W") == 0) {
92: if (at == -1)
93: *px = 0;
94: else {
95: TAILQ_FOREACH(sr, &c->status.entries[0].ranges, entry) {
96: if (sr->type != STYLE_RANGE_WINDOW)
97: continue;
98: if (sr->argument == (u_int)wl->idx)
99: break;
100: }
101: if (sr != NULL)
102: *px = sr->start;
103: else
104: *px = 0;
105: }
106: } else
107: *px = strtoul(xp, NULL, 10);
108: if ((*px) + w >= c->tty.sx)
109: *px = c->tty.sx - w;
110:
111: yp = args_get(args, 'y');
1.10 ! nicm 112: if (yp == NULL || strcmp(yp, "C") == 0)
1.6 nicm 113: *py = (c->tty.sy - 1) / 2 + h / 2;
1.5 nicm 114: else if (strcmp(yp, "P") == 0) {
115: tty_window_offset(&c->tty, &ox, &oy, &sx, &sy);
116: if (wp->yoff + wp->sy >= oy)
117: *py = wp->yoff + wp->sy - oy;
118: else
119: *py = 0;
120: } else if (strcmp(yp, "M") == 0 && item->shared->mouse.valid)
121: *py = item->shared->mouse.y + h;
122: else if (strcmp(yp, "S") == 0) {
123: if (at == -1)
124: *py = c->tty.sy;
125: else if (at == 0)
126: *py = status_line_size(c) + h;
127: else
128: *py = at;
129: } else
130: *py = strtoul(yp, NULL, 10);
131: if (*py < h)
132: *py = 0;
133: else
134: *py -= h;
135: if ((*py) + h >= c->tty.sy)
136: *py = c->tty.sy - h;
137: }
138:
1.1 nicm 139: static enum cmd_retval
140: cmd_display_menu_exec(struct cmd *self, struct cmdq_item *item)
141: {
142: struct args *args = self->args;
143: struct client *c;
144: struct session *s = item->target.s;
145: struct winlink *wl = item->target.wl;
146: struct window_pane *wp = item->target.wp;
147: struct cmd_find_state *fs = &item->target;
148: struct menu *menu = NULL;
1.4 nicm 149: struct menu_item menu_item;
1.5 nicm 150: const char *key;
1.4 nicm 151: char *title, *name;
1.6 nicm 152: int flags = 0, i;
1.5 nicm 153: u_int px, py;
1.1 nicm 154:
155: if ((c = cmd_find_client(item, args_get(args, 'c'), 0)) == NULL)
156: return (CMD_RETURN_ERROR);
157: if (c->overlay_draw != NULL)
158: return (CMD_RETURN_NORMAL);
159:
160: if (args_has(args, 'T'))
161: title = format_single(NULL, args_get(args, 'T'), c, s, wl, wp);
162: else
163: title = xstrdup("");
1.4 nicm 164:
165: menu = menu_create(title);
166:
167: for (i = 0; i != args->argc; /* nothing */) {
168: name = args->argv[i++];
169: if (*name == '\0') {
170: menu_add_item(menu, NULL, item, c, fs);
171: continue;
172: }
173:
174: if (args->argc - i < 2) {
175: cmdq_error(item, "not enough arguments");
176: free(title);
177: menu_free(menu);
178: return (CMD_RETURN_ERROR);
179: }
180: key = args->argv[i++];
181:
182: menu_item.name = name;
183: menu_item.key = key_string_lookup_string(key);
184: menu_item.command = args->argv[i++];
185:
186: menu_add_item(menu, &menu_item, item, c, fs);
187: }
1.1 nicm 188: free(title);
189: if (menu == NULL) {
1.4 nicm 190: cmdq_error(item, "invalid menu arguments");
1.1 nicm 191: return (CMD_RETURN_ERROR);
192: }
193: if (menu->count == 0) {
194: menu_free(menu);
195: return (CMD_RETURN_NORMAL);
196: }
1.5 nicm 197: cmd_display_menu_get_position(c, item, args, &px, &py, menu->width + 4,
198: menu->count + 2);
1.1 nicm 199:
200: if (!item->shared->mouse.valid)
201: flags |= MENU_NOMOUSE;
202: if (menu_display(menu, flags, item, px, py, c, fs, NULL, NULL) != 0)
1.7 nicm 203: return (CMD_RETURN_NORMAL);
204: return (CMD_RETURN_WAIT);
205: }
206:
207: static enum cmd_retval
208: cmd_display_popup_exec(struct cmd *self, struct cmdq_item *item)
209: {
210: struct args *args = self->args;
211: struct client *c;
212: struct cmd_find_state *fs = &item->target;
213: const char *value, *cmd = NULL, **lines = NULL;
214: const char *shellcmd = NULL;
215: char *cwd, *cause;
216: int flags = 0;
217: u_int px, py, w, h, nlines = 0;
218:
219: if ((c = cmd_find_client(item, args_get(args, 'c'), 0)) == NULL)
220: return (CMD_RETURN_ERROR);
221: if (args_has(args, 'C')) {
222: server_client_clear_overlay(c);
223: return (CMD_RETURN_NORMAL);
224: }
225: if (c->overlay_draw != NULL)
226: return (CMD_RETURN_NORMAL);
227:
228: if (args->argc >= 1)
229: cmd = args->argv[0];
230: if (args->argc >= 2) {
231: lines = (const char **)args->argv + 1;
232: nlines = args->argc - 1;
233: }
234:
235: if (nlines != 0)
1.8 nicm 236: h = popup_height(nlines, lines) + 2;
1.7 nicm 237: else
238: h = c->tty.sy / 2;
239: if (args_has(args, 'h')) {
240: h = args_percentage(args, 'h', 1, c->tty.sy, c->tty.sy, &cause);
241: if (cause != NULL) {
242: cmdq_error(item, "height %s", cause);
243: free(cause);
244: return (CMD_RETURN_ERROR);
245: }
246: }
247:
248: if (nlines != 0)
249: w = popup_width(item, nlines, lines, c, fs) + 2;
250: else
251: w = c->tty.sx / 2;
252: if (args_has(args, 'w')) {
253: w = args_percentage(args, 'w', 1, c->tty.sx, c->tty.sx, &cause);
254: if (cause != NULL) {
255: cmdq_error(item, "width %s", cause);
256: free(cause);
257: return (CMD_RETURN_ERROR);
258: }
259: }
260:
1.8 nicm 261: if (w > c->tty.sx - 1)
262: w = c->tty.sx - 1;
263: if (h > c->tty.sy - 1)
264: h = c->tty.sy - 1;
1.7 nicm 265: cmd_display_menu_get_position(c, item, args, &px, &py, w, h);
266:
267: value = args_get(args, 'd');
268: if (value != NULL)
269: cwd = format_single(NULL, value, c, fs->s, fs->wl, fs->wp);
270: else
271: cwd = xstrdup(server_client_get_cwd(c, fs->s));
272:
273: value = args_get(args, 'R');
274: if (value != NULL)
275: shellcmd = format_single(NULL, value, c, fs->s, fs->wl, fs->wp);
276:
277: if (args_has(args, 'K'))
278: flags |= POPUP_WRITEKEYS;
1.9 nicm 279: if (args_has(args, 'E') > 1)
280: flags |= POPUP_CLOSEEXITZERO;
281: else if (args_has(args, 'E'))
1.7 nicm 282: flags |= POPUP_CLOSEEXIT;
283: if (popup_display(flags, item, px, py, w, h, nlines, lines, shellcmd,
284: cmd, cwd, c, fs) != 0)
1.1 nicm 285: return (CMD_RETURN_NORMAL);
286: return (CMD_RETURN_WAIT);
287: }