1 /* $OpenBSD$ */
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 enum cmd_retval	 cmd_move_window_exec(struct cmd *, struct cmd_q *);
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 enum cmd_retval
cmd_move_window_exec(struct cmd * self,struct cmd_q * cmdq)60 cmd_move_window_exec(struct cmd *self, struct cmd_q *cmdq)
61 {
62 	struct args	*args = self->args;
63 	struct session	*src = cmdq->state.sflag.s;
64 	struct session	*dst = cmdq->state.tflag.s;
65 	struct winlink	*wl = cmdq->state.sflag.wl;
66 	char		*cause;
67 	int		 idx = cmdq->state.tflag.idx, kflag, dflag, sflag;
68 
69 	kflag = args_has(self->args, 'k');
70 	dflag = args_has(self->args, 'd');
71 
72 	if (args_has(args, 'r')) {
73 		session_renumber_windows(dst);
74 		recalculate_sizes();
75 
76 		return (CMD_RETURN_NORMAL);
77 	}
78 
79 	kflag = args_has(self->args, 'k');
80 	dflag = args_has(self->args, 'd');
81 	sflag = args_has(self->args, 's');
82 
83 	if (args_has(self->args, 'a')) {
84 		if ((idx = winlink_shuffle_up(dst, dst->curw)) == -1)
85 			return (CMD_RETURN_ERROR);
86 	}
87 
88 	if (server_link_window(src, wl, dst, idx, kflag, !dflag,
89 	    &cause) != 0) {
90 		cmdq_error(cmdq, "can't link window: %s", cause);
91 		free(cause);
92 		return (CMD_RETURN_ERROR);
93 	}
94 	if (self->entry == &cmd_move_window_entry)
95 		server_unlink_window(src, wl);
96 
97 	/*
98 	 * Renumber the winlinks in the src session only, the destination
99 	 * session already has the correct winlink id to us, either
100 	 * automatically or specified by -s.
101 	 */
102 	if (!sflag && options_get_number(src->options, "renumber-windows"))
103 		session_renumber_windows(src);
104 
105 	recalculate_sizes();
106 
107 	return (CMD_RETURN_NORMAL);
108 }
109