xref: /openbsd-src/usr.bin/tmux/cmd-select-pane.c (revision ae3cb403620ab940fbaabb3055fac045a63d56b7)
1 /* $OpenBSD: cmd-select-pane.c,v 1.42 2017/11/17 09:52:18 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 enum cmd_retval
58 cmd_select_pane_exec(struct cmd *self, struct cmdq_item *item)
59 {
60 	struct args		*args = self->args;
61 	struct cmd_find_state	*current = &item->shared->current;
62 	struct client		*c = cmd_find_client(item, NULL, 1);
63 	struct winlink		*wl = item->target.wl;
64 	struct window		*w = wl->window;
65 	struct session		*s = item->target.s;
66 	struct window_pane	*wp = item->target.wp, *lastwp, *markedwp;
67 	char			*pane_title;
68 	const char		*style;
69 
70 	if (self->entry == &cmd_last_pane_entry || args_has(args, 'l')) {
71 		lastwp = w->last;
72 		if (lastwp == NULL) {
73 			cmdq_error(item, "no last pane");
74 			return (CMD_RETURN_ERROR);
75 		}
76 		if (args_has(self->args, 'e'))
77 			lastwp->flags &= ~PANE_INPUTOFF;
78 		else if (args_has(self->args, 'd'))
79 			lastwp->flags |= PANE_INPUTOFF;
80 		else {
81 			server_unzoom_window(w);
82 			window_redraw_active_switch(w, lastwp);
83 			if (window_set_active_pane(w, lastwp)) {
84 				cmd_find_from_winlink(current, wl, 0);
85 				server_status_window(w);
86 				server_redraw_window_borders(w);
87 			}
88 		}
89 		return (CMD_RETURN_NORMAL);
90 	}
91 
92 	if (args_has(args, 'm') || args_has(args, 'M')) {
93 		if (args_has(args, 'm') && !window_pane_visible(wp))
94 			return (CMD_RETURN_NORMAL);
95 		lastwp = marked_pane.wp;
96 
97 		if (args_has(args, 'M') || server_is_marked(s, wl, wp))
98 			server_clear_marked();
99 		else
100 			server_set_marked(s, wl, wp);
101 		markedwp = marked_pane.wp;
102 
103 		if (lastwp != NULL) {
104 			server_redraw_window_borders(lastwp->window);
105 			server_status_window(lastwp->window);
106 		}
107 		if (markedwp != NULL) {
108 			server_redraw_window_borders(markedwp->window);
109 			server_status_window(markedwp->window);
110 		}
111 		return (CMD_RETURN_NORMAL);
112 	}
113 
114 	if (args_has(self->args, 'P') || args_has(self->args, 'g')) {
115 		if (args_has(args, 'P')) {
116 			style = args_get(args, 'P');
117 			if (style_parse(&grid_default_cell, &wp->colgc,
118 			    style) == -1) {
119 				cmdq_error(item, "bad style: %s", style);
120 				return (CMD_RETURN_ERROR);
121 			}
122 			wp->flags |= PANE_REDRAW;
123 		}
124 		if (args_has(self->args, 'g'))
125 			cmdq_print(item, "%s", style_tostring(&wp->colgc));
126 		return (CMD_RETURN_NORMAL);
127 	}
128 
129 	if (args_has(self->args, 'L')) {
130 		server_unzoom_window(wp->window);
131 		wp = window_pane_find_left(wp);
132 	} else if (args_has(self->args, 'R')) {
133 		server_unzoom_window(wp->window);
134 		wp = window_pane_find_right(wp);
135 	} else if (args_has(self->args, 'U')) {
136 		server_unzoom_window(wp->window);
137 		wp = window_pane_find_up(wp);
138 	} else if (args_has(self->args, 'D')) {
139 		server_unzoom_window(wp->window);
140 		wp = window_pane_find_down(wp);
141 	}
142 	if (wp == NULL)
143 		return (CMD_RETURN_NORMAL);
144 
145 	if (args_has(self->args, 'e')) {
146 		wp->flags &= ~PANE_INPUTOFF;
147 		return (CMD_RETURN_NORMAL);
148 	}
149 	if (args_has(self->args, 'd')) {
150 		wp->flags |= PANE_INPUTOFF;
151 		return (CMD_RETURN_NORMAL);
152 	}
153 
154 	if (args_has(self->args, 'T')) {
155 		pane_title = format_single(item, args_get(self->args, 'T'),
156 		    c, s, wl, wp);
157 		screen_set_title(&wp->base, pane_title);
158 		server_status_window(wp->window);
159 		free(pane_title);
160 	}
161 
162 	if (wp == w->active)
163 		return (CMD_RETURN_NORMAL);
164 	server_unzoom_window(wp->window);
165 	if (!window_pane_visible(wp)) {
166 		cmdq_error(item, "pane not visible");
167 		return (CMD_RETURN_ERROR);
168 	}
169 	window_redraw_active_switch(w, wp);
170 	if (window_set_active_pane(w, wp)) {
171 		cmd_find_from_winlink_pane(current, wl, wp, 0);
172 		hooks_insert(s->hooks, item, current, "after-select-pane");
173 		server_status_window(w);
174 		server_redraw_window_borders(w);
175 	}
176 
177 	return (CMD_RETURN_NORMAL);
178 }
179