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
/// Select the last (previously selected) pane
///
/// # Manual
///
/// tmux ^3.1:
/// ```text
/// last-pane [-deZ] [-t target-window]
/// (alias: lastp)
/// ```
///
/// tmux ^2.0:
/// ```text
/// last-pane [-de] [-t target-window]
/// (alias: lastp)
/// ```
///
/// tmux ^1.4:
/// ```text
/// last-pane [-t target-window]
/// (alias: lastp)
/// ```
// FIXME: versions and function parameters
#[macro_export]
macro_rules! last_pane {
    // `[-d]`
    (@cmd ($cmd:expr) -d, $($tail:tt)*) => {{
        $crate::last_pane!(@cmd ({
            $cmd.disable()
        }) $($tail)*)
    }};
    // `[-e]`
    (@cmd ($cmd:expr) -e, $($tail:tt)*) => {{
        $crate::last_pane!(@cmd ({
            $cmd.enable()
        }) $($tail)*)
    }};
    // `[-Z]`
    (@cmd ($cmd:expr) -Z, $($tail:tt)*) => {{
        $crate::last_pane!(@cmd ({
            $cmd.keep_zoomed()
        }) $($tail)*)
    }};
    // `[-t target-window]`
    (@cmd ($cmd:expr) -t $target_window:expr, $($tail:tt)*) => {{
        $crate::last_pane!(@cmd ({
            $cmd.target_window($target_window)
        }) $($tail)*)
    }};
    //(@cmd ($cmd:expr) -$unknown:tt, $($tail:tt)*) => {{
        //::std::compile_error!("unknown flag, option or parameter: {}", $unknown);
    //}};
    (@cmd ($cmd:expr)) => {{
        $cmd
    }};
    () => {{
        $crate::LastPane::new()
    }};
    (($cmd:expr), $($tail:tt)*) => {{
        $crate::last_pane!(@cmd ($cmd) $($tail)*,)
    }};
    ($($tail:tt)*) => {{
        $crate::last_pane!(@cmd ({ $crate::LastPane::new() }) $($tail)*,)
    }};
}

#[test]
fn last_pane_macro() {
    use crate::TargetWindow;
    use std::borrow::Cow;

    // Select the last (previously selected) pane
    //
    // # Manual
    //
    // tmux ^3.1:
    // ```text
    // last-pane [-deZ] [-t target-window]
    // (alias: lastp)
    // ```
    //
    // tmux ^2.0:
    // ```text
    // last-pane [-de] [-t target-window]
    // (alias: lastp)
    // ```
    //
    // tmux ^1.4:
    // ```text
    // last-pane [-t target-window]
    // (alias: lastp)
    // ```
    let target_window = TargetWindow::Raw("1").to_string();

    let last_pane = last_pane!();
    #[cfg(feature = "tmux_2_0")]
    let last_pane = last_pane!((last_pane), -d);
    #[cfg(feature = "tmux_2_0")]
    let last_pane = last_pane!((last_pane), -e);
    #[cfg(feature = "tmux_3_1")]
    let last_pane = last_pane!((last_pane), -Z);
    #[cfg(feature = "tmux_1_4")]
    let last_pane = last_pane!((last_pane), -t & target_window);

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

    let mut s = Vec::new();
    s.push(cmd);
    #[cfg(feature = "tmux_2_0")]
    s.push("-d");
    #[cfg(feature = "tmux_2_0")]
    s.push("-e");
    #[cfg(feature = "tmux_3_1")]
    s.push("-Z");
    #[cfg(feature = "tmux_1_4")]
    s.extend_from_slice(&["-t", "1"]);
    let s: Vec<Cow<str>> = s.into_iter().map(|a| a.into()).collect();

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

    assert_eq!(last_pane, s);
}