This commit was manufactured by cvs2svn to create tag 'TMUX_0_8'.
[tmux.git] / cmd-move-window.c
blobb7f02091f227694082cf475e5da689bf940f1028
1 /* $Id: cmd-move-window.c,v 1.5 2009-01-23 16:59:14 nicm Exp $ */
3 /*
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>
21 #include <stdlib.h>
23 #include "tmux.h"
26 * Move a window.
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,
34 CMD_DFLAG|CMD_KFLAG,
35 cmd_srcdst_init,
36 cmd_srcdst_parse,
37 cmd_move_window_exec,
38 cmd_srcdst_send,
39 cmd_srcdst_recv,
40 cmd_srcdst_free,
41 cmd_srcdst_print
44 int
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;
50 struct client *c;
51 u_int i;
52 int destroyed, idx;
53 char *cause;
55 if ((wl_src = cmd_find_window(ctx, data->src, &src)) == NULL)
56 return (-1);
58 if (arg_parse_window(data->dst, &dst, &idx) != 0) {
59 ctx->error(ctx, "bad window: %s", data->dst);
60 return (-1);
62 if (dst == NULL)
63 dst = ctx->cursession;
64 if (dst == NULL)
65 dst = cmd_current_session(ctx);
66 if (dst == NULL) {
67 ctx->error(ctx, "session not found: %s", data->dst);
68 return (-1);
71 wl_dst = NULL;
72 if (idx != -1)
73 wl_dst = winlink_find_by_index(&dst->windows, idx);
74 if (wl_dst != NULL) {
75 if (wl_dst->window == wl_src->window)
76 return (0);
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;
90 dst->curw = NULL;
95 wl_dst = session_attach(dst, wl_src->window, idx, &cause);
96 if (wl_dst == NULL) {
97 ctx->error(ctx, "attach window failed: %s", cause);
98 xfree(cause);
99 return (-1);
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)
106 continue;
107 if (destroyed) {
108 c->session = NULL;
109 server_write_client(c, MSG_EXIT, NULL, 0);
110 } else
111 server_redraw_client(c);
114 if (data->flags & CMD_DFLAG)
115 server_status_session(dst);
116 else {
117 session_select(dst, wl_dst->idx);
118 server_redraw_session(dst);
120 recalculate_sizes();
122 return (0);