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>
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "tmux.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Display a menu on a client.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static enum cmd_retval cmd_display_menu_exec(struct cmd *,
|
|
|
|
struct cmdq_item *);
|
2020-03-24 08:09:43 +00:00
|
|
|
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",
|
|
|
|
|
2019-05-28 07:18:42 +00:00
|
|
|
.args = { "c:t:T:x:y:", 1, -1 },
|
|
|
|
.usage = "[-c target-client] " 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",
|
|
|
|
|
|
|
|
.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 ...]",
|
|
|
|
|
|
|
|
.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
|
|
|
|
};
|
|
|
|
|
2020-03-18 09:13:49 +00:00
|
|
|
static void
|
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-04-02 05:35:15 +00:00
|
|
|
struct style_ranges *ranges;
|
2020-03-18 09:13:49 +00:00
|
|
|
struct style_range *sr;
|
|
|
|
const char *xp, *yp;
|
2020-04-02 05:35:15 +00:00
|
|
|
u_int line, ox, oy, sx, sy, lines;
|
|
|
|
|
2020-04-13 20:51:57 +00:00
|
|
|
lines = status_line_size(tc);
|
2020-04-02 05:35:15 +00:00
|
|
|
for (line = 0; line < lines; line++) {
|
2020-04-13 20:51:57 +00:00
|
|
|
ranges = &tc->status.entries[line].ranges;
|
2020-04-02 05:35:15 +00:00
|
|
|
TAILQ_FOREACH(sr, ranges, entry) {
|
|
|
|
if (sr->type == STYLE_RANGE_WINDOW)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (sr != NULL)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (line == lines)
|
2020-04-13 20:51:57 +00:00
|
|
|
ranges = &tc->status.entries[0].ranges;
|
2020-03-18 09:13:49 +00:00
|
|
|
|
|
|
|
xp = args_get(args, 'x');
|
2020-03-28 09:55:30 +00:00
|
|
|
if (xp == NULL || strcmp(xp, "C") == 0)
|
2020-04-13 20:51:57 +00:00
|
|
|
*px = (tty->sx - 1) / 2 - w / 2;
|
2020-03-18 09:13:49 +00:00
|
|
|
else if (strcmp(xp, "R") == 0)
|
2020-04-13 20:51:57 +00:00
|
|
|
*px = tty->sx - 1;
|
2020-03-18 09:13:49 +00:00
|
|
|
else if (strcmp(xp, "P") == 0) {
|
2020-04-13 20:51:57 +00:00
|
|
|
tty_window_offset(&tc->tty, &ox, &oy, &sx, &sy);
|
2020-03-18 09:13:49 +00:00
|
|
|
if (wp->xoff >= ox)
|
|
|
|
*px = wp->xoff - ox;
|
|
|
|
else
|
|
|
|
*px = 0;
|
2020-04-13 13:42:35 +00:00
|
|
|
} else if (strcmp(xp, "M") == 0) {
|
2020-04-13 14:46:04 +00:00
|
|
|
if (event->m.valid && event->m.x > w / 2)
|
|
|
|
*px = event->m.x - w / 2;
|
2020-03-18 09:13:49 +00:00
|
|
|
else
|
|
|
|
*px = 0;
|
|
|
|
} else if (strcmp(xp, "W") == 0) {
|
2020-04-13 20:51:57 +00:00
|
|
|
if (status_at_line(tc) == -1)
|
2020-03-18 09:13:49 +00:00
|
|
|
*px = 0;
|
|
|
|
else {
|
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)
|
|
|
|
*px = sr->start;
|
|
|
|
else
|
|
|
|
*px = 0;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
*px = strtoul(xp, NULL, 10);
|
2020-04-13 20:51:57 +00:00
|
|
|
if ((*px) + w >= tty->sx)
|
|
|
|
*px = tty->sx - w;
|
2020-03-18 09:13:49 +00:00
|
|
|
|
|
|
|
yp = args_get(args, 'y');
|
2020-03-28 09:55:30 +00:00
|
|
|
if (yp == NULL || strcmp(yp, "C") == 0)
|
2020-04-13 20:51:57 +00:00
|
|
|
*py = (tty->sy - 1) / 2 + h / 2;
|
2020-03-18 09:13:49 +00:00
|
|
|
else if (strcmp(yp, "P") == 0) {
|
2020-04-13 20:51:57 +00:00
|
|
|
tty_window_offset(&tc->tty, &ox, &oy, &sx, &sy);
|
2020-03-18 09:13:49 +00:00
|
|
|
if (wp->yoff + wp->sy >= oy)
|
|
|
|
*py = wp->yoff + wp->sy - oy;
|
|
|
|
else
|
|
|
|
*py = 0;
|
2020-04-13 13:42:35 +00:00
|
|
|
} else if (strcmp(yp, "M") == 0) {
|
2020-04-13 14:46:04 +00:00
|
|
|
if (event->m.valid)
|
|
|
|
*py = event->m.y + h;
|
2020-04-13 13:42:35 +00:00
|
|
|
else
|
|
|
|
*py = 0;
|
|
|
|
} else if (strcmp(yp, "S") == 0) {
|
2020-04-02 05:35:15 +00:00
|
|
|
if (options_get_number(s->options, "status-position") == 0) {
|
|
|
|
if (lines != 0)
|
|
|
|
*py = lines + h;
|
|
|
|
else
|
|
|
|
*py = 0;
|
|
|
|
} else {
|
|
|
|
if (lines != 0)
|
2020-04-13 20:51:57 +00:00
|
|
|
*py = tty->sy - lines;
|
2020-04-02 05:35:15 +00:00
|
|
|
else
|
2020-04-13 20:51:57 +00:00
|
|
|
*py = tty->sy;
|
2020-04-02 05:35:15 +00:00
|
|
|
}
|
2020-04-13 13:42:35 +00:00
|
|
|
} else if (strcmp(yp, "W") == 0) {
|
2020-04-02 05:35:15 +00:00
|
|
|
if (options_get_number(s->options, "status-position") == 0) {
|
|
|
|
if (lines != 0)
|
|
|
|
*py = line + 1 + h;
|
|
|
|
else
|
|
|
|
*py = 0;
|
|
|
|
} else {
|
|
|
|
if (lines != 0)
|
2020-04-13 20:51:57 +00:00
|
|
|
*py = tty->sy - lines + line;
|
2020-04-02 05:35:15 +00:00
|
|
|
else
|
2020-04-13 20:51:57 +00:00
|
|
|
*py = tty->sy;
|
2020-04-02 05:35:15 +00:00
|
|
|
}
|
2020-03-18 09:13:49 +00:00
|
|
|
} else
|
|
|
|
*py = strtoul(yp, NULL, 10);
|
|
|
|
if (*py < h)
|
|
|
|
*py = 0;
|
|
|
|
else
|
|
|
|
*py -= h;
|
2020-04-13 20:51:57 +00:00
|
|
|
if ((*py) + h >= tty->sy)
|
|
|
|
*py = tty->sy - h;
|
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;
|
2020-03-18 09:13:49 +00:00
|
|
|
const char *key;
|
2019-05-28 07:18:42 +00:00
|
|
|
char *title, *name;
|
2020-03-19 13:32:49 +00:00
|
|
|
int flags = 0, i;
|
2020-03-18 09:13:49 +00:00
|
|
|
u_int px, py;
|
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);
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
for (i = 0; i != args->argc; /* nothing */) {
|
|
|
|
name = args->argv[i++];
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (args->argc - i < 2) {
|
|
|
|
cmdq_error(item, "not enough arguments");
|
|
|
|
free(title);
|
|
|
|
menu_free(menu);
|
|
|
|
return (CMD_RETURN_ERROR);
|
|
|
|
}
|
|
|
|
key = args->argv[i++];
|
|
|
|
|
|
|
|
menu_item.name = name;
|
|
|
|
menu_item.key = key_string_lookup_string(key);
|
|
|
|
menu_item.command = args->argv[i++];
|
|
|
|
|
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
|
|
|
free(title);
|
|
|
|
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-04-13 20:51:57 +00:00
|
|
|
cmd_display_menu_get_position(tc, item, args, &px, &py, menu->width + 4,
|
2020-03-18 09:13:49 +00:00
|
|
|
menu->count + 2);
|
2019-05-10 18:04:06 +00:00
|
|
|
|
2020-04-13 14:46:04 +00:00
|
|
|
if (!event->m.valid)
|
2019-05-10 18:04:06 +00:00
|
|
|
flags |= MENU_NOMOUSE;
|
2020-04-13 20:51:57 +00:00
|
|
|
if (menu_display(menu, flags, item, px, py, tc, 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);
|
2020-04-13 20:51:57 +00:00
|
|
|
struct client *tc = cmdq_get_target_client(item);
|
|
|
|
struct tty *tty = &tc->tty;
|
2020-03-24 08:09:43 +00:00
|
|
|
const char *value, *cmd = NULL, **lines = NULL;
|
|
|
|
const char *shellcmd = NULL;
|
|
|
|
char *cwd, *cause;
|
|
|
|
int flags = 0;
|
|
|
|
u_int px, py, w, h, nlines = 0;
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
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)
|
2020-03-28 09:39:44 +00:00
|
|
|
h = popup_height(nlines, lines) + 2;
|
2020-03-24 08:09:43 +00:00
|
|
|
else
|
2020-04-13 20:51:57 +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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nlines != 0)
|
2020-04-13 20:51:57 +00:00
|
|
|
w = popup_width(item, nlines, lines, tc, target) + 2;
|
2020-03-24 08:09:43 +00:00
|
|
|
else
|
2020-04-13 20:51:57 +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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-13 20:51:57 +00:00
|
|
|
if (w > tty->sx - 1)
|
|
|
|
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);
|
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
|
2020-04-13 20:51:57 +00:00
|
|
|
cwd = xstrdup(server_client_get_cwd(tc, target->s));
|
2020-03-24 08:09:43 +00:00
|
|
|
|
|
|
|
value = args_get(args, 'R');
|
|
|
|
if (value != NULL)
|
2020-04-13 20:51:57 +00:00
|
|
|
shellcmd = format_single_from_target(item, value);
|
2020-03-24 08:09:43 +00:00
|
|
|
|
|
|
|
if (args_has(args, 'K'))
|
|
|
|
flags |= POPUP_WRITEKEYS;
|
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;
|
|
|
|
if (popup_display(flags, item, px, py, w, h, nlines, lines, shellcmd,
|
2020-05-16 15:24:28 +00:00
|
|
|
cmd, cwd, tc, target, NULL, NULL) != 0)
|
2020-03-24 08:09:43 +00:00
|
|
|
return (CMD_RETURN_NORMAL);
|
|
|
|
return (CMD_RETURN_WAIT);
|
|
|
|
}
|