1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
use types::{self as nvim, conversion::FromObject, Array, Integer, Object};

use super::ffi::autocmd::*;
use super::opts::*;
use super::types::*;
use super::LUA_INTERNAL_CALL;
use crate::choose;
use crate::Result;
use crate::SuperIterator;

/// Binding to [`nvim_clear_autocmds()`][1].
///
/// Clears all the autocommands matched by at least one of `opts`'s fields.
///
/// [1]: https://neovim.io/doc/user/api.html#nvim_clear_autocmds()
pub fn clear_autocmds(opts: &ClearAutocmdsOpts) -> Result<()> {
    let mut err = nvim::Error::new();
    unsafe {
        nvim_clear_autocmds(
            opts,
            #[cfg(feature = "neovim-0-10")] // On 0.10 and nightly.
            types::arena(),
            &mut err,
        )
    };
    choose!(err, ())
}

/// Binding to [`nvim_create_augroup()`][1].
///
/// Creates a new autocommand group or gets an existing one. To get the id of
/// an existing augroup set the
/// [`clear`](super::opts::CreateAugroupOptsBuilder::clear) field of `opts` to
/// `false`.
///
/// [1]: https://neovim.io/doc/user/api.html#nvim_create_augroup()
pub fn create_augroup(name: &str, opts: &CreateAugroupOpts) -> Result<u32> {
    let name = nvim::String::from(name);
    let mut err = nvim::Error::new();
    let id = unsafe {
        nvim_create_augroup(
            LUA_INTERNAL_CALL,
            name.non_owning(),
            opts,
            &mut err,
        )
    };
    choose!(err, Ok(id.try_into().expect("always positive")))
}

/// Binding to [`nvim_create_autocmd()`][1].
///
/// Creates a new autocommand.
///
/// [1]: https://neovim.io/doc/user/api.html#nvim_create_autocmd()
pub fn create_autocmd<'a, I>(
    events: I,
    opts: &CreateAutocmdOpts,
) -> Result<u32>
where
    I: IntoIterator<Item = &'a str>,
{
    let events = Object::from(Array::from_iter(events));
    let mut err = nvim::Error::new();
    let id = unsafe {
        nvim_create_autocmd(
            LUA_INTERNAL_CALL,
            events.non_owning(),
            opts,
            #[cfg(feature = "neovim-0-10")] // On 0.10 and nightly.
            types::arena(),
            &mut err,
        )
    };
    choose!(err, Ok(id.try_into().expect("always positive")))
}

/// Binding to [`nvim_del_augroup_by_id()`][1].
///
/// Deletes an autocommand group by id.
///
/// [1]: https://neovim.io/doc/user/api.html#nvim_del_augroup_by_id()
pub fn del_augroup_by_id(id: u32) -> Result<()> {
    let mut err = nvim::Error::new();
    unsafe { nvim_del_augroup_by_id(id as Integer, &mut err) };
    choose!(err, ())
}

/// Binding to [`nvim_del_augroup_by_name()`][1].
///
/// Deletes an autocommand group by name.
///
/// [1]: https://neovim.io/doc/user/api.html#nvim_del_augroup_by_name()
pub fn del_augroup_by_name(name: &str) -> Result<()> {
    let name = nvim::String::from(name);
    let mut err = nvim::Error::new();
    unsafe { nvim_del_augroup_by_name(name.non_owning(), &mut err) };
    choose!(err, ())
}

/// Binding to [`nvim_del_autocmd()`][1].
///
/// Deletes an autocommand by id.
///
/// [1]: https://neovim.io/doc/user/api.html#nvim_del_autocmd()
pub fn del_autocmd(id: u32) -> Result<()> {
    let mut err = nvim::Error::new();
    unsafe { nvim_del_autocmd(id as Integer, &mut err) };
    choose!(err, ())
}

/// Binding to [`nvim_exec_autocmds()`][1].
///
/// Executes all the autocommands registered on the given `events` that also
/// match `opts`.
///
/// [1]: https://neovim.io/doc/user/api.html#nvim_exec_autocmds()
pub fn exec_autocmds<'a, I>(events: I, opts: &ExecAutocmdsOpts) -> Result<()>
where
    I: IntoIterator<Item = &'a str>,
{
    let events = Object::from(Array::from_iter(events));
    let mut err = nvim::Error::new();
    unsafe {
        nvim_exec_autocmds(
            events.non_owning(),
            opts,
            #[cfg(feature = "neovim-0-10")] // On 0.10 and nightly.
            types::arena(),
            &mut err,
        )
    };
    choose!(err, ())
}

/// Binding to [`nvim_get_autocmds()`][1].
///
/// Gets all the autocommands that match `opts`. When multiple patterns or
/// events are provided, it will find all the autocommands that match any
/// combination of them.
///
/// [1]: https://neovim.io/doc/user/api.html#nvim_get_autocmds()
pub fn get_autocmds(
    opts: &GetAutocmdsOpts,
) -> Result<impl SuperIterator<AutocmdInfos>> {
    let mut err = nvim::Error::new();
    let infos = unsafe {
        nvim_get_autocmds(
            opts,
            #[cfg(feature = "neovim-0-10")] // On 0.10 and nightly.
            types::arena(),
            &mut err,
        )
    };
    choose!(
        err,
        Ok({
            infos
                .into_iter()
                .map(|obj| AutocmdInfos::from_object(obj).unwrap())
        })
    )
}