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
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 types::arena(),
26 &mut err,
27 )
28 };
29 choose!(err, {
30 Ok((!output.is_empty()).then(|| output.to_string_lossy().into()))
31 })
32}
33
34pub 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
62pub 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
75pub 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
95pub 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 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 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 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}