xref: /openbsd-src/usr.bin/tmux/cmd-rotate-window.c (revision a224d0d3ac3348ad8dc1a5ad50b3236b714355cb)
1 /* $OpenBSD: cmd-rotate-window.c,v 1.12 2012/07/11 07:10:15 nicm Exp $ */
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  * Rotate the panes in a window.
25  */
26 
27 void		 cmd_rotate_window_key_binding(struct cmd *, int);
28 enum cmd_retval	 cmd_rotate_window_exec(struct cmd *, struct cmd_ctx *);
29 
30 const struct cmd_entry cmd_rotate_window_entry = {
31 	"rotate-window", "rotatew",
32 	"Dt:U", 0, 0,
33 	"[-DU] " CMD_TARGET_WINDOW_USAGE,
34 	0,
35 	cmd_rotate_window_key_binding,
36 	NULL,
37 	cmd_rotate_window_exec
38 };
39 
40 void
41 cmd_rotate_window_key_binding(struct cmd *self, int key)
42 {
43 	self->args = args_create(0);
44 	if (key == ('o' | KEYC_ESCAPE))
45 		args_set(self->args, 'D', NULL);
46 }
47 
48 enum cmd_retval
49 cmd_rotate_window_exec(struct cmd *self, struct cmd_ctx *ctx)
50 {
51 	struct args		*args = self->args;
52 	struct winlink		*wl;
53 	struct window		*w;
54 	struct window_pane	*wp, *wp2;
55 	struct layout_cell	*lc;
56 	u_int			 sx, sy, xoff, yoff;
57 
58 	if ((wl = cmd_find_window(ctx, args_get(args, 't'), NULL)) == NULL)
59 		return (CMD_RETURN_ERROR);
60 	w = wl->window;
61 
62 	if (args_has(self->args, 'D')) {
63 		wp = TAILQ_LAST(&w->panes, window_panes);
64 		TAILQ_REMOVE(&w->panes, wp, entry);
65 		TAILQ_INSERT_HEAD(&w->panes, wp, entry);
66 
67 		lc = wp->layout_cell;
68 		xoff = wp->xoff; yoff = wp->yoff;
69 		sx = wp->sx; sy = wp->sy;
70 		TAILQ_FOREACH(wp, &w->panes, entry) {
71 			if ((wp2 = TAILQ_NEXT(wp, entry)) == NULL)
72 				break;
73 			wp->layout_cell = wp2->layout_cell;
74 			if (wp->layout_cell != NULL)
75 				wp->layout_cell->wp = wp;
76 			wp->xoff = wp2->xoff; wp->yoff = wp2->yoff;
77 			window_pane_resize(wp, wp2->sx, wp2->sy);
78 		}
79 		wp->layout_cell = lc;
80 		if (wp->layout_cell != NULL)
81 			wp->layout_cell->wp = wp;
82 		wp->xoff = xoff; wp->yoff = yoff;
83 		window_pane_resize(wp, sx, sy);
84 
85 		if ((wp = TAILQ_PREV(w->active, window_panes, entry)) == NULL)
86 			wp = TAILQ_LAST(&w->panes, window_panes);
87 		window_set_active_pane(w, wp);
88 		server_redraw_window(w);
89 	} else {
90 		wp = TAILQ_FIRST(&w->panes);
91 		TAILQ_REMOVE(&w->panes, wp, entry);
92 		TAILQ_INSERT_TAIL(&w->panes, wp, entry);
93 
94 		lc = wp->layout_cell;
95 		xoff = wp->xoff; yoff = wp->yoff;
96 		sx = wp->sx; sy = wp->sy;
97 		TAILQ_FOREACH_REVERSE(wp, &w->panes, window_panes, entry) {
98 			if ((wp2 = TAILQ_PREV(wp, window_panes, entry)) == NULL)
99 				break;
100 			wp->layout_cell = wp2->layout_cell;
101 			if (wp->layout_cell != NULL)
102 				wp->layout_cell->wp = wp;
103 			wp->xoff = wp2->xoff; wp->yoff = wp2->yoff;
104 			window_pane_resize(wp, wp2->sx, wp2->sy);
105 		}
106 		wp->layout_cell = lc;
107 		if (wp->layout_cell != NULL)
108 			wp->layout_cell->wp = wp;
109 		wp->xoff = xoff; wp->yoff = yoff;
110 		window_pane_resize(wp, sx, sy);
111 
112 		if ((wp = TAILQ_NEXT(w->active, entry)) == NULL)
113 			wp = TAILQ_FIRST(&w->panes);
114 		window_set_active_pane(w, wp);
115 		server_redraw_window(w);
116 	}
117 
118 	return (CMD_RETURN_NORMAL);
119 }
120