robin/laufey

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

robinmerge: 9a29622b3b00ddb601

master
56.4 KiB1908 linesraw
1-- Test suite for testing interactions with API bindings
2local t = require('test.testutil')
3local n = require('test.functional.testnvim')()
4local Screen = require('test.functional.ui.screen')
5
6local describe, it, before_each, teardown = t.describe, t.it, t.before_each, t.teardown
7local command = n.command
8local api = n.api
9local fn = n.fn
10local clear = n.clear
11local eq = t.eq
12local exec_lua = n.exec_lua
13local feed = n.feed
14local expect_events = t.expect_events
15local write_file = t.write_file
16local dedent = t.dedent
17local matches = t.matches
18local pcall_err = t.pcall_err
19
20local origlines = {
21  'original line 1',
22  'original line 2',
23  'original line 3',
24  'original line 4',
25  'original line 5',
26  'original line 6',
27  '    indented line',
28}
29
30before_each(function()
31  clear()
32  exec_lua(function()
33    local events = {}
34
35    function _G.test_register(bufnr, evname, id, changedtick, utf_sizes, preview)
36      local function callback(...)
37        table.insert(events, { id, ... })
38        if _G.test_unreg == id then
39          return true
40        end
41      end
42      local opts = {
43        [evname] = callback,
44        on_detach = callback,
45        on_reload = callback,
46        utf_sizes = utf_sizes,
47        preview = preview,
48      }
49      if changedtick then
50        opts.on_changedtick = callback
51      end
52      vim.api.nvim_buf_attach(bufnr, false, opts)
53    end
54
55    function _G.get_events()
56      local ret_events = events
57      events = {}
58      return ret_events
59    end
60  end)
61end)
62
63describe('lua: nvim_buf_attach on_lines', function()
64  local function setup_eventcheck(verify, utf_sizes, lines)
65    local lastsize
66    api.nvim_buf_set_lines(0, 0, -1, true, lines)
67    if verify then
68      lastsize = api.nvim_buf_get_offset(0, api.nvim_buf_line_count(0))
69    end
70    exec_lua('return test_register(...)', 0, 'on_lines', 'test1', false, utf_sizes)
71    local verify_name = 'test1'
72
73    local function check_events(expected)
74      local events = exec_lua('return get_events(...)')
75      if utf_sizes then
76        -- this test case uses ASCII only, so sizes should be the same.
77        -- Unicode is tested below.
78        for _, event in ipairs(expected) do
79          event[9] = event[9] or event[8]
80          event[10] = event[10] or event[9]
81        end
82      end
83      expect_events(expected, events, 'line updates')
84      if verify then
85        for _, event in ipairs(events) do
86          if event[1] == verify_name and event[2] == 'lines' then
87            local startline, endline = event[5], event[7]
88            local newrange = api.nvim_buf_get_offset(0, endline)
89              - api.nvim_buf_get_offset(0, startline)
90            local newsize = api.nvim_buf_get_offset(0, api.nvim_buf_line_count(0))
91            local oldrange = newrange + lastsize - newsize
92            eq(oldrange, event[8])
93            lastsize = newsize
94          end
95        end
96      end
97    end
98    return check_events, function(new)
99      verify_name = new
100    end
101  end
102
103  -- verifying the sizes with nvim_buf_get_offset is nice (checks we cannot
104  -- assert the wrong thing), but masks errors with unflushed lines (as
105  -- nvim_buf_get_offset forces a flush of the memline). To be safe run the
106  -- test both ways.
107  local function check(verify, utf_sizes)
108    local check_events, verify_name = setup_eventcheck(verify, utf_sizes, origlines)
109
110    local tick = api.nvim_buf_get_changedtick(0)
111    command('set autoindent')
112    command('normal! GyyggP')
113    tick = tick + 1
114    check_events { { 'test1', 'lines', 1, tick, 0, 0, 1, 0 } }
115
116    api.nvim_buf_set_lines(0, 3, 5, true, { 'changed line' })
117    tick = tick + 1
118    check_events { { 'test1', 'lines', 1, tick, 3, 5, 4, 32 } }
119
120    exec_lua('return test_register(...)', 0, 'on_lines', 'test2', true, utf_sizes)
121    tick = tick + 1
122    command('undo')
123
124    -- plugins can opt in to receive changedtick events, or choose
125    -- to only receive actual changes.
126    check_events {
127      { 'test1', 'lines', 1, tick, 3, 4, 5, 13 },
128      { 'test2', 'lines', 1, tick, 3, 4, 5, 13 },
129      { 'test2', 'changedtick', 1, tick + 1 },
130    }
131    tick = tick + 1
132
133    tick = tick + 1
134    command('redo')
135    check_events {
136      { 'test1', 'lines', 1, tick, 3, 5, 4, 32 },
137      { 'test2', 'lines', 1, tick, 3, 5, 4, 32 },
138      { 'test2', 'changedtick', 1, tick + 1 },
139    }
140    tick = tick + 1
141
142    tick = tick + 1
143    command('undo!')
144    check_events {
145      { 'test1', 'lines', 1, tick, 3, 4, 5, 13 },
146      { 'test2', 'lines', 1, tick, 3, 4, 5, 13 },
147      { 'test2', 'changedtick', 1, tick + 1 },
148    }
149    tick = tick + 1
150
151    -- simulate next callback returning true
152    exec_lua("test_unreg = 'test1'")
153
154    api.nvim_buf_set_lines(0, 6, 7, true, { 'x1', 'x2', 'x3' })
155    tick = tick + 1
156
157    -- plugins can opt in to receive changedtick events, or choose
158    -- to only receive actual changes.
159    check_events {
160      { 'test1', 'lines', 1, tick, 6, 7, 9, 16 },
161      { 'test2', 'lines', 1, tick, 6, 7, 9, 16 },
162    }
163
164    verify_name 'test2'
165
166    api.nvim_buf_set_lines(0, 1, 1, true, { 'added' })
167    tick = tick + 1
168    check_events { { 'test2', 'lines', 1, tick, 1, 1, 2, 0 } }
169
170    -- Undo restored the cursor (|restore-undo-cursor|); pin it for the relative edits below.
171    api.nvim_win_set_cursor(0, { 5, 0 })
172    feed('wix')
173    tick = tick + 1
174    check_events { { 'test2', 'lines', 1, tick, 4, 5, 5, 16 } }
175
176    -- check hot path for multiple insert
177    feed('yz')
178    tick = tick + 1
179    check_events { { 'test2', 'lines', 1, tick, 4, 5, 5, 17 } }
180
181    feed('<bs>')
182    tick = tick + 1
183    check_events { { 'test2', 'lines', 1, tick, 4, 5, 5, 19 } }
184
185    feed('<esc>Go')
186    tick = tick + 1
187    check_events { { 'test2', 'lines', 1, tick, 11, 11, 12, 0 } }
188
189    feed('x')
190    tick = tick + 1
191    check_events { { 'test2', 'lines', 1, tick, 11, 12, 12, 5 } }
192
193    command('bwipe!')
194    check_events { { 'test2', 'detach', 1 } }
195  end
196
197  it('works', function()
198    check(false)
199  end)
200
201  it('works with verify', function()
202    check(true)
203  end)
204
205  it('works with utf_sizes and ASCII text', function()
206    check(false, true)
207  end)
208
209  local function check_unicode(verify)
210    local unicode_text = {
211      'ascii text',
212      'latin text åäö',
213      'BMP text ɧ αλφά',
214      'BMP text 汉语 ↥↧',
215      'SMP 🤦 🦄🦃',
216      'combining å بِيَّة',
217    }
218    local check_events, verify_name = setup_eventcheck(verify, true, unicode_text)
219
220    local tick = api.nvim_buf_get_changedtick(0)
221
222    feed('ggdd')
223    tick = tick + 1
224    check_events { { 'test1', 'lines', 1, tick, 0, 1, 0, 11, 11, 11 } }
225
226    feed('A<bs>')
227    tick = tick + 1
228    check_events { { 'test1', 'lines', 1, tick, 0, 1, 1, 18, 15, 15 } }
229
230    feed('<esc>jylp')
231    tick = tick + 1
232    check_events { { 'test1', 'lines', 1, tick, 1, 2, 2, 21, 16, 16 } }
233
234    feed('+eea<cr>')
235    tick = tick + 1
236    check_events { { 'test1', 'lines', 1, tick, 2, 3, 4, 23, 15, 15 } }
237
238    feed('<esc>jdw')
239    tick = tick + 1
240    -- non-BMP chars count as 2 UTF-2 codeunits
241    check_events { { 'test1', 'lines', 1, tick, 4, 5, 5, 18, 9, 12 } }
242
243    feed('+rx')
244    tick = tick + 1
245    -- count the individual codepoints of a composed character.
246    check_events { { 'test1', 'lines', 1, tick, 5, 6, 6, 27, 20, 20 } }
247
248    feed('kJ')
249    tick = tick + 1
250    -- verification fails with multiple line updates, sorry about that
251    verify_name ''
252    -- NB: this is inefficient (but not really wrong).
253    check_events {
254      { 'test1', 'lines', 1, tick, 4, 5, 5, 14, 5, 8 },
255      { 'test1', 'lines', 1, tick + 1, 5, 6, 5, 27, 20, 20 },
256    }
257  end
258
259  it('works with utf_sizes and unicode text', function()
260    check_unicode(false)
261  end)
262
263  it('works with utf_sizes and unicode text with verify', function()
264    check_unicode(true)
265  end)
266
267  it('has valid cursor position while shifting', function()
268    api.nvim_buf_set_lines(0, 0, -1, true, { 'line1' })
269    exec_lua(function()
270      vim.api.nvim_buf_attach(0, false, {
271        on_lines = function()
272          vim.api.nvim_set_var('listener_cursor_line', vim.api.nvim_win_get_cursor(0)[1])
273        end,
274      })
275    end)
276    feed('>>')
277    eq(1, api.nvim_get_var('listener_cursor_line'))
278  end)
279
280  it('has valid cursor position while deleting lines', function()
281    api.nvim_buf_set_lines(0, 0, -1, true, { 'line_1', 'line_2', 'line_3', 'line_4' })
282    api.nvim_win_set_cursor(0, { 2, 0 })
283    eq(2, api.nvim_win_get_cursor(0)[1])
284    api.nvim_buf_set_lines(0, 0, -1, true, { 'line_1', 'line_2', 'line_3' })
285    eq(2, api.nvim_win_get_cursor(0)[1])
286  end)
287
288  it('#12718 lnume', function()
289    api.nvim_buf_set_lines(0, 0, -1, true, { '1', '2', '3' })
290    exec_lua(function()
291      vim.api.nvim_buf_attach(0, false, {
292        on_lines = function(...)
293          vim.api.nvim_set_var('linesev', { ... })
294        end,
295      })
296    end)
297    feed('1G0')
298    feed('y<C-v>2j')
299    feed('G0')
300    feed('p')
301    -- Is the last arg old_byte_size correct? Doesn't matter for this PR
302    eq({ 'lines', 1, 4, 2, 3, 5, 4 }, api.nvim_get_var('linesev'))
303
304    feed('2G0')
305    feed('p')
306    eq({ 'lines', 1, 5, 1, 4, 4, 8 }, api.nvim_get_var('linesev'))
307
308    feed('1G0')
309    feed('P')
310    eq({ 'lines', 1, 6, 0, 3, 3, 9 }, api.nvim_get_var('linesev'))
311  end)
312
313  it('nvim_buf_call() from callback does not cause wrong Normal mode CTRL-A #16729', function()
314    exec_lua(function()
315      vim.api.nvim_buf_attach(0, false, {
316        on_lines = function()
317          vim.api.nvim_buf_call(0, function() end)
318        end,
319      })
320    end)
321    feed('itest123<Esc><C-A>')
322    eq('test124', api.nvim_get_current_line())
323  end)
324
325  it('setting extmark in on_lines callback works', function()
326    local screen = Screen.new(40, 6)
327
328    api.nvim_buf_set_lines(0, 0, -1, true, { 'aaa', 'bbb', 'ccc' })
329    exec_lua(function()
330      local ns = vim.api.nvim_create_namespace('')
331      vim.api.nvim_buf_attach(0, false, {
332        on_lines = function(_, _, _, row, _, end_row)
333          vim.api.nvim_buf_clear_namespace(0, ns, row, end_row)
334          for i = row, end_row - 1 do
335            vim.api.nvim_buf_set_extmark(0, ns, i, 0, {
336              virt_text = { { 'NEW' .. tostring(i), 'WarningMsg' } },
337            })
338          end
339        end,
340      })
341    end)
342
343    feed('o')
344    screen:expect({
345      grid = [[
346        aaa                                     |
347        ^ {19:NEW1}                                   |
348        bbb                                     |
349        ccc                                     |
350        {1:~                                       }|
351        {5:-- INSERT --}                            |
352      ]],
353    })
354    feed('<CR>')
355    screen:expect({
356      grid = [[
357        aaa                                     |
358         {19:NEW1}                                   |
359        ^ {19:NEW2}                                   |
360        bbb                                     |
361        ccc                                     |
362        {5:-- INSERT --}                            |
363      ]],
364    })
365  end)
366
367  it('line lengths are correct when pressing TAB with folding #29119', function()
368    api.nvim_buf_set_lines(0, 0, -1, true, { 'a', 'b' })
369
370    exec_lua(function()
371      _G.res = {}
372      vim.o.foldmethod = 'indent'
373      vim.o.softtabstop = -1
374      vim.api.nvim_buf_attach(0, false, {
375        on_lines = function(_, bufnr, _, row, _, end_row)
376          local lines = vim.api.nvim_buf_get_lines(bufnr, row, end_row, true)
377          table.insert(_G.res, lines)
378        end,
379      })
380    end)
381
382    feed('i<Tab>')
383    eq({ '\ta' }, exec_lua('return _G.res[#_G.res]'))
384  end)
385
386  it('quickfix buffer send change', function()
387    command('copen')
388    exec_lua(function()
389      vim.api.nvim_buf_attach(vim.api.nvim_get_current_buf(), false, {
390        on_lines = function(...)
391          vim.g.qf_on_lines = { ... }
392        end,
393        on_bytes = function(...)
394          vim.g.qf_on_bytes = { ... }
395        end,
396      })
397    end)
398    command('caddexpr "foo"')
399    eq({ 'bytes', 2, 2, 0, 0, 0, 0, 0, 0, 0, 6, 6 }, api.nvim_get_var('qf_on_bytes'))
400    eq({ 'lines', 2, 3, 0, 1, 1, 1 }, api.nvim_get_var('qf_on_lines'))
401
402    command('caddexpr "bar"')
403    eq({ 'bytes', 2, 3, 0, 6, 6, 0, 0, 0, 1, 6, 6 }, api.nvim_get_var('qf_on_bytes'))
404    eq({ 'lines', 2, 4, 1, 1, 2, 0 }, api.nvim_get_var('qf_on_lines'))
405
406    command('caddexpr ["line1", "line2", "line3"]')
407    eq({ 'bytes', 2, 4, 1, 6, 13, 0, 0, 0, 3, 8, 26 }, api.nvim_get_var('qf_on_bytes'))
408    eq({ 'lines', 2, 5, 2, 2, 5, 0 }, api.nvim_get_var('qf_on_lines'))
409
410    command('cexpr "replace"')
411    eq({ 'bytes', 2, 5, 0, 0, 0, 4, 0, 40, 0, 10, 10 }, api.nvim_get_var('qf_on_bytes'))
412    eq({ 'lines', 2, 6, 0, 5, 1, 42 }, api.nvim_get_var('qf_on_lines'))
413  end)
414
415  it('single-line visual block insert should not trigger extra on_lines #22009', function()
416    exec_lua(function()
417      _G.res = {}
418      vim.api.nvim_buf_attach(0, false, {
419        on_lines = function(_, bufnr, _, first, last, last_updated, _, _, _)
420          _G.res = { bufnr, first, last, last_updated }
421        end,
422      })
423    end)
424    feed('<C-v>I <ESC>')
425    eq({ api.nvim_get_current_buf(), 0, 1, 1 }, exec_lua('return _G.res'))
426  end)
427
428  it('prompt buffer', function()
429    local check_events = setup_eventcheck(false, nil, {})
430    api.nvim_set_option_value('buftype', 'prompt', {})
431    feed('i')
432    check_events {
433      { 'test1', 'lines', 1, 4, 0, 1, 1, 1 },
434    }
435    fn.prompt_setprompt('', 'foo > ')
436    check_events {
437      { 'test1', 'lines', 1, 5, 0, 1, 1, 3 },
438    }
439    feed('hello')
440    check_events {
441      { 'test1', 'lines', 1, 6, 0, 1, 1, 7 },
442    }
443    fn.prompt_setprompt('', 'super-foo > ')
444    check_events {
445      { 'test1', 'lines', 1, 7, 0, 1, 1, 12 },
446    }
447    eq({ 'super-foo > hello' }, api.nvim_buf_get_lines(0, 0, -1, true))
448    -- Do this in the same event.
449    exec_lua(function()
450      vim.fn.setpos("':", { 0, 1, 999, 0 })
451      vim.fn.prompt_setprompt('', 'discard > ')
452    end)
453    check_events {
454      { 'test1', 'lines', 1, 8, 0, 1, 1, 18 },
455    }
456    eq({ 'discard > ' }, api.nvim_buf_get_lines(0, 0, -1, true))
457    feed('hello<S-CR>there')
458    check_events {
459      { 'test1', 'lines', 1, 9, 0, 1, 1, 11 },
460      { 'test1', 'lines', 1, 10, 0, 1, 2, 16 },
461      { 'test1', 'lines', 1, 11, 1, 2, 2, 1 },
462    }
463    fn.prompt_setprompt('', 'foo > ')
464    check_events {
465      { 'test1', 'lines', 1, 12, 0, 1, 1, 16 },
466    }
467    eq({ 'foo > hello', 'there' }, api.nvim_buf_get_lines(0, 0, -1, true))
468
469    -- init_prompt uses appended_lines_mark when appending to fix prompt.
470    api.nvim_buf_set_lines(0, 0, -1, true, { 'hi' })
471    eq({ 'hi', 'foo > ' }, api.nvim_buf_get_lines(0, 0, -1, true))
472    check_events {
473      { 'test1', 'lines', 1, 13, 0, 2, 1, 18 },
474      { 'test1', 'lines', 1, 14, 1, 1, 2, 0 },
475    }
476  end)
477end)
478
479describe('lua: nvim_buf_attach on_bytes', function()
480  -- verifying the sizes with nvim_buf_get_offset is nice (checks we cannot
481  -- assert the wrong thing), but masks errors with unflushed lines (as
482  -- nvim_buf_get_offset forces a flush of the memline). To be safe run the
483  -- test both ways.
484  local function setup_eventcheck(verify, start_txt)
485    if start_txt then
486      api.nvim_buf_set_lines(0, 0, -1, true, start_txt)
487    else
488      start_txt = api.nvim_buf_get_lines(0, 0, -1, true)
489    end
490    local shadowbytes = table.concat(start_txt, '\n') .. '\n'
491    -- TODO: while we are brewing the real strong coffee,
492    -- verify should check buf_get_offset after every check_events
493    if verify then
494      local len = api.nvim_buf_get_offset(0, api.nvim_buf_line_count(0))
495      eq(len == -1 and 1 or len, string.len(shadowbytes))
496    end
497    exec_lua('return test_register(...)', 0, 'on_bytes', 'test1', false, false, true)
498    api.nvim_buf_get_changedtick(0)
499
500    local verify_name = 'test1'
501    local function check_events(expected)
502      local events = exec_lua('return get_events(...)')
503      expect_events(expected, events, 'byte updates')
504
505      if not verify then
506        return
507      end
508
509      for _, event in ipairs(events) do
510        for _, elem in ipairs(event) do
511          if type(elem) == 'number' and elem < 0 then
512            error('Received event has negative values')
513          end
514        end
515
516        if event[1] == verify_name and event[2] == 'bytes' then
517          local _, _, _, _, _, _, start_byte, _, _, old_byte, _, _, new_byte = unpack(event)
518          local before = string.sub(shadowbytes, 1, start_byte)
519          -- no text in the tests will contain 0xff bytes (invalid UTF-8)
520          -- so we can use it as marker for unknown bytes
521          local unknown = string.rep('\255', new_byte)
522          local after = string.sub(shadowbytes, start_byte + old_byte + 1)
523          shadowbytes = before .. unknown .. after
524        elseif event[1] == verify_name and event[2] == 'reload' then
525          shadowbytes = table.concat(api.nvim_buf_get_lines(0, 0, -1, true), '\n') .. '\n'
526        end
527      end
528
529      local text = api.nvim_buf_get_lines(0, 0, -1, true)
530      local bytes = table.concat(text, '\n') .. '\n'
531
532      eq(
533        string.len(bytes),
534        string.len(shadowbytes),
535        '\non_bytes: total bytecount of buffer is wrong'
536      )
537      for i = 1, string.len(shadowbytes) do
538        local shadowbyte = string.sub(shadowbytes, i, i)
539        if shadowbyte ~= '\255' then
540          eq(string.sub(bytes, i, i), shadowbyte, i)
541        end
542      end
543    end
544
545    return check_events
546  end
547
548  -- Yes, we can do both
549  local function do_both(verify)
550    it('single and multiple join', function()
551      local check_events = setup_eventcheck(verify, origlines)
552      feed 'ggJ'
553      check_events {
554        { 'test1', 'bytes', 1, 3, 0, 15, 15, 1, 0, 1, 0, 1, 1 },
555      }
556
557      feed '3J'
558      check_events {
559        { 'test1', 'bytes', 1, 5, 0, 31, 31, 1, 0, 1, 0, 1, 1 },
560        { 'test1', 'bytes', 1, 5, 0, 47, 47, 1, 0, 1, 0, 1, 1 },
561      }
562    end)
563
564    it('opening lines', function()
565      local check_events = setup_eventcheck(verify, origlines)
566      api.nvim_set_option_value('autoindent', false, {})
567      feed 'Go'
568      check_events {
569        { 'test1', 'bytes', 1, 3, 7, 0, 114, 0, 0, 0, 1, 0, 1 },
570      }
571      feed '<cr>'
572      check_events {
573        { 'test1', 'bytes', 1, 4, 7, 0, 114, 0, 0, 0, 1, 0, 1 },
574      }
575    end)
576
577    it('opening lines with autoindent', function()
578      local check_events = setup_eventcheck(verify, origlines)
579      api.nvim_set_option_value('autoindent', true, {})
580      feed 'Go'
581      check_events {
582        { 'test1', 'bytes', 1, 3, 7, 0, 114, 0, 0, 0, 1, 0, 5 },
583      }
584      feed '<cr>'
585      check_events {
586        { 'test1', 'bytes', 1, 4, 7, 0, 114, 0, 4, 4, 0, 0, 0 },
587        { 'test1', 'bytes', 1, 4, 7, 0, 114, 0, 0, 0, 1, 4, 5 },
588      }
589    end)
590
591    it('setline(num, line)', function()
592      local check_events = setup_eventcheck(verify, origlines)
593      fn.setline(2, 'babla')
594      check_events {
595        { 'test1', 'bytes', 1, 3, 1, 0, 16, 0, 15, 15, 0, 5, 5 },
596      }
597
598      fn.setline(2, { 'foo', 'bar' })
599      check_events {
600        { 'test1', 'bytes', 1, 4, 1, 0, 16, 0, 5, 5, 0, 3, 3 },
601        { 'test1', 'bytes', 1, 5, 2, 0, 20, 0, 15, 15, 0, 3, 3 },
602      }
603
604      local buf_len = api.nvim_buf_line_count(0)
605      fn.setline(buf_len + 1, 'baz')
606      check_events {
607        { 'test1', 'bytes', 1, 6, 7, 0, 90, 0, 0, 0, 1, 0, 4 },
608      }
609    end)
610
611    it('continuing comments with fo=or', function()
612      local check_events = setup_eventcheck(verify, { '// Comment' })
613      api.nvim_set_option_value('formatoptions', 'ro', {})
614      api.nvim_set_option_value('filetype', 'c', {})
615      feed 'A<CR>'
616      check_events {
617        { 'test1', 'bytes', 1, 3, 0, 10, 10, 0, 0, 0, 1, 3, 4 },
618      }
619
620      feed '<ESC>'
621      check_events {
622        { 'test1', 'bytes', 1, 4, 1, 2, 13, 0, 1, 1, 0, 0, 0 },
623      }
624
625      feed 'ggo' -- goto first line to continue testing
626      check_events {
627        { 'test1', 'bytes', 1, 5, 1, 0, 11, 0, 0, 0, 1, 0, 4 },
628      }
629
630      feed '<CR>'
631      check_events {
632        { 'test1', 'bytes', 1, 6, 1, 2, 13, 0, 1, 1, 0, 0, 0 },
633        { 'test1', 'bytes', 1, 6, 1, 2, 13, 0, 0, 0, 1, 3, 4 },
634      }
635    end)
636
637    it('editing empty buffers', function()
638      local check_events = setup_eventcheck(verify, {})
639
640      feed 'ia'
641      check_events {
642        { 'test1', 'bytes', 1, 3, 0, 0, 0, 0, 0, 0, 0, 1, 1 },
643      }
644    end)
645
646    it('deleting lines', function()
647      local check_events = setup_eventcheck(verify, origlines)
648
649      feed('dd')
650
651      check_events {
652        { 'test1', 'bytes', 1, 3, 0, 0, 0, 1, 0, 16, 0, 0, 0 },
653      }
654
655      feed('d2j')
656
657      check_events {
658        { 'test1', 'bytes', 1, 4, 0, 0, 0, 3, 0, 48, 0, 0, 0 },
659      }
660
661      feed('ld<c-v>2j')
662
663      check_events {
664        { 'test1', 'bytes', 1, 5, 0, 1, 1, 0, 1, 1, 0, 0, 0 },
665        { 'test1', 'bytes', 1, 5, 1, 1, 16, 0, 1, 1, 0, 0, 0 },
666        { 'test1', 'bytes', 1, 5, 2, 1, 31, 0, 1, 1, 0, 0, 0 },
667      }
668
669      feed('vjwd')
670
671      check_events {
672        { 'test1', 'bytes', 1, 10, 0, 1, 1, 1, 9, 23, 0, 0, 0 },
673      }
674    end)
675
676    it('changing lines', function()
677      local check_events = setup_eventcheck(verify, origlines)
678
679      feed 'cc'
680      check_events {
681        { 'test1', 'bytes', 1, 3, 0, 0, 0, 0, 15, 15, 0, 0, 0 },
682      }
683
684      feed '<ESC>'
685      check_events {}
686
687      feed 'c3j'
688      check_events {
689        { 'test1', 'bytes', 1, 4, 1, 0, 1, 3, 0, 48, 0, 0, 0 },
690      }
691    end)
692
693    it('visual charwise paste', function()
694      local check_events = setup_eventcheck(verify, { '1234567890' })
695      fn.setreg('a', '___')
696
697      feed '1G1|vll'
698      check_events {}
699
700      feed '"ap'
701      check_events {
702        { 'test1', 'bytes', 1, 3, 0, 0, 0, 0, 3, 3, 0, 0, 0 },
703        { 'test1', 'bytes', 1, 5, 0, 0, 0, 0, 0, 0, 0, 3, 3 },
704      }
705    end)
706
707    it('blockwise paste', function()
708      local check_events = setup_eventcheck(verify, { '1', '2', '3' })
709      feed('1G0')
710      feed('y<C-v>2j')
711      feed('G0')
712      feed('p')
713      check_events {
714        { 'test1', 'bytes', 1, 3, 2, 1, 5, 0, 0, 0, 0, 1, 1 },
715        { 'test1', 'bytes', 1, 3, 3, 0, 7, 0, 0, 0, 0, 3, 3 },
716        { 'test1', 'bytes', 1, 3, 4, 0, 10, 0, 0, 0, 0, 3, 3 },
717      }
718
719      feed('2G0')
720      feed('p')
721      check_events {
722        { 'test1', 'bytes', 1, 4, 1, 1, 3, 0, 0, 0, 0, 1, 1 },
723        { 'test1', 'bytes', 1, 4, 2, 1, 6, 0, 0, 0, 0, 1, 1 },
724        { 'test1', 'bytes', 1, 4, 3, 1, 10, 0, 0, 0, 0, 1, 1 },
725      }
726
727      feed('1G0')
728      feed('P')
729      check_events {
730        { 'test1', 'bytes', 1, 5, 0, 0, 0, 0, 0, 0, 0, 1, 1 },
731        { 'test1', 'bytes', 1, 5, 1, 0, 3, 0, 0, 0, 0, 1, 1 },
732        { 'test1', 'bytes', 1, 5, 2, 0, 7, 0, 0, 0, 0, 1, 1 },
733      }
734    end)
735
736    it('linewise paste', function()
737      local check_events = setup_eventcheck(verify, origlines)
738
739      feed 'yyp'
740      check_events {
741        { 'test1', 'bytes', 1, 3, 1, 0, 16, 0, 0, 0, 1, 0, 16 },
742      }
743
744      feed 'Gyyp'
745      check_events {
746        { 'test1', 'bytes', 1, 4, 8, 0, 130, 0, 0, 0, 1, 0, 18 },
747      }
748    end)
749
750    it('inccomand=nosplit and substitute', function()
751      local check_events = setup_eventcheck(verify, { 'abcde', '12345' })
752      api.nvim_set_option_value('inccommand', 'nosplit', {})
753
754      -- linewise substitute
755      feed(':%s/bcd/')
756      check_events {
757        { 'test1', 'bytes', 1, 3, 0, 1, 1, 0, 3, 3, 0, 0, 0 },
758        { 'test1', 'bytes', 1, 5, 0, 1, 1, 0, 0, 0, 0, 3, 3 },
759      }
760
761      feed('a')
762      check_events {
763        { 'test1', 'bytes', 1, 3, 0, 1, 1, 0, 3, 3, 0, 1, 1 },
764        { 'test1', 'bytes', 1, 5, 0, 1, 1, 0, 1, 1, 0, 3, 3 },
765      }
766
767      feed('<esc>')
768
769      -- splitting lines
770      feed([[:%s/abc/\r]])
771      check_events {
772        { 'test1', 'bytes', 1, 3, 0, 0, 0, 0, 3, 3, 1, 0, 1 },
773        { 'test1', 'bytes', 1, 6, 0, 0, 0, 1, 0, 1, 0, 3, 3 },
774      }
775
776      feed('<esc>')
777      -- multi-line regex
778      feed([[:%s/de\n123/a]])
779
780      check_events {
781        { 'test1', 'bytes', 1, 3, 0, 3, 3, 1, 3, 6, 0, 1, 1 },
782        { 'test1', 'bytes', 1, 6, 0, 3, 3, 0, 1, 1, 1, 3, 6 },
783      }
784
785      feed('<esc>')
786      -- replacing with unicode
787      feed(':%s/b/→')
788
789      check_events {
790        { 'test1', 'bytes', 1, 3, 0, 1, 1, 0, 1, 1, 0, 3, 3 },
791        { 'test1', 'bytes', 1, 5, 0, 1, 1, 0, 3, 3, 0, 1, 1 },
792      }
793
794      feed('<esc>')
795      -- replacing with expression register
796      feed([[:%s/b/\=5+5]])
797      check_events {
798        { 'test1', 'bytes', 1, 3, 0, 1, 1, 0, 1, 1, 0, 2, 2 },
799        { 'test1', 'bytes', 1, 5, 0, 1, 1, 0, 2, 2, 0, 1, 1 },
800      }
801
802      feed('<esc>')
803      -- replacing with backslash
804      feed([[:%s/b/\\]])
805      check_events {
806        { 'test1', 'bytes', 1, 3, 0, 1, 1, 0, 1, 1, 0, 1, 1 },
807        { 'test1', 'bytes', 1, 5, 0, 1, 1, 0, 1, 1, 0, 1, 1 },
808      }
809
810      feed('<esc>')
811      -- replacing with backslash from expression register
812      feed([[:%s/b/\='\']])
813      check_events {
814        { 'test1', 'bytes', 1, 3, 0, 1, 1, 0, 1, 1, 0, 1, 1 },
815        { 'test1', 'bytes', 1, 5, 0, 1, 1, 0, 1, 1, 0, 1, 1 },
816      }
817
818      feed('<esc>')
819      -- replacing with backslash followed by another character
820      feed([[:%s/b/\\!]])
821      check_events {
822        { 'test1', 'bytes', 1, 3, 0, 1, 1, 0, 1, 1, 0, 2, 2 },
823        { 'test1', 'bytes', 1, 5, 0, 1, 1, 0, 2, 2, 0, 1, 1 },
824      }
825
826      feed('<esc>')
827      -- replacing with backslash followed by another character from expression register
828      feed([[:%s/b/\='\!']])
829      check_events {
830        { 'test1', 'bytes', 1, 3, 0, 1, 1, 0, 1, 1, 0, 2, 2 },
831        { 'test1', 'bytes', 1, 5, 0, 1, 1, 0, 2, 2, 0, 1, 1 },
832      }
833    end)
834
835    it('nvim_buf_set_text insert', function()
836      local check_events = setup_eventcheck(verify, { 'bastext' })
837      api.nvim_buf_set_text(0, 0, 3, 0, 3, { 'fiol', 'kontra' })
838      check_events {
839        { 'test1', 'bytes', 1, 3, 0, 3, 3, 0, 0, 0, 1, 6, 11 },
840      }
841
842      api.nvim_buf_set_text(0, 1, 6, 1, 6, { 'punkt', 'syntgitarr', 'övnings' })
843      check_events {
844        { 'test1', 'bytes', 1, 4, 1, 6, 14, 0, 0, 0, 2, 8, 25 },
845      }
846
847      eq(
848        { 'basfiol', 'kontrapunkt', 'syntgitarr', 'övningstext' },
849        api.nvim_buf_get_lines(0, 0, -1, true)
850      )
851    end)
852
853    it('nvim_buf_set_text replace', function()
854      local check_events = setup_eventcheck(verify, origlines)
855
856      api.nvim_buf_set_text(0, 2, 3, 2, 8, { 'very text' })
857      check_events {
858        { 'test1', 'bytes', 1, 3, 2, 3, 35, 0, 5, 5, 0, 9, 9 },
859      }
860
861      api.nvim_buf_set_text(0, 3, 5, 3, 7, { ' splitty', 'line ' })
862      check_events {
863        { 'test1', 'bytes', 1, 4, 3, 5, 57, 0, 2, 2, 1, 5, 14 },
864      }
865
866      api.nvim_buf_set_text(0, 0, 8, 1, 2, { 'JOINY' })
867      check_events {
868        { 'test1', 'bytes', 1, 5, 0, 8, 8, 1, 2, 10, 0, 5, 5 },
869      }
870
871      api.nvim_buf_set_text(0, 4, 0, 6, 0, { 'was 5,6', '' })
872      check_events {
873        { 'test1', 'bytes', 1, 6, 4, 0, 75, 2, 0, 32, 1, 0, 8 },
874      }
875
876      eq({
877        'originalJOINYiginal line 2',
878        'orivery text line 3',
879        'origi splitty',
880        'line l line 4',
881        'was 5,6',
882        '    indented line',
883      }, api.nvim_buf_get_lines(0, 0, -1, true))
884    end)
885
886    it('nvim_buf_set_text delete', function()
887      local check_events = setup_eventcheck(verify, origlines)
888
889      -- really {""} but accepts {} as a shorthand
890      api.nvim_buf_set_text(0, 0, 0, 1, 0, {})
891      check_events {
892        { 'test1', 'bytes', 1, 3, 0, 0, 0, 1, 0, 16, 0, 0, 0 },
893      }
894
895      -- TODO(bfredl): this works but is not as convenient as set_lines
896      api.nvim_buf_set_text(0, 4, 15, 5, 17, { '' })
897      check_events {
898        { 'test1', 'bytes', 1, 4, 4, 15, 79, 1, 17, 18, 0, 0, 0 },
899      }
900      eq({
901        'original line 2',
902        'original line 3',
903        'original line 4',
904        'original line 5',
905        'original line 6',
906      }, api.nvim_buf_get_lines(0, 0, -1, true))
907    end)
908
909    it('checktime autoread', function()
910      write_file(
911        'Xtest-reload',
912        dedent [[
913        old line 1
914        old line 2]]
915      )
916      local atime = os.time() - 10
917      vim.uv.fs_utime('Xtest-reload', atime, atime)
918      command 'e Xtest-reload'
919      command 'set autoread'
920
921      local check_events = setup_eventcheck(verify, nil)
922
923      write_file(
924        'Xtest-reload',
925        dedent [[
926        new line 1
927        new line 2
928        new line 3]]
929      )
930
931      command 'checktime'
932      check_events {
933        { 'test1', 'reload', 1 },
934      }
935
936      feed 'ggJ'
937      check_events {
938        { 'test1', 'bytes', 1, 5, 0, 10, 10, 1, 0, 1, 0, 1, 1 },
939      }
940
941      eq({ 'new line 1 new line 2', 'new line 3' }, api.nvim_buf_get_lines(0, 0, -1, true))
942
943      -- check we can undo and redo a reload event.
944      feed 'u'
945      check_events {
946        { 'test1', 'bytes', 1, 8, 0, 10, 10, 0, 1, 1, 1, 0, 1 },
947      }
948
949      feed 'u'
950      check_events {
951        { 'test1', 'reload', 1 },
952      }
953
954      feed '<c-r>'
955      check_events {
956        { 'test1', 'reload', 1 },
957      }
958
959      feed '<c-r>'
960      check_events {
961        { 'test1', 'bytes', 1, 14, 0, 10, 10, 1, 0, 1, 0, 1, 1 },
962      }
963    end)
964
965    it('tab with noexpandtab and softtabstop', function()
966      command('set noet')
967      command('set ts=4')
968      command('set sw=2')
969      command('set sts=4')
970
971      local check_events = setup_eventcheck(verify, { 'asdfasdf' })
972
973      feed('gg0i<tab>')
974
975      check_events {
976        { 'test1', 'bytes', 1, 3, 0, 0, 0, 0, 0, 0, 0, 1, 1 },
977        { 'test1', 'bytes', 1, 4, 0, 1, 1, 0, 0, 0, 0, 1, 1 },
978      }
979      feed('<tab>')
980
981      -- when spaces are merged into a tabstop
982      check_events {
983        { 'test1', 'bytes', 1, 5, 0, 2, 2, 0, 0, 0, 0, 1, 1 },
984        { 'test1', 'bytes', 1, 6, 0, 3, 3, 0, 0, 0, 0, 1, 1 },
985        { 'test1', 'bytes', 1, 7, 0, 0, 0, 0, 4, 4, 0, 1, 1 },
986      }
987
988      feed('<esc>u')
989      check_events {
990        { 'test1', 'bytes', 1, 9, 0, 0, 0, 0, 1, 1, 0, 4, 4 },
991        { 'test1', 'bytes', 1, 9, 0, 0, 0, 0, 4, 4, 0, 0, 0 },
992      }
993
994      -- in REPLACE mode
995      feed('R<tab><tab>')
996      check_events {
997        { 'test1', 'bytes', 1, 10, 0, 0, 0, 0, 1, 1, 0, 1, 1 },
998        { 'test1', 'bytes', 1, 11, 0, 1, 1, 0, 0, 0, 0, 1, 1 },
999        { 'test1', 'bytes', 1, 12, 0, 2, 2, 0, 1, 1, 0, 1, 1 },
1000        { 'test1', 'bytes', 1, 13, 0, 3, 3, 0, 0, 0, 0, 1, 1 },
1001        { 'test1', 'bytes', 1, 14, 0, 0, 0, 0, 4, 4, 0, 1, 1 },
1002      }
1003      feed('<esc>u')
1004      check_events {
1005        { 'test1', 'bytes', 1, 16, 0, 0, 0, 0, 1, 1, 0, 4, 4 },
1006        { 'test1', 'bytes', 1, 16, 0, 2, 2, 0, 2, 2, 0, 1, 1 },
1007        { 'test1', 'bytes', 1, 16, 0, 0, 0, 0, 2, 2, 0, 1, 1 },
1008      }
1009
1010      -- in VISUALREPLACE mode
1011      feed('gR<tab><tab>')
1012      check_events {
1013        { 'test1', 'bytes', 1, 17, 0, 0, 0, 0, 1, 1, 0, 1, 1 },
1014        { 'test1', 'bytes', 1, 18, 0, 1, 1, 0, 1, 1, 0, 1, 1 },
1015        { 'test1', 'bytes', 1, 19, 0, 2, 2, 0, 1, 1, 0, 1, 1 },
1016        { 'test1', 'bytes', 1, 20, 0, 3, 3, 0, 1, 1, 0, 1, 1 },
1017        { 'test1', 'bytes', 1, 21, 0, 3, 3, 0, 1, 1, 0, 0, 0 },
1018        { 'test1', 'bytes', 1, 22, 0, 3, 3, 0, 0, 0, 0, 1, 1 },
1019        { 'test1', 'bytes', 1, 24, 0, 2, 2, 0, 1, 1, 0, 0, 0 },
1020        { 'test1', 'bytes', 1, 25, 0, 2, 2, 0, 0, 0, 0, 1, 1 },
1021        { 'test1', 'bytes', 1, 27, 0, 1, 1, 0, 1, 1, 0, 0, 0 },
1022        { 'test1', 'bytes', 1, 28, 0, 1, 1, 0, 0, 0, 0, 1, 1 },
1023        { 'test1', 'bytes', 1, 30, 0, 0, 0, 0, 1, 1, 0, 0, 0 },
1024        { 'test1', 'bytes', 1, 31, 0, 0, 0, 0, 0, 0, 0, 1, 1 },
1025        { 'test1', 'bytes', 1, 33, 0, 0, 0, 0, 4, 4, 0, 1, 1 },
1026      }
1027
1028      -- inserting tab after other tabs
1029      command('set sw=4')
1030      feed('<esc>0a<tab>')
1031      check_events {
1032        { 'test1', 'bytes', 1, 34, 0, 1, 1, 0, 0, 0, 0, 1, 1 },
1033        { 'test1', 'bytes', 1, 35, 0, 2, 2, 0, 0, 0, 0, 1, 1 },
1034        { 'test1', 'bytes', 1, 36, 0, 3, 3, 0, 0, 0, 0, 1, 1 },
1035        { 'test1', 'bytes', 1, 37, 0, 4, 4, 0, 0, 0, 0, 1, 1 },
1036        { 'test1', 'bytes', 1, 38, 0, 1, 1, 0, 4, 4, 0, 1, 1 },
1037      }
1038    end)
1039
1040    it('retab', function()
1041      command('set noet')
1042      command('set ts=4')
1043
1044      local check_events = setup_eventcheck(verify, { '			asdf' })
1045      command('retab 8')
1046
1047      check_events {
1048        { 'test1', 'bytes', 1, 3, 0, 0, 0, 0, 7, 7, 0, 9, 9 },
1049      }
1050    end)
1051
1052    it('sends events when undoing with undofile', function()
1053      write_file(
1054        'Xtest-undofile',
1055        dedent([[
1056      12345
1057      hello world
1058      ]])
1059      )
1060
1061      command('e! Xtest-undofile')
1062      command('set undodir=. | set undofile')
1063
1064      local ns = n.request('nvim_create_namespace', 'ns1')
1065      api.nvim_buf_set_extmark(0, ns, 0, 0, {})
1066
1067      eq({ '12345', 'hello world' }, api.nvim_buf_get_lines(0, 0, -1, true))
1068
1069      -- splice
1070      feed('gg0d2l')
1071
1072      eq({ '345', 'hello world' }, api.nvim_buf_get_lines(0, 0, -1, true))
1073
1074      -- move
1075      command('.m+1')
1076
1077      eq({ 'hello world', '345' }, api.nvim_buf_get_lines(0, 0, -1, true))
1078
1079      -- reload undofile and undo changes
1080      command('w')
1081      command('set noundofile')
1082      command('bw!')
1083      command('e! Xtest-undofile')
1084
1085      command('set undofile')
1086
1087      local check_events = setup_eventcheck(verify, nil)
1088
1089      feed('u')
1090      eq({ '345', 'hello world' }, api.nvim_buf_get_lines(0, 0, -1, true))
1091
1092      check_events {
1093        { 'test1', 'bytes', 2, 6, 1, 0, 12, 1, 0, 4, 0, 0, 0 },
1094        { 'test1', 'bytes', 2, 6, 0, 0, 0, 0, 0, 0, 1, 0, 4 },
1095      }
1096
1097      feed('u')
1098      eq({ '12345', 'hello world' }, api.nvim_buf_get_lines(0, 0, -1, true))
1099
1100      check_events {
1101        { 'test1', 'bytes', 2, 8, 0, 0, 0, 0, 0, 0, 0, 2, 2 },
1102      }
1103      command('bw!')
1104    end)
1105
1106    it('blockwise paste with uneven line lengths', function()
1107      local check_events = setup_eventcheck(verify, { 'aaaa', 'aaa', 'aaa' })
1108
1109      -- eq({}, api.nvim_buf_get_lines(0, 0, -1, true))
1110      feed('gg0<c-v>jj$d')
1111
1112      check_events {
1113        { 'test1', 'bytes', 1, 3, 0, 0, 0, 0, 4, 4, 0, 0, 0 },
1114        { 'test1', 'bytes', 1, 3, 1, 0, 1, 0, 3, 3, 0, 0, 0 },
1115        { 'test1', 'bytes', 1, 3, 2, 0, 2, 0, 3, 3, 0, 0, 0 },
1116      }
1117
1118      feed('p')
1119      check_events {
1120        { 'test1', 'bytes', 1, 4, 0, 0, 0, 0, 0, 0, 0, 4, 4 },
1121        { 'test1', 'bytes', 1, 4, 1, 0, 5, 0, 0, 0, 0, 3, 3 },
1122        { 'test1', 'bytes', 1, 4, 2, 0, 9, 0, 0, 0, 0, 3, 3 },
1123      }
1124    end)
1125
1126    it(':luado', function()
1127      local check_events = setup_eventcheck(verify, { 'abc', '12345' })
1128
1129      command(".luado return 'a'")
1130
1131      check_events {
1132        { 'test1', 'bytes', 1, 3, 0, 0, 0, 0, 3, 3, 0, 1, 1 },
1133      }
1134
1135      command('luado return 10')
1136
1137      check_events {
1138        { 'test1', 'bytes', 1, 4, 0, 0, 0, 0, 1, 1, 0, 2, 2 },
1139        { 'test1', 'bytes', 1, 5, 1, 0, 3, 0, 5, 5, 0, 2, 2 },
1140      }
1141    end)
1142
1143    it('flushes deleted bytes on move', function()
1144      local check_events = setup_eventcheck(verify, { 'AAA', 'BBB', 'CCC', 'DDD' })
1145
1146      feed(':.move+1<cr>')
1147
1148      check_events {
1149        { 'test1', 'bytes', 1, 5, 0, 0, 0, 1, 0, 4, 0, 0, 0 },
1150        { 'test1', 'bytes', 1, 5, 1, 0, 4, 0, 0, 0, 1, 0, 4 },
1151      }
1152
1153      feed('jd2j')
1154
1155      check_events {
1156        { 'test1', 'bytes', 1, 6, 2, 0, 8, 2, 0, 8, 0, 0, 0 },
1157      }
1158    end)
1159
1160    it('virtual edit', function()
1161      local check_events = setup_eventcheck(verify, { '', '	' })
1162
1163      api.nvim_set_option_value('virtualedit', 'all', {})
1164
1165      feed [[<Right><Right>iab<ESC>]]
1166
1167      check_events {
1168        { 'test1', 'bytes', 1, 3, 0, 0, 0, 0, 0, 0, 0, 2, 2 },
1169        { 'test1', 'bytes', 1, 4, 0, 2, 2, 0, 0, 0, 0, 2, 2 },
1170      }
1171
1172      feed [[j<Right><Right>iab<ESC>]]
1173
1174      check_events {
1175        { 'test1', 'bytes', 1, 5, 1, 0, 5, 0, 1, 1, 0, 8, 8 },
1176        { 'test1', 'bytes', 1, 6, 1, 5, 10, 0, 0, 0, 0, 2, 2 },
1177      }
1178    end)
1179
1180    it('block visual paste', function()
1181      local check_events = setup_eventcheck(verify, { 'AAA', 'BBB', 'CCC', 'DDD', 'EEE', 'FFF' })
1182      fn.setreg('a', '___')
1183      feed([[gg0l<c-v>3jl"ap]])
1184
1185      check_events {
1186        { 'test1', 'bytes', 1, 3, 0, 1, 1, 0, 2, 2, 0, 0, 0 },
1187        { 'test1', 'bytes', 1, 3, 1, 1, 3, 0, 2, 2, 0, 0, 0 },
1188        { 'test1', 'bytes', 1, 3, 2, 1, 5, 0, 2, 2, 0, 0, 0 },
1189        { 'test1', 'bytes', 1, 3, 3, 1, 7, 0, 2, 2, 0, 0, 0 },
1190        { 'test1', 'bytes', 1, 5, 0, 1, 1, 0, 0, 0, 0, 3, 3 },
1191        { 'test1', 'bytes', 1, 6, 1, 1, 6, 0, 0, 0, 0, 3, 3 },
1192        { 'test1', 'bytes', 1, 7, 2, 1, 11, 0, 0, 0, 0, 3, 3 },
1193        { 'test1', 'bytes', 1, 8, 3, 1, 16, 0, 0, 0, 0, 3, 3 },
1194      }
1195    end)
1196
1197    it('visual paste', function()
1198      local check_events = setup_eventcheck(verify, { 'aaa {', 'b', '}' })
1199      -- Setting up
1200      feed [[jdd]]
1201      check_events {
1202        { 'test1', 'bytes', 1, 3, 1, 0, 6, 1, 0, 2, 0, 0, 0 },
1203      }
1204
1205      -- Actually testing
1206      feed [[v%p]]
1207      check_events {
1208        { 'test1', 'bytes', 1, 8, 0, 4, 4, 1, 1, 3, 0, 0, 0 },
1209        { 'test1', 'bytes', 1, 8, 0, 4, 4, 0, 0, 0, 2, 0, 3 },
1210      }
1211    end)
1212
1213    it('visual paste 2: splitting an empty line', function()
1214      local check_events = setup_eventcheck(verify, { 'abc', '{', 'def', '}' })
1215      feed('ggyyjjvi{p')
1216      check_events {
1217        { 'test1', 'bytes', 1, 6, 2, 0, 6, 1, 0, 4, 0, 0, 0 },
1218        { 'test1', 'bytes', 1, 6, 2, 0, 6, 0, 0, 0, 2, 0, 5 },
1219      }
1220    end)
1221
1222    it('nvim_buf_set_lines', function()
1223      local check_events = setup_eventcheck(verify, { 'AAA', 'BBB' })
1224
1225      -- delete
1226      api.nvim_buf_set_lines(0, 0, 1, true, {})
1227
1228      check_events {
1229        { 'test1', 'bytes', 1, 3, 0, 0, 0, 1, 0, 4, 0, 0, 0 },
1230      }
1231
1232      -- add
1233      api.nvim_buf_set_lines(0, 0, 0, true, { 'asdf' })
1234      check_events {
1235        { 'test1', 'bytes', 1, 4, 0, 0, 0, 0, 0, 0, 1, 0, 5 },
1236      }
1237
1238      -- replace
1239      api.nvim_buf_set_lines(0, 0, 1, true, { 'asdf', 'fdsa' })
1240      check_events {
1241        { 'test1', 'bytes', 1, 5, 0, 0, 0, 1, 0, 5, 2, 0, 10 },
1242      }
1243    end)
1244
1245    it('flushes delbytes on substitute', function()
1246      local check_events = setup_eventcheck(verify, { 'AAA', 'BBB', 'CCC' })
1247
1248      feed('gg0')
1249      command('s/AAA/GGG/')
1250
1251      check_events {
1252        { 'test1', 'bytes', 1, 3, 0, 0, 0, 0, 3, 3, 0, 3, 3 },
1253      }
1254
1255      -- check that byte updates for :delete (which uses curbuf->deleted_bytes2)
1256      -- are correct
1257      command('delete')
1258      check_events {
1259        { 'test1', 'bytes', 1, 4, 0, 0, 0, 1, 0, 4, 0, 0, 0 },
1260      }
1261    end)
1262
1263    it('on_bytes sees modified buffer after substitute', function()
1264      api.nvim_buf_set_lines(0, 0, -1, true, { 'Hello' })
1265
1266      local buffer_lines = exec_lua(function()
1267        local lines
1268        vim.api.nvim_buf_attach(0, false, {
1269          on_bytes = function()
1270            lines = vim.api.nvim_buf_get_lines(0, 0, -1, true)
1271          end,
1272        })
1273        vim.cmd('s/llo/y/')
1274        return lines
1275      end)
1276
1277      -- Make sure on_bytes is called after the buffer is modified.
1278      eq({ 'Hey' }, buffer_lines)
1279    end)
1280
1281    it('on_bytes called multiple times for multiple substitutions on same line', function()
1282      api.nvim_buf_set_lines(0, 0, -1, true, { 'Hello Hello' })
1283
1284      local call_count, args = exec_lua(function()
1285        local count = 0
1286        local args = {}
1287        vim.api.nvim_buf_attach(0, false, {
1288          on_bytes = function(
1289            _,
1290            _,
1291            _,
1292            start_row,
1293            start_col,
1294            start_byte,
1295            old_row,
1296            old_col,
1297            old_byte,
1298            new_row,
1299            new_col,
1300            new_byte
1301          )
1302            count = count + 1
1303            table.insert(args, {
1304              start_row = start_row,
1305              start_col = start_col,
1306              start_byte = start_byte,
1307              old_row = old_row,
1308              old_col = old_col,
1309              old_byte = old_byte,
1310              new_row = new_row,
1311              new_col = new_col,
1312              new_byte = new_byte,
1313              buffer_lines = vim.api.nvim_buf_get_lines(0, 0, -1, true),
1314            })
1315          end,
1316        })
1317        vim.cmd('s/llo/y/g')
1318        return count, args
1319      end)
1320
1321      -- Should be called twice, once for each match.
1322      eq(2, call_count)
1323
1324      -- First match: "llo" at column 2 -> "y".
1325      eq({
1326        start_row = 0,
1327        start_col = 2,
1328        start_byte = 2,
1329        old_row = 0,
1330        old_col = 3,
1331        old_byte = 3,
1332        new_row = 0,
1333        new_col = 1,
1334        new_byte = 1,
1335        buffer_lines = { 'Hey Hey' },
1336      }, args[1])
1337
1338      -- Second match: "llo" at column 8 (in original) -> column 6 (after first substitution).
1339      eq({
1340        start_row = 0,
1341        start_col = 6, -- Adjusted position after first substitution.
1342        start_byte = 6,
1343        old_row = 0,
1344        old_col = 3,
1345        old_byte = 3,
1346        new_row = 0,
1347        new_col = 1,
1348        new_byte = 1,
1349        buffer_lines = { 'Hey Hey' },
1350      }, args[2])
1351    end)
1352
1353    it('on_bytes called correctly for multi-line substitutions', function()
1354      api.nvim_buf_set_lines(0, 0, -1, true, { 'foo bar', 'baz qux' })
1355
1356      local call_count, args = exec_lua(function()
1357        local count = 0
1358        local args = {}
1359        vim.api.nvim_buf_attach(0, false, {
1360          on_bytes = function(
1361            _,
1362            _,
1363            _,
1364            start_row,
1365            start_col,
1366            start_byte,
1367            old_row,
1368            old_col,
1369            old_byte,
1370            new_row,
1371            new_col,
1372            new_byte
1373          )
1374            count = count + 1
1375            table.insert(args, {
1376              start_row = start_row,
1377              start_col = start_col,
1378              start_byte = start_byte,
1379              old_row = old_row,
1380              old_col = old_col,
1381              old_byte = old_byte,
1382              new_row = new_row,
1383              new_col = new_col,
1384              new_byte = new_byte,
1385              buffer_lines = vim.api.nvim_buf_get_lines(0, 0, -1, true),
1386            })
1387          end,
1388        })
1389        vim.cmd('s/bar/X\\rY/')
1390        return count, args
1391      end)
1392
1393      -- Should be called once for the substitution.
1394      eq(1, call_count)
1395
1396      eq({
1397        start_row = 0,
1398        start_col = 4,
1399        start_byte = 4,
1400        old_row = 0,
1401        old_col = 3,
1402        old_byte = 3,
1403        new_row = 1,
1404        new_col = 1,
1405        new_byte = 3,
1406        buffer_lines = { 'foo X', 'Y', 'baz qux' },
1407      }, args[1])
1408    end)
1409
1410    it('on_bytes called multiple times for global substitution creating multiple lines', function()
1411      api.nvim_buf_set_lines(0, 0, -1, true, { 'foo bar baz' })
1412
1413      local call_count, args = exec_lua(function()
1414        local count = 0
1415        local args = {}
1416        vim.api.nvim_buf_attach(0, false, {
1417          on_bytes = function(
1418            _,
1419            _,
1420            _,
1421            start_row,
1422            start_col,
1423            start_byte,
1424            old_row,
1425            old_col,
1426            old_byte,
1427            new_row,
1428            new_col,
1429            new_byte
1430          )
1431            count = count + 1
1432            table.insert(args, {
1433              start_row = start_row,
1434              start_col = start_col,
1435              start_byte = start_byte,
1436              old_row = old_row,
1437              old_col = old_col,
1438              old_byte = old_byte,
1439              new_row = new_row,
1440              new_col = new_col,
1441              new_byte = new_byte,
1442              buffer_lines = vim.api.nvim_buf_get_lines(0, 0, -1, true),
1443            })
1444          end,
1445        })
1446        -- Global substitution with newlines in replacement.
1447        vim.cmd([[s/ /\r/g]])
1448        return count, args
1449      end)
1450
1451      -- Should be called once per space replacement.
1452      eq(2, call_count)
1453
1454      eq({
1455        start_row = 0,
1456        start_col = 3,
1457        start_byte = 3,
1458        old_row = 0,
1459        old_col = 1,
1460        old_byte = 1,
1461        new_row = 1,
1462        new_col = 0,
1463        new_byte = 1,
1464        buffer_lines = { 'foo', 'bar', 'baz' },
1465      }, args[1])
1466
1467      eq({
1468        start_row = 1,
1469        start_col = 3,
1470        start_byte = 7,
1471        old_row = 0,
1472        old_col = 1,
1473        old_byte = 1,
1474        new_row = 1,
1475        new_col = 0,
1476        new_byte = 1,
1477        buffer_lines = { 'foo', 'bar', 'baz' },
1478      }, args[2])
1479    end)
1480
1481    it(
1482      'no buffer update event is emitted while editing substitute command, only after confirmation',
1483      function()
1484        api.nvim_buf_set_lines(0, 0, -1, true, { 'Hello world', 'Hello Neovim' })
1485
1486        exec_lua(function()
1487          _G.num_buffer_updates = 0
1488          vim.api.nvim_buf_attach(0, false, {
1489            on_bytes = function()
1490              _G.num_buffer_updates = _G.num_buffer_updates + 1
1491            end,
1492          })
1493        end)
1494
1495        -- Start typing the substitute command - no events should be emitted yet.
1496        feed(':%s/Hello/Hi')
1497        eq(0, exec_lua('return _G.num_buffer_updates'))
1498
1499        -- Continue editing the command - still no events.
1500        feed('<BS><BS>Hey')
1501        eq(0, exec_lua('return _G.num_buffer_updates'))
1502
1503        -- After confirming the substitution, two events should be emitted (one per line).
1504        feed('<CR>')
1505        eq(2, exec_lua('return _G.num_buffer_updates'))
1506
1507        -- Verify the buffer was actually modified.
1508        eq({ 'Hey world', 'Hey Neovim' }, api.nvim_buf_get_lines(0, 0, -1, true))
1509      end
1510    )
1511
1512    it('flushes delbytes on join', function()
1513      local check_events = setup_eventcheck(verify, { 'AAA', 'BBB', 'CCC' })
1514
1515      feed('gg0J')
1516
1517      check_events {
1518        { 'test1', 'bytes', 1, 3, 0, 3, 3, 1, 0, 1, 0, 1, 1 },
1519      }
1520
1521      command('delete')
1522      check_events {
1523        { 'test1', 'bytes', 1, 5, 0, 0, 0, 1, 0, 8, 0, 0, 0 },
1524      }
1525    end)
1526
1527    it('sends updates on U', function()
1528      feed('ggiAAA<cr>BBB')
1529      feed('<esc>gg$a CCC')
1530
1531      local check_events = setup_eventcheck(verify, nil)
1532
1533      feed('ggU')
1534
1535      check_events {
1536        { 'test1', 'bytes', 1, 6, 0, 7, 7, 0, 0, 0, 0, 3, 3 },
1537      }
1538    end)
1539
1540    it('delete in completely empty buffer', function()
1541      local check_events = setup_eventcheck(verify, nil)
1542
1543      command 'delete'
1544      check_events {}
1545    end)
1546
1547    it('delete the only line of a buffer', function()
1548      local check_events = setup_eventcheck(verify, { 'AAA' })
1549
1550      command 'delete'
1551      check_events {
1552        { 'test1', 'bytes', 1, 3, 0, 0, 0, 1, 0, 4, 1, 0, 1 },
1553      }
1554    end)
1555
1556    it('delete the last line of a buffer with two lines', function()
1557      local check_events = setup_eventcheck(verify, { 'AAA', 'BBB' })
1558
1559      command '2delete'
1560      check_events {
1561        { 'test1', 'bytes', 1, 3, 1, 0, 4, 1, 0, 4, 0, 0, 0 },
1562      }
1563    end)
1564
1565    it(':sort lines', function()
1566      local check_events = setup_eventcheck(verify, { 'CCC', 'BBB', 'AAA' })
1567
1568      command '%sort'
1569      check_events {
1570        { 'test1', 'bytes', 1, 3, 0, 0, 0, 3, 0, 12, 3, 0, 12 },
1571      }
1572    end)
1573
1574    it('handles already sorted lines', function()
1575      local check_events = setup_eventcheck(verify, { 'AAA', 'BBB', 'CCC' })
1576
1577      command '%sort'
1578      check_events {}
1579    end)
1580
1581    it('works with accepting spell suggestions', function()
1582      local check_events = setup_eventcheck(verify, { 'hallo world', 'hallo world' })
1583
1584      feed('gg0z=4<cr><cr>') -- accepts 'Hello'
1585      check_events {
1586        { 'test1', 'bytes', 1, 3, 0, 0, 0, 0, 2, 2, 0, 2, 2 },
1587      }
1588
1589      command('spellrepall') -- replaces whole words
1590      check_events {
1591        { 'test1', 'bytes', 1, 4, 1, 0, 12, 0, 5, 5, 0, 5, 5 },
1592      }
1593    end)
1594
1595    it('works with :diffput and :diffget', function()
1596      local check_events = setup_eventcheck(verify, { 'AAA' })
1597      command('diffthis')
1598      command('new')
1599      command('diffthis')
1600      api.nvim_buf_set_lines(0, 0, -1, true, { 'AAA', 'BBB' })
1601      feed('G')
1602      command('diffput')
1603      check_events {
1604        { 'test1', 'bytes', 1, 3, 1, 0, 4, 0, 0, 0, 1, 0, 4 },
1605      }
1606      api.nvim_buf_set_lines(0, 0, -1, true, { 'AAA', 'CCC' })
1607      feed('<C-w>pG')
1608      command('diffget')
1609      check_events {
1610        { 'test1', 'bytes', 1, 4, 1, 0, 4, 1, 0, 4, 1, 0, 4 },
1611      }
1612    end)
1613
1614    it('prompt buffer', function()
1615      local check_events = setup_eventcheck(verify, {})
1616      api.nvim_set_option_value('buftype', 'prompt', {})
1617      feed('i')
1618      check_events {
1619        { 'test1', 'bytes', 1, 3, 0, 0, 0, 0, 0, 0, 0, 2, 2 },
1620      }
1621      feed('<CR>')
1622      check_events {
1623        { 'test1', 'bytes', 1, 4, 1, 0, 3, 0, 0, 0, 1, 0, 1 },
1624        { 'test1', 'bytes', 1, 5, 1, 0, 3, 0, 0, 0, 0, 2, 2 },
1625      }
1626      feed('<CR>')
1627      check_events {
1628        { 'test1', 'bytes', 1, 6, 2, 0, 6, 0, 0, 0, 1, 0, 1 },
1629        { 'test1', 'bytes', 1, 7, 2, 0, 6, 0, 0, 0, 0, 2, 2 },
1630      }
1631      fn.prompt_setprompt('', 'foo > ')
1632      check_events {
1633        { 'test1', 'bytes', 1, 8, 2, 0, 6, 0, 2, 2, 0, 6, 6 },
1634      }
1635      feed('hello')
1636      check_events {
1637        { 'test1', 'bytes', 1, 9, 2, 6, 12, 0, 0, 0, 0, 5, 5 },
1638      }
1639      fn.prompt_setprompt('', 'uber-foo > ')
1640      check_events {
1641        { 'test1', 'bytes', 1, 10, 2, 0, 6, 0, 6, 6, 0, 11, 11 },
1642      }
1643      eq({ '% ', '% ', 'uber-foo > hello' }, api.nvim_buf_get_lines(0, 0, -1, true))
1644      -- Do this in the same event.
1645      exec_lua(function()
1646        vim.fn.setpos("':", { 0, vim.fn.line('.'), 999, 0 })
1647        vim.fn.prompt_setprompt('', 'discard > ')
1648      end)
1649      check_events {
1650        { 'test1', 'bytes', 1, 11, 2, 0, 6, 0, 16, 16, 0, 10, 10 },
1651      }
1652      eq({ '% ', '% ', 'discard > ' }, api.nvim_buf_get_lines(0, 0, -1, true))
1653      feed('sup<S-CR>dood')
1654      check_events {
1655        { 'test1', 'bytes', 1, 12, 2, 10, 16, 0, 0, 0, 0, 3, 3 },
1656        { 'test1', 'bytes', 1, 13, 2, 13, 19, 0, 0, 0, 1, 0, 1 },
1657        { 'test1', 'bytes', 1, 14, 3, 0, 20, 0, 0, 0, 0, 4, 4 },
1658      }
1659      eq({ '% ', '% ', 'discard > sup', 'dood' }, api.nvim_buf_get_lines(0, 0, -1, true))
1660      fn.prompt_setprompt('', 'cool > ')
1661      check_events {
1662        { 'test1', 'bytes', 1, 15, 2, 0, 6, 0, 10, 10, 0, 7, 7 },
1663      }
1664      eq({ '% ', '% ', 'cool > sup', 'dood' }, api.nvim_buf_get_lines(0, 0, -1, true))
1665
1666      -- init_prompt uses appended_lines_mark when appending to fix prompt.
1667      api.nvim_buf_set_lines(0, 0, -1, true, { 'hi' })
1668      eq({ 'hi', 'cool > ' }, api.nvim_buf_get_lines(0, 0, -1, true))
1669      check_events {
1670        { 'test1', 'bytes', 1, 16, 0, 0, 0, 4, 0, 22, 1, 0, 3 },
1671        { 'test1', 'bytes', 1, 17, 1, 0, 3, 0, 0, 0, 1, 0, 8 },
1672      }
1673    end)
1674
1675    local function test_lockmarks(mode)
1676      local description = (mode ~= '') and mode or '(baseline)'
1677      it('test_lockmarks ' .. description .. ' %delete _', function()
1678        local check_events = setup_eventcheck(verify, { 'AAA', 'BBB', 'CCC' })
1679
1680        command(mode .. ' %delete _')
1681        check_events {
1682          { 'test1', 'bytes', 1, 3, 0, 0, 0, 3, 0, 12, 1, 0, 1 },
1683        }
1684      end)
1685
1686      it('test_lockmarks ' .. description .. ' append()', function()
1687        local check_events = setup_eventcheck(verify)
1688
1689        command(mode .. " call append(0, 'CCC')")
1690        check_events {
1691          { 'test1', 'bytes', 1, 2, 0, 0, 0, 0, 0, 0, 1, 0, 4 },
1692        }
1693
1694        command(mode .. " call append(1, 'BBBB')")
1695        check_events {
1696          { 'test1', 'bytes', 1, 3, 1, 0, 4, 0, 0, 0, 1, 0, 5 },
1697        }
1698
1699        command(mode .. " call append(2, '')")
1700        check_events {
1701          { 'test1', 'bytes', 1, 4, 2, 0, 9, 0, 0, 0, 1, 0, 1 },
1702        }
1703
1704        command(mode .. ' $delete _')
1705        check_events {
1706          { 'test1', 'bytes', 1, 5, 3, 0, 10, 1, 0, 1, 0, 0, 0 },
1707        }
1708
1709        eq('CCC|BBBB|', table.concat(api.nvim_buf_get_lines(0, 0, -1, true), '|'))
1710      end)
1711    end
1712
1713    -- check that behavior is identical with and without "lockmarks"
1714    test_lockmarks ''
1715    test_lockmarks 'lockmarks'
1716
1717    teardown(function()
1718      os.remove 'Xtest-reload'
1719      os.remove 'Xtest-undofile'
1720      os.remove '.Xtest-undofile.un~'
1721    end)
1722  end
1723
1724  describe('(with verify) handles', function()
1725    do_both(true)
1726  end)
1727
1728  describe('(without verify) handles', function()
1729    do_both(false)
1730  end)
1731end)
1732
1733describe('nvim_buf_attach on_detach', function()
1734  it('does not SEGFAULT when accessing window buffer info #14998', function()
1735    local code = function()
1736      local buf = vim.api.nvim_create_buf(false, false)
1737
1738      vim.cmd 'split'
1739      vim.api.nvim_win_set_buf(0, buf)
1740
1741      vim.api.nvim_buf_attach(buf, false, {
1742        on_detach = function(_, buf0)
1743          vim.fn.tabpagebuflist()
1744          vim.fn.win_findbuf(buf0)
1745        end,
1746      })
1747    end
1748
1749    exec_lua(code)
1750    command('q!')
1751    n.assert_alive()
1752
1753    exec_lua(code)
1754    command('bd!')
1755    n.assert_alive()
1756  end)
1757
1758  it('no invalid lnum error for closed memline #31251', function()
1759    eq(vim.NIL, exec_lua('return _G.did_detach'))
1760    exec_lua(function()
1761      vim.api.nvim_buf_set_lines(0, 0, -1, false, { '' })
1762      local bufname = 'buf2'
1763      local buf = vim.api.nvim_create_buf(false, true)
1764      vim.api.nvim_buf_set_name(buf, bufname)
1765      vim.bo[buf].bufhidden = 'wipe'
1766      vim.cmd('vertical diffsplit ' .. bufname)
1767      vim.api.nvim_buf_attach(0, false, {
1768        on_detach = function()
1769          vim.cmd('redraw')
1770          _G.did_detach = true
1771        end,
1772      })
1773      vim.cmd.bdelete()
1774    end)
1775    eq(true, exec_lua('return _G.did_detach'))
1776  end)
1777
1778  it('called before buf_freeall autocommands', function()
1779    exec_lua(function()
1780      vim.api.nvim_create_autocmd({ 'BufUnload', 'BufDelete', 'BufWipeout' }, {
1781        callback = function(ev)
1782          table.insert(
1783            _G.events,
1784            ('%s: %d %s'):format(ev.event, ev.buf, tostring(vim.api.nvim_buf_is_loaded(ev.buf)))
1785          )
1786        end,
1787      })
1788      function _G.on_detach(_, b)
1789        table.insert(
1790          _G.events,
1791          ('on_detach: %d %s'):format(b, tostring(vim.api.nvim_buf_is_loaded(b)))
1792        )
1793      end
1794      _G.events = {}
1795      vim.cmd 'new'
1796      vim.bo.bufhidden = 'wipe'
1797      vim.api.nvim_buf_attach(0, false, { on_detach = _G.on_detach })
1798      vim.cmd 'quit!'
1799    end)
1800
1801    eq(
1802      { 'on_detach: 2 true', 'BufUnload: 2 true', 'BufDelete: 2 true', 'BufWipeout: 2 true' },
1803      exec_lua('return _G.events')
1804    )
1805    eq(false, api.nvim_buf_is_valid(2))
1806
1807    exec_lua(function()
1808      _G.events = {}
1809      local buf = vim.api.nvim_create_buf(false, true)
1810      vim.api.nvim_buf_attach(buf, false, { on_detach = _G.on_detach })
1811      vim.api.nvim_buf_delete(buf, { force = true })
1812    end)
1813
1814    -- Was unlisted, so no BufDelete.
1815    eq(
1816      { 'on_detach: 3 true', 'BufUnload: 3 true', 'BufWipeout: 3 true' },
1817      exec_lua('return _G.events')
1818    )
1819    eq(false, api.nvim_buf_is_valid(3))
1820
1821    exec_lua(function()
1822      _G.events = {}
1823      vim.api.nvim_buf_attach(1, false, { on_detach = _G.on_detach })
1824      vim.api.nvim_create_autocmd('BufUnload', {
1825        buf = 1,
1826        once = true,
1827        callback = function()
1828          vim.api.nvim_buf_attach(1, false, {
1829            on_detach = function(...)
1830              vim.fn.bufload(1) -- Leaks the memfile it were to run inside free_buffer_stuff.
1831              return _G.on_detach(...)
1832            end,
1833          })
1834          table.insert(_G.events, 'local BufUnload')
1835        end,
1836      })
1837      vim.cmd 'edit asdf' -- Reuses buffer 1.
1838    end)
1839
1840    -- on_detach shouldn't run after autocommands when reusing a buffer (in free_buffer_stuff), even
1841    -- if those autocommands registered it, as curbuf may be in a semi-unloaded state at that point.
1842    eq({
1843      'on_detach: 1 true',
1844      'BufUnload: 1 true',
1845      'local BufUnload',
1846      'BufDelete: 1 true',
1847      'BufWipeout: 1 true',
1848    }, exec_lua('return _G.events'))
1849
1850    exec_lua(function()
1851      _G.events = {}
1852      vim.api.nvim_buf_attach(0, false, { on_detach = _G.on_detach })
1853      vim.cmd 'edit'
1854    end)
1855
1856    -- Re-edit buffer; on_detach is called.
1857    eq({ 'on_detach: 1 true', 'BufUnload: 1 true' }, exec_lua('return _G.events'))
1858    eq(true, api.nvim_buf_is_valid(1))
1859
1860    exec_lua(function()
1861      vim.cmd '%bwipeout!'
1862      vim.bo.modified = true
1863      _G.events = {}
1864      vim.api.nvim_buf_attach(0, false, { on_detach = _G.on_detach })
1865      vim.api.nvim_buf_delete(0, { force = true })
1866    end)
1867
1868    -- on_detach must still be first when wiping the last buffer if it's listed and non-reusable.
1869    -- Previously: BufUnload → BufDelete → on_detach → BufWipeout.
1870    eq(
1871      { 'on_detach: 4 true', 'BufUnload: 4 true', 'BufDelete: 4 true', 'BufWipeout: 4 false' },
1872      exec_lua('return _G.events')
1873    )
1874  end)
1875
1876  it('disallows splitting', function()
1877    command('new | setlocal bufhidden=wipe')
1878    local buf = api.nvim_get_current_buf()
1879    exec_lua(function()
1880      vim.api.nvim_buf_attach(0, false, {
1881        on_detach = function()
1882          -- Used to allow opening more views into a closing buffer, resulting in open windows to an
1883          -- unloaded buffer.
1884          vim.cmd [=[execute "normal! \<C-W>s"]=]
1885        end,
1886      })
1887    end)
1888    matches('E1159: Cannot split a window when closing the buffer$', pcall_err(command, 'quit!'))
1889    eq({}, fn.win_findbuf(buf))
1890    eq(false, api.nvim_buf_is_valid(buf))
1891  end)
1892end)
1893
1894it('nvim_buf_attach from buf_freeall autocommands does not leak', function()
1895  exec_lua(function()
1896    local b = vim.api.nvim_create_buf(true, true)
1897    vim.api.nvim_create_autocmd('BufWipeout', {
1898      buf = b,
1899      once = true,
1900      callback = function()
1901        vim.api.nvim_buf_attach(b, false, {})
1902        _G.autocmd_fired = true
1903      end,
1904    })
1905    vim.api.nvim_buf_delete(b, { force = true })
1906  end)
1907  eq(true, exec_lua('return _G.autocmd_fired'))
1908end)