Annotation of src/usr.bin/tmux/cmd-move-window.c, Revision 1.4
1.4 ! nicm 1: /* $OpenBSD: cmd-move-window.c,v 1.3 2009/07/13 23:11:35 nicm Exp $ */
1.1 nicm 2:
3: /*
4: * Copyright (c) 2008 Nicholas Marriott <nicm@users.sourceforge.net>
5: *
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.
9: *
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.
17: */
18:
19: #include <sys/types.h>
20:
21: #include <stdlib.h>
22:
23: #include "tmux.h"
24:
25: /*
26: * Move a window.
27: */
28:
29: int cmd_move_window_exec(struct cmd *, struct cmd_ctx *);
30:
31: const struct cmd_entry cmd_move_window_entry = {
32: "move-window", "movew",
33: "[-dk] " CMD_SRCDST_WINDOW_USAGE,
1.3 nicm 34: 0, CMD_CHFLAG('d')|CMD_CHFLAG('k'),
1.1 nicm 35: cmd_srcdst_init,
36: cmd_srcdst_parse,
37: cmd_move_window_exec,
38: cmd_srcdst_free,
39: cmd_srcdst_print
40: };
41:
42: int
43: cmd_move_window_exec(struct cmd *self, struct cmd_ctx *ctx)
44: {
45: struct cmd_srcdst_data *data = self->data;
46: struct session *src, *dst;
47: struct winlink *wl_src, *wl_dst;
48: struct client *c;
49: u_int i;
50: int destroyed, idx;
51: char *cause;
52:
53: if ((wl_src = cmd_find_window(ctx, data->src, &src)) == NULL)
54: return (-1);
1.2 nicm 55: if ((idx = cmd_find_index(ctx, data->dst, &dst)) == -2)
1.1 nicm 56: return (-1);
57:
58: wl_dst = NULL;
59: if (idx != -1)
60: wl_dst = winlink_find_by_index(&dst->windows, idx);
61: if (wl_dst != NULL) {
62: if (wl_dst->window == wl_src->window)
63: return (0);
64:
1.3 nicm 65: if (data->chflags & CMD_CHFLAG('k')) {
1.1 nicm 66: /*
67: * Can't use session_detach as it will destroy session
68: * if this makes it empty.
69: */
70: session_alert_cancel(dst, wl_dst);
71: winlink_stack_remove(&dst->lastw, wl_dst);
72: winlink_remove(&dst->windows, wl_dst);
73:
74: /* Force select/redraw if current. */
75: if (wl_dst == dst->curw) {
1.3 nicm 76: data->chflags &= ~CMD_CHFLAG('d');
1.1 nicm 77: dst->curw = NULL;
78: }
79: }
80: }
81:
82: wl_dst = session_attach(dst, wl_src->window, idx, &cause);
83: if (wl_dst == NULL) {
84: ctx->error(ctx, "attach window failed: %s", cause);
85: xfree(cause);
86: return (-1);
87: }
88:
89: destroyed = session_detach(src, wl_src);
90: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
91: c = ARRAY_ITEM(&clients, i);
92: if (c == NULL || c->session != src)
93: continue;
94: if (destroyed) {
95: c->session = NULL;
96: server_write_client(c, MSG_EXIT, NULL, 0);
97: } else
98: server_redraw_client(c);
99: }
100:
1.3 nicm 101: if (data->chflags & CMD_CHFLAG('d'))
1.1 nicm 102: server_status_session(dst);
103: else {
104: session_select(dst, wl_dst->idx);
105: server_redraw_session(dst);
106: }
107: recalculate_sizes();
108:
109: return (0);
110: }