Annotation of src/usr.bin/tmux/status.c, Revision 1.161
1.161 ! nicm 1: /* $OpenBSD: status.c,v 1.160 2017/02/03 11:57:27 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 char *status_redraw_get_left(struct client *, time_t,
33: struct grid_cell *, size_t *);
34: static char *status_redraw_get_right(struct client *, time_t,
35: struct grid_cell *, size_t *);
36: static char *status_print(struct client *, struct winlink *, time_t,
37: struct grid_cell *);
38: static char *status_replace(struct client *, struct winlink *, const char *,
39: time_t);
40: static void status_message_callback(int, short, void *);
41: static void status_timer_callback(int, short, void *);
42:
43: static char *status_prompt_find_history_file(void);
44: static const char *status_prompt_up_history(u_int *);
45: static const char *status_prompt_down_history(u_int *);
46: static void status_prompt_add_history(const char *);
47:
48: static const char **status_prompt_complete_list(u_int *, const char *);
49: static char *status_prompt_complete_prefix(const char **, u_int);
50: static char *status_prompt_complete(struct session *, const char *);
1.130 nicm 51:
1.65 nicm 52: /* Status prompt history. */
1.128 nicm 53: #define PROMPT_HISTORY 100
1.151 nicm 54: static char **status_prompt_hlist;
55: static u_int status_prompt_hsize;
1.130 nicm 56:
57: /* Find the history file to load/save from/to. */
1.151 nicm 58: static char *
1.130 nicm 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. */
1.151 nicm 147: static 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.161 ! nicm 195: /* Update status cache. */
! 196: void
! 197: status_update_saved(struct session *s)
! 198: {
! 199: if (!options_get_number(s->options, "status"))
! 200: s->statusat = -1;
! 201: else if (options_get_number(s->options, "status-position") == 0)
! 202: s->statusat = 0;
! 203: else
! 204: s->statusat = 1;
! 205: }
! 206:
1.87 nicm 207: /* Get screen line of status line. -1 means off. */
208: int
209: status_at_line(struct client *c)
210: {
211: struct session *s = c->session;
212:
1.161 ! nicm 213: if (s->statusat != 1)
! 214: return (s->statusat);
1.87 nicm 215: return (c->tty.sy - 1);
1.71 nicm 216: }
217:
1.48 nicm 218: /* Retrieve options for left string. */
1.151 nicm 219: static char *
1.139 nicm 220: status_redraw_get_left(struct client *c, time_t t, struct grid_cell *gc,
221: size_t *size)
1.48 nicm 222: {
223: struct session *s = c->session;
1.120 nicm 224: const char *template;
1.48 nicm 225: char *left;
226: size_t leftlen;
227:
1.137 nicm 228: style_apply_update(gc, s->options, "status-left-style");
1.48 nicm 229:
1.137 nicm 230: template = options_get_string(s->options, "status-left");
1.123 nicm 231: left = status_replace(c, NULL, template, t);
1.48 nicm 232:
1.137 nicm 233: *size = options_get_number(s->options, "status-left-length");
1.139 nicm 234: leftlen = screen_write_cstrlen("%s", left);
1.48 nicm 235: if (leftlen < *size)
236: *size = leftlen;
237: return (left);
238: }
239:
240: /* Retrieve options for right string. */
1.151 nicm 241: static char *
1.139 nicm 242: status_redraw_get_right(struct client *c, time_t t, struct grid_cell *gc,
243: size_t *size)
1.48 nicm 244: {
245: struct session *s = c->session;
1.120 nicm 246: const char *template;
1.48 nicm 247: char *right;
248: size_t rightlen;
249:
1.137 nicm 250: style_apply_update(gc, s->options, "status-right-style");
1.48 nicm 251:
1.137 nicm 252: template = options_get_string(s->options, "status-right");
1.123 nicm 253: right = status_replace(c, NULL, template, t);
1.48 nicm 254:
1.137 nicm 255: *size = options_get_number(s->options, "status-right-length");
1.139 nicm 256: rightlen = screen_write_cstrlen("%s", right);
1.48 nicm 257: if (rightlen < *size)
258: *size = rightlen;
259: return (right);
260: }
261:
1.125 nicm 262: /* Get window at window list position. */
263: struct window *
264: status_get_window_at(struct client *c, u_int x)
1.73 nicm 265: {
266: struct session *s = c->session;
267: struct winlink *wl;
1.115 nicm 268: struct options *oo;
1.149 nicm 269: const char *sep;
270: size_t seplen;
1.73 nicm 271:
1.88 nicm 272: x += c->wlmouse;
1.73 nicm 273: RB_FOREACH(wl, winlinks, &s->windows) {
1.137 nicm 274: oo = wl->window->options;
1.149 nicm 275:
276: sep = options_get_string(oo, "window-status-separator");
277: seplen = screen_write_cstrlen("%s", sep);
1.115 nicm 278:
1.125 nicm 279: if (x < wl->status_width)
280: return (wl->window);
1.149 nicm 281: x -= wl->status_width + seplen;
1.73 nicm 282: }
1.125 nicm 283: return (NULL);
1.73 nicm 284: }
285:
1.1 nicm 286: /* Draw status for client on the last lines of given context. */
287: int
288: status_redraw(struct client *c)
289: {
1.159 nicm 290: struct screen_write_ctx ctx;
291: struct session *s = c->session;
292: struct winlink *wl;
293: struct screen old_status, window_list;
294: struct grid_cell stdgc, lgc, rgc, gc;
295: struct options *oo;
296: time_t t;
297: char *left, *right;
298: const char *sep;
299: u_int offset, needed;
300: u_int wlstart, wlwidth, wlavailable, wloffset, wlsize;
301: size_t llen, rlen, seplen;
302: int larrow, rarrow;
1.1 nicm 303:
1.49 nicm 304: /* No status line? */
1.137 nicm 305: if (c->tty.sy == 0 || !options_get_number(s->options, "status"))
1.7 nicm 306: return (1);
1.48 nicm 307: left = right = NULL;
1.7 nicm 308: larrow = rarrow = 0;
1.1 nicm 309:
1.133 nicm 310: /* Store current time. */
311: t = time(NULL);
1.48 nicm 312:
313: /* Set up default colour. */
1.137 nicm 314: style_apply(&stdgc, s->options, "status-style");
1.1 nicm 315:
1.48 nicm 316: /* Create the target screen. */
317: memcpy(&old_status, &c->status, sizeof old_status);
318: screen_init(&c->status, c->tty.sx, 1, 0);
319: screen_write_start(&ctx, NULL, &c->status);
320: for (offset = 0; offset < c->tty.sx; offset++)
321: screen_write_putc(&ctx, &stdgc, ' ');
322: screen_write_stop(&ctx);
323:
324: /* If the height is one line, blank status line. */
325: if (c->tty.sy <= 1)
326: goto out;
1.1 nicm 327:
1.48 nicm 328: /* Work out left and right strings. */
329: memcpy(&lgc, &stdgc, sizeof lgc);
1.139 nicm 330: left = status_redraw_get_left(c, t, &lgc, &llen);
1.48 nicm 331: memcpy(&rgc, &stdgc, sizeof rgc);
1.139 nicm 332: right = status_redraw_get_right(c, t, &rgc, &rlen);
1.1 nicm 333:
334: /*
1.48 nicm 335: * Figure out how much space we have for the window list. If there
336: * isn't enough space, just show a blank status line.
1.1 nicm 337: */
1.55 nicm 338: needed = 0;
1.1 nicm 339: if (llen != 0)
1.118 nicm 340: needed += llen;
1.1 nicm 341: if (rlen != 0)
1.118 nicm 342: needed += rlen;
1.48 nicm 343: if (c->tty.sx == 0 || c->tty.sx <= needed)
344: goto out;
345: wlavailable = c->tty.sx - needed;
1.1 nicm 346:
1.48 nicm 347: /* Calculate the total size needed for the window list. */
348: wlstart = wloffset = wlwidth = 0;
1.1 nicm 349: RB_FOREACH(wl, winlinks, &s->windows) {
1.94 nicm 350: free(wl->status_text);
1.48 nicm 351: memcpy(&wl->status_cell, &stdgc, sizeof wl->status_cell);
352: wl->status_text = status_print(c, wl, t, &wl->status_cell);
1.139 nicm 353: wl->status_width = screen_write_cstrlen("%s", wl->status_text);
1.48 nicm 354:
1.1 nicm 355: if (wl == s->curw)
1.48 nicm 356: wloffset = wlwidth;
1.91 nicm 357:
1.137 nicm 358: oo = wl->window->options;
1.91 nicm 359: sep = options_get_string(oo, "window-status-separator");
1.149 nicm 360: seplen = screen_write_cstrlen("%s", sep);
1.91 nicm 361: wlwidth += wl->status_width + seplen;
1.48 nicm 362: }
363:
364: /* Create a new screen for the window list. */
365: screen_init(&window_list, wlwidth, 1, 0);
366:
367: /* And draw the window list into it. */
368: screen_write_start(&ctx, NULL, &window_list);
369: RB_FOREACH(wl, winlinks, &s->windows) {
1.139 nicm 370: screen_write_cnputs(&ctx, -1, &wl->status_cell, "%s",
371: wl->status_text);
1.91 nicm 372:
1.137 nicm 373: oo = wl->window->options;
1.91 nicm 374: sep = options_get_string(oo, "window-status-separator");
1.149 nicm 375: screen_write_cnputs(&ctx, -1, &stdgc, "%s", sep);
1.1 nicm 376: }
1.48 nicm 377: screen_write_stop(&ctx);
1.1 nicm 378:
1.48 nicm 379: /* If there is enough space for the total width, skip to draw now. */
380: if (wlwidth <= wlavailable)
1.1 nicm 381: goto draw;
382:
383: /* Find size of current window text. */
1.48 nicm 384: wlsize = s->curw->status_width;
1.1 nicm 385:
386: /*
1.48 nicm 387: * If the current window is already on screen, good to draw from the
1.1 nicm 388: * start and just leave off the end.
389: */
1.48 nicm 390: if (wloffset + wlsize < wlavailable) {
391: if (wlavailable > 0) {
392: rarrow = 1;
393: wlavailable--;
394: }
395: wlwidth = wlavailable;
396: } else {
397: /*
398: * Work out how many characters we need to omit from the
399: * start. There are wlavailable characters to fill, and
400: * wloffset + wlsize must be the last. So, the start character
401: * is wloffset + wlsize - wlavailable.
402: */
403: if (wlavailable > 0) {
404: larrow = 1;
405: wlavailable--;
406: }
1.55 nicm 407:
1.48 nicm 408: wlstart = wloffset + wlsize - wlavailable;
409: if (wlavailable > 0 && wlwidth > wlstart + wlavailable + 1) {
1.1 nicm 410: rarrow = 1;
1.48 nicm 411: wlstart++;
412: wlavailable--;
1.1 nicm 413: }
1.48 nicm 414: wlwidth = wlavailable;
415: }
1.1 nicm 416:
1.48 nicm 417: /* Bail if anything is now too small too. */
418: if (wlwidth == 0 || wlavailable == 0) {
419: screen_free(&window_list);
420: goto out;
1.1 nicm 421: }
422:
423: /*
1.48 nicm 424: * Now the start position is known, work out the state of the left and
425: * right arrows.
1.1 nicm 426: */
427: offset = 0;
428: RB_FOREACH(wl, winlinks, &s->windows) {
1.63 nicm 429: if (wl->flags & WINLINK_ALERTFLAGS &&
430: larrow == 1 && offset < wlstart)
431: larrow = -1;
1.1 nicm 432:
1.48 nicm 433: offset += wl->status_width;
1.1 nicm 434:
1.63 nicm 435: if (wl->flags & WINLINK_ALERTFLAGS &&
436: rarrow == 1 && offset > wlstart + wlwidth)
437: rarrow = -1;
1.1 nicm 438: }
439:
1.48 nicm 440: draw:
1.55 nicm 441: /* Begin drawing. */
1.48 nicm 442: screen_write_start(&ctx, NULL, &c->status);
1.1 nicm 443:
1.48 nicm 444: /* Draw the left string and arrow. */
445: screen_write_cursormove(&ctx, 0, 0);
1.118 nicm 446: if (llen != 0)
1.139 nicm 447: screen_write_cnputs(&ctx, llen, &lgc, "%s", left);
1.1 nicm 448: if (larrow != 0) {
449: memcpy(&gc, &stdgc, sizeof gc);
450: if (larrow == -1)
451: gc.attr ^= GRID_ATTR_REVERSE;
452: screen_write_putc(&ctx, &gc, '<');
453: }
1.48 nicm 454:
455: /* Draw the right string and arrow. */
1.1 nicm 456: if (rarrow != 0) {
1.118 nicm 457: screen_write_cursormove(&ctx, c->tty.sx - rlen - 1, 0);
1.1 nicm 458: memcpy(&gc, &stdgc, sizeof gc);
459: if (rarrow == -1)
460: gc.attr ^= GRID_ATTR_REVERSE;
461: screen_write_putc(&ctx, &gc, '>');
1.48 nicm 462: } else
1.118 nicm 463: screen_write_cursormove(&ctx, c->tty.sx - rlen, 0);
464: if (rlen != 0)
1.139 nicm 465: screen_write_cnputs(&ctx, rlen, &rgc, "%s", right);
1.1 nicm 466:
1.48 nicm 467: /* Figure out the offset for the window list. */
1.58 nicm 468: if (llen != 0)
1.118 nicm 469: wloffset = llen;
1.58 nicm 470: else
471: wloffset = 0;
1.48 nicm 472: if (wlwidth < wlavailable) {
1.137 nicm 473: switch (options_get_number(s->options, "status-justify")) {
1.119 sthen 474: case 1: /* centred */
1.58 nicm 475: wloffset += (wlavailable - wlwidth) / 2;
1.48 nicm 476: break;
477: case 2: /* right */
1.58 nicm 478: wloffset += (wlavailable - wlwidth);
1.48 nicm 479: break;
480: }
481: }
482: if (larrow != 0)
483: wloffset++;
484:
485: /* Copy the window list. */
1.88 nicm 486: c->wlmouse = -wloffset + wlstart;
1.48 nicm 487: screen_write_cursormove(&ctx, wloffset, 0);
1.157 nicm 488: screen_write_copy(&ctx, &window_list, wlstart, 0, wlwidth, 1, NULL,
489: NULL);
1.55 nicm 490: screen_free(&window_list);
1.1 nicm 491:
1.48 nicm 492: screen_write_stop(&ctx);
1.1 nicm 493:
494: out:
1.94 nicm 495: free(left);
496: free(right);
1.1 nicm 497:
498: if (grid_compare(c->status.grid, old_status.grid) == 0) {
499: screen_free(&old_status);
500: return (0);
501: }
502: screen_free(&old_status);
503: return (1);
504: }
505:
1.46 nicm 506: /* Replace special sequences in fmt. */
1.151 nicm 507: static char *
1.123 nicm 508: status_replace(struct client *c, struct winlink *wl, const char *fmt, time_t t)
1.46 nicm 509: {
1.96 nicm 510: struct format_tree *ft;
1.129 nicm 511: char *expanded;
1.160 nicm 512: u_int tag;
1.47 nicm 513:
1.93 nicm 514: if (fmt == NULL)
515: return (xstrdup(""));
516:
1.160 nicm 517: if (wl != NULL)
518: tag = FORMAT_WINDOW|wl->window->id;
519: else
520: tag = FORMAT_NONE;
1.135 nicm 521: if (c->flags & CLIENT_STATUSFORCE)
1.160 nicm 522: ft = format_create(NULL, tag, FORMAT_STATUS|FORMAT_FORCE);
1.135 nicm 523: else
1.160 nicm 524: ft = format_create(NULL, tag, FORMAT_STATUS);
1.129 nicm 525: format_defaults(ft, c, NULL, wl, NULL);
1.1 nicm 526:
1.129 nicm 527: expanded = format_expand_time(ft, fmt, t);
1.46 nicm 528:
1.99 nicm 529: format_free(ft);
530: return (expanded);
1.1 nicm 531: }
532:
1.46 nicm 533: /* Return winlink status line entry and adjust gc as necessary. */
1.151 nicm 534: static char *
1.120 nicm 535: status_print(struct client *c, struct winlink *wl, time_t t,
536: struct grid_cell *gc)
1.1 nicm 537: {
1.137 nicm 538: struct options *oo = wl->window->options;
1.47 nicm 539: struct session *s = c->session;
540: const char *fmt;
541: char *text;
1.1 nicm 542:
1.108 nicm 543: style_apply_update(gc, oo, "window-status-style");
1.47 nicm 544: fmt = options_get_string(oo, "window-status-format");
1.14 nicm 545: if (wl == s->curw) {
1.108 nicm 546: style_apply_update(gc, oo, "window-status-current-style");
1.47 nicm 547: fmt = options_get_string(oo, "window-status-current-format");
1.95 nicm 548: }
1.108 nicm 549: if (wl == TAILQ_FIRST(&s->lastw))
550: style_apply_update(gc, oo, "window-status-last-style");
551:
552: if (wl->flags & WINLINK_BELL)
553: style_apply_update(gc, oo, "window-status-bell-style");
554: else if (wl->flags & (WINLINK_ACTIVITY|WINLINK_SILENCE))
555: style_apply_update(gc, oo, "window-status-activity-style");
1.1 nicm 556:
1.123 nicm 557: text = status_replace(c, wl, fmt, t);
1.1 nicm 558: return (text);
559: }
560:
1.46 nicm 561: /* Set a status line message. */
1.117 nicm 562: void
1.10 nicm 563: status_message_set(struct client *c, const char *fmt, ...)
1.1 nicm 564: {
1.44 nicm 565: struct timeval tv;
1.127 nicm 566: struct message_entry *msg, *msg1;
1.44 nicm 567: va_list ap;
568: int delay;
1.147 nicm 569: u_int limit;
1.127 nicm 570:
1.137 nicm 571: limit = options_get_number(global_options, "message-limit");
1.1 nicm 572:
1.12 nicm 573: status_message_clear(c);
574:
1.28 nicm 575: va_start(ap, fmt);
576: xvasprintf(&c->message_string, fmt, ap);
577: va_end(ap);
578:
1.127 nicm 579: msg = xcalloc(1, sizeof *msg);
1.44 nicm 580: msg->msg_time = time(NULL);
1.127 nicm 581: msg->msg_num = c->message_next++;
1.44 nicm 582: msg->msg = xstrdup(c->message_string);
1.127 nicm 583: TAILQ_INSERT_TAIL(&c->message_log, msg, entry);
1.44 nicm 584:
1.127 nicm 585: TAILQ_FOREACH_SAFE(msg, &c->message_log, entry, msg1) {
1.147 nicm 586: if (msg->msg_num + limit >= c->message_next)
587: break;
1.127 nicm 588: free(msg->msg);
589: TAILQ_REMOVE(&c->message_log, msg, entry);
590: free(msg);
1.44 nicm 591: }
592:
1.137 nicm 593: delay = options_get_number(c->session->options, "display-time");
1.143 tim 594: if (delay > 0) {
595: tv.tv_sec = delay / 1000;
596: tv.tv_usec = (delay % 1000) * 1000L;
597:
598: if (event_initialized(&c->message_timer))
599: evtimer_del(&c->message_timer);
600: evtimer_set(&c->message_timer, status_message_callback, c);
601: evtimer_add(&c->message_timer, &tv);
602: }
1.1 nicm 603:
604: c->tty.flags |= (TTY_NOCURSOR|TTY_FREEZE);
605: c->flags |= CLIENT_STATUS;
606: }
607:
1.46 nicm 608: /* Clear status line message. */
1.1 nicm 609: void
610: status_message_clear(struct client *c)
611: {
612: if (c->message_string == NULL)
613: return;
614:
1.94 nicm 615: free(c->message_string);
1.1 nicm 616: c->message_string = NULL;
617:
1.156 nicm 618: if (c->prompt_string == NULL)
619: c->tty.flags &= ~(TTY_NOCURSOR|TTY_FREEZE);
1.12 nicm 620: c->flags |= CLIENT_REDRAW; /* screen was frozen and may have changed */
1.7 nicm 621:
622: screen_reinit(&c->status);
1.42 nicm 623: }
624:
1.46 nicm 625: /* Clear status line message after timer expires. */
1.151 nicm 626: static void
1.141 nicm 627: status_message_callback(__unused int fd, __unused short event, void *data)
1.42 nicm 628: {
629: struct client *c = data;
630:
631: status_message_clear(c);
1.1 nicm 632: }
633:
634: /* Draw client message on status line of present else on last line. */
635: int
636: status_message_redraw(struct client *c)
637: {
638: struct screen_write_ctx ctx;
639: struct session *s = c->session;
640: struct screen old_status;
641: size_t len;
642: struct grid_cell gc;
643:
644: if (c->tty.sx == 0 || c->tty.sy == 0)
645: return (0);
646: memcpy(&old_status, &c->status, sizeof old_status);
647: screen_init(&c->status, c->tty.sx, 1, 0);
648:
1.139 nicm 649: len = screen_write_strlen("%s", c->message_string);
1.1 nicm 650: if (len > c->tty.sx)
651: len = c->tty.sx;
652:
1.137 nicm 653: style_apply(&gc, s->options, "message-style");
1.1 nicm 654:
655: screen_write_start(&ctx, NULL, &c->status);
656:
657: screen_write_cursormove(&ctx, 0, 0);
1.139 nicm 658: screen_write_nputs(&ctx, len, &gc, "%s", c->message_string);
1.1 nicm 659: for (; len < c->tty.sx; len++)
660: screen_write_putc(&ctx, &gc, ' ');
661:
662: screen_write_stop(&ctx);
663:
664: if (grid_compare(c->status.grid, old_status.grid) == 0) {
665: screen_free(&old_status);
666: return (0);
667: }
668: screen_free(&old_status);
669: return (1);
670: }
671:
1.46 nicm 672: /* Enable status line prompt. */
1.1 nicm 673: void
1.76 nicm 674: status_prompt_set(struct client *c, const char *msg, const char *input,
1.158 nicm 675: int (*callbackfn)(void *, const char *, int), void (*freefn)(void *),
1.12 nicm 676: void *data, int flags)
1.1 nicm 677: {
1.122 nicm 678: struct format_tree *ft;
679: time_t t;
1.152 nicm 680: char *tmp;
1.122 nicm 681:
1.160 nicm 682: ft = format_create(NULL, FORMAT_NONE, 0);
1.122 nicm 683: format_defaults(ft, c, NULL, NULL, NULL);
1.152 nicm 684:
1.122 nicm 685: t = time(NULL);
1.152 nicm 686: tmp = format_expand_time(ft, input, t);
1.20 nicm 687:
1.12 nicm 688: status_message_clear(c);
689: status_prompt_clear(c);
690:
1.124 nicm 691: c->prompt_string = format_expand_time(ft, msg, t);
1.1 nicm 692:
1.152 nicm 693: c->prompt_buffer = utf8_fromcstr(tmp);
694: c->prompt_index = utf8_strlen(c->prompt_buffer);
1.1 nicm 695:
1.12 nicm 696: c->prompt_callbackfn = callbackfn;
697: c->prompt_freefn = freefn;
1.1 nicm 698: c->prompt_data = data;
699:
700: c->prompt_hindex = 0;
701:
702: c->prompt_flags = flags;
1.155 nicm 703: c->prompt_mode = PROMPT_ENTRY;
1.1 nicm 704:
1.158 nicm 705: if (~flags & PROMPT_INCREMENTAL)
706: c->tty.flags |= (TTY_NOCURSOR|TTY_FREEZE);
1.1 nicm 707: c->flags |= CLIENT_STATUS;
1.122 nicm 708:
1.152 nicm 709: free(tmp);
1.122 nicm 710: format_free(ft);
1.1 nicm 711: }
712:
1.46 nicm 713: /* Remove status line prompt. */
1.1 nicm 714: void
715: status_prompt_clear(struct client *c)
716: {
1.55 nicm 717: if (c->prompt_string == NULL)
1.1 nicm 718: return;
719:
1.12 nicm 720: if (c->prompt_freefn != NULL && c->prompt_data != NULL)
721: c->prompt_freefn(c->prompt_data);
1.1 nicm 722:
1.94 nicm 723: free(c->prompt_string);
1.1 nicm 724: c->prompt_string = NULL;
725:
1.94 nicm 726: free(c->prompt_buffer);
1.1 nicm 727: c->prompt_buffer = NULL;
728:
729: c->tty.flags &= ~(TTY_NOCURSOR|TTY_FREEZE);
1.12 nicm 730: c->flags |= CLIENT_REDRAW; /* screen was frozen and may have changed */
1.7 nicm 731:
732: screen_reinit(&c->status);
1.27 nicm 733: }
734:
1.46 nicm 735: /* Update status line prompt with a new prompt string. */
1.27 nicm 736: void
1.76 nicm 737: status_prompt_update(struct client *c, const char *msg, const char *input)
1.27 nicm 738: {
1.122 nicm 739: struct format_tree *ft;
740: time_t t;
1.152 nicm 741: char *tmp;
1.122 nicm 742:
1.160 nicm 743: ft = format_create(NULL, FORMAT_NONE, 0);
1.122 nicm 744: format_defaults(ft, c, NULL, NULL, NULL);
1.152 nicm 745:
1.122 nicm 746: t = time(NULL);
1.152 nicm 747: tmp = format_expand_time(ft, input, t);
1.122 nicm 748:
1.94 nicm 749: free(c->prompt_string);
1.124 nicm 750: c->prompt_string = format_expand_time(ft, msg, t);
1.27 nicm 751:
1.94 nicm 752: free(c->prompt_buffer);
1.152 nicm 753: c->prompt_buffer = utf8_fromcstr(tmp);
754: c->prompt_index = utf8_strlen(c->prompt_buffer);
1.27 nicm 755:
756: c->prompt_hindex = 0;
757:
758: c->flags |= CLIENT_STATUS;
1.122 nicm 759:
1.152 nicm 760: free(tmp);
1.122 nicm 761: format_free(ft);
1.1 nicm 762: }
763:
764: /* Draw client prompt on status line of present else on last line. */
765: int
766: status_prompt_redraw(struct client *c)
767: {
1.152 nicm 768: struct screen_write_ctx ctx;
769: struct session *s = c->session;
770: struct screen old_status;
771: u_int i, offset, left, start, pcursor, pwidth, width;
772: struct grid_cell gc, cursorgc;
1.1 nicm 773:
774: if (c->tty.sx == 0 || c->tty.sy == 0)
775: return (0);
776: memcpy(&old_status, &c->status, sizeof old_status);
777: screen_init(&c->status, c->tty.sx, 1, 0);
778:
1.155 nicm 779: if (c->prompt_mode == PROMPT_COMMAND)
1.137 nicm 780: style_apply(&gc, s->options, "message-command-style");
1.108 nicm 781: else
1.137 nicm 782: style_apply(&gc, s->options, "message-style");
1.1 nicm 783:
1.152 nicm 784: memcpy(&cursorgc, &gc, sizeof cursorgc);
785: cursorgc.attr ^= GRID_ATTR_REVERSE;
786:
787: start = screen_write_strlen("%s", c->prompt_string);
788: if (start > c->tty.sx)
789: start = c->tty.sx;
790:
1.1 nicm 791: screen_write_start(&ctx, NULL, &c->status);
792: screen_write_cursormove(&ctx, 0, 0);
1.152 nicm 793: screen_write_nputs(&ctx, start, &gc, "%s", c->prompt_string);
794: while (c->status.cx < screen_size_x(&c->status))
795: screen_write_putc(&ctx, &gc, ' ');
796: screen_write_cursormove(&ctx, start, 0);
1.1 nicm 797:
1.152 nicm 798: left = c->tty.sx - start;
799: if (left == 0)
800: goto finished;
801:
802: pcursor = utf8_strwidth(c->prompt_buffer, c->prompt_index);
803: pwidth = utf8_strwidth(c->prompt_buffer, -1);
804: if (pcursor >= left) {
805: /*
806: * The cursor would be outside the screen so start drawing
807: * with it on the right.
808: */
809: offset = (pcursor - left) + 1;
810: pwidth = left;
811: } else
812: offset = 0;
813: if (pwidth > left)
814: pwidth = left;
815:
816: width = 0;
817: for (i = 0; c->prompt_buffer[i].size != 0; i++) {
818: if (width < offset) {
819: width += c->prompt_buffer[i].width;
820: continue;
1.1 nicm 821: }
1.152 nicm 822: if (width >= offset + pwidth)
823: break;
824: width += c->prompt_buffer[i].width;
825: if (width > offset + pwidth)
826: break;
1.1 nicm 827:
1.152 nicm 828: if (i != c->prompt_index) {
829: utf8_copy(&gc.data, &c->prompt_buffer[i]);
830: screen_write_cell(&ctx, &gc);
831: } else {
832: utf8_copy(&cursorgc.data, &c->prompt_buffer[i]);
833: screen_write_cell(&ctx, &cursorgc);
834: }
1.1 nicm 835: }
1.152 nicm 836: if (c->status.cx < screen_size_x(&c->status) && c->prompt_index >= i)
837: screen_write_putc(&ctx, &cursorgc, ' ');
1.1 nicm 838:
1.152 nicm 839: finished:
1.1 nicm 840: screen_write_stop(&ctx);
1.51 nicm 841:
1.1 nicm 842: if (grid_compare(c->status.grid, old_status.grid) == 0) {
843: screen_free(&old_status);
844: return (0);
845: }
846: screen_free(&old_status);
847: return (1);
848: }
849:
1.152 nicm 850: /* Is this a separator? */
851: static int
852: status_prompt_in_list(const char *ws, const struct utf8_data *ud)
853: {
854: if (ud->size != 1 || ud->width != 1)
855: return (0);
856: return (strchr(ws, *ud->data) != NULL);
857: }
858:
859: /* Is this a space? */
860: static int
861: status_prompt_space(const struct utf8_data *ud)
862: {
863: if (ud->size != 1 || ud->width != 1)
864: return (0);
865: return (*ud->data == ' ');
866: }
867:
1.155 nicm 868: /*
869: * Translate key from emacs to vi. Return 0 to drop key, 1 to process the key
870: * as an emacs key; return 2 to append to the buffer.
871: */
872: static int
873: status_prompt_translate_key(struct client *c, key_code key, key_code *new_key)
874: {
875: if (c->prompt_mode == PROMPT_ENTRY) {
876: switch (key) {
877: case '\003': /* C-c */
878: case '\010': /* C-h */
879: case '\011': /* Tab */
880: case '\025': /* C-u */
881: case '\027': /* C-w */
882: case '\n':
883: case '\r':
884: case KEYC_BSPACE:
885: case KEYC_DC:
886: case KEYC_DOWN:
887: case KEYC_END:
888: case KEYC_HOME:
889: case KEYC_LEFT:
890: case KEYC_RIGHT:
891: case KEYC_UP:
892: *new_key = key;
893: return (1);
894: case '\033': /* Escape */
895: c->prompt_mode = PROMPT_COMMAND;
896: c->flags |= CLIENT_STATUS;
897: return (0);
898: }
899: *new_key = key;
900: return (2);
901: }
902:
903: switch (key) {
904: case 'A':
905: case 'I':
906: case 'C':
907: case 's':
908: case 'a':
909: c->prompt_mode = PROMPT_ENTRY;
910: c->flags |= CLIENT_STATUS;
911: break; /* switch mode and... */
912: case 'S':
913: c->prompt_mode = PROMPT_ENTRY;
914: c->flags |= CLIENT_STATUS;
915: *new_key = '\025'; /* C-u */
916: return (1);
917: case 'i':
918: case '\033': /* Escape */
919: c->prompt_mode = PROMPT_ENTRY;
920: c->flags |= CLIENT_STATUS;
921: return (0);
922: }
923:
924: switch (key) {
925: case 'A':
926: case '$':
927: *new_key = KEYC_END;
928: return (1);
929: case 'I':
930: case '0':
931: case '^':
932: *new_key = KEYC_HOME;
933: return (1);
934: case 'C':
935: case 'D':
936: *new_key = '\013'; /* C-k */
937: return (1);
938: case KEYC_BSPACE:
939: case 'X':
940: *new_key = KEYC_BSPACE;
941: return (1);
942: case 'b':
943: case 'B':
944: *new_key = 'b'|KEYC_ESCAPE;
945: return (1);
946: case 'd':
947: *new_key = '\025';
948: return (1);
949: case 'e':
950: case 'E':
951: case 'w':
952: case 'W':
953: *new_key = 'f'|KEYC_ESCAPE;
954: return (1);
955: case 'p':
956: *new_key = '\031'; /* C-y */
957: return (1);
958: case 's':
959: case KEYC_DC:
960: case 'x':
961: *new_key = KEYC_DC;
962: return (1);
963: case KEYC_DOWN:
964: case 'j':
965: *new_key = KEYC_DOWN;
966: return (1);
967: case KEYC_LEFT:
968: case 'h':
969: *new_key = KEYC_LEFT;
970: return (1);
971: case 'a':
972: case KEYC_RIGHT:
973: case 'l':
974: *new_key = KEYC_RIGHT;
975: return (1);
976: case KEYC_UP:
977: case 'k':
978: *new_key = KEYC_UP;
979: return (1);
980: case '\010' /* C-h */:
981: case '\003' /* C-c */:
982: case '\n':
983: case '\r':
984: return (1);
985: }
986: return (0);
987: }
988:
1.1 nicm 989: /* Handle keys in prompt. */
1.154 nicm 990: int
1.138 nicm 991: status_prompt_key(struct client *c, key_code key)
1.1 nicm 992: {
1.152 nicm 993: struct options *oo = c->session->options;
1.1 nicm 994: struct paste_buffer *pb;
1.158 nicm 995: char *s, *cp, word[64], prefix = '=';
1.152 nicm 996: const char *histstr, *bufdata, *ws = NULL;
1.53 nicm 997: u_char ch;
1.152 nicm 998: size_t size, n, off, idx, bufsize, used;
999: struct utf8_data tmp, *first, *last, *ud;
1.155 nicm 1000: int keys;
1.1 nicm 1001:
1.152 nicm 1002: size = utf8_strlen(c->prompt_buffer);
1.154 nicm 1003:
1004: if (c->prompt_flags & PROMPT_NUMERIC) {
1005: if (key >= '0' && key <= '9')
1006: goto append_key;
1007: s = utf8_tocstr(c->prompt_buffer);
1.158 nicm 1008: c->prompt_callbackfn(c->prompt_data, s, 1);
1.154 nicm 1009: status_prompt_clear(c);
1010: free(s);
1011: return (1);
1012: }
1013:
1.155 nicm 1014: keys = options_get_number(c->session->options, "status-keys");
1015: if (keys == MODEKEY_VI) {
1016: switch (status_prompt_translate_key(c, key, &key)) {
1017: case 1:
1018: goto process_key;
1019: case 2:
1020: goto append_key;
1021: default:
1022: return (0);
1023: }
1024: }
1025:
1026: process_key:
1027: switch (key) {
1028: case KEYC_LEFT:
1029: case '\002': /* C-b */
1.1 nicm 1030: if (c->prompt_index > 0) {
1031: c->prompt_index--;
1.158 nicm 1032: break;
1.1 nicm 1033: }
1034: break;
1.155 nicm 1035: case KEYC_RIGHT:
1036: case '\006': /* C-f */
1.1 nicm 1037: if (c->prompt_index < size) {
1038: c->prompt_index++;
1.158 nicm 1039: break;
1.1 nicm 1040: }
1041: break;
1.155 nicm 1042: case KEYC_HOME:
1043: case '\001': /* C-a */
1.1 nicm 1044: if (c->prompt_index != 0) {
1045: c->prompt_index = 0;
1.158 nicm 1046: break;
1.1 nicm 1047: }
1048: break;
1.155 nicm 1049: case KEYC_END:
1050: case '\005': /* C-e */
1.1 nicm 1051: if (c->prompt_index != size) {
1052: c->prompt_index = size;
1.158 nicm 1053: break;
1.1 nicm 1054: }
1055: break;
1.155 nicm 1056: case '\011': /* Tab */
1.152 nicm 1057: if (c->prompt_buffer[0].size == 0)
1.1 nicm 1058: break;
1059:
1060: idx = c->prompt_index;
1061: if (idx != 0)
1062: idx--;
1063:
1064: /* Find the word we are in. */
1.152 nicm 1065: first = &c->prompt_buffer[idx];
1066: while (first > c->prompt_buffer && !status_prompt_space(first))
1.1 nicm 1067: first--;
1.152 nicm 1068: while (first->size != 0 && status_prompt_space(first))
1.1 nicm 1069: first++;
1.152 nicm 1070: last = &c->prompt_buffer[idx];
1071: while (last->size != 0 && !status_prompt_space(last))
1.1 nicm 1072: last++;
1.152 nicm 1073: while (last > c->prompt_buffer && status_prompt_space(last))
1.1 nicm 1074: last--;
1.152 nicm 1075: if (last->size != 0)
1.1 nicm 1076: last++;
1.152 nicm 1077: if (last <= first)
1078: break;
1079:
1080: used = 0;
1081: for (ud = first; ud < last; ud++) {
1082: if (used + ud->size >= sizeof word)
1083: break;
1084: memcpy(word + used, ud->data, ud->size);
1085: used += ud->size;
1086: }
1087: if (ud != last)
1.1 nicm 1088: break;
1.152 nicm 1089: word[used] = '\0';
1.1 nicm 1090:
1091: /* And try to complete it. */
1.152 nicm 1092: if ((s = status_prompt_complete(c->session, word)) == NULL)
1.1 nicm 1093: break;
1094:
1095: /* Trim out word. */
1096: n = size - (last - c->prompt_buffer) + 1; /* with \0 */
1.152 nicm 1097: memmove(first, last, n * sizeof *c->prompt_buffer);
1.1 nicm 1098: size -= last - first;
1099:
1100: /* Insert the new word. */
1.55 nicm 1101: size += strlen(s);
1.1 nicm 1102: off = first - c->prompt_buffer;
1.152 nicm 1103: c->prompt_buffer = xreallocarray(c->prompt_buffer, size + 1,
1104: sizeof *c->prompt_buffer);
1.1 nicm 1105: first = c->prompt_buffer + off;
1.152 nicm 1106: memmove(first + strlen(s), first, n * sizeof *c->prompt_buffer);
1107: for (idx = 0; idx < strlen(s); idx++)
1108: utf8_set(&first[idx], s[idx]);
1.1 nicm 1109:
1110: c->prompt_index = (first - c->prompt_buffer) + strlen(s);
1.94 nicm 1111: free(s);
1.1 nicm 1112:
1.158 nicm 1113: goto changed;
1.155 nicm 1114: case KEYC_BSPACE:
1115: case '\010': /* C-h */
1.1 nicm 1116: if (c->prompt_index != 0) {
1117: if (c->prompt_index == size)
1.152 nicm 1118: c->prompt_buffer[--c->prompt_index].size = 0;
1.1 nicm 1119: else {
1120: memmove(c->prompt_buffer + c->prompt_index - 1,
1121: c->prompt_buffer + c->prompt_index,
1.152 nicm 1122: (size + 1 - c->prompt_index) *
1123: sizeof *c->prompt_buffer);
1.1 nicm 1124: c->prompt_index--;
1125: }
1.158 nicm 1126: goto changed;
1.1 nicm 1127: }
1128: break;
1.155 nicm 1129: case KEYC_DC:
1130: case '\004': /* C-d */
1.1 nicm 1131: if (c->prompt_index != size) {
1132: memmove(c->prompt_buffer + c->prompt_index,
1133: c->prompt_buffer + c->prompt_index + 1,
1.152 nicm 1134: (size + 1 - c->prompt_index) *
1135: sizeof *c->prompt_buffer);
1.158 nicm 1136: goto changed;
1.18 nicm 1137: }
1.26 nicm 1138: break;
1.155 nicm 1139: case '\025': /* C-u */
1.152 nicm 1140: c->prompt_buffer[0].size = 0;
1.26 nicm 1141: c->prompt_index = 0;
1.158 nicm 1142: goto changed;
1.155 nicm 1143: case '\013': /* C-k */
1.18 nicm 1144: if (c->prompt_index < size) {
1.152 nicm 1145: c->prompt_buffer[c->prompt_index].size = 0;
1.158 nicm 1146: goto changed;
1.1 nicm 1147: }
1148: break;
1.155 nicm 1149: case '\027': /* C-w */
1.152 nicm 1150: ws = options_get_string(oo, "word-separators");
1.81 nicm 1151: idx = c->prompt_index;
1152:
1153: /* Find a non-separator. */
1154: while (idx != 0) {
1155: idx--;
1.152 nicm 1156: if (!status_prompt_in_list(ws, &c->prompt_buffer[idx]))
1.81 nicm 1157: break;
1158: }
1159:
1160: /* Find the separator at the beginning of the word. */
1161: while (idx != 0) {
1162: idx--;
1.152 nicm 1163: if (status_prompt_in_list(ws, &c->prompt_buffer[idx])) {
1.81 nicm 1164: /* Go back to the word. */
1165: idx++;
1166: break;
1167: }
1168: }
1169:
1170: memmove(c->prompt_buffer + idx,
1171: c->prompt_buffer + c->prompt_index,
1.152 nicm 1172: (size + 1 - c->prompt_index) *
1173: sizeof *c->prompt_buffer);
1.81 nicm 1174: memset(c->prompt_buffer + size - (c->prompt_index - idx),
1.152 nicm 1175: '\0', (c->prompt_index - idx) * sizeof *c->prompt_buffer);
1.81 nicm 1176: c->prompt_index = idx;
1.152 nicm 1177:
1.158 nicm 1178: goto changed;
1.155 nicm 1179: case 'f'|KEYC_ESCAPE:
1180: ws = options_get_string(oo, "word-separators");
1.81 nicm 1181:
1182: /* Find a word. */
1183: while (c->prompt_index != size) {
1.152 nicm 1184: idx = ++c->prompt_index;
1185: if (!status_prompt_in_list(ws, &c->prompt_buffer[idx]))
1.81 nicm 1186: break;
1187: }
1188:
1189: /* Find the separator at the end of the word. */
1190: while (c->prompt_index != size) {
1.152 nicm 1191: idx = ++c->prompt_index;
1192: if (status_prompt_in_list(ws, &c->prompt_buffer[idx]))
1.81 nicm 1193: break;
1194: }
1.106 nicm 1195:
1196: /* Back up to the end-of-word like vi. */
1197: if (options_get_number(oo, "status-keys") == MODEKEY_VI &&
1198: c->prompt_index != 0)
1199: c->prompt_index--;
1.81 nicm 1200:
1.158 nicm 1201: goto changed;
1.155 nicm 1202: case 'b'|KEYC_ESCAPE:
1203: ws = options_get_string(oo, "word-separators");
1.81 nicm 1204:
1205: /* Find a non-separator. */
1206: while (c->prompt_index != 0) {
1.152 nicm 1207: idx = --c->prompt_index;
1208: if (!status_prompt_in_list(ws, &c->prompt_buffer[idx]))
1.81 nicm 1209: break;
1210: }
1211:
1212: /* Find the separator at the beginning of the word. */
1213: while (c->prompt_index != 0) {
1.152 nicm 1214: idx = --c->prompt_index;
1215: if (status_prompt_in_list(ws, &c->prompt_buffer[idx])) {
1.81 nicm 1216: /* Go back to the word. */
1217: c->prompt_index++;
1218: break;
1219: }
1220: }
1.158 nicm 1221: goto changed;
1.155 nicm 1222: case KEYC_UP:
1223: case '\020': /* C-p */
1.66 nicm 1224: histstr = status_prompt_up_history(&c->prompt_hindex);
1225: if (histstr == NULL)
1.1 nicm 1226: break;
1.94 nicm 1227: free(c->prompt_buffer);
1.152 nicm 1228: c->prompt_buffer = utf8_fromcstr(histstr);
1229: c->prompt_index = utf8_strlen(c->prompt_buffer);
1.158 nicm 1230: goto changed;
1.155 nicm 1231: case KEYC_DOWN:
1232: case '\016': /* C-n */
1.66 nicm 1233: histstr = status_prompt_down_history(&c->prompt_hindex);
1234: if (histstr == NULL)
1.65 nicm 1235: break;
1.94 nicm 1236: free(c->prompt_buffer);
1.152 nicm 1237: c->prompt_buffer = utf8_fromcstr(histstr);
1238: c->prompt_index = utf8_strlen(c->prompt_buffer);
1.158 nicm 1239: goto changed;
1.155 nicm 1240: case '\031': /* C-y */
1.134 nicm 1241: if ((pb = paste_get_top(NULL)) == NULL)
1.1 nicm 1242: break;
1.134 nicm 1243: bufdata = paste_buffer_data(pb, &bufsize);
1244: for (n = 0; n < bufsize; n++) {
1245: ch = (u_char)bufdata[n];
1.152 nicm 1246: if (ch < 32 || ch >= 127)
1.32 nicm 1247: break;
1248: }
1.1 nicm 1249:
1.152 nicm 1250: c->prompt_buffer = xreallocarray(c->prompt_buffer, size + n + 1,
1251: sizeof *c->prompt_buffer);
1.1 nicm 1252: if (c->prompt_index == size) {
1.152 nicm 1253: for (idx = 0; idx < n; idx++) {
1254: ud = &c->prompt_buffer[c->prompt_index + idx];
1255: utf8_set(ud, bufdata[idx]);
1256: }
1.1 nicm 1257: c->prompt_index += n;
1.152 nicm 1258: c->prompt_buffer[c->prompt_index].size = 0;
1.1 nicm 1259: } else {
1260: memmove(c->prompt_buffer + c->prompt_index + n,
1261: c->prompt_buffer + c->prompt_index,
1.152 nicm 1262: (size + 1 - c->prompt_index) *
1263: sizeof *c->prompt_buffer);
1264: for (idx = 0; idx < n; idx++) {
1265: ud = &c->prompt_buffer[c->prompt_index + idx];
1266: utf8_set(ud, bufdata[idx]);
1267: }
1.1 nicm 1268: c->prompt_index += n;
1269: }
1.158 nicm 1270: goto changed;
1.155 nicm 1271: case '\024': /* C-t */
1.30 nicm 1272: idx = c->prompt_index;
1273: if (idx < size)
1274: idx++;
1275: if (idx >= 2) {
1.152 nicm 1276: utf8_copy(&tmp, &c->prompt_buffer[idx - 2]);
1277: utf8_copy(&c->prompt_buffer[idx - 2],
1278: &c->prompt_buffer[idx - 1]);
1279: utf8_copy(&c->prompt_buffer[idx - 1], &tmp);
1.30 nicm 1280: c->prompt_index = idx;
1.158 nicm 1281: goto changed;
1.30 nicm 1282: }
1.1 nicm 1283: break;
1.155 nicm 1284: case '\r':
1285: case '\n':
1.152 nicm 1286: s = utf8_tocstr(c->prompt_buffer);
1287: if (*s != '\0')
1288: status_prompt_add_history(s);
1.158 nicm 1289: if (c->prompt_callbackfn(c->prompt_data, s, 1) == 0)
1.25 nicm 1290: status_prompt_clear(c);
1.152 nicm 1291: free(s);
1.25 nicm 1292: break;
1.155 nicm 1293: case '\033': /* Escape */
1294: case '\003': /* C-c */
1.158 nicm 1295: if (c->prompt_callbackfn(c->prompt_data, NULL, 1) == 0)
1.1 nicm 1296: status_prompt_clear(c);
1297: break;
1.158 nicm 1298: case '\022': /* C-r */
1299: if (c->prompt_flags & PROMPT_INCREMENTAL) {
1300: prefix = '-';
1301: goto changed;
1302: }
1303: break;
1304: case '\023': /* C-s */
1305: if (c->prompt_flags & PROMPT_INCREMENTAL) {
1306: prefix = '+';
1307: goto changed;
1308: }
1309: break;
1310: default:
1311: goto append_key;
1.154 nicm 1312: }
1.152 nicm 1313:
1.158 nicm 1314: c->flags |= CLIENT_STATUS;
1315: return (0);
1316:
1.154 nicm 1317: append_key:
1318: if (key <= 0x1f || key >= KEYC_BASE)
1319: return (0);
1320: if (utf8_split(key, &tmp) != UTF8_DONE)
1321: return (0);
1.1 nicm 1322:
1.154 nicm 1323: c->prompt_buffer = xreallocarray(c->prompt_buffer, size + 2,
1324: sizeof *c->prompt_buffer);
1.1 nicm 1325:
1.154 nicm 1326: if (c->prompt_index == size) {
1327: utf8_copy(&c->prompt_buffer[c->prompt_index], &tmp);
1328: c->prompt_index++;
1329: c->prompt_buffer[c->prompt_index].size = 0;
1330: } else {
1331: memmove(c->prompt_buffer + c->prompt_index + 1,
1332: c->prompt_buffer + c->prompt_index,
1333: (size + 1 - c->prompt_index) *
1334: sizeof *c->prompt_buffer);
1335: utf8_copy(&c->prompt_buffer[c->prompt_index], &tmp);
1336: c->prompt_index++;
1337: }
1.1 nicm 1338:
1.154 nicm 1339: if (c->prompt_flags & PROMPT_SINGLE) {
1340: s = utf8_tocstr(c->prompt_buffer);
1341: if (strlen(s) != 1)
1342: status_prompt_clear(c);
1.158 nicm 1343: else if (c->prompt_callbackfn(c->prompt_data, s, 1) == 0)
1.154 nicm 1344: status_prompt_clear(c);
1345: free(s);
1.1 nicm 1346: }
1.154 nicm 1347:
1.158 nicm 1348: changed:
1.154 nicm 1349: c->flags |= CLIENT_STATUS;
1.158 nicm 1350: if (c->prompt_flags & PROMPT_INCREMENTAL) {
1351: s = utf8_tocstr(c->prompt_buffer);
1352: xasprintf(&cp, "%c%s", prefix, s);
1353: c->prompt_callbackfn(c->prompt_data, cp, 0);
1354: free(cp);
1355: free(s);
1356: }
1.154 nicm 1357: return (0);
1.1 nicm 1358: }
1359:
1.65 nicm 1360: /* Get previous line from the history. */
1.151 nicm 1361: static const char *
1.65 nicm 1362: status_prompt_up_history(u_int *idx)
1363: {
1364: /*
1.128 nicm 1365: * History runs from 0 to size - 1. Index is from 0 to size. Zero is
1366: * empty.
1.65 nicm 1367: */
1368:
1.128 nicm 1369: if (status_prompt_hsize == 0 || *idx == status_prompt_hsize)
1.65 nicm 1370: return (NULL);
1371: (*idx)++;
1.128 nicm 1372: return (status_prompt_hlist[status_prompt_hsize - *idx]);
1.65 nicm 1373: }
1374:
1375: /* Get next line from the history. */
1.151 nicm 1376: static const char *
1.65 nicm 1377: status_prompt_down_history(u_int *idx)
1378: {
1.128 nicm 1379: if (status_prompt_hsize == 0 || *idx == 0)
1.65 nicm 1380: return ("");
1381: (*idx)--;
1382: if (*idx == 0)
1383: return ("");
1.128 nicm 1384: return (status_prompt_hlist[status_prompt_hsize - *idx]);
1.65 nicm 1385: }
1386:
1.1 nicm 1387: /* Add line to the history. */
1.151 nicm 1388: static void
1.65 nicm 1389: status_prompt_add_history(const char *line)
1.1 nicm 1390: {
1.128 nicm 1391: size_t size;
1.65 nicm 1392:
1.128 nicm 1393: if (status_prompt_hsize > 0 &&
1394: strcmp(status_prompt_hlist[status_prompt_hsize - 1], line) == 0)
1.1 nicm 1395: return;
1396:
1.128 nicm 1397: if (status_prompt_hsize == PROMPT_HISTORY) {
1398: free(status_prompt_hlist[0]);
1.1 nicm 1399:
1.128 nicm 1400: size = (PROMPT_HISTORY - 1) * sizeof *status_prompt_hlist;
1401: memmove(&status_prompt_hlist[0], &status_prompt_hlist[1], size);
1.1 nicm 1402:
1.128 nicm 1403: status_prompt_hlist[status_prompt_hsize - 1] = xstrdup(line);
1404: return;
1405: }
1.1 nicm 1406:
1.128 nicm 1407: status_prompt_hlist = xreallocarray(status_prompt_hlist,
1408: status_prompt_hsize + 1, sizeof *status_prompt_hlist);
1409: status_prompt_hlist[status_prompt_hsize++] = xstrdup(line);
1410: }
1411:
1412: /* Build completion list. */
1.151 nicm 1413: static const char **
1.128 nicm 1414: status_prompt_complete_list(u_int *size, const char *s)
1415: {
1416: const char **list = NULL, **layout;
1417: const struct cmd_entry **cmdent;
1418: const struct options_table_entry *oe;
1419: const char *layouts[] = {
1420: "even-horizontal", "even-vertical", "main-horizontal",
1421: "main-vertical", "tiled", NULL
1422: };
1.1 nicm 1423:
1.128 nicm 1424: *size = 0;
1.1 nicm 1425: for (cmdent = cmd_table; *cmdent != NULL; cmdent++) {
1.128 nicm 1426: if (strncmp((*cmdent)->name, s, strlen(s)) == 0) {
1427: list = xreallocarray(list, (*size) + 1, sizeof *list);
1428: list[(*size)++] = (*cmdent)->name;
1429: }
1.56 nicm 1430: }
1.142 nicm 1431: for (oe = options_table; oe->name != NULL; oe++) {
1.128 nicm 1432: if (strncmp(oe->name, s, strlen(s)) == 0) {
1433: list = xreallocarray(list, (*size) + 1, sizeof *list);
1434: list[(*size)++] = oe->name;
1435: }
1436: }
1437: for (layout = layouts; *layout != NULL; layout++) {
1438: if (strncmp(*layout, s, strlen(s)) == 0) {
1439: list = xreallocarray(list, (*size) + 1, sizeof *list);
1440: list[(*size)++] = *layout;
1441: }
1442: }
1443: return (list);
1444: }
1445:
1446: /* Find longest prefix. */
1.151 nicm 1447: static char *
1.128 nicm 1448: status_prompt_complete_prefix(const char **list, u_int size)
1449: {
1450: char *out;
1451: u_int i;
1452: size_t j;
1453:
1454: out = xstrdup(list[0]);
1455: for (i = 1; i < size; i++) {
1456: j = strlen(list[i]);
1457: if (j > strlen(out))
1458: j = strlen(out);
1459: for (; j > 0; j--) {
1460: if (out[j - 1] != list[i][j - 1])
1461: out[j - 1] = '\0';
1462: }
1.1 nicm 1463: }
1.128 nicm 1464: return (out);
1465: }
1.1 nicm 1466:
1.128 nicm 1467: /* Complete word. */
1.151 nicm 1468: static char *
1.152 nicm 1469: status_prompt_complete(struct session *session, const char *s)
1.128 nicm 1470: {
1471: const char **list = NULL, *colon;
1472: u_int size = 0, i;
1473: struct session *s_loop;
1474: struct winlink *wl;
1475: struct window *w;
1476: char *copy, *out, *tmp;
1477:
1478: if (*s == '\0')
1.1 nicm 1479: return (NULL);
1.128 nicm 1480: out = NULL;
1481:
1482: if (strncmp(s, "-t", 2) != 0 && strncmp(s, "-s", 2) != 0) {
1483: list = status_prompt_complete_list(&size, s);
1484: if (size == 0)
1485: out = NULL;
1486: else if (size == 1)
1487: xasprintf(&out, "%s ", list[0]);
1488: else
1489: out = status_prompt_complete_prefix(list, size);
1490: free(list);
1491: return (out);
1492: }
1493: copy = xstrdup(s);
1494:
1495: colon = ":";
1496: if (copy[strlen(copy) - 1] == ':')
1497: copy[strlen(copy) - 1] = '\0';
1498: else
1499: colon = "";
1500: s = copy + 2;
1.1 nicm 1501:
1.128 nicm 1502: RB_FOREACH(s_loop, sessions, &sessions) {
1503: if (strncmp(s_loop->name, s, strlen(s)) == 0) {
1504: list = xreallocarray(list, size + 2, sizeof *list);
1505: list[size++] = s_loop->name;
1506: }
1507: }
1508: if (size == 1) {
1509: out = xstrdup(list[0]);
1510: if (session_find(list[0]) != NULL)
1511: colon = ":";
1512: } else if (size != 0)
1513: out = status_prompt_complete_prefix(list, size);
1514: if (out != NULL) {
1515: xasprintf(&tmp, "-%c%s%s", copy[1], out, colon);
1516: out = tmp;
1517: goto found;
1518: }
1519:
1520: colon = "";
1521: if (*s == ':') {
1.152 nicm 1522: RB_FOREACH(wl, winlinks, &session->windows) {
1.128 nicm 1523: xasprintf(&tmp, ":%s", wl->window->name);
1524: if (strncmp(tmp, s, strlen(s)) == 0){
1525: list = xreallocarray(list, size + 1,
1526: sizeof *list);
1527: list[size++] = tmp;
1528: continue;
1529: }
1530: free(tmp);
1.1 nicm 1531:
1.128 nicm 1532: xasprintf(&tmp, ":%d", wl->idx);
1533: if (strncmp(tmp, s, strlen(s)) == 0) {
1534: list = xreallocarray(list, size + 1,
1535: sizeof *list);
1536: list[size++] = tmp;
1537: continue;
1538: }
1539: free(tmp);
1540: }
1541: } else {
1542: RB_FOREACH(s_loop, sessions, &sessions) {
1543: RB_FOREACH(wl, winlinks, &s_loop->windows) {
1544: w = wl->window;
1545:
1546: xasprintf(&tmp, "%s:%s", s_loop->name, w->name);
1547: if (strncmp(tmp, s, strlen(s)) == 0) {
1548: list = xreallocarray(list, size + 1,
1549: sizeof *list);
1550: list[size++] = tmp;
1551: continue;
1552: }
1553: free(tmp);
1554:
1555: xasprintf(&tmp, "%s:%d", s_loop->name, wl->idx);
1556: if (strncmp(tmp, s, strlen(s)) == 0) {
1557: list = xreallocarray(list, size + 1,
1558: sizeof *list);
1559: list[size++] = tmp;
1560: continue;
1561: }
1562: free(tmp);
1563: }
1.1 nicm 1564: }
1565: }
1.128 nicm 1566: if (size == 1) {
1567: out = xstrdup(list[0]);
1568: colon = " ";
1569: } else if (size != 0)
1570: out = status_prompt_complete_prefix(list, size);
1571: if (out != NULL) {
1572: xasprintf(&tmp, "-%c%s%s", copy[1], out, colon);
1573: out = tmp;
1574: }
1575:
1576: for (i = 0; i < size; i++)
1577: free((void *)list[i]);
1578:
1579: found:
1580: free(copy);
1581: free(list);
1582: return (out);
1.1 nicm 1583: }