Home | History | Annotate | Line # | Download | only in dist
      1 /* $OpenBSD$ */
      2 
      3 /*
      4  * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott (at) gmail.com>
      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 <errno.h>
     22 #include <fcntl.h>
     23 #include <stdlib.h>
     24 #include <string.h>
     25 #include <unistd.h>
     26 
     27 #include "tmux.h"
     28 
     29 /*
     30  * Create a new window.
     31  */
     32 
     33 #define NEW_WINDOW_TEMPLATE "#{session_name}:#{window_index}.#{pane_index}"
     34 
     35 static enum cmd_retval	cmd_new_window_exec(struct cmd *, struct cmdq_item *);
     36 
     37 const struct cmd_entry cmd_new_window_entry = {
     38 	.name = "new-window",
     39 	.alias = "neww",
     40 
     41 	.args = { "abc:de:F:kn:PSt:", 0, -1, NULL },
     42 	.usage = "[-abdkPS] [-c start-directory] [-e environment] [-F format] "
     43 		 "[-n window-name] " CMD_TARGET_WINDOW_USAGE
     44 		 " [shell-command [argument ...]]",
     45 
     46 	.target = { 't', CMD_FIND_WINDOW, CMD_FIND_WINDOW_INDEX },
     47 
     48 	.flags = 0,
     49 	.exec = cmd_new_window_exec
     50 };
     51 
     52 static enum cmd_retval
     53 cmd_new_window_exec(struct cmd *self, struct cmdq_item *item)
     54 {
     55 	struct args		*args = cmd_get_args(self);
     56 	struct client		*c = cmdq_get_client(item);
     57 	struct cmd_find_state	*current = cmdq_get_current(item);
     58 	struct cmd_find_state	*target = cmdq_get_target(item);
     59 	struct spawn_context	 sc = { 0 };
     60 	struct client		*tc = cmdq_get_target_client(item);
     61 	struct session		*s = target->s;
     62 	struct winlink		*wl = target->wl, *new_wl = NULL;
     63 	int			 idx = target->idx, before;
     64 	char			*cause = NULL, *cp, *expanded;
     65 	const char		*template, *name;
     66 	struct cmd_find_state	 fs;
     67 	struct args_value	*av;
     68 
     69 	/*
     70 	 * If -S and -n are given and -t is not and a single window with this
     71 	 * name already exists, select it.
     72 	 */
     73 	name = args_get(args, 'n');
     74 	if (args_has(args, 'S') && name != NULL && target->idx == -1) {
     75 		expanded = format_single(item, name, c, s, NULL, NULL);
     76 		RB_FOREACH(wl, winlinks, &s->windows) {
     77 			if (strcmp(wl->window->name, expanded) != 0)
     78 				continue;
     79 			if (new_wl == NULL) {
     80 				new_wl = wl;
     81 				continue;
     82 			}
     83 			cmdq_error(item, "multiple windows named %s", name);
     84 			free(expanded);
     85 			return (CMD_RETURN_ERROR);
     86 		}
     87 		free(expanded);
     88 		if (new_wl != NULL) {
     89 			if (args_has(args, 'd'))
     90 				return (CMD_RETURN_NORMAL);
     91 			if (session_set_current(s, new_wl) == 0)
     92 				server_redraw_session(s);
     93 			if (c != NULL && c->session != NULL)
     94 				s->curw->window->latest = c;
     95 			recalculate_sizes();
     96 			return (CMD_RETURN_NORMAL);
     97 		}
     98 	}
     99 
    100 	before = args_has(args, 'b');
    101 	if (args_has(args, 'a') || before) {
    102 		idx = winlink_shuffle_up(s, wl, before);
    103 		if (idx == -1)
    104 			idx = target->idx;
    105 	}
    106 
    107 	sc.item = item;
    108 	sc.s = s;
    109 	sc.tc = tc;
    110 
    111 	sc.name = args_get(args, 'n');
    112 	args_to_vector(args, &sc.argc, &sc.argv);
    113 	sc.environ = environ_create();
    114 
    115 	av = args_first_value(args, 'e');
    116 	while (av != NULL) {
    117 		environ_put(sc.environ, av->string, 0);
    118 		av = args_next_value(av);
    119 	}
    120 
    121 	sc.idx = idx;
    122 	sc.cwd = args_get(args, 'c');
    123 
    124 	sc.flags = 0;
    125 	if (args_has(args, 'd'))
    126 		sc.flags |= SPAWN_DETACHED;
    127 	if (args_has(args, 'k'))
    128 		sc.flags |= SPAWN_KILL;
    129 
    130 	if ((new_wl = spawn_window(&sc, &cause)) == NULL) {
    131 		cmdq_error(item, "create window failed: %s", cause);
    132 		free(cause);
    133 		if (sc.argv != NULL)
    134 			cmd_free_argv(sc.argc, sc.argv);
    135 		environ_free(sc.environ);
    136 		return (CMD_RETURN_ERROR);
    137 	}
    138 	if (!args_has(args, 'd') || new_wl == s->curw) {
    139 		cmd_find_from_winlink(current, new_wl, 0);
    140 		server_redraw_session_group(s);
    141 	} else
    142 		server_status_session_group(s);
    143 
    144 	if (args_has(args, 'P')) {
    145 		if ((template = args_get(args, 'F')) == NULL)
    146 			template = NEW_WINDOW_TEMPLATE;
    147 		cp = format_single(item, template, tc, s, new_wl,
    148 			new_wl->window->active);
    149 		cmdq_print(item, "%s", cp);
    150 		free(cp);
    151 	}
    152 
    153 	cmd_find_from_winlink(&fs, new_wl, 0);
    154 	cmdq_insert_hook(s, item, &fs, "after-new-window");
    155 
    156 	if (sc.argv != NULL)
    157 		cmd_free_argv(sc.argc, sc.argv);
    158 	environ_free(sc.environ);
    159 	return (CMD_RETURN_NORMAL);
    160 }
    161