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

1.146   ! nicm        1: /* $OpenBSD: status.c,v 1.145 2015/12/11 12:27:36 nicm Exp $ */
1.1       nicm        2:
                      3: /*
                      4:  * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
                      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.139     nicm       32: char   *status_redraw_get_left(struct client *, time_t, struct grid_cell *,
                     33:            size_t *);
                     34: char   *status_redraw_get_right(struct client *, time_t, struct grid_cell *,
1.123     nicm       35:            size_t *);
                     36: char   *status_print(struct client *, struct winlink *, time_t,
                     37:            struct grid_cell *);
                     38: char   *status_replace(struct client *, struct winlink *, const char *, time_t);
1.42      nicm       39: void   status_message_callback(int, short, void *);
1.133     nicm       40: void   status_timer_callback(int, short, void *);
1.1       nicm       41:
1.65      nicm       42: const char *status_prompt_up_history(u_int *);
                     43: const char *status_prompt_down_history(u_int *);
                     44: void   status_prompt_add_history(const char *);
1.128     nicm       45:
                     46: const char **status_prompt_complete_list(u_int *, const char *);
                     47: char   *status_prompt_complete_prefix(const char **, u_int);
                     48: char   *status_prompt_complete(struct session *, const char *);
1.1       nicm       49:
1.130     nicm       50: char   *status_prompt_find_history_file(void);
                     51:
1.65      nicm       52: /* Status prompt history. */
1.128     nicm       53: #define PROMPT_HISTORY 100
                     54: char   **status_prompt_hlist;
                     55: u_int    status_prompt_hsize;
1.130     nicm       56:
                     57: /* Find the history file to load/save from/to. */
                     58: char *
                     59: status_prompt_find_history_file(void)
                     60: {
                     61:        const char      *home, *history_file;
                     62:        char            *path;
                     63:
1.137     nicm       64:        history_file = options_get_string(global_options, "history-file");
1.130     nicm       65:        if (*history_file == '\0')
                     66:                return (NULL);
                     67:        if (*history_file == '/')
                     68:                return (xstrdup(history_file));
                     69:
                     70:        if (history_file[0] != '~' || history_file[1] != '/')
                     71:                return (NULL);
                     72:        if ((home = find_home()) == NULL)
                     73:                return (NULL);
                     74:        xasprintf(&path, "%s%s", home, history_file + 1);
                     75:        return (path);
                     76: }
                     77:
                     78: /* Load status prompt history from file. */
                     79: void
                     80: status_prompt_load_history(void)
                     81: {
                     82:        FILE    *f;
                     83:        char    *history_file, *line, *tmp;
                     84:        size_t   length;
                     85:
                     86:        if ((history_file = status_prompt_find_history_file()) == NULL)
                     87:                return;
                     88:        log_debug("loading history from %s", history_file);
                     89:
                     90:        f = fopen(history_file, "r");
                     91:        if (f == NULL) {
                     92:                log_debug("%s: %s", history_file, strerror(errno));
                     93:                free(history_file);
                     94:                return;
                     95:        }
                     96:        free(history_file);
                     97:
                     98:        for (;;) {
                     99:                if ((line = fgetln(f, &length)) == NULL)
                    100:                        break;
                    101:
                    102:                if (length > 0) {
                    103:                        if (line[length - 1] == '\n') {
                    104:                                line[length - 1] = '\0';
                    105:                                status_prompt_add_history(line);
                    106:                        } else {
                    107:                                tmp = xmalloc(length + 1);
                    108:                                memcpy(tmp, line, length);
                    109:                                tmp[length] = '\0';
                    110:                                status_prompt_add_history(tmp);
                    111:                                free(tmp);
                    112:                        }
                    113:                }
                    114:        }
                    115:        fclose(f);
                    116: }
                    117:
                    118: /* Save status prompt history to file. */
                    119: void
                    120: status_prompt_save_history(void)
                    121: {
                    122:        FILE    *f;
                    123:        u_int    i;
                    124:        char    *history_file;
                    125:
                    126:        if ((history_file = status_prompt_find_history_file()) == NULL)
                    127:                return;
                    128:        log_debug("saving history to %s", history_file);
                    129:
                    130:        f = fopen(history_file, "w");
                    131:        if (f == NULL) {
                    132:                log_debug("%s: %s", history_file, strerror(errno));
                    133:                free(history_file);
                    134:                return;
                    135:        }
                    136:        free(history_file);
                    137:
                    138:        for (i = 0; i < status_prompt_hsize; i++) {
                    139:                fputs(status_prompt_hlist[i], f);
                    140:                fputc('\n', f);
                    141:        }
                    142:        fclose(f);
                    143:
1.87      nicm      144: }
                    145:
1.133     nicm      146: /* Status timer callback. */
                    147: void
1.141     nicm      148: status_timer_callback(__unused int fd, __unused short events, void *arg)
1.133     nicm      149: {
                    150:        struct client   *c = arg;
                    151:        struct session  *s = c->session;
                    152:        struct timeval   tv;
                    153:
                    154:        evtimer_del(&c->status_timer);
                    155:
                    156:        if (s == NULL)
                    157:                return;
                    158:
                    159:        if (c->message_string == NULL && c->prompt_string == NULL)
                    160:                c->flags |= CLIENT_STATUS;
                    161:
                    162:        timerclear(&tv);
1.137     nicm      163:        tv.tv_sec = options_get_number(s->options, "status-interval");
1.133     nicm      164:
                    165:        if (tv.tv_sec != 0)
                    166:                evtimer_add(&c->status_timer, &tv);
1.136     nicm      167:        log_debug("client %p, status interval %d", c, (int)tv.tv_sec);
1.133     nicm      168: }
                    169:
                    170: /* Start status timer for client. */
                    171: void
                    172: status_timer_start(struct client *c)
                    173: {
                    174:        struct session  *s = c->session;
                    175:
                    176:        if (event_initialized(&c->status_timer))
                    177:                evtimer_del(&c->status_timer);
                    178:        else
                    179:                evtimer_set(&c->status_timer, status_timer_callback, c);
                    180:
1.137     nicm      181:        if (s != NULL && options_get_number(s->options, "status"))
1.133     nicm      182:                status_timer_callback(-1, 0, c);
                    183: }
                    184:
                    185: /* Start status timer for all clients. */
                    186: void
                    187: status_timer_start_all(void)
                    188: {
                    189:        struct client   *c;
                    190:
                    191:        TAILQ_FOREACH(c, &clients, entry)
                    192:                status_timer_start(c);
                    193: }
                    194:
1.87      nicm      195: /* Get screen line of status line. -1 means off. */
                    196: int
                    197: status_at_line(struct client *c)
                    198: {
                    199:        struct session  *s = c->session;
                    200:
1.137     nicm      201:        if (!options_get_number(s->options, "status"))
1.87      nicm      202:                return (-1);
                    203:
1.137     nicm      204:        if (options_get_number(s->options, "status-position") == 0)
1.87      nicm      205:                return (0);
                    206:        return (c->tty.sy - 1);
1.71      nicm      207: }
                    208:
1.48      nicm      209: /* Retrieve options for left string. */
                    210: char *
1.139     nicm      211: status_redraw_get_left(struct client *c, time_t t, struct grid_cell *gc,
                    212:     size_t *size)
1.48      nicm      213: {
                    214:        struct session  *s = c->session;
1.120     nicm      215:        const char      *template;
1.48      nicm      216:        char            *left;
                    217:        size_t           leftlen;
                    218:
1.137     nicm      219:        style_apply_update(gc, s->options, "status-left-style");
1.48      nicm      220:
1.137     nicm      221:        template = options_get_string(s->options, "status-left");
1.123     nicm      222:        left = status_replace(c, NULL, template, t);
1.48      nicm      223:
1.137     nicm      224:        *size = options_get_number(s->options, "status-left-length");
1.139     nicm      225:        leftlen = screen_write_cstrlen("%s", left);
1.48      nicm      226:        if (leftlen < *size)
                    227:                *size = leftlen;
                    228:        return (left);
                    229: }
                    230:
                    231: /* Retrieve options for right string. */
                    232: char *
1.139     nicm      233: status_redraw_get_right(struct client *c, time_t t, struct grid_cell *gc,
                    234:     size_t *size)
1.48      nicm      235: {
                    236:        struct session  *s = c->session;
1.120     nicm      237:        const char      *template;
1.48      nicm      238:        char            *right;
                    239:        size_t           rightlen;
                    240:
1.137     nicm      241:        style_apply_update(gc, s->options, "status-right-style");
1.48      nicm      242:
1.137     nicm      243:        template = options_get_string(s->options, "status-right");
1.123     nicm      244:        right = status_replace(c, NULL, template, t);
1.48      nicm      245:
1.137     nicm      246:        *size = options_get_number(s->options, "status-right-length");
1.139     nicm      247:        rightlen = screen_write_cstrlen("%s", right);
1.48      nicm      248:        if (rightlen < *size)
                    249:                *size = rightlen;
                    250:        return (right);
                    251: }
                    252:
1.125     nicm      253: /* Get window at window list position. */
                    254: struct window *
                    255: status_get_window_at(struct client *c, u_int x)
1.73      nicm      256: {
                    257:        struct session  *s = c->session;
                    258:        struct winlink  *wl;
1.115     nicm      259:        struct options  *oo;
                    260:        size_t           len;
1.73      nicm      261:
1.88      nicm      262:        x += c->wlmouse;
1.73      nicm      263:        RB_FOREACH(wl, winlinks, &s->windows) {
1.137     nicm      264:                oo = wl->window->options;
1.125     nicm      265:                len = strlen(options_get_string(oo, "window-status-separator"));
1.115     nicm      266:
1.125     nicm      267:                if (x < wl->status_width)
                    268:                        return (wl->window);
1.115     nicm      269:                x -= wl->status_width + len;
1.73      nicm      270:        }
1.125     nicm      271:        return (NULL);
1.73      nicm      272: }
                    273:
1.1       nicm      274: /* Draw status for client on the last lines of given context. */
                    275: int
                    276: status_redraw(struct client *c)
                    277: {
1.48      nicm      278:        struct screen_write_ctx ctx;
                    279:        struct session         *s = c->session;
                    280:        struct winlink         *wl;
                    281:        struct screen           old_status, window_list;
                    282:        struct grid_cell        stdgc, lgc, rgc, gc;
1.91      nicm      283:        struct options         *oo;
1.48      nicm      284:        time_t                  t;
1.91      nicm      285:        char                   *left, *right, *sep;
1.48      nicm      286:        u_int                   offset, needed;
                    287:        u_int                   wlstart, wlwidth, wlavailable, wloffset, wlsize;
1.91      nicm      288:        size_t                  llen, rlen, seplen;
1.139     nicm      289:        int                     larrow, rarrow;
1.1       nicm      290:
1.49      nicm      291:        /* No status line? */
1.137     nicm      292:        if (c->tty.sy == 0 || !options_get_number(s->options, "status"))
1.7       nicm      293:                return (1);
1.48      nicm      294:        left = right = NULL;
1.7       nicm      295:        larrow = rarrow = 0;
1.1       nicm      296:
1.133     nicm      297:        /* Store current time. */
                    298:        t = time(NULL);
1.48      nicm      299:
                    300:        /* Set up default colour. */
1.137     nicm      301:        style_apply(&stdgc, s->options, "status-style");
1.1       nicm      302:
1.48      nicm      303:        /* Create the target screen. */
                    304:        memcpy(&old_status, &c->status, sizeof old_status);
                    305:        screen_init(&c->status, c->tty.sx, 1, 0);
                    306:        screen_write_start(&ctx, NULL, &c->status);
                    307:        for (offset = 0; offset < c->tty.sx; offset++)
                    308:                screen_write_putc(&ctx, &stdgc, ' ');
                    309:        screen_write_stop(&ctx);
                    310:
                    311:        /* If the height is one line, blank status line. */
                    312:        if (c->tty.sy <= 1)
                    313:                goto out;
1.1       nicm      314:
1.48      nicm      315:        /* Work out left and right strings. */
                    316:        memcpy(&lgc, &stdgc, sizeof lgc);
1.139     nicm      317:        left = status_redraw_get_left(c, t, &lgc, &llen);
1.48      nicm      318:        memcpy(&rgc, &stdgc, sizeof rgc);
1.139     nicm      319:        right = status_redraw_get_right(c, t, &rgc, &rlen);
1.1       nicm      320:
                    321:        /*
1.48      nicm      322:         * Figure out how much space we have for the window list. If there
                    323:         * isn't enough space, just show a blank status line.
1.1       nicm      324:         */
1.55      nicm      325:        needed = 0;
1.1       nicm      326:        if (llen != 0)
1.118     nicm      327:                needed += llen;
1.1       nicm      328:        if (rlen != 0)
1.118     nicm      329:                needed += rlen;
1.48      nicm      330:        if (c->tty.sx == 0 || c->tty.sx <= needed)
                    331:                goto out;
                    332:        wlavailable = c->tty.sx - needed;
1.1       nicm      333:
1.48      nicm      334:        /* Calculate the total size needed for the window list. */
                    335:        wlstart = wloffset = wlwidth = 0;
1.1       nicm      336:        RB_FOREACH(wl, winlinks, &s->windows) {
1.94      nicm      337:                free(wl->status_text);
1.48      nicm      338:                memcpy(&wl->status_cell, &stdgc, sizeof wl->status_cell);
                    339:                wl->status_text = status_print(c, wl, t, &wl->status_cell);
1.139     nicm      340:                wl->status_width = screen_write_cstrlen("%s", wl->status_text);
1.48      nicm      341:
1.1       nicm      342:                if (wl == s->curw)
1.48      nicm      343:                        wloffset = wlwidth;
1.91      nicm      344:
1.137     nicm      345:                oo = wl->window->options;
1.91      nicm      346:                sep = options_get_string(oo, "window-status-separator");
1.139     nicm      347:                seplen = screen_write_strlen("%s", sep);
1.91      nicm      348:                wlwidth += wl->status_width + seplen;
1.48      nicm      349:        }
                    350:
                    351:        /* Create a new screen for the window list. */
                    352:        screen_init(&window_list, wlwidth, 1, 0);
                    353:
                    354:        /* And draw the window list into it. */
                    355:        screen_write_start(&ctx, NULL, &window_list);
                    356:        RB_FOREACH(wl, winlinks, &s->windows) {
1.139     nicm      357:                screen_write_cnputs(&ctx, -1, &wl->status_cell, "%s",
                    358:                    wl->status_text);
1.91      nicm      359:
1.137     nicm      360:                oo = wl->window->options;
1.91      nicm      361:                sep = options_get_string(oo, "window-status-separator");
1.139     nicm      362:                screen_write_nputs(&ctx, -1, &stdgc, "%s", sep);
1.1       nicm      363:        }
1.48      nicm      364:        screen_write_stop(&ctx);
1.1       nicm      365:
1.48      nicm      366:        /* If there is enough space for the total width, skip to draw now. */
                    367:        if (wlwidth <= wlavailable)
1.1       nicm      368:                goto draw;
                    369:
                    370:        /* Find size of current window text. */
1.48      nicm      371:        wlsize = s->curw->status_width;
1.1       nicm      372:
                    373:        /*
1.48      nicm      374:         * If the current window is already on screen, good to draw from the
1.1       nicm      375:         * start and just leave off the end.
                    376:         */
1.48      nicm      377:        if (wloffset + wlsize < wlavailable) {
                    378:                if (wlavailable > 0) {
                    379:                        rarrow = 1;
                    380:                        wlavailable--;
                    381:                }
                    382:                wlwidth = wlavailable;
                    383:        } else {
                    384:                /*
                    385:                 * Work out how many characters we need to omit from the
                    386:                 * start. There are wlavailable characters to fill, and
                    387:                 * wloffset + wlsize must be the last. So, the start character
                    388:                 * is wloffset + wlsize - wlavailable.
                    389:                 */
                    390:                if (wlavailable > 0) {
                    391:                        larrow = 1;
                    392:                        wlavailable--;
                    393:                }
1.55      nicm      394:
1.48      nicm      395:                wlstart = wloffset + wlsize - wlavailable;
                    396:                if (wlavailable > 0 && wlwidth > wlstart + wlavailable + 1) {
1.1       nicm      397:                        rarrow = 1;
1.48      nicm      398:                        wlstart++;
                    399:                        wlavailable--;
1.1       nicm      400:                }
1.48      nicm      401:                wlwidth = wlavailable;
                    402:        }
1.1       nicm      403:
1.48      nicm      404:        /* Bail if anything is now too small too. */
                    405:        if (wlwidth == 0 || wlavailable == 0) {
                    406:                screen_free(&window_list);
                    407:                goto out;
1.1       nicm      408:        }
                    409:
                    410:        /*
1.48      nicm      411:         * Now the start position is known, work out the state of the left and
                    412:         * right arrows.
1.1       nicm      413:         */
                    414:        offset = 0;
                    415:        RB_FOREACH(wl, winlinks, &s->windows) {
1.63      nicm      416:                if (wl->flags & WINLINK_ALERTFLAGS &&
                    417:                    larrow == 1 && offset < wlstart)
                    418:                        larrow = -1;
1.1       nicm      419:
1.48      nicm      420:                offset += wl->status_width;
1.1       nicm      421:
1.63      nicm      422:                if (wl->flags & WINLINK_ALERTFLAGS &&
                    423:                    rarrow == 1 && offset > wlstart + wlwidth)
                    424:                        rarrow = -1;
1.1       nicm      425:        }
                    426:
1.48      nicm      427: draw:
1.55      nicm      428:        /* Begin drawing. */
1.48      nicm      429:        screen_write_start(&ctx, NULL, &c->status);
1.1       nicm      430:
1.48      nicm      431:        /* Draw the left string and arrow. */
                    432:        screen_write_cursormove(&ctx, 0, 0);
1.118     nicm      433:        if (llen != 0)
1.139     nicm      434:                screen_write_cnputs(&ctx, llen, &lgc, "%s", left);
1.1       nicm      435:        if (larrow != 0) {
                    436:                memcpy(&gc, &stdgc, sizeof gc);
                    437:                if (larrow == -1)
                    438:                        gc.attr ^= GRID_ATTR_REVERSE;
                    439:                screen_write_putc(&ctx, &gc, '<');
                    440:        }
1.48      nicm      441:
                    442:        /* Draw the right string and arrow. */
1.1       nicm      443:        if (rarrow != 0) {
1.118     nicm      444:                screen_write_cursormove(&ctx, c->tty.sx - rlen - 1, 0);
1.1       nicm      445:                memcpy(&gc, &stdgc, sizeof gc);
                    446:                if (rarrow == -1)
                    447:                        gc.attr ^= GRID_ATTR_REVERSE;
                    448:                screen_write_putc(&ctx, &gc, '>');
1.48      nicm      449:        } else
1.118     nicm      450:                screen_write_cursormove(&ctx, c->tty.sx - rlen, 0);
                    451:        if (rlen != 0)
1.139     nicm      452:                screen_write_cnputs(&ctx, rlen, &rgc, "%s", right);
1.1       nicm      453:
1.48      nicm      454:        /* Figure out the offset for the window list. */
1.58      nicm      455:        if (llen != 0)
1.118     nicm      456:                wloffset = llen;
1.58      nicm      457:        else
                    458:                wloffset = 0;
1.48      nicm      459:        if (wlwidth < wlavailable) {
1.137     nicm      460:                switch (options_get_number(s->options, "status-justify")) {
1.119     sthen     461:                case 1: /* centred */
1.58      nicm      462:                        wloffset += (wlavailable - wlwidth) / 2;
1.48      nicm      463:                        break;
                    464:                case 2: /* right */
1.58      nicm      465:                        wloffset += (wlavailable - wlwidth);
1.48      nicm      466:                        break;
                    467:                }
                    468:        }
                    469:        if (larrow != 0)
                    470:                wloffset++;
                    471:
                    472:        /* Copy the window list. */
1.88      nicm      473:        c->wlmouse = -wloffset + wlstart;
1.48      nicm      474:        screen_write_cursormove(&ctx, wloffset, 0);
                    475:        screen_write_copy(&ctx, &window_list, wlstart, 0, wlwidth, 1);
1.55      nicm      476:        screen_free(&window_list);
1.1       nicm      477:
1.48      nicm      478:        screen_write_stop(&ctx);
1.1       nicm      479:
                    480: out:
1.94      nicm      481:        free(left);
                    482:        free(right);
1.1       nicm      483:
                    484:        if (grid_compare(c->status.grid, old_status.grid) == 0) {
                    485:                screen_free(&old_status);
                    486:                return (0);
                    487:        }
                    488:        screen_free(&old_status);
                    489:        return (1);
                    490: }
                    491:
1.46      nicm      492: /* Replace special sequences in fmt. */
                    493: char *
1.123     nicm      494: status_replace(struct client *c, struct winlink *wl, const char *fmt, time_t t)
1.46      nicm      495: {
1.96      nicm      496:        struct format_tree      *ft;
1.129     nicm      497:        char                    *expanded;
1.47      nicm      498:
1.93      nicm      499:        if (fmt == NULL)
                    500:                return (xstrdup(""));
                    501:
1.135     nicm      502:        if (c->flags & CLIENT_STATUSFORCE)
1.145     nicm      503:                ft = format_create(NULL, FORMAT_STATUS|FORMAT_FORCE);
1.135     nicm      504:        else
1.145     nicm      505:                ft = format_create(NULL, FORMAT_STATUS);
1.129     nicm      506:        format_defaults(ft, c, NULL, wl, NULL);
1.1       nicm      507:
1.129     nicm      508:        expanded = format_expand_time(ft, fmt, t);
1.46      nicm      509:
1.99      nicm      510:        format_free(ft);
                    511:        return (expanded);
1.1       nicm      512: }
                    513:
1.46      nicm      514: /* Return winlink status line entry and adjust gc as necessary. */
1.1       nicm      515: char *
1.120     nicm      516: status_print(struct client *c, struct winlink *wl, time_t t,
                    517:     struct grid_cell *gc)
1.1       nicm      518: {
1.137     nicm      519:        struct options  *oo = wl->window->options;
1.47      nicm      520:        struct session  *s = c->session;
                    521:        const char      *fmt;
                    522:        char            *text;
1.1       nicm      523:
1.108     nicm      524:        style_apply_update(gc, oo, "window-status-style");
1.47      nicm      525:        fmt = options_get_string(oo, "window-status-format");
1.14      nicm      526:        if (wl == s->curw) {
1.108     nicm      527:                style_apply_update(gc, oo, "window-status-current-style");
1.47      nicm      528:                fmt = options_get_string(oo, "window-status-current-format");
1.95      nicm      529:        }
1.108     nicm      530:        if (wl == TAILQ_FIRST(&s->lastw))
                    531:                style_apply_update(gc, oo, "window-status-last-style");
                    532:
                    533:        if (wl->flags & WINLINK_BELL)
                    534:                style_apply_update(gc, oo, "window-status-bell-style");
                    535:        else if (wl->flags & (WINLINK_ACTIVITY|WINLINK_SILENCE))
                    536:                style_apply_update(gc, oo, "window-status-activity-style");
1.1       nicm      537:
1.123     nicm      538:        text = status_replace(c, wl, fmt, t);
1.1       nicm      539:        return (text);
                    540: }
                    541:
1.46      nicm      542: /* Set a status line message. */
1.117     nicm      543: void
1.10      nicm      544: status_message_set(struct client *c, const char *fmt, ...)
1.1       nicm      545: {
1.44      nicm      546:        struct timeval           tv;
1.127     nicm      547:        struct message_entry    *msg, *msg1;
1.44      nicm      548:        va_list                  ap;
                    549:        int                      delay;
1.127     nicm      550:        u_int                    first, limit;
                    551:
1.137     nicm      552:        limit = options_get_number(global_options, "message-limit");
1.1       nicm      553:
1.12      nicm      554:        status_prompt_clear(c);
                    555:        status_message_clear(c);
                    556:
1.28      nicm      557:        va_start(ap, fmt);
                    558:        xvasprintf(&c->message_string, fmt, ap);
                    559:        va_end(ap);
                    560:
1.127     nicm      561:        msg = xcalloc(1, sizeof *msg);
1.44      nicm      562:        msg->msg_time = time(NULL);
1.127     nicm      563:        msg->msg_num = c->message_next++;
1.44      nicm      564:        msg->msg = xstrdup(c->message_string);
1.127     nicm      565:        TAILQ_INSERT_TAIL(&c->message_log, msg, entry);
1.44      nicm      566:
1.127     nicm      567:        first = c->message_next - limit;
                    568:        TAILQ_FOREACH_SAFE(msg, &c->message_log, entry, msg1) {
                    569:                if (msg->msg_num >= first)
                    570:                        continue;
                    571:                free(msg->msg);
                    572:                TAILQ_REMOVE(&c->message_log, msg, entry);
                    573:                free(msg);
1.44      nicm      574:        }
                    575:
1.137     nicm      576:        delay = options_get_number(c->session->options, "display-time");
1.143     tim       577:        if (delay > 0) {
                    578:                tv.tv_sec = delay / 1000;
                    579:                tv.tv_usec = (delay % 1000) * 1000L;
                    580:
                    581:                if (event_initialized(&c->message_timer))
                    582:                        evtimer_del(&c->message_timer);
                    583:                evtimer_set(&c->message_timer, status_message_callback, c);
                    584:                evtimer_add(&c->message_timer, &tv);
                    585:        }
1.1       nicm      586:
                    587:        c->tty.flags |= (TTY_NOCURSOR|TTY_FREEZE);
                    588:        c->flags |= CLIENT_STATUS;
                    589: }
                    590:
1.46      nicm      591: /* Clear status line message. */
1.1       nicm      592: void
                    593: status_message_clear(struct client *c)
                    594: {
                    595:        if (c->message_string == NULL)
                    596:                return;
                    597:
1.94      nicm      598:        free(c->message_string);
1.1       nicm      599:        c->message_string = NULL;
                    600:
                    601:        c->tty.flags &= ~(TTY_NOCURSOR|TTY_FREEZE);
1.12      nicm      602:        c->flags |= CLIENT_REDRAW; /* screen was frozen and may have changed */
1.7       nicm      603:
                    604:        screen_reinit(&c->status);
1.42      nicm      605: }
                    606:
1.46      nicm      607: /* Clear status line message after timer expires. */
1.42      nicm      608: void
1.141     nicm      609: status_message_callback(__unused int fd, __unused short event, void *data)
1.42      nicm      610: {
                    611:        struct client   *c = data;
                    612:
                    613:        status_message_clear(c);
1.1       nicm      614: }
                    615:
                    616: /* Draw client message on status line of present else on last line. */
                    617: int
                    618: status_message_redraw(struct client *c)
                    619: {
                    620:        struct screen_write_ctx         ctx;
                    621:        struct session                 *s = c->session;
                    622:        struct screen                   old_status;
                    623:        size_t                          len;
                    624:        struct grid_cell                gc;
                    625:
                    626:        if (c->tty.sx == 0 || c->tty.sy == 0)
                    627:                return (0);
                    628:        memcpy(&old_status, &c->status, sizeof old_status);
                    629:        screen_init(&c->status, c->tty.sx, 1, 0);
                    630:
1.139     nicm      631:        len = screen_write_strlen("%s", c->message_string);
1.1       nicm      632:        if (len > c->tty.sx)
                    633:                len = c->tty.sx;
                    634:
1.137     nicm      635:        style_apply(&gc, s->options, "message-style");
1.1       nicm      636:
                    637:        screen_write_start(&ctx, NULL, &c->status);
                    638:
                    639:        screen_write_cursormove(&ctx, 0, 0);
1.139     nicm      640:        screen_write_nputs(&ctx, len, &gc, "%s", c->message_string);
1.1       nicm      641:        for (; len < c->tty.sx; len++)
                    642:                screen_write_putc(&ctx, &gc, ' ');
                    643:
                    644:        screen_write_stop(&ctx);
                    645:
                    646:        if (grid_compare(c->status.grid, old_status.grid) == 0) {
                    647:                screen_free(&old_status);
                    648:                return (0);
                    649:        }
                    650:        screen_free(&old_status);
                    651:        return (1);
                    652: }
                    653:
1.46      nicm      654: /* Enable status line prompt. */
1.1       nicm      655: void
1.76      nicm      656: status_prompt_set(struct client *c, const char *msg, const char *input,
1.12      nicm      657:     int (*callbackfn)(void *, const char *), void (*freefn)(void *),
                    658:     void *data, int flags)
1.1       nicm      659: {
1.122     nicm      660:        struct format_tree      *ft;
                    661:        int                      keys;
                    662:        time_t                   t;
                    663:
1.145     nicm      664:        ft = format_create(NULL, 0);
1.122     nicm      665:        format_defaults(ft, c, NULL, NULL, NULL);
                    666:        t = time(NULL);
1.20      nicm      667:
1.12      nicm      668:        status_message_clear(c);
                    669:        status_prompt_clear(c);
                    670:
1.124     nicm      671:        c->prompt_string = format_expand_time(ft, msg, t);
1.1       nicm      672:
1.124     nicm      673:        c->prompt_buffer = format_expand_time(ft, input, t);
1.76      nicm      674:        c->prompt_index = strlen(c->prompt_buffer);
1.1       nicm      675:
1.12      nicm      676:        c->prompt_callbackfn = callbackfn;
                    677:        c->prompt_freefn = freefn;
1.1       nicm      678:        c->prompt_data = data;
                    679:
                    680:        c->prompt_hindex = 0;
                    681:
                    682:        c->prompt_flags = flags;
                    683:
1.137     nicm      684:        keys = options_get_number(c->session->options, "status-keys");
1.20      nicm      685:        if (keys == MODEKEY_EMACS)
1.21      nicm      686:                mode_key_init(&c->prompt_mdata, &mode_key_tree_emacs_edit);
1.20      nicm      687:        else
1.21      nicm      688:                mode_key_init(&c->prompt_mdata, &mode_key_tree_vi_edit);
1.1       nicm      689:
                    690:        c->tty.flags |= (TTY_NOCURSOR|TTY_FREEZE);
                    691:        c->flags |= CLIENT_STATUS;
1.122     nicm      692:
                    693:        format_free(ft);
1.1       nicm      694: }
                    695:
1.46      nicm      696: /* Remove status line prompt. */
1.1       nicm      697: void
                    698: status_prompt_clear(struct client *c)
                    699: {
1.55      nicm      700:        if (c->prompt_string == NULL)
1.1       nicm      701:                return;
                    702:
1.12      nicm      703:        if (c->prompt_freefn != NULL && c->prompt_data != NULL)
                    704:                c->prompt_freefn(c->prompt_data);
1.1       nicm      705:
1.94      nicm      706:        free(c->prompt_string);
1.1       nicm      707:        c->prompt_string = NULL;
                    708:
1.94      nicm      709:        free(c->prompt_buffer);
1.1       nicm      710:        c->prompt_buffer = NULL;
                    711:
                    712:        c->tty.flags &= ~(TTY_NOCURSOR|TTY_FREEZE);
1.12      nicm      713:        c->flags |= CLIENT_REDRAW; /* screen was frozen and may have changed */
1.7       nicm      714:
                    715:        screen_reinit(&c->status);
1.27      nicm      716: }
                    717:
1.46      nicm      718: /* Update status line prompt with a new prompt string. */
1.27      nicm      719: void
1.76      nicm      720: status_prompt_update(struct client *c, const char *msg, const char *input)
1.27      nicm      721: {
1.122     nicm      722:        struct format_tree      *ft;
                    723:        time_t                   t;
                    724:
1.145     nicm      725:        ft = format_create(NULL, 0);
1.122     nicm      726:        format_defaults(ft, c, NULL, NULL, NULL);
                    727:        t = time(NULL);
                    728:
1.94      nicm      729:        free(c->prompt_string);
1.124     nicm      730:        c->prompt_string = format_expand_time(ft, msg, t);
1.27      nicm      731:
1.94      nicm      732:        free(c->prompt_buffer);
1.124     nicm      733:        c->prompt_buffer = format_expand_time(ft, input, t);
1.76      nicm      734:        c->prompt_index = strlen(c->prompt_buffer);
1.27      nicm      735:
                    736:        c->prompt_hindex = 0;
                    737:
                    738:        c->flags |= CLIENT_STATUS;
1.122     nicm      739:
                    740:        format_free(ft);
1.1       nicm      741: }
                    742:
                    743: /* Draw client prompt on status line of present else on last line. */
                    744: int
                    745: status_prompt_redraw(struct client *c)
                    746: {
1.51      nicm      747:        struct screen_write_ctx         ctx;
1.1       nicm      748:        struct session                 *s = c->session;
                    749:        struct screen                   old_status;
1.29      nicm      750:        size_t                          i, size, left, len, off;
1.140     nicm      751:        struct grid_cell                gc;
1.1       nicm      752:
                    753:        if (c->tty.sx == 0 || c->tty.sy == 0)
                    754:                return (0);
                    755:        memcpy(&old_status, &c->status, sizeof old_status);
                    756:        screen_init(&c->status, c->tty.sx, 1, 0);
                    757:
1.139     nicm      758:        len = screen_write_strlen("%s", c->prompt_string);
1.1       nicm      759:        if (len > c->tty.sx)
                    760:                len = c->tty.sx;
1.51      nicm      761:        off = 0;
1.1       nicm      762:
1.79      nicm      763:        /* Change colours for command mode. */
1.108     nicm      764:        if (c->prompt_mdata.mode == 1)
1.137     nicm      765:                style_apply(&gc, s->options, "message-command-style");
1.108     nicm      766:        else
1.137     nicm      767:                style_apply(&gc, s->options, "message-style");
1.1       nicm      768:
                    769:        screen_write_start(&ctx, NULL, &c->status);
                    770:
                    771:        screen_write_cursormove(&ctx, 0, 0);
1.139     nicm      772:        screen_write_nputs(&ctx, len, &gc, "%s", c->prompt_string);
1.1       nicm      773:
                    774:        left = c->tty.sx - len;
                    775:        if (left != 0) {
1.139     nicm      776:                size = screen_write_strlen("%s", c->prompt_buffer);
1.51      nicm      777:                if (c->prompt_index >= left) {
1.17      nicm      778:                        off = c->prompt_index - left + 1;
1.51      nicm      779:                        if (c->prompt_index == size)
1.1       nicm      780:                                left--;
                    781:                        size = left;
                    782:                }
1.139     nicm      783:                screen_write_nputs(&ctx, left, &gc, "%s", c->prompt_buffer +
                    784:                    off);
1.1       nicm      785:
1.55      nicm      786:                for (i = len + size; i < c->tty.sx; i++)
                    787:                        screen_write_putc(&ctx, &gc, ' ');
1.1       nicm      788:        }
                    789:
                    790:        screen_write_stop(&ctx);
1.51      nicm      791:
                    792:        /* Apply fake cursor. */
                    793:        off = len + c->prompt_index - off;
1.140     nicm      794:        grid_view_get_cell(c->status.grid, off, 0, &gc);
                    795:        gc.attr ^= GRID_ATTR_REVERSE;
                    796:        grid_view_set_cell(c->status.grid, off, 0, &gc);
1.1       nicm      797:
                    798:        if (grid_compare(c->status.grid, old_status.grid) == 0) {
                    799:                screen_free(&old_status);
                    800:                return (0);
                    801:        }
                    802:        screen_free(&old_status);
                    803:        return (1);
                    804: }
                    805:
                    806: /* Handle keys in prompt. */
                    807: void
1.138     nicm      808: status_prompt_key(struct client *c, key_code key)
1.1       nicm      809: {
1.81      nicm      810:        struct session          *sess = c->session;
1.137     nicm      811:        struct options          *oo = sess->options;
1.1       nicm      812:        struct paste_buffer     *pb;
1.81      nicm      813:        char                    *s, *first, *last, word[64], swapc;
1.134     nicm      814:        const char              *histstr, *bufdata, *wsep = NULL;
1.53      nicm      815:        u_char                   ch;
1.134     nicm      816:        size_t                   size, n, off, idx, bufsize;
1.1       nicm      817:
                    818:        size = strlen(c->prompt_buffer);
1.101     nicm      819:        switch (mode_key_lookup(&c->prompt_mdata, key, NULL)) {
1.20      nicm      820:        case MODEKEYEDIT_CURSORLEFT:
1.1       nicm      821:                if (c->prompt_index > 0) {
                    822:                        c->prompt_index--;
                    823:                        c->flags |= CLIENT_STATUS;
                    824:                }
                    825:                break;
1.79      nicm      826:        case MODEKEYEDIT_SWITCHMODE:
                    827:                c->flags |= CLIENT_STATUS;
                    828:                break;
1.20      nicm      829:        case MODEKEYEDIT_SWITCHMODEAPPEND:
1.79      nicm      830:                c->flags |= CLIENT_STATUS;
                    831:                /* FALLTHROUGH */
1.20      nicm      832:        case MODEKEYEDIT_CURSORRIGHT:
1.1       nicm      833:                if (c->prompt_index < size) {
                    834:                        c->prompt_index++;
                    835:                        c->flags |= CLIENT_STATUS;
                    836:                }
                    837:                break;
1.89      nicm      838:        case MODEKEYEDIT_SWITCHMODEBEGINLINE:
                    839:                c->flags |= CLIENT_STATUS;
                    840:                /* FALLTHROUGH */
1.20      nicm      841:        case MODEKEYEDIT_STARTOFLINE:
1.1       nicm      842:                if (c->prompt_index != 0) {
                    843:                        c->prompt_index = 0;
                    844:                        c->flags |= CLIENT_STATUS;
                    845:                }
                    846:                break;
1.89      nicm      847:        case MODEKEYEDIT_SWITCHMODEAPPENDLINE:
                    848:                c->flags |= CLIENT_STATUS;
                    849:                /* FALLTHROUGH */
1.20      nicm      850:        case MODEKEYEDIT_ENDOFLINE:
1.1       nicm      851:                if (c->prompt_index != size) {
                    852:                        c->prompt_index = size;
                    853:                        c->flags |= CLIENT_STATUS;
                    854:                }
                    855:                break;
1.20      nicm      856:        case MODEKEYEDIT_COMPLETE:
1.1       nicm      857:                if (*c->prompt_buffer == '\0')
                    858:                        break;
                    859:
                    860:                idx = c->prompt_index;
                    861:                if (idx != 0)
                    862:                        idx--;
                    863:
                    864:                /* Find the word we are in. */
                    865:                first = c->prompt_buffer + idx;
                    866:                while (first > c->prompt_buffer && *first != ' ')
                    867:                        first--;
                    868:                while (*first == ' ')
                    869:                        first++;
                    870:                last = c->prompt_buffer + idx;
                    871:                while (*last != '\0' && *last != ' ')
                    872:                        last++;
                    873:                while (*last == ' ')
                    874:                        last--;
                    875:                if (*last != '\0')
                    876:                        last++;
                    877:                if (last <= first ||
                    878:                    ((size_t) (last - first)) > (sizeof word) - 1)
                    879:                        break;
                    880:                memcpy(word, first, last - first);
                    881:                word[last - first] = '\0';
                    882:
                    883:                /* And try to complete it. */
1.128     nicm      884:                if ((s = status_prompt_complete(sess, word)) == NULL)
1.1       nicm      885:                        break;
                    886:
                    887:                /* Trim out word. */
                    888:                n = size - (last - c->prompt_buffer) + 1; /* with \0 */
                    889:                memmove(first, last, n);
                    890:                size -= last - first;
                    891:
                    892:                /* Insert the new word. */
1.55      nicm      893:                size += strlen(s);
1.1       nicm      894:                off = first - c->prompt_buffer;
1.116     nicm      895:                c->prompt_buffer = xrealloc(c->prompt_buffer, size + 1);
1.1       nicm      896:                first = c->prompt_buffer + off;
1.55      nicm      897:                memmove(first + strlen(s), first, n);
                    898:                memcpy(first, s, strlen(s));
1.1       nicm      899:
                    900:                c->prompt_index = (first - c->prompt_buffer) + strlen(s);
1.94      nicm      901:                free(s);
1.1       nicm      902:
                    903:                c->flags |= CLIENT_STATUS;
                    904:                break;
1.20      nicm      905:        case MODEKEYEDIT_BACKSPACE:
1.1       nicm      906:                if (c->prompt_index != 0) {
                    907:                        if (c->prompt_index == size)
                    908:                                c->prompt_buffer[--c->prompt_index] = '\0';
                    909:                        else {
                    910:                                memmove(c->prompt_buffer + c->prompt_index - 1,
                    911:                                    c->prompt_buffer + c->prompt_index,
                    912:                                    size + 1 - c->prompt_index);
                    913:                                c->prompt_index--;
                    914:                        }
                    915:                        c->flags |= CLIENT_STATUS;
                    916:                }
                    917:                break;
1.55      nicm      918:        case MODEKEYEDIT_DELETE:
1.105     nicm      919:        case MODEKEYEDIT_SWITCHMODESUBSTITUTE:
1.1       nicm      920:                if (c->prompt_index != size) {
                    921:                        memmove(c->prompt_buffer + c->prompt_index,
                    922:                            c->prompt_buffer + c->prompt_index + 1,
                    923:                            size + 1 - c->prompt_index);
1.18      nicm      924:                        c->flags |= CLIENT_STATUS;
                    925:                }
1.26      nicm      926:                break;
                    927:        case MODEKEYEDIT_DELETELINE:
1.105     nicm      928:        case MODEKEYEDIT_SWITCHMODESUBSTITUTELINE:
1.26      nicm      929:                *c->prompt_buffer = '\0';
                    930:                c->prompt_index = 0;
                    931:                c->flags |= CLIENT_STATUS;
1.18      nicm      932:                break;
1.20      nicm      933:        case MODEKEYEDIT_DELETETOENDOFLINE:
1.105     nicm      934:        case MODEKEYEDIT_SWITCHMODECHANGELINE:
1.18      nicm      935:                if (c->prompt_index < size) {
                    936:                        c->prompt_buffer[c->prompt_index] = '\0';
1.1       nicm      937:                        c->flags |= CLIENT_STATUS;
                    938:                }
                    939:                break;
1.81      nicm      940:        case MODEKEYEDIT_DELETEWORD:
                    941:                wsep = options_get_string(oo, "word-separators");
                    942:                idx = c->prompt_index;
                    943:
                    944:                /* Find a non-separator. */
                    945:                while (idx != 0) {
                    946:                        idx--;
                    947:                        if (!strchr(wsep, c->prompt_buffer[idx]))
                    948:                                break;
                    949:                }
                    950:
                    951:                /* Find the separator at the beginning of the word. */
                    952:                while (idx != 0) {
                    953:                        idx--;
                    954:                        if (strchr(wsep, c->prompt_buffer[idx])) {
                    955:                                /* Go back to the word. */
                    956:                                idx++;
                    957:                                break;
                    958:                        }
                    959:                }
                    960:
                    961:                memmove(c->prompt_buffer + idx,
                    962:                    c->prompt_buffer + c->prompt_index,
                    963:                    size + 1 - c->prompt_index);
                    964:                memset(c->prompt_buffer + size - (c->prompt_index - idx),
                    965:                    '\0', c->prompt_index - idx);
                    966:                c->prompt_index = idx;
                    967:                c->flags |= CLIENT_STATUS;
                    968:                break;
1.83      nicm      969:        case MODEKEYEDIT_NEXTSPACE:
                    970:                wsep = " ";
                    971:                /* FALLTHROUGH */
1.81      nicm      972:        case MODEKEYEDIT_NEXTWORD:
1.83      nicm      973:                if (wsep == NULL)
                    974:                        wsep = options_get_string(oo, "word-separators");
1.81      nicm      975:
                    976:                /* Find a separator. */
                    977:                while (c->prompt_index != size) {
                    978:                        c->prompt_index++;
                    979:                        if (strchr(wsep, c->prompt_buffer[c->prompt_index]))
                    980:                                break;
                    981:                }
                    982:
                    983:                /* Find the word right after the separation. */
                    984:                while (c->prompt_index != size) {
                    985:                        c->prompt_index++;
                    986:                        if (!strchr(wsep, c->prompt_buffer[c->prompt_index]))
                    987:                                break;
                    988:                }
                    989:
                    990:                c->flags |= CLIENT_STATUS;
                    991:                break;
1.83      nicm      992:        case MODEKEYEDIT_NEXTSPACEEND:
                    993:                wsep = " ";
                    994:                /* FALLTHROUGH */
1.81      nicm      995:        case MODEKEYEDIT_NEXTWORDEND:
1.83      nicm      996:                if (wsep == NULL)
                    997:                        wsep = options_get_string(oo, "word-separators");
1.81      nicm      998:
                    999:                /* Find a word. */
                   1000:                while (c->prompt_index != size) {
                   1001:                        c->prompt_index++;
                   1002:                        if (!strchr(wsep, c->prompt_buffer[c->prompt_index]))
                   1003:                                break;
                   1004:                }
                   1005:
                   1006:                /* Find the separator at the end of the word. */
                   1007:                while (c->prompt_index != size) {
                   1008:                        c->prompt_index++;
1.82      nicm     1009:                        if (strchr(wsep, c->prompt_buffer[c->prompt_index]))
1.81      nicm     1010:                                break;
                   1011:                }
1.106     nicm     1012:
                   1013:                /* Back up to the end-of-word like vi. */
                   1014:                if (options_get_number(oo, "status-keys") == MODEKEY_VI &&
                   1015:                    c->prompt_index != 0)
                   1016:                        c->prompt_index--;
1.81      nicm     1017:
                   1018:                c->flags |= CLIENT_STATUS;
                   1019:                break;
1.83      nicm     1020:        case MODEKEYEDIT_PREVIOUSSPACE:
                   1021:                wsep = " ";
                   1022:                /* FALLTHROUGH */
1.81      nicm     1023:        case MODEKEYEDIT_PREVIOUSWORD:
1.83      nicm     1024:                if (wsep == NULL)
                   1025:                        wsep = options_get_string(oo, "word-separators");
1.81      nicm     1026:
                   1027:                /* Find a non-separator. */
                   1028:                while (c->prompt_index != 0) {
                   1029:                        c->prompt_index--;
                   1030:                        if (!strchr(wsep, c->prompt_buffer[c->prompt_index]))
                   1031:                                break;
                   1032:                }
                   1033:
                   1034:                /* Find the separator at the beginning of the word. */
                   1035:                while (c->prompt_index != 0) {
                   1036:                        c->prompt_index--;
                   1037:                        if (strchr(wsep, c->prompt_buffer[c->prompt_index])) {
                   1038:                                /* Go back to the word. */
                   1039:                                c->prompt_index++;
                   1040:                                break;
                   1041:                        }
                   1042:                }
                   1043:
                   1044:                c->flags |= CLIENT_STATUS;
                   1045:                break;
1.20      nicm     1046:        case MODEKEYEDIT_HISTORYUP:
1.66      nicm     1047:                histstr = status_prompt_up_history(&c->prompt_hindex);
                   1048:                if (histstr == NULL)
1.1       nicm     1049:                        break;
1.94      nicm     1050:                free(c->prompt_buffer);
1.66      nicm     1051:                c->prompt_buffer = xstrdup(histstr);
1.1       nicm     1052:                c->prompt_index = strlen(c->prompt_buffer);
                   1053:                c->flags |= CLIENT_STATUS;
                   1054:                break;
1.20      nicm     1055:        case MODEKEYEDIT_HISTORYDOWN:
1.66      nicm     1056:                histstr = status_prompt_down_history(&c->prompt_hindex);
                   1057:                if (histstr == NULL)
1.65      nicm     1058:                        break;
1.94      nicm     1059:                free(c->prompt_buffer);
1.66      nicm     1060:                c->prompt_buffer = xstrdup(histstr);
1.1       nicm     1061:                c->prompt_index = strlen(c->prompt_buffer);
                   1062:                c->flags |= CLIENT_STATUS;
                   1063:                break;
1.20      nicm     1064:        case MODEKEYEDIT_PASTE:
1.134     nicm     1065:                if ((pb = paste_get_top(NULL)) == NULL)
1.1       nicm     1066:                        break;
1.134     nicm     1067:                bufdata = paste_buffer_data(pb, &bufsize);
                   1068:                for (n = 0; n < bufsize; n++) {
                   1069:                        ch = (u_char)bufdata[n];
1.53      nicm     1070:                        if (ch < 32 || ch == 127)
1.32      nicm     1071:                                break;
                   1072:                }
1.1       nicm     1073:
1.116     nicm     1074:                c->prompt_buffer = xrealloc(c->prompt_buffer, size + n + 1);
1.1       nicm     1075:                if (c->prompt_index == size) {
1.134     nicm     1076:                        memcpy(c->prompt_buffer + c->prompt_index, bufdata, n);
1.1       nicm     1077:                        c->prompt_index += n;
                   1078:                        c->prompt_buffer[c->prompt_index] = '\0';
                   1079:                } else {
                   1080:                        memmove(c->prompt_buffer + c->prompt_index + n,
                   1081:                            c->prompt_buffer + c->prompt_index,
                   1082:                            size + 1 - c->prompt_index);
1.134     nicm     1083:                        memcpy(c->prompt_buffer + c->prompt_index, bufdata, n);
1.1       nicm     1084:                        c->prompt_index += n;
                   1085:                }
                   1086:
                   1087:                c->flags |= CLIENT_STATUS;
1.30      nicm     1088:                break;
                   1089:        case MODEKEYEDIT_TRANSPOSECHARS:
                   1090:                idx = c->prompt_index;
                   1091:                if (idx < size)
                   1092:                        idx++;
                   1093:                if (idx >= 2) {
                   1094:                        swapc = c->prompt_buffer[idx - 2];
                   1095:                        c->prompt_buffer[idx - 2] = c->prompt_buffer[idx - 1];
                   1096:                        c->prompt_buffer[idx - 1] = swapc;
                   1097:                        c->prompt_index = idx;
                   1098:                        c->flags |= CLIENT_STATUS;
                   1099:                }
1.1       nicm     1100:                break;
1.55      nicm     1101:        case MODEKEYEDIT_ENTER:
1.25      nicm     1102:                if (*c->prompt_buffer != '\0')
1.65      nicm     1103:                        status_prompt_add_history(c->prompt_buffer);
1.25      nicm     1104:                if (c->prompt_callbackfn(c->prompt_data, c->prompt_buffer) == 0)
                   1105:                        status_prompt_clear(c);
                   1106:                break;
1.20      nicm     1107:        case MODEKEYEDIT_CANCEL:
1.12      nicm     1108:                if (c->prompt_callbackfn(c->prompt_data, NULL) == 0)
1.1       nicm     1109:                        status_prompt_clear(c);
                   1110:                break;
1.20      nicm     1111:        case MODEKEY_OTHER:
1.138     nicm     1112:                if (key <= 0x1f || key >= 0x7f)
1.1       nicm     1113:                        break;
1.116     nicm     1114:                c->prompt_buffer = xrealloc(c->prompt_buffer, size + 2);
1.1       nicm     1115:
                   1116:                if (c->prompt_index == size) {
                   1117:                        c->prompt_buffer[c->prompt_index++] = key;
                   1118:                        c->prompt_buffer[c->prompt_index] = '\0';
                   1119:                } else {
                   1120:                        memmove(c->prompt_buffer + c->prompt_index + 1,
                   1121:                            c->prompt_buffer + c->prompt_index,
                   1122:                            size + 1 - c->prompt_index);
                   1123:                        c->prompt_buffer[c->prompt_index++] = key;
                   1124:                }
                   1125:
                   1126:                if (c->prompt_flags & PROMPT_SINGLE) {
1.146   ! nicm     1127:                        if (c->prompt_callbackfn(c->prompt_data,
        !          1128:                            c->prompt_buffer) == 0)
1.1       nicm     1129:                                status_prompt_clear(c);
                   1130:                }
                   1131:
                   1132:                c->flags |= CLIENT_STATUS;
                   1133:                break;
                   1134:        default:
                   1135:                break;
                   1136:        }
                   1137: }
                   1138:
1.65      nicm     1139: /* Get previous line from the history. */
                   1140: const char *
                   1141: status_prompt_up_history(u_int *idx)
                   1142: {
                   1143:        /*
1.128     nicm     1144:         * History runs from 0 to size - 1. Index is from 0 to size. Zero is
                   1145:         * empty.
1.65      nicm     1146:         */
                   1147:
1.128     nicm     1148:        if (status_prompt_hsize == 0 || *idx == status_prompt_hsize)
1.65      nicm     1149:                return (NULL);
                   1150:        (*idx)++;
1.128     nicm     1151:        return (status_prompt_hlist[status_prompt_hsize - *idx]);
1.65      nicm     1152: }
                   1153:
                   1154: /* Get next line from the history. */
                   1155: const char *
                   1156: status_prompt_down_history(u_int *idx)
                   1157: {
1.128     nicm     1158:        if (status_prompt_hsize == 0 || *idx == 0)
1.65      nicm     1159:                return ("");
                   1160:        (*idx)--;
                   1161:        if (*idx == 0)
                   1162:                return ("");
1.128     nicm     1163:        return (status_prompt_hlist[status_prompt_hsize - *idx]);
1.65      nicm     1164: }
                   1165:
1.1       nicm     1166: /* Add line to the history. */
                   1167: void
1.65      nicm     1168: status_prompt_add_history(const char *line)
1.1       nicm     1169: {
1.128     nicm     1170:        size_t  size;
1.65      nicm     1171:
1.128     nicm     1172:        if (status_prompt_hsize > 0 &&
                   1173:            strcmp(status_prompt_hlist[status_prompt_hsize - 1], line) == 0)
1.1       nicm     1174:                return;
                   1175:
1.128     nicm     1176:        if (status_prompt_hsize == PROMPT_HISTORY) {
                   1177:                free(status_prompt_hlist[0]);
1.1       nicm     1178:
1.128     nicm     1179:                size = (PROMPT_HISTORY - 1) * sizeof *status_prompt_hlist;
                   1180:                memmove(&status_prompt_hlist[0], &status_prompt_hlist[1], size);
1.1       nicm     1181:
1.128     nicm     1182:                status_prompt_hlist[status_prompt_hsize - 1] = xstrdup(line);
                   1183:                return;
                   1184:        }
1.1       nicm     1185:
1.128     nicm     1186:        status_prompt_hlist = xreallocarray(status_prompt_hlist,
                   1187:            status_prompt_hsize + 1, sizeof *status_prompt_hlist);
                   1188:        status_prompt_hlist[status_prompt_hsize++] = xstrdup(line);
                   1189: }
                   1190:
                   1191: /* Build completion list. */
                   1192: const char **
                   1193: status_prompt_complete_list(u_int *size, const char *s)
                   1194: {
                   1195:        const char                              **list = NULL, **layout;
                   1196:        const struct cmd_entry                  **cmdent;
                   1197:        const struct options_table_entry         *oe;
                   1198:        const char                               *layouts[] = {
                   1199:                "even-horizontal", "even-vertical", "main-horizontal",
                   1200:                "main-vertical", "tiled", NULL
                   1201:        };
1.1       nicm     1202:
1.128     nicm     1203:        *size = 0;
1.1       nicm     1204:        for (cmdent = cmd_table; *cmdent != NULL; cmdent++) {
1.128     nicm     1205:                if (strncmp((*cmdent)->name, s, strlen(s)) == 0) {
                   1206:                        list = xreallocarray(list, (*size) + 1, sizeof *list);
                   1207:                        list[(*size)++] = (*cmdent)->name;
                   1208:                }
1.56      nicm     1209:        }
1.142     nicm     1210:        for (oe = options_table; oe->name != NULL; oe++) {
1.128     nicm     1211:                if (strncmp(oe->name, s, strlen(s)) == 0) {
                   1212:                        list = xreallocarray(list, (*size) + 1, sizeof *list);
                   1213:                        list[(*size)++] = oe->name;
                   1214:                }
                   1215:        }
                   1216:        for (layout = layouts; *layout != NULL; layout++) {
                   1217:                if (strncmp(*layout, s, strlen(s)) == 0) {
                   1218:                        list = xreallocarray(list, (*size) + 1, sizeof *list);
                   1219:                        list[(*size)++] = *layout;
                   1220:                }
                   1221:        }
                   1222:        return (list);
                   1223: }
                   1224:
                   1225: /* Find longest prefix. */
                   1226: char *
                   1227: status_prompt_complete_prefix(const char **list, u_int size)
                   1228: {
                   1229:        char     *out;
                   1230:        u_int     i;
                   1231:        size_t    j;
                   1232:
                   1233:        out = xstrdup(list[0]);
                   1234:        for (i = 1; i < size; i++) {
                   1235:                j = strlen(list[i]);
                   1236:                if (j > strlen(out))
                   1237:                        j = strlen(out);
                   1238:                for (; j > 0; j--) {
                   1239:                        if (out[j - 1] != list[i][j - 1])
                   1240:                                out[j - 1] = '\0';
                   1241:                }
1.1       nicm     1242:        }
1.128     nicm     1243:        return (out);
                   1244: }
1.1       nicm     1245:
1.128     nicm     1246: /* Complete word. */
                   1247: char *
                   1248: status_prompt_complete(struct session *sess, const char *s)
                   1249: {
                   1250:        const char      **list = NULL, *colon;
                   1251:        u_int             size = 0, i;
                   1252:        struct session   *s_loop;
                   1253:        struct winlink   *wl;
                   1254:        struct window    *w;
                   1255:        char             *copy, *out, *tmp;
                   1256:
                   1257:        if (*s == '\0')
1.1       nicm     1258:                return (NULL);
1.128     nicm     1259:        out = NULL;
                   1260:
                   1261:        if (strncmp(s, "-t", 2) != 0 && strncmp(s, "-s", 2) != 0) {
                   1262:                list = status_prompt_complete_list(&size, s);
                   1263:                if (size == 0)
                   1264:                        out = NULL;
                   1265:                else if (size == 1)
                   1266:                        xasprintf(&out, "%s ", list[0]);
                   1267:                else
                   1268:                        out = status_prompt_complete_prefix(list, size);
                   1269:                free(list);
                   1270:                return (out);
                   1271:        }
                   1272:        copy = xstrdup(s);
                   1273:
                   1274:        colon = ":";
                   1275:        if (copy[strlen(copy) - 1] == ':')
                   1276:                copy[strlen(copy) - 1] = '\0';
                   1277:        else
                   1278:                colon = "";
                   1279:        s = copy + 2;
1.1       nicm     1280:
1.128     nicm     1281:        RB_FOREACH(s_loop, sessions, &sessions) {
                   1282:                if (strncmp(s_loop->name, s, strlen(s)) == 0) {
                   1283:                        list = xreallocarray(list, size + 2, sizeof *list);
                   1284:                        list[size++] = s_loop->name;
                   1285:                }
                   1286:        }
                   1287:        if (size == 1) {
                   1288:                out = xstrdup(list[0]);
                   1289:                if (session_find(list[0]) != NULL)
                   1290:                        colon = ":";
                   1291:        } else if (size != 0)
                   1292:                out = status_prompt_complete_prefix(list, size);
                   1293:        if (out != NULL) {
                   1294:                xasprintf(&tmp, "-%c%s%s", copy[1], out, colon);
                   1295:                out = tmp;
                   1296:                goto found;
                   1297:        }
                   1298:
                   1299:        colon = "";
                   1300:        if (*s == ':') {
                   1301:                RB_FOREACH(wl, winlinks, &sess->windows) {
                   1302:                        xasprintf(&tmp, ":%s", wl->window->name);
                   1303:                        if (strncmp(tmp, s, strlen(s)) == 0){
                   1304:                                list = xreallocarray(list, size + 1,
                   1305:                                    sizeof *list);
                   1306:                                list[size++] = tmp;
                   1307:                                continue;
                   1308:                        }
                   1309:                        free(tmp);
1.1       nicm     1310:
1.128     nicm     1311:                        xasprintf(&tmp, ":%d", wl->idx);
                   1312:                        if (strncmp(tmp, s, strlen(s)) == 0) {
                   1313:                                list = xreallocarray(list, size + 1,
                   1314:                                    sizeof *list);
                   1315:                                list[size++] = tmp;
                   1316:                                continue;
                   1317:                        }
                   1318:                        free(tmp);
                   1319:                }
                   1320:        } else {
                   1321:                RB_FOREACH(s_loop, sessions, &sessions) {
                   1322:                        RB_FOREACH(wl, winlinks, &s_loop->windows) {
                   1323:                                w = wl->window;
                   1324:
                   1325:                                xasprintf(&tmp, "%s:%s", s_loop->name, w->name);
                   1326:                                if (strncmp(tmp, s, strlen(s)) == 0) {
                   1327:                                        list = xreallocarray(list, size + 1,
                   1328:                                            sizeof *list);
                   1329:                                        list[size++] = tmp;
                   1330:                                        continue;
                   1331:                                }
                   1332:                                free(tmp);
                   1333:
                   1334:                                xasprintf(&tmp, "%s:%d", s_loop->name, wl->idx);
                   1335:                                if (strncmp(tmp, s, strlen(s)) == 0) {
                   1336:                                        list = xreallocarray(list, size + 1,
                   1337:                                            sizeof *list);
                   1338:                                        list[size++] = tmp;
                   1339:                                        continue;
                   1340:                                }
                   1341:                                free(tmp);
                   1342:                        }
1.1       nicm     1343:                }
                   1344:        }
1.128     nicm     1345:        if (size == 1) {
                   1346:                out = xstrdup(list[0]);
                   1347:                colon = " ";
                   1348:        } else if (size != 0)
                   1349:                out = status_prompt_complete_prefix(list, size);
                   1350:        if (out != NULL) {
                   1351:                xasprintf(&tmp, "-%c%s%s", copy[1], out, colon);
                   1352:                out = tmp;
                   1353:        }
                   1354:
                   1355:        for (i = 0; i < size; i++)
                   1356:                free((void *)list[i]);
                   1357:
                   1358: found:
                   1359:        free(copy);
                   1360:        free(list);
                   1361:        return (out);
1.1       nicm     1362: }