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

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