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