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

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