2009-06-01 22:58:49 +00:00
|
|
|
/* $OpenBSD$ */
|
|
|
|
|
|
|
|
/*
|
2016-01-19 15:59:12 +00:00
|
|
|
* Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
|
2009-06-01 22:58:49 +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-10 12:26:34 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
2009-06-01 22:58:49 +00:00
|
|
|
#include <stdlib.h>
|
2013-10-10 12:26:34 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
2009-06-01 22:58:49 +00:00
|
|
|
|
|
|
|
#include "tmux.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create a new window.
|
|
|
|
*/
|
|
|
|
|
2014-10-20 23:35:28 +00:00
|
|
|
#define NEW_WINDOW_TEMPLATE "#{session_name}:#{window_index}.#{pane_index}"
|
|
|
|
|
2016-10-16 19:04:05 +00:00
|
|
|
static enum cmd_retval cmd_new_window_exec(struct cmd *, struct cmdq_item *);
|
2009-06-01 22:58:49 +00:00
|
|
|
|
|
|
|
const struct cmd_entry cmd_new_window_entry = {
|
2015-12-13 21:53:57 +00:00
|
|
|
.name = "new-window",
|
|
|
|
.alias = "neww",
|
|
|
|
|
|
|
|
.args = { "ac:dF:kn:Pt:", 0, -1 },
|
|
|
|
.usage = "[-adkP] [-c start-directory] [-F format] [-n window-name] "
|
|
|
|
CMD_TARGET_WINDOW_USAGE " [command]",
|
|
|
|
|
2017-04-22 10:22:39 +00:00
|
|
|
.target = { 't', CMD_FIND_WINDOW, CMD_FIND_WINDOW_INDEX },
|
2015-12-14 00:31:54 +00:00
|
|
|
|
|
|
|
.flags = 0,
|
2015-12-13 21:53:57 +00:00
|
|
|
.exec = cmd_new_window_exec
|
2009-06-01 22:58:49 +00:00
|
|
|
};
|
|
|
|
|
2016-10-10 21:51:39 +00:00
|
|
|
static enum cmd_retval
|
2016-10-16 19:04:05 +00:00
|
|
|
cmd_new_window_exec(struct cmd *self, struct cmdq_item *item)
|
2009-06-01 22:58:49 +00:00
|
|
|
{
|
2012-03-04 20:50:53 +00:00
|
|
|
struct args *args = self->args;
|
2017-04-22 08:56:24 +00:00
|
|
|
struct cmd_find_state *current = &item->shared->current;
|
2019-04-07 12:01:03 +00:00
|
|
|
struct spawn_context sc;
|
|
|
|
struct client *c = cmd_find_client(item, NULL, 1);
|
2017-04-22 10:22:39 +00:00
|
|
|
struct session *s = item->target.s;
|
|
|
|
struct winlink *wl = item->target.wl;
|
|
|
|
int idx = item->target.idx;
|
2019-04-07 12:01:03 +00:00
|
|
|
struct winlink *new_wl;
|
|
|
|
char *cause = NULL, *cp;
|
|
|
|
const char *template;
|
2016-10-13 22:48:51 +00:00
|
|
|
struct cmd_find_state fs;
|
2011-01-04 00:42:46 +00:00
|
|
|
|
2019-04-07 12:01:03 +00:00
|
|
|
if (args_has(args, 'a') && (idx = winlink_shuffle_up(s, wl)) == -1) {
|
|
|
|
cmdq_error(item, "couldn't get a window index");
|
|
|
|
return (CMD_RETURN_ERROR);
|
2014-05-13 08:08:32 +00:00
|
|
|
}
|
2013-10-10 12:26:34 +00:00
|
|
|
|
2019-04-07 12:01:03 +00:00
|
|
|
memset(&sc, 0, sizeof sc);
|
|
|
|
sc.item = item;
|
|
|
|
sc.s = s;
|
2013-11-23 09:18:29 +00:00
|
|
|
|
2019-04-07 12:01:03 +00:00
|
|
|
sc.name = args_get(args, 'n');
|
|
|
|
sc.argc = args->argc;
|
|
|
|
sc.argv = args->argv;
|
|
|
|
|
|
|
|
sc.idx = idx;
|
|
|
|
sc.cwd = args_get(args, 'c');
|
|
|
|
|
|
|
|
sc.flags = 0;
|
|
|
|
if (args_has(args, 'd'))
|
|
|
|
sc.flags |= SPAWN_DETACHED;
|
|
|
|
if (args_has(args, 'k'))
|
|
|
|
sc.flags |= SPAWN_KILL;
|
|
|
|
|
|
|
|
if ((new_wl = spawn_window(&sc, &cause)) == NULL) {
|
2016-10-16 19:04:05 +00:00
|
|
|
cmdq_error(item, "create window failed: %s", cause);
|
2012-07-10 11:53:01 +00:00
|
|
|
free(cause);
|
2019-04-07 12:01:03 +00:00
|
|
|
return (CMD_RETURN_ERROR);
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|
2019-04-07 12:01:03 +00:00
|
|
|
if (!args_has(args, 'd') || new_wl == s->curw) {
|
|
|
|
cmd_find_from_winlink(current, new_wl, 0);
|
2009-10-10 10:02:48 +00:00
|
|
|
server_redraw_session_group(s);
|
2009-12-03 22:50:09 +00:00
|
|
|
} else
|
2009-10-10 10:02:48 +00:00
|
|
|
server_status_session_group(s);
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2012-03-04 20:50:53 +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 = NEW_WINDOW_TEMPLATE;
|
2019-04-07 12:01:03 +00:00
|
|
|
cp = format_single(item, template, c, s, new_wl, NULL);
|
2016-10-16 19:04:05 +00:00
|
|
|
cmdq_print(item, "%s", cp);
|
2012-07-10 11:53:01 +00:00
|
|
|
free(cp);
|
2012-03-04 20:50:53 +00:00
|
|
|
}
|
|
|
|
|
2019-04-10 10:40:59 +00:00
|
|
|
cmd_find_from_winlink(&fs, new_wl, 0);
|
2016-10-16 19:04:05 +00:00
|
|
|
hooks_insert(s->hooks, item, &fs, "after-new-window");
|
2016-10-16 17:55:14 +00:00
|
|
|
|
2012-07-11 07:10:15 +00:00
|
|
|
return (CMD_RETURN_NORMAL);
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|