Annotation of src/usr.bin/tmux/cmd-new-window.c, Revision 1.90
1.90 ! nicm 1: /* $OpenBSD: cmd-new-window.c,v 1.89 2020/06/25 08:56:02 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.90 ! nicm 41: .args = { "abc:de:F:kn:PSt:", 0, -1 },
! 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.77 nicm 58: struct spawn_context sc;
1.87 nicm 59: struct client *tc = cmdq_get_target_client(item);
1.84 nicm 60: struct session *s = target->s;
61: struct winlink *wl = target->wl;
1.88 nicm 62: int idx = target->idx, before;
1.90 ! nicm 63: struct winlink *new_wl = NULL;
1.77 nicm 64: char *cause = NULL, *cp;
1.90 ! nicm 65: const char *template, *add, *name;
1.62 nicm 66: struct cmd_find_state fs;
1.79 nicm 67: struct args_value *value;
1.90 ! nicm 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: RB_FOREACH(wl, winlinks, &s->windows) {
! 76: if (strcmp(wl->window->name, name) != 0)
! 77: continue;
! 78: if (new_wl == NULL) {
! 79: new_wl = wl;
! 80: continue;
! 81: }
! 82: cmdq_error(item, "multiple windows named %s", name);
! 83: return (CMD_RETURN_ERROR);
! 84: }
! 85: if (new_wl != NULL) {
! 86: if (args_has(args, 'd'))
! 87: return (CMD_RETURN_NORMAL);
! 88: if (session_set_current(s, new_wl) == 0)
! 89: server_redraw_session(s);
! 90: if (c != NULL && c->session != NULL)
! 91: s->curw->window->latest = c;
! 92: recalculate_sizes();
! 93: return (CMD_RETURN_NORMAL);
! 94: }
! 95: }
! 96:
1.17 nicm 97:
1.88 nicm 98: before = args_has(args, 'b');
99: if (args_has(args, 'a') || before) {
100: idx = winlink_shuffle_up(s, wl, before);
1.89 nicm 101: if (idx == -1)
102: idx = target->idx;
1.40 nicm 103: }
1.35 nicm 104:
1.77 nicm 105: memset(&sc, 0, sizeof sc);
106: sc.item = item;
107: sc.s = s;
1.87 nicm 108: sc.tc = tc;
1.77 nicm 109:
110: sc.name = args_get(args, 'n');
111: sc.argc = args->argc;
112: sc.argv = args->argv;
1.79 nicm 113: sc.environ = environ_create();
114:
115: add = args_first_value(args, 'e', &value);
116: while (add != NULL) {
1.81 nicm 117: environ_put(sc.environ, add, 0);
1.79 nicm 118: add = args_next_value(&value);
119: }
1.77 nicm 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;
1.1 nicm 129:
1.77 nicm 130: if ((new_wl = spawn_window(&sc, &cause)) == NULL) {
1.64 nicm 131: cmdq_error(item, "create window failed: %s", cause);
1.25 nicm 132: free(cause);
1.77 nicm 133: return (CMD_RETURN_ERROR);
1.1 nicm 134: }
1.77 nicm 135: if (!args_has(args, 'd') || new_wl == s->curw) {
136: cmd_find_from_winlink(current, new_wl, 0);
1.8 nicm 137: server_redraw_session_group(s);
1.11 nicm 138: } else
1.8 nicm 139: server_status_session_group(s);
1.1 nicm 140:
1.21 nicm 141: if (args_has(args, 'P')) {
1.24 nicm 142: if ((template = args_get(args, 'F')) == NULL)
1.27 nicm 143: template = NEW_WINDOW_TEMPLATE;
1.87 nicm 144: cp = format_single(item, template, tc, s, new_wl,
1.89 nicm 145: new_wl->window->active);
1.64 nicm 146: cmdq_print(item, "%s", cp);
1.25 nicm 147: free(cp);
1.21 nicm 148: }
1.61 nicm 149:
1.77 nicm 150: cmd_find_from_winlink(&fs, new_wl, 0);
1.78 nicm 151: cmdq_insert_hook(s, item, &fs, "after-new-window");
1.63 nicm 152:
1.79 nicm 153: environ_free(sc.environ);
1.26 nicm 154: return (CMD_RETURN_NORMAL);
1.1 nicm 155: }