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

Annotation of src/usr.bin/tmux/status.c, Revision 1.210

1.210   ! nicm        1: /* $OpenBSD: status.c,v 1.209 2020/05/16 15:47:22 nicm Exp $ */
1.1       nicm        2:
                      3: /*
1.148     nicm        4:  * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
1.1       nicm        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: #include <sys/time.h>
                     21:
                     22: #include <errno.h>
                     23: #include <limits.h>
                     24: #include <stdarg.h>
                     25: #include <stdlib.h>
                     26: #include <string.h>
                     27: #include <time.h>
                     28: #include <unistd.h>
                     29:
                     30: #include "tmux.h"
                     31:
1.151     nicm       32: static void     status_message_callback(int, short, void *);
                     33: static void     status_timer_callback(int, short, void *);
                     34:
                     35: static char    *status_prompt_find_history_file(void);
                     36: static const char *status_prompt_up_history(u_int *);
                     37: static const char *status_prompt_down_history(u_int *);
                     38: static void     status_prompt_add_history(const char *);
                     39:
1.204     nicm       40: static char    *status_prompt_complete(struct client *, const char *, u_int);
1.205     nicm       41: static char    *status_prompt_complete_window_menu(struct client *,
1.206     nicm       42:                     struct session *, const char *, u_int, char);
1.204     nicm       43:
                     44: struct status_prompt_menu {
                     45:        struct client    *c;
                     46:        u_int             start;
                     47:        u_int             size;
                     48:        char            **list;
                     49:        char              flag;
                     50: };
1.130     nicm       51:
1.65      nicm       52: /* Status prompt history. */
1.128     nicm       53: #define PROMPT_HISTORY 100
1.151     nicm       54: static char    **status_prompt_hlist;
                     55: static u_int     status_prompt_hsize;
1.130     nicm       56:
                     57: /* Find the history file to load/save from/to. */
1.151     nicm       58: static char *
1.130     nicm       59: status_prompt_find_history_file(void)
                     60: {
                     61:        const char      *home, *history_file;
                     62:        char            *path;
                     63:
1.137     nicm       64:        history_file = options_get_string(global_options, "history-file");
1.130     nicm       65:        if (*history_file == '\0')
                     66:                return (NULL);
                     67:        if (*history_file == '/')
                     68:                return (xstrdup(history_file));
                     69:
                     70:        if (history_file[0] != '~' || history_file[1] != '/')
                     71:                return (NULL);
                     72:        if ((home = find_home()) == NULL)
                     73:                return (NULL);
                     74:        xasprintf(&path, "%s%s", home, history_file + 1);
                     75:        return (path);
                     76: }
                     77:
                     78: /* Load status prompt history from file. */
                     79: void
                     80: status_prompt_load_history(void)
                     81: {
                     82:        FILE    *f;
                     83:        char    *history_file, *line, *tmp;
                     84:        size_t   length;
                     85:
                     86:        if ((history_file = status_prompt_find_history_file()) == NULL)
                     87:                return;
                     88:        log_debug("loading history from %s", history_file);
                     89:
                     90:        f = fopen(history_file, "r");
                     91:        if (f == NULL) {
                     92:                log_debug("%s: %s", history_file, strerror(errno));
                     93:                free(history_file);
                     94:                return;
                     95:        }
                     96:        free(history_file);
                     97:
                     98:        for (;;) {
                     99:                if ((line = fgetln(f, &length)) == NULL)
                    100:                        break;
                    101:
                    102:                if (length > 0) {
                    103:                        if (line[length - 1] == '\n') {
                    104:                                line[length - 1] = '\0';
                    105:                                status_prompt_add_history(line);
                    106:                        } else {
                    107:                                tmp = xmalloc(length + 1);
                    108:                                memcpy(tmp, line, length);
                    109:                                tmp[length] = '\0';
                    110:                                status_prompt_add_history(tmp);
                    111:                                free(tmp);
                    112:                        }
                    113:                }
                    114:        }
                    115:        fclose(f);
                    116: }
                    117:
                    118: /* Save status prompt history to file. */
                    119: void
                    120: status_prompt_save_history(void)
                    121: {
                    122:        FILE    *f;
                    123:        u_int    i;
                    124:        char    *history_file;
                    125:
                    126:        if ((history_file = status_prompt_find_history_file()) == NULL)
                    127:                return;
                    128:        log_debug("saving history to %s", history_file);
                    129:
                    130:        f = fopen(history_file, "w");
                    131:        if (f == NULL) {
                    132:                log_debug("%s: %s", history_file, strerror(errno));
                    133:                free(history_file);
                    134:                return;
                    135:        }
                    136:        free(history_file);
                    137:
                    138:        for (i = 0; i < status_prompt_hsize; i++) {
                    139:                fputs(status_prompt_hlist[i], f);
                    140:                fputc('\n', f);
                    141:        }
                    142:        fclose(f);
                    143:
1.87      nicm      144: }
                    145:
1.133     nicm      146: /* Status timer callback. */
1.151     nicm      147: static void
1.141     nicm      148: status_timer_callback(__unused int fd, __unused short events, void *arg)
1.133     nicm      149: {
                    150:        struct client   *c = arg;
                    151:        struct session  *s = c->session;
                    152:        struct timeval   tv;
                    153:
1.175     nicm      154:        evtimer_del(&c->status.timer);
1.133     nicm      155:
                    156:        if (s == NULL)
                    157:                return;
                    158:
                    159:        if (c->message_string == NULL && c->prompt_string == NULL)
1.177     nicm      160:                c->flags |= CLIENT_REDRAWSTATUS;
1.133     nicm      161:
                    162:        timerclear(&tv);
1.137     nicm      163:        tv.tv_sec = options_get_number(s->options, "status-interval");
1.133     nicm      164:
                    165:        if (tv.tv_sec != 0)
1.175     nicm      166:                evtimer_add(&c->status.timer, &tv);
1.136     nicm      167:        log_debug("client %p, status interval %d", c, (int)tv.tv_sec);
1.133     nicm      168: }
                    169:
                    170: /* Start status timer for client. */
                    171: void
                    172: status_timer_start(struct client *c)
                    173: {
                    174:        struct session  *s = c->session;
                    175:
1.175     nicm      176:        if (event_initialized(&c->status.timer))
                    177:                evtimer_del(&c->status.timer);
1.133     nicm      178:        else
1.175     nicm      179:                evtimer_set(&c->status.timer, status_timer_callback, c);
1.133     nicm      180:
1.137     nicm      181:        if (s != NULL && options_get_number(s->options, "status"))
1.133     nicm      182:                status_timer_callback(-1, 0, c);
                    183: }
                    184:
                    185: /* Start status timer for all clients. */
                    186: void
                    187: status_timer_start_all(void)
                    188: {
                    189:        struct client   *c;
                    190:
                    191:        TAILQ_FOREACH(c, &clients, entry)
                    192:                status_timer_start(c);
                    193: }
                    194:
1.161     nicm      195: /* Update status cache. */
                    196: void
1.187     nicm      197: status_update_cache(struct session *s)
1.161     nicm      198: {
1.192     nicm      199:        s->statuslines = options_get_number(s->options, "status");
                    200:        if (s->statuslines == 0)
1.161     nicm      201:                s->statusat = -1;
                    202:        else if (options_get_number(s->options, "status-position") == 0)
                    203:                s->statusat = 0;
                    204:        else
                    205:                s->statusat = 1;
                    206: }
                    207:
1.87      nicm      208: /* Get screen line of status line. -1 means off. */
                    209: int
                    210: status_at_line(struct client *c)
                    211: {
                    212:        struct session  *s = c->session;
                    213:
1.198     nicm      214:        if (c->flags & (CLIENT_STATUSOFF|CLIENT_CONTROL))
1.169     nicm      215:                return (-1);
1.161     nicm      216:        if (s->statusat != 1)
                    217:                return (s->statusat);
1.182     nicm      218:        return (c->tty.sy - status_line_size(c));
1.169     nicm      219: }
                    220:
1.182     nicm      221: /* Get size of status line for client's session. 0 means off. */
1.169     nicm      222: u_int
1.182     nicm      223: status_line_size(struct client *c)
1.169     nicm      224: {
1.182     nicm      225:        struct session  *s = c->session;
                    226:
1.198     nicm      227:        if (c->flags & (CLIENT_STATUSOFF|CLIENT_CONTROL))
1.182     nicm      228:                return (0);
1.192     nicm      229:        return (s->statuslines);
1.71      nicm      230: }
                    231:
1.192     nicm      232: /* Get window at window list position. */
                    233: struct style_range *
                    234: status_get_range(struct client *c, u_int x, u_int y)
1.48      nicm      235: {
1.192     nicm      236:        struct status_line      *sl = &c->status;
                    237:        struct style_range      *sr;
1.48      nicm      238:
1.192     nicm      239:        if (y >= nitems(sl->entries))
                    240:                return (NULL);
                    241:        TAILQ_FOREACH(sr, &sl->entries[y].ranges, entry) {
                    242:                if (x >= sr->start && x < sr->end)
                    243:                        return (sr);
                    244:        }
                    245:        return (NULL);
1.48      nicm      246: }
                    247:
1.192     nicm      248: /* Free all ranges. */
                    249: static void
                    250: status_free_ranges(struct style_ranges *srs)
1.48      nicm      251: {
1.192     nicm      252:        struct style_range      *sr, *sr1;
1.48      nicm      253:
1.192     nicm      254:        TAILQ_FOREACH_SAFE(sr, srs, entry, sr1) {
                    255:                TAILQ_REMOVE(srs, sr, entry);
                    256:                free(sr);
1.73      nicm      257:        }
                    258: }
                    259:
1.189     nicm      260: /* Save old status line. */
                    261: static void
                    262: status_push_screen(struct client *c)
                    263: {
                    264:        struct status_line *sl = &c->status;
                    265:
                    266:        if (sl->active == &sl->screen) {
                    267:                sl->active = xmalloc(sizeof *sl->active);
                    268:                screen_init(sl->active, c->tty.sx, status_line_size(c), 0);
                    269:        }
                    270:        sl->references++;
                    271: }
                    272:
                    273: /* Restore old status line. */
                    274: static void
                    275: status_pop_screen(struct client *c)
                    276: {
                    277:        struct status_line *sl = &c->status;
                    278:
                    279:        if (--sl->references == 0) {
                    280:                screen_free(sl->active);
                    281:                free(sl->active);
                    282:                sl->active = &sl->screen;
                    283:        }
                    284: }
                    285:
1.187     nicm      286: /* Initialize status line. */
                    287: void
                    288: status_init(struct client *c)
                    289: {
                    290:        struct status_line      *sl = &c->status;
1.192     nicm      291:        u_int                    i;
                    292:
                    293:        for (i = 0; i < nitems(sl->entries); i++)
                    294:                TAILQ_INIT(&sl->entries[i].ranges);
1.187     nicm      295:
                    296:        screen_init(&sl->screen, c->tty.sx, 1, 0);
1.189     nicm      297:        sl->active = &sl->screen;
1.187     nicm      298: }
                    299:
1.186     nicm      300: /* Free status line. */
                    301: void
                    302: status_free(struct client *c)
                    303: {
                    304:        struct status_line      *sl = &c->status;
1.192     nicm      305:        u_int                    i;
                    306:
                    307:        for (i = 0; i < nitems(sl->entries); i++) {
                    308:                status_free_ranges(&sl->entries[i].ranges);
                    309:                free((void *)sl->entries[i].expanded);
                    310:        }
1.186     nicm      311:
                    312:        if (event_initialized(&sl->timer))
                    313:                evtimer_del(&sl->timer);
                    314:
1.189     nicm      315:        if (sl->active != &sl->screen) {
                    316:                screen_free(sl->active);
                    317:                free(sl->active);
                    318:        }
1.187     nicm      319:        screen_free(&sl->screen);
1.186     nicm      320: }
                    321:
                    322: /* Draw status line for client. */
1.1       nicm      323: int
                    324: status_redraw(struct client *c)
                    325: {
1.192     nicm      326:        struct status_line              *sl = &c->status;
                    327:        struct status_line_entry        *sle;
                    328:        struct session                  *s = c->session;
                    329:        struct screen_write_ctx          ctx;
                    330:        struct grid_cell                 gc;
1.197     nicm      331:        u_int                            lines, i, n, width = c->tty.sx;
1.203     nicm      332:        int                              flags, force = 0, changed = 0, fg, bg;
1.192     nicm      333:        struct options_entry            *o;
1.193     nicm      334:        union options_value             *ov;
1.192     nicm      335:        struct format_tree              *ft;
                    336:        char                            *expanded;
                    337:
                    338:        log_debug("%s enter", __func__);
1.1       nicm      339:
1.189     nicm      340:        /* Shouldn't get here if not the active screen. */
                    341:        if (sl->active != &sl->screen)
                    342:                fatalx("not the active screen");
1.167     nicm      343:
1.49      nicm      344:        /* No status line? */
1.182     nicm      345:        lines = status_line_size(c);
1.169     nicm      346:        if (c->tty.sy == 0 || lines == 0)
1.7       nicm      347:                return (1);
1.1       nicm      348:
1.207     nicm      349:        /* Create format tree. */
                    350:        flags = FORMAT_STATUS;
                    351:        if (c->flags & CLIENT_STATUSFORCE)
                    352:                flags |= FORMAT_FORCE;
                    353:        ft = format_create(c, NULL, FORMAT_NONE, flags);
                    354:        format_defaults(ft, c, NULL, NULL, NULL);
                    355:
1.48      nicm      356:        /* Set up default colour. */
1.207     nicm      357:        style_apply(&gc, s->options, "status-style", ft);
1.203     nicm      358:        fg = options_get_number(s->options, "status-fg");
                    359:        if (fg != 8)
                    360:                gc.fg = fg;
                    361:        bg = options_get_number(s->options, "status-bg");
                    362:        if (bg != 8)
                    363:                gc.bg = bg;
1.192     nicm      364:        if (!grid_cells_equal(&gc, &sl->style)) {
                    365:                force = 1;
                    366:                memcpy(&sl->style, &gc, sizeof sl->style);
                    367:        }
                    368:
                    369:        /* Resize the target screen. */
                    370:        if (screen_size_x(&sl->screen) != width ||
                    371:            screen_size_y(&sl->screen) != lines) {
                    372:                screen_resize(&sl->screen, width, lines, 0);
1.200     nicm      373:                changed = force = 1;
1.1       nicm      374:        }
1.208     nicm      375:        screen_write_start(&ctx, &sl->screen);
1.1       nicm      376:
1.192     nicm      377:        /* Write the status lines. */
                    378:        o = options_get(s->options, "status-format");
1.197     nicm      379:        if (o == NULL) {
                    380:                for (n = 0; n < width * lines; n++)
                    381:                        screen_write_putc(&ctx, &gc, ' ');
                    382:        } else {
1.192     nicm      383:                for (i = 0; i < lines; i++) {
                    384:                        screen_write_cursormove(&ctx, 0, i, 0);
                    385:
1.193     nicm      386:                        ov = options_array_get(o, i);
                    387:                        if (ov == NULL) {
1.197     nicm      388:                                for (n = 0; n < width; n++)
                    389:                                        screen_write_putc(&ctx, &gc, ' ');
1.192     nicm      390:                                continue;
                    391:                        }
                    392:                        sle = &sl->entries[i];
1.1       nicm      393:
1.193     nicm      394:                        expanded = format_expand_time(ft, ov->string);
1.192     nicm      395:                        if (!force &&
                    396:                            sle->expanded != NULL &&
                    397:                            strcmp(expanded, sle->expanded) == 0) {
                    398:                                free(expanded);
                    399:                                continue;
                    400:                        }
                    401:                        changed = 1;
1.1       nicm      402:
1.197     nicm      403:                        for (n = 0; n < width; n++)
                    404:                                screen_write_putc(&ctx, &gc, ' ');
                    405:                        screen_write_cursormove(&ctx, 0, i, 0);
                    406:
1.192     nicm      407:                        status_free_ranges(&sle->ranges);
                    408:                        format_draw(&ctx, &gc, width, expanded, &sle->ranges);
1.1       nicm      409:
1.192     nicm      410:                        free(sle->expanded);
                    411:                        sle->expanded = expanded;
1.48      nicm      412:                }
                    413:        }
                    414:        screen_write_stop(&ctx);
1.1       nicm      415:
1.192     nicm      416:        /* Free the format tree. */
1.99      nicm      417:        format_free(ft);
1.1       nicm      418:
1.192     nicm      419:        /* Return if the status line has changed. */
                    420:        log_debug("%s exit: force=%d, changed=%d", __func__, force, changed);
                    421:        return (force || changed);
1.1       nicm      422: }
                    423:
1.46      nicm      424: /* Set a status line message. */
1.117     nicm      425: void
1.210   ! nicm      426: status_message_set(struct client *c, int ignore_styles, const char *fmt, ...)
1.1       nicm      427: {
1.162     nicm      428:        struct timeval  tv;
                    429:        va_list         ap;
                    430:        int             delay;
1.1       nicm      431:
1.12      nicm      432:        status_message_clear(c);
1.189     nicm      433:        status_push_screen(c);
1.167     nicm      434:
1.28      nicm      435:        va_start(ap, fmt);
1.210   ! nicm      436:        c->message_ignore_styles = ignore_styles;
1.28      nicm      437:        xvasprintf(&c->message_string, fmt, ap);
                    438:        va_end(ap);
                    439:
1.209     nicm      440:        server_add_message("%s message: %s", c->name, c->message_string);
1.44      nicm      441:
1.137     nicm      442:        delay = options_get_number(c->session->options, "display-time");
1.143     tim       443:        if (delay > 0) {
                    444:                tv.tv_sec = delay / 1000;
                    445:                tv.tv_usec = (delay % 1000) * 1000L;
                    446:
                    447:                if (event_initialized(&c->message_timer))
                    448:                        evtimer_del(&c->message_timer);
                    449:                evtimer_set(&c->message_timer, status_message_callback, c);
                    450:                evtimer_add(&c->message_timer, &tv);
                    451:        }
1.1       nicm      452:
                    453:        c->tty.flags |= (TTY_NOCURSOR|TTY_FREEZE);
1.177     nicm      454:        c->flags |= CLIENT_REDRAWSTATUS;
1.1       nicm      455: }
                    456:
1.46      nicm      457: /* Clear status line message. */
1.1       nicm      458: void
                    459: status_message_clear(struct client *c)
                    460: {
                    461:        if (c->message_string == NULL)
                    462:                return;
                    463:
1.94      nicm      464:        free(c->message_string);
1.1       nicm      465:        c->message_string = NULL;
                    466:
1.156     nicm      467:        if (c->prompt_string == NULL)
                    468:                c->tty.flags &= ~(TTY_NOCURSOR|TTY_FREEZE);
1.177     nicm      469:        c->flags |= CLIENT_ALLREDRAWFLAGS; /* was frozen and may have changed */
1.7       nicm      470:
1.189     nicm      471:        status_pop_screen(c);
1.42      nicm      472: }
                    473:
1.46      nicm      474: /* Clear status line message after timer expires. */
1.151     nicm      475: static void
1.141     nicm      476: status_message_callback(__unused int fd, __unused short event, void *data)
1.42      nicm      477: {
                    478:        struct client   *c = data;
                    479:
                    480:        status_message_clear(c);
1.1       nicm      481: }
                    482:
                    483: /* Draw client message on status line of present else on last line. */
                    484: int
                    485: status_message_redraw(struct client *c)
                    486: {
1.189     nicm      487:        struct status_line      *sl = &c->status;
                    488:        struct screen_write_ctx  ctx;
                    489:        struct session          *s = c->session;
                    490:        struct screen            old_screen;
                    491:        size_t                   len;
                    492:        u_int                    lines, offset;
                    493:        struct grid_cell         gc;
1.207     nicm      494:        struct format_tree      *ft;
1.1       nicm      495:
                    496:        if (c->tty.sx == 0 || c->tty.sy == 0)
                    497:                return (0);
1.189     nicm      498:        memcpy(&old_screen, sl->active, sizeof old_screen);
1.169     nicm      499:
1.182     nicm      500:        lines = status_line_size(c);
1.189     nicm      501:        if (lines <= 1)
1.173     nicm      502:                lines = 1;
1.190     nicm      503:        screen_init(sl->active, c->tty.sx, lines, 0);
1.1       nicm      504:
1.139     nicm      505:        len = screen_write_strlen("%s", c->message_string);
1.1       nicm      506:        if (len > c->tty.sx)
                    507:                len = c->tty.sx;
                    508:
1.207     nicm      509:        ft = format_create_defaults(NULL, c, NULL, NULL, NULL);
                    510:        style_apply(&gc, s->options, "message-style", ft);
                    511:        format_free(ft);
1.1       nicm      512:
1.208     nicm      513:        screen_write_start(&ctx, sl->active);
1.192     nicm      514:        screen_write_fast_copy(&ctx, &sl->screen, 0, 0, c->tty.sx, lines - 1);
                    515:        screen_write_cursormove(&ctx, 0, lines - 1, 0);
                    516:        for (offset = 0; offset < c->tty.sx; offset++)
1.170     nicm      517:                screen_write_putc(&ctx, &gc, ' ');
1.184     nicm      518:        screen_write_cursormove(&ctx, 0, lines - 1, 0);
1.210   ! nicm      519:        if (c->message_ignore_styles)
        !           520:                screen_write_nputs(&ctx, len, &gc, "%s", c->message_string);
        !           521:        else
        !           522:                format_draw(&ctx, &gc, c->tty.sx, c->message_string, NULL);
1.1       nicm      523:        screen_write_stop(&ctx);
                    524:
1.189     nicm      525:        if (grid_compare(sl->active->grid, old_screen.grid) == 0) {
                    526:                screen_free(&old_screen);
1.1       nicm      527:                return (0);
                    528:        }
1.189     nicm      529:        screen_free(&old_screen);
1.1       nicm      530:        return (1);
                    531: }
                    532:
1.46      nicm      533: /* Enable status line prompt. */
1.1       nicm      534: void
1.76      nicm      535: status_prompt_set(struct client *c, const char *msg, const char *input,
1.166     nicm      536:     prompt_input_cb inputcb, prompt_free_cb freecb, void *data, int flags)
1.1       nicm      537: {
1.122     nicm      538:        struct format_tree      *ft;
1.165     nicm      539:        char                    *tmp, *cp;
1.122     nicm      540:
1.164     nicm      541:        ft = format_create(c, NULL, FORMAT_NONE, 0);
1.122     nicm      542:        format_defaults(ft, c, NULL, NULL, NULL);
1.152     nicm      543:
1.168     nicm      544:        if (input == NULL)
                    545:                input = "";
                    546:        if (flags & PROMPT_NOFORMAT)
                    547:                tmp = xstrdup(input);
                    548:        else
1.185     nicm      549:                tmp = format_expand_time(ft, input);
1.20      nicm      550:
1.12      nicm      551:        status_message_clear(c);
                    552:        status_prompt_clear(c);
1.189     nicm      553:        status_push_screen(c);
1.12      nicm      554:
1.185     nicm      555:        c->prompt_string = format_expand_time(ft, msg);
1.1       nicm      556:
1.152     nicm      557:        c->prompt_buffer = utf8_fromcstr(tmp);
                    558:        c->prompt_index = utf8_strlen(c->prompt_buffer);
1.1       nicm      559:
1.166     nicm      560:        c->prompt_inputcb = inputcb;
                    561:        c->prompt_freecb = freecb;
1.1       nicm      562:        c->prompt_data = data;
                    563:
                    564:        c->prompt_hindex = 0;
                    565:
                    566:        c->prompt_flags = flags;
1.155     nicm      567:        c->prompt_mode = PROMPT_ENTRY;
1.1       nicm      568:
1.158     nicm      569:        if (~flags & PROMPT_INCREMENTAL)
                    570:                c->tty.flags |= (TTY_NOCURSOR|TTY_FREEZE);
1.177     nicm      571:        c->flags |= CLIENT_REDRAWSTATUS;
1.165     nicm      572:
                    573:        if ((flags & PROMPT_INCREMENTAL) && *tmp != '\0') {
                    574:                xasprintf(&cp, "=%s", tmp);
1.166     nicm      575:                c->prompt_inputcb(c, c->prompt_data, cp, 0);
1.165     nicm      576:                free(cp);
                    577:        }
1.122     nicm      578:
1.152     nicm      579:        free(tmp);
1.122     nicm      580:        format_free(ft);
1.1       nicm      581: }
                    582:
1.46      nicm      583: /* Remove status line prompt. */
1.1       nicm      584: void
                    585: status_prompt_clear(struct client *c)
                    586: {
1.55      nicm      587:        if (c->prompt_string == NULL)
1.1       nicm      588:                return;
                    589:
1.166     nicm      590:        if (c->prompt_freecb != NULL && c->prompt_data != NULL)
                    591:                c->prompt_freecb(c->prompt_data);
1.1       nicm      592:
1.94      nicm      593:        free(c->prompt_string);
1.1       nicm      594:        c->prompt_string = NULL;
                    595:
1.94      nicm      596:        free(c->prompt_buffer);
1.1       nicm      597:        c->prompt_buffer = NULL;
                    598:
1.181     nicm      599:        free(c->prompt_saved);
                    600:        c->prompt_saved = NULL;
                    601:
1.1       nicm      602:        c->tty.flags &= ~(TTY_NOCURSOR|TTY_FREEZE);
1.177     nicm      603:        c->flags |= CLIENT_ALLREDRAWFLAGS; /* was frozen and may have changed */
1.7       nicm      604:
1.189     nicm      605:        status_pop_screen(c);
1.27      nicm      606: }
                    607:
1.46      nicm      608: /* Update status line prompt with a new prompt string. */
1.27      nicm      609: void
1.76      nicm      610: status_prompt_update(struct client *c, const char *msg, const char *input)
1.27      nicm      611: {
1.122     nicm      612:        struct format_tree      *ft;
1.152     nicm      613:        char                    *tmp;
1.122     nicm      614:
1.164     nicm      615:        ft = format_create(c, NULL, FORMAT_NONE, 0);
1.122     nicm      616:        format_defaults(ft, c, NULL, NULL, NULL);
1.152     nicm      617:
1.185     nicm      618:        tmp = format_expand_time(ft, input);
1.122     nicm      619:
1.94      nicm      620:        free(c->prompt_string);
1.185     nicm      621:        c->prompt_string = format_expand_time(ft, msg);
1.27      nicm      622:
1.94      nicm      623:        free(c->prompt_buffer);
1.152     nicm      624:        c->prompt_buffer = utf8_fromcstr(tmp);
                    625:        c->prompt_index = utf8_strlen(c->prompt_buffer);
1.27      nicm      626:
                    627:        c->prompt_hindex = 0;
                    628:
1.177     nicm      629:        c->flags |= CLIENT_REDRAWSTATUS;
1.122     nicm      630:
1.152     nicm      631:        free(tmp);
1.122     nicm      632:        format_free(ft);
1.1       nicm      633: }
                    634:
                    635: /* Draw client prompt on status line of present else on last line. */
                    636: int
                    637: status_prompt_redraw(struct client *c)
                    638: {
1.189     nicm      639:        struct status_line      *sl = &c->status;
1.152     nicm      640:        struct screen_write_ctx  ctx;
                    641:        struct session          *s = c->session;
1.189     nicm      642:        struct screen            old_screen;
                    643:        u_int                    i, lines, offset, left, start, width;
                    644:        u_int                    pcursor, pwidth;
1.152     nicm      645:        struct grid_cell         gc, cursorgc;
1.207     nicm      646:        struct format_tree      *ft;
1.1       nicm      647:
                    648:        if (c->tty.sx == 0 || c->tty.sy == 0)
                    649:                return (0);
1.189     nicm      650:        memcpy(&old_screen, sl->active, sizeof old_screen);
1.169     nicm      651:
1.182     nicm      652:        lines = status_line_size(c);
1.189     nicm      653:        if (lines <= 1)
1.173     nicm      654:                lines = 1;
1.189     nicm      655:        screen_init(sl->active, c->tty.sx, lines, 0);
1.1       nicm      656:
1.207     nicm      657:        ft = format_create_defaults(NULL, c, NULL, NULL, NULL);
1.155     nicm      658:        if (c->prompt_mode == PROMPT_COMMAND)
1.207     nicm      659:                style_apply(&gc, s->options, "message-command-style", ft);
1.108     nicm      660:        else
1.207     nicm      661:                style_apply(&gc, s->options, "message-style", ft);
                    662:        format_free(ft);
1.1       nicm      663:
1.152     nicm      664:        memcpy(&cursorgc, &gc, sizeof cursorgc);
                    665:        cursorgc.attr ^= GRID_ATTR_REVERSE;
                    666:
                    667:        start = screen_write_strlen("%s", c->prompt_string);
                    668:        if (start > c->tty.sx)
                    669:                start = c->tty.sx;
                    670:
1.208     nicm      671:        screen_write_start(&ctx, sl->active);
1.192     nicm      672:        screen_write_fast_copy(&ctx, &sl->screen, 0, 0, c->tty.sx, lines - 1);
                    673:        screen_write_cursormove(&ctx, 0, lines - 1, 0);
                    674:        for (offset = 0; offset < c->tty.sx; offset++)
1.170     nicm      675:                screen_write_putc(&ctx, &gc, ' ');
1.192     nicm      676:        screen_write_cursormove(&ctx, 0, lines - 1, 0);
1.152     nicm      677:        screen_write_nputs(&ctx, start, &gc, "%s", c->prompt_string);
1.192     nicm      678:        screen_write_cursormove(&ctx, start, lines - 1, 0);
1.1       nicm      679:
1.152     nicm      680:        left = c->tty.sx - start;
                    681:        if (left == 0)
                    682:                goto finished;
                    683:
                    684:        pcursor = utf8_strwidth(c->prompt_buffer, c->prompt_index);
                    685:        pwidth = utf8_strwidth(c->prompt_buffer, -1);
                    686:        if (pcursor >= left) {
                    687:                /*
                    688:                 * The cursor would be outside the screen so start drawing
                    689:                 * with it on the right.
                    690:                 */
                    691:                offset = (pcursor - left) + 1;
                    692:                pwidth = left;
                    693:        } else
                    694:                offset = 0;
                    695:        if (pwidth > left)
                    696:                pwidth = left;
                    697:
                    698:        width = 0;
                    699:        for (i = 0; c->prompt_buffer[i].size != 0; i++) {
                    700:                if (width < offset) {
                    701:                        width += c->prompt_buffer[i].width;
                    702:                        continue;
1.1       nicm      703:                }
1.152     nicm      704:                if (width >= offset + pwidth)
                    705:                        break;
                    706:                width += c->prompt_buffer[i].width;
                    707:                if (width > offset + pwidth)
                    708:                        break;
1.1       nicm      709:
1.152     nicm      710:                if (i != c->prompt_index) {
                    711:                        utf8_copy(&gc.data, &c->prompt_buffer[i]);
                    712:                        screen_write_cell(&ctx, &gc);
                    713:                } else {
                    714:                        utf8_copy(&cursorgc.data, &c->prompt_buffer[i]);
                    715:                        screen_write_cell(&ctx, &cursorgc);
                    716:                }
1.1       nicm      717:        }
1.189     nicm      718:        if (sl->active->cx < screen_size_x(sl->active) && c->prompt_index >= i)
1.152     nicm      719:                screen_write_putc(&ctx, &cursorgc, ' ');
1.1       nicm      720:
1.152     nicm      721: finished:
1.1       nicm      722:        screen_write_stop(&ctx);
1.51      nicm      723:
1.189     nicm      724:        if (grid_compare(sl->active->grid, old_screen.grid) == 0) {
                    725:                screen_free(&old_screen);
1.1       nicm      726:                return (0);
                    727:        }
1.189     nicm      728:        screen_free(&old_screen);
1.1       nicm      729:        return (1);
                    730: }
                    731:
1.152     nicm      732: /* Is this a separator? */
                    733: static int
                    734: status_prompt_in_list(const char *ws, const struct utf8_data *ud)
                    735: {
                    736:        if (ud->size != 1 || ud->width != 1)
                    737:                return (0);
                    738:        return (strchr(ws, *ud->data) != NULL);
                    739: }
                    740:
                    741: /* Is this a space? */
                    742: static int
                    743: status_prompt_space(const struct utf8_data *ud)
                    744: {
                    745:        if (ud->size != 1 || ud->width != 1)
                    746:                return (0);
                    747:        return (*ud->data == ' ');
                    748: }
                    749:
1.155     nicm      750: /*
                    751:  * Translate key from emacs to vi. Return 0 to drop key, 1 to process the key
                    752:  * as an emacs key; return 2 to append to the buffer.
                    753:  */
                    754: static int
                    755: status_prompt_translate_key(struct client *c, key_code key, key_code *new_key)
                    756: {
                    757:        if (c->prompt_mode == PROMPT_ENTRY) {
                    758:                switch (key) {
                    759:                case '\003': /* C-c */
1.202     nicm      760:                case '\007': /* C-g */
1.155     nicm      761:                case '\010': /* C-h */
                    762:                case '\011': /* Tab */
                    763:                case '\025': /* C-u */
                    764:                case '\027': /* C-w */
                    765:                case '\n':
                    766:                case '\r':
                    767:                case KEYC_BSPACE:
                    768:                case KEYC_DC:
                    769:                case KEYC_DOWN:
                    770:                case KEYC_END:
                    771:                case KEYC_HOME:
                    772:                case KEYC_LEFT:
                    773:                case KEYC_RIGHT:
                    774:                case KEYC_UP:
                    775:                        *new_key = key;
                    776:                        return (1);
                    777:                case '\033': /* Escape */
                    778:                        c->prompt_mode = PROMPT_COMMAND;
1.177     nicm      779:                        c->flags |= CLIENT_REDRAWSTATUS;
1.155     nicm      780:                        return (0);
                    781:                }
                    782:                *new_key = key;
                    783:                return (2);
                    784:        }
                    785:
                    786:        switch (key) {
                    787:        case 'A':
                    788:        case 'I':
                    789:        case 'C':
                    790:        case 's':
                    791:        case 'a':
                    792:                c->prompt_mode = PROMPT_ENTRY;
1.177     nicm      793:                c->flags |= CLIENT_REDRAWSTATUS;
1.155     nicm      794:                break; /* switch mode and... */
                    795:        case 'S':
                    796:                c->prompt_mode = PROMPT_ENTRY;
1.177     nicm      797:                c->flags |= CLIENT_REDRAWSTATUS;
1.155     nicm      798:                *new_key = '\025'; /* C-u */
                    799:                return (1);
                    800:        case 'i':
                    801:        case '\033': /* Escape */
                    802:                c->prompt_mode = PROMPT_ENTRY;
1.177     nicm      803:                c->flags |= CLIENT_REDRAWSTATUS;
1.155     nicm      804:                return (0);
                    805:        }
                    806:
                    807:        switch (key) {
                    808:        case 'A':
                    809:        case '$':
                    810:                *new_key = KEYC_END;
                    811:                return (1);
                    812:        case 'I':
                    813:        case '0':
                    814:        case '^':
                    815:                *new_key = KEYC_HOME;
                    816:                return (1);
                    817:        case 'C':
                    818:        case 'D':
                    819:                *new_key = '\013'; /* C-k */
                    820:                return (1);
                    821:        case KEYC_BSPACE:
                    822:        case 'X':
                    823:                *new_key = KEYC_BSPACE;
                    824:                return (1);
                    825:        case 'b':
                    826:        case 'B':
                    827:                *new_key = 'b'|KEYC_ESCAPE;
                    828:                return (1);
                    829:        case 'd':
                    830:                *new_key = '\025';
                    831:                return (1);
                    832:        case 'e':
                    833:        case 'E':
                    834:        case 'w':
                    835:        case 'W':
                    836:                *new_key = 'f'|KEYC_ESCAPE;
                    837:                return (1);
                    838:        case 'p':
                    839:                *new_key = '\031'; /* C-y */
1.202     nicm      840:                return (1);
                    841:        case 'q':
                    842:                *new_key = '\003'; /* C-c */
1.155     nicm      843:                return (1);
                    844:        case 's':
                    845:        case KEYC_DC:
                    846:        case 'x':
                    847:                *new_key = KEYC_DC;
                    848:                return (1);
                    849:        case KEYC_DOWN:
                    850:        case 'j':
                    851:                *new_key = KEYC_DOWN;
                    852:                return (1);
                    853:        case KEYC_LEFT:
                    854:        case 'h':
                    855:                *new_key = KEYC_LEFT;
                    856:                return (1);
                    857:        case 'a':
                    858:        case KEYC_RIGHT:
                    859:        case 'l':
                    860:                *new_key = KEYC_RIGHT;
                    861:                return (1);
                    862:        case KEYC_UP:
                    863:        case 'k':
                    864:                *new_key = KEYC_UP;
                    865:                return (1);
                    866:        case '\010' /* C-h */:
                    867:        case '\003' /* C-c */:
                    868:        case '\n':
                    869:        case '\r':
                    870:                return (1);
                    871:        }
                    872:        return (0);
                    873: }
                    874:
1.199     nicm      875: /* Paste into prompt. */
                    876: static int
                    877: status_prompt_paste(struct client *c)
                    878: {
                    879:        struct paste_buffer     *pb;
                    880:        const char              *bufdata;
                    881:        size_t                   size, n, bufsize;
                    882:        u_int                    i;
                    883:        struct utf8_data        *ud, *udp;
                    884:        enum utf8_state          more;
                    885:
                    886:        size = utf8_strlen(c->prompt_buffer);
                    887:        if (c->prompt_saved != NULL) {
                    888:                ud = c->prompt_saved;
                    889:                n = utf8_strlen(c->prompt_saved);
                    890:        } else {
                    891:                if ((pb = paste_get_top(NULL)) == NULL)
                    892:                        return (0);
                    893:                bufdata = paste_buffer_data(pb, &bufsize);
1.200     nicm      894:                ud = xreallocarray(NULL, bufsize + 1, sizeof *ud);
1.199     nicm      895:                udp = ud;
                    896:                for (i = 0; i != bufsize; /* nothing */) {
                    897:                        more = utf8_open(udp, bufdata[i]);
                    898:                        if (more == UTF8_MORE) {
                    899:                                while (++i != bufsize && more == UTF8_MORE)
                    900:                                        more = utf8_append(udp, bufdata[i]);
                    901:                                if (more == UTF8_DONE) {
                    902:                                        udp++;
                    903:                                        continue;
                    904:                                }
                    905:                                i -= udp->have;
                    906:                        }
                    907:                        if (bufdata[i] <= 31 || bufdata[i] >= 127)
                    908:                                break;
                    909:                        utf8_set(udp, bufdata[i]);
                    910:                        udp++;
                    911:                        i++;
                    912:                }
                    913:                udp->size = 0;
                    914:                n = udp - ud;
                    915:        }
                    916:        if (n == 0)
                    917:                return (0);
                    918:
                    919:        c->prompt_buffer = xreallocarray(c->prompt_buffer, size + n + 1,
                    920:            sizeof *c->prompt_buffer);
                    921:        if (c->prompt_index == size) {
                    922:                memcpy(c->prompt_buffer + c->prompt_index, ud,
                    923:                    n * sizeof *c->prompt_buffer);
                    924:                c->prompt_index += n;
                    925:                c->prompt_buffer[c->prompt_index].size = 0;
                    926:        } else {
                    927:                memmove(c->prompt_buffer + c->prompt_index + n,
                    928:                    c->prompt_buffer + c->prompt_index,
                    929:                    (size + 1 - c->prompt_index) * sizeof *c->prompt_buffer);
                    930:                memcpy(c->prompt_buffer + c->prompt_index, ud,
                    931:                    n * sizeof *c->prompt_buffer);
                    932:                c->prompt_index += n;
                    933:        }
                    934:
                    935:        if (ud != c->prompt_saved)
                    936:                free(ud);
                    937:        return (1);
                    938: }
                    939:
1.204     nicm      940: /* Finish completion. */
                    941: static int
                    942: status_prompt_replace_complete(struct client *c, const char *s)
                    943: {
                    944:        char                     word[64], *allocated = NULL;
                    945:        size_t                   size, n, off, idx, used;
                    946:        struct utf8_data        *first, *last, *ud;
                    947:
1.205     nicm      948:        /* Work out where the cursor currently is. */
1.204     nicm      949:        idx = c->prompt_index;
                    950:        if (idx != 0)
                    951:                idx--;
1.205     nicm      952:        size = utf8_strlen(c->prompt_buffer);
1.204     nicm      953:
                    954:        /* Find the word we are in. */
                    955:        first = &c->prompt_buffer[idx];
                    956:        while (first > c->prompt_buffer && !status_prompt_space(first))
                    957:                first--;
                    958:        while (first->size != 0 && status_prompt_space(first))
                    959:                first++;
                    960:        last = &c->prompt_buffer[idx];
                    961:        while (last->size != 0 && !status_prompt_space(last))
                    962:                last++;
                    963:        while (last > c->prompt_buffer && status_prompt_space(last))
                    964:                last--;
                    965:        if (last->size != 0)
                    966:                last++;
1.205     nicm      967:        if (last < first)
1.204     nicm      968:                return (0);
                    969:        if (s == NULL) {
                    970:                used = 0;
                    971:                for (ud = first; ud < last; ud++) {
                    972:                        if (used + ud->size >= sizeof word)
                    973:                                break;
                    974:                        memcpy(word + used, ud->data, ud->size);
                    975:                        used += ud->size;
                    976:                }
                    977:                if (ud != last)
                    978:                        return (0);
                    979:                word[used] = '\0';
                    980:        }
                    981:
                    982:        /* Try to complete it. */
                    983:        if (s == NULL) {
                    984:                allocated = status_prompt_complete(c, word,
                    985:                    first - c->prompt_buffer);
                    986:                if (allocated == NULL)
                    987:                        return (0);
                    988:                s = allocated;
                    989:        }
                    990:
                    991:        /* Trim out word. */
                    992:        n = size - (last - c->prompt_buffer) + 1; /* with \0 */
                    993:        memmove(first, last, n * sizeof *c->prompt_buffer);
                    994:        size -= last - first;
                    995:
                    996:        /* Insert the new word. */
                    997:        size += strlen(s);
                    998:        off = first - c->prompt_buffer;
                    999:        c->prompt_buffer = xreallocarray(c->prompt_buffer, size + 1,
                   1000:            sizeof *c->prompt_buffer);
                   1001:        first = c->prompt_buffer + off;
                   1002:        memmove(first + strlen(s), first, n * sizeof *c->prompt_buffer);
                   1003:        for (idx = 0; idx < strlen(s); idx++)
                   1004:                utf8_set(&first[idx], s[idx]);
                   1005:        c->prompt_index = (first - c->prompt_buffer) + strlen(s);
                   1006:
                   1007:        free(allocated);
                   1008:        return (1);
                   1009: }
                   1010:
1.1       nicm     1011: /* Handle keys in prompt. */
1.154     nicm     1012: int
1.138     nicm     1013: status_prompt_key(struct client *c, key_code key)
1.1       nicm     1014: {
1.152     nicm     1015:        struct options          *oo = c->session->options;
1.204     nicm     1016:        char                    *s, *cp, prefix = '=';
1.201     nicm     1017:        const char              *histstr, *ws = NULL, *keystring;
1.204     nicm     1018:        size_t                   size, idx;
                   1019:        struct utf8_data         tmp;
1.155     nicm     1020:        int                      keys;
1.1       nicm     1021:
1.201     nicm     1022:        if (c->prompt_flags & PROMPT_KEY) {
                   1023:                keystring = key_string_lookup_key(key);
                   1024:                c->prompt_inputcb(c, c->prompt_data, keystring, 1);
                   1025:                status_prompt_clear(c);
                   1026:                return (0);
                   1027:        }
1.152     nicm     1028:        size = utf8_strlen(c->prompt_buffer);
1.154     nicm     1029:
                   1030:        if (c->prompt_flags & PROMPT_NUMERIC) {
                   1031:                if (key >= '0' && key <= '9')
                   1032:                        goto append_key;
                   1033:                s = utf8_tocstr(c->prompt_buffer);
1.166     nicm     1034:                c->prompt_inputcb(c, c->prompt_data, s, 1);
1.154     nicm     1035:                status_prompt_clear(c);
                   1036:                free(s);
                   1037:                return (1);
                   1038:        }
1.180     nicm     1039:        key &= ~KEYC_XTERM;
1.154     nicm     1040:
1.155     nicm     1041:        keys = options_get_number(c->session->options, "status-keys");
                   1042:        if (keys == MODEKEY_VI) {
                   1043:                switch (status_prompt_translate_key(c, key, &key)) {
                   1044:                case 1:
                   1045:                        goto process_key;
                   1046:                case 2:
                   1047:                        goto append_key;
                   1048:                default:
                   1049:                        return (0);
                   1050:                }
                   1051:        }
                   1052:
                   1053: process_key:
                   1054:        switch (key) {
                   1055:        case KEYC_LEFT:
                   1056:        case '\002': /* C-b */
1.1       nicm     1057:                if (c->prompt_index > 0) {
                   1058:                        c->prompt_index--;
1.158     nicm     1059:                        break;
1.1       nicm     1060:                }
                   1061:                break;
1.155     nicm     1062:        case KEYC_RIGHT:
                   1063:        case '\006': /* C-f */
1.1       nicm     1064:                if (c->prompt_index < size) {
                   1065:                        c->prompt_index++;
1.158     nicm     1066:                        break;
1.1       nicm     1067:                }
                   1068:                break;
1.155     nicm     1069:        case KEYC_HOME:
                   1070:        case '\001': /* C-a */
1.1       nicm     1071:                if (c->prompt_index != 0) {
                   1072:                        c->prompt_index = 0;
1.158     nicm     1073:                        break;
1.1       nicm     1074:                }
                   1075:                break;
1.155     nicm     1076:        case KEYC_END:
                   1077:        case '\005': /* C-e */
1.1       nicm     1078:                if (c->prompt_index != size) {
                   1079:                        c->prompt_index = size;
1.158     nicm     1080:                        break;
1.1       nicm     1081:                }
                   1082:                break;
1.155     nicm     1083:        case '\011': /* Tab */
1.206     nicm     1084:                if (status_prompt_replace_complete(c, NULL))
1.204     nicm     1085:                        goto changed;
                   1086:                break;
1.155     nicm     1087:        case KEYC_BSPACE:
                   1088:        case '\010': /* C-h */
1.1       nicm     1089:                if (c->prompt_index != 0) {
                   1090:                        if (c->prompt_index == size)
1.152     nicm     1091:                                c->prompt_buffer[--c->prompt_index].size = 0;
1.1       nicm     1092:                        else {
                   1093:                                memmove(c->prompt_buffer + c->prompt_index - 1,
                   1094:                                    c->prompt_buffer + c->prompt_index,
1.152     nicm     1095:                                    (size + 1 - c->prompt_index) *
                   1096:                                    sizeof *c->prompt_buffer);
1.1       nicm     1097:                                c->prompt_index--;
                   1098:                        }
1.158     nicm     1099:                        goto changed;
1.1       nicm     1100:                }
                   1101:                break;
1.155     nicm     1102:        case KEYC_DC:
                   1103:        case '\004': /* C-d */
1.1       nicm     1104:                if (c->prompt_index != size) {
                   1105:                        memmove(c->prompt_buffer + c->prompt_index,
                   1106:                            c->prompt_buffer + c->prompt_index + 1,
1.152     nicm     1107:                            (size + 1 - c->prompt_index) *
                   1108:                            sizeof *c->prompt_buffer);
1.158     nicm     1109:                        goto changed;
1.18      nicm     1110:                }
1.26      nicm     1111:                break;
1.155     nicm     1112:        case '\025': /* C-u */
1.152     nicm     1113:                c->prompt_buffer[0].size = 0;
1.26      nicm     1114:                c->prompt_index = 0;
1.158     nicm     1115:                goto changed;
1.155     nicm     1116:        case '\013': /* C-k */
1.18      nicm     1117:                if (c->prompt_index < size) {
1.152     nicm     1118:                        c->prompt_buffer[c->prompt_index].size = 0;
1.158     nicm     1119:                        goto changed;
1.1       nicm     1120:                }
                   1121:                break;
1.155     nicm     1122:        case '\027': /* C-w */
1.152     nicm     1123:                ws = options_get_string(oo, "word-separators");
1.81      nicm     1124:                idx = c->prompt_index;
                   1125:
                   1126:                /* Find a non-separator. */
                   1127:                while (idx != 0) {
                   1128:                        idx--;
1.152     nicm     1129:                        if (!status_prompt_in_list(ws, &c->prompt_buffer[idx]))
1.81      nicm     1130:                                break;
                   1131:                }
                   1132:
                   1133:                /* Find the separator at the beginning of the word. */
                   1134:                while (idx != 0) {
                   1135:                        idx--;
1.152     nicm     1136:                        if (status_prompt_in_list(ws, &c->prompt_buffer[idx])) {
1.81      nicm     1137:                                /* Go back to the word. */
                   1138:                                idx++;
                   1139:                                break;
                   1140:                        }
                   1141:                }
                   1142:
1.181     nicm     1143:                free(c->prompt_saved);
                   1144:                c->prompt_saved = xcalloc(sizeof *c->prompt_buffer,
                   1145:                    (c->prompt_index - idx) + 1);
                   1146:                memcpy(c->prompt_saved, c->prompt_buffer + idx,
                   1147:                    (c->prompt_index - idx) * sizeof *c->prompt_buffer);
                   1148:
1.81      nicm     1149:                memmove(c->prompt_buffer + idx,
                   1150:                    c->prompt_buffer + c->prompt_index,
1.152     nicm     1151:                    (size + 1 - c->prompt_index) *
                   1152:                    sizeof *c->prompt_buffer);
1.81      nicm     1153:                memset(c->prompt_buffer + size - (c->prompt_index - idx),
1.152     nicm     1154:                    '\0', (c->prompt_index - idx) * sizeof *c->prompt_buffer);
1.81      nicm     1155:                c->prompt_index = idx;
1.152     nicm     1156:
1.158     nicm     1157:                goto changed;
1.155     nicm     1158:        case 'f'|KEYC_ESCAPE:
1.180     nicm     1159:        case KEYC_RIGHT|KEYC_CTRL:
1.155     nicm     1160:                ws = options_get_string(oo, "word-separators");
1.81      nicm     1161:
                   1162:                /* Find a word. */
                   1163:                while (c->prompt_index != size) {
1.152     nicm     1164:                        idx = ++c->prompt_index;
                   1165:                        if (!status_prompt_in_list(ws, &c->prompt_buffer[idx]))
1.81      nicm     1166:                                break;
                   1167:                }
                   1168:
                   1169:                /* Find the separator at the end of the word. */
                   1170:                while (c->prompt_index != size) {
1.152     nicm     1171:                        idx = ++c->prompt_index;
                   1172:                        if (status_prompt_in_list(ws, &c->prompt_buffer[idx]))
1.81      nicm     1173:                                break;
                   1174:                }
1.106     nicm     1175:
                   1176:                /* Back up to the end-of-word like vi. */
                   1177:                if (options_get_number(oo, "status-keys") == MODEKEY_VI &&
                   1178:                    c->prompt_index != 0)
                   1179:                        c->prompt_index--;
1.81      nicm     1180:
1.158     nicm     1181:                goto changed;
1.155     nicm     1182:        case 'b'|KEYC_ESCAPE:
1.180     nicm     1183:        case KEYC_LEFT|KEYC_CTRL:
1.155     nicm     1184:                ws = options_get_string(oo, "word-separators");
1.81      nicm     1185:
                   1186:                /* Find a non-separator. */
                   1187:                while (c->prompt_index != 0) {
1.152     nicm     1188:                        idx = --c->prompt_index;
                   1189:                        if (!status_prompt_in_list(ws, &c->prompt_buffer[idx]))
1.81      nicm     1190:                                break;
                   1191:                }
                   1192:
                   1193:                /* Find the separator at the beginning of the word. */
                   1194:                while (c->prompt_index != 0) {
1.152     nicm     1195:                        idx = --c->prompt_index;
                   1196:                        if (status_prompt_in_list(ws, &c->prompt_buffer[idx])) {
1.81      nicm     1197:                                /* Go back to the word. */
                   1198:                                c->prompt_index++;
                   1199:                                break;
                   1200:                        }
                   1201:                }
1.158     nicm     1202:                goto changed;
1.155     nicm     1203:        case KEYC_UP:
                   1204:        case '\020': /* C-p */
1.66      nicm     1205:                histstr = status_prompt_up_history(&c->prompt_hindex);
                   1206:                if (histstr == NULL)
1.1       nicm     1207:                        break;
1.94      nicm     1208:                free(c->prompt_buffer);
1.152     nicm     1209:                c->prompt_buffer = utf8_fromcstr(histstr);
                   1210:                c->prompt_index = utf8_strlen(c->prompt_buffer);
1.158     nicm     1211:                goto changed;
1.155     nicm     1212:        case KEYC_DOWN:
                   1213:        case '\016': /* C-n */
1.66      nicm     1214:                histstr = status_prompt_down_history(&c->prompt_hindex);
                   1215:                if (histstr == NULL)
1.65      nicm     1216:                        break;
1.94      nicm     1217:                free(c->prompt_buffer);
1.152     nicm     1218:                c->prompt_buffer = utf8_fromcstr(histstr);
                   1219:                c->prompt_index = utf8_strlen(c->prompt_buffer);
1.158     nicm     1220:                goto changed;
1.155     nicm     1221:        case '\031': /* C-y */
1.199     nicm     1222:                if (status_prompt_paste(c))
                   1223:                        goto changed;
                   1224:                break;
1.155     nicm     1225:        case '\024': /* C-t */
1.30      nicm     1226:                idx = c->prompt_index;
                   1227:                if (idx < size)
                   1228:                        idx++;
                   1229:                if (idx >= 2) {
1.152     nicm     1230:                        utf8_copy(&tmp, &c->prompt_buffer[idx - 2]);
                   1231:                        utf8_copy(&c->prompt_buffer[idx - 2],
                   1232:                            &c->prompt_buffer[idx - 1]);
                   1233:                        utf8_copy(&c->prompt_buffer[idx - 1], &tmp);
1.30      nicm     1234:                        c->prompt_index = idx;
1.158     nicm     1235:                        goto changed;
1.30      nicm     1236:                }
1.1       nicm     1237:                break;
1.155     nicm     1238:        case '\r':
                   1239:        case '\n':
1.152     nicm     1240:                s = utf8_tocstr(c->prompt_buffer);
                   1241:                if (*s != '\0')
                   1242:                        status_prompt_add_history(s);
1.166     nicm     1243:                if (c->prompt_inputcb(c, c->prompt_data, s, 1) == 0)
1.25      nicm     1244:                        status_prompt_clear(c);
1.152     nicm     1245:                free(s);
1.25      nicm     1246:                break;
1.155     nicm     1247:        case '\033': /* Escape */
                   1248:        case '\003': /* C-c */
1.174     nicm     1249:        case '\007': /* C-g */
1.166     nicm     1250:                if (c->prompt_inputcb(c, c->prompt_data, NULL, 1) == 0)
1.1       nicm     1251:                        status_prompt_clear(c);
                   1252:                break;
1.158     nicm     1253:        case '\022': /* C-r */
                   1254:                if (c->prompt_flags & PROMPT_INCREMENTAL) {
                   1255:                        prefix = '-';
                   1256:                        goto changed;
                   1257:                }
                   1258:                break;
                   1259:        case '\023': /* C-s */
                   1260:                if (c->prompt_flags & PROMPT_INCREMENTAL) {
                   1261:                        prefix = '+';
                   1262:                        goto changed;
                   1263:                }
                   1264:                break;
                   1265:        default:
                   1266:                goto append_key;
1.154     nicm     1267:        }
1.152     nicm     1268:
1.177     nicm     1269:        c->flags |= CLIENT_REDRAWSTATUS;
1.158     nicm     1270:        return (0);
                   1271:
1.154     nicm     1272: append_key:
                   1273:        if (key <= 0x1f || key >= KEYC_BASE)
                   1274:                return (0);
                   1275:        if (utf8_split(key, &tmp) != UTF8_DONE)
                   1276:                return (0);
1.1       nicm     1277:
1.154     nicm     1278:        c->prompt_buffer = xreallocarray(c->prompt_buffer, size + 2,
                   1279:            sizeof *c->prompt_buffer);
1.1       nicm     1280:
1.154     nicm     1281:        if (c->prompt_index == size) {
                   1282:                utf8_copy(&c->prompt_buffer[c->prompt_index], &tmp);
                   1283:                c->prompt_index++;
                   1284:                c->prompt_buffer[c->prompt_index].size = 0;
                   1285:        } else {
                   1286:                memmove(c->prompt_buffer + c->prompt_index + 1,
                   1287:                    c->prompt_buffer + c->prompt_index,
                   1288:                    (size + 1 - c->prompt_index) *
                   1289:                    sizeof *c->prompt_buffer);
                   1290:                utf8_copy(&c->prompt_buffer[c->prompt_index], &tmp);
                   1291:                c->prompt_index++;
                   1292:        }
1.1       nicm     1293:
1.154     nicm     1294:        if (c->prompt_flags & PROMPT_SINGLE) {
                   1295:                s = utf8_tocstr(c->prompt_buffer);
                   1296:                if (strlen(s) != 1)
                   1297:                        status_prompt_clear(c);
1.166     nicm     1298:                else if (c->prompt_inputcb(c, c->prompt_data, s, 1) == 0)
1.154     nicm     1299:                        status_prompt_clear(c);
                   1300:                free(s);
1.1       nicm     1301:        }
1.154     nicm     1302:
1.158     nicm     1303: changed:
1.177     nicm     1304:        c->flags |= CLIENT_REDRAWSTATUS;
1.158     nicm     1305:        if (c->prompt_flags & PROMPT_INCREMENTAL) {
                   1306:                s = utf8_tocstr(c->prompt_buffer);
                   1307:                xasprintf(&cp, "%c%s", prefix, s);
1.166     nicm     1308:                c->prompt_inputcb(c, c->prompt_data, cp, 0);
1.158     nicm     1309:                free(cp);
                   1310:                free(s);
                   1311:        }
1.154     nicm     1312:        return (0);
1.1       nicm     1313: }
                   1314:
1.65      nicm     1315: /* Get previous line from the history. */
1.151     nicm     1316: static const char *
1.65      nicm     1317: status_prompt_up_history(u_int *idx)
                   1318: {
                   1319:        /*
1.128     nicm     1320:         * History runs from 0 to size - 1. Index is from 0 to size. Zero is
                   1321:         * empty.
1.65      nicm     1322:         */
                   1323:
1.128     nicm     1324:        if (status_prompt_hsize == 0 || *idx == status_prompt_hsize)
1.65      nicm     1325:                return (NULL);
                   1326:        (*idx)++;
1.128     nicm     1327:        return (status_prompt_hlist[status_prompt_hsize - *idx]);
1.65      nicm     1328: }
                   1329:
                   1330: /* Get next line from the history. */
1.151     nicm     1331: static const char *
1.65      nicm     1332: status_prompt_down_history(u_int *idx)
                   1333: {
1.128     nicm     1334:        if (status_prompt_hsize == 0 || *idx == 0)
1.65      nicm     1335:                return ("");
                   1336:        (*idx)--;
                   1337:        if (*idx == 0)
                   1338:                return ("");
1.128     nicm     1339:        return (status_prompt_hlist[status_prompt_hsize - *idx]);
1.65      nicm     1340: }
                   1341:
1.1       nicm     1342: /* Add line to the history. */
1.151     nicm     1343: static void
1.65      nicm     1344: status_prompt_add_history(const char *line)
1.1       nicm     1345: {
1.128     nicm     1346:        size_t  size;
1.65      nicm     1347:
1.128     nicm     1348:        if (status_prompt_hsize > 0 &&
                   1349:            strcmp(status_prompt_hlist[status_prompt_hsize - 1], line) == 0)
1.1       nicm     1350:                return;
                   1351:
1.128     nicm     1352:        if (status_prompt_hsize == PROMPT_HISTORY) {
                   1353:                free(status_prompt_hlist[0]);
1.1       nicm     1354:
1.128     nicm     1355:                size = (PROMPT_HISTORY - 1) * sizeof *status_prompt_hlist;
                   1356:                memmove(&status_prompt_hlist[0], &status_prompt_hlist[1], size);
1.1       nicm     1357:
1.128     nicm     1358:                status_prompt_hlist[status_prompt_hsize - 1] = xstrdup(line);
                   1359:                return;
                   1360:        }
1.1       nicm     1361:
1.128     nicm     1362:        status_prompt_hlist = xreallocarray(status_prompt_hlist,
                   1363:            status_prompt_hsize + 1, sizeof *status_prompt_hlist);
                   1364:        status_prompt_hlist[status_prompt_hsize++] = xstrdup(line);
                   1365: }
                   1366:
                   1367: /* Build completion list. */
1.204     nicm     1368: static char **
                   1369: status_prompt_complete_list(u_int *size, const char *s, int at_start)
1.128     nicm     1370: {
1.183     nicm     1371:        char                                    **list = NULL;
                   1372:        const char                              **layout, *value, *cp;
1.128     nicm     1373:        const struct cmd_entry                  **cmdent;
                   1374:        const struct options_table_entry         *oe;
1.183     nicm     1375:        size_t                                    slen = strlen(s), valuelen;
                   1376:        struct options_entry                     *o;
1.191     nicm     1377:        struct options_array_item                *a;
1.128     nicm     1378:        const char                               *layouts[] = {
                   1379:                "even-horizontal", "even-vertical", "main-horizontal",
                   1380:                "main-vertical", "tiled", NULL
                   1381:        };
1.1       nicm     1382:
1.128     nicm     1383:        *size = 0;
1.1       nicm     1384:        for (cmdent = cmd_table; *cmdent != NULL; cmdent++) {
1.183     nicm     1385:                if (strncmp((*cmdent)->name, s, slen) == 0) {
1.128     nicm     1386:                        list = xreallocarray(list, (*size) + 1, sizeof *list);
1.183     nicm     1387:                        list[(*size)++] = xstrdup((*cmdent)->name);
1.128     nicm     1388:                }
1.205     nicm     1389:                if ((*cmdent)->alias != NULL &&
                   1390:                    strncmp((*cmdent)->alias, s, slen) == 0) {
                   1391:                        list = xreallocarray(list, (*size) + 1, sizeof *list);
                   1392:                        list[(*size)++] = xstrdup((*cmdent)->alias);
                   1393:                }
1.56      nicm     1394:        }
1.183     nicm     1395:        o = options_get_only(global_options, "command-alias");
1.191     nicm     1396:        if (o != NULL) {
                   1397:                a = options_array_first(o);
                   1398:                while (a != NULL) {
1.195     nicm     1399:                        value = options_array_item_value(a)->string;
1.194     nicm     1400:                        if ((cp = strchr(value, '=')) == NULL)
1.196     nicm     1401:                                goto next;
1.183     nicm     1402:                        valuelen = cp - value;
                   1403:                        if (slen > valuelen || strncmp(value, s, slen) != 0)
1.191     nicm     1404:                                goto next;
                   1405:
1.183     nicm     1406:                        list = xreallocarray(list, (*size) + 1, sizeof *list);
                   1407:                        list[(*size)++] = xstrndup(value, valuelen);
1.191     nicm     1408:
                   1409:                next:
                   1410:                        a = options_array_next(a);
1.183     nicm     1411:                }
                   1412:        }
1.204     nicm     1413:        if (at_start)
                   1414:                return (list);
                   1415:
                   1416:        for (oe = options_table; oe->name != NULL; oe++) {
                   1417:                if (strncmp(oe->name, s, slen) == 0) {
                   1418:                        list = xreallocarray(list, (*size) + 1, sizeof *list);
                   1419:                        list[(*size)++] = xstrdup(oe->name);
                   1420:                }
                   1421:        }
                   1422:        for (layout = layouts; *layout != NULL; layout++) {
                   1423:                if (strncmp(*layout, s, slen) == 0) {
                   1424:                        list = xreallocarray(list, (*size) + 1, sizeof *list);
                   1425:                        list[(*size)++] = xstrdup(*layout);
                   1426:                }
                   1427:        }
1.128     nicm     1428:        return (list);
                   1429: }
                   1430:
                   1431: /* Find longest prefix. */
1.151     nicm     1432: static char *
1.183     nicm     1433: status_prompt_complete_prefix(char **list, u_int size)
1.128     nicm     1434: {
                   1435:        char     *out;
                   1436:        u_int     i;
                   1437:        size_t    j;
                   1438:
                   1439:        out = xstrdup(list[0]);
                   1440:        for (i = 1; i < size; i++) {
                   1441:                j = strlen(list[i]);
                   1442:                if (j > strlen(out))
                   1443:                        j = strlen(out);
                   1444:                for (; j > 0; j--) {
                   1445:                        if (out[j - 1] != list[i][j - 1])
                   1446:                                out[j - 1] = '\0';
                   1447:                }
1.1       nicm     1448:        }
1.128     nicm     1449:        return (out);
                   1450: }
1.1       nicm     1451:
1.204     nicm     1452: /* Complete word menu callback. */
                   1453: static void
                   1454: status_prompt_menu_callback(__unused struct menu *menu, u_int idx, key_code key,
                   1455:     void *data)
                   1456: {
                   1457:        struct status_prompt_menu       *spm = data;
                   1458:        struct client                   *c = spm->c;
                   1459:        u_int                            i;
                   1460:        char                            *s;
                   1461:
                   1462:        if (key != KEYC_NONE) {
                   1463:                idx += spm->start;
                   1464:                if (spm->flag == '\0')
                   1465:                        s = xstrdup(spm->list[idx]);
                   1466:                else
                   1467:                        xasprintf(&s, "-%c%s", spm->flag, spm->list[idx]);
1.205     nicm     1468:                if (c->prompt_flags & PROMPT_WINDOW) {
                   1469:                        free(c->prompt_buffer);
                   1470:                        c->prompt_buffer = utf8_fromcstr(s);
                   1471:                        c->prompt_index = utf8_strlen(c->prompt_buffer);
                   1472:                        c->flags |= CLIENT_REDRAWSTATUS;
                   1473:                } else if (status_prompt_replace_complete(c, s))
1.204     nicm     1474:                        c->flags |= CLIENT_REDRAWSTATUS;
                   1475:                free(s);
                   1476:        }
                   1477:
                   1478:        for (i = 0; i < spm->size; i++)
                   1479:                free(spm->list[i]);
                   1480:        free(spm->list);
                   1481: }
                   1482:
                   1483: /* Show complete word menu. */
                   1484: static int
                   1485: status_prompt_complete_list_menu(struct client *c, char **list, u_int size,
                   1486:     u_int offset, char flag)
                   1487: {
                   1488:        struct menu                     *menu;
                   1489:        struct menu_item                 item;
                   1490:        struct status_prompt_menu       *spm;
                   1491:        u_int                            lines = status_line_size(c), height, i;
                   1492:        u_int                            py;
                   1493:
                   1494:        if (size <= 1)
                   1495:                return (0);
                   1496:        if (c->tty.sy - lines < 3)
                   1497:                return (0);
                   1498:
                   1499:        spm = xmalloc(sizeof *spm);
                   1500:        spm->c = c;
                   1501:        spm->size = size;
                   1502:        spm->list = list;
                   1503:        spm->flag = flag;
                   1504:
                   1505:        height = c->tty.sy - lines - 2;
                   1506:        if (height > 10)
                   1507:                height = 10;
                   1508:        if (height > size)
                   1509:                height = size;
                   1510:        spm->start = size - height;
                   1511:
                   1512:        menu = menu_create("");
                   1513:        for (i = spm->start; i < size; i++) {
                   1514:                item.name = list[i];
                   1515:                item.key = '0' + (i - spm->start);
                   1516:                item.command = NULL;
                   1517:                menu_add_item(menu, &item, NULL, NULL, NULL);
                   1518:        }
                   1519:
                   1520:        if (options_get_number(c->session->options, "status-position") == 0)
                   1521:                py = lines;
                   1522:        else
                   1523:                py = c->tty.sy - 3 - height;
                   1524:        offset += utf8_cstrwidth(c->prompt_string);
                   1525:        if (offset > 2)
                   1526:                offset -= 2;
                   1527:        else
                   1528:                offset = 0;
                   1529:
                   1530:        if (menu_display(menu, MENU_NOMOUSE|MENU_TAB, NULL, offset,
                   1531:            py, c, NULL, status_prompt_menu_callback, spm) != 0) {
                   1532:                menu_free(menu);
                   1533:                free(spm);
                   1534:                return (0);
                   1535:        }
                   1536:        return (1);
                   1537: }
                   1538:
                   1539: /* Show complete word menu. */
                   1540: static char *
                   1541: status_prompt_complete_window_menu(struct client *c, struct session *s,
1.206     nicm     1542:     const char *word, u_int offset, char flag)
1.204     nicm     1543: {
                   1544:        struct menu                      *menu;
                   1545:        struct menu_item                  item;
                   1546:        struct status_prompt_menu        *spm;
                   1547:        struct winlink                   *wl;
                   1548:        char                            **list = NULL, *tmp;
                   1549:        u_int                             lines = status_line_size(c), height;
                   1550:        u_int                             py, size = 0;
                   1551:
                   1552:        if (c->tty.sy - lines < 3)
                   1553:                return (NULL);
                   1554:
                   1555:        spm = xmalloc(sizeof *spm);
                   1556:        spm->c = c;
                   1557:        spm->flag = flag;
                   1558:
                   1559:        height = c->tty.sy - lines - 2;
                   1560:        if (height > 10)
                   1561:                height = 10;
                   1562:        spm->start = 0;
                   1563:
                   1564:        menu = menu_create("");
                   1565:        RB_FOREACH(wl, winlinks, &s->windows) {
1.206     nicm     1566:                if (word != NULL && *word != '\0') {
                   1567:                        xasprintf(&tmp, "%d", wl->idx);
                   1568:                        if (strncmp(tmp, word, strlen(word)) != 0) {
                   1569:                                free(tmp);
                   1570:                                continue;
                   1571:                        }
                   1572:                        free(tmp);
                   1573:                }
                   1574:
1.204     nicm     1575:                list = xreallocarray(list, size + 1, sizeof *list);
1.205     nicm     1576:                if (c->prompt_flags & PROMPT_WINDOW) {
                   1577:                        xasprintf(&tmp, "%d (%s)", wl->idx, wl->window->name);
                   1578:                        xasprintf(&list[size++], "%d", wl->idx);
                   1579:                } else {
                   1580:                        xasprintf(&tmp, "%s:%d (%s)", s->name, wl->idx,
                   1581:                            wl->window->name);
                   1582:                        xasprintf(&list[size++], "%s:%d", s->name, wl->idx);
                   1583:                }
1.204     nicm     1584:                item.name = tmp;
                   1585:                item.key = '0' + size - 1;
                   1586:                item.command = NULL;
                   1587:                menu_add_item(menu, &item, NULL, NULL, NULL);
                   1588:                free(tmp);
                   1589:
                   1590:                if (size == height)
                   1591:                        break;
                   1592:        }
1.206     nicm     1593:        if (size == 0) {
                   1594:                menu_free(menu);
                   1595:                return (NULL);
                   1596:        }
1.204     nicm     1597:        if (size == 1) {
                   1598:                menu_free(menu);
1.205     nicm     1599:                if (flag != '\0') {
                   1600:                        xasprintf(&tmp, "-%c%s", flag, list[0]);
                   1601:                        free(list[0]);
                   1602:                } else
                   1603:                        tmp = list[0];
1.204     nicm     1604:                free(list);
                   1605:                return (tmp);
                   1606:        }
                   1607:        if (height > size)
                   1608:                height = size;
                   1609:
                   1610:        spm->size = size;
                   1611:        spm->list = list;
                   1612:
                   1613:        if (options_get_number(c->session->options, "status-position") == 0)
                   1614:                py = lines;
                   1615:        else
                   1616:                py = c->tty.sy - 3 - height;
                   1617:        offset += utf8_cstrwidth(c->prompt_string);
                   1618:        if (offset > 2)
                   1619:                offset -= 2;
                   1620:        else
                   1621:                offset = 0;
                   1622:
                   1623:        if (menu_display(menu, MENU_NOMOUSE|MENU_TAB, NULL, offset,
                   1624:            py, c, NULL, status_prompt_menu_callback, spm) != 0) {
                   1625:                menu_free(menu);
                   1626:                free(spm);
                   1627:                return (NULL);
                   1628:        }
                   1629:        return (NULL);
                   1630: }
                   1631:
                   1632: /* Sort complete list. */
                   1633: static int
                   1634: status_prompt_complete_sort(const void *a, const void *b)
                   1635: {
                   1636:        const char      **aa = (const char **)a, **bb = (const char **)b;
                   1637:
                   1638:        return (strcmp(*aa, *bb));
                   1639: }
                   1640:
1.205     nicm     1641: /* Complete a session. */
                   1642: static char *
                   1643: status_prompt_complete_session(char ***list, u_int *size, const char *s,
                   1644:     char flag)
                   1645: {
                   1646:        struct session  *loop;
                   1647:        char            *out, *tmp;
                   1648:
                   1649:        RB_FOREACH(loop, sessions, &sessions) {
                   1650:                if (*s != '\0' && strncmp(loop->name, s, strlen(s)) != 0)
                   1651:                        continue;
                   1652:                *list = xreallocarray(*list, (*size) + 2, sizeof **list);
                   1653:                xasprintf(&(*list)[(*size)++], "%s:", loop->name);
                   1654:        }
                   1655:        out = status_prompt_complete_prefix(*list, *size);
                   1656:        if (out != NULL && flag != '\0') {
                   1657:                xasprintf(&tmp, "-%c%s", flag, out);
                   1658:                free(out);
                   1659:                out = tmp;
                   1660:        }
                   1661:        return (out);
                   1662: }
                   1663:
1.128     nicm     1664: /* Complete word. */
1.151     nicm     1665: static char *
1.204     nicm     1666: status_prompt_complete(struct client *c, const char *word, u_int offset)
1.128     nicm     1667: {
1.205     nicm     1668:        struct session   *session;
1.204     nicm     1669:        const char       *s, *colon;
1.205     nicm     1670:        char            **list = NULL, *copy = NULL, *out = NULL;
1.204     nicm     1671:        char              flag = '\0';
1.128     nicm     1672:        u_int             size = 0, i;
                   1673:
1.206     nicm     1674:        if (*word == '\0' &&
                   1675:            ((c->prompt_flags & (PROMPT_TARGET|PROMPT_WINDOW)) == 0))
1.1       nicm     1676:                return (NULL);
1.128     nicm     1677:
1.206     nicm     1678:        if (((c->prompt_flags & (PROMPT_TARGET|PROMPT_WINDOW)) == 0) &&
1.205     nicm     1679:            strncmp(word, "-t", 2) != 0 &&
                   1680:            strncmp(word, "-s", 2) != 0) {
1.204     nicm     1681:                list = status_prompt_complete_list(&size, word, offset == 0);
1.128     nicm     1682:                if (size == 0)
                   1683:                        out = NULL;
                   1684:                else if (size == 1)
                   1685:                        xasprintf(&out, "%s ", list[0]);
                   1686:                else
                   1687:                        out = status_prompt_complete_prefix(list, size);
1.204     nicm     1688:                goto found;
1.128     nicm     1689:        }
                   1690:
1.206     nicm     1691:        if (c->prompt_flags & (PROMPT_TARGET|PROMPT_WINDOW)) {
1.205     nicm     1692:                s = word;
                   1693:                flag = '\0';
                   1694:        } else {
                   1695:                s = word + 2;
                   1696:                flag = word[1];
                   1697:                offset += 2;
                   1698:        }
1.206     nicm     1699:
                   1700:        /* If this is a window completion, open the window menu. */
                   1701:        if (c->prompt_flags & PROMPT_WINDOW) {
                   1702:                out = status_prompt_complete_window_menu(c, c->session, s,
                   1703:                    offset, '\0');
                   1704:                goto found;
                   1705:        }
1.204     nicm     1706:        colon = strchr(s, ':');
1.1       nicm     1707:
1.204     nicm     1708:        /* If there is no colon, complete as a session. */
                   1709:        if (colon == NULL) {
1.205     nicm     1710:                out = status_prompt_complete_session(&list, &size, s, flag);
1.128     nicm     1711:                goto found;
                   1712:        }
                   1713:
1.204     nicm     1714:        /* If there is a colon but no period, find session and show a menu. */
                   1715:        if (strchr(colon + 1, '.') == NULL) {
                   1716:                if (*s == ':')
                   1717:                        session = c->session;
                   1718:                else {
                   1719:                        copy = xstrdup(s);
                   1720:                        *strchr(copy, ':') = '\0';
                   1721:                        session = session_find(copy);
                   1722:                        free(copy);
                   1723:                        if (session == NULL)
                   1724:                                goto found;
                   1725:                }
1.206     nicm     1726:                out = status_prompt_complete_window_menu(c, session, colon + 1,
                   1727:                    offset, flag);
1.204     nicm     1728:                if (out == NULL)
                   1729:                        return (NULL);
                   1730:        }
1.1       nicm     1731:
1.204     nicm     1732: found:
                   1733:        if (size != 0) {
                   1734:                qsort(list, size, sizeof *list, status_prompt_complete_sort);
                   1735:                for (i = 0; i < size; i++)
                   1736:                        log_debug("complete %u: %s", i, list[i]);
                   1737:        }
1.128     nicm     1738:
1.204     nicm     1739:        if (out != NULL && strcmp(word, out) == 0) {
                   1740:                free(out);
                   1741:                out = NULL;
1.1       nicm     1742:        }
1.204     nicm     1743:        if (out != NULL ||
                   1744:            !status_prompt_complete_list_menu(c, list, size, offset, flag)) {
                   1745:                for (i = 0; i < size; i++)
                   1746:                        free(list[i]);
                   1747:                free(list);
1.128     nicm     1748:        }
                   1749:        return (out);
1.1       nicm     1750: }