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

1.134   ! nicm        1: /* $OpenBSD: status.c,v 1.133 2015/08/28 12:16:28 nicm Exp $ */
1.1       nicm        2:
                      3: /*
                      4:  * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
                      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.123     nicm       32: char   *status_redraw_get_left(struct client *, time_t, int, struct grid_cell *,
                     33:            size_t *);
                     34: char   *status_redraw_get_right(struct client *, time_t, int,
                     35:            struct grid_cell *, size_t *);
                     36: char   *status_print(struct client *, struct winlink *, time_t,
                     37:            struct grid_cell *);
                     38: char   *status_replace(struct client *, struct winlink *, const char *, time_t);
1.42      nicm       39: void   status_message_callback(int, short, void *);
1.133     nicm       40: void   status_timer_callback(int, short, void *);
1.1       nicm       41:
1.65      nicm       42: const char *status_prompt_up_history(u_int *);
                     43: const char *status_prompt_down_history(u_int *);
                     44: void   status_prompt_add_history(const char *);
1.128     nicm       45:
                     46: const char **status_prompt_complete_list(u_int *, const char *);
                     47: char   *status_prompt_complete_prefix(const char **, u_int);
                     48: char   *status_prompt_complete(struct session *, const char *);
1.1       nicm       49:
1.130     nicm       50: char   *status_prompt_find_history_file(void);
                     51:
1.65      nicm       52: /* Status prompt history. */
1.128     nicm       53: #define PROMPT_HISTORY 100
                     54: char   **status_prompt_hlist;
                     55: u_int    status_prompt_hsize;
1.130     nicm       56:
                     57: /* Find the history file to load/save from/to. */
                     58: char *
                     59: status_prompt_find_history_file(void)
                     60: {
                     61:        const char      *home, *history_file;
                     62:        char            *path;
                     63:
                     64:        history_file = options_get_string(&global_options, "history-file");
                     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. */
                    147: void
                    148: status_timer_callback(unused int fd, unused short events, void *arg)
                    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);
                    163:        tv.tv_sec = options_get_number(&s->options, "status-interval");
                    164:
                    165:        if (tv.tv_sec != 0)
                    166:                evtimer_add(&c->status_timer, &tv);
                    167:        log_debug("client %d, status interval %d", c->ibuf.fd, (int)tv.tv_sec);
                    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:
                    181:        if (s != NULL && options_get_number(&s->options, "status"))
                    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:
                    201:        if (!options_get_number(&s->options, "status"))
                    202:                return (-1);
                    203:
                    204:        if (options_get_number(&s->options, "status-position") == 0)
                    205:                return (0);
                    206:        return (c->tty.sy - 1);
1.71      nicm      207: }
                    208:
1.48      nicm      209: /* Retrieve options for left string. */
                    210: char *
1.120     nicm      211: status_redraw_get_left(struct client *c, time_t t, int utf8flag,
                    212:     struct grid_cell *gc, 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.108     nicm      219:        style_apply_update(gc, &s->options, "status-left-style");
1.48      nicm      220:
1.120     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:
                    224:        *size = options_get_number(&s->options, "status-left-length");
                    225:        leftlen = screen_write_cstrlen(utf8flag, "%s", left);
                    226:        if (leftlen < *size)
                    227:                *size = leftlen;
                    228:        return (left);
                    229: }
                    230:
                    231: /* Retrieve options for right string. */
                    232: char *
1.120     nicm      233: status_redraw_get_right(struct client *c, time_t t, int utf8flag,
                    234:     struct grid_cell *gc, 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.108     nicm      241:        style_apply_update(gc, &s->options, "status-right-style");
1.48      nicm      242:
1.120     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:
                    246:        *size = options_get_number(&s->options, "status-right-length");
                    247:        rightlen = screen_write_cstrlen(utf8flag, "%s", right);
                    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;
                    260:        size_t           len;
1.73      nicm      261:
1.88      nicm      262:        x += c->wlmouse;
1.73      nicm      263:        RB_FOREACH(wl, winlinks, &s->windows) {
1.115     nicm      264:                oo = &wl->window->options;
1.125     nicm      265:                len = strlen(options_get_string(oo, "window-status-separator"));
1.115     nicm      266:
1.125     nicm      267:                if (x < wl->status_width)
                    268:                        return (wl->window);
1.115     nicm      269:                x -= wl->status_width + len;
1.73      nicm      270:        }
1.125     nicm      271:        return (NULL);
1.73      nicm      272: }
                    273:
1.1       nicm      274: /* Draw status for client on the last lines of given context. */
                    275: int
                    276: status_redraw(struct client *c)
                    277: {
1.48      nicm      278:        struct screen_write_ctx ctx;
                    279:        struct session         *s = c->session;
                    280:        struct winlink         *wl;
                    281:        struct screen           old_status, window_list;
                    282:        struct grid_cell        stdgc, lgc, rgc, gc;
1.91      nicm      283:        struct options         *oo;
1.48      nicm      284:        time_t                  t;
1.91      nicm      285:        char                   *left, *right, *sep;
1.48      nicm      286:        u_int                   offset, needed;
                    287:        u_int                   wlstart, wlwidth, wlavailable, wloffset, wlsize;
1.91      nicm      288:        size_t                  llen, rlen, seplen;
1.48      nicm      289:        int                     larrow, rarrow, utf8flag;
1.1       nicm      290:
1.49      nicm      291:        /* No status line? */
1.7       nicm      292:        if (c->tty.sy == 0 || !options_get_number(&s->options, "status"))
                    293:                return (1);
1.48      nicm      294:        left = right = NULL;
1.7       nicm      295:        larrow = rarrow = 0;
1.1       nicm      296:
1.133     nicm      297:        /* Store current time. */
                    298:        t = time(NULL);
1.48      nicm      299:
                    300:        /* Set up default colour. */
1.108     nicm      301:        style_apply(&stdgc, &s->options, "status-style");
1.1       nicm      302:
1.48      nicm      303:        /* Create the target screen. */
                    304:        memcpy(&old_status, &c->status, sizeof old_status);
                    305:        screen_init(&c->status, c->tty.sx, 1, 0);
                    306:        screen_write_start(&ctx, NULL, &c->status);
                    307:        for (offset = 0; offset < c->tty.sx; offset++)
                    308:                screen_write_putc(&ctx, &stdgc, ' ');
                    309:        screen_write_stop(&ctx);
                    310:
                    311:        /* If the height is one line, blank status line. */
                    312:        if (c->tty.sy <= 1)
                    313:                goto out;
1.1       nicm      314:
1.48      nicm      315:        /* Get UTF-8 flag. */
1.3       nicm      316:        utf8flag = options_get_number(&s->options, "status-utf8");
                    317:
1.48      nicm      318:        /* Work out left and right strings. */
                    319:        memcpy(&lgc, &stdgc, sizeof lgc);
                    320:        left = status_redraw_get_left(c, t, utf8flag, &lgc, &llen);
                    321:        memcpy(&rgc, &stdgc, sizeof rgc);
                    322:        right = status_redraw_get_right(c, t, utf8flag, &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.55      nicm      343:                wl->status_width =
1.48      nicm      344:                    screen_write_cstrlen(utf8flag, "%s", wl->status_text);
                    345:
1.1       nicm      346:                if (wl == s->curw)
1.48      nicm      347:                        wloffset = wlwidth;
1.91      nicm      348:
                    349:                oo = &wl->window->options;
                    350:                sep = options_get_string(oo, "window-status-separator");
                    351:                seplen = screen_write_strlen(utf8flag, "%s", sep);
                    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.55      nicm      361:                screen_write_cnputs(&ctx,
1.48      nicm      362:                    -1, &wl->status_cell, utf8flag, "%s", wl->status_text);
1.91      nicm      363:
                    364:                oo = &wl->window->options;
                    365:                sep = options_get_string(oo, "window-status-separator");
                    366:                screen_write_nputs(&ctx, -1, &stdgc, utf8flag, "%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.48      nicm      438:                screen_write_cnputs(&ctx, llen, &lgc, utf8flag, "%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.48      nicm      456:                screen_write_cnputs(&ctx, rlen, &rgc, utf8flag, "%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) {
                    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);
                    479:        screen_write_copy(&ctx, &window_list, wlstart, 0, wlwidth, 1);
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. */
                    497: 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.129     nicm      506:        ft = format_create_status(1);
                    507:        format_defaults(ft, c, NULL, wl, NULL);
1.1       nicm      508:
1.129     nicm      509:        expanded = format_expand_time(ft, fmt, t);
1.46      nicm      510:
1.99      nicm      511:        format_free(ft);
                    512:        return (expanded);
1.1       nicm      513: }
                    514:
1.46      nicm      515: /* Return winlink status line entry and adjust gc as necessary. */
1.1       nicm      516: char *
1.120     nicm      517: status_print(struct client *c, struct winlink *wl, time_t t,
                    518:     struct grid_cell *gc)
1.1       nicm      519: {
1.33      nicm      520:        struct options  *oo = &wl->window->options;
1.47      nicm      521:        struct session  *s = c->session;
                    522:        const char      *fmt;
                    523:        char            *text;
1.1       nicm      524:
1.108     nicm      525:        style_apply_update(gc, oo, "window-status-style");
1.47      nicm      526:        fmt = options_get_string(oo, "window-status-format");
1.14      nicm      527:        if (wl == s->curw) {
1.108     nicm      528:                style_apply_update(gc, oo, "window-status-current-style");
1.47      nicm      529:                fmt = options_get_string(oo, "window-status-current-format");
1.95      nicm      530:        }
1.108     nicm      531:        if (wl == TAILQ_FIRST(&s->lastw))
                    532:                style_apply_update(gc, oo, "window-status-last-style");
                    533:
                    534:        if (wl->flags & WINLINK_BELL)
                    535:                style_apply_update(gc, oo, "window-status-bell-style");
                    536:        else if (wl->flags & (WINLINK_ACTIVITY|WINLINK_SILENCE))
                    537:                style_apply_update(gc, oo, "window-status-activity-style");
1.1       nicm      538:
1.123     nicm      539:        text = status_replace(c, wl, fmt, t);
1.1       nicm      540:        return (text);
                    541: }
                    542:
1.46      nicm      543: /* Set a status line message. */
1.117     nicm      544: void
1.10      nicm      545: status_message_set(struct client *c, const char *fmt, ...)
1.1       nicm      546: {
1.44      nicm      547:        struct timeval           tv;
1.127     nicm      548:        struct message_entry    *msg, *msg1;
1.44      nicm      549:        va_list                  ap;
                    550:        int                      delay;
1.127     nicm      551:        u_int                    first, limit;
                    552:
                    553:        limit = options_get_number(&global_options, "message-limit");
1.1       nicm      554:
1.12      nicm      555:        status_prompt_clear(c);
                    556:        status_message_clear(c);
                    557:
1.28      nicm      558:        va_start(ap, fmt);
                    559:        xvasprintf(&c->message_string, fmt, ap);
                    560:        va_end(ap);
                    561:
1.127     nicm      562:        msg = xcalloc(1, sizeof *msg);
1.44      nicm      563:        msg->msg_time = time(NULL);
1.127     nicm      564:        msg->msg_num = c->message_next++;
1.44      nicm      565:        msg->msg = xstrdup(c->message_string);
1.127     nicm      566:        TAILQ_INSERT_TAIL(&c->message_log, msg, entry);
1.44      nicm      567:
1.127     nicm      568:        first = c->message_next - limit;
                    569:        TAILQ_FOREACH_SAFE(msg, &c->message_log, entry, msg1) {
                    570:                if (msg->msg_num >= first)
                    571:                        continue;
                    572:                free(msg->msg);
                    573:                TAILQ_REMOVE(&c->message_log, msg, entry);
                    574:                free(msg);
1.44      nicm      575:        }
                    576:
1.1       nicm      577:        delay = options_get_number(&c->session->options, "display-time");
                    578:        tv.tv_sec = delay / 1000;
                    579:        tv.tv_usec = (delay % 1000) * 1000L;
1.44      nicm      580:
1.110     nicm      581:        if (event_initialized(&c->message_timer))
1.90      nicm      582:                evtimer_del(&c->message_timer);
1.42      nicm      583:        evtimer_set(&c->message_timer, status_message_callback, c);
                    584:        evtimer_add(&c->message_timer, &tv);
1.1       nicm      585:
                    586:        c->tty.flags |= (TTY_NOCURSOR|TTY_FREEZE);
                    587:        c->flags |= CLIENT_STATUS;
                    588: }
                    589:
1.46      nicm      590: /* Clear status line message. */
1.1       nicm      591: void
                    592: status_message_clear(struct client *c)
                    593: {
                    594:        if (c->message_string == NULL)
                    595:                return;
                    596:
1.94      nicm      597:        free(c->message_string);
1.1       nicm      598:        c->message_string = NULL;
                    599:
                    600:        c->tty.flags &= ~(TTY_NOCURSOR|TTY_FREEZE);
1.12      nicm      601:        c->flags |= CLIENT_REDRAW; /* screen was frozen and may have changed */
1.7       nicm      602:
                    603:        screen_reinit(&c->status);
1.42      nicm      604: }
                    605:
1.46      nicm      606: /* Clear status line message after timer expires. */
1.42      nicm      607: void
                    608: status_message_callback(unused int fd, unused short event, void *data)
                    609: {
                    610:        struct client   *c = data;
                    611:
                    612:        status_message_clear(c);
1.1       nicm      613: }
                    614:
                    615: /* Draw client message on status line of present else on last line. */
                    616: int
                    617: status_message_redraw(struct client *c)
                    618: {
                    619:        struct screen_write_ctx         ctx;
                    620:        struct session                 *s = c->session;
                    621:        struct screen                   old_status;
                    622:        size_t                          len;
                    623:        struct grid_cell                gc;
1.51      nicm      624:        int                             utf8flag;
1.1       nicm      625:
                    626:        if (c->tty.sx == 0 || c->tty.sy == 0)
                    627:                return (0);
                    628:        memcpy(&old_status, &c->status, sizeof old_status);
                    629:        screen_init(&c->status, c->tty.sx, 1, 0);
                    630:
1.51      nicm      631:        utf8flag = options_get_number(&s->options, "status-utf8");
                    632:
                    633:        len = screen_write_strlen(utf8flag, "%s", c->message_string);
1.1       nicm      634:        if (len > c->tty.sx)
                    635:                len = c->tty.sx;
                    636:
1.108     nicm      637:        style_apply(&gc, &s->options, "message-style");
1.1       nicm      638:
                    639:        screen_write_start(&ctx, NULL, &c->status);
                    640:
                    641:        screen_write_cursormove(&ctx, 0, 0);
1.51      nicm      642:        screen_write_nputs(&ctx, len, &gc, utf8flag, "%s", c->message_string);
1.1       nicm      643:        for (; len < c->tty.sx; len++)
                    644:                screen_write_putc(&ctx, &gc, ' ');
                    645:
                    646:        screen_write_stop(&ctx);
                    647:
                    648:        if (grid_compare(c->status.grid, old_status.grid) == 0) {
                    649:                screen_free(&old_status);
                    650:                return (0);
                    651:        }
                    652:        screen_free(&old_status);
                    653:        return (1);
                    654: }
                    655:
1.46      nicm      656: /* Enable status line prompt. */
1.1       nicm      657: void
1.76      nicm      658: status_prompt_set(struct client *c, const char *msg, const char *input,
1.12      nicm      659:     int (*callbackfn)(void *, const char *), void (*freefn)(void *),
                    660:     void *data, int flags)
1.1       nicm      661: {
1.122     nicm      662:        struct format_tree      *ft;
                    663:        int                      keys;
                    664:        time_t                   t;
                    665:
                    666:        ft = format_create();
                    667:        format_defaults(ft, c, NULL, NULL, NULL);
                    668:        t = time(NULL);
1.20      nicm      669:
1.12      nicm      670:        status_message_clear(c);
                    671:        status_prompt_clear(c);
                    672:
1.124     nicm      673:        c->prompt_string = format_expand_time(ft, msg, t);
1.1       nicm      674:
1.124     nicm      675:        c->prompt_buffer = format_expand_time(ft, input, t);
1.76      nicm      676:        c->prompt_index = strlen(c->prompt_buffer);
1.1       nicm      677:
1.12      nicm      678:        c->prompt_callbackfn = callbackfn;
                    679:        c->prompt_freefn = freefn;
1.1       nicm      680:        c->prompt_data = data;
                    681:
                    682:        c->prompt_hindex = 0;
                    683:
                    684:        c->prompt_flags = flags;
                    685:
1.20      nicm      686:        keys = options_get_number(&c->session->options, "status-keys");
                    687:        if (keys == MODEKEY_EMACS)
1.21      nicm      688:                mode_key_init(&c->prompt_mdata, &mode_key_tree_emacs_edit);
1.20      nicm      689:        else
1.21      nicm      690:                mode_key_init(&c->prompt_mdata, &mode_key_tree_vi_edit);
1.1       nicm      691:
                    692:        c->tty.flags |= (TTY_NOCURSOR|TTY_FREEZE);
                    693:        c->flags |= CLIENT_STATUS;
1.122     nicm      694:
                    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;
                    726:
                    727:        ft = format_create();
                    728:        format_defaults(ft, c, NULL, NULL, NULL);
                    729:        t = time(NULL);
                    730:
1.94      nicm      731:        free(c->prompt_string);
1.124     nicm      732:        c->prompt_string = format_expand_time(ft, msg, t);
1.27      nicm      733:
1.94      nicm      734:        free(c->prompt_buffer);
1.124     nicm      735:        c->prompt_buffer = format_expand_time(ft, input, t);
1.76      nicm      736:        c->prompt_index = strlen(c->prompt_buffer);
1.27      nicm      737:
                    738:        c->prompt_hindex = 0;
                    739:
                    740:        c->flags |= CLIENT_STATUS;
1.122     nicm      741:
                    742:        format_free(ft);
1.1       nicm      743: }
                    744:
                    745: /* Draw client prompt on status line of present else on last line. */
                    746: int
                    747: status_prompt_redraw(struct client *c)
                    748: {
1.51      nicm      749:        struct screen_write_ctx         ctx;
1.1       nicm      750:        struct session                 *s = c->session;
                    751:        struct screen                   old_status;
1.29      nicm      752:        size_t                          i, size, left, len, off;
1.51      nicm      753:        struct grid_cell                gc, *gcp;
                    754:        int                             utf8flag;
1.1       nicm      755:
                    756:        if (c->tty.sx == 0 || c->tty.sy == 0)
                    757:                return (0);
                    758:        memcpy(&old_status, &c->status, sizeof old_status);
                    759:        screen_init(&c->status, c->tty.sx, 1, 0);
                    760:
1.51      nicm      761:        utf8flag = options_get_number(&s->options, "status-utf8");
                    762:
                    763:        len = screen_write_strlen(utf8flag, "%s", c->prompt_string);
1.1       nicm      764:        if (len > c->tty.sx)
                    765:                len = c->tty.sx;
1.51      nicm      766:        off = 0;
1.1       nicm      767:
1.79      nicm      768:        /* Change colours for command mode. */
1.108     nicm      769:        if (c->prompt_mdata.mode == 1)
                    770:                style_apply(&gc, &s->options, "message-command-style");
                    771:        else
                    772:                style_apply(&gc, &s->options, "message-style");
1.1       nicm      773:
                    774:        screen_write_start(&ctx, NULL, &c->status);
                    775:
                    776:        screen_write_cursormove(&ctx, 0, 0);
1.51      nicm      777:        screen_write_nputs(&ctx, len, &gc, utf8flag, "%s", c->prompt_string);
1.1       nicm      778:
                    779:        left = c->tty.sx - len;
                    780:        if (left != 0) {
1.51      nicm      781:                size = screen_write_strlen(utf8flag, "%s", c->prompt_buffer);
                    782:                if (c->prompt_index >= left) {
1.17      nicm      783:                        off = c->prompt_index - left + 1;
1.51      nicm      784:                        if (c->prompt_index == size)
1.1       nicm      785:                                left--;
                    786:                        size = left;
                    787:                }
1.51      nicm      788:                screen_write_nputs(
                    789:                    &ctx, left, &gc, utf8flag, "%s", c->prompt_buffer + off);
1.1       nicm      790:
1.55      nicm      791:                for (i = len + size; i < c->tty.sx; i++)
                    792:                        screen_write_putc(&ctx, &gc, ' ');
1.1       nicm      793:        }
                    794:
                    795:        screen_write_stop(&ctx);
1.51      nicm      796:
                    797:        /* Apply fake cursor. */
                    798:        off = len + c->prompt_index - off;
                    799:        gcp = grid_view_get_cell(c->status.grid, off, 0);
                    800:        gcp->attr ^= GRID_ATTR_REVERSE;
1.1       nicm      801:
                    802:        if (grid_compare(c->status.grid, old_status.grid) == 0) {
                    803:                screen_free(&old_status);
                    804:                return (0);
                    805:        }
                    806:        screen_free(&old_status);
                    807:        return (1);
                    808: }
                    809:
                    810: /* Handle keys in prompt. */
                    811: void
                    812: status_prompt_key(struct client *c, int key)
                    813: {
1.81      nicm      814:        struct session          *sess = c->session;
                    815:        struct options          *oo = &sess->options;
1.1       nicm      816:        struct paste_buffer     *pb;
1.81      nicm      817:        char                    *s, *first, *last, word[64], swapc;
1.134   ! nicm      818:        const char              *histstr, *bufdata, *wsep = NULL;
1.53      nicm      819:        u_char                   ch;
1.134   ! nicm      820:        size_t                   size, n, off, idx, bufsize;
1.1       nicm      821:
                    822:        size = strlen(c->prompt_buffer);
1.101     nicm      823:        switch (mode_key_lookup(&c->prompt_mdata, key, NULL)) {
1.20      nicm      824:        case MODEKEYEDIT_CURSORLEFT:
1.1       nicm      825:                if (c->prompt_index > 0) {
                    826:                        c->prompt_index--;
                    827:                        c->flags |= CLIENT_STATUS;
                    828:                }
                    829:                break;
1.79      nicm      830:        case MODEKEYEDIT_SWITCHMODE:
                    831:                c->flags |= CLIENT_STATUS;
                    832:                break;
1.20      nicm      833:        case MODEKEYEDIT_SWITCHMODEAPPEND:
1.79      nicm      834:                c->flags |= CLIENT_STATUS;
                    835:                /* FALLTHROUGH */
1.20      nicm      836:        case MODEKEYEDIT_CURSORRIGHT:
1.1       nicm      837:                if (c->prompt_index < size) {
                    838:                        c->prompt_index++;
                    839:                        c->flags |= CLIENT_STATUS;
                    840:                }
                    841:                break;
1.89      nicm      842:        case MODEKEYEDIT_SWITCHMODEBEGINLINE:
                    843:                c->flags |= CLIENT_STATUS;
                    844:                /* FALLTHROUGH */
1.20      nicm      845:        case MODEKEYEDIT_STARTOFLINE:
1.1       nicm      846:                if (c->prompt_index != 0) {
                    847:                        c->prompt_index = 0;
                    848:                        c->flags |= CLIENT_STATUS;
                    849:                }
                    850:                break;
1.89      nicm      851:        case MODEKEYEDIT_SWITCHMODEAPPENDLINE:
                    852:                c->flags |= CLIENT_STATUS;
                    853:                /* FALLTHROUGH */
1.20      nicm      854:        case MODEKEYEDIT_ENDOFLINE:
1.1       nicm      855:                if (c->prompt_index != size) {
                    856:                        c->prompt_index = size;
                    857:                        c->flags |= CLIENT_STATUS;
                    858:                }
                    859:                break;
1.20      nicm      860:        case MODEKEYEDIT_COMPLETE:
1.1       nicm      861:                if (*c->prompt_buffer == '\0')
                    862:                        break;
                    863:
                    864:                idx = c->prompt_index;
                    865:                if (idx != 0)
                    866:                        idx--;
                    867:
                    868:                /* Find the word we are in. */
                    869:                first = c->prompt_buffer + idx;
                    870:                while (first > c->prompt_buffer && *first != ' ')
                    871:                        first--;
                    872:                while (*first == ' ')
                    873:                        first++;
                    874:                last = c->prompt_buffer + idx;
                    875:                while (*last != '\0' && *last != ' ')
                    876:                        last++;
                    877:                while (*last == ' ')
                    878:                        last--;
                    879:                if (*last != '\0')
                    880:                        last++;
                    881:                if (last <= first ||
                    882:                    ((size_t) (last - first)) > (sizeof word) - 1)
                    883:                        break;
                    884:                memcpy(word, first, last - first);
                    885:                word[last - first] = '\0';
                    886:
                    887:                /* And try to complete it. */
1.128     nicm      888:                if ((s = status_prompt_complete(sess, word)) == NULL)
1.1       nicm      889:                        break;
                    890:
                    891:                /* Trim out word. */
                    892:                n = size - (last - c->prompt_buffer) + 1; /* with \0 */
                    893:                memmove(first, last, n);
                    894:                size -= last - first;
                    895:
                    896:                /* Insert the new word. */
1.55      nicm      897:                size += strlen(s);
1.1       nicm      898:                off = first - c->prompt_buffer;
1.116     nicm      899:                c->prompt_buffer = xrealloc(c->prompt_buffer, size + 1);
1.1       nicm      900:                first = c->prompt_buffer + off;
1.55      nicm      901:                memmove(first + strlen(s), first, n);
                    902:                memcpy(first, s, strlen(s));
1.1       nicm      903:
                    904:                c->prompt_index = (first - c->prompt_buffer) + strlen(s);
1.94      nicm      905:                free(s);
1.1       nicm      906:
                    907:                c->flags |= CLIENT_STATUS;
                    908:                break;
1.20      nicm      909:        case MODEKEYEDIT_BACKSPACE:
1.1       nicm      910:                if (c->prompt_index != 0) {
                    911:                        if (c->prompt_index == size)
                    912:                                c->prompt_buffer[--c->prompt_index] = '\0';
                    913:                        else {
                    914:                                memmove(c->prompt_buffer + c->prompt_index - 1,
                    915:                                    c->prompt_buffer + c->prompt_index,
                    916:                                    size + 1 - c->prompt_index);
                    917:                                c->prompt_index--;
                    918:                        }
                    919:                        c->flags |= CLIENT_STATUS;
                    920:                }
                    921:                break;
1.55      nicm      922:        case MODEKEYEDIT_DELETE:
1.105     nicm      923:        case MODEKEYEDIT_SWITCHMODESUBSTITUTE:
1.1       nicm      924:                if (c->prompt_index != size) {
                    925:                        memmove(c->prompt_buffer + c->prompt_index,
                    926:                            c->prompt_buffer + c->prompt_index + 1,
                    927:                            size + 1 - c->prompt_index);
1.18      nicm      928:                        c->flags |= CLIENT_STATUS;
                    929:                }
1.26      nicm      930:                break;
                    931:        case MODEKEYEDIT_DELETELINE:
1.105     nicm      932:        case MODEKEYEDIT_SWITCHMODESUBSTITUTELINE:
1.26      nicm      933:                *c->prompt_buffer = '\0';
                    934:                c->prompt_index = 0;
                    935:                c->flags |= CLIENT_STATUS;
1.18      nicm      936:                break;
1.20      nicm      937:        case MODEKEYEDIT_DELETETOENDOFLINE:
1.105     nicm      938:        case MODEKEYEDIT_SWITCHMODECHANGELINE:
1.18      nicm      939:                if (c->prompt_index < size) {
                    940:                        c->prompt_buffer[c->prompt_index] = '\0';
1.1       nicm      941:                        c->flags |= CLIENT_STATUS;
                    942:                }
                    943:                break;
1.81      nicm      944:        case MODEKEYEDIT_DELETEWORD:
                    945:                wsep = options_get_string(oo, "word-separators");
                    946:                idx = c->prompt_index;
                    947:
                    948:                /* Find a non-separator. */
                    949:                while (idx != 0) {
                    950:                        idx--;
                    951:                        if (!strchr(wsep, c->prompt_buffer[idx]))
                    952:                                break;
                    953:                }
                    954:
                    955:                /* Find the separator at the beginning of the word. */
                    956:                while (idx != 0) {
                    957:                        idx--;
                    958:                        if (strchr(wsep, c->prompt_buffer[idx])) {
                    959:                                /* Go back to the word. */
                    960:                                idx++;
                    961:                                break;
                    962:                        }
                    963:                }
                    964:
                    965:                memmove(c->prompt_buffer + idx,
                    966:                    c->prompt_buffer + c->prompt_index,
                    967:                    size + 1 - c->prompt_index);
                    968:                memset(c->prompt_buffer + size - (c->prompt_index - idx),
                    969:                    '\0', c->prompt_index - idx);
                    970:                c->prompt_index = idx;
                    971:                c->flags |= CLIENT_STATUS;
                    972:                break;
1.83      nicm      973:        case MODEKEYEDIT_NEXTSPACE:
                    974:                wsep = " ";
                    975:                /* FALLTHROUGH */
1.81      nicm      976:        case MODEKEYEDIT_NEXTWORD:
1.83      nicm      977:                if (wsep == NULL)
                    978:                        wsep = options_get_string(oo, "word-separators");
1.81      nicm      979:
                    980:                /* Find a separator. */
                    981:                while (c->prompt_index != size) {
                    982:                        c->prompt_index++;
                    983:                        if (strchr(wsep, c->prompt_buffer[c->prompt_index]))
                    984:                                break;
                    985:                }
                    986:
                    987:                /* Find the word right after the separation. */
                    988:                while (c->prompt_index != size) {
                    989:                        c->prompt_index++;
                    990:                        if (!strchr(wsep, c->prompt_buffer[c->prompt_index]))
                    991:                                break;
                    992:                }
                    993:
                    994:                c->flags |= CLIENT_STATUS;
                    995:                break;
1.83      nicm      996:        case MODEKEYEDIT_NEXTSPACEEND:
                    997:                wsep = " ";
                    998:                /* FALLTHROUGH */
1.81      nicm      999:        case MODEKEYEDIT_NEXTWORDEND:
1.83      nicm     1000:                if (wsep == NULL)
                   1001:                        wsep = options_get_string(oo, "word-separators");
1.81      nicm     1002:
                   1003:                /* Find a word. */
                   1004:                while (c->prompt_index != size) {
                   1005:                        c->prompt_index++;
                   1006:                        if (!strchr(wsep, c->prompt_buffer[c->prompt_index]))
                   1007:                                break;
                   1008:                }
                   1009:
                   1010:                /* Find the separator at the end of the word. */
                   1011:                while (c->prompt_index != size) {
                   1012:                        c->prompt_index++;
1.82      nicm     1013:                        if (strchr(wsep, c->prompt_buffer[c->prompt_index]))
1.81      nicm     1014:                                break;
                   1015:                }
1.106     nicm     1016:
                   1017:                /* Back up to the end-of-word like vi. */
                   1018:                if (options_get_number(oo, "status-keys") == MODEKEY_VI &&
                   1019:                    c->prompt_index != 0)
                   1020:                        c->prompt_index--;
1.81      nicm     1021:
                   1022:                c->flags |= CLIENT_STATUS;
                   1023:                break;
1.83      nicm     1024:        case MODEKEYEDIT_PREVIOUSSPACE:
                   1025:                wsep = " ";
                   1026:                /* FALLTHROUGH */
1.81      nicm     1027:        case MODEKEYEDIT_PREVIOUSWORD:
1.83      nicm     1028:                if (wsep == NULL)
                   1029:                        wsep = options_get_string(oo, "word-separators");
1.81      nicm     1030:
                   1031:                /* Find a non-separator. */
                   1032:                while (c->prompt_index != 0) {
                   1033:                        c->prompt_index--;
                   1034:                        if (!strchr(wsep, c->prompt_buffer[c->prompt_index]))
                   1035:                                break;
                   1036:                }
                   1037:
                   1038:                /* Find the separator at the beginning of the word. */
                   1039:                while (c->prompt_index != 0) {
                   1040:                        c->prompt_index--;
                   1041:                        if (strchr(wsep, c->prompt_buffer[c->prompt_index])) {
                   1042:                                /* Go back to the word. */
                   1043:                                c->prompt_index++;
                   1044:                                break;
                   1045:                        }
                   1046:                }
                   1047:
                   1048:                c->flags |= CLIENT_STATUS;
                   1049:                break;
1.20      nicm     1050:        case MODEKEYEDIT_HISTORYUP:
1.66      nicm     1051:                histstr = status_prompt_up_history(&c->prompt_hindex);
                   1052:                if (histstr == NULL)
1.1       nicm     1053:                        break;
1.94      nicm     1054:                free(c->prompt_buffer);
1.66      nicm     1055:                c->prompt_buffer = xstrdup(histstr);
1.1       nicm     1056:                c->prompt_index = strlen(c->prompt_buffer);
                   1057:                c->flags |= CLIENT_STATUS;
                   1058:                break;
1.20      nicm     1059:        case MODEKEYEDIT_HISTORYDOWN:
1.66      nicm     1060:                histstr = status_prompt_down_history(&c->prompt_hindex);
                   1061:                if (histstr == NULL)
1.65      nicm     1062:                        break;
1.94      nicm     1063:                free(c->prompt_buffer);
1.66      nicm     1064:                c->prompt_buffer = xstrdup(histstr);
1.1       nicm     1065:                c->prompt_index = strlen(c->prompt_buffer);
                   1066:                c->flags |= CLIENT_STATUS;
                   1067:                break;
1.20      nicm     1068:        case MODEKEYEDIT_PASTE:
1.134   ! nicm     1069:                if ((pb = paste_get_top(NULL)) == NULL)
1.1       nicm     1070:                        break;
1.134   ! nicm     1071:                bufdata = paste_buffer_data(pb, &bufsize);
        !          1072:                for (n = 0; n < bufsize; n++) {
        !          1073:                        ch = (u_char)bufdata[n];
1.53      nicm     1074:                        if (ch < 32 || ch == 127)
1.32      nicm     1075:                                break;
                   1076:                }
1.1       nicm     1077:
1.116     nicm     1078:                c->prompt_buffer = xrealloc(c->prompt_buffer, size + n + 1);
1.1       nicm     1079:                if (c->prompt_index == size) {
1.134   ! nicm     1080:                        memcpy(c->prompt_buffer + c->prompt_index, bufdata, n);
1.1       nicm     1081:                        c->prompt_index += n;
                   1082:                        c->prompt_buffer[c->prompt_index] = '\0';
                   1083:                } else {
                   1084:                        memmove(c->prompt_buffer + c->prompt_index + n,
                   1085:                            c->prompt_buffer + c->prompt_index,
                   1086:                            size + 1 - c->prompt_index);
1.134   ! nicm     1087:                        memcpy(c->prompt_buffer + c->prompt_index, bufdata, n);
1.1       nicm     1088:                        c->prompt_index += n;
                   1089:                }
                   1090:
                   1091:                c->flags |= CLIENT_STATUS;
1.30      nicm     1092:                break;
                   1093:        case MODEKEYEDIT_TRANSPOSECHARS:
                   1094:                idx = c->prompt_index;
                   1095:                if (idx < size)
                   1096:                        idx++;
                   1097:                if (idx >= 2) {
                   1098:                        swapc = c->prompt_buffer[idx - 2];
                   1099:                        c->prompt_buffer[idx - 2] = c->prompt_buffer[idx - 1];
                   1100:                        c->prompt_buffer[idx - 1] = swapc;
                   1101:                        c->prompt_index = idx;
                   1102:                        c->flags |= CLIENT_STATUS;
                   1103:                }
1.1       nicm     1104:                break;
1.55      nicm     1105:        case MODEKEYEDIT_ENTER:
1.25      nicm     1106:                if (*c->prompt_buffer != '\0')
1.65      nicm     1107:                        status_prompt_add_history(c->prompt_buffer);
1.25      nicm     1108:                if (c->prompt_callbackfn(c->prompt_data, c->prompt_buffer) == 0)
                   1109:                        status_prompt_clear(c);
                   1110:                break;
1.20      nicm     1111:        case MODEKEYEDIT_CANCEL:
1.12      nicm     1112:                if (c->prompt_callbackfn(c->prompt_data, NULL) == 0)
1.1       nicm     1113:                        status_prompt_clear(c);
                   1114:                break;
1.20      nicm     1115:        case MODEKEY_OTHER:
1.61      nicm     1116:                if ((key & 0xff00) != 0 || key < 32 || key == 127)
1.1       nicm     1117:                        break;
1.116     nicm     1118:                c->prompt_buffer = xrealloc(c->prompt_buffer, size + 2);
1.1       nicm     1119:
                   1120:                if (c->prompt_index == size) {
                   1121:                        c->prompt_buffer[c->prompt_index++] = key;
                   1122:                        c->prompt_buffer[c->prompt_index] = '\0';
                   1123:                } else {
                   1124:                        memmove(c->prompt_buffer + c->prompt_index + 1,
                   1125:                            c->prompt_buffer + c->prompt_index,
                   1126:                            size + 1 - c->prompt_index);
                   1127:                        c->prompt_buffer[c->prompt_index++] = key;
                   1128:                }
                   1129:
                   1130:                if (c->prompt_flags & PROMPT_SINGLE) {
1.12      nicm     1131:                        if (c->prompt_callbackfn(
1.1       nicm     1132:                            c->prompt_data, c->prompt_buffer) == 0)
                   1133:                                status_prompt_clear(c);
                   1134:                }
                   1135:
                   1136:                c->flags |= CLIENT_STATUS;
                   1137:                break;
                   1138:        default:
                   1139:                break;
                   1140:        }
                   1141: }
                   1142:
1.65      nicm     1143: /* Get previous line from the history. */
                   1144: const char *
                   1145: status_prompt_up_history(u_int *idx)
                   1146: {
                   1147:        /*
1.128     nicm     1148:         * History runs from 0 to size - 1. Index is from 0 to size. Zero is
                   1149:         * empty.
1.65      nicm     1150:         */
                   1151:
1.128     nicm     1152:        if (status_prompt_hsize == 0 || *idx == status_prompt_hsize)
1.65      nicm     1153:                return (NULL);
                   1154:        (*idx)++;
1.128     nicm     1155:        return (status_prompt_hlist[status_prompt_hsize - *idx]);
1.65      nicm     1156: }
                   1157:
                   1158: /* Get next line from the history. */
                   1159: const char *
                   1160: status_prompt_down_history(u_int *idx)
                   1161: {
1.128     nicm     1162:        if (status_prompt_hsize == 0 || *idx == 0)
1.65      nicm     1163:                return ("");
                   1164:        (*idx)--;
                   1165:        if (*idx == 0)
                   1166:                return ("");
1.128     nicm     1167:        return (status_prompt_hlist[status_prompt_hsize - *idx]);
1.65      nicm     1168: }
                   1169:
1.1       nicm     1170: /* Add line to the history. */
                   1171: void
1.65      nicm     1172: status_prompt_add_history(const char *line)
1.1       nicm     1173: {
1.128     nicm     1174:        size_t  size;
1.65      nicm     1175:
1.128     nicm     1176:        if (status_prompt_hsize > 0 &&
                   1177:            strcmp(status_prompt_hlist[status_prompt_hsize - 1], line) == 0)
1.1       nicm     1178:                return;
                   1179:
1.128     nicm     1180:        if (status_prompt_hsize == PROMPT_HISTORY) {
                   1181:                free(status_prompt_hlist[0]);
1.1       nicm     1182:
1.128     nicm     1183:                size = (PROMPT_HISTORY - 1) * sizeof *status_prompt_hlist;
                   1184:                memmove(&status_prompt_hlist[0], &status_prompt_hlist[1], size);
1.1       nicm     1185:
1.128     nicm     1186:                status_prompt_hlist[status_prompt_hsize - 1] = xstrdup(line);
                   1187:                return;
                   1188:        }
1.1       nicm     1189:
1.128     nicm     1190:        status_prompt_hlist = xreallocarray(status_prompt_hlist,
                   1191:            status_prompt_hsize + 1, sizeof *status_prompt_hlist);
                   1192:        status_prompt_hlist[status_prompt_hsize++] = xstrdup(line);
                   1193: }
                   1194:
                   1195: /* Build completion list. */
                   1196: const char **
                   1197: status_prompt_complete_list(u_int *size, const char *s)
                   1198: {
                   1199:        const char                              **list = NULL, **layout;
                   1200:        const struct cmd_entry                  **cmdent;
                   1201:        const struct options_table_entry         *oe;
                   1202:        const char                               *layouts[] = {
                   1203:                "even-horizontal", "even-vertical", "main-horizontal",
                   1204:                "main-vertical", "tiled", NULL
                   1205:        };
1.1       nicm     1206:
1.128     nicm     1207:        *size = 0;
1.1       nicm     1208:        for (cmdent = cmd_table; *cmdent != NULL; cmdent++) {
1.128     nicm     1209:                if (strncmp((*cmdent)->name, s, strlen(s)) == 0) {
                   1210:                        list = xreallocarray(list, (*size) + 1, sizeof *list);
                   1211:                        list[(*size)++] = (*cmdent)->name;
                   1212:                }
1.56      nicm     1213:        }
1.69      nicm     1214:        for (oe = server_options_table; oe->name != NULL; oe++) {
1.128     nicm     1215:                if (strncmp(oe->name, s, strlen(s)) == 0) {
                   1216:                        list = xreallocarray(list, (*size) + 1, sizeof *list);
                   1217:                        list[(*size)++] = oe->name;
                   1218:                }
1.69      nicm     1219:        }
                   1220:        for (oe = session_options_table; oe->name != NULL; oe++) {
1.128     nicm     1221:                if (strncmp(oe->name, s, strlen(s)) == 0) {
                   1222:                        list = xreallocarray(list, (*size) + 1, sizeof *list);
                   1223:                        list[(*size)++] = oe->name;
                   1224:                }
1.69      nicm     1225:        }
                   1226:        for (oe = window_options_table; oe->name != NULL; oe++) {
1.128     nicm     1227:                if (strncmp(oe->name, s, strlen(s)) == 0) {
                   1228:                        list = xreallocarray(list, (*size) + 1, sizeof *list);
                   1229:                        list[(*size)++] = oe->name;
                   1230:                }
                   1231:        }
                   1232:        for (layout = layouts; *layout != NULL; layout++) {
                   1233:                if (strncmp(*layout, s, strlen(s)) == 0) {
                   1234:                        list = xreallocarray(list, (*size) + 1, sizeof *list);
                   1235:                        list[(*size)++] = *layout;
                   1236:                }
                   1237:        }
                   1238:        return (list);
                   1239: }
                   1240:
                   1241: /* Find longest prefix. */
                   1242: char *
                   1243: status_prompt_complete_prefix(const char **list, u_int size)
                   1244: {
                   1245:        char     *out;
                   1246:        u_int     i;
                   1247:        size_t    j;
                   1248:
                   1249:        out = xstrdup(list[0]);
                   1250:        for (i = 1; i < size; i++) {
                   1251:                j = strlen(list[i]);
                   1252:                if (j > strlen(out))
                   1253:                        j = strlen(out);
                   1254:                for (; j > 0; j--) {
                   1255:                        if (out[j - 1] != list[i][j - 1])
                   1256:                                out[j - 1] = '\0';
                   1257:                }
1.1       nicm     1258:        }
1.128     nicm     1259:        return (out);
                   1260: }
1.1       nicm     1261:
1.128     nicm     1262: /* Complete word. */
                   1263: char *
                   1264: status_prompt_complete(struct session *sess, const char *s)
                   1265: {
                   1266:        const char      **list = NULL, *colon;
                   1267:        u_int             size = 0, i;
                   1268:        struct session   *s_loop;
                   1269:        struct winlink   *wl;
                   1270:        struct window    *w;
                   1271:        char             *copy, *out, *tmp;
                   1272:
                   1273:        if (*s == '\0')
1.1       nicm     1274:                return (NULL);
1.128     nicm     1275:        out = NULL;
                   1276:
                   1277:        if (strncmp(s, "-t", 2) != 0 && strncmp(s, "-s", 2) != 0) {
                   1278:                list = status_prompt_complete_list(&size, s);
                   1279:                if (size == 0)
                   1280:                        out = NULL;
                   1281:                else if (size == 1)
                   1282:                        xasprintf(&out, "%s ", list[0]);
                   1283:                else
                   1284:                        out = status_prompt_complete_prefix(list, size);
                   1285:                free(list);
                   1286:                return (out);
                   1287:        }
                   1288:        copy = xstrdup(s);
                   1289:
                   1290:        colon = ":";
                   1291:        if (copy[strlen(copy) - 1] == ':')
                   1292:                copy[strlen(copy) - 1] = '\0';
                   1293:        else
                   1294:                colon = "";
                   1295:        s = copy + 2;
1.1       nicm     1296:
1.128     nicm     1297:        RB_FOREACH(s_loop, sessions, &sessions) {
                   1298:                if (strncmp(s_loop->name, s, strlen(s)) == 0) {
                   1299:                        list = xreallocarray(list, size + 2, sizeof *list);
                   1300:                        list[size++] = s_loop->name;
                   1301:                }
                   1302:        }
                   1303:        if (size == 1) {
                   1304:                out = xstrdup(list[0]);
                   1305:                if (session_find(list[0]) != NULL)
                   1306:                        colon = ":";
                   1307:        } else if (size != 0)
                   1308:                out = status_prompt_complete_prefix(list, size);
                   1309:        if (out != NULL) {
                   1310:                xasprintf(&tmp, "-%c%s%s", copy[1], out, colon);
                   1311:                out = tmp;
                   1312:                goto found;
                   1313:        }
                   1314:
                   1315:        colon = "";
                   1316:        if (*s == ':') {
                   1317:                RB_FOREACH(wl, winlinks, &sess->windows) {
                   1318:                        xasprintf(&tmp, ":%s", wl->window->name);
                   1319:                        if (strncmp(tmp, s, strlen(s)) == 0){
                   1320:                                list = xreallocarray(list, size + 1,
                   1321:                                    sizeof *list);
                   1322:                                list[size++] = tmp;
                   1323:                                continue;
                   1324:                        }
                   1325:                        free(tmp);
1.1       nicm     1326:
1.128     nicm     1327:                        xasprintf(&tmp, ":%d", wl->idx);
                   1328:                        if (strncmp(tmp, s, strlen(s)) == 0) {
                   1329:                                list = xreallocarray(list, size + 1,
                   1330:                                    sizeof *list);
                   1331:                                list[size++] = tmp;
                   1332:                                continue;
                   1333:                        }
                   1334:                        free(tmp);
                   1335:                }
                   1336:        } else {
                   1337:                RB_FOREACH(s_loop, sessions, &sessions) {
                   1338:                        RB_FOREACH(wl, winlinks, &s_loop->windows) {
                   1339:                                w = wl->window;
                   1340:
                   1341:                                xasprintf(&tmp, "%s:%s", s_loop->name, w->name);
                   1342:                                if (strncmp(tmp, s, strlen(s)) == 0) {
                   1343:                                        list = xreallocarray(list, size + 1,
                   1344:                                            sizeof *list);
                   1345:                                        list[size++] = tmp;
                   1346:                                        continue;
                   1347:                                }
                   1348:                                free(tmp);
                   1349:
                   1350:                                xasprintf(&tmp, "%s:%d", s_loop->name, wl->idx);
                   1351:                                if (strncmp(tmp, s, strlen(s)) == 0) {
                   1352:                                        list = xreallocarray(list, size + 1,
                   1353:                                            sizeof *list);
                   1354:                                        list[size++] = tmp;
                   1355:                                        continue;
                   1356:                                }
                   1357:                                free(tmp);
                   1358:                        }
1.1       nicm     1359:                }
                   1360:        }
1.128     nicm     1361:        if (size == 1) {
                   1362:                out = xstrdup(list[0]);
                   1363:                colon = " ";
                   1364:        } else if (size != 0)
                   1365:                out = status_prompt_complete_prefix(list, size);
                   1366:        if (out != NULL) {
                   1367:                xasprintf(&tmp, "-%c%s%s", copy[1], out, colon);
                   1368:                out = tmp;
                   1369:        }
                   1370:
                   1371:        for (i = 0; i < size; i++)
                   1372:                free((void *)list[i]);
                   1373:
                   1374: found:
                   1375:        free(copy);
                   1376:        free(list);
                   1377:        return (out);
1.1       nicm     1378: }