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

1.241   ! nicm        1: /* $OpenBSD: status.c,v 1.240 2023/08/15 07:01:47 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);
1.224     nicm       36: static const char *status_prompt_up_history(u_int *, u_int);
                     37: static const char *status_prompt_down_history(u_int *, u_int);
                     38: static void     status_prompt_add_history(const char *, u_int);
1.151     nicm       39:
1.204     nicm       40: static char    *status_prompt_complete(struct client *, const char *, u_int);
1.205     nicm       41: static char    *status_prompt_complete_window_menu(struct client *,
1.206     nicm       42:                     struct session *, const char *, u_int, char);
1.204     nicm       43:
                     44: struct status_prompt_menu {
                     45:        struct client    *c;
                     46:        u_int             start;
                     47:        u_int             size;
                     48:        char            **list;
                     49:        char              flag;
                     50: };
1.130     nicm       51:
1.224     nicm       52: static const char      *prompt_type_strings[] = {
                     53:        "command",
                     54:        "search",
                     55:        "target",
                     56:        "window-target"
                     57: };
                     58:
1.65      nicm       59: /* Status prompt history. */
1.224     nicm       60: char           **status_prompt_hlist[PROMPT_NTYPES];
                     61: u_int            status_prompt_hsize[PROMPT_NTYPES];
1.130     nicm       62:
                     63: /* Find the history file to load/save from/to. */
1.151     nicm       64: static char *
1.130     nicm       65: status_prompt_find_history_file(void)
                     66: {
                     67:        const char      *home, *history_file;
                     68:        char            *path;
                     69:
1.137     nicm       70:        history_file = options_get_string(global_options, "history-file");
1.130     nicm       71:        if (*history_file == '\0')
                     72:                return (NULL);
                     73:        if (*history_file == '/')
                     74:                return (xstrdup(history_file));
                     75:
                     76:        if (history_file[0] != '~' || history_file[1] != '/')
                     77:                return (NULL);
                     78:        if ((home = find_home()) == NULL)
                     79:                return (NULL);
                     80:        xasprintf(&path, "%s%s", home, history_file + 1);
                     81:        return (path);
                     82: }
                     83:
1.224     nicm       84: /* Add loaded history item to the appropriate list. */
                     85: static void
                     86: status_prompt_add_typed_history(char *line)
                     87: {
                     88:        char                    *typestr;
                     89:        enum prompt_type         type = PROMPT_TYPE_INVALID;
                     90:
                     91:        typestr = strsep(&line, ":");
                     92:        if (line != NULL)
                     93:                type = status_prompt_type(typestr);
                     94:        if (type == PROMPT_TYPE_INVALID) {
                     95:                /*
                     96:                 * Invalid types are not expected, but this provides backward
                     97:                 * compatibility with old history files.
                     98:                 */
                     99:                if (line != NULL)
                    100:                        *(--line) = ':';
                    101:                status_prompt_add_history(typestr, PROMPT_TYPE_COMMAND);
                    102:        } else
                    103:                status_prompt_add_history(line, type);
                    104: }
                    105:
1.130     nicm      106: /* Load status prompt history from file. */
                    107: void
                    108: status_prompt_load_history(void)
                    109: {
                    110:        FILE    *f;
                    111:        char    *history_file, *line, *tmp;
                    112:        size_t   length;
                    113:
                    114:        if ((history_file = status_prompt_find_history_file()) == NULL)
                    115:                return;
                    116:        log_debug("loading history from %s", history_file);
                    117:
                    118:        f = fopen(history_file, "r");
                    119:        if (f == NULL) {
                    120:                log_debug("%s: %s", history_file, strerror(errno));
                    121:                free(history_file);
                    122:                return;
                    123:        }
                    124:        free(history_file);
                    125:
                    126:        for (;;) {
                    127:                if ((line = fgetln(f, &length)) == NULL)
                    128:                        break;
                    129:
                    130:                if (length > 0) {
                    131:                        if (line[length - 1] == '\n') {
                    132:                                line[length - 1] = '\0';
1.224     nicm      133:                                status_prompt_add_typed_history(line);
1.130     nicm      134:                        } else {
                    135:                                tmp = xmalloc(length + 1);
                    136:                                memcpy(tmp, line, length);
                    137:                                tmp[length] = '\0';
1.224     nicm      138:                                status_prompt_add_typed_history(tmp);
1.130     nicm      139:                                free(tmp);
                    140:                        }
                    141:                }
                    142:        }
                    143:        fclose(f);
                    144: }
                    145:
                    146: /* Save status prompt history to file. */
                    147: void
                    148: status_prompt_save_history(void)
                    149: {
                    150:        FILE    *f;
1.224     nicm      151:        u_int    i, type;
1.130     nicm      152:        char    *history_file;
                    153:
                    154:        if ((history_file = status_prompt_find_history_file()) == NULL)
                    155:                return;
                    156:        log_debug("saving history to %s", history_file);
                    157:
                    158:        f = fopen(history_file, "w");
                    159:        if (f == NULL) {
                    160:                log_debug("%s: %s", history_file, strerror(errno));
                    161:                free(history_file);
                    162:                return;
                    163:        }
                    164:        free(history_file);
                    165:
1.224     nicm      166:        for (type = 0; type < PROMPT_NTYPES; type++) {
                    167:                for (i = 0; i < status_prompt_hsize[type]; i++) {
                    168:                        fputs(prompt_type_strings[type], f);
                    169:                        fputc(':', f);
                    170:                        fputs(status_prompt_hlist[type][i], f);
                    171:                        fputc('\n', f);
                    172:                }
1.130     nicm      173:        }
                    174:        fclose(f);
                    175:
1.87      nicm      176: }
                    177:
1.133     nicm      178: /* Status timer callback. */
1.151     nicm      179: static void
1.141     nicm      180: status_timer_callback(__unused int fd, __unused short events, void *arg)
1.133     nicm      181: {
                    182:        struct client   *c = arg;
                    183:        struct session  *s = c->session;
                    184:        struct timeval   tv;
                    185:
1.175     nicm      186:        evtimer_del(&c->status.timer);
1.133     nicm      187:
                    188:        if (s == NULL)
                    189:                return;
                    190:
                    191:        if (c->message_string == NULL && c->prompt_string == NULL)
1.177     nicm      192:                c->flags |= CLIENT_REDRAWSTATUS;
1.133     nicm      193:
                    194:        timerclear(&tv);
1.137     nicm      195:        tv.tv_sec = options_get_number(s->options, "status-interval");
1.133     nicm      196:
                    197:        if (tv.tv_sec != 0)
1.175     nicm      198:                evtimer_add(&c->status.timer, &tv);
1.136     nicm      199:        log_debug("client %p, status interval %d", c, (int)tv.tv_sec);
1.133     nicm      200: }
                    201:
                    202: /* Start status timer for client. */
                    203: void
                    204: status_timer_start(struct client *c)
                    205: {
                    206:        struct session  *s = c->session;
                    207:
1.175     nicm      208:        if (event_initialized(&c->status.timer))
                    209:                evtimer_del(&c->status.timer);
1.133     nicm      210:        else
1.175     nicm      211:                evtimer_set(&c->status.timer, status_timer_callback, c);
1.133     nicm      212:
1.137     nicm      213:        if (s != NULL && options_get_number(s->options, "status"))
1.133     nicm      214:                status_timer_callback(-1, 0, c);
                    215: }
                    216:
                    217: /* Start status timer for all clients. */
                    218: void
                    219: status_timer_start_all(void)
                    220: {
                    221:        struct client   *c;
                    222:
                    223:        TAILQ_FOREACH(c, &clients, entry)
                    224:                status_timer_start(c);
                    225: }
                    226:
1.161     nicm      227: /* Update status cache. */
                    228: void
1.187     nicm      229: status_update_cache(struct session *s)
1.161     nicm      230: {
1.192     nicm      231:        s->statuslines = options_get_number(s->options, "status");
                    232:        if (s->statuslines == 0)
1.161     nicm      233:                s->statusat = -1;
                    234:        else if (options_get_number(s->options, "status-position") == 0)
                    235:                s->statusat = 0;
                    236:        else
                    237:                s->statusat = 1;
                    238: }
                    239:
1.87      nicm      240: /* Get screen line of status line. -1 means off. */
                    241: int
                    242: status_at_line(struct client *c)
                    243: {
                    244:        struct session  *s = c->session;
                    245:
1.198     nicm      246:        if (c->flags & (CLIENT_STATUSOFF|CLIENT_CONTROL))
1.169     nicm      247:                return (-1);
1.161     nicm      248:        if (s->statusat != 1)
                    249:                return (s->statusat);
1.182     nicm      250:        return (c->tty.sy - status_line_size(c));
1.169     nicm      251: }
                    252:
1.182     nicm      253: /* Get size of status line for client's session. 0 means off. */
1.169     nicm      254: u_int
1.182     nicm      255: status_line_size(struct client *c)
1.169     nicm      256: {
1.182     nicm      257:        struct session  *s = c->session;
                    258:
1.198     nicm      259:        if (c->flags & (CLIENT_STATUSOFF|CLIENT_CONTROL))
1.182     nicm      260:                return (0);
1.222     nicm      261:        if (s == NULL)
                    262:                return (options_get_number(global_s_options, "status"));
1.192     nicm      263:        return (s->statuslines);
1.71      nicm      264: }
                    265:
1.235     nicm      266: /* Get the prompt line number for client's session. 1 means at the bottom. */
                    267: static u_int
                    268: status_prompt_line_at(struct client *c)
                    269: {
                    270:        struct session  *s = c->session;
                    271:
                    272:        if (c->flags & (CLIENT_STATUSOFF|CLIENT_CONTROL))
                    273:                return (1);
1.236     nicm      274:        return (options_get_number(s->options, "message-line"));
1.235     nicm      275: }
                    276:
1.192     nicm      277: /* Get window at window list position. */
                    278: struct style_range *
                    279: status_get_range(struct client *c, u_int x, u_int y)
1.48      nicm      280: {
1.192     nicm      281:        struct status_line      *sl = &c->status;
                    282:        struct style_range      *sr;
1.48      nicm      283:
1.192     nicm      284:        if (y >= nitems(sl->entries))
                    285:                return (NULL);
                    286:        TAILQ_FOREACH(sr, &sl->entries[y].ranges, entry) {
                    287:                if (x >= sr->start && x < sr->end)
                    288:                        return (sr);
                    289:        }
                    290:        return (NULL);
1.48      nicm      291: }
                    292:
1.192     nicm      293: /* Free all ranges. */
                    294: static void
                    295: status_free_ranges(struct style_ranges *srs)
1.48      nicm      296: {
1.192     nicm      297:        struct style_range      *sr, *sr1;
1.48      nicm      298:
1.192     nicm      299:        TAILQ_FOREACH_SAFE(sr, srs, entry, sr1) {
                    300:                TAILQ_REMOVE(srs, sr, entry);
                    301:                free(sr);
1.73      nicm      302:        }
                    303: }
                    304:
1.189     nicm      305: /* Save old status line. */
                    306: static void
                    307: status_push_screen(struct client *c)
                    308: {
                    309:        struct status_line *sl = &c->status;
                    310:
                    311:        if (sl->active == &sl->screen) {
                    312:                sl->active = xmalloc(sizeof *sl->active);
                    313:                screen_init(sl->active, c->tty.sx, status_line_size(c), 0);
                    314:        }
                    315:        sl->references++;
                    316: }
                    317:
                    318: /* Restore old status line. */
                    319: static void
                    320: status_pop_screen(struct client *c)
                    321: {
                    322:        struct status_line *sl = &c->status;
                    323:
                    324:        if (--sl->references == 0) {
                    325:                screen_free(sl->active);
                    326:                free(sl->active);
                    327:                sl->active = &sl->screen;
                    328:        }
                    329: }
                    330:
1.187     nicm      331: /* Initialize status line. */
                    332: void
                    333: status_init(struct client *c)
                    334: {
                    335:        struct status_line      *sl = &c->status;
1.192     nicm      336:        u_int                    i;
                    337:
                    338:        for (i = 0; i < nitems(sl->entries); i++)
                    339:                TAILQ_INIT(&sl->entries[i].ranges);
1.187     nicm      340:
                    341:        screen_init(&sl->screen, c->tty.sx, 1, 0);
1.189     nicm      342:        sl->active = &sl->screen;
1.187     nicm      343: }
                    344:
1.186     nicm      345: /* Free status line. */
                    346: void
                    347: status_free(struct client *c)
                    348: {
                    349:        struct status_line      *sl = &c->status;
1.192     nicm      350:        u_int                    i;
                    351:
                    352:        for (i = 0; i < nitems(sl->entries); i++) {
                    353:                status_free_ranges(&sl->entries[i].ranges);
                    354:                free((void *)sl->entries[i].expanded);
                    355:        }
1.186     nicm      356:
                    357:        if (event_initialized(&sl->timer))
                    358:                evtimer_del(&sl->timer);
                    359:
1.189     nicm      360:        if (sl->active != &sl->screen) {
                    361:                screen_free(sl->active);
                    362:                free(sl->active);
                    363:        }
1.187     nicm      364:        screen_free(&sl->screen);
1.186     nicm      365: }
                    366:
                    367: /* Draw status line for client. */
1.1       nicm      368: int
                    369: status_redraw(struct client *c)
                    370: {
1.192     nicm      371:        struct status_line              *sl = &c->status;
                    372:        struct status_line_entry        *sle;
                    373:        struct session                  *s = c->session;
                    374:        struct screen_write_ctx          ctx;
                    375:        struct grid_cell                 gc;
1.197     nicm      376:        u_int                            lines, i, n, width = c->tty.sx;
1.203     nicm      377:        int                              flags, force = 0, changed = 0, fg, bg;
1.192     nicm      378:        struct options_entry            *o;
1.193     nicm      379:        union options_value             *ov;
1.192     nicm      380:        struct format_tree              *ft;
                    381:        char                            *expanded;
                    382:
                    383:        log_debug("%s enter", __func__);
1.1       nicm      384:
1.189     nicm      385:        /* Shouldn't get here if not the active screen. */
                    386:        if (sl->active != &sl->screen)
                    387:                fatalx("not the active screen");
1.167     nicm      388:
1.49      nicm      389:        /* No status line? */
1.182     nicm      390:        lines = status_line_size(c);
1.169     nicm      391:        if (c->tty.sy == 0 || lines == 0)
1.7       nicm      392:                return (1);
1.1       nicm      393:
1.207     nicm      394:        /* Create format tree. */
                    395:        flags = FORMAT_STATUS;
                    396:        if (c->flags & CLIENT_STATUSFORCE)
                    397:                flags |= FORMAT_FORCE;
                    398:        ft = format_create(c, NULL, FORMAT_NONE, flags);
                    399:        format_defaults(ft, c, NULL, NULL, NULL);
                    400:
1.48      nicm      401:        /* Set up default colour. */
1.207     nicm      402:        style_apply(&gc, s->options, "status-style", ft);
1.203     nicm      403:        fg = options_get_number(s->options, "status-fg");
1.226     nicm      404:        if (!COLOUR_DEFAULT(fg))
1.203     nicm      405:                gc.fg = fg;
                    406:        bg = options_get_number(s->options, "status-bg");
1.226     nicm      407:        if (!COLOUR_DEFAULT(bg))
1.203     nicm      408:                gc.bg = bg;
1.192     nicm      409:        if (!grid_cells_equal(&gc, &sl->style)) {
                    410:                force = 1;
                    411:                memcpy(&sl->style, &gc, sizeof sl->style);
                    412:        }
                    413:
                    414:        /* Resize the target screen. */
                    415:        if (screen_size_x(&sl->screen) != width ||
                    416:            screen_size_y(&sl->screen) != lines) {
                    417:                screen_resize(&sl->screen, width, lines, 0);
1.200     nicm      418:                changed = force = 1;
1.1       nicm      419:        }
1.208     nicm      420:        screen_write_start(&ctx, &sl->screen);
1.1       nicm      421:
1.192     nicm      422:        /* Write the status lines. */
                    423:        o = options_get(s->options, "status-format");
1.197     nicm      424:        if (o == NULL) {
                    425:                for (n = 0; n < width * lines; n++)
                    426:                        screen_write_putc(&ctx, &gc, ' ');
                    427:        } else {
1.192     nicm      428:                for (i = 0; i < lines; i++) {
                    429:                        screen_write_cursormove(&ctx, 0, i, 0);
                    430:
1.193     nicm      431:                        ov = options_array_get(o, i);
                    432:                        if (ov == NULL) {
1.197     nicm      433:                                for (n = 0; n < width; n++)
                    434:                                        screen_write_putc(&ctx, &gc, ' ');
1.192     nicm      435:                                continue;
                    436:                        }
                    437:                        sle = &sl->entries[i];
1.1       nicm      438:
1.193     nicm      439:                        expanded = format_expand_time(ft, ov->string);
1.192     nicm      440:                        if (!force &&
                    441:                            sle->expanded != NULL &&
                    442:                            strcmp(expanded, sle->expanded) == 0) {
                    443:                                free(expanded);
                    444:                                continue;
                    445:                        }
                    446:                        changed = 1;
1.1       nicm      447:
1.197     nicm      448:                        for (n = 0; n < width; n++)
                    449:                                screen_write_putc(&ctx, &gc, ' ');
                    450:                        screen_write_cursormove(&ctx, 0, i, 0);
                    451:
1.192     nicm      452:                        status_free_ranges(&sle->ranges);
1.228     nicm      453:                        format_draw(&ctx, &gc, width, expanded, &sle->ranges,
                    454:                            0);
1.1       nicm      455:
1.192     nicm      456:                        free(sle->expanded);
                    457:                        sle->expanded = expanded;
1.48      nicm      458:                }
                    459:        }
                    460:        screen_write_stop(&ctx);
1.1       nicm      461:
1.192     nicm      462:        /* Free the format tree. */
1.99      nicm      463:        format_free(ft);
1.1       nicm      464:
1.192     nicm      465:        /* Return if the status line has changed. */
                    466:        log_debug("%s exit: force=%d, changed=%d", __func__, force, changed);
                    467:        return (force || changed);
1.1       nicm      468: }
                    469:
1.46      nicm      470: /* Set a status line message. */
1.117     nicm      471: void
1.218     nicm      472: status_message_set(struct client *c, int delay, int ignore_styles,
1.221     nicm      473:     int ignore_keys, const char *fmt, ...)
1.1       nicm      474: {
1.241   ! nicm      475:        struct timeval   tv;
        !           476:        va_list          ap;
        !           477:        char            *s;
1.167     nicm      478:
1.28      nicm      479:        va_start(ap, fmt);
1.241   ! nicm      480:        xvasprintf(&s, fmt, ap);
1.28      nicm      481:        va_end(ap);
                    482:
1.241   ! nicm      483:        log_debug("%s: %s", __func__, s);
        !           484:
        !           485:        if (c == NULL) {
        !           486:                server_add_message("message: %s", s);
        !           487:                free(s);
        !           488:                return;
        !           489:        }
        !           490:
        !           491:        status_message_clear(c);
        !           492:        status_push_screen(c);
        !           493:        c->message_string = s;
        !           494:        server_add_message("%s message: %s", c->name, s);
1.44      nicm      495:
1.218     nicm      496:        /*
                    497:         * With delay -1, the display-time option is used; zero means wait for
                    498:         * key press; more than zero is the actual delay time in milliseconds.
                    499:         */
                    500:        if (delay == -1)
                    501:                delay = options_get_number(c->session->options, "display-time");
1.143     tim       502:        if (delay > 0) {
                    503:                tv.tv_sec = delay / 1000;
                    504:                tv.tv_usec = (delay % 1000) * 1000L;
                    505:
                    506:                if (event_initialized(&c->message_timer))
                    507:                        evtimer_del(&c->message_timer);
                    508:                evtimer_set(&c->message_timer, status_message_callback, c);
1.218     nicm      509:
1.143     tim       510:                evtimer_add(&c->message_timer, &tv);
                    511:        }
1.221     nicm      512:
                    513:        if (delay != 0)
                    514:                c->message_ignore_keys = ignore_keys;
                    515:        c->message_ignore_styles = ignore_styles;
1.1       nicm      516:
                    517:        c->tty.flags |= (TTY_NOCURSOR|TTY_FREEZE);
1.177     nicm      518:        c->flags |= CLIENT_REDRAWSTATUS;
1.1       nicm      519: }
                    520:
1.46      nicm      521: /* Clear status line message. */
1.1       nicm      522: void
                    523: status_message_clear(struct client *c)
                    524: {
                    525:        if (c->message_string == NULL)
                    526:                return;
                    527:
1.94      nicm      528:        free(c->message_string);
1.1       nicm      529:        c->message_string = NULL;
                    530:
1.156     nicm      531:        if (c->prompt_string == NULL)
                    532:                c->tty.flags &= ~(TTY_NOCURSOR|TTY_FREEZE);
1.177     nicm      533:        c->flags |= CLIENT_ALLREDRAWFLAGS; /* was frozen and may have changed */
1.7       nicm      534:
1.189     nicm      535:        status_pop_screen(c);
1.42      nicm      536: }
                    537:
1.46      nicm      538: /* Clear status line message after timer expires. */
1.151     nicm      539: static void
1.141     nicm      540: status_message_callback(__unused int fd, __unused short event, void *data)
1.42      nicm      541: {
                    542:        struct client   *c = data;
                    543:
                    544:        status_message_clear(c);
1.1       nicm      545: }
                    546:
                    547: /* Draw client message on status line of present else on last line. */
                    548: int
                    549: status_message_redraw(struct client *c)
                    550: {
1.189     nicm      551:        struct status_line      *sl = &c->status;
                    552:        struct screen_write_ctx  ctx;
                    553:        struct session          *s = c->session;
                    554:        struct screen            old_screen;
                    555:        size_t                   len;
1.235     nicm      556:        u_int                    lines, offset, messageline;
1.189     nicm      557:        struct grid_cell         gc;
1.207     nicm      558:        struct format_tree      *ft;
1.1       nicm      559:
                    560:        if (c->tty.sx == 0 || c->tty.sy == 0)
                    561:                return (0);
1.189     nicm      562:        memcpy(&old_screen, sl->active, sizeof old_screen);
1.169     nicm      563:
1.182     nicm      564:        lines = status_line_size(c);
1.189     nicm      565:        if (lines <= 1)
1.173     nicm      566:                lines = 1;
1.190     nicm      567:        screen_init(sl->active, c->tty.sx, lines, 0);
1.1       nicm      568:
1.235     nicm      569:        messageline = status_prompt_line_at(c);
                    570:        if (messageline > lines - 1)
                    571:                messageline = lines - 1;
                    572:
1.139     nicm      573:        len = screen_write_strlen("%s", c->message_string);
1.1       nicm      574:        if (len > c->tty.sx)
                    575:                len = c->tty.sx;
                    576:
1.207     nicm      577:        ft = format_create_defaults(NULL, c, NULL, NULL, NULL);
                    578:        style_apply(&gc, s->options, "message-style", ft);
                    579:        format_free(ft);
1.1       nicm      580:
1.208     nicm      581:        screen_write_start(&ctx, sl->active);
1.235     nicm      582:        screen_write_fast_copy(&ctx, &sl->screen, 0, 0, c->tty.sx, lines);
                    583:        screen_write_cursormove(&ctx, 0, messageline, 0);
1.192     nicm      584:        for (offset = 0; offset < c->tty.sx; offset++)
1.170     nicm      585:                screen_write_putc(&ctx, &gc, ' ');
1.235     nicm      586:        screen_write_cursormove(&ctx, 0, messageline, 0);
1.210     nicm      587:        if (c->message_ignore_styles)
                    588:                screen_write_nputs(&ctx, len, &gc, "%s", c->message_string);
                    589:        else
1.228     nicm      590:                format_draw(&ctx, &gc, c->tty.sx, c->message_string, NULL, 0);
1.1       nicm      591:        screen_write_stop(&ctx);
                    592:
1.189     nicm      593:        if (grid_compare(sl->active->grid, old_screen.grid) == 0) {
                    594:                screen_free(&old_screen);
1.1       nicm      595:                return (0);
                    596:        }
1.189     nicm      597:        screen_free(&old_screen);
1.1       nicm      598:        return (1);
                    599: }
                    600:
1.46      nicm      601: /* Enable status line prompt. */
1.1       nicm      602: void
1.211     nicm      603: status_prompt_set(struct client *c, struct cmd_find_state *fs,
                    604:     const char *msg, const char *input, prompt_input_cb inputcb,
1.224     nicm      605:     prompt_free_cb freecb, void *data, int flags, enum prompt_type prompt_type)
1.1       nicm      606: {
1.122     nicm      607:        struct format_tree      *ft;
1.219     nicm      608:        char                    *tmp;
1.122     nicm      609:
1.211     nicm      610:        if (fs != NULL)
                    611:                ft = format_create_from_state(NULL, c, fs);
                    612:        else
                    613:                ft = format_create_defaults(NULL, c, NULL, NULL, NULL);
1.152     nicm      614:
1.168     nicm      615:        if (input == NULL)
                    616:                input = "";
                    617:        if (flags & PROMPT_NOFORMAT)
                    618:                tmp = xstrdup(input);
                    619:        else
1.185     nicm      620:                tmp = format_expand_time(ft, input);
1.20      nicm      621:
1.12      nicm      622:        status_message_clear(c);
                    623:        status_prompt_clear(c);
1.189     nicm      624:        status_push_screen(c);
1.12      nicm      625:
1.185     nicm      626:        c->prompt_string = format_expand_time(ft, msg);
1.1       nicm      627:
1.219     nicm      628:        if (flags & PROMPT_INCREMENTAL) {
                    629:                c->prompt_last = xstrdup(tmp);
                    630:                c->prompt_buffer = utf8_fromcstr("");
                    631:        } else {
                    632:                c->prompt_last = NULL;
                    633:                c->prompt_buffer = utf8_fromcstr(tmp);
                    634:        }
1.152     nicm      635:        c->prompt_index = utf8_strlen(c->prompt_buffer);
1.1       nicm      636:
1.166     nicm      637:        c->prompt_inputcb = inputcb;
                    638:        c->prompt_freecb = freecb;
1.1       nicm      639:        c->prompt_data = data;
                    640:
1.224     nicm      641:        memset(c->prompt_hindex, 0, sizeof c->prompt_hindex);
1.1       nicm      642:
                    643:        c->prompt_flags = flags;
1.224     nicm      644:        c->prompt_type = prompt_type;
1.155     nicm      645:        c->prompt_mode = PROMPT_ENTRY;
1.1       nicm      646:
1.158     nicm      647:        if (~flags & PROMPT_INCREMENTAL)
                    648:                c->tty.flags |= (TTY_NOCURSOR|TTY_FREEZE);
1.177     nicm      649:        c->flags |= CLIENT_REDRAWSTATUS;
1.165     nicm      650:
1.219     nicm      651:        if (flags & PROMPT_INCREMENTAL)
                    652:                c->prompt_inputcb(c, c->prompt_data, "=", 0);
1.122     nicm      653:
1.152     nicm      654:        free(tmp);
1.122     nicm      655:        format_free(ft);
1.1       nicm      656: }
                    657:
1.46      nicm      658: /* Remove status line prompt. */
1.1       nicm      659: void
                    660: status_prompt_clear(struct client *c)
                    661: {
1.55      nicm      662:        if (c->prompt_string == NULL)
1.1       nicm      663:                return;
                    664:
1.166     nicm      665:        if (c->prompt_freecb != NULL && c->prompt_data != NULL)
                    666:                c->prompt_freecb(c->prompt_data);
1.1       nicm      667:
1.219     nicm      668:        free(c->prompt_last);
                    669:        c->prompt_last = NULL;
                    670:
1.94      nicm      671:        free(c->prompt_string);
1.1       nicm      672:        c->prompt_string = NULL;
                    673:
1.94      nicm      674:        free(c->prompt_buffer);
1.1       nicm      675:        c->prompt_buffer = NULL;
                    676:
1.181     nicm      677:        free(c->prompt_saved);
                    678:        c->prompt_saved = NULL;
                    679:
1.1       nicm      680:        c->tty.flags &= ~(TTY_NOCURSOR|TTY_FREEZE);
1.177     nicm      681:        c->flags |= CLIENT_ALLREDRAWFLAGS; /* was frozen and may have changed */
1.7       nicm      682:
1.189     nicm      683:        status_pop_screen(c);
1.27      nicm      684: }
                    685:
1.46      nicm      686: /* Update status line prompt with a new prompt string. */
1.27      nicm      687: void
1.76      nicm      688: status_prompt_update(struct client *c, const char *msg, const char *input)
1.27      nicm      689: {
1.122     nicm      690:        struct format_tree      *ft;
1.152     nicm      691:        char                    *tmp;
1.122     nicm      692:
1.164     nicm      693:        ft = format_create(c, NULL, FORMAT_NONE, 0);
1.122     nicm      694:        format_defaults(ft, c, NULL, NULL, NULL);
1.152     nicm      695:
1.185     nicm      696:        tmp = format_expand_time(ft, input);
1.122     nicm      697:
1.94      nicm      698:        free(c->prompt_string);
1.185     nicm      699:        c->prompt_string = format_expand_time(ft, msg);
1.27      nicm      700:
1.94      nicm      701:        free(c->prompt_buffer);
1.152     nicm      702:        c->prompt_buffer = utf8_fromcstr(tmp);
                    703:        c->prompt_index = utf8_strlen(c->prompt_buffer);
1.27      nicm      704:
1.224     nicm      705:        memset(c->prompt_hindex, 0, sizeof c->prompt_hindex);
1.27      nicm      706:
1.177     nicm      707:        c->flags |= CLIENT_REDRAWSTATUS;
1.122     nicm      708:
1.152     nicm      709:        free(tmp);
1.122     nicm      710:        format_free(ft);
1.1       nicm      711: }
                    712:
                    713: /* Draw client prompt on status line of present else on last line. */
                    714: int
                    715: status_prompt_redraw(struct client *c)
                    716: {
1.189     nicm      717:        struct status_line      *sl = &c->status;
1.152     nicm      718:        struct screen_write_ctx  ctx;
                    719:        struct session          *s = c->session;
1.189     nicm      720:        struct screen            old_screen;
                    721:        u_int                    i, lines, offset, left, start, width;
1.235     nicm      722:        u_int                    pcursor, pwidth, promptline;
1.152     nicm      723:        struct grid_cell         gc, cursorgc;
1.207     nicm      724:        struct format_tree      *ft;
1.1       nicm      725:
                    726:        if (c->tty.sx == 0 || c->tty.sy == 0)
                    727:                return (0);
1.189     nicm      728:        memcpy(&old_screen, sl->active, sizeof old_screen);
1.169     nicm      729:
1.182     nicm      730:        lines = status_line_size(c);
1.189     nicm      731:        if (lines <= 1)
1.173     nicm      732:                lines = 1;
1.189     nicm      733:        screen_init(sl->active, c->tty.sx, lines, 0);
1.1       nicm      734:
1.235     nicm      735:        promptline = status_prompt_line_at(c);
                    736:        if (promptline > lines - 1)
                    737:                promptline = lines - 1;
                    738:
1.207     nicm      739:        ft = format_create_defaults(NULL, c, NULL, NULL, NULL);
1.155     nicm      740:        if (c->prompt_mode == PROMPT_COMMAND)
1.207     nicm      741:                style_apply(&gc, s->options, "message-command-style", ft);
1.108     nicm      742:        else
1.207     nicm      743:                style_apply(&gc, s->options, "message-style", ft);
                    744:        format_free(ft);
1.1       nicm      745:
1.152     nicm      746:        memcpy(&cursorgc, &gc, sizeof cursorgc);
                    747:        cursorgc.attr ^= GRID_ATTR_REVERSE;
                    748:
1.232     nicm      749:        start = format_width(c->prompt_string);
1.152     nicm      750:        if (start > c->tty.sx)
                    751:                start = c->tty.sx;
                    752:
1.208     nicm      753:        screen_write_start(&ctx, sl->active);
1.235     nicm      754:        screen_write_fast_copy(&ctx, &sl->screen, 0, 0, c->tty.sx, lines);
                    755:        screen_write_cursormove(&ctx, 0, promptline, 0);
1.192     nicm      756:        for (offset = 0; offset < c->tty.sx; offset++)
1.170     nicm      757:                screen_write_putc(&ctx, &gc, ' ');
1.235     nicm      758:        screen_write_cursormove(&ctx, 0, promptline, 0);
1.232     nicm      759:        format_draw(&ctx, &gc, start, c->prompt_string, NULL, 0);
1.235     nicm      760:        screen_write_cursormove(&ctx, start, promptline, 0);
1.1       nicm      761:
1.152     nicm      762:        left = c->tty.sx - start;
                    763:        if (left == 0)
                    764:                goto finished;
                    765:
                    766:        pcursor = utf8_strwidth(c->prompt_buffer, c->prompt_index);
                    767:        pwidth = utf8_strwidth(c->prompt_buffer, -1);
                    768:        if (pcursor >= left) {
                    769:                /*
                    770:                 * The cursor would be outside the screen so start drawing
                    771:                 * with it on the right.
                    772:                 */
                    773:                offset = (pcursor - left) + 1;
                    774:                pwidth = left;
                    775:        } else
                    776:                offset = 0;
                    777:        if (pwidth > left)
                    778:                pwidth = left;
1.231     nicm      779:        c->prompt_cursor = start + c->prompt_index - offset;
1.152     nicm      780:
                    781:        width = 0;
                    782:        for (i = 0; c->prompt_buffer[i].size != 0; i++) {
                    783:                if (width < offset) {
                    784:                        width += c->prompt_buffer[i].width;
                    785:                        continue;
1.1       nicm      786:                }
1.152     nicm      787:                if (width >= offset + pwidth)
                    788:                        break;
                    789:                width += c->prompt_buffer[i].width;
                    790:                if (width > offset + pwidth)
                    791:                        break;
1.1       nicm      792:
1.152     nicm      793:                if (i != c->prompt_index) {
                    794:                        utf8_copy(&gc.data, &c->prompt_buffer[i]);
                    795:                        screen_write_cell(&ctx, &gc);
                    796:                } else {
                    797:                        utf8_copy(&cursorgc.data, &c->prompt_buffer[i]);
                    798:                        screen_write_cell(&ctx, &cursorgc);
                    799:                }
1.1       nicm      800:        }
1.189     nicm      801:        if (sl->active->cx < screen_size_x(sl->active) && c->prompt_index >= i)
1.152     nicm      802:                screen_write_putc(&ctx, &cursorgc, ' ');
1.1       nicm      803:
1.152     nicm      804: finished:
1.1       nicm      805:        screen_write_stop(&ctx);
1.51      nicm      806:
1.189     nicm      807:        if (grid_compare(sl->active->grid, old_screen.grid) == 0) {
                    808:                screen_free(&old_screen);
1.1       nicm      809:                return (0);
                    810:        }
1.189     nicm      811:        screen_free(&old_screen);
1.1       nicm      812:        return (1);
                    813: }
                    814:
1.152     nicm      815: /* Is this a separator? */
                    816: static int
                    817: status_prompt_in_list(const char *ws, const struct utf8_data *ud)
                    818: {
                    819:        if (ud->size != 1 || ud->width != 1)
                    820:                return (0);
                    821:        return (strchr(ws, *ud->data) != NULL);
                    822: }
                    823:
                    824: /* Is this a space? */
                    825: static int
                    826: status_prompt_space(const struct utf8_data *ud)
                    827: {
                    828:        if (ud->size != 1 || ud->width != 1)
                    829:                return (0);
                    830:        return (*ud->data == ' ');
                    831: }
                    832:
1.155     nicm      833: /*
1.224     nicm      834:  * Translate key from vi to emacs. Return 0 to drop key, 1 to process the key
1.155     nicm      835:  * as an emacs key; return 2 to append to the buffer.
                    836:  */
                    837: static int
                    838: status_prompt_translate_key(struct client *c, key_code key, key_code *new_key)
                    839: {
                    840:        if (c->prompt_mode == PROMPT_ENTRY) {
                    841:                switch (key) {
1.229     nicm      842:                case '\001': /* C-a */
1.155     nicm      843:                case '\003': /* C-c */
1.229     nicm      844:                case '\005': /* C-e */
1.202     nicm      845:                case '\007': /* C-g */
1.155     nicm      846:                case '\010': /* C-h */
                    847:                case '\011': /* Tab */
1.229     nicm      848:                case '\013': /* C-k */
                    849:                case '\016': /* C-n */
                    850:                case '\020': /* C-p */
                    851:                case '\024': /* C-t */
1.155     nicm      852:                case '\025': /* C-u */
                    853:                case '\027': /* C-w */
1.229     nicm      854:                case '\031': /* C-y */
1.155     nicm      855:                case '\n':
                    856:                case '\r':
1.229     nicm      857:                case KEYC_LEFT|KEYC_CTRL:
                    858:                case KEYC_RIGHT|KEYC_CTRL:
1.155     nicm      859:                case KEYC_BSPACE:
                    860:                case KEYC_DC:
                    861:                case KEYC_DOWN:
                    862:                case KEYC_END:
                    863:                case KEYC_HOME:
                    864:                case KEYC_LEFT:
                    865:                case KEYC_RIGHT:
                    866:                case KEYC_UP:
                    867:                        *new_key = key;
                    868:                        return (1);
                    869:                case '\033': /* Escape */
                    870:                        c->prompt_mode = PROMPT_COMMAND;
1.177     nicm      871:                        c->flags |= CLIENT_REDRAWSTATUS;
1.155     nicm      872:                        return (0);
                    873:                }
                    874:                *new_key = key;
                    875:                return (2);
                    876:        }
                    877:
                    878:        switch (key) {
1.229     nicm      879:        case KEYC_BSPACE:
                    880:                *new_key = KEYC_LEFT;
                    881:                return (1);
1.155     nicm      882:        case 'A':
                    883:        case 'I':
                    884:        case 'C':
                    885:        case 's':
                    886:        case 'a':
                    887:                c->prompt_mode = PROMPT_ENTRY;
1.177     nicm      888:                c->flags |= CLIENT_REDRAWSTATUS;
1.155     nicm      889:                break; /* switch mode and... */
                    890:        case 'S':
                    891:                c->prompt_mode = PROMPT_ENTRY;
1.177     nicm      892:                c->flags |= CLIENT_REDRAWSTATUS;
1.155     nicm      893:                *new_key = '\025'; /* C-u */
                    894:                return (1);
                    895:        case 'i':
                    896:        case '\033': /* Escape */
                    897:                c->prompt_mode = PROMPT_ENTRY;
1.177     nicm      898:                c->flags |= CLIENT_REDRAWSTATUS;
1.155     nicm      899:                return (0);
                    900:        }
                    901:
                    902:        switch (key) {
                    903:        case 'A':
                    904:        case '$':
                    905:                *new_key = KEYC_END;
                    906:                return (1);
                    907:        case 'I':
                    908:        case '0':
                    909:        case '^':
                    910:                *new_key = KEYC_HOME;
                    911:                return (1);
                    912:        case 'C':
                    913:        case 'D':
                    914:                *new_key = '\013'; /* C-k */
                    915:                return (1);
                    916:        case KEYC_BSPACE:
                    917:        case 'X':
                    918:                *new_key = KEYC_BSPACE;
                    919:                return (1);
                    920:        case 'b':
1.225     nicm      921:                *new_key = 'b'|KEYC_META;
                    922:                return (1);
1.155     nicm      923:        case 'B':
1.225     nicm      924:                *new_key = 'B'|KEYC_VI;
1.155     nicm      925:                return (1);
                    926:        case 'd':
1.229     nicm      927:                *new_key = '\025'; /* C-u */
1.155     nicm      928:                return (1);
                    929:        case 'e':
1.225     nicm      930:                *new_key = 'e'|KEYC_VI;
                    931:                return (1);
1.155     nicm      932:        case 'E':
1.225     nicm      933:                *new_key = 'E'|KEYC_VI;
                    934:                return (1);
1.155     nicm      935:        case 'w':
1.225     nicm      936:                *new_key = 'w'|KEYC_VI;
                    937:                return (1);
1.155     nicm      938:        case 'W':
1.225     nicm      939:                *new_key = 'W'|KEYC_VI;
1.155     nicm      940:                return (1);
                    941:        case 'p':
                    942:                *new_key = '\031'; /* C-y */
1.202     nicm      943:                return (1);
                    944:        case 'q':
                    945:                *new_key = '\003'; /* C-c */
1.155     nicm      946:                return (1);
                    947:        case 's':
                    948:        case KEYC_DC:
                    949:        case 'x':
                    950:                *new_key = KEYC_DC;
                    951:                return (1);
                    952:        case KEYC_DOWN:
                    953:        case 'j':
                    954:                *new_key = KEYC_DOWN;
                    955:                return (1);
                    956:        case KEYC_LEFT:
                    957:        case 'h':
                    958:                *new_key = KEYC_LEFT;
                    959:                return (1);
                    960:        case 'a':
                    961:        case KEYC_RIGHT:
                    962:        case 'l':
                    963:                *new_key = KEYC_RIGHT;
                    964:                return (1);
                    965:        case KEYC_UP:
                    966:        case 'k':
                    967:                *new_key = KEYC_UP;
                    968:                return (1);
                    969:        case '\010' /* C-h */:
                    970:        case '\003' /* C-c */:
                    971:        case '\n':
                    972:        case '\r':
                    973:                return (1);
                    974:        }
                    975:        return (0);
                    976: }
                    977:
1.199     nicm      978: /* Paste into prompt. */
                    979: static int
                    980: status_prompt_paste(struct client *c)
                    981: {
                    982:        struct paste_buffer     *pb;
                    983:        const char              *bufdata;
                    984:        size_t                   size, n, bufsize;
                    985:        u_int                    i;
                    986:        struct utf8_data        *ud, *udp;
                    987:        enum utf8_state          more;
                    988:
                    989:        size = utf8_strlen(c->prompt_buffer);
                    990:        if (c->prompt_saved != NULL) {
                    991:                ud = c->prompt_saved;
                    992:                n = utf8_strlen(c->prompt_saved);
                    993:        } else {
                    994:                if ((pb = paste_get_top(NULL)) == NULL)
                    995:                        return (0);
                    996:                bufdata = paste_buffer_data(pb, &bufsize);
1.200     nicm      997:                ud = xreallocarray(NULL, bufsize + 1, sizeof *ud);
1.199     nicm      998:                udp = ud;
                    999:                for (i = 0; i != bufsize; /* nothing */) {
                   1000:                        more = utf8_open(udp, bufdata[i]);
                   1001:                        if (more == UTF8_MORE) {
                   1002:                                while (++i != bufsize && more == UTF8_MORE)
                   1003:                                        more = utf8_append(udp, bufdata[i]);
                   1004:                                if (more == UTF8_DONE) {
                   1005:                                        udp++;
                   1006:                                        continue;
                   1007:                                }
                   1008:                                i -= udp->have;
                   1009:                        }
                   1010:                        if (bufdata[i] <= 31 || bufdata[i] >= 127)
                   1011:                                break;
                   1012:                        utf8_set(udp, bufdata[i]);
                   1013:                        udp++;
                   1014:                        i++;
                   1015:                }
                   1016:                udp->size = 0;
                   1017:                n = udp - ud;
                   1018:        }
                   1019:        if (n == 0)
                   1020:                return (0);
                   1021:
                   1022:        c->prompt_buffer = xreallocarray(c->prompt_buffer, size + n + 1,
                   1023:            sizeof *c->prompt_buffer);
                   1024:        if (c->prompt_index == size) {
                   1025:                memcpy(c->prompt_buffer + c->prompt_index, ud,
                   1026:                    n * sizeof *c->prompt_buffer);
                   1027:                c->prompt_index += n;
                   1028:                c->prompt_buffer[c->prompt_index].size = 0;
                   1029:        } else {
                   1030:                memmove(c->prompt_buffer + c->prompt_index + n,
                   1031:                    c->prompt_buffer + c->prompt_index,
                   1032:                    (size + 1 - c->prompt_index) * sizeof *c->prompt_buffer);
                   1033:                memcpy(c->prompt_buffer + c->prompt_index, ud,
                   1034:                    n * sizeof *c->prompt_buffer);
                   1035:                c->prompt_index += n;
                   1036:        }
                   1037:
                   1038:        if (ud != c->prompt_saved)
                   1039:                free(ud);
                   1040:        return (1);
                   1041: }
                   1042:
1.204     nicm     1043: /* Finish completion. */
                   1044: static int
                   1045: status_prompt_replace_complete(struct client *c, const char *s)
                   1046: {
                   1047:        char                     word[64], *allocated = NULL;
                   1048:        size_t                   size, n, off, idx, used;
                   1049:        struct utf8_data        *first, *last, *ud;
                   1050:
1.205     nicm     1051:        /* Work out where the cursor currently is. */
1.204     nicm     1052:        idx = c->prompt_index;
                   1053:        if (idx != 0)
                   1054:                idx--;
1.205     nicm     1055:        size = utf8_strlen(c->prompt_buffer);
1.204     nicm     1056:
                   1057:        /* Find the word we are in. */
                   1058:        first = &c->prompt_buffer[idx];
                   1059:        while (first > c->prompt_buffer && !status_prompt_space(first))
                   1060:                first--;
                   1061:        while (first->size != 0 && status_prompt_space(first))
                   1062:                first++;
                   1063:        last = &c->prompt_buffer[idx];
                   1064:        while (last->size != 0 && !status_prompt_space(last))
                   1065:                last++;
                   1066:        while (last > c->prompt_buffer && status_prompt_space(last))
                   1067:                last--;
                   1068:        if (last->size != 0)
                   1069:                last++;
1.205     nicm     1070:        if (last < first)
1.204     nicm     1071:                return (0);
                   1072:        if (s == NULL) {
                   1073:                used = 0;
                   1074:                for (ud = first; ud < last; ud++) {
                   1075:                        if (used + ud->size >= sizeof word)
                   1076:                                break;
                   1077:                        memcpy(word + used, ud->data, ud->size);
                   1078:                        used += ud->size;
                   1079:                }
                   1080:                if (ud != last)
                   1081:                        return (0);
                   1082:                word[used] = '\0';
                   1083:        }
                   1084:
                   1085:        /* Try to complete it. */
                   1086:        if (s == NULL) {
                   1087:                allocated = status_prompt_complete(c, word,
                   1088:                    first - c->prompt_buffer);
                   1089:                if (allocated == NULL)
                   1090:                        return (0);
                   1091:                s = allocated;
                   1092:        }
                   1093:
                   1094:        /* Trim out word. */
                   1095:        n = size - (last - c->prompt_buffer) + 1; /* with \0 */
                   1096:        memmove(first, last, n * sizeof *c->prompt_buffer);
                   1097:        size -= last - first;
                   1098:
                   1099:        /* Insert the new word. */
                   1100:        size += strlen(s);
                   1101:        off = first - c->prompt_buffer;
                   1102:        c->prompt_buffer = xreallocarray(c->prompt_buffer, size + 1,
                   1103:            sizeof *c->prompt_buffer);
                   1104:        first = c->prompt_buffer + off;
                   1105:        memmove(first + strlen(s), first, n * sizeof *c->prompt_buffer);
                   1106:        for (idx = 0; idx < strlen(s); idx++)
                   1107:                utf8_set(&first[idx], s[idx]);
                   1108:        c->prompt_index = (first - c->prompt_buffer) + strlen(s);
                   1109:
                   1110:        free(allocated);
                   1111:        return (1);
                   1112: }
                   1113:
1.225     nicm     1114: /* Prompt forward to the next beginning of a word. */
                   1115: static void
                   1116: status_prompt_forward_word(struct client *c, size_t size, int vi,
                   1117:     const char *separators)
                   1118: {
                   1119:        size_t           idx = c->prompt_index;
                   1120:        int              word_is_separators;
                   1121:
                   1122:        /* In emacs mode, skip until the first non-whitespace character. */
                   1123:        if (!vi)
                   1124:                while (idx != size &&
                   1125:                    status_prompt_space(&c->prompt_buffer[idx]))
                   1126:                        idx++;
                   1127:
                   1128:        /* Can't move forward if we're already at the end. */
                   1129:        if (idx == size) {
                   1130:                c->prompt_index = idx;
                   1131:                return;
                   1132:        }
                   1133:
                   1134:        /* Determine the current character class (separators or not). */
                   1135:        word_is_separators = status_prompt_in_list(separators,
                   1136:            &c->prompt_buffer[idx]) &&
                   1137:            !status_prompt_space(&c->prompt_buffer[idx]);
                   1138:
                   1139:        /* Skip ahead until the first space or opposite character class. */
                   1140:        do {
                   1141:                idx++;
                   1142:                if (status_prompt_space(&c->prompt_buffer[idx])) {
                   1143:                        /* In vi mode, go to the start of the next word. */
                   1144:                        if (vi)
                   1145:                                while (idx != size &&
                   1146:                                    status_prompt_space(&c->prompt_buffer[idx]))
                   1147:                                        idx++;
                   1148:                        break;
                   1149:                }
                   1150:        } while (idx != size && word_is_separators == status_prompt_in_list(
                   1151:            separators, &c->prompt_buffer[idx]));
                   1152:
                   1153:        c->prompt_index = idx;
                   1154: }
                   1155:
                   1156: /* Prompt forward to the next end of a word. */
                   1157: static void
                   1158: status_prompt_end_word(struct client *c, size_t size, const char *separators)
                   1159: {
                   1160:        size_t           idx = c->prompt_index;
                   1161:        int              word_is_separators;
                   1162:
                   1163:        /* Can't move forward if we're already at the end. */
                   1164:        if (idx == size)
                   1165:                return;
                   1166:
                   1167:        /* Find the next word. */
                   1168:        do {
                   1169:                idx++;
                   1170:                if (idx == size) {
                   1171:                        c->prompt_index = idx;
                   1172:                        return;
                   1173:                }
                   1174:        } while (status_prompt_space(&c->prompt_buffer[idx]));
                   1175:
                   1176:        /* Determine the character class (separators or not). */
                   1177:        word_is_separators = status_prompt_in_list(separators,
                   1178:            &c->prompt_buffer[idx]);
                   1179:
                   1180:        /* Skip ahead until the next space or opposite character class. */
                   1181:        do {
                   1182:                idx++;
                   1183:                if (idx == size)
                   1184:                        break;
                   1185:        } while (!status_prompt_space(&c->prompt_buffer[idx]) &&
                   1186:            word_is_separators == status_prompt_in_list(separators,
                   1187:            &c->prompt_buffer[idx]));
                   1188:
                   1189:        /* Back up to the previous character to stop at the end of the word. */
                   1190:        c->prompt_index = idx - 1;
                   1191: }
                   1192:
                   1193: /* Prompt backward to the previous beginning of a word. */
                   1194: static void
                   1195: status_prompt_backward_word(struct client *c, const char *separators)
                   1196: {
                   1197:        size_t  idx = c->prompt_index;
                   1198:        int     word_is_separators;
                   1199:
                   1200:        /* Find non-whitespace. */
                   1201:        while (idx != 0) {
                   1202:                --idx;
                   1203:                if (!status_prompt_space(&c->prompt_buffer[idx]))
                   1204:                        break;
                   1205:        }
                   1206:        word_is_separators = status_prompt_in_list(separators,
                   1207:            &c->prompt_buffer[idx]);
                   1208:
                   1209:        /* Find the character before the beginning of the word. */
                   1210:        while (idx != 0) {
                   1211:                --idx;
                   1212:                if (status_prompt_space(&c->prompt_buffer[idx]) ||
                   1213:                    word_is_separators != status_prompt_in_list(separators,
                   1214:                    &c->prompt_buffer[idx])) {
                   1215:                        /* Go back to the word. */
                   1216:                        idx++;
                   1217:                        break;
                   1218:                }
                   1219:        }
                   1220:        c->prompt_index = idx;
                   1221: }
                   1222:
1.1       nicm     1223: /* Handle keys in prompt. */
1.154     nicm     1224: int
1.138     nicm     1225: status_prompt_key(struct client *c, key_code key)
1.1       nicm     1226: {
1.152     nicm     1227:        struct options          *oo = c->session->options;
1.204     nicm     1228:        char                    *s, *cp, prefix = '=';
1.225     nicm     1229:        const char              *histstr, *separators = NULL, *keystring;
1.204     nicm     1230:        size_t                   size, idx;
                   1231:        struct utf8_data         tmp;
1.225     nicm     1232:        int                      keys, word_is_separators;
1.1       nicm     1233:
1.201     nicm     1234:        if (c->prompt_flags & PROMPT_KEY) {
1.213     nicm     1235:                keystring = key_string_lookup_key(key, 0);
1.201     nicm     1236:                c->prompt_inputcb(c, c->prompt_data, keystring, 1);
                   1237:                status_prompt_clear(c);
                   1238:                return (0);
                   1239:        }
1.152     nicm     1240:        size = utf8_strlen(c->prompt_buffer);
1.154     nicm     1241:
                   1242:        if (c->prompt_flags & PROMPT_NUMERIC) {
                   1243:                if (key >= '0' && key <= '9')
                   1244:                        goto append_key;
                   1245:                s = utf8_tocstr(c->prompt_buffer);
1.166     nicm     1246:                c->prompt_inputcb(c, c->prompt_data, s, 1);
1.154     nicm     1247:                status_prompt_clear(c);
                   1248:                free(s);
                   1249:                return (1);
                   1250:        }
1.213     nicm     1251:        key &= ~KEYC_MASK_FLAGS;
1.154     nicm     1252:
1.155     nicm     1253:        keys = options_get_number(c->session->options, "status-keys");
                   1254:        if (keys == MODEKEY_VI) {
                   1255:                switch (status_prompt_translate_key(c, key, &key)) {
                   1256:                case 1:
                   1257:                        goto process_key;
                   1258:                case 2:
                   1259:                        goto append_key;
                   1260:                default:
                   1261:                        return (0);
                   1262:                }
                   1263:        }
                   1264:
                   1265: process_key:
                   1266:        switch (key) {
                   1267:        case KEYC_LEFT:
                   1268:        case '\002': /* C-b */
1.1       nicm     1269:                if (c->prompt_index > 0) {
                   1270:                        c->prompt_index--;
1.158     nicm     1271:                        break;
1.1       nicm     1272:                }
                   1273:                break;
1.155     nicm     1274:        case KEYC_RIGHT:
                   1275:        case '\006': /* C-f */
1.1       nicm     1276:                if (c->prompt_index < size) {
                   1277:                        c->prompt_index++;
1.158     nicm     1278:                        break;
1.1       nicm     1279:                }
                   1280:                break;
1.155     nicm     1281:        case KEYC_HOME:
                   1282:        case '\001': /* C-a */
1.1       nicm     1283:                if (c->prompt_index != 0) {
                   1284:                        c->prompt_index = 0;
1.158     nicm     1285:                        break;
1.1       nicm     1286:                }
                   1287:                break;
1.155     nicm     1288:        case KEYC_END:
                   1289:        case '\005': /* C-e */
1.1       nicm     1290:                if (c->prompt_index != size) {
                   1291:                        c->prompt_index = size;
1.158     nicm     1292:                        break;
1.1       nicm     1293:                }
                   1294:                break;
1.155     nicm     1295:        case '\011': /* Tab */
1.206     nicm     1296:                if (status_prompt_replace_complete(c, NULL))
1.204     nicm     1297:                        goto changed;
                   1298:                break;
1.155     nicm     1299:        case KEYC_BSPACE:
                   1300:        case '\010': /* C-h */
1.1       nicm     1301:                if (c->prompt_index != 0) {
                   1302:                        if (c->prompt_index == size)
1.152     nicm     1303:                                c->prompt_buffer[--c->prompt_index].size = 0;
1.1       nicm     1304:                        else {
                   1305:                                memmove(c->prompt_buffer + c->prompt_index - 1,
                   1306:                                    c->prompt_buffer + c->prompt_index,
1.152     nicm     1307:                                    (size + 1 - c->prompt_index) *
                   1308:                                    sizeof *c->prompt_buffer);
1.1       nicm     1309:                                c->prompt_index--;
                   1310:                        }
1.158     nicm     1311:                        goto changed;
1.1       nicm     1312:                }
                   1313:                break;
1.155     nicm     1314:        case KEYC_DC:
                   1315:        case '\004': /* C-d */
1.1       nicm     1316:                if (c->prompt_index != size) {
                   1317:                        memmove(c->prompt_buffer + c->prompt_index,
                   1318:                            c->prompt_buffer + c->prompt_index + 1,
1.152     nicm     1319:                            (size + 1 - c->prompt_index) *
                   1320:                            sizeof *c->prompt_buffer);
1.158     nicm     1321:                        goto changed;
1.18      nicm     1322:                }
1.26      nicm     1323:                break;
1.155     nicm     1324:        case '\025': /* C-u */
1.152     nicm     1325:                c->prompt_buffer[0].size = 0;
1.26      nicm     1326:                c->prompt_index = 0;
1.158     nicm     1327:                goto changed;
1.155     nicm     1328:        case '\013': /* C-k */
1.18      nicm     1329:                if (c->prompt_index < size) {
1.152     nicm     1330:                        c->prompt_buffer[c->prompt_index].size = 0;
1.158     nicm     1331:                        goto changed;
1.1       nicm     1332:                }
                   1333:                break;
1.155     nicm     1334:        case '\027': /* C-w */
1.225     nicm     1335:                separators = options_get_string(oo, "word-separators");
1.81      nicm     1336:                idx = c->prompt_index;
                   1337:
1.225     nicm     1338:                /* Find non-whitespace. */
1.81      nicm     1339:                while (idx != 0) {
                   1340:                        idx--;
1.225     nicm     1341:                        if (!status_prompt_space(&c->prompt_buffer[idx]))
1.81      nicm     1342:                                break;
                   1343:                }
1.225     nicm     1344:                word_is_separators = status_prompt_in_list(separators,
                   1345:                    &c->prompt_buffer[idx]);
1.81      nicm     1346:
1.225     nicm     1347:                /* Find the character before the beginning of the word. */
1.81      nicm     1348:                while (idx != 0) {
                   1349:                        idx--;
1.225     nicm     1350:                        if (status_prompt_space(&c->prompt_buffer[idx]) ||
                   1351:                            word_is_separators != status_prompt_in_list(
                   1352:                            separators, &c->prompt_buffer[idx])) {
1.81      nicm     1353:                                /* Go back to the word. */
                   1354:                                idx++;
                   1355:                                break;
                   1356:                        }
                   1357:                }
                   1358:
1.181     nicm     1359:                free(c->prompt_saved);
                   1360:                c->prompt_saved = xcalloc(sizeof *c->prompt_buffer,
                   1361:                    (c->prompt_index - idx) + 1);
                   1362:                memcpy(c->prompt_saved, c->prompt_buffer + idx,
                   1363:                    (c->prompt_index - idx) * sizeof *c->prompt_buffer);
                   1364:
1.81      nicm     1365:                memmove(c->prompt_buffer + idx,
                   1366:                    c->prompt_buffer + c->prompt_index,
1.152     nicm     1367:                    (size + 1 - c->prompt_index) *
                   1368:                    sizeof *c->prompt_buffer);
1.81      nicm     1369:                memset(c->prompt_buffer + size - (c->prompt_index - idx),
1.152     nicm     1370:                    '\0', (c->prompt_index - idx) * sizeof *c->prompt_buffer);
1.81      nicm     1371:                c->prompt_index = idx;
1.152     nicm     1372:
1.158     nicm     1373:                goto changed;
1.225     nicm     1374:        case KEYC_RIGHT|KEYC_CTRL:
1.212     nicm     1375:        case 'f'|KEYC_META:
1.225     nicm     1376:                separators = options_get_string(oo, "word-separators");
                   1377:                status_prompt_forward_word(c, size, 0, separators);
                   1378:                goto changed;
                   1379:        case 'E'|KEYC_VI:
                   1380:                status_prompt_end_word(c, size, "");
                   1381:                goto changed;
                   1382:        case 'e'|KEYC_VI:
                   1383:                separators = options_get_string(oo, "word-separators");
                   1384:                status_prompt_end_word(c, size, separators);
                   1385:                goto changed;
                   1386:        case 'W'|KEYC_VI:
                   1387:                status_prompt_forward_word(c, size, 1, "");
                   1388:                goto changed;
                   1389:        case 'w'|KEYC_VI:
                   1390:                separators = options_get_string(oo, "word-separators");
                   1391:                status_prompt_forward_word(c, size, 1, separators);
                   1392:                goto changed;
                   1393:        case 'B'|KEYC_VI:
                   1394:                status_prompt_backward_word(c, "");
1.158     nicm     1395:                goto changed;
1.225     nicm     1396:        case KEYC_LEFT|KEYC_CTRL:
1.212     nicm     1397:        case 'b'|KEYC_META:
1.225     nicm     1398:                separators = options_get_string(oo, "word-separators");
                   1399:                status_prompt_backward_word(c, separators);
1.158     nicm     1400:                goto changed;
1.155     nicm     1401:        case KEYC_UP:
                   1402:        case '\020': /* C-p */
1.224     nicm     1403:                histstr = status_prompt_up_history(c->prompt_hindex,
                   1404:                    c->prompt_type);
1.66      nicm     1405:                if (histstr == NULL)
1.1       nicm     1406:                        break;
1.94      nicm     1407:                free(c->prompt_buffer);
1.152     nicm     1408:                c->prompt_buffer = utf8_fromcstr(histstr);
                   1409:                c->prompt_index = utf8_strlen(c->prompt_buffer);
1.158     nicm     1410:                goto changed;
1.155     nicm     1411:        case KEYC_DOWN:
                   1412:        case '\016': /* C-n */
1.224     nicm     1413:                histstr = status_prompt_down_history(c->prompt_hindex,
                   1414:                    c->prompt_type);
1.66      nicm     1415:                if (histstr == NULL)
1.65      nicm     1416:                        break;
1.94      nicm     1417:                free(c->prompt_buffer);
1.152     nicm     1418:                c->prompt_buffer = utf8_fromcstr(histstr);
                   1419:                c->prompt_index = utf8_strlen(c->prompt_buffer);
1.158     nicm     1420:                goto changed;
1.155     nicm     1421:        case '\031': /* C-y */
1.199     nicm     1422:                if (status_prompt_paste(c))
                   1423:                        goto changed;
                   1424:                break;
1.155     nicm     1425:        case '\024': /* C-t */
1.30      nicm     1426:                idx = c->prompt_index;
                   1427:                if (idx < size)
                   1428:                        idx++;
                   1429:                if (idx >= 2) {
1.152     nicm     1430:                        utf8_copy(&tmp, &c->prompt_buffer[idx - 2]);
                   1431:                        utf8_copy(&c->prompt_buffer[idx - 2],
                   1432:                            &c->prompt_buffer[idx - 1]);
                   1433:                        utf8_copy(&c->prompt_buffer[idx - 1], &tmp);
1.30      nicm     1434:                        c->prompt_index = idx;
1.158     nicm     1435:                        goto changed;
1.30      nicm     1436:                }
1.1       nicm     1437:                break;
1.155     nicm     1438:        case '\r':
                   1439:        case '\n':
1.152     nicm     1440:                s = utf8_tocstr(c->prompt_buffer);
                   1441:                if (*s != '\0')
1.224     nicm     1442:                        status_prompt_add_history(s, c->prompt_type);
1.166     nicm     1443:                if (c->prompt_inputcb(c, c->prompt_data, s, 1) == 0)
1.25      nicm     1444:                        status_prompt_clear(c);
1.152     nicm     1445:                free(s);
1.25      nicm     1446:                break;
1.155     nicm     1447:        case '\033': /* Escape */
                   1448:        case '\003': /* C-c */
1.174     nicm     1449:        case '\007': /* C-g */
1.166     nicm     1450:                if (c->prompt_inputcb(c, c->prompt_data, NULL, 1) == 0)
1.1       nicm     1451:                        status_prompt_clear(c);
                   1452:                break;
1.158     nicm     1453:        case '\022': /* C-r */
1.219     nicm     1454:                if (~c->prompt_flags & PROMPT_INCREMENTAL)
                   1455:                        break;
                   1456:                if (c->prompt_buffer[0].size == 0) {
                   1457:                        prefix = '=';
1.227     nicm     1458:                        free(c->prompt_buffer);
1.219     nicm     1459:                        c->prompt_buffer = utf8_fromcstr(c->prompt_last);
                   1460:                        c->prompt_index = utf8_strlen(c->prompt_buffer);
                   1461:                } else
1.158     nicm     1462:                        prefix = '-';
1.219     nicm     1463:                goto changed;
1.158     nicm     1464:        case '\023': /* C-s */
1.219     nicm     1465:                if (~c->prompt_flags & PROMPT_INCREMENTAL)
                   1466:                        break;
                   1467:                if (c->prompt_buffer[0].size == 0) {
                   1468:                        prefix = '=';
1.227     nicm     1469:                        free(c->prompt_buffer);
1.219     nicm     1470:                        c->prompt_buffer = utf8_fromcstr(c->prompt_last);
                   1471:                        c->prompt_index = utf8_strlen(c->prompt_buffer);
                   1472:                } else
1.158     nicm     1473:                        prefix = '+';
1.219     nicm     1474:                goto changed;
1.158     nicm     1475:        default:
                   1476:                goto append_key;
1.154     nicm     1477:        }
1.152     nicm     1478:
1.177     nicm     1479:        c->flags |= CLIENT_REDRAWSTATUS;
1.158     nicm     1480:        return (0);
                   1481:
1.154     nicm     1482: append_key:
1.216     nicm     1483:        if (key <= 0x7f)
1.215     nicm     1484:                utf8_set(&tmp, key);
1.225     nicm     1485:        else if (KEYC_IS_UNICODE(key))
                   1486:                utf8_to_data(key, &tmp);
1.215     nicm     1487:        else
1.225     nicm     1488:                return (0);
1.1       nicm     1489:
1.154     nicm     1490:        c->prompt_buffer = xreallocarray(c->prompt_buffer, size + 2,
                   1491:            sizeof *c->prompt_buffer);
1.1       nicm     1492:
1.154     nicm     1493:        if (c->prompt_index == size) {
                   1494:                utf8_copy(&c->prompt_buffer[c->prompt_index], &tmp);
                   1495:                c->prompt_index++;
                   1496:                c->prompt_buffer[c->prompt_index].size = 0;
                   1497:        } else {
                   1498:                memmove(c->prompt_buffer + c->prompt_index + 1,
                   1499:                    c->prompt_buffer + c->prompt_index,
                   1500:                    (size + 1 - c->prompt_index) *
                   1501:                    sizeof *c->prompt_buffer);
                   1502:                utf8_copy(&c->prompt_buffer[c->prompt_index], &tmp);
                   1503:                c->prompt_index++;
                   1504:        }
1.1       nicm     1505:
1.154     nicm     1506:        if (c->prompt_flags & PROMPT_SINGLE) {
1.220     nicm     1507:                if (utf8_strlen(c->prompt_buffer) != 1)
1.154     nicm     1508:                        status_prompt_clear(c);
1.220     nicm     1509:                else {
                   1510:                        s = utf8_tocstr(c->prompt_buffer);
                   1511:                        if (c->prompt_inputcb(c, c->prompt_data, s, 1) == 0)
                   1512:                                status_prompt_clear(c);
                   1513:                        free(s);
                   1514:                }
1.1       nicm     1515:        }
1.154     nicm     1516:
1.158     nicm     1517: changed:
1.177     nicm     1518:        c->flags |= CLIENT_REDRAWSTATUS;
1.158     nicm     1519:        if (c->prompt_flags & PROMPT_INCREMENTAL) {
                   1520:                s = utf8_tocstr(c->prompt_buffer);
                   1521:                xasprintf(&cp, "%c%s", prefix, s);
1.166     nicm     1522:                c->prompt_inputcb(c, c->prompt_data, cp, 0);
1.158     nicm     1523:                free(cp);
                   1524:                free(s);
                   1525:        }
1.154     nicm     1526:        return (0);
1.1       nicm     1527: }
                   1528:
1.65      nicm     1529: /* Get previous line from the history. */
1.151     nicm     1530: static const char *
1.224     nicm     1531: status_prompt_up_history(u_int *idx, u_int type)
1.65      nicm     1532: {
                   1533:        /*
1.128     nicm     1534:         * History runs from 0 to size - 1. Index is from 0 to size. Zero is
                   1535:         * empty.
1.65      nicm     1536:         */
                   1537:
1.224     nicm     1538:        if (status_prompt_hsize[type] == 0 ||
                   1539:            idx[type] == status_prompt_hsize[type])
1.65      nicm     1540:                return (NULL);
1.224     nicm     1541:        idx[type]++;
                   1542:        return (status_prompt_hlist[type][status_prompt_hsize[type] - idx[type]]);
1.65      nicm     1543: }
                   1544:
                   1545: /* Get next line from the history. */
1.151     nicm     1546: static const char *
1.224     nicm     1547: status_prompt_down_history(u_int *idx, u_int type)
1.65      nicm     1548: {
1.224     nicm     1549:        if (status_prompt_hsize[type] == 0 || idx[type] == 0)
1.65      nicm     1550:                return ("");
1.224     nicm     1551:        idx[type]--;
                   1552:        if (idx[type] == 0)
1.65      nicm     1553:                return ("");
1.224     nicm     1554:        return (status_prompt_hlist[type][status_prompt_hsize[type] - idx[type]]);
1.65      nicm     1555: }
                   1556:
1.1       nicm     1557: /* Add line to the history. */
1.151     nicm     1558: static void
1.224     nicm     1559: status_prompt_add_history(const char *line, u_int type)
1.1       nicm     1560: {
1.224     nicm     1561:        u_int   i, oldsize, newsize, freecount, hlimit, new = 1;
                   1562:        size_t  movesize;
1.65      nicm     1563:
1.224     nicm     1564:        oldsize = status_prompt_hsize[type];
                   1565:        if (oldsize > 0 &&
                   1566:            strcmp(status_prompt_hlist[type][oldsize - 1], line) == 0)
                   1567:                new = 0;
                   1568:
                   1569:        hlimit = options_get_number(global_options, "prompt-history-limit");
                   1570:        if (hlimit > oldsize) {
                   1571:                if (new == 0)
                   1572:                        return;
                   1573:                newsize = oldsize + new;
                   1574:        } else {
                   1575:                newsize = hlimit;
                   1576:                freecount = oldsize + new - newsize;
                   1577:                if (freecount > oldsize)
                   1578:                        freecount = oldsize;
                   1579:                if (freecount == 0)
                   1580:                        return;
                   1581:                for (i = 0; i < freecount; i++)
                   1582:                        free(status_prompt_hlist[type][i]);
                   1583:                movesize = (oldsize - freecount) *
                   1584:                    sizeof *status_prompt_hlist[type];
                   1585:                if (movesize > 0) {
                   1586:                        memmove(&status_prompt_hlist[type][0],
                   1587:                            &status_prompt_hlist[type][freecount], movesize);
                   1588:                }
1.128     nicm     1589:        }
1.1       nicm     1590:
1.224     nicm     1591:        if (newsize == 0) {
                   1592:                free(status_prompt_hlist[type]);
                   1593:                status_prompt_hlist[type] = NULL;
                   1594:        } else if (newsize != oldsize) {
                   1595:                status_prompt_hlist[type] =
                   1596:                    xreallocarray(status_prompt_hlist[type], newsize,
                   1597:                        sizeof *status_prompt_hlist[type]);
                   1598:        }
                   1599:
                   1600:        if (new == 1 && newsize > 0)
                   1601:                status_prompt_hlist[type][newsize - 1] = xstrdup(line);
                   1602:        status_prompt_hsize[type] = newsize;
1.128     nicm     1603: }
                   1604:
1.234     nicm     1605: /* Add to completion list. */
                   1606: static void
                   1607: status_prompt_add_list(char ***list, u_int *size, const char *s)
                   1608: {
                   1609:        u_int   i;
                   1610:
                   1611:        for (i = 0; i < *size; i++) {
                   1612:                if (strcmp((*list)[i], s) == 0)
                   1613:                        return;
                   1614:        }
                   1615:        *list = xreallocarray(*list, (*size) + 1, sizeof **list);
                   1616:        (*list)[(*size)++] = xstrdup(s);
                   1617: }
                   1618:
1.128     nicm     1619: /* Build completion list. */
1.204     nicm     1620: static char **
                   1621: status_prompt_complete_list(u_int *size, const char *s, int at_start)
1.128     nicm     1622: {
1.234     nicm     1623:        char                                    **list = NULL, *tmp;
1.183     nicm     1624:        const char                              **layout, *value, *cp;
1.128     nicm     1625:        const struct cmd_entry                  **cmdent;
                   1626:        const struct options_table_entry         *oe;
1.183     nicm     1627:        size_t                                    slen = strlen(s), valuelen;
                   1628:        struct options_entry                     *o;
1.191     nicm     1629:        struct options_array_item                *a;
1.128     nicm     1630:        const char                               *layouts[] = {
                   1631:                "even-horizontal", "even-vertical", "main-horizontal",
                   1632:                "main-vertical", "tiled", NULL
                   1633:        };
1.1       nicm     1634:
1.128     nicm     1635:        *size = 0;
1.1       nicm     1636:        for (cmdent = cmd_table; *cmdent != NULL; cmdent++) {
1.234     nicm     1637:                if (strncmp((*cmdent)->name, s, slen) == 0)
                   1638:                        status_prompt_add_list(&list, size, (*cmdent)->name);
1.205     nicm     1639:                if ((*cmdent)->alias != NULL &&
1.234     nicm     1640:                    strncmp((*cmdent)->alias, s, slen) == 0)
                   1641:                        status_prompt_add_list(&list, size, (*cmdent)->alias);
1.56      nicm     1642:        }
1.183     nicm     1643:        o = options_get_only(global_options, "command-alias");
1.191     nicm     1644:        if (o != NULL) {
                   1645:                a = options_array_first(o);
                   1646:                while (a != NULL) {
1.195     nicm     1647:                        value = options_array_item_value(a)->string;
1.194     nicm     1648:                        if ((cp = strchr(value, '=')) == NULL)
1.196     nicm     1649:                                goto next;
1.183     nicm     1650:                        valuelen = cp - value;
                   1651:                        if (slen > valuelen || strncmp(value, s, slen) != 0)
1.191     nicm     1652:                                goto next;
                   1653:
1.234     nicm     1654:                        xasprintf(&tmp, "%.*s", (int)valuelen, value);
                   1655:                        status_prompt_add_list(&list, size, tmp);
                   1656:                        free(tmp);
1.191     nicm     1657:
                   1658:                next:
                   1659:                        a = options_array_next(a);
1.183     nicm     1660:                }
                   1661:        }
1.204     nicm     1662:        if (at_start)
                   1663:                return (list);
                   1664:        for (oe = options_table; oe->name != NULL; oe++) {
1.234     nicm     1665:                if (strncmp(oe->name, s, slen) == 0)
                   1666:                        status_prompt_add_list(&list, size, oe->name);
1.204     nicm     1667:        }
                   1668:        for (layout = layouts; *layout != NULL; layout++) {
1.234     nicm     1669:                if (strncmp(*layout, s, slen) == 0)
                   1670:                        status_prompt_add_list(&list, size, *layout);
1.204     nicm     1671:        }
1.128     nicm     1672:        return (list);
                   1673: }
                   1674:
                   1675: /* Find longest prefix. */
1.151     nicm     1676: static char *
1.183     nicm     1677: status_prompt_complete_prefix(char **list, u_int size)
1.128     nicm     1678: {
                   1679:        char     *out;
                   1680:        u_int     i;
                   1681:        size_t    j;
                   1682:
1.217     nicm     1683:        if (list == NULL || size == 0)
                   1684:                return (NULL);
1.128     nicm     1685:        out = xstrdup(list[0]);
                   1686:        for (i = 1; i < size; i++) {
                   1687:                j = strlen(list[i]);
                   1688:                if (j > strlen(out))
                   1689:                        j = strlen(out);
                   1690:                for (; j > 0; j--) {
                   1691:                        if (out[j - 1] != list[i][j - 1])
                   1692:                                out[j - 1] = '\0';
                   1693:                }
1.1       nicm     1694:        }
1.128     nicm     1695:        return (out);
                   1696: }
1.1       nicm     1697:
1.204     nicm     1698: /* Complete word menu callback. */
                   1699: static void
                   1700: status_prompt_menu_callback(__unused struct menu *menu, u_int idx, key_code key,
                   1701:     void *data)
                   1702: {
                   1703:        struct status_prompt_menu       *spm = data;
                   1704:        struct client                   *c = spm->c;
                   1705:        u_int                            i;
                   1706:        char                            *s;
                   1707:
                   1708:        if (key != KEYC_NONE) {
                   1709:                idx += spm->start;
                   1710:                if (spm->flag == '\0')
                   1711:                        s = xstrdup(spm->list[idx]);
                   1712:                else
                   1713:                        xasprintf(&s, "-%c%s", spm->flag, spm->list[idx]);
1.224     nicm     1714:                if (c->prompt_type == PROMPT_TYPE_WINDOW_TARGET) {
1.205     nicm     1715:                        free(c->prompt_buffer);
                   1716:                        c->prompt_buffer = utf8_fromcstr(s);
                   1717:                        c->prompt_index = utf8_strlen(c->prompt_buffer);
                   1718:                        c->flags |= CLIENT_REDRAWSTATUS;
                   1719:                } else if (status_prompt_replace_complete(c, s))
1.204     nicm     1720:                        c->flags |= CLIENT_REDRAWSTATUS;
                   1721:                free(s);
                   1722:        }
                   1723:
                   1724:        for (i = 0; i < spm->size; i++)
                   1725:                free(spm->list[i]);
                   1726:        free(spm->list);
                   1727: }
                   1728:
                   1729: /* Show complete word menu. */
                   1730: static int
                   1731: status_prompt_complete_list_menu(struct client *c, char **list, u_int size,
                   1732:     u_int offset, char flag)
                   1733: {
                   1734:        struct menu                     *menu;
                   1735:        struct menu_item                 item;
                   1736:        struct status_prompt_menu       *spm;
                   1737:        u_int                            lines = status_line_size(c), height, i;
                   1738:        u_int                            py;
                   1739:
                   1740:        if (size <= 1)
                   1741:                return (0);
                   1742:        if (c->tty.sy - lines < 3)
                   1743:                return (0);
                   1744:
                   1745:        spm = xmalloc(sizeof *spm);
                   1746:        spm->c = c;
                   1747:        spm->size = size;
                   1748:        spm->list = list;
                   1749:        spm->flag = flag;
                   1750:
                   1751:        height = c->tty.sy - lines - 2;
                   1752:        if (height > 10)
                   1753:                height = 10;
                   1754:        if (height > size)
                   1755:                height = size;
                   1756:        spm->start = size - height;
                   1757:
                   1758:        menu = menu_create("");
                   1759:        for (i = spm->start; i < size; i++) {
                   1760:                item.name = list[i];
                   1761:                item.key = '0' + (i - spm->start);
                   1762:                item.command = NULL;
1.230     nicm     1763:                menu_add_item(menu, &item, NULL, c, NULL);
1.204     nicm     1764:        }
                   1765:
                   1766:        if (options_get_number(c->session->options, "status-position") == 0)
                   1767:                py = lines;
                   1768:        else
                   1769:                py = c->tty.sy - 3 - height;
                   1770:        offset += utf8_cstrwidth(c->prompt_string);
                   1771:        if (offset > 2)
                   1772:                offset -= 2;
                   1773:        else
                   1774:                offset = 0;
                   1775:
1.239     nicm     1776:        if (menu_display(menu, MENU_NOMOUSE|MENU_TAB, 0, NULL, offset, py, c,
1.240     nicm     1777:            BOX_LINES_DEFAULT, NULL, NULL, NULL, NULL,
                   1778:            status_prompt_menu_callback, spm) != 0) {
1.204     nicm     1779:                menu_free(menu);
                   1780:                free(spm);
                   1781:                return (0);
                   1782:        }
                   1783:        return (1);
                   1784: }
                   1785:
                   1786: /* Show complete word menu. */
                   1787: static char *
                   1788: status_prompt_complete_window_menu(struct client *c, struct session *s,
1.206     nicm     1789:     const char *word, u_int offset, char flag)
1.204     nicm     1790: {
                   1791:        struct menu                      *menu;
                   1792:        struct menu_item                  item;
                   1793:        struct status_prompt_menu        *spm;
                   1794:        struct winlink                   *wl;
                   1795:        char                            **list = NULL, *tmp;
                   1796:        u_int                             lines = status_line_size(c), height;
                   1797:        u_int                             py, size = 0;
                   1798:
                   1799:        if (c->tty.sy - lines < 3)
                   1800:                return (NULL);
                   1801:
                   1802:        spm = xmalloc(sizeof *spm);
                   1803:        spm->c = c;
                   1804:        spm->flag = flag;
                   1805:
                   1806:        height = c->tty.sy - lines - 2;
                   1807:        if (height > 10)
                   1808:                height = 10;
                   1809:        spm->start = 0;
                   1810:
                   1811:        menu = menu_create("");
                   1812:        RB_FOREACH(wl, winlinks, &s->windows) {
1.206     nicm     1813:                if (word != NULL && *word != '\0') {
                   1814:                        xasprintf(&tmp, "%d", wl->idx);
                   1815:                        if (strncmp(tmp, word, strlen(word)) != 0) {
                   1816:                                free(tmp);
                   1817:                                continue;
                   1818:                        }
                   1819:                        free(tmp);
                   1820:                }
                   1821:
1.204     nicm     1822:                list = xreallocarray(list, size + 1, sizeof *list);
1.224     nicm     1823:                if (c->prompt_type == PROMPT_TYPE_WINDOW_TARGET) {
1.205     nicm     1824:                        xasprintf(&tmp, "%d (%s)", wl->idx, wl->window->name);
                   1825:                        xasprintf(&list[size++], "%d", wl->idx);
                   1826:                } else {
                   1827:                        xasprintf(&tmp, "%s:%d (%s)", s->name, wl->idx,
                   1828:                            wl->window->name);
                   1829:                        xasprintf(&list[size++], "%s:%d", s->name, wl->idx);
                   1830:                }
1.204     nicm     1831:                item.name = tmp;
                   1832:                item.key = '0' + size - 1;
                   1833:                item.command = NULL;
1.233     nicm     1834:                menu_add_item(menu, &item, NULL, c, NULL);
1.204     nicm     1835:                free(tmp);
                   1836:
                   1837:                if (size == height)
                   1838:                        break;
                   1839:        }
1.206     nicm     1840:        if (size == 0) {
                   1841:                menu_free(menu);
                   1842:                return (NULL);
                   1843:        }
1.204     nicm     1844:        if (size == 1) {
                   1845:                menu_free(menu);
1.205     nicm     1846:                if (flag != '\0') {
                   1847:                        xasprintf(&tmp, "-%c%s", flag, list[0]);
                   1848:                        free(list[0]);
                   1849:                } else
                   1850:                        tmp = list[0];
1.204     nicm     1851:                free(list);
                   1852:                return (tmp);
                   1853:        }
                   1854:        if (height > size)
                   1855:                height = size;
                   1856:
                   1857:        spm->size = size;
                   1858:        spm->list = list;
                   1859:
                   1860:        if (options_get_number(c->session->options, "status-position") == 0)
                   1861:                py = lines;
                   1862:        else
                   1863:                py = c->tty.sy - 3 - height;
                   1864:        offset += utf8_cstrwidth(c->prompt_string);
                   1865:        if (offset > 2)
                   1866:                offset -= 2;
                   1867:        else
                   1868:                offset = 0;
                   1869:
1.239     nicm     1870:        if (menu_display(menu, MENU_NOMOUSE|MENU_TAB, 0, NULL, offset, py, c,
1.240     nicm     1871:            BOX_LINES_DEFAULT, NULL, NULL, NULL, NULL,
                   1872:            status_prompt_menu_callback, spm) != 0) {
1.204     nicm     1873:                menu_free(menu);
                   1874:                free(spm);
                   1875:                return (NULL);
                   1876:        }
                   1877:        return (NULL);
                   1878: }
                   1879:
                   1880: /* Sort complete list. */
                   1881: static int
                   1882: status_prompt_complete_sort(const void *a, const void *b)
                   1883: {
                   1884:        const char      **aa = (const char **)a, **bb = (const char **)b;
                   1885:
                   1886:        return (strcmp(*aa, *bb));
                   1887: }
                   1888:
1.205     nicm     1889: /* Complete a session. */
                   1890: static char *
                   1891: status_prompt_complete_session(char ***list, u_int *size, const char *s,
                   1892:     char flag)
                   1893: {
                   1894:        struct session  *loop;
1.217     nicm     1895:        char            *out, *tmp, n[11];
1.205     nicm     1896:
                   1897:        RB_FOREACH(loop, sessions, &sessions) {
1.217     nicm     1898:                if (*s == '\0' || strncmp(loop->name, s, strlen(s)) == 0) {
                   1899:                        *list = xreallocarray(*list, (*size) + 2,
                   1900:                            sizeof **list);
                   1901:                        xasprintf(&(*list)[(*size)++], "%s:", loop->name);
                   1902:                } else if (*s == '$') {
                   1903:                        xsnprintf(n, sizeof n, "%u", loop->id);
                   1904:                        if (s[1] == '\0' ||
                   1905:                            strncmp(n, s + 1, strlen(s) - 1) == 0) {
                   1906:                                *list = xreallocarray(*list, (*size) + 2,
                   1907:                                    sizeof **list);
                   1908:                                xasprintf(&(*list)[(*size)++], "$%s:", n);
                   1909:                        }
                   1910:                }
1.205     nicm     1911:        }
                   1912:        out = status_prompt_complete_prefix(*list, *size);
                   1913:        if (out != NULL && flag != '\0') {
                   1914:                xasprintf(&tmp, "-%c%s", flag, out);
                   1915:                free(out);
                   1916:                out = tmp;
                   1917:        }
                   1918:        return (out);
                   1919: }
                   1920:
1.128     nicm     1921: /* Complete word. */
1.151     nicm     1922: static char *
1.204     nicm     1923: status_prompt_complete(struct client *c, const char *word, u_int offset)
1.128     nicm     1924: {
1.217     nicm     1925:        struct session   *session;
1.204     nicm     1926:        const char       *s, *colon;
1.205     nicm     1927:        char            **list = NULL, *copy = NULL, *out = NULL;
1.204     nicm     1928:        char              flag = '\0';
1.128     nicm     1929:        u_int             size = 0, i;
                   1930:
1.206     nicm     1931:        if (*word == '\0' &&
1.224     nicm     1932:            c->prompt_type != PROMPT_TYPE_TARGET &&
                   1933:            c->prompt_type != PROMPT_TYPE_WINDOW_TARGET)
1.1       nicm     1934:                return (NULL);
1.128     nicm     1935:
1.224     nicm     1936:        if (c->prompt_type != PROMPT_TYPE_TARGET &&
                   1937:            c->prompt_type != PROMPT_TYPE_WINDOW_TARGET &&
1.205     nicm     1938:            strncmp(word, "-t", 2) != 0 &&
                   1939:            strncmp(word, "-s", 2) != 0) {
1.204     nicm     1940:                list = status_prompt_complete_list(&size, word, offset == 0);
1.128     nicm     1941:                if (size == 0)
                   1942:                        out = NULL;
                   1943:                else if (size == 1)
                   1944:                        xasprintf(&out, "%s ", list[0]);
                   1945:                else
                   1946:                        out = status_prompt_complete_prefix(list, size);
1.204     nicm     1947:                goto found;
1.128     nicm     1948:        }
                   1949:
1.224     nicm     1950:        if (c->prompt_type == PROMPT_TYPE_TARGET ||
                   1951:            c->prompt_type == PROMPT_TYPE_WINDOW_TARGET) {
1.205     nicm     1952:                s = word;
                   1953:                flag = '\0';
                   1954:        } else {
                   1955:                s = word + 2;
                   1956:                flag = word[1];
                   1957:                offset += 2;
                   1958:        }
1.206     nicm     1959:
                   1960:        /* If this is a window completion, open the window menu. */
1.224     nicm     1961:        if (c->prompt_type == PROMPT_TYPE_WINDOW_TARGET) {
1.206     nicm     1962:                out = status_prompt_complete_window_menu(c, c->session, s,
                   1963:                    offset, '\0');
                   1964:                goto found;
                   1965:        }
1.204     nicm     1966:        colon = strchr(s, ':');
1.1       nicm     1967:
1.204     nicm     1968:        /* If there is no colon, complete as a session. */
                   1969:        if (colon == NULL) {
1.205     nicm     1970:                out = status_prompt_complete_session(&list, &size, s, flag);
1.128     nicm     1971:                goto found;
                   1972:        }
                   1973:
1.204     nicm     1974:        /* If there is a colon but no period, find session and show a menu. */
                   1975:        if (strchr(colon + 1, '.') == NULL) {
                   1976:                if (*s == ':')
                   1977:                        session = c->session;
                   1978:                else {
                   1979:                        copy = xstrdup(s);
                   1980:                        *strchr(copy, ':') = '\0';
                   1981:                        session = session_find(copy);
                   1982:                        free(copy);
                   1983:                        if (session == NULL)
                   1984:                                goto found;
                   1985:                }
1.206     nicm     1986:                out = status_prompt_complete_window_menu(c, session, colon + 1,
                   1987:                    offset, flag);
1.204     nicm     1988:                if (out == NULL)
                   1989:                        return (NULL);
                   1990:        }
1.1       nicm     1991:
1.204     nicm     1992: found:
                   1993:        if (size != 0) {
                   1994:                qsort(list, size, sizeof *list, status_prompt_complete_sort);
                   1995:                for (i = 0; i < size; i++)
                   1996:                        log_debug("complete %u: %s", i, list[i]);
                   1997:        }
1.128     nicm     1998:
1.204     nicm     1999:        if (out != NULL && strcmp(word, out) == 0) {
                   2000:                free(out);
                   2001:                out = NULL;
1.1       nicm     2002:        }
1.204     nicm     2003:        if (out != NULL ||
                   2004:            !status_prompt_complete_list_menu(c, list, size, offset, flag)) {
                   2005:                for (i = 0; i < size; i++)
                   2006:                        free(list[i]);
                   2007:                free(list);
1.128     nicm     2008:        }
                   2009:        return (out);
1.224     nicm     2010: }
                   2011:
                   2012: /* Return the type of the prompt as an enum. */
                   2013: enum prompt_type
                   2014: status_prompt_type(const char *type)
                   2015: {
                   2016:        u_int   i;
                   2017:
                   2018:        for (i = 0; i < PROMPT_NTYPES; i++) {
                   2019:                if (strcmp(type, status_prompt_type_string(i)) == 0)
                   2020:                        return (i);
                   2021:        }
                   2022:        return (PROMPT_TYPE_INVALID);
                   2023: }
                   2024:
                   2025: /* Accessor for prompt_type_strings. */
                   2026: const char *
                   2027: status_prompt_type_string(u_int type)
                   2028: {
                   2029:        if (type >= PROMPT_NTYPES)
                   2030:                return ("invalid");
                   2031:        return (prompt_type_strings[type]);
1.1       nicm     2032: }