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 const struct window_pane
*const *a
= a0
;
276 const struct window_pane
*const *b
= b0
;
279 if (window_tree_sort
->field
== WINDOW_TREE_BY_TIME
)
280 result
= (*a
)->active_point
- (*b
)->active_point
;
283 * Panes don't have names, so use number order for any other
286 result
= (*a
)->id
- (*b
)->id
;
288 if (window_tree_sort
->reversed
)
294 window_tree_build_pane(struct session
*s
, struct winlink
*wl
,
295 struct window_pane
*wp
, void *modedata
, struct mode_tree_item
*parent
)
297 struct window_tree_modedata
*data
= modedata
;
298 struct window_tree_itemdata
*item
;
302 window_pane_index(wp
, &idx
);
304 item
= window_tree_add_item(data
);
305 item
->type
= WINDOW_TREE_PANE
;
306 item
->session
= s
->id
;
307 item
->winlink
= wl
->idx
;
310 text
= format_single(NULL
, data
->format
, NULL
, s
, wl
, wp
);
311 xasprintf(&name
, "%u", idx
);
313 mode_tree_add(data
->data
, parent
, item
, (uint64_t)wp
, name
, text
, -1);
319 window_tree_filter_pane(struct session
*s
, struct winlink
*wl
,
320 struct window_pane
*wp
, const char *filter
)
328 cp
= format_single(NULL
, filter
, NULL
, s
, wl
, wp
);
329 result
= format_true(cp
);
336 window_tree_build_window(struct session
*s
, struct winlink
*wl
,
337 void *modedata
, struct mode_tree_sort_criteria
*sort_crit
,
338 struct mode_tree_item
*parent
, const char *filter
)
340 struct window_tree_modedata
*data
= modedata
;
341 struct window_tree_itemdata
*item
;
342 struct mode_tree_item
*mti
;
344 struct window_pane
*wp
, **l
;
348 item
= window_tree_add_item(data
);
349 item
->type
= WINDOW_TREE_WINDOW
;
350 item
->session
= s
->id
;
351 item
->winlink
= wl
->idx
;
354 text
= format_single(NULL
, data
->format
, NULL
, s
, wl
, NULL
);
355 xasprintf(&name
, "%u", wl
->idx
);
357 if (data
->type
== WINDOW_TREE_SESSION
||
358 data
->type
== WINDOW_TREE_WINDOW
)
362 mti
= mode_tree_add(data
->data
, parent
, item
, (uint64_t)wl
, name
, text
,
367 if ((wp
= TAILQ_FIRST(&wl
->window
->panes
)) == NULL
)
369 if (TAILQ_NEXT(wp
, entry
) == NULL
) {
370 if (!window_tree_filter_pane(s
, wl
, wp
, filter
))
378 TAILQ_FOREACH(wp
, &wl
->window
->panes
, entry
) {
379 if (!window_tree_filter_pane(s
, wl
, wp
, filter
))
381 l
= xreallocarray(l
, n
+ 1, sizeof *l
);
387 window_tree_sort
= sort_crit
;
388 qsort(l
, n
, sizeof *l
, window_tree_cmp_pane
);
390 for (i
= 0; i
< n
; i
++)
391 window_tree_build_pane(s
, wl
, l
[i
], modedata
, mti
);
396 window_tree_free_item(item
);
398 mode_tree_remove(data
->data
, mti
);
403 window_tree_build_session(struct session
*s
, void *modedata
,
404 struct mode_tree_sort_criteria
*sort_crit
, const char *filter
)
406 struct window_tree_modedata
*data
= modedata
;
407 struct window_tree_itemdata
*item
;
408 struct mode_tree_item
*mti
;
410 struct winlink
*wl
, **l
;
414 item
= window_tree_add_item(data
);
415 item
->type
= WINDOW_TREE_SESSION
;
416 item
->session
= s
->id
;
420 text
= format_single(NULL
, data
->format
, NULL
, s
, NULL
, NULL
);
422 if (data
->type
== WINDOW_TREE_SESSION
)
426 mti
= mode_tree_add(data
->data
, NULL
, item
, (uint64_t)s
, s
->name
, text
,
432 RB_FOREACH(wl
, winlinks
, &s
->windows
) {
433 l
= xreallocarray(l
, n
+ 1, sizeof *l
);
436 window_tree_sort
= sort_crit
;
437 qsort(l
, n
, sizeof *l
, window_tree_cmp_window
);
440 for (i
= 0; i
< n
; i
++) {
441 if (!window_tree_build_window(s
, l
[i
], modedata
, sort_crit
, mti
,
446 window_tree_free_item(item
);
448 mode_tree_remove(data
->data
, mti
);
454 window_tree_build(void *modedata
, struct mode_tree_sort_criteria
*sort_crit
,
455 uint64_t *tag
, const char *filter
)
457 struct window_tree_modedata
*data
= modedata
;
458 struct session
*s
, **l
;
459 struct session_group
*sg
, *current
;
462 current
= session_group_contains(data
->fs
.s
);
464 for (i
= 0; i
< data
->item_size
; i
++)
465 window_tree_free_item(data
->item_list
[i
]);
466 free(data
->item_list
);
467 data
->item_list
= NULL
;
472 RB_FOREACH(s
, sessions
, &sessions
) {
473 if (data
->squash_groups
&&
474 (sg
= session_group_contains(s
)) != NULL
) {
475 if ((sg
== current
&& s
!= data
->fs
.s
) ||
476 (sg
!= current
&& s
!= TAILQ_FIRST(&sg
->sessions
)))
479 l
= xreallocarray(l
, n
+ 1, sizeof *l
);
482 window_tree_sort
= sort_crit
;
483 qsort(l
, n
, sizeof *l
, window_tree_cmp_session
);
485 for (i
= 0; i
< n
; i
++)
486 window_tree_build_session(l
[i
], modedata
, sort_crit
, filter
);
489 switch (data
->type
) {
490 case WINDOW_TREE_NONE
:
492 case WINDOW_TREE_SESSION
:
493 *tag
= (uint64_t)data
->fs
.s
;
495 case WINDOW_TREE_WINDOW
:
496 *tag
= (uint64_t)data
->fs
.wl
;
498 case WINDOW_TREE_PANE
:
499 if (window_count_panes(data
->fs
.wl
->window
) == 1)
500 *tag
= (uint64_t)data
->fs
.wl
;
502 *tag
= (uint64_t)data
->fs
.wp
;
508 window_tree_draw_label(struct screen_write_ctx
*ctx
, u_int px
, u_int py
,
509 u_int sx
, u_int sy
, const struct grid_cell
*gc
, const char *label
)
515 if (sx
== 0 || sy
== 1 || len
> sx
)
517 ox
= (sx
- len
+ 1) / 2;
520 if (ox
> 1 && ox
+ len
< sx
- 1 && sy
>= 3) {
521 screen_write_cursormove(ctx
, px
+ ox
- 1, py
+ oy
- 1, 0);
522 screen_write_box(ctx
, len
+ 2, 3, BOX_LINES_DEFAULT
, NULL
,
525 screen_write_cursormove(ctx
, px
+ ox
, py
+ oy
, 0);
526 screen_write_puts(ctx
, gc
, "%s", label
);
530 window_tree_draw_session(struct window_tree_modedata
*data
, struct session
*s
,
531 struct screen_write_ctx
*ctx
, u_int sx
, u_int sy
)
533 struct options
*oo
= s
->options
;
536 u_int cx
= ctx
->s
->cx
, cy
= ctx
->s
->cy
;
537 u_int loop
, total
, visible
, each
, width
, offset
;
538 u_int current
, start
, end
, remaining
, i
;
540 int colour
, active_colour
, left
, right
;
543 total
= winlink_count(&s
->windows
);
545 memcpy(&gc
, &grid_default_cell
, sizeof gc
);
546 colour
= options_get_number(oo
, "display-panes-colour");
547 active_colour
= options_get_number(oo
, "display-panes-active-colour");
549 if (sx
/ total
< 24) {
557 RB_FOREACH(wl
, winlinks
, &s
->windows
) {
563 if (current
< visible
) {
566 } else if (current
>= total
- visible
) {
567 start
= total
- visible
;
570 start
= current
- (visible
/ 2);
571 end
= start
+ visible
;
574 if (data
->offset
< -(int)start
)
575 data
->offset
= -(int)start
;
576 if (data
->offset
> (int)(total
- end
))
577 data
->offset
= (int)(total
- end
);
578 start
+= data
->offset
;
582 right
= (end
!= total
);
583 if (((left
&& right
) && sx
<= 6) || ((left
|| right
) && sx
<= 3))
586 each
= (sx
- 6) / visible
;
587 remaining
= (sx
- 6) - (visible
* each
);
588 } else if (left
|| right
) {
589 each
= (sx
- 3) / visible
;
590 remaining
= (sx
- 3) - (visible
* each
);
593 remaining
= sx
- (visible
* each
);
600 screen_write_cursormove(ctx
, cx
+ 2, cy
, 0);
601 screen_write_vline(ctx
, sy
, 0, 0);
602 screen_write_cursormove(ctx
, cx
, cy
+ sy
/ 2, 0);
603 screen_write_puts(ctx
, &grid_default_cell
, "<");
607 data
->right
= cx
+ sx
- 3;
608 screen_write_cursormove(ctx
, cx
+ sx
- 3, cy
, 0);
609 screen_write_vline(ctx
, sy
, 0, 0);
610 screen_write_cursormove(ctx
, cx
+ sx
- 1, cy
+ sy
/ 2, 0);
611 screen_write_puts(ctx
, &grid_default_cell
, ">");
620 RB_FOREACH(wl
, winlinks
, &s
->windows
) {
630 gc
.fg
= active_colour
;
635 offset
= 3 + (i
* each
);
639 width
= each
+ remaining
;
643 screen_write_cursormove(ctx
, cx
+ offset
, cy
, 0);
644 screen_write_preview(ctx
, &w
->active
->base
, width
, sy
);
646 xasprintf(&label
, " %u:%s ", wl
->idx
, w
->name
);
647 if (strlen(label
) > width
)
648 xasprintf(&label
, " %u ", wl
->idx
);
649 window_tree_draw_label(ctx
, cx
+ offset
, cy
, width
, sy
, &gc
,
653 if (loop
!= end
- 1) {
654 screen_write_cursormove(ctx
, cx
+ offset
+ width
, cy
, 0);
655 screen_write_vline(ctx
, sy
, 0, 0);
664 window_tree_draw_window(struct window_tree_modedata
*data
, struct session
*s
,
665 struct window
*w
, struct screen_write_ctx
*ctx
, u_int sx
, u_int sy
)
667 struct options
*oo
= s
->options
;
668 struct window_pane
*wp
;
669 u_int cx
= ctx
->s
->cx
, cy
= ctx
->s
->cy
;
670 u_int loop
, total
, visible
, each
, width
, offset
;
671 u_int current
, start
, end
, remaining
, i
;
673 int colour
, active_colour
, left
, right
, pane_idx
;
676 total
= window_count_panes(w
);
678 memcpy(&gc
, &grid_default_cell
, sizeof gc
);
679 colour
= options_get_number(oo
, "display-panes-colour");
680 active_colour
= options_get_number(oo
, "display-panes-active-colour");
682 if (sx
/ total
< 24) {
690 TAILQ_FOREACH(wp
, &w
->panes
, entry
) {
696 if (current
< visible
) {
699 } else if (current
>= total
- visible
) {
700 start
= total
- visible
;
703 start
= current
- (visible
/ 2);
704 end
= start
+ visible
;
707 if (data
->offset
< -(int)start
)
708 data
->offset
= -(int)start
;
709 if (data
->offset
> (int)(total
- end
))
710 data
->offset
= (int)(total
- end
);
711 start
+= data
->offset
;
715 right
= (end
!= total
);
716 if (((left
&& right
) && sx
<= 6) || ((left
|| right
) && sx
<= 3))
719 each
= (sx
- 6) / visible
;
720 remaining
= (sx
- 6) - (visible
* each
);
721 } else if (left
|| right
) {
722 each
= (sx
- 3) / visible
;
723 remaining
= (sx
- 3) - (visible
* each
);
726 remaining
= sx
- (visible
* each
);
733 screen_write_cursormove(ctx
, cx
+ 2, cy
, 0);
734 screen_write_vline(ctx
, sy
, 0, 0);
735 screen_write_cursormove(ctx
, cx
, cy
+ sy
/ 2, 0);
736 screen_write_puts(ctx
, &grid_default_cell
, "<");
740 data
->right
= cx
+ sx
- 3;
741 screen_write_cursormove(ctx
, cx
+ sx
- 3, cy
, 0);
742 screen_write_vline(ctx
, sy
, 0, 0);
743 screen_write_cursormove(ctx
, cx
+ sx
- 1, cy
+ sy
/ 2, 0);
744 screen_write_puts(ctx
, &grid_default_cell
, ">");
753 TAILQ_FOREACH(wp
, &w
->panes
, entry
) {
762 gc
.fg
= active_colour
;
767 offset
= 3 + (i
* each
);
771 width
= each
+ remaining
;
775 screen_write_cursormove(ctx
, cx
+ offset
, cy
, 0);
776 screen_write_preview(ctx
, &wp
->base
, width
, sy
);
778 if (window_pane_index(wp
, &pane_idx
) != 0)
780 xasprintf(&label
, " %u ", pane_idx
);
781 window_tree_draw_label(ctx
, cx
+ offset
, cy
, each
, sy
, &gc
,
785 if (loop
!= end
- 1) {
786 screen_write_cursormove(ctx
, cx
+ offset
+ width
, cy
, 0);
787 screen_write_vline(ctx
, sy
, 0, 0);
796 window_tree_draw(void *modedata
, void *itemdata
, struct screen_write_ctx
*ctx
,
799 struct window_tree_itemdata
*item
= itemdata
;
802 struct window_pane
*wp
;
804 window_tree_pull_item(item
, &sp
, &wlp
, &wp
);
808 switch (item
->type
) {
809 case WINDOW_TREE_NONE
:
811 case WINDOW_TREE_SESSION
:
812 window_tree_draw_session(modedata
, sp
, ctx
, sx
, sy
);
814 case WINDOW_TREE_WINDOW
:
815 window_tree_draw_window(modedata
, sp
, wlp
->window
, ctx
, sx
, sy
);
817 case WINDOW_TREE_PANE
:
818 screen_write_preview(ctx
, &wp
->base
, sx
, sy
);
824 window_tree_search(__unused
void *modedata
, void *itemdata
, const char *ss
)
826 struct window_tree_itemdata
*item
= itemdata
;
829 struct window_pane
*wp
;
833 window_tree_pull_item(item
, &s
, &wl
, &wp
);
835 switch (item
->type
) {
836 case WINDOW_TREE_NONE
:
838 case WINDOW_TREE_SESSION
:
841 return (strstr(s
->name
, ss
) != NULL
);
842 case WINDOW_TREE_WINDOW
:
843 if (s
== NULL
|| wl
== NULL
)
845 return (strstr(wl
->window
->name
, ss
) != NULL
);
846 case WINDOW_TREE_PANE
:
847 if (s
== NULL
|| wl
== NULL
|| wp
== NULL
)
849 cmd
= get_proc_name(wp
->fd
, wp
->tty
);
850 if (cmd
== NULL
|| *cmd
== '\0')
852 retval
= (strstr(cmd
, ss
) != NULL
);
860 window_tree_menu(void *modedata
, struct client
*c
, key_code key
)
862 struct window_tree_modedata
*data
= modedata
;
863 struct window_pane
*wp
= data
->wp
;
864 struct window_mode_entry
*wme
;
866 wme
= TAILQ_FIRST(&wp
->modes
);
867 if (wme
== NULL
|| wme
->data
!= modedata
)
869 window_tree_key(wme
, c
, NULL
, NULL
, key
, NULL
);
873 window_tree_get_key(void *modedata
, void *itemdata
, u_int line
)
875 struct window_tree_modedata
*data
= modedata
;
876 struct window_tree_itemdata
*item
= itemdata
;
877 struct format_tree
*ft
;
880 struct window_pane
*wp
;
884 ft
= format_create(NULL
, NULL
, FORMAT_NONE
, 0);
885 window_tree_pull_item(item
, &s
, &wl
, &wp
);
886 if (item
->type
== WINDOW_TREE_SESSION
)
887 format_defaults(ft
, NULL
, s
, NULL
, NULL
);
888 else if (item
->type
== WINDOW_TREE_WINDOW
)
889 format_defaults(ft
, NULL
, s
, wl
, NULL
);
891 format_defaults(ft
, NULL
, s
, wl
, wp
);
892 format_add(ft
, "line", "%u", line
);
894 expanded
= format_expand(ft
, data
->key_format
);
895 key
= key_string_lookup_string(expanded
);
901 static struct screen
*
902 window_tree_init(struct window_mode_entry
*wme
, struct cmd_find_state
*fs
,
905 struct window_pane
*wp
= wme
->wp
;
906 struct window_tree_modedata
*data
;
909 wme
->data
= data
= xcalloc(1, sizeof *data
);
911 data
->references
= 1;
913 if (args_has(args
, 's'))
914 data
->type
= WINDOW_TREE_SESSION
;
915 else if (args_has(args
, 'w'))
916 data
->type
= WINDOW_TREE_WINDOW
;
918 data
->type
= WINDOW_TREE_PANE
;
919 memcpy(&data
->fs
, fs
, sizeof data
->fs
);
921 if (args
== NULL
|| !args_has(args
, 'F'))
922 data
->format
= xstrdup(WINDOW_TREE_DEFAULT_FORMAT
);
924 data
->format
= xstrdup(args_get(args
, 'F'));
925 if (args
== NULL
|| !args_has(args
, 'K'))
926 data
->key_format
= xstrdup(WINDOW_TREE_DEFAULT_KEY_FORMAT
);
928 data
->key_format
= xstrdup(args_get(args
, 'K'));
929 if (args
== NULL
|| args_count(args
) == 0)
930 data
->command
= xstrdup(WINDOW_TREE_DEFAULT_COMMAND
);
932 data
->command
= xstrdup(args_string(args
, 0));
933 data
->squash_groups
= !args_has(args
, 'G');
935 data
->data
= mode_tree_start(wp
, args
, window_tree_build
,
936 window_tree_draw
, window_tree_search
, window_tree_menu
, NULL
,
937 window_tree_get_key
, data
, window_tree_menu_items
,
938 window_tree_sort_list
, nitems(window_tree_sort_list
), &s
);
939 mode_tree_zoom(data
->data
, args
);
941 mode_tree_build(data
->data
);
942 mode_tree_draw(data
->data
);
944 data
->type
= WINDOW_TREE_NONE
;
950 window_tree_destroy(struct window_tree_modedata
*data
)
954 if (--data
->references
!= 0)
957 for (i
= 0; i
< data
->item_size
; i
++)
958 window_tree_free_item(data
->item_list
[i
]);
959 free(data
->item_list
);
962 free(data
->key_format
);
969 window_tree_free(struct window_mode_entry
*wme
)
971 struct window_tree_modedata
*data
= wme
->data
;
977 mode_tree_free(data
->data
);
978 window_tree_destroy(data
);
982 window_tree_resize(struct window_mode_entry
*wme
, u_int sx
, u_int sy
)
984 struct window_tree_modedata
*data
= wme
->data
;
986 mode_tree_resize(data
->data
, sx
, sy
);
990 window_tree_update(struct window_mode_entry
*wme
)
992 struct window_tree_modedata
*data
= wme
->data
;
994 mode_tree_build(data
->data
);
995 mode_tree_draw(data
->data
);
996 data
->wp
->flags
|= PANE_REDRAW
;
1000 window_tree_get_target(struct window_tree_itemdata
*item
,
1001 struct cmd_find_state
*fs
)
1005 struct window_pane
*wp
;
1008 window_tree_pull_item(item
, &s
, &wl
, &wp
);
1011 switch (item
->type
) {
1012 case WINDOW_TREE_NONE
:
1014 case WINDOW_TREE_SESSION
:
1017 xasprintf(&target
, "=%s:", s
->name
);
1019 case WINDOW_TREE_WINDOW
:
1020 if (s
== NULL
|| wl
== NULL
)
1022 xasprintf(&target
, "=%s:%u.", s
->name
, wl
->idx
);
1024 case WINDOW_TREE_PANE
:
1025 if (s
== NULL
|| wl
== NULL
|| wp
== NULL
)
1027 xasprintf(&target
, "=%s:%u.%%%u", s
->name
, wl
->idx
, wp
->id
);
1031 cmd_find_clear_state(fs
, 0);
1033 cmd_find_from_winlink_pane(fs
, wl
, wp
, 0);
1038 window_tree_command_each(void *modedata
, void *itemdata
, struct client
*c
,
1039 __unused key_code key
)
1041 struct window_tree_modedata
*data
= modedata
;
1042 struct window_tree_itemdata
*item
= itemdata
;
1044 struct cmd_find_state fs
;
1046 name
= window_tree_get_target(item
, &fs
);
1048 mode_tree_run_command(c
, &fs
, data
->entered
, name
);
1052 static enum cmd_retval
1053 window_tree_command_done(__unused
struct cmdq_item
*item
, void *modedata
)
1055 struct window_tree_modedata
*data
= modedata
;
1058 mode_tree_build(data
->data
);
1059 mode_tree_draw(data
->data
);
1060 data
->wp
->flags
|= PANE_REDRAW
;
1062 window_tree_destroy(data
);
1063 return (CMD_RETURN_NORMAL
);
1067 window_tree_command_callback(struct client
*c
, void *modedata
, const char *s
,
1070 struct window_tree_modedata
*data
= modedata
;
1072 if (s
== NULL
|| *s
== '\0' || data
->dead
)
1076 mode_tree_each_tagged(data
->data
, window_tree_command_each
, c
,
1078 data
->entered
= NULL
;
1081 cmdq_append(c
, cmdq_get_callback(window_tree_command_done
, data
));
1087 window_tree_command_free(void *modedata
)
1089 struct window_tree_modedata
*data
= modedata
;
1091 window_tree_destroy(data
);
1095 window_tree_kill_each(__unused
void *modedata
, void *itemdata
,
1096 __unused
struct client
*c
, __unused key_code key
)
1098 struct window_tree_itemdata
*item
= itemdata
;
1101 struct window_pane
*wp
;
1103 window_tree_pull_item(item
, &s
, &wl
, &wp
);
1105 switch (item
->type
) {
1106 case WINDOW_TREE_NONE
:
1108 case WINDOW_TREE_SESSION
:
1110 server_destroy_session(s
);
1111 session_destroy(s
, 1, __func__
);
1114 case WINDOW_TREE_WINDOW
:
1116 server_kill_window(wl
->window
, 0);
1118 case WINDOW_TREE_PANE
:
1120 server_kill_pane(wp
);
1126 window_tree_kill_current_callback(struct client
*c
, void *modedata
,
1127 const char *s
, __unused
int done
)
1129 struct window_tree_modedata
*data
= modedata
;
1130 struct mode_tree_data
*mtd
= data
->data
;
1132 if (s
== NULL
|| *s
== '\0' || data
->dead
)
1134 if (tolower((u_char
) s
[0]) != 'y' || s
[1] != '\0')
1137 window_tree_kill_each(data
, mode_tree_get_current(mtd
), c
, KEYC_NONE
);
1138 server_renumber_all();
1141 cmdq_append(c
, cmdq_get_callback(window_tree_command_done
, data
));
1147 window_tree_kill_tagged_callback(struct client
*c
, void *modedata
,
1148 const char *s
, __unused
int done
)
1150 struct window_tree_modedata
*data
= modedata
;
1151 struct mode_tree_data
*mtd
= data
->data
;
1153 if (s
== NULL
|| *s
== '\0' || data
->dead
)
1155 if (tolower((u_char
) s
[0]) != 'y' || s
[1] != '\0')
1158 mode_tree_each_tagged(mtd
, window_tree_kill_each
, c
, KEYC_NONE
, 1);
1159 server_renumber_all();
1162 cmdq_append(c
, cmdq_get_callback(window_tree_command_done
, data
));
1168 window_tree_mouse(struct window_tree_modedata
*data
, key_code key
, u_int x
,
1169 struct window_tree_itemdata
*item
)
1173 struct window_pane
*wp
;
1176 if (key
!= KEYC_MOUSEDOWN1_PANE
)
1179 if (data
->left
!= -1 && x
<= (u_int
)data
->left
)
1181 if (data
->right
!= -1 && x
>= (u_int
)data
->right
)
1184 if (data
->left
!= -1)
1188 if (x
== 0 || data
->end
== 0)
1192 if (data
->start
+ x
>= data
->end
)
1196 window_tree_pull_item(item
, &s
, &wl
, &wp
);
1197 if (item
->type
== WINDOW_TREE_SESSION
) {
1200 mode_tree_expand_current(data
->data
);
1202 RB_FOREACH(wl
, winlinks
, &s
->windows
) {
1203 if (loop
== data
->start
+ x
)
1208 mode_tree_set_current(data
->data
, (uint64_t)wl
);
1211 if (item
->type
== WINDOW_TREE_WINDOW
) {
1214 mode_tree_expand_current(data
->data
);
1216 TAILQ_FOREACH(wp
, &wl
->window
->panes
, entry
) {
1217 if (loop
== data
->start
+ x
)
1222 mode_tree_set_current(data
->data
, (uint64_t)wp
);
1229 window_tree_key(struct window_mode_entry
*wme
, struct client
*c
,
1230 __unused
struct session
*s
, __unused
struct winlink
*wl
, key_code key
,
1231 struct mouse_event
*m
)
1233 struct window_pane
*wp
= wme
->wp
;
1234 struct window_tree_modedata
*data
= wme
->data
;
1235 struct window_tree_itemdata
*item
, *new_item
;
1236 char *name
, *prompt
= NULL
;
1237 struct cmd_find_state fs
, *fsp
= &data
->fs
;
1239 u_int tagged
, x
, y
, idx
;
1241 struct winlink
*nwl
;
1242 struct window_pane
*nwp
;
1244 item
= mode_tree_get_current(data
->data
);
1245 finished
= mode_tree_key(data
->data
, c
, &key
, m
, &x
, &y
);
1246 if (item
!= (new_item
= mode_tree_get_current(data
->data
))) {
1250 if (KEYC_IS_MOUSE(key
) && m
!= NULL
)
1251 key
= window_tree_mouse(data
, key
, x
, item
);
1260 mode_tree_expand(data
->data
, (uint64_t)fsp
->s
);
1261 mode_tree_expand(data
->data
, (uint64_t)fsp
->wl
);
1262 if (!mode_tree_set_current(data
->data
, (uint64_t)wme
->wp
))
1263 mode_tree_set_current(data
->data
, (uint64_t)fsp
->wl
);
1266 window_tree_pull_item(item
, &ns
, &nwl
, &nwp
);
1267 server_set_marked(ns
, nwl
, nwp
);
1268 mode_tree_build(data
->data
);
1271 server_clear_marked();
1272 mode_tree_build(data
->data
);
1275 window_tree_pull_item(item
, &ns
, &nwl
, &nwp
);
1276 switch (item
->type
) {
1277 case WINDOW_TREE_NONE
:
1279 case WINDOW_TREE_SESSION
:
1282 xasprintf(&prompt
, "Kill session %s? ", ns
->name
);
1284 case WINDOW_TREE_WINDOW
:
1287 xasprintf(&prompt
, "Kill window %u? ", nwl
->idx
);
1289 case WINDOW_TREE_PANE
:
1290 if (nwp
== NULL
|| window_pane_index(nwp
, &idx
) != 0)
1292 xasprintf(&prompt
, "Kill pane %u? ", idx
);
1298 status_prompt_set(c
, NULL
, prompt
, "",
1299 window_tree_kill_current_callback
, window_tree_command_free
,
1300 data
, PROMPT_SINGLE
|PROMPT_NOFORMAT
, PROMPT_TYPE_COMMAND
);
1304 tagged
= mode_tree_count_tagged(data
->data
);
1307 xasprintf(&prompt
, "Kill %u tagged? ", tagged
);
1309 status_prompt_set(c
, NULL
, prompt
, "",
1310 window_tree_kill_tagged_callback
, window_tree_command_free
,
1311 data
, PROMPT_SINGLE
|PROMPT_NOFORMAT
, PROMPT_TYPE_COMMAND
);
1315 tagged
= mode_tree_count_tagged(data
->data
);
1317 xasprintf(&prompt
, "(%u tagged) ", tagged
);
1319 xasprintf(&prompt
, "(current) ");
1321 status_prompt_set(c
, NULL
, prompt
, "",
1322 window_tree_command_callback
, window_tree_command_free
,
1323 data
, PROMPT_NOFORMAT
, PROMPT_TYPE_COMMAND
);
1327 name
= window_tree_get_target(item
, &fs
);
1329 mode_tree_run_command(c
, NULL
, data
->command
, name
);
1335 window_pane_reset_mode(wp
);
1337 mode_tree_draw(data
->data
);
1338 wp
->flags
|= PANE_REDRAW
;