Annotation of src/usr.bin/tmux/cmd-choose-tree.c, Revision 1.8
1.8 ! nicm 1: /* $OpenBSD: cmd-choose-tree.c,v 1.7 2012/08/14 08:51:53 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,
42: "S:W:swb:c:t:", 0, 1,
1.2 nicm 43: "[-sw] [-b session-template] [-c window template] [-S format] " \
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;
1.8 ! nicm 77: struct tty *tty;
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:
90: if (ctx->curclient == NULL) {
91: ctx->error(ctx, "must be run interactively");
1.4 nicm 92: return (CMD_RETURN_ERROR);
1.1 nicm 93: }
94:
95: s = ctx->curclient->session;
1.8 ! nicm 96: tty = &ctx->curclient->tty;
1.1 nicm 97:
98: if ((wl = cmd_find_window(ctx, args_get(args, 't'), NULL)) == NULL)
1.4 nicm 99: return (CMD_RETURN_ERROR);
1.1 nicm 100:
101: if (window_pane_set_mode(wl->window->active, &window_choose_mode) != 0)
1.4 nicm 102: return (CMD_RETURN_NORMAL);
1.1 nicm 103:
104: /* Sort out which command this is. */
105: wflag = sflag = 0;
106: if (self->entry == &cmd_choose_session_entry) {
107: sflag = 1;
108: if ((ses_template = args_get(args, 'F')) == NULL)
1.7 nicm 109: ses_template = CHOOSE_TREE_SESSION_TEMPLATE;
1.1 nicm 110:
111: if (args->argc != 0)
112: ses_action = args->argv[0];
113: else
114: ses_action = CMD_CHOOSE_TREE_SESSION_ACTION;
115: } else if (self->entry == &cmd_choose_window_entry) {
116: wflag = 1;
117: if ((win_template = args_get(args, 'F')) == NULL)
1.7 nicm 118: win_template = CHOOSE_TREE_WINDOW_TEMPLATE;
1.1 nicm 119:
120: if (args->argc != 0)
121: win_action = args->argv[0];
122: else
123: win_action = CMD_CHOOSE_TREE_WINDOW_ACTION;
124: } else {
125: wflag = args_has(args, 'w');
126: sflag = args_has(args, 's');
127:
128: if ((ses_action = args_get(args, 'b')) == NULL)
129: ses_action = CMD_CHOOSE_TREE_SESSION_ACTION;
130:
131: if ((win_action = args_get(args, 'c')) == NULL)
132: win_action = CMD_CHOOSE_TREE_WINDOW_ACTION;
133:
134: if ((ses_template = args_get(args, 'S')) == NULL)
1.7 nicm 135: ses_template = CHOOSE_TREE_SESSION_TEMPLATE;
1.1 nicm 136:
137: if ((win_template = args_get(args, 'W')) == NULL)
1.7 nicm 138: win_template = CHOOSE_TREE_WINDOW_TEMPLATE;
1.1 nicm 139: }
140:
141: /*
142: * If not asking for windows and sessions, assume no "-ws" given and
143: * hence display the entire tree outright.
144: */
145: if (!wflag && !sflag)
146: wflag = sflag = 1;
147:
148: /*
149: * If we're drawing in tree mode, including sessions, then pad the
150: * window template, otherwise just render the windows as a flat list
151: * without any padding.
152: */
1.8 ! nicm 153: if (wflag && sflag) {
! 154: xasprintf(&final_win_template_middle, " |-> %s", win_template);
! 155: xasprintf(&final_win_template_last, " \\-> %s", win_template);
! 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,
! 217: final_win_action, idx_ses);
1.1 nicm 218:
1.3 nicm 219: free(final_win_action);
1.1 nicm 220: }
221: /*
222: * If we're just drawing windows, don't consider moving on to
223: * other sessions as we only list windows in this session.
224: */
225: if (wflag && !sflag)
226: break;
227: }
1.8 ! nicm 228: free(final_win_template_middle);
! 229: free(final_win_template_last);
1.1 nicm 230:
231: window_choose_ready(wl->window->active, cur_win,
232: cmd_choose_tree_callback, cmd_choose_tree_free);
233:
1.4 nicm 234: return (CMD_RETURN_NORMAL);
1.1 nicm 235: }
236:
237: void
238: cmd_choose_tree_callback(struct window_choose_data *cdata)
239: {
240: if (cdata == NULL)
241: return;
242:
243: if (cdata->client->flags & CLIENT_DEAD)
244: return;
245:
246: window_choose_ctx(cdata);
247: }
248:
249: void
250: cmd_choose_tree_free(struct window_choose_data *cdata)
251: {
252: cdata->session->references--;
253: cdata->client->references--;
254:
1.3 nicm 255: free(cdata->ft_template);
256: free(cdata->command);
1.1 nicm 257: format_free(cdata->ft);
1.3 nicm 258: free(cdata);
1.1 nicm 259:
260: }