[BACK]Return to menu.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / tmux

Annotation of src/usr.bin/tmux/menu.c, Revision 1.41

1.41    ! nicm        1: /* $OpenBSD: menu.c,v 1.40 2021/10/22 17:12:50 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: struct menu_data {
                     27:        struct cmdq_item        *item;
                     28:        int                      flags;
                     29:
                     30:        struct cmd_find_state    fs;
                     31:        struct screen            s;
                     32:
                     33:        u_int                    px;
                     34:        u_int                    py;
                     35:
                     36:        struct menu             *menu;
                     37:        int                      choice;
                     38:
                     39:        menu_choice_cb           cb;
                     40:        void                    *data;
                     41: };
                     42:
1.8       nicm       43: void
                     44: menu_add_items(struct menu *menu, const struct menu_item *items,
                     45:     struct cmdq_item *qitem, struct client *c, struct cmd_find_state *fs)
                     46: {
                     47:        const struct menu_item  *loop;
                     48:
                     49:        for (loop = items; loop->name != NULL; loop++)
                     50:                menu_add_item(menu, loop, qitem, c, fs);
                     51: }
                     52:
                     53: void
                     54: menu_add_item(struct menu *menu, const struct menu_item *item,
1.6       nicm       55:     struct cmdq_item *qitem, struct client *c, struct cmd_find_state *fs)
1.1       nicm       56: {
                     57:        struct menu_item        *new_item;
1.40      nicm       58:        const char              *key = NULL, *cmd, *suffix = "";
1.8       nicm       59:        char                    *s, *name;
1.40      nicm       60:        u_int                    width, max_width;
1.8       nicm       61:        int                      line;
1.40      nicm       62:        size_t                   keylen, slen;
1.8       nicm       63:
                     64:        line = (item == NULL || item->name == NULL || *item->name == '\0');
                     65:        if (line && menu->count == 0)
                     66:                return;
1.1       nicm       67:
                     68:        menu->items = xreallocarray(menu->items, menu->count + 1,
                     69:            sizeof *menu->items);
                     70:        new_item = &menu->items[menu->count++];
                     71:        memset(new_item, 0, sizeof *new_item);
                     72:
1.8       nicm       73:        if (line)
1.1       nicm       74:                return;
1.8       nicm       75:
                     76:        if (fs != NULL)
1.24      nicm       77:                s = format_single_from_state(qitem, item->name, c, fs);
1.8       nicm       78:        else
                     79:                s = format_single(qitem, item->name, c, NULL, NULL, NULL);
                     80:        if (*s == '\0') { /* no item if empty after format expanded */
1.1       nicm       81:                menu->count--;
                     82:                return;
                     83:        }
1.40      nicm       84:        max_width = c->tty.sx - 4;
                     85:
                     86:        slen = strlen(s);
1.9       nicm       87:        if (*s != '-' && item->key != KEYC_UNKNOWN && item->key != KEYC_NONE) {
1.29      nicm       88:                key = key_string_lookup_key(item->key, 0);
1.40      nicm       89:                keylen = strlen(key) + 3; /* 3 = space and two brackets */
                     90:
                     91:                /*
1.41    ! nicm       92:                 * Add the key if it is shorter than a quarter of the available
        !            93:                 * space or there is space for the entire item text and the
        !            94:                 * key.
1.40      nicm       95:                 */
1.41    ! nicm       96:                if (keylen <= max_width / 4)
        !            97:                        max_width -= keylen;
        !            98:                else if (keylen >= max_width || slen >= max_width - keylen)
1.40      nicm       99:                        key = NULL;
                    100:        }
                    101:
1.41    ! nicm      102:        if (slen > max_width) {
        !           103:                max_width--;
        !           104:                suffix = ">";
        !           105:        }
1.40      nicm      106:        if (key != NULL)
1.41    ! nicm      107:                xasprintf(&name, "%.*s%s#[default] #[align=right](%s)",
        !           108:                    (int)max_width, s, suffix, key);
        !           109:        else
1.40      nicm      110:                xasprintf(&name, "%.*s%s", (int)max_width, s, suffix);
1.41    ! nicm      111:
1.8       nicm      112:        new_item->name = name;
                    113:        free(s);
                    114:
                    115:        cmd = item->command;
                    116:        if (cmd != NULL) {
                    117:                if (fs != NULL)
1.24      nicm      118:                        s = format_single_from_state(qitem, cmd, c, fs);
1.8       nicm      119:                else
                    120:                        s = format_single(qitem, cmd, c, NULL, NULL, NULL);
1.6       nicm      121:        } else
1.8       nicm      122:                s = NULL;
                    123:        new_item->command = s;
1.1       nicm      124:        new_item->key = item->key;
                    125:
                    126:        width = format_width(new_item->name);
1.39      nicm      127:        if (*new_item->name == '-')
                    128:                width--;
1.1       nicm      129:        if (width > menu->width)
                    130:                menu->width = width;
                    131: }
                    132:
                    133: struct menu *
1.8       nicm      134: menu_create(const char *title)
1.1       nicm      135: {
                    136:        struct menu     *menu;
                    137:
                    138:        menu = xcalloc(1, sizeof *menu);
                    139:        menu->title = xstrdup(title);
1.30      nicm      140:        menu->width = format_width(title);
1.1       nicm      141:
                    142:        return (menu);
                    143: }
                    144:
                    145: void
                    146: menu_free(struct menu *menu)
                    147: {
                    148:        u_int   i;
                    149:
                    150:        for (i = 0; i < menu->count; i++) {
1.8       nicm      151:                free((void *)menu->items[i].name);
                    152:                free((void *)menu->items[i].command);
1.1       nicm      153:        }
                    154:        free(menu->items);
                    155:
1.8       nicm      156:        free((void *)menu->title);
1.1       nicm      157:        free(menu);
                    158: }
                    159:
1.35      nicm      160: struct screen *
                    161: menu_mode_cb(__unused struct client *c, void *data, __unused u_int *cx,
                    162:     __unused u_int *cy)
1.15      nicm      163: {
1.35      nicm      164:        struct menu_data        *md = data;
1.15      nicm      165:
1.27      nicm      166:        return (&md->s);
1.15      nicm      167: }
                    168:
1.38      nicm      169: /* Return parts of the input range which are not obstructed by the menu. */
                    170: void
                    171: menu_check_cb(__unused struct client *c, void *data, u_int px, u_int py,
                    172:     u_int nx, struct overlay_ranges *r)
1.35      nicm      173: {
                    174:        struct menu_data        *md = data;
                    175:        struct menu             *menu = md->menu;
                    176:
1.38      nicm      177:        server_client_overlay_range(md->px, md->py, menu->width + 4,
                    178:            menu->count + 2, px, py, nx, r);
1.35      nicm      179: }
                    180:
                    181: void
                    182: menu_draw_cb(struct client *c, void *data,
                    183:     __unused struct screen_redraw_ctx *rctx)
1.1       nicm      184: {
1.35      nicm      185:        struct menu_data        *md = data;
1.1       nicm      186:        struct tty              *tty = &c->tty;
                    187:        struct screen           *s = &md->s;
                    188:        struct menu             *menu = md->menu;
                    189:        struct screen_write_ctx  ctx;
1.13      nicm      190:        u_int                    i, px = md->px, py = md->py;
1.22      nicm      191:        struct grid_cell         gc;
                    192:
1.25      nicm      193:        style_apply(&gc, c->session->curw->window->options, "mode-style", NULL);
1.1       nicm      194:
1.27      nicm      195:        screen_write_start(&ctx, s);
1.1       nicm      196:        screen_write_clearscreen(&ctx, 8);
1.22      nicm      197:        screen_write_menu(&ctx, menu, md->choice, &gc);
1.1       nicm      198:        screen_write_stop(&ctx);
                    199:
1.27      nicm      200:        for (i = 0; i < screen_size_y(&md->s); i++) {
                    201:                tty_draw_line(tty, s, 0, i, menu->width + 4, px, py + i,
                    202:                    &grid_default_cell, NULL);
                    203:        }
1.1       nicm      204: }
                    205:
1.35      nicm      206: void
                    207: menu_free_cb(__unused struct client *c, void *data)
1.1       nicm      208: {
1.35      nicm      209:        struct menu_data        *md = data;
1.1       nicm      210:
                    211:        if (md->item != NULL)
1.10      nicm      212:                cmdq_continue(md->item);
1.1       nicm      213:
1.3       nicm      214:        if (md->cb != NULL)
                    215:                md->cb(md->menu, UINT_MAX, KEYC_NONE, md->data);
                    216:
1.1       nicm      217:        screen_free(&md->s);
                    218:        menu_free(md->menu);
                    219:        free(md);
                    220: }
                    221:
1.35      nicm      222: int
                    223: menu_key_cb(struct client *c, void *data, struct key_event *event)
1.1       nicm      224: {
1.35      nicm      225:        struct menu_data                *md = data;
1.1       nicm      226:        struct menu                     *menu = md->menu;
                    227:        struct mouse_event              *m = &event->m;
                    228:        u_int                            i;
                    229:        int                              count = menu->count, old = md->choice;
1.31      nicm      230:        const char                      *name = NULL;
1.1       nicm      231:        const struct menu_item          *item;
1.21      nicm      232:        struct cmdq_state               *state;
                    233:        enum cmd_parse_status            status;
                    234:        char                            *error;
1.1       nicm      235:
                    236:        if (KEYC_IS_MOUSE(event->key)) {
1.12      nicm      237:                if (md->flags & MENU_NOMOUSE) {
                    238:                        if (MOUSE_BUTTONS(m->b) != 0)
                    239:                                return (1);
1.1       nicm      240:                        return (0);
1.12      nicm      241:                }
1.1       nicm      242:                if (m->x < md->px ||
                    243:                    m->x > md->px + 4 + menu->width ||
                    244:                    m->y < md->py + 1 ||
                    245:                    m->y > md->py + 1 + count - 1) {
1.32      nicm      246:                        if (~md->flags & MENU_STAYOPEN) {
                    247:                                if (MOUSE_RELEASE(m->b))
                    248:                                        return (1);
                    249:                        } else {
                    250:                                if (!MOUSE_RELEASE(m->b) &&
                    251:                                    MOUSE_WHEEL(m->b) == 0 &&
                    252:                                    !MOUSE_DRAG(m->b))
                    253:                                        return (1);
                    254:                        }
1.1       nicm      255:                        if (md->choice != -1) {
                    256:                                md->choice = -1;
                    257:                                c->flags |= CLIENT_REDRAWOVERLAY;
                    258:                        }
                    259:                        return (0);
                    260:                }
1.32      nicm      261:                if (~md->flags & MENU_STAYOPEN) {
                    262:                        if (MOUSE_RELEASE(m->b))
                    263:                                goto chosen;
                    264:                } else {
                    265:                        if (MOUSE_WHEEL(m->b) == 0 && !MOUSE_DRAG(m->b))
                    266:                                goto chosen;
                    267:                }
1.7       nicm      268:                md->choice = m->y - (md->py + 1);
1.1       nicm      269:                if (md->choice != old)
                    270:                        c->flags |= CLIENT_REDRAWOVERLAY;
                    271:                return (0);
                    272:        }
1.11      nicm      273:        for (i = 0; i < (u_int)count; i++) {
                    274:                name = menu->items[i].name;
                    275:                if (name == NULL || *name == '-')
                    276:                        continue;
                    277:                if (event->key == menu->items[i].key) {
                    278:                        md->choice = i;
                    279:                        goto chosen;
                    280:                }
                    281:        }
1.29      nicm      282:        switch (event->key & ~KEYC_MASK_FLAGS) {
1.1       nicm      283:        case KEYC_UP:
1.11      nicm      284:        case 'k':
1.9       nicm      285:                if (old == -1)
                    286:                        old = 0;
1.1       nicm      287:                do {
                    288:                        if (md->choice == -1 || md->choice == 0)
                    289:                                md->choice = count - 1;
                    290:                        else
                    291:                                md->choice--;
1.9       nicm      292:                        name = menu->items[md->choice].name;
                    293:                } while ((name == NULL || *name == '-') && md->choice != old);
1.1       nicm      294:                c->flags |= CLIENT_REDRAWOVERLAY;
                    295:                return (0);
1.26      nicm      296:        case KEYC_BSPACE:
                    297:                if (~md->flags & MENU_TAB)
                    298:                        break;
                    299:                return (1);
                    300:        case '\011': /* Tab */
                    301:                if (~md->flags & MENU_TAB)
                    302:                        break;
                    303:                if (md->choice == count - 1)
                    304:                        return (1);
                    305:                /* FALLTHROUGH */
1.1       nicm      306:        case KEYC_DOWN:
1.11      nicm      307:        case 'j':
1.9       nicm      308:                if (old == -1)
                    309:                        old = 0;
1.1       nicm      310:                do {
                    311:                        if (md->choice == -1 || md->choice == count - 1)
                    312:                                md->choice = 0;
1.9       nicm      313:                        else
                    314:                                md->choice++;
                    315:                        name = menu->items[md->choice].name;
                    316:                } while ((name == NULL || *name == '-') && md->choice != old);
1.1       nicm      317:                c->flags |= CLIENT_REDRAWOVERLAY;
                    318:                return (0);
1.26      nicm      319:        case 'g':
                    320:        case KEYC_PPAGE:
                    321:        case '\002': /* C-b */
                    322:                if (md->choice > 5)
                    323:                        md->choice -= 5;
                    324:                else
                    325:                        md->choice = 0;
                    326:                while (md->choice != count && (name == NULL || *name == '-'))
                    327:                        md->choice++;
                    328:                if (md->choice == count)
                    329:                        md->choice = -1;
                    330:                c->flags |= CLIENT_REDRAWOVERLAY;
                    331:                break;
                    332:        case 'G':
                    333:        case KEYC_NPAGE:
                    334:                if (md->choice > count - 6)
                    335:                        md->choice = count - 1;
                    336:                else
                    337:                        md->choice += 5;
                    338:                while (md->choice != -1 && (name == NULL || *name == '-'))
                    339:                        md->choice--;
                    340:                c->flags |= CLIENT_REDRAWOVERLAY;
                    341:                break;
                    342:        case '\006': /* C-f */
                    343:                break;
1.1       nicm      344:        case '\r':
                    345:                goto chosen;
                    346:        case '\033': /* Escape */
                    347:        case '\003': /* C-c */
                    348:        case '\007': /* C-g */
                    349:        case 'q':
                    350:                return (1);
                    351:        }
                    352:        return (0);
                    353:
                    354: chosen:
                    355:        if (md->choice == -1)
                    356:                return (1);
                    357:        item = &menu->items[md->choice];
1.33      nicm      358:        if (item->name == NULL || *item->name == '-') {
                    359:                if (md->flags & MENU_STAYOPEN)
                    360:                        return (0);
1.1       nicm      361:                return (1);
1.33      nicm      362:        }
1.1       nicm      363:        if (md->cb != NULL) {
                    364:            md->cb(md->menu, md->choice, item->key, md->data);
1.3       nicm      365:            md->cb = NULL;
1.1       nicm      366:            return (1);
                    367:        }
1.5       nicm      368:
1.21      nicm      369:        if (md->item != NULL)
                    370:                event = cmdq_get_event(md->item);
                    371:        else
                    372:                event = NULL;
                    373:        state = cmdq_new_state(&md->fs, event, 0);
                    374:
                    375:        status = cmd_parse_and_append(item->command, NULL, c, state, &error);
                    376:        if (status == CMD_PARSE_ERROR) {
                    377:                cmdq_append(c, cmdq_get_error(error));
                    378:                free(error);
1.1       nicm      379:        }
1.21      nicm      380:        cmdq_free_state(state);
                    381:
1.1       nicm      382:        return (1);
                    383: }
                    384:
1.35      nicm      385: struct menu_data *
                    386: menu_prepare(struct menu *menu, int flags, struct cmdq_item *item, u_int px,
1.1       nicm      387:     u_int py, struct client *c, struct cmd_find_state *fs, menu_choice_cb cb,
                    388:     void *data)
                    389: {
                    390:        struct menu_data        *md;
1.23      nicm      391:        u_int                    i;
                    392:        const char              *name;
1.1       nicm      393:
                    394:        if (c->tty.sx < menu->width + 4 || c->tty.sy < menu->count + 2)
1.35      nicm      395:                return (NULL);
1.14      nicm      396:        if (px + menu->width + 4 > c->tty.sx)
                    397:                px = c->tty.sx - menu->width - 4;
                    398:        if (py + menu->count + 2 > c->tty.sy)
                    399:                py = c->tty.sy - menu->count - 2;
1.1       nicm      400:
                    401:        md = xcalloc(1, sizeof *md);
                    402:        md->item = item;
                    403:        md->flags = flags;
                    404:
1.3       nicm      405:        if (fs != NULL)
                    406:                cmd_find_copy_state(&md->fs, fs);
1.1       nicm      407:        screen_init(&md->s, menu->width + 4, menu->count + 2, 0);
1.27      nicm      408:        if (~md->flags & MENU_NOMOUSE)
1.37      nicm      409:                md->s.mode |= (MODE_MOUSE_ALL|MODE_MOUSE_BUTTON);
1.28      nicm      410:        md->s.mode &= ~MODE_CURSOR;
1.1       nicm      411:
                    412:        md->px = px;
                    413:        md->py = py;
                    414:
                    415:        md->menu = menu;
1.23      nicm      416:        if (md->flags & MENU_NOMOUSE) {
                    417:                for (i = 0; i < menu->count; i++) {
                    418:                        name = menu->items[i].name;
                    419:                        if (name != NULL && *name != '-')
                    420:                                break;
                    421:                }
                    422:                if (i != menu->count)
                    423:                        md->choice = i;
                    424:                else
                    425:                        md->choice = -1;
                    426:        } else
                    427:                md->choice = -1;
1.1       nicm      428:
                    429:        md->cb = cb;
                    430:        md->data = data;
1.35      nicm      431:        return (md);
                    432: }
                    433:
                    434: int
                    435: menu_display(struct menu *menu, int flags, struct cmdq_item *item, u_int px,
                    436:     u_int py, struct client *c, struct cmd_find_state *fs, menu_choice_cb cb,
                    437:     void *data)
                    438: {
                    439:        struct menu_data        *md;
1.1       nicm      440:
1.35      nicm      441:        md = menu_prepare(menu, flags, item, px, py, c, fs, cb, data);
                    442:        if (md == NULL)
                    443:                return (-1);
1.15      nicm      444:        server_client_set_overlay(c, 0, NULL, menu_mode_cb, menu_draw_cb,
1.34      nicm      445:            menu_key_cb, menu_free_cb, NULL, md);
1.1       nicm      446:        return (0);
                    447: }