Coverage Report

Created: 2026-07-16 06:55

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/tmux/cmd-kill-pane.c
Line
Count
Source
1
/* $OpenBSD: cmd-kill-pane.c,v 1.34 2026/06/09 21:22:22 nicm Exp $ */
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 <stdlib.h>
22
23
#include "tmux.h"
24
25
/*
26
 * Kill pane.
27
 */
28
29
static enum cmd_retval  cmd_kill_pane_exec(struct cmd *, struct cmdq_item *);
30
static enum cmd_retval  cmd_kill_pane_all(struct cmdq_item *, const char *);
31
static int    cmd_kill_pane_filter(struct cmdq_item *,
32
          struct session *, struct winlink *,
33
          struct window_pane *, const char *);
34
35
const struct cmd_entry cmd_kill_pane_entry = {
36
  .name = "kill-pane",
37
  .alias = "killp",
38
39
  .args = { "af:t:", 0, 0, NULL },
40
  .usage = "[-a] [-f filter] " CMD_TARGET_PANE_USAGE,
41
42
  .target = { 't', CMD_FIND_PANE, 0 },
43
44
  .flags = CMD_AFTERHOOK,
45
  .exec = cmd_kill_pane_exec
46
};
47
48
static enum cmd_retval
49
cmd_kill_pane_exec(struct cmd *self, struct cmdq_item *item)
50
0
{
51
0
  struct args   *args = cmd_get_args(self);
52
0
  struct cmd_find_state *target = cmdq_get_target(item);
53
0
  struct window_pane  *wp = target->wp;
54
0
  const char    *filter = args_get(args, 'f');
55
56
0
  if (filter != NULL && !args_has(args, 'a')) {
57
0
    cmdq_error(item, "-f only valid with -a");
58
0
    return (CMD_RETURN_ERROR);
59
0
  }
60
61
0
  if (args_has(args, 'a'))
62
0
    return (cmd_kill_pane_all(item, filter));
63
64
0
  if (wp == NULL) {
65
0
    cmdq_error(item, "no active pane to kill");
66
0
    return (CMD_RETURN_ERROR);
67
0
  }
68
0
  server_kill_pane(wp);
69
0
  return (CMD_RETURN_NORMAL);
70
0
}
71
72
static enum cmd_retval
73
cmd_kill_pane_all(struct cmdq_item *item, const char *filter)
74
0
{
75
0
  struct cmd_find_state *target = cmdq_get_target(item);
76
0
  struct session    *s = target->s;
77
0
  struct winlink    *wl = target->wl;
78
0
  struct window_pane  *wp = target->wp;
79
0
  struct window_pane  *loopwp, *tmpwp;
80
81
0
  server_unzoom_window(wl->window);
82
0
  TAILQ_FOREACH_SAFE(loopwp, &wl->window->panes, entry, tmpwp) {
83
0
    if (loopwp == wp)
84
0
      continue;
85
0
    if (!cmd_kill_pane_filter(item, s, wl, loopwp, filter))
86
0
      continue;
87
0
    server_client_remove_pane(loopwp);
88
0
    layout_close_pane(loopwp);
89
0
    window_remove_pane(wl->window, loopwp);
90
0
  }
91
0
  server_redraw_window(wl->window);
92
0
  return (CMD_RETURN_NORMAL);
93
0
}
94
95
static int
96
cmd_kill_pane_filter(struct cmdq_item *item, struct session *s,
97
    struct winlink *wl, struct window_pane *wp, const char *filter)
98
0
{
99
0
  struct format_tree  *ft;
100
0
  char      *expanded;
101
0
  int      flag;
102
103
0
  if (filter == NULL)
104
0
    return (1);
105
106
0
  ft = format_create(cmdq_get_client(item), item, FORMAT_NONE, 0);
107
0
  format_defaults(ft, NULL, s, wl, wp);
108
109
0
  expanded = format_expand(ft, filter);
110
0
  flag = format_true(expanded);
111
0
  free(expanded);
112
113
0
  format_free(ft);
114
0
  return (flag);
115
0
}