2009-06-01 22:58:49 +00:00
|
|
|
/* $OpenBSD$ */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2009 Nicholas Marriott <nicm@users.sourceforge.net>
|
|
|
|
*
|
|
|
|
* 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>
|
|
|
|
|
2009-09-01 13:09:49 +00:00
|
|
|
#include <paths.h>
|
2009-06-01 22:58:49 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "tmux.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Split a window (add a new pane).
|
|
|
|
*/
|
|
|
|
|
2012-07-11 07:10:15 +00:00
|
|
|
void cmd_split_window_key_binding(struct cmd *, int);
|
2013-03-24 09:54:10 +00:00
|
|
|
enum cmd_retval cmd_split_window_exec(struct cmd *, struct cmd_q *);
|
2009-06-01 22:58:49 +00:00
|
|
|
|
|
|
|
const struct cmd_entry cmd_split_window_entry = {
|
|
|
|
"split-window", "splitw",
|
2012-03-04 20:50:53 +00:00
|
|
|
"c:dF:l:hp:Pt:v", 0, 1,
|
|
|
|
"[-dhvP] [-c start-directory] [-F format] [-p percentage|-l size] "
|
2012-12-09 23:17:35 +00:00
|
|
|
CMD_TARGET_PANE_USAGE " [command]",
|
2011-01-04 00:42:46 +00:00
|
|
|
0,
|
|
|
|
cmd_split_window_key_binding,
|
|
|
|
NULL,
|
|
|
|
cmd_split_window_exec
|
2009-06-01 22:58:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
void
|
2011-01-04 00:42:46 +00:00
|
|
|
cmd_split_window_key_binding(struct cmd *self, int key)
|
2009-06-01 22:58:49 +00:00
|
|
|
{
|
2011-01-04 00:42:46 +00:00
|
|
|
self->args = args_create(0);
|
|
|
|
if (key == '%')
|
|
|
|
args_set(self->args, 'h', NULL);
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|
|
|
|
|
2012-07-11 07:10:15 +00:00
|
|
|
enum cmd_retval
|
2013-03-24 09:54:10 +00:00
|
|
|
cmd_split_window_exec(struct cmd *self, struct cmd_q *cmdq)
|
2009-06-01 22:58:49 +00:00
|
|
|
{
|
2011-01-04 00:42:46 +00:00
|
|
|
struct args *args = self->args;
|
|
|
|
struct session *s;
|
|
|
|
struct winlink *wl;
|
|
|
|
struct window *w;
|
|
|
|
struct window_pane *wp, *new_wp = NULL;
|
|
|
|
struct environ env;
|
2013-03-24 09:27:19 +00:00
|
|
|
const char *cmd, *cwd, *shell, *prefix;
|
|
|
|
char *cause, *new_cause, *cmd1;
|
2012-03-04 20:50:53 +00:00
|
|
|
u_int hlimit;
|
2011-01-04 00:42:46 +00:00
|
|
|
int size, percentage;
|
|
|
|
enum layout_type type;
|
|
|
|
struct layout_cell *lc;
|
2012-03-04 20:50:53 +00:00
|
|
|
const char *template;
|
|
|
|
struct client *c;
|
|
|
|
struct format_tree *ft;
|
|
|
|
char *cp;
|
2011-01-04 00:42:46 +00:00
|
|
|
|
2013-03-24 09:54:10 +00:00
|
|
|
if ((wl = cmd_find_pane(cmdq, args_get(args, 't'), &s, &wp)) == NULL)
|
2012-07-11 07:10:15 +00:00
|
|
|
return (CMD_RETURN_ERROR);
|
2009-06-01 22:58:49 +00:00
|
|
|
w = wl->window;
|
|
|
|
|
2009-08-08 21:52:43 +00:00
|
|
|
environ_init(&env);
|
|
|
|
environ_copy(&global_environ, &env);
|
|
|
|
environ_copy(&s->environ, &env);
|
|
|
|
server_fill_environ(s, &env);
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2011-01-04 00:42:46 +00:00
|
|
|
if (args->argc == 0)
|
2009-06-01 22:58:49 +00:00
|
|
|
cmd = options_get_string(&s->options, "default-command");
|
2011-01-04 00:42:46 +00:00
|
|
|
else
|
|
|
|
cmd = args->argv[0];
|
2013-03-24 09:54:10 +00:00
|
|
|
cwd = cmd_get_default_path(cmdq, args_get(args, 'c'));
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2009-11-20 19:12:39 +00:00
|
|
|
type = LAYOUT_TOPBOTTOM;
|
2011-01-04 00:42:46 +00:00
|
|
|
if (args_has(args, 'h'))
|
2009-11-20 19:12:39 +00:00
|
|
|
type = LAYOUT_LEFTRIGHT;
|
|
|
|
|
2009-07-19 13:21:40 +00:00
|
|
|
size = -1;
|
2011-02-10 12:12:14 +00:00
|
|
|
if (args_has(args, 'l')) {
|
|
|
|
size = args_strtonum(args, 'l', 0, INT_MAX, &cause);
|
2011-01-04 00:42:46 +00:00
|
|
|
if (cause != NULL) {
|
2011-08-30 09:18:52 +00:00
|
|
|
xasprintf(&new_cause, "size %s", cause);
|
2012-07-10 11:53:01 +00:00
|
|
|
free(cause);
|
2011-08-30 09:18:52 +00:00
|
|
|
cause = new_cause;
|
|
|
|
goto error;
|
2011-01-04 00:42:46 +00:00
|
|
|
}
|
|
|
|
} else if (args_has(args, 'p')) {
|
|
|
|
percentage = args_strtonum(args, 'p', 0, INT_MAX, &cause);
|
|
|
|
if (cause != NULL) {
|
2011-08-30 09:18:52 +00:00
|
|
|
xasprintf(&new_cause, "percentage %s", cause);
|
2012-07-10 11:53:01 +00:00
|
|
|
free(cause);
|
2011-08-30 09:18:52 +00:00
|
|
|
cause = new_cause;
|
|
|
|
goto error;
|
2011-01-04 00:42:46 +00:00
|
|
|
}
|
2009-11-20 19:12:39 +00:00
|
|
|
if (type == LAYOUT_TOPBOTTOM)
|
2011-01-04 00:42:46 +00:00
|
|
|
size = (wp->sy * percentage) / 100;
|
2009-11-20 19:12:39 +00:00
|
|
|
else
|
2011-01-04 00:42:46 +00:00
|
|
|
size = (wp->sx * percentage) / 100;
|
2009-11-20 19:12:39 +00:00
|
|
|
}
|
2009-06-01 22:58:49 +00:00
|
|
|
hlimit = options_get_number(&s->options, "history-limit");
|
2009-07-19 13:21:40 +00:00
|
|
|
|
2009-09-01 13:09:49 +00:00
|
|
|
shell = options_get_string(&s->options, "default-shell");
|
|
|
|
if (*shell == '\0' || areshell(shell))
|
|
|
|
shell = _PATH_BSHELL;
|
|
|
|
|
2012-03-03 08:31:18 +00:00
|
|
|
if ((lc = layout_split_pane(wp, type, size, 0)) == NULL) {
|
2009-07-19 13:21:40 +00:00
|
|
|
cause = xstrdup("pane too small");
|
|
|
|
goto error;
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|
2010-01-07 20:52:18 +00:00
|
|
|
new_wp = window_add_pane(w, hlimit);
|
2013-03-24 09:27:19 +00:00
|
|
|
|
|
|
|
if (*cmd != '\0') {
|
|
|
|
prefix = options_get_string(&w->options, "command-prefix");
|
|
|
|
xasprintf(&cmd1, "%s%s", prefix, cmd);
|
|
|
|
} else
|
|
|
|
cmd1 = xstrdup("");
|
|
|
|
if (window_pane_spawn(new_wp, cmd1, shell, cwd, &env, s->tio,
|
|
|
|
&cause) != 0) {
|
|
|
|
free(cmd1);
|
2010-01-07 20:52:18 +00:00
|
|
|
goto error;
|
2013-03-24 09:27:19 +00:00
|
|
|
}
|
|
|
|
free(cmd1);
|
2010-01-07 20:52:18 +00:00
|
|
|
layout_assign_pane(lc, new_wp);
|
2009-07-19 13:21:40 +00:00
|
|
|
|
2009-06-01 22:58:49 +00:00
|
|
|
server_redraw_window(w);
|
|
|
|
|
2011-01-04 00:42:46 +00:00
|
|
|
if (!args_has(args, 'd')) {
|
2010-01-07 19:47:10 +00:00
|
|
|
window_set_active_pane(w, new_wp);
|
2009-06-01 22:58:49 +00:00
|
|
|
session_select(s, wl->idx);
|
|
|
|
server_redraw_session(s);
|
|
|
|
} else
|
|
|
|
server_status_session(s);
|
|
|
|
|
2009-08-08 21:52:43 +00:00
|
|
|
environ_free(&env);
|
2011-01-01 01:33:07 +00:00
|
|
|
|
2011-01-04 00:42:46 +00:00
|
|
|
if (args_has(args, 'P')) {
|
2012-05-22 11:35:37 +00:00
|
|
|
if ((template = args_get(args, 'F')) == NULL)
|
2012-08-14 08:51:53 +00:00
|
|
|
template = SPLIT_WINDOW_TEMPLATE;
|
2012-03-04 20:50:53 +00:00
|
|
|
|
|
|
|
ft = format_create();
|
2013-03-24 09:54:10 +00:00
|
|
|
if ((c = cmd_find_client(cmdq, NULL, 1)) != NULL)
|
|
|
|
format_client(ft, c);
|
2012-03-04 20:50:53 +00:00
|
|
|
format_session(ft, s);
|
|
|
|
format_winlink(ft, s, wl);
|
|
|
|
format_window_pane(ft, new_wp);
|
|
|
|
|
|
|
|
cp = format_expand(ft, template);
|
2013-03-24 09:54:10 +00:00
|
|
|
cmdq_print(cmdq, "%s", cp);
|
2012-07-10 11:53:01 +00:00
|
|
|
free(cp);
|
2012-03-04 20:50:53 +00:00
|
|
|
|
|
|
|
format_free(ft);
|
2011-01-01 01:33:07 +00:00
|
|
|
}
|
2012-03-17 22:35:09 +00:00
|
|
|
notify_window_layout_changed(w);
|
2012-07-11 07:10:15 +00:00
|
|
|
return (CMD_RETURN_NORMAL);
|
2009-07-19 13:21:40 +00:00
|
|
|
|
|
|
|
error:
|
2009-08-08 21:52:43 +00:00
|
|
|
environ_free(&env);
|
2010-01-07 19:47:10 +00:00
|
|
|
if (new_wp != NULL)
|
|
|
|
window_remove_pane(w, new_wp);
|
2013-03-24 09:54:10 +00:00
|
|
|
cmdq_error(cmdq, "create pane failed: %s", cause);
|
2012-07-10 11:53:01 +00:00
|
|
|
free(cause);
|
2012-07-11 07:10:15 +00:00
|
|
|
return (CMD_RETURN_ERROR);
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|