nvim_oxi_api/
autocmd.rs

1use types::{self as nvim, Array, Integer, Object, conversion::FromObject};
2
3use super::LUA_INTERNAL_CALL;
4use super::ffi::autocmd::*;
5use super::opts::*;
6use super::types::*;
7use crate::Result;
8use crate::SuperIterator;
9use crate::choose;
10
11/// Binding to [`nvim_clear_autocmds()`][1].
12///
13/// Clears all the autocommands matched by at least one of `opts`'s fields.
14///
15/// [1]: https://neovim.io/doc/user/api.html#nvim_clear_autocmds()
16pub fn clear_autocmds(opts: &ClearAutocmdsOpts) -> Result<()> {
17    let mut err = nvim::Error::new();
18    unsafe { nvim_clear_autocmds(opts, types::arena(), &mut err) };
19    choose!(err, ())
20}
21
22/// Binding to [`nvim_create_augroup()`][1].
23///
24/// Creates a new autocommand group or gets an existing one. To get the id of
25/// an existing augroup set the
26/// [`clear`](super::opts::CreateAugroupOptsBuilder::clear) field of `opts` to
27/// `false`.
28///
29/// [1]: https://neovim.io/doc/user/api.html#nvim_create_augroup()
30pub fn create_augroup(name: &str, opts: &CreateAugroupOpts) -> Result<u32> {
31    let name = nvim::String::from(name);
32    let mut err = nvim::Error::new();
33    let id = unsafe {
34        nvim_create_augroup(
35            LUA_INTERNAL_CALL,
36            name.as_nvim_str(),
37            opts,
38            &mut err,
39        )
40    };
41    choose!(err, Ok(id.try_into().expect("always positive")))
42}
43
44/// Binding to [`nvim_create_autocmd()`][1].
45///
46/// Creates a new autocommand.
47///
48/// [1]: https://neovim.io/doc/user/api.html#nvim_create_autocmd()
49pub fn create_autocmd<'a, I>(
50    events: I,
51    opts: &CreateAutocmdOpts,
52) -> Result<u32>
53where
54    I: IntoIterator<Item = &'a str>,
55{
56    let events = Object::from(Array::from_iter(events));
57    let mut err = nvim::Error::new();
58    let id = unsafe {
59        nvim_create_autocmd(
60            LUA_INTERNAL_CALL,
61            events.non_owning(),
62            opts,
63            types::arena(),
64            &mut err,
65        )
66    };
67    choose!(err, Ok(id.try_into().expect("always positive")))
68}
69
70/// Binding to [`nvim_del_augroup_by_id()`][1].
71///
72/// Deletes an autocommand group by id.
73///
74/// [1]: https://neovim.io/doc/user/api.html#nvim_del_augroup_by_id()
75pub fn del_augroup_by_id(id: u32) -> Result<()> {
76    let mut err = nvim::Error::new();
77    unsafe { nvim_del_augroup_by_id(id as Integer, &mut err) };
78    choose!(err, ())
79}
80
81/// Binding to [`nvim_del_augroup_by_name()`][1].
82///
83/// Deletes an autocommand group by name.
84///
85/// [1]: https://neovim.io/doc/user/api.html#nvim_del_augroup_by_name()
86pub fn del_augroup_by_name(name: &str) -> Result<()> {
87    let name = nvim::String::from(name);
88    let mut err = nvim::Error::new();
89    unsafe { nvim_del_augroup_by_name(name.as_nvim_str(), &mut err) };
90    choose!(err, ())
91}
92
93/// Binding to [`nvim_del_autocmd()`][1].
94///
95/// Deletes an autocommand by id.
96///
97/// [1]: https://neovim.io/doc/user/api.html#nvim_del_autocmd()
98pub fn del_autocmd(id: u32) -> Result<()> {
99    let mut err = nvim::Error::new();
100    unsafe { nvim_del_autocmd(id as Integer, &mut err) };
101    choose!(err, ())
102}
103
104/// Binding to [`nvim_exec_autocmds()`][1].
105///
106/// Executes all the autocommands registered on the given `events` that also
107/// match `opts`.
108///
109/// [1]: https://neovim.io/doc/user/api.html#nvim_exec_autocmds()
110pub fn exec_autocmds<'a, I>(events: I, opts: &ExecAutocmdsOpts) -> Result<()>
111where
112    I: IntoIterator<Item = &'a str>,
113{
114    let events = Object::from(Array::from_iter(events));
115    let mut err = nvim::Error::new();
116    unsafe {
117        nvim_exec_autocmds(events.non_owning(), opts, types::arena(), &mut err)
118    };
119    choose!(err, ())
120}
121
122/// Binding to [`nvim_get_autocmds()`][1].
123///
124/// Gets all the autocommands that match `opts`. When multiple patterns or
125/// events are provided, it will find all the autocommands that match any
126/// combination of them.
127///
128/// [1]: https://neovim.io/doc/user/api.html#nvim_get_autocmds()
129pub fn get_autocmds(
130    opts: &GetAutocmdsOpts,
131) -> Result<impl SuperIterator<AutocmdInfos> + use<>> {
132    let mut err = nvim::Error::new();
133    let infos = unsafe { nvim_get_autocmds(opts, types::arena(), &mut err) };
134    choose!(
135        err,
136        Ok({
137            infos
138                .into_iter()
139                .map(|obj| AutocmdInfos::from_object(obj).unwrap())
140        })
141    )
142}