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

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