Annotation of src/usr.bin/tmux/status.c, Revision 1.217
1.217 ! nicm 1: /* $OpenBSD: status.c,v 1.216 2020/05/26 08:56:48 nicm Exp $ */
1.1 nicm 2:
3: /*
1.148 nicm 4: * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
1.1 nicm 5: *
6: * Permission to use, copy, modify, and distribute this software for any
7: * purpose with or without fee is hereby granted, provided that the above
8: * copyright notice and this permission notice appear in all copies.
9: *
10: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14: * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15: * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16: * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17: */
18:
19: #include <sys/types.h>
20: #include <sys/time.h>
21:
22: #include <errno.h>
23: #include <limits.h>
24: #include <stdarg.h>
25: #include <stdlib.h>
26: #include <string.h>
27: #include <time.h>
28: #include <unistd.h>
29:
30: #include "tmux.h"
31:
1.151 nicm 32: static void status_message_callback(int, short, void *);
33: static void status_timer_callback(int, short, void *);
34:
35: static char *status_prompt_find_history_file(void);
36: static const char *status_prompt_up_history(u_int *);
37: static const char *status_prompt_down_history(u_int *);
38: static void status_prompt_add_history(const char *);
39:
1.204 nicm 40: static char *status_prompt_complete(struct client *, const char *, u_int);
1.205 nicm 41: static char *status_prompt_complete_window_menu(struct client *,
1.206 nicm 42: struct session *, const char *, u_int, char);
1.204 nicm 43:
44: struct status_prompt_menu {
45: struct client *c;
46: u_int start;
47: u_int size;
48: char **list;
49: char flag;
50: };
1.130 nicm 51:
1.65 nicm 52: /* Status prompt history. */
1.128 nicm 53: #define PROMPT_HISTORY 100
1.151 nicm 54: static char **status_prompt_hlist;
55: static u_int status_prompt_hsize;
1.130 nicm 56:
57: /* Find the history file to load/save from/to. */
1.151 nicm 58: static char *
1.130 nicm 59: status_prompt_find_history_file(void)
60: {
61: const char *home, *history_file;
62: char *path;
63:
1.137 nicm 64: history_file = options_get_string(global_options, "history-file");
1.130 nicm 65: if (*history_file == '\0')
66: return (NULL);
67: if (*history_file == '/')
68: return (xstrdup(history_file));
69:
70: if (history_file[0] != '~' || history_file[1] != '/')
71: return (NULL);
72: if ((home = find_home()) == NULL)
73: return (NULL);
74: xasprintf(&path, "%s%s", home, history_file + 1);
75: return (path);
76: }
77:
78: /* Load status prompt history from file. */
79: void
80: status_prompt_load_history(void)
81: {
82: FILE *f;
83: char *history_file, *line, *tmp;
84: size_t length;
85:
86: if ((history_file = status_prompt_find_history_file()) == NULL)
87: return;
88: log_debug("loading history from %s", history_file);
89:
90: f = fopen(history_file, "r");
91: if (f == NULL) {
92: log_debug("%s: %s", history_file, strerror(errno));
93: free(history_file);
94: return;
95: }
96: free(history_file);
97:
98: for (;;) {
99: if ((line = fgetln(f, &length)) == NULL)
100: break;
101:
102: if (length > 0) {
103: if (line[length - 1] == '\n') {
104: line[length - 1] = '\0';
105: status_prompt_add_history(line);
106: } else {
107: tmp = xmalloc(length + 1);
108: memcpy(tmp, line, length);
109: tmp[length] = '\0';
110: status_prompt_add_history(tmp);
111: free(tmp);
112: }
113: }
114: }
115: fclose(f);
116: }
117:
118: /* Save status prompt history to file. */
119: void
120: status_prompt_save_history(void)
121: {
122: FILE *f;
123: u_int i;
124: char *history_file;
125:
126: if ((history_file = status_prompt_find_history_file()) == NULL)
127: return;
128: log_debug("saving history to %s", history_file);
129:
130: f = fopen(history_file, "w");
131: if (f == NULL) {
132: log_debug("%s: %s", history_file, strerror(errno));
133: free(history_file);
134: return;
135: }
136: free(history_file);
137:
138: for (i = 0; i < status_prompt_hsize; i++) {
139: fputs(status_prompt_hlist[i], f);
140: fputc('\n', f);
141: }
142: fclose(f);
143:
1.87 nicm 144: }
145:
1.133 nicm 146: /* Status timer callback. */
1.151 nicm 147: static void
1.141 nicm 148: status_timer_callback(__unused int fd, __unused short events, void *arg)
1.133 nicm 149: {
150: struct client *c = arg;
151: struct session *s = c->session;
152: struct timeval tv;
153:
1.175 nicm 154: evtimer_del(&c->status.timer);
1.133 nicm 155:
156: if (s == NULL)
157: return;
158:
159: if (c->message_string == NULL && c->prompt_string == NULL)
1.177 nicm 160: c->flags |= CLIENT_REDRAWSTATUS;
1.133 nicm 161:
162: timerclear(&tv);
1.137 nicm 163: tv.tv_sec = options_get_number(s->options, "status-interval");
1.133 nicm 164:
165: if (tv.tv_sec != 0)
1.175 nicm 166: evtimer_add(&c->status.timer, &tv);
1.136 nicm 167: log_debug("client %p, status interval %d", c, (int)tv.tv_sec);
1.133 nicm 168: }
169:
170: /* Start status timer for client. */
171: void
172: status_timer_start(struct client *c)
173: {
174: struct session *s = c->session;
175:
1.175 nicm 176: if (event_initialized(&c->status.timer))
177: evtimer_del(&c->status.timer);
1.133 nicm 178: else
1.175 nicm 179: evtimer_set(&c->status.timer, status_timer_callback, c);
1.133 nicm 180:
1.137 nicm 181: if (s != NULL && options_get_number(s->options, "status"))
1.133 nicm 182: status_timer_callback(-1, 0, c);
183: }
184:
185: /* Start status timer for all clients. */
186: void
187: status_timer_start_all(void)
188: {
189: struct client *c;
190:
191: TAILQ_FOREACH(c, &clients, entry)
192: status_timer_start(c);
193: }
194:
1.161 nicm 195: /* Update status cache. */
196: void
1.187 nicm 197: status_update_cache(struct session *s)
1.161 nicm 198: {
1.192 nicm 199: s->statuslines = options_get_number(s->options, "status");
200: if (s->statuslines == 0)
1.161 nicm 201: s->statusat = -1;
202: else if (options_get_number(s->options, "status-position") == 0)
203: s->statusat = 0;
204: else
205: s->statusat = 1;
206: }
207:
1.87 nicm 208: /* Get screen line of status line. -1 means off. */
209: int
210: status_at_line(struct client *c)
211: {
212: struct session *s = c->session;
213:
1.198 nicm 214: if (c->flags & (CLIENT_STATUSOFF|CLIENT_CONTROL))
1.169 nicm 215: return (-1);
1.161 nicm 216: if (s->statusat != 1)
217: return (s->statusat);
1.182 nicm 218: return (c->tty.sy - status_line_size(c));
1.169 nicm 219: }
220:
1.182 nicm 221: /* Get size of status line for client's session. 0 means off. */
1.169 nicm 222: u_int
1.182 nicm 223: status_line_size(struct client *c)
1.169 nicm 224: {
1.182 nicm 225: struct session *s = c->session;
226:
1.198 nicm 227: if (c->flags & (CLIENT_STATUSOFF|CLIENT_CONTROL))
1.182 nicm 228: return (0);
1.192 nicm 229: return (s->statuslines);
1.71 nicm 230: }
231:
1.192 nicm 232: /* Get window at window list position. */
233: struct style_range *
234: status_get_range(struct client *c, u_int x, u_int y)
1.48 nicm 235: {
1.192 nicm 236: struct status_line *sl = &c->status;
237: struct style_range *sr;
1.48 nicm 238:
1.192 nicm 239: if (y >= nitems(sl->entries))
240: return (NULL);
241: TAILQ_FOREACH(sr, &sl->entries[y].ranges, entry) {
242: if (x >= sr->start && x < sr->end)
243: return (sr);
244: }
245: return (NULL);
1.48 nicm 246: }
247:
1.192 nicm 248: /* Free all ranges. */
249: static void
250: status_free_ranges(struct style_ranges *srs)
1.48 nicm 251: {
1.192 nicm 252: struct style_range *sr, *sr1;
1.48 nicm 253:
1.192 nicm 254: TAILQ_FOREACH_SAFE(sr, srs, entry, sr1) {
255: TAILQ_REMOVE(srs, sr, entry);
256: free(sr);
1.73 nicm 257: }
258: }
259:
1.189 nicm 260: /* Save old status line. */
261: static void
262: status_push_screen(struct client *c)
263: {
264: struct status_line *sl = &c->status;
265:
266: if (sl->active == &sl->screen) {
267: sl->active = xmalloc(sizeof *sl->active);
268: screen_init(sl->active, c->tty.sx, status_line_size(c), 0);
269: }
270: sl->references++;
271: }
272:
273: /* Restore old status line. */
274: static void
275: status_pop_screen(struct client *c)
276: {
277: struct status_line *sl = &c->status;
278:
279: if (--sl->references == 0) {
280: screen_free(sl->active);
281: free(sl->active);
282: sl->active = &sl->screen;
283: }
284: }
285:
1.187 nicm 286: /* Initialize status line. */
287: void
288: status_init(struct client *c)
289: {
290: struct status_line *sl = &c->status;
1.192 nicm 291: u_int i;
292:
293: for (i = 0; i < nitems(sl->entries); i++)
294: TAILQ_INIT(&sl->entries[i].ranges);
1.187 nicm 295:
296: screen_init(&sl->screen, c->tty.sx, 1, 0);
1.189 nicm 297: sl->active = &sl->screen;
1.187 nicm 298: }
299:
1.186 nicm 300: /* Free status line. */
301: void
302: status_free(struct client *c)
303: {
304: struct status_line *sl = &c->status;
1.192 nicm 305: u_int i;
306:
307: for (i = 0; i < nitems(sl->entries); i++) {
308: status_free_ranges(&sl->entries[i].ranges);
309: free((void *)sl->entries[i].expanded);
310: }
1.186 nicm 311:
312: if (event_initialized(&sl->timer))
313: evtimer_del(&sl->timer);
314:
1.189 nicm 315: if (sl->active != &sl->screen) {
316: screen_free(sl->active);
317: free(sl->active);
318: }
1.187 nicm 319: screen_free(&sl->screen);
1.186 nicm 320: }
321:
322: /* Draw status line for client. */
1.1 nicm 323: int
324: status_redraw(struct client *c)
325: {
1.192 nicm 326: struct status_line *sl = &c->status;
327: struct status_line_entry *sle;
328: struct session *s = c->session;
329: struct screen_write_ctx ctx;
330: struct grid_cell gc;
1.197 nicm 331: u_int lines, i, n, width = c->tty.sx;
1.203 nicm 332: int flags, force = 0, changed = 0, fg, bg;
1.192 nicm 333: struct options_entry *o;
1.193 nicm 334: union options_value *ov;
1.192 nicm 335: struct format_tree *ft;
336: char *expanded;
337:
338: log_debug("%s enter", __func__);
1.1 nicm 339:
1.189 nicm 340: /* Shouldn't get here if not the active screen. */
341: if (sl->active != &sl->screen)
342: fatalx("not the active screen");
1.167 nicm 343:
1.49 nicm 344: /* No status line? */
1.182 nicm 345: lines = status_line_size(c);
1.169 nicm 346: if (c->tty.sy == 0 || lines == 0)
1.7 nicm 347: return (1);
1.1 nicm 348:
1.207 nicm 349: /* Create format tree. */
350: flags = FORMAT_STATUS;
351: if (c->flags & CLIENT_STATUSFORCE)
352: flags |= FORMAT_FORCE;
353: ft = format_create(c, NULL, FORMAT_NONE, flags);
354: format_defaults(ft, c, NULL, NULL, NULL);
355:
1.48 nicm 356: /* Set up default colour. */
1.207 nicm 357: style_apply(&gc, s->options, "status-style", ft);
1.203 nicm 358: fg = options_get_number(s->options, "status-fg");
359: if (fg != 8)
360: gc.fg = fg;
361: bg = options_get_number(s->options, "status-bg");
362: if (bg != 8)
363: gc.bg = bg;
1.192 nicm 364: if (!grid_cells_equal(&gc, &sl->style)) {
365: force = 1;
366: memcpy(&sl->style, &gc, sizeof sl->style);
367: }
368:
369: /* Resize the target screen. */
370: if (screen_size_x(&sl->screen) != width ||
371: screen_size_y(&sl->screen) != lines) {
372: screen_resize(&sl->screen, width, lines, 0);
1.200 nicm 373: changed = force = 1;
1.1 nicm 374: }
1.208 nicm 375: screen_write_start(&ctx, &sl->screen);
1.1 nicm 376:
1.192 nicm 377: /* Write the status lines. */
378: o = options_get(s->options, "status-format");
1.197 nicm 379: if (o == NULL) {
380: for (n = 0; n < width * lines; n++)
381: screen_write_putc(&ctx, &gc, ' ');
382: } else {
1.192 nicm 383: for (i = 0; i < lines; i++) {
384: screen_write_cursormove(&ctx, 0, i, 0);
385:
1.193 nicm 386: ov = options_array_get(o, i);
387: if (ov == NULL) {
1.197 nicm 388: for (n = 0; n < width; n++)
389: screen_write_putc(&ctx, &gc, ' ');
1.192 nicm 390: continue;
391: }
392: sle = &sl->entries[i];
1.1 nicm 393:
1.193 nicm 394: expanded = format_expand_time(ft, ov->string);
1.192 nicm 395: if (!force &&
396: sle->expanded != NULL &&
397: strcmp(expanded, sle->expanded) == 0) {
398: free(expanded);
399: continue;
400: }
401: changed = 1;
1.1 nicm 402:
1.197 nicm 403: for (n = 0; n < width; n++)
404: screen_write_putc(&ctx, &gc, ' ');
405: screen_write_cursormove(&ctx, 0, i, 0);
406:
1.192 nicm 407: status_free_ranges(&sle->ranges);
408: format_draw(&ctx, &gc, width, expanded, &sle->ranges);
1.1 nicm 409:
1.192 nicm 410: free(sle->expanded);
411: sle->expanded = expanded;
1.48 nicm 412: }
413: }
414: screen_write_stop(&ctx);
1.1 nicm 415:
1.192 nicm 416: /* Free the format tree. */
1.99 nicm 417: format_free(ft);
1.1 nicm 418:
1.192 nicm 419: /* Return if the status line has changed. */
420: log_debug("%s exit: force=%d, changed=%d", __func__, force, changed);
421: return (force || changed);
1.1 nicm 422: }
423:
1.46 nicm 424: /* Set a status line message. */
1.117 nicm 425: void
1.210 nicm 426: status_message_set(struct client *c, int ignore_styles, const char *fmt, ...)
1.1 nicm 427: {
1.162 nicm 428: struct timeval tv;
429: va_list ap;
430: int delay;
1.1 nicm 431:
1.12 nicm 432: status_message_clear(c);
1.189 nicm 433: status_push_screen(c);
1.167 nicm 434:
1.28 nicm 435: va_start(ap, fmt);
1.210 nicm 436: c->message_ignore_styles = ignore_styles;
1.28 nicm 437: xvasprintf(&c->message_string, fmt, ap);
438: va_end(ap);
439:
1.209 nicm 440: server_add_message("%s message: %s", c->name, c->message_string);
1.44 nicm 441:
1.137 nicm 442: delay = options_get_number(c->session->options, "display-time");
1.143 tim 443: if (delay > 0) {
444: tv.tv_sec = delay / 1000;
445: tv.tv_usec = (delay % 1000) * 1000L;
446:
447: if (event_initialized(&c->message_timer))
448: evtimer_del(&c->message_timer);
449: evtimer_set(&c->message_timer, status_message_callback, c);
450: evtimer_add(&c->message_timer, &tv);
451: }
1.1 nicm 452:
453: c->tty.flags |= (TTY_NOCURSOR|TTY_FREEZE);
1.177 nicm 454: c->flags |= CLIENT_REDRAWSTATUS;
1.1 nicm 455: }
456:
1.46 nicm 457: /* Clear status line message. */
1.1 nicm 458: void
459: status_message_clear(struct client *c)
460: {
461: if (c->message_string == NULL)
462: return;
463:
1.94 nicm 464: free(c->message_string);
1.1 nicm 465: c->message_string = NULL;
466:
1.156 nicm 467: if (c->prompt_string == NULL)
468: c->tty.flags &= ~(TTY_NOCURSOR|TTY_FREEZE);
1.177 nicm 469: c->flags |= CLIENT_ALLREDRAWFLAGS; /* was frozen and may have changed */
1.7 nicm 470:
1.189 nicm 471: status_pop_screen(c);
1.42 nicm 472: }
473:
1.46 nicm 474: /* Clear status line message after timer expires. */
1.151 nicm 475: static void
1.141 nicm 476: status_message_callback(__unused int fd, __unused short event, void *data)
1.42 nicm 477: {
478: struct client *c = data;
479:
480: status_message_clear(c);
1.1 nicm 481: }
482:
483: /* Draw client message on status line of present else on last line. */
484: int
485: status_message_redraw(struct client *c)
486: {
1.189 nicm 487: struct status_line *sl = &c->status;
488: struct screen_write_ctx ctx;
489: struct session *s = c->session;
490: struct screen old_screen;
491: size_t len;
492: u_int lines, offset;
493: struct grid_cell gc;
1.207 nicm 494: struct format_tree *ft;
1.1 nicm 495:
496: if (c->tty.sx == 0 || c->tty.sy == 0)
497: return (0);
1.189 nicm 498: memcpy(&old_screen, sl->active, sizeof old_screen);
1.169 nicm 499:
1.182 nicm 500: lines = status_line_size(c);
1.189 nicm 501: if (lines <= 1)
1.173 nicm 502: lines = 1;
1.190 nicm 503: screen_init(sl->active, c->tty.sx, lines, 0);
1.1 nicm 504:
1.139 nicm 505: len = screen_write_strlen("%s", c->message_string);
1.1 nicm 506: if (len > c->tty.sx)
507: len = c->tty.sx;
508:
1.207 nicm 509: ft = format_create_defaults(NULL, c, NULL, NULL, NULL);
510: style_apply(&gc, s->options, "message-style", ft);
511: format_free(ft);
1.1 nicm 512:
1.208 nicm 513: screen_write_start(&ctx, sl->active);
1.192 nicm 514: screen_write_fast_copy(&ctx, &sl->screen, 0, 0, c->tty.sx, lines - 1);
515: screen_write_cursormove(&ctx, 0, lines - 1, 0);
516: for (offset = 0; offset < c->tty.sx; offset++)
1.170 nicm 517: screen_write_putc(&ctx, &gc, ' ');
1.184 nicm 518: screen_write_cursormove(&ctx, 0, lines - 1, 0);
1.210 nicm 519: if (c->message_ignore_styles)
520: screen_write_nputs(&ctx, len, &gc, "%s", c->message_string);
521: else
522: format_draw(&ctx, &gc, c->tty.sx, c->message_string, NULL);
1.1 nicm 523: screen_write_stop(&ctx);
524:
1.189 nicm 525: if (grid_compare(sl->active->grid, old_screen.grid) == 0) {
526: screen_free(&old_screen);
1.1 nicm 527: return (0);
528: }
1.189 nicm 529: screen_free(&old_screen);
1.1 nicm 530: return (1);
531: }
532:
1.46 nicm 533: /* Enable status line prompt. */
1.1 nicm 534: void
1.211 nicm 535: status_prompt_set(struct client *c, struct cmd_find_state *fs,
536: const char *msg, const char *input, prompt_input_cb inputcb,
537: prompt_free_cb freecb, void *data, int flags)
1.1 nicm 538: {
1.122 nicm 539: struct format_tree *ft;
1.165 nicm 540: char *tmp, *cp;
1.122 nicm 541:
1.211 nicm 542: if (fs != NULL)
543: ft = format_create_from_state(NULL, c, fs);
544: else
545: ft = format_create_defaults(NULL, c, NULL, NULL, NULL);
1.152 nicm 546:
1.168 nicm 547: if (input == NULL)
548: input = "";
549: if (flags & PROMPT_NOFORMAT)
550: tmp = xstrdup(input);
551: else
1.185 nicm 552: tmp = format_expand_time(ft, input);
1.20 nicm 553:
1.12 nicm 554: status_message_clear(c);
555: status_prompt_clear(c);
1.189 nicm 556: status_push_screen(c);
1.12 nicm 557:
1.185 nicm 558: c->prompt_string = format_expand_time(ft, msg);
1.1 nicm 559:
1.152 nicm 560: c->prompt_buffer = utf8_fromcstr(tmp);
561: c->prompt_index = utf8_strlen(c->prompt_buffer);
1.1 nicm 562:
1.166 nicm 563: c->prompt_inputcb = inputcb;
564: c->prompt_freecb = freecb;
1.1 nicm 565: c->prompt_data = data;
566:
567: c->prompt_hindex = 0;
568:
569: c->prompt_flags = flags;
1.155 nicm 570: c->prompt_mode = PROMPT_ENTRY;
1.1 nicm 571:
1.158 nicm 572: if (~flags & PROMPT_INCREMENTAL)
573: c->tty.flags |= (TTY_NOCURSOR|TTY_FREEZE);
1.177 nicm 574: c->flags |= CLIENT_REDRAWSTATUS;
1.165 nicm 575:
576: if ((flags & PROMPT_INCREMENTAL) && *tmp != '\0') {
577: xasprintf(&cp, "=%s", tmp);
1.166 nicm 578: c->prompt_inputcb(c, c->prompt_data, cp, 0);
1.165 nicm 579: free(cp);
580: }
1.122 nicm 581:
1.152 nicm 582: free(tmp);
1.122 nicm 583: format_free(ft);
1.1 nicm 584: }
585:
1.46 nicm 586: /* Remove status line prompt. */
1.1 nicm 587: void
588: status_prompt_clear(struct client *c)
589: {
1.55 nicm 590: if (c->prompt_string == NULL)
1.1 nicm 591: return;
592:
1.166 nicm 593: if (c->prompt_freecb != NULL && c->prompt_data != NULL)
594: c->prompt_freecb(c->prompt_data);
1.1 nicm 595:
1.94 nicm 596: free(c->prompt_string);
1.1 nicm 597: c->prompt_string = NULL;
598:
1.94 nicm 599: free(c->prompt_buffer);
1.1 nicm 600: c->prompt_buffer = NULL;
601:
1.181 nicm 602: free(c->prompt_saved);
603: c->prompt_saved = NULL;
604:
1.1 nicm 605: c->tty.flags &= ~(TTY_NOCURSOR|TTY_FREEZE);
1.177 nicm 606: c->flags |= CLIENT_ALLREDRAWFLAGS; /* was frozen and may have changed */
1.7 nicm 607:
1.189 nicm 608: status_pop_screen(c);
1.27 nicm 609: }
610:
1.46 nicm 611: /* Update status line prompt with a new prompt string. */
1.27 nicm 612: void
1.76 nicm 613: status_prompt_update(struct client *c, const char *msg, const char *input)
1.27 nicm 614: {
1.122 nicm 615: struct format_tree *ft;
1.152 nicm 616: char *tmp;
1.122 nicm 617:
1.164 nicm 618: ft = format_create(c, NULL, FORMAT_NONE, 0);
1.122 nicm 619: format_defaults(ft, c, NULL, NULL, NULL);
1.152 nicm 620:
1.185 nicm 621: tmp = format_expand_time(ft, input);
1.122 nicm 622:
1.94 nicm 623: free(c->prompt_string);
1.185 nicm 624: c->prompt_string = format_expand_time(ft, msg);
1.27 nicm 625:
1.94 nicm 626: free(c->prompt_buffer);
1.152 nicm 627: c->prompt_buffer = utf8_fromcstr(tmp);
628: c->prompt_index = utf8_strlen(c->prompt_buffer);
1.27 nicm 629:
630: c->prompt_hindex = 0;
631:
1.177 nicm 632: c->flags |= CLIENT_REDRAWSTATUS;
1.122 nicm 633:
1.152 nicm 634: free(tmp);
1.122 nicm 635: format_free(ft);
1.1 nicm 636: }
637:
638: /* Draw client prompt on status line of present else on last line. */
639: int
640: status_prompt_redraw(struct client *c)
641: {
1.189 nicm 642: struct status_line *sl = &c->status;
1.152 nicm 643: struct screen_write_ctx ctx;
644: struct session *s = c->session;
1.189 nicm 645: struct screen old_screen;
646: u_int i, lines, offset, left, start, width;
647: u_int pcursor, pwidth;
1.152 nicm 648: struct grid_cell gc, cursorgc;
1.207 nicm 649: struct format_tree *ft;
1.1 nicm 650:
651: if (c->tty.sx == 0 || c->tty.sy == 0)
652: return (0);
1.189 nicm 653: memcpy(&old_screen, sl->active, sizeof old_screen);
1.169 nicm 654:
1.182 nicm 655: lines = status_line_size(c);
1.189 nicm 656: if (lines <= 1)
1.173 nicm 657: lines = 1;
1.189 nicm 658: screen_init(sl->active, c->tty.sx, lines, 0);
1.1 nicm 659:
1.207 nicm 660: ft = format_create_defaults(NULL, c, NULL, NULL, NULL);
1.155 nicm 661: if (c->prompt_mode == PROMPT_COMMAND)
1.207 nicm 662: style_apply(&gc, s->options, "message-command-style", ft);
1.108 nicm 663: else
1.207 nicm 664: style_apply(&gc, s->options, "message-style", ft);
665: format_free(ft);
1.1 nicm 666:
1.152 nicm 667: memcpy(&cursorgc, &gc, sizeof cursorgc);
668: cursorgc.attr ^= GRID_ATTR_REVERSE;
669:
670: start = screen_write_strlen("%s", c->prompt_string);
671: if (start > c->tty.sx)
672: start = c->tty.sx;
673:
1.208 nicm 674: screen_write_start(&ctx, sl->active);
1.192 nicm 675: screen_write_fast_copy(&ctx, &sl->screen, 0, 0, c->tty.sx, lines - 1);
676: screen_write_cursormove(&ctx, 0, lines - 1, 0);
677: for (offset = 0; offset < c->tty.sx; offset++)
1.170 nicm 678: screen_write_putc(&ctx, &gc, ' ');
1.192 nicm 679: screen_write_cursormove(&ctx, 0, lines - 1, 0);
1.152 nicm 680: screen_write_nputs(&ctx, start, &gc, "%s", c->prompt_string);
1.192 nicm 681: screen_write_cursormove(&ctx, start, lines - 1, 0);
1.1 nicm 682:
1.152 nicm 683: left = c->tty.sx - start;
684: if (left == 0)
685: goto finished;
686:
687: pcursor = utf8_strwidth(c->prompt_buffer, c->prompt_index);
688: pwidth = utf8_strwidth(c->prompt_buffer, -1);
689: if (pcursor >= left) {
690: /*
691: * The cursor would be outside the screen so start drawing
692: * with it on the right.
693: */
694: offset = (pcursor - left) + 1;
695: pwidth = left;
696: } else
697: offset = 0;
698: if (pwidth > left)
699: pwidth = left;
700:
701: width = 0;
702: for (i = 0; c->prompt_buffer[i].size != 0; i++) {
703: if (width < offset) {
704: width += c->prompt_buffer[i].width;
705: continue;
1.1 nicm 706: }
1.152 nicm 707: if (width >= offset + pwidth)
708: break;
709: width += c->prompt_buffer[i].width;
710: if (width > offset + pwidth)
711: break;
1.1 nicm 712:
1.152 nicm 713: if (i != c->prompt_index) {
714: utf8_copy(&gc.data, &c->prompt_buffer[i]);
715: screen_write_cell(&ctx, &gc);
716: } else {
717: utf8_copy(&cursorgc.data, &c->prompt_buffer[i]);
718: screen_write_cell(&ctx, &cursorgc);
719: }
1.1 nicm 720: }
1.189 nicm 721: if (sl->active->cx < screen_size_x(sl->active) && c->prompt_index >= i)
1.152 nicm 722: screen_write_putc(&ctx, &cursorgc, ' ');
1.1 nicm 723:
1.152 nicm 724: finished:
1.1 nicm 725: screen_write_stop(&ctx);
1.51 nicm 726:
1.189 nicm 727: if (grid_compare(sl->active->grid, old_screen.grid) == 0) {
728: screen_free(&old_screen);
1.1 nicm 729: return (0);
730: }
1.189 nicm 731: screen_free(&old_screen);
1.1 nicm 732: return (1);
733: }
734:
1.152 nicm 735: /* Is this a separator? */
736: static int
737: status_prompt_in_list(const char *ws, const struct utf8_data *ud)
738: {
739: if (ud->size != 1 || ud->width != 1)
740: return (0);
741: return (strchr(ws, *ud->data) != NULL);
742: }
743:
744: /* Is this a space? */
745: static int
746: status_prompt_space(const struct utf8_data *ud)
747: {
748: if (ud->size != 1 || ud->width != 1)
749: return (0);
750: return (*ud->data == ' ');
751: }
752:
1.155 nicm 753: /*
754: * Translate key from emacs to vi. Return 0 to drop key, 1 to process the key
755: * as an emacs key; return 2 to append to the buffer.
756: */
757: static int
758: status_prompt_translate_key(struct client *c, key_code key, key_code *new_key)
759: {
760: if (c->prompt_mode == PROMPT_ENTRY) {
761: switch (key) {
762: case '\003': /* C-c */
1.202 nicm 763: case '\007': /* C-g */
1.155 nicm 764: case '\010': /* C-h */
765: case '\011': /* Tab */
766: case '\025': /* C-u */
767: case '\027': /* C-w */
768: case '\n':
769: case '\r':
770: case KEYC_BSPACE:
771: case KEYC_DC:
772: case KEYC_DOWN:
773: case KEYC_END:
774: case KEYC_HOME:
775: case KEYC_LEFT:
776: case KEYC_RIGHT:
777: case KEYC_UP:
778: *new_key = key;
779: return (1);
780: case '\033': /* Escape */
781: c->prompt_mode = PROMPT_COMMAND;
1.177 nicm 782: c->flags |= CLIENT_REDRAWSTATUS;
1.155 nicm 783: return (0);
784: }
785: *new_key = key;
786: return (2);
787: }
788:
789: switch (key) {
790: case 'A':
791: case 'I':
792: case 'C':
793: case 's':
794: case 'a':
795: c->prompt_mode = PROMPT_ENTRY;
1.177 nicm 796: c->flags |= CLIENT_REDRAWSTATUS;
1.155 nicm 797: break; /* switch mode and... */
798: case 'S':
799: c->prompt_mode = PROMPT_ENTRY;
1.177 nicm 800: c->flags |= CLIENT_REDRAWSTATUS;
1.155 nicm 801: *new_key = '\025'; /* C-u */
802: return (1);
803: case 'i':
804: case '\033': /* Escape */
805: c->prompt_mode = PROMPT_ENTRY;
1.177 nicm 806: c->flags |= CLIENT_REDRAWSTATUS;
1.155 nicm 807: return (0);
808: }
809:
810: switch (key) {
811: case 'A':
812: case '$':
813: *new_key = KEYC_END;
814: return (1);
815: case 'I':
816: case '0':
817: case '^':
818: *new_key = KEYC_HOME;
819: return (1);
820: case 'C':
821: case 'D':
822: *new_key = '\013'; /* C-k */
823: return (1);
824: case KEYC_BSPACE:
825: case 'X':
826: *new_key = KEYC_BSPACE;
827: return (1);
828: case 'b':
829: case 'B':
1.212 nicm 830: *new_key = 'b'|KEYC_META;
1.155 nicm 831: return (1);
832: case 'd':
833: *new_key = '\025';
834: return (1);
835: case 'e':
836: case 'E':
837: case 'w':
838: case 'W':
1.212 nicm 839: *new_key = 'f'|KEYC_META;
1.155 nicm 840: return (1);
841: case 'p':
842: *new_key = '\031'; /* C-y */
1.202 nicm 843: return (1);
844: case 'q':
845: *new_key = '\003'; /* C-c */
1.155 nicm 846: return (1);
847: case 's':
848: case KEYC_DC:
849: case 'x':
850: *new_key = KEYC_DC;
851: return (1);
852: case KEYC_DOWN:
853: case 'j':
854: *new_key = KEYC_DOWN;
855: return (1);
856: case KEYC_LEFT:
857: case 'h':
858: *new_key = KEYC_LEFT;
859: return (1);
860: case 'a':
861: case KEYC_RIGHT:
862: case 'l':
863: *new_key = KEYC_RIGHT;
864: return (1);
865: case KEYC_UP:
866: case 'k':
867: *new_key = KEYC_UP;
868: return (1);
869: case '\010' /* C-h */:
870: case '\003' /* C-c */:
871: case '\n':
872: case '\r':
873: return (1);
874: }
875: return (0);
876: }
877:
1.199 nicm 878: /* Paste into prompt. */
879: static int
880: status_prompt_paste(struct client *c)
881: {
882: struct paste_buffer *pb;
883: const char *bufdata;
884: size_t size, n, bufsize;
885: u_int i;
886: struct utf8_data *ud, *udp;
887: enum utf8_state more;
888:
889: size = utf8_strlen(c->prompt_buffer);
890: if (c->prompt_saved != NULL) {
891: ud = c->prompt_saved;
892: n = utf8_strlen(c->prompt_saved);
893: } else {
894: if ((pb = paste_get_top(NULL)) == NULL)
895: return (0);
896: bufdata = paste_buffer_data(pb, &bufsize);
1.200 nicm 897: ud = xreallocarray(NULL, bufsize + 1, sizeof *ud);
1.199 nicm 898: udp = ud;
899: for (i = 0; i != bufsize; /* nothing */) {
900: more = utf8_open(udp, bufdata[i]);
901: if (more == UTF8_MORE) {
902: while (++i != bufsize && more == UTF8_MORE)
903: more = utf8_append(udp, bufdata[i]);
904: if (more == UTF8_DONE) {
905: udp++;
906: continue;
907: }
908: i -= udp->have;
909: }
910: if (bufdata[i] <= 31 || bufdata[i] >= 127)
911: break;
912: utf8_set(udp, bufdata[i]);
913: udp++;
914: i++;
915: }
916: udp->size = 0;
917: n = udp - ud;
918: }
919: if (n == 0)
920: return (0);
921:
922: c->prompt_buffer = xreallocarray(c->prompt_buffer, size + n + 1,
923: sizeof *c->prompt_buffer);
924: if (c->prompt_index == size) {
925: memcpy(c->prompt_buffer + c->prompt_index, ud,
926: n * sizeof *c->prompt_buffer);
927: c->prompt_index += n;
928: c->prompt_buffer[c->prompt_index].size = 0;
929: } else {
930: memmove(c->prompt_buffer + c->prompt_index + n,
931: c->prompt_buffer + c->prompt_index,
932: (size + 1 - c->prompt_index) * sizeof *c->prompt_buffer);
933: memcpy(c->prompt_buffer + c->prompt_index, ud,
934: n * sizeof *c->prompt_buffer);
935: c->prompt_index += n;
936: }
937:
938: if (ud != c->prompt_saved)
939: free(ud);
940: return (1);
941: }
942:
1.204 nicm 943: /* Finish completion. */
944: static int
945: status_prompt_replace_complete(struct client *c, const char *s)
946: {
947: char word[64], *allocated = NULL;
948: size_t size, n, off, idx, used;
949: struct utf8_data *first, *last, *ud;
950:
1.205 nicm 951: /* Work out where the cursor currently is. */
1.204 nicm 952: idx = c->prompt_index;
953: if (idx != 0)
954: idx--;
1.205 nicm 955: size = utf8_strlen(c->prompt_buffer);
1.204 nicm 956:
957: /* Find the word we are in. */
958: first = &c->prompt_buffer[idx];
959: while (first > c->prompt_buffer && !status_prompt_space(first))
960: first--;
961: while (first->size != 0 && status_prompt_space(first))
962: first++;
963: last = &c->prompt_buffer[idx];
964: while (last->size != 0 && !status_prompt_space(last))
965: last++;
966: while (last > c->prompt_buffer && status_prompt_space(last))
967: last--;
968: if (last->size != 0)
969: last++;
1.205 nicm 970: if (last < first)
1.204 nicm 971: return (0);
972: if (s == NULL) {
973: used = 0;
974: for (ud = first; ud < last; ud++) {
975: if (used + ud->size >= sizeof word)
976: break;
977: memcpy(word + used, ud->data, ud->size);
978: used += ud->size;
979: }
980: if (ud != last)
981: return (0);
982: word[used] = '\0';
983: }
984:
985: /* Try to complete it. */
986: if (s == NULL) {
987: allocated = status_prompt_complete(c, word,
988: first - c->prompt_buffer);
989: if (allocated == NULL)
990: return (0);
991: s = allocated;
992: }
993:
994: /* Trim out word. */
995: n = size - (last - c->prompt_buffer) + 1; /* with \0 */
996: memmove(first, last, n * sizeof *c->prompt_buffer);
997: size -= last - first;
998:
999: /* Insert the new word. */
1000: size += strlen(s);
1001: off = first - c->prompt_buffer;
1002: c->prompt_buffer = xreallocarray(c->prompt_buffer, size + 1,
1003: sizeof *c->prompt_buffer);
1004: first = c->prompt_buffer + off;
1005: memmove(first + strlen(s), first, n * sizeof *c->prompt_buffer);
1006: for (idx = 0; idx < strlen(s); idx++)
1007: utf8_set(&first[idx], s[idx]);
1008: c->prompt_index = (first - c->prompt_buffer) + strlen(s);
1009:
1010: free(allocated);
1011: return (1);
1012: }
1013:
1.1 nicm 1014: /* Handle keys in prompt. */
1.154 nicm 1015: int
1.138 nicm 1016: status_prompt_key(struct client *c, key_code key)
1.1 nicm 1017: {
1.152 nicm 1018: struct options *oo = c->session->options;
1.204 nicm 1019: char *s, *cp, prefix = '=';
1.201 nicm 1020: const char *histstr, *ws = NULL, *keystring;
1.204 nicm 1021: size_t size, idx;
1022: struct utf8_data tmp;
1.155 nicm 1023: int keys;
1.1 nicm 1024:
1.201 nicm 1025: if (c->prompt_flags & PROMPT_KEY) {
1.213 nicm 1026: keystring = key_string_lookup_key(key, 0);
1.201 nicm 1027: c->prompt_inputcb(c, c->prompt_data, keystring, 1);
1028: status_prompt_clear(c);
1029: return (0);
1030: }
1.152 nicm 1031: size = utf8_strlen(c->prompt_buffer);
1.154 nicm 1032:
1033: if (c->prompt_flags & PROMPT_NUMERIC) {
1034: if (key >= '0' && key <= '9')
1035: goto append_key;
1036: s = utf8_tocstr(c->prompt_buffer);
1.166 nicm 1037: c->prompt_inputcb(c, c->prompt_data, s, 1);
1.154 nicm 1038: status_prompt_clear(c);
1039: free(s);
1040: return (1);
1041: }
1.213 nicm 1042: key &= ~KEYC_MASK_FLAGS;
1.154 nicm 1043:
1.155 nicm 1044: keys = options_get_number(c->session->options, "status-keys");
1045: if (keys == MODEKEY_VI) {
1046: switch (status_prompt_translate_key(c, key, &key)) {
1047: case 1:
1048: goto process_key;
1049: case 2:
1050: goto append_key;
1051: default:
1052: return (0);
1053: }
1054: }
1055:
1056: process_key:
1057: switch (key) {
1058: case KEYC_LEFT:
1059: case '\002': /* C-b */
1.1 nicm 1060: if (c->prompt_index > 0) {
1061: c->prompt_index--;
1.158 nicm 1062: break;
1.1 nicm 1063: }
1064: break;
1.155 nicm 1065: case KEYC_RIGHT:
1066: case '\006': /* C-f */
1.1 nicm 1067: if (c->prompt_index < size) {
1068: c->prompt_index++;
1.158 nicm 1069: break;
1.1 nicm 1070: }
1071: break;
1.155 nicm 1072: case KEYC_HOME:
1073: case '\001': /* C-a */
1.1 nicm 1074: if (c->prompt_index != 0) {
1075: c->prompt_index = 0;
1.158 nicm 1076: break;
1.1 nicm 1077: }
1078: break;
1.155 nicm 1079: case KEYC_END:
1080: case '\005': /* C-e */
1.1 nicm 1081: if (c->prompt_index != size) {
1082: c->prompt_index = size;
1.158 nicm 1083: break;
1.1 nicm 1084: }
1085: break;
1.155 nicm 1086: case '\011': /* Tab */
1.206 nicm 1087: if (status_prompt_replace_complete(c, NULL))
1.204 nicm 1088: goto changed;
1089: break;
1.155 nicm 1090: case KEYC_BSPACE:
1091: case '\010': /* C-h */
1.1 nicm 1092: if (c->prompt_index != 0) {
1093: if (c->prompt_index == size)
1.152 nicm 1094: c->prompt_buffer[--c->prompt_index].size = 0;
1.1 nicm 1095: else {
1096: memmove(c->prompt_buffer + c->prompt_index - 1,
1097: c->prompt_buffer + c->prompt_index,
1.152 nicm 1098: (size + 1 - c->prompt_index) *
1099: sizeof *c->prompt_buffer);
1.1 nicm 1100: c->prompt_index--;
1101: }
1.158 nicm 1102: goto changed;
1.1 nicm 1103: }
1104: break;
1.155 nicm 1105: case KEYC_DC:
1106: case '\004': /* C-d */
1.1 nicm 1107: if (c->prompt_index != size) {
1108: memmove(c->prompt_buffer + c->prompt_index,
1109: c->prompt_buffer + c->prompt_index + 1,
1.152 nicm 1110: (size + 1 - c->prompt_index) *
1111: sizeof *c->prompt_buffer);
1.158 nicm 1112: goto changed;
1.18 nicm 1113: }
1.26 nicm 1114: break;
1.155 nicm 1115: case '\025': /* C-u */
1.152 nicm 1116: c->prompt_buffer[0].size = 0;
1.26 nicm 1117: c->prompt_index = 0;
1.158 nicm 1118: goto changed;
1.155 nicm 1119: case '\013': /* C-k */
1.18 nicm 1120: if (c->prompt_index < size) {
1.152 nicm 1121: c->prompt_buffer[c->prompt_index].size = 0;
1.158 nicm 1122: goto changed;
1.1 nicm 1123: }
1124: break;
1.155 nicm 1125: case '\027': /* C-w */
1.152 nicm 1126: ws = options_get_string(oo, "word-separators");
1.81 nicm 1127: idx = c->prompt_index;
1128:
1129: /* Find a non-separator. */
1130: while (idx != 0) {
1131: idx--;
1.152 nicm 1132: if (!status_prompt_in_list(ws, &c->prompt_buffer[idx]))
1.81 nicm 1133: break;
1134: }
1135:
1136: /* Find the separator at the beginning of the word. */
1137: while (idx != 0) {
1138: idx--;
1.152 nicm 1139: if (status_prompt_in_list(ws, &c->prompt_buffer[idx])) {
1.81 nicm 1140: /* Go back to the word. */
1141: idx++;
1142: break;
1143: }
1144: }
1145:
1.181 nicm 1146: free(c->prompt_saved);
1147: c->prompt_saved = xcalloc(sizeof *c->prompt_buffer,
1148: (c->prompt_index - idx) + 1);
1149: memcpy(c->prompt_saved, c->prompt_buffer + idx,
1150: (c->prompt_index - idx) * sizeof *c->prompt_buffer);
1151:
1.81 nicm 1152: memmove(c->prompt_buffer + idx,
1153: c->prompt_buffer + c->prompt_index,
1.152 nicm 1154: (size + 1 - c->prompt_index) *
1155: sizeof *c->prompt_buffer);
1.81 nicm 1156: memset(c->prompt_buffer + size - (c->prompt_index - idx),
1.152 nicm 1157: '\0', (c->prompt_index - idx) * sizeof *c->prompt_buffer);
1.81 nicm 1158: c->prompt_index = idx;
1.152 nicm 1159:
1.158 nicm 1160: goto changed;
1.212 nicm 1161: case 'f'|KEYC_META:
1.180 nicm 1162: case KEYC_RIGHT|KEYC_CTRL:
1.155 nicm 1163: ws = options_get_string(oo, "word-separators");
1.81 nicm 1164:
1165: /* Find a word. */
1166: while (c->prompt_index != size) {
1.152 nicm 1167: idx = ++c->prompt_index;
1168: if (!status_prompt_in_list(ws, &c->prompt_buffer[idx]))
1.81 nicm 1169: break;
1170: }
1171:
1172: /* Find the separator at the end of the word. */
1173: while (c->prompt_index != size) {
1.152 nicm 1174: idx = ++c->prompt_index;
1175: if (status_prompt_in_list(ws, &c->prompt_buffer[idx]))
1.81 nicm 1176: break;
1177: }
1.106 nicm 1178:
1179: /* Back up to the end-of-word like vi. */
1180: if (options_get_number(oo, "status-keys") == MODEKEY_VI &&
1181: c->prompt_index != 0)
1182: c->prompt_index--;
1.81 nicm 1183:
1.158 nicm 1184: goto changed;
1.212 nicm 1185: case 'b'|KEYC_META:
1.180 nicm 1186: case KEYC_LEFT|KEYC_CTRL:
1.155 nicm 1187: ws = options_get_string(oo, "word-separators");
1.81 nicm 1188:
1189: /* Find a non-separator. */
1190: while (c->prompt_index != 0) {
1.152 nicm 1191: idx = --c->prompt_index;
1192: if (!status_prompt_in_list(ws, &c->prompt_buffer[idx]))
1.81 nicm 1193: break;
1194: }
1195:
1196: /* Find the separator at the beginning of the word. */
1197: while (c->prompt_index != 0) {
1.152 nicm 1198: idx = --c->prompt_index;
1199: if (status_prompt_in_list(ws, &c->prompt_buffer[idx])) {
1.81 nicm 1200: /* Go back to the word. */
1201: c->prompt_index++;
1202: break;
1203: }
1204: }
1.158 nicm 1205: goto changed;
1.155 nicm 1206: case KEYC_UP:
1207: case '\020': /* C-p */
1.66 nicm 1208: histstr = status_prompt_up_history(&c->prompt_hindex);
1209: if (histstr == NULL)
1.1 nicm 1210: break;
1.94 nicm 1211: free(c->prompt_buffer);
1.152 nicm 1212: c->prompt_buffer = utf8_fromcstr(histstr);
1213: c->prompt_index = utf8_strlen(c->prompt_buffer);
1.158 nicm 1214: goto changed;
1.155 nicm 1215: case KEYC_DOWN:
1216: case '\016': /* C-n */
1.66 nicm 1217: histstr = status_prompt_down_history(&c->prompt_hindex);
1218: if (histstr == NULL)
1.65 nicm 1219: break;
1.94 nicm 1220: free(c->prompt_buffer);
1.152 nicm 1221: c->prompt_buffer = utf8_fromcstr(histstr);
1222: c->prompt_index = utf8_strlen(c->prompt_buffer);
1.158 nicm 1223: goto changed;
1.155 nicm 1224: case '\031': /* C-y */
1.199 nicm 1225: if (status_prompt_paste(c))
1226: goto changed;
1227: break;
1.155 nicm 1228: case '\024': /* C-t */
1.30 nicm 1229: idx = c->prompt_index;
1230: if (idx < size)
1231: idx++;
1232: if (idx >= 2) {
1.152 nicm 1233: utf8_copy(&tmp, &c->prompt_buffer[idx - 2]);
1234: utf8_copy(&c->prompt_buffer[idx - 2],
1235: &c->prompt_buffer[idx - 1]);
1236: utf8_copy(&c->prompt_buffer[idx - 1], &tmp);
1.30 nicm 1237: c->prompt_index = idx;
1.158 nicm 1238: goto changed;
1.30 nicm 1239: }
1.1 nicm 1240: break;
1.155 nicm 1241: case '\r':
1242: case '\n':
1.152 nicm 1243: s = utf8_tocstr(c->prompt_buffer);
1244: if (*s != '\0')
1245: status_prompt_add_history(s);
1.166 nicm 1246: if (c->prompt_inputcb(c, c->prompt_data, s, 1) == 0)
1.25 nicm 1247: status_prompt_clear(c);
1.152 nicm 1248: free(s);
1.25 nicm 1249: break;
1.155 nicm 1250: case '\033': /* Escape */
1251: case '\003': /* C-c */
1.174 nicm 1252: case '\007': /* C-g */
1.166 nicm 1253: if (c->prompt_inputcb(c, c->prompt_data, NULL, 1) == 0)
1.1 nicm 1254: status_prompt_clear(c);
1255: break;
1.158 nicm 1256: case '\022': /* C-r */
1257: if (c->prompt_flags & PROMPT_INCREMENTAL) {
1258: prefix = '-';
1259: goto changed;
1260: }
1261: break;
1262: case '\023': /* C-s */
1263: if (c->prompt_flags & PROMPT_INCREMENTAL) {
1264: prefix = '+';
1265: goto changed;
1266: }
1267: break;
1268: default:
1269: goto append_key;
1.154 nicm 1270: }
1.152 nicm 1271:
1.177 nicm 1272: c->flags |= CLIENT_REDRAWSTATUS;
1.158 nicm 1273: return (0);
1274:
1.154 nicm 1275: append_key:
1276: if (key <= 0x1f || key >= KEYC_BASE)
1277: return (0);
1.216 nicm 1278: if (key <= 0x7f)
1.215 nicm 1279: utf8_set(&tmp, key);
1280: else
1281: utf8_to_data(key, &tmp);
1.1 nicm 1282:
1.154 nicm 1283: c->prompt_buffer = xreallocarray(c->prompt_buffer, size + 2,
1284: sizeof *c->prompt_buffer);
1.1 nicm 1285:
1.154 nicm 1286: if (c->prompt_index == size) {
1287: utf8_copy(&c->prompt_buffer[c->prompt_index], &tmp);
1288: c->prompt_index++;
1289: c->prompt_buffer[c->prompt_index].size = 0;
1290: } else {
1291: memmove(c->prompt_buffer + c->prompt_index + 1,
1292: c->prompt_buffer + c->prompt_index,
1293: (size + 1 - c->prompt_index) *
1294: sizeof *c->prompt_buffer);
1295: utf8_copy(&c->prompt_buffer[c->prompt_index], &tmp);
1296: c->prompt_index++;
1297: }
1.1 nicm 1298:
1.154 nicm 1299: if (c->prompt_flags & PROMPT_SINGLE) {
1300: s = utf8_tocstr(c->prompt_buffer);
1301: if (strlen(s) != 1)
1302: status_prompt_clear(c);
1.166 nicm 1303: else if (c->prompt_inputcb(c, c->prompt_data, s, 1) == 0)
1.154 nicm 1304: status_prompt_clear(c);
1305: free(s);
1.1 nicm 1306: }
1.154 nicm 1307:
1.158 nicm 1308: changed:
1.177 nicm 1309: c->flags |= CLIENT_REDRAWSTATUS;
1.158 nicm 1310: if (c->prompt_flags & PROMPT_INCREMENTAL) {
1311: s = utf8_tocstr(c->prompt_buffer);
1312: xasprintf(&cp, "%c%s", prefix, s);
1.166 nicm 1313: c->prompt_inputcb(c, c->prompt_data, cp, 0);
1.158 nicm 1314: free(cp);
1315: free(s);
1316: }
1.154 nicm 1317: return (0);
1.1 nicm 1318: }
1319:
1.65 nicm 1320: /* Get previous line from the history. */
1.151 nicm 1321: static const char *
1.65 nicm 1322: status_prompt_up_history(u_int *idx)
1323: {
1324: /*
1.128 nicm 1325: * History runs from 0 to size - 1. Index is from 0 to size. Zero is
1326: * empty.
1.65 nicm 1327: */
1328:
1.128 nicm 1329: if (status_prompt_hsize == 0 || *idx == status_prompt_hsize)
1.65 nicm 1330: return (NULL);
1331: (*idx)++;
1.128 nicm 1332: return (status_prompt_hlist[status_prompt_hsize - *idx]);
1.65 nicm 1333: }
1334:
1335: /* Get next line from the history. */
1.151 nicm 1336: static const char *
1.65 nicm 1337: status_prompt_down_history(u_int *idx)
1338: {
1.128 nicm 1339: if (status_prompt_hsize == 0 || *idx == 0)
1.65 nicm 1340: return ("");
1341: (*idx)--;
1342: if (*idx == 0)
1343: return ("");
1.128 nicm 1344: return (status_prompt_hlist[status_prompt_hsize - *idx]);
1.65 nicm 1345: }
1346:
1.1 nicm 1347: /* Add line to the history. */
1.151 nicm 1348: static void
1.65 nicm 1349: status_prompt_add_history(const char *line)
1.1 nicm 1350: {
1.128 nicm 1351: size_t size;
1.65 nicm 1352:
1.128 nicm 1353: if (status_prompt_hsize > 0 &&
1354: strcmp(status_prompt_hlist[status_prompt_hsize - 1], line) == 0)
1.1 nicm 1355: return;
1356:
1.128 nicm 1357: if (status_prompt_hsize == PROMPT_HISTORY) {
1358: free(status_prompt_hlist[0]);
1.1 nicm 1359:
1.128 nicm 1360: size = (PROMPT_HISTORY - 1) * sizeof *status_prompt_hlist;
1361: memmove(&status_prompt_hlist[0], &status_prompt_hlist[1], size);
1.1 nicm 1362:
1.128 nicm 1363: status_prompt_hlist[status_prompt_hsize - 1] = xstrdup(line);
1364: return;
1365: }
1.1 nicm 1366:
1.128 nicm 1367: status_prompt_hlist = xreallocarray(status_prompt_hlist,
1368: status_prompt_hsize + 1, sizeof *status_prompt_hlist);
1369: status_prompt_hlist[status_prompt_hsize++] = xstrdup(line);
1370: }
1371:
1372: /* Build completion list. */
1.204 nicm 1373: static char **
1374: status_prompt_complete_list(u_int *size, const char *s, int at_start)
1.128 nicm 1375: {
1.183 nicm 1376: char **list = NULL;
1377: const char **layout, *value, *cp;
1.128 nicm 1378: const struct cmd_entry **cmdent;
1379: const struct options_table_entry *oe;
1.183 nicm 1380: size_t slen = strlen(s), valuelen;
1381: struct options_entry *o;
1.191 nicm 1382: struct options_array_item *a;
1.128 nicm 1383: const char *layouts[] = {
1384: "even-horizontal", "even-vertical", "main-horizontal",
1385: "main-vertical", "tiled", NULL
1386: };
1.1 nicm 1387:
1.128 nicm 1388: *size = 0;
1.1 nicm 1389: for (cmdent = cmd_table; *cmdent != NULL; cmdent++) {
1.183 nicm 1390: if (strncmp((*cmdent)->name, s, slen) == 0) {
1.128 nicm 1391: list = xreallocarray(list, (*size) + 1, sizeof *list);
1.183 nicm 1392: list[(*size)++] = xstrdup((*cmdent)->name);
1.128 nicm 1393: }
1.205 nicm 1394: if ((*cmdent)->alias != NULL &&
1395: strncmp((*cmdent)->alias, s, slen) == 0) {
1396: list = xreallocarray(list, (*size) + 1, sizeof *list);
1397: list[(*size)++] = xstrdup((*cmdent)->alias);
1398: }
1.56 nicm 1399: }
1.183 nicm 1400: o = options_get_only(global_options, "command-alias");
1.191 nicm 1401: if (o != NULL) {
1402: a = options_array_first(o);
1403: while (a != NULL) {
1.195 nicm 1404: value = options_array_item_value(a)->string;
1.194 nicm 1405: if ((cp = strchr(value, '=')) == NULL)
1.196 nicm 1406: goto next;
1.183 nicm 1407: valuelen = cp - value;
1408: if (slen > valuelen || strncmp(value, s, slen) != 0)
1.191 nicm 1409: goto next;
1410:
1.183 nicm 1411: list = xreallocarray(list, (*size) + 1, sizeof *list);
1412: list[(*size)++] = xstrndup(value, valuelen);
1.191 nicm 1413:
1414: next:
1415: a = options_array_next(a);
1.183 nicm 1416: }
1417: }
1.204 nicm 1418: if (at_start)
1419: return (list);
1420:
1421: for (oe = options_table; oe->name != NULL; oe++) {
1422: if (strncmp(oe->name, s, slen) == 0) {
1423: list = xreallocarray(list, (*size) + 1, sizeof *list);
1424: list[(*size)++] = xstrdup(oe->name);
1425: }
1426: }
1427: for (layout = layouts; *layout != NULL; layout++) {
1428: if (strncmp(*layout, s, slen) == 0) {
1429: list = xreallocarray(list, (*size) + 1, sizeof *list);
1430: list[(*size)++] = xstrdup(*layout);
1431: }
1432: }
1.128 nicm 1433: return (list);
1434: }
1435:
1436: /* Find longest prefix. */
1.151 nicm 1437: static char *
1.183 nicm 1438: status_prompt_complete_prefix(char **list, u_int size)
1.128 nicm 1439: {
1440: char *out;
1441: u_int i;
1442: size_t j;
1443:
1.217 ! nicm 1444: if (list == NULL || size == 0)
! 1445: return (NULL);
1.128 nicm 1446: out = xstrdup(list[0]);
1447: for (i = 1; i < size; i++) {
1448: j = strlen(list[i]);
1449: if (j > strlen(out))
1450: j = strlen(out);
1451: for (; j > 0; j--) {
1452: if (out[j - 1] != list[i][j - 1])
1453: out[j - 1] = '\0';
1454: }
1.1 nicm 1455: }
1.128 nicm 1456: return (out);
1457: }
1.1 nicm 1458:
1.204 nicm 1459: /* Complete word menu callback. */
1460: static void
1461: status_prompt_menu_callback(__unused struct menu *menu, u_int idx, key_code key,
1462: void *data)
1463: {
1464: struct status_prompt_menu *spm = data;
1465: struct client *c = spm->c;
1466: u_int i;
1467: char *s;
1468:
1469: if (key != KEYC_NONE) {
1470: idx += spm->start;
1471: if (spm->flag == '\0')
1472: s = xstrdup(spm->list[idx]);
1473: else
1474: xasprintf(&s, "-%c%s", spm->flag, spm->list[idx]);
1.205 nicm 1475: if (c->prompt_flags & PROMPT_WINDOW) {
1476: free(c->prompt_buffer);
1477: c->prompt_buffer = utf8_fromcstr(s);
1478: c->prompt_index = utf8_strlen(c->prompt_buffer);
1479: c->flags |= CLIENT_REDRAWSTATUS;
1480: } else if (status_prompt_replace_complete(c, s))
1.204 nicm 1481: c->flags |= CLIENT_REDRAWSTATUS;
1482: free(s);
1483: }
1484:
1485: for (i = 0; i < spm->size; i++)
1486: free(spm->list[i]);
1487: free(spm->list);
1488: }
1489:
1490: /* Show complete word menu. */
1491: static int
1492: status_prompt_complete_list_menu(struct client *c, char **list, u_int size,
1493: u_int offset, char flag)
1494: {
1495: struct menu *menu;
1496: struct menu_item item;
1497: struct status_prompt_menu *spm;
1498: u_int lines = status_line_size(c), height, i;
1499: u_int py;
1500:
1501: if (size <= 1)
1502: return (0);
1503: if (c->tty.sy - lines < 3)
1504: return (0);
1505:
1506: spm = xmalloc(sizeof *spm);
1507: spm->c = c;
1508: spm->size = size;
1509: spm->list = list;
1510: spm->flag = flag;
1511:
1512: height = c->tty.sy - lines - 2;
1513: if (height > 10)
1514: height = 10;
1515: if (height > size)
1516: height = size;
1517: spm->start = size - height;
1518:
1519: menu = menu_create("");
1520: for (i = spm->start; i < size; i++) {
1521: item.name = list[i];
1522: item.key = '0' + (i - spm->start);
1523: item.command = NULL;
1524: menu_add_item(menu, &item, NULL, NULL, NULL);
1525: }
1526:
1527: if (options_get_number(c->session->options, "status-position") == 0)
1528: py = lines;
1529: else
1530: py = c->tty.sy - 3 - height;
1531: offset += utf8_cstrwidth(c->prompt_string);
1532: if (offset > 2)
1533: offset -= 2;
1534: else
1535: offset = 0;
1536:
1537: if (menu_display(menu, MENU_NOMOUSE|MENU_TAB, NULL, offset,
1538: py, c, NULL, status_prompt_menu_callback, spm) != 0) {
1539: menu_free(menu);
1540: free(spm);
1541: return (0);
1542: }
1543: return (1);
1544: }
1545:
1546: /* Show complete word menu. */
1547: static char *
1548: status_prompt_complete_window_menu(struct client *c, struct session *s,
1.206 nicm 1549: const char *word, u_int offset, char flag)
1.204 nicm 1550: {
1551: struct menu *menu;
1552: struct menu_item item;
1553: struct status_prompt_menu *spm;
1554: struct winlink *wl;
1555: char **list = NULL, *tmp;
1556: u_int lines = status_line_size(c), height;
1557: u_int py, size = 0;
1558:
1559: if (c->tty.sy - lines < 3)
1560: return (NULL);
1561:
1562: spm = xmalloc(sizeof *spm);
1563: spm->c = c;
1564: spm->flag = flag;
1565:
1566: height = c->tty.sy - lines - 2;
1567: if (height > 10)
1568: height = 10;
1569: spm->start = 0;
1570:
1571: menu = menu_create("");
1572: RB_FOREACH(wl, winlinks, &s->windows) {
1.206 nicm 1573: if (word != NULL && *word != '\0') {
1574: xasprintf(&tmp, "%d", wl->idx);
1575: if (strncmp(tmp, word, strlen(word)) != 0) {
1576: free(tmp);
1577: continue;
1578: }
1579: free(tmp);
1580: }
1581:
1.204 nicm 1582: list = xreallocarray(list, size + 1, sizeof *list);
1.205 nicm 1583: if (c->prompt_flags & PROMPT_WINDOW) {
1584: xasprintf(&tmp, "%d (%s)", wl->idx, wl->window->name);
1585: xasprintf(&list[size++], "%d", wl->idx);
1586: } else {
1587: xasprintf(&tmp, "%s:%d (%s)", s->name, wl->idx,
1588: wl->window->name);
1589: xasprintf(&list[size++], "%s:%d", s->name, wl->idx);
1590: }
1.204 nicm 1591: item.name = tmp;
1592: item.key = '0' + size - 1;
1593: item.command = NULL;
1594: menu_add_item(menu, &item, NULL, NULL, NULL);
1595: free(tmp);
1596:
1597: if (size == height)
1598: break;
1599: }
1.206 nicm 1600: if (size == 0) {
1601: menu_free(menu);
1602: return (NULL);
1603: }
1.204 nicm 1604: if (size == 1) {
1605: menu_free(menu);
1.205 nicm 1606: if (flag != '\0') {
1607: xasprintf(&tmp, "-%c%s", flag, list[0]);
1608: free(list[0]);
1609: } else
1610: tmp = list[0];
1.204 nicm 1611: free(list);
1612: return (tmp);
1613: }
1614: if (height > size)
1615: height = size;
1616:
1617: spm->size = size;
1618: spm->list = list;
1619:
1620: if (options_get_number(c->session->options, "status-position") == 0)
1621: py = lines;
1622: else
1623: py = c->tty.sy - 3 - height;
1624: offset += utf8_cstrwidth(c->prompt_string);
1625: if (offset > 2)
1626: offset -= 2;
1627: else
1628: offset = 0;
1629:
1630: if (menu_display(menu, MENU_NOMOUSE|MENU_TAB, NULL, offset,
1631: py, c, NULL, status_prompt_menu_callback, spm) != 0) {
1632: menu_free(menu);
1633: free(spm);
1634: return (NULL);
1635: }
1636: return (NULL);
1637: }
1638:
1639: /* Sort complete list. */
1640: static int
1641: status_prompt_complete_sort(const void *a, const void *b)
1642: {
1643: const char **aa = (const char **)a, **bb = (const char **)b;
1644:
1645: return (strcmp(*aa, *bb));
1646: }
1647:
1.205 nicm 1648: /* Complete a session. */
1649: static char *
1650: status_prompt_complete_session(char ***list, u_int *size, const char *s,
1651: char flag)
1652: {
1653: struct session *loop;
1.217 ! nicm 1654: char *out, *tmp, n[11];
1.205 nicm 1655:
1656: RB_FOREACH(loop, sessions, &sessions) {
1.217 ! nicm 1657: if (*s == '\0' || strncmp(loop->name, s, strlen(s)) == 0) {
! 1658: *list = xreallocarray(*list, (*size) + 2,
! 1659: sizeof **list);
! 1660: xasprintf(&(*list)[(*size)++], "%s:", loop->name);
! 1661: } else if (*s == '$') {
! 1662: xsnprintf(n, sizeof n, "%u", loop->id);
! 1663: if (s[1] == '\0' ||
! 1664: strncmp(n, s + 1, strlen(s) - 1) == 0) {
! 1665: *list = xreallocarray(*list, (*size) + 2,
! 1666: sizeof **list);
! 1667: xasprintf(&(*list)[(*size)++], "$%s:", n);
! 1668: }
! 1669: }
1.205 nicm 1670: }
1671: out = status_prompt_complete_prefix(*list, *size);
1672: if (out != NULL && flag != '\0') {
1673: xasprintf(&tmp, "-%c%s", flag, out);
1674: free(out);
1675: out = tmp;
1676: }
1677: return (out);
1678: }
1679:
1.128 nicm 1680: /* Complete word. */
1.151 nicm 1681: static char *
1.204 nicm 1682: status_prompt_complete(struct client *c, const char *word, u_int offset)
1.128 nicm 1683: {
1.217 ! nicm 1684: struct session *session;
1.204 nicm 1685: const char *s, *colon;
1.205 nicm 1686: char **list = NULL, *copy = NULL, *out = NULL;
1.204 nicm 1687: char flag = '\0';
1.128 nicm 1688: u_int size = 0, i;
1689:
1.206 nicm 1690: if (*word == '\0' &&
1691: ((c->prompt_flags & (PROMPT_TARGET|PROMPT_WINDOW)) == 0))
1.1 nicm 1692: return (NULL);
1.128 nicm 1693:
1.206 nicm 1694: if (((c->prompt_flags & (PROMPT_TARGET|PROMPT_WINDOW)) == 0) &&
1.205 nicm 1695: strncmp(word, "-t", 2) != 0 &&
1696: strncmp(word, "-s", 2) != 0) {
1.204 nicm 1697: list = status_prompt_complete_list(&size, word, offset == 0);
1.128 nicm 1698: if (size == 0)
1699: out = NULL;
1700: else if (size == 1)
1701: xasprintf(&out, "%s ", list[0]);
1702: else
1703: out = status_prompt_complete_prefix(list, size);
1.204 nicm 1704: goto found;
1.128 nicm 1705: }
1706:
1.206 nicm 1707: if (c->prompt_flags & (PROMPT_TARGET|PROMPT_WINDOW)) {
1.205 nicm 1708: s = word;
1709: flag = '\0';
1710: } else {
1711: s = word + 2;
1712: flag = word[1];
1713: offset += 2;
1714: }
1.206 nicm 1715:
1716: /* If this is a window completion, open the window menu. */
1717: if (c->prompt_flags & PROMPT_WINDOW) {
1718: out = status_prompt_complete_window_menu(c, c->session, s,
1719: offset, '\0');
1720: goto found;
1721: }
1.204 nicm 1722: colon = strchr(s, ':');
1.1 nicm 1723:
1.204 nicm 1724: /* If there is no colon, complete as a session. */
1725: if (colon == NULL) {
1.205 nicm 1726: out = status_prompt_complete_session(&list, &size, s, flag);
1.128 nicm 1727: goto found;
1728: }
1729:
1.204 nicm 1730: /* If there is a colon but no period, find session and show a menu. */
1731: if (strchr(colon + 1, '.') == NULL) {
1732: if (*s == ':')
1733: session = c->session;
1734: else {
1735: copy = xstrdup(s);
1736: *strchr(copy, ':') = '\0';
1737: session = session_find(copy);
1738: free(copy);
1739: if (session == NULL)
1740: goto found;
1741: }
1.206 nicm 1742: out = status_prompt_complete_window_menu(c, session, colon + 1,
1743: offset, flag);
1.204 nicm 1744: if (out == NULL)
1745: return (NULL);
1746: }
1.1 nicm 1747:
1.204 nicm 1748: found:
1749: if (size != 0) {
1750: qsort(list, size, sizeof *list, status_prompt_complete_sort);
1751: for (i = 0; i < size; i++)
1752: log_debug("complete %u: %s", i, list[i]);
1753: }
1.128 nicm 1754:
1.204 nicm 1755: if (out != NULL && strcmp(word, out) == 0) {
1756: free(out);
1757: out = NULL;
1.1 nicm 1758: }
1.204 nicm 1759: if (out != NULL ||
1760: !status_prompt_complete_list_menu(c, list, size, offset, flag)) {
1761: for (i = 0; i < size; i++)
1762: free(list[i]);
1763: free(list);
1.128 nicm 1764: }
1765: return (out);
1.1 nicm 1766: }