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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
use types::conversion::{FromObject, ToObject};

use crate::choose;
use crate::ffi::deprecated::*;
use crate::types::*;
use crate::Result;
use crate::LUA_INTERNAL_CALL;
use crate::{Buffer, Window};

/// Binding to [`nvim_exec()`][1].
///
/// Executes a multiline block of Ex commands. If `output` is true the
/// output is captured and returned.
///
/// [1]: https://neovim.io/doc/user/api.html#nvim_exec()
#[cfg_attr(
    feature = "neovim-nightly",
    deprecated(since = "0.5.0", note = "use `exec2` instead")
)]
pub fn exec(src: &str, output: bool) -> Result<Option<String>> {
    let src = types::String::from(src);
    let mut err = types::Error::new();
    let output = unsafe {
        nvim_exec(LUA_INTERNAL_CALL, src.non_owning(), output, &mut err)
    };
    choose!(err, {
        Ok((!output.is_empty()).then(|| output.to_string_lossy().into()))
    })
}

/// Binding to [`nvim_get_hl_by_id()`][1].
///
/// Gets a highlight definition by id.
///
/// [1]: https://neovim.io/doc/user/api.html#nvim_get_hl_by_id[1]
///
/// [1]: https://neovim.io/doc/user/api.html#nvim_get_current_win()
#[cfg_attr(
    feature = "neovim-nightly",
    deprecated(since = "0.5.0", note = "use `get_hl` instead")
)]
pub fn get_hl_by_id(hl_id: u32, rgb: bool) -> Result<HighlightInfos> {
    let mut err = types::Error::new();

    let hl = unsafe {
        nvim_get_hl_by_id(hl_id.into(), rgb, core::ptr::null_mut(), &mut err)
    };

    choose!(err, Ok(HighlightInfos::from_object(hl.into())?))
}

/// Binding to [`nvim_get_hl_by_name()`][1].
///
/// Gets a highlight definition by name.
///
/// [1]: https://neovim.io/doc/user/api.html#nvim_get_hl_by_name()
#[cfg_attr(
    feature = "neovim-nightly",
    deprecated(since = "0.5.0", note = "use `get_hl` instead")
)]
pub fn get_hl_by_name(name: &str, rgb: bool) -> Result<HighlightInfos> {
    let name = types::String::from(name);
    let mut err = types::Error::new();
    let hl = unsafe {
        nvim_get_hl_by_name(
            name.non_owning(),
            rgb,
            core::ptr::null_mut(),
            &mut err,
        )
    };
    choose!(err, Ok(HighlightInfos::from_object(hl.into())?))
}

/// Binding to [`nvim_get_option()`][1].
///
/// Gets the value of a global option.
///
/// [1]: https://neovim.io/doc/user/api.html#nvim_get_option()
#[cfg_attr(
    feature = "neovim-nightly",
    deprecated(since = "0.5.0", note = "use `get_option_value` instead")
)]
pub fn get_option<Opt>(name: &str) -> Result<Opt>
where
    Opt: FromObject,
{
    let name = types::String::from(name);
    let mut err = types::Error::new();
    let obj = unsafe {
        nvim_get_option(
            name.non_owning(),
            #[cfg(not(feature = "neovim-0-10"))] // 0nly on 0.9.
            types::arena(),
            &mut err,
        )
    };
    choose!(err, Ok(Opt::from_object(obj)?))
}

/// Binding to [`nvim_get_option_info()`][1].
///
/// Gets all the informations related to an option.
///
/// [1]: https://neovim.io/doc/user/api.html#nvim_get_option_info()
#[cfg_attr(
    feature = "neovim-nightly",
    deprecated(since = "0.5.0", note = "use `get_option_info2` instead")
)]
pub fn get_option_info(name: &str) -> Result<OptionInfos> {
    let name = types::String::from(name);
    let mut err = types::Error::new();
    let obj = unsafe {
        nvim_get_option_info(
            name.non_owning(),
            #[cfg(feature = "neovim-0-10")] // On 0.10 and nightly.
            types::arena(),
            &mut err,
        )
    };
    choose!(err, Ok(OptionInfos::from_object(obj.into())?))
}

/// Binding to [`nvim_set_option()`][1].
///
/// Sets the global value of an option.
///
/// [1]: https://neovim.io/doc/user/api.html#nvim_set_option()
#[cfg_attr(
    feature = "neovim-nightly",
    deprecated(since = "0.5.0", note = "use `set_option_value` instead")
)]
pub fn set_option<Opt>(name: &str, value: Opt) -> Result<()>
where
    Opt: ToObject,
{
    let name = types::String::from(name);
    let mut err = types::Error::new();
    unsafe {
        nvim_set_option(
            LUA_INTERNAL_CALL,
            name.non_owning(),
            value.to_object()?.non_owning(),
            &mut err,
        )
    };
    choose!(err, ())
}

impl Buffer {
    /// Binding to [`nvim_buf_get_option()`][1].
    ///
    /// Gets a buffer option value.
    ///
    /// [1]: https://neovim.io/doc/user/api.html#nvim_buf_get_option()
    #[cfg_attr(
        feature = "neovim-nightly",
        deprecated(since = "0.5.0", note = "use `get_option_value` instead")
    )]
    pub fn get_option<Opt>(&self, name: &str) -> Result<Opt>
    where
        Opt: FromObject,
    {
        let mut err = types::Error::new();
        let name = types::String::from(name);
        let obj = unsafe {
            nvim_buf_get_option(
                self.0,
                name.non_owning(),
                #[cfg(not(feature = "neovim-0-10"))] // 0nly on 0.9.
                types::arena(),
                &mut err,
            )
        };
        choose!(err, Ok(Opt::from_object(obj)?))
    }

    /// Binding to [`nvim_buf_set_option()`][1].
    ///
    /// Sets a buffer option value. Passing `None` as value deletes the option
    /// (only works if there's a global fallback).
    ///
    /// [1]: https://neovim.io/doc/user/api.html#nvim_buf_set_option()
    #[cfg_attr(
        feature = "neovim-nightly",
        deprecated(since = "0.5.0", note = "use `set_option_value` instead")
    )]
    pub fn set_option<V>(&mut self, name: &str, value: V) -> Result<()>
    where
        V: ToObject,
    {
        let mut err = types::Error::new();
        let name = types::String::from(name);
        unsafe {
            nvim_buf_set_option(
                LUA_INTERNAL_CALL,
                self.0,
                name.non_owning(),
                value.to_object()?.non_owning(),
                &mut err,
            )
        };
        choose!(err, ())
    }
}

impl Window {
    /// Binding to [`nvim_win_get_option()`][1].
    ///
    /// Gets a window option value.
    ///
    /// [1]: https://neovim.io/doc/user/api.html#nvim_win_get_option()
    #[cfg_attr(
        feature = "neovim-nightly",
        deprecated(since = "0.5.0", note = "use `get_option_value` instead")
    )]
    pub fn get_option<Opt>(&self, name: &str) -> Result<Opt>
    where
        Opt: FromObject,
    {
        let mut err = types::Error::new();
        let name = types::String::from(name);
        let obj = unsafe {
            nvim_win_get_option(
                self.0,
                name.non_owning(),
                #[cfg(not(feature = "neovim-0-10"))] // 0nly on 0.9.
                types::arena(),
                &mut err,
            )
        };
        choose!(err, Ok(Opt::from_object(obj)?))
    }

    /// Binding to [`nvim_win_set_option()`][1].
    ///
    /// Sets a window option value. Passing `None` as value deletes the option
    /// (only works if there's a global fallback).
    ///
    /// [1]: https://neovim.io/doc/user/api.html#nvim_win_set_option()
    #[cfg_attr(
        feature = "neovim-nightly",
        deprecated(since = "0.5.0", note = "use `set_option_value` instead")
    )]
    pub fn set_option<Opt>(&mut self, name: &str, value: Opt) -> Result<()>
    where
        Opt: ToObject,
    {
        let mut err = types::Error::new();
        let name = types::String::from(name);
        unsafe {
            nvim_win_set_option(
                LUA_INTERNAL_CALL,
                self.0,
                name.non_owning(),
                value.to_object()?.non_owning(),
                &mut err,
            )
        };
        choose!(err, ())
    }
}