Coverage Report

Created: 2026-07-16 06:55

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/tmux/cmd-kill-window.c
Line
Count
Source
1
/* $OpenBSD: cmd-kill-window.c,v 1.30 2026/06/09 12:57:40 nicm Exp $ */
2
3
/*
4
 * Copyright (c) 2007 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
 * Destroy window.
27
 */
28
29
static enum cmd_retval  cmd_kill_window_exec(struct cmd *, struct cmdq_item *);
30
static enum cmd_retval  cmd_kill_window_all(struct cmdq_item *, const char *);
31
static int    cmd_kill_window_filter(struct cmdq_item *,
32
          struct session *, struct winlink *, const char *);
33
34
const struct cmd_entry cmd_kill_window_entry = {
35
  .name = "kill-window",
36
  .alias = "killw",
37
38
  .args = { "af:t:", 0, 0, NULL },
39
  .usage = "[-a] [-f filter] " CMD_TARGET_WINDOW_USAGE,
40
41
  .target = { 't', CMD_FIND_WINDOW, 0 },
42
43
  .flags = 0,
44
  .exec = cmd_kill_window_exec
45
};
46
47
const struct cmd_entry cmd_unlink_window_entry = {
48
  .name = "unlink-window",
49
  .alias = "unlinkw",
50
51
  .args = { "kt:", 0, 0, NULL },
52
  .usage = "[-k] " CMD_TARGET_WINDOW_USAGE,
53
54
  .target = { 't', CMD_FIND_WINDOW, 0 },
55
56
  .flags = 0,
57
  .exec = cmd_kill_window_exec
58
};
59
60
static enum cmd_retval
61
cmd_kill_window_exec(struct cmd *self, struct cmdq_item *item)
62
0
{
63
0
  struct args   *args = cmd_get_args(self);
64
0
  struct cmd_find_state *target = cmdq_get_target(item);
65
0
  struct winlink    *wl = target->wl;
66
0
  struct window   *w = wl->window;
67
0
  struct session    *s = target->s;
68
0
  const char    *filter = args_get(args, 'f');
69
70
0
  if (filter != NULL && !args_has(args, 'a')) {
71
0
    cmdq_error(item, "-f only valid with -a");
72
0
    return (CMD_RETURN_ERROR);
73
0
  }
74
75
0
  if (cmd_get_entry(self) == &cmd_unlink_window_entry) {
76
0
    if (!args_has(args, 'k') && !session_is_linked(s, w)) {
77
0
      cmdq_error(item, "window only linked to one session");
78
0
      return (CMD_RETURN_ERROR);
79
0
    }
80
0
    server_unlink_window(s, wl);
81
0
    recalculate_sizes();
82
0
    return (CMD_RETURN_NORMAL);
83
0
  }
84
85
0
  if (args_has(args, 'a'))
86
0
    return (cmd_kill_window_all(item, filter));
87
88
0
  server_kill_window(wl->window, 1);
89
0
  return (CMD_RETURN_NORMAL);
90
0
}
91
92
static enum cmd_retval
93
cmd_kill_window_all(struct cmdq_item *item, const char *filter)
94
0
{
95
0
  struct cmd_find_state *target = cmdq_get_target(item);
96
0
  struct session    *s = target->s;
97
0
  struct winlink    *wl = target->wl;
98
0
  struct winlink  *loop;
99
0
  u_int    found, kill_current;
100
101
0
  if (RB_PREV(winlinks, &s->windows, wl) == NULL &&
102
0
      RB_NEXT(winlinks, &s->windows, wl) == NULL)
103
0
    return (CMD_RETURN_NORMAL);
104
105
  /* Kill all windows except the current one. */
106
0
  do {
107
0
    found = 0;
108
0
    RB_FOREACH(loop, winlinks, &s->windows) {
109
0
      if (loop->window != wl->window &&
110
0
          cmd_kill_window_filter(item, s, loop, filter)) {
111
0
        server_kill_window(loop->window, 0);
112
0
        found++;
113
0
        break;
114
0
      }
115
0
    }
116
0
  } while (found != 0);
117
118
  /*
119
   * If the current window appears in the session more than once, kill it
120
   * as well if it matches the filter.
121
   */
122
0
  found = kill_current = 0;
123
0
  RB_FOREACH(loop, winlinks, &s->windows) {
124
0
    if (loop->window == wl->window) {
125
0
      found++;
126
0
      if (cmd_kill_window_filter(item, s, loop, filter))
127
0
        kill_current = 1;
128
0
    }
129
0
  }
130
0
  if (kill_current && found > 1)
131
0
    server_kill_window(wl->window, 0);
132
133
0
  server_renumber_all();
134
0
  return (CMD_RETURN_NORMAL);
135
0
}
136
137
static int
138
cmd_kill_window_filter(struct cmdq_item *item, struct session *s,
139
    struct winlink *wl, const char *filter)
140
0
{
141
0
  struct format_tree  *ft;
142
0
  char      *expanded;
143
0
  int      flag;
144
145
0
  if (filter == NULL)
146
0
    return (1);
147
148
0
  ft = format_create(cmdq_get_client(item), item, FORMAT_NONE, 0);
149
0
  format_defaults(ft, NULL, s, wl, NULL);
150
151
0
  expanded = format_expand(ft, filter);
152
0
  flag = format_true(expanded);
153
0
  free(expanded);
154
155
0
  format_free(ft);
156
0
  return (flag);
157
0
}