nvim_oxi_api/
command.rs

1use types::{self as nvim, conversion::FromObject};
2
3use super::opts::*;
4use crate::choose;
5use crate::ffi::command::*;
6use crate::trait_utils::{StringOrFunction, SuperIterator};
7use crate::types::*;
8use crate::Buffer;
9use crate::Result;
10use crate::LUA_INTERNAL_CALL;
11
12/// Binding to [`nvim_cmd()`][1].
13///
14/// Executes an Ex command. Unlike `crare::api::command` it takes a structured
15/// `CmdInfos` object instead of a string.
16///
17/// [1]: https://neovim.io/doc/user/api.html#nvim_cmd()
18pub fn cmd(infos: &CmdInfos, opts: &CmdOpts) -> Result<Option<String>> {
19    let mut err = nvim::Error::new();
20    let output = unsafe {
21        nvim_cmd(
22            LUA_INTERNAL_CALL,
23            &infos.into(),
24            opts,
25            #[cfg(feature = "neovim-0-10")] // On 0.10 and nightly.
26            types::arena(),
27            &mut err,
28        )
29    };
30    choose!(err, {
31        Ok((!output.is_empty()).then(|| output.to_string_lossy().into()))
32    })
33}
34
35/// Binding to [`nvim_create_user_command()`][1].
36///
37/// Creates a new [user command](https://neovim.io/doc/user/map.html#user-commands).
38///
39/// [1]: https://neovim.io/doc/user/api.html#nvim_create_user_command()
40pub fn create_user_command<Cmd>(
41    name: &str,
42    command: Cmd,
43    opts: &CreateCommandOpts,
44) -> Result<()>
45where
46    Cmd: StringOrFunction<CommandArgs, ()>,
47{
48    let name = nvim::String::from(name);
49    let command = command.to_object();
50    let mut err = nvim::Error::new();
51    unsafe {
52        nvim_create_user_command(
53            LUA_INTERNAL_CALL,
54            name.non_owning(),
55            command.non_owning(),
56            opts,
57            &mut err,
58        )
59    };
60    choose!(err, ())
61}
62
63/// Binding to [`nvim_del_user_command()`][1].
64///
65/// Deletes a global user-defined command.  Use [`Buffer::del_user_command`] to
66/// delete a buffer-local command.
67///
68/// [1]: https://neovim.io/doc/user/api.html#nvim_del_user_command()
69pub fn del_user_command(name: &str) -> Result<()> {
70    let name = nvim::String::from(name);
71    let mut err = nvim::Error::new();
72    unsafe { nvim_del_user_command(name.non_owning(), &mut err) };
73    choose!(err, ())
74}
75
76/// Binding to [`nvim_get_commands()`][1].
77///
78/// Returns an iterator over the infos of the global ex commands. Only
79/// user-defined commands are returned, not builtin ones.
80///
81/// [1]: https://neovim.io/doc/user/api.html#nvim_get_commands()
82pub fn get_commands(
83    opts: &GetCommandsOpts,
84) -> Result<impl SuperIterator<CommandInfos>> {
85    let mut err = nvim::Error::new();
86    let cmds = unsafe {
87        nvim_get_commands(
88            opts,
89            #[cfg(feature = "neovim-0-10")] // On 0.10 and nightly.
90            types::arena(),
91            &mut err,
92        )
93    };
94    choose!(
95        err,
96        Ok({
97            cmds.into_iter()
98                .map(|(_, cmd)| CommandInfos::from_object(cmd).unwrap())
99        })
100    )
101}
102
103/// Binding to [`nvim_parse_cmd()`][1].
104///
105/// Parses the command line.
106///
107/// [1]: https://neovim.io/doc/user/api.html#nvim_parse_cmd()
108pub fn parse_cmd(src: &str, opts: &ParseCmdOpts) -> Result<CmdInfos> {
109    let src = nvim::String::from(src);
110    #[cfg(not(feature = "neovim-0-10"))] // 0nly on 0.9.
111    let opts = nvim::Dictionary::from(opts);
112    let mut err = nvim::Error::new();
113
114    let out = unsafe {
115        nvim_parse_cmd(
116            src.non_owning(),
117            #[cfg(not(feature = "neovim-0-10"))] // 0nly on 0.9.
118            opts.non_owning(),
119            #[cfg(feature = "neovim-0-10")] // On 0.10 and nightly.
120            opts,
121            #[cfg(feature = "neovim-0-10")] // On 0.10 and nightly.
122            types::arena(),
123            &mut err,
124        )
125    };
126
127    #[cfg(not(feature = "neovim-0-10"))] // 0nly on 0.9.
128    let out = CmdInfos::from_object(out.into())?;
129
130    #[cfg(feature = "neovim-0-10")] // On 0.10 and nightly.
131    let out = CmdInfos::try_from(out)?;
132
133    choose!(err, Ok(out))
134}
135
136impl Buffer {
137    /// Binding to [`nvim_buf_create_user_command()`][1].
138    ///
139    /// Creates a new buffer-local user command.
140    ///
141    /// [1]: https://neovim.io/doc/user/api.html#nvim_buf_create_user_command()
142    pub fn create_user_command<Cmd>(
143        &mut self,
144        name: &str,
145        command: Cmd,
146        opts: &CreateCommandOpts,
147    ) -> Result<()>
148    where
149        Cmd: StringOrFunction<CommandArgs, ()>,
150    {
151        let mut err = nvim::Error::new();
152        let name = nvim::String::from(name);
153        let command = command.to_object();
154        unsafe {
155            nvim_buf_create_user_command(
156                #[cfg(any(
157                    feature = "neovim-0-9",
158                    feature = "neovim-nightly"
159                ))]
160                LUA_INTERNAL_CALL,
161                self.0,
162                name.non_owning(),
163                command.non_owning(),
164                opts,
165                &mut err,
166            )
167        };
168        choose!(err, ())
169    }
170
171    /// Binding to [`nvim_buf_del_user_command()`][1].
172    ///
173    /// Deletes a buffer-local user-command. Use
174    /// [`del_user_command`](crate::del_user_command) to delete a global
175    /// command.
176    ///
177    /// [1]: https://neovim.io/doc/user/api.html#nvim_buf_del_user_command()
178    pub fn del_user_command(&mut self, name: &str) -> Result<()> {
179        let mut err = nvim::Error::new();
180        let name = nvim::String::from(name);
181        unsafe {
182            nvim_buf_del_user_command(self.0, name.non_owning(), &mut err)
183        };
184        choose!(err, ())
185    }
186
187    /// Binding to [`nvim_buf_get_commands()`][1].
188    ///
189    /// [1]: https://neovim.io/doc/user/api.html#nvim_buf_get_commands()
190    pub fn get_commands(
191        &self,
192        opts: &GetCommandsOpts,
193    ) -> Result<impl SuperIterator<CommandInfos>> {
194        let mut err = nvim::Error::new();
195        let cmds = unsafe {
196            nvim_buf_get_commands(
197                self.0,
198                opts,
199                #[cfg(feature = "neovim-0-10")] // On 0.10 and nightly.
200                types::arena(),
201                &mut err,
202            )
203        };
204        choose!(
205            err,
206            Ok({
207                cmds.into_iter()
208                    .map(|(_, cmd)| CommandInfos::from_object(cmd).unwrap())
209            })
210        )
211    }
212}