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 struct mode_tree_item
;
29 TAILQ_HEAD(mode_tree_list
, mode_tree_item
);
31 struct mode_tree_data
{
36 struct window_pane
*wp
;
38 const struct menu_item
*menu
;
40 const char **sort_list
;
42 struct mode_tree_sort_criteria sort_crit
;
44 mode_tree_build_cb buildcb
;
45 mode_tree_draw_cb drawcb
;
46 mode_tree_search_cb searchcb
;
47 mode_tree_menu_cb menucb
;
48 mode_tree_height_cb heightcb
;
49 mode_tree_key_cb keycb
;
51 struct mode_tree_list children
;
52 struct mode_tree_list saved
;
54 struct mode_tree_line
*line_list
;
73 struct mode_tree_item
{
74 struct mode_tree_item
*parent
;
92 struct mode_tree_list children
;
93 TAILQ_ENTRY(mode_tree_item
) entry
;
96 struct mode_tree_line
{
97 struct mode_tree_item
*item
;
103 struct mode_tree_menu
{
104 struct mode_tree_data
*data
;
109 static void mode_tree_free_items(struct mode_tree_list
*);
111 static const struct menu_item mode_tree_menu_items
[] = {
112 { "Scroll Left", '<', NULL
},
113 { "Scroll Right", '>', NULL
},
114 { "", KEYC_NONE
, NULL
},
115 { "Cancel", 'q', NULL
},
117 { NULL
, KEYC_NONE
, NULL
}
120 static struct mode_tree_item
*
121 mode_tree_find_item(struct mode_tree_list
*mtl
, uint64_t tag
)
123 struct mode_tree_item
*mti
, *child
;
125 TAILQ_FOREACH(mti
, mtl
, entry
) {
128 child
= mode_tree_find_item(&mti
->children
, tag
);
136 mode_tree_free_item(struct mode_tree_item
*mti
)
138 mode_tree_free_items(&mti
->children
);
140 free((void *)mti
->name
);
141 free((void *)mti
->text
);
142 free((void *)mti
->keystr
);
148 mode_tree_free_items(struct mode_tree_list
*mtl
)
150 struct mode_tree_item
*mti
, *mti1
;
152 TAILQ_FOREACH_SAFE(mti
, mtl
, entry
, mti1
) {
153 TAILQ_REMOVE(mtl
, mti
, entry
);
154 mode_tree_free_item(mti
);
159 mode_tree_check_selected(struct mode_tree_data
*mtd
)
162 * If the current line would now be off screen reset the offset to the
165 if (mtd
->current
> mtd
->height
- 1)
166 mtd
->offset
= mtd
->current
- mtd
->height
+ 1;
170 mode_tree_clear_lines(struct mode_tree_data
*mtd
)
172 free(mtd
->line_list
);
173 mtd
->line_list
= NULL
;
178 mode_tree_build_lines(struct mode_tree_data
*mtd
,
179 struct mode_tree_list
*mtl
, u_int depth
)
181 struct mode_tree_item
*mti
;
182 struct mode_tree_line
*line
;
187 TAILQ_FOREACH(mti
, mtl
, entry
) {
188 mtd
->line_list
= xreallocarray(mtd
->line_list
,
189 mtd
->line_size
+ 1, sizeof *mtd
->line_list
);
191 line
= &mtd
->line_list
[mtd
->line_size
++];
194 line
->last
= (mti
== TAILQ_LAST(mtl
, mode_tree_list
));
196 mti
->line
= (mtd
->line_size
- 1);
197 if (!TAILQ_EMPTY(&mti
->children
))
200 mode_tree_build_lines(mtd
, &mti
->children
, depth
+ 1);
202 if (mtd
->keycb
!= NULL
) {
203 mti
->key
= mtd
->keycb(mtd
->modedata
, mti
->itemdata
,
205 if (mti
->key
== KEYC_UNKNOWN
)
206 mti
->key
= KEYC_NONE
;
207 } else if (mti
->line
< 10)
208 mti
->key
= '0' + mti
->line
;
209 else if (mti
->line
< 36)
210 mti
->key
= KEYC_META
|('a' + mti
->line
- 10);
212 mti
->key
= KEYC_NONE
;
213 if (mti
->key
!= KEYC_NONE
) {
214 mti
->keystr
= xstrdup(key_string_lookup_key(mti
->key
,
216 mti
->keylen
= strlen(mti
->keystr
);
222 TAILQ_FOREACH(mti
, mtl
, entry
) {
223 for (i
= 0; i
< mtd
->line_size
; i
++) {
224 line
= &mtd
->line_list
[i
];
225 if (line
->item
== mti
)
232 mode_tree_clear_tagged(struct mode_tree_list
*mtl
)
234 struct mode_tree_item
*mti
;
236 TAILQ_FOREACH(mti
, mtl
, entry
) {
238 mode_tree_clear_tagged(&mti
->children
);
243 mode_tree_up(struct mode_tree_data
*mtd
, int wrap
)
245 if (mtd
->current
== 0) {
247 mtd
->current
= mtd
->line_size
- 1;
248 if (mtd
->line_size
>= mtd
->height
)
249 mtd
->offset
= mtd
->line_size
- mtd
->height
;
253 if (mtd
->current
< mtd
->offset
)
259 mode_tree_down(struct mode_tree_data
*mtd
, int wrap
)
261 if (mtd
->current
== mtd
->line_size
- 1) {
268 if (mtd
->current
> mtd
->offset
+ mtd
->height
- 1)
274 mode_tree_get_current(struct mode_tree_data
*mtd
)
276 return (mtd
->line_list
[mtd
->current
].item
->itemdata
);
280 mode_tree_get_current_name(struct mode_tree_data
*mtd
)
282 return (mtd
->line_list
[mtd
->current
].item
->name
);
286 mode_tree_expand_current(struct mode_tree_data
*mtd
)
288 if (!mtd
->line_list
[mtd
->current
].item
->expanded
) {
289 mtd
->line_list
[mtd
->current
].item
->expanded
= 1;
290 mode_tree_build(mtd
);
295 mode_tree_collapse_current(struct mode_tree_data
*mtd
)
297 if (mtd
->line_list
[mtd
->current
].item
->expanded
) {
298 mtd
->line_list
[mtd
->current
].item
->expanded
= 0;
299 mode_tree_build(mtd
);
304 mode_tree_get_tag(struct mode_tree_data
*mtd
, uint64_t tag
, u_int
*found
)
308 for (i
= 0; i
< mtd
->line_size
; i
++) {
309 if (mtd
->line_list
[i
].item
->tag
== tag
)
312 if (i
!= mtd
->line_size
) {
320 mode_tree_expand(struct mode_tree_data
*mtd
, uint64_t tag
)
324 if (!mode_tree_get_tag(mtd
, tag
, &found
))
326 if (!mtd
->line_list
[found
].item
->expanded
) {
327 mtd
->line_list
[found
].item
->expanded
= 1;
328 mode_tree_build(mtd
);
333 mode_tree_set_current(struct mode_tree_data
*mtd
, uint64_t tag
)
337 if (mode_tree_get_tag(mtd
, tag
, &found
)) {
338 mtd
->current
= found
;
339 if (mtd
->current
> mtd
->height
- 1)
340 mtd
->offset
= mtd
->current
- mtd
->height
+ 1;
351 mode_tree_count_tagged(struct mode_tree_data
*mtd
)
353 struct mode_tree_item
*mti
;
357 for (i
= 0; i
< mtd
->line_size
; i
++) {
358 mti
= mtd
->line_list
[i
].item
;
366 mode_tree_each_tagged(struct mode_tree_data
*mtd
, mode_tree_each_cb cb
,
367 struct client
*c
, key_code key
, int current
)
369 struct mode_tree_item
*mti
;
374 for (i
= 0; i
< mtd
->line_size
; i
++) {
375 mti
= mtd
->line_list
[i
].item
;
378 cb(mtd
->modedata
, mti
->itemdata
, c
, key
);
381 if (!fired
&& current
) {
382 mti
= mtd
->line_list
[mtd
->current
].item
;
383 cb(mtd
->modedata
, mti
->itemdata
, c
, key
);
387 struct mode_tree_data
*
388 mode_tree_start(struct window_pane
*wp
, struct args
*args
,
389 mode_tree_build_cb buildcb
, mode_tree_draw_cb drawcb
,
390 mode_tree_search_cb searchcb
, mode_tree_menu_cb menucb
,
391 mode_tree_height_cb heightcb
, mode_tree_key_cb keycb
, void *modedata
,
392 const struct menu_item
*menu
, const char **sort_list
, u_int sort_size
,
395 struct mode_tree_data
*mtd
;
399 mtd
= xcalloc(1, sizeof *mtd
);
403 mtd
->modedata
= modedata
;
406 mtd
->sort_list
= sort_list
;
407 mtd
->sort_size
= sort_size
;
409 mtd
->preview
= !args_has(args
, 'N');
411 sort
= args_get(args
, 'O');
413 for (i
= 0; i
< sort_size
; i
++) {
414 if (strcasecmp(sort
, sort_list
[i
]) == 0)
415 mtd
->sort_crit
.field
= i
;
418 mtd
->sort_crit
.reversed
= args_has(args
, 'r');
420 if (args_has(args
, 'f'))
421 mtd
->filter
= xstrdup(args_get(args
, 'f'));
425 mtd
->buildcb
= buildcb
;
426 mtd
->drawcb
= drawcb
;
427 mtd
->searchcb
= searchcb
;
428 mtd
->menucb
= menucb
;
429 mtd
->heightcb
= heightcb
;
432 TAILQ_INIT(&mtd
->children
);
435 screen_init(*s
, screen_size_x(&wp
->base
), screen_size_y(&wp
->base
), 0);
436 (*s
)->mode
&= ~MODE_CURSOR
;
442 mode_tree_zoom(struct mode_tree_data
*mtd
, struct args
*args
)
444 struct window_pane
*wp
= mtd
->wp
;
446 if (args_has(args
, 'Z')) {
447 mtd
->zoomed
= (wp
->window
->flags
& WINDOW_ZOOMED
);
448 if (!mtd
->zoomed
&& window_zoom(wp
) == 0)
449 server_redraw_window(wp
->window
);
455 mode_tree_set_height(struct mode_tree_data
*mtd
)
457 struct screen
*s
= &mtd
->screen
;
460 if (mtd
->heightcb
!= NULL
) {
461 height
= mtd
->heightcb(mtd
, screen_size_y(s
));
462 if (height
< screen_size_y(s
))
463 mtd
->height
= screen_size_y(s
) - height
;
465 mtd
->height
= (screen_size_y(s
) / 3) * 2;
466 if (mtd
->height
> mtd
->line_size
)
467 mtd
->height
= screen_size_y(s
) / 2;
469 if (mtd
->height
< 10)
470 mtd
->height
= screen_size_y(s
);
471 if (screen_size_y(s
) - mtd
->height
< 2)
472 mtd
->height
= screen_size_y(s
);
476 mode_tree_build(struct mode_tree_data
*mtd
)
478 struct screen
*s
= &mtd
->screen
;
481 if (mtd
->line_list
!= NULL
)
482 tag
= mtd
->line_list
[mtd
->current
].item
->tag
;
486 TAILQ_CONCAT(&mtd
->saved
, &mtd
->children
, entry
);
487 TAILQ_INIT(&mtd
->children
);
489 mtd
->buildcb(mtd
->modedata
, &mtd
->sort_crit
, &tag
, mtd
->filter
);
490 mtd
->no_matches
= TAILQ_EMPTY(&mtd
->children
);
492 mtd
->buildcb(mtd
->modedata
, &mtd
->sort_crit
, &tag
, NULL
);
494 mode_tree_free_items(&mtd
->saved
);
495 TAILQ_INIT(&mtd
->saved
);
497 mode_tree_clear_lines(mtd
);
498 mode_tree_build_lines(mtd
, &mtd
->children
, 0);
500 if (tag
== UINT64_MAX
)
501 tag
= mtd
->line_list
[mtd
->current
].item
->tag
;
502 mode_tree_set_current(mtd
, tag
);
504 mtd
->width
= screen_size_x(s
);
506 mode_tree_set_height(mtd
);
508 mtd
->height
= screen_size_y(s
);
509 mode_tree_check_selected(mtd
);
513 mode_tree_remove_ref(struct mode_tree_data
*mtd
)
515 if (--mtd
->references
== 0)
520 mode_tree_free(struct mode_tree_data
*mtd
)
522 struct window_pane
*wp
= mtd
->wp
;
524 if (mtd
->zoomed
== 0)
525 server_unzoom_window(wp
->window
);
527 mode_tree_free_items(&mtd
->children
);
528 mode_tree_clear_lines(mtd
);
529 screen_free(&mtd
->screen
);
535 mode_tree_remove_ref(mtd
);
539 mode_tree_resize(struct mode_tree_data
*mtd
, u_int sx
, u_int sy
)
541 struct screen
*s
= &mtd
->screen
;
543 screen_resize(s
, sx
, sy
, 0);
545 mode_tree_build(mtd
);
548 mtd
->wp
->flags
|= PANE_REDRAW
;
551 struct mode_tree_item
*
552 mode_tree_add(struct mode_tree_data
*mtd
, struct mode_tree_item
*parent
,
553 void *itemdata
, uint64_t tag
, const char *name
, const char *text
,
556 struct mode_tree_item
*mti
, *saved
;
558 log_debug("%s: %llu, %s %s", __func__
, (unsigned long long)tag
,
559 name
, (text
== NULL
? "" : text
));
561 mti
= xcalloc(1, sizeof *mti
);
562 mti
->parent
= parent
;
563 mti
->itemdata
= itemdata
;
566 mti
->name
= xstrdup(name
);
568 mti
->text
= xstrdup(text
);
570 saved
= mode_tree_find_item(&mtd
->saved
, tag
);
572 if (parent
== NULL
|| parent
->expanded
)
573 mti
->tagged
= saved
->tagged
;
574 mti
->expanded
= saved
->expanded
;
575 } else if (expanded
== -1)
578 mti
->expanded
= expanded
;
580 TAILQ_INIT(&mti
->children
);
583 TAILQ_INSERT_TAIL(&parent
->children
, mti
, entry
);
585 TAILQ_INSERT_TAIL(&mtd
->children
, mti
, entry
);
591 mode_tree_draw_as_parent(struct mode_tree_item
*mti
)
593 mti
->draw_as_parent
= 1;
597 mode_tree_no_tag(struct mode_tree_item
*mti
)
603 mode_tree_remove(struct mode_tree_data
*mtd
, struct mode_tree_item
*mti
)
605 struct mode_tree_item
*parent
= mti
->parent
;
608 TAILQ_REMOVE(&parent
->children
, mti
, entry
);
610 TAILQ_REMOVE(&mtd
->children
, mti
, entry
);
611 mode_tree_free_item(mti
);
615 mode_tree_draw(struct mode_tree_data
*mtd
)
617 struct window_pane
*wp
= mtd
->wp
;
618 struct screen
*s
= &mtd
->screen
;
619 struct mode_tree_line
*line
;
620 struct mode_tree_item
*mti
;
621 struct options
*oo
= wp
->window
->options
;
622 struct screen_write_ctx ctx
;
623 struct grid_cell gc0
, gc
;
624 u_int w
, h
, i
, j
, sy
, box_x
, box_y
, width
;
625 char *text
, *start
, *key
;
626 const char *tag
, *symbol
;
630 if (mtd
->line_size
== 0)
633 memcpy(&gc0
, &grid_default_cell
, sizeof gc0
);
634 memcpy(&gc
, &grid_default_cell
, sizeof gc
);
635 style_apply(&gc
, oo
, "mode-style", NULL
);
640 screen_write_start(&ctx
, s
);
641 screen_write_clearscreen(&ctx
, 8);
644 for (i
= 0; i
< mtd
->line_size
; i
++) {
645 mti
= mtd
->line_list
[i
].item
;
646 if (mti
->key
== KEYC_NONE
)
648 if ((int)mti
->keylen
+ 3 > keylen
)
649 keylen
= mti
->keylen
+ 3;
652 for (i
= 0; i
< mtd
->line_size
; i
++) {
655 if (i
> mtd
->offset
+ h
- 1)
657 line
= &mtd
->line_list
[i
];
660 screen_write_cursormove(&ctx
, 0, i
- mtd
->offset
, 0);
662 pad
= keylen
- 2 - mti
->keylen
;
663 if (mti
->key
!= KEYC_NONE
)
664 xasprintf(&key
, "(%s)%*s", mti
->keystr
, pad
, "");
670 else if (TAILQ_EMPTY(&mti
->children
))
672 else if (mti
->expanded
)
677 if (line
->depth
== 0)
678 start
= xstrdup(symbol
);
680 size
= (4 * line
->depth
) + 32;
682 start
= xcalloc(1, size
);
683 for (j
= 1; j
< line
->depth
; j
++) {
684 if (mti
->parent
!= NULL
&&
685 mtd
->line_list
[mti
->parent
->line
].last
)
686 strlcat(start
, " ", size
);
688 strlcat(start
, "\001x\001 ", size
);
691 strlcat(start
, "\001mq\001> ", size
);
693 strlcat(start
, "\001tq\001> ", size
);
694 strlcat(start
, symbol
, size
);
701 xasprintf(&text
, "%-*s%s%s%s%s", keylen
, key
, start
, mti
->name
,
702 tag
, (mti
->text
!= NULL
) ? ": " : "" );
703 width
= utf8_cstrwidth(text
);
709 gc
.attr
^= GRID_ATTR_BRIGHT
;
710 gc0
.attr
^= GRID_ATTR_BRIGHT
;
713 if (i
!= mtd
->current
) {
714 screen_write_clearendofline(&ctx
, 8);
715 screen_write_nputs(&ctx
, w
, &gc0
, "%s", text
);
716 if (mti
->text
!= NULL
) {
717 format_draw(&ctx
, &gc0
, w
- width
, mti
->text
,
721 screen_write_clearendofline(&ctx
, gc
.bg
);
722 screen_write_nputs(&ctx
, w
, &gc
, "%s", text
);
723 if (mti
->text
!= NULL
) {
724 format_draw(&ctx
, &gc
, w
- width
, mti
->text
,
732 gc
.attr
^= GRID_ATTR_BRIGHT
;
733 gc0
.attr
^= GRID_ATTR_BRIGHT
;
737 sy
= screen_size_y(s
);
738 if (!mtd
->preview
|| sy
<= 4 || h
<= 4 || sy
- h
<= 4 || w
<= 4)
741 line
= &mtd
->line_list
[mtd
->current
];
743 if (mti
->draw_as_parent
)
746 screen_write_cursormove(&ctx
, 0, h
, 0);
747 screen_write_box(&ctx
, w
, sy
- h
, BOX_LINES_DEFAULT
, NULL
, NULL
);
749 if (mtd
->sort_list
!= NULL
) {
750 xasprintf(&text
, " %s (sort: %s%s)", mti
->name
,
751 mtd
->sort_list
[mtd
->sort_crit
.field
],
752 mtd
->sort_crit
.reversed
? ", reversed" : "");
754 xasprintf(&text
, " %s", mti
->name
);
755 if (w
- 2 >= strlen(text
)) {
756 screen_write_cursormove(&ctx
, 1, h
, 0);
757 screen_write_puts(&ctx
, &gc0
, "%s", text
);
760 n
= (sizeof "no matches") - 1;
762 n
= (sizeof "active") - 1;
763 if (mtd
->filter
!= NULL
&& w
- 2 >= strlen(text
) + 10 + n
+ 2) {
764 screen_write_puts(&ctx
, &gc0
, " (filter: ");
766 screen_write_puts(&ctx
, &gc
, "no matches");
768 screen_write_puts(&ctx
, &gc0
, "active");
769 screen_write_puts(&ctx
, &gc0
, ") ");
771 screen_write_puts(&ctx
, &gc0
, " ");
778 if (box_x
!= 0 && box_y
!= 0) {
779 screen_write_cursormove(&ctx
, 2, h
+ 1, 0);
780 mtd
->drawcb(mtd
->modedata
, mti
->itemdata
, &ctx
, box_x
, box_y
);
784 screen_write_cursormove(&ctx
, 0, mtd
->current
- mtd
->offset
, 0);
785 screen_write_stop(&ctx
);
788 static struct mode_tree_item
*
789 mode_tree_search_for(struct mode_tree_data
*mtd
)
791 struct mode_tree_item
*mti
, *last
, *next
;
793 if (mtd
->search
== NULL
)
796 mti
= last
= mtd
->line_list
[mtd
->current
].item
;
798 if (!TAILQ_EMPTY(&mti
->children
))
799 mti
= TAILQ_FIRST(&mti
->children
);
800 else if ((next
= TAILQ_NEXT(mti
, entry
)) != NULL
)
807 if ((next
= TAILQ_NEXT(mti
, entry
)) != NULL
) {
814 mti
= TAILQ_FIRST(&mtd
->children
);
818 if (mtd
->searchcb
== NULL
) {
819 if (strstr(mti
->name
, mtd
->search
) != NULL
)
823 if (mtd
->searchcb(mtd
->modedata
, mti
->itemdata
, mtd
->search
))
830 mode_tree_search_set(struct mode_tree_data
*mtd
)
832 struct mode_tree_item
*mti
, *loop
;
835 mti
= mode_tree_search_for(mtd
);
841 while (loop
!= NULL
) {
846 mode_tree_build(mtd
);
847 mode_tree_set_current(mtd
, tag
);
849 mtd
->wp
->flags
|= PANE_REDRAW
;
853 mode_tree_search_callback(__unused
struct client
*c
, void *data
, const char *s
,
856 struct mode_tree_data
*mtd
= data
;
862 if (s
== NULL
|| *s
== '\0') {
866 mtd
->search
= xstrdup(s
);
867 mode_tree_search_set(mtd
);
873 mode_tree_search_free(void *data
)
875 mode_tree_remove_ref(data
);
879 mode_tree_filter_callback(__unused
struct client
*c
, void *data
, const char *s
,
882 struct mode_tree_data
*mtd
= data
;
887 if (mtd
->filter
!= NULL
)
889 if (s
== NULL
|| *s
== '\0')
892 mtd
->filter
= xstrdup(s
);
894 mode_tree_build(mtd
);
896 mtd
->wp
->flags
|= PANE_REDRAW
;
902 mode_tree_filter_free(void *data
)
904 mode_tree_remove_ref(data
);
908 mode_tree_menu_callback(__unused
struct menu
*menu
, __unused u_int idx
,
909 key_code key
, void *data
)
911 struct mode_tree_menu
*mtm
= data
;
912 struct mode_tree_data
*mtd
= mtm
->data
;
914 if (mtd
->dead
|| key
== KEYC_NONE
)
917 if (mtm
->line
>= mtd
->line_size
)
919 mtd
->current
= mtm
->line
;
920 mtd
->menucb(mtd
->modedata
, mtm
->c
, key
);
923 mode_tree_remove_ref(mtd
);
928 mode_tree_display_menu(struct mode_tree_data
*mtd
, struct client
*c
, u_int x
,
929 u_int y
, int outside
)
931 struct mode_tree_item
*mti
;
933 const struct menu_item
*items
;
934 struct mode_tree_menu
*mtm
;
938 if (mtd
->offset
+ y
> mtd
->line_size
- 1)
941 line
= mtd
->offset
+ y
;
942 mti
= mtd
->line_list
[line
].item
;
946 xasprintf(&title
, "#[align=centre]%s", mti
->name
);
948 items
= mode_tree_menu_items
;
951 menu
= menu_create(title
);
952 menu_add_items(menu
, items
, NULL
, c
, NULL
);
955 mtm
= xmalloc(sizeof *mtm
);
961 if (x
>= (menu
->width
+ 4) / 2)
962 x
-= (menu
->width
+ 4) / 2;
965 if (menu_display(menu
, 0, NULL
, x
, y
, c
, NULL
, mode_tree_menu_callback
,
971 mode_tree_key(struct mode_tree_data
*mtd
, struct client
*c
, key_code
*key
,
972 struct mouse_event
*m
, u_int
*xp
, u_int
*yp
)
974 struct mode_tree_line
*line
;
975 struct mode_tree_item
*current
, *parent
, *mti
;
979 if (KEYC_IS_MOUSE(*key
) && m
!= NULL
) {
980 if (cmd_mouse_at(mtd
->wp
, m
, &x
, &y
, 0) != 0) {
988 if (x
> mtd
->width
|| y
> mtd
->height
) {
989 if (*key
== KEYC_MOUSEDOWN3_PANE
)
990 mode_tree_display_menu(mtd
, c
, x
, y
, 1);
995 if (mtd
->offset
+ y
< mtd
->line_size
) {
996 if (*key
== KEYC_MOUSEDOWN1_PANE
||
997 *key
== KEYC_MOUSEDOWN3_PANE
||
998 *key
== KEYC_DOUBLECLICK1_PANE
)
999 mtd
->current
= mtd
->offset
+ y
;
1000 if (*key
== KEYC_DOUBLECLICK1_PANE
)
1003 if (*key
== KEYC_MOUSEDOWN3_PANE
)
1004 mode_tree_display_menu(mtd
, c
, x
, y
, 0);
1008 if (*key
== KEYC_MOUSEDOWN3_PANE
)
1009 mode_tree_display_menu(mtd
, c
, x
, y
, 0);
1015 line
= &mtd
->line_list
[mtd
->current
];
1016 current
= line
->item
;
1019 for (i
= 0; i
< mtd
->line_size
; i
++) {
1020 if (*key
== mtd
->line_list
[i
].item
->key
) {
1026 if ((u_int
)choice
> mtd
->line_size
- 1) {
1030 mtd
->current
= choice
;
1037 case '\033': /* Escape */
1038 case '\007': /* C-g */
1042 case KEYC_WHEELUP_PANE
:
1043 case '\020': /* C-p */
1044 mode_tree_up(mtd
, 1);
1048 case KEYC_WHEELDOWN_PANE
:
1049 case '\016': /* C-n */
1050 mode_tree_down(mtd
, 1);
1053 case '\002': /* C-b */
1054 for (i
= 0; i
< mtd
->height
; i
++) {
1055 if (mtd
->current
== 0)
1057 mode_tree_up(mtd
, 1);
1061 case '\006': /* C-f */
1062 for (i
= 0; i
< mtd
->height
; i
++) {
1063 if (mtd
->current
== mtd
->line_size
- 1)
1065 mode_tree_down(mtd
, 1);
1075 mtd
->current
= mtd
->line_size
- 1;
1076 if (mtd
->current
> mtd
->height
- 1)
1077 mtd
->offset
= mtd
->current
- mtd
->height
+ 1;
1083 * Do not allow parents and children to both be tagged: untag
1084 * all parents and children of current.
1086 if (current
->no_tag
)
1088 if (!current
->tagged
) {
1089 parent
= current
->parent
;
1090 while (parent
!= NULL
) {
1092 parent
= parent
->parent
;
1094 mode_tree_clear_tagged(¤t
->children
);
1095 current
->tagged
= 1;
1097 current
->tagged
= 0;
1099 mode_tree_down(mtd
, 0);
1102 for (i
= 0; i
< mtd
->line_size
; i
++)
1103 mtd
->line_list
[i
].item
->tagged
= 0;
1105 case '\024': /* C-t */
1106 for (i
= 0; i
< mtd
->line_size
; i
++) {
1107 if ((mtd
->line_list
[i
].item
->parent
== NULL
&&
1108 !mtd
->line_list
[i
].item
->no_tag
) ||
1109 (mtd
->line_list
[i
].item
->parent
!= NULL
&&
1110 mtd
->line_list
[i
].item
->parent
->no_tag
))
1111 mtd
->line_list
[i
].item
->tagged
= 1;
1113 mtd
->line_list
[i
].item
->tagged
= 0;
1117 mtd
->sort_crit
.field
++;
1118 if (mtd
->sort_crit
.field
>= mtd
->sort_size
)
1119 mtd
->sort_crit
.field
= 0;
1120 mode_tree_build(mtd
);
1123 mtd
->sort_crit
.reversed
= !mtd
->sort_crit
.reversed
;
1124 mode_tree_build(mtd
);
1129 if (line
->flat
|| !current
->expanded
)
1130 current
= current
->parent
;
1131 if (current
== NULL
)
1132 mode_tree_up(mtd
, 0);
1134 current
->expanded
= 0;
1135 mtd
->current
= current
->line
;
1136 mode_tree_build(mtd
);
1142 if (line
->flat
|| current
->expanded
)
1143 mode_tree_down(mtd
, 0);
1144 else if (!line
->flat
) {
1145 current
->expanded
= 1;
1146 mode_tree_build(mtd
);
1150 TAILQ_FOREACH(mti
, &mtd
->children
, entry
)
1152 mode_tree_build(mtd
);
1155 TAILQ_FOREACH(mti
, &mtd
->children
, entry
)
1157 mode_tree_build(mtd
);
1161 case '\023': /* C-s */
1163 status_prompt_set(c
, NULL
, "(search) ", "",
1164 mode_tree_search_callback
, mode_tree_search_free
, mtd
,
1165 PROMPT_NOFORMAT
, PROMPT_TYPE_SEARCH
);
1168 mode_tree_search_set(mtd
);
1172 status_prompt_set(c
, NULL
, "(filter) ", mtd
->filter
,
1173 mode_tree_filter_callback
, mode_tree_filter_free
, mtd
,
1174 PROMPT_NOFORMAT
, PROMPT_TYPE_SEARCH
);
1177 mtd
->preview
= !mtd
->preview
;
1178 mode_tree_build(mtd
);
1180 mode_tree_check_selected(mtd
);
1187 mode_tree_run_command(struct client
*c
, struct cmd_find_state
*fs
,
1188 const char *template, const char *name
)
1190 struct cmdq_state
*state
;
1191 char *command
, *error
;
1192 enum cmd_parse_status status
;
1194 command
= cmd_template_replace(template, name
, 1);
1195 if (command
!= NULL
&& *command
!= '\0') {
1196 state
= cmdq_new_state(fs
, NULL
, 0);
1197 status
= cmd_parse_and_append(command
, NULL
, c
, state
, &error
);
1198 if (status
== CMD_PARSE_ERROR
) {
1200 *error
= toupper((u_char
)*error
);
1201 status_message_set(c
, -1, 1, 0, "%s", error
);
1205 cmdq_free_state(state
);