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