Merge branch 'obsd-master'
[tmux.git] / mode-tree.c
blobff12b54f9934682fa301a7e2d9a52dc56357f52f
1 /* $OpenBSD$ */
3 /*
4 * Copyright (c) 2017 Nicholas Marriott <nicholas.marriott@gmail.com>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include <sys/types.h>
21 #include <ctype.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
26 #include "tmux.h"
28 enum mode_tree_search_dir {
29 MODE_TREE_SEARCH_FORWARD,
30 MODE_TREE_SEARCH_BACKWARD
33 enum mode_tree_preview {
34 MODE_TREE_PREVIEW_OFF,
35 MODE_TREE_PREVIEW_NORMAL,
36 MODE_TREE_PREVIEW_BIG
39 struct mode_tree_item;
40 TAILQ_HEAD(mode_tree_list, mode_tree_item);
42 struct mode_tree_data {
43 int dead;
44 u_int references;
45 int zoomed;
47 struct window_pane *wp;
48 void *modedata;
49 const struct menu_item *menu;
51 const char **sort_list;
52 u_int sort_size;
53 struct mode_tree_sort_criteria sort_crit;
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;
62 struct mode_tree_list children;
63 struct mode_tree_list saved;
65 struct mode_tree_line *line_list;
66 u_int line_size;
68 u_int depth;
70 u_int width;
71 u_int height;
73 u_int offset;
74 u_int current;
76 struct screen screen;
78 int preview;
79 char *search;
80 char *filter;
81 int no_matches;
82 enum mode_tree_search_dir search_dir;
85 struct mode_tree_item {
86 struct mode_tree_item *parent;
87 void *itemdata;
88 u_int line;
90 key_code key;
91 const char *keystr;
92 size_t keylen;
94 uint64_t tag;
95 const char *name;
96 const char *text;
98 int expanded;
99 int tagged;
101 int draw_as_parent;
102 int no_tag;
104 struct mode_tree_list children;
105 TAILQ_ENTRY(mode_tree_item) entry;
108 struct mode_tree_line {
109 struct mode_tree_item *item;
110 u_int depth;
111 int last;
112 int flat;
115 struct mode_tree_menu {
116 struct mode_tree_data *data;
117 struct client *c;
118 u_int line;
121 static void mode_tree_free_items(struct mode_tree_list *);
123 static const struct menu_item mode_tree_menu_items[] = {
124 { "Scroll Left", '<', NULL },
125 { "Scroll Right", '>', NULL },
126 { "", KEYC_NONE, NULL },
127 { "Cancel", 'q', NULL },
129 { NULL, KEYC_NONE, NULL }
132 static struct mode_tree_item *
133 mode_tree_find_item(struct mode_tree_list *mtl, uint64_t tag)
135 struct mode_tree_item *mti, *child;
137 TAILQ_FOREACH(mti, mtl, entry) {
138 if (mti->tag == tag)
139 return (mti);
140 child = mode_tree_find_item(&mti->children, tag);
141 if (child != NULL)
142 return (child);
144 return (NULL);
147 static void
148 mode_tree_free_item(struct mode_tree_item *mti)
150 mode_tree_free_items(&mti->children);
152 free((void *)mti->name);
153 free((void *)mti->text);
154 free((void *)mti->keystr);
156 free(mti);
159 static void
160 mode_tree_free_items(struct mode_tree_list *mtl)
162 struct mode_tree_item *mti, *mti1;
164 TAILQ_FOREACH_SAFE(mti, mtl, entry, mti1) {
165 TAILQ_REMOVE(mtl, mti, entry);
166 mode_tree_free_item(mti);
170 static void
171 mode_tree_check_selected(struct mode_tree_data *mtd)
174 * If the current line would now be off screen reset the offset to the
175 * last visible line.
177 if (mtd->current > mtd->height - 1)
178 mtd->offset = mtd->current - mtd->height + 1;
181 static void
182 mode_tree_clear_lines(struct mode_tree_data *mtd)
184 free(mtd->line_list);
185 mtd->line_list = NULL;
186 mtd->line_size = 0;
189 static void
190 mode_tree_build_lines(struct mode_tree_data *mtd,
191 struct mode_tree_list *mtl, u_int depth)
193 struct mode_tree_item *mti;
194 struct mode_tree_line *line;
195 u_int i;
196 int flat = 1;
198 mtd->depth = depth;
199 TAILQ_FOREACH(mti, mtl, entry) {
200 mtd->line_list = xreallocarray(mtd->line_list,
201 mtd->line_size + 1, sizeof *mtd->line_list);
203 line = &mtd->line_list[mtd->line_size++];
204 line->item = mti;
205 line->depth = depth;
206 line->last = (mti == TAILQ_LAST(mtl, mode_tree_list));
208 mti->line = (mtd->line_size - 1);
209 if (!TAILQ_EMPTY(&mti->children))
210 flat = 0;
211 if (mti->expanded)
212 mode_tree_build_lines(mtd, &mti->children, depth + 1);
214 if (mtd->keycb != NULL) {
215 mti->key = mtd->keycb(mtd->modedata, mti->itemdata,
216 mti->line);
217 if (mti->key == KEYC_UNKNOWN)
218 mti->key = KEYC_NONE;
219 } else if (mti->line < 10)
220 mti->key = '0' + mti->line;
221 else if (mti->line < 36)
222 mti->key = KEYC_META|('a' + mti->line - 10);
223 else
224 mti->key = KEYC_NONE;
225 if (mti->key != KEYC_NONE) {
226 mti->keystr = xstrdup(key_string_lookup_key(mti->key,
227 0));
228 mti->keylen = strlen(mti->keystr);
229 } else {
230 mti->keystr = NULL;
231 mti->keylen = 0;
234 TAILQ_FOREACH(mti, mtl, entry) {
235 for (i = 0; i < mtd->line_size; i++) {
236 line = &mtd->line_list[i];
237 if (line->item == mti)
238 line->flat = flat;
243 static void
244 mode_tree_clear_tagged(struct mode_tree_list *mtl)
246 struct mode_tree_item *mti;
248 TAILQ_FOREACH(mti, mtl, entry) {
249 mti->tagged = 0;
250 mode_tree_clear_tagged(&mti->children);
254 void
255 mode_tree_up(struct mode_tree_data *mtd, int wrap)
257 if (mtd->current == 0) {
258 if (wrap) {
259 mtd->current = mtd->line_size - 1;
260 if (mtd->line_size >= mtd->height)
261 mtd->offset = mtd->line_size - mtd->height;
263 } else {
264 mtd->current--;
265 if (mtd->current < mtd->offset)
266 mtd->offset--;
271 mode_tree_down(struct mode_tree_data *mtd, int wrap)
273 if (mtd->current == mtd->line_size - 1) {
274 if (wrap) {
275 mtd->current = 0;
276 mtd->offset = 0;
277 } else
278 return (0);
279 } else {
280 mtd->current++;
281 if (mtd->current > mtd->offset + mtd->height - 1)
282 mtd->offset++;
284 return (1);
287 void *
288 mode_tree_get_current(struct mode_tree_data *mtd)
290 return (mtd->line_list[mtd->current].item->itemdata);
293 const char *
294 mode_tree_get_current_name(struct mode_tree_data *mtd)
296 return (mtd->line_list[mtd->current].item->name);
299 void
300 mode_tree_expand_current(struct mode_tree_data *mtd)
302 if (!mtd->line_list[mtd->current].item->expanded) {
303 mtd->line_list[mtd->current].item->expanded = 1;
304 mode_tree_build(mtd);
308 void
309 mode_tree_collapse_current(struct mode_tree_data *mtd)
311 if (mtd->line_list[mtd->current].item->expanded) {
312 mtd->line_list[mtd->current].item->expanded = 0;
313 mode_tree_build(mtd);
317 static int
318 mode_tree_get_tag(struct mode_tree_data *mtd, uint64_t tag, u_int *found)
320 u_int i;
322 for (i = 0; i < mtd->line_size; i++) {
323 if (mtd->line_list[i].item->tag == tag)
324 break;
326 if (i != mtd->line_size) {
327 *found = i;
328 return (1);
330 return (0);
333 void
334 mode_tree_expand(struct mode_tree_data *mtd, uint64_t tag)
336 u_int found;
338 if (!mode_tree_get_tag(mtd, tag, &found))
339 return;
340 if (!mtd->line_list[found].item->expanded) {
341 mtd->line_list[found].item->expanded = 1;
342 mode_tree_build(mtd);
347 mode_tree_set_current(struct mode_tree_data *mtd, uint64_t tag)
349 u_int found;
351 if (mode_tree_get_tag(mtd, tag, &found)) {
352 mtd->current = found;
353 if (mtd->current > mtd->height - 1)
354 mtd->offset = mtd->current - mtd->height + 1;
355 else
356 mtd->offset = 0;
357 return (1);
359 mtd->current = 0;
360 mtd->offset = 0;
361 return (0);
364 u_int
365 mode_tree_count_tagged(struct mode_tree_data *mtd)
367 struct mode_tree_item *mti;
368 u_int i, tagged;
370 tagged = 0;
371 for (i = 0; i < mtd->line_size; i++) {
372 mti = mtd->line_list[i].item;
373 if (mti->tagged)
374 tagged++;
376 return (tagged);
379 void
380 mode_tree_each_tagged(struct mode_tree_data *mtd, mode_tree_each_cb cb,
381 struct client *c, key_code key, int current)
383 struct mode_tree_item *mti;
384 u_int i;
385 int fired;
387 fired = 0;
388 for (i = 0; i < mtd->line_size; i++) {
389 mti = mtd->line_list[i].item;
390 if (mti->tagged) {
391 fired = 1;
392 cb(mtd->modedata, mti->itemdata, c, key);
395 if (!fired && current) {
396 mti = mtd->line_list[mtd->current].item;
397 cb(mtd->modedata, mti->itemdata, c, key);
401 struct mode_tree_data *
402 mode_tree_start(struct window_pane *wp, struct args *args,
403 mode_tree_build_cb buildcb, mode_tree_draw_cb drawcb,
404 mode_tree_search_cb searchcb, mode_tree_menu_cb menucb,
405 mode_tree_height_cb heightcb, mode_tree_key_cb keycb, void *modedata,
406 const struct menu_item *menu, const char **sort_list, u_int sort_size,
407 struct screen **s)
409 struct mode_tree_data *mtd;
410 const char *sort;
411 u_int i;
413 mtd = xcalloc(1, sizeof *mtd);
414 mtd->references = 1;
416 mtd->wp = wp;
417 mtd->modedata = modedata;
418 mtd->menu = menu;
420 mtd->sort_list = sort_list;
421 mtd->sort_size = sort_size;
423 if (args_has(args, 'N') > 1)
424 mtd->preview = MODE_TREE_PREVIEW_BIG;
425 else if (args_has(args, 'N'))
426 mtd->preview = MODE_TREE_PREVIEW_OFF;
427 else
428 mtd->preview = MODE_TREE_PREVIEW_NORMAL;
430 sort = args_get(args, 'O');
431 if (sort != NULL) {
432 for (i = 0; i < sort_size; i++) {
433 if (strcasecmp(sort, sort_list[i]) == 0)
434 mtd->sort_crit.field = i;
437 mtd->sort_crit.reversed = args_has(args, 'r');
439 if (args_has(args, 'f'))
440 mtd->filter = xstrdup(args_get(args, 'f'));
441 else
442 mtd->filter = NULL;
444 mtd->buildcb = buildcb;
445 mtd->drawcb = drawcb;
446 mtd->searchcb = searchcb;
447 mtd->menucb = menucb;
448 mtd->heightcb = heightcb;
449 mtd->keycb = keycb;
451 TAILQ_INIT(&mtd->children);
453 *s = &mtd->screen;
454 screen_init(*s, screen_size_x(&wp->base), screen_size_y(&wp->base), 0);
455 (*s)->mode &= ~MODE_CURSOR;
457 return (mtd);
460 void
461 mode_tree_zoom(struct mode_tree_data *mtd, struct args *args)
463 struct window_pane *wp = mtd->wp;
465 if (args_has(args, 'Z')) {
466 mtd->zoomed = (wp->window->flags & WINDOW_ZOOMED);
467 if (!mtd->zoomed && window_zoom(wp) == 0)
468 server_redraw_window(wp->window);
469 } else
470 mtd->zoomed = -1;
473 static void
474 mode_tree_set_height(struct mode_tree_data *mtd)
476 struct screen *s = &mtd->screen;
477 u_int height;
479 if (mtd->heightcb != NULL) {
480 height = mtd->heightcb(mtd, screen_size_y(s));
481 if (height < screen_size_y(s))
482 mtd->height = screen_size_y(s) - height;
483 } else {
484 if (mtd->preview == MODE_TREE_PREVIEW_NORMAL) {
485 mtd->height = (screen_size_y(s) / 3) * 2;
486 if (mtd->height > mtd->line_size)
487 mtd->height = screen_size_y(s) / 2;
488 if (mtd->height < 10)
489 mtd->height = screen_size_y(s);
490 } else if (mtd->preview == MODE_TREE_PREVIEW_BIG) {
491 mtd->height = screen_size_y(s) / 4;
492 if (mtd->height > mtd->line_size)
493 mtd->height = mtd->line_size;
494 if (mtd->height < 2)
495 mtd->height = 2;
496 } else
497 mtd->height = screen_size_y(s);
499 if (screen_size_y(s) - mtd->height < 2)
500 mtd->height = screen_size_y(s);
503 void
504 mode_tree_build(struct mode_tree_data *mtd)
506 struct screen *s = &mtd->screen;
507 uint64_t tag;
509 if (mtd->line_list != NULL)
510 tag = mtd->line_list[mtd->current].item->tag;
511 else
512 tag = UINT64_MAX;
514 TAILQ_CONCAT(&mtd->saved, &mtd->children, entry);
515 TAILQ_INIT(&mtd->children);
517 mtd->buildcb(mtd->modedata, &mtd->sort_crit, &tag, mtd->filter);
518 mtd->no_matches = TAILQ_EMPTY(&mtd->children);
519 if (mtd->no_matches)
520 mtd->buildcb(mtd->modedata, &mtd->sort_crit, &tag, NULL);
522 mode_tree_free_items(&mtd->saved);
523 TAILQ_INIT(&mtd->saved);
525 mode_tree_clear_lines(mtd);
526 mode_tree_build_lines(mtd, &mtd->children, 0);
528 if (mtd->line_list != NULL && tag == UINT64_MAX)
529 tag = mtd->line_list[mtd->current].item->tag;
530 mode_tree_set_current(mtd, tag);
532 mtd->width = screen_size_x(s);
533 if (mtd->preview != MODE_TREE_PREVIEW_OFF)
534 mode_tree_set_height(mtd);
535 else
536 mtd->height = screen_size_y(s);
537 mode_tree_check_selected(mtd);
540 static void
541 mode_tree_remove_ref(struct mode_tree_data *mtd)
543 if (--mtd->references == 0)
544 free(mtd);
547 void
548 mode_tree_free(struct mode_tree_data *mtd)
550 struct window_pane *wp = mtd->wp;
552 if (mtd->zoomed == 0)
553 server_unzoom_window(wp->window);
555 mode_tree_free_items(&mtd->children);
556 mode_tree_clear_lines(mtd);
557 screen_free(&mtd->screen);
559 free(mtd->search);
560 free(mtd->filter);
562 mtd->dead = 1;
563 mode_tree_remove_ref(mtd);
566 void
567 mode_tree_resize(struct mode_tree_data *mtd, u_int sx, u_int sy)
569 struct screen *s = &mtd->screen;
571 screen_resize(s, sx, sy, 0);
573 mode_tree_build(mtd);
574 mode_tree_draw(mtd);
576 mtd->wp->flags |= PANE_REDRAW;
579 struct mode_tree_item *
580 mode_tree_add(struct mode_tree_data *mtd, struct mode_tree_item *parent,
581 void *itemdata, uint64_t tag, const char *name, const char *text,
582 int expanded)
584 struct mode_tree_item *mti, *saved;
586 log_debug("%s: %llu, %s %s", __func__, (unsigned long long)tag,
587 name, (text == NULL ? "" : text));
589 mti = xcalloc(1, sizeof *mti);
590 mti->parent = parent;
591 mti->itemdata = itemdata;
593 mti->tag = tag;
594 mti->name = xstrdup(name);
595 if (text != NULL)
596 mti->text = xstrdup(text);
598 saved = mode_tree_find_item(&mtd->saved, tag);
599 if (saved != NULL) {
600 if (parent == NULL || parent->expanded)
601 mti->tagged = saved->tagged;
602 mti->expanded = saved->expanded;
603 } else if (expanded == -1)
604 mti->expanded = 1;
605 else
606 mti->expanded = expanded;
608 TAILQ_INIT(&mti->children);
610 if (parent != NULL)
611 TAILQ_INSERT_TAIL(&parent->children, mti, entry);
612 else
613 TAILQ_INSERT_TAIL(&mtd->children, mti, entry);
615 return (mti);
618 void
619 mode_tree_draw_as_parent(struct mode_tree_item *mti)
621 mti->draw_as_parent = 1;
624 void
625 mode_tree_no_tag(struct mode_tree_item *mti)
627 mti->no_tag = 1;
630 void
631 mode_tree_remove(struct mode_tree_data *mtd, struct mode_tree_item *mti)
633 struct mode_tree_item *parent = mti->parent;
635 if (parent != NULL)
636 TAILQ_REMOVE(&parent->children, mti, entry);
637 else
638 TAILQ_REMOVE(&mtd->children, mti, entry);
639 mode_tree_free_item(mti);
642 void
643 mode_tree_draw(struct mode_tree_data *mtd)
645 struct window_pane *wp = mtd->wp;
646 struct screen *s = &mtd->screen;
647 struct mode_tree_line *line;
648 struct mode_tree_item *mti;
649 struct options *oo = wp->window->options;
650 struct screen_write_ctx ctx;
651 struct grid_cell gc0, gc;
652 u_int w, h, i, j, sy, box_x, box_y, width;
653 char *text, *start, *key;
654 const char *tag, *symbol;
655 size_t size, n;
656 int keylen, pad;
658 if (mtd->line_size == 0)
659 return;
661 memcpy(&gc0, &grid_default_cell, sizeof gc0);
662 memcpy(&gc, &grid_default_cell, sizeof gc);
663 style_apply(&gc, oo, "mode-style", NULL);
665 w = mtd->width;
666 h = mtd->height;
668 screen_write_start(&ctx, s);
669 screen_write_clearscreen(&ctx, 8);
671 keylen = 0;
672 for (i = 0; i < mtd->line_size; i++) {
673 mti = mtd->line_list[i].item;
674 if (mti->key == KEYC_NONE)
675 continue;
676 if ((int)mti->keylen + 3 > keylen)
677 keylen = mti->keylen + 3;
680 for (i = 0; i < mtd->line_size; i++) {
681 if (i < mtd->offset)
682 continue;
683 if (i > mtd->offset + h - 1)
684 break;
685 line = &mtd->line_list[i];
686 mti = line->item;
688 screen_write_cursormove(&ctx, 0, i - mtd->offset, 0);
690 pad = keylen - 2 - mti->keylen;
691 if (mti->key != KEYC_NONE)
692 xasprintf(&key, "(%s)%*s", mti->keystr, pad, "");
693 else
694 key = xstrdup("");
696 if (line->flat)
697 symbol = "";
698 else if (TAILQ_EMPTY(&mti->children))
699 symbol = " ";
700 else if (mti->expanded)
701 symbol = "- ";
702 else
703 symbol = "+ ";
705 if (line->depth == 0)
706 start = xstrdup(symbol);
707 else {
708 size = (4 * line->depth) + 32;
710 start = xcalloc(1, size);
711 for (j = 1; j < line->depth; j++) {
712 if (mti->parent != NULL &&
713 mtd->line_list[mti->parent->line].last)
714 strlcat(start, " ", size);
715 else
716 strlcat(start, "\001x\001 ", size);
718 if (line->last)
719 strlcat(start, "\001mq\001> ", size);
720 else
721 strlcat(start, "\001tq\001> ", size);
722 strlcat(start, symbol, size);
725 if (mti->tagged)
726 tag = "*";
727 else
728 tag = "";
729 xasprintf(&text, "%-*s%s%s%s%s", keylen, key, start, mti->name,
730 tag, (mti->text != NULL) ? ": " : "" );
731 width = utf8_cstrwidth(text);
732 if (width > w)
733 width = w;
734 free(start);
736 if (mti->tagged) {
737 gc.attr ^= GRID_ATTR_BRIGHT;
738 gc0.attr ^= GRID_ATTR_BRIGHT;
741 if (i != mtd->current) {
742 screen_write_clearendofline(&ctx, 8);
743 screen_write_nputs(&ctx, w, &gc0, "%s", text);
744 if (mti->text != NULL) {
745 format_draw(&ctx, &gc0, w - width, mti->text,
746 NULL, 0);
748 } else {
749 screen_write_clearendofline(&ctx, gc.bg);
750 screen_write_nputs(&ctx, w, &gc, "%s", text);
751 if (mti->text != NULL) {
752 format_draw(&ctx, &gc, w - width, mti->text,
753 NULL, 0);
756 free(text);
757 free(key);
759 if (mti->tagged) {
760 gc.attr ^= GRID_ATTR_BRIGHT;
761 gc0.attr ^= GRID_ATTR_BRIGHT;
765 if (mtd->preview == MODE_TREE_PREVIEW_OFF)
766 goto done;
768 sy = screen_size_y(s);
769 if (sy <= 4 || h < 2 || sy - h <= 4 || w <= 4)
770 goto done;
772 line = &mtd->line_list[mtd->current];
773 mti = line->item;
774 if (mti->draw_as_parent)
775 mti = mti->parent;
777 screen_write_cursormove(&ctx, 0, h, 0);
778 screen_write_box(&ctx, w, sy - h, BOX_LINES_DEFAULT, NULL, NULL);
780 if (mtd->sort_list != NULL) {
781 xasprintf(&text, " %s (sort: %s%s)", mti->name,
782 mtd->sort_list[mtd->sort_crit.field],
783 mtd->sort_crit.reversed ? ", reversed" : "");
784 } else
785 xasprintf(&text, " %s", mti->name);
786 if (w - 2 >= strlen(text)) {
787 screen_write_cursormove(&ctx, 1, h, 0);
788 screen_write_puts(&ctx, &gc0, "%s", text);
790 if (mtd->no_matches)
791 n = (sizeof "no matches") - 1;
792 else
793 n = (sizeof "active") - 1;
794 if (mtd->filter != NULL && w - 2 >= strlen(text) + 10 + n + 2) {
795 screen_write_puts(&ctx, &gc0, " (filter: ");
796 if (mtd->no_matches)
797 screen_write_puts(&ctx, &gc, "no matches");
798 else
799 screen_write_puts(&ctx, &gc0, "active");
800 screen_write_puts(&ctx, &gc0, ") ");
801 } else
802 screen_write_puts(&ctx, &gc0, " ");
804 free(text);
806 box_x = w - 4;
807 box_y = sy - h - 2;
809 if (box_x != 0 && box_y != 0) {
810 screen_write_cursormove(&ctx, 2, h + 1, 0);
811 mtd->drawcb(mtd->modedata, mti->itemdata, &ctx, box_x, box_y);
814 done:
815 screen_write_cursormove(&ctx, 0, mtd->current - mtd->offset, 0);
816 screen_write_stop(&ctx);
819 static struct mode_tree_item *
820 mode_tree_search_backward(struct mode_tree_data *mtd)
822 struct mode_tree_item *mti, *last, *prev;
824 if (mtd->search == NULL)
825 return (NULL);
827 mti = last = mtd->line_list[mtd->current].item;
828 for (;;) {
829 if ((prev = TAILQ_PREV(mti, mode_tree_list, entry)) != NULL) {
830 /* Point to the last child in the previous subtree. */
831 while (!TAILQ_EMPTY(&prev->children))
832 prev = TAILQ_LAST(&prev->children, mode_tree_list);
833 mti = prev;
834 } else {
835 /* If prev is NULL, jump to the parent. */
836 mti = mti->parent;
839 if (mti == NULL) {
840 /* Point to the last child in the last root subtree. */
841 prev = TAILQ_LAST(&mtd->children, mode_tree_list);
842 while (!TAILQ_EMPTY(&prev->children))
843 prev = TAILQ_LAST(&prev->children, mode_tree_list);
844 mti = prev;
846 if (mti == last)
847 break;
849 if (mtd->searchcb == NULL) {
850 if (strstr(mti->name, mtd->search) != NULL)
851 return (mti);
852 continue;
854 if (mtd->searchcb(mtd->modedata, mti->itemdata, mtd->search))
855 return (mti);
857 return (NULL);
861 static struct mode_tree_item *
862 mode_tree_search_forward(struct mode_tree_data *mtd)
864 struct mode_tree_item *mti, *last, *next;
866 if (mtd->search == NULL)
867 return (NULL);
869 mti = last = mtd->line_list[mtd->current].item;
870 for (;;) {
871 if (!TAILQ_EMPTY(&mti->children))
872 mti = TAILQ_FIRST(&mti->children);
873 else if ((next = TAILQ_NEXT(mti, entry)) != NULL)
874 mti = next;
875 else {
876 for (;;) {
877 mti = mti->parent;
878 if (mti == NULL)
879 break;
880 if ((next = TAILQ_NEXT(mti, entry)) != NULL) {
881 mti = next;
882 break;
886 if (mti == NULL)
887 mti = TAILQ_FIRST(&mtd->children);
888 if (mti == last)
889 break;
891 if (mtd->searchcb == NULL) {
892 if (strstr(mti->name, mtd->search) != NULL)
893 return (mti);
894 continue;
896 if (mtd->searchcb(mtd->modedata, mti->itemdata, mtd->search))
897 return (mti);
899 return (NULL);
902 static void
903 mode_tree_search_set(struct mode_tree_data *mtd)
905 struct mode_tree_item *mti, *loop;
906 uint64_t tag;
908 if (mtd->search_dir == MODE_TREE_SEARCH_FORWARD)
909 mti = mode_tree_search_forward(mtd);
910 else
911 mti = mode_tree_search_backward(mtd);
912 if (mti == NULL)
913 return;
914 tag = mti->tag;
916 loop = mti->parent;
917 while (loop != NULL) {
918 loop->expanded = 1;
919 loop = loop->parent;
922 mode_tree_build(mtd);
923 mode_tree_set_current(mtd, tag);
924 mode_tree_draw(mtd);
925 mtd->wp->flags |= PANE_REDRAW;
928 static int
929 mode_tree_search_callback(__unused struct client *c, void *data, const char *s,
930 __unused int done)
932 struct mode_tree_data *mtd = data;
934 if (mtd->dead)
935 return (0);
937 free(mtd->search);
938 if (s == NULL || *s == '\0') {
939 mtd->search = NULL;
940 return (0);
942 mtd->search = xstrdup(s);
943 mode_tree_search_set(mtd);
945 return (0);
948 static void
949 mode_tree_search_free(void *data)
951 mode_tree_remove_ref(data);
954 static int
955 mode_tree_filter_callback(__unused struct client *c, void *data, const char *s,
956 __unused int done)
958 struct mode_tree_data *mtd = data;
960 if (mtd->dead)
961 return (0);
963 if (mtd->filter != NULL)
964 free(mtd->filter);
965 if (s == NULL || *s == '\0')
966 mtd->filter = NULL;
967 else
968 mtd->filter = xstrdup(s);
970 mode_tree_build(mtd);
971 mode_tree_draw(mtd);
972 mtd->wp->flags |= PANE_REDRAW;
974 return (0);
977 static void
978 mode_tree_filter_free(void *data)
980 mode_tree_remove_ref(data);
983 static void
984 mode_tree_menu_callback(__unused struct menu *menu, __unused u_int idx,
985 key_code key, void *data)
987 struct mode_tree_menu *mtm = data;
988 struct mode_tree_data *mtd = mtm->data;
990 if (mtd->dead || key == KEYC_NONE)
991 goto out;
993 if (mtm->line >= mtd->line_size)
994 goto out;
995 mtd->current = mtm->line;
996 mtd->menucb(mtd->modedata, mtm->c, key);
998 out:
999 mode_tree_remove_ref(mtd);
1000 free(mtm);
1003 static void
1004 mode_tree_display_menu(struct mode_tree_data *mtd, struct client *c, u_int x,
1005 u_int y, int outside)
1007 struct mode_tree_item *mti;
1008 struct menu *menu;
1009 const struct menu_item *items;
1010 struct mode_tree_menu *mtm;
1011 char *title;
1012 u_int line;
1014 if (mtd->offset + y > mtd->line_size - 1)
1015 line = mtd->current;
1016 else
1017 line = mtd->offset + y;
1018 mti = mtd->line_list[line].item;
1020 if (!outside) {
1021 items = mtd->menu;
1022 xasprintf(&title, "#[align=centre]%s", mti->name);
1023 } else {
1024 items = mode_tree_menu_items;
1025 title = xstrdup("");
1027 menu = menu_create(title);
1028 menu_add_items(menu, items, NULL, c, NULL);
1029 free(title);
1031 mtm = xmalloc(sizeof *mtm);
1032 mtm->data = mtd;
1033 mtm->c = c;
1034 mtm->line = line;
1035 mtd->references++;
1037 if (x >= (menu->width + 4) / 2)
1038 x -= (menu->width + 4) / 2;
1039 else
1040 x = 0;
1041 if (menu_display(menu, 0, 0, NULL, x, y, c, BOX_LINES_DEFAULT, NULL,
1042 NULL, NULL, NULL, mode_tree_menu_callback, mtm) != 0)
1043 menu_free(menu);
1047 mode_tree_key(struct mode_tree_data *mtd, struct client *c, key_code *key,
1048 struct mouse_event *m, u_int *xp, u_int *yp)
1050 struct mode_tree_line *line;
1051 struct mode_tree_item *current, *parent, *mti;
1052 u_int i, x, y;
1053 int choice;
1055 if (KEYC_IS_MOUSE(*key) && m != NULL) {
1056 if (cmd_mouse_at(mtd->wp, m, &x, &y, 0) != 0) {
1057 *key = KEYC_NONE;
1058 return (0);
1060 if (xp != NULL)
1061 *xp = x;
1062 if (yp != NULL)
1063 *yp = y;
1064 if (x > mtd->width || y > mtd->height) {
1065 if (*key == KEYC_MOUSEDOWN3_PANE)
1066 mode_tree_display_menu(mtd, c, x, y, 1);
1067 if (mtd->preview == MODE_TREE_PREVIEW_OFF)
1068 *key = KEYC_NONE;
1069 return (0);
1071 if (mtd->offset + y < mtd->line_size) {
1072 if (*key == KEYC_MOUSEDOWN1_PANE ||
1073 *key == KEYC_MOUSEDOWN3_PANE ||
1074 *key == KEYC_DOUBLECLICK1_PANE)
1075 mtd->current = mtd->offset + y;
1076 if (*key == KEYC_DOUBLECLICK1_PANE)
1077 *key = '\r';
1078 else {
1079 if (*key == KEYC_MOUSEDOWN3_PANE)
1080 mode_tree_display_menu(mtd, c, x, y, 0);
1081 *key = KEYC_NONE;
1083 } else {
1084 if (*key == KEYC_MOUSEDOWN3_PANE)
1085 mode_tree_display_menu(mtd, c, x, y, 0);
1086 *key = KEYC_NONE;
1088 return (0);
1091 line = &mtd->line_list[mtd->current];
1092 current = line->item;
1094 choice = -1;
1095 for (i = 0; i < mtd->line_size; i++) {
1096 if (*key == mtd->line_list[i].item->key) {
1097 choice = i;
1098 break;
1101 if (choice != -1) {
1102 if ((u_int)choice > mtd->line_size - 1) {
1103 *key = KEYC_NONE;
1104 return (0);
1106 mtd->current = choice;
1107 *key = '\r';
1108 return (0);
1111 switch (*key) {
1112 case 'q':
1113 case '\033': /* Escape */
1114 case 'g'|KEYC_CTRL:
1115 return (1);
1116 case KEYC_UP:
1117 case 'k':
1118 case KEYC_WHEELUP_PANE:
1119 case 'p'|KEYC_CTRL:
1120 mode_tree_up(mtd, 1);
1121 break;
1122 case KEYC_DOWN:
1123 case 'j':
1124 case KEYC_WHEELDOWN_PANE:
1125 case 'n'|KEYC_CTRL:
1126 mode_tree_down(mtd, 1);
1127 break;
1128 case KEYC_PPAGE:
1129 case 'b'|KEYC_CTRL:
1130 for (i = 0; i < mtd->height; i++) {
1131 if (mtd->current == 0)
1132 break;
1133 mode_tree_up(mtd, 1);
1135 break;
1136 case KEYC_NPAGE:
1137 case 'f'|KEYC_CTRL:
1138 for (i = 0; i < mtd->height; i++) {
1139 if (mtd->current == mtd->line_size - 1)
1140 break;
1141 mode_tree_down(mtd, 1);
1143 break;
1144 case 'g':
1145 case KEYC_HOME:
1146 mtd->current = 0;
1147 mtd->offset = 0;
1148 break;
1149 case 'G':
1150 case KEYC_END:
1151 mtd->current = mtd->line_size - 1;
1152 if (mtd->current > mtd->height - 1)
1153 mtd->offset = mtd->current - mtd->height + 1;
1154 else
1155 mtd->offset = 0;
1156 break;
1157 case 't':
1159 * Do not allow parents and children to both be tagged: untag
1160 * all parents and children of current.
1162 if (current->no_tag)
1163 break;
1164 if (!current->tagged) {
1165 parent = current->parent;
1166 while (parent != NULL) {
1167 parent->tagged = 0;
1168 parent = parent->parent;
1170 mode_tree_clear_tagged(&current->children);
1171 current->tagged = 1;
1172 } else
1173 current->tagged = 0;
1174 if (m != NULL)
1175 mode_tree_down(mtd, 0);
1176 break;
1177 case 'T':
1178 for (i = 0; i < mtd->line_size; i++)
1179 mtd->line_list[i].item->tagged = 0;
1180 break;
1181 case 't'|KEYC_CTRL:
1182 for (i = 0; i < mtd->line_size; i++) {
1183 if ((mtd->line_list[i].item->parent == NULL &&
1184 !mtd->line_list[i].item->no_tag) ||
1185 (mtd->line_list[i].item->parent != NULL &&
1186 mtd->line_list[i].item->parent->no_tag))
1187 mtd->line_list[i].item->tagged = 1;
1188 else
1189 mtd->line_list[i].item->tagged = 0;
1191 break;
1192 case 'O':
1193 mtd->sort_crit.field++;
1194 if (mtd->sort_crit.field >= mtd->sort_size)
1195 mtd->sort_crit.field = 0;
1196 mode_tree_build(mtd);
1197 break;
1198 case 'r':
1199 mtd->sort_crit.reversed = !mtd->sort_crit.reversed;
1200 mode_tree_build(mtd);
1201 break;
1202 case KEYC_LEFT:
1203 case 'h':
1204 case '-':
1205 if (line->flat || !current->expanded)
1206 current = current->parent;
1207 if (current == NULL)
1208 mode_tree_up(mtd, 0);
1209 else {
1210 current->expanded = 0;
1211 mtd->current = current->line;
1212 mode_tree_build(mtd);
1214 break;
1215 case KEYC_RIGHT:
1216 case 'l':
1217 case '+':
1218 if (line->flat || current->expanded)
1219 mode_tree_down(mtd, 0);
1220 else if (!line->flat) {
1221 current->expanded = 1;
1222 mode_tree_build(mtd);
1224 break;
1225 case '-'|KEYC_META:
1226 TAILQ_FOREACH(mti, &mtd->children, entry)
1227 mti->expanded = 0;
1228 mode_tree_build(mtd);
1229 break;
1230 case '+'|KEYC_META:
1231 TAILQ_FOREACH(mti, &mtd->children, entry)
1232 mti->expanded = 1;
1233 mode_tree_build(mtd);
1234 break;
1235 case '?':
1236 case '/':
1237 case 's'|KEYC_CTRL:
1238 mtd->references++;
1239 status_prompt_set(c, NULL, "(search) ", "",
1240 mode_tree_search_callback, mode_tree_search_free, mtd,
1241 PROMPT_NOFORMAT, PROMPT_TYPE_SEARCH);
1242 break;
1243 case 'n':
1244 mtd->search_dir = MODE_TREE_SEARCH_FORWARD;
1245 mode_tree_search_set(mtd);
1246 break;
1247 case 'N':
1248 mtd->search_dir = MODE_TREE_SEARCH_BACKWARD;
1249 mode_tree_search_set(mtd);
1250 break;
1251 case 'f':
1252 mtd->references++;
1253 status_prompt_set(c, NULL, "(filter) ", mtd->filter,
1254 mode_tree_filter_callback, mode_tree_filter_free, mtd,
1255 PROMPT_NOFORMAT, PROMPT_TYPE_SEARCH);
1256 break;
1257 case 'v':
1258 switch (mtd->preview) {
1259 case MODE_TREE_PREVIEW_OFF:
1260 mtd->preview = MODE_TREE_PREVIEW_BIG;
1261 break;
1262 case MODE_TREE_PREVIEW_NORMAL:
1263 mtd->preview = MODE_TREE_PREVIEW_OFF;
1264 break;
1265 case MODE_TREE_PREVIEW_BIG:
1266 mtd->preview = MODE_TREE_PREVIEW_NORMAL;
1267 break;
1269 mode_tree_build(mtd);
1270 if (mtd->preview != MODE_TREE_PREVIEW_OFF)
1271 mode_tree_check_selected(mtd);
1272 break;
1274 return (0);
1277 void
1278 mode_tree_run_command(struct client *c, struct cmd_find_state *fs,
1279 const char *template, const char *name)
1281 struct cmdq_state *state;
1282 char *command, *error;
1283 enum cmd_parse_status status;
1285 command = cmd_template_replace(template, name, 1);
1286 if (command != NULL && *command != '\0') {
1287 state = cmdq_new_state(fs, NULL, 0);
1288 status = cmd_parse_and_append(command, NULL, c, state, &error);
1289 if (status == CMD_PARSE_ERROR) {
1290 if (c != NULL) {
1291 *error = toupper((u_char)*error);
1292 status_message_set(c, -1, 1, 0, "%s", error);
1294 free(error);
1296 cmdq_free_state(state);
1298 free(command);