xref: /openbsd-src/usr.bin/tmux/cmd-select-pane.c (revision d1df930ffab53da22f3324c32bed7ac5709915e6)
1 /* $OpenBSD: cmd-select-pane.c,v 1.44 2018/06/25 17:23:16 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 && window_count_panes(w) == 2) {
73 			lastwp = TAILQ_PREV(w->active, window_panes, entry);
74 			if (lastwp == NULL)
75 				lastwp = TAILQ_NEXT(w->active, entry);
76 		}
77 		if (lastwp == NULL) {
78 			cmdq_error(item, "no last pane");
79 			return (CMD_RETURN_ERROR);
80 		}
81 		if (args_has(self->args, 'e'))
82 			lastwp->flags &= ~PANE_INPUTOFF;
83 		else if (args_has(self->args, 'd'))
84 			lastwp->flags |= PANE_INPUTOFF;
85 		else {
86 			server_unzoom_window(w);
87 			window_redraw_active_switch(w, lastwp);
88 			if (window_set_active_pane(w, lastwp)) {
89 				cmd_find_from_winlink(current, wl, 0);
90 				server_status_window(w);
91 				server_redraw_window_borders(w);
92 			}
93 		}
94 		return (CMD_RETURN_NORMAL);
95 	}
96 
97 	if (args_has(args, 'm') || args_has(args, 'M')) {
98 		if (args_has(args, 'm') && !window_pane_visible(wp))
99 			return (CMD_RETURN_NORMAL);
100 		lastwp = marked_pane.wp;
101 
102 		if (args_has(args, 'M') || server_is_marked(s, wl, wp))
103 			server_clear_marked();
104 		else
105 			server_set_marked(s, wl, wp);
106 		markedwp = marked_pane.wp;
107 
108 		if (lastwp != NULL) {
109 			server_redraw_window_borders(lastwp->window);
110 			server_status_window(lastwp->window);
111 		}
112 		if (markedwp != NULL) {
113 			server_redraw_window_borders(markedwp->window);
114 			server_status_window(markedwp->window);
115 		}
116 		return (CMD_RETURN_NORMAL);
117 	}
118 
119 	if (args_has(self->args, 'P') || args_has(self->args, 'g')) {
120 		if (args_has(args, 'P')) {
121 			style = args_get(args, 'P');
122 			if (style_parse(&grid_default_cell, &wp->colgc,
123 			    style) == -1) {
124 				cmdq_error(item, "bad style: %s", style);
125 				return (CMD_RETURN_ERROR);
126 			}
127 			wp->flags |= PANE_REDRAW;
128 		}
129 		if (args_has(self->args, 'g'))
130 			cmdq_print(item, "%s", style_tostring(&wp->colgc));
131 		return (CMD_RETURN_NORMAL);
132 	}
133 
134 	if (args_has(self->args, 'L')) {
135 		server_unzoom_window(wp->window);
136 		wp = window_pane_find_left(wp);
137 	} else if (args_has(self->args, 'R')) {
138 		server_unzoom_window(wp->window);
139 		wp = window_pane_find_right(wp);
140 	} else if (args_has(self->args, 'U')) {
141 		server_unzoom_window(wp->window);
142 		wp = window_pane_find_up(wp);
143 	} else if (args_has(self->args, 'D')) {
144 		server_unzoom_window(wp->window);
145 		wp = window_pane_find_down(wp);
146 	}
147 	if (wp == NULL)
148 		return (CMD_RETURN_NORMAL);
149 
150 	if (args_has(self->args, 'e')) {
151 		wp->flags &= ~PANE_INPUTOFF;
152 		return (CMD_RETURN_NORMAL);
153 	}
154 	if (args_has(self->args, 'd')) {
155 		wp->flags |= PANE_INPUTOFF;
156 		return (CMD_RETURN_NORMAL);
157 	}
158 
159 	if (args_has(self->args, 'T')) {
160 		pane_title = format_single(item, args_get(self->args, 'T'),
161 		    c, s, wl, wp);
162 		screen_set_title(&wp->base, pane_title);
163 		server_status_window(wp->window);
164 		free(pane_title);
165 		return (CMD_RETURN_NORMAL);
166 	}
167 
168 	if (wp == w->active)
169 		return (CMD_RETURN_NORMAL);
170 	server_unzoom_window(wp->window);
171 	if (!window_pane_visible(wp)) {
172 		cmdq_error(item, "pane not visible");
173 		return (CMD_RETURN_ERROR);
174 	}
175 	window_redraw_active_switch(w, wp);
176 	if (window_set_active_pane(w, wp)) {
177 		cmd_find_from_winlink_pane(current, wl, wp, 0);
178 		hooks_insert(s->hooks, item, current, "after-select-pane");
179 		server_status_window(w);
180 		server_redraw_window_borders(w);
181 	}
182 
183 	return (CMD_RETURN_NORMAL);
184 }
185