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

1.9     ! nicm        1: /* $OpenBSD: cmd-display-menu.c,v 1.8 2020/03/28 09:39:44 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.4       nicm       39:        .args = { "c:t:T:x:y:", 1, -1 },
                     40:        .usage = "[-c target-client] " CMD_TARGET_PANE_USAGE " [-T title] "
                     41:                 "[-x position] [-y position] name key command ...",
1.1       nicm       42:
                     43:        .target = { 't', CMD_FIND_PANE, 0 },
                     44:
                     45:        .flags = CMD_AFTERHOOK,
                     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:
                     60:        .flags = CMD_AFTERHOOK,
                     61:        .exec = cmd_display_popup_exec
                     62: };
                     63:
1.5       nicm       64: static void
                     65: cmd_display_menu_get_position(struct client *c, struct cmdq_item *item,
                     66:     struct args *args, u_int *px, u_int *py, u_int w, u_int h)
                     67: {
                     68:        struct winlink          *wl = item->target.wl;
                     69:        struct window_pane      *wp = item->target.wp;
                     70:        struct style_range      *sr;
                     71:        const char              *xp, *yp;
                     72:        int                      at = status_at_line(c);
                     73:        u_int                    ox, oy, sx, sy;
                     74:
                     75:        xp = args_get(args, 'x');
                     76:        if (xp == NULL)
                     77:                *px = 0;
                     78:        else if (strcmp(xp, "R") == 0)
                     79:                *px = c->tty.sx - 1;
1.6       nicm       80:        else if (strcmp(xp, "C") == 0)
                     81:                *px = (c->tty.sx - 1) / 2 - w / 2;
1.5       nicm       82:        else if (strcmp(xp, "P") == 0) {
                     83:                tty_window_offset(&c->tty, &ox, &oy, &sx, &sy);
                     84:                if (wp->xoff >= ox)
                     85:                        *px = wp->xoff - ox;
                     86:                else
                     87:                        *px = 0;
                     88:        } else if (strcmp(xp, "M") == 0 && item->shared->mouse.valid) {
                     89:                if (item->shared->mouse.x > w / 2)
                     90:                        *px = item->shared->mouse.x - w / 2;
                     91:                else
                     92:                        *px = 0;
                     93:        } else if (strcmp(xp, "W") == 0) {
                     94:                if (at == -1)
                     95:                        *px = 0;
                     96:                else {
                     97:                        TAILQ_FOREACH(sr, &c->status.entries[0].ranges, entry) {
                     98:                                if (sr->type != STYLE_RANGE_WINDOW)
                     99:                                        continue;
                    100:                                if (sr->argument == (u_int)wl->idx)
                    101:                                        break;
                    102:                        }
                    103:                        if (sr != NULL)
                    104:                                *px = sr->start;
                    105:                        else
                    106:                                *px = 0;
                    107:                }
                    108:        } else
                    109:                *px = strtoul(xp, NULL, 10);
                    110:        if ((*px) + w >= c->tty.sx)
                    111:                *px = c->tty.sx - w;
                    112:
                    113:        yp = args_get(args, 'y');
                    114:        if (yp == NULL)
                    115:                *py = 0;
1.6       nicm      116:        else if (strcmp(yp, "C") == 0)
                    117:                *py = (c->tty.sy - 1) / 2 + h / 2;
1.5       nicm      118:        else if (strcmp(yp, "P") == 0) {
                    119:                tty_window_offset(&c->tty, &ox, &oy, &sx, &sy);
                    120:                if (wp->yoff + wp->sy >= oy)
                    121:                        *py = wp->yoff + wp->sy - oy;
                    122:                else
                    123:                        *py = 0;
                    124:        } else if (strcmp(yp, "M") == 0 && item->shared->mouse.valid)
                    125:                *py = item->shared->mouse.y + h;
                    126:        else if (strcmp(yp, "S") == 0) {
                    127:                if (at == -1)
                    128:                        *py = c->tty.sy;
                    129:                else if (at == 0)
                    130:                        *py = status_line_size(c) + h;
                    131:                else
                    132:                        *py = at;
                    133:        } else
                    134:                *py = strtoul(yp, NULL, 10);
                    135:        if (*py < h)
                    136:                *py = 0;
                    137:        else
                    138:                *py -= h;
                    139:        if ((*py) + h >= c->tty.sy)
                    140:                *py = c->tty.sy - h;
                    141: }
                    142:
1.1       nicm      143: static enum cmd_retval
                    144: cmd_display_menu_exec(struct cmd *self, struct cmdq_item *item)
                    145: {
                    146:        struct args             *args = self->args;
                    147:        struct client           *c;
                    148:        struct session          *s = item->target.s;
                    149:        struct winlink          *wl = item->target.wl;
                    150:        struct window_pane      *wp = item->target.wp;
                    151:        struct cmd_find_state   *fs = &item->target;
                    152:        struct menu             *menu = NULL;
1.4       nicm      153:        struct menu_item         menu_item;
1.5       nicm      154:        const char              *key;
1.4       nicm      155:        char                    *title, *name;
1.6       nicm      156:        int                      flags = 0, i;
1.5       nicm      157:        u_int                    px, py;
1.1       nicm      158:
                    159:        if ((c = cmd_find_client(item, args_get(args, 'c'), 0)) == NULL)
                    160:                return (CMD_RETURN_ERROR);
                    161:        if (c->overlay_draw != NULL)
                    162:                return (CMD_RETURN_NORMAL);
                    163:
                    164:        if (args_has(args, 'T'))
                    165:                title = format_single(NULL, args_get(args, 'T'), c, s, wl, wp);
                    166:        else
                    167:                title = xstrdup("");
1.4       nicm      168:
                    169:        menu = menu_create(title);
                    170:
                    171:        for (i = 0; i != args->argc; /* nothing */) {
                    172:                name = args->argv[i++];
                    173:                if (*name == '\0') {
                    174:                        menu_add_item(menu, NULL, item, c, fs);
                    175:                        continue;
                    176:                }
                    177:
                    178:                if (args->argc - i < 2) {
                    179:                        cmdq_error(item, "not enough arguments");
                    180:                        free(title);
                    181:                        menu_free(menu);
                    182:                        return (CMD_RETURN_ERROR);
                    183:                }
                    184:                key = args->argv[i++];
                    185:
                    186:                menu_item.name = name;
                    187:                menu_item.key = key_string_lookup_string(key);
                    188:                menu_item.command = args->argv[i++];
                    189:
                    190:                menu_add_item(menu, &menu_item, item, c, fs);
                    191:        }
1.1       nicm      192:        free(title);
                    193:        if (menu == NULL) {
1.4       nicm      194:                cmdq_error(item, "invalid menu arguments");
1.1       nicm      195:                return (CMD_RETURN_ERROR);
                    196:        }
                    197:        if (menu->count == 0) {
                    198:                menu_free(menu);
                    199:                return (CMD_RETURN_NORMAL);
                    200:        }
1.5       nicm      201:        cmd_display_menu_get_position(c, item, args, &px, &py, menu->width + 4,
                    202:            menu->count + 2);
1.1       nicm      203:
                    204:        if (!item->shared->mouse.valid)
                    205:                flags |= MENU_NOMOUSE;
                    206:        if (menu_display(menu, flags, item, px, py, c, fs, NULL, NULL) != 0)
1.7       nicm      207:                return (CMD_RETURN_NORMAL);
                    208:        return (CMD_RETURN_WAIT);
                    209: }
                    210:
                    211: static enum cmd_retval
                    212: cmd_display_popup_exec(struct cmd *self, struct cmdq_item *item)
                    213: {
                    214:        struct args             *args = self->args;
                    215:        struct client           *c;
                    216:        struct cmd_find_state   *fs = &item->target;
                    217:        const char              *value, *cmd = NULL, **lines = NULL;
                    218:        const char              *shellcmd = NULL;
                    219:        char                    *cwd, *cause;
                    220:        int                      flags = 0;
                    221:        u_int                    px, py, w, h, nlines = 0;
                    222:
                    223:        if ((c = cmd_find_client(item, args_get(args, 'c'), 0)) == NULL)
                    224:                return (CMD_RETURN_ERROR);
                    225:        if (args_has(args, 'C')) {
                    226:                server_client_clear_overlay(c);
                    227:                return (CMD_RETURN_NORMAL);
                    228:        }
                    229:        if (c->overlay_draw != NULL)
                    230:                return (CMD_RETURN_NORMAL);
                    231:
                    232:        if (args->argc >= 1)
                    233:                cmd = args->argv[0];
                    234:        if (args->argc >= 2) {
                    235:                lines = (const char **)args->argv + 1;
                    236:                nlines = args->argc - 1;
                    237:        }
                    238:
                    239:        if (nlines != 0)
1.8       nicm      240:                h = popup_height(nlines, lines) + 2;
1.7       nicm      241:        else
                    242:                h = c->tty.sy / 2;
                    243:        if (args_has(args, 'h')) {
                    244:                h = args_percentage(args, 'h', 1, c->tty.sy, c->tty.sy, &cause);
                    245:                if (cause != NULL) {
                    246:                        cmdq_error(item, "height %s", cause);
                    247:                        free(cause);
                    248:                        return (CMD_RETURN_ERROR);
                    249:                }
                    250:        }
                    251:
                    252:        if (nlines != 0)
                    253:                w = popup_width(item, nlines, lines, c, fs) + 2;
                    254:        else
                    255:                w = c->tty.sx / 2;
                    256:        if (args_has(args, 'w')) {
                    257:                w = args_percentage(args, 'w', 1, c->tty.sx, c->tty.sx, &cause);
                    258:                if (cause != NULL) {
                    259:                        cmdq_error(item, "width %s", cause);
                    260:                        free(cause);
                    261:                        return (CMD_RETURN_ERROR);
                    262:                }
                    263:        }
                    264:
1.8       nicm      265:        if (w > c->tty.sx - 1)
                    266:                w = c->tty.sx - 1;
                    267:        if (h > c->tty.sy - 1)
                    268:                h = c->tty.sy - 1;
1.7       nicm      269:        cmd_display_menu_get_position(c, item, args, &px, &py, w, h);
                    270:
                    271:        value = args_get(args, 'd');
                    272:        if (value != NULL)
                    273:                cwd = format_single(NULL, value, c, fs->s, fs->wl, fs->wp);
                    274:        else
                    275:                cwd = xstrdup(server_client_get_cwd(c, fs->s));
                    276:
                    277:        value = args_get(args, 'R');
                    278:        if (value != NULL)
                    279:                shellcmd = format_single(NULL, value, c, fs->s, fs->wl, fs->wp);
                    280:
                    281:        if (args_has(args, 'K'))
                    282:                flags |= POPUP_WRITEKEYS;
1.9     ! nicm      283:        if (args_has(args, 'E') > 1)
        !           284:                flags |= POPUP_CLOSEEXITZERO;
        !           285:        else if (args_has(args, 'E'))
1.7       nicm      286:                flags |= POPUP_CLOSEEXIT;
                    287:        if (popup_display(flags, item, px, py, w, h, nlines, lines, shellcmd,
                    288:            cmd, cwd, c, fs) != 0)
1.1       nicm      289:                return (CMD_RETURN_NORMAL);
                    290:        return (CMD_RETURN_WAIT);
                    291: }