nvim_api/opts/
set_keymap.rs

1use derive_builder::Builder;
2use nvim_types::{self as nvim, NonOwning, Object};
3
4use crate::ToFunction;
5
6/// Options passed to [`Buffer::set_keymap`](crate::Buffer::set_keymap)
7/// and [`api::set_keymap`](crate::set_keymap).
8#[derive(Clone, Debug, Default, PartialEq, Builder)]
9#[builder(default, build_fn(private, name = "fallible_build"))]
10pub struct SetKeymapOpts {
11    #[builder(setter(custom))]
12    callback: Object,
13
14    #[builder(setter(custom))]
15    desc: Object,
16
17    /// Whether the keymap argument is an expression.
18    #[builder(setter(strip_option))]
19    expr: Option<bool>,
20
21    /// Whether the right-hand side of the mapping shouldn't be remappable.
22    #[builder(setter(strip_option))]
23    noremap: Option<bool>,
24
25    /// For buffer-local mappings, whether Neovim should wait for more
26    /// characters to be typed if there's a global mapping that could also
27    /// match. See `:h map-nowait` for more details.
28    #[builder(setter(strip_option))]
29    nowait: Option<bool>,
30
31    /// When [`expr`](SetKeymapOptsBuilder::expr) is `true`, this option can be
32    /// used to replace the keycodes in the resulting string (see
33    /// [nvim_oxi::api::replace_termcodes](crate::replace_termcodes)).
34    #[cfg(any(feature = "neovim-0-8", feature = "neovim-nightly"))]
35    #[cfg_attr(
36        docsrs,
37        doc(cfg(any(feature = "neovim-0-8", feature = "neovim-nightly")))
38    )]
39    #[builder(setter(strip_option))]
40    replace_keycodes: Option<bool>,
41
42    /// Whether to remap characters in the right-hand side by expanding the
43    /// `<sid>` script tag.
44    #[builder(setter(strip_option))]
45    script: Option<bool>,
46
47    /// Whether the keymap should be silent.
48    #[builder(setter(strip_option))]
49    silent: Option<bool>,
50
51    /// If `true` setting the keymap fill fail if another keymap with the same
52    /// left-hand side already exists.
53    #[builder(setter(strip_option))]
54    unique: Option<bool>,
55}
56
57impl SetKeymapOpts {
58    #[inline(always)]
59    /// Creates a new [`SetKeymapOptsBuilder`].
60    pub fn builder() -> SetKeymapOptsBuilder {
61        SetKeymapOptsBuilder::default()
62    }
63}
64
65impl SetKeymapOptsBuilder {
66    /// A function to call when the mapping is executed.
67    pub fn callback<F>(&mut self, fun: F) -> &mut Self
68    where
69        F: ToFunction<(), ()>,
70    {
71        self.callback = Some(fun.to_object());
72        self
73    }
74
75    /// A description for the keymap.
76    pub fn desc(&mut self, desc: &str) -> &mut Self {
77        self.desc = Some(nvim::String::from(desc).into());
78        self
79    }
80
81    pub fn build(&mut self) -> SetKeymapOpts {
82        self.fallible_build().expect("never fails, all fields have defaults")
83    }
84}
85
86#[derive(Default)]
87#[allow(non_camel_case_types)]
88#[repr(C)]
89pub(crate) struct KeyDict_keymap<'a> {
90    desc: NonOwning<'a, Object>,
91    expr: Object,
92    script: Object,
93    silent: Object,
94    unique: Object,
95    nowait: Object,
96    noremap: Object,
97    callback: NonOwning<'a, Object>,
98    #[cfg(any(feature = "neovim-0-8", feature = "neovim-nightly"))]
99    replace_keycodes: Object,
100}
101
102impl<'a> From<&'a SetKeymapOpts> for KeyDict_keymap<'a> {
103    fn from(opts: &'a SetKeymapOpts) -> Self {
104        Self {
105            desc: opts.desc.non_owning(),
106            expr: opts.expr.into(),
107            script: opts.script.into(),
108            silent: opts.silent.into(),
109            unique: opts.unique.into(),
110            nowait: opts.nowait.into(),
111            noremap: opts.noremap.into(),
112            callback: opts.callback.non_owning(),
113            #[cfg(any(feature = "neovim-0-8", feature = "neovim-nightly"))]
114            replace_keycodes: opts.replace_keycodes.into(),
115        }
116    }
117}