nvim_api/
tabpage.rs

1use std::fmt;
2use std::result::Result as StdResult;
3
4use luajit_bindings::{self as lua, Poppable, Pushable};
5use nvim_types::{
6    self as nvim,
7    conversion::{self, FromObject, ToObject},
8    Object,
9    TabHandle,
10};
11use serde::{Deserialize, Serialize};
12
13use crate::choose;
14use crate::ffi::tabpage::*;
15use crate::iterator::SuperIterator;
16use crate::Result;
17use crate::Window;
18
19/// A wrapper around a Neovim tab handle.
20#[derive(Clone, Eq, PartialEq, Hash, Serialize, Deserialize)]
21pub struct TabPage(pub(crate) TabHandle);
22
23impl fmt::Debug for TabPage {
24    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
25        f.debug_tuple("TabPage").field(&self.0).finish()
26    }
27}
28
29impl fmt::Display for TabPage {
30    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
31        fmt::Debug::fmt(self, f)
32    }
33}
34
35impl<H: Into<TabHandle>> From<H> for TabPage {
36    fn from(handle: H) -> Self {
37        Self(handle.into())
38    }
39}
40
41impl From<TabPage> for Object {
42    fn from(tabpage: TabPage) -> Self {
43        tabpage.0.into()
44    }
45}
46
47impl Poppable for TabPage {
48    unsafe fn pop(
49        lstate: *mut lua::ffi::lua_State,
50    ) -> std::result::Result<Self, lua::Error> {
51        TabHandle::pop(lstate).map(Into::into)
52    }
53}
54
55impl Pushable for TabPage {
56    unsafe fn push(
57        self,
58        lstate: *mut lua::ffi::lua_State,
59    ) -> std::result::Result<std::ffi::c_int, lua::Error> {
60        self.0.push(lstate)
61    }
62}
63
64impl FromObject for TabPage {
65    fn from_object(obj: Object) -> StdResult<Self, conversion::Error> {
66        Ok(TabHandle::from_object(obj)?.into())
67    }
68}
69
70impl TabPage {
71    /// Shorthand for [`get_current_tabpage`](crate::get_current_tabpage).
72    #[inline(always)]
73    pub fn current() -> Self {
74        crate::get_current_tabpage()
75    }
76
77    /// Binding to [`nvim_tabpage_del_var`](https://neovim.io/doc/user/api.html#nvim_tabpage_del_var()).
78    ///
79    /// Removes a tab-scoped (`t:`) variable.
80    pub fn del_var(&mut self, name: &str) -> Result<()> {
81        let mut err = nvim::Error::new();
82        let name = nvim::String::from(name);
83        unsafe { nvim_tabpage_del_var(self.0, name.non_owning(), &mut err) };
84        choose!(err, ())
85    }
86
87    /// Binding to [`nvim_tabpage_get_number`](https://neovim.io/doc/user/api.html#nvim_tabpage_get_number()).
88    ///
89    /// Gets the tabpage number.
90    pub fn get_number(&self) -> Result<u32> {
91        let mut err = nvim::Error::new();
92        let number = unsafe { nvim_tabpage_get_number(self.0, &mut err) };
93        choose!(err, Ok(number.try_into().expect("always positive")))
94    }
95
96    /// Binding to [`nvim_tabpage_get_var`][1].
97    ///
98    /// Gets a tab-scoped (`t:`) variable.
99    ///
100    /// [1]: https://neovim.io/doc/user/api.html#nvim_tabpage_get_var()
101    pub fn get_var<Var>(&self, name: &str) -> Result<Var>
102    where
103        Var: FromObject,
104    {
105        let mut err = nvim::Error::new();
106        let name = nvim::String::from(name);
107        let obj = unsafe {
108            nvim_tabpage_get_var(self.0, name.non_owning(), &mut err)
109        };
110        choose!(err, Ok(Var::from_object(obj)?))
111    }
112
113    /// Binding to [`nvim_tabpage_get_win`](https://neovim.io/doc/user/api.html#nvim_tabpage_get_win()).
114    ///
115    /// Gets the current window in a tabpage.
116    pub fn get_win(&self) -> Result<Window> {
117        let mut err = nvim::Error::new();
118        let handle = unsafe { nvim_tabpage_get_win(self.0, &mut err) };
119        choose!(err, Ok(handle.into()))
120    }
121
122    /// Binding to [`nvim_tabpage_is_valid`](https://neovim.io/doc/user/api.html#nvim_tabpage_is_valid()).
123    ///
124    /// Checks if a tabpage is valid.
125    pub fn is_valid(&self) -> bool {
126        unsafe { nvim_tabpage_is_valid(self.0) }
127    }
128
129    /// Binding to [`nvim_tabpage_list_wins`](https://neovim.io/doc/user/api.html#nvim_tabpage_list_wins()).
130    ///
131    /// Gets the windows in a tabpage.
132    pub fn list_wins(&self) -> Result<impl SuperIterator<Window>> {
133        let mut err = nvim::Error::new();
134        let list = unsafe { nvim_tabpage_list_wins(self.0, &mut err) };
135        choose!(
136            err,
137            Ok({
138                list.into_iter().map(|obj| Window::from_object(obj).unwrap())
139            })
140        )
141    }
142
143    /// Binding to [`nvim_tabpage_set_var`](https://neovim.io/doc/user/api.html#nvim_tabpage_set_var()).
144    ///
145    /// Sets a tab-scoped (`t:`) variable.
146    pub fn set_var<Var>(&mut self, name: &str, value: Var) -> Result<()>
147    where
148        Var: ToObject,
149    {
150        let mut err = nvim::Error::new();
151        let name = nvim::String::from(name);
152        unsafe {
153            nvim_tabpage_set_var(
154                self.0,
155                name.non_owning(),
156                value.to_object()?.non_owning(),
157                &mut err,
158            )
159        };
160        choose!(err, ())
161    }
162}