xref: /openbsd-src/usr.bin/tmux/cmd-rotate-window.c (revision 99fd087599a8791921855f21bd7e36130f39aadc)
1 /* $OpenBSD: cmd-rotate-window.c,v 1.27 2019/08/14 09:58:31 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  * Rotate the panes in a window.
25  */
26 
27 static enum cmd_retval	cmd_rotate_window_exec(struct cmd *,
28 			    struct cmdq_item *);
29 
30 const struct cmd_entry cmd_rotate_window_entry = {
31 	.name = "rotate-window",
32 	.alias = "rotatew",
33 
34 	.args = { "Dt:UZ", 0, 0 },
35 	.usage = "[-DUZ] " CMD_TARGET_WINDOW_USAGE,
36 
37 	.target = { 't', CMD_FIND_WINDOW, 0 },
38 
39 	.flags = 0,
40 	.exec = cmd_rotate_window_exec
41 };
42 
43 static enum cmd_retval
44 cmd_rotate_window_exec(struct cmd *self, struct cmdq_item *item)
45 {
46 	struct cmd_find_state	*current = &item->shared->current;
47 	struct winlink		*wl = item->target.wl;
48 	struct window		*w = wl->window;
49 	struct window_pane	*wp, *wp2;
50 	struct layout_cell	*lc;
51 	u_int			 sx, sy, xoff, yoff;
52 
53 	window_push_zoom(w, args_has(self->args, 'Z'));
54 
55 	if (args_has(self->args, 'D')) {
56 		wp = TAILQ_LAST(&w->panes, window_panes);
57 		TAILQ_REMOVE(&w->panes, wp, entry);
58 		TAILQ_INSERT_HEAD(&w->panes, wp, entry);
59 
60 		lc = wp->layout_cell;
61 		xoff = wp->xoff; yoff = wp->yoff;
62 		sx = wp->sx; sy = wp->sy;
63 		TAILQ_FOREACH(wp, &w->panes, entry) {
64 			if ((wp2 = TAILQ_NEXT(wp, entry)) == NULL)
65 				break;
66 			wp->layout_cell = wp2->layout_cell;
67 			if (wp->layout_cell != NULL)
68 				wp->layout_cell->wp = wp;
69 			wp->xoff = wp2->xoff; wp->yoff = wp2->yoff;
70 			window_pane_resize(wp, wp2->sx, wp2->sy);
71 		}
72 		wp->layout_cell = lc;
73 		if (wp->layout_cell != NULL)
74 			wp->layout_cell->wp = wp;
75 		wp->xoff = xoff; wp->yoff = yoff;
76 		window_pane_resize(wp, sx, sy);
77 
78 		if ((wp = TAILQ_PREV(w->active, window_panes, entry)) == NULL)
79 			wp = TAILQ_LAST(&w->panes, window_panes);
80 	} else {
81 		wp = TAILQ_FIRST(&w->panes);
82 		TAILQ_REMOVE(&w->panes, wp, entry);
83 		TAILQ_INSERT_TAIL(&w->panes, wp, entry);
84 
85 		lc = wp->layout_cell;
86 		xoff = wp->xoff; yoff = wp->yoff;
87 		sx = wp->sx; sy = wp->sy;
88 		TAILQ_FOREACH_REVERSE(wp, &w->panes, window_panes, entry) {
89 			if ((wp2 = TAILQ_PREV(wp, window_panes, entry)) == NULL)
90 				break;
91 			wp->layout_cell = wp2->layout_cell;
92 			if (wp->layout_cell != NULL)
93 				wp->layout_cell->wp = wp;
94 			wp->xoff = wp2->xoff; wp->yoff = wp2->yoff;
95 			window_pane_resize(wp, wp2->sx, wp2->sy);
96 		}
97 		wp->layout_cell = lc;
98 		if (wp->layout_cell != NULL)
99 			wp->layout_cell->wp = wp;
100 		wp->xoff = xoff; wp->yoff = yoff;
101 		window_pane_resize(wp, sx, sy);
102 
103 		if ((wp = TAILQ_NEXT(w->active, entry)) == NULL)
104 			wp = TAILQ_FIRST(&w->panes);
105 	}
106 
107 	window_set_active_pane(w, wp, 1);
108 	cmd_find_from_winlink_pane(current, wl, wp, 0);
109 	window_pop_zoom(w);
110 	server_redraw_window(w);
111 
112 	return (CMD_RETURN_NORMAL);
113 }
114