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
12pub 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")] types::arena(),
27 &mut err,
28 )
29 };
30 choose!(err, {
31 Ok((!output.is_empty()).then(|| output.to_string_lossy().into()))
32 })
33}
34
35pub 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
63pub 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
76pub 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")] 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
103pub fn parse_cmd(src: &str, opts: &ParseCmdOpts) -> Result<CmdInfos> {
109 let src = nvim::String::from(src);
110 #[cfg(not(feature = "neovim-0-10"))] 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"))] opts.non_owning(),
119 #[cfg(feature = "neovim-0-10")] opts,
121 #[cfg(feature = "neovim-0-10")] types::arena(),
123 &mut err,
124 )
125 };
126
127 #[cfg(not(feature = "neovim-0-10"))] let out = CmdInfos::from_object(out.into())?;
129
130 #[cfg(feature = "neovim-0-10")] let out = CmdInfos::try_from(out)?;
132
133 choose!(err, Ok(out))
134}
135
136impl Buffer {
137 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 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 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")] 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}