nvim_api/
autocmd.rs

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