nvim_oxi_api/
options.rs

1use types::{
2    self as nvim,
3    conversion::{FromObject, ToObject},
4};
5
6use crate::Result;
7use crate::SuperIterator;
8use crate::choose;
9use crate::ffi::options::*;
10use crate::opts::*;
11use crate::types::*;
12
13/// Binding to [`nvim_get_all_options_info()`][1].
14///
15/// Gets the option information for all options.
16///
17/// [1]: https://neovim.io/doc/user/api.html#nvim_get_all_options_info()
18pub fn get_all_options_info() -> Result<impl SuperIterator<OptionInfos>> {
19    let mut err = nvim::Error::new();
20    let infos = unsafe { nvim_get_all_options_info(types::arena(), &mut err) };
21    choose!(
22        err,
23        Ok({
24            infos
25                .into_iter()
26                .map(|(_, optinf)| OptionInfos::from_object(optinf).unwrap())
27        })
28    )
29}
30
31/// Binding to [`nvim_get_option_info2()`][1].
32///
33/// Gets the option information for one option from an arbitrary buffer or
34/// window.
35///
36/// [1]: https://neovim.io/doc/user/api.html#nvim_get_option_info2()
37pub fn get_option_info2(name: &str, opts: &OptionOpts) -> Result<OptionInfos> {
38    let name = types::String::from(name);
39    let mut err = types::Error::new();
40    let dict = unsafe {
41        nvim_get_option_info2(
42            name.as_nvim_str(),
43            opts,
44            types::arena(),
45            &mut err,
46        )
47    };
48    choose!(err, Ok(OptionInfos::from_object(dict.into())?))
49}
50
51/// Binding to [`nvim_get_option_value()`][1].
52///
53/// Gets the local value of an option if it exists, or the global value
54/// otherwise. Local values always correspond to the current buffer or window.
55///
56/// To get a buffer-local orr window-local option for a specific buffer of
57/// window consider using [`Buffer::get_option`](crate::Buffer::get_option) or
58/// [`Window::get_option`](crate::Window::get_option) instead.
59///
60/// [1]: https://neovim.io/doc/user/api.html#nvim_get_option_value()
61pub fn get_option_value<Opt>(name: &str, opts: &OptionOpts) -> Result<Opt>
62where
63    Opt: FromObject,
64{
65    let name = nvim::String::from(name);
66    let mut err = nvim::Error::new();
67    let obj =
68        unsafe { nvim_get_option_value(name.as_nvim_str(), opts, &mut err) };
69    choose!(err, Ok(Opt::from_object(obj)?))
70}
71
72/// Binding to [`nvim_set_option_value()`][1].
73///
74/// Sets the value of an option. The behaviour of this function matches that of
75/// `:set`: for global-local options, both the global and local value are set
76/// unless specified otherwise in the [`scope`](OptionOptsBuilder::scope)
77/// field of `opts`.
78///
79/// [1]: https://neovim.io/doc/user/api.html#nvim_set_option_value()
80pub fn set_option_value<Opt>(
81    name: &str,
82    value: Opt,
83    opts: &OptionOpts,
84) -> Result<()>
85where
86    Opt: ToObject,
87{
88    let name = nvim::String::from(name);
89    let mut err = nvim::Error::new();
90    unsafe {
91        nvim_set_option_value(
92            crate::LUA_INTERNAL_CALL,
93            name.as_nvim_str(),
94            value.to_object()?.non_owning(),
95            opts,
96            &mut err,
97        )
98    };
99    choose!(err, ())
100}