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

Annotation of src/usr.bin/tmux/cmd-split-window.c, Revision 1.105

1.105   ! nicm        1: /* $OpenBSD: cmd-split-window.c,v 1.104 2020/05/16 16:20:59 nicm Exp $ */
1.1       nicm        2:
                      3: /*
1.68      nicm        4:  * Copyright (c) 2009 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.46      nicm       21: #include <errno.h>
                     22: #include <fcntl.h>
1.11      nicm       23: #include <paths.h>
1.1       nicm       24: #include <stdlib.h>
1.46      nicm       25: #include <string.h>
1.1       nicm       26: #include <unistd.h>
                     27:
                     28: #include "tmux.h"
                     29:
                     30: /*
                     31:  * Split a window (add a new pane).
                     32:  */
1.53      nicm       33:
                     34: #define SPLIT_WINDOW_TEMPLATE "#{session_name}:#{window_index}.#{pane_index}"
1.1       nicm       35:
1.75      nicm       36: static enum cmd_retval cmd_split_window_exec(struct cmd *,
                     37:                            struct cmdq_item *);
1.1       nicm       38:
                     39: const struct cmd_entry cmd_split_window_entry = {
1.66      nicm       40:        .name = "split-window",
                     41:        .alias = "splitw",
                     42:
1.105   ! nicm       43:        .args = { "bc:de:fF:hIl:p:Pt:vZ", 0, -1 },
        !            44:        .usage = "[-bdefhIPvZ] [-c start-directory] [-e environment] "
1.96      nicm       45:                 "[-F format] [-l size] " CMD_TARGET_PANE_USAGE " [command]",
1.66      nicm       46:
1.83      nicm       47:        .target = { 't', CMD_FIND_PANE, 0 },
1.67      nicm       48:
                     49:        .flags = 0,
1.66      nicm       50:        .exec = cmd_split_window_exec
1.1       nicm       51: };
                     52:
1.71      nicm       53: static enum cmd_retval
1.75      nicm       54: cmd_split_window_exec(struct cmd *self, struct cmdq_item *item)
1.1       nicm       55: {
1.99      nicm       56:        struct args             *args = cmd_get_args(self);
1.102     nicm       57:        struct cmd_find_state   *current = cmdq_get_current(item);
1.100     nicm       58:        struct cmd_find_state   *target = cmdq_get_target(item);
1.92      nicm       59:        struct spawn_context     sc;
1.103     nicm       60:        struct client           *tc = cmdq_get_target_client(item);
1.100     nicm       61:        struct session          *s = target->s;
                     62:        struct winlink          *wl = target->wl;
                     63:        struct window_pane      *wp = target->wp, *new_wp;
1.22      nicm       64:        enum layout_type         type;
                     65:        struct layout_cell      *lc;
1.92      nicm       66:        struct cmd_find_state    fs;
1.95      nicm       67:        int                      size, percentage, flags, input;
1.96      nicm       68:        const char              *template, *add, *errstr, *p;
                     69:        char                    *cause, *cp, *copy;
                     70:        size_t                   plen;
1.94      nicm       71:        struct args_value       *value;
1.46      nicm       72:
1.22      nicm       73:        if (args_has(args, 'h'))
1.15      nicm       74:                type = LAYOUT_LEFTRIGHT;
1.92      nicm       75:        else
                     76:                type = LAYOUT_TOPBOTTOM;
1.96      nicm       77:        if ((p = args_get(args, 'l')) != NULL) {
                     78:                plen = strlen(p);
                     79:                if (p[plen - 1] == '%') {
                     80:                        copy = xstrdup(p);
                     81:                        copy[plen - 1] = '\0';
                     82:                        percentage = strtonum(copy, 0, INT_MAX, &errstr);
                     83:                        free(copy);
                     84:                        if (errstr != NULL) {
                     85:                                cmdq_error(item, "percentage %s", errstr);
                     86:                                return (CMD_RETURN_ERROR);
                     87:                        }
                     88:                        if (type == LAYOUT_TOPBOTTOM)
                     89:                                size = (wp->sy * percentage) / 100;
                     90:                        else
                     91:                                size = (wp->sx * percentage) / 100;
                     92:                } else {
                     93:                        size = args_strtonum(args, 'l', 0, INT_MAX, &cause);
                     94:                        if (cause != NULL) {
                     95:                                cmdq_error(item, "lines %s", cause);
                     96:                                free(cause);
                     97:                                return (CMD_RETURN_ERROR);
                     98:                        }
1.22      nicm       99:                }
                    100:        } else if (args_has(args, 'p')) {
                    101:                percentage = args_strtonum(args, 'p', 0, INT_MAX, &cause);
                    102:                if (cause != NULL) {
1.92      nicm      103:                        cmdq_error(item, "create pane failed: -p %s", cause);
1.34      nicm      104:                        free(cause);
1.92      nicm      105:                        return (CMD_RETURN_ERROR);
1.22      nicm      106:                }
1.15      nicm      107:                if (type == LAYOUT_TOPBOTTOM)
1.22      nicm      108:                        size = (wp->sy * percentage) / 100;
1.15      nicm      109:                else
1.22      nicm      110:                        size = (wp->sx * percentage) / 100;
1.92      nicm      111:        } else
                    112:                size = -1;
                    113:
1.105   ! nicm      114:        window_push_zoom(wp->window, 1, args_has(args, 'Z'));
1.95      nicm      115:        input = (args_has(args, 'I') && args->argc == 0);
1.5       nicm      116:
1.92      nicm      117:        flags = 0;
                    118:        if (args_has(args, 'b'))
                    119:                flags |= SPAWN_BEFORE;
                    120:        if (args_has(args, 'f'))
                    121:                flags |= SPAWN_FULLSIZE;
1.95      nicm      122:        if (input || (args->argc == 1 && *args->argv[0] == '\0'))
                    123:                flags |= SPAWN_EMPTY;
1.11      nicm      124:
1.92      nicm      125:        lc = layout_split_pane(wp, type, size, flags);
1.54      nicm      126:        if (lc == NULL) {
1.92      nicm      127:                cmdq_error(item, "no space for new pane");
                    128:                return (CMD_RETURN_ERROR);
1.80      nicm      129:        }
1.5       nicm      130:
1.92      nicm      131:        memset(&sc, 0, sizeof sc);
                    132:        sc.item = item;
                    133:        sc.s = s;
                    134:        sc.wl = wl;
                    135:
                    136:        sc.wp0 = wp;
                    137:        sc.lc = lc;
                    138:
                    139:        sc.name = NULL;
                    140:        sc.argc = args->argc;
                    141:        sc.argv = args->argv;
1.94      nicm      142:        sc.environ = environ_create();
                    143:
                    144:        add = args_first_value(args, 'e', &value);
                    145:        while (add != NULL) {
1.98      nicm      146:                environ_put(sc.environ, add, 0);
1.94      nicm      147:                add = args_next_value(&value);
                    148:        }
1.92      nicm      149:
                    150:        sc.idx = -1;
                    151:        sc.cwd = args_get(args, 'c');
                    152:
                    153:        sc.flags = flags;
                    154:        if (args_has(args, 'd'))
                    155:                sc.flags |= SPAWN_DETACHED;
1.105   ! nicm      156:        if (args_has(args, 'Z'))
        !           157:                sc.flags |= SPAWN_ZOOM;
1.92      nicm      158:
                    159:        if ((new_wp = spawn_pane(&sc, &cause)) == NULL) {
                    160:                cmdq_error(item, "create pane failed: %s", cause);
                    161:                free(cause);
                    162:                return (CMD_RETURN_ERROR);
                    163:        }
1.95      nicm      164:        if (input && window_pane_start_input(new_wp, item, &cause) != 0) {
1.104     nicm      165:                server_client_remove_pane(new_wp);
1.95      nicm      166:                layout_close_pane(new_wp);
                    167:                window_remove_pane(wp->window, new_wp);
                    168:                cmdq_error(item, "%s", cause);
                    169:                free(cause);
                    170:                return (CMD_RETURN_ERROR);
                    171:        }
1.92      nicm      172:        if (!args_has(args, 'd'))
                    173:                cmd_find_from_winlink_pane(current, wl, new_wp, 0);
1.105   ! nicm      174:        window_pop_zoom(wp->window);
1.92      nicm      175:        server_redraw_window(wp->window);
                    176:        server_status_session(s);
1.1       nicm      177:
1.22      nicm      178:        if (args_has(args, 'P')) {
1.33      nicm      179:                if ((template = args_get(args, 'F')) == NULL)
1.36      nicm      180:                        template = SPLIT_WINDOW_TEMPLATE;
1.103     nicm      181:                cp = format_single(item, template, tc, s, wl, new_wp);
1.75      nicm      182:                cmdq_print(item, "%s", cp);
1.34      nicm      183:                free(cp);
1.21      nicm      184:        }
1.72      nicm      185:
1.87      nicm      186:        cmd_find_from_winlink_pane(&fs, wl, new_wp, 0);
1.93      nicm      187:        cmdq_insert_hook(s, item, &fs, "after-split-window");
1.74      nicm      188:
1.94      nicm      189:        environ_free(sc.environ);
1.95      nicm      190:        if (input)
                    191:                return (CMD_RETURN_WAIT);
1.35      nicm      192:        return (CMD_RETURN_NORMAL);
1.1       nicm      193: }