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>
27 static struct screen
*window_tree_init(struct window_mode_entry
*,
28 struct cmd_find_state
*, struct args
*);
29 static void window_tree_free(struct window_mode_entry
*);
30 static void window_tree_resize(struct window_mode_entry
*, u_int
,
32 static void window_tree_update(struct window_mode_entry
*);
33 static void window_tree_key(struct window_mode_entry
*,
34 struct client
*, struct session
*,
35 struct winlink
*, key_code
, struct mouse_event
*);
37 #define WINDOW_TREE_DEFAULT_COMMAND "switch-client -Zt '%%'"
39 #define WINDOW_TREE_DEFAULT_FORMAT \
41 "#{?pane_marked,#[reverse],}" \
42 "#{pane_current_command}#{?pane_active,*,}#{?pane_marked,M,}" \
43 "#{?#{&&:#{pane_title},#{!=:#{pane_title},#{host_short}}},: \"#{pane_title}\",}" \
46 "#{?window_marked_flag,#[reverse],}" \
47 "#{window_name}#{window_flags}" \
48 "#{?#{&&:#{==:#{window_panes},1},#{&&:#{pane_title},#{!=:#{pane_title},#{host_short}}}},: \"#{pane_title}\",}" \
50 "#{session_windows} windows" \
51 "#{?session_grouped, " \
52 "(group #{session_group}: " \
53 "#{session_group_list})," \
55 "#{?session_attached, (attached),}" \
59 #define WINDOW_TREE_DEFAULT_KEY_FORMAT \
60 "#{?#{e|<:#{line},10}," \
63 "#{?#{e|<:#{line},36}," \
64 "M-#{a:#{e|+:97,#{e|-:#{line},10}}}" \
70 static const struct menu_item window_tree_menu_items
[] = {
71 { "Select", '\r', NULL
},
72 { "Expand", KEYC_RIGHT
, NULL
},
73 { "Mark", 'm', NULL
},
74 { "", KEYC_NONE
, NULL
},
76 { "Tag All", '\024', NULL
},
77 { "Tag None", 'T', NULL
},
78 { "", KEYC_NONE
, NULL
},
79 { "Kill", 'x', NULL
},
80 { "Kill Tagged", 'X', NULL
},
81 { "", KEYC_NONE
, NULL
},
82 { "Cancel", 'q', NULL
},
84 { NULL
, KEYC_NONE
, NULL
}
87 const struct window_mode window_tree_mode
= {
89 .default_format
= WINDOW_TREE_DEFAULT_FORMAT
,
91 .init
= window_tree_init
,
92 .free
= window_tree_free
,
93 .resize
= window_tree_resize
,
94 .update
= window_tree_update
,
95 .key
= window_tree_key
,
98 enum window_tree_sort_type
{
103 static const char *window_tree_sort_list
[] = {
108 static struct mode_tree_sort_criteria
*window_tree_sort
;
110 enum window_tree_type
{
117 struct window_tree_itemdata
{
118 enum window_tree_type type
;
124 struct window_tree_modedata
{
125 struct window_pane
*wp
;
129 struct mode_tree_data
*data
;
135 struct window_tree_itemdata
**item_list
;
140 struct cmd_find_state fs
;
141 enum window_tree_type type
;
153 window_tree_pull_item(struct window_tree_itemdata
*item
, struct session
**sp
,
154 struct winlink
**wlp
, struct window_pane
**wp
)
158 *sp
= session_find_by_id(item
->session
);
161 if (item
->type
== WINDOW_TREE_SESSION
) {
163 *wp
= (*wlp
)->window
->active
;
167 *wlp
= winlink_find_by_index(&(*sp
)->windows
, item
->winlink
);
172 if (item
->type
== WINDOW_TREE_WINDOW
) {
173 *wp
= (*wlp
)->window
->active
;
177 *wp
= window_pane_find_by_id(item
->pane
);
178 if (!window_has_pane((*wlp
)->window
, *wp
))
187 static struct window_tree_itemdata
*
188 window_tree_add_item(struct window_tree_modedata
*data
)
190 struct window_tree_itemdata
*item
;
192 data
->item_list
= xreallocarray(data
->item_list
, data
->item_size
+ 1,
193 sizeof *data
->item_list
);
194 item
= data
->item_list
[data
->item_size
++] = xcalloc(1, sizeof *item
);
199 window_tree_free_item(struct window_tree_itemdata
*item
)
205 window_tree_cmp_session(const void *a0
, const void *b0
)
207 const struct session
*const *a
= a0
;
208 const struct session
*const *b
= b0
;
209 const struct session
*sa
= *a
;
210 const struct session
*sb
= *b
;
213 switch (window_tree_sort
->field
) {
214 case WINDOW_TREE_BY_INDEX
:
215 result
= sa
->id
- sb
->id
;
217 case WINDOW_TREE_BY_TIME
:
218 if (timercmp(&sa
->activity_time
, &sb
->activity_time
, >)) {
222 if (timercmp(&sa
->activity_time
, &sb
->activity_time
, <)) {
227 case WINDOW_TREE_BY_NAME
:
228 result
= strcmp(sa
->name
, sb
->name
);
232 if (window_tree_sort
->reversed
)
238 window_tree_cmp_window(const void *a0
, const void *b0
)
240 const struct winlink
*const *a
= a0
;
241 const struct winlink
*const *b
= b0
;
242 const struct winlink
*wla
= *a
;
243 const struct winlink
*wlb
= *b
;
244 struct window
*wa
= wla
->window
;
245 struct window
*wb
= wlb
->window
;
248 switch (window_tree_sort
->field
) {
249 case WINDOW_TREE_BY_INDEX
:
250 result
= wla
->idx
- wlb
->idx
;
252 case WINDOW_TREE_BY_TIME
:
253 if (timercmp(&wa
->activity_time
, &wb
->activity_time
, >)) {
257 if (timercmp(&wa
->activity_time
, &wb
->activity_time
, <)) {
262 case WINDOW_TREE_BY_NAME
:
263 result
= strcmp(wa
->name
, wb
->name
);
267 if (window_tree_sort
->reversed
)
273 window_tree_cmp_pane(const void *a0
, const void *b0
)
275 struct window_pane
**a
= (struct window_pane
**)a0
;
276 struct window_pane
**b
= (struct window_pane
**)b0
;
280 if (window_tree_sort
->field
== WINDOW_TREE_BY_TIME
)
281 result
= (*a
)->active_point
- (*b
)->active_point
;
284 * Panes don't have names, so use number order for any other
287 window_pane_index(*a
, &ai
);
288 window_pane_index(*b
, &bi
);
291 if (window_tree_sort
->reversed
)
297 window_tree_build_pane(struct session
*s
, struct winlink
*wl
,
298 struct window_pane
*wp
, void *modedata
, struct mode_tree_item
*parent
)
300 struct window_tree_modedata
*data
= modedata
;
301 struct window_tree_itemdata
*item
;
305 window_pane_index(wp
, &idx
);
307 item
= window_tree_add_item(data
);
308 item
->type
= WINDOW_TREE_PANE
;
309 item
->session
= s
->id
;
310 item
->winlink
= wl
->idx
;
313 text
= format_single(NULL
, data
->format
, NULL
, s
, wl
, wp
);
314 xasprintf(&name
, "%u", idx
);
316 mode_tree_add(data
->data
, parent
, item
, (uint64_t)wp
, name
, text
, -1);
322 window_tree_filter_pane(struct session
*s
, struct winlink
*wl
,
323 struct window_pane
*wp
, const char *filter
)
331 cp
= format_single(NULL
, filter
, NULL
, s
, wl
, wp
);
332 result
= format_true(cp
);
339 window_tree_build_window(struct session
*s
, struct winlink
*wl
,
340 void *modedata
, struct mode_tree_sort_criteria
*sort_crit
,
341 struct mode_tree_item
*parent
, const char *filter
)
343 struct window_tree_modedata
*data
= modedata
;
344 struct window_tree_itemdata
*item
;
345 struct mode_tree_item
*mti
;
347 struct window_pane
*wp
, **l
;
351 item
= window_tree_add_item(data
);
352 item
->type
= WINDOW_TREE_WINDOW
;
353 item
->session
= s
->id
;
354 item
->winlink
= wl
->idx
;
357 text
= format_single(NULL
, data
->format
, NULL
, s
, wl
, NULL
);
358 xasprintf(&name
, "%u", wl
->idx
);
360 if (data
->type
== WINDOW_TREE_SESSION
||
361 data
->type
== WINDOW_TREE_WINDOW
)
365 mti
= mode_tree_add(data
->data
, parent
, item
, (uint64_t)wl
, name
, text
,
370 if ((wp
= TAILQ_FIRST(&wl
->window
->panes
)) == NULL
)
372 if (TAILQ_NEXT(wp
, entry
) == NULL
) {
373 if (!window_tree_filter_pane(s
, wl
, wp
, filter
))
381 TAILQ_FOREACH(wp
, &wl
->window
->panes
, entry
) {
382 if (!window_tree_filter_pane(s
, wl
, wp
, filter
))
384 l
= xreallocarray(l
, n
+ 1, sizeof *l
);
390 window_tree_sort
= sort_crit
;
391 qsort(l
, n
, sizeof *l
, window_tree_cmp_pane
);
393 for (i
= 0; i
< n
; i
++)
394 window_tree_build_pane(s
, wl
, l
[i
], modedata
, mti
);
399 window_tree_free_item(item
);
401 mode_tree_remove(data
->data
, mti
);
406 window_tree_build_session(struct session
*s
, void *modedata
,
407 struct mode_tree_sort_criteria
*sort_crit
, const char *filter
)
409 struct window_tree_modedata
*data
= modedata
;
410 struct window_tree_itemdata
*item
;
411 struct mode_tree_item
*mti
;
413 struct winlink
*wl
, **l
;
417 item
= window_tree_add_item(data
);
418 item
->type
= WINDOW_TREE_SESSION
;
419 item
->session
= s
->id
;
423 text
= format_single(NULL
, data
->format
, NULL
, s
, NULL
, NULL
);
425 if (data
->type
== WINDOW_TREE_SESSION
)
429 mti
= mode_tree_add(data
->data
, NULL
, item
, (uint64_t)s
, s
->name
, text
,
435 RB_FOREACH(wl
, winlinks
, &s
->windows
) {
436 l
= xreallocarray(l
, n
+ 1, sizeof *l
);
439 window_tree_sort
= sort_crit
;
440 qsort(l
, n
, sizeof *l
, window_tree_cmp_window
);
443 for (i
= 0; i
< n
; i
++) {
444 if (!window_tree_build_window(s
, l
[i
], modedata
, sort_crit
, mti
,
449 window_tree_free_item(item
);
451 mode_tree_remove(data
->data
, mti
);
457 window_tree_build(void *modedata
, struct mode_tree_sort_criteria
*sort_crit
,
458 uint64_t *tag
, const char *filter
)
460 struct window_tree_modedata
*data
= modedata
;
461 struct session
*s
, **l
;
462 struct session_group
*sg
, *current
;
465 current
= session_group_contains(data
->fs
.s
);
467 for (i
= 0; i
< data
->item_size
; i
++)
468 window_tree_free_item(data
->item_list
[i
]);
469 free(data
->item_list
);
470 data
->item_list
= NULL
;
475 RB_FOREACH(s
, sessions
, &sessions
) {
476 if (data
->squash_groups
&&
477 (sg
= session_group_contains(s
)) != NULL
) {
478 if ((sg
== current
&& s
!= data
->fs
.s
) ||
479 (sg
!= current
&& s
!= TAILQ_FIRST(&sg
->sessions
)))
482 l
= xreallocarray(l
, n
+ 1, sizeof *l
);
485 window_tree_sort
= sort_crit
;
486 qsort(l
, n
, sizeof *l
, window_tree_cmp_session
);
488 for (i
= 0; i
< n
; i
++)
489 window_tree_build_session(l
[i
], modedata
, sort_crit
, filter
);
492 switch (data
->type
) {
493 case WINDOW_TREE_NONE
:
495 case WINDOW_TREE_SESSION
:
496 *tag
= (uint64_t)data
->fs
.s
;
498 case WINDOW_TREE_WINDOW
:
499 *tag
= (uint64_t)data
->fs
.wl
;
501 case WINDOW_TREE_PANE
:
502 if (window_count_panes(data
->fs
.wl
->window
) == 1)
503 *tag
= (uint64_t)data
->fs
.wl
;
505 *tag
= (uint64_t)data
->fs
.wp
;
511 window_tree_draw_label(struct screen_write_ctx
*ctx
, u_int px
, u_int py
,
512 u_int sx
, u_int sy
, const struct grid_cell
*gc
, const char *label
)
518 if (sx
== 0 || sy
== 1 || len
> sx
)
520 ox
= (sx
- len
+ 1) / 2;
523 if (ox
> 1 && ox
+ len
< sx
- 1 && sy
>= 3) {
524 screen_write_cursormove(ctx
, px
+ ox
- 1, py
+ oy
- 1, 0);
525 screen_write_box(ctx
, len
+ 2, 3, BOX_LINES_DEFAULT
, NULL
,
528 screen_write_cursormove(ctx
, px
+ ox
, py
+ oy
, 0);
529 screen_write_puts(ctx
, gc
, "%s", label
);
533 window_tree_draw_session(struct window_tree_modedata
*data
, struct session
*s
,
534 struct screen_write_ctx
*ctx
, u_int sx
, u_int sy
)
536 struct options
*oo
= s
->options
;
539 u_int cx
= ctx
->s
->cx
, cy
= ctx
->s
->cy
;
540 u_int loop
, total
, visible
, each
, width
, offset
;
541 u_int current
, start
, end
, remaining
, i
;
543 int colour
, active_colour
, left
, right
;
546 total
= winlink_count(&s
->windows
);
548 memcpy(&gc
, &grid_default_cell
, sizeof gc
);
549 colour
= options_get_number(oo
, "display-panes-colour");
550 active_colour
= options_get_number(oo
, "display-panes-active-colour");
552 if (sx
/ total
< 24) {
560 RB_FOREACH(wl
, winlinks
, &s
->windows
) {
566 if (current
< visible
) {
569 } else if (current
>= total
- visible
) {
570 start
= total
- visible
;
573 start
= current
- (visible
/ 2);
574 end
= start
+ visible
;
577 if (data
->offset
< -(int)start
)
578 data
->offset
= -(int)start
;
579 if (data
->offset
> (int)(total
- end
))
580 data
->offset
= (int)(total
- end
);
581 start
+= data
->offset
;
585 right
= (end
!= total
);
586 if (((left
&& right
) && sx
<= 6) || ((left
|| right
) && sx
<= 3))
589 each
= (sx
- 6) / visible
;
590 remaining
= (sx
- 6) - (visible
* each
);
591 } else if (left
|| right
) {
592 each
= (sx
- 3) / visible
;
593 remaining
= (sx
- 3) - (visible
* each
);
596 remaining
= sx
- (visible
* each
);
603 screen_write_cursormove(ctx
, cx
+ 2, cy
, 0);
604 screen_write_vline(ctx
, sy
, 0, 0);
605 screen_write_cursormove(ctx
, cx
, cy
+ sy
/ 2, 0);
606 screen_write_puts(ctx
, &grid_default_cell
, "<");
610 data
->right
= cx
+ sx
- 3;
611 screen_write_cursormove(ctx
, cx
+ sx
- 3, cy
, 0);
612 screen_write_vline(ctx
, sy
, 0, 0);
613 screen_write_cursormove(ctx
, cx
+ sx
- 1, cy
+ sy
/ 2, 0);
614 screen_write_puts(ctx
, &grid_default_cell
, ">");
623 RB_FOREACH(wl
, winlinks
, &s
->windows
) {
633 gc
.fg
= active_colour
;
638 offset
= 3 + (i
* each
);
642 width
= each
+ remaining
;
646 screen_write_cursormove(ctx
, cx
+ offset
, cy
, 0);
647 screen_write_preview(ctx
, &w
->active
->base
, width
, sy
);
649 xasprintf(&label
, " %u:%s ", wl
->idx
, w
->name
);
650 if (strlen(label
) > width
)
651 xasprintf(&label
, " %u ", wl
->idx
);
652 window_tree_draw_label(ctx
, cx
+ offset
, cy
, width
, sy
, &gc
,
656 if (loop
!= end
- 1) {
657 screen_write_cursormove(ctx
, cx
+ offset
+ width
, cy
, 0);
658 screen_write_vline(ctx
, sy
, 0, 0);
667 window_tree_draw_window(struct window_tree_modedata
*data
, struct session
*s
,
668 struct window
*w
, struct screen_write_ctx
*ctx
, u_int sx
, u_int sy
)
670 struct options
*oo
= s
->options
;
671 struct window_pane
*wp
;
672 u_int cx
= ctx
->s
->cx
, cy
= ctx
->s
->cy
;
673 u_int loop
, total
, visible
, each
, width
, offset
;
674 u_int current
, start
, end
, remaining
, i
, pane_idx
;
676 int colour
, active_colour
, left
, right
;
679 total
= window_count_panes(w
);
681 memcpy(&gc
, &grid_default_cell
, sizeof gc
);
682 colour
= options_get_number(oo
, "display-panes-colour");
683 active_colour
= options_get_number(oo
, "display-panes-active-colour");
685 if (sx
/ total
< 24) {
693 TAILQ_FOREACH(wp
, &w
->panes
, entry
) {
699 if (current
< visible
) {
702 } else if (current
>= total
- visible
) {
703 start
= total
- visible
;
706 start
= current
- (visible
/ 2);
707 end
= start
+ visible
;
710 if (data
->offset
< -(int)start
)
711 data
->offset
= -(int)start
;
712 if (data
->offset
> (int)(total
- end
))
713 data
->offset
= (int)(total
- end
);
714 start
+= data
->offset
;
718 right
= (end
!= total
);
719 if (((left
&& right
) && sx
<= 6) || ((left
|| right
) && sx
<= 3))
722 each
= (sx
- 6) / visible
;
723 remaining
= (sx
- 6) - (visible
* each
);
724 } else if (left
|| right
) {
725 each
= (sx
- 3) / visible
;
726 remaining
= (sx
- 3) - (visible
* each
);
729 remaining
= sx
- (visible
* each
);
736 screen_write_cursormove(ctx
, cx
+ 2, cy
, 0);
737 screen_write_vline(ctx
, sy
, 0, 0);
738 screen_write_cursormove(ctx
, cx
, cy
+ sy
/ 2, 0);
739 screen_write_puts(ctx
, &grid_default_cell
, "<");
743 data
->right
= cx
+ sx
- 3;
744 screen_write_cursormove(ctx
, cx
+ sx
- 3, cy
, 0);
745 screen_write_vline(ctx
, sy
, 0, 0);
746 screen_write_cursormove(ctx
, cx
+ sx
- 1, cy
+ sy
/ 2, 0);
747 screen_write_puts(ctx
, &grid_default_cell
, ">");
756 TAILQ_FOREACH(wp
, &w
->panes
, entry
) {
765 gc
.fg
= active_colour
;
770 offset
= 3 + (i
* each
);
774 width
= each
+ remaining
;
778 screen_write_cursormove(ctx
, cx
+ offset
, cy
, 0);
779 screen_write_preview(ctx
, &wp
->base
, width
, sy
);
781 if (window_pane_index(wp
, &pane_idx
) != 0)
783 xasprintf(&label
, " %u ", pane_idx
);
784 window_tree_draw_label(ctx
, cx
+ offset
, cy
, each
, sy
, &gc
,
788 if (loop
!= end
- 1) {
789 screen_write_cursormove(ctx
, cx
+ offset
+ width
, cy
, 0);
790 screen_write_vline(ctx
, sy
, 0, 0);
799 window_tree_draw(void *modedata
, void *itemdata
, struct screen_write_ctx
*ctx
,
802 struct window_tree_itemdata
*item
= itemdata
;
805 struct window_pane
*wp
;
807 window_tree_pull_item(item
, &sp
, &wlp
, &wp
);
811 switch (item
->type
) {
812 case WINDOW_TREE_NONE
:
814 case WINDOW_TREE_SESSION
:
815 window_tree_draw_session(modedata
, sp
, ctx
, sx
, sy
);
817 case WINDOW_TREE_WINDOW
:
818 window_tree_draw_window(modedata
, sp
, wlp
->window
, ctx
, sx
, sy
);
820 case WINDOW_TREE_PANE
:
821 screen_write_preview(ctx
, &wp
->base
, sx
, sy
);
827 window_tree_search(__unused
void *modedata
, void *itemdata
, const char *ss
)
829 struct window_tree_itemdata
*item
= itemdata
;
832 struct window_pane
*wp
;
836 window_tree_pull_item(item
, &s
, &wl
, &wp
);
838 switch (item
->type
) {
839 case WINDOW_TREE_NONE
:
841 case WINDOW_TREE_SESSION
:
844 return (strstr(s
->name
, ss
) != NULL
);
845 case WINDOW_TREE_WINDOW
:
846 if (s
== NULL
|| wl
== NULL
)
848 return (strstr(wl
->window
->name
, ss
) != NULL
);
849 case WINDOW_TREE_PANE
:
850 if (s
== NULL
|| wl
== NULL
|| wp
== NULL
)
852 cmd
= osdep_get_name(wp
->fd
, wp
->tty
);
853 if (cmd
== NULL
|| *cmd
== '\0')
855 retval
= (strstr(cmd
, ss
) != NULL
);
863 window_tree_menu(void *modedata
, struct client
*c
, key_code key
)
865 struct window_tree_modedata
*data
= modedata
;
866 struct window_pane
*wp
= data
->wp
;
867 struct window_mode_entry
*wme
;
869 wme
= TAILQ_FIRST(&wp
->modes
);
870 if (wme
== NULL
|| wme
->data
!= modedata
)
872 window_tree_key(wme
, c
, NULL
, NULL
, key
, NULL
);
876 window_tree_get_key(void *modedata
, void *itemdata
, u_int line
)
878 struct window_tree_modedata
*data
= modedata
;
879 struct window_tree_itemdata
*item
= itemdata
;
880 struct format_tree
*ft
;
883 struct window_pane
*wp
;
887 ft
= format_create(NULL
, NULL
, FORMAT_NONE
, 0);
888 window_tree_pull_item(item
, &s
, &wl
, &wp
);
889 if (item
->type
== WINDOW_TREE_SESSION
)
890 format_defaults(ft
, NULL
, s
, NULL
, NULL
);
891 else if (item
->type
== WINDOW_TREE_WINDOW
)
892 format_defaults(ft
, NULL
, s
, wl
, NULL
);
894 format_defaults(ft
, NULL
, s
, wl
, wp
);
895 format_add(ft
, "line", "%u", line
);
897 expanded
= format_expand(ft
, data
->key_format
);
898 key
= key_string_lookup_string(expanded
);
904 static struct screen
*
905 window_tree_init(struct window_mode_entry
*wme
, struct cmd_find_state
*fs
,
908 struct window_pane
*wp
= wme
->wp
;
909 struct window_tree_modedata
*data
;
912 wme
->data
= data
= xcalloc(1, sizeof *data
);
914 data
->references
= 1;
916 if (args_has(args
, 's'))
917 data
->type
= WINDOW_TREE_SESSION
;
918 else if (args_has(args
, 'w'))
919 data
->type
= WINDOW_TREE_WINDOW
;
921 data
->type
= WINDOW_TREE_PANE
;
922 memcpy(&data
->fs
, fs
, sizeof data
->fs
);
924 if (args
== NULL
|| !args_has(args
, 'F'))
925 data
->format
= xstrdup(WINDOW_TREE_DEFAULT_FORMAT
);
927 data
->format
= xstrdup(args_get(args
, 'F'));
928 if (args
== NULL
|| !args_has(args
, 'K'))
929 data
->key_format
= xstrdup(WINDOW_TREE_DEFAULT_KEY_FORMAT
);
931 data
->key_format
= xstrdup(args_get(args
, 'K'));
932 if (args
== NULL
|| args_count(args
) == 0)
933 data
->command
= xstrdup(WINDOW_TREE_DEFAULT_COMMAND
);
935 data
->command
= xstrdup(args_string(args
, 0));
936 data
->squash_groups
= !args_has(args
, 'G');
938 data
->data
= mode_tree_start(wp
, args
, window_tree_build
,
939 window_tree_draw
, window_tree_search
, window_tree_menu
, NULL
,
940 window_tree_get_key
, data
, window_tree_menu_items
,
941 window_tree_sort_list
, nitems(window_tree_sort_list
), &s
);
942 mode_tree_zoom(data
->data
, args
);
944 mode_tree_build(data
->data
);
945 mode_tree_draw(data
->data
);
947 data
->type
= WINDOW_TREE_NONE
;
953 window_tree_destroy(struct window_tree_modedata
*data
)
957 if (--data
->references
!= 0)
960 for (i
= 0; i
< data
->item_size
; i
++)
961 window_tree_free_item(data
->item_list
[i
]);
962 free(data
->item_list
);
965 free(data
->key_format
);
972 window_tree_free(struct window_mode_entry
*wme
)
974 struct window_tree_modedata
*data
= wme
->data
;
980 mode_tree_free(data
->data
);
981 window_tree_destroy(data
);
985 window_tree_resize(struct window_mode_entry
*wme
, u_int sx
, u_int sy
)
987 struct window_tree_modedata
*data
= wme
->data
;
989 mode_tree_resize(data
->data
, sx
, sy
);
993 window_tree_update(struct window_mode_entry
*wme
)
995 struct window_tree_modedata
*data
= wme
->data
;
997 mode_tree_build(data
->data
);
998 mode_tree_draw(data
->data
);
999 data
->wp
->flags
|= PANE_REDRAW
;
1003 window_tree_get_target(struct window_tree_itemdata
*item
,
1004 struct cmd_find_state
*fs
)
1008 struct window_pane
*wp
;
1011 window_tree_pull_item(item
, &s
, &wl
, &wp
);
1014 switch (item
->type
) {
1015 case WINDOW_TREE_NONE
:
1017 case WINDOW_TREE_SESSION
:
1020 xasprintf(&target
, "=%s:", s
->name
);
1022 case WINDOW_TREE_WINDOW
:
1023 if (s
== NULL
|| wl
== NULL
)
1025 xasprintf(&target
, "=%s:%u.", s
->name
, wl
->idx
);
1027 case WINDOW_TREE_PANE
:
1028 if (s
== NULL
|| wl
== NULL
|| wp
== NULL
)
1030 xasprintf(&target
, "=%s:%u.%%%u", s
->name
, wl
->idx
, wp
->id
);
1034 cmd_find_clear_state(fs
, 0);
1036 cmd_find_from_winlink_pane(fs
, wl
, wp
, 0);
1041 window_tree_command_each(void *modedata
, void *itemdata
, struct client
*c
,
1042 __unused key_code key
)
1044 struct window_tree_modedata
*data
= modedata
;
1045 struct window_tree_itemdata
*item
= itemdata
;
1047 struct cmd_find_state fs
;
1049 name
= window_tree_get_target(item
, &fs
);
1051 mode_tree_run_command(c
, &fs
, data
->entered
, name
);
1055 static enum cmd_retval
1056 window_tree_command_done(__unused
struct cmdq_item
*item
, void *modedata
)
1058 struct window_tree_modedata
*data
= modedata
;
1061 mode_tree_build(data
->data
);
1062 mode_tree_draw(data
->data
);
1063 data
->wp
->flags
|= PANE_REDRAW
;
1065 window_tree_destroy(data
);
1066 return (CMD_RETURN_NORMAL
);
1070 window_tree_command_callback(struct client
*c
, void *modedata
, const char *s
,
1073 struct window_tree_modedata
*data
= modedata
;
1075 if (s
== NULL
|| *s
== '\0' || data
->dead
)
1079 mode_tree_each_tagged(data
->data
, window_tree_command_each
, c
,
1081 data
->entered
= NULL
;
1084 cmdq_append(c
, cmdq_get_callback(window_tree_command_done
, data
));
1090 window_tree_command_free(void *modedata
)
1092 struct window_tree_modedata
*data
= modedata
;
1094 window_tree_destroy(data
);
1098 window_tree_kill_each(__unused
void *modedata
, void *itemdata
,
1099 __unused
struct client
*c
, __unused key_code key
)
1101 struct window_tree_itemdata
*item
= itemdata
;
1104 struct window_pane
*wp
;
1106 window_tree_pull_item(item
, &s
, &wl
, &wp
);
1108 switch (item
->type
) {
1109 case WINDOW_TREE_NONE
:
1111 case WINDOW_TREE_SESSION
:
1113 server_destroy_session(s
);
1114 session_destroy(s
, 1, __func__
);
1117 case WINDOW_TREE_WINDOW
:
1119 server_kill_window(wl
->window
, 0);
1121 case WINDOW_TREE_PANE
:
1123 server_kill_pane(wp
);
1129 window_tree_kill_current_callback(struct client
*c
, void *modedata
,
1130 const char *s
, __unused
int done
)
1132 struct window_tree_modedata
*data
= modedata
;
1133 struct mode_tree_data
*mtd
= data
->data
;
1135 if (s
== NULL
|| *s
== '\0' || data
->dead
)
1137 if (tolower((u_char
) s
[0]) != 'y' || s
[1] != '\0')
1140 window_tree_kill_each(data
, mode_tree_get_current(mtd
), c
, KEYC_NONE
);
1141 server_renumber_all();
1144 cmdq_append(c
, cmdq_get_callback(window_tree_command_done
, data
));
1150 window_tree_kill_tagged_callback(struct client
*c
, void *modedata
,
1151 const char *s
, __unused
int done
)
1153 struct window_tree_modedata
*data
= modedata
;
1154 struct mode_tree_data
*mtd
= data
->data
;
1156 if (s
== NULL
|| *s
== '\0' || data
->dead
)
1158 if (tolower((u_char
) s
[0]) != 'y' || s
[1] != '\0')
1161 mode_tree_each_tagged(mtd
, window_tree_kill_each
, c
, KEYC_NONE
, 1);
1162 server_renumber_all();
1165 cmdq_append(c
, cmdq_get_callback(window_tree_command_done
, data
));
1171 window_tree_mouse(struct window_tree_modedata
*data
, key_code key
, u_int x
,
1172 struct window_tree_itemdata
*item
)
1176 struct window_pane
*wp
;
1179 if (key
!= KEYC_MOUSEDOWN1_PANE
)
1182 if (data
->left
!= -1 && x
<= (u_int
)data
->left
)
1184 if (data
->right
!= -1 && x
>= (u_int
)data
->right
)
1187 if (data
->left
!= -1)
1191 if (x
== 0 || data
->end
== 0)
1195 if (data
->start
+ x
>= data
->end
)
1199 window_tree_pull_item(item
, &s
, &wl
, &wp
);
1200 if (item
->type
== WINDOW_TREE_SESSION
) {
1203 mode_tree_expand_current(data
->data
);
1205 RB_FOREACH(wl
, winlinks
, &s
->windows
) {
1206 if (loop
== data
->start
+ x
)
1211 mode_tree_set_current(data
->data
, (uint64_t)wl
);
1214 if (item
->type
== WINDOW_TREE_WINDOW
) {
1217 mode_tree_expand_current(data
->data
);
1219 TAILQ_FOREACH(wp
, &wl
->window
->panes
, entry
) {
1220 if (loop
== data
->start
+ x
)
1225 mode_tree_set_current(data
->data
, (uint64_t)wp
);
1232 window_tree_key(struct window_mode_entry
*wme
, struct client
*c
,
1233 __unused
struct session
*s
, __unused
struct winlink
*wl
, key_code key
,
1234 struct mouse_event
*m
)
1236 struct window_pane
*wp
= wme
->wp
;
1237 struct window_tree_modedata
*data
= wme
->data
;
1238 struct window_tree_itemdata
*item
, *new_item
;
1239 char *name
, *prompt
= NULL
;
1240 struct cmd_find_state fs
, *fsp
= &data
->fs
;
1242 u_int tagged
, x
, y
, idx
;
1244 struct winlink
*nwl
;
1245 struct window_pane
*nwp
;
1247 item
= mode_tree_get_current(data
->data
);
1248 finished
= mode_tree_key(data
->data
, c
, &key
, m
, &x
, &y
);
1251 if (item
!= (new_item
= mode_tree_get_current(data
->data
))) {
1255 if (KEYC_IS_MOUSE(key
) && m
!= NULL
) {
1256 key
= window_tree_mouse(data
, key
, x
, item
);
1268 mode_tree_expand(data
->data
, (uint64_t)fsp
->s
);
1269 mode_tree_expand(data
->data
, (uint64_t)fsp
->wl
);
1270 if (!mode_tree_set_current(data
->data
, (uint64_t)wme
->wp
))
1271 mode_tree_set_current(data
->data
, (uint64_t)fsp
->wl
);
1274 window_tree_pull_item(item
, &ns
, &nwl
, &nwp
);
1275 server_set_marked(ns
, nwl
, nwp
);
1276 mode_tree_build(data
->data
);
1279 server_clear_marked();
1280 mode_tree_build(data
->data
);
1283 window_tree_pull_item(item
, &ns
, &nwl
, &nwp
);
1284 switch (item
->type
) {
1285 case WINDOW_TREE_NONE
:
1287 case WINDOW_TREE_SESSION
:
1290 xasprintf(&prompt
, "Kill session %s? ", ns
->name
);
1292 case WINDOW_TREE_WINDOW
:
1295 xasprintf(&prompt
, "Kill window %u? ", nwl
->idx
);
1297 case WINDOW_TREE_PANE
:
1298 if (nwp
== NULL
|| window_pane_index(nwp
, &idx
) != 0)
1300 xasprintf(&prompt
, "Kill pane %u? ", idx
);
1306 status_prompt_set(c
, NULL
, prompt
, "",
1307 window_tree_kill_current_callback
, window_tree_command_free
,
1308 data
, PROMPT_SINGLE
|PROMPT_NOFORMAT
, PROMPT_TYPE_COMMAND
);
1312 tagged
= mode_tree_count_tagged(data
->data
);
1315 xasprintf(&prompt
, "Kill %u tagged? ", tagged
);
1317 status_prompt_set(c
, NULL
, prompt
, "",
1318 window_tree_kill_tagged_callback
, window_tree_command_free
,
1319 data
, PROMPT_SINGLE
|PROMPT_NOFORMAT
, PROMPT_TYPE_COMMAND
);
1323 tagged
= mode_tree_count_tagged(data
->data
);
1325 xasprintf(&prompt
, "(%u tagged) ", tagged
);
1327 xasprintf(&prompt
, "(current) ");
1329 status_prompt_set(c
, NULL
, prompt
, "",
1330 window_tree_command_callback
, window_tree_command_free
,
1331 data
, PROMPT_NOFORMAT
, PROMPT_TYPE_COMMAND
);
1335 name
= window_tree_get_target(item
, &fs
);
1337 mode_tree_run_command(c
, NULL
, data
->command
, name
);
1343 window_pane_reset_mode(wp
);
1345 mode_tree_draw(data
->data
);
1346 wp
->flags
|= PANE_REDRAW
;