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

1.185   ! nicm        1: /* $OpenBSD: status.c,v 1.184 2019/03/12 20:02:47 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.185   ! nicm       32: static char    *status_redraw_get_left(struct client *, struct grid_cell *,
        !            33:                     size_t *);
        !            34: static char    *status_redraw_get_right(struct client *, struct grid_cell *,
        !            35:                     size_t *);
        !            36: static char    *status_print(struct client *, struct winlink *,
1.151     nicm       37:                     struct grid_cell *);
1.185   ! nicm       38: static char    *status_replace(struct client *, struct winlink *,
        !            39:                     const char *);
1.151     nicm       40: static void     status_message_callback(int, short, void *);
                     41: static void     status_timer_callback(int, short, void *);
                     42:
                     43: static char    *status_prompt_find_history_file(void);
                     44: static const char *status_prompt_up_history(u_int *);
                     45: static const char *status_prompt_down_history(u_int *);
                     46: static void     status_prompt_add_history(const char *);
                     47:
1.183     nicm       48: static char    **status_prompt_complete_list(u_int *, const char *);
                     49: static char    *status_prompt_complete_prefix(char **, u_int);
1.151     nicm       50: static char    *status_prompt_complete(struct session *, const char *);
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
                    197: status_update_saved(struct session *s)
                    198: {
                    199:        if (!options_get_number(s->options, "status"))
                    200:                s->statusat = -1;
                    201:        else if (options_get_number(s->options, "status-position") == 0)
                    202:                s->statusat = 0;
                    203:        else
                    204:                s->statusat = 1;
                    205: }
                    206:
1.87      nicm      207: /* Get screen line of status line. -1 means off. */
                    208: int
                    209: status_at_line(struct client *c)
                    210: {
                    211:        struct session  *s = c->session;
                    212:
1.169     nicm      213:        if (c->flags & CLIENT_STATUSOFF)
                    214:                return (-1);
1.161     nicm      215:        if (s->statusat != 1)
                    216:                return (s->statusat);
1.182     nicm      217:        return (c->tty.sy - status_line_size(c));
1.169     nicm      218: }
                    219:
1.182     nicm      220: /* Get size of status line for client's session. 0 means off. */
1.169     nicm      221: u_int
1.182     nicm      222: status_line_size(struct client *c)
1.169     nicm      223: {
1.182     nicm      224:        struct session  *s = c->session;
                    225:
                    226:        if (c->flags & CLIENT_STATUSOFF)
                    227:                return (0);
1.169     nicm      228:        if (s->statusat == -1)
                    229:                return (0);
                    230:        return (1);
1.71      nicm      231: }
                    232:
1.48      nicm      233: /* Retrieve options for left string. */
1.151     nicm      234: static char *
1.185   ! nicm      235: status_redraw_get_left(struct client *c, struct grid_cell *gc, size_t *size)
1.48      nicm      236: {
                    237:        struct session  *s = c->session;
1.120     nicm      238:        const char      *template;
1.48      nicm      239:        char            *left;
                    240:        size_t           leftlen;
                    241:
1.137     nicm      242:        style_apply_update(gc, s->options, "status-left-style");
1.48      nicm      243:
1.137     nicm      244:        template = options_get_string(s->options, "status-left");
1.185   ! nicm      245:        left = status_replace(c, NULL, template);
1.48      nicm      246:
1.137     nicm      247:        *size = options_get_number(s->options, "status-left-length");
1.139     nicm      248:        leftlen = screen_write_cstrlen("%s", left);
1.48      nicm      249:        if (leftlen < *size)
                    250:                *size = leftlen;
                    251:        return (left);
                    252: }
                    253:
                    254: /* Retrieve options for right string. */
1.151     nicm      255: static char *
1.185   ! nicm      256: status_redraw_get_right(struct client *c, struct grid_cell *gc, size_t *size)
1.48      nicm      257: {
                    258:        struct session  *s = c->session;
1.120     nicm      259:        const char      *template;
1.48      nicm      260:        char            *right;
                    261:        size_t           rightlen;
                    262:
1.137     nicm      263:        style_apply_update(gc, s->options, "status-right-style");
1.48      nicm      264:
1.137     nicm      265:        template = options_get_string(s->options, "status-right");
1.185   ! nicm      266:        right = status_replace(c, NULL, template);
1.48      nicm      267:
1.137     nicm      268:        *size = options_get_number(s->options, "status-right-length");
1.139     nicm      269:        rightlen = screen_write_cstrlen("%s", right);
1.48      nicm      270:        if (rightlen < *size)
                    271:                *size = rightlen;
                    272:        return (right);
                    273: }
                    274:
1.125     nicm      275: /* Get window at window list position. */
                    276: struct window *
                    277: status_get_window_at(struct client *c, u_int x)
1.73      nicm      278: {
                    279:        struct session  *s = c->session;
                    280:        struct winlink  *wl;
1.115     nicm      281:        struct options  *oo;
1.149     nicm      282:        const char      *sep;
                    283:        size_t           seplen;
1.73      nicm      284:
1.178     nicm      285:        x += c->status.window_list_offset;
1.73      nicm      286:        RB_FOREACH(wl, winlinks, &s->windows) {
1.137     nicm      287:                oo = wl->window->options;
1.149     nicm      288:
                    289:                sep = options_get_string(oo, "window-status-separator");
                    290:                seplen = screen_write_cstrlen("%s", sep);
1.115     nicm      291:
1.125     nicm      292:                if (x < wl->status_width)
                    293:                        return (wl->window);
1.149     nicm      294:                x -= wl->status_width + seplen;
1.73      nicm      295:        }
1.125     nicm      296:        return (NULL);
1.73      nicm      297: }
                    298:
1.1       nicm      299: /* Draw status for client on the last lines of given context. */
                    300: int
                    301: status_redraw(struct client *c)
                    302: {
1.159     nicm      303:        struct screen_write_ctx  ctx;
                    304:        struct session          *s = c->session;
                    305:        struct winlink          *wl;
                    306:        struct screen            old_status, window_list;
                    307:        struct grid_cell         stdgc, lgc, rgc, gc;
                    308:        struct options          *oo;
                    309:        char                    *left, *right;
                    310:        const char              *sep;
1.169     nicm      311:        u_int                    offset, needed, lines;
1.159     nicm      312:        u_int                    wlstart, wlwidth, wlavailable, wloffset, wlsize;
                    313:        size_t                   llen, rlen, seplen;
                    314:        int                      larrow, rarrow;
1.1       nicm      315:
1.167     nicm      316:        /* Delete the saved status line, if any. */
1.175     nicm      317:        if (c->status.old_status != NULL) {
                    318:                screen_free(c->status.old_status);
                    319:                free(c->status.old_status);
                    320:                c->status.old_status = NULL;
1.167     nicm      321:        }
                    322:
1.49      nicm      323:        /* No status line? */
1.182     nicm      324:        lines = status_line_size(c);
1.169     nicm      325:        if (c->tty.sy == 0 || lines == 0)
1.7       nicm      326:                return (1);
1.48      nicm      327:        left = right = NULL;
1.7       nicm      328:        larrow = rarrow = 0;
1.1       nicm      329:
1.48      nicm      330:        /* Set up default colour. */
1.137     nicm      331:        style_apply(&stdgc, s->options, "status-style");
1.1       nicm      332:
1.48      nicm      333:        /* Create the target screen. */
1.175     nicm      334:        memcpy(&old_status, &c->status.status, sizeof old_status);
                    335:        screen_init(&c->status.status, c->tty.sx, lines, 0);
                    336:        screen_write_start(&ctx, NULL, &c->status.status);
1.170     nicm      337:        for (offset = 0; offset < lines * c->tty.sx; offset++)
                    338:                screen_write_putc(&ctx, &stdgc, ' ');
1.48      nicm      339:        screen_write_stop(&ctx);
                    340:
1.169     nicm      341:        /* If the height is too small, blank status line. */
                    342:        if (c->tty.sy < lines)
1.48      nicm      343:                goto out;
1.1       nicm      344:
1.48      nicm      345:        /* Work out left and right strings. */
                    346:        memcpy(&lgc, &stdgc, sizeof lgc);
1.185   ! nicm      347:        left = status_redraw_get_left(c, &lgc, &llen);
1.48      nicm      348:        memcpy(&rgc, &stdgc, sizeof rgc);
1.185   ! nicm      349:        right = status_redraw_get_right(c, &rgc, &rlen);
1.1       nicm      350:
                    351:        /*
1.48      nicm      352:         * Figure out how much space we have for the window list. If there
                    353:         * isn't enough space, just show a blank status line.
1.1       nicm      354:         */
1.55      nicm      355:        needed = 0;
1.1       nicm      356:        if (llen != 0)
1.118     nicm      357:                needed += llen;
1.1       nicm      358:        if (rlen != 0)
1.118     nicm      359:                needed += rlen;
1.48      nicm      360:        if (c->tty.sx == 0 || c->tty.sx <= needed)
                    361:                goto out;
                    362:        wlavailable = c->tty.sx - needed;
1.1       nicm      363:
1.48      nicm      364:        /* Calculate the total size needed for the window list. */
                    365:        wlstart = wloffset = wlwidth = 0;
1.1       nicm      366:        RB_FOREACH(wl, winlinks, &s->windows) {
1.94      nicm      367:                free(wl->status_text);
1.48      nicm      368:                memcpy(&wl->status_cell, &stdgc, sizeof wl->status_cell);
1.185   ! nicm      369:                wl->status_text = status_print(c, wl, &wl->status_cell);
1.139     nicm      370:                wl->status_width = screen_write_cstrlen("%s", wl->status_text);
1.48      nicm      371:
1.1       nicm      372:                if (wl == s->curw)
1.48      nicm      373:                        wloffset = wlwidth;
1.91      nicm      374:
1.137     nicm      375:                oo = wl->window->options;
1.91      nicm      376:                sep = options_get_string(oo, "window-status-separator");
1.149     nicm      377:                seplen = screen_write_cstrlen("%s", sep);
1.91      nicm      378:                wlwidth += wl->status_width + seplen;
1.48      nicm      379:        }
                    380:
                    381:        /* Create a new screen for the window list. */
                    382:        screen_init(&window_list, wlwidth, 1, 0);
                    383:
                    384:        /* And draw the window list into it. */
                    385:        screen_write_start(&ctx, NULL, &window_list);
                    386:        RB_FOREACH(wl, winlinks, &s->windows) {
1.139     nicm      387:                screen_write_cnputs(&ctx, -1, &wl->status_cell, "%s",
                    388:                    wl->status_text);
1.91      nicm      389:
1.137     nicm      390:                oo = wl->window->options;
1.91      nicm      391:                sep = options_get_string(oo, "window-status-separator");
1.149     nicm      392:                screen_write_cnputs(&ctx, -1, &stdgc, "%s", sep);
1.1       nicm      393:        }
1.48      nicm      394:        screen_write_stop(&ctx);
1.1       nicm      395:
1.48      nicm      396:        /* If there is enough space for the total width, skip to draw now. */
                    397:        if (wlwidth <= wlavailable)
1.1       nicm      398:                goto draw;
                    399:
                    400:        /* Find size of current window text. */
1.48      nicm      401:        wlsize = s->curw->status_width;
1.1       nicm      402:
                    403:        /*
1.48      nicm      404:         * If the current window is already on screen, good to draw from the
1.1       nicm      405:         * start and just leave off the end.
                    406:         */
1.48      nicm      407:        if (wloffset + wlsize < wlavailable) {
                    408:                if (wlavailable > 0) {
                    409:                        rarrow = 1;
                    410:                        wlavailable--;
                    411:                }
                    412:                wlwidth = wlavailable;
                    413:        } else {
                    414:                /*
                    415:                 * Work out how many characters we need to omit from the
                    416:                 * start. There are wlavailable characters to fill, and
                    417:                 * wloffset + wlsize must be the last. So, the start character
                    418:                 * is wloffset + wlsize - wlavailable.
                    419:                 */
                    420:                if (wlavailable > 0) {
                    421:                        larrow = 1;
                    422:                        wlavailable--;
                    423:                }
1.55      nicm      424:
1.48      nicm      425:                wlstart = wloffset + wlsize - wlavailable;
                    426:                if (wlavailable > 0 && wlwidth > wlstart + wlavailable + 1) {
1.1       nicm      427:                        rarrow = 1;
1.48      nicm      428:                        wlstart++;
                    429:                        wlavailable--;
1.1       nicm      430:                }
1.48      nicm      431:                wlwidth = wlavailable;
                    432:        }
1.1       nicm      433:
1.48      nicm      434:        /* Bail if anything is now too small too. */
                    435:        if (wlwidth == 0 || wlavailable == 0) {
                    436:                screen_free(&window_list);
                    437:                goto out;
1.1       nicm      438:        }
                    439:
                    440:        /*
1.48      nicm      441:         * Now the start position is known, work out the state of the left and
                    442:         * right arrows.
1.1       nicm      443:         */
                    444:        offset = 0;
                    445:        RB_FOREACH(wl, winlinks, &s->windows) {
1.63      nicm      446:                if (wl->flags & WINLINK_ALERTFLAGS &&
                    447:                    larrow == 1 && offset < wlstart)
                    448:                        larrow = -1;
1.1       nicm      449:
1.48      nicm      450:                offset += wl->status_width;
1.1       nicm      451:
1.63      nicm      452:                if (wl->flags & WINLINK_ALERTFLAGS &&
                    453:                    rarrow == 1 && offset > wlstart + wlwidth)
                    454:                        rarrow = -1;
1.1       nicm      455:        }
                    456:
1.48      nicm      457: draw:
1.55      nicm      458:        /* Begin drawing. */
1.175     nicm      459:        screen_write_start(&ctx, NULL, &c->status.status);
1.1       nicm      460:
1.48      nicm      461:        /* Draw the left string and arrow. */
1.184     nicm      462:        screen_write_cursormove(&ctx, 0, 0, 0);
1.118     nicm      463:        if (llen != 0)
1.139     nicm      464:                screen_write_cnputs(&ctx, llen, &lgc, "%s", left);
1.1       nicm      465:        if (larrow != 0) {
                    466:                memcpy(&gc, &stdgc, sizeof gc);
                    467:                if (larrow == -1)
                    468:                        gc.attr ^= GRID_ATTR_REVERSE;
                    469:                screen_write_putc(&ctx, &gc, '<');
                    470:        }
1.48      nicm      471:
                    472:        /* Draw the right string and arrow. */
1.1       nicm      473:        if (rarrow != 0) {
1.184     nicm      474:                screen_write_cursormove(&ctx, c->tty.sx - rlen - 1, 0, 0);
1.1       nicm      475:                memcpy(&gc, &stdgc, sizeof gc);
                    476:                if (rarrow == -1)
                    477:                        gc.attr ^= GRID_ATTR_REVERSE;
                    478:                screen_write_putc(&ctx, &gc, '>');
1.48      nicm      479:        } else
1.184     nicm      480:                screen_write_cursormove(&ctx, c->tty.sx - rlen, 0, 0);
1.118     nicm      481:        if (rlen != 0)
1.139     nicm      482:                screen_write_cnputs(&ctx, rlen, &rgc, "%s", right);
1.1       nicm      483:
1.48      nicm      484:        /* Figure out the offset for the window list. */
1.58      nicm      485:        if (llen != 0)
1.118     nicm      486:                wloffset = llen;
1.58      nicm      487:        else
                    488:                wloffset = 0;
1.48      nicm      489:        if (wlwidth < wlavailable) {
1.137     nicm      490:                switch (options_get_number(s->options, "status-justify")) {
1.119     sthen     491:                case 1: /* centred */
1.58      nicm      492:                        wloffset += (wlavailable - wlwidth) / 2;
1.48      nicm      493:                        break;
                    494:                case 2: /* right */
1.58      nicm      495:                        wloffset += (wlavailable - wlwidth);
1.48      nicm      496:                        break;
                    497:                }
                    498:        }
                    499:        if (larrow != 0)
                    500:                wloffset++;
                    501:
                    502:        /* Copy the window list. */
1.178     nicm      503:        c->status.window_list_offset = -wloffset + wlstart;
1.184     nicm      504:        screen_write_cursormove(&ctx, wloffset, 0, 0);
1.171     nicm      505:        screen_write_fast_copy(&ctx, &window_list, wlstart, 0, wlwidth, 1);
1.55      nicm      506:        screen_free(&window_list);
1.179     nicm      507:
                    508:        /* Save left and right size. */
                    509:        c->status.left_size = llen;
                    510:        c->status.right_size = rlen;
1.1       nicm      511:
1.48      nicm      512:        screen_write_stop(&ctx);
1.1       nicm      513:
                    514: out:
1.94      nicm      515:        free(left);
                    516:        free(right);
1.1       nicm      517:
1.175     nicm      518:        if (grid_compare(c->status.status.grid, old_status.grid) == 0) {
1.1       nicm      519:                screen_free(&old_status);
                    520:                return (0);
                    521:        }
                    522:        screen_free(&old_status);
                    523:        return (1);
                    524: }
                    525:
1.46      nicm      526: /* Replace special sequences in fmt. */
1.151     nicm      527: static char *
1.185   ! nicm      528: status_replace(struct client *c, struct winlink *wl, const char *fmt)
1.46      nicm      529: {
1.96      nicm      530:        struct format_tree      *ft;
1.129     nicm      531:        char                    *expanded;
1.160     nicm      532:        u_int                    tag;
1.47      nicm      533:
1.93      nicm      534:        if (fmt == NULL)
                    535:                return (xstrdup(""));
                    536:
1.160     nicm      537:        if (wl != NULL)
                    538:                tag = FORMAT_WINDOW|wl->window->id;
                    539:        else
                    540:                tag = FORMAT_NONE;
1.135     nicm      541:        if (c->flags & CLIENT_STATUSFORCE)
1.164     nicm      542:                ft = format_create(c, NULL, tag, FORMAT_STATUS|FORMAT_FORCE);
1.135     nicm      543:        else
1.164     nicm      544:                ft = format_create(c, NULL, tag, FORMAT_STATUS);
1.129     nicm      545:        format_defaults(ft, c, NULL, wl, NULL);
1.1       nicm      546:
1.185   ! nicm      547:        expanded = format_expand_time(ft, fmt);
1.46      nicm      548:
1.99      nicm      549:        format_free(ft);
                    550:        return (expanded);
1.1       nicm      551: }
                    552:
1.46      nicm      553: /* Return winlink status line entry and adjust gc as necessary. */
1.151     nicm      554: static char *
1.185   ! nicm      555: status_print(struct client *c, struct winlink *wl, struct grid_cell *gc)
1.1       nicm      556: {
1.137     nicm      557:        struct options  *oo = wl->window->options;
1.47      nicm      558:        struct session  *s = c->session;
                    559:        const char      *fmt;
                    560:        char            *text;
1.1       nicm      561:
1.108     nicm      562:        style_apply_update(gc, oo, "window-status-style");
1.47      nicm      563:        fmt = options_get_string(oo, "window-status-format");
1.14      nicm      564:        if (wl == s->curw) {
1.108     nicm      565:                style_apply_update(gc, oo, "window-status-current-style");
1.47      nicm      566:                fmt = options_get_string(oo, "window-status-current-format");
1.95      nicm      567:        }
1.108     nicm      568:        if (wl == TAILQ_FIRST(&s->lastw))
                    569:                style_apply_update(gc, oo, "window-status-last-style");
                    570:
                    571:        if (wl->flags & WINLINK_BELL)
                    572:                style_apply_update(gc, oo, "window-status-bell-style");
                    573:        else if (wl->flags & (WINLINK_ACTIVITY|WINLINK_SILENCE))
                    574:                style_apply_update(gc, oo, "window-status-activity-style");
1.1       nicm      575:
1.185   ! nicm      576:        text = status_replace(c, wl, fmt);
1.1       nicm      577:        return (text);
                    578: }
                    579:
1.46      nicm      580: /* Set a status line message. */
1.117     nicm      581: void
1.10      nicm      582: status_message_set(struct client *c, const char *fmt, ...)
1.1       nicm      583: {
1.162     nicm      584:        struct timeval  tv;
                    585:        va_list         ap;
                    586:        int             delay;
1.1       nicm      587:
1.12      nicm      588:        status_message_clear(c);
                    589:
1.175     nicm      590:        if (c->status.old_status == NULL) {
                    591:                c->status.old_status = xmalloc(sizeof *c->status.old_status);
                    592:                memcpy(c->status.old_status, &c->status.status,
                    593:                    sizeof *c->status.old_status);
                    594:                screen_init(&c->status.status, c->tty.sx, 1, 0);
1.167     nicm      595:        }
                    596:
1.28      nicm      597:        va_start(ap, fmt);
                    598:        xvasprintf(&c->message_string, fmt, ap);
                    599:        va_end(ap);
                    600:
1.162     nicm      601:        server_client_add_message(c, "%s", c->message_string);
1.44      nicm      602:
1.137     nicm      603:        delay = options_get_number(c->session->options, "display-time");
1.143     tim       604:        if (delay > 0) {
                    605:                tv.tv_sec = delay / 1000;
                    606:                tv.tv_usec = (delay % 1000) * 1000L;
                    607:
                    608:                if (event_initialized(&c->message_timer))
                    609:                        evtimer_del(&c->message_timer);
                    610:                evtimer_set(&c->message_timer, status_message_callback, c);
                    611:                evtimer_add(&c->message_timer, &tv);
                    612:        }
1.1       nicm      613:
                    614:        c->tty.flags |= (TTY_NOCURSOR|TTY_FREEZE);
1.177     nicm      615:        c->flags |= CLIENT_REDRAWSTATUS;
1.1       nicm      616: }
                    617:
1.46      nicm      618: /* Clear status line message. */
1.1       nicm      619: void
                    620: status_message_clear(struct client *c)
                    621: {
                    622:        if (c->message_string == NULL)
                    623:                return;
                    624:
1.94      nicm      625:        free(c->message_string);
1.1       nicm      626:        c->message_string = NULL;
                    627:
1.156     nicm      628:        if (c->prompt_string == NULL)
                    629:                c->tty.flags &= ~(TTY_NOCURSOR|TTY_FREEZE);
1.177     nicm      630:        c->flags |= CLIENT_ALLREDRAWFLAGS; /* was frozen and may have changed */
1.7       nicm      631:
1.175     nicm      632:        screen_reinit(&c->status.status);
1.42      nicm      633: }
                    634:
1.46      nicm      635: /* Clear status line message after timer expires. */
1.151     nicm      636: static void
1.141     nicm      637: status_message_callback(__unused int fd, __unused short event, void *data)
1.42      nicm      638: {
                    639:        struct client   *c = data;
                    640:
                    641:        status_message_clear(c);
1.1       nicm      642: }
                    643:
                    644: /* Draw client message on status line of present else on last line. */
                    645: int
                    646: status_message_redraw(struct client *c)
                    647: {
                    648:        struct screen_write_ctx         ctx;
                    649:        struct session                 *s = c->session;
                    650:        struct screen                   old_status;
                    651:        size_t                          len;
                    652:        struct grid_cell                gc;
1.170     nicm      653:        u_int                           lines, offset;
1.1       nicm      654:
                    655:        if (c->tty.sx == 0 || c->tty.sy == 0)
                    656:                return (0);
1.175     nicm      657:        memcpy(&old_status, &c->status.status, sizeof old_status);
1.169     nicm      658:
1.182     nicm      659:        lines = status_line_size(c);
1.173     nicm      660:        if (lines <= 1) {
                    661:                lines = 1;
1.175     nicm      662:                screen_init(&c->status.status, c->tty.sx, 1, 0);
1.173     nicm      663:        } else
1.175     nicm      664:                screen_init(&c->status.status, c->tty.sx, lines, 0);
1.1       nicm      665:
1.139     nicm      666:        len = screen_write_strlen("%s", c->message_string);
1.1       nicm      667:        if (len > c->tty.sx)
                    668:                len = c->tty.sx;
                    669:
1.137     nicm      670:        style_apply(&gc, s->options, "message-style");
1.1       nicm      671:
1.175     nicm      672:        screen_write_start(&ctx, NULL, &c->status.status);
1.184     nicm      673:        screen_write_cursormove(&ctx, 0, 0, 0);
1.170     nicm      674:        for (offset = 0; offset < lines * c->tty.sx; offset++)
                    675:                screen_write_putc(&ctx, &gc, ' ');
1.184     nicm      676:        screen_write_cursormove(&ctx, 0, lines - 1, 0);
1.139     nicm      677:        screen_write_nputs(&ctx, len, &gc, "%s", c->message_string);
1.1       nicm      678:        screen_write_stop(&ctx);
                    679:
1.175     nicm      680:        if (grid_compare(c->status.status.grid, old_status.grid) == 0) {
1.1       nicm      681:                screen_free(&old_status);
                    682:                return (0);
                    683:        }
                    684:        screen_free(&old_status);
                    685:        return (1);
                    686: }
                    687:
1.46      nicm      688: /* Enable status line prompt. */
1.1       nicm      689: void
1.76      nicm      690: status_prompt_set(struct client *c, const char *msg, const char *input,
1.166     nicm      691:     prompt_input_cb inputcb, prompt_free_cb freecb, void *data, int flags)
1.1       nicm      692: {
1.122     nicm      693:        struct format_tree      *ft;
1.165     nicm      694:        char                    *tmp, *cp;
1.122     nicm      695:
1.164     nicm      696:        ft = format_create(c, NULL, FORMAT_NONE, 0);
1.122     nicm      697:        format_defaults(ft, c, NULL, NULL, NULL);
1.152     nicm      698:
1.168     nicm      699:        if (input == NULL)
                    700:                input = "";
                    701:        if (flags & PROMPT_NOFORMAT)
                    702:                tmp = xstrdup(input);
                    703:        else
1.185   ! nicm      704:                tmp = format_expand_time(ft, input);
1.20      nicm      705:
1.12      nicm      706:        status_message_clear(c);
                    707:        status_prompt_clear(c);
1.167     nicm      708:
1.175     nicm      709:        if (c->status.old_status == NULL) {
                    710:                c->status.old_status = xmalloc(sizeof *c->status.old_status);
                    711:                memcpy(c->status.old_status, &c->status.status,
                    712:                    sizeof *c->status.old_status);
                    713:                screen_init(&c->status.status, c->tty.sx, 1, 0);
1.167     nicm      714:        }
1.12      nicm      715:
1.185   ! nicm      716:        c->prompt_string = format_expand_time(ft, msg);
1.1       nicm      717:
1.152     nicm      718:        c->prompt_buffer = utf8_fromcstr(tmp);
                    719:        c->prompt_index = utf8_strlen(c->prompt_buffer);
1.1       nicm      720:
1.166     nicm      721:        c->prompt_inputcb = inputcb;
                    722:        c->prompt_freecb = freecb;
1.1       nicm      723:        c->prompt_data = data;
                    724:
                    725:        c->prompt_hindex = 0;
                    726:
                    727:        c->prompt_flags = flags;
1.155     nicm      728:        c->prompt_mode = PROMPT_ENTRY;
1.1       nicm      729:
1.158     nicm      730:        if (~flags & PROMPT_INCREMENTAL)
                    731:                c->tty.flags |= (TTY_NOCURSOR|TTY_FREEZE);
1.177     nicm      732:        c->flags |= CLIENT_REDRAWSTATUS;
1.165     nicm      733:
                    734:        if ((flags & PROMPT_INCREMENTAL) && *tmp != '\0') {
                    735:                xasprintf(&cp, "=%s", tmp);
1.166     nicm      736:                c->prompt_inputcb(c, c->prompt_data, cp, 0);
1.165     nicm      737:                free(cp);
                    738:        }
1.122     nicm      739:
1.152     nicm      740:        free(tmp);
1.122     nicm      741:        format_free(ft);
1.1       nicm      742: }
                    743:
1.46      nicm      744: /* Remove status line prompt. */
1.1       nicm      745: void
                    746: status_prompt_clear(struct client *c)
                    747: {
1.55      nicm      748:        if (c->prompt_string == NULL)
1.1       nicm      749:                return;
                    750:
1.166     nicm      751:        if (c->prompt_freecb != NULL && c->prompt_data != NULL)
                    752:                c->prompt_freecb(c->prompt_data);
1.1       nicm      753:
1.94      nicm      754:        free(c->prompt_string);
1.1       nicm      755:        c->prompt_string = NULL;
                    756:
1.94      nicm      757:        free(c->prompt_buffer);
1.1       nicm      758:        c->prompt_buffer = NULL;
                    759:
1.181     nicm      760:        free(c->prompt_saved);
                    761:        c->prompt_saved = NULL;
                    762:
1.1       nicm      763:        c->tty.flags &= ~(TTY_NOCURSOR|TTY_FREEZE);
1.177     nicm      764:        c->flags |= CLIENT_ALLREDRAWFLAGS; /* was frozen and may have changed */
1.7       nicm      765:
1.175     nicm      766:        screen_reinit(&c->status.status);
1.27      nicm      767: }
                    768:
1.46      nicm      769: /* Update status line prompt with a new prompt string. */
1.27      nicm      770: void
1.76      nicm      771: status_prompt_update(struct client *c, const char *msg, const char *input)
1.27      nicm      772: {
1.122     nicm      773:        struct format_tree      *ft;
1.152     nicm      774:        char                    *tmp;
1.122     nicm      775:
1.164     nicm      776:        ft = format_create(c, NULL, FORMAT_NONE, 0);
1.122     nicm      777:        format_defaults(ft, c, NULL, NULL, NULL);
1.152     nicm      778:
1.185   ! nicm      779:        tmp = format_expand_time(ft, input);
1.122     nicm      780:
1.94      nicm      781:        free(c->prompt_string);
1.185   ! nicm      782:        c->prompt_string = format_expand_time(ft, msg);
1.27      nicm      783:
1.94      nicm      784:        free(c->prompt_buffer);
1.152     nicm      785:        c->prompt_buffer = utf8_fromcstr(tmp);
                    786:        c->prompt_index = utf8_strlen(c->prompt_buffer);
1.27      nicm      787:
                    788:        c->prompt_hindex = 0;
                    789:
1.177     nicm      790:        c->flags |= CLIENT_REDRAWSTATUS;
1.122     nicm      791:
1.152     nicm      792:        free(tmp);
1.122     nicm      793:        format_free(ft);
1.1       nicm      794: }
                    795:
                    796: /* Draw client prompt on status line of present else on last line. */
                    797: int
                    798: status_prompt_redraw(struct client *c)
                    799: {
1.152     nicm      800:        struct screen_write_ctx  ctx;
                    801:        struct session          *s = c->session;
                    802:        struct screen            old_status;
                    803:        u_int                    i, offset, left, start, pcursor, pwidth, width;
1.169     nicm      804:        u_int                    lines;
1.152     nicm      805:        struct grid_cell         gc, cursorgc;
1.1       nicm      806:
                    807:        if (c->tty.sx == 0 || c->tty.sy == 0)
                    808:                return (0);
1.175     nicm      809:        memcpy(&old_status, &c->status.status, sizeof old_status);
1.169     nicm      810:
1.182     nicm      811:        lines = status_line_size(c);
1.173     nicm      812:        if (lines <= 1) {
                    813:                lines = 1;
1.175     nicm      814:                screen_init(&c->status.status, c->tty.sx, 1, 0);
1.173     nicm      815:        } else
1.175     nicm      816:                screen_init(&c->status.status, c->tty.sx, lines, 0);
1.1       nicm      817:
1.155     nicm      818:        if (c->prompt_mode == PROMPT_COMMAND)
1.137     nicm      819:                style_apply(&gc, s->options, "message-command-style");
1.108     nicm      820:        else
1.137     nicm      821:                style_apply(&gc, s->options, "message-style");
1.1       nicm      822:
1.152     nicm      823:        memcpy(&cursorgc, &gc, sizeof cursorgc);
                    824:        cursorgc.attr ^= GRID_ATTR_REVERSE;
                    825:
                    826:        start = screen_write_strlen("%s", c->prompt_string);
                    827:        if (start > c->tty.sx)
                    828:                start = c->tty.sx;
                    829:
1.175     nicm      830:        screen_write_start(&ctx, NULL, &c->status.status);
1.184     nicm      831:        screen_write_cursormove(&ctx, 0, 0, 0);
1.170     nicm      832:        for (offset = 0; offset < lines * c->tty.sx; offset++)
                    833:                screen_write_putc(&ctx, &gc, ' ');
1.184     nicm      834:        screen_write_cursormove(&ctx, 0, 0, 0);
1.152     nicm      835:        screen_write_nputs(&ctx, start, &gc, "%s", c->prompt_string);
1.184     nicm      836:        screen_write_cursormove(&ctx, start, 0, 0);
1.1       nicm      837:
1.152     nicm      838:        left = c->tty.sx - start;
                    839:        if (left == 0)
                    840:                goto finished;
                    841:
                    842:        pcursor = utf8_strwidth(c->prompt_buffer, c->prompt_index);
                    843:        pwidth = utf8_strwidth(c->prompt_buffer, -1);
                    844:        if (pcursor >= left) {
                    845:                /*
                    846:                 * The cursor would be outside the screen so start drawing
                    847:                 * with it on the right.
                    848:                 */
                    849:                offset = (pcursor - left) + 1;
                    850:                pwidth = left;
                    851:        } else
                    852:                offset = 0;
                    853:        if (pwidth > left)
                    854:                pwidth = left;
                    855:
                    856:        width = 0;
                    857:        for (i = 0; c->prompt_buffer[i].size != 0; i++) {
                    858:                if (width < offset) {
                    859:                        width += c->prompt_buffer[i].width;
                    860:                        continue;
1.1       nicm      861:                }
1.152     nicm      862:                if (width >= offset + pwidth)
                    863:                        break;
                    864:                width += c->prompt_buffer[i].width;
                    865:                if (width > offset + pwidth)
                    866:                        break;
1.1       nicm      867:
1.152     nicm      868:                if (i != c->prompt_index) {
                    869:                        utf8_copy(&gc.data, &c->prompt_buffer[i]);
                    870:                        screen_write_cell(&ctx, &gc);
                    871:                } else {
                    872:                        utf8_copy(&cursorgc.data, &c->prompt_buffer[i]);
                    873:                        screen_write_cell(&ctx, &cursorgc);
                    874:                }
1.1       nicm      875:        }
1.175     nicm      876:        if (c->status.status.cx < screen_size_x(&c->status.status) &&
                    877:            c->prompt_index >= i)
1.152     nicm      878:                screen_write_putc(&ctx, &cursorgc, ' ');
1.1       nicm      879:
1.152     nicm      880: finished:
1.1       nicm      881:        screen_write_stop(&ctx);
1.51      nicm      882:
1.175     nicm      883:        if (grid_compare(c->status.status.grid, old_status.grid) == 0) {
1.1       nicm      884:                screen_free(&old_status);
                    885:                return (0);
                    886:        }
                    887:        screen_free(&old_status);
                    888:        return (1);
                    889: }
                    890:
1.152     nicm      891: /* Is this a separator? */
                    892: static int
                    893: status_prompt_in_list(const char *ws, const struct utf8_data *ud)
                    894: {
                    895:        if (ud->size != 1 || ud->width != 1)
                    896:                return (0);
                    897:        return (strchr(ws, *ud->data) != NULL);
                    898: }
                    899:
                    900: /* Is this a space? */
                    901: static int
                    902: status_prompt_space(const struct utf8_data *ud)
                    903: {
                    904:        if (ud->size != 1 || ud->width != 1)
                    905:                return (0);
                    906:        return (*ud->data == ' ');
                    907: }
                    908:
1.155     nicm      909: /*
                    910:  * Translate key from emacs to vi. Return 0 to drop key, 1 to process the key
                    911:  * as an emacs key; return 2 to append to the buffer.
                    912:  */
                    913: static int
                    914: status_prompt_translate_key(struct client *c, key_code key, key_code *new_key)
                    915: {
                    916:        if (c->prompt_mode == PROMPT_ENTRY) {
                    917:                switch (key) {
                    918:                case '\003': /* C-c */
                    919:                case '\010': /* C-h */
                    920:                case '\011': /* Tab */
                    921:                case '\025': /* C-u */
                    922:                case '\027': /* C-w */
                    923:                case '\n':
                    924:                case '\r':
                    925:                case KEYC_BSPACE:
                    926:                case KEYC_DC:
                    927:                case KEYC_DOWN:
                    928:                case KEYC_END:
                    929:                case KEYC_HOME:
                    930:                case KEYC_LEFT:
                    931:                case KEYC_RIGHT:
                    932:                case KEYC_UP:
                    933:                        *new_key = key;
                    934:                        return (1);
                    935:                case '\033': /* Escape */
                    936:                        c->prompt_mode = PROMPT_COMMAND;
1.177     nicm      937:                        c->flags |= CLIENT_REDRAWSTATUS;
1.155     nicm      938:                        return (0);
                    939:                }
                    940:                *new_key = key;
                    941:                return (2);
                    942:        }
                    943:
                    944:        switch (key) {
                    945:        case 'A':
                    946:        case 'I':
                    947:        case 'C':
                    948:        case 's':
                    949:        case 'a':
                    950:                c->prompt_mode = PROMPT_ENTRY;
1.177     nicm      951:                c->flags |= CLIENT_REDRAWSTATUS;
1.155     nicm      952:                break; /* switch mode and... */
                    953:        case 'S':
                    954:                c->prompt_mode = PROMPT_ENTRY;
1.177     nicm      955:                c->flags |= CLIENT_REDRAWSTATUS;
1.155     nicm      956:                *new_key = '\025'; /* C-u */
                    957:                return (1);
                    958:        case 'i':
                    959:        case '\033': /* Escape */
                    960:                c->prompt_mode = PROMPT_ENTRY;
1.177     nicm      961:                c->flags |= CLIENT_REDRAWSTATUS;
1.155     nicm      962:                return (0);
                    963:        }
                    964:
                    965:        switch (key) {
                    966:        case 'A':
                    967:        case '$':
                    968:                *new_key = KEYC_END;
                    969:                return (1);
                    970:        case 'I':
                    971:        case '0':
                    972:        case '^':
                    973:                *new_key = KEYC_HOME;
                    974:                return (1);
                    975:        case 'C':
                    976:        case 'D':
                    977:                *new_key = '\013'; /* C-k */
                    978:                return (1);
                    979:        case KEYC_BSPACE:
                    980:        case 'X':
                    981:                *new_key = KEYC_BSPACE;
                    982:                return (1);
                    983:        case 'b':
                    984:        case 'B':
                    985:                *new_key = 'b'|KEYC_ESCAPE;
                    986:                return (1);
                    987:        case 'd':
                    988:                *new_key = '\025';
                    989:                return (1);
                    990:        case 'e':
                    991:        case 'E':
                    992:        case 'w':
                    993:        case 'W':
                    994:                *new_key = 'f'|KEYC_ESCAPE;
                    995:                return (1);
                    996:        case 'p':
                    997:                *new_key = '\031'; /* C-y */
                    998:                return (1);
                    999:        case 's':
                   1000:        case KEYC_DC:
                   1001:        case 'x':
                   1002:                *new_key = KEYC_DC;
                   1003:                return (1);
                   1004:        case KEYC_DOWN:
                   1005:        case 'j':
                   1006:                *new_key = KEYC_DOWN;
                   1007:                return (1);
                   1008:        case KEYC_LEFT:
                   1009:        case 'h':
                   1010:                *new_key = KEYC_LEFT;
                   1011:                return (1);
                   1012:        case 'a':
                   1013:        case KEYC_RIGHT:
                   1014:        case 'l':
                   1015:                *new_key = KEYC_RIGHT;
                   1016:                return (1);
                   1017:        case KEYC_UP:
                   1018:        case 'k':
                   1019:                *new_key = KEYC_UP;
                   1020:                return (1);
                   1021:        case '\010' /* C-h */:
                   1022:        case '\003' /* C-c */:
                   1023:        case '\n':
                   1024:        case '\r':
                   1025:                return (1);
                   1026:        }
                   1027:        return (0);
                   1028: }
                   1029:
1.1       nicm     1030: /* Handle keys in prompt. */
1.154     nicm     1031: int
1.138     nicm     1032: status_prompt_key(struct client *c, key_code key)
1.1       nicm     1033: {
1.152     nicm     1034:        struct options          *oo = c->session->options;
1.1       nicm     1035:        struct paste_buffer     *pb;
1.158     nicm     1036:        char                    *s, *cp, word[64], prefix = '=';
1.152     nicm     1037:        const char              *histstr, *bufdata, *ws = NULL;
1.53      nicm     1038:        u_char                   ch;
1.152     nicm     1039:        size_t                   size, n, off, idx, bufsize, used;
                   1040:        struct utf8_data         tmp, *first, *last, *ud;
1.155     nicm     1041:        int                      keys;
1.1       nicm     1042:
1.152     nicm     1043:        size = utf8_strlen(c->prompt_buffer);
1.154     nicm     1044:
                   1045:        if (c->prompt_flags & PROMPT_NUMERIC) {
                   1046:                if (key >= '0' && key <= '9')
                   1047:                        goto append_key;
                   1048:                s = utf8_tocstr(c->prompt_buffer);
1.166     nicm     1049:                c->prompt_inputcb(c, c->prompt_data, s, 1);
1.154     nicm     1050:                status_prompt_clear(c);
                   1051:                free(s);
                   1052:                return (1);
                   1053:        }
1.180     nicm     1054:        key &= ~KEYC_XTERM;
1.154     nicm     1055:
1.155     nicm     1056:        keys = options_get_number(c->session->options, "status-keys");
                   1057:        if (keys == MODEKEY_VI) {
                   1058:                switch (status_prompt_translate_key(c, key, &key)) {
                   1059:                case 1:
                   1060:                        goto process_key;
                   1061:                case 2:
                   1062:                        goto append_key;
                   1063:                default:
                   1064:                        return (0);
                   1065:                }
                   1066:        }
                   1067:
                   1068: process_key:
                   1069:        switch (key) {
                   1070:        case KEYC_LEFT:
                   1071:        case '\002': /* C-b */
1.1       nicm     1072:                if (c->prompt_index > 0) {
                   1073:                        c->prompt_index--;
1.158     nicm     1074:                        break;
1.1       nicm     1075:                }
                   1076:                break;
1.155     nicm     1077:        case KEYC_RIGHT:
                   1078:        case '\006': /* C-f */
1.1       nicm     1079:                if (c->prompt_index < size) {
                   1080:                        c->prompt_index++;
1.158     nicm     1081:                        break;
1.1       nicm     1082:                }
                   1083:                break;
1.155     nicm     1084:        case KEYC_HOME:
                   1085:        case '\001': /* C-a */
1.1       nicm     1086:                if (c->prompt_index != 0) {
                   1087:                        c->prompt_index = 0;
1.158     nicm     1088:                        break;
1.1       nicm     1089:                }
                   1090:                break;
1.155     nicm     1091:        case KEYC_END:
                   1092:        case '\005': /* C-e */
1.1       nicm     1093:                if (c->prompt_index != size) {
                   1094:                        c->prompt_index = size;
1.158     nicm     1095:                        break;
1.1       nicm     1096:                }
                   1097:                break;
1.155     nicm     1098:        case '\011': /* Tab */
1.152     nicm     1099:                if (c->prompt_buffer[0].size == 0)
1.1       nicm     1100:                        break;
                   1101:
                   1102:                idx = c->prompt_index;
                   1103:                if (idx != 0)
                   1104:                        idx--;
                   1105:
                   1106:                /* Find the word we are in. */
1.152     nicm     1107:                first = &c->prompt_buffer[idx];
                   1108:                while (first > c->prompt_buffer && !status_prompt_space(first))
1.1       nicm     1109:                        first--;
1.152     nicm     1110:                while (first->size != 0 && status_prompt_space(first))
1.1       nicm     1111:                        first++;
1.152     nicm     1112:                last = &c->prompt_buffer[idx];
                   1113:                while (last->size != 0 && !status_prompt_space(last))
1.1       nicm     1114:                        last++;
1.152     nicm     1115:                while (last > c->prompt_buffer && status_prompt_space(last))
1.1       nicm     1116:                        last--;
1.152     nicm     1117:                if (last->size != 0)
1.1       nicm     1118:                        last++;
1.152     nicm     1119:                if (last <= first)
                   1120:                        break;
                   1121:
                   1122:                used = 0;
                   1123:                for (ud = first; ud < last; ud++) {
                   1124:                        if (used + ud->size >= sizeof word)
                   1125:                                break;
                   1126:                        memcpy(word + used, ud->data, ud->size);
                   1127:                        used += ud->size;
                   1128:                }
                   1129:                if (ud != last)
1.1       nicm     1130:                        break;
1.152     nicm     1131:                word[used] = '\0';
1.1       nicm     1132:
                   1133:                /* And try to complete it. */
1.152     nicm     1134:                if ((s = status_prompt_complete(c->session, word)) == NULL)
1.1       nicm     1135:                        break;
                   1136:
                   1137:                /* Trim out word. */
                   1138:                n = size - (last - c->prompt_buffer) + 1; /* with \0 */
1.152     nicm     1139:                memmove(first, last, n * sizeof *c->prompt_buffer);
1.1       nicm     1140:                size -= last - first;
                   1141:
                   1142:                /* Insert the new word. */
1.55      nicm     1143:                size += strlen(s);
1.1       nicm     1144:                off = first - c->prompt_buffer;
1.152     nicm     1145:                c->prompt_buffer = xreallocarray(c->prompt_buffer, size + 1,
                   1146:                    sizeof *c->prompt_buffer);
1.1       nicm     1147:                first = c->prompt_buffer + off;
1.152     nicm     1148:                memmove(first + strlen(s), first, n * sizeof *c->prompt_buffer);
                   1149:                for (idx = 0; idx < strlen(s); idx++)
                   1150:                        utf8_set(&first[idx], s[idx]);
1.1       nicm     1151:
                   1152:                c->prompt_index = (first - c->prompt_buffer) + strlen(s);
1.94      nicm     1153:                free(s);
1.1       nicm     1154:
1.158     nicm     1155:                goto changed;
1.155     nicm     1156:        case KEYC_BSPACE:
                   1157:        case '\010': /* C-h */
1.1       nicm     1158:                if (c->prompt_index != 0) {
                   1159:                        if (c->prompt_index == size)
1.152     nicm     1160:                                c->prompt_buffer[--c->prompt_index].size = 0;
1.1       nicm     1161:                        else {
                   1162:                                memmove(c->prompt_buffer + c->prompt_index - 1,
                   1163:                                    c->prompt_buffer + c->prompt_index,
1.152     nicm     1164:                                    (size + 1 - c->prompt_index) *
                   1165:                                    sizeof *c->prompt_buffer);
1.1       nicm     1166:                                c->prompt_index--;
                   1167:                        }
1.158     nicm     1168:                        goto changed;
1.1       nicm     1169:                }
                   1170:                break;
1.155     nicm     1171:        case KEYC_DC:
                   1172:        case '\004': /* C-d */
1.1       nicm     1173:                if (c->prompt_index != size) {
                   1174:                        memmove(c->prompt_buffer + c->prompt_index,
                   1175:                            c->prompt_buffer + c->prompt_index + 1,
1.152     nicm     1176:                            (size + 1 - c->prompt_index) *
                   1177:                            sizeof *c->prompt_buffer);
1.158     nicm     1178:                        goto changed;
1.18      nicm     1179:                }
1.26      nicm     1180:                break;
1.155     nicm     1181:        case '\025': /* C-u */
1.152     nicm     1182:                c->prompt_buffer[0].size = 0;
1.26      nicm     1183:                c->prompt_index = 0;
1.158     nicm     1184:                goto changed;
1.155     nicm     1185:        case '\013': /* C-k */
1.18      nicm     1186:                if (c->prompt_index < size) {
1.152     nicm     1187:                        c->prompt_buffer[c->prompt_index].size = 0;
1.158     nicm     1188:                        goto changed;
1.1       nicm     1189:                }
                   1190:                break;
1.155     nicm     1191:        case '\027': /* C-w */
1.152     nicm     1192:                ws = options_get_string(oo, "word-separators");
1.81      nicm     1193:                idx = c->prompt_index;
                   1194:
                   1195:                /* Find a non-separator. */
                   1196:                while (idx != 0) {
                   1197:                        idx--;
1.152     nicm     1198:                        if (!status_prompt_in_list(ws, &c->prompt_buffer[idx]))
1.81      nicm     1199:                                break;
                   1200:                }
                   1201:
                   1202:                /* Find the separator at the beginning of the word. */
                   1203:                while (idx != 0) {
                   1204:                        idx--;
1.152     nicm     1205:                        if (status_prompt_in_list(ws, &c->prompt_buffer[idx])) {
1.81      nicm     1206:                                /* Go back to the word. */
                   1207:                                idx++;
                   1208:                                break;
                   1209:                        }
                   1210:                }
                   1211:
1.181     nicm     1212:                free(c->prompt_saved);
                   1213:                c->prompt_saved = xcalloc(sizeof *c->prompt_buffer,
                   1214:                    (c->prompt_index - idx) + 1);
                   1215:                memcpy(c->prompt_saved, c->prompt_buffer + idx,
                   1216:                    (c->prompt_index - idx) * sizeof *c->prompt_buffer);
                   1217:
1.81      nicm     1218:                memmove(c->prompt_buffer + idx,
                   1219:                    c->prompt_buffer + c->prompt_index,
1.152     nicm     1220:                    (size + 1 - c->prompt_index) *
                   1221:                    sizeof *c->prompt_buffer);
1.81      nicm     1222:                memset(c->prompt_buffer + size - (c->prompt_index - idx),
1.152     nicm     1223:                    '\0', (c->prompt_index - idx) * sizeof *c->prompt_buffer);
1.81      nicm     1224:                c->prompt_index = idx;
1.152     nicm     1225:
1.158     nicm     1226:                goto changed;
1.155     nicm     1227:        case 'f'|KEYC_ESCAPE:
1.180     nicm     1228:        case KEYC_RIGHT|KEYC_CTRL:
1.155     nicm     1229:                ws = options_get_string(oo, "word-separators");
1.81      nicm     1230:
                   1231:                /* Find a word. */
                   1232:                while (c->prompt_index != size) {
1.152     nicm     1233:                        idx = ++c->prompt_index;
                   1234:                        if (!status_prompt_in_list(ws, &c->prompt_buffer[idx]))
1.81      nicm     1235:                                break;
                   1236:                }
                   1237:
                   1238:                /* Find the separator at the end of the word. */
                   1239:                while (c->prompt_index != size) {
1.152     nicm     1240:                        idx = ++c->prompt_index;
                   1241:                        if (status_prompt_in_list(ws, &c->prompt_buffer[idx]))
1.81      nicm     1242:                                break;
                   1243:                }
1.106     nicm     1244:
                   1245:                /* Back up to the end-of-word like vi. */
                   1246:                if (options_get_number(oo, "status-keys") == MODEKEY_VI &&
                   1247:                    c->prompt_index != 0)
                   1248:                        c->prompt_index--;
1.81      nicm     1249:
1.158     nicm     1250:                goto changed;
1.155     nicm     1251:        case 'b'|KEYC_ESCAPE:
1.180     nicm     1252:        case KEYC_LEFT|KEYC_CTRL:
1.155     nicm     1253:                ws = options_get_string(oo, "word-separators");
1.81      nicm     1254:
                   1255:                /* Find a non-separator. */
                   1256:                while (c->prompt_index != 0) {
1.152     nicm     1257:                        idx = --c->prompt_index;
                   1258:                        if (!status_prompt_in_list(ws, &c->prompt_buffer[idx]))
1.81      nicm     1259:                                break;
                   1260:                }
                   1261:
                   1262:                /* Find the separator at the beginning of the word. */
                   1263:                while (c->prompt_index != 0) {
1.152     nicm     1264:                        idx = --c->prompt_index;
                   1265:                        if (status_prompt_in_list(ws, &c->prompt_buffer[idx])) {
1.81      nicm     1266:                                /* Go back to the word. */
                   1267:                                c->prompt_index++;
                   1268:                                break;
                   1269:                        }
                   1270:                }
1.158     nicm     1271:                goto changed;
1.155     nicm     1272:        case KEYC_UP:
                   1273:        case '\020': /* C-p */
1.66      nicm     1274:                histstr = status_prompt_up_history(&c->prompt_hindex);
                   1275:                if (histstr == NULL)
1.1       nicm     1276:                        break;
1.94      nicm     1277:                free(c->prompt_buffer);
1.152     nicm     1278:                c->prompt_buffer = utf8_fromcstr(histstr);
                   1279:                c->prompt_index = utf8_strlen(c->prompt_buffer);
1.158     nicm     1280:                goto changed;
1.155     nicm     1281:        case KEYC_DOWN:
                   1282:        case '\016': /* C-n */
1.66      nicm     1283:                histstr = status_prompt_down_history(&c->prompt_hindex);
                   1284:                if (histstr == NULL)
1.65      nicm     1285:                        break;
1.94      nicm     1286:                free(c->prompt_buffer);
1.152     nicm     1287:                c->prompt_buffer = utf8_fromcstr(histstr);
                   1288:                c->prompt_index = utf8_strlen(c->prompt_buffer);
1.158     nicm     1289:                goto changed;
1.155     nicm     1290:        case '\031': /* C-y */
1.181     nicm     1291:                if (c->prompt_saved != NULL) {
                   1292:                        ud = c->prompt_saved;
                   1293:                        n = utf8_strlen(c->prompt_saved);
                   1294:                } else {
                   1295:                        if ((pb = paste_get_top(NULL)) == NULL)
1.32      nicm     1296:                                break;
1.181     nicm     1297:                        bufdata = paste_buffer_data(pb, &bufsize);
                   1298:                        for (n = 0; n < bufsize; n++) {
                   1299:                                ch = (u_char)bufdata[n];
                   1300:                                if (ch < 32 || ch >= 127)
                   1301:                                        break;
                   1302:                        }
                   1303:                        ud = xreallocarray(NULL, n, sizeof *ud);
                   1304:                        for (idx = 0; idx < n; idx++)
                   1305:                                utf8_set(&ud[idx], bufdata[idx]);
1.32      nicm     1306:                }
1.1       nicm     1307:
1.152     nicm     1308:                c->prompt_buffer = xreallocarray(c->prompt_buffer, size + n + 1,
                   1309:                    sizeof *c->prompt_buffer);
1.1       nicm     1310:                if (c->prompt_index == size) {
1.181     nicm     1311:                        memcpy(c->prompt_buffer + c->prompt_index, ud,
                   1312:                            n * sizeof *c->prompt_buffer);
1.1       nicm     1313:                        c->prompt_index += n;
1.152     nicm     1314:                        c->prompt_buffer[c->prompt_index].size = 0;
1.1       nicm     1315:                } else {
                   1316:                        memmove(c->prompt_buffer + c->prompt_index + n,
                   1317:                            c->prompt_buffer + c->prompt_index,
1.152     nicm     1318:                            (size + 1 - c->prompt_index) *
                   1319:                            sizeof *c->prompt_buffer);
1.181     nicm     1320:                        memcpy(c->prompt_buffer + c->prompt_index, ud,
                   1321:                            n * sizeof *c->prompt_buffer);
1.1       nicm     1322:                        c->prompt_index += n;
                   1323:                }
1.181     nicm     1324:
                   1325:                if (ud != c->prompt_saved)
                   1326:                        free(ud);
1.158     nicm     1327:                goto changed;
1.155     nicm     1328:        case '\024': /* C-t */
1.30      nicm     1329:                idx = c->prompt_index;
                   1330:                if (idx < size)
                   1331:                        idx++;
                   1332:                if (idx >= 2) {
1.152     nicm     1333:                        utf8_copy(&tmp, &c->prompt_buffer[idx - 2]);
                   1334:                        utf8_copy(&c->prompt_buffer[idx - 2],
                   1335:                            &c->prompt_buffer[idx - 1]);
                   1336:                        utf8_copy(&c->prompt_buffer[idx - 1], &tmp);
1.30      nicm     1337:                        c->prompt_index = idx;
1.158     nicm     1338:                        goto changed;
1.30      nicm     1339:                }
1.1       nicm     1340:                break;
1.155     nicm     1341:        case '\r':
                   1342:        case '\n':
1.152     nicm     1343:                s = utf8_tocstr(c->prompt_buffer);
                   1344:                if (*s != '\0')
                   1345:                        status_prompt_add_history(s);
1.166     nicm     1346:                if (c->prompt_inputcb(c, c->prompt_data, s, 1) == 0)
1.25      nicm     1347:                        status_prompt_clear(c);
1.152     nicm     1348:                free(s);
1.25      nicm     1349:                break;
1.155     nicm     1350:        case '\033': /* Escape */
                   1351:        case '\003': /* C-c */
1.174     nicm     1352:        case '\007': /* C-g */
1.166     nicm     1353:                if (c->prompt_inputcb(c, c->prompt_data, NULL, 1) == 0)
1.1       nicm     1354:                        status_prompt_clear(c);
                   1355:                break;
1.158     nicm     1356:        case '\022': /* C-r */
                   1357:                if (c->prompt_flags & PROMPT_INCREMENTAL) {
                   1358:                        prefix = '-';
                   1359:                        goto changed;
                   1360:                }
                   1361:                break;
                   1362:        case '\023': /* C-s */
                   1363:                if (c->prompt_flags & PROMPT_INCREMENTAL) {
                   1364:                        prefix = '+';
                   1365:                        goto changed;
                   1366:                }
                   1367:                break;
                   1368:        default:
                   1369:                goto append_key;
1.154     nicm     1370:        }
1.152     nicm     1371:
1.177     nicm     1372:        c->flags |= CLIENT_REDRAWSTATUS;
1.158     nicm     1373:        return (0);
                   1374:
1.154     nicm     1375: append_key:
                   1376:        if (key <= 0x1f || key >= KEYC_BASE)
                   1377:                return (0);
                   1378:        if (utf8_split(key, &tmp) != UTF8_DONE)
                   1379:                return (0);
1.1       nicm     1380:
1.154     nicm     1381:        c->prompt_buffer = xreallocarray(c->prompt_buffer, size + 2,
                   1382:            sizeof *c->prompt_buffer);
1.1       nicm     1383:
1.154     nicm     1384:        if (c->prompt_index == size) {
                   1385:                utf8_copy(&c->prompt_buffer[c->prompt_index], &tmp);
                   1386:                c->prompt_index++;
                   1387:                c->prompt_buffer[c->prompt_index].size = 0;
                   1388:        } else {
                   1389:                memmove(c->prompt_buffer + c->prompt_index + 1,
                   1390:                    c->prompt_buffer + c->prompt_index,
                   1391:                    (size + 1 - c->prompt_index) *
                   1392:                    sizeof *c->prompt_buffer);
                   1393:                utf8_copy(&c->prompt_buffer[c->prompt_index], &tmp);
                   1394:                c->prompt_index++;
                   1395:        }
1.1       nicm     1396:
1.154     nicm     1397:        if (c->prompt_flags & PROMPT_SINGLE) {
                   1398:                s = utf8_tocstr(c->prompt_buffer);
                   1399:                if (strlen(s) != 1)
                   1400:                        status_prompt_clear(c);
1.166     nicm     1401:                else if (c->prompt_inputcb(c, c->prompt_data, s, 1) == 0)
1.154     nicm     1402:                        status_prompt_clear(c);
                   1403:                free(s);
1.1       nicm     1404:        }
1.154     nicm     1405:
1.158     nicm     1406: changed:
1.177     nicm     1407:        c->flags |= CLIENT_REDRAWSTATUS;
1.158     nicm     1408:        if (c->prompt_flags & PROMPT_INCREMENTAL) {
                   1409:                s = utf8_tocstr(c->prompt_buffer);
                   1410:                xasprintf(&cp, "%c%s", prefix, s);
1.166     nicm     1411:                c->prompt_inputcb(c, c->prompt_data, cp, 0);
1.158     nicm     1412:                free(cp);
                   1413:                free(s);
                   1414:        }
1.154     nicm     1415:        return (0);
1.1       nicm     1416: }
                   1417:
1.65      nicm     1418: /* Get previous line from the history. */
1.151     nicm     1419: static const char *
1.65      nicm     1420: status_prompt_up_history(u_int *idx)
                   1421: {
                   1422:        /*
1.128     nicm     1423:         * History runs from 0 to size - 1. Index is from 0 to size. Zero is
                   1424:         * empty.
1.65      nicm     1425:         */
                   1426:
1.128     nicm     1427:        if (status_prompt_hsize == 0 || *idx == status_prompt_hsize)
1.65      nicm     1428:                return (NULL);
                   1429:        (*idx)++;
1.128     nicm     1430:        return (status_prompt_hlist[status_prompt_hsize - *idx]);
1.65      nicm     1431: }
                   1432:
                   1433: /* Get next line from the history. */
1.151     nicm     1434: static const char *
1.65      nicm     1435: status_prompt_down_history(u_int *idx)
                   1436: {
1.128     nicm     1437:        if (status_prompt_hsize == 0 || *idx == 0)
1.65      nicm     1438:                return ("");
                   1439:        (*idx)--;
                   1440:        if (*idx == 0)
                   1441:                return ("");
1.128     nicm     1442:        return (status_prompt_hlist[status_prompt_hsize - *idx]);
1.65      nicm     1443: }
                   1444:
1.1       nicm     1445: /* Add line to the history. */
1.151     nicm     1446: static void
1.65      nicm     1447: status_prompt_add_history(const char *line)
1.1       nicm     1448: {
1.128     nicm     1449:        size_t  size;
1.65      nicm     1450:
1.128     nicm     1451:        if (status_prompt_hsize > 0 &&
                   1452:            strcmp(status_prompt_hlist[status_prompt_hsize - 1], line) == 0)
1.1       nicm     1453:                return;
                   1454:
1.128     nicm     1455:        if (status_prompt_hsize == PROMPT_HISTORY) {
                   1456:                free(status_prompt_hlist[0]);
1.1       nicm     1457:
1.128     nicm     1458:                size = (PROMPT_HISTORY - 1) * sizeof *status_prompt_hlist;
                   1459:                memmove(&status_prompt_hlist[0], &status_prompt_hlist[1], size);
1.1       nicm     1460:
1.128     nicm     1461:                status_prompt_hlist[status_prompt_hsize - 1] = xstrdup(line);
                   1462:                return;
                   1463:        }
1.1       nicm     1464:
1.128     nicm     1465:        status_prompt_hlist = xreallocarray(status_prompt_hlist,
                   1466:            status_prompt_hsize + 1, sizeof *status_prompt_hlist);
                   1467:        status_prompt_hlist[status_prompt_hsize++] = xstrdup(line);
                   1468: }
                   1469:
                   1470: /* Build completion list. */
1.183     nicm     1471: char **
1.128     nicm     1472: status_prompt_complete_list(u_int *size, const char *s)
                   1473: {
1.183     nicm     1474:        char                                    **list = NULL;
                   1475:        const char                              **layout, *value, *cp;
1.128     nicm     1476:        const struct cmd_entry                  **cmdent;
                   1477:        const struct options_table_entry         *oe;
1.183     nicm     1478:        u_int                                     items, idx;
                   1479:        size_t                                    slen = strlen(s), valuelen;
                   1480:        struct options_entry                     *o;
1.128     nicm     1481:        const char                               *layouts[] = {
                   1482:                "even-horizontal", "even-vertical", "main-horizontal",
                   1483:                "main-vertical", "tiled", NULL
                   1484:        };
1.1       nicm     1485:
1.128     nicm     1486:        *size = 0;
1.1       nicm     1487:        for (cmdent = cmd_table; *cmdent != NULL; cmdent++) {
1.183     nicm     1488:                if (strncmp((*cmdent)->name, s, slen) == 0) {
1.128     nicm     1489:                        list = xreallocarray(list, (*size) + 1, sizeof *list);
1.183     nicm     1490:                        list[(*size)++] = xstrdup((*cmdent)->name);
1.128     nicm     1491:                }
1.56      nicm     1492:        }
1.142     nicm     1493:        for (oe = options_table; oe->name != NULL; oe++) {
1.183     nicm     1494:                if (strncmp(oe->name, s, slen) == 0) {
1.128     nicm     1495:                        list = xreallocarray(list, (*size) + 1, sizeof *list);
1.183     nicm     1496:                        list[(*size)++] = xstrdup(oe->name);
1.128     nicm     1497:                }
                   1498:        }
                   1499:        for (layout = layouts; *layout != NULL; layout++) {
1.183     nicm     1500:                if (strncmp(*layout, s, slen) == 0) {
1.128     nicm     1501:                        list = xreallocarray(list, (*size) + 1, sizeof *list);
1.183     nicm     1502:                        list[(*size)++] = xstrdup(*layout);
1.128     nicm     1503:                }
                   1504:        }
1.183     nicm     1505:        o = options_get_only(global_options, "command-alias");
                   1506:        if (o != NULL && options_array_size(o, &items) != -1) {
                   1507:                for (idx = 0; idx < items; idx++) {
                   1508:                        value = options_array_get(o, idx);
                   1509:                        if (value == NULL || (cp = strchr(value, '=')) == NULL)
                   1510:                                continue;
                   1511:                        valuelen = cp - value;
                   1512:                        if (slen > valuelen || strncmp(value, s, slen) != 0)
                   1513:                                continue;
                   1514:                        list = xreallocarray(list, (*size) + 1, sizeof *list);
                   1515:                        list[(*size)++] = xstrndup(value, valuelen);
                   1516:                }
                   1517:        }
                   1518:        for (idx = 0; idx < (*size); idx++)
                   1519:                log_debug("complete %u: %s", idx, list[idx]);
1.128     nicm     1520:        return (list);
                   1521: }
                   1522:
                   1523: /* Find longest prefix. */
1.151     nicm     1524: static char *
1.183     nicm     1525: status_prompt_complete_prefix(char **list, u_int size)
1.128     nicm     1526: {
                   1527:        char     *out;
                   1528:        u_int     i;
                   1529:        size_t    j;
                   1530:
                   1531:        out = xstrdup(list[0]);
                   1532:        for (i = 1; i < size; i++) {
                   1533:                j = strlen(list[i]);
                   1534:                if (j > strlen(out))
                   1535:                        j = strlen(out);
                   1536:                for (; j > 0; j--) {
                   1537:                        if (out[j - 1] != list[i][j - 1])
                   1538:                                out[j - 1] = '\0';
                   1539:                }
1.1       nicm     1540:        }
1.128     nicm     1541:        return (out);
                   1542: }
1.1       nicm     1543:
1.128     nicm     1544: /* Complete word. */
1.151     nicm     1545: static char *
1.152     nicm     1546: status_prompt_complete(struct session *session, const char *s)
1.128     nicm     1547: {
1.183     nicm     1548:        char            **list = NULL;
                   1549:        const char       *colon;
1.128     nicm     1550:        u_int             size = 0, i;
                   1551:        struct session   *s_loop;
                   1552:        struct winlink   *wl;
                   1553:        struct window    *w;
                   1554:        char             *copy, *out, *tmp;
                   1555:
                   1556:        if (*s == '\0')
1.1       nicm     1557:                return (NULL);
1.128     nicm     1558:        out = NULL;
                   1559:
                   1560:        if (strncmp(s, "-t", 2) != 0 && strncmp(s, "-s", 2) != 0) {
                   1561:                list = status_prompt_complete_list(&size, s);
                   1562:                if (size == 0)
                   1563:                        out = NULL;
                   1564:                else if (size == 1)
                   1565:                        xasprintf(&out, "%s ", list[0]);
                   1566:                else
                   1567:                        out = status_prompt_complete_prefix(list, size);
1.183     nicm     1568:                for (i = 0; i < size; i++)
                   1569:                        free(list[i]);
1.128     nicm     1570:                free(list);
                   1571:                return (out);
                   1572:        }
                   1573:        copy = xstrdup(s);
                   1574:
                   1575:        colon = ":";
                   1576:        if (copy[strlen(copy) - 1] == ':')
                   1577:                copy[strlen(copy) - 1] = '\0';
                   1578:        else
                   1579:                colon = "";
                   1580:        s = copy + 2;
1.1       nicm     1581:
1.128     nicm     1582:        RB_FOREACH(s_loop, sessions, &sessions) {
                   1583:                if (strncmp(s_loop->name, s, strlen(s)) == 0) {
                   1584:                        list = xreallocarray(list, size + 2, sizeof *list);
                   1585:                        list[size++] = s_loop->name;
                   1586:                }
                   1587:        }
                   1588:        if (size == 1) {
                   1589:                out = xstrdup(list[0]);
                   1590:                if (session_find(list[0]) != NULL)
                   1591:                        colon = ":";
                   1592:        } else if (size != 0)
                   1593:                out = status_prompt_complete_prefix(list, size);
                   1594:        if (out != NULL) {
                   1595:                xasprintf(&tmp, "-%c%s%s", copy[1], out, colon);
1.163     nicm     1596:                free(out);
1.128     nicm     1597:                out = tmp;
                   1598:                goto found;
                   1599:        }
                   1600:
                   1601:        colon = "";
                   1602:        if (*s == ':') {
1.152     nicm     1603:                RB_FOREACH(wl, winlinks, &session->windows) {
1.128     nicm     1604:                        xasprintf(&tmp, ":%s", wl->window->name);
                   1605:                        if (strncmp(tmp, s, strlen(s)) == 0){
                   1606:                                list = xreallocarray(list, size + 1,
                   1607:                                    sizeof *list);
                   1608:                                list[size++] = tmp;
                   1609:                                continue;
                   1610:                        }
                   1611:                        free(tmp);
1.1       nicm     1612:
1.128     nicm     1613:                        xasprintf(&tmp, ":%d", wl->idx);
                   1614:                        if (strncmp(tmp, s, strlen(s)) == 0) {
                   1615:                                list = xreallocarray(list, size + 1,
                   1616:                                    sizeof *list);
                   1617:                                list[size++] = tmp;
                   1618:                                continue;
                   1619:                        }
                   1620:                        free(tmp);
                   1621:                }
                   1622:        } else {
                   1623:                RB_FOREACH(s_loop, sessions, &sessions) {
                   1624:                        RB_FOREACH(wl, winlinks, &s_loop->windows) {
                   1625:                                w = wl->window;
                   1626:
                   1627:                                xasprintf(&tmp, "%s:%s", s_loop->name, w->name);
                   1628:                                if (strncmp(tmp, s, strlen(s)) == 0) {
                   1629:                                        list = xreallocarray(list, size + 1,
                   1630:                                            sizeof *list);
                   1631:                                        list[size++] = tmp;
                   1632:                                        continue;
                   1633:                                }
                   1634:                                free(tmp);
                   1635:
                   1636:                                xasprintf(&tmp, "%s:%d", s_loop->name, wl->idx);
                   1637:                                if (strncmp(tmp, s, strlen(s)) == 0) {
                   1638:                                        list = xreallocarray(list, size + 1,
                   1639:                                            sizeof *list);
                   1640:                                        list[size++] = tmp;
                   1641:                                        continue;
                   1642:                                }
                   1643:                                free(tmp);
                   1644:                        }
1.1       nicm     1645:                }
                   1646:        }
1.128     nicm     1647:        if (size == 1) {
                   1648:                out = xstrdup(list[0]);
                   1649:                colon = " ";
                   1650:        } else if (size != 0)
                   1651:                out = status_prompt_complete_prefix(list, size);
                   1652:        if (out != NULL) {
                   1653:                xasprintf(&tmp, "-%c%s%s", copy[1], out, colon);
                   1654:                out = tmp;
                   1655:        }
                   1656:
                   1657:        for (i = 0; i < size; i++)
                   1658:                free((void *)list[i]);
                   1659:
                   1660: found:
                   1661:        free(copy);
                   1662:        free(list);
                   1663:        return (out);
1.1       nicm     1664: }