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

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