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