xref: /minix/external/bsd/tmux/dist/cmd-select-pane.c (revision 0a6a1f1d)
1 /* Id */
2 
3 /*
4  * Copyright (c) 2009 Nicholas Marriott <nicm@users.sourceforge.net>
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 void		 cmd_select_pane_key_binding(struct cmd *, int);
28 enum cmd_retval	 cmd_select_pane_exec(struct cmd *, struct cmd_q *);
29 
30 const struct cmd_entry cmd_select_pane_entry = {
31 	"select-pane", "selectp",
32 	"lDLRt:U", 0, 0,
33 	"[-lDLRU] " CMD_TARGET_PANE_USAGE,
34 	0,
35 	cmd_select_pane_key_binding,
36 	cmd_select_pane_exec
37 };
38 
39 const struct cmd_entry cmd_last_pane_entry = {
40 	"last-pane", "lastp",
41 	"t:", 0, 0,
42 	CMD_TARGET_WINDOW_USAGE,
43 	0,
44 	NULL,
45 	cmd_select_pane_exec
46 };
47 
48 void
cmd_select_pane_key_binding(struct cmd * self,int key)49 cmd_select_pane_key_binding(struct cmd *self, int key)
50 {
51 	self->args = args_create(0);
52 	if (key == KEYC_UP)
53 		args_set(self->args, 'U', NULL);
54 	if (key == KEYC_DOWN)
55 		args_set(self->args, 'D', NULL);
56 	if (key == KEYC_LEFT)
57 		args_set(self->args, 'L', NULL);
58 	if (key == KEYC_RIGHT)
59 		args_set(self->args, 'R', NULL);
60 	if (key == 'o')
61 		args_set(self->args, 't', ":.+");
62 }
63 
64 enum cmd_retval
cmd_select_pane_exec(struct cmd * self,struct cmd_q * cmdq)65 cmd_select_pane_exec(struct cmd *self, struct cmd_q *cmdq)
66 {
67 	struct args		*args = self->args;
68 	struct winlink		*wl;
69 	struct window_pane	*wp;
70 
71 	if (self->entry == &cmd_last_pane_entry || args_has(args, 'l')) {
72 		wl = cmd_find_window(cmdq, args_get(args, 't'), NULL);
73 		if (wl == NULL)
74 			return (CMD_RETURN_ERROR);
75 
76 		if (wl->window->last == NULL) {
77 			cmdq_error(cmdq, "no last pane");
78 			return (CMD_RETURN_ERROR);
79 		}
80 
81 		server_unzoom_window(wl->window);
82 		window_set_active_pane(wl->window, wl->window->last);
83 		server_status_window(wl->window);
84 		server_redraw_window_borders(wl->window);
85 
86 		return (CMD_RETURN_NORMAL);
87 	}
88 
89 	if ((wl = cmd_find_pane(cmdq, args_get(args, 't'), NULL, &wp)) == NULL)
90 		return (CMD_RETURN_ERROR);
91 
92 	server_unzoom_window(wp->window);
93 	if (!window_pane_visible(wp)) {
94 		cmdq_error(cmdq, "pane not visible");
95 		return (CMD_RETURN_ERROR);
96 	}
97 
98 	if (args_has(self->args, 'L'))
99 		wp = window_pane_find_left(wp);
100 	else if (args_has(self->args, 'R'))
101 		wp = window_pane_find_right(wp);
102 	else if (args_has(self->args, 'U'))
103 		wp = window_pane_find_up(wp);
104 	else if (args_has(self->args, 'D'))
105 		wp = window_pane_find_down(wp);
106 	if (wp == NULL) {
107 		cmdq_error(cmdq, "pane not found");
108 		return (CMD_RETURN_ERROR);
109 	}
110 
111 	window_set_active_pane(wl->window, wp);
112 	server_status_window(wl->window);
113 	server_redraw_window_borders(wl->window);
114 
115 	return (CMD_RETURN_NORMAL);
116 }
117