blob: 96afce068fcc3be9b882fc78d5d148abf449a1a3 [file] [log] [blame]
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001" Test for reset 'scroll' and 'smoothscroll'
2
3source check.vim
4source screendump.vim
Yee Cheng Chine6392b12022-11-19 14:31:08 +00005source mouse.vim
Bram Moolenaaraf2d20c2017-10-29 15:26:57 +01006
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
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +020040
Bram Moolenaar8df97482022-10-03 12:11:13 +010041func Test_CtrlE_CtrlY_stop_at_end()
42 enew
43 call setline(1, ['one', 'two'])
44 set number
45 exe "normal \<C-Y>"
46 call assert_equal([" 1 one "], ScreenLines(1, 10))
47 exe "normal \<C-E>\<C-E>\<C-E>"
48 call assert_equal([" 2 two "], ScreenLines(1, 10))
49
50 bwipe!
51 set nonumber
52endfunc
53
Bram Moolenaarf6196f42022-10-02 21:29:55 +010054func Test_smoothscroll_CtrlE_CtrlY()
55 CheckScreendump
56
57 let lines =<< trim END
58 vim9script
59 setline(1, [
60 'line one',
61 'word '->repeat(20),
62 'line three',
63 'long word '->repeat(7),
64 'line',
65 'line',
66 'line',
67 ])
68 set smoothscroll
69 :5
70 END
71 call writefile(lines, 'XSmoothScroll', 'D')
72 let buf = RunVimInTerminal('-S XSmoothScroll', #{rows: 12, cols: 40})
73
74 call term_sendkeys(buf, "\<C-E>")
75 call VerifyScreenDump(buf, 'Test_smoothscroll_1', {})
76 call term_sendkeys(buf, "\<C-E>")
77 call VerifyScreenDump(buf, 'Test_smoothscroll_2', {})
78 call term_sendkeys(buf, "\<C-E>")
79 call VerifyScreenDump(buf, 'Test_smoothscroll_3', {})
80 call term_sendkeys(buf, "\<C-E>")
81 call VerifyScreenDump(buf, 'Test_smoothscroll_4', {})
82
83 call term_sendkeys(buf, "\<C-Y>")
84 call VerifyScreenDump(buf, 'Test_smoothscroll_5', {})
85 call term_sendkeys(buf, "\<C-Y>")
86 call VerifyScreenDump(buf, 'Test_smoothscroll_6', {})
87 call term_sendkeys(buf, "\<C-Y>")
88 call VerifyScreenDump(buf, 'Test_smoothscroll_7', {})
89 call term_sendkeys(buf, "\<C-Y>")
90 call VerifyScreenDump(buf, 'Test_smoothscroll_8', {})
91
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +010092 if has('folding')
93 call term_sendkeys(buf, ":set foldmethod=indent\<CR>")
94 " move the cursor so we can reuse the same dumps
95 call term_sendkeys(buf, "5G")
96 call term_sendkeys(buf, "\<C-E>")
97 call VerifyScreenDump(buf, 'Test_smoothscroll_1', {})
98 call term_sendkeys(buf, "\<C-E>")
99 call VerifyScreenDump(buf, 'Test_smoothscroll_2', {})
100 call term_sendkeys(buf, "7G")
101 call term_sendkeys(buf, "\<C-Y>")
102 call VerifyScreenDump(buf, 'Test_smoothscroll_7', {})
103 call term_sendkeys(buf, "\<C-Y>")
104 call VerifyScreenDump(buf, 'Test_smoothscroll_8', {})
105 endif
106
Bram Moolenaarf6196f42022-10-02 21:29:55 +0100107 call StopVimInTerminal(buf)
108endfunc
109
Bram Moolenaarb6aab8f2022-10-03 20:01:16 +0100110func Test_smoothscroll_number()
111 CheckScreendump
112
113 let lines =<< trim END
114 vim9script
115 setline(1, [
116 'one ' .. 'word '->repeat(20),
117 'two ' .. 'long word '->repeat(7),
118 'line',
119 'line',
120 'line',
121 ])
122 set smoothscroll
123 set number cpo+=n
124 :3
Bram Moolenaareb4de622022-10-15 13:42:17 +0100125
126 def g:DoRel()
127 set number relativenumber scrolloff=0
128 :%del
129 setline(1, [
130 'one',
131 'very long text '->repeat(12),
132 'three',
133 ])
134 exe "normal 2Gzt\<C-E>"
135 enddef
Bram Moolenaarb6aab8f2022-10-03 20:01:16 +0100136 END
137 call writefile(lines, 'XSmoothNumber', 'D')
138 let buf = RunVimInTerminal('-S XSmoothNumber', #{rows: 12, cols: 40})
139
140 call VerifyScreenDump(buf, 'Test_smooth_number_1', {})
141 call term_sendkeys(buf, "\<C-E>")
142 call VerifyScreenDump(buf, 'Test_smooth_number_2', {})
143 call term_sendkeys(buf, "\<C-E>")
144 call VerifyScreenDump(buf, 'Test_smooth_number_3', {})
145
146 call term_sendkeys(buf, ":set cpo-=n\<CR>")
147 call VerifyScreenDump(buf, 'Test_smooth_number_4', {})
148 call term_sendkeys(buf, "\<C-Y>")
149 call VerifyScreenDump(buf, 'Test_smooth_number_5', {})
150 call term_sendkeys(buf, "\<C-Y>")
151 call VerifyScreenDump(buf, 'Test_smooth_number_6', {})
152
Bram Moolenaareb4de622022-10-15 13:42:17 +0100153 call term_sendkeys(buf, ":call DoRel()\<CR>")
154 call VerifyScreenDump(buf, 'Test_smooth_number_7', {})
155
Bram Moolenaarb6aab8f2022-10-03 20:01:16 +0100156 call StopVimInTerminal(buf)
157endfunc
158
Bram Moolenaar13cdde32022-10-15 14:07:48 +0100159func Test_smoothscroll_list()
160 CheckScreendump
161
162 let lines =<< trim END
163 vim9script
164 set smoothscroll scrolloff=0
165 set list
166 setline(1, [
167 'one',
168 'very long text '->repeat(12),
169 'three',
170 ])
171 exe "normal 2Gzt\<C-E>"
172 END
173 call writefile(lines, 'XSmoothList', 'D')
174 let buf = RunVimInTerminal('-S XSmoothList', #{rows: 8, cols: 40})
175
176 call VerifyScreenDump(buf, 'Test_smooth_list_1', {})
177
178 call term_sendkeys(buf, ":set listchars+=precedes:#\<CR>")
179 call VerifyScreenDump(buf, 'Test_smooth_list_2', {})
180
181 call StopVimInTerminal(buf)
182endfunc
183
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +0100184func Test_smoothscroll_diff_mode()
185 CheckScreendump
186
187 let lines =<< trim END
188 vim9script
189 var text = 'just some text here'
190 setline(1, text)
191 set smoothscroll
192 diffthis
193 new
194 setline(1, text)
195 set smoothscroll
196 diffthis
197 END
198 call writefile(lines, 'XSmoothDiff', 'D')
199 let buf = RunVimInTerminal('-S XSmoothDiff', #{rows: 8})
200
201 call VerifyScreenDump(buf, 'Test_smooth_diff_1', {})
202 call term_sendkeys(buf, "\<C-Y>")
203 call VerifyScreenDump(buf, 'Test_smooth_diff_1', {})
204 call term_sendkeys(buf, "\<C-E>")
205 call VerifyScreenDump(buf, 'Test_smooth_diff_1', {})
206
207 call StopVimInTerminal(buf)
208endfunc
209
Bram Moolenaar9bab7a02022-10-06 14:57:53 +0100210func Test_smoothscroll_wrap_scrolloff_zero()
211 CheckScreendump
212
213 let lines =<< trim END
214 vim9script
215 setline(1, ['Line' .. (' with some text'->repeat(7))]->repeat(7))
216 set smoothscroll scrolloff=0
217 :3
218 END
219 call writefile(lines, 'XSmoothWrap', 'D')
220 let buf = RunVimInTerminal('-S XSmoothWrap', #{rows: 8, cols: 40})
221
222 call VerifyScreenDump(buf, 'Test_smooth_wrap_1', {})
223
Bram Moolenaar46b54742022-10-06 15:46:49 +0100224 " moving cursor down - whole bottom line shows
Bram Moolenaar9bab7a02022-10-06 14:57:53 +0100225 call term_sendkeys(buf, "j")
226 call VerifyScreenDump(buf, 'Test_smooth_wrap_2', {})
227
228 call term_sendkeys(buf, "\<C-E>j")
229 call VerifyScreenDump(buf, 'Test_smooth_wrap_3', {})
230
231 call term_sendkeys(buf, "G")
232 call VerifyScreenDump(buf, 'Test_smooth_wrap_4', {})
233
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +0000234 " moving cursor up right after the >>> marker - no need to show whole line
235 call term_sendkeys(buf, "2gj3l2k")
Bram Moolenaar46b54742022-10-06 15:46:49 +0100236 call VerifyScreenDump(buf, 'Test_smooth_wrap_5', {})
237
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +0000238 " moving cursor up where the >>> marker is - whole top line shows
239 call term_sendkeys(buf, "2j02k")
240 call VerifyScreenDump(buf, 'Test_smooth_wrap_6', {})
241
Bram Moolenaar9bab7a02022-10-06 14:57:53 +0100242 call StopVimInTerminal(buf)
243endfunc
Bram Moolenaarf6196f42022-10-02 21:29:55 +0100244
Bram Moolenaar8cf34592022-10-08 21:13:40 +0100245func Test_smoothscroll_wrap_long_line()
246 CheckScreendump
247
248 let lines =<< trim END
249 vim9script
Yee Cheng Chin361895d2022-11-19 12:25:16 +0000250 setline(1, ['one', 'two', 'Line' .. (' with lots of text'->repeat(30)) .. ' end', 'four'])
Bram Moolenaar8cf34592022-10-08 21:13:40 +0100251 set smoothscroll scrolloff=0
252 normal 3G10|zt
253 END
254 call writefile(lines, 'XSmoothWrap', 'D')
255 let buf = RunVimInTerminal('-S XSmoothWrap', #{rows: 6, cols: 40})
256 call VerifyScreenDump(buf, 'Test_smooth_long_1', {})
257
258 " scrolling up, cursor moves screen line down
259 call term_sendkeys(buf, "\<C-E>")
260 call VerifyScreenDump(buf, 'Test_smooth_long_2', {})
261 call term_sendkeys(buf, "5\<C-E>")
262 call VerifyScreenDump(buf, 'Test_smooth_long_3', {})
263
264 " scrolling down, cursor moves screen line up
265 call term_sendkeys(buf, "5\<C-Y>")
266 call VerifyScreenDump(buf, 'Test_smooth_long_4', {})
267 call term_sendkeys(buf, "\<C-Y>")
268 call VerifyScreenDump(buf, 'Test_smooth_long_5', {})
269
Bram Moolenaar118c2352022-10-09 17:19:27 +0100270 " 'scrolloff' set to 1, scrolling up, cursor moves screen line down
271 call term_sendkeys(buf, ":set scrolloff=1\<CR>")
272 call term_sendkeys(buf, "10|\<C-E>")
273 call VerifyScreenDump(buf, 'Test_smooth_long_6', {})
274
275 " 'scrolloff' set to 1, scrolling down, cursor moves screen line up
276 call term_sendkeys(buf, "\<C-E>")
277 call term_sendkeys(buf, "gjgj")
278 call term_sendkeys(buf, "\<C-Y>")
279 call VerifyScreenDump(buf, 'Test_smooth_long_7', {})
280
281 " 'scrolloff' set to 2, scrolling up, cursor moves screen line down
282 call term_sendkeys(buf, ":set scrolloff=2\<CR>")
283 call term_sendkeys(buf, "10|\<C-E>")
284 call VerifyScreenDump(buf, 'Test_smooth_long_8', {})
285
286 " 'scrolloff' set to 2, scrolling down, cursor moves screen line up
287 call term_sendkeys(buf, "\<C-E>")
288 call term_sendkeys(buf, "gj")
289 call term_sendkeys(buf, "\<C-Y>")
290 call VerifyScreenDump(buf, 'Test_smooth_long_9', {})
Yee Cheng Chin361895d2022-11-19 12:25:16 +0000291
292 " 'scrolloff' set to 0, move cursor down one line.
293 " Cursor should move properly, and since this is a really long line, it will
294 " be put on top of the screen.
295 call term_sendkeys(buf, ":set scrolloff=0\<CR>")
296 call term_sendkeys(buf, "0j")
297 call VerifyScreenDump(buf, 'Test_smooth_long_10', {})
298
299 " Repeat the step and move the cursor down again.
300 " This time, use a shorter long line that is barely long enough to span more
301 " than one window. Note that the cursor is at the bottom this time because
302 " Vim prefers to do so if we are scrolling a few lines only.
303 call term_sendkeys(buf, ":call setline(1, ['one', 'two', 'Line' .. (' with lots of text'->repeat(10)) .. ' end', 'four'])\<CR>")
304 call term_sendkeys(buf, "3Gzt")
305 call term_sendkeys(buf, "j")
306 call VerifyScreenDump(buf, 'Test_smooth_long_11', {})
307
308 " Repeat the step but this time start it when the line is smooth-scrolled by
309 " one line. This tests that the offset calculation is still correct and
310 " still end up scrolling down to the next line with cursor at bottom of
311 " screen.
312 call term_sendkeys(buf, "3Gzt")
313 call term_sendkeys(buf, "\<C-E>j")
314 call VerifyScreenDump(buf, 'Test_smooth_long_12', {})
Bram Moolenaar118c2352022-10-09 17:19:27 +0100315
Bram Moolenaar8cf34592022-10-08 21:13:40 +0100316 call StopVimInTerminal(buf)
317endfunc
318
Bram Moolenaar2fbabd22022-10-12 19:53:38 +0100319func Test_smoothscroll_one_long_line()
320 CheckScreendump
321
322 let lines =<< trim END
323 vim9script
324 setline(1, 'with lots of text '->repeat(7))
325 set smoothscroll scrolloff=0
326 END
327 call writefile(lines, 'XSmoothOneLong', 'D')
328 let buf = RunVimInTerminal('-S XSmoothOneLong', #{rows: 6, cols: 40})
329 call VerifyScreenDump(buf, 'Test_smooth_one_long_1', {})
330
331 call term_sendkeys(buf, "\<C-E>")
332 call VerifyScreenDump(buf, 'Test_smooth_one_long_2', {})
333
334 call term_sendkeys(buf, "0")
335 call VerifyScreenDump(buf, 'Test_smooth_one_long_1', {})
336
337 call StopVimInTerminal(buf)
338endfunc
339
Bram Moolenaar75ac25b2022-11-17 19:00:14 +0000340func Test_smoothscroll_long_line_showbreak()
341 CheckScreendump
342
343 let lines =<< trim END
344 vim9script
345 # a line that spans four screen lines
346 setline(1, 'with lots of text in one line '->repeat(6))
347 set smoothscroll scrolloff=0 showbreak=+++\
348 END
349 call writefile(lines, 'XSmoothLongShowbreak', 'D')
350 let buf = RunVimInTerminal('-S XSmoothLongShowbreak', #{rows: 6, cols: 40})
351 call VerifyScreenDump(buf, 'Test_smooth_long_showbreak_1', {})
352
Bram Moolenaar75ac25b2022-11-17 19:00:14 +0000353 call term_sendkeys(buf, "\<C-E>")
354 call VerifyScreenDump(buf, 'Test_smooth_long_showbreak_2', {})
355
356 call term_sendkeys(buf, "0")
357 call VerifyScreenDump(buf, 'Test_smooth_long_showbreak_1', {})
358
359 call StopVimInTerminal(buf)
360endfunc
361
Yee Cheng Chin01ee52b2022-11-17 12:41:42 +0000362" Test that if the current cursor is on a smooth scrolled line, we correctly
363" reposition it. Also check that we don't miscalculate the values by checking
364" the consistency between wincol() and col('.') as they are calculated
365" separately in code.
366func Test_smoothscroll_cursor_position()
367 call NewWindow(10, 20)
368 setl smoothscroll wrap
369 call setline(1, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
370
371 func s:check_col_calc(win_col, win_line, buf_col)
372 call assert_equal(a:win_col, wincol())
373 call assert_equal(a:win_line, winline())
374 call assert_equal(a:buf_col, col('.'))
375 endfunc
376
377 call s:check_col_calc(1, 1, 1)
378 exe "normal \<C-E>"
379
380 " Move down another line to avoid blocking the <<< display
381 call s:check_col_calc(1, 2, 41)
382 exe "normal \<C-Y>"
383 call s:check_col_calc(1, 3, 41)
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +0000384
385 normal gg3l
Yee Cheng Chin01ee52b2022-11-17 12:41:42 +0000386 exe "normal \<C-E>"
387
388 " Move down only 1 line when we are out of the range of the <<< display
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +0000389 call s:check_col_calc(4, 1, 24)
390 exe "normal \<C-Y>"
391 call s:check_col_calc(4, 2, 24)
392 normal ggg$
393 exe "normal \<C-E>"
Yee Cheng Chin01ee52b2022-11-17 12:41:42 +0000394 call s:check_col_calc(20, 1, 40)
395 exe "normal \<C-Y>"
396 call s:check_col_calc(20, 2, 40)
397 normal gg
398
399 " Test number, where we have indented lines
400 setl number
401 call s:check_col_calc(5, 1, 1)
402 exe "normal \<C-E>"
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +0000403
404 " Move down only 1 line when the <<< display is on the number column
405 call s:check_col_calc(5, 1, 17)
Yee Cheng Chin01ee52b2022-11-17 12:41:42 +0000406 exe "normal \<C-Y>"
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +0000407 call s:check_col_calc(5, 2, 17)
Yee Cheng Chin01ee52b2022-11-17 12:41:42 +0000408 normal ggg$
409 exe "normal \<C-E>"
410 call s:check_col_calc(20, 1, 32)
411 exe "normal \<C-Y>"
412 call s:check_col_calc(20, 2, 32)
413 normal gg
414
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +0000415 setl numberwidth=1
416
417 " Move down another line when numberwidth is too short to cover the whole
418 " <<< display
419 call s:check_col_calc(3, 1, 1)
420 exe "normal \<C-E>"
421 call s:check_col_calc(3, 2, 37)
422 exe "normal \<C-Y>"
423 call s:check_col_calc(3, 3, 37)
424 normal ggl
425
426 " Only move 1 line down when we are just past the <<< display
427 call s:check_col_calc(4, 1, 2)
428 exe "normal \<C-E>"
429 call s:check_col_calc(4, 1, 20)
430 exe "normal \<C-Y>"
431 call s:check_col_calc(4, 2, 20)
432 normal gg
433 setl numberwidth&
434
Yee Cheng Chin01ee52b2022-11-17 12:41:42 +0000435 " Test number + showbreak, so test that the additional indentation works
436 setl number showbreak=+++
437 call s:check_col_calc(5, 1, 1)
438 exe "normal \<C-E>"
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +0000439 call s:check_col_calc(8, 1, 17)
Yee Cheng Chin01ee52b2022-11-17 12:41:42 +0000440 exe "normal \<C-Y>"
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +0000441 call s:check_col_calc(8, 2, 17)
Yee Cheng Chin01ee52b2022-11-17 12:41:42 +0000442 normal gg
443
444 " Test number + cpo+=n mode, where wrapped lines aren't indented
445 setl number cpo+=n showbreak=
446 call s:check_col_calc(5, 1, 1)
447 exe "normal \<C-E>"
448 call s:check_col_calc(1, 2, 37)
449 exe "normal \<C-Y>"
450 call s:check_col_calc(1, 3, 37)
451 normal gg
452
453 bwipeout!
454endfunc
455
Yee Cheng Chine6392b12022-11-19 14:31:08 +0000456" Test that mouse picking is still accurate when we have smooth scrolled lines
457func Test_smoothscroll_mouse_pos()
458 CheckNotGui
459 CheckUnix
460
461 let save_mouse = &mouse
462 let save_term = &term
463 let save_ttymouse = &ttymouse
464 set mouse=a term=xterm ttymouse=xterm2
465
466 call NewWindow(10, 20)
467 setl smoothscroll wrap
468 " First line will wrap to 3 physical lines. 2nd/3rd lines are short lines.
469 call setline(1, ["abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", "line 2", "line 3"])
470
471 func s:check_mouse_click(row, col, buf_row, buf_col)
472 call MouseLeftClick(a:row, a:col)
473
474 call assert_equal(a:col, wincol())
475 call assert_equal(a:row, winline())
476 call assert_equal(a:buf_row, line('.'))
477 call assert_equal(a:buf_col, col('.'))
478 endfunc
479
480 " Check that clicking without scroll works first.
481 call s:check_mouse_click(3, 5, 1, 45)
482 call s:check_mouse_click(4, 1, 2, 1)
483 call s:check_mouse_click(4, 6, 2, 6)
484 call s:check_mouse_click(5, 1, 3, 1)
485 call s:check_mouse_click(5, 6, 3, 6)
486
487 " Smooth scroll, and checks that this didn't mess up mouse clicking
488 exe "normal \<C-E>"
489 call s:check_mouse_click(2, 5, 1, 45)
490 call s:check_mouse_click(3, 1, 2, 1)
491 call s:check_mouse_click(3, 6, 2, 6)
492 call s:check_mouse_click(4, 1, 3, 1)
493 call s:check_mouse_click(4, 6, 3, 6)
494
495 exe "normal \<C-E>"
496 call s:check_mouse_click(1, 5, 1, 45)
497 call s:check_mouse_click(2, 1, 2, 1)
498 call s:check_mouse_click(2, 6, 2, 6)
499 call s:check_mouse_click(3, 1, 3, 1)
500 call s:check_mouse_click(3, 6, 3, 6)
501
502 " Make a new first line 11 physical lines tall so it's taller than window
503 " height, to test overflow calculations with really long lines wrapping.
504 normal gg
505 call setline(1, "12345678901234567890"->repeat(11))
506 exe "normal 6\<C-E>"
507 call s:check_mouse_click(5, 1, 1, 201)
508 call s:check_mouse_click(6, 1, 2, 1)
509 call s:check_mouse_click(7, 1, 3, 1)
510
511 let &mouse = save_mouse
512 let &term = save_term
513 let &ttymouse = save_ttymouse
514endfunc
515
Bram Moolenaarf6196f42022-10-02 21:29:55 +0100516
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200517" vim: shiftwidth=2 sts=2 expandtab