makefile: fixup bad merge
[tmux.git] / cmd-swap-pane.c
blob6931bd16c3e5730fe578aa29a2afd50d00e2acd9
1 /* $OpenBSD$ */
3 /*
4 * Copyright (c) 2009 Nicholas Marriott <nicholas.marriott@gmail.com>
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 static enum cmd_retval cmd_swap_pane_exec(struct cmd *, struct cmdq_item *);
31 const struct cmd_entry cmd_swap_pane_entry = {
32 .name = "swap-pane",
33 .alias = "swapp",
35 .args = { "dDs:t:UZ", 0, 0, NULL },
36 .usage = "[-dDUZ] " CMD_SRCDST_PANE_USAGE,
38 .source = { 's', CMD_FIND_PANE, CMD_FIND_DEFAULT_MARKED },
39 .target = { 't', CMD_FIND_PANE, 0 },
41 .flags = 0,
42 .exec = cmd_swap_pane_exec
45 static enum cmd_retval
46 cmd_swap_pane_exec(struct cmd *self, struct cmdq_item *item)
48 struct args *args = cmd_get_args(self);
49 struct cmd_find_state *source = cmdq_get_source(item);
50 struct cmd_find_state *target = cmdq_get_target(item);
51 struct window *src_w, *dst_w;
52 struct window_pane *tmp_wp, *src_wp, *dst_wp;
53 struct layout_cell *src_lc, *dst_lc;
54 u_int sx, sy, xoff, yoff;
56 dst_w = target->wl->window;
57 dst_wp = target->wp;
58 src_w = source->wl->window;
59 src_wp = source->wp;
61 if (window_push_zoom(dst_w, 0, args_has(args, 'Z')))
62 server_redraw_window(dst_w);
64 if (args_has(args, 'D')) {
65 src_w = dst_w;
66 src_wp = TAILQ_NEXT(dst_wp, entry);
67 if (src_wp == NULL)
68 src_wp = TAILQ_FIRST(&dst_w->panes);
69 } else if (args_has(args, 'U')) {
70 src_w = dst_w;
71 src_wp = TAILQ_PREV(dst_wp, window_panes, entry);
72 if (src_wp == NULL)
73 src_wp = TAILQ_LAST(&dst_w->panes, window_panes);
76 if (src_w != dst_w && window_push_zoom(src_w, 0, args_has(args, 'Z')))
77 server_redraw_window(src_w);
79 if (src_wp == dst_wp)
80 goto out;
82 server_client_remove_pane(src_wp);
83 server_client_remove_pane(dst_wp);
85 tmp_wp = TAILQ_PREV(dst_wp, window_panes, entry);
86 TAILQ_REMOVE(&dst_w->panes, dst_wp, entry);
87 TAILQ_REPLACE(&src_w->panes, src_wp, dst_wp, entry);
88 if (tmp_wp == src_wp)
89 tmp_wp = dst_wp;
90 if (tmp_wp == NULL)
91 TAILQ_INSERT_HEAD(&dst_w->panes, src_wp, entry);
92 else
93 TAILQ_INSERT_AFTER(&dst_w->panes, tmp_wp, src_wp, entry);
95 src_lc = src_wp->layout_cell;
96 dst_lc = dst_wp->layout_cell;
97 src_lc->wp = dst_wp;
98 dst_wp->layout_cell = src_lc;
99 dst_lc->wp = src_wp;
100 src_wp->layout_cell = dst_lc;
102 src_wp->window = dst_w;
103 options_set_parent(src_wp->options, dst_w->options);
104 src_wp->flags |= PANE_STYLECHANGED;
105 dst_wp->window = src_w;
106 options_set_parent(dst_wp->options, src_w->options);
107 dst_wp->flags |= PANE_STYLECHANGED;
109 sx = src_wp->sx; sy = src_wp->sy;
110 xoff = src_wp->xoff; yoff = src_wp->yoff;
111 src_wp->xoff = dst_wp->xoff; src_wp->yoff = dst_wp->yoff;
112 window_pane_resize(src_wp, dst_wp->sx, dst_wp->sy);
113 dst_wp->xoff = xoff; dst_wp->yoff = yoff;
114 window_pane_resize(dst_wp, sx, sy);
116 if (!args_has(args, 'd')) {
117 if (src_w != dst_w) {
118 window_set_active_pane(src_w, dst_wp, 1);
119 window_set_active_pane(dst_w, src_wp, 1);
120 } else {
121 tmp_wp = dst_wp;
122 window_set_active_pane(src_w, tmp_wp, 1);
124 } else {
125 if (src_w->active == src_wp)
126 window_set_active_pane(src_w, dst_wp, 1);
127 if (dst_w->active == dst_wp)
128 window_set_active_pane(dst_w, src_wp, 1);
130 if (src_w != dst_w) {
131 window_pane_stack_remove(&src_w->last_panes, src_wp);
132 window_pane_stack_remove(&dst_w->last_panes, dst_wp);
133 colour_palette_from_option(&src_wp->palette, src_wp->options);
134 colour_palette_from_option(&dst_wp->palette, dst_wp->options);
136 server_redraw_window(src_w);
137 server_redraw_window(dst_w);
138 notify_window("window-layout-changed", src_w);
139 if (src_w != dst_w)
140 notify_window("window-layout-changed", dst_w);
142 out:
143 if (window_pop_zoom(src_w))
144 server_redraw_window(src_w);
145 if (src_w != dst_w && window_pop_zoom(dst_w))
146 server_redraw_window(dst_w);
147 return (CMD_RETURN_NORMAL);