[BACK]Return to cmd-display-menu.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / tmux

Annotation of src/usr.bin/tmux/cmd-display-menu.c, Revision 1.33

1.33    ! nicm        1: /* $OpenBSD: cmd-display-menu.c,v 1.32 2021/08/25 08:51:55 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:
1.23      nicm       21: #include <paths.h>
1.1       nicm       22: #include <stdlib.h>
                     23: #include <string.h>
                     24:
                     25: #include "tmux.h"
                     26:
                     27: /*
                     28:  * Display a menu on a client.
                     29:  */
                     30:
1.32      nicm       31: static enum args_parse_type    cmd_display_menu_args_parse(struct args *,
                     32:                                    u_int, char **);
                     33: static enum cmd_retval         cmd_display_menu_exec(struct cmd *,
                     34:                                    struct cmdq_item *);
                     35: static enum cmd_retval         cmd_display_popup_exec(struct cmd *,
                     36:                                    struct cmdq_item *);
1.1       nicm       37:
                     38: const struct cmd_entry cmd_display_menu_entry = {
                     39:        .name = "display-menu",
                     40:        .alias = "menu",
                     41:
1.32      nicm       42:        .args = { "c:t:OT:x:y:", 1, -1, cmd_display_menu_args_parse },
1.20      nicm       43:        .usage = "[-O] [-c target-client] " CMD_TARGET_PANE_USAGE " [-T title] "
1.4       nicm       44:                 "[-x position] [-y position] name key command ...",
1.1       nicm       45:
                     46:        .target = { 't', CMD_FIND_PANE, 0 },
                     47:
1.18      nicm       48:        .flags = CMD_AFTERHOOK|CMD_CLIENT_CFLAG,
1.1       nicm       49:        .exec = cmd_display_menu_exec
                     50: };
                     51:
1.7       nicm       52: const struct cmd_entry cmd_display_popup_entry = {
                     53:        .name = "display-popup",
                     54:        .alias = "popup",
                     55:
1.29      nicm       56:        .args = { "BCc:d:Eh:t:w:x:y:", 0, -1, NULL },
1.27      nicm       57:        .usage = "[-BCE] [-c target-client] [-d start-directory] [-h height] "
1.32      nicm       58:                 CMD_TARGET_PANE_USAGE " [-w width] "
                     59:                 "[-x position] [-y position] [shell-command]",
1.7       nicm       60:
                     61:        .target = { 't', CMD_FIND_PANE, 0 },
                     62:
1.18      nicm       63:        .flags = CMD_AFTERHOOK|CMD_CLIENT_CFLAG,
1.7       nicm       64:        .exec = cmd_display_popup_exec
                     65: };
1.32      nicm       66:
                     67: static enum args_parse_type
                     68: cmd_display_menu_args_parse(struct args *args, u_int idx, __unused char **cause)
                     69: {
                     70:        u_int                    i = 0;
                     71:        enum args_parse_type     type = ARGS_PARSE_STRING;
                     72:
                     73:        for (;;) {
                     74:                type = ARGS_PARSE_STRING;
                     75:                if (i == idx)
                     76:                        break;
                     77:                if (*args_string(args, i++) == '\0')
                     78:                        continue;
                     79:
                     80:                type = ARGS_PARSE_STRING;
                     81:                if (i++ == idx)
                     82:                        break;
                     83:
                     84:                type = ARGS_PARSE_COMMANDS_OR_STRING;
                     85:                if (i++ == idx)
                     86:                        break;
                     87:        }
                     88:        return (type);
                     89: }
1.7       nicm       90:
1.21      nicm       91: static int
1.18      nicm       92: cmd_display_menu_get_position(struct client *tc, struct cmdq_item *item,
1.5       nicm       93:     struct args *args, u_int *px, u_int *py, u_int w, u_int h)
                     94: {
1.18      nicm       95:        struct tty              *tty = &tc->tty;
1.14      nicm       96:        struct cmd_find_state   *target = cmdq_get_target(item);
1.17      nicm       97:        struct key_event        *event = cmdq_get_event(item);
1.18      nicm       98:        struct session          *s = tc->session;
1.14      nicm       99:        struct winlink          *wl = target->wl;
                    100:        struct window_pane      *wp = target->wp;
1.21      nicm      101:        struct style_ranges     *ranges = NULL;
                    102:        struct style_range      *sr = NULL;
1.5       nicm      103:        const char              *xp, *yp;
1.21      nicm      104:        char                    *p;
                    105:        int                      top;
                    106:        u_int                    line, ox, oy, sx, sy, lines, position;
                    107:        long                     n;
                    108:        struct format_tree      *ft;
                    109:
                    110:        /*
                    111:         * Work out the position from the -x and -y arguments. This is the
                    112:         * bottom-left position.
                    113:         */
                    114:
                    115:        /* If the popup is too big, stop now. */
                    116:        if (w > tty->sx || h > tty->sy)
                    117:                return (0);
                    118:
                    119:        /* Create format with mouse position if any. */
                    120:        ft = format_create_from_target(item);
                    121:        if (event->m.valid) {
                    122:                format_add(ft, "popup_mouse_x", "%u", event->m.x);
                    123:                format_add(ft, "popup_mouse_y", "%u", event->m.y);
1.11      nicm      124:        }
1.5       nicm      125:
1.21      nicm      126:        /*
                    127:         * If there are any status lines, add this window position and the
                    128:         * status line position.
                    129:         */
                    130:        top = status_at_line(tc);
                    131:        if (top != -1) {
                    132:                lines = status_line_size(tc);
                    133:                if (top == 0)
                    134:                        top = lines;
1.5       nicm      135:                else
1.21      nicm      136:                        top = 0;
                    137:                position = options_get_number(s->options, "status-position");
                    138:
                    139:                for (line = 0; line < lines; line++) {
                    140:                        ranges = &tc->status.entries[line].ranges;
1.11      nicm      141:                        TAILQ_FOREACH(sr, ranges, entry) {
1.5       nicm      142:                                if (sr->type != STYLE_RANGE_WINDOW)
                    143:                                        continue;
                    144:                                if (sr->argument == (u_int)wl->idx)
                    145:                                        break;
                    146:                        }
                    147:                        if (sr != NULL)
1.21      nicm      148:                                break;
                    149:                }
                    150:
                    151:                if (sr != NULL) {
                    152:                        format_add(ft, "popup_window_status_line_x", "%u",
                    153:                            sr->start);
                    154:                        if (position == 0) {
                    155:                                format_add(ft, "popup_window_status_line_y",
                    156:                                    "%u", line + 1 + h);
                    157:                        } else {
                    158:                                format_add(ft, "popup_window_status_line_y",
                    159:                                    "%u", tty->sy - lines + line);
                    160:                        }
                    161:                }
                    162:
                    163:                if (position == 0)
                    164:                        format_add(ft, "popup_status_line_y", "%u", lines + h);
                    165:                else {
                    166:                        format_add(ft, "popup_status_line_y", "%u",
                    167:                            tty->sy - lines);
1.5       nicm      168:                }
                    169:        } else
1.21      nicm      170:                top = 0;
1.5       nicm      171:
1.21      nicm      172:        /* Popup width and height. */
                    173:        format_add(ft, "popup_width", "%u", w);
                    174:        format_add(ft, "popup_height", "%u", h);
                    175:
                    176:        /* Position so popup is in the centre. */
                    177:        n = (long)(tty->sx - 1) / 2 - w / 2;
                    178:        if (n < 0)
                    179:                format_add(ft, "popup_centre_x", "%u", 0);
                    180:        else
                    181:                format_add(ft, "popup_centre_x", "%ld", n);
                    182:        n = (tty->sy - 1) / 2 + h / 2;
1.22      nicm      183:        if (n >= tty->sy)
1.21      nicm      184:                format_add(ft, "popup_centre_y", "%u", tty->sy - h);
                    185:        else
                    186:                format_add(ft, "popup_centre_y", "%ld", n);
                    187:
                    188:        /* Position of popup relative to mouse. */
                    189:        if (event->m.valid) {
                    190:                n = (long)event->m.x - w / 2;
                    191:                if (n < 0)
                    192:                        format_add(ft, "popup_mouse_centre_x", "%u", 0);
                    193:                else
                    194:                        format_add(ft, "popup_mouse_centre_x", "%ld", n);
                    195:                n = event->m.y - h / 2;
                    196:                if (n + h >= tty->sy) {
                    197:                        format_add(ft, "popup_mouse_centre_y", "%u",
                    198:                            tty->sy - h);
                    199:                } else
                    200:                        format_add(ft, "popup_mouse_centre_y", "%ld", n);
                    201:                n = (long)event->m.y + h;
1.26      nicm      202:                if (n >= tty->sy)
1.25      nicm      203:                        format_add(ft, "popup_mouse_top", "%u", tty->sy - 1);
1.21      nicm      204:                else
                    205:                        format_add(ft, "popup_mouse_top", "%ld", n);
                    206:                n = event->m.y - h;
                    207:                if (n < 0)
                    208:                        format_add(ft, "popup_mouse_bottom", "%u", 0);
                    209:                else
                    210:                        format_add(ft, "popup_mouse_bottom", "%ld", n);
                    211:        }
                    212:
                    213:        /* Position in pane. */
                    214:        tty_window_offset(&tc->tty, &ox, &oy, &sx, &sy);
                    215:        n = top + wp->yoff - oy + h;
                    216:        if (n >= tty->sy)
                    217:                format_add(ft, "popup_pane_top", "%u", tty->sy - h);
                    218:        else
                    219:                format_add(ft, "popup_pane_top", "%ld", n);
                    220:        format_add(ft, "popup_pane_bottom", "%u", top + wp->yoff + wp->sy - oy);
                    221:        format_add(ft, "popup_pane_left", "%u", wp->xoff - ox);
                    222:        n = (long)wp->xoff + wp->sx - ox - w;
                    223:        if (n < 0)
                    224:                format_add(ft, "popup_pane_right", "%u", 0);
                    225:        else
                    226:                format_add(ft, "popup_pane_right", "%ld", n);
                    227:
                    228:        /* Expand horizontal position. */
                    229:        xp = args_get(args, 'x');
                    230:        if (xp == NULL || strcmp(xp, "C") == 0)
                    231:                xp = "#{popup_centre_x}";
                    232:        else if (strcmp(xp, "R") == 0)
1.24      nicm      233:                xp = "#{popup_pane_right}";
1.21      nicm      234:        else if (strcmp(xp, "P") == 0)
                    235:                xp = "#{popup_pane_left}";
                    236:        else if (strcmp(xp, "M") == 0)
                    237:                xp = "#{popup_mouse_centre_x}";
                    238:        else if (strcmp(xp, "W") == 0)
                    239:                xp = "#{popup_window_status_line_x}";
                    240:        p = format_expand(ft, xp);
                    241:        n = strtol(p, NULL, 10);
                    242:        if (n + w >= tty->sx)
                    243:                n = tty->sx - w;
                    244:        else if (n < 0)
                    245:                n = 0;
                    246:        *px = n;
1.31      nicm      247:        log_debug("%s: -x: %s = %s = %u (-w %u)", __func__, xp, p, *px, w);
1.21      nicm      248:        free(p);
                    249:
                    250:        /* Expand vertical position  */
1.5       nicm      251:        yp = args_get(args, 'y');
1.10      nicm      252:        if (yp == NULL || strcmp(yp, "C") == 0)
1.21      nicm      253:                yp = "#{popup_centre_y}";
                    254:        else if (strcmp(yp, "P") == 0)
                    255:                yp = "#{popup_pane_bottom}";
                    256:        else if (strcmp(yp, "M") == 0)
                    257:                yp = "#{popup_mouse_top}";
                    258:        else if (strcmp(yp, "S") == 0)
                    259:                yp = "#{popup_status_line_y}";
                    260:        else if (strcmp(yp, "W") == 0)
                    261:                yp = "#{popup_window_status_line_y}";
                    262:        p = format_expand(ft, yp);
                    263:        n = strtol(p, NULL, 10);
                    264:        if (n < h)
                    265:                n = 0;
1.5       nicm      266:        else
1.21      nicm      267:                n -= h;
                    268:        if (n + h >= tty->sy)
                    269:                n = tty->sy - h;
                    270:        else if (n < 0)
                    271:                n = 0;
                    272:        *py = n;
1.31      nicm      273:        log_debug("%s: -y: %s = %s = %u (-h %u)", __func__, yp, p, *py, h);
1.21      nicm      274:        free(p);
                    275:
                    276:        return (1);
1.5       nicm      277: }
                    278:
1.1       nicm      279: static enum cmd_retval
                    280: cmd_display_menu_exec(struct cmd *self, struct cmdq_item *item)
                    281: {
1.13      nicm      282:        struct args             *args = cmd_get_args(self);
1.14      nicm      283:        struct cmd_find_state   *target = cmdq_get_target(item);
1.17      nicm      284:        struct key_event        *event = cmdq_get_event(item);
1.18      nicm      285:        struct client           *tc = cmdq_get_target_client(item);
1.1       nicm      286:        struct menu             *menu = NULL;
1.4       nicm      287:        struct menu_item         menu_item;
1.28      nicm      288:        const char              *key, *name;
                    289:        char                    *title;
                    290:        int                      flags = 0;
                    291:        u_int                    px, py, i, count = args_count(args);
1.1       nicm      292:
1.18      nicm      293:        if (tc->overlay_draw != NULL)
1.1       nicm      294:                return (CMD_RETURN_NORMAL);
                    295:
                    296:        if (args_has(args, 'T'))
1.18      nicm      297:                title = format_single_from_target(item, args_get(args, 'T'));
1.1       nicm      298:        else
                    299:                title = xstrdup("");
1.4       nicm      300:        menu = menu_create(title);
                    301:
1.28      nicm      302:        for (i = 0; i != count; /* nothing */) {
                    303:                name = args_string(args, i++);
1.4       nicm      304:                if (*name == '\0') {
1.18      nicm      305:                        menu_add_item(menu, NULL, item, tc, target);
1.4       nicm      306:                        continue;
                    307:                }
                    308:
1.28      nicm      309:                if (count - i < 2) {
1.4       nicm      310:                        cmdq_error(item, "not enough arguments");
                    311:                        free(title);
                    312:                        menu_free(menu);
                    313:                        return (CMD_RETURN_ERROR);
                    314:                }
1.28      nicm      315:                key = args_string(args, i++);
1.4       nicm      316:
                    317:                menu_item.name = name;
                    318:                menu_item.key = key_string_lookup_string(key);
1.28      nicm      319:                menu_item.command = args_string(args, i++);
1.4       nicm      320:
1.18      nicm      321:                menu_add_item(menu, &menu_item, item, tc, target);
1.4       nicm      322:        }
1.1       nicm      323:        free(title);
                    324:        if (menu == NULL) {
1.4       nicm      325:                cmdq_error(item, "invalid menu arguments");
1.1       nicm      326:                return (CMD_RETURN_ERROR);
                    327:        }
                    328:        if (menu->count == 0) {
                    329:                menu_free(menu);
                    330:                return (CMD_RETURN_NORMAL);
                    331:        }
1.21      nicm      332:        if (!cmd_display_menu_get_position(tc, item, args, &px, &py,
                    333:            menu->width + 4, menu->count + 2)) {
                    334:                menu_free(menu);
                    335:                return (CMD_RETURN_NORMAL);
                    336:        }
1.1       nicm      337:
1.20      nicm      338:        if (args_has(args, 'O'))
                    339:                flags |= MENU_STAYOPEN;
1.17      nicm      340:        if (!event->m.valid)
1.1       nicm      341:                flags |= MENU_NOMOUSE;
1.18      nicm      342:        if (menu_display(menu, flags, item, px, py, tc, target, NULL,
                    343:            NULL) != 0)
1.7       nicm      344:                return (CMD_RETURN_NORMAL);
                    345:        return (CMD_RETURN_WAIT);
                    346: }
                    347:
                    348: static enum cmd_retval
                    349: cmd_display_popup_exec(struct cmd *self, struct cmdq_item *item)
                    350: {
1.13      nicm      351:        struct args             *args = cmd_get_args(self);
1.14      nicm      352:        struct cmd_find_state   *target = cmdq_get_target(item);
1.23      nicm      353:        struct session          *s = target->s;
1.18      nicm      354:        struct client           *tc = cmdq_get_target_client(item);
                    355:        struct tty              *tty = &tc->tty;
1.28      nicm      356:        const char              *value, *shell, *shellcmd = NULL;
                    357:        char                    *cwd, *cause, **argv = NULL;
                    358:        int                      flags = 0, argc = 0;
                    359:        u_int                    px, py, w, h, count = args_count(args);
1.7       nicm      360:
                    361:        if (args_has(args, 'C')) {
1.18      nicm      362:                server_client_clear_overlay(tc);
1.7       nicm      363:                return (CMD_RETURN_NORMAL);
                    364:        }
1.18      nicm      365:        if (tc->overlay_draw != NULL)
1.7       nicm      366:                return (CMD_RETURN_NORMAL);
                    367:
1.23      nicm      368:        h = tty->sy / 2;
1.7       nicm      369:        if (args_has(args, 'h')) {
1.18      nicm      370:                h = args_percentage(args, 'h', 1, tty->sy, tty->sy, &cause);
1.7       nicm      371:                if (cause != NULL) {
                    372:                        cmdq_error(item, "height %s", cause);
                    373:                        free(cause);
                    374:                        return (CMD_RETURN_ERROR);
                    375:                }
                    376:        }
                    377:
1.23      nicm      378:        w = tty->sx / 2;
1.7       nicm      379:        if (args_has(args, 'w')) {
1.18      nicm      380:                w = args_percentage(args, 'w', 1, tty->sx, tty->sx, &cause);
1.7       nicm      381:                if (cause != NULL) {
                    382:                        cmdq_error(item, "width %s", cause);
                    383:                        free(cause);
                    384:                        return (CMD_RETURN_ERROR);
                    385:                }
                    386:        }
                    387:
1.31      nicm      388:        if (w > tty->sx)
                    389:                w = tty->sx;
                    390:        if (h > tty->sy)
                    391:                h = tty->sy;
1.21      nicm      392:        if (!cmd_display_menu_get_position(tc, item, args, &px, &py, w, h))
                    393:                return (CMD_RETURN_NORMAL);
1.7       nicm      394:
                    395:        value = args_get(args, 'd');
                    396:        if (value != NULL)
1.18      nicm      397:                cwd = format_single_from_target(item, value);
1.7       nicm      398:        else
1.23      nicm      399:                cwd = xstrdup(server_client_get_cwd(tc, s));
1.28      nicm      400:        if (count == 0)
1.23      nicm      401:                shellcmd = options_get_string(s->options, "default-command");
1.28      nicm      402:        else if (count == 1)
                    403:                shellcmd = args_string(args, 0);
                    404:        if (count <= 1 && (shellcmd == NULL || *shellcmd == '\0')) {
1.23      nicm      405:                shellcmd = NULL;
1.28      nicm      406:                shell = options_get_string(s->options, "default-shell");
                    407:                if (!checkshell(shell))
                    408:                        shell = _PATH_BSHELL;
                    409:                cmd_append_argv(&argc, &argv, shell);
                    410:        } else
1.33    ! nicm      411:                args_to_vector(args, &argc, &argv);
1.7       nicm      412:
1.9       nicm      413:        if (args_has(args, 'E') > 1)
                    414:                flags |= POPUP_CLOSEEXITZERO;
                    415:        else if (args_has(args, 'E'))
1.7       nicm      416:                flags |= POPUP_CLOSEEXIT;
1.27      nicm      417:        if (args_has(args, 'B'))
                    418:                flags |= POPUP_NOBORDER;
1.23      nicm      419:        if (popup_display(flags, item, px, py, w, h, shellcmd, argc, argv, cwd,
1.28      nicm      420:            tc, s, NULL, NULL) != 0) {
                    421:                cmd_free_argv(argc, argv);
1.1       nicm      422:                return (CMD_RETURN_NORMAL);
1.28      nicm      423:        }
                    424:        cmd_free_argv(argc, argv);
1.1       nicm      425:        return (CMD_RETURN_WAIT);
                    426: }