Annotation of src/usr.bin/tmux/cmd-new-window.c, Revision 1.34
1.34 ! nicm 1: /* $OpenBSD: cmd-new-window.c,v 1.33 2013/10/10 12:00:21 nicm Exp $ */
1.1 nicm 2:
3: /*
4: * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
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 <stdlib.h>
22:
23: #include "tmux.h"
24:
25: /*
26: * Create a new window.
27: */
28:
1.31 nicm 29: enum cmd_retval cmd_new_window_exec(struct cmd *, struct cmd_q *);
1.1 nicm 30:
31: const struct cmd_entry cmd_new_window_entry = {
32: "new-window", "neww",
1.21 nicm 33: "ac:dF:kn:Pt:", 0, 1,
34: "[-adkP] [-c start-directory] [-F format] [-n window-name] "
1.29 nicm 35: CMD_TARGET_WINDOW_USAGE " [command]",
1.17 nicm 36: 0,
37: NULL,
38: cmd_new_window_exec
1.1 nicm 39: };
40:
1.26 nicm 41: enum cmd_retval
1.31 nicm 42: cmd_new_window_exec(struct cmd *self, struct cmd_q *cmdq)
1.1 nicm 43: {
1.21 nicm 44: struct args *args = self->args;
45: struct session *s;
46: struct winlink *wl;
47: struct client *c;
1.32 nicm 48: const char *cmd, *cwd, *template;
49: char *cause, *cp;
1.21 nicm 50: int idx, last, detached;
51: struct format_tree *ft;
1.17 nicm 52:
53: if (args_has(args, 'a')) {
1.31 nicm 54: wl = cmd_find_window(cmdq, args_get(args, 't'), &s);
1.17 nicm 55: if (wl == NULL)
1.26 nicm 56: return (CMD_RETURN_ERROR);
1.13 nicm 57: idx = wl->idx + 1;
58:
59: /* Find the next free index. */
60: for (last = idx; last < INT_MAX; last++) {
61: if (winlink_find_by_index(&s->windows, last) == NULL)
62: break;
63: }
64: if (last == INT_MAX) {
1.31 nicm 65: cmdq_error(cmdq, "no free window indexes");
1.26 nicm 66: return (CMD_RETURN_ERROR);
1.13 nicm 67: }
68:
69: /* Move everything from last - 1 to idx up a bit. */
70: for (; last > idx; last--) {
71: wl = winlink_find_by_index(&s->windows, last - 1);
72: server_link_window(s, wl, s, last, 0, 0, NULL);
73: server_unlink_window(s, wl);
74: }
75: } else {
1.31 nicm 76: if ((idx = cmd_find_index(cmdq, args_get(args, 't'), &s)) == -2)
1.26 nicm 77: return (CMD_RETURN_ERROR);
1.13 nicm 78: }
1.17 nicm 79: detached = args_has(args, 'd');
1.1 nicm 80:
81: wl = NULL;
82: if (idx != -1)
83: wl = winlink_find_by_index(&s->windows, idx);
1.17 nicm 84: if (wl != NULL && args_has(args, 'k')) {
1.12 nicm 85: /*
86: * Can't use session_detach as it will destroy session if this
87: * makes it empty.
88: */
1.23 nicm 89: notify_window_unlinked(s, wl->window);
1.14 nicm 90: wl->flags &= ~WINLINK_ALERTFLAGS;
1.12 nicm 91: winlink_stack_remove(&s->lastw, wl);
92: winlink_remove(&s->windows, wl);
93:
94: /* Force select/redraw if current. */
95: if (wl == s->curw) {
1.17 nicm 96: detached = 0;
1.12 nicm 97: s->curw = NULL;
1.1 nicm 98: }
99: }
100:
1.17 nicm 101: if (args->argc == 0)
1.1 nicm 102: cmd = options_get_string(&s->options, "default-command");
1.17 nicm 103: else
104: cmd = args->argv[0];
1.34 ! nicm 105: cwd = cmdq_default_path(cmdq, args_get(args, 'c'));
1.1 nicm 106:
1.6 nicm 107: if (idx == -1)
108: idx = -1 - options_get_number(&s->options, "base-index");
1.17 nicm 109: wl = session_new(s, args_get(args, 'n'), cmd, cwd, idx, &cause);
1.1 nicm 110: if (wl == NULL) {
1.31 nicm 111: cmdq_error(cmdq, "create window failed: %s", cause);
1.25 nicm 112: free(cause);
1.26 nicm 113: return (CMD_RETURN_ERROR);
1.1 nicm 114: }
1.17 nicm 115: if (!detached) {
1.1 nicm 116: session_select(s, wl->idx);
1.8 nicm 117: server_redraw_session_group(s);
1.11 nicm 118: } else
1.8 nicm 119: server_status_session_group(s);
1.1 nicm 120:
1.21 nicm 121: if (args_has(args, 'P')) {
1.24 nicm 122: if ((template = args_get(args, 'F')) == NULL)
1.27 nicm 123: template = NEW_WINDOW_TEMPLATE;
1.21 nicm 124:
125: ft = format_create();
1.31 nicm 126: if ((c = cmd_find_client(cmdq, NULL, 1)) != NULL)
127: format_client(ft, c);
1.21 nicm 128: format_session(ft, s);
129: format_winlink(ft, s, wl);
1.24 nicm 130: format_window_pane(ft, wl->window->active);
1.21 nicm 131:
132: cp = format_expand(ft, template);
1.31 nicm 133: cmdq_print(cmdq, "%s", cp);
1.25 nicm 134: free(cp);
1.21 nicm 135:
136: format_free(ft);
137: }
138:
1.26 nicm 139: return (CMD_RETURN_NORMAL);
1.1 nicm 140: }