/src/tmux/cmd-choose-tree.c
Line | Count | Source |
1 | | /* $OpenBSD: cmd-choose-tree.c,v 1.61 2026/10/15 08:07:17 nicm Exp $ */ |
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 <stdlib.h> |
22 | | #include <string.h> |
23 | | |
24 | | #include "tmux.h" |
25 | | |
26 | | /* |
27 | | * Enter a mode. |
28 | | */ |
29 | | |
30 | | static enum args_parse_type cmd_choose_tree_args_parse(struct args *args, |
31 | | u_int idx, char **cause); |
32 | | static enum cmd_retval cmd_choose_tree_exec(struct cmd *, |
33 | | struct cmdq_item *); |
34 | | static void cmd_choose_tree_read_done(struct client *, |
35 | | const char *, int, int, struct evbuffer *, |
36 | | void *); |
37 | | |
38 | | const struct cmd_entry cmd_choose_tree_entry = { |
39 | | .name = "choose-tree", |
40 | | .description = "Browse sessions and windows interactively.", |
41 | | .alias = NULL, |
42 | | |
43 | | .args = { "F:f:GhK:kNO:rst:wyZ", 0, 1, cmd_choose_tree_args_parse }, |
44 | | .usage = "[-GhkNrswZ] [-F format] [-f filter] [-K key-format] " |
45 | | "[-O sort-order] " CMD_TARGET_PANE_USAGE " [template]", |
46 | | |
47 | | .target = { 't', CMD_FIND_PANE, 0 }, |
48 | | |
49 | | .flags = 0, |
50 | | .exec = cmd_choose_tree_exec |
51 | | }; |
52 | | |
53 | | const struct cmd_entry cmd_choose_client_entry = { |
54 | | .name = "choose-client", |
55 | | .description = "Choose a client interactively.", |
56 | | .alias = NULL, |
57 | | |
58 | | .args = { "F:f:hiK:kNO:rt:yZ", 0, 1, cmd_choose_tree_args_parse }, |
59 | | .usage = "[-hikNrZ] [-F format] [-f filter] [-K key-format] " |
60 | | "[-O sort-order] " CMD_TARGET_PANE_USAGE " [template]", |
61 | | |
62 | | .target = { 't', CMD_FIND_PANE, 0 }, |
63 | | |
64 | | .flags = 0, |
65 | | .exec = cmd_choose_tree_exec |
66 | | }; |
67 | | |
68 | | const struct cmd_entry cmd_choose_buffer_entry = { |
69 | | .name = "choose-buffer", |
70 | | .description = "Choose a paste buffer interactively.", |
71 | | .alias = NULL, |
72 | | |
73 | | .args = { "F:f:K:kNO:rt:yZ", 0, 1, cmd_choose_tree_args_parse }, |
74 | | .usage = "[-kNrZ] [-F format] [-f filter] [-K key-format] " |
75 | | "[-O sort-order] " CMD_TARGET_PANE_USAGE " [template]", |
76 | | |
77 | | .target = { 't', CMD_FIND_PANE, 0 }, |
78 | | |
79 | | .flags = 0, |
80 | | .exec = cmd_choose_tree_exec |
81 | | }; |
82 | | |
83 | | const struct cmd_entry cmd_customize_mode_entry = { |
84 | | .name = "customize-mode", |
85 | | .description = "Browse and edit options and key bindings.", |
86 | | .alias = NULL, |
87 | | |
88 | | .args = { "F:f:kNt:yZ", 0, 0, NULL }, |
89 | | .usage = "[-kNZ] [-F format] [-f filter] " CMD_TARGET_PANE_USAGE, |
90 | | |
91 | | .target = { 't', CMD_FIND_PANE, 0 }, |
92 | | |
93 | | .flags = 0, |
94 | | .exec = cmd_choose_tree_exec |
95 | | }; |
96 | | |
97 | | const struct cmd_entry cmd_switch_mode_entry = { |
98 | | .name = "switch-mode", |
99 | | .description = "Switch the active pane mode.", |
100 | | .alias = NULL, |
101 | | |
102 | | .args = { "F:f:kO:rst:wZ", 0, 1, cmd_choose_tree_args_parse }, |
103 | | .usage = "[-krsZw] [-F format] [-f path] [-O sort-order] " |
104 | | CMD_TARGET_PANE_USAGE " [command]", |
105 | | |
106 | | .target = { 't', CMD_FIND_PANE, 0 }, |
107 | | |
108 | | .flags = 0, |
109 | | .exec = cmd_choose_tree_exec |
110 | | }; |
111 | | |
112 | | const struct cmd_entry cmd_display_panes_entry = { |
113 | | .name = "display-panes", |
114 | | .description = "Display pane numbers.", |
115 | | .alias = "displayp", |
116 | | |
117 | | .args = { "d:kNs:t:Z", 0, 1, cmd_choose_tree_args_parse }, |
118 | | .usage = "[-kNZ] [-d duration] [-s source-window] " |
119 | | CMD_TARGET_PANE_USAGE " [template]", |
120 | | |
121 | | .source = { 's', CMD_FIND_WINDOW, 0 }, |
122 | | .target = { 't', CMD_FIND_PANE, 0 }, |
123 | | |
124 | | .flags = CMD_AFTERHOOK, |
125 | | .exec = cmd_choose_tree_exec |
126 | | }; |
127 | | |
128 | | static enum args_parse_type |
129 | | cmd_choose_tree_args_parse(__unused struct args *args, __unused u_int idx, |
130 | | __unused char **cause) |
131 | 228 | { |
132 | 228 | return (ARGS_PARSE_COMMANDS_OR_STRING); |
133 | 228 | } |
134 | | |
135 | | static enum cmd_retval |
136 | | cmd_choose_tree_exec(struct cmd *self, struct cmdq_item *item) |
137 | 0 | { |
138 | 0 | struct args *args = cmd_get_args(self); |
139 | 0 | struct cmd_find_state *target = cmdq_get_target(item); |
140 | 0 | struct client *c = cmdq_get_client(item); |
141 | 0 | struct window_pane *wp = target->wp; |
142 | 0 | const struct window_mode *mode; |
143 | 0 | enum sort_order order; |
144 | 0 | char *path; |
145 | |
|
146 | 0 | order = sort_order_from_string(args_get(args, 'O')); |
147 | 0 | if (order == SORT_END && args_has(args, 'O')) { |
148 | 0 | cmdq_error(item, "invalid sort order"); |
149 | 0 | return (CMD_RETURN_ERROR); |
150 | 0 | } |
151 | | |
152 | 0 | if (cmd_get_entry(self) == &cmd_choose_buffer_entry) { |
153 | 0 | if (paste_is_empty()) |
154 | 0 | return (CMD_RETURN_NORMAL); |
155 | 0 | mode = &window_buffer_mode; |
156 | 0 | } else if (cmd_get_entry(self) == &cmd_choose_client_entry) { |
157 | 0 | if (server_client_how_many() == 0) |
158 | 0 | return (CMD_RETURN_NORMAL); |
159 | 0 | mode = &window_client_mode; |
160 | 0 | } else if (cmd_get_entry(self) == &cmd_customize_mode_entry) |
161 | 0 | mode = &window_customize_mode; |
162 | 0 | else if (cmd_get_entry(self) == &cmd_switch_mode_entry) |
163 | 0 | mode = &window_switch_mode; |
164 | 0 | else if (cmd_get_entry(self) == &cmd_display_panes_entry) |
165 | 0 | mode = &window_panes_mode; |
166 | 0 | else |
167 | 0 | mode = &window_tree_mode; |
168 | | |
169 | 0 | if (mode == &window_switch_mode && args_has(args, 'f')) { |
170 | 0 | path = format_single_from_target(item, args_get(args, 'f')); |
171 | 0 | file_read(c, path, cmd_choose_tree_read_done, item); |
172 | 0 | free(path); |
173 | 0 | return (CMD_RETURN_WAIT); |
174 | 0 | } |
175 | 0 | window_pane_set_mode(wp, NULL, mode, item, target, args); |
176 | 0 | return (CMD_RETURN_NORMAL); |
177 | 0 | } |
178 | | |
179 | | static void |
180 | | cmd_choose_tree_read_done(__unused struct client *c, const char *path, |
181 | | int error, int closed, struct evbuffer *buffer, void *data) |
182 | 0 | { |
183 | 0 | struct cmdq_item *item = data; |
184 | 0 | struct cmd *cmd = cmdq_get_cmd(item); |
185 | 0 | struct args *args = cmd_get_args(cmd); |
186 | 0 | struct cmd_find_state *target = cmdq_get_target(item); |
187 | 0 | struct window_pane *wp = target->wp; |
188 | 0 | struct window_mode_entry *wme; |
189 | 0 | char *buf = EVBUFFER_DATA(buffer); |
190 | 0 | size_t len = EVBUFFER_LENGTH(buffer); |
191 | |
|
192 | 0 | if (!closed) |
193 | 0 | return; |
194 | | |
195 | 0 | if (error != 0) { |
196 | 0 | cmdq_error(item, "%s: %s", strerror(error), path); |
197 | 0 | goto out; |
198 | 0 | } |
199 | 0 | if (len != 0 && memchr(buf, '\0', len) != NULL) { |
200 | 0 | cmdq_error(item, "%s: contains binary data", path); |
201 | 0 | goto out; |
202 | 0 | } |
203 | 0 | if (!cmd_find_valid_state(target)) { |
204 | 0 | cmdq_error(item, "target pane no longer exists"); |
205 | 0 | goto out; |
206 | 0 | } |
207 | | |
208 | 0 | TAILQ_FOREACH(wme, &wp->modes, entry) { |
209 | 0 | if (wme->mode == &window_switch_mode) |
210 | 0 | break; |
211 | 0 | } |
212 | 0 | if (window_pane_set_mode(wp, NULL, &window_switch_mode, item, target, |
213 | 0 | args) == 0 && wme == NULL) |
214 | 0 | window_switch_set_input(TAILQ_FIRST(&wp->modes), buffer); |
215 | |
|
216 | 0 | out: |
217 | 0 | cmdq_continue(item); |
218 | 0 | } |