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

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