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