Annotation of src/usr.bin/tmux/cmd-resize-pane.c, Revision 1.51
1.51 ! nicm 1: /* $OpenBSD: cmd-resize-pane.c,v 1.50 2021/08/21 10:22:39 nicm Exp $ */
1.1 nicm 2:
3: /*
1.22 nicm 4: * Copyright (c) 2009 Nicholas Marriott <nicholas.marriott@gmail.com>
1.1 nicm 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>
1.39 nicm 22: #include <string.h>
1.1 nicm 23:
24: #include "tmux.h"
25:
26: /*
27: * Increase or decrease pane size.
28: */
29:
1.28 nicm 30: static enum cmd_retval cmd_resize_pane_exec(struct cmd *, struct cmdq_item *);
1.1 nicm 31:
1.25 nicm 32: static void cmd_resize_pane_mouse_update(struct client *,
33: struct mouse_event *);
1.18 nicm 34:
1.1 nicm 35: const struct cmd_entry cmd_resize_pane_entry = {
1.20 nicm 36: .name = "resize-pane",
37: .alias = "resizep",
38:
1.50 nicm 39: .args = { "DLMRTt:Ux:y:Z", 0, 1, NULL },
1.41 nicm 40: .usage = "[-DLMRTUZ] [-x width] [-y height] " CMD_TARGET_PANE_USAGE " "
1.20 nicm 41: "[adjustment]",
42:
1.30 nicm 43: .target = { 't', CMD_FIND_PANE, 0 },
1.21 nicm 44:
1.26 nicm 45: .flags = CMD_AFTERHOOK,
1.20 nicm 46: .exec = cmd_resize_pane_exec
1.1 nicm 47: };
48:
1.25 nicm 49: static enum cmd_retval
1.28 nicm 50: cmd_resize_pane_exec(struct cmd *self, struct cmdq_item *item)
1.1 nicm 51: {
1.42 nicm 52: struct args *args = cmd_get_args(self);
1.43 nicm 53: struct cmd_find_state *target = cmdq_get_target(item);
1.46 nicm 54: struct key_event *event = cmdq_get_event(item);
1.43 nicm 55: struct window_pane *wp = target->wp;
56: struct winlink *wl = target->wl;
1.19 nicm 57: struct window *w = wl->window;
1.43 nicm 58: struct client *c = cmdq_get_client(item);
59: struct session *s = target->s;
1.40 nicm 60: const char *errstr;
61: char *cause;
1.1 nicm 62: u_int adjust;
1.40 nicm 63: int x, y;
1.41 nicm 64: struct grid *gd = wp->base.grid;
65:
66: if (args_has(args, 'T')) {
67: if (!TAILQ_EMPTY(&wp->modes))
68: return (CMD_RETURN_NORMAL);
69: adjust = screen_size_y(&wp->base) - 1 - wp->base.cy;
70: if (adjust > gd->hsize)
71: adjust = gd->hsize;
72: grid_remove_history(gd, adjust);
73: wp->base.cy += adjust;
74: wp->flags |= PANE_REDRAW;
75: return (CMD_RETURN_NORMAL);
76: }
1.1 nicm 77:
1.18 nicm 78: if (args_has(args, 'M')) {
1.46 nicm 79: if (!event->m.valid || cmd_mouse_window(&event->m, &s) == NULL)
1.18 nicm 80: return (CMD_RETURN_NORMAL);
81: if (c == NULL || c->session != s)
82: return (CMD_RETURN_NORMAL);
83: c->tty.mouse_drag_update = cmd_resize_pane_mouse_update;
1.46 nicm 84: cmd_resize_pane_mouse_update(c, &event->m);
1.18 nicm 85: return (CMD_RETURN_NORMAL);
86: }
87:
1.15 nicm 88: if (args_has(args, 'Z')) {
89: if (w->flags & WINDOW_ZOOMED)
90: window_unzoom(w);
91: else
92: window_zoom(wp);
93: server_redraw_window(w);
94: return (CMD_RETURN_NORMAL);
95: }
96: server_unzoom_window(w);
1.1 nicm 97:
1.49 nicm 98: if (args_count(args) == 0)
1.1 nicm 99: adjust = 1;
100: else {
1.51 ! nicm 101: adjust = strtonum(args_string(args, 0), 1, INT_MAX, &errstr);
1.1 nicm 102: if (errstr != NULL) {
1.28 nicm 103: cmdq_error(item, "adjustment %s", errstr);
1.11 nicm 104: return (CMD_RETURN_ERROR);
1.1 nicm 105: }
1.13 nicm 106: }
107:
1.40 nicm 108: if (args_has(args, 'x')) {
109: x = args_percentage(args, 'x', 0, INT_MAX, w->sx, &cause);
110: if (cause != NULL) {
111: cmdq_error(item, "width %s", cause);
112: free(cause);
113: return (CMD_RETURN_ERROR);
1.13 nicm 114: }
115: layout_resize_pane_to(wp, LAYOUT_LEFTRIGHT, x);
116: }
1.40 nicm 117: if (args_has(args, 'y')) {
118: y = args_percentage(args, 'y', 0, INT_MAX, w->sy, &cause);
119: if (cause != NULL) {
1.48 nicm 120: cmdq_error(item, "height %s", cause);
1.40 nicm 121: free(cause);
122: return (CMD_RETURN_ERROR);
1.13 nicm 123: }
124: layout_resize_pane_to(wp, LAYOUT_TOPBOTTOM, y);
1.1 nicm 125: }
126:
1.35 nicm 127: if (args_has(args, 'L'))
1.24 nicm 128: layout_resize_pane(wp, LAYOUT_LEFTRIGHT, -adjust, 1);
1.35 nicm 129: else if (args_has(args, 'R'))
1.24 nicm 130: layout_resize_pane(wp, LAYOUT_LEFTRIGHT, adjust, 1);
1.35 nicm 131: else if (args_has(args, 'U'))
1.24 nicm 132: layout_resize_pane(wp, LAYOUT_TOPBOTTOM, -adjust, 1);
1.35 nicm 133: else if (args_has(args, 'D'))
1.24 nicm 134: layout_resize_pane(wp, LAYOUT_TOPBOTTOM, adjust, 1);
1.1 nicm 135: server_redraw_window(wl->window);
136:
1.11 nicm 137: return (CMD_RETURN_NORMAL);
1.18 nicm 138: }
139:
1.25 nicm 140: static void
1.18 nicm 141: cmd_resize_pane_mouse_update(struct client *c, struct mouse_event *m)
142: {
143: struct winlink *wl;
1.32 nicm 144: struct window *w;
145: u_int y, ly, x, lx;
1.34 nicm 146: static const int offsets[][2] = {
147: { 0, 0 }, { 0, 1 }, { 1, 0 }, { 0, -1 }, { -1, 0 },
148: };
149: struct layout_cell *cells[nitems(offsets)], *lc;
1.33 nicm 150: u_int ncells = 0, i, j, resizes = 0;
151: enum layout_type type;
1.18 nicm 152:
153: wl = cmd_mouse_window(m, NULL);
154: if (wl == NULL) {
155: c->tty.mouse_drag_update = NULL;
156: return;
157: }
1.32 nicm 158: w = wl->window;
1.18 nicm 159:
1.37 nicm 160: y = m->y + m->oy; x = m->x + m->ox;
1.38 nicm 161: if (m->statusat == 0 && y >= m->statuslines)
162: y -= m->statuslines;
1.18 nicm 163: else if (m->statusat > 0 && y >= (u_int)m->statusat)
164: y = m->statusat - 1;
1.36 nicm 165: ly = m->ly + m->oy; lx = m->lx + m->ox;
1.38 nicm 166: if (m->statusat == 0 && ly >= m->statuslines)
167: ly -= m->statuslines;
1.18 nicm 168: else if (m->statusat > 0 && ly >= (u_int)m->statusat)
169: ly = m->statusat - 1;
170:
1.33 nicm 171: for (i = 0; i < nitems(cells); i++) {
172: lc = layout_search_by_border(w->layout_root, lx + offsets[i][0],
173: ly + offsets[i][1]);
174: if (lc == NULL)
175: continue;
176:
177: for (j = 0; j < ncells; j++) {
178: if (cells[j] == lc) {
179: lc = NULL;
180: break;
181: }
182: }
183: if (lc == NULL)
184: continue;
1.32 nicm 185:
1.33 nicm 186: cells[ncells] = lc;
187: ncells++;
188: }
189: if (ncells == 0)
1.31 nicm 190: return;
1.32 nicm 191:
1.33 nicm 192: for (i = 0; i < ncells; i++) {
193: type = cells[i]->parent->type;
194: if (y != ly && type == LAYOUT_TOPBOTTOM) {
195: layout_resize_layout(w, cells[i], type, y - ly, 0);
196: resizes++;
197: } else if (x != lx && type == LAYOUT_LEFTRIGHT) {
198: layout_resize_layout(w, cells[i], type, x - lx, 0);
199: resizes++;
200: }
201: }
202: if (resizes != 0)
203: server_redraw_window(w);
1.1 nicm 204: }