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

1.207   ! nicm        1: /* $OpenBSD: status.c,v 1.206 2020/05/16 15:18:17 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.192     nicm      375:        screen_write_start(&ctx, NULL, &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.10      nicm      426: status_message_set(struct client *c, 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);
                    436:        xvasprintf(&c->message_string, fmt, ap);
                    437:        va_end(ap);
                    438:
1.162     nicm      439:        server_client_add_message(c, "%s", c->message_string);
1.44      nicm      440:
1.137     nicm      441:        delay = options_get_number(c->session->options, "display-time");
1.143     tim       442:        if (delay > 0) {
                    443:                tv.tv_sec = delay / 1000;
                    444:                tv.tv_usec = (delay % 1000) * 1000L;
                    445:
                    446:                if (event_initialized(&c->message_timer))
                    447:                        evtimer_del(&c->message_timer);
                    448:                evtimer_set(&c->message_timer, status_message_callback, c);
                    449:                evtimer_add(&c->message_timer, &tv);
                    450:        }
1.1       nicm      451:
                    452:        c->tty.flags |= (TTY_NOCURSOR|TTY_FREEZE);
1.177     nicm      453:        c->flags |= CLIENT_REDRAWSTATUS;
1.1       nicm      454: }
                    455:
1.46      nicm      456: /* Clear status line message. */
1.1       nicm      457: void
                    458: status_message_clear(struct client *c)
                    459: {
                    460:        if (c->message_string == NULL)
                    461:                return;
                    462:
1.94      nicm      463:        free(c->message_string);
1.1       nicm      464:        c->message_string = NULL;
                    465:
1.156     nicm      466:        if (c->prompt_string == NULL)
                    467:                c->tty.flags &= ~(TTY_NOCURSOR|TTY_FREEZE);
1.177     nicm      468:        c->flags |= CLIENT_ALLREDRAWFLAGS; /* was frozen and may have changed */
1.7       nicm      469:
1.189     nicm      470:        status_pop_screen(c);
1.42      nicm      471: }
                    472:
1.46      nicm      473: /* Clear status line message after timer expires. */
1.151     nicm      474: static void
1.141     nicm      475: status_message_callback(__unused int fd, __unused short event, void *data)
1.42      nicm      476: {
                    477:        struct client   *c = data;
                    478:
                    479:        status_message_clear(c);
1.1       nicm      480: }
                    481:
                    482: /* Draw client message on status line of present else on last line. */
                    483: int
                    484: status_message_redraw(struct client *c)
                    485: {
1.189     nicm      486:        struct status_line      *sl = &c->status;
                    487:        struct screen_write_ctx  ctx;
                    488:        struct session          *s = c->session;
                    489:        struct screen            old_screen;
                    490:        size_t                   len;
                    491:        u_int                    lines, offset;
                    492:        struct grid_cell         gc;
1.207   ! nicm      493:        struct format_tree      *ft;
1.1       nicm      494:
                    495:        if (c->tty.sx == 0 || c->tty.sy == 0)
                    496:                return (0);
1.189     nicm      497:        memcpy(&old_screen, sl->active, sizeof old_screen);
1.169     nicm      498:
1.182     nicm      499:        lines = status_line_size(c);
1.189     nicm      500:        if (lines <= 1)
1.173     nicm      501:                lines = 1;
1.190     nicm      502:        screen_init(sl->active, c->tty.sx, lines, 0);
1.1       nicm      503:
1.139     nicm      504:        len = screen_write_strlen("%s", c->message_string);
1.1       nicm      505:        if (len > c->tty.sx)
                    506:                len = c->tty.sx;
                    507:
1.207   ! nicm      508:        ft = format_create_defaults(NULL, c, NULL, NULL, NULL);
        !           509:        style_apply(&gc, s->options, "message-style", ft);
        !           510:        format_free(ft);
1.1       nicm      511:
1.189     nicm      512:        screen_write_start(&ctx, NULL, sl->active);
1.192     nicm      513:        screen_write_fast_copy(&ctx, &sl->screen, 0, 0, c->tty.sx, lines - 1);
                    514:        screen_write_cursormove(&ctx, 0, lines - 1, 0);
                    515:        for (offset = 0; offset < c->tty.sx; offset++)
1.170     nicm      516:                screen_write_putc(&ctx, &gc, ' ');
1.184     nicm      517:        screen_write_cursormove(&ctx, 0, lines - 1, 0);
1.139     nicm      518:        screen_write_nputs(&ctx, len, &gc, "%s", c->message_string);
1.1       nicm      519:        screen_write_stop(&ctx);
                    520:
1.189     nicm      521:        if (grid_compare(sl->active->grid, old_screen.grid) == 0) {
                    522:                screen_free(&old_screen);
1.1       nicm      523:                return (0);
                    524:        }
1.189     nicm      525:        screen_free(&old_screen);
1.1       nicm      526:        return (1);
                    527: }
                    528:
1.46      nicm      529: /* Enable status line prompt. */
1.1       nicm      530: void
1.76      nicm      531: status_prompt_set(struct client *c, const char *msg, const char *input,
1.166     nicm      532:     prompt_input_cb inputcb, prompt_free_cb freecb, void *data, int flags)
1.1       nicm      533: {
1.122     nicm      534:        struct format_tree      *ft;
1.165     nicm      535:        char                    *tmp, *cp;
1.122     nicm      536:
1.164     nicm      537:        ft = format_create(c, NULL, FORMAT_NONE, 0);
1.122     nicm      538:        format_defaults(ft, c, NULL, NULL, NULL);
1.152     nicm      539:
1.168     nicm      540:        if (input == NULL)
                    541:                input = "";
                    542:        if (flags & PROMPT_NOFORMAT)
                    543:                tmp = xstrdup(input);
                    544:        else
1.185     nicm      545:                tmp = format_expand_time(ft, input);
1.20      nicm      546:
1.12      nicm      547:        status_message_clear(c);
                    548:        status_prompt_clear(c);
1.189     nicm      549:        status_push_screen(c);
1.12      nicm      550:
1.185     nicm      551:        c->prompt_string = format_expand_time(ft, msg);
1.1       nicm      552:
1.152     nicm      553:        c->prompt_buffer = utf8_fromcstr(tmp);
                    554:        c->prompt_index = utf8_strlen(c->prompt_buffer);
1.1       nicm      555:
1.166     nicm      556:        c->prompt_inputcb = inputcb;
                    557:        c->prompt_freecb = freecb;
1.1       nicm      558:        c->prompt_data = data;
                    559:
                    560:        c->prompt_hindex = 0;
                    561:
                    562:        c->prompt_flags = flags;
1.155     nicm      563:        c->prompt_mode = PROMPT_ENTRY;
1.1       nicm      564:
1.158     nicm      565:        if (~flags & PROMPT_INCREMENTAL)
                    566:                c->tty.flags |= (TTY_NOCURSOR|TTY_FREEZE);
1.177     nicm      567:        c->flags |= CLIENT_REDRAWSTATUS;
1.165     nicm      568:
                    569:        if ((flags & PROMPT_INCREMENTAL) && *tmp != '\0') {
                    570:                xasprintf(&cp, "=%s", tmp);
1.166     nicm      571:                c->prompt_inputcb(c, c->prompt_data, cp, 0);
1.165     nicm      572:                free(cp);
                    573:        }
1.122     nicm      574:
1.152     nicm      575:        free(tmp);
1.122     nicm      576:        format_free(ft);
1.1       nicm      577: }
                    578:
1.46      nicm      579: /* Remove status line prompt. */
1.1       nicm      580: void
                    581: status_prompt_clear(struct client *c)
                    582: {
1.55      nicm      583:        if (c->prompt_string == NULL)
1.1       nicm      584:                return;
                    585:
1.166     nicm      586:        if (c->prompt_freecb != NULL && c->prompt_data != NULL)
                    587:                c->prompt_freecb(c->prompt_data);
1.1       nicm      588:
1.94      nicm      589:        free(c->prompt_string);
1.1       nicm      590:        c->prompt_string = NULL;
                    591:
1.94      nicm      592:        free(c->prompt_buffer);
1.1       nicm      593:        c->prompt_buffer = NULL;
                    594:
1.181     nicm      595:        free(c->prompt_saved);
                    596:        c->prompt_saved = NULL;
                    597:
1.1       nicm      598:        c->tty.flags &= ~(TTY_NOCURSOR|TTY_FREEZE);
1.177     nicm      599:        c->flags |= CLIENT_ALLREDRAWFLAGS; /* was frozen and may have changed */
1.7       nicm      600:
1.189     nicm      601:        status_pop_screen(c);
1.27      nicm      602: }
                    603:
1.46      nicm      604: /* Update status line prompt with a new prompt string. */
1.27      nicm      605: void
1.76      nicm      606: status_prompt_update(struct client *c, const char *msg, const char *input)
1.27      nicm      607: {
1.122     nicm      608:        struct format_tree      *ft;
1.152     nicm      609:        char                    *tmp;
1.122     nicm      610:
1.164     nicm      611:        ft = format_create(c, NULL, FORMAT_NONE, 0);
1.122     nicm      612:        format_defaults(ft, c, NULL, NULL, NULL);
1.152     nicm      613:
1.185     nicm      614:        tmp = format_expand_time(ft, input);
1.122     nicm      615:
1.94      nicm      616:        free(c->prompt_string);
1.185     nicm      617:        c->prompt_string = format_expand_time(ft, msg);
1.27      nicm      618:
1.94      nicm      619:        free(c->prompt_buffer);
1.152     nicm      620:        c->prompt_buffer = utf8_fromcstr(tmp);
                    621:        c->prompt_index = utf8_strlen(c->prompt_buffer);
1.27      nicm      622:
                    623:        c->prompt_hindex = 0;
                    624:
1.177     nicm      625:        c->flags |= CLIENT_REDRAWSTATUS;
1.122     nicm      626:
1.152     nicm      627:        free(tmp);
1.122     nicm      628:        format_free(ft);
1.1       nicm      629: }
                    630:
                    631: /* Draw client prompt on status line of present else on last line. */
                    632: int
                    633: status_prompt_redraw(struct client *c)
                    634: {
1.189     nicm      635:        struct status_line      *sl = &c->status;
1.152     nicm      636:        struct screen_write_ctx  ctx;
                    637:        struct session          *s = c->session;
1.189     nicm      638:        struct screen            old_screen;
                    639:        u_int                    i, lines, offset, left, start, width;
                    640:        u_int                    pcursor, pwidth;
1.152     nicm      641:        struct grid_cell         gc, cursorgc;
1.207   ! nicm      642:        struct format_tree      *ft;
1.1       nicm      643:
                    644:        if (c->tty.sx == 0 || c->tty.sy == 0)
                    645:                return (0);
1.189     nicm      646:        memcpy(&old_screen, sl->active, sizeof old_screen);
1.169     nicm      647:
1.182     nicm      648:        lines = status_line_size(c);
1.189     nicm      649:        if (lines <= 1)
1.173     nicm      650:                lines = 1;
1.189     nicm      651:        screen_init(sl->active, c->tty.sx, lines, 0);
1.1       nicm      652:
1.207   ! nicm      653:        ft = format_create_defaults(NULL, c, NULL, NULL, NULL);
1.155     nicm      654:        if (c->prompt_mode == PROMPT_COMMAND)
1.207   ! nicm      655:                style_apply(&gc, s->options, "message-command-style", ft);
1.108     nicm      656:        else
1.207   ! nicm      657:                style_apply(&gc, s->options, "message-style", ft);
        !           658:        format_free(ft);
1.1       nicm      659:
1.152     nicm      660:        memcpy(&cursorgc, &gc, sizeof cursorgc);
                    661:        cursorgc.attr ^= GRID_ATTR_REVERSE;
                    662:
                    663:        start = screen_write_strlen("%s", c->prompt_string);
                    664:        if (start > c->tty.sx)
                    665:                start = c->tty.sx;
                    666:
1.189     nicm      667:        screen_write_start(&ctx, NULL, sl->active);
1.192     nicm      668:        screen_write_fast_copy(&ctx, &sl->screen, 0, 0, c->tty.sx, lines - 1);
                    669:        screen_write_cursormove(&ctx, 0, lines - 1, 0);
                    670:        for (offset = 0; offset < c->tty.sx; offset++)
1.170     nicm      671:                screen_write_putc(&ctx, &gc, ' ');
1.192     nicm      672:        screen_write_cursormove(&ctx, 0, lines - 1, 0);
1.152     nicm      673:        screen_write_nputs(&ctx, start, &gc, "%s", c->prompt_string);
1.192     nicm      674:        screen_write_cursormove(&ctx, start, lines - 1, 0);
1.1       nicm      675:
1.152     nicm      676:        left = c->tty.sx - start;
                    677:        if (left == 0)
                    678:                goto finished;
                    679:
                    680:        pcursor = utf8_strwidth(c->prompt_buffer, c->prompt_index);
                    681:        pwidth = utf8_strwidth(c->prompt_buffer, -1);
                    682:        if (pcursor >= left) {
                    683:                /*
                    684:                 * The cursor would be outside the screen so start drawing
                    685:                 * with it on the right.
                    686:                 */
                    687:                offset = (pcursor - left) + 1;
                    688:                pwidth = left;
                    689:        } else
                    690:                offset = 0;
                    691:        if (pwidth > left)
                    692:                pwidth = left;
                    693:
                    694:        width = 0;
                    695:        for (i = 0; c->prompt_buffer[i].size != 0; i++) {
                    696:                if (width < offset) {
                    697:                        width += c->prompt_buffer[i].width;
                    698:                        continue;
1.1       nicm      699:                }
1.152     nicm      700:                if (width >= offset + pwidth)
                    701:                        break;
                    702:                width += c->prompt_buffer[i].width;
                    703:                if (width > offset + pwidth)
                    704:                        break;
1.1       nicm      705:
1.152     nicm      706:                if (i != c->prompt_index) {
                    707:                        utf8_copy(&gc.data, &c->prompt_buffer[i]);
                    708:                        screen_write_cell(&ctx, &gc);
                    709:                } else {
                    710:                        utf8_copy(&cursorgc.data, &c->prompt_buffer[i]);
                    711:                        screen_write_cell(&ctx, &cursorgc);
                    712:                }
1.1       nicm      713:        }
1.189     nicm      714:        if (sl->active->cx < screen_size_x(sl->active) && c->prompt_index >= i)
1.152     nicm      715:                screen_write_putc(&ctx, &cursorgc, ' ');
1.1       nicm      716:
1.152     nicm      717: finished:
1.1       nicm      718:        screen_write_stop(&ctx);
1.51      nicm      719:
1.189     nicm      720:        if (grid_compare(sl->active->grid, old_screen.grid) == 0) {
                    721:                screen_free(&old_screen);
1.1       nicm      722:                return (0);
                    723:        }
1.189     nicm      724:        screen_free(&old_screen);
1.1       nicm      725:        return (1);
                    726: }
                    727:
1.152     nicm      728: /* Is this a separator? */
                    729: static int
                    730: status_prompt_in_list(const char *ws, const struct utf8_data *ud)
                    731: {
                    732:        if (ud->size != 1 || ud->width != 1)
                    733:                return (0);
                    734:        return (strchr(ws, *ud->data) != NULL);
                    735: }
                    736:
                    737: /* Is this a space? */
                    738: static int
                    739: status_prompt_space(const struct utf8_data *ud)
                    740: {
                    741:        if (ud->size != 1 || ud->width != 1)
                    742:                return (0);
                    743:        return (*ud->data == ' ');
                    744: }
                    745:
1.155     nicm      746: /*
                    747:  * Translate key from emacs to vi. Return 0 to drop key, 1 to process the key
                    748:  * as an emacs key; return 2 to append to the buffer.
                    749:  */
                    750: static int
                    751: status_prompt_translate_key(struct client *c, key_code key, key_code *new_key)
                    752: {
                    753:        if (c->prompt_mode == PROMPT_ENTRY) {
                    754:                switch (key) {
                    755:                case '\003': /* C-c */
1.202     nicm      756:                case '\007': /* C-g */
1.155     nicm      757:                case '\010': /* C-h */
                    758:                case '\011': /* Tab */
                    759:                case '\025': /* C-u */
                    760:                case '\027': /* C-w */
                    761:                case '\n':
                    762:                case '\r':
                    763:                case KEYC_BSPACE:
                    764:                case KEYC_DC:
                    765:                case KEYC_DOWN:
                    766:                case KEYC_END:
                    767:                case KEYC_HOME:
                    768:                case KEYC_LEFT:
                    769:                case KEYC_RIGHT:
                    770:                case KEYC_UP:
                    771:                        *new_key = key;
                    772:                        return (1);
                    773:                case '\033': /* Escape */
                    774:                        c->prompt_mode = PROMPT_COMMAND;
1.177     nicm      775:                        c->flags |= CLIENT_REDRAWSTATUS;
1.155     nicm      776:                        return (0);
                    777:                }
                    778:                *new_key = key;
                    779:                return (2);
                    780:        }
                    781:
                    782:        switch (key) {
                    783:        case 'A':
                    784:        case 'I':
                    785:        case 'C':
                    786:        case 's':
                    787:        case 'a':
                    788:                c->prompt_mode = PROMPT_ENTRY;
1.177     nicm      789:                c->flags |= CLIENT_REDRAWSTATUS;
1.155     nicm      790:                break; /* switch mode and... */
                    791:        case 'S':
                    792:                c->prompt_mode = PROMPT_ENTRY;
1.177     nicm      793:                c->flags |= CLIENT_REDRAWSTATUS;
1.155     nicm      794:                *new_key = '\025'; /* C-u */
                    795:                return (1);
                    796:        case 'i':
                    797:        case '\033': /* Escape */
                    798:                c->prompt_mode = PROMPT_ENTRY;
1.177     nicm      799:                c->flags |= CLIENT_REDRAWSTATUS;
1.155     nicm      800:                return (0);
                    801:        }
                    802:
                    803:        switch (key) {
                    804:        case 'A':
                    805:        case '$':
                    806:                *new_key = KEYC_END;
                    807:                return (1);
                    808:        case 'I':
                    809:        case '0':
                    810:        case '^':
                    811:                *new_key = KEYC_HOME;
                    812:                return (1);
                    813:        case 'C':
                    814:        case 'D':
                    815:                *new_key = '\013'; /* C-k */
                    816:                return (1);
                    817:        case KEYC_BSPACE:
                    818:        case 'X':
                    819:                *new_key = KEYC_BSPACE;
                    820:                return (1);
                    821:        case 'b':
                    822:        case 'B':
                    823:                *new_key = 'b'|KEYC_ESCAPE;
                    824:                return (1);
                    825:        case 'd':
                    826:                *new_key = '\025';
                    827:                return (1);
                    828:        case 'e':
                    829:        case 'E':
                    830:        case 'w':
                    831:        case 'W':
                    832:                *new_key = 'f'|KEYC_ESCAPE;
                    833:                return (1);
                    834:        case 'p':
                    835:                *new_key = '\031'; /* C-y */
1.202     nicm      836:                return (1);
                    837:        case 'q':
                    838:                *new_key = '\003'; /* C-c */
1.155     nicm      839:                return (1);
                    840:        case 's':
                    841:        case KEYC_DC:
                    842:        case 'x':
                    843:                *new_key = KEYC_DC;
                    844:                return (1);
                    845:        case KEYC_DOWN:
                    846:        case 'j':
                    847:                *new_key = KEYC_DOWN;
                    848:                return (1);
                    849:        case KEYC_LEFT:
                    850:        case 'h':
                    851:                *new_key = KEYC_LEFT;
                    852:                return (1);
                    853:        case 'a':
                    854:        case KEYC_RIGHT:
                    855:        case 'l':
                    856:                *new_key = KEYC_RIGHT;
                    857:                return (1);
                    858:        case KEYC_UP:
                    859:        case 'k':
                    860:                *new_key = KEYC_UP;
                    861:                return (1);
                    862:        case '\010' /* C-h */:
                    863:        case '\003' /* C-c */:
                    864:        case '\n':
                    865:        case '\r':
                    866:                return (1);
                    867:        }
                    868:        return (0);
                    869: }
                    870:
1.199     nicm      871: /* Paste into prompt. */
                    872: static int
                    873: status_prompt_paste(struct client *c)
                    874: {
                    875:        struct paste_buffer     *pb;
                    876:        const char              *bufdata;
                    877:        size_t                   size, n, bufsize;
                    878:        u_int                    i;
                    879:        struct utf8_data        *ud, *udp;
                    880:        enum utf8_state          more;
                    881:
                    882:        size = utf8_strlen(c->prompt_buffer);
                    883:        if (c->prompt_saved != NULL) {
                    884:                ud = c->prompt_saved;
                    885:                n = utf8_strlen(c->prompt_saved);
                    886:        } else {
                    887:                if ((pb = paste_get_top(NULL)) == NULL)
                    888:                        return (0);
                    889:                bufdata = paste_buffer_data(pb, &bufsize);
1.200     nicm      890:                ud = xreallocarray(NULL, bufsize + 1, sizeof *ud);
1.199     nicm      891:                udp = ud;
                    892:                for (i = 0; i != bufsize; /* nothing */) {
                    893:                        more = utf8_open(udp, bufdata[i]);
                    894:                        if (more == UTF8_MORE) {
                    895:                                while (++i != bufsize && more == UTF8_MORE)
                    896:                                        more = utf8_append(udp, bufdata[i]);
                    897:                                if (more == UTF8_DONE) {
                    898:                                        udp++;
                    899:                                        continue;
                    900:                                }
                    901:                                i -= udp->have;
                    902:                        }
                    903:                        if (bufdata[i] <= 31 || bufdata[i] >= 127)
                    904:                                break;
                    905:                        utf8_set(udp, bufdata[i]);
                    906:                        udp++;
                    907:                        i++;
                    908:                }
                    909:                udp->size = 0;
                    910:                n = udp - ud;
                    911:        }
                    912:        if (n == 0)
                    913:                return (0);
                    914:
                    915:        c->prompt_buffer = xreallocarray(c->prompt_buffer, size + n + 1,
                    916:            sizeof *c->prompt_buffer);
                    917:        if (c->prompt_index == size) {
                    918:                memcpy(c->prompt_buffer + c->prompt_index, ud,
                    919:                    n * sizeof *c->prompt_buffer);
                    920:                c->prompt_index += n;
                    921:                c->prompt_buffer[c->prompt_index].size = 0;
                    922:        } else {
                    923:                memmove(c->prompt_buffer + c->prompt_index + n,
                    924:                    c->prompt_buffer + c->prompt_index,
                    925:                    (size + 1 - c->prompt_index) * sizeof *c->prompt_buffer);
                    926:                memcpy(c->prompt_buffer + c->prompt_index, ud,
                    927:                    n * sizeof *c->prompt_buffer);
                    928:                c->prompt_index += n;
                    929:        }
                    930:
                    931:        if (ud != c->prompt_saved)
                    932:                free(ud);
                    933:        return (1);
                    934: }
                    935:
1.204     nicm      936: /* Finish completion. */
                    937: static int
                    938: status_prompt_replace_complete(struct client *c, const char *s)
                    939: {
                    940:        char                     word[64], *allocated = NULL;
                    941:        size_t                   size, n, off, idx, used;
                    942:        struct utf8_data        *first, *last, *ud;
                    943:
1.205     nicm      944:        /* Work out where the cursor currently is. */
1.204     nicm      945:        idx = c->prompt_index;
                    946:        if (idx != 0)
                    947:                idx--;
1.205     nicm      948:        size = utf8_strlen(c->prompt_buffer);
1.204     nicm      949:
                    950:        /* Find the word we are in. */
                    951:        first = &c->prompt_buffer[idx];
                    952:        while (first > c->prompt_buffer && !status_prompt_space(first))
                    953:                first--;
                    954:        while (first->size != 0 && status_prompt_space(first))
                    955:                first++;
                    956:        last = &c->prompt_buffer[idx];
                    957:        while (last->size != 0 && !status_prompt_space(last))
                    958:                last++;
                    959:        while (last > c->prompt_buffer && status_prompt_space(last))
                    960:                last--;
                    961:        if (last->size != 0)
                    962:                last++;
1.205     nicm      963:        if (last < first)
1.204     nicm      964:                return (0);
                    965:        if (s == NULL) {
                    966:                used = 0;
                    967:                for (ud = first; ud < last; ud++) {
                    968:                        if (used + ud->size >= sizeof word)
                    969:                                break;
                    970:                        memcpy(word + used, ud->data, ud->size);
                    971:                        used += ud->size;
                    972:                }
                    973:                if (ud != last)
                    974:                        return (0);
                    975:                word[used] = '\0';
                    976:        }
                    977:
                    978:        /* Try to complete it. */
                    979:        if (s == NULL) {
                    980:                allocated = status_prompt_complete(c, word,
                    981:                    first - c->prompt_buffer);
                    982:                if (allocated == NULL)
                    983:                        return (0);
                    984:                s = allocated;
                    985:        }
                    986:
                    987:        /* Trim out word. */
                    988:        n = size - (last - c->prompt_buffer) + 1; /* with \0 */
                    989:        memmove(first, last, n * sizeof *c->prompt_buffer);
                    990:        size -= last - first;
                    991:
                    992:        /* Insert the new word. */
                    993:        size += strlen(s);
                    994:        off = first - c->prompt_buffer;
                    995:        c->prompt_buffer = xreallocarray(c->prompt_buffer, size + 1,
                    996:            sizeof *c->prompt_buffer);
                    997:        first = c->prompt_buffer + off;
                    998:        memmove(first + strlen(s), first, n * sizeof *c->prompt_buffer);
                    999:        for (idx = 0; idx < strlen(s); idx++)
                   1000:                utf8_set(&first[idx], s[idx]);
                   1001:        c->prompt_index = (first - c->prompt_buffer) + strlen(s);
                   1002:
                   1003:        free(allocated);
                   1004:        return (1);
                   1005: }
                   1006:
1.1       nicm     1007: /* Handle keys in prompt. */
1.154     nicm     1008: int
1.138     nicm     1009: status_prompt_key(struct client *c, key_code key)
1.1       nicm     1010: {
1.152     nicm     1011:        struct options          *oo = c->session->options;
1.204     nicm     1012:        char                    *s, *cp, prefix = '=';
1.201     nicm     1013:        const char              *histstr, *ws = NULL, *keystring;
1.204     nicm     1014:        size_t                   size, idx;
                   1015:        struct utf8_data         tmp;
1.155     nicm     1016:        int                      keys;
1.1       nicm     1017:
1.201     nicm     1018:        if (c->prompt_flags & PROMPT_KEY) {
                   1019:                keystring = key_string_lookup_key(key);
                   1020:                c->prompt_inputcb(c, c->prompt_data, keystring, 1);
                   1021:                status_prompt_clear(c);
                   1022:                return (0);
                   1023:        }
1.152     nicm     1024:        size = utf8_strlen(c->prompt_buffer);
1.154     nicm     1025:
                   1026:        if (c->prompt_flags & PROMPT_NUMERIC) {
                   1027:                if (key >= '0' && key <= '9')
                   1028:                        goto append_key;
                   1029:                s = utf8_tocstr(c->prompt_buffer);
1.166     nicm     1030:                c->prompt_inputcb(c, c->prompt_data, s, 1);
1.154     nicm     1031:                status_prompt_clear(c);
                   1032:                free(s);
                   1033:                return (1);
                   1034:        }
1.180     nicm     1035:        key &= ~KEYC_XTERM;
1.154     nicm     1036:
1.155     nicm     1037:        keys = options_get_number(c->session->options, "status-keys");
                   1038:        if (keys == MODEKEY_VI) {
                   1039:                switch (status_prompt_translate_key(c, key, &key)) {
                   1040:                case 1:
                   1041:                        goto process_key;
                   1042:                case 2:
                   1043:                        goto append_key;
                   1044:                default:
                   1045:                        return (0);
                   1046:                }
                   1047:        }
                   1048:
                   1049: process_key:
                   1050:        switch (key) {
                   1051:        case KEYC_LEFT:
                   1052:        case '\002': /* C-b */
1.1       nicm     1053:                if (c->prompt_index > 0) {
                   1054:                        c->prompt_index--;
1.158     nicm     1055:                        break;
1.1       nicm     1056:                }
                   1057:                break;
1.155     nicm     1058:        case KEYC_RIGHT:
                   1059:        case '\006': /* C-f */
1.1       nicm     1060:                if (c->prompt_index < size) {
                   1061:                        c->prompt_index++;
1.158     nicm     1062:                        break;
1.1       nicm     1063:                }
                   1064:                break;
1.155     nicm     1065:        case KEYC_HOME:
                   1066:        case '\001': /* C-a */
1.1       nicm     1067:                if (c->prompt_index != 0) {
                   1068:                        c->prompt_index = 0;
1.158     nicm     1069:                        break;
1.1       nicm     1070:                }
                   1071:                break;
1.155     nicm     1072:        case KEYC_END:
                   1073:        case '\005': /* C-e */
1.1       nicm     1074:                if (c->prompt_index != size) {
                   1075:                        c->prompt_index = size;
1.158     nicm     1076:                        break;
1.1       nicm     1077:                }
                   1078:                break;
1.155     nicm     1079:        case '\011': /* Tab */
1.206     nicm     1080:                if (status_prompt_replace_complete(c, NULL))
1.204     nicm     1081:                        goto changed;
                   1082:                break;
1.155     nicm     1083:        case KEYC_BSPACE:
                   1084:        case '\010': /* C-h */
1.1       nicm     1085:                if (c->prompt_index != 0) {
                   1086:                        if (c->prompt_index == size)
1.152     nicm     1087:                                c->prompt_buffer[--c->prompt_index].size = 0;
1.1       nicm     1088:                        else {
                   1089:                                memmove(c->prompt_buffer + c->prompt_index - 1,
                   1090:                                    c->prompt_buffer + c->prompt_index,
1.152     nicm     1091:                                    (size + 1 - c->prompt_index) *
                   1092:                                    sizeof *c->prompt_buffer);
1.1       nicm     1093:                                c->prompt_index--;
                   1094:                        }
1.158     nicm     1095:                        goto changed;
1.1       nicm     1096:                }
                   1097:                break;
1.155     nicm     1098:        case KEYC_DC:
                   1099:        case '\004': /* C-d */
1.1       nicm     1100:                if (c->prompt_index != size) {
                   1101:                        memmove(c->prompt_buffer + c->prompt_index,
                   1102:                            c->prompt_buffer + c->prompt_index + 1,
1.152     nicm     1103:                            (size + 1 - c->prompt_index) *
                   1104:                            sizeof *c->prompt_buffer);
1.158     nicm     1105:                        goto changed;
1.18      nicm     1106:                }
1.26      nicm     1107:                break;
1.155     nicm     1108:        case '\025': /* C-u */
1.152     nicm     1109:                c->prompt_buffer[0].size = 0;
1.26      nicm     1110:                c->prompt_index = 0;
1.158     nicm     1111:                goto changed;
1.155     nicm     1112:        case '\013': /* C-k */
1.18      nicm     1113:                if (c->prompt_index < size) {
1.152     nicm     1114:                        c->prompt_buffer[c->prompt_index].size = 0;
1.158     nicm     1115:                        goto changed;
1.1       nicm     1116:                }
                   1117:                break;
1.155     nicm     1118:        case '\027': /* C-w */
1.152     nicm     1119:                ws = options_get_string(oo, "word-separators");
1.81      nicm     1120:                idx = c->prompt_index;
                   1121:
                   1122:                /* Find a non-separator. */
                   1123:                while (idx != 0) {
                   1124:                        idx--;
1.152     nicm     1125:                        if (!status_prompt_in_list(ws, &c->prompt_buffer[idx]))
1.81      nicm     1126:                                break;
                   1127:                }
                   1128:
                   1129:                /* Find the separator at the beginning of the word. */
                   1130:                while (idx != 0) {
                   1131:                        idx--;
1.152     nicm     1132:                        if (status_prompt_in_list(ws, &c->prompt_buffer[idx])) {
1.81      nicm     1133:                                /* Go back to the word. */
                   1134:                                idx++;
                   1135:                                break;
                   1136:                        }
                   1137:                }
                   1138:
1.181     nicm     1139:                free(c->prompt_saved);
                   1140:                c->prompt_saved = xcalloc(sizeof *c->prompt_buffer,
                   1141:                    (c->prompt_index - idx) + 1);
                   1142:                memcpy(c->prompt_saved, c->prompt_buffer + idx,
                   1143:                    (c->prompt_index - idx) * sizeof *c->prompt_buffer);
                   1144:
1.81      nicm     1145:                memmove(c->prompt_buffer + idx,
                   1146:                    c->prompt_buffer + c->prompt_index,
1.152     nicm     1147:                    (size + 1 - c->prompt_index) *
                   1148:                    sizeof *c->prompt_buffer);
1.81      nicm     1149:                memset(c->prompt_buffer + size - (c->prompt_index - idx),
1.152     nicm     1150:                    '\0', (c->prompt_index - idx) * sizeof *c->prompt_buffer);
1.81      nicm     1151:                c->prompt_index = idx;
1.152     nicm     1152:
1.158     nicm     1153:                goto changed;
1.155     nicm     1154:        case 'f'|KEYC_ESCAPE:
1.180     nicm     1155:        case KEYC_RIGHT|KEYC_CTRL:
1.155     nicm     1156:                ws = options_get_string(oo, "word-separators");
1.81      nicm     1157:
                   1158:                /* Find a word. */
                   1159:                while (c->prompt_index != size) {
1.152     nicm     1160:                        idx = ++c->prompt_index;
                   1161:                        if (!status_prompt_in_list(ws, &c->prompt_buffer[idx]))
1.81      nicm     1162:                                break;
                   1163:                }
                   1164:
                   1165:                /* Find the separator at the end of the word. */
                   1166:                while (c->prompt_index != size) {
1.152     nicm     1167:                        idx = ++c->prompt_index;
                   1168:                        if (status_prompt_in_list(ws, &c->prompt_buffer[idx]))
1.81      nicm     1169:                                break;
                   1170:                }
1.106     nicm     1171:
                   1172:                /* Back up to the end-of-word like vi. */
                   1173:                if (options_get_number(oo, "status-keys") == MODEKEY_VI &&
                   1174:                    c->prompt_index != 0)
                   1175:                        c->prompt_index--;
1.81      nicm     1176:
1.158     nicm     1177:                goto changed;
1.155     nicm     1178:        case 'b'|KEYC_ESCAPE:
1.180     nicm     1179:        case KEYC_LEFT|KEYC_CTRL:
1.155     nicm     1180:                ws = options_get_string(oo, "word-separators");
1.81      nicm     1181:
                   1182:                /* Find a non-separator. */
                   1183:                while (c->prompt_index != 0) {
1.152     nicm     1184:                        idx = --c->prompt_index;
                   1185:                        if (!status_prompt_in_list(ws, &c->prompt_buffer[idx]))
1.81      nicm     1186:                                break;
                   1187:                }
                   1188:
                   1189:                /* Find the separator at the beginning of the word. */
                   1190:                while (c->prompt_index != 0) {
1.152     nicm     1191:                        idx = --c->prompt_index;
                   1192:                        if (status_prompt_in_list(ws, &c->prompt_buffer[idx])) {
1.81      nicm     1193:                                /* Go back to the word. */
                   1194:                                c->prompt_index++;
                   1195:                                break;
                   1196:                        }
                   1197:                }
1.158     nicm     1198:                goto changed;
1.155     nicm     1199:        case KEYC_UP:
                   1200:        case '\020': /* C-p */
1.66      nicm     1201:                histstr = status_prompt_up_history(&c->prompt_hindex);
                   1202:                if (histstr == NULL)
1.1       nicm     1203:                        break;
1.94      nicm     1204:                free(c->prompt_buffer);
1.152     nicm     1205:                c->prompt_buffer = utf8_fromcstr(histstr);
                   1206:                c->prompt_index = utf8_strlen(c->prompt_buffer);
1.158     nicm     1207:                goto changed;
1.155     nicm     1208:        case KEYC_DOWN:
                   1209:        case '\016': /* C-n */
1.66      nicm     1210:                histstr = status_prompt_down_history(&c->prompt_hindex);
                   1211:                if (histstr == NULL)
1.65      nicm     1212:                        break;
1.94      nicm     1213:                free(c->prompt_buffer);
1.152     nicm     1214:                c->prompt_buffer = utf8_fromcstr(histstr);
                   1215:                c->prompt_index = utf8_strlen(c->prompt_buffer);
1.158     nicm     1216:                goto changed;
1.155     nicm     1217:        case '\031': /* C-y */
1.199     nicm     1218:                if (status_prompt_paste(c))
                   1219:                        goto changed;
                   1220:                break;
1.155     nicm     1221:        case '\024': /* C-t */
1.30      nicm     1222:                idx = c->prompt_index;
                   1223:                if (idx < size)
                   1224:                        idx++;
                   1225:                if (idx >= 2) {
1.152     nicm     1226:                        utf8_copy(&tmp, &c->prompt_buffer[idx - 2]);
                   1227:                        utf8_copy(&c->prompt_buffer[idx - 2],
                   1228:                            &c->prompt_buffer[idx - 1]);
                   1229:                        utf8_copy(&c->prompt_buffer[idx - 1], &tmp);
1.30      nicm     1230:                        c->prompt_index = idx;
1.158     nicm     1231:                        goto changed;
1.30      nicm     1232:                }
1.1       nicm     1233:                break;
1.155     nicm     1234:        case '\r':
                   1235:        case '\n':
1.152     nicm     1236:                s = utf8_tocstr(c->prompt_buffer);
                   1237:                if (*s != '\0')
                   1238:                        status_prompt_add_history(s);
1.166     nicm     1239:                if (c->prompt_inputcb(c, c->prompt_data, s, 1) == 0)
1.25      nicm     1240:                        status_prompt_clear(c);
1.152     nicm     1241:                free(s);
1.25      nicm     1242:                break;
1.155     nicm     1243:        case '\033': /* Escape */
                   1244:        case '\003': /* C-c */
1.174     nicm     1245:        case '\007': /* C-g */
1.166     nicm     1246:                if (c->prompt_inputcb(c, c->prompt_data, NULL, 1) == 0)
1.1       nicm     1247:                        status_prompt_clear(c);
                   1248:                break;
1.158     nicm     1249:        case '\022': /* C-r */
                   1250:                if (c->prompt_flags & PROMPT_INCREMENTAL) {
                   1251:                        prefix = '-';
                   1252:                        goto changed;
                   1253:                }
                   1254:                break;
                   1255:        case '\023': /* C-s */
                   1256:                if (c->prompt_flags & PROMPT_INCREMENTAL) {
                   1257:                        prefix = '+';
                   1258:                        goto changed;
                   1259:                }
                   1260:                break;
                   1261:        default:
                   1262:                goto append_key;
1.154     nicm     1263:        }
1.152     nicm     1264:
1.177     nicm     1265:        c->flags |= CLIENT_REDRAWSTATUS;
1.158     nicm     1266:        return (0);
                   1267:
1.154     nicm     1268: append_key:
                   1269:        if (key <= 0x1f || key >= KEYC_BASE)
                   1270:                return (0);
                   1271:        if (utf8_split(key, &tmp) != UTF8_DONE)
                   1272:                return (0);
1.1       nicm     1273:
1.154     nicm     1274:        c->prompt_buffer = xreallocarray(c->prompt_buffer, size + 2,
                   1275:            sizeof *c->prompt_buffer);
1.1       nicm     1276:
1.154     nicm     1277:        if (c->prompt_index == size) {
                   1278:                utf8_copy(&c->prompt_buffer[c->prompt_index], &tmp);
                   1279:                c->prompt_index++;
                   1280:                c->prompt_buffer[c->prompt_index].size = 0;
                   1281:        } else {
                   1282:                memmove(c->prompt_buffer + c->prompt_index + 1,
                   1283:                    c->prompt_buffer + c->prompt_index,
                   1284:                    (size + 1 - c->prompt_index) *
                   1285:                    sizeof *c->prompt_buffer);
                   1286:                utf8_copy(&c->prompt_buffer[c->prompt_index], &tmp);
                   1287:                c->prompt_index++;
                   1288:        }
1.1       nicm     1289:
1.154     nicm     1290:        if (c->prompt_flags & PROMPT_SINGLE) {
                   1291:                s = utf8_tocstr(c->prompt_buffer);
                   1292:                if (strlen(s) != 1)
                   1293:                        status_prompt_clear(c);
1.166     nicm     1294:                else if (c->prompt_inputcb(c, c->prompt_data, s, 1) == 0)
1.154     nicm     1295:                        status_prompt_clear(c);
                   1296:                free(s);
1.1       nicm     1297:        }
1.154     nicm     1298:
1.158     nicm     1299: changed:
1.177     nicm     1300:        c->flags |= CLIENT_REDRAWSTATUS;
1.158     nicm     1301:        if (c->prompt_flags & PROMPT_INCREMENTAL) {
                   1302:                s = utf8_tocstr(c->prompt_buffer);
                   1303:                xasprintf(&cp, "%c%s", prefix, s);
1.166     nicm     1304:                c->prompt_inputcb(c, c->prompt_data, cp, 0);
1.158     nicm     1305:                free(cp);
                   1306:                free(s);
                   1307:        }
1.154     nicm     1308:        return (0);
1.1       nicm     1309: }
                   1310:
1.65      nicm     1311: /* Get previous line from the history. */
1.151     nicm     1312: static const char *
1.65      nicm     1313: status_prompt_up_history(u_int *idx)
                   1314: {
                   1315:        /*
1.128     nicm     1316:         * History runs from 0 to size - 1. Index is from 0 to size. Zero is
                   1317:         * empty.
1.65      nicm     1318:         */
                   1319:
1.128     nicm     1320:        if (status_prompt_hsize == 0 || *idx == status_prompt_hsize)
1.65      nicm     1321:                return (NULL);
                   1322:        (*idx)++;
1.128     nicm     1323:        return (status_prompt_hlist[status_prompt_hsize - *idx]);
1.65      nicm     1324: }
                   1325:
                   1326: /* Get next line from the history. */
1.151     nicm     1327: static const char *
1.65      nicm     1328: status_prompt_down_history(u_int *idx)
                   1329: {
1.128     nicm     1330:        if (status_prompt_hsize == 0 || *idx == 0)
1.65      nicm     1331:                return ("");
                   1332:        (*idx)--;
                   1333:        if (*idx == 0)
                   1334:                return ("");
1.128     nicm     1335:        return (status_prompt_hlist[status_prompt_hsize - *idx]);
1.65      nicm     1336: }
                   1337:
1.1       nicm     1338: /* Add line to the history. */
1.151     nicm     1339: static void
1.65      nicm     1340: status_prompt_add_history(const char *line)
1.1       nicm     1341: {
1.128     nicm     1342:        size_t  size;
1.65      nicm     1343:
1.128     nicm     1344:        if (status_prompt_hsize > 0 &&
                   1345:            strcmp(status_prompt_hlist[status_prompt_hsize - 1], line) == 0)
1.1       nicm     1346:                return;
                   1347:
1.128     nicm     1348:        if (status_prompt_hsize == PROMPT_HISTORY) {
                   1349:                free(status_prompt_hlist[0]);
1.1       nicm     1350:
1.128     nicm     1351:                size = (PROMPT_HISTORY - 1) * sizeof *status_prompt_hlist;
                   1352:                memmove(&status_prompt_hlist[0], &status_prompt_hlist[1], size);
1.1       nicm     1353:
1.128     nicm     1354:                status_prompt_hlist[status_prompt_hsize - 1] = xstrdup(line);
                   1355:                return;
                   1356:        }
1.1       nicm     1357:
1.128     nicm     1358:        status_prompt_hlist = xreallocarray(status_prompt_hlist,
                   1359:            status_prompt_hsize + 1, sizeof *status_prompt_hlist);
                   1360:        status_prompt_hlist[status_prompt_hsize++] = xstrdup(line);
                   1361: }
                   1362:
                   1363: /* Build completion list. */
1.204     nicm     1364: static char **
                   1365: status_prompt_complete_list(u_int *size, const char *s, int at_start)
1.128     nicm     1366: {
1.183     nicm     1367:        char                                    **list = NULL;
                   1368:        const char                              **layout, *value, *cp;
1.128     nicm     1369:        const struct cmd_entry                  **cmdent;
                   1370:        const struct options_table_entry         *oe;
1.183     nicm     1371:        size_t                                    slen = strlen(s), valuelen;
                   1372:        struct options_entry                     *o;
1.191     nicm     1373:        struct options_array_item                *a;
1.128     nicm     1374:        const char                               *layouts[] = {
                   1375:                "even-horizontal", "even-vertical", "main-horizontal",
                   1376:                "main-vertical", "tiled", NULL
                   1377:        };
1.1       nicm     1378:
1.128     nicm     1379:        *size = 0;
1.1       nicm     1380:        for (cmdent = cmd_table; *cmdent != NULL; cmdent++) {
1.183     nicm     1381:                if (strncmp((*cmdent)->name, s, slen) == 0) {
1.128     nicm     1382:                        list = xreallocarray(list, (*size) + 1, sizeof *list);
1.183     nicm     1383:                        list[(*size)++] = xstrdup((*cmdent)->name);
1.128     nicm     1384:                }
1.205     nicm     1385:                if ((*cmdent)->alias != NULL &&
                   1386:                    strncmp((*cmdent)->alias, s, slen) == 0) {
                   1387:                        list = xreallocarray(list, (*size) + 1, sizeof *list);
                   1388:                        list[(*size)++] = xstrdup((*cmdent)->alias);
                   1389:                }
1.56      nicm     1390:        }
1.183     nicm     1391:        o = options_get_only(global_options, "command-alias");
1.191     nicm     1392:        if (o != NULL) {
                   1393:                a = options_array_first(o);
                   1394:                while (a != NULL) {
1.195     nicm     1395:                        value = options_array_item_value(a)->string;
1.194     nicm     1396:                        if ((cp = strchr(value, '=')) == NULL)
1.196     nicm     1397:                                goto next;
1.183     nicm     1398:                        valuelen = cp - value;
                   1399:                        if (slen > valuelen || strncmp(value, s, slen) != 0)
1.191     nicm     1400:                                goto next;
                   1401:
1.183     nicm     1402:                        list = xreallocarray(list, (*size) + 1, sizeof *list);
                   1403:                        list[(*size)++] = xstrndup(value, valuelen);
1.191     nicm     1404:
                   1405:                next:
                   1406:                        a = options_array_next(a);
1.183     nicm     1407:                }
                   1408:        }
1.204     nicm     1409:        if (at_start)
                   1410:                return (list);
                   1411:
                   1412:        for (oe = options_table; oe->name != NULL; oe++) {
                   1413:                if (strncmp(oe->name, s, slen) == 0) {
                   1414:                        list = xreallocarray(list, (*size) + 1, sizeof *list);
                   1415:                        list[(*size)++] = xstrdup(oe->name);
                   1416:                }
                   1417:        }
                   1418:        for (layout = layouts; *layout != NULL; layout++) {
                   1419:                if (strncmp(*layout, s, slen) == 0) {
                   1420:                        list = xreallocarray(list, (*size) + 1, sizeof *list);
                   1421:                        list[(*size)++] = xstrdup(*layout);
                   1422:                }
                   1423:        }
1.128     nicm     1424:        return (list);
                   1425: }
                   1426:
                   1427: /* Find longest prefix. */
1.151     nicm     1428: static char *
1.183     nicm     1429: status_prompt_complete_prefix(char **list, u_int size)
1.128     nicm     1430: {
                   1431:        char     *out;
                   1432:        u_int     i;
                   1433:        size_t    j;
                   1434:
                   1435:        out = xstrdup(list[0]);
                   1436:        for (i = 1; i < size; i++) {
                   1437:                j = strlen(list[i]);
                   1438:                if (j > strlen(out))
                   1439:                        j = strlen(out);
                   1440:                for (; j > 0; j--) {
                   1441:                        if (out[j - 1] != list[i][j - 1])
                   1442:                                out[j - 1] = '\0';
                   1443:                }
1.1       nicm     1444:        }
1.128     nicm     1445:        return (out);
                   1446: }
1.1       nicm     1447:
1.204     nicm     1448: /* Complete word menu callback. */
                   1449: static void
                   1450: status_prompt_menu_callback(__unused struct menu *menu, u_int idx, key_code key,
                   1451:     void *data)
                   1452: {
                   1453:        struct status_prompt_menu       *spm = data;
                   1454:        struct client                   *c = spm->c;
                   1455:        u_int                            i;
                   1456:        char                            *s;
                   1457:
                   1458:        if (key != KEYC_NONE) {
                   1459:                idx += spm->start;
                   1460:                if (spm->flag == '\0')
                   1461:                        s = xstrdup(spm->list[idx]);
                   1462:                else
                   1463:                        xasprintf(&s, "-%c%s", spm->flag, spm->list[idx]);
1.205     nicm     1464:                if (c->prompt_flags & PROMPT_WINDOW) {
                   1465:                        free(c->prompt_buffer);
                   1466:                        c->prompt_buffer = utf8_fromcstr(s);
                   1467:                        c->prompt_index = utf8_strlen(c->prompt_buffer);
                   1468:                        c->flags |= CLIENT_REDRAWSTATUS;
                   1469:                } else if (status_prompt_replace_complete(c, s))
1.204     nicm     1470:                        c->flags |= CLIENT_REDRAWSTATUS;
                   1471:                free(s);
                   1472:        }
                   1473:
                   1474:        for (i = 0; i < spm->size; i++)
                   1475:                free(spm->list[i]);
                   1476:        free(spm->list);
                   1477: }
                   1478:
                   1479: /* Show complete word menu. */
                   1480: static int
                   1481: status_prompt_complete_list_menu(struct client *c, char **list, u_int size,
                   1482:     u_int offset, char flag)
                   1483: {
                   1484:        struct menu                     *menu;
                   1485:        struct menu_item                 item;
                   1486:        struct status_prompt_menu       *spm;
                   1487:        u_int                            lines = status_line_size(c), height, i;
                   1488:        u_int                            py;
                   1489:
                   1490:        if (size <= 1)
                   1491:                return (0);
                   1492:        if (c->tty.sy - lines < 3)
                   1493:                return (0);
                   1494:
                   1495:        spm = xmalloc(sizeof *spm);
                   1496:        spm->c = c;
                   1497:        spm->size = size;
                   1498:        spm->list = list;
                   1499:        spm->flag = flag;
                   1500:
                   1501:        height = c->tty.sy - lines - 2;
                   1502:        if (height > 10)
                   1503:                height = 10;
                   1504:        if (height > size)
                   1505:                height = size;
                   1506:        spm->start = size - height;
                   1507:
                   1508:        menu = menu_create("");
                   1509:        for (i = spm->start; i < size; i++) {
                   1510:                item.name = list[i];
                   1511:                item.key = '0' + (i - spm->start);
                   1512:                item.command = NULL;
                   1513:                menu_add_item(menu, &item, NULL, NULL, NULL);
                   1514:        }
                   1515:
                   1516:        if (options_get_number(c->session->options, "status-position") == 0)
                   1517:                py = lines;
                   1518:        else
                   1519:                py = c->tty.sy - 3 - height;
                   1520:        offset += utf8_cstrwidth(c->prompt_string);
                   1521:        if (offset > 2)
                   1522:                offset -= 2;
                   1523:        else
                   1524:                offset = 0;
                   1525:
                   1526:        if (menu_display(menu, MENU_NOMOUSE|MENU_TAB, NULL, offset,
                   1527:            py, c, NULL, status_prompt_menu_callback, spm) != 0) {
                   1528:                menu_free(menu);
                   1529:                free(spm);
                   1530:                return (0);
                   1531:        }
                   1532:        return (1);
                   1533: }
                   1534:
                   1535: /* Show complete word menu. */
                   1536: static char *
                   1537: status_prompt_complete_window_menu(struct client *c, struct session *s,
1.206     nicm     1538:     const char *word, u_int offset, char flag)
1.204     nicm     1539: {
                   1540:        struct menu                      *menu;
                   1541:        struct menu_item                  item;
                   1542:        struct status_prompt_menu        *spm;
                   1543:        struct winlink                   *wl;
                   1544:        char                            **list = NULL, *tmp;
                   1545:        u_int                             lines = status_line_size(c), height;
                   1546:        u_int                             py, size = 0;
                   1547:
                   1548:        if (c->tty.sy - lines < 3)
                   1549:                return (NULL);
                   1550:
                   1551:        spm = xmalloc(sizeof *spm);
                   1552:        spm->c = c;
                   1553:        spm->flag = flag;
                   1554:
                   1555:        height = c->tty.sy - lines - 2;
                   1556:        if (height > 10)
                   1557:                height = 10;
                   1558:        spm->start = 0;
                   1559:
                   1560:        menu = menu_create("");
                   1561:        RB_FOREACH(wl, winlinks, &s->windows) {
1.206     nicm     1562:                if (word != NULL && *word != '\0') {
                   1563:                        xasprintf(&tmp, "%d", wl->idx);
                   1564:                        if (strncmp(tmp, word, strlen(word)) != 0) {
                   1565:                                free(tmp);
                   1566:                                continue;
                   1567:                        }
                   1568:                        free(tmp);
                   1569:                }
                   1570:
1.204     nicm     1571:                list = xreallocarray(list, size + 1, sizeof *list);
1.205     nicm     1572:                if (c->prompt_flags & PROMPT_WINDOW) {
                   1573:                        xasprintf(&tmp, "%d (%s)", wl->idx, wl->window->name);
                   1574:                        xasprintf(&list[size++], "%d", wl->idx);
                   1575:                } else {
                   1576:                        xasprintf(&tmp, "%s:%d (%s)", s->name, wl->idx,
                   1577:                            wl->window->name);
                   1578:                        xasprintf(&list[size++], "%s:%d", s->name, wl->idx);
                   1579:                }
1.204     nicm     1580:                item.name = tmp;
                   1581:                item.key = '0' + size - 1;
                   1582:                item.command = NULL;
                   1583:                menu_add_item(menu, &item, NULL, NULL, NULL);
                   1584:                free(tmp);
                   1585:
                   1586:                if (size == height)
                   1587:                        break;
                   1588:        }
1.206     nicm     1589:        if (size == 0) {
                   1590:                menu_free(menu);
                   1591:                return (NULL);
                   1592:        }
1.204     nicm     1593:        if (size == 1) {
                   1594:                menu_free(menu);
1.205     nicm     1595:                if (flag != '\0') {
                   1596:                        xasprintf(&tmp, "-%c%s", flag, list[0]);
                   1597:                        free(list[0]);
                   1598:                } else
                   1599:                        tmp = list[0];
1.204     nicm     1600:                free(list);
                   1601:                return (tmp);
                   1602:        }
                   1603:        if (height > size)
                   1604:                height = size;
                   1605:
                   1606:        spm->size = size;
                   1607:        spm->list = list;
                   1608:
                   1609:        if (options_get_number(c->session->options, "status-position") == 0)
                   1610:                py = lines;
                   1611:        else
                   1612:                py = c->tty.sy - 3 - height;
                   1613:        offset += utf8_cstrwidth(c->prompt_string);
                   1614:        if (offset > 2)
                   1615:                offset -= 2;
                   1616:        else
                   1617:                offset = 0;
                   1618:
                   1619:        if (menu_display(menu, MENU_NOMOUSE|MENU_TAB, NULL, offset,
                   1620:            py, c, NULL, status_prompt_menu_callback, spm) != 0) {
                   1621:                menu_free(menu);
                   1622:                free(spm);
                   1623:                return (NULL);
                   1624:        }
                   1625:        return (NULL);
                   1626: }
                   1627:
                   1628: /* Sort complete list. */
                   1629: static int
                   1630: status_prompt_complete_sort(const void *a, const void *b)
                   1631: {
                   1632:        const char      **aa = (const char **)a, **bb = (const char **)b;
                   1633:
                   1634:        return (strcmp(*aa, *bb));
                   1635: }
                   1636:
1.205     nicm     1637: /* Complete a session. */
                   1638: static char *
                   1639: status_prompt_complete_session(char ***list, u_int *size, const char *s,
                   1640:     char flag)
                   1641: {
                   1642:        struct session  *loop;
                   1643:        char            *out, *tmp;
                   1644:
                   1645:        RB_FOREACH(loop, sessions, &sessions) {
                   1646:                if (*s != '\0' && strncmp(loop->name, s, strlen(s)) != 0)
                   1647:                        continue;
                   1648:                *list = xreallocarray(*list, (*size) + 2, sizeof **list);
                   1649:                xasprintf(&(*list)[(*size)++], "%s:", loop->name);
                   1650:        }
                   1651:        out = status_prompt_complete_prefix(*list, *size);
                   1652:        if (out != NULL && flag != '\0') {
                   1653:                xasprintf(&tmp, "-%c%s", flag, out);
                   1654:                free(out);
                   1655:                out = tmp;
                   1656:        }
                   1657:        return (out);
                   1658: }
                   1659:
1.128     nicm     1660: /* Complete word. */
1.151     nicm     1661: static char *
1.204     nicm     1662: status_prompt_complete(struct client *c, const char *word, u_int offset)
1.128     nicm     1663: {
1.205     nicm     1664:        struct session   *session;
1.204     nicm     1665:        const char       *s, *colon;
1.205     nicm     1666:        char            **list = NULL, *copy = NULL, *out = NULL;
1.204     nicm     1667:        char              flag = '\0';
1.128     nicm     1668:        u_int             size = 0, i;
                   1669:
1.206     nicm     1670:        if (*word == '\0' &&
                   1671:            ((c->prompt_flags & (PROMPT_TARGET|PROMPT_WINDOW)) == 0))
1.1       nicm     1672:                return (NULL);
1.128     nicm     1673:
1.206     nicm     1674:        if (((c->prompt_flags & (PROMPT_TARGET|PROMPT_WINDOW)) == 0) &&
1.205     nicm     1675:            strncmp(word, "-t", 2) != 0 &&
                   1676:            strncmp(word, "-s", 2) != 0) {
1.204     nicm     1677:                list = status_prompt_complete_list(&size, word, offset == 0);
1.128     nicm     1678:                if (size == 0)
                   1679:                        out = NULL;
                   1680:                else if (size == 1)
                   1681:                        xasprintf(&out, "%s ", list[0]);
                   1682:                else
                   1683:                        out = status_prompt_complete_prefix(list, size);
1.204     nicm     1684:                goto found;
1.128     nicm     1685:        }
                   1686:
1.206     nicm     1687:        if (c->prompt_flags & (PROMPT_TARGET|PROMPT_WINDOW)) {
1.205     nicm     1688:                s = word;
                   1689:                flag = '\0';
                   1690:        } else {
                   1691:                s = word + 2;
                   1692:                flag = word[1];
                   1693:                offset += 2;
                   1694:        }
1.206     nicm     1695:
                   1696:        /* If this is a window completion, open the window menu. */
                   1697:        if (c->prompt_flags & PROMPT_WINDOW) {
                   1698:                out = status_prompt_complete_window_menu(c, c->session, s,
                   1699:                    offset, '\0');
                   1700:                goto found;
                   1701:        }
1.204     nicm     1702:        colon = strchr(s, ':');
1.1       nicm     1703:
1.204     nicm     1704:        /* If there is no colon, complete as a session. */
                   1705:        if (colon == NULL) {
1.205     nicm     1706:                out = status_prompt_complete_session(&list, &size, s, flag);
1.128     nicm     1707:                goto found;
                   1708:        }
                   1709:
1.204     nicm     1710:        /* If there is a colon but no period, find session and show a menu. */
                   1711:        if (strchr(colon + 1, '.') == NULL) {
                   1712:                if (*s == ':')
                   1713:                        session = c->session;
                   1714:                else {
                   1715:                        copy = xstrdup(s);
                   1716:                        *strchr(copy, ':') = '\0';
                   1717:                        session = session_find(copy);
                   1718:                        free(copy);
                   1719:                        if (session == NULL)
                   1720:                                goto found;
                   1721:                }
1.206     nicm     1722:                out = status_prompt_complete_window_menu(c, session, colon + 1,
                   1723:                    offset, flag);
1.204     nicm     1724:                if (out == NULL)
                   1725:                        return (NULL);
                   1726:        }
1.1       nicm     1727:
1.204     nicm     1728: found:
                   1729:        if (size != 0) {
                   1730:                qsort(list, size, sizeof *list, status_prompt_complete_sort);
                   1731:                for (i = 0; i < size; i++)
                   1732:                        log_debug("complete %u: %s", i, list[i]);
                   1733:        }
1.128     nicm     1734:
1.204     nicm     1735:        if (out != NULL && strcmp(word, out) == 0) {
                   1736:                free(out);
                   1737:                out = NULL;
1.1       nicm     1738:        }
1.204     nicm     1739:        if (out != NULL ||
                   1740:            !status_prompt_complete_list_menu(c, list, size, offset, flag)) {
                   1741:                for (i = 0; i < size; i++)
                   1742:                        free(list[i]);
                   1743:                free(list);
1.128     nicm     1744:        }
                   1745:        return (out);
1.1       nicm     1746: }