Merge branch 'obsd-master'
[tmux.git] / status.c
blob2f64845d2b0f2aed814289b52f18f433dfe64f1e
1 /* $OpenBSD$ */
3 /*
4 * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
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.
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.
19 #include <sys/types.h>
20 #include <sys/time.h>
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>
30 #include "tmux.h"
32 static void status_message_callback(int, short, void *);
33 static void status_timer_callback(int, short, void *);
35 static char *status_prompt_find_history_file(void);
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);
40 static char *status_prompt_complete(struct client *, const char *, u_int);
41 static char *status_prompt_complete_window_menu(struct client *,
42 struct session *, const char *, u_int, char);
44 struct status_prompt_menu {
45 struct client *c;
46 u_int start;
47 u_int size;
48 char **list;
49 char flag;
52 static const char *prompt_type_strings[] = {
53 "command",
54 "search",
55 "target",
56 "window-target"
59 /* Status prompt history. */
60 char **status_prompt_hlist[PROMPT_NTYPES];
61 u_int status_prompt_hsize[PROMPT_NTYPES];
63 /* Find the history file to load/save from/to. */
64 static char *
65 status_prompt_find_history_file(void)
67 const char *home, *history_file;
68 char *path;
70 history_file = options_get_string(global_options, "history-file");
71 if (*history_file == '\0')
72 return (NULL);
73 if (*history_file == '/')
74 return (xstrdup(history_file));
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);
84 /* Add loaded history item to the appropriate list. */
85 static void
86 status_prompt_add_typed_history(char *line)
88 char *typestr;
89 enum prompt_type type = PROMPT_TYPE_INVALID;
91 typestr = strsep(&line, ":");
92 if (line != NULL)
93 type = status_prompt_type(typestr);
94 if (type == PROMPT_TYPE_INVALID) {
96 * Invalid types are not expected, but this provides backward
97 * compatibility with old history files.
99 if (line != NULL)
100 *(--line) = ':';
101 status_prompt_add_history(typestr, PROMPT_TYPE_COMMAND);
102 } else
103 status_prompt_add_history(line, type);
106 /* Load status prompt history from file. */
107 void
108 status_prompt_load_history(void)
110 FILE *f;
111 char *history_file, *line, *tmp;
112 size_t length;
114 if ((history_file = status_prompt_find_history_file()) == NULL)
115 return;
116 log_debug("loading history from %s", history_file);
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;
124 free(history_file);
126 for (;;) {
127 if ((line = fgetln(f, &length)) == NULL)
128 break;
130 if (length > 0) {
131 if (line[length - 1] == '\n') {
132 line[length - 1] = '\0';
133 status_prompt_add_typed_history(line);
134 } else {
135 tmp = xmalloc(length + 1);
136 memcpy(tmp, line, length);
137 tmp[length] = '\0';
138 status_prompt_add_typed_history(tmp);
139 free(tmp);
143 fclose(f);
146 /* Save status prompt history to file. */
147 void
148 status_prompt_save_history(void)
150 FILE *f;
151 u_int i, type;
152 char *history_file;
154 if ((history_file = status_prompt_find_history_file()) == NULL)
155 return;
156 log_debug("saving history to %s", history_file);
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;
164 free(history_file);
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);
174 fclose(f);
178 /* Status timer callback. */
179 static void
180 status_timer_callback(__unused int fd, __unused short events, void *arg)
182 struct client *c = arg;
183 struct session *s = c->session;
184 struct timeval tv;
186 evtimer_del(&c->status.timer);
188 if (s == NULL)
189 return;
191 if (c->message_string == NULL && c->prompt_string == NULL)
192 c->flags |= CLIENT_REDRAWSTATUS;
194 timerclear(&tv);
195 tv.tv_sec = options_get_number(s->options, "status-interval");
197 if (tv.tv_sec != 0)
198 evtimer_add(&c->status.timer, &tv);
199 log_debug("client %p, status interval %d", c, (int)tv.tv_sec);
202 /* Start status timer for client. */
203 void
204 status_timer_start(struct client *c)
206 struct session *s = c->session;
208 if (event_initialized(&c->status.timer))
209 evtimer_del(&c->status.timer);
210 else
211 evtimer_set(&c->status.timer, status_timer_callback, c);
213 if (s != NULL && options_get_number(s->options, "status"))
214 status_timer_callback(-1, 0, c);
217 /* Start status timer for all clients. */
218 void
219 status_timer_start_all(void)
221 struct client *c;
223 TAILQ_FOREACH(c, &clients, entry)
224 status_timer_start(c);
227 /* Update status cache. */
228 void
229 status_update_cache(struct session *s)
231 s->statuslines = options_get_number(s->options, "status");
232 if (s->statuslines == 0)
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;
240 /* Get screen line of status line. -1 means off. */
242 status_at_line(struct client *c)
244 struct session *s = c->session;
246 if (c->flags & (CLIENT_STATUSOFF|CLIENT_CONTROL))
247 return (-1);
248 if (s->statusat != 1)
249 return (s->statusat);
250 return (c->tty.sy - status_line_size(c));
253 /* Get size of status line for client's session. 0 means off. */
254 u_int
255 status_line_size(struct client *c)
257 struct session *s = c->session;
259 if (c->flags & (CLIENT_STATUSOFF|CLIENT_CONTROL))
260 return (0);
261 if (s == NULL)
262 return (options_get_number(global_s_options, "status"));
263 return (s->statuslines);
266 /* Get the prompt line number for client's session. 1 means at the bottom. */
267 static u_int
268 status_prompt_line_at(struct client *c)
270 struct session *s = c->session;
272 if (c->flags & (CLIENT_STATUSOFF|CLIENT_CONTROL))
273 return (1);
274 return (options_get_number(s->options, "message-line"));
277 /* Get window at window list position. */
278 struct style_range *
279 status_get_range(struct client *c, u_int x, u_int y)
281 struct status_line *sl = &c->status;
282 struct style_range *sr;
284 if (y >= nitems(sl->entries))
285 return (NULL);
286 TAILQ_FOREACH(sr, &sl->entries[y].ranges, entry) {
287 if (x >= sr->start && x < sr->end)
288 return (sr);
290 return (NULL);
293 /* Free all ranges. */
294 static void
295 status_free_ranges(struct style_ranges *srs)
297 struct style_range *sr, *sr1;
299 TAILQ_FOREACH_SAFE(sr, srs, entry, sr1) {
300 TAILQ_REMOVE(srs, sr, entry);
301 free(sr);
305 /* Save old status line. */
306 static void
307 status_push_screen(struct client *c)
309 struct status_line *sl = &c->status;
311 if (sl->active == &sl->screen) {
312 sl->active = xmalloc(sizeof *sl->active);
313 screen_init(sl->active, c->tty.sx, status_line_size(c), 0);
315 sl->references++;
318 /* Restore old status line. */
319 static void
320 status_pop_screen(struct client *c)
322 struct status_line *sl = &c->status;
324 if (--sl->references == 0) {
325 screen_free(sl->active);
326 free(sl->active);
327 sl->active = &sl->screen;
331 /* Initialize status line. */
332 void
333 status_init(struct client *c)
335 struct status_line *sl = &c->status;
336 u_int i;
338 for (i = 0; i < nitems(sl->entries); i++)
339 TAILQ_INIT(&sl->entries[i].ranges);
341 screen_init(&sl->screen, c->tty.sx, 1, 0);
342 sl->active = &sl->screen;
345 /* Free status line. */
346 void
347 status_free(struct client *c)
349 struct status_line *sl = &c->status;
350 u_int i;
352 for (i = 0; i < nitems(sl->entries); i++) {
353 status_free_ranges(&sl->entries[i].ranges);
354 free((void *)sl->entries[i].expanded);
357 if (event_initialized(&sl->timer))
358 evtimer_del(&sl->timer);
360 if (sl->active != &sl->screen) {
361 screen_free(sl->active);
362 free(sl->active);
364 screen_free(&sl->screen);
367 /* Draw status line for client. */
369 status_redraw(struct client *c)
371 struct status_line *sl = &c->status;
372 struct status_line_entry *sle;
373 struct session *s = c->session;
374 struct screen_write_ctx ctx;
375 struct grid_cell gc;
376 u_int lines, i, n, width = c->tty.sx;
377 int flags, force = 0, changed = 0, fg, bg;
378 struct options_entry *o;
379 union options_value *ov;
380 struct format_tree *ft;
381 char *expanded;
383 log_debug("%s enter", __func__);
385 /* Shouldn't get here if not the active screen. */
386 if (sl->active != &sl->screen)
387 fatalx("not the active screen");
389 /* No status line? */
390 lines = status_line_size(c);
391 if (c->tty.sy == 0 || lines == 0)
392 return (1);
394 /* Create format tree. */
395 flags = FORMAT_STATUS;
396 if (c->flags & CLIENT_STATUSFORCE)
397 flags |= FORMAT_FORCE;
398 ft = format_create(c, NULL, FORMAT_NONE, flags);
399 format_defaults(ft, c, NULL, NULL, NULL);
401 /* Set up default colour. */
402 style_apply(&gc, s->options, "status-style", ft);
403 fg = options_get_number(s->options, "status-fg");
404 if (!COLOUR_DEFAULT(fg))
405 gc.fg = fg;
406 bg = options_get_number(s->options, "status-bg");
407 if (!COLOUR_DEFAULT(bg))
408 gc.bg = bg;
409 if (!grid_cells_equal(&gc, &sl->style)) {
410 force = 1;
411 memcpy(&sl->style, &gc, sizeof sl->style);
414 /* Resize the target screen. */
415 if (screen_size_x(&sl->screen) != width ||
416 screen_size_y(&sl->screen) != lines) {
417 screen_resize(&sl->screen, width, lines, 0);
418 changed = force = 1;
420 screen_write_start(&ctx, &sl->screen);
422 /* Write the status lines. */
423 o = options_get(s->options, "status-format");
424 if (o == NULL) {
425 for (n = 0; n < width * lines; n++)
426 screen_write_putc(&ctx, &gc, ' ');
427 } else {
428 for (i = 0; i < lines; i++) {
429 screen_write_cursormove(&ctx, 0, i, 0);
431 ov = options_array_get(o, i);
432 if (ov == NULL) {
433 for (n = 0; n < width; n++)
434 screen_write_putc(&ctx, &gc, ' ');
435 continue;
437 sle = &sl->entries[i];
439 expanded = format_expand_time(ft, ov->string);
440 if (!force &&
441 sle->expanded != NULL &&
442 strcmp(expanded, sle->expanded) == 0) {
443 free(expanded);
444 continue;
446 changed = 1;
448 for (n = 0; n < width; n++)
449 screen_write_putc(&ctx, &gc, ' ');
450 screen_write_cursormove(&ctx, 0, i, 0);
452 status_free_ranges(&sle->ranges);
453 format_draw(&ctx, &gc, width, expanded, &sle->ranges,
456 free(sle->expanded);
457 sle->expanded = expanded;
460 screen_write_stop(&ctx);
462 /* Free the format tree. */
463 format_free(ft);
465 /* Return if the status line has changed. */
466 log_debug("%s exit: force=%d, changed=%d", __func__, force, changed);
467 return (force || changed);
470 /* Set a status line message. */
471 void
472 status_message_set(struct client *c, int delay, int ignore_styles,
473 int ignore_keys, const char *fmt, ...)
475 struct timeval tv;
476 va_list ap;
477 char *s;
479 va_start(ap, fmt);
480 xvasprintf(&s, fmt, ap);
481 va_end(ap);
483 log_debug("%s: %s", __func__, s);
485 if (c == NULL) {
486 server_add_message("message: %s", s);
487 free(s);
488 return;
491 status_message_clear(c);
492 status_push_screen(c);
493 c->message_string = s;
494 server_add_message("%s message: %s", c->name, s);
497 * With delay -1, the display-time option is used; zero means wait for
498 * key press; more than zero is the actual delay time in milliseconds.
500 if (delay == -1)
501 delay = options_get_number(c->session->options, "display-time");
502 if (delay > 0) {
503 tv.tv_sec = delay / 1000;
504 tv.tv_usec = (delay % 1000) * 1000L;
506 if (event_initialized(&c->message_timer))
507 evtimer_del(&c->message_timer);
508 evtimer_set(&c->message_timer, status_message_callback, c);
510 evtimer_add(&c->message_timer, &tv);
513 if (delay != 0)
514 c->message_ignore_keys = ignore_keys;
515 c->message_ignore_styles = ignore_styles;
517 c->tty.flags |= (TTY_NOCURSOR|TTY_FREEZE);
518 c->flags |= CLIENT_REDRAWSTATUS;
521 /* Clear status line message. */
522 void
523 status_message_clear(struct client *c)
525 if (c->message_string == NULL)
526 return;
528 free(c->message_string);
529 c->message_string = NULL;
531 if (c->prompt_string == NULL)
532 c->tty.flags &= ~(TTY_NOCURSOR|TTY_FREEZE);
533 c->flags |= CLIENT_ALLREDRAWFLAGS; /* was frozen and may have changed */
535 status_pop_screen(c);
538 /* Clear status line message after timer expires. */
539 static void
540 status_message_callback(__unused int fd, __unused short event, void *data)
542 struct client *c = data;
544 status_message_clear(c);
547 /* Draw client message on status line of present else on last line. */
549 status_message_redraw(struct client *c)
551 struct status_line *sl = &c->status;
552 struct screen_write_ctx ctx;
553 struct session *s = c->session;
554 struct screen old_screen;
555 size_t len;
556 u_int lines, offset, messageline;
557 struct grid_cell gc;
558 struct format_tree *ft;
560 if (c->tty.sx == 0 || c->tty.sy == 0)
561 return (0);
562 memcpy(&old_screen, sl->active, sizeof old_screen);
564 lines = status_line_size(c);
565 if (lines <= 1)
566 lines = 1;
567 screen_init(sl->active, c->tty.sx, lines, 0);
569 messageline = status_prompt_line_at(c);
570 if (messageline > lines - 1)
571 messageline = lines - 1;
573 len = screen_write_strlen("%s", c->message_string);
574 if (len > c->tty.sx)
575 len = c->tty.sx;
577 ft = format_create_defaults(NULL, c, NULL, NULL, NULL);
578 style_apply(&gc, s->options, "message-style", ft);
579 format_free(ft);
581 screen_write_start(&ctx, sl->active);
582 screen_write_fast_copy(&ctx, &sl->screen, 0, 0, c->tty.sx, lines);
583 screen_write_cursormove(&ctx, 0, messageline, 0);
584 for (offset = 0; offset < c->tty.sx; offset++)
585 screen_write_putc(&ctx, &gc, ' ');
586 screen_write_cursormove(&ctx, 0, messageline, 0);
587 if (c->message_ignore_styles)
588 screen_write_nputs(&ctx, len, &gc, "%s", c->message_string);
589 else
590 format_draw(&ctx, &gc, c->tty.sx, c->message_string, NULL, 0);
591 screen_write_stop(&ctx);
593 if (grid_compare(sl->active->grid, old_screen.grid) == 0) {
594 screen_free(&old_screen);
595 return (0);
597 screen_free(&old_screen);
598 return (1);
601 /* Accept prompt immediately. */
602 static enum cmd_retval
603 status_prompt_accept(__unused struct cmdq_item *item, void *data)
605 struct client *c = data;
607 if (c->prompt_string != NULL) {
608 c->prompt_inputcb(c, c->prompt_data, "y", 1);
609 status_prompt_clear(c);
611 return (CMD_RETURN_NORMAL);
614 /* Enable status line prompt. */
615 void
616 status_prompt_set(struct client *c, struct cmd_find_state *fs,
617 const char *msg, const char *input, prompt_input_cb inputcb,
618 prompt_free_cb freecb, void *data, int flags, enum prompt_type prompt_type)
620 struct format_tree *ft;
621 char *tmp;
623 server_client_clear_overlay(c);
625 if (fs != NULL)
626 ft = format_create_from_state(NULL, c, fs);
627 else
628 ft = format_create_defaults(NULL, c, NULL, NULL, NULL);
630 if (input == NULL)
631 input = "";
632 if (flags & PROMPT_NOFORMAT)
633 tmp = xstrdup(input);
634 else
635 tmp = format_expand_time(ft, input);
637 status_message_clear(c);
638 status_prompt_clear(c);
639 status_push_screen(c);
641 c->prompt_string = format_expand_time(ft, msg);
643 if (flags & PROMPT_INCREMENTAL) {
644 c->prompt_last = xstrdup(tmp);
645 c->prompt_buffer = utf8_fromcstr("");
646 } else {
647 c->prompt_last = NULL;
648 c->prompt_buffer = utf8_fromcstr(tmp);
650 c->prompt_index = utf8_strlen(c->prompt_buffer);
652 c->prompt_inputcb = inputcb;
653 c->prompt_freecb = freecb;
654 c->prompt_data = data;
656 memset(c->prompt_hindex, 0, sizeof c->prompt_hindex);
658 c->prompt_flags = flags;
659 c->prompt_type = prompt_type;
660 c->prompt_mode = PROMPT_ENTRY;
662 if (~flags & PROMPT_INCREMENTAL)
663 c->tty.flags |= TTY_FREEZE;
664 c->flags |= CLIENT_REDRAWSTATUS;
666 if (flags & PROMPT_INCREMENTAL)
667 c->prompt_inputcb(c, c->prompt_data, "=", 0);
669 free(tmp);
670 format_free(ft);
672 if ((flags & PROMPT_SINGLE) && (flags & PROMPT_ACCEPT))
673 cmdq_append(c, cmdq_get_callback(status_prompt_accept, c));
676 /* Remove status line prompt. */
677 void
678 status_prompt_clear(struct client *c)
680 if (c->prompt_string == NULL)
681 return;
683 if (c->prompt_freecb != NULL && c->prompt_data != NULL)
684 c->prompt_freecb(c->prompt_data);
686 free(c->prompt_last);
687 c->prompt_last = NULL;
689 free(c->prompt_string);
690 c->prompt_string = NULL;
692 free(c->prompt_buffer);
693 c->prompt_buffer = NULL;
695 free(c->prompt_saved);
696 c->prompt_saved = NULL;
698 c->tty.flags &= ~(TTY_NOCURSOR|TTY_FREEZE);
699 c->flags |= CLIENT_ALLREDRAWFLAGS; /* was frozen and may have changed */
701 status_pop_screen(c);
704 /* Update status line prompt with a new prompt string. */
705 void
706 status_prompt_update(struct client *c, const char *msg, const char *input)
708 struct format_tree *ft;
709 char *tmp;
711 ft = format_create(c, NULL, FORMAT_NONE, 0);
712 format_defaults(ft, c, NULL, NULL, NULL);
714 tmp = format_expand_time(ft, input);
716 free(c->prompt_string);
717 c->prompt_string = format_expand_time(ft, msg);
719 free(c->prompt_buffer);
720 c->prompt_buffer = utf8_fromcstr(tmp);
721 c->prompt_index = utf8_strlen(c->prompt_buffer);
723 memset(c->prompt_hindex, 0, sizeof c->prompt_hindex);
725 c->flags |= CLIENT_REDRAWSTATUS;
727 free(tmp);
728 format_free(ft);
731 /* Draw client prompt on status line of present else on last line. */
733 status_prompt_redraw(struct client *c)
735 struct status_line *sl = &c->status;
736 struct screen_write_ctx ctx;
737 struct session *s = c->session;
738 struct screen old_screen;
739 u_int i, lines, offset, left, start, width, n;
740 u_int pcursor, pwidth, promptline;
741 struct grid_cell gc;
742 struct format_tree *ft;
744 if (c->tty.sx == 0 || c->tty.sy == 0)
745 return (0);
746 memcpy(&old_screen, sl->active, sizeof old_screen);
748 lines = status_line_size(c);
749 if (lines <= 1)
750 lines = 1;
751 screen_init(sl->active, c->tty.sx, lines, 0);
753 n = options_get_number(s->options, "prompt-cursor-colour");
754 sl->active->default_ccolour = n;
755 n = options_get_number(s->options, "prompt-cursor-style");
756 screen_set_cursor_style(n, &sl->active->default_cstyle,
757 &sl->active->default_mode);
759 promptline = status_prompt_line_at(c);
760 if (promptline > lines - 1)
761 promptline = lines - 1;
763 ft = format_create_defaults(NULL, c, NULL, NULL, NULL);
764 if (c->prompt_mode == PROMPT_COMMAND)
765 style_apply(&gc, s->options, "message-command-style", ft);
766 else
767 style_apply(&gc, s->options, "message-style", ft);
768 format_free(ft);
770 start = format_width(c->prompt_string);
771 if (start > c->tty.sx)
772 start = c->tty.sx;
774 screen_write_start(&ctx, sl->active);
775 screen_write_fast_copy(&ctx, &sl->screen, 0, 0, c->tty.sx, lines);
776 screen_write_cursormove(&ctx, 0, promptline, 0);
777 for (offset = 0; offset < c->tty.sx; offset++)
778 screen_write_putc(&ctx, &gc, ' ');
779 screen_write_cursormove(&ctx, 0, promptline, 0);
780 format_draw(&ctx, &gc, start, c->prompt_string, NULL, 0);
781 screen_write_cursormove(&ctx, start, promptline, 0);
783 left = c->tty.sx - start;
784 if (left == 0)
785 goto finished;
787 pcursor = utf8_strwidth(c->prompt_buffer, c->prompt_index);
788 pwidth = utf8_strwidth(c->prompt_buffer, -1);
789 if (pcursor >= left) {
791 * The cursor would be outside the screen so start drawing
792 * with it on the right.
794 offset = (pcursor - left) + 1;
795 pwidth = left;
796 } else
797 offset = 0;
798 if (pwidth > left)
799 pwidth = left;
800 c->prompt_cursor = start + c->prompt_index - offset;
802 width = 0;
803 for (i = 0; c->prompt_buffer[i].size != 0; i++) {
804 if (width < offset) {
805 width += c->prompt_buffer[i].width;
806 continue;
808 if (width >= offset + pwidth)
809 break;
810 width += c->prompt_buffer[i].width;
811 if (width > offset + pwidth)
812 break;
814 utf8_copy(&gc.data, &c->prompt_buffer[i]);
815 screen_write_cell(&ctx, &gc);
818 finished:
819 screen_write_stop(&ctx);
821 if (grid_compare(sl->active->grid, old_screen.grid) == 0) {
822 screen_free(&old_screen);
823 return (0);
825 screen_free(&old_screen);
826 return (1);
829 /* Is this a separator? */
830 static int
831 status_prompt_in_list(const char *ws, const struct utf8_data *ud)
833 if (ud->size != 1 || ud->width != 1)
834 return (0);
835 return (strchr(ws, *ud->data) != NULL);
838 /* Is this a space? */
839 static int
840 status_prompt_space(const struct utf8_data *ud)
842 if (ud->size != 1 || ud->width != 1)
843 return (0);
844 return (*ud->data == ' ');
848 * Translate key from vi to emacs. Return 0 to drop key, 1 to process the key
849 * as an emacs key; return 2 to append to the buffer.
851 static int
852 status_prompt_translate_key(struct client *c, key_code key, key_code *new_key)
854 if (c->prompt_mode == PROMPT_ENTRY) {
855 switch (key) {
856 case 'a'|KEYC_CTRL:
857 case 'c'|KEYC_CTRL:
858 case 'e'|KEYC_CTRL:
859 case 'g'|KEYC_CTRL:
860 case 'h'|KEYC_CTRL:
861 case '\011': /* Tab */
862 case 'k'|KEYC_CTRL:
863 case 'n'|KEYC_CTRL:
864 case 'p'|KEYC_CTRL:
865 case 't'|KEYC_CTRL:
866 case 'u'|KEYC_CTRL:
867 case 'w'|KEYC_CTRL:
868 case 'y'|KEYC_CTRL:
869 case '\n':
870 case '\r':
871 case KEYC_LEFT|KEYC_CTRL:
872 case KEYC_RIGHT|KEYC_CTRL:
873 case KEYC_BSPACE:
874 case KEYC_DC:
875 case KEYC_DOWN:
876 case KEYC_END:
877 case KEYC_HOME:
878 case KEYC_LEFT:
879 case KEYC_RIGHT:
880 case KEYC_UP:
881 *new_key = key;
882 return (1);
883 case '\033': /* Escape */
884 c->prompt_mode = PROMPT_COMMAND;
885 c->flags |= CLIENT_REDRAWSTATUS;
886 return (0);
888 *new_key = key;
889 return (2);
892 switch (key) {
893 case KEYC_BSPACE:
894 *new_key = KEYC_LEFT;
895 return (1);
896 case 'A':
897 case 'I':
898 case 'C':
899 case 's':
900 case 'a':
901 c->prompt_mode = PROMPT_ENTRY;
902 c->flags |= CLIENT_REDRAWSTATUS;
903 break; /* switch mode and... */
904 case 'S':
905 c->prompt_mode = PROMPT_ENTRY;
906 c->flags |= CLIENT_REDRAWSTATUS;
907 *new_key = 'u'|KEYC_CTRL;
908 return (1);
909 case 'i':
910 case '\033': /* Escape */
911 c->prompt_mode = PROMPT_ENTRY;
912 c->flags |= CLIENT_REDRAWSTATUS;
913 return (0);
916 switch (key) {
917 case 'A':
918 case '$':
919 *new_key = KEYC_END;
920 return (1);
921 case 'I':
922 case '0':
923 case '^':
924 *new_key = KEYC_HOME;
925 return (1);
926 case 'C':
927 case 'D':
928 *new_key = 'k'|KEYC_CTRL;
929 return (1);
930 case KEYC_BSPACE:
931 case 'X':
932 *new_key = KEYC_BSPACE;
933 return (1);
934 case 'b':
935 *new_key = 'b'|KEYC_META;
936 return (1);
937 case 'B':
938 *new_key = 'B'|KEYC_VI;
939 return (1);
940 case 'd':
941 *new_key = 'u'|KEYC_CTRL;
942 return (1);
943 case 'e':
944 *new_key = 'e'|KEYC_VI;
945 return (1);
946 case 'E':
947 *new_key = 'E'|KEYC_VI;
948 return (1);
949 case 'w':
950 *new_key = 'w'|KEYC_VI;
951 return (1);
952 case 'W':
953 *new_key = 'W'|KEYC_VI;
954 return (1);
955 case 'p':
956 *new_key = 'y'|KEYC_CTRL;
957 return (1);
958 case 'q':
959 *new_key = 'c'|KEYC_CTRL;
960 return (1);
961 case 's':
962 case KEYC_DC:
963 case 'x':
964 *new_key = KEYC_DC;
965 return (1);
966 case KEYC_DOWN:
967 case 'j':
968 *new_key = KEYC_DOWN;
969 return (1);
970 case KEYC_LEFT:
971 case 'h':
972 *new_key = KEYC_LEFT;
973 return (1);
974 case 'a':
975 case KEYC_RIGHT:
976 case 'l':
977 *new_key = KEYC_RIGHT;
978 return (1);
979 case KEYC_UP:
980 case 'k':
981 *new_key = KEYC_UP;
982 return (1);
983 case 'h'|KEYC_CTRL:
984 case 'c'|KEYC_CTRL:
985 case '\n':
986 case '\r':
987 return (1);
989 return (0);
992 /* Paste into prompt. */
993 static int
994 status_prompt_paste(struct client *c)
996 struct paste_buffer *pb;
997 const char *bufdata;
998 size_t size, n, bufsize;
999 u_int i;
1000 struct utf8_data *ud, *udp;
1001 enum utf8_state more;
1003 size = utf8_strlen(c->prompt_buffer);
1004 if (c->prompt_saved != NULL) {
1005 ud = c->prompt_saved;
1006 n = utf8_strlen(c->prompt_saved);
1007 } else {
1008 if ((pb = paste_get_top(NULL)) == NULL)
1009 return (0);
1010 bufdata = paste_buffer_data(pb, &bufsize);
1011 ud = udp = xreallocarray(NULL, bufsize + 1, sizeof *ud);
1012 for (i = 0; i != bufsize; /* nothing */) {
1013 more = utf8_open(udp, bufdata[i]);
1014 if (more == UTF8_MORE) {
1015 while (++i != bufsize && more == UTF8_MORE)
1016 more = utf8_append(udp, bufdata[i]);
1017 if (more == UTF8_DONE) {
1018 udp++;
1019 continue;
1021 i -= udp->have;
1023 if (bufdata[i] <= 31 || bufdata[i] >= 127)
1024 break;
1025 utf8_set(udp, bufdata[i]);
1026 udp++;
1027 i++;
1029 udp->size = 0;
1030 n = udp - ud;
1032 if (n != 0) {
1033 c->prompt_buffer = xreallocarray(c->prompt_buffer, size + n + 1,
1034 sizeof *c->prompt_buffer);
1035 if (c->prompt_index == size) {
1036 memcpy(c->prompt_buffer + c->prompt_index, ud,
1037 n * sizeof *c->prompt_buffer);
1038 c->prompt_index += n;
1039 c->prompt_buffer[c->prompt_index].size = 0;
1040 } else {
1041 memmove(c->prompt_buffer + c->prompt_index + n,
1042 c->prompt_buffer + c->prompt_index,
1043 (size + 1 - c->prompt_index) *
1044 sizeof *c->prompt_buffer);
1045 memcpy(c->prompt_buffer + c->prompt_index, ud,
1046 n * sizeof *c->prompt_buffer);
1047 c->prompt_index += n;
1050 if (ud != c->prompt_saved)
1051 free(ud);
1052 return (1);
1055 /* Finish completion. */
1056 static int
1057 status_prompt_replace_complete(struct client *c, const char *s)
1059 char word[64], *allocated = NULL;
1060 size_t size, n, off, idx, used;
1061 struct utf8_data *first, *last, *ud;
1063 /* Work out where the cursor currently is. */
1064 idx = c->prompt_index;
1065 if (idx != 0)
1066 idx--;
1067 size = utf8_strlen(c->prompt_buffer);
1069 /* Find the word we are in. */
1070 first = &c->prompt_buffer[idx];
1071 while (first > c->prompt_buffer && !status_prompt_space(first))
1072 first--;
1073 while (first->size != 0 && status_prompt_space(first))
1074 first++;
1075 last = &c->prompt_buffer[idx];
1076 while (last->size != 0 && !status_prompt_space(last))
1077 last++;
1078 while (last > c->prompt_buffer && status_prompt_space(last))
1079 last--;
1080 if (last->size != 0)
1081 last++;
1082 if (last < first)
1083 return (0);
1084 if (s == NULL) {
1085 used = 0;
1086 for (ud = first; ud < last; ud++) {
1087 if (used + ud->size >= sizeof word)
1088 break;
1089 memcpy(word + used, ud->data, ud->size);
1090 used += ud->size;
1092 if (ud != last)
1093 return (0);
1094 word[used] = '\0';
1097 /* Try to complete it. */
1098 if (s == NULL) {
1099 allocated = status_prompt_complete(c, word,
1100 first - c->prompt_buffer);
1101 if (allocated == NULL)
1102 return (0);
1103 s = allocated;
1106 /* Trim out word. */
1107 n = size - (last - c->prompt_buffer) + 1; /* with \0 */
1108 memmove(first, last, n * sizeof *c->prompt_buffer);
1109 size -= last - first;
1111 /* Insert the new word. */
1112 size += strlen(s);
1113 off = first - c->prompt_buffer;
1114 c->prompt_buffer = xreallocarray(c->prompt_buffer, size + 1,
1115 sizeof *c->prompt_buffer);
1116 first = c->prompt_buffer + off;
1117 memmove(first + strlen(s), first, n * sizeof *c->prompt_buffer);
1118 for (idx = 0; idx < strlen(s); idx++)
1119 utf8_set(&first[idx], s[idx]);
1120 c->prompt_index = (first - c->prompt_buffer) + strlen(s);
1122 free(allocated);
1123 return (1);
1126 /* Prompt forward to the next beginning of a word. */
1127 static void
1128 status_prompt_forward_word(struct client *c, size_t size, int vi,
1129 const char *separators)
1131 size_t idx = c->prompt_index;
1132 int word_is_separators;
1134 /* In emacs mode, skip until the first non-whitespace character. */
1135 if (!vi)
1136 while (idx != size &&
1137 status_prompt_space(&c->prompt_buffer[idx]))
1138 idx++;
1140 /* Can't move forward if we're already at the end. */
1141 if (idx == size) {
1142 c->prompt_index = idx;
1143 return;
1146 /* Determine the current character class (separators or not). */
1147 word_is_separators = status_prompt_in_list(separators,
1148 &c->prompt_buffer[idx]) &&
1149 !status_prompt_space(&c->prompt_buffer[idx]);
1151 /* Skip ahead until the first space or opposite character class. */
1152 do {
1153 idx++;
1154 if (status_prompt_space(&c->prompt_buffer[idx])) {
1155 /* In vi mode, go to the start of the next word. */
1156 if (vi)
1157 while (idx != size &&
1158 status_prompt_space(&c->prompt_buffer[idx]))
1159 idx++;
1160 break;
1162 } while (idx != size && word_is_separators == status_prompt_in_list(
1163 separators, &c->prompt_buffer[idx]));
1165 c->prompt_index = idx;
1168 /* Prompt forward to the next end of a word. */
1169 static void
1170 status_prompt_end_word(struct client *c, size_t size, const char *separators)
1172 size_t idx = c->prompt_index;
1173 int word_is_separators;
1175 /* Can't move forward if we're already at the end. */
1176 if (idx == size)
1177 return;
1179 /* Find the next word. */
1180 do {
1181 idx++;
1182 if (idx == size) {
1183 c->prompt_index = idx;
1184 return;
1186 } while (status_prompt_space(&c->prompt_buffer[idx]));
1188 /* Determine the character class (separators or not). */
1189 word_is_separators = status_prompt_in_list(separators,
1190 &c->prompt_buffer[idx]);
1192 /* Skip ahead until the next space or opposite character class. */
1193 do {
1194 idx++;
1195 if (idx == size)
1196 break;
1197 } while (!status_prompt_space(&c->prompt_buffer[idx]) &&
1198 word_is_separators == status_prompt_in_list(separators,
1199 &c->prompt_buffer[idx]));
1201 /* Back up to the previous character to stop at the end of the word. */
1202 c->prompt_index = idx - 1;
1205 /* Prompt backward to the previous beginning of a word. */
1206 static void
1207 status_prompt_backward_word(struct client *c, const char *separators)
1209 size_t idx = c->prompt_index;
1210 int word_is_separators;
1212 /* Find non-whitespace. */
1213 while (idx != 0) {
1214 --idx;
1215 if (!status_prompt_space(&c->prompt_buffer[idx]))
1216 break;
1218 word_is_separators = status_prompt_in_list(separators,
1219 &c->prompt_buffer[idx]);
1221 /* Find the character before the beginning of the word. */
1222 while (idx != 0) {
1223 --idx;
1224 if (status_prompt_space(&c->prompt_buffer[idx]) ||
1225 word_is_separators != status_prompt_in_list(separators,
1226 &c->prompt_buffer[idx])) {
1227 /* Go back to the word. */
1228 idx++;
1229 break;
1232 c->prompt_index = idx;
1235 /* Handle keys in prompt. */
1237 status_prompt_key(struct client *c, key_code key)
1239 struct options *oo = c->session->options;
1240 char *s, *cp, prefix = '=';
1241 const char *histstr, *separators = NULL, *keystring;
1242 size_t size, idx;
1243 struct utf8_data tmp;
1244 int keys, word_is_separators;
1246 if (c->prompt_flags & PROMPT_KEY) {
1247 keystring = key_string_lookup_key(key, 0);
1248 c->prompt_inputcb(c, c->prompt_data, keystring, 1);
1249 status_prompt_clear(c);
1250 return (0);
1252 size = utf8_strlen(c->prompt_buffer);
1254 if (c->prompt_flags & PROMPT_NUMERIC) {
1255 if (key >= '0' && key <= '9')
1256 goto append_key;
1257 s = utf8_tocstr(c->prompt_buffer);
1258 c->prompt_inputcb(c, c->prompt_data, s, 1);
1259 status_prompt_clear(c);
1260 free(s);
1261 return (1);
1263 key &= ~KEYC_MASK_FLAGS;
1265 keys = options_get_number(c->session->options, "status-keys");
1266 if (keys == MODEKEY_VI) {
1267 switch (status_prompt_translate_key(c, key, &key)) {
1268 case 1:
1269 goto process_key;
1270 case 2:
1271 goto append_key;
1272 default:
1273 return (0);
1277 process_key:
1278 switch (key) {
1279 case KEYC_LEFT:
1280 case 'b'|KEYC_CTRL:
1281 if (c->prompt_index > 0) {
1282 c->prompt_index--;
1283 break;
1285 break;
1286 case KEYC_RIGHT:
1287 case 'f'|KEYC_CTRL:
1288 if (c->prompt_index < size) {
1289 c->prompt_index++;
1290 break;
1292 break;
1293 case KEYC_HOME:
1294 case 'a'|KEYC_CTRL:
1295 if (c->prompt_index != 0) {
1296 c->prompt_index = 0;
1297 break;
1299 break;
1300 case KEYC_END:
1301 case 'e'|KEYC_CTRL:
1302 if (c->prompt_index != size) {
1303 c->prompt_index = size;
1304 break;
1306 break;
1307 case '\011': /* Tab */
1308 if (status_prompt_replace_complete(c, NULL))
1309 goto changed;
1310 break;
1311 case KEYC_BSPACE:
1312 case 'h'|KEYC_CTRL:
1313 if (c->prompt_index != 0) {
1314 if (c->prompt_index == size)
1315 c->prompt_buffer[--c->prompt_index].size = 0;
1316 else {
1317 memmove(c->prompt_buffer + c->prompt_index - 1,
1318 c->prompt_buffer + c->prompt_index,
1319 (size + 1 - c->prompt_index) *
1320 sizeof *c->prompt_buffer);
1321 c->prompt_index--;
1323 goto changed;
1325 break;
1326 case KEYC_DC:
1327 case 'd'|KEYC_CTRL:
1328 if (c->prompt_index != size) {
1329 memmove(c->prompt_buffer + c->prompt_index,
1330 c->prompt_buffer + c->prompt_index + 1,
1331 (size + 1 - c->prompt_index) *
1332 sizeof *c->prompt_buffer);
1333 goto changed;
1335 break;
1336 case 'u'|KEYC_CTRL:
1337 c->prompt_buffer[0].size = 0;
1338 c->prompt_index = 0;
1339 goto changed;
1340 case 'k'|KEYC_CTRL:
1341 if (c->prompt_index < size) {
1342 c->prompt_buffer[c->prompt_index].size = 0;
1343 goto changed;
1345 break;
1346 case 'w'|KEYC_CTRL:
1347 separators = options_get_string(oo, "word-separators");
1348 idx = c->prompt_index;
1350 /* Find non-whitespace. */
1351 while (idx != 0) {
1352 idx--;
1353 if (!status_prompt_space(&c->prompt_buffer[idx]))
1354 break;
1356 word_is_separators = status_prompt_in_list(separators,
1357 &c->prompt_buffer[idx]);
1359 /* Find the character before the beginning of the word. */
1360 while (idx != 0) {
1361 idx--;
1362 if (status_prompt_space(&c->prompt_buffer[idx]) ||
1363 word_is_separators != status_prompt_in_list(
1364 separators, &c->prompt_buffer[idx])) {
1365 /* Go back to the word. */
1366 idx++;
1367 break;
1371 free(c->prompt_saved);
1372 c->prompt_saved = xcalloc(sizeof *c->prompt_buffer,
1373 (c->prompt_index - idx) + 1);
1374 memcpy(c->prompt_saved, c->prompt_buffer + idx,
1375 (c->prompt_index - idx) * sizeof *c->prompt_buffer);
1377 memmove(c->prompt_buffer + idx,
1378 c->prompt_buffer + c->prompt_index,
1379 (size + 1 - c->prompt_index) *
1380 sizeof *c->prompt_buffer);
1381 memset(c->prompt_buffer + size - (c->prompt_index - idx),
1382 '\0', (c->prompt_index - idx) * sizeof *c->prompt_buffer);
1383 c->prompt_index = idx;
1385 goto changed;
1386 case KEYC_RIGHT|KEYC_CTRL:
1387 case 'f'|KEYC_META:
1388 separators = options_get_string(oo, "word-separators");
1389 status_prompt_forward_word(c, size, 0, separators);
1390 goto changed;
1391 case 'E'|KEYC_VI:
1392 status_prompt_end_word(c, size, "");
1393 goto changed;
1394 case 'e'|KEYC_VI:
1395 separators = options_get_string(oo, "word-separators");
1396 status_prompt_end_word(c, size, separators);
1397 goto changed;
1398 case 'W'|KEYC_VI:
1399 status_prompt_forward_word(c, size, 1, "");
1400 goto changed;
1401 case 'w'|KEYC_VI:
1402 separators = options_get_string(oo, "word-separators");
1403 status_prompt_forward_word(c, size, 1, separators);
1404 goto changed;
1405 case 'B'|KEYC_VI:
1406 status_prompt_backward_word(c, "");
1407 goto changed;
1408 case KEYC_LEFT|KEYC_CTRL:
1409 case 'b'|KEYC_META:
1410 separators = options_get_string(oo, "word-separators");
1411 status_prompt_backward_word(c, separators);
1412 goto changed;
1413 case KEYC_UP:
1414 case 'p'|KEYC_CTRL:
1415 histstr = status_prompt_up_history(c->prompt_hindex,
1416 c->prompt_type);
1417 if (histstr == NULL)
1418 break;
1419 free(c->prompt_buffer);
1420 c->prompt_buffer = utf8_fromcstr(histstr);
1421 c->prompt_index = utf8_strlen(c->prompt_buffer);
1422 goto changed;
1423 case KEYC_DOWN:
1424 case 'n'|KEYC_CTRL:
1425 histstr = status_prompt_down_history(c->prompt_hindex,
1426 c->prompt_type);
1427 if (histstr == NULL)
1428 break;
1429 free(c->prompt_buffer);
1430 c->prompt_buffer = utf8_fromcstr(histstr);
1431 c->prompt_index = utf8_strlen(c->prompt_buffer);
1432 goto changed;
1433 case 'y'|KEYC_CTRL:
1434 if (status_prompt_paste(c))
1435 goto changed;
1436 break;
1437 case 't'|KEYC_CTRL:
1438 idx = c->prompt_index;
1439 if (idx < size)
1440 idx++;
1441 if (idx >= 2) {
1442 utf8_copy(&tmp, &c->prompt_buffer[idx - 2]);
1443 utf8_copy(&c->prompt_buffer[idx - 2],
1444 &c->prompt_buffer[idx - 1]);
1445 utf8_copy(&c->prompt_buffer[idx - 1], &tmp);
1446 c->prompt_index = idx;
1447 goto changed;
1449 break;
1450 case '\r':
1451 case '\n':
1452 s = utf8_tocstr(c->prompt_buffer);
1453 if (*s != '\0')
1454 status_prompt_add_history(s, c->prompt_type);
1455 if (c->prompt_inputcb(c, c->prompt_data, s, 1) == 0)
1456 status_prompt_clear(c);
1457 free(s);
1458 break;
1459 case '\033': /* Escape */
1460 case 'c'|KEYC_CTRL:
1461 case 'g'|KEYC_CTRL:
1462 if (c->prompt_inputcb(c, c->prompt_data, NULL, 1) == 0)
1463 status_prompt_clear(c);
1464 break;
1465 case 'r'|KEYC_CTRL:
1466 if (~c->prompt_flags & PROMPT_INCREMENTAL)
1467 break;
1468 if (c->prompt_buffer[0].size == 0) {
1469 prefix = '=';
1470 free(c->prompt_buffer);
1471 c->prompt_buffer = utf8_fromcstr(c->prompt_last);
1472 c->prompt_index = utf8_strlen(c->prompt_buffer);
1473 } else
1474 prefix = '-';
1475 goto changed;
1476 case 's'|KEYC_CTRL:
1477 if (~c->prompt_flags & PROMPT_INCREMENTAL)
1478 break;
1479 if (c->prompt_buffer[0].size == 0) {
1480 prefix = '=';
1481 free(c->prompt_buffer);
1482 c->prompt_buffer = utf8_fromcstr(c->prompt_last);
1483 c->prompt_index = utf8_strlen(c->prompt_buffer);
1484 } else
1485 prefix = '+';
1486 goto changed;
1487 default:
1488 goto append_key;
1491 c->flags |= CLIENT_REDRAWSTATUS;
1492 return (0);
1494 append_key:
1495 if (key <= 0x7f)
1496 utf8_set(&tmp, key);
1497 else if (KEYC_IS_UNICODE(key))
1498 utf8_to_data(key, &tmp);
1499 else
1500 return (0);
1502 c->prompt_buffer = xreallocarray(c->prompt_buffer, size + 2,
1503 sizeof *c->prompt_buffer);
1505 if (c->prompt_index == size) {
1506 utf8_copy(&c->prompt_buffer[c->prompt_index], &tmp);
1507 c->prompt_index++;
1508 c->prompt_buffer[c->prompt_index].size = 0;
1509 } else {
1510 memmove(c->prompt_buffer + c->prompt_index + 1,
1511 c->prompt_buffer + c->prompt_index,
1512 (size + 1 - c->prompt_index) *
1513 sizeof *c->prompt_buffer);
1514 utf8_copy(&c->prompt_buffer[c->prompt_index], &tmp);
1515 c->prompt_index++;
1518 if (c->prompt_flags & PROMPT_SINGLE) {
1519 if (utf8_strlen(c->prompt_buffer) != 1)
1520 status_prompt_clear(c);
1521 else {
1522 s = utf8_tocstr(c->prompt_buffer);
1523 if (c->prompt_inputcb(c, c->prompt_data, s, 1) == 0)
1524 status_prompt_clear(c);
1525 free(s);
1529 changed:
1530 c->flags |= CLIENT_REDRAWSTATUS;
1531 if (c->prompt_flags & PROMPT_INCREMENTAL) {
1532 s = utf8_tocstr(c->prompt_buffer);
1533 xasprintf(&cp, "%c%s", prefix, s);
1534 c->prompt_inputcb(c, c->prompt_data, cp, 0);
1535 free(cp);
1536 free(s);
1538 return (0);
1541 /* Get previous line from the history. */
1542 static const char *
1543 status_prompt_up_history(u_int *idx, u_int type)
1546 * History runs from 0 to size - 1. Index is from 0 to size. Zero is
1547 * empty.
1550 if (status_prompt_hsize[type] == 0 ||
1551 idx[type] == status_prompt_hsize[type])
1552 return (NULL);
1553 idx[type]++;
1554 return (status_prompt_hlist[type][status_prompt_hsize[type] - idx[type]]);
1557 /* Get next line from the history. */
1558 static const char *
1559 status_prompt_down_history(u_int *idx, u_int type)
1561 if (status_prompt_hsize[type] == 0 || idx[type] == 0)
1562 return ("");
1563 idx[type]--;
1564 if (idx[type] == 0)
1565 return ("");
1566 return (status_prompt_hlist[type][status_prompt_hsize[type] - idx[type]]);
1569 /* Add line to the history. */
1570 static void
1571 status_prompt_add_history(const char *line, u_int type)
1573 u_int i, oldsize, newsize, freecount, hlimit, new = 1;
1574 size_t movesize;
1576 oldsize = status_prompt_hsize[type];
1577 if (oldsize > 0 &&
1578 strcmp(status_prompt_hlist[type][oldsize - 1], line) == 0)
1579 new = 0;
1581 hlimit = options_get_number(global_options, "prompt-history-limit");
1582 if (hlimit > oldsize) {
1583 if (new == 0)
1584 return;
1585 newsize = oldsize + new;
1586 } else {
1587 newsize = hlimit;
1588 freecount = oldsize + new - newsize;
1589 if (freecount > oldsize)
1590 freecount = oldsize;
1591 if (freecount == 0)
1592 return;
1593 for (i = 0; i < freecount; i++)
1594 free(status_prompt_hlist[type][i]);
1595 movesize = (oldsize - freecount) *
1596 sizeof *status_prompt_hlist[type];
1597 if (movesize > 0) {
1598 memmove(&status_prompt_hlist[type][0],
1599 &status_prompt_hlist[type][freecount], movesize);
1603 if (newsize == 0) {
1604 free(status_prompt_hlist[type]);
1605 status_prompt_hlist[type] = NULL;
1606 } else if (newsize != oldsize) {
1607 status_prompt_hlist[type] =
1608 xreallocarray(status_prompt_hlist[type], newsize,
1609 sizeof *status_prompt_hlist[type]);
1612 if (new == 1 && newsize > 0)
1613 status_prompt_hlist[type][newsize - 1] = xstrdup(line);
1614 status_prompt_hsize[type] = newsize;
1617 /* Add to completion list. */
1618 static void
1619 status_prompt_add_list(char ***list, u_int *size, const char *s)
1621 u_int i;
1623 for (i = 0; i < *size; i++) {
1624 if (strcmp((*list)[i], s) == 0)
1625 return;
1627 *list = xreallocarray(*list, (*size) + 1, sizeof **list);
1628 (*list)[(*size)++] = xstrdup(s);
1631 /* Build completion list. */
1632 static char **
1633 status_prompt_complete_list(u_int *size, const char *s, int at_start)
1635 char **list = NULL, *tmp;
1636 const char **layout, *value, *cp;
1637 const struct cmd_entry **cmdent;
1638 const struct options_table_entry *oe;
1639 size_t slen = strlen(s), valuelen;
1640 struct options_entry *o;
1641 struct options_array_item *a;
1642 const char *layouts[] = {
1643 "even-horizontal", "even-vertical",
1644 "main-horizontal", "main-horizontal-mirrored",
1645 "main-vertical", "main-vertical-mirrored", "tiled", NULL
1648 *size = 0;
1649 for (cmdent = cmd_table; *cmdent != NULL; cmdent++) {
1650 if (strncmp((*cmdent)->name, s, slen) == 0)
1651 status_prompt_add_list(&list, size, (*cmdent)->name);
1652 if ((*cmdent)->alias != NULL &&
1653 strncmp((*cmdent)->alias, s, slen) == 0)
1654 status_prompt_add_list(&list, size, (*cmdent)->alias);
1656 o = options_get_only(global_options, "command-alias");
1657 if (o != NULL) {
1658 a = options_array_first(o);
1659 while (a != NULL) {
1660 value = options_array_item_value(a)->string;
1661 if ((cp = strchr(value, '=')) == NULL)
1662 goto next;
1663 valuelen = cp - value;
1664 if (slen > valuelen || strncmp(value, s, slen) != 0)
1665 goto next;
1667 xasprintf(&tmp, "%.*s", (int)valuelen, value);
1668 status_prompt_add_list(&list, size, tmp);
1669 free(tmp);
1671 next:
1672 a = options_array_next(a);
1675 if (at_start)
1676 return (list);
1677 for (oe = options_table; oe->name != NULL; oe++) {
1678 if (strncmp(oe->name, s, slen) == 0)
1679 status_prompt_add_list(&list, size, oe->name);
1681 for (layout = layouts; *layout != NULL; layout++) {
1682 if (strncmp(*layout, s, slen) == 0)
1683 status_prompt_add_list(&list, size, *layout);
1685 return (list);
1688 /* Find longest prefix. */
1689 static char *
1690 status_prompt_complete_prefix(char **list, u_int size)
1692 char *out;
1693 u_int i;
1694 size_t j;
1696 if (list == NULL || size == 0)
1697 return (NULL);
1698 out = xstrdup(list[0]);
1699 for (i = 1; i < size; i++) {
1700 j = strlen(list[i]);
1701 if (j > strlen(out))
1702 j = strlen(out);
1703 for (; j > 0; j--) {
1704 if (out[j - 1] != list[i][j - 1])
1705 out[j - 1] = '\0';
1708 return (out);
1711 /* Complete word menu callback. */
1712 static void
1713 status_prompt_menu_callback(__unused struct menu *menu, u_int idx, key_code key,
1714 void *data)
1716 struct status_prompt_menu *spm = data;
1717 struct client *c = spm->c;
1718 u_int i;
1719 char *s;
1721 if (key != KEYC_NONE) {
1722 idx += spm->start;
1723 if (spm->flag == '\0')
1724 s = xstrdup(spm->list[idx]);
1725 else
1726 xasprintf(&s, "-%c%s", spm->flag, spm->list[idx]);
1727 if (c->prompt_type == PROMPT_TYPE_WINDOW_TARGET) {
1728 free(c->prompt_buffer);
1729 c->prompt_buffer = utf8_fromcstr(s);
1730 c->prompt_index = utf8_strlen(c->prompt_buffer);
1731 c->flags |= CLIENT_REDRAWSTATUS;
1732 } else if (status_prompt_replace_complete(c, s))
1733 c->flags |= CLIENT_REDRAWSTATUS;
1734 free(s);
1737 for (i = 0; i < spm->size; i++)
1738 free(spm->list[i]);
1739 free(spm->list);
1742 /* Show complete word menu. */
1743 static int
1744 status_prompt_complete_list_menu(struct client *c, char **list, u_int size,
1745 u_int offset, char flag)
1747 struct menu *menu;
1748 struct menu_item item;
1749 struct status_prompt_menu *spm;
1750 u_int lines = status_line_size(c), height, i;
1751 u_int py;
1753 if (size <= 1)
1754 return (0);
1755 if (c->tty.sy - lines < 3)
1756 return (0);
1758 spm = xmalloc(sizeof *spm);
1759 spm->c = c;
1760 spm->size = size;
1761 spm->list = list;
1762 spm->flag = flag;
1764 height = c->tty.sy - lines - 2;
1765 if (height > 10)
1766 height = 10;
1767 if (height > size)
1768 height = size;
1769 spm->start = size - height;
1771 menu = menu_create("");
1772 for (i = spm->start; i < size; i++) {
1773 item.name = list[i];
1774 item.key = '0' + (i - spm->start);
1775 item.command = NULL;
1776 menu_add_item(menu, &item, NULL, c, NULL);
1779 if (options_get_number(c->session->options, "status-position") == 0)
1780 py = lines;
1781 else
1782 py = c->tty.sy - 3 - height;
1783 offset += utf8_cstrwidth(c->prompt_string);
1784 if (offset > 2)
1785 offset -= 2;
1786 else
1787 offset = 0;
1789 if (menu_display(menu, MENU_NOMOUSE|MENU_TAB, 0, NULL, offset, py, c,
1790 BOX_LINES_DEFAULT, NULL, NULL, NULL, NULL,
1791 status_prompt_menu_callback, spm) != 0) {
1792 menu_free(menu);
1793 free(spm);
1794 return (0);
1796 return (1);
1799 /* Show complete word menu. */
1800 static char *
1801 status_prompt_complete_window_menu(struct client *c, struct session *s,
1802 const char *word, u_int offset, char flag)
1804 struct menu *menu;
1805 struct menu_item item;
1806 struct status_prompt_menu *spm;
1807 struct winlink *wl;
1808 char **list = NULL, *tmp;
1809 u_int lines = status_line_size(c), height;
1810 u_int py, size = 0;
1812 if (c->tty.sy - lines < 3)
1813 return (NULL);
1815 spm = xmalloc(sizeof *spm);
1816 spm->c = c;
1817 spm->flag = flag;
1819 height = c->tty.sy - lines - 2;
1820 if (height > 10)
1821 height = 10;
1822 spm->start = 0;
1824 menu = menu_create("");
1825 RB_FOREACH(wl, winlinks, &s->windows) {
1826 if (word != NULL && *word != '\0') {
1827 xasprintf(&tmp, "%d", wl->idx);
1828 if (strncmp(tmp, word, strlen(word)) != 0) {
1829 free(tmp);
1830 continue;
1832 free(tmp);
1835 list = xreallocarray(list, size + 1, sizeof *list);
1836 if (c->prompt_type == PROMPT_TYPE_WINDOW_TARGET) {
1837 xasprintf(&tmp, "%d (%s)", wl->idx, wl->window->name);
1838 xasprintf(&list[size++], "%d", wl->idx);
1839 } else {
1840 xasprintf(&tmp, "%s:%d (%s)", s->name, wl->idx,
1841 wl->window->name);
1842 xasprintf(&list[size++], "%s:%d", s->name, wl->idx);
1844 item.name = tmp;
1845 item.key = '0' + size - 1;
1846 item.command = NULL;
1847 menu_add_item(menu, &item, NULL, c, NULL);
1848 free(tmp);
1850 if (size == height)
1851 break;
1853 if (size == 0) {
1854 menu_free(menu);
1855 free(spm);
1856 return (NULL);
1858 if (size == 1) {
1859 menu_free(menu);
1860 if (flag != '\0') {
1861 xasprintf(&tmp, "-%c%s", flag, list[0]);
1862 free(list[0]);
1863 } else
1864 tmp = list[0];
1865 free(list);
1866 free(spm);
1867 return (tmp);
1869 if (height > size)
1870 height = size;
1872 spm->size = size;
1873 spm->list = list;
1875 if (options_get_number(c->session->options, "status-position") == 0)
1876 py = lines;
1877 else
1878 py = c->tty.sy - 3 - height;
1879 offset += utf8_cstrwidth(c->prompt_string);
1880 if (offset > 2)
1881 offset -= 2;
1882 else
1883 offset = 0;
1885 if (menu_display(menu, MENU_NOMOUSE|MENU_TAB, 0, NULL, offset, py, c,
1886 BOX_LINES_DEFAULT, NULL, NULL, NULL, NULL,
1887 status_prompt_menu_callback, spm) != 0) {
1888 menu_free(menu);
1889 free(spm);
1890 return (NULL);
1892 return (NULL);
1895 /* Sort complete list. */
1896 static int
1897 status_prompt_complete_sort(const void *a, const void *b)
1899 const char **aa = (const char **)a, **bb = (const char **)b;
1901 return (strcmp(*aa, *bb));
1904 /* Complete a session. */
1905 static char *
1906 status_prompt_complete_session(char ***list, u_int *size, const char *s,
1907 char flag)
1909 struct session *loop;
1910 char *out, *tmp, n[11];
1912 RB_FOREACH(loop, sessions, &sessions) {
1913 if (*s == '\0' || strncmp(loop->name, s, strlen(s)) == 0) {
1914 *list = xreallocarray(*list, (*size) + 2,
1915 sizeof **list);
1916 xasprintf(&(*list)[(*size)++], "%s:", loop->name);
1917 } else if (*s == '$') {
1918 xsnprintf(n, sizeof n, "%u", loop->id);
1919 if (s[1] == '\0' ||
1920 strncmp(n, s + 1, strlen(s) - 1) == 0) {
1921 *list = xreallocarray(*list, (*size) + 2,
1922 sizeof **list);
1923 xasprintf(&(*list)[(*size)++], "$%s:", n);
1927 out = status_prompt_complete_prefix(*list, *size);
1928 if (out != NULL && flag != '\0') {
1929 xasprintf(&tmp, "-%c%s", flag, out);
1930 free(out);
1931 out = tmp;
1933 return (out);
1936 /* Complete word. */
1937 static char *
1938 status_prompt_complete(struct client *c, const char *word, u_int offset)
1940 struct session *session;
1941 const char *s, *colon;
1942 char **list = NULL, *copy = NULL, *out = NULL;
1943 char flag = '\0';
1944 u_int size = 0, i;
1946 if (*word == '\0' &&
1947 c->prompt_type != PROMPT_TYPE_TARGET &&
1948 c->prompt_type != PROMPT_TYPE_WINDOW_TARGET)
1949 return (NULL);
1951 if (c->prompt_type != PROMPT_TYPE_TARGET &&
1952 c->prompt_type != PROMPT_TYPE_WINDOW_TARGET &&
1953 strncmp(word, "-t", 2) != 0 &&
1954 strncmp(word, "-s", 2) != 0) {
1955 list = status_prompt_complete_list(&size, word, offset == 0);
1956 if (size == 0)
1957 out = NULL;
1958 else if (size == 1)
1959 xasprintf(&out, "%s ", list[0]);
1960 else
1961 out = status_prompt_complete_prefix(list, size);
1962 goto found;
1965 if (c->prompt_type == PROMPT_TYPE_TARGET ||
1966 c->prompt_type == PROMPT_TYPE_WINDOW_TARGET) {
1967 s = word;
1968 flag = '\0';
1969 } else {
1970 s = word + 2;
1971 flag = word[1];
1972 offset += 2;
1975 /* If this is a window completion, open the window menu. */
1976 if (c->prompt_type == PROMPT_TYPE_WINDOW_TARGET) {
1977 out = status_prompt_complete_window_menu(c, c->session, s,
1978 offset, '\0');
1979 goto found;
1981 colon = strchr(s, ':');
1983 /* If there is no colon, complete as a session. */
1984 if (colon == NULL) {
1985 out = status_prompt_complete_session(&list, &size, s, flag);
1986 goto found;
1989 /* If there is a colon but no period, find session and show a menu. */
1990 if (strchr(colon + 1, '.') == NULL) {
1991 if (*s == ':')
1992 session = c->session;
1993 else {
1994 copy = xstrdup(s);
1995 *strchr(copy, ':') = '\0';
1996 session = session_find(copy);
1997 free(copy);
1998 if (session == NULL)
1999 goto found;
2001 out = status_prompt_complete_window_menu(c, session, colon + 1,
2002 offset, flag);
2003 if (out == NULL)
2004 return (NULL);
2007 found:
2008 if (size != 0) {
2009 qsort(list, size, sizeof *list, status_prompt_complete_sort);
2010 for (i = 0; i < size; i++)
2011 log_debug("complete %u: %s", i, list[i]);
2014 if (out != NULL && strcmp(word, out) == 0) {
2015 free(out);
2016 out = NULL;
2018 if (out != NULL ||
2019 !status_prompt_complete_list_menu(c, list, size, offset, flag)) {
2020 for (i = 0; i < size; i++)
2021 free(list[i]);
2022 free(list);
2024 return (out);
2027 /* Return the type of the prompt as an enum. */
2028 enum prompt_type
2029 status_prompt_type(const char *type)
2031 u_int i;
2033 for (i = 0; i < PROMPT_NTYPES; i++) {
2034 if (strcmp(type, status_prompt_type_string(i)) == 0)
2035 return (i);
2037 return (PROMPT_TYPE_INVALID);
2040 /* Accessor for prompt_type_strings. */
2041 const char *
2042 status_prompt_type_string(u_int type)
2044 if (type >= PROMPT_NTYPES)
2045 return ("invalid");
2046 return (prompt_type_strings[type]);