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