Annotation of src/usr.bin/tmux/cmd-choose-tree.c, Revision 1.29
1.29 ! nicm 1: /* $OpenBSD: cmd-choose-tree.c,v 1.28 2015/04/27 16:25:57 nicm Exp $ */
1.1 nicm 2:
3: /*
4: * Copyright (c) 2012 Thomas Adam <thomas@xteddy.org>
5: *
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.
9: *
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.
17: */
18:
19: #include <sys/types.h>
20:
21: #include <ctype.h>
1.3 nicm 22: #include <stdlib.h>
1.1 nicm 23:
24: #include <string.h>
25:
26: #include "tmux.h"
27:
28: #define CMD_CHOOSE_TREE_WINDOW_ACTION "select-window -t '%%'"
29: #define CMD_CHOOSE_TREE_SESSION_ACTION "switch-client -t '%%'"
30:
31: /*
32: * Enter choice mode to choose a session and/or window.
33: */
1.27 nicm 34:
35: #define CHOOSE_TREE_SESSION_TEMPLATE \
36: "#{session_name}: #{session_windows} windows" \
37: "#{?session_grouped, (group ,}" \
38: "#{session_group}#{?session_grouped,),}" \
39: "#{?session_attached, (attached),}"
40: #define CHOOSE_TREE_WINDOW_TEMPLATE \
41: "#{window_index}: #{window_name}#{window_flags} " \
42: "\"#{pane_title}\""
1.1 nicm 43:
1.21 nicm 44: enum cmd_retval cmd_choose_tree_exec(struct cmd *, struct cmd_q *);
1.1 nicm 45:
46: const struct cmd_entry cmd_choose_tree_entry = {
47: "choose-tree", NULL,
1.12 nicm 48: "S:W:swub:c:t:", 0, 1,
1.17 nicm 49: "[-suw] [-b session-template] [-c window template] [-S format] " \
1.2 nicm 50: "[-W format] " CMD_TARGET_WINDOW_USAGE,
1.29 ! nicm 51: CMD_WINDOW_T,
1.1 nicm 52: cmd_choose_tree_exec
53: };
54:
55: const struct cmd_entry cmd_choose_session_entry = {
56: "choose-session", NULL,
57: "F:t:", 0, 1,
58: CMD_TARGET_WINDOW_USAGE " [-F format] [template]",
1.29 ! nicm 59: CMD_WINDOW_T,
1.1 nicm 60: cmd_choose_tree_exec
61: };
62:
63: const struct cmd_entry cmd_choose_window_entry = {
64: "choose-window", NULL,
65: "F:t:", 0, 1,
66: CMD_TARGET_WINDOW_USAGE "[-F format] [template]",
1.29 ! nicm 67: CMD_WINDOW_T,
1.1 nicm 68: cmd_choose_tree_exec
69: };
70:
1.4 nicm 71: enum cmd_retval
1.21 nicm 72: cmd_choose_tree_exec(struct cmd *self, struct cmd_q *cmdq)
1.1 nicm 73: {
74: struct args *args = self->args;
1.29 ! nicm 75: struct client *c = cmdq->state.c;
! 76: struct winlink *wl = cmdq->state.tflag.wl, *wm;
! 77: struct session *s = cmdq->state.tflag.s, *s2;
1.1 nicm 78: struct window_choose_data *wcd = NULL;
79: const char *ses_template, *win_template;
1.8 nicm 80: char *final_win_action, *cur_win_template;
81: char *final_win_template_middle;
82: char *final_win_template_last;
1.1 nicm 83: const char *ses_action, *win_action;
1.8 nicm 84: u_int cur_win, idx_ses, win_ses, win_max;
1.1 nicm 85: u_int wflag, sflag;
86:
87: ses_template = win_template = NULL;
88: ses_action = win_action = NULL;
89:
1.29 ! nicm 90: if (c == NULL) {
1.21 nicm 91: cmdq_error(cmdq, "no client available");
1.4 nicm 92: return (CMD_RETURN_ERROR);
1.1 nicm 93: }
94:
95: if (window_pane_set_mode(wl->window->active, &window_choose_mode) != 0)
1.4 nicm 96: return (CMD_RETURN_NORMAL);
1.1 nicm 97:
98: /* Sort out which command this is. */
99: wflag = sflag = 0;
100: if (self->entry == &cmd_choose_session_entry) {
101: sflag = 1;
102: if ((ses_template = args_get(args, 'F')) == NULL)
1.7 nicm 103: ses_template = CHOOSE_TREE_SESSION_TEMPLATE;
1.1 nicm 104:
105: if (args->argc != 0)
106: ses_action = args->argv[0];
107: else
108: ses_action = CMD_CHOOSE_TREE_SESSION_ACTION;
109: } else if (self->entry == &cmd_choose_window_entry) {
110: wflag = 1;
111: if ((win_template = args_get(args, 'F')) == NULL)
1.7 nicm 112: win_template = CHOOSE_TREE_WINDOW_TEMPLATE;
1.1 nicm 113:
114: if (args->argc != 0)
115: win_action = args->argv[0];
116: else
117: win_action = CMD_CHOOSE_TREE_WINDOW_ACTION;
118: } else {
119: wflag = args_has(args, 'w');
120: sflag = args_has(args, 's');
121:
122: if ((ses_action = args_get(args, 'b')) == NULL)
123: ses_action = CMD_CHOOSE_TREE_SESSION_ACTION;
124:
125: if ((win_action = args_get(args, 'c')) == NULL)
126: win_action = CMD_CHOOSE_TREE_WINDOW_ACTION;
127:
128: if ((ses_template = args_get(args, 'S')) == NULL)
1.7 nicm 129: ses_template = CHOOSE_TREE_SESSION_TEMPLATE;
1.1 nicm 130:
131: if ((win_template = args_get(args, 'W')) == NULL)
1.7 nicm 132: win_template = CHOOSE_TREE_WINDOW_TEMPLATE;
1.1 nicm 133: }
134:
135: /*
136: * If not asking for windows and sessions, assume no "-ws" given and
137: * hence display the entire tree outright.
138: */
139: if (!wflag && !sflag)
140: wflag = sflag = 1;
141:
142: /*
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.
146: */
1.8 nicm 147: if (wflag && sflag) {
1.11 nicm 148: xasprintf(&final_win_template_middle,
149: " \001tq\001> %s", win_template);
150: xasprintf(&final_win_template_last,
151: " \001mq\001> %s", win_template);
1.8 nicm 152: } else if (wflag) {
153: final_win_template_middle = xstrdup(win_template);
154: final_win_template_last = xstrdup(win_template);
155: } else
156: final_win_template_middle = final_win_template_last = NULL;
1.1 nicm 157:
158: idx_ses = cur_win = -1;
159: RB_FOREACH(s2, sessions, &sessions) {
160: idx_ses++;
161:
162: /*
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.
166: */
167: if (wflag && !sflag) {
168: if (s != s2)
169: continue;
170: goto windows_only;
171: }
172:
173: wcd = window_choose_add_session(wl->window->active,
1.18 nicm 174: c, s2, ses_template, ses_action, idx_ses);
1.1 nicm 175:
176: /* If we're just choosing sessions, skip choosing windows. */
177: if (sflag && !wflag) {
178: if (s == s2)
179: cur_win = idx_ses;
180: continue;
181: }
182: windows_only:
1.8 nicm 183: win_ses = win_max = -1;
184: RB_FOREACH(wm, winlinks, &s2->windows)
185: win_max++;
1.1 nicm 186: RB_FOREACH(wm, winlinks, &s2->windows) {
187: win_ses++;
188: if (sflag && wflag)
189: idx_ses++;
190:
191: if (wm == s2->curw && s == s2) {
192: if (wflag && !sflag) {
193: /*
194: * Then we're only counting windows.
195: * So remember which is the current
196: * window in the list.
197: */
198: cur_win = win_ses;
199: } else
200: cur_win = idx_ses;
201: }
202:
1.20 nicm 203: xasprintf(&final_win_action, "%s %s %s",
204: wcd != NULL ? wcd->command : "",
205: wcd != NULL ? ";" : "", win_action);
1.8 nicm 206:
207: if (win_ses != win_max)
208: cur_win_template = final_win_template_middle;
209: else
210: cur_win_template = final_win_template_last;
1.1 nicm 211:
212: window_choose_add_window(wl->window->active,
1.16 nicm 213: c, s2, wm, cur_win_template,
1.10 nicm 214: final_win_action,
215: (wflag && !sflag) ? win_ses : idx_ses);
1.1 nicm 216:
1.3 nicm 217: free(final_win_action);
1.1 nicm 218: }
1.13 nicm 219:
1.1 nicm 220: /*
221: * If we're just drawing windows, don't consider moving on to
222: * other sessions as we only list windows in this session.
223: */
224: if (wflag && !sflag)
225: break;
226: }
1.8 nicm 227: free(final_win_template_middle);
228: free(final_win_template_last);
1.1 nicm 229:
1.15 nicm 230: window_choose_ready(wl->window->active, cur_win, NULL);
1.12 nicm 231:
1.23 nicm 232: if (args_has(args, 'u')) {
1.12 nicm 233: window_choose_expand_all(wl->window->active);
1.23 nicm 234: window_choose_set_current(wl->window->active, cur_win);
235: }
1.1 nicm 236:
1.4 nicm 237: return (CMD_RETURN_NORMAL);
1.1 nicm 238: }