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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#[cfg(not(feature = "neovim-0-10"))] // 0nly on 0.9.
use types::Object;
#[cfg(feature = "neovim-0-10")] // On 0.10 and nightly.
use types::{Boolean, LuaRef};

use crate::ToFunction;

/// Options passed to [`Buffer::set_keymap()`](crate::Buffer::set_keymap)
/// and [`set_keymap()`](crate::set_keymap).
#[cfg(feature = "neovim-0-10")] // On 0.10 and nightly.
#[derive(Clone, Debug, Default, PartialEq, macros::OptsBuilder)]
#[repr(C)]
pub struct SetKeymapOpts {
    #[builder(mask)]
    mask: u64,

    /// Whether the right-hand side of the mapping shouldn't be remappable.
    #[builder(argtype = "bool")]
    noremap: Boolean,

    /// For buffer-local mappings, whether Neovim should wait for more
    /// characters to be typed if there's a global mapping that could also
    /// match. See `:h map-nowait` for more details.
    #[builder(argtype = "bool")]
    nowait: Boolean,

    /// Whether the keymap should be silent.
    #[builder(argtype = "bool")]
    silent: Boolean,

    /// Whether to remap characters in the right-hand side by expanding the
    /// `<sid>` script tag.
    #[builder(argtype = "bool")]
    script: Boolean,

    /// Whether the keymap argument is an expression.
    #[builder(argtype = "bool")]
    expr: Boolean,

    /// If `true` setting the keymap fill fail if another keymap with the same
    /// left-hand side already exists.
    #[builder(argtype = "bool")]
    unique: Boolean,

    /// A function to call when the mapping is executed.
    #[builder(
        generics = "F: ToFunction<(), ()>",
        argtype = "F",
        inline = "{0}.into_luaref()"
    )]
    callback: LuaRef,

    /// A description for the keymap.
    #[builder(
        generics = "D: Into<types::String>",
        argtype = "D",
        inline = "{0}.into()"
    )]
    desc: types::String,

    /// When [`expr`](SetKeymapOptsBuilder::expr) is `true`, this option can be
    /// used to replace the keycodes in the resulting string (see
    /// [replace_termcodes()](crate::replace_termcodes)).
    #[builder(argtype = "bool")]
    replace_keycodes: Boolean,
}

/// Options passed to [`Buffer::set_keymap()`](crate::Buffer::set_keymap)
/// and [`set_keymap()`](crate::set_keymap).
#[cfg(not(feature = "neovim-0-10"))] // 0nly on 0.9.
#[derive(Clone, Debug, Default, PartialEq)]
#[repr(C)]
pub struct SetKeymapOpts {
    desc: Object,
    expr: Object,
    script: Object,
    silent: Object,
    unique: Object,
    nowait: Object,
    noremap: Object,
    callback: Object,
    replace_keycodes: Object,
}

#[cfg(not(feature = "neovim-0-10"))] // 0nly on 0.9.
impl SetKeymapOpts {
    #[inline(always)]
    /// Creates a new [`SetKeymapOptsBuilder`].
    pub fn builder() -> SetKeymapOptsBuilder {
        SetKeymapOptsBuilder::default()
    }
}

#[cfg(not(feature = "neovim-0-10"))] // 0nly on 0.9.
#[derive(Clone, Default)]
pub struct SetKeymapOptsBuilder(SetKeymapOpts);

#[cfg(not(feature = "neovim-0-10"))] // 0nly on 0.9.
impl SetKeymapOptsBuilder {
    /// A function to call when the mapping is executed.
    #[inline]
    pub fn callback<F>(&mut self, fun: F) -> &mut Self
    where
        F: ToFunction<(), ()>,
    {
        let callback = fun.into_luaref();
        self.0.callback = Object::from_luaref(callback);
        self
    }

    /// A description for the keymap.
    #[inline]
    pub fn desc(&mut self, desc: &str) -> &mut Self {
        let desc = types::String::from(desc);
        self.0.desc = desc.into();
        self
    }

    /// Whether the keymap argument is an expression.
    #[inline]
    pub fn expr(&mut self, expr: bool) -> &mut Self {
        self.0.expr = expr.into();
        self
    }

    /// Whether the right-hand side of the mapping shouldn't be remappable.
    #[inline]
    pub fn noremap(&mut self, noremap: bool) -> &mut Self {
        self.0.noremap = noremap.into();
        self
    }

    /// For buffer-local mappings, whether Neovim should wait for more
    /// characters to be typed if there's a global mapping that could also
    /// match. See `:h map-nowait` for more details.
    #[inline]
    pub fn nowait(&mut self, nowait: bool) -> &mut Self {
        self.0.nowait = nowait.into();
        self
    }

    /// When [`expr`](SetKeymapOptsBuilder::expr) is `true`, this option can be
    /// used to replace the keycodes in the resulting string (see
    /// [replace_termcodes()](crate::replace_termcodes)).
    #[inline]
    pub fn replace_keycodes(&mut self, replace_keycodes: bool) -> &mut Self {
        self.0.replace_keycodes = replace_keycodes.into();
        self
    }

    /// Whether to remap characters in the right-hand side by expanding the
    /// `<sid>` script tag.
    #[inline]
    pub fn script(&mut self, script: bool) -> &mut Self {
        self.0.script = script.into();
        self
    }

    /// Whether the keymap should be silent.
    #[inline]
    pub fn silent(&mut self, silent: bool) -> &mut Self {
        self.0.silent = silent.into();
        self
    }

    /// If `true` setting the keymap fill fail if another keymap with the same
    /// left-hand side already exists.
    #[inline]
    pub fn unique(&mut self, unique: bool) -> &mut Self {
        self.0.unique = unique.into();
        self
    }

    #[inline]
    pub fn build(&mut self) -> SetKeymapOpts {
        std::mem::take(&mut self.0)
    }
}