2009-06-01 22:58:49 +00:00
|
|
|
/* $OpenBSD$ */
|
|
|
|
|
|
|
|
/*
|
2016-01-19 15:59:12 +00:00
|
|
|
* Copyright (c) 2009 Nicholas Marriott <nicholas.marriott@gmail.com>
|
2009-06-01 22:58:49 +00:00
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
|
|
|
|
* IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include "tmux.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Swap two panes.
|
|
|
|
*/
|
|
|
|
|
2016-10-16 19:04:05 +00:00
|
|
|
static enum cmd_retval cmd_swap_pane_exec(struct cmd *, struct cmdq_item *);
|
2009-06-01 22:58:49 +00:00
|
|
|
|
|
|
|
const struct cmd_entry cmd_swap_pane_entry = {
|
2015-12-13 21:53:57 +00:00
|
|
|
.name = "swap-pane",
|
|
|
|
.alias = "swapp",
|
|
|
|
|
2021-08-21 10:22:38 +00:00
|
|
|
.args = { "dDs:t:UZ", 0, 0, NULL },
|
2019-08-14 09:58:31 +00:00
|
|
|
.usage = "[-dDUZ] " CMD_SRCDST_PANE_USAGE,
|
2015-12-13 21:53:57 +00:00
|
|
|
|
2017-04-22 10:22:39 +00:00
|
|
|
.source = { 's', CMD_FIND_PANE, CMD_FIND_DEFAULT_MARKED },
|
|
|
|
.target = { 't', CMD_FIND_PANE, 0 },
|
2015-12-14 00:31:54 +00:00
|
|
|
|
|
|
|
.flags = 0,
|
2015-12-13 21:53:57 +00:00
|
|
|
.exec = cmd_swap_pane_exec
|
2009-06-01 22:58:49 +00:00
|
|
|
};
|
|
|
|
|
2016-10-10 21:51:39 +00:00
|
|
|
static enum cmd_retval
|
2016-10-16 19:04:05 +00:00
|
|
|
cmd_swap_pane_exec(struct cmd *self, struct cmdq_item *item)
|
2009-06-01 22:58:49 +00:00
|
|
|
{
|
2020-04-13 08:26:27 +00:00
|
|
|
struct args *args = cmd_get_args(self);
|
2020-04-13 10:59:58 +00:00
|
|
|
struct cmd_find_state *source = cmdq_get_source(item);
|
|
|
|
struct cmd_find_state *target = cmdq_get_target(item);
|
2009-07-30 13:45:56 +00:00
|
|
|
struct window *src_w, *dst_w;
|
|
|
|
struct window_pane *tmp_wp, *src_wp, *dst_wp;
|
|
|
|
struct layout_cell *src_lc, *dst_lc;
|
|
|
|
u_int sx, sy, xoff, yoff;
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2020-04-13 10:59:58 +00:00
|
|
|
dst_w = target->wl->window;
|
|
|
|
dst_wp = target->wp;
|
|
|
|
src_w = source->wl->window;
|
|
|
|
src_wp = source->wp;
|
2009-07-30 13:45:56 +00:00
|
|
|
|
2021-03-11 06:31:05 +00:00
|
|
|
if (window_push_zoom(dst_w, 0, args_has(args, 'Z')))
|
2019-08-14 09:58:31 +00:00
|
|
|
server_redraw_window(dst_w);
|
|
|
|
|
|
|
|
if (args_has(args, 'D')) {
|
2015-12-14 00:31:54 +00:00
|
|
|
src_w = dst_w;
|
|
|
|
src_wp = TAILQ_NEXT(dst_wp, entry);
|
|
|
|
if (src_wp == NULL)
|
|
|
|
src_wp = TAILQ_FIRST(&dst_w->panes);
|
2019-08-14 09:58:31 +00:00
|
|
|
} else if (args_has(args, 'U')) {
|
2009-07-30 13:45:56 +00:00
|
|
|
src_w = dst_w;
|
2015-12-14 00:31:54 +00:00
|
|
|
src_wp = TAILQ_PREV(dst_wp, window_panes, entry);
|
|
|
|
if (src_wp == NULL)
|
|
|
|
src_wp = TAILQ_LAST(&dst_w->panes, window_panes);
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|
2019-08-14 09:58:31 +00:00
|
|
|
|
2021-03-11 06:31:05 +00:00
|
|
|
if (src_w != dst_w && window_push_zoom(src_w, 0, args_has(args, 'Z')))
|
2019-08-14 09:58:31 +00:00
|
|
|
server_redraw_window(src_w);
|
2009-06-01 22:58:49 +00:00
|
|
|
|
|
|
|
if (src_wp == dst_wp)
|
2019-08-14 09:58:31 +00:00
|
|
|
goto out;
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2020-05-16 16:20:59 +00:00
|
|
|
server_client_remove_pane(src_wp);
|
|
|
|
server_client_remove_pane(dst_wp);
|
|
|
|
|
2009-06-01 22:58:49 +00:00
|
|
|
tmp_wp = TAILQ_PREV(dst_wp, window_panes, entry);
|
2009-07-30 13:45:56 +00:00
|
|
|
TAILQ_REMOVE(&dst_w->panes, dst_wp, entry);
|
|
|
|
TAILQ_REPLACE(&src_w->panes, src_wp, dst_wp, entry);
|
2009-06-01 22:58:49 +00:00
|
|
|
if (tmp_wp == src_wp)
|
|
|
|
tmp_wp = dst_wp;
|
|
|
|
if (tmp_wp == NULL)
|
2009-07-30 13:45:56 +00:00
|
|
|
TAILQ_INSERT_HEAD(&dst_w->panes, src_wp, entry);
|
2009-06-01 22:58:49 +00:00
|
|
|
else
|
2009-07-30 13:45:56 +00:00
|
|
|
TAILQ_INSERT_AFTER(&dst_w->panes, tmp_wp, src_wp, entry);
|
|
|
|
|
|
|
|
src_lc = src_wp->layout_cell;
|
|
|
|
dst_lc = dst_wp->layout_cell;
|
|
|
|
src_lc->wp = dst_wp;
|
|
|
|
dst_wp->layout_cell = src_lc;
|
|
|
|
dst_lc->wp = src_wp;
|
|
|
|
src_wp->layout_cell = dst_lc;
|
2009-12-03 22:50:09 +00:00
|
|
|
|
2009-07-30 13:45:56 +00:00
|
|
|
src_wp->window = dst_w;
|
2019-06-20 11:59:59 +00:00
|
|
|
options_set_parent(src_wp->options, dst_w->options);
|
|
|
|
src_wp->flags |= PANE_STYLECHANGED;
|
2009-07-30 13:45:56 +00:00
|
|
|
dst_wp->window = src_w;
|
2019-06-20 11:59:59 +00:00
|
|
|
options_set_parent(dst_wp->options, src_w->options);
|
|
|
|
dst_wp->flags |= PANE_STYLECHANGED;
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2009-07-20 07:13:31 +00:00
|
|
|
sx = src_wp->sx; sy = src_wp->sy;
|
|
|
|
xoff = src_wp->xoff; yoff = src_wp->yoff;
|
|
|
|
src_wp->xoff = dst_wp->xoff; src_wp->yoff = dst_wp->yoff;
|
2009-06-01 22:58:49 +00:00
|
|
|
window_pane_resize(src_wp, dst_wp->sx, dst_wp->sy);
|
2009-07-20 07:13:31 +00:00
|
|
|
dst_wp->xoff = xoff; dst_wp->yoff = yoff;
|
|
|
|
window_pane_resize(dst_wp, sx, sy);
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2019-08-14 09:58:31 +00:00
|
|
|
if (!args_has(args, 'd')) {
|
2009-07-30 13:45:56 +00:00
|
|
|
if (src_w != dst_w) {
|
2019-04-17 14:37:48 +00:00
|
|
|
window_set_active_pane(src_w, dst_wp, 1);
|
|
|
|
window_set_active_pane(dst_w, src_wp, 1);
|
2009-07-30 13:45:56 +00:00
|
|
|
} else {
|
|
|
|
tmp_wp = dst_wp;
|
2019-04-17 14:37:48 +00:00
|
|
|
window_set_active_pane(src_w, tmp_wp, 1);
|
2009-07-30 13:45:56 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (src_w->active == src_wp)
|
2019-04-17 14:37:48 +00:00
|
|
|
window_set_active_pane(src_w, dst_wp, 1);
|
2009-07-30 13:45:56 +00:00
|
|
|
if (dst_w->active == dst_wp)
|
2019-04-17 14:37:48 +00:00
|
|
|
window_set_active_pane(dst_w, src_wp, 1);
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|
2011-05-08 21:12:52 +00:00
|
|
|
if (src_w != dst_w) {
|
|
|
|
if (src_w->last == src_wp)
|
|
|
|
src_w->last = NULL;
|
|
|
|
if (dst_w->last == dst_wp)
|
|
|
|
dst_w->last = NULL;
|
|
|
|
}
|
2009-07-30 13:45:56 +00:00
|
|
|
server_redraw_window(src_w);
|
|
|
|
server_redraw_window(dst_w);
|
2022-04-06 13:43:12 +00:00
|
|
|
notify_window("window-layout-changed", src_w);
|
|
|
|
if (src_w != dst_w)
|
|
|
|
notify_window("window-layout-changed", dst_w);
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2019-08-14 09:58:31 +00:00
|
|
|
out:
|
|
|
|
if (window_pop_zoom(src_w))
|
|
|
|
server_redraw_window(src_w);
|
|
|
|
if (src_w != dst_w && window_pop_zoom(dst_w))
|
|
|
|
server_redraw_window(dst_w);
|
2012-07-11 07:10:15 +00:00
|
|
|
return (CMD_RETURN_NORMAL);
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|