1*a51dead1Snicm /* $OpenBSD: cmd-swap-window.c,v 1.28 2021/08/21 10:22:39 nicm Exp $ */
2311827fbSnicm
3311827fbSnicm /*
498ca8272Snicm * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
5311827fbSnicm *
6311827fbSnicm * Permission to use, copy, modify, and distribute this software for any
7311827fbSnicm * purpose with or without fee is hereby granted, provided that the above
8311827fbSnicm * copyright notice and this permission notice appear in all copies.
9311827fbSnicm *
10311827fbSnicm * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11311827fbSnicm * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12311827fbSnicm * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13311827fbSnicm * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14311827fbSnicm * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15311827fbSnicm * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16311827fbSnicm * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17311827fbSnicm */
18311827fbSnicm
19311827fbSnicm #include <sys/types.h>
20311827fbSnicm
21311827fbSnicm #include <stdlib.h>
22311827fbSnicm
23311827fbSnicm #include "tmux.h"
24311827fbSnicm
25311827fbSnicm /*
26311827fbSnicm * Swap one window with another.
27311827fbSnicm */
28311827fbSnicm
2968e0a7f2Snicm static enum cmd_retval cmd_swap_window_exec(struct cmd *, struct cmdq_item *);
30311827fbSnicm
31311827fbSnicm const struct cmd_entry cmd_swap_window_entry = {
32c057646bSnicm .name = "swap-window",
33c057646bSnicm .alias = "swapw",
34c057646bSnicm
35*a51dead1Snicm .args = { "ds:t:", 0, 0, NULL },
36c057646bSnicm .usage = "[-d] " CMD_SRCDST_WINDOW_USAGE,
37c057646bSnicm
38bf0d297eSnicm .source = { 's', CMD_FIND_WINDOW, CMD_FIND_DEFAULT_MARKED },
39bf0d297eSnicm .target = { 't', CMD_FIND_WINDOW, 0 },
408d471e80Snicm
418d471e80Snicm .flags = 0,
42c057646bSnicm .exec = cmd_swap_window_exec
43311827fbSnicm };
44311827fbSnicm
45dc1f0f5fSnicm static enum cmd_retval
cmd_swap_window_exec(struct cmd * self,struct cmdq_item * item)4668e0a7f2Snicm cmd_swap_window_exec(struct cmd *self, struct cmdq_item *item)
47311827fbSnicm {
4890d7ba38Snicm struct args *args = cmd_get_args(self);
49040343aeSnicm struct cmd_find_state *source = cmdq_get_source(item);
50040343aeSnicm struct cmd_find_state *target = cmdq_get_target(item);
51040343aeSnicm struct session *src = source->s, *dst = target->s;
5201b2421eSnicm struct session_group *sg_src, *sg_dst;
53040343aeSnicm struct winlink *wl_src = source->wl, *wl_dst = target->wl;
54a42a75cbSnicm struct window *w_src, *w_dst;
55311827fbSnicm
5606440b28Snicm sg_src = session_group_contains(src);
5706440b28Snicm sg_dst = session_group_contains(dst);
583447b427Snicm
59040343aeSnicm if (src != dst &&
60040343aeSnicm sg_src != NULL &&
61040343aeSnicm sg_dst != NULL &&
623447b427Snicm sg_src == sg_dst) {
6368e0a7f2Snicm cmdq_error(item, "can't move window, sessions are grouped");
64a224d0d3Snicm return (CMD_RETURN_ERROR);
6501b2421eSnicm }
6601b2421eSnicm
67311827fbSnicm if (wl_dst->window == wl_src->window)
68a224d0d3Snicm return (CMD_RETURN_NORMAL);
69311827fbSnicm
70a42a75cbSnicm w_dst = wl_dst->window;
71a42a75cbSnicm TAILQ_REMOVE(&w_dst->winlinks, wl_dst, wentry);
72a42a75cbSnicm w_src = wl_src->window;
73a42a75cbSnicm TAILQ_REMOVE(&w_src->winlinks, wl_src, wentry);
74a42a75cbSnicm
75a42a75cbSnicm wl_dst->window = w_src;
76a42a75cbSnicm TAILQ_INSERT_TAIL(&w_src->winlinks, wl_dst, wentry);
77a42a75cbSnicm wl_src->window = w_dst;
78a42a75cbSnicm TAILQ_INSERT_TAIL(&w_dst->winlinks, wl_src, wentry);
79311827fbSnicm
8090d7ba38Snicm if (args_has(args, 'd')) {
8171a6d125Snicm session_select(dst, wl_dst->idx);
82d2a2fa77Snicm if (src != dst)
83d2a2fa77Snicm session_select(src, wl_src->idx);
84311827fbSnicm }
8501b2421eSnicm session_group_synchronize_from(src);
8601b2421eSnicm server_redraw_session_group(src);
8701b2421eSnicm if (src != dst) {
8801b2421eSnicm session_group_synchronize_from(dst);
8901b2421eSnicm server_redraw_session_group(dst);
9001b2421eSnicm }
91311827fbSnicm recalculate_sizes();
92311827fbSnicm
93a224d0d3Snicm return (CMD_RETURN_NORMAL);
94311827fbSnicm }
95