Coverage Report

Created: 2026-10-17 06:19

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