robin/laufey

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

robinmerge: 9a29622b3b00ddb601

master
83.1 KiB2970 linesraw
1local t = require('test.testutil')
2local n = require('test.functional.testnvim')()
3local Screen = require('test.functional.ui.screen')
4
5local describe, it, before_each = t.describe, t.it, t.before_each
6local clear = n.clear
7local command = n.command
8local eq = t.eq
9local eval = n.eval
10local expect = n.expect
11local feed = n.feed
12local insert = n.insert
13local fn = n.fn
14local api = n.api
15local matches = t.matches
16local ok = t.ok
17local retry = t.retry
18local source = n.source
19local poke_eventloop = n.poke_eventloop
20local sleep = vim.uv.sleep
21local testprg = n.testprg
22local assert_alive = n.assert_alive
23
24local default_text = [[
25  Inc substitution on
26  two lines
27]]
28
29local multiline_text = [[
30  1 2 3
31  A B C
32  4 5 6
33  X Y Z
34  7 8 9
35]]
36
37local multimatch_text = [[
38  a bdc eae a fgl lzia r
39  x
40]]
41
42local multibyte_text = [[
43 £ ¥ ѫѫ PEPPERS
44£ ¥ ѫfѫ
45 a£ ѫ¥KOL
46£ ¥  libm
47£ ¥
48]]
49
50local long_multiline_text = [[
51  1 2 3
52  A B C
53  4 5 6
54  X Y Z
55  7 8 9
56  K L M
57  a b c
58  d e f
59  q r s
60  x y z
61  £ m n
62  t œ ¥
63]]
64
65local function common_setup(screen, inccommand, text)
66  if screen then
67    command('syntax on')
68    command('set nohlsearch')
69    command('hi Substitute guifg=red guibg=yellow')
70
71    screen:add_extra_attr_ids {
72      [100] = { underline = true },
73      [101] = { underline = true, foreground = Screen.colors.SlateBlue, bold = true },
74      vis = { background = Screen.colors.LightGrey },
75    }
76  end
77
78  command('set inccommand=' .. (inccommand or ''))
79
80  if text then
81    insert(text)
82  end
83end
84
85describe(':substitute, inccommand=split interactivity', function()
86  before_each(function()
87    clear()
88    common_setup(nil, 'split', default_text)
89  end)
90
91  -- Test the tests: verify that the `1==bufnr('$')` assertion
92  -- in the "no preview" tests (below) actually means something.
93  it('previews interactive cmdline', function()
94    feed(':%s/tw/MO/g')
95    retry(nil, 1000, function()
96      eq(2, eval("bufnr('$')"))
97    end)
98  end)
99
100  it('no preview if invoked by a script', function()
101    source('%s/tw/MO/g')
102    poke_eventloop()
103    eq(1, eval("bufnr('$')"))
104    -- sanity check: assert the buffer state
105    expect(default_text:gsub('tw', 'MO'))
106  end)
107
108  it('no preview if invoked by feedkeys()', function()
109    -- in a script...
110    source([[:call feedkeys(":%s/tw/MO/g\<CR>")]])
111    -- or interactively...
112    feed([[:call feedkeys(":%s/bs/BUU/g\<lt>CR>")<CR>]])
113    eq(1, eval("bufnr('$')"))
114    -- sanity check: assert the buffer state
115    expect(default_text:gsub('tw', 'MO'):gsub('bs', 'BUU'))
116  end)
117end)
118
119describe(":substitute, 'inccommand' preserves", function()
120  before_each(clear)
121
122  it('listed buffers (:ls)', function()
123    local screen = Screen.new(30, 10)
124    common_setup(screen, 'split', 'ABC')
125
126    feed(':%s/AB/BA/')
127    poke_eventloop()
128    feed('<CR>')
129    feed(':ls<CR>')
130
131    screen:expect([[
132      BAC                           |
133      {1:~                             }|*3
134      {3:                              }|
135      :ls                           |
136        1 %a + "[No Name]"          |
137                line 1              |
138      {6:Press ENTER or type command to}|
139      {6: continue}^                     |
140    ]])
141  end)
142
143  it("'[ and '] marks #26439", function()
144    local screen = Screen.new(30, 10)
145    common_setup(screen, 'nosplit', ('abc\ndef\n'):rep(50))
146
147    feed('ggyG')
148    local X = api.nvim_get_vvar('maxcol')
149    eq({ 0, 1, 1, 0 }, fn.getpos("'["))
150    eq({ 0, 101, X, 0 }, fn.getpos("']"))
151
152    feed(":'[,']s/def/")
153    poke_eventloop()
154    eq({ 0, 1, 1, 0 }, fn.getpos("'["))
155    eq({ 0, 101, X, 0 }, fn.getpos("']"))
156
157    feed('DEF/g')
158    poke_eventloop()
159    eq({ 0, 1, 1, 0 }, fn.getpos("'["))
160    eq({ 0, 101, X, 0 }, fn.getpos("']"))
161
162    feed('<CR>')
163    expect(('abc\nDEF\n'):rep(50))
164  end)
165
166  for _, case in pairs { '', 'split', 'nosplit' } do
167    it('various delimiters (inccommand=' .. case .. ')', function()
168      insert(default_text)
169      command('set inccommand=' .. case)
170
171      local delims = { '/', '#', ';', '%', ',', '@', '!' }
172      for _, delim in pairs(delims) do
173        feed(':%s' .. delim .. 'lines' .. delim .. 'LINES' .. delim .. 'g')
174        poke_eventloop()
175        feed('<CR>')
176        expect([[
177          Inc substitution on
178          two LINES
179          ]])
180        command('undo')
181      end
182    end)
183  end
184
185  for _, case in pairs { '', 'split', 'nosplit' } do
186    it("'undolevels' (inccommand=" .. case .. ')', function()
187      command('set undolevels=139')
188      command('setlocal undolevels=34')
189      command('split') -- Show the buffer in multiple windows
190      command('set inccommand=' .. case)
191      insert('as')
192      feed(':%s/as/glork/')
193      poke_eventloop()
194      feed('<enter>')
195      eq(139, api.nvim_get_option_value('undolevels', { scope = 'global' }))
196      eq(34, api.nvim_get_option_value('undolevels', { buf = 0 }))
197    end)
198  end
199
200  for _, case in ipairs({ '', 'split', 'nosplit' }) do
201    it('empty undotree() (inccommand=' .. case .. ')', function()
202      command('set undolevels=1000')
203      command('set inccommand=' .. case)
204      local expected_undotree = eval('undotree()')
205
206      -- Start typing an incomplete :substitute command.
207      feed([[:%s/e/YYYY/g]])
208      poke_eventloop()
209      -- Cancel the :substitute.
210      feed([[<C-\><C-N>]])
211
212      -- The undo tree should be unchanged.
213      eq(expected_undotree, eval('undotree()'))
214      eq({}, eval('undotree()')['entries'])
215    end)
216  end
217
218  for _, case in ipairs({ '', 'split', 'nosplit' }) do
219    it('undotree() with branches (inccommand=' .. case .. ')', function()
220      command('set undolevels=1000')
221      command('set inccommand=' .. case)
222      -- Make some changes.
223      feed([[isome text 1<C-\><C-N>]])
224      feed([[osome text 2<C-\><C-N>]])
225      -- Add an undo branch.
226      feed([[u]])
227      -- More changes, more undo branches.
228      feed([[osome text 3<C-\><C-N>]])
229      feed([[AX<C-\><C-N>]])
230      feed([[...]])
231      feed([[uu]])
232      feed([[osome text 4<C-\><C-N>]])
233      feed([[u<C-R>u]])
234      feed([[osome text 5<C-\><C-N>]])
235      expect([[
236        some text 1
237        some text 3XX
238        some text 5]])
239      local expected_undotree = eval('undotree()')
240      eq(5, #expected_undotree['entries']) -- sanity
241
242      -- Start typing an incomplete :substitute command.
243      feed([[:%s/e/YYYY/g]])
244      poke_eventloop()
245      -- Cancel the :substitute.
246      feed([[<C-\><C-N>]])
247
248      -- The undo tree should be unchanged.
249      eq(expected_undotree, eval('undotree()'))
250    end)
251  end
252
253  for _, case in pairs { '', 'split', 'nosplit' } do
254    it('b:changedtick (inccommand=' .. case .. ')', function()
255      command('set inccommand=' .. case)
256      feed([[isome text 1<C-\><C-N>]])
257      feed([[osome text 2<C-\><C-N>]])
258      local expected_tick = eval('b:changedtick')
259      ok(expected_tick > 0)
260
261      expect([[
262        some text 1
263        some text 2]])
264      feed(':%s/e/XXX/')
265      poke_eventloop()
266
267      eq(expected_tick, eval('b:changedtick'))
268    end)
269  end
270
271  for _, case in ipairs({ '', 'split', 'nosplit' }) do
272    it('previous substitute string ~ (inccommand=' .. case .. ') #12109', function()
273      local screen = Screen.new(30, 10)
274      common_setup(screen, case, default_text)
275
276      feed(':%s/Inc/SUB<CR>')
277      expect([[
278        SUB substitution on
279        two lines
280        ]])
281
282      feed(':%s/line/')
283      poke_eventloop()
284      feed('~')
285      poke_eventloop()
286      feed('<CR>')
287      expect([[
288        SUB substitution on
289        two SUBs
290        ]])
291
292      feed(':%s/sti/')
293      poke_eventloop()
294      feed('~')
295      poke_eventloop()
296      feed('B')
297      poke_eventloop()
298      feed('<CR>')
299      expect([[
300        SUB subSUBBtution on
301        two SUBs
302        ]])
303
304      feed(':%s/ion/NEW<CR>')
305      expect([[
306        SUB subSUBBtutNEW on
307        two SUBs
308        ]])
309
310      feed(':%s/two/')
311      poke_eventloop()
312      feed('N')
313      poke_eventloop()
314      feed('~')
315      poke_eventloop()
316      feed('<CR>')
317      expect([[
318        SUB subSUBBtutNEW on
319        NNEW SUBs
320        ]])
321
322      feed(':%s/bS/')
323      poke_eventloop()
324      feed('~')
325      poke_eventloop()
326      feed('W')
327      poke_eventloop()
328      feed('<CR>')
329      expect([[
330        SUB suNNEWWUBBtutNEW on
331        NNEW SUBs
332        ]])
333    end)
334  end
335end)
336
337describe(":substitute, 'inccommand' preserves undo", function()
338  local cases = { '', 'split', 'nosplit' }
339
340  local substrings = {
341    { ':%s/', '1' },
342    { ':%s/', '1', '/' },
343    { ':%s/', '1', '/', '<bs>' },
344    { ':%s/', '1', '/', 'a' },
345    { ':%s/', '1', '/', 'a', '<bs>' },
346    { ':%s/', '1', '/', 'a', 'x' },
347    { ':%s/', '1', '/', 'a', 'x', '<bs>' },
348    { ':%s/', '1', '/', 'a', 'x', '<bs>', '<bs>' },
349    { ':%s/', '1', '/', 'a', 'x', '<bs>', '<bs>', '<bs>' },
350    { ':%s/', '1', '/', 'a', 'x', '/' },
351    { ':%s/', '1', '/', 'a', 'x', '/', '<bs>' },
352    { ':%s/', '1', '/', 'a', 'x', '/', '<bs>', '/' },
353    { ':%s/', '1', '/', 'a', 'x', '/', 'g' },
354    { ':%s/', '1', '/', 'a', 'x', '/', 'g', '<bs>' },
355    { ':%s/', '1', '/', 'a', 'x', '/', 'g', '<bs>', '<bs>' },
356  }
357
358  local function test_sub(substring, split, redoable)
359    command('bwipe!')
360    command('set inccommand=' .. split)
361
362    insert('1')
363    feed('o2<esc>')
364    command('undo')
365    feed('o3<esc>')
366    if redoable then
367      feed('o4<esc>')
368      command('undo')
369    end
370    for _, s in pairs(substring) do
371      feed(s)
372    end
373    poke_eventloop()
374    feed('<enter>')
375    command('undo')
376
377    feed('g-')
378    expect([[
379      1
380      2]])
381
382    feed('g+')
383    expect([[
384      1
385      3]])
386  end
387
388  local function test_notsub(substring, split, redoable)
389    command('bwipe!')
390    command('set inccommand=' .. split)
391
392    insert('1')
393    feed('o2<esc>')
394    command('undo')
395    feed('o3<esc>')
396    if redoable then
397      feed('o4<esc>')
398      command('undo')
399    end
400    for _, s in pairs(substring) do
401      feed(s)
402    end
403    poke_eventloop()
404    feed('<esc>')
405
406    feed('g-')
407    expect([[
408      1
409      2]])
410
411    feed('g+')
412    expect([[
413      1
414      3]])
415
416    if redoable then
417      feed('<c-r>')
418      expect([[
419        1
420        3
421        4]])
422    end
423  end
424
425  local function test_threetree(substring, split)
426    command('bwipe!')
427    command('set inccommand=' .. split)
428
429    insert('1')
430    feed('o2<esc>')
431    feed('o3<esc>')
432    feed('uu')
433    feed('oa<esc>')
434    feed('ob<esc>')
435    feed('uu')
436    feed('oA<esc>')
437    feed('oB<esc>')
438
439    -- This is the undo tree (x-Axis is timeline), we're at B now
440    --    ----------------A - B
441    --   /
442    --  | --------a - b
443    --  |/
444    --  1 - 2 - 3
445
446    feed('2u')
447    for _, s in pairs(substring) do
448      feed(s)
449      poke_eventloop()
450    end
451    feed('<esc>')
452    expect([[
453      1]])
454    feed('g-')
455    expect([[
456      ]])
457    feed('g+')
458    expect([[
459      1]])
460    feed('<c-r>')
461    expect([[
462      1
463      A]])
464
465    feed('g-') -- go to b
466    feed('2u')
467    for _, s in pairs(substring) do
468      feed(s)
469      poke_eventloop()
470    end
471    feed('<esc>')
472    feed('<c-r>')
473    expect([[
474      1
475      a]])
476
477    feed('g-') -- go to 3
478    feed('2u')
479    for _, s in pairs(substring) do
480      feed(s)
481      poke_eventloop()
482    end
483    feed('<esc>')
484    feed('<c-r>')
485    expect([[
486      1
487      2]])
488  end
489
490  before_each(clear)
491
492  it('at a non-leaf of the undo tree', function()
493    for _, case in pairs(cases) do
494      for _, str in pairs(substrings) do
495        for _, redoable in pairs({ true }) do
496          test_sub(str, case, redoable)
497        end
498      end
499    end
500  end)
501
502  it('at a leaf of the undo tree', function()
503    for _, case in pairs(cases) do
504      for _, str in pairs(substrings) do
505        for _, redoable in pairs({ false }) do
506          test_sub(str, case, redoable)
507        end
508      end
509    end
510  end)
511
512  it('when interrupting substitution', function()
513    for _, case in pairs(cases) do
514      for _, str in pairs(substrings) do
515        for _, redoable in pairs({ true, false }) do
516          test_notsub(str, case, redoable)
517        end
518      end
519    end
520  end)
521
522  it('in a complex undo scenario', function()
523    for _, case in pairs(cases) do
524      for _, str in pairs(substrings) do
525        test_threetree(str, case)
526      end
527    end
528  end)
529
530  it('with undolevels=0', function()
531    for _, case in pairs(cases) do
532      clear()
533      common_setup(nil, case, default_text)
534      command('set undolevels=0')
535
536      feed('1G0')
537      insert('X')
538      feed(':%s/tw/MO/')
539      poke_eventloop()
540      feed('<esc>')
541      command('undo')
542      expect(default_text)
543      command('undo')
544      expect(default_text:gsub('Inc', 'XInc'))
545      command('undo')
546
547      feed(':%s/tw/MO/g')
548      poke_eventloop()
549      feed('<CR>')
550      expect(default_text:gsub('tw', 'MO'))
551      command('undo')
552      expect(default_text)
553      command('undo')
554      expect(default_text:gsub('tw', 'MO'))
555    end
556  end)
557
558  it('with undolevels=1', function()
559    for _, case in pairs(cases) do
560      clear()
561      local screen = Screen.new(20, 10)
562      common_setup(screen, case, default_text)
563      screen:expect([[
564        Inc substitution on |
565        two lines           |
566        ^                    |
567        {1:~                   }|*6
568                            |
569      ]])
570      command('set undolevels=1')
571
572      feed('1G0')
573      insert('X')
574      feed('IY<esc>')
575      feed(':%s/tw/MO/')
576      poke_eventloop()
577      feed('<esc>')
578      feed('u')
579      expect(default_text:gsub('Inc', 'XInc'))
580      feed('u')
581      expect(default_text)
582
583      feed(':%s/tw/MO/g')
584      poke_eventloop()
585      feed('<enter>')
586      feed(':%s/MO/GO/g')
587      poke_eventloop()
588      feed('<enter>')
589      feed(':%s/GO/NO/g')
590      poke_eventloop()
591      feed('<enter>')
592      feed('u')
593      expect(default_text:gsub('tw', 'GO'))
594      feed('u')
595      expect(default_text:gsub('tw', 'MO'))
596      feed('u')
597
598      if case == 'split' then
599        screen:expect([[
600          Inc substitution on |
601          ^MOo lines           |
602                              |
603          {1:~                   }|*6
604          Already ...t change |
605        ]])
606      else
607        screen:expect([[
608          Inc substitution on |
609          ^MOo lines           |
610                              |
611          {1:~                   }|*6
612          Already ...t change |
613        ]])
614      end
615    end
616  end)
617
618  it('with undolevels=2', function()
619    for _, case in pairs(cases) do
620      clear()
621      local screen = Screen.new(20, 10)
622      common_setup(screen, case, default_text)
623      command('set undolevels=2')
624
625      feed('2GAx<esc>')
626      feed('Ay<esc>')
627      feed('Az<esc>')
628      feed(':%s/tw/AR')
629      poke_eventloop()
630      feed('<esc>')
631      feed('u')
632      expect(default_text:gsub('lines', 'linesxy'))
633      feed('u')
634      expect(default_text:gsub('lines', 'linesx'))
635      feed('u')
636      expect(default_text)
637      feed('u')
638
639      if case == 'split' then
640        screen:expect([[
641          Inc substitution on |
642          ^two lines           |
643                              |
644          {1:~                   }|*6
645          Already ...t change |
646        ]])
647      else
648        screen:expect([[
649          Inc substitution on |
650          ^two lines           |
651                              |
652          {1:~                   }|*6
653          Already ...t change |
654        ]])
655      end
656
657      feed(':%s/tw/MO/g')
658      poke_eventloop()
659      feed('<enter>')
660      feed(':%s/MO/GO/g')
661      poke_eventloop()
662      feed('<enter>')
663      feed(':%s/GO/NO/g')
664      poke_eventloop()
665      feed('<enter>')
666      feed(':%s/NO/LO/g')
667      poke_eventloop()
668      feed('<enter>')
669      feed('u')
670      expect(default_text:gsub('tw', 'NO'))
671      feed('u')
672      expect(default_text:gsub('tw', 'GO'))
673      feed('u')
674      expect(default_text:gsub('tw', 'MO'))
675      feed('u')
676
677      if case == 'split' then
678        screen:expect([[
679          Inc substitution on |
680          ^MOo lines           |
681                              |
682          {1:~                   }|*6
683          Already ...t change |
684        ]])
685      else
686        screen:expect([[
687          Inc substitution on |
688          ^MOo lines           |
689                              |
690          {1:~                   }|*6
691          Already ...t change |
692        ]])
693      end
694    end
695  end)
696
697  it('with undolevels=-1', function()
698    for _, case in pairs(cases) do
699      clear()
700      local screen = Screen.new(20, 10)
701      common_setup(screen, case, default_text)
702
703      command('set undolevels=-1')
704      feed(':%s/tw/MO/g')
705      poke_eventloop()
706      feed('<enter>')
707      feed('u')
708      if case == 'split' then
709        screen:expect([[
710          Inc substitution on |
711          ^MOo lines           |
712                              |
713          {1:~                   }|*6
714          Already ...t change |
715        ]])
716      else
717        screen:expect([[
718          Inc substitution on |
719          ^MOo lines           |
720                              |
721          {1:~                   }|*6
722          Already ...t change |
723        ]])
724      end
725
726      -- repeat with an interrupted substitution
727      clear()
728      screen = Screen.new(20, 10)
729      common_setup(screen, case, default_text)
730
731      command('set undolevels=-1')
732      feed('1G')
733      feed('IL<esc>')
734      feed(':%s/tw/MO/g')
735      poke_eventloop()
736      feed('<esc>')
737      feed('u')
738
739      screen:expect([[
740        ^LInc substitution on|
741        two lines           |
742                            |
743        {1:~                   }|*6
744        Already ...t change |
745      ]])
746    end
747  end)
748end)
749
750describe(':substitute, inccommand=split', function()
751  local screen
752
753  before_each(function()
754    clear()
755    screen = Screen.new(30, 15)
756    common_setup(screen, 'split', default_text .. default_text)
757  end)
758
759  it("preserves 'modified' buffer flag", function()
760    command('set nomodified')
761    feed(':%s/tw')
762    screen:expect([[
763      Inc substitution on           |
764      {20:tw}o lines                     |
765      Inc substitution on           |
766      {20:tw}o lines                     |
767                                    |
768      {3:[No Name]                     }|
769      |2| {20:tw}o lines                 |
770      |4| {20:tw}o lines                 |
771      {1:~                             }|*5
772      {2:[Preview]                     }|
773      :%s/tw^                        |
774    ]])
775    feed([[<C-\><C-N>]]) -- Cancel the :substitute command.
776    eq(0, eval('&modified'))
777  end)
778
779  it('shows preview when cmd modifiers are present', function()
780    -- one modifier
781    feed(':keeppatterns %s/tw/to')
782    screen:expect { any = [[{20:to}o lines]] }
783    feed('<Esc>')
784    screen:expect { any = [[two lines]] }
785
786    -- multiple modifiers
787    feed(':keeppatterns silent %s/tw/to')
788    screen:expect { any = [[{20:to}o lines]] }
789    feed('<Esc>')
790    screen:expect { any = [[two lines]] }
791
792    -- non-modifier prefix
793    feed(':silent tabedit %s/tw/to')
794    screen:expect([[
795      Inc substitution on           |
796      two lines                     |
797      Inc substitution on           |
798      two lines                     |
799                                    |
800      {1:~                             }|*9
801      :silent tabedit %s/tw/to^      |
802    ]])
803    feed('<Esc>')
804
805    -- leading colons
806    feed(':::%s/tw/to')
807    screen:expect { any = [[{20:to}o lines]] }
808    feed('<Esc>')
809    screen:expect { any = [[two lines]] }
810  end)
811
812  it('ignores new-window modifiers when splitting the preview window', function()
813    -- one modifier
814    feed(':topleft %s/tw/to')
815    screen:expect([[
816      Inc substitution on           |
817      {20:to}o lines                     |
818      Inc substitution on           |
819      {20:to}o lines                     |
820                                    |
821      {3:[No Name] [+]                 }|
822      |2| {20:to}o lines                 |
823      |4| {20:to}o lines                 |
824      {1:~                             }|*5
825      {2:[Preview]                     }|
826      :topleft %s/tw/to^             |
827    ]])
828    feed('<Esc>')
829    screen:expect { any = [[two lines]] }
830
831    -- multiple modifiers
832    feed(':topleft vert %s/tw/to')
833    screen:expect([[
834      Inc substitution on           |
835      {20:to}o lines                     |
836      Inc substitution on           |
837      {20:to}o lines                     |
838                                    |
839      {3:[No Name] [+]                 }|
840      |2| {20:to}o lines                 |
841      |4| {20:to}o lines                 |
842      {1:~                             }|*5
843      {2:[Preview]                     }|
844      :topleft vert %s/tw/to^        |
845    ]])
846    feed('<Esc>')
847    screen:expect { any = [[two lines]] }
848  end)
849
850  it('shows split window when typing the pattern', function()
851    feed(':%s/tw')
852    screen:expect([[
853      Inc substitution on           |
854      {20:tw}o lines                     |
855      Inc substitution on           |
856      {20:tw}o lines                     |
857                                    |
858      {3:[No Name] [+]                 }|
859      |2| {20:tw}o lines                 |
860      |4| {20:tw}o lines                 |
861      {1:~                             }|*5
862      {2:[Preview]                     }|
863      :%s/tw^                        |
864    ]])
865  end)
866
867  it('shows preview with empty replacement', function()
868    feed(':%s/tw/')
869    screen:expect([[
870      Inc substitution on           |
871      o lines                       |
872      Inc substitution on           |
873      o lines                       |
874                                    |
875      {3:[No Name] [+]                 }|
876      |2| o lines                   |
877      |4| o lines                   |
878      {1:~                             }|*5
879      {2:[Preview]                     }|
880      :%s/tw/^                       |
881    ]])
882
883    feed('x')
884    screen:expect([[
885      Inc substitution on           |
886      {20:x}o lines                      |
887      Inc substitution on           |
888      {20:x}o lines                      |
889                                    |
890      {3:[No Name] [+]                 }|
891      |2| {20:x}o lines                  |
892      |4| {20:x}o lines                  |
893      {1:~                             }|*5
894      {2:[Preview]                     }|
895      :%s/tw/x^                      |
896    ]])
897
898    feed('<bs>')
899    screen:expect([[
900      Inc substitution on           |
901      o lines                       |
902      Inc substitution on           |
903      o lines                       |
904                                    |
905      {3:[No Name] [+]                 }|
906      |2| o lines                   |
907      |4| o lines                   |
908      {1:~                             }|*5
909      {2:[Preview]                     }|
910      :%s/tw/^                       |
911    ]])
912  end)
913
914  it('shows split window when typing replacement', function()
915    feed(':%s/tw/XX')
916    screen:expect([[
917      Inc substitution on           |
918      {20:XX}o lines                     |
919      Inc substitution on           |
920      {20:XX}o lines                     |
921                                    |
922      {3:[No Name] [+]                 }|
923      |2| {20:XX}o lines                 |
924      |4| {20:XX}o lines                 |
925      {1:~                             }|*5
926      {2:[Preview]                     }|
927      :%s/tw/XX^                     |
928    ]])
929  end)
930
931  it('does not show split window for :s/', function()
932    feed('2gg')
933    feed(':s/tw')
934    screen:expect([[
935      Inc substitution on           |
936      {20:tw}o lines                     |
937      Inc substitution on           |
938      two lines                     |
939                                    |
940      {1:~                             }|*9
941      :s/tw^                         |
942    ]])
943  end)
944
945  it("'hlsearch' is active, 'cursorline' is not", function()
946    command('set hlsearch cursorline')
947    feed('gg')
948
949    -- Assert that 'cursorline' is active.
950    screen:expect([[
951      {21:^Inc substitution on           }|
952      two lines                     |
953      Inc substitution on           |
954      two lines                     |
955                                    |
956      {1:~                             }|*9
957                                    |
958    ]])
959
960    feed(':%s/tw')
961    -- 'cursorline' is NOT active during preview.
962    screen:expect([[
963      Inc substitution on           |
964      {20:tw}o lines                     |
965      Inc substitution on           |
966      {20:tw}o lines                     |
967                                    |
968      {3:[No Name] [+]                 }|
969      |2| {20:tw}o lines                 |
970      |4| {20:tw}o lines                 |
971      {1:~                             }|*5
972      {2:[Preview]                     }|
973      :%s/tw^                        |
974    ]])
975  end)
976
977  it('highlights the replacement text', function()
978    feed('ggO')
979    feed('M     M       M<esc>')
980    feed(':%s/M/123/g')
981    screen:expect([[
982      {20:123}     {20:123}       {20:123}         |
983      Inc substitution on           |
984      two lines                     |
985      Inc substitution on           |
986      two lines                     |
987      {3:[No Name] [+]                 }|
988      |1| {20:123}     {20:123}       {20:123}     |
989      {1:~                             }|*6
990      {2:[Preview]                     }|
991      :%s/M/123/g^                   |
992    ]])
993  end)
994
995  it("highlights nothing when there's no match", function()
996    feed('gg')
997    feed(':%s/Inx')
998    screen:expect([[
999      Inc substitution on           |
1000      two lines                     |
1001      Inc substitution on           |
1002      two lines                     |
1003                                    |
1004      {3:[No Name] [+]                 }|
1005                                    |
1006      {1:~                             }|*6
1007      {2:[Preview]                     }|
1008      :%s/Inx^                       |
1009    ]])
1010  end)
1011
1012  it('previews correctly when previewhight is small', function()
1013    command('set cwh=3')
1014    command('set hls')
1015    feed('ggdG')
1016    insert(string.rep('abc abc abc\n', 20))
1017    feed(':%s/abc/MMM/g')
1018    screen:expect([[
1019      {20:MMM} {20:MMM} {20:MMM}                   |*9
1020      {3:[No Name] [+]                 }|
1021      | 1| {20:MMM} {20:MMM} {20:MMM}              |
1022      | 2| {20:MMM} {20:MMM} {20:MMM}              |
1023      | 3| {20:MMM} {20:MMM} {20:MMM}              |
1024      {2:[Preview]                     }|
1025      :%s/abc/MMM/g^                 |
1026    ]])
1027  end)
1028
1029  it('actually replaces text', function()
1030    feed(':%s/tw/XX/g')
1031    poke_eventloop()
1032    feed('<Enter>')
1033
1034    screen:expect([[
1035      Inc substitution on           |
1036      XXo lines                     |
1037      Inc substitution on           |
1038      ^XXo lines                     |
1039                                    |
1040      {1:~                             }|*9
1041      :%s/tw/XX/g                   |
1042    ]])
1043  end)
1044
1045  it('shows correct line numbers with many lines', function()
1046    feed('gg')
1047    feed('2yy')
1048    feed('2000p')
1049    command('1,1000s/tw/BB/g')
1050
1051    feed(':%s/tw/X')
1052    screen:expect([[
1053      Inc substitution on           |
1054      BBo lines                     |
1055      Inc substitution on           |
1056      {20:X}o lines                      |
1057      Inc substitution on           |
1058      {3:[No Name] [+]                 }|
1059      |1001| {20:X}o lines               |
1060      |1003| {20:X}o lines               |
1061      |1005| {20:X}o lines               |
1062      |1007| {20:X}o lines               |
1063      |1009| {20:X}o lines               |
1064      |1011| {20:X}o lines               |
1065      |1013| {20:X}o lines               |
1066      {2:[Preview]                     }|
1067      :%s/tw/X^                      |
1068    ]])
1069  end)
1070
1071  it('does not spam the buffer numbers', function()
1072    -- The preview buffer is re-used (unless user deleted it), so buffer numbers
1073    -- will not increase on each keystroke.
1074    feed(':%s/tw/Xo/g')
1075    -- Delete and re-type the g a few times.
1076    feed('<BS>')
1077    poke_eventloop()
1078    feed('g')
1079    poke_eventloop()
1080    feed('<BS>')
1081    poke_eventloop()
1082    feed('g')
1083    poke_eventloop()
1084    feed('<CR>')
1085    poke_eventloop()
1086    feed(':vs tmp<enter>')
1087    eq(3, fn.bufnr('$'))
1088  end)
1089
1090  it('works with the n flag', function()
1091    feed(':%s/tw/Mix/n')
1092    poke_eventloop()
1093    feed('<Enter>')
1094    screen:expect([[
1095      Inc substitution on           |
1096      two lines                     |
1097      Inc substitution on           |
1098      two lines                     |
1099      ^                              |
1100      {1:~                             }|*9
1101      2 matches on 2 lines          |
1102    ]])
1103  end)
1104
1105  it("deactivates if 'redrawtime' is exceeded #5602", function()
1106    -- prevent redraws from 'incsearch'
1107    api.nvim_set_option_value('incsearch', false, {})
1108    -- Assert that 'inccommand' is ENABLED initially.
1109    eq('split', eval('&inccommand'))
1110    -- Set 'redrawtime' to minimal value, to ensure timeout is triggered.
1111    command('set redrawtime=1 nowrap')
1112    -- Load a big file.
1113    command('silent edit! test/functional/fixtures/bigfile_oneline.txt')
1114    -- Start :substitute with a slow pattern.
1115    feed([[:%s/B.*N/x]])
1116    poke_eventloop()
1117
1118    -- Assert that 'inccommand' is DISABLED in cmdline mode.
1119    eq('', eval('&inccommand'))
1120    -- Assert that preview cleared (or never manifested).
1121    screen:expect([[
1122      0000;<control>;Cc;0;BN;;;;;N;N|
1123      2F923;CJK COMPATIBILITY IDEOGR|
1124      2F924;CJK COMPATIBILITY IDEOGR|
1125      2F925;CJK COMPATIBILITY IDEOGR|
1126      2F926;CJK COMPATIBILITY IDEOGR|
1127      2F927;CJK COMPATIBILITY IDEOGR|
1128      2F928;CJK COMPATIBILITY IDEOGR|
1129      2F929;CJK COMPATIBILITY IDEOGR|
1130      2F92A;CJK COMPATIBILITY IDEOGR|
1131      2F92B;CJK COMPATIBILITY IDEOGR|
1132      2F92C;CJK COMPATIBILITY IDEOGR|
1133      2F92D;CJK COMPATIBILITY IDEOGR|
1134      2F92E;CJK COMPATIBILITY IDEOGR|
1135      2F92F;CJK COMPATIBILITY IDEOGR|
1136      :%s/B.*N/x^                    |
1137    ]])
1138
1139    -- Assert that 'inccommand' is again ENABLED after leaving cmdline mode.
1140    feed([[<C-\><C-N>]])
1141    eq('split', eval('&inccommand'))
1142  end)
1143
1144  it('time limit is enforced while matching a single line #40773', function()
1145    -- prevent redraws from 'incsearch'
1146    api.nvim_set_option_value('incsearch', false, {})
1147    -- Assert that 'inccommand' is ENABLED initially.
1148    eq('split', eval('&inccommand'))
1149    -- Set 'redrawtime' to minimal value, to ensure timeout is triggered.
1150    command('set redrawtime=1 nowrap')
1151    -- Prepare the text
1152    api.nvim_buf_set_lines(0, 0, -1, true, { ('aaaaaaaa/'):rep(6) .. ('b'):rep(200) })
1153    feed([[:%s/.\+\/\(.\+\)\+ft\/\1]])
1154    screen:expect({ any = vim.pesc([[:%s/.\+\/\(.\+\)\+ft\/\1]]) })
1155    -- Assert that 'inccommand' is DISABLED in cmdline mode.
1156    eq('', eval('&inccommand'))
1157    -- Assert that 'inccommand' is again ENABLED after leaving cmdline mode.
1158    feed([[<C-\><C-N>]])
1159    eq('split', eval('&inccommand'))
1160  end)
1161
1162  it("deactivates if 'foldexpr' is slow #9557", function()
1163    insert([[
1164      a
1165      a
1166      a
1167      a
1168      a
1169      a
1170      a
1171      a
1172    ]])
1173    source([[
1174      function! Slowfold(lnum)
1175        sleep 5m
1176        return a:lnum % 3
1177      endfun
1178    ]])
1179    command('set redrawtime=1 inccommand=split')
1180    command('set foldmethod=expr foldexpr=Slowfold(v:lnum)')
1181    feed(':%s/a/bcdef')
1182
1183    -- Assert that 'inccommand' is DISABLED in cmdline mode.
1184    retry(nil, nil, function()
1185      eq('', eval('&inccommand'))
1186    end)
1187
1188    -- Assert that 'inccommand' is again ENABLED after leaving cmdline mode.
1189    feed([[<C-\><C-N>]])
1190    retry(nil, nil, function()
1191      eq('split', eval('&inccommand'))
1192    end)
1193  end)
1194
1195  it('clears preview if non-previewable command is edited #5585', function()
1196    feed('gg')
1197    -- Put a non-previewable command in history.
1198    feed(":echo 'foo'<CR>")
1199    -- Start an incomplete :substitute command.
1200    feed(':1,2s/t/X')
1201
1202    screen:expect([[
1203      Inc subs{20:X}itution on           |
1204      {20:X}wo lines                     |
1205      Inc substitution on           |
1206      two lines                     |
1207                                    |
1208      {3:[No Name] [+]                 }|
1209      |1| Inc subs{20:X}itution on       |
1210      |2| {20:X}wo lines                 |
1211      {1:~                             }|*5
1212      {2:[Preview]                     }|
1213      :1,2s/t/X^                     |
1214    ]])
1215
1216    -- Select the previous command.
1217    feed('<C-P>')
1218    -- Assert that preview was cleared.
1219    screen:expect([[
1220      Inc substitution on           |
1221      two lines                     |
1222      Inc substitution on           |
1223      two lines                     |
1224                                    |
1225      {1:~                             }|*9
1226      :echo 'foo'^                   |
1227    ]])
1228  end)
1229
1230  it([[preview changes correctly with c_CTRL-R_= and c_CTRL-\_e]], function()
1231    feed('gg')
1232    feed(':1,2s/t/X')
1233    screen:expect([[
1234      Inc subs{20:X}itution on           |
1235      {20:X}wo lines                     |
1236      Inc substitution on           |
1237      two lines                     |
1238                                    |
1239      {3:[No Name] [+]                 }|
1240      |1| Inc subs{20:X}itution on       |
1241      |2| {20:X}wo lines                 |
1242      {1:~                             }|*5
1243      {2:[Preview]                     }|
1244      :1,2s/t/X^                     |
1245    ]])
1246
1247    feed([[<C-R>='Y']])
1248    -- preview should be unchanged during c_CTRL-R_= editing
1249    screen:expect([[
1250      Inc subs{20:X}itution on           |
1251      {20:X}wo lines                     |
1252      Inc substitution on           |
1253      two lines                     |
1254                                    |
1255      {3:[No Name] [+]                 }|
1256      |1| Inc subs{20:X}itution on       |
1257      |2| {20:X}wo lines                 |
1258      {1:~                             }|*5
1259      {2:[Preview]                     }|
1260      ={26:'Y'}^                          |
1261    ]])
1262
1263    feed('<CR>')
1264    -- preview should be changed by the result of the expression
1265    screen:expect([[
1266      Inc subs{20:XY}itution on          |
1267      {20:XY}wo lines                    |
1268      Inc substitution on           |
1269      two lines                     |
1270                                    |
1271      {3:[No Name] [+]                 }|
1272      |1| Inc subs{20:XY}itution on      |
1273      |2| {20:XY}wo lines                |
1274      {1:~                             }|*5
1275      {2:[Preview]                     }|
1276      :1,2s/t/XY^                    |
1277    ]])
1278
1279    feed([[<C-\>e'echo']])
1280    -- preview should be unchanged during c_CTRL-\_e editing
1281    screen:expect([[
1282      Inc subs{20:XY}itution on          |
1283      {20:XY}wo lines                    |
1284      Inc substitution on           |
1285      two lines                     |
1286                                    |
1287      {3:[No Name] [+]                 }|
1288      |1| Inc subs{20:XY}itution on      |
1289      |2| {20:XY}wo lines                |
1290      {1:~                             }|*5
1291      {2:[Preview]                     }|
1292      ={26:'echo'}^                       |
1293    ]])
1294
1295    feed('<CR>')
1296    -- preview should be cleared if command is changed to a non-previewable one
1297    screen:expect([[
1298      Inc substitution on           |
1299      two lines                     |
1300      Inc substitution on           |
1301      two lines                     |
1302                                    |
1303      {1:~                             }|*9
1304      :echo^                         |
1305    ]])
1306  end)
1307end)
1308
1309describe('inccommand=nosplit', function()
1310  local screen
1311
1312  before_each(function()
1313    clear()
1314    screen = Screen.new(20, 10)
1315    common_setup(screen, 'nosplit', default_text .. default_text)
1316  end)
1317
1318  it('works with :smagic, :snomagic', function()
1319    command('set hlsearch')
1320    insert('Line *.3.* here')
1321
1322    feed(':%smagic/3.*/X') -- start :smagic command
1323    screen:expect([[
1324      Inc substitution on |
1325      two lines           |
1326      Inc substitution on |
1327      two lines           |
1328      Line *.{20:X}            |
1329      {1:~                   }|*4
1330      :%smagic/3.*/X^      |
1331    ]])
1332
1333    feed([[<C-\><C-N>]]) -- cancel
1334    feed(':%snomagic/3.*/X') -- start :snomagic command
1335    screen:expect([[
1336      Inc substitution on |
1337      two lines           |
1338      Inc substitution on |
1339      two lines           |
1340      Line *.{20:X} here       |
1341      {1:~                   }|*4
1342      :%snomagic/3.*/X^    |
1343    ]])
1344  end)
1345
1346  it('shows preview when cmd modifiers are present', function()
1347    -- one modifier
1348    feed(':keeppatterns %s/tw/to')
1349    screen:expect { any = [[{20:to}o lines]] }
1350    feed('<Esc>')
1351    screen:expect { any = [[two lines]] }
1352
1353    -- multiple modifiers
1354    feed(':keeppatterns silent %s/tw/to')
1355    screen:expect { any = [[{20:to}o lines]] }
1356    feed('<Esc>')
1357    screen:expect { any = [[two lines]] }
1358
1359    -- non-modifier prefix
1360    feed(':silent tabedit %s/tw/to')
1361    screen:expect([[
1362      Inc substitution on |
1363      two lines           |
1364      Inc substitution on |
1365      two lines           |
1366                          |
1367      {1:~                   }|*2
1368      {3:                    }|
1369      :silent tabedit %s/t|
1370      w/to^                |
1371    ]])
1372  end)
1373
1374  it('does not show window after toggling :set inccommand', function()
1375    feed(':%s/tw/OKOK')
1376    feed('<Esc>')
1377    command('set icm=split')
1378    feed(':%s/tw/OKOK')
1379    feed('<Esc>')
1380    command('set icm=nosplit')
1381    feed(':%s/tw/OKOK')
1382    poke_eventloop()
1383    screen:expect([[
1384      Inc substitution on |
1385      {20:OKOK}o lines         |
1386      Inc substitution on |
1387      {20:OKOK}o lines         |
1388                          |
1389      {1:~                   }|*4
1390      :%s/tw/OKOK^         |
1391    ]])
1392  end)
1393
1394  it('never shows preview buffer', function()
1395    command('set hlsearch')
1396
1397    feed(':%s/tw')
1398    screen:expect([[
1399      Inc substitution on |
1400      {20:tw}o lines           |
1401      Inc substitution on |
1402      {20:tw}o lines           |
1403                          |
1404      {1:~                   }|*4
1405      :%s/tw^              |
1406    ]])
1407
1408    feed('/BM')
1409    screen:expect([[
1410      Inc substitution on |
1411      {20:BM}o lines           |
1412      Inc substitution on |
1413      {20:BM}o lines           |
1414                          |
1415      {1:~                   }|*4
1416      :%s/tw/BM^           |
1417    ]])
1418
1419    feed('/')
1420    screen:expect([[
1421      Inc substitution on |
1422      {20:BM}o lines           |
1423      Inc substitution on |
1424      {20:BM}o lines           |
1425                          |
1426      {1:~                   }|*4
1427      :%s/tw/BM/^          |
1428    ]])
1429
1430    feed('<enter>')
1431    screen:expect([[
1432      Inc substitution on |
1433      BMo lines           |
1434      Inc substitution on |
1435      ^BMo lines           |
1436                          |
1437      {1:~                   }|*4
1438      :%s/tw/BM/          |
1439    ]])
1440  end)
1441
1442  it('clears preview if non-previewable command is edited', function()
1443    -- Put a non-previewable command in history.
1444    feed(":echo 'foo'<CR>")
1445    -- Start an incomplete :substitute command.
1446    feed(':1,2s/t/X')
1447
1448    screen:expect([[
1449      Inc subs{20:X}itution on |
1450      {20:X}wo lines           |
1451      Inc substitution on |
1452      two lines           |
1453                          |
1454      {1:~                   }|*4
1455      :1,2s/t/X^           |
1456    ]])
1457
1458    -- Select the previous command.
1459    feed('<C-P>')
1460    -- Assert that preview was cleared.
1461    screen:expect([[
1462      Inc substitution on |
1463      two lines           |
1464      Inc substitution on |
1465      two lines           |
1466                          |
1467      {1:~                   }|*4
1468      :echo 'foo'^         |
1469    ]])
1470  end)
1471
1472  it('does not execute trailing bar-separated commands #7494', function()
1473    feed(':%s/two/three/g|q!')
1474    screen:expect([[
1475      Inc substitution on |
1476      {20:three} lines         |
1477      Inc substitution on |
1478      {20:three} lines         |
1479                          |
1480      {1:~                   }|*4
1481      :%s/two/three/g|q!^  |
1482    ]])
1483    eq(eval('v:null'), eval('v:exiting'))
1484  end)
1485
1486  it('does not break bar-separated command #8796', function()
1487    source([[
1488      function! F()
1489        if v:false | return | endif
1490      endfun
1491    ]])
1492    command('call timer_start(10, {-> F()}, {"repeat":-1})')
1493    feed(':%s/')
1494    sleep(20) -- Allow some timer activity.
1495    screen:expect([[
1496      Inc substitution on |
1497      two lines           |
1498      Inc substitution on |
1499      two lines           |
1500                          |
1501      {1:~                   }|*4
1502      :%s/^                |
1503    ]])
1504  end)
1505end)
1506
1507describe(":substitute, 'inccommand' with a failing expression", function()
1508  local screen
1509  local cases = { '', 'split', 'nosplit' }
1510
1511  local function refresh(case)
1512    clear()
1513    screen = Screen.new(20, 10)
1514    common_setup(screen, case, default_text)
1515  end
1516
1517  it('in the pattern does nothing', function()
1518    for _, case in pairs(cases) do
1519      refresh(case)
1520      command('set inccommand=' .. case)
1521      feed(':silent! %s/tw\\(/LARD/')
1522      poke_eventloop()
1523      feed('<enter>')
1524      expect(default_text)
1525    end
1526  end)
1527
1528  it('in the replacement deletes the matches', function()
1529    for _, case in pairs(cases) do
1530      refresh(case)
1531      local replacements = { "\\='LARD", '\\=xx_novar__xx' }
1532
1533      for _, repl in pairs(replacements) do
1534        command('set inccommand=' .. case)
1535        feed(':silent! %s/tw/' .. repl .. '/')
1536        poke_eventloop()
1537        feed('<enter>')
1538        expect(default_text:gsub('tw', ''))
1539        command('undo')
1540      end
1541    end
1542  end)
1543
1544  it('in the range does not error #5912', function()
1545    for _, case in pairs(cases) do
1546      refresh(case)
1547      feed(':100s/')
1548
1549      screen:expect([[
1550        Inc substitution on |
1551        two lines           |
1552                            |
1553        {1:~                   }|*6
1554        :100s/^              |
1555      ]])
1556
1557      feed('<enter>')
1558      screen:expect([[
1559        Inc substitution on |
1560        two lines           |
1561        ^                    |
1562        {1:~                   }|*6
1563        {9:E16: Invalid range}  |
1564      ]])
1565    end
1566  end)
1567end)
1568
1569describe("'inccommand' and :cnoremap", function()
1570  local cases = { '', 'split', 'nosplit' }
1571  local screen
1572
1573  local function refresh(case, visual)
1574    clear()
1575    screen = visual and Screen.new(80, 10) or nil
1576    common_setup(screen, case, default_text)
1577  end
1578
1579  it('work with remapped characters', function()
1580    for _, case in pairs(cases) do
1581      refresh(case)
1582      local cmd = '%s/lines/LINES/g'
1583
1584      for i = 1, string.len(cmd) do
1585        local c = string.sub(cmd, i, i)
1586        command('cnoremap ' .. c .. ' ' .. c)
1587      end
1588
1589      feed(':' .. cmd)
1590      poke_eventloop()
1591      feed('<CR>')
1592      expect([[
1593        Inc substitution on
1594        two LINES
1595        ]])
1596    end
1597  end)
1598
1599  it('work when mappings move the cursor', function()
1600    for _, case in pairs(cases) do
1601      refresh(case)
1602      command('cnoremap ,S LINES/<left><left><left><left><left><left>')
1603
1604      feed(':%s/lines/')
1605      poke_eventloop()
1606      feed(',S')
1607      poke_eventloop()
1608      feed('or three <enter>')
1609      poke_eventloop()
1610      expect([[
1611        Inc substitution on
1612        two or three LINES
1613        ]])
1614
1615      command('cnoremap ;S /X/<left><left><left>')
1616      feed(':%s/')
1617      poke_eventloop()
1618      feed(';S')
1619      poke_eventloop()
1620      feed('I<enter>')
1621      expect([[
1622        Xnc substitution on
1623        two or three LXNES
1624        ]])
1625
1626      command('cnoremap ,T //Y/<left><left><left>')
1627      feed(':%s')
1628      poke_eventloop()
1629      feed(',T')
1630      poke_eventloop()
1631      feed('X<enter>')
1632      expect([[
1633        Ync substitution on
1634        two or three LYNES
1635        ]])
1636
1637      command('cnoremap ;T s//Z/<left><left><left>')
1638      feed(':%')
1639      poke_eventloop()
1640      feed(';T')
1641      poke_eventloop()
1642      feed('Y<enter>')
1643      expect([[
1644        Znc substitution on
1645        two or three LZNES
1646        ]])
1647    end
1648  end)
1649
1650  it('still works with a broken mapping', function()
1651    for _, case in pairs(cases) do
1652      refresh(case, true)
1653      command("cnoremap <expr> x execute('bwipeout!')[-1].'x'")
1654
1655      api.nvim_set_vvar('errmsg', '')
1656      feed(':%s/tw/tox')
1657      -- error thrown b/c of the mapping
1658      matches('^E565:', api.nvim_get_vvar('errmsg'))
1659
1660      feed('<enter>')
1661      expect([[
1662      Inc substitution on
1663      toxo lines
1664      ]])
1665    end
1666  end)
1667
1668  it('work when temporarily moving the cursor', function()
1669    for _, case in pairs(cases) do
1670      refresh(case)
1671      command("cnoremap <expr> x cursor(1, 1)[-1].'x'")
1672
1673      feed(':%s/tw/tox')
1674      poke_eventloop()
1675      feed('/g<enter>')
1676      expect(default_text:gsub('tw', 'tox'))
1677    end
1678  end)
1679
1680  it("work when a mapping disables 'inccommand'", function()
1681    for _, case in pairs(cases) do
1682      refresh(case)
1683      command("cnoremap <expr> x execute('set inccommand=')[-1]")
1684
1685      feed(':%s/tw/tox')
1686      poke_eventloop()
1687      feed('a/g<enter>')
1688      expect(default_text:gsub('tw', 'toa'))
1689    end
1690  end)
1691
1692  it('work with a complex mapping', function()
1693    for _, case in pairs(cases) do
1694      refresh(case)
1695      source([[cnoremap x <C-\>eextend(g:, {'fo': getcmdline()})
1696      \.fo<CR><C-c>:new<CR>:bw!<CR>:<C-r>=remove(g:, 'fo')<CR>x]])
1697
1698      feed(':%s/tw/tox')
1699      poke_eventloop()
1700      feed('/<enter>')
1701      expect(default_text:gsub('tw', 'tox'))
1702    end
1703  end)
1704end)
1705
1706describe("'inccommand' autocommands", function()
1707  before_each(clear)
1708
1709  -- keys are events to be tested
1710  -- values are arrays like
1711  --    { open = { 1 }, close = { 2, 3} }
1712  -- which would mean that during the test below the event fires for
1713  -- buffer 1 when opening the preview window, and for buffers 2 and 3
1714  -- when closing the preview window
1715  local eventsExpected = {
1716    BufAdd = {},
1717    BufDelete = {},
1718    BufEnter = {},
1719    BufFilePost = {},
1720    BufFilePre = {},
1721    BufHidden = {},
1722    BufLeave = {},
1723    BufNew = {},
1724    BufNewFile = {},
1725    BufRead = {},
1726    BufReadCmd = {},
1727    BufReadPre = {},
1728    BufUnload = {},
1729    BufWinEnter = {},
1730    BufWinLeave = {},
1731    BufWipeout = {},
1732    BufWrite = {},
1733    BufWriteCmd = {},
1734    BufWritePost = {},
1735    Syntax = {},
1736    FileType = {},
1737    WinEnter = {},
1738    WinLeave = {},
1739    CmdwinEnter = {},
1740    CmdwinLeave = {},
1741  }
1742
1743  local function bufferlist(q)
1744    local s = ''
1745    for _, buffer in pairs(q) do
1746      s = s .. ', ' .. tostring(buffer)
1747    end
1748    return s
1749  end
1750
1751  -- fill the table with default values
1752  for event, _ in pairs(eventsExpected) do
1753    eventsExpected[event].open = eventsExpected[event].open or {}
1754    eventsExpected[event].close = eventsExpected[event].close or {}
1755  end
1756
1757  local function register_autocmd(event)
1758    api.nvim_set_var(event .. '_fired', {})
1759    command('autocmd ' .. event .. ' * call add(g:' .. event .. "_fired, expand('<abuf>'))")
1760  end
1761
1762  it('are not fired when splitting', function()
1763    common_setup(nil, 'split', default_text)
1764
1765    local eventsObserved = {}
1766    for event, _ in pairs(eventsExpected) do
1767      eventsObserved[event] = {}
1768      register_autocmd(event)
1769    end
1770
1771    feed(':%s/tw')
1772
1773    for event, _ in pairs(eventsExpected) do
1774      eventsObserved[event].open = api.nvim_get_var(event .. '_fired')
1775      api.nvim_set_var(event .. '_fired', {})
1776    end
1777
1778    feed('/<enter>')
1779
1780    for event, _ in pairs(eventsExpected) do
1781      eventsObserved[event].close = api.nvim_get_var(event .. '_fired')
1782    end
1783
1784    for event, _ in pairs(eventsExpected) do
1785      eq(
1786        event .. bufferlist(eventsExpected[event].open),
1787        event .. bufferlist(eventsObserved[event].open)
1788      )
1789      eq(
1790        event .. bufferlist(eventsExpected[event].close),
1791        event .. bufferlist(eventsObserved[event].close)
1792      )
1793    end
1794  end)
1795end)
1796
1797describe("'inccommand' split windows", function()
1798  local screen
1799  local function refresh()
1800    clear()
1801    screen = Screen.new(40, 30)
1802    common_setup(screen, 'split', default_text)
1803  end
1804
1805  it('work after more splits', function()
1806    refresh()
1807
1808    feed('gg')
1809    command('vsplit')
1810    command('split')
1811    feed(':%s/tw')
1812    screen:expect([[
1813      Inc substitution on │Inc substitution on|
1814      {20:tw}o lines           │{20:tw}o lines          |
1815                          │                   |
1816      {1:~                   }│{1:~                  }|*11
1817      {3:[No Name] [+]       }│{1:~                  }|
1818      Inc substitution on │{1:~                  }|
1819      {20:tw}o lines           │{1:~                  }|
1820                          │{1:~                  }|
1821      {1:~                   }│{1:~                  }|*2
1822      {2:[No Name] [+]        [No Name] [+]      }|
1823      |2| {20:tw}o lines                           |
1824      {1:~                                       }|*6
1825      {2:[Preview]                               }|
1826      :%s/tw^                                  |
1827    ]])
1828
1829    feed('<esc>')
1830    command('only')
1831    command('split')
1832    command('vsplit')
1833
1834    feed(':%s/tw')
1835    screen:expect([[
1836      Inc substitution on │Inc substitution on|
1837      {20:tw}o lines           │{20:tw}o lines          |
1838                          │                   |
1839      {1:~                   }│{1:~                  }|*11
1840      {3:[No Name] [+]        }{2:[No Name] [+]      }|
1841      Inc substitution on                     |
1842      {20:tw}o lines                               |
1843                                              |
1844      {1:~                                       }|*2
1845      {2:[No Name] [+]                           }|
1846      |2| {20:tw}o lines                           |
1847      {1:~                                       }|*6
1848      {2:[Preview]                               }|
1849      :%s/tw^                                  |
1850    ]])
1851  end)
1852
1853  local settings = {
1854    'splitbelow',
1855    'splitright',
1856    'noequalalways',
1857    'equalalways eadirection=ver',
1858    'equalalways eadirection=hor',
1859    'equalalways eadirection=both',
1860  }
1861
1862  it('are not affected by various settings', function()
1863    for _, setting in pairs(settings) do
1864      refresh()
1865      command('set ' .. setting)
1866
1867      feed(':%s/tw')
1868
1869      screen:expect([[
1870        Inc substitution on                     |
1871        {20:tw}o lines                               |
1872                                                |
1873        {1:~                                       }|*17
1874        {3:[No Name] [+]                           }|
1875        |2| {20:tw}o lines                           |
1876        {1:~                                       }|*6
1877        {2:[Preview]                               }|
1878        :%s/tw^                                  |
1879      ]])
1880    end
1881  end)
1882
1883  it("don't open if there's not enough room", function()
1884    refresh()
1885    screen:try_resize(40, 3)
1886    feed('gg:%s/tw')
1887    screen:expect([[
1888      Inc substitution on                     |
1889      {20:tw}o lines                               |
1890      :%s/tw^                                  |
1891    ]])
1892  end)
1893end)
1894
1895describe("'inccommand' with 'gdefault'", function()
1896  before_each(function()
1897    clear()
1898  end)
1899
1900  it('does not lock up #7244', function()
1901    common_setup(nil, 'nosplit', '{')
1902    command('set gdefault')
1903    feed(':s/{\\n')
1904    eq({ mode = 'c', blocking = false }, api.nvim_get_mode())
1905    feed('/A<Enter>')
1906    expect('A')
1907    eq({ mode = 'n', blocking = false }, api.nvim_get_mode())
1908  end)
1909
1910  it('with multiline text and range, does not lock up #7244', function()
1911    common_setup(nil, 'nosplit', '{\n\n{')
1912    command('set gdefault')
1913    feed(':%s/{\\n')
1914    eq({ mode = 'c', blocking = false }, api.nvim_get_mode())
1915    feed('/A<Enter>')
1916    expect('A\nA')
1917    eq({ mode = 'n', blocking = false }, api.nvim_get_mode())
1918  end)
1919
1920  it('does not crash on zero-width matches #7485', function()
1921    common_setup(nil, 'split', default_text)
1922    command('set gdefault')
1923    feed('gg')
1924    feed('Vj')
1925    feed(':s/\\%V')
1926    eq({ mode = 'c', blocking = false }, api.nvim_get_mode())
1927    feed('<Esc>')
1928    eq({ mode = 'n', blocking = false }, api.nvim_get_mode())
1929  end)
1930
1931  it('removes highlights after abort for a zero-width match', function()
1932    local screen = Screen.new(30, 5)
1933    common_setup(screen, 'nosplit', default_text)
1934    command('set gdefault')
1935
1936    feed(':%s/\\%1c/a/')
1937    screen:expect([[
1938      {20:a}Inc substitution on          |
1939      {20:a}two lines                    |
1940      {20:a}                             |
1941      {1:~                             }|
1942      :%s/\%1c/a/^                   |
1943    ]])
1944
1945    feed('<Esc>')
1946    screen:expect([[
1947      Inc substitution on           |
1948      two lines                     |
1949      ^                              |
1950      {1:~                             }|
1951                                    |
1952    ]])
1953  end)
1954end)
1955
1956describe(':substitute', function()
1957  local screen
1958  before_each(function()
1959    clear()
1960    screen = Screen.new(30, 15)
1961  end)
1962
1963  it('inccommand=split, highlights multiline substitutions', function()
1964    common_setup(screen, 'split', multiline_text)
1965    feed('gg')
1966
1967    feed(':%s/2\\_.*X')
1968    screen:expect([[
1969      1 {20:2 3}                         |
1970      {20:A B C}                         |
1971      {20:4 5 6}                         |
1972      {20:X} Y Z                         |
1973      7 8 9                         |
1974      {3:[No Name] [+]                 }|
1975      |1| 1 {20:2 3}                     |
1976      |2|{20: A B C}                     |
1977      |3|{20: 4 5 6}                     |
1978      |4|{20: X} Y Z                     |
1979      {1:~                             }|*3
1980      {2:[Preview]                     }|
1981      :%s/2\_.*X^                    |
1982    ]])
1983
1984    feed('/MMM')
1985    screen:expect([[
1986      1 {20:MMM} Y Z                     |
1987      7 8 9                         |
1988                                    |
1989      {1:~                             }|*2
1990      {3:[No Name] [+]                 }|
1991      |1| 1 {20:MMM} Y Z                 |
1992      {1:~                             }|*6
1993      {2:[Preview]                     }|
1994      :%s/2\_.*X/MMM^                |
1995    ]])
1996
1997    feed('\\rK\\rLLL')
1998    screen:expect([[
1999      1 {20:MMM}                         |
2000      {20:K}                             |
2001      {20:LLL} Y Z                       |
2002      7 8 9                         |
2003                                    |
2004      {3:[No Name] [+]                 }|
2005      |1| 1 {20:MMM}                     |
2006      |2|{20: K}                         |
2007      |3|{20: LLL} Y Z                   |
2008      {1:~                             }|*4
2009      {2:[Preview]                     }|
2010      :%s/2\_.*X/MMM\rK\rLLL^        |
2011    ]])
2012  end)
2013
2014  it('inccommand=nosplit, highlights multiline substitutions', function()
2015    common_setup(screen, 'nosplit', multiline_text)
2016    feed('gg')
2017
2018    feed(':%s/2\\_.*X/MMM')
2019    screen:expect([[
2020      1 {20:MMM} Y Z                     |
2021      7 8 9                         |
2022                                    |
2023      {1:~                             }|*11
2024      :%s/2\_.*X/MMM^                |
2025    ]])
2026
2027    feed('\\rK\\rLLL')
2028    screen:expect([[
2029      1 {20:MMM}                         |
2030      {20:K}                             |
2031      {20:LLL} Y Z                       |
2032      7 8 9                         |
2033                                    |
2034      {1:~                             }|*9
2035      :%s/2\_.*X/MMM\rK\rLLL^        |
2036    ]])
2037  end)
2038
2039  it('inccommand=split, highlights multiple matches on a line', function()
2040    common_setup(screen, 'split', multimatch_text)
2041    command('set gdefault')
2042    feed('gg')
2043
2044    feed(':%s/a/XLK')
2045    screen:expect([[
2046      {20:XLK} bdc e{20:XLK}e {20:XLK} fgl lzi{20:XLK} r|
2047      x                             |
2048                                    |
2049      {1:~                             }|*2
2050      {3:[No Name] [+]                 }|
2051      |1| {20:XLK} bdc e{20:XLK}e {20:XLK} fgl lzi{20:X}|
2052      {20:LK} r                          |
2053      {1:~                             }|*5
2054      {2:[Preview]                     }|
2055      :%s/a/XLK^                     |
2056    ]])
2057  end)
2058
2059  it('inccommand=nosplit, highlights multiple matches on a line', function()
2060    common_setup(screen, 'nosplit', multimatch_text)
2061    command('set gdefault')
2062    feed('gg')
2063
2064    feed(':%s/a/XLK')
2065    screen:expect([[
2066      {20:XLK} bdc e{20:XLK}e {20:XLK} fgl lzi{20:XLK} r|
2067      x                             |
2068                                    |
2069      {1:~                             }|*11
2070      :%s/a/XLK^                     |
2071    ]])
2072  end)
2073
2074  it('inccommand=split, with \\zs', function()
2075    common_setup(screen, 'split', multiline_text)
2076    feed('gg')
2077
2078    feed(':%s/[0-9]\\n\\zs[A-Z]/OKO')
2079    screen:expect([[
2080      {20:OKO} B C                       |
2081      4 5 6                         |
2082      {20:OKO} Y Z                       |
2083      7 8 9                         |
2084                                    |
2085      {3:[No Name] [+]                 }|
2086      |1| 1 2 3                     |
2087      |2| {20:OKO} B C                   |
2088      |3| 4 5 6                     |
2089      |4| {20:OKO} Y Z                   |
2090      {1:~                             }|*3
2091      {2:[Preview]                     }|
2092      :%s/[0-9]\n\zs[A-Z]/OKO^       |
2093    ]])
2094  end)
2095
2096  it('inccommand=nosplit, with \\zs', function()
2097    common_setup(screen, 'nosplit', multiline_text)
2098    feed('gg')
2099
2100    feed(':%s/[0-9]\\n\\zs[A-Z]/OKO')
2101    screen:expect([[
2102      1 2 3                         |
2103      {20:OKO} B C                       |
2104      4 5 6                         |
2105      {20:OKO} Y Z                       |
2106      7 8 9                         |
2107                                    |
2108      {1:~                             }|*8
2109      :%s/[0-9]\n\zs[A-Z]/OKO^       |
2110    ]])
2111  end)
2112
2113  it('inccommand=split, substitutions of different length', function()
2114    common_setup(screen, 'split', 'T T123 T2T TTT T090804\nx')
2115
2116    feed(':%s/T\\([0-9]\\+\\)/\\1\\1/g')
2117    screen:expect([[
2118      T {20:123123} {20:22}T TTT {20:090804090804} |
2119      x                             |
2120      {1:~                             }|*3
2121      {3:[No Name] [+]                 }|
2122      |1| T {20:123123} {20:22}T TTT {20:090804090}|
2123      {20:804}                           |
2124      {1:~                             }|*5
2125      {2:[Preview]                     }|
2126      :%s/T\([0-9]\+\)/\1\1/g^       |
2127    ]])
2128  end)
2129
2130  it('inccommand=nosplit, substitutions of different length', function()
2131    common_setup(screen, 'nosplit', 'T T123 T2T TTT T090804\nx')
2132
2133    feed(':%s/T\\([0-9]\\+\\)/\\1\\1/g')
2134    screen:expect([[
2135      T {20:123123} {20:22}T TTT {20:090804090804} |
2136      x                             |
2137      {1:~                             }|*12
2138      :%s/T\([0-9]\+\)/\1\1/g^       |
2139    ]])
2140  end)
2141
2142  it('inccommand=split, contraction of lines', function()
2143    local text = [[
2144      T T123 T T123 T2T TT T23423424
2145      x
2146      afa Q
2147      adf la;lkd R
2148      alx
2149      ]]
2150
2151    common_setup(screen, 'split', text)
2152    feed(':%s/[QR]\\n')
2153    screen:expect([[
2154      afa {20:Q}                         |
2155      adf la;lkd {20:R}                  |
2156      alx                           |
2157                                    |
2158      {1:~                             }|
2159      {3:[No Name] [+]                 }|
2160      |3| afa {20:Q}                     |
2161      |4|{20: }adf la;lkd {20:R}              |
2162      |5|{20: }alx                       |
2163      {1:~                             }|*4
2164      {2:[Preview]                     }|
2165      :%s/[QR]\n^                    |
2166    ]])
2167
2168    feed('/KKK')
2169    screen:expect([[
2170      T T123 T T123 T2T TT T23423424|
2171      x                             |
2172      afa {20:KKK}adf la;lkd {20:KKK}alx      |
2173                                    |
2174      {1:~                             }|
2175      {3:[No Name] [+]                 }|
2176      |3| afa {20:KKK}adf la;lkd {20:KKK}alx  |
2177      {1:~                             }|*6
2178      {2:[Preview]                     }|
2179      :%s/[QR]\n/KKK^                |
2180    ]])
2181  end)
2182
2183  it('inccommand=nosplit, contraction of lines', function()
2184    local text = [[
2185      T T123 T T123 T2T TT T23423424
2186      x
2187      afa Q
2188      adf la;lkd R
2189      alx
2190      ]]
2191
2192    common_setup(screen, 'nosplit', text)
2193    feed(':%s/[QR]\\n/KKK')
2194    screen:expect([[
2195      T T123 T T123 T2T TT T23423424|
2196      x                             |
2197      afa {20:KKK}adf la;lkd {20:KKK}alx      |
2198                                    |
2199      {1:~                             }|*10
2200      :%s/[QR]\n/KKK^                |
2201    ]])
2202  end)
2203
2204  it('inccommand=split, contraction of two subsequent NL chars', function()
2205    local text = [[
2206      AAA AA
2207
2208      BBB BB
2209
2210      CCC CC
2211
2212]]
2213
2214    -- This used to crash, but more than 20 highlight entries are required
2215    -- to reproduce it (so that the marktree has multiple nodes)
2216    common_setup(screen, 'split', string.rep(text, 10))
2217    feed(':%s/\\n\\n/<c-v><c-m>/g')
2218    screen:expect {
2219      grid = [[
2220      CCC CC                        |
2221      AAA AA                        |
2222      BBB BB                        |
2223      CCC CC                        |
2224                                    |
2225      {3:[No Name] [+]                 }|
2226      | 1| AAA AA                   |
2227      | 2|{20: }BBB BB                   |
2228      | 3|{20: }CCC CC                   |
2229      | 4|{20: }AAA AA                   |
2230      | 5|{20: }BBB BB                   |
2231      | 6|{20: }CCC CC                   |
2232      | 7|{20: }AAA AA                   |
2233      {2:[Preview]                     }|
2234      :%s/\n\n/{18:^M}/g^                 |
2235    ]],
2236    }
2237    assert_alive()
2238  end)
2239
2240  it('inccommand=nosplit, contraction of two subsequent NL chars', function()
2241    local text = [[
2242      AAA AA
2243
2244      BBB BB
2245
2246      CCC CC
2247
2248]]
2249
2250    common_setup(screen, 'nosplit', string.rep(text, 10))
2251    feed(':%s/\\n\\n/<c-v><c-m>/g')
2252    screen:expect {
2253      grid = [[
2254      CCC CC                        |
2255      AAA AA                        |
2256      BBB BB                        |
2257      CCC CC                        |
2258      AAA AA                        |
2259      BBB BB                        |
2260      CCC CC                        |
2261      AAA AA                        |
2262      BBB BB                        |
2263      CCC CC                        |
2264      AAA AA                        |
2265      BBB BB                        |
2266      CCC CC                        |
2267                                    |
2268      :%s/\n\n/{18:^M}/g^                 |
2269    ]],
2270    }
2271    assert_alive()
2272  end)
2273
2274  it('inccommand=split, multibyte text', function()
2275    common_setup(screen, 'split', multibyte_text)
2276    feed(':%s/£.*ѫ/X¥¥')
2277    screen:expect([[
2278       a{20:X¥¥}¥KOL                     |
2279      £ ¥  libm                     |
2280      £ ¥                           |
2281                                    |
2282      {1:~                             }|
2283      {3:[No Name] [+]                 }|
2284      |1|  {20:X¥¥} PEPPERS              |
2285      |2| {20:X¥¥}                       |
2286      |3|  a{20:X¥¥}¥KOL                 |
2287      {1:~                             }|*4
2288      {2:[Preview]                     }|
2289      :%s/£.*ѫ/X¥¥^                  |
2290    ]])
2291
2292    feed('\\ra££   ¥')
2293    screen:expect([[
2294       a{20:X¥¥}                         |
2295      {20:a££   ¥}¥KOL                   |
2296      £ ¥  libm                     |
2297      £ ¥                           |
2298                                    |
2299      {3:[No Name] [+]                 }|
2300      |1|  {20:X¥¥}                      |
2301      |2|{20: a££   ¥} PEPPERS           |
2302      |3| {20:X¥¥}                       |
2303      |4|{20: a££   ¥}                   |
2304      |5|  a{20:X¥¥}                     |
2305      |6|{20: a££   ¥}¥KOL               |
2306      {1:~                             }|
2307      {2:[Preview]                     }|
2308      :%s/£.*ѫ/X¥¥\ra££   ¥^         |
2309    ]])
2310  end)
2311
2312  it('inccommand=nosplit, multibyte text', function()
2313    common_setup(screen, 'nosplit', multibyte_text)
2314    feed(':%s/£.*ѫ/X¥¥')
2315    screen:expect([[
2316       {20:X¥¥} PEPPERS                  |
2317      {20:X¥¥}                           |
2318       a{20:X¥¥}¥KOL                     |
2319      £ ¥  libm                     |
2320      £ ¥                           |
2321                                    |
2322      {1:~                             }|*8
2323      :%s/£.*ѫ/X¥¥^                  |
2324    ]])
2325
2326    feed('\\ra££   ¥')
2327    screen:expect([[
2328       {20:X¥¥}                          |
2329      {20:a££   ¥} PEPPERS               |
2330      {20:X¥¥}                           |
2331      {20:a££   ¥}                       |
2332       a{20:X¥¥}                         |
2333      {20:a££   ¥}¥KOL                   |
2334      £ ¥  libm                     |
2335      £ ¥                           |
2336                                    |
2337      {1:~                             }|*5
2338      :%s/£.*ѫ/X¥¥\ra££   ¥^         |
2339    ]])
2340  end)
2341
2342  it('inccommand=split, small cmdwinheight', function()
2343    common_setup(screen, 'split', long_multiline_text)
2344    command('set cmdwinheight=2')
2345
2346    feed(':%s/[a-z]')
2347    screen:expect([[
2348      X Y Z                         |
2349      7 8 9                         |
2350      K L M                         |
2351      {20:a} b c                         |
2352      {20:d} e f                         |
2353      {20:q} r s                         |
2354      {20:x} y z                         |
2355      £ {20:m} n                         |
2356      {20:t} œ ¥                         |
2357                                    |
2358      {3:[No Name] [+]                 }|
2359      | 7| {20:a} b c                    |
2360      | 8| {20:d} e f                    |
2361      {2:[Preview]                     }|
2362      :%s/[a-z]^                     |
2363    ]])
2364
2365    feed('/JLKR £')
2366    screen:expect([[
2367      X Y Z                         |
2368      7 8 9                         |
2369      K L M                         |
2370      {20:JLKR £} b c                    |
2371      {20:JLKR £} e f                    |
2372      {20:JLKR £} r s                    |
2373      {20:JLKR £} y z                    |
2374      £ {20:JLKR £} n                    |
2375      {20:JLKR £} œ ¥                    |
2376                                    |
2377      {3:[No Name] [+]                 }|
2378      | 7| {20:JLKR £} b c               |
2379      | 8| {20:JLKR £} e f               |
2380      {2:[Preview]                     }|
2381      :%s/[a-z]/JLKR £^              |
2382    ]])
2383
2384    feed('\\rѫ ab   \\rXXXX')
2385    screen:expect([[
2386      7 8 9                         |
2387      K L M                         |
2388      {20:JLKR £}                        |
2389      {20:ѫ ab   }                       |
2390      {20:XXXX} b c                      |
2391      {20:JLKR £}                        |
2392      {20:ѫ ab   }                       |
2393      {20:XXXX} e f                      |
2394      {20:JLKR £}                        |
2395      {20:ѫ ab   }                       |
2396      {3:[No Name] [+]                 }|
2397      | 7| {20:JLKR £}                   |
2398      {3:                              }|
2399      :%s/[a-z]/JLKR £\rѫ ab   \rXXX|
2400      X^                             |
2401    ]])
2402  end)
2403
2404  it('inccommand=split, large cmdwinheight', function()
2405    common_setup(screen, 'split', long_multiline_text)
2406    command('set cmdwinheight=11')
2407
2408    feed(':%s/. .$')
2409    screen:expect([[
2410      t {20:œ ¥}                         |
2411      {3:[No Name] [+]                 }|
2412      | 1| 1 {20:2 3}                    |
2413      | 2| A {20:B C}                    |
2414      | 3| 4 {20:5 6}                    |
2415      | 4| X {20:Y Z}                    |
2416      | 5| 7 {20:8 9}                    |
2417      | 6| K {20:L M}                    |
2418      | 7| a {20:b c}                    |
2419      | 8| d {20:e f}                    |
2420      | 9| q {20:r s}                    |
2421      |10| x {20:y z}                    |
2422      |11| £ {20:m n}                    |
2423      {2:[Preview]                     }|
2424      :%s/. .$^                      |
2425    ]])
2426
2427    feed('/ YYY')
2428    screen:expect([[
2429      t {20: YYY}                        |
2430      {3:[No Name] [+]                 }|
2431      | 1| 1 {20: YYY}                   |
2432      | 2| A {20: YYY}                   |
2433      | 3| 4 {20: YYY}                   |
2434      | 4| X {20: YYY}                   |
2435      | 5| 7 {20: YYY}                   |
2436      | 6| K {20: YYY}                   |
2437      | 7| a {20: YYY}                   |
2438      | 8| d {20: YYY}                   |
2439      | 9| q {20: YYY}                   |
2440      |10| x {20: YYY}                   |
2441      |11| £ {20: YYY}                   |
2442      {2:[Preview]                     }|
2443      :%s/. .$/ YYY^                 |
2444    ]])
2445
2446    feed('\\r KKK')
2447    screen:expect([[
2448      a {20: YYY}                        |
2449      {3:[No Name] [+]                 }|
2450      | 1| 1 {20: YYY}                   |
2451      | 2|{20:  KKK}                     |
2452      | 3| A {20: YYY}                   |
2453      | 4|{20:  KKK}                     |
2454      | 5| 4 {20: YYY}                   |
2455      | 6|{20:  KKK}                     |
2456      | 7| X {20: YYY}                   |
2457      | 8|{20:  KKK}                     |
2458      | 9| 7 {20: YYY}                   |
2459      |10|{20:  KKK}                     |
2460      |11| K {20: YYY}                   |
2461      {2:[Preview]                     }|
2462      :%s/. .$/ YYY\r KKK^           |
2463    ]])
2464  end)
2465
2466  it('inccommand=split, lookaround', function()
2467    common_setup(screen, 'split', 'something\neverything\nsomeone')
2468    feed([[:%s/\(some\)\@<lt>=thing/one/]])
2469    screen:expect([[
2470      some{20:one}                       |
2471      everything                    |
2472      someone                       |
2473      {1:~                             }|*2
2474      {3:[No Name] [+]                 }|
2475      |1| some{20:one}                   |
2476      {1:~                             }|*6
2477      {2:[Preview]                     }|
2478      :%s/\(some\)\@<=thing/one/^    |
2479    ]])
2480
2481    feed('<C-c>')
2482    feed('gg')
2483    poke_eventloop()
2484    feed([[:%s/\(some\)\@<lt>!thing/one/]])
2485    screen:expect([[
2486      something                     |
2487      every{20:one}                      |
2488      someone                       |
2489      {1:~                             }|*2
2490      {3:[No Name] [+]                 }|
2491      |2| every{20:one}                  |
2492      {1:~                             }|*6
2493      {2:[Preview]                     }|
2494      :%s/\(some\)\@<!thing/one/^    |
2495    ]])
2496
2497    feed([[<C-c>]])
2498    poke_eventloop()
2499    feed([[:%s/some\(thing\)\@=/every/]])
2500    screen:expect([[
2501      {20:every}thing                    |
2502      everything                    |
2503      someone                       |
2504      {1:~                             }|*2
2505      {3:[No Name] [+]                 }|
2506      |1| {20:every}thing                |
2507      {1:~                             }|*6
2508      {2:[Preview]                     }|
2509      :%s/some\(thing\)\@=/every/^   |
2510    ]])
2511
2512    feed([[<C-c>]])
2513    poke_eventloop()
2514    feed([[:%s/some\(thing\)\@!/every/]])
2515    screen:expect([[
2516      something                     |
2517      everything                    |
2518      {20:every}one                      |
2519      {1:~                             }|*2
2520      {3:[No Name] [+]                 }|
2521      |3| {20:every}one                  |
2522      {1:~                             }|*6
2523      {2:[Preview]                     }|
2524      :%s/some\(thing\)\@!/every/^   |
2525    ]])
2526  end)
2527
2528  it("doesn't prompt to swap cmd range", function()
2529    screen:try_resize(50, 8) -- wide to avoid hit-enter prompt
2530    common_setup(screen, 'split', default_text)
2531    feed(':2,1s/tw/MO/g')
2532
2533    -- substitution preview should have been made, without prompting
2534    screen:expect([[
2535      {20:MO}o lines                                         |
2536      {3:[No Name] [+]                                     }|
2537      |2| {20:MO}o lines                                     |
2538      {1:~                                                 }|*3
2539      {2:[Preview]                                         }|
2540      :2,1s/tw/MO/g^                                     |
2541    ]])
2542
2543    -- but should be prompted on hitting enter
2544    feed('<CR>')
2545    screen:expect([[
2546      {20:MO}o lines                                         |
2547      {3:[No Name] [+]                                     }|
2548      |2| {20:MO}o lines                                     |
2549      {1:~                                                 }|*3
2550      {2:[Preview]                                         }|
2551      {6:Backwards range given, OK to swap (y/n)?}^          |
2552    ]])
2553
2554    feed('y')
2555    screen:expect([[
2556      Inc substitution on                               |
2557      ^MOo lines                                         |
2558                                                        |
2559      {1:~                                                 }|*4
2560      {6:Backwards range given, OK to swap (y/n)?}y         |
2561    ]])
2562  end)
2563end)
2564
2565it(':substitute with inccommand during :terminal activity', function()
2566  if t.skip_fragile(t.pending) then
2567    return
2568  end
2569  retry(2, 40000, function()
2570    clear()
2571    local screen = Screen.new(30, 15)
2572
2573    command('set cmdwinheight=3')
2574    feed(([[:terminal "%s" REP 5000 xxx<cr>]]):format(testprg('shell-test')))
2575    command('file term')
2576    feed('G') -- Follow :terminal output.
2577    command('new')
2578    common_setup(screen, 'split', 'foo bar baz\nbar baz fox\nbar foo baz')
2579    command('wincmd =')
2580
2581    feed('gg')
2582    feed(':%s/foo/ZZZ')
2583    sleep(20) -- Allow some terminal activity.
2584    poke_eventloop()
2585    screen:sleep(0)
2586    screen:expect_unchanged()
2587  end)
2588end)
2589
2590it(':substitute with inccommand, timer-induced :redraw #9777', function()
2591  clear()
2592  local screen = Screen.new(30, 12)
2593  command('set cmdwinheight=3')
2594  command('call timer_start(10, {-> execute("redraw")}, {"repeat":-1})')
2595  command('call timer_start(10, {-> execute("redrawstatus")}, {"repeat":-1})')
2596  common_setup(screen, 'split', 'foo bar baz\nbar baz fox\nbar foo baz')
2597
2598  feed('gg')
2599  feed(':%s/foo/ZZZ')
2600  sleep(20) -- Allow some timer activity.
2601  screen:expect([[
2602    {20:ZZZ} bar baz                   |
2603    bar baz fox                   |
2604    bar {20:ZZZ} baz                   |
2605    {1:~                             }|*3
2606    {3:[No Name] [+]                 }|
2607    |1| {20:ZZZ} bar baz               |
2608    |3| bar {20:ZZZ} baz               |
2609    {1:~                             }|
2610    {2:[Preview]                     }|
2611    :%s/foo/ZZZ^                   |
2612  ]])
2613
2614  -- Also with nvim__redraw()
2615  command('call timer_start(10, {-> nvim__redraw(#{flush:1})}, {"repeat":-1})')
2616  command('call timer_start(10, {-> nvim__redraw(#{statusline:1})}, {"repeat":-1})')
2617  sleep(20) -- Allow some timer activity.
2618  screen:expect_unchanged()
2619end)
2620
2621it(':substitute with inccommand, allows :redraw before first separator is typed #18857', function()
2622  clear()
2623  local screen = Screen.new(30, 6)
2624  common_setup(screen, 'split', 'foo bar baz\nbar baz fox\nbar foo baz')
2625  command('hi! link NormalFloat CursorLine')
2626  local float_buf = api.nvim_create_buf(false, true)
2627  api.nvim_open_win(float_buf, false, {
2628    relative = 'editor',
2629    height = 1,
2630    width = 5,
2631    row = 3,
2632    col = 0,
2633    focusable = false,
2634  })
2635  feed(':')
2636  screen:expect([[
2637    foo bar baz                   |
2638    bar baz fox                   |
2639    bar foo baz                   |
2640    {21:     }{1:                         }|
2641    {1:~                             }|
2642    :^                             |
2643  ]])
2644  feed('%s')
2645  screen:expect([[
2646    foo bar baz                   |
2647    bar baz fox                   |
2648    bar foo baz                   |
2649    {21:     }{1:                         }|
2650    {1:~                             }|
2651    :%s^                           |
2652  ]])
2653  api.nvim_buf_set_lines(float_buf, 0, -1, true, { 'foo' })
2654  command('redraw')
2655  screen:expect([[
2656    foo bar baz                   |
2657    bar baz fox                   |
2658    bar foo baz                   |
2659    {21:foo  }{1:                         }|
2660    {1:~                             }|
2661    :%s^                           |
2662  ]])
2663end)
2664
2665it(':substitute with inccommand, does not crash if range contains invalid marks', function()
2666  clear()
2667  local screen = Screen.new(30, 6)
2668  common_setup(screen, 'split', 'test')
2669  feed([[:'a,'bs]])
2670  screen:expect([[
2671    test                          |
2672    {1:~                             }|*4
2673    :'a,'bs^                       |
2674  ]])
2675  -- v:errmsg shouldn't be set either before the first separator is typed
2676  eq('', eval('v:errmsg'))
2677  feed('/')
2678  screen:expect([[
2679    test                          |
2680    {1:~                             }|*4
2681    :'a,'bs/^                      |
2682  ]])
2683end)
2684
2685it(':substitute with inccommand, no unnecessary redraw if preview is not shown', function()
2686  clear()
2687  local screen = Screen.new(60, 6)
2688  common_setup(screen, 'split', 'test')
2689  feed(':ls<CR>')
2690  screen:expect([[
2691    test                                                        |
2692    {1:~                                                           }|
2693    {3:                                                            }|
2694    :ls                                                         |
2695      1 %a + "[No Name]"                    line 1              |
2696    {6:Press ENTER or type command to continue}^                     |
2697  ]])
2698  feed(':s')
2699  -- no unnecessary redraw, so messages are still shown
2700  screen:expect([[
2701    test                                                        |
2702    {1:~                                                           }|
2703    {3:                                                            }|
2704    :ls                                                         |
2705      1 %a + "[No Name]"                    line 1              |
2706    :s^                                                          |
2707  ]])
2708  feed('o')
2709  screen:expect([[
2710    test                                                        |
2711    {1:~                                                           }|
2712    {3:                                                            }|
2713    :ls                                                         |
2714      1 %a + "[No Name]"                    line 1              |
2715    :so^                                                         |
2716  ]])
2717  feed('<BS>')
2718  screen:expect([[
2719    test                                                        |
2720    {1:~                                                           }|
2721    {3:                                                            }|
2722    :ls                                                         |
2723      1 %a + "[No Name]"                    line 1              |
2724    :s^                                                          |
2725  ]])
2726  feed('/test')
2727  -- now inccommand is shown, so screen is redrawn
2728  screen:expect([[
2729    {20:test}                                                        |
2730    {1:~                                                           }|*4
2731    :s/test^                                                     |
2732  ]])
2733end)
2734
2735it(":substitute doesn't crash with inccommand, if undo is empty #12932", function()
2736  clear()
2737  local screen = Screen.new(10, 5)
2738  command('set undolevels=-1')
2739  common_setup(screen, 'split', 'test')
2740  feed(':%s/test')
2741  sleep(100)
2742  feed('/')
2743  sleep(100)
2744  feed('f')
2745  screen:expect([[
2746  {20:f}           |
2747  {1:~           }|*3
2748  :%s/test/f^  |
2749  ]])
2750  assert_alive()
2751end)
2752
2753it(':substitute with inccommand works properly if undo is not synced #20029', function()
2754  clear()
2755  local screen = Screen.new(30, 6)
2756  common_setup(screen, 'nosplit', 'foo\nbar\nbaz')
2757  api.nvim_set_keymap('x', '<F2>', '<Esc>`<Oaaaaa asdf<Esc>`>obbbbb asdf<Esc>V`<k:s/asdf/', {})
2758  feed('gg0<C-V>lljj<F2>')
2759  screen:expect([[
2760    aaaaa                         |
2761    foo                           |
2762    bar                           |
2763    baz                           |
2764    bbbbb                         |
2765    :'<,'>s/asdf/^                 |
2766  ]])
2767  feed('hjkl')
2768  screen:expect([[
2769    aaaaa {20:hjkl}                    |
2770    foo                           |
2771    bar                           |
2772    baz                           |
2773    bbbbb {20:hjkl}                    |
2774    :'<,'>s/asdf/hjkl^             |
2775  ]])
2776  feed('<CR>')
2777  expect([[
2778    aaaaa hjkl
2779    foo
2780    bar
2781    baz
2782    bbbbb hjkl]])
2783  feed('u')
2784  expect([[
2785    foo
2786    bar
2787    baz]])
2788end)
2789
2790it(':substitute with inccommand does not unexpectedly change viewport #25697', function()
2791  clear()
2792  local screen = Screen.new(45, 5)
2793  common_setup(screen, 'nosplit', long_multiline_text)
2794  command('vnew | tabnew | tabclose')
2795  screen:expect([[
2796    ^                      │£ m n                 |
2797    {1:~                     }│t œ ¥                 |
2798    {1:~                     }│                      |
2799    {3:[No Name]              }{2:[No Name] [+]         }|
2800                                                 |
2801  ]])
2802  feed(':s/')
2803  screen:expect([[
2804                          │£ m n                 |
2805    {1:~                     }│t œ ¥                 |
2806    {1:~                     }│                      |
2807    {3:[No Name]              }{2:[No Name] [+]         }|
2808    :s/^                                          |
2809  ]])
2810  feed('<Esc>')
2811  screen:expect([[
2812    ^                      │£ m n                 |
2813    {1:~                     }│t œ ¥                 |
2814    {1:~                     }│                      |
2815    {3:[No Name]              }{2:[No Name] [+]         }|
2816                                                 |
2817  ]])
2818end)
2819
2820it('long :%s/ with inccommand does not collapse cmdline', function()
2821  clear()
2822  local screen = Screen.new(10, 5)
2823  common_setup(screen, 'nosplit')
2824  feed(
2825    ':%s/AAAAAAA',
2826    'A',
2827    'A',
2828    'A',
2829    'A',
2830    'A',
2831    'A',
2832    'A',
2833    'A',
2834    'A',
2835    'A',
2836    'A',
2837    'A',
2838    'A',
2839    'A',
2840    'A',
2841    'A',
2842    'A',
2843    'A',
2844    'A',
2845    'A'
2846  )
2847  screen:expect([[
2848                |
2849    {3:            }|
2850    :%s/AAAAAAAA|
2851    AAAAAAAAAAAA|
2852    AAAAAAA^     |
2853  ]])
2854end)
2855
2856it("with 'inccommand' typing invalid `={expr}` does not show error", function()
2857  clear()
2858  local screen = Screen.new(30, 6)
2859  common_setup(screen, 'nosplit')
2860  feed(':edit `=`')
2861  screen:expect([[
2862                                  |
2863    {1:~                             }|*4
2864    :edit `=`^                     |
2865  ]])
2866end)
2867
2868it("with 'inccommand' typing :filter doesn't segfault or leak memory #19057", function()
2869  clear()
2870  common_setup(nil, 'nosplit')
2871  feed(':filter s')
2872  assert_alive()
2873  feed(' ')
2874  assert_alive()
2875  feed('h')
2876  assert_alive()
2877  feed('i')
2878  assert_alive()
2879end)
2880
2881it("'inccommand' cannot be changed during preview #23136", function()
2882  clear()
2883  local screen = Screen.new(30, 6)
2884  common_setup(screen, 'nosplit', 'foo\nbar\nbaz')
2885  source([[
2886    function! IncCommandToggle()
2887      let prev = &inccommand
2888
2889      if &inccommand ==# 'split'
2890        set inccommand=nosplit
2891      elseif &inccommand ==# 'nosplit'
2892        set inccommand=split
2893      elseif &inccommand ==# ''
2894        set inccommand=nosplit
2895      else
2896        throw 'unknown inccommand'
2897      endif
2898
2899      return " \<BS>"
2900    endfun
2901
2902    cnoremap <expr> <C-E> IncCommandToggle()
2903  ]])
2904
2905  feed(':%s/foo/bar<C-E><C-E><C-E>')
2906  assert_alive()
2907end)
2908
2909it("'inccommand' value can be changed multiple times #27086", function()
2910  clear()
2911  local screen = Screen.new(30, 20)
2912  common_setup(screen, 'split', 'foo1\nfoo2\nfoo3')
2913  for _ = 1, 3 do
2914    feed(':%s/foo/bar')
2915    screen:expect([[
2916      {20:bar}1                          |
2917      {20:bar}2                          |
2918      {20:bar}3                          |
2919      {1:~                             }|*7
2920      {3:[No Name] [+]                 }|
2921      |1| {20:bar}1                      |
2922      |2| {20:bar}2                      |
2923      |3| {20:bar}3                      |
2924      {1:~                             }|*4
2925      {2:[Preview]                     }|
2926      :%s/foo/bar^                   |
2927    ]])
2928    feed('<Esc>')
2929    command('set inccommand=nosplit')
2930    feed(':%s/foo/bar')
2931    screen:expect([[
2932      {20:bar}1                          |
2933      {20:bar}2                          |
2934      {20:bar}3                          |
2935      {1:~                             }|*16
2936      :%s/foo/bar^                   |
2937    ]])
2938    feed('<Esc>')
2939    command('set inccommand=split')
2940  end
2941end)
2942
2943it("'inccommand' disables preview if preview buffer can't be created #27086", function()
2944  clear()
2945  api.nvim_buf_set_name(0, '[Preview]')
2946  local screen = Screen.new(30, 20)
2947  common_setup(screen, 'split', 'foo1\nfoo2\nfoo3')
2948  eq('split', api.nvim_get_option_value('inccommand', {}))
2949  feed(':%s/foo/bar')
2950  screen:expect([[
2951    {20:bar}1                          |
2952    {20:bar}2                          |
2953    {20:bar}3                          |
2954    {1:~                             }|*16
2955    :%s/foo/bar^                   |
2956  ]])
2957  eq('nosplit', api.nvim_get_option_value('inccommand', {}))
2958end)
2959
2960it("'inccommand' :substitute preview skips input() prompt #11940", function()
2961  clear()
2962  local screen = Screen.new(30, 3)
2963  common_setup(screen, 'split', 'foo')
2964  feed([[:s/f/\=input("sub: ")]])
2965  screen:expect([[
2966    oo                            |
2967    {1:~                             }|
2968    :s/f/\=input("sub: ")^         |
2969  ]])
2970end)