robin/laufey

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

robinmerge: 1dc9728daa1d8a2702

master
67.0 KiB2313 linesraw
1" Test for 'scroll', 'scrolloff', 'smoothscroll', etc.
2
3source check.vim
4source screendump.vim
5source mouse.vim
6
7func Test_reset_scroll()
8  let scr = &l:scroll
9
10  setlocal scroll=1
11  setlocal scroll&
12  call assert_equal(scr, &l:scroll)
13
14  setlocal scroll=1
15  setlocal scroll=0
16  call assert_equal(scr, &l:scroll)
17
18  try
19    execute 'setlocal scroll=' . (winheight(0) + 1)
20    " not reached
21    call assert_false(1)
22  catch
23    call assert_exception('E49:')
24  endtry
25
26  split
27
28  let scr = &l:scroll
29
30  setlocal scroll=1
31  setlocal scroll&
32  call assert_equal(scr, &l:scroll)
33
34  setlocal scroll=1
35  setlocal scroll=0
36  call assert_equal(scr, &l:scroll)
37
38  quit!
39endfunc
40
41func Test_scolloff_even_line_count()
42  new
43  resize 6
44  setlocal scrolloff=3
45  call setline(1, range(20))
46  normal 2j
47  call assert_equal(1, getwininfo(win_getid())[0].topline)
48  normal j
49  call assert_equal(1, getwininfo(win_getid())[0].topline)
50  normal j
51  call assert_equal(2, getwininfo(win_getid())[0].topline)
52  normal j
53  call assert_equal(3, getwininfo(win_getid())[0].topline)
54
55  bwipe!
56endfunc
57
58func Test_mouse_scroll_inactive_with_cursorbind()
59  for scb in [0, 1]
60    for so in [0, 1, 2]
61      let msg = $'scb={scb} so={so}'
62
63      new | only
64      let w1 = win_getid()
65      setlocal cursorbind
66      let &l:scb = scb
67      let &l:so = so
68      call setline(1, range(101, 109))
69      rightbelow vnew
70      let w2 = win_getid()
71      setlocal cursorbind
72      let &l:scb = scb
73      let &l:so = so
74      call setline(1, range(101, 109))
75
76      normal! $
77      call assert_equal(3, col('.', w1), msg)
78      call assert_equal(3, col('.', w2), msg)
79      call Ntest_setmouse(1, 1)
80      call feedkeys("\<ScrollWheelDown>", 'xt')
81      call assert_equal(4, line('w0', w1), msg)
82      call assert_equal(4 + so, line('.', w1), msg)
83      call assert_equal(1, line('w0', w2), msg)
84      call assert_equal(1, line('.', w2), msg)
85      call feedkeys("\<ScrollWheelDown>", 'xt')
86      call assert_equal(7, line('w0', w1), msg)
87      call assert_equal(7 + so, line('.', w1), msg)
88      call assert_equal(1, line('w0', w2), msg)
89      call assert_equal(1, line('.', w2), msg)
90      call feedkeys("\<ScrollWheelUp>", 'xt')
91      call assert_equal(4, line('w0', w1), msg)
92      call assert_equal(7 + so, line('.', w1), msg)
93      call assert_equal(1, line('w0', w2), msg)
94      call assert_equal(1, line('.', w2), msg)
95      call feedkeys("\<ScrollWheelUp>", 'xt')
96      call assert_equal(1, line('w0', w1), msg)
97      call assert_equal(7 + so, line('.', w1), msg)
98      call assert_equal(1, line('w0', w2), msg)
99      call assert_equal(1, line('.', w2), msg)
100      normal! 0
101      call assert_equal(1, line('.', w1), msg)
102      call assert_equal(1, col('.', w1), msg)
103      call assert_equal(1, line('.', w2), msg)
104      call assert_equal(1, col('.', w2), msg)
105
106      bwipe!
107      bwipe!
108    endfor
109  endfor
110endfunc
111
112func Test_CtrlE_CtrlY_stop_at_end()
113  enew
114  call setline(1, ['one', 'two'])
115  set number
116  exe "normal \<C-Y>"
117  call assert_equal(["  1 one   "], ScreenLines(1, 10))
118  exe "normal \<C-E>\<C-E>\<C-E>"
119  call assert_equal(["  2 two   "], ScreenLines(1, 10))
120
121  bwipe!
122  set nonumber
123endfunc
124
125func Test_smoothscroll_CtrlE_CtrlY()
126  CheckScreendump
127
128  let lines =<< trim END
129      vim9script
130      setline(1, [
131        'line one',
132        'word '->repeat(20),
133        'line three',
134        'long word '->repeat(7),
135        'line',
136        'line',
137        'line',
138      ])
139      set smoothscroll
140      :5
141  END
142  call writefile(lines, 'XSmoothScroll', 'D')
143  let buf = RunVimInTerminal('-S XSmoothScroll', #{rows: 12, cols: 40})
144
145  call term_sendkeys(buf, "\<C-E>")
146  call VerifyScreenDump(buf, 'Test_smoothscroll_1', {})
147  call term_sendkeys(buf, "\<C-E>")
148  call VerifyScreenDump(buf, 'Test_smoothscroll_2', {})
149  call term_sendkeys(buf, "\<C-E>")
150  call VerifyScreenDump(buf, 'Test_smoothscroll_3', {})
151  call term_sendkeys(buf, "\<C-E>")
152  call VerifyScreenDump(buf, 'Test_smoothscroll_4', {})
153
154  call term_sendkeys(buf, "\<C-Y>")
155  call VerifyScreenDump(buf, 'Test_smoothscroll_5', {})
156  call term_sendkeys(buf, "\<C-Y>")
157  call VerifyScreenDump(buf, 'Test_smoothscroll_6', {})
158  call term_sendkeys(buf, "\<C-Y>")
159  call VerifyScreenDump(buf, 'Test_smoothscroll_7', {})
160  call term_sendkeys(buf, "\<C-Y>")
161  call VerifyScreenDump(buf, 'Test_smoothscroll_8', {})
162
163  if has('folding')
164    call term_sendkeys(buf, ":set foldmethod=indent\<CR>")
165    " move the cursor so we can reuse the same dumps
166    call term_sendkeys(buf, "5G")
167    call term_sendkeys(buf, "\<C-E>")
168    call VerifyScreenDump(buf, 'Test_smoothscroll_1', {})
169    call term_sendkeys(buf, "\<C-E>")
170    call VerifyScreenDump(buf, 'Test_smoothscroll_2', {})
171    call term_sendkeys(buf, "7G")
172    call term_sendkeys(buf, "\<C-Y>")
173    call VerifyScreenDump(buf, 'Test_smoothscroll_7', {})
174    call term_sendkeys(buf, "\<C-Y>")
175    call VerifyScreenDump(buf, 'Test_smoothscroll_8', {})
176  endif
177
178  call StopVimInTerminal(buf)
179endfunc
180
181func Test_smoothscroll_multibyte()
182  CheckScreendump
183
184  let lines =<< trim END
185      set scrolloff=0 smoothscroll
186      call setline(1, [repeat('ϛ', 45), repeat('2', 36)])
187      exe "normal G35l\<C-E>k"
188  END
189  call writefile(lines, 'XSmoothMultibyte', 'D')
190  let buf = RunVimInTerminal('-S XSmoothMultibyte', #{rows: 6, cols: 40})
191  call VerifyScreenDump(buf, 'Test_smoothscroll_multi_1', {})
192
193  call StopVimInTerminal(buf)
194endfunc
195
196func Test_smoothscroll_number()
197  CheckScreendump
198
199  let lines =<< trim END
200      vim9script
201      setline(1, [
202        'one ' .. 'word '->repeat(20),
203        'two ' .. 'long word '->repeat(7),
204        'line',
205        'line',
206        'line',
207      ])
208      set smoothscroll
209      set splitkeep=topline
210      set number cpo+=n
211      :3
212
213      def g:DoRel()
214        set number relativenumber scrolloff=0
215        :%del
216        setline(1, [
217          'one',
218          'very long text '->repeat(12),
219          'three',
220        ])
221        exe "normal 2Gzt\<C-E>"
222      enddef
223  END
224  call writefile(lines, 'XSmoothNumber', 'D')
225  let buf = RunVimInTerminal('-S XSmoothNumber', #{rows: 12, cols: 40})
226
227  call VerifyScreenDump(buf, 'Test_smooth_number_1', {})
228  call term_sendkeys(buf, "\<C-E>")
229  call VerifyScreenDump(buf, 'Test_smooth_number_2', {})
230  call term_sendkeys(buf, "\<C-E>")
231  call VerifyScreenDump(buf, 'Test_smooth_number_3', {})
232
233  call term_sendkeys(buf, ":set cpo-=n\<CR>")
234  call VerifyScreenDump(buf, 'Test_smooth_number_4', {})
235  call term_sendkeys(buf, "\<C-Y>")
236  call VerifyScreenDump(buf, 'Test_smooth_number_5', {})
237  call term_sendkeys(buf, "\<C-Y>")
238  call VerifyScreenDump(buf, 'Test_smooth_number_6', {})
239
240  call term_sendkeys(buf, ":botright split\<CR>\<C-L>gg")
241  call VerifyScreenDump(buf, 'Test_smooth_number_7', {})
242  call term_sendkeys(buf, "\<C-E>")
243  call VerifyScreenDump(buf, 'Test_smooth_number_8', {})
244  call term_sendkeys(buf, "\<C-E>")
245  call VerifyScreenDump(buf, 'Test_smooth_number_9', {})
246  call term_sendkeys(buf, ":close\<CR>")
247
248  call term_sendkeys(buf, ":call DoRel()\<CR>")
249  call VerifyScreenDump(buf, 'Test_smooth_number_10', {})
250
251  call StopVimInTerminal(buf)
252endfunc
253
254func Test_smoothscroll_list()
255  CheckScreendump
256
257  let lines =<< trim END
258      vim9script
259      set smoothscroll scrolloff=0
260      set list
261      setline(1, [
262        'one',
263        'very long text '->repeat(12),
264        'three',
265      ])
266      exe "normal 2Gzt\<C-E>"
267  END
268  call writefile(lines, 'XSmoothList', 'D')
269  let buf = RunVimInTerminal('-S XSmoothList', #{rows: 8, cols: 40})
270
271  call VerifyScreenDump(buf, 'Test_smooth_list_1', {})
272
273  call term_sendkeys(buf, ":set listchars+=precedes:#\<CR>")
274  call VerifyScreenDump(buf, 'Test_smooth_list_2', {})
275
276  call StopVimInTerminal(buf)
277endfunc
278
279func Test_smoothscroll_diff_mode()
280  CheckScreendump
281  CheckFeature diff
282
283  let lines =<< trim END
284      vim9script
285      var text = 'just some text here'
286      setline(1, text)
287      set smoothscroll
288      diffthis
289      new
290      setline(1, text)
291      set smoothscroll
292      diffthis
293  END
294  call writefile(lines, 'XSmoothDiff', 'D')
295  let buf = RunVimInTerminal('-S XSmoothDiff', #{rows: 8})
296
297  call VerifyScreenDump(buf, 'Test_smooth_diff_1', {})
298  call term_sendkeys(buf, "\<C-Y>")
299  call VerifyScreenDump(buf, 'Test_smooth_diff_1', {})
300  call term_sendkeys(buf, "\<C-E>")
301  call VerifyScreenDump(buf, 'Test_smooth_diff_1', {})
302
303  call StopVimInTerminal(buf)
304endfunc
305
306func Test_smoothscroll_diff_change_line_default()
307  CheckScreendump
308  CheckFeature diff
309
310  " Uses the new diffopt default with indent-heuristic and inline:char
311  let lines =<< trim END
312    set diffopt=internal,filler,closeoff,indent-heuristic,inline:char,followwrap smoothscroll
313    call setline(1, repeat(' abc', &columns))
314    call setline(2, 'bar')
315    call setline(3, repeat(' abc', &columns))
316    vnew
317    call setline(1, repeat(' abc', &columns))
318    call setline(2, 'foo')
319    call setline(3, 'bar')
320    call setline(4, repeat(' abc', &columns))
321    windo exe "normal! 2gg5\<C-E>"
322    windo diffthis
323  END
324  call writefile(lines, 'XSmoothDiffChangeLine', 'D')
325  let buf = RunVimInTerminal('-S XSmoothDiffChangeLine', #{rows: 20, columns: 55})
326
327  call VerifyScreenDump(buf, 'Test_smooth_diff_change_line_1', {})
328  call term_sendkeys(buf, "Abar")
329  call VerifyScreenDump(buf, 'Test_smooth_diff_change_line_2', {})
330  call term_sendkeys(buf, "\<Esc>")
331  call VerifyScreenDump(buf, 'Test_smooth_diff_change_line_3a', {})
332  call term_sendkeys(buf, "yyp")
333  call VerifyScreenDump(buf, 'Test_smooth_diff_change_line_4', {})
334
335  call StopVimInTerminal(buf)
336endfunc
337
338func Test_smoothscroll_diff_change_line()
339  CheckScreendump
340  CheckFeature diff
341
342  " Uses the old diffopt default
343  let lines =<< trim END
344    set diffopt=internal,filler,closeoff,followwrap,inline:simple smoothscroll
345    call setline(1, repeat(' abc', &columns))
346    call setline(2, 'bar')
347    call setline(3, repeat(' abc', &columns))
348    vnew
349    call setline(1, repeat(' abc', &columns))
350    call setline(2, 'foo')
351    call setline(3, 'bar')
352    call setline(4, repeat(' abc', &columns))
353    windo exe "normal! 2gg5\<C-E>"
354    windo diffthis
355  END
356  call writefile(lines, 'XSmoothDiffChangeLine', 'D')
357  let buf = RunVimInTerminal('-S XSmoothDiffChangeLine', #{rows: 20, columns: 55})
358
359  call VerifyScreenDump(buf, 'Test_smooth_diff_change_line_1', {})
360  call term_sendkeys(buf, "Abar")
361  call VerifyScreenDump(buf, 'Test_smooth_diff_change_line_2', {})
362  call term_sendkeys(buf, "\<Esc>")
363  call VerifyScreenDump(buf, 'Test_smooth_diff_change_line_3', {})
364  call term_sendkeys(buf, "yyp")
365  call VerifyScreenDump(buf, 'Test_smooth_diff_change_line_4', {})
366
367  call StopVimInTerminal(buf)
368endfunc
369
370func Test_smoothscroll_wrap_scrolloff_zero()
371  CheckScreendump
372
373  let lines =<< trim END
374      vim9script
375      setline(1, ['Line' .. (' with some text'->repeat(7))]->repeat(7))
376      set smoothscroll scrolloff=0
377      :3
378  END
379  call writefile(lines, 'XSmoothWrap', 'D')
380  let buf = RunVimInTerminal('-S XSmoothWrap', #{rows: 8, cols: 40})
381
382  call VerifyScreenDump(buf, 'Test_smooth_wrap_1', {})
383
384  " moving cursor down - whole bottom line shows
385  call term_sendkeys(buf, "j")
386  call VerifyScreenDump(buf, 'Test_smooth_wrap_2', {})
387
388  call term_sendkeys(buf, "\<C-E>j")
389  call VerifyScreenDump(buf, 'Test_smooth_wrap_3', {})
390
391  call term_sendkeys(buf, "G")
392  call VerifyScreenDump(buf, 'Test_smooth_wrap_4', {})
393
394  call term_sendkeys(buf, "4\<C-Y>G")
395  call VerifyScreenDump(buf, 'Test_smooth_wrap_4', {})
396
397  " moving cursor up right after the <<< marker - no need to show whole line
398  call term_sendkeys(buf, "2gj3l2k")
399  call VerifyScreenDump(buf, 'Test_smooth_wrap_5', {})
400
401  " moving cursor up where the <<< marker is - whole top line shows
402  call term_sendkeys(buf, "2j02k")
403  call VerifyScreenDump(buf, 'Test_smooth_wrap_6', {})
404
405  call StopVimInTerminal(buf)
406endfunc
407
408func Test_smoothscroll_wrap_long_line()
409  CheckScreendump
410
411  let lines =<< trim END
412      vim9script
413      setline(1, ['one', 'two', 'Line' .. (' with lots of text'->repeat(30)) .. ' end', 'four'])
414      set smoothscroll scrolloff=0
415      normal 3G10|zt
416  END
417  call writefile(lines, 'XSmoothWrap', 'D')
418  let buf = RunVimInTerminal('-S XSmoothWrap', #{rows: 6, cols: 40})
419  call VerifyScreenDump(buf, 'Test_smooth_long_1', {})
420
421  " scrolling up, cursor moves screen line down
422  call term_sendkeys(buf, "\<C-E>")
423  call VerifyScreenDump(buf, 'Test_smooth_long_2', {})
424  call term_sendkeys(buf, "5\<C-E>")
425  call VerifyScreenDump(buf, 'Test_smooth_long_3', {})
426
427  " scrolling down, cursor moves screen line up
428  call term_sendkeys(buf, "5\<C-Y>")
429  call VerifyScreenDump(buf, 'Test_smooth_long_4', {})
430  call term_sendkeys(buf, "\<C-Y>")
431  call VerifyScreenDump(buf, 'Test_smooth_long_5', {})
432
433  " 'scrolloff' set to 1, scrolling up, cursor moves screen line down
434  call term_sendkeys(buf, ":set scrolloff=1\<CR>")
435  call term_sendkeys(buf, "10|\<C-E>")
436  call VerifyScreenDump(buf, 'Test_smooth_long_6', {})
437
438  " 'scrolloff' set to 1, scrolling down, cursor moves screen line up
439  call term_sendkeys(buf, "\<C-E>")
440  call term_sendkeys(buf, "gjgj")
441  call term_sendkeys(buf, "\<C-Y>")
442  call VerifyScreenDump(buf, 'Test_smooth_long_7', {})
443
444  " 'scrolloff' set to 2, scrolling up, cursor moves screen line down
445  call term_sendkeys(buf, ":set scrolloff=2\<CR>")
446  call term_sendkeys(buf, "10|\<C-E>")
447  call VerifyScreenDump(buf, 'Test_smooth_long_8', {})
448
449  " 'scrolloff' set to 2, scrolling down, cursor moves screen line up
450  call term_sendkeys(buf, "\<C-E>")
451  call term_sendkeys(buf, "gj")
452  call term_sendkeys(buf, "\<C-Y>")
453  call VerifyScreenDump(buf, 'Test_smooth_long_9', {})
454
455  " 'scrolloff' set to 0, move cursor down one line.
456  " Cursor should move properly, and since this is a really long line, it will
457  " be put on top of the screen.
458  call term_sendkeys(buf, ":set scrolloff=0\<CR>")
459  call term_sendkeys(buf, "0j")
460  call VerifyScreenDump(buf, 'Test_smooth_long_10', {})
461
462  " Test zt/zz/zb that they work properly when a long line is above it
463  call term_sendkeys(buf, "zt")
464  call VerifyScreenDump(buf, 'Test_smooth_long_11', {})
465  call term_sendkeys(buf, "zz")
466  call VerifyScreenDump(buf, 'Test_smooth_long_12', {})
467  call term_sendkeys(buf, "zb")
468  call VerifyScreenDump(buf, 'Test_smooth_long_13', {})
469
470  " Repeat the step and move the cursor down again.
471  " This time, use a shorter long line that is barely long enough to span more
472  " than one window. Note that the cursor is at the bottom this time because
473  " Vim prefers to do so if we are scrolling a few lines only.
474  call term_sendkeys(buf, ":call setline(1, ['one', 'two', 'Line' .. (' with lots of text'->repeat(10)) .. ' end', 'four'])\<CR>")
475  " Currently visible lines were replaced, test that the lines and cursor
476  " are correctly displayed.
477  call VerifyScreenDump(buf, 'Test_smooth_long_14', {})
478  call term_sendkeys(buf, "3Gzt")
479  call term_sendkeys(buf, "j")
480  call VerifyScreenDump(buf, 'Test_smooth_long_15', {})
481
482  " Repeat the step but this time start it when the line is smooth-scrolled by
483  " one line. This tests that the offset calculation is still correct and
484  " still end up scrolling down to the next line with cursor at bottom of
485  " screen.
486  call term_sendkeys(buf, "3Gzt")
487  call term_sendkeys(buf, "\<C-E>j")
488  call VerifyScreenDump(buf, 'Test_smooth_long_16', {})
489
490  call StopVimInTerminal(buf)
491endfunc
492
493func Test_smoothscroll_one_long_line()
494  CheckScreendump
495
496  let lines =<< trim END
497      vim9script
498      setline(1, 'with lots of text '->repeat(7))
499      set smoothscroll scrolloff=0
500  END
501  call writefile(lines, 'XSmoothOneLong', 'D')
502  let buf = RunVimInTerminal('-S XSmoothOneLong', #{rows: 6, cols: 40})
503  call VerifyScreenDump(buf, 'Test_smooth_one_long_1', {})
504
505  call term_sendkeys(buf, "\<C-E>")
506  call VerifyScreenDump(buf, 'Test_smooth_one_long_2', {})
507
508  call term_sendkeys(buf, "0")
509  call VerifyScreenDump(buf, 'Test_smooth_one_long_1', {})
510
511  call StopVimInTerminal(buf)
512endfunc
513
514func Test_smoothscroll_long_line_showbreak()
515  CheckScreendump
516
517  let lines =<< trim END
518      vim9script
519      # a line that spans four screen lines
520      setline(1, 'with lots of text in one line '->repeat(6))
521      set smoothscroll scrolloff=0
522      &showbreak = '+++ '
523  END
524  call writefile(lines, 'XSmoothLongShowbreak', 'D')
525  let buf = RunVimInTerminal('-S XSmoothLongShowbreak', #{rows: 6, cols: 40})
526  call VerifyScreenDump(buf, 'Test_smooth_long_showbreak_1', {})
527
528  call term_sendkeys(buf, "\<C-E>")
529  call VerifyScreenDump(buf, 'Test_smooth_long_showbreak_2', {})
530
531  call term_sendkeys(buf, "0")
532  call VerifyScreenDump(buf, 'Test_smooth_long_showbreak_1', {})
533
534  call StopVimInTerminal(buf)
535endfunc
536
537" Check that 'smoothscroll' marker is drawn over double-width char correctly.
538" Run with multiple encodings.
539func Test_smoothscroll_marker_over_double_width()
540  " Run this in a separate Vim instance to avoid messing up.
541  let after =<< trim [CODE]
542    scriptencoding utf-8
543    call setline(1, 'a'->repeat(&columns) .. '口'->repeat(10))
544    setlocal smoothscroll
545    redraw
546    exe "norm \<C-E>"
547    redraw
548    " Check the chars one by one. Don't check the whole line concatenated.
549    call assert_equal('<', screenstring(1, 1))
550    call assert_equal('<', screenstring(1, 2))
551    call assert_equal('<', screenstring(1, 3))
552    call assert_equal(' ', screenstring(1, 4))
553    call assert_equal('口', screenstring(1, 5))
554    call assert_equal('口', screenstring(1, 7))
555    call assert_equal('口', screenstring(1, 9))
556    call assert_equal('口', screenstring(1, 11))
557    call assert_equal('口', screenstring(1, 13))
558    call assert_equal('口', screenstring(1, 15))
559    call writefile(v:errors, 'Xresult')
560    qall!
561  [CODE]
562
563  let encodings = ['utf-8', 'cp932', 'cp936', 'cp949', 'cp950']
564  if !has('win32')
565    let encodings += ['euc-jp']
566  endif
567  if has('nvim')
568    let encodings = ['utf-8']
569  endif
570  for enc in encodings
571    let msg = 'enc=' .. enc
572    if RunVim([], after, $'--clean --cmd "set encoding={enc}"')
573      call assert_equal([], readfile('Xresult'), msg)
574    endif
575    call delete('Xresult')
576  endfor
577endfunc
578
579" Same as the test above, but check the text actually shown on screen.
580" Only run with UTF-8 encoding.
581func Test_smoothscroll_marker_over_double_width_dump()
582  CheckScreendump
583
584  let lines =<< trim END
585    call setline(1, 'a'->repeat(&columns) .. '口'->repeat(10))
586    setlocal smoothscroll
587  END
588  call writefile(lines, 'XSmoothMarkerOverDoubleWidth', 'D')
589  let buf = RunVimInTerminal('-S XSmoothMarkerOverDoubleWidth', #{rows: 6, cols: 40})
590  call VerifyScreenDump(buf, 'Test_smooth_marker_over_double_width_1', {})
591
592  call term_sendkeys(buf, "\<C-E>")
593  call VerifyScreenDump(buf, 'Test_smooth_marker_over_double_width_2', {})
594
595  call StopVimInTerminal(buf)
596endfunc
597
598func s:check_col_calc(win_col, win_line, buf_col)
599  call assert_equal(a:win_col, wincol())
600  call assert_equal(a:win_line, winline())
601  call assert_equal(a:buf_col, col('.'))
602endfunc
603
604" Test that if the current cursor is on a smooth scrolled line, we correctly
605" reposition it. Also check that we don't miscalculate the values by checking
606" the consistency between wincol() and col('.') as they are calculated
607" separately in code.
608func Test_smoothscroll_cursor_position()
609  call NewWindow(10, 20)
610  setl smoothscroll wrap
611  call setline(1, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
612
613  call s:check_col_calc(1, 1, 1)
614  exe "normal \<C-E>"
615
616  " Move down another line to avoid blocking the <<< display
617  call s:check_col_calc(1, 2, 41)
618  exe "normal \<C-Y>"
619  call s:check_col_calc(1, 3, 41)
620
621  " Test "g0/g<Home>"
622  exe "normal gg\<C-E>"
623  norm $gkg0
624  call s:check_col_calc(4, 1, 24)
625
626  " Test moving the cursor behind the <<< display with 'virtualedit'
627  set virtualedit=all
628  exe "normal \<C-E>gkh"
629  call s:check_col_calc(3, 2, 23)
630  set virtualedit&
631
632  normal gg3l
633  exe "normal \<C-E>"
634
635  " Move down only 1 line when we are out of the range of the <<< display
636  call s:check_col_calc(4, 1, 24)
637  exe "normal \<C-Y>"
638  call s:check_col_calc(4, 2, 24)
639  normal ggg$
640  exe "normal \<C-E>"
641  call s:check_col_calc(20, 1, 40)
642  exe "normal \<C-Y>"
643  call s:check_col_calc(20, 2, 40)
644  normal gg
645
646  " Test number, where we have indented lines
647  setl number
648  call s:check_col_calc(5, 1, 1)
649  exe "normal \<C-E>"
650
651  " Move down only 1 line when the <<< display is on the number column
652  call s:check_col_calc(5, 1, 17)
653  exe "normal \<C-Y>"
654  call s:check_col_calc(5, 2, 17)
655  normal ggg$
656  exe "normal \<C-E>"
657  call s:check_col_calc(20, 1, 32)
658  exe "normal \<C-Y>"
659  call s:check_col_calc(20, 2, 32)
660  normal gg
661
662  setl numberwidth=1
663
664  " Move down another line when numberwidth is too short to cover the whole
665  " <<< display
666  call s:check_col_calc(3, 1, 1)
667  exe "normal \<C-E>"
668  call s:check_col_calc(3, 2, 37)
669  exe "normal \<C-Y>"
670  call s:check_col_calc(3, 3, 37)
671  normal ggl
672
673  " Only move 1 line down when we are just past the <<< display
674  call s:check_col_calc(4, 1, 2)
675  exe "normal \<C-E>"
676  call s:check_col_calc(4, 1, 20)
677  exe "normal \<C-Y>"
678  call s:check_col_calc(4, 2, 20)
679  normal gg
680  setl numberwidth&
681
682  " Test number + showbreak, so test that the additional indentation works
683  setl number showbreak=+++
684  call s:check_col_calc(5, 1, 1)
685  exe "normal \<C-E>"
686  call s:check_col_calc(8, 1, 17)
687  exe "normal \<C-Y>"
688  call s:check_col_calc(8, 2, 17)
689  normal gg
690
691  " Test number + cpo+=n mode, where wrapped lines aren't indented
692  setl number cpo+=n showbreak=
693  call s:check_col_calc(5, 1, 1)
694  exe "normal \<C-E>"
695  call s:check_col_calc(1, 2, 37)
696  exe "normal \<C-Y>"
697  call s:check_col_calc(1, 3, 37)
698  normal gg
699
700  " Test list + listchars "precedes", where there is always 1 overlap
701  " regardless of number and cpo-=n.
702  setl number list listchars=precedes:< cpo-=n
703  call s:check_col_calc(5, 1, 1)
704  exe "normal 3|\<C-E>h"
705  call s:check_col_calc(6, 1, 18)
706  norm h
707  call s:check_col_calc(5, 2, 17)
708  normal gg
709
710  bwipe!
711endfunc
712
713func Test_smoothscroll_cursor_scrolloff()
714  call NewWindow(10, 20)
715  setl smoothscroll wrap
716  setl scrolloff=3
717
718  " 120 chars are 6 screen lines
719  call setline(1, "abcdefghijklmnopqrstABCDEFGHIJKLMNOPQRSTabcdefghijklmnopqrstABCDEFGHIJKLMNOPQRSTabcdefghijklmnopqrstABCDEFGHIJKLMNOPQRST")
720  call setline(2, "below")
721
722  call s:check_col_calc(1, 1, 1)
723
724  " CTRL-E shows "<<<DEFG...", cursor move four lines down
725  exe "normal \<C-E>"
726  call s:check_col_calc(1, 4, 81)
727
728  " cursor on start of second line, "gk" moves into first line, skipcol doesn't
729  " change
730  exe "normal G0gk"
731  call s:check_col_calc(1, 5, 101)
732
733  " move cursor left one window width worth, scrolls one screen line
734  exe "normal 20h"
735  call s:check_col_calc(1, 5, 81)
736
737  " move cursor left one window width worth, scrolls one screen line
738  exe "normal 20h"
739  call s:check_col_calc(1, 4, 61)
740
741  " cursor on last line, "gk" should not cause a scroll
742  set scrolloff=0
743  normal G0
744  call s:check_col_calc(1, 7, 1)
745  normal gk
746  call s:check_col_calc(1, 6, 101)
747
748  bwipe!
749endfunc
750
751
752" Test that mouse picking is still accurate when we have smooth scrolled lines
753func Test_smoothscroll_mouse_pos()
754  CheckNotGui
755  CheckUnix
756
757  let save_mouse = &mouse
758  "let save_term = &term
759  "let save_ttymouse = &ttymouse
760  set mouse=a "term=xterm ttymouse=xterm2
761
762  call NewWindow(10, 20)
763  setl smoothscroll wrap
764  " First line will wrap to 3 physical lines. 2nd/3rd lines are short lines.
765  call setline(1, ["abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", "line 2", "line 3"])
766
767  func s:check_mouse_click(row, col, buf_row, buf_col)
768    call MouseLeftClick(a:row, a:col)
769
770    call assert_equal(a:col, wincol())
771    call assert_equal(a:row, winline())
772    call assert_equal(a:buf_row, line('.'))
773    call assert_equal(a:buf_col, col('.'))
774  endfunc
775
776  " Check that clicking without scroll works first.
777  call s:check_mouse_click(3, 5, 1, 45)
778  call s:check_mouse_click(4, 1, 2, 1)
779  call s:check_mouse_click(4, 6, 2, 6)
780  call s:check_mouse_click(5, 1, 3, 1)
781  call s:check_mouse_click(5, 6, 3, 6)
782
783  " Smooth scroll, and checks that this didn't mess up mouse clicking
784  exe "normal \<C-E>"
785  call s:check_mouse_click(2, 5, 1, 45)
786  call s:check_mouse_click(3, 1, 2, 1)
787  call s:check_mouse_click(3, 6, 2, 6)
788  call s:check_mouse_click(4, 1, 3, 1)
789  call s:check_mouse_click(4, 6, 3, 6)
790
791  exe "normal \<C-E>"
792  call s:check_mouse_click(1, 5, 1, 45)
793  call s:check_mouse_click(2, 1, 2, 1)
794  call s:check_mouse_click(2, 6, 2, 6)
795  call s:check_mouse_click(3, 1, 3, 1)
796  call s:check_mouse_click(3, 6, 3, 6)
797
798  " Make a new first line 11 physical lines tall so it's taller than window
799  " height, to test overflow calculations with really long lines wrapping.
800  normal gg
801  call setline(1, "12345678901234567890"->repeat(11))
802  exe "normal 6\<C-E>"
803  call s:check_mouse_click(5, 1, 1, 201)
804  call s:check_mouse_click(6, 1, 2, 1)
805  call s:check_mouse_click(7, 1, 3, 1)
806
807  let &mouse = save_mouse
808  "let &term = save_term
809  "let &ttymouse = save_ttymouse
810  bwipe!
811endfunc
812
813" this was dividing by zero
814func Test_smoothscroll_zero_width()
815  CheckScreendump
816
817  let lines =<< trim END
818      winsize 0 0
819      vsplit
820      vsplit
821      vsplit
822      vsplit
823      vsplit
824      sil norm H
825      set wrap
826      set smoothscroll
827      set number
828  END
829  call writefile(lines, 'XSmoothScrollZero', 'D')
830  let buf = RunVimInTerminal('-u NONE -i NONE -n -m -X -Z -e -s -S XSmoothScrollZero', #{rows: 6, cols: 60, wait_for_ruler: 0})
831  call VerifyScreenDump(buf, 'Test_smoothscroll_zero_1', {})
832
833  call term_sendkeys(buf, ":sil norm \<C-V>\<C-W>\<C-V>\<C-N>\<CR>")
834  call VerifyScreenDump(buf, 'Test_smoothscroll_zero_2', {})
835
836  call StopVimInTerminal(buf)
837endfunc
838
839" this was unnecessarily inserting lines
840func Test_smoothscroll_ins_lines()
841  CheckScreendump
842
843  let lines =<< trim END
844      set wrap
845      set smoothscroll
846      set scrolloff=0
847      set conceallevel=2
848      call setline(1, [
849        \'line one' .. 'with lots of text in one line '->repeat(2),
850        \'line two',
851        \'line three',
852        \'line four',
853        \'line five'
854      \])
855  END
856  call writefile(lines, 'XSmoothScrollInsLines', 'D')
857  let buf = RunVimInTerminal('-S XSmoothScrollInsLines', #{rows: 6, cols: 40})
858
859  call term_sendkeys(buf, "\<C-E>gjgk")
860  call VerifyScreenDump(buf, 'Test_smooth_ins_lines', {})
861
862  call StopVimInTerminal(buf)
863endfunc
864
865" this placed the cursor in the command line
866func Test_smoothscroll_cursormoved_line()
867  CheckScreendump
868
869  let lines =<< trim END
870      set smoothscroll
871      call setline(1, [
872        \'',
873        \'_'->repeat(&lines * &columns),
874        \(('_')->repeat(&columns - 2) .. 'xxx')->repeat(2)
875      \])
876      autocmd CursorMoved * eval [line('w0'), line('w$')]
877      call search('xxx')
878  END
879  call writefile(lines, 'XSmoothCursorMovedLine', 'D')
880  let buf = RunVimInTerminal('-S XSmoothCursorMovedLine', #{rows: 6})
881
882  call VerifyScreenDump(buf, 'Test_smooth_cursormoved_line', {})
883
884  call StopVimInTerminal(buf)
885endfunc
886
887func Test_smoothscroll_eob()
888  CheckScreendump
889
890  let lines =<< trim END
891      set smoothscroll
892      call setline(1, ['']->repeat(100))
893      norm G
894  END
895  call writefile(lines, 'XSmoothEob', 'D')
896  let buf = RunVimInTerminal('-S XSmoothEob', #{rows: 10})
897
898  " does not scroll halfway when scrolling to end of buffer
899  call VerifyScreenDump(buf, 'Test_smooth_eob_1', {})
900
901  " cursor is not placed below window
902  call term_sendkeys(buf, ":call setline(92, 'a'->repeat(100))\<CR>\<C-L>\<C-B>G")
903  call VerifyScreenDump(buf, 'Test_smooth_eob_2', {})
904
905  call StopVimInTerminal(buf)
906endfunc
907
908" skipcol should not reset when doing incremental search on the same word
909func Test_smoothscroll_incsearch()
910  CheckRunVimInTerminal
911
912  let lines =<< trim END
913      set smoothscroll number scrolloff=0 incsearch
914      call setline(1, repeat([''], 20))
915      call setline(11, repeat('a', 100))
916      call setline(14, 'bbbb')
917  END
918  call writefile(lines, 'XSmoothIncsearch', 'D')
919  let buf = RunVimInTerminal('-S XSmoothIncsearch', #{rows: 8, cols: 40})
920  let g:test_is_flaky = 1
921
922  call term_sendkeys(buf, "/b")
923  call WaitForAssert({-> assert_match('^/b\s*$', term_getline(buf, 8))}, 1000)
924  let view1 = map(range(1, 7), {_, i -> term_getline(buf, i)})
925
926  call term_sendkeys(buf, "b")
927  call WaitForAssert({-> assert_match('^/bb\s*$', term_getline(buf, 8))}, 1000)
928  call assert_equal(view1, map(range(1, 7), {_, i -> term_getline(buf, i)}))
929
930  call term_sendkeys(buf, "b")
931  call WaitForAssert({-> assert_match('^/bbb\s*$', term_getline(buf, 8))}, 1000)
932  call assert_equal(view1, map(range(1, 7), {_, i -> term_getline(buf, i)}))
933
934  call term_sendkeys(buf, "b")
935  call WaitForAssert({-> assert_match('^/bbbb\s*$', term_getline(buf, 8))}, 1000)
936  call assert_equal(view1, map(range(1, 7), {_, i -> term_getline(buf, i)}))
937
938  call term_sendkeys(buf, "\<CR>")
939
940  call StopVimInTerminal(buf)
941endfunc
942
943" Test scrolling multiple lines and stopping at non-zero skipcol.
944func Test_smoothscroll_multi_skipcol()
945  CheckScreendump
946
947  let lines =<< trim END
948      setlocal cursorline scrolloff=0 smoothscroll
949      call setline(1, repeat([''], 8))
950      call setline(3, repeat('a', 50))
951      call setline(4, repeat('a', 50))
952      call setline(7, 'bbb')
953      call setline(8, 'ccc')
954      redraw
955  END
956  call writefile(lines, 'XSmoothMultiSkipcol', 'D')
957  let buf = RunVimInTerminal('-S XSmoothMultiSkipcol', #{rows: 10, cols: 40})
958  call VerifyScreenDump(buf, 'Test_smooth_multi_skipcol_1', {})
959
960  call term_sendkeys(buf, "3\<C-E>")
961  call VerifyScreenDump(buf, 'Test_smooth_multi_skipcol_2', {})
962
963  call term_sendkeys(buf, "2\<C-E>")
964  call VerifyScreenDump(buf, 'Test_smooth_multi_skipcol_3', {})
965
966  call StopVimInTerminal(buf)
967endfunc
968
969" this was dividing by zero bug in scroll_cursor_bot
970func Test_smoothscroll_zero_width_scroll_cursor_bot_noruler()
971  CheckScreendump
972
973  let lines =<< trim END
974      set noruler
975      silent normal yy
976      silent normal 19p
977      set cpoptions+=n
978      vsplit
979      vertical resize 0
980      set foldcolumn=1
981      set number
982      set smoothscroll
983      silent normal 20G
984  END
985  call writefile(lines, 'XSmoothScrollZeroBot', 'D')
986  let buf = RunVimInTerminal('-u NONE -S XSmoothScrollZeroBot', #{rows: 19})
987  call VerifyScreenDump(buf, 'Test_smoothscroll_zero_bot', {})
988
989  call StopVimInTerminal(buf)
990endfunc
991
992func Test_smoothscroll_zero_width_scroll_cursor_bot_ruler()
993  CheckScreendump
994
995  let lines =<< trim END
996      set ruler
997      silent normal yy
998      silent normal 19p
999      set cpoptions+=n
1000      vsplit
1001      vertical resize 0
1002      set foldcolumn=1
1003      set number
1004      set smoothscroll
1005      silent normal 20G
1006  END
1007  call writefile(lines, 'XSmoothScrollZeroBot', 'D')
1008  let buf = RunVimInTerminal('-u NONE -S XSmoothScrollZeroBot', #{rows: 19})
1009  call VerifyScreenDump(buf, 'Test_smoothscroll_zero_bot_ruler', {})
1010
1011  call StopVimInTerminal(buf)
1012endfunc
1013
1014" scroll_cursor_top() should reset skipcol when it changes topline
1015func Test_smoothscroll_cursor_top_noruler()
1016  CheckScreendump
1017
1018  let lines =<< trim END
1019      set smoothscroll scrolloff=2 noruler
1020      new | 11resize | wincmd j
1021      call setline(1, ['line1', 'line2', 'line3'->repeat(20), 'line4'])
1022      exe "norm G3\<C-E>k"
1023  END
1024  call writefile(lines, 'XSmoothScrollCursorTop', 'D')
1025  let buf = RunVimInTerminal('-u NONE -S XSmoothScrollCursorTop', #{rows: 12, cols: 40})
1026  call VerifyScreenDump(buf, 'Test_smoothscroll_cursor_top', {})
1027
1028  call StopVimInTerminal(buf)
1029endfunc
1030
1031func Test_smoothscroll_cursor_top_ruler()
1032  CheckScreendump
1033
1034  let lines =<< trim END
1035      set smoothscroll scrolloff=2 ruler
1036      new | 11resize | wincmd j
1037      call setline(1, ['line1', 'line2', 'line3'->repeat(20), 'line4'])
1038      exe "norm G3\<C-E>k"
1039  END
1040  call writefile(lines, 'XSmoothScrollCursorTop', 'D')
1041  let buf = RunVimInTerminal('-u NONE -S XSmoothScrollCursorTop', #{rows: 12, cols: 40})
1042  call VerifyScreenDump(buf, 'Test_smoothscroll_cursor_ru_top', {})
1043
1044  call StopVimInTerminal(buf)
1045endfunc
1046
1047" Division by zero, shouldn't crash
1048func Test_smoothscroll_crash()
1049  CheckScreendump
1050
1051  let lines =<< trim END
1052      20 new
1053      vsp
1054      put =repeat('aaaa', 20)
1055      set nu fdc=1  smoothscroll cpo+=n
1056      vert resize 0
1057      exe "norm! 0\<c-e>"
1058  END
1059  call writefile(lines, 'XSmoothScrollCrash', 'D')
1060  let buf = RunVimInTerminal('-u NONE -S XSmoothScrollCrash', #{rows: 12, cols: 40})
1061  call term_sendkeys(buf, "2\<C-E>\<C-L>")
1062
1063  call StopVimInTerminal(buf)
1064endfunc
1065
1066func Test_smoothscroll_insert_bottom_noruler()
1067  CheckScreendump
1068
1069  let lines =<< trim END
1070    call setline(1, repeat([repeat('A very long line ...', 10)], 5))
1071    set wrap smoothscroll scrolloff=0 noruler
1072  END
1073  call writefile(lines, 'XSmoothScrollInsertBottom', 'D')
1074  let buf = RunVimInTerminal('-u NONE -S XSmoothScrollInsertBottom', #{rows: 9, cols: 40})
1075  call term_sendkeys(buf, "Go123456789\<CR>")
1076  call VerifyScreenDump(buf, 'Test_smoothscroll_insert_bottom', {})
1077
1078  call StopVimInTerminal(buf)
1079endfunc
1080
1081func Test_smoothscroll_insert_bottom_ruler()
1082  CheckScreendump
1083
1084  let lines =<< trim END
1085    call setline(1, repeat([repeat('A very long line ...', 10)], 5))
1086    set wrap smoothscroll scrolloff=0 ruler
1087  END
1088  call writefile(lines, 'XSmoothScrollInsertBottom', 'D')
1089  let buf = RunVimInTerminal('-u NONE -S XSmoothScrollInsertBottom', #{rows: 9, cols: 40})
1090  call term_sendkeys(buf, "Go123456789\<CR>")
1091  call VerifyScreenDump(buf, 'Test_smoothscroll_insert_bottom_ruler', {})
1092
1093  call StopVimInTerminal(buf)
1094endfunc
1095
1096func Test_smoothscroll_in_qf_window_noruler()
1097  CheckFeature quickfix
1098  CheckScreendump
1099
1100  let lines =<< trim END
1101    set nocompatible display=lastline noruler
1102    copen 5
1103    setlocal number smoothscroll
1104    let g:l = [{'text': 'foo'}] + repeat([{'text': join(range(30))}], 10)
1105    call setqflist(g:l, 'r')
1106    normal! G
1107    wincmd t
1108    let g:l1 = [{'text': join(range(1000))}]
1109  END
1110  call writefile(lines, 'XSmoothScrollInQfWindow', 'D')
1111  let buf = RunVimInTerminal('-u NONE -S XSmoothScrollInQfWindow', #{rows: 20, cols: 60})
1112  call VerifyScreenDump(buf, 'Test_smoothscroll_in_qf_window_1', {})
1113
1114  call term_sendkeys(buf, ":call setqflist([], 'r')\<CR>")
1115  call VerifyScreenDump(buf, 'Test_smoothscroll_in_qf_window_2', {})
1116
1117  call term_sendkeys(buf, ":call setqflist(g:l, 'r')\<CR>")
1118  call VerifyScreenDump(buf, 'Test_smoothscroll_in_qf_window_3', {})
1119
1120  call term_sendkeys(buf, ":call setqflist(g:l1, 'r')\<CR>")
1121  call VerifyScreenDump(buf, 'Test_smoothscroll_in_qf_window_4', {})
1122
1123  call term_sendkeys(buf, "\<C-W>b$\<C-W>t")
1124  call VerifyScreenDump(buf, 'Test_smoothscroll_in_qf_window_5', {})
1125
1126  call term_sendkeys(buf, ":call setqflist([], 'r')\<CR>")
1127  call VerifyScreenDump(buf, 'Test_smoothscroll_in_qf_window_2', {})
1128
1129  call term_sendkeys(buf, ":call setqflist(g:l1, 'r')\<CR>")
1130  call VerifyScreenDump(buf, 'Test_smoothscroll_in_qf_window_4', {})
1131
1132  call term_sendkeys(buf, "\<C-W>b$\<C-W>t")
1133  call VerifyScreenDump(buf, 'Test_smoothscroll_in_qf_window_5', {})
1134
1135  call term_sendkeys(buf, ":call setqflist(g:l, 'r')\<CR>")
1136  call VerifyScreenDump(buf, 'Test_smoothscroll_in_qf_window_3', {})
1137
1138  call StopVimInTerminal(buf)
1139endfunc
1140
1141func Test_smoothscroll_in_qf_window_ruler()
1142  CheckFeature quickfix
1143  CheckScreendump
1144
1145  let lines =<< trim END
1146    set nocompatible display=lastline ruler
1147    copen 5
1148    setlocal number smoothscroll
1149    let g:l = [{'text': 'foo'}] + repeat([{'text': join(range(30))}], 10)
1150    call setqflist(g:l, 'r')
1151    normal! G
1152    wincmd t
1153    let g:l1 = [{'text': join(range(1000))}]
1154  END
1155  call writefile(lines, 'XSmoothScrollInQfWindow', 'D')
1156  let buf = RunVimInTerminal('-u NONE -S XSmoothScrollInQfWindow', #{rows: 20, cols: 60})
1157  call VerifyScreenDump(buf, 'Test_smoothscroll_in_qf_window_ru_1', {})
1158
1159  call term_sendkeys(buf, ":call setqflist([], 'r')\<CR>")
1160  call VerifyScreenDump(buf, 'Test_smoothscroll_in_qf_window_ru_2', {})
1161
1162  call term_sendkeys(buf, ":call setqflist(g:l, 'r')\<CR>")
1163  call VerifyScreenDump(buf, 'Test_smoothscroll_in_qf_window_ru_3', {})
1164
1165  call term_sendkeys(buf, ":call setqflist(g:l1, 'r')\<CR>")
1166  call VerifyScreenDump(buf, 'Test_smoothscroll_in_qf_window_ru_4', {})
1167
1168  call term_sendkeys(buf, "\<C-W>b$\<C-W>t")
1169  call VerifyScreenDump(buf, 'Test_smoothscroll_in_qf_window_ru_5', {})
1170
1171  call term_sendkeys(buf, ":call setqflist([], 'r')\<CR>")
1172  call VerifyScreenDump(buf, 'Test_smoothscroll_in_qf_window_ru_2', {})
1173
1174  call term_sendkeys(buf, ":call setqflist(g:l1, 'r')\<CR>")
1175  call VerifyScreenDump(buf, 'Test_smoothscroll_in_qf_window_ru_4', {})
1176
1177  call term_sendkeys(buf, "\<C-W>b$\<C-W>t")
1178  call VerifyScreenDump(buf, 'Test_smoothscroll_in_qf_window_ru_5', {})
1179
1180  call term_sendkeys(buf, ":call setqflist(g:l, 'r')\<CR>")
1181  call VerifyScreenDump(buf, 'Test_smoothscroll_in_qf_window_ru_3', {})
1182
1183  call StopVimInTerminal(buf)
1184endfunc
1185
1186func Test_smoothscroll_in_zero_width_window()
1187  set cpo+=n number smoothscroll
1188  set winwidth=99999 winminwidth=0
1189
1190  vsplit
1191  call assert_equal(0, winwidth(winnr('#')))
1192  call win_execute(win_getid(winnr('#')), "norm! \<C-Y>")
1193
1194  only!
1195  set winwidth& winminwidth&
1196  set cpo-=n nonumber nosmoothscroll
1197endfunc
1198
1199func Test_smoothscroll_textoff_small_winwidth()
1200  set smoothscroll number
1201  call setline(1, 'llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch')
1202  vsplit
1203
1204  let textoff = getwininfo(win_getid())[0].textoff
1205  execute 'vertical resize' textoff + 1
1206  redraw
1207  call assert_equal(0, winsaveview().skipcol)
1208  execute "normal! 0\<C-E>"
1209  redraw
1210  call assert_equal(1, winsaveview().skipcol)
1211  execute 'vertical resize' textoff - 1
1212  " This caused a signed integer overflow.
1213  redraw
1214  call assert_equal(1, winsaveview().skipcol)
1215  execute 'vertical resize' textoff
1216  " This caused an infinite loop.
1217  redraw
1218  call assert_equal(1, winsaveview().skipcol)
1219
1220  %bw!
1221  set smoothscroll& number&
1222endfunc
1223
1224func Test_smoothscroll_page()
1225  call NewWindow(10, 40)
1226  setlocal smoothscroll
1227  call setline(1, 'abcde '->repeat(150))
1228
1229  exe "norm! \<C-F>"
1230  call assert_equal(400, winsaveview().skipcol)
1231  exe "norm! \<C-F>"
1232  call assert_equal(800, winsaveview().skipcol)
1233  exe "norm! \<C-F>"
1234  call assert_equal(880, winsaveview().skipcol)
1235  exe "norm! \<C-B>"
1236  call assert_equal(480, winsaveview().skipcol)
1237  exe "norm! \<C-B>"
1238  call assert_equal(80, winsaveview().skipcol)
1239  exe "norm! \<C-B>"
1240  call assert_equal(0, winsaveview().skipcol)
1241
1242  " Half-page scrolling does not go beyond end of buffer and moves the cursor.
1243  " Even with 'nostartofline', the correct amount of lines is scrolled.
1244  setl nostartofline
1245  exe "norm! 15|\<C-D>"
1246  call assert_equal(200, winsaveview().skipcol)
1247  call assert_equal(215, col('.'))
1248  exe "norm! \<C-D>"
1249  call assert_equal(400, winsaveview().skipcol)
1250  call assert_equal(415, col('.'))
1251  exe "norm! \<C-D>"
1252  call assert_equal(520, winsaveview().skipcol)
1253  call assert_equal(615, col('.'))
1254  exe "norm! \<C-D>"
1255  call assert_equal(520, winsaveview().skipcol)
1256  call assert_equal(815, col('.'))
1257  exe "norm! \<C-D>"
1258  call assert_equal(520, winsaveview().skipcol)
1259  call assert_equal(895, col('.'))
1260  exe "norm! \<C-U>"
1261  call assert_equal(320, winsaveview().skipcol)
1262  call assert_equal(695, col('.'))
1263  exe "norm! \<C-U>"
1264  call assert_equal(120, winsaveview().skipcol)
1265  call assert_equal(495, col('.'))
1266  exe "norm! \<C-U>"
1267  call assert_equal(0, winsaveview().skipcol)
1268  call assert_equal(295, col('.'))
1269  exe "norm! \<C-U>"
1270  call assert_equal(0, winsaveview().skipcol)
1271  call assert_equal(95, col('.'))
1272  exe "norm! \<C-U>"
1273  call assert_equal(0, winsaveview().skipcol)
1274  call assert_equal(15, col('.'))
1275
1276  bwipe!
1277endfunc
1278
1279func Test_smoothscroll_next_topline()
1280  call NewWindow(10, 40)
1281  setlocal smoothscroll
1282  call setline(1, ['abcde '->repeat(150)]->repeat(2))
1283
1284  " Scrolling a screenline that causes the cursor to move to the next buffer
1285  " line should not skip part of that line to bring the cursor into view.
1286  exe "norm! 22\<C-E>"
1287  call assert_equal(880, winsaveview().skipcol)
1288  exe "norm! \<C-E>"
1289  redraw
1290  call assert_equal(0, winsaveview().skipcol)
1291
1292  " Also when scrolling back.
1293  exe "norm! G\<C-Y>"
1294  redraw
1295  call assert_equal(880, winsaveview().skipcol)
1296
1297  " Cursor in correct place when not in the first screenline of a buffer line.
1298  exe "norm! gg4gj20\<C-D>\<C-D>"
1299  redraw
1300  call assert_equal(2, line('w0'))
1301
1302  " Cursor does not end up above topline, adjusting topline later.
1303  setlocal nu cpo+=n
1304  exe "norm! G$g013\<C-Y>"
1305  redraw
1306  call assert_equal(2, line('.'))
1307  call assert_equal(0, winsaveview().skipcol)
1308
1309  set cpo-=n
1310  bwipe!
1311endfunc
1312
1313func Test_smoothscroll_keep_skipcol()
1314  call NewWindow(10, 40)
1315  setlocal smoothscroll
1316  call setline(1, ['abcde '->repeat(150)]->repeat(2))
1317
1318  exe "norm! 10\<C-E>"
1319  redraw
1320  let skipcol = winsaveview().skipcol
1321  call assert_notequal(0, skipcol)
1322
1323  " Changing the height of the window must not reset the scroll position.
1324  resize -3
1325  resize +3
1326  redraw
1327  call assert_equal(skipcol, winsaveview().skipcol)
1328
1329  " Using the autocommand window changes the height as well.
1330  call bufload(bufadd(''))
1331  redraw
1332  call assert_equal(skipcol, winsaveview().skipcol)
1333
1334  bwipe!
1335endfunc
1336
1337func Test_smoothscroll_cursor_back_in_line()
1338  call NewWindow(10, 40)
1339  setlocal smoothscroll
1340  call setline(1, ['abcde '->repeat(150)]->repeat(2))
1341
1342  exe "norm! 10\<C-E>"
1343  redraw
1344  call assert_equal(400, winsaveview().skipcol)
1345
1346  " Moving to an earlier column in the same line scrolls back only as far as
1347  " needed, not all the way to the start of the line.
1348  norm! 240|
1349  redraw
1350  call assert_equal(200, winsaveview().skipcol)
1351  call assert_equal(1, winline())
1352
1353  bwipe!
1354endfunc
1355
1356func Test_smoothscroll_squeezed_window()
1357  setlocal smoothscroll
1358  call setline(1, [repeat('x', 3000)] + repeat(['line'], 10))
1359  exe "norm! gg10\<C-E>"
1360  redraw
1361  let skipcol = winsaveview().skipcol
1362  call assert_notequal(0, skipcol)
1363  let virtcol = virtcol('.')
1364
1365  " Squeezing the window to one line and restoring it must not scroll back to
1366  " the start of the line.
1367  new
1368  wincmd _
1369  close
1370  redraw
1371  call assert_notequal(0, winsaveview().skipcol)
1372  call assert_equal(virtcol, virtcol('.'))
1373
1374  bwipe!
1375endfunc
1376
1377func Test_smoothscroll_long_line_zb()
1378  call NewWindow(10, 40)
1379  call setline(1, 'abcde '->repeat(150))
1380
1381  " Also works without 'smoothscroll' when last line of buffer doesn't fit.
1382  " Used to set topline to buffer line count plus one, causing an empty screen.
1383  norm zb
1384  redraw
1385  call assert_equal(1, winsaveview().topline)
1386
1387  " Moving cursor to bottom works on line that doesn't fit with 'smoothscroll'.
1388  " Skipcol was adjusted later for cursor being on not visible part of line.
1389  setlocal smoothscroll
1390  norm zb
1391  redraw
1392  call assert_equal(520, winsaveview().skipcol)
1393
1394  bwipe!
1395endfunc
1396
1397func Test_smooth_long_scrolloff_noruler()
1398  CheckScreendump
1399
1400  let lines =<< trim END
1401    set smoothscroll scrolloff=3
1402    set noruler
1403    call setline(1, ['one', 'two long '->repeat(100), 'three', 'four', 'five', 'six'])
1404  END
1405  call writefile(lines, 'XSmoothLongScrolloff', 'D')
1406  let buf = RunVimInTerminal('-u NONE -S XSmoothLongScrolloff', #{rows: 8, cols: 40})
1407  call term_sendkeys(buf, ":norm j721|\<CR>")
1408  call VerifyScreenDump(buf, 'Test_smooth_long_scrolloff_1', {})
1409
1410  call term_sendkeys(buf, "gj")
1411  call VerifyScreenDump(buf, 'Test_smooth_long_scrolloff_2', {})
1412
1413  call term_sendkeys(buf, "gj")
1414  call VerifyScreenDump(buf, 'Test_smooth_long_scrolloff_3', {})
1415
1416  call term_sendkeys(buf, "gj")
1417  call VerifyScreenDump(buf, 'Test_smooth_long_scrolloff_4', {})
1418
1419  call term_sendkeys(buf, "gj")
1420  call VerifyScreenDump(buf, 'Test_smooth_long_scrolloff_5', {})
1421
1422  call term_sendkeys(buf, "gj")
1423  call VerifyScreenDump(buf, 'Test_smooth_long_scrolloff_6', {})
1424
1425  call term_sendkeys(buf, "gk")
1426  call VerifyScreenDump(buf, 'Test_smooth_long_scrolloff_7', {})
1427
1428  call StopVimInTerminal(buf)
1429endfunc
1430
1431func Test_smooth_long_scrolloff_ruler()
1432  CheckScreendump
1433
1434  let lines =<< trim END
1435    set smoothscroll scrolloff=3 ruler
1436    call setline(1, ['one', 'two long '->repeat(100), 'three', 'four', 'five', 'six'])
1437  END
1438  call writefile(lines, 'XSmoothLongScrolloff', 'D')
1439  let buf = RunVimInTerminal('-u NONE -S XSmoothLongScrolloff', #{rows: 8, cols: 40})
1440  call term_sendkeys(buf, ":norm j721|\<CR>")
1441  call VerifyScreenDump(buf, 'Test_smooth_long_scrolloff_ru_1', {})
1442
1443  call term_sendkeys(buf, "gj")
1444  call VerifyScreenDump(buf, 'Test_smooth_long_scrolloff_ru_2', {})
1445
1446  call term_sendkeys(buf, "gj")
1447  call VerifyScreenDump(buf, 'Test_smooth_long_scrolloff_ru_3', {})
1448
1449  call term_sendkeys(buf, "gj")
1450  call VerifyScreenDump(buf, 'Test_smooth_long_scrolloff_ru_4', {})
1451
1452  call term_sendkeys(buf, "gj")
1453  call VerifyScreenDump(buf, 'Test_smooth_long_scrolloff_ru_5', {})
1454
1455  call term_sendkeys(buf, "gj")
1456  call VerifyScreenDump(buf, 'Test_smooth_long_scrolloff_ru_6', {})
1457
1458  call term_sendkeys(buf, "gk")
1459  call VerifyScreenDump(buf, 'Test_smooth_long_scrolloff_ru_7', {})
1460
1461  call StopVimInTerminal(buf)
1462endfunc
1463
1464func Test_smoothscroll_listchars_eol()
1465  call NewWindow(10, 40)
1466  setlocal list listchars=eol:$ scrolloff=0 smoothscroll
1467  call setline(1, repeat('-', 40))
1468  call append(1, repeat(['foobar'], 10))
1469
1470  normal! G
1471  call assert_equal(2, line('w0'))
1472  call assert_equal(0, winsaveview().skipcol)
1473
1474  exe "normal! \<C-Y>"
1475  call assert_equal(1, line('w0'))
1476  call assert_equal(40, winsaveview().skipcol)
1477
1478  exe "normal! \<C-Y>"
1479  call assert_equal(1, line('w0'))
1480  call assert_equal(0, winsaveview().skipcol)
1481
1482  exe "normal! \<C-Y>"
1483  call assert_equal(1, line('w0'))
1484  call assert_equal(0, winsaveview().skipcol)
1485
1486  exe "normal! \<C-E>"
1487  call assert_equal(1, line('w0'))
1488  call assert_equal(40, winsaveview().skipcol)
1489
1490  exe "normal! \<C-E>"
1491  call assert_equal(2, line('w0'))
1492  call assert_equal(0, winsaveview().skipcol)
1493
1494  for ve in ['', 'all', 'onemore']
1495    let &virtualedit = ve
1496    normal! gg
1497    call assert_equal(1, line('w0'))
1498    call assert_equal(0, winsaveview().skipcol)
1499
1500    exe "normal! \<C-E>"
1501    redraw  " redrawing should not cause another scroll
1502    call assert_equal(1, line('w0'))
1503    call assert_equal(40, winsaveview().skipcol)
1504
1505    exe "normal! \<C-E>"
1506    redraw
1507    call assert_equal(2, line('w0'))
1508    call assert_equal(0, winsaveview().skipcol)
1509
1510    if ve != 'all'
1511      call assert_equal([0, 2, 1, 0], getpos('.'))
1512    endif
1513  endfor
1514
1515  set virtualedit&
1516  bwipe!
1517endfunc
1518
1519" scrolloffpad contract:
1520" - augment scrolloff only under EOF pressure (insufficient real lines below);
1521" - do not change explicit "z" viewport placement command semantics;
1522" - current scope is EOF-only, so BOF behavior remains unchanged.
1523func Test_scrolloffpad_zb_keeps_bottom_command_semantics()
1524  new
1525  resize 12
1526  setlocal scrolloff=10
1527  call setline(1, map(range(1, 300), 'printf("line %d", v:val)'))
1528
1529  setlocal scrolloffpad=0
1530  normal! gg150Gzb
1531  let baseline = [line('.'), line('w$'), winline()]
1532
1533  setlocal scrolloffpad=1
1534  normal! gg150Gzb
1535  call assert_equal(baseline, [line('.'), line('w$'), winline()])
1536
1537  bwipe!
1538endfunc
1539
1540func Test_scrolloffpad_zminus_keeps_bottom_beginline_semantics()
1541  new
1542  resize 12
1543  setlocal scrolloff=10
1544  call setline(1, map(range(1, 300), 'printf("    line %d", v:val)'))
1545
1546  setlocal scrolloffpad=0
1547  normal! gg150Gz-
1548  let baseline = [line('.'), line('w$'), winline(), col('.')]
1549  call assert_equal(match(getline('.'), '\S') + 1, col('.'))
1550
1551  setlocal scrolloffpad=1
1552  normal! gg150Gz-
1553  call assert_equal(baseline, [line('.'), line('w$'), winline(), col('.')])
1554  call assert_equal(match(getline('.'), '\S') + 1, col('.'))
1555
1556  bwipe!
1557endfunc
1558
1559func Test_scrolloffpad_zb_is_one_shot_then_scrolloff_reapplies()
1560  new
1561  resize 12
1562  setlocal scrolloff=10
1563  call setline(1, map(range(1, 300), 'printf("line %d", v:val)'))
1564
1565  let after_zb = {}
1566  let after_j = {}
1567  for sop in [0, 1]
1568    let &l:scrolloffpad = sop
1569    normal! gg150Gzb
1570    let after_zb[sop] = [line('.'), line('w$'), winline(), winsaveview().topline]
1571
1572    normal! j
1573    let after_j[sop] = [line('.'), line('w$'), winline(), winsaveview().topline]
1574    call assert_notequal(after_zb[sop][3], after_j[sop][3])
1575    call assert_true(line('.') < line('w$'))
1576  endfor
1577  call assert_equal(after_zb[0], after_zb[1])
1578  call assert_equal(after_j[0], after_j[1])
1579
1580  bwipe!
1581endfunc
1582
1583func Test_scrolloffpad_has_no_mid_buffer_effect()
1584  new
1585  resize 12
1586  setlocal scrolloff=10 scrolloffpad=0
1587  call setline(1, map(range(1, 500), 'printf("line %d", v:val)'))
1588
1589  normal! gg150G
1590  let topline_without_pad = winsaveview().topline
1591
1592  setlocal scrolloffpad=1
1593  normal! gg150G
1594  let topline_with_pad = winsaveview().topline
1595
1596  call assert_equal(topline_without_pad, topline_with_pad)
1597
1598  bwipe!
1599endfunc
1600
1601func Test_scrolloffpad_changes_eof_pressure_only()
1602  new
1603  resize 12
1604  setlocal scrolloff=10 scrolloffpad=0
1605  call setline(1, map(range(1, 200), 'printf("line %d", v:val)'))
1606
1607  normal! ggG
1608  let view_without_pad = winsaveview()
1609  let cursor_without_pad = line('.')
1610  let row_without_pad = winline()
1611
1612  setlocal scrolloffpad=1
1613  normal! ggG
1614  let view_with_pad = winsaveview()
1615  let row_with_pad = winline()
1616
1617  call assert_equal(line('$'), line('.'))
1618  call assert_equal(cursor_without_pad, line('.'))
1619  call assert_notequal(view_without_pad.topline, view_with_pad.topline)
1620  call assert_true(row_with_pad < row_without_pad)
1621
1622  bwipe!
1623endfunc
1624
1625func Test_scrolloffpad_large_scrolloff_no_overflow()
1626  new
1627  resize 12
1628  call setline(1, map(range(1, 200), 'printf("line %d", v:val)'))
1629  setlocal scrolloff=2147483647 scrolloffpad=0
1630
1631  normal! ggG
1632  let view_without_pad = winsaveview()
1633  let row_without_pad = winline()
1634
1635  setlocal scrolloffpad=1
1636  normal! ggG
1637  let view_with_pad = winsaveview()
1638  let row_with_pad = winline()
1639
1640  call assert_equal(line('$'), line('.'))
1641  call assert_notequal(view_without_pad.topline, view_with_pad.topline)
1642  call assert_true(row_with_pad < row_without_pad)
1643
1644  bwipe!
1645endfunc
1646
1647func Test_scrolloffpad_boolean_gate_values()
1648  new
1649  resize 12
1650  setlocal scrolloff=10
1651  call setline(1, map(range(1, 200), 'printf("line %d", v:val)'))
1652
1653  let views = {}
1654  let rows = {}
1655  for sop in [0, 1, 2]
1656    let &l:scrolloffpad = sop
1657    normal! ggG
1658    let views[sop] = winsaveview()
1659    let rows[sop] = winline()
1660    call assert_equal(line('$'), line('.'))
1661  endfor
1662
1663  call assert_equal(views[1].topline, views[2].topline)
1664  call assert_equal(rows[1], rows[2])
1665  call assert_notequal(views[0].topline, views[1].topline)
1666  call assert_true(rows[1] < rows[0])
1667
1668  bwipe!
1669endfunc
1670
1671func Test_scrolloffpad_requires_scrolloff_nonzero()
1672  new
1673  resize 12
1674  call setline(1, map(range(1, 200), 'printf("line %d", v:val)'))
1675
1676  let states = {}
1677  for so in [0, 10]
1678    let states[so] = {}
1679    for sop in [0, 1]
1680      let &l:scrolloff = so
1681      let &l:scrolloffpad = sop
1682      normal! ggG
1683      let states[so][sop] = [line('.'), line('w0'), line('w$'), winline()]
1684      call assert_equal(line('$'), line('.'))
1685    endfor
1686  endfor
1687
1688  call assert_equal(states[0][0], states[0][1])
1689  call assert_notequal(states[10][0], states[10][1])
1690  call assert_true(states[10][1][3] < states[10][0][3])
1691
1692  bwipe!
1693endfunc
1694
1695func Test_scrolloffpad_search_to_eof()
1696  new
1697  resize 12
1698  setlocal scrolloff=10
1699  call setline(1, map(range(1, 200), 'printf("line %d", v:val)'))
1700  call setline(line('$'), 'EOF TARGET')
1701
1702  let states = {}
1703  for sop in [0, 1]
1704    let &l:scrolloffpad = sop
1705    normal! gg
1706    call assert_true(search('EOF TARGET') > 0)
1707    let states[sop] = [line('.'), line('w0'), line('w$'), winline()]
1708    call assert_equal(line('$'), line('.'))
1709  endfor
1710
1711  call assert_notequal(states[0], states[1])
1712  call assert_true(states[1][3] < states[0][3])
1713
1714  bwipe!
1715endfunc
1716
1717func Test_scrolloffpad_paging_to_eof()
1718  new
1719  resize 12
1720  setlocal scrolloff=10
1721  call setline(1, map(range(1, 240), 'printf("line %d", v:val)'))
1722
1723  let states = {}
1724  for sop in [0, 1]
1725    let &l:scrolloffpad = sop
1726    normal! gg
1727
1728    let prev = -1
1729    for _ in range(1, 200)
1730      execute "normal! \<C-D>"
1731      if line('.') == prev
1732	break
1733      endif
1734      let prev = line('.')
1735    endfor
1736
1737    let states[sop] = [line('.'), line('w0'), line('w$'), winline()]
1738    call assert_equal(line('$'), line('w$'))
1739  endfor
1740
1741  call assert_notequal(states[0], states[1])
1742  call assert_true(states[1][3] < states[0][3])
1743
1744  bwipe!
1745endfunc
1746
1747func Test_scrolloffpad_ctrl_d_at_eof()
1748  new
1749  setlocal scroll=6
1750  call setline(1, map(range(1, 80), 'printf("line %d", v:val)'))
1751
1752  for height in [11, 12]
1753    execute 'resize ' .. height
1754    for so in [1, 999]
1755      let &l:scrolloff = so
1756      for sop in [0, 1]
1757        let &l:scrolloffpad = sop
1758        for endcmd in ['ggG', 'ggGzb']
1759          let context = printf('height=%d so=%d sop=%d %s',
1760                \ height, so, sop, endcmd)
1761          execute 'normal! ' .. endcmd
1762          let view_before = winsaveview()
1763
1764          call assert_beeps('execute "normal! \<C-D>"')
1765          call assert_equal(view_before, winsaveview(), context)
1766        endfor
1767      endfor
1768    endfor
1769  endfor
1770
1771  bwipe!
1772endfunc
1773
1774func Test_scrolloffpad_ctrl_e_at_eof()
1775  new
1776  call setline(1, map(range(1, 80), 'printf("line %d", v:val)'))
1777
1778  for height in [11, 12]
1779    execute 'resize ' .. height
1780    for so in [1, 999]
1781      let &l:scrolloff = so
1782      for sop in [0, 1]
1783        let &l:scrolloffpad = sop
1784        for endcmd in ['ggG', 'ggGzb']
1785          let context = printf('height=%d so=%d sop=%d %s',
1786                \ height, so, sop, endcmd)
1787          execute 'normal! ' .. endcmd
1788          let expected_view = winsaveview()
1789          let expected_view.topline += 1
1790
1791          execute "normal! \<C-E>"
1792          call assert_equal(expected_view, winsaveview(), context)
1793        endfor
1794      endfor
1795    endfor
1796  endfor
1797
1798  bwipe!
1799endfunc
1800
1801func Test_scrolloffpad_autocmd_append_at_eof()
1802  let states = {}
1803  for sop in [0, 1]
1804    new
1805    resize 12
1806    setlocal scrolloff=10
1807    let &l:scrolloffpad = sop
1808    call setline(1, map(range(1, 120), 'printf("line %d", v:val)'))
1809
1810    let b:scrolloffpad_appended = 0
1811    augroup ScrolloffpadAppendAtEof
1812      autocmd!
1813      autocmd CursorMoved <buffer> if b:scrolloffpad_appended == 0 && line('.') == line('$') | call append('$', 'appended') | let b:scrolloffpad_appended = 1 | endif
1814    augroup END
1815
1816    normal! ggG
1817    doautocmd <nomodeline> CursorMoved
1818    let states[sop] = [
1819	  \ line('.'),
1820	  \ line('$'),
1821	  \ line('w0'),
1822	  \ line('w$'),
1823	  \ winline(),
1824	  \ b:scrolloffpad_appended,
1825	  \ ]
1826
1827    call assert_equal(1, b:scrolloffpad_appended)
1828    call assert_equal(states[sop][1] - 1, states[sop][0])
1829
1830    augroup ScrolloffpadAppendAtEof
1831      autocmd!
1832    augroup END
1833    bwipe!
1834  endfor
1835
1836  call assert_notequal(states[0], states[1])
1837  call assert_true(states[1][4] < states[0][4])
1838
1839endfunc
1840
1841func Test_scrolloffpad_eof_no_reverse_scroll_on_j()
1842  new
1843  resize 20
1844  setlocal scrolloff=20 scrolloffpad=1
1845  call setline(1, map(range(1, 80), 'printf("line %d", v:val)'))
1846
1847  normal! gg
1848  let prev_topline = winsaveview().topline
1849  for lnum in range(2, line('$'))
1850    normal! j
1851    let cur_topline = winsaveview().topline
1852    call assert_true(
1853	  \ cur_topline >= prev_topline,
1854	  \ printf('topline moved backwards at line %d: %d -> %d',
1855	  \ lnum, prev_topline, cur_topline))
1856    let prev_topline = cur_topline
1857  endfor
1858
1859  bwipe!
1860endfunc
1861
1862func Test_scrolloffpad_bof_unchanged()
1863  new
1864  resize 12
1865  setlocal scrolloff=10 scrolloffpad=0
1866  call setline(1, map(range(1, 200), 'printf("line %d", v:val)'))
1867
1868  normal! Ggg
1869  let view_without_pad = winsaveview()
1870  let w0_without_pad = line('w0')
1871
1872  setlocal scrolloffpad=1
1873  normal! Ggg
1874  let view_with_pad = winsaveview()
1875  let w0_with_pad = line('w0')
1876
1877  call assert_equal(1, w0_without_pad)
1878  call assert_equal(1, w0_with_pad)
1879  call assert_equal(view_without_pad.topline, view_with_pad.topline)
1880
1881  bwipe!
1882endfunc
1883
1884func Test_scrolloffpad_mouse_drag_uses_drag_scrolloff()
1885  CheckFeature mouse
1886
1887  let save_mouse = &mouse
1888  set mouse=a
1889
1890  new
1891  resize 20
1892  call setline(1, map(range(1, 240), 'printf("line %d", v:val)'))
1893  setlocal scrolloff=50
1894
1895  let after_drag = {}
1896  for sop in [0, 1]
1897    let &l:scrolloffpad = sop
1898    normal! gg160Gzt
1899    normal! v
1900    call Ntest_setmouse(2, 1)
1901    call feedkeys("\<LeftMouse>", 'xt')
1902    call Ntest_setmouse(3, 1)
1903    call feedkeys("\<LeftDrag>", 'xt')
1904    let after_drag[sop] = [winsaveview().topline, line('.'), winline()]
1905    call feedkeys("\<Esc>", 'xt')
1906  endfor
1907
1908  call assert_equal(after_drag[0], after_drag[1])
1909
1910  bwipe!
1911  let &mouse = save_mouse
1912endfunc
1913
1914func Test_scrolloffpad_basic()
1915  CheckScreendump
1916  CheckRunVimInTerminal
1917
1918  let save_termwinsize = &termwinsize
1919  set termwinsize=
1920
1921  let lines =<< trim END
1922      set scrolloff=10
1923      set scrolloffpad=5
1924      enew!
1925      call setline(1, map(range(1, 100), 'printf("line %d", v:val)'))
1926      normal! gg
1927  END
1928  call writefile(lines, 'XScrolloffpadBasic', 'D')
1929
1930  let buf = RunVimInTerminal('-S XScrolloffpadBasic', {'rows': 20, 'cols': 78})
1931
1932  " Enabled: scrolloffpad > 0, expect EOF centering/padding
1933  call term_sendkeys(buf, "\<Esc>:\<C-U>normal! G\<CR>")
1934  call term_sendkeys(buf, "\<C-L>")
1935  call TermWait(buf)
1936  call VerifyScreenDump(buf, 'Test_scrolloffpad_basic_1', {})
1937
1938  " Beginning-of-file is unchanged (Top)
1939  call term_sendkeys(buf, "\<Esc>:\<C-U>normal! gg\<CR>")
1940  call term_sendkeys(buf, "\<C-L>")
1941  call TermWait(buf)
1942  call VerifyScreenDump(buf, 'Test_scrolloffpad_basic_2', {})
1943
1944  " Gating: disable scrolloffpad, then go to EOF again
1945  " Expect normal EOF behavior (no extra centering/padding)
1946  call term_sendkeys(buf, "\<Esc>:\<C-U>set scrolloffpad=0\<CR>")
1947  call term_sendkeys(buf, "\<Esc>:\<C-U>normal! G\<CR>")
1948  call term_sendkeys(buf, "\<C-L>")
1949  call TermWait(buf)
1950  call VerifyScreenDump(buf, 'Test_scrolloffpad_basic_3', {})
1951
1952  call StopVimInTerminal(buf)
1953  let &termwinsize = save_termwinsize
1954endfunc
1955
1956func Test_scrolloffpad_smoothscroll()
1957  CheckScreendump
1958  CheckRunVimInTerminal
1959
1960  let save_termwinsize = &termwinsize
1961  set termwinsize=
1962
1963  let lines =<< trim END
1964      set smoothscroll scrolloff=10 scrolloffpad=1
1965      enew!
1966      call setline(1, map(range(1, 100), 'printf("line %d", v:val)'))
1967      normal! gg
1968  END
1969  call writefile(lines, 'XScrolloffpadSmoothscroll', 'D')
1970
1971  let buf = RunVimInTerminal('-S XScrolloffpadSmoothscroll', #{rows: 20, cols: 78})
1972
1973  call term_sendkeys(buf, "\<Esc>:\<C-U>normal! G\<CR>")
1974  call term_sendkeys(buf, "\<C-L>")
1975  call TermWait(buf)
1976  call VerifyScreenDump(buf, 'Test_scrolloffpad_smoothscroll_1', {})
1977
1978  call term_sendkeys(buf, "\<Esc>:\<C-U>call setline(line('$'), repeat('LONG ', 30))\<CR>")
1979  call term_sendkeys(buf, "\<Esc>:\<C-U>normal! 41|\<CR>")
1980  call term_sendkeys(buf, "\<C-L>")
1981  call TermWait(buf)
1982  call VerifyScreenDump(buf, 'Test_scrolloffpad_smoothscroll_2', {})
1983
1984  call StopVimInTerminal(buf)
1985  let &termwinsize = save_termwinsize
1986endfunc
1987
1988func Test_scrolloffpad_insert_eof()
1989  let save_so = &scrolloff
1990  let save_sop = &scrolloffpad
1991
1992  set scrolloff=10 scrolloffpad=1
1993  enew!
1994  call setline(1, map(range(1, 200), 'printf("line %d", v:val)'))
1995  normal! G
1996
1997  let topline_before = winsaveview().topline
1998  call feedkeys("i\<Esc>", 'xt')
1999  call assert_equal(topline_before, winsaveview().topline)
2000
2001  exe "normal! \<C-E>"
2002  let topline_after = winsaveview().topline
2003  call feedkeys("i\<Esc>", 'xt')
2004  call assert_equal(topline_after, winsaveview().topline)
2005
2006  let &scrolloff = save_so
2007  let &scrolloffpad = save_sop
2008  bwipe!
2009endfunc
2010
2011func Test_scrolloffpad_in_diff_mode()
2012  CheckFeature diff
2013
2014  let save_so = &scrolloff
2015  let save_sop = &scrolloffpad
2016  let save_splitright = &splitright
2017
2018  set nosplitright
2019  set scrolloff=10
2020  set scrolloffpad=0
2021
2022  enew
2023  call setline(1, map(range(1, 100), {_, v -> 'line ' .. v}))
2024  diffthis
2025
2026  vnew
2027  call setline(1, map(range(1, 100), {_, v -> 'line ' .. v}))
2028  " Make buffers minimally different to avoid diff folding everything.
2029  call setline(50, 'DIFF LINE 50')
2030  diffthis
2031
2032  windo normal! zR
2033  windo normal! gg
2034  wincmd =
2035
2036  let rows_without = []
2037  let rows_with = []
2038  let near_states = []
2039  let eof_states = []
2040  for sop in [0, 1]
2041    let &scrolloffpad = sop
2042
2043    " Near EOF with real text visible in both windows.
2044    windo normal! 99G
2045    for w in range(1, winnr('$'))
2046      execute w .. 'wincmd w'
2047      let state = [line('.'), line('w0'), line('w$'), winline()]
2048      call assert_equal(99, state[0])
2049      call assert_equal(100, state[2])
2050      if sop == 0
2051	call add(near_states, state)
2052      endif
2053    endfor
2054    call assert_equal(near_states[0], near_states[1])
2055
2056    " EOF in both windows: scrolloffpad should raise the cursor row.
2057    windo normal! G
2058    for w in range(1, winnr('$'))
2059      execute w .. 'wincmd w'
2060      let state = [line('.'), line('w0'), line('w$'), winline()]
2061      call assert_equal(line('$'), state[0])
2062      if sop == 0
2063	call add(eof_states, state)
2064	call add(rows_without, state[3])
2065      else
2066	call add(rows_with, state[3])
2067      endif
2068    endfor
2069    call assert_equal(eof_states[0], eof_states[1])
2070  endfor
2071
2072  call assert_true(rows_with[0] < rows_without[0])
2073  call assert_true(rows_with[1] < rows_without[1])
2074
2075  windo diffoff
2076  %bwipe!
2077  let &scrolloff = save_so
2078  let &scrolloffpad = save_sop
2079  let &splitright = save_splitright
2080endfunc
2081
2082func Test_scrolloffpad_diff_eof_filler_behavior()
2083  CheckFeature diff
2084
2085  let save_so = &scrolloff
2086  let save_sop = &scrolloffpad
2087  let save_diffopt = &diffopt
2088  let save_splitright = &splitright
2089
2090  set diffopt+=filler
2091  set scrolloff=10
2092  set scrolloffpad=0
2093  set nosplitright
2094
2095  20new
2096  call setline(1, map(range(1, 100), {_, v -> 'left ' .. v}))
2097  diffthis
2098  let short_wid = win_getid()
2099
2100  vnew
2101  call setline(1, map(range(1, 120), {_, v -> 'right ' .. v}))
2102  diffthis
2103  let long_wid = win_getid()
2104
2105  call assert_true(win_gotoid(short_wid))
2106  let short_height = winheight(0)
2107  call assert_true(win_gotoid(long_wid))
2108  let long_height = winheight(0)
2109  call assert_equal(short_height, long_height)
2110  call assert_equal(20, short_height)
2111
2112  let ordered_diff_wids = [long_wid, short_wid]
2113  let states = {}
2114  for sop in [0, 1]
2115    execute 'set scrolloffpad=' .. sop
2116    for wid in ordered_diff_wids
2117      call assert_true(win_gotoid(wid))
2118      normal! gg
2119    endfor
2120    for wid in ordered_diff_wids
2121      call assert_true(win_gotoid(wid))
2122      normal! G
2123    endfor
2124
2125    call assert_true(win_gotoid(short_wid))
2126    let short_view = winsaveview()
2127    let short_state = [
2128          \ line('.'),
2129          \ line('$'),
2130          \ winline(),
2131          \ short_view.topline,
2132          \ short_view.topfill,
2133          \ diff_filler(line('$') + 1),
2134          \ ]
2135    call assert_equal(short_state[1], short_state[0])
2136    call assert_true(short_state[5] > 0)
2137
2138    call assert_true(win_gotoid(long_wid))
2139    let long_view = winsaveview()
2140    let long_state = [
2141          \ line('.'),
2142          \ line('$'),
2143          \ winline(),
2144          \ long_view.topline,
2145          \ long_view.topfill,
2146          \ ]
2147    call assert_true(long_state[0] > 0 && long_state[0] <= long_state[1])
2148    call assert_equal(short_state[0], long_state[0])
2149
2150    let states[sop] = [short_state, long_state]
2151  endfor
2152
2153  let short_without = states[0][0]
2154  let short_with = states[1][0]
2155  " Environment/layout can shift direction of movement; require only that
2156  " scrolloffpad changes the short-window viewport state under EOF filler.
2157  call assert_true(short_with[2] != short_without[2]
2158	\ || short_with[3] != short_without[3]
2159	\ || short_with[4] != short_without[4])
2160
2161  windo diffoff
2162  call assert_true(win_gotoid(short_wid))
2163  only!
2164  %bwipe!
2165  let &scrolloff = save_so
2166  let &scrolloffpad = save_sop
2167  let &diffopt = save_diffopt
2168  let &splitright = save_splitright
2169endfunc
2170
2171func Test_scrolloffpad_with_folds()
2172  CheckRunVimInTerminal
2173  CheckFeature folding
2174  let g:test_is_flaky = 1
2175
2176  let save_termwinsize = &termwinsize
2177  set termwinsize=
2178
2179  let lines =<< trim END
2180      set scrolloff=10
2181      set scrolloffpad=1
2182
2183      enew
2184      call setline(1, map(range(1, 120), {_, v -> 'line ' . v}))
2185
2186      " Create a large fold near the end of the file.
2187      " Fold lines 60-110, leaving 111-120 visible after the fold.
2188      set foldmethod=manual
2189      set foldenable
2190      normal! gg
2191      normal! 60G
2192      normal! zf50j
2193      normal! gg
2194  END
2195  call writefile(lines, 'XScrolloffpadFolds', 'D')
2196
2197  let buf = RunVimInTerminal('-S XScrolloffpadFolds', #{rows: 20, cols: 78})
2198
2199  " Case 1: Jump to end-of-file.
2200  " With folds present, scrolloffpad should still keep the cursor positioned
2201  " with padding below EOF.
2202  call term_sendkeys(buf, "\<Esc>:\<C-U>normal! G\<CR>")
2203  call term_sendkeys(buf, "\<C-L>")
2204  call WaitForAssert({-> assert_equal('line 120', trim(term_getline(buf, 10)))},
2205        \ 1000)
2206  call assert_equal('line 111', trim(term_getline(buf, 1)))
2207  call assert_equal('line 119', trim(term_getline(buf, 9)))
2208  call assert_equal('~', trim(term_getline(buf, 11)))
2209  call assert_match('120,1\s\+Bot', term_getline(buf, 20))
2210
2211  " Case 2: Move to the folded line to ensure the fold is actually in view.
2212  call term_sendkeys(buf, "\<Esc>:\<C-U>normal! 60G\<CR>")
2213  call term_sendkeys(buf, "\<C-L>")
2214  call WaitForAssert({-> assert_match('^\s*+-- 51 lines: line 60--',
2215        \ term_getline(buf, 10))}, 1000)
2216  call assert_equal('line 51', trim(term_getline(buf, 1)))
2217  call assert_equal('line 59', trim(term_getline(buf, 9)))
2218  call assert_equal('line 111', trim(term_getline(buf, 11)))
2219  call assert_equal('line 119', trim(term_getline(buf, 19)))
2220  call assert_match('60,1\s\+98%', term_getline(buf, 20))
2221
2222  " Case 3: Close the fold explicitly and go to EOF again.
2223  " Behavior should remain stable with closed folds.
2224  call term_sendkeys(buf, "\<Esc>:\<C-U>normal! zc\<CR>")
2225  call term_sendkeys(buf, "\<Esc>:\<C-U>normal! G\<CR>")
2226  call term_sendkeys(buf, "\<C-L>")
2227  call WaitForAssert({-> assert_equal('line 120', trim(term_getline(buf, 10)))},
2228        \ 1000)
2229  call assert_equal('line 111', trim(term_getline(buf, 1)))
2230  call assert_equal('line 119', trim(term_getline(buf, 9)))
2231  call assert_equal('~', trim(term_getline(buf, 11)))
2232  call assert_match('120,1\s\+Bot', term_getline(buf, 20))
2233
2234  call StopVimInTerminal(buf)
2235  let &termwinsize = save_termwinsize
2236endfunc
2237
2238" Resizing to "textoff" after 'smoothscroll' skips part of a wrapped line must
2239" not crash.
2240func Test_smoothscroll_textoff_showbreak()
2241  CheckOption smoothscroll
2242  CheckRunVimInTerminal
2243
2244  let donefile = 'XTest_crash_textoff_showbreak_done'
2245  defer delete(donefile)
2246  let lines =<< trim END
2247    set noswapfile
2248
2249    set scrolloff=0
2250    set lines=12 columns=40
2251
2252    call setline(1, 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa')
2253    set number wrap smoothscroll showbreak=>
2254    vsplit
2255
2256    let textoff = getwininfo(win_getid())[0].textoff
2257    execute "normal! 0\<C-E>"
2258    redraw
2259    execute 'vertical resize' textoff
2260    redraw
2261    call writefile(['done'], 'XTest_crash_textoff_showbreak_done')
2262  END
2263  call writefile(lines, 'XTest_crash_textoff_showbreak', 'D')
2264
2265  let buf = 0
2266  let buf = term_start([GetVimProg(), '--clean'], #{term_rows: 24, term_cols: 80})
2267  call TermWait(buf, 200)
2268  call term_sendkeys(buf, ":source XTest_crash_textoff_showbreak\<CR>")
2269  call WaitForAssert({-> assert_true(filereadable(donefile))})
2270  let status = term_getstatus(buf)
2271  call assert_equal('running', status)
2272  call assert_true(filereadable(donefile))
2273  call StopVimInTerminal(buf)
2274endfunc
2275
2276" comp_botline() reuses the line heights computed for the previous redraw.
2277" Changing an option that affects the displayed height of a line must
2278" invalidate that cache.  Check that scrolling after such a change gives the
2279" same result as setting the option before scrolling.
2280func Test_botline_cache_invalidated_on_option_change()
2281  " Resizing the height of a vertically split window pushes the reclaimed
2282  " lines into 'cmdheight'; save and restore it so the following tests run
2283  " with a full-height screen.
2284  let save_cmdheight = &cmdheight
2285  vnew
2286  vertical resize 40
2287  resize 10
2288  setlocal scrolloff=0
2289  let lines = map(range(1, 400), {_, v -> printf('%4d ', v) .. repeat('word ', 30)})
2290
2291  for opt in ['number', 'breakindent', 'foldcolumn=4', 'list', 'nowrap']
2292    " Reference: option set before scrolling, so no stale cache is possible.
2293    call setline(1, lines)
2294    setlocal wrap nonumber nobreakindent nolist foldcolumn=0
2295    exe 'setlocal ' .. opt
2296    normal! 120Gzt
2297    redraw
2298    let expected = getwininfo(win_getid())[0].botline
2299
2300    " Same option applied after scrolling, when the cache is populated.
2301    setlocal wrap nonumber nobreakindent nolist foldcolumn=0
2302    normal! 120Gzt
2303    redraw
2304    exe 'setlocal ' .. opt
2305    redraw
2306    call assert_equal(expected, getwininfo(win_getid())[0].botline, 'setlocal ' .. opt)
2307  endfor
2308
2309  bwipe!
2310  let &cmdheight = save_cmdheight
2311endfunc
2312
2313" vim: shiftwidth=2 sts=2 expandtab