xref: /openbsd-src/usr.bin/tmux/cmd-select-pane.c (revision 824adb5411e4389b29bae28eba5c2c2bbd147f34)
1 /* $OpenBSD: cmd-select-pane.c,v 1.68 2021/08/21 10:22:39 nicm Exp $ */
2 
3 /*
4  * Copyright (c) 2009 Nicholas Marriott <nicholas.marriott@gmail.com>
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  * Select pane.
28  */
29 
30 static enum cmd_retval	cmd_select_pane_exec(struct cmd *, struct cmdq_item *);
31 
32 const struct cmd_entry cmd_select_pane_entry = {
33 	.name = "select-pane",
34 	.alias = "selectp",
35 
36 	.args = { "DdegLlMmP:RT:t:UZ", 0, 0, NULL }, /* -P and -g deprecated */
37 	.usage = "[-DdeLlMmRUZ] [-T title] " CMD_TARGET_PANE_USAGE,
38 
39 	.target = { 't', CMD_FIND_PANE, 0 },
40 
41 	.flags = 0,
42 	.exec = cmd_select_pane_exec
43 };
44 
45 const struct cmd_entry cmd_last_pane_entry = {
46 	.name = "last-pane",
47 	.alias = "lastp",
48 
49 	.args = { "det:Z", 0, 0, NULL },
50 	.usage = "[-deZ] " CMD_TARGET_WINDOW_USAGE,
51 
52 	.target = { 't', CMD_FIND_WINDOW, 0 },
53 
54 	.flags = 0,
55 	.exec = cmd_select_pane_exec
56 };
57 
58 static void
59 cmd_select_pane_redraw(struct window *w)
60 {
61 	struct client	*c;
62 
63 	/*
64 	 * Redraw entire window if it is bigger than the client (the
65 	 * offset may change), otherwise just draw borders.
66 	 */
67 
68 	TAILQ_FOREACH(c, &clients, entry) {
69 		if (c->session == NULL || (c->flags & CLIENT_CONTROL))
70 			continue;
71 		if (c->session->curw->window == w && tty_window_bigger(&c->tty))
72 			server_redraw_client(c);
73 		else {
74 			if (c->session->curw->window == w)
75 				c->flags |= CLIENT_REDRAWBORDERS;
76 			if (session_has(c->session, w))
77 				c->flags |= CLIENT_REDRAWSTATUS;
78 		}
79 
80 	}
81 }
82 
83 static enum cmd_retval
84 cmd_select_pane_exec(struct cmd *self, struct cmdq_item *item)
85 {
86 	struct args		*args = cmd_get_args(self);
87 	const struct cmd_entry	*entry = cmd_get_entry(self);
88 	struct cmd_find_state	*current = cmdq_get_current(item);
89 	struct cmd_find_state	*target = cmdq_get_target(item);
90 	struct client		*c = cmdq_get_client(item);
91 	struct winlink		*wl = target->wl;
92 	struct window		*w = wl->window;
93 	struct session		*s = target->s;
94 	struct window_pane	*wp = target->wp, *activewp, *lastwp, *markedwp;
95 	struct options		*oo = wp->options;
96 	char			*title;
97 	const char		*style;
98 	struct options_entry	*o;
99 
100 	if (entry == &cmd_last_pane_entry || args_has(args, 'l')) {
101 		lastwp = w->last;
102 		if (lastwp == NULL && window_count_panes(w) == 2) {
103 			lastwp = TAILQ_PREV(w->active, window_panes, entry);
104 			if (lastwp == NULL)
105 				lastwp = TAILQ_NEXT(w->active, entry);
106 		}
107 		if (lastwp == NULL) {
108 			cmdq_error(item, "no last pane");
109 			return (CMD_RETURN_ERROR);
110 		}
111 		if (args_has(args, 'e')) {
112 			lastwp->flags &= ~PANE_INPUTOFF;
113 			server_redraw_window_borders(lastwp->window);
114 			server_status_window(lastwp->window);
115 		} else if (args_has(args, 'd')) {
116 			lastwp->flags |= PANE_INPUTOFF;
117 			server_redraw_window_borders(lastwp->window);
118 			server_status_window(lastwp->window);
119 		} else {
120 			if (window_push_zoom(w, 0, args_has(args, 'Z')))
121 				server_redraw_window(w);
122 			window_redraw_active_switch(w, lastwp);
123 			if (window_set_active_pane(w, lastwp, 1)) {
124 				cmd_find_from_winlink(current, wl, 0);
125 				cmd_select_pane_redraw(w);
126 			}
127 			if (window_pop_zoom(w))
128 				server_redraw_window(w);
129 		}
130 		return (CMD_RETURN_NORMAL);
131 	}
132 
133 	if (args_has(args, 'm') || args_has(args, 'M')) {
134 		if (args_has(args, 'm') && !window_pane_visible(wp))
135 			return (CMD_RETURN_NORMAL);
136 		if (server_check_marked())
137 			lastwp = marked_pane.wp;
138 		else
139 			lastwp = NULL;
140 
141 		if (args_has(args, 'M') || server_is_marked(s, wl, wp))
142 			server_clear_marked();
143 		else
144 			server_set_marked(s, wl, wp);
145 		markedwp = marked_pane.wp;
146 
147 		if (lastwp != NULL) {
148 			lastwp->flags |= (PANE_REDRAW|PANE_STYLECHANGED);
149 			server_redraw_window_borders(lastwp->window);
150 			server_status_window(lastwp->window);
151 		}
152 		if (markedwp != NULL) {
153 			markedwp->flags |= (PANE_REDRAW|PANE_STYLECHANGED);
154 			server_redraw_window_borders(markedwp->window);
155 			server_status_window(markedwp->window);
156 		}
157 		return (CMD_RETURN_NORMAL);
158 	}
159 
160 	style = args_get(args, 'P');
161 	if (style != NULL) {
162 		o = options_set_string(oo, "window-style", 0, "%s", style);
163 		if (o == NULL) {
164 			cmdq_error(item, "bad style: %s", style);
165 			return (CMD_RETURN_ERROR);
166 		}
167 		options_set_string(oo, "window-active-style", 0, "%s", style);
168 		wp->flags |= (PANE_REDRAW|PANE_STYLECHANGED);
169 	}
170 	if (args_has(args, 'g')) {
171 		cmdq_print(item, "%s", options_get_string(oo, "window-style"));
172 		return (CMD_RETURN_NORMAL);
173 	}
174 
175 	if (args_has(args, 'L')) {
176 		window_push_zoom(w, 0, 1);
177 		wp = window_pane_find_left(wp);
178 		window_pop_zoom(w);
179 	} else if (args_has(args, 'R')) {
180 		window_push_zoom(w, 0, 1);
181 		wp = window_pane_find_right(wp);
182 		window_pop_zoom(w);
183 	} else if (args_has(args, 'U')) {
184 		window_push_zoom(w, 0, 1);
185 		wp = window_pane_find_up(wp);
186 		window_pop_zoom(w);
187 	} else if (args_has(args, 'D')) {
188 		window_push_zoom(w, 0, 1);
189 		wp = window_pane_find_down(wp);
190 		window_pop_zoom(w);
191 	}
192 	if (wp == NULL)
193 		return (CMD_RETURN_NORMAL);
194 
195 	if (args_has(args, 'e')) {
196 		wp->flags &= ~PANE_INPUTOFF;
197 		server_redraw_window_borders(wp->window);
198 		server_status_window(wp->window);
199 		return (CMD_RETURN_NORMAL);
200 	}
201 	if (args_has(args, 'd')) {
202 		wp->flags |= PANE_INPUTOFF;
203 		server_redraw_window_borders(wp->window);
204 		server_status_window(wp->window);
205 		return (CMD_RETURN_NORMAL);
206 	}
207 
208 	if (args_has(args, 'T')) {
209 		title = format_single_from_target(item, args_get(args, 'T'));
210 		if (screen_set_title(&wp->base, title)) {
211 			notify_pane("pane-title-changed", wp);
212 			server_redraw_window_borders(wp->window);
213 			server_status_window(wp->window);
214 		}
215 		free(title);
216 		return (CMD_RETURN_NORMAL);
217 	}
218 
219 	if (c != NULL && c->session != NULL && (c->flags & CLIENT_ACTIVEPANE))
220 		activewp = server_client_get_pane(c);
221 	else
222 		activewp = w->active;
223 	if (wp == activewp)
224 		return (CMD_RETURN_NORMAL);
225 	if (window_push_zoom(w, 0, args_has(args, 'Z')))
226 		server_redraw_window(w);
227 	window_redraw_active_switch(w, wp);
228 	if (c != NULL && c->session != NULL && (c->flags & CLIENT_ACTIVEPANE))
229 		server_client_set_pane(c, wp);
230 	else if (window_set_active_pane(w, wp, 1))
231 		cmd_find_from_winlink_pane(current, wl, wp, 0);
232 	cmdq_insert_hook(s, item, current, "after-select-pane");
233 	cmd_select_pane_redraw(w);
234 	if (window_pop_zoom(w))
235 		server_redraw_window(w);
236 
237 	return (CMD_RETURN_NORMAL);
238 }
239