From 9d83c5e94827db0248226149cffee23296c851aa Mon Sep 17 00:00:00 2001 From: nicm Date: Tue, 3 Nov 2020 08:09:35 +0000 Subject: Expand menu and popup -x and -y as a format, from teo_paul1 at yahoo dot com in GitHub issue 2442. --- cmd-display-menu.c | 256 +++++++++++++++++++++++++++++++++++------------------ 1 file changed, 169 insertions(+), 87 deletions(-) (limited to 'cmd-display-menu.c') diff --git a/cmd-display-menu.c b/cmd-display-menu.c index 205d1243..7115dc81 100644 --- a/cmd-display-menu.c +++ b/cmd-display-menu.c @@ -61,7 +61,7 @@ const struct cmd_entry cmd_display_popup_entry = { .exec = cmd_display_popup_exec }; -static void +static int cmd_display_menu_get_position(struct client *tc, struct cmdq_item *item, struct args *args, u_int *px, u_int *py, u_int w, u_int h) { @@ -71,44 +71,46 @@ cmd_display_menu_get_position(struct client *tc, struct cmdq_item *item, struct session *s = tc->session; struct winlink *wl = target->wl; struct window_pane *wp = target->wp; - struct style_ranges *ranges; - struct style_range *sr; + struct style_ranges *ranges = NULL; + struct style_range *sr = NULL; const char *xp, *yp; - u_int line, ox, oy, sx, sy, lines; - - lines = status_line_size(tc); - for (line = 0; line < lines; line++) { - ranges = &tc->status.entries[line].ranges; - TAILQ_FOREACH(sr, ranges, entry) { - if (sr->type == STYLE_RANGE_WINDOW) - break; - } - if (sr != NULL) - break; + char *p; + int top; + u_int line, ox, oy, sx, sy, lines, position; + long n; + struct format_tree *ft; + + /* + * Work out the position from the -x and -y arguments. This is the + * bottom-left position. + */ + + /* If the popup is too big, stop now. */ + if (w > tty->sx || h > tty->sy) + return (0); + + /* Create format with mouse position if any. */ + ft = format_create_from_target(item); + if (event->m.valid) { + format_add(ft, "popup_mouse_x", "%u", event->m.x); + format_add(ft, "popup_mouse_y", "%u", event->m.y); } - if (line == lines) - ranges = &tc->status.entries[0].ranges; - xp = args_get(args, 'x'); - if (xp == NULL || strcmp(xp, "C") == 0) - *px = (tty->sx - 1) / 2 - w / 2; - else if (strcmp(xp, "R") == 0) - *px = tty->sx - 1; - else if (strcmp(xp, "P") == 0) { - tty_window_offset(&tc->tty, &ox, &oy, &sx, &sy); - if (wp->xoff >= ox) - *px = wp->xoff - ox; - else - *px = 0; - } else if (strcmp(xp, "M") == 0) { - if (event->m.valid && event->m.x > w / 2) - *px = event->m.x - w / 2; + /* + * If there are any status lines, add this window position and the + * status line position. + */ + top = status_at_line(tc); + if (top != -1) { + lines = status_line_size(tc); + if (top == 0) + top = lines; else - *px = 0; - } else if (strcmp(xp, "W") == 0) { - if (status_at_line(tc) == -1) - *px = 0; - else { + top = 0; + position = options_get_number(s->options, "status-position"); + + for (line = 0; line < lines; line++) { + ranges = &tc->status.entries[line].ranges; TAILQ_FOREACH(sr, ranges, entry) { if (sr->type != STYLE_RANGE_WINDOW) continue; @@ -116,61 +118,137 @@ cmd_display_menu_get_position(struct client *tc, struct cmdq_item *item, break; } if (sr != NULL) - *px = sr->start; - else - *px = 0; + break; + } + if (line == lines) + ranges = &tc->status.entries[0].ranges; + + if (sr != NULL) { + format_add(ft, "popup_window_status_line_x", "%u", + sr->start); + if (position == 0) { + format_add(ft, "popup_window_status_line_y", + "%u", line + 1 + h); + } else { + format_add(ft, "popup_window_status_line_y", + "%u", tty->sy - lines + line); + } + } + + if (position == 0) + format_add(ft, "popup_status_line_y", "%u", lines + h); + else { + format_add(ft, "popup_status_line_y", "%u", + tty->sy - lines); } } else - *px = strtoul(xp, NULL, 10); - if ((*px) + w >= tty->sx) - *px = tty->sx - w; + top = 0; - yp = args_get(args, 'y'); - if (yp == NULL || strcmp(yp, "C") == 0) - *py = (tty->sy - 1) / 2 + h / 2; - else if (strcmp(yp, "P") == 0) { - tty_window_offset(&tc->tty, &ox, &oy, &sx, &sy); - if (wp->yoff + wp->sy >= oy) - *py = wp->yoff + wp->sy - oy; + /* Popup width and height. */ + format_add(ft, "popup_width", "%u", w); + format_add(ft, "popup_height", "%u", h); + + /* Position so popup is in the centre. */ + n = (long)(tty->sx - 1) / 2 - w / 2; + if (n < 0) + format_add(ft, "popup_centre_x", "%u", 0); + else + format_add(ft, "popup_centre_x", "%ld", n); + n = (tty->sy - 1) / 2 + h / 2; + if (n + h >= tty->sy) + format_add(ft, "popup_centre_y", "%u", tty->sy - h); + else + format_add(ft, "popup_centre_y", "%ld", n); + + /* Position of popup relative to mouse. */ + if (event->m.valid) { + n = (long)event->m.x - w / 2; + if (n < 0) + format_add(ft, "popup_mouse_centre_x", "%u", 0); else - *py = 0; - } else if (strcmp(yp, "M") == 0) { - if (event->m.valid) - *py = event->m.y + h; + format_add(ft, "popup_mouse_centre_x", "%ld", n); + n = event->m.y - h / 2; + if (n + h >= tty->sy) { + format_add(ft, "popup_mouse_centre_y", "%u", + tty->sy - h); + } else + format_add(ft, "popup_mouse_centre_y", "%ld", n); + n = (long)event->m.y + h; + if (n + h >= tty->sy) + format_add(ft, "popup_mouse_top", "%u", tty->sy - h); else - *py = 0; - } else if (strcmp(yp, "S") == 0) { - if (options_get_number(s->options, "status-position") == 0) { - if (lines != 0) - *py = lines + h; - else - *py = 0; - } else { - if (lines != 0) - *py = tty->sy - lines; - else - *py = tty->sy; - } - } else if (strcmp(yp, "W") == 0) { - if (options_get_number(s->options, "status-position") == 0) { - if (lines != 0) - *py = line + 1 + h; - else - *py = 0; - } else { - if (lines != 0) - *py = tty->sy - lines + line; - else - *py = tty->sy; - } - } else - *py = strtoul(yp, NULL, 10); - if (*py < h) - *py = 0; + format_add(ft, "popup_mouse_top", "%ld", n); + n = event->m.y - h; + if (n < 0) + format_add(ft, "popup_mouse_bottom", "%u", 0); + else + format_add(ft, "popup_mouse_bottom", "%ld", n); + } + + /* Position in pane. */ + tty_window_offset(&tc->tty, &ox, &oy, &sx, &sy); + n = top + wp->yoff - oy + h; + if (n >= tty->sy) + format_add(ft, "popup_pane_top", "%u", tty->sy - h); + else + format_add(ft, "popup_pane_top", "%ld", n); + format_add(ft, "popup_pane_bottom", "%u", top + wp->yoff + wp->sy - oy); + format_add(ft, "popup_pane_left", "%u", wp->xoff - ox); + n = (long)wp->xoff + wp->sx - ox - w; + if (n < 0) + format_add(ft, "popup_pane_right", "%u", 0); else - *py -= h; - if ((*py) + h >= tty->sy) - *py = tty->sy - h; + format_add(ft, "popup_pane_right", "%ld", n); + + /* Expand horizontal position. */ + xp = args_get(args, 'x'); + if (xp == NULL || strcmp(xp, "C") == 0) + xp = "#{popup_centre_x}"; + else if (strcmp(xp, "R") == 0) + xp = "#{popup_right}"; + else if (strcmp(xp, "P") == 0) + xp = "#{popup_pane_left}"; + else if (strcmp(xp, "M") == 0) + xp = "#{popup_mouse_centre_x}"; + else if (strcmp(xp, "W") == 0) + xp = "#{popup_window_status_line_x}"; + p = format_expand(ft, xp); + n = strtol(p, NULL, 10); + if (n + w >= tty->sx) + n = tty->sx - w; + else if (n < 0) + n = 0; + *px = n; + log_debug("%s: -x: %s = %s = %u", __func__, xp, p, *px); + free(p); + + /* Expand vertical position */ + yp = args_get(args, 'y'); + if (yp == NULL || strcmp(yp, "C") == 0) + yp = "#{popup_centre_y}"; + else if (strcmp(yp, "P") == 0) + yp = "#{popup_pane_bottom}"; + else if (strcmp(yp, "M") == 0) + yp = "#{popup_mouse_top}"; + else if (strcmp(yp, "S") == 0) + yp = "#{popup_status_line_y}"; + else if (strcmp(yp, "W") == 0) + yp = "#{popup_window_status_line_y}"; + p = format_expand(ft, yp); + n = strtol(p, NULL, 10); + if (n < h) + n = 0; + else + n -= h; + if (n + h >= tty->sy) + n = tty->sy - h; + else if (n < 0) + n = 0; + *py = n; + log_debug("%s: -y: %s = %s = %u", __func__, yp, p, *py); + free(p); + + return (1); } static enum cmd_retval @@ -226,8 +304,11 @@ cmd_display_menu_exec(struct cmd *self, struct cmdq_item *item) menu_free(menu); return (CMD_RETURN_NORMAL); } - cmd_display_menu_get_position(tc, item, args, &px, &py, menu->width + 4, - menu->count + 2); + if (!cmd_display_menu_get_position(tc, item, args, &px, &py, + menu->width + 4, menu->count + 2)) { + menu_free(menu); + return (CMD_RETURN_NORMAL); + } if (args_has(args, 'O')) flags |= MENU_STAYOPEN; @@ -296,7 +377,8 @@ cmd_display_popup_exec(struct cmd *self, struct cmdq_item *item) w = tty->sx - 1; if (h > tty->sy - 1) h = tty->sy - 1; - cmd_display_menu_get_position(tc, item, args, &px, &py, w, h); + if (!cmd_display_menu_get_position(tc, item, args, &px, &py, w, h)) + return (CMD_RETURN_NORMAL); value = args_get(args, 'd'); if (value != NULL) -- cgit v1.2.3 From fd5c3e6122faea45d3eb44275424d78bc67b0ce2 Mon Sep 17 00:00:00 2001 From: nicm Date: Thu, 26 Nov 2020 13:06:21 +0000 Subject: Fix check for vertical centre. --- cmd-display-menu.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'cmd-display-menu.c') diff --git a/cmd-display-menu.c b/cmd-display-menu.c index 7115dc81..ae000abe 100644 --- a/cmd-display-menu.c +++ b/cmd-display-menu.c @@ -155,7 +155,7 @@ cmd_display_menu_get_position(struct client *tc, struct cmdq_item *item, else format_add(ft, "popup_centre_x", "%ld", n); n = (tty->sy - 1) / 2 + h / 2; - if (n + h >= tty->sy) + if (n >= tty->sy) format_add(ft, "popup_centre_y", "%u", tty->sy - h); else format_add(ft, "popup_centre_y", "%ld", n); -- cgit v1.2.3 From c44750792a9683c5cd6f9df5a69e7417b88772d2 Mon Sep 17 00:00:00 2001 From: nicm Date: Tue, 2 Mar 2021 10:56:45 +0000 Subject: Drop support for popups where the content is provided directly to tmux (which does not have many practical uses) and only support running a program in the popup. display-popup is now simpler and can accept multiple arguments to avoid escaping problems (like the other commands). --- cmd-display-menu.c | 59 +++++++++++++++++++++++++----------------------------- 1 file changed, 27 insertions(+), 32 deletions(-) (limited to 'cmd-display-menu.c') diff --git a/cmd-display-menu.c b/cmd-display-menu.c index ae000abe..27a4c1d7 100644 --- a/cmd-display-menu.c +++ b/cmd-display-menu.c @@ -18,6 +18,7 @@ #include +#include #include #include @@ -50,10 +51,10 @@ const struct cmd_entry cmd_display_popup_entry = { .name = "display-popup", .alias = "popup", - .args = { "CEKc:d:h:R:t:w:x:y:", 0, -1 }, - .usage = "[-CEK] [-c target-client] [-d start-directory] [-h height] " - "[-R shell-command] " CMD_TARGET_PANE_USAGE " [-w width] " - "[-x position] [-y position] [command line ...]", + .args = { "Cc:d:Eh:t:w:x:y:", 0, -1 }, + .usage = "[-CE] [-c target-client] [-d start-directory] [-h height] " + CMD_TARGET_PANE_USAGE " [-w width] " + "[-x position] [-y position] [command]", .target = { 't', CMD_FIND_PANE, 0 }, @@ -325,13 +326,14 @@ cmd_display_popup_exec(struct cmd *self, struct cmdq_item *item) { struct args *args = cmd_get_args(self); struct cmd_find_state *target = cmdq_get_target(item); + struct session *s = target->s; struct client *tc = cmdq_get_target_client(item); struct tty *tty = &tc->tty; - const char *value, *cmd = NULL, **lines = NULL; + const char *value, *shell[] = { NULL, NULL }; const char *shellcmd = NULL; - char *cwd, *cause; - int flags = 0; - u_int px, py, w, h, nlines = 0; + char *cwd, *cause, **argv = args->argv; + int flags = 0, argc = args->argc; + u_int px, py, w, h; if (args_has(args, 'C')) { server_client_clear_overlay(tc); @@ -340,17 +342,7 @@ cmd_display_popup_exec(struct cmd *self, struct cmdq_item *item) if (tc->overlay_draw != NULL) return (CMD_RETURN_NORMAL); - if (args->argc >= 1) - cmd = args->argv[0]; - if (args->argc >= 2) { - lines = (const char **)args->argv + 1; - nlines = args->argc - 1; - } - - if (nlines != 0) - h = popup_height(nlines, lines) + 2; - else - h = tty->sy / 2; + h = tty->sy / 2; if (args_has(args, 'h')) { h = args_percentage(args, 'h', 1, tty->sy, tty->sy, &cause); if (cause != NULL) { @@ -360,10 +352,7 @@ cmd_display_popup_exec(struct cmd *self, struct cmdq_item *item) } } - if (nlines != 0) - w = popup_width(item, nlines, lines, tc, target) + 2; - else - w = tty->sx / 2; + w = tty->sx / 2; if (args_has(args, 'w')) { w = args_percentage(args, 'w', 1, tty->sx, tty->sx, &cause); if (cause != NULL) { @@ -384,20 +373,26 @@ cmd_display_popup_exec(struct cmd *self, struct cmdq_item *item) if (value != NULL) cwd = format_single_from_target(item, value); else - cwd = xstrdup(server_client_get_cwd(tc, target->s)); - - value = args_get(args, 'R'); - if (value != NULL) - shellcmd = format_single_from_target(item, value); + cwd = xstrdup(server_client_get_cwd(tc, s)); + if (argc == 0) + shellcmd = options_get_string(s->options, "default-command"); + else if (argc == 1) + shellcmd = argv[0]; + if (argc <= 1 && (shellcmd == NULL || *shellcmd == '\0')) { + shellcmd = NULL; + shell[0] = options_get_string(s->options, "default-shell"); + if (!checkshell(shell[0])) + shell[0] = _PATH_BSHELL; + argc = 1; + argv = (char**)shell; + } - if (args_has(args, 'K')) - flags |= POPUP_WRITEKEYS; if (args_has(args, 'E') > 1) flags |= POPUP_CLOSEEXITZERO; else if (args_has(args, 'E')) flags |= POPUP_CLOSEEXIT; - if (popup_display(flags, item, px, py, w, h, nlines, lines, shellcmd, - cmd, cwd, tc, target, NULL, NULL) != 0) + if (popup_display(flags, item, px, py, w, h, shellcmd, argc, argv, cwd, + tc, s, NULL, NULL) != 0) return (CMD_RETURN_NORMAL); return (CMD_RETURN_WAIT); } -- cgit v1.2.3