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