xref: /openbsd-src/usr.bin/tmux/cmd-resize-pane.c (revision 43003dfe3ad45d1698bed8a37f2b0f5b14f20d4f)
1 /* $OpenBSD: cmd-resize-pane.c,v 1.6 2009/07/30 13:45:56 nicm Exp $ */
2 
3 /*
4  * Copyright (c) 2009 Nicholas Marriott <nicm@users.sourceforge.net>
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 pane size.
27  */
28 
29 void	cmd_resize_pane_init(struct cmd *, int);
30 int	cmd_resize_pane_exec(struct cmd *, struct cmd_ctx *);
31 
32 const struct cmd_entry cmd_resize_pane_entry = {
33 	"resize-pane", "resizep",
34 	"[-DU] " CMD_TARGET_PANE_USAGE " [adjustment]",
35 	CMD_ARG01,
36 	CMD_CHFLAG('D')|CMD_CHFLAG('L')|CMD_CHFLAG('R')|CMD_CHFLAG('U'),
37 	cmd_resize_pane_init,
38 	cmd_target_parse,
39 	cmd_resize_pane_exec,
40 	cmd_target_free,
41 	cmd_target_print
42 };
43 
44 void
45 cmd_resize_pane_init(struct cmd *self, int key)
46 {
47 	struct cmd_target_data	*data;
48 
49 	cmd_target_init(self, key);
50 	data = self->data;
51 
52 	if (key == (KEYC_UP | KEYC_CTRL))
53 		data->chflags |= CMD_CHFLAG('U');
54 	if (key == (KEYC_DOWN | KEYC_CTRL))
55 		data->chflags |= CMD_CHFLAG('D');
56 	if (key == (KEYC_LEFT | KEYC_CTRL))
57 		data->chflags |= CMD_CHFLAG('L');
58 	if (key == (KEYC_RIGHT | KEYC_CTRL))
59 		data->chflags |= CMD_CHFLAG('R');
60 
61 	if (key == (KEYC_UP | KEYC_ESCAPE)) {
62 		data->chflags |= CMD_CHFLAG('U');
63 		data->arg = xstrdup("5");
64 	}
65 	if (key == (KEYC_DOWN | KEYC_ESCAPE)) {
66 		data->chflags |= CMD_CHFLAG('D');
67 		data->arg = xstrdup("5");
68 	}
69 	if (key == (KEYC_LEFT | KEYC_ESCAPE)) {
70 		data->chflags |= CMD_CHFLAG('L');
71 		data->arg = xstrdup("5");
72 	}
73 	if (key == (KEYC_RIGHT | KEYC_ESCAPE)) {
74 		data->chflags |= CMD_CHFLAG('R');
75 		data->arg = xstrdup("5");
76 	}
77 }
78 
79 int
80 cmd_resize_pane_exec(struct cmd *self, struct cmd_ctx *ctx)
81 {
82 	struct cmd_target_data	*data = self->data;
83 	struct winlink		*wl;
84 	const char	       	*errstr;
85 	struct window_pane	*wp;
86 	u_int			 adjust;
87 
88 	if ((wl = cmd_find_pane(ctx, data->target, NULL, &wp)) == NULL)
89 		return (-1);
90 
91 	if (data->arg == NULL)
92 		adjust = 1;
93 	else {
94 		adjust = strtonum(data->arg, 1, INT_MAX, &errstr);
95 		if (errstr != NULL) {
96 			ctx->error(ctx, "adjustment %s: %s", errstr, data->arg);
97 			return (-1);
98 		}
99 	}
100 
101 	if (data->chflags & (CMD_CHFLAG('L')|CMD_CHFLAG('R'))) {
102 		if (data->chflags & CMD_CHFLAG('L'))
103 			adjust = -adjust;
104 		layout_resize_pane(wp, LAYOUT_LEFTRIGHT, adjust);
105 	} else {
106 		if (data->chflags & CMD_CHFLAG('U'))
107 			adjust = -adjust;
108 		layout_resize_pane(wp, LAYOUT_TOPBOTTOM, adjust);
109 	}
110 	server_redraw_window(wl->window);
111 
112 	return (0);
113 }
114