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>
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
,
39 struct mode_tree_item
;
40 TAILQ_HEAD(mode_tree_list
, mode_tree_item
);
42 struct mode_tree_data
{
47 struct window_pane
*wp
;
49 const struct menu_item
*menu
;
51 const char **sort_list
;
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
;
82 enum mode_tree_search_dir search_dir
;
85 struct mode_tree_item
{
86 struct mode_tree_item
*parent
;
104 struct mode_tree_list children
;
105 TAILQ_ENTRY(mode_tree_item
) entry
;
108 struct mode_tree_line
{
109 struct mode_tree_item
*item
;
115 struct mode_tree_menu
{
116 struct mode_tree_data
*data
;
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
) {
140 child
= mode_tree_find_item(&mti
->children
, tag
);
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
);
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
);
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
177 if (mtd
->current
> mtd
->height
- 1)
178 mtd
->offset
= mtd
->current
- mtd
->height
+ 1;
182 mode_tree_clear_lines(struct mode_tree_data
*mtd
)
184 free(mtd
->line_list
);
185 mtd
->line_list
= NULL
;
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
;
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
++];
206 line
->last
= (mti
== TAILQ_LAST(mtl
, mode_tree_list
));
208 mti
->line
= (mtd
->line_size
- 1);
209 if (!TAILQ_EMPTY(&mti
->children
))
212 mode_tree_build_lines(mtd
, &mti
->children
, depth
+ 1);
214 if (mtd
->keycb
!= NULL
) {
215 mti
->key
= mtd
->keycb(mtd
->modedata
, mti
->itemdata
,
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);
224 mti
->key
= KEYC_NONE
;
225 if (mti
->key
!= KEYC_NONE
) {
226 mti
->keystr
= xstrdup(key_string_lookup_key(mti
->key
,
228 mti
->keylen
= strlen(mti
->keystr
);
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
)
244 mode_tree_clear_tagged(struct mode_tree_list
*mtl
)
246 struct mode_tree_item
*mti
;
248 TAILQ_FOREACH(mti
, mtl
, entry
) {
250 mode_tree_clear_tagged(&mti
->children
);
255 mode_tree_up(struct mode_tree_data
*mtd
, int wrap
)
257 if (mtd
->current
== 0) {
259 mtd
->current
= mtd
->line_size
- 1;
260 if (mtd
->line_size
>= mtd
->height
)
261 mtd
->offset
= mtd
->line_size
- mtd
->height
;
265 if (mtd
->current
< mtd
->offset
)
271 mode_tree_down(struct mode_tree_data
*mtd
, int wrap
)
273 if (mtd
->current
== mtd
->line_size
- 1) {
281 if (mtd
->current
> mtd
->offset
+ mtd
->height
- 1)
288 mode_tree_get_current(struct mode_tree_data
*mtd
)
290 return (mtd
->line_list
[mtd
->current
].item
->itemdata
);
294 mode_tree_get_current_name(struct mode_tree_data
*mtd
)
296 return (mtd
->line_list
[mtd
->current
].item
->name
);
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
);
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
);
318 mode_tree_get_tag(struct mode_tree_data
*mtd
, uint64_t tag
, u_int
*found
)
322 for (i
= 0; i
< mtd
->line_size
; i
++) {
323 if (mtd
->line_list
[i
].item
->tag
== tag
)
326 if (i
!= mtd
->line_size
) {
334 mode_tree_expand(struct mode_tree_data
*mtd
, uint64_t tag
)
338 if (!mode_tree_get_tag(mtd
, tag
, &found
))
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
)
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;
359 if (mtd
->current
>= mtd
->line_size
) {
360 mtd
->current
= mtd
->line_size
- 1;
361 if (mtd
->current
> mtd
->height
- 1)
362 mtd
->offset
= mtd
->current
- mtd
->height
+ 1;
370 mode_tree_count_tagged(struct mode_tree_data
*mtd
)
372 struct mode_tree_item
*mti
;
376 for (i
= 0; i
< mtd
->line_size
; i
++) {
377 mti
= mtd
->line_list
[i
].item
;
385 mode_tree_each_tagged(struct mode_tree_data
*mtd
, mode_tree_each_cb cb
,
386 struct client
*c
, key_code key
, int current
)
388 struct mode_tree_item
*mti
;
393 for (i
= 0; i
< mtd
->line_size
; i
++) {
394 mti
= mtd
->line_list
[i
].item
;
397 cb(mtd
->modedata
, mti
->itemdata
, c
, key
);
400 if (!fired
&& current
) {
401 mti
= mtd
->line_list
[mtd
->current
].item
;
402 cb(mtd
->modedata
, mti
->itemdata
, c
, key
);
406 struct mode_tree_data
*
407 mode_tree_start(struct window_pane
*wp
, struct args
*args
,
408 mode_tree_build_cb buildcb
, mode_tree_draw_cb drawcb
,
409 mode_tree_search_cb searchcb
, mode_tree_menu_cb menucb
,
410 mode_tree_height_cb heightcb
, mode_tree_key_cb keycb
, void *modedata
,
411 const struct menu_item
*menu
, const char **sort_list
, u_int sort_size
,
414 struct mode_tree_data
*mtd
;
418 mtd
= xcalloc(1, sizeof *mtd
);
422 mtd
->modedata
= modedata
;
425 mtd
->sort_list
= sort_list
;
426 mtd
->sort_size
= sort_size
;
428 if (args_has(args
, 'N') > 1)
429 mtd
->preview
= MODE_TREE_PREVIEW_BIG
;
430 else if (args_has(args
, 'N'))
431 mtd
->preview
= MODE_TREE_PREVIEW_OFF
;
433 mtd
->preview
= MODE_TREE_PREVIEW_NORMAL
;
435 sort
= args_get(args
, 'O');
437 for (i
= 0; i
< sort_size
; i
++) {
438 if (strcasecmp(sort
, sort_list
[i
]) == 0)
439 mtd
->sort_crit
.field
= i
;
442 mtd
->sort_crit
.reversed
= args_has(args
, 'r');
444 if (args_has(args
, 'f'))
445 mtd
->filter
= xstrdup(args_get(args
, 'f'));
449 mtd
->buildcb
= buildcb
;
450 mtd
->drawcb
= drawcb
;
451 mtd
->searchcb
= searchcb
;
452 mtd
->menucb
= menucb
;
453 mtd
->heightcb
= heightcb
;
456 TAILQ_INIT(&mtd
->children
);
459 screen_init(*s
, screen_size_x(&wp
->base
), screen_size_y(&wp
->base
), 0);
460 (*s
)->mode
&= ~MODE_CURSOR
;
466 mode_tree_zoom(struct mode_tree_data
*mtd
, struct args
*args
)
468 struct window_pane
*wp
= mtd
->wp
;
470 if (args_has(args
, 'Z')) {
471 mtd
->zoomed
= (wp
->window
->flags
& WINDOW_ZOOMED
);
472 if (!mtd
->zoomed
&& window_zoom(wp
) == 0)
473 server_redraw_window(wp
->window
);
479 mode_tree_set_height(struct mode_tree_data
*mtd
)
481 struct screen
*s
= &mtd
->screen
;
484 if (mtd
->heightcb
!= NULL
) {
485 height
= mtd
->heightcb(mtd
, screen_size_y(s
));
486 if (height
< screen_size_y(s
))
487 mtd
->height
= screen_size_y(s
) - height
;
489 if (mtd
->preview
== MODE_TREE_PREVIEW_NORMAL
) {
490 mtd
->height
= (screen_size_y(s
) / 3) * 2;
491 if (mtd
->height
> mtd
->line_size
)
492 mtd
->height
= screen_size_y(s
) / 2;
493 if (mtd
->height
< 10)
494 mtd
->height
= screen_size_y(s
);
495 } else if (mtd
->preview
== MODE_TREE_PREVIEW_BIG
) {
496 mtd
->height
= screen_size_y(s
) / 4;
497 if (mtd
->height
> mtd
->line_size
)
498 mtd
->height
= mtd
->line_size
;
502 mtd
->height
= screen_size_y(s
);
504 if (screen_size_y(s
) - mtd
->height
< 2)
505 mtd
->height
= screen_size_y(s
);
509 mode_tree_build(struct mode_tree_data
*mtd
)
511 struct screen
*s
= &mtd
->screen
;
514 if (mtd
->line_list
!= NULL
)
515 tag
= mtd
->line_list
[mtd
->current
].item
->tag
;
519 TAILQ_CONCAT(&mtd
->saved
, &mtd
->children
, entry
);
520 TAILQ_INIT(&mtd
->children
);
522 mtd
->buildcb(mtd
->modedata
, &mtd
->sort_crit
, &tag
, mtd
->filter
);
523 mtd
->no_matches
= TAILQ_EMPTY(&mtd
->children
);
525 mtd
->buildcb(mtd
->modedata
, &mtd
->sort_crit
, &tag
, NULL
);
527 mode_tree_free_items(&mtd
->saved
);
528 TAILQ_INIT(&mtd
->saved
);
530 mode_tree_clear_lines(mtd
);
531 mode_tree_build_lines(mtd
, &mtd
->children
, 0);
533 if (mtd
->line_list
!= NULL
&& tag
== UINT64_MAX
)
534 tag
= mtd
->line_list
[mtd
->current
].item
->tag
;
535 mode_tree_set_current(mtd
, tag
);
537 mtd
->width
= screen_size_x(s
);
538 if (mtd
->preview
!= MODE_TREE_PREVIEW_OFF
)
539 mode_tree_set_height(mtd
);
541 mtd
->height
= screen_size_y(s
);
542 mode_tree_check_selected(mtd
);
546 mode_tree_remove_ref(struct mode_tree_data
*mtd
)
548 if (--mtd
->references
== 0)
553 mode_tree_free(struct mode_tree_data
*mtd
)
555 struct window_pane
*wp
= mtd
->wp
;
557 if (mtd
->zoomed
== 0)
558 server_unzoom_window(wp
->window
);
560 mode_tree_free_items(&mtd
->children
);
561 mode_tree_clear_lines(mtd
);
562 screen_free(&mtd
->screen
);
568 mode_tree_remove_ref(mtd
);
572 mode_tree_resize(struct mode_tree_data
*mtd
, u_int sx
, u_int sy
)
574 struct screen
*s
= &mtd
->screen
;
576 screen_resize(s
, sx
, sy
, 0);
578 mode_tree_build(mtd
);
581 mtd
->wp
->flags
|= PANE_REDRAW
;
584 struct mode_tree_item
*
585 mode_tree_add(struct mode_tree_data
*mtd
, struct mode_tree_item
*parent
,
586 void *itemdata
, uint64_t tag
, const char *name
, const char *text
,
589 struct mode_tree_item
*mti
, *saved
;
591 log_debug("%s: %llu, %s %s", __func__
, (unsigned long long)tag
,
592 name
, (text
== NULL
? "" : text
));
594 mti
= xcalloc(1, sizeof *mti
);
595 mti
->parent
= parent
;
596 mti
->itemdata
= itemdata
;
599 mti
->name
= xstrdup(name
);
601 mti
->text
= xstrdup(text
);
603 saved
= mode_tree_find_item(&mtd
->saved
, tag
);
605 if (parent
== NULL
|| parent
->expanded
)
606 mti
->tagged
= saved
->tagged
;
607 mti
->expanded
= saved
->expanded
;
608 } else if (expanded
== -1)
611 mti
->expanded
= expanded
;
613 TAILQ_INIT(&mti
->children
);
616 TAILQ_INSERT_TAIL(&parent
->children
, mti
, entry
);
618 TAILQ_INSERT_TAIL(&mtd
->children
, mti
, entry
);
624 mode_tree_draw_as_parent(struct mode_tree_item
*mti
)
626 mti
->draw_as_parent
= 1;
630 mode_tree_no_tag(struct mode_tree_item
*mti
)
636 mode_tree_remove(struct mode_tree_data
*mtd
, struct mode_tree_item
*mti
)
638 struct mode_tree_item
*parent
= mti
->parent
;
641 TAILQ_REMOVE(&parent
->children
, mti
, entry
);
643 TAILQ_REMOVE(&mtd
->children
, mti
, entry
);
644 mode_tree_free_item(mti
);
648 mode_tree_draw(struct mode_tree_data
*mtd
)
650 struct window_pane
*wp
= mtd
->wp
;
651 struct screen
*s
= &mtd
->screen
;
652 struct mode_tree_line
*line
;
653 struct mode_tree_item
*mti
;
654 struct options
*oo
= wp
->window
->options
;
655 struct screen_write_ctx ctx
;
656 struct grid_cell gc0
, gc
;
657 u_int w
, h
, i
, j
, sy
, box_x
, box_y
, width
;
658 char *text
, *start
, *key
;
659 const char *tag
, *symbol
;
663 if (mtd
->line_size
== 0)
666 memcpy(&gc0
, &grid_default_cell
, sizeof gc0
);
667 memcpy(&gc
, &grid_default_cell
, sizeof gc
);
668 style_apply(&gc
, oo
, "mode-style", NULL
);
673 screen_write_start(&ctx
, s
);
674 screen_write_clearscreen(&ctx
, 8);
677 for (i
= 0; i
< mtd
->line_size
; i
++) {
678 mti
= mtd
->line_list
[i
].item
;
679 if (mti
->key
== KEYC_NONE
)
681 if ((int)mti
->keylen
+ 3 > keylen
)
682 keylen
= mti
->keylen
+ 3;
685 for (i
= 0; i
< mtd
->line_size
; i
++) {
688 if (i
> mtd
->offset
+ h
- 1)
690 line
= &mtd
->line_list
[i
];
693 screen_write_cursormove(&ctx
, 0, i
- mtd
->offset
, 0);
695 pad
= keylen
- 2 - mti
->keylen
;
696 if (mti
->key
!= KEYC_NONE
)
697 xasprintf(&key
, "(%s)%*s", mti
->keystr
, pad
, "");
703 else if (TAILQ_EMPTY(&mti
->children
))
705 else if (mti
->expanded
)
710 if (line
->depth
== 0)
711 start
= xstrdup(symbol
);
713 size
= (4 * line
->depth
) + 32;
715 start
= xcalloc(1, size
);
716 for (j
= 1; j
< line
->depth
; j
++) {
717 if (mti
->parent
!= NULL
&&
718 mtd
->line_list
[mti
->parent
->line
].last
)
719 strlcat(start
, " ", size
);
721 strlcat(start
, "\001x\001 ", size
);
724 strlcat(start
, "\001mq\001> ", size
);
726 strlcat(start
, "\001tq\001> ", size
);
727 strlcat(start
, symbol
, size
);
734 xasprintf(&text
, "%-*s%s%s%s%s", keylen
, key
, start
, mti
->name
,
735 tag
, (mti
->text
!= NULL
) ? ": " : "" );
736 width
= utf8_cstrwidth(text
);
742 gc
.attr
^= GRID_ATTR_BRIGHT
;
743 gc0
.attr
^= GRID_ATTR_BRIGHT
;
746 if (i
!= mtd
->current
) {
747 screen_write_clearendofline(&ctx
, 8);
748 screen_write_nputs(&ctx
, w
, &gc0
, "%s", text
);
749 if (mti
->text
!= NULL
) {
750 format_draw(&ctx
, &gc0
, w
- width
, mti
->text
,
754 screen_write_clearendofline(&ctx
, gc
.bg
);
755 screen_write_nputs(&ctx
, w
, &gc
, "%s", text
);
756 if (mti
->text
!= NULL
) {
757 format_draw(&ctx
, &gc
, w
- width
, mti
->text
,
765 gc
.attr
^= GRID_ATTR_BRIGHT
;
766 gc0
.attr
^= GRID_ATTR_BRIGHT
;
770 if (mtd
->preview
== MODE_TREE_PREVIEW_OFF
)
773 sy
= screen_size_y(s
);
774 if (sy
<= 4 || h
< 2 || sy
- h
<= 4 || w
<= 4)
777 line
= &mtd
->line_list
[mtd
->current
];
779 if (mti
->draw_as_parent
)
782 screen_write_cursormove(&ctx
, 0, h
, 0);
783 screen_write_box(&ctx
, w
, sy
- h
, BOX_LINES_DEFAULT
, NULL
, NULL
);
785 if (mtd
->sort_list
!= NULL
) {
786 xasprintf(&text
, " %s (sort: %s%s)", mti
->name
,
787 mtd
->sort_list
[mtd
->sort_crit
.field
],
788 mtd
->sort_crit
.reversed
? ", reversed" : "");
790 xasprintf(&text
, " %s", mti
->name
);
791 if (w
- 2 >= strlen(text
)) {
792 screen_write_cursormove(&ctx
, 1, h
, 0);
793 screen_write_puts(&ctx
, &gc0
, "%s", text
);
796 n
= (sizeof "no matches") - 1;
798 n
= (sizeof "active") - 1;
799 if (mtd
->filter
!= NULL
&& w
- 2 >= strlen(text
) + 10 + n
+ 2) {
800 screen_write_puts(&ctx
, &gc0
, " (filter: ");
802 screen_write_puts(&ctx
, &gc
, "no matches");
804 screen_write_puts(&ctx
, &gc0
, "active");
805 screen_write_puts(&ctx
, &gc0
, ") ");
807 screen_write_puts(&ctx
, &gc0
, " ");
814 if (box_x
!= 0 && box_y
!= 0) {
815 screen_write_cursormove(&ctx
, 2, h
+ 1, 0);
816 mtd
->drawcb(mtd
->modedata
, mti
->itemdata
, &ctx
, box_x
, box_y
);
820 screen_write_cursormove(&ctx
, 0, mtd
->current
- mtd
->offset
, 0);
821 screen_write_stop(&ctx
);
824 static struct mode_tree_item
*
825 mode_tree_search_backward(struct mode_tree_data
*mtd
)
827 struct mode_tree_item
*mti
, *last
, *prev
;
829 if (mtd
->search
== NULL
)
832 mti
= last
= mtd
->line_list
[mtd
->current
].item
;
834 if ((prev
= TAILQ_PREV(mti
, mode_tree_list
, entry
)) != NULL
) {
835 /* Point to the last child in the previous subtree. */
836 while (!TAILQ_EMPTY(&prev
->children
))
837 prev
= TAILQ_LAST(&prev
->children
, mode_tree_list
);
840 /* If prev is NULL, jump to the parent. */
845 /* Point to the last child in the last root subtree. */
846 prev
= TAILQ_LAST(&mtd
->children
, mode_tree_list
);
847 while (!TAILQ_EMPTY(&prev
->children
))
848 prev
= TAILQ_LAST(&prev
->children
, mode_tree_list
);
854 if (mtd
->searchcb
== NULL
) {
855 if (strstr(mti
->name
, mtd
->search
) != NULL
)
859 if (mtd
->searchcb(mtd
->modedata
, mti
->itemdata
, mtd
->search
))
866 static struct mode_tree_item
*
867 mode_tree_search_forward(struct mode_tree_data
*mtd
)
869 struct mode_tree_item
*mti
, *last
, *next
;
871 if (mtd
->search
== NULL
)
874 mti
= last
= mtd
->line_list
[mtd
->current
].item
;
876 if (!TAILQ_EMPTY(&mti
->children
))
877 mti
= TAILQ_FIRST(&mti
->children
);
878 else if ((next
= TAILQ_NEXT(mti
, entry
)) != NULL
)
885 if ((next
= TAILQ_NEXT(mti
, entry
)) != NULL
) {
892 mti
= TAILQ_FIRST(&mtd
->children
);
896 if (mtd
->searchcb
== NULL
) {
897 if (strstr(mti
->name
, mtd
->search
) != NULL
)
901 if (mtd
->searchcb(mtd
->modedata
, mti
->itemdata
, mtd
->search
))
908 mode_tree_search_set(struct mode_tree_data
*mtd
)
910 struct mode_tree_item
*mti
, *loop
;
913 if (mtd
->search_dir
== MODE_TREE_SEARCH_FORWARD
)
914 mti
= mode_tree_search_forward(mtd
);
916 mti
= mode_tree_search_backward(mtd
);
922 while (loop
!= NULL
) {
927 mode_tree_build(mtd
);
928 mode_tree_set_current(mtd
, tag
);
930 mtd
->wp
->flags
|= PANE_REDRAW
;
934 mode_tree_search_callback(__unused
struct client
*c
, void *data
, const char *s
,
937 struct mode_tree_data
*mtd
= data
;
943 if (s
== NULL
|| *s
== '\0') {
947 mtd
->search
= xstrdup(s
);
948 mode_tree_search_set(mtd
);
954 mode_tree_search_free(void *data
)
956 mode_tree_remove_ref(data
);
960 mode_tree_filter_callback(__unused
struct client
*c
, void *data
, const char *s
,
963 struct mode_tree_data
*mtd
= data
;
968 if (mtd
->filter
!= NULL
)
970 if (s
== NULL
|| *s
== '\0')
973 mtd
->filter
= xstrdup(s
);
975 mode_tree_build(mtd
);
977 mtd
->wp
->flags
|= PANE_REDRAW
;
983 mode_tree_filter_free(void *data
)
985 mode_tree_remove_ref(data
);
989 mode_tree_menu_callback(__unused
struct menu
*menu
, __unused u_int idx
,
990 key_code key
, void *data
)
992 struct mode_tree_menu
*mtm
= data
;
993 struct mode_tree_data
*mtd
= mtm
->data
;
995 if (mtd
->dead
|| key
== KEYC_NONE
)
998 if (mtm
->line
>= mtd
->line_size
)
1000 mtd
->current
= mtm
->line
;
1001 mtd
->menucb(mtd
->modedata
, mtm
->c
, key
);
1004 mode_tree_remove_ref(mtd
);
1009 mode_tree_display_menu(struct mode_tree_data
*mtd
, struct client
*c
, u_int x
,
1010 u_int y
, int outside
)
1012 struct mode_tree_item
*mti
;
1014 const struct menu_item
*items
;
1015 struct mode_tree_menu
*mtm
;
1019 if (mtd
->offset
+ y
> mtd
->line_size
- 1)
1020 line
= mtd
->current
;
1022 line
= mtd
->offset
+ y
;
1023 mti
= mtd
->line_list
[line
].item
;
1027 xasprintf(&title
, "#[align=centre]%s", mti
->name
);
1029 items
= mode_tree_menu_items
;
1030 title
= xstrdup("");
1032 menu
= menu_create(title
);
1033 menu_add_items(menu
, items
, NULL
, c
, NULL
);
1036 mtm
= xmalloc(sizeof *mtm
);
1042 if (x
>= (menu
->width
+ 4) / 2)
1043 x
-= (menu
->width
+ 4) / 2;
1046 if (menu_display(menu
, 0, 0, NULL
, x
, y
, c
, BOX_LINES_DEFAULT
, NULL
,
1047 NULL
, NULL
, NULL
, mode_tree_menu_callback
, mtm
) != 0)
1052 mode_tree_key(struct mode_tree_data
*mtd
, struct client
*c
, key_code
*key
,
1053 struct mouse_event
*m
, u_int
*xp
, u_int
*yp
)
1055 struct mode_tree_line
*line
;
1056 struct mode_tree_item
*current
, *parent
, *mti
;
1060 if (KEYC_IS_MOUSE(*key
) && m
!= NULL
) {
1061 if (cmd_mouse_at(mtd
->wp
, m
, &x
, &y
, 0) != 0) {
1069 if (x
> mtd
->width
|| y
> mtd
->height
) {
1070 if (*key
== KEYC_MOUSEDOWN3_PANE
)
1071 mode_tree_display_menu(mtd
, c
, x
, y
, 1);
1072 if (mtd
->preview
== MODE_TREE_PREVIEW_OFF
)
1076 if (mtd
->offset
+ y
< mtd
->line_size
) {
1077 if (*key
== KEYC_MOUSEDOWN1_PANE
||
1078 *key
== KEYC_MOUSEDOWN3_PANE
||
1079 *key
== KEYC_DOUBLECLICK1_PANE
)
1080 mtd
->current
= mtd
->offset
+ y
;
1081 if (*key
== KEYC_DOUBLECLICK1_PANE
)
1084 if (*key
== KEYC_MOUSEDOWN3_PANE
)
1085 mode_tree_display_menu(mtd
, c
, x
, y
, 0);
1089 if (*key
== KEYC_MOUSEDOWN3_PANE
)
1090 mode_tree_display_menu(mtd
, c
, x
, y
, 0);
1096 line
= &mtd
->line_list
[mtd
->current
];
1097 current
= line
->item
;
1100 for (i
= 0; i
< mtd
->line_size
; i
++) {
1101 if (*key
== mtd
->line_list
[i
].item
->key
) {
1107 if ((u_int
)choice
> mtd
->line_size
- 1) {
1111 mtd
->current
= choice
;
1118 case '\033': /* Escape */
1123 case KEYC_WHEELUP_PANE
:
1125 mode_tree_up(mtd
, 1);
1129 case KEYC_WHEELDOWN_PANE
:
1131 mode_tree_down(mtd
, 1);
1135 for (i
= 0; i
< mtd
->height
; i
++) {
1136 if (mtd
->current
== 0)
1138 mode_tree_up(mtd
, 1);
1143 for (i
= 0; i
< mtd
->height
; i
++) {
1144 if (mtd
->current
== mtd
->line_size
- 1)
1146 mode_tree_down(mtd
, 1);
1156 mtd
->current
= mtd
->line_size
- 1;
1157 if (mtd
->current
> mtd
->height
- 1)
1158 mtd
->offset
= mtd
->current
- mtd
->height
+ 1;
1164 * Do not allow parents and children to both be tagged: untag
1165 * all parents and children of current.
1167 if (current
->no_tag
)
1169 if (!current
->tagged
) {
1170 parent
= current
->parent
;
1171 while (parent
!= NULL
) {
1173 parent
= parent
->parent
;
1175 mode_tree_clear_tagged(¤t
->children
);
1176 current
->tagged
= 1;
1178 current
->tagged
= 0;
1180 mode_tree_down(mtd
, 0);
1183 for (i
= 0; i
< mtd
->line_size
; i
++)
1184 mtd
->line_list
[i
].item
->tagged
= 0;
1187 for (i
= 0; i
< mtd
->line_size
; i
++) {
1188 if ((mtd
->line_list
[i
].item
->parent
== NULL
&&
1189 !mtd
->line_list
[i
].item
->no_tag
) ||
1190 (mtd
->line_list
[i
].item
->parent
!= NULL
&&
1191 mtd
->line_list
[i
].item
->parent
->no_tag
))
1192 mtd
->line_list
[i
].item
->tagged
= 1;
1194 mtd
->line_list
[i
].item
->tagged
= 0;
1198 mtd
->sort_crit
.field
++;
1199 if (mtd
->sort_crit
.field
>= mtd
->sort_size
)
1200 mtd
->sort_crit
.field
= 0;
1201 mode_tree_build(mtd
);
1204 mtd
->sort_crit
.reversed
= !mtd
->sort_crit
.reversed
;
1205 mode_tree_build(mtd
);
1210 if (line
->flat
|| !current
->expanded
)
1211 current
= current
->parent
;
1212 if (current
== NULL
)
1213 mode_tree_up(mtd
, 0);
1215 current
->expanded
= 0;
1216 mtd
->current
= current
->line
;
1217 mode_tree_build(mtd
);
1223 if (line
->flat
|| current
->expanded
)
1224 mode_tree_down(mtd
, 0);
1225 else if (!line
->flat
) {
1226 current
->expanded
= 1;
1227 mode_tree_build(mtd
);
1231 TAILQ_FOREACH(mti
, &mtd
->children
, entry
)
1233 mode_tree_build(mtd
);
1236 TAILQ_FOREACH(mti
, &mtd
->children
, entry
)
1238 mode_tree_build(mtd
);
1244 status_prompt_set(c
, NULL
, "(search) ", "",
1245 mode_tree_search_callback
, mode_tree_search_free
, mtd
,
1246 PROMPT_NOFORMAT
, PROMPT_TYPE_SEARCH
);
1249 mtd
->search_dir
= MODE_TREE_SEARCH_FORWARD
;
1250 mode_tree_search_set(mtd
);
1253 mtd
->search_dir
= MODE_TREE_SEARCH_BACKWARD
;
1254 mode_tree_search_set(mtd
);
1258 status_prompt_set(c
, NULL
, "(filter) ", mtd
->filter
,
1259 mode_tree_filter_callback
, mode_tree_filter_free
, mtd
,
1260 PROMPT_NOFORMAT
, PROMPT_TYPE_SEARCH
);
1263 switch (mtd
->preview
) {
1264 case MODE_TREE_PREVIEW_OFF
:
1265 mtd
->preview
= MODE_TREE_PREVIEW_BIG
;
1267 case MODE_TREE_PREVIEW_NORMAL
:
1268 mtd
->preview
= MODE_TREE_PREVIEW_OFF
;
1270 case MODE_TREE_PREVIEW_BIG
:
1271 mtd
->preview
= MODE_TREE_PREVIEW_NORMAL
;
1274 mode_tree_build(mtd
);
1275 if (mtd
->preview
!= MODE_TREE_PREVIEW_OFF
)
1276 mode_tree_check_selected(mtd
);
1283 mode_tree_run_command(struct client
*c
, struct cmd_find_state
*fs
,
1284 const char *template, const char *name
)
1286 struct cmdq_state
*state
;
1287 char *command
, *error
;
1288 enum cmd_parse_status status
;
1290 command
= cmd_template_replace(template, name
, 1);
1291 if (command
!= NULL
&& *command
!= '\0') {
1292 state
= cmdq_new_state(fs
, NULL
, 0);
1293 status
= cmd_parse_and_append(command
, NULL
, c
, state
, &error
);
1294 if (status
== CMD_PARSE_ERROR
) {
1296 *error
= toupper((u_char
)*error
);
1297 status_message_set(c
, -1, 1, 0, "%s", error
);
1301 cmdq_free_state(state
);