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

1.2     ! nicm        1: /* $OpenBSD: cmd-display-menu.c,v 1.1 2019/05/10 18:04:06 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 *);
                     32:
                     33: const struct cmd_entry cmd_display_menu_entry = {
                     34:        .name = "display-menu",
                     35:        .alias = "menu",
                     36:
                     37:        .args = { "c:FM:t:T:x:y:", 0, 0 },
                     38:        .usage = "[-F] [-c target-client] [-M menu] " CMD_TARGET_PANE_USAGE " "
                     39:                 "[-T title] [-x position] [-y position]",
                     40:
                     41:        .target = { 't', CMD_FIND_PANE, 0 },
                     42:
                     43:        .flags = CMD_AFTERHOOK,
                     44:        .exec = cmd_display_menu_exec
                     45: };
                     46:
                     47: static enum cmd_retval
                     48: cmd_display_menu_exec(struct cmd *self, struct cmdq_item *item)
                     49: {
                     50:        struct args             *args = self->args;
                     51:        struct client           *c;
                     52:        struct session          *s = item->target.s;
                     53:        struct winlink          *wl = item->target.wl;
                     54:        struct window_pane      *wp = item->target.wp;
                     55:        struct cmd_find_state   *fs = &item->target;
                     56:        struct menu             *menu = NULL;
                     57:        struct style_range      *sr;
                     58:        const char              *string, *xp, *yp;
                     59:        int                      at, flags;
                     60:        u_int                    px, py, ox, oy, sx, sy;
                     61:        char                    *title;
                     62:
                     63:        if ((c = cmd_find_client(item, args_get(args, 'c'), 0)) == NULL)
                     64:                return (CMD_RETURN_ERROR);
                     65:        if (c->overlay_draw != NULL)
                     66:                return (CMD_RETURN_NORMAL);
                     67:        at = status_at_line(c);
                     68:
                     69:        string = args_get(args, 'M');
                     70:        if (string == NULL) {
                     71:                cmdq_error(item, "no menu specified");
                     72:                return (CMD_RETURN_ERROR);
                     73:        }
                     74:        if (args_has(args, 'F'))
                     75:                string = format_single(NULL, string, c, s, wl, wp);
                     76:        else
                     77:                string = xstrdup(string);
                     78:        if (args_has(args, 'T'))
                     79:                title = format_single(NULL, args_get(args, 'T'), c, s, wl, wp);
                     80:        else
                     81:                title = xstrdup("");
1.2     ! nicm       82:        menu = menu_create(string, c, fs, title);
1.1       nicm       83:        free(title);
                     84:        if (menu == NULL) {
                     85:                cmdq_error(item, "invalid menu %s", string);
                     86:                return (CMD_RETURN_ERROR);
                     87:        }
                     88:        if (menu->count == 0) {
                     89:                menu_free(menu);
                     90:                return (CMD_RETURN_NORMAL);
                     91:        }
                     92:
                     93:        xp = args_get(args, 'x');
                     94:        if (xp == NULL)
                     95:                px = 0;
                     96:        else if (strcmp(xp, "R") == 0)
                     97:                px = c->tty.sx - 1;
                     98:        else if (strcmp(xp, "P") == 0) {
                     99:                tty_window_offset(&c->tty, &ox, &oy, &sx, &sy);
                    100:                if (wp->xoff >= ox)
                    101:                        px = wp->xoff - ox;
                    102:                else
                    103:                        px = 0;
                    104:        } else if (strcmp(xp, "M") == 0 && item->shared->mouse.valid) {
                    105:                if (item->shared->mouse.x > (menu->width + 4) / 2)
                    106:                        px = item->shared->mouse.x - (menu->width + 4) / 2;
                    107:                else
                    108:                        px = 0;
                    109:        }
                    110:        else if (strcmp(xp, "W") == 0) {
                    111:                if (at == -1)
                    112:                        px = 0;
                    113:                else {
                    114:                        TAILQ_FOREACH(sr, &c->status.entries[0].ranges, entry) {
                    115:                                if (sr->type != STYLE_RANGE_WINDOW)
                    116:                                        continue;
                    117:                                if (sr->argument == (u_int)wl->idx)
                    118:                                        break;
                    119:                        }
                    120:                        if (sr != NULL)
                    121:                                px = sr->start;
                    122:                        else
                    123:                                px = 0;
                    124:                }
                    125:        } else
                    126:                px = strtoul(xp, NULL, 10);
                    127:        if (px + menu->width + 4 >= c->tty.sx)
                    128:                px = c->tty.sx - menu->width - 4;
                    129:
                    130:        yp = args_get(args, 'y');
                    131:        if (yp == NULL)
                    132:                py = 0;
                    133:        else if (strcmp(yp, "P") == 0) {
                    134:                tty_window_offset(&c->tty, &ox, &oy, &sx, &sy);
                    135:                if (wp->yoff + wp->sy >= oy)
                    136:                        py = wp->yoff + wp->sy - oy;
                    137:                else
                    138:                        py = 0;
                    139:        } else if (strcmp(yp, "M") == 0 && item->shared->mouse.valid)
                    140:                py = item->shared->mouse.y + menu->count + 2;
                    141:        else if (strcmp(yp, "S") == 0) {
                    142:                if (at == -1)
                    143:                        py = c->tty.sy;
                    144:                else if (at == 0)
                    145:                        py = status_line_size(c) + menu->count + 2;
                    146:                else
                    147:                        py = at;
                    148:        } else
                    149:                py = strtoul(yp, NULL, 10);
                    150:        if (py < menu->count + 2)
                    151:                py = 0;
                    152:        else
                    153:                py -= menu->count + 2;
                    154:        if (py + menu->count + 2 >= c->tty.sy)
                    155:                py = c->tty.sy - menu->count - 2;
                    156:
                    157:        flags = 0;
                    158:        if (!item->shared->mouse.valid)
                    159:                flags |= MENU_NOMOUSE;
                    160:        if (menu_display(menu, flags, item, px, py, c, fs, NULL, NULL) != 0)
                    161:                return (CMD_RETURN_NORMAL);
                    162:        return (CMD_RETURN_WAIT);
                    163: }