xref: /openbsd/usr.bin/tmux/cmd-resize-window.c (revision 274d7c50)
1 /* $OpenBSD: cmd-resize-window.c,v 1.2 2019/09/23 15:41:11 nicm Exp $ */
2 
3 /*
4  * Copyright (c) 2018 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 
23 #include "tmux.h"
24 
25 /*
26  * Increase or decrease window size.
27  */
28 
29 static enum cmd_retval	cmd_resize_window_exec(struct cmd *,
30 			    struct cmdq_item *);
31 
32 const struct cmd_entry cmd_resize_window_entry = {
33 	.name = "resize-window",
34 	.alias = "resizew",
35 
36 	.args = { "aADLRt:Ux:y:", 0, 1 },
37 	.usage = "[-aADLRU] [-x width] [-y height] " CMD_TARGET_WINDOW_USAGE " "
38 		 "[adjustment]",
39 
40 	.target = { 't', CMD_FIND_WINDOW, 0 },
41 
42 	.flags = CMD_AFTERHOOK,
43 	.exec = cmd_resize_window_exec
44 };
45 
46 static enum cmd_retval
47 cmd_resize_window_exec(struct cmd *self, struct cmdq_item *item)
48 {
49 	struct args		*args = self->args;
50 	struct winlink		*wl = item->target.wl;
51 	struct window		*w = wl->window;
52 	struct session		*s = item->target.s;
53 	const char	       	*errstr;
54 	char			*cause;
55 	u_int			 adjust, sx, sy;
56 
57 	if (args->argc == 0)
58 		adjust = 1;
59 	else {
60 		adjust = strtonum(args->argv[0], 1, INT_MAX, &errstr);
61 		if (errstr != NULL) {
62 			cmdq_error(item, "adjustment %s", errstr);
63 			return (CMD_RETURN_ERROR);
64 		}
65 	}
66 
67 	sx = w->sx;
68 	sy = w->sy;
69 
70 	if (args_has(args, 'x')) {
71 		sx = args_strtonum(args, 'x', WINDOW_MINIMUM, WINDOW_MAXIMUM,
72 		    &cause);
73 		if (cause != NULL) {
74 			cmdq_error(item, "width %s", cause);
75 			free(cause);
76 			return (CMD_RETURN_ERROR);
77 		}
78 	}
79 	if (args_has(args, 'y')) {
80 		sy = args_strtonum(args, 'y', WINDOW_MINIMUM, WINDOW_MAXIMUM,
81 		    &cause);
82 		if (cause != NULL) {
83 			cmdq_error(item, "height %s", cause);
84 			free(cause);
85 			return (CMD_RETURN_ERROR);
86 		}
87 	}
88 
89 	if (args_has(args, 'L')) {
90 		if (sx >= adjust)
91 			sx -= adjust;
92 	} else if (args_has(args, 'R'))
93 		sx += adjust;
94 	else if (args_has(args, 'U')) {
95 		if (sy >= adjust)
96 			sy -= adjust;
97 	} else if (args_has(args, 'D'))
98 		sy += adjust;
99 
100 	if (args_has(args, 'A'))
101 		default_window_size(NULL, s, w, &sx, &sy, WINDOW_SIZE_LARGEST);
102 	else if (args_has(args, 'a'))
103 		default_window_size(NULL, s, w, &sx, &sy, WINDOW_SIZE_SMALLEST);
104 
105 	options_set_number(w->options, "window-size", WINDOW_SIZE_MANUAL);
106 	resize_window(w, sx, sy);
107 
108 	return (CMD_RETURN_NORMAL);
109 }
110