xref: /openbsd-src/usr.bin/tmux/cmd-select-pane.c (revision d4741794dd2f512d997014f8bd85fbb24d935059)
1 /* $OpenBSD: cmd-select-pane.c,v 1.35 2016/11/16 00:24:03 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 "tmux.h"
22 
23 /*
24  * Select pane.
25  */
26 
27 static enum cmd_retval	cmd_select_pane_exec(struct cmd *, struct cmdq_item *);
28 
29 const struct cmd_entry cmd_select_pane_entry = {
30 	.name = "select-pane",
31 	.alias = "selectp",
32 
33 	.args = { "DdegLlMmP:Rt:U", 0, 0 },
34 	.usage = "[-DdegLlMmRU] [-P style] " CMD_TARGET_PANE_USAGE,
35 
36 	.tflag = CMD_PANE,
37 
38 	.flags = 0,
39 	.exec = cmd_select_pane_exec
40 };
41 
42 const struct cmd_entry cmd_last_pane_entry = {
43 	.name = "last-pane",
44 	.alias = "lastp",
45 
46 	.args = { "det:", 0, 0 },
47 	.usage = "[-de] " CMD_TARGET_WINDOW_USAGE,
48 
49 	.tflag = CMD_WINDOW,
50 
51 	.flags = 0,
52 	.exec = cmd_select_pane_exec
53 };
54 
55 static enum cmd_retval
56 cmd_select_pane_exec(struct cmd *self, struct cmdq_item *item)
57 {
58 	struct args		*args = self->args;
59 	struct winlink		*wl = item->state.tflag.wl;
60 	struct window		*w = wl->window;
61 	struct session		*s = item->state.tflag.s;
62 	struct window_pane	*wp = item->state.tflag.wp, *lastwp, *markedwp;
63 	const char		*style;
64 
65 	if (self->entry == &cmd_last_pane_entry || args_has(args, 'l')) {
66 		if (wl->window->last == NULL) {
67 			cmdq_error(item, "no last pane");
68 			return (CMD_RETURN_ERROR);
69 		}
70 
71 		if (args_has(self->args, 'e'))
72 			w->last->flags &= ~PANE_INPUTOFF;
73 		else if (args_has(self->args, 'd'))
74 			w->last->flags |= PANE_INPUTOFF;
75 		else {
76 			server_unzoom_window(w);
77 			window_redraw_active_switch(w, w->last);
78 			if (window_set_active_pane(w, w->last)) {
79 				server_status_window(w);
80 				server_redraw_window_borders(w);
81 			}
82 		}
83 
84 		return (CMD_RETURN_NORMAL);
85 	}
86 
87 	if (args_has(args, 'm') || args_has(args, 'M')) {
88 		if (args_has(args, 'm') && !window_pane_visible(wp))
89 			return (CMD_RETURN_NORMAL);
90 		lastwp = marked_pane.wp;
91 
92 		if (args_has(args, 'M') || server_is_marked(s, wl, wp))
93 			server_clear_marked();
94 		else
95 			server_set_marked(s, wl, wp);
96 		markedwp = marked_pane.wp;
97 
98 		if (lastwp != NULL) {
99 			server_redraw_window_borders(lastwp->window);
100 			server_status_window(lastwp->window);
101 		}
102 		if (markedwp != NULL) {
103 			server_redraw_window_borders(markedwp->window);
104 			server_status_window(markedwp->window);
105 		}
106 		return (CMD_RETURN_NORMAL);
107 	}
108 
109 	if (args_has(self->args, 'P') || args_has(self->args, 'g')) {
110 		if (args_has(args, 'P')) {
111 			style = args_get(args, 'P');
112 			if (style_parse(&grid_default_cell, &wp->colgc,
113 			    style) == -1) {
114 				cmdq_error(item, "bad style: %s", style);
115 				return (CMD_RETURN_ERROR);
116 			}
117 			wp->flags |= PANE_REDRAW;
118 		}
119 		if (args_has(self->args, 'g'))
120 			cmdq_print(item, "%s", style_tostring(&wp->colgc));
121 		return (CMD_RETURN_NORMAL);
122 	}
123 
124 	if (args_has(self->args, 'L')) {
125 		server_unzoom_window(wp->window);
126 		wp = window_pane_find_left(wp);
127 	} else if (args_has(self->args, 'R')) {
128 		server_unzoom_window(wp->window);
129 		wp = window_pane_find_right(wp);
130 	} else if (args_has(self->args, 'U')) {
131 		server_unzoom_window(wp->window);
132 		wp = window_pane_find_up(wp);
133 	} else if (args_has(self->args, 'D')) {
134 		server_unzoom_window(wp->window);
135 		wp = window_pane_find_down(wp);
136 	}
137 	if (wp == NULL)
138 		return (CMD_RETURN_NORMAL);
139 
140 	if (args_has(self->args, 'e')) {
141 		wp->flags &= ~PANE_INPUTOFF;
142 		return (CMD_RETURN_NORMAL);
143 	}
144 	if (args_has(self->args, 'd')) {
145 		wp->flags |= PANE_INPUTOFF;
146 		return (CMD_RETURN_NORMAL);
147 	}
148 
149 	if (wp == w->active)
150 		return (CMD_RETURN_NORMAL);
151 	server_unzoom_window(wp->window);
152 	if (!window_pane_visible(wp)) {
153 		cmdq_error(item, "pane not visible");
154 		return (CMD_RETURN_ERROR);
155 	}
156 	window_redraw_active_switch(w, wp);
157 	if (window_set_active_pane(w, wp)) {
158 		server_status_window(w);
159 		server_redraw_window_borders(w);
160 	}
161 
162 	return (CMD_RETURN_NORMAL);
163 }
164