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

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