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

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