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