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

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