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