[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.38

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