Merge branch 'obsd-master'
[tmux.git] / status.c
bloba15981e9c2021156b0f567e026f1aeb51a1feb10
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;
478 status_message_clear(c);
479 status_push_screen(c);
481 va_start(ap, fmt);
482 xvasprintf(&c->message_string, fmt, ap);
483 va_end(ap);
485 server_add_message("%s message: %s", c->name, c->message_string);
488 * With delay -1, the display-time option is used; zero means wait for
489 * key press; more than zero is the actual delay time in milliseconds.
491 if (delay == -1)
492 delay = options_get_number(c->session->options, "display-time");
493 if (delay > 0) {
494 tv.tv_sec = delay / 1000;
495 tv.tv_usec = (delay % 1000) * 1000L;
497 if (event_initialized(&c->message_timer))
498 evtimer_del(&c->message_timer);
499 evtimer_set(&c->message_timer, status_message_callback, c);
501 evtimer_add(&c->message_timer, &tv);
504 if (delay != 0)
505 c->message_ignore_keys = ignore_keys;
506 c->message_ignore_styles = ignore_styles;
508 c->tty.flags |= (TTY_NOCURSOR|TTY_FREEZE);
509 c->flags |= CLIENT_REDRAWSTATUS;
512 /* Clear status line message. */
513 void
514 status_message_clear(struct client *c)
516 if (c->message_string == NULL)
517 return;
519 free(c->message_string);
520 c->message_string = NULL;
522 if (c->prompt_string == NULL)
523 c->tty.flags &= ~(TTY_NOCURSOR|TTY_FREEZE);
524 c->flags |= CLIENT_ALLREDRAWFLAGS; /* was frozen and may have changed */
526 status_pop_screen(c);
529 /* Clear status line message after timer expires. */
530 static void
531 status_message_callback(__unused int fd, __unused short event, void *data)
533 struct client *c = data;
535 status_message_clear(c);
538 /* Draw client message on status line of present else on last line. */
540 status_message_redraw(struct client *c)
542 struct status_line *sl = &c->status;
543 struct screen_write_ctx ctx;
544 struct session *s = c->session;
545 struct screen old_screen;
546 size_t len;
547 u_int lines, offset, messageline;
548 struct grid_cell gc;
549 struct format_tree *ft;
551 if (c->tty.sx == 0 || c->tty.sy == 0)
552 return (0);
553 memcpy(&old_screen, sl->active, sizeof old_screen);
555 lines = status_line_size(c);
556 if (lines <= 1)
557 lines = 1;
558 screen_init(sl->active, c->tty.sx, lines, 0);
560 messageline = status_prompt_line_at(c);
561 if (messageline > lines - 1)
562 messageline = lines - 1;
564 len = screen_write_strlen("%s", c->message_string);
565 if (len > c->tty.sx)
566 len = c->tty.sx;
568 ft = format_create_defaults(NULL, c, NULL, NULL, NULL);
569 style_apply(&gc, s->options, "message-style", ft);
570 format_free(ft);
572 screen_write_start(&ctx, sl->active);
573 screen_write_fast_copy(&ctx, &sl->screen, 0, 0, c->tty.sx, lines);
574 screen_write_cursormove(&ctx, 0, messageline, 0);
575 for (offset = 0; offset < c->tty.sx; offset++)
576 screen_write_putc(&ctx, &gc, ' ');
577 screen_write_cursormove(&ctx, 0, messageline, 0);
578 if (c->message_ignore_styles)
579 screen_write_nputs(&ctx, len, &gc, "%s", c->message_string);
580 else
581 format_draw(&ctx, &gc, c->tty.sx, c->message_string, NULL, 0);
582 screen_write_stop(&ctx);
584 if (grid_compare(sl->active->grid, old_screen.grid) == 0) {
585 screen_free(&old_screen);
586 return (0);
588 screen_free(&old_screen);
589 return (1);
592 /* Enable status line prompt. */
593 void
594 status_prompt_set(struct client *c, struct cmd_find_state *fs,
595 const char *msg, const char *input, prompt_input_cb inputcb,
596 prompt_free_cb freecb, void *data, int flags, enum prompt_type prompt_type)
598 struct format_tree *ft;
599 char *tmp;
601 if (fs != NULL)
602 ft = format_create_from_state(NULL, c, fs);
603 else
604 ft = format_create_defaults(NULL, c, NULL, NULL, NULL);
606 if (input == NULL)
607 input = "";
608 if (flags & PROMPT_NOFORMAT)
609 tmp = xstrdup(input);
610 else
611 tmp = format_expand_time(ft, input);
613 status_message_clear(c);
614 status_prompt_clear(c);
615 status_push_screen(c);
617 c->prompt_string = format_expand_time(ft, msg);
619 if (flags & PROMPT_INCREMENTAL) {
620 c->prompt_last = xstrdup(tmp);
621 c->prompt_buffer = utf8_fromcstr("");
622 } else {
623 c->prompt_last = NULL;
624 c->prompt_buffer = utf8_fromcstr(tmp);
626 c->prompt_index = utf8_strlen(c->prompt_buffer);
628 c->prompt_inputcb = inputcb;
629 c->prompt_freecb = freecb;
630 c->prompt_data = data;
632 memset(c->prompt_hindex, 0, sizeof c->prompt_hindex);
634 c->prompt_flags = flags;
635 c->prompt_type = prompt_type;
636 c->prompt_mode = PROMPT_ENTRY;
638 if (~flags & PROMPT_INCREMENTAL)
639 c->tty.flags |= (TTY_NOCURSOR|TTY_FREEZE);
640 c->flags |= CLIENT_REDRAWSTATUS;
642 if (flags & PROMPT_INCREMENTAL)
643 c->prompt_inputcb(c, c->prompt_data, "=", 0);
645 free(tmp);
646 format_free(ft);
649 /* Remove status line prompt. */
650 void
651 status_prompt_clear(struct client *c)
653 if (c->prompt_string == NULL)
654 return;
656 if (c->prompt_freecb != NULL && c->prompt_data != NULL)
657 c->prompt_freecb(c->prompt_data);
659 free(c->prompt_last);
660 c->prompt_last = NULL;
662 free(c->prompt_string);
663 c->prompt_string = NULL;
665 free(c->prompt_buffer);
666 c->prompt_buffer = NULL;
668 free(c->prompt_saved);
669 c->prompt_saved = NULL;
671 c->tty.flags &= ~(TTY_NOCURSOR|TTY_FREEZE);
672 c->flags |= CLIENT_ALLREDRAWFLAGS; /* was frozen and may have changed */
674 status_pop_screen(c);
677 /* Update status line prompt with a new prompt string. */
678 void
679 status_prompt_update(struct client *c, const char *msg, const char *input)
681 struct format_tree *ft;
682 char *tmp;
684 ft = format_create(c, NULL, FORMAT_NONE, 0);
685 format_defaults(ft, c, NULL, NULL, NULL);
687 tmp = format_expand_time(ft, input);
689 free(c->prompt_string);
690 c->prompt_string = format_expand_time(ft, msg);
692 free(c->prompt_buffer);
693 c->prompt_buffer = utf8_fromcstr(tmp);
694 c->prompt_index = utf8_strlen(c->prompt_buffer);
696 memset(c->prompt_hindex, 0, sizeof c->prompt_hindex);
698 c->flags |= CLIENT_REDRAWSTATUS;
700 free(tmp);
701 format_free(ft);
704 /* Draw client prompt on status line of present else on last line. */
706 status_prompt_redraw(struct client *c)
708 struct status_line *sl = &c->status;
709 struct screen_write_ctx ctx;
710 struct session *s = c->session;
711 struct screen old_screen;
712 u_int i, lines, offset, left, start, width;
713 u_int pcursor, pwidth, promptline;
714 struct grid_cell gc, cursorgc;
715 struct format_tree *ft;
717 if (c->tty.sx == 0 || c->tty.sy == 0)
718 return (0);
719 memcpy(&old_screen, sl->active, sizeof old_screen);
721 lines = status_line_size(c);
722 if (lines <= 1)
723 lines = 1;
724 screen_init(sl->active, c->tty.sx, lines, 0);
726 promptline = status_prompt_line_at(c);
727 if (promptline > lines - 1)
728 promptline = lines - 1;
730 ft = format_create_defaults(NULL, c, NULL, NULL, NULL);
731 if (c->prompt_mode == PROMPT_COMMAND)
732 style_apply(&gc, s->options, "message-command-style", ft);
733 else
734 style_apply(&gc, s->options, "message-style", ft);
735 format_free(ft);
737 memcpy(&cursorgc, &gc, sizeof cursorgc);
738 cursorgc.attr ^= GRID_ATTR_REVERSE;
740 start = format_width(c->prompt_string);
741 if (start > c->tty.sx)
742 start = c->tty.sx;
744 screen_write_start(&ctx, sl->active);
745 screen_write_fast_copy(&ctx, &sl->screen, 0, 0, c->tty.sx, lines);
746 screen_write_cursormove(&ctx, 0, promptline, 0);
747 for (offset = 0; offset < c->tty.sx; offset++)
748 screen_write_putc(&ctx, &gc, ' ');
749 screen_write_cursormove(&ctx, 0, promptline, 0);
750 format_draw(&ctx, &gc, start, c->prompt_string, NULL, 0);
751 screen_write_cursormove(&ctx, start, promptline, 0);
753 left = c->tty.sx - start;
754 if (left == 0)
755 goto finished;
757 pcursor = utf8_strwidth(c->prompt_buffer, c->prompt_index);
758 pwidth = utf8_strwidth(c->prompt_buffer, -1);
759 if (pcursor >= left) {
761 * The cursor would be outside the screen so start drawing
762 * with it on the right.
764 offset = (pcursor - left) + 1;
765 pwidth = left;
766 } else
767 offset = 0;
768 if (pwidth > left)
769 pwidth = left;
770 c->prompt_cursor = start + c->prompt_index - offset;
772 width = 0;
773 for (i = 0; c->prompt_buffer[i].size != 0; i++) {
774 if (width < offset) {
775 width += c->prompt_buffer[i].width;
776 continue;
778 if (width >= offset + pwidth)
779 break;
780 width += c->prompt_buffer[i].width;
781 if (width > offset + pwidth)
782 break;
784 if (i != c->prompt_index) {
785 utf8_copy(&gc.data, &c->prompt_buffer[i]);
786 screen_write_cell(&ctx, &gc);
787 } else {
788 utf8_copy(&cursorgc.data, &c->prompt_buffer[i]);
789 screen_write_cell(&ctx, &cursorgc);
792 if (sl->active->cx < screen_size_x(sl->active) && c->prompt_index >= i)
793 screen_write_putc(&ctx, &cursorgc, ' ');
795 finished:
796 screen_write_stop(&ctx);
798 if (grid_compare(sl->active->grid, old_screen.grid) == 0) {
799 screen_free(&old_screen);
800 return (0);
802 screen_free(&old_screen);
803 return (1);
806 /* Is this a separator? */
807 static int
808 status_prompt_in_list(const char *ws, const struct utf8_data *ud)
810 if (ud->size != 1 || ud->width != 1)
811 return (0);
812 return (strchr(ws, *ud->data) != NULL);
815 /* Is this a space? */
816 static int
817 status_prompt_space(const struct utf8_data *ud)
819 if (ud->size != 1 || ud->width != 1)
820 return (0);
821 return (*ud->data == ' ');
825 * Translate key from vi to emacs. Return 0 to drop key, 1 to process the key
826 * as an emacs key; return 2 to append to the buffer.
828 static int
829 status_prompt_translate_key(struct client *c, key_code key, key_code *new_key)
831 if (c->prompt_mode == PROMPT_ENTRY) {
832 switch (key) {
833 case '\001': /* C-a */
834 case '\003': /* C-c */
835 case '\005': /* C-e */
836 case '\007': /* C-g */
837 case '\010': /* C-h */
838 case '\011': /* Tab */
839 case '\013': /* C-k */
840 case '\016': /* C-n */
841 case '\020': /* C-p */
842 case '\024': /* C-t */
843 case '\025': /* C-u */
844 case '\027': /* C-w */
845 case '\031': /* C-y */
846 case '\n':
847 case '\r':
848 case KEYC_LEFT|KEYC_CTRL:
849 case KEYC_RIGHT|KEYC_CTRL:
850 case KEYC_BSPACE:
851 case KEYC_DC:
852 case KEYC_DOWN:
853 case KEYC_END:
854 case KEYC_HOME:
855 case KEYC_LEFT:
856 case KEYC_RIGHT:
857 case KEYC_UP:
858 *new_key = key;
859 return (1);
860 case '\033': /* Escape */
861 c->prompt_mode = PROMPT_COMMAND;
862 c->flags |= CLIENT_REDRAWSTATUS;
863 return (0);
865 *new_key = key;
866 return (2);
869 switch (key) {
870 case KEYC_BSPACE:
871 *new_key = KEYC_LEFT;
872 return (1);
873 case 'A':
874 case 'I':
875 case 'C':
876 case 's':
877 case 'a':
878 c->prompt_mode = PROMPT_ENTRY;
879 c->flags |= CLIENT_REDRAWSTATUS;
880 break; /* switch mode and... */
881 case 'S':
882 c->prompt_mode = PROMPT_ENTRY;
883 c->flags |= CLIENT_REDRAWSTATUS;
884 *new_key = '\025'; /* C-u */
885 return (1);
886 case 'i':
887 case '\033': /* Escape */
888 c->prompt_mode = PROMPT_ENTRY;
889 c->flags |= CLIENT_REDRAWSTATUS;
890 return (0);
893 switch (key) {
894 case 'A':
895 case '$':
896 *new_key = KEYC_END;
897 return (1);
898 case 'I':
899 case '0':
900 case '^':
901 *new_key = KEYC_HOME;
902 return (1);
903 case 'C':
904 case 'D':
905 *new_key = '\013'; /* C-k */
906 return (1);
907 case KEYC_BSPACE:
908 case 'X':
909 *new_key = KEYC_BSPACE;
910 return (1);
911 case 'b':
912 *new_key = 'b'|KEYC_META;
913 return (1);
914 case 'B':
915 *new_key = 'B'|KEYC_VI;
916 return (1);
917 case 'd':
918 *new_key = '\025'; /* C-u */
919 return (1);
920 case 'e':
921 *new_key = 'e'|KEYC_VI;
922 return (1);
923 case 'E':
924 *new_key = 'E'|KEYC_VI;
925 return (1);
926 case 'w':
927 *new_key = 'w'|KEYC_VI;
928 return (1);
929 case 'W':
930 *new_key = 'W'|KEYC_VI;
931 return (1);
932 case 'p':
933 *new_key = '\031'; /* C-y */
934 return (1);
935 case 'q':
936 *new_key = '\003'; /* C-c */
937 return (1);
938 case 's':
939 case KEYC_DC:
940 case 'x':
941 *new_key = KEYC_DC;
942 return (1);
943 case KEYC_DOWN:
944 case 'j':
945 *new_key = KEYC_DOWN;
946 return (1);
947 case KEYC_LEFT:
948 case 'h':
949 *new_key = KEYC_LEFT;
950 return (1);
951 case 'a':
952 case KEYC_RIGHT:
953 case 'l':
954 *new_key = KEYC_RIGHT;
955 return (1);
956 case KEYC_UP:
957 case 'k':
958 *new_key = KEYC_UP;
959 return (1);
960 case '\010' /* C-h */:
961 case '\003' /* C-c */:
962 case '\n':
963 case '\r':
964 return (1);
966 return (0);
969 /* Paste into prompt. */
970 static int
971 status_prompt_paste(struct client *c)
973 struct paste_buffer *pb;
974 const char *bufdata;
975 size_t size, n, bufsize;
976 u_int i;
977 struct utf8_data *ud, *udp;
978 enum utf8_state more;
980 size = utf8_strlen(c->prompt_buffer);
981 if (c->prompt_saved != NULL) {
982 ud = c->prompt_saved;
983 n = utf8_strlen(c->prompt_saved);
984 } else {
985 if ((pb = paste_get_top(NULL)) == NULL)
986 return (0);
987 bufdata = paste_buffer_data(pb, &bufsize);
988 ud = xreallocarray(NULL, bufsize + 1, sizeof *ud);
989 udp = ud;
990 for (i = 0; i != bufsize; /* nothing */) {
991 more = utf8_open(udp, bufdata[i]);
992 if (more == UTF8_MORE) {
993 while (++i != bufsize && more == UTF8_MORE)
994 more = utf8_append(udp, bufdata[i]);
995 if (more == UTF8_DONE) {
996 udp++;
997 continue;
999 i -= udp->have;
1001 if (bufdata[i] <= 31 || bufdata[i] >= 127)
1002 break;
1003 utf8_set(udp, bufdata[i]);
1004 udp++;
1005 i++;
1007 udp->size = 0;
1008 n = udp - ud;
1010 if (n == 0)
1011 return (0);
1013 c->prompt_buffer = xreallocarray(c->prompt_buffer, size + n + 1,
1014 sizeof *c->prompt_buffer);
1015 if (c->prompt_index == size) {
1016 memcpy(c->prompt_buffer + c->prompt_index, ud,
1017 n * sizeof *c->prompt_buffer);
1018 c->prompt_index += n;
1019 c->prompt_buffer[c->prompt_index].size = 0;
1020 } else {
1021 memmove(c->prompt_buffer + c->prompt_index + n,
1022 c->prompt_buffer + c->prompt_index,
1023 (size + 1 - c->prompt_index) * sizeof *c->prompt_buffer);
1024 memcpy(c->prompt_buffer + c->prompt_index, ud,
1025 n * sizeof *c->prompt_buffer);
1026 c->prompt_index += n;
1029 if (ud != c->prompt_saved)
1030 free(ud);
1031 return (1);
1034 /* Finish completion. */
1035 static int
1036 status_prompt_replace_complete(struct client *c, const char *s)
1038 char word[64], *allocated = NULL;
1039 size_t size, n, off, idx, used;
1040 struct utf8_data *first, *last, *ud;
1042 /* Work out where the cursor currently is. */
1043 idx = c->prompt_index;
1044 if (idx != 0)
1045 idx--;
1046 size = utf8_strlen(c->prompt_buffer);
1048 /* Find the word we are in. */
1049 first = &c->prompt_buffer[idx];
1050 while (first > c->prompt_buffer && !status_prompt_space(first))
1051 first--;
1052 while (first->size != 0 && status_prompt_space(first))
1053 first++;
1054 last = &c->prompt_buffer[idx];
1055 while (last->size != 0 && !status_prompt_space(last))
1056 last++;
1057 while (last > c->prompt_buffer && status_prompt_space(last))
1058 last--;
1059 if (last->size != 0)
1060 last++;
1061 if (last < first)
1062 return (0);
1063 if (s == NULL) {
1064 used = 0;
1065 for (ud = first; ud < last; ud++) {
1066 if (used + ud->size >= sizeof word)
1067 break;
1068 memcpy(word + used, ud->data, ud->size);
1069 used += ud->size;
1071 if (ud != last)
1072 return (0);
1073 word[used] = '\0';
1076 /* Try to complete it. */
1077 if (s == NULL) {
1078 allocated = status_prompt_complete(c, word,
1079 first - c->prompt_buffer);
1080 if (allocated == NULL)
1081 return (0);
1082 s = allocated;
1085 /* Trim out word. */
1086 n = size - (last - c->prompt_buffer) + 1; /* with \0 */
1087 memmove(first, last, n * sizeof *c->prompt_buffer);
1088 size -= last - first;
1090 /* Insert the new word. */
1091 size += strlen(s);
1092 off = first - c->prompt_buffer;
1093 c->prompt_buffer = xreallocarray(c->prompt_buffer, size + 1,
1094 sizeof *c->prompt_buffer);
1095 first = c->prompt_buffer + off;
1096 memmove(first + strlen(s), first, n * sizeof *c->prompt_buffer);
1097 for (idx = 0; idx < strlen(s); idx++)
1098 utf8_set(&first[idx], s[idx]);
1099 c->prompt_index = (first - c->prompt_buffer) + strlen(s);
1101 free(allocated);
1102 return (1);
1105 /* Prompt forward to the next beginning of a word. */
1106 static void
1107 status_prompt_forward_word(struct client *c, size_t size, int vi,
1108 const char *separators)
1110 size_t idx = c->prompt_index;
1111 int word_is_separators;
1113 /* In emacs mode, skip until the first non-whitespace character. */
1114 if (!vi)
1115 while (idx != size &&
1116 status_prompt_space(&c->prompt_buffer[idx]))
1117 idx++;
1119 /* Can't move forward if we're already at the end. */
1120 if (idx == size) {
1121 c->prompt_index = idx;
1122 return;
1125 /* Determine the current character class (separators or not). */
1126 word_is_separators = status_prompt_in_list(separators,
1127 &c->prompt_buffer[idx]) &&
1128 !status_prompt_space(&c->prompt_buffer[idx]);
1130 /* Skip ahead until the first space or opposite character class. */
1131 do {
1132 idx++;
1133 if (status_prompt_space(&c->prompt_buffer[idx])) {
1134 /* In vi mode, go to the start of the next word. */
1135 if (vi)
1136 while (idx != size &&
1137 status_prompt_space(&c->prompt_buffer[idx]))
1138 idx++;
1139 break;
1141 } while (idx != size && word_is_separators == status_prompt_in_list(
1142 separators, &c->prompt_buffer[idx]));
1144 c->prompt_index = idx;
1147 /* Prompt forward to the next end of a word. */
1148 static void
1149 status_prompt_end_word(struct client *c, size_t size, const char *separators)
1151 size_t idx = c->prompt_index;
1152 int word_is_separators;
1154 /* Can't move forward if we're already at the end. */
1155 if (idx == size)
1156 return;
1158 /* Find the next word. */
1159 do {
1160 idx++;
1161 if (idx == size) {
1162 c->prompt_index = idx;
1163 return;
1165 } while (status_prompt_space(&c->prompt_buffer[idx]));
1167 /* Determine the character class (separators or not). */
1168 word_is_separators = status_prompt_in_list(separators,
1169 &c->prompt_buffer[idx]);
1171 /* Skip ahead until the next space or opposite character class. */
1172 do {
1173 idx++;
1174 if (idx == size)
1175 break;
1176 } while (!status_prompt_space(&c->prompt_buffer[idx]) &&
1177 word_is_separators == status_prompt_in_list(separators,
1178 &c->prompt_buffer[idx]));
1180 /* Back up to the previous character to stop at the end of the word. */
1181 c->prompt_index = idx - 1;
1184 /* Prompt backward to the previous beginning of a word. */
1185 static void
1186 status_prompt_backward_word(struct client *c, const char *separators)
1188 size_t idx = c->prompt_index;
1189 int word_is_separators;
1191 /* Find non-whitespace. */
1192 while (idx != 0) {
1193 --idx;
1194 if (!status_prompt_space(&c->prompt_buffer[idx]))
1195 break;
1197 word_is_separators = status_prompt_in_list(separators,
1198 &c->prompt_buffer[idx]);
1200 /* Find the character before the beginning of the word. */
1201 while (idx != 0) {
1202 --idx;
1203 if (status_prompt_space(&c->prompt_buffer[idx]) ||
1204 word_is_separators != status_prompt_in_list(separators,
1205 &c->prompt_buffer[idx])) {
1206 /* Go back to the word. */
1207 idx++;
1208 break;
1211 c->prompt_index = idx;
1214 /* Handle keys in prompt. */
1216 status_prompt_key(struct client *c, key_code key)
1218 struct options *oo = c->session->options;
1219 char *s, *cp, prefix = '=';
1220 const char *histstr, *separators = NULL, *keystring;
1221 size_t size, idx;
1222 struct utf8_data tmp;
1223 int keys, word_is_separators;
1225 if (c->prompt_flags & PROMPT_KEY) {
1226 keystring = key_string_lookup_key(key, 0);
1227 c->prompt_inputcb(c, c->prompt_data, keystring, 1);
1228 status_prompt_clear(c);
1229 return (0);
1231 size = utf8_strlen(c->prompt_buffer);
1233 if (c->prompt_flags & PROMPT_NUMERIC) {
1234 if (key >= '0' && key <= '9')
1235 goto append_key;
1236 s = utf8_tocstr(c->prompt_buffer);
1237 c->prompt_inputcb(c, c->prompt_data, s, 1);
1238 status_prompt_clear(c);
1239 free(s);
1240 return (1);
1242 key &= ~KEYC_MASK_FLAGS;
1244 keys = options_get_number(c->session->options, "status-keys");
1245 if (keys == MODEKEY_VI) {
1246 switch (status_prompt_translate_key(c, key, &key)) {
1247 case 1:
1248 goto process_key;
1249 case 2:
1250 goto append_key;
1251 default:
1252 return (0);
1256 process_key:
1257 switch (key) {
1258 case KEYC_LEFT:
1259 case '\002': /* C-b */
1260 if (c->prompt_index > 0) {
1261 c->prompt_index--;
1262 break;
1264 break;
1265 case KEYC_RIGHT:
1266 case '\006': /* C-f */
1267 if (c->prompt_index < size) {
1268 c->prompt_index++;
1269 break;
1271 break;
1272 case KEYC_HOME:
1273 case '\001': /* C-a */
1274 if (c->prompt_index != 0) {
1275 c->prompt_index = 0;
1276 break;
1278 break;
1279 case KEYC_END:
1280 case '\005': /* C-e */
1281 if (c->prompt_index != size) {
1282 c->prompt_index = size;
1283 break;
1285 break;
1286 case '\011': /* Tab */
1287 if (status_prompt_replace_complete(c, NULL))
1288 goto changed;
1289 break;
1290 case KEYC_BSPACE:
1291 case '\010': /* C-h */
1292 if (c->prompt_index != 0) {
1293 if (c->prompt_index == size)
1294 c->prompt_buffer[--c->prompt_index].size = 0;
1295 else {
1296 memmove(c->prompt_buffer + c->prompt_index - 1,
1297 c->prompt_buffer + c->prompt_index,
1298 (size + 1 - c->prompt_index) *
1299 sizeof *c->prompt_buffer);
1300 c->prompt_index--;
1302 goto changed;
1304 break;
1305 case KEYC_DC:
1306 case '\004': /* C-d */
1307 if (c->prompt_index != size) {
1308 memmove(c->prompt_buffer + c->prompt_index,
1309 c->prompt_buffer + c->prompt_index + 1,
1310 (size + 1 - c->prompt_index) *
1311 sizeof *c->prompt_buffer);
1312 goto changed;
1314 break;
1315 case '\025': /* C-u */
1316 c->prompt_buffer[0].size = 0;
1317 c->prompt_index = 0;
1318 goto changed;
1319 case '\013': /* C-k */
1320 if (c->prompt_index < size) {
1321 c->prompt_buffer[c->prompt_index].size = 0;
1322 goto changed;
1324 break;
1325 case '\027': /* C-w */
1326 separators = options_get_string(oo, "word-separators");
1327 idx = c->prompt_index;
1329 /* Find non-whitespace. */
1330 while (idx != 0) {
1331 idx--;
1332 if (!status_prompt_space(&c->prompt_buffer[idx]))
1333 break;
1335 word_is_separators = status_prompt_in_list(separators,
1336 &c->prompt_buffer[idx]);
1338 /* Find the character before the beginning of the word. */
1339 while (idx != 0) {
1340 idx--;
1341 if (status_prompt_space(&c->prompt_buffer[idx]) ||
1342 word_is_separators != status_prompt_in_list(
1343 separators, &c->prompt_buffer[idx])) {
1344 /* Go back to the word. */
1345 idx++;
1346 break;
1350 free(c->prompt_saved);
1351 c->prompt_saved = xcalloc(sizeof *c->prompt_buffer,
1352 (c->prompt_index - idx) + 1);
1353 memcpy(c->prompt_saved, c->prompt_buffer + idx,
1354 (c->prompt_index - idx) * sizeof *c->prompt_buffer);
1356 memmove(c->prompt_buffer + idx,
1357 c->prompt_buffer + c->prompt_index,
1358 (size + 1 - c->prompt_index) *
1359 sizeof *c->prompt_buffer);
1360 memset(c->prompt_buffer + size - (c->prompt_index - idx),
1361 '\0', (c->prompt_index - idx) * sizeof *c->prompt_buffer);
1362 c->prompt_index = idx;
1364 goto changed;
1365 case KEYC_RIGHT|KEYC_CTRL:
1366 case 'f'|KEYC_META:
1367 separators = options_get_string(oo, "word-separators");
1368 status_prompt_forward_word(c, size, 0, separators);
1369 goto changed;
1370 case 'E'|KEYC_VI:
1371 status_prompt_end_word(c, size, "");
1372 goto changed;
1373 case 'e'|KEYC_VI:
1374 separators = options_get_string(oo, "word-separators");
1375 status_prompt_end_word(c, size, separators);
1376 goto changed;
1377 case 'W'|KEYC_VI:
1378 status_prompt_forward_word(c, size, 1, "");
1379 goto changed;
1380 case 'w'|KEYC_VI:
1381 separators = options_get_string(oo, "word-separators");
1382 status_prompt_forward_word(c, size, 1, separators);
1383 goto changed;
1384 case 'B'|KEYC_VI:
1385 status_prompt_backward_word(c, "");
1386 goto changed;
1387 case KEYC_LEFT|KEYC_CTRL:
1388 case 'b'|KEYC_META:
1389 separators = options_get_string(oo, "word-separators");
1390 status_prompt_backward_word(c, separators);
1391 goto changed;
1392 case KEYC_UP:
1393 case '\020': /* C-p */
1394 histstr = status_prompt_up_history(c->prompt_hindex,
1395 c->prompt_type);
1396 if (histstr == NULL)
1397 break;
1398 free(c->prompt_buffer);
1399 c->prompt_buffer = utf8_fromcstr(histstr);
1400 c->prompt_index = utf8_strlen(c->prompt_buffer);
1401 goto changed;
1402 case KEYC_DOWN:
1403 case '\016': /* C-n */
1404 histstr = status_prompt_down_history(c->prompt_hindex,
1405 c->prompt_type);
1406 if (histstr == NULL)
1407 break;
1408 free(c->prompt_buffer);
1409 c->prompt_buffer = utf8_fromcstr(histstr);
1410 c->prompt_index = utf8_strlen(c->prompt_buffer);
1411 goto changed;
1412 case '\031': /* C-y */
1413 if (status_prompt_paste(c))
1414 goto changed;
1415 break;
1416 case '\024': /* C-t */
1417 idx = c->prompt_index;
1418 if (idx < size)
1419 idx++;
1420 if (idx >= 2) {
1421 utf8_copy(&tmp, &c->prompt_buffer[idx - 2]);
1422 utf8_copy(&c->prompt_buffer[idx - 2],
1423 &c->prompt_buffer[idx - 1]);
1424 utf8_copy(&c->prompt_buffer[idx - 1], &tmp);
1425 c->prompt_index = idx;
1426 goto changed;
1428 break;
1429 case '\r':
1430 case '\n':
1431 s = utf8_tocstr(c->prompt_buffer);
1432 if (*s != '\0')
1433 status_prompt_add_history(s, c->prompt_type);
1434 if (c->prompt_inputcb(c, c->prompt_data, s, 1) == 0)
1435 status_prompt_clear(c);
1436 free(s);
1437 break;
1438 case '\033': /* Escape */
1439 case '\003': /* C-c */
1440 case '\007': /* C-g */
1441 if (c->prompt_inputcb(c, c->prompt_data, NULL, 1) == 0)
1442 status_prompt_clear(c);
1443 break;
1444 case '\022': /* C-r */
1445 if (~c->prompt_flags & PROMPT_INCREMENTAL)
1446 break;
1447 if (c->prompt_buffer[0].size == 0) {
1448 prefix = '=';
1449 free(c->prompt_buffer);
1450 c->prompt_buffer = utf8_fromcstr(c->prompt_last);
1451 c->prompt_index = utf8_strlen(c->prompt_buffer);
1452 } else
1453 prefix = '-';
1454 goto changed;
1455 case '\023': /* C-s */
1456 if (~c->prompt_flags & PROMPT_INCREMENTAL)
1457 break;
1458 if (c->prompt_buffer[0].size == 0) {
1459 prefix = '=';
1460 free(c->prompt_buffer);
1461 c->prompt_buffer = utf8_fromcstr(c->prompt_last);
1462 c->prompt_index = utf8_strlen(c->prompt_buffer);
1463 } else
1464 prefix = '+';
1465 goto changed;
1466 default:
1467 goto append_key;
1470 c->flags |= CLIENT_REDRAWSTATUS;
1471 return (0);
1473 append_key:
1474 if (key <= 0x7f)
1475 utf8_set(&tmp, key);
1476 else if (KEYC_IS_UNICODE(key))
1477 utf8_to_data(key, &tmp);
1478 else
1479 return (0);
1481 c->prompt_buffer = xreallocarray(c->prompt_buffer, size + 2,
1482 sizeof *c->prompt_buffer);
1484 if (c->prompt_index == size) {
1485 utf8_copy(&c->prompt_buffer[c->prompt_index], &tmp);
1486 c->prompt_index++;
1487 c->prompt_buffer[c->prompt_index].size = 0;
1488 } else {
1489 memmove(c->prompt_buffer + c->prompt_index + 1,
1490 c->prompt_buffer + c->prompt_index,
1491 (size + 1 - c->prompt_index) *
1492 sizeof *c->prompt_buffer);
1493 utf8_copy(&c->prompt_buffer[c->prompt_index], &tmp);
1494 c->prompt_index++;
1497 if (c->prompt_flags & PROMPT_SINGLE) {
1498 if (utf8_strlen(c->prompt_buffer) != 1)
1499 status_prompt_clear(c);
1500 else {
1501 s = utf8_tocstr(c->prompt_buffer);
1502 if (c->prompt_inputcb(c, c->prompt_data, s, 1) == 0)
1503 status_prompt_clear(c);
1504 free(s);
1508 changed:
1509 c->flags |= CLIENT_REDRAWSTATUS;
1510 if (c->prompt_flags & PROMPT_INCREMENTAL) {
1511 s = utf8_tocstr(c->prompt_buffer);
1512 xasprintf(&cp, "%c%s", prefix, s);
1513 c->prompt_inputcb(c, c->prompt_data, cp, 0);
1514 free(cp);
1515 free(s);
1517 return (0);
1520 /* Get previous line from the history. */
1521 static const char *
1522 status_prompt_up_history(u_int *idx, u_int type)
1525 * History runs from 0 to size - 1. Index is from 0 to size. Zero is
1526 * empty.
1529 if (status_prompt_hsize[type] == 0 ||
1530 idx[type] == status_prompt_hsize[type])
1531 return (NULL);
1532 idx[type]++;
1533 return (status_prompt_hlist[type][status_prompt_hsize[type] - idx[type]]);
1536 /* Get next line from the history. */
1537 static const char *
1538 status_prompt_down_history(u_int *idx, u_int type)
1540 if (status_prompt_hsize[type] == 0 || idx[type] == 0)
1541 return ("");
1542 idx[type]--;
1543 if (idx[type] == 0)
1544 return ("");
1545 return (status_prompt_hlist[type][status_prompt_hsize[type] - idx[type]]);
1548 /* Add line to the history. */
1549 static void
1550 status_prompt_add_history(const char *line, u_int type)
1552 u_int i, oldsize, newsize, freecount, hlimit, new = 1;
1553 size_t movesize;
1555 oldsize = status_prompt_hsize[type];
1556 if (oldsize > 0 &&
1557 strcmp(status_prompt_hlist[type][oldsize - 1], line) == 0)
1558 new = 0;
1560 hlimit = options_get_number(global_options, "prompt-history-limit");
1561 if (hlimit > oldsize) {
1562 if (new == 0)
1563 return;
1564 newsize = oldsize + new;
1565 } else {
1566 newsize = hlimit;
1567 freecount = oldsize + new - newsize;
1568 if (freecount > oldsize)
1569 freecount = oldsize;
1570 if (freecount == 0)
1571 return;
1572 for (i = 0; i < freecount; i++)
1573 free(status_prompt_hlist[type][i]);
1574 movesize = (oldsize - freecount) *
1575 sizeof *status_prompt_hlist[type];
1576 if (movesize > 0) {
1577 memmove(&status_prompt_hlist[type][0],
1578 &status_prompt_hlist[type][freecount], movesize);
1582 if (newsize == 0) {
1583 free(status_prompt_hlist[type]);
1584 status_prompt_hlist[type] = NULL;
1585 } else if (newsize != oldsize) {
1586 status_prompt_hlist[type] =
1587 xreallocarray(status_prompt_hlist[type], newsize,
1588 sizeof *status_prompt_hlist[type]);
1591 if (new == 1 && newsize > 0)
1592 status_prompt_hlist[type][newsize - 1] = xstrdup(line);
1593 status_prompt_hsize[type] = newsize;
1596 /* Add to completion list. */
1597 static void
1598 status_prompt_add_list(char ***list, u_int *size, const char *s)
1600 u_int i;
1602 for (i = 0; i < *size; i++) {
1603 if (strcmp((*list)[i], s) == 0)
1604 return;
1606 *list = xreallocarray(*list, (*size) + 1, sizeof **list);
1607 (*list)[(*size)++] = xstrdup(s);
1610 /* Build completion list. */
1611 static char **
1612 status_prompt_complete_list(u_int *size, const char *s, int at_start)
1614 char **list = NULL, *tmp;
1615 const char **layout, *value, *cp;
1616 const struct cmd_entry **cmdent;
1617 const struct options_table_entry *oe;
1618 size_t slen = strlen(s), valuelen;
1619 struct options_entry *o;
1620 struct options_array_item *a;
1621 const char *layouts[] = {
1622 "even-horizontal", "even-vertical", "main-horizontal",
1623 "main-vertical", "tiled", NULL
1626 *size = 0;
1627 for (cmdent = cmd_table; *cmdent != NULL; cmdent++) {
1628 if (strncmp((*cmdent)->name, s, slen) == 0)
1629 status_prompt_add_list(&list, size, (*cmdent)->name);
1630 if ((*cmdent)->alias != NULL &&
1631 strncmp((*cmdent)->alias, s, slen) == 0)
1632 status_prompt_add_list(&list, size, (*cmdent)->alias);
1634 o = options_get_only(global_options, "command-alias");
1635 if (o != NULL) {
1636 a = options_array_first(o);
1637 while (a != NULL) {
1638 value = options_array_item_value(a)->string;
1639 if ((cp = strchr(value, '=')) == NULL)
1640 goto next;
1641 valuelen = cp - value;
1642 if (slen > valuelen || strncmp(value, s, slen) != 0)
1643 goto next;
1645 xasprintf(&tmp, "%.*s", (int)valuelen, value);
1646 status_prompt_add_list(&list, size, tmp);
1647 free(tmp);
1649 next:
1650 a = options_array_next(a);
1653 if (at_start)
1654 return (list);
1655 for (oe = options_table; oe->name != NULL; oe++) {
1656 if (strncmp(oe->name, s, slen) == 0)
1657 status_prompt_add_list(&list, size, oe->name);
1659 for (layout = layouts; *layout != NULL; layout++) {
1660 if (strncmp(*layout, s, slen) == 0)
1661 status_prompt_add_list(&list, size, *layout);
1663 return (list);
1666 /* Find longest prefix. */
1667 static char *
1668 status_prompt_complete_prefix(char **list, u_int size)
1670 char *out;
1671 u_int i;
1672 size_t j;
1674 if (list == NULL || size == 0)
1675 return (NULL);
1676 out = xstrdup(list[0]);
1677 for (i = 1; i < size; i++) {
1678 j = strlen(list[i]);
1679 if (j > strlen(out))
1680 j = strlen(out);
1681 for (; j > 0; j--) {
1682 if (out[j - 1] != list[i][j - 1])
1683 out[j - 1] = '\0';
1686 return (out);
1689 /* Complete word menu callback. */
1690 static void
1691 status_prompt_menu_callback(__unused struct menu *menu, u_int idx, key_code key,
1692 void *data)
1694 struct status_prompt_menu *spm = data;
1695 struct client *c = spm->c;
1696 u_int i;
1697 char *s;
1699 if (key != KEYC_NONE) {
1700 idx += spm->start;
1701 if (spm->flag == '\0')
1702 s = xstrdup(spm->list[idx]);
1703 else
1704 xasprintf(&s, "-%c%s", spm->flag, spm->list[idx]);
1705 if (c->prompt_type == PROMPT_TYPE_WINDOW_TARGET) {
1706 free(c->prompt_buffer);
1707 c->prompt_buffer = utf8_fromcstr(s);
1708 c->prompt_index = utf8_strlen(c->prompt_buffer);
1709 c->flags |= CLIENT_REDRAWSTATUS;
1710 } else if (status_prompt_replace_complete(c, s))
1711 c->flags |= CLIENT_REDRAWSTATUS;
1712 free(s);
1715 for (i = 0; i < spm->size; i++)
1716 free(spm->list[i]);
1717 free(spm->list);
1720 /* Show complete word menu. */
1721 static int
1722 status_prompt_complete_list_menu(struct client *c, char **list, u_int size,
1723 u_int offset, char flag)
1725 struct menu *menu;
1726 struct menu_item item;
1727 struct status_prompt_menu *spm;
1728 u_int lines = status_line_size(c), height, i;
1729 u_int py;
1731 if (size <= 1)
1732 return (0);
1733 if (c->tty.sy - lines < 3)
1734 return (0);
1736 spm = xmalloc(sizeof *spm);
1737 spm->c = c;
1738 spm->size = size;
1739 spm->list = list;
1740 spm->flag = flag;
1742 height = c->tty.sy - lines - 2;
1743 if (height > 10)
1744 height = 10;
1745 if (height > size)
1746 height = size;
1747 spm->start = size - height;
1749 menu = menu_create("");
1750 for (i = spm->start; i < size; i++) {
1751 item.name = list[i];
1752 item.key = '0' + (i - spm->start);
1753 item.command = NULL;
1754 menu_add_item(menu, &item, NULL, c, NULL);
1757 if (options_get_number(c->session->options, "status-position") == 0)
1758 py = lines;
1759 else
1760 py = c->tty.sy - 3 - height;
1761 offset += utf8_cstrwidth(c->prompt_string);
1762 if (offset > 2)
1763 offset -= 2;
1764 else
1765 offset = 0;
1767 if (menu_display(menu, MENU_NOMOUSE|MENU_TAB, 0, NULL, offset, py, c,
1768 BOX_LINES_DEFAULT, NULL, NULL, NULL, NULL,
1769 status_prompt_menu_callback, spm) != 0) {
1770 menu_free(menu);
1771 free(spm);
1772 return (0);
1774 return (1);
1777 /* Show complete word menu. */
1778 static char *
1779 status_prompt_complete_window_menu(struct client *c, struct session *s,
1780 const char *word, u_int offset, char flag)
1782 struct menu *menu;
1783 struct menu_item item;
1784 struct status_prompt_menu *spm;
1785 struct winlink *wl;
1786 char **list = NULL, *tmp;
1787 u_int lines = status_line_size(c), height;
1788 u_int py, size = 0;
1790 if (c->tty.sy - lines < 3)
1791 return (NULL);
1793 spm = xmalloc(sizeof *spm);
1794 spm->c = c;
1795 spm->flag = flag;
1797 height = c->tty.sy - lines - 2;
1798 if (height > 10)
1799 height = 10;
1800 spm->start = 0;
1802 menu = menu_create("");
1803 RB_FOREACH(wl, winlinks, &s->windows) {
1804 if (word != NULL && *word != '\0') {
1805 xasprintf(&tmp, "%d", wl->idx);
1806 if (strncmp(tmp, word, strlen(word)) != 0) {
1807 free(tmp);
1808 continue;
1810 free(tmp);
1813 list = xreallocarray(list, size + 1, sizeof *list);
1814 if (c->prompt_type == PROMPT_TYPE_WINDOW_TARGET) {
1815 xasprintf(&tmp, "%d (%s)", wl->idx, wl->window->name);
1816 xasprintf(&list[size++], "%d", wl->idx);
1817 } else {
1818 xasprintf(&tmp, "%s:%d (%s)", s->name, wl->idx,
1819 wl->window->name);
1820 xasprintf(&list[size++], "%s:%d", s->name, wl->idx);
1822 item.name = tmp;
1823 item.key = '0' + size - 1;
1824 item.command = NULL;
1825 menu_add_item(menu, &item, NULL, c, NULL);
1826 free(tmp);
1828 if (size == height)
1829 break;
1831 if (size == 0) {
1832 menu_free(menu);
1833 return (NULL);
1835 if (size == 1) {
1836 menu_free(menu);
1837 if (flag != '\0') {
1838 xasprintf(&tmp, "-%c%s", flag, list[0]);
1839 free(list[0]);
1840 } else
1841 tmp = list[0];
1842 free(list);
1843 return (tmp);
1845 if (height > size)
1846 height = size;
1848 spm->size = size;
1849 spm->list = list;
1851 if (options_get_number(c->session->options, "status-position") == 0)
1852 py = lines;
1853 else
1854 py = c->tty.sy - 3 - height;
1855 offset += utf8_cstrwidth(c->prompt_string);
1856 if (offset > 2)
1857 offset -= 2;
1858 else
1859 offset = 0;
1861 if (menu_display(menu, MENU_NOMOUSE|MENU_TAB, 0, NULL, offset, py, c,
1862 BOX_LINES_DEFAULT, NULL, NULL, NULL, NULL,
1863 status_prompt_menu_callback, spm) != 0) {
1864 menu_free(menu);
1865 free(spm);
1866 return (NULL);
1868 return (NULL);
1871 /* Sort complete list. */
1872 static int
1873 status_prompt_complete_sort(const void *a, const void *b)
1875 const char **aa = (const char **)a, **bb = (const char **)b;
1877 return (strcmp(*aa, *bb));
1880 /* Complete a session. */
1881 static char *
1882 status_prompt_complete_session(char ***list, u_int *size, const char *s,
1883 char flag)
1885 struct session *loop;
1886 char *out, *tmp, n[11];
1888 RB_FOREACH(loop, sessions, &sessions) {
1889 if (*s == '\0' || strncmp(loop->name, s, strlen(s)) == 0) {
1890 *list = xreallocarray(*list, (*size) + 2,
1891 sizeof **list);
1892 xasprintf(&(*list)[(*size)++], "%s:", loop->name);
1893 } else if (*s == '$') {
1894 xsnprintf(n, sizeof n, "%u", loop->id);
1895 if (s[1] == '\0' ||
1896 strncmp(n, s + 1, strlen(s) - 1) == 0) {
1897 *list = xreallocarray(*list, (*size) + 2,
1898 sizeof **list);
1899 xasprintf(&(*list)[(*size)++], "$%s:", n);
1903 out = status_prompt_complete_prefix(*list, *size);
1904 if (out != NULL && flag != '\0') {
1905 xasprintf(&tmp, "-%c%s", flag, out);
1906 free(out);
1907 out = tmp;
1909 return (out);
1912 /* Complete word. */
1913 static char *
1914 status_prompt_complete(struct client *c, const char *word, u_int offset)
1916 struct session *session;
1917 const char *s, *colon;
1918 char **list = NULL, *copy = NULL, *out = NULL;
1919 char flag = '\0';
1920 u_int size = 0, i;
1922 if (*word == '\0' &&
1923 c->prompt_type != PROMPT_TYPE_TARGET &&
1924 c->prompt_type != PROMPT_TYPE_WINDOW_TARGET)
1925 return (NULL);
1927 if (c->prompt_type != PROMPT_TYPE_TARGET &&
1928 c->prompt_type != PROMPT_TYPE_WINDOW_TARGET &&
1929 strncmp(word, "-t", 2) != 0 &&
1930 strncmp(word, "-s", 2) != 0) {
1931 list = status_prompt_complete_list(&size, word, offset == 0);
1932 if (size == 0)
1933 out = NULL;
1934 else if (size == 1)
1935 xasprintf(&out, "%s ", list[0]);
1936 else
1937 out = status_prompt_complete_prefix(list, size);
1938 goto found;
1941 if (c->prompt_type == PROMPT_TYPE_TARGET ||
1942 c->prompt_type == PROMPT_TYPE_WINDOW_TARGET) {
1943 s = word;
1944 flag = '\0';
1945 } else {
1946 s = word + 2;
1947 flag = word[1];
1948 offset += 2;
1951 /* If this is a window completion, open the window menu. */
1952 if (c->prompt_type == PROMPT_TYPE_WINDOW_TARGET) {
1953 out = status_prompt_complete_window_menu(c, c->session, s,
1954 offset, '\0');
1955 goto found;
1957 colon = strchr(s, ':');
1959 /* If there is no colon, complete as a session. */
1960 if (colon == NULL) {
1961 out = status_prompt_complete_session(&list, &size, s, flag);
1962 goto found;
1965 /* If there is a colon but no period, find session and show a menu. */
1966 if (strchr(colon + 1, '.') == NULL) {
1967 if (*s == ':')
1968 session = c->session;
1969 else {
1970 copy = xstrdup(s);
1971 *strchr(copy, ':') = '\0';
1972 session = session_find(copy);
1973 free(copy);
1974 if (session == NULL)
1975 goto found;
1977 out = status_prompt_complete_window_menu(c, session, colon + 1,
1978 offset, flag);
1979 if (out == NULL)
1980 return (NULL);
1983 found:
1984 if (size != 0) {
1985 qsort(list, size, sizeof *list, status_prompt_complete_sort);
1986 for (i = 0; i < size; i++)
1987 log_debug("complete %u: %s", i, list[i]);
1990 if (out != NULL && strcmp(word, out) == 0) {
1991 free(out);
1992 out = NULL;
1994 if (out != NULL ||
1995 !status_prompt_complete_list_menu(c, list, size, offset, flag)) {
1996 for (i = 0; i < size; i++)
1997 free(list[i]);
1998 free(list);
2000 return (out);
2003 /* Return the type of the prompt as an enum. */
2004 enum prompt_type
2005 status_prompt_type(const char *type)
2007 u_int i;
2009 for (i = 0; i < PROMPT_NTYPES; i++) {
2010 if (strcmp(type, status_prompt_type_string(i)) == 0)
2011 return (i);
2013 return (PROMPT_TYPE_INVALID);
2016 /* Accessor for prompt_type_strings. */
2017 const char *
2018 status_prompt_type_string(u_int type)
2020 if (type >= PROMPT_NTYPES)
2021 return ("invalid");
2022 return (prompt_type_strings[type]);