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