Coverage Report

Created: 2026-06-12 06:50

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/tmux/cmd-split-window.c
Line
Count
Source
1
/* $OpenBSD$ */
2
3
/*
4
 * Copyright (c) 2009 Nicholas Marriott <nicholas.marriott@gmail.com>
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 <errno.h>
22
#include <fcntl.h>
23
#include <stdlib.h>
24
#include <string.h>
25
#include <unistd.h>
26
27
#include "tmux.h"
28
29
/*
30
 * Create a new pane.
31
 */
32
33
0
#define SPLIT_WINDOW_TEMPLATE "#{session_name}:#{window_index}.#{pane_index}"
34
35
static enum cmd_retval  cmd_split_window_exec(struct cmd *, struct cmdq_item *);
36
37
const struct cmd_entry cmd_new_pane_entry = {
38
  .name = "new-pane",
39
  .alias = "newp",
40
41
  .args = { "bc:de:EfF:hIkl:Lm:p:PR:s:S:t:T:vWx:X:y:Y:Z", 0, -1, NULL },
42
  .usage = "[-bdefhIklPvWZ] [-c start-directory] [-e environment] "
43
     "[-F format] [-l size] [-m message] [-p percentage] "
44
     "[-s style] [-S active-border-style] "
45
     "[-R inactive-border-style] [-T title] [-x width] [-y height] "
46
     "[-X x-position] [-Y y-position] " CMD_TARGET_PANE_USAGE " "
47
     "[shell-command [argument ...]]",
48
49
  .target = { 't', CMD_FIND_PANE, 0 },
50
51
  .flags = 0,
52
  .exec = cmd_split_window_exec
53
};
54
55
const struct cmd_entry cmd_split_window_entry = {
56
  .name = "split-window",
57
  .alias = "splitw",
58
59
  .args = { "bc:de:EfF:hIkl:m:p:PR:s:S:t:T:vWZ", 0, -1, NULL },
60
  .usage = "[-bdefhIklPvWZ] [-c start-directory] [-e environment] "
61
     "[-F format] [-l size] [-m message] [-p percentage] "
62
     "[-s style] [-S active-border-style] "
63
     "[-R inactive-border-style] [-T title] " CMD_TARGET_PANE_USAGE " "
64
     "[shell-command [argument ...]]",
65
66
  .target = { 't', CMD_FIND_PANE, 0 },
67
68
  .flags = 0,
69
  .exec = cmd_split_window_exec
70
};
71
72
static enum cmd_retval
73
cmd_split_window_exec(struct cmd *self, struct cmdq_item *item)
74
0
{
75
0
  struct args   *args = cmd_get_args(self);
76
0
  struct cmd_find_state *current = cmdq_get_current(item);
77
0
  struct cmd_find_state *target = cmdq_get_target(item);
78
0
  struct spawn_context   sc = { 0 };
79
0
  struct client   *tc = cmdq_get_target_client(item);
80
0
  struct session    *s = target->s;
81
0
  struct winlink    *wl = target->wl;
82
0
  struct window   *w = wl->window;
83
0
  struct window_pane  *wp = target->wp, *new_wp;
84
0
  struct layout_cell  *lc = NULL;
85
0
  struct cmd_find_state  fs;
86
0
  int      input, empty, is_floating, flags = 0;
87
0
  const char    *template, *style;
88
0
  char      *cause = NULL, *cp, *title;
89
0
  struct args_value *av;
90
0
  u_int      count = args_count(args);
91
92
0
  if (cmd_get_entry(self) == &cmd_new_pane_entry)
93
0
    is_floating = !args_has(args, 'L');
94
0
  else
95
0
    is_floating = 0;
96
97
0
  flags = is_floating ? SPAWN_FLOATING : 0;
98
0
  if (args_has(args, 'b'))
99
0
    flags |= SPAWN_BEFORE;
100
0
  if (args_has(args, 'f'))
101
0
    flags |= SPAWN_FULLSIZE;
102
103
0
  input = args_has(args, 'I');
104
0
  if (input)
105
0
    empty = 1;
106
0
  else
107
0
    empty = args_has(args, 'E');
108
0
  if (empty &&
109
0
      count != 0 &&
110
0
      (count != 1 || *args_string(args, 0) != '\0')) {
111
0
    cmdq_error(item, "command cannot be given for empty pane");
112
0
    return (CMD_RETURN_ERROR);
113
0
  }
114
0
  if (empty)
115
0
    flags |= SPAWN_EMPTY;
116
117
0
  if (is_floating)
118
0
    lc = layout_get_floating_cell(item, args, w, wp, &cause);
119
0
  else
120
0
    lc = layout_get_tiled_cell(item, args, w, wp, flags, &cause);
121
0
  if (cause != NULL) {
122
0
    cmdq_error(item, "size or position %s", cause);
123
0
    free(cause);
124
0
    return (CMD_RETURN_ERROR);
125
0
  }
126
127
0
  sc.item = item;
128
0
  sc.s = s;
129
0
  sc.wl = wl;
130
131
0
  sc.wp0 = wp;
132
0
  sc.lc = lc;
133
134
0
  args_to_vector(args, &sc.argc, &sc.argv);
135
0
  sc.environ = environ_create();
136
137
0
  av = args_first_value(args, 'e');
138
0
  while (av != NULL) {
139
0
    environ_put(sc.environ, av->string, 0);
140
0
    av = args_next_value(av);
141
0
  }
142
143
0
  sc.idx = -1;
144
0
  sc.cwd = args_get(args, 'c');
145
146
0
  sc.flags = flags;
147
0
  if (args_has(args, 'd'))
148
0
    sc.flags |= SPAWN_DETACHED;
149
0
  if (args_has(args, 'Z'))
150
0
    sc.flags |= SPAWN_ZOOM;
151
152
0
  if ((new_wp = spawn_pane(&sc, &cause)) == NULL) {
153
0
    cmdq_error(item, "create pane failed: %s", cause);
154
0
    free(cause);
155
0
    if (sc.argv != NULL)
156
0
      cmd_free_argv(sc.argc, sc.argv);
157
0
    environ_free(sc.environ);
158
0
    return (CMD_RETURN_ERROR);
159
0
  }
160
161
0
  style = args_get(args, 's');
162
0
  if (style != NULL) {
163
0
    if (options_set_string(new_wp->options, "window-style", 0,
164
0
        "%s", style) == NULL) {
165
0
      cmdq_error(item, "bad style: %s", style);
166
0
      return (CMD_RETURN_ERROR);
167
0
    }
168
0
    options_set_string(new_wp->options, "window-active-style", 0,
169
0
        "%s", style);
170
0
    new_wp->flags |= (PANE_REDRAW|PANE_STYLECHANGED|
171
0
        PANE_THEMECHANGED);
172
0
  }
173
0
  style = args_get(args, 'S');
174
0
  if (style != NULL) {
175
0
    if (options_set_string(new_wp->options,
176
0
        "pane-active-border-style", 0, "%s", style) == NULL) {
177
0
      cmdq_error(item, "bad active border style: %s", style);
178
0
      return (CMD_RETURN_ERROR);
179
0
    }
180
0
  }
181
0
  style = args_get(args, 'R');
182
0
  if (style != NULL) {
183
0
    if (options_set_string(new_wp->options, "pane-border-style", 0,
184
0
        "%s", style) == NULL) {
185
0
      cmdq_error(item, "bad inactive border style: %s",
186
0
          style);
187
0
      return (CMD_RETURN_ERROR);
188
0
    }
189
0
  }
190
0
  if (args_has(args, 'k') || args_has(args, 'm')) {
191
0
    options_set_number(new_wp->options, "remain-on-exit", 3);
192
0
    if (args_has(args, 'm'))
193
0
      options_set_string(new_wp->options,
194
0
        "remain-on-exit-format",
195
0
        0, "%s", args_get(args, 'm'));
196
0
  }
197
0
  if (args_has(args, 'T')) {
198
0
    title = format_single_from_target(item, args_get(args, 'T'));
199
0
    screen_set_title(&new_wp->base, title);
200
0
    notify_pane("pane-title-changed", new_wp);
201
0
    free(title);
202
0
  }
203
204
0
  if (input) {
205
0
    switch (window_pane_start_input(new_wp, item, &cause)) {
206
0
    case -1:
207
0
      server_client_remove_pane(new_wp);
208
0
      if (!is_floating)
209
0
        layout_close_pane(new_wp);
210
0
      window_remove_pane(wp->window, new_wp);
211
0
      cmdq_error(item, "%s", cause);
212
0
      free(cause);
213
0
      if (sc.argv != NULL)
214
0
        cmd_free_argv(sc.argc, sc.argv);
215
0
      environ_free(sc.environ);
216
0
      return (CMD_RETURN_ERROR);
217
0
    case 1:
218
0
      input = 0;
219
0
      break;
220
0
    }
221
0
  }
222
0
  if (!args_has(args, 'd'))
223
0
    cmd_find_from_winlink_pane(current, wl, new_wp, 0);
224
225
0
  if (!is_floating) {
226
0
    window_pop_zoom(wp->window);
227
0
    server_redraw_window(wp->window);
228
0
  }
229
0
  server_redraw_session(s);
230
231
0
  if (args_has(args, 'P')) {
232
0
    if ((template = args_get(args, 'F')) == NULL)
233
0
      template = SPLIT_WINDOW_TEMPLATE;
234
0
    cp = format_single(item, template, tc, s, wl, new_wp);
235
0
    cmdq_print(item, "%s", cp);
236
0
    free(cp);
237
0
  }
238
239
0
  cmd_find_from_winlink_pane(&fs, wl, new_wp, 0);
240
0
  cmdq_insert_hook(s, item, &fs, "after-split-window");
241
242
0
  if (sc.argv != NULL)
243
0
    cmd_free_argv(sc.argc, sc.argv);
244
0
  environ_free(sc.environ);
245
0
  if (input)
246
0
    return (CMD_RETURN_WAIT);
247
248
0
  if (args_has(args, 'W')) {
249
    /*
250
     * With -W, block this command queue item until the pane's
251
     * command exits; window_pane_wait_finish will be called to
252
     * continue it.
253
     */
254
0
    new_wp->wait_item = item;
255
0
    return (CMD_RETURN_WAIT);
256
0
  }
257
0
  return (CMD_RETURN_NORMAL);
258
0
}