Don't return if in the current window since we may want to report a bell
[tmux-openbsd.git] / cmd-rotate-window.c
blobfbfba09916d77e891859415cb65016a2adf7312f
1 /* $OpenBSD$ */
3 /*
4 * Copyright (c) 2009 Nicholas Marriott <nicm@users.sourceforge.net>
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.
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.
19 #include <sys/types.h>
21 #include "tmux.h"
24 * Rotate the panes in a window.
27 void cmd_rotate_window_init(struct cmd *, int);
28 int cmd_rotate_window_exec(struct cmd *, struct cmd_ctx *);
30 const struct cmd_entry cmd_rotate_window_entry = {
31 "rotate-window", "rotatew",
32 "[-DU] " CMD_TARGET_WINDOW_USAGE,
33 0, "DU",
34 cmd_rotate_window_init,
35 cmd_target_parse,
36 cmd_rotate_window_exec,
37 cmd_target_free,
38 cmd_target_print
41 void
42 cmd_rotate_window_init(struct cmd *self, int key)
44 struct cmd_target_data *data;
46 cmd_target_init(self, key);
47 data = self->data;
49 if (key == ('o' | KEYC_ESCAPE))
50 cmd_set_flag(&data->chflags, 'D');
53 int
54 cmd_rotate_window_exec(struct cmd *self, struct cmd_ctx *ctx)
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;
63 if ((wl = cmd_find_window(ctx, data->target, NULL)) == NULL)
64 return (-1);
65 w = wl->window;
67 if (cmd_check_flag(data->chflags, 'D')) {
68 wp = TAILQ_LAST(&w->panes, window_panes);
69 TAILQ_REMOVE(&w->panes, wp, entry);
70 TAILQ_INSERT_HEAD(&w->panes, wp, entry);
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);
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);
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);
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);
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);
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);
123 return (0);