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

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