Coverage Report

Created: 2026-10-28 06:08

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/tmux/cmd-select-window.c
Line
Count
Source
1
/* $OpenBSD: cmd-select-window.c,v 1.31 2026/10/09 13:12:14 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
 * Select window by index.
27
 */
28
29
static enum cmd_retval  cmd_select_window_exec(struct cmd *,
30
          struct cmdq_item *);
31
32
const struct cmd_entry cmd_select_window_entry = {
33
  .name = "select-window",
34
  .description = "Select a window.",
35
  .alias = "selectw",
36
37
  .args = { "lnpTt:", 0, 0, NULL },
38
  .usage = "[-lnpT] " CMD_TARGET_WINDOW_USAGE,
39
40
  .target = { 't', CMD_FIND_WINDOW, 0 },
41
42
  .flags = 0,
43
  .exec = cmd_select_window_exec
44
};
45
46
const struct cmd_entry cmd_next_window_entry = {
47
  .name = "next-window",
48
  .description = "Select the next window.",
49
  .alias = "next",
50
51
  .args = { "at:", 0, 0, NULL },
52
  .usage = "[-a] " CMD_TARGET_SESSION_USAGE,
53
54
  .target = { 't', CMD_FIND_SESSION, 0 },
55
56
  .flags = 0,
57
  .exec = cmd_select_window_exec
58
};
59
60
const struct cmd_entry cmd_previous_window_entry = {
61
  .name = "previous-window",
62
  .description = "Select the previous window.",
63
  .alias = "prev",
64
65
  .args = { "at:", 0, 0, NULL },
66
  .usage = "[-a] " CMD_TARGET_SESSION_USAGE,
67
68
  .target = { 't', CMD_FIND_SESSION, 0 },
69
70
  .flags = 0,
71
  .exec = cmd_select_window_exec
72
};
73
74
const struct cmd_entry cmd_last_window_entry = {
75
  .name = "last-window",
76
  .description = "Select the previously active window.",
77
  .alias = "last",
78
79
  .args = { "t:", 0, 0, NULL },
80
  .usage = CMD_TARGET_SESSION_USAGE,
81
82
  .target = { 't', CMD_FIND_SESSION, 0 },
83
84
  .flags = 0,
85
  .exec = cmd_select_window_exec
86
};
87
88
static enum cmd_retval
89
cmd_select_window_exec(struct cmd *self, struct cmdq_item *item)
90
0
{
91
0
  struct args   *args = cmd_get_args(self);
92
0
  struct client   *c = cmdq_get_client(item);
93
0
  struct cmd_find_state *current = cmdq_get_current(item);
94
0
  struct cmd_find_state *target = cmdq_get_target(item);
95
0
  struct winlink    *wl = target->wl;
96
0
  struct session    *s = target->s;
97
0
  int      next, previous, last, activity;
98
99
0
  next = (cmd_get_entry(self) == &cmd_next_window_entry);
100
0
  if (args_has(args, 'n'))
101
0
    next = 1;
102
0
  previous = (cmd_get_entry(self) == &cmd_previous_window_entry);
103
0
  if (args_has(args, 'p'))
104
0
    previous = 1;
105
0
  last = (cmd_get_entry(self) == &cmd_last_window_entry);
106
0
  if (args_has(args, 'l'))
107
0
    last = 1;
108
109
0
  if (next || previous || last) {
110
0
    activity = args_has(args, 'a');
111
0
    if (next) {
112
0
      if (session_next(s, activity) != 0) {
113
0
        cmdq_error(item, "no next window");
114
0
        return (CMD_RETURN_ERROR);
115
0
      }
116
0
    } else if (previous) {
117
0
      if (session_previous(s, activity) != 0) {
118
0
        cmdq_error(item, "no previous window");
119
0
        return (CMD_RETURN_ERROR);
120
0
      }
121
0
    } else {
122
0
      if (session_last(s) != 0) {
123
0
        cmdq_error(item, "no last window");
124
0
        return (CMD_RETURN_ERROR);
125
0
      }
126
0
    }
127
0
    cmd_find_from_session(current, s, 0);
128
0
    server_redraw_session(s);
129
0
    cmdq_insert_hook(s, item, current, "after-select-window");
130
0
  } else {
131
    /*
132
     * If -T and select-window is invoked on same window as
133
     * current, switch to previous window.
134
     */
135
0
    if (args_has(args, 'T') && wl == s->curw) {
136
0
      if (session_last(s) != 0) {
137
0
        cmdq_error(item, "no last window");
138
0
        return (-1);
139
0
      }
140
0
      if (current->s == s)
141
0
        cmd_find_from_session(current, s, 0);
142
0
      server_redraw_session(s);
143
0
    } else if (session_select(s, wl->idx) == 0) {
144
0
      cmd_find_from_session(current, s, 0);
145
0
      server_redraw_session(s);
146
0
    }
147
0
    cmdq_insert_hook(s, item, current, "after-select-window");
148
0
  }
149
0
  if (c != NULL && c->session != NULL)
150
0
    s->curw->window->latest = c;
151
0
  recalculate_sizes();
152
153
0
  return (CMD_RETURN_NORMAL);
154
0
}