Coverage Report

Created: 2025-08-26 06:15

/src/tmux/cmd-select-pane.c
Line
Count
Source (jump to first uncovered line)
1
/* $OpenBSD$ */
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_exec(struct cmd *self, struct cmdq_item *item)
85
0
{
86
0
  struct args   *args = cmd_get_args(self);
87
0
  const struct cmd_entry  *entry = cmd_get_entry(self);
88
0
  struct cmd_find_state *current = cmdq_get_current(item);
89
0
  struct cmd_find_state *target = cmdq_get_target(item);
90
0
  struct client   *c = cmdq_get_client(item);
91
0
  struct winlink    *wl = target->wl;
92
0
  struct window   *w = wl->window;
93
0
  struct session    *s = target->s;
94
0
  struct window_pane  *wp = target->wp, *activewp, *lastwp, *markedwp;
95
0
  struct options    *oo = wp->options;
96
0
  char      *title;
97
0
  const char    *style;
98
0
  struct options_entry  *o;
99
100
0
  if (entry == &cmd_last_pane_entry || args_has(args, 'l')) {
101
    /*
102
     * Check for no last pane found in case the other pane was
103
     * spawned without being visited (for example split-window -d).
104
     */
105
0
    lastwp = TAILQ_FIRST(&w->last_panes);
106
0
    if (lastwp == NULL && window_count_panes(w) == 2) {
107
0
      lastwp = TAILQ_PREV(w->active, window_panes, entry);
108
0
      if (lastwp == NULL)
109
0
        lastwp = TAILQ_NEXT(w->active, entry);
110
0
    }
111
0
    if (lastwp == NULL) {
112
0
      cmdq_error(item, "no last pane");
113
0
      return (CMD_RETURN_ERROR);
114
0
    }
115
0
    if (args_has(args, 'e')) {
116
0
      lastwp->flags &= ~PANE_INPUTOFF;
117
0
      server_redraw_window_borders(lastwp->window);
118
0
      server_status_window(lastwp->window);
119
0
    } else if (args_has(args, 'd')) {
120
0
      lastwp->flags |= PANE_INPUTOFF;
121
0
      server_redraw_window_borders(lastwp->window);
122
0
      server_status_window(lastwp->window);
123
0
    } else {
124
0
      if (window_push_zoom(w, 0, args_has(args, 'Z')))
125
0
        server_redraw_window(w);
126
0
      window_redraw_active_switch(w, lastwp);
127
0
      if (window_set_active_pane(w, lastwp, 1)) {
128
0
        cmd_find_from_winlink(current, wl, 0);
129
0
        cmd_select_pane_redraw(w);
130
0
      }
131
0
      if (window_pop_zoom(w))
132
0
        server_redraw_window(w);
133
0
    }
134
0
    return (CMD_RETURN_NORMAL);
135
0
  }
136
137
0
  if (args_has(args, 'm') || args_has(args, 'M')) {
138
0
    if (args_has(args, 'm') && !window_pane_visible(wp))
139
0
      return (CMD_RETURN_NORMAL);
140
0
    if (server_check_marked())
141
0
      lastwp = marked_pane.wp;
142
0
    else
143
0
      lastwp = NULL;
144
145
0
    if (args_has(args, 'M') || server_is_marked(s, wl, wp))
146
0
      server_clear_marked();
147
0
    else
148
0
      server_set_marked(s, wl, wp);
149
0
    markedwp = marked_pane.wp;
150
151
0
    if (lastwp != NULL) {
152
0
      lastwp->flags |= (PANE_REDRAW|PANE_STYLECHANGED|
153
0
          PANE_THEMECHANGED);
154
0
      server_redraw_window_borders(lastwp->window);
155
0
      server_status_window(lastwp->window);
156
0
    }
157
0
    if (markedwp != NULL) {
158
0
      markedwp->flags |= (PANE_REDRAW|PANE_STYLECHANGED|
159
0
          PANE_THEMECHANGED);
160
0
      server_redraw_window_borders(markedwp->window);
161
0
      server_status_window(markedwp->window);
162
0
    }
163
0
    return (CMD_RETURN_NORMAL);
164
0
  }
165
166
0
  style = args_get(args, 'P');
167
0
  if (style != NULL) {
168
0
    o = options_set_string(oo, "window-style", 0, "%s", style);
169
0
    if (o == NULL) {
170
0
      cmdq_error(item, "bad style: %s", style);
171
0
      return (CMD_RETURN_ERROR);
172
0
    }
173
0
    options_set_string(oo, "window-active-style", 0, "%s", style);
174
0
    wp->flags |= (PANE_REDRAW|PANE_STYLECHANGED|PANE_THEMECHANGED);
175
0
  }
176
0
  if (args_has(args, 'g')) {
177
0
    cmdq_print(item, "%s", options_get_string(oo, "window-style"));
178
0
    return (CMD_RETURN_NORMAL);
179
0
  }
180
181
0
  if (args_has(args, 'L')) {
182
0
    window_push_zoom(w, 0, 1);
183
0
    wp = window_pane_find_left(wp);
184
0
    window_pop_zoom(w);
185
0
  } else if (args_has(args, 'R')) {
186
0
    window_push_zoom(w, 0, 1);
187
0
    wp = window_pane_find_right(wp);
188
0
    window_pop_zoom(w);
189
0
  } else if (args_has(args, 'U')) {
190
0
    window_push_zoom(w, 0, 1);
191
0
    wp = window_pane_find_up(wp);
192
0
    window_pop_zoom(w);
193
0
  } else if (args_has(args, 'D')) {
194
0
    window_push_zoom(w, 0, 1);
195
0
    wp = window_pane_find_down(wp);
196
0
    window_pop_zoom(w);
197
0
  }
198
0
  if (wp == NULL)
199
0
    return (CMD_RETURN_NORMAL);
200
201
0
  if (args_has(args, 'e')) {
202
0
    wp->flags &= ~PANE_INPUTOFF;
203
0
    server_redraw_window_borders(wp->window);
204
0
    server_status_window(wp->window);
205
0
    return (CMD_RETURN_NORMAL);
206
0
  }
207
0
  if (args_has(args, 'd')) {
208
0
    wp->flags |= PANE_INPUTOFF;
209
0
    server_redraw_window_borders(wp->window);
210
0
    server_status_window(wp->window);
211
0
    return (CMD_RETURN_NORMAL);
212
0
  }
213
214
0
  if (args_has(args, 'T')) {
215
0
    title = format_single_from_target(item, args_get(args, 'T'));
216
0
    if (screen_set_title(&wp->base, title)) {
217
0
      notify_pane("pane-title-changed", wp);
218
0
      server_redraw_window_borders(wp->window);
219
0
      server_status_window(wp->window);
220
0
    }
221
0
    free(title);
222
0
    return (CMD_RETURN_NORMAL);
223
0
  }
224
225
0
  if (c != NULL && c->session != NULL && (c->flags & CLIENT_ACTIVEPANE))
226
0
    activewp = server_client_get_pane(c);
227
0
  else
228
0
    activewp = w->active;
229
0
  if (wp == activewp)
230
0
    return (CMD_RETURN_NORMAL);
231
0
  if (window_push_zoom(w, 0, args_has(args, 'Z')))
232
0
    server_redraw_window(w);
233
0
  window_redraw_active_switch(w, wp);
234
0
  if (c != NULL && c->session != NULL && (c->flags & CLIENT_ACTIVEPANE))
235
0
    server_client_set_pane(c, wp);
236
0
  else if (window_set_active_pane(w, wp, 1))
237
0
    cmd_find_from_winlink_pane(current, wl, wp, 0);
238
0
  cmdq_insert_hook(s, item, current, "after-select-pane");
239
0
  cmd_select_pane_redraw(w);
240
0
  if (window_pop_zoom(w))
241
0
    server_redraw_window(w);
242
243
0
  return (CMD_RETURN_NORMAL);
244
0
}