1/* $Id: cmd-swap-pane.c,v 1.1.1.2 2011/08/17 18:40:04 jmmv Exp $ */ 2 3/* 4 * Copyright (c) 2009 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 * Swap two panes. 27 */ 28 29void cmd_swap_pane_key_binding(struct cmd *, int); 30int cmd_swap_pane_exec(struct cmd *, struct cmd_ctx *); 31 32const struct cmd_entry cmd_swap_pane_entry = { 33 "swap-pane", "swapp", 34 "dDs:t:U", 0, 0, 35 "[-dDU] " CMD_SRCDST_PANE_USAGE, 36 0, 37 cmd_swap_pane_key_binding, 38 NULL, 39 cmd_swap_pane_exec 40}; 41 42void 43cmd_swap_pane_key_binding(struct cmd *self, int key) 44{ 45 self->args = args_create(0); 46 if (key == '{') 47 args_set(self->args, 'U', NULL); 48 else if (key == '}') 49 args_set(self->args, 'D', NULL); 50} 51 52int 53cmd_swap_pane_exec(struct cmd *self, struct cmd_ctx *ctx) 54{ 55 struct args *args = self->args; 56 struct winlink *src_wl, *dst_wl; 57 struct window *src_w, *dst_w; 58 struct window_pane *tmp_wp, *src_wp, *dst_wp; 59 struct layout_cell *src_lc, *dst_lc; 60 u_int sx, sy, xoff, yoff; 61 62 dst_wl = cmd_find_pane(ctx, args_get(args, 't'), NULL, &dst_wp); 63 if (dst_wl == NULL) 64 return (-1); 65 dst_w = dst_wl->window; 66 67 if (!args_has(args, 's')) { 68 src_w = dst_w; 69 if (args_has(self->args, 'D')) { 70 src_wp = TAILQ_NEXT(dst_wp, entry); 71 if (src_wp == NULL) 72 src_wp = TAILQ_FIRST(&dst_w->panes); 73 } else if (args_has(self->args, 'U')) { 74 src_wp = TAILQ_PREV(dst_wp, window_panes, entry); 75 if (src_wp == NULL) 76 src_wp = TAILQ_LAST(&dst_w->panes, window_panes); 77 } else 78 return (0); 79 } else { 80 src_wl = cmd_find_pane(ctx, args_get(args, 's'), NULL, &src_wp); 81 if (src_wl == NULL) 82 return (-1); 83 src_w = src_wl->window; 84 } 85 86 if (src_wp == dst_wp) 87 return (0); 88 89 tmp_wp = TAILQ_PREV(dst_wp, window_panes, entry); 90 TAILQ_REMOVE(&dst_w->panes, dst_wp, entry); 91 TAILQ_REPLACE(&src_w->panes, src_wp, dst_wp, entry); 92 if (tmp_wp == src_wp) 93 tmp_wp = dst_wp; 94 if (tmp_wp == NULL) 95 TAILQ_INSERT_HEAD(&dst_w->panes, src_wp, entry); 96 else 97 TAILQ_INSERT_AFTER(&dst_w->panes, tmp_wp, src_wp, entry); 98 99 src_lc = src_wp->layout_cell; 100 dst_lc = dst_wp->layout_cell; 101 src_lc->wp = dst_wp; 102 dst_wp->layout_cell = src_lc; 103 dst_lc->wp = src_wp; 104 src_wp->layout_cell = dst_lc; 105 106 src_wp->window = dst_w; 107 dst_wp->window = src_w; 108 109 sx = src_wp->sx; sy = src_wp->sy; 110 xoff = src_wp->xoff; yoff = src_wp->yoff; 111 src_wp->xoff = dst_wp->xoff; src_wp->yoff = dst_wp->yoff; 112 window_pane_resize(src_wp, dst_wp->sx, dst_wp->sy); 113 dst_wp->xoff = xoff; dst_wp->yoff = yoff; 114 window_pane_resize(dst_wp, sx, sy); 115 116 if (!args_has(self->args, 'd')) { 117 if (src_w != dst_w) { 118 window_set_active_pane(src_w, dst_wp); 119 window_set_active_pane(dst_w, src_wp); 120 } else { 121 tmp_wp = dst_wp; 122 if (!window_pane_visible(tmp_wp)) 123 tmp_wp = src_wp; 124 window_set_active_pane(src_w, tmp_wp); 125 } 126 } else { 127 if (src_w->active == src_wp) 128 window_set_active_pane(src_w, dst_wp); 129 if (dst_w->active == dst_wp) 130 window_set_active_pane(dst_w, src_wp); 131 } 132 if (src_w != dst_w) { 133 if (src_w->last == src_wp) 134 src_w->last = NULL; 135 if (dst_w->last == dst_wp) 136 dst_w->last = NULL; 137 } 138 server_redraw_window(src_w); 139 server_redraw_window(dst_w); 140 141 return (0); 142} 143