nvim_oxi_api/
command.rs

1use types::{self as nvim, conversion::FromObject};
2
3use super::opts::*;
4use crate::Buffer;
5use crate::LUA_INTERNAL_CALL;
6use crate::Result;
7use crate::choose;
8use crate::ffi::command::*;
9use crate::trait_utils::{StringOrFunction, SuperIterator};
10use crate::types::*;
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            types::arena(),
26            &mut err,
27        )
28    };
29    choose!(err, {
30        Ok((!output.is_empty()).then(|| output.to_string_lossy().into()))
31    })
32}
33
34/// Binding to [`nvim_create_user_command()`][1].
35///
36/// Creates a new [user command](https://neovim.io/doc/user/map.html#user-commands).
37///
38/// [1]: https://neovim.io/doc/user/api.html#nvim_create_user_command()
39pub fn create_user_command<Cmd>(
40    name: &str,
41    command: Cmd,
42    opts: &CreateCommandOpts,
43) -> Result<()>
44where
45    Cmd: StringOrFunction<CommandArgs, ()>,
46{
47    let name = nvim::String::from(name);
48    let command = command.to_object();
49    let mut err = nvim::Error::new();
50    unsafe {
51        nvim_create_user_command(
52            LUA_INTERNAL_CALL,
53            name.as_nvim_str(),
54            command.non_owning(),
55            opts,
56            &mut err,
57        )
58    };
59    choose!(err, ())
60}
61
62/// Binding to [`nvim_del_user_command()`][1].
63///
64/// Deletes a global user-defined command.  Use [`Buffer::del_user_command`] to
65/// delete a buffer-local command.
66///
67/// [1]: https://neovim.io/doc/user/api.html#nvim_del_user_command()
68pub fn del_user_command(name: &str) -> Result<()> {
69    let name = nvim::String::from(name);
70    let mut err = nvim::Error::new();
71    unsafe { nvim_del_user_command(name.as_nvim_str(), &mut err) };
72    choose!(err, ())
73}
74
75/// Binding to [`nvim_get_commands()`][1].
76///
77/// Returns an iterator over the infos of the global ex commands. Only
78/// user-defined commands are returned, not builtin ones.
79///
80/// [1]: https://neovim.io/doc/user/api.html#nvim_get_commands()
81pub fn get_commands(
82    opts: &GetCommandsOpts,
83) -> Result<impl SuperIterator<CommandInfos>> {
84    let mut err = nvim::Error::new();
85    let cmds = unsafe { nvim_get_commands(opts, types::arena(), &mut err) };
86    choose!(
87        err,
88        Ok({
89            cmds.into_iter()
90                .map(|(_, cmd)| CommandInfos::from_object(cmd).unwrap())
91        })
92    )
93}
94
95/// Binding to [`nvim_parse_cmd()`][1].
96///
97/// Parses the command line.
98///
99/// [1]: https://neovim.io/doc/user/api.html#nvim_parse_cmd()
100pub fn parse_cmd(src: &str, opts: &ParseCmdOpts) -> Result<CmdInfos> {
101    let src = nvim::String::from(src);
102    let mut err = nvim::Error::new();
103
104    let out = unsafe {
105        nvim_parse_cmd(src.as_nvim_str(), opts, types::arena(), &mut err)
106    }
107    .try_into()?;
108
109    choose!(err, Ok(out))
110}
111
112impl Buffer {
113    /// Binding to [`nvim_buf_create_user_command()`][1].
114    ///
115    /// Creates a new buffer-local user command.
116    ///
117    /// [1]: https://neovim.io/doc/user/api.html#nvim_buf_create_user_command()
118    pub fn create_user_command<Cmd>(
119        &mut self,
120        name: &str,
121        command: Cmd,
122        opts: &CreateCommandOpts,
123    ) -> Result<()>
124    where
125        Cmd: StringOrFunction<CommandArgs, ()>,
126    {
127        let mut err = nvim::Error::new();
128        let name = nvim::String::from(name);
129        let command = command.to_object();
130        unsafe {
131            nvim_buf_create_user_command(
132                LUA_INTERNAL_CALL,
133                self.0,
134                name.as_nvim_str(),
135                command.non_owning(),
136                opts,
137                &mut err,
138            )
139        };
140        choose!(err, ())
141    }
142
143    /// Binding to [`nvim_buf_del_user_command()`][1].
144    ///
145    /// Deletes a buffer-local user-command. Use
146    /// [`del_user_command`](crate::del_user_command) to delete a global
147    /// command.
148    ///
149    /// [1]: https://neovim.io/doc/user/api.html#nvim_buf_del_user_command()
150    pub fn del_user_command(&mut self, name: &str) -> Result<()> {
151        let mut err = nvim::Error::new();
152        let name = nvim::String::from(name);
153        unsafe {
154            nvim_buf_del_user_command(self.0, name.as_nvim_str(), &mut err)
155        };
156        choose!(err, ())
157    }
158
159    /// Binding to [`nvim_buf_get_commands()`][1].
160    ///
161    /// [1]: https://neovim.io/doc/user/api.html#nvim_buf_get_commands()
162    pub fn get_commands(
163        &self,
164        opts: &GetCommandsOpts,
165    ) -> Result<impl SuperIterator<CommandInfos>> {
166        let mut err = nvim::Error::new();
167        let cmds = unsafe {
168            nvim_buf_get_commands(self.0, opts, types::arena(), &mut err)
169        };
170        choose!(
171            err,
172            Ok({
173                cmds.into_iter()
174                    .map(|(_, cmd)| CommandInfos::from_object(cmd).unwrap())
175            })
176        )
177    }
178}