xref: /netbsd-src/external/bsd/tmux/dist/cmd-split-window.c (revision 5bbd2a12505d72a8177929a37b5cee489d0a1cfd)
1 /* $Id: cmd-split-window.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 #include <unistd.h>
23 
24 #include "tmux.h"
25 
26 /*
27  * Split a window (add a new pane).
28  */
29 
30 void	cmd_split_window_key_binding(struct cmd *, int);
31 int	cmd_split_window_exec(struct cmd *, struct cmd_ctx *);
32 
33 const struct cmd_entry cmd_split_window_entry = {
34 	"split-window", "splitw",
35 	"dl:hp:Pt:v", 0, 1,
36 	"[-dhvP] [-p percentage|-l size] [-t target-pane] [command]",
37 	0,
38 	cmd_split_window_key_binding,
39 	NULL,
40 	cmd_split_window_exec
41 };
42 
43 void
44 cmd_split_window_key_binding(struct cmd *self, int key)
45 {
46 	self->args = args_create(0);
47 	if (key == '%')
48 		args_set(self->args, 'h', NULL);
49 }
50 
51 int
52 cmd_split_window_exec(struct cmd *self, struct cmd_ctx *ctx)
53 {
54 	struct args		*args = self->args;
55 	struct session		*s;
56 	struct winlink		*wl;
57 	struct window		*w;
58 	struct window_pane	*wp, *new_wp = NULL;
59 	struct environ		 env;
60 	char		 	*cmd, *cwd, *cause;
61 	const char		*shell;
62 	u_int			 hlimit, paneidx;
63 	int			 size, percentage;
64 	enum layout_type	 type;
65 	struct layout_cell	*lc;
66 
67 	if ((wl = cmd_find_pane(ctx, args_get(args, 't'), &s, &wp)) == NULL)
68 		return (-1);
69 	w = wl->window;
70 
71 	environ_init(&env);
72 	environ_copy(&global_environ, &env);
73 	environ_copy(&s->environ, &env);
74 	server_fill_environ(s, &env);
75 
76 	if (args->argc == 0)
77 		cmd = options_get_string(&s->options, "default-command");
78 	else
79 		cmd = args->argv[0];
80 	cwd = options_get_string(&s->options, "default-path");
81 	if (*cwd == '\0') {
82 		if (ctx->cmdclient != NULL && ctx->cmdclient->cwd != NULL)
83 			cwd = ctx->cmdclient->cwd;
84 		else
85 			cwd = s->cwd;
86 	}
87 
88 	type = LAYOUT_TOPBOTTOM;
89 	if (args_has(args, 'h'))
90 		type = LAYOUT_LEFTRIGHT;
91 
92 	size = -1;
93 	if (args_has(args, 'l')) {
94 		size = args_strtonum(args, 'l', 0, INT_MAX, &cause);
95 		if (cause != NULL) {
96 			ctx->error(ctx, "size %s", cause);
97 			xfree(cause);
98 			return (-1);
99 		}
100 	} else if (args_has(args, 'p')) {
101 		percentage = args_strtonum(args, 'p', 0, INT_MAX, &cause);
102 		if (cause != NULL) {
103 			ctx->error(ctx, "percentage %s", cause);
104 			xfree(cause);
105 			return (-1);
106 		}
107 		if (type == LAYOUT_TOPBOTTOM)
108 			size = (wp->sy * percentage) / 100;
109 		else
110 			size = (wp->sx * percentage) / 100;
111 	}
112 	hlimit = options_get_number(&s->options, "history-limit");
113 
114 	shell = options_get_string(&s->options, "default-shell");
115 	if (*shell == '\0' || areshell(shell))
116 		shell = _PATH_BSHELL;
117 
118 	if ((lc = layout_split_pane(wp, type, size)) == NULL) {
119 		cause = xstrdup("pane too small");
120 		goto error;
121 	}
122 	new_wp = window_add_pane(w, hlimit);
123 	if (window_pane_spawn(
124 	    new_wp, cmd, shell, cwd, &env, s->tio, &cause) != 0)
125 		goto error;
126 	layout_assign_pane(lc, new_wp);
127 
128 	server_redraw_window(w);
129 
130 	if (!args_has(args, 'd')) {
131 		window_set_active_pane(w, new_wp);
132 		session_select(s, wl->idx);
133 		server_redraw_session(s);
134 	} else
135 		server_status_session(s);
136 
137 	environ_free(&env);
138 
139 	if (args_has(args, 'P')) {
140 		paneidx = window_pane_index(wl->window, new_wp);
141 		ctx->print(ctx, "%s:%u.%u", s->name, wl->idx, paneidx);
142 	}
143 	return (0);
144 
145 error:
146 	environ_free(&env);
147 	if (new_wp != NULL)
148 		window_remove_pane(w, new_wp);
149 	ctx->error(ctx, "create pane failed: %s", cause);
150 	xfree(cause);
151 	return (-1);
152 }
153