nvim_oxi_api/
vimscript.rs

1use 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
9/// Binding to [`nvim_call_dict_function()`][1].
10///
11/// Calls a VimL dictionary function with the given arguments, returning the
12/// result of the funtion call.
13///
14/// [1]: https://neovim.io/doc/user/api.html#nvim_call_dict_function()
15pub 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
40/// Binding to [`nvim_call_function()`][1].
41///
42/// Calls a VimL function with the given arguments, returning the result of the
43/// funtion call.
44///
45/// [1]: https://neovim.io/doc/user/api.html#nvim_call_function()
46pub 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
65/// Binding to [`nvim_command()`][1].
66///
67/// Executes an Ex command.
68///
69/// [1]: https://neovim.io/doc/user/api.html#nvim_command()
70pub 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
77/// Binding to [`nvim_eval()`][1].
78///
79/// Evaluates a VimL expression.
80///
81/// [1]: https://neovim.io/doc/user/api.html#nvim_eval()
82pub 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
93/// Binding to [`nvim_exec2()`][1].
94///
95/// Executes Vimscript (multiline block of Ex commands), like anonymous
96/// `:source`.
97///
98/// Unlike [`command`] this function supports heredocs, script-scope (s:), etc.
99///
100/// [1]: https://neovim.io/doc/user/api.html#nvim_exec2()
101pub 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
115/// Binding to [`nvim_parse_expression()`][1].
116///
117/// Parses a VimL expression.
118///
119/// [1]: https://neovim.io/doc/user/api.html#nvim_parse_expression()
120pub 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}