1 /* $Id: cmd-move-window.c,v 1.5 2009-01-23 16:59:14 nicm Exp $ */
4 * Copyright (c) 2008 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 int cmd_move_window_exec(struct cmd
*, struct cmd_ctx
*);
31 const struct cmd_entry cmd_move_window_entry
= {
32 "move-window", "movew",
33 "[-dk] " CMD_SRCDST_WINDOW_USAGE
,
45 cmd_move_window_exec(struct cmd
*self
, struct cmd_ctx
*ctx
)
47 struct cmd_srcdst_data
*data
= self
->data
;
48 struct session
*src
, *dst
;
49 struct winlink
*wl_src
, *wl_dst
;
55 if ((wl_src
= cmd_find_window(ctx
, data
->src
, &src
)) == NULL
)
58 if (arg_parse_window(data
->dst
, &dst
, &idx
) != 0) {
59 ctx
->error(ctx
, "bad window: %s", data
->dst
);
63 dst
= ctx
->cursession
;
65 dst
= cmd_current_session(ctx
);
67 ctx
->error(ctx
, "session not found: %s", data
->dst
);
73 wl_dst
= winlink_find_by_index(&dst
->windows
, idx
);
75 if (wl_dst
->window
== wl_src
->window
)
78 if (data
->flags
& CMD_KFLAG
) {
80 * Can't use session_detach as it will destroy session
81 * if this makes it empty.
83 session_alert_cancel(dst
, wl_dst
);
84 winlink_stack_remove(&dst
->lastw
, wl_dst
);
85 winlink_remove(&dst
->windows
, wl_dst
);
87 /* Force select/redraw if current. */
88 if (wl_dst
== dst
->curw
) {
89 data
->flags
&= ~CMD_DFLAG
;
95 wl_dst
= session_attach(dst
, wl_src
->window
, idx
, &cause
);
97 ctx
->error(ctx
, "attach window failed: %s", cause
);
102 destroyed
= session_detach(src
, wl_src
);
103 for (i
= 0; i
< ARRAY_LENGTH(&clients
); i
++) {
104 c
= ARRAY_ITEM(&clients
, i
);
105 if (c
== NULL
|| c
->session
!= src
)
109 server_write_client(c
, MSG_EXIT
, NULL
, 0);
111 server_redraw_client(c
);
114 if (data
->flags
& CMD_DFLAG
)
115 server_status_session(dst
);
117 session_select(dst
, wl_dst
->idx
);
118 server_redraw_session(dst
);