Home | History | Annotate | Line # | Download | only in dist
      1 /* $OpenBSD$ */
      2 
      3 /*
      4  * Copyright (c) 2017 Nicholas Marriott <nicholas.marriott (at) 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 
     21 #include <ctype.h>
     22 #include <stdio.h>
     23 #include <stdlib.h>
     24 #include <string.h>
     25 
     26 #include "tmux.h"
     27 
     28 enum mode_tree_search_dir {
     29 	MODE_TREE_SEARCH_FORWARD,
     30 	MODE_TREE_SEARCH_BACKWARD
     31 };
     32 
     33 enum mode_tree_preview {
     34 	MODE_TREE_PREVIEW_OFF,
     35 	MODE_TREE_PREVIEW_NORMAL,
     36 	MODE_TREE_PREVIEW_BIG
     37 };
     38 
     39 struct mode_tree_item;
     40 TAILQ_HEAD(mode_tree_list, mode_tree_item);
     41 
     42 struct mode_tree_data {
     43 	int			  dead;
     44 	u_int			  references;
     45 	int			  zoomed;
     46 
     47 	struct window_pane	 *wp;
     48 	void			 *modedata;
     49 	const struct menu_item	 *menu;
     50 
     51 	const char		**sort_list;
     52 	u_int			  sort_size;
     53 	struct mode_tree_sort_criteria sort_crit;
     54 
     55 	mode_tree_build_cb        buildcb;
     56 	mode_tree_draw_cb         drawcb;
     57 	mode_tree_search_cb       searchcb;
     58 	mode_tree_menu_cb         menucb;
     59 	mode_tree_height_cb       heightcb;
     60 	mode_tree_key_cb	  keycb;
     61 	mode_tree_swap_cb	  swapcb;
     62 
     63 	struct mode_tree_list	  children;
     64 	struct mode_tree_list	  saved;
     65 
     66 	struct mode_tree_line	 *line_list;
     67 	u_int			  line_size;
     68 
     69 	u_int			  depth;
     70 	u_int			  maxdepth;
     71 
     72 	u_int			  width;
     73 	u_int			  height;
     74 
     75 	u_int			  offset;
     76 	u_int			  current;
     77 
     78 	struct screen		  screen;
     79 
     80 	int			  preview;
     81 	char			 *search;
     82 	char			 *filter;
     83 	int			  no_matches;
     84 	enum mode_tree_search_dir search_dir;
     85 	int			  search_icase;
     86 };
     87 
     88 struct mode_tree_item {
     89 	struct mode_tree_item		*parent;
     90 	void				*itemdata;
     91 	u_int				 line;
     92 
     93 	key_code			 key;
     94 	const char			*keystr;
     95 	size_t				 keylen;
     96 
     97 	uint64_t			 tag;
     98 	const char			*name;
     99 	const char			*text;
    100 
    101 	int				 expanded;
    102 	int				 tagged;
    103 
    104 	int				 draw_as_parent;
    105 	int				 no_tag;
    106 	int				 align;
    107 
    108 	struct mode_tree_list		 children;
    109 	TAILQ_ENTRY(mode_tree_item)	 entry;
    110 };
    111 
    112 struct mode_tree_line {
    113 	struct mode_tree_item		*item;
    114 	u_int				 depth;
    115 	int				 last;
    116 	int				 flat;
    117 };
    118 
    119 struct mode_tree_menu {
    120 	struct mode_tree_data		*data;
    121 	struct client			*c;
    122 	u_int				 line;
    123 };
    124 
    125 static void mode_tree_free_items(struct mode_tree_list *);
    126 
    127 static const struct menu_item mode_tree_menu_items[] = {
    128 	{ "Scroll Left", '<', NULL },
    129 	{ "Scroll Right", '>', NULL },
    130 	{ "", KEYC_NONE, NULL },
    131 	{ "Cancel", 'q', NULL },
    132 
    133 	{ NULL, KEYC_NONE, NULL }
    134 };
    135 
    136 static int
    137 mode_tree_is_lowercase(const char *ptr)
    138 {
    139 	while (*ptr != '\0') {
    140 		if (*ptr != tolower((u_char)*ptr))
    141 			return (0);
    142 		++ptr;
    143 	}
    144 	return (1);
    145 }
    146 
    147 static struct mode_tree_item *
    148 mode_tree_find_item(struct mode_tree_list *mtl, uint64_t tag)
    149 {
    150 	struct mode_tree_item	*mti, *child;
    151 
    152 	TAILQ_FOREACH(mti, mtl, entry) {
    153 		if (mti->tag == tag)
    154 			return (mti);
    155 		child = mode_tree_find_item(&mti->children, tag);
    156 		if (child != NULL)
    157 			return (child);
    158 	}
    159 	return (NULL);
    160 }
    161 
    162 static void
    163 mode_tree_free_item(struct mode_tree_item *mti)
    164 {
    165 	mode_tree_free_items(&mti->children);
    166 
    167 	free(__UNCONST(mti->name));
    168 	free(__UNCONST(mti->text));
    169 	free(__UNCONST(mti->keystr));
    170 
    171 	free(mti);
    172 }
    173 
    174 static void
    175 mode_tree_free_items(struct mode_tree_list *mtl)
    176 {
    177 	struct mode_tree_item	*mti, *mti1;
    178 
    179 	TAILQ_FOREACH_SAFE(mti, mtl, entry, mti1) {
    180 		TAILQ_REMOVE(mtl, mti, entry);
    181 		mode_tree_free_item(mti);
    182 	}
    183 }
    184 
    185 static void
    186 mode_tree_check_selected(struct mode_tree_data *mtd)
    187 {
    188 	/*
    189 	 * If the current line would now be off screen reset the offset to the
    190 	 * last visible line.
    191 	 */
    192 	if (mtd->current > mtd->height - 1)
    193 		mtd->offset = mtd->current - mtd->height + 1;
    194 }
    195 
    196 static void
    197 mode_tree_clear_lines(struct mode_tree_data *mtd)
    198 {
    199 	free(mtd->line_list);
    200 	mtd->line_list = NULL;
    201 	mtd->line_size = 0;
    202 }
    203 
    204 static void
    205 mode_tree_build_lines(struct mode_tree_data *mtd,
    206     struct mode_tree_list *mtl, u_int depth)
    207 {
    208 	struct mode_tree_item	*mti;
    209 	struct mode_tree_line	*line;
    210 	u_int			 i;
    211 	int			 flat = 1;
    212 
    213 	mtd->depth = depth;
    214 	if (depth > mtd->maxdepth)
    215 		mtd->maxdepth = depth;
    216 	TAILQ_FOREACH(mti, mtl, entry) {
    217 		mtd->line_list = xreallocarray(mtd->line_list,
    218 		    mtd->line_size + 1, sizeof *mtd->line_list);
    219 
    220 		line = &mtd->line_list[mtd->line_size++];
    221 		line->item = mti;
    222 		line->depth = depth;
    223 		line->last = (mti == TAILQ_LAST(mtl, mode_tree_list));
    224 
    225 		mti->line = (mtd->line_size - 1);
    226 		if (!TAILQ_EMPTY(&mti->children))
    227 			flat = 0;
    228 		if (mti->expanded)
    229 			mode_tree_build_lines(mtd, &mti->children, depth + 1);
    230 
    231 		if (mtd->keycb != NULL) {
    232 			mti->key = mtd->keycb(mtd->modedata, mti->itemdata,
    233 			    mti->line);
    234 			if (mti->key == KEYC_UNKNOWN)
    235 				mti->key = KEYC_NONE;
    236 		} else if (mti->line < 10)
    237 			mti->key = '0' + mti->line;
    238 		else if (mti->line < 36)
    239 			mti->key = KEYC_META|('a' + mti->line - 10);
    240 		else
    241 			mti->key = KEYC_NONE;
    242 		if (mti->key != KEYC_NONE) {
    243 			mti->keystr = xstrdup(key_string_lookup_key(mti->key,
    244 			    0));
    245 			mti->keylen = strlen(mti->keystr);
    246 		} else {
    247 			mti->keystr = NULL;
    248 			mti->keylen = 0;
    249 		}
    250 	}
    251 	TAILQ_FOREACH(mti, mtl, entry) {
    252 		for (i = 0; i < mtd->line_size; i++) {
    253 			line = &mtd->line_list[i];
    254 			if (line->item == mti)
    255 				line->flat = flat;
    256 		}
    257 	}
    258 }
    259 
    260 static void
    261 mode_tree_clear_tagged(struct mode_tree_list *mtl)
    262 {
    263 	struct mode_tree_item	*mti;
    264 
    265 	TAILQ_FOREACH(mti, mtl, entry) {
    266 		mti->tagged = 0;
    267 		mode_tree_clear_tagged(&mti->children);
    268 	}
    269 }
    270 
    271 void
    272 mode_tree_up(struct mode_tree_data *mtd, int wrap)
    273 {
    274 	if (mtd->current == 0) {
    275 		if (wrap) {
    276 			mtd->current = mtd->line_size - 1;
    277 			if (mtd->line_size >= mtd->height)
    278 				mtd->offset = mtd->line_size - mtd->height;
    279 		}
    280 	} else {
    281 		mtd->current--;
    282 		if (mtd->current < mtd->offset)
    283 			mtd->offset--;
    284 	}
    285 }
    286 
    287 int
    288 mode_tree_down(struct mode_tree_data *mtd, int wrap)
    289 {
    290 	if (mtd->current == mtd->line_size - 1) {
    291 		if (wrap) {
    292 			mtd->current = 0;
    293 			mtd->offset = 0;
    294 		} else
    295 			return (0);
    296 	} else {
    297 		mtd->current++;
    298 		if (mtd->current > mtd->offset + mtd->height - 1)
    299 			mtd->offset++;
    300 	}
    301 	return (1);
    302 }
    303 
    304 static void
    305 mode_tree_swap(struct mode_tree_data *mtd, int direction)
    306 {
    307 	u_int	current_depth = mtd->line_list[mtd->current].depth;
    308 	u_int	swap_with, swap_with_depth;
    309 
    310 	if (mtd->swapcb == NULL)
    311 		return;
    312 
    313 	/* Find the next line at the same depth with the same parent . */
    314 	swap_with = mtd->current;
    315 	do {
    316 		if (direction < 0 && swap_with < (u_int)-direction)
    317 			return;
    318 		if (direction > 0 && swap_with + direction >= mtd->line_size)
    319 			return;
    320 		swap_with += direction;
    321 		swap_with_depth = mtd->line_list[swap_with].depth;
    322 	} while (swap_with_depth > current_depth);
    323 	if (swap_with_depth != current_depth)
    324 		return;
    325 
    326 	if (mtd->swapcb(mtd->line_list[mtd->current].item->itemdata,
    327 	    mtd->line_list[swap_with].item->itemdata)) {
    328 		mtd->current = swap_with;
    329 		mode_tree_build(mtd);
    330 	}
    331 }
    332 
    333 void *
    334 mode_tree_get_current(struct mode_tree_data *mtd)
    335 {
    336 	return (mtd->line_list[mtd->current].item->itemdata);
    337 }
    338 
    339 const char *
    340 mode_tree_get_current_name(struct mode_tree_data *mtd)
    341 {
    342 	return (mtd->line_list[mtd->current].item->name);
    343 }
    344 
    345 void
    346 mode_tree_expand_current(struct mode_tree_data *mtd)
    347 {
    348 	if (!mtd->line_list[mtd->current].item->expanded) {
    349 		mtd->line_list[mtd->current].item->expanded = 1;
    350 		mode_tree_build(mtd);
    351 	}
    352 }
    353 
    354 void
    355 mode_tree_collapse_current(struct mode_tree_data *mtd)
    356 {
    357 	if (mtd->line_list[mtd->current].item->expanded) {
    358 		mtd->line_list[mtd->current].item->expanded = 0;
    359 		mode_tree_build(mtd);
    360 	}
    361 }
    362 
    363 static int
    364 mode_tree_get_tag(struct mode_tree_data *mtd, uint64_t tag, u_int *found)
    365 {
    366 	u_int	i;
    367 
    368 	for (i = 0; i < mtd->line_size; i++) {
    369 		if (mtd->line_list[i].item->tag == tag)
    370 			break;
    371 	}
    372 	if (i != mtd->line_size) {
    373 		*found = i;
    374 		return (1);
    375 	}
    376 	return (0);
    377 }
    378 
    379 void
    380 mode_tree_expand(struct mode_tree_data *mtd, uint64_t tag)
    381 {
    382 	u_int	found;
    383 
    384 	if (!mode_tree_get_tag(mtd, tag, &found))
    385 	    return;
    386 	if (!mtd->line_list[found].item->expanded) {
    387 		mtd->line_list[found].item->expanded = 1;
    388 		mode_tree_build(mtd);
    389 	}
    390 }
    391 
    392 int
    393 mode_tree_set_current(struct mode_tree_data *mtd, uint64_t tag)
    394 {
    395 	u_int	found;
    396 
    397 	if (mode_tree_get_tag(mtd, tag, &found)) {
    398 		mtd->current = found;
    399 		if (mtd->current > mtd->height - 1)
    400 			mtd->offset = mtd->current - mtd->height + 1;
    401 		else
    402 			mtd->offset = 0;
    403 		return (1);
    404 	}
    405 	if (mtd->current >= mtd->line_size) {
    406 		mtd->current = mtd->line_size - 1;
    407 		if (mtd->current > mtd->height - 1)
    408 			mtd->offset = mtd->current - mtd->height + 1;
    409 		else
    410 			mtd->offset = 0;
    411 	}
    412 	return (0);
    413 }
    414 
    415 u_int
    416 mode_tree_count_tagged(struct mode_tree_data *mtd)
    417 {
    418 	struct mode_tree_item	*mti;
    419 	u_int			 i, tagged;
    420 
    421 	tagged = 0;
    422 	for (i = 0; i < mtd->line_size; i++) {
    423 		mti = mtd->line_list[i].item;
    424 		if (mti->tagged)
    425 			tagged++;
    426 	}
    427 	return (tagged);
    428 }
    429 
    430 void
    431 mode_tree_each_tagged(struct mode_tree_data *mtd, mode_tree_each_cb cb,
    432     struct client *c, key_code key, int current)
    433 {
    434 	struct mode_tree_item	*mti;
    435 	u_int			 i;
    436 	int			 fired;
    437 
    438 	fired = 0;
    439 	for (i = 0; i < mtd->line_size; i++) {
    440 		mti = mtd->line_list[i].item;
    441 		if (mti->tagged) {
    442 			fired = 1;
    443 			cb(mtd->modedata, mti->itemdata, c, key);
    444 		}
    445 	}
    446 	if (!fired && current) {
    447 		mti = mtd->line_list[mtd->current].item;
    448 		cb(mtd->modedata, mti->itemdata, c, key);
    449 	}
    450 }
    451 
    452 struct mode_tree_data *
    453 mode_tree_start(struct window_pane *wp, struct args *args,
    454     mode_tree_build_cb buildcb, mode_tree_draw_cb drawcb,
    455     mode_tree_search_cb searchcb, mode_tree_menu_cb menucb,
    456     mode_tree_height_cb heightcb, mode_tree_key_cb keycb,
    457     mode_tree_swap_cb swapcb, void *modedata, const struct menu_item *menu,
    458     const char **sort_list, u_int sort_size, struct screen **s)
    459 {
    460 	struct mode_tree_data	*mtd;
    461 	const char		*sort;
    462 	u_int			 i;
    463 
    464 	mtd = xcalloc(1, sizeof *mtd);
    465 	mtd->references = 1;
    466 
    467 	mtd->wp = wp;
    468 	mtd->modedata = modedata;
    469 	mtd->menu = menu;
    470 
    471 	mtd->sort_list = sort_list;
    472 	mtd->sort_size = sort_size;
    473 
    474 	if (args_has(args, 'N') > 1)
    475 		mtd->preview = MODE_TREE_PREVIEW_BIG;
    476 	else if (args_has(args, 'N'))
    477 		mtd->preview = MODE_TREE_PREVIEW_OFF;
    478 	else
    479 		mtd->preview = MODE_TREE_PREVIEW_NORMAL;
    480 
    481 	sort = args_get(args, 'O');
    482 	if (sort != NULL) {
    483 		for (i = 0; i < sort_size; i++) {
    484 			if (strcasecmp(sort, sort_list[i]) == 0)
    485 				mtd->sort_crit.field = i;
    486 		}
    487 	}
    488 	mtd->sort_crit.reversed = args_has(args, 'r');
    489 
    490 	if (args_has(args, 'f'))
    491 		mtd->filter = xstrdup(args_get(args, 'f'));
    492 	else
    493 		mtd->filter = NULL;
    494 
    495 	mtd->buildcb = buildcb;
    496 	mtd->drawcb = drawcb;
    497 	mtd->searchcb = searchcb;
    498 	mtd->menucb = menucb;
    499 	mtd->heightcb = heightcb;
    500 	mtd->keycb = keycb;
    501 	mtd->swapcb = swapcb;
    502 
    503 	TAILQ_INIT(&mtd->children);
    504 
    505 	*s = &mtd->screen;
    506 	screen_init(*s, screen_size_x(&wp->base), screen_size_y(&wp->base), 0);
    507 	(*s)->mode &= ~MODE_CURSOR;
    508 
    509 	return (mtd);
    510 }
    511 
    512 void
    513 mode_tree_zoom(struct mode_tree_data *mtd, struct args *args)
    514 {
    515 	struct window_pane	*wp = mtd->wp;
    516 
    517 	if (args_has(args, 'Z')) {
    518 		mtd->zoomed = (wp->window->flags & WINDOW_ZOOMED);
    519 		if (!mtd->zoomed && window_zoom(wp) == 0)
    520 			server_redraw_window(wp->window);
    521 	} else
    522 		mtd->zoomed = -1;
    523 }
    524 
    525 static void
    526 mode_tree_set_height(struct mode_tree_data *mtd)
    527 {
    528 	struct screen	*s = &mtd->screen;
    529 	u_int		 height;
    530 
    531 	if (mtd->heightcb != NULL) {
    532 		height = mtd->heightcb(mtd, screen_size_y(s));
    533 		if (height < screen_size_y(s))
    534 		    mtd->height = screen_size_y(s) - height;
    535 	} else {
    536 		if (mtd->preview == MODE_TREE_PREVIEW_NORMAL) {
    537 			mtd->height = (screen_size_y(s) / 3) * 2;
    538 			if (mtd->height > mtd->line_size)
    539 				mtd->height = screen_size_y(s) / 2;
    540 			if (mtd->height < 10)
    541 				mtd->height = screen_size_y(s);
    542 		} else if (mtd->preview == MODE_TREE_PREVIEW_BIG) {
    543 			mtd->height = screen_size_y(s) / 4;
    544 			if (mtd->height > mtd->line_size)
    545 				mtd->height = mtd->line_size;
    546 			if (mtd->height < 2)
    547 				mtd->height = 2;
    548 		} else
    549 			mtd->height = screen_size_y(s);
    550 	}
    551 	if (screen_size_y(s) - mtd->height < 2)
    552 		mtd->height = screen_size_y(s);
    553 }
    554 
    555 void
    556 mode_tree_build(struct mode_tree_data *mtd)
    557 {
    558 	struct screen	*s = &mtd->screen;
    559 	uint64_t	 tag;
    560 
    561 	if (mtd->line_list != NULL)
    562 		tag = mtd->line_list[mtd->current].item->tag;
    563 	else
    564 		tag = UINT64_MAX;
    565 
    566 	TAILQ_CONCAT(&mtd->saved, &mtd->children, entry);
    567 	TAILQ_INIT(&mtd->children);
    568 
    569 	mtd->buildcb(mtd->modedata, &mtd->sort_crit, &tag, mtd->filter);
    570 	mtd->no_matches = TAILQ_EMPTY(&mtd->children);
    571 	if (mtd->no_matches)
    572 		mtd->buildcb(mtd->modedata, &mtd->sort_crit, &tag, NULL);
    573 
    574 	mode_tree_free_items(&mtd->saved);
    575 	TAILQ_INIT(&mtd->saved);
    576 
    577 	mode_tree_clear_lines(mtd);
    578 	mtd->maxdepth = 0;
    579 	mode_tree_build_lines(mtd, &mtd->children, 0);
    580 
    581 	if (mtd->line_list != NULL && tag == UINT64_MAX)
    582 		tag = mtd->line_list[mtd->current].item->tag;
    583 	mode_tree_set_current(mtd, tag);
    584 
    585 	mtd->width = screen_size_x(s);
    586 	if (mtd->preview != MODE_TREE_PREVIEW_OFF)
    587 		mode_tree_set_height(mtd);
    588 	else
    589 		mtd->height = screen_size_y(s);
    590 	mode_tree_check_selected(mtd);
    591 }
    592 
    593 static void
    594 mode_tree_remove_ref(struct mode_tree_data *mtd)
    595 {
    596 	if (--mtd->references == 0)
    597 		free(mtd);
    598 }
    599 
    600 void
    601 mode_tree_free(struct mode_tree_data *mtd)
    602 {
    603 	struct window_pane	*wp = mtd->wp;
    604 
    605 	if (mtd->zoomed == 0)
    606 		server_unzoom_window(wp->window);
    607 
    608 	mode_tree_free_items(&mtd->children);
    609 	mode_tree_clear_lines(mtd);
    610 	screen_free(&mtd->screen);
    611 
    612 	free(mtd->search);
    613 	free(mtd->filter);
    614 
    615 	mtd->dead = 1;
    616 	mode_tree_remove_ref(mtd);
    617 }
    618 
    619 void
    620 mode_tree_resize(struct mode_tree_data *mtd, u_int sx, u_int sy)
    621 {
    622 	struct screen	*s = &mtd->screen;
    623 
    624 	screen_resize(s, sx, sy, 0);
    625 
    626 	mode_tree_build(mtd);
    627 	mode_tree_draw(mtd);
    628 
    629 	mtd->wp->flags |= PANE_REDRAW;
    630 }
    631 
    632 struct mode_tree_item *
    633 mode_tree_add(struct mode_tree_data *mtd, struct mode_tree_item *parent,
    634     void *itemdata, uint64_t tag, const char *name, const char *text,
    635     int expanded)
    636 {
    637 	struct mode_tree_item	*mti, *saved;
    638 
    639 	log_debug("%s: %llu, %s %s", __func__, (unsigned long long)tag,
    640 	    name, (text == NULL ? "" : text));
    641 
    642 	mti = xcalloc(1, sizeof *mti);
    643 	mti->parent = parent;
    644 	mti->itemdata = itemdata;
    645 
    646 	mti->tag = tag;
    647 	mti->name = xstrdup(name);
    648 	if (text != NULL)
    649 		mti->text = xstrdup(text);
    650 
    651 	saved = mode_tree_find_item(&mtd->saved, tag);
    652 	if (saved != NULL) {
    653 		if (parent == NULL || parent->expanded)
    654 			mti->tagged = saved->tagged;
    655 		mti->expanded = saved->expanded;
    656 	} else if (expanded == -1)
    657 		mti->expanded = 1;
    658 	else
    659 		mti->expanded = expanded;
    660 
    661 	TAILQ_INIT(&mti->children);
    662 
    663 	if (parent != NULL)
    664 		TAILQ_INSERT_TAIL(&parent->children, mti, entry);
    665 	else
    666 		TAILQ_INSERT_TAIL(&mtd->children, mti, entry);
    667 
    668 	return (mti);
    669 }
    670 
    671 void
    672 mode_tree_draw_as_parent(struct mode_tree_item *mti)
    673 {
    674 	mti->draw_as_parent = 1;
    675 }
    676 
    677 void
    678 mode_tree_no_tag(struct mode_tree_item *mti)
    679 {
    680 	mti->no_tag = 1;
    681 }
    682 
    683 /*
    684  * Set the alignment of mti->name: -1 to align left, 0 (default) to not align,
    685  * or 1 to align right.
    686  */
    687 void
    688 mode_tree_align(struct mode_tree_item *mti, int align)
    689 {
    690 	mti->align = align;
    691 }
    692 
    693 void
    694 mode_tree_remove(struct mode_tree_data *mtd, struct mode_tree_item *mti)
    695 {
    696 	struct mode_tree_item	*parent = mti->parent;
    697 
    698 	if (parent != NULL)
    699 		TAILQ_REMOVE(&parent->children, mti, entry);
    700 	else
    701 		TAILQ_REMOVE(&mtd->children, mti, entry);
    702 	mode_tree_free_item(mti);
    703 }
    704 
    705 void
    706 mode_tree_draw(struct mode_tree_data *mtd)
    707 {
    708 	struct window_pane	*wp = mtd->wp;
    709 	struct screen		*s = &mtd->screen;
    710 	struct mode_tree_line	*line;
    711 	struct mode_tree_item	*mti;
    712 	struct options		*oo = wp->window->options;
    713 	struct screen_write_ctx	 ctx;
    714 	struct grid_cell	 gc0, gc;
    715 	u_int			 w, h, i, j, sy, box_x, box_y, width;
    716 	char			*text, *start, *key;
    717 	const char		*tag, *symbol;
    718 	size_t			 size, n;
    719 	int			 keylen, pad, alignlen[mtd->maxdepth + 1];
    720 
    721 	if (mtd->line_size == 0)
    722 		return;
    723 
    724 	memcpy(&gc0, &grid_default_cell, sizeof gc0);
    725 	memcpy(&gc, &grid_default_cell, sizeof gc);
    726 	style_apply(&gc, oo, "mode-style", NULL);
    727 
    728 	w = mtd->width;
    729 	h = mtd->height;
    730 
    731 	screen_write_start(&ctx, s);
    732 	screen_write_clearscreen(&ctx, 8);
    733 
    734 	keylen = 0;
    735 	for (i = 0; i < mtd->line_size; i++) {
    736 		mti = mtd->line_list[i].item;
    737 		if (mti->key == KEYC_NONE)
    738 			continue;
    739 		if ((int)mti->keylen + 3 > keylen)
    740 			keylen = mti->keylen + 3;
    741 	}
    742 
    743 	for (i = 0; i < mtd->maxdepth + 1; i++)
    744 		alignlen[i] = 0;
    745 	for (i = 0; i < mtd->line_size; i++) {
    746 		line = &mtd->line_list[i];
    747 		mti = line->item;
    748 		if (mti->align &&
    749 		    (int)strlen(mti->name) > alignlen[line->depth])
    750 			alignlen[line->depth] = strlen(mti->name);
    751 	}
    752 
    753 	for (i = 0; i < mtd->line_size; i++) {
    754 		if (i < mtd->offset)
    755 			continue;
    756 		if (i > mtd->offset + h - 1)
    757 			break;
    758 		line = &mtd->line_list[i];
    759 		mti = line->item;
    760 
    761 		screen_write_cursormove(&ctx, 0, i - mtd->offset, 0);
    762 
    763 		pad = keylen - 2 - mti->keylen;
    764 		if (mti->key != KEYC_NONE)
    765 			xasprintf(&key, "(%s)%*s", mti->keystr, pad, "");
    766 		else
    767 			key = xstrdup("");
    768 
    769 		if (line->flat)
    770 			symbol = "";
    771 		else if (TAILQ_EMPTY(&mti->children))
    772 			symbol = "  ";
    773 		else if (mti->expanded)
    774 			symbol = "- ";
    775 		else
    776 			symbol = "+ ";
    777 
    778 		if (line->depth == 0)
    779 			start = xstrdup(symbol);
    780 		else {
    781 			size = (4 * line->depth) + 32;
    782 
    783 			start = xcalloc(1, size);
    784 			for (j = 1; j < line->depth; j++) {
    785 				if (mti->parent != NULL &&
    786 				    mtd->line_list[mti->parent->line].last)
    787 					strlcat(start, "    ", size);
    788 				else
    789 					strlcat(start, "\001x\001   ", size);
    790 			}
    791 			if (line->last)
    792 				strlcat(start, "\001mq\001> ", size);
    793 			else
    794 				strlcat(start, "\001tq\001> ", size);
    795 			strlcat(start, symbol, size);
    796 		}
    797 
    798 		if (mti->tagged)
    799 			tag = "*";
    800 		else
    801 			tag = "";
    802 		xasprintf(&text, "%-*s%s%*s%s%s", keylen, key, start,
    803 		    mti->align * alignlen[line->depth], mti->name, tag,
    804 		    (mti->text != NULL) ? ": " : "" );
    805 		width = utf8_cstrwidth(text);
    806 		if (width > w)
    807 			width = w;
    808 		free(start);
    809 
    810 		if (mti->tagged) {
    811 			gc.attr ^= GRID_ATTR_BRIGHT;
    812 			gc0.attr ^= GRID_ATTR_BRIGHT;
    813 		}
    814 
    815 		if (i != mtd->current) {
    816 			screen_write_clearendofline(&ctx, 8);
    817 			screen_write_nputs(&ctx, w, &gc0, "%s", text);
    818 			if (mti->text != NULL) {
    819 				format_draw(&ctx, &gc0, w - width, mti->text,
    820 				    NULL, 0);
    821 			}
    822 		} else {
    823 			screen_write_clearendofline(&ctx, gc.bg);
    824 			screen_write_nputs(&ctx, w, &gc, "%s", text);
    825 			if (mti->text != NULL) {
    826 				format_draw(&ctx, &gc, w - width, mti->text,
    827 				    NULL, 0);
    828 			}
    829 		}
    830 		free(text);
    831 		free(key);
    832 
    833 		if (mti->tagged) {
    834 			gc.attr ^= GRID_ATTR_BRIGHT;
    835 			gc0.attr ^= GRID_ATTR_BRIGHT;
    836 		}
    837 	}
    838 
    839 	if (mtd->preview == MODE_TREE_PREVIEW_OFF)
    840 		goto done;
    841 
    842 	sy = screen_size_y(s);
    843 	if (sy <= 4 || h < 2 || sy - h <= 4 || w <= 4)
    844 		goto done;
    845 
    846 	line = &mtd->line_list[mtd->current];
    847 	mti = line->item;
    848 	if (mti->draw_as_parent)
    849 		mti = mti->parent;
    850 
    851 	screen_write_cursormove(&ctx, 0, h, 0);
    852 	screen_write_box(&ctx, w, sy - h, BOX_LINES_DEFAULT, NULL, NULL);
    853 
    854 	if (mtd->sort_list != NULL) {
    855 		xasprintf(&text, " %s (sort: %s%s)", mti->name,
    856 		    mtd->sort_list[mtd->sort_crit.field],
    857 		    mtd->sort_crit.reversed ? ", reversed" : "");
    858 	} else
    859 		xasprintf(&text, " %s", mti->name);
    860 	if (w - 2 >= strlen(text)) {
    861 		screen_write_cursormove(&ctx, 1, h, 0);
    862 		screen_write_puts(&ctx, &gc0, "%s", text);
    863 
    864 		if (mtd->no_matches)
    865 			n = (sizeof "no matches") - 1;
    866 		else
    867 			n = (sizeof "active") - 1;
    868 		if (mtd->filter != NULL && w - 2 >= strlen(text) + 10 + n + 2) {
    869 			screen_write_puts(&ctx, &gc0, " (filter: ");
    870 			if (mtd->no_matches)
    871 				screen_write_puts(&ctx, &gc, "no matches");
    872 			else
    873 				screen_write_puts(&ctx, &gc0, "active");
    874 			screen_write_puts(&ctx, &gc0, ") ");
    875 		} else
    876 			screen_write_puts(&ctx, &gc0, " ");
    877 	}
    878 	free(text);
    879 
    880 	box_x = w - 4;
    881 	box_y = sy - h - 2;
    882 
    883 	if (box_x != 0 && box_y != 0) {
    884 		screen_write_cursormove(&ctx, 2, h + 1, 0);
    885 		mtd->drawcb(mtd->modedata, mti->itemdata, &ctx, box_x, box_y);
    886 	}
    887 
    888 done:
    889 	screen_write_cursormove(&ctx, 0, mtd->current - mtd->offset, 0);
    890 	screen_write_stop(&ctx);
    891 }
    892 
    893 static struct mode_tree_item *
    894 mode_tree_search_backward(struct mode_tree_data *mtd)
    895 {
    896 	struct mode_tree_item	*mti, *last, *prev;
    897 	int			 icase = mtd->search_icase;
    898 
    899 	if (mtd->search == NULL)
    900 		return (NULL);
    901 
    902 	mti = last = mtd->line_list[mtd->current].item;
    903 	for (;;) {
    904 		if ((prev = TAILQ_PREV(mti, mode_tree_list, entry)) != NULL) {
    905 			/* Point to the last child in the previous subtree. */
    906 			while (!TAILQ_EMPTY(&prev->children)) {
    907 				prev = TAILQ_LAST(&prev->children,
    908 				    mode_tree_list);
    909 			}
    910 			mti = prev;
    911 		} else {
    912 			/* If prev is NULL, jump to the parent. */
    913 			mti = mti->parent;
    914 		}
    915 
    916 		if (mti == NULL) {
    917 			/* Point to the last child in the last root subtree. */
    918 			prev = TAILQ_LAST(&mtd->children, mode_tree_list);
    919 			while (!TAILQ_EMPTY(&prev->children)) {
    920 				prev = TAILQ_LAST(&prev->children,
    921 				    mode_tree_list);
    922 			}
    923 			mti = prev;
    924 		}
    925 		if (mti == last)
    926 			break;
    927 
    928 		if (mtd->searchcb == NULL) {
    929 			if (!icase && strstr(mti->name, mtd->search) != NULL)
    930 				return (mti);
    931 			if (icase && strcasestr(mti->name, mtd->search) != NULL)
    932 				return (mti);
    933 			continue;
    934 		}
    935 		if (mtd->searchcb(mtd->modedata, mti->itemdata, mtd->search,
    936 		    icase))
    937 			return (mti);
    938 	}
    939 	return (NULL);
    940 }
    941 
    942 static struct mode_tree_item *
    943 mode_tree_search_forward(struct mode_tree_data *mtd)
    944 {
    945 	struct mode_tree_item	*mti, *last, *next;
    946 	int			 icase = mtd->search_icase;
    947 
    948 	if (mtd->search == NULL)
    949 		return (NULL);
    950 
    951 	mti = last = mtd->line_list[mtd->current].item;
    952 	for (;;) {
    953 		if (!TAILQ_EMPTY(&mti->children))
    954 			mti = TAILQ_FIRST(&mti->children);
    955 		else if ((next = TAILQ_NEXT(mti, entry)) != NULL)
    956 			mti = next;
    957 		else {
    958 			for (;;) {
    959 				mti = mti->parent;
    960 				if (mti == NULL)
    961 					break;
    962 				if ((next = TAILQ_NEXT(mti, entry)) != NULL) {
    963 					mti = next;
    964 					break;
    965 				}
    966 			}
    967 		}
    968 		if (mti == NULL)
    969 			mti = TAILQ_FIRST(&mtd->children);
    970 		if (mti == last)
    971 			break;
    972 
    973 		if (mtd->searchcb == NULL) {
    974 			if (!icase && strstr(mti->name, mtd->search) != NULL)
    975 				return (mti);
    976 			if (icase && strcasestr(mti->name, mtd->search) != NULL)
    977 				return (mti);
    978 			continue;
    979 		}
    980 		if (mtd->searchcb(mtd->modedata, mti->itemdata, mtd->search,
    981 		    icase))
    982 			return (mti);
    983 	}
    984 	return (NULL);
    985 }
    986 
    987 static void
    988 mode_tree_search_set(struct mode_tree_data *mtd)
    989 {
    990 	struct mode_tree_item	*mti, *loop;
    991 	uint64_t		 tag;
    992 
    993 	if (mtd->search_dir == MODE_TREE_SEARCH_FORWARD)
    994 		mti = mode_tree_search_forward(mtd);
    995 	else
    996 		mti = mode_tree_search_backward(mtd);
    997 	if (mti == NULL)
    998 		return;
    999 	tag = mti->tag;
   1000 
   1001 	loop = mti->parent;
   1002 	while (loop != NULL) {
   1003 		loop->expanded = 1;
   1004 		loop = loop->parent;
   1005 	}
   1006 
   1007 	mode_tree_build(mtd);
   1008 	mode_tree_set_current(mtd, tag);
   1009 	mode_tree_draw(mtd);
   1010 	mtd->wp->flags |= PANE_REDRAW;
   1011 }
   1012 
   1013 static int
   1014 mode_tree_search_callback(__unused struct client *c, void *data, const char *s,
   1015     __unused int done)
   1016 {
   1017 	struct mode_tree_data	*mtd = data;
   1018 
   1019 	if (mtd->dead)
   1020 		return (0);
   1021 
   1022 	free(mtd->search);
   1023 	if (s == NULL || *s == '\0') {
   1024 		mtd->search = NULL;
   1025 		return (0);
   1026 	}
   1027 	mtd->search = xstrdup(s);
   1028 	mtd->search_icase = mode_tree_is_lowercase(s);
   1029 	mode_tree_search_set(mtd);
   1030 
   1031 	return (0);
   1032 }
   1033 
   1034 static void
   1035 mode_tree_search_free(void *data)
   1036 {
   1037 	mode_tree_remove_ref(data);
   1038 }
   1039 
   1040 static int
   1041 mode_tree_filter_callback(__unused struct client *c, void *data, const char *s,
   1042     __unused int done)
   1043 {
   1044 	struct mode_tree_data	*mtd = data;
   1045 
   1046 	if (mtd->dead)
   1047 		return (0);
   1048 
   1049 	if (mtd->filter != NULL)
   1050 		free(mtd->filter);
   1051 	if (s == NULL || *s == '\0')
   1052 		mtd->filter = NULL;
   1053 	else
   1054 		mtd->filter = xstrdup(s);
   1055 
   1056 	mode_tree_build(mtd);
   1057 	mode_tree_draw(mtd);
   1058 	mtd->wp->flags |= PANE_REDRAW;
   1059 
   1060 	return (0);
   1061 }
   1062 
   1063 static void
   1064 mode_tree_filter_free(void *data)
   1065 {
   1066 	mode_tree_remove_ref(data);
   1067 }
   1068 
   1069 static void
   1070 mode_tree_menu_callback(__unused struct menu *menu, __unused u_int idx,
   1071     key_code key, void *data)
   1072 {
   1073 	struct mode_tree_menu	*mtm = data;
   1074 	struct mode_tree_data	*mtd = mtm->data;
   1075 
   1076 	if (mtd->dead || key == KEYC_NONE)
   1077 		goto out;
   1078 
   1079 	if (mtm->line >= mtd->line_size)
   1080 		goto out;
   1081 	mtd->current = mtm->line;
   1082 	mtd->menucb(mtd->modedata, mtm->c, key);
   1083 
   1084 out:
   1085 	mode_tree_remove_ref(mtd);
   1086 	free(mtm);
   1087 }
   1088 
   1089 static void
   1090 mode_tree_display_menu(struct mode_tree_data *mtd, struct client *c, u_int x,
   1091     u_int y, int outside)
   1092 {
   1093 	struct mode_tree_item	*mti;
   1094 	struct menu		*menu;
   1095 	const struct menu_item	*items;
   1096 	struct mode_tree_menu	*mtm;
   1097 	char			*title;
   1098 	u_int			 line;
   1099 
   1100 	if (mtd->offset + y > mtd->line_size - 1)
   1101 		line = mtd->current;
   1102 	else
   1103 		line = mtd->offset + y;
   1104 	mti = mtd->line_list[line].item;
   1105 
   1106 	if (!outside) {
   1107 		items = mtd->menu;
   1108 		xasprintf(&title, "#[align=centre]%s", mti->name);
   1109 	} else {
   1110 		items = mode_tree_menu_items;
   1111 		title = xstrdup("");
   1112 	}
   1113 	menu = menu_create(title);
   1114 	menu_add_items(menu, items, NULL, c, NULL);
   1115 	free(title);
   1116 
   1117 	mtm = xmalloc(sizeof *mtm);
   1118 	mtm->data = mtd;
   1119 	mtm->c = c;
   1120 	mtm->line = line;
   1121 	mtd->references++;
   1122 
   1123 	if (x >= (menu->width + 4) / 2)
   1124 		x -= (menu->width + 4) / 2;
   1125 	else
   1126 		x = 0;
   1127 	if (menu_display(menu, 0, 0, NULL, x, y, c, BOX_LINES_DEFAULT, NULL,
   1128 	    NULL, NULL, NULL, mode_tree_menu_callback, mtm) != 0) {
   1129 		mode_tree_remove_ref(mtd);
   1130 		free(mtm);
   1131 		menu_free(menu);
   1132 	}
   1133 }
   1134 
   1135 int
   1136 mode_tree_key(struct mode_tree_data *mtd, struct client *c, key_code *key,
   1137     struct mouse_event *m, u_int *xp, u_int *yp)
   1138 {
   1139 	struct mode_tree_line	*line;
   1140 	struct mode_tree_item	*current, *parent, *mti;
   1141 	u_int			 i, x, y;
   1142 	int			 choice;
   1143 
   1144 	if (KEYC_IS_MOUSE(*key) && m != NULL) {
   1145 		if (cmd_mouse_at(mtd->wp, m, &x, &y, 0) != 0) {
   1146 			*key = KEYC_NONE;
   1147 			return (0);
   1148 		}
   1149 		if (xp != NULL)
   1150 			*xp = x;
   1151 		if (yp != NULL)
   1152 			*yp = y;
   1153 		if (x > mtd->width || y > mtd->height) {
   1154 			if (*key == KEYC_MOUSEDOWN3_PANE)
   1155 				mode_tree_display_menu(mtd, c, x, y, 1);
   1156 			if (mtd->preview == MODE_TREE_PREVIEW_OFF)
   1157 				*key = KEYC_NONE;
   1158 			return (0);
   1159 		}
   1160 		if (mtd->offset + y < mtd->line_size) {
   1161 			if (*key == KEYC_MOUSEDOWN1_PANE ||
   1162 			    *key == KEYC_MOUSEDOWN3_PANE ||
   1163 			    *key == KEYC_DOUBLECLICK1_PANE)
   1164 				mtd->current = mtd->offset + y;
   1165 			if (*key == KEYC_DOUBLECLICK1_PANE)
   1166 				*key = '\r';
   1167 			else {
   1168 				if (*key == KEYC_MOUSEDOWN3_PANE)
   1169 					mode_tree_display_menu(mtd, c, x, y, 0);
   1170 				*key = KEYC_NONE;
   1171 			}
   1172 		} else {
   1173 			if (*key == KEYC_MOUSEDOWN3_PANE)
   1174 				mode_tree_display_menu(mtd, c, x, y, 0);
   1175 			*key = KEYC_NONE;
   1176 		}
   1177 		return (0);
   1178 	}
   1179 
   1180 	line = &mtd->line_list[mtd->current];
   1181 	current = line->item;
   1182 
   1183 	choice = -1;
   1184 	for (i = 0; i < mtd->line_size; i++) {
   1185 		if (*key == mtd->line_list[i].item->key) {
   1186 			choice = i;
   1187 			break;
   1188 		}
   1189 	}
   1190 	if (choice != -1) {
   1191 		if ((u_int)choice > mtd->line_size - 1) {
   1192 			*key = KEYC_NONE;
   1193 			return (0);
   1194 		}
   1195 		mtd->current = choice;
   1196 		*key = '\r';
   1197 		return (0);
   1198 	}
   1199 
   1200 	switch (*key) {
   1201 	case 'q':
   1202 	case '\033': /* Escape */
   1203 	case 'g'|KEYC_CTRL:
   1204 		return (1);
   1205 	case KEYC_UP:
   1206 	case 'k':
   1207 	case KEYC_WHEELUP_PANE:
   1208 	case 'p'|KEYC_CTRL:
   1209 		mode_tree_up(mtd, 1);
   1210 		break;
   1211 	case KEYC_DOWN:
   1212 	case 'j':
   1213 	case KEYC_WHEELDOWN_PANE:
   1214 	case 'n'|KEYC_CTRL:
   1215 		mode_tree_down(mtd, 1);
   1216 		break;
   1217 	case KEYC_UP|KEYC_SHIFT:
   1218 	case 'K':
   1219 		mode_tree_swap(mtd, -1);
   1220 		break;
   1221 	case KEYC_DOWN|KEYC_SHIFT:
   1222 	case 'J':
   1223 		mode_tree_swap(mtd, 1);
   1224 		break;
   1225 	case KEYC_PPAGE:
   1226 	case 'b'|KEYC_CTRL:
   1227 		for (i = 0; i < mtd->height; i++) {
   1228 			if (mtd->current == 0)
   1229 				break;
   1230 			mode_tree_up(mtd, 1);
   1231 		}
   1232 		break;
   1233 	case KEYC_NPAGE:
   1234 	case 'f'|KEYC_CTRL:
   1235 		for (i = 0; i < mtd->height; i++) {
   1236 			if (mtd->current == mtd->line_size - 1)
   1237 				break;
   1238 			mode_tree_down(mtd, 1);
   1239 		}
   1240 		break;
   1241 	case 'g':
   1242 	case KEYC_HOME:
   1243 		mtd->current = 0;
   1244 		mtd->offset = 0;
   1245 		break;
   1246 	case 'G':
   1247 	case KEYC_END:
   1248 		mtd->current = mtd->line_size - 1;
   1249 		if (mtd->current > mtd->height - 1)
   1250 			mtd->offset = mtd->current - mtd->height + 1;
   1251 		else
   1252 			mtd->offset = 0;
   1253 		break;
   1254 	case 't':
   1255 		/*
   1256 		 * Do not allow parents and children to both be tagged: untag
   1257 		 * all parents and children of current.
   1258 		 */
   1259 		if (current->no_tag)
   1260 			break;
   1261 		if (!current->tagged) {
   1262 			parent = current->parent;
   1263 			while (parent != NULL) {
   1264 				parent->tagged = 0;
   1265 				parent = parent->parent;
   1266 			}
   1267 			mode_tree_clear_tagged(&current->children);
   1268 			current->tagged = 1;
   1269 		} else
   1270 			current->tagged = 0;
   1271 		if (m != NULL)
   1272 			mode_tree_down(mtd, 0);
   1273 		break;
   1274 	case 'T':
   1275 		for (i = 0; i < mtd->line_size; i++)
   1276 			mtd->line_list[i].item->tagged = 0;
   1277 		break;
   1278 	case 't'|KEYC_CTRL:
   1279 		for (i = 0; i < mtd->line_size; i++) {
   1280 			if ((mtd->line_list[i].item->parent == NULL &&
   1281 			    !mtd->line_list[i].item->no_tag) ||
   1282 			    (mtd->line_list[i].item->parent != NULL &&
   1283 			    mtd->line_list[i].item->parent->no_tag))
   1284 				mtd->line_list[i].item->tagged = 1;
   1285 			else
   1286 				mtd->line_list[i].item->tagged = 0;
   1287 		}
   1288 		break;
   1289 	case 'O':
   1290 		mtd->sort_crit.field++;
   1291 		if (mtd->sort_crit.field >= mtd->sort_size)
   1292 			mtd->sort_crit.field = 0;
   1293 		mode_tree_build(mtd);
   1294 		break;
   1295 	case 'r':
   1296 		mtd->sort_crit.reversed = !mtd->sort_crit.reversed;
   1297 		mode_tree_build(mtd);
   1298 		break;
   1299 	case KEYC_LEFT:
   1300 	case 'h':
   1301 	case '-':
   1302 		if (line->flat || !current->expanded)
   1303 			current = current->parent;
   1304 		if (current == NULL)
   1305 			mode_tree_up(mtd, 0);
   1306 		else {
   1307 			current->expanded = 0;
   1308 			mtd->current = current->line;
   1309 			mode_tree_build(mtd);
   1310 		}
   1311 		break;
   1312 	case KEYC_RIGHT:
   1313 	case 'l':
   1314 	case '+':
   1315 		if (line->flat || current->expanded)
   1316 			mode_tree_down(mtd, 0);
   1317 		else if (!line->flat) {
   1318 			current->expanded = 1;
   1319 			mode_tree_build(mtd);
   1320 		}
   1321 		break;
   1322 	case '-'|KEYC_META:
   1323 		TAILQ_FOREACH(mti, &mtd->children, entry)
   1324 			mti->expanded = 0;
   1325 		mode_tree_build(mtd);
   1326 		break;
   1327 	case '+'|KEYC_META:
   1328 		TAILQ_FOREACH(mti, &mtd->children, entry)
   1329 			mti->expanded = 1;
   1330 		mode_tree_build(mtd);
   1331 		break;
   1332 	case '?':
   1333 	case '/':
   1334 	case 's'|KEYC_CTRL:
   1335 		mtd->references++;
   1336 		mtd->search_dir = MODE_TREE_SEARCH_FORWARD;
   1337 		status_prompt_set(c, NULL, "(search) ", "",
   1338 		    mode_tree_search_callback, mode_tree_search_free, mtd,
   1339 		    PROMPT_NOFORMAT, PROMPT_TYPE_SEARCH);
   1340 		break;
   1341 	case 'n':
   1342 		mtd->search_dir = MODE_TREE_SEARCH_FORWARD;
   1343 		mode_tree_search_set(mtd);
   1344 		break;
   1345 	case 'N':
   1346 		mtd->search_dir = MODE_TREE_SEARCH_BACKWARD;
   1347 		mode_tree_search_set(mtd);
   1348 		break;
   1349 	case 'f':
   1350 		mtd->references++;
   1351 		status_prompt_set(c, NULL, "(filter) ", mtd->filter,
   1352 		    mode_tree_filter_callback, mode_tree_filter_free, mtd,
   1353 		    PROMPT_NOFORMAT, PROMPT_TYPE_SEARCH);
   1354 		break;
   1355 	case 'v':
   1356 		switch (mtd->preview) {
   1357 		case MODE_TREE_PREVIEW_OFF:
   1358 			mtd->preview = MODE_TREE_PREVIEW_BIG;
   1359 			break;
   1360 		case MODE_TREE_PREVIEW_NORMAL:
   1361 			mtd->preview = MODE_TREE_PREVIEW_OFF;
   1362 			break;
   1363 		case MODE_TREE_PREVIEW_BIG:
   1364 			mtd->preview = MODE_TREE_PREVIEW_NORMAL;
   1365 			break;
   1366 		}
   1367 		mode_tree_build(mtd);
   1368 		if (mtd->preview != MODE_TREE_PREVIEW_OFF)
   1369 			mode_tree_check_selected(mtd);
   1370 		break;
   1371 	}
   1372 	return (0);
   1373 }
   1374 
   1375 void
   1376 mode_tree_run_command(struct client *c, struct cmd_find_state *fs,
   1377     const char *template, const char *name)
   1378 {
   1379 	struct cmdq_state	*state;
   1380 	char			*command, *error;
   1381 	enum cmd_parse_status	 status;
   1382 
   1383 	command = cmd_template_replace(template, name, 1);
   1384 	if (command != NULL && *command != '\0') {
   1385 		state = cmdq_new_state(fs, NULL, 0);
   1386 		status = cmd_parse_and_append(command, NULL, c, state, &error);
   1387 		if (status == CMD_PARSE_ERROR) {
   1388 			if (c != NULL) {
   1389 				*error = toupper((u_char)*error);
   1390 				status_message_set(c, -1, 1, 0, 0, "%s", error);
   1391 			}
   1392 			free(error);
   1393 		}
   1394 		cmdq_free_state(state);
   1395 	}
   1396 	free(command);
   1397 }
   1398