robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

robinmerge: 1dc9728daa1d8a2702

master
140.8 KiB4082 linesraw
1local t = require('test.testutil')
2local n = require('test.functional.testnvim')()
3local Screen = require('test.functional.ui.screen')
4local tt = require('test.functional.testterm')
5
6local describe, it, before_each, after_each, finally =
7  t.describe, t.it, t.before_each, t.after_each, t.finally
8local clear, curbuf, curbuf_contents, curwin, eq, neq, matches, ok, feed, insert, eval =
9  n.clear,
10  n.api.nvim_get_current_buf,
11  n.curbuf_contents,
12  n.api.nvim_get_current_win,
13  t.eq,
14  t.neq,
15  t.matches,
16  t.ok,
17  n.feed,
18  n.insert,
19  n.eval
20local poke_eventloop = n.poke_eventloop
21local exec = n.exec
22local exec_lua = n.exec_lua
23local fn = n.fn
24local request = n.request
25local NIL = vim.NIL
26local api = n.api
27local command = n.command
28local pcall_err = t.pcall_err
29local assert_alive = n.assert_alive
30
31describe('API/win', function()
32  before_each(clear)
33
34  describe('get_buf', function()
35    it('works', function()
36      eq(curbuf(), api.nvim_win_get_buf(api.nvim_list_wins()[1]))
37      command('new')
38      api.nvim_set_current_win(api.nvim_list_wins()[2])
39      eq(curbuf(), api.nvim_win_get_buf(api.nvim_list_wins()[2]))
40      neq(
41        api.nvim_win_get_buf(api.nvim_list_wins()[1]),
42        api.nvim_win_get_buf(api.nvim_list_wins()[2])
43      )
44    end)
45  end)
46
47  describe('set_buf', function()
48    it('works', function()
49      command('new')
50      local windows = api.nvim_list_wins()
51      neq(api.nvim_win_get_buf(windows[1]), api.nvim_win_get_buf(windows[2]))
52      api.nvim_win_set_buf(windows[2], api.nvim_win_get_buf(windows[1]))
53      eq(api.nvim_win_get_buf(windows[1]), api.nvim_win_get_buf(windows[2]))
54    end)
55
56    it('validates args', function()
57      eq('Invalid buffer id: 23', pcall_err(api.nvim_win_set_buf, api.nvim_get_current_win(), 23))
58      eq('Invalid window id: 23', pcall_err(api.nvim_win_set_buf, 23, api.nvim_get_current_buf()))
59      -- When both are invalid the window is reported, so the message allocated
60      -- for it is not overwritten and leaked.
61      eq('Invalid window id: 23', pcall_err(api.nvim_win_set_buf, 23, 24))
62    end)
63
64    it('in cmdwin #40312', function()
65      local new_buf = api.nvim_create_buf(true, true)
66      local old_win = api.nvim_get_current_win()
67      local new_win = api.nvim_open_win(new_buf, false, {
68        relative = 'editor',
69        row = 10,
70        col = 10,
71        width = 50,
72        height = 10,
73      })
74      feed('q:')
75      n.poke_eventloop()
76      -- Replacing the cmdwin's own buffer is allowed.
77      api.nvim_win_set_buf(0, new_buf)
78      -- Also other windows can be touched freely.
79      local next_buf = api.nvim_create_buf(true, true)
80      api.nvim_win_set_buf(new_win, next_buf)
81      eq(next_buf, api.nvim_win_get_buf(new_win))
82      -- Doesn't matter that old_win was the caller; no cmdwin guard.
83      api.nvim_win_set_buf(old_win, new_buf)
84      eq(new_buf, api.nvim_win_get_buf(old_win))
85    end)
86
87    describe("with 'autochdir'", function()
88      local topdir
89      local otherbuf
90      local oldwin
91      local newwin
92
93      before_each(function()
94        command('set shellslash')
95        topdir = fn.getcwd()
96        t.mkdir(topdir .. '/Xacd')
97        t.mkdir(topdir .. '/Xacd/foo')
98        otherbuf = api.nvim_create_buf(false, true)
99        api.nvim_buf_set_name(otherbuf, topdir .. '/Xacd/baz.txt')
100
101        command('set autochdir')
102        command('edit Xacd/foo/bar.txt')
103        eq(topdir .. '/Xacd/foo', fn.getcwd())
104
105        oldwin = api.nvim_get_current_win()
106        command('vsplit')
107        newwin = api.nvim_get_current_win()
108      end)
109
110      after_each(function()
111        n.rmdir(topdir .. '/Xacd')
112      end)
113
114      it('does not change cwd with non-current window', function()
115        api.nvim_win_set_buf(oldwin, otherbuf)
116        eq(topdir .. '/Xacd/foo', fn.getcwd())
117      end)
118
119      it('changes cwd with current window', function()
120        api.nvim_win_set_buf(newwin, otherbuf)
121        eq(topdir .. '/Xacd', fn.getcwd())
122      end)
123    end)
124  end)
125
126  describe('{get,set}_cursor', function()
127    it('works', function()
128      eq({ 1, 0 }, api.nvim_win_get_cursor(0))
129      command('normal ityping\027o  some text')
130      eq('typing\n  some text', curbuf_contents())
131      eq({ 2, 10 }, api.nvim_win_get_cursor(0))
132      api.nvim_win_set_cursor(0, { 2, 6 })
133      command('normal i dumb')
134      eq('typing\n  some dumb text', curbuf_contents())
135    end)
136
137    it('no memory leak when using invalid window ID with invalid pos', function()
138      eq('Invalid window id: 1', pcall_err(api.nvim_win_set_cursor, 1, { 'b\na' }))
139    end)
140
141    it('updates the screen, and also when the window is unfocused', function()
142      local screen = Screen.new(30, 9)
143
144      insert('prologue')
145      feed('100o<esc>')
146      insert('epilogue')
147      local win = curwin()
148      feed('gg')
149
150      local s1 = [[
151        ^prologue                      |
152                                      |*8
153      ]]
154      screen:expect(s1)
155      -- cursor position is at beginning
156      eq({ 1, 0 }, api.nvim_win_get_cursor(win))
157
158      -- move cursor to end
159      api.nvim_win_set_cursor(win, { 101, 0 })
160      screen:expect([[
161                                      |*7
162        ^epilogue                      |
163                                      |
164      ]])
165
166      -- move cursor to the beginning again
167      api.nvim_win_set_cursor(win, { 1, 0 })
168      screen:expect(s1)
169
170      -- move focus to new window
171      command('new')
172      neq(win, curwin())
173
174      -- sanity check, cursor position is kept
175      eq({ 1, 0 }, api.nvim_win_get_cursor(win))
176      local s2 = [[
177        ^                              |
178        {1:~                             }|*2
179        {3:[No Name]                     }|
180        prologue                      |
181                                      |*2
182        {2:[No Name] [+]                 }|
183                                      |
184      ]]
185      screen:expect(s2)
186
187      -- move cursor to end
188      api.nvim_win_set_cursor(win, { 101, 0 })
189      screen:expect([[
190        ^                              |
191        {1:~                             }|*2
192        {3:[No Name]                     }|
193                                      |*2
194        epilogue                      |
195        {2:[No Name] [+]                 }|
196                                      |
197      ]])
198
199      -- move cursor to the beginning again
200      api.nvim_win_set_cursor(win, { 1, 0 })
201      screen:expect(s2)
202
203      -- curwin didn't change back
204      neq(win, curwin())
205    end)
206
207    it('remembers what column it wants to be in', function()
208      insert('first line')
209      feed('o<esc>')
210      insert('second line')
211
212      feed('gg')
213      poke_eventloop() -- let nvim process the 'gg' command
214
215      -- cursor position is at beginning
216      local win = curwin()
217      eq({ 1, 0 }, api.nvim_win_get_cursor(win))
218
219      -- move cursor to column 5
220      api.nvim_win_set_cursor(win, { 1, 5 })
221
222      -- move down a line
223      feed('j')
224      poke_eventloop() -- let nvim process the 'j' command
225
226      -- cursor is still in column 5
227      eq({ 2, 5 }, api.nvim_win_get_cursor(win))
228    end)
229
230    it('updates cursorline and statusline ruler in non-current window', function()
231      local screen = Screen.new(60, 8)
232      command('set ruler')
233      command('set cursorline')
234      insert([[
235        aaa
236        bbb
237        ccc
238        ddd]])
239      local oldwin = curwin()
240      command('vsplit')
241      screen:expect([[
242        aaa                           │aaa                          |
243        bbb                           │bbb                          |
244        ccc                           │ccc                          |
245        {21:dd^d                           }│{21:ddd                          }|
246        {1:~                             }│{1:~                            }|*2
247        {3:< Name] [+] 4,3            All }{2:<Name] [+] 4,3            All}|
248                                                                    |
249      ]])
250      api.nvim_win_set_cursor(oldwin, { 1, 0 })
251      screen:expect([[
252        aaa                           │{21:aaa                          }|
253        bbb                           │bbb                          |
254        ccc                           │ccc                          |
255        {21:dd^d                           }│ddd                          |
256        {1:~                             }│{1:~                            }|*2
257        {3:< Name] [+] 4,3            All }{2:<Name] [+] 1,1            All}|
258                                                                    |
259      ]])
260    end)
261
262    it('updates cursorcolumn in non-current window', function()
263      local screen = Screen.new(60, 8)
264      command('set cursorcolumn')
265      insert([[
266        aaa
267        bbb
268        ccc
269        ddd]])
270      local oldwin = curwin()
271      command('vsplit')
272      screen:expect([[
273        aa{21:a}                           │aa{21:a}                          |
274        bb{21:b}                           │bb{21:b}                          |
275        cc{21:c}                           │cc{21:c}                          |
276        dd^d                           │ddd                          |
277        {1:~                             }│{1:~                            }|*2
278        {3:[No Name] [+]                  }{2:[No Name] [+]                }|
279                                                                    |
280      ]])
281      api.nvim_win_set_cursor(oldwin, { 2, 0 })
282      screen:expect([[
283        aa{21:a}                           │{21:a}aa                          |
284        bb{21:b}                           │bbb                          |
285        cc{21:c}                           │{21:c}cc                          |
286        dd^d                           │{21:d}dd                          |
287        {1:~                             }│{1:~                            }|*2
288        {3:[No Name] [+]                  }{2:[No Name] [+]                }|
289                                                                    |
290      ]])
291    end)
292  end)
293
294  describe('resize', function()
295    local function heights(wins)
296      return vim.tbl_map(api.nvim_win_get_height, wins)
297    end
298    local function widths(wins)
299      return vim.tbl_map(api.nvim_win_get_width, wins)
300    end
301
302    it('height: default anchor="top"', function()
303      command('split | split')
304      local wins = api.nvim_list_wins()
305      local before = heights(wins)
306      api.nvim_win_resize(wins[2], -1, before[2] + 2, {})
307      -- default anchor top.
308      local after = heights(wins)
309      eq(before[1], after[1])
310      eq(before[2] + 2, after[2])
311      eq(before[3] - 2, after[3])
312    end)
313
314    it('height: anchor="bottom"', function()
315      command('split | split')
316      local wins = api.nvim_list_wins()
317      local before = heights(wins)
318      api.nvim_win_resize(wins[2], -1, before[2] + 2, { anchor = 'bottom' })
319      local after = heights(wins)
320      -- bottom anchor unchanged.
321      eq(before[1] - 2, after[1])
322      eq(before[2] + 2, after[2])
323      eq(before[3], after[3])
324    end)
325
326    it('height: anchor="bottom" in a nested frame', function()
327      -- layout: column[ Top, row[ML|MR], Bottom ]; current window = ML
328      command('split | split | 2wincmd w | vsplit')
329      local ml = api.nvim_get_current_win()
330      local function row_of(w)
331        return fn.win_screenpos(fn.win_id2win(w))[1]
332      end
333      local top, bottom
334      for _, w in ipairs(api.nvim_list_wins()) do
335        if row_of(w) == 1 then
336          top = w
337        end
338        if not bottom or row_of(w) > row_of(bottom) then
339          bottom = w
340        end
341      end
342      local top_h = api.nvim_win_get_height(top)
343      local bottom_h = api.nvim_win_get_height(bottom)
344      api.nvim_win_resize(ml, -1, api.nvim_win_get_height(ml) + 3, { anchor = 'bottom' })
345      -- "bottom" grow the row upward, taking from Top, not Bottom
346      eq(top_h - 3, api.nvim_win_get_height(top))
347      eq(bottom_h, api.nvim_win_get_height(bottom))
348    end)
349
350    it('height: anchor="bottom" with borrowing', function()
351      -- column[ Wupper, row[ col[Ltop|Ltarget], Right ], Wlower ]; current = Ltarget.
352      -- Growing Ltarget more than Ltop can supply forces us to borrow from row
353      -- and row's growth must respect given anchor.
354      command('split | split | 2wincmd w | vsplit | split | wincmd j')
355      local target = api.nvim_get_current_win()
356      local function row_of(w)
357        return fn.win_screenpos(fn.win_id2win(w))[1]
358      end
359      local wupper, wlower
360      for _, w in ipairs(api.nvim_list_wins()) do
361        if row_of(w) == 1 then
362          wupper = w
363        end
364        if not wlower or row_of(w) > row_of(wlower) then
365          wlower = w
366        end
367      end
368      local wupper_h = api.nvim_win_get_height(wupper)
369      local wlower_h = api.nvim_win_get_height(wlower)
370      api.nvim_win_resize(target, -1, api.nvim_win_get_height(target) + 6, { anchor = 'bottom' })
371      -- must borrow from above.
372      ok(api.nvim_win_get_height(wupper) < wupper_h)
373      eq(wlower_h, api.nvim_win_get_height(wlower))
374    end)
375
376    it('width: default anchor="left"', function()
377      command('vsplit | vsplit')
378      local wins = api.nvim_list_wins()
379      local before = widths(wins)
380      api.nvim_win_resize(wins[2], before[2] + 4, -1, {})
381      local after = widths(wins)
382      eq(before[1], after[1])
383      eq(before[2] + 4, after[2])
384      eq(before[3] - 4, after[3])
385    end)
386
387    it('width: anchor="right"', function()
388      command('vsplit | vsplit')
389      local wins = api.nvim_list_wins()
390      local before = widths(wins)
391      api.nvim_win_resize(wins[2], before[2] + 4, -1, { anchor = 'right' })
392      local after = widths(wins)
393      eq(before[1] - 4, after[1])
394      eq(before[2] + 4, after[2])
395      eq(before[3], after[3])
396    end)
397
398    it('width: anchor="right" a nested frame', function()
399      -- layout: row[ Left, col[MT|MB], Right ]; current window = MT
400      command('vsplit | vsplit | 2wincmd w | split')
401      local mt = api.nvim_get_current_win()
402      local function col_of(w)
403        return fn.win_screenpos(fn.win_id2win(w))[2]
404      end
405      local left, right
406      for _, w in ipairs(api.nvim_list_wins()) do
407        if col_of(w) == 1 then
408          left = w
409        end
410        if not right or col_of(w) > col_of(right) then
411          right = w
412        end
413      end
414      local left_w = api.nvim_win_get_width(left)
415      local right_w = api.nvim_win_get_width(right)
416      api.nvim_win_resize(mt, api.nvim_win_get_width(mt) + 3, -1, { anchor = 'right' })
417      -- "right" must grow the column leftward, taking from Left, not Right
418      eq(left_w - 3, api.nvim_win_get_width(left))
419      eq(right_w, api.nvim_win_get_width(right))
420    end)
421
422    it('sets both height and width in a single call', function()
423      -- layout: column[ Top, row[ML|MR], Bottom ]; current = ML.
424      command('split | split | 2wincmd w | vsplit')
425      local ml = api.nvim_get_current_win()
426      local function row_of(w)
427        return fn.win_screenpos(fn.win_id2win(w))[1]
428      end
429      local top, bottom, mr
430      for _, w in ipairs(api.nvim_list_wins()) do
431        if row_of(w) == 1 then
432          top = w
433        end
434        if not bottom or row_of(w) > row_of(bottom) then
435          bottom = w
436        end
437        if row_of(w) == row_of(ml) and w ~= ml then
438          mr = w
439        end
440      end
441      local top_h = api.nvim_win_get_height(top)
442      local bottom_h = api.nvim_win_get_height(bottom)
443      local mr_w = api.nvim_win_get_width(mr)
444      local h, w = api.nvim_win_get_height(ml), api.nvim_win_get_width(ml)
445      -- "bottom" anchors the height axis; width has no matching anchor so it defaults to "left"
446      api.nvim_win_resize(ml, w - 4, h + 2, { anchor = 'bottom' })
447      eq(h + 2, api.nvim_win_get_height(ml))
448      eq(w - 4, api.nvim_win_get_width(ml))
449      -- height took from above
450      eq(top_h - 2, api.nvim_win_get_height(top))
451      eq(bottom_h, api.nvim_win_get_height(bottom))
452      -- width default to "left"
453      eq(mr_w + 4, api.nvim_win_get_width(mr))
454    end)
455
456    it('correctly handles height=1', function()
457      command('split')
458      api.nvim_set_current_win(api.nvim_list_wins()[1])
459      api.nvim_win_resize(api.nvim_list_wins()[2], -1, 1, {})
460      eq(1, api.nvim_win_get_height(api.nvim_list_wins()[2]))
461    end)
462
463    it('correctly handles height=1 with a winbar', function()
464      command('set winbar=foobar')
465      command('set winminheight=0')
466      command('split')
467      api.nvim_set_current_win(api.nvim_list_wins()[1])
468      api.nvim_win_resize(api.nvim_list_wins()[2], -1, 1, {})
469      eq(1, api.nvim_win_get_height(api.nvim_list_wins()[2]))
470    end)
471
472    it('does not cause ml_get errors with foldmethod=expr #19989', function()
473      insert([[
474        aaaaa
475        bbbbb
476        ccccc]])
477      command('set foldmethod=expr')
478      exec([[
479        new
480        let w = nvim_get_current_win()
481        wincmd w
482        call nvim_win_resize(w, -1, 5, #{})
483      ]])
484      feed('l')
485      eq('', api.nvim_get_vvar('errmsg'))
486    end)
487
488    it(
489      'accepts size = 0 (clamp to minimum size). Compatible with deprecated set_height/width',
490      function()
491        command('set winminheight=1')
492        command('split')
493        api.nvim_set_current_win(api.nvim_list_wins()[1])
494        api.nvim_win_resize(api.nvim_list_wins()[2], -1, 0, {})
495        eq(1, api.nvim_win_get_height(api.nvim_list_wins()[2]))
496      end
497    )
498
499    it('validation', function()
500      command('split')
501      eq("Required: 'height' or 'width'", pcall_err(api.nvim_win_resize, 0, -1, -1, {}))
502      eq(
503        "Invalid 'width': expected non-negative number or -1",
504        pcall_err(api.nvim_win_resize, 0, -2, 5, {})
505      )
506      eq(
507        "Invalid 'height': expected non-negative number or -1",
508        pcall_err(api.nvim_win_resize, 0, 5, -2, {})
509      )
510      -- anchor not applicable to the dimension(s) being changed
511      eq(
512        "Conflict: 'left' not allowed with 'width'",
513        pcall_err(api.nvim_win_resize, 0, -1, 5, { anchor = 'left' })
514      )
515      -- unrecognized anchor value
516      eq(
517        [[Invalid 'anchor': expected "top", "bottom", "left" or "right", got garbage]],
518        pcall_err(api.nvim_win_resize, 0, -1, 5, { anchor = 'garbage' })
519      )
520      eq('Invalid window id: 999999', pcall_err(api.nvim_win_resize, 999999, 5, -1, {}))
521    end)
522  end)
523
524  describe('{get,set,del}_var', function()
525    it('works', function()
526      api.nvim_win_set_var(0, 'lua', { 1, 2, { ['3'] = 1 } })
527      eq({ 1, 2, { ['3'] = 1 } }, api.nvim_win_get_var(0, 'lua'))
528      eq({ 1, 2, { ['3'] = 1 } }, api.nvim_eval('w:lua'))
529      eq(1, fn.exists('w:lua'))
530      api.nvim_win_del_var(0, 'lua')
531      eq(0, fn.exists('w:lua'))
532      eq('Key not found: lua', pcall_err(api.nvim_win_del_var, 0, 'lua'))
533      api.nvim_win_set_var(0, 'lua', 1)
534      command('lockvar w:lua')
535      eq('Key is locked: lua', pcall_err(api.nvim_win_del_var, 0, 'lua'))
536      eq('Key is locked: lua', pcall_err(api.nvim_win_set_var, 0, 'lua', 1))
537    end)
538
539    it('window_set_var returns the old value', function()
540      local val1 = { 1, 2, { ['3'] = 1 } }
541      local val2 = { 4, 7 }
542      eq(NIL, request('window_set_var', 0, 'lua', val1))
543      eq(val1, request('window_set_var', 0, 'lua', val2))
544    end)
545
546    it('window_del_var returns the old value', function()
547      local val1 = { 1, 2, { ['3'] = 1 } }
548      local val2 = { 4, 7 }
549      eq(NIL, request('window_set_var', 0, 'lua', val1))
550      eq(val1, request('window_set_var', 0, 'lua', val2))
551      eq(val2, request('window_del_var', 0, 'lua'))
552    end)
553  end)
554
555  describe('nvim_get_option_value, nvim_set_option_value', function()
556    it('works', function()
557      api.nvim_set_option_value('colorcolumn', '4,3', {})
558      eq('4,3', api.nvim_get_option_value('colorcolumn', {}))
559      command('set modified hidden')
560      command('enew') -- edit new buffer, window option is preserved
561      eq('4,3', api.nvim_get_option_value('colorcolumn', {}))
562
563      -- global-local option
564      api.nvim_set_option_value('statusline', 'window-status', { win = 0 })
565      eq('window-status', api.nvim_get_option_value('statusline', { win = 0 }))
566      eq(
567        "%<%f %{%nvim_eval_statusline('%h%w%m%r', {'maxwidth': 30}).width > 0 ? '%h%w%m%r ' : ''%}%=%{% &showcmdloc == 'statusline' ? '%-10.S ' : '' %}%{% exists('b:keymap_name') ? '<'..b:keymap_name..'> ' : '' %}%{% &ruler ? &rulerformat : '' %}",
568        api.nvim_get_option_value('statusline', { scope = 'global' })
569      )
570      command('set modified')
571      command('enew') -- global-local: not preserved in new buffer
572      -- confirm local value was not copied
573      eq(
574        "%<%f %{%nvim_eval_statusline('%h%w%m%r', {'maxwidth': 30}).width > 0 ? '%h%w%m%r ' : ''%}%=%{% &showcmdloc == 'statusline' ? '%-10.S ' : '' %}%{% exists('b:keymap_name') ? '<'..b:keymap_name..'> ' : '' %}%{% &ruler ? &rulerformat : '' %}",
575        api.nvim_get_option_value('statusline', { win = 0 })
576      )
577      eq('', eval('&l:statusline'))
578    end)
579
580    it('after switching windows #15390', function()
581      command('tabnew')
582      local tab1 = unpack(api.nvim_list_tabpages())
583      local win1 = unpack(api.nvim_tabpage_list_wins(tab1))
584      api.nvim_set_option_value('statusline', 'window-status', { win = win1 })
585      command('split')
586      command('wincmd J')
587      command('wincmd j')
588      eq('window-status', api.nvim_get_option_value('statusline', { win = win1 }))
589      assert_alive()
590    end)
591
592    describe('after closing', function()
593      local buf, win0, win1, win2
594
595      before_each(function()
596        win0 = api.nvim_get_current_win()
597        command('new')
598        buf = api.nvim_get_current_buf()
599        win1 = api.nvim_get_current_win()
600        command('set numberwidth=10')
601        command('split')
602        win2 = api.nvim_get_current_win()
603        command('set numberwidth=15')
604        command('enew')
605        api.nvim_set_current_win(win1)
606        command('normal ix')
607        command('enew')
608        api.nvim_set_current_win(win0)
609        eq(4, api.nvim_get_option_value('numberwidth', {}))
610      end)
611
612      -- at this point buffer `buf` is current in no windows. Closing shouldn't affect its defaults
613      it('0 windows', function()
614        api.nvim_set_current_buf(buf)
615        eq(10, api.nvim_get_option_value('numberwidth', {}))
616      end)
617
618      it('1 window', function()
619        api.nvim_win_close(win1, false)
620
621        api.nvim_set_current_buf(buf)
622        eq(10, api.nvim_get_option_value('numberwidth', {}))
623      end)
624
625      it('2 windows', function()
626        api.nvim_win_close(win1, false)
627        api.nvim_win_close(win2, false)
628
629        api.nvim_set_current_buf(buf)
630        eq(10, api.nvim_get_option_value('numberwidth', {}))
631      end)
632    end)
633
634    it('returns values for unset local options', function()
635      eq(-1, api.nvim_get_option_value('scrolloff', { win = 0, scope = 'local' }))
636    end)
637  end)
638
639  describe('get_position', function()
640    it('works', function()
641      local height = api.nvim_win_get_height(api.nvim_list_wins()[1])
642      local width = api.nvim_win_get_width(api.nvim_list_wins()[1])
643      command('split')
644      command('vsplit')
645      eq({ 0, 0 }, api.nvim_win_get_position(api.nvim_list_wins()[1]))
646      local vsplit_pos = math.floor(width / 2)
647      local split_pos = math.floor(height / 2)
648      local win2row, win2col = unpack(api.nvim_win_get_position(api.nvim_list_wins()[2]))
649      local win3row, win3col = unpack(api.nvim_win_get_position(api.nvim_list_wins()[3]))
650      eq(0, win2row)
651      eq(0, win3col)
652      ok(vsplit_pos - 1 <= win2col and win2col <= vsplit_pos + 1)
653      ok(split_pos - 1 <= win3row and win3row <= split_pos + 1)
654    end)
655  end)
656
657  describe('get_tabpage', function()
658    it('works', function()
659      command('tabnew')
660      command('vsplit')
661      eq(api.nvim_list_tabpages()[1], api.nvim_win_get_tabpage(api.nvim_list_wins()[1]))
662      eq(api.nvim_list_tabpages()[2], api.nvim_win_get_tabpage(api.nvim_list_wins()[2]))
663      eq(api.nvim_list_tabpages()[2], api.nvim_win_get_tabpage(api.nvim_list_wins()[3]))
664    end)
665  end)
666
667  describe('get_number', function()
668    it('works', function()
669      local wins = api.nvim_list_wins()
670      eq(1, api.nvim_win_get_number(wins[1]))
671
672      command('split')
673      local win1, win2 = unpack(api.nvim_list_wins())
674      eq(1, api.nvim_win_get_number(win1))
675      eq(2, api.nvim_win_get_number(win2))
676
677      command('wincmd J')
678      eq(2, api.nvim_win_get_number(win1))
679      eq(1, api.nvim_win_get_number(win2))
680
681      command('tabnew')
682      local win3 = api.nvim_list_wins()[3]
683      -- First tab page
684      eq(2, api.nvim_win_get_number(win1))
685      eq(1, api.nvim_win_get_number(win2))
686      -- Second tab page
687      eq(1, api.nvim_win_get_number(win3))
688    end)
689  end)
690
691  describe('is_valid', function()
692    it('works', function()
693      command('split')
694      local win = api.nvim_list_wins()[2]
695      api.nvim_set_current_win(win)
696      ok(api.nvim_win_is_valid(win))
697      command('close')
698      ok(not api.nvim_win_is_valid(win))
699    end)
700  end)
701
702  describe('close', function()
703    it('can close current window', function()
704      local oldwin = api.nvim_get_current_win()
705      command('split')
706      local newwin = api.nvim_get_current_win()
707      api.nvim_win_close(newwin, false)
708      eq({ oldwin }, api.nvim_list_wins())
709    end)
710
711    it('can close noncurrent window', function()
712      local oldwin = api.nvim_get_current_win()
713      command('split')
714      local newwin = api.nvim_get_current_win()
715      api.nvim_win_close(oldwin, false)
716      eq({ newwin }, api.nvim_list_wins())
717    end)
718
719    it("handles changed buffer when 'hidden' is unset", function()
720      command('set nohidden')
721      local oldwin = api.nvim_get_current_win()
722      insert('text')
723      command('new')
724      local newwin = api.nvim_get_current_win()
725      eq(
726        'Vim:E37: No write since last change (add ! to override)',
727        pcall_err(api.nvim_win_close, oldwin, false)
728      )
729      eq({ newwin, oldwin }, api.nvim_list_wins())
730    end)
731
732    it('handles changed buffer with force', function()
733      local oldwin = api.nvim_get_current_win()
734      insert('text')
735      command('new')
736      local newwin = api.nvim_get_current_win()
737      api.nvim_win_close(oldwin, true)
738      eq({ newwin }, api.nvim_list_wins())
739    end)
740
741    it('in cmdwin #9767', function()
742      command('split')
743      eq(2, #api.nvim_list_wins())
744      local otherwin = api.nvim_open_win(0, false, {
745        relative = 'editor',
746        row = 10,
747        col = 10,
748        width = 10,
749        height = 10,
750      })
751      feed('q:')
752      n.poke_eventloop()
753      eq(4, #api.nvim_list_wins())
754      eq(':', fn.getcmdwintype())
755      -- Closing other windows still works.
756      api.nvim_win_close(otherwin, true)
757      eq(false, api.nvim_win_is_valid(otherwin))
758      -- Close cmdline-window.
759      api.nvim_win_close(0, true)
760      eq(2, #api.nvim_list_wins())
761      eq('', fn.getcmdwintype())
762    end)
763
764    it('closing current (float) window of another tabpage #15313 #40745', function()
765      command('tabedit')
766      command('botright split')
767      local prevwin = curwin()
768      eq(2, eval('tabpagenr()'))
769      local opts = { relative = 'editor', row = 10, col = 10, width = 50, height = 10 }
770      local win = api.nvim_open_win(0, true, opts)
771      local tab = eval('tabpagenr()')
772      command('tabprevious')
773      eq(1, eval('tabpagenr()'))
774      api.nvim_win_close(win, false)
775
776      eq(prevwin, api.nvim_tabpage_get_win(tab))
777      assert_alive()
778
779      command('tabnext')
780      local win1 = api.nvim_open_win(0, true, opts)
781      local win2 = api.nvim_open_win(0, true, opts)
782      command('tabprevious')
783      -- win1 becomes the tabpage's current window after win2 is closed
784      api.nvim_win_close(win2, false)
785      api.nvim_win_close(win1, false)
786      assert_alive()
787    end)
788
789    it('closing a float does not enter unfocusable or hidden prevwin', function()
790      local firstwin = api.nvim_get_current_win()
791      local wins = {} ---@type integer[]
792      for _ = 1, 4 do
793        wins[#wins + 1] = api.nvim_open_win(0, true, {
794          relative = 'editor',
795          row = 10,
796          col = 10,
797          width = 50,
798          height = 10,
799        })
800      end
801      api.nvim_win_set_config(wins[3], { hide = true })
802      api.nvim_win_close(0, false)
803      eq(firstwin, api.nvim_get_current_win())
804      api.nvim_set_current_win(wins[2])
805      api.nvim_set_current_win(wins[3])
806      api.nvim_win_set_config(wins[2], { focusable = false })
807      api.nvim_win_close(0, false)
808      eq(firstwin, api.nvim_get_current_win())
809      api.nvim_set_current_win(wins[1])
810      api.nvim_set_current_win(wins[2])
811      api.nvim_win_close(0, false)
812      eq(wins[1], api.nvim_get_current_win())
813    end)
814  end)
815
816  describe('hide', function()
817    it('can hide current window', function()
818      local oldwin = api.nvim_get_current_win()
819      command('split')
820      local newwin = api.nvim_get_current_win()
821      api.nvim_win_hide(newwin)
822      eq({ oldwin }, api.nvim_list_wins())
823    end)
824    it('can hide noncurrent window', function()
825      local oldwin = api.nvim_get_current_win()
826      command('split')
827      local newwin = api.nvim_get_current_win()
828      api.nvim_win_hide(oldwin)
829      eq({ newwin }, api.nvim_list_wins())
830    end)
831    it('does not close the buffer', function()
832      local oldwin = api.nvim_get_current_win()
833      local oldbuf = api.nvim_get_current_buf()
834      local buf = api.nvim_create_buf(true, false)
835      local newwin = api.nvim_open_win(buf, true, {
836        relative = 'win',
837        row = 3,
838        col = 3,
839        width = 12,
840        height = 3,
841      })
842      api.nvim_win_hide(newwin)
843      eq({ oldwin }, api.nvim_list_wins())
844      eq({ oldbuf, buf }, api.nvim_list_bufs())
845    end)
846    it('deletes the buffer when bufhidden=wipe', function()
847      local oldwin = api.nvim_get_current_win()
848      local oldbuf = api.nvim_get_current_buf()
849      local buf = api.nvim_create_buf(true, false)
850      local newwin = api.nvim_open_win(buf, true, {
851        relative = 'win',
852        row = 3,
853        col = 3,
854        width = 12,
855        height = 3,
856      })
857      api.nvim_set_option_value('bufhidden', 'wipe', { buf = buf })
858      api.nvim_win_hide(newwin)
859      eq({ oldwin }, api.nvim_list_wins())
860      eq({ oldbuf }, api.nvim_list_bufs())
861    end)
862    it('in cmdwin', function()
863      feed('q:')
864      n.poke_eventloop()
865      api.nvim_win_hide(0)
866      eq('', fn.getcmdwintype())
867    end)
868  end)
869
870  describe('text_height', function()
871    local screen, ns, X
872    before_each(function()
873      screen = Screen.new(45, 22)
874      ns = api.nvim_create_namespace('')
875      X = api.nvim_get_vvar('maxcol')
876    end)
877
878    it('validation', function()
879      insert([[
880        aaa
881        bbb
882        ccc
883        ddd
884        eee]])
885      eq('Invalid window id: 23', pcall_err(api.nvim_win_text_height, 23, {}))
886      eq('Line index out of bounds', pcall_err(api.nvim_win_text_height, 0, { start_row = 5 }))
887      eq('Line index out of bounds', pcall_err(api.nvim_win_text_height, 0, { start_row = -6 }))
888      eq('Line index out of bounds', pcall_err(api.nvim_win_text_height, 0, { end_row = 5 }))
889      eq('Line index out of bounds', pcall_err(api.nvim_win_text_height, 0, { end_row = -6 }))
890      eq(
891        "'start_row' is higher than 'end_row'",
892        pcall_err(api.nvim_win_text_height, 0, { start_row = 3, end_row = 1 })
893      )
894      eq(
895        "'start_vcol' specified without 'start_row'",
896        pcall_err(api.nvim_win_text_height, 0, { end_row = 2, start_vcol = 0 })
897      )
898      eq(
899        "'end_vcol' specified without 'end_row'",
900        pcall_err(api.nvim_win_text_height, 0, { start_row = 2, end_vcol = 0 })
901      )
902      eq(
903        "Invalid 'start_vcol': out of range",
904        pcall_err(api.nvim_win_text_height, 0, { start_row = 2, start_vcol = -1 })
905      )
906      eq(
907        "Invalid 'start_vcol': out of range",
908        pcall_err(api.nvim_win_text_height, 0, { start_row = 2, start_vcol = X + 1 })
909      )
910      eq(
911        "Invalid 'end_vcol': out of range",
912        pcall_err(api.nvim_win_text_height, 0, { end_row = 2, end_vcol = -1 })
913      )
914      eq(
915        "Invalid 'end_vcol': out of range",
916        pcall_err(api.nvim_win_text_height, 0, { end_row = 2, end_vcol = X + 1 })
917      )
918      eq(
919        "Invalid 'max_height': out of range",
920        pcall_err(api.nvim_win_text_height, 0, { max_height = 0 })
921      )
922      eq(
923        "'start_vcol' is higher than 'end_vcol'",
924        pcall_err(
925          api.nvim_win_text_height,
926          0,
927          { start_row = 2, end_row = 2, start_vcol = 10, end_vcol = 5 }
928        )
929      )
930    end)
931
932    it('with two diff windows', function()
933      exec([[
934        set diffopt+=context:2 number
935        let expr = 'printf("%08d", v:val) .. repeat("!", v:val)'
936        call setline(1, map(range(1, 20) + range(25, 45), expr))
937        vnew
938        call setline(1, map(range(3, 20) + range(28, 50), expr))
939        windo diffthis
940      ]])
941      feed('24gg')
942      screen:expect([[
943        {7:  }{8:    }{23:----------------}│{7:  }{8:  1 }{22:00000001!       }|
944        {7:  }{8:    }{23:----------------}│{7:  }{8:  2 }{22:00000002!!      }|
945        {7:  }{8:  1 }00000003!!!     │{7:  }{8:  3 }00000003!!!     |
946        {7:  }{8:  2 }00000004!!!!    │{7:  }{8:  4 }00000004!!!!    |
947        {7:+ }{8:  3 }{13:+-- 14 lines: 00}│{7:+ }{8:  5 }{13:+-- 14 lines: 00}|
948        {7:  }{8: 17 }00000019!!!!!!!!│{7:  }{8: 19 }00000019!!!!!!!!|
949        {7:  }{8: 18 }00000020!!!!!!!!│{7:  }{8: 20 }00000020!!!!!!!!|
950        {7:  }{8:    }{23:----------------}│{7:  }{8: 21 }{22:00000025!!!!!!!!}|
951        {7:  }{8:    }{23:----------------}│{7:  }{8: 22 }{22:00000026!!!!!!!!}|
952        {7:  }{8:    }{23:----------------}│{7:  }{8: 23 }{22:00000027!!!!!!!!}|
953        {7:  }{8: 19 }00000028!!!!!!!!│{7:  }{8: 24 }^00000028!!!!!!!!|
954        {7:  }{8: 20 }00000029!!!!!!!!│{7:  }{8: 25 }00000029!!!!!!!!|
955        {7:+ }{8: 21 }{13:+-- 14 lines: 00}│{7:+ }{8: 26 }{13:+-- 14 lines: 00}|
956        {7:  }{8: 35 }00000044!!!!!!!!│{7:  }{8: 40 }00000044!!!!!!!!|
957        {7:  }{8: 36 }00000045!!!!!!!!│{7:  }{8: 41 }00000045!!!!!!!!|
958        {7:  }{8: 37 }{22:00000046!!!!!!!!}│{7:  }{8:    }{23:----------------}|
959        {7:  }{8: 38 }{22:00000047!!!!!!!!}│{7:  }{8:    }{23:----------------}|
960        {7:  }{8: 39 }{22:00000048!!!!!!!!}│{7:  }{8:    }{23:----------------}|
961        {7:  }{8: 40 }{22:00000049!!!!!!!!}│{7:  }{8:    }{23:----------------}|
962        {7:  }{8: 41 }{22:00000050!!!!!!!!}│{7:  }{8:    }{23:----------------}|
963        {2:[No Name] [+]          }{3:[No Name] [+]         }|
964                                                     |
965      ]])
966      screen:try_resize(45, 3)
967      screen:expect([[
968        {7:  }{8: 19 }00000028!!!!!!!!│{7:  }{8: 24 }^00000028!!!!!!!!|
969        {2:[No Name] [+]          }{3:[No Name] [+]         }|
970                                                     |
971      ]])
972      eq({ all = 20, fill = 5, end_row = 40, end_vcol = 53 }, api.nvim_win_text_height(1000, {}))
973      eq({ all = 20, fill = 5, end_row = 40, end_vcol = 58 }, api.nvim_win_text_height(1001, {}))
974      eq(
975        { all = 20, fill = 5, end_row = 40, end_vcol = 53 },
976        api.nvim_win_text_height(1000, { start_row = 0 })
977      )
978      eq(
979        { all = 20, fill = 5, end_row = 40, end_vcol = 58 },
980        api.nvim_win_text_height(1001, { start_row = 0 })
981      )
982      eq(
983        { all = 15, fill = 0, end_row = 40, end_vcol = 53 },
984        api.nvim_win_text_height(1000, { end_row = -1 })
985      )
986      eq(
987        { all = 15, fill = 0, end_row = 40, end_vcol = 53 },
988        api.nvim_win_text_height(1000, { end_row = 40 })
989      )
990      eq(
991        { all = 20, fill = 5, end_row = 40, end_vcol = 58 },
992        api.nvim_win_text_height(1001, { end_row = -1 })
993      )
994      eq(
995        { all = 20, fill = 5, end_row = 40, end_vcol = 58 },
996        api.nvim_win_text_height(1001, { end_row = 40 })
997      )
998      eq(
999        { all = 10, fill = 5, end_row = 40, end_vcol = 53 },
1000        api.nvim_win_text_height(1000, { start_row = 23 })
1001      )
1002      eq(
1003        { all = 13, fill = 3, end_row = 40, end_vcol = 58 },
1004        api.nvim_win_text_height(1001, { start_row = 18 })
1005      )
1006      eq(
1007        { all = 11, fill = 0, end_row = 23, end_vcol = 36 },
1008        api.nvim_win_text_height(1000, { end_row = 23 })
1009      )
1010      eq(
1011        { all = 11, fill = 5, end_row = 18, end_vcol = 36 },
1012        api.nvim_win_text_height(1001, { end_row = 18 })
1013      )
1014      eq(
1015        { all = 11, fill = 0, end_row = 39, end_vcol = 52 },
1016        api.nvim_win_text_height(1000, { start_row = 3, end_row = 39 })
1017      )
1018      eq(
1019        { all = 11, fill = 3, end_row = 34, end_vcol = 52 },
1020        api.nvim_win_text_height(1001, { start_row = 1, end_row = 34 })
1021      )
1022      eq(
1023        { all = 9, fill = 0, end_row = 25, end_vcol = 0 },
1024        api.nvim_win_text_height(1000, { start_row = 4, end_row = 38 })
1025      )
1026      eq(
1027        { all = 9, fill = 3, end_row = 20, end_vcol = 0 },
1028        api.nvim_win_text_height(1001, { start_row = 2, end_row = 33 })
1029      )
1030      eq(
1031        { all = 9, fill = 0, end_row = 25, end_vcol = 0 },
1032        api.nvim_win_text_height(1000, { start_row = 5, end_row = 37 })
1033      )
1034      eq(
1035        { all = 9, fill = 3, end_row = 20, end_vcol = 0 },
1036        api.nvim_win_text_height(1001, { start_row = 3, end_row = 32 })
1037      )
1038      eq(
1039        { all = 9, fill = 0, end_row = 25, end_vcol = 0 },
1040        api.nvim_win_text_height(1000, { start_row = 17, end_row = 25 })
1041      )
1042      eq(
1043        { all = 9, fill = 3, end_row = 20, end_vcol = 0 },
1044        api.nvim_win_text_height(1001, { start_row = 15, end_row = 20 })
1045      )
1046      eq(
1047        { all = 7, fill = 0, end_row = 24, end_vcol = 37 },
1048        api.nvim_win_text_height(1000, { start_row = 18, end_row = 24 })
1049      )
1050      eq(
1051        { all = 7, fill = 3, end_row = 19, end_vcol = 37 },
1052        api.nvim_win_text_height(1001, { start_row = 16, end_row = 19 })
1053      )
1054      eq(
1055        { all = 6, fill = 5, end_row = 40, end_vcol = 53 },
1056        api.nvim_win_text_height(1000, { start_row = -1 })
1057      )
1058      eq(
1059        { all = 5, fill = 5, end_row = 40, end_vcol = 53 },
1060        api.nvim_win_text_height(1000, { start_row = -1, start_vcol = X })
1061      )
1062      eq(
1063        { all = 0, fill = 0, end_row = 40, end_vcol = 53 },
1064        api.nvim_win_text_height(1000, { start_row = -1, start_vcol = X, end_row = -1 })
1065      )
1066      eq(
1067        { all = 0, fill = 0, end_row = 40, end_vcol = 53 },
1068        api.nvim_win_text_height(
1069          1000,
1070          { start_row = -1, start_vcol = X, end_row = -1, end_vcol = X }
1071        )
1072      )
1073      eq(
1074        { all = 1, fill = 0, end_row = 40, end_vcol = 53 },
1075        api.nvim_win_text_height(
1076          1000,
1077          { start_row = -1, start_vcol = 0, end_row = -1, end_vcol = X }
1078        )
1079      )
1080      eq(
1081        { all = 3, fill = 2, end_row = 0, end_vcol = 11 },
1082        api.nvim_win_text_height(1001, { end_row = 0 })
1083      )
1084      eq(
1085        { all = 2, fill = 2, end_row = 0, end_vcol = 0 },
1086        api.nvim_win_text_height(1001, { end_row = 0, end_vcol = 0 })
1087      )
1088      eq(
1089        { all = 2, fill = 2, end_row = 0, end_vcol = 0 },
1090        api.nvim_win_text_height(1001, { start_row = 0, end_row = 0, end_vcol = 0 })
1091      )
1092      eq(
1093        { all = 0, fill = 0, end_row = 0, end_vcol = 0 },
1094        api.nvim_win_text_height(1001, { start_row = 0, start_vcol = 0, end_row = 0, end_vcol = 0 })
1095      )
1096      eq(
1097        { all = 1, fill = 0, end_row = 0, end_vcol = 11 },
1098        api.nvim_win_text_height(1001, { start_row = 0, start_vcol = 0, end_row = 0, end_vcol = X })
1099      )
1100      eq(
1101        { all = 11, fill = 5, end_row = 18, end_vcol = 36 },
1102        api.nvim_win_text_height(1001, { end_row = 18 })
1103      )
1104      eq(
1105        { all = 9, fill = 3, end_row = 18, end_vcol = 36 },
1106        api.nvim_win_text_height(1001, { start_row = 0, start_vcol = 0, end_row = 18 })
1107      )
1108      eq(
1109        { all = 10, fill = 5, end_row = 18, end_vcol = 0 },
1110        api.nvim_win_text_height(1001, { end_row = 18, end_vcol = 0 })
1111      )
1112      eq(
1113        { all = 8, fill = 3, end_row = 18, end_vcol = 0 },
1114        api.nvim_win_text_height(
1115          1001,
1116          { start_row = 0, start_vcol = 0, end_row = 18, end_vcol = 0 }
1117        )
1118      )
1119    end)
1120
1121    it('with wrapped lines', function()
1122      exec([[
1123        set number cpoptions+=n
1124        call setline(1, repeat([repeat('foobar-', 36)], 3))
1125      ]])
1126      api.nvim_buf_set_extmark(
1127        0,
1128        ns,
1129        1,
1130        100,
1131        { virt_text = { { ('?'):rep(15), 'Search' } }, virt_text_pos = 'inline' }
1132      )
1133      api.nvim_buf_set_extmark(
1134        0,
1135        ns,
1136        2,
1137        200,
1138        { virt_text = { { ('!'):rep(75), 'Search' } }, virt_text_pos = 'inline' }
1139      )
1140      screen:expect([[
1141        {8:  1 }^foobar-foobar-foobar-foobar-foobar-foobar|
1142        -foobar-foobar-foobar-foobar-foobar-foobar-fo|
1143        obar-foobar-foobar-foobar-foobar-foobar-fooba|
1144        r-foobar-foobar-foobar-foobar-foobar-foobar-f|
1145        oobar-foobar-foobar-foobar-foobar-foobar-foob|
1146        ar-foobar-foobar-foobar-foobar-              |
1147        {8:  2 }foobar-foobar-foobar-foobar-foobar-foobar|
1148        -foobar-foobar-foobar-foobar-foobar-foobar-fo|
1149        obar-foobar-fo{10:???????????????}obar-foobar-foob|
1150        ar-foobar-foobar-foobar-foobar-foobar-foobar-|
1151        foobar-foobar-foobar-foobar-foobar-foobar-foo|
1152        bar-foobar-foobar-foobar-foobar-foobar-foobar|
1153        -                                            |
1154        {8:  3 }foobar-foobar-foobar-foobar-foobar-foobar|
1155        -foobar-foobar-foobar-foobar-foobar-foobar-fo|
1156        obar-foobar-foobar-foobar-foobar-foobar-fooba|
1157        r-foobar-foobar-foobar-foobar-foobar-foobar-f|
1158        oobar-foobar-foobar-foob{10:!!!!!!!!!!!!!!!!!!!!!}|
1159        {10:!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!}|
1160        {10:!!!!!!!!!}ar-foobar-foobar-foobar-foobar-fooba|
1161        r-foobar-foobar-                             |
1162                                                     |
1163      ]])
1164      screen:try_resize(45, 2)
1165      screen:expect([[
1166        {8:  1 }^foobar-foobar-foobar-foobar-foobar-foobar|
1167                                                     |
1168      ]])
1169      eq({ all = 21, fill = 0, end_row = 2, end_vcol = 327 }, api.nvim_win_text_height(0, {}))
1170      eq(
1171        { all = 6, fill = 0, end_row = 0, end_vcol = 252 },
1172        api.nvim_win_text_height(0, { start_row = 0, end_row = 0 })
1173      )
1174      eq(
1175        { all = 7, fill = 0, end_row = 1, end_vcol = 267 },
1176        api.nvim_win_text_height(0, { start_row = 1, end_row = 1 })
1177      )
1178      eq(
1179        { all = 8, fill = 0, end_row = 2, end_vcol = 327 },
1180        api.nvim_win_text_height(0, { start_row = 2, end_row = 2 })
1181      )
1182      eq(
1183        { all = 0, fill = 0, end_row = 1, end_vcol = 0 },
1184        api.nvim_win_text_height(0, { start_row = 1, start_vcol = 0, end_row = 1, end_vcol = 0 })
1185      )
1186      eq(
1187        { all = 1, fill = 0, end_row = 1, end_vcol = 41 },
1188        api.nvim_win_text_height(0, { start_row = 1, start_vcol = 0, end_row = 1, end_vcol = 41 })
1189      )
1190      eq(
1191        { all = 2, fill = 0, end_row = 1, end_vcol = 42 },
1192        api.nvim_win_text_height(0, { start_row = 1, start_vcol = 0, end_row = 1, end_vcol = 42 })
1193      )
1194      eq(
1195        { all = 2, fill = 0, end_row = 1, end_vcol = 86 },
1196        api.nvim_win_text_height(0, { start_row = 1, start_vcol = 0, end_row = 1, end_vcol = 86 })
1197      )
1198      eq(
1199        { all = 3, fill = 0, end_row = 1, end_vcol = 87 },
1200        api.nvim_win_text_height(0, { start_row = 1, start_vcol = 0, end_row = 1, end_vcol = 87 })
1201      )
1202      eq(
1203        { all = 6, fill = 0, end_row = 1, end_vcol = 266 },
1204        api.nvim_win_text_height(0, { start_row = 1, start_vcol = 0, end_row = 1, end_vcol = 266 })
1205      )
1206      eq(
1207        { all = 7, fill = 0, end_row = 1, end_vcol = 267 },
1208        api.nvim_win_text_height(0, { start_row = 1, start_vcol = 0, end_row = 1, end_vcol = 267 })
1209      )
1210      eq(
1211        { all = 7, fill = 0, end_row = 1, end_vcol = 267 },
1212        api.nvim_win_text_height(0, { start_row = 1, start_vcol = 0, end_row = 1, end_vcol = 311 })
1213      )
1214      eq(
1215        { all = 7, fill = 0, end_row = 1, end_vcol = 267 },
1216        api.nvim_win_text_height(0, { start_row = 1, start_vcol = 0, end_row = 1, end_vcol = 312 })
1217      )
1218      eq(
1219        { all = 7, fill = 0, end_row = 1, end_vcol = 267 },
1220        api.nvim_win_text_height(0, { start_row = 1, start_vcol = 0, end_row = 1, end_vcol = X })
1221      )
1222      eq(
1223        { all = 7, fill = 0, end_row = 1, end_vcol = 267 },
1224        api.nvim_win_text_height(0, { start_row = 1, start_vcol = 40, end_row = 1, end_vcol = X })
1225      )
1226      eq(
1227        { all = 6, fill = 0, end_row = 1, end_vcol = 267 },
1228        api.nvim_win_text_height(0, { start_row = 1, start_vcol = 41, end_row = 1, end_vcol = X })
1229      )
1230      eq(
1231        { all = 6, fill = 0, end_row = 1, end_vcol = 267 },
1232        api.nvim_win_text_height(0, { start_row = 1, start_vcol = 85, end_row = 1, end_vcol = X })
1233      )
1234      eq(
1235        { all = 5, fill = 0, end_row = 1, end_vcol = 267 },
1236        api.nvim_win_text_height(0, { start_row = 1, start_vcol = 86, end_row = 1, end_vcol = X })
1237      )
1238      eq(
1239        { all = 2, fill = 0, end_row = 1, end_vcol = 267 },
1240        api.nvim_win_text_height(0, { start_row = 1, start_vcol = 265, end_row = 1, end_vcol = X })
1241      )
1242      eq(
1243        { all = 1, fill = 0, end_row = 1, end_vcol = 267 },
1244        api.nvim_win_text_height(0, { start_row = 1, start_vcol = 266, end_row = 1, end_vcol = X })
1245      )
1246      eq(
1247        { all = 1, fill = 0, end_row = 1, end_vcol = 267 },
1248        api.nvim_win_text_height(0, { start_row = 1, start_vcol = 310, end_row = 1, end_vcol = X })
1249      )
1250      eq(
1251        { all = 0, fill = 0, end_row = 1, end_vcol = 267 },
1252        api.nvim_win_text_height(0, { start_row = 1, start_vcol = 311, end_row = 1, end_vcol = X })
1253      )
1254      eq(
1255        { all = 1, fill = 0, end_row = 1, end_vcol = 131 },
1256        api.nvim_win_text_height(0, { start_row = 1, start_vcol = 86, end_row = 1, end_vcol = 131 })
1257      )
1258      eq(
1259        { all = 1, fill = 0, end_row = 1, end_vcol = 266 },
1260        api.nvim_win_text_height(
1261          0,
1262          { start_row = 1, start_vcol = 221, end_row = 1, end_vcol = 266 }
1263        )
1264      )
1265      eq(
1266        { all = 18, fill = 0, end_row = 2, end_vcol = 327 },
1267        api.nvim_win_text_height(0, { start_row = 0, start_vcol = 131 })
1268      )
1269      eq(
1270        { all = 19, fill = 0, end_row = 2, end_vcol = 327 },
1271        api.nvim_win_text_height(0, { start_row = 0, start_vcol = 130 })
1272      )
1273      eq(
1274        { all = 20, fill = 0, end_row = 2, end_vcol = 311 },
1275        api.nvim_win_text_height(0, { end_row = 2, end_vcol = 311 })
1276      )
1277      eq(
1278        { all = 21, fill = 0, end_row = 2, end_vcol = 312 },
1279        api.nvim_win_text_height(0, { end_row = 2, end_vcol = 312 })
1280      )
1281      eq(
1282        { all = 17, fill = 0, end_row = 2, end_vcol = 311 },
1283        api.nvim_win_text_height(
1284          0,
1285          { start_row = 0, start_vcol = 131, end_row = 2, end_vcol = 311 }
1286        )
1287      )
1288      eq(
1289        { all = 19, fill = 0, end_row = 2, end_vcol = 312 },
1290        api.nvim_win_text_height(
1291          0,
1292          { start_row = 0, start_vcol = 130, end_row = 2, end_vcol = 312 }
1293        )
1294      )
1295      eq(
1296        { all = 16, fill = 0, end_row = 2, end_vcol = 327 },
1297        api.nvim_win_text_height(0, { start_row = 0, start_vcol = 221 })
1298      )
1299      eq(
1300        { all = 17, fill = 0, end_row = 2, end_vcol = 327 },
1301        api.nvim_win_text_height(0, { start_row = 0, start_vcol = 220 })
1302      )
1303      eq(
1304        { all = 14, fill = 0, end_row = 2, end_vcol = 41 },
1305        api.nvim_win_text_height(0, { end_row = 2, end_vcol = 41 })
1306      )
1307      eq(
1308        { all = 15, fill = 0, end_row = 2, end_vcol = 42 },
1309        api.nvim_win_text_height(0, { end_row = 2, end_vcol = 42 })
1310      )
1311      eq(
1312        { all = 9, fill = 0, end_row = 2, end_vcol = 41 },
1313        api.nvim_win_text_height(0, { start_row = 0, start_vcol = 221, end_row = 2, end_vcol = 41 })
1314      )
1315      eq(
1316        { all = 11, fill = 0, end_row = 2, end_vcol = 42 },
1317        api.nvim_win_text_height(0, { start_row = 0, start_vcol = 220, end_row = 2, end_vcol = 42 })
1318      )
1319      exec('call setline(1, "foo")')
1320      eq(
1321        { all = 1, fill = 0, end_row = 0, end_vcol = 3 },
1322        api.nvim_win_text_height(0, { max_height = 1 })
1323      )
1324      eq(
1325        { all = 8, fill = 0, end_row = 1, end_vcol = 41 },
1326        api.nvim_win_text_height(0, { max_height = 2 })
1327      )
1328      eq(
1329        { all = 2, fill = 0, end_row = 1, end_vcol = 1 },
1330        api.nvim_win_text_height(0, { max_height = 2, end_row = 1, end_vcol = 1 })
1331      )
1332      eq(
1333        { all = 8, fill = 0, end_row = 1, end_vcol = 41 },
1334        api.nvim_win_text_height(0, { max_height = 2, end_row = 2, end_vcol = 1 })
1335      )
1336    end)
1337
1338    it('with virtual lines around a fold', function()
1339      screen:try_resize(45, 10)
1340      exec([[
1341        call setline(1, range(1, 8))
1342        3,6fold
1343      ]])
1344      api.nvim_buf_set_extmark(
1345        0,
1346        ns,
1347        1,
1348        0,
1349        { virt_lines = { { { 'VIRT LINE 1' } }, { { 'VIRT LINE 2' } } } }
1350      )
1351      api.nvim_buf_set_extmark(
1352        0,
1353        ns,
1354        6,
1355        0,
1356        { virt_lines = { { { 'VIRT LINE 3' } } }, virt_lines_above = true }
1357      )
1358      screen:expect([[
1359        ^1                                            |
1360        2                                            |
1361        VIRT LINE 1                                  |
1362        VIRT LINE 2                                  |
1363        {13:+--  4 lines: 3······························}|
1364        VIRT LINE 3                                  |
1365        7                                            |
1366        8                                            |
1367        {1:~                                            }|
1368                                                     |
1369      ]])
1370      eq({ all = 8, fill = 3, end_row = 7, end_vcol = 1 }, api.nvim_win_text_height(0, {}))
1371      eq(
1372        { all = 5, fill = 2, end_row = 2, end_vcol = 0 },
1373        api.nvim_win_text_height(0, { end_row = 2 })
1374      )
1375      eq(
1376        { all = 5, fill = 2, end_row = 2, end_vcol = 0 },
1377        api.nvim_win_text_height(0, { end_row = 2, end_vcol = X })
1378      )
1379      eq(
1380        { all = 5, fill = 2, end_row = 2, end_vcol = 0 },
1381        api.nvim_win_text_height(0, { end_row = 2, end_vcol = 90 })
1382      )
1383      eq(
1384        { all = 5, fill = 2, end_row = 2, end_vcol = 0 },
1385        api.nvim_win_text_height(0, { end_row = 2, end_vcol = 46 })
1386      )
1387      eq(
1388        { all = 5, fill = 2, end_row = 2, end_vcol = 0 },
1389        api.nvim_win_text_height(0, { end_row = 2, end_vcol = 45 })
1390      )
1391      eq(
1392        { all = 5, fill = 2, end_row = 2, end_vcol = 0 },
1393        api.nvim_win_text_height(0, { end_row = 2, end_vcol = 1 })
1394      )
1395      eq(
1396        { all = 4, fill = 2, end_row = 2, end_vcol = 0 },
1397        api.nvim_win_text_height(0, { end_row = 2, end_vcol = 0 })
1398      )
1399      eq(
1400        { all = 6, fill = 3, end_row = 7, end_vcol = 1 },
1401        api.nvim_win_text_height(0, { start_row = 2 })
1402      )
1403      eq(
1404        { all = 4, fill = 1, end_row = 7, end_vcol = 1 },
1405        api.nvim_win_text_height(0, { start_row = 2, start_vcol = 0 })
1406      )
1407      eq(
1408        { all = 4, fill = 1, end_row = 7, end_vcol = 1 },
1409        api.nvim_win_text_height(0, { start_row = 2, start_vcol = 44 })
1410      )
1411      eq(
1412        { all = 3, fill = 1, end_row = 7, end_vcol = 1 },
1413        api.nvim_win_text_height(0, { start_row = 2, start_vcol = 45 })
1414      )
1415      eq(
1416        { all = 3, fill = 1, end_row = 7, end_vcol = 1 },
1417        api.nvim_win_text_height(0, { start_row = 2, start_vcol = 89 })
1418      )
1419      eq(
1420        { all = 3, fill = 1, end_row = 7, end_vcol = 1 },
1421        api.nvim_win_text_height(0, { start_row = 2, start_vcol = 90 })
1422      )
1423      eq(
1424        { all = 3, fill = 1, end_row = 7, end_vcol = 1 },
1425        api.nvim_win_text_height(0, { start_row = 2, start_vcol = X })
1426      )
1427    end)
1428
1429    it('with virt_lines above max_height row', function()
1430      screen:try_resize(45, 10)
1431      exec('call setline(1, range(1, 7) + ["foo"->repeat(20)])')
1432      api.nvim_buf_set_extmark(0, ns, 6, 0, { virt_lines = { { { 'VIRT LINE 1' } } } })
1433      screen:expect([[
1434        ^1                                            |
1435        2                                            |
1436        3                                            |
1437        4                                            |
1438        5                                            |
1439        6                                            |
1440        7                                            |
1441        VIRT LINE 1                                  |
1442        foofoofoofoofoofoofoofoofoofoofoofoofoofoo{1:@@@}|
1443                                                     |
1444      ]])
1445      eq(
1446        { all = 10, fill = 1, end_row = 7, end_vcol = 45 },
1447        api.nvim_win_text_height(0, { max_height = api.nvim_win_get_height(0) })
1448      )
1449    end)
1450  end)
1451
1452  describe('open_win', function()
1453    it('works while cmdwin is open #40312', function()
1454      -- Legacy cmdwin blocked nvim_open_win with E11; now there is no such restriction.
1455      local new_buf = api.nvim_create_buf(true, true)
1456      feed('q:')
1457      n.poke_eventloop()
1458      local new_win = api.nvim_open_win(new_buf, false, {
1459        relative = 'editor',
1460        row = 5,
1461        col = 5,
1462        width = 5,
1463        height = 5,
1464      })
1465      eq(new_buf, api.nvim_win_get_buf(new_win))
1466    end)
1467
1468    it('aborts if buffer is invalid', function()
1469      local wins_before = api.nvim_list_wins()
1470      eq(
1471        'Invalid buffer id: 1337',
1472        pcall_err(api.nvim_open_win, 1337, false, {
1473          relative = 'editor',
1474          row = 5,
1475          col = 5,
1476          width = 5,
1477          height = 5,
1478        })
1479      )
1480      eq(wins_before, api.nvim_list_wins())
1481    end)
1482
1483    describe('creates a split window above', function()
1484      local function test_open_win_split_above(key, val)
1485        local initial_win = api.nvim_get_current_win()
1486        local win = api.nvim_open_win(0, true, {
1487          [key] = val,
1488          height = 10,
1489        })
1490        eq('', api.nvim_win_get_config(win).relative)
1491        eq(10, api.nvim_win_get_height(win))
1492        local layout = fn.winlayout()
1493        eq({
1494          'col',
1495          {
1496            { 'leaf', win },
1497            { 'leaf', initial_win },
1498          },
1499        }, layout)
1500      end
1501
1502      it("with split = 'above'", function()
1503        test_open_win_split_above('split', 'above')
1504      end)
1505
1506      it("with vertical = false and 'nosplitbelow'", function()
1507        api.nvim_set_option_value('splitbelow', false, {})
1508        test_open_win_split_above('vertical', false)
1509      end)
1510    end)
1511
1512    describe('creates a split window below', function()
1513      local function test_open_win_split_below(key, val)
1514        local initial_win = api.nvim_get_current_win()
1515        local win = api.nvim_open_win(0, true, {
1516          [key] = val,
1517          height = 15,
1518        })
1519        eq('', api.nvim_win_get_config(win).relative)
1520        eq(15, api.nvim_win_get_height(win))
1521        local layout = fn.winlayout()
1522        eq({
1523          'col',
1524          {
1525            { 'leaf', initial_win },
1526            { 'leaf', win },
1527          },
1528        }, layout)
1529      end
1530
1531      it("with split = 'below'", function()
1532        test_open_win_split_below('split', 'below')
1533      end)
1534
1535      it("with vertical = false and 'splitbelow'", function()
1536        api.nvim_set_option_value('splitbelow', true, {})
1537        test_open_win_split_below('vertical', false)
1538      end)
1539    end)
1540
1541    describe('creates a split window to the left', function()
1542      local function test_open_win_split_left(key, val)
1543        local initial_win = api.nvim_get_current_win()
1544        local win = api.nvim_open_win(0, true, {
1545          [key] = val,
1546          width = 25,
1547        })
1548        eq('', api.nvim_win_get_config(win).relative)
1549        eq(25, api.nvim_win_get_width(win))
1550        local layout = fn.winlayout()
1551        eq({
1552          'row',
1553          {
1554            { 'leaf', win },
1555            { 'leaf', initial_win },
1556          },
1557        }, layout)
1558      end
1559
1560      it("with split = 'left'", function()
1561        test_open_win_split_left('split', 'left')
1562      end)
1563
1564      it("with vertical = true and 'nosplitright'", function()
1565        api.nvim_set_option_value('splitright', false, {})
1566        test_open_win_split_left('vertical', true)
1567      end)
1568    end)
1569
1570    describe('creates a split window to the right', function()
1571      local function test_open_win_split_right(key, val)
1572        local initial_win = api.nvim_get_current_win()
1573        local win = api.nvim_open_win(0, true, {
1574          [key] = val,
1575          width = 30,
1576        })
1577        eq('', api.nvim_win_get_config(win).relative)
1578        eq(30, api.nvim_win_get_width(win))
1579        local layout = fn.winlayout()
1580        eq({
1581          'row',
1582          {
1583            { 'leaf', initial_win },
1584            { 'leaf', win },
1585          },
1586        }, layout)
1587      end
1588
1589      it("with split = 'right'", function()
1590        test_open_win_split_right('split', 'right')
1591      end)
1592
1593      it("with vertical = true and 'splitright'", function()
1594        api.nvim_set_option_value('splitright', true, {})
1595        test_open_win_split_right('vertical', true)
1596      end)
1597    end)
1598
1599    it("doesn't change tp_curwin when splitting window in another tab with enter=false", function()
1600      local tab1 = api.nvim_get_current_tabpage()
1601      local tab1_win = api.nvim_get_current_win()
1602
1603      n.command('tabnew')
1604      local tab2 = api.nvim_get_current_tabpage()
1605      local tab2_win = api.nvim_get_current_win()
1606
1607      eq({ tab1_win, tab2_win }, api.nvim_list_wins())
1608      eq({ tab1, tab2 }, api.nvim_list_tabpages())
1609
1610      api.nvim_set_current_tabpage(tab1)
1611      eq(tab1_win, api.nvim_get_current_win())
1612
1613      local tab2_prevwin = fn.tabpagewinnr(tab2, '#')
1614
1615      -- split in tab2 whine in tab2, with enter = false
1616      local tab2_win2 = api.nvim_open_win(api.nvim_create_buf(false, true), false, {
1617        win = tab2_win,
1618        split = 'right',
1619      })
1620      eq(tab1_win, api.nvim_get_current_win()) -- we should still be in the first tp
1621      eq(tab1_win, api.nvim_tabpage_get_win(tab1))
1622
1623      eq(tab2_win, api.nvim_tabpage_get_win(tab2)) -- tab2's tp_curwin should not have changed
1624      eq(tab2_prevwin, fn.tabpagewinnr(tab2, '#')) -- tab2's tp_prevwin should not have changed
1625      eq({ tab1_win, tab2_win, tab2_win2 }, api.nvim_list_wins())
1626      eq({ tab2_win, tab2_win2 }, api.nvim_tabpage_list_wins(tab2))
1627    end)
1628
1629    it('creates splits in the correct location', function()
1630      local first_win = api.nvim_get_current_win()
1631      -- specifying window 0 should create a split next to the current window
1632      local win = api.nvim_open_win(0, true, {
1633        vertical = false,
1634      })
1635      local layout = fn.winlayout()
1636      eq({
1637        'col',
1638        {
1639          { 'leaf', win },
1640          { 'leaf', first_win },
1641        },
1642      }, layout)
1643      -- not specifying a window should create a top-level split
1644      local win2 = api.nvim_open_win(0, true, {
1645        split = 'left',
1646        win = -1,
1647      })
1648      layout = fn.winlayout()
1649      eq({
1650        'row',
1651        {
1652          { 'leaf', win2 },
1653          {
1654            'col',
1655            {
1656              { 'leaf', win },
1657              { 'leaf', first_win },
1658            },
1659          },
1660        },
1661      }, layout)
1662
1663      -- specifying a window should create a split next to that window
1664      local win3 = api.nvim_open_win(0, true, {
1665        win = win,
1666        vertical = false,
1667      })
1668      layout = fn.winlayout()
1669      eq({
1670        'row',
1671        {
1672          { 'leaf', win2 },
1673          {
1674            'col',
1675            {
1676              { 'leaf', win3 },
1677              { 'leaf', win },
1678              { 'leaf', first_win },
1679            },
1680          },
1681        },
1682      }, layout)
1683    end)
1684
1685    it('opens floating windows in other tabpages', function()
1686      local first_win = api.nvim_get_current_win()
1687      local first_tab = api.nvim_get_current_tabpage()
1688
1689      command('tabnew')
1690      local new_tab = api.nvim_get_current_tabpage()
1691      local win = api.nvim_open_win(0, false, {
1692        relative = 'win',
1693        win = first_win,
1694        width = 5,
1695        height = 5,
1696        row = 1,
1697        col = 1,
1698      })
1699      eq(first_tab, api.nvim_win_get_tabpage(win))
1700      eq(new_tab, api.nvim_get_current_tabpage())
1701    end)
1702
1703    it('switches to new windows in non-current tabpages when enter=true', function()
1704      local first_win = api.nvim_get_current_win()
1705      local first_tab = api.nvim_get_current_tabpage()
1706      command('tabnew')
1707      local win = api.nvim_open_win(0, true, {
1708        relative = 'win',
1709        win = first_win,
1710        width = 5,
1711        height = 5,
1712        row = 1,
1713        col = 1,
1714      })
1715      eq(first_tab, api.nvim_win_get_tabpage(win))
1716      eq(first_tab, api.nvim_get_current_tabpage())
1717    end)
1718
1719    local function setup_tabbed_autocmd_test()
1720      local info = {}
1721      info.orig_buf = api.nvim_get_current_buf()
1722      info.other_buf = api.nvim_create_buf(true, true)
1723      info.tab1_curwin = api.nvim_get_current_win()
1724      info.tab1 = api.nvim_get_current_tabpage()
1725      command('tab split | split')
1726      info.tab2_curwin = api.nvim_get_current_win()
1727      info.tab2 = api.nvim_get_current_tabpage()
1728      exec([=[
1729        tabfirst
1730        let result = []
1731        autocmd TabEnter * let result += [["TabEnter", nvim_get_current_tabpage()]]
1732        autocmd TabLeave * let result += [["TabLeave", nvim_get_current_tabpage()]]
1733        autocmd WinEnter * let result += [["WinEnter", win_getid()]]
1734        autocmd WinLeave * let result += [["WinLeave", win_getid()]]
1735        autocmd WinNew * let result += [["WinNew", win_getid()]]
1736        autocmd WinClosed * let result += [["WinClosed", str2nr(expand("<afile>"))]]
1737        autocmd BufEnter * let result += [["BufEnter", win_getid(), bufnr()]]
1738        autocmd BufLeave * let result += [["BufLeave", win_getid(), bufnr()]]
1739        autocmd BufWinEnter * let result += [["BufWinEnter", win_getid(), bufnr()]]
1740        autocmd BufWinLeave * let result += [["BufWinLeave", win_getid(), bufnr()]]
1741      ]=])
1742      return info
1743    end
1744
1745    it('noautocmd option works', function()
1746      local info = setup_tabbed_autocmd_test()
1747
1748      api.nvim_open_win(
1749        info.other_buf,
1750        true,
1751        { split = 'left', win = info.tab2_curwin, noautocmd = true }
1752      )
1753      eq({}, eval('result'))
1754
1755      api.nvim_open_win(
1756        info.orig_buf,
1757        true,
1758        { relative = 'editor', row = 0, col = 0, width = 10, height = 10, noautocmd = true }
1759      )
1760      eq({}, eval('result'))
1761    end)
1762
1763    it('fires expected autocmds when creating splits without entering', function()
1764      local info = setup_tabbed_autocmd_test()
1765
1766      -- For these, don't want BufWinEnter if visiting the same buffer, like :{s}buffer.
1767      -- Same tabpage, same buffer.
1768      local new_win = api.nvim_open_win(0, false, { split = 'left', win = info.tab1_curwin })
1769      eq({
1770        { 'WinNew', new_win },
1771      }, eval('result'))
1772      eq(info.tab1_curwin, api.nvim_get_current_win())
1773
1774      -- Other tabpage, same buffer.
1775      command('let result = []')
1776      new_win = api.nvim_open_win(0, false, { split = 'left', win = info.tab2_curwin })
1777      eq({
1778        { 'WinNew', new_win },
1779      }, eval('result'))
1780      eq(info.tab1_curwin, api.nvim_get_current_win())
1781
1782      -- Same tabpage, other buffer.
1783      command('let result = []')
1784      new_win = api.nvim_open_win(info.other_buf, false, { split = 'left', win = info.tab1_curwin })
1785      eq({
1786        { 'WinNew', new_win },
1787        { 'BufWinEnter', new_win, info.other_buf },
1788      }, eval('result'))
1789      eq(info.tab1_curwin, api.nvim_get_current_win())
1790
1791      -- Other tabpage, other buffer.
1792      command('let result = []')
1793      new_win = api.nvim_open_win(info.other_buf, false, { split = 'left', win = info.tab2_curwin })
1794      eq({
1795        { 'WinNew', new_win },
1796        { 'BufWinEnter', new_win, info.other_buf },
1797      }, eval('result'))
1798      eq(info.tab1_curwin, api.nvim_get_current_win())
1799    end)
1800
1801    it('fires expected autocmds when creating and entering splits', function()
1802      local info = setup_tabbed_autocmd_test()
1803
1804      -- Same tabpage, same buffer.
1805      local new_win = api.nvim_open_win(0, true, { split = 'left', win = info.tab1_curwin })
1806      eq({
1807        { 'WinNew', new_win },
1808        { 'WinLeave', info.tab1_curwin },
1809        { 'WinEnter', new_win },
1810      }, eval('result'))
1811
1812      -- Same tabpage, other buffer.
1813      api.nvim_set_current_win(info.tab1_curwin)
1814      command('let result = []')
1815      new_win = api.nvim_open_win(info.other_buf, true, { split = 'left', win = info.tab1_curwin })
1816      eq({
1817        { 'WinNew', new_win },
1818        { 'WinLeave', info.tab1_curwin },
1819        { 'WinEnter', new_win },
1820        { 'BufLeave', new_win, info.orig_buf },
1821        { 'BufEnter', new_win, info.other_buf },
1822        { 'BufWinEnter', new_win, info.other_buf },
1823      }, eval('result'))
1824
1825      -- For these, the other tabpage's prevwin and curwin will change like we switched from its old
1826      -- curwin to the new window, so the extra events near TabEnter reflect that.
1827      -- Other tabpage, same buffer.
1828      api.nvim_set_current_win(info.tab1_curwin)
1829      command('let result = []')
1830      new_win = api.nvim_open_win(0, true, { split = 'left', win = info.tab2_curwin })
1831      eq({
1832        { 'WinNew', new_win },
1833        { 'WinLeave', info.tab1_curwin },
1834        { 'TabLeave', info.tab1 },
1835
1836        { 'WinEnter', info.tab2_curwin },
1837        { 'TabEnter', info.tab2 },
1838        { 'WinLeave', info.tab2_curwin },
1839        { 'WinEnter', new_win },
1840      }, eval('result'))
1841
1842      -- Other tabpage, other buffer.
1843      api.nvim_set_current_win(info.tab2_curwin)
1844      api.nvim_set_current_win(info.tab1_curwin)
1845      command('let result = []')
1846      new_win = api.nvim_open_win(info.other_buf, true, { split = 'left', win = info.tab2_curwin })
1847      eq({
1848        { 'WinNew', new_win },
1849        { 'WinLeave', info.tab1_curwin },
1850        { 'TabLeave', info.tab1 },
1851
1852        { 'WinEnter', info.tab2_curwin },
1853        { 'TabEnter', info.tab2 },
1854        { 'WinLeave', info.tab2_curwin },
1855        { 'WinEnter', new_win },
1856
1857        { 'BufLeave', new_win, info.orig_buf },
1858        { 'BufEnter', new_win, info.other_buf },
1859        { 'BufWinEnter', new_win, info.other_buf },
1860      }, eval('result'))
1861
1862      -- Other tabpage, other buffer; but other tabpage's curwin has a new buffer active.
1863      api.nvim_set_current_win(info.tab2_curwin)
1864      local new_buf = api.nvim_create_buf(true, true)
1865      api.nvim_set_current_buf(new_buf)
1866      api.nvim_set_current_win(info.tab1_curwin)
1867      command('let result = []')
1868      new_win = api.nvim_open_win(info.other_buf, true, { split = 'left', win = info.tab2_curwin })
1869      eq({
1870        { 'WinNew', new_win },
1871        { 'BufLeave', info.tab1_curwin, info.orig_buf },
1872        { 'WinLeave', info.tab1_curwin },
1873        { 'TabLeave', info.tab1 },
1874
1875        { 'WinEnter', info.tab2_curwin },
1876        { 'TabEnter', info.tab2 },
1877        { 'BufEnter', info.tab2_curwin, new_buf },
1878        { 'WinLeave', info.tab2_curwin },
1879        { 'WinEnter', new_win },
1880        { 'BufLeave', new_win, new_buf },
1881        { 'BufEnter', new_win, info.other_buf },
1882        { 'BufWinEnter', new_win, info.other_buf },
1883      }, eval('result'))
1884    end)
1885
1886    it('OK when new window is moved to other tabpage by autocommands', function()
1887      -- Use nvim_win_set_config in the autocommands, as other methods of moving a window to a
1888      -- different tabpage (e.g: wincmd T) actually creates a new window.
1889      local tab0 = api.nvim_get_current_tabpage()
1890      local tab0_win = api.nvim_get_current_win()
1891      command('tabnew')
1892      local new_buf = api.nvim_create_buf(true, true)
1893      local tab1 = api.nvim_get_current_tabpage()
1894      local tab1_parent = api.nvim_get_current_win()
1895      command(
1896        'tabfirst | autocmd WinNew * ++once call nvim_win_set_config(0, #{split: "left", win: '
1897          .. tab1_parent
1898          .. '})'
1899      )
1900      local new_win = api.nvim_open_win(new_buf, true, { split = 'left' })
1901      eq(tab1, api.nvim_get_current_tabpage())
1902      eq(new_win, api.nvim_get_current_win())
1903      eq(new_buf, api.nvim_get_current_buf())
1904
1905      -- nvim_win_set_config called after entering. It doesn't follow a curwin that is moved to a
1906      -- different tabpage, but instead moves to the win filling the space, which is tab0_win.
1907      command(
1908        'tabfirst | autocmd WinEnter * ++once call nvim_win_set_config(0, #{split: "left", win: '
1909          .. tab1_parent
1910          .. '})'
1911      )
1912      new_win = api.nvim_open_win(new_buf, true, { split = 'left' })
1913      eq(tab0, api.nvim_get_current_tabpage())
1914      eq(tab0_win, api.nvim_get_current_win())
1915      eq(tab1, api.nvim_win_get_tabpage(new_win))
1916      eq(new_buf, api.nvim_win_get_buf(new_win))
1917
1918      command(
1919        'tabfirst | autocmd BufEnter * ++once call nvim_win_set_config(0, #{split: "left", win: '
1920          .. tab1_parent
1921          .. '})'
1922      )
1923      new_win = api.nvim_open_win(new_buf, true, { split = 'left' })
1924      eq(tab0, api.nvim_get_current_tabpage())
1925      eq(tab0_win, api.nvim_get_current_win())
1926      eq(tab1, api.nvim_win_get_tabpage(new_win))
1927      eq(new_buf, api.nvim_win_get_buf(new_win))
1928    end)
1929
1930    it('does not fire BufWinEnter if win_set_buf fails', function()
1931      exec([[
1932        set nohidden modified
1933        autocmd WinNew * ++once only!
1934        let fired = v:false
1935        autocmd BufWinEnter * ++once let fired = v:true
1936      ]])
1937      eq(
1938        'Vim:E37: No write since last change (add ! to override)',
1939        pcall_err(api.nvim_open_win, api.nvim_create_buf(true, true), false, { split = 'left' })
1940      )
1941      eq(false, eval('fired'))
1942    end)
1943
1944    it('fires Buf* autocommands when `!enter` if window is entered via autocommands', function()
1945      exec([[
1946        autocmd WinNew * ++once only!
1947        let fired = v:false
1948        autocmd BufEnter * ++once let fired = v:true
1949      ]])
1950      api.nvim_open_win(api.nvim_create_buf(true, true), false, { split = 'left' })
1951      eq(true, eval('fired'))
1952    end)
1953
1954    it('no heap-use-after-free if target buffer deleted by autocommands', function()
1955      local cur_buf = api.nvim_get_current_buf()
1956      local new_buf = api.nvim_create_buf(true, true)
1957      command('autocmd WinNew * ++once call nvim_buf_delete(' .. new_buf .. ', #{force: 1})')
1958      api.nvim_open_win(new_buf, true, { split = 'left' })
1959      eq(cur_buf, api.nvim_get_current_buf())
1960    end)
1961
1962    it('checks if splitting disallowed', function()
1963      command('split | autocmd WinEnter * ++once call nvim_open_win(0, 0, #{split: "right"})')
1964      matches("E242: Can't split a window while closing another$", pcall_err(command, 'quit'))
1965      -- E242 is not needed for floats.
1966      exec([[
1967        split
1968        autocmd WinEnter * ++once let g:win = nvim_open_win(0, 0, #{relative: "editor", row: 0, col: 0, width: 5, height: 5})
1969        quit
1970      ]])
1971      eq('editor', eval('nvim_win_get_config(g:win).relative'))
1972
1973      command('only | autocmd BufHidden * ++once call nvim_open_win(0, 0, #{split: "left"})')
1974      matches(
1975        'E1159: Cannot split a window when closing the buffer$',
1976        pcall_err(command, 'new | quit')
1977      )
1978
1979      local w = api.nvim_get_current_win()
1980      command(
1981        'only | new | autocmd BufHidden * ++once call nvim_open_win(0, 0, #{split: "left", win: '
1982          .. w
1983          .. '})'
1984      )
1985      matches(
1986        'E1159: Cannot split a window when closing the buffer$',
1987        pcall_err(api.nvim_win_close, w, true)
1988      )
1989
1990      -- OK when using a buffer that isn't closing.
1991      w = api.nvim_get_current_win()
1992      command(
1993        'only | autocmd BufHidden * ++once call nvim_open_win(bufnr("#"), 0, #{split: "left", win: '
1994          .. w
1995          .. '})'
1996      )
1997      command('new | quit')
1998
1999      -- Apply to opening floats too, as that can similarly create new views into a closing buffer.
2000      -- For example, the following would open a float into an unloaded buffer:
2001      exec([[
2002        only
2003        new
2004        let g:buf = bufnr()
2005        autocmd BufUnload * ++once call nvim_open_win(g:buf, 0, #{relative: "editor", width: 5, height: 5, row: 1, col: 1})
2006        setlocal bufhidden=unload
2007      ]])
2008      matches('E1159: Cannot open a float when closing the buffer$', pcall_err(command, 'quit'))
2009      eq(false, eval('nvim_buf_is_loaded(g:buf)'))
2010      eq(0, eval('win_findbuf(g:buf)->len()'))
2011
2012      -- Only checking b_locked_split for the target buffer is insufficient, as naughty autocommands
2013      -- can cause win_set_buf to remain in a closing curbuf:
2014      exec([[
2015        only
2016        new
2017        let g:buf = bufnr()
2018        autocmd BufWipeout * ++once ++nested let g:buf2 = nvim_create_buf(1, 0)
2019              \| execute 'autocmd BufLeave * ++once call nvim_buf_delete(g:buf2, #{force: 1})'
2020              \| setlocal bufhidden=
2021              \| call nvim_open_win(g:buf2, 1, #{relative: 'editor', width: 5, height: 5, col: 5, row: 5})
2022        setlocal bufhidden=wipe
2023      ]])
2024      matches('E1159: Cannot open a float when closing the buffer$', pcall_err(command, 'quit'))
2025      eq(false, eval('nvim_buf_is_loaded(g:buf)'))
2026      eq(0, eval('win_findbuf(g:buf)->len()'))
2027      -- BufLeave shouldn't run here (buf2 isn't deleted and remains hidden)
2028      eq(true, eval('nvim_buf_is_loaded(g:buf2)'))
2029      eq(0, eval('win_findbuf(g:buf2)->len()'))
2030    end)
2031
2032    it('restores last known cursor position if BufWinEnter did not move it', function()
2033      -- This test mostly exists to ensure BufWinEnter is executed before enter_buffer's epilogue.
2034      local buf = api.nvim_get_current_buf()
2035      insert([[
2036        foo
2037        bar baz .etc
2038        i love autocommand bugs!
2039        supercalifragilisticexpialidocious
2040        marvim is actually a human
2041        llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch
2042      ]])
2043      api.nvim_win_set_cursor(0, { 5, 2 })
2044      command('set nostartofline | enew')
2045      local new_win = api.nvim_open_win(buf, false, { split = 'left' })
2046      eq({ 5, 2 }, api.nvim_win_get_cursor(new_win))
2047
2048      exec([[
2049        only!
2050        autocmd BufWinEnter * ++once normal! j6l
2051      ]])
2052      new_win = api.nvim_open_win(buf, false, { split = 'left' })
2053      eq({ 2, 6 }, api.nvim_win_get_cursor(new_win))
2054    end)
2055
2056    it('does not block all win_set_buf autocommands if !enter and !noautocmd', function()
2057      local new_buf = fn.bufadd('foobarbaz')
2058      exec([[
2059        let triggered = ""
2060        autocmd BufReadCmd * ++once let triggered = bufname()
2061      ]])
2062      api.nvim_open_win(new_buf, false, { split = 'left' })
2063      eq('foobarbaz', eval('triggered'))
2064    end)
2065
2066    it('sets error when no room', function()
2067      matches('E36: Not enough room$', pcall_err(command, 'execute "split|"->repeat(&lines)'))
2068      matches(
2069        'E36: Not enough room$',
2070        pcall_err(api.nvim_open_win, 0, true, { split = 'above', win = 0 })
2071      )
2072      matches(
2073        'E36: Not enough room$',
2074        pcall_err(api.nvim_open_win, 0, true, { split = 'below', win = 0 })
2075      )
2076    end)
2077
2078    it("can create split window when 'winborder' is set", function()
2079      local old_win = api.nvim_get_current_win()
2080      api.nvim_set_option_value('winborder', 'single', {})
2081      local new_win = api.nvim_open_win(0, false, { split = 'right', win = 0 })
2082      eq({ 'row', { { 'leaf', old_win }, { 'leaf', new_win } } }, fn.winlayout())
2083      eq('', api.nvim_win_get_config(new_win).relative)
2084    end)
2085
2086    describe("with 'autochdir'", function()
2087      local topdir
2088      local otherbuf
2089
2090      before_each(function()
2091        command('set shellslash')
2092        topdir = fn.getcwd()
2093        t.mkdir(topdir .. '/Xacd')
2094        t.mkdir(topdir .. '/Xacd/foo')
2095        otherbuf = api.nvim_create_buf(false, true)
2096        api.nvim_buf_set_name(otherbuf, topdir .. '/Xacd/baz.txt')
2097
2098        command('set autochdir')
2099        command('edit Xacd/foo/bar.txt')
2100        eq(topdir .. '/Xacd/foo', fn.getcwd())
2101      end)
2102
2103      after_each(function()
2104        n.rmdir(topdir .. '/Xacd')
2105      end)
2106
2107      it('does not change cwd with enter=false #15280', function()
2108        api.nvim_open_win(
2109          otherbuf,
2110          false,
2111          { relative = 'editor', height = 5, width = 5, row = 5, col = 5 }
2112        )
2113        eq(topdir .. '/Xacd/foo', fn.getcwd())
2114      end)
2115
2116      it('changes cwd with enter=true', function()
2117        api.nvim_open_win(
2118          otherbuf,
2119          true,
2120          { relative = 'editor', height = 5, width = 5, row = 5, col = 5 }
2121        )
2122        eq(topdir .. '/Xacd', fn.getcwd())
2123      end)
2124    end)
2125
2126    it('no memory leak with valid title and invalid footer', function()
2127      eq(
2128        "Invalid 'title/footer': expected String or Array, got Integer",
2129        pcall_err(api.nvim_open_win, 0, false, {
2130          relative = 'editor',
2131          row = 10,
2132          col = 10,
2133          height = 10,
2134          width = 10,
2135          border = 'single',
2136          title = { { 'TITLE' } },
2137          footer = 0,
2138        })
2139      )
2140    end)
2141
2142    it('no memory leak with invalid title and valid footer', function()
2143      eq(
2144        "Invalid 'title/footer': expected String or Array, got Integer",
2145        pcall_err(api.nvim_open_win, 0, false, {
2146          relative = 'editor',
2147          row = 10,
2148          col = 10,
2149          height = 10,
2150          width = 10,
2151          border = 'single',
2152          title = 0,
2153          footer = { { 'FOOTER' } },
2154        })
2155      )
2156    end)
2157
2158    it('no crash when closing the only non-float in other tabpage #31236', function()
2159      local tp = api.nvim_get_current_tabpage()
2160      local split_win = api.nvim_get_current_win()
2161      local float_win = api.nvim_open_win(
2162        0,
2163        false,
2164        { relative = 'editor', width = 5, height = 5, row = 1, col = 1 }
2165      )
2166      command('tabnew')
2167
2168      api.nvim_win_close(split_win, false)
2169      eq(false, api.nvim_win_is_valid(split_win))
2170      eq(false, api.nvim_win_is_valid(float_win))
2171      eq(false, api.nvim_tabpage_is_valid(tp))
2172
2173      tp = api.nvim_get_current_tabpage()
2174      split_win = api.nvim_get_current_win()
2175      local float_buf = api.nvim_create_buf(true, false)
2176      float_win = api.nvim_open_win(
2177        float_buf,
2178        false,
2179        { relative = 'editor', width = 5, height = 5, row = 1, col = 1 }
2180      )
2181      -- Set these options to prevent the float from being automatically closed.
2182      api.nvim_set_option_value('modified', true, { buf = float_buf })
2183      api.nvim_set_option_value('bufhidden', 'wipe', { buf = float_buf })
2184      command('tabnew')
2185
2186      matches(
2187        'E5601: Cannot close window, only floating window would remain$',
2188        pcall_err(api.nvim_win_close, split_win, false)
2189      )
2190      eq(true, api.nvim_win_is_valid(split_win))
2191      eq(true, api.nvim_win_is_valid(float_win))
2192      eq(true, api.nvim_tabpage_is_valid(tp))
2193
2194      api.nvim_set_current_win(float_win)
2195      api.nvim_win_close(split_win, true) -- Force it this time.
2196      eq(false, api.nvim_win_is_valid(split_win))
2197      eq(false, api.nvim_win_is_valid(float_win))
2198      eq(false, api.nvim_tabpage_is_valid(tp))
2199
2200      -- Ensure opening a float after the initial check (like in WinClosed) doesn't crash...
2201      exec([[
2202        tabnew
2203        let g:tp = nvim_get_current_tabpage()
2204        let g:win = win_getid()
2205        tabprevious
2206        autocmd! WinClosed * ++once call nvim_open_win(0, 0, #{win: g:win, relative: 'win', width: 5, height: 5, row: 5, col: 5})
2207      ]])
2208      matches(
2209        'E5601: Cannot close window, only floating window would remain$',
2210        pcall_err(command, 'call nvim_win_close(g:win, 0)')
2211      )
2212      eq(true, eval 'nvim_tabpage_is_valid(g:tp)')
2213
2214      exec([[
2215        tabnew
2216        let g:tp = nvim_get_current_tabpage()
2217        let g:win = win_getid()
2218        let g:buf = bufnr()
2219        tabprevious
2220        let s:buf2 = nvim_create_buf(0, 0)
2221        call setbufvar(s:buf2, '&modified', 1)
2222        call setbufvar(s:buf2, '&bufhidden', 'wipe')
2223        autocmd! WinClosed * ++once call nvim_open_win(s:buf2, 0, #{win: g:win, relative: 'win', width: 5, height: 5, row: 5, col: 5})
2224      ]])
2225      matches(
2226        'E5601: Cannot close window, only floating window would remain$',
2227        pcall_err(command, 'call nvim_buf_delete(g:buf, #{force: 1})')
2228      )
2229      eq(true, eval 'nvim_tabpage_is_valid(g:tp)')
2230    end)
2231
2232    it('respects requested size for large splits', function()
2233      command('vsplit')
2234      local win = api.nvim_open_win(0, false, { win = -1, split = 'right', width = 38 })
2235      eq(38, api.nvim_win_get_width(win))
2236
2237      -- No zero-sized windows (e.g: from skipping forced equalization in win_split_ins) if
2238      -- requesting a chonky window; that could lead to crashes!
2239      api.nvim_open_win(0, false, { win = -1, split = 'right', width = 9999 })
2240      eq({ 1, 1, 1, 74 }, eval("range(1, winnr('$'))->map({_, nr -> winwidth(nr)})"))
2241
2242      command('split')
2243      win = api.nvim_open_win(0, false, { win = 0, split = 'below', height = 10 })
2244      eq(10, api.nvim_win_get_height(win))
2245
2246      -- Still defaults to half-sized when no size was specified.
2247      command('only')
2248      eq(80, api.nvim_win_get_width(0))
2249      api.nvim_open_win(0, true, { split = 'right' })
2250      eq(40, api.nvim_win_get_width(0))
2251
2252      eq(22, api.nvim_win_get_height(0))
2253      api.nvim_open_win(0, true, { split = 'below' })
2254      eq(11, api.nvim_win_get_height(0))
2255    end)
2256
2257    it('no leak when win_set_buf fails and window is closed immediately', function()
2258      -- Following used to leak.
2259      command('autocmd BufEnter * ++once quit! | throw 1337')
2260      eq(
2261        'Window was closed immediately',
2262        pcall_err(
2263          api.nvim_open_win,
2264          api.nvim_create_buf(true, true),
2265          true,
2266          { relative = 'editor', width = 5, height = 5, row = 1, col = 1 }
2267        )
2268      )
2269      -- If the window wasn't closed, still set errors from win_set_buf.
2270      command('autocmd BufEnter * ++once throw 1337')
2271      eq(
2272        'BufEnter Autocommands for "*": 1337',
2273        pcall_err(
2274          api.nvim_open_win,
2275          api.nvim_create_buf(true, true),
2276          true,
2277          { relative = 'editor', width = 5, height = 5, row = 1, col = 1 }
2278        )
2279      )
2280    end)
2281
2282    it('redraws after setting minimal style', function()
2283      local screen = Screen.new(10, 10)
2284      -- Autocommand processes pending redraws earlier than when minimal style is set, so it doesn't
2285      -- implicitly rely on those.
2286      exec([[
2287        set cursorline cursorcolumn number
2288        autocmd WinNew * ++once redraw | let g:triggered = 1
2289      ]])
2290      screen:expect([[
2291        {15:  1 }{21:^        }|
2292        {1:~           }|*8
2293                    |
2294      ]])
2295      api.nvim_open_win(0, false, { style = 'minimal', split = 'below' })
2296      eq(1, eval('g:triggered'))
2297      screen:expect([[
2298        {15:  1 }{21:^        }|
2299        {1:~           }|*2
2300        {2:[No Name]   }|
2301                    |*4
2302        {2:[No Name]   }|
2303                    |
2304      ]])
2305      -- Also check nvim_win_set_config: only set style to avoid redraws from other config fields.
2306      api.nvim_win_set_config(0, { style = 'minimal' })
2307      screen:expect([[
2308        ^            |
2309                    |*2
2310        {3:[No Name]   }|
2311                    |*4
2312        {2:[No Name]   }|
2313                    |
2314      ]])
2315    end)
2316
2317    it('keep focus when creating split window with enter=false in init script', function()
2318      local script_file = 'Xstartup.lua'
2319      t.write_file(
2320        script_file,
2321        [[
2322        vim.o.laststatus = 0
2323        local enter = vim.g.test_enter
2324        local win = vim.api.nvim_open_win(vim.api.nvim_create_buf(false, true), enter, {
2325          split = 'left',
2326          win = 0,
2327        })
2328      ]]
2329      )
2330      finally(function()
2331        os.remove(script_file)
2332      end)
2333      local screen = tt.setup_child_nvim({
2334        '--clean',
2335        '--cmd',
2336        'let g:test_enter = v:false',
2337        '-u',
2338        script_file,
2339      })
2340      screen:expect([[
2341                                 │^                        |
2342        ~                        │~                       |*4
2343                                        0,0-1          All|
2344        {5:-- TERMINAL --}                                    |
2345      ]])
2346      screen:detach()
2347      screen = tt.setup_child_nvim({
2348        '--clean',
2349        '--cmd',
2350        'let g:test_enter = v:true',
2351        '-u',
2352        script_file,
2353      })
2354      screen:expect([[
2355        ^                         │                        |
2356        ~                        │~                       |*4
2357                                        0,0-1          All|
2358        {5:-- TERMINAL --}                                    |
2359      ]])
2360    end)
2361  end)
2362
2363  describe('set_config', function()
2364    it("uses 'winborder' when converting a split to a floating window", function()
2365      api.nvim_set_option_value('winborder', 'single', {})
2366      command('split')
2367      local winid = api.nvim_get_current_win()
2368      -- Convert split to float without specifying border
2369      api.nvim_win_set_config(winid, {
2370        relative = 'editor',
2371        row = 2,
2372        col = 2,
2373        width = 10,
2374        height = 5,
2375      })
2376      local config = api.nvim_win_get_config(winid)
2377      eq('┌', config.border[1])
2378    end)
2379
2380    it('erases border of a floating window when converting to split window', function()
2381      api.nvim_set_option_value('winborder', 'single', {})
2382      local winid = api.nvim_open_win(api.nvim_create_buf(false, false), false, {
2383        relative = 'editor',
2384        row = 2,
2385        col = 2,
2386        width = 10,
2387        height = 5,
2388      })
2389      local config = api.nvim_win_get_config(winid)
2390      eq('┌', config.border[1])
2391      api.nvim_win_set_config(winid, { split = 'right', win = 0 })
2392      config = api.nvim_win_get_config(winid)
2393      eq(nil, config.border)
2394    end)
2395
2396    it('moves a split into a float', function()
2397      local win = api.nvim_open_win(0, true, {
2398        vertical = false,
2399      })
2400      eq('', api.nvim_win_get_config(win).relative)
2401      api.nvim_win_set_config(win, {
2402        relative = 'editor',
2403        row = 5,
2404        col = 5,
2405        width = 5,
2406        height = 5,
2407      })
2408      eq('editor', api.nvim_win_get_config(win).relative)
2409    end)
2410
2411    it('throws error when attempting to move the last non-floating window', function()
2412      local err = pcall_err(api.nvim_win_set_config, 0, {
2413        vertical = false,
2414      })
2415      eq('Cannot move last non-floating window', err)
2416
2417      local win1 = api.nvim_get_current_win()
2418      command('tabnew')
2419      eq(
2420        'Cannot move last non-floating window',
2421        pcall_err(api.nvim_win_set_config, 0, { win = win1, split = 'left' })
2422      )
2423      api.nvim_open_win(0, false, { relative = 'editor', width = 5, height = 5, row = 1, col = 1 })
2424      eq(
2425        'Cannot move last non-floating window',
2426        pcall_err(api.nvim_win_set_config, 0, { win = win1, split = 'left' })
2427      )
2428
2429      -- If it's no longer the last non-float, still an error if autocommands make it the last
2430      -- non-float again before it's moved.
2431      command('vsplit')
2432      exec_lua(function()
2433        vim.api.nvim_create_autocmd('WinEnter', {
2434          once = true,
2435          callback = function()
2436            vim.api.nvim_win_set_config(
2437              0,
2438              { relative = 'editor', width = 5, height = 5, row = 1, col = 1 }
2439            )
2440          end,
2441        })
2442      end)
2443      eq(
2444        'Cannot move last non-floating window',
2445        pcall_err(api.nvim_win_set_config, 0, { win = win1, split = 'left' })
2446      )
2447    end)
2448
2449    it('passing retval of get_config results in no-op', function()
2450      -- simple split layout
2451      local win = api.nvim_open_win(0, true, {
2452        split = 'left',
2453      })
2454      local layout = fn.winlayout()
2455      local config = api.nvim_win_get_config(win)
2456      api.nvim_win_set_config(win, config)
2457      eq(layout, fn.winlayout())
2458
2459      -- nested split layout
2460      local win2 = api.nvim_open_win(0, true, {
2461        vertical = true,
2462      })
2463      local win3 = api.nvim_open_win(0, true, {
2464        win = win2,
2465        vertical = false,
2466      })
2467      layout = fn.winlayout()
2468      config = api.nvim_win_get_config(win2)
2469      api.nvim_win_set_config(win2, config)
2470      eq(layout, fn.winlayout())
2471
2472      config = api.nvim_win_get_config(win3)
2473      api.nvim_win_set_config(win3, config)
2474      eq(layout, fn.winlayout())
2475    end)
2476
2477    it('moves a float into a split', function()
2478      local layout = fn.winlayout()
2479      eq('leaf', layout[1])
2480      local win = api.nvim_open_win(0, true, {
2481        relative = 'editor',
2482        row = 5,
2483        col = 5,
2484        width = 5,
2485        height = 5,
2486      })
2487      api.nvim_win_set_config(win, {
2488        split = 'below',
2489        win = -1,
2490      })
2491      eq('', api.nvim_win_get_config(win).relative)
2492      layout = fn.winlayout()
2493      eq('col', layout[1])
2494      eq(2, #layout[2])
2495      eq(win, layout[2][2][2])
2496    end)
2497
2498    it('respects the "split" option', function()
2499      local layout = fn.winlayout()
2500      eq('leaf', layout[1])
2501      local first_win = layout[2]
2502      local win = api.nvim_open_win(0, true, {
2503        relative = 'editor',
2504        row = 5,
2505        col = 5,
2506        width = 5,
2507        height = 5,
2508      })
2509      api.nvim_win_set_config(win, {
2510        split = 'right',
2511        win = first_win,
2512      })
2513      layout = fn.winlayout()
2514      eq('row', layout[1])
2515      eq(2, #layout[2])
2516      eq(win, layout[2][2][2])
2517      local config = api.nvim_win_get_config(win)
2518      eq('', config.relative)
2519      eq('right', config.split)
2520      api.nvim_win_set_config(win, {
2521        split = 'below',
2522        win = first_win,
2523      })
2524      layout = fn.winlayout()
2525      eq('col', layout[1])
2526      eq(2, #layout[2])
2527      eq(win, layout[2][2][2])
2528      config = api.nvim_win_get_config(win)
2529      eq('', config.relative)
2530      eq('below', config.split)
2531
2532      eq(
2533        "Required: non-float with 'win' requires 'split' or 'vertical'",
2534        pcall_err(api.nvim_win_set_config, 0, { win = 0 })
2535      )
2536      eq(
2537        "Required: non-float with 'win' requires 'split' or 'vertical'",
2538        pcall_err(api.nvim_win_set_config, 0, { win = 0, relative = '' })
2539      )
2540
2541      -- "minimal" style takes effect immediately for a split.
2542      api.nvim_set_option_value('cursorline', true, { win = win, scope = 'local' })
2543      eq(true, api.nvim_get_option_value('cursorline', { win = win }))
2544      api.nvim_win_set_config(win, { style = 'minimal' })
2545      eq(false, api.nvim_get_option_value('cursorline', { win = win }))
2546    end)
2547
2548    it('creates top-level splits', function()
2549      local win = api.nvim_open_win(0, true, {
2550        vertical = false,
2551      })
2552      local win2 = api.nvim_open_win(0, true, {
2553        vertical = true,
2554        win = -1,
2555      })
2556      local layout = fn.winlayout()
2557      eq('row', layout[1])
2558      eq(2, #layout[2])
2559      eq(win2, layout[2][1][2])
2560      api.nvim_win_set_config(win, {
2561        split = 'below',
2562        win = -1,
2563      })
2564      layout = fn.winlayout()
2565      eq('col', layout[1])
2566      eq(2, #layout[2])
2567      eq('row', layout[2][1][1])
2568      eq(win, layout[2][2][2])
2569    end)
2570
2571    it('moves windows to other tabpages', function()
2572      local first_tab = api.nvim_get_current_tabpage()
2573      local first_win = api.nvim_get_current_win()
2574      local win = api.nvim_open_win(0, false, { split = 'left' })
2575      command('tabnew')
2576      local new_tab = api.nvim_get_current_tabpage()
2577      local tab2_win = api.nvim_get_current_win()
2578      api.nvim_set_current_tabpage(first_tab)
2579      -- move new win to new tabpage
2580      api.nvim_win_set_config(win, { split = 'right', win = api.nvim_tabpage_get_win(new_tab) })
2581      eq(new_tab, api.nvim_win_get_tabpage(win))
2582      -- we are changing the config, the current tabpage should not change
2583      eq(first_tab, api.nvim_get_current_tabpage())
2584
2585      api.nvim_set_current_tabpage(new_tab)
2586      local layout = fn.winlayout()
2587      eq({
2588        'row',
2589        {
2590          { 'leaf', api.nvim_tabpage_get_win(new_tab) },
2591          { 'leaf', win },
2592        },
2593      }, layout)
2594
2595      -- directly convert split into a float for a different tabpage
2596      local win2 = api.nvim_open_win(0, true, { split = 'below' })
2597      eq('', api.nvim_win_get_config(win2).relative)
2598      api.nvim_win_set_config(
2599        win2,
2600        { relative = 'editor', row = 0, col = 0, width = 1, height = 1, win = first_win }
2601      )
2602      eq(first_tab, api.nvim_win_get_tabpage(win2))
2603      eq('editor', api.nvim_win_get_config(win2).relative)
2604      eq({ first_win, win2 }, api.nvim_tabpage_list_wins(first_tab))
2605      eq({ tab2_win, win }, api.nvim_tabpage_list_wins(new_tab))
2606
2607      -- convert new win to float in new tabpage
2608      api.nvim_win_set_config(win, { relative = 'editor', row = 2, col = 2, height = 2, width = 2 })
2609      api.nvim_set_current_tabpage(first_tab)
2610      -- move to other tabpage
2611      api.nvim_win_set_config(win, { win = first_win })
2612      eq(first_tab, api.nvim_win_get_tabpage(win))
2613      eq({ first_win, win, win2 }, api.nvim_tabpage_list_wins(first_tab))
2614      eq({ tab2_win }, api.nvim_tabpage_list_wins(new_tab))
2615      -- unlike splits, negative win is invalid
2616      eq('Invalid window id: -1', pcall_err(api.nvim_win_set_config, win, { win = -1 }))
2617
2618      -- can't convert only window in other tabpage to float
2619      command('tabnew')
2620      local only_win = api.nvim_get_current_win()
2621      command('tabprevious')
2622      eq(
2623        'Cannot change last window into float',
2624        pcall_err(
2625          api.nvim_win_set_config,
2626          only_win,
2627          { relative = 'editor', width = 5, height = 5, row = 0, col = 0 }
2628        )
2629      )
2630    end)
2631
2632    it('correctly moves curwin when moving curwin to a different tabpage', function()
2633      local tab1 = api.nvim_get_current_tabpage()
2634      local tab1_win = api.nvim_get_current_win()
2635      command('tabnew')
2636      local tab2 = api.nvim_get_current_tabpage()
2637      local tab2_win = api.nvim_get_current_win()
2638      api.nvim_set_current_tabpage(tab1) -- return to the initial tab
2639      -- create and enter a new split
2640      local win = api.nvim_open_win(0, true, {
2641        vertical = false,
2642      })
2643
2644      eq(tab1, api.nvim_win_get_tabpage(win))
2645      eq({ win, tab1_win }, api.nvim_tabpage_list_wins(tab1))
2646
2647      -- move the current win to a different tabpage
2648      api.nvim_win_set_config(win, {
2649        split = 'right',
2650        win = api.nvim_tabpage_get_win(tab2),
2651      })
2652      eq(tab1, api.nvim_get_current_tabpage())
2653
2654      -- win should have moved to tab2
2655      eq(tab2, api.nvim_win_get_tabpage(win))
2656      -- tp_curwin of tab2 should not have changed
2657      eq(tab2_win, api.nvim_tabpage_get_win(tab2))
2658      -- win lists should be correct
2659      eq({ tab2_win, win }, api.nvim_tabpage_list_wins(tab2))
2660      eq({ tab1_win }, api.nvim_tabpage_list_wins(tab1))
2661      -- current win should have moved to neighboring win
2662      eq(tab1_win, api.nvim_tabpage_get_win(tab1))
2663
2664      api.nvim_set_current_tabpage(tab2)
2665      -- convert new win to float
2666      api.nvim_win_set_config(win, { relative = 'editor', row = 2, col = 2, height = 2, width = 2 })
2667      api.nvim_set_current_win(win)
2668      api.nvim_win_set_config(win, { relative = 'win', win = tab1_win, row = 3, col = 3 })
2669      eq(tab1, api.nvim_win_get_tabpage(win))
2670      eq(tab2, api.nvim_get_current_tabpage())
2671      eq({ tab1_win, win }, api.nvim_tabpage_list_wins(tab1))
2672    end)
2673
2674    it('splits windows in non-current tabpage', function()
2675      local curtab = api.nvim_get_current_tabpage()
2676      command('tabnew')
2677      local tabnr = api.nvim_get_current_tabpage()
2678      command('tabprev') -- return to the initial tab
2679
2680      local win = api.nvim_open_win(0, false, {
2681        vertical = false,
2682        win = api.nvim_tabpage_get_win(tabnr),
2683      })
2684
2685      eq(tabnr, api.nvim_win_get_tabpage(win))
2686      -- since enter = false, the current tabpage should not change
2687      eq(curtab, api.nvim_get_current_tabpage())
2688    end)
2689
2690    it('moves the current split window', function()
2691      local initial_win = api.nvim_get_current_win()
2692      local win = api.nvim_open_win(0, true, {
2693        vertical = true,
2694      })
2695      local win2 = api.nvim_open_win(0, true, {
2696        vertical = true,
2697      })
2698      api.nvim_set_current_win(win)
2699      eq({
2700        'row',
2701        {
2702          { 'leaf', win2 },
2703          { 'leaf', win },
2704          { 'leaf', initial_win },
2705        },
2706      }, fn.winlayout())
2707
2708      api.nvim_win_set_config(0, {
2709        vertical = false,
2710        win = 0,
2711      })
2712      eq(win, api.nvim_get_current_win())
2713      eq({
2714        'col',
2715        {
2716          { 'leaf', win },
2717          {
2718            'row',
2719            {
2720              { 'leaf', win2 },
2721              { 'leaf', initial_win },
2722            },
2723          },
2724        },
2725      }, fn.winlayout())
2726
2727      api.nvim_set_current_win(win2)
2728      local win3 = api.nvim_open_win(0, true, {
2729        vertical = true,
2730      })
2731      eq(win3, api.nvim_get_current_win())
2732
2733      eq({
2734        'col',
2735        {
2736          { 'leaf', win },
2737          {
2738            'row',
2739            {
2740              { 'leaf', win3 },
2741              { 'leaf', win2 },
2742              { 'leaf', initial_win },
2743            },
2744          },
2745        },
2746      }, fn.winlayout())
2747
2748      api.nvim_win_set_config(0, {
2749        vertical = false,
2750        win = 0,
2751      })
2752
2753      eq(win3, api.nvim_get_current_win())
2754      eq({
2755        'col',
2756        {
2757          { 'leaf', win },
2758          {
2759            'row',
2760            {
2761              {
2762                'col',
2763                {
2764                  { 'leaf', win3 },
2765                  { 'leaf', win2 },
2766                },
2767              },
2768              { 'leaf', initial_win },
2769            },
2770          },
2771        },
2772      }, fn.winlayout())
2773    end)
2774
2775    it('closing new curwin when moving window to other tabpage works', function()
2776      command('split | tabnew')
2777      local t2_win = api.nvim_get_current_win()
2778      command('tabfirst | autocmd WinEnter * ++once quit')
2779      local t1_move_win = api.nvim_get_current_win()
2780      -- win_set_config fails to switch away from "t1_move_win" because the WinEnter autocmd that
2781      -- closed the window we're switched to returns us to "t1_move_win", as it filled the space.
2782      eq(
2783        'Failed to switch away from window ' .. t1_move_win,
2784        pcall_err(api.nvim_win_set_config, t1_move_win, { win = t2_win, split = 'left' })
2785      )
2786      eq(t1_move_win, api.nvim_get_current_win())
2787
2788      command('split | split | autocmd WinEnter * ++once quit')
2789      t1_move_win = api.nvim_get_current_win()
2790      -- In this case, we closed the window that we got switched to, but doing so didn't switch us
2791      -- back to "t1_move_win", which is fine.
2792      api.nvim_win_set_config(t1_move_win, { win = t2_win, split = 'left' })
2793      neq(t1_move_win, api.nvim_get_current_win())
2794    end)
2795
2796    it('messing with "win" or "parent" when moving "win" to other tabpage', function()
2797      command('split | tabnew')
2798      local t2_win1 = api.nvim_get_current_win()
2799      command('split')
2800      local t2_win2 = api.nvim_get_current_win()
2801      command('split')
2802      local t2_win3 = api.nvim_get_current_win()
2803
2804      command('tabfirst | autocmd WinEnter * ++once call nvim_win_close(' .. t2_win1 .. ', 1)')
2805      local cur_win = api.nvim_get_current_win()
2806      eq(
2807        'Windows to split were closed',
2808        pcall_err(api.nvim_win_set_config, 0, { win = t2_win1, split = 'left' })
2809      )
2810      eq(cur_win, api.nvim_get_current_win())
2811
2812      command('split | autocmd WinLeave * ++once quit!')
2813      cur_win = api.nvim_get_current_win()
2814      eq(
2815        'Windows to split were closed',
2816        pcall_err(api.nvim_win_set_config, 0, { win = t2_win2, split = 'left' })
2817      )
2818      neq(cur_win, api.nvim_get_current_win())
2819
2820      exec([[
2821        split
2822        autocmd WinLeave * ++once
2823              \ call nvim_win_set_config(0, #{relative:'editor', row:0, col:0, width:5, height:5})
2824      ]])
2825      cur_win = api.nvim_get_current_win()
2826      eq(
2827        'Floating state of windows to split changed',
2828        pcall_err(api.nvim_win_set_config, 0, { win = t2_win3, split = 'left' })
2829      )
2830      eq('editor', api.nvim_win_get_config(0).relative)
2831      eq(cur_win, api.nvim_get_current_win())
2832
2833      command('autocmd WinLeave * ++once wincmd J')
2834      cur_win = api.nvim_get_current_win()
2835      eq(
2836        'Floating state of windows to split changed',
2837        pcall_err(api.nvim_win_set_config, 0, { win = t2_win3, split = 'left' })
2838      )
2839      eq('', api.nvim_win_get_config(0).relative)
2840      eq(cur_win, api.nvim_get_current_win())
2841
2842      -- Try to make "parent" floating. This should give the same error as before.
2843      exec(([[
2844        autocmd WinLeave * ++once
2845              \ call nvim_win_set_config(%d, #{relative:'editor', row:0, col:0, width:5, height:5})
2846      ]]):format(t2_win3))
2847      cur_win = api.nvim_get_current_win()
2848      eq(
2849        'Floating state of windows to split changed',
2850        pcall_err(api.nvim_win_set_config, 0, { win = t2_win3, split = 'left' })
2851      )
2852      eq('editor', api.nvim_win_get_config(t2_win3).relative)
2853      eq('', api.nvim_win_get_config(0).relative)
2854      eq(cur_win, api.nvim_get_current_win())
2855    end)
2856
2857    it('expected autocmds when moving window to other tabpage', function()
2858      local new_curwin = api.nvim_get_current_win()
2859      command('split')
2860      local win = api.nvim_get_current_win()
2861      command('tabnew')
2862      local parent = api.nvim_get_current_win()
2863      exec([[
2864        tabfirst
2865        let result = []
2866        autocmd WinEnter * let result += ["Enter", win_getid()]
2867        autocmd WinLeave * let result += ["Leave", win_getid()]
2868        autocmd WinNew * let result += ["New", win_getid()]
2869      ]])
2870      api.nvim_win_set_config(0, { win = parent, split = 'left' })
2871      -- Shouldn't see WinNew, as we're not creating any new windows, just moving existing ones.
2872      eq({ 'Leave', win, 'Enter', new_curwin }, eval('result'))
2873    end)
2874
2875    it('no autocmds when moving window in same or other tabpage', function()
2876      local parent = api.nvim_get_current_win()
2877      exec([[
2878        split
2879        let g:result = []
2880        autocmd WinEnter * let g:result += ["Enter", win_getid()]
2881        autocmd WinLeave * let g:result += ["Leave", win_getid()]
2882        autocmd WinNew * let g:result += ["New", win_getid()]
2883      ]])
2884      api.nvim_win_set_config(0, { win = parent, split = 'left' })
2885      -- Shouldn't see any of those events, as we remain in the same window.
2886      eq({}, eval('g:result'))
2887
2888      -- move float window from tab2 to tab1
2889      command('tabdo only')
2890      local tab1 = api.nvim_get_current_tabpage()
2891      local tab1_win1 = api.nvim_get_current_win()
2892      command('tabnew')
2893      local fwin = api.nvim_open_win(0, false, {
2894        relative = 'editor',
2895        row = 2,
2896        col = 2,
2897        height = 2,
2898        width = 2,
2899      })
2900      api.nvim_set_current_tabpage(tab1)
2901      api.nvim_set_var('result', {})
2902      api.nvim_win_set_config(fwin, { win = tab1_win1 })
2903      eq({}, eval('g:result'))
2904    end)
2905
2906    it('checks if splitting disallowed', function()
2907      command('split | autocmd WinEnter * ++once call nvim_win_set_config(0, #{split: "right"})')
2908      matches("E242: Can't split a window while closing another$", pcall_err(command, 'quit'))
2909
2910      command('autocmd BufHidden * ++once call nvim_win_set_config(0, #{split: "left"})')
2911      matches(
2912        'E1159: Cannot split a window when closing the buffer$',
2913        pcall_err(command, 'new | quit')
2914      )
2915
2916      -- OK when using window to different buffer.
2917      local w = api.nvim_get_current_win()
2918      command('autocmd BufHidden * ++once call nvim_win_set_config(' .. w .. ', #{split: "left"})')
2919      command('new | quit')
2920    end)
2921
2922    --- Returns a function to get information about the window layout, sizes and positions of a
2923    --- tabpage.
2924    local function define_tp_info_function()
2925      exec_lua([[
2926        function tp_info(tp)
2927          return {
2928            layout = vim.fn.winlayout(vim.api.nvim_tabpage_get_number(tp)),
2929            pos_sizes = vim.tbl_map(
2930              function(w)
2931                local pos = vim.fn.win_screenpos(w)
2932                return {
2933                  row = pos[1],
2934                  col = pos[2],
2935                  width = vim.fn.winwidth(w),
2936                  height = vim.fn.winheight(w)
2937                }
2938              end,
2939              vim.api.nvim_tabpage_list_wins(tp)
2940            )
2941          }
2942        end
2943      ]])
2944
2945      return function(tp)
2946        return exec_lua('return tp_info(...)', tp)
2947      end
2948    end
2949
2950    it('attempt to move window with no room', function()
2951      -- Fill the 2nd tabpage full of windows until we run out of room.
2952      -- Use &laststatus=0 to ensure restoring missing statuslines doesn't affect things.
2953      command('set laststatus=0 | tabnew')
2954      matches('E36: Not enough room$', pcall_err(command, 'execute "split|"->repeat(&lines)'))
2955      command('vsplit | wincmd | | wincmd p')
2956      local t2 = api.nvim_get_current_tabpage()
2957      local t2_cur_win = api.nvim_get_current_win()
2958      local t2_top_split = fn.win_getid(1)
2959      local t2_bot_split = fn.win_getid(fn.winnr('$'))
2960      local t2_float = api.nvim_open_win(
2961        0,
2962        false,
2963        { relative = 'editor', row = 0, col = 0, width = 10, height = 10 }
2964      )
2965      local t2_float_config = api.nvim_win_get_config(t2_float)
2966      local tp_info = define_tp_info_function()
2967      local t2_info = tp_info(t2)
2968      matches(
2969        'E36: Not enough room$',
2970        pcall_err(api.nvim_win_set_config, 0, { win = t2_top_split, split = 'above' })
2971      )
2972      matches(
2973        'E36: Not enough room$',
2974        pcall_err(api.nvim_win_set_config, 0, { win = t2_top_split, split = 'below' })
2975      )
2976      matches(
2977        'E36: Not enough room$',
2978        pcall_err(api.nvim_win_set_config, 0, { win = t2_bot_split, split = 'above' })
2979      )
2980      matches(
2981        'E36: Not enough room$',
2982        pcall_err(api.nvim_win_set_config, 0, { win = t2_bot_split, split = 'below' })
2983      )
2984      matches(
2985        'E36: Not enough room$',
2986        pcall_err(api.nvim_win_set_config, t2_float, { win = t2_top_split, split = 'above' })
2987      )
2988      matches(
2989        'E36: Not enough room$',
2990        pcall_err(api.nvim_win_set_config, t2_float, { win = t2_top_split, split = 'below' })
2991      )
2992      matches(
2993        'E36: Not enough room$',
2994        pcall_err(api.nvim_win_set_config, t2_float, { win = t2_bot_split, split = 'above' })
2995      )
2996      matches(
2997        'E36: Not enough room$',
2998        pcall_err(api.nvim_win_set_config, t2_float, { win = t2_bot_split, split = 'below' })
2999      )
3000      eq(t2_cur_win, api.nvim_get_current_win())
3001      eq(t2_info, tp_info(t2))
3002      eq(t2_float_config, api.nvim_win_get_config(t2_float))
3003
3004      -- Try to move windows from the 1st tabpage to the 2nd.
3005      command('tabfirst | split | wincmd _')
3006      local t1 = api.nvim_get_current_tabpage()
3007      local t1_cur_win = api.nvim_get_current_win()
3008      local t1_float = api.nvim_open_win(
3009        0,
3010        false,
3011        { relative = 'editor', row = 5, col = 3, width = 7, height = 6 }
3012      )
3013      local t1_float_config = api.nvim_win_get_config(t1_float)
3014      local t1_info = tp_info(t1)
3015      matches(
3016        'E36: Not enough room$',
3017        pcall_err(api.nvim_win_set_config, 0, { win = t2_top_split, split = 'above' })
3018      )
3019      matches(
3020        'E36: Not enough room$',
3021        pcall_err(api.nvim_win_set_config, 0, { win = t2_top_split, split = 'below' })
3022      )
3023      matches(
3024        'E36: Not enough room$',
3025        pcall_err(api.nvim_win_set_config, 0, { win = t2_bot_split, split = 'above' })
3026      )
3027      matches(
3028        'E36: Not enough room$',
3029        pcall_err(api.nvim_win_set_config, 0, { win = t2_bot_split, split = 'below' })
3030      )
3031      matches(
3032        'E36: Not enough room$',
3033        pcall_err(api.nvim_win_set_config, t1_float, { win = t2_top_split, split = 'above' })
3034      )
3035      matches(
3036        'E36: Not enough room$',
3037        pcall_err(api.nvim_win_set_config, t1_float, { win = t2_top_split, split = 'below' })
3038      )
3039      matches(
3040        'E36: Not enough room$',
3041        pcall_err(api.nvim_win_set_config, t1_float, { win = t2_bot_split, split = 'above' })
3042      )
3043      matches(
3044        'E36: Not enough room$',
3045        pcall_err(api.nvim_win_set_config, t1_float, { win = t2_bot_split, split = 'below' })
3046      )
3047      eq(t1_cur_win, api.nvim_get_current_win())
3048      eq(t1_info, tp_info(t1))
3049      eq(t1_float_config, api.nvim_win_get_config(t1_float))
3050    end)
3051
3052    it('attempt to move window from other tabpage with no room', function()
3053      -- Fill up the 1st tabpage with horizontal splits, then create a 2nd with only a few. Go back
3054      -- to the 1st and try to move windows from the 2nd (while it's non-current) to it. Check that
3055      -- window positions and sizes in the 2nd are unchanged.
3056      command('set laststatus=0')
3057      matches('E36: Not enough room$', pcall_err(command, 'execute "split|"->repeat(&lines)'))
3058
3059      command('tab split')
3060      local t2 = api.nvim_get_current_tabpage()
3061      local t2_top = api.nvim_get_current_win()
3062      command('belowright split')
3063      local t2_mid_left = api.nvim_get_current_win()
3064      command('belowright vsplit')
3065      local t2_mid_right = api.nvim_get_current_win()
3066      command('split | wincmd J')
3067      local t2_bot = api.nvim_get_current_win()
3068      local tp_info = define_tp_info_function()
3069      local t2_info = tp_info(t2)
3070      eq({
3071        'col',
3072        {
3073          { 'leaf', t2_top },
3074          {
3075            'row',
3076            {
3077              { 'leaf', t2_mid_left },
3078              { 'leaf', t2_mid_right },
3079            },
3080          },
3081          { 'leaf', t2_bot },
3082        },
3083      }, t2_info.layout)
3084
3085      local function try_move_t2_wins_to_t1()
3086        for _, w in ipairs({ t2_bot, t2_mid_left, t2_mid_right, t2_top }) do
3087          matches(
3088            'E36: Not enough room$',
3089            pcall_err(api.nvim_win_set_config, w, { win = 0, split = 'below' })
3090          )
3091          eq(t2_info, tp_info(t2))
3092        end
3093      end
3094      command('tabfirst')
3095      try_move_t2_wins_to_t1()
3096      -- Go to the 2nd tabpage to ensure nothing changes after win_comp_pos, last_status, .etc.
3097      -- from enter_tabpage.
3098      command('tabnext')
3099      eq(t2_info, tp_info(t2))
3100
3101      -- Check things are fine with the global statusline too, for good measure.
3102      -- Set it while the 2nd tabpage is current, so last_status runs for it.
3103      command('set laststatus=3')
3104      t2_info = tp_info(t2)
3105      command('tabfirst')
3106      try_move_t2_wins_to_t1()
3107    end)
3108
3109    it('handles textlock', function()
3110      command('tabnew')
3111      local t2_win = api.nvim_get_current_win()
3112      command('tabfirst')
3113
3114      exec(([[
3115        new
3116        call setline(1, 'foo')
3117        setlocal debug=throw indentexpr=nvim_win_set_config(0,#{split:'left',win:%d})
3118      ]]):format(t2_win))
3119      local cur_win = api.nvim_get_current_win()
3120      matches(
3121        'E565: Not allowed to change text or change window$',
3122        pcall_err(command, 'normal! ==')
3123      )
3124      eq(cur_win, api.nvim_get_current_win())
3125      exec(([[
3126        wincmd p
3127        call setline(1, 'bar')
3128        setlocal indentexpr=nvim_win_set_config(win_getid(winnr('#')),#{split:'left',win:%d})
3129      ]]):format(t2_win))
3130      neq(cur_win, api.nvim_get_current_win())
3131      matches(
3132        'E565: Not allowed to change text or change window$',
3133        pcall_err(command, 'normal! ==')
3134      )
3135      -- expr_map_lock
3136      exec(([[
3137        nnoremap <expr> @ nvim_win_set_config(win_getid(winnr('#')),#{split:'left',win:%d})
3138      ]]):format(t2_win))
3139      neq(cur_win, api.nvim_get_current_win())
3140      matches(
3141        'E565: Not allowed to change text or change window$',
3142        pcall_err(fn.feedkeys, '@', 'x')
3143      )
3144
3145      exec(([[
3146        wincmd p
3147        autocmd WinNewPre * ++once call nvim_win_set_config(0, #{relative:'editor', win:%d, row:0, col:0, width:1, height:1})
3148      ]]):format(t2_win))
3149      matches(
3150        'E1312: Not allowed to change the window layout in this autocmd$',
3151        pcall_err(command, 'split')
3152      )
3153      eq(cur_win, api.nvim_get_current_win()) -- :split didn't enter new window due to error
3154
3155      exec(([[
3156        autocmd WinLeave * ++once call nvim_win_set_config(0, #{relative:'editor', win:%d, row:0, col:0, width:1, height:1})
3157      ]]):format(t2_win))
3158      matches('Cannot move window to another tabpage whilst in use$', pcall_err(command, 'quit'))
3159      eq(cur_win, api.nvim_get_current_win()) -- :quit didn't close window due to error
3160    end)
3161
3162    it('updates statusline when moving bottom split', function()
3163      local screen = Screen.new(10, 10)
3164      exec([[
3165        set laststatus=0
3166        belowright split
3167        call nvim_win_set_config(0, #{split: 'above', win: win_getid(winnr('#'))})
3168      ]])
3169      screen:expect([[
3170        ^            |
3171        {1:~           }|*3
3172        {3:[No Name]   }|
3173                    |
3174        {1:~           }|*3
3175                    |
3176      ]])
3177    end)
3178
3179    it("updates tp_curwin of moved window's original tabpage", function()
3180      local t1 = api.nvim_get_current_tabpage()
3181      command('tab split | split')
3182      local t2 = api.nvim_get_current_tabpage()
3183      local t2_alt_win = api.nvim_get_current_win()
3184      command('vsplit')
3185      local t2_cur_win = api.nvim_get_current_win()
3186      command('tabprevious')
3187      eq(t2_cur_win, api.nvim_tabpage_get_win(t2))
3188
3189      -- tp_curwin is unchanged when moved within the same tabpage.
3190      api.nvim_win_set_config(t2_cur_win, { split = 'left', win = t2_alt_win })
3191      eq(t2_cur_win, api.nvim_tabpage_get_win(t2))
3192
3193      -- Also unchanged if the move failed.
3194      command('let &winwidth = &columns | let &winminwidth = &columns')
3195      matches(
3196        'E36: Not enough room$',
3197        pcall_err(api.nvim_win_set_config, t2_cur_win, { split = 'left', win = 0 })
3198      )
3199      eq(t2_cur_win, api.nvim_tabpage_get_win(t2))
3200      command('set winminwidth& winwidth&')
3201
3202      -- But is changed if successfully moved to a different tabpage.
3203      api.nvim_win_set_config(t2_cur_win, { split = 'left', win = 0 })
3204      eq(t2_alt_win, api.nvim_tabpage_get_win(t2))
3205      eq(t1, api.nvim_win_get_tabpage(t2_cur_win))
3206
3207      -- Now do it for a float, which has different altwin logic.
3208      command('tabnext')
3209      t2_cur_win =
3210        api.nvim_open_win(0, true, { relative = 'editor', row = 5, col = 5, width = 5, height = 5 })
3211      eq(t2_alt_win, fn.win_getid(fn.winnr('#')))
3212      command('tabprevious')
3213      eq(t2_cur_win, api.nvim_tabpage_get_win(t2))
3214
3215      api.nvim_win_set_config(t2_cur_win, { split = 'left', win = 0 })
3216      eq(t2_alt_win, api.nvim_tabpage_get_win(t2))
3217      eq(t1, api.nvim_win_get_tabpage(t2_cur_win))
3218
3219      -- Very fun: move curwin between tabpages, converting from split to float, but with an autocmd
3220      -- that deletes altwin after we're bumped to it, re-enters curwin, then switches to a 3rd
3221      -- tabpage. tp_curwin of the window's old tabpage shouldn't be set to the freed altwin!
3222      command('tablast | tab split | tabprevious | split')
3223      command('autocmd WinEnter * ++once quit | let expect_alt = win_getid() | wincmd p | tabnext')
3224      api.nvim_win_set_config(0, {
3225        relative = 'editor',
3226        win = api.nvim_tabpage_get_win(t1),
3227        row = 0,
3228        col = 0,
3229        width = 5,
3230        height = 5,
3231      })
3232      eq(eval('g:expect_alt'), api.nvim_tabpage_get_win(t2))
3233
3234      -- Same, but for float -> float.
3235      command('tabprevious | split')
3236      api.nvim_open_win(0, true, { relative = 'editor', row = 0, col = 0, width = 1, height = 1 })
3237      command('autocmd WinEnter * ++once quit | let expect_alt = win_getid() | wincmd p | tabnext')
3238      api.nvim_win_set_config(0, {
3239        relative = 'editor',
3240        win = api.nvim_tabpage_get_win(t1),
3241        row = 0,
3242        col = 0,
3243        width = 5,
3244        height = 5,
3245      })
3246      eq(eval('g:expect_alt'), api.nvim_tabpage_get_win(t2))
3247    end)
3248
3249    it('set_config cannot change "noautocmd" #36409', function()
3250      local cfg = { relative = 'editor', row = 1, col = 1, height = 2, width = 2, noautocmd = true }
3251      local win = api.nvim_open_win(0, false, cfg)
3252      cfg.height = 10
3253      eq(true, pcall(api.nvim_win_set_config, win, cfg))
3254      cfg.noautocmd = false
3255      eq(
3256        "'noautocmd' cannot be changed on existing window",
3257        pcall_err(api.nvim_win_set_config, win, cfg)
3258      )
3259    end)
3260
3261    it('removes last statusline if needed', function()
3262      local screen = Screen.new(30, 9)
3263      command('set laststatus=1 | botright split')
3264      screen:expect([[
3265                                      |
3266        {1:~                             }|*2
3267        {2:[No Name]                     }|
3268        ^                              |
3269        {1:~                             }|*2
3270        {3:[No Name]                     }|
3271                                      |
3272      ]])
3273      api.nvim_win_set_config(0, { relative = 'editor', row = 0, col = 0, width = 4, height = 4 })
3274      screen:expect([[
3275        {4:^    }                          |
3276        {11:~   }{1:                          }|*3
3277        {1:~                             }|*4
3278                                      |
3279      ]])
3280      command('quit | set laststatus=2 | botright split')
3281      screen:expect([[
3282                                      |
3283        {1:~                             }|*2
3284        {2:[No Name]                     }|
3285        ^                              |
3286        {1:~                             }|*2
3287        {3:[No Name]                     }|
3288                                      |
3289      ]])
3290      api.nvim_win_set_config(0, { relative = 'editor', row = 1, col = 5, width = 4, height = 4 })
3291      screen:expect([[
3292                                      |
3293        {1:~    }{4:^    }{1:                     }|
3294        {1:~    }{11:~   }{1:                     }|*3
3295        {1:~                             }|*2
3296        {2:[No Name]                     }|
3297                                      |
3298      ]])
3299    end)
3300
3301    it('can convert external window to non-external', function()
3302      Screen.new(20, 7, { ext_multigrid = true }) -- multigrid needed for external windows
3303      api.nvim_open_win(0, true, { external = true, width = 5, height = 5 })
3304      eq(true, api.nvim_win_get_config(0).external)
3305      api.nvim_win_set_config(0, { split = 'below', win = fn.win_getid(1) })
3306      eq(false, api.nvim_win_get_config(0).external)
3307
3308      api.nvim_win_set_config(0, { external = true, width = 5, height = 5 })
3309      eq(true, api.nvim_win_get_config(0).external)
3310      api.nvim_win_set_config(0, { relative = 'editor', row = 3, col = 3 })
3311      eq(false, api.nvim_win_get_config(0).external)
3312
3313      api.nvim_win_set_config(0, { external = true, width = 5, height = 5 })
3314      eq(true, api.nvim_win_get_config(0).external)
3315      api.nvim_win_set_config(0, { external = false })
3316      eq(false, api.nvim_win_get_config(0).external)
3317    end)
3318  end)
3319
3320  describe('get_config', function()
3321    it('includes border', function()
3322      local b = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h' }
3323      local win = api.nvim_open_win(0, true, {
3324        relative = 'win',
3325        row = 3,
3326        col = 3,
3327        width = 12,
3328        height = 3,
3329        border = b,
3330      })
3331
3332      local cfg = api.nvim_win_get_config(win)
3333      eq(b, cfg.border)
3334    end)
3335
3336    it('includes border with highlight group', function()
3337      local b = {
3338        { 'a', 'Normal' },
3339        { 'b', 'Special' },
3340        { 'c', 'String' },
3341        { 'd', 'Comment' },
3342        { 'e', 'Visual' },
3343        { 'f', 'Error' },
3344        { 'g', 'Constant' },
3345        { 'h', 'PreProc' },
3346      }
3347      local win = api.nvim_open_win(0, true, {
3348        relative = 'win',
3349        row = 3,
3350        col = 3,
3351        width = 12,
3352        height = 3,
3353        border = b,
3354      })
3355
3356      local cfg = api.nvim_win_get_config(win)
3357      eq(b, cfg.border)
3358    end)
3359
3360    it('includes title and footer', function()
3361      local title = { { 'A', { 'StatusLine', 'TabLine' } }, { 'B' }, { 'C', 'WinBar' } }
3362      local footer = { { 'A', 'WinBar' }, { 'B' }, { 'C', { 'StatusLine', 'TabLine' } } }
3363      local win = api.nvim_open_win(0, true, {
3364        relative = 'win',
3365        row = 3,
3366        col = 3,
3367        width = 12,
3368        height = 3,
3369        border = 'single',
3370        title = title,
3371        footer = footer,
3372      })
3373
3374      local cfg = api.nvim_win_get_config(win)
3375      eq(title, cfg.title)
3376      eq(footer, cfg.footer)
3377    end)
3378
3379    it('includes split for normal windows', function()
3380      local win = api.nvim_open_win(0, true, {
3381        vertical = true,
3382        win = -1,
3383      })
3384      eq('left', api.nvim_win_get_config(win).split)
3385      api.nvim_win_set_config(win, {
3386        vertical = false,
3387        win = -1,
3388      })
3389      eq('above', api.nvim_win_get_config(win).split)
3390      api.nvim_win_set_config(win, {
3391        split = 'below',
3392        win = -1,
3393      })
3394      eq('below', api.nvim_win_get_config(win).split)
3395    end)
3396
3397    it('includes split when splitting with ex commands', function()
3398      local win = api.nvim_get_current_win()
3399      eq('left', api.nvim_win_get_config(win).split)
3400
3401      command('vsplit')
3402      local win2 = api.nvim_get_current_win()
3403
3404      -- initial window now be marked as right split
3405      -- since it was split with a vertical split
3406      -- and 'splitright' is false by default
3407      eq('right', api.nvim_win_get_config(win).split)
3408      eq('left', api.nvim_win_get_config(win2).split)
3409
3410      api.nvim_set_option_value('splitbelow', true, {
3411        scope = 'global',
3412      })
3413      api.nvim_win_close(win, true)
3414      command('split')
3415      local win3 = api.nvim_get_current_win()
3416      eq('below', api.nvim_win_get_config(win3).split)
3417    end)
3418
3419    it("includes the correct 'split' option in complex layouts", function()
3420      local initial_win = api.nvim_get_current_win()
3421      local win = api.nvim_open_win(0, false, {
3422        split = 'right',
3423        win = -1,
3424      })
3425
3426      local win2 = api.nvim_open_win(0, false, {
3427        split = 'below',
3428        win = win,
3429      })
3430
3431      api.nvim_win_set_config(win2, {
3432        width = 50,
3433      })
3434
3435      api.nvim_win_set_config(win, {
3436        split = 'left',
3437        win = -1,
3438      })
3439
3440      local win3 = api.nvim_open_win(0, false, {
3441        split = 'above',
3442        win = -1,
3443      })
3444      local float = api.nvim_open_win(0, false, {
3445        relative = 'editor',
3446        width = 40,
3447        height = 20,
3448        col = 20,
3449        row = 10,
3450      })
3451      api.nvim_win_set_config(float, {
3452        split = 'right',
3453        win = -1,
3454      })
3455
3456      local layout = fn.winlayout()
3457
3458      eq({
3459        'row',
3460        {
3461          {
3462            'col',
3463            {
3464              { 'leaf', win3 },
3465              {
3466                'row',
3467                {
3468                  { 'leaf', win },
3469                  { 'leaf', initial_win },
3470                  { 'leaf', win2 },
3471                },
3472              },
3473            },
3474          },
3475          {
3476            'leaf',
3477            float,
3478          },
3479        },
3480      }, layout)
3481
3482      eq('above', api.nvim_win_get_config(win3).split)
3483      eq('left', api.nvim_win_get_config(win).split)
3484      eq('left', api.nvim_win_get_config(initial_win).split)
3485      eq('right', api.nvim_win_get_config(win2).split)
3486      eq('right', api.nvim_win_get_config(float).split)
3487    end)
3488
3489    it('includes style', function()
3490      local unused_style1 = api.nvim_open_win(0, false, {
3491        width = 10,
3492        height = 10,
3493        relative = 'editor',
3494        row = 10,
3495        col = 10,
3496      })
3497      local unused_style2 = api.nvim_open_win(0, false, {
3498        width = 10,
3499        height = 10,
3500        relative = 'editor',
3501        row = 10,
3502        col = 10,
3503        style = '',
3504      })
3505      local minimal_style = api.nvim_open_win(0, false, {
3506        width = 10,
3507        height = 10,
3508        relative = 'editor',
3509        row = 10,
3510        col = 10,
3511        style = 'minimal',
3512      })
3513
3514      eq('', api.nvim_win_get_config(unused_style1).style)
3515      eq('', api.nvim_win_get_config(unused_style2).style)
3516      eq('minimal', api.nvim_win_get_config(minimal_style).style)
3517
3518      -- "style" is allowed for splits too.
3519      eq('', api.nvim_win_get_config(0).relative)
3520      eq('', api.nvim_win_get_config(0).style)
3521      api.nvim_win_set_config(0, { style = 'minimal' })
3522      eq('minimal', api.nvim_win_get_config(0).style)
3523      api.nvim_win_set_config(0, { height = 1 }) -- "style" unchanged when not included.
3524      eq('minimal', api.nvim_win_get_config(0).style)
3525      api.nvim_win_set_config(0, { style = '' })
3526      eq('', api.nvim_win_get_config(0).style)
3527    end)
3528  end)
3529
3530  describe('set_config', function()
3531    it('no crash with invalid title', function()
3532      local win = api.nvim_open_win(0, true, {
3533        width = 10,
3534        height = 10,
3535        relative = 'editor',
3536        row = 10,
3537        col = 10,
3538        title = { { 'test' } },
3539        border = 'single',
3540      })
3541      eq(
3542        "Invalid 'title/footer': expected String or Array, got Integer",
3543        pcall_err(api.nvim_win_set_config, win, { title = 0 })
3544      )
3545      command('redraw!')
3546      assert_alive()
3547      eq(
3548        "Invalid 'title/footer': expected non-empty Array",
3549        pcall_err(api.nvim_win_set_config, win, { title = {} })
3550      )
3551      command('redraw!')
3552      assert_alive()
3553    end)
3554
3555    it('no crash with invalid footer', function()
3556      local win = api.nvim_open_win(0, true, {
3557        width = 10,
3558        height = 10,
3559        relative = 'editor',
3560        row = 10,
3561        col = 10,
3562        footer = { { 'test' } },
3563        border = 'single',
3564      })
3565      eq(
3566        "Invalid 'title/footer': expected String or Array, got Integer",
3567        pcall_err(api.nvim_win_set_config, win, { footer = 0 })
3568      )
3569      command('redraw!')
3570      assert_alive()
3571      eq(
3572        "Invalid 'title/footer': expected non-empty Array",
3573        pcall_err(api.nvim_win_set_config, win, { footer = {} })
3574      )
3575      command('redraw!')
3576      assert_alive()
3577    end)
3578
3579    describe('no crash or memory leak', function()
3580      local win
3581
3582      before_each(function()
3583        win = api.nvim_open_win(0, false, {
3584          relative = 'editor',
3585          row = 10,
3586          col = 10,
3587          height = 10,
3588          width = 10,
3589          border = 'single',
3590          title = { { 'OLD_TITLE' } },
3591          footer = { { 'OLD_FOOTER' } },
3592        })
3593      end)
3594
3595      it('with valid title and invalid footer', function()
3596        eq(
3597          "Invalid 'title/footer': expected String or Array, got Integer",
3598          pcall_err(api.nvim_win_set_config, win, {
3599            title = { { 'NEW_TITLE' } },
3600            footer = 0,
3601          })
3602        )
3603        command('redraw!')
3604        assert_alive()
3605        eq({ { 'OLD_TITLE' } }, api.nvim_win_get_config(win).title)
3606      end)
3607
3608      it('with invalid title and valid footer', function()
3609        eq(
3610          "Invalid 'title/footer': expected String or Array, got Integer",
3611          pcall_err(api.nvim_win_set_config, win, {
3612            title = 0,
3613            footer = { { 'NEW_FOOTER' } },
3614          })
3615        )
3616        command('redraw!')
3617        assert_alive()
3618        eq({ { 'OLD_FOOTER' } }, api.nvim_win_get_config(win).footer)
3619      end)
3620    end)
3621
3622    it('cannot split from a float', function()
3623      local win = api.nvim_get_current_win()
3624      local float_win = api.nvim_open_win(0, true, {
3625        relative = 'editor',
3626        width = 10,
3627        height = 10,
3628        row = 10,
3629        col = 10,
3630      })
3631      eq(
3632        'Cannot split a floating window',
3633        pcall_err(api.nvim_win_set_config, win, { win = float_win, split = 'right' })
3634      )
3635      eq(
3636        'Cannot split a floating window',
3637        pcall_err(api.nvim_win_set_config, win, { win = 0, split = 'right' })
3638      )
3639
3640      -- No errors when not actually splitting.
3641      local cfg = api.nvim_win_get_config(win)
3642      api.nvim_win_set_config(win, {})
3643      eq(cfg, api.nvim_win_get_config(win))
3644
3645      eq(1, eval('&cmdheight'))
3646      api.nvim_win_set_config(win, { height = 1 })
3647      cfg.height = 1
3648      eq(cfg, api.nvim_win_get_config(win))
3649      eq(23, eval('&cmdheight'))
3650
3651      api.nvim_win_set_config(win, { style = 'minimal' })
3652      cfg.style = 'minimal'
3653      eq(cfg, api.nvim_win_get_config(win))
3654    end)
3655
3656    it('cannot move autocmd window between tabpages', function()
3657      local win_type, split_ok, split_err, float_ok, float_err = exec_lua(function()
3658        local other_tp_win = vim.api.nvim_get_current_win()
3659        vim.cmd.tabnew()
3660
3661        local win_type, split_ok, split_err, float_ok, float_err
3662        vim.api.nvim_buf_call(vim.api.nvim_create_buf(true, true), function()
3663          win_type = vim.fn.win_gettype()
3664
3665          split_ok, split_err =
3666            pcall(vim.api.nvim_win_set_config, 0, { win = other_tp_win, split = 'right' })
3667
3668          float_ok, float_err = pcall(vim.api.nvim_win_set_config, 0, { win = other_tp_win })
3669        end)
3670        return win_type, split_ok, split_err, float_ok, float_err
3671      end)
3672
3673      eq('autocmd', win_type)
3674      eq({ false, 'Cannot move autocmd window to another tabpage' }, { split_ok, split_err })
3675      eq({ false, 'Cannot move autocmd window to another tabpage' }, { float_ok, float_err })
3676    end)
3677
3678    it('can move cmdwin between tabpages #40312', function()
3679      local other_tp_win = api.nvim_get_current_win()
3680      command('tabnew')
3681      feed('q:')
3682      n.poke_eventloop()
3683      eq('command', fn.win_gettype())
3684      api.nvim_win_set_config(0, { win = other_tp_win, split = 'right' })
3685    end)
3686
3687    it('minimal style persists through float-to-split and buffer change #37067', function()
3688      -- Set all options globally
3689      command('set number relativenumber cursorline cursorcolumn spell list')
3690      command('set signcolumn=yes colorcolumn=80 statuscolumn=%l foldcolumn=2')
3691      local buf1 = api.nvim_create_buf(false, true)
3692      local win = api.nvim_open_win(buf1, true, {
3693        relative = 'editor',
3694        width = 10,
3695        height = 10,
3696        row = 5,
3697        col = 5,
3698        style = 'minimal',
3699      })
3700      -- Convert to split then change buffer
3701      api.nvim_win_set_config(win, { split = 'below', win = -1 })
3702      local buf2 = api.nvim_create_buf(false, true)
3703      api.nvim_win_set_buf(win, buf2)
3704      eq(false, api.nvim_get_option_value('number', { win = win }))
3705      eq(false, api.nvim_get_option_value('relativenumber', { win = win }))
3706      eq(false, api.nvim_get_option_value('cursorline', { win = win }))
3707      eq(false, api.nvim_get_option_value('cursorcolumn', { win = win }))
3708      eq(false, api.nvim_get_option_value('spell', { win = win }))
3709      eq(false, api.nvim_get_option_value('list', { win = win }))
3710      eq('0', api.nvim_get_option_value('foldcolumn', { win = win }))
3711      eq('auto', api.nvim_get_option_value('signcolumn', { win = win }))
3712      eq('', api.nvim_get_option_value('colorcolumn', { win = win }))
3713      eq('', api.nvim_get_option_value('statuscolumn', { win = win }))
3714    end)
3715
3716    it('merges configs only after successfully configuring split', function()
3717      local win = api.nvim_open_win(0, true, {
3718        relative = 'editor',
3719        width = 10,
3720        height = 10,
3721        row = 5,
3722        col = 5,
3723      })
3724      local cfg = api.nvim_win_get_config(win)
3725      eq('', cfg.style)
3726      command('set cursorline | tabnew')
3727      local tp2_win = api.nvim_get_current_win()
3728      command('tabfirst | autocmd WinEnter * ++once wincmd p')
3729      eq(
3730        'Failed to switch away from window 1001',
3731        pcall_err(
3732          api.nvim_win_set_config,
3733          win,
3734          { split = 'below', win = tp2_win, style = 'minimal' }
3735        )
3736      )
3737      eq(cfg, api.nvim_win_get_config(win))
3738      eq(true, api.nvim_get_option_value('cursorline', { win = win }))
3739
3740      exec([[
3741        autocmd WinLeave * ++once let g:style_before = nvim_win_get_config(0).style
3742                               \| let g:cul_before = &cursorline
3743                               \| call nvim_win_set_config(0, #{style: ""})
3744      ]])
3745      api.nvim_win_set_config(win, { split = 'below', win = tp2_win, style = 'minimal' })
3746      eq('', eval('g:style_before'))
3747      eq(1, eval('g:cul_before'))
3748      eq('minimal', api.nvim_win_get_config(win).style)
3749      eq(false, api.nvim_get_option_value('cursorline', { win = win }))
3750    end)
3751
3752    it('minimal style not re-applied if style is unchanged', function()
3753      api.nvim_open_win(0, true, { relative = 'editor', width = 10, height = 10, row = 5, col = 5 })
3754      command('setlocal number rightleft')
3755      api.nvim_win_set_config(0, { style = 'minimal' })
3756      eq(0, eval('&number')) -- style changed; should've reset
3757      command('setlocal number')
3758      api.nvim_win_set_config(0, { style = 'minimal' })
3759      eq(1, eval('&number'))
3760      eq(1, eval('&rightleft')) -- unrelated option unaffected
3761    end)
3762
3763    it('minimal style does not leak WinInfo fold memory', function()
3764      feed('zfG')
3765      api.nvim_open_win(0, true, { split = 'below', style = 'minimal' })
3766      command('quit')
3767    end)
3768
3769    it('preserve current floating window when moving fails', function()
3770      local buf = api.nvim_create_buf(false, true)
3771      local float_win = api.nvim_open_win(buf, true, {
3772        relative = 'editor',
3773        row = 1,
3774        col = 1,
3775        width = 10,
3776        height = 5,
3777      })
3778      command('tabnew')
3779      local tab2_win = api.nvim_get_current_win()
3780      command('tabprev')
3781      api.nvim_set_current_win(float_win)
3782      command('autocmd WinLeave * ++once call nvim_win_close(' .. tab2_win .. ', v:true)')
3783      eq(
3784        'Target windows were closed',
3785        pcall_err(api.nvim_win_set_config, float_win, { win = tab2_win })
3786      )
3787      eq(float_win, api.nvim_get_current_win())
3788
3789      command('tabnew')
3790      local tab3_win = api.nvim_get_current_win()
3791      command('tabprev | autocmd WinEnter * ++once wincmd p')
3792      eq(
3793        ('Failed to switch away from window %d'):format(float_win),
3794        pcall_err(api.nvim_win_set_config, float_win, { win = tab3_win })
3795      )
3796      eq(float_win, api.nvim_get_current_win())
3797    end)
3798  end)
3799
3800  it(':fclose and :only skip hidden and pinned windows #36123', function()
3801    local cfg = { relative = 'editor', row = 0, col = 0, width = 1, height = 1 }
3802    local win1 = api.nvim_open_win(0, false, cfg)
3803    command('fclose')
3804    eq(false, api.nvim_win_is_valid(win1))
3805    cfg.hide = true
3806    win1 = api.nvim_open_win(0, false, cfg)
3807    cfg.hide, cfg.focusable = false, false
3808    local win2 = api.nvim_open_win(0, false, cfg)
3809    command('fclose')
3810    eq(true, api.nvim_win_is_valid(win1))
3811    eq(false, api.nvim_win_is_valid(win2))
3812    api.nvim_win_set_config(win1, { hide = false })
3813    api.nvim_set_option_value('winpinned', true, { win = win1, scope = 'local' })
3814    win2 = api.nvim_open_win(0, false, { split = 'right' })
3815    api.nvim_set_option_value('winpinned', true, { win = win2, scope = 'local' })
3816    command('only')
3817    eq(true, api.nvim_win_is_valid(win1))
3818    eq(true, api.nvim_win_is_valid(win2))
3819    local tab2 = api.nvim_open_tabpage(0, false, {})
3820    command('tabclose')
3821    eq(tab2, api.nvim_get_current_tabpage())
3822  end)
3823
3824  describe('nvim_win_call', function()
3825    it('restores prevwin', function()
3826      local w1 = api.nvim_get_current_win()
3827      command('split')
3828      local w2 = api.nvim_get_current_win()
3829      command('split')
3830      local w3 = api.nvim_get_current_win()
3831      -- Entry state: curwin=w3, prevwin=w2.
3832      eq(w2, fn.win_getid(fn.winnr('#')))
3833      exec_lua(function()
3834        vim.api.nvim_win_call(w1, function()
3835          vim.api.nvim_set_current_win(w2) -- changes prevwin to w1
3836        end)
3837      end)
3838      -- No evidence of the context-switch; curwin/prevwin are restored.
3839      eq(w3, api.nvim_get_current_win())
3840      eq(w2, fn.win_getid(fn.winnr('#')))
3841    end)
3842
3843    it('supports multiple returns', function()
3844      local cur = api.nvim_get_current_win()
3845      local other = api.nvim_open_win(api.nvim_create_buf(false, true), false, { split = 'left' })
3846      exec_lua(function()
3847        function with_len(...)
3848          return select('#', ...), { ... }
3849        end
3850        function test(fn)
3851          local len, res = with_len(vim.api.nvim_win_call(other, fn))
3852          -- convert to serializable vim.NIL
3853          for i = 1, len do
3854            if res[i] == nil then
3855              res[i] = vim.NIL
3856            end
3857          end
3858          return res
3859        end
3860      end)
3861
3862      eq(
3863        { other },
3864        exec_lua(function()
3865          return test(function()
3866            return vim.api.nvim_get_current_win()
3867          end)
3868        end)
3869      )
3870      eq(cur, api.nvim_get_current_win())
3871      eq(
3872        { other, vim.NIL },
3873        exec_lua(function()
3874          return test(function()
3875            return vim.api.nvim_get_current_win(), nil
3876          end)
3877        end)
3878      )
3879
3880      eq(
3881        { 6, 7 },
3882        exec_lua(function()
3883          return test(function()
3884            return 6, 7
3885          end)
3886        end)
3887      )
3888      eq(
3889        { 6, vim.NIL, 7 },
3890        exec_lua(function()
3891          return test(function()
3892            return 6, nil, 7
3893          end)
3894        end)
3895      )
3896      eq(
3897        {},
3898        exec_lua(function()
3899          return test(function() end)
3900        end)
3901      )
3902      eq(
3903        { vim.NIL },
3904        exec_lua(function()
3905          return test(function()
3906            return nil
3907          end)
3908        end)
3909      )
3910    end)
3911
3912    it('propagates return values when called from a coroutine #39834', function()
3913      local other = api.nvim_open_win(api.nvim_create_buf(false, true), false, { split = 'left' })
3914      -- Single return value.
3915      eq(
3916        { other },
3917        exec_lua(function()
3918          local out
3919          local co = coroutine.create(function()
3920            out = {
3921              vim.api.nvim_win_call(other, function()
3922                return vim.api.nvim_get_current_win()
3923              end),
3924            }
3925          end)
3926          assert(coroutine.resume(co))
3927          return out
3928        end)
3929      )
3930      -- Multiple return values (including nil in the middle).
3931      eq(
3932        { 6, vim.NIL, 7 },
3933        exec_lua(function()
3934          local out
3935          local co = coroutine.create(function()
3936            local function pack(...)
3937              local r = { ... }
3938              for i = 1, select('#', ...) do
3939                if r[i] == nil then
3940                  r[i] = vim.NIL
3941                end
3942              end
3943              return r
3944            end
3945            out = pack(vim.api.nvim_win_call(other, function()
3946              return 6, nil, 7
3947            end))
3948          end)
3949          assert(coroutine.resume(co))
3950          return out
3951        end)
3952      )
3953    end)
3954
3955    it('can access window options', function()
3956      command('vsplit')
3957      local win1 = api.nvim_get_current_win()
3958      command('wincmd w')
3959      local win2 = exec_lua [[
3960        win2 = vim.api.nvim_get_current_win()
3961        return win2
3962      ]]
3963      command('wincmd p')
3964
3965      eq('', api.nvim_get_option_value('winhighlight', { win = win1 }))
3966      eq('', api.nvim_get_option_value('winhighlight', { win = win2 }))
3967
3968      local val = exec_lua [[
3969        return vim.api.nvim_win_call(win2, function()
3970          vim.cmd "setlocal winhighlight=Normal:Normal"
3971          return vim.api.nvim_get_current_win()
3972        end)
3973      ]]
3974
3975      eq('', api.nvim_get_option_value('winhighlight', { win = win1 }))
3976      eq('Normal:Normal', api.nvim_get_option_value('winhighlight', { win = win2 }))
3977      eq(win1, api.nvim_get_current_win())
3978      eq(win2, val)
3979    end)
3980
3981    it('failure modes', function()
3982      matches(
3983        'nvim_exec2%(%), line 1: Vim:E492: Not an editor command: fooooo',
3984        pcall_err(exec_lua, [[vim.api.nvim_win_call(0, function() vim.cmd 'fooooo' end)]])
3985      )
3986      eq(
3987        'Lua: [string "<nvim>"]:0: fooooo',
3988        pcall_err(exec_lua, [[vim.api.nvim_win_call(0, function() error('fooooo') end)]])
3989      )
3990    end)
3991
3992    it('does not cause ml_get errors with invalid visual selection', function()
3993      -- Add lines to the current buffer and make another window looking into an empty buffer.
3994      exec_lua [[
3995        _G.api = vim.api
3996        _G.t = function(s) return api.nvim_replace_termcodes(s, true, true, true) end
3997        _G.win_lines = api.nvim_get_current_win()
3998        vim.cmd "new"
3999        _G.win_empty = api.nvim_get_current_win()
4000        api.nvim_set_current_win(win_lines)
4001        api.nvim_buf_set_lines(0, 0, -1, true, {"a", "b", "c"})
4002      ]]
4003
4004      -- Start Visual in current window, redraw in other window with fewer lines.
4005      -- Should be fixed by vim-patch:8.2.4018.
4006      exec_lua [[
4007        api.nvim_feedkeys(t "G<C-V>", "txn", false)
4008        api.nvim_win_call(win_empty, function() vim.cmd "redraw" end)
4009      ]]
4010
4011      -- Start Visual in current window, extend it in other window with more lines.
4012      -- Fixed for win_execute by vim-patch:8.2.4026, but nvim_win_call should also not be affected.
4013      exec_lua [[
4014        api.nvim_feedkeys(t "<Esc>gg", "txn", false)
4015        api.nvim_set_current_win(win_empty)
4016        api.nvim_feedkeys(t "gg<C-V>", "txn", false)
4017        api.nvim_win_call(win_lines, function() api.nvim_feedkeys(t "G<C-V>", "txn", false) end)
4018        vim.cmd "redraw"
4019      ]]
4020    end)
4021
4022    it('updates ruler if cursor moved', function()
4023      -- Fixed for win_execute in vim-patch:8.1.2124, but should've applied to nvim_win_call too!
4024      local screen = Screen.new(30, 5)
4025      exec_lua [[
4026        _G.api = vim.api
4027        vim.opt.ruler = true
4028        local lines = {}
4029        for i = 0, 499 do lines[#lines + 1] = tostring(i) end
4030        api.nvim_buf_set_lines(0, 0, -1, true, lines)
4031        api.nvim_win_set_cursor(0, {20, 0})
4032        vim.cmd "split"
4033        _G.win = api.nvim_get_current_win()
4034        vim.cmd "wincmd w | redraw"
4035      ]]
4036      screen:expect [[
4037        19                            |
4038        {2:< Name] [+] 20,1            3%}|
4039        ^19                            |
4040        {3:< Name] [+] 20,1            3%}|
4041                                      |
4042      ]]
4043      exec_lua [[
4044        api.nvim_win_call(win, function() api.nvim_win_set_cursor(0, {100, 0}) end)
4045        vim.cmd "redraw"
4046      ]]
4047      screen:expect [[
4048        99                            |
4049        {2:< Name] [+] 100,1          19%}|
4050        ^19                            |
4051        {3:< Name] [+] 20,1            3%}|
4052                                      |
4053      ]]
4054    end)
4055
4056    it('can return values by reference', function()
4057      eq(
4058        { 7, 10 },
4059        exec_lua [[
4060        local val = {4, 10}
4061        local ref = vim.api.nvim_win_call(0, function() return val end)
4062        ref[1] = 7
4063        return val
4064      ]]
4065      )
4066    end)
4067
4068    it('layout in current tabpage does not affect windows in others', function()
4069      command('tab split')
4070      local t2_move_win = api.nvim_get_current_win()
4071      command('vsplit')
4072      local t2_other_win = api.nvim_get_current_win()
4073      command('tabprevious')
4074      matches('E36: Not enough room$', pcall_err(command, 'execute "split|"->repeat(&lines)'))
4075      command('vsplit')
4076
4077      -- Without vim-patch:8.2.3862, this gives E36, despite just the 1st tabpage being full.
4078      exec_lua('vim.api.nvim_win_call(..., function() vim.cmd.wincmd "J" end)', t2_move_win)
4079      eq({ 'col', { { 'leaf', t2_other_win }, { 'leaf', t2_move_win } } }, fn.winlayout(2))
4080    end)
4081  end)
4082end)