Annotation of src/usr.bin/tmux/cmd-choose-tree.c, Revision 1.12
1.12 ! nicm 1: /* $OpenBSD: cmd-choose-tree.c,v 1.11 2012/09/24 12:53:55 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: */
34:
1.6 nicm 35: enum cmd_retval cmd_choose_tree_exec(struct cmd *, struct cmd_ctx *);
1.1 nicm 36:
37: void cmd_choose_tree_callback(struct window_choose_data *);
38: void cmd_choose_tree_free(struct window_choose_data *);
39:
40: const struct cmd_entry cmd_choose_tree_entry = {
41: "choose-tree", NULL,
1.12 ! nicm 42: "S:W:swub:c:t:", 0, 1,
! 43: "[-swu] [-b session-template] [-c window template] [-S format] " \
1.2 nicm 44: "[-W format] " CMD_TARGET_WINDOW_USAGE,
1.1 nicm 45: 0,
46: NULL,
47: NULL,
48: cmd_choose_tree_exec
49: };
50:
51: const struct cmd_entry cmd_choose_session_entry = {
52: "choose-session", NULL,
53: "F:t:", 0, 1,
54: CMD_TARGET_WINDOW_USAGE " [-F format] [template]",
55: 0,
56: NULL,
57: NULL,
58: cmd_choose_tree_exec
59: };
60:
61: const struct cmd_entry cmd_choose_window_entry = {
62: "choose-window", NULL,
63: "F:t:", 0, 1,
64: CMD_TARGET_WINDOW_USAGE "[-F format] [template]",
65: 0,
66: NULL,
67: NULL,
68: cmd_choose_tree_exec
69: };
70:
1.4 nicm 71: enum cmd_retval
1.1 nicm 72: cmd_choose_tree_exec(struct cmd *self, struct cmd_ctx *ctx)
73: {
74: struct args *args = self->args;
75: struct winlink *wl, *wm;
76: struct session *s, *s2;
77: struct window_choose_data *wcd = NULL;
78: const char *ses_template, *win_template;
1.8 nicm 79: char *final_win_action, *cur_win_template;
80: char *final_win_template_middle;
81: char *final_win_template_last;
1.1 nicm 82: const char *ses_action, *win_action;
1.8 nicm 83: u_int cur_win, idx_ses, win_ses, win_max;
1.1 nicm 84: u_int wflag, sflag;
85:
86: ses_template = win_template = NULL;
87: ses_action = win_action = NULL;
88:
89: if (ctx->curclient == NULL) {
90: ctx->error(ctx, "must be run interactively");
1.4 nicm 91: return (CMD_RETURN_ERROR);
1.1 nicm 92: }
93:
94: s = ctx->curclient->session;
95:
96: if ((wl = cmd_find_window(ctx, args_get(args, 't'), NULL)) == NULL)
1.4 nicm 97: return (CMD_RETURN_ERROR);
1.1 nicm 98:
99: if (window_pane_set_mode(wl->window->active, &window_choose_mode) != 0)
1.4 nicm 100: return (CMD_RETURN_NORMAL);
1.1 nicm 101:
102: /* Sort out which command this is. */
103: wflag = sflag = 0;
104: if (self->entry == &cmd_choose_session_entry) {
105: sflag = 1;
106: if ((ses_template = args_get(args, 'F')) == NULL)
1.7 nicm 107: ses_template = CHOOSE_TREE_SESSION_TEMPLATE;
1.1 nicm 108:
109: if (args->argc != 0)
110: ses_action = args->argv[0];
111: else
112: ses_action = CMD_CHOOSE_TREE_SESSION_ACTION;
113: } else if (self->entry == &cmd_choose_window_entry) {
114: wflag = 1;
115: if ((win_template = args_get(args, 'F')) == NULL)
1.7 nicm 116: win_template = CHOOSE_TREE_WINDOW_TEMPLATE;
1.1 nicm 117:
118: if (args->argc != 0)
119: win_action = args->argv[0];
120: else
121: win_action = CMD_CHOOSE_TREE_WINDOW_ACTION;
122: } else {
123: wflag = args_has(args, 'w');
124: sflag = args_has(args, 's');
125:
126: if ((ses_action = args_get(args, 'b')) == NULL)
127: ses_action = CMD_CHOOSE_TREE_SESSION_ACTION;
128:
129: if ((win_action = args_get(args, 'c')) == NULL)
130: win_action = CMD_CHOOSE_TREE_WINDOW_ACTION;
131:
132: if ((ses_template = args_get(args, 'S')) == NULL)
1.7 nicm 133: ses_template = CHOOSE_TREE_SESSION_TEMPLATE;
1.1 nicm 134:
135: if ((win_template = args_get(args, 'W')) == NULL)
1.7 nicm 136: win_template = CHOOSE_TREE_WINDOW_TEMPLATE;
1.1 nicm 137: }
138:
139: /*
140: * If not asking for windows and sessions, assume no "-ws" given and
141: * hence display the entire tree outright.
142: */
143: if (!wflag && !sflag)
144: wflag = sflag = 1;
145:
146: /*
147: * If we're drawing in tree mode, including sessions, then pad the
148: * window template, otherwise just render the windows as a flat list
149: * without any padding.
150: */
1.8 nicm 151: if (wflag && sflag) {
1.11 nicm 152: xasprintf(&final_win_template_middle,
153: " \001tq\001> %s", win_template);
154: xasprintf(&final_win_template_last,
155: " \001mq\001> %s", win_template);
1.8 nicm 156: } else if (wflag) {
157: final_win_template_middle = xstrdup(win_template);
158: final_win_template_last = xstrdup(win_template);
159: } else
160: final_win_template_middle = final_win_template_last = NULL;
1.1 nicm 161:
162: idx_ses = cur_win = -1;
163: RB_FOREACH(s2, sessions, &sessions) {
164: idx_ses++;
165:
166: /*
167: * If we're just choosing windows, jump straight there. Note
168: * that this implies the current session, so only choose
169: * windows when the session matches this one.
170: */
171: if (wflag && !sflag) {
172: if (s != s2)
173: continue;
174: goto windows_only;
175: }
176:
177: wcd = window_choose_add_session(wl->window->active,
1.8 nicm 178: ctx, s2, ses_template, (char *)ses_action, idx_ses);
1.1 nicm 179:
180: /* If we're just choosing sessions, skip choosing windows. */
181: if (sflag && !wflag) {
182: if (s == s2)
183: cur_win = idx_ses;
184: continue;
185: }
186: windows_only:
1.8 nicm 187: win_ses = win_max = -1;
188: RB_FOREACH(wm, winlinks, &s2->windows)
189: win_max++;
1.1 nicm 190: RB_FOREACH(wm, winlinks, &s2->windows) {
191: win_ses++;
192: if (sflag && wflag)
193: idx_ses++;
194:
195: if (wm == s2->curw && s == s2) {
196: if (wflag && !sflag) {
197: /*
198: * Then we're only counting windows.
199: * So remember which is the current
200: * window in the list.
201: */
202: cur_win = win_ses;
203: } else
204: cur_win = idx_ses;
205: }
206:
207: xasprintf(&final_win_action, "%s ; %s", win_action,
1.8 nicm 208: wcd ? wcd->command : "");
209:
210: if (win_ses != win_max)
211: cur_win_template = final_win_template_middle;
212: else
213: cur_win_template = final_win_template_last;
1.1 nicm 214:
215: window_choose_add_window(wl->window->active,
1.8 nicm 216: ctx, s2, wm, cur_win_template,
1.10 nicm 217: final_win_action,
218: (wflag && !sflag) ? win_ses : idx_ses);
1.1 nicm 219:
1.3 nicm 220: free(final_win_action);
1.1 nicm 221: }
222: /*
223: * If we're just drawing windows, don't consider moving on to
224: * other sessions as we only list windows in this session.
225: */
226: if (wflag && !sflag)
227: break;
228: }
1.8 nicm 229: free(final_win_template_middle);
230: free(final_win_template_last);
1.1 nicm 231:
232: window_choose_ready(wl->window->active, cur_win,
233: cmd_choose_tree_callback, cmd_choose_tree_free);
1.12 ! nicm 234:
! 235: if (args_has(args, 'u'))
! 236: window_choose_expand_all(wl->window->active);
1.1 nicm 237:
1.4 nicm 238: return (CMD_RETURN_NORMAL);
1.1 nicm 239: }
240:
241: void
242: cmd_choose_tree_callback(struct window_choose_data *cdata)
243: {
244: if (cdata == NULL)
245: return;
246:
247: if (cdata->client->flags & CLIENT_DEAD)
248: return;
249:
250: window_choose_ctx(cdata);
251: }
252:
253: void
254: cmd_choose_tree_free(struct window_choose_data *cdata)
255: {
256: cdata->session->references--;
257: cdata->client->references--;
258:
1.3 nicm 259: free(cdata->ft_template);
260: free(cdata->command);
1.1 nicm 261: format_free(cdata->ft);
1.3 nicm 262: free(cdata);
1.1 nicm 263:
264: }