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
;
136 struct window_tree_itemdata
**item_list
;
141 struct cmd_find_state fs
;
142 enum window_tree_type type
;
154 window_tree_pull_item(struct window_tree_itemdata
*item
, struct session
**sp
,
155 struct winlink
**wlp
, struct window_pane
**wp
)
159 *sp
= session_find_by_id(item
->session
);
162 if (item
->type
== WINDOW_TREE_SESSION
) {
164 *wp
= (*wlp
)->window
->active
;
168 *wlp
= winlink_find_by_index(&(*sp
)->windows
, item
->winlink
);
173 if (item
->type
== WINDOW_TREE_WINDOW
) {
174 *wp
= (*wlp
)->window
->active
;
178 *wp
= window_pane_find_by_id(item
->pane
);
179 if (!window_has_pane((*wlp
)->window
, *wp
))
188 static struct window_tree_itemdata
*
189 window_tree_add_item(struct window_tree_modedata
*data
)
191 struct window_tree_itemdata
*item
;
193 data
->item_list
= xreallocarray(data
->item_list
, data
->item_size
+ 1,
194 sizeof *data
->item_list
);
195 item
= data
->item_list
[data
->item_size
++] = xcalloc(1, sizeof *item
);
200 window_tree_free_item(struct window_tree_itemdata
*item
)
206 window_tree_cmp_session(const void *a0
, const void *b0
)
208 const struct session
*const *a
= a0
;
209 const struct session
*const *b
= b0
;
210 const struct session
*sa
= *a
;
211 const struct session
*sb
= *b
;
214 switch (window_tree_sort
->field
) {
215 case WINDOW_TREE_BY_INDEX
:
216 result
= sa
->id
- sb
->id
;
218 case WINDOW_TREE_BY_TIME
:
219 if (timercmp(&sa
->activity_time
, &sb
->activity_time
, >)) {
223 if (timercmp(&sa
->activity_time
, &sb
->activity_time
, <)) {
228 case WINDOW_TREE_BY_NAME
:
229 result
= strcmp(sa
->name
, sb
->name
);
233 if (window_tree_sort
->reversed
)
239 window_tree_cmp_window(const void *a0
, const void *b0
)
241 const struct winlink
*const *a
= a0
;
242 const struct winlink
*const *b
= b0
;
243 const struct winlink
*wla
= *a
;
244 const struct winlink
*wlb
= *b
;
245 struct window
*wa
= wla
->window
;
246 struct window
*wb
= wlb
->window
;
249 switch (window_tree_sort
->field
) {
250 case WINDOW_TREE_BY_INDEX
:
251 result
= wla
->idx
- wlb
->idx
;
253 case WINDOW_TREE_BY_TIME
:
254 if (timercmp(&wa
->activity_time
, &wb
->activity_time
, >)) {
258 if (timercmp(&wa
->activity_time
, &wb
->activity_time
, <)) {
263 case WINDOW_TREE_BY_NAME
:
264 result
= strcmp(wa
->name
, wb
->name
);
268 if (window_tree_sort
->reversed
)
274 window_tree_cmp_pane(const void *a0
, const void *b0
)
276 struct window_pane
**a
= (struct window_pane
**)a0
;
277 struct window_pane
**b
= (struct window_pane
**)b0
;
281 if (window_tree_sort
->field
== WINDOW_TREE_BY_TIME
)
282 result
= (*a
)->active_point
- (*b
)->active_point
;
285 * Panes don't have names, so use number order for any other
288 window_pane_index(*a
, &ai
);
289 window_pane_index(*b
, &bi
);
292 if (window_tree_sort
->reversed
)
298 window_tree_build_pane(struct session
*s
, struct winlink
*wl
,
299 struct window_pane
*wp
, void *modedata
, struct mode_tree_item
*parent
)
301 struct window_tree_modedata
*data
= modedata
;
302 struct window_tree_itemdata
*item
;
306 window_pane_index(wp
, &idx
);
308 item
= window_tree_add_item(data
);
309 item
->type
= WINDOW_TREE_PANE
;
310 item
->session
= s
->id
;
311 item
->winlink
= wl
->idx
;
314 text
= format_single(NULL
, data
->format
, NULL
, s
, wl
, wp
);
315 xasprintf(&name
, "%u", idx
);
317 mode_tree_add(data
->data
, parent
, item
, (uint64_t)wp
, name
, text
, -1);
323 window_tree_filter_pane(struct session
*s
, struct winlink
*wl
,
324 struct window_pane
*wp
, const char *filter
)
332 cp
= format_single(NULL
, filter
, NULL
, s
, wl
, wp
);
333 result
= format_true(cp
);
340 window_tree_build_window(struct session
*s
, struct winlink
*wl
,
341 void *modedata
, struct mode_tree_sort_criteria
*sort_crit
,
342 struct mode_tree_item
*parent
, const char *filter
)
344 struct window_tree_modedata
*data
= modedata
;
345 struct window_tree_itemdata
*item
;
346 struct mode_tree_item
*mti
;
348 struct window_pane
*wp
, **l
;
352 item
= window_tree_add_item(data
);
353 item
->type
= WINDOW_TREE_WINDOW
;
354 item
->session
= s
->id
;
355 item
->winlink
= wl
->idx
;
358 text
= format_single(NULL
, data
->format
, NULL
, s
, wl
, NULL
);
359 xasprintf(&name
, "%u", wl
->idx
);
361 if (data
->type
== WINDOW_TREE_SESSION
||
362 data
->type
== WINDOW_TREE_WINDOW
)
366 mti
= mode_tree_add(data
->data
, parent
, item
, (uint64_t)wl
, name
, text
,
371 if ((wp
= TAILQ_FIRST(&wl
->window
->panes
)) == NULL
)
373 if (TAILQ_NEXT(wp
, entry
) == NULL
) {
374 if (!window_tree_filter_pane(s
, wl
, wp
, filter
))
382 TAILQ_FOREACH(wp
, &wl
->window
->panes
, entry
) {
383 if (!window_tree_filter_pane(s
, wl
, wp
, filter
))
385 l
= xreallocarray(l
, n
+ 1, sizeof *l
);
391 window_tree_sort
= sort_crit
;
392 qsort(l
, n
, sizeof *l
, window_tree_cmp_pane
);
394 for (i
= 0; i
< n
; i
++)
395 window_tree_build_pane(s
, wl
, l
[i
], modedata
, mti
);
400 window_tree_free_item(item
);
402 mode_tree_remove(data
->data
, mti
);
407 window_tree_build_session(struct session
*s
, void *modedata
,
408 struct mode_tree_sort_criteria
*sort_crit
, const char *filter
)
410 struct window_tree_modedata
*data
= modedata
;
411 struct window_tree_itemdata
*item
;
412 struct mode_tree_item
*mti
;
414 struct winlink
*wl
, **l
;
418 item
= window_tree_add_item(data
);
419 item
->type
= WINDOW_TREE_SESSION
;
420 item
->session
= s
->id
;
424 text
= format_single(NULL
, data
->format
, NULL
, s
, NULL
, NULL
);
426 if (data
->type
== WINDOW_TREE_SESSION
)
430 mti
= mode_tree_add(data
->data
, NULL
, item
, (uint64_t)s
, s
->name
, text
,
436 RB_FOREACH(wl
, winlinks
, &s
->windows
) {
437 l
= xreallocarray(l
, n
+ 1, sizeof *l
);
440 window_tree_sort
= sort_crit
;
441 qsort(l
, n
, sizeof *l
, window_tree_cmp_window
);
444 for (i
= 0; i
< n
; i
++) {
445 if (!window_tree_build_window(s
, l
[i
], modedata
, sort_crit
, mti
,
450 window_tree_free_item(item
);
452 mode_tree_remove(data
->data
, mti
);
458 window_tree_build(void *modedata
, struct mode_tree_sort_criteria
*sort_crit
,
459 uint64_t *tag
, const char *filter
)
461 struct window_tree_modedata
*data
= modedata
;
462 struct session
*s
, **l
;
463 struct session_group
*sg
, *current
;
466 current
= session_group_contains(data
->fs
.s
);
468 for (i
= 0; i
< data
->item_size
; i
++)
469 window_tree_free_item(data
->item_list
[i
]);
470 free(data
->item_list
);
471 data
->item_list
= NULL
;
476 RB_FOREACH(s
, sessions
, &sessions
) {
477 if (data
->squash_groups
&&
478 (sg
= session_group_contains(s
)) != NULL
) {
479 if ((sg
== current
&& s
!= data
->fs
.s
) ||
480 (sg
!= current
&& s
!= TAILQ_FIRST(&sg
->sessions
)))
483 l
= xreallocarray(l
, n
+ 1, sizeof *l
);
486 window_tree_sort
= sort_crit
;
487 qsort(l
, n
, sizeof *l
, window_tree_cmp_session
);
489 for (i
= 0; i
< n
; i
++)
490 window_tree_build_session(l
[i
], modedata
, sort_crit
, filter
);
493 switch (data
->type
) {
494 case WINDOW_TREE_NONE
:
496 case WINDOW_TREE_SESSION
:
497 *tag
= (uint64_t)data
->fs
.s
;
499 case WINDOW_TREE_WINDOW
:
500 *tag
= (uint64_t)data
->fs
.wl
;
502 case WINDOW_TREE_PANE
:
503 if (window_count_panes(data
->fs
.wl
->window
) == 1)
504 *tag
= (uint64_t)data
->fs
.wl
;
506 *tag
= (uint64_t)data
->fs
.wp
;
512 window_tree_draw_label(struct screen_write_ctx
*ctx
, u_int px
, u_int py
,
513 u_int sx
, u_int sy
, const struct grid_cell
*gc
, const char *label
)
519 if (sx
== 0 || sy
== 1 || len
> sx
)
521 ox
= (sx
- len
+ 1) / 2;
524 if (ox
> 1 && ox
+ len
< sx
- 1 && sy
>= 3) {
525 screen_write_cursormove(ctx
, px
+ ox
- 1, py
+ oy
- 1, 0);
526 screen_write_box(ctx
, len
+ 2, 3, BOX_LINES_DEFAULT
, NULL
,
529 screen_write_cursormove(ctx
, px
+ ox
, py
+ oy
, 0);
530 screen_write_puts(ctx
, gc
, "%s", label
);
534 window_tree_draw_session(struct window_tree_modedata
*data
, struct session
*s
,
535 struct screen_write_ctx
*ctx
, u_int sx
, u_int sy
)
537 struct options
*oo
= s
->options
;
540 u_int cx
= ctx
->s
->cx
, cy
= ctx
->s
->cy
;
541 u_int loop
, total
, visible
, each
, width
, offset
;
542 u_int current
, start
, end
, remaining
, i
;
544 int colour
, active_colour
, left
, right
;
547 total
= winlink_count(&s
->windows
);
549 memcpy(&gc
, &grid_default_cell
, sizeof gc
);
550 colour
= options_get_number(oo
, "display-panes-colour");
551 active_colour
= options_get_number(oo
, "display-panes-active-colour");
553 if (sx
/ total
< 24) {
561 RB_FOREACH(wl
, winlinks
, &s
->windows
) {
567 if (current
< visible
) {
570 } else if (current
>= total
- visible
) {
571 start
= total
- visible
;
574 start
= current
- (visible
/ 2);
575 end
= start
+ visible
;
578 if (data
->offset
< -(int)start
)
579 data
->offset
= -(int)start
;
580 if (data
->offset
> (int)(total
- end
))
581 data
->offset
= (int)(total
- end
);
582 start
+= data
->offset
;
586 right
= (end
!= total
);
587 if (((left
&& right
) && sx
<= 6) || ((left
|| right
) && sx
<= 3))
590 each
= (sx
- 6) / visible
;
591 remaining
= (sx
- 6) - (visible
* each
);
592 } else if (left
|| right
) {
593 each
= (sx
- 3) / visible
;
594 remaining
= (sx
- 3) - (visible
* each
);
597 remaining
= sx
- (visible
* each
);
604 screen_write_cursormove(ctx
, cx
+ 2, cy
, 0);
605 screen_write_vline(ctx
, sy
, 0, 0);
606 screen_write_cursormove(ctx
, cx
, cy
+ sy
/ 2, 0);
607 screen_write_puts(ctx
, &grid_default_cell
, "<");
611 data
->right
= cx
+ sx
- 3;
612 screen_write_cursormove(ctx
, cx
+ sx
- 3, cy
, 0);
613 screen_write_vline(ctx
, sy
, 0, 0);
614 screen_write_cursormove(ctx
, cx
+ sx
- 1, cy
+ sy
/ 2, 0);
615 screen_write_puts(ctx
, &grid_default_cell
, ">");
624 RB_FOREACH(wl
, winlinks
, &s
->windows
) {
634 gc
.fg
= active_colour
;
639 offset
= 3 + (i
* each
);
643 width
= each
+ remaining
;
647 screen_write_cursormove(ctx
, cx
+ offset
, cy
, 0);
648 screen_write_preview(ctx
, &w
->active
->base
, width
, sy
);
650 xasprintf(&label
, " %u:%s ", wl
->idx
, w
->name
);
651 if (strlen(label
) > width
)
652 xasprintf(&label
, " %u ", wl
->idx
);
653 window_tree_draw_label(ctx
, cx
+ offset
, cy
, width
, sy
, &gc
,
657 if (loop
!= end
- 1) {
658 screen_write_cursormove(ctx
, cx
+ offset
+ width
, cy
, 0);
659 screen_write_vline(ctx
, sy
, 0, 0);
668 window_tree_draw_window(struct window_tree_modedata
*data
, struct session
*s
,
669 struct window
*w
, struct screen_write_ctx
*ctx
, u_int sx
, u_int sy
)
671 struct options
*oo
= s
->options
;
672 struct window_pane
*wp
;
673 u_int cx
= ctx
->s
->cx
, cy
= ctx
->s
->cy
;
674 u_int loop
, total
, visible
, each
, width
, offset
;
675 u_int current
, start
, end
, remaining
, i
, pane_idx
;
677 int colour
, active_colour
, left
, right
;
680 total
= window_count_panes(w
);
682 memcpy(&gc
, &grid_default_cell
, sizeof gc
);
683 colour
= options_get_number(oo
, "display-panes-colour");
684 active_colour
= options_get_number(oo
, "display-panes-active-colour");
686 if (sx
/ total
< 24) {
694 TAILQ_FOREACH(wp
, &w
->panes
, entry
) {
700 if (current
< visible
) {
703 } else if (current
>= total
- visible
) {
704 start
= total
- visible
;
707 start
= current
- (visible
/ 2);
708 end
= start
+ visible
;
711 if (data
->offset
< -(int)start
)
712 data
->offset
= -(int)start
;
713 if (data
->offset
> (int)(total
- end
))
714 data
->offset
= (int)(total
- end
);
715 start
+= data
->offset
;
719 right
= (end
!= total
);
720 if (((left
&& right
) && sx
<= 6) || ((left
|| right
) && sx
<= 3))
723 each
= (sx
- 6) / visible
;
724 remaining
= (sx
- 6) - (visible
* each
);
725 } else if (left
|| right
) {
726 each
= (sx
- 3) / visible
;
727 remaining
= (sx
- 3) - (visible
* each
);
730 remaining
= sx
- (visible
* each
);
737 screen_write_cursormove(ctx
, cx
+ 2, cy
, 0);
738 screen_write_vline(ctx
, sy
, 0, 0);
739 screen_write_cursormove(ctx
, cx
, cy
+ sy
/ 2, 0);
740 screen_write_puts(ctx
, &grid_default_cell
, "<");
744 data
->right
= cx
+ sx
- 3;
745 screen_write_cursormove(ctx
, cx
+ sx
- 3, cy
, 0);
746 screen_write_vline(ctx
, sy
, 0, 0);
747 screen_write_cursormove(ctx
, cx
+ sx
- 1, cy
+ sy
/ 2, 0);
748 screen_write_puts(ctx
, &grid_default_cell
, ">");
757 TAILQ_FOREACH(wp
, &w
->panes
, entry
) {
766 gc
.fg
= active_colour
;
771 offset
= 3 + (i
* each
);
775 width
= each
+ remaining
;
779 screen_write_cursormove(ctx
, cx
+ offset
, cy
, 0);
780 screen_write_preview(ctx
, &wp
->base
, width
, sy
);
782 if (window_pane_index(wp
, &pane_idx
) != 0)
784 xasprintf(&label
, " %u ", pane_idx
);
785 window_tree_draw_label(ctx
, cx
+ offset
, cy
, each
, sy
, &gc
,
789 if (loop
!= end
- 1) {
790 screen_write_cursormove(ctx
, cx
+ offset
+ width
, cy
, 0);
791 screen_write_vline(ctx
, sy
, 0, 0);
800 window_tree_draw(void *modedata
, void *itemdata
, struct screen_write_ctx
*ctx
,
803 struct window_tree_itemdata
*item
= itemdata
;
806 struct window_pane
*wp
;
808 window_tree_pull_item(item
, &sp
, &wlp
, &wp
);
812 switch (item
->type
) {
813 case WINDOW_TREE_NONE
:
815 case WINDOW_TREE_SESSION
:
816 window_tree_draw_session(modedata
, sp
, ctx
, sx
, sy
);
818 case WINDOW_TREE_WINDOW
:
819 window_tree_draw_window(modedata
, sp
, wlp
->window
, ctx
, sx
, sy
);
821 case WINDOW_TREE_PANE
:
822 screen_write_preview(ctx
, &wp
->base
, sx
, sy
);
828 window_tree_search(__unused
void *modedata
, void *itemdata
, const char *ss
)
830 struct window_tree_itemdata
*item
= itemdata
;
833 struct window_pane
*wp
;
837 window_tree_pull_item(item
, &s
, &wl
, &wp
);
839 switch (item
->type
) {
840 case WINDOW_TREE_NONE
:
842 case WINDOW_TREE_SESSION
:
845 return (strstr(s
->name
, ss
) != NULL
);
846 case WINDOW_TREE_WINDOW
:
847 if (s
== NULL
|| wl
== NULL
)
849 return (strstr(wl
->window
->name
, ss
) != NULL
);
850 case WINDOW_TREE_PANE
:
851 if (s
== NULL
|| wl
== NULL
|| wp
== NULL
)
853 cmd
= get_proc_name(wp
->fd
, wp
->tty
);
854 if (cmd
== NULL
|| *cmd
== '\0')
856 retval
= (strstr(cmd
, ss
) != NULL
);
864 window_tree_menu(void *modedata
, struct client
*c
, key_code key
)
866 struct window_tree_modedata
*data
= modedata
;
867 struct window_pane
*wp
= data
->wp
;
868 struct window_mode_entry
*wme
;
870 wme
= TAILQ_FIRST(&wp
->modes
);
871 if (wme
== NULL
|| wme
->data
!= modedata
)
873 window_tree_key(wme
, c
, NULL
, NULL
, key
, NULL
);
877 window_tree_get_key(void *modedata
, void *itemdata
, u_int line
)
879 struct window_tree_modedata
*data
= modedata
;
880 struct window_tree_itemdata
*item
= itemdata
;
881 struct format_tree
*ft
;
884 struct window_pane
*wp
;
888 ft
= format_create(NULL
, NULL
, FORMAT_NONE
, 0);
889 window_tree_pull_item(item
, &s
, &wl
, &wp
);
890 if (item
->type
== WINDOW_TREE_SESSION
)
891 format_defaults(ft
, NULL
, s
, NULL
, NULL
);
892 else if (item
->type
== WINDOW_TREE_WINDOW
)
893 format_defaults(ft
, NULL
, s
, wl
, NULL
);
895 format_defaults(ft
, NULL
, s
, wl
, wp
);
896 format_add(ft
, "line", "%u", line
);
898 expanded
= format_expand(ft
, data
->key_format
);
899 key
= key_string_lookup_string(expanded
);
905 static struct screen
*
906 window_tree_init(struct window_mode_entry
*wme
, struct cmd_find_state
*fs
,
909 struct window_pane
*wp
= wme
->wp
;
910 struct window_tree_modedata
*data
;
913 wme
->data
= data
= xcalloc(1, sizeof *data
);
915 data
->references
= 1;
917 if (args_has(args
, 's'))
918 data
->type
= WINDOW_TREE_SESSION
;
919 else if (args_has(args
, 'w'))
920 data
->type
= WINDOW_TREE_WINDOW
;
922 data
->type
= WINDOW_TREE_PANE
;
923 memcpy(&data
->fs
, fs
, sizeof data
->fs
);
925 if (args
== NULL
|| !args_has(args
, 'F'))
926 data
->format
= xstrdup(WINDOW_TREE_DEFAULT_FORMAT
);
928 data
->format
= xstrdup(args_get(args
, 'F'));
929 if (args
== NULL
|| !args_has(args
, 'K'))
930 data
->key_format
= xstrdup(WINDOW_TREE_DEFAULT_KEY_FORMAT
);
932 data
->key_format
= xstrdup(args_get(args
, 'K'));
933 if (args
== NULL
|| args_count(args
) == 0)
934 data
->command
= xstrdup(WINDOW_TREE_DEFAULT_COMMAND
);
936 data
->command
= xstrdup(args_string(args
, 0));
937 data
->squash_groups
= !args_has(args
, 'G');
938 if (args_has(args
, 'y'))
939 data
->prompt_flags
= PROMPT_ACCEPT
;
941 data
->data
= mode_tree_start(wp
, args
, window_tree_build
,
942 window_tree_draw
, window_tree_search
, window_tree_menu
, NULL
,
943 window_tree_get_key
, data
, window_tree_menu_items
,
944 window_tree_sort_list
, nitems(window_tree_sort_list
), &s
);
945 mode_tree_zoom(data
->data
, args
);
947 mode_tree_build(data
->data
);
948 mode_tree_draw(data
->data
);
950 data
->type
= WINDOW_TREE_NONE
;
956 window_tree_destroy(struct window_tree_modedata
*data
)
960 if (--data
->references
!= 0)
963 for (i
= 0; i
< data
->item_size
; i
++)
964 window_tree_free_item(data
->item_list
[i
]);
965 free(data
->item_list
);
968 free(data
->key_format
);
975 window_tree_free(struct window_mode_entry
*wme
)
977 struct window_tree_modedata
*data
= wme
->data
;
983 mode_tree_free(data
->data
);
984 window_tree_destroy(data
);
988 window_tree_resize(struct window_mode_entry
*wme
, u_int sx
, u_int sy
)
990 struct window_tree_modedata
*data
= wme
->data
;
992 mode_tree_resize(data
->data
, sx
, sy
);
996 window_tree_update(struct window_mode_entry
*wme
)
998 struct window_tree_modedata
*data
= wme
->data
;
1000 mode_tree_build(data
->data
);
1001 mode_tree_draw(data
->data
);
1002 data
->wp
->flags
|= PANE_REDRAW
;
1006 window_tree_get_target(struct window_tree_itemdata
*item
,
1007 struct cmd_find_state
*fs
)
1011 struct window_pane
*wp
;
1014 window_tree_pull_item(item
, &s
, &wl
, &wp
);
1017 switch (item
->type
) {
1018 case WINDOW_TREE_NONE
:
1020 case WINDOW_TREE_SESSION
:
1023 xasprintf(&target
, "=%s:", s
->name
);
1025 case WINDOW_TREE_WINDOW
:
1026 if (s
== NULL
|| wl
== NULL
)
1028 xasprintf(&target
, "=%s:%u.", s
->name
, wl
->idx
);
1030 case WINDOW_TREE_PANE
:
1031 if (s
== NULL
|| wl
== NULL
|| wp
== NULL
)
1033 xasprintf(&target
, "=%s:%u.%%%u", s
->name
, wl
->idx
, wp
->id
);
1037 cmd_find_clear_state(fs
, 0);
1039 cmd_find_from_winlink_pane(fs
, wl
, wp
, 0);
1044 window_tree_command_each(void *modedata
, void *itemdata
, struct client
*c
,
1045 __unused key_code key
)
1047 struct window_tree_modedata
*data
= modedata
;
1048 struct window_tree_itemdata
*item
= itemdata
;
1050 struct cmd_find_state fs
;
1052 name
= window_tree_get_target(item
, &fs
);
1054 mode_tree_run_command(c
, &fs
, data
->entered
, name
);
1058 static enum cmd_retval
1059 window_tree_command_done(__unused
struct cmdq_item
*item
, void *modedata
)
1061 struct window_tree_modedata
*data
= modedata
;
1064 mode_tree_build(data
->data
);
1065 mode_tree_draw(data
->data
);
1066 data
->wp
->flags
|= PANE_REDRAW
;
1068 window_tree_destroy(data
);
1069 return (CMD_RETURN_NORMAL
);
1073 window_tree_command_callback(struct client
*c
, void *modedata
, const char *s
,
1076 struct window_tree_modedata
*data
= modedata
;
1078 if (s
== NULL
|| *s
== '\0' || data
->dead
)
1082 mode_tree_each_tagged(data
->data
, window_tree_command_each
, c
,
1084 data
->entered
= NULL
;
1087 cmdq_append(c
, cmdq_get_callback(window_tree_command_done
, data
));
1093 window_tree_command_free(void *modedata
)
1095 struct window_tree_modedata
*data
= modedata
;
1097 window_tree_destroy(data
);
1101 window_tree_kill_each(__unused
void *modedata
, void *itemdata
,
1102 __unused
struct client
*c
, __unused key_code key
)
1104 struct window_tree_itemdata
*item
= itemdata
;
1107 struct window_pane
*wp
;
1109 window_tree_pull_item(item
, &s
, &wl
, &wp
);
1111 switch (item
->type
) {
1112 case WINDOW_TREE_NONE
:
1114 case WINDOW_TREE_SESSION
:
1116 server_destroy_session(s
);
1117 session_destroy(s
, 1, __func__
);
1120 case WINDOW_TREE_WINDOW
:
1122 server_kill_window(wl
->window
, 0);
1124 case WINDOW_TREE_PANE
:
1126 server_kill_pane(wp
);
1132 window_tree_kill_current_callback(struct client
*c
, void *modedata
,
1133 const char *s
, __unused
int done
)
1135 struct window_tree_modedata
*data
= modedata
;
1136 struct mode_tree_data
*mtd
= data
->data
;
1138 if (s
== NULL
|| *s
== '\0' || data
->dead
)
1140 if (tolower((u_char
) s
[0]) != 'y' || s
[1] != '\0')
1143 window_tree_kill_each(data
, mode_tree_get_current(mtd
), c
, KEYC_NONE
);
1144 server_renumber_all();
1147 cmdq_append(c
, cmdq_get_callback(window_tree_command_done
, data
));
1153 window_tree_kill_tagged_callback(struct client
*c
, void *modedata
,
1154 const char *s
, __unused
int done
)
1156 struct window_tree_modedata
*data
= modedata
;
1157 struct mode_tree_data
*mtd
= data
->data
;
1159 if (s
== NULL
|| *s
== '\0' || data
->dead
)
1161 if (tolower((u_char
) s
[0]) != 'y' || s
[1] != '\0')
1164 mode_tree_each_tagged(mtd
, window_tree_kill_each
, c
, KEYC_NONE
, 1);
1165 server_renumber_all();
1168 cmdq_append(c
, cmdq_get_callback(window_tree_command_done
, data
));
1174 window_tree_mouse(struct window_tree_modedata
*data
, key_code key
, u_int x
,
1175 struct window_tree_itemdata
*item
)
1179 struct window_pane
*wp
;
1182 if (key
!= KEYC_MOUSEDOWN1_PANE
)
1185 if (data
->left
!= -1 && x
<= (u_int
)data
->left
)
1187 if (data
->right
!= -1 && x
>= (u_int
)data
->right
)
1190 if (data
->left
!= -1)
1194 if (x
== 0 || data
->end
== 0)
1198 if (data
->start
+ x
>= data
->end
)
1202 window_tree_pull_item(item
, &s
, &wl
, &wp
);
1203 if (item
->type
== WINDOW_TREE_SESSION
) {
1206 mode_tree_expand_current(data
->data
);
1208 RB_FOREACH(wl
, winlinks
, &s
->windows
) {
1209 if (loop
== data
->start
+ x
)
1214 mode_tree_set_current(data
->data
, (uint64_t)wl
);
1217 if (item
->type
== WINDOW_TREE_WINDOW
) {
1220 mode_tree_expand_current(data
->data
);
1222 TAILQ_FOREACH(wp
, &wl
->window
->panes
, entry
) {
1223 if (loop
== data
->start
+ x
)
1228 mode_tree_set_current(data
->data
, (uint64_t)wp
);
1235 window_tree_key(struct window_mode_entry
*wme
, struct client
*c
,
1236 __unused
struct session
*s
, __unused
struct winlink
*wl
, key_code key
,
1237 struct mouse_event
*m
)
1239 struct window_pane
*wp
= wme
->wp
;
1240 struct window_tree_modedata
*data
= wme
->data
;
1241 struct window_tree_itemdata
*item
, *new_item
;
1242 char *name
, *prompt
= NULL
;
1243 struct cmd_find_state fs
, *fsp
= &data
->fs
;
1245 u_int tagged
, x
, y
, idx
;
1247 struct winlink
*nwl
;
1248 struct window_pane
*nwp
;
1250 item
= mode_tree_get_current(data
->data
);
1251 finished
= mode_tree_key(data
->data
, c
, &key
, m
, &x
, &y
);
1254 if (item
!= (new_item
= mode_tree_get_current(data
->data
))) {
1258 if (KEYC_IS_MOUSE(key
) && m
!= NULL
) {
1259 key
= window_tree_mouse(data
, key
, x
, item
);
1271 mode_tree_expand(data
->data
, (uint64_t)fsp
->s
);
1272 mode_tree_expand(data
->data
, (uint64_t)fsp
->wl
);
1273 if (!mode_tree_set_current(data
->data
, (uint64_t)wme
->wp
))
1274 mode_tree_set_current(data
->data
, (uint64_t)fsp
->wl
);
1277 window_tree_pull_item(item
, &ns
, &nwl
, &nwp
);
1278 server_set_marked(ns
, nwl
, nwp
);
1279 mode_tree_build(data
->data
);
1282 server_clear_marked();
1283 mode_tree_build(data
->data
);
1286 window_tree_pull_item(item
, &ns
, &nwl
, &nwp
);
1287 switch (item
->type
) {
1288 case WINDOW_TREE_NONE
:
1290 case WINDOW_TREE_SESSION
:
1293 xasprintf(&prompt
, "Kill session %s? ", ns
->name
);
1295 case WINDOW_TREE_WINDOW
:
1298 xasprintf(&prompt
, "Kill window %u? ", nwl
->idx
);
1300 case WINDOW_TREE_PANE
:
1301 if (nwp
== NULL
|| window_pane_index(nwp
, &idx
) != 0)
1303 xasprintf(&prompt
, "Kill pane %u? ", idx
);
1309 status_prompt_set(c
, NULL
, prompt
, "",
1310 window_tree_kill_current_callback
, window_tree_command_free
,
1311 data
, PROMPT_SINGLE
|PROMPT_NOFORMAT
|data
->prompt_flags
,
1312 PROMPT_TYPE_COMMAND
);
1316 tagged
= mode_tree_count_tagged(data
->data
);
1319 xasprintf(&prompt
, "Kill %u tagged? ", tagged
);
1321 status_prompt_set(c
, NULL
, prompt
, "",
1322 window_tree_kill_tagged_callback
, window_tree_command_free
,
1323 data
, PROMPT_SINGLE
|PROMPT_NOFORMAT
|data
->prompt_flags
,
1324 PROMPT_TYPE_COMMAND
);
1328 tagged
= mode_tree_count_tagged(data
->data
);
1330 xasprintf(&prompt
, "(%u tagged) ", tagged
);
1332 xasprintf(&prompt
, "(current) ");
1334 status_prompt_set(c
, NULL
, prompt
, "",
1335 window_tree_command_callback
, window_tree_command_free
,
1336 data
, PROMPT_NOFORMAT
, PROMPT_TYPE_COMMAND
);
1340 name
= window_tree_get_target(item
, &fs
);
1342 mode_tree_run_command(c
, NULL
, data
->command
, name
);
1348 window_pane_reset_mode(wp
);
1350 mode_tree_draw(data
->data
);
1351 wp
->flags
|= PANE_REDRAW
;