use nvim_types::{self as nvim, conversion::FromObject, Array, Object};
use crate::choose;
use crate::ffi::vimscript::*;
use crate::types::*;
use crate::Result;
use crate::LUA_INTERNAL_CALL;
pub fn call_dict_function<Args, Ret>(
dict: &str,
func: &str,
args: Args,
) -> Result<Ret>
where
Args: Into<Array>,
Ret: FromObject,
{
let dict = Object::from(nvim::String::from(dict));
let func = nvim::String::from(func);
let args = args.into();
let mut err = nvim::Error::new();
let res = unsafe {
nvim_call_dict_function(
dict.non_owning(),
func.non_owning(),
args.non_owning(),
&mut err,
)
};
choose!(err, Ok(Ret::from_object(res)?))
}
pub fn call_function<Args, Ret>(func: &str, args: Args) -> Result<Ret>
where
Args: Into<Array>,
Ret: FromObject,
{
let func = nvim::String::from(func);
let args = args.into();
let mut err = nvim::Error::new();
let res = unsafe {
nvim_call_function(func.non_owning(), args.non_owning(), &mut err)
};
choose!(err, Ok(Ret::from_object(res)?))
}
#[cfg(any(feature = "neovim-0-8", feature = "neovim-nightly"))]
#[cfg_attr(
docsrs,
doc(cfg(any(feature = "neovim-0-8", feature = "neovim-nightly")))
)]
pub fn cmd(
infos: &CmdInfos,
opts: &super::opts::CmdOpts,
) -> Result<Option<String>> {
let opts = super::opts::KeyDict_cmd_opts::from(opts);
let mut err = nvim::Error::new();
let output = unsafe {
nvim_cmd(LUA_INTERNAL_CALL, &infos.into(), &opts.into(), &mut err)
};
choose!(err, {
output
.into_string()
.map_err(From::from)
.map(|output| (!output.is_empty()).then_some(output))
})
}
pub fn command(command: &str) -> Result<()> {
let command = nvim::String::from(command);
let mut err = nvim::Error::new();
unsafe { nvim_command(command.non_owning(), &mut err) };
choose!(err, ())
}
pub fn eval<V>(expr: &str) -> Result<V>
where
V: FromObject,
{
let expr = nvim::String::from(expr);
let mut err = nvim::Error::new();
let output = unsafe { nvim_eval(expr.non_owning(), &mut err) };
choose!(err, Ok(V::from_object(output)?))
}
pub fn exec(src: &str, output: bool) -> Result<Option<String>> {
let src = nvim::String::from(src);
let mut err = nvim::Error::new();
let output = unsafe {
nvim_exec(LUA_INTERNAL_CALL, src.non_owning(), output, &mut err)
};
choose!(err, {
output
.into_string()
.map_err(From::from)
.map(|output| (!output.is_empty()).then_some(output))
})
}
#[cfg(any(feature = "neovim-0-8", feature = "neovim-nightly"))]
#[cfg_attr(
docsrs,
doc(cfg(any(feature = "neovim-0-8", feature = "neovim-nightly")))
)]
pub fn parse_cmd(
src: &str,
opts: &super::opts::ParseCmdOpts,
) -> Result<CmdInfos> {
let src = nvim::String::from(src);
let opts = nvim::Dictionary::from(opts);
let mut err = nvim::Error::new();
let dict = unsafe {
nvim_parse_cmd(src.non_owning(), opts.non_owning(), &mut err)
};
choose!(err, Ok(CmdInfos::from_object(dict.into())?))
}
pub fn parse_expression(
expr: &str,
flags: &str,
include_highlight: bool,
) -> Result<ParsedVimLExpression> {
let expr = nvim::String::from(expr);
let flags = nvim::String::from(flags);
let mut err = nvim::Error::new();
let dict = unsafe {
nvim_parse_expression(
expr.non_owning(),
flags.non_owning(),
include_highlight,
&mut err,
)
};
choose!(err, Ok(ParsedVimLExpression::from_object(dict.into())?))
}