Coverage Report

Created: 2026-09-01 06:30

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/tmux/cmd-select-pane.c
Line
Count
Source
1
/* $OpenBSD: cmd-select-pane.c,v 1.78 2026/08/20 09:19:24 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
#include <string.h>
23
24
#include "tmux.h"
25
26
/*
27
 * Select pane.
28
 */
29
30
static enum cmd_retval  cmd_select_pane_exec(struct cmd *, struct cmdq_item *);
31
32
const struct cmd_entry cmd_select_pane_entry = {
33
  .name = "select-pane",
34
  .alias = "selectp",
35
36
  .args = { "DdegLlMmP:RT:t:UZ", 0, 0, NULL }, /* -P and -g deprecated */
37
  .usage = "[-DdeLlMmRUZ] [-T title] " CMD_TARGET_PANE_USAGE,
38
39
  .target = { 't', CMD_FIND_PANE, 0 },
40
41
  .flags = 0,
42
  .exec = cmd_select_pane_exec
43
};
44
45
const struct cmd_entry cmd_last_pane_entry = {
46
  .name = "last-pane",
47
  .alias = "lastp",
48
49
  .args = { "det:Z", 0, 0, NULL },
50
  .usage = "[-deZ] " CMD_TARGET_WINDOW_USAGE,
51
52
  .target = { 't', CMD_FIND_WINDOW, 0 },
53
54
  .flags = 0,
55
  .exec = cmd_select_pane_exec
56
};
57
58
static void
59
cmd_select_pane_redraw(struct window *w)
60
0
{
61
0
  struct client *c;
62
63
  /*
64
   * Redraw entire window if it is bigger than the client (the
65
   * offset may change), otherwise just draw borders.
66
   */
67
68
0
  TAILQ_FOREACH(c, &clients, entry) {
69
0
    if (c->session == NULL || (c->flags & CLIENT_CONTROL))
70
0
      continue;
71
0
    if (c->session->curw->window == w && tty_window_bigger(&c->tty))
72
0
      server_redraw_client(c);
73
0
    else {
74
0
      if (c->session->curw->window == w)
75
0
        c->flags |= CLIENT_REDRAWBORDERS;
76
0
      if (session_has(c->session, w))
77
0
        c->flags |= CLIENT_REDRAWSTATUS;
78
0
    }
79
80
0
  }
81
0
}
82
83
static enum cmd_retval
84
cmd_select_pane_marked_pane(struct cmd *self, struct cmdq_item *item)
85
0
{
86
0
  struct args   *args = cmd_get_args(self);
87
0
  struct cmd_find_state *target = cmdq_get_target(item);
88
0
  struct event_payload  *ep;
89
0
  struct cmd_find_state  fs;
90
0
  struct winlink    *wl = target->wl;
91
0
  struct window_pane  *wp = target->wp, *lwp = NULL, *mwp;
92
0
  struct session    *s = target->s;
93
94
0
  if (args_has(args, 'm') && !window_pane_is_visible(wp))
95
0
    return (CMD_RETURN_NORMAL);
96
0
  if (server_check_marked())
97
0
    lwp = marked_pane.wp;
98
99
0
  if (args_has(args, 'M') || server_is_marked(s, wl, wp))
100
0
    server_clear_marked();
101
0
  else
102
0
    server_set_marked(s, wl, wp);
103
0
  mwp = marked_pane.wp;
104
105
0
  ep = event_payload_create();
106
0
  if (mwp != NULL)
107
0
    cmd_find_from_pane(&fs, mwp, 0);
108
0
  else if (lwp != NULL)
109
0
    cmd_find_from_pane(&fs, lwp, 0);
110
0
  else
111
0
    cmd_find_from_pane(&fs, wp, 0);
112
0
  event_payload_set_target(ep, &fs);
113
0
  if (mwp != NULL) {
114
0
    event_payload_set_pane(ep, "pane", mwp);
115
0
    event_payload_set_pane(ep, "new_pane", mwp);
116
0
    event_payload_set_window(ep, "window", mwp->window);
117
0
  } else if (lwp != NULL) {
118
0
    event_payload_set_pane(ep, "pane", lwp);
119
0
    event_payload_set_window(ep, "window", lwp->window);
120
0
  } else {
121
0
    event_payload_set_pane(ep, "pane", wp);
122
0
    event_payload_set_window(ep, "window", wp->window);
123
0
  }
124
0
  if (lwp != NULL)
125
0
    event_payload_set_pane(ep, "old_pane", lwp);
126
0
  event_payload_set_int(ep, "marked", mwp != NULL);
127
0
  events_fire("marked-pane-changed", ep);
128
129
0
  if (lwp != NULL) {
130
0
    lwp->flags |= (PANE_REDRAW|PANE_STYLECHANGED|PANE_THEMECHANGED);
131
0
    server_redraw_window_borders(lwp->window);
132
0
    server_status_window(lwp->window);
133
0
  }
134
0
  if (mwp != NULL) {
135
0
    mwp->flags |= (PANE_REDRAW|PANE_STYLECHANGED|PANE_THEMECHANGED);
136
0
    server_redraw_window_borders(mwp->window);
137
0
    server_status_window(mwp->window);
138
0
  }
139
0
  if (window_pane_is_floating(wp)) {
140
0
    window_redraw_active_switch(wp->window, wp);
141
0
    window_set_active_pane(wp->window, wp, 1);
142
0
  }
143
0
  return (CMD_RETURN_NORMAL);
144
0
}
145
146
static enum cmd_retval
147
cmd_select_pane_exec(struct cmd *self, struct cmdq_item *item)
148
0
{
149
0
  struct args   *args = cmd_get_args(self);
150
0
  const struct cmd_entry  *entry = cmd_get_entry(self);
151
0
  struct cmd_find_state *current = cmdq_get_current(item);
152
0
  struct cmd_find_state *target = cmdq_get_target(item);
153
0
  struct event_payload  *ep;
154
0
  struct cmd_find_state  fs;
155
0
  struct winlink    *wl = target->wl;
156
0
  struct window   *w = wl->window;
157
0
  struct session    *s = target->s;
158
0
  struct window_pane  *wp = target->wp, *lastwp;
159
0
  struct options    *oo = wp->options;
160
0
  char      *title;
161
0
  const char    *style;
162
0
  struct options_entry  *o;
163
0
  int      visible, Zflag = args_has(args, 'Z');
164
165
0
  if (entry == &cmd_last_pane_entry || args_has(args, 'l')) {
166
    /*
167
     * Check for no last pane found in case the other pane was
168
     * spawned without being visited (for example split-window -d).
169
     */
170
0
    lastwp = TAILQ_FIRST(&w->last_panes);
171
0
    if (lastwp == NULL && window_count_panes(w, 1) == 2) {
172
0
      lastwp = TAILQ_PREV(w->active, window_panes, entry);
173
0
      if (lastwp == NULL)
174
0
        lastwp = TAILQ_NEXT(w->active, entry);
175
0
    }
176
0
    if (lastwp == NULL) {
177
0
      cmdq_error(item, "no last pane");
178
0
      return (CMD_RETURN_ERROR);
179
0
    }
180
0
    if (args_has(args, 'e')) {
181
0
      lastwp->flags &= ~PANE_INPUTOFF;
182
0
      server_redraw_window_borders(lastwp->window);
183
0
      server_status_window(lastwp->window);
184
0
    } else if (args_has(args, 'd')) {
185
0
      lastwp->flags |= PANE_INPUTOFF;
186
0
      server_redraw_window_borders(lastwp->window);
187
0
      server_status_window(lastwp->window);
188
0
    } else {
189
0
      if (w->modal != NULL && lastwp != w->modal)
190
0
        visible = 1;
191
0
      else
192
0
        visible = window_pane_is_visible(lastwp);
193
0
      if (!visible && window_push_zoom(w, 0, Zflag))
194
0
        server_redraw_window(w);
195
0
      window_redraw_active_switch(w, lastwp);
196
0
      if (window_set_active_pane(w, lastwp, 1)) {
197
0
        cmd_find_from_winlink(current, wl, 0);
198
0
        cmd_select_pane_redraw(w);
199
0
      }
200
0
      if (!visible && window_pop_zoom(w))
201
0
        server_redraw_window(w);
202
0
    }
203
0
    return (CMD_RETURN_NORMAL);
204
0
  }
205
206
0
  if (args_has(args, 'm') || args_has(args, 'M'))
207
0
    return (cmd_select_pane_marked_pane(self, item));
208
209
0
  style = args_get(args, 'P');
210
0
  if (style != NULL) {
211
0
    o = options_set_string(oo, "window-style", 0, "%s", style);
212
0
    if (o == NULL) {
213
0
      cmdq_error(item, "bad style: %s", style);
214
0
      return (CMD_RETURN_ERROR);
215
0
    }
216
0
    options_set_string(oo, "window-active-style", 0, "%s", style);
217
0
    wp->flags |= (PANE_REDRAW|PANE_STYLECHANGED|PANE_THEMECHANGED);
218
0
  }
219
0
  if (args_has(args, 'g')) {
220
0
    cmdq_print(item, "%s", options_get_string(oo, "window-style"));
221
0
    return (CMD_RETURN_NORMAL);
222
0
  }
223
224
0
  if (args_has(args, 'L')) {
225
0
    window_push_zoom(w, 0, 1);
226
0
    wp = window_pane_find_left(wp);
227
0
    window_pop_zoom(w);
228
0
  } else if (args_has(args, 'R')) {
229
0
    window_push_zoom(w, 0, 1);
230
0
    wp = window_pane_find_right(wp);
231
0
    window_pop_zoom(w);
232
0
  } else if (args_has(args, 'U')) {
233
0
    window_push_zoom(w, 0, 1);
234
0
    wp = window_pane_find_up(wp);
235
0
    window_pop_zoom(w);
236
0
  } else if (args_has(args, 'D')) {
237
0
    window_push_zoom(w, 0, 1);
238
0
    wp = window_pane_find_down(wp);
239
0
    window_pop_zoom(w);
240
0
  }
241
0
  if (wp == NULL)
242
0
    return (CMD_RETURN_NORMAL);
243
244
0
  if (args_has(args, 'e')) {
245
0
    wp->flags &= ~PANE_INPUTOFF;
246
0
    server_redraw_window_borders(wp->window);
247
0
    server_status_window(wp->window);
248
0
    return (CMD_RETURN_NORMAL);
249
0
  }
250
0
  if (args_has(args, 'd')) {
251
0
    wp->flags |= PANE_INPUTOFF;
252
0
    server_redraw_window_borders(wp->window);
253
0
    server_status_window(wp->window);
254
0
    return (CMD_RETURN_NORMAL);
255
0
  }
256
257
0
  if (args_has(args, 'T')) {
258
0
    title = format_single_from_target(item, args_get(args, 'T'));
259
0
    if (screen_set_title(&wp->base, title, 0)) {
260
0
      ep = event_payload_create();
261
0
      cmd_find_from_pane(&fs, wp, 0);
262
0
      event_payload_set_target(ep, &fs);
263
0
      event_payload_set_pane(ep, "pane", wp);
264
0
      event_payload_set_window(ep, "window", wp->window);
265
0
      event_payload_set_string(ep, "new_title", "%s", title);
266
0
      events_fire("pane-title-changed", ep);
267
0
      server_redraw_window_borders(wp->window);
268
0
      server_status_window(wp->window);
269
0
    }
270
0
    free(title);
271
0
    return (CMD_RETURN_NORMAL);
272
0
  }
273
274
0
  if (wp == w->active)
275
0
    return (CMD_RETURN_NORMAL);
276
0
  if (w->modal != NULL && wp != w->modal)
277
0
    visible = 1;
278
0
  else
279
0
    visible = window_pane_is_visible(wp);
280
0
  if (!visible && window_push_zoom(w, 0, Zflag))
281
0
    server_redraw_window(w);
282
0
  window_redraw_active_switch(w, wp);
283
0
  if (window_set_active_pane(w, wp, 1))
284
0
    cmd_find_from_winlink_pane(current, wl, wp, 0);
285
0
  cmdq_insert_hook(s, item, current, "after-select-pane");
286
0
  cmd_select_pane_redraw(w);
287
0
  if (!visible && window_pop_zoom(w))
288
0
    server_redraw_window(w);
289
290
0
  return (CMD_RETURN_NORMAL);
291
0
}