1 /* $OpenBSD: cmd-split-window.c,v 1.107 2021/08/20 19:50:17 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:de:fF:hIl:p:Pt:vZ", 0, -1 }, 44 .usage = "[-bdefhIPvZ] [-c start-directory] [-e environment] " 45 "[-F format] [-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 = cmd_get_args(self); 57 struct cmd_find_state *current = cmdq_get_current(item); 58 struct cmd_find_state *target = cmdq_get_target(item); 59 struct spawn_context sc = { 0 }; 60 struct client *tc = cmdq_get_target_client(item); 61 struct session *s = target->s; 62 struct winlink *wl = target->wl; 63 struct window_pane *wp = target->wp, *new_wp; 64 enum layout_type type; 65 struct layout_cell *lc; 66 struct cmd_find_state fs; 67 int size, percentage, flags, input; 68 const char *template, *errstr, *p; 69 char *cause, *cp, *copy; 70 size_t plen; 71 struct args_value *av; 72 u_int count = args_count(args); 73 74 if (args_has(args, 'h')) 75 type = LAYOUT_LEFTRIGHT; 76 else 77 type = LAYOUT_TOPBOTTOM; 78 if ((p = args_get(args, 'l')) != NULL) { 79 plen = strlen(p); 80 if (p[plen - 1] == '%') { 81 copy = xstrdup(p); 82 copy[plen - 1] = '\0'; 83 percentage = strtonum(copy, 0, INT_MAX, &errstr); 84 free(copy); 85 if (errstr != NULL) { 86 cmdq_error(item, "percentage %s", errstr); 87 return (CMD_RETURN_ERROR); 88 } 89 if (type == LAYOUT_TOPBOTTOM) 90 size = (wp->sy * percentage) / 100; 91 else 92 size = (wp->sx * percentage) / 100; 93 } else { 94 size = args_strtonum(args, 'l', 0, INT_MAX, &cause); 95 if (cause != NULL) { 96 cmdq_error(item, "lines %s", cause); 97 free(cause); 98 return (CMD_RETURN_ERROR); 99 } 100 } 101 } else if (args_has(args, 'p')) { 102 percentage = args_strtonum(args, 'p', 0, INT_MAX, &cause); 103 if (cause != NULL) { 104 cmdq_error(item, "create pane failed: -p %s", cause); 105 free(cause); 106 return (CMD_RETURN_ERROR); 107 } 108 if (type == LAYOUT_TOPBOTTOM) 109 size = (wp->sy * percentage) / 100; 110 else 111 size = (wp->sx * percentage) / 100; 112 } else 113 size = -1; 114 115 window_push_zoom(wp->window, 1, args_has(args, 'Z')); 116 input = (args_has(args, 'I') && count == 0); 117 118 flags = 0; 119 if (args_has(args, 'b')) 120 flags |= SPAWN_BEFORE; 121 if (args_has(args, 'f')) 122 flags |= SPAWN_FULLSIZE; 123 if (input || (count == 1 && *args_string(args, 0) == '\0')) 124 flags |= SPAWN_EMPTY; 125 126 lc = layout_split_pane(wp, type, size, flags); 127 if (lc == NULL) { 128 cmdq_error(item, "no space for new pane"); 129 return (CMD_RETURN_ERROR); 130 } 131 132 sc.item = item; 133 sc.s = s; 134 sc.wl = wl; 135 136 sc.wp0 = wp; 137 sc.lc = lc; 138 139 sc.name = NULL; 140 args_vector(args, &sc.argc, &sc.argv); 141 sc.environ = environ_create(); 142 143 av = args_first_value(args, 'e'); 144 while (av != NULL) { 145 environ_put(sc.environ, av->value, 0); 146 av = args_next_value(av); 147 } 148 149 sc.idx = -1; 150 sc.cwd = args_get(args, 'c'); 151 152 sc.flags = flags; 153 if (args_has(args, 'd')) 154 sc.flags |= SPAWN_DETACHED; 155 if (args_has(args, 'Z')) 156 sc.flags |= SPAWN_ZOOM; 157 158 if ((new_wp = spawn_pane(&sc, &cause)) == NULL) { 159 cmdq_error(item, "create pane failed: %s", cause); 160 free(cause); 161 if (sc.argv != NULL) 162 cmd_free_argv(sc.argc, sc.argv); 163 environ_free(sc.environ); 164 return (CMD_RETURN_ERROR); 165 } 166 if (input && window_pane_start_input(new_wp, item, &cause) != 0) { 167 server_client_remove_pane(new_wp); 168 layout_close_pane(new_wp); 169 window_remove_pane(wp->window, new_wp); 170 cmdq_error(item, "%s", cause); 171 free(cause); 172 if (sc.argv != NULL) 173 cmd_free_argv(sc.argc, sc.argv); 174 environ_free(sc.environ); 175 return (CMD_RETURN_ERROR); 176 } 177 if (!args_has(args, 'd')) 178 cmd_find_from_winlink_pane(current, wl, new_wp, 0); 179 window_pop_zoom(wp->window); 180 server_redraw_window(wp->window); 181 server_status_session(s); 182 183 if (args_has(args, 'P')) { 184 if ((template = args_get(args, 'F')) == NULL) 185 template = SPLIT_WINDOW_TEMPLATE; 186 cp = format_single(item, template, tc, s, wl, new_wp); 187 cmdq_print(item, "%s", cp); 188 free(cp); 189 } 190 191 cmd_find_from_winlink_pane(&fs, wl, new_wp, 0); 192 cmdq_insert_hook(s, item, &fs, "after-split-window"); 193 194 if (sc.argv != NULL) 195 cmd_free_argv(sc.argc, sc.argv); 196 environ_free(sc.environ); 197 if (input) 198 return (CMD_RETURN_WAIT); 199 return (CMD_RETURN_NORMAL); 200 } 201