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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
use std::error::Error as StdError;
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},
    Array,
    Function,
    Integer,
    Object,
    WinHandle,
};

use crate::choose;
use crate::ffi::window::*;
#[cfg(feature = "neovim-0-10")] // On 0.10 and nightly.
use crate::opts::WinTextHeightOpts;
#[cfg(feature = "neovim-0-10")] // On 0.10 and nightly.
use crate::types::WinTextHeightInfos;
use crate::Result;
use crate::{Buffer, IntoResult, TabPage};

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

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

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

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

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

impl From<&Window> for Object {
    fn from(win: &Window) -> Self {
        win.0.into()
    }
}

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

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

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

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

    /// Binding to [`nvim_win_call()`][1].
    ///
    /// Calls a function with this window as the temporary current window.
    ///
    /// [1]: https://neovim.io/doc/user/api.html#nvim_win_call()
    pub fn call<F, Res, Ret>(&self, fun: F) -> Result<Ret>
    where
        F: FnOnce(()) -> Res + 'static,
        Res: IntoResult<Ret>,
        Res::Error: StdError + 'static,
        Ret: Pushable + FromObject,
    {
        let fun = Function::from_fn_once(fun);
        let mut err = nvim::Error::new();
        let obj = unsafe { nvim_win_call(self.0, fun.lua_ref(), &mut err) };

        choose!(err, {
            fun.remove_from_lua_registry();
            Ok(Ret::from_object(obj)?)
        })
    }

    /// Binding to [`nvim_win_close()`][1].
    ///
    /// Closes the window. Not allowed when
    /// [`textlock`](https://neovim.io/doc/user/eval.html#textlock) is active.
    ///
    /// [1]: https://neovim.io/doc/user/api.html#nvim_win_close()
    pub fn close(self, force: bool) -> Result<()> {
        let mut err = nvim::Error::new();
        unsafe { nvim_win_close(self.0, force, &mut err) };
        choose!(err, ())
    }

    /// Binding to [`nvim_win_del_var()`][1].
    ///
    /// Removes a window-scoped (`w:`) variable.
    ///
    /// [1]: https://neovim.io/doc/user/api.html#nvim_win_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_win_del_var(self.0, name.non_owning(), &mut err) };
        choose!(err, ())
    }

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

    /// Binding to [`nvim_win_get_cursor()`][1].
    ///
    /// Gets the (1,0)-indexed cursor position in the window.
    ///
    /// [1]: https://neovim.io/doc/user/api.html#nvim_win_get_cursor()
    pub fn get_cursor(&self) -> Result<(usize, usize)> {
        let mut err = nvim::Error::new();
        let arr = unsafe {
            nvim_win_get_cursor(
                self.0,
                #[cfg(feature = "neovim-0-10")] // On 0.10 and nightly.
                types::arena(),
                &mut err,
            )
        };
        choose!(err, {
            let mut iter = arr.into_iter();
            let line = usize::from_object(iter.next().unwrap())?;
            let col = usize::from_object(iter.next().unwrap())?;
            Ok((line, col))
        })
    }

    /// Binding to [`nvim_win_get_height()`][1].
    ///
    /// Gets the window height as a count of rows.
    ///
    /// [1]: https://neovim.io/doc/user/api.html#nvim_win_get_height()
    pub fn get_height(&self) -> Result<u32> {
        let mut err = nvim::Error::new();
        let height = unsafe { nvim_win_get_height(self.0, &mut err) };
        choose!(err, Ok(height.try_into().expect("always positive")))
    }

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

    /// Binding to [`nvim_win_get_position()`][1].
    ///
    /// Gets the window position in display cells.
    ///
    /// [1]: https://neovim.io/doc/user/api.html#nvim_win_get_position()
    pub fn get_position(&self) -> Result<(usize, usize)> {
        let mut err = nvim::Error::new();
        let arr = unsafe {
            nvim_win_get_position(
                self.0,
                #[cfg(feature = "neovim-0-10")] // On 0.10 and nightly.
                types::arena(),
                &mut err,
            )
        };
        choose!(err, {
            let mut iter = arr.into_iter();
            let line = usize::from_object(iter.next().unwrap())?;
            let col = usize::from_object(iter.next().unwrap())?;
            Ok((line, col))
        })
    }

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

    /// Binding to [`nvim_win_get_var()`][1].
    ///
    /// Gets a window-scoped (`w:`) variable.
    ///
    /// [1]: https://neovim.io/doc/user/api.html#nvim_win_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_win_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_win_get_width()`][1].
    ///
    /// Gets the window width as a number of columns.
    ///
    /// [1]: https://neovim.io/doc/user/api.html#nvim_win_get_width()
    pub fn get_width(&self) -> Result<u32> {
        let mut err = nvim::Error::new();
        let width = unsafe { nvim_win_get_width(self.0, &mut err) };
        choose!(err, Ok(width.try_into().expect("always positive")))
    }

    /// Binding to [`nvim_win_hide()`][1].
    ///
    /// Closes the window and hides the buffer it contains.
    ///
    /// [1]: https://neovim.io/doc/user/api.html#nvim_win_hide()
    pub fn hide(self) -> Result<()> {
        let mut err = nvim::Error::new();
        unsafe { nvim_win_hide(self.0, &mut err) };
        choose!(err, ())
    }

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

    /// Binding to [`nvim_win_set_buf()`][1].
    ///
    /// Sets `buffer` as the current buffer in the window.
    ///
    /// [1]: https://neovim.io/doc/user/api.html#nvim_win_set_buf()
    pub fn set_buf(&mut self, buffer: &Buffer) -> Result<()> {
        let mut err = nvim::Error::new();
        unsafe { nvim_win_set_buf(self.0, buffer.0, &mut err) };
        choose!(err, ())
    }

    /// Binding to [`nvim_win_set_cursor()`][1].
    ///
    /// Sets the (1,0)-indexed cursor in the window. This will scroll the
    /// window even if it's not the current one.
    ///
    /// [1]: https://neovim.io/doc/user/api.html#nvim_win_set_cursor()
    pub fn set_cursor(&mut self, line: usize, col: usize) -> Result<()> {
        let mut err = nvim::Error::new();
        let pos = Array::from_iter([line as Integer, col as Integer]);
        unsafe { nvim_win_set_cursor(self.0, pos.non_owning(), &mut err) };
        choose!(err, ())
    }

    /// Binding to [`nvim_win_set_height()`][1].
    ///
    /// Sets the window height.
    ///
    /// [1]: https://neovim.io/doc/user/api.html#nvim_win_set_height()
    pub fn set_height(&mut self, height: u32) -> Result<()> {
        let mut err = nvim::Error::new();
        unsafe { nvim_win_set_height(self.0, height.into(), &mut err) };
        choose!(err, ())
    }

    /// Binding to [`nvim_win_set_hl()`][1].
    ///
    /// Sets the highlight namespace for this window. This will the highlights
    /// defined with [`set_hl`](crate::set_hl) for the given namespace, but
    /// fall back to global highlights (`ns_id = 0`) if those are missing.
    ///
    /// This takes precedence over the `winhighlight` option.
    ///
    /// [1]: https://neovim.io/doc/user/api.html#nvim_win_set_hl()
    #[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_hl(&mut self, ns_id: u32) -> Result<()> {
        let mut err = nvim::Error::new();
        unsafe { nvim_win_set_hl(self.0, ns_id.into(), &mut err) };
        choose!(err, ())
    }

    /// Binding to [`nvim_win_set_var()`][1].
    ///
    /// Sets a window-scoped (`w:`) variable.
    ///
    /// [1]: https://neovim.io/doc/user/api.html#nvim_win_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_win_set_var(
                self.0,
                name.non_owning(),
                value.to_object()?.non_owning(),
                &mut err,
            )
        };
        choose!(err, ())
    }

    /// Binding to [`nvim_win_set_width()`][1].
    ///
    /// Sets the window width.
    ///
    /// [1]: https://neovim.io/doc/user/api.html#nvim_win_set_width()
    pub fn set_width(&mut self, width: u32) -> Result<()> {
        let mut err = nvim::Error::new();
        unsafe { nvim_win_set_width(self.0, width.into(), &mut err) };
        choose!(err, ())
    }

    /// Binding to [`nvim_win_text_height()`][1].
    ///
    /// Computes the number of screen lines occupied by a range of text in a
    /// given window. Works for off-screen text and takes folds into account.
    ///
    /// [1]: https://neovim.io/doc/user/api.html#nvim_win_text_height()
    #[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 text_height(
        &self,
        opts: &WinTextHeightOpts,
    ) -> Result<WinTextHeightInfos> {
        let mut err = nvim::Error::new();
        let dict = unsafe {
            nvim_win_text_height(self.0, opts, types::arena(), &mut err)
        };
        choose!(err, dict.try_into().map_err(Into::into))
    }
}