robin/laufey

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

robinmerge: 1dc9728daa1d8a2702

master
109.3 KiB3532 linesraw
1" Tests for diff mode
2
3source shared.vim
4source screendump.vim
5source check.vim
6source view_util.vim
7
8func Test_diff_fold_sync()
9  enew!
10  let g:update_count = 0
11  au DiffUpdated * let g:update_count += 1
12
13  let l = range(50)
14  call setline(1, l)
15  diffthis
16  let winone = win_getid()
17  new
18  let l[25] = 'diff'
19  call setline(1, l)
20  diffthis
21  let wintwo = win_getid()
22  " line 15 is inside the closed fold
23  call assert_equal(19, foldclosedend(10))
24  call win_gotoid(winone)
25  call assert_equal(19, foldclosedend(10))
26  " open the fold
27  normal zv
28  call assert_equal(-1, foldclosedend(10))
29  " fold in other window must have opened too
30  call win_gotoid(wintwo)
31  call assert_equal(-1, foldclosedend(10))
32
33  " cursor position is in sync
34  normal 23G
35  call win_gotoid(winone)
36  call assert_equal(23, getcurpos()[1])
37
38  " depending on how redraw is done DiffUpdated may be triggered once or twice
39  call assert_inrange(1, 2, g:update_count)
40  au! DiffUpdated
41
42  windo diffoff
43  close!
44  set nomodified
45endfunc
46
47func Test_vert_split()
48  set diffopt=filler
49  call Common_vert_split()
50  set diffopt&
51endfunc
52
53" Test for diff folding redraw after last diff is resolved
54func Test_diff_fold_redraw()
55  " Set up two files with a minimal case.
56  call writefile(['Paragraph 1', '', 'Paragraph 2', '', 'Paragraph 3'], 'Xfile1')
57  call writefile(['Paragraph 1', '', 'Paragraph 3'], 'Xfile2')
58
59  " Open in diff mode.
60  edit Xfile1
61  vert diffsplit Xfile2
62
63  " Go to the diff and apply :diffput to copy Paragraph 2 to Xfile2.
64  wincmd l
65  3
66  diffput
67
68  " Check that the folds in both windows are closed and extend from the first
69  " line of the buffer to the last line of the buffer.
70  call assert_equal(1, foldclosed(line("$")))
71  wincmd h
72  call assert_equal(1, foldclosed(line("$")))
73
74  " Clean up.
75  bwipe!
76  bwipe!
77  call delete('Xfile1')
78  call delete('Xfile2')
79endfunc
80
81func Test_vert_split_internal()
82  set diffopt=internal,filler
83  call Common_vert_split()
84  set diffopt&
85endfunc
86
87func Common_vert_split()
88  " Disable the title to avoid xterm keeping the wrong one.
89  set notitle noicon
90  new
91  let l = ['1 aa', '2 bb', '3 cc', '4 dd', '5 ee']
92  call setline(1, l)
93  w! Xtest
94  normal dd
95  $
96  put
97  normal kkrXoxxx
98  w! Xtest2
99  file Nop
100  normal ggoyyyjjjozzzz
101  set foldmethod=marker foldcolumn=4
102  call assert_equal(0, &diff)
103  call assert_equal('marker', &foldmethod)
104  call assert_equal('4', &foldcolumn)
105  call assert_equal(0, &scrollbind)
106  call assert_equal(0, &cursorbind)
107  call assert_equal(1, &wrap)
108
109  vert diffsplit Xtest
110  vert diffsplit Xtest2
111  call assert_equal(1, &diff)
112  call assert_equal('diff', &foldmethod)
113  call assert_equal('2', &foldcolumn)
114  call assert_equal(1, &scrollbind)
115  call assert_equal(1, &cursorbind)
116  call assert_equal(0, &wrap)
117
118  let diff_fdm = &fdm
119  let diff_fdc = &fdc
120  " repeat entering diff mode here to see if this saves the wrong settings
121  diffthis
122  " jump to second window for a moment to have filler line appear at start of
123  " first window
124  wincmd w
125  normal gg
126  wincmd p
127  normal gg
128  call assert_equal(2, winline())
129  normal j
130  call assert_equal(4, winline())
131  normal j
132  call assert_equal(5, winline())
133  normal j
134  call assert_equal(6, winline())
135  normal j
136  call assert_equal(8, winline())
137  normal j
138  call assert_equal(9, winline())
139
140  wincmd w
141  normal gg
142  call assert_equal(1, winline())
143  normal j
144  call assert_equal(2, winline())
145  normal j
146  call assert_equal(4, winline())
147  normal j
148  call assert_equal(5, winline())
149  normal j
150  call assert_equal(8, winline())
151
152  wincmd w
153  normal gg
154  call assert_equal(2, winline())
155  normal j
156  call assert_equal(3, winline())
157  normal j
158  call assert_equal(4, winline())
159  normal j
160  call assert_equal(5, winline())
161  normal j
162  call assert_equal(6, winline())
163  normal j
164  call assert_equal(7, winline())
165  normal j
166  call assert_equal(8, winline())
167
168  " Test diffoff
169  diffoff!
170  1wincmd w
171  let &diff = 1
172  let &fdm = diff_fdm
173  let &fdc = diff_fdc
174  4wincmd w
175  diffoff!
176  1wincmd w
177  call assert_equal(0, &diff)
178  call assert_equal('marker', &foldmethod)
179  call assert_equal('4', &foldcolumn)
180  call assert_equal(0, &scrollbind)
181  call assert_equal(0, &cursorbind)
182  call assert_equal(1, &wrap)
183
184  wincmd w
185  call assert_equal(0, &diff)
186  call assert_equal('marker', &foldmethod)
187  call assert_equal('4', &foldcolumn)
188  call assert_equal(0, &scrollbind)
189  call assert_equal(0, &cursorbind)
190  call assert_equal(1, &wrap)
191
192  wincmd w
193  call assert_equal(0, &diff)
194  call assert_equal('marker', &foldmethod)
195  call assert_equal('4', &foldcolumn)
196  call assert_equal(0, &scrollbind)
197  call assert_equal(0, &cursorbind)
198  call assert_equal(1, &wrap)
199
200  call delete('Xtest')
201  call delete('Xtest2')
202  windo bw!
203endfunc
204
205func Test_filler_lines()
206  " Test that diffing shows correct filler lines
207  enew!
208  put =range(4,10)
209  1d _
210  vnew
211  put =range(1,10)
212  1d _
213  windo diffthis
214  wincmd h
215  call assert_equal(1, line('w0'))
216  unlet! diff_fdm diff_fdc
217  windo diffoff
218  bwipe!
219  enew!
220endfunc
221
222func Test_diffget_diffput()
223  enew!
224  let l = range(50)
225  call setline(1, l)
226  call assert_fails('diffget', 'E99:')
227  diffthis
228  call assert_fails('diffget', 'E100:')
229  new
230  let l[10] = 'one'
231  let l[20] = 'two'
232  let l[30] = 'three'
233  let l[40] = 'four'
234  call setline(1, l)
235  diffthis
236  call assert_equal('one', getline(11))
237  11diffget
238  call assert_equal('10', getline(11))
239  21diffput
240  wincmd w
241  call assert_equal('two', getline(21))
242  normal 31Gdo
243  call assert_equal('three', getline(31))
244  call assert_equal('40', getline(41))
245  normal 41Gdp
246  wincmd w
247  call assert_equal('40', getline(41))
248  new
249  diffthis
250  call assert_fails('diffget', 'E101:')
251
252  windo diffoff
253  %bwipe!
254endfunc
255
256" Undo after getting lines into an empty buffer must leave it empty again
257func Test_diffget_undo_empty_buffer()
258  enew!
259  diffthis
260  new
261  call setline(1, ['1', '2'])
262  diffthis
263
264  wincmd p
265  normal do
266  call assert_equal(['1', '2'], getline(1, '$'))
267  undo
268  call assert_equal([''], getline(1, '$'))
269
270  windo diffoff
271  %bwipe!
272endfunc
273
274" Test putting two changes from one buffer to another
275func Test_diffput_two()
276  new a
277  let win_a = win_getid()
278  call setline(1, range(1, 10))
279  diffthis
280  new b
281  let win_b = win_getid()
282  call setline(1, range(1, 10))
283  8del
284  5del
285  diffthis
286  call win_gotoid(win_a)
287  %diffput
288  call win_gotoid(win_b)
289  call assert_equal(map(range(1, 10), 'string(v:val)'), getline(1, '$'))
290  bwipe! a
291  bwipe! b
292endfunc
293
294" Test for :diffget/:diffput with a range that is inside a diff chunk
295func Test_diffget_diffput_range()
296  call setline(1, range(1, 10))
297  new
298  call setline(1, range(11, 20))
299  windo diffthis
300  3,5diffget
301  call assert_equal(['13', '14', '15'], getline(3, 5))
302  call setline(1, range(1, 10))
303  4,8diffput
304  wincmd p
305  call assert_equal(['13', '4', '5', '6', '7', '8', '19'], getline(3, 9))
306  %bw!
307endfunc
308
309" Test :diffget/:diffput handling of added/deleted lines
310func Test_diffget_diffput_deleted_lines()
311  call setline(1, ['2','4','6'])
312  diffthis
313  new
314  call setline(1, range(1,7))
315  diffthis
316  wincmd w
317
318  3,3diffget " get nothing
319  call assert_equal(['2', '4', '6'], getline(1, '$'))
320  3,4diffget " get the last insertion past the end of file
321  call assert_equal(['2', '4', '6', '7'], getline(1, '$'))
322  0,1diffget " get the first insertion above first line
323  call assert_equal(['1', '2', '4', '6', '7'], getline(1, '$'))
324
325  " When using non-range diffget on the last line, it should get the
326  " change above or at the line as usual, but if the only change is below the
327  " last line, diffget should get that instead.
328  1,$delete
329  call setline(1, ['2','4','6'])
330  diffupdate
331  norm Gdo
332  call assert_equal(['2', '4', '5', '6'], getline(1, '$'))
333  norm Gdo
334  call assert_equal(['2', '4', '5', '6', '7'], getline(1, '$'))
335
336  " Test non-range diffput on last line with the same logic
337  1,$delete
338  call setline(1, ['2','4','6'])
339  diffupdate
340  norm Gdp
341  wincmd w
342  call assert_equal(['1', '2', '3', '4', '6', '7'], getline(1, '$'))
343  wincmd w
344  norm Gdp
345  wincmd w
346  call assert_equal(['1', '2', '3', '4', '6'], getline(1, '$'))
347  call setline(1, range(1,7))
348  diffupdate
349  wincmd w
350
351  " Test that 0,$+1 will get/put all changes from/to the other buffer
352  1,$delete
353  call setline(1, ['2','4','6'])
354  diffupdate
355  0,$+1diffget
356  call assert_equal(['1', '2', '3', '4', '5', '6', '7'], getline(1, '$'))
357  1,$delete
358  call setline(1, ['2','4','6'])
359  diffupdate
360  0,$+1diffput
361  wincmd w
362  call assert_equal(['2', '4', '6'], getline(1, '$'))
363  %bw!
364endfunc
365
366" Test for :diffget/:diffput with an empty buffer and a non-empty buffer
367func Test_diffget_diffput_empty_buffer()
368  %d _
369  new
370  call setline(1, 'one')
371  windo diffthis
372  diffget
373  call assert_equal(['one'], getline(1, '$'))
374  %d _
375  diffput
376  wincmd p
377  call assert_equal([''], getline(1, '$'))
378  %bw!
379endfunc
380
381" :diffput and :diffget completes names of buffers which
382" are in diff mode and which are different than current buffer.
383" No completion when the current window is not in diff mode.
384func Test_diffget_diffput_completion()
385  e            Xdiff1 | diffthis
386  botright new Xdiff2
387  botright new Xdiff3 | split | diffthis
388  botright new Xdiff4 | diffthis
389
390  wincmd t
391  call assert_equal('Xdiff1', bufname('%'))
392  call feedkeys(":diffput \<C-A>\<C-B>\"\<CR>", 'tx')
393  call assert_equal('"diffput Xdiff3 Xdiff4', @:)
394  call feedkeys(":diffget \<C-A>\<C-B>\"\<CR>", 'tx')
395  call assert_equal('"diffget Xdiff3 Xdiff4', @:)
396  call assert_equal(['Xdiff3', 'Xdiff4'], getcompletion('', 'diff_buffer'))
397
398  " Xdiff2 is not in diff mode, so no completion for :diffput, :diffget
399  wincmd j
400  call assert_equal('Xdiff2', bufname('%'))
401  call feedkeys(":diffput \<C-A>\<C-B>\"\<CR>", 'tx')
402  call assert_equal('"diffput ', @:)
403  call feedkeys(":diffget \<C-A>\<C-B>\"\<CR>", 'tx')
404  call assert_equal('"diffget ', @:)
405  call assert_equal([], getcompletion('', 'diff_buffer'))
406
407  " Xdiff3 is split in 2 windows, only the top one is in diff mode.
408  " So completion of :diffput :diffget only happens in the top window.
409  wincmd j
410  call assert_equal('Xdiff3', bufname('%'))
411  call assert_equal(1, &diff)
412  call feedkeys(":diffput \<C-A>\<C-B>\"\<CR>", 'tx')
413  call assert_equal('"diffput Xdiff1 Xdiff4', @:)
414  call feedkeys(":diffget \<C-A>\<C-B>\"\<CR>", 'tx')
415  call assert_equal('"diffget Xdiff1 Xdiff4', @:)
416  call assert_equal(['Xdiff1', 'Xdiff4'], getcompletion('', 'diff_buffer'))
417
418  wincmd j
419  call assert_equal('Xdiff3', bufname('%'))
420  call assert_equal(0, &diff)
421  call feedkeys(":diffput \<C-A>\<C-B>\"\<CR>", 'tx')
422  call assert_equal('"diffput ', @:)
423  call feedkeys(":diffget \<C-A>\<C-B>\"\<CR>", 'tx')
424  call assert_equal('"diffget ', @:)
425  call assert_equal([], getcompletion('', 'diff_buffer'))
426
427  wincmd j
428  call assert_equal('Xdiff4', bufname('%'))
429  call feedkeys(":diffput \<C-A>\<C-B>\"\<CR>", 'tx')
430  call assert_equal('"diffput Xdiff1 Xdiff3', @:)
431  call feedkeys(":diffget \<C-A>\<C-B>\"\<CR>", 'tx')
432  call assert_equal('"diffget Xdiff1 Xdiff3', @:)
433  call assert_equal(['Xdiff1', 'Xdiff3'], getcompletion('', 'diff_buffer'))
434
435  %bwipe
436endfunc
437
438func Test_dp_do_buffer()
439  e! one
440  let bn1=bufnr('%')
441  let l = range(60)
442  call setline(1, l)
443  diffthis
444
445  new two
446  let l[10] = 'one'
447  let l[20] = 'two'
448  let l[30] = 'three'
449  let l[40] = 'four'
450  let l[50] = 'five'
451  call setline(1, l)
452  diffthis
453
454  " dp and do with invalid buffer number.
455  11
456  call assert_fails('norm 99999dp', 'E102:')
457  call assert_fails('norm 99999do', 'E102:')
458  call assert_fails('diffput non_existing_buffer', 'E94:')
459  call assert_fails('diffget non_existing_buffer', 'E94:')
460
461  " dp and do with valid buffer number.
462  call assert_equal('one', getline('.'))
463  exe 'norm ' . bn1 . 'do'
464  call assert_equal('10', getline('.'))
465  21
466  call assert_equal('two', getline('.'))
467  diffget one
468  call assert_equal('20', getline('.'))
469
470  31
471  exe 'norm ' . bn1 . 'dp'
472  41
473  diffput one
474  wincmd w
475  31
476  call assert_equal('three', getline('.'))
477  41
478  call assert_equal('four', getline('.'))
479
480  " dp and do with buffer number which is not in diff mode.
481  new not_in_diff_mode
482  let bn3=bufnr('%')
483  wincmd w
484  51
485  call assert_fails('exe "norm" . bn3 . "dp"', 'E103:')
486  call assert_fails('exe "norm" . bn3 . "do"', 'E103:')
487  call assert_fails('diffput not_in_diff_mode', 'E94:')
488  call assert_fails('diffget not_in_diff_mode', 'E94:')
489
490  windo diffoff
491  %bwipe!
492endfunc
493
494func Test_do_lastline()
495  e! one
496  call setline(1, ['1','2','3','4','5','6'])
497  diffthis
498
499  new two
500  call setline(1, ['2','4','5'])
501  diffthis
502
503  1
504  norm dp]c
505  norm dp]c
506  wincmd w
507  call assert_equal(4, line('$'))
508  norm G
509  norm do
510  call assert_equal(3, line('$'))
511
512  windo diffoff
513  %bwipe!
514endfunc
515
516func Test_diffoff()
517  enew!
518  call setline(1, ['Two', 'Three'])
519  redraw
520  let normattr = screenattr(1, 1)
521  diffthis
522  botright vert new
523  call setline(1, ['One', '', 'Two', 'Three'])
524  diffthis
525  redraw
526  call assert_notequal(normattr, 1->screenattr(1))
527  diffoff!
528  redraw
529  call assert_equal(normattr, screenattr(1, 1))
530  bwipe!
531  bwipe!
532endfunc
533
534func Common_icase_test()
535  edit one
536  call setline(1, ['One', 'Two', 'Three', 'Four', 'Fi#vϵ', 'Si⃗x', 'Se⃗ve⃗n'])
537  redraw
538  let normattr = screenattr(1, 1)
539  diffthis
540
541  botright vert new two
542  call setline(1, ['one', 'TWO', 'Three ', 'Four', 'fI=VΕ', 'SI⃗x', 'SEvE⃗n'])
543  diffthis
544
545  redraw
546  call assert_equal(normattr, screenattr(1, 1))
547  call assert_equal(normattr, screenattr(2, 1))
548  call assert_notequal(normattr, screenattr(3, 1))
549  call assert_equal(normattr, screenattr(4, 1))
550  call assert_equal(normattr, screenattr(6, 2))
551  call assert_notequal(normattr, screenattr(7, 2))
552
553  let dtextattr = screenattr(5, 3)
554  call assert_notequal(dtextattr, screenattr(5, 1))
555  call assert_notequal(dtextattr, screenattr(5, 5))
556  call assert_notequal(dtextattr, screenattr(7, 4))
557
558  diffoff!
559  %bwipe!
560endfunc
561
562func Test_diffopt_icase()
563  set diffopt=icase,foldcolumn:0
564  call Common_icase_test()
565  set diffopt&
566endfunc
567
568func Test_diffopt_icase_internal()
569  set diffopt=icase,foldcolumn:0,internal
570  call Common_icase_test()
571  set diffopt&
572endfunc
573
574func Common_iwhite_test()
575  edit one
576  " Difference in trailing spaces and amount of spaces should be ignored,
577  " but not other space differences.
578  call setline(1, ["One \t", 'Two', 'Three', 'one two', 'one two', 'Four'])
579  redraw
580  let normattr = screenattr(1, 1)
581  diffthis
582
583  botright vert new two
584  call setline(1, ["One\t ", "Two\t ", 'Three', 'one   two', 'onetwo', ' Four'])
585  diffthis
586
587  redraw
588  call assert_equal(normattr, screenattr(1, 1))
589  call assert_equal(normattr, screenattr(2, 1))
590  call assert_equal(normattr, screenattr(3, 1))
591  call assert_equal(normattr, screenattr(4, 1))
592  call assert_notequal(normattr, screenattr(5, 1))
593  call assert_notequal(normattr, screenattr(6, 1))
594
595  diffoff!
596  %bwipe!
597endfunc
598
599func Test_diffopt_iwhite()
600  set diffopt=iwhite,foldcolumn:0
601  call Common_iwhite_test()
602  set diffopt&
603endfunc
604
605func Test_diffopt_iwhite_internal()
606  set diffopt=internal,iwhite,foldcolumn:0
607  call Common_iwhite_test()
608  set diffopt&
609endfunc
610
611func Test_diffopt_context()
612  enew!
613  call setline(1, ['1', '2', '3', '4', '5', '6', '7'])
614  diffthis
615  new
616  call setline(1, ['1', '2', '3', '4', '5x', '6', '7'])
617  diffthis
618
619  set diffopt=context:2
620  call assert_equal('+--  2 lines: 1', foldtextresult(1))
621  set diffopt=internal,context:2
622  call assert_equal('+--  2 lines: 1', foldtextresult(1))
623
624  set diffopt=context:1
625  call assert_equal('+--  3 lines: 1', foldtextresult(1))
626  set diffopt=internal,context:1
627  call assert_equal('+--  3 lines: 1', foldtextresult(1))
628
629  diffoff!
630  %bwipe!
631  set diffopt&
632endfunc
633
634func Test_diffopt_horizontal()
635  set diffopt=internal,horizontal
636  diffsplit
637
638  call assert_equal(&columns, winwidth(1))
639  call assert_equal(&columns, winwidth(2))
640  call assert_equal(&lines, winheight(1) + winheight(2) + 3)
641  call assert_inrange(0, 1, winheight(1) - winheight(2))
642
643  set diffopt&
644  diffoff!
645  %bwipe
646endfunc
647
648func Test_diffopt_vertical()
649  set diffopt=internal,vertical
650  diffsplit
651
652  call assert_equal(&lines - 2, winheight(1))
653  call assert_equal(&lines - 2, winheight(2))
654  call assert_equal(&columns, winwidth(1) + winwidth(2) + 1)
655  call assert_inrange(0, 1, winwidth(1) - winwidth(2))
656
657  set diffopt&
658  diffoff!
659  %bwipe
660endfunc
661
662func Test_diffopt_hiddenoff()
663  set diffopt=internal,filler,foldcolumn:0,hiddenoff
664  e! one
665  call setline(1, ['Two', 'Three'])
666  redraw
667  let normattr = screenattr(1, 1)
668  diffthis
669  botright vert new two
670  call setline(1, ['One', 'Four'])
671  diffthis
672  redraw
673  call assert_notequal(normattr, screenattr(1, 1))
674  set hidden
675  close
676  redraw
677  " should not diffing with hidden buffer two while 'hiddenoff' is enabled
678  call assert_equal(normattr, screenattr(1, 1))
679
680  bwipe!
681  bwipe!
682  set hidden& diffopt&
683endfunc
684
685func Test_diffoff_hidden()
686  set diffopt=internal,filler,foldcolumn:0
687  e! one
688  call setline(1, ['Two', 'Three'])
689  redraw
690  let normattr = screenattr(1, 1)
691  diffthis
692  botright vert new two
693  call setline(1, ['One', 'Four'])
694  diffthis
695  redraw
696  call assert_notequal(normattr, screenattr(1, 1))
697  set hidden
698  close
699  redraw
700  " diffing with hidden buffer two
701  call assert_notequal(normattr, screenattr(1, 1))
702  diffoff
703  redraw
704  call assert_equal(normattr, screenattr(1, 1))
705  diffthis
706  redraw
707  " still diffing with hidden buffer two
708  call assert_notequal(normattr, screenattr(1, 1))
709  diffoff!
710  redraw
711  call assert_equal(normattr, screenattr(1, 1))
712  diffthis
713  redraw
714  " no longer diffing with hidden buffer two
715  call assert_equal(normattr, screenattr(1, 1))
716
717  bwipe!
718  bwipe!
719  set hidden& diffopt&
720endfunc
721
722func Test_setting_cursor()
723  new Xtest1
724  put =range(1,90)
725  wq
726  new Xtest2
727  put =range(1,100)
728  wq
729
730  tabe Xtest2
731  $
732  diffsp Xtest1
733  tabclose
734
735  call delete('Xtest1')
736  call delete('Xtest2')
737endfunc
738
739func Test_diff_move_to()
740  new
741  call setline(1, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
742  diffthis
743  vnew
744  call setline(1, [1, '2x', 3, 4, 4, 5, '6x', 7, '8x', 9, '10x'])
745  diffthis
746  norm ]c
747  call assert_equal(2, line('.'))
748  norm 3]c
749  call assert_equal(9, line('.'))
750  norm 10]c
751  call assert_equal(11, line('.'))
752  norm [c
753  call assert_equal(9, line('.'))
754  norm 2[c
755  call assert_equal(5, line('.'))
756  norm 10[c
757  call assert_equal(2, line('.'))
758  %bwipe!
759endfunc
760
761func Test_diffexpr()
762  CheckExecutable diff
763
764  func DiffExpr()
765    " Prepend some text to check diff type detection
766    call writefile(['warning', '  message'], v:fname_out)
767    silent exe '!diff ' . v:fname_in . ' ' . v:fname_new . '>>' . v:fname_out
768  endfunc
769  set diffexpr=DiffExpr()
770  set diffopt=foldcolumn:0
771
772  enew!
773  call setline(1, ['one', 'two', 'three'])
774  redraw
775  let normattr = screenattr(1, 1)
776  diffthis
777
778  botright vert new
779  call setline(1, ['one', 'two', 'three.'])
780  diffthis
781
782  redraw
783  call assert_equal(normattr, screenattr(1, 1))
784  call assert_equal(normattr, screenattr(2, 1))
785  call assert_notequal(normattr, screenattr(3, 1))
786  diffoff!
787
788  " Try using a non-existing function for 'diffexpr'.
789  set diffexpr=NewDiffFunc()
790  call assert_fails('windo diffthis', ['E117:', 'E97:'])
791  diffoff!
792
793  " Using a script-local function
794  func s:NewDiffExpr()
795  endfunc
796  set diffexpr=s:NewDiffExpr()
797  call assert_equal(expand('<SID>') .. 'NewDiffExpr()', &diffexpr)
798  set diffexpr=<SID>NewDiffExpr()
799  call assert_equal(expand('<SID>') .. 'NewDiffExpr()', &diffexpr)
800
801  %bwipe!
802  set diffexpr& diffopt&
803  delfunc DiffExpr
804  delfunc s:NewDiffExpr
805endfunc
806
807func Test_diffpatch()
808  " The patch program on MS-Windows may fail or hang.
809  CheckExecutable patch
810  CheckUnix
811  new
812  insert
813***************
814*** 1,3 ****
815  1
816! 2
817  3
818--- 1,4 ----
819  1
820! 2x
821  3
822+ 4
823.
824  saveas! Xpatch
825  bwipe!
826  new
827  call assert_fails('diffpatch Xpatch', 'E816:')
828
829  for name in ['Xpatch', 'Xpatch$HOME', 'Xpa''tch']
830    call setline(1, ['1', '2', '3'])
831    if name != 'Xpatch'
832      call rename('Xpatch', name)
833    endif
834    exe 'diffpatch ' . escape(name, '$')
835    call assert_equal(['1', '2x', '3', '4'], getline(1, '$'))
836    if name != 'Xpatch'
837      call rename(name, 'Xpatch')
838    endif
839    bwipe!
840  endfor
841
842  call delete('Xpatch')
843  bwipe!
844endfunc
845
846func Test_diff_too_many_buffers()
847  for i in range(1, 8)
848    exe "new Xtest" . i
849    diffthis
850  endfor
851  new Xtest9
852  call assert_fails('diffthis', 'E96:')
853  %bwipe!
854endfunc
855
856func Test_diff_nomodifiable()
857  new
858  call setline(1, [1, 2, 3, 4])
859  setl nomodifiable
860  diffthis
861  vnew
862  call setline(1, ['1x', 2, 3, 3, 4])
863  diffthis
864  call assert_fails('norm dp', 'E793:')
865  setl nomodifiable
866  call assert_fails('norm do', 'E21:')
867  %bwipe!
868endfunc
869
870func Test_diff_hlID()
871  set diffopt=internal,filler
872  new
873  call setline(1, [1, 2, 3, 'Yz', 'a dxxg',])
874  diffthis
875  vnew
876  call setline(1, ['1x', 2, 'x', 3, 'yx', 'abc defg'])
877  diffthis
878  redraw
879
880  call diff_hlID(-1, 1)->synIDattr("name")->assert_equal("")
881
882  call diff_hlID(1, 1)->synIDattr("name")->assert_equal("DiffChange")
883  call diff_hlID(1, 2)->synIDattr("name")->assert_equal("DiffText")
884  call diff_hlID(2, 1)->synIDattr("name")->assert_equal("")
885  call diff_hlID(3, 1)->synIDattr("name")->assert_equal("DiffAdd")
886  eval 4->diff_hlID(1)->synIDattr("name")->assert_equal("")
887  call diff_hlID(5, 1)->synIDattr("name")->assert_equal("DiffText")
888  call diff_hlID(5, 2)->synIDattr("name")->assert_equal("DiffText")
889
890  set diffopt+=icase " test that caching is invalidated by diffopt change
891  call diff_hlID(5, 1)->synIDattr("name")->assert_equal("DiffChange")
892  set diffopt-=icase
893  call diff_hlID(5, 1)->synIDattr("name")->assert_equal("DiffText")
894
895  call diff_hlID(6, 1)->synIDattr("name")->assert_equal("DiffChange")
896  call diff_hlID(6, 2)->synIDattr("name")->assert_equal("DiffText")
897  call diff_hlID(6, 4)->synIDattr("name")->assert_equal("DiffText")
898  call diff_hlID(6, 7)->synIDattr("name")->assert_equal("DiffText")
899  call diff_hlID(6, 8)->synIDattr("name")->assert_equal("DiffChange")
900  set diffopt+=inline:char
901  call diff_hlID(6, 1)->synIDattr("name")->assert_equal("DiffChange")
902  call diff_hlID(6, 2)->synIDattr("name")->assert_equal("DiffTextAdd")
903  call diff_hlID(6, 4)->synIDattr("name")->assert_equal("DiffChange")
904  call diff_hlID(6, 7)->synIDattr("name")->assert_equal("DiffText")
905  call diff_hlID(6, 8)->synIDattr("name")->assert_equal("DiffChange")
906  set diffopt-=inline:char
907
908  wincmd w
909  call assert_equal(synIDattr(diff_hlID(1, 1), "name"), "DiffChange")
910  call assert_equal(synIDattr(diff_hlID(2, 1), "name"), "")
911  call assert_equal(synIDattr(diff_hlID(3, 1), "name"), "")
912
913  %bwipe!
914  set diffopt&
915endfunc
916
917func Test_diff_filler()
918  new
919  call setline(1, [1, 2, 3, 'x', 4])
920  diffthis
921  vnew
922  call setline(1, [1, 2, 'y', 'y', 3, 4])
923  diffthis
924  redraw
925
926  call assert_equal([0, 0, 0, 0, 0, 0, 0, 1, 0], map(range(-1, 7), 'v:val->diff_filler()'))
927  wincmd w
928  call assert_equal([0, 0, 0, 0, 2, 0, 0, 0], map(range(-1, 6), 'diff_filler(v:val)'))
929
930  %bwipe!
931endfunc
932
933func Test_diff_lastline()
934  enew!
935  only!
936  call setline(1, ['This is a ', 'line with five ', 'rows'])
937  diffthis
938  botright vert new
939  call setline(1, ['This is', 'a line with ', 'four rows'])
940  diffthis
941  1
942  call feedkeys("Je a\<CR>", 'tx')
943  call feedkeys("Je a\<CR>", 'tx')
944  let w1lines = winline()
945  wincmd w
946  $
947  let w2lines = winline()
948  call assert_equal(w2lines, w1lines)
949  bwipe!
950  bwipe!
951endfunc
952
953func WriteDiffFiles(buf, list1, list2)
954  call writefile(a:list1, 'Xdifile1')
955  call writefile(a:list2, 'Xdifile2')
956  if a:buf
957    call term_sendkeys(a:buf, ":checktime\<CR>")
958  endif
959endfunc
960
961func WriteDiffFiles3(buf, list1, list2, list3)
962  call writefile(a:list1, 'Xdifile1')
963  call writefile(a:list2, 'Xdifile2')
964  call writefile(a:list3, 'Xdifile3')
965  if a:buf
966    call term_sendkeys(a:buf, ":checktime\<CR>")
967  endif
968endfunc
969
970" Verify a screendump with both the internal and external diff.
971func VerifyBoth(buf, dumpfile, extra)
972  CheckScreendump
973
974  " trailing : for leaving the cursor on the command line
975  for cmd in [":set diffopt=filler" . a:extra . "\<CR>:", ":set diffopt+=internal\<CR>:"]
976    call term_sendkeys(a:buf, cmd)
977    if VerifyScreenDump(a:buf, a:dumpfile, {}, cmd =~ 'internal' ? 'internal' : 'external')
978      break " don't let the next iteration overwrite the "failed" file.
979      " don't let the next iteration overwrite the "failed" file.
980      return
981    endif
982  endfor
983
984  " also test unified diff
985  call term_sendkeys(a:buf, ":call SetupUnified()\<CR>:")
986  call term_sendkeys(a:buf, ":redraw!\<CR>:")
987  call VerifyScreenDump(a:buf, a:dumpfile, {}, 'unified')
988  call term_sendkeys(a:buf, ":call StopUnified()\<CR>:")
989endfunc
990
991" Verify a screendump with the internal diff only.
992func VerifyInternal(buf, dumpfile, extra)
993  CheckScreendump
994
995  call term_sendkeys(a:buf, ":diffupdate!\<CR>")
996  " trailing : for leaving the cursor on the command line
997  call term_sendkeys(a:buf, ":set diffopt=internal,filler" . a:extra . "\<CR>:")
998  call VerifyScreenDump(a:buf, a:dumpfile, {})
999endfunc
1000
1001func Test_diff_screen()
1002  let g:test_is_flaky = 1
1003  CheckScreendump
1004  CheckFeature menu
1005
1006  let lines =<< trim END
1007      func UnifiedDiffExpr()
1008        " Prepend some text to check diff type detection
1009        call writefile(['warning', '  message'], v:fname_out)
1010        silent exe '!diff -U0 ' .. v:fname_in .. ' ' .. v:fname_new .. '>>' .. v:fname_out
1011      endfunc
1012      func SetupUnified()
1013        set diffexpr=UnifiedDiffExpr()
1014        diffupdate
1015      endfunc
1016      func StopUnified()
1017        set diffexpr=
1018      endfunc
1019  END
1020  call writefile(lines, 'XdiffSetup', 'D')
1021
1022  " clean up already existing swap files, just in case
1023  call delete('.Xdifile1.swp')
1024  call delete('.Xdifile2.swp')
1025
1026  " Test 1: Add a line in beginning of file 2
1027  call WriteDiffFiles(0, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
1028  let buf = RunVimInTerminal('-d -S XdiffSetup Xdifile1 Xdifile2', {})
1029  " Set autoread mode, so that Vim won't complain once we re-write the test
1030  " files
1031  call term_sendkeys(buf, ":set autoread\<CR>\<c-w>w:set autoread\<CR>\<c-w>w")
1032
1033  call VerifyBoth(buf, 'Test_diff_01', '')
1034
1035  " Test 2: Add a line in beginning of file 1
1036  call WriteDiffFiles(buf, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
1037  call VerifyBoth(buf, 'Test_diff_02', '')
1038
1039  " Test 3: Add a line at the end of file 2
1040  call WriteDiffFiles(buf, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
1041  call VerifyBoth(buf, 'Test_diff_03', '')
1042
1043  " Test 4: Add a line at the end of file 1
1044  call WriteDiffFiles(buf, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
1045  call VerifyBoth(buf, 'Test_diff_04', '')
1046
1047  " Test 5: Add a line in the middle of file 2, remove on at the end of file 1
1048  call WriteDiffFiles(buf, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11], [1, 2, 3, 4, 4, 5, 6, 7, 8, 9, 10])
1049  call VerifyBoth(buf, 'Test_diff_05', '')
1050
1051  " Test 6: Add a line in the middle of file 1, remove on at the end of file 2
1052  call WriteDiffFiles(buf, [1, 2, 3, 4, 4, 5, 6, 7, 8, 9, 10], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
1053  call VerifyBoth(buf, 'Test_diff_06', '')
1054
1055  " Variants on test 6 with different context settings
1056  call term_sendkeys(buf, ":set diffopt+=context:2\<cr>")
1057  call VerifyScreenDump(buf, 'Test_diff_06.2', {})
1058  call term_sendkeys(buf, ":set diffopt-=context:2\<cr>")
1059  call term_sendkeys(buf, ":set diffopt+=context:1\<cr>")
1060  call VerifyScreenDump(buf, 'Test_diff_06.1', {})
1061  call term_sendkeys(buf, ":set diffopt-=context:1\<cr>")
1062  call term_sendkeys(buf, ":set diffopt+=context:0\<cr>")
1063  call VerifyScreenDump(buf, 'Test_diff_06.0', {})
1064  call term_sendkeys(buf, ":set diffopt-=context:0\<cr>")
1065
1066  " Test 7 - 9: Test normal/patience/histogram diff algorithm
1067  call WriteDiffFiles(buf, ['#include <stdio.h>', '', '// Frobs foo heartily', 'int frobnitz(int foo)', '{',
1068      \ '    int i;', '    for(i = 0; i < 10; i++)', '    {', '        printf("Your answer is: ");',
1069      \ '        printf("%d\n", foo);', '    }', '}', '', 'int fact(int n)', '{', '    if(n > 1)', '    {',
1070      \ '        return fact(n-1) * n;', '    }', '    return 1;', '}', '', 'int main(int argc, char **argv)',
1071      \ '{', '    frobnitz(fact(10));', '}'],
1072      \ ['#include <stdio.h>', '', 'int fib(int n)', '{', '    if(n > 2)', '    {',
1073      \ '        return fib(n-1) + fib(n-2);', '    }', '    return 1;', '}', '', '// Frobs foo heartily',
1074      \ 'int frobnitz(int foo)', '{', '    int i;', '    for(i = 0; i < 10; i++)', '    {',
1075      \ '        printf("%d\n", foo);', '    }', '}', '',
1076      \ 'int main(int argc, char **argv)', '{', '    frobnitz(fib(10));', '}'])
1077  call term_sendkeys(buf, ":diffupdate!\<cr>")
1078  call term_sendkeys(buf, ":set diffopt+=internal\<cr>")
1079  call VerifyScreenDump(buf, 'Test_diff_07', {})
1080
1081  call term_sendkeys(buf, ":set diffopt+=algorithm:patience\<cr>")
1082  call VerifyScreenDump(buf, 'Test_diff_08', {})
1083
1084  call term_sendkeys(buf, ":set diffopt+=algorithm:histogram\<cr>")
1085  call VerifyScreenDump(buf, 'Test_diff_09', {})
1086
1087  " Test 10-11: with/without indent-heuristic
1088  call term_sendkeys(buf, ":set diffopt&vim\<cr>")
1089  call WriteDiffFiles(buf, ['', '  def finalize(values)', '', '    values.each do |v|', '      v.finalize', '    end'],
1090      \ ['', '  def finalize(values)', '', '    values.each do |v|', '      v.prepare', '    end', '',
1091      \ '    values.each do |v|', '      v.finalize', '    end'])
1092  call term_sendkeys(buf, ":diffupdate!\<cr>")
1093  call term_sendkeys(buf, ":set diffopt+=internal\<cr>:\<cr>")
1094  call VerifyScreenDump(buf, 'Test_diff_11', {})
1095
1096  " Leave trailing : at commandline!
1097  call term_sendkeys(buf, ":set diffopt-=indent-heuristic\<cr>:\<cr>")
1098  call VerifyScreenDump(buf, 'Test_diff_10', {}, 'one')
1099  " shouldn't matter, if indent-algorithm comes before or after the algorithm
1100  call term_sendkeys(buf, ":set diffopt&\<cr>")
1101  call term_sendkeys(buf, ":set diffopt+=indent-heuristic,algorithm:patience\<cr>:\<cr>")
1102  call VerifyScreenDump(buf, 'Test_diff_11', {}, 'two')
1103  call term_sendkeys(buf, ":set diffopt&\<cr>")
1104  call term_sendkeys(buf, ":set diffopt+=algorithm:patience,indent-heuristic\<cr>:\<cr>")
1105  call VerifyScreenDump(buf, 'Test_diff_11', {}, 'three')
1106
1107  " Test 12: diff the same file
1108  call WriteDiffFiles(buf, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
1109  call VerifyBoth(buf, 'Test_diff_12', '')
1110
1111  " Test 13: diff an empty file
1112  call WriteDiffFiles(buf, [], [])
1113  call VerifyBoth(buf, 'Test_diff_13', '')
1114
1115  " Test 14: test diffopt+=icase
1116  call WriteDiffFiles(buf, ['a', 'b', 'cd'], ['A', 'b', 'cDe'])
1117  call VerifyBoth(buf, 'Test_diff_14', " diffopt+=filler diffopt+=icase")
1118
1119  " Test 15-16: test diffopt+=iwhite
1120  call WriteDiffFiles(buf, ['int main()', '{', '   printf("Hello, World!");', '   return 0;', '}'],
1121      \ ['int main()', '{', '   if (0)', '   {', '      printf("Hello, World!");', '      return 0;', '   }', '}'])
1122  call term_sendkeys(buf, ":diffupdate!\<cr>")
1123  call term_sendkeys(buf, ":set diffopt&vim diffopt+=filler diffopt+=iwhite\<cr>")
1124  call VerifyScreenDump(buf, 'Test_diff_15', {})
1125  call term_sendkeys(buf, ":set diffopt+=internal\<cr>")
1126  call VerifyScreenDump(buf, 'Test_diff_16', {})
1127
1128  " Test 17: test diffopt+=iblank
1129  call WriteDiffFiles(buf, ['a', ' ', 'cd', 'ef', 'xxx'], ['a', 'cd', '', 'ef', 'yyy'])
1130  call VerifyInternal(buf, 'Test_diff_17', " diffopt+=iblank")
1131
1132  " Test 18: test diffopt+=iblank,iwhite / iwhiteall / iwhiteeol
1133  call VerifyInternal(buf, 'Test_diff_18', " diffopt+=iblank,iwhite")
1134  call VerifyInternal(buf, 'Test_diff_18', " diffopt+=iblank,iwhiteall")
1135  call VerifyInternal(buf, 'Test_diff_18', " diffopt+=iblank,iwhiteeol")
1136
1137  " Test 19: test diffopt+=iwhiteeol
1138  call WriteDiffFiles(buf, ['a ', 'x', 'cd', 'ef', 'xx  xx', 'foo', 'bar'], ['a', 'x', 'c d', ' ef', 'xx xx', 'foo', '', 'bar'])
1139  call VerifyInternal(buf, 'Test_diff_19', " diffopt+=iwhiteeol")
1140
1141  " Test 20: test diffopt+=iwhiteall
1142  call VerifyInternal(buf, 'Test_diff_20', " diffopt+=iwhiteall")
1143
1144  " Test 21: Delete all lines
1145  call WriteDiffFiles(buf, [0], [])
1146  call VerifyBoth(buf, "Test_diff_21", "")
1147
1148  " Test 22: Add line to empty file
1149  call WriteDiffFiles(buf, [], [0])
1150  call VerifyBoth(buf, "Test_diff_22", "")
1151
1152  call WriteDiffFiles(buf, ['?a', '?b', '?c'], ['!b'])
1153  call VerifyInternal(buf, 'Test_diff_23', " diffopt+=linematch:30")
1154
1155  call WriteDiffFiles(buf, ['',
1156      \ 'common line',
1157      \ 'common line',
1158      \ '',
1159      \ 'DEFabc',
1160      \ 'xyz',
1161      \ 'xyz',
1162      \ 'xyz',
1163      \ 'DEFabc',
1164      \ 'DEFabc',
1165      \ 'DEFabc',
1166      \ 'common line',
1167      \ 'common line',
1168      \ 'DEF',
1169      \ 'common line',
1170      \ 'DEF',
1171      \ 'something' ],
1172      \ ['',
1173      \ 'common line',
1174      \ 'common line',
1175      \ '',
1176      \ 'ABCabc',
1177      \ 'ABCabc',
1178      \ 'ABCabc',
1179      \ 'ABCabc',
1180      \ 'common line',
1181      \ 'common line',
1182      \ 'common line',
1183      \ 'something'])
1184  call VerifyInternal(buf, 'Test_diff_24', " diffopt+=linematch:30")
1185
1186
1187  " clean up
1188  call StopVimInTerminal(buf)
1189  call delete('Xdifile1')
1190  call delete('Xdifile2')
1191endfunc
1192
1193func Test_diff_with_scroll_and_change()
1194  CheckScreendump
1195
1196  let lines =<< trim END
1197	call setline(1, range(1, 15))
1198	vnew
1199	call setline(1, range(9, 15))
1200	windo diffthis
1201	wincmd h
1202	exe "normal Gl5\<C-E>"
1203  END
1204  call writefile(lines, 'Xtest_scroll_change', 'D')
1205  let buf = RunVimInTerminal('-S Xtest_scroll_change', {})
1206
1207  call VerifyScreenDump(buf, 'Test_diff_scroll_change_01', {})
1208
1209  call term_sendkeys(buf, "ax\<Esc>")
1210  call VerifyScreenDump(buf, 'Test_diff_scroll_change_02', {})
1211
1212  call term_sendkeys(buf, "\<C-W>lay\<Esc>")
1213  call VerifyScreenDump(buf, 'Test_diff_scroll_change_03', {})
1214
1215  " clean up
1216  call StopVimInTerminal(buf)
1217endfunc
1218
1219func Test_diff_with_cursorline()
1220  CheckScreendump
1221
1222  call writefile([
1223	\ 'hi CursorLine ctermbg=red ctermfg=white',
1224	\ 'set cursorline',
1225	\ 'call setline(1, ["foo","foo","foo","bar"])',
1226	\ 'vnew',
1227	\ 'call setline(1, ["bee","foo","foo","baz"])',
1228	\ 'windo diffthis',
1229	\ '2wincmd w',
1230	\ ], 'Xtest_diff_cursorline', 'D')
1231  let buf = RunVimInTerminal('-S Xtest_diff_cursorline', {})
1232
1233  call VerifyScreenDump(buf, 'Test_diff_with_cursorline_01', {})
1234  call term_sendkeys(buf, "j")
1235  call VerifyScreenDump(buf, 'Test_diff_with_cursorline_02', {})
1236  call term_sendkeys(buf, "j")
1237  call VerifyScreenDump(buf, 'Test_diff_with_cursorline_03', {})
1238
1239  " clean up
1240  call StopVimInTerminal(buf)
1241endfunc
1242
1243func Test_diff_with_cursorline_number()
1244  CheckScreendump
1245
1246  let lines =<< trim END
1247      hi CursorLine ctermbg=red ctermfg=white
1248      hi CursorLineNr ctermbg=white ctermfg=black cterm=underline
1249      set cursorline number
1250      call setline(1, ["baz", "foo", "foo", "bar"])
1251      2
1252      vnew
1253      call setline(1, ["foo", "foo", "bar"])
1254      windo diffthis
1255      1wincmd w
1256  END
1257  call writefile(lines, 'Xtest_diff_cursorline_number', 'D')
1258  let buf = RunVimInTerminal('-S Xtest_diff_cursorline_number', {})
1259
1260  call VerifyScreenDump(buf, 'Test_diff_with_cursorline_number_01', {})
1261  call term_sendkeys(buf, ":set cursorlineopt=number\r")
1262  call VerifyScreenDump(buf, 'Test_diff_with_cursorline_number_02', {})
1263
1264  " clean up
1265  call StopVimInTerminal(buf)
1266endfunc
1267
1268func Test_diff_with_cursorline_breakindent()
1269  CheckScreendump
1270
1271  let lines =<< trim END
1272    hi CursorLine ctermbg=red ctermfg=white
1273    set noequalalways wrap diffopt=followwrap cursorline breakindent
1274    50vnew
1275    call setline(1, ['  ', '  ', '  ', '  '])
1276    exe "norm! 20Afoo\<Esc>j20Afoo\<Esc>j20Afoo\<Esc>j20Abar\<Esc>"
1277    vnew
1278    call setline(1, ['  ', '  ', '  ', '  '])
1279    exe "norm! 20Abee\<Esc>j20Afoo\<Esc>j20Afoo\<Esc>j20Abaz\<Esc>"
1280    windo diffthis
1281    2wincmd w
1282  END
1283  call writefile(lines, 'Xtest_diff_cursorline_breakindent', 'D')
1284  let buf = RunVimInTerminal('-S Xtest_diff_cursorline_breakindent', {})
1285
1286  call term_sendkeys(buf, "gg0")
1287  call VerifyScreenDump(buf, 'Test_diff_with_cul_bri_01', {})
1288  call term_sendkeys(buf, "j")
1289  call VerifyScreenDump(buf, 'Test_diff_with_cul_bri_02', {})
1290  call term_sendkeys(buf, "j")
1291  call VerifyScreenDump(buf, 'Test_diff_with_cul_bri_03', {})
1292  call term_sendkeys(buf, "j")
1293  call VerifyScreenDump(buf, 'Test_diff_with_cul_bri_04', {})
1294
1295  " clean up
1296  call StopVimInTerminal(buf)
1297endfunc
1298
1299func Test_diff_breakindent_after_filler()
1300  CheckScreendump
1301
1302  let lines =<< trim END
1303    set laststatus=0 diffopt+=followwrap breakindent breakindentopt=min:0
1304    call setline(1, ['a', '  ' .. repeat('c', 50)])
1305    vnew
1306    call setline(1, ['a', 'b', '  ' .. repeat('c', 50)])
1307    windo diffthis
1308    norm! G$
1309  END
1310  call writefile(lines, 'Xtest_diff_breakindent_after_filler', 'D')
1311  let buf = RunVimInTerminal('-S Xtest_diff_breakindent_after_filler', #{rows: 8, cols: 45})
1312  call VerifyScreenDump(buf, 'Test_diff_breakindent_after_filler', {})
1313
1314  " clean up
1315  call StopVimInTerminal(buf)
1316endfunc
1317
1318func Test_diff_with_syntax()
1319  CheckScreendump
1320
1321  let lines =<< trim END
1322	void doNothing() {
1323	   int x = 0;
1324	   char *s = "hello";
1325	   return 5;
1326	}
1327  END
1328  call writefile(lines, 'Xprogram1.c', 'D')
1329  let lines =<< trim END
1330	void doSomething() {
1331	   int x = 0;
1332	   char *s = "there";
1333	   return 5;
1334	}
1335  END
1336  call writefile(lines, 'Xprogram2.c', 'D')
1337
1338  let lines =<< trim END
1339    set diffopt=internal,filler
1340	edit Xprogram1.c
1341	diffsplit Xprogram2.c
1342  END
1343  call writefile(lines, 'Xtest_diff_syntax', 'D')
1344  let buf = RunVimInTerminal('-S Xtest_diff_syntax', {})
1345
1346  call VerifyScreenDump(buf, 'Test_diff_syntax_1', {})
1347
1348  " clean up
1349  call StopVimInTerminal(buf)
1350endfunc
1351
1352func Test_diff_of_diff()
1353  CheckScreendump
1354  CheckFeature rightleft
1355
1356  call writefile([
1357	\ 'call setline(1, ["aa","bb","cc","@@ -3,2 +5,7 @@","dd","ee","ff"])',
1358	\ 'vnew',
1359	\ 'call setline(1, ["aa","bb","cc"])',
1360	\ 'windo diffthis',
1361	\ '1wincmd w',
1362	\ 'setlocal number',
1363	\ ], 'Xtest_diff_diff', 'D')
1364  let buf = RunVimInTerminal('-S Xtest_diff_diff', {})
1365
1366  call VerifyScreenDump(buf, 'Test_diff_of_diff_01', {})
1367
1368  call term_sendkeys(buf, ":set rightleft\<cr>")
1369  call VerifyScreenDump(buf, 'Test_diff_of_diff_02', {})
1370
1371  " clean up
1372  call StopVimInTerminal(buf)
1373endfunc
1374
1375func CloseoffSetup()
1376  enew
1377  call setline(1, ['one', 'two', 'three'])
1378  diffthis
1379  new
1380  call setline(1, ['one', 'tow', 'three'])
1381  diffthis
1382  call assert_equal(1, &diff)
1383  bw!
1384endfunc
1385
1386func Test_diff_closeoff()
1387  " "closeoff" included by default: last diff win gets 'diff' reset'
1388  call CloseoffSetup()
1389  call assert_equal(0, &diff)
1390  enew!
1391
1392  " "closeoff" excluded: last diff win keeps 'diff' set'
1393  set diffopt-=closeoff
1394  call CloseoffSetup()
1395  call assert_equal(1, &diff)
1396  diffoff!
1397  enew!
1398endfunc
1399
1400func Test_diff_followwrap()
1401  new
1402  set diffopt+=followwrap
1403  set wrap
1404  diffthis
1405  call assert_equal(1, &wrap)
1406  diffoff
1407  set nowrap
1408  diffthis
1409  call assert_equal(0, &wrap)
1410  diffoff
1411  set diffopt&
1412  bwipe!
1413endfunc
1414
1415func Test_diff_maintains_change_mark()
1416  func DiffMaintainsChangeMark()
1417    enew!
1418    call setline(1, ['a', 'b', 'c', 'd'])
1419    diffthis
1420    new
1421    call setline(1, ['a', 'b', 'c', 'e'])
1422    " Set '[ and '] marks
1423    2,3yank
1424    call assert_equal([2, 3], [line("'["), line("']")])
1425    " Verify they aren't affected by the implicit diff
1426    diffthis
1427    call assert_equal([2, 3], [line("'["), line("']")])
1428    " Verify they aren't affected by an explicit diff
1429    diffupdate
1430    call assert_equal([2, 3], [line("'["), line("']")])
1431    bwipe!
1432    bwipe!
1433  endfunc
1434
1435  set diffopt-=internal
1436  call DiffMaintainsChangeMark()
1437  set diffopt+=internal
1438  call DiffMaintainsChangeMark()
1439
1440  set diffopt&
1441  delfunc DiffMaintainsChangeMark
1442endfunc
1443
1444" Test for 'patchexpr'
1445func Test_patchexpr()
1446  let g:patch_args = []
1447  func TPatch()
1448    call add(g:patch_args, readfile(v:fname_in))
1449    call add(g:patch_args, readfile(v:fname_diff))
1450    call writefile(['output file'], v:fname_out)
1451  endfunc
1452  set patchexpr=TPatch()
1453
1454  call writefile(['input file'], 'Xinput', 'D')
1455  call writefile(['diff file'], 'Xdiff', 'D')
1456  %bwipe!
1457  edit Xinput
1458  diffpatch Xdiff
1459  call assert_equal('output file', getline(1))
1460  call assert_equal('Xinput.new', bufname())
1461  call assert_equal(2, winnr('$'))
1462  call assert_true(&diff)
1463
1464  " Using a script-local function
1465  func s:NewPatchExpr()
1466  endfunc
1467  set patchexpr=s:NewPatchExpr()
1468  call assert_equal(expand('<SID>') .. 'NewPatchExpr()', &patchexpr)
1469  set patchexpr=<SID>NewPatchExpr()
1470  call assert_equal(expand('<SID>') .. 'NewPatchExpr()', &patchexpr)
1471
1472  set patchexpr&
1473  delfunc TPatch
1474  delfunc s:NewPatchExpr
1475  %bwipe!
1476endfunc
1477
1478func Test_diff_rnu()
1479  CheckScreendump
1480
1481  let content =<< trim END
1482    set diffopt=internal,filler
1483    call setline(1, ['a', 'a', 'a', 'y', 'b', 'b', 'b', 'b', 'b'])
1484    vnew
1485    call setline(1, ['a', 'a', 'a', 'x', 'x', 'x', 'b', 'b', 'b', 'b', 'b'])
1486    windo diffthis
1487    setlocal number rnu foldcolumn=0
1488  END
1489  call writefile(content, 'Xtest_diff_rnu', 'D')
1490  let buf = RunVimInTerminal('-S Xtest_diff_rnu', {})
1491
1492  call VerifyScreenDump(buf, 'Test_diff_rnu_01', {})
1493
1494  call term_sendkeys(buf, "j")
1495  call VerifyScreenDump(buf, 'Test_diff_rnu_02', {})
1496  call term_sendkeys(buf, "j")
1497  call VerifyScreenDump(buf, 'Test_diff_rnu_03', {})
1498
1499  " clean up
1500  call StopVimInTerminal(buf)
1501endfunc
1502
1503func Test_diff_multilineconceal()
1504  new
1505  diffthis
1506
1507  new
1508  call matchadd('Conceal', 'a\nb', 9, -1, {'conceal': 'Y'})
1509  set cole=2 cocu=n
1510  call setline(1, ["a", "b"])
1511  diffthis
1512  redraw
1513endfunc
1514
1515func Test_diff_and_scroll()
1516  " this was causing an ml_get error
1517  set ls=2
1518  for i in range(winheight(0) * 2)
1519    call setline(i, i < winheight(0) - 10 ? i : i + 10)
1520  endfor
1521  vnew
1522  for i in range(winheight(0)*2 + 10)
1523    call setline(i, i < winheight(0) - 10 ? 0 : i)
1524  endfor
1525  diffthis
1526  wincmd p
1527  diffthis
1528  execute 'normal ' . winheight(0) . "\<C-d>"
1529
1530  bwipe!
1531  bwipe!
1532  set ls&
1533endfunc
1534
1535func Test_diff_filler_cursorcolumn()
1536  CheckScreendump
1537
1538  let content =<< trim END
1539    call setline(1, ['aa', 'bb', 'cc'])
1540    vnew
1541    call setline(1, ['aa', 'cc'])
1542    windo diffthis
1543    wincmd p
1544    setlocal cursorcolumn foldcolumn=0
1545    norm! gg0
1546    redraw!
1547  END
1548  call writefile(content, 'Xtest_diff_cuc', 'D')
1549  let buf = RunVimInTerminal('-S Xtest_diff_cuc', {})
1550
1551  call VerifyScreenDump(buf, 'Test_diff_cuc_01', {})
1552
1553  call term_sendkeys(buf, "l")
1554  call term_sendkeys(buf, "\<C-l>")
1555  call VerifyScreenDump(buf, 'Test_diff_cuc_02', {})
1556  call term_sendkeys(buf, "0j")
1557  call term_sendkeys(buf, "\<C-l>")
1558  call VerifyScreenDump(buf, 'Test_diff_cuc_03', {})
1559  call term_sendkeys(buf, "l")
1560  call term_sendkeys(buf, "\<C-l>")
1561  call VerifyScreenDump(buf, 'Test_diff_cuc_04', {})
1562
1563  " clean up
1564  call StopVimInTerminal(buf)
1565endfunc
1566
1567" Test for adding/removing lines inside diff chunks, between diff chunks
1568" and before diff chunks
1569func Test_diff_modify_chunks()
1570  set diffopt=internal,filler
1571  enew!
1572  let w2_id = win_getid()
1573  call setline(1, ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'])
1574  new
1575  let w1_id = win_getid()
1576  call setline(1, ['a', '2', '3', 'd', 'e', 'f', '7', '8', 'i'])
1577  windo diffthis
1578
1579  " remove a line between two diff chunks and create a new diff chunk
1580  call win_gotoid(w2_id)
1581  5d
1582  call win_gotoid(w1_id)
1583  call diff_hlID(5, 1)->synIDattr('name')->assert_equal('DiffAdd')
1584
1585  " add a line between two diff chunks
1586  call win_gotoid(w2_id)
1587  normal! 4Goe
1588  call win_gotoid(w1_id)
1589  call diff_hlID(4, 1)->synIDattr('name')->assert_equal('')
1590  call diff_hlID(5, 1)->synIDattr('name')->assert_equal('')
1591
1592  " remove all the lines in a diff chunk.
1593  call win_gotoid(w2_id)
1594  7,8d
1595  call win_gotoid(w1_id)
1596  let hl = range(1, 9)->map({_, lnum -> diff_hlID(lnum, 1)->synIDattr('name')})
1597  call assert_equal(['', 'DiffText', 'DiffText', '', '', '', 'DiffAdd',
1598        \ 'DiffAdd', ''], hl)
1599
1600  " remove lines from one diff chunk to just before the next diff chunk
1601  call win_gotoid(w2_id)
1602  call setline(1, ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'])
1603  2,6d
1604  call win_gotoid(w1_id)
1605  let hl = range(1, 9)->map({_, lnum -> diff_hlID(lnum, 1)->synIDattr('name')})
1606  call assert_equal(['', 'DiffText', 'DiffText', 'DiffAdd', 'DiffAdd',
1607        \ 'DiffAdd', 'DiffAdd', 'DiffAdd', ''], hl)
1608
1609  " remove lines just before the top of a diff chunk
1610  call win_gotoid(w2_id)
1611  call setline(1, ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'])
1612  5,6d
1613  call win_gotoid(w1_id)
1614  let hl = range(1, 9)->map({_, lnum -> diff_hlID(lnum, 1)->synIDattr('name')})
1615  call assert_equal(['', 'DiffText', 'DiffText', '', 'DiffText', 'DiffText',
1616        \ 'DiffAdd', 'DiffAdd', ''], hl)
1617
1618  " remove line after the end of a diff chunk
1619  call win_gotoid(w2_id)
1620  call setline(1, ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'])
1621  4d
1622  call win_gotoid(w1_id)
1623  let hl = range(1, 9)->map({_, lnum -> diff_hlID(lnum, 1)->synIDattr('name')})
1624  call assert_equal(['', 'DiffText', 'DiffText', 'DiffAdd', '', '', 'DiffText',
1625        \ 'DiffText', ''], hl)
1626
1627  " remove lines starting from the end of one diff chunk and ending inside
1628  " another diff chunk
1629  call win_gotoid(w2_id)
1630  call setline(1, ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'])
1631  4,7d
1632  call win_gotoid(w1_id)
1633  let hl = range(1, 9)->map({_, lnum -> diff_hlID(lnum, 1)->synIDattr('name')})
1634  call assert_equal(['', 'DiffText', 'DiffText', 'DiffText', 'DiffAdd',
1635        \ 'DiffAdd', 'DiffAdd', 'DiffAdd', ''], hl)
1636
1637  " removing the only remaining diff chunk should make the files equal
1638  call win_gotoid(w2_id)
1639  call setline(1, ['a', '2', '3', 'x', 'd', 'e', 'f', 'x', '7', '8', 'i'])
1640  8d
1641  let hl = range(1, 10)->map({_, lnum -> diff_hlID(lnum, 1)->synIDattr('name')})
1642  call assert_equal(['', '', '', 'DiffAdd', '', '', '', '', '', ''], hl)
1643  call win_gotoid(w2_id)
1644  4d
1645  call win_gotoid(w1_id)
1646  let hl = range(1, 9)->map({_, lnum -> diff_hlID(lnum, 1)->synIDattr('name')})
1647  call assert_equal(['', '', '', '', '', '', '', '', ''], hl)
1648
1649  %bw!
1650  set diffopt&
1651endfunc
1652
1653func Test_diff_binary()
1654  CheckScreendump
1655
1656  let content =<< trim END
1657    call setline(1, ['a', 'b', "c\n", 'd', 'e', 'f', 'g'])
1658    vnew
1659    call setline(1, ['A', 'b', 'c', 'd', 'E', 'f', 'g'])
1660    windo diffthis
1661    wincmd p
1662    norm! gg0
1663    redraw!
1664  END
1665  call writefile(content, 'Xtest_diff_bin', 'D')
1666  let buf = RunVimInTerminal('-S Xtest_diff_bin', {})
1667
1668  " Test using internal diff
1669  call VerifyScreenDump(buf, 'Test_diff_bin_01', {})
1670
1671  " Test using internal diff and case folding
1672  call term_sendkeys(buf, ":set diffopt+=icase\<cr>")
1673  call term_sendkeys(buf, "\<C-l>")
1674  call VerifyScreenDump(buf, 'Test_diff_bin_02', {})
1675  " Test using external diff
1676  call term_sendkeys(buf, ":set diffopt=filler\<cr>")
1677  call term_sendkeys(buf, "\<C-l>")
1678  call VerifyScreenDump(buf, 'Test_diff_bin_03', {})
1679  " Test using external diff and case folding
1680  call term_sendkeys(buf, ":set diffopt=filler,icase\<cr>")
1681  call term_sendkeys(buf, "\<C-l>")
1682  call VerifyScreenDump(buf, 'Test_diff_bin_04', {})
1683
1684  " clean up
1685  call StopVimInTerminal(buf)
1686  set diffopt&vim
1687endfunc
1688
1689" Test for using the 'zi' command to invert 'foldenable' in diff windows (test
1690" for the issue fixed by patch 6.2.317)
1691func Test_diff_foldinvert()
1692  %bw!
1693  edit Xdoffile1
1694  new Xdoffile2
1695  new Xdoffile3
1696  windo diffthis
1697  " open a non-diff window
1698  botright new
1699  1wincmd w
1700  call assert_true(getwinvar(1, '&foldenable'))
1701  call assert_true(getwinvar(2, '&foldenable'))
1702  call assert_true(getwinvar(3, '&foldenable'))
1703  normal zi
1704  call assert_false(getwinvar(1, '&foldenable'))
1705  call assert_false(getwinvar(2, '&foldenable'))
1706  call assert_false(getwinvar(3, '&foldenable'))
1707  normal zi
1708  call assert_true(getwinvar(1, '&foldenable'))
1709  call assert_true(getwinvar(2, '&foldenable'))
1710  call assert_true(getwinvar(3, '&foldenable'))
1711
1712  " If the current window has 'noscrollbind', then 'zi' should not change
1713  " 'foldenable' in other windows.
1714  1wincmd w
1715  set noscrollbind
1716  normal zi
1717  call assert_false(getwinvar(1, '&foldenable'))
1718  call assert_true(getwinvar(2, '&foldenable'))
1719  call assert_true(getwinvar(3, '&foldenable'))
1720
1721  " 'zi' should not change the 'foldenable' for windows with 'noscrollbind'
1722  1wincmd w
1723  set scrollbind
1724  normal zi
1725  call setwinvar(2, '&scrollbind', v:false)
1726  normal zi
1727  call assert_false(getwinvar(1, '&foldenable'))
1728  call assert_true(getwinvar(2, '&foldenable'))
1729  call assert_false(getwinvar(3, '&foldenable'))
1730
1731  %bw!
1732  set scrollbind&
1733endfunc
1734
1735" This was scrolling for 'cursorbind' but 'scrollbind' is more important
1736func Test_diff_scroll()
1737  CheckScreendump
1738
1739  let left =<< trim END
1740      line 1
1741      line 2
1742      line 3
1743      line 4
1744
1745      // Common block
1746      // one
1747      // containing
1748      // four lines
1749
1750      // Common block
1751      // two
1752      // containing
1753      // four lines
1754  END
1755  call writefile(left, 'Xleft', 'D')
1756  let right =<< trim END
1757      line 1
1758      line 2
1759      line 3
1760      line 4
1761
1762      Lorem
1763      ipsum
1764      dolor
1765      sit
1766      amet,
1767      consectetur
1768      adipiscing
1769      elit.
1770      Etiam
1771      luctus
1772      lectus
1773      sodales,
1774      dictum
1775
1776      // Common block
1777      // one
1778      // containing
1779      // four lines
1780
1781      Vestibulum
1782      tincidunt
1783      aliquet
1784      nulla.
1785
1786      // Common block
1787      // two
1788      // containing
1789      // four lines
1790  END
1791  call writefile(right, 'Xright', 'D')
1792  let buf = RunVimInTerminal('-d Xleft Xright', {'rows': 12})
1793  call term_sendkeys(buf, "\<C-W>\<C-W>jjjj")
1794  call VerifyScreenDump(buf, 'Test_diff_scroll_1', {})
1795  call term_sendkeys(buf, "j")
1796  call VerifyScreenDump(buf, 'Test_diff_scroll_2', {})
1797
1798  call StopVimInTerminal(buf)
1799endfunc
1800
1801" This was scrolling too many lines.
1802func Test_diff_scroll_wrap_on()
1803  20new
1804  40vsplit
1805  call setline(1, map(range(1, 9), 'repeat(v:val, 200)'))
1806  setlocal number diff so=0
1807  redraw
1808  normal! jj
1809  call assert_equal(1, winsaveview().topline)
1810  normal! j
1811  call assert_equal(2, winsaveview().topline)
1812
1813  bwipe!
1814  bwipe!
1815endfunc
1816
1817func Test_diff_scroll_many_filler()
1818  20new
1819  vnew
1820  call setline(1, range(1, 40))
1821  diffthis
1822  setlocal scrolloff=0
1823  wincmd p
1824  call setline(1, range(1, 20)->reverse() + ['###']->repeat(41) + range(21, 40)->reverse())
1825  diffthis
1826  setlocal scrolloff=0
1827  wincmd p
1828  redraw
1829
1830  " Note: need a redraw after each scroll, otherwise the test always passes.
1831  for _ in range(2)
1832    normal! G
1833    redraw
1834    call assert_equal(40, winsaveview().topline)
1835    call assert_equal(19, winsaveview().topfill)
1836    exe "normal! \<C-B>"
1837    redraw
1838    call assert_equal(22, winsaveview().topline)
1839    call assert_equal(0, winsaveview().topfill)
1840    exe "normal! \<C-B>"
1841    redraw
1842    call assert_equal(4, winsaveview().topline)
1843    call assert_equal(0, winsaveview().topfill)
1844    exe "normal! \<C-B>"
1845    redraw
1846    call assert_equal(1, winsaveview().topline)
1847    call assert_equal(0, winsaveview().topfill)
1848    set smoothscroll
1849  endfor
1850
1851  set smoothscroll&
1852  %bwipe!
1853endfunc
1854
1855" This was trying to update diffs for a buffer being closed
1856func Test_diff_only()
1857  silent! lfile
1858  set diff
1859  lopen
1860  norm o
1861  silent! norm o
1862
1863  set nodiff
1864  %bwipe!
1865endfunc
1866
1867" This was causing invalid diff block values
1868" FIXME: somehow this causes a valgrind error when run directly but not when
1869" run as a test.
1870func Test_diff_manipulations()
1871  set diff
1872  split 0
1873  sil! norm R
doobdeuR
doobdeuR
doobdeu
1874
1875  set nodiff
1876  %bwipe!
1877endfunc
1878
1879" This was causing the line number in the diff block to go below one.
1880" FIXME: somehow this causes a valgrind error when run directly but not when
1881" run as a test.
1882func Test_diff_put_and_undo()
1883  set diff
1884  next 0
1885  split 00
1886  sil! norm o0gguudpo0ggJuudp
1887
1888  bwipe!
1889  bwipe!
1890  set nodiff
1891endfunc
1892
1893
1894func Test_diff_toggle_wrap_skipcol_leftcol()
1895  61vnew
1896  call setline(1, 'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.')
1897  30vnew
1898  call setline(1, 'ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.')
1899  let win1 = win_getid()
1900  setlocal smoothscroll
1901  exe "normal! $\<C-E>"
1902  wincmd l
1903  let win2 = win_getid()
1904  setlocal smoothscroll
1905  exe "normal! $\<C-E>"
1906  call assert_equal([
1907        \ '<<<sadipscing elitr, sed diam |<<<tetur sadipscing elitr, sed|',
1908        \ 'nonumy eirmod tempor invidunt | diam nonumy eirmod tempor inv|',
1909        \ 'ut labore et dolore magna aliq|idunt ut labore et dolore magn|',
1910        \ 'uyam erat, sed diam voluptua. |a aliquyam erat, sed diam volu|',
1911        \ '~                             |ptua.                         |',
1912        \ ], ScreenLines([1, 5], 62))
1913  call assert_equal({'col': 29, 'row': 4, 'endcol': 29, 'curscol': 29},
1914        \ screenpos(win1, line('.', win1), col('.', win1)))
1915  call assert_equal({'col': 36, 'row': 5, 'endcol': 36, 'curscol': 36},
1916        \ screenpos(win2, line('.', win2), col('.', win2)))
1917
1918  wincmd h
1919  diffthis
1920  wincmd l
1921  diffthis
1922  normal! 0
1923  call assert_equal([
1924        \ '  ipsum dolor sit amet, conset|  Lorem ipsum dolor sit amet, |',
1925        \ '~                             |~                             |',
1926        \ ], ScreenLines([1, 2], 62))
1927  call assert_equal({'col': 3, 'row': 1, 'endcol': 3, 'curscol': 3},
1928        \ screenpos(win1, line('.', win1), col('.', win1)))
1929  call assert_equal({'col': 34, 'row': 1, 'endcol': 34, 'curscol': 34},
1930        \ screenpos(win2, line('.', win2), col('.', win2)))
1931
1932  normal! $
1933  call assert_equal([
1934        \ '  voluptua.                   |   diam voluptua.             |',
1935        \ '~                             |~                             |',
1936        \ ], ScreenLines([1, 2], 62))
1937  call assert_equal({'col': 11, 'row': 1, 'endcol': 11, 'curscol': 11},
1938        \ screenpos(win1, line('.', win1), col('.', win1)))
1939  call assert_equal({'col': 48, 'row': 1, 'endcol': 48, 'curscol': 48},
1940        \ screenpos(win2, line('.', win2), col('.', win2)))
1941
1942  diffoff!
1943  call assert_equal([
1944        \ 'ipsum dolor sit amet, consetet|Lorem ipsum dolor sit amet, co|',
1945        \ 'ur sadipscing elitr, sed diam |nsetetur sadipscing elitr, sed|',
1946        \ 'nonumy eirmod tempor invidunt | diam nonumy eirmod tempor inv|',
1947        \ 'ut labore et dolore magna aliq|idunt ut labore et dolore magn|',
1948        \ 'uyam erat, sed diam voluptua. |a aliquyam erat, sed diam volu|',
1949        \ '~                             |ptua.                         |',
1950        \ ], ScreenLines([1, 6], 62))
1951  call assert_equal({'col': 29, 'row': 5, 'endcol': 29, 'curscol': 29},
1952        \ screenpos(win1, line('.', win1), col('.', win1)))
1953  call assert_equal({'col': 36, 'row': 6, 'endcol': 36, 'curscol': 36},
1954        \ screenpos(win2, line('.', win2), col('.', win2)))
1955
1956  bwipe!
1957  bwipe!
1958endfunc
1959
1960" Ctrl-D reveals filler lines below the last line in the buffer.
1961func Test_diff_eob_halfpage()
1962  new
1963  call setline(1, ['']->repeat(10) + ['a'])
1964  diffthis
1965  new
1966  call setline(1, ['']->repeat(3) + ['a', 'b'])
1967  diffthis
1968  resize 5
1969  wincmd j
1970  resize 5
1971  norm G
1972  call assert_equal(7, line('w0'))
1973  exe "norm! \<C-D>"
1974  call assert_equal(8, line('w0'))
1975
1976  %bwipe!
1977endfunc
1978
1979func Test_diff_overlapped_diff_blocks_will_be_merged()
1980  CheckScreendump
1981
1982  let lines =<< trim END
1983    func DiffExprStub()
1984      let txt_in = readfile(v:fname_in)
1985      let txt_new = readfile(v:fname_new)
1986      if txt_in == ["line1"] && txt_new == ["line2"]
1987        call writefile(["1c1"], v:fname_out)
1988      elseif txt_in == readfile("Xdiin1") && txt_new == readfile("Xdinew1")
1989        call writefile(readfile("Xdiout1"), v:fname_out)
1990      elseif txt_in == readfile("Xdiin2") && txt_new == readfile("Xdinew2")
1991        call writefile(readfile("Xdiout2"), v:fname_out)
1992      endif
1993    endfunc
1994  END
1995  call writefile(lines, 'XdiffSetup', 'D')
1996
1997  call WriteDiffFiles(0, [], [])
1998  let buf = RunVimInTerminal('-d -S XdiffSetup Xdifile1 Xdifile2', {})
1999  call term_sendkeys(buf, ":set autoread\<CR>\<c-w>w:set autoread\<CR>\<c-w>w")
2000
2001  call WriteDiffFiles(buf, ["a", "b"], ["x", "x"])
2002  call writefile(["a", "b"], "Xdiin1")
2003  call writefile(["x", "x"], "Xdinew1")
2004  call writefile(["1c1", "2c2"], "Xdiout1")
2005  call term_sendkeys(buf, ":set diffexpr=DiffExprStub()\<CR>:")
2006  call VerifyBoth(buf, "Test_diff_overlapped_2.01", "")
2007  call term_sendkeys(buf, ":set diffexpr&\<CR>:")
2008
2009  call WriteDiffFiles(buf, ["a", "b", "c"], ["x", "c"])
2010  call writefile(["a", "b", "c"], "Xdiin1")
2011  call writefile(["x", "c"], "Xdinew1")
2012  call writefile(["1c1", "2d1"], "Xdiout1")
2013  call term_sendkeys(buf, ":set diffexpr=DiffExprStub()\<CR>:")
2014  call VerifyBoth(buf, "Test_diff_overlapped_2.02", "")
2015  call term_sendkeys(buf, ":set diffexpr&\<CR>:")
2016
2017  call WriteDiffFiles(buf, ["a", "c"], ["x", "x", "c"])
2018  call writefile(["a", "c"], "Xdiin1")
2019  call writefile(["x", "x", "c"], "Xdinew1")
2020  call writefile(["1c1", "1a2"], "Xdiout1")
2021  call term_sendkeys(buf, ":set diffexpr=DiffExprStub()\<CR>:")
2022  call VerifyBoth(buf, "Test_diff_overlapped_2.03", "")
2023  call term_sendkeys(buf, ":set diffexpr&\<CR>:")
2024
2025  call StopVimInTerminal(buf)
2026  wincmd c
2027
2028  call WriteDiffFiles3(0, [], [], [])
2029  let buf = RunVimInTerminal('-d -S XdiffSetup Xdifile1 Xdifile2 Xdifile3', {})
2030  call term_sendkeys(buf, ":set autoread\<CR>\<c-w>w:set autoread\<CR>\<c-w>w:set autoread\<CR>\<c-w>w")
2031
2032  call WriteDiffFiles3(buf, ["a", "b", "c"], ["a", "x", "c"], ["y", "b", "c"])
2033  call VerifyBoth(buf, "Test_diff_overlapped_3.01", "")
2034
2035  call WriteDiffFiles3(buf, ["a", "b", "c"], ["a", "x", "c"], ["a", "y", "c"])
2036  call VerifyBoth(buf, "Test_diff_overlapped_3.02", "")
2037
2038  call WriteDiffFiles3(buf, ["a", "b", "c"], ["a", "x", "c"], ["a", "b", "y"])
2039  call VerifyBoth(buf, "Test_diff_overlapped_3.03", "")
2040
2041  call WriteDiffFiles3(buf, ["a", "b", "c"], ["a", "x", "c"], ["y", "y", "c"])
2042  call VerifyBoth(buf, "Test_diff_overlapped_3.04", "")
2043
2044  call WriteDiffFiles3(buf, ["a", "b", "c"], ["a", "x", "c"], ["a", "y", "y"])
2045  call VerifyBoth(buf, "Test_diff_overlapped_3.05", "")
2046
2047  call WriteDiffFiles3(buf, ["a", "b", "c"], ["a", "x", "c"], ["y", "y", "y"])
2048  call VerifyBoth(buf, "Test_diff_overlapped_3.06", "")
2049
2050  call WriteDiffFiles3(buf, ["a", "b", "c"], ["a", "x", "x"], ["y", "y", "c"])
2051  call VerifyBoth(buf, "Test_diff_overlapped_3.07", "")
2052
2053  call WriteDiffFiles3(buf, ["a", "b", "c"], ["x", "x", "c"], ["a", "y", "y"])
2054  call VerifyBoth(buf, "Test_diff_overlapped_3.08", "")
2055
2056  call WriteDiffFiles3(buf, ["a", "b", "c", "d", "e"], ["a", "x", "c", "x", "e"], ["y", "y", "y", "d", "e"])
2057  call VerifyBoth(buf, "Test_diff_overlapped_3.09", "")
2058
2059  call WriteDiffFiles3(buf, ["a", "b", "c", "d", "e"], ["a", "x", "c", "x", "e"], ["y", "y", "y", "y", "e"])
2060  call VerifyBoth(buf, "Test_diff_overlapped_3.10", "")
2061
2062  call WriteDiffFiles3(buf, ["a", "b", "c", "d", "e"], ["a", "x", "c", "x", "e"], ["y", "y", "y", "y", "y"])
2063  call VerifyBoth(buf, "Test_diff_overlapped_3.11", "")
2064
2065  call WriteDiffFiles3(buf, ["a", "b", "c", "d", "e"], ["a", "x", "c", "x", "e"], ["a", "y", "y", "d", "e"])
2066  call VerifyBoth(buf, "Test_diff_overlapped_3.12", "")
2067
2068  call WriteDiffFiles3(buf, ["a", "b", "c", "d", "e"], ["a", "x", "c", "x", "e"], ["a", "y", "y", "y", "e"])
2069  call VerifyBoth(buf, "Test_diff_overlapped_3.13", "")
2070
2071  call WriteDiffFiles3(buf, ["a", "b", "c", "d", "e"], ["a", "x", "c", "x", "e"], ["a", "y", "y", "y", "y"])
2072  call VerifyBoth(buf, "Test_diff_overlapped_3.14", "")
2073
2074  call WriteDiffFiles3(buf, ["a", "b", "c", "d", "e"], ["a", "x", "c", "x", "e"], ["a", "b", "y", "d", "e"])
2075  call VerifyBoth(buf, "Test_diff_overlapped_3.15", "")
2076
2077  call WriteDiffFiles3(buf, ["a", "b", "c", "d", "e"], ["a", "x", "c", "x", "e"], ["a", "b", "y", "y", "e"])
2078  call VerifyBoth(buf, "Test_diff_overlapped_3.16", "")
2079
2080  call WriteDiffFiles3(buf, ["a", "b", "c", "d", "e"], ["a", "x", "c", "x", "e"], ["a", "b", "y", "y", "y"])
2081  call VerifyBoth(buf, "Test_diff_overlapped_3.17", "")
2082
2083  call WriteDiffFiles3(buf, ["a", "b"], ["x", "b"], ["y", "y"])
2084  call writefile(["a", "b"], "Xdiin1")
2085  call writefile(["x", "b"], "Xdinew1")
2086  call writefile(["1c1"], "Xdiout1")
2087  call writefile(["a", "b"], "Xdiin2")
2088  call writefile(["y", "y"], "Xdinew2")
2089  call writefile(["1c1", "2c2"], "Xdiout2")
2090  call term_sendkeys(buf, ":set diffexpr=DiffExprStub()\<CR>:")
2091  call VerifyInternal(buf, "Test_diff_overlapped_3.18", "")
2092  call term_sendkeys(buf, ":set diffexpr&\<CR>:")
2093
2094  call WriteDiffFiles3(buf, ["a", "b", "c", "d"], ["x", "b", "x", "d"], ["y", "y", "c", "d"])
2095  call writefile(["a", "b", "c", "d"], "Xdiin1")
2096  call writefile(["x", "b", "x", "d"], "Xdinew1")
2097  call writefile(["1c1", "3c3"], "Xdiout1")
2098  call writefile(["a", "b", "c", "d"], "Xdiin2")
2099  call writefile(["y", "y", "c", "d"], "Xdinew2")
2100  call writefile(["1c1", "2c2"], "Xdiout2")
2101  call term_sendkeys(buf, ":set diffexpr=DiffExprStub()\<CR>:")
2102  call VerifyInternal(buf, "Test_diff_overlapped_3.19", "")
2103  call term_sendkeys(buf, ":set diffexpr&\<CR>:")
2104
2105  call WriteDiffFiles3(buf, ["a", "b", "c", "d"], ["x", "b", "x", "d"], ["y", "y", "y", "d"])
2106  call writefile(["a", "b", "c", "d"], "Xdiin1")
2107  call writefile(["x", "b", "x", "d"], "Xdinew1")
2108  call writefile(["1c1", "3c3"], "Xdiout1")
2109  call writefile(["a", "b", "c", "d"], "Xdiin2")
2110  call writefile(["y", "y", "y", "d"], "Xdinew2")
2111  call writefile(["1c1", "2,3c2,3"], "Xdiout2")
2112  call term_sendkeys(buf, ":set diffexpr=DiffExprStub()\<CR>:")
2113  call VerifyInternal(buf, "Test_diff_overlapped_3.20", "")
2114  call term_sendkeys(buf, ":set diffexpr&\<CR>:")
2115
2116  call WriteDiffFiles3(buf, ["a", "b", "c", "d"], ["x", "b", "x", "d"], ["y", "y", "y", "y"])
2117  call writefile(["a", "b", "c", "d"], "Xdiin1")
2118  call writefile(["x", "b", "x", "d"], "Xdinew1")
2119  call writefile(["1c1", "3c3"], "Xdiout1")
2120  call writefile(["a", "b", "c", "d"], "Xdiin2")
2121  call writefile(["y", "y", "y", "y"], "Xdinew2")
2122  call writefile(["1c1", "2,4c2,4"], "Xdiout2")
2123  call term_sendkeys(buf, ":set diffexpr=DiffExprStub()\<CR>:")
2124  call VerifyInternal(buf, "Test_diff_overlapped_3.21", "")
2125  call term_sendkeys(buf, ":set diffexpr&\<CR>:")
2126
2127  call WriteDiffFiles3(buf, ["a", "b", "c"], ["a", "x", "c"], ["b", "c"])
2128  call VerifyBoth(buf, "Test_diff_overlapped_3.22", "")
2129
2130  call WriteDiffFiles3(buf, ["a", "b", "c"], ["a", "x", "c"], ["c"])
2131  call VerifyBoth(buf, "Test_diff_overlapped_3.23", "")
2132
2133  call WriteDiffFiles3(buf, ["a", "b", "c"], ["a", "x", "c"], [])
2134  call VerifyBoth(buf, "Test_diff_overlapped_3.24", "")
2135
2136  call WriteDiffFiles3(buf, ["a", "b", "c"], ["a", "x", "c"], ["a", "c"])
2137  call VerifyBoth(buf, "Test_diff_overlapped_3.25", "")
2138
2139  call WriteDiffFiles3(buf, ["a", "b", "c"], ["a", "x", "c"], ["a"])
2140  call VerifyBoth(buf, "Test_diff_overlapped_3.26", "")
2141
2142  call WriteDiffFiles3(buf, ["a", "b", "c"], ["a", "x", "c"], ["b"])
2143  call VerifyBoth(buf, "Test_diff_overlapped_3.27", "")
2144
2145  call WriteDiffFiles3(buf, ["a", "b", "c", "d", "e"], ["a", "x", "c", "x", "e"], ["d", "e"])
2146  call VerifyBoth(buf, "Test_diff_overlapped_3.28", "")
2147
2148  call WriteDiffFiles3(buf, ["a", "b", "c", "d", "e"], ["a", "x", "c", "x", "e"], ["e"])
2149  call VerifyBoth(buf, "Test_diff_overlapped_3.29", "")
2150
2151  call WriteDiffFiles3(buf, ["a", "b", "c", "d", "e"], ["a", "x", "c", "x", "e"], ["a", "d", "e"])
2152  call VerifyBoth(buf, "Test_diff_overlapped_3.30", "")
2153
2154  call WriteDiffFiles3(buf, ["a", "b", "c", "d", "e"], ["a", "x", "c", "x", "e"], ["a", "e"])
2155  call VerifyBoth(buf, "Test_diff_overlapped_3.31", "")
2156
2157  call WriteDiffFiles3(buf, ["a", "b", "c", "d", "e"], ["a", "x", "c", "x", "e"], ["a"])
2158  call VerifyBoth(buf, "Test_diff_overlapped_3.32", "")
2159
2160  call WriteDiffFiles3(buf, ["a", "b", "c", "d", "e"], ["a", "x", "c", "x", "e"], ["a", "b", "d", "e"])
2161  call VerifyBoth(buf, "Test_diff_overlapped_3.33", "")
2162
2163  call WriteDiffFiles3(buf, ["a", "b", "c", "d", "e"], ["a", "x", "c", "x", "e"], ["a", "b", "e"])
2164  call VerifyBoth(buf, "Test_diff_overlapped_3.34", "")
2165
2166  call WriteDiffFiles3(buf, ["a", "b", "c", "d", "e"], ["a", "x", "c", "x", "e"], ["a", "b"])
2167  call VerifyBoth(buf, "Test_diff_overlapped_3.35", "")
2168
2169  call WriteDiffFiles3(buf, ["a", "b", "c"], ["a", "x", "c"], ["a", "y", "b", "c"])
2170  call VerifyBoth(buf, "Test_diff_overlapped_3.36", "")
2171
2172  call WriteDiffFiles3(buf, ["a", "b", "c"], ["a", "x", "c"], ["a", "b", "y", "c"])
2173  call VerifyBoth(buf, "Test_diff_overlapped_3.37", "")
2174
2175  call WriteDiffFiles3(buf, ["a", "b", "c"], ["d", "e"], ["b", "f"])
2176  call VerifyBoth(buf, "Test_diff_overlapped_3.38", "")
2177
2178  call WriteDiffFiles3(buf, ["a", "b", "c"], ["d", "e"], ["b"])
2179  call VerifyBoth(buf, "Test_diff_overlapped_3.39", "")
2180
2181  " File 3 overlaps twice, 2nd overlap completely within the existing block.
2182  call WriteDiffFiles3(buf, ["foo", "a", "b", "c", "bar"], ["foo", "w", "x", "y", "z", "bar"], ["foo", "1", "a", "b", "2", "bar"])
2183  call VerifyBoth(buf, "Test_diff_overlapped_3.40", "")
2184
2185  " File 3 overlaps twice, 2nd overlap extends beyond existing block on new
2186  " side. Make sure we don't over-extend the range and hit 'bar'.
2187  call WriteDiffFiles3(buf, ["foo", "a", "b", "c", "d", "bar"], ["foo", "w", "x", "y", "z", "u", "bar"], ["foo", "1", "a", "b", "2", "d", "bar"])
2188  call VerifyBoth(buf, "Test_diff_overlapped_3.41", "")
2189
2190  " Chained overlaps. File 3's 2nd overlap spans two diff blocks and is longer
2191  " than the 2nd one.
2192  call WriteDiffFiles3(buf, ["foo", "a", "b", "c", "d", "e", "f", "bar"], ["foo", "w", "x", "y", "z", "e", "u", "bar"], ["foo", "1", "b", "2", "3", "d", "4", "f", "bar"])
2193  call VerifyBoth(buf, "Test_diff_overlapped_3.42", "")
2194
2195  " File 3 has 2 overlaps. An add and a delete. First overlap's expansion hits
2196  " the 2nd one. Make sure we adjust the diff block to have fewer lines.
2197  call WriteDiffFiles3(buf, ["foo", "a", "b", "bar"], ["foo", "x", "y", "bar"], ["foo", "1", "a", "bar"])
2198  call VerifyBoth(buf, "Test_diff_overlapped_3.43", "")
2199
2200  " File 3 has 2 overlaps. An add and another add. First overlap's expansion hits
2201  " the 2nd one. Make sure we adjust the diff block to have more lines.
2202  call WriteDiffFiles3(buf, ["foo", "a", "b", "c", "d", "bar"], ["foo", "w", "x", "y", "z", "u", "bar"], ["foo", "1", "a", "b", "3", "4", "d", "bar"])
2203  call VerifyBoth(buf, "Test_diff_overlapped_3.44", "")
2204
2205  call StopVimInTerminal(buf)
2206endfunc
2207
2208" switching windows in diff mode caused an unnecessary scroll
2209func Test_diff_topline_noscroll()
2210  CheckScreendump
2211
2212  let content =<< trim END
2213    call setline(1, range(1,60))
2214    vnew
2215    call setline(1, range(1,10) + range(50,60))
2216    windo diffthis
2217    norm! G
2218    exe "norm! 30\<C-y>"
2219  END
2220  call writefile(content, 'Xcontent', 'D')
2221  let buf = RunVimInTerminal('-S Xcontent', {'rows': 20})
2222  call VerifyScreenDump(buf, 'Test_diff_topline_1', {})
2223  call term_sendkeys(buf, ":echo line('w0', 1001)\<cr>")
2224  call term_wait(buf)
2225  call VerifyScreenDump(buf, 'Test_diff_topline_2', {})
2226  call term_sendkeys(buf, "\<C-W>p")
2227  call term_wait(buf)
2228  call VerifyScreenDump(buf, 'Test_diff_topline_3', {})
2229  call term_sendkeys(buf, "\<C-W>p")
2230  call term_wait(buf)
2231  call VerifyScreenDump(buf, 'Test_diff_topline_4', {})
2232  call StopVimInTerminal(buf)
2233endfunc
2234
2235" Test inline highlighting which shows what's different within each diff block
2236func Test_diff_inline()
2237  CheckScreendump
2238
2239  call WriteDiffFiles(0, [], [])
2240  let buf = RunVimInTerminal('-d Xdifile1 Xdifile2', {})
2241  call term_sendkeys(buf, ":set autoread\<CR>\<c-w>w:set autoread\<CR>\<c-w>w")
2242
2243  call WriteDiffFiles(buf, ["abcdef ghi jk n", "x", "y"], ["aBcef gHi lm n", "y", "z"])
2244  call VerifyInternal(buf, "Test_diff_inline_01", "")
2245  call VerifyInternal(buf, "Test_diff_inline_02", " diffopt+=inline:none")
2246
2247  " inline:simple is the same as default
2248  call VerifyInternal(buf, "Test_diff_inline_01", " diffopt+=inline:simple")
2249
2250  call VerifyInternal(buf, "Test_diff_inline_03", " diffopt+=inline:char")
2251  call VerifyInternal(buf, "Test_diff_inline_04", " diffopt+=inline:word")
2252
2253  " multiple inline values will the last one
2254  call VerifyInternal(buf, "Test_diff_inline_01", " diffopt+=inline:none,inline:char,inline:simple")
2255  call VerifyInternal(buf, "Test_diff_inline_02", " diffopt+=inline:simple,inline:word,inline:none")
2256  call VerifyInternal(buf, "Test_diff_inline_03", " diffopt+=inline:simple,inline:word,inline:char")
2257
2258  " DiffTextAdd highlight
2259  call term_sendkeys(buf, ":hi DiffTextAdd ctermbg=blue\<CR>")
2260  call VerifyInternal(buf, "Test_diff_inline_05", " diffopt+=inline:char")
2261
2262  " Live update in insert mode
2263  call term_sendkeys(buf, "\<Esc>isometext")
2264  call VerifyScreenDump(buf, "Test_diff_inline_06", {})
2265  call term_sendkeys(buf, "\<Esc>u")
2266
2267  " icase simple scenarios
2268  call VerifyInternal(buf, "Test_diff_inline_07", " diffopt+=inline:simple,icase")
2269  call VerifyInternal(buf, "Test_diff_inline_08", " diffopt+=inline:char,icase")
2270  call VerifyInternal(buf, "Test_diff_inline_09", " diffopt+=inline:word,icase")
2271
2272  " diff algorithms should affect highlight
2273  call WriteDiffFiles(buf, ["apples and oranges"], ["oranges and apples"])
2274  call VerifyInternal(buf, "Test_diff_inline_10", " diffopt+=inline:char")
2275  call VerifyInternal(buf, "Test_diff_inline_11", " diffopt+=inline:char,algorithm:patience")
2276
2277  " icase: composing chars and Unicode fold case edge cases
2278  call WriteDiffFiles(buf,
2279        \ ["1 - sigma in 6σ and Ὀδυσσεύς", "1 - angstrom in åå", "1 - composing: ii⃗I⃗"],
2280        \ ["2 - Sigma in 6Σ and ὈΔΥΣΣΕΎΣ", "2 - Angstrom in ÅÅ", "2 - Composing: i⃗I⃗I⃗"])
2281  call VerifyInternal(buf, "Test_diff_inline_12", " diffopt+=inline:char")
2282  call VerifyInternal(buf, "Test_diff_inline_13", " diffopt+=inline:char,icase")
2283
2284  " wide chars
2285  call WriteDiffFiles(buf, ["abc😅xde一", "f🚀g"], ["abcy😢de", "二f🚀g"])
2286  call VerifyInternal(buf, "Test_diff_inline_14", " diffopt+=inline:char,icase")
2287
2288  " NUL char (\n below is internally substituted as NUL)
2289  call WriteDiffFiles(buf, ["1\n34\n5\n6"], ["1234\n5", "6"])
2290  call VerifyInternal(buf, "Test_diff_inline_15", " diffopt+=inline:char")
2291
2292  " word diff: always use first buffer's iskeyword and ignore others' for consistency
2293  call WriteDiffFiles(buf, ["foo+bar test"], ["foo+baz test"])
2294  call VerifyInternal(buf, "Test_diff_inline_word_01", " diffopt+=inline:word")
2295
2296  call term_sendkeys(buf, ":set iskeyword+=+\<CR>:diffupdate\<CR>")
2297  call VerifyInternal(buf, "Test_diff_inline_word_02", " diffopt+=inline:word")
2298
2299  call term_sendkeys(buf, ":set iskeyword&\<CR>:wincmd w\<CR>")
2300  call term_sendkeys(buf, ":set iskeyword+=+\<CR>:wincmd w\<CR>:diffupdate\<CR>")
2301  " Use the previous screen dump as 2nd buffer's iskeyword does not matter
2302  call VerifyInternal(buf, "Test_diff_inline_word_01", " diffopt+=inline:word")
2303
2304  call term_sendkeys(buf, ":windo set iskeyword&\<CR>:1wincmd w\<CR>")
2305
2306  " word diff: test handling of multi-byte characters. Only alphanumeric chars
2307  " (e.g. Greek alphabet, but not CJK/emoji) count as words.
2308  call WriteDiffFiles(buf, ["🚀⛵️一二三ひらがなΔέλτα Δelta foobar"], ["🚀🛸一二四ひらなδέλτα δelta foobar"])
2309  call VerifyInternal(buf, "Test_diff_inline_word_03", " diffopt+=inline:word")
2310
2311  " char diff: should slide highlight to whitespace boundary if possible for
2312  " better readability (by using forced indent-heuristics). A wrong result
2313  " would be if the highlight is "Bar, prefix". It should be "prefixBar, "
2314  " instead.
2315  call WriteDiffFiles(buf, ["prefixFoo, prefixEnd"], ["prefixFoo, prefixBar, prefixEnd"])
2316  call VerifyInternal(buf, "Test_diff_inline_char_01", " diffopt+=inline:char")
2317
2318  " char diff: small gaps between inline diff blocks will be merged during refine step
2319  " - first segment: test that we iteratively merge small gaps after we merged
2320  "   adjacent blocks, but only with limited number (set to 4) of iterations.
2321  " - second and third segments: show that we need a large enough adjacent block to
2322  "   trigger a merge.
2323  " - fourth segment: small gaps are not merged when adjacent large block is
2324  "   on a different line.
2325  call WriteDiffFiles(buf,
2326        \ ["abcdefghijklmno", "anchor1",
2327        \  "abcdefghijklmno", "anchor2",
2328        \  "abcdefghijklmno", "anchor3",
2329        \  "test", "multiline"],
2330        \ ["a?c?e?g?i?k???o", "anchor1",
2331        \  "a??de?????klmno", "anchor2",
2332        \  "a??de??????lmno", "anchor3",
2333        \  "t?s?", "??????i?e"])
2334  call VerifyInternal(buf, "Test_diff_inline_char_02", " diffopt+=inline:char")
2335
2336  " Test multi-line blocks and whitespace
2337  call WriteDiffFiles(buf,
2338        \ ["this   is   ", "sometest text foo", "baz abc def ", "one", "word another word", "additional line"],
2339        \ ["this is some test", "texts", "foo bar abX Yef     ", "oneword another word"])
2340  call VerifyInternal(buf, "Test_diff_inline_multiline_01", " diffopt+=inline:char,iwhite")
2341  call VerifyInternal(buf, "Test_diff_inline_multiline_02", " diffopt+=inline:word,iwhite")
2342  call VerifyInternal(buf, "Test_diff_inline_multiline_03", " diffopt+=inline:char,iwhiteeol")
2343  call VerifyInternal(buf, "Test_diff_inline_multiline_04", " diffopt+=inline:word,iwhiteeol")
2344  call VerifyInternal(buf, "Test_diff_inline_multiline_05", " diffopt+=inline:char,iwhiteall")
2345  call VerifyInternal(buf, "Test_diff_inline_multiline_06", " diffopt+=inline:word,iwhiteall")
2346
2347  " newline should be highlighted too when 'list' is set
2348  call term_sendkeys(buf, ":windo set list\<CR>")
2349  call VerifyInternal(buf, "Test_diff_inline_multiline_07", " diffopt+=inline:char")
2350  call VerifyInternal(buf, "Test_diff_inline_multiline_08", " diffopt+=inline:char,iwhite")
2351  call VerifyInternal(buf, "Test_diff_inline_multiline_09", " diffopt+=inline:char,iwhiteeol")
2352  call VerifyInternal(buf, "Test_diff_inline_multiline_10", " diffopt+=inline:char,iwhiteall")
2353  call term_sendkeys(buf, ":windo set nolist\<CR>")
2354
2355  call StopVimInTerminal(buf)
2356endfunc
2357
2358func Test_diff_inline_multibuffer()
2359  CheckScreendump
2360
2361  call WriteDiffFiles3(0, [], [], [])
2362  let buf = RunVimInTerminal('-d Xdifile1 Xdifile2 Xdifile3', {})
2363  call term_sendkeys(buf, ":windo set autoread\<CR>:1wincmd w\<CR>")
2364  call term_sendkeys(buf, ":hi DiffTextAdd ctermbg=blue\<CR>")
2365
2366  call WriteDiffFiles3(buf,
2367        \ ["That is buffer1.", "anchor", "Some random text", "anchor"],
2368        \ ["This is buffer2.", "anchor", "Some text", "anchor", "buffer2/3"],
2369        \ ["This is buffer3. Last.", "anchor", "Some more", "text here.", "anchor", "only in buffer2/3", "not in buffer1"])
2370  call VerifyInternal(buf, "Test_diff_inline_multibuffer_01", " diffopt+=inline:char")
2371
2372  " Close one of the buffers and make sure it updates correctly
2373  call term_sendkeys(buf, ":diffoff\<CR>")
2374  call VerifyInternal(buf, "Test_diff_inline_multibuffer_02", " diffopt+=inline:char")
2375
2376  " Update text in the non-diff buffer and nothing should be changed
2377  call term_sendkeys(buf, "\<Esc>isometext")
2378  call VerifyScreenDump(buf, "Test_diff_inline_multibuffer_03", {})
2379  call term_sendkeys(buf, "\<Esc>u")
2380
2381  call term_sendkeys(buf, ":diffthis\<CR>")
2382  call VerifyInternal(buf, "Test_diff_inline_multibuffer_01", " diffopt+=inline:char")
2383
2384  " Test that removing first buffer from diff will in turn use the next
2385  " earliest buffer's iskeyword during word diff.
2386  call WriteDiffFiles3(buf,
2387        \ ["This+is=a-setence"],
2388        \ ["This+is=another-setence"],
2389        \ ["That+is=a-setence"])
2390  call term_sendkeys(buf, ":set iskeyword+=+\<CR>:2wincmd w\<CR>:set iskeyword+=-\<CR>:1wincmd w\<CR>")
2391  call VerifyInternal(buf, "Test_diff_inline_multibuffer_04", " diffopt+=inline:word")
2392  call term_sendkeys(buf, ":diffoff\<CR>")
2393  call VerifyInternal(buf, "Test_diff_inline_multibuffer_05", " diffopt+=inline:word")
2394  call term_sendkeys(buf, ":diffthis\<CR>")
2395  call VerifyInternal(buf, "Test_diff_inline_multibuffer_04", " diffopt+=inline:word")
2396
2397  " Test multi-buffer char diff refinement, and that removing a buffer from
2398  " diff will update the others properly.
2399  call WriteDiffFiles3(buf,
2400        \ ["abcdefghijkYmYYY"],
2401        \ ["aXXdXXghijklmnop"],
2402        \ ["abcdefghijkYmYop"])
2403  call VerifyInternal(buf, "Test_diff_inline_multibuffer_06", " diffopt+=inline:char")
2404  call term_sendkeys(buf, ":diffoff\<CR>")
2405  call VerifyInternal(buf, "Test_diff_inline_multibuffer_07", " diffopt+=inline:char")
2406  call term_sendkeys(buf, ":diffthis\<CR>")
2407  call VerifyInternal(buf, "Test_diff_inline_multibuffer_06", " diffopt+=inline:char")
2408
2409  call StopVimInTerminal(buf)
2410endfunc
2411
2412" Test that when using multi-buffer diff, an empty block would be correctly
2413" skipped in the result, without resulting in invalid states or crashes.
2414func Test_diff_inline_multibuffer_empty_block()
2415  CheckScreendump
2416
2417  call writefile(['anchor1', '1234567890abcde', 'anchor2'], 'Xdifile1')
2418  call writefile(['anchor1', '1234567--0abc-e', 'anchor2'], 'Xdifile2')
2419  call writefile(['anchor1', 'anchor2'], 'Xdifile3')
2420  call writefile(['anchor1', '1???????90abcd?', 'anchor2'], 'Xdifile4')
2421
2422  let buf = RunVimInTerminal('-d Xdifile1 Xdifile2 Xdifile3 Xdifile4', {})
2423  call VerifyInternal(buf, "Test_diff_inline_multibuffer_empty_block_01", " diffopt+=inline:char")
2424
2425  call StopVimInTerminal(buf)
2426endfunc
2427
2428" An inline:word diff block that starts with inserted words has df_count zero
2429" for the first buffer, then the index of its last line is negative.  This
2430" caused a heap-buffer underflow
2431func Test_diff_inline_word_empty_block()
2432  set diffopt=internal,filler,inline:word
2433  enew!
2434  call setline(1, ['b c', 'y z'])
2435  diffthis
2436  vnew
2437  call setline(1, ['a b c d e', 'x y z w v'])
2438  diffthis
2439  redraw
2440
2441  " Part of the line is highlighted, not all of it.
2442  let attrs = map(range(1, 9), 'screenattr(1, v:val)')
2443  call assert_true(len(uniq(sort(attrs))) > 1)
2444
2445  diffoff!
2446  set diffopt&
2447  bwipe!
2448  bwipe!
2449endfunc
2450
2451func Test_diffget_diffput_linematch()
2452  CheckScreendump
2453  call delete('.Xdifile1.swp')
2454  call delete('.Xdifile2.swp')
2455  call WriteDiffFiles(0, [], [])
2456  let buf = RunVimInTerminal('-d Xdifile1 Xdifile2', {})
2457  call term_sendkeys(buf, ":set autoread\<CR>\<c-w>w:set autoread\<CR>\<c-w>w")
2458
2459  " enable linematch
2460  call term_sendkeys(buf, ":set diffopt+=linematch:30\<CR>")
2461  call WriteDiffFiles(buf, ['',
2462      \ 'common line',
2463      \ 'common line',
2464      \ '',
2465      \ 'ABCabc',
2466      \ 'ABCabc',
2467      \ 'ABCabc',
2468      \ 'ABCabc',
2469      \ 'common line',
2470      \ 'common line',
2471      \ 'common line',
2472      \ 'something' ],
2473      \ ['',
2474      \ 'common line',
2475      \ 'common line',
2476      \ '',
2477      \ 'DEFabc',
2478      \ 'xyz',
2479      \ 'xyz',
2480      \ 'xyz',
2481      \ 'DEFabc',
2482      \ 'DEFabc',
2483      \ 'DEFabc',
2484      \ 'common line',
2485      \ 'common line',
2486      \ 'DEF',
2487      \ 'common line',
2488      \ 'DEF',
2489      \ 'something'])
2490  call VerifyScreenDump(buf, 'Test_diff_get_put_linematch_1', {})
2491
2492  " get from window 1 from line 5 to 9
2493  call term_sendkeys(buf, "1\<c-w>w")
2494  call term_sendkeys(buf, ":5,9diffget\<CR>")
2495  call VerifyScreenDump(buf, 'Test_diff_get_put_linematch_2', {})
2496
2497  " undo the last diffget
2498  call term_sendkeys(buf, "u")
2499
2500  " get from window 2 from line 5 to 10
2501  call term_sendkeys(buf, "2\<c-w>w")
2502  call term_sendkeys(buf, ":5,10diffget\<CR>")
2503  call VerifyScreenDump(buf, 'Test_diff_get_put_linematch_3', {})
2504
2505  " undo the last diffget
2506  call term_sendkeys(buf, "u")
2507
2508  " get all from window 2
2509  call term_sendkeys(buf, "2\<c-w>w")
2510  call term_sendkeys(buf, ":4,17diffget\<CR>")
2511  call VerifyScreenDump(buf, 'Test_diff_get_put_linematch_4', {})
2512
2513  " undo the last diffget
2514  call term_sendkeys(buf, "u")
2515
2516  " get all from window 1
2517  call term_sendkeys(buf, "1\<c-w>w")
2518  call term_sendkeys(buf, ":4,12diffget\<CR>")
2519  call VerifyScreenDump(buf, 'Test_diff_get_put_linematch_5', {})
2520
2521  " undo the last diffget
2522  call term_sendkeys(buf, "u")
2523
2524  " get from window 1 using do 1 line 5
2525  call term_sendkeys(buf, "1\<c-w>w")
2526  call term_sendkeys(buf, "5gg")
2527  call term_sendkeys(buf, ":diffget\<CR>")
2528  call VerifyScreenDump(buf, 'Test_diff_get_put_linematch_6', {})
2529
2530  " undo the last diffget
2531  call term_sendkeys(buf, "u")
2532
2533  " get from window 1 using do 2 line 6
2534  call term_sendkeys(buf, "1\<c-w>w")
2535  call term_sendkeys(buf, "6gg")
2536  call term_sendkeys(buf, ":diffget\<CR>")
2537  call VerifyScreenDump(buf, 'Test_diff_get_put_linematch_7', {})
2538
2539  " undo the last diffget
2540  call term_sendkeys(buf, "u")
2541
2542  " get from window 1 using do 2 line 7
2543  call term_sendkeys(buf, "1\<c-w>w")
2544  call term_sendkeys(buf, "7gg")
2545  call term_sendkeys(buf, ":diffget\<CR>")
2546  call VerifyScreenDump(buf, 'Test_diff_get_put_linematch_8', {})
2547
2548  " undo the last diffget
2549  call term_sendkeys(buf, "u")
2550
2551  " get from window 1 using do 2 line 11
2552  call term_sendkeys(buf, "1\<c-w>w")
2553  call term_sendkeys(buf, "11gg")
2554  call term_sendkeys(buf, ":diffget\<CR>")
2555  call VerifyScreenDump(buf, 'Test_diff_get_put_linematch_9', {})
2556
2557  " undo the last diffget
2558  call term_sendkeys(buf, "u")
2559
2560  " get from window 1 using do 2 line 12
2561  call term_sendkeys(buf, "1\<c-w>w")
2562  call term_sendkeys(buf, "12gg")
2563  call term_sendkeys(buf, ":diffget\<CR>")
2564  call VerifyScreenDump(buf, 'Test_diff_get_put_linematch_10', {})
2565
2566  " undo the last diffget
2567  call term_sendkeys(buf, "u")
2568
2569  " put from window 1 using dp 1 line 5
2570  call term_sendkeys(buf, "1\<c-w>w")
2571  call term_sendkeys(buf, "5gg")
2572  call term_sendkeys(buf, ":diffput\<CR>")
2573  call VerifyScreenDump(buf, 'Test_diff_get_put_linematch_11', {})
2574
2575  " undo the last diffput
2576  call term_sendkeys(buf, "2\<c-w>w")
2577  call term_sendkeys(buf, "u")
2578
2579  " put from window 1 using dp 2 line 6
2580  call term_sendkeys(buf, "1\<c-w>w")
2581  call term_sendkeys(buf, "6gg")
2582  call term_sendkeys(buf, ":diffput\<CR>")
2583  call VerifyScreenDump(buf, 'Test_diff_get_put_linematch_12', {})
2584
2585  " undo the last diffput
2586  call term_sendkeys(buf, "2\<c-w>w")
2587  call term_sendkeys(buf, "u")
2588
2589  " put from window 1 using dp 2 line 7
2590  call term_sendkeys(buf, "1\<c-w>w")
2591  call term_sendkeys(buf, "7gg")
2592  call term_sendkeys(buf, ":diffput\<CR>")
2593  call VerifyScreenDump(buf, 'Test_diff_get_put_linematch_13', {})
2594
2595  " undo the last diffput
2596  call term_sendkeys(buf, "2\<c-w>w")
2597  call term_sendkeys(buf, "u")
2598
2599  " put from window 1 using dp 2 line 11
2600  call term_sendkeys(buf, "1\<c-w>w")
2601  call term_sendkeys(buf, "11gg")
2602  call term_sendkeys(buf, ":diffput\<CR>")
2603  call VerifyScreenDump(buf, 'Test_diff_get_put_linematch_14', {})
2604
2605  " undo the last diffput
2606  call term_sendkeys(buf, "2\<c-w>w")
2607  call term_sendkeys(buf, "u")
2608
2609  " put from window 1 using dp 2 line 12
2610  call term_sendkeys(buf, "1\<c-w>w")
2611  call term_sendkeys(buf, "12gg")
2612  call term_sendkeys(buf, ":diffput\<CR>")
2613  call VerifyScreenDump(buf, 'Test_diff_get_put_linematch_15', {})
2614
2615  " undo the last diffput
2616  call term_sendkeys(buf, "2\<c-w>w")
2617  call term_sendkeys(buf, "u")
2618
2619  " put from window 2 using dp line 6
2620  call term_sendkeys(buf, "2\<c-w>w")
2621  call term_sendkeys(buf, "6gg")
2622  call term_sendkeys(buf, ":diffput\<CR>")
2623  call VerifyScreenDump(buf, 'Test_diff_get_put_linematch_16', {})
2624
2625  " undo the last diffput
2626  call term_sendkeys(buf, "1\<c-w>w")
2627  call term_sendkeys(buf, "u")
2628
2629  " put from window 2 using dp line 8
2630  call term_sendkeys(buf, "2\<c-w>w")
2631  call term_sendkeys(buf, "8gg")
2632  call term_sendkeys(buf, ":diffput\<CR>")
2633  call VerifyScreenDump(buf, 'Test_diff_get_put_linematch_17', {})
2634
2635  " undo the last diffput
2636  call term_sendkeys(buf, "1\<c-w>w")
2637  call term_sendkeys(buf, "u")
2638
2639  " put from window 2 using dp line 9
2640  call term_sendkeys(buf, "2\<c-w>w")
2641  call term_sendkeys(buf, "9gg")
2642  call term_sendkeys(buf, ":diffput\<CR>")
2643  call VerifyScreenDump(buf, 'Test_diff_get_put_linematch_18', {})
2644
2645  " undo the last diffput
2646  call term_sendkeys(buf, "1\<c-w>w")
2647  call term_sendkeys(buf, "u")
2648
2649  " put from window 2 using dp line 17
2650  call term_sendkeys(buf, "2\<c-w>w")
2651  call term_sendkeys(buf, "17gg")
2652  call term_sendkeys(buf, ":diffput\<CR>")
2653  call VerifyScreenDump(buf, 'Test_diff_get_put_linematch_19', {})
2654  " clean up
2655  call StopVimInTerminal(buf)
2656endfunc
2657
2658func Test_linematch_diff()
2659  CheckScreendump
2660  call delete('.Xdifile1.swp')
2661  call delete('.Xdifile2.swp')
2662  call WriteDiffFiles(0, [], [])
2663  let buf = RunVimInTerminal('-d Xdifile1 Xdifile2', {})
2664  call term_sendkeys(buf, ":set autoread\<CR>\<c-w>w:set autoread\<CR>\<c-w>w")
2665
2666  " enable linematch
2667  call term_sendkeys(buf, ":set diffopt=internal,filler,linematch:30\<CR>")
2668  call WriteDiffFiles(buf, ['// abc d?',
2669      \ '// d?',
2670      \ '// d?' ],
2671      \ ['!',
2672      \ 'abc d!',
2673      \ 'd!'])
2674  call term_sendkeys(buf, ":\<CR>") " clear cmdline
2675  call VerifyScreenDump(buf, 'Test_linematch_diff1', {})
2676
2677  " test that filler is always implicitly set by linematch
2678  call term_sendkeys(buf, ":set diffopt-=filler\<CR>")
2679  call term_sendkeys(buf, ":\<CR>") " clear cmdline
2680  call VerifyScreenDump(buf, 'Test_linematch_diff1', {})
2681
2682  " clean up
2683  call StopVimInTerminal(buf)
2684endfunc
2685
2686func Test_linematch_diff_iwhite()
2687  CheckScreendump
2688  call delete('.Xdifile1.swp')
2689  call delete('.Xdifile2.swp')
2690  call WriteDiffFiles(0, [], [])
2691  let buf = RunVimInTerminal('-d Xdifile1 Xdifile2', {})
2692  call term_sendkeys(buf, ":set autoread\<CR>\<c-w>w:set autoread\<CR>\<c-w>w")
2693
2694  " setup a diff with 2 files and set linematch:30, with ignore white
2695  call term_sendkeys(buf, ":set diffopt=internal,filler,linematch:30\<CR>")
2696  call WriteDiffFiles(buf, ['void testFunction () {',
2697      \ '  for (int i = 0; i < 10; i++) {',
2698      \ '    for (int j = 0; j < 10; j++) {',
2699      \ '    }',
2700      \ '  }',
2701      \ '}' ],
2702      \ ['void testFunction () {',
2703      \ '  // for (int j = 0; j < 10; i++) {',
2704      \ '  // }',
2705      \ '}'])
2706  call VerifyScreenDump(buf, 'Test_linematch_diff_iwhite1', {})
2707  call term_sendkeys(buf, ":set diffopt+=iwhiteall\<CR>")
2708  call VerifyScreenDump(buf, 'Test_linematch_diff_iwhite2', {})
2709  " clean up
2710  call StopVimInTerminal(buf)
2711endfunc
2712
2713func Test_linematch_diff_grouping()
2714  CheckScreendump
2715  call delete('.Xdifile1.swp')
2716  call delete('.Xdifile2.swp')
2717  call WriteDiffFiles(0, [], [])
2718  let buf = RunVimInTerminal('-d Xdifile1 Xdifile2', {})
2719  call term_sendkeys(buf, ":set autoread\<CR>\<c-w>w:set autoread\<CR>\<c-w>w")
2720
2721  " a diff that would result in multiple groups before grouping optimization
2722  call term_sendkeys(buf, ":set diffopt=internal,filler,linematch:30\<CR>")
2723  call WriteDiffFiles(buf, ['!A',
2724      \ '!B',
2725      \ '!C' ],
2726      \ ['?Z',
2727      \ '?A',
2728      \ '?B',
2729      \ '?C',
2730      \ '?A',
2731      \ '?B',
2732      \ '?B',
2733      \ '?C'])
2734  call VerifyScreenDump(buf, 'Test_linematch_diff_grouping1', {})
2735  call WriteDiffFiles(buf, ['!A',
2736      \ '!B',
2737      \ '!C' ],
2738      \ ['?A',
2739      \ '?Z',
2740      \ '?B',
2741      \ '?C',
2742      \ '?A',
2743      \ '?B',
2744      \ '?C',
2745      \ '?C'])
2746  call VerifyScreenDump(buf, 'Test_linematch_diff_grouping2', {})
2747  " clean up
2748  call StopVimInTerminal(buf)
2749endfunc
2750
2751func Test_linematch_diff_scroll()
2752  CheckScreendump
2753  call delete('.Xdifile1.swp')
2754  call delete('.Xdifile2.swp')
2755  call WriteDiffFiles(0, [], [])
2756  let buf = RunVimInTerminal('-d Xdifile1 Xdifile2', {})
2757  call term_sendkeys(buf, ":set autoread\<CR>\<c-w>w:set autoread\<CR>\<c-w>w")
2758
2759  " a diff that would result in multiple groups before grouping optimization
2760  call term_sendkeys(buf, ":set diffopt=internal,filler,linematch:30\<CR>")
2761  call WriteDiffFiles(buf, ['!A',
2762      \ '!B',
2763      \ '!C' ],
2764      \ ['?A',
2765      \ '?Z',
2766      \ '?B',
2767      \ '?C',
2768      \ '?A',
2769      \ '?B',
2770      \ '?C',
2771      \ '?C'])
2772  " scroll down to show calculation of top fill and scroll to correct line in
2773  " both windows
2774  call VerifyScreenDump(buf, 'Test_linematch_diff_grouping_scroll0', {})
2775  call term_sendkeys(buf, "3\<c-e>")
2776  call VerifyScreenDump(buf, 'Test_linematch_diff_grouping_scroll1', {})
2777  call term_sendkeys(buf, "3\<c-e>")
2778  call VerifyScreenDump(buf, 'Test_linematch_diff_grouping_scroll2', {})
2779  " clean up
2780  call StopVimInTerminal(buf)
2781endfunc
2782
2783func Test_linematch_line_limit_exceeded()
2784  CheckScreendump
2785  call delete('.Xdifile1.swp')
2786  call delete('.Xdifile2.swp')
2787  call WriteDiffFiles(0, [], [])
2788  let buf = RunVimInTerminal('-d Xdifile1 Xdifile2', {})
2789  call term_sendkeys(buf, ":set autoread\<CR>\<c-w>w:set autoread\<CR>\<c-w>w")
2790
2791  call term_sendkeys(buf, ":set diffopt=internal,filler,linematch:10\<CR>")
2792  " a diff block will not be aligned with linematch because it's contents
2793  " exceed 10 lines
2794  call WriteDiffFiles(buf,
2795        \ ['common line',
2796        \ 'HIL',
2797        \ '',
2798        \ 'aABCabc',
2799        \ 'aABCabc',
2800        \ 'aABCabc',
2801        \ 'aABCabc',
2802        \ 'common line',
2803        \ 'HIL',
2804        \ 'common line',
2805        \ 'something'],
2806        \ ['common line',
2807        \ 'DEF',
2808        \ 'GHI',
2809        \ 'something',
2810        \ '',
2811        \ 'aDEFabc',
2812        \ 'xyz',
2813        \ 'xyz',
2814        \ 'xyz',
2815        \ 'aDEFabc',
2816        \ 'aDEFabc',
2817        \ 'aDEFabc',
2818        \ 'common line',
2819        \ 'DEF',
2820        \ 'GHI',
2821        \ 'something else',
2822        \ 'common line',
2823        \ 'something'])
2824  call VerifyScreenDump(buf, 'Test_linematch_line_limit_exceeded1', {})
2825  " after increasing the count to 30, the limit is not exceeded, and the
2826  " alignment algorithm will run on the largest diff block here
2827  call term_sendkeys(buf, ":set diffopt+=linematch:30\<CR>")
2828  call VerifyScreenDump(buf, 'Test_linematch_line_limit_exceeded2', {})
2829  " clean up
2830  call StopVimInTerminal(buf)
2831endfunc
2832
2833func Test_linematch_3diffs()
2834  CheckScreendump
2835  call delete('.Xdifile1.swp')
2836  call delete('.Xdifile2.swp')
2837  call delete('.Xdifile3.swp')
2838  call WriteDiffFiles3(0, [], [], [])
2839  let buf = RunVimInTerminal('-d Xdifile1 Xdifile2 Xdifile3', {})
2840  call term_sendkeys(buf, "1\<c-w>w:set autoread\<CR>")
2841  call term_sendkeys(buf, "2\<c-w>w:set autoread\<CR>")
2842  call term_sendkeys(buf, "3\<c-w>w:set autoread\<CR>")
2843  call term_sendkeys(buf, ":set diffopt=internal,filler,linematch:30\<CR>")
2844  call WriteDiffFiles3(buf,
2845        \ ["",
2846        \ "  common line",
2847        \ "      AAA",
2848        \ "      AAA",
2849        \ "      AAA"],
2850        \ ["",
2851        \ "  common line",
2852        \ "  <<<<<<< HEAD",
2853        \ "      AAA",
2854        \ "      AAA",
2855        \ "      AAA",
2856        \ "  =======",
2857        \ "      BBB",
2858        \ "      BBB",
2859        \ "      BBB",
2860        \ "  >>>>>>> branch1"],
2861        \ ["",
2862        \ "  common line",
2863        \ "      BBB",
2864        \ "      BBB",
2865        \ "      BBB"])
2866  call VerifyScreenDump(buf, 'Test_linematch_3diffs1', {})
2867  " clean up
2868  call StopVimInTerminal(buf)
2869endfunc
2870
2871" this used to access invalid memory
2872func Test_linematch_3diffs_sanity_check()
2873  CheckScreendump
2874  call delete('.Xfile_linematch1.swp')
2875  call delete('.Xfile_linematch2.swp')
2876  call delete('.Xfile_linematch3.swp')
2877  let lines =<< trim END
2878    set diffopt=internal,filler,linematch:60
2879    call feedkeys("Aq\<esc>")
2880    call feedkeys("GAklm\<esc>")
2881    call feedkeys("o")
2882  END
2883  call writefile(lines, 'Xlinematch_3diffs.vim', 'D')
2884  call writefile(['abcd', 'def', 'hij'], 'Xfile_linematch1', 'D')
2885  call writefile(['defq', 'hijk', 'nopq'], 'Xfile_linematch2', 'D')
2886  call writefile(['hijklm', 'nopqr', 'stuv'], 'Xfile_linematch3', 'D')
2887  call WriteDiffFiles3(0, [], [], [])
2888  let buf = RunVimInTerminal('-d -S Xlinematch_3diffs.vim Xfile_linematch1 Xfile_linematch2 Xfile_linematch3', {})
2889  call VerifyScreenDump(buf, 'Test_linematch_3diffs2', {})
2890
2891  " clean up
2892  call StopVimInTerminal(buf)
2893endfunc
2894
2895func Test_diffanchors()
2896  CheckScreendump
2897  call WriteDiffFiles3(0,
2898      \ ["anchorA1", "1", "2", "3",
2899      \  "100", "101", "102", "anchorB", "103", "104", "105"],
2900      \ ["100", "101", "102", "anchorB", "103", "104", "105",
2901      \  "anchorA2", "1", "2", "3"],
2902      \ ["100", "anchorB", "103",
2903      \  "anchorA3", "1", "2", "3"])
2904  let buf = RunVimInTerminal('-d Xdifile1 Xdifile2 Xdifile3', {})
2905
2906  " Simple diff without any anchors
2907  call VerifyInternal(buf, "Test_diff_anchors_00", "")
2908
2909  " Setting diffopt+=anchor or diffanchors without the other won't do anything
2910  call VerifyInternal(buf, "Test_diff_anchors_00", " diffopt+=anchor")
2911  call VerifyInternal(buf, "Test_diff_anchors_00", " dia=1/anchorA/")
2912
2913  " Use a single anchor by specifying a pattern. Test both internal and
2914  " external diff to make sure both paths work.
2915  call VerifyBoth(buf, "Test_diff_anchors_01", " dia=1/anchorA/ diffopt+=anchor")
2916
2917  " Use 2 anchors. They should be sorted by line number, so in file 2/3
2918  " anchorB is used before anchorA.
2919  call VerifyBoth(buf, "Test_diff_anchors_02", " dia=1/anchorA/,1/anchorB/ diffopt+=anchor")
2920
2921  " Set marks and specify addresses using marks and repeat the test
2922  call term_sendkeys(buf, ":2wincmd w\<CR>:1/anchorA/mark a\<CR>")
2923  call term_sendkeys(buf, ":1/anchorB/mark b\<CR>")
2924  call term_sendkeys(buf, ":3wincmd w\<CR>:1/anchorA/mark a\<CR>")
2925  call term_sendkeys(buf, ":1/anchorB/mark b\<CR>")
2926  call term_sendkeys(buf, ":1wincmd w\<CR>:1/anchorA/mark a\<CR>")
2927  call term_sendkeys(buf, ":1/anchorB/mark b\<CR>")
2928
2929  call VerifyInternal(buf, "Test_diff_anchors_01", " dia='a diffopt+=anchor")
2930  call VerifyInternal(buf, "Test_diff_anchors_02", " dia='a,'b diffopt+=anchor")
2931
2932  " Update marks to point somewhere else. When we first set the mark the diff
2933  " won't be updated until we manually invoke :diffupdate.
2934  call VerifyInternal(buf, "Test_diff_anchors_01", " dia='a diffopt+=anchor")
2935  call term_sendkeys(buf, ":1wincmd w\<CR>:1/anchorB/mark a\<CR>:")
2936  call term_wait(buf)
2937  call VerifyScreenDump(buf, "Test_diff_anchors_01", {})
2938  call term_sendkeys(buf, ":diffupdate\<CR>:")
2939  call term_wait(buf)
2940  call VerifyScreenDump(buf, "Test_diff_anchors_03", {})
2941
2942  " Use local diff anchors with line numbers, and repeat the same test
2943  call term_sendkeys(buf, ":2wincmd w\<CR>:setlocal dia=8\<CR>")
2944  call term_sendkeys(buf, ":3wincmd w\<CR>:setlocal dia=4\<CR>")
2945  call term_sendkeys(buf, ":1wincmd w\<CR>:setlocal dia=1\<CR>")
2946  call VerifyInternal(buf, "Test_diff_anchors_01", " diffopt+=anchor")
2947  call term_sendkeys(buf, ":2wincmd w\<CR>:setlocal dia=8,4\<CR>")
2948  call term_sendkeys(buf, ":3wincmd w\<CR>:setlocal dia=4,2\<CR>")
2949  call term_sendkeys(buf, ":1wincmd w\<CR>:setlocal dia=1,8\<CR>")
2950  call VerifyInternal(buf, "Test_diff_anchors_02", " diffopt+=anchor")
2951
2952  " Test multiple diff anchors on the same line in file 1.
2953  call term_sendkeys(buf, ":1wincmd w\<CR>:setlocal dia=1,1\<CR>")
2954  call VerifyInternal(buf, "Test_diff_anchors_04", " diffopt+=anchor")
2955
2956  " Test that if one file has fewer diff anchors than others. Vim should only
2957  " use the minimum in this case.
2958  call term_sendkeys(buf, ":1wincmd w\<CR>:setlocal dia=8\<CR>")
2959  call VerifyInternal(buf, "Test_diff_anchors_05", " diffopt+=anchor")
2960
2961  " $+1 should anchor everything past the last line
2962  call term_sendkeys(buf, ":1wincmd w\<CR>:setlocal dia=$+1\<CR>")
2963  call VerifyInternal(buf, "Test_diff_anchors_06", " diffopt+=anchor")
2964
2965  " Sorting of diff anchors should work with multiple anchors
2966  call term_sendkeys(buf, ":1wincmd w\<CR>:setlocal dia=1,10,8,2\<CR>")
2967  call term_sendkeys(buf, ":2wincmd w\<CR>:setlocal dia=1,2,3,4\<CR>")
2968  call term_sendkeys(buf, ":3wincmd w\<CR>:setlocal dia=4,3,2,1\<CR>")
2969  call VerifyInternal(buf, "Test_diff_anchors_07", " diffopt+=anchor")
2970
2971  " Intentionally set an invalid anchor with wrong line number. Should fall
2972  " back to treat it as if no anchors are used at all.
2973  call term_sendkeys(buf, ":1wincmd w\<CR>:setlocal dia=1,10,8,2,1000 | silent! diffupdate\<CR>:")
2974  call VerifyScreenDump(buf, "Test_diff_anchors_00", {})
2975
2976  call StopVimInTerminal(buf)
2977endfunc
2978
2979" Test that scrollbind and topline calculations work correctly, even when diff
2980" anchors create adjacent diff blocks which complicates the calculations.
2981func Test_diffanchors_scrollbind_topline()
2982  CheckScreendump
2983
2984  " Simple overlapped line anchored to be adjacent to each other
2985  call WriteDiffFiles(0,
2986      \ ["anchor1", "diff1a", "anchor2"],
2987      \ ["anchor1", "diff2a", "anchor2"])
2988  let buf = RunVimInTerminal('-d Xdifile1 Xdifile2', {})
2989
2990  call term_sendkeys(buf, ":1wincmd w\<CR>:setlocal dia=2\<CR>")
2991  call term_sendkeys(buf, ":2wincmd w\<CR>:setlocal dia=3\<CR>")
2992
2993  call VerifyInternal(buf, "Test_diff_anchors_scrollbind_topline_01", " diffopt+=anchor")
2994  call term_sendkeys(buf, "\<Esc>\<C-E>")
2995  call VerifyScreenDump(buf, "Test_diff_anchors_scrollbind_topline_02", {})
2996  call term_sendkeys(buf, "\<C-E>")
2997  call VerifyScreenDump(buf, "Test_diff_anchors_scrollbind_topline_03", {})
2998  call term_sendkeys(buf, "\<C-E>")
2999  call VerifyScreenDump(buf, "Test_diff_anchors_scrollbind_topline_04", {})
3000
3001  " Also test no-filler
3002  call term_sendkeys(buf, "gg")
3003  call VerifyInternal(buf, "Test_diff_anchors_scrollbind_topline_05", " diffopt+=anchor diffopt-=filler")
3004  call term_sendkeys(buf, "\<Esc>\<C-E>")
3005  call VerifyScreenDump(buf, "Test_diff_anchors_scrollbind_topline_06", {})
3006  call term_sendkeys(buf, "\<C-E>")
3007  call VerifyScreenDump(buf, "Test_diff_anchors_scrollbind_topline_07", {})
3008
3009  call StopVimInTerminal(buf)
3010endfunc
3011
3012func Test_diffanchors_scrollbind_topline2()
3013  CheckScreendump
3014
3015  " More-complicated case with 3 files and multiple overlapping diff blocks
3016  call WriteDiffFiles3(0,
3017      \ ["anchor1"],
3018      \ ["diff2a", "diff2b", "diff2c", "diff2d", "anchor2"],
3019      \ ["diff3a", "diff3c", "diff3d", "anchor3", "diff3e"])
3020  let buf = RunVimInTerminal('-d Xdifile1 Xdifile2 Xdifile3', {})
3021
3022  call term_sendkeys(buf, ":1wincmd w\<CR>:setlocal dia=1,1,2\<CR>")
3023  call term_sendkeys(buf, ":2wincmd w\<CR>:setlocal dia=3,5,6\<CR>")
3024  call term_sendkeys(buf, ":3wincmd w\<CR>:setlocal dia=2,4,5\<CR>")
3025
3026  call VerifyInternal(buf, "Test_diff_anchors_scrollbind_topline_08", " diffopt+=anchor")
3027  call term_sendkeys(buf, ":1wincmd w\<CR>")
3028  call term_sendkeys(buf, "\<C-E>")
3029  call VerifyScreenDump(buf, "Test_diff_anchors_scrollbind_topline_09", {})
3030  call term_sendkeys(buf, "\<C-E>")
3031  call VerifyScreenDump(buf, "Test_diff_anchors_scrollbind_topline_10", {})
3032  call term_sendkeys(buf, "\<C-E>")
3033  call VerifyScreenDump(buf, "Test_diff_anchors_scrollbind_topline_11", {})
3034  call term_sendkeys(buf, "\<C-E>")
3035  call VerifyScreenDump(buf, "Test_diff_anchors_scrollbind_topline_12", {})
3036
3037  " Also test no-filler
3038  call term_sendkeys(buf, ":3wincmd w\<CR>gg")
3039  call VerifyInternal(buf, "Test_diff_anchors_scrollbind_topline_13", " diffopt+=anchor diffopt-=filler")
3040  call term_sendkeys(buf, "\<Esc>\<C-E>")
3041  call VerifyScreenDump(buf, "Test_diff_anchors_scrollbind_topline_14", {})
3042  call term_sendkeys(buf, "\<C-E>")
3043  call VerifyScreenDump(buf, "Test_diff_anchors_scrollbind_topline_15", {})
3044  call term_sendkeys(buf, "\<C-E>")
3045  call VerifyScreenDump(buf, "Test_diff_anchors_scrollbind_topline_16", {})
3046  call term_sendkeys(buf, "\<C-E>")
3047  call VerifyScreenDump(buf, "Test_diff_anchors_scrollbind_topline_17", {})
3048
3049  call StopVimInTerminal(buf)
3050endfunc
3051
3052" Test that setting 'diffanchors' will update the diff.
3053func Test_diffanchors_option_set_update()
3054  set diffanchors='a diffopt=internal,filler,anchor
3055
3056  " Set up 3 tabs that share some buffers, and set up marks on each of them.
3057  " We want to make sure only relevant tabs are updated if buffer-local diff
3058  " anchors are updated, but all tabs should refresh if global diff anchors
3059  " are updated (see diffanchors_changed() in code).
3060
3061  " Tab 1. A buffer here will be reused.
3062  call setline(1, range(1, 10))
3063  3mark a
3064  4mark b
3065  diffthis
3066  new
3067  call setline(1, range(21, 25))
3068  let buf = bufnr()
3069  1mark a
3070  2mark b
3071  diffthis
3072  call assert_equal(2, diff_filler(1))
3073  call assert_equal(0, diff_filler(2))
3074
3075  " Tab 2. "buf" is here but intentionally not participating in diff.
3076  tabnew
3077  exec 'buf ' .. buf
3078  diffoff
3079  new
3080  call setline(1, range(31, 40))
3081  8mark a
3082  9mark b
3083  diffthis
3084  new
3085  call setline(1, range(41, 50))
3086  5mark a
3087  6mark b
3088  diffthis
3089
3090  call assert_equal(3, diff_filler(5))
3091  call assert_equal(0, diff_filler(6))
3092  call assert_equal(0, diff_filler(7))
3093
3094  " Update mark a location, and check that the diff has *not* updated. When
3095  " updating marks diff's won't automatically update.
3096  7mark a
3097  call assert_equal(3, diff_filler(5))
3098  call assert_equal(0, diff_filler(6))
3099  call assert_equal(0, diff_filler(7))
3100
3101  " Tab 3. "buf" is used here and also in a diff.
3102  tabnew
3103  call setline(1, range(51, 65))
3104  10mark a
3105  11mark b
3106  diffthis
3107  exec 'sbuffer ' .. buf
3108  diffthis
3109
3110  " Change local diff anchor of "buf" to mark b
3111  setlocal diffanchors='b
3112
3113  " Tab 1 should immediately update the diff to use mark b because the buf
3114  " local diff anchor has been changed in "buf".
3115  1tabnext
3116  call assert_equal(0, diff_filler(1))
3117  call assert_equal(1, diff_filler(2))
3118
3119  " Tab 2 should not immediately update because "buf" is not a diff buffer
3120  " here.
3121  2tabnext
3122  call assert_equal(3, diff_filler(5))
3123  call assert_equal(0, diff_filler(6))
3124  call assert_equal(0, diff_filler(7))
3125
3126  " Manual diff update would refresh the diff since we previously changed mark
3127  " a's location above.
3128  diffupdate
3129  call assert_equal(0, diff_filler(5))
3130  call assert_equal(0, diff_filler(6))
3131  call assert_equal(1, diff_filler(7))
3132
3133  " Go back to tab 1. Reset diff anchor to global value and make sure it uses
3134  " mark a again.
3135  1tabnext
3136  set diffanchors<
3137  call assert_equal(2, diff_filler(1))
3138  call assert_equal(0, diff_filler(2))
3139
3140  " Now, change the global diff anchor to mark b. This should affect all tabs
3141  " including tab 2 which should update automatically.
3142  set diffanchors='b
3143  call assert_equal(0, diff_filler(1))
3144  call assert_equal(2, diff_filler(2))
3145
3146  2tabnext
3147  call assert_equal(0, diff_filler(5))
3148  call assert_equal(3, diff_filler(6))
3149  call assert_equal(0, diff_filler(7))
3150
3151  %bw!
3152  set diffopt&
3153  set diffanchors&
3154endfunc
3155
3156" Test that using diff anchors with window/buffer-local addresses will work as
3157" expected and use the relevant window/buffer instead of curbuf/curwin.
3158func Test_diffanchors_buf_win_local_addresses()
3159  " Win 1-3 point to buffer 1. Set up different window-specific jump history
3160  " Win 2 is the one we activate diff mode on.
3161  call setline(1, range(1, 15))
3162  norm 2gg
3163  norm 3gg
3164
3165  split
3166  norm 4gg
3167  norm 5gg
3168
3169  split
3170  norm 11gg
3171  norm 12gg
3172  call setline(10, 'new text 1') " update the '. mark to line 10
3173
3174  " Win 4 points to buffer 2
3175  botright vert new
3176  call setline(1, range(101, 110))
3177  norm 8gg
3178  norm 9gg
3179  call setline(3, 'new text 2') " update the '. mark to line 3
3180
3181  2wincmd w
3182  diffthis
3183  4wincmd w
3184  diffthis
3185
3186  " Test buffer-local marks using '. Should be anchored to lines 10 / 3.
3187  set diffopt=internal,filler,anchor
3188  set diffanchors='.
3189  4wincmd w
3190  call assert_equal(7, diff_filler(3))
3191
3192  " Test window-local marks using '' Should be anchored to lines 4 / 8.
3193  " Note that windows 1 & 3 point to the buffer being diff'ed but are not used
3194  " for diffing themselves and therefore should not be used. Windows 2 & 4
3195  " should be used.
3196  set diffanchors=''
3197  2wincmd w
3198  call assert_equal(4, diff_filler(4))
3199
3200  " Also test "." for the current cursor position, which is also
3201  " window-specific. Make sure the cursor position at the longer file doesn't
3202  " result in the other file using out of bounds line number.
3203  4wincmd w
3204  norm G
3205  2wincmd w
3206  norm G
3207  set diffanchors=.
3208  diffupdate
3209  4wincmd w
3210  call assert_equal(5, diff_filler(10))
3211
3212  %bw!
3213  set diffopt&
3214  set diffanchors&
3215endfunc
3216
3217" Test diff anchors error handling for anchors that fail to resolve to a line.
3218" These are not handled during option parsing because they depend on the
3219" specifics of the buffer at diff time.
3220func Test_diffanchors_invalid()
3221  call setline(1, range(1, 5))
3222  new
3223  call setline(1, range(11, 20))
3224  set diffopt=internal,filler,anchor
3225  windo diffthis
3226  1wincmd w
3227
3228  " Line numbers that are out of bounds should be an error
3229  set diffanchors=0
3230  call assert_fails('diffupdate', 'E16:')
3231  set diffanchors=1
3232  diffupdate
3233  set diffanchors=$
3234  diffupdate
3235  set diffanchors=$+1
3236  diffupdate
3237  set diffanchors=$+2
3238  call assert_fails('diffupdate', 'E16:')
3239
3240  " Test that non-existent marks in any one buffer will be detected
3241  set diffanchors='a
3242  call assert_fails('diffupdate', 'E20:')
3243  2mark a
3244  call assert_fails('diffupdate', 'E20:')
3245
3246  set diffanchors=1
3247  setlocal diffanchors='a
3248  diffupdate
3249
3250  set diffanchors<
3251  windo 2mark a
3252  set diffanchors='b
3253  call assert_fails('diffupdate', 'E20:')
3254  set diffanchors='a
3255  diffupdate
3256
3257  " File marks are ok to use for anchors only if it is in the same file
3258  1wincmd w
3259  3mark C
3260  setlocal diffanchors='C
3261  diffupdate
3262  set diffanchors='C
3263  call assert_fails('diffupdate', 'E20:')
3264
3265  " Buffer-local marks also can only be used in buffers that have them.
3266  set diffanchors=1
3267  exec "norm 1ggVj\<Esc>"
3268  setlocal diffanchors='<
3269  diffupdate
3270  set diffanchors='<
3271  call assert_fails('diffupdate', 'E20:')
3272
3273  " Pattern search that failed will be an error too
3274  let @/='orig_search_pat'
3275  set diffanchors=1/5/
3276  diffupdate
3277  call assert_equal('orig_search_pat', @/) " also check we don't pollute the search register
3278  set diffanchors=1/does_not_exist/
3279  call assert_fails('diffupdate', 'E1550:')
3280  call assert_equal('orig_search_pat', @/)
3281
3282  " Hidden buffers are not supported right now
3283  hide
3284  call assert_fails('diffupdate', 'E1562:')
3285
3286  %bw!
3287  set diffopt&
3288  set diffanchors&
3289endfunc
3290
3291" Test diffget/diffput behaviors when using diff anchors which could create
3292" adjacent diff blocks.
3293func Test_diffget_diffput_diffanchors()
3294  set diffanchors=1/anchor/
3295  set diffopt=internal,filler,anchor
3296
3297  call setline(1, ['1', 'anchor1', '4'])
3298  diffthis
3299  new
3300  call setline(1, ['2', '3', 'anchor2', '4', '5'])
3301  diffthis
3302  wincmd w
3303
3304  " Test using no-range diffget. It should grab the closest diff block only,
3305  " even if there are multiple adjacent blocks.
3306  2
3307  diffget
3308  call assert_equal(['1', 'anchor2', '4'], getline(1, '$'))
3309  diffget
3310  call assert_equal(['2', '3', 'anchor2', '4'], getline(1, '$'))
3311
3312  " Test using a range to get/put all the adjacent diff blocks.
3313  1,$delete
3314  call setline(1, ['anchor1', '4'])
3315  0,1 diffget
3316  call assert_equal(['2', '3', 'anchor2', '4'], getline(1, '$'))
3317
3318  1,$delete
3319  call setline(1, ['anchor1', '4'])
3320  0,$+1 diffget
3321  call assert_equal(['2', '3', 'anchor2', '4', '5'], getline(1, '$'))
3322
3323  1,$delete
3324  call setline(1, ['anchor1', '4'])
3325  0,1 diffput
3326  wincmd w
3327  call assert_equal(['anchor1', '4', '5'], getline(1,'$'))
3328
3329  %bw!
3330  set diffopt&
3331  set diffanchors&
3332endfunc
3333
3334func Test_diff_add_prop_in_autocmd()
3335  CheckScreendump
3336
3337  let lines =<< trim END
3338    func MyDiff() abort
3339      let f1 = readfile(v:fname_in)
3340      let f2 = readfile(v:fname_new)
3341      let f0 = diff(f1, f2)
3342      call writefile(split(f0, "\n"), v:fname_out)
3343    endfunc
3344
3345    call prop_type_add('myprop', #{highlight: 'Search', override: 1})
3346    autocmd OptionSet diff call prop_add(1, 1, #{type: 'myprop', length: 100})
3347    set diffexpr=MyDiff()
3348  END
3349  call writefile(lines, 'Xtest_diff_add_prop_in_autocmd', 'D')
3350  call writefile(['foo', 'bar', 'baz'], 'Xdiffsplit_file', 'D')
3351
3352  let buf = RunVimInTerminal('-S Xtest_diff_add_prop_in_autocmd', {})
3353  call term_sendkeys(buf, ":diffsplit Xdiffsplit_file\<CR>")
3354  call VerifyScreenDump(buf, 'Test_diff_add_prop_in_autocmd_01', {})
3355
3356  call StopVimInTerminal(buf)
3357endfunc
3358
3359" this was causing a use-after-free by calling winframe_remove() recursively
3360func Test_diffexpr_wipe_buffers()
3361  CheckRunVimInTerminal
3362
3363  let lines =<< trim END
3364    def DiffFuncExpr()
3365      var in: list<string> = readfile(v:fname_in)
3366      var new = readfile(v:fname_new)
3367      var out: string = diff(in, new)
3368      writefile(split(out, "n"), v:fname_out)
3369    enddef
3370
3371    new
3372    vnew
3373    set diffexpr=DiffFuncExpr()
3374    wincmd l
3375    new
3376    cal setline(1,range(20))
3377    wind difft
3378    wincm w
3379    hid
3380    %bw!
3381  END
3382  call writefile(lines, 'Xtest_diffexpr_wipe', 'D')
3383
3384  let buf = RunVimInTerminal('Xtest_diffexpr_wipe', {})
3385  call term_sendkeys(buf, ":so\<CR>")
3386  call WaitForAssert({-> assert_match('4 buffers wiped out', term_getline(buf, 20))})
3387
3388  call StopVimInTerminal(buf)
3389endfunc
3390
3391func Test_diffput_to_empty_buf()
3392  CheckScreendump
3393
3394  let lines =<< trim END
3395    call setline(1, ['foo', 'bar', 'baz'])
3396    rightbelow vnew
3397    windo diffthis
3398    windo set cursorline nofoldenable
3399    wincmd t
3400  END
3401  call writefile(lines, 'Xtest_diffput_to_empty_buf', 'D')
3402
3403  let buf = RunVimInTerminal('-S Xtest_diffput_to_empty_buf', {})
3404  call VerifyScreenDump(buf, 'Test_diffput_to_empty_buf_01', {})
3405  call term_sendkeys(buf, '0')  " Trigger an initial 'cursorbind' check.
3406  call VerifyScreenDump(buf, 'Test_diffput_to_empty_buf_01', {})
3407  call term_sendkeys(buf, ":diffput | echo\<CR>")
3408  call VerifyScreenDump(buf, 'Test_diffput_to_empty_buf_02', {})
3409  call term_sendkeys(buf, ":redraw!\<CR>")
3410  call VerifyScreenDump(buf, 'Test_diffput_to_empty_buf_02', {})
3411  call term_sendkeys(buf, 'j')
3412  call VerifyScreenDump(buf, 'Test_diffput_to_empty_buf_03', {})
3413
3414  call StopVimInTerminal(buf)
3415endfunc
3416
3417" Undo can change which lines correspond in a diff. 'cursorbind' must update
3418" the other window even when the cursor here did not move.
3419func Test_diff_cursorbind_after_undo()
3420  call setline(1, ['x', 'y', 'c', 'd'])
3421  let w1 = win_getid()
3422  new
3423  call setline(1, ['p', 'q', 'c', 'd'])
3424  let w2 = win_getid()
3425  windo diffthis
3426  call win_gotoid(w1)
3427
3428  normal! 2dd
3429  call assert_equal(1, line('.', w1))
3430  call assert_equal(1, line('.', w2))
3431  normal! jk
3432  call assert_equal(1, line('.', w1))
3433  call assert_equal(3, line('.', w2))
3434
3435  normal! u
3436  call assert_equal(1, line('.', w1))
3437  call assert_equal(1, line('.', w2))
3438
3439  %bw!
3440endfunc
3441
3442" Redrawing folds scans the diff block list.  The scan is resumed from a
3443" remembered position (the "finger") when the line number does not move
3444" backwards, so make sure it yields exactly the same folds as a full scan
3445" with many changes, when walking up and down, in both windows, and after an
3446" edit invalidates the finger.
3447func Test_diff_fold_scan_finger()
3448  enew!
3449  let lines = []
3450  for i in range(1, 600)
3451    call add(lines, string(i))
3452  endfor
3453  call setline(1, lines)
3454  diffthis
3455  let winone = win_getid()
3456  new
3457  let other = copy(lines)
3458  " Change one line in every 30-line block, far enough apart to stay separate.
3459  let changes = range(30, 570, 30)
3460  for c in changes
3461    let other[c - 1] = 'x' . other[c - 1]
3462  endfor
3463  call setline(1, other)
3464  diffthis
3465  let wintwo = win_getid()
3466
3467  set diffopt=internal,filler,context:4
3468  diffupdate
3469  " Park the cursor on a changed (never folded) line so the fold under the
3470  " cursor is not opened, which would perturb the checks below.
3471  call cursor(300, 1)
3472  normal! zx
3473
3474  " Fold boundaries derived independently from the change positions: each
3475  " change at line c keeps [c - 4, c + 4] visible, the gaps are closed folds.
3476  let expected = []
3477  let prev_end = 0
3478  for c in changes
3479    if c - 4 - 1 >= prev_end + 1
3480      call add(expected, [prev_end + 1, c - 4 - 1])
3481    endif
3482    let prev_end = c + 4
3483  endfor
3484  if prev_end + 1 <= 600
3485    call add(expected, [prev_end + 1, 600])
3486  endif
3487
3488  " Walk the folds forward (lnum increasing: exercises the resume path).
3489  for [s, e] in expected
3490    call assert_equal(s, foldclosed(s), 'foldclosed at ' . s)
3491    call assert_equal(e, foldclosedend(s), 'foldclosedend at ' . s)
3492  endfor
3493
3494  " Walk the folds backward (lnum decreasing: forces the finger to restart).
3495  for [s, e] in reverse(copy(expected))
3496    call assert_equal(s, foldclosed(e), 'reverse foldclosed at ' . e)
3497    call assert_equal(e, foldclosedend(e), 'reverse foldclosedend at ' . e)
3498  endfor
3499
3500  " The context lines around every change must not be in a closed fold.
3501  for c in changes
3502    call assert_equal(-1, foldclosed(c), 'change line ' . c . ' visible')
3503  endfor
3504
3505  " The other window (different buffer index) must produce the same folds.
3506  call win_gotoid(winone)
3507  call cursor(300, 1)
3508  normal! zx
3509  for [s, e] in expected
3510    call assert_equal(s, foldclosed(s), 'winone foldclosed at ' . s)
3511    call assert_equal(e, foldclosedend(s), 'winone foldclosedend at ' . s)
3512  endfor
3513
3514  " Editing invalidates the finger; a new change deep inside a fold must split
3515  " it and become visible.
3516  call win_gotoid(wintwo)
3517  call cursor(300, 1)
3518  call setline(315, 'x' . getline(315))
3519  diffupdate
3520  normal! zx
3521  call assert_equal(305, foldclosed(305))
3522  call assert_equal(310, foldclosedend(305))
3523  call assert_equal(-1, foldclosed(315))
3524  call assert_equal(320, foldclosed(320))
3525  call assert_equal(325, foldclosedend(320))
3526
3527  diffoff!
3528  %bwipe!
3529  set diffopt&
3530endfunc
3531
3532" vim: shiftwidth=2 sts=2 expandtab