1 /* $OpenBSD: cmd-split-window.c,v 1.92 2019/04/17 14:37:48 nicm Exp $ */ 2 3 /* 4 * Copyright (c) 2009 Nicholas Marriott <nicholas.marriott@gmail.com> 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 <errno.h> 22 #include <fcntl.h> 23 #include <paths.h> 24 #include <stdlib.h> 25 #include <string.h> 26 #include <unistd.h> 27 28 #include "tmux.h" 29 30 /* 31 * Split a window (add a new pane). 32 */ 33 34 #define SPLIT_WINDOW_TEMPLATE "#{session_name}:#{window_index}.#{pane_index}" 35 36 static enum cmd_retval cmd_split_window_exec(struct cmd *, 37 struct cmdq_item *); 38 39 const struct cmd_entry cmd_split_window_entry = { 40 .name = "split-window", 41 .alias = "splitw", 42 43 .args = { "bc:dfF:l:hp:Pt:v", 0, -1 }, 44 .usage = "[-bdfhvP] [-c start-directory] [-F format] " 45 "[-p percentage|-l size] " CMD_TARGET_PANE_USAGE " [command]", 46 47 .target = { 't', CMD_FIND_PANE, 0 }, 48 49 .flags = 0, 50 .exec = cmd_split_window_exec 51 }; 52 53 static enum cmd_retval 54 cmd_split_window_exec(struct cmd *self, struct cmdq_item *item) 55 { 56 struct args *args = self->args; 57 struct cmd_find_state *current = &item->shared->current; 58 struct spawn_context sc; 59 struct client *c = cmd_find_client(item, NULL, 1); 60 struct session *s = item->target.s; 61 struct winlink *wl = item->target.wl; 62 struct window_pane *wp = item->target.wp, *new_wp; 63 enum layout_type type; 64 struct layout_cell *lc; 65 struct cmd_find_state fs; 66 int size, percentage, flags; 67 const char *template; 68 char *cause, *cp; 69 70 if (args_has(args, 'h')) 71 type = LAYOUT_LEFTRIGHT; 72 else 73 type = LAYOUT_TOPBOTTOM; 74 if (args_has(args, 'l')) { 75 size = args_strtonum(args, 'l', 0, INT_MAX, &cause); 76 if (cause != NULL) { 77 cmdq_error(item, "create pane failed: -l %s", cause); 78 free(cause); 79 return (CMD_RETURN_ERROR); 80 } 81 } else if (args_has(args, 'p')) { 82 percentage = args_strtonum(args, 'p', 0, INT_MAX, &cause); 83 if (cause != NULL) { 84 cmdq_error(item, "create pane failed: -p %s", cause); 85 free(cause); 86 return (CMD_RETURN_ERROR); 87 } 88 if (type == LAYOUT_TOPBOTTOM) 89 size = (wp->sy * percentage) / 100; 90 else 91 size = (wp->sx * percentage) / 100; 92 } else 93 size = -1; 94 95 server_unzoom_window(wp->window); 96 97 flags = 0; 98 if (args_has(args, 'b')) 99 flags |= SPAWN_BEFORE; 100 if (args_has(args, 'f')) 101 flags |= SPAWN_FULLSIZE; 102 103 lc = layout_split_pane(wp, type, size, flags); 104 if (lc == NULL) { 105 cmdq_error(item, "no space for new pane"); 106 return (CMD_RETURN_ERROR); 107 } 108 109 memset(&sc, 0, sizeof sc); 110 sc.item = item; 111 sc.s = s; 112 sc.wl = wl; 113 114 sc.wp0 = wp; 115 sc.lc = lc; 116 117 sc.name = NULL; 118 sc.argc = args->argc; 119 sc.argv = args->argv; 120 121 sc.idx = -1; 122 sc.cwd = args_get(args, 'c'); 123 124 sc.flags = flags; 125 if (args_has(args, 'd')) 126 sc.flags |= SPAWN_DETACHED; 127 128 if ((new_wp = spawn_pane(&sc, &cause)) == NULL) { 129 cmdq_error(item, "create pane failed: %s", cause); 130 free(cause); 131 return (CMD_RETURN_ERROR); 132 } 133 if (!args_has(args, 'd')) 134 cmd_find_from_winlink_pane(current, wl, new_wp, 0); 135 server_redraw_window(wp->window); 136 server_status_session(s); 137 138 if (args_has(args, 'P')) { 139 if ((template = args_get(args, 'F')) == NULL) 140 template = SPLIT_WINDOW_TEMPLATE; 141 cp = format_single(item, template, c, s, wl, new_wp); 142 cmdq_print(item, "%s", cp); 143 free(cp); 144 } 145 146 cmd_find_from_winlink_pane(&fs, wl, new_wp, 0); 147 hooks_insert(s->hooks, item, &fs, "after-split-window"); 148 149 return (CMD_RETURN_NORMAL); 150 } 151