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

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