xref: /openbsd-src/usr.bin/tmux/cmd-rotate-window.c (revision 76dc86626f6ba0640e88a8fadd94cb6158b55963)
1 /* $OpenBSD: cmd-rotate-window.c,v 1.4 2009/06/25 08:08:18 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 	CMD_BIGUFLAG|CMD_BIGDFLAG,
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->flags |= CMD_BIGDFLAG;
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 	u_int			 sx, sy, xoff, yoff;
63 	int			 flags;
64 
65 	if ((wl = cmd_find_window(ctx, data->target, NULL)) == NULL)
66 		return (-1);
67 	w = wl->window;
68 
69 	if (data->flags & CMD_BIGDFLAG) {
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 		xoff = wp->xoff; yoff = wp->yoff;
75 		sx = wp->sx; sy = wp->sy;
76 		flags = wp->flags;
77 		TAILQ_FOREACH(wp, &w->panes, entry) {
78 			if ((wp2 = TAILQ_NEXT(wp, entry)) == NULL)
79 				break;
80 			wp->xoff = wp2->xoff; wp->yoff = wp2->yoff;
81 			wp->flags &= ~PANE_HIDDEN;
82 			wp->flags |= wp2->flags & PANE_HIDDEN;
83 			window_pane_resize(wp, wp2->sx, wp2->sy);
84 		}
85 		wp->xoff = xoff; wp->yoff = yoff;
86 		wp->flags &= ~PANE_HIDDEN;
87 		wp->flags |= flags & PANE_HIDDEN;
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 	} else {
94 		wp = TAILQ_FIRST(&w->panes);
95 		TAILQ_REMOVE(&w->panes, wp, entry);
96 		TAILQ_INSERT_TAIL(&w->panes, wp, entry);
97 
98 		xoff = wp->xoff; yoff = wp->yoff;
99 		sx = wp->sx; sy = wp->sy;
100 		flags = wp->flags;
101 		TAILQ_FOREACH_REVERSE(wp, &w->panes, window_panes, entry) {
102 			if ((wp2 = TAILQ_PREV(wp, window_panes, entry)) == NULL)
103 				break;
104 			wp->xoff = wp2->xoff; wp->yoff = wp2->yoff;
105 			wp->flags &= ~PANE_HIDDEN;
106 			wp->flags |= wp2->flags & PANE_HIDDEN;
107 			window_pane_resize(wp, wp2->sx, wp2->sy);
108 		}
109 		wp->xoff = xoff; wp->yoff = yoff;
110 		wp->flags &= ~PANE_HIDDEN;
111 		wp->flags |= flags & PANE_HIDDEN;
112 		window_pane_resize(wp, sx, sy);
113 
114 		if ((wp = TAILQ_NEXT(w->active, entry)) == NULL)
115 			wp = TAILQ_FIRST(&w->panes);
116 		window_set_active_pane(w, wp);
117 	}
118 
119 	layout_refresh(w, 0);
120 
121 	return (0);
122 }
123