nvim_api/
win_config.rs

1use nvim_types::{self as nvim, conversion::FromObject};
2
3use crate::choose;
4use crate::ffi::win_config::*;
5use crate::types::*;
6use crate::Result;
7use crate::{Buffer, Window};
8
9/// Binding to [`nvim_open_win`](https://neovim.io/doc/user/api.html#nvim_open_win()).
10///
11/// Opens a new floating or external window.
12pub fn open_win(
13    buf: &Buffer,
14    enter: bool,
15    config: &WindowConfig,
16) -> Result<Window> {
17    let mut err = nvim::Error::new();
18    let handle =
19        unsafe { nvim_open_win(buf.0, enter, &config.into(), &mut err) };
20    choose!(err, Ok(handle.into()))
21}
22
23impl Window {
24    /// Binding to [`nvim_win_get_config`](https://neovim.io/doc/user/api.html#nvim_win_get_config()).
25    ///
26    /// Gets the window configuration.
27    pub fn get_config(&self) -> Result<WindowConfig> {
28        let mut err = nvim::Error::new();
29        let mut dict = unsafe { nvim_win_get_config(self.0, &mut err) };
30        let win = dict.get(&"win").map(|obj| unsafe {
31            // SAFETY: if the `win` key is present it's set to an integer
32            // representing a window handle.
33            obj.as_integer_unchecked() as i32
34        });
35        if let Some(handle) = win {
36            dict["relative"] = handle.into();
37        }
38        choose!(err, Ok(WindowConfig::from_object(dict.into())?))
39    }
40
41    /// Binding to [`nvim_win_get_config`](https://neovim.io/doc/user/api.html#nvim_win_get_config()).
42    ///
43    /// Configures the window layout. Only for floating and external windows.
44    pub fn set_config(&mut self, config: &WindowConfig) -> Result<()> {
45        let mut err = nvim::Error::new();
46        unsafe { nvim_win_set_config(self.0, &config.into(), &mut err) };
47        choose!(err, ())
48    }
49}