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>
29 void cmd_swap_pane_key_binding(struct cmd
*, int);
30 enum cmd_retval
cmd_swap_pane_exec(struct cmd
*, struct cmd_q
*);
32 const struct cmd_entry cmd_swap_pane_entry
= {
35 "[-dDU] " CMD_SRCDST_PANE_USAGE
,
37 cmd_swap_pane_key_binding
,
43 cmd_swap_pane_key_binding(struct cmd
*self
, int key
)
45 self
->args
= args_create(0);
47 args_set(self
->args
, 'U', NULL
);
49 args_set(self
->args
, 'D', NULL
);
53 cmd_swap_pane_exec(struct cmd
*self
, struct cmd_q
*cmdq
)
55 struct args
*args
= self
->args
;
56 struct winlink
*src_wl
, *dst_wl
;
57 struct window
*src_w
, *dst_w
;
58 struct window_pane
*tmp_wp
, *src_wp
, *dst_wp
;
59 struct layout_cell
*src_lc
, *dst_lc
;
60 u_int sx
, sy
, xoff
, yoff
;
62 dst_wl
= cmd_find_pane(cmdq
, args_get(args
, 't'), NULL
, &dst_wp
);
64 return (CMD_RETURN_ERROR
);
65 dst_w
= dst_wl
->window
;
66 server_unzoom_window(dst_w
);
68 if (!args_has(args
, 's')) {
70 if (args_has(self
->args
, 'D')) {
71 src_wp
= TAILQ_NEXT(dst_wp
, entry
);
73 src_wp
= TAILQ_FIRST(&dst_w
->panes
);
74 } else if (args_has(self
->args
, 'U')) {
75 src_wp
= TAILQ_PREV(dst_wp
, window_panes
, entry
);
77 src_wp
= TAILQ_LAST(&dst_w
->panes
, window_panes
);
79 return (CMD_RETURN_NORMAL
);
81 src_wl
= cmd_find_pane(cmdq
, args_get(args
, 's'), NULL
, &src_wp
);
83 return (CMD_RETURN_ERROR
);
84 src_w
= src_wl
->window
;
86 server_unzoom_window(src_w
);
89 return (CMD_RETURN_NORMAL
);
91 tmp_wp
= TAILQ_PREV(dst_wp
, window_panes
, entry
);
92 TAILQ_REMOVE(&dst_w
->panes
, dst_wp
, entry
);
93 TAILQ_REPLACE(&src_w
->panes
, src_wp
, dst_wp
, entry
);
97 TAILQ_INSERT_HEAD(&dst_w
->panes
, src_wp
, entry
);
99 TAILQ_INSERT_AFTER(&dst_w
->panes
, tmp_wp
, src_wp
, entry
);
101 src_lc
= src_wp
->layout_cell
;
102 dst_lc
= dst_wp
->layout_cell
;
104 dst_wp
->layout_cell
= src_lc
;
106 src_wp
->layout_cell
= dst_lc
;
108 src_wp
->window
= dst_w
;
109 dst_wp
->window
= src_w
;
111 sx
= src_wp
->sx
; sy
= src_wp
->sy
;
112 xoff
= src_wp
->xoff
; yoff
= src_wp
->yoff
;
113 src_wp
->xoff
= dst_wp
->xoff
; src_wp
->yoff
= dst_wp
->yoff
;
114 window_pane_resize(src_wp
, dst_wp
->sx
, dst_wp
->sy
);
115 dst_wp
->xoff
= xoff
; dst_wp
->yoff
= yoff
;
116 window_pane_resize(dst_wp
, sx
, sy
);
118 if (!args_has(self
->args
, 'd')) {
119 if (src_w
!= dst_w
) {
120 window_set_active_pane(src_w
, dst_wp
);
121 window_set_active_pane(dst_w
, src_wp
);
124 if (!window_pane_visible(tmp_wp
))
126 window_set_active_pane(src_w
, tmp_wp
);
129 if (src_w
->active
== src_wp
)
130 window_set_active_pane(src_w
, dst_wp
);
131 if (dst_w
->active
== dst_wp
)
132 window_set_active_pane(dst_w
, src_wp
);
134 if (src_w
!= dst_w
) {
135 if (src_w
->last
== src_wp
)
137 if (dst_w
->last
== dst_wp
)
140 server_redraw_window(src_w
);
141 server_redraw_window(dst_w
);
143 return (CMD_RETURN_NORMAL
);