Annotation of src/usr.bin/tmux/cmd-move-window.c, Revision 1.1
1.1 ! nicm 1: /* $OpenBSD$ */
! 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,
! 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
! 42: };
! 43:
! 44: int
! 45: cmd_move_window_exec(struct cmd *self, struct cmd_ctx *ctx)
! 46: {
! 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;
! 54:
! 55: if ((wl_src = cmd_find_window(ctx, data->src, &src)) == NULL)
! 56: return (-1);
! 57:
! 58: if (arg_parse_window(data->dst, &dst, &idx) != 0) {
! 59: ctx->error(ctx, "bad window: %s", data->dst);
! 60: return (-1);
! 61: }
! 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);
! 69: }
! 70:
! 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);
! 77:
! 78: if (data->flags & CMD_KFLAG) {
! 79: /*
! 80: * Can't use session_detach as it will destroy session
! 81: * if this makes it empty.
! 82: */
! 83: session_alert_cancel(dst, wl_dst);
! 84: winlink_stack_remove(&dst->lastw, wl_dst);
! 85: winlink_remove(&dst->windows, wl_dst);
! 86:
! 87: /* Force select/redraw if current. */
! 88: if (wl_dst == dst->curw) {
! 89: data->flags &= ~CMD_DFLAG;
! 90: dst->curw = NULL;
! 91: }
! 92: }
! 93: }
! 94:
! 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);
! 100: }
! 101:
! 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);
! 112: }
! 113:
! 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);
! 119: }
! 120: recalculate_sizes();
! 121:
! 122: return (0);
! 123: }