2019-05-10 18:04:06 +00:00
|
|
|
/* $OpenBSD$ */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2019 Nicholas Marriott <nicholas.marriott@gmail.com>
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
|
|
|
|
* IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
2021-03-02 10:56:45 +00:00
|
|
|
#include <paths.h>
|
2019-05-10 18:04:06 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "tmux.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Display a menu on a client.
|
|
|
|
*/
|
|
|
|
|
2021-08-25 08:51:55 +00:00
|
|
|
static enum args_parse_type cmd_display_menu_args_parse(struct args *,
|
|
|
|
u_int, char **);
|
|
|
|
static enum cmd_retval cmd_display_menu_exec(struct cmd *,
|
|
|
|
struct cmdq_item *);
|
|
|
|
static enum cmd_retval cmd_display_popup_exec(struct cmd *,
|
|
|
|
struct cmdq_item *);
|
2019-05-10 18:04:06 +00:00
|
|
|
|
|
|
|
const struct cmd_entry cmd_display_menu_entry = {
|
|
|
|
.name = "display-menu",
|
|
|
|
.alias = "menu",
|
|
|
|
|
2024-03-21 11:51:32 +00:00
|
|
|
.args = { "b:c:C:H:s:S:MOt:T:x:y:", 1, -1, cmd_display_menu_args_parse },
|
|
|
|
.usage = "[-MO] [-b border-lines] [-c target-client] "
|
2023-08-15 07:01:47 +00:00
|
|
|
"[-C starting-choice] [-H selected-style] [-s style] "
|
|
|
|
"[-S border-style] " CMD_TARGET_PANE_USAGE "[-T title] "
|
|
|
|
"[-x position] [-y position] name key command ...",
|
2019-05-10 18:04:06 +00:00
|
|
|
|
|
|
|
.target = { 't', CMD_FIND_PANE, 0 },
|
|
|
|
|
2020-04-13 20:51:57 +00:00
|
|
|
.flags = CMD_AFTERHOOK|CMD_CLIENT_CFLAG,
|
2019-05-10 18:04:06 +00:00
|
|
|
.exec = cmd_display_menu_exec
|
|
|
|
};
|
|
|
|
|
2020-03-24 08:09:43 +00:00
|
|
|
const struct cmd_entry cmd_display_popup_entry = {
|
|
|
|
.name = "display-popup",
|
|
|
|
.alias = "popup",
|
|
|
|
|
2021-10-25 09:38:36 +00:00
|
|
|
.args = { "Bb:Cc:d:e:Eh:s:S:t:T:w:x:y:", 0, -1, NULL },
|
2021-10-14 13:19:01 +00:00
|
|
|
.usage = "[-BCE] [-b border-lines] [-c target-client] "
|
|
|
|
"[-d start-directory] [-e environment] [-h height] "
|
2021-10-25 09:38:36 +00:00
|
|
|
"[-s style] [-S border-style] " CMD_TARGET_PANE_USAGE
|
|
|
|
"[-T title] [-w width] [-x position] [-y position] "
|
|
|
|
"[shell-command]",
|
2020-03-24 08:09:43 +00:00
|
|
|
|
|
|
|
.target = { 't', CMD_FIND_PANE, 0 },
|
|
|
|
|
2020-04-13 20:51:57 +00:00
|
|
|
.flags = CMD_AFTERHOOK|CMD_CLIENT_CFLAG,
|
2020-03-24 08:09:43 +00:00
|
|
|
.exec = cmd_display_popup_exec
|
|
|
|
};
|
|
|
|
|
2021-08-25 08:51:55 +00:00
|
|
|
static enum args_parse_type
|
|
|
|
cmd_display_menu_args_parse(struct args *args, u_int idx, __unused char **cause)
|
|
|
|
{
|
|
|
|
u_int i = 0;
|
|
|
|
enum args_parse_type type = ARGS_PARSE_STRING;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
type = ARGS_PARSE_STRING;
|
|
|
|
if (i == idx)
|
|
|
|
break;
|
|
|
|
if (*args_string(args, i++) == '\0')
|
|
|
|
continue;
|
|
|
|
|
|
|
|
type = ARGS_PARSE_STRING;
|
|
|
|
if (i++ == idx)
|
|
|
|
break;
|
|
|
|
|
|
|
|
type = ARGS_PARSE_COMMANDS_OR_STRING;
|
|
|
|
if (i++ == idx)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return (type);
|
|
|
|
}
|
|
|
|
|
2020-11-03 08:09:35 +00:00
|
|
|
static int
|
2020-04-13 20:51:57 +00:00
|
|
|
cmd_display_menu_get_position(struct client *tc, struct cmdq_item *item,
|
2020-03-18 09:13:49 +00:00
|
|
|
struct args *args, u_int *px, u_int *py, u_int w, u_int h)
|
|
|
|
{
|
2020-04-13 20:51:57 +00:00
|
|
|
struct tty *tty = &tc->tty;
|
2020-04-13 10:59:58 +00:00
|
|
|
struct cmd_find_state *target = cmdq_get_target(item);
|
2020-04-13 14:46:04 +00:00
|
|
|
struct key_event *event = cmdq_get_event(item);
|
2020-04-13 20:51:57 +00:00
|
|
|
struct session *s = tc->session;
|
2020-04-13 10:59:58 +00:00
|
|
|
struct winlink *wl = target->wl;
|
|
|
|
struct window_pane *wp = target->wp;
|
2020-11-03 08:09:35 +00:00
|
|
|
struct style_ranges *ranges = NULL;
|
|
|
|
struct style_range *sr = NULL;
|
2020-03-18 09:13:49 +00:00
|
|
|
const char *xp, *yp;
|
2020-11-03 08:09:35 +00:00
|
|
|
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);
|
2020-04-02 05:35:15 +00:00
|
|
|
}
|
2020-03-18 09:13:49 +00:00
|
|
|
|
2020-11-03 08:09:35 +00:00
|
|
|
/*
|
|
|
|
* 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;
|
2020-03-18 09:13:49 +00:00
|
|
|
else
|
2020-11-03 08:09:35 +00:00
|
|
|
top = 0;
|
|
|
|
position = options_get_number(s->options, "status-position");
|
|
|
|
|
|
|
|
for (line = 0; line < lines; line++) {
|
|
|
|
ranges = &tc->status.entries[line].ranges;
|
2020-04-02 05:35:15 +00:00
|
|
|
TAILQ_FOREACH(sr, ranges, entry) {
|
2020-03-18 09:13:49 +00:00
|
|
|
if (sr->type != STYLE_RANGE_WINDOW)
|
|
|
|
continue;
|
|
|
|
if (sr->argument == (u_int)wl->idx)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (sr != NULL)
|
2020-11-03 08:09:35 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2020-03-18 09:13:49 +00:00
|
|
|
}
|
|
|
|
} else
|
2020-11-03 08:09:35 +00:00
|
|
|
top = 0;
|
2020-03-18 09:13:49 +00:00
|
|
|
|
2020-11-03 08:09:35 +00:00
|
|
|
/* 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;
|
2020-11-26 13:06:21 +00:00
|
|
|
if (n >= tty->sy)
|
2020-11-03 08:09:35 +00:00
|
|
|
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);
|
2020-03-18 09:13:49 +00:00
|
|
|
else
|
2020-11-03 08:09:35 +00:00
|
|
|
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;
|
2021-07-21 08:09:43 +00:00
|
|
|
if (n >= tty->sy)
|
2021-07-08 11:14:53 +00:00
|
|
|
format_add(ft, "popup_mouse_top", "%u", tty->sy - 1);
|
2020-04-13 13:42:35 +00:00
|
|
|
else
|
2020-11-03 08:09:35 +00:00
|
|
|
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);
|
2020-03-18 09:13:49 +00:00
|
|
|
else
|
2020-11-03 08:09:35 +00:00
|
|
|
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)
|
2021-06-10 07:22:06 +00:00
|
|
|
xp = "#{popup_pane_right}";
|
2020-11-03 08:09:35 +00:00
|
|
|
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;
|
2021-08-23 08:17:41 +00:00
|
|
|
log_debug("%s: -x: %s = %s = %u (-w %u)", __func__, xp, p, *px, w);
|
2020-11-03 08:09:35 +00:00
|
|
|
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;
|
2021-08-23 08:17:41 +00:00
|
|
|
log_debug("%s: -y: %s = %s = %u (-h %u)", __func__, yp, p, *py, h);
|
2020-11-03 08:09:35 +00:00
|
|
|
free(p);
|
|
|
|
|
2023-03-15 19:23:22 +00:00
|
|
|
format_free(ft);
|
2020-11-03 08:09:35 +00:00
|
|
|
return (1);
|
2020-03-18 09:13:49 +00:00
|
|
|
}
|
|
|
|
|
2019-05-10 18:04:06 +00:00
|
|
|
static enum cmd_retval
|
|
|
|
cmd_display_menu_exec(struct cmd *self, struct cmdq_item *item)
|
|
|
|
{
|
2020-04-13 08:26:27 +00:00
|
|
|
struct args *args = cmd_get_args(self);
|
2020-04-13 10:59:58 +00:00
|
|
|
struct cmd_find_state *target = cmdq_get_target(item);
|
2020-04-13 14:46:04 +00:00
|
|
|
struct key_event *event = cmdq_get_event(item);
|
2020-04-13 20:51:57 +00:00
|
|
|
struct client *tc = cmdq_get_target_client(item);
|
2019-05-10 18:04:06 +00:00
|
|
|
struct menu *menu = NULL;
|
2019-05-28 07:18:42 +00:00
|
|
|
struct menu_item menu_item;
|
2023-08-08 08:08:47 +00:00
|
|
|
const char *key, *name, *value;
|
|
|
|
const char *style = args_get(args, 's');
|
|
|
|
const char *border_style = args_get(args, 'S');
|
2023-08-15 07:01:47 +00:00
|
|
|
const char *selected_style = args_get(args, 'H');
|
2023-08-08 08:08:47 +00:00
|
|
|
enum box_lines lines = BOX_LINES_DEFAULT;
|
2023-01-20 21:36:00 +00:00
|
|
|
char *title, *cause;
|
|
|
|
int flags = 0, starting_choice = 0;
|
2021-08-20 19:50:16 +00:00
|
|
|
u_int px, py, i, count = args_count(args);
|
2023-08-08 08:08:47 +00:00
|
|
|
struct options *o = target->s->curw->window->options;
|
|
|
|
struct options_entry *oe;
|
|
|
|
|
2019-05-10 18:04:06 +00:00
|
|
|
|
2020-04-13 20:51:57 +00:00
|
|
|
if (tc->overlay_draw != NULL)
|
2019-05-10 18:04:06 +00:00
|
|
|
return (CMD_RETURN_NORMAL);
|
|
|
|
|
2023-08-08 08:08:47 +00:00
|
|
|
if (args_has(args, 'C')) {
|
|
|
|
if (strcmp(args_get(args, 'C'), "-") == 0)
|
2023-01-20 21:36:00 +00:00
|
|
|
starting_choice = -1;
|
|
|
|
else {
|
2023-08-08 08:08:47 +00:00
|
|
|
starting_choice = args_strtonum(args, 'C', 0, UINT_MAX,
|
2023-01-20 21:36:00 +00:00
|
|
|
&cause);
|
|
|
|
if (cause != NULL) {
|
|
|
|
cmdq_error(item, "starting choice %s", cause);
|
|
|
|
free(cause);
|
|
|
|
return (CMD_RETURN_ERROR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-10 18:04:06 +00:00
|
|
|
if (args_has(args, 'T'))
|
2020-04-13 20:51:57 +00:00
|
|
|
title = format_single_from_target(item, args_get(args, 'T'));
|
2019-05-10 18:04:06 +00:00
|
|
|
else
|
|
|
|
title = xstrdup("");
|
2019-05-28 07:18:42 +00:00
|
|
|
menu = menu_create(title);
|
2023-08-07 10:52:00 +00:00
|
|
|
free(title);
|
2019-05-28 07:18:42 +00:00
|
|
|
|
2021-08-20 19:50:16 +00:00
|
|
|
for (i = 0; i != count; /* nothing */) {
|
|
|
|
name = args_string(args, i++);
|
2019-05-28 07:18:42 +00:00
|
|
|
if (*name == '\0') {
|
2020-04-13 20:51:57 +00:00
|
|
|
menu_add_item(menu, NULL, item, tc, target);
|
2019-05-28 07:18:42 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-08-20 19:50:16 +00:00
|
|
|
if (count - i < 2) {
|
2019-05-28 07:18:42 +00:00
|
|
|
cmdq_error(item, "not enough arguments");
|
|
|
|
menu_free(menu);
|
|
|
|
return (CMD_RETURN_ERROR);
|
|
|
|
}
|
2021-08-20 19:50:16 +00:00
|
|
|
key = args_string(args, i++);
|
2019-05-28 07:18:42 +00:00
|
|
|
|
|
|
|
menu_item.name = name;
|
|
|
|
menu_item.key = key_string_lookup_string(key);
|
2021-08-20 19:50:16 +00:00
|
|
|
menu_item.command = args_string(args, i++);
|
2019-05-28 07:18:42 +00:00
|
|
|
|
2020-04-13 20:51:57 +00:00
|
|
|
menu_add_item(menu, &menu_item, item, tc, target);
|
2019-05-28 07:18:42 +00:00
|
|
|
}
|
2019-05-10 18:04:06 +00:00
|
|
|
if (menu == NULL) {
|
2019-05-28 07:18:42 +00:00
|
|
|
cmdq_error(item, "invalid menu arguments");
|
2019-05-10 18:04:06 +00:00
|
|
|
return (CMD_RETURN_ERROR);
|
|
|
|
}
|
|
|
|
if (menu->count == 0) {
|
|
|
|
menu_free(menu);
|
|
|
|
return (CMD_RETURN_NORMAL);
|
|
|
|
}
|
2020-11-03 08:09:35 +00:00
|
|
|
if (!cmd_display_menu_get_position(tc, item, args, &px, &py,
|
|
|
|
menu->width + 4, menu->count + 2)) {
|
|
|
|
menu_free(menu);
|
|
|
|
return (CMD_RETURN_NORMAL);
|
|
|
|
}
|
2019-05-10 18:04:06 +00:00
|
|
|
|
2023-08-08 08:08:47 +00:00
|
|
|
value = args_get(args, 'b');
|
|
|
|
if (value != NULL) {
|
|
|
|
oe = options_get(o, "menu-border-lines");
|
|
|
|
lines = options_find_choice(options_table_entry(oe), value,
|
|
|
|
&cause);
|
|
|
|
if (lines == -1) {
|
|
|
|
cmdq_error(item, "menu-border-lines %s", cause);
|
|
|
|
free(cause);
|
|
|
|
return (CMD_RETURN_ERROR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-30 08:55:56 +00:00
|
|
|
if (args_has(args, 'O'))
|
|
|
|
flags |= MENU_STAYOPEN;
|
2024-03-21 11:51:32 +00:00
|
|
|
if (!event->m.valid && !args_has(args, 'M'))
|
2019-05-10 18:04:06 +00:00
|
|
|
flags |= MENU_NOMOUSE;
|
2023-08-08 08:08:47 +00:00
|
|
|
if (menu_display(menu, flags, starting_choice, item, px, py, tc, lines,
|
2023-08-15 07:01:47 +00:00
|
|
|
style, selected_style, border_style, target, NULL, NULL) != 0)
|
2019-05-10 18:04:06 +00:00
|
|
|
return (CMD_RETURN_NORMAL);
|
|
|
|
return (CMD_RETURN_WAIT);
|
|
|
|
}
|
2020-03-24 08:09:43 +00:00
|
|
|
|
|
|
|
static enum cmd_retval
|
|
|
|
cmd_display_popup_exec(struct cmd *self, struct cmdq_item *item)
|
|
|
|
{
|
2020-04-13 08:26:27 +00:00
|
|
|
struct args *args = cmd_get_args(self);
|
2020-04-13 10:59:58 +00:00
|
|
|
struct cmd_find_state *target = cmdq_get_target(item);
|
2021-03-02 10:56:45 +00:00
|
|
|
struct session *s = target->s;
|
2020-04-13 20:51:57 +00:00
|
|
|
struct client *tc = cmdq_get_target_client(item);
|
|
|
|
struct tty *tty = &tc->tty;
|
2021-08-20 19:50:16 +00:00
|
|
|
const char *value, *shell, *shellcmd = NULL;
|
2021-10-25 09:38:36 +00:00
|
|
|
const char *style = args_get(args, 's');
|
|
|
|
const char *border_style = args_get(args, 'S');
|
2021-10-20 09:50:40 +00:00
|
|
|
char *cwd, *cause = NULL, **argv = NULL, *title;
|
2021-08-20 19:50:16 +00:00
|
|
|
int flags = 0, argc = 0;
|
2021-10-14 13:19:01 +00:00
|
|
|
enum box_lines lines = BOX_LINES_DEFAULT;
|
2021-08-20 19:50:16 +00:00
|
|
|
u_int px, py, w, h, count = args_count(args);
|
2021-10-11 10:55:30 +00:00
|
|
|
struct args_value *av;
|
|
|
|
struct environ *env = NULL;
|
2021-10-14 13:19:01 +00:00
|
|
|
struct options *o = s->curw->window->options;
|
|
|
|
struct options_entry *oe;
|
2020-03-24 08:09:43 +00:00
|
|
|
|
|
|
|
if (args_has(args, 'C')) {
|
2020-04-13 20:51:57 +00:00
|
|
|
server_client_clear_overlay(tc);
|
2020-03-24 08:09:43 +00:00
|
|
|
return (CMD_RETURN_NORMAL);
|
|
|
|
}
|
2020-04-13 20:51:57 +00:00
|
|
|
if (tc->overlay_draw != NULL)
|
2020-03-24 08:09:43 +00:00
|
|
|
return (CMD_RETURN_NORMAL);
|
|
|
|
|
2021-03-02 10:56:45 +00:00
|
|
|
h = tty->sy / 2;
|
2020-03-24 08:09:43 +00:00
|
|
|
if (args_has(args, 'h')) {
|
2020-04-13 20:51:57 +00:00
|
|
|
h = args_percentage(args, 'h', 1, tty->sy, tty->sy, &cause);
|
2020-03-24 08:09:43 +00:00
|
|
|
if (cause != NULL) {
|
|
|
|
cmdq_error(item, "height %s", cause);
|
|
|
|
free(cause);
|
|
|
|
return (CMD_RETURN_ERROR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-02 10:56:45 +00:00
|
|
|
w = tty->sx / 2;
|
2020-03-24 08:09:43 +00:00
|
|
|
if (args_has(args, 'w')) {
|
2020-04-13 20:51:57 +00:00
|
|
|
w = args_percentage(args, 'w', 1, tty->sx, tty->sx, &cause);
|
2020-03-24 08:09:43 +00:00
|
|
|
if (cause != NULL) {
|
|
|
|
cmdq_error(item, "width %s", cause);
|
|
|
|
free(cause);
|
|
|
|
return (CMD_RETURN_ERROR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-23 08:17:41 +00:00
|
|
|
if (w > tty->sx)
|
|
|
|
w = tty->sx;
|
|
|
|
if (h > tty->sy)
|
|
|
|
h = tty->sy;
|
2020-11-03 08:09:35 +00:00
|
|
|
if (!cmd_display_menu_get_position(tc, item, args, &px, &py, w, h))
|
|
|
|
return (CMD_RETURN_NORMAL);
|
2020-03-24 08:09:43 +00:00
|
|
|
|
2021-10-14 13:19:01 +00:00
|
|
|
value = args_get(args, 'b');
|
|
|
|
if (args_has(args, 'B'))
|
|
|
|
lines = BOX_LINES_NONE;
|
|
|
|
else if (value != NULL) {
|
|
|
|
oe = options_get(o, "popup-border-lines");
|
|
|
|
lines = options_find_choice(options_table_entry(oe), value,
|
|
|
|
&cause);
|
|
|
|
if (cause != NULL) {
|
|
|
|
cmdq_error(item, "popup-border-lines %s", cause);
|
|
|
|
free(cause);
|
|
|
|
return (CMD_RETURN_ERROR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-24 08:09:43 +00:00
|
|
|
value = args_get(args, 'd');
|
|
|
|
if (value != NULL)
|
2020-04-13 20:51:57 +00:00
|
|
|
cwd = format_single_from_target(item, value);
|
2020-03-24 08:09:43 +00:00
|
|
|
else
|
2021-03-02 10:56:45 +00:00
|
|
|
cwd = xstrdup(server_client_get_cwd(tc, s));
|
2021-08-20 19:50:16 +00:00
|
|
|
if (count == 0)
|
2021-03-02 10:56:45 +00:00
|
|
|
shellcmd = options_get_string(s->options, "default-command");
|
2021-08-20 19:50:16 +00:00
|
|
|
else if (count == 1)
|
|
|
|
shellcmd = args_string(args, 0);
|
|
|
|
if (count <= 1 && (shellcmd == NULL || *shellcmd == '\0')) {
|
2021-03-02 10:56:45 +00:00
|
|
|
shellcmd = NULL;
|
2021-08-20 19:50:16 +00:00
|
|
|
shell = options_get_string(s->options, "default-shell");
|
|
|
|
if (!checkshell(shell))
|
|
|
|
shell = _PATH_BSHELL;
|
|
|
|
cmd_append_argv(&argc, &argv, shell);
|
|
|
|
} else
|
2021-08-27 17:25:55 +00:00
|
|
|
args_to_vector(args, &argc, &argv);
|
2020-03-24 08:09:43 +00:00
|
|
|
|
2021-10-11 10:55:30 +00:00
|
|
|
if (args_has(args, 'e') >= 1) {
|
|
|
|
env = environ_create();
|
|
|
|
av = args_first_value(args, 'e');
|
|
|
|
while (av != NULL) {
|
|
|
|
environ_put(env, av->string, 0);
|
|
|
|
av = args_next_value(av);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-20 09:50:40 +00:00
|
|
|
if (args_has(args, 'T'))
|
|
|
|
title = format_single_from_target(item, args_get(args, 'T'));
|
|
|
|
else
|
|
|
|
title = xstrdup("");
|
2020-03-28 09:55:30 +00:00
|
|
|
if (args_has(args, 'E') > 1)
|
|
|
|
flags |= POPUP_CLOSEEXITZERO;
|
|
|
|
else if (args_has(args, 'E'))
|
2020-03-24 08:09:43 +00:00
|
|
|
flags |= POPUP_CLOSEEXIT;
|
2021-10-14 13:19:01 +00:00
|
|
|
if (popup_display(flags, lines, item, px, py, w, h, env, shellcmd, argc,
|
2021-10-25 09:38:36 +00:00
|
|
|
argv, cwd, title, tc, s, style, border_style, NULL, NULL) != 0) {
|
2021-08-20 19:50:16 +00:00
|
|
|
cmd_free_argv(argc, argv);
|
2021-10-11 10:55:30 +00:00
|
|
|
if (env != NULL)
|
|
|
|
environ_free(env);
|
2023-03-15 19:23:22 +00:00
|
|
|
free(cwd);
|
2021-10-20 09:50:40 +00:00
|
|
|
free(title);
|
2020-03-24 08:09:43 +00:00
|
|
|
return (CMD_RETURN_NORMAL);
|
2021-08-20 19:50:16 +00:00
|
|
|
}
|
2021-10-11 10:55:30 +00:00
|
|
|
if (env != NULL)
|
|
|
|
environ_free(env);
|
2023-03-15 19:23:22 +00:00
|
|
|
free(cwd);
|
2021-10-20 09:50:40 +00:00
|
|
|
free(title);
|
2021-08-20 19:50:16 +00:00
|
|
|
cmd_free_argv(argc, argv);
|
2020-03-24 08:09:43 +00:00
|
|
|
return (CMD_RETURN_WAIT);
|
|
|
|
}
|