1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/// Like join-pane, but `src-pane` and `dst-pane` may belong to the same window
///
/// # Manual
///
/// tmux ^3.1:
/// ```text
/// move-pane [-bdhv] [-l size] [-s src-pane] [-t dst-pane]
/// (alias: movep)
/// ```
///
/// tmux ^1.7:
/// ```text
/// move-pane [-bdhv] [-l size | -p percentage] [-s src-pane] [-t dst-pane]
/// (alias: movep)
/// ```
#[macro_export]
macro_rules! move_pane {
    // `[-b]` - cause src-pane to be joined to left of or above dst-pane
    (@cmd ($cmd:expr) -b, $($tail:tt)*) => {{
        $crate::move_pane!(@cmd ({
            $cmd.left_above()
        }) $($tail)*)
    }};
    // `[-d]` -
    (@cmd ($cmd:expr) -d, $($tail:tt)*) => {{
        $crate::move_pane!(@cmd ({
            $cmd.detached()
        }) $($tail)*)
    }};
    // `[-h]` - full height
    (@cmd ($cmd:expr) -h, $($tail:tt)*) => {{
        $crate::move_pane!(@cmd ({
            $cmd.horizontal()
        }) $($tail)*)
    }};
    // `[-v]` - full width
    (@cmd ($cmd:expr) -v, $($tail:tt)*) => {{
        $crate::move_pane!(@cmd ({
            $cmd.vertical()
        }) $($tail)*)
    }};

    // `[-l size]` - specify the size of the new pane in lines/columns
    (@cmd ($cmd:expr) -l $size:expr, $($tail:tt)*) => {{
        $crate::move_pane!(@cmd ({
            $cmd.size($size)
        }) $($tail)*)
    }};
    // `[-s src-pane]` - src-pane
    (@cmd ($cmd:expr) -s $src_pane:expr, $($tail:tt)*) => {{
        $crate::move_pane!(@cmd ({
            $cmd.src_pane($src_pane)
        }) $($tail)*)
    }};
    // `[-t dst-pane]` - dst-pane
    (@cmd ($cmd:expr) -t $dst_pane:expr, $($tail:tt)*) => {{
        $crate::move_pane!(@cmd ({
            $cmd.dst_pane($dst_pane)
        }) $($tail)*)
    }};
    //(@cmd ($cmd:expr) -$unknown:tt, $($tail:tt)*) => {{
        //::std::compile_error!("unknown flag, option or parameter: {}", $unknown);
    //}};
    (@cmd ($cmd:expr)) => {{
        $cmd
    }};
    () => {{
        $crate::MovePane::new()
    }};
    (($cmd:expr), $($tail:tt)*) => {{
        $crate::move_pane!(@cmd ($cmd) $($tail)*,)
    }};
    ($($tail:tt)*) => {{
        $crate::move_pane!(@cmd ({ $crate::MovePane::new() }) $($tail)*,)
    }};
}

#[test]
fn move_pane_macro() {
    use crate::{PaneSize, TargetPane};
    use std::borrow::Cow;

    // Like join-pane, but `src-pane` and `dst-pane` may belong to the same window
    //
    // # Manual
    //
    // tmux ^3.1:
    // ```text
    // move-pane [-bdhv] [-l size] [-s src-pane] [-t dst-pane]
    // (alias: movep)
    // ```
    //
    // tmux ^1.7:
    // ```text
    // move-pane [-bdhv] [-l size | -p percentage] [-s src-pane] [-t dst-pane]
    // (alias: movep)
    // ```
    let src_pane = TargetPane::Raw("2").to_string();
    let dst_pane = TargetPane::Raw("3").to_string();

    let move_pane = move_pane!();
    #[cfg(feature = "tmux_1_7")]
    let move_pane = move_pane!((move_pane), -b);
    #[cfg(feature = "tmux_1_7")]
    let move_pane = move_pane!((move_pane), -d);
    #[cfg(feature = "tmux_1_7")]
    let move_pane = move_pane!((move_pane), -h);
    #[cfg(feature = "tmux_1_7")]
    let move_pane = move_pane!((move_pane), -v);
    #[cfg(feature = "tmux_1_7")]
    let move_pane = move_pane!((move_pane), -l & PaneSize::Size(1));
    #[cfg(feature = "tmux_1_7")]
    let move_pane = move_pane!((move_pane), -s & src_pane);
    #[cfg(feature = "tmux_1_7")]
    let move_pane = move_pane!((move_pane), -t & dst_pane);

    #[cfg(not(feature = "cmd_alias"))]
    let cmd = "move-pane";
    #[cfg(feature = "cmd_alias")]
    let cmd = "movep";

    let mut s = Vec::new();
    s.push(cmd);
    #[cfg(feature = "tmux_1_7")]
    s.push("-b");
    #[cfg(feature = "tmux_1_7")]
    s.push("-d");
    #[cfg(feature = "tmux_1_7")]
    s.push("-h");
    #[cfg(feature = "tmux_1_7")]
    s.push("-v");
    #[cfg(feature = "tmux_1_7")]
    s.extend_from_slice(&["-l", "1"]);
    #[cfg(feature = "tmux_1_7")]
    s.extend_from_slice(&["-s", "2"]);
    #[cfg(feature = "tmux_1_7")]
    s.extend_from_slice(&["-t", "3"]);
    let s: Vec<Cow<str>> = s.into_iter().map(|a| a.into()).collect();

    let move_pane = move_pane.build().to_vec();

    assert_eq!(move_pane, s);
}