Rephrase a confusing sentence.
[tmux-openbsd.git] / cmd-swap-pane.c
blobbf4b048e4f7ace24d45ca667ade12fa6fd8af20f
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 <stdlib.h>
23 #include "tmux.h"
26 * Swap two panes.
29 void cmd_swap_pane_init(struct cmd *, int);
30 int cmd_swap_pane_exec(struct cmd *, struct cmd_ctx *);
32 const struct cmd_entry cmd_swap_pane_entry = {
33 "swap-pane", "swapp",
34 "[-dDU] " CMD_SRCDST_PANE_USAGE,
35 0, "dDU",
36 cmd_swap_pane_init,
37 cmd_srcdst_parse,
38 cmd_swap_pane_exec,
39 cmd_srcdst_free,
40 cmd_srcdst_print
43 void
44 cmd_swap_pane_init(struct cmd *self, int key)
46 struct cmd_target_data *data;
48 cmd_srcdst_init(self, key);
49 data = self->data;
51 if (key == '{')
52 cmd_set_flag(&data->chflags, 'U');
53 else if (key == '}')
54 cmd_set_flag(&data->chflags, 'D');
58 int
59 cmd_swap_pane_exec(struct cmd *self, struct cmd_ctx *ctx)
61 struct cmd_srcdst_data *data = self->data;
62 struct winlink *src_wl, *dst_wl;
63 struct window *src_w, *dst_w;
64 struct window_pane *tmp_wp, *src_wp, *dst_wp;
65 struct layout_cell *src_lc, *dst_lc;
66 u_int sx, sy, xoff, yoff;
68 if (data == NULL)
69 return (0);
71 if ((dst_wl = cmd_find_pane(ctx, data->dst, NULL, &dst_wp)) == NULL)
72 return (-1);
73 dst_w = dst_wl->window;
75 if (data->src == NULL) {
76 src_w = dst_w;
77 if (cmd_check_flag(data->chflags, 'D')) {
78 src_wp = TAILQ_NEXT(dst_wp, entry);
79 if (src_wp == NULL)
80 src_wp = TAILQ_FIRST(&dst_w->panes);
81 } else if (cmd_check_flag(data->chflags, 'U')) {
82 src_wp = TAILQ_PREV(dst_wp, window_panes, entry);
83 if (src_wp == NULL)
84 src_wp = TAILQ_LAST(&dst_w->panes, window_panes);
85 } else
86 return (0);
87 } else {
88 src_wl = cmd_find_pane(ctx, data->src, NULL, &src_wp);
89 if (src_wl == NULL)
90 return (-1);
91 src_w = src_wl->window;
94 if (src_wp == dst_wp)
95 return (0);
97 tmp_wp = TAILQ_PREV(dst_wp, window_panes, entry);
98 TAILQ_REMOVE(&dst_w->panes, dst_wp, entry);
99 TAILQ_REPLACE(&src_w->panes, src_wp, dst_wp, entry);
100 if (tmp_wp == src_wp)
101 tmp_wp = dst_wp;
102 if (tmp_wp == NULL)
103 TAILQ_INSERT_HEAD(&dst_w->panes, src_wp, entry);
104 else
105 TAILQ_INSERT_AFTER(&dst_w->panes, tmp_wp, src_wp, entry);
107 src_lc = src_wp->layout_cell;
108 dst_lc = dst_wp->layout_cell;
109 src_lc->wp = dst_wp;
110 dst_wp->layout_cell = src_lc;
111 dst_lc->wp = src_wp;
112 src_wp->layout_cell = dst_lc;
114 src_wp->window = dst_w;
115 dst_wp->window = src_w;
117 sx = src_wp->sx; sy = src_wp->sy;
118 xoff = src_wp->xoff; yoff = src_wp->yoff;
119 src_wp->xoff = dst_wp->xoff; src_wp->yoff = dst_wp->yoff;
120 window_pane_resize(src_wp, dst_wp->sx, dst_wp->sy);
121 dst_wp->xoff = xoff; dst_wp->yoff = yoff;
122 window_pane_resize(dst_wp, sx, sy);
124 if (!cmd_check_flag(data->chflags, 'd')) {
125 if (src_w != dst_w) {
126 window_set_active_pane(src_w, dst_wp);
127 window_set_active_pane(dst_w, src_wp);
128 } else {
129 tmp_wp = dst_wp;
130 if (!window_pane_visible(tmp_wp))
131 tmp_wp = src_wp;
132 window_set_active_pane(src_w, tmp_wp);
134 } else {
135 if (src_w->active == src_wp)
136 window_set_active_pane(src_w, dst_wp);
137 if (dst_w->active == dst_wp)
138 window_set_active_pane(dst_w, src_wp);
140 server_redraw_window(src_w);
141 server_redraw_window(dst_w);
143 return (0);