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

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