Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 150 lines (128 sloc) 3.938 kb
0f2e336 Romain Francoise Import upstream snapshot from SVN r2670
orebokech authored
1 /* $Id: cmd-split-window.c 2664 2012-01-20 21:21:32Z tcunha $ */
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
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
7216c39 Karl Ferdinand Ebert Imported Upstream version 1.5
kfebert authored
30 void cmd_split_window_key_binding(struct cmd *, int);
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
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",
7216c39 Karl Ferdinand Ebert Imported Upstream version 1.5
kfebert authored
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
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
41 };
42
43 void
7216c39 Karl Ferdinand Ebert Imported Upstream version 1.5
kfebert authored
44 cmd_split_window_key_binding(struct cmd *self, int key)
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
45 {
7216c39 Karl Ferdinand Ebert Imported Upstream version 1.5
kfebert authored
46 self->args = args_create(0);
47 if (key == '%')
48 args_set(self->args, 'h', NULL);
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
49 }
50
51 int
52 cmd_split_window_exec(struct cmd *self, struct cmd_ctx *ctx)
53 {
7216c39 Karl Ferdinand Ebert Imported Upstream version 1.5
kfebert authored
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;
0f2e336 Romain Francoise Import upstream snapshot from SVN r2670
orebokech authored
60 const char *cmd, *cwd, *shell;
61 char *cause, *new_cause;
7216c39 Karl Ferdinand Ebert Imported Upstream version 1.5
kfebert authored
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)
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
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
7216c39 Karl Ferdinand Ebert Imported Upstream version 1.5
kfebert authored
76 if (args->argc == 0)
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
77 cmd = options_get_string(&s->options, "default-command");
7216c39 Karl Ferdinand Ebert Imported Upstream version 1.5
kfebert authored
78 else
79 cmd = args->argv[0];
4ecaa51 Romain Francoise Import upstream snapshot from SVN r2647
orebokech authored
80 cwd = cmd_get_default_path(ctx);
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
81
dda8e8e Karl Ferdinand Ebert Imported Upstream version 1.2
kfebert authored
82 type = LAYOUT_TOPBOTTOM;
7216c39 Karl Ferdinand Ebert Imported Upstream version 1.5
kfebert authored
83 if (args_has(args, 'h'))
dda8e8e Karl Ferdinand Ebert Imported Upstream version 1.2
kfebert authored
84 type = LAYOUT_LEFTRIGHT;
85
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
86 size = -1;
7216c39 Karl Ferdinand Ebert Imported Upstream version 1.5
kfebert authored
87 if (args_has(args, 'l')) {
88 size = args_strtonum(args, 'l', 0, INT_MAX, &cause);
89 if (cause != NULL) {
b016948 Romain Francoise Import upstream snapshot from SVN r2608
orebokech authored
90 xasprintf(&new_cause, "size %s", cause);
7216c39 Karl Ferdinand Ebert Imported Upstream version 1.5
kfebert authored
91 xfree(cause);
b016948 Romain Francoise Import upstream snapshot from SVN r2608
orebokech authored
92 cause = new_cause;
93 goto error;
7216c39 Karl Ferdinand Ebert Imported Upstream version 1.5
kfebert authored
94 }
95 } else if (args_has(args, 'p')) {
96 percentage = args_strtonum(args, 'p', 0, INT_MAX, &cause);
97 if (cause != NULL) {
b016948 Romain Francoise Import upstream snapshot from SVN r2608
orebokech authored
98 xasprintf(&new_cause, "percentage %s", cause);
7216c39 Karl Ferdinand Ebert Imported Upstream version 1.5
kfebert authored
99 xfree(cause);
b016948 Romain Francoise Import upstream snapshot from SVN r2608
orebokech authored
100 cause = new_cause;
101 goto error;
7216c39 Karl Ferdinand Ebert Imported Upstream version 1.5
kfebert authored
102 }
dda8e8e Karl Ferdinand Ebert Imported Upstream version 1.2
kfebert authored
103 if (type == LAYOUT_TOPBOTTOM)
7216c39 Karl Ferdinand Ebert Imported Upstream version 1.5
kfebert authored
104 size = (wp->sy * percentage) / 100;
dda8e8e Karl Ferdinand Ebert Imported Upstream version 1.2
kfebert authored
105 else
7216c39 Karl Ferdinand Ebert Imported Upstream version 1.5
kfebert authored
106 size = (wp->sx * percentage) / 100;
dda8e8e Karl Ferdinand Ebert Imported Upstream version 1.2
kfebert authored
107 }
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
108 hlimit = options_get_number(&s->options, "history-limit");
109
110 shell = options_get_string(&s->options, "default-shell");
111 if (*shell == '\0' || areshell(shell))
112 shell = _PATH_BSHELL;
113
dda8e8e Karl Ferdinand Ebert Imported Upstream version 1.2
kfebert authored
114 if ((lc = layout_split_pane(wp, type, size)) == NULL) {
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
115 cause = xstrdup("pane too small");
116 goto error;
117 }
dda8e8e Karl Ferdinand Ebert Imported Upstream version 1.2
kfebert authored
118 new_wp = window_add_pane(w, hlimit);
119 if (window_pane_spawn(
120 new_wp, cmd, shell, cwd, &env, s->tio, &cause) != 0)
121 goto error;
122 layout_assign_pane(lc, new_wp);
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
123
124 server_redraw_window(w);
125
7216c39 Karl Ferdinand Ebert Imported Upstream version 1.5
kfebert authored
126 if (!args_has(args, 'd')) {
dda8e8e Karl Ferdinand Ebert Imported Upstream version 1.2
kfebert authored
127 window_set_active_pane(w, new_wp);
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
128 session_select(s, wl->idx);
129 server_redraw_session(s);
130 } else
131 server_status_session(s);
132
133 environ_free(&env);
7216c39 Karl Ferdinand Ebert Imported Upstream version 1.5
kfebert authored
134
135 if (args_has(args, 'P')) {
d873474 Romain Francoise Import upstream snapshot from SVN r2642
orebokech authored
136 if (window_pane_index(new_wp, &paneidx) != 0)
137 fatalx("index not found");
7216c39 Karl Ferdinand Ebert Imported Upstream version 1.5
kfebert authored
138 ctx->print(ctx, "%s:%u.%u", s->name, wl->idx, paneidx);
139 }
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
140 return (0);
141
142 error:
143 environ_free(&env);
dda8e8e Karl Ferdinand Ebert Imported Upstream version 1.2
kfebert authored
144 if (new_wp != NULL)
145 window_remove_pane(w, new_wp);
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
146 ctx->error(ctx, "create pane failed: %s", cause);
147 xfree(cause);
148 return (-1);
149 }
Something went wrong with that request. Please try again.