Annotation of src/usr.bin/tmux/status.c, Revision 1.11
1.11 ! nicm 1: /* $OpenBSD: status.c,v 1.10 2009/07/15 17:39:00 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:
32: char *status_replace(struct session *, char *, time_t);
33: char *status_replace_popen(char **);
34: size_t status_width(struct winlink *);
35: char *status_print(struct session *, struct winlink *, struct grid_cell *);
36:
37: void status_prompt_add_history(struct client *);
38: char *status_prompt_complete(const char *);
39:
40: /* Draw status for client on the last lines of given context. */
41: int
42: status_redraw(struct client *c)
43: {
44: struct screen_write_ctx ctx;
45: struct session *s = c->session;
46: struct winlink *wl;
1.7 nicm 47: struct screen old_status;
1.1 nicm 48: char *left, *right, *text, *ptr;
1.2 nicm 49: size_t llen, llen2, rlen, rlen2, offset;
1.7 nicm 50: size_t xx, yy, size, start, width;
1.1 nicm 51: struct grid_cell stdgc, gc;
1.3 nicm 52: int larrow, rarrow, utf8flag;
1.1 nicm 53:
54: left = right = NULL;
55:
1.7 nicm 56: /* No status line?*/
57: if (c->tty.sy == 0 || !options_get_number(&s->options, "status"))
58: return (1);
59: larrow = rarrow = 0;
1.1 nicm 60:
1.7 nicm 61: /* Create the target screen. */
62: memcpy(&old_status, &c->status, sizeof old_status);
63: screen_init(&c->status, c->tty.sx, 1, 0);
1.1 nicm 64:
65: if (gettimeofday(&c->status_timer, NULL) != 0)
66: fatal("gettimeofday");
67: memcpy(&stdgc, &grid_default_cell, sizeof gc);
68: stdgc.bg = options_get_number(&s->options, "status-fg");
69: stdgc.fg = options_get_number(&s->options, "status-bg");
70: stdgc.attr |= options_get_number(&s->options, "status-attr");
71:
72: yy = c->tty.sy - 1;
73: if (yy == 0)
74: goto blank;
75:
1.3 nicm 76: /* Caring about UTF-8 in status line? */
77: utf8flag = options_get_number(&s->options, "status-utf8");
78:
1.1 nicm 79: /* Work out the left and right strings. */
80: left = status_replace(s, options_get_string(
81: &s->options, "status-left"), c->status_timer.tv_sec);
82: llen = options_get_number(&s->options, "status-left-length");
1.3 nicm 83: llen2 = screen_write_strlen(utf8flag, "%s", left);
1.2 nicm 84: if (llen2 < llen)
85: llen = llen2;
1.1 nicm 86:
87: right = status_replace(s, options_get_string(
88: &s->options, "status-right"), c->status_timer.tv_sec);
89: rlen = options_get_number(&s->options, "status-right-length");
1.3 nicm 90: rlen2 = screen_write_strlen(utf8flag, "%s", right);
1.2 nicm 91: if (rlen2 < rlen)
92: rlen = rlen2;
1.1 nicm 93:
94: /*
95: * Figure out how much space we have for the window list. If there isn't
96: * enough space, just wimp out.
97: */
98: xx = 0;
99: if (llen != 0)
100: xx += llen + 1;
101: if (rlen != 0)
102: xx += rlen + 1;
103: if (c->tty.sx == 0 || c->tty.sx <= xx)
104: goto blank;
105: xx = c->tty.sx - xx;
106:
107: /*
108: * Right. We have xx characters to fill. Find out how much is to go in
109: * them and the offset of the current window (it must be on screen).
110: */
111: width = offset = 0;
112: RB_FOREACH(wl, winlinks, &s->windows) {
113: size = status_width(wl) + 1;
114: if (wl == s->curw)
115: offset = width;
116: width += size;
117: }
118: start = 0;
119:
120: /* If there is enough space for the total width, all is gravy. */
121: if (width <= xx)
122: goto draw;
123:
124: /* Find size of current window text. */
125: size = status_width(s->curw);
126:
127: /*
128: * If the offset is already on screen, we're good to draw from the
129: * start and just leave off the end.
130: */
131: if (offset + size < xx) {
132: if (xx > 0) {
133: rarrow = 1;
134: xx--;
135: }
136:
137: width = xx;
138: goto draw;
139: }
140:
141: /*
142: * Work out how many characters we need to omit from the start. There
143: * are xx characters to fill, and offset + size must be the last. So,
144: * the start character is offset + size - xx.
145: */
146: if (xx > 0) {
147: larrow = 1;
148: xx--;
149: }
150:
151: start = offset + size - xx;
152: if (xx > 0 && width > start + xx + 1) { /* + 1, eh? */
153: rarrow = 1;
154: start++;
155: xx--;
156: }
157: width = xx;
158:
159: draw:
160: /* Bail here if anything is too small too. XXX. */
161: if (width == 0 || xx == 0)
162: goto blank;
163:
164: /* Begin drawing and move to the starting position. */
165: screen_write_start(&ctx, NULL, &c->status);
166: if (llen != 0) {
167: screen_write_cursormove(&ctx, 0, yy);
1.5 nicm 168: screen_write_nputs(&ctx, llen, &stdgc, utf8flag, "%s", left);
169: screen_write_putc(&ctx, &stdgc, ' ');
1.1 nicm 170: if (larrow)
171: screen_write_putc(&ctx, &stdgc, ' ');
172: } else {
173: if (larrow)
174: screen_write_cursormove(&ctx, 1, yy);
175: else
176: screen_write_cursormove(&ctx, 0, yy);
177: }
178:
179: /* Draw each character in succession. */
180: offset = 0;
181: RB_FOREACH(wl, winlinks, &s->windows) {
182: memcpy(&gc, &stdgc, sizeof gc);
183: text = status_print(s, wl, &gc);
184:
185: if (larrow == 1 && offset < start) {
186: if (session_alert_has(s, wl, WINDOW_ACTIVITY))
187: larrow = -1;
188: else if (session_alert_has(s, wl, WINDOW_BELL))
189: larrow = -1;
190: else if (session_alert_has(s, wl, WINDOW_CONTENT))
191: larrow = -1;
192: }
193:
194: for (ptr = text; *ptr != '\0'; ptr++) {
195: if (offset >= start && offset < start + width)
196: screen_write_putc(&ctx, &gc, *ptr);
197: offset++;
198: }
199:
200: if (rarrow == 1 && offset > start + width) {
201: if (session_alert_has(s, wl, WINDOW_ACTIVITY))
202: rarrow = -1;
203: else if (session_alert_has(s, wl, WINDOW_BELL))
204: rarrow = -1;
205: else if (session_alert_has(s, wl, WINDOW_CONTENT))
206: rarrow = -1;
207: }
208:
209: if (offset < start + width) {
210: if (offset >= start) {
211: screen_write_putc(&ctx, &stdgc, ' ');
212: }
213: offset++;
214: }
215:
216: xfree(text);
217: }
218:
219: /* Fill the remaining space if any. */
220: while (offset++ < xx)
221: screen_write_putc(&ctx, &stdgc, ' ');
222:
223: /* Draw the last item. */
224: if (rlen != 0) {
225: screen_write_cursormove(&ctx, c->tty.sx - rlen - 1, yy);
1.5 nicm 226: screen_write_putc(&ctx, &stdgc, ' ');
227: screen_write_nputs(&ctx, rlen, &stdgc, utf8flag, "%s", right);
1.1 nicm 228: }
229:
230: /* Draw the arrows. */
231: if (larrow != 0) {
232: memcpy(&gc, &stdgc, sizeof gc);
233: if (larrow == -1)
234: gc.attr ^= GRID_ATTR_REVERSE;
235: if (llen != 0)
236: screen_write_cursormove(&ctx, llen + 1, yy);
237: else
238: screen_write_cursormove(&ctx, 0, yy);
239: screen_write_putc(&ctx, &gc, '<');
240: }
241: if (rarrow != 0) {
242: memcpy(&gc, &stdgc, sizeof gc);
243: if (rarrow == -1)
244: gc.attr ^= GRID_ATTR_REVERSE;
245: if (rlen != 0)
246: screen_write_cursormove(&ctx, c->tty.sx - rlen - 2, yy);
247: else
248: screen_write_cursormove(&ctx, c->tty.sx - 1, yy);
249: screen_write_putc(&ctx, &gc, '>');
250: }
251:
252: goto out;
253:
254: blank:
255: /* Just draw the whole line as blank. */
256: screen_write_start(&ctx, NULL, &c->status);
257: screen_write_cursormove(&ctx, 0, yy);
258: for (offset = 0; offset < c->tty.sx; offset++)
259: screen_write_putc(&ctx, &stdgc, ' ');
260:
261: out:
262: screen_write_stop(&ctx);
263:
264: if (left != NULL)
265: xfree(left);
266: if (right != NULL)
267: xfree(right);
268:
269: if (grid_compare(c->status.grid, old_status.grid) == 0) {
270: screen_free(&old_status);
271: return (0);
272: }
273: screen_free(&old_status);
274: return (1);
275: }
276:
277: char *
278: status_replace(struct session *s, char *fmt, time_t t)
279: {
280: struct winlink *wl = s->curw;
281: static char out[BUFSIZ];
282: char in[BUFSIZ], tmp[256], ch, *iptr, *optr, *ptr, *endptr;
283: char *savedptr;
284: size_t len;
285: long n;
286:
287: strftime(in, sizeof in, fmt, localtime(&t));
288: in[(sizeof in) - 1] = '\0';
289:
290: iptr = in;
291: optr = out;
292: savedptr = NULL;
293:
294: while (*iptr != '\0') {
295: if (optr >= out + (sizeof out) - 1)
296: break;
297: switch (ch = *iptr++) {
298: case '#':
299: errno = 0;
300: n = strtol(iptr, &endptr, 10);
301: if ((n == 0 && errno != EINVAL) ||
302: (n == LONG_MIN && errno != ERANGE) ||
303: (n == LONG_MAX && errno != ERANGE) ||
304: n != 0)
305: iptr = endptr;
306: if (n <= 0)
307: n = LONG_MAX;
308:
309: ptr = NULL;
310: switch (*iptr++) {
311: case '(':
312: if (ptr == NULL) {
313: ptr = status_replace_popen(&iptr);
314: if (ptr == NULL)
315: break;
316: savedptr = ptr;
317: }
318: /* FALLTHROUGH */
319: case 'H':
320: if (ptr == NULL) {
321: if (gethostname(tmp, sizeof tmp) != 0)
322: fatal("gethostname");
323: ptr = tmp;
324: }
325: /* FALLTHROUGH */
326: case 'S':
327: if (ptr == NULL)
328: ptr = s->name;
329: /* FALLTHROUGH */
330: case 'T':
331: if (ptr == NULL)
332: ptr = wl->window->active->base.title;
333: len = strlen(ptr);
334: if ((size_t) n < len)
335: len = n;
336: if (optr + len >= out + (sizeof out) - 1)
337: break;
338: while (len > 0 && *ptr != '\0') {
339: *optr++ = *ptr++;
340: len--;
341: }
342: break;
343: case '#':
344: *optr++ = '#';
345: break;
346: }
347: if (savedptr != NULL) {
348: xfree(savedptr);
349: savedptr = NULL;
350: }
351: break;
352: default:
353: *optr++ = ch;
354: break;
355: }
356: }
357: *optr = '\0';
358:
359: return (xstrdup(out));
360: }
361:
362: char *
363: status_replace_popen(char **iptr)
364: {
365: FILE *f;
366: char *buf, *cmd, *ptr;
367: int lastesc;
368: size_t len;
369:
370: if (**iptr == '\0')
371: return (NULL);
372: if (**iptr == ')') { /* no command given */
373: (*iptr)++;
374: return (NULL);
375: }
376:
377: buf = NULL;
378:
379: cmd = xmalloc(strlen(*iptr) + 1);
380: len = 0;
381:
382: lastesc = 0;
383: for (; **iptr != '\0'; (*iptr)++) {
384: if (!lastesc && **iptr == ')')
385: break; /* unescaped ) is the end */
386: if (!lastesc && **iptr == '\\') {
387: lastesc = 1;
388: continue; /* skip \ if not escaped */
389: }
390: lastesc = 0;
391: cmd[len++] = **iptr;
392: }
393: if (**iptr == '\0') /* no terminating ) */
394: goto out;
395: (*iptr)++; /* skip final ) */
396: cmd[len] = '\0';
397:
398: if ((f = popen(cmd, "r")) == NULL)
399: goto out;
400:
401: if ((buf = fgetln(f, &len)) == NULL) {
402: pclose(f);
403: goto out;
404: }
405: if (buf[len - 1] == '\n') {
406: buf[len - 1] = '\0';
407: buf = xstrdup(buf);
408: } else {
409: ptr = xmalloc(len + 1);
410: memcpy(ptr, buf, len);
411: ptr[len] = '\0';
412: buf = ptr;
413: }
414: pclose(f);
415:
416: out:
417: xfree(cmd);
418: return (buf);
419: }
420:
421: size_t
422: status_width(struct winlink *wl)
423: {
424: return (xsnprintf(NULL, 0, "%d:%s ", wl->idx, wl->window->name));
425: }
426:
427: char *
428: status_print(struct session *s, struct winlink *wl, struct grid_cell *gc)
429: {
430: char *text, flag;
431: u_char fg, bg, attr;
432:
433: fg = options_get_number(&wl->window->options, "window-status-fg");
434: if (fg != 8)
435: gc->fg = fg;
436: bg = options_get_number(&wl->window->options, "window-status-bg");
437: if (bg != 8)
438: gc->bg = bg;
439: attr = options_get_number(&wl->window->options, "window-status-attr");
440: if (attr != 0)
441: gc->attr = attr;
442:
443: flag = ' ';
444: if (wl == SLIST_FIRST(&s->lastw))
445: flag = '-';
446: if (wl == s->curw)
447: flag = '*';
448:
449: if (session_alert_has(s, wl, WINDOW_ACTIVITY)) {
450: flag = '#';
451: gc->attr ^= GRID_ATTR_REVERSE;
452: } else if (session_alert_has(s, wl, WINDOW_BELL)) {
453: flag = '!';
454: gc->attr ^= GRID_ATTR_REVERSE;
455: } else if (session_alert_has(s, wl, WINDOW_CONTENT)) {
456: flag = '+';
457: gc->attr ^= GRID_ATTR_REVERSE;
458: }
459:
460: xasprintf(&text, "%d:%s%c", wl->idx, wl->window->name, flag);
461: return (text);
462: }
463:
1.10 nicm 464: void printflike2
465: status_message_set(struct client *c, const char *fmt, ...)
1.1 nicm 466: {
467: struct timeval tv;
1.10 nicm 468: va_list ap;
1.1 nicm 469: int delay;
470:
471: delay = options_get_number(&c->session->options, "display-time");
472: tv.tv_sec = delay / 1000;
473: tv.tv_usec = (delay % 1000) * 1000L;
474:
1.10 nicm 475: va_start(ap, fmt);
476: xvasprintf(&c->message_string, fmt, ap);
477: va_end(ap);
1.1 nicm 478: if (gettimeofday(&c->message_timer, NULL) != 0)
479: fatal("gettimeofday");
480: timeradd(&c->message_timer, &tv, &c->message_timer);
481:
482: c->tty.flags |= (TTY_NOCURSOR|TTY_FREEZE);
483: c->flags |= CLIENT_STATUS;
484: }
485:
486: void
487: status_message_clear(struct client *c)
488: {
489: if (c->message_string == NULL)
490: return;
491:
492: xfree(c->message_string);
493: c->message_string = NULL;
494:
495: c->tty.flags &= ~(TTY_NOCURSOR|TTY_FREEZE);
1.8 nicm 496: c->flags |= CLIENT_STATUS;
1.7 nicm 497:
498: screen_reinit(&c->status);
1.1 nicm 499: }
500:
501: /* Draw client message on status line of present else on last line. */
502: int
503: status_message_redraw(struct client *c)
504: {
505: struct screen_write_ctx ctx;
506: struct session *s = c->session;
507: struct screen old_status;
508: size_t len;
509: struct grid_cell gc;
510:
511: if (c->tty.sx == 0 || c->tty.sy == 0)
512: return (0);
513: memcpy(&old_status, &c->status, sizeof old_status);
514: screen_init(&c->status, c->tty.sx, 1, 0);
515:
516: len = strlen(c->message_string);
517: if (len > c->tty.sx)
518: len = c->tty.sx;
519:
520: memcpy(&gc, &grid_default_cell, sizeof gc);
521: gc.bg = options_get_number(&s->options, "message-fg");
522: gc.fg = options_get_number(&s->options, "message-bg");
523: gc.attr |= options_get_number(&s->options, "message-attr");
524:
525: screen_write_start(&ctx, NULL, &c->status);
526:
527: screen_write_cursormove(&ctx, 0, 0);
528: screen_write_puts(&ctx, &gc, "%.*s", (int) len, c->message_string);
529: for (; len < c->tty.sx; len++)
530: screen_write_putc(&ctx, &gc, ' ');
531:
532: screen_write_stop(&ctx);
533:
534: if (grid_compare(c->status.grid, old_status.grid) == 0) {
535: screen_free(&old_status);
536: return (0);
537: }
538: screen_free(&old_status);
539: return (1);
540: }
541:
542: void
543: status_prompt_set(struct client *c,
544: const char *msg, int (*fn)(void *, const char *), void *data, int flags)
545: {
546: c->prompt_string = xstrdup(msg);
547:
548: c->prompt_buffer = xstrdup("");
549: c->prompt_index = 0;
550:
551: c->prompt_callback = fn;
552: c->prompt_data = data;
553:
554: c->prompt_hindex = 0;
555:
556: c->prompt_flags = flags;
557:
558: mode_key_init(&c->prompt_mdata,
559: options_get_number(&c->session->options, "status-keys"),
560: MODEKEY_CANEDIT);
561:
562: c->tty.flags |= (TTY_NOCURSOR|TTY_FREEZE);
563: c->flags |= CLIENT_STATUS;
564: }
565:
566: void
567: status_prompt_clear(struct client *c)
568: {
569: if (c->prompt_string == NULL)
570: return;
571:
572: mode_key_free(&c->prompt_mdata);
573:
574: xfree(c->prompt_string);
575: c->prompt_string = NULL;
576:
1.4 nicm 577: if (c->prompt_flags & PROMPT_HIDDEN)
578: memset(c->prompt_buffer, 0, strlen(c->prompt_buffer));
1.1 nicm 579: xfree(c->prompt_buffer);
580: c->prompt_buffer = NULL;
581:
582: c->tty.flags &= ~(TTY_NOCURSOR|TTY_FREEZE);
1.8 nicm 583: c->flags |= CLIENT_STATUS;
1.7 nicm 584:
585: screen_reinit(&c->status);
1.1 nicm 586: }
587:
588: /* Draw client prompt on status line of present else on last line. */
589: int
590: status_prompt_redraw(struct client *c)
591: {
592: struct screen_write_ctx ctx;
593: struct session *s = c->session;
594: struct screen old_status;
595: size_t i, size, left, len, offset, n;
596: char ch;
597: struct grid_cell gc;
598:
599: if (c->tty.sx == 0 || c->tty.sy == 0)
600: return (0);
601: memcpy(&old_status, &c->status, sizeof old_status);
602: screen_init(&c->status, c->tty.sx, 1, 0);
603: offset = 0;
604:
605: len = strlen(c->prompt_string);
606: if (len > c->tty.sx)
607: len = c->tty.sx;
608:
609: memcpy(&gc, &grid_default_cell, sizeof gc);
610: gc.bg = options_get_number(&s->options, "message-fg");
611: gc.fg = options_get_number(&s->options, "message-bg");
612: gc.attr |= options_get_number(&s->options, "message-attr");
613:
614: screen_write_start(&ctx, NULL, &c->status);
615:
616: screen_write_cursormove(&ctx, 0, 0);
617: screen_write_puts(&ctx, &gc, "%.*s", (int) len, c->prompt_string);
618:
619: left = c->tty.sx - len;
620: if (left != 0) {
621: if (c->prompt_index < left)
622: size = strlen(c->prompt_buffer);
623: else {
624: offset = c->prompt_index - left - 1;
625: if (c->prompt_index == strlen(c->prompt_buffer))
626: left--;
627: size = left;
628: }
629: if (c->prompt_flags & PROMPT_HIDDEN) {
630: n = strlen(c->prompt_buffer);
631: if (n > left)
632: n = left;
633: for (i = 0; i < n; i++)
634: screen_write_putc(&ctx, &gc, '*');
635: } else {
636: screen_write_puts(&ctx, &gc,
637: "%.*s", (int) left, c->prompt_buffer + offset);
638: }
639:
640: for (i = len + size; i < c->tty.sx; i++)
641: screen_write_putc(&ctx, &gc, ' ');
642: }
643:
644: /* Draw a fake cursor. */
645: screen_write_cursormove(&ctx, len + c->prompt_index - offset, 0);
646: if (c->prompt_index == strlen(c->prompt_buffer))
647: ch = ' ';
648: else {
649: if (c->prompt_flags & PROMPT_HIDDEN)
650: ch = '*';
651: else
652: ch = c->prompt_buffer[c->prompt_index];
653: }
654: if (ch == '\0')
655: ch = ' ';
656: gc.attr ^= GRID_ATTR_REVERSE;
657: screen_write_putc(&ctx, &gc, ch);
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:
669: /* Handle keys in prompt. */
670: void
671: status_prompt_key(struct client *c, int key)
672: {
673: struct paste_buffer *pb;
674: char *s, *first, *last, word[64];
675: size_t size, n, off, idx;
676:
677: size = strlen(c->prompt_buffer);
678: switch (mode_key_lookup(&c->prompt_mdata, key)) {
679: case MODEKEYCMD_LEFT:
680: if (c->prompt_index > 0) {
681: c->prompt_index--;
682: c->flags |= CLIENT_STATUS;
683: }
684: break;
685: case MODEKEYCMD_RIGHT:
686: if (c->prompt_index < size) {
687: c->prompt_index++;
688: c->flags |= CLIENT_STATUS;
689: }
690: break;
691: case MODEKEYCMD_STARTOFLINE:
1.6 nicm 692: case MODEKEYCMD_BACKTOINDENTATION:
1.1 nicm 693: if (c->prompt_index != 0) {
694: c->prompt_index = 0;
695: c->flags |= CLIENT_STATUS;
696: }
697: break;
698: case MODEKEYCMD_ENDOFLINE:
699: if (c->prompt_index != size) {
700: c->prompt_index = size;
701: c->flags |= CLIENT_STATUS;
702: }
703: break;
704: case MODEKEYCMD_COMPLETE:
705: if (*c->prompt_buffer == '\0')
706: break;
707:
708: idx = c->prompt_index;
709: if (idx != 0)
710: idx--;
711:
712: /* Find the word we are in. */
713: first = c->prompt_buffer + idx;
714: while (first > c->prompt_buffer && *first != ' ')
715: first--;
716: while (*first == ' ')
717: first++;
718: last = c->prompt_buffer + idx;
719: while (*last != '\0' && *last != ' ')
720: last++;
721: while (*last == ' ')
722: last--;
723: if (*last != '\0')
724: last++;
725: if (last <= first ||
726: ((size_t) (last - first)) > (sizeof word) - 1)
727: break;
728: memcpy(word, first, last - first);
729: word[last - first] = '\0';
730:
731: /* And try to complete it. */
732: if ((s = status_prompt_complete(word)) == NULL)
733: break;
734:
735: /* Trim out word. */
736: n = size - (last - c->prompt_buffer) + 1; /* with \0 */
737: memmove(first, last, n);
738: size -= last - first;
739:
740: /* Insert the new word. */
741: size += strlen(s);
742: off = first - c->prompt_buffer;
743: c->prompt_buffer = xrealloc(c->prompt_buffer, 1, size + 1);
744: first = c->prompt_buffer + off;
745: memmove(first + strlen(s), first, n);
746: memcpy(first, s, strlen(s));
747:
748: c->prompt_index = (first - c->prompt_buffer) + strlen(s);
749:
750: c->flags |= CLIENT_STATUS;
751: break;
752: case MODEKEYCMD_BACKSPACE:
753: if (c->prompt_index != 0) {
754: if (c->prompt_index == size)
755: c->prompt_buffer[--c->prompt_index] = '\0';
756: else {
757: memmove(c->prompt_buffer + c->prompt_index - 1,
758: c->prompt_buffer + c->prompt_index,
759: size + 1 - c->prompt_index);
760: c->prompt_index--;
761: }
762: c->flags |= CLIENT_STATUS;
763: }
764: break;
765: case MODEKEYCMD_DELETE:
766: if (c->prompt_index != size) {
767: memmove(c->prompt_buffer + c->prompt_index,
768: c->prompt_buffer + c->prompt_index + 1,
769: size + 1 - c->prompt_index);
770: c->flags |= CLIENT_STATUS;
771: }
772: break;
773: case MODEKEYCMD_UP:
774: if (server_locked)
775: break;
776:
777: if (ARRAY_LENGTH(&c->prompt_hdata) == 0)
778: break;
1.4 nicm 779: if (c->prompt_flags & PROMPT_HIDDEN)
780: memset(c->prompt_buffer, 0, strlen(c->prompt_buffer));
1.1 nicm 781: xfree(c->prompt_buffer);
782:
783: c->prompt_buffer = xstrdup(ARRAY_ITEM(&c->prompt_hdata,
784: ARRAY_LENGTH(&c->prompt_hdata) - 1 - c->prompt_hindex));
785: if (c->prompt_hindex != ARRAY_LENGTH(&c->prompt_hdata) - 1)
786: c->prompt_hindex++;
787:
788: c->prompt_index = strlen(c->prompt_buffer);
789: c->flags |= CLIENT_STATUS;
790: break;
791: case MODEKEYCMD_DOWN:
792: if (server_locked)
793: break;
794:
1.4 nicm 795: if (c->prompt_flags & PROMPT_HIDDEN)
796: memset(c->prompt_buffer, 0, strlen(c->prompt_buffer));
1.1 nicm 797: xfree(c->prompt_buffer);
798:
799: if (c->prompt_hindex != 0) {
800: c->prompt_hindex--;
801: c->prompt_buffer = xstrdup(ARRAY_ITEM(
802: &c->prompt_hdata, ARRAY_LENGTH(
803: &c->prompt_hdata) - 1 - c->prompt_hindex));
804: } else
805: c->prompt_buffer = xstrdup("");
806:
807: c->prompt_index = strlen(c->prompt_buffer);
808: c->flags |= CLIENT_STATUS;
809: break;
810: case MODEKEYCMD_PASTE:
811: if ((pb = paste_get_top(&c->session->buffers)) == NULL)
812: break;
813: if ((last = strchr(pb->data, '\n')) == NULL)
814: last = strchr(pb->data, '\0');
815: n = last - pb->data;
816:
817: c->prompt_buffer = xrealloc(c->prompt_buffer, 1, size + n + 1);
818: if (c->prompt_index == size) {
819: memcpy(c->prompt_buffer + c->prompt_index, pb->data, n);
820: c->prompt_index += n;
821: c->prompt_buffer[c->prompt_index] = '\0';
822: } else {
823: memmove(c->prompt_buffer + c->prompt_index + n,
824: c->prompt_buffer + c->prompt_index,
825: size + 1 - c->prompt_index);
826: memcpy(c->prompt_buffer + c->prompt_index, pb->data, n);
827: c->prompt_index += n;
828: }
829:
830: c->flags |= CLIENT_STATUS;
831: break;
832: case MODEKEYCMD_CHOOSE:
833: if (*c->prompt_buffer != '\0') {
834: status_prompt_add_history(c);
835: if (c->prompt_callback(
836: c->prompt_data, c->prompt_buffer) == 0)
837: status_prompt_clear(c);
838: break;
839: }
840: /* FALLTHROUGH */
841: case MODEKEYCMD_QUIT:
842: if (c->prompt_callback(c->prompt_data, NULL) == 0)
843: status_prompt_clear(c);
844: break;
845: case MODEKEYCMD_OTHERKEY:
846: if (key < 32 || key > 126)
847: break;
848: c->prompt_buffer = xrealloc(c->prompt_buffer, 1, size + 2);
849:
850: if (c->prompt_index == size) {
851: c->prompt_buffer[c->prompt_index++] = key;
852: c->prompt_buffer[c->prompt_index] = '\0';
853: } else {
854: memmove(c->prompt_buffer + c->prompt_index + 1,
855: c->prompt_buffer + c->prompt_index,
856: size + 1 - c->prompt_index);
857: c->prompt_buffer[c->prompt_index++] = key;
858: }
859:
860: if (c->prompt_flags & PROMPT_SINGLE) {
861: if (c->prompt_callback(
862: c->prompt_data, c->prompt_buffer) == 0)
863: status_prompt_clear(c);
864: }
865:
866: c->flags |= CLIENT_STATUS;
867: break;
868: default:
869: break;
870: }
871: }
872:
873: /* Add line to the history. */
874: void
875: status_prompt_add_history(struct client *c)
876: {
877: if (server_locked)
878: return;
879:
880: if (ARRAY_LENGTH(&c->prompt_hdata) > 0 &&
881: strcmp(ARRAY_LAST(&c->prompt_hdata), c->prompt_buffer) == 0)
882: return;
883:
884: if (ARRAY_LENGTH(&c->prompt_hdata) == PROMPT_HISTORY) {
885: xfree(ARRAY_FIRST(&c->prompt_hdata));
886: ARRAY_REMOVE(&c->prompt_hdata, 0);
887: }
888:
889: ARRAY_ADD(&c->prompt_hdata, xstrdup(c->prompt_buffer));
890: }
891:
892: /* Complete word. */
893: char *
894: status_prompt_complete(const char *s)
895: {
896: const struct cmd_entry **cmdent;
897: const struct set_option_entry *optent;
898: ARRAY_DECL(, const char *) list;
899: char *prefix, *s2;
1.9 nicm 900: u_int i;
1.1 nicm 901: size_t j;
902:
903: if (*s == '\0')
904: return (NULL);
905:
906: /* First, build a list of all the possible matches. */
907: ARRAY_INIT(&list);
908: for (cmdent = cmd_table; *cmdent != NULL; cmdent++) {
909: if (strncmp((*cmdent)->name, s, strlen(s)) == 0)
910: ARRAY_ADD(&list, (*cmdent)->name);
911: }
1.9 nicm 912: for (optent = set_option_table; optent->name != NULL; optent++) {
1.1 nicm 913: if (strncmp(optent->name, s, strlen(s)) == 0)
914: ARRAY_ADD(&list, optent->name);
915: }
1.9 nicm 916: for (optent = set_window_option_table; optent->name != NULL; optent++) {
1.1 nicm 917: if (strncmp(optent->name, s, strlen(s)) == 0)
918: ARRAY_ADD(&list, optent->name);
919: }
920:
921: /* If none, bail now. */
922: if (ARRAY_LENGTH(&list) == 0) {
923: ARRAY_FREE(&list);
924: return (NULL);
925: }
926:
927: /* If an exact match, return it, with a trailing space. */
928: if (ARRAY_LENGTH(&list) == 1) {
929: xasprintf(&s2, "%s ", ARRAY_FIRST(&list));
930: ARRAY_FREE(&list);
931: return (s2);
932: }
933:
934: /* Now loop through the list and find the longest common prefix. */
935: prefix = xstrdup(ARRAY_FIRST(&list));
936: for (i = 1; i < ARRAY_LENGTH(&list); i++) {
937: s = ARRAY_ITEM(&list, i);
938:
939: j = strlen(s);
940: if (j > strlen(prefix))
941: j = strlen(prefix);
942: for (; j > 0; j--) {
943: if (prefix[j - 1] != s[j - 1])
944: prefix[j - 1] = '\0';
945: }
946: }
947:
948: ARRAY_FREE(&list);
949: return (prefix);
950: }