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  * 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_q *);
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 	cmd_rotate_window_exec
37 };
38 
39 void
cmd_rotate_window_key_binding(struct cmd * self,int key)40 cmd_rotate_window_key_binding(struct cmd *self, int key)
41 {
42 	self->args = args_create(0);
43 	if (key == ('o' | KEYC_ESCAPE))
44 		args_set(self->args, 'D', NULL);
45 }
46 
47 enum cmd_retval
cmd_rotate_window_exec(struct cmd * self,struct cmd_q * cmdq)48 cmd_rotate_window_exec(struct cmd *self, struct cmd_q *cmdq)
49 {
50 	struct args		*args = self->args;
51 	struct winlink		*wl;
52 	struct window		*w;
53 	struct window_pane	*wp, *wp2;
54 	struct layout_cell	*lc;
55 	u_int			 sx, sy, xoff, yoff;
56 
57 	if ((wl = cmd_find_window(cmdq, args_get(args, 't'), NULL)) == NULL)
58 		return (CMD_RETURN_ERROR);
59 	w = wl->window;
60 
61 	if (args_has(self->args, 'D')) {
62 		wp = TAILQ_LAST(&w->panes, window_panes);
63 		TAILQ_REMOVE(&w->panes, wp, entry);
64 		TAILQ_INSERT_HEAD(&w->panes, wp, entry);
65 
66 		lc = wp->layout_cell;
67 		xoff = wp->xoff; yoff = wp->yoff;
68 		sx = wp->sx; sy = wp->sy;
69 		TAILQ_FOREACH(wp, &w->panes, entry) {
70 			if ((wp2 = TAILQ_NEXT(wp, entry)) == NULL)
71 				break;
72 			wp->layout_cell = wp2->layout_cell;
73 			if (wp->layout_cell != NULL)
74 				wp->layout_cell->wp = wp;
75 			wp->xoff = wp2->xoff; wp->yoff = wp2->yoff;
76 			window_pane_resize(wp, wp2->sx, wp2->sy);
77 		}
78 		wp->layout_cell = lc;
79 		if (wp->layout_cell != NULL)
80 			wp->layout_cell->wp = wp;
81 		wp->xoff = xoff; wp->yoff = yoff;
82 		window_pane_resize(wp, sx, sy);
83 
84 		if ((wp = TAILQ_PREV(w->active, window_panes, entry)) == NULL)
85 			wp = TAILQ_LAST(&w->panes, window_panes);
86 		window_set_active_pane(w, wp);
87 		server_redraw_window(w);
88 	} else {
89 		wp = TAILQ_FIRST(&w->panes);
90 		TAILQ_REMOVE(&w->panes, wp, entry);
91 		TAILQ_INSERT_TAIL(&w->panes, wp, entry);
92 
93 		lc = wp->layout_cell;
94 		xoff = wp->xoff; yoff = wp->yoff;
95 		sx = wp->sx; sy = wp->sy;
96 		TAILQ_FOREACH_REVERSE(wp, &w->panes, window_panes, entry) {
97 			if ((wp2 = TAILQ_PREV(wp, window_panes, entry)) == NULL)
98 				break;
99 			wp->layout_cell = wp2->layout_cell;
100 			if (wp->layout_cell != NULL)
101 				wp->layout_cell->wp = wp;
102 			wp->xoff = wp2->xoff; wp->yoff = wp2->yoff;
103 			window_pane_resize(wp, wp2->sx, wp2->sy);
104 		}
105 		wp->layout_cell = lc;
106 		if (wp->layout_cell != NULL)
107 			wp->layout_cell->wp = wp;
108 		wp->xoff = xoff; wp->yoff = yoff;
109 		window_pane_resize(wp, sx, sy);
110 
111 		if ((wp = TAILQ_NEXT(w->active, entry)) == NULL)
112 			wp = TAILQ_FIRST(&w->panes);
113 		window_set_active_pane(w, wp);
114 		server_redraw_window(w);
115 	}
116 
117 	return (CMD_RETURN_NORMAL);
118 }
119