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

1.240   ! nicm        1: /* $OpenBSD: status.c,v 1.239 2023/08/08 08:08: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.162     nicm      475:        struct timeval  tv;
                    476:        va_list         ap;
1.1       nicm      477:
1.12      nicm      478:        status_message_clear(c);
1.189     nicm      479:        status_push_screen(c);
1.167     nicm      480:
1.28      nicm      481:        va_start(ap, fmt);
                    482:        xvasprintf(&c->message_string, fmt, ap);
                    483:        va_end(ap);
                    484:
1.209     nicm      485:        server_add_message("%s message: %s", c->name, c->message_string);
1.44      nicm      486:
1.218     nicm      487:        /*
                    488:         * With delay -1, the display-time option is used; zero means wait for
                    489:         * key press; more than zero is the actual delay time in milliseconds.
                    490:         */
                    491:        if (delay == -1)
                    492:                delay = options_get_number(c->session->options, "display-time");
1.143     tim       493:        if (delay > 0) {
                    494:                tv.tv_sec = delay / 1000;
                    495:                tv.tv_usec = (delay % 1000) * 1000L;
                    496:
                    497:                if (event_initialized(&c->message_timer))
                    498:                        evtimer_del(&c->message_timer);
                    499:                evtimer_set(&c->message_timer, status_message_callback, c);
1.218     nicm      500:
1.143     tim       501:                evtimer_add(&c->message_timer, &tv);
                    502:        }
1.221     nicm      503:
                    504:        if (delay != 0)
                    505:                c->message_ignore_keys = ignore_keys;
                    506:        c->message_ignore_styles = ignore_styles;
1.1       nicm      507:
                    508:        c->tty.flags |= (TTY_NOCURSOR|TTY_FREEZE);
1.177     nicm      509:        c->flags |= CLIENT_REDRAWSTATUS;
1.1       nicm      510: }
                    511:
1.46      nicm      512: /* Clear status line message. */
1.1       nicm      513: void
                    514: status_message_clear(struct client *c)
                    515: {
                    516:        if (c->message_string == NULL)
                    517:                return;
                    518:
1.94      nicm      519:        free(c->message_string);
1.1       nicm      520:        c->message_string = NULL;
                    521:
1.156     nicm      522:        if (c->prompt_string == NULL)
                    523:                c->tty.flags &= ~(TTY_NOCURSOR|TTY_FREEZE);
1.177     nicm      524:        c->flags |= CLIENT_ALLREDRAWFLAGS; /* was frozen and may have changed */
1.7       nicm      525:
1.189     nicm      526:        status_pop_screen(c);
1.42      nicm      527: }
                    528:
1.46      nicm      529: /* Clear status line message after timer expires. */
1.151     nicm      530: static void
1.141     nicm      531: status_message_callback(__unused int fd, __unused short event, void *data)
1.42      nicm      532: {
                    533:        struct client   *c = data;
                    534:
                    535:        status_message_clear(c);
1.1       nicm      536: }
                    537:
                    538: /* Draw client message on status line of present else on last line. */
                    539: int
                    540: status_message_redraw(struct client *c)
                    541: {
1.189     nicm      542:        struct status_line      *sl = &c->status;
                    543:        struct screen_write_ctx  ctx;
                    544:        struct session          *s = c->session;
                    545:        struct screen            old_screen;
                    546:        size_t                   len;
1.235     nicm      547:        u_int                    lines, offset, messageline;
1.189     nicm      548:        struct grid_cell         gc;
1.207     nicm      549:        struct format_tree      *ft;
1.1       nicm      550:
                    551:        if (c->tty.sx == 0 || c->tty.sy == 0)
                    552:                return (0);
1.189     nicm      553:        memcpy(&old_screen, sl->active, sizeof old_screen);
1.169     nicm      554:
1.182     nicm      555:        lines = status_line_size(c);
1.189     nicm      556:        if (lines <= 1)
1.173     nicm      557:                lines = 1;
1.190     nicm      558:        screen_init(sl->active, c->tty.sx, lines, 0);
1.1       nicm      559:
1.235     nicm      560:        messageline = status_prompt_line_at(c);
                    561:        if (messageline > lines - 1)
                    562:                messageline = lines - 1;
                    563:
1.139     nicm      564:        len = screen_write_strlen("%s", c->message_string);
1.1       nicm      565:        if (len > c->tty.sx)
                    566:                len = c->tty.sx;
                    567:
1.207     nicm      568:        ft = format_create_defaults(NULL, c, NULL, NULL, NULL);
                    569:        style_apply(&gc, s->options, "message-style", ft);
                    570:        format_free(ft);
1.1       nicm      571:
1.208     nicm      572:        screen_write_start(&ctx, sl->active);
1.235     nicm      573:        screen_write_fast_copy(&ctx, &sl->screen, 0, 0, c->tty.sx, lines);
                    574:        screen_write_cursormove(&ctx, 0, messageline, 0);
1.192     nicm      575:        for (offset = 0; offset < c->tty.sx; offset++)
1.170     nicm      576:                screen_write_putc(&ctx, &gc, ' ');
1.235     nicm      577:        screen_write_cursormove(&ctx, 0, messageline, 0);
1.210     nicm      578:        if (c->message_ignore_styles)
                    579:                screen_write_nputs(&ctx, len, &gc, "%s", c->message_string);
                    580:        else
1.228     nicm      581:                format_draw(&ctx, &gc, c->tty.sx, c->message_string, NULL, 0);
1.1       nicm      582:        screen_write_stop(&ctx);
                    583:
1.189     nicm      584:        if (grid_compare(sl->active->grid, old_screen.grid) == 0) {
                    585:                screen_free(&old_screen);
1.1       nicm      586:                return (0);
                    587:        }
1.189     nicm      588:        screen_free(&old_screen);
1.1       nicm      589:        return (1);
                    590: }
                    591:
1.46      nicm      592: /* Enable status line prompt. */
1.1       nicm      593: void
1.211     nicm      594: status_prompt_set(struct client *c, struct cmd_find_state *fs,
                    595:     const char *msg, const char *input, prompt_input_cb inputcb,
1.224     nicm      596:     prompt_free_cb freecb, void *data, int flags, enum prompt_type prompt_type)
1.1       nicm      597: {
1.122     nicm      598:        struct format_tree      *ft;
1.219     nicm      599:        char                    *tmp;
1.122     nicm      600:
1.211     nicm      601:        if (fs != NULL)
                    602:                ft = format_create_from_state(NULL, c, fs);
                    603:        else
                    604:                ft = format_create_defaults(NULL, c, NULL, NULL, NULL);
1.152     nicm      605:
1.168     nicm      606:        if (input == NULL)
                    607:                input = "";
                    608:        if (flags & PROMPT_NOFORMAT)
                    609:                tmp = xstrdup(input);
                    610:        else
1.185     nicm      611:                tmp = format_expand_time(ft, input);
1.20      nicm      612:
1.12      nicm      613:        status_message_clear(c);
                    614:        status_prompt_clear(c);
1.189     nicm      615:        status_push_screen(c);
1.12      nicm      616:
1.185     nicm      617:        c->prompt_string = format_expand_time(ft, msg);
1.1       nicm      618:
1.219     nicm      619:        if (flags & PROMPT_INCREMENTAL) {
                    620:                c->prompt_last = xstrdup(tmp);
                    621:                c->prompt_buffer = utf8_fromcstr("");
                    622:        } else {
                    623:                c->prompt_last = NULL;
                    624:                c->prompt_buffer = utf8_fromcstr(tmp);
                    625:        }
1.152     nicm      626:        c->prompt_index = utf8_strlen(c->prompt_buffer);
1.1       nicm      627:
1.166     nicm      628:        c->prompt_inputcb = inputcb;
                    629:        c->prompt_freecb = freecb;
1.1       nicm      630:        c->prompt_data = data;
                    631:
1.224     nicm      632:        memset(c->prompt_hindex, 0, sizeof c->prompt_hindex);
1.1       nicm      633:
                    634:        c->prompt_flags = flags;
1.224     nicm      635:        c->prompt_type = prompt_type;
1.155     nicm      636:        c->prompt_mode = PROMPT_ENTRY;
1.1       nicm      637:
1.158     nicm      638:        if (~flags & PROMPT_INCREMENTAL)
                    639:                c->tty.flags |= (TTY_NOCURSOR|TTY_FREEZE);
1.177     nicm      640:        c->flags |= CLIENT_REDRAWSTATUS;
1.165     nicm      641:
1.219     nicm      642:        if (flags & PROMPT_INCREMENTAL)
                    643:                c->prompt_inputcb(c, c->prompt_data, "=", 0);
1.122     nicm      644:
1.152     nicm      645:        free(tmp);
1.122     nicm      646:        format_free(ft);
1.1       nicm      647: }
                    648:
1.46      nicm      649: /* Remove status line prompt. */
1.1       nicm      650: void
                    651: status_prompt_clear(struct client *c)
                    652: {
1.55      nicm      653:        if (c->prompt_string == NULL)
1.1       nicm      654:                return;
                    655:
1.166     nicm      656:        if (c->prompt_freecb != NULL && c->prompt_data != NULL)
                    657:                c->prompt_freecb(c->prompt_data);
1.1       nicm      658:
1.219     nicm      659:        free(c->prompt_last);
                    660:        c->prompt_last = NULL;
                    661:
1.94      nicm      662:        free(c->prompt_string);
1.1       nicm      663:        c->prompt_string = NULL;
                    664:
1.94      nicm      665:        free(c->prompt_buffer);
1.1       nicm      666:        c->prompt_buffer = NULL;
                    667:
1.181     nicm      668:        free(c->prompt_saved);
                    669:        c->prompt_saved = NULL;
                    670:
1.1       nicm      671:        c->tty.flags &= ~(TTY_NOCURSOR|TTY_FREEZE);
1.177     nicm      672:        c->flags |= CLIENT_ALLREDRAWFLAGS; /* was frozen and may have changed */
1.7       nicm      673:
1.189     nicm      674:        status_pop_screen(c);
1.27      nicm      675: }
                    676:
1.46      nicm      677: /* Update status line prompt with a new prompt string. */
1.27      nicm      678: void
1.76      nicm      679: status_prompt_update(struct client *c, const char *msg, const char *input)
1.27      nicm      680: {
1.122     nicm      681:        struct format_tree      *ft;
1.152     nicm      682:        char                    *tmp;
1.122     nicm      683:
1.164     nicm      684:        ft = format_create(c, NULL, FORMAT_NONE, 0);
1.122     nicm      685:        format_defaults(ft, c, NULL, NULL, NULL);
1.152     nicm      686:
1.185     nicm      687:        tmp = format_expand_time(ft, input);
1.122     nicm      688:
1.94      nicm      689:        free(c->prompt_string);
1.185     nicm      690:        c->prompt_string = format_expand_time(ft, msg);
1.27      nicm      691:
1.94      nicm      692:        free(c->prompt_buffer);
1.152     nicm      693:        c->prompt_buffer = utf8_fromcstr(tmp);
                    694:        c->prompt_index = utf8_strlen(c->prompt_buffer);
1.27      nicm      695:
1.224     nicm      696:        memset(c->prompt_hindex, 0, sizeof c->prompt_hindex);
1.27      nicm      697:
1.177     nicm      698:        c->flags |= CLIENT_REDRAWSTATUS;
1.122     nicm      699:
1.152     nicm      700:        free(tmp);
1.122     nicm      701:        format_free(ft);
1.1       nicm      702: }
                    703:
                    704: /* Draw client prompt on status line of present else on last line. */
                    705: int
                    706: status_prompt_redraw(struct client *c)
                    707: {
1.189     nicm      708:        struct status_line      *sl = &c->status;
1.152     nicm      709:        struct screen_write_ctx  ctx;
                    710:        struct session          *s = c->session;
1.189     nicm      711:        struct screen            old_screen;
                    712:        u_int                    i, lines, offset, left, start, width;
1.235     nicm      713:        u_int                    pcursor, pwidth, promptline;
1.152     nicm      714:        struct grid_cell         gc, cursorgc;
1.207     nicm      715:        struct format_tree      *ft;
1.1       nicm      716:
                    717:        if (c->tty.sx == 0 || c->tty.sy == 0)
                    718:                return (0);
1.189     nicm      719:        memcpy(&old_screen, sl->active, sizeof old_screen);
1.169     nicm      720:
1.182     nicm      721:        lines = status_line_size(c);
1.189     nicm      722:        if (lines <= 1)
1.173     nicm      723:                lines = 1;
1.189     nicm      724:        screen_init(sl->active, c->tty.sx, lines, 0);
1.1       nicm      725:
1.235     nicm      726:        promptline = status_prompt_line_at(c);
                    727:        if (promptline > lines - 1)
                    728:                promptline = lines - 1;
                    729:
1.207     nicm      730:        ft = format_create_defaults(NULL, c, NULL, NULL, NULL);
1.155     nicm      731:        if (c->prompt_mode == PROMPT_COMMAND)
1.207     nicm      732:                style_apply(&gc, s->options, "message-command-style", ft);
1.108     nicm      733:        else
1.207     nicm      734:                style_apply(&gc, s->options, "message-style", ft);
                    735:        format_free(ft);
1.1       nicm      736:
1.152     nicm      737:        memcpy(&cursorgc, &gc, sizeof cursorgc);
                    738:        cursorgc.attr ^= GRID_ATTR_REVERSE;
                    739:
1.232     nicm      740:        start = format_width(c->prompt_string);
1.152     nicm      741:        if (start > c->tty.sx)
                    742:                start = c->tty.sx;
                    743:
1.208     nicm      744:        screen_write_start(&ctx, sl->active);
1.235     nicm      745:        screen_write_fast_copy(&ctx, &sl->screen, 0, 0, c->tty.sx, lines);
                    746:        screen_write_cursormove(&ctx, 0, promptline, 0);
1.192     nicm      747:        for (offset = 0; offset < c->tty.sx; offset++)
1.170     nicm      748:                screen_write_putc(&ctx, &gc, ' ');
1.235     nicm      749:        screen_write_cursormove(&ctx, 0, promptline, 0);
1.232     nicm      750:        format_draw(&ctx, &gc, start, c->prompt_string, NULL, 0);
1.235     nicm      751:        screen_write_cursormove(&ctx, start, promptline, 0);
1.1       nicm      752:
1.152     nicm      753:        left = c->tty.sx - start;
                    754:        if (left == 0)
                    755:                goto finished;
                    756:
                    757:        pcursor = utf8_strwidth(c->prompt_buffer, c->prompt_index);
                    758:        pwidth = utf8_strwidth(c->prompt_buffer, -1);
                    759:        if (pcursor >= left) {
                    760:                /*
                    761:                 * The cursor would be outside the screen so start drawing
                    762:                 * with it on the right.
                    763:                 */
                    764:                offset = (pcursor - left) + 1;
                    765:                pwidth = left;
                    766:        } else
                    767:                offset = 0;
                    768:        if (pwidth > left)
                    769:                pwidth = left;
1.231     nicm      770:        c->prompt_cursor = start + c->prompt_index - offset;
1.152     nicm      771:
                    772:        width = 0;
                    773:        for (i = 0; c->prompt_buffer[i].size != 0; i++) {
                    774:                if (width < offset) {
                    775:                        width += c->prompt_buffer[i].width;
                    776:                        continue;
1.1       nicm      777:                }
1.152     nicm      778:                if (width >= offset + pwidth)
                    779:                        break;
                    780:                width += c->prompt_buffer[i].width;
                    781:                if (width > offset + pwidth)
                    782:                        break;
1.1       nicm      783:
1.152     nicm      784:                if (i != c->prompt_index) {
                    785:                        utf8_copy(&gc.data, &c->prompt_buffer[i]);
                    786:                        screen_write_cell(&ctx, &gc);
                    787:                } else {
                    788:                        utf8_copy(&cursorgc.data, &c->prompt_buffer[i]);
                    789:                        screen_write_cell(&ctx, &cursorgc);
                    790:                }
1.1       nicm      791:        }
1.189     nicm      792:        if (sl->active->cx < screen_size_x(sl->active) && c->prompt_index >= i)
1.152     nicm      793:                screen_write_putc(&ctx, &cursorgc, ' ');
1.1       nicm      794:
1.152     nicm      795: finished:
1.1       nicm      796:        screen_write_stop(&ctx);
1.51      nicm      797:
1.189     nicm      798:        if (grid_compare(sl->active->grid, old_screen.grid) == 0) {
                    799:                screen_free(&old_screen);
1.1       nicm      800:                return (0);
                    801:        }
1.189     nicm      802:        screen_free(&old_screen);
1.1       nicm      803:        return (1);
                    804: }
                    805:
1.152     nicm      806: /* Is this a separator? */
                    807: static int
                    808: status_prompt_in_list(const char *ws, const struct utf8_data *ud)
                    809: {
                    810:        if (ud->size != 1 || ud->width != 1)
                    811:                return (0);
                    812:        return (strchr(ws, *ud->data) != NULL);
                    813: }
                    814:
                    815: /* Is this a space? */
                    816: static int
                    817: status_prompt_space(const struct utf8_data *ud)
                    818: {
                    819:        if (ud->size != 1 || ud->width != 1)
                    820:                return (0);
                    821:        return (*ud->data == ' ');
                    822: }
                    823:
1.155     nicm      824: /*
1.224     nicm      825:  * Translate key from vi to emacs. Return 0 to drop key, 1 to process the key
1.155     nicm      826:  * as an emacs key; return 2 to append to the buffer.
                    827:  */
                    828: static int
                    829: status_prompt_translate_key(struct client *c, key_code key, key_code *new_key)
                    830: {
                    831:        if (c->prompt_mode == PROMPT_ENTRY) {
                    832:                switch (key) {
1.229     nicm      833:                case '\001': /* C-a */
1.155     nicm      834:                case '\003': /* C-c */
1.229     nicm      835:                case '\005': /* C-e */
1.202     nicm      836:                case '\007': /* C-g */
1.155     nicm      837:                case '\010': /* C-h */
                    838:                case '\011': /* Tab */
1.229     nicm      839:                case '\013': /* C-k */
                    840:                case '\016': /* C-n */
                    841:                case '\020': /* C-p */
                    842:                case '\024': /* C-t */
1.155     nicm      843:                case '\025': /* C-u */
                    844:                case '\027': /* C-w */
1.229     nicm      845:                case '\031': /* C-y */
1.155     nicm      846:                case '\n':
                    847:                case '\r':
1.229     nicm      848:                case KEYC_LEFT|KEYC_CTRL:
                    849:                case KEYC_RIGHT|KEYC_CTRL:
1.155     nicm      850:                case KEYC_BSPACE:
                    851:                case KEYC_DC:
                    852:                case KEYC_DOWN:
                    853:                case KEYC_END:
                    854:                case KEYC_HOME:
                    855:                case KEYC_LEFT:
                    856:                case KEYC_RIGHT:
                    857:                case KEYC_UP:
                    858:                        *new_key = key;
                    859:                        return (1);
                    860:                case '\033': /* Escape */
                    861:                        c->prompt_mode = PROMPT_COMMAND;
1.177     nicm      862:                        c->flags |= CLIENT_REDRAWSTATUS;
1.155     nicm      863:                        return (0);
                    864:                }
                    865:                *new_key = key;
                    866:                return (2);
                    867:        }
                    868:
                    869:        switch (key) {
1.229     nicm      870:        case KEYC_BSPACE:
                    871:                *new_key = KEYC_LEFT;
                    872:                return (1);
1.155     nicm      873:        case 'A':
                    874:        case 'I':
                    875:        case 'C':
                    876:        case 's':
                    877:        case 'a':
                    878:                c->prompt_mode = PROMPT_ENTRY;
1.177     nicm      879:                c->flags |= CLIENT_REDRAWSTATUS;
1.155     nicm      880:                break; /* switch mode and... */
                    881:        case 'S':
                    882:                c->prompt_mode = PROMPT_ENTRY;
1.177     nicm      883:                c->flags |= CLIENT_REDRAWSTATUS;
1.155     nicm      884:                *new_key = '\025'; /* C-u */
                    885:                return (1);
                    886:        case 'i':
                    887:        case '\033': /* Escape */
                    888:                c->prompt_mode = PROMPT_ENTRY;
1.177     nicm      889:                c->flags |= CLIENT_REDRAWSTATUS;
1.155     nicm      890:                return (0);
                    891:        }
                    892:
                    893:        switch (key) {
                    894:        case 'A':
                    895:        case '$':
                    896:                *new_key = KEYC_END;
                    897:                return (1);
                    898:        case 'I':
                    899:        case '0':
                    900:        case '^':
                    901:                *new_key = KEYC_HOME;
                    902:                return (1);
                    903:        case 'C':
                    904:        case 'D':
                    905:                *new_key = '\013'; /* C-k */
                    906:                return (1);
                    907:        case KEYC_BSPACE:
                    908:        case 'X':
                    909:                *new_key = KEYC_BSPACE;
                    910:                return (1);
                    911:        case 'b':
1.225     nicm      912:                *new_key = 'b'|KEYC_META;
                    913:                return (1);
1.155     nicm      914:        case 'B':
1.225     nicm      915:                *new_key = 'B'|KEYC_VI;
1.155     nicm      916:                return (1);
                    917:        case 'd':
1.229     nicm      918:                *new_key = '\025'; /* C-u */
1.155     nicm      919:                return (1);
                    920:        case 'e':
1.225     nicm      921:                *new_key = 'e'|KEYC_VI;
                    922:                return (1);
1.155     nicm      923:        case 'E':
1.225     nicm      924:                *new_key = 'E'|KEYC_VI;
                    925:                return (1);
1.155     nicm      926:        case 'w':
1.225     nicm      927:                *new_key = 'w'|KEYC_VI;
                    928:                return (1);
1.155     nicm      929:        case 'W':
1.225     nicm      930:                *new_key = 'W'|KEYC_VI;
1.155     nicm      931:                return (1);
                    932:        case 'p':
                    933:                *new_key = '\031'; /* C-y */
1.202     nicm      934:                return (1);
                    935:        case 'q':
                    936:                *new_key = '\003'; /* C-c */
1.155     nicm      937:                return (1);
                    938:        case 's':
                    939:        case KEYC_DC:
                    940:        case 'x':
                    941:                *new_key = KEYC_DC;
                    942:                return (1);
                    943:        case KEYC_DOWN:
                    944:        case 'j':
                    945:                *new_key = KEYC_DOWN;
                    946:                return (1);
                    947:        case KEYC_LEFT:
                    948:        case 'h':
                    949:                *new_key = KEYC_LEFT;
                    950:                return (1);
                    951:        case 'a':
                    952:        case KEYC_RIGHT:
                    953:        case 'l':
                    954:                *new_key = KEYC_RIGHT;
                    955:                return (1);
                    956:        case KEYC_UP:
                    957:        case 'k':
                    958:                *new_key = KEYC_UP;
                    959:                return (1);
                    960:        case '\010' /* C-h */:
                    961:        case '\003' /* C-c */:
                    962:        case '\n':
                    963:        case '\r':
                    964:                return (1);
                    965:        }
                    966:        return (0);
                    967: }
                    968:
1.199     nicm      969: /* Paste into prompt. */
                    970: static int
                    971: status_prompt_paste(struct client *c)
                    972: {
                    973:        struct paste_buffer     *pb;
                    974:        const char              *bufdata;
                    975:        size_t                   size, n, bufsize;
                    976:        u_int                    i;
                    977:        struct utf8_data        *ud, *udp;
                    978:        enum utf8_state          more;
                    979:
                    980:        size = utf8_strlen(c->prompt_buffer);
                    981:        if (c->prompt_saved != NULL) {
                    982:                ud = c->prompt_saved;
                    983:                n = utf8_strlen(c->prompt_saved);
                    984:        } else {
                    985:                if ((pb = paste_get_top(NULL)) == NULL)
                    986:                        return (0);
                    987:                bufdata = paste_buffer_data(pb, &bufsize);
1.200     nicm      988:                ud = xreallocarray(NULL, bufsize + 1, sizeof *ud);
1.199     nicm      989:                udp = ud;
                    990:                for (i = 0; i != bufsize; /* nothing */) {
                    991:                        more = utf8_open(udp, bufdata[i]);
                    992:                        if (more == UTF8_MORE) {
                    993:                                while (++i != bufsize && more == UTF8_MORE)
                    994:                                        more = utf8_append(udp, bufdata[i]);
                    995:                                if (more == UTF8_DONE) {
                    996:                                        udp++;
                    997:                                        continue;
                    998:                                }
                    999:                                i -= udp->have;
                   1000:                        }
                   1001:                        if (bufdata[i] <= 31 || bufdata[i] >= 127)
                   1002:                                break;
                   1003:                        utf8_set(udp, bufdata[i]);
                   1004:                        udp++;
                   1005:                        i++;
                   1006:                }
                   1007:                udp->size = 0;
                   1008:                n = udp - ud;
                   1009:        }
                   1010:        if (n == 0)
                   1011:                return (0);
                   1012:
                   1013:        c->prompt_buffer = xreallocarray(c->prompt_buffer, size + n + 1,
                   1014:            sizeof *c->prompt_buffer);
                   1015:        if (c->prompt_index == size) {
                   1016:                memcpy(c->prompt_buffer + c->prompt_index, ud,
                   1017:                    n * sizeof *c->prompt_buffer);
                   1018:                c->prompt_index += n;
                   1019:                c->prompt_buffer[c->prompt_index].size = 0;
                   1020:        } else {
                   1021:                memmove(c->prompt_buffer + c->prompt_index + n,
                   1022:                    c->prompt_buffer + c->prompt_index,
                   1023:                    (size + 1 - c->prompt_index) * sizeof *c->prompt_buffer);
                   1024:                memcpy(c->prompt_buffer + c->prompt_index, ud,
                   1025:                    n * sizeof *c->prompt_buffer);
                   1026:                c->prompt_index += n;
                   1027:        }
                   1028:
                   1029:        if (ud != c->prompt_saved)
                   1030:                free(ud);
                   1031:        return (1);
                   1032: }
                   1033:
1.204     nicm     1034: /* Finish completion. */
                   1035: static int
                   1036: status_prompt_replace_complete(struct client *c, const char *s)
                   1037: {
                   1038:        char                     word[64], *allocated = NULL;
                   1039:        size_t                   size, n, off, idx, used;
                   1040:        struct utf8_data        *first, *last, *ud;
                   1041:
1.205     nicm     1042:        /* Work out where the cursor currently is. */
1.204     nicm     1043:        idx = c->prompt_index;
                   1044:        if (idx != 0)
                   1045:                idx--;
1.205     nicm     1046:        size = utf8_strlen(c->prompt_buffer);
1.204     nicm     1047:
                   1048:        /* Find the word we are in. */
                   1049:        first = &c->prompt_buffer[idx];
                   1050:        while (first > c->prompt_buffer && !status_prompt_space(first))
                   1051:                first--;
                   1052:        while (first->size != 0 && status_prompt_space(first))
                   1053:                first++;
                   1054:        last = &c->prompt_buffer[idx];
                   1055:        while (last->size != 0 && !status_prompt_space(last))
                   1056:                last++;
                   1057:        while (last > c->prompt_buffer && status_prompt_space(last))
                   1058:                last--;
                   1059:        if (last->size != 0)
                   1060:                last++;
1.205     nicm     1061:        if (last < first)
1.204     nicm     1062:                return (0);
                   1063:        if (s == NULL) {
                   1064:                used = 0;
                   1065:                for (ud = first; ud < last; ud++) {
                   1066:                        if (used + ud->size >= sizeof word)
                   1067:                                break;
                   1068:                        memcpy(word + used, ud->data, ud->size);
                   1069:                        used += ud->size;
                   1070:                }
                   1071:                if (ud != last)
                   1072:                        return (0);
                   1073:                word[used] = '\0';
                   1074:        }
                   1075:
                   1076:        /* Try to complete it. */
                   1077:        if (s == NULL) {
                   1078:                allocated = status_prompt_complete(c, word,
                   1079:                    first - c->prompt_buffer);
                   1080:                if (allocated == NULL)
                   1081:                        return (0);
                   1082:                s = allocated;
                   1083:        }
                   1084:
                   1085:        /* Trim out word. */
                   1086:        n = size - (last - c->prompt_buffer) + 1; /* with \0 */
                   1087:        memmove(first, last, n * sizeof *c->prompt_buffer);
                   1088:        size -= last - first;
                   1089:
                   1090:        /* Insert the new word. */
                   1091:        size += strlen(s);
                   1092:        off = first - c->prompt_buffer;
                   1093:        c->prompt_buffer = xreallocarray(c->prompt_buffer, size + 1,
                   1094:            sizeof *c->prompt_buffer);
                   1095:        first = c->prompt_buffer + off;
                   1096:        memmove(first + strlen(s), first, n * sizeof *c->prompt_buffer);
                   1097:        for (idx = 0; idx < strlen(s); idx++)
                   1098:                utf8_set(&first[idx], s[idx]);
                   1099:        c->prompt_index = (first - c->prompt_buffer) + strlen(s);
                   1100:
                   1101:        free(allocated);
                   1102:        return (1);
                   1103: }
                   1104:
1.225     nicm     1105: /* Prompt forward to the next beginning of a word. */
                   1106: static void
                   1107: status_prompt_forward_word(struct client *c, size_t size, int vi,
                   1108:     const char *separators)
                   1109: {
                   1110:        size_t           idx = c->prompt_index;
                   1111:        int              word_is_separators;
                   1112:
                   1113:        /* In emacs mode, skip until the first non-whitespace character. */
                   1114:        if (!vi)
                   1115:                while (idx != size &&
                   1116:                    status_prompt_space(&c->prompt_buffer[idx]))
                   1117:                        idx++;
                   1118:
                   1119:        /* Can't move forward if we're already at the end. */
                   1120:        if (idx == size) {
                   1121:                c->prompt_index = idx;
                   1122:                return;
                   1123:        }
                   1124:
                   1125:        /* Determine the current character class (separators or not). */
                   1126:        word_is_separators = status_prompt_in_list(separators,
                   1127:            &c->prompt_buffer[idx]) &&
                   1128:            !status_prompt_space(&c->prompt_buffer[idx]);
                   1129:
                   1130:        /* Skip ahead until the first space or opposite character class. */
                   1131:        do {
                   1132:                idx++;
                   1133:                if (status_prompt_space(&c->prompt_buffer[idx])) {
                   1134:                        /* In vi mode, go to the start of the next word. */
                   1135:                        if (vi)
                   1136:                                while (idx != size &&
                   1137:                                    status_prompt_space(&c->prompt_buffer[idx]))
                   1138:                                        idx++;
                   1139:                        break;
                   1140:                }
                   1141:        } while (idx != size && word_is_separators == status_prompt_in_list(
                   1142:            separators, &c->prompt_buffer[idx]));
                   1143:
                   1144:        c->prompt_index = idx;
                   1145: }
                   1146:
                   1147: /* Prompt forward to the next end of a word. */
                   1148: static void
                   1149: status_prompt_end_word(struct client *c, size_t size, const char *separators)
                   1150: {
                   1151:        size_t           idx = c->prompt_index;
                   1152:        int              word_is_separators;
                   1153:
                   1154:        /* Can't move forward if we're already at the end. */
                   1155:        if (idx == size)
                   1156:                return;
                   1157:
                   1158:        /* Find the next word. */
                   1159:        do {
                   1160:                idx++;
                   1161:                if (idx == size) {
                   1162:                        c->prompt_index = idx;
                   1163:                        return;
                   1164:                }
                   1165:        } while (status_prompt_space(&c->prompt_buffer[idx]));
                   1166:
                   1167:        /* Determine the character class (separators or not). */
                   1168:        word_is_separators = status_prompt_in_list(separators,
                   1169:            &c->prompt_buffer[idx]);
                   1170:
                   1171:        /* Skip ahead until the next space or opposite character class. */
                   1172:        do {
                   1173:                idx++;
                   1174:                if (idx == size)
                   1175:                        break;
                   1176:        } while (!status_prompt_space(&c->prompt_buffer[idx]) &&
                   1177:            word_is_separators == status_prompt_in_list(separators,
                   1178:            &c->prompt_buffer[idx]));
                   1179:
                   1180:        /* Back up to the previous character to stop at the end of the word. */
                   1181:        c->prompt_index = idx - 1;
                   1182: }
                   1183:
                   1184: /* Prompt backward to the previous beginning of a word. */
                   1185: static void
                   1186: status_prompt_backward_word(struct client *c, const char *separators)
                   1187: {
                   1188:        size_t  idx = c->prompt_index;
                   1189:        int     word_is_separators;
                   1190:
                   1191:        /* Find non-whitespace. */
                   1192:        while (idx != 0) {
                   1193:                --idx;
                   1194:                if (!status_prompt_space(&c->prompt_buffer[idx]))
                   1195:                        break;
                   1196:        }
                   1197:        word_is_separators = status_prompt_in_list(separators,
                   1198:            &c->prompt_buffer[idx]);
                   1199:
                   1200:        /* Find the character before the beginning of the word. */
                   1201:        while (idx != 0) {
                   1202:                --idx;
                   1203:                if (status_prompt_space(&c->prompt_buffer[idx]) ||
                   1204:                    word_is_separators != status_prompt_in_list(separators,
                   1205:                    &c->prompt_buffer[idx])) {
                   1206:                        /* Go back to the word. */
                   1207:                        idx++;
                   1208:                        break;
                   1209:                }
                   1210:        }
                   1211:        c->prompt_index = idx;
                   1212: }
                   1213:
1.1       nicm     1214: /* Handle keys in prompt. */
1.154     nicm     1215: int
1.138     nicm     1216: status_prompt_key(struct client *c, key_code key)
1.1       nicm     1217: {
1.152     nicm     1218:        struct options          *oo = c->session->options;
1.204     nicm     1219:        char                    *s, *cp, prefix = '=';
1.225     nicm     1220:        const char              *histstr, *separators = NULL, *keystring;
1.204     nicm     1221:        size_t                   size, idx;
                   1222:        struct utf8_data         tmp;
1.225     nicm     1223:        int                      keys, word_is_separators;
1.1       nicm     1224:
1.201     nicm     1225:        if (c->prompt_flags & PROMPT_KEY) {
1.213     nicm     1226:                keystring = key_string_lookup_key(key, 0);
1.201     nicm     1227:                c->prompt_inputcb(c, c->prompt_data, keystring, 1);
                   1228:                status_prompt_clear(c);
                   1229:                return (0);
                   1230:        }
1.152     nicm     1231:        size = utf8_strlen(c->prompt_buffer);
1.154     nicm     1232:
                   1233:        if (c->prompt_flags & PROMPT_NUMERIC) {
                   1234:                if (key >= '0' && key <= '9')
                   1235:                        goto append_key;
                   1236:                s = utf8_tocstr(c->prompt_buffer);
1.166     nicm     1237:                c->prompt_inputcb(c, c->prompt_data, s, 1);
1.154     nicm     1238:                status_prompt_clear(c);
                   1239:                free(s);
                   1240:                return (1);
                   1241:        }
1.213     nicm     1242:        key &= ~KEYC_MASK_FLAGS;
1.154     nicm     1243:
1.155     nicm     1244:        keys = options_get_number(c->session->options, "status-keys");
                   1245:        if (keys == MODEKEY_VI) {
                   1246:                switch (status_prompt_translate_key(c, key, &key)) {
                   1247:                case 1:
                   1248:                        goto process_key;
                   1249:                case 2:
                   1250:                        goto append_key;
                   1251:                default:
                   1252:                        return (0);
                   1253:                }
                   1254:        }
                   1255:
                   1256: process_key:
                   1257:        switch (key) {
                   1258:        case KEYC_LEFT:
                   1259:        case '\002': /* C-b */
1.1       nicm     1260:                if (c->prompt_index > 0) {
                   1261:                        c->prompt_index--;
1.158     nicm     1262:                        break;
1.1       nicm     1263:                }
                   1264:                break;
1.155     nicm     1265:        case KEYC_RIGHT:
                   1266:        case '\006': /* C-f */
1.1       nicm     1267:                if (c->prompt_index < size) {
                   1268:                        c->prompt_index++;
1.158     nicm     1269:                        break;
1.1       nicm     1270:                }
                   1271:                break;
1.155     nicm     1272:        case KEYC_HOME:
                   1273:        case '\001': /* C-a */
1.1       nicm     1274:                if (c->prompt_index != 0) {
                   1275:                        c->prompt_index = 0;
1.158     nicm     1276:                        break;
1.1       nicm     1277:                }
                   1278:                break;
1.155     nicm     1279:        case KEYC_END:
                   1280:        case '\005': /* C-e */
1.1       nicm     1281:                if (c->prompt_index != size) {
                   1282:                        c->prompt_index = size;
1.158     nicm     1283:                        break;
1.1       nicm     1284:                }
                   1285:                break;
1.155     nicm     1286:        case '\011': /* Tab */
1.206     nicm     1287:                if (status_prompt_replace_complete(c, NULL))
1.204     nicm     1288:                        goto changed;
                   1289:                break;
1.155     nicm     1290:        case KEYC_BSPACE:
                   1291:        case '\010': /* C-h */
1.1       nicm     1292:                if (c->prompt_index != 0) {
                   1293:                        if (c->prompt_index == size)
1.152     nicm     1294:                                c->prompt_buffer[--c->prompt_index].size = 0;
1.1       nicm     1295:                        else {
                   1296:                                memmove(c->prompt_buffer + c->prompt_index - 1,
                   1297:                                    c->prompt_buffer + c->prompt_index,
1.152     nicm     1298:                                    (size + 1 - c->prompt_index) *
                   1299:                                    sizeof *c->prompt_buffer);
1.1       nicm     1300:                                c->prompt_index--;
                   1301:                        }
1.158     nicm     1302:                        goto changed;
1.1       nicm     1303:                }
                   1304:                break;
1.155     nicm     1305:        case KEYC_DC:
                   1306:        case '\004': /* C-d */
1.1       nicm     1307:                if (c->prompt_index != size) {
                   1308:                        memmove(c->prompt_buffer + c->prompt_index,
                   1309:                            c->prompt_buffer + c->prompt_index + 1,
1.152     nicm     1310:                            (size + 1 - c->prompt_index) *
                   1311:                            sizeof *c->prompt_buffer);
1.158     nicm     1312:                        goto changed;
1.18      nicm     1313:                }
1.26      nicm     1314:                break;
1.155     nicm     1315:        case '\025': /* C-u */
1.152     nicm     1316:                c->prompt_buffer[0].size = 0;
1.26      nicm     1317:                c->prompt_index = 0;
1.158     nicm     1318:                goto changed;
1.155     nicm     1319:        case '\013': /* C-k */
1.18      nicm     1320:                if (c->prompt_index < size) {
1.152     nicm     1321:                        c->prompt_buffer[c->prompt_index].size = 0;
1.158     nicm     1322:                        goto changed;
1.1       nicm     1323:                }
                   1324:                break;
1.155     nicm     1325:        case '\027': /* C-w */
1.225     nicm     1326:                separators = options_get_string(oo, "word-separators");
1.81      nicm     1327:                idx = c->prompt_index;
                   1328:
1.225     nicm     1329:                /* Find non-whitespace. */
1.81      nicm     1330:                while (idx != 0) {
                   1331:                        idx--;
1.225     nicm     1332:                        if (!status_prompt_space(&c->prompt_buffer[idx]))
1.81      nicm     1333:                                break;
                   1334:                }
1.225     nicm     1335:                word_is_separators = status_prompt_in_list(separators,
                   1336:                    &c->prompt_buffer[idx]);
1.81      nicm     1337:
1.225     nicm     1338:                /* Find the character before the beginning of the word. */
1.81      nicm     1339:                while (idx != 0) {
                   1340:                        idx--;
1.225     nicm     1341:                        if (status_prompt_space(&c->prompt_buffer[idx]) ||
                   1342:                            word_is_separators != status_prompt_in_list(
                   1343:                            separators, &c->prompt_buffer[idx])) {
1.81      nicm     1344:                                /* Go back to the word. */
                   1345:                                idx++;
                   1346:                                break;
                   1347:                        }
                   1348:                }
                   1349:
1.181     nicm     1350:                free(c->prompt_saved);
                   1351:                c->prompt_saved = xcalloc(sizeof *c->prompt_buffer,
                   1352:                    (c->prompt_index - idx) + 1);
                   1353:                memcpy(c->prompt_saved, c->prompt_buffer + idx,
                   1354:                    (c->prompt_index - idx) * sizeof *c->prompt_buffer);
                   1355:
1.81      nicm     1356:                memmove(c->prompt_buffer + idx,
                   1357:                    c->prompt_buffer + c->prompt_index,
1.152     nicm     1358:                    (size + 1 - c->prompt_index) *
                   1359:                    sizeof *c->prompt_buffer);
1.81      nicm     1360:                memset(c->prompt_buffer + size - (c->prompt_index - idx),
1.152     nicm     1361:                    '\0', (c->prompt_index - idx) * sizeof *c->prompt_buffer);
1.81      nicm     1362:                c->prompt_index = idx;
1.152     nicm     1363:
1.158     nicm     1364:                goto changed;
1.225     nicm     1365:        case KEYC_RIGHT|KEYC_CTRL:
1.212     nicm     1366:        case 'f'|KEYC_META:
1.225     nicm     1367:                separators = options_get_string(oo, "word-separators");
                   1368:                status_prompt_forward_word(c, size, 0, separators);
                   1369:                goto changed;
                   1370:        case 'E'|KEYC_VI:
                   1371:                status_prompt_end_word(c, size, "");
                   1372:                goto changed;
                   1373:        case 'e'|KEYC_VI:
                   1374:                separators = options_get_string(oo, "word-separators");
                   1375:                status_prompt_end_word(c, size, separators);
                   1376:                goto changed;
                   1377:        case 'W'|KEYC_VI:
                   1378:                status_prompt_forward_word(c, size, 1, "");
                   1379:                goto changed;
                   1380:        case 'w'|KEYC_VI:
                   1381:                separators = options_get_string(oo, "word-separators");
                   1382:                status_prompt_forward_word(c, size, 1, separators);
                   1383:                goto changed;
                   1384:        case 'B'|KEYC_VI:
                   1385:                status_prompt_backward_word(c, "");
1.158     nicm     1386:                goto changed;
1.225     nicm     1387:        case KEYC_LEFT|KEYC_CTRL:
1.212     nicm     1388:        case 'b'|KEYC_META:
1.225     nicm     1389:                separators = options_get_string(oo, "word-separators");
                   1390:                status_prompt_backward_word(c, separators);
1.158     nicm     1391:                goto changed;
1.155     nicm     1392:        case KEYC_UP:
                   1393:        case '\020': /* C-p */
1.224     nicm     1394:                histstr = status_prompt_up_history(c->prompt_hindex,
                   1395:                    c->prompt_type);
1.66      nicm     1396:                if (histstr == NULL)
1.1       nicm     1397:                        break;
1.94      nicm     1398:                free(c->prompt_buffer);
1.152     nicm     1399:                c->prompt_buffer = utf8_fromcstr(histstr);
                   1400:                c->prompt_index = utf8_strlen(c->prompt_buffer);
1.158     nicm     1401:                goto changed;
1.155     nicm     1402:        case KEYC_DOWN:
                   1403:        case '\016': /* C-n */
1.224     nicm     1404:                histstr = status_prompt_down_history(c->prompt_hindex,
                   1405:                    c->prompt_type);
1.66      nicm     1406:                if (histstr == NULL)
1.65      nicm     1407:                        break;
1.94      nicm     1408:                free(c->prompt_buffer);
1.152     nicm     1409:                c->prompt_buffer = utf8_fromcstr(histstr);
                   1410:                c->prompt_index = utf8_strlen(c->prompt_buffer);
1.158     nicm     1411:                goto changed;
1.155     nicm     1412:        case '\031': /* C-y */
1.199     nicm     1413:                if (status_prompt_paste(c))
                   1414:                        goto changed;
                   1415:                break;
1.155     nicm     1416:        case '\024': /* C-t */
1.30      nicm     1417:                idx = c->prompt_index;
                   1418:                if (idx < size)
                   1419:                        idx++;
                   1420:                if (idx >= 2) {
1.152     nicm     1421:                        utf8_copy(&tmp, &c->prompt_buffer[idx - 2]);
                   1422:                        utf8_copy(&c->prompt_buffer[idx - 2],
                   1423:                            &c->prompt_buffer[idx - 1]);
                   1424:                        utf8_copy(&c->prompt_buffer[idx - 1], &tmp);
1.30      nicm     1425:                        c->prompt_index = idx;
1.158     nicm     1426:                        goto changed;
1.30      nicm     1427:                }
1.1       nicm     1428:                break;
1.155     nicm     1429:        case '\r':
                   1430:        case '\n':
1.152     nicm     1431:                s = utf8_tocstr(c->prompt_buffer);
                   1432:                if (*s != '\0')
1.224     nicm     1433:                        status_prompt_add_history(s, c->prompt_type);
1.166     nicm     1434:                if (c->prompt_inputcb(c, c->prompt_data, s, 1) == 0)
1.25      nicm     1435:                        status_prompt_clear(c);
1.152     nicm     1436:                free(s);
1.25      nicm     1437:                break;
1.155     nicm     1438:        case '\033': /* Escape */
                   1439:        case '\003': /* C-c */
1.174     nicm     1440:        case '\007': /* C-g */
1.166     nicm     1441:                if (c->prompt_inputcb(c, c->prompt_data, NULL, 1) == 0)
1.1       nicm     1442:                        status_prompt_clear(c);
                   1443:                break;
1.158     nicm     1444:        case '\022': /* C-r */
1.219     nicm     1445:                if (~c->prompt_flags & PROMPT_INCREMENTAL)
                   1446:                        break;
                   1447:                if (c->prompt_buffer[0].size == 0) {
                   1448:                        prefix = '=';
1.227     nicm     1449:                        free(c->prompt_buffer);
1.219     nicm     1450:                        c->prompt_buffer = utf8_fromcstr(c->prompt_last);
                   1451:                        c->prompt_index = utf8_strlen(c->prompt_buffer);
                   1452:                } else
1.158     nicm     1453:                        prefix = '-';
1.219     nicm     1454:                goto changed;
1.158     nicm     1455:        case '\023': /* C-s */
1.219     nicm     1456:                if (~c->prompt_flags & PROMPT_INCREMENTAL)
                   1457:                        break;
                   1458:                if (c->prompt_buffer[0].size == 0) {
                   1459:                        prefix = '=';
1.227     nicm     1460:                        free(c->prompt_buffer);
1.219     nicm     1461:                        c->prompt_buffer = utf8_fromcstr(c->prompt_last);
                   1462:                        c->prompt_index = utf8_strlen(c->prompt_buffer);
                   1463:                } else
1.158     nicm     1464:                        prefix = '+';
1.219     nicm     1465:                goto changed;
1.158     nicm     1466:        default:
                   1467:                goto append_key;
1.154     nicm     1468:        }
1.152     nicm     1469:
1.177     nicm     1470:        c->flags |= CLIENT_REDRAWSTATUS;
1.158     nicm     1471:        return (0);
                   1472:
1.154     nicm     1473: append_key:
1.216     nicm     1474:        if (key <= 0x7f)
1.215     nicm     1475:                utf8_set(&tmp, key);
1.225     nicm     1476:        else if (KEYC_IS_UNICODE(key))
                   1477:                utf8_to_data(key, &tmp);
1.215     nicm     1478:        else
1.225     nicm     1479:                return (0);
1.1       nicm     1480:
1.154     nicm     1481:        c->prompt_buffer = xreallocarray(c->prompt_buffer, size + 2,
                   1482:            sizeof *c->prompt_buffer);
1.1       nicm     1483:
1.154     nicm     1484:        if (c->prompt_index == size) {
                   1485:                utf8_copy(&c->prompt_buffer[c->prompt_index], &tmp);
                   1486:                c->prompt_index++;
                   1487:                c->prompt_buffer[c->prompt_index].size = 0;
                   1488:        } else {
                   1489:                memmove(c->prompt_buffer + c->prompt_index + 1,
                   1490:                    c->prompt_buffer + c->prompt_index,
                   1491:                    (size + 1 - c->prompt_index) *
                   1492:                    sizeof *c->prompt_buffer);
                   1493:                utf8_copy(&c->prompt_buffer[c->prompt_index], &tmp);
                   1494:                c->prompt_index++;
                   1495:        }
1.1       nicm     1496:
1.154     nicm     1497:        if (c->prompt_flags & PROMPT_SINGLE) {
1.220     nicm     1498:                if (utf8_strlen(c->prompt_buffer) != 1)
1.154     nicm     1499:                        status_prompt_clear(c);
1.220     nicm     1500:                else {
                   1501:                        s = utf8_tocstr(c->prompt_buffer);
                   1502:                        if (c->prompt_inputcb(c, c->prompt_data, s, 1) == 0)
                   1503:                                status_prompt_clear(c);
                   1504:                        free(s);
                   1505:                }
1.1       nicm     1506:        }
1.154     nicm     1507:
1.158     nicm     1508: changed:
1.177     nicm     1509:        c->flags |= CLIENT_REDRAWSTATUS;
1.158     nicm     1510:        if (c->prompt_flags & PROMPT_INCREMENTAL) {
                   1511:                s = utf8_tocstr(c->prompt_buffer);
                   1512:                xasprintf(&cp, "%c%s", prefix, s);
1.166     nicm     1513:                c->prompt_inputcb(c, c->prompt_data, cp, 0);
1.158     nicm     1514:                free(cp);
                   1515:                free(s);
                   1516:        }
1.154     nicm     1517:        return (0);
1.1       nicm     1518: }
                   1519:
1.65      nicm     1520: /* Get previous line from the history. */
1.151     nicm     1521: static const char *
1.224     nicm     1522: status_prompt_up_history(u_int *idx, u_int type)
1.65      nicm     1523: {
                   1524:        /*
1.128     nicm     1525:         * History runs from 0 to size - 1. Index is from 0 to size. Zero is
                   1526:         * empty.
1.65      nicm     1527:         */
                   1528:
1.224     nicm     1529:        if (status_prompt_hsize[type] == 0 ||
                   1530:            idx[type] == status_prompt_hsize[type])
1.65      nicm     1531:                return (NULL);
1.224     nicm     1532:        idx[type]++;
                   1533:        return (status_prompt_hlist[type][status_prompt_hsize[type] - idx[type]]);
1.65      nicm     1534: }
                   1535:
                   1536: /* Get next line from the history. */
1.151     nicm     1537: static const char *
1.224     nicm     1538: status_prompt_down_history(u_int *idx, u_int type)
1.65      nicm     1539: {
1.224     nicm     1540:        if (status_prompt_hsize[type] == 0 || idx[type] == 0)
1.65      nicm     1541:                return ("");
1.224     nicm     1542:        idx[type]--;
                   1543:        if (idx[type] == 0)
1.65      nicm     1544:                return ("");
1.224     nicm     1545:        return (status_prompt_hlist[type][status_prompt_hsize[type] - idx[type]]);
1.65      nicm     1546: }
                   1547:
1.1       nicm     1548: /* Add line to the history. */
1.151     nicm     1549: static void
1.224     nicm     1550: status_prompt_add_history(const char *line, u_int type)
1.1       nicm     1551: {
1.224     nicm     1552:        u_int   i, oldsize, newsize, freecount, hlimit, new = 1;
                   1553:        size_t  movesize;
1.65      nicm     1554:
1.224     nicm     1555:        oldsize = status_prompt_hsize[type];
                   1556:        if (oldsize > 0 &&
                   1557:            strcmp(status_prompt_hlist[type][oldsize - 1], line) == 0)
                   1558:                new = 0;
                   1559:
                   1560:        hlimit = options_get_number(global_options, "prompt-history-limit");
                   1561:        if (hlimit > oldsize) {
                   1562:                if (new == 0)
                   1563:                        return;
                   1564:                newsize = oldsize + new;
                   1565:        } else {
                   1566:                newsize = hlimit;
                   1567:                freecount = oldsize + new - newsize;
                   1568:                if (freecount > oldsize)
                   1569:                        freecount = oldsize;
                   1570:                if (freecount == 0)
                   1571:                        return;
                   1572:                for (i = 0; i < freecount; i++)
                   1573:                        free(status_prompt_hlist[type][i]);
                   1574:                movesize = (oldsize - freecount) *
                   1575:                    sizeof *status_prompt_hlist[type];
                   1576:                if (movesize > 0) {
                   1577:                        memmove(&status_prompt_hlist[type][0],
                   1578:                            &status_prompt_hlist[type][freecount], movesize);
                   1579:                }
1.128     nicm     1580:        }
1.1       nicm     1581:
1.224     nicm     1582:        if (newsize == 0) {
                   1583:                free(status_prompt_hlist[type]);
                   1584:                status_prompt_hlist[type] = NULL;
                   1585:        } else if (newsize != oldsize) {
                   1586:                status_prompt_hlist[type] =
                   1587:                    xreallocarray(status_prompt_hlist[type], newsize,
                   1588:                        sizeof *status_prompt_hlist[type]);
                   1589:        }
                   1590:
                   1591:        if (new == 1 && newsize > 0)
                   1592:                status_prompt_hlist[type][newsize - 1] = xstrdup(line);
                   1593:        status_prompt_hsize[type] = newsize;
1.128     nicm     1594: }
                   1595:
1.234     nicm     1596: /* Add to completion list. */
                   1597: static void
                   1598: status_prompt_add_list(char ***list, u_int *size, const char *s)
                   1599: {
                   1600:        u_int   i;
                   1601:
                   1602:        for (i = 0; i < *size; i++) {
                   1603:                if (strcmp((*list)[i], s) == 0)
                   1604:                        return;
                   1605:        }
                   1606:        *list = xreallocarray(*list, (*size) + 1, sizeof **list);
                   1607:        (*list)[(*size)++] = xstrdup(s);
                   1608: }
                   1609:
1.128     nicm     1610: /* Build completion list. */
1.204     nicm     1611: static char **
                   1612: status_prompt_complete_list(u_int *size, const char *s, int at_start)
1.128     nicm     1613: {
1.234     nicm     1614:        char                                    **list = NULL, *tmp;
1.183     nicm     1615:        const char                              **layout, *value, *cp;
1.128     nicm     1616:        const struct cmd_entry                  **cmdent;
                   1617:        const struct options_table_entry         *oe;
1.183     nicm     1618:        size_t                                    slen = strlen(s), valuelen;
                   1619:        struct options_entry                     *o;
1.191     nicm     1620:        struct options_array_item                *a;
1.128     nicm     1621:        const char                               *layouts[] = {
                   1622:                "even-horizontal", "even-vertical", "main-horizontal",
                   1623:                "main-vertical", "tiled", NULL
                   1624:        };
1.1       nicm     1625:
1.128     nicm     1626:        *size = 0;
1.1       nicm     1627:        for (cmdent = cmd_table; *cmdent != NULL; cmdent++) {
1.234     nicm     1628:                if (strncmp((*cmdent)->name, s, slen) == 0)
                   1629:                        status_prompt_add_list(&list, size, (*cmdent)->name);
1.205     nicm     1630:                if ((*cmdent)->alias != NULL &&
1.234     nicm     1631:                    strncmp((*cmdent)->alias, s, slen) == 0)
                   1632:                        status_prompt_add_list(&list, size, (*cmdent)->alias);
1.56      nicm     1633:        }
1.183     nicm     1634:        o = options_get_only(global_options, "command-alias");
1.191     nicm     1635:        if (o != NULL) {
                   1636:                a = options_array_first(o);
                   1637:                while (a != NULL) {
1.195     nicm     1638:                        value = options_array_item_value(a)->string;
1.194     nicm     1639:                        if ((cp = strchr(value, '=')) == NULL)
1.196     nicm     1640:                                goto next;
1.183     nicm     1641:                        valuelen = cp - value;
                   1642:                        if (slen > valuelen || strncmp(value, s, slen) != 0)
1.191     nicm     1643:                                goto next;
                   1644:
1.234     nicm     1645:                        xasprintf(&tmp, "%.*s", (int)valuelen, value);
                   1646:                        status_prompt_add_list(&list, size, tmp);
                   1647:                        free(tmp);
1.191     nicm     1648:
                   1649:                next:
                   1650:                        a = options_array_next(a);
1.183     nicm     1651:                }
                   1652:        }
1.204     nicm     1653:        if (at_start)
                   1654:                return (list);
                   1655:        for (oe = options_table; oe->name != NULL; oe++) {
1.234     nicm     1656:                if (strncmp(oe->name, s, slen) == 0)
                   1657:                        status_prompt_add_list(&list, size, oe->name);
1.204     nicm     1658:        }
                   1659:        for (layout = layouts; *layout != NULL; layout++) {
1.234     nicm     1660:                if (strncmp(*layout, s, slen) == 0)
                   1661:                        status_prompt_add_list(&list, size, *layout);
1.204     nicm     1662:        }
1.128     nicm     1663:        return (list);
                   1664: }
                   1665:
                   1666: /* Find longest prefix. */
1.151     nicm     1667: static char *
1.183     nicm     1668: status_prompt_complete_prefix(char **list, u_int size)
1.128     nicm     1669: {
                   1670:        char     *out;
                   1671:        u_int     i;
                   1672:        size_t    j;
                   1673:
1.217     nicm     1674:        if (list == NULL || size == 0)
                   1675:                return (NULL);
1.128     nicm     1676:        out = xstrdup(list[0]);
                   1677:        for (i = 1; i < size; i++) {
                   1678:                j = strlen(list[i]);
                   1679:                if (j > strlen(out))
                   1680:                        j = strlen(out);
                   1681:                for (; j > 0; j--) {
                   1682:                        if (out[j - 1] != list[i][j - 1])
                   1683:                                out[j - 1] = '\0';
                   1684:                }
1.1       nicm     1685:        }
1.128     nicm     1686:        return (out);
                   1687: }
1.1       nicm     1688:
1.204     nicm     1689: /* Complete word menu callback. */
                   1690: static void
                   1691: status_prompt_menu_callback(__unused struct menu *menu, u_int idx, key_code key,
                   1692:     void *data)
                   1693: {
                   1694:        struct status_prompt_menu       *spm = data;
                   1695:        struct client                   *c = spm->c;
                   1696:        u_int                            i;
                   1697:        char                            *s;
                   1698:
                   1699:        if (key != KEYC_NONE) {
                   1700:                idx += spm->start;
                   1701:                if (spm->flag == '\0')
                   1702:                        s = xstrdup(spm->list[idx]);
                   1703:                else
                   1704:                        xasprintf(&s, "-%c%s", spm->flag, spm->list[idx]);
1.224     nicm     1705:                if (c->prompt_type == PROMPT_TYPE_WINDOW_TARGET) {
1.205     nicm     1706:                        free(c->prompt_buffer);
                   1707:                        c->prompt_buffer = utf8_fromcstr(s);
                   1708:                        c->prompt_index = utf8_strlen(c->prompt_buffer);
                   1709:                        c->flags |= CLIENT_REDRAWSTATUS;
                   1710:                } else if (status_prompt_replace_complete(c, s))
1.204     nicm     1711:                        c->flags |= CLIENT_REDRAWSTATUS;
                   1712:                free(s);
                   1713:        }
                   1714:
                   1715:        for (i = 0; i < spm->size; i++)
                   1716:                free(spm->list[i]);
                   1717:        free(spm->list);
                   1718: }
                   1719:
                   1720: /* Show complete word menu. */
                   1721: static int
                   1722: status_prompt_complete_list_menu(struct client *c, char **list, u_int size,
                   1723:     u_int offset, char flag)
                   1724: {
                   1725:        struct menu                     *menu;
                   1726:        struct menu_item                 item;
                   1727:        struct status_prompt_menu       *spm;
                   1728:        u_int                            lines = status_line_size(c), height, i;
                   1729:        u_int                            py;
                   1730:
                   1731:        if (size <= 1)
                   1732:                return (0);
                   1733:        if (c->tty.sy - lines < 3)
                   1734:                return (0);
                   1735:
                   1736:        spm = xmalloc(sizeof *spm);
                   1737:        spm->c = c;
                   1738:        spm->size = size;
                   1739:        spm->list = list;
                   1740:        spm->flag = flag;
                   1741:
                   1742:        height = c->tty.sy - lines - 2;
                   1743:        if (height > 10)
                   1744:                height = 10;
                   1745:        if (height > size)
                   1746:                height = size;
                   1747:        spm->start = size - height;
                   1748:
                   1749:        menu = menu_create("");
                   1750:        for (i = spm->start; i < size; i++) {
                   1751:                item.name = list[i];
                   1752:                item.key = '0' + (i - spm->start);
                   1753:                item.command = NULL;
1.230     nicm     1754:                menu_add_item(menu, &item, NULL, c, NULL);
1.204     nicm     1755:        }
                   1756:
                   1757:        if (options_get_number(c->session->options, "status-position") == 0)
                   1758:                py = lines;
                   1759:        else
                   1760:                py = c->tty.sy - 3 - height;
                   1761:        offset += utf8_cstrwidth(c->prompt_string);
                   1762:        if (offset > 2)
                   1763:                offset -= 2;
                   1764:        else
                   1765:                offset = 0;
                   1766:
1.239     nicm     1767:        if (menu_display(menu, MENU_NOMOUSE|MENU_TAB, 0, NULL, offset, py, c,
1.240   ! nicm     1768:            BOX_LINES_DEFAULT, NULL, NULL, NULL, NULL,
        !          1769:            status_prompt_menu_callback, spm) != 0) {
1.204     nicm     1770:                menu_free(menu);
                   1771:                free(spm);
                   1772:                return (0);
                   1773:        }
                   1774:        return (1);
                   1775: }
                   1776:
                   1777: /* Show complete word menu. */
                   1778: static char *
                   1779: status_prompt_complete_window_menu(struct client *c, struct session *s,
1.206     nicm     1780:     const char *word, u_int offset, char flag)
1.204     nicm     1781: {
                   1782:        struct menu                      *menu;
                   1783:        struct menu_item                  item;
                   1784:        struct status_prompt_menu        *spm;
                   1785:        struct winlink                   *wl;
                   1786:        char                            **list = NULL, *tmp;
                   1787:        u_int                             lines = status_line_size(c), height;
                   1788:        u_int                             py, size = 0;
                   1789:
                   1790:        if (c->tty.sy - lines < 3)
                   1791:                return (NULL);
                   1792:
                   1793:        spm = xmalloc(sizeof *spm);
                   1794:        spm->c = c;
                   1795:        spm->flag = flag;
                   1796:
                   1797:        height = c->tty.sy - lines - 2;
                   1798:        if (height > 10)
                   1799:                height = 10;
                   1800:        spm->start = 0;
                   1801:
                   1802:        menu = menu_create("");
                   1803:        RB_FOREACH(wl, winlinks, &s->windows) {
1.206     nicm     1804:                if (word != NULL && *word != '\0') {
                   1805:                        xasprintf(&tmp, "%d", wl->idx);
                   1806:                        if (strncmp(tmp, word, strlen(word)) != 0) {
                   1807:                                free(tmp);
                   1808:                                continue;
                   1809:                        }
                   1810:                        free(tmp);
                   1811:                }
                   1812:
1.204     nicm     1813:                list = xreallocarray(list, size + 1, sizeof *list);
1.224     nicm     1814:                if (c->prompt_type == PROMPT_TYPE_WINDOW_TARGET) {
1.205     nicm     1815:                        xasprintf(&tmp, "%d (%s)", wl->idx, wl->window->name);
                   1816:                        xasprintf(&list[size++], "%d", wl->idx);
                   1817:                } else {
                   1818:                        xasprintf(&tmp, "%s:%d (%s)", s->name, wl->idx,
                   1819:                            wl->window->name);
                   1820:                        xasprintf(&list[size++], "%s:%d", s->name, wl->idx);
                   1821:                }
1.204     nicm     1822:                item.name = tmp;
                   1823:                item.key = '0' + size - 1;
                   1824:                item.command = NULL;
1.233     nicm     1825:                menu_add_item(menu, &item, NULL, c, NULL);
1.204     nicm     1826:                free(tmp);
                   1827:
                   1828:                if (size == height)
                   1829:                        break;
                   1830:        }
1.206     nicm     1831:        if (size == 0) {
                   1832:                menu_free(menu);
                   1833:                return (NULL);
                   1834:        }
1.204     nicm     1835:        if (size == 1) {
                   1836:                menu_free(menu);
1.205     nicm     1837:                if (flag != '\0') {
                   1838:                        xasprintf(&tmp, "-%c%s", flag, list[0]);
                   1839:                        free(list[0]);
                   1840:                } else
                   1841:                        tmp = list[0];
1.204     nicm     1842:                free(list);
                   1843:                return (tmp);
                   1844:        }
                   1845:        if (height > size)
                   1846:                height = size;
                   1847:
                   1848:        spm->size = size;
                   1849:        spm->list = list;
                   1850:
                   1851:        if (options_get_number(c->session->options, "status-position") == 0)
                   1852:                py = lines;
                   1853:        else
                   1854:                py = c->tty.sy - 3 - height;
                   1855:        offset += utf8_cstrwidth(c->prompt_string);
                   1856:        if (offset > 2)
                   1857:                offset -= 2;
                   1858:        else
                   1859:                offset = 0;
                   1860:
1.239     nicm     1861:        if (menu_display(menu, MENU_NOMOUSE|MENU_TAB, 0, NULL, offset, py, c,
1.240   ! nicm     1862:            BOX_LINES_DEFAULT, NULL, NULL, NULL, NULL,
        !          1863:            status_prompt_menu_callback, spm) != 0) {
1.204     nicm     1864:                menu_free(menu);
                   1865:                free(spm);
                   1866:                return (NULL);
                   1867:        }
                   1868:        return (NULL);
                   1869: }
                   1870:
                   1871: /* Sort complete list. */
                   1872: static int
                   1873: status_prompt_complete_sort(const void *a, const void *b)
                   1874: {
                   1875:        const char      **aa = (const char **)a, **bb = (const char **)b;
                   1876:
                   1877:        return (strcmp(*aa, *bb));
                   1878: }
                   1879:
1.205     nicm     1880: /* Complete a session. */
                   1881: static char *
                   1882: status_prompt_complete_session(char ***list, u_int *size, const char *s,
                   1883:     char flag)
                   1884: {
                   1885:        struct session  *loop;
1.217     nicm     1886:        char            *out, *tmp, n[11];
1.205     nicm     1887:
                   1888:        RB_FOREACH(loop, sessions, &sessions) {
1.217     nicm     1889:                if (*s == '\0' || strncmp(loop->name, s, strlen(s)) == 0) {
                   1890:                        *list = xreallocarray(*list, (*size) + 2,
                   1891:                            sizeof **list);
                   1892:                        xasprintf(&(*list)[(*size)++], "%s:", loop->name);
                   1893:                } else if (*s == '$') {
                   1894:                        xsnprintf(n, sizeof n, "%u", loop->id);
                   1895:                        if (s[1] == '\0' ||
                   1896:                            strncmp(n, s + 1, strlen(s) - 1) == 0) {
                   1897:                                *list = xreallocarray(*list, (*size) + 2,
                   1898:                                    sizeof **list);
                   1899:                                xasprintf(&(*list)[(*size)++], "$%s:", n);
                   1900:                        }
                   1901:                }
1.205     nicm     1902:        }
                   1903:        out = status_prompt_complete_prefix(*list, *size);
                   1904:        if (out != NULL && flag != '\0') {
                   1905:                xasprintf(&tmp, "-%c%s", flag, out);
                   1906:                free(out);
                   1907:                out = tmp;
                   1908:        }
                   1909:        return (out);
                   1910: }
                   1911:
1.128     nicm     1912: /* Complete word. */
1.151     nicm     1913: static char *
1.204     nicm     1914: status_prompt_complete(struct client *c, const char *word, u_int offset)
1.128     nicm     1915: {
1.217     nicm     1916:        struct session   *session;
1.204     nicm     1917:        const char       *s, *colon;
1.205     nicm     1918:        char            **list = NULL, *copy = NULL, *out = NULL;
1.204     nicm     1919:        char              flag = '\0';
1.128     nicm     1920:        u_int             size = 0, i;
                   1921:
1.206     nicm     1922:        if (*word == '\0' &&
1.224     nicm     1923:            c->prompt_type != PROMPT_TYPE_TARGET &&
                   1924:            c->prompt_type != PROMPT_TYPE_WINDOW_TARGET)
1.1       nicm     1925:                return (NULL);
1.128     nicm     1926:
1.224     nicm     1927:        if (c->prompt_type != PROMPT_TYPE_TARGET &&
                   1928:            c->prompt_type != PROMPT_TYPE_WINDOW_TARGET &&
1.205     nicm     1929:            strncmp(word, "-t", 2) != 0 &&
                   1930:            strncmp(word, "-s", 2) != 0) {
1.204     nicm     1931:                list = status_prompt_complete_list(&size, word, offset == 0);
1.128     nicm     1932:                if (size == 0)
                   1933:                        out = NULL;
                   1934:                else if (size == 1)
                   1935:                        xasprintf(&out, "%s ", list[0]);
                   1936:                else
                   1937:                        out = status_prompt_complete_prefix(list, size);
1.204     nicm     1938:                goto found;
1.128     nicm     1939:        }
                   1940:
1.224     nicm     1941:        if (c->prompt_type == PROMPT_TYPE_TARGET ||
                   1942:            c->prompt_type == PROMPT_TYPE_WINDOW_TARGET) {
1.205     nicm     1943:                s = word;
                   1944:                flag = '\0';
                   1945:        } else {
                   1946:                s = word + 2;
                   1947:                flag = word[1];
                   1948:                offset += 2;
                   1949:        }
1.206     nicm     1950:
                   1951:        /* If this is a window completion, open the window menu. */
1.224     nicm     1952:        if (c->prompt_type == PROMPT_TYPE_WINDOW_TARGET) {
1.206     nicm     1953:                out = status_prompt_complete_window_menu(c, c->session, s,
                   1954:                    offset, '\0');
                   1955:                goto found;
                   1956:        }
1.204     nicm     1957:        colon = strchr(s, ':');
1.1       nicm     1958:
1.204     nicm     1959:        /* If there is no colon, complete as a session. */
                   1960:        if (colon == NULL) {
1.205     nicm     1961:                out = status_prompt_complete_session(&list, &size, s, flag);
1.128     nicm     1962:                goto found;
                   1963:        }
                   1964:
1.204     nicm     1965:        /* If there is a colon but no period, find session and show a menu. */
                   1966:        if (strchr(colon + 1, '.') == NULL) {
                   1967:                if (*s == ':')
                   1968:                        session = c->session;
                   1969:                else {
                   1970:                        copy = xstrdup(s);
                   1971:                        *strchr(copy, ':') = '\0';
                   1972:                        session = session_find(copy);
                   1973:                        free(copy);
                   1974:                        if (session == NULL)
                   1975:                                goto found;
                   1976:                }
1.206     nicm     1977:                out = status_prompt_complete_window_menu(c, session, colon + 1,
                   1978:                    offset, flag);
1.204     nicm     1979:                if (out == NULL)
                   1980:                        return (NULL);
                   1981:        }
1.1       nicm     1982:
1.204     nicm     1983: found:
                   1984:        if (size != 0) {
                   1985:                qsort(list, size, sizeof *list, status_prompt_complete_sort);
                   1986:                for (i = 0; i < size; i++)
                   1987:                        log_debug("complete %u: %s", i, list[i]);
                   1988:        }
1.128     nicm     1989:
1.204     nicm     1990:        if (out != NULL && strcmp(word, out) == 0) {
                   1991:                free(out);
                   1992:                out = NULL;
1.1       nicm     1993:        }
1.204     nicm     1994:        if (out != NULL ||
                   1995:            !status_prompt_complete_list_menu(c, list, size, offset, flag)) {
                   1996:                for (i = 0; i < size; i++)
                   1997:                        free(list[i]);
                   1998:                free(list);
1.128     nicm     1999:        }
                   2000:        return (out);
1.224     nicm     2001: }
                   2002:
                   2003: /* Return the type of the prompt as an enum. */
                   2004: enum prompt_type
                   2005: status_prompt_type(const char *type)
                   2006: {
                   2007:        u_int   i;
                   2008:
                   2009:        for (i = 0; i < PROMPT_NTYPES; i++) {
                   2010:                if (strcmp(type, status_prompt_type_string(i)) == 0)
                   2011:                        return (i);
                   2012:        }
                   2013:        return (PROMPT_TYPE_INVALID);
                   2014: }
                   2015:
                   2016: /* Accessor for prompt_type_strings. */
                   2017: const char *
                   2018: status_prompt_type_string(u_int type)
                   2019: {
                   2020:        if (type >= PROMPT_NTYPES)
                   2021:                return ("invalid");
                   2022:        return (prompt_type_strings[type]);
1.1       nicm     2023: }