2014-11-08 12:27:43 +00:00
|
|
|
/* $OpenBSD$ */
|
2009-01-11 23:31:46 +00:00
|
|
|
|
|
|
|
/*
|
2016-01-19 15:59:12 +00:00
|
|
|
* Copyright (c) 2009 Nicholas Marriott <nicholas.marriott@gmail.com>
|
2009-01-11 23:31:46 +00:00
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
|
|
|
|
* IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
2013-10-06 20:02:23 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
2009-01-11 23:31:46 +00:00
|
|
|
#include <stdlib.h>
|
2013-10-06 20:02:23 +00:00
|
|
|
#include <string.h>
|
2009-01-11 23:31:46 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "tmux.h"
|
|
|
|
|
|
|
|
/*
|
2009-04-03 17:21:46 +00:00
|
|
|
* Split a window (add a new pane).
|
2009-01-11 23:31:46 +00:00
|
|
|
*/
|
|
|
|
|
2014-10-20 23:35:28 +00:00
|
|
|
#define SPLIT_WINDOW_TEMPLATE "#{session_name}:#{window_index}.#{pane_index}"
|
|
|
|
|
2016-10-10 21:51:39 +00:00
|
|
|
static enum cmd_retval cmd_split_window_exec(struct cmd *, struct cmd_q *);
|
2009-01-11 23:31:46 +00:00
|
|
|
|
|
|
|
const struct cmd_entry cmd_split_window_entry = {
|
2015-12-13 21:53:57 +00:00
|
|
|
.name = "split-window",
|
|
|
|
.alias = "splitw",
|
|
|
|
|
2016-09-04 17:37:06 +00:00
|
|
|
.args = { "bc:dfF:l:hp:Pt:v", 0, -1 },
|
|
|
|
.usage = "[-bdfhvP] [-c start-directory] [-F format] "
|
2015-12-13 21:53:57 +00:00
|
|
|
"[-p percentage|-l size] " CMD_TARGET_PANE_USAGE " [command]",
|
|
|
|
|
2015-12-14 00:31:54 +00:00
|
|
|
.tflag = CMD_PANE,
|
|
|
|
|
|
|
|
.flags = 0,
|
2015-12-13 21:53:57 +00:00
|
|
|
.exec = cmd_split_window_exec
|
2009-01-11 23:31:46 +00:00
|
|
|
};
|
|
|
|
|
2016-10-10 21:51:39 +00:00
|
|
|
static enum cmd_retval
|
2013-02-23 22:25:58 +00:00
|
|
|
cmd_split_window_exec(struct cmd *self, struct cmd_q *cmdq)
|
2009-01-11 23:31:46 +00:00
|
|
|
{
|
2011-01-07 14:45:34 +00:00
|
|
|
struct args *args = self->args;
|
2015-12-13 14:32:38 +00:00
|
|
|
struct session *s = cmdq->state.tflag.s;
|
|
|
|
struct winlink *wl = cmdq->state.tflag.wl;
|
|
|
|
struct window *w = wl->window;
|
|
|
|
struct window_pane *wp = cmdq->state.tflag.wp, *new_wp = NULL;
|
2015-10-28 09:51:55 +00:00
|
|
|
struct environ *env;
|
2015-10-31 08:13:58 +00:00
|
|
|
const char *cmd, *path, *shell, *template, *cwd, *to_free;
|
2014-05-13 08:08:32 +00:00
|
|
|
char **argv, *cause, *new_cause, *cp;
|
2012-03-07 13:39:29 +00:00
|
|
|
u_int hlimit;
|
2015-10-31 08:13:58 +00:00
|
|
|
int argc, size, percentage;
|
2011-01-07 14:45:34 +00:00
|
|
|
enum layout_type type;
|
|
|
|
struct layout_cell *lc;
|
2012-03-07 13:39:29 +00:00
|
|
|
struct format_tree *ft;
|
2014-04-17 13:02:59 +00:00
|
|
|
struct environ_entry *envent;
|
2016-10-13 22:48:51 +00:00
|
|
|
struct cmd_find_state fs;
|
2011-01-07 14:45:34 +00:00
|
|
|
|
2013-02-24 00:25:03 +00:00
|
|
|
server_unzoom_window(w);
|
2009-01-11 23:31:46 +00:00
|
|
|
|
2015-10-28 09:51:55 +00:00
|
|
|
env = environ_create();
|
|
|
|
environ_copy(global_environ, env);
|
|
|
|
environ_copy(s->environ, env);
|
|
|
|
server_fill_environ(s, env);
|
2009-01-11 23:31:46 +00:00
|
|
|
|
2014-05-13 08:08:32 +00:00
|
|
|
if (args->argc == 0) {
|
2015-10-27 15:58:42 +00:00
|
|
|
cmd = options_get_string(s->options, "default-command");
|
2014-05-13 08:08:32 +00:00
|
|
|
if (cmd != NULL && *cmd != '\0') {
|
|
|
|
argc = 1;
|
2014-09-01 21:50:18 +00:00
|
|
|
argv = (char **)&cmd;
|
2014-05-13 08:08:32 +00:00
|
|
|
} else {
|
|
|
|
argc = 0;
|
|
|
|
argv = NULL;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
argc = args->argc;
|
|
|
|
argv = args->argv;
|
|
|
|
}
|
2013-10-06 20:02:23 +00:00
|
|
|
|
2015-10-31 08:13:58 +00:00
|
|
|
to_free = NULL;
|
2013-10-06 20:02:23 +00:00
|
|
|
if (args_has(args, 'c')) {
|
2015-12-11 12:27:36 +00:00
|
|
|
ft = format_create(cmdq, 0);
|
2015-12-13 14:32:38 +00:00
|
|
|
format_defaults(ft, cmdq->state.c, s, NULL, NULL);
|
2015-10-31 08:13:58 +00:00
|
|
|
to_free = cwd = format_expand(ft, args_get(args, 'c'));
|
2013-10-06 20:02:23 +00:00
|
|
|
format_free(ft);
|
2013-10-06 20:31:55 +00:00
|
|
|
} else if (cmdq->client != NULL && cmdq->client->session == NULL)
|
2013-10-06 20:02:23 +00:00
|
|
|
cwd = cmdq->client->cwd;
|
|
|
|
else
|
|
|
|
cwd = s->cwd;
|
2009-01-11 23:31:46 +00:00
|
|
|
|
2009-11-22 00:12:33 +00:00
|
|
|
type = LAYOUT_TOPBOTTOM;
|
2011-01-07 14:45:34 +00:00
|
|
|
if (args_has(args, 'h'))
|
2009-11-22 00:12:33 +00:00
|
|
|
type = LAYOUT_LEFTRIGHT;
|
|
|
|
|
2009-07-20 15:42:05 +00:00
|
|
|
size = -1;
|
2011-02-15 15:26:54 +00:00
|
|
|
if (args_has(args, 'l')) {
|
|
|
|
size = args_strtonum(args, 'l', 0, INT_MAX, &cause);
|
2011-01-07 14:45:34 +00:00
|
|
|
if (cause != NULL) {
|
2011-09-21 16:34:04 +00:00
|
|
|
xasprintf(&new_cause, "size %s", cause);
|
2012-07-11 19:34:16 +00:00
|
|
|
free(cause);
|
2011-09-21 16:34:04 +00:00
|
|
|
cause = new_cause;
|
|
|
|
goto error;
|
2011-01-07 14:45:34 +00:00
|
|
|
}
|
|
|
|
} else if (args_has(args, 'p')) {
|
|
|
|
percentage = args_strtonum(args, 'p', 0, INT_MAX, &cause);
|
|
|
|
if (cause != NULL) {
|
2011-09-21 16:34:04 +00:00
|
|
|
xasprintf(&new_cause, "percentage %s", cause);
|
2012-07-11 19:34:16 +00:00
|
|
|
free(cause);
|
2011-09-21 16:34:04 +00:00
|
|
|
cause = new_cause;
|
|
|
|
goto error;
|
2011-01-07 14:45:34 +00:00
|
|
|
}
|
2009-11-22 00:12:33 +00:00
|
|
|
if (type == LAYOUT_TOPBOTTOM)
|
2011-01-07 14:45:34 +00:00
|
|
|
size = (wp->sy * percentage) / 100;
|
2009-11-22 00:12:33 +00:00
|
|
|
else
|
2011-01-07 14:45:34 +00:00
|
|
|
size = (wp->sx * percentage) / 100;
|
2009-11-22 00:12:33 +00:00
|
|
|
}
|
2015-10-27 15:58:42 +00:00
|
|
|
hlimit = options_get_number(s->options, "history-limit");
|
2009-07-20 15:42:05 +00:00
|
|
|
|
2015-10-27 15:58:42 +00:00
|
|
|
shell = options_get_string(s->options, "default-shell");
|
2009-09-02 01:02:44 +00:00
|
|
|
if (*shell == '\0' || areshell(shell))
|
|
|
|
shell = _PATH_BSHELL;
|
|
|
|
|
2016-09-04 17:37:06 +00:00
|
|
|
lc = layout_split_pane(wp, type, size, args_has(args, 'b'),
|
|
|
|
args_has(args, 'f'));
|
2014-11-12 22:57:06 +00:00
|
|
|
if (lc == NULL) {
|
2009-07-20 15:42:05 +00:00
|
|
|
cause = xstrdup("pane too small");
|
|
|
|
goto error;
|
2009-01-11 23:31:46 +00:00
|
|
|
}
|
2016-06-06 07:24:31 +00:00
|
|
|
new_wp = window_add_pane(w, wp, hlimit);
|
2015-04-26 20:25:20 +00:00
|
|
|
layout_assign_pane(lc, new_wp);
|
2014-04-17 13:02:59 +00:00
|
|
|
|
|
|
|
path = NULL;
|
|
|
|
if (cmdq->client != NULL && cmdq->client->session == NULL)
|
2015-10-28 09:51:55 +00:00
|
|
|
envent = environ_find(cmdq->client->environ, "PATH");
|
2014-04-17 13:02:59 +00:00
|
|
|
else
|
2015-10-28 09:51:55 +00:00
|
|
|
envent = environ_find(s->environ, "PATH");
|
2014-04-17 13:02:59 +00:00
|
|
|
if (envent != NULL)
|
|
|
|
path = envent->value;
|
|
|
|
|
2015-10-28 09:51:55 +00:00
|
|
|
if (window_pane_spawn(new_wp, argc, argv, path, shell, cwd, env,
|
2014-05-13 08:08:32 +00:00
|
|
|
s->tio, &cause) != 0)
|
2010-01-08 16:31:35 +00:00
|
|
|
goto error;
|
2009-07-20 15:42:05 +00:00
|
|
|
|
2009-01-12 18:22:47 +00:00
|
|
|
server_redraw_window(w);
|
2009-05-04 17:58:27 +00:00
|
|
|
|
2011-01-07 14:45:34 +00:00
|
|
|
if (!args_has(args, 'd')) {
|
2010-01-08 16:23:38 +00:00
|
|
|
window_set_active_pane(w, new_wp);
|
2009-01-11 23:31:46 +00:00
|
|
|
session_select(s, wl->idx);
|
|
|
|
server_redraw_session(s);
|
|
|
|
} else
|
2009-01-11 23:41:29 +00:00
|
|
|
server_status_session(s);
|
2009-01-11 23:31:46 +00:00
|
|
|
|
2015-10-28 09:51:55 +00:00
|
|
|
environ_free(env);
|
2011-01-03 23:29:09 +00:00
|
|
|
|
2011-01-07 14:45:34 +00:00
|
|
|
if (args_has(args, 'P')) {
|
2012-05-22 21:03:25 +00:00
|
|
|
if ((template = args_get(args, 'F')) == NULL)
|
2012-08-31 09:18:50 +00:00
|
|
|
template = SPLIT_WINDOW_TEMPLATE;
|
2012-03-07 13:39:29 +00:00
|
|
|
|
2015-12-11 12:27:36 +00:00
|
|
|
ft = format_create(cmdq, 0);
|
2015-12-13 14:32:38 +00:00
|
|
|
format_defaults(ft, cmdq->state.c, s, wl, new_wp);
|
2012-03-07 13:39:29 +00:00
|
|
|
|
|
|
|
cp = format_expand(ft, template);
|
2013-02-23 22:25:58 +00:00
|
|
|
cmdq_print(cmdq, "%s", cp);
|
2012-07-11 19:34:16 +00:00
|
|
|
free(cp);
|
2012-03-07 13:39:29 +00:00
|
|
|
|
|
|
|
format_free(ft);
|
2011-01-03 23:29:09 +00:00
|
|
|
}
|
2012-03-18 02:22:09 +00:00
|
|
|
notify_window_layout_changed(w);
|
2013-10-06 20:02:23 +00:00
|
|
|
|
2015-10-31 08:13:58 +00:00
|
|
|
if (to_free != NULL)
|
|
|
|
free((void *)to_free);
|
2016-10-13 22:48:51 +00:00
|
|
|
|
|
|
|
cmd_find_clear_state(&fs, NULL, 0);
|
|
|
|
fs.s = s;
|
|
|
|
fs.wl = wl;
|
|
|
|
fs.w = w;
|
|
|
|
fs.wp = new_wp;
|
|
|
|
cmd_find_log_state(__func__, &fs);
|
2016-10-16 17:55:14 +00:00
|
|
|
hooks_insert(s->hooks, cmdq, &fs, "after-split-window");
|
|
|
|
|
2012-07-11 19:37:32 +00:00
|
|
|
return (CMD_RETURN_NORMAL);
|
2009-07-20 15:42:05 +00:00
|
|
|
|
|
|
|
error:
|
2015-10-28 09:51:55 +00:00
|
|
|
environ_free(env);
|
2015-04-26 20:25:20 +00:00
|
|
|
if (new_wp != NULL) {
|
|
|
|
layout_close_pane(new_wp);
|
2010-01-08 16:23:38 +00:00
|
|
|
window_remove_pane(w, new_wp);
|
2015-04-26 20:25:20 +00:00
|
|
|
}
|
2013-02-23 22:25:58 +00:00
|
|
|
cmdq_error(cmdq, "create pane failed: %s", cause);
|
2012-07-11 19:34:16 +00:00
|
|
|
free(cause);
|
2015-10-31 08:13:58 +00:00
|
|
|
|
|
|
|
if (to_free != NULL)
|
|
|
|
free((void *)to_free);
|
2012-07-11 19:37:32 +00:00
|
|
|
return (CMD_RETURN_ERROR);
|
2009-01-11 23:31:46 +00:00
|
|
|
}
|