nvim_oxi_api/
autocmd.rs

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