status.c revision 1.156
1/* $OpenBSD: status.c,v 1.156 2016/12/07 23:03:04 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 char	*status_redraw_get_left(struct client *, time_t,
33		     struct grid_cell *, size_t *);
34static char	*status_redraw_get_right(struct client *, time_t,
35		     struct grid_cell *, size_t *);
36static char	*status_print(struct client *, struct winlink *, time_t,
37		     struct grid_cell *);
38static char	*status_replace(struct client *, struct winlink *, const char *,
39		     time_t);
40static void	 status_message_callback(int, short, void *);
41static void	 status_timer_callback(int, short, void *);
42
43static char	*status_prompt_find_history_file(void);
44static const char *status_prompt_up_history(u_int *);
45static const char *status_prompt_down_history(u_int *);
46static void	 status_prompt_add_history(const char *);
47
48static const char **status_prompt_complete_list(u_int *, const char *);
49static char	*status_prompt_complete_prefix(const char **, u_int);
50static char	*status_prompt_complete(struct session *, const char *);
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_STATUS;
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/* Get screen line of status line. -1 means off. */
196int
197status_at_line(struct client *c)
198{
199	struct session	*s = c->session;
200
201	if (!options_get_number(s->options, "status"))
202		return (-1);
203
204	if (options_get_number(s->options, "status-position") == 0)
205		return (0);
206	return (c->tty.sy - 1);
207}
208
209/* Retrieve options for left string. */
210static char *
211status_redraw_get_left(struct client *c, time_t t, struct grid_cell *gc,
212    size_t *size)
213{
214	struct session	*s = c->session;
215	const char	*template;
216	char		*left;
217	size_t		 leftlen;
218
219	style_apply_update(gc, s->options, "status-left-style");
220
221	template = options_get_string(s->options, "status-left");
222	left = status_replace(c, NULL, template, t);
223
224	*size = options_get_number(s->options, "status-left-length");
225	leftlen = screen_write_cstrlen("%s", left);
226	if (leftlen < *size)
227		*size = leftlen;
228	return (left);
229}
230
231/* Retrieve options for right string. */
232static char *
233status_redraw_get_right(struct client *c, time_t t, struct grid_cell *gc,
234    size_t *size)
235{
236	struct session	*s = c->session;
237	const char	*template;
238	char		*right;
239	size_t		 rightlen;
240
241	style_apply_update(gc, s->options, "status-right-style");
242
243	template = options_get_string(s->options, "status-right");
244	right = status_replace(c, NULL, template, t);
245
246	*size = options_get_number(s->options, "status-right-length");
247	rightlen = screen_write_cstrlen("%s", right);
248	if (rightlen < *size)
249		*size = rightlen;
250	return (right);
251}
252
253/* Get window at window list position. */
254struct window *
255status_get_window_at(struct client *c, u_int x)
256{
257	struct session	*s = c->session;
258	struct winlink	*wl;
259	struct options	*oo;
260	const char	*sep;
261	size_t		 seplen;
262
263	x += c->wlmouse;
264	RB_FOREACH(wl, winlinks, &s->windows) {
265		oo = wl->window->options;
266
267		sep = options_get_string(oo, "window-status-separator");
268		seplen = screen_write_cstrlen("%s", sep);
269
270		if (x < wl->status_width)
271			return (wl->window);
272		x -= wl->status_width + seplen;
273	}
274	return (NULL);
275}
276
277/* Draw status for client on the last lines of given context. */
278int
279status_redraw(struct client *c)
280{
281	struct screen_write_ctx	ctx;
282	struct session	       *s = c->session;
283	struct winlink	       *wl;
284	struct screen		old_status, window_list;
285	struct grid_cell	stdgc, lgc, rgc, gc;
286	struct options	       *oo;
287	time_t			t;
288	char		       *left, *right, *sep;
289	u_int			offset, needed;
290	u_int			wlstart, wlwidth, wlavailable, wloffset, wlsize;
291	size_t			llen, rlen, seplen;
292	int			larrow, rarrow;
293
294	/* No status line? */
295	if (c->tty.sy == 0 || !options_get_number(s->options, "status"))
296		return (1);
297	left = right = NULL;
298	larrow = rarrow = 0;
299
300	/* Store current time. */
301	t = time(NULL);
302
303	/* Set up default colour. */
304	style_apply(&stdgc, s->options, "status-style");
305
306	/* Create the target screen. */
307	memcpy(&old_status, &c->status, sizeof old_status);
308	screen_init(&c->status, c->tty.sx, 1, 0);
309	screen_write_start(&ctx, NULL, &c->status);
310	for (offset = 0; offset < c->tty.sx; offset++)
311		screen_write_putc(&ctx, &stdgc, ' ');
312	screen_write_stop(&ctx);
313
314	/* If the height is one line, blank status line. */
315	if (c->tty.sy <= 1)
316		goto out;
317
318	/* Work out left and right strings. */
319	memcpy(&lgc, &stdgc, sizeof lgc);
320	left = status_redraw_get_left(c, t, &lgc, &llen);
321	memcpy(&rgc, &stdgc, sizeof rgc);
322	right = status_redraw_get_right(c, t, &rgc, &rlen);
323
324	/*
325	 * Figure out how much space we have for the window list. If there
326	 * isn't enough space, just show a blank status line.
327	 */
328	needed = 0;
329	if (llen != 0)
330		needed += llen;
331	if (rlen != 0)
332		needed += rlen;
333	if (c->tty.sx == 0 || c->tty.sx <= needed)
334		goto out;
335	wlavailable = c->tty.sx - needed;
336
337	/* Calculate the total size needed for the window list. */
338	wlstart = wloffset = wlwidth = 0;
339	RB_FOREACH(wl, winlinks, &s->windows) {
340		free(wl->status_text);
341		memcpy(&wl->status_cell, &stdgc, sizeof wl->status_cell);
342		wl->status_text = status_print(c, wl, t, &wl->status_cell);
343		wl->status_width = screen_write_cstrlen("%s", wl->status_text);
344
345		if (wl == s->curw)
346			wloffset = wlwidth;
347
348		oo = wl->window->options;
349		sep = options_get_string(oo, "window-status-separator");
350		seplen = screen_write_cstrlen("%s", sep);
351		wlwidth += wl->status_width + seplen;
352	}
353
354	/* Create a new screen for the window list. */
355	screen_init(&window_list, wlwidth, 1, 0);
356
357	/* And draw the window list into it. */
358	screen_write_start(&ctx, NULL, &window_list);
359	RB_FOREACH(wl, winlinks, &s->windows) {
360		screen_write_cnputs(&ctx, -1, &wl->status_cell, "%s",
361		    wl->status_text);
362
363		oo = wl->window->options;
364		sep = options_get_string(oo, "window-status-separator");
365		screen_write_cnputs(&ctx, -1, &stdgc, "%s", sep);
366	}
367	screen_write_stop(&ctx);
368
369	/* If there is enough space for the total width, skip to draw now. */
370	if (wlwidth <= wlavailable)
371		goto draw;
372
373	/* Find size of current window text. */
374	wlsize = s->curw->status_width;
375
376	/*
377	 * If the current window is already on screen, good to draw from the
378	 * start and just leave off the end.
379	 */
380	if (wloffset + wlsize < wlavailable) {
381		if (wlavailable > 0) {
382			rarrow = 1;
383			wlavailable--;
384		}
385		wlwidth = wlavailable;
386	} else {
387		/*
388		 * Work out how many characters we need to omit from the
389		 * start. There are wlavailable characters to fill, and
390		 * wloffset + wlsize must be the last. So, the start character
391		 * is wloffset + wlsize - wlavailable.
392		 */
393		if (wlavailable > 0) {
394			larrow = 1;
395			wlavailable--;
396		}
397
398		wlstart = wloffset + wlsize - wlavailable;
399		if (wlavailable > 0 && wlwidth > wlstart + wlavailable + 1) {
400			rarrow = 1;
401			wlstart++;
402			wlavailable--;
403		}
404		wlwidth = wlavailable;
405	}
406
407	/* Bail if anything is now too small too. */
408	if (wlwidth == 0 || wlavailable == 0) {
409		screen_free(&window_list);
410		goto out;
411	}
412
413	/*
414	 * Now the start position is known, work out the state of the left and
415	 * right arrows.
416	 */
417	offset = 0;
418	RB_FOREACH(wl, winlinks, &s->windows) {
419		if (wl->flags & WINLINK_ALERTFLAGS &&
420		    larrow == 1 && offset < wlstart)
421			larrow = -1;
422
423		offset += wl->status_width;
424
425		if (wl->flags & WINLINK_ALERTFLAGS &&
426		    rarrow == 1 && offset > wlstart + wlwidth)
427			rarrow = -1;
428	}
429
430draw:
431	/* Begin drawing. */
432	screen_write_start(&ctx, NULL, &c->status);
433
434	/* Draw the left string and arrow. */
435	screen_write_cursormove(&ctx, 0, 0);
436	if (llen != 0)
437		screen_write_cnputs(&ctx, llen, &lgc, "%s", left);
438	if (larrow != 0) {
439		memcpy(&gc, &stdgc, sizeof gc);
440		if (larrow == -1)
441			gc.attr ^= GRID_ATTR_REVERSE;
442		screen_write_putc(&ctx, &gc, '<');
443	}
444
445	/* Draw the right string and arrow. */
446	if (rarrow != 0) {
447		screen_write_cursormove(&ctx, c->tty.sx - rlen - 1, 0);
448		memcpy(&gc, &stdgc, sizeof gc);
449		if (rarrow == -1)
450			gc.attr ^= GRID_ATTR_REVERSE;
451		screen_write_putc(&ctx, &gc, '>');
452	} else
453		screen_write_cursormove(&ctx, c->tty.sx - rlen, 0);
454	if (rlen != 0)
455		screen_write_cnputs(&ctx, rlen, &rgc, "%s", right);
456
457	/* Figure out the offset for the window list. */
458	if (llen != 0)
459		wloffset = llen;
460	else
461		wloffset = 0;
462	if (wlwidth < wlavailable) {
463		switch (options_get_number(s->options, "status-justify")) {
464		case 1:	/* centred */
465			wloffset += (wlavailable - wlwidth) / 2;
466			break;
467		case 2:	/* right */
468			wloffset += (wlavailable - wlwidth);
469			break;
470		}
471	}
472	if (larrow != 0)
473		wloffset++;
474
475	/* Copy the window list. */
476	c->wlmouse = -wloffset + wlstart;
477	screen_write_cursormove(&ctx, wloffset, 0);
478	screen_write_copy(&ctx, &window_list, wlstart, 0, wlwidth, 1);
479	screen_free(&window_list);
480
481	screen_write_stop(&ctx);
482
483out:
484	free(left);
485	free(right);
486
487	if (grid_compare(c->status.grid, old_status.grid) == 0) {
488		screen_free(&old_status);
489		return (0);
490	}
491	screen_free(&old_status);
492	return (1);
493}
494
495/* Replace special sequences in fmt. */
496static char *
497status_replace(struct client *c, struct winlink *wl, const char *fmt, time_t t)
498{
499	struct format_tree	*ft;
500	char			*expanded;
501
502	if (fmt == NULL)
503		return (xstrdup(""));
504
505	if (c->flags & CLIENT_STATUSFORCE)
506		ft = format_create(NULL, FORMAT_STATUS|FORMAT_FORCE);
507	else
508		ft = format_create(NULL, FORMAT_STATUS);
509	format_defaults(ft, c, NULL, wl, NULL);
510
511	expanded = format_expand_time(ft, fmt, t);
512
513	format_free(ft);
514	return (expanded);
515}
516
517/* Return winlink status line entry and adjust gc as necessary. */
518static char *
519status_print(struct client *c, struct winlink *wl, time_t t,
520    struct grid_cell *gc)
521{
522	struct options	*oo = wl->window->options;
523	struct session	*s = c->session;
524	const char	*fmt;
525	char   		*text;
526
527	style_apply_update(gc, oo, "window-status-style");
528	fmt = options_get_string(oo, "window-status-format");
529	if (wl == s->curw) {
530		style_apply_update(gc, oo, "window-status-current-style");
531		fmt = options_get_string(oo, "window-status-current-format");
532	}
533	if (wl == TAILQ_FIRST(&s->lastw))
534		style_apply_update(gc, oo, "window-status-last-style");
535
536	if (wl->flags & WINLINK_BELL)
537		style_apply_update(gc, oo, "window-status-bell-style");
538	else if (wl->flags & (WINLINK_ACTIVITY|WINLINK_SILENCE))
539		style_apply_update(gc, oo, "window-status-activity-style");
540
541	text = status_replace(c, wl, fmt, t);
542	return (text);
543}
544
545/* Set a status line message. */
546void
547status_message_set(struct client *c, const char *fmt, ...)
548{
549	struct timeval		 tv;
550	struct message_entry	*msg, *msg1;
551	va_list			 ap;
552	int			 delay;
553	u_int			 limit;
554
555	limit = options_get_number(global_options, "message-limit");
556
557	status_message_clear(c);
558
559	va_start(ap, fmt);
560	xvasprintf(&c->message_string, fmt, ap);
561	va_end(ap);
562
563	msg = xcalloc(1, sizeof *msg);
564	msg->msg_time = time(NULL);
565	msg->msg_num = c->message_next++;
566	msg->msg = xstrdup(c->message_string);
567	TAILQ_INSERT_TAIL(&c->message_log, msg, entry);
568
569	TAILQ_FOREACH_SAFE(msg, &c->message_log, entry, msg1) {
570		if (msg->msg_num + limit >= c->message_next)
571			break;
572		free(msg->msg);
573		TAILQ_REMOVE(&c->message_log, msg, entry);
574		free(msg);
575	}
576
577	delay = options_get_number(c->session->options, "display-time");
578	if (delay > 0) {
579		tv.tv_sec = delay / 1000;
580		tv.tv_usec = (delay % 1000) * 1000L;
581
582		if (event_initialized(&c->message_timer))
583			evtimer_del(&c->message_timer);
584		evtimer_set(&c->message_timer, status_message_callback, c);
585		evtimer_add(&c->message_timer, &tv);
586	}
587
588	c->tty.flags |= (TTY_NOCURSOR|TTY_FREEZE);
589	c->flags |= CLIENT_STATUS;
590}
591
592/* Clear status line message. */
593void
594status_message_clear(struct client *c)
595{
596	if (c->message_string == NULL)
597		return;
598
599	free(c->message_string);
600	c->message_string = NULL;
601
602	if (c->prompt_string == NULL)
603		c->tty.flags &= ~(TTY_NOCURSOR|TTY_FREEZE);
604	c->flags |= CLIENT_REDRAW; /* screen was frozen and may have changed */
605
606	screen_reinit(&c->status);
607}
608
609/* Clear status line message after timer expires. */
610static void
611status_message_callback(__unused int fd, __unused short event, void *data)
612{
613	struct client	*c = data;
614
615	status_message_clear(c);
616}
617
618/* Draw client message on status line of present else on last line. */
619int
620status_message_redraw(struct client *c)
621{
622	struct screen_write_ctx		ctx;
623	struct session		       *s = c->session;
624	struct screen		        old_status;
625	size_t			        len;
626	struct grid_cell		gc;
627
628	if (c->tty.sx == 0 || c->tty.sy == 0)
629		return (0);
630	memcpy(&old_status, &c->status, sizeof old_status);
631	screen_init(&c->status, c->tty.sx, 1, 0);
632
633	len = screen_write_strlen("%s", c->message_string);
634	if (len > c->tty.sx)
635		len = c->tty.sx;
636
637	style_apply(&gc, s->options, "message-style");
638
639	screen_write_start(&ctx, NULL, &c->status);
640
641	screen_write_cursormove(&ctx, 0, 0);
642	screen_write_nputs(&ctx, len, &gc, "%s", c->message_string);
643	for (; len < c->tty.sx; len++)
644		screen_write_putc(&ctx, &gc, ' ');
645
646	screen_write_stop(&ctx);
647
648	if (grid_compare(c->status.grid, old_status.grid) == 0) {
649		screen_free(&old_status);
650		return (0);
651	}
652	screen_free(&old_status);
653	return (1);
654}
655
656/* Enable status line prompt. */
657void
658status_prompt_set(struct client *c, const char *msg, const char *input,
659    int (*callbackfn)(void *, const char *), void (*freefn)(void *),
660    void *data, int flags)
661{
662	struct format_tree	*ft;
663	time_t			 t;
664	char			*tmp;
665
666	ft = format_create(NULL, 0);
667	format_defaults(ft, c, NULL, NULL, NULL);
668
669	t = time(NULL);
670	tmp = format_expand_time(ft, input, t);
671
672	status_message_clear(c);
673	status_prompt_clear(c);
674
675	c->prompt_string = format_expand_time(ft, msg, t);
676
677	c->prompt_buffer = utf8_fromcstr(tmp);
678	c->prompt_index = utf8_strlen(c->prompt_buffer);
679
680	c->prompt_callbackfn = callbackfn;
681	c->prompt_freefn = freefn;
682	c->prompt_data = data;
683
684	c->prompt_hindex = 0;
685
686	c->prompt_flags = flags;
687	c->prompt_mode = PROMPT_ENTRY;
688
689	c->tty.flags |= (TTY_NOCURSOR|TTY_FREEZE);
690	c->flags |= CLIENT_STATUS;
691
692	free(tmp);
693	format_free(ft);
694}
695
696/* Remove status line prompt. */
697void
698status_prompt_clear(struct client *c)
699{
700	if (c->prompt_string == NULL)
701		return;
702
703	if (c->prompt_freefn != NULL && c->prompt_data != NULL)
704		c->prompt_freefn(c->prompt_data);
705
706	free(c->prompt_string);
707	c->prompt_string = NULL;
708
709	free(c->prompt_buffer);
710	c->prompt_buffer = NULL;
711
712	c->tty.flags &= ~(TTY_NOCURSOR|TTY_FREEZE);
713	c->flags |= CLIENT_REDRAW; /* screen was frozen and may have changed */
714
715	screen_reinit(&c->status);
716}
717
718/* Update status line prompt with a new prompt string. */
719void
720status_prompt_update(struct client *c, const char *msg, const char *input)
721{
722	struct format_tree	*ft;
723	time_t			 t;
724	char			*tmp;
725
726	ft = format_create(NULL, 0);
727	format_defaults(ft, c, NULL, NULL, NULL);
728
729	t = time(NULL);
730	tmp = format_expand_time(ft, input, t);
731
732	free(c->prompt_string);
733	c->prompt_string = format_expand_time(ft, msg, t);
734
735	free(c->prompt_buffer);
736	c->prompt_buffer = utf8_fromcstr(tmp);
737	c->prompt_index = utf8_strlen(c->prompt_buffer);
738
739	c->prompt_hindex = 0;
740
741	c->flags |= CLIENT_STATUS;
742
743	free(tmp);
744	format_free(ft);
745}
746
747/* Draw client prompt on status line of present else on last line. */
748int
749status_prompt_redraw(struct client *c)
750{
751	struct screen_write_ctx	 ctx;
752	struct session		*s = c->session;
753	struct screen		 old_status;
754	u_int			 i, offset, left, start, pcursor, pwidth, width;
755	struct grid_cell	 gc, cursorgc;
756
757	if (c->tty.sx == 0 || c->tty.sy == 0)
758		return (0);
759	memcpy(&old_status, &c->status, sizeof old_status);
760	screen_init(&c->status, c->tty.sx, 1, 0);
761
762	if (c->prompt_mode == PROMPT_COMMAND)
763		style_apply(&gc, s->options, "message-command-style");
764	else
765		style_apply(&gc, s->options, "message-style");
766
767	memcpy(&cursorgc, &gc, sizeof cursorgc);
768	cursorgc.attr ^= GRID_ATTR_REVERSE;
769
770	start = screen_write_strlen("%s", c->prompt_string);
771	if (start > c->tty.sx)
772		start = c->tty.sx;
773
774	screen_write_start(&ctx, NULL, &c->status);
775	screen_write_cursormove(&ctx, 0, 0);
776	screen_write_nputs(&ctx, start, &gc, "%s", c->prompt_string);
777	while (c->status.cx < screen_size_x(&c->status))
778		screen_write_putc(&ctx, &gc, ' ');
779	screen_write_cursormove(&ctx, start, 0);
780
781	left = c->tty.sx - start;
782	if (left == 0)
783		goto finished;
784
785	pcursor = utf8_strwidth(c->prompt_buffer, c->prompt_index);
786	pwidth = utf8_strwidth(c->prompt_buffer, -1);
787	if (pcursor >= left) {
788		/*
789		 * The cursor would be outside the screen so start drawing
790		 * with it on the right.
791		 */
792		offset = (pcursor - left) + 1;
793		pwidth = left;
794	} else
795		offset = 0;
796	if (pwidth > left)
797		pwidth = left;
798
799	width = 0;
800	for (i = 0; c->prompt_buffer[i].size != 0; i++) {
801		if (width < offset) {
802			width += c->prompt_buffer[i].width;
803			continue;
804		}
805		if (width >= offset + pwidth)
806			break;
807		width += c->prompt_buffer[i].width;
808		if (width > offset + pwidth)
809			break;
810
811		if (i != c->prompt_index) {
812			utf8_copy(&gc.data, &c->prompt_buffer[i]);
813			screen_write_cell(&ctx, &gc);
814		} else {
815			utf8_copy(&cursorgc.data, &c->prompt_buffer[i]);
816			screen_write_cell(&ctx, &cursorgc);
817		}
818	}
819	if (c->status.cx < screen_size_x(&c->status) && c->prompt_index >= i)
820		screen_write_putc(&ctx, &cursorgc, ' ');
821
822finished:
823	screen_write_stop(&ctx);
824
825	if (grid_compare(c->status.grid, old_status.grid) == 0) {
826		screen_free(&old_status);
827		return (0);
828	}
829	screen_free(&old_status);
830	return (1);
831}
832
833/* Is this a separator? */
834static int
835status_prompt_in_list(const char *ws, const struct utf8_data *ud)
836{
837	if (ud->size != 1 || ud->width != 1)
838		return (0);
839	return (strchr(ws, *ud->data) != NULL);
840}
841
842/* Is this a space? */
843static int
844status_prompt_space(const struct utf8_data *ud)
845{
846	if (ud->size != 1 || ud->width != 1)
847		return (0);
848	return (*ud->data == ' ');
849}
850
851/*
852 * Translate key from emacs to vi. Return 0 to drop key, 1 to process the key
853 * as an emacs key; return 2 to append to the buffer.
854 */
855static int
856status_prompt_translate_key(struct client *c, key_code key, key_code *new_key)
857{
858	if (c->prompt_mode == PROMPT_ENTRY) {
859		switch (key) {
860		case '\003': /* C-c */
861		case '\010': /* C-h */
862		case '\011': /* Tab */
863		case '\025': /* C-u */
864		case '\027': /* C-w */
865		case '\n':
866		case '\r':
867		case KEYC_BSPACE:
868		case KEYC_DC:
869		case KEYC_DOWN:
870		case KEYC_END:
871		case KEYC_HOME:
872		case KEYC_LEFT:
873		case KEYC_RIGHT:
874		case KEYC_UP:
875			*new_key = key;
876			return (1);
877		case '\033': /* Escape */
878			c->prompt_mode = PROMPT_COMMAND;
879			c->flags |= CLIENT_STATUS;
880			return (0);
881		}
882		*new_key = key;
883		return (2);
884	}
885
886	switch (key) {
887	case 'A':
888	case 'I':
889	case 'C':
890	case 's':
891	case 'a':
892		c->prompt_mode = PROMPT_ENTRY;
893		c->flags |= CLIENT_STATUS;
894		break; /* switch mode and... */
895	case 'S':
896		c->prompt_mode = PROMPT_ENTRY;
897		c->flags |= CLIENT_STATUS;
898		*new_key = '\025'; /* C-u */
899		return (1);
900	case 'i':
901	case '\033': /* Escape */
902		c->prompt_mode = PROMPT_ENTRY;
903		c->flags |= CLIENT_STATUS;
904		return (0);
905	}
906
907	switch (key) {
908	case 'A':
909	case '$':
910		*new_key = KEYC_END;
911		return (1);
912	case 'I':
913	case '0':
914	case '^':
915		*new_key = KEYC_HOME;
916		return (1);
917	case 'C':
918	case 'D':
919		*new_key = '\013'; /* C-k */
920		return (1);
921	case KEYC_BSPACE:
922	case 'X':
923		*new_key = KEYC_BSPACE;
924		return (1);
925	case 'b':
926	case 'B':
927		*new_key = 'b'|KEYC_ESCAPE;
928		return (1);
929	case 'd':
930		*new_key = '\025';
931		return (1);
932	case 'e':
933	case 'E':
934	case 'w':
935	case 'W':
936		*new_key = 'f'|KEYC_ESCAPE;
937		return (1);
938	case 'p':
939		*new_key = '\031'; /* C-y */
940		return (1);
941	case 's':
942	case KEYC_DC:
943	case 'x':
944		*new_key = KEYC_DC;
945		return (1);
946	case KEYC_DOWN:
947	case 'j':
948		*new_key = KEYC_DOWN;
949		return (1);
950	case KEYC_LEFT:
951	case 'h':
952		*new_key = KEYC_LEFT;
953		return (1);
954	case 'a':
955	case KEYC_RIGHT:
956	case 'l':
957		*new_key = KEYC_RIGHT;
958		return (1);
959	case KEYC_UP:
960	case 'k':
961		*new_key = KEYC_UP;
962		return (1);
963	case '\010' /* C-h */:
964	case '\003' /* C-c */:
965	case '\n':
966	case '\r':
967		return (1);
968	}
969	return (0);
970}
971
972/* Handle keys in prompt. */
973int
974status_prompt_key(struct client *c, key_code key)
975{
976	struct options		*oo = c->session->options;
977	struct paste_buffer	*pb;
978	char			*s, word[64];
979	const char		*histstr, *bufdata, *ws = NULL;
980	u_char			 ch;
981	size_t			 size, n, off, idx, bufsize, used;
982	struct utf8_data	 tmp, *first, *last, *ud;
983	int			 keys;
984
985	size = utf8_strlen(c->prompt_buffer);
986
987	if (c->prompt_flags & PROMPT_NUMERIC) {
988		if (key >= '0' && key <= '9')
989			goto append_key;
990		s = utf8_tocstr(c->prompt_buffer);
991		c->prompt_callbackfn(c->prompt_data, s);
992		status_prompt_clear(c);
993		free(s);
994		return (1);
995	}
996
997	keys = options_get_number(c->session->options, "status-keys");
998	if (keys == MODEKEY_VI) {
999		switch (status_prompt_translate_key(c, key, &key)) {
1000		case 1:
1001			goto process_key;
1002		case 2:
1003			goto append_key;
1004		default:
1005			return (0);
1006		}
1007	}
1008
1009process_key:
1010	switch (key) {
1011	case KEYC_LEFT:
1012	case '\002': /* C-b */
1013		if (c->prompt_index > 0) {
1014			c->prompt_index--;
1015			c->flags |= CLIENT_STATUS;
1016		}
1017		break;
1018	case KEYC_RIGHT:
1019	case '\006': /* C-f */
1020		if (c->prompt_index < size) {
1021			c->prompt_index++;
1022			c->flags |= CLIENT_STATUS;
1023		}
1024		break;
1025	case KEYC_HOME:
1026	case '\001': /* C-a */
1027		if (c->prompt_index != 0) {
1028			c->prompt_index = 0;
1029			c->flags |= CLIENT_STATUS;
1030		}
1031		break;
1032	case KEYC_END:
1033	case '\005': /* C-e */
1034		if (c->prompt_index != size) {
1035			c->prompt_index = size;
1036			c->flags |= CLIENT_STATUS;
1037		}
1038		break;
1039	case '\011': /* Tab */
1040		if (c->prompt_buffer[0].size == 0)
1041			break;
1042
1043		idx = c->prompt_index;
1044		if (idx != 0)
1045			idx--;
1046
1047		/* Find the word we are in. */
1048		first = &c->prompt_buffer[idx];
1049		while (first > c->prompt_buffer && !status_prompt_space(first))
1050			first--;
1051		while (first->size != 0 && status_prompt_space(first))
1052			first++;
1053		last = &c->prompt_buffer[idx];
1054		while (last->size != 0 && !status_prompt_space(last))
1055			last++;
1056		while (last > c->prompt_buffer && status_prompt_space(last))
1057			last--;
1058		if (last->size != 0)
1059			last++;
1060		if (last <= first)
1061			break;
1062
1063		used = 0;
1064		for (ud = first; ud < last; ud++) {
1065			if (used + ud->size >= sizeof word)
1066				break;
1067			memcpy(word + used, ud->data, ud->size);
1068			used += ud->size;
1069		}
1070		if (ud != last)
1071			break;
1072		word[used] = '\0';
1073
1074		/* And try to complete it. */
1075		if ((s = status_prompt_complete(c->session, word)) == NULL)
1076			break;
1077
1078		/* Trim out word. */
1079		n = size - (last - c->prompt_buffer) + 1; /* with \0 */
1080		memmove(first, last, n * sizeof *c->prompt_buffer);
1081		size -= last - first;
1082
1083		/* Insert the new word. */
1084		size += strlen(s);
1085		off = first - c->prompt_buffer;
1086		c->prompt_buffer = xreallocarray(c->prompt_buffer, size + 1,
1087		    sizeof *c->prompt_buffer);
1088		first = c->prompt_buffer + off;
1089		memmove(first + strlen(s), first, n * sizeof *c->prompt_buffer);
1090		for (idx = 0; idx < strlen(s); idx++)
1091			utf8_set(&first[idx], s[idx]);
1092
1093		c->prompt_index = (first - c->prompt_buffer) + strlen(s);
1094		free(s);
1095
1096		c->flags |= CLIENT_STATUS;
1097		break;
1098	case KEYC_BSPACE:
1099	case '\010': /* C-h */
1100		if (c->prompt_index != 0) {
1101			if (c->prompt_index == size)
1102				c->prompt_buffer[--c->prompt_index].size = 0;
1103			else {
1104				memmove(c->prompt_buffer + c->prompt_index - 1,
1105				    c->prompt_buffer + c->prompt_index,
1106				    (size + 1 - c->prompt_index) *
1107				    sizeof *c->prompt_buffer);
1108				c->prompt_index--;
1109			}
1110			c->flags |= CLIENT_STATUS;
1111		}
1112		break;
1113	case KEYC_DC:
1114	case '\004': /* C-d */
1115		if (c->prompt_index != size) {
1116			memmove(c->prompt_buffer + c->prompt_index,
1117			    c->prompt_buffer + c->prompt_index + 1,
1118			    (size + 1 - c->prompt_index) *
1119			    sizeof *c->prompt_buffer);
1120			c->flags |= CLIENT_STATUS;
1121		}
1122		break;
1123	case '\025': /* C-u */
1124		c->prompt_buffer[0].size = 0;
1125		c->prompt_index = 0;
1126		c->flags |= CLIENT_STATUS;
1127		break;
1128	case '\013': /* C-k */
1129		if (c->prompt_index < size) {
1130			c->prompt_buffer[c->prompt_index].size = 0;
1131			c->flags |= CLIENT_STATUS;
1132		}
1133		break;
1134	case '\027': /* C-w */
1135		ws = options_get_string(oo, "word-separators");
1136		idx = c->prompt_index;
1137
1138		/* Find a non-separator. */
1139		while (idx != 0) {
1140			idx--;
1141			if (!status_prompt_in_list(ws, &c->prompt_buffer[idx]))
1142				break;
1143		}
1144
1145		/* Find the separator at the beginning of the word. */
1146		while (idx != 0) {
1147			idx--;
1148			if (status_prompt_in_list(ws, &c->prompt_buffer[idx])) {
1149				/* Go back to the word. */
1150				idx++;
1151				break;
1152			}
1153		}
1154
1155		memmove(c->prompt_buffer + idx,
1156		    c->prompt_buffer + c->prompt_index,
1157		    (size + 1 - c->prompt_index) *
1158		    sizeof *c->prompt_buffer);
1159		memset(c->prompt_buffer + size - (c->prompt_index - idx),
1160		    '\0', (c->prompt_index - idx) * sizeof *c->prompt_buffer);
1161		c->prompt_index = idx;
1162
1163		c->flags |= CLIENT_STATUS;
1164		break;
1165	case 'f'|KEYC_ESCAPE:
1166		ws = options_get_string(oo, "word-separators");
1167
1168		/* Find a word. */
1169		while (c->prompt_index != size) {
1170			idx = ++c->prompt_index;
1171			if (!status_prompt_in_list(ws, &c->prompt_buffer[idx]))
1172				break;
1173		}
1174
1175		/* Find the separator at the end of the word. */
1176		while (c->prompt_index != size) {
1177			idx = ++c->prompt_index;
1178			if (status_prompt_in_list(ws, &c->prompt_buffer[idx]))
1179				break;
1180		}
1181
1182		/* Back up to the end-of-word like vi. */
1183		if (options_get_number(oo, "status-keys") == MODEKEY_VI &&
1184		    c->prompt_index != 0)
1185			c->prompt_index--;
1186
1187		c->flags |= CLIENT_STATUS;
1188		break;
1189	case 'b'|KEYC_ESCAPE:
1190		ws = options_get_string(oo, "word-separators");
1191
1192		/* Find a non-separator. */
1193		while (c->prompt_index != 0) {
1194			idx = --c->prompt_index;
1195			if (!status_prompt_in_list(ws, &c->prompt_buffer[idx]))
1196				break;
1197		}
1198
1199		/* Find the separator at the beginning of the word. */
1200		while (c->prompt_index != 0) {
1201			idx = --c->prompt_index;
1202			if (status_prompt_in_list(ws, &c->prompt_buffer[idx])) {
1203				/* Go back to the word. */
1204				c->prompt_index++;
1205				break;
1206			}
1207		}
1208
1209		c->flags |= CLIENT_STATUS;
1210		break;
1211	case KEYC_UP:
1212	case '\020': /* C-p */
1213		histstr = status_prompt_up_history(&c->prompt_hindex);
1214		if (histstr == NULL)
1215			break;
1216		free(c->prompt_buffer);
1217		c->prompt_buffer = utf8_fromcstr(histstr);
1218		c->prompt_index = utf8_strlen(c->prompt_buffer);
1219		c->flags |= CLIENT_STATUS;
1220		break;
1221	case KEYC_DOWN:
1222	case '\016': /* C-n */
1223		histstr = status_prompt_down_history(&c->prompt_hindex);
1224		if (histstr == NULL)
1225			break;
1226		free(c->prompt_buffer);
1227		c->prompt_buffer = utf8_fromcstr(histstr);
1228		c->prompt_index = utf8_strlen(c->prompt_buffer);
1229		c->flags |= CLIENT_STATUS;
1230		break;
1231	case '\031': /* C-y */
1232		if ((pb = paste_get_top(NULL)) == NULL)
1233			break;
1234		bufdata = paste_buffer_data(pb, &bufsize);
1235		for (n = 0; n < bufsize; n++) {
1236			ch = (u_char)bufdata[n];
1237			if (ch < 32 || ch >= 127)
1238				break;
1239		}
1240
1241		c->prompt_buffer = xreallocarray(c->prompt_buffer, size + n + 1,
1242		    sizeof *c->prompt_buffer);
1243		if (c->prompt_index == size) {
1244			for (idx = 0; idx < n; idx++) {
1245				ud = &c->prompt_buffer[c->prompt_index + idx];
1246				utf8_set(ud, bufdata[idx]);
1247			}
1248			c->prompt_index += n;
1249			c->prompt_buffer[c->prompt_index].size = 0;
1250		} else {
1251			memmove(c->prompt_buffer + c->prompt_index + n,
1252			    c->prompt_buffer + c->prompt_index,
1253			    (size + 1 - c->prompt_index) *
1254			    sizeof *c->prompt_buffer);
1255			for (idx = 0; idx < n; idx++) {
1256				ud = &c->prompt_buffer[c->prompt_index + idx];
1257				utf8_set(ud, bufdata[idx]);
1258			}
1259			c->prompt_index += n;
1260		}
1261
1262		c->flags |= CLIENT_STATUS;
1263		break;
1264	case '\024': /* C-t */
1265		idx = c->prompt_index;
1266		if (idx < size)
1267			idx++;
1268		if (idx >= 2) {
1269			utf8_copy(&tmp, &c->prompt_buffer[idx - 2]);
1270			utf8_copy(&c->prompt_buffer[idx - 2],
1271			    &c->prompt_buffer[idx - 1]);
1272			utf8_copy(&c->prompt_buffer[idx - 1], &tmp);
1273			c->prompt_index = idx;
1274			c->flags |= CLIENT_STATUS;
1275		}
1276		break;
1277	case '\r':
1278	case '\n':
1279		s = utf8_tocstr(c->prompt_buffer);
1280		if (*s != '\0')
1281			status_prompt_add_history(s);
1282		if (c->prompt_callbackfn(c->prompt_data, s) == 0)
1283			status_prompt_clear(c);
1284		free(s);
1285		break;
1286	case '\033': /* Escape */
1287	case '\003': /* C-c */
1288		if (c->prompt_callbackfn(c->prompt_data, NULL) == 0)
1289			status_prompt_clear(c);
1290		break;
1291	}
1292
1293append_key:
1294	if (key <= 0x1f || key >= KEYC_BASE)
1295		return (0);
1296	if (utf8_split(key, &tmp) != UTF8_DONE)
1297		return (0);
1298
1299	c->prompt_buffer = xreallocarray(c->prompt_buffer, size + 2,
1300	    sizeof *c->prompt_buffer);
1301
1302	if (c->prompt_index == size) {
1303		utf8_copy(&c->prompt_buffer[c->prompt_index], &tmp);
1304		c->prompt_index++;
1305		c->prompt_buffer[c->prompt_index].size = 0;
1306	} else {
1307		memmove(c->prompt_buffer + c->prompt_index + 1,
1308		    c->prompt_buffer + c->prompt_index,
1309		    (size + 1 - c->prompt_index) *
1310		    sizeof *c->prompt_buffer);
1311		utf8_copy(&c->prompt_buffer[c->prompt_index], &tmp);
1312		c->prompt_index++;
1313	}
1314
1315	if (c->prompt_flags & PROMPT_SINGLE) {
1316		s = utf8_tocstr(c->prompt_buffer);
1317		if (strlen(s) != 1)
1318			status_prompt_clear(c);
1319		else if (c->prompt_callbackfn(c->prompt_data, s) == 0)
1320			status_prompt_clear(c);
1321		free(s);
1322	}
1323
1324	c->flags |= CLIENT_STATUS;
1325	return (0);
1326}
1327
1328/* Get previous line from the history. */
1329static const char *
1330status_prompt_up_history(u_int *idx)
1331{
1332	/*
1333	 * History runs from 0 to size - 1. Index is from 0 to size. Zero is
1334	 * empty.
1335	 */
1336
1337	if (status_prompt_hsize == 0 || *idx == status_prompt_hsize)
1338		return (NULL);
1339	(*idx)++;
1340	return (status_prompt_hlist[status_prompt_hsize - *idx]);
1341}
1342
1343/* Get next line from the history. */
1344static const char *
1345status_prompt_down_history(u_int *idx)
1346{
1347	if (status_prompt_hsize == 0 || *idx == 0)
1348		return ("");
1349	(*idx)--;
1350	if (*idx == 0)
1351		return ("");
1352	return (status_prompt_hlist[status_prompt_hsize - *idx]);
1353}
1354
1355/* Add line to the history. */
1356static void
1357status_prompt_add_history(const char *line)
1358{
1359	size_t	size;
1360
1361	if (status_prompt_hsize > 0 &&
1362	    strcmp(status_prompt_hlist[status_prompt_hsize - 1], line) == 0)
1363		return;
1364
1365	if (status_prompt_hsize == PROMPT_HISTORY) {
1366		free(status_prompt_hlist[0]);
1367
1368		size = (PROMPT_HISTORY - 1) * sizeof *status_prompt_hlist;
1369		memmove(&status_prompt_hlist[0], &status_prompt_hlist[1], size);
1370
1371		status_prompt_hlist[status_prompt_hsize - 1] = xstrdup(line);
1372		return;
1373	}
1374
1375	status_prompt_hlist = xreallocarray(status_prompt_hlist,
1376	    status_prompt_hsize + 1, sizeof *status_prompt_hlist);
1377	status_prompt_hlist[status_prompt_hsize++] = xstrdup(line);
1378}
1379
1380/* Build completion list. */
1381static const char **
1382status_prompt_complete_list(u_int *size, const char *s)
1383{
1384	const char				**list = NULL, **layout;
1385	const struct cmd_entry			**cmdent;
1386	const struct options_table_entry	 *oe;
1387	const char				 *layouts[] = {
1388		"even-horizontal", "even-vertical", "main-horizontal",
1389		"main-vertical", "tiled", NULL
1390	};
1391
1392	*size = 0;
1393	for (cmdent = cmd_table; *cmdent != NULL; cmdent++) {
1394		if (strncmp((*cmdent)->name, s, strlen(s)) == 0) {
1395			list = xreallocarray(list, (*size) + 1, sizeof *list);
1396			list[(*size)++] = (*cmdent)->name;
1397		}
1398	}
1399	for (oe = options_table; oe->name != NULL; oe++) {
1400		if (strncmp(oe->name, s, strlen(s)) == 0) {
1401			list = xreallocarray(list, (*size) + 1, sizeof *list);
1402			list[(*size)++] = oe->name;
1403		}
1404	}
1405	for (layout = layouts; *layout != NULL; layout++) {
1406		if (strncmp(*layout, s, strlen(s)) == 0) {
1407			list = xreallocarray(list, (*size) + 1, sizeof *list);
1408			list[(*size)++] = *layout;
1409		}
1410	}
1411	return (list);
1412}
1413
1414/* Find longest prefix. */
1415static char *
1416status_prompt_complete_prefix(const char **list, u_int size)
1417{
1418	char	 *out;
1419	u_int	  i;
1420	size_t	  j;
1421
1422	out = xstrdup(list[0]);
1423	for (i = 1; i < size; i++) {
1424		j = strlen(list[i]);
1425		if (j > strlen(out))
1426			j = strlen(out);
1427		for (; j > 0; j--) {
1428			if (out[j - 1] != list[i][j - 1])
1429				out[j - 1] = '\0';
1430		}
1431	}
1432	return (out);
1433}
1434
1435/* Complete word. */
1436static char *
1437status_prompt_complete(struct session *session, const char *s)
1438{
1439	const char	**list = NULL, *colon;
1440	u_int		  size = 0, i;
1441	struct session	 *s_loop;
1442	struct winlink	 *wl;
1443	struct window	 *w;
1444	char		 *copy, *out, *tmp;
1445
1446	if (*s == '\0')
1447		return (NULL);
1448	out = NULL;
1449
1450	if (strncmp(s, "-t", 2) != 0 && strncmp(s, "-s", 2) != 0) {
1451		list = status_prompt_complete_list(&size, s);
1452		if (size == 0)
1453			out = NULL;
1454		else if (size == 1)
1455			xasprintf(&out, "%s ", list[0]);
1456		else
1457			out = status_prompt_complete_prefix(list, size);
1458		free(list);
1459		return (out);
1460	}
1461	copy = xstrdup(s);
1462
1463	colon = ":";
1464	if (copy[strlen(copy) - 1] == ':')
1465		copy[strlen(copy) - 1] = '\0';
1466	else
1467		colon = "";
1468	s = copy + 2;
1469
1470	RB_FOREACH(s_loop, sessions, &sessions) {
1471		if (strncmp(s_loop->name, s, strlen(s)) == 0) {
1472			list = xreallocarray(list, size + 2, sizeof *list);
1473			list[size++] = s_loop->name;
1474		}
1475	}
1476	if (size == 1) {
1477		out = xstrdup(list[0]);
1478		if (session_find(list[0]) != NULL)
1479			colon = ":";
1480	} else if (size != 0)
1481		out = status_prompt_complete_prefix(list, size);
1482	if (out != NULL) {
1483		xasprintf(&tmp, "-%c%s%s", copy[1], out, colon);
1484		out = tmp;
1485		goto found;
1486	}
1487
1488	colon = "";
1489	if (*s == ':') {
1490		RB_FOREACH(wl, winlinks, &session->windows) {
1491			xasprintf(&tmp, ":%s", wl->window->name);
1492			if (strncmp(tmp, s, strlen(s)) == 0){
1493				list = xreallocarray(list, size + 1,
1494				    sizeof *list);
1495				list[size++] = tmp;
1496				continue;
1497			}
1498			free(tmp);
1499
1500			xasprintf(&tmp, ":%d", wl->idx);
1501			if (strncmp(tmp, s, strlen(s)) == 0) {
1502				list = xreallocarray(list, size + 1,
1503				    sizeof *list);
1504				list[size++] = tmp;
1505				continue;
1506			}
1507			free(tmp);
1508		}
1509	} else {
1510		RB_FOREACH(s_loop, sessions, &sessions) {
1511			RB_FOREACH(wl, winlinks, &s_loop->windows) {
1512				w = wl->window;
1513
1514				xasprintf(&tmp, "%s:%s", s_loop->name, w->name);
1515				if (strncmp(tmp, s, strlen(s)) == 0) {
1516					list = xreallocarray(list, size + 1,
1517					    sizeof *list);
1518					list[size++] = tmp;
1519					continue;
1520				}
1521				free(tmp);
1522
1523				xasprintf(&tmp, "%s:%d", s_loop->name, wl->idx);
1524				if (strncmp(tmp, s, strlen(s)) == 0) {
1525					list = xreallocarray(list, size + 1,
1526					    sizeof *list);
1527					list[size++] = tmp;
1528					continue;
1529				}
1530				free(tmp);
1531			}
1532		}
1533	}
1534	if (size == 1) {
1535		out = xstrdup(list[0]);
1536		colon = " ";
1537	} else if (size != 0)
1538		out = status_prompt_complete_prefix(list, size);
1539	if (out != NULL) {
1540		xasprintf(&tmp, "-%c%s%s", copy[1], out, colon);
1541		out = tmp;
1542	}
1543
1544	for (i = 0; i < size; i++)
1545		free((void *)list[i]);
1546
1547found:
1548	free(copy);
1549	free(list);
1550	return (out);
1551}
1552