xref: /openbsd-src/usr.bin/tmux/cmd-select-pane.c (revision 7350f337b9e3eb4461d99580e625c7ef148d107c)
1 /* $OpenBSD: cmd-select-pane.c,v 1.51 2019/06/20 11:59:59 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:U", 0, 0 }, /* -P and -g deprecated */
37 	.usage = "[-DdeLlMmRU] [-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:", 0, 0 },
50 	.usage = "[-de] " 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 = self->args;
87 	struct cmd_find_state	*current = &item->shared->current;
88 	struct client		*c = cmd_find_client(item, NULL, 1);
89 	struct winlink		*wl = item->target.wl;
90 	struct window		*w = wl->window;
91 	struct session		*s = item->target.s;
92 	struct window_pane	*wp = item->target.wp, *lastwp, *markedwp;
93 	char			*pane_title;
94 	const char		*style;
95 	struct style		*sy;
96 	struct options_entry	*o;
97 
98 	if (self->entry == &cmd_last_pane_entry || args_has(args, 'l')) {
99 		lastwp = w->last;
100 		if (lastwp == NULL && window_count_panes(w) == 2) {
101 			lastwp = TAILQ_PREV(w->active, window_panes, entry);
102 			if (lastwp == NULL)
103 				lastwp = TAILQ_NEXT(w->active, entry);
104 		}
105 		if (lastwp == NULL) {
106 			cmdq_error(item, "no last pane");
107 			return (CMD_RETURN_ERROR);
108 		}
109 		if (args_has(self->args, 'e'))
110 			lastwp->flags &= ~PANE_INPUTOFF;
111 		else if (args_has(self->args, 'd'))
112 			lastwp->flags |= PANE_INPUTOFF;
113 		else {
114 			server_unzoom_window(w);
115 			window_redraw_active_switch(w, lastwp);
116 			if (window_set_active_pane(w, lastwp, 1)) {
117 				cmd_find_from_winlink(current, wl, 0);
118 				cmd_select_pane_redraw(w);
119 			}
120 		}
121 		return (CMD_RETURN_NORMAL);
122 	}
123 
124 	if (args_has(args, 'm') || args_has(args, 'M')) {
125 		if (args_has(args, 'm') && !window_pane_visible(wp))
126 			return (CMD_RETURN_NORMAL);
127 		lastwp = marked_pane.wp;
128 
129 		if (args_has(args, 'M') || server_is_marked(s, wl, wp))
130 			server_clear_marked();
131 		else
132 			server_set_marked(s, wl, wp);
133 		markedwp = marked_pane.wp;
134 
135 		if (lastwp != NULL) {
136 			server_redraw_window_borders(lastwp->window);
137 			server_status_window(lastwp->window);
138 		}
139 		if (markedwp != NULL) {
140 			server_redraw_window_borders(markedwp->window);
141 			server_status_window(markedwp->window);
142 		}
143 		return (CMD_RETURN_NORMAL);
144 	}
145 
146 	if (args_has(self->args, 'P') || args_has(self->args, 'g')) {
147 		if ((style = args_get(args, 'P')) != NULL) {
148 			o = options_set_style(wp->options, "window-style", 0,
149 			    style);
150 			if (o == NULL) {
151 				cmdq_error(item, "bad style: %s", style);
152 				return (CMD_RETURN_ERROR);
153 			}
154 			wp->flags |= (PANE_REDRAW|PANE_STYLECHANGED);
155 		}
156 		if (args_has(self->args, 'g')) {
157 			sy = options_get_style(wp->options, "window-style");
158 			cmdq_print(item, "%s", style_tostring(sy));
159 		}
160 		return (CMD_RETURN_NORMAL);
161 	}
162 
163 	if (args_has(self->args, 'L')) {
164 		server_unzoom_window(wp->window);
165 		wp = window_pane_find_left(wp);
166 	} else if (args_has(self->args, 'R')) {
167 		server_unzoom_window(wp->window);
168 		wp = window_pane_find_right(wp);
169 	} else if (args_has(self->args, 'U')) {
170 		server_unzoom_window(wp->window);
171 		wp = window_pane_find_up(wp);
172 	} else if (args_has(self->args, 'D')) {
173 		server_unzoom_window(wp->window);
174 		wp = window_pane_find_down(wp);
175 	}
176 	if (wp == NULL)
177 		return (CMD_RETURN_NORMAL);
178 
179 	if (args_has(self->args, 'e')) {
180 		wp->flags &= ~PANE_INPUTOFF;
181 		return (CMD_RETURN_NORMAL);
182 	}
183 	if (args_has(self->args, 'd')) {
184 		wp->flags |= PANE_INPUTOFF;
185 		return (CMD_RETURN_NORMAL);
186 	}
187 
188 	if (args_has(self->args, 'T')) {
189 		pane_title = format_single(item, args_get(self->args, 'T'),
190 		    c, s, wl, wp);
191 		screen_set_title(&wp->base, pane_title);
192 		server_status_window(wp->window);
193 		free(pane_title);
194 		return (CMD_RETURN_NORMAL);
195 	}
196 
197 	if (wp == w->active)
198 		return (CMD_RETURN_NORMAL);
199 	server_unzoom_window(wp->window);
200 	window_redraw_active_switch(w, wp);
201 	if (window_set_active_pane(w, wp, 1)) {
202 		cmd_find_from_winlink_pane(current, wl, wp, 0);
203 		cmdq_insert_hook(s, item, current, "after-select-pane");
204 		cmd_select_pane_redraw(w);
205 	}
206 
207 	return (CMD_RETURN_NORMAL);
208 }
209