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