1  | 
     | 
     | 
    /* $OpenBSD: cmd-join-pane.c,v 1.33 2017/08/30 10:33:57 nicm Exp $ */  | 
    
    
    2  | 
     | 
     | 
     | 
    
    
    3  | 
     | 
     | 
    /*  | 
    
    
    4  | 
     | 
     | 
     * Copyright (c) 2011 George Nachman <tmux@georgester.com>  | 
    
    
    5  | 
     | 
     | 
     * Copyright (c) 2009 Nicholas Marriott <nicholas.marriott@gmail.com>  | 
    
    
    6  | 
     | 
     | 
     *  | 
    
    
    7  | 
     | 
     | 
     * Permission to use, copy, modify, and distribute this software for any  | 
    
    
    8  | 
     | 
     | 
     * purpose with or without fee is hereby granted, provided that the above  | 
    
    
    9  | 
     | 
     | 
     * copyright notice and this permission notice appear in all copies.  | 
    
    
    10  | 
     | 
     | 
     *  | 
    
    
    11  | 
     | 
     | 
     * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES  | 
    
    
    12  | 
     | 
     | 
     * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF  | 
    
    
    13  | 
     | 
     | 
     * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR  | 
    
    
    14  | 
     | 
     | 
     * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES  | 
    
    
    15  | 
     | 
     | 
     * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER  | 
    
    
    16  | 
     | 
     | 
     * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING  | 
    
    
    17  | 
     | 
     | 
     * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.  | 
    
    
    18  | 
     | 
     | 
     */  | 
    
    
    19  | 
     | 
     | 
     | 
    
    
    20  | 
     | 
     | 
    #include <sys/types.h>  | 
    
    
    21  | 
     | 
     | 
     | 
    
    
    22  | 
     | 
     | 
    #include <paths.h>  | 
    
    
    23  | 
     | 
     | 
    #include <stdlib.h>  | 
    
    
    24  | 
     | 
     | 
    #include <unistd.h>  | 
    
    
    25  | 
     | 
     | 
     | 
    
    
    26  | 
     | 
     | 
    #include "tmux.h"  | 
    
    
    27  | 
     | 
     | 
     | 
    
    
    28  | 
     | 
     | 
    /*  | 
    
    
    29  | 
     | 
     | 
     * Join or move a pane into another (like split/swap/kill).  | 
    
    
    30  | 
     | 
     | 
     */  | 
    
    
    31  | 
     | 
     | 
     | 
    
    
    32  | 
     | 
     | 
    static enum cmd_retval	cmd_join_pane_exec(struct cmd *, struct cmdq_item *);  | 
    
    
    33  | 
     | 
     | 
     | 
    
    
    34  | 
     | 
     | 
    const struct cmd_entry cmd_join_pane_entry = { | 
    
    
    35  | 
     | 
     | 
    	.name = "join-pane",  | 
    
    
    36  | 
     | 
     | 
    	.alias = "joinp",  | 
    
    
    37  | 
     | 
     | 
     | 
    
    
    38  | 
     | 
     | 
    	.args = { "bdhvp:l:s:t:", 0, 0 }, | 
    
    
    39  | 
     | 
     | 
    	.usage = "[-bdhv] [-p percentage|-l size] " CMD_SRCDST_PANE_USAGE,  | 
    
    
    40  | 
     | 
     | 
     | 
    
    
    41  | 
     | 
     | 
    	.source = { 's', CMD_FIND_PANE, CMD_FIND_DEFAULT_MARKED }, | 
    
    
    42  | 
     | 
     | 
    	.target = { 't', CMD_FIND_PANE, 0 }, | 
    
    
    43  | 
     | 
     | 
     | 
    
    
    44  | 
     | 
     | 
    	.flags = 0,  | 
    
    
    45  | 
     | 
     | 
    	.exec = cmd_join_pane_exec  | 
    
    
    46  | 
     | 
     | 
    };  | 
    
    
    47  | 
     | 
     | 
     | 
    
    
    48  | 
     | 
     | 
    const struct cmd_entry cmd_move_pane_entry = { | 
    
    
    49  | 
     | 
     | 
    	.name = "move-pane",  | 
    
    
    50  | 
     | 
     | 
    	.alias = "movep",  | 
    
    
    51  | 
     | 
     | 
     | 
    
    
    52  | 
     | 
     | 
    	.args = { "bdhvp:l:s:t:", 0, 0 }, | 
    
    
    53  | 
     | 
     | 
    	.usage = "[-bdhv] [-p percentage|-l size] " CMD_SRCDST_PANE_USAGE,  | 
    
    
    54  | 
     | 
     | 
     | 
    
    
    55  | 
     | 
     | 
    	.source = { 's', CMD_FIND_PANE, 0 }, | 
    
    
    56  | 
     | 
     | 
    	.target = { 't', CMD_FIND_PANE, 0 }, | 
    
    
    57  | 
     | 
     | 
     | 
    
    
    58  | 
     | 
     | 
    	.flags = 0,  | 
    
    
    59  | 
     | 
     | 
    	.exec = cmd_join_pane_exec  | 
    
    
    60  | 
     | 
     | 
    };  | 
    
    
    61  | 
     | 
     | 
     | 
    
    
    62  | 
     | 
     | 
    static enum cmd_retval  | 
    
    
    63  | 
     | 
     | 
    cmd_join_pane_exec(struct cmd *self, struct cmdq_item *item)  | 
    
    
    64  | 
     | 
     | 
    { | 
    
    
    65  | 
     | 
     | 
    	struct args		*args = self->args;  | 
    
    
    66  | 
     | 
     | 
    	struct cmd_find_state	*current = &item->shared->current;  | 
    
    
    67  | 
     | 
     | 
    	struct session		*dst_s;  | 
    
    
    68  | 
     | 
     | 
    	struct winlink		*src_wl, *dst_wl;  | 
    
    
    69  | 
     | 
     | 
    	struct window		*src_w, *dst_w;  | 
    
    
    70  | 
     | 
     | 
    	struct window_pane	*src_wp, *dst_wp;  | 
    
    
    71  | 
     | 
     | 
    	char			*cause;  | 
    
    
    72  | 
     | 
     | 
    	int			 size, percentage, dst_idx;  | 
    
    
    73  | 
     | 
     | 
    	enum layout_type	 type;  | 
    
    
    74  | 
     | 
     | 
    	struct layout_cell	*lc;  | 
    
    
    75  | 
     | 
     | 
    	int			 not_same_window;  | 
    
    
    76  | 
     | 
     | 
     | 
    
    
    77  | 
     | 
     | 
    	if (self->entry == &cmd_join_pane_entry)  | 
    
    
    78  | 
     | 
     | 
    		not_same_window = 1;  | 
    
    
    79  | 
     | 
     | 
    	else  | 
    
    
    80  | 
     | 
     | 
    		not_same_window = 0;  | 
    
    
    81  | 
     | 
     | 
     | 
    
    
    82  | 
     | 
     | 
    	dst_s = item->target.s;  | 
    
    
    83  | 
     | 
     | 
    	dst_wl = item->target.wl;  | 
    
    
    84  | 
     | 
     | 
    	dst_wp = item->target.wp;  | 
    
    
    85  | 
     | 
     | 
    	dst_w = dst_wl->window;  | 
    
    
    86  | 
     | 
     | 
    	dst_idx = dst_wl->idx;  | 
    
    
    87  | 
     | 
     | 
    	server_unzoom_window(dst_w);  | 
    
    
    88  | 
     | 
     | 
     | 
    
    
    89  | 
     | 
     | 
    	src_wl = item->source.wl;  | 
    
    
    90  | 
     | 
     | 
    	src_wp = item->source.wp;  | 
    
    
    91  | 
     | 
     | 
    	src_w = src_wl->window;  | 
    
    
    92  | 
     | 
     | 
    	server_unzoom_window(src_w);  | 
    
    
    93  | 
     | 
     | 
     | 
    
    
    94  | 
     | 
     | 
    	if (not_same_window && src_w == dst_w) { | 
    
    
    95  | 
     | 
     | 
    		cmdq_error(item, "can't join a pane to its own window");  | 
    
    
    96  | 
     | 
     | 
    		return (CMD_RETURN_ERROR);  | 
    
    
    97  | 
     | 
     | 
    	}  | 
    
    
    98  | 
     | 
     | 
    	if (!not_same_window && src_wp == dst_wp) { | 
    
    
    99  | 
     | 
     | 
    		cmdq_error(item, "source and target panes must be different");  | 
    
    
    100  | 
     | 
     | 
    		return (CMD_RETURN_ERROR);  | 
    
    
    101  | 
     | 
     | 
    	}  | 
    
    
    102  | 
     | 
     | 
     | 
    
    
    103  | 
     | 
     | 
    	type = LAYOUT_TOPBOTTOM;  | 
    
    
    104  | 
     | 
     | 
    	if (args_has(args, 'h'))  | 
    
    
    105  | 
     | 
     | 
    		type = LAYOUT_LEFTRIGHT;  | 
    
    
    106  | 
     | 
     | 
     | 
    
    
    107  | 
     | 
     | 
    	size = -1;  | 
    
    
    108  | 
     | 
     | 
    	if (args_has(args, 'l')) { | 
    
    
    109  | 
     | 
     | 
    		size = args_strtonum(args, 'l', 0, INT_MAX, &cause);  | 
    
    
    110  | 
     | 
     | 
    		if (cause != NULL) { | 
    
    
    111  | 
     | 
     | 
    			cmdq_error(item, "size %s", cause);  | 
    
    
    112  | 
     | 
     | 
    			free(cause);  | 
    
    
    113  | 
     | 
     | 
    			return (CMD_RETURN_ERROR);  | 
    
    
    114  | 
     | 
     | 
    		}  | 
    
    
    115  | 
     | 
     | 
    	} else if (args_has(args, 'p')) { | 
    
    
    116  | 
     | 
     | 
    		percentage = args_strtonum(args, 'p', 0, 100, &cause);  | 
    
    
    117  | 
     | 
     | 
    		if (cause != NULL) { | 
    
    
    118  | 
     | 
     | 
    			cmdq_error(item, "percentage %s", cause);  | 
    
    
    119  | 
     | 
     | 
    			free(cause);  | 
    
    
    120  | 
     | 
     | 
    			return (CMD_RETURN_ERROR);  | 
    
    
    121  | 
     | 
     | 
    		}  | 
    
    
    122  | 
     | 
     | 
    		if (type == LAYOUT_TOPBOTTOM)  | 
    
    
    123  | 
     | 
     | 
    			size = (dst_wp->sy * percentage) / 100;  | 
    
    
    124  | 
     | 
     | 
    		else  | 
    
    
    125  | 
     | 
     | 
    			size = (dst_wp->sx * percentage) / 100;  | 
    
    
    126  | 
     | 
     | 
    	}  | 
    
    
    127  | 
     | 
     | 
    	lc = layout_split_pane(dst_wp, type, size, args_has(args, 'b'), 0);  | 
    
    
    128  | 
     | 
     | 
    	if (lc == NULL) { | 
    
    
    129  | 
     | 
     | 
    		cmdq_error(item, "create pane failed: pane too small");  | 
    
    
    130  | 
     | 
     | 
    		return (CMD_RETURN_ERROR);  | 
    
    
    131  | 
     | 
     | 
    	}  | 
    
    
    132  | 
     | 
     | 
     | 
    
    
    133  | 
     | 
     | 
    	layout_close_pane(src_wp);  | 
    
    
    134  | 
     | 
     | 
     | 
    
    
    135  | 
     | 
     | 
    	window_lost_pane(src_w, src_wp);  | 
    
    
    136  | 
     | 
     | 
    	TAILQ_REMOVE(&src_w->panes, src_wp, entry);  | 
    
    
    137  | 
     | 
     | 
     | 
    
    
    138  | 
     | 
     | 
    	src_wp->window = dst_w;  | 
    
    
    139  | 
     | 
     | 
    	TAILQ_INSERT_AFTER(&dst_w->panes, dst_wp, src_wp, entry);  | 
    
    
    140  | 
     | 
     | 
    	layout_assign_pane(lc, src_wp);  | 
    
    
    141  | 
     | 
     | 
     | 
    
    
    142  | 
     | 
     | 
    	recalculate_sizes();  | 
    
    
    143  | 
     | 
     | 
     | 
    
    
    144  | 
     | 
     | 
    	server_redraw_window(src_w);  | 
    
    
    145  | 
     | 
     | 
    	server_redraw_window(dst_w);  | 
    
    
    146  | 
     | 
     | 
     | 
    
    
    147  | 
     | 
     | 
    	if (!args_has(args, 'd')) { | 
    
    
    148  | 
     | 
     | 
    		window_set_active_pane(dst_w, src_wp);  | 
    
    
    149  | 
     | 
     | 
    		session_select(dst_s, dst_idx);  | 
    
    
    150  | 
     | 
     | 
    		cmd_find_from_session(current, dst_s, 0);  | 
    
    
    151  | 
     | 
     | 
    		server_redraw_session(dst_s);  | 
    
    
    152  | 
     | 
     | 
    	} else  | 
    
    
    153  | 
     | 
     | 
    		server_status_session(dst_s);  | 
    
    
    154  | 
     | 
     | 
     | 
    
    
    155  | 
     | 
     | 
    	if (window_count_panes(src_w) == 0)  | 
    
    
    156  | 
     | 
     | 
    		server_kill_window(src_w);  | 
    
    
    157  | 
     | 
     | 
    	else  | 
    
    
    158  | 
     | 
     | 
    		notify_window("window-layout-changed", src_w); | 
    
    
    159  | 
     | 
     | 
    	notify_window("window-layout-changed", dst_w); | 
    
    
    160  | 
     | 
     | 
     | 
    
    
    161  | 
     | 
     | 
    	return (CMD_RETURN_NORMAL);  | 
    
    
    162  | 
     | 
     | 
    }  |