Coverage Report

Created: 2026-10-17 06:18

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