Clear window->flags when clearing winlinks
[tmux-openbsd.git] / cmd-new-window.c
blobeac0df445e876c9a2262d04b388219cf69010cde
1 /* $OpenBSD$ */
3 /*
4 * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
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.
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.
19 #include <sys/types.h>
21 #include <stdlib.h>
23 #include "tmux.h"
26 * Create a new window.
29 enum cmd_retval cmd_new_window_exec(struct cmd *, struct cmd_q *);
31 const struct cmd_entry cmd_new_window_entry = {
32 "new-window", "neww",
33 "ac:dF:kn:Pt:", 0, 1,
34 "[-adkP] [-c start-directory] [-F format] [-n window-name] "
35 CMD_TARGET_WINDOW_USAGE " [command]",
37 NULL,
38 NULL,
39 cmd_new_window_exec
42 enum cmd_retval
43 cmd_new_window_exec(struct cmd *self, struct cmd_q *cmdq)
45 struct args *args = self->args;
46 struct session *s;
47 struct winlink *wl;
48 struct client *c;
49 const char *cmd, *cwd, *template;
50 char *cause, *cp;
51 int idx, last, detached;
52 struct format_tree *ft;
54 if (args_has(args, 'a')) {
55 wl = cmd_find_window(cmdq, args_get(args, 't'), &s);
56 if (wl == NULL)
57 return (CMD_RETURN_ERROR);
58 idx = wl->idx + 1;
60 /* Find the next free index. */
61 for (last = idx; last < INT_MAX; last++) {
62 if (winlink_find_by_index(&s->windows, last) == NULL)
63 break;
65 if (last == INT_MAX) {
66 cmdq_error(cmdq, "no free window indexes");
67 return (CMD_RETURN_ERROR);
70 /* Move everything from last - 1 to idx up a bit. */
71 for (; last > idx; last--) {
72 wl = winlink_find_by_index(&s->windows, last - 1);
73 server_link_window(s, wl, s, last, 0, 0, NULL);
74 server_unlink_window(s, wl);
76 } else {
77 if ((idx = cmd_find_index(cmdq, args_get(args, 't'), &s)) == -2)
78 return (CMD_RETURN_ERROR);
80 detached = args_has(args, 'd');
82 wl = NULL;
83 if (idx != -1)
84 wl = winlink_find_by_index(&s->windows, idx);
85 if (wl != NULL && args_has(args, 'k')) {
87 * Can't use session_detach as it will destroy session if this
88 * makes it empty.
90 notify_window_unlinked(s, wl->window);
91 wl->flags &= ~WINLINK_ALERTFLAGS;
92 winlink_stack_remove(&s->lastw, wl);
93 winlink_remove(&s->windows, wl);
95 /* Force select/redraw if current. */
96 if (wl == s->curw) {
97 detached = 0;
98 s->curw = NULL;
102 if (args->argc == 0)
103 cmd = options_get_string(&s->options, "default-command");
104 else
105 cmd = args->argv[0];
106 cwd = cmd_get_default_path(cmdq, args_get(args, 'c'));
108 if (idx == -1)
109 idx = -1 - options_get_number(&s->options, "base-index");
110 wl = session_new(s, args_get(args, 'n'), cmd, cwd, idx, &cause);
111 if (wl == NULL) {
112 cmdq_error(cmdq, "create window failed: %s", cause);
113 free(cause);
114 return (CMD_RETURN_ERROR);
116 if (!detached) {
117 session_select(s, wl->idx);
118 server_redraw_session_group(s);
119 } else
120 server_status_session_group(s);
122 if (args_has(args, 'P')) {
123 if ((template = args_get(args, 'F')) == NULL)
124 template = NEW_WINDOW_TEMPLATE;
126 ft = format_create();
127 if ((c = cmd_find_client(cmdq, NULL, 1)) != NULL)
128 format_client(ft, c);
129 format_session(ft, s);
130 format_winlink(ft, s, wl);
131 format_window_pane(ft, wl->window->active);
133 cp = format_expand(ft, template);
134 cmdq_print(cmdq, "%s", cp);
135 free(cp);
137 format_free(ft);
140 return (CMD_RETURN_NORMAL);