4 * Copyright (c) 2012 Thomas Adam <thomas@xteddy.org>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include <sys/types.h>
28 #define CMD_CHOOSE_TREE_WINDOW_ACTION "select-window -t '%%'"
29 #define CMD_CHOOSE_TREE_SESSION_ACTION "switch-client -t '%%'"
32 * Enter choice mode to choose a session and/or window.
35 enum cmd_retval
cmd_choose_tree_exec(struct cmd
*, struct cmd_q
*);
37 const struct cmd_entry cmd_choose_tree_entry
= {
39 "S:W:swub:c:t:", 0, 1,
40 "[-suw] [-b session-template] [-c window template] [-S format] " \
41 "[-W format] " CMD_TARGET_WINDOW_USAGE
,
48 const struct cmd_entry cmd_choose_session_entry
= {
49 "choose-session", NULL
,
51 CMD_TARGET_WINDOW_USAGE
" [-F format] [template]",
58 const struct cmd_entry cmd_choose_window_entry
= {
59 "choose-window", NULL
,
61 CMD_TARGET_WINDOW_USAGE
"[-F format] [template]",
69 cmd_choose_tree_exec(struct cmd
*self
, struct cmd_q
*cmdq
)
71 struct args
*args
= self
->args
;
72 struct winlink
*wl
, *wm
;
73 struct session
*s
, *s2
;
75 struct window_choose_data
*wcd
= NULL
;
76 const char *ses_template
, *win_template
;
77 char *final_win_action
, *cur_win_template
;
78 char *final_win_template_middle
;
79 char *final_win_template_last
;
80 const char *ses_action
, *win_action
;
81 u_int cur_win
, idx_ses
, win_ses
, win_max
;
84 ses_template
= win_template
= NULL
;
85 ses_action
= win_action
= NULL
;
87 if ((c
= cmd_current_client(cmdq
)) == NULL
) {
88 cmdq_error(cmdq
, "no client available");
89 return (CMD_RETURN_ERROR
);
92 if ((wl
= cmd_find_window(cmdq
, args_get(args
, 't'), &s
)) == NULL
)
93 return (CMD_RETURN_ERROR
);
95 if (window_pane_set_mode(wl
->window
->active
, &window_choose_mode
) != 0)
96 return (CMD_RETURN_NORMAL
);
98 /* Sort out which command this is. */
100 if (self
->entry
== &cmd_choose_session_entry
) {
102 if ((ses_template
= args_get(args
, 'F')) == NULL
)
103 ses_template
= CHOOSE_TREE_SESSION_TEMPLATE
;
106 ses_action
= args
->argv
[0];
108 ses_action
= CMD_CHOOSE_TREE_SESSION_ACTION
;
109 } else if (self
->entry
== &cmd_choose_window_entry
) {
111 if ((win_template
= args_get(args
, 'F')) == NULL
)
112 win_template
= CHOOSE_TREE_WINDOW_TEMPLATE
;
115 win_action
= args
->argv
[0];
117 win_action
= CMD_CHOOSE_TREE_WINDOW_ACTION
;
119 wflag
= args_has(args
, 'w');
120 sflag
= args_has(args
, 's');
122 if ((ses_action
= args_get(args
, 'b')) == NULL
)
123 ses_action
= CMD_CHOOSE_TREE_SESSION_ACTION
;
125 if ((win_action
= args_get(args
, 'c')) == NULL
)
126 win_action
= CMD_CHOOSE_TREE_WINDOW_ACTION
;
128 if ((ses_template
= args_get(args
, 'S')) == NULL
)
129 ses_template
= CHOOSE_TREE_SESSION_TEMPLATE
;
131 if ((win_template
= args_get(args
, 'W')) == NULL
)
132 win_template
= CHOOSE_TREE_WINDOW_TEMPLATE
;
136 * If not asking for windows and sessions, assume no "-ws" given and
137 * hence display the entire tree outright.
139 if (!wflag
&& !sflag
)
143 * If we're drawing in tree mode, including sessions, then pad the
144 * window template, otherwise just render the windows as a flat list
145 * without any padding.
147 if (wflag
&& sflag
) {
148 xasprintf(&final_win_template_middle
,
149 " \001tq\001> %s", win_template
);
150 xasprintf(&final_win_template_last
,
151 " \001mq\001> %s", win_template
);
153 final_win_template_middle
= xstrdup(win_template
);
154 final_win_template_last
= xstrdup(win_template
);
156 final_win_template_middle
= final_win_template_last
= NULL
;
158 idx_ses
= cur_win
= -1;
159 RB_FOREACH(s2
, sessions
, &sessions
) {
163 * If we're just choosing windows, jump straight there. Note
164 * that this implies the current session, so only choose
165 * windows when the session matches this one.
167 if (wflag
&& !sflag
) {
173 wcd
= window_choose_add_session(wl
->window
->active
,
174 c
, s2
, ses_template
, ses_action
, idx_ses
);
176 /* If we're just choosing sessions, skip choosing windows. */
177 if (sflag
&& !wflag
) {
183 win_ses
= win_max
= -1;
184 RB_FOREACH(wm
, winlinks
, &s2
->windows
)
186 RB_FOREACH(wm
, winlinks
, &s2
->windows
) {
191 if (wm
== s2
->curw
&& s
== s2
) {
192 if (wflag
&& !sflag
) {
194 * Then we're only counting windows.
195 * So remember which is the current
196 * window in the list.
203 xasprintf(&final_win_action
, "%s %s %s",
204 wcd
!= NULL
? wcd
->command
: "",
205 wcd
!= NULL
? ";" : "", win_action
);
207 if (win_ses
!= win_max
)
208 cur_win_template
= final_win_template_middle
;
210 cur_win_template
= final_win_template_last
;
212 window_choose_add_window(wl
->window
->active
,
213 c
, s2
, wm
, cur_win_template
,
215 (wflag
&& !sflag
) ? win_ses
: idx_ses
);
217 free(final_win_action
);
221 * If we're just drawing windows, don't consider moving on to
222 * other sessions as we only list windows in this session.
227 free(final_win_template_middle
);
228 free(final_win_template_last
);
230 window_choose_ready(wl
->window
->active
, cur_win
, NULL
);
231 window_choose_collapse_all(wl
->window
->active
);
233 if (args_has(args
, 'u')) {
234 window_choose_expand_all(wl
->window
->active
);
235 window_choose_set_current(wl
->window
->active
, cur_win
);
238 return (CMD_RETURN_NORMAL
);