Add -Wno-macro-redefined for macOS.
[tmux.git] / window-tree.c
blob07d2bc2e8b4b0f38fb8288bbc19ec9b811a5dfd6
1 /* $OpenBSD$ */
3 /*
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>
21 #include <ctype.h>
22 #include <stdlib.h>
23 #include <string.h>
25 #include "tmux.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,
31 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 \
40 "#{?pane_format," \
41 "#{?pane_marked,#[reverse],}" \
42 "#{pane_current_command}#{?pane_active,*,}#{?pane_marked,M,}" \
43 "#{?#{&&:#{pane_title},#{!=:#{pane_title},#{host_short}}},: \"#{pane_title}\",}" \
44 "," \
45 "#{?window_format," \
46 "#{?window_marked_flag,#[reverse],}" \
47 "#{window_name}#{window_flags}" \
48 "#{?#{&&:#{==:#{window_panes},1},#{&&:#{pane_title},#{!=:#{pane_title},#{host_short}}}},: \"#{pane_title}\",}" \
49 "," \
50 "#{session_windows} windows" \
51 "#{?session_grouped, " \
52 "(group #{session_group}: " \
53 "#{session_group_list})," \
54 "}" \
55 "#{?session_attached, (attached),}" \
56 "}" \
57 "}"
59 #define WINDOW_TREE_DEFAULT_KEY_FORMAT \
60 "#{?#{e|<:#{line},10}," \
61 "#{line}" \
62 "," \
63 "#{?#{e|<:#{line},36}," \
64 "M-#{a:#{e|+:97,#{e|-:#{line},10}}}" \
65 "," \
66 "" \
67 "}" \
68 "}"
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 },
75 { "Tag", 't', 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 = {
88 .name = "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 {
99 WINDOW_TREE_BY_INDEX,
100 WINDOW_TREE_BY_NAME,
101 WINDOW_TREE_BY_TIME,
103 static const char *window_tree_sort_list[] = {
104 "index",
105 "name",
106 "time"
108 static struct mode_tree_sort_criteria *window_tree_sort;
110 enum window_tree_type {
111 WINDOW_TREE_NONE,
112 WINDOW_TREE_SESSION,
113 WINDOW_TREE_WINDOW,
114 WINDOW_TREE_PANE,
117 struct window_tree_itemdata {
118 enum window_tree_type type;
119 int session;
120 int winlink;
121 int pane;
124 struct window_tree_modedata {
125 struct window_pane *wp;
126 int dead;
127 int references;
129 struct mode_tree_data *data;
130 char *format;
131 char *key_format;
132 char *command;
133 int squash_groups;
134 int prompt_flags;
136 struct window_tree_itemdata **item_list;
137 u_int item_size;
139 const char *entered;
141 struct cmd_find_state fs;
142 enum window_tree_type type;
144 int offset;
146 int left;
147 int right;
148 u_int start;
149 u_int end;
150 u_int each;
153 static void
154 window_tree_pull_item(struct window_tree_itemdata *item, struct session **sp,
155 struct winlink **wlp, struct window_pane **wp)
157 *wp = NULL;
158 *wlp = NULL;
159 *sp = session_find_by_id(item->session);
160 if (*sp == NULL)
161 return;
162 if (item->type == WINDOW_TREE_SESSION) {
163 *wlp = (*sp)->curw;
164 *wp = (*wlp)->window->active;
165 return;
168 *wlp = winlink_find_by_index(&(*sp)->windows, item->winlink);
169 if (*wlp == NULL) {
170 *sp = NULL;
171 return;
173 if (item->type == WINDOW_TREE_WINDOW) {
174 *wp = (*wlp)->window->active;
175 return;
178 *wp = window_pane_find_by_id(item->pane);
179 if (!window_has_pane((*wlp)->window, *wp))
180 *wp = NULL;
181 if (*wp == NULL) {
182 *sp = NULL;
183 *wlp = NULL;
184 return;
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);
196 return (item);
199 static void
200 window_tree_free_item(struct window_tree_itemdata *item)
202 free(item);
205 static int
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;
212 int result = 0;
214 switch (window_tree_sort->field) {
215 case WINDOW_TREE_BY_INDEX:
216 result = sa->id - sb->id;
217 break;
218 case WINDOW_TREE_BY_TIME:
219 if (timercmp(&sa->activity_time, &sb->activity_time, >)) {
220 result = -1;
221 break;
223 if (timercmp(&sa->activity_time, &sb->activity_time, <)) {
224 result = 1;
225 break;
227 /* FALLTHROUGH */
228 case WINDOW_TREE_BY_NAME:
229 result = strcmp(sa->name, sb->name);
230 break;
233 if (window_tree_sort->reversed)
234 result = -result;
235 return (result);
238 static int
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;
247 int result = 0;
249 switch (window_tree_sort->field) {
250 case WINDOW_TREE_BY_INDEX:
251 result = wla->idx - wlb->idx;
252 break;
253 case WINDOW_TREE_BY_TIME:
254 if (timercmp(&wa->activity_time, &wb->activity_time, >)) {
255 result = -1;
256 break;
258 if (timercmp(&wa->activity_time, &wb->activity_time, <)) {
259 result = 1;
260 break;
262 /* FALLTHROUGH */
263 case WINDOW_TREE_BY_NAME:
264 result = strcmp(wa->name, wb->name);
265 break;
268 if (window_tree_sort->reversed)
269 result = -result;
270 return (result);
273 static int
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;
278 int result;
279 u_int ai, bi;
281 if (window_tree_sort->field == WINDOW_TREE_BY_TIME)
282 result = (*a)->active_point - (*b)->active_point;
283 else {
285 * Panes don't have names, so use number order for any other
286 * sort field.
288 window_pane_index(*a, &ai);
289 window_pane_index(*b, &bi);
290 result = ai - bi;
292 if (window_tree_sort->reversed)
293 result = -result;
294 return (result);
297 static void
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;
303 char *name, *text;
304 u_int idx;
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;
312 item->pane = wp->id;
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);
318 free(text);
319 free(name);
322 static int
323 window_tree_filter_pane(struct session *s, struct winlink *wl,
324 struct window_pane *wp, const char *filter)
326 char *cp;
327 int result;
329 if (filter == NULL)
330 return (1);
332 cp = format_single(NULL, filter, NULL, s, wl, wp);
333 result = format_true(cp);
334 free(cp);
336 return (result);
339 static int
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;
347 char *name, *text;
348 struct window_pane *wp, **l;
349 u_int n, i;
350 int expanded;
352 item = window_tree_add_item(data);
353 item->type = WINDOW_TREE_WINDOW;
354 item->session = s->id;
355 item->winlink = wl->idx;
356 item->pane = -1;
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)
363 expanded = 0;
364 else
365 expanded = 1;
366 mti = mode_tree_add(data->data, parent, item, (uint64_t)wl, name, text,
367 expanded);
368 free(text);
369 free(name);
371 if ((wp = TAILQ_FIRST(&wl->window->panes)) == NULL)
372 goto empty;
373 if (TAILQ_NEXT(wp, entry) == NULL) {
374 if (!window_tree_filter_pane(s, wl, wp, filter))
375 goto empty;
376 return (1);
379 l = NULL;
380 n = 0;
382 TAILQ_FOREACH(wp, &wl->window->panes, entry) {
383 if (!window_tree_filter_pane(s, wl, wp, filter))
384 continue;
385 l = xreallocarray(l, n + 1, sizeof *l);
386 l[n++] = wp;
388 if (n == 0)
389 goto empty;
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);
396 free(l);
397 return (1);
399 empty:
400 window_tree_free_item(item);
401 data->item_size--;
402 mode_tree_remove(data->data, mti);
403 return (0);
406 static void
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;
413 char *text;
414 struct winlink *wl, **l;
415 u_int n, i, empty;
416 int expanded;
418 item = window_tree_add_item(data);
419 item->type = WINDOW_TREE_SESSION;
420 item->session = s->id;
421 item->winlink = -1;
422 item->pane = -1;
424 text = format_single(NULL, data->format, NULL, s, NULL, NULL);
426 if (data->type == WINDOW_TREE_SESSION)
427 expanded = 0;
428 else
429 expanded = 1;
430 mti = mode_tree_add(data->data, NULL, item, (uint64_t)s, s->name, text,
431 expanded);
432 free(text);
434 l = NULL;
435 n = 0;
436 RB_FOREACH(wl, winlinks, &s->windows) {
437 l = xreallocarray(l, n + 1, sizeof *l);
438 l[n++] = wl;
440 window_tree_sort = sort_crit;
441 qsort(l, n, sizeof *l, window_tree_cmp_window);
443 empty = 0;
444 for (i = 0; i < n; i++) {
445 if (!window_tree_build_window(s, l[i], modedata, sort_crit, mti,
446 filter))
447 empty++;
449 if (empty == n) {
450 window_tree_free_item(item);
451 data->item_size--;
452 mode_tree_remove(data->data, mti);
454 free(l);
457 static void
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;
464 u_int n, i;
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;
472 data->item_size = 0;
474 l = NULL;
475 n = 0;
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)))
481 continue;
483 l = xreallocarray(l, n + 1, sizeof *l);
484 l[n++] = s;
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);
491 free(l);
493 switch (data->type) {
494 case WINDOW_TREE_NONE:
495 break;
496 case WINDOW_TREE_SESSION:
497 *tag = (uint64_t)data->fs.s;
498 break;
499 case WINDOW_TREE_WINDOW:
500 *tag = (uint64_t)data->fs.wl;
501 break;
502 case WINDOW_TREE_PANE:
503 if (window_count_panes(data->fs.wl->window) == 1)
504 *tag = (uint64_t)data->fs.wl;
505 else
506 *tag = (uint64_t)data->fs.wp;
507 break;
511 static void
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)
515 size_t len;
516 u_int ox, oy;
518 len = strlen(label);
519 if (sx == 0 || sy == 1 || len > sx)
520 return;
521 ox = (sx - len + 1) / 2;
522 oy = (sy + 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,
527 NULL);
529 screen_write_cursormove(ctx, px + ox, py + oy, 0);
530 screen_write_puts(ctx, gc, "%s", label);
533 static void
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;
538 struct winlink *wl;
539 struct window *w;
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;
543 struct grid_cell gc;
544 int colour, active_colour, left, right;
545 char *label;
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) {
554 visible = sx / 24;
555 if (visible == 0)
556 visible = 1;
557 } else
558 visible = total;
560 current = 0;
561 RB_FOREACH(wl, winlinks, &s->windows) {
562 if (wl == s->curw)
563 break;
564 current++;
567 if (current < visible) {
568 start = 0;
569 end = visible;
570 } else if (current >= total - visible) {
571 start = total - visible;
572 end = total;
573 } else {
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;
583 end += data->offset;
585 left = (start != 0);
586 right = (end != total);
587 if (((left && right) && sx <= 6) || ((left || right) && sx <= 3))
588 left = right = 0;
589 if (left && right) {
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);
595 } else {
596 each = sx / visible;
597 remaining = sx - (visible * each);
599 if (each == 0)
600 return;
602 if (left) {
603 data->left = cx + 2;
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, "<");
608 } else
609 data->left = -1;
610 if (right) {
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, ">");
616 } else
617 data->right = -1;
619 data->start = start;
620 data->end = end;
621 data->each = each;
623 i = loop = 0;
624 RB_FOREACH(wl, winlinks, &s->windows) {
625 if (loop == end)
626 break;
627 if (loop < start) {
628 loop++;
629 continue;
631 w = wl->window;
633 if (wl == s->curw)
634 gc.fg = active_colour;
635 else
636 gc.fg = colour;
638 if (left)
639 offset = 3 + (i * each);
640 else
641 offset = (i * each);
642 if (loop == end - 1)
643 width = each + remaining;
644 else
645 width = each - 1;
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,
654 label);
655 free(label);
657 if (loop != end - 1) {
658 screen_write_cursormove(ctx, cx + offset + width, cy, 0);
659 screen_write_vline(ctx, sy, 0, 0);
661 loop++;
663 i++;
667 static void
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;
676 struct grid_cell gc;
677 int colour, active_colour, left, right;
678 char *label;
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) {
687 visible = sx / 24;
688 if (visible == 0)
689 visible = 1;
690 } else
691 visible = total;
693 current = 0;
694 TAILQ_FOREACH(wp, &w->panes, entry) {
695 if (wp == w->active)
696 break;
697 current++;
700 if (current < visible) {
701 start = 0;
702 end = visible;
703 } else if (current >= total - visible) {
704 start = total - visible;
705 end = total;
706 } else {
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;
716 end += data->offset;
718 left = (start != 0);
719 right = (end != total);
720 if (((left && right) && sx <= 6) || ((left || right) && sx <= 3))
721 left = right = 0;
722 if (left && right) {
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);
728 } else {
729 each = sx / visible;
730 remaining = sx - (visible * each);
732 if (each == 0)
733 return;
735 if (left) {
736 data->left = cx + 2;
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, "<");
741 } else
742 data->left = -1;
743 if (right) {
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, ">");
749 } else
750 data->right = -1;
752 data->start = start;
753 data->end = end;
754 data->each = each;
756 i = loop = 0;
757 TAILQ_FOREACH(wp, &w->panes, entry) {
758 if (loop == end)
759 break;
760 if (loop < start) {
761 loop++;
762 continue;
765 if (wp == w->active)
766 gc.fg = active_colour;
767 else
768 gc.fg = colour;
770 if (left)
771 offset = 3 + (i * each);
772 else
773 offset = (i * each);
774 if (loop == end - 1)
775 width = each + remaining;
776 else
777 width = each - 1;
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)
783 pane_idx = loop;
784 xasprintf(&label, " %u ", pane_idx);
785 window_tree_draw_label(ctx, cx + offset, cy, each, sy, &gc,
786 label);
787 free(label);
789 if (loop != end - 1) {
790 screen_write_cursormove(ctx, cx + offset + width, cy, 0);
791 screen_write_vline(ctx, sy, 0, 0);
793 loop++;
795 i++;
799 static void
800 window_tree_draw(void *modedata, void *itemdata, struct screen_write_ctx *ctx,
801 u_int sx, u_int sy)
803 struct window_tree_itemdata *item = itemdata;
804 struct session *sp;
805 struct winlink *wlp;
806 struct window_pane *wp;
808 window_tree_pull_item(item, &sp, &wlp, &wp);
809 if (wp == NULL)
810 return;
812 switch (item->type) {
813 case WINDOW_TREE_NONE:
814 break;
815 case WINDOW_TREE_SESSION:
816 window_tree_draw_session(modedata, sp, ctx, sx, sy);
817 break;
818 case WINDOW_TREE_WINDOW:
819 window_tree_draw_window(modedata, sp, wlp->window, ctx, sx, sy);
820 break;
821 case WINDOW_TREE_PANE:
822 screen_write_preview(ctx, &wp->base, sx, sy);
823 break;
827 static int
828 window_tree_search(__unused void *modedata, void *itemdata, const char *ss)
830 struct window_tree_itemdata *item = itemdata;
831 struct session *s;
832 struct winlink *wl;
833 struct window_pane *wp;
834 char *cmd;
835 int retval;
837 window_tree_pull_item(item, &s, &wl, &wp);
839 switch (item->type) {
840 case WINDOW_TREE_NONE:
841 return (0);
842 case WINDOW_TREE_SESSION:
843 if (s == NULL)
844 return (0);
845 return (strstr(s->name, ss) != NULL);
846 case WINDOW_TREE_WINDOW:
847 if (s == NULL || wl == NULL)
848 return (0);
849 return (strstr(wl->window->name, ss) != NULL);
850 case WINDOW_TREE_PANE:
851 if (s == NULL || wl == NULL || wp == NULL)
852 break;
853 cmd = osdep_get_name(wp->fd, wp->tty);
854 if (cmd == NULL || *cmd == '\0')
855 return (0);
856 retval = (strstr(cmd, ss) != NULL);
857 free(cmd);
858 return (retval);
860 return (0);
863 static void
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)
872 return;
873 window_tree_key(wme, c, NULL, NULL, key, NULL);
876 static key_code
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;
882 struct session *s;
883 struct winlink *wl;
884 struct window_pane *wp;
885 char *expanded;
886 key_code key;
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);
894 else
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);
900 free(expanded);
901 format_free(ft);
902 return (key);
905 static struct screen *
906 window_tree_init(struct window_mode_entry *wme, struct cmd_find_state *fs,
907 struct args *args)
909 struct window_pane *wp = wme->wp;
910 struct window_tree_modedata *data;
911 struct screen *s;
913 wme->data = data = xcalloc(1, sizeof *data);
914 data->wp = wp;
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;
921 else
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);
927 else
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);
931 else
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);
935 else
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;
952 return (s);
955 static void
956 window_tree_destroy(struct window_tree_modedata *data)
958 u_int i;
960 if (--data->references != 0)
961 return;
963 for (i = 0; i < data->item_size; i++)
964 window_tree_free_item(data->item_list[i]);
965 free(data->item_list);
967 free(data->format);
968 free(data->key_format);
969 free(data->command);
971 free(data);
974 static void
975 window_tree_free(struct window_mode_entry *wme)
977 struct window_tree_modedata *data = wme->data;
979 if (data == NULL)
980 return;
982 data->dead = 1;
983 mode_tree_free(data->data);
984 window_tree_destroy(data);
987 static void
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);
995 static void
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;
1005 static char *
1006 window_tree_get_target(struct window_tree_itemdata *item,
1007 struct cmd_find_state *fs)
1009 struct session *s;
1010 struct winlink *wl;
1011 struct window_pane *wp;
1012 char *target;
1014 window_tree_pull_item(item, &s, &wl, &wp);
1016 target = NULL;
1017 switch (item->type) {
1018 case WINDOW_TREE_NONE:
1019 break;
1020 case WINDOW_TREE_SESSION:
1021 if (s == NULL)
1022 break;
1023 xasprintf(&target, "=%s:", s->name);
1024 break;
1025 case WINDOW_TREE_WINDOW:
1026 if (s == NULL || wl == NULL)
1027 break;
1028 xasprintf(&target, "=%s:%u.", s->name, wl->idx);
1029 break;
1030 case WINDOW_TREE_PANE:
1031 if (s == NULL || wl == NULL || wp == NULL)
1032 break;
1033 xasprintf(&target, "=%s:%u.%%%u", s->name, wl->idx, wp->id);
1034 break;
1036 if (target == NULL)
1037 cmd_find_clear_state(fs, 0);
1038 else
1039 cmd_find_from_winlink_pane(fs, wl, wp, 0);
1040 return (target);
1043 static void
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;
1049 char *name;
1050 struct cmd_find_state fs;
1052 name = window_tree_get_target(item, &fs);
1053 if (name != NULL)
1054 mode_tree_run_command(c, &fs, data->entered, name);
1055 free(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;
1063 if (!data->dead) {
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);
1072 static int
1073 window_tree_command_callback(struct client *c, void *modedata, const char *s,
1074 __unused int done)
1076 struct window_tree_modedata *data = modedata;
1078 if (s == NULL || *s == '\0' || data->dead)
1079 return (0);
1081 data->entered = s;
1082 mode_tree_each_tagged(data->data, window_tree_command_each, c,
1083 KEYC_NONE, 1);
1084 data->entered = NULL;
1086 data->references++;
1087 cmdq_append(c, cmdq_get_callback(window_tree_command_done, data));
1089 return (0);
1092 static void
1093 window_tree_command_free(void *modedata)
1095 struct window_tree_modedata *data = modedata;
1097 window_tree_destroy(data);
1100 static void
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;
1105 struct session *s;
1106 struct winlink *wl;
1107 struct window_pane *wp;
1109 window_tree_pull_item(item, &s, &wl, &wp);
1111 switch (item->type) {
1112 case WINDOW_TREE_NONE:
1113 break;
1114 case WINDOW_TREE_SESSION:
1115 if (s != NULL) {
1116 server_destroy_session(s);
1117 session_destroy(s, 1, __func__);
1119 break;
1120 case WINDOW_TREE_WINDOW:
1121 if (wl != NULL)
1122 server_kill_window(wl->window, 0);
1123 break;
1124 case WINDOW_TREE_PANE:
1125 if (wp != NULL)
1126 server_kill_pane(wp);
1127 break;
1131 static int
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)
1139 return (0);
1140 if (tolower((u_char) s[0]) != 'y' || s[1] != '\0')
1141 return (0);
1143 window_tree_kill_each(data, mode_tree_get_current(mtd), c, KEYC_NONE);
1144 server_renumber_all();
1146 data->references++;
1147 cmdq_append(c, cmdq_get_callback(window_tree_command_done, data));
1149 return (0);
1152 static int
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)
1160 return (0);
1161 if (tolower((u_char) s[0]) != 'y' || s[1] != '\0')
1162 return (0);
1164 mode_tree_each_tagged(mtd, window_tree_kill_each, c, KEYC_NONE, 1);
1165 server_renumber_all();
1167 data->references++;
1168 cmdq_append(c, cmdq_get_callback(window_tree_command_done, data));
1170 return (0);
1173 static key_code
1174 window_tree_mouse(struct window_tree_modedata *data, key_code key, u_int x,
1175 struct window_tree_itemdata *item)
1177 struct session *s;
1178 struct winlink *wl;
1179 struct window_pane *wp;
1180 u_int loop;
1182 if (key != KEYC_MOUSEDOWN1_PANE)
1183 return (KEYC_NONE);
1185 if (data->left != -1 && x <= (u_int)data->left)
1186 return ('<');
1187 if (data->right != -1 && x >= (u_int)data->right)
1188 return ('>');
1190 if (data->left != -1)
1191 x -= data->left;
1192 else if (x != 0)
1193 x--;
1194 if (x == 0 || data->end == 0)
1195 x = 0;
1196 else {
1197 x = x / data->each;
1198 if (data->start + x >= data->end)
1199 x = data->end - 1;
1202 window_tree_pull_item(item, &s, &wl, &wp);
1203 if (item->type == WINDOW_TREE_SESSION) {
1204 if (s == NULL)
1205 return (KEYC_NONE);
1206 mode_tree_expand_current(data->data);
1207 loop = 0;
1208 RB_FOREACH(wl, winlinks, &s->windows) {
1209 if (loop == data->start + x)
1210 break;
1211 loop++;
1213 if (wl != NULL)
1214 mode_tree_set_current(data->data, (uint64_t)wl);
1215 return ('\r');
1217 if (item->type == WINDOW_TREE_WINDOW) {
1218 if (wl == NULL)
1219 return (KEYC_NONE);
1220 mode_tree_expand_current(data->data);
1221 loop = 0;
1222 TAILQ_FOREACH(wp, &wl->window->panes, entry) {
1223 if (loop == data->start + x)
1224 break;
1225 loop++;
1227 if (wp != NULL)
1228 mode_tree_set_current(data->data, (uint64_t)wp);
1229 return ('\r');
1231 return (KEYC_NONE);
1234 static void
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;
1244 int finished;
1245 u_int tagged, x, y, idx;
1246 struct session *ns;
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);
1253 again:
1254 if (item != (new_item = mode_tree_get_current(data->data))) {
1255 item = new_item;
1256 data->offset = 0;
1258 if (KEYC_IS_MOUSE(key) && m != NULL) {
1259 key = window_tree_mouse(data, key, x, item);
1260 goto again;
1263 switch (key) {
1264 case '<':
1265 data->offset--;
1266 break;
1267 case '>':
1268 data->offset++;
1269 break;
1270 case 'H':
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);
1275 break;
1276 case 'm':
1277 window_tree_pull_item(item, &ns, &nwl, &nwp);
1278 server_set_marked(ns, nwl, nwp);
1279 mode_tree_build(data->data);
1280 break;
1281 case 'M':
1282 server_clear_marked();
1283 mode_tree_build(data->data);
1284 break;
1285 case 'x':
1286 window_tree_pull_item(item, &ns, &nwl, &nwp);
1287 switch (item->type) {
1288 case WINDOW_TREE_NONE:
1289 break;
1290 case WINDOW_TREE_SESSION:
1291 if (ns == NULL)
1292 break;
1293 xasprintf(&prompt, "Kill session %s? ", ns->name);
1294 break;
1295 case WINDOW_TREE_WINDOW:
1296 if (nwl == NULL)
1297 break;
1298 xasprintf(&prompt, "Kill window %u? ", nwl->idx);
1299 break;
1300 case WINDOW_TREE_PANE:
1301 if (nwp == NULL || window_pane_index(nwp, &idx) != 0)
1302 break;
1303 xasprintf(&prompt, "Kill pane %u? ", idx);
1304 break;
1306 if (prompt == NULL)
1307 break;
1308 data->references++;
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);
1313 free(prompt);
1314 break;
1315 case 'X':
1316 tagged = mode_tree_count_tagged(data->data);
1317 if (tagged == 0)
1318 break;
1319 xasprintf(&prompt, "Kill %u tagged? ", tagged);
1320 data->references++;
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);
1325 free(prompt);
1326 break;
1327 case ':':
1328 tagged = mode_tree_count_tagged(data->data);
1329 if (tagged != 0)
1330 xasprintf(&prompt, "(%u tagged) ", tagged);
1331 else
1332 xasprintf(&prompt, "(current) ");
1333 data->references++;
1334 status_prompt_set(c, NULL, prompt, "",
1335 window_tree_command_callback, window_tree_command_free,
1336 data, PROMPT_NOFORMAT, PROMPT_TYPE_COMMAND);
1337 free(prompt);
1338 break;
1339 case '\r':
1340 name = window_tree_get_target(item, &fs);
1341 if (name != NULL)
1342 mode_tree_run_command(c, NULL, data->command, name);
1343 finished = 1;
1344 free(name);
1345 break;
1347 if (finished)
1348 window_pane_reset_mode(wp);
1349 else {
1350 mode_tree_draw(data->data);
1351 wp->flags |= PANE_REDRAW;