Coverage Report

Created: 2026-10-05 06:03

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