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
use std::fmt;
use std::result::Result as StdResult;

use luajit::{self as lua, Poppable, Pushable};
use serde::{Deserialize, Serialize};
use types::{
    self as nvim,
    conversion::{self, FromObject, ToObject},
    Object,
    TabHandle,
};

use crate::choose;
use crate::ffi::tabpage::*;
use crate::Result;
use crate::SuperIterator;
use crate::Window;

/// A wrapper around a Neovim tab handle.
#[derive(Clone, Eq, PartialEq, Hash, Serialize, Deserialize)]
pub struct TabPage(pub(crate) TabHandle);

impl fmt::Debug for TabPage {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.debug_tuple("TabPage").field(&self.0).finish()
    }
}

impl fmt::Display for TabPage {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        fmt::Debug::fmt(self, f)
    }
}

impl<H: Into<TabHandle>> From<H> for TabPage {
    fn from(handle: H) -> Self {
        Self(handle.into())
    }
}

impl From<TabPage> for Object {
    fn from(tabpage: TabPage) -> Self {
        tabpage.0.into()
    }
}

impl Poppable for TabPage {
    unsafe fn pop(
        lstate: *mut lua::ffi::lua_State,
    ) -> std::result::Result<Self, lua::Error> {
        TabHandle::pop(lstate).map(Into::into)
    }
}

impl Pushable for TabPage {
    unsafe fn push(
        self,
        lstate: *mut lua::ffi::lua_State,
    ) -> std::result::Result<std::ffi::c_int, lua::Error> {
        self.0.push(lstate)
    }
}

impl FromObject for TabPage {
    fn from_object(obj: Object) -> StdResult<Self, conversion::Error> {
        Ok(TabHandle::from_object(obj)?.into())
    }
}

impl TabPage {
    /// Shorthand for [`get_current_tabpage`](crate::get_current_tabpage).
    #[inline(always)]
    pub fn current() -> Self {
        crate::get_current_tabpage()
    }

    /// Binding to [`nvim_tabpage_del_var()`][1].
    ///
    /// Removes a tab-scoped (`t:`) variable.
    ///
    /// [1]: https://neovim.io/doc/user/api.html#nvim_tabpage_del_var()
    pub fn del_var(&mut self, name: &str) -> Result<()> {
        let mut err = nvim::Error::new();
        let name = nvim::String::from(name);
        unsafe { nvim_tabpage_del_var(self.0, name.non_owning(), &mut err) };
        choose!(err, ())
    }

    /// Binding to [`nvim_tabpage_get_number()`][1].
    ///
    /// Gets the tabpage number.
    ///
    /// [1]: https://neovim.io/doc/user/api.html#nvim_tabpage_get_number()
    pub fn get_number(&self) -> Result<u32> {
        let mut err = nvim::Error::new();
        let number = unsafe { nvim_tabpage_get_number(self.0, &mut err) };
        choose!(err, Ok(number.try_into().expect("always positive")))
    }

    /// Binding to [`nvim_tabpage_get_var()`][1].
    ///
    /// Gets a tab-scoped (`t:`) variable.
    ///
    /// [1]: https://neovim.io/doc/user/api.html#nvim_tabpage_get_var()
    pub fn get_var<Var>(&self, name: &str) -> Result<Var>
    where
        Var: FromObject,
    {
        let mut err = nvim::Error::new();
        let name = nvim::String::from(name);
        let obj = unsafe {
            nvim_tabpage_get_var(
                self.0,
                name.non_owning(),
                #[cfg(feature = "neovim-0-10")] // On 0.10 and nightly.
                types::arena(),
                &mut err,
            )
        };
        choose!(err, Ok(Var::from_object(obj)?))
    }

    /// Binding to [`nvim_tabpage_get_win()`][1].
    ///
    /// Gets the current window in a tabpage.
    ///
    /// [1]: https://neovim.io/doc/user/api.html#nvim_tabpage_get_win()
    pub fn get_win(&self) -> Result<Window> {
        let mut err = nvim::Error::new();
        let handle = unsafe { nvim_tabpage_get_win(self.0, &mut err) };
        choose!(err, Ok(handle.into()))
    }

    /// Binding to [`nvim_tabpage_is_valid()`][1].
    ///
    /// Checks if a tabpage is valid.
    ///
    /// [1]: https://neovim.io/doc/user/api.html#nvim_tabpage_is_valid()
    pub fn is_valid(&self) -> bool {
        unsafe { nvim_tabpage_is_valid(self.0) }
    }

    /// Binding to [`nvim_tabpage_list_wins()`][1].
    ///
    /// Gets the windows in a tabpage.
    ///
    /// [1]: https://neovim.io/doc/user/api.html#nvim_tabpage_list_wins()
    pub fn list_wins(&self) -> Result<impl SuperIterator<Window>> {
        let mut err = nvim::Error::new();
        let list = unsafe {
            nvim_tabpage_list_wins(
                self.0,
                #[cfg(feature = "neovim-0-10")] // On 0.10 and nightly.
                types::arena(),
                &mut err,
            )
        };
        choose!(
            err,
            Ok({
                list.into_iter().map(|obj| Window::from_object(obj).unwrap())
            })
        )
    }

    /// Binding to [`nvim_tabpage_set_var()`][1].
    ///
    /// Sets a tab-scoped (`t:`) variable.
    ///
    /// [1]: https://neovim.io/doc/user/api.html#nvim_tabpage_set_var()
    pub fn set_var<Var>(&mut self, name: &str, value: Var) -> Result<()>
    where
        Var: ToObject,
    {
        let mut err = nvim::Error::new();
        let name = nvim::String::from(name);
        unsafe {
            nvim_tabpage_set_var(
                self.0,
                name.non_owning(),
                value.to_object()?.non_owning(),
                &mut err,
            )
        };
        choose!(err, ())
    }

    /// Binding to [`nvim_tabpage_set_win()`][1].
    ///
    /// Sets the current window in the tabpage.
    ///
    /// [1]: https://neovim.io/doc/user/api.html#nvim_tabpage_set_win()
    #[cfg(feature = "neovim-0-10")] // On 0.10 and nightly.
    #[cfg_attr(
        docsrs,
        doc(cfg(any(feature = "neovim-0-10", feature = "neovim-nightly")))
    )]
    pub fn set_win(&mut self, win: &Window) -> Result<()> {
        let mut err = nvim::Error::new();
        unsafe { nvim_tabpage_set_win(self.0, win.0, &mut err) };
        choose!(err, ())
    }
}