diff options
Diffstat (limited to 'cmd-display-menu.c')
-rw-r--r-- | cmd-display-menu.c | 305 |
1 files changed, 223 insertions, 82 deletions
diff --git a/cmd-display-menu.c b/cmd-display-menu.c index ac7a4cfe..0a5c7f78 100644 --- a/cmd-display-menu.c +++ b/cmd-display-menu.c @@ -29,6 +29,8 @@ 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 *); const struct cmd_entry cmd_display_menu_entry = { .name = "display-menu", @@ -40,44 +42,164 @@ const struct cmd_entry cmd_display_menu_entry = { .target = { 't', CMD_FIND_PANE, 0 }, - .flags = CMD_AFTERHOOK, + .flags = CMD_AFTERHOOK|CMD_CLIENT_CFLAG, .exec = cmd_display_menu_exec }; +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 }, + + .flags = CMD_AFTERHOOK|CMD_CLIENT_CFLAG, + .exec = cmd_display_popup_exec +}; + +static void +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) +{ + struct tty *tty = &tc->tty; + struct cmd_find_state *target = cmdq_get_target(item); + struct key_event *event = cmdq_get_event(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; + 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; + } + 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; + else + *px = 0; + } else if (strcmp(xp, "W") == 0) { + if (status_at_line(tc) == -1) + *px = 0; + else { + TAILQ_FOREACH(sr, ranges, entry) { + 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); + if ((*px) + w >= tty->sx) + *px = tty->sx - w; + + 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; + else + *py = 0; + } else if (strcmp(yp, "M") == 0) { + if (event->m.valid) + *py = event->m.y + 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; + else + *py -= h; + if ((*py) + h >= tty->sy) + *py = tty->sy - h; +} + static enum cmd_retval cmd_display_menu_exec(struct cmd *self, struct cmdq_item *item) { - struct args *args = self->args; - struct client *c; - struct session *s = item->target.s; - struct winlink *wl = item->target.wl; - struct window_pane *wp = item->target.wp; - struct cmd_find_state *fs = &item->target; + struct args *args = cmd_get_args(self); + struct cmd_find_state *target = cmdq_get_target(item); + struct key_event *event = cmdq_get_event(item); + struct client *tc = cmdq_get_target_client(item); struct menu *menu = NULL; - struct style_range *sr; struct menu_item menu_item; - const char *xp, *yp, *key; + const char *key; char *title, *name; - int at, flags, i; - u_int px, py, ox, oy, sx, sy; + int flags = 0, i; + u_int px, py; - if ((c = cmd_find_client(item, args_get(args, 'c'), 0)) == NULL) - return (CMD_RETURN_ERROR); - if (c->overlay_draw != NULL) + if (tc->overlay_draw != NULL) return (CMD_RETURN_NORMAL); - at = status_at_line(c); if (args_has(args, 'T')) - title = format_single(NULL, args_get(args, 'T'), c, s, wl, wp); + title = format_single_from_target(item, args_get(args, 'T')); else title = xstrdup(""); - menu = menu_create(title); for (i = 0; i != args->argc; /* nothing */) { name = args->argv[i++]; if (*name == '\0') { - menu_add_item(menu, NULL, item, c, fs); + menu_add_item(menu, NULL, item, tc, target); continue; } @@ -93,7 +215,7 @@ cmd_display_menu_exec(struct cmd *self, struct cmdq_item *item) menu_item.key = key_string_lookup_string(key); menu_item.command = args->argv[i++]; - menu_add_item(menu, &menu_item, item, c, fs); + menu_add_item(menu, &menu_item, item, tc, target); } free(title); if (menu == NULL) { @@ -104,75 +226,94 @@ 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); - xp = args_get(args, 'x'); - if (xp == NULL) - px = 0; - else if (strcmp(xp, "R") == 0) - px = c->tty.sx - 1; - else if (strcmp(xp, "P") == 0) { - tty_window_offset(&c->tty, &ox, &oy, &sx, &sy); - if (wp->xoff >= ox) - px = wp->xoff - ox; - else - px = 0; - } else if (strcmp(xp, "M") == 0 && item->shared->mouse.valid) { - if (item->shared->mouse.x > (menu->width + 4) / 2) - px = item->shared->mouse.x - (menu->width + 4) / 2; - else - px = 0; + if (!event->m.valid) + flags |= MENU_NOMOUSE; + if (menu_display(menu, flags, item, px, py, tc, target, NULL, + NULL) != 0) + return (CMD_RETURN_NORMAL); + return (CMD_RETURN_WAIT); +} + +static enum cmd_retval +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 client *tc = cmdq_get_target_client(item); + struct tty *tty = &tc->tty; + 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')) { + server_client_clear_overlay(tc); + return (CMD_RETURN_NORMAL); } - else if (strcmp(xp, "W") == 0) { - if (at == -1) - px = 0; - else { - TAILQ_FOREACH(sr, &c->status.entries[0].ranges, entry) { - if (sr->type != STYLE_RANGE_WINDOW) - continue; - if (sr->argument == (u_int)wl->idx) - break; - } - if (sr != NULL) - px = sr->start; - else - px = 0; + 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; + if (args_has(args, 'h')) { + h = args_percentage(args, 'h', 1, tty->sy, tty->sy, &cause); + if (cause != NULL) { + cmdq_error(item, "height %s", cause); + free(cause); + return (CMD_RETURN_ERROR); } - } else - px = strtoul(xp, NULL, 10); - if (px + menu->width + 4 >= c->tty.sx) - px = c->tty.sx - menu->width - 4; + } - yp = args_get(args, 'y'); - if (yp == NULL) - py = 0; - else if (strcmp(yp, "P") == 0) { - tty_window_offset(&c->tty, &ox, &oy, &sx, &sy); - if (wp->yoff + wp->sy >= oy) - py = wp->yoff + wp->sy - oy; - else - py = 0; - } else if (strcmp(yp, "M") == 0 && item->shared->mouse.valid) - py = item->shared->mouse.y + menu->count + 2; - else if (strcmp(yp, "S") == 0) { - if (at == -1) - py = c->tty.sy; - else if (at == 0) - py = status_line_size(c) + menu->count + 2; - else - py = at; - } else - py = strtoul(yp, NULL, 10); - if (py < menu->count + 2) - py = 0; + if (nlines != 0) + w = popup_width(item, nlines, lines, tc, target) + 2; else - py -= menu->count + 2; - if (py + menu->count + 2 >= c->tty.sy) - py = c->tty.sy - menu->count - 2; + w = tty->sx / 2; + if (args_has(args, 'w')) { + w = args_percentage(args, 'w', 1, tty->sx, tty->sx, &cause); + if (cause != NULL) { + cmdq_error(item, "width %s", cause); + free(cause); + return (CMD_RETURN_ERROR); + } + } - flags = 0; - if (!item->shared->mouse.valid) - flags |= MENU_NOMOUSE; - if (menu_display(menu, flags, item, px, py, c, fs, NULL, NULL) != 0) + 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); + + value = args_get(args, 'd'); + 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); + + 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) != 0) return (CMD_RETURN_NORMAL); return (CMD_RETURN_WAIT); } |