[BACK]Return to cmd-new-window.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / tmux

Annotation of src/usr.bin/tmux/cmd-new-window.c, Revision 1.74

1.74    ! nicm        1: /* $OpenBSD: cmd-new-window.c,v 1.73 2018/03/01 12:53:08 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:
                     41:        .args = { "ac:dF:kn:Pt:", 0, -1 },
                     42:        .usage = "[-adkP] [-c start-directory] [-F format] [-n window-name] "
                     43:                 CMD_TARGET_WINDOW_USAGE " [command]",
                     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.21      nicm       54:        struct args             *args = self->args;
1.69      nicm       55:        struct cmd_find_state   *current = &item->shared->current;
1.70      nicm       56:        struct session          *s = item->target.s;
                     57:        struct winlink          *wl = item->target.wl;
                     58:        struct client           *c = cmd_find_client(item, NULL, 1);
                     59:        int                      idx = item->target.idx;
1.73      nicm       60:        const char              *cmd, *path, *template, *tmp;
                     61:        char                   **argv, *cause, *cp, *cwd, *name;
1.55      nicm       62:        int                      argc, detached;
1.39      nicm       63:        struct environ_entry    *envent;
1.62      nicm       64:        struct cmd_find_state    fs;
1.17      nicm       65:
1.74    ! nicm       66:        if (args_has(args, 'a') && wl != NULL) {
1.46      nicm       67:                if ((idx = winlink_shuffle_up(s, wl)) == -1) {
1.64      nicm       68:                        cmdq_error(item, "no free window indexes");
1.26      nicm       69:                        return (CMD_RETURN_ERROR);
1.13      nicm       70:                }
                     71:        }
1.17      nicm       72:        detached = args_has(args, 'd');
1.1       nicm       73:
1.40      nicm       74:        if (args->argc == 0) {
1.48      nicm       75:                cmd = options_get_string(s->options, "default-command");
1.40      nicm       76:                if (cmd != NULL && *cmd != '\0') {
                     77:                        argc = 1;
1.41      nicm       78:                        argv = (char **)&cmd;
1.40      nicm       79:                } else {
                     80:                        argc = 0;
                     81:                        argv = NULL;
                     82:                }
                     83:        } else {
                     84:                argc = args->argc;
                     85:                argv = args->argv;
                     86:        }
1.35      nicm       87:
1.39      nicm       88:        path = NULL;
1.64      nicm       89:        if (item->client != NULL && item->client->session == NULL)
                     90:                envent = environ_find(item->client->environ, "PATH");
1.39      nicm       91:        else
1.49      nicm       92:                envent = environ_find(s->environ, "PATH");
1.39      nicm       93:        if (envent != NULL)
                     94:                path = envent->value;
                     95:
1.73      nicm       96:        if ((tmp = args_get(args, 'c')) != NULL)
                     97:                cwd = format_single(item, tmp, c, s, NULL, NULL);
                     98:        else if (item->client != NULL && item->client->session == NULL)
                     99:                cwd = xstrdup(item->client->cwd);
1.35      nicm      100:        else
1.73      nicm      101:                cwd = xstrdup(s->cwd);
                    102:
                    103:        if ((tmp = args_get(args, 'n')) != NULL)
                    104:                name = format_single(item, tmp, c, s, NULL, NULL);
                    105:        else
                    106:                name = NULL;
1.38      nicm      107:
                    108:        wl = NULL;
                    109:        if (idx != -1)
                    110:                wl = winlink_find_by_index(&s->windows, idx);
                    111:        if (wl != NULL && args_has(args, 'k')) {
                    112:                /*
                    113:                 * Can't use session_detach as it will destroy session if this
                    114:                 * makes it empty.
                    115:                 */
1.65      nicm      116:                notify_session_window("window-unlinked", s, wl->window);
1.38      nicm      117:                wl->flags &= ~WINLINK_ALERTFLAGS;
                    118:                winlink_stack_remove(&s->lastw, wl);
                    119:                winlink_remove(&s->windows, wl);
                    120:
                    121:                /* Force select/redraw if current. */
                    122:                if (wl == s->curw) {
                    123:                        detached = 0;
                    124:                        s->curw = NULL;
                    125:                }
                    126:        }
1.1       nicm      127:
1.6       nicm      128:        if (idx == -1)
1.48      nicm      129:                idx = -1 - options_get_number(s->options, "base-index");
1.73      nicm      130:        wl = session_new(s, name, argc, argv, path, cwd, idx,
1.40      nicm      131:                &cause);
1.1       nicm      132:        if (wl == NULL) {
1.64      nicm      133:                cmdq_error(item, "create window failed: %s", cause);
1.25      nicm      134:                free(cause);
1.35      nicm      135:                goto error;
1.1       nicm      136:        }
1.17      nicm      137:        if (!detached) {
1.1       nicm      138:                session_select(s, wl->idx);
1.72      nicm      139:                cmd_find_from_winlink(current, wl, 0);
1.8       nicm      140:                server_redraw_session_group(s);
1.11      nicm      141:        } else
1.8       nicm      142:                server_status_session_group(s);
1.1       nicm      143:
1.21      nicm      144:        if (args_has(args, 'P')) {
1.24      nicm      145:                if ((template = args_get(args, 'F')) == NULL)
1.27      nicm      146:                        template = NEW_WINDOW_TEMPLATE;
1.67      nicm      147:                cp = format_single(item, template, c, s, wl, NULL);
1.64      nicm      148:                cmdq_print(item, "%s", cp);
1.25      nicm      149:                free(cp);
1.21      nicm      150:        }
1.61      nicm      151:
1.72      nicm      152:        cmd_find_from_winlink(&fs, wl, 0);
1.64      nicm      153:        hooks_insert(s->hooks, item, &fs, "after-new-window");
1.63      nicm      154:
1.73      nicm      155:        free(name);
                    156:        free(cwd);
1.26      nicm      157:        return (CMD_RETURN_NORMAL);
1.35      nicm      158:
                    159: error:
1.73      nicm      160:        free(name);
                    161:        free(cwd);
1.35      nicm      162:        return (CMD_RETURN_ERROR);
1.1       nicm      163: }