Annotation of src/usr.bin/tmux/status.c, Revision 1.166
1.166 ! nicm 1: /* $OpenBSD: status.c,v 1.165 2017/05/03 05:53:34 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.164 nicm 522: ft = format_create(c, NULL, tag, FORMAT_STATUS|FORMAT_FORCE);
1.135 nicm 523: else
1.164 nicm 524: ft = format_create(c, 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.162 nicm 565: struct timeval tv;
566: va_list ap;
567: int delay;
1.1 nicm 568:
1.12 nicm 569: status_message_clear(c);
570:
1.28 nicm 571: va_start(ap, fmt);
572: xvasprintf(&c->message_string, fmt, ap);
573: va_end(ap);
574:
1.162 nicm 575: server_client_add_message(c, "%s", c->message_string);
1.44 nicm 576:
1.137 nicm 577: delay = options_get_number(c->session->options, "display-time");
1.143 tim 578: if (delay > 0) {
579: tv.tv_sec = delay / 1000;
580: tv.tv_usec = (delay % 1000) * 1000L;
581:
582: if (event_initialized(&c->message_timer))
583: evtimer_del(&c->message_timer);
584: evtimer_set(&c->message_timer, status_message_callback, c);
585: evtimer_add(&c->message_timer, &tv);
586: }
1.1 nicm 587:
588: c->tty.flags |= (TTY_NOCURSOR|TTY_FREEZE);
589: c->flags |= CLIENT_STATUS;
590: }
591:
1.46 nicm 592: /* Clear status line message. */
1.1 nicm 593: void
594: status_message_clear(struct client *c)
595: {
596: if (c->message_string == NULL)
597: return;
598:
1.94 nicm 599: free(c->message_string);
1.1 nicm 600: c->message_string = NULL;
601:
1.156 nicm 602: if (c->prompt_string == NULL)
603: c->tty.flags &= ~(TTY_NOCURSOR|TTY_FREEZE);
1.12 nicm 604: c->flags |= CLIENT_REDRAW; /* screen was frozen and may have changed */
1.7 nicm 605:
606: screen_reinit(&c->status);
1.42 nicm 607: }
608:
1.46 nicm 609: /* Clear status line message after timer expires. */
1.151 nicm 610: static void
1.141 nicm 611: status_message_callback(__unused int fd, __unused short event, void *data)
1.42 nicm 612: {
613: struct client *c = data;
614:
615: status_message_clear(c);
1.1 nicm 616: }
617:
618: /* Draw client message on status line of present else on last line. */
619: int
620: status_message_redraw(struct client *c)
621: {
622: struct screen_write_ctx ctx;
623: struct session *s = c->session;
624: struct screen old_status;
625: size_t len;
626: struct grid_cell gc;
627:
628: if (c->tty.sx == 0 || c->tty.sy == 0)
629: return (0);
630: memcpy(&old_status, &c->status, sizeof old_status);
631: screen_init(&c->status, c->tty.sx, 1, 0);
632:
1.139 nicm 633: len = screen_write_strlen("%s", c->message_string);
1.1 nicm 634: if (len > c->tty.sx)
635: len = c->tty.sx;
636:
1.137 nicm 637: style_apply(&gc, s->options, "message-style");
1.1 nicm 638:
639: screen_write_start(&ctx, NULL, &c->status);
640:
641: screen_write_cursormove(&ctx, 0, 0);
1.139 nicm 642: screen_write_nputs(&ctx, len, &gc, "%s", c->message_string);
1.1 nicm 643: for (; len < c->tty.sx; len++)
644: screen_write_putc(&ctx, &gc, ' ');
645:
646: screen_write_stop(&ctx);
647:
648: if (grid_compare(c->status.grid, old_status.grid) == 0) {
649: screen_free(&old_status);
650: return (0);
651: }
652: screen_free(&old_status);
653: return (1);
654: }
655:
1.46 nicm 656: /* Enable status line prompt. */
1.1 nicm 657: void
1.76 nicm 658: status_prompt_set(struct client *c, const char *msg, const char *input,
1.166 ! nicm 659: prompt_input_cb inputcb, prompt_free_cb freecb, void *data, int flags)
1.1 nicm 660: {
1.122 nicm 661: struct format_tree *ft;
662: time_t t;
1.165 nicm 663: char *tmp, *cp;
1.122 nicm 664:
1.164 nicm 665: ft = format_create(c, NULL, FORMAT_NONE, 0);
1.122 nicm 666: format_defaults(ft, c, NULL, NULL, NULL);
1.152 nicm 667:
1.122 nicm 668: t = time(NULL);
1.152 nicm 669: tmp = format_expand_time(ft, input, t);
1.20 nicm 670:
1.12 nicm 671: status_message_clear(c);
672: status_prompt_clear(c);
673:
1.124 nicm 674: c->prompt_string = format_expand_time(ft, msg, t);
1.1 nicm 675:
1.152 nicm 676: c->prompt_buffer = utf8_fromcstr(tmp);
677: c->prompt_index = utf8_strlen(c->prompt_buffer);
1.1 nicm 678:
1.166 ! nicm 679: c->prompt_inputcb = inputcb;
! 680: c->prompt_freecb = freecb;
1.1 nicm 681: c->prompt_data = data;
682:
683: c->prompt_hindex = 0;
684:
685: c->prompt_flags = flags;
1.155 nicm 686: c->prompt_mode = PROMPT_ENTRY;
1.1 nicm 687:
1.158 nicm 688: if (~flags & PROMPT_INCREMENTAL)
689: c->tty.flags |= (TTY_NOCURSOR|TTY_FREEZE);
1.1 nicm 690: c->flags |= CLIENT_STATUS;
1.165 nicm 691:
692: if ((flags & PROMPT_INCREMENTAL) && *tmp != '\0') {
693: xasprintf(&cp, "=%s", tmp);
1.166 ! nicm 694: c->prompt_inputcb(c, c->prompt_data, cp, 0);
1.165 nicm 695: free(cp);
696: }
1.122 nicm 697:
1.152 nicm 698: free(tmp);
1.122 nicm 699: format_free(ft);
1.1 nicm 700: }
701:
1.46 nicm 702: /* Remove status line prompt. */
1.1 nicm 703: void
704: status_prompt_clear(struct client *c)
705: {
1.55 nicm 706: if (c->prompt_string == NULL)
1.1 nicm 707: return;
708:
1.166 ! nicm 709: if (c->prompt_freecb != NULL && c->prompt_data != NULL)
! 710: c->prompt_freecb(c->prompt_data);
1.1 nicm 711:
1.94 nicm 712: free(c->prompt_string);
1.1 nicm 713: c->prompt_string = NULL;
714:
1.94 nicm 715: free(c->prompt_buffer);
1.1 nicm 716: c->prompt_buffer = NULL;
717:
718: c->tty.flags &= ~(TTY_NOCURSOR|TTY_FREEZE);
1.12 nicm 719: c->flags |= CLIENT_REDRAW; /* screen was frozen and may have changed */
1.7 nicm 720:
721: screen_reinit(&c->status);
1.27 nicm 722: }
723:
1.46 nicm 724: /* Update status line prompt with a new prompt string. */
1.27 nicm 725: void
1.76 nicm 726: status_prompt_update(struct client *c, const char *msg, const char *input)
1.27 nicm 727: {
1.122 nicm 728: struct format_tree *ft;
729: time_t t;
1.152 nicm 730: char *tmp;
1.122 nicm 731:
1.164 nicm 732: ft = format_create(c, NULL, FORMAT_NONE, 0);
1.122 nicm 733: format_defaults(ft, c, NULL, NULL, NULL);
1.152 nicm 734:
1.122 nicm 735: t = time(NULL);
1.152 nicm 736: tmp = format_expand_time(ft, input, t);
1.122 nicm 737:
1.94 nicm 738: free(c->prompt_string);
1.124 nicm 739: c->prompt_string = format_expand_time(ft, msg, t);
1.27 nicm 740:
1.94 nicm 741: free(c->prompt_buffer);
1.152 nicm 742: c->prompt_buffer = utf8_fromcstr(tmp);
743: c->prompt_index = utf8_strlen(c->prompt_buffer);
1.27 nicm 744:
745: c->prompt_hindex = 0;
746:
747: c->flags |= CLIENT_STATUS;
1.122 nicm 748:
1.152 nicm 749: free(tmp);
1.122 nicm 750: format_free(ft);
1.1 nicm 751: }
752:
753: /* Draw client prompt on status line of present else on last line. */
754: int
755: status_prompt_redraw(struct client *c)
756: {
1.152 nicm 757: struct screen_write_ctx ctx;
758: struct session *s = c->session;
759: struct screen old_status;
760: u_int i, offset, left, start, pcursor, pwidth, width;
761: struct grid_cell gc, cursorgc;
1.1 nicm 762:
763: if (c->tty.sx == 0 || c->tty.sy == 0)
764: return (0);
765: memcpy(&old_status, &c->status, sizeof old_status);
766: screen_init(&c->status, c->tty.sx, 1, 0);
767:
1.155 nicm 768: if (c->prompt_mode == PROMPT_COMMAND)
1.137 nicm 769: style_apply(&gc, s->options, "message-command-style");
1.108 nicm 770: else
1.137 nicm 771: style_apply(&gc, s->options, "message-style");
1.1 nicm 772:
1.152 nicm 773: memcpy(&cursorgc, &gc, sizeof cursorgc);
774: cursorgc.attr ^= GRID_ATTR_REVERSE;
775:
776: start = screen_write_strlen("%s", c->prompt_string);
777: if (start > c->tty.sx)
778: start = c->tty.sx;
779:
1.1 nicm 780: screen_write_start(&ctx, NULL, &c->status);
781: screen_write_cursormove(&ctx, 0, 0);
1.152 nicm 782: screen_write_nputs(&ctx, start, &gc, "%s", c->prompt_string);
783: while (c->status.cx < screen_size_x(&c->status))
784: screen_write_putc(&ctx, &gc, ' ');
785: screen_write_cursormove(&ctx, start, 0);
1.1 nicm 786:
1.152 nicm 787: left = c->tty.sx - start;
788: if (left == 0)
789: goto finished;
790:
791: pcursor = utf8_strwidth(c->prompt_buffer, c->prompt_index);
792: pwidth = utf8_strwidth(c->prompt_buffer, -1);
793: if (pcursor >= left) {
794: /*
795: * The cursor would be outside the screen so start drawing
796: * with it on the right.
797: */
798: offset = (pcursor - left) + 1;
799: pwidth = left;
800: } else
801: offset = 0;
802: if (pwidth > left)
803: pwidth = left;
804:
805: width = 0;
806: for (i = 0; c->prompt_buffer[i].size != 0; i++) {
807: if (width < offset) {
808: width += c->prompt_buffer[i].width;
809: continue;
1.1 nicm 810: }
1.152 nicm 811: if (width >= offset + pwidth)
812: break;
813: width += c->prompt_buffer[i].width;
814: if (width > offset + pwidth)
815: break;
1.1 nicm 816:
1.152 nicm 817: if (i != c->prompt_index) {
818: utf8_copy(&gc.data, &c->prompt_buffer[i]);
819: screen_write_cell(&ctx, &gc);
820: } else {
821: utf8_copy(&cursorgc.data, &c->prompt_buffer[i]);
822: screen_write_cell(&ctx, &cursorgc);
823: }
1.1 nicm 824: }
1.152 nicm 825: if (c->status.cx < screen_size_x(&c->status) && c->prompt_index >= i)
826: screen_write_putc(&ctx, &cursorgc, ' ');
1.1 nicm 827:
1.152 nicm 828: finished:
1.1 nicm 829: screen_write_stop(&ctx);
1.51 nicm 830:
1.1 nicm 831: if (grid_compare(c->status.grid, old_status.grid) == 0) {
832: screen_free(&old_status);
833: return (0);
834: }
835: screen_free(&old_status);
836: return (1);
837: }
838:
1.152 nicm 839: /* Is this a separator? */
840: static int
841: status_prompt_in_list(const char *ws, const struct utf8_data *ud)
842: {
843: if (ud->size != 1 || ud->width != 1)
844: return (0);
845: return (strchr(ws, *ud->data) != NULL);
846: }
847:
848: /* Is this a space? */
849: static int
850: status_prompt_space(const struct utf8_data *ud)
851: {
852: if (ud->size != 1 || ud->width != 1)
853: return (0);
854: return (*ud->data == ' ');
855: }
856:
1.155 nicm 857: /*
858: * Translate key from emacs to vi. Return 0 to drop key, 1 to process the key
859: * as an emacs key; return 2 to append to the buffer.
860: */
861: static int
862: status_prompt_translate_key(struct client *c, key_code key, key_code *new_key)
863: {
864: if (c->prompt_mode == PROMPT_ENTRY) {
865: switch (key) {
866: case '\003': /* C-c */
867: case '\010': /* C-h */
868: case '\011': /* Tab */
869: case '\025': /* C-u */
870: case '\027': /* C-w */
871: case '\n':
872: case '\r':
873: case KEYC_BSPACE:
874: case KEYC_DC:
875: case KEYC_DOWN:
876: case KEYC_END:
877: case KEYC_HOME:
878: case KEYC_LEFT:
879: case KEYC_RIGHT:
880: case KEYC_UP:
881: *new_key = key;
882: return (1);
883: case '\033': /* Escape */
884: c->prompt_mode = PROMPT_COMMAND;
885: c->flags |= CLIENT_STATUS;
886: return (0);
887: }
888: *new_key = key;
889: return (2);
890: }
891:
892: switch (key) {
893: case 'A':
894: case 'I':
895: case 'C':
896: case 's':
897: case 'a':
898: c->prompt_mode = PROMPT_ENTRY;
899: c->flags |= CLIENT_STATUS;
900: break; /* switch mode and... */
901: case 'S':
902: c->prompt_mode = PROMPT_ENTRY;
903: c->flags |= CLIENT_STATUS;
904: *new_key = '\025'; /* C-u */
905: return (1);
906: case 'i':
907: case '\033': /* Escape */
908: c->prompt_mode = PROMPT_ENTRY;
909: c->flags |= CLIENT_STATUS;
910: return (0);
911: }
912:
913: switch (key) {
914: case 'A':
915: case '$':
916: *new_key = KEYC_END;
917: return (1);
918: case 'I':
919: case '0':
920: case '^':
921: *new_key = KEYC_HOME;
922: return (1);
923: case 'C':
924: case 'D':
925: *new_key = '\013'; /* C-k */
926: return (1);
927: case KEYC_BSPACE:
928: case 'X':
929: *new_key = KEYC_BSPACE;
930: return (1);
931: case 'b':
932: case 'B':
933: *new_key = 'b'|KEYC_ESCAPE;
934: return (1);
935: case 'd':
936: *new_key = '\025';
937: return (1);
938: case 'e':
939: case 'E':
940: case 'w':
941: case 'W':
942: *new_key = 'f'|KEYC_ESCAPE;
943: return (1);
944: case 'p':
945: *new_key = '\031'; /* C-y */
946: return (1);
947: case 's':
948: case KEYC_DC:
949: case 'x':
950: *new_key = KEYC_DC;
951: return (1);
952: case KEYC_DOWN:
953: case 'j':
954: *new_key = KEYC_DOWN;
955: return (1);
956: case KEYC_LEFT:
957: case 'h':
958: *new_key = KEYC_LEFT;
959: return (1);
960: case 'a':
961: case KEYC_RIGHT:
962: case 'l':
963: *new_key = KEYC_RIGHT;
964: return (1);
965: case KEYC_UP:
966: case 'k':
967: *new_key = KEYC_UP;
968: return (1);
969: case '\010' /* C-h */:
970: case '\003' /* C-c */:
971: case '\n':
972: case '\r':
973: return (1);
974: }
975: return (0);
976: }
977:
1.1 nicm 978: /* Handle keys in prompt. */
1.154 nicm 979: int
1.138 nicm 980: status_prompt_key(struct client *c, key_code key)
1.1 nicm 981: {
1.152 nicm 982: struct options *oo = c->session->options;
1.1 nicm 983: struct paste_buffer *pb;
1.158 nicm 984: char *s, *cp, word[64], prefix = '=';
1.152 nicm 985: const char *histstr, *bufdata, *ws = NULL;
1.53 nicm 986: u_char ch;
1.152 nicm 987: size_t size, n, off, idx, bufsize, used;
988: struct utf8_data tmp, *first, *last, *ud;
1.155 nicm 989: int keys;
1.1 nicm 990:
1.152 nicm 991: size = utf8_strlen(c->prompt_buffer);
1.154 nicm 992:
993: if (c->prompt_flags & PROMPT_NUMERIC) {
994: if (key >= '0' && key <= '9')
995: goto append_key;
996: s = utf8_tocstr(c->prompt_buffer);
1.166 ! nicm 997: c->prompt_inputcb(c, c->prompt_data, s, 1);
1.154 nicm 998: status_prompt_clear(c);
999: free(s);
1000: return (1);
1001: }
1002:
1.155 nicm 1003: keys = options_get_number(c->session->options, "status-keys");
1004: if (keys == MODEKEY_VI) {
1005: switch (status_prompt_translate_key(c, key, &key)) {
1006: case 1:
1007: goto process_key;
1008: case 2:
1009: goto append_key;
1010: default:
1011: return (0);
1012: }
1013: }
1014:
1015: process_key:
1016: switch (key) {
1017: case KEYC_LEFT:
1018: case '\002': /* C-b */
1.1 nicm 1019: if (c->prompt_index > 0) {
1020: c->prompt_index--;
1.158 nicm 1021: break;
1.1 nicm 1022: }
1023: break;
1.155 nicm 1024: case KEYC_RIGHT:
1025: case '\006': /* C-f */
1.1 nicm 1026: if (c->prompt_index < size) {
1027: c->prompt_index++;
1.158 nicm 1028: break;
1.1 nicm 1029: }
1030: break;
1.155 nicm 1031: case KEYC_HOME:
1032: case '\001': /* C-a */
1.1 nicm 1033: if (c->prompt_index != 0) {
1034: c->prompt_index = 0;
1.158 nicm 1035: break;
1.1 nicm 1036: }
1037: break;
1.155 nicm 1038: case KEYC_END:
1039: case '\005': /* C-e */
1.1 nicm 1040: if (c->prompt_index != size) {
1041: c->prompt_index = size;
1.158 nicm 1042: break;
1.1 nicm 1043: }
1044: break;
1.155 nicm 1045: case '\011': /* Tab */
1.152 nicm 1046: if (c->prompt_buffer[0].size == 0)
1.1 nicm 1047: break;
1048:
1049: idx = c->prompt_index;
1050: if (idx != 0)
1051: idx--;
1052:
1053: /* Find the word we are in. */
1.152 nicm 1054: first = &c->prompt_buffer[idx];
1055: while (first > c->prompt_buffer && !status_prompt_space(first))
1.1 nicm 1056: first--;
1.152 nicm 1057: while (first->size != 0 && status_prompt_space(first))
1.1 nicm 1058: first++;
1.152 nicm 1059: last = &c->prompt_buffer[idx];
1060: while (last->size != 0 && !status_prompt_space(last))
1.1 nicm 1061: last++;
1.152 nicm 1062: while (last > c->prompt_buffer && status_prompt_space(last))
1.1 nicm 1063: last--;
1.152 nicm 1064: if (last->size != 0)
1.1 nicm 1065: last++;
1.152 nicm 1066: if (last <= first)
1067: break;
1068:
1069: used = 0;
1070: for (ud = first; ud < last; ud++) {
1071: if (used + ud->size >= sizeof word)
1072: break;
1073: memcpy(word + used, ud->data, ud->size);
1074: used += ud->size;
1075: }
1076: if (ud != last)
1.1 nicm 1077: break;
1.152 nicm 1078: word[used] = '\0';
1.1 nicm 1079:
1080: /* And try to complete it. */
1.152 nicm 1081: if ((s = status_prompt_complete(c->session, word)) == NULL)
1.1 nicm 1082: break;
1083:
1084: /* Trim out word. */
1085: n = size - (last - c->prompt_buffer) + 1; /* with \0 */
1.152 nicm 1086: memmove(first, last, n * sizeof *c->prompt_buffer);
1.1 nicm 1087: size -= last - first;
1088:
1089: /* Insert the new word. */
1.55 nicm 1090: size += strlen(s);
1.1 nicm 1091: off = first - c->prompt_buffer;
1.152 nicm 1092: c->prompt_buffer = xreallocarray(c->prompt_buffer, size + 1,
1093: sizeof *c->prompt_buffer);
1.1 nicm 1094: first = c->prompt_buffer + off;
1.152 nicm 1095: memmove(first + strlen(s), first, n * sizeof *c->prompt_buffer);
1096: for (idx = 0; idx < strlen(s); idx++)
1097: utf8_set(&first[idx], s[idx]);
1.1 nicm 1098:
1099: c->prompt_index = (first - c->prompt_buffer) + strlen(s);
1.94 nicm 1100: free(s);
1.1 nicm 1101:
1.158 nicm 1102: goto changed;
1.155 nicm 1103: case KEYC_BSPACE:
1104: case '\010': /* C-h */
1.1 nicm 1105: if (c->prompt_index != 0) {
1106: if (c->prompt_index == size)
1.152 nicm 1107: c->prompt_buffer[--c->prompt_index].size = 0;
1.1 nicm 1108: else {
1109: memmove(c->prompt_buffer + c->prompt_index - 1,
1110: c->prompt_buffer + c->prompt_index,
1.152 nicm 1111: (size + 1 - c->prompt_index) *
1112: sizeof *c->prompt_buffer);
1.1 nicm 1113: c->prompt_index--;
1114: }
1.158 nicm 1115: goto changed;
1.1 nicm 1116: }
1117: break;
1.155 nicm 1118: case KEYC_DC:
1119: case '\004': /* C-d */
1.1 nicm 1120: if (c->prompt_index != size) {
1121: memmove(c->prompt_buffer + c->prompt_index,
1122: c->prompt_buffer + c->prompt_index + 1,
1.152 nicm 1123: (size + 1 - c->prompt_index) *
1124: sizeof *c->prompt_buffer);
1.158 nicm 1125: goto changed;
1.18 nicm 1126: }
1.26 nicm 1127: break;
1.155 nicm 1128: case '\025': /* C-u */
1.152 nicm 1129: c->prompt_buffer[0].size = 0;
1.26 nicm 1130: c->prompt_index = 0;
1.158 nicm 1131: goto changed;
1.155 nicm 1132: case '\013': /* C-k */
1.18 nicm 1133: if (c->prompt_index < size) {
1.152 nicm 1134: c->prompt_buffer[c->prompt_index].size = 0;
1.158 nicm 1135: goto changed;
1.1 nicm 1136: }
1137: break;
1.155 nicm 1138: case '\027': /* C-w */
1.152 nicm 1139: ws = options_get_string(oo, "word-separators");
1.81 nicm 1140: idx = c->prompt_index;
1141:
1142: /* Find a non-separator. */
1143: while (idx != 0) {
1144: idx--;
1.152 nicm 1145: if (!status_prompt_in_list(ws, &c->prompt_buffer[idx]))
1.81 nicm 1146: break;
1147: }
1148:
1149: /* Find the separator at the beginning of the word. */
1150: while (idx != 0) {
1151: idx--;
1.152 nicm 1152: if (status_prompt_in_list(ws, &c->prompt_buffer[idx])) {
1.81 nicm 1153: /* Go back to the word. */
1154: idx++;
1155: break;
1156: }
1157: }
1158:
1159: memmove(c->prompt_buffer + idx,
1160: c->prompt_buffer + c->prompt_index,
1.152 nicm 1161: (size + 1 - c->prompt_index) *
1162: sizeof *c->prompt_buffer);
1.81 nicm 1163: memset(c->prompt_buffer + size - (c->prompt_index - idx),
1.152 nicm 1164: '\0', (c->prompt_index - idx) * sizeof *c->prompt_buffer);
1.81 nicm 1165: c->prompt_index = idx;
1.152 nicm 1166:
1.158 nicm 1167: goto changed;
1.155 nicm 1168: case 'f'|KEYC_ESCAPE:
1169: ws = options_get_string(oo, "word-separators");
1.81 nicm 1170:
1171: /* Find a word. */
1172: while (c->prompt_index != size) {
1.152 nicm 1173: idx = ++c->prompt_index;
1174: if (!status_prompt_in_list(ws, &c->prompt_buffer[idx]))
1.81 nicm 1175: break;
1176: }
1177:
1178: /* Find the separator at the end of the word. */
1179: while (c->prompt_index != size) {
1.152 nicm 1180: idx = ++c->prompt_index;
1181: if (status_prompt_in_list(ws, &c->prompt_buffer[idx]))
1.81 nicm 1182: break;
1183: }
1.106 nicm 1184:
1185: /* Back up to the end-of-word like vi. */
1186: if (options_get_number(oo, "status-keys") == MODEKEY_VI &&
1187: c->prompt_index != 0)
1188: c->prompt_index--;
1.81 nicm 1189:
1.158 nicm 1190: goto changed;
1.155 nicm 1191: case 'b'|KEYC_ESCAPE:
1192: ws = options_get_string(oo, "word-separators");
1.81 nicm 1193:
1194: /* Find a non-separator. */
1195: while (c->prompt_index != 0) {
1.152 nicm 1196: idx = --c->prompt_index;
1197: if (!status_prompt_in_list(ws, &c->prompt_buffer[idx]))
1.81 nicm 1198: break;
1199: }
1200:
1201: /* Find the separator at the beginning of the word. */
1202: while (c->prompt_index != 0) {
1.152 nicm 1203: idx = --c->prompt_index;
1204: if (status_prompt_in_list(ws, &c->prompt_buffer[idx])) {
1.81 nicm 1205: /* Go back to the word. */
1206: c->prompt_index++;
1207: break;
1208: }
1209: }
1.158 nicm 1210: goto changed;
1.155 nicm 1211: case KEYC_UP:
1212: case '\020': /* C-p */
1.66 nicm 1213: histstr = status_prompt_up_history(&c->prompt_hindex);
1214: if (histstr == NULL)
1.1 nicm 1215: break;
1.94 nicm 1216: free(c->prompt_buffer);
1.152 nicm 1217: c->prompt_buffer = utf8_fromcstr(histstr);
1218: c->prompt_index = utf8_strlen(c->prompt_buffer);
1.158 nicm 1219: goto changed;
1.155 nicm 1220: case KEYC_DOWN:
1221: case '\016': /* C-n */
1.66 nicm 1222: histstr = status_prompt_down_history(&c->prompt_hindex);
1223: if (histstr == NULL)
1.65 nicm 1224: break;
1.94 nicm 1225: free(c->prompt_buffer);
1.152 nicm 1226: c->prompt_buffer = utf8_fromcstr(histstr);
1227: c->prompt_index = utf8_strlen(c->prompt_buffer);
1.158 nicm 1228: goto changed;
1.155 nicm 1229: case '\031': /* C-y */
1.134 nicm 1230: if ((pb = paste_get_top(NULL)) == NULL)
1.1 nicm 1231: break;
1.134 nicm 1232: bufdata = paste_buffer_data(pb, &bufsize);
1233: for (n = 0; n < bufsize; n++) {
1234: ch = (u_char)bufdata[n];
1.152 nicm 1235: if (ch < 32 || ch >= 127)
1.32 nicm 1236: break;
1237: }
1.1 nicm 1238:
1.152 nicm 1239: c->prompt_buffer = xreallocarray(c->prompt_buffer, size + n + 1,
1240: sizeof *c->prompt_buffer);
1.1 nicm 1241: if (c->prompt_index == size) {
1.152 nicm 1242: for (idx = 0; idx < n; idx++) {
1243: ud = &c->prompt_buffer[c->prompt_index + idx];
1244: utf8_set(ud, bufdata[idx]);
1245: }
1.1 nicm 1246: c->prompt_index += n;
1.152 nicm 1247: c->prompt_buffer[c->prompt_index].size = 0;
1.1 nicm 1248: } else {
1249: memmove(c->prompt_buffer + c->prompt_index + n,
1250: c->prompt_buffer + c->prompt_index,
1.152 nicm 1251: (size + 1 - c->prompt_index) *
1252: sizeof *c->prompt_buffer);
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;
1258: }
1.158 nicm 1259: goto changed;
1.155 nicm 1260: case '\024': /* C-t */
1.30 nicm 1261: idx = c->prompt_index;
1262: if (idx < size)
1263: idx++;
1264: if (idx >= 2) {
1.152 nicm 1265: utf8_copy(&tmp, &c->prompt_buffer[idx - 2]);
1266: utf8_copy(&c->prompt_buffer[idx - 2],
1267: &c->prompt_buffer[idx - 1]);
1268: utf8_copy(&c->prompt_buffer[idx - 1], &tmp);
1.30 nicm 1269: c->prompt_index = idx;
1.158 nicm 1270: goto changed;
1.30 nicm 1271: }
1.1 nicm 1272: break;
1.155 nicm 1273: case '\r':
1274: case '\n':
1.152 nicm 1275: s = utf8_tocstr(c->prompt_buffer);
1276: if (*s != '\0')
1277: status_prompt_add_history(s);
1.166 ! nicm 1278: if (c->prompt_inputcb(c, c->prompt_data, s, 1) == 0)
1.25 nicm 1279: status_prompt_clear(c);
1.152 nicm 1280: free(s);
1.25 nicm 1281: break;
1.155 nicm 1282: case '\033': /* Escape */
1283: case '\003': /* C-c */
1.166 ! nicm 1284: if (c->prompt_inputcb(c, c->prompt_data, NULL, 1) == 0)
1.1 nicm 1285: status_prompt_clear(c);
1286: break;
1.158 nicm 1287: case '\022': /* C-r */
1288: if (c->prompt_flags & PROMPT_INCREMENTAL) {
1289: prefix = '-';
1290: goto changed;
1291: }
1292: break;
1293: case '\023': /* C-s */
1294: if (c->prompt_flags & PROMPT_INCREMENTAL) {
1295: prefix = '+';
1296: goto changed;
1297: }
1298: break;
1299: default:
1300: goto append_key;
1.154 nicm 1301: }
1.152 nicm 1302:
1.158 nicm 1303: c->flags |= CLIENT_STATUS;
1304: return (0);
1305:
1.154 nicm 1306: append_key:
1307: if (key <= 0x1f || key >= KEYC_BASE)
1308: return (0);
1309: if (utf8_split(key, &tmp) != UTF8_DONE)
1310: return (0);
1.1 nicm 1311:
1.154 nicm 1312: c->prompt_buffer = xreallocarray(c->prompt_buffer, size + 2,
1313: sizeof *c->prompt_buffer);
1.1 nicm 1314:
1.154 nicm 1315: if (c->prompt_index == size) {
1316: utf8_copy(&c->prompt_buffer[c->prompt_index], &tmp);
1317: c->prompt_index++;
1318: c->prompt_buffer[c->prompt_index].size = 0;
1319: } else {
1320: memmove(c->prompt_buffer + c->prompt_index + 1,
1321: c->prompt_buffer + c->prompt_index,
1322: (size + 1 - c->prompt_index) *
1323: sizeof *c->prompt_buffer);
1324: utf8_copy(&c->prompt_buffer[c->prompt_index], &tmp);
1325: c->prompt_index++;
1326: }
1.1 nicm 1327:
1.154 nicm 1328: if (c->prompt_flags & PROMPT_SINGLE) {
1329: s = utf8_tocstr(c->prompt_buffer);
1330: if (strlen(s) != 1)
1331: status_prompt_clear(c);
1.166 ! nicm 1332: else if (c->prompt_inputcb(c, c->prompt_data, s, 1) == 0)
1.154 nicm 1333: status_prompt_clear(c);
1334: free(s);
1.1 nicm 1335: }
1.154 nicm 1336:
1.158 nicm 1337: changed:
1.154 nicm 1338: c->flags |= CLIENT_STATUS;
1.158 nicm 1339: if (c->prompt_flags & PROMPT_INCREMENTAL) {
1340: s = utf8_tocstr(c->prompt_buffer);
1341: xasprintf(&cp, "%c%s", prefix, s);
1.166 ! nicm 1342: c->prompt_inputcb(c, c->prompt_data, cp, 0);
1.158 nicm 1343: free(cp);
1344: free(s);
1345: }
1.154 nicm 1346: return (0);
1.1 nicm 1347: }
1348:
1.65 nicm 1349: /* Get previous line from the history. */
1.151 nicm 1350: static const char *
1.65 nicm 1351: status_prompt_up_history(u_int *idx)
1352: {
1353: /*
1.128 nicm 1354: * History runs from 0 to size - 1. Index is from 0 to size. Zero is
1355: * empty.
1.65 nicm 1356: */
1357:
1.128 nicm 1358: if (status_prompt_hsize == 0 || *idx == status_prompt_hsize)
1.65 nicm 1359: return (NULL);
1360: (*idx)++;
1.128 nicm 1361: return (status_prompt_hlist[status_prompt_hsize - *idx]);
1.65 nicm 1362: }
1363:
1364: /* Get next line from the history. */
1.151 nicm 1365: static const char *
1.65 nicm 1366: status_prompt_down_history(u_int *idx)
1367: {
1.128 nicm 1368: if (status_prompt_hsize == 0 || *idx == 0)
1.65 nicm 1369: return ("");
1370: (*idx)--;
1371: if (*idx == 0)
1372: return ("");
1.128 nicm 1373: return (status_prompt_hlist[status_prompt_hsize - *idx]);
1.65 nicm 1374: }
1375:
1.1 nicm 1376: /* Add line to the history. */
1.151 nicm 1377: static void
1.65 nicm 1378: status_prompt_add_history(const char *line)
1.1 nicm 1379: {
1.128 nicm 1380: size_t size;
1.65 nicm 1381:
1.128 nicm 1382: if (status_prompt_hsize > 0 &&
1383: strcmp(status_prompt_hlist[status_prompt_hsize - 1], line) == 0)
1.1 nicm 1384: return;
1385:
1.128 nicm 1386: if (status_prompt_hsize == PROMPT_HISTORY) {
1387: free(status_prompt_hlist[0]);
1.1 nicm 1388:
1.128 nicm 1389: size = (PROMPT_HISTORY - 1) * sizeof *status_prompt_hlist;
1390: memmove(&status_prompt_hlist[0], &status_prompt_hlist[1], size);
1.1 nicm 1391:
1.128 nicm 1392: status_prompt_hlist[status_prompt_hsize - 1] = xstrdup(line);
1393: return;
1394: }
1.1 nicm 1395:
1.128 nicm 1396: status_prompt_hlist = xreallocarray(status_prompt_hlist,
1397: status_prompt_hsize + 1, sizeof *status_prompt_hlist);
1398: status_prompt_hlist[status_prompt_hsize++] = xstrdup(line);
1399: }
1400:
1401: /* Build completion list. */
1.151 nicm 1402: static const char **
1.128 nicm 1403: status_prompt_complete_list(u_int *size, const char *s)
1404: {
1405: const char **list = NULL, **layout;
1406: const struct cmd_entry **cmdent;
1407: const struct options_table_entry *oe;
1408: const char *layouts[] = {
1409: "even-horizontal", "even-vertical", "main-horizontal",
1410: "main-vertical", "tiled", NULL
1411: };
1.1 nicm 1412:
1.128 nicm 1413: *size = 0;
1.1 nicm 1414: for (cmdent = cmd_table; *cmdent != NULL; cmdent++) {
1.128 nicm 1415: if (strncmp((*cmdent)->name, s, strlen(s)) == 0) {
1416: list = xreallocarray(list, (*size) + 1, sizeof *list);
1417: list[(*size)++] = (*cmdent)->name;
1418: }
1.56 nicm 1419: }
1.142 nicm 1420: for (oe = options_table; oe->name != NULL; oe++) {
1.128 nicm 1421: if (strncmp(oe->name, s, strlen(s)) == 0) {
1422: list = xreallocarray(list, (*size) + 1, sizeof *list);
1423: list[(*size)++] = oe->name;
1424: }
1425: }
1426: for (layout = layouts; *layout != NULL; layout++) {
1427: if (strncmp(*layout, s, strlen(s)) == 0) {
1428: list = xreallocarray(list, (*size) + 1, sizeof *list);
1429: list[(*size)++] = *layout;
1430: }
1431: }
1432: return (list);
1433: }
1434:
1435: /* Find longest prefix. */
1.151 nicm 1436: static char *
1.128 nicm 1437: status_prompt_complete_prefix(const char **list, u_int size)
1438: {
1439: char *out;
1440: u_int i;
1441: size_t j;
1442:
1443: out = xstrdup(list[0]);
1444: for (i = 1; i < size; i++) {
1445: j = strlen(list[i]);
1446: if (j > strlen(out))
1447: j = strlen(out);
1448: for (; j > 0; j--) {
1449: if (out[j - 1] != list[i][j - 1])
1450: out[j - 1] = '\0';
1451: }
1.1 nicm 1452: }
1.128 nicm 1453: return (out);
1454: }
1.1 nicm 1455:
1.128 nicm 1456: /* Complete word. */
1.151 nicm 1457: static char *
1.152 nicm 1458: status_prompt_complete(struct session *session, const char *s)
1.128 nicm 1459: {
1460: const char **list = NULL, *colon;
1461: u_int size = 0, i;
1462: struct session *s_loop;
1463: struct winlink *wl;
1464: struct window *w;
1465: char *copy, *out, *tmp;
1466:
1467: if (*s == '\0')
1.1 nicm 1468: return (NULL);
1.128 nicm 1469: out = NULL;
1470:
1471: if (strncmp(s, "-t", 2) != 0 && strncmp(s, "-s", 2) != 0) {
1472: list = status_prompt_complete_list(&size, s);
1473: if (size == 0)
1474: out = NULL;
1475: else if (size == 1)
1476: xasprintf(&out, "%s ", list[0]);
1477: else
1478: out = status_prompt_complete_prefix(list, size);
1479: free(list);
1480: return (out);
1481: }
1482: copy = xstrdup(s);
1483:
1484: colon = ":";
1485: if (copy[strlen(copy) - 1] == ':')
1486: copy[strlen(copy) - 1] = '\0';
1487: else
1488: colon = "";
1489: s = copy + 2;
1.1 nicm 1490:
1.128 nicm 1491: RB_FOREACH(s_loop, sessions, &sessions) {
1492: if (strncmp(s_loop->name, s, strlen(s)) == 0) {
1493: list = xreallocarray(list, size + 2, sizeof *list);
1494: list[size++] = s_loop->name;
1495: }
1496: }
1497: if (size == 1) {
1498: out = xstrdup(list[0]);
1499: if (session_find(list[0]) != NULL)
1500: colon = ":";
1501: } else if (size != 0)
1502: out = status_prompt_complete_prefix(list, size);
1503: if (out != NULL) {
1504: xasprintf(&tmp, "-%c%s%s", copy[1], out, colon);
1.163 nicm 1505: free(out);
1.128 nicm 1506: out = tmp;
1507: goto found;
1508: }
1509:
1510: colon = "";
1511: if (*s == ':') {
1.152 nicm 1512: RB_FOREACH(wl, winlinks, &session->windows) {
1.128 nicm 1513: xasprintf(&tmp, ":%s", wl->window->name);
1514: if (strncmp(tmp, s, strlen(s)) == 0){
1515: list = xreallocarray(list, size + 1,
1516: sizeof *list);
1517: list[size++] = tmp;
1518: continue;
1519: }
1520: free(tmp);
1.1 nicm 1521:
1.128 nicm 1522: xasprintf(&tmp, ":%d", wl->idx);
1523: if (strncmp(tmp, s, strlen(s)) == 0) {
1524: list = xreallocarray(list, size + 1,
1525: sizeof *list);
1526: list[size++] = tmp;
1527: continue;
1528: }
1529: free(tmp);
1530: }
1531: } else {
1532: RB_FOREACH(s_loop, sessions, &sessions) {
1533: RB_FOREACH(wl, winlinks, &s_loop->windows) {
1534: w = wl->window;
1535:
1536: xasprintf(&tmp, "%s:%s", s_loop->name, w->name);
1537: if (strncmp(tmp, s, strlen(s)) == 0) {
1538: list = xreallocarray(list, size + 1,
1539: sizeof *list);
1540: list[size++] = tmp;
1541: continue;
1542: }
1543: free(tmp);
1544:
1545: xasprintf(&tmp, "%s:%d", s_loop->name, wl->idx);
1546: if (strncmp(tmp, s, strlen(s)) == 0) {
1547: list = xreallocarray(list, size + 1,
1548: sizeof *list);
1549: list[size++] = tmp;
1550: continue;
1551: }
1552: free(tmp);
1553: }
1.1 nicm 1554: }
1555: }
1.128 nicm 1556: if (size == 1) {
1557: out = xstrdup(list[0]);
1558: colon = " ";
1559: } else if (size != 0)
1560: out = status_prompt_complete_prefix(list, size);
1561: if (out != NULL) {
1562: xasprintf(&tmp, "-%c%s%s", copy[1], out, colon);
1563: out = tmp;
1564: }
1565:
1566: for (i = 0; i < size; i++)
1567: free((void *)list[i]);
1568:
1569: found:
1570: free(copy);
1571: free(list);
1572: return (out);
1.1 nicm 1573: }