cmd-split-window.c revision 1.8 1 /* $OpenBSD$ */
2
3 /*
4 * Copyright (c) 2009 Nicholas Marriott <nicholas.marriott (at) 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 <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
26
27 #include "tmux.h"
28
29 /*
30 * Split a window (add a new pane).
31 */
32
33 #define SPLIT_WINDOW_TEMPLATE "#{session_name}:#{window_index}.#{pane_index}"
34
35 static enum cmd_retval cmd_split_window_exec(struct cmd *,
36 struct cmdq_item *);
37
38 const struct cmd_entry cmd_split_window_entry = {
39 .name = "split-window",
40 .alias = "splitw",
41
42 .args = { "bc:de:fF:hIl:p:Pt:v", 0, -1 },
43 .usage = "[-bdefhIPv] [-c start-directory] [-e environment] "
44 "[-F format] [-p percentage|-l size] " CMD_TARGET_PANE_USAGE
45 " [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, input;
67 const char *template, *add;
68 char *cause, *cp;
69 struct args_value *value;
70
71 if (args_has(args, 'h'))
72 type = LAYOUT_LEFTRIGHT;
73 else
74 type = LAYOUT_TOPBOTTOM;
75 if (args_has(args, 'l')) {
76 size = args_strtonum(args, 'l', 0, INT_MAX, &cause);
77 if (cause != NULL) {
78 cmdq_error(item, "create pane failed: -l %s", cause);
79 free(cause);
80 return (CMD_RETURN_ERROR);
81 }
82 } else if (args_has(args, 'p')) {
83 percentage = args_strtonum(args, 'p', 0, INT_MAX, &cause);
84 if (cause != NULL) {
85 cmdq_error(item, "create pane failed: -p %s", cause);
86 free(cause);
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 = -1;
95
96 server_unzoom_window(wp->window);
97 input = (args_has(args, 'I') && args->argc == 0);
98
99 flags = 0;
100 if (args_has(args, 'b'))
101 flags |= SPAWN_BEFORE;
102 if (args_has(args, 'f'))
103 flags |= SPAWN_FULLSIZE;
104 if (input || (args->argc == 1 && *args->argv[0] == '\0'))
105 flags |= SPAWN_EMPTY;
106
107 lc = layout_split_pane(wp, type, size, flags);
108 if (lc == NULL) {
109 cmdq_error(item, "no space for new pane");
110 return (CMD_RETURN_ERROR);
111 }
112
113 memset(&sc, 0, sizeof sc);
114 sc.item = item;
115 sc.s = s;
116 sc.wl = wl;
117
118 sc.wp0 = wp;
119 sc.lc = lc;
120
121 sc.name = NULL;
122 sc.argc = args->argc;
123 sc.argv = args->argv;
124 sc.environ = environ_create();
125
126 add = args_first_value(args, 'e', &value);
127 while (add != NULL) {
128 environ_put(sc.environ, add);
129 add = args_next_value(&value);
130 }
131
132 sc.idx = -1;
133 sc.cwd = args_get(args, 'c');
134
135 sc.flags = flags;
136 if (args_has(args, 'd'))
137 sc.flags |= SPAWN_DETACHED;
138
139 if ((new_wp = spawn_pane(&sc, &cause)) == NULL) {
140 layout_close_pane(new_wp);
141 cmdq_error(item, "create pane failed: %s", cause);
142 free(cause);
143 return (CMD_RETURN_ERROR);
144 }
145 if (input && window_pane_start_input(new_wp, item, &cause) != 0) {
146 layout_close_pane(new_wp);
147 window_remove_pane(wp->window, new_wp);
148 cmdq_error(item, "%s", cause);
149 free(cause);
150 return (CMD_RETURN_ERROR);
151 }
152 if (!args_has(args, 'd'))
153 cmd_find_from_winlink_pane(current, wl, new_wp, 0);
154 server_redraw_window(wp->window);
155 server_status_session(s);
156
157 if (args_has(args, 'P')) {
158 if ((template = args_get(args, 'F')) == NULL)
159 template = SPLIT_WINDOW_TEMPLATE;
160 cp = format_single(item, template, c, s, wl, new_wp);
161 cmdq_print(item, "%s", cp);
162 free(cp);
163 }
164
165 cmd_find_from_winlink_pane(&fs, wl, new_wp, 0);
166 cmdq_insert_hook(s, item, &fs, "after-split-window");
167
168 environ_free(sc.environ);
169 if (input)
170 return (CMD_RETURN_WAIT);
171 return (CMD_RETURN_NORMAL);
172 }
173