xref: /openbsd-src/usr.bin/tmux/cmd-resize-pane.c (revision 040343ae18e7aa6a49c6acf6e4add8a0b76f6fff)
1 /* $OpenBSD: cmd-resize-pane.c,v 1.43 2020/04/13 10:59:58 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 <stdlib.h>
22 #include <string.h>
23 
24 #include "tmux.h"
25 
26 /*
27  * Increase or decrease pane size.
28  */
29 
30 static enum cmd_retval	cmd_resize_pane_exec(struct cmd *, struct cmdq_item *);
31 
32 static void	cmd_resize_pane_mouse_update(struct client *,
33 		    struct mouse_event *);
34 
35 const struct cmd_entry cmd_resize_pane_entry = {
36 	.name = "resize-pane",
37 	.alias = "resizep",
38 
39 	.args = { "DLMRTt:Ux:y:Z", 0, 1 },
40 	.usage = "[-DLMRTUZ] [-x width] [-y height] " CMD_TARGET_PANE_USAGE " "
41 		 "[adjustment]",
42 
43 	.target = { 't', CMD_FIND_PANE, 0 },
44 
45 	.flags = CMD_AFTERHOOK,
46 	.exec = cmd_resize_pane_exec
47 };
48 
49 static enum cmd_retval
50 cmd_resize_pane_exec(struct cmd *self, struct cmdq_item *item)
51 {
52 	struct args		*args = cmd_get_args(self);
53 	struct cmdq_shared	*shared = cmdq_get_shared(item);
54 	struct cmd_find_state	*target = cmdq_get_target(item);
55 	struct window_pane	*wp = target->wp;
56 	struct winlink		*wl = target->wl;
57 	struct window		*w = wl->window;
58 	struct client		*c = cmdq_get_client(item);
59 	struct session		*s = target->s;
60 	const char	       	*errstr;
61 	char			*cause;
62 	u_int			 adjust;
63 	int			 x, y;
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 	}
77 
78 	if (args_has(args, 'M')) {
79 		if (cmd_mouse_window(&shared->mouse, &s) == NULL)
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;
84 		cmd_resize_pane_mouse_update(c, &shared->mouse);
85 		return (CMD_RETURN_NORMAL);
86 	}
87 
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 		server_status_window(w);
95 		return (CMD_RETURN_NORMAL);
96 	}
97 	server_unzoom_window(w);
98 
99 	if (args->argc == 0)
100 		adjust = 1;
101 	else {
102 		adjust = strtonum(args->argv[0], 1, INT_MAX, &errstr);
103 		if (errstr != NULL) {
104 			cmdq_error(item, "adjustment %s", errstr);
105 			return (CMD_RETURN_ERROR);
106 		}
107 	}
108 
109 	if (args_has(args, 'x')) {
110 		x = args_percentage(args, 'x', 0, INT_MAX, w->sx, &cause);
111 		if (cause != NULL) {
112 			cmdq_error(item, "width %s", cause);
113 			free(cause);
114 			return (CMD_RETURN_ERROR);
115 		}
116 		layout_resize_pane_to(wp, LAYOUT_LEFTRIGHT, x);
117 	}
118 	if (args_has(args, 'y')) {
119 		y = args_percentage(args, 'y', 0, INT_MAX, w->sy, &cause);
120 		if (cause != NULL) {
121 			cmdq_error(item, "width %s", cause);
122 			free(cause);
123 			return (CMD_RETURN_ERROR);
124 		}
125 		layout_resize_pane_to(wp, LAYOUT_TOPBOTTOM, y);
126 	}
127 
128 	if (args_has(args, 'L'))
129 		layout_resize_pane(wp, LAYOUT_LEFTRIGHT, -adjust, 1);
130 	else if (args_has(args, 'R'))
131 		layout_resize_pane(wp, LAYOUT_LEFTRIGHT, adjust, 1);
132 	else if (args_has(args, 'U'))
133 		layout_resize_pane(wp, LAYOUT_TOPBOTTOM, -adjust, 1);
134 	else if (args_has(args, 'D'))
135 		layout_resize_pane(wp, LAYOUT_TOPBOTTOM, adjust, 1);
136 	server_redraw_window(wl->window);
137 
138 	return (CMD_RETURN_NORMAL);
139 }
140 
141 static void
142 cmd_resize_pane_mouse_update(struct client *c, struct mouse_event *m)
143 {
144 	struct winlink		*wl;
145 	struct window		*w;
146 	u_int			 y, ly, x, lx;
147 	static const int         offsets[][2] = {
148 	    { 0, 0 }, { 0, 1 }, { 1, 0 }, { 0, -1 }, { -1, 0 },
149 	};
150 	struct layout_cell	*cells[nitems(offsets)], *lc;
151 	u_int			 ncells = 0, i, j, resizes = 0;
152 	enum layout_type	 type;
153 
154 	wl = cmd_mouse_window(m, NULL);
155 	if (wl == NULL) {
156 		c->tty.mouse_drag_update = NULL;
157 		return;
158 	}
159 	w = wl->window;
160 
161 	y = m->y + m->oy; x = m->x + m->ox;
162 	if (m->statusat == 0 && y >= m->statuslines)
163 		y -= m->statuslines;
164 	else if (m->statusat > 0 && y >= (u_int)m->statusat)
165 		y = m->statusat - 1;
166 	ly = m->ly + m->oy; lx = m->lx + m->ox;
167 	if (m->statusat == 0 && ly >= m->statuslines)
168 		ly -= m->statuslines;
169 	else if (m->statusat > 0 && ly >= (u_int)m->statusat)
170 		ly = m->statusat - 1;
171 
172 	for (i = 0; i < nitems(cells); i++) {
173 		lc = layout_search_by_border(w->layout_root, lx + offsets[i][0],
174 		    ly + offsets[i][1]);
175 		if (lc == NULL)
176 			continue;
177 
178 		for (j = 0; j < ncells; j++) {
179 			if (cells[j] == lc) {
180 				lc = NULL;
181 				break;
182 			}
183 		}
184 		if (lc == NULL)
185 			continue;
186 
187 		cells[ncells] = lc;
188 		ncells++;
189 	}
190 	if (ncells == 0)
191 		return;
192 
193 	for (i = 0; i < ncells; i++) {
194 		type = cells[i]->parent->type;
195 		if (y != ly && type == LAYOUT_TOPBOTTOM) {
196 			layout_resize_layout(w, cells[i], type, y - ly, 0);
197 			resizes++;
198 		} else if (x != lx && type == LAYOUT_LEFTRIGHT) {
199 			layout_resize_layout(w, cells[i], type, x - lx, 0);
200 			resizes++;
201 		}
202 	}
203 	if (resizes != 0)
204 		server_redraw_window(w);
205 }
206