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