[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.19

1.19    ! nicm        1: /* $OpenBSD: cmd-new-window.c,v 1.18 2011/12/09 16:28:18 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:
                     29: int    cmd_new_window_exec(struct cmd *, struct cmd_ctx *);
                     30:
                     31: const struct cmd_entry cmd_new_window_entry = {
                     32:        "new-window", "neww",
1.17      nicm       33:        "adkn:Pt:", 0, 1,
                     34:        "[-adk] [-n window-name] [-t target-window] [command]",
                     35:        0,
                     36:        NULL,
                     37:        NULL,
                     38:        cmd_new_window_exec
1.1       nicm       39: };
                     40:
                     41: int
                     42: cmd_new_window_exec(struct cmd *self, struct cmd_ctx *ctx)
                     43: {
1.17      nicm       44:        struct args     *args = self->args;
                     45:        struct session  *s;
                     46:        struct winlink  *wl;
1.19    ! nicm       47:        const char      *cmd, *cwd;
        !            48:        char            *cause;
1.17      nicm       49:        int              idx, last, detached;
                     50:
                     51:        if (args_has(args, 'a')) {
                     52:                wl = cmd_find_window(ctx, args_get(args, 't'), &s);
                     53:                if (wl == NULL)
1.13      nicm       54:                        return (-1);
                     55:                idx = wl->idx + 1;
                     56:
                     57:                /* Find the next free index. */
                     58:                for (last = idx; last < INT_MAX; last++) {
                     59:                        if (winlink_find_by_index(&s->windows, last) == NULL)
                     60:                                break;
                     61:                }
                     62:                if (last == INT_MAX) {
                     63:                        ctx->error(ctx, "no free window indexes");
                     64:                        return (-1);
                     65:                }
                     66:
                     67:                /* Move everything from last - 1 to idx up a bit. */
                     68:                for (; last > idx; last--) {
                     69:                        wl = winlink_find_by_index(&s->windows, last - 1);
                     70:                        server_link_window(s, wl, s, last, 0, 0, NULL);
                     71:                        server_unlink_window(s, wl);
                     72:                }
                     73:        } else {
1.17      nicm       74:                if ((idx = cmd_find_index(ctx, args_get(args, 't'), &s)) == -2)
1.13      nicm       75:                        return (-1);
                     76:        }
1.17      nicm       77:        detached = args_has(args, 'd');
1.1       nicm       78:
                     79:        wl = NULL;
                     80:        if (idx != -1)
                     81:                wl = winlink_find_by_index(&s->windows, idx);
1.17      nicm       82:        if (wl != NULL && args_has(args, 'k')) {
1.12      nicm       83:                /*
                     84:                 * Can't use session_detach as it will destroy session if this
                     85:                 * makes it empty.
                     86:                 */
1.14      nicm       87:                wl->flags &= ~WINLINK_ALERTFLAGS;
1.12      nicm       88:                winlink_stack_remove(&s->lastw, wl);
                     89:                winlink_remove(&s->windows, wl);
                     90:
                     91:                /* Force select/redraw if current. */
                     92:                if (wl == s->curw) {
1.17      nicm       93:                        detached = 0;
1.12      nicm       94:                        s->curw = NULL;
1.1       nicm       95:                }
                     96:        }
                     97:
1.17      nicm       98:        if (args->argc == 0)
1.1       nicm       99:                cmd = options_get_string(&s->options, "default-command");
1.17      nicm      100:        else
                    101:                cmd = args->argv[0];
1.18      nicm      102:        cwd = cmd_get_default_path(ctx);
1.1       nicm      103:
1.6       nicm      104:        if (idx == -1)
                    105:                idx = -1 - options_get_number(&s->options, "base-index");
1.17      nicm      106:        wl = session_new(s, args_get(args, 'n'), cmd, cwd, idx, &cause);
1.1       nicm      107:        if (wl == NULL) {
                    108:                ctx->error(ctx, "create window failed: %s", cause);
                    109:                xfree(cause);
                    110:                return (-1);
                    111:        }
1.17      nicm      112:        if (!detached) {
1.1       nicm      113:                session_select(s, wl->idx);
1.8       nicm      114:                server_redraw_session_group(s);
1.11      nicm      115:        } else
1.8       nicm      116:                server_status_session_group(s);
1.1       nicm      117:
1.17      nicm      118:        if (args_has(args, 'P'))
1.16      nicm      119:                ctx->print(ctx, "%s:%u", s->name, wl->idx);
1.1       nicm      120:        return (0);
                    121: }