Coverage Report

Created: 2026-07-30 07:08

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.77 2026/07/17 12:42:51 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
164
0
  if (entry == &cmd_last_pane_entry || args_has(args, 'l')) {
165
    /*
166
     * Check for no last pane found in case the other pane was
167
     * spawned without being visited (for example split-window -d).
168
     */
169
0
    lastwp = TAILQ_FIRST(&w->last_panes);
170
0
    if (lastwp == NULL && window_count_panes(w, 1) == 2) {
171
0
      lastwp = TAILQ_PREV(w->active, window_panes, entry);
172
0
      if (lastwp == NULL)
173
0
        lastwp = TAILQ_NEXT(w->active, entry);
174
0
    }
175
0
    if (lastwp == NULL) {
176
0
      cmdq_error(item, "no last pane");
177
0
      return (CMD_RETURN_ERROR);
178
0
    }
179
0
    if (args_has(args, 'e')) {
180
0
      lastwp->flags &= ~PANE_INPUTOFF;
181
0
      server_redraw_window_borders(lastwp->window);
182
0
      server_status_window(lastwp->window);
183
0
    } else if (args_has(args, 'd')) {
184
0
      lastwp->flags |= PANE_INPUTOFF;
185
0
      server_redraw_window_borders(lastwp->window);
186
0
      server_status_window(lastwp->window);
187
0
    } else {
188
0
      if (window_push_zoom(w, 0, args_has(args, 'Z')))
189
0
        server_redraw_window(w);
190
0
      window_redraw_active_switch(w, lastwp);
191
0
      if (window_set_active_pane(w, lastwp, 1)) {
192
0
        cmd_find_from_winlink(current, wl, 0);
193
0
        cmd_select_pane_redraw(w);
194
0
      }
195
0
      if (window_pop_zoom(w))
196
0
        server_redraw_window(w);
197
0
    }
198
0
    return (CMD_RETURN_NORMAL);
199
0
  }
200
201
0
  if (args_has(args, 'm') || args_has(args, 'M'))
202
0
    return (cmd_select_pane_marked_pane(self, item));
203
204
0
  style = args_get(args, 'P');
205
0
  if (style != NULL) {
206
0
    o = options_set_string(oo, "window-style", 0, "%s", style);
207
0
    if (o == NULL) {
208
0
      cmdq_error(item, "bad style: %s", style);
209
0
      return (CMD_RETURN_ERROR);
210
0
    }
211
0
    options_set_string(oo, "window-active-style", 0, "%s", style);
212
0
    wp->flags |= (PANE_REDRAW|PANE_STYLECHANGED|PANE_THEMECHANGED);
213
0
  }
214
0
  if (args_has(args, 'g')) {
215
0
    cmdq_print(item, "%s", options_get_string(oo, "window-style"));
216
0
    return (CMD_RETURN_NORMAL);
217
0
  }
218
219
0
  if (args_has(args, 'L')) {
220
0
    window_push_zoom(w, 0, 1);
221
0
    wp = window_pane_find_left(wp);
222
0
    window_pop_zoom(w);
223
0
  } else if (args_has(args, 'R')) {
224
0
    window_push_zoom(w, 0, 1);
225
0
    wp = window_pane_find_right(wp);
226
0
    window_pop_zoom(w);
227
0
  } else if (args_has(args, 'U')) {
228
0
    window_push_zoom(w, 0, 1);
229
0
    wp = window_pane_find_up(wp);
230
0
    window_pop_zoom(w);
231
0
  } else if (args_has(args, 'D')) {
232
0
    window_push_zoom(w, 0, 1);
233
0
    wp = window_pane_find_down(wp);
234
0
    window_pop_zoom(w);
235
0
  }
236
0
  if (wp == NULL)
237
0
    return (CMD_RETURN_NORMAL);
238
239
0
  if (args_has(args, 'e')) {
240
0
    wp->flags &= ~PANE_INPUTOFF;
241
0
    server_redraw_window_borders(wp->window);
242
0
    server_status_window(wp->window);
243
0
    return (CMD_RETURN_NORMAL);
244
0
  }
245
0
  if (args_has(args, 'd')) {
246
0
    wp->flags |= PANE_INPUTOFF;
247
0
    server_redraw_window_borders(wp->window);
248
0
    server_status_window(wp->window);
249
0
    return (CMD_RETURN_NORMAL);
250
0
  }
251
252
0
  if (args_has(args, 'T')) {
253
0
    title = format_single_from_target(item, args_get(args, 'T'));
254
0
    if (screen_set_title(&wp->base, title, 0)) {
255
0
      ep = event_payload_create();
256
0
      cmd_find_from_pane(&fs, wp, 0);
257
0
      event_payload_set_target(ep, &fs);
258
0
      event_payload_set_pane(ep, "pane", wp);
259
0
      event_payload_set_window(ep, "window", wp->window);
260
0
      event_payload_set_string(ep, "new_title", "%s", title);
261
0
      events_fire("pane-title-changed", ep);
262
0
      server_redraw_window_borders(wp->window);
263
0
      server_status_window(wp->window);
264
0
    }
265
0
    free(title);
266
0
    return (CMD_RETURN_NORMAL);
267
0
  }
268
269
0
  if (wp == w->active)
270
0
    return (CMD_RETURN_NORMAL);
271
0
  if (window_push_zoom(w, 0, args_has(args, 'Z')))
272
0
    server_redraw_window(w);
273
0
  window_redraw_active_switch(w, wp);
274
0
  if (window_set_active_pane(w, wp, 1))
275
0
    cmd_find_from_winlink_pane(current, wl, wp, 0);
276
0
  cmdq_insert_hook(s, item, current, "after-select-pane");
277
0
  cmd_select_pane_redraw(w);
278
0
  if (window_pop_zoom(w))
279
0
    server_redraw_window(w);
280
281
0
  return (CMD_RETURN_NORMAL);
282
0
}