blob: d6839ea33b75c89d54cda0eddd024b6f2827b7c3 [file] [log] [blame] [raw]
Nicholas Marriott93230a62009-01-19 18:23:40 +00001/* $Id: cmd-resize-pane-down.c,v 1.6 2009-01-19 18:23:40 nicm Exp $ */
Nicholas Marriottc35a50b2009-01-12 19:23:14 +00002
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/*
Nicholas Marriottb4ac8c12009-01-14 19:29:32 +000026 * Decrease pane size.
Nicholas Marriottc35a50b2009-01-12 19:23:14 +000027 */
28
Nicholas Marriott0a99ba02009-01-14 21:08:52 +000029void cmd_resize_pane_down_init(struct cmd *, int);
Nicholas Marriott93230a62009-01-19 18:23:40 +000030int cmd_resize_pane_down_exec(struct cmd *, struct cmd_ctx *);
Nicholas Marriottc35a50b2009-01-12 19:23:14 +000031
32const struct cmd_entry cmd_resize_pane_down_entry = {
Nicholas Marriottb4ac8c12009-01-14 19:29:32 +000033 "resize-pane-down", "resizep-down",
34 CMD_PANE_WINDOW_USAGE " [adjustment]",
Nicholas Marriott78c96752009-01-14 22:16:57 +000035 CMD_ARG01|CMD_CANREPEAT,
Nicholas Marriott0a99ba02009-01-14 21:08:52 +000036 cmd_resize_pane_down_init,
Nicholas Marriottb4ac8c12009-01-14 19:29:32 +000037 cmd_pane_parse,
Nicholas Marriottc35a50b2009-01-12 19:23:14 +000038 cmd_resize_pane_down_exec,
Nicholas Marriottb4ac8c12009-01-14 19:29:32 +000039 cmd_pane_send,
40 cmd_pane_recv,
41 cmd_pane_free,
42 cmd_pane_print
Nicholas Marriottc35a50b2009-01-12 19:23:14 +000043};
44
45void
Nicholas Marriott0a99ba02009-01-14 21:08:52 +000046cmd_resize_pane_down_init(struct cmd *self, int key)
47{
48 struct cmd_pane_data *data;
49
50 cmd_pane_init(self, key);
51 data = self->data;
52
53 if (key == KEYC_ADDESC(KEYC_DOWN))
54 data->arg = xstrdup("5");
55}
56
Nicholas Marriott93230a62009-01-19 18:23:40 +000057int
Nicholas Marriottc35a50b2009-01-12 19:23:14 +000058cmd_resize_pane_down_exec(struct cmd *self, struct cmd_ctx *ctx)
59{
Nicholas Marriottb4ac8c12009-01-14 19:29:32 +000060 struct cmd_pane_data *data = self->data;
Nicholas Marriottc35a50b2009-01-12 19:23:14 +000061 struct winlink *wl;
Nicholas Marriottc35a50b2009-01-12 19:23:14 +000062 const char *errstr;
Nicholas Marriottb4ac8c12009-01-14 19:29:32 +000063 struct window_pane *wp, *wq;
64 u_int adjust;
Nicholas Marriottc35a50b2009-01-12 19:23:14 +000065
66 if ((wl = cmd_find_window(ctx, data->target, NULL)) == NULL)
Nicholas Marriott93230a62009-01-19 18:23:40 +000067 return (-1);
Nicholas Marriottc35a50b2009-01-12 19:23:14 +000068 if (data->pane == -1)
69 wp = wl->window->active;
70 else {
Nicholas Marriottb4ac8c12009-01-14 19:29:32 +000071 wp = window_pane_at_index(wl->window, data->pane);
72 if (wp == NULL) {
Nicholas Marriottc35a50b2009-01-12 19:23:14 +000073 ctx->error(ctx, "no pane: %d", data->pane);
Nicholas Marriott93230a62009-01-19 18:23:40 +000074 return (-1);
Nicholas Marriottc35a50b2009-01-12 19:23:14 +000075 }
Nicholas Marriottc35a50b2009-01-12 19:23:14 +000076 }
Nicholas Marriottc35a50b2009-01-12 19:23:14 +000077
78 if (data->arg == NULL)
79 adjust = 1;
80 else {
Nicholas Marriottb4ac8c12009-01-14 19:29:32 +000081 adjust = strtonum(data->arg, 1, INT_MAX, &errstr);
Nicholas Marriottc35a50b2009-01-12 19:23:14 +000082 if (errstr != NULL) {
83 ctx->error(ctx, "adjustment %s: %s", errstr, data->arg);
Nicholas Marriott93230a62009-01-19 18:23:40 +000084 return (-1);
Nicholas Marriottc35a50b2009-01-12 19:23:14 +000085 }
86 }
87
Nicholas Marriottb4ac8c12009-01-14 19:29:32 +000088 /*
89 * If this is not the last window, keep trying to increase size and
90 * remove it from the next windows. If it is the last, do so on the
91 * previous window.
92 */
93 if (TAILQ_NEXT(wp, entry) == NULL) {
94 if (wp == TAILQ_FIRST(&wl->window->panes)) {
95 /* Only one pane. */
Nicholas Marriott93230a62009-01-19 18:23:40 +000096 return (0);
Nicholas Marriottb4ac8c12009-01-14 19:29:32 +000097 }
98 wp = TAILQ_PREV(wp, window_panes, entry);
99 }
100 while (adjust-- > 0) {
101 wq = wp;
102 while ((wq = TAILQ_NEXT(wq, entry)) != NULL) {
103 if (wq->sy > PANE_MINIMUM) {
104 window_pane_resize(wq, wq->sx, wq->sy - 1);
105 break;
106 }
107 }
108 if (wq == NULL)
109 break;
110 window_pane_resize(wp, wp->sx, wp->sy + 1);
Nicholas Marriottc35a50b2009-01-12 19:23:14 +0000111
Nicholas Marriottb4ac8c12009-01-14 19:29:32 +0000112 }
113 window_update_panes(wl->window);
Nicholas Marriottc35a50b2009-01-12 19:23:14 +0000114
115 server_redraw_window(wl->window);
116
Nicholas Marriott93230a62009-01-19 18:23:40 +0000117 return (0);
Nicholas Marriottc35a50b2009-01-12 19:23:14 +0000118}