xref: /openbsd-src/usr.bin/tmux/cmd-move-window.c (revision d4741794dd2f512d997014f8bd85fbb24d935059)
1 /* $OpenBSD: cmd-move-window.c,v 1.28 2016/10/16 19:04:05 nicm Exp $ */
2 
3 /*
4  * Copyright (c) 2008 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  * Move a window.
27  */
28 
29 static enum cmd_retval	cmd_move_window_exec(struct cmd *, struct cmdq_item *);
30 
31 const struct cmd_entry cmd_move_window_entry = {
32 	.name = "move-window",
33 	.alias = "movew",
34 
35 	.args = { "adkrs:t:", 0, 0 },
36 	.usage = "[-dkr] " CMD_SRCDST_WINDOW_USAGE,
37 
38 	.sflag = CMD_WINDOW,
39 	.tflag = CMD_MOVEW_R,
40 
41 	.flags = 0,
42 	.exec = cmd_move_window_exec
43 };
44 
45 const struct cmd_entry cmd_link_window_entry = {
46 	.name = "link-window",
47 	.alias = "linkw",
48 
49 	.args = { "adks:t:", 0, 0 },
50 	.usage = "[-dk] " CMD_SRCDST_WINDOW_USAGE,
51 
52 	.sflag = CMD_WINDOW,
53 	.tflag = CMD_WINDOW_INDEX,
54 
55 	.flags = 0,
56 	.exec = cmd_move_window_exec
57 };
58 
59 static enum cmd_retval
60 cmd_move_window_exec(struct cmd *self, struct cmdq_item *item)
61 {
62 	struct args	*args = self->args;
63 	struct session	*src = item->state.sflag.s;
64 	struct session	*dst = item->state.tflag.s;
65 	struct winlink	*wl = item->state.sflag.wl;
66 	char		*cause;
67 	int		 idx = item->state.tflag.idx, kflag, dflag, sflag;
68 
69 	if (args_has(args, 'r')) {
70 		session_renumber_windows(dst);
71 		recalculate_sizes();
72 
73 		return (CMD_RETURN_NORMAL);
74 	}
75 
76 	kflag = args_has(self->args, 'k');
77 	dflag = args_has(self->args, 'd');
78 	sflag = args_has(self->args, 's');
79 
80 	if (args_has(self->args, 'a')) {
81 		if ((idx = winlink_shuffle_up(dst, dst->curw)) == -1)
82 			return (CMD_RETURN_ERROR);
83 	}
84 
85 	if (server_link_window(src, wl, dst, idx, kflag, !dflag,
86 	    &cause) != 0) {
87 		cmdq_error(item, "can't link window: %s", cause);
88 		free(cause);
89 		return (CMD_RETURN_ERROR);
90 	}
91 	if (self->entry == &cmd_move_window_entry)
92 		server_unlink_window(src, wl);
93 
94 	/*
95 	 * Renumber the winlinks in the src session only, the destination
96 	 * session already has the correct winlink id to us, either
97 	 * automatically or specified by -s.
98 	 */
99 	if (!sflag && options_get_number(src->options, "renumber-windows"))
100 		session_renumber_windows(src);
101 
102 	recalculate_sizes();
103 
104 	return (CMD_RETURN_NORMAL);
105 }
106