nvim_api/
vimscript.rs

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