xref: /openbsd-src/usr.bin/tmux/cmd-select-pane.c (revision c90a81c56dcebd6a1b73fe4aff9b03385b8e63b3)
1 /* $OpenBSD: cmd-select-pane.c,v 1.45 2018/10/18 08:38:01 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 
23 #include "tmux.h"
24 
25 /*
26  * Select pane.
27  */
28 
29 static enum cmd_retval	cmd_select_pane_exec(struct cmd *, struct cmdq_item *);
30 
31 const struct cmd_entry cmd_select_pane_entry = {
32 	.name = "select-pane",
33 	.alias = "selectp",
34 
35 	.args = { "DdegLlMmP:RT:t:U", 0, 0 },
36 	.usage = "[-DdegLlMmRU] [-P style] [-T title] " CMD_TARGET_PANE_USAGE,
37 
38 	.target = { 't', CMD_FIND_PANE, 0 },
39 
40 	.flags = 0,
41 	.exec = cmd_select_pane_exec
42 };
43 
44 const struct cmd_entry cmd_last_pane_entry = {
45 	.name = "last-pane",
46 	.alias = "lastp",
47 
48 	.args = { "det:", 0, 0 },
49 	.usage = "[-de] " CMD_TARGET_WINDOW_USAGE,
50 
51 	.target = { 't', CMD_FIND_WINDOW, 0 },
52 
53 	.flags = 0,
54 	.exec = cmd_select_pane_exec
55 };
56 
57 static void
58 cmd_select_pane_redraw(struct window *w)
59 {
60 	struct client	*c;
61 
62 	/*
63 	 * Redraw entire window if it is bigger than the client (the
64 	 * offset may change), otherwise just draw borders.
65 	 */
66 
67 	TAILQ_FOREACH(c, &clients, entry) {
68 		if (c->session == NULL)
69 			continue;
70 		if (c->session->curw->window == w && tty_window_bigger(&c->tty))
71 			server_redraw_client(c);
72 		else {
73 			if (c->session->curw->window == w)
74 				c->flags |= CLIENT_REDRAWBORDERS;
75 			if (session_has(c->session, w))
76 				c->flags |= CLIENT_REDRAWSTATUS;
77 		}
78 
79 	}
80 }
81 
82 static enum cmd_retval
83 cmd_select_pane_exec(struct cmd *self, struct cmdq_item *item)
84 {
85 	struct args		*args = self->args;
86 	struct cmd_find_state	*current = &item->shared->current;
87 	struct client		*c = cmd_find_client(item, NULL, 1);
88 	struct winlink		*wl = item->target.wl;
89 	struct window		*w = wl->window;
90 	struct session		*s = item->target.s;
91 	struct window_pane	*wp = item->target.wp, *lastwp, *markedwp;
92 	char			*pane_title;
93 	const char		*style;
94 
95 	if (self->entry == &cmd_last_pane_entry || args_has(args, 'l')) {
96 		lastwp = w->last;
97 		if (lastwp == NULL && window_count_panes(w) == 2) {
98 			lastwp = TAILQ_PREV(w->active, window_panes, entry);
99 			if (lastwp == NULL)
100 				lastwp = TAILQ_NEXT(w->active, entry);
101 		}
102 		if (lastwp == NULL) {
103 			cmdq_error(item, "no last pane");
104 			return (CMD_RETURN_ERROR);
105 		}
106 		if (args_has(self->args, 'e'))
107 			lastwp->flags &= ~PANE_INPUTOFF;
108 		else if (args_has(self->args, 'd'))
109 			lastwp->flags |= PANE_INPUTOFF;
110 		else {
111 			server_unzoom_window(w);
112 			window_redraw_active_switch(w, lastwp);
113 			if (window_set_active_pane(w, lastwp)) {
114 				cmd_find_from_winlink(current, wl, 0);
115 				cmd_select_pane_redraw(w);
116 			}
117 		}
118 		return (CMD_RETURN_NORMAL);
119 	}
120 
121 	if (args_has(args, 'm') || args_has(args, 'M')) {
122 		if (args_has(args, 'm') && !window_pane_visible(wp))
123 			return (CMD_RETURN_NORMAL);
124 		lastwp = marked_pane.wp;
125 
126 		if (args_has(args, 'M') || server_is_marked(s, wl, wp))
127 			server_clear_marked();
128 		else
129 			server_set_marked(s, wl, wp);
130 		markedwp = marked_pane.wp;
131 
132 		if (lastwp != NULL) {
133 			server_redraw_window_borders(lastwp->window);
134 			server_status_window(lastwp->window);
135 		}
136 		if (markedwp != NULL) {
137 			server_redraw_window_borders(markedwp->window);
138 			server_status_window(markedwp->window);
139 		}
140 		return (CMD_RETURN_NORMAL);
141 	}
142 
143 	if (args_has(self->args, 'P') || args_has(self->args, 'g')) {
144 		if (args_has(args, 'P')) {
145 			style = args_get(args, 'P');
146 			if (style_parse(&grid_default_cell, &wp->colgc,
147 			    style) == -1) {
148 				cmdq_error(item, "bad style: %s", style);
149 				return (CMD_RETURN_ERROR);
150 			}
151 			wp->flags |= PANE_REDRAW;
152 		}
153 		if (args_has(self->args, 'g'))
154 			cmdq_print(item, "%s", style_tostring(&wp->colgc));
155 		return (CMD_RETURN_NORMAL);
156 	}
157 
158 	if (args_has(self->args, 'L')) {
159 		server_unzoom_window(wp->window);
160 		wp = window_pane_find_left(wp);
161 	} else if (args_has(self->args, 'R')) {
162 		server_unzoom_window(wp->window);
163 		wp = window_pane_find_right(wp);
164 	} else if (args_has(self->args, 'U')) {
165 		server_unzoom_window(wp->window);
166 		wp = window_pane_find_up(wp);
167 	} else if (args_has(self->args, 'D')) {
168 		server_unzoom_window(wp->window);
169 		wp = window_pane_find_down(wp);
170 	}
171 	if (wp == NULL)
172 		return (CMD_RETURN_NORMAL);
173 
174 	if (args_has(self->args, 'e')) {
175 		wp->flags &= ~PANE_INPUTOFF;
176 		return (CMD_RETURN_NORMAL);
177 	}
178 	if (args_has(self->args, 'd')) {
179 		wp->flags |= PANE_INPUTOFF;
180 		return (CMD_RETURN_NORMAL);
181 	}
182 
183 	if (args_has(self->args, 'T')) {
184 		pane_title = format_single(item, args_get(self->args, 'T'),
185 		    c, s, wl, wp);
186 		screen_set_title(&wp->base, pane_title);
187 		server_status_window(wp->window);
188 		free(pane_title);
189 		return (CMD_RETURN_NORMAL);
190 	}
191 
192 	if (wp == w->active)
193 		return (CMD_RETURN_NORMAL);
194 	server_unzoom_window(wp->window);
195 	window_redraw_active_switch(w, wp);
196 	if (window_set_active_pane(w, wp)) {
197 		cmd_find_from_winlink_pane(current, wl, wp, 0);
198 		hooks_insert(s->hooks, item, current, "after-select-pane");
199 		cmd_select_pane_redraw(w);
200 	}
201 
202 	return (CMD_RETURN_NORMAL);
203 }
204