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 struct mode_tree_item
;
34 TAILQ_HEAD(mode_tree_list
, mode_tree_item
);
36 struct mode_tree_data
{
41 struct window_pane
*wp
;
43 const struct menu_item
*menu
;
45 const char **sort_list
;
47 struct mode_tree_sort_criteria sort_crit
;
49 mode_tree_build_cb buildcb
;
50 mode_tree_draw_cb drawcb
;
51 mode_tree_search_cb searchcb
;
52 mode_tree_menu_cb menucb
;
53 mode_tree_height_cb heightcb
;
54 mode_tree_key_cb keycb
;
56 struct mode_tree_list children
;
57 struct mode_tree_list saved
;
59 struct mode_tree_line
*line_list
;
76 enum mode_tree_search_dir search_dir
;
79 struct mode_tree_item
{
80 struct mode_tree_item
*parent
;
98 struct mode_tree_list children
;
99 TAILQ_ENTRY(mode_tree_item
) entry
;
102 struct mode_tree_line
{
103 struct mode_tree_item
*item
;
109 struct mode_tree_menu
{
110 struct mode_tree_data
*data
;
115 static void mode_tree_free_items(struct mode_tree_list
*);
117 static const struct menu_item mode_tree_menu_items
[] = {
118 { "Scroll Left", '<', NULL
},
119 { "Scroll Right", '>', NULL
},
120 { "", KEYC_NONE
, NULL
},
121 { "Cancel", 'q', NULL
},
123 { NULL
, KEYC_NONE
, NULL
}
126 static struct mode_tree_item
*
127 mode_tree_find_item(struct mode_tree_list
*mtl
, uint64_t tag
)
129 struct mode_tree_item
*mti
, *child
;
131 TAILQ_FOREACH(mti
, mtl
, entry
) {
134 child
= mode_tree_find_item(&mti
->children
, tag
);
142 mode_tree_free_item(struct mode_tree_item
*mti
)
144 mode_tree_free_items(&mti
->children
);
146 free((void *)mti
->name
);
147 free((void *)mti
->text
);
148 free((void *)mti
->keystr
);
154 mode_tree_free_items(struct mode_tree_list
*mtl
)
156 struct mode_tree_item
*mti
, *mti1
;
158 TAILQ_FOREACH_SAFE(mti
, mtl
, entry
, mti1
) {
159 TAILQ_REMOVE(mtl
, mti
, entry
);
160 mode_tree_free_item(mti
);
165 mode_tree_check_selected(struct mode_tree_data
*mtd
)
168 * If the current line would now be off screen reset the offset to the
171 if (mtd
->current
> mtd
->height
- 1)
172 mtd
->offset
= mtd
->current
- mtd
->height
+ 1;
176 mode_tree_clear_lines(struct mode_tree_data
*mtd
)
178 free(mtd
->line_list
);
179 mtd
->line_list
= NULL
;
184 mode_tree_build_lines(struct mode_tree_data
*mtd
,
185 struct mode_tree_list
*mtl
, u_int depth
)
187 struct mode_tree_item
*mti
;
188 struct mode_tree_line
*line
;
193 TAILQ_FOREACH(mti
, mtl
, entry
) {
194 mtd
->line_list
= xreallocarray(mtd
->line_list
,
195 mtd
->line_size
+ 1, sizeof *mtd
->line_list
);
197 line
= &mtd
->line_list
[mtd
->line_size
++];
200 line
->last
= (mti
== TAILQ_LAST(mtl
, mode_tree_list
));
202 mti
->line
= (mtd
->line_size
- 1);
203 if (!TAILQ_EMPTY(&mti
->children
))
206 mode_tree_build_lines(mtd
, &mti
->children
, depth
+ 1);
208 if (mtd
->keycb
!= NULL
) {
209 mti
->key
= mtd
->keycb(mtd
->modedata
, mti
->itemdata
,
211 if (mti
->key
== KEYC_UNKNOWN
)
212 mti
->key
= KEYC_NONE
;
213 } else if (mti
->line
< 10)
214 mti
->key
= '0' + mti
->line
;
215 else if (mti
->line
< 36)
216 mti
->key
= KEYC_META
|('a' + mti
->line
- 10);
218 mti
->key
= KEYC_NONE
;
219 if (mti
->key
!= KEYC_NONE
) {
220 mti
->keystr
= xstrdup(key_string_lookup_key(mti
->key
,
222 mti
->keylen
= strlen(mti
->keystr
);
228 TAILQ_FOREACH(mti
, mtl
, entry
) {
229 for (i
= 0; i
< mtd
->line_size
; i
++) {
230 line
= &mtd
->line_list
[i
];
231 if (line
->item
== mti
)
238 mode_tree_clear_tagged(struct mode_tree_list
*mtl
)
240 struct mode_tree_item
*mti
;
242 TAILQ_FOREACH(mti
, mtl
, entry
) {
244 mode_tree_clear_tagged(&mti
->children
);
249 mode_tree_up(struct mode_tree_data
*mtd
, int wrap
)
251 if (mtd
->current
== 0) {
253 mtd
->current
= mtd
->line_size
- 1;
254 if (mtd
->line_size
>= mtd
->height
)
255 mtd
->offset
= mtd
->line_size
- mtd
->height
;
259 if (mtd
->current
< mtd
->offset
)
265 mode_tree_down(struct mode_tree_data
*mtd
, int wrap
)
267 if (mtd
->current
== mtd
->line_size
- 1) {
275 if (mtd
->current
> mtd
->offset
+ mtd
->height
- 1)
282 mode_tree_get_current(struct mode_tree_data
*mtd
)
284 return (mtd
->line_list
[mtd
->current
].item
->itemdata
);
288 mode_tree_get_current_name(struct mode_tree_data
*mtd
)
290 return (mtd
->line_list
[mtd
->current
].item
->name
);
294 mode_tree_expand_current(struct mode_tree_data
*mtd
)
296 if (!mtd
->line_list
[mtd
->current
].item
->expanded
) {
297 mtd
->line_list
[mtd
->current
].item
->expanded
= 1;
298 mode_tree_build(mtd
);
303 mode_tree_collapse_current(struct mode_tree_data
*mtd
)
305 if (mtd
->line_list
[mtd
->current
].item
->expanded
) {
306 mtd
->line_list
[mtd
->current
].item
->expanded
= 0;
307 mode_tree_build(mtd
);
312 mode_tree_get_tag(struct mode_tree_data
*mtd
, uint64_t tag
, u_int
*found
)
316 for (i
= 0; i
< mtd
->line_size
; i
++) {
317 if (mtd
->line_list
[i
].item
->tag
== tag
)
320 if (i
!= mtd
->line_size
) {
328 mode_tree_expand(struct mode_tree_data
*mtd
, uint64_t tag
)
332 if (!mode_tree_get_tag(mtd
, tag
, &found
))
334 if (!mtd
->line_list
[found
].item
->expanded
) {
335 mtd
->line_list
[found
].item
->expanded
= 1;
336 mode_tree_build(mtd
);
341 mode_tree_set_current(struct mode_tree_data
*mtd
, uint64_t tag
)
345 if (mode_tree_get_tag(mtd
, tag
, &found
)) {
346 mtd
->current
= found
;
347 if (mtd
->current
> mtd
->height
- 1)
348 mtd
->offset
= mtd
->current
- mtd
->height
+ 1;
359 mode_tree_count_tagged(struct mode_tree_data
*mtd
)
361 struct mode_tree_item
*mti
;
365 for (i
= 0; i
< mtd
->line_size
; i
++) {
366 mti
= mtd
->line_list
[i
].item
;
374 mode_tree_each_tagged(struct mode_tree_data
*mtd
, mode_tree_each_cb cb
,
375 struct client
*c
, key_code key
, int current
)
377 struct mode_tree_item
*mti
;
382 for (i
= 0; i
< mtd
->line_size
; i
++) {
383 mti
= mtd
->line_list
[i
].item
;
386 cb(mtd
->modedata
, mti
->itemdata
, c
, key
);
389 if (!fired
&& current
) {
390 mti
= mtd
->line_list
[mtd
->current
].item
;
391 cb(mtd
->modedata
, mti
->itemdata
, c
, key
);
395 struct mode_tree_data
*
396 mode_tree_start(struct window_pane
*wp
, struct args
*args
,
397 mode_tree_build_cb buildcb
, mode_tree_draw_cb drawcb
,
398 mode_tree_search_cb searchcb
, mode_tree_menu_cb menucb
,
399 mode_tree_height_cb heightcb
, mode_tree_key_cb keycb
, void *modedata
,
400 const struct menu_item
*menu
, const char **sort_list
, u_int sort_size
,
403 struct mode_tree_data
*mtd
;
407 mtd
= xcalloc(1, sizeof *mtd
);
411 mtd
->modedata
= modedata
;
414 mtd
->sort_list
= sort_list
;
415 mtd
->sort_size
= sort_size
;
417 mtd
->preview
= !args_has(args
, 'N');
419 sort
= args_get(args
, 'O');
421 for (i
= 0; i
< sort_size
; i
++) {
422 if (strcasecmp(sort
, sort_list
[i
]) == 0)
423 mtd
->sort_crit
.field
= i
;
426 mtd
->sort_crit
.reversed
= args_has(args
, 'r');
428 if (args_has(args
, 'f'))
429 mtd
->filter
= xstrdup(args_get(args
, 'f'));
433 mtd
->buildcb
= buildcb
;
434 mtd
->drawcb
= drawcb
;
435 mtd
->searchcb
= searchcb
;
436 mtd
->menucb
= menucb
;
437 mtd
->heightcb
= heightcb
;
440 TAILQ_INIT(&mtd
->children
);
443 screen_init(*s
, screen_size_x(&wp
->base
), screen_size_y(&wp
->base
), 0);
444 (*s
)->mode
&= ~MODE_CURSOR
;
450 mode_tree_zoom(struct mode_tree_data
*mtd
, struct args
*args
)
452 struct window_pane
*wp
= mtd
->wp
;
454 if (args_has(args
, 'Z')) {
455 mtd
->zoomed
= (wp
->window
->flags
& WINDOW_ZOOMED
);
456 if (!mtd
->zoomed
&& window_zoom(wp
) == 0)
457 server_redraw_window(wp
->window
);
463 mode_tree_set_height(struct mode_tree_data
*mtd
)
465 struct screen
*s
= &mtd
->screen
;
468 if (mtd
->heightcb
!= NULL
) {
469 height
= mtd
->heightcb(mtd
, screen_size_y(s
));
470 if (height
< screen_size_y(s
))
471 mtd
->height
= screen_size_y(s
) - height
;
473 mtd
->height
= (screen_size_y(s
) / 3) * 2;
474 if (mtd
->height
> mtd
->line_size
)
475 mtd
->height
= screen_size_y(s
) / 2;
477 if (mtd
->height
< 10)
478 mtd
->height
= screen_size_y(s
);
479 if (screen_size_y(s
) - mtd
->height
< 2)
480 mtd
->height
= screen_size_y(s
);
484 mode_tree_build(struct mode_tree_data
*mtd
)
486 struct screen
*s
= &mtd
->screen
;
489 if (mtd
->line_list
!= NULL
)
490 tag
= mtd
->line_list
[mtd
->current
].item
->tag
;
494 TAILQ_CONCAT(&mtd
->saved
, &mtd
->children
, entry
);
495 TAILQ_INIT(&mtd
->children
);
497 mtd
->buildcb(mtd
->modedata
, &mtd
->sort_crit
, &tag
, mtd
->filter
);
498 mtd
->no_matches
= TAILQ_EMPTY(&mtd
->children
);
500 mtd
->buildcb(mtd
->modedata
, &mtd
->sort_crit
, &tag
, NULL
);
502 mode_tree_free_items(&mtd
->saved
);
503 TAILQ_INIT(&mtd
->saved
);
505 mode_tree_clear_lines(mtd
);
506 mode_tree_build_lines(mtd
, &mtd
->children
, 0);
508 if (mtd
->line_list
!= NULL
&& tag
== UINT64_MAX
)
509 tag
= mtd
->line_list
[mtd
->current
].item
->tag
;
510 mode_tree_set_current(mtd
, tag
);
512 mtd
->width
= screen_size_x(s
);
514 mode_tree_set_height(mtd
);
516 mtd
->height
= screen_size_y(s
);
517 mode_tree_check_selected(mtd
);
521 mode_tree_remove_ref(struct mode_tree_data
*mtd
)
523 if (--mtd
->references
== 0)
528 mode_tree_free(struct mode_tree_data
*mtd
)
530 struct window_pane
*wp
= mtd
->wp
;
532 if (mtd
->zoomed
== 0)
533 server_unzoom_window(wp
->window
);
535 mode_tree_free_items(&mtd
->children
);
536 mode_tree_clear_lines(mtd
);
537 screen_free(&mtd
->screen
);
543 mode_tree_remove_ref(mtd
);
547 mode_tree_resize(struct mode_tree_data
*mtd
, u_int sx
, u_int sy
)
549 struct screen
*s
= &mtd
->screen
;
551 screen_resize(s
, sx
, sy
, 0);
553 mode_tree_build(mtd
);
556 mtd
->wp
->flags
|= PANE_REDRAW
;
559 struct mode_tree_item
*
560 mode_tree_add(struct mode_tree_data
*mtd
, struct mode_tree_item
*parent
,
561 void *itemdata
, uint64_t tag
, const char *name
, const char *text
,
564 struct mode_tree_item
*mti
, *saved
;
566 log_debug("%s: %llu, %s %s", __func__
, (unsigned long long)tag
,
567 name
, (text
== NULL
? "" : text
));
569 mti
= xcalloc(1, sizeof *mti
);
570 mti
->parent
= parent
;
571 mti
->itemdata
= itemdata
;
574 mti
->name
= xstrdup(name
);
576 mti
->text
= xstrdup(text
);
578 saved
= mode_tree_find_item(&mtd
->saved
, tag
);
580 if (parent
== NULL
|| parent
->expanded
)
581 mti
->tagged
= saved
->tagged
;
582 mti
->expanded
= saved
->expanded
;
583 } else if (expanded
== -1)
586 mti
->expanded
= expanded
;
588 TAILQ_INIT(&mti
->children
);
591 TAILQ_INSERT_TAIL(&parent
->children
, mti
, entry
);
593 TAILQ_INSERT_TAIL(&mtd
->children
, mti
, entry
);
599 mode_tree_draw_as_parent(struct mode_tree_item
*mti
)
601 mti
->draw_as_parent
= 1;
605 mode_tree_no_tag(struct mode_tree_item
*mti
)
611 mode_tree_remove(struct mode_tree_data
*mtd
, struct mode_tree_item
*mti
)
613 struct mode_tree_item
*parent
= mti
->parent
;
616 TAILQ_REMOVE(&parent
->children
, mti
, entry
);
618 TAILQ_REMOVE(&mtd
->children
, mti
, entry
);
619 mode_tree_free_item(mti
);
623 mode_tree_draw(struct mode_tree_data
*mtd
)
625 struct window_pane
*wp
= mtd
->wp
;
626 struct screen
*s
= &mtd
->screen
;
627 struct mode_tree_line
*line
;
628 struct mode_tree_item
*mti
;
629 struct options
*oo
= wp
->window
->options
;
630 struct screen_write_ctx ctx
;
631 struct grid_cell gc0
, gc
;
632 u_int w
, h
, i
, j
, sy
, box_x
, box_y
, width
;
633 char *text
, *start
, *key
;
634 const char *tag
, *symbol
;
638 if (mtd
->line_size
== 0)
641 memcpy(&gc0
, &grid_default_cell
, sizeof gc0
);
642 memcpy(&gc
, &grid_default_cell
, sizeof gc
);
643 style_apply(&gc
, oo
, "mode-style", NULL
);
648 screen_write_start(&ctx
, s
);
649 screen_write_clearscreen(&ctx
, 8);
652 for (i
= 0; i
< mtd
->line_size
; i
++) {
653 mti
= mtd
->line_list
[i
].item
;
654 if (mti
->key
== KEYC_NONE
)
656 if ((int)mti
->keylen
+ 3 > keylen
)
657 keylen
= mti
->keylen
+ 3;
660 for (i
= 0; i
< mtd
->line_size
; i
++) {
663 if (i
> mtd
->offset
+ h
- 1)
665 line
= &mtd
->line_list
[i
];
668 screen_write_cursormove(&ctx
, 0, i
- mtd
->offset
, 0);
670 pad
= keylen
- 2 - mti
->keylen
;
671 if (mti
->key
!= KEYC_NONE
)
672 xasprintf(&key
, "(%s)%*s", mti
->keystr
, pad
, "");
678 else if (TAILQ_EMPTY(&mti
->children
))
680 else if (mti
->expanded
)
685 if (line
->depth
== 0)
686 start
= xstrdup(symbol
);
688 size
= (4 * line
->depth
) + 32;
690 start
= xcalloc(1, size
);
691 for (j
= 1; j
< line
->depth
; j
++) {
692 if (mti
->parent
!= NULL
&&
693 mtd
->line_list
[mti
->parent
->line
].last
)
694 strlcat(start
, " ", size
);
696 strlcat(start
, "\001x\001 ", size
);
699 strlcat(start
, "\001mq\001> ", size
);
701 strlcat(start
, "\001tq\001> ", size
);
702 strlcat(start
, symbol
, size
);
709 xasprintf(&text
, "%-*s%s%s%s%s", keylen
, key
, start
, mti
->name
,
710 tag
, (mti
->text
!= NULL
) ? ": " : "" );
711 width
= utf8_cstrwidth(text
);
717 gc
.attr
^= GRID_ATTR_BRIGHT
;
718 gc0
.attr
^= GRID_ATTR_BRIGHT
;
721 if (i
!= mtd
->current
) {
722 screen_write_clearendofline(&ctx
, 8);
723 screen_write_nputs(&ctx
, w
, &gc0
, "%s", text
);
724 if (mti
->text
!= NULL
) {
725 format_draw(&ctx
, &gc0
, w
- width
, mti
->text
,
729 screen_write_clearendofline(&ctx
, gc
.bg
);
730 screen_write_nputs(&ctx
, w
, &gc
, "%s", text
);
731 if (mti
->text
!= NULL
) {
732 format_draw(&ctx
, &gc
, w
- width
, mti
->text
,
740 gc
.attr
^= GRID_ATTR_BRIGHT
;
741 gc0
.attr
^= GRID_ATTR_BRIGHT
;
745 sy
= screen_size_y(s
);
746 if (!mtd
->preview
|| sy
<= 4 || h
<= 4 || sy
- h
<= 4 || w
<= 4)
749 line
= &mtd
->line_list
[mtd
->current
];
751 if (mti
->draw_as_parent
)
754 screen_write_cursormove(&ctx
, 0, h
, 0);
755 screen_write_box(&ctx
, w
, sy
- h
, BOX_LINES_DEFAULT
, NULL
, NULL
);
757 if (mtd
->sort_list
!= NULL
) {
758 xasprintf(&text
, " %s (sort: %s%s)", mti
->name
,
759 mtd
->sort_list
[mtd
->sort_crit
.field
],
760 mtd
->sort_crit
.reversed
? ", reversed" : "");
762 xasprintf(&text
, " %s", mti
->name
);
763 if (w
- 2 >= strlen(text
)) {
764 screen_write_cursormove(&ctx
, 1, h
, 0);
765 screen_write_puts(&ctx
, &gc0
, "%s", text
);
768 n
= (sizeof "no matches") - 1;
770 n
= (sizeof "active") - 1;
771 if (mtd
->filter
!= NULL
&& w
- 2 >= strlen(text
) + 10 + n
+ 2) {
772 screen_write_puts(&ctx
, &gc0
, " (filter: ");
774 screen_write_puts(&ctx
, &gc
, "no matches");
776 screen_write_puts(&ctx
, &gc0
, "active");
777 screen_write_puts(&ctx
, &gc0
, ") ");
779 screen_write_puts(&ctx
, &gc0
, " ");
786 if (box_x
!= 0 && box_y
!= 0) {
787 screen_write_cursormove(&ctx
, 2, h
+ 1, 0);
788 mtd
->drawcb(mtd
->modedata
, mti
->itemdata
, &ctx
, box_x
, box_y
);
792 screen_write_cursormove(&ctx
, 0, mtd
->current
- mtd
->offset
, 0);
793 screen_write_stop(&ctx
);
796 static struct mode_tree_item
*
797 mode_tree_search_backward(struct mode_tree_data
*mtd
)
799 struct mode_tree_item
*mti
, *last
, *prev
;
801 if (mtd
->search
== NULL
)
804 mti
= last
= mtd
->line_list
[mtd
->current
].item
;
806 if ((prev
= TAILQ_PREV(mti
, mode_tree_list
, entry
)) != NULL
) {
807 /* Point to the last child in the previous subtree. */
808 while (!TAILQ_EMPTY(&prev
->children
))
809 prev
= TAILQ_LAST(&prev
->children
, mode_tree_list
);
812 /* If prev is NULL, jump to the parent. */
817 /* Point to the last child in the last root subtree. */
818 prev
= TAILQ_LAST(&mtd
->children
, mode_tree_list
);
819 while (!TAILQ_EMPTY(&prev
->children
))
820 prev
= TAILQ_LAST(&prev
->children
, mode_tree_list
);
826 if (mtd
->searchcb
== NULL
) {
827 if (strstr(mti
->name
, mtd
->search
) != NULL
)
831 if (mtd
->searchcb(mtd
->modedata
, mti
->itemdata
, mtd
->search
))
838 static struct mode_tree_item
*
839 mode_tree_search_forward(struct mode_tree_data
*mtd
)
841 struct mode_tree_item
*mti
, *last
, *next
;
843 if (mtd
->search
== NULL
)
846 mti
= last
= mtd
->line_list
[mtd
->current
].item
;
848 if (!TAILQ_EMPTY(&mti
->children
))
849 mti
= TAILQ_FIRST(&mti
->children
);
850 else if ((next
= TAILQ_NEXT(mti
, entry
)) != NULL
)
857 if ((next
= TAILQ_NEXT(mti
, entry
)) != NULL
) {
864 mti
= TAILQ_FIRST(&mtd
->children
);
868 if (mtd
->searchcb
== NULL
) {
869 if (strstr(mti
->name
, mtd
->search
) != NULL
)
873 if (mtd
->searchcb(mtd
->modedata
, mti
->itemdata
, mtd
->search
))
880 mode_tree_search_set(struct mode_tree_data
*mtd
)
882 struct mode_tree_item
*mti
, *loop
;
885 if (mtd
->search_dir
== MODE_TREE_SEARCH_FORWARD
)
886 mti
= mode_tree_search_forward(mtd
);
888 mti
= mode_tree_search_backward(mtd
);
894 while (loop
!= NULL
) {
899 mode_tree_build(mtd
);
900 mode_tree_set_current(mtd
, tag
);
902 mtd
->wp
->flags
|= PANE_REDRAW
;
906 mode_tree_search_callback(__unused
struct client
*c
, void *data
, const char *s
,
909 struct mode_tree_data
*mtd
= data
;
915 if (s
== NULL
|| *s
== '\0') {
919 mtd
->search
= xstrdup(s
);
920 mode_tree_search_set(mtd
);
926 mode_tree_search_free(void *data
)
928 mode_tree_remove_ref(data
);
932 mode_tree_filter_callback(__unused
struct client
*c
, void *data
, const char *s
,
935 struct mode_tree_data
*mtd
= data
;
940 if (mtd
->filter
!= NULL
)
942 if (s
== NULL
|| *s
== '\0')
945 mtd
->filter
= xstrdup(s
);
947 mode_tree_build(mtd
);
949 mtd
->wp
->flags
|= PANE_REDRAW
;
955 mode_tree_filter_free(void *data
)
957 mode_tree_remove_ref(data
);
961 mode_tree_menu_callback(__unused
struct menu
*menu
, __unused u_int idx
,
962 key_code key
, void *data
)
964 struct mode_tree_menu
*mtm
= data
;
965 struct mode_tree_data
*mtd
= mtm
->data
;
967 if (mtd
->dead
|| key
== KEYC_NONE
)
970 if (mtm
->line
>= mtd
->line_size
)
972 mtd
->current
= mtm
->line
;
973 mtd
->menucb(mtd
->modedata
, mtm
->c
, key
);
976 mode_tree_remove_ref(mtd
);
981 mode_tree_display_menu(struct mode_tree_data
*mtd
, struct client
*c
, u_int x
,
982 u_int y
, int outside
)
984 struct mode_tree_item
*mti
;
986 const struct menu_item
*items
;
987 struct mode_tree_menu
*mtm
;
991 if (mtd
->offset
+ y
> mtd
->line_size
- 1)
994 line
= mtd
->offset
+ y
;
995 mti
= mtd
->line_list
[line
].item
;
999 xasprintf(&title
, "#[align=centre]%s", mti
->name
);
1001 items
= mode_tree_menu_items
;
1002 title
= xstrdup("");
1004 menu
= menu_create(title
);
1005 menu_add_items(menu
, items
, NULL
, c
, NULL
);
1008 mtm
= xmalloc(sizeof *mtm
);
1014 if (x
>= (menu
->width
+ 4) / 2)
1015 x
-= (menu
->width
+ 4) / 2;
1018 if (menu_display(menu
, 0, 0, NULL
, x
, y
, c
, BOX_LINES_DEFAULT
, NULL
,
1019 NULL
, NULL
, NULL
, mode_tree_menu_callback
, mtm
) != 0)
1024 mode_tree_key(struct mode_tree_data
*mtd
, struct client
*c
, key_code
*key
,
1025 struct mouse_event
*m
, u_int
*xp
, u_int
*yp
)
1027 struct mode_tree_line
*line
;
1028 struct mode_tree_item
*current
, *parent
, *mti
;
1032 if (KEYC_IS_MOUSE(*key
) && m
!= NULL
) {
1033 if (cmd_mouse_at(mtd
->wp
, m
, &x
, &y
, 0) != 0) {
1041 if (x
> mtd
->width
|| y
> mtd
->height
) {
1042 if (*key
== KEYC_MOUSEDOWN3_PANE
)
1043 mode_tree_display_menu(mtd
, c
, x
, y
, 1);
1048 if (mtd
->offset
+ y
< mtd
->line_size
) {
1049 if (*key
== KEYC_MOUSEDOWN1_PANE
||
1050 *key
== KEYC_MOUSEDOWN3_PANE
||
1051 *key
== KEYC_DOUBLECLICK1_PANE
)
1052 mtd
->current
= mtd
->offset
+ y
;
1053 if (*key
== KEYC_DOUBLECLICK1_PANE
)
1056 if (*key
== KEYC_MOUSEDOWN3_PANE
)
1057 mode_tree_display_menu(mtd
, c
, x
, y
, 0);
1061 if (*key
== KEYC_MOUSEDOWN3_PANE
)
1062 mode_tree_display_menu(mtd
, c
, x
, y
, 0);
1068 line
= &mtd
->line_list
[mtd
->current
];
1069 current
= line
->item
;
1072 for (i
= 0; i
< mtd
->line_size
; i
++) {
1073 if (*key
== mtd
->line_list
[i
].item
->key
) {
1079 if ((u_int
)choice
> mtd
->line_size
- 1) {
1083 mtd
->current
= choice
;
1090 case '\033': /* Escape */
1095 case KEYC_WHEELUP_PANE
:
1097 mode_tree_up(mtd
, 1);
1101 case KEYC_WHEELDOWN_PANE
:
1103 mode_tree_down(mtd
, 1);
1107 for (i
= 0; i
< mtd
->height
; i
++) {
1108 if (mtd
->current
== 0)
1110 mode_tree_up(mtd
, 1);
1115 for (i
= 0; i
< mtd
->height
; i
++) {
1116 if (mtd
->current
== mtd
->line_size
- 1)
1118 mode_tree_down(mtd
, 1);
1128 mtd
->current
= mtd
->line_size
- 1;
1129 if (mtd
->current
> mtd
->height
- 1)
1130 mtd
->offset
= mtd
->current
- mtd
->height
+ 1;
1136 * Do not allow parents and children to both be tagged: untag
1137 * all parents and children of current.
1139 if (current
->no_tag
)
1141 if (!current
->tagged
) {
1142 parent
= current
->parent
;
1143 while (parent
!= NULL
) {
1145 parent
= parent
->parent
;
1147 mode_tree_clear_tagged(¤t
->children
);
1148 current
->tagged
= 1;
1150 current
->tagged
= 0;
1152 mode_tree_down(mtd
, 0);
1155 for (i
= 0; i
< mtd
->line_size
; i
++)
1156 mtd
->line_list
[i
].item
->tagged
= 0;
1159 for (i
= 0; i
< mtd
->line_size
; i
++) {
1160 if ((mtd
->line_list
[i
].item
->parent
== NULL
&&
1161 !mtd
->line_list
[i
].item
->no_tag
) ||
1162 (mtd
->line_list
[i
].item
->parent
!= NULL
&&
1163 mtd
->line_list
[i
].item
->parent
->no_tag
))
1164 mtd
->line_list
[i
].item
->tagged
= 1;
1166 mtd
->line_list
[i
].item
->tagged
= 0;
1170 mtd
->sort_crit
.field
++;
1171 if (mtd
->sort_crit
.field
>= mtd
->sort_size
)
1172 mtd
->sort_crit
.field
= 0;
1173 mode_tree_build(mtd
);
1176 mtd
->sort_crit
.reversed
= !mtd
->sort_crit
.reversed
;
1177 mode_tree_build(mtd
);
1182 if (line
->flat
|| !current
->expanded
)
1183 current
= current
->parent
;
1184 if (current
== NULL
)
1185 mode_tree_up(mtd
, 0);
1187 current
->expanded
= 0;
1188 mtd
->current
= current
->line
;
1189 mode_tree_build(mtd
);
1195 if (line
->flat
|| current
->expanded
)
1196 mode_tree_down(mtd
, 0);
1197 else if (!line
->flat
) {
1198 current
->expanded
= 1;
1199 mode_tree_build(mtd
);
1203 TAILQ_FOREACH(mti
, &mtd
->children
, entry
)
1205 mode_tree_build(mtd
);
1208 TAILQ_FOREACH(mti
, &mtd
->children
, entry
)
1210 mode_tree_build(mtd
);
1216 status_prompt_set(c
, NULL
, "(search) ", "",
1217 mode_tree_search_callback
, mode_tree_search_free
, mtd
,
1218 PROMPT_NOFORMAT
, PROMPT_TYPE_SEARCH
);
1221 mtd
->search_dir
= MODE_TREE_SEARCH_FORWARD
;
1222 mode_tree_search_set(mtd
);
1225 mtd
->search_dir
= MODE_TREE_SEARCH_BACKWARD
;
1226 mode_tree_search_set(mtd
);
1230 status_prompt_set(c
, NULL
, "(filter) ", mtd
->filter
,
1231 mode_tree_filter_callback
, mode_tree_filter_free
, mtd
,
1232 PROMPT_NOFORMAT
, PROMPT_TYPE_SEARCH
);
1235 mtd
->preview
= !mtd
->preview
;
1236 mode_tree_build(mtd
);
1238 mode_tree_check_selected(mtd
);
1245 mode_tree_run_command(struct client
*c
, struct cmd_find_state
*fs
,
1246 const char *template, const char *name
)
1248 struct cmdq_state
*state
;
1249 char *command
, *error
;
1250 enum cmd_parse_status status
;
1252 command
= cmd_template_replace(template, name
, 1);
1253 if (command
!= NULL
&& *command
!= '\0') {
1254 state
= cmdq_new_state(fs
, NULL
, 0);
1255 status
= cmd_parse_and_append(command
, NULL
, c
, state
, &error
);
1256 if (status
== CMD_PARSE_ERROR
) {
1258 *error
= toupper((u_char
)*error
);
1259 status_message_set(c
, -1, 1, 0, "%s", error
);
1263 cmdq_free_state(state
);