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