nvim_oxi_api/
vimscript.rs1use types::{self as nvim, Array, Object, conversion::FromObject};
2
3use crate::Result;
4use crate::choose;
5use crate::ffi::vimscript::*;
6use crate::opts::ExecOpts;
7use crate::types::*;
8
9pub fn call_dict_function<Args, Ret>(
16 dict: &str,
17 func: &str,
18 args: Args,
19) -> Result<Ret>
20where
21 Args: Into<Array>,
22 Ret: FromObject,
23{
24 let dict = Object::from(nvim::String::from(dict));
25 let func = nvim::String::from(func);
26 let args = args.into();
27 let mut err = nvim::Error::new();
28 let res = unsafe {
29 nvim_call_dict_function(
30 dict.non_owning(),
31 func.as_nvim_str(),
32 args.non_owning(),
33 types::arena(),
34 &mut err,
35 )
36 };
37 choose!(err, Ok(Ret::from_object(res)?))
38}
39
40pub fn call_function<Args, Ret>(func: &str, args: Args) -> Result<Ret>
47where
48 Args: Into<Array>,
49 Ret: FromObject,
50{
51 let func = nvim::String::from(func);
52 let args = args.into();
53 let mut err = nvim::Error::new();
54 let res = unsafe {
55 nvim_call_function(
56 func.as_nvim_str(),
57 args.non_owning(),
58 types::arena(),
59 &mut err,
60 )
61 };
62 choose!(err, Ok(Ret::from_object(res)?))
63}
64
65pub fn command(command: &str) -> Result<()> {
71 let command = nvim::String::from(command);
72 let mut err = nvim::Error::new();
73 unsafe { nvim_command(command.as_nvim_str(), &mut err) };
74 choose!(err, ())
75}
76
77pub fn eval<V>(expr: &str) -> Result<V>
83where
84 V: FromObject,
85{
86 let expr = nvim::String::from(expr);
87 let mut err = nvim::Error::new();
88 let output =
89 unsafe { nvim_eval(expr.as_nvim_str(), types::arena(), &mut err) };
90 choose!(err, Ok(V::from_object(output)?))
91}
92
93pub fn exec2(src: &str, opts: &ExecOpts) -> Result<Option<nvim::String>> {
102 let src = types::String::from(src);
103 let mut err = types::Error::new();
104 let dict = unsafe {
105 nvim_exec2(crate::LUA_INTERNAL_CALL, src.as_nvim_str(), opts, &mut err)
106 };
107 choose!(err, {
108 Ok(dict
109 .into_iter()
110 .next()
111 .map(|(_s, output)| nvim::String::from_object(output).unwrap()))
112 })
113}
114
115pub fn parse_expression(
121 expr: &str,
122 flags: &str,
123 include_highlight: bool,
124) -> Result<ParsedVimLExpression> {
125 let expr = nvim::String::from(expr);
126 let flags = nvim::String::from(flags);
127 let mut err = nvim::Error::new();
128 let dict = unsafe {
129 nvim_parse_expression(
130 expr.as_nvim_str(),
131 flags.as_nvim_str(),
132 include_highlight,
133 types::arena(),
134 &mut err,
135 )
136 };
137 choose!(err, Ok(ParsedVimLExpression::from_object(dict.into())?))
138}