Annotation of src/usr.bin/tmux/status.c, Revision 1.205
1.205 ! nicm 1: /* $OpenBSD: status.c,v 1.204 2020/05/16 15:06:03 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 *,
! 42: struct session *, 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.48 nicm 349: /* Set up default colour. */
1.203 nicm 350: style_apply(&gc, s->options, "status-style", NULL);
351: fg = options_get_number(s->options, "status-fg");
352: if (fg != 8)
353: gc.fg = fg;
354: bg = options_get_number(s->options, "status-bg");
355: if (bg != 8)
356: gc.bg = bg;
1.192 nicm 357: if (!grid_cells_equal(&gc, &sl->style)) {
358: force = 1;
359: memcpy(&sl->style, &gc, sizeof sl->style);
360: }
361:
362: /* Resize the target screen. */
363: if (screen_size_x(&sl->screen) != width ||
364: screen_size_y(&sl->screen) != lines) {
365: screen_resize(&sl->screen, width, lines, 0);
1.200 nicm 366: changed = force = 1;
1.1 nicm 367: }
1.192 nicm 368: screen_write_start(&ctx, NULL, &sl->screen);
1.1 nicm 369:
1.192 nicm 370: /* Create format tree. */
371: flags = FORMAT_STATUS;
372: if (c->flags & CLIENT_STATUSFORCE)
373: flags |= FORMAT_FORCE;
374: ft = format_create(c, NULL, FORMAT_NONE, flags);
375: format_defaults(ft, c, NULL, NULL, NULL);
1.55 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.10 nicm 426: status_message_set(struct client *c, 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);
436: xvasprintf(&c->message_string, fmt, ap);
437: va_end(ap);
438:
1.162 nicm 439: server_client_add_message(c, "%s", c->message_string);
1.44 nicm 440:
1.137 nicm 441: delay = options_get_number(c->session->options, "display-time");
1.143 tim 442: if (delay > 0) {
443: tv.tv_sec = delay / 1000;
444: tv.tv_usec = (delay % 1000) * 1000L;
445:
446: if (event_initialized(&c->message_timer))
447: evtimer_del(&c->message_timer);
448: evtimer_set(&c->message_timer, status_message_callback, c);
449: evtimer_add(&c->message_timer, &tv);
450: }
1.1 nicm 451:
452: c->tty.flags |= (TTY_NOCURSOR|TTY_FREEZE);
1.177 nicm 453: c->flags |= CLIENT_REDRAWSTATUS;
1.1 nicm 454: }
455:
1.46 nicm 456: /* Clear status line message. */
1.1 nicm 457: void
458: status_message_clear(struct client *c)
459: {
460: if (c->message_string == NULL)
461: return;
462:
1.94 nicm 463: free(c->message_string);
1.1 nicm 464: c->message_string = NULL;
465:
1.156 nicm 466: if (c->prompt_string == NULL)
467: c->tty.flags &= ~(TTY_NOCURSOR|TTY_FREEZE);
1.177 nicm 468: c->flags |= CLIENT_ALLREDRAWFLAGS; /* was frozen and may have changed */
1.7 nicm 469:
1.189 nicm 470: status_pop_screen(c);
1.42 nicm 471: }
472:
1.46 nicm 473: /* Clear status line message after timer expires. */
1.151 nicm 474: static void
1.141 nicm 475: status_message_callback(__unused int fd, __unused short event, void *data)
1.42 nicm 476: {
477: struct client *c = data;
478:
479: status_message_clear(c);
1.1 nicm 480: }
481:
482: /* Draw client message on status line of present else on last line. */
483: int
484: status_message_redraw(struct client *c)
485: {
1.189 nicm 486: struct status_line *sl = &c->status;
487: struct screen_write_ctx ctx;
488: struct session *s = c->session;
489: struct screen old_screen;
490: size_t len;
491: u_int lines, offset;
492: struct grid_cell gc;
1.1 nicm 493:
494: if (c->tty.sx == 0 || c->tty.sy == 0)
495: return (0);
1.189 nicm 496: memcpy(&old_screen, sl->active, sizeof old_screen);
1.169 nicm 497:
1.182 nicm 498: lines = status_line_size(c);
1.189 nicm 499: if (lines <= 1)
1.173 nicm 500: lines = 1;
1.190 nicm 501: screen_init(sl->active, c->tty.sx, lines, 0);
1.1 nicm 502:
1.139 nicm 503: len = screen_write_strlen("%s", c->message_string);
1.1 nicm 504: if (len > c->tty.sx)
505: len = c->tty.sx;
506:
1.203 nicm 507: style_apply(&gc, s->options, "message-style", NULL);
1.1 nicm 508:
1.189 nicm 509: screen_write_start(&ctx, NULL, sl->active);
1.192 nicm 510: screen_write_fast_copy(&ctx, &sl->screen, 0, 0, c->tty.sx, lines - 1);
511: screen_write_cursormove(&ctx, 0, lines - 1, 0);
512: for (offset = 0; offset < c->tty.sx; offset++)
1.170 nicm 513: screen_write_putc(&ctx, &gc, ' ');
1.184 nicm 514: screen_write_cursormove(&ctx, 0, lines - 1, 0);
1.139 nicm 515: screen_write_nputs(&ctx, len, &gc, "%s", c->message_string);
1.1 nicm 516: screen_write_stop(&ctx);
517:
1.189 nicm 518: if (grid_compare(sl->active->grid, old_screen.grid) == 0) {
519: screen_free(&old_screen);
1.1 nicm 520: return (0);
521: }
1.189 nicm 522: screen_free(&old_screen);
1.1 nicm 523: return (1);
524: }
525:
1.46 nicm 526: /* Enable status line prompt. */
1.1 nicm 527: void
1.76 nicm 528: status_prompt_set(struct client *c, const char *msg, const char *input,
1.166 nicm 529: prompt_input_cb inputcb, prompt_free_cb freecb, void *data, int flags)
1.1 nicm 530: {
1.122 nicm 531: struct format_tree *ft;
1.165 nicm 532: char *tmp, *cp;
1.122 nicm 533:
1.164 nicm 534: ft = format_create(c, NULL, FORMAT_NONE, 0);
1.122 nicm 535: format_defaults(ft, c, NULL, NULL, NULL);
1.152 nicm 536:
1.168 nicm 537: if (input == NULL)
538: input = "";
539: if (flags & PROMPT_NOFORMAT)
540: tmp = xstrdup(input);
541: else
1.185 nicm 542: tmp = format_expand_time(ft, input);
1.20 nicm 543:
1.12 nicm 544: status_message_clear(c);
545: status_prompt_clear(c);
1.189 nicm 546: status_push_screen(c);
1.12 nicm 547:
1.185 nicm 548: c->prompt_string = format_expand_time(ft, msg);
1.1 nicm 549:
1.152 nicm 550: c->prompt_buffer = utf8_fromcstr(tmp);
551: c->prompt_index = utf8_strlen(c->prompt_buffer);
1.1 nicm 552:
1.166 nicm 553: c->prompt_inputcb = inputcb;
554: c->prompt_freecb = freecb;
1.1 nicm 555: c->prompt_data = data;
556:
557: c->prompt_hindex = 0;
558:
559: c->prompt_flags = flags;
1.155 nicm 560: c->prompt_mode = PROMPT_ENTRY;
1.1 nicm 561:
1.158 nicm 562: if (~flags & PROMPT_INCREMENTAL)
563: c->tty.flags |= (TTY_NOCURSOR|TTY_FREEZE);
1.177 nicm 564: c->flags |= CLIENT_REDRAWSTATUS;
1.165 nicm 565:
566: if ((flags & PROMPT_INCREMENTAL) && *tmp != '\0') {
567: xasprintf(&cp, "=%s", tmp);
1.166 nicm 568: c->prompt_inputcb(c, c->prompt_data, cp, 0);
1.165 nicm 569: free(cp);
570: }
1.122 nicm 571:
1.152 nicm 572: free(tmp);
1.122 nicm 573: format_free(ft);
1.1 nicm 574: }
575:
1.46 nicm 576: /* Remove status line prompt. */
1.1 nicm 577: void
578: status_prompt_clear(struct client *c)
579: {
1.55 nicm 580: if (c->prompt_string == NULL)
1.1 nicm 581: return;
582:
1.166 nicm 583: if (c->prompt_freecb != NULL && c->prompt_data != NULL)
584: c->prompt_freecb(c->prompt_data);
1.1 nicm 585:
1.94 nicm 586: free(c->prompt_string);
1.1 nicm 587: c->prompt_string = NULL;
588:
1.94 nicm 589: free(c->prompt_buffer);
1.1 nicm 590: c->prompt_buffer = NULL;
591:
1.181 nicm 592: free(c->prompt_saved);
593: c->prompt_saved = NULL;
594:
1.1 nicm 595: c->tty.flags &= ~(TTY_NOCURSOR|TTY_FREEZE);
1.177 nicm 596: c->flags |= CLIENT_ALLREDRAWFLAGS; /* was frozen and may have changed */
1.7 nicm 597:
1.189 nicm 598: status_pop_screen(c);
1.27 nicm 599: }
600:
1.46 nicm 601: /* Update status line prompt with a new prompt string. */
1.27 nicm 602: void
1.76 nicm 603: status_prompt_update(struct client *c, const char *msg, const char *input)
1.27 nicm 604: {
1.122 nicm 605: struct format_tree *ft;
1.152 nicm 606: char *tmp;
1.122 nicm 607:
1.164 nicm 608: ft = format_create(c, NULL, FORMAT_NONE, 0);
1.122 nicm 609: format_defaults(ft, c, NULL, NULL, NULL);
1.152 nicm 610:
1.185 nicm 611: tmp = format_expand_time(ft, input);
1.122 nicm 612:
1.94 nicm 613: free(c->prompt_string);
1.185 nicm 614: c->prompt_string = format_expand_time(ft, msg);
1.27 nicm 615:
1.94 nicm 616: free(c->prompt_buffer);
1.152 nicm 617: c->prompt_buffer = utf8_fromcstr(tmp);
618: c->prompt_index = utf8_strlen(c->prompt_buffer);
1.27 nicm 619:
620: c->prompt_hindex = 0;
621:
1.177 nicm 622: c->flags |= CLIENT_REDRAWSTATUS;
1.122 nicm 623:
1.152 nicm 624: free(tmp);
1.122 nicm 625: format_free(ft);
1.1 nicm 626: }
627:
628: /* Draw client prompt on status line of present else on last line. */
629: int
630: status_prompt_redraw(struct client *c)
631: {
1.189 nicm 632: struct status_line *sl = &c->status;
1.152 nicm 633: struct screen_write_ctx ctx;
634: struct session *s = c->session;
1.189 nicm 635: struct screen old_screen;
636: u_int i, lines, offset, left, start, width;
637: u_int pcursor, pwidth;
1.152 nicm 638: struct grid_cell gc, cursorgc;
1.1 nicm 639:
640: if (c->tty.sx == 0 || c->tty.sy == 0)
641: return (0);
1.189 nicm 642: memcpy(&old_screen, sl->active, sizeof old_screen);
1.169 nicm 643:
1.182 nicm 644: lines = status_line_size(c);
1.189 nicm 645: if (lines <= 1)
1.173 nicm 646: lines = 1;
1.189 nicm 647: screen_init(sl->active, c->tty.sx, lines, 0);
1.1 nicm 648:
1.155 nicm 649: if (c->prompt_mode == PROMPT_COMMAND)
1.203 nicm 650: style_apply(&gc, s->options, "message-command-style", NULL);
1.108 nicm 651: else
1.203 nicm 652: style_apply(&gc, s->options, "message-style", NULL);
1.1 nicm 653:
1.152 nicm 654: memcpy(&cursorgc, &gc, sizeof cursorgc);
655: cursorgc.attr ^= GRID_ATTR_REVERSE;
656:
657: start = screen_write_strlen("%s", c->prompt_string);
658: if (start > c->tty.sx)
659: start = c->tty.sx;
660:
1.189 nicm 661: screen_write_start(&ctx, NULL, sl->active);
1.192 nicm 662: screen_write_fast_copy(&ctx, &sl->screen, 0, 0, c->tty.sx, lines - 1);
663: screen_write_cursormove(&ctx, 0, lines - 1, 0);
664: for (offset = 0; offset < c->tty.sx; offset++)
1.170 nicm 665: screen_write_putc(&ctx, &gc, ' ');
1.192 nicm 666: screen_write_cursormove(&ctx, 0, lines - 1, 0);
1.152 nicm 667: screen_write_nputs(&ctx, start, &gc, "%s", c->prompt_string);
1.192 nicm 668: screen_write_cursormove(&ctx, start, lines - 1, 0);
1.1 nicm 669:
1.152 nicm 670: left = c->tty.sx - start;
671: if (left == 0)
672: goto finished;
673:
674: pcursor = utf8_strwidth(c->prompt_buffer, c->prompt_index);
675: pwidth = utf8_strwidth(c->prompt_buffer, -1);
676: if (pcursor >= left) {
677: /*
678: * The cursor would be outside the screen so start drawing
679: * with it on the right.
680: */
681: offset = (pcursor - left) + 1;
682: pwidth = left;
683: } else
684: offset = 0;
685: if (pwidth > left)
686: pwidth = left;
687:
688: width = 0;
689: for (i = 0; c->prompt_buffer[i].size != 0; i++) {
690: if (width < offset) {
691: width += c->prompt_buffer[i].width;
692: continue;
1.1 nicm 693: }
1.152 nicm 694: if (width >= offset + pwidth)
695: break;
696: width += c->prompt_buffer[i].width;
697: if (width > offset + pwidth)
698: break;
1.1 nicm 699:
1.152 nicm 700: if (i != c->prompt_index) {
701: utf8_copy(&gc.data, &c->prompt_buffer[i]);
702: screen_write_cell(&ctx, &gc);
703: } else {
704: utf8_copy(&cursorgc.data, &c->prompt_buffer[i]);
705: screen_write_cell(&ctx, &cursorgc);
706: }
1.1 nicm 707: }
1.189 nicm 708: if (sl->active->cx < screen_size_x(sl->active) && c->prompt_index >= i)
1.152 nicm 709: screen_write_putc(&ctx, &cursorgc, ' ');
1.1 nicm 710:
1.152 nicm 711: finished:
1.1 nicm 712: screen_write_stop(&ctx);
1.51 nicm 713:
1.189 nicm 714: if (grid_compare(sl->active->grid, old_screen.grid) == 0) {
715: screen_free(&old_screen);
1.1 nicm 716: return (0);
717: }
1.189 nicm 718: screen_free(&old_screen);
1.1 nicm 719: return (1);
720: }
721:
1.152 nicm 722: /* Is this a separator? */
723: static int
724: status_prompt_in_list(const char *ws, const struct utf8_data *ud)
725: {
726: if (ud->size != 1 || ud->width != 1)
727: return (0);
728: return (strchr(ws, *ud->data) != NULL);
729: }
730:
731: /* Is this a space? */
732: static int
733: status_prompt_space(const struct utf8_data *ud)
734: {
735: if (ud->size != 1 || ud->width != 1)
736: return (0);
737: return (*ud->data == ' ');
738: }
739:
1.155 nicm 740: /*
741: * Translate key from emacs to vi. Return 0 to drop key, 1 to process the key
742: * as an emacs key; return 2 to append to the buffer.
743: */
744: static int
745: status_prompt_translate_key(struct client *c, key_code key, key_code *new_key)
746: {
747: if (c->prompt_mode == PROMPT_ENTRY) {
748: switch (key) {
749: case '\003': /* C-c */
1.202 nicm 750: case '\007': /* C-g */
1.155 nicm 751: case '\010': /* C-h */
752: case '\011': /* Tab */
753: case '\025': /* C-u */
754: case '\027': /* C-w */
755: case '\n':
756: case '\r':
757: case KEYC_BSPACE:
758: case KEYC_DC:
759: case KEYC_DOWN:
760: case KEYC_END:
761: case KEYC_HOME:
762: case KEYC_LEFT:
763: case KEYC_RIGHT:
764: case KEYC_UP:
765: *new_key = key;
766: return (1);
767: case '\033': /* Escape */
768: c->prompt_mode = PROMPT_COMMAND;
1.177 nicm 769: c->flags |= CLIENT_REDRAWSTATUS;
1.155 nicm 770: return (0);
771: }
772: *new_key = key;
773: return (2);
774: }
775:
776: switch (key) {
777: case 'A':
778: case 'I':
779: case 'C':
780: case 's':
781: case 'a':
782: c->prompt_mode = PROMPT_ENTRY;
1.177 nicm 783: c->flags |= CLIENT_REDRAWSTATUS;
1.155 nicm 784: break; /* switch mode and... */
785: case 'S':
786: c->prompt_mode = PROMPT_ENTRY;
1.177 nicm 787: c->flags |= CLIENT_REDRAWSTATUS;
1.155 nicm 788: *new_key = '\025'; /* C-u */
789: return (1);
790: case 'i':
791: case '\033': /* Escape */
792: c->prompt_mode = PROMPT_ENTRY;
1.177 nicm 793: c->flags |= CLIENT_REDRAWSTATUS;
1.155 nicm 794: return (0);
795: }
796:
797: switch (key) {
798: case 'A':
799: case '$':
800: *new_key = KEYC_END;
801: return (1);
802: case 'I':
803: case '0':
804: case '^':
805: *new_key = KEYC_HOME;
806: return (1);
807: case 'C':
808: case 'D':
809: *new_key = '\013'; /* C-k */
810: return (1);
811: case KEYC_BSPACE:
812: case 'X':
813: *new_key = KEYC_BSPACE;
814: return (1);
815: case 'b':
816: case 'B':
817: *new_key = 'b'|KEYC_ESCAPE;
818: return (1);
819: case 'd':
820: *new_key = '\025';
821: return (1);
822: case 'e':
823: case 'E':
824: case 'w':
825: case 'W':
826: *new_key = 'f'|KEYC_ESCAPE;
827: return (1);
828: case 'p':
829: *new_key = '\031'; /* C-y */
1.202 nicm 830: return (1);
831: case 'q':
832: *new_key = '\003'; /* C-c */
1.155 nicm 833: return (1);
834: case 's':
835: case KEYC_DC:
836: case 'x':
837: *new_key = KEYC_DC;
838: return (1);
839: case KEYC_DOWN:
840: case 'j':
841: *new_key = KEYC_DOWN;
842: return (1);
843: case KEYC_LEFT:
844: case 'h':
845: *new_key = KEYC_LEFT;
846: return (1);
847: case 'a':
848: case KEYC_RIGHT:
849: case 'l':
850: *new_key = KEYC_RIGHT;
851: return (1);
852: case KEYC_UP:
853: case 'k':
854: *new_key = KEYC_UP;
855: return (1);
856: case '\010' /* C-h */:
857: case '\003' /* C-c */:
858: case '\n':
859: case '\r':
860: return (1);
861: }
862: return (0);
863: }
864:
1.199 nicm 865: /* Paste into prompt. */
866: static int
867: status_prompt_paste(struct client *c)
868: {
869: struct paste_buffer *pb;
870: const char *bufdata;
871: size_t size, n, bufsize;
872: u_int i;
873: struct utf8_data *ud, *udp;
874: enum utf8_state more;
875:
876: size = utf8_strlen(c->prompt_buffer);
877: if (c->prompt_saved != NULL) {
878: ud = c->prompt_saved;
879: n = utf8_strlen(c->prompt_saved);
880: } else {
881: if ((pb = paste_get_top(NULL)) == NULL)
882: return (0);
883: bufdata = paste_buffer_data(pb, &bufsize);
1.200 nicm 884: ud = xreallocarray(NULL, bufsize + 1, sizeof *ud);
1.199 nicm 885: udp = ud;
886: for (i = 0; i != bufsize; /* nothing */) {
887: more = utf8_open(udp, bufdata[i]);
888: if (more == UTF8_MORE) {
889: while (++i != bufsize && more == UTF8_MORE)
890: more = utf8_append(udp, bufdata[i]);
891: if (more == UTF8_DONE) {
892: udp++;
893: continue;
894: }
895: i -= udp->have;
896: }
897: if (bufdata[i] <= 31 || bufdata[i] >= 127)
898: break;
899: utf8_set(udp, bufdata[i]);
900: udp++;
901: i++;
902: }
903: udp->size = 0;
904: n = udp - ud;
905: }
906: if (n == 0)
907: return (0);
908:
909: c->prompt_buffer = xreallocarray(c->prompt_buffer, size + n + 1,
910: sizeof *c->prompt_buffer);
911: if (c->prompt_index == size) {
912: memcpy(c->prompt_buffer + c->prompt_index, ud,
913: n * sizeof *c->prompt_buffer);
914: c->prompt_index += n;
915: c->prompt_buffer[c->prompt_index].size = 0;
916: } else {
917: memmove(c->prompt_buffer + c->prompt_index + n,
918: c->prompt_buffer + c->prompt_index,
919: (size + 1 - c->prompt_index) * sizeof *c->prompt_buffer);
920: memcpy(c->prompt_buffer + c->prompt_index, ud,
921: n * sizeof *c->prompt_buffer);
922: c->prompt_index += n;
923: }
924:
925: if (ud != c->prompt_saved)
926: free(ud);
927: return (1);
928: }
929:
1.204 nicm 930: /* Finish completion. */
931: static int
932: status_prompt_replace_complete(struct client *c, const char *s)
933: {
934: char word[64], *allocated = NULL;
935: size_t size, n, off, idx, used;
936: struct utf8_data *first, *last, *ud;
937:
1.205 ! nicm 938: /* Work out where the cursor currently is. */
1.204 nicm 939: idx = c->prompt_index;
940: if (idx != 0)
941: idx--;
1.205 ! nicm 942: size = utf8_strlen(c->prompt_buffer);
1.204 nicm 943:
944: /* Find the word we are in. */
945: first = &c->prompt_buffer[idx];
946: while (first > c->prompt_buffer && !status_prompt_space(first))
947: first--;
948: while (first->size != 0 && status_prompt_space(first))
949: first++;
950: last = &c->prompt_buffer[idx];
951: while (last->size != 0 && !status_prompt_space(last))
952: last++;
953: while (last > c->prompt_buffer && status_prompt_space(last))
954: last--;
955: if (last->size != 0)
956: last++;
1.205 ! nicm 957: if (last < first)
1.204 nicm 958: return (0);
959: if (s == NULL) {
960: used = 0;
961: for (ud = first; ud < last; ud++) {
962: if (used + ud->size >= sizeof word)
963: break;
964: memcpy(word + used, ud->data, ud->size);
965: used += ud->size;
966: }
967: if (ud != last)
968: return (0);
969: word[used] = '\0';
970: }
971:
972: /* Try to complete it. */
973: if (s == NULL) {
974: allocated = status_prompt_complete(c, word,
975: first - c->prompt_buffer);
976: if (allocated == NULL)
977: return (0);
978: s = allocated;
979: }
980:
981: /* Trim out word. */
982: n = size - (last - c->prompt_buffer) + 1; /* with \0 */
983: memmove(first, last, n * sizeof *c->prompt_buffer);
984: size -= last - first;
985:
986: /* Insert the new word. */
987: size += strlen(s);
988: off = first - c->prompt_buffer;
989: c->prompt_buffer = xreallocarray(c->prompt_buffer, size + 1,
990: sizeof *c->prompt_buffer);
991: first = c->prompt_buffer + off;
992: memmove(first + strlen(s), first, n * sizeof *c->prompt_buffer);
993: for (idx = 0; idx < strlen(s); idx++)
994: utf8_set(&first[idx], s[idx]);
995: c->prompt_index = (first - c->prompt_buffer) + strlen(s);
996:
997: free(allocated);
998: return (1);
999: }
1000:
1.1 nicm 1001: /* Handle keys in prompt. */
1.154 nicm 1002: int
1.138 nicm 1003: status_prompt_key(struct client *c, key_code key)
1.1 nicm 1004: {
1.152 nicm 1005: struct options *oo = c->session->options;
1.204 nicm 1006: char *s, *cp, prefix = '=';
1.201 nicm 1007: const char *histstr, *ws = NULL, *keystring;
1.204 nicm 1008: size_t size, idx;
1009: struct utf8_data tmp;
1.155 nicm 1010: int keys;
1.1 nicm 1011:
1.201 nicm 1012: if (c->prompt_flags & PROMPT_KEY) {
1013: keystring = key_string_lookup_key(key);
1014: c->prompt_inputcb(c, c->prompt_data, keystring, 1);
1015: status_prompt_clear(c);
1016: return (0);
1017: }
1.152 nicm 1018: size = utf8_strlen(c->prompt_buffer);
1.154 nicm 1019:
1020: if (c->prompt_flags & PROMPT_NUMERIC) {
1021: if (key >= '0' && key <= '9')
1022: goto append_key;
1023: s = utf8_tocstr(c->prompt_buffer);
1.166 nicm 1024: c->prompt_inputcb(c, c->prompt_data, s, 1);
1.154 nicm 1025: status_prompt_clear(c);
1026: free(s);
1027: return (1);
1028: }
1.180 nicm 1029: key &= ~KEYC_XTERM;
1.154 nicm 1030:
1.155 nicm 1031: keys = options_get_number(c->session->options, "status-keys");
1032: if (keys == MODEKEY_VI) {
1033: switch (status_prompt_translate_key(c, key, &key)) {
1034: case 1:
1035: goto process_key;
1036: case 2:
1037: goto append_key;
1038: default:
1039: return (0);
1040: }
1041: }
1042:
1043: process_key:
1044: switch (key) {
1045: case KEYC_LEFT:
1046: case '\002': /* C-b */
1.1 nicm 1047: if (c->prompt_index > 0) {
1048: c->prompt_index--;
1.158 nicm 1049: break;
1.1 nicm 1050: }
1051: break;
1.155 nicm 1052: case KEYC_RIGHT:
1053: case '\006': /* C-f */
1.1 nicm 1054: if (c->prompt_index < size) {
1055: c->prompt_index++;
1.158 nicm 1056: break;
1.1 nicm 1057: }
1058: break;
1.155 nicm 1059: case KEYC_HOME:
1060: case '\001': /* C-a */
1.1 nicm 1061: if (c->prompt_index != 0) {
1062: c->prompt_index = 0;
1.158 nicm 1063: break;
1.1 nicm 1064: }
1065: break;
1.155 nicm 1066: case KEYC_END:
1067: case '\005': /* C-e */
1.1 nicm 1068: if (c->prompt_index != size) {
1069: c->prompt_index = size;
1.158 nicm 1070: break;
1.1 nicm 1071: }
1072: break;
1.155 nicm 1073: case '\011': /* Tab */
1.205 ! nicm 1074: if (c->prompt_flags & PROMPT_WINDOW) {
! 1075: s = status_prompt_complete_window_menu(c, c->session,
! 1076: 0, '\0');
! 1077: if (s != NULL) {
! 1078: free(c->prompt_buffer);
! 1079: c->prompt_buffer = utf8_fromcstr(s);
! 1080: c->prompt_index = utf8_strlen(c->prompt_buffer);
! 1081: }
! 1082: } else if (status_prompt_replace_complete(c, NULL))
1.204 nicm 1083: goto changed;
1084: break;
1.155 nicm 1085: case KEYC_BSPACE:
1086: case '\010': /* C-h */
1.1 nicm 1087: if (c->prompt_index != 0) {
1088: if (c->prompt_index == size)
1.152 nicm 1089: c->prompt_buffer[--c->prompt_index].size = 0;
1.1 nicm 1090: else {
1091: memmove(c->prompt_buffer + c->prompt_index - 1,
1092: c->prompt_buffer + c->prompt_index,
1.152 nicm 1093: (size + 1 - c->prompt_index) *
1094: sizeof *c->prompt_buffer);
1.1 nicm 1095: c->prompt_index--;
1096: }
1.158 nicm 1097: goto changed;
1.1 nicm 1098: }
1099: break;
1.155 nicm 1100: case KEYC_DC:
1101: case '\004': /* C-d */
1.1 nicm 1102: if (c->prompt_index != size) {
1103: memmove(c->prompt_buffer + c->prompt_index,
1104: c->prompt_buffer + c->prompt_index + 1,
1.152 nicm 1105: (size + 1 - c->prompt_index) *
1106: sizeof *c->prompt_buffer);
1.158 nicm 1107: goto changed;
1.18 nicm 1108: }
1.26 nicm 1109: break;
1.155 nicm 1110: case '\025': /* C-u */
1.152 nicm 1111: c->prompt_buffer[0].size = 0;
1.26 nicm 1112: c->prompt_index = 0;
1.158 nicm 1113: goto changed;
1.155 nicm 1114: case '\013': /* C-k */
1.18 nicm 1115: if (c->prompt_index < size) {
1.152 nicm 1116: c->prompt_buffer[c->prompt_index].size = 0;
1.158 nicm 1117: goto changed;
1.1 nicm 1118: }
1119: break;
1.155 nicm 1120: case '\027': /* C-w */
1.152 nicm 1121: ws = options_get_string(oo, "word-separators");
1.81 nicm 1122: idx = c->prompt_index;
1123:
1124: /* Find a non-separator. */
1125: while (idx != 0) {
1126: idx--;
1.152 nicm 1127: if (!status_prompt_in_list(ws, &c->prompt_buffer[idx]))
1.81 nicm 1128: break;
1129: }
1130:
1131: /* Find the separator at the beginning of the word. */
1132: while (idx != 0) {
1133: idx--;
1.152 nicm 1134: if (status_prompt_in_list(ws, &c->prompt_buffer[idx])) {
1.81 nicm 1135: /* Go back to the word. */
1136: idx++;
1137: break;
1138: }
1139: }
1140:
1.181 nicm 1141: free(c->prompt_saved);
1142: c->prompt_saved = xcalloc(sizeof *c->prompt_buffer,
1143: (c->prompt_index - idx) + 1);
1144: memcpy(c->prompt_saved, c->prompt_buffer + idx,
1145: (c->prompt_index - idx) * sizeof *c->prompt_buffer);
1146:
1.81 nicm 1147: memmove(c->prompt_buffer + idx,
1148: c->prompt_buffer + c->prompt_index,
1.152 nicm 1149: (size + 1 - c->prompt_index) *
1150: sizeof *c->prompt_buffer);
1.81 nicm 1151: memset(c->prompt_buffer + size - (c->prompt_index - idx),
1.152 nicm 1152: '\0', (c->prompt_index - idx) * sizeof *c->prompt_buffer);
1.81 nicm 1153: c->prompt_index = idx;
1.152 nicm 1154:
1.158 nicm 1155: goto changed;
1.155 nicm 1156: case 'f'|KEYC_ESCAPE:
1.180 nicm 1157: case KEYC_RIGHT|KEYC_CTRL:
1.155 nicm 1158: ws = options_get_string(oo, "word-separators");
1.81 nicm 1159:
1160: /* Find a word. */
1161: while (c->prompt_index != size) {
1.152 nicm 1162: idx = ++c->prompt_index;
1163: if (!status_prompt_in_list(ws, &c->prompt_buffer[idx]))
1.81 nicm 1164: break;
1165: }
1166:
1167: /* Find the separator at the end of the word. */
1168: while (c->prompt_index != size) {
1.152 nicm 1169: idx = ++c->prompt_index;
1170: if (status_prompt_in_list(ws, &c->prompt_buffer[idx]))
1.81 nicm 1171: break;
1172: }
1.106 nicm 1173:
1174: /* Back up to the end-of-word like vi. */
1175: if (options_get_number(oo, "status-keys") == MODEKEY_VI &&
1176: c->prompt_index != 0)
1177: c->prompt_index--;
1.81 nicm 1178:
1.158 nicm 1179: goto changed;
1.155 nicm 1180: case 'b'|KEYC_ESCAPE:
1.180 nicm 1181: case KEYC_LEFT|KEYC_CTRL:
1.155 nicm 1182: ws = options_get_string(oo, "word-separators");
1.81 nicm 1183:
1184: /* Find a non-separator. */
1185: while (c->prompt_index != 0) {
1.152 nicm 1186: idx = --c->prompt_index;
1187: if (!status_prompt_in_list(ws, &c->prompt_buffer[idx]))
1.81 nicm 1188: break;
1189: }
1190:
1191: /* Find the separator at the beginning of the word. */
1192: while (c->prompt_index != 0) {
1.152 nicm 1193: idx = --c->prompt_index;
1194: if (status_prompt_in_list(ws, &c->prompt_buffer[idx])) {
1.81 nicm 1195: /* Go back to the word. */
1196: c->prompt_index++;
1197: break;
1198: }
1199: }
1.158 nicm 1200: goto changed;
1.155 nicm 1201: case KEYC_UP:
1202: case '\020': /* C-p */
1.66 nicm 1203: histstr = status_prompt_up_history(&c->prompt_hindex);
1204: if (histstr == NULL)
1.1 nicm 1205: break;
1.94 nicm 1206: free(c->prompt_buffer);
1.152 nicm 1207: c->prompt_buffer = utf8_fromcstr(histstr);
1208: c->prompt_index = utf8_strlen(c->prompt_buffer);
1.158 nicm 1209: goto changed;
1.155 nicm 1210: case KEYC_DOWN:
1211: case '\016': /* C-n */
1.66 nicm 1212: histstr = status_prompt_down_history(&c->prompt_hindex);
1213: if (histstr == NULL)
1.65 nicm 1214: break;
1.94 nicm 1215: free(c->prompt_buffer);
1.152 nicm 1216: c->prompt_buffer = utf8_fromcstr(histstr);
1217: c->prompt_index = utf8_strlen(c->prompt_buffer);
1.158 nicm 1218: goto changed;
1.155 nicm 1219: case '\031': /* C-y */
1.199 nicm 1220: if (status_prompt_paste(c))
1221: goto changed;
1222: break;
1.155 nicm 1223: case '\024': /* C-t */
1.30 nicm 1224: idx = c->prompt_index;
1225: if (idx < size)
1226: idx++;
1227: if (idx >= 2) {
1.152 nicm 1228: utf8_copy(&tmp, &c->prompt_buffer[idx - 2]);
1229: utf8_copy(&c->prompt_buffer[idx - 2],
1230: &c->prompt_buffer[idx - 1]);
1231: utf8_copy(&c->prompt_buffer[idx - 1], &tmp);
1.30 nicm 1232: c->prompt_index = idx;
1.158 nicm 1233: goto changed;
1.30 nicm 1234: }
1.1 nicm 1235: break;
1.155 nicm 1236: case '\r':
1237: case '\n':
1.152 nicm 1238: s = utf8_tocstr(c->prompt_buffer);
1239: if (*s != '\0')
1240: status_prompt_add_history(s);
1.166 nicm 1241: if (c->prompt_inputcb(c, c->prompt_data, s, 1) == 0)
1.25 nicm 1242: status_prompt_clear(c);
1.152 nicm 1243: free(s);
1.25 nicm 1244: break;
1.155 nicm 1245: case '\033': /* Escape */
1246: case '\003': /* C-c */
1.174 nicm 1247: case '\007': /* C-g */
1.166 nicm 1248: if (c->prompt_inputcb(c, c->prompt_data, NULL, 1) == 0)
1.1 nicm 1249: status_prompt_clear(c);
1250: break;
1.158 nicm 1251: case '\022': /* C-r */
1252: if (c->prompt_flags & PROMPT_INCREMENTAL) {
1253: prefix = '-';
1254: goto changed;
1255: }
1256: break;
1257: case '\023': /* C-s */
1258: if (c->prompt_flags & PROMPT_INCREMENTAL) {
1259: prefix = '+';
1260: goto changed;
1261: }
1262: break;
1263: default:
1264: goto append_key;
1.154 nicm 1265: }
1.152 nicm 1266:
1.177 nicm 1267: c->flags |= CLIENT_REDRAWSTATUS;
1.158 nicm 1268: return (0);
1269:
1.154 nicm 1270: append_key:
1271: if (key <= 0x1f || key >= KEYC_BASE)
1272: return (0);
1273: if (utf8_split(key, &tmp) != UTF8_DONE)
1274: return (0);
1.1 nicm 1275:
1.154 nicm 1276: c->prompt_buffer = xreallocarray(c->prompt_buffer, size + 2,
1277: sizeof *c->prompt_buffer);
1.1 nicm 1278:
1.154 nicm 1279: if (c->prompt_index == size) {
1280: utf8_copy(&c->prompt_buffer[c->prompt_index], &tmp);
1281: c->prompt_index++;
1282: c->prompt_buffer[c->prompt_index].size = 0;
1283: } else {
1284: memmove(c->prompt_buffer + c->prompt_index + 1,
1285: c->prompt_buffer + c->prompt_index,
1286: (size + 1 - c->prompt_index) *
1287: sizeof *c->prompt_buffer);
1288: utf8_copy(&c->prompt_buffer[c->prompt_index], &tmp);
1289: c->prompt_index++;
1290: }
1.1 nicm 1291:
1.154 nicm 1292: if (c->prompt_flags & PROMPT_SINGLE) {
1293: s = utf8_tocstr(c->prompt_buffer);
1294: if (strlen(s) != 1)
1295: status_prompt_clear(c);
1.166 nicm 1296: else if (c->prompt_inputcb(c, c->prompt_data, s, 1) == 0)
1.154 nicm 1297: status_prompt_clear(c);
1298: free(s);
1.1 nicm 1299: }
1.154 nicm 1300:
1.158 nicm 1301: changed:
1.177 nicm 1302: c->flags |= CLIENT_REDRAWSTATUS;
1.158 nicm 1303: if (c->prompt_flags & PROMPT_INCREMENTAL) {
1304: s = utf8_tocstr(c->prompt_buffer);
1305: xasprintf(&cp, "%c%s", prefix, s);
1.166 nicm 1306: c->prompt_inputcb(c, c->prompt_data, cp, 0);
1.158 nicm 1307: free(cp);
1308: free(s);
1309: }
1.154 nicm 1310: return (0);
1.1 nicm 1311: }
1312:
1.65 nicm 1313: /* Get previous line from the history. */
1.151 nicm 1314: static const char *
1.65 nicm 1315: status_prompt_up_history(u_int *idx)
1316: {
1317: /*
1.128 nicm 1318: * History runs from 0 to size - 1. Index is from 0 to size. Zero is
1319: * empty.
1.65 nicm 1320: */
1321:
1.128 nicm 1322: if (status_prompt_hsize == 0 || *idx == status_prompt_hsize)
1.65 nicm 1323: return (NULL);
1324: (*idx)++;
1.128 nicm 1325: return (status_prompt_hlist[status_prompt_hsize - *idx]);
1.65 nicm 1326: }
1327:
1328: /* Get next line from the history. */
1.151 nicm 1329: static const char *
1.65 nicm 1330: status_prompt_down_history(u_int *idx)
1331: {
1.128 nicm 1332: if (status_prompt_hsize == 0 || *idx == 0)
1.65 nicm 1333: return ("");
1334: (*idx)--;
1335: if (*idx == 0)
1336: return ("");
1.128 nicm 1337: return (status_prompt_hlist[status_prompt_hsize - *idx]);
1.65 nicm 1338: }
1339:
1.1 nicm 1340: /* Add line to the history. */
1.151 nicm 1341: static void
1.65 nicm 1342: status_prompt_add_history(const char *line)
1.1 nicm 1343: {
1.128 nicm 1344: size_t size;
1.65 nicm 1345:
1.128 nicm 1346: if (status_prompt_hsize > 0 &&
1347: strcmp(status_prompt_hlist[status_prompt_hsize - 1], line) == 0)
1.1 nicm 1348: return;
1349:
1.128 nicm 1350: if (status_prompt_hsize == PROMPT_HISTORY) {
1351: free(status_prompt_hlist[0]);
1.1 nicm 1352:
1.128 nicm 1353: size = (PROMPT_HISTORY - 1) * sizeof *status_prompt_hlist;
1354: memmove(&status_prompt_hlist[0], &status_prompt_hlist[1], size);
1.1 nicm 1355:
1.128 nicm 1356: status_prompt_hlist[status_prompt_hsize - 1] = xstrdup(line);
1357: return;
1358: }
1.1 nicm 1359:
1.128 nicm 1360: status_prompt_hlist = xreallocarray(status_prompt_hlist,
1361: status_prompt_hsize + 1, sizeof *status_prompt_hlist);
1362: status_prompt_hlist[status_prompt_hsize++] = xstrdup(line);
1363: }
1364:
1365: /* Build completion list. */
1.204 nicm 1366: static char **
1367: status_prompt_complete_list(u_int *size, const char *s, int at_start)
1.128 nicm 1368: {
1.183 nicm 1369: char **list = NULL;
1370: const char **layout, *value, *cp;
1.128 nicm 1371: const struct cmd_entry **cmdent;
1372: const struct options_table_entry *oe;
1.183 nicm 1373: size_t slen = strlen(s), valuelen;
1374: struct options_entry *o;
1.191 nicm 1375: struct options_array_item *a;
1.128 nicm 1376: const char *layouts[] = {
1377: "even-horizontal", "even-vertical", "main-horizontal",
1378: "main-vertical", "tiled", NULL
1379: };
1.1 nicm 1380:
1.128 nicm 1381: *size = 0;
1.1 nicm 1382: for (cmdent = cmd_table; *cmdent != NULL; cmdent++) {
1.183 nicm 1383: if (strncmp((*cmdent)->name, s, slen) == 0) {
1.128 nicm 1384: list = xreallocarray(list, (*size) + 1, sizeof *list);
1.183 nicm 1385: list[(*size)++] = xstrdup((*cmdent)->name);
1.128 nicm 1386: }
1.205 ! nicm 1387: if ((*cmdent)->alias != NULL &&
! 1388: strncmp((*cmdent)->alias, s, slen) == 0) {
! 1389: list = xreallocarray(list, (*size) + 1, sizeof *list);
! 1390: list[(*size)++] = xstrdup((*cmdent)->alias);
! 1391: }
1.56 nicm 1392: }
1.183 nicm 1393: o = options_get_only(global_options, "command-alias");
1.191 nicm 1394: if (o != NULL) {
1395: a = options_array_first(o);
1396: while (a != NULL) {
1.195 nicm 1397: value = options_array_item_value(a)->string;
1.194 nicm 1398: if ((cp = strchr(value, '=')) == NULL)
1.196 nicm 1399: goto next;
1.183 nicm 1400: valuelen = cp - value;
1401: if (slen > valuelen || strncmp(value, s, slen) != 0)
1.191 nicm 1402: goto next;
1403:
1.183 nicm 1404: list = xreallocarray(list, (*size) + 1, sizeof *list);
1405: list[(*size)++] = xstrndup(value, valuelen);
1.191 nicm 1406:
1407: next:
1408: a = options_array_next(a);
1.183 nicm 1409: }
1410: }
1.204 nicm 1411: if (at_start)
1412: return (list);
1413:
1414: for (oe = options_table; oe->name != NULL; oe++) {
1415: if (strncmp(oe->name, s, slen) == 0) {
1416: list = xreallocarray(list, (*size) + 1, sizeof *list);
1417: list[(*size)++] = xstrdup(oe->name);
1418: }
1419: }
1420: for (layout = layouts; *layout != NULL; layout++) {
1421: if (strncmp(*layout, s, slen) == 0) {
1422: list = xreallocarray(list, (*size) + 1, sizeof *list);
1423: list[(*size)++] = xstrdup(*layout);
1424: }
1425: }
1.128 nicm 1426: return (list);
1427: }
1428:
1429: /* Find longest prefix. */
1.151 nicm 1430: static char *
1.183 nicm 1431: status_prompt_complete_prefix(char **list, u_int size)
1.128 nicm 1432: {
1433: char *out;
1434: u_int i;
1435: size_t j;
1436:
1437: out = xstrdup(list[0]);
1438: for (i = 1; i < size; i++) {
1439: j = strlen(list[i]);
1440: if (j > strlen(out))
1441: j = strlen(out);
1442: for (; j > 0; j--) {
1443: if (out[j - 1] != list[i][j - 1])
1444: out[j - 1] = '\0';
1445: }
1.1 nicm 1446: }
1.128 nicm 1447: return (out);
1448: }
1.1 nicm 1449:
1.204 nicm 1450: /* Complete word menu callback. */
1451: static void
1452: status_prompt_menu_callback(__unused struct menu *menu, u_int idx, key_code key,
1453: void *data)
1454: {
1455: struct status_prompt_menu *spm = data;
1456: struct client *c = spm->c;
1457: u_int i;
1458: char *s;
1459:
1460: if (key != KEYC_NONE) {
1461: idx += spm->start;
1462: if (spm->flag == '\0')
1463: s = xstrdup(spm->list[idx]);
1464: else
1465: xasprintf(&s, "-%c%s", spm->flag, spm->list[idx]);
1.205 ! nicm 1466: if (c->prompt_flags & PROMPT_WINDOW) {
! 1467: free(c->prompt_buffer);
! 1468: c->prompt_buffer = utf8_fromcstr(s);
! 1469: c->prompt_index = utf8_strlen(c->prompt_buffer);
! 1470: c->flags |= CLIENT_REDRAWSTATUS;
! 1471: } else if (status_prompt_replace_complete(c, s))
1.204 nicm 1472: c->flags |= CLIENT_REDRAWSTATUS;
1473: free(s);
1474: }
1475:
1476: for (i = 0; i < spm->size; i++)
1477: free(spm->list[i]);
1478: free(spm->list);
1479: }
1480:
1481: /* Show complete word menu. */
1482: static int
1483: status_prompt_complete_list_menu(struct client *c, char **list, u_int size,
1484: u_int offset, char flag)
1485: {
1486: struct menu *menu;
1487: struct menu_item item;
1488: struct status_prompt_menu *spm;
1489: u_int lines = status_line_size(c), height, i;
1490: u_int py;
1491:
1492: if (size <= 1)
1493: return (0);
1494: if (c->tty.sy - lines < 3)
1495: return (0);
1496:
1497: spm = xmalloc(sizeof *spm);
1498: spm->c = c;
1499: spm->size = size;
1500: spm->list = list;
1501: spm->flag = flag;
1502:
1503: height = c->tty.sy - lines - 2;
1504: if (height > 10)
1505: height = 10;
1506: if (height > size)
1507: height = size;
1508: spm->start = size - height;
1509:
1510: menu = menu_create("");
1511: for (i = spm->start; i < size; i++) {
1512: item.name = list[i];
1513: item.key = '0' + (i - spm->start);
1514: item.command = NULL;
1515: menu_add_item(menu, &item, NULL, NULL, NULL);
1516: }
1517:
1518: if (options_get_number(c->session->options, "status-position") == 0)
1519: py = lines;
1520: else
1521: py = c->tty.sy - 3 - height;
1522: offset += utf8_cstrwidth(c->prompt_string);
1523: if (offset > 2)
1524: offset -= 2;
1525: else
1526: offset = 0;
1527:
1528: if (menu_display(menu, MENU_NOMOUSE|MENU_TAB, NULL, offset,
1529: py, c, NULL, status_prompt_menu_callback, spm) != 0) {
1530: menu_free(menu);
1531: free(spm);
1532: return (0);
1533: }
1534: return (1);
1535: }
1536:
1537: /* Show complete word menu. */
1538: static char *
1539: status_prompt_complete_window_menu(struct client *c, struct session *s,
1540: u_int offset, char flag)
1541: {
1542: struct menu *menu;
1543: struct menu_item item;
1544: struct status_prompt_menu *spm;
1545: struct winlink *wl;
1546: char **list = NULL, *tmp;
1547: u_int lines = status_line_size(c), height;
1548: u_int py, size = 0;
1549:
1550: if (c->tty.sy - lines < 3)
1551: return (NULL);
1552:
1553: spm = xmalloc(sizeof *spm);
1554: spm->c = c;
1555: spm->flag = flag;
1556:
1557: height = c->tty.sy - lines - 2;
1558: if (height > 10)
1559: height = 10;
1560: spm->start = 0;
1561:
1562: menu = menu_create("");
1563: RB_FOREACH(wl, winlinks, &s->windows) {
1564: list = xreallocarray(list, size + 1, sizeof *list);
1.205 ! nicm 1565: if (c->prompt_flags & PROMPT_WINDOW) {
! 1566: xasprintf(&tmp, "%d (%s)", wl->idx, wl->window->name);
! 1567: xasprintf(&list[size++], "%d", wl->idx);
! 1568: } else {
! 1569: xasprintf(&tmp, "%s:%d (%s)", s->name, wl->idx,
! 1570: wl->window->name);
! 1571: xasprintf(&list[size++], "%s:%d", s->name, wl->idx);
! 1572: }
1.204 nicm 1573: item.name = tmp;
1574: item.key = '0' + size - 1;
1575: item.command = NULL;
1576: menu_add_item(menu, &item, NULL, NULL, NULL);
1577: free(tmp);
1578:
1579: if (size == height)
1580: break;
1581: }
1582: if (size == 1) {
1583: menu_free(menu);
1.205 ! nicm 1584: if (flag != '\0') {
! 1585: xasprintf(&tmp, "-%c%s", flag, list[0]);
! 1586: free(list[0]);
! 1587: } else
! 1588: tmp = list[0];
1.204 nicm 1589: free(list);
1590: return (tmp);
1591: }
1592: if (height > size)
1593: height = size;
1594:
1595: spm->size = size;
1596: spm->list = list;
1597:
1598: if (options_get_number(c->session->options, "status-position") == 0)
1599: py = lines;
1600: else
1601: py = c->tty.sy - 3 - height;
1602: offset += utf8_cstrwidth(c->prompt_string);
1603: if (offset > 2)
1604: offset -= 2;
1605: else
1606: offset = 0;
1607:
1608: if (menu_display(menu, MENU_NOMOUSE|MENU_TAB, NULL, offset,
1609: py, c, NULL, status_prompt_menu_callback, spm) != 0) {
1610: menu_free(menu);
1611: free(spm);
1612: return (NULL);
1613: }
1614: return (NULL);
1615: }
1616:
1617: /* Sort complete list. */
1618: static int
1619: status_prompt_complete_sort(const void *a, const void *b)
1620: {
1621: const char **aa = (const char **)a, **bb = (const char **)b;
1622:
1623: return (strcmp(*aa, *bb));
1624: }
1625:
1.205 ! nicm 1626: /* Complete a session. */
! 1627: static char *
! 1628: status_prompt_complete_session(char ***list, u_int *size, const char *s,
! 1629: char flag)
! 1630: {
! 1631: struct session *loop;
! 1632: char *out, *tmp;
! 1633:
! 1634: RB_FOREACH(loop, sessions, &sessions) {
! 1635: if (*s != '\0' && strncmp(loop->name, s, strlen(s)) != 0)
! 1636: continue;
! 1637: *list = xreallocarray(*list, (*size) + 2, sizeof **list);
! 1638: xasprintf(&(*list)[(*size)++], "%s:", loop->name);
! 1639: }
! 1640: out = status_prompt_complete_prefix(*list, *size);
! 1641: if (out != NULL && flag != '\0') {
! 1642: xasprintf(&tmp, "-%c%s", flag, out);
! 1643: free(out);
! 1644: out = tmp;
! 1645: }
! 1646: return (out);
! 1647: }
! 1648:
1.128 nicm 1649: /* Complete word. */
1.151 nicm 1650: static char *
1.204 nicm 1651: status_prompt_complete(struct client *c, const char *word, u_int offset)
1.128 nicm 1652: {
1.205 ! nicm 1653: struct session *session;
1.204 nicm 1654: const char *s, *colon;
1.205 ! nicm 1655: char **list = NULL, *copy = NULL, *out = NULL;
1.204 nicm 1656: char flag = '\0';
1.128 nicm 1657: u_int size = 0, i;
1658:
1.205 ! nicm 1659: if (*word == '\0' && (~c->prompt_flags & PROMPT_TARGET))
1.1 nicm 1660: return (NULL);
1.128 nicm 1661:
1.205 ! nicm 1662: if ((~c->prompt_flags & PROMPT_TARGET) &&
! 1663: strncmp(word, "-t", 2) != 0 &&
! 1664: strncmp(word, "-s", 2) != 0) {
1.204 nicm 1665: list = status_prompt_complete_list(&size, word, offset == 0);
1.128 nicm 1666: if (size == 0)
1667: out = NULL;
1668: else if (size == 1)
1669: xasprintf(&out, "%s ", list[0]);
1670: else
1671: out = status_prompt_complete_prefix(list, size);
1.204 nicm 1672: goto found;
1.128 nicm 1673: }
1674:
1.205 ! nicm 1675: if (c->prompt_flags & PROMPT_TARGET) {
! 1676: s = word;
! 1677: flag = '\0';
! 1678: } else {
! 1679: s = word + 2;
! 1680: flag = word[1];
! 1681: offset += 2;
! 1682: }
1.204 nicm 1683: colon = strchr(s, ':');
1.1 nicm 1684:
1.204 nicm 1685: /* If there is no colon, complete as a session. */
1686: if (colon == NULL) {
1.205 ! nicm 1687: out = status_prompt_complete_session(&list, &size, s, flag);
1.128 nicm 1688: goto found;
1689: }
1690:
1.204 nicm 1691: /* If there is a colon but no period, find session and show a menu. */
1692: if (strchr(colon + 1, '.') == NULL) {
1693: if (*s == ':')
1694: session = c->session;
1695: else {
1696: copy = xstrdup(s);
1697: *strchr(copy, ':') = '\0';
1698: session = session_find(copy);
1699: free(copy);
1700: if (session == NULL)
1701: goto found;
1702: }
1703: out = status_prompt_complete_window_menu(c, session, offset,
1704: flag);
1705: if (out == NULL)
1706: return (NULL);
1707: }
1.1 nicm 1708:
1.204 nicm 1709: found:
1710: if (size != 0) {
1711: qsort(list, size, sizeof *list, status_prompt_complete_sort);
1712: for (i = 0; i < size; i++)
1713: log_debug("complete %u: %s", i, list[i]);
1714: }
1.128 nicm 1715:
1.204 nicm 1716: if (out != NULL && strcmp(word, out) == 0) {
1717: free(out);
1718: out = NULL;
1.1 nicm 1719: }
1.204 nicm 1720: if (out != NULL ||
1721: !status_prompt_complete_list_menu(c, list, size, offset, flag)) {
1722: for (i = 0; i < size; i++)
1723: free(list[i]);
1724: free(list);
1.128 nicm 1725: }
1726: return (out);
1.1 nicm 1727: }