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