Coverage Report

Created: 2026-07-16 06:53

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/tmux/cmd-resize-pane.c
Line
Count
Source
1
/* $OpenBSD: cmd-resize-pane.c,v 1.67 2026/07/10 13:38:45 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 <ctype.h>
22
#include <stdlib.h>
23
#include <string.h>
24
25
#include "tmux.h"
26
27
/*
28
 * Increase or decrease pane size.
29
 */
30
31
static enum cmd_retval  cmd_resize_pane_exec(struct cmd *, struct cmdq_item *);
32
33
static enum cmd_retval  cmd_resize_pane_mouse_update(struct cmd *,
34
          struct cmdq_item *);
35
static void   cmd_resize_pane_mouse_resize_move_floating(
36
          struct client *, struct mouse_event *);
37
static void   cmd_resize_pane_mouse_resize_tiled(struct client *,
38
          struct mouse_event *);
39
40
const struct cmd_entry cmd_resize_pane_entry = {
41
  .name = "resize-pane",
42
  .alias = "resizep",
43
44
  .args = { "D::L::MR::Tt:U::x:y:Z", 0, 1, NULL },
45
  .usage = "[-MTZ] [-D lines] [-L columns] [-R columns] [-U lines] "
46
     "[-x width] [-y height] " CMD_TARGET_PANE_USAGE,
47
48
  .target = { 't', CMD_FIND_PANE, 0 },
49
50
  .flags = CMD_AFTERHOOK,
51
  .exec = cmd_resize_pane_exec
52
};
53
54
static enum cmd_retval
55
cmd_resize_pane_exec(struct cmd *self, struct cmdq_item *item)
56
0
{
57
0
  struct args   *args = cmd_get_args(self);
58
0
  struct cmd_find_state *target = cmdq_get_target(item);
59
0
  struct window_pane  *wp = target->wp;
60
0
  struct winlink    *wl = target->wl;
61
0
  struct window   *w = wl->window;
62
0
  struct layout_cell  *lc = wp->layout_cell;
63
0
  enum layout_type   type;
64
0
  const char    *errstr, *argval;
65
0
  const char     flags[4] = { 'U', 'D', 'L', 'R' };
66
0
  char      *cause = NULL, flag;
67
0
  int      adjust, x, y, status, opposite = 0;
68
0
  long unsigned    i;
69
0
  struct grid   *gd = wp->base.grid;
70
71
0
  if (args_has(args, 'T')) {
72
0
    if (!TAILQ_EMPTY(&wp->modes))
73
0
      return (CMD_RETURN_NORMAL);
74
0
    adjust = screen_size_y(&wp->base) - 1 - wp->base.cy;
75
0
    if (adjust > (int)gd->hsize)
76
0
      adjust = gd->hsize;
77
0
    grid_remove_history(gd, adjust);
78
0
    wp->base.cy += adjust;
79
0
    wp->flags |= PANE_REDRAW;
80
0
    return (CMD_RETURN_NORMAL);
81
0
  }
82
83
0
  if (args_has(args, 'M'))
84
0
    return (cmd_resize_pane_mouse_update(self, item));
85
86
0
  if (args_has(args, 'Z')) {
87
0
    if (w->flags & WINDOW_ZOOMED)
88
0
      window_unzoom(w, 1);
89
0
    else
90
0
      window_zoom(wp);
91
0
    server_redraw_window(w);
92
0
    return (CMD_RETURN_NORMAL);
93
0
  }
94
0
  server_unzoom_window(w);
95
96
0
  if (args_has(args, 'x')) {
97
0
    x = args_percentage(args, 'x', 0, PANE_MAXIMUM, w->sx, &cause);
98
0
    if (cause != NULL) {
99
0
      cmdq_error(item, "width %s", cause);
100
0
      free(cause);
101
0
      return (CMD_RETURN_ERROR);
102
0
    }
103
0
    if (window_pane_is_floating(wp)) {
104
0
      if (layout_resize_floating_pane_to(wp, LAYOUT_LEFTRIGHT,
105
0
          x, &cause) != 0) {
106
0
        cmdq_error(item, "size %s", cause);
107
0
        free(cause);
108
0
        return (CMD_RETURN_ERROR);
109
0
      }
110
0
    } else
111
0
      layout_resize_pane_to(wp, LAYOUT_LEFTRIGHT, x);
112
0
  }
113
0
  if (args_has(args, 'y')) {
114
0
    y = args_percentage(args, 'y', 0, PANE_MAXIMUM, w->sy, &cause);
115
0
    if (cause != NULL) {
116
0
      cmdq_error(item, "height %s", cause);
117
0
      free(cause);
118
0
      return (CMD_RETURN_ERROR);
119
0
    }
120
0
    status = window_get_pane_status(w);
121
0
    switch (status) {
122
0
    case PANE_STATUS_TOP:
123
0
      if (y != INT_MAX && wp->yoff == 1)
124
0
        y++;
125
0
      break;
126
0
    case PANE_STATUS_BOTTOM:
127
0
      if (y != INT_MAX && wp->yoff + wp->sy == w->sy - 1)
128
0
        y++;
129
0
      break;
130
0
    }
131
0
    if (window_pane_is_floating(wp)) {
132
0
      if (layout_resize_floating_pane_to(wp, LAYOUT_TOPBOTTOM,
133
0
          y, &cause) != 0) {
134
0
        cmdq_error(item, "size %s", cause);
135
0
        free(cause);
136
0
        return (CMD_RETURN_ERROR);
137
0
      }
138
0
    } else
139
0
      layout_resize_pane_to(wp, LAYOUT_TOPBOTTOM, y);
140
0
  }
141
142
0
  for (i = 0; i < nitems(flags); i++) {
143
0
    flag = flags[i];
144
0
    if (!args_has(args, flag))
145
0
      continue;
146
147
0
    argval = args_get(args, flag);
148
0
    if (argval == NULL) {
149
0
      if (args_count(args) == 0)
150
0
        argval = "1";
151
0
      else
152
0
        argval = args_string(args, 0);
153
0
    }
154
155
0
    adjust = strtonum(argval, INT_MIN, INT_MAX, &errstr);
156
0
    if (errstr != NULL) {
157
0
      cmdq_error(item, "adjustment %s", errstr);
158
0
      return (CMD_RETURN_ERROR);
159
0
    }
160
161
0
    type = LAYOUT_TOPBOTTOM;
162
0
    if (flag == 'L' || flag == 'R')
163
0
      type = LAYOUT_LEFTRIGHT;
164
165
0
    if (window_pane_is_floating(wp)) {
166
0
      if (flag == 'L' || flag == 'U')
167
0
        opposite = 1;
168
169
0
      if (layout_resize_floating_pane(wp, type, adjust,
170
0
          opposite, &cause) != 0) {
171
0
        cmdq_error(item, "adjustment %s", cause);
172
0
        free(cause);
173
0
        return (CMD_RETURN_ERROR);
174
0
      }
175
0
    } else {
176
0
      if (flag == 'L' || flag == 'U')
177
0
        adjust = -adjust;
178
0
      layout_resize_pane(wp, type, adjust, 1);
179
0
    }
180
0
  }
181
182
0
  if (lc->parent != NULL)
183
0
    layout_fix_offsets(w);
184
0
  layout_fix_panes(w, NULL);
185
0
  events_fire_window("window-layout-changed", w);
186
0
  server_redraw_window(w);
187
188
0
  return (CMD_RETURN_NORMAL);
189
0
}
190
191
static enum cmd_retval
192
cmd_resize_pane_mouse_update(__unused struct cmd *self, struct cmdq_item *item)
193
0
{
194
0
  struct cmd_find_state *target = cmdq_get_target(item);
195
0
  struct key_event  *event = cmdq_get_event(item);
196
0
  struct window_pane  *wp = target->wp;
197
0
  struct winlink    *wl = target->wl;
198
0
  struct window   *w = wl->window;
199
0
  struct client   *c = cmdq_get_client(item);
200
0
  struct session    *s = target->s;
201
202
0
  if (!event->m.valid)
203
0
    return (CMD_RETURN_NORMAL);
204
0
  wp = cmd_mouse_pane(&event->m, &s, NULL);
205
0
  if (wp == NULL || c == NULL || c->session != s)
206
0
    return (CMD_RETURN_NORMAL);
207
208
0
  if (!window_pane_is_floating(wp)) {
209
0
    c->tty.mouse_drag_update = cmd_resize_pane_mouse_resize_tiled;
210
0
    cmd_resize_pane_mouse_resize_tiled(c, &event->m);
211
0
    return (CMD_RETURN_NORMAL);
212
0
  }
213
214
0
  window_redraw_active_switch(w, wp);
215
0
  window_set_active_pane(w, wp, 1);
216
217
0
  c->tty.mouse_drag_update = cmd_resize_pane_mouse_resize_move_floating;
218
0
  cmd_resize_pane_mouse_resize_move_floating(c, &event->m);
219
0
  return (CMD_RETURN_NORMAL);
220
0
}
221
222
/*
223
 * Resizes or moves the pane by dragging. Resize a floating pane by dragging
224
 * the borders or corners. Grabbing an edge only resizes that axis (special
225
 * case). Moves the pane if dragging the top border. Since characters are
226
 * generally rectangular, to make it easier to grab the corner, the character
227
 * next to the corner is also considered the corner.
228
 */
229
static void
230
cmd_resize_pane_mouse_resize_move_floating(struct client *c,
231
    struct mouse_event *m)
232
0
{
233
0
  struct winlink    *wl;
234
0
  struct window   *w;
235
0
  struct window_pane  *wp;
236
0
  struct layout_cell  *lc;
237
0
  int      y, ly, x, lx, sx, sy, new_sx, new_sy;
238
0
  int      left, right;
239
0
  int      new_xoff, new_yoff, resizes = 0;
240
241
0
  wp = cmd_mouse_pane(m, NULL, &wl);
242
0
  if (wp == NULL) {
243
0
    c->tty.mouse_drag_update = NULL;
244
0
    return;
245
0
  }
246
0
  w = wl->window;
247
0
  lc = wp->layout_cell;
248
0
  sx = wp->sx;
249
0
  sy = wp->sy;
250
0
  left = wp->xoff - 1;
251
0
  right = wp->xoff + sx;
252
0
  if (window_pane_scrollbar_reserve(wp) &&
253
0
      w->sb_pos == PANE_SCROLLBARS_LEFT) {
254
0
    left -= wp->scrollbar_style.width + wp->scrollbar_style.pad;
255
0
  } else if (window_pane_scrollbar_reserve(wp) &&
256
0
      w->sb_pos == PANE_SCROLLBARS_RIGHT) {
257
0
    right += wp->scrollbar_style.width + wp->scrollbar_style.pad;
258
0
  }
259
260
0
  y = m->y + m->oy; x = m->x + m->ox;
261
0
  if (m->statusat == 0 && y >= (int)m->statuslines)
262
0
    y -= m->statuslines;
263
0
  else if (m->statusat > 0 && y >= m->statusat)
264
0
    y = m->statusat - 1;
265
0
  ly = m->ly + m->oy; lx = m->lx + m->ox;
266
0
  if (m->statusat == 0 && ly >= (int)m->statuslines)
267
0
    ly -= m->statuslines;
268
0
  else if (m->statusat > 0 && ly >= m->statusat)
269
0
    ly = m->statusat - 1;
270
271
0
  if ((lx == left || lx == left + 1) && ly == wp->yoff - 1) {
272
    /* Top left corner. */
273
0
    new_sx = lc->g.sx + (lx - x);
274
0
    if (new_sx < PANE_MINIMUM)
275
0
      new_sx = PANE_MINIMUM;
276
0
    new_sy = lc->g.sy + (ly - y);
277
0
    if (new_sy < PANE_MINIMUM)
278
0
      new_sy = PANE_MINIMUM;
279
0
    new_xoff = x + 1; /* because mouse is on border at xoff - 1 */
280
0
    new_yoff = y + 1;
281
0
    layout_set_size(lc, new_sx, new_sy, new_xoff, new_yoff);
282
0
    resizes++;
283
0
  } else if ((lx == right + 1 || lx == right) &&
284
0
      ly == wp->yoff - 1) {
285
    /* Top right corner. */
286
0
    new_sx = x - lc->g.xoff;
287
0
    if (new_sx < PANE_MINIMUM)
288
0
      new_sx = PANE_MINIMUM;
289
0
    new_sy = lc->g.sy + (ly - y);
290
0
    if (new_sy < PANE_MINIMUM)
291
0
      new_sy = PANE_MINIMUM;
292
0
    new_yoff = y + 1;
293
0
    layout_set_size(lc, new_sx, new_sy, lc->g.xoff, new_yoff);
294
0
    resizes++;
295
0
  } else if ((lx == left || lx == left + 1) &&
296
0
      ly == wp->yoff + sy) {
297
    /* Bottom left corner. */
298
0
    new_sx = lc->g.sx + (lx - x);
299
0
    if (new_sx < PANE_MINIMUM)
300
0
      new_sx = PANE_MINIMUM;
301
0
    new_sy = y - lc->g.yoff;
302
0
    if (new_sy < PANE_MINIMUM)
303
0
      return;
304
0
    new_xoff = x + 1;
305
0
    layout_set_size(lc, new_sx, new_sy, new_xoff, lc->g.yoff);
306
0
    resizes++;
307
0
  } else if ((lx == right + 1 || lx == right) &&
308
0
      ly == wp->yoff + sy) {
309
    /* Bottom right corner. */
310
0
    new_sx = x - lc->g.xoff;
311
0
    if (new_sx < PANE_MINIMUM)
312
0
      new_sx = PANE_MINIMUM;
313
0
    new_sy = y - lc->g.yoff;
314
0
    if (new_sy < PANE_MINIMUM)
315
0
      new_sy = PANE_MINIMUM;
316
0
    layout_set_size(lc, new_sx, new_sy, lc->g.xoff, lc->g.yoff);
317
0
    resizes++;
318
0
  } else if (lx == right) {
319
    /* Right border. */
320
0
    new_sx = x - lc->g.xoff;
321
0
    if (new_sx < PANE_MINIMUM)
322
0
      return;
323
0
    layout_set_size(lc, new_sx, lc->g.sy, lc->g.xoff, lc->g.yoff);
324
0
    resizes++;
325
0
  } else if (lx == left) {
326
    /* Left border. */
327
0
    new_sx = lc->g.sx + (lx - x);
328
0
    if (new_sx < PANE_MINIMUM)
329
0
      return;
330
0
    new_xoff = x + 1;
331
0
    layout_set_size(lc, new_sx, lc->g.sy, new_xoff, lc->g.yoff);
332
0
    resizes++;
333
0
  } else if (ly == wp->yoff + sy) {
334
    /* Bottom border. */
335
0
    new_sy = y - lc->g.yoff;
336
0
    if (new_sy < PANE_MINIMUM)
337
0
      return;
338
0
    layout_set_size(lc, lc->g.sx, new_sy, lc->g.xoff, lc->g.yoff);
339
0
    resizes++;
340
0
  } else if (ly == wp->yoff - 1) {
341
    /* Top border (move instead of resize). */
342
0
    new_xoff = lc->g.xoff + (x - lx);
343
0
    new_yoff = y + 1;
344
0
    layout_set_size(lc, lc->g.sx, lc->g.sy, new_xoff, new_yoff);
345
0
    resizes++;
346
0
  }
347
0
  if (resizes != 0) {
348
0
    layout_fix_panes(w, NULL);
349
0
    server_redraw_window(w);
350
0
    server_redraw_window_borders(w);
351
0
  }
352
0
}
353
354
static void
355
cmd_resize_pane_mouse_resize_tiled(struct client *c, struct mouse_event *m)
356
0
{
357
0
  struct winlink    *wl;
358
0
  struct window   *w;
359
0
  u_int      y, ly, x, lx;
360
0
  static const int   offsets[][2] = {
361
0
      { 0, 0 }, { 0, 1 }, { 1, 0 }, { 0, -1 }, { -1, 0 },
362
0
  };
363
0
  struct layout_cell  *cells[nitems(offsets)], *lc;
364
0
  u_int      ncells = 0, i, j, resizes = 0;
365
0
  enum layout_type   type;
366
367
0
  wl = cmd_mouse_window(m, NULL);
368
0
  if (wl == NULL) {
369
0
    c->tty.mouse_drag_update = NULL;
370
0
    return;
371
0
  }
372
0
  w = wl->window;
373
374
0
  y = m->y + m->oy; x = m->x + m->ox;
375
0
  if (m->statusat == 0 && y >= m->statuslines)
376
0
    y -= m->statuslines;
377
0
  else if (m->statusat > 0 && y >= (u_int)m->statusat)
378
0
    y = m->statusat - 1;
379
0
  ly = m->ly + m->oy; lx = m->lx + m->ox;
380
0
  if (m->statusat == 0 && ly >= m->statuslines)
381
0
    ly -= m->statuslines;
382
0
  else if (m->statusat > 0 && ly >= (u_int)m->statusat)
383
0
    ly = m->statusat - 1;
384
385
0
  for (i = 0; i < nitems(cells); i++) {
386
0
    lc = layout_search_by_border(w->layout_root, lx + offsets[i][0],
387
0
        ly + offsets[i][1]);
388
0
    if (lc == NULL)
389
0
      continue;
390
391
0
    for (j = 0; j < ncells; j++) {
392
0
      if (cells[j] == lc) {
393
0
        lc = NULL;
394
0
        break;
395
0
      }
396
0
    }
397
0
    if (lc == NULL)
398
0
      continue;
399
400
0
    cells[ncells] = lc;
401
0
    ncells++;
402
0
  }
403
0
  if (ncells == 0)
404
0
    return;
405
406
0
  for (i = 0; i < ncells; i++) {
407
0
    type = cells[i]->parent->type;
408
0
    if (y != ly && type == LAYOUT_TOPBOTTOM) {
409
0
      layout_resize_layout(w, cells[i], type, y - ly, 0);
410
0
      resizes++;
411
0
    } else if (x != lx && type == LAYOUT_LEFTRIGHT) {
412
0
      layout_resize_layout(w, cells[i], type, x - lx, 0);
413
0
      resizes++;
414
0
    }
415
0
  }
416
0
  if (resizes != 0)
417
0
    server_redraw_window(w);
418
0
}