nvim_oxi_api/opts/
set_keymap.rs

1#[cfg(not(feature = "neovim-0-10"))] // 0nly on 0.9.
2use types::Object;
3#[cfg(feature = "neovim-0-10")] // On 0.10 and nightly.
4use types::{Boolean, LuaRef};
5
6use crate::ToFunction;
7
8/// Options passed to [`Buffer::set_keymap()`](crate::Buffer::set_keymap)
9/// and [`set_keymap()`](crate::set_keymap).
10#[cfg(feature = "neovim-0-10")] // On 0.10 and nightly.
11#[derive(Clone, Debug, Default, PartialEq, macros::OptsBuilder)]
12#[repr(C)]
13pub struct SetKeymapOpts {
14    #[builder(mask)]
15    mask: u64,
16
17    /// Whether the right-hand side of the mapping shouldn't be remappable.
18    #[builder(argtype = "bool")]
19    noremap: Boolean,
20
21    /// For buffer-local mappings, whether Neovim should wait for more
22    /// characters to be typed if there's a global mapping that could also
23    /// match. See `:h map-nowait` for more details.
24    #[builder(argtype = "bool")]
25    nowait: Boolean,
26
27    /// Whether the keymap should be silent.
28    #[builder(argtype = "bool")]
29    silent: Boolean,
30
31    /// Whether to remap characters in the right-hand side by expanding the
32    /// `<sid>` script tag.
33    #[builder(argtype = "bool")]
34    script: Boolean,
35
36    /// Whether the keymap argument is an expression.
37    #[builder(argtype = "bool")]
38    expr: Boolean,
39
40    /// If `true` setting the keymap fill fail if another keymap with the same
41    /// left-hand side already exists.
42    #[builder(argtype = "bool")]
43    unique: Boolean,
44
45    /// A function to call when the mapping is executed.
46    #[builder(
47        generics = "F: ToFunction<(), ()>",
48        argtype = "F",
49        inline = "{0}.into_luaref()"
50    )]
51    callback: LuaRef,
52
53    /// A description for the keymap.
54    #[builder(
55        generics = "D: Into<types::String>",
56        argtype = "D",
57        inline = "{0}.into()"
58    )]
59    desc: types::String,
60
61    /// When [`expr`](SetKeymapOptsBuilder::expr) is `true`, this option can be
62    /// used to replace the keycodes in the resulting string (see
63    /// [replace_termcodes()](crate::replace_termcodes)).
64    #[builder(argtype = "bool")]
65    replace_keycodes: Boolean,
66}
67
68/// Options passed to [`Buffer::set_keymap()`](crate::Buffer::set_keymap)
69/// and [`set_keymap()`](crate::set_keymap).
70#[cfg(not(feature = "neovim-0-10"))] // 0nly on 0.9.
71#[derive(Clone, Debug, Default, PartialEq)]
72#[repr(C)]
73pub struct SetKeymapOpts {
74    desc: Object,
75    expr: Object,
76    script: Object,
77    silent: Object,
78    unique: Object,
79    nowait: Object,
80    noremap: Object,
81    callback: Object,
82    replace_keycodes: Object,
83}
84
85#[cfg(not(feature = "neovim-0-10"))] // 0nly on 0.9.
86impl SetKeymapOpts {
87    #[inline(always)]
88    /// Creates a new [`SetKeymapOptsBuilder`].
89    pub fn builder() -> SetKeymapOptsBuilder {
90        SetKeymapOptsBuilder::default()
91    }
92}
93
94#[cfg(not(feature = "neovim-0-10"))] // 0nly on 0.9.
95#[derive(Clone, Default)]
96pub struct SetKeymapOptsBuilder(SetKeymapOpts);
97
98#[cfg(not(feature = "neovim-0-10"))] // 0nly on 0.9.
99impl SetKeymapOptsBuilder {
100    /// A function to call when the mapping is executed.
101    #[inline]
102    pub fn callback<F>(&mut self, fun: F) -> &mut Self
103    where
104        F: ToFunction<(), ()>,
105    {
106        let callback = fun.into_luaref();
107        self.0.callback = Object::from_luaref(callback);
108        self
109    }
110
111    /// A description for the keymap.
112    #[inline]
113    pub fn desc(&mut self, desc: &str) -> &mut Self {
114        let desc = types::String::from(desc);
115        self.0.desc = desc.into();
116        self
117    }
118
119    /// Whether the keymap argument is an expression.
120    #[inline]
121    pub fn expr(&mut self, expr: bool) -> &mut Self {
122        self.0.expr = expr.into();
123        self
124    }
125
126    /// Whether the right-hand side of the mapping shouldn't be remappable.
127    #[inline]
128    pub fn noremap(&mut self, noremap: bool) -> &mut Self {
129        self.0.noremap = noremap.into();
130        self
131    }
132
133    /// For buffer-local mappings, whether Neovim should wait for more
134    /// characters to be typed if there's a global mapping that could also
135    /// match. See `:h map-nowait` for more details.
136    #[inline]
137    pub fn nowait(&mut self, nowait: bool) -> &mut Self {
138        self.0.nowait = nowait.into();
139        self
140    }
141
142    /// When [`expr`](SetKeymapOptsBuilder::expr) is `true`, this option can be
143    /// used to replace the keycodes in the resulting string (see
144    /// [replace_termcodes()](crate::replace_termcodes)).
145    #[inline]
146    pub fn replace_keycodes(&mut self, replace_keycodes: bool) -> &mut Self {
147        self.0.replace_keycodes = replace_keycodes.into();
148        self
149    }
150
151    /// Whether to remap characters in the right-hand side by expanding the
152    /// `<sid>` script tag.
153    #[inline]
154    pub fn script(&mut self, script: bool) -> &mut Self {
155        self.0.script = script.into();
156        self
157    }
158
159    /// Whether the keymap should be silent.
160    #[inline]
161    pub fn silent(&mut self, silent: bool) -> &mut Self {
162        self.0.silent = silent.into();
163        self
164    }
165
166    /// If `true` setting the keymap fill fail if another keymap with the same
167    /// left-hand side already exists.
168    #[inline]
169    pub fn unique(&mut self, unique: bool) -> &mut Self {
170        self.0.unique = unique.into();
171        self
172    }
173
174    #[inline]
175    pub fn build(&mut self) -> SetKeymapOpts {
176        std::mem::take(&mut self.0)
177    }
178}