xref: /netbsd-src/external/bsd/tmux/dist/window-tree.c (revision f844e94ef29eebc7999c12636b87f541bb86868b)
1c9ad075bSchristos /* $OpenBSD$ */
2c9ad075bSchristos 
3c9ad075bSchristos /*
4c9ad075bSchristos  * Copyright (c) 2017 Nicholas Marriott <nicholas.marriott@gmail.com>
5c9ad075bSchristos  *
6c9ad075bSchristos  * Permission to use, copy, modify, and distribute this software for any
7c9ad075bSchristos  * purpose with or without fee is hereby granted, provided that the above
8c9ad075bSchristos  * copyright notice and this permission notice appear in all copies.
9c9ad075bSchristos  *
10c9ad075bSchristos  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11c9ad075bSchristos  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12c9ad075bSchristos  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13c9ad075bSchristos  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14c9ad075bSchristos  * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15c9ad075bSchristos  * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16c9ad075bSchristos  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17c9ad075bSchristos  */
18c9ad075bSchristos 
19c9ad075bSchristos #include <sys/types.h>
20c9ad075bSchristos 
21c7e17de0Schristos #include <ctype.h>
22c9ad075bSchristos #include <stdlib.h>
23c9ad075bSchristos #include <string.h>
24c9ad075bSchristos 
25c9ad075bSchristos #include "tmux.h"
26c9ad075bSchristos 
270a274e86Schristos static struct screen	*window_tree_init(struct window_mode_entry *,
28c9ad075bSchristos 			     struct cmd_find_state *, struct args *);
290a274e86Schristos static void		 window_tree_free(struct window_mode_entry *);
300a274e86Schristos static void		 window_tree_resize(struct window_mode_entry *, u_int,
310a274e86Schristos 			     u_int);
32e271dbb8Schristos static void		 window_tree_update(struct window_mode_entry *);
330a274e86Schristos static void		 window_tree_key(struct window_mode_entry *,
340a274e86Schristos 			     struct client *, struct session *,
350a274e86Schristos 			     struct winlink *, key_code, struct mouse_event *);
36c9ad075bSchristos 
3768e6ba84Schristos #define WINDOW_TREE_DEFAULT_COMMAND "switch-client -Zt '%%'"
38c9ad075bSchristos 
39c9ad075bSchristos #define WINDOW_TREE_DEFAULT_FORMAT \
40c9ad075bSchristos 	"#{?pane_format," \
41e271dbb8Schristos 		"#{?pane_marked,#[reverse],}" \
42e271dbb8Schristos 		"#{pane_current_command}#{?pane_active,*,}#{?pane_marked,M,}" \
43e271dbb8Schristos 		"#{?#{&&:#{pane_title},#{!=:#{pane_title},#{host_short}}},: \"#{pane_title}\",}" \
44c9ad075bSchristos 	"," \
45c9ad075bSchristos 		"#{?window_format," \
46e271dbb8Schristos 			"#{?window_marked_flag,#[reverse],}" \
47c9ad075bSchristos 			"#{window_name}#{window_flags}" \
48e271dbb8Schristos 			"#{?#{&&:#{==:#{window_panes},1},#{&&:#{pane_title},#{!=:#{pane_title},#{host_short}}}},: \"#{pane_title}\",}" \
49c9ad075bSchristos 		"," \
50c9ad075bSchristos 			"#{session_windows} windows" \
51c7e17de0Schristos 			"#{?session_grouped, " \
52c7e17de0Schristos 				"(group #{session_group}: " \
53c7e17de0Schristos 				"#{session_group_list})," \
54c7e17de0Schristos 			"}" \
55c9ad075bSchristos 			"#{?session_attached, (attached),}" \
56c9ad075bSchristos 		"}" \
57c9ad075bSchristos 	"}"
58c9ad075bSchristos 
59e271dbb8Schristos #define WINDOW_TREE_DEFAULT_KEY_FORMAT \
60e271dbb8Schristos 	"#{?#{e|<:#{line},10}," \
61e271dbb8Schristos 		"#{line}" \
62e271dbb8Schristos 	"," \
63e271dbb8Schristos 		"#{?#{e|<:#{line},36},"	\
64e271dbb8Schristos 	        	"M-#{a:#{e|+:97,#{e|-:#{line},10}}}" \
65e271dbb8Schristos 		"," \
66e271dbb8Schristos 	        	"" \
67e271dbb8Schristos 		"}" \
68e271dbb8Schristos 	"}"
69e271dbb8Schristos 
7030744affSchristos static const struct menu_item window_tree_menu_items[] = {
71e271dbb8Schristos 	{ "Select", '\r', NULL },
72e271dbb8Schristos 	{ "Expand", KEYC_RIGHT, NULL },
73e271dbb8Schristos 	{ "Mark", 'm', NULL },
7430744affSchristos 	{ "", KEYC_NONE, NULL },
7530744affSchristos 	{ "Tag", 't', NULL },
7630744affSchristos 	{ "Tag All", '\024', NULL },
7730744affSchristos 	{ "Tag None", 'T', NULL },
7830744affSchristos 	{ "", KEYC_NONE, NULL },
7930744affSchristos 	{ "Kill", 'x', NULL },
8030744affSchristos 	{ "Kill Tagged", 'X', NULL },
8130744affSchristos 	{ "", KEYC_NONE, NULL },
8230744affSchristos 	{ "Cancel", 'q', NULL },
8330744affSchristos 
8430744affSchristos 	{ NULL, KEYC_NONE, NULL }
8530744affSchristos };
8630744affSchristos 
87c9ad075bSchristos const struct window_mode window_tree_mode = {
88c9ad075bSchristos 	.name = "tree-mode",
890a274e86Schristos 	.default_format = WINDOW_TREE_DEFAULT_FORMAT,
90c9ad075bSchristos 
91c9ad075bSchristos 	.init = window_tree_init,
92c9ad075bSchristos 	.free = window_tree_free,
93c9ad075bSchristos 	.resize = window_tree_resize,
94e271dbb8Schristos 	.update = window_tree_update,
95c9ad075bSchristos 	.key = window_tree_key,
96c9ad075bSchristos };
97c9ad075bSchristos 
98c9ad075bSchristos enum window_tree_sort_type {
99c9ad075bSchristos 	WINDOW_TREE_BY_INDEX,
100c9ad075bSchristos 	WINDOW_TREE_BY_NAME,
101c9ad075bSchristos 	WINDOW_TREE_BY_TIME,
102c9ad075bSchristos };
103c9ad075bSchristos static const char *window_tree_sort_list[] = {
104c9ad075bSchristos 	"index",
105c9ad075bSchristos 	"name",
106c9ad075bSchristos 	"time"
107c9ad075bSchristos };
10868e6ba84Schristos static struct mode_tree_sort_criteria *window_tree_sort;
109c9ad075bSchristos 
110c9ad075bSchristos enum window_tree_type {
111c9ad075bSchristos 	WINDOW_TREE_NONE,
112c9ad075bSchristos 	WINDOW_TREE_SESSION,
113c9ad075bSchristos 	WINDOW_TREE_WINDOW,
114c9ad075bSchristos 	WINDOW_TREE_PANE,
115c9ad075bSchristos };
116c9ad075bSchristos 
117c9ad075bSchristos struct window_tree_itemdata {
118c9ad075bSchristos 	enum window_tree_type	type;
119c9ad075bSchristos 	int			session;
120c9ad075bSchristos 	int			winlink;
121c9ad075bSchristos 	int			pane;
122c9ad075bSchristos };
123c9ad075bSchristos 
124c9ad075bSchristos struct window_tree_modedata {
125c9ad075bSchristos 	struct window_pane		 *wp;
126c9ad075bSchristos 	int				  dead;
127c9ad075bSchristos 	int				  references;
128c9ad075bSchristos 
129c9ad075bSchristos 	struct mode_tree_data		 *data;
130c9ad075bSchristos 	char				 *format;
131e271dbb8Schristos 	char				 *key_format;
132c9ad075bSchristos 	char				 *command;
133c7e17de0Schristos 	int				  squash_groups;
134c9ad075bSchristos 
135c9ad075bSchristos 	struct window_tree_itemdata	**item_list;
136c9ad075bSchristos 	u_int				  item_size;
137c9ad075bSchristos 
138c9ad075bSchristos 	const char			 *entered;
139c9ad075bSchristos 
140c9ad075bSchristos 	struct cmd_find_state		  fs;
141c9ad075bSchristos 	enum window_tree_type		  type;
142c9ad075bSchristos 
143c9ad075bSchristos 	int				  offset;
144c7e17de0Schristos 
145c7e17de0Schristos 	int				  left;
146c7e17de0Schristos 	int				  right;
147c7e17de0Schristos 	u_int				  start;
148c7e17de0Schristos 	u_int				  end;
149c7e17de0Schristos 	u_int				  each;
150c9ad075bSchristos };
151c9ad075bSchristos 
152c9ad075bSchristos static void
window_tree_pull_item(struct window_tree_itemdata * item,struct session ** sp,struct winlink ** wlp,struct window_pane ** wp)153c9ad075bSchristos window_tree_pull_item(struct window_tree_itemdata *item, struct session **sp,
154c9ad075bSchristos     struct winlink **wlp, struct window_pane **wp)
155c9ad075bSchristos {
156c9ad075bSchristos 	*wp = NULL;
157c9ad075bSchristos 	*wlp = NULL;
158c9ad075bSchristos 	*sp = session_find_by_id(item->session);
159c9ad075bSchristos 	if (*sp == NULL)
160c9ad075bSchristos 		return;
161c9ad075bSchristos 	if (item->type == WINDOW_TREE_SESSION) {
162c9ad075bSchristos 		*wlp = (*sp)->curw;
163c9ad075bSchristos 		*wp = (*wlp)->window->active;
164c9ad075bSchristos 		return;
165c9ad075bSchristos 	}
166c9ad075bSchristos 
167c9ad075bSchristos 	*wlp = winlink_find_by_index(&(*sp)->windows, item->winlink);
168c9ad075bSchristos 	if (*wlp == NULL) {
169c9ad075bSchristos 		*sp = NULL;
170c9ad075bSchristos 		return;
171c9ad075bSchristos 	}
172c9ad075bSchristos 	if (item->type == WINDOW_TREE_WINDOW) {
173c9ad075bSchristos 		*wp = (*wlp)->window->active;
174c9ad075bSchristos 		return;
175c9ad075bSchristos 	}
176c9ad075bSchristos 
177c9ad075bSchristos 	*wp = window_pane_find_by_id(item->pane);
178c9ad075bSchristos 	if (!window_has_pane((*wlp)->window, *wp))
179c9ad075bSchristos 		*wp = NULL;
180c9ad075bSchristos 	if (*wp == NULL) {
181c9ad075bSchristos 		*sp = NULL;
182c9ad075bSchristos 		*wlp = NULL;
183c9ad075bSchristos 		return;
184c9ad075bSchristos 	}
185c9ad075bSchristos }
186c9ad075bSchristos 
187c9ad075bSchristos static struct window_tree_itemdata *
window_tree_add_item(struct window_tree_modedata * data)188c9ad075bSchristos window_tree_add_item(struct window_tree_modedata *data)
189c9ad075bSchristos {
190c9ad075bSchristos 	struct window_tree_itemdata	*item;
191c9ad075bSchristos 
192c9ad075bSchristos 	data->item_list = xreallocarray(data->item_list, data->item_size + 1,
193c9ad075bSchristos 	    sizeof *data->item_list);
194c9ad075bSchristos 	item = data->item_list[data->item_size++] = xcalloc(1, sizeof *item);
195c9ad075bSchristos 	return (item);
196c9ad075bSchristos }
197c9ad075bSchristos 
198c9ad075bSchristos static void
window_tree_free_item(struct window_tree_itemdata * item)199c9ad075bSchristos window_tree_free_item(struct window_tree_itemdata *item)
200c9ad075bSchristos {
201c9ad075bSchristos 	free(item);
202c9ad075bSchristos }
203c9ad075bSchristos 
204c9ad075bSchristos static int
window_tree_cmp_session(const void * a0,const void * b0)20568e6ba84Schristos window_tree_cmp_session(const void *a0, const void *b0)
206c9ad075bSchristos {
207c9ad075bSchristos 	const struct session *const	*a = a0;
208c9ad075bSchristos 	const struct session *const	*b = b0;
20968e6ba84Schristos 	const struct session		*sa = *a;
21068e6ba84Schristos 	const struct session		*sb = *b;
21168e6ba84Schristos 	int				 result = 0;
212c9ad075bSchristos 
21368e6ba84Schristos 	switch (window_tree_sort->field) {
21468e6ba84Schristos 	case WINDOW_TREE_BY_INDEX:
21568e6ba84Schristos 		result = sa->id - sb->id;
21668e6ba84Schristos 		break;
21768e6ba84Schristos 	case WINDOW_TREE_BY_TIME:
21868e6ba84Schristos 		if (timercmp(&sa->activity_time, &sb->activity_time, >)) {
21968e6ba84Schristos 			result = -1;
22068e6ba84Schristos 			break;
22168e6ba84Schristos 		}
22268e6ba84Schristos 		if (timercmp(&sa->activity_time, &sb->activity_time, <)) {
22368e6ba84Schristos 			result = 1;
22468e6ba84Schristos 			break;
22568e6ba84Schristos 		}
22668e6ba84Schristos 		/* FALLTHROUGH */
22768e6ba84Schristos 	case WINDOW_TREE_BY_NAME:
22868e6ba84Schristos 		result = strcmp(sa->name, sb->name);
22968e6ba84Schristos 		break;
23068e6ba84Schristos 	}
23168e6ba84Schristos 
23268e6ba84Schristos 	if (window_tree_sort->reversed)
23368e6ba84Schristos 		result = -result;
23468e6ba84Schristos 	return (result);
235c9ad075bSchristos }
236c9ad075bSchristos 
237c9ad075bSchristos static int
window_tree_cmp_window(const void * a0,const void * b0)23868e6ba84Schristos window_tree_cmp_window(const void *a0, const void *b0)
239c9ad075bSchristos {
240c9ad075bSchristos 	const struct winlink *const	*a = a0;
241c9ad075bSchristos 	const struct winlink *const	*b = b0;
24268e6ba84Schristos 	const struct winlink		*wla = *a;
24368e6ba84Schristos 	const struct winlink		*wlb = *b;
24468e6ba84Schristos 	struct window			*wa = wla->window;
24568e6ba84Schristos 	struct window			*wb = wlb->window;
24668e6ba84Schristos 	int				 result = 0;
247c9ad075bSchristos 
24868e6ba84Schristos 	switch (window_tree_sort->field) {
24968e6ba84Schristos 	case WINDOW_TREE_BY_INDEX:
25068e6ba84Schristos 		result = wla->idx - wlb->idx;
25168e6ba84Schristos 		break;
25268e6ba84Schristos 	case WINDOW_TREE_BY_TIME:
25368e6ba84Schristos 		if (timercmp(&wa->activity_time, &wb->activity_time, >)) {
25468e6ba84Schristos 			result = -1;
25568e6ba84Schristos 			break;
25668e6ba84Schristos 		}
25768e6ba84Schristos 		if (timercmp(&wa->activity_time, &wb->activity_time, <)) {
25868e6ba84Schristos 			result = 1;
25968e6ba84Schristos 			break;
26068e6ba84Schristos 		}
26168e6ba84Schristos 		/* FALLTHROUGH */
26268e6ba84Schristos 	case WINDOW_TREE_BY_NAME:
26368e6ba84Schristos 		result = strcmp(wa->name, wb->name);
26468e6ba84Schristos 		break;
26568e6ba84Schristos 	}
26668e6ba84Schristos 
26768e6ba84Schristos 	if (window_tree_sort->reversed)
26868e6ba84Schristos 		result = -result;
26968e6ba84Schristos 	return (result);
270c9ad075bSchristos }
271c9ad075bSchristos 
272c9ad075bSchristos static int
window_tree_cmp_pane(const void * a0,const void * b0)27368e6ba84Schristos window_tree_cmp_pane(const void *a0, const void *b0)
274c9ad075bSchristos {
275*f844e94eSwiz 	struct window_pane	**a = (struct window_pane **)__UNCONST(a0);
276*f844e94eSwiz 	struct window_pane	**b = (struct window_pane **)__UNCONST(b0);
27768e6ba84Schristos 	int			  result;
278*f844e94eSwiz 	u_int			  ai, bi;
279c9ad075bSchristos 
28068e6ba84Schristos 	if (window_tree_sort->field == WINDOW_TREE_BY_TIME)
28168e6ba84Schristos 		result = (*a)->active_point - (*b)->active_point;
28268e6ba84Schristos 	else {
28368e6ba84Schristos 		/*
28468e6ba84Schristos 		 * Panes don't have names, so use number order for any other
28568e6ba84Schristos 		 * sort field.
28668e6ba84Schristos 		 */
287*f844e94eSwiz 		window_pane_index(*a, &ai);
288*f844e94eSwiz 		window_pane_index(*b, &bi);
289*f844e94eSwiz 		result = ai - bi;
29068e6ba84Schristos 	}
29168e6ba84Schristos 	if (window_tree_sort->reversed)
29268e6ba84Schristos 		result = -result;
29368e6ba84Schristos 	return (result);
294c9ad075bSchristos }
295c9ad075bSchristos 
296c9ad075bSchristos static void
window_tree_build_pane(struct session * s,struct winlink * wl,struct window_pane * wp,void * modedata,struct mode_tree_item * parent)297c9ad075bSchristos window_tree_build_pane(struct session *s, struct winlink *wl,
298c9ad075bSchristos     struct window_pane *wp, void *modedata, struct mode_tree_item *parent)
299c9ad075bSchristos {
300c9ad075bSchristos 	struct window_tree_modedata	*data = modedata;
301c9ad075bSchristos 	struct window_tree_itemdata	*item;
302c9ad075bSchristos 	char				*name, *text;
303c9ad075bSchristos 	u_int				 idx;
304c9ad075bSchristos 
305c9ad075bSchristos 	window_pane_index(wp, &idx);
306c9ad075bSchristos 
307c9ad075bSchristos 	item = window_tree_add_item(data);
308c9ad075bSchristos 	item->type = WINDOW_TREE_PANE;
309c9ad075bSchristos 	item->session = s->id;
310c9ad075bSchristos 	item->winlink = wl->idx;
311c9ad075bSchristos 	item->pane = wp->id;
312c9ad075bSchristos 
313c9ad075bSchristos 	text = format_single(NULL, data->format, NULL, s, wl, wp);
314c9ad075bSchristos 	xasprintf(&name, "%u", idx);
315c9ad075bSchristos 
3166cb901deSkre 	mode_tree_add(data->data, parent, item, (uintptr_t)wp, name, text, -1);
317c9ad075bSchristos 	free(text);
318c9ad075bSchristos 	free(name);
319c9ad075bSchristos }
320c9ad075bSchristos 
321c9ad075bSchristos static int
window_tree_filter_pane(struct session * s,struct winlink * wl,struct window_pane * wp,const char * filter)322c9ad075bSchristos window_tree_filter_pane(struct session *s, struct winlink *wl,
323c9ad075bSchristos     struct window_pane *wp, const char *filter)
324c9ad075bSchristos {
325c9ad075bSchristos 	char	*cp;
326c9ad075bSchristos 	int	 result;
327c9ad075bSchristos 
328c9ad075bSchristos 	if (filter == NULL)
329c9ad075bSchristos 		return (1);
330c9ad075bSchristos 
331c9ad075bSchristos 	cp = format_single(NULL, filter, NULL, s, wl, wp);
332c9ad075bSchristos 	result = format_true(cp);
333c9ad075bSchristos 	free(cp);
334c9ad075bSchristos 
335c9ad075bSchristos 	return (result);
336c9ad075bSchristos }
337c9ad075bSchristos 
338c9ad075bSchristos static int
window_tree_build_window(struct session * s,struct winlink * wl,void * modedata,struct mode_tree_sort_criteria * sort_crit,struct mode_tree_item * parent,const char * filter)33968e6ba84Schristos window_tree_build_window(struct session *s, struct winlink *wl,
34068e6ba84Schristos     void *modedata, struct mode_tree_sort_criteria *sort_crit,
34168e6ba84Schristos     struct mode_tree_item *parent, const char *filter)
342c9ad075bSchristos {
343c9ad075bSchristos 	struct window_tree_modedata	*data = modedata;
344c9ad075bSchristos 	struct window_tree_itemdata	*item;
345c9ad075bSchristos 	struct mode_tree_item		*mti;
346c9ad075bSchristos 	char				*name, *text;
347c9ad075bSchristos 	struct window_pane		*wp, **l;
348c9ad075bSchristos 	u_int				 n, i;
349c9ad075bSchristos 	int				 expanded;
350c9ad075bSchristos 
351c9ad075bSchristos 	item = window_tree_add_item(data);
352c9ad075bSchristos 	item->type = WINDOW_TREE_WINDOW;
353c9ad075bSchristos 	item->session = s->id;
354c9ad075bSchristos 	item->winlink = wl->idx;
355c9ad075bSchristos 	item->pane = -1;
356c9ad075bSchristos 
357c9ad075bSchristos 	text = format_single(NULL, data->format, NULL, s, wl, NULL);
358c9ad075bSchristos 	xasprintf(&name, "%u", wl->idx);
359c9ad075bSchristos 
360c9ad075bSchristos 	if (data->type == WINDOW_TREE_SESSION ||
361c9ad075bSchristos 	    data->type == WINDOW_TREE_WINDOW)
362c9ad075bSchristos 		expanded = 0;
363c9ad075bSchristos 	else
364c9ad075bSchristos 		expanded = 1;
3656cb901deSkre 	mti = mode_tree_add(data->data, parent, item, (uintptr_t)wl, name, text,
366c9ad075bSchristos 	    expanded);
367c9ad075bSchristos 	free(text);
368c9ad075bSchristos 	free(name);
369c9ad075bSchristos 
370c7e17de0Schristos 	if ((wp = TAILQ_FIRST(&wl->window->panes)) == NULL)
371c7e17de0Schristos 		goto empty;
372c9ad075bSchristos 	if (TAILQ_NEXT(wp, entry) == NULL) {
373c9ad075bSchristos 		if (!window_tree_filter_pane(s, wl, wp, filter))
374c9ad075bSchristos 			goto empty;
375c9ad075bSchristos 		return (1);
376c9ad075bSchristos 	}
377c9ad075bSchristos 
378c9ad075bSchristos 	l = NULL;
379c9ad075bSchristos 	n = 0;
380c9ad075bSchristos 
381c9ad075bSchristos 	TAILQ_FOREACH(wp, &wl->window->panes, entry) {
382c9ad075bSchristos 		if (!window_tree_filter_pane(s, wl, wp, filter))
383c9ad075bSchristos 			continue;
384c9ad075bSchristos 		l = xreallocarray(l, n + 1, sizeof *l);
385c9ad075bSchristos 		l[n++] = wp;
386c9ad075bSchristos 	}
387c9ad075bSchristos 	if (n == 0)
388c9ad075bSchristos 		goto empty;
389c9ad075bSchristos 
39068e6ba84Schristos 	window_tree_sort = sort_crit;
39168e6ba84Schristos 	qsort(l, n, sizeof *l, window_tree_cmp_pane);
392c9ad075bSchristos 
393c9ad075bSchristos 	for (i = 0; i < n; i++)
394c9ad075bSchristos 		window_tree_build_pane(s, wl, l[i], modedata, mti);
395c9ad075bSchristos 	free(l);
396c9ad075bSchristos 	return (1);
397c9ad075bSchristos 
398c9ad075bSchristos empty:
399c9ad075bSchristos 	window_tree_free_item(item);
400c9ad075bSchristos 	data->item_size--;
401c9ad075bSchristos 	mode_tree_remove(data->data, mti);
402c9ad075bSchristos 	return (0);
403c9ad075bSchristos }
404c9ad075bSchristos 
405c9ad075bSchristos static void
window_tree_build_session(struct session * s,void * modedata,struct mode_tree_sort_criteria * sort_crit,const char * filter)406c9ad075bSchristos window_tree_build_session(struct session *s, void *modedata,
40768e6ba84Schristos     struct mode_tree_sort_criteria *sort_crit, const char *filter)
408c9ad075bSchristos {
409c9ad075bSchristos 	struct window_tree_modedata	*data = modedata;
410c9ad075bSchristos 	struct window_tree_itemdata	*item;
411c9ad075bSchristos 	struct mode_tree_item		*mti;
412c9ad075bSchristos 	char				*text;
413c9ad075bSchristos 	struct winlink			*wl, **l;
414c9ad075bSchristos 	u_int				 n, i, empty;
415c9ad075bSchristos 	int				 expanded;
416c9ad075bSchristos 
417c9ad075bSchristos 	item = window_tree_add_item(data);
418c9ad075bSchristos 	item->type = WINDOW_TREE_SESSION;
419c9ad075bSchristos 	item->session = s->id;
420c9ad075bSchristos 	item->winlink = -1;
421c9ad075bSchristos 	item->pane = -1;
422c9ad075bSchristos 
423c9ad075bSchristos 	text = format_single(NULL, data->format, NULL, s, NULL, NULL);
424c9ad075bSchristos 
425c9ad075bSchristos 	if (data->type == WINDOW_TREE_SESSION)
426c9ad075bSchristos 		expanded = 0;
427c9ad075bSchristos 	else
428c9ad075bSchristos 		expanded = 1;
4296cb901deSkre 	mti = mode_tree_add(data->data, NULL, item, (uintptr_t)s, s->name, text,
430c9ad075bSchristos 	    expanded);
431c9ad075bSchristos 	free(text);
432c9ad075bSchristos 
433c9ad075bSchristos 	l = NULL;
434c9ad075bSchristos 	n = 0;
435c9ad075bSchristos 	RB_FOREACH(wl, winlinks, &s->windows) {
436c9ad075bSchristos 		l = xreallocarray(l, n + 1, sizeof *l);
437c9ad075bSchristos 		l[n++] = wl;
438c9ad075bSchristos 	}
43968e6ba84Schristos 	window_tree_sort = sort_crit;
44068e6ba84Schristos 	qsort(l, n, sizeof *l, window_tree_cmp_window);
441c9ad075bSchristos 
442c9ad075bSchristos 	empty = 0;
443c9ad075bSchristos 	for (i = 0; i < n; i++) {
44468e6ba84Schristos 		if (!window_tree_build_window(s, l[i], modedata, sort_crit, mti,
445c9ad075bSchristos 		    filter))
446c9ad075bSchristos 			empty++;
447c9ad075bSchristos 	}
448c9ad075bSchristos 	if (empty == n) {
449c9ad075bSchristos 		window_tree_free_item(item);
450c9ad075bSchristos 		data->item_size--;
451c9ad075bSchristos 		mode_tree_remove(data->data, mti);
452c9ad075bSchristos 	}
453c9ad075bSchristos 	free(l);
454c9ad075bSchristos }
455c9ad075bSchristos 
456c9ad075bSchristos static void
window_tree_build(void * modedata,struct mode_tree_sort_criteria * sort_crit,uint64_t * tag,const char * filter)45768e6ba84Schristos window_tree_build(void *modedata, struct mode_tree_sort_criteria *sort_crit,
45868e6ba84Schristos     uint64_t *tag, const char *filter)
459c9ad075bSchristos {
460c9ad075bSchristos 	struct window_tree_modedata	*data = modedata;
461c9ad075bSchristos 	struct session			*s, **l;
462c7e17de0Schristos 	struct session_group		*sg, *current;
463c9ad075bSchristos 	u_int				 n, i;
464c9ad075bSchristos 
465c7e17de0Schristos 	current = session_group_contains(data->fs.s);
466c7e17de0Schristos 
467c9ad075bSchristos 	for (i = 0; i < data->item_size; i++)
468c9ad075bSchristos 		window_tree_free_item(data->item_list[i]);
469c9ad075bSchristos 	free(data->item_list);
470c9ad075bSchristos 	data->item_list = NULL;
471c9ad075bSchristos 	data->item_size = 0;
472c9ad075bSchristos 
473c9ad075bSchristos 	l = NULL;
474c9ad075bSchristos 	n = 0;
475c9ad075bSchristos 	RB_FOREACH(s, sessions, &sessions) {
476c7e17de0Schristos 		if (data->squash_groups &&
477c7e17de0Schristos 		    (sg = session_group_contains(s)) != NULL) {
478c7e17de0Schristos 			if ((sg == current && s != data->fs.s) ||
479c7e17de0Schristos 			    (sg != current && s != TAILQ_FIRST(&sg->sessions)))
480c7e17de0Schristos 				continue;
481c7e17de0Schristos 		}
482c9ad075bSchristos 		l = xreallocarray(l, n + 1, sizeof *l);
483c9ad075bSchristos 		l[n++] = s;
484c9ad075bSchristos 	}
48568e6ba84Schristos 	window_tree_sort = sort_crit;
48668e6ba84Schristos 	qsort(l, n, sizeof *l, window_tree_cmp_session);
487c9ad075bSchristos 
488c9ad075bSchristos 	for (i = 0; i < n; i++)
48968e6ba84Schristos 		window_tree_build_session(l[i], modedata, sort_crit, filter);
490c9ad075bSchristos 	free(l);
491c9ad075bSchristos 
492c9ad075bSchristos 	switch (data->type) {
493c9ad075bSchristos 	case WINDOW_TREE_NONE:
494c9ad075bSchristos 		break;
495c9ad075bSchristos 	case WINDOW_TREE_SESSION:
4966cb901deSkre 		*tag = (uintptr_t)data->fs.s;
497c9ad075bSchristos 		break;
498c9ad075bSchristos 	case WINDOW_TREE_WINDOW:
4996cb901deSkre 		*tag = (uintptr_t)data->fs.wl;
500c9ad075bSchristos 		break;
501c9ad075bSchristos 	case WINDOW_TREE_PANE:
502c7e17de0Schristos 		if (window_count_panes(data->fs.wl->window) == 1)
5033ef653e5Schristos 			*tag = (uintptr_t)data->fs.wl;
504c7e17de0Schristos 		else
5053ef653e5Schristos 			*tag = (uintptr_t)data->fs.wp;
506c9ad075bSchristos 		break;
507c9ad075bSchristos 	}
508c9ad075bSchristos }
509c9ad075bSchristos 
510c7e17de0Schristos static void
window_tree_draw_label(struct screen_write_ctx * ctx,u_int px,u_int py,u_int sx,u_int sy,const struct grid_cell * gc,const char * label)511c7e17de0Schristos window_tree_draw_label(struct screen_write_ctx *ctx, u_int px, u_int py,
512c7e17de0Schristos     u_int sx, u_int sy, const struct grid_cell *gc, const char *label)
513c7e17de0Schristos {
514c7e17de0Schristos 	size_t	 len;
515c7e17de0Schristos 	u_int	 ox, oy;
516c7e17de0Schristos 
517c7e17de0Schristos 	len = strlen(label);
518c7e17de0Schristos 	if (sx == 0 || sy == 1 || len > sx)
519c7e17de0Schristos 		return;
520c7e17de0Schristos 	ox = (sx - len + 1) / 2;
521c7e17de0Schristos 	oy = (sy + 1) / 2;
522c7e17de0Schristos 
523c7e17de0Schristos 	if (ox > 1 && ox + len < sx - 1 && sy >= 3) {
5240a274e86Schristos 		screen_write_cursormove(ctx, px + ox - 1, py + oy - 1, 0);
52546548964Swiz 		screen_write_box(ctx, len + 2, 3, BOX_LINES_DEFAULT, NULL,
52646548964Swiz 		    NULL);
527c7e17de0Schristos 	}
5280a274e86Schristos 	screen_write_cursormove(ctx, px + ox, py + oy, 0);
529c7e17de0Schristos 	screen_write_puts(ctx, gc, "%s", label);
530c7e17de0Schristos }
531c7e17de0Schristos 
532c9ad075bSchristos static void
window_tree_draw_session(struct window_tree_modedata * data,struct session * s,struct screen_write_ctx * ctx,u_int sx,u_int sy)533c9ad075bSchristos window_tree_draw_session(struct window_tree_modedata *data, struct session *s,
534c9ad075bSchristos     struct screen_write_ctx *ctx, u_int sx, u_int sy)
535c9ad075bSchristos {
536c9ad075bSchristos 	struct options		*oo = s->options;
537c9ad075bSchristos 	struct winlink		*wl;
538c9ad075bSchristos 	struct window		*w;
539c7e17de0Schristos 	u_int			 cx = ctx->s->cx, cy = ctx->s->cy;
540c9ad075bSchristos 	u_int			 loop, total, visible, each, width, offset;
541c9ad075bSchristos 	u_int			 current, start, end, remaining, i;
542c9ad075bSchristos 	struct grid_cell	 gc;
543c9ad075bSchristos 	int			 colour, active_colour, left, right;
544c9ad075bSchristos 	char			*label;
545c9ad075bSchristos 
546c9ad075bSchristos 	total = winlink_count(&s->windows);
547c9ad075bSchristos 
548c9ad075bSchristos 	memcpy(&gc, &grid_default_cell, sizeof gc);
549c9ad075bSchristos 	colour = options_get_number(oo, "display-panes-colour");
550c9ad075bSchristos 	active_colour = options_get_number(oo, "display-panes-active-colour");
551c9ad075bSchristos 
552c9ad075bSchristos 	if (sx / total < 24) {
553c9ad075bSchristos 		visible = sx / 24;
554c9ad075bSchristos 		if (visible == 0)
555c9ad075bSchristos 			visible = 1;
556c9ad075bSchristos 	} else
557c9ad075bSchristos 		visible = total;
558c9ad075bSchristos 
559c9ad075bSchristos 	current = 0;
560c9ad075bSchristos 	RB_FOREACH(wl, winlinks, &s->windows) {
561c9ad075bSchristos 		if (wl == s->curw)
562c9ad075bSchristos 			break;
563c9ad075bSchristos 		current++;
564c9ad075bSchristos 	}
565c9ad075bSchristos 
566c9ad075bSchristos 	if (current < visible) {
567c9ad075bSchristos 		start = 0;
568c9ad075bSchristos 		end = visible;
569c9ad075bSchristos 	} else if (current >= total - visible) {
570c9ad075bSchristos 		start = total - visible;
571c9ad075bSchristos 		end = total;
572c9ad075bSchristos 	} else {
573c9ad075bSchristos 		start = current - (visible / 2);
574c9ad075bSchristos 		end = start + visible;
575c9ad075bSchristos 	}
576c9ad075bSchristos 
577c9ad075bSchristos 	if (data->offset < -(int)start)
578c9ad075bSchristos 		data->offset = -(int)start;
579c9ad075bSchristos 	if (data->offset > (int)(total - end))
580c9ad075bSchristos 		data->offset = (int)(total - end);
581c9ad075bSchristos 	start += data->offset;
582c9ad075bSchristos 	end += data->offset;
583c9ad075bSchristos 
584c9ad075bSchristos 	left = (start != 0);
585c9ad075bSchristos 	right = (end != total);
586c9ad075bSchristos 	if (((left && right) && sx <= 6) || ((left || right) && sx <= 3))
587c9ad075bSchristos 		left = right = 0;
588c9ad075bSchristos 	if (left && right) {
589c9ad075bSchristos 		each = (sx - 6) / visible;
590c9ad075bSchristos 		remaining = (sx - 6) - (visible * each);
591c9ad075bSchristos 	} else if (left || right) {
592c9ad075bSchristos 		each = (sx - 3) / visible;
593c9ad075bSchristos 		remaining = (sx - 3) - (visible * each);
594c9ad075bSchristos 	} else {
595c9ad075bSchristos 		each = sx / visible;
596c9ad075bSchristos 		remaining = sx - (visible * each);
597c9ad075bSchristos 	}
598c9ad075bSchristos 	if (each == 0)
599c9ad075bSchristos 		return;
600c9ad075bSchristos 
601c9ad075bSchristos 	if (left) {
602c7e17de0Schristos 		data->left = cx + 2;
6030a274e86Schristos 		screen_write_cursormove(ctx, cx + 2, cy, 0);
604c9ad075bSchristos 		screen_write_vline(ctx, sy, 0, 0);
6050a274e86Schristos 		screen_write_cursormove(ctx, cx, cy + sy / 2, 0);
606c9ad075bSchristos 		screen_write_puts(ctx, &grid_default_cell, "<");
607c7e17de0Schristos 	} else
608c7e17de0Schristos 		data->left = -1;
609c9ad075bSchristos 	if (right) {
610c7e17de0Schristos 		data->right = cx + sx - 3;
6110a274e86Schristos 		screen_write_cursormove(ctx, cx + sx - 3, cy, 0);
612c9ad075bSchristos 		screen_write_vline(ctx, sy, 0, 0);
6130a274e86Schristos 		screen_write_cursormove(ctx, cx + sx - 1, cy + sy / 2, 0);
614c9ad075bSchristos 		screen_write_puts(ctx, &grid_default_cell, ">");
615c7e17de0Schristos 	} else
616c7e17de0Schristos 		data->right = -1;
617c7e17de0Schristos 
618c7e17de0Schristos 	data->start = start;
619c7e17de0Schristos 	data->end = end;
620c7e17de0Schristos 	data->each = each;
621c9ad075bSchristos 
622c9ad075bSchristos 	i = loop = 0;
623c9ad075bSchristos 	RB_FOREACH(wl, winlinks, &s->windows) {
624c9ad075bSchristos 		if (loop == end)
625c9ad075bSchristos 			break;
626c9ad075bSchristos 		if (loop < start) {
627c9ad075bSchristos 			loop++;
628c9ad075bSchristos 			continue;
629c9ad075bSchristos 		}
630c9ad075bSchristos 		w = wl->window;
631c9ad075bSchristos 
632c9ad075bSchristos 		if (wl == s->curw)
633c9ad075bSchristos 			gc.fg = active_colour;
634c9ad075bSchristos 		else
635c9ad075bSchristos 			gc.fg = colour;
636c9ad075bSchristos 
637c9ad075bSchristos 		if (left)
638c9ad075bSchristos 			offset = 3 + (i * each);
639c9ad075bSchristos 		else
640c9ad075bSchristos 			offset = (i * each);
641c9ad075bSchristos 		if (loop == end - 1)
642c9ad075bSchristos 			width = each + remaining;
643c9ad075bSchristos 		else
644c9ad075bSchristos 			width = each - 1;
645c9ad075bSchristos 
6460a274e86Schristos 		screen_write_cursormove(ctx, cx + offset, cy, 0);
647c9ad075bSchristos 		screen_write_preview(ctx, &w->active->base, width, sy);
648c9ad075bSchristos 
649c9ad075bSchristos 		xasprintf(&label, " %u:%s ", wl->idx, w->name);
650c9ad075bSchristos 		if (strlen(label) > width)
651c9ad075bSchristos 			xasprintf(&label, " %u ", wl->idx);
652c7e17de0Schristos 		window_tree_draw_label(ctx, cx + offset, cy, width, sy, &gc,
653c7e17de0Schristos 		    label);
654c9ad075bSchristos 		free(label);
655c9ad075bSchristos 
656c9ad075bSchristos 		if (loop != end - 1) {
6570a274e86Schristos 			screen_write_cursormove(ctx, cx + offset + width, cy, 0);
658c9ad075bSchristos 			screen_write_vline(ctx, sy, 0, 0);
659c9ad075bSchristos 		}
660c9ad075bSchristos 		loop++;
661c9ad075bSchristos 
662c9ad075bSchristos 		i++;
663c9ad075bSchristos 	}
664c9ad075bSchristos }
665c9ad075bSchristos 
666c9ad075bSchristos static void
window_tree_draw_window(struct window_tree_modedata * data,struct session * s,struct window * w,struct screen_write_ctx * ctx,u_int sx,u_int sy)667c9ad075bSchristos window_tree_draw_window(struct window_tree_modedata *data, struct session *s,
668c9ad075bSchristos     struct window *w, struct screen_write_ctx *ctx, u_int sx, u_int sy)
669c9ad075bSchristos {
670c9ad075bSchristos 	struct options		*oo = s->options;
671c9ad075bSchristos 	struct window_pane	*wp;
672c7e17de0Schristos 	u_int			 cx = ctx->s->cx, cy = ctx->s->cy;
673c9ad075bSchristos 	u_int			 loop, total, visible, each, width, offset;
674*f844e94eSwiz 	u_int			 current, start, end, remaining, i, pane_idx;
675c9ad075bSchristos 	struct grid_cell	 gc;
676c9ad075bSchristos 	int			 colour, active_colour, left, right;
677c9ad075bSchristos 	char			*label;
678c9ad075bSchristos 
679c9ad075bSchristos 	total = window_count_panes(w);
680c9ad075bSchristos 
681c9ad075bSchristos 	memcpy(&gc, &grid_default_cell, sizeof gc);
682c9ad075bSchristos 	colour = options_get_number(oo, "display-panes-colour");
683c9ad075bSchristos 	active_colour = options_get_number(oo, "display-panes-active-colour");
684c9ad075bSchristos 
685c9ad075bSchristos 	if (sx / total < 24) {
686c9ad075bSchristos 		visible = sx / 24;
687c9ad075bSchristos 		if (visible == 0)
688c9ad075bSchristos 			visible = 1;
689c9ad075bSchristos 	} else
690c9ad075bSchristos 		visible = total;
691c9ad075bSchristos 
692c9ad075bSchristos 	current = 0;
693c9ad075bSchristos 	TAILQ_FOREACH(wp, &w->panes, entry) {
694c9ad075bSchristos 		if (wp == w->active)
695c9ad075bSchristos 			break;
696c9ad075bSchristos 		current++;
697c9ad075bSchristos 	}
698c9ad075bSchristos 
699c9ad075bSchristos 	if (current < visible) {
700c9ad075bSchristos 		start = 0;
701c9ad075bSchristos 		end = visible;
702c9ad075bSchristos 	} else if (current >= total - visible) {
703c9ad075bSchristos 		start = total - visible;
704c9ad075bSchristos 		end = total;
705c9ad075bSchristos 	} else {
706c9ad075bSchristos 		start = current - (visible / 2);
707c9ad075bSchristos 		end = start + visible;
708c9ad075bSchristos 	}
709c9ad075bSchristos 
710c9ad075bSchristos 	if (data->offset < -(int)start)
711c9ad075bSchristos 		data->offset = -(int)start;
712c9ad075bSchristos 	if (data->offset > (int)(total - end))
713c9ad075bSchristos 		data->offset = (int)(total - end);
714c9ad075bSchristos 	start += data->offset;
715c9ad075bSchristos 	end += data->offset;
716c9ad075bSchristos 
717c9ad075bSchristos 	left = (start != 0);
718c9ad075bSchristos 	right = (end != total);
719c9ad075bSchristos 	if (((left && right) && sx <= 6) || ((left || right) && sx <= 3))
720c9ad075bSchristos 		left = right = 0;
721c9ad075bSchristos 	if (left && right) {
722c9ad075bSchristos 		each = (sx - 6) / visible;
723c9ad075bSchristos 		remaining = (sx - 6) - (visible * each);
724c9ad075bSchristos 	} else if (left || right) {
725c9ad075bSchristos 		each = (sx - 3) / visible;
726c9ad075bSchristos 		remaining = (sx - 3) - (visible * each);
727c9ad075bSchristos 	} else {
728c9ad075bSchristos 		each = sx / visible;
729c9ad075bSchristos 		remaining = sx - (visible * each);
730c9ad075bSchristos 	}
731c9ad075bSchristos 	if (each == 0)
732c9ad075bSchristos 		return;
733c9ad075bSchristos 
734c9ad075bSchristos 	if (left) {
735c7e17de0Schristos 		data->left = cx + 2;
7360a274e86Schristos 		screen_write_cursormove(ctx, cx + 2, cy, 0);
737c9ad075bSchristos 		screen_write_vline(ctx, sy, 0, 0);
7380a274e86Schristos 		screen_write_cursormove(ctx, cx, cy + sy / 2, 0);
739c9ad075bSchristos 		screen_write_puts(ctx, &grid_default_cell, "<");
740c7e17de0Schristos 	} else
741c7e17de0Schristos 		data->left = -1;
742c9ad075bSchristos 	if (right) {
743c7e17de0Schristos 		data->right = cx + sx - 3;
7440a274e86Schristos 		screen_write_cursormove(ctx, cx + sx - 3, cy, 0);
745c9ad075bSchristos 		screen_write_vline(ctx, sy, 0, 0);
7460a274e86Schristos 		screen_write_cursormove(ctx, cx + sx - 1, cy + sy / 2, 0);
747c9ad075bSchristos 		screen_write_puts(ctx, &grid_default_cell, ">");
748c7e17de0Schristos 	} else
749c7e17de0Schristos 		data->right = -1;
750c7e17de0Schristos 
751c7e17de0Schristos 	data->start = start;
752c7e17de0Schristos 	data->end = end;
753c7e17de0Schristos 	data->each = each;
754c9ad075bSchristos 
755c9ad075bSchristos 	i = loop = 0;
756c9ad075bSchristos 	TAILQ_FOREACH(wp, &w->panes, entry) {
757c9ad075bSchristos 		if (loop == end)
758c9ad075bSchristos 			break;
759c9ad075bSchristos 		if (loop < start) {
760c9ad075bSchristos 			loop++;
761c9ad075bSchristos 			continue;
762c9ad075bSchristos 		}
763c9ad075bSchristos 
764c9ad075bSchristos 		if (wp == w->active)
765c9ad075bSchristos 			gc.fg = active_colour;
766c9ad075bSchristos 		else
767c9ad075bSchristos 			gc.fg = colour;
768c9ad075bSchristos 
769c9ad075bSchristos 		if (left)
770c9ad075bSchristos 			offset = 3 + (i * each);
771c9ad075bSchristos 		else
772c9ad075bSchristos 			offset = (i * each);
773c9ad075bSchristos 		if (loop == end - 1)
774c9ad075bSchristos 			width = each + remaining;
775c9ad075bSchristos 		else
776c9ad075bSchristos 			width = each - 1;
777c9ad075bSchristos 
7780a274e86Schristos 		screen_write_cursormove(ctx, cx + offset, cy, 0);
779c9ad075bSchristos 		screen_write_preview(ctx, &wp->base, width, sy);
780c9ad075bSchristos 
781c7e17de0Schristos 		if (window_pane_index(wp, &pane_idx) != 0)
782c7e17de0Schristos 			pane_idx = loop;
783c7e17de0Schristos 		xasprintf(&label, " %u ", pane_idx);
784c7e17de0Schristos 		window_tree_draw_label(ctx, cx + offset, cy, each, sy, &gc,
785c7e17de0Schristos 		    label);
786c9ad075bSchristos 		free(label);
787c9ad075bSchristos 
788c9ad075bSchristos 		if (loop != end - 1) {
7890a274e86Schristos 			screen_write_cursormove(ctx, cx + offset + width, cy, 0);
790c9ad075bSchristos 			screen_write_vline(ctx, sy, 0, 0);
791c9ad075bSchristos 		}
792c9ad075bSchristos 		loop++;
793c9ad075bSchristos 
794c9ad075bSchristos 		i++;
795c9ad075bSchristos 	}
796c9ad075bSchristos }
797c9ad075bSchristos 
798c7e17de0Schristos static void
window_tree_draw(void * modedata,void * itemdata,struct screen_write_ctx * ctx,u_int sx,u_int sy)799c7e17de0Schristos window_tree_draw(void *modedata, void *itemdata, struct screen_write_ctx *ctx,
800c7e17de0Schristos     u_int sx, u_int sy)
801c9ad075bSchristos {
802c9ad075bSchristos 	struct window_tree_itemdata	*item = itemdata;
803c9ad075bSchristos 	struct session			*sp;
804c9ad075bSchristos 	struct winlink			*wlp;
805c9ad075bSchristos 	struct window_pane		*wp;
806c9ad075bSchristos 
807c9ad075bSchristos 	window_tree_pull_item(item, &sp, &wlp, &wp);
808c9ad075bSchristos 	if (wp == NULL)
809c7e17de0Schristos 		return;
810c9ad075bSchristos 
811c9ad075bSchristos 	switch (item->type) {
812c9ad075bSchristos 	case WINDOW_TREE_NONE:
813c7e17de0Schristos 		break;
814c9ad075bSchristos 	case WINDOW_TREE_SESSION:
815c7e17de0Schristos 		window_tree_draw_session(modedata, sp, ctx, sx, sy);
816c9ad075bSchristos 		break;
817c9ad075bSchristos 	case WINDOW_TREE_WINDOW:
818c7e17de0Schristos 		window_tree_draw_window(modedata, sp, wlp->window, ctx, sx, sy);
819c9ad075bSchristos 		break;
820c9ad075bSchristos 	case WINDOW_TREE_PANE:
821c7e17de0Schristos 		screen_write_preview(ctx, &wp->base, sx, sy);
822c9ad075bSchristos 		break;
823c9ad075bSchristos 	}
824c9ad075bSchristos }
825c9ad075bSchristos 
826c9ad075bSchristos static int
window_tree_search(__unused void * modedata,void * itemdata,const char * ss)827c9ad075bSchristos window_tree_search(__unused void *modedata, void *itemdata, const char *ss)
828c9ad075bSchristos {
829c9ad075bSchristos 	struct window_tree_itemdata	*item = itemdata;
830c9ad075bSchristos 	struct session			*s;
831c9ad075bSchristos 	struct winlink			*wl;
832c9ad075bSchristos 	struct window_pane		*wp;
83330744affSchristos 	char				*cmd;
83430744affSchristos 	int				 retval;
835c9ad075bSchristos 
836c9ad075bSchristos 	window_tree_pull_item(item, &s, &wl, &wp);
837c9ad075bSchristos 
838c9ad075bSchristos 	switch (item->type) {
839c9ad075bSchristos 	case WINDOW_TREE_NONE:
840c9ad075bSchristos 		return (0);
841c9ad075bSchristos 	case WINDOW_TREE_SESSION:
842c9ad075bSchristos 		if (s == NULL)
843c9ad075bSchristos 			return (0);
844c9ad075bSchristos 		return (strstr(s->name, ss) != NULL);
845c9ad075bSchristos 	case WINDOW_TREE_WINDOW:
846c9ad075bSchristos 		if (s == NULL || wl == NULL)
847c9ad075bSchristos 			return (0);
848c9ad075bSchristos 		return (strstr(wl->window->name, ss) != NULL);
849c9ad075bSchristos 	case WINDOW_TREE_PANE:
850c9ad075bSchristos 		if (s == NULL || wl == NULL || wp == NULL)
851c9ad075bSchristos 			break;
852c9ad075bSchristos 		cmd = osdep_get_name(wp->fd, wp->tty);
853c9ad075bSchristos 		if (cmd == NULL || *cmd == '\0')
854c9ad075bSchristos 			return (0);
85530744affSchristos 		retval = (strstr(cmd, ss) != NULL);
85630744affSchristos 		free(cmd);
857e271dbb8Schristos 		return (retval);
858c9ad075bSchristos 	}
859c9ad075bSchristos 	return (0);
860c9ad075bSchristos }
861c9ad075bSchristos 
86230744affSchristos static void
window_tree_menu(void * modedata,struct client * c,key_code key)86330744affSchristos window_tree_menu(void *modedata, struct client *c, key_code key)
86430744affSchristos {
86530744affSchristos 	struct window_tree_modedata	*data = modedata;
86630744affSchristos 	struct window_pane		*wp = data->wp;
86730744affSchristos 	struct window_mode_entry	*wme;
86830744affSchristos 
86930744affSchristos 	wme = TAILQ_FIRST(&wp->modes);
87030744affSchristos 	if (wme == NULL || wme->data != modedata)
87130744affSchristos 		return;
87230744affSchristos 	window_tree_key(wme, c, NULL, NULL, key, NULL);
87330744affSchristos }
87430744affSchristos 
875e271dbb8Schristos static key_code
window_tree_get_key(void * modedata,void * itemdata,u_int line)876e271dbb8Schristos window_tree_get_key(void *modedata, void *itemdata, u_int line)
877e271dbb8Schristos {
878e271dbb8Schristos 	struct window_tree_modedata	*data = modedata;
879e271dbb8Schristos 	struct window_tree_itemdata	*item = itemdata;
880e271dbb8Schristos 	struct format_tree		*ft;
881e271dbb8Schristos 	struct session			*s;
882e271dbb8Schristos 	struct winlink			*wl;
883e271dbb8Schristos 	struct window_pane		*wp;
884e271dbb8Schristos 	char				*expanded;
885e271dbb8Schristos 	key_code			 key;
886e271dbb8Schristos 
887e271dbb8Schristos 	ft = format_create(NULL, NULL, FORMAT_NONE, 0);
888e271dbb8Schristos 	window_tree_pull_item(item, &s, &wl, &wp);
889e271dbb8Schristos 	if (item->type == WINDOW_TREE_SESSION)
890e271dbb8Schristos 		format_defaults(ft, NULL, s, NULL, NULL);
891e271dbb8Schristos 	else if (item->type == WINDOW_TREE_WINDOW)
892e271dbb8Schristos 		format_defaults(ft, NULL, s, wl, NULL);
893e271dbb8Schristos 	else
894e271dbb8Schristos 		format_defaults(ft, NULL, s, wl, wp);
895e271dbb8Schristos 	format_add(ft, "line", "%u", line);
896e271dbb8Schristos 
897e271dbb8Schristos 	expanded = format_expand(ft, data->key_format);
898e271dbb8Schristos 	key = key_string_lookup_string(expanded);
899e271dbb8Schristos 	free(expanded);
900e271dbb8Schristos 	format_free(ft);
90146548964Swiz 	return (key);
902e271dbb8Schristos }
903e271dbb8Schristos 
904c9ad075bSchristos static struct screen *
window_tree_init(struct window_mode_entry * wme,struct cmd_find_state * fs,struct args * args)9050a274e86Schristos window_tree_init(struct window_mode_entry *wme, struct cmd_find_state *fs,
906c9ad075bSchristos     struct args *args)
907c9ad075bSchristos {
9080a274e86Schristos 	struct window_pane		*wp = wme->wp;
909c9ad075bSchristos 	struct window_tree_modedata	*data;
910c9ad075bSchristos 	struct screen			*s;
911c9ad075bSchristos 
9120a274e86Schristos 	wme->data = data = xcalloc(1, sizeof *data);
91330744affSchristos 	data->wp = wp;
91430744affSchristos 	data->references = 1;
915c9ad075bSchristos 
916c9ad075bSchristos 	if (args_has(args, 's'))
917c9ad075bSchristos 		data->type = WINDOW_TREE_SESSION;
918c9ad075bSchristos 	else if (args_has(args, 'w'))
919c9ad075bSchristos 		data->type = WINDOW_TREE_WINDOW;
920c9ad075bSchristos 	else
921c9ad075bSchristos 		data->type = WINDOW_TREE_PANE;
922c9ad075bSchristos 	memcpy(&data->fs, fs, sizeof data->fs);
923c9ad075bSchristos 
924c9ad075bSchristos 	if (args == NULL || !args_has(args, 'F'))
925c9ad075bSchristos 		data->format = xstrdup(WINDOW_TREE_DEFAULT_FORMAT);
926c9ad075bSchristos 	else
927c9ad075bSchristos 		data->format = xstrdup(args_get(args, 'F'));
928e271dbb8Schristos 	if (args == NULL || !args_has(args, 'K'))
929e271dbb8Schristos 		data->key_format = xstrdup(WINDOW_TREE_DEFAULT_KEY_FORMAT);
930e271dbb8Schristos 	else
931e271dbb8Schristos 		data->key_format = xstrdup(args_get(args, 'K'));
93246548964Swiz 	if (args == NULL || args_count(args) == 0)
933c9ad075bSchristos 		data->command = xstrdup(WINDOW_TREE_DEFAULT_COMMAND);
934c9ad075bSchristos 	else
93546548964Swiz 		data->command = xstrdup(args_string(args, 0));
936c7e17de0Schristos 	data->squash_groups = !args_has(args, 'G');
937c9ad075bSchristos 
938c9ad075bSchristos 	data->data = mode_tree_start(wp, args, window_tree_build,
939e271dbb8Schristos 	    window_tree_draw, window_tree_search, window_tree_menu, NULL,
940e271dbb8Schristos 	    window_tree_get_key, data, window_tree_menu_items,
941e271dbb8Schristos 	    window_tree_sort_list, nitems(window_tree_sort_list), &s);
942c7e17de0Schristos 	mode_tree_zoom(data->data, args);
943c9ad075bSchristos 
944c9ad075bSchristos 	mode_tree_build(data->data);
945c9ad075bSchristos 	mode_tree_draw(data->data);
946c9ad075bSchristos 
947c9ad075bSchristos 	data->type = WINDOW_TREE_NONE;
948c9ad075bSchristos 
949c9ad075bSchristos 	return (s);
950c9ad075bSchristos }
951c9ad075bSchristos 
952c9ad075bSchristos static void
window_tree_destroy(struct window_tree_modedata * data)953c9ad075bSchristos window_tree_destroy(struct window_tree_modedata *data)
954c9ad075bSchristos {
955c9ad075bSchristos 	u_int	i;
956c9ad075bSchristos 
957c9ad075bSchristos 	if (--data->references != 0)
958c9ad075bSchristos 		return;
959c9ad075bSchristos 
960c9ad075bSchristos 	for (i = 0; i < data->item_size; i++)
961c9ad075bSchristos 		window_tree_free_item(data->item_list[i]);
962c9ad075bSchristos 	free(data->item_list);
963c9ad075bSchristos 
964c9ad075bSchristos 	free(data->format);
965e271dbb8Schristos 	free(data->key_format);
966c9ad075bSchristos 	free(data->command);
967c9ad075bSchristos 
968c9ad075bSchristos 	free(data);
969c9ad075bSchristos }
970c9ad075bSchristos 
971c9ad075bSchristos static void
window_tree_free(struct window_mode_entry * wme)9720a274e86Schristos window_tree_free(struct window_mode_entry *wme)
973c9ad075bSchristos {
9740a274e86Schristos 	struct window_tree_modedata *data = wme->data;
975c9ad075bSchristos 
976c9ad075bSchristos 	if (data == NULL)
977c9ad075bSchristos 		return;
978c9ad075bSchristos 
979c9ad075bSchristos 	data->dead = 1;
980c7e17de0Schristos 	mode_tree_free(data->data);
981c9ad075bSchristos 	window_tree_destroy(data);
982c9ad075bSchristos }
983c9ad075bSchristos 
984c9ad075bSchristos static void
window_tree_resize(struct window_mode_entry * wme,u_int sx,u_int sy)9850a274e86Schristos window_tree_resize(struct window_mode_entry *wme, u_int sx, u_int sy)
986c9ad075bSchristos {
9870a274e86Schristos 	struct window_tree_modedata	*data = wme->data;
988c9ad075bSchristos 
989c9ad075bSchristos 	mode_tree_resize(data->data, sx, sy);
990c9ad075bSchristos }
991c9ad075bSchristos 
992e271dbb8Schristos static void
window_tree_update(struct window_mode_entry * wme)993e271dbb8Schristos window_tree_update(struct window_mode_entry *wme)
994e271dbb8Schristos {
995e271dbb8Schristos 	struct window_tree_modedata	*data = wme->data;
996e271dbb8Schristos 
997e271dbb8Schristos 	mode_tree_build(data->data);
998e271dbb8Schristos 	mode_tree_draw(data->data);
999e271dbb8Schristos 	data->wp->flags |= PANE_REDRAW;
1000e271dbb8Schristos }
1001e271dbb8Schristos 
1002c9ad075bSchristos static char *
window_tree_get_target(struct window_tree_itemdata * item,struct cmd_find_state * fs)1003c9ad075bSchristos window_tree_get_target(struct window_tree_itemdata *item,
1004c9ad075bSchristos     struct cmd_find_state *fs)
1005c9ad075bSchristos {
1006c9ad075bSchristos 	struct session		*s;
1007c9ad075bSchristos 	struct winlink		*wl;
1008c9ad075bSchristos 	struct window_pane	*wp;
1009c9ad075bSchristos 	char			*target;
1010c9ad075bSchristos 
1011c9ad075bSchristos 	window_tree_pull_item(item, &s, &wl, &wp);
1012c9ad075bSchristos 
1013c9ad075bSchristos 	target = NULL;
1014c9ad075bSchristos 	switch (item->type) {
1015c9ad075bSchristos 	case WINDOW_TREE_NONE:
1016c9ad075bSchristos 		break;
1017c9ad075bSchristos 	case WINDOW_TREE_SESSION:
1018c9ad075bSchristos 		if (s == NULL)
1019c9ad075bSchristos 			break;
1020c9ad075bSchristos 		xasprintf(&target, "=%s:", s->name);
1021c9ad075bSchristos 		break;
1022c9ad075bSchristos 	case WINDOW_TREE_WINDOW:
1023c9ad075bSchristos 		if (s == NULL || wl == NULL)
1024c9ad075bSchristos 			break;
1025c9ad075bSchristos 		xasprintf(&target, "=%s:%u.", s->name, wl->idx);
1026c9ad075bSchristos 		break;
1027c9ad075bSchristos 	case WINDOW_TREE_PANE:
1028c9ad075bSchristos 		if (s == NULL || wl == NULL || wp == NULL)
1029c9ad075bSchristos 			break;
1030c9ad075bSchristos 		xasprintf(&target, "=%s:%u.%%%u", s->name, wl->idx, wp->id);
1031c9ad075bSchristos 		break;
1032c9ad075bSchristos 	}
1033c9ad075bSchristos 	if (target == NULL)
1034c9ad075bSchristos 		cmd_find_clear_state(fs, 0);
1035c9ad075bSchristos 	else
1036c9ad075bSchristos 		cmd_find_from_winlink_pane(fs, wl, wp, 0);
1037c9ad075bSchristos 	return (target);
1038c9ad075bSchristos }
1039c9ad075bSchristos 
1040c9ad075bSchristos static void
window_tree_command_each(void * modedata,void * itemdata,struct client * c,__unused key_code key)1041c7e17de0Schristos window_tree_command_each(void *modedata, void *itemdata, struct client *c,
1042c7e17de0Schristos     __unused key_code key)
1043c9ad075bSchristos {
1044c9ad075bSchristos 	struct window_tree_modedata	*data = modedata;
1045c9ad075bSchristos 	struct window_tree_itemdata	*item = itemdata;
1046c9ad075bSchristos 	char				*name;
1047c9ad075bSchristos 	struct cmd_find_state		 fs;
1048c9ad075bSchristos 
1049c9ad075bSchristos 	name = window_tree_get_target(item, &fs);
1050c9ad075bSchristos 	if (name != NULL)
1051c7e17de0Schristos 		mode_tree_run_command(c, &fs, data->entered, name);
1052c9ad075bSchristos 	free(name);
1053c9ad075bSchristos }
1054c9ad075bSchristos 
1055c9ad075bSchristos static enum cmd_retval
window_tree_command_done(__unused struct cmdq_item * item,void * modedata)1056c9ad075bSchristos window_tree_command_done(__unused struct cmdq_item *item, void *modedata)
1057c9ad075bSchristos {
1058c9ad075bSchristos 	struct window_tree_modedata	*data = modedata;
1059c9ad075bSchristos 
1060c9ad075bSchristos 	if (!data->dead) {
1061c9ad075bSchristos 		mode_tree_build(data->data);
1062c9ad075bSchristos 		mode_tree_draw(data->data);
1063c9ad075bSchristos 		data->wp->flags |= PANE_REDRAW;
1064c9ad075bSchristos 	}
1065c9ad075bSchristos 	window_tree_destroy(data);
1066c9ad075bSchristos 	return (CMD_RETURN_NORMAL);
1067c9ad075bSchristos }
1068c9ad075bSchristos 
1069c9ad075bSchristos static int
window_tree_command_callback(struct client * c,void * modedata,const char * s,__unused int done)1070c9ad075bSchristos window_tree_command_callback(struct client *c, void *modedata, const char *s,
1071c9ad075bSchristos     __unused int done)
1072c9ad075bSchristos {
1073c9ad075bSchristos 	struct window_tree_modedata	*data = modedata;
1074c9ad075bSchristos 
1075c7e17de0Schristos 	if (s == NULL || *s == '\0' || data->dead)
1076c9ad075bSchristos 		return (0);
1077c9ad075bSchristos 
1078c9ad075bSchristos 	data->entered = s;
1079c7e17de0Schristos 	mode_tree_each_tagged(data->data, window_tree_command_each, c,
1080c7e17de0Schristos 	    KEYC_NONE, 1);
1081c9ad075bSchristos 	data->entered = NULL;
1082c9ad075bSchristos 
1083c9ad075bSchristos 	data->references++;
1084c9ad075bSchristos 	cmdq_append(c, cmdq_get_callback(window_tree_command_done, data));
1085c9ad075bSchristos 
1086c9ad075bSchristos 	return (0);
1087c9ad075bSchristos }
1088c9ad075bSchristos 
1089c9ad075bSchristos static void
window_tree_command_free(void * modedata)1090c9ad075bSchristos window_tree_command_free(void *modedata)
1091c9ad075bSchristos {
1092c9ad075bSchristos 	struct window_tree_modedata	*data = modedata;
1093c9ad075bSchristos 
1094c9ad075bSchristos 	window_tree_destroy(data);
1095c9ad075bSchristos }
1096c9ad075bSchristos 
1097c9ad075bSchristos static void
window_tree_kill_each(__unused void * modedata,void * itemdata,__unused struct client * c,__unused key_code key)1098c7e17de0Schristos window_tree_kill_each(__unused void *modedata, void *itemdata,
1099c7e17de0Schristos     __unused struct client *c, __unused key_code key)
1100c7e17de0Schristos {
1101c7e17de0Schristos 	struct window_tree_itemdata	*item = itemdata;
1102c7e17de0Schristos 	struct session			*s;
1103c7e17de0Schristos 	struct winlink			*wl;
1104c7e17de0Schristos 	struct window_pane		*wp;
1105c7e17de0Schristos 
1106c7e17de0Schristos 	window_tree_pull_item(item, &s, &wl, &wp);
1107c7e17de0Schristos 
1108c7e17de0Schristos 	switch (item->type) {
1109c7e17de0Schristos 	case WINDOW_TREE_NONE:
1110c7e17de0Schristos 		break;
1111c7e17de0Schristos 	case WINDOW_TREE_SESSION:
1112c7e17de0Schristos 		if (s != NULL) {
1113c7e17de0Schristos 			server_destroy_session(s);
111430744affSchristos 			session_destroy(s, 1, __func__);
1115c7e17de0Schristos 		}
1116c7e17de0Schristos 		break;
1117c7e17de0Schristos 	case WINDOW_TREE_WINDOW:
1118c7e17de0Schristos 		if (wl != NULL)
1119e271dbb8Schristos 			server_kill_window(wl->window, 0);
1120c7e17de0Schristos 		break;
1121c7e17de0Schristos 	case WINDOW_TREE_PANE:
1122c7e17de0Schristos 		if (wp != NULL)
1123c7e17de0Schristos 			server_kill_pane(wp);
1124c7e17de0Schristos 		break;
1125c7e17de0Schristos 	}
1126c7e17de0Schristos }
1127c7e17de0Schristos 
1128c7e17de0Schristos static int
window_tree_kill_current_callback(struct client * c,void * modedata,const char * s,__unused int done)1129c7e17de0Schristos window_tree_kill_current_callback(struct client *c, void *modedata,
1130c7e17de0Schristos     const char *s, __unused int done)
1131c7e17de0Schristos {
1132c7e17de0Schristos 	struct window_tree_modedata	*data = modedata;
1133c7e17de0Schristos 	struct mode_tree_data		*mtd = data->data;
1134c7e17de0Schristos 
1135c7e17de0Schristos 	if (s == NULL || *s == '\0' || data->dead)
1136c7e17de0Schristos 		return (0);
1137c7e17de0Schristos 	if (tolower((u_char) s[0]) != 'y' || s[1] != '\0')
1138c7e17de0Schristos 		return (0);
1139c7e17de0Schristos 
1140c7e17de0Schristos 	window_tree_kill_each(data, mode_tree_get_current(mtd), c, KEYC_NONE);
1141e271dbb8Schristos 	server_renumber_all();
1142c7e17de0Schristos 
1143c7e17de0Schristos 	data->references++;
1144c7e17de0Schristos 	cmdq_append(c, cmdq_get_callback(window_tree_command_done, data));
1145c7e17de0Schristos 
1146c7e17de0Schristos 	return (0);
1147c7e17de0Schristos }
1148c7e17de0Schristos 
1149c7e17de0Schristos static int
window_tree_kill_tagged_callback(struct client * c,void * modedata,const char * s,__unused int done)1150c7e17de0Schristos window_tree_kill_tagged_callback(struct client *c, void *modedata,
1151c7e17de0Schristos     const char *s, __unused int done)
1152c7e17de0Schristos {
1153c7e17de0Schristos 	struct window_tree_modedata	*data = modedata;
1154c7e17de0Schristos 	struct mode_tree_data		*mtd = data->data;
1155c7e17de0Schristos 
1156c7e17de0Schristos 	if (s == NULL || *s == '\0' || data->dead)
1157c7e17de0Schristos 		return (0);
1158c7e17de0Schristos 	if (tolower((u_char) s[0]) != 'y' || s[1] != '\0')
1159c7e17de0Schristos 		return (0);
1160c7e17de0Schristos 
1161c7e17de0Schristos 	mode_tree_each_tagged(mtd, window_tree_kill_each, c, KEYC_NONE, 1);
1162e271dbb8Schristos 	server_renumber_all();
1163c7e17de0Schristos 
1164c7e17de0Schristos 	data->references++;
1165c7e17de0Schristos 	cmdq_append(c, cmdq_get_callback(window_tree_command_done, data));
1166c7e17de0Schristos 
1167c7e17de0Schristos 	return (0);
1168c7e17de0Schristos }
1169c7e17de0Schristos 
1170c7e17de0Schristos static key_code
window_tree_mouse(struct window_tree_modedata * data,key_code key,u_int x,struct window_tree_itemdata * item)1171c7e17de0Schristos window_tree_mouse(struct window_tree_modedata *data, key_code key, u_int x,
1172c7e17de0Schristos     struct window_tree_itemdata *item)
1173c7e17de0Schristos {
1174c7e17de0Schristos 	struct session		*s;
1175c7e17de0Schristos 	struct winlink		*wl;
1176c7e17de0Schristos 	struct window_pane	*wp;
1177c7e17de0Schristos 	u_int			 loop;
1178c7e17de0Schristos 
1179c7e17de0Schristos 	if (key != KEYC_MOUSEDOWN1_PANE)
1180c7e17de0Schristos 		return (KEYC_NONE);
1181c7e17de0Schristos 
1182c7e17de0Schristos 	if (data->left != -1 && x <= (u_int)data->left)
1183c7e17de0Schristos 		return ('<');
1184c7e17de0Schristos 	if (data->right != -1 && x >= (u_int)data->right)
1185c7e17de0Schristos 		return ('>');
1186c7e17de0Schristos 
1187c7e17de0Schristos 	if (data->left != -1)
1188c7e17de0Schristos 		x -= data->left;
1189c7e17de0Schristos 	else if (x != 0)
1190c7e17de0Schristos 		x--;
1191c7e17de0Schristos 	if (x == 0 || data->end == 0)
1192c7e17de0Schristos 		x = 0;
1193c7e17de0Schristos 	else {
1194c7e17de0Schristos 		x = x / data->each;
1195c7e17de0Schristos 		if (data->start + x >= data->end)
1196c7e17de0Schristos 			x = data->end - 1;
1197c7e17de0Schristos 	}
1198c7e17de0Schristos 
1199c7e17de0Schristos 	window_tree_pull_item(item, &s, &wl, &wp);
1200c7e17de0Schristos 	if (item->type == WINDOW_TREE_SESSION) {
1201c7e17de0Schristos 		if (s == NULL)
1202c7e17de0Schristos 			return (KEYC_NONE);
1203c7e17de0Schristos 		mode_tree_expand_current(data->data);
1204c7e17de0Schristos 		loop = 0;
1205c7e17de0Schristos 		RB_FOREACH(wl, winlinks, &s->windows) {
1206c7e17de0Schristos 			if (loop == data->start + x)
1207c7e17de0Schristos 				break;
1208c7e17de0Schristos 			loop++;
1209c7e17de0Schristos 		}
1210c7e17de0Schristos 		if (wl != NULL)
12113ef653e5Schristos 			mode_tree_set_current(data->data, (uintptr_t)wl);
1212c7e17de0Schristos 		return ('\r');
1213c7e17de0Schristos 	}
1214c7e17de0Schristos 	if (item->type == WINDOW_TREE_WINDOW) {
1215c7e17de0Schristos 		if (wl == NULL)
1216c7e17de0Schristos 			return (KEYC_NONE);
1217c7e17de0Schristos 		mode_tree_expand_current(data->data);
1218c7e17de0Schristos 		loop = 0;
1219c7e17de0Schristos 		TAILQ_FOREACH(wp, &wl->window->panes, entry) {
1220c7e17de0Schristos 			if (loop == data->start + x)
1221c7e17de0Schristos 				break;
1222c7e17de0Schristos 			loop++;
1223c7e17de0Schristos 		}
1224c7e17de0Schristos 		if (wp != NULL)
12253ef653e5Schristos 			mode_tree_set_current(data->data, (uintptr_t)wp);
1226c7e17de0Schristos 		return ('\r');
1227c7e17de0Schristos 	}
1228c7e17de0Schristos 	return (KEYC_NONE);
1229c7e17de0Schristos }
1230c7e17de0Schristos 
1231c7e17de0Schristos static void
window_tree_key(struct window_mode_entry * wme,struct client * c,__unused struct session * s,__unused struct winlink * wl,key_code key,struct mouse_event * m)12320a274e86Schristos window_tree_key(struct window_mode_entry *wme, struct client *c,
12330a274e86Schristos     __unused struct session *s, __unused struct winlink *wl, key_code key,
12340a274e86Schristos     struct mouse_event *m)
1235c9ad075bSchristos {
12360a274e86Schristos 	struct window_pane		*wp = wme->wp;
12370a274e86Schristos 	struct window_tree_modedata	*data = wme->data;
1238c7e17de0Schristos 	struct window_tree_itemdata	*item, *new_item;
1239c7e17de0Schristos 	char				*name, *prompt = NULL;
1240e271dbb8Schristos 	struct cmd_find_state		 fs, *fsp = &data->fs;
1241c9ad075bSchristos 	int				 finished;
1242c7e17de0Schristos 	u_int				 tagged, x, y, idx;
1243c7e17de0Schristos 	struct session			*ns;
1244c7e17de0Schristos 	struct winlink			*nwl;
1245c7e17de0Schristos 	struct window_pane		*nwp;
1246c9ad075bSchristos 
1247c9ad075bSchristos 	item = mode_tree_get_current(data->data);
1248c7e17de0Schristos 	finished = mode_tree_key(data->data, c, &key, m, &x, &y);
1249*f844e94eSwiz 
1250*f844e94eSwiz again:
1251c7e17de0Schristos 	if (item != (new_item = mode_tree_get_current(data->data))) {
1252c7e17de0Schristos 		item = new_item;
1253c9ad075bSchristos 		data->offset = 0;
1254c7e17de0Schristos 	}
1255*f844e94eSwiz 	if (KEYC_IS_MOUSE(key) && m != NULL) {
1256c7e17de0Schristos 		key = window_tree_mouse(data, key, x, item);
1257*f844e94eSwiz 		goto again;
1258*f844e94eSwiz 	}
1259*f844e94eSwiz 
1260c9ad075bSchristos 	switch (key) {
1261c9ad075bSchristos 	case '<':
1262c9ad075bSchristos 		data->offset--;
1263c9ad075bSchristos 		break;
1264c9ad075bSchristos 	case '>':
1265c9ad075bSchristos 		data->offset++;
1266c9ad075bSchristos 		break;
1267e271dbb8Schristos 	case 'H':
1268c745c111Schristos 		mode_tree_expand(data->data, (uintptr_t)fsp->s);
1269c745c111Schristos 		mode_tree_expand(data->data, (uintptr_t)fsp->wl);
1270c745c111Schristos 		if (!mode_tree_set_current(data->data, (uintptr_t)wme->wp))
1271c745c111Schristos 			mode_tree_set_current(data->data, (uintptr_t)fsp->wl);
1272e271dbb8Schristos 		break;
1273e271dbb8Schristos 	case 'm':
1274e271dbb8Schristos 		window_tree_pull_item(item, &ns, &nwl, &nwp);
1275e271dbb8Schristos 		server_set_marked(ns, nwl, nwp);
1276e271dbb8Schristos 		mode_tree_build(data->data);
1277e271dbb8Schristos 		break;
1278e271dbb8Schristos 	case 'M':
1279e271dbb8Schristos 		server_clear_marked();
1280e271dbb8Schristos 		mode_tree_build(data->data);
1281e271dbb8Schristos 		break;
1282c7e17de0Schristos 	case 'x':
1283c7e17de0Schristos 		window_tree_pull_item(item, &ns, &nwl, &nwp);
1284c7e17de0Schristos 		switch (item->type) {
1285c7e17de0Schristos 		case WINDOW_TREE_NONE:
1286c7e17de0Schristos 			break;
1287c7e17de0Schristos 		case WINDOW_TREE_SESSION:
1288c7e17de0Schristos 			if (ns == NULL)
1289c7e17de0Schristos 				break;
1290c7e17de0Schristos 			xasprintf(&prompt, "Kill session %s? ", ns->name);
1291c7e17de0Schristos 			break;
1292c7e17de0Schristos 		case WINDOW_TREE_WINDOW:
1293c7e17de0Schristos 			if (nwl == NULL)
1294c7e17de0Schristos 				break;
1295c7e17de0Schristos 			xasprintf(&prompt, "Kill window %u? ", nwl->idx);
1296c7e17de0Schristos 			break;
1297c7e17de0Schristos 		case WINDOW_TREE_PANE:
1298c7e17de0Schristos 			if (nwp == NULL || window_pane_index(nwp, &idx) != 0)
1299c7e17de0Schristos 				break;
1300c7e17de0Schristos 			xasprintf(&prompt, "Kill pane %u? ", idx);
1301c7e17de0Schristos 			break;
1302c7e17de0Schristos 		}
1303c7e17de0Schristos 		if (prompt == NULL)
1304c7e17de0Schristos 			break;
1305c7e17de0Schristos 		data->references++;
1306e271dbb8Schristos 		status_prompt_set(c, NULL, prompt, "",
1307c7e17de0Schristos 		    window_tree_kill_current_callback, window_tree_command_free,
130846548964Swiz 		    data, PROMPT_SINGLE|PROMPT_NOFORMAT, PROMPT_TYPE_COMMAND);
1309c7e17de0Schristos 		free(prompt);
1310c7e17de0Schristos 		break;
1311c7e17de0Schristos 	case 'X':
1312c7e17de0Schristos 		tagged = mode_tree_count_tagged(data->data);
1313c7e17de0Schristos 		if (tagged == 0)
1314c7e17de0Schristos 			break;
1315c7e17de0Schristos 		xasprintf(&prompt, "Kill %u tagged? ", tagged);
1316c7e17de0Schristos 		data->references++;
1317e271dbb8Schristos 		status_prompt_set(c, NULL, prompt, "",
1318c7e17de0Schristos 		    window_tree_kill_tagged_callback, window_tree_command_free,
131946548964Swiz 		    data, PROMPT_SINGLE|PROMPT_NOFORMAT, PROMPT_TYPE_COMMAND);
1320c7e17de0Schristos 		free(prompt);
1321c7e17de0Schristos 		break;
1322c9ad075bSchristos 	case ':':
1323c9ad075bSchristos 		tagged = mode_tree_count_tagged(data->data);
1324c9ad075bSchristos 		if (tagged != 0)
1325c9ad075bSchristos 			xasprintf(&prompt, "(%u tagged) ", tagged);
1326c9ad075bSchristos 		else
1327c9ad075bSchristos 			xasprintf(&prompt, "(current) ");
1328c9ad075bSchristos 		data->references++;
1329e271dbb8Schristos 		status_prompt_set(c, NULL, prompt, "",
1330e271dbb8Schristos 		    window_tree_command_callback, window_tree_command_free,
133146548964Swiz 		    data, PROMPT_NOFORMAT, PROMPT_TYPE_COMMAND);
1332c9ad075bSchristos 		free(prompt);
1333c9ad075bSchristos 		break;
1334c9ad075bSchristos 	case '\r':
1335c9ad075bSchristos 		name = window_tree_get_target(item, &fs);
1336c9ad075bSchristos 		if (name != NULL)
1337c7e17de0Schristos 			mode_tree_run_command(c, NULL, data->command, name);
1338c7e17de0Schristos 		finished = 1;
1339c9ad075bSchristos 		free(name);
1340c7e17de0Schristos 		break;
1341c9ad075bSchristos 	}
1342c9ad075bSchristos 	if (finished)
1343c9ad075bSchristos 		window_pane_reset_mode(wp);
1344c9ad075bSchristos 	else {
1345c9ad075bSchristos 		mode_tree_draw(data->data);
1346c9ad075bSchristos 		wp->flags |= PANE_REDRAW;
1347c9ad075bSchristos 	}
1348c9ad075bSchristos }
1349