nvim_oxi_api/
vimscript.rs

1use types::{self as nvim, conversion::FromObject, Array, Object};
2
3use crate::choose;
4use crate::ffi::vimscript::*;
5#[cfg(feature = "neovim-0-10")] // On 0.10 and nightly.
6use crate::opts::ExecOpts;
7use crate::types::*;
8use crate::Result;
9
10/// Binding to [`nvim_call_dict_function()`][1].
11///
12/// Calls a VimL dictionary function with the given arguments, returning the
13/// result of the funtion call.
14///
15/// [1]: https://neovim.io/doc/user/api.html#nvim_call_dict_function()
16pub fn call_dict_function<Args, Ret>(
17    dict: &str,
18    func: &str,
19    args: Args,
20) -> Result<Ret>
21where
22    Args: Into<Array>,
23    Ret: FromObject,
24{
25    let dict = Object::from(nvim::String::from(dict));
26    let func = nvim::String::from(func);
27    let args = args.into();
28    let mut err = nvim::Error::new();
29    let res = unsafe {
30        nvim_call_dict_function(
31            dict.non_owning(),
32            func.non_owning(),
33            args.non_owning(),
34            #[cfg(feature = "neovim-0-10")] // On 0.10 and nightly.
35            types::arena(),
36            &mut err,
37        )
38    };
39    choose!(err, Ok(Ret::from_object(res)?))
40}
41
42/// Binding to [`nvim_call_function()`][1].
43///
44/// Calls a VimL function with the given arguments, returning the result of the
45/// funtion call.
46///
47/// [1]: https://neovim.io/doc/user/api.html#nvim_call_function()
48pub fn call_function<Args, Ret>(func: &str, args: Args) -> Result<Ret>
49where
50    Args: Into<Array>,
51    Ret: FromObject,
52{
53    let func = nvim::String::from(func);
54    let args = args.into();
55    let mut err = nvim::Error::new();
56    let res = unsafe {
57        nvim_call_function(
58            func.non_owning(),
59            args.non_owning(),
60            #[cfg(feature = "neovim-0-10")] // On 0.10 and nightly.
61            types::arena(),
62            &mut err,
63        )
64    };
65    choose!(err, Ok(Ret::from_object(res)?))
66}
67
68/// Binding to [`nvim_command()`][1].
69///
70/// Executes an Ex command.
71///
72/// [1]: https://neovim.io/doc/user/api.html#nvim_command()
73pub fn command(command: &str) -> Result<()> {
74    let command = nvim::String::from(command);
75    let mut err = nvim::Error::new();
76    unsafe { nvim_command(command.non_owning(), &mut err) };
77    choose!(err, ())
78}
79
80/// Binding to [`nvim_eval()`][1].
81///
82/// Evaluates a VimL expression.
83///
84/// [1]: https://neovim.io/doc/user/api.html#nvim_eval()
85pub fn eval<V>(expr: &str) -> Result<V>
86where
87    V: FromObject,
88{
89    let expr = nvim::String::from(expr);
90    let mut err = nvim::Error::new();
91    let output = unsafe {
92        nvim_eval(
93            expr.non_owning(),
94            #[cfg(feature = "neovim-0-10")] // On 0.10 and nightly.
95            types::arena(),
96            &mut err,
97        )
98    };
99    choose!(err, Ok(V::from_object(output)?))
100}
101
102/// Binding to [`nvim_exec2()`][1].
103///
104/// Executes Vimscript (multiline block of Ex commands), like anonymous
105/// `:source`.
106///
107/// Unlike [`command`] this function supports heredocs, script-scope (s:), etc.
108///
109/// [1]: https://neovim.io/doc/user/api.html#nvim_exec2()
110#[cfg(feature = "neovim-0-10")] // On 0.10 and nightly.
111#[cfg_attr(
112    docsrs,
113    doc(cfg(any(feature = "neovim-0-10", feature = "neovim-nightly")))
114)]
115pub fn exec2(src: &str, opts: &ExecOpts) -> Result<Option<nvim::String>> {
116    let src = types::String::from(src);
117    let mut err = types::Error::new();
118    let dict = unsafe {
119        nvim_exec2(crate::LUA_INTERNAL_CALL, src.non_owning(), opts, &mut err)
120    };
121    choose!(err, {
122        Ok(dict
123            .into_iter()
124            .next()
125            .map(|(_s, output)| nvim::String::from_object(output).unwrap()))
126    })
127}
128
129/// Binding to [`nvim_parse_expression()`][1].
130///
131/// Parses a VimL expression.
132///
133/// [1]: https://neovim.io/doc/user/api.html#nvim_parse_expression()
134pub fn parse_expression(
135    expr: &str,
136    flags: &str,
137    include_highlight: bool,
138) -> Result<ParsedVimLExpression> {
139    let expr = nvim::String::from(expr);
140    let flags = nvim::String::from(flags);
141    let mut err = nvim::Error::new();
142    let dict = unsafe {
143        nvim_parse_expression(
144            expr.non_owning(),
145            flags.non_owning(),
146            include_highlight,
147            #[cfg(feature = "neovim-0-10")] // On 0.10 and nightly.
148            types::arena(),
149            &mut err,
150        )
151    };
152    choose!(err, Ok(ParsedVimLExpression::from_object(dict.into())?))
153}