blob: 2f9b1cef7e48110b991bb7f3ece630a35433294a [file] [log] [blame]
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02001" Test for various Normal mode commands
2
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01003source shared.vim
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004source check.vim
Bram Moolenaarca68ae12020-03-30 19:32:53 +02005source view_util.vim
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00006source vim9.vim
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01007
Bram Moolenaar1bbb6192018-11-10 16:02:01 +01008func Setup_NewWindow()
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02009 10new
10 call setline(1, range(1,100))
11endfunc
12
Bram Moolenaar1bbb6192018-11-10 16:02:01 +010013func MyFormatExpr()
Bram Moolenaar87bc3f72016-09-03 17:33:54 +020014 " Adds '->$' at lines having numbers followed by trailing whitespace
15 for ln in range(v:lnum, v:lnum+v:count-1)
16 let line = getline(ln)
17 if getline(ln) =~# '\d\s\+$'
18 call setline(ln, substitute(line, '\s\+$', '', '') . '->$')
19 endif
20 endfor
Bram Moolenaar2931f2a2016-09-09 16:59:08 +020021endfunc
Bram Moolenaar87bc3f72016-09-03 17:33:54 +020022
Bram Moolenaar1bbb6192018-11-10 16:02:01 +010023func CountSpaces(type, ...)
Bram Moolenaar87bc3f72016-09-03 17:33:54 +020024 " for testing operatorfunc
25 " will count the number of spaces
26 " and return the result in g:a
27 let sel_save = &selection
28 let &selection = "inclusive"
29 let reg_save = @@
30
31 if a:0 " Invoked from Visual mode, use gv command.
32 silent exe "normal! gvy"
33 elseif a:type == 'line'
34 silent exe "normal! '[V']y"
35 else
36 silent exe "normal! `[v`]y"
37 endif
Bram Moolenaar777e7c22021-10-25 17:07:04 +010038 let g:a = strlen(substitute(@@, '[^ ]', '', 'g'))
Bram Moolenaar87bc3f72016-09-03 17:33:54 +020039 let &selection = sel_save
40 let @@ = reg_save
Bram Moolenaar2931f2a2016-09-09 16:59:08 +020041endfunc
42
Bram Moolenaar1bbb6192018-11-10 16:02:01 +010043func OpfuncDummy(type, ...)
Bram Moolenaar4a08b0d2016-11-05 21:55:13 +010044 " for testing operatorfunc
Bram Moolenaar777e7c22021-10-25 17:07:04 +010045 let g:opt = &linebreak
Bram Moolenaar4a08b0d2016-11-05 21:55:13 +010046
47 if a:0 " Invoked from Visual mode, use gv command.
48 silent exe "normal! gvy"
49 elseif a:type == 'line'
50 silent exe "normal! '[V']y"
51 else
52 silent exe "normal! `[v`]y"
53 endif
54 " Create a new dummy window
55 new
Bram Moolenaar777e7c22021-10-25 17:07:04 +010056 let g:bufnr = bufnr('%')
Bram Moolenaar2931f2a2016-09-09 16:59:08 +020057endfunc
Bram Moolenaar87bc3f72016-09-03 17:33:54 +020058
Bram Moolenaar1671f442020-03-10 07:48:13 +010059func Test_normal00_optrans()
Bram Moolenaar87bc3f72016-09-03 17:33:54 +020060 new
61 call append(0, ['1 This is a simple test: abcd', '2 This is the second line', '3 this is the third line'])
62 1
63 exe "norm! Sfoobar\<esc>"
64 call assert_equal(['foobar', '2 This is the second line', '3 this is the third line', ''], getline(1,'$'))
65 2
Bram Moolenaar87bc3f72016-09-03 17:33:54 +020066 exe "norm! $vbsone"
67 call assert_equal(['foobar', '2 This is the second one', '3 this is the third line', ''], getline(1,'$'))
Bram Moolenaar87bc3f72016-09-03 17:33:54 +020068 norm! VS Second line here
69 call assert_equal(['foobar', ' Second line here', '3 this is the third line', ''], getline(1, '$'))
70 %d
71 call append(0, ['4 This is a simple test: abcd', '5 This is the second line', '6 this is the third line'])
72 call append(0, ['1 This is a simple test: abcd', '2 This is the second line', '3 this is the third line'])
73
74 1
75 norm! 2D
76 call assert_equal(['3 this is the third line', '4 This is a simple test: abcd', '5 This is the second line', '6 this is the third line', ''], getline(1,'$'))
77 set cpo+=#
78 norm! 4D
79 call assert_equal(['', '4 This is a simple test: abcd', '5 This is the second line', '6 this is the third line', ''], getline(1,'$'))
80
81 " clean up
82 set cpo-=#
83 bw!
Bram Moolenaar2931f2a2016-09-09 16:59:08 +020084endfunc
Bram Moolenaar87bc3f72016-09-03 17:33:54 +020085
Bram Moolenaar1bbb6192018-11-10 16:02:01 +010086func Test_normal01_keymodel()
Bram Moolenaar87bc3f72016-09-03 17:33:54 +020087 call Setup_NewWindow()
88 " Test 1: depending on 'keymodel' <s-down> does something different
Bram Moolenaar2931f2a2016-09-09 16:59:08 +020089 50
Bram Moolenaar87bc3f72016-09-03 17:33:54 +020090 call feedkeys("V\<S-Up>y", 'tx')
91 call assert_equal(['47', '48', '49', '50'], getline("'<", "'>"))
Bram Moolenaar2931f2a2016-09-09 16:59:08 +020092 set keymodel=startsel
93 50
Bram Moolenaar87bc3f72016-09-03 17:33:54 +020094 call feedkeys("V\<S-Up>y", 'tx')
95 call assert_equal(['49', '50'], getline("'<", "'>"))
96 " Start visual mode when keymodel = startsel
Bram Moolenaar2931f2a2016-09-09 16:59:08 +020097 50
Bram Moolenaar87bc3f72016-09-03 17:33:54 +020098 call feedkeys("\<S-Up>y", 'tx')
99 call assert_equal(['49', '5'], getreg(0, 0, 1))
Bram Moolenaar1671f442020-03-10 07:48:13 +0100100 " Use the different Shift special keys
101 50
102 call feedkeys("\<S-Right>\<S-Left>\<S-Up>\<S-Down>\<S-Home>\<S-End>y", 'tx')
103 call assert_equal(['50'], getline("'<", "'>"))
104 call assert_equal(['50', ''], getreg(0, 0, 1))
105
Bram Moolenaar87bc3f72016-09-03 17:33:54 +0200106 " Do not start visual mode when keymodel=
Bram Moolenaar2931f2a2016-09-09 16:59:08 +0200107 set keymodel=
108 50
Bram Moolenaar87bc3f72016-09-03 17:33:54 +0200109 call feedkeys("\<S-Up>y$", 'tx')
110 call assert_equal(['42'], getreg(0, 0, 1))
Bram Moolenaar2931f2a2016-09-09 16:59:08 +0200111 " Stop visual mode when keymodel=stopsel
112 set keymodel=stopsel
113 50
114 call feedkeys("Vkk\<Up>yy", 'tx')
115 call assert_equal(['47'], getreg(0, 0, 1))
116
117 set keymodel=
118 50
119 call feedkeys("Vkk\<Up>yy", 'tx')
120 call assert_equal(['47', '48', '49', '50'], getreg(0, 0, 1))
Bram Moolenaar87bc3f72016-09-03 17:33:54 +0200121
Bram Moolenaard7e5e942020-10-07 16:54:52 +0200122 " Test for using special keys to start visual selection
123 %d
124 call setline(1, ['red fox tail', 'red fox tail', 'red fox tail'])
125 set keymodel=startsel
126 " Test for <S-PageUp> and <S-PageDown>
127 call cursor(1, 1)
128 call feedkeys("\<S-PageDown>y", 'xt')
129 call assert_equal([0, 1, 1, 0], getpos("'<"))
130 call assert_equal([0, 3, 1, 0], getpos("'>"))
131 call feedkeys("Gz\<CR>8|\<S-PageUp>y", 'xt')
132 call assert_equal([0, 2, 1, 0], getpos("'<"))
133 call assert_equal([0, 3, 8, 0], getpos("'>"))
134 " Test for <S-C-Home> and <S-C-End>
135 call cursor(2, 12)
136 call feedkeys("\<S-C-Home>y", 'xt')
137 call assert_equal([0, 1, 1, 0], getpos("'<"))
138 call assert_equal([0, 2, 12, 0], getpos("'>"))
139 call cursor(1, 4)
140 call feedkeys("\<S-C-End>y", 'xt')
141 call assert_equal([0, 1, 4, 0], getpos("'<"))
142 call assert_equal([0, 3, 13, 0], getpos("'>"))
143 " Test for <S-C-Left> and <S-C-Right>
144 call cursor(2, 5)
145 call feedkeys("\<S-C-Right>y", 'xt')
146 call assert_equal([0, 2, 5, 0], getpos("'<"))
147 call assert_equal([0, 2, 9, 0], getpos("'>"))
148 call cursor(2, 9)
149 call feedkeys("\<S-C-Left>y", 'xt')
150 call assert_equal([0, 2, 5, 0], getpos("'<"))
151 call assert_equal([0, 2, 9, 0], getpos("'>"))
152
153 set keymodel&
154
Bram Moolenaar87bc3f72016-09-03 17:33:54 +0200155 " clean up
156 bw!
157endfunc
158
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100159func Test_normal03_join()
Bram Moolenaar87bc3f72016-09-03 17:33:54 +0200160 " basic join test
161 call Setup_NewWindow()
162 50
163 norm! VJ
164 call assert_equal('50 51', getline('.'))
165 $
166 norm! J
167 call assert_equal('100', getline('.'))
168 $
169 norm! V9-gJ
170 call assert_equal('919293949596979899100', getline('.'))
171 call setline(1, range(1,100))
172 $
173 :j 10
174 call assert_equal('100', getline('.'))
Bram Moolenaar004a6782020-04-11 17:09:31 +0200175 call assert_beeps('normal GVJ')
Bram Moolenaar87bc3f72016-09-03 17:33:54 +0200176 " clean up
177 bw!
Bram Moolenaar2931f2a2016-09-09 16:59:08 +0200178endfunc
Bram Moolenaar87bc3f72016-09-03 17:33:54 +0200179
Bram Moolenaar004a6782020-04-11 17:09:31 +0200180" basic filter test
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100181func Test_normal04_filter()
Bram Moolenaar87bc3f72016-09-03 17:33:54 +0200182 " only test on non windows platform
Bram Moolenaar004a6782020-04-11 17:09:31 +0200183 CheckNotMSWindows
Bram Moolenaar87bc3f72016-09-03 17:33:54 +0200184 call Setup_NewWindow()
185 1
186 call feedkeys("!!sed -e 's/^/| /'\n", 'tx')
187 call assert_equal('| 1', getline('.'))
188 90
189 :sil :!echo one
190 call feedkeys('.', 'tx')
191 call assert_equal('| 90', getline('.'))
192 95
193 set cpo+=!
194 " 2 <CR>, 1: for executing the command,
195 " 2: clear hit-enter-prompt
196 call feedkeys("!!\n", 'tx')
197 call feedkeys(":!echo one\n\n", 'tx')
198 call feedkeys(".", 'tx')
199 call assert_equal('one', getline('.'))
200 set cpo-=!
201 bw!
Bram Moolenaar2931f2a2016-09-09 16:59:08 +0200202endfunc
Bram Moolenaar87bc3f72016-09-03 17:33:54 +0200203
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100204func Test_normal05_formatexpr()
Bram Moolenaar87bc3f72016-09-03 17:33:54 +0200205 " basic formatexpr test
206 call Setup_NewWindow()
207 %d_
208 call setline(1, ['here: 1 ', '2', 'here: 3 ', '4', 'not here: '])
209 1
210 set formatexpr=MyFormatExpr()
211 norm! gqG
212 call assert_equal(['here: 1->$', '2', 'here: 3->$', '4', 'not here: '], getline(1,'$'))
213 set formatexpr=
214 bw!
Bram Moolenaar2931f2a2016-09-09 16:59:08 +0200215endfunc
Bram Moolenaar87bc3f72016-09-03 17:33:54 +0200216
Bram Moolenaard77f9d52016-09-04 15:13:39 +0200217func Test_normal05_formatexpr_newbuf()
218 " Edit another buffer in the 'formatexpr' function
219 new
220 func! Format()
221 edit another
222 endfunc
223 set formatexpr=Format()
224 norm gqG
225 bw!
226 set formatexpr=
227endfunc
228
229func Test_normal05_formatexpr_setopt()
230 " Change the 'formatexpr' value in the function
231 new
232 func! Format()
233 set formatexpr=
234 endfunc
235 set formatexpr=Format()
236 norm gqG
237 bw!
238 set formatexpr=
239endfunc
240
Bram Moolenaar2eaeaf32020-05-03 16:04:43 +0200241" When 'formatexpr' returns non-zero, internal formatting is used.
242func Test_normal_formatexpr_returns_nonzero()
243 new
244 call setline(1, ['one', 'two'])
245 func! Format()
246 return 1
247 endfunc
248 setlocal formatexpr=Format()
249 normal VGgq
250 call assert_equal(['one two'], getline(1, '$'))
251 setlocal formatexpr=
252 delfunc Format
253 close!
254endfunc
255
Bram Moolenaar004a6782020-04-11 17:09:31 +0200256" basic test for formatprg
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100257func Test_normal06_formatprg()
Bram Moolenaar87bc3f72016-09-03 17:33:54 +0200258 " only test on non windows platform
Bram Moolenaar004a6782020-04-11 17:09:31 +0200259 CheckNotMSWindows
Bram Moolenaar9be7c042017-01-14 14:28:30 +0100260
261 " uses sed to number non-empty lines
262 call writefile(['#!/bin/sh', 'sed ''/./=''|sed ''/./{', 'N', 's/\n/ /', '}'''], 'Xsed_format.sh')
263 call system('chmod +x ./Xsed_format.sh')
264 let text = ['a', '', 'c', '', ' ', 'd', 'e']
265 let expected = ['1 a', '', '3 c', '', '5 ', '6 d', '7 e']
266
267 10new
268 call setline(1, text)
Bram Moolenaar87bc3f72016-09-03 17:33:54 +0200269 set formatprg=./Xsed_format.sh
270 norm! gggqG
Bram Moolenaar9be7c042017-01-14 14:28:30 +0100271 call assert_equal(expected, getline(1, '$'))
Bram Moolenaar004a6782020-04-11 17:09:31 +0200272 %d
Bram Moolenaar9be7c042017-01-14 14:28:30 +0100273
Bram Moolenaar9be7c042017-01-14 14:28:30 +0100274 call setline(1, text)
275 set formatprg=donothing
276 setlocal formatprg=./Xsed_format.sh
277 norm! gggqG
278 call assert_equal(expected, getline(1, '$'))
Bram Moolenaar004a6782020-04-11 17:09:31 +0200279 %d
Bram Moolenaar9be7c042017-01-14 14:28:30 +0100280
Bram Moolenaar004a6782020-04-11 17:09:31 +0200281 " Check for the command-line ranges added to 'formatprg'
282 set formatprg=cat
283 call setline(1, ['one', 'two', 'three', 'four', 'five'])
284 call feedkeys('gggqG', 'xt')
285 call assert_equal('.,$!cat', @:)
286 call feedkeys('2Ggq2j', 'xt')
287 call assert_equal('.,.+2!cat', @:)
288
289 bw!
Bram Moolenaar87bc3f72016-09-03 17:33:54 +0200290 " clean up
291 set formatprg=
Bram Moolenaar9be7c042017-01-14 14:28:30 +0100292 setlocal formatprg=
Bram Moolenaar87bc3f72016-09-03 17:33:54 +0200293 call delete('Xsed_format.sh')
Bram Moolenaar2931f2a2016-09-09 16:59:08 +0200294endfunc
Bram Moolenaar87bc3f72016-09-03 17:33:54 +0200295
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100296func Test_normal07_internalfmt()
Bram Moolenaar87bc3f72016-09-03 17:33:54 +0200297 " basic test for internal formmatter to textwidth of 12
298 let list=range(1,11)
299 call map(list, 'v:val." "')
300 10new
301 call setline(1, list)
302 set tw=12
Bram Moolenaar004a6782020-04-11 17:09:31 +0200303 norm! ggVGgq
Bram Moolenaar87bc3f72016-09-03 17:33:54 +0200304 call assert_equal(['1 2 3', '4 5 6', '7 8 9', '10 11 '], getline(1, '$'))
305 " clean up
Bram Moolenaar9be7c042017-01-14 14:28:30 +0100306 set tw=0
Bram Moolenaar87bc3f72016-09-03 17:33:54 +0200307 bw!
Bram Moolenaar2931f2a2016-09-09 16:59:08 +0200308endfunc
Bram Moolenaar87bc3f72016-09-03 17:33:54 +0200309
Bram Moolenaar004a6782020-04-11 17:09:31 +0200310" basic tests for foldopen/folddelete
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100311func Test_normal08_fold()
Bram Moolenaar004a6782020-04-11 17:09:31 +0200312 CheckFeature folding
Bram Moolenaar87bc3f72016-09-03 17:33:54 +0200313 call Setup_NewWindow()
314 50
315 setl foldenable fdm=marker
316 " First fold
317 norm! V4jzf
318 " check that folds have been created
319 call assert_equal(['50/*{{{*/', '51', '52', '53', '54/*}}}*/'], getline(50,54))
320 " Second fold
321 46
322 norm! V10jzf
323 " check that folds have been created
324 call assert_equal('46/*{{{*/', getline(46))
325 call assert_equal('60/*}}}*/', getline(60))
326 norm! k
327 call assert_equal('45', getline('.'))
328 norm! j
329 call assert_equal('46/*{{{*/', getline('.'))
330 norm! j
331 call assert_equal('61', getline('.'))
332 norm! k
333 " open a fold
334 norm! Vzo
335 norm! k
336 call assert_equal('45', getline('.'))
337 norm! j
338 call assert_equal('46/*{{{*/', getline('.'))
339 norm! j
340 call assert_equal('47', getline('.'))
341 norm! k
342 norm! zcVzO
343 call assert_equal('46/*{{{*/', getline('.'))
344 norm! j
345 call assert_equal('47', getline('.'))
346 norm! j
347 call assert_equal('48', getline('.'))
348 norm! j
349 call assert_equal('49', getline('.'))
350 norm! j
351 call assert_equal('50/*{{{*/', getline('.'))
352 norm! j
353 call assert_equal('51', getline('.'))
354 " delete folds
355 :46
356 " collapse fold
357 norm! V14jzC
358 " delete all folds recursively
359 norm! VzD
360 call assert_equal(['46', '47', '48', '49', '50', '51', '52', '53', '54', '55', '56', '57', '58', '59', '60'], getline(46,60))
361
362 " clean up
363 setl nofoldenable fdm=marker
364 bw!
Bram Moolenaar2931f2a2016-09-09 16:59:08 +0200365endfunc
Bram Moolenaar87bc3f72016-09-03 17:33:54 +0200366
Bram Moolenaar2228cd72021-11-22 14:16:08 +0000367func Test_normal09a_operatorfunc()
Bram Moolenaar87bc3f72016-09-03 17:33:54 +0200368 " Test operatorfunc
369 call Setup_NewWindow()
370 " Add some spaces for counting
371 50,60s/$/ /
372 unlet! g:a
373 let g:a=0
374 nmap <buffer><silent> ,, :set opfunc=CountSpaces<CR>g@
375 vmap <buffer><silent> ,, :<C-U>call CountSpaces(visualmode(), 1)<CR>
376 50
377 norm V2j,,
378 call assert_equal(6, g:a)
379 norm V,,
380 call assert_equal(2, g:a)
381 norm ,,l
382 call assert_equal(0, g:a)
383 50
384 exe "norm 0\<c-v>10j2l,,"
385 call assert_equal(11, g:a)
386 50
387 norm V10j,,
388 call assert_equal(22, g:a)
389
390 " clean up
391 unmap <buffer> ,,
392 set opfunc=
Bram Moolenaar4a08b0d2016-11-05 21:55:13 +0100393 unlet! g:a
Bram Moolenaar87bc3f72016-09-03 17:33:54 +0200394 bw!
Bram Moolenaar2931f2a2016-09-09 16:59:08 +0200395endfunc
Bram Moolenaar87bc3f72016-09-03 17:33:54 +0200396
Bram Moolenaar2228cd72021-11-22 14:16:08 +0000397func Test_normal09b_operatorfunc()
Bram Moolenaar4a08b0d2016-11-05 21:55:13 +0100398 " Test operatorfunc
399 call Setup_NewWindow()
400 " Add some spaces for counting
401 50,60s/$/ /
402 unlet! g:opt
403 set linebreak
404 nmap <buffer><silent> ,, :set opfunc=OpfuncDummy<CR>g@
405 50
406 norm ,,j
407 exe "bd!" g:bufnr
408 call assert_true(&linebreak)
409 call assert_equal(g:opt, &linebreak)
410 set nolinebreak
411 norm ,,j
412 exe "bd!" g:bufnr
413 call assert_false(&linebreak)
414 call assert_equal(g:opt, &linebreak)
415
416 " clean up
417 unmap <buffer> ,,
418 set opfunc=
Bram Moolenaaree4e0c12020-04-06 21:35:05 +0200419 call assert_fails('normal Vg@', 'E774:')
Bram Moolenaar4a08b0d2016-11-05 21:55:13 +0100420 bw!
421 unlet! g:opt
422endfunc
423
Bram Moolenaar2228cd72021-11-22 14:16:08 +0000424func OperatorfuncRedo(_)
425 let g:opfunc_count = v:count
426endfunc
427
428func Test_normal09c_operatorfunc()
429 " Test redoing operatorfunc
430 new
431 call setline(1, 'some text')
432 set operatorfunc=OperatorfuncRedo
433 normal v3g@
434 call assert_equal(3, g:opfunc_count)
435 let g:opfunc_count = 0
436 normal .
437 call assert_equal(3, g:opfunc_count)
438
439 bw!
440 unlet g:opfunc_count
441 set operatorfunc=
442endfunc
443
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +0000444" Test for different ways of setting the 'operatorfunc' option
445func Test_opfunc_callback()
446 new
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000447 func OpFunc1(callnr, type)
448 let g:OpFunc1Args = [a:callnr, a:type]
449 endfunc
450 func OpFunc2(type)
451 let g:OpFunc2Args = [a:type]
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +0000452 endfunc
453
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000454 let lines =<< trim END
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000455 #" Test for using a function name
456 LET &opfunc = 'g:OpFunc2'
457 LET g:OpFunc2Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000458 normal! g@l
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000459 call assert_equal(['char'], g:OpFunc2Args)
460
461 #" Test for using a function()
462 set opfunc=function('g:OpFunc1',\ [10])
463 LET g:OpFunc1Args = []
464 normal! g@l
465 call assert_equal([10, 'char'], g:OpFunc1Args)
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +0000466
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000467 #" Using a funcref variable to set 'operatorfunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000468 VAR Fn = function('g:OpFunc1', [11])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000469 LET &opfunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000470 LET g:OpFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000471 normal! g@l
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000472 call assert_equal([11, 'char'], g:OpFunc1Args)
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +0000473
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000474 #" Using a string(funcref_variable) to set 'operatorfunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000475 LET Fn = function('g:OpFunc1', [12])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000476 LET &operatorfunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000477 LET g:OpFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000478 normal! g@l
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000479 call assert_equal([12, 'char'], g:OpFunc1Args)
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +0000480
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000481 #" Test for using a funcref()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000482 set operatorfunc=funcref('g:OpFunc1',\ [13])
483 LET g:OpFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000484 normal! g@l
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000485 call assert_equal([13, 'char'], g:OpFunc1Args)
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +0000486
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000487 #" Using a funcref variable to set 'operatorfunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000488 LET Fn = funcref('g:OpFunc1', [14])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000489 LET &opfunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000490 LET g:OpFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000491 normal! g@l
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000492 call assert_equal([14, 'char'], g:OpFunc1Args)
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +0000493
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000494 #" Using a string(funcref_variable) to set 'operatorfunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000495 LET Fn = funcref('g:OpFunc1', [15])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000496 LET &opfunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000497 LET g:OpFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000498 normal! g@l
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000499 call assert_equal([15, 'char'], g:OpFunc1Args)
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +0000500
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000501 #" Test for using a lambda function using set
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000502 VAR optval = "LSTART a LMIDDLE OpFunc1(16, a) LEND"
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000503 LET optval = substitute(optval, ' ', '\\ ', 'g')
504 exe "set opfunc=" .. optval
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000505 LET g:OpFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000506 normal! g@l
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000507 call assert_equal([16, 'char'], g:OpFunc1Args)
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +0000508
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000509 #" Test for using a lambda function using LET
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000510 LET &opfunc = LSTART a LMIDDLE OpFunc1(17, a) LEND
511 LET g:OpFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000512 normal! g@l
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000513 call assert_equal([17, 'char'], g:OpFunc1Args)
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +0000514
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000515 #" Set 'operatorfunc' to a string(lambda expression)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000516 LET &opfunc = 'LSTART a LMIDDLE OpFunc1(18, a) LEND'
517 LET g:OpFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000518 normal! g@l
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000519 call assert_equal([18, 'char'], g:OpFunc1Args)
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +0000520
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000521 #" Set 'operatorfunc' to a variable with a lambda expression
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000522 VAR Lambda = LSTART a LMIDDLE OpFunc1(19, a) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000523 LET &opfunc = Lambda
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000524 LET g:OpFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000525 normal! g@l
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000526 call assert_equal([19, 'char'], g:OpFunc1Args)
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +0000527
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000528 #" Set 'operatorfunc' to a string(variable with a lambda expression)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000529 LET Lambda = LSTART a LMIDDLE OpFunc1(20, a) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000530 LET &opfunc = string(Lambda)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000531 LET g:OpFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000532 normal! g@l
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000533 call assert_equal([20, 'char'], g:OpFunc1Args)
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +0000534
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000535 #" Try to use 'operatorfunc' after the function is deleted
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000536 func g:TmpOpFunc1(type)
537 let g:TmpOpFunc1Args = [21, a:type]
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000538 endfunc
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000539 LET &opfunc = function('g:TmpOpFunc1')
540 delfunc g:TmpOpFunc1
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000541 call test_garbagecollect_now()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000542 LET g:TmpOpFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000543 call assert_fails('normal! g@l', 'E117:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000544 call assert_equal([], g:TmpOpFunc1Args)
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +0000545
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000546 #" Try to use a function with two arguments for 'operatorfunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000547 func g:TmpOpFunc2(x, y)
548 let g:TmpOpFunc2Args = [a:x, a:y]
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000549 endfunc
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000550 set opfunc=TmpOpFunc2
551 LET g:TmpOpFunc2Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000552 call assert_fails('normal! g@l', 'E119:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000553 call assert_equal([], g:TmpOpFunc2Args)
554 delfunc TmpOpFunc2
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +0000555
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000556 #" Try to use a lambda function with two arguments for 'operatorfunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000557 LET &opfunc = LSTART a, b LMIDDLE OpFunc1(22, b) LEND
558 LET g:OpFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000559 call assert_fails('normal! g@l', 'E119:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000560 call assert_equal([], g:OpFunc1Args)
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +0000561
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000562 #" Test for clearing the 'operatorfunc' option
563 set opfunc=''
564 set opfunc&
565 call assert_fails("set opfunc=function('abc')", "E700:")
566 call assert_fails("set opfunc=funcref('abc')", "E700:")
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +0000567
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000568 #" set 'operatorfunc' to a non-existing function
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000569 LET &opfunc = function('g:OpFunc1', [23])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000570 call assert_fails("set opfunc=function('NonExistingFunc')", 'E700:')
571 call assert_fails("LET &opfunc = function('NonExistingFunc')", 'E700:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000572 LET g:OpFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000573 normal! g@l
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000574 call assert_equal([23, 'char'], g:OpFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000575 END
576 call CheckTransLegacySuccess(lines)
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +0000577
578 " Using Vim9 lambda expression in legacy context should fail
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000579 set opfunc=(a)\ =>\ OpFunc1(24,\ a)
580 let g:OpFunc1Args = []
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +0000581 call assert_fails('normal! g@l', 'E117:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000582 call assert_equal([], g:OpFunc1Args)
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +0000583
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000584 " set 'operatorfunc' to a partial with dict. This used to cause a crash.
585 func SetOpFunc()
586 let operator = {'execute': function('OperatorExecute')}
587 let &opfunc = operator.execute
588 endfunc
589 func OperatorExecute(_) dict
590 endfunc
591 call SetOpFunc()
592 call test_garbagecollect_now()
593 set operatorfunc=
594 delfunc SetOpFunc
595 delfunc OperatorExecute
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +0000596
597 " Vim9 tests
598 let lines =<< trim END
599 vim9script
600
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000601 # Test for using a def function with opfunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +0000602 def g:Vim9opFunc(val: number, type: string): void
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000603 g:OpFunc1Args = [val, type]
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +0000604 enddef
605 set opfunc=function('g:Vim9opFunc',\ [60])
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000606 g:OpFunc1Args = []
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +0000607 normal! g@l
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000608 assert_equal([60, 'char'], g:OpFunc1Args)
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +0000609 END
610 call CheckScriptSuccess(lines)
611
612 " cleanup
613 set opfunc&
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000614 delfunc OpFunc1
615 delfunc OpFunc2
616 unlet g:OpFunc1Args g:OpFunc2Args
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +0000617 %bw!
618endfunc
619
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100620func Test_normal10_expand()
Bram Moolenaar87bc3f72016-09-03 17:33:54 +0200621 " Test for expand()
622 10new
623 call setline(1, ['1', 'ifooar,,cbar'])
624 2
625 norm! $
Bram Moolenaar65f08472017-09-10 18:16:20 +0200626 call assert_equal('cbar', expand('<cword>'))
627 call assert_equal('ifooar,,cbar', expand('<cWORD>'))
628
629 call setline(1, ['prx = list[idx];'])
630 1
631 let expected = ['', 'prx', 'prx', 'prx',
632 \ 'list', 'list', 'list', 'list', 'list', 'list', 'list',
633 \ 'idx', 'idx', 'idx', 'idx',
634 \ 'list[idx]',
635 \ '];',
636 \ ]
637 for i in range(1, 16)
638 exe 'norm ' . i . '|'
639 call assert_equal(expected[i], expand('<cexpr>'), 'i == ' . i)
640 endfor
641
Bram Moolenaard7e5e942020-10-07 16:54:52 +0200642 " Test for <cexpr> in state.val and ptr->val
643 call setline(1, 'x = state.val;')
644 call cursor(1, 10)
645 call assert_equal('state.val', expand('<cexpr>'))
646 call setline(1, 'x = ptr->val;')
647 call cursor(1, 9)
648 call assert_equal('ptr->val', expand('<cexpr>'))
649
Bram Moolenaarae6f8652017-12-20 22:32:20 +0100650 if executable('echo')
651 " Test expand(`...`) i.e. backticks command expansion.
Bram Moolenaar077ff432019-10-28 00:42:21 +0100652 call assert_equal('abcde', expand('`echo abcde`'))
Bram Moolenaarae6f8652017-12-20 22:32:20 +0100653 endif
654
655 " Test expand(`=...`) i.e. backticks expression expansion
656 call assert_equal('5', expand('`=2+3`'))
Bram Moolenaar8b633132020-03-20 18:20:51 +0100657 call assert_equal('3.14', expand('`=3.14`'))
Bram Moolenaarae6f8652017-12-20 22:32:20 +0100658
Bram Moolenaar87bc3f72016-09-03 17:33:54 +0200659 " clean up
660 bw!
Bram Moolenaar2931f2a2016-09-09 16:59:08 +0200661endfunc
Bram Moolenaar87bc3f72016-09-03 17:33:54 +0200662
Bram Moolenaard7e5e942020-10-07 16:54:52 +0200663" Test for expand() in latin1 encoding
664func Test_normal_expand_latin1()
665 new
666 let save_enc = &encoding
667 set encoding=latin1
668 call setline(1, 'val = item->color;')
669 call cursor(1, 11)
670 call assert_equal('color', expand("<cword>"))
671 call assert_equal('item->color', expand("<cexpr>"))
672 let &encoding = save_enc
673 bw!
674endfunc
675
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100676func Test_normal11_showcmd()
Bram Moolenaar87bc3f72016-09-03 17:33:54 +0200677 " test for 'showcmd'
678 10new
679 exe "norm! ofoobar\<esc>"
680 call assert_equal(2, line('$'))
681 set showcmd
682 exe "norm! ofoobar2\<esc>"
683 call assert_equal(3, line('$'))
684 exe "norm! VAfoobar3\<esc>"
685 call assert_equal(3, line('$'))
686 exe "norm! 0d3\<del>2l"
687 call assert_equal('obar2foobar3', getline('.'))
Bram Moolenaard1ad99b2020-10-04 16:16:54 +0200688 " test for the visual block size displayed in the status line
689 call setline(1, ['aaaaa', 'bbbbb', 'ccccc'])
690 call feedkeys("ggl\<C-V>lljj", 'xt')
691 redraw!
692 call assert_match('3x3$', Screenline(&lines))
693 call feedkeys("\<C-V>", 'xt')
694 " test for visually selecting a multi-byte character
695 call setline(1, ["\U2206"])
696 call feedkeys("ggv", 'xt')
697 redraw!
698 call assert_match('1-3$', Screenline(&lines))
699 call feedkeys("v", 'xt')
Bram Moolenaard7e5e942020-10-07 16:54:52 +0200700 " test for visually selecting the end of line
701 call setline(1, ["foobar"])
702 call feedkeys("$vl", 'xt')
703 redraw!
704 call assert_match('2$', Screenline(&lines))
705 call feedkeys("y", 'xt')
706 call assert_equal("r\n", @")
Bram Moolenaar87bc3f72016-09-03 17:33:54 +0200707 bw!
Bram Moolenaar2931f2a2016-09-09 16:59:08 +0200708endfunc
Bram Moolenaar87bc3f72016-09-03 17:33:54 +0200709
Bram Moolenaar1671f442020-03-10 07:48:13 +0100710" Test for nv_error and normal command errors
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100711func Test_normal12_nv_error()
Bram Moolenaar87bc3f72016-09-03 17:33:54 +0200712 10new
713 call setline(1, range(1,5))
714 " should not do anything, just beep
Bram Moolenaarf5f1e102020-03-08 05:13:15 +0100715 call assert_beeps('exe "norm! <c-k>"')
Bram Moolenaar87bc3f72016-09-03 17:33:54 +0200716 call assert_equal(map(range(1,5), 'string(v:val)'), getline(1,'$'))
Bram Moolenaarf5f1e102020-03-08 05:13:15 +0100717 call assert_beeps('normal! G2dd')
718 call assert_beeps("normal! g\<C-A>")
719 call assert_beeps("normal! g\<C-X>")
720 call assert_beeps("normal! g\<C-B>")
Bram Moolenaar1671f442020-03-10 07:48:13 +0100721 call assert_beeps("normal! vQ\<Esc>")
722 call assert_beeps("normal! 2[[")
723 call assert_beeps("normal! 2]]")
724 call assert_beeps("normal! 2[]")
725 call assert_beeps("normal! 2][")
726 call assert_beeps("normal! 4[z")
727 call assert_beeps("normal! 4]z")
728 call assert_beeps("normal! 4[c")
729 call assert_beeps("normal! 4]c")
730 call assert_beeps("normal! 200%")
731 call assert_beeps("normal! %")
732 call assert_beeps("normal! 2{")
733 call assert_beeps("normal! 2}")
734 call assert_beeps("normal! r\<Right>")
735 call assert_beeps("normal! 8ry")
736 call assert_beeps('normal! "@')
Bram Moolenaar87bc3f72016-09-03 17:33:54 +0200737 bw!
Bram Moolenaar2931f2a2016-09-09 16:59:08 +0200738endfunc
Bram Moolenaar87bc3f72016-09-03 17:33:54 +0200739
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100740func Test_normal13_help()
Bram Moolenaar87bc3f72016-09-03 17:33:54 +0200741 " Test for F1
742 call assert_equal(1, winnr())
743 call feedkeys("\<f1>", 'txi')
744 call assert_match('help\.txt', bufname('%'))
745 call assert_equal(2, winnr('$'))
746 bw!
Bram Moolenaar2931f2a2016-09-09 16:59:08 +0200747endfunc
Bram Moolenaar87bc3f72016-09-03 17:33:54 +0200748
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100749func Test_normal14_page()
Bram Moolenaar87bc3f72016-09-03 17:33:54 +0200750 " basic test for Ctrl-F and Ctrl-B
751 call Setup_NewWindow()
752 exe "norm! \<c-f>"
753 call assert_equal('9', getline('.'))
754 exe "norm! 2\<c-f>"
755 call assert_equal('25', getline('.'))
756 exe "norm! 2\<c-b>"
757 call assert_equal('18', getline('.'))
758 1
759 set scrolloff=5
760 exe "norm! 2\<c-f>"
761 call assert_equal('21', getline('.'))
762 exe "norm! \<c-b>"
763 call assert_equal('13', getline('.'))
764 1
765 set scrolloff=99
766 exe "norm! \<c-f>"
767 call assert_equal('13', getline('.'))
768 set scrolloff=0
769 100
770 exe "norm! $\<c-b>"
771 call assert_equal('92', getline('.'))
772 call assert_equal([0, 92, 1, 0, 1], getcurpos())
773 100
774 set nostartofline
775 exe "norm! $\<c-b>"
776 call assert_equal('92', getline('.'))
777 call assert_equal([0, 92, 2, 0, 2147483647], getcurpos())
778 " cleanup
779 set startofline
780 bw!
Bram Moolenaar2931f2a2016-09-09 16:59:08 +0200781endfunc
Bram Moolenaar87bc3f72016-09-03 17:33:54 +0200782
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100783func Test_normal14_page_eol()
Bram Moolenaarbc54f3f2016-09-04 14:34:28 +0200784 10new
785 norm oxxxxxxx
786 exe "norm 2\<c-f>"
787 " check with valgrind that cursor is put back in column 1
788 exe "norm 2\<c-b>"
789 bw!
790endfunc
791
Bram Moolenaar1671f442020-03-10 07:48:13 +0100792" Test for errors with z command
793func Test_normal_z_error()
794 call assert_beeps('normal! z2p')
Christian Brabandt2fa93842021-05-30 22:17:25 +0200795 call assert_beeps('normal! zq')
Bram Moolenaar1671f442020-03-10 07:48:13 +0100796endfunc
797
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100798func Test_normal15_z_scroll_vert()
Bram Moolenaar87bc3f72016-09-03 17:33:54 +0200799 " basic test for z commands that scroll the window
800 call Setup_NewWindow()
801 100
802 norm! >>
803 " Test for z<cr>
804 exe "norm! z\<cr>"
805 call assert_equal(' 100', getline('.'))
806 call assert_equal(100, winsaveview()['topline'])
807 call assert_equal([0, 100, 2, 0, 9], getcurpos())
808
809 " Test for zt
810 21
811 norm! >>0zt
812 call assert_equal(' 21', getline('.'))
813 call assert_equal(21, winsaveview()['topline'])
814 call assert_equal([0, 21, 1, 0, 8], getcurpos())
815
816 " Test for zb
817 30
818 norm! >>$ztzb
819 call assert_equal(' 30', getline('.'))
820 call assert_equal(30, winsaveview()['topline']+winheight(0)-1)
821 call assert_equal([0, 30, 3, 0, 2147483647], getcurpos())
822
823 " Test for z-
824 1
825 30
826 norm! 0z-
827 call assert_equal(' 30', getline('.'))
828 call assert_equal(30, winsaveview()['topline']+winheight(0)-1)
829 call assert_equal([0, 30, 2, 0, 9], getcurpos())
830
831 " Test for z{height}<cr>
832 call assert_equal(10, winheight(0))
833 exe "norm! z12\<cr>"
834 call assert_equal(12, winheight(0))
835 exe "norm! z10\<cr>"
836 call assert_equal(10, winheight(0))
837
838 " Test for z.
839 1
840 21
841 norm! 0z.
842 call assert_equal(' 21', getline('.'))
843 call assert_equal(17, winsaveview()['topline'])
844 call assert_equal([0, 21, 2, 0, 9], getcurpos())
845
846 " Test for zz
847 1
848 21
849 norm! 0zz
850 call assert_equal(' 21', getline('.'))
851 call assert_equal(17, winsaveview()['topline'])
852 call assert_equal([0, 21, 1, 0, 8], getcurpos())
853
854 " Test for z+
855 11
856 norm! zt
857 norm! z+
858 call assert_equal(' 21', getline('.'))
859 call assert_equal(21, winsaveview()['topline'])
860 call assert_equal([0, 21, 2, 0, 9], getcurpos())
861
862 " Test for [count]z+
863 1
864 norm! 21z+
865 call assert_equal(' 21', getline('.'))
866 call assert_equal(21, winsaveview()['topline'])
867 call assert_equal([0, 21, 2, 0, 9], getcurpos())
868
Bram Moolenaar8a9bc952020-10-02 18:48:07 +0200869 " Test for z+ with [count] greater than buffer size
870 1
871 norm! 1000z+
872 call assert_equal(' 100', getline('.'))
873 call assert_equal(100, winsaveview()['topline'])
874 call assert_equal([0, 100, 2, 0, 9], getcurpos())
875
876 " Test for z+ from the last buffer line
877 norm! Gz.z+
878 call assert_equal(' 100', getline('.'))
879 call assert_equal(100, winsaveview()['topline'])
880 call assert_equal([0, 100, 2, 0, 9], getcurpos())
881
Bram Moolenaar87bc3f72016-09-03 17:33:54 +0200882 " Test for z^
883 norm! 22z+0
884 norm! z^
885 call assert_equal(' 21', getline('.'))
886 call assert_equal(12, winsaveview()['topline'])
887 call assert_equal([0, 21, 2, 0, 9], getcurpos())
888
Bram Moolenaar8a9bc952020-10-02 18:48:07 +0200889 " Test for z^ from first buffer line
890 norm! ggz^
891 call assert_equal('1', getline('.'))
892 call assert_equal(1, winsaveview()['topline'])
893 call assert_equal([0, 1, 1, 0, 1], getcurpos())
894
Bram Moolenaar87bc3f72016-09-03 17:33:54 +0200895 " Test for [count]z^
896 1
897 norm! 30z^
898 call assert_equal(' 21', getline('.'))
899 call assert_equal(12, winsaveview()['topline'])
900 call assert_equal([0, 21, 2, 0, 9], getcurpos())
901
902 " cleanup
903 bw!
Bram Moolenaar2931f2a2016-09-09 16:59:08 +0200904endfunc
Bram Moolenaar87bc3f72016-09-03 17:33:54 +0200905
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100906func Test_normal16_z_scroll_hor()
Bram Moolenaar87bc3f72016-09-03 17:33:54 +0200907 " basic test for z commands that scroll the window
908 10new
909 15vsp
910 set nowrap listchars=
911 let lineA='abcdefghijklmnopqrstuvwxyz'
912 let lineB='0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'
913 $put =lineA
914 $put =lineB
915 1d
916
Bram Moolenaar1671f442020-03-10 07:48:13 +0100917 " Test for zl and zh with a count
918 norm! 0z10l
919 call assert_equal([11, 1], [col('.'), wincol()])
920 norm! z4h
921 call assert_equal([11, 5], [col('.'), wincol()])
922 normal! 2gg
923
Bram Moolenaar87bc3f72016-09-03 17:33:54 +0200924 " Test for zl
925 1
926 norm! 5zl
927 call assert_equal(lineA, getline('.'))
928 call assert_equal(6, col('.'))
929 call assert_equal(5, winsaveview()['leftcol'])
930 norm! yl
931 call assert_equal('f', @0)
932
933 " Test for zh
934 norm! 2zh
935 call assert_equal(lineA, getline('.'))
936 call assert_equal(6, col('.'))
937 norm! yl
938 call assert_equal('f', @0)
939 call assert_equal(3, winsaveview()['leftcol'])
940
941 " Test for zL
942 norm! zL
943 call assert_equal(11, col('.'))
944 norm! yl
945 call assert_equal('k', @0)
946 call assert_equal(10, winsaveview()['leftcol'])
947 norm! 2zL
948 call assert_equal(25, col('.'))
949 norm! yl
950 call assert_equal('y', @0)
951 call assert_equal(24, winsaveview()['leftcol'])
952
953 " Test for zH
954 norm! 2zH
955 call assert_equal(25, col('.'))
956 call assert_equal(10, winsaveview()['leftcol'])
957 norm! yl
958 call assert_equal('y', @0)
959
960 " Test for zs
961 norm! $zs
962 call assert_equal(26, col('.'))
963 call assert_equal(25, winsaveview()['leftcol'])
964 norm! yl
965 call assert_equal('z', @0)
966
967 " Test for ze
968 norm! ze
969 call assert_equal(26, col('.'))
970 call assert_equal(11, winsaveview()['leftcol'])
971 norm! yl
972 call assert_equal('z', @0)
973
Bram Moolenaar8a9bc952020-10-02 18:48:07 +0200974 " Test for zs and ze with folds
975 %fold
976 norm! $zs
977 call assert_equal(26, col('.'))
978 call assert_equal(0, winsaveview()['leftcol'])
979 norm! yl
980 call assert_equal('z', @0)
981 norm! ze
982 call assert_equal(26, col('.'))
983 call assert_equal(0, winsaveview()['leftcol'])
984 norm! yl
985 call assert_equal('z', @0)
986
Bram Moolenaar87bc3f72016-09-03 17:33:54 +0200987 " cleanup
988 set wrap listchars=eol:$
989 bw!
Bram Moolenaar2931f2a2016-09-09 16:59:08 +0200990endfunc
Bram Moolenaar87bc3f72016-09-03 17:33:54 +0200991
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100992func Test_normal17_z_scroll_hor2()
Bram Moolenaar87bc3f72016-09-03 17:33:54 +0200993 " basic test for z commands that scroll the window
994 " using 'sidescrolloff' setting
995 10new
996 20vsp
997 set nowrap listchars= sidescrolloff=5
998 let lineA='abcdefghijklmnopqrstuvwxyz'
999 let lineB='0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'
1000 $put =lineA
1001 $put =lineB
1002 1d
1003
1004 " Test for zl
1005 1
1006 norm! 5zl
1007 call assert_equal(lineA, getline('.'))
1008 call assert_equal(11, col('.'))
1009 call assert_equal(5, winsaveview()['leftcol'])
1010 norm! yl
1011 call assert_equal('k', @0)
1012
1013 " Test for zh
1014 norm! 2zh
1015 call assert_equal(lineA, getline('.'))
1016 call assert_equal(11, col('.'))
1017 norm! yl
1018 call assert_equal('k', @0)
1019 call assert_equal(3, winsaveview()['leftcol'])
1020
1021 " Test for zL
1022 norm! 0zL
1023 call assert_equal(16, col('.'))
1024 norm! yl
1025 call assert_equal('p', @0)
1026 call assert_equal(10, winsaveview()['leftcol'])
1027 norm! 2zL
1028 call assert_equal(26, col('.'))
1029 norm! yl
1030 call assert_equal('z', @0)
1031 call assert_equal(15, winsaveview()['leftcol'])
1032
1033 " Test for zH
1034 norm! 2zH
1035 call assert_equal(15, col('.'))
1036 call assert_equal(0, winsaveview()['leftcol'])
1037 norm! yl
1038 call assert_equal('o', @0)
1039
1040 " Test for zs
1041 norm! $zs
1042 call assert_equal(26, col('.'))
1043 call assert_equal(20, winsaveview()['leftcol'])
1044 norm! yl
1045 call assert_equal('z', @0)
1046
1047 " Test for ze
1048 norm! ze
1049 call assert_equal(26, col('.'))
1050 call assert_equal(11, winsaveview()['leftcol'])
1051 norm! yl
1052 call assert_equal('z', @0)
1053
1054 " cleanup
1055 set wrap listchars=eol:$ sidescrolloff=0
1056 bw!
Bram Moolenaar2931f2a2016-09-09 16:59:08 +02001057endfunc
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02001058
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02001059" Test for commands that scroll the window horizontally. Test with folds.
1060" H, M, L, CTRL-E, CTRL-Y, CTRL-U, CTRL-D, PageUp, PageDown commands
1061func Test_vert_scroll_cmds()
Bram Moolenaar1671f442020-03-10 07:48:13 +01001062 15new
1063 call setline(1, range(1, 100))
1064 exe "normal! 30ggz\<CR>"
1065 set foldenable
1066 33,36fold
1067 40,43fold
1068 46,49fold
1069 let h = winheight(0)
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02001070
1071 " Test for H, M and L commands
Bram Moolenaar1671f442020-03-10 07:48:13 +01001072 " Top of the screen = 30
1073 " Folded lines = 9
1074 " Bottom of the screen = 30 + h + 9 - 1
1075 normal! 4L
1076 call assert_equal(35 + h, line('.'))
1077 normal! 4H
1078 call assert_equal(33, line('.'))
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02001079
Bram Moolenaar8a9bc952020-10-02 18:48:07 +02001080 " Test for using a large count value
1081 %d
1082 call setline(1, range(1, 4))
1083 norm! 6H
1084 call assert_equal(4, line('.'))
1085
1086 " Test for 'M' with folded lines
1087 %d
1088 call setline(1, range(1, 20))
1089 1,5fold
1090 norm! LM
1091 call assert_equal(12, line('.'))
1092
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02001093 " Test for the CTRL-E and CTRL-Y commands with folds
1094 %d
1095 call setline(1, range(1, 10))
1096 3,5fold
1097 exe "normal 6G3\<C-E>"
1098 call assert_equal(6, line('w0'))
1099 exe "normal 2\<C-Y>"
1100 call assert_equal(2, line('w0'))
1101
1102 " Test for CTRL-Y on a folded line
1103 %d
1104 call setline(1, range(1, 100))
1105 exe (h + 2) .. "," .. (h + 4) .. "fold"
1106 exe h + 5
1107 normal z-
1108 exe "normal \<C-Y>\<C-Y>"
1109 call assert_equal(h + 1, line('w$'))
1110
Bram Moolenaard1ad99b2020-10-04 16:16:54 +02001111 " Test for CTRL-Y from the first line and CTRL-E from the last line
1112 %d
1113 set scrolloff=2
1114 call setline(1, range(1, 4))
1115 exe "normal gg\<C-Y>"
1116 call assert_equal(1, line('w0'))
1117 call assert_equal(1, line('.'))
1118 exe "normal G4\<C-E>\<C-E>"
1119 call assert_equal(4, line('w$'))
1120 call assert_equal(4, line('.'))
1121 set scrolloff&
1122
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02001123 " Using <PageUp> and <PageDown> in an empty buffer should beep
1124 %d
1125 call assert_beeps('exe "normal \<PageUp>"')
1126 call assert_beeps('exe "normal \<C-B>"')
1127 call assert_beeps('exe "normal \<PageDown>"')
1128 call assert_beeps('exe "normal \<C-F>"')
1129
1130 " Test for <C-U> and <C-D> with fold
1131 %d
1132 call setline(1, range(1, 100))
1133 10,35fold
1134 set scroll=10
1135 exe "normal \<C-D>"
1136 call assert_equal(36, line('.'))
1137 exe "normal \<C-D>"
1138 call assert_equal(46, line('.'))
1139 exe "normal \<C-U>"
1140 call assert_equal(36, line('.'))
1141 exe "normal \<C-U>"
1142 call assert_equal(10, line('.'))
1143 exe "normal \<C-U>"
1144 call assert_equal(1, line('.'))
1145 set scroll&
1146
1147 " Test for scrolling to the top of the file with <C-U> and a fold
1148 10
1149 normal ztL
1150 exe "normal \<C-U>\<C-U>"
1151 call assert_equal(1, line('w0'))
1152
1153 " Test for CTRL-D on a folded line
1154 %d
1155 call setline(1, range(1, 100))
1156 50,100fold
1157 75
1158 normal z-
1159 exe "normal \<C-D>"
1160 call assert_equal(50, line('.'))
1161 call assert_equal(100, line('w$'))
1162 normal z.
1163 let lnum = winline()
1164 exe "normal \<C-D>"
1165 call assert_equal(lnum, winline())
1166 call assert_equal(50, line('.'))
1167 normal zt
1168 exe "normal \<C-D>"
1169 call assert_equal(50, line('w0'))
1170
Bram Moolenaard1ad99b2020-10-04 16:16:54 +02001171 " Test for <S-CR>. Page down.
1172 %d
1173 call setline(1, range(1, 100))
1174 call feedkeys("\<S-CR>", 'xt')
1175 call assert_equal(14, line('w0'))
1176 call assert_equal(28, line('w$'))
1177
1178 " Test for <S-->. Page up.
1179 call feedkeys("\<S-->", 'xt')
1180 call assert_equal(1, line('w0'))
1181 call assert_equal(15, line('w$'))
1182
Bram Moolenaar1671f442020-03-10 07:48:13 +01001183 set foldenable&
1184 close!
1185endfunc
1186
Bram Moolenaar777e7c22021-10-25 17:07:04 +01001187func Test_scroll_in_ex_mode()
1188 " This was using invalid memory because w_botline was invalid.
1189 let lines =<< trim END
1190 diffsplit
1191 norm os00(
1192 call writefile(['done'], 'Xdone')
1193 qa!
1194 END
1195 call writefile(lines, 'Xscript')
1196 call assert_equal(1, RunVim([], [], '--clean -X -Z -e -s -S Xscript'))
1197 call assert_equal(['done'], readfile('Xdone'))
1198
1199 call delete('Xscript')
1200 call delete('Xdone')
1201endfunc
1202
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02001203" Test for the 'sidescroll' option
1204func Test_sidescroll_opt()
1205 new
1206 20vnew
1207
1208 " scroll by 2 characters horizontally
1209 set sidescroll=2 nowrap
1210 call setline(1, repeat('a', 40))
1211 normal g$l
1212 call assert_equal(19, screenpos(0, 1, 21).col)
1213 normal l
1214 call assert_equal(20, screenpos(0, 1, 22).col)
1215 normal g0h
1216 call assert_equal(2, screenpos(0, 1, 2).col)
1217 call assert_equal(20, screenpos(0, 1, 20).col)
1218
1219 " when 'sidescroll' is 0, cursor positioned at the center
1220 set sidescroll=0
1221 normal g$l
1222 call assert_equal(11, screenpos(0, 1, 21).col)
1223 normal g0h
1224 call assert_equal(10, screenpos(0, 1, 10).col)
1225
1226 %bw!
1227 set wrap& sidescroll&
1228endfunc
1229
Bram Moolenaar004a6782020-04-11 17:09:31 +02001230" basic tests for foldopen/folddelete
Bram Moolenaar1bbb6192018-11-10 16:02:01 +01001231func Test_normal18_z_fold()
Bram Moolenaar004a6782020-04-11 17:09:31 +02001232 CheckFeature folding
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02001233 call Setup_NewWindow()
1234 50
1235 setl foldenable fdm=marker foldlevel=5
1236
Bram Moolenaar1671f442020-03-10 07:48:13 +01001237 call assert_beeps('normal! zj')
1238 call assert_beeps('normal! zk')
1239
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02001240 " Test for zF
1241 " First fold
1242 norm! 4zF
1243 " check that folds have been created
1244 call assert_equal(['50/*{{{*/', '51', '52', '53/*}}}*/'], getline(50,53))
1245
1246 " Test for zd
1247 51
1248 norm! 2zF
1249 call assert_equal(2, foldlevel('.'))
1250 norm! kzd
1251 call assert_equal(['50', '51/*{{{*/', '52/*}}}*/', '53'], getline(50,53))
1252 norm! j
1253 call assert_equal(1, foldlevel('.'))
1254
1255 " Test for zD
1256 " also deletes partially selected folds recursively
1257 51
1258 norm! zF
1259 call assert_equal(2, foldlevel('.'))
1260 norm! kV2jzD
1261 call assert_equal(['50', '51', '52', '53'], getline(50,53))
1262
1263 " Test for zE
1264 85
1265 norm! 4zF
1266 86
1267 norm! 2zF
1268 90
1269 norm! 4zF
1270 call assert_equal(['85/*{{{*/', '86/*{{{*/', '87/*}}}*/', '88/*}}}*/', '89', '90/*{{{*/', '91', '92', '93/*}}}*/'], getline(85,93))
1271 norm! zE
1272 call assert_equal(['85', '86', '87', '88', '89', '90', '91', '92', '93'], getline(85,93))
1273
1274 " Test for zn
1275 50
1276 set foldlevel=0
1277 norm! 2zF
1278 norm! zn
1279 norm! k
1280 call assert_equal('49', getline('.'))
1281 norm! j
1282 call assert_equal('50/*{{{*/', getline('.'))
1283 norm! j
1284 call assert_equal('51/*}}}*/', getline('.'))
1285 norm! j
1286 call assert_equal('52', getline('.'))
1287 call assert_equal(0, &foldenable)
1288
1289 " Test for zN
1290 49
1291 norm! zN
1292 call assert_equal('49', getline('.'))
1293 norm! j
1294 call assert_equal('50/*{{{*/', getline('.'))
1295 norm! j
1296 call assert_equal('52', getline('.'))
1297 call assert_equal(1, &foldenable)
1298
1299 " Test for zi
1300 norm! zi
1301 call assert_equal(0, &foldenable)
1302 norm! zi
1303 call assert_equal(1, &foldenable)
1304 norm! zi
1305 call assert_equal(0, &foldenable)
1306 norm! zi
1307 call assert_equal(1, &foldenable)
1308
1309 " Test for za
1310 50
1311 norm! za
1312 norm! k
1313 call assert_equal('49', getline('.'))
1314 norm! j
1315 call assert_equal('50/*{{{*/', getline('.'))
1316 norm! j
1317 call assert_equal('51/*}}}*/', getline('.'))
1318 norm! j
1319 call assert_equal('52', getline('.'))
1320 50
1321 norm! za
1322 norm! k
1323 call assert_equal('49', getline('.'))
1324 norm! j
1325 call assert_equal('50/*{{{*/', getline('.'))
1326 norm! j
1327 call assert_equal('52', getline('.'))
1328
1329 49
1330 norm! 5zF
1331 norm! k
1332 call assert_equal('48', getline('.'))
1333 norm! j
1334 call assert_equal('49/*{{{*/', getline('.'))
1335 norm! j
1336 call assert_equal('55', getline('.'))
1337 49
1338 norm! za
1339 call assert_equal('49/*{{{*/', getline('.'))
1340 norm! j
1341 call assert_equal('50/*{{{*/', getline('.'))
1342 norm! j
1343 call assert_equal('52', getline('.'))
1344 set nofoldenable
1345 " close fold and set foldenable
1346 norm! za
1347 call assert_equal(1, &foldenable)
1348
1349 50
1350 " have to use {count}za to open all folds and make the cursor visible
1351 norm! 2za
1352 norm! 2k
1353 call assert_equal('48', getline('.'))
1354 norm! j
1355 call assert_equal('49/*{{{*/', getline('.'))
1356 norm! j
1357 call assert_equal('50/*{{{*/', getline('.'))
1358 norm! j
1359 call assert_equal('51/*}}}*/', getline('.'))
1360 norm! j
1361 call assert_equal('52', getline('.'))
1362
1363 " Test for zA
1364 49
1365 set foldlevel=0
1366 50
1367 norm! zA
1368 norm! 2k
1369 call assert_equal('48', getline('.'))
1370 norm! j
1371 call assert_equal('49/*{{{*/', getline('.'))
1372 norm! j
1373 call assert_equal('50/*{{{*/', getline('.'))
1374 norm! j
1375 call assert_equal('51/*}}}*/', getline('.'))
1376 norm! j
1377 call assert_equal('52', getline('.'))
1378
Dominique Pelle923dce22021-11-21 11:36:04 +00001379 " zA on an opened fold when foldenable is not set
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02001380 50
1381 set nofoldenable
1382 norm! zA
1383 call assert_equal(1, &foldenable)
1384 norm! k
1385 call assert_equal('48', getline('.'))
1386 norm! j
1387 call assert_equal('49/*{{{*/', getline('.'))
1388 norm! j
1389 call assert_equal('55', getline('.'))
1390
1391 " Test for zc
1392 norm! zE
1393 50
1394 norm! 2zF
1395 49
1396 norm! 5zF
1397 set nofoldenable
1398 50
1399 " There most likely is a bug somewhere:
1400 " https://groups.google.com/d/msg/vim_dev/v2EkfJ_KQjI/u-Cvv94uCAAJ
1401 " TODO: Should this only close the inner most fold or both folds?
1402 norm! zc
1403 call assert_equal(1, &foldenable)
1404 norm! k
1405 call assert_equal('48', getline('.'))
1406 norm! j
1407 call assert_equal('49/*{{{*/', getline('.'))
1408 norm! j
1409 call assert_equal('55', getline('.'))
1410 set nofoldenable
1411 50
1412 norm! Vjzc
1413 norm! k
1414 call assert_equal('48', getline('.'))
1415 norm! j
1416 call assert_equal('49/*{{{*/', getline('.'))
1417 norm! j
1418 call assert_equal('55', getline('.'))
1419
1420 " Test for zC
1421 set nofoldenable
1422 50
1423 norm! zCk
1424 call assert_equal('48', getline('.'))
1425 norm! j
1426 call assert_equal('49/*{{{*/', getline('.'))
1427 norm! j
1428 call assert_equal('55', getline('.'))
1429
1430 " Test for zx
1431 " 1) close folds at line 49-54
1432 set nofoldenable
1433 48
1434 norm! zx
1435 call assert_equal(1, &foldenable)
1436 norm! j
1437 call assert_equal('49/*{{{*/', getline('.'))
1438 norm! j
1439 call assert_equal('55', getline('.'))
1440
Bram Moolenaar395b6ba2017-04-07 20:09:51 +02001441 " 2) do not close fold under cursor
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02001442 51
1443 set nofoldenable
1444 norm! zx
1445 call assert_equal(1, &foldenable)
1446 norm! 3k
1447 call assert_equal('48', getline('.'))
1448 norm! j
1449 call assert_equal('49/*{{{*/', getline('.'))
1450 norm! j
1451 call assert_equal('50/*{{{*/', getline('.'))
1452 norm! j
1453 call assert_equal('51/*}}}*/', getline('.'))
1454 norm! j
1455 call assert_equal('52', getline('.'))
1456 norm! j
1457 call assert_equal('53', getline('.'))
1458 norm! j
1459 call assert_equal('54/*}}}*/', getline('.'))
1460 norm! j
1461 call assert_equal('55', getline('.'))
1462
1463 " 3) close one level of folds
1464 48
1465 set nofoldenable
1466 set foldlevel=1
1467 norm! zx
1468 call assert_equal(1, &foldenable)
1469 call assert_equal('48', getline('.'))
1470 norm! j
1471 call assert_equal('49/*{{{*/', getline('.'))
1472 norm! j
1473 call assert_equal('50/*{{{*/', getline('.'))
1474 norm! j
1475 call assert_equal('52', getline('.'))
1476 norm! j
1477 call assert_equal('53', getline('.'))
1478 norm! j
1479 call assert_equal('54/*}}}*/', getline('.'))
1480 norm! j
1481 call assert_equal('55', getline('.'))
1482
1483 " Test for zX
1484 " Close all folds
1485 set foldlevel=0 nofoldenable
1486 50
1487 norm! zX
1488 call assert_equal(1, &foldenable)
1489 norm! k
1490 call assert_equal('48', getline('.'))
1491 norm! j
1492 call assert_equal('49/*{{{*/', getline('.'))
1493 norm! j
1494 call assert_equal('55', getline('.'))
1495
1496 " Test for zm
1497 50
1498 set nofoldenable foldlevel=2
1499 norm! zm
1500 call assert_equal(1, &foldenable)
1501 call assert_equal(1, &foldlevel)
1502 norm! zm
1503 call assert_equal(0, &foldlevel)
1504 norm! zm
1505 call assert_equal(0, &foldlevel)
1506 norm! k
1507 call assert_equal('48', getline('.'))
1508 norm! j
1509 call assert_equal('49/*{{{*/', getline('.'))
1510 norm! j
1511 call assert_equal('55', getline('.'))
1512
Bram Moolenaar8a9bc952020-10-02 18:48:07 +02001513 " Test for zm with a count
1514 50
1515 set foldlevel=2
1516 norm! 3zm
1517 call assert_equal(0, &foldlevel)
1518 call assert_equal(49, foldclosed(line('.')))
1519
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02001520 " Test for zM
1521 48
1522 set nofoldenable foldlevel=99
1523 norm! zM
1524 call assert_equal(1, &foldenable)
1525 call assert_equal(0, &foldlevel)
1526 call assert_equal('48', getline('.'))
1527 norm! j
1528 call assert_equal('49/*{{{*/', getline('.'))
1529 norm! j
1530 call assert_equal('55', getline('.'))
1531
1532 " Test for zr
1533 48
1534 set nofoldenable foldlevel=0
1535 norm! zr
1536 call assert_equal(0, &foldenable)
1537 call assert_equal(1, &foldlevel)
1538 set foldlevel=0 foldenable
1539 norm! zr
1540 call assert_equal(1, &foldenable)
1541 call assert_equal(1, &foldlevel)
1542 norm! zr
1543 call assert_equal(2, &foldlevel)
1544 call assert_equal('48', getline('.'))
1545 norm! j
1546 call assert_equal('49/*{{{*/', getline('.'))
1547 norm! j
1548 call assert_equal('50/*{{{*/', getline('.'))
1549 norm! j
1550 call assert_equal('51/*}}}*/', getline('.'))
1551 norm! j
1552 call assert_equal('52', getline('.'))
1553
1554 " Test for zR
1555 48
1556 set nofoldenable foldlevel=0
1557 norm! zR
1558 call assert_equal(0, &foldenable)
1559 call assert_equal(2, &foldlevel)
1560 set foldenable foldlevel=0
1561 norm! zR
1562 call assert_equal(1, &foldenable)
1563 call assert_equal(2, &foldlevel)
1564 call assert_equal('48', getline('.'))
1565 norm! j
1566 call assert_equal('49/*{{{*/', getline('.'))
1567 norm! j
1568 call assert_equal('50/*{{{*/', getline('.'))
1569 norm! j
1570 call assert_equal('51/*}}}*/', getline('.'))
1571 norm! j
1572 call assert_equal('52', getline('.'))
1573 call append(50, ['a /*{{{*/', 'b /*}}}*/'])
1574 48
1575 call assert_equal('48', getline('.'))
1576 norm! j
1577 call assert_equal('49/*{{{*/', getline('.'))
1578 norm! j
1579 call assert_equal('50/*{{{*/', getline('.'))
1580 norm! j
1581 call assert_equal('a /*{{{*/', getline('.'))
1582 norm! j
1583 call assert_equal('51/*}}}*/', getline('.'))
1584 norm! j
1585 call assert_equal('52', getline('.'))
1586 48
1587 norm! zR
1588 call assert_equal(1, &foldenable)
1589 call assert_equal(3, &foldlevel)
1590 call assert_equal('48', getline('.'))
1591 norm! j
1592 call assert_equal('49/*{{{*/', getline('.'))
1593 norm! j
1594 call assert_equal('50/*{{{*/', getline('.'))
1595 norm! j
1596 call assert_equal('a /*{{{*/', getline('.'))
1597 norm! j
1598 call assert_equal('b /*}}}*/', getline('.'))
1599 norm! j
1600 call assert_equal('51/*}}}*/', getline('.'))
1601 norm! j
1602 call assert_equal('52', getline('.'))
1603
1604 " clean up
1605 setl nofoldenable fdm=marker foldlevel=0
1606 bw!
Bram Moolenaar2931f2a2016-09-09 16:59:08 +02001607endfunc
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02001608
Bram Moolenaar1bbb6192018-11-10 16:02:01 +01001609func Test_normal20_exmode()
Bram Moolenaar004a6782020-04-11 17:09:31 +02001610 " Reading from redirected file doesn't work on MS-Windows
1611 CheckNotMSWindows
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02001612 call writefile(['1a', 'foo', 'bar', '.', 'w! Xfile2', 'q!'], 'Xscript')
1613 call writefile(['1', '2'], 'Xfile')
Bram Moolenaar93344c22019-08-14 21:12:05 +02001614 call system(GetVimCommand() .. ' -e -s < Xscript Xfile')
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02001615 let a=readfile('Xfile2')
1616 call assert_equal(['1', 'foo', 'bar', '2'], a)
1617
1618 " clean up
1619 for file in ['Xfile', 'Xfile2', 'Xscript']
1620 call delete(file)
1621 endfor
1622 bw!
Bram Moolenaar2931f2a2016-09-09 16:59:08 +02001623endfunc
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02001624
Bram Moolenaar1bbb6192018-11-10 16:02:01 +01001625func Test_normal21_nv_hat()
1626
1627 " Edit a fresh file and wipe the buffer list so that there is no alternate
1628 " file present. Next, check for the expected command failures.
1629 edit Xfoo | %bw
Bram Moolenaare2e40752020-09-04 21:18:46 +02001630 call assert_fails(':buffer #', 'E86:')
1631 call assert_fails(':execute "normal! \<C-^>"', 'E23:')
Bram Moolenaarb7e24832020-06-24 13:37:35 +02001632 call assert_fails("normal i\<C-R>#", 'E23:')
Bram Moolenaar1bbb6192018-11-10 16:02:01 +01001633
1634 " Test for the expected behavior when switching between two named buffers.
1635 edit Xfoo | edit Xbar
1636 call feedkeys("\<C-^>", 'tx')
1637 call assert_equal('Xfoo', fnamemodify(bufname('%'), ':t'))
1638 call feedkeys("\<C-^>", 'tx')
1639 call assert_equal('Xbar', fnamemodify(bufname('%'), ':t'))
1640
1641 " Test for the expected behavior when only one buffer is named.
1642 enew | let l:nr = bufnr('%')
1643 call feedkeys("\<C-^>", 'tx')
1644 call assert_equal('Xbar', fnamemodify(bufname('%'), ':t'))
1645 call feedkeys("\<C-^>", 'tx')
1646 call assert_equal('', bufname('%'))
1647 call assert_equal(l:nr, bufnr('%'))
1648
1649 " Test that no action is taken by "<C-^>" when an operator is pending.
1650 edit Xfoo
1651 call feedkeys("ci\<C-^>", 'tx')
1652 call assert_equal('Xfoo', fnamemodify(bufname('%'), ':t'))
1653
1654 %bw!
Bram Moolenaar2931f2a2016-09-09 16:59:08 +02001655endfunc
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02001656
Bram Moolenaar1bbb6192018-11-10 16:02:01 +01001657func Test_normal22_zet()
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02001658 " Test for ZZ
Bram Moolenaar0913a102016-09-03 19:11:59 +02001659 " let shell = &shell
1660 " let &shell = 'sh'
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02001661 call writefile(['1', '2'], 'Xfile')
Bram Moolenaar93344c22019-08-14 21:12:05 +02001662 let args = ' -N -i NONE --noplugins -X --not-a-term'
1663 call system(GetVimCommand() .. args .. ' -c "%d" -c ":norm! ZZ" Xfile')
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02001664 let a = readfile('Xfile')
1665 call assert_equal([], a)
1666 " Test for ZQ
1667 call writefile(['1', '2'], 'Xfile')
Bram Moolenaar93344c22019-08-14 21:12:05 +02001668 call system(GetVimCommand() . args . ' -c "%d" -c ":norm! ZQ" Xfile')
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02001669 let a = readfile('Xfile')
1670 call assert_equal(['1', '2'], a)
1671
Bram Moolenaar1671f442020-03-10 07:48:13 +01001672 " Unsupported Z command
1673 call assert_beeps('normal! ZW')
1674
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02001675 " clean up
1676 for file in ['Xfile']
1677 call delete(file)
1678 endfor
Bram Moolenaar0913a102016-09-03 19:11:59 +02001679 " let &shell = shell
Bram Moolenaar2931f2a2016-09-09 16:59:08 +02001680endfunc
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02001681
Bram Moolenaar1bbb6192018-11-10 16:02:01 +01001682func Test_normal23_K()
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02001683 " Test for K command
1684 new
Bram Moolenaar426f3752016-11-04 21:22:37 +01001685 call append(0, ['version8.txt', 'man', 'aa%bb', 'cc|dd'])
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02001686 let k = &keywordprg
1687 set keywordprg=:help
1688 1
1689 norm! VK
1690 call assert_equal('version8.txt', fnamemodify(bufname('%'), ':t'))
1691 call assert_equal('help', &ft)
1692 call assert_match('\*version8.txt\*', getline('.'))
1693 helpclose
1694 norm! 0K
1695 call assert_equal('version8.txt', fnamemodify(bufname('%'), ':t'))
1696 call assert_equal('help', &ft)
Bram Moolenaarb1c91982018-05-17 17:04:55 +02001697 call assert_match('\*version8\.\d\*', getline('.'))
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02001698 helpclose
1699
Bram Moolenaar426f3752016-11-04 21:22:37 +01001700 set keywordprg=:new
1701 set iskeyword+=%
1702 set iskeyword+=\|
1703 2
1704 norm! K
1705 call assert_equal('man', fnamemodify(bufname('%'), ':t'))
1706 bwipe!
1707 3
1708 norm! K
1709 call assert_equal('aa%bb', fnamemodify(bufname('%'), ':t'))
1710 bwipe!
Bram Moolenaareb828d02016-11-05 19:54:01 +01001711 if !has('win32')
1712 4
1713 norm! K
1714 call assert_equal('cc|dd', fnamemodify(bufname('%'), ':t'))
1715 bwipe!
1716 endif
Bram Moolenaar426f3752016-11-04 21:22:37 +01001717 set iskeyword-=%
1718 set iskeyword-=\|
1719
Bram Moolenaar8a9bc952020-10-02 18:48:07 +02001720 " Test for specifying a count to K
1721 1
1722 com! -nargs=* Kprog let g:Kprog_Args = <q-args>
1723 set keywordprg=:Kprog
1724 norm! 3K
1725 call assert_equal('3 version8', g:Kprog_Args)
1726 delcom Kprog
1727
Bram Moolenaar0913a102016-09-03 19:11:59 +02001728 " Only expect "man" to work on Unix
1729 if !has("unix")
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02001730 let &keywordprg = k
1731 bw!
1732 return
1733 endif
Bram Moolenaarc6b37db2019-04-27 18:00:34 +02001734
Bram Moolenaar9134f1e2019-11-29 20:26:13 +01001735 let not_gnu_man = has('mac') || has('bsd')
1736 if not_gnu_man
Dominique Pelle923dce22021-11-21 11:36:04 +00001737 " In macOS and BSD, the option for specifying a pager is different
Bram Moolenaarc6b37db2019-04-27 18:00:34 +02001738 set keywordprg=man\ -P\ cat
1739 else
1740 set keywordprg=man\ --pager=cat
1741 endif
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02001742 " Test for using man
1743 2
1744 let a = execute('unsilent norm! K')
Bram Moolenaar9134f1e2019-11-29 20:26:13 +01001745 if not_gnu_man
Bram Moolenaarc6b37db2019-04-27 18:00:34 +02001746 call assert_match("man -P cat 'man'", a)
1747 else
1748 call assert_match("man --pager=cat 'man'", a)
1749 endif
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02001750
Bram Moolenaar1671f442020-03-10 07:48:13 +01001751 " Error cases
1752 call setline(1, '#$#')
1753 call assert_fails('normal! ggK', 'E349:')
1754 call setline(1, '---')
1755 call assert_fails('normal! ggv2lK', 'E349:')
1756 call setline(1, ['abc', 'xyz'])
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001757 call assert_fails("normal! gg2lv2h\<C-]>", 'E433:')
Bram Moolenaar1671f442020-03-10 07:48:13 +01001758 call assert_beeps("normal! ggVjK")
Bram Moolenaar8a9bc952020-10-02 18:48:07 +02001759 norm! V
1760 call assert_beeps("norm! cK")
Bram Moolenaar1671f442020-03-10 07:48:13 +01001761
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02001762 " clean up
1763 let &keywordprg = k
1764 bw!
Bram Moolenaar2931f2a2016-09-09 16:59:08 +02001765endfunc
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02001766
Bram Moolenaar1bbb6192018-11-10 16:02:01 +01001767func Test_normal24_rot13()
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02001768 " Testing for g?? g?g?
1769 new
1770 call append(0, 'abcdefghijklmnopqrstuvwxyzäüö')
1771 1
1772 norm! g??
1773 call assert_equal('nopqrstuvwxyzabcdefghijklmäüö', getline('.'))
1774 norm! g?g?
1775 call assert_equal('abcdefghijklmnopqrstuvwxyzäüö', getline('.'))
1776
1777 " clean up
1778 bw!
Bram Moolenaar2931f2a2016-09-09 16:59:08 +02001779endfunc
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02001780
Bram Moolenaar1bbb6192018-11-10 16:02:01 +01001781func Test_normal25_tag()
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01001782 CheckFeature quickfix
1783
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02001784 " Testing for CTRL-] g CTRL-] g]
1785 " CTRL-W g] CTRL-W CTRL-] CTRL-W g CTRL-]
1786 h
1787 " Test for CTRL-]
1788 call search('\<x\>$')
1789 exe "norm! \<c-]>"
1790 call assert_equal("change.txt", fnamemodify(bufname('%'), ':t'))
1791 norm! yiW
1792 call assert_equal("*x*", @0)
1793 exe ":norm \<c-o>"
1794
1795 " Test for g_CTRL-]
1796 call search('\<v_u\>$')
1797 exe "norm! g\<c-]>"
1798 call assert_equal("change.txt", fnamemodify(bufname('%'), ':t'))
1799 norm! yiW
1800 call assert_equal("*v_u*", @0)
1801 exe ":norm \<c-o>"
1802
1803 " Test for g]
1804 call search('\<i_<Esc>$')
1805 let a = execute(":norm! g]")
1806 call assert_match('i_<Esc>.*insert.txt', a)
1807
1808 if !empty(exepath('cscope')) && has('cscope')
1809 " setting cscopetag changes how g] works
1810 set cst
1811 exe "norm! g]"
1812 call assert_equal("insert.txt", fnamemodify(bufname('%'), ':t'))
1813 norm! yiW
1814 call assert_equal("*i_<Esc>*", @0)
1815 exe ":norm \<c-o>"
1816 " Test for CTRL-W g]
1817 exe "norm! \<C-W>g]"
1818 call assert_equal("insert.txt", fnamemodify(bufname('%'), ':t'))
1819 norm! yiW
1820 call assert_equal("*i_<Esc>*", @0)
1821 call assert_equal(3, winnr('$'))
1822 helpclose
1823 set nocst
1824 endif
1825
1826 " Test for CTRL-W g]
1827 let a = execute("norm! \<C-W>g]")
1828 call assert_match('i_<Esc>.*insert.txt', a)
1829
1830 " Test for CTRL-W CTRL-]
1831 exe "norm! \<C-W>\<C-]>"
1832 call assert_equal("insert.txt", fnamemodify(bufname('%'), ':t'))
1833 norm! yiW
1834 call assert_equal("*i_<Esc>*", @0)
1835 call assert_equal(3, winnr('$'))
1836 helpclose
1837
1838 " Test for CTRL-W g CTRL-]
1839 exe "norm! \<C-W>g\<C-]>"
1840 call assert_equal("insert.txt", fnamemodify(bufname('%'), ':t'))
1841 norm! yiW
1842 call assert_equal("*i_<Esc>*", @0)
1843 call assert_equal(3, winnr('$'))
1844 helpclose
1845
1846 " clean up
1847 helpclose
Bram Moolenaar2931f2a2016-09-09 16:59:08 +02001848endfunc
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02001849
Bram Moolenaar1bbb6192018-11-10 16:02:01 +01001850func Test_normal26_put()
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02001851 " Test for ]p ]P [p and [P
1852 new
1853 call append(0, ['while read LINE', 'do', ' ((count++))', ' if [ $? -ne 0 ]; then', " echo 'Error writing file'", ' fi', 'done'])
1854 1
1855 /Error/y a
1856 2
1857 norm! "a]pj"a[p
1858 call assert_equal(['do', "echo 'Error writing file'", " echo 'Error writing file'", ' ((count++))'], getline(2,5))
1859 1
1860 /^\s\{4}/
1861 exe "norm! \"a]P3Eldt'"
1862 exe "norm! j\"a[P2Eldt'"
1863 call assert_equal([' if [ $? -ne 0 ]; then', " echo 'Error writing'", " echo 'Error'", " echo 'Error writing file'", ' fi'], getline(6,10))
1864
1865 " clean up
1866 bw!
Bram Moolenaar2931f2a2016-09-09 16:59:08 +02001867endfunc
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02001868
Bram Moolenaar1bbb6192018-11-10 16:02:01 +01001869func Test_normal27_bracket()
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02001870 " Test for [' [` ]' ]`
1871 call Setup_NewWindow()
1872 1,21s/.\+/ & b/
1873 1
1874 norm! $ma
1875 5
1876 norm! $mb
1877 10
1878 norm! $mc
1879 15
1880 norm! $md
1881 20
1882 norm! $me
1883
1884 " Test for ['
1885 9
1886 norm! 2['
1887 call assert_equal(' 1 b', getline('.'))
1888 call assert_equal(1, line('.'))
1889 call assert_equal(3, col('.'))
1890
1891 " Test for ]'
1892 norm! ]'
1893 call assert_equal(' 5 b', getline('.'))
1894 call assert_equal(5, line('.'))
1895 call assert_equal(3, col('.'))
1896
1897 " No mark after line 21, cursor moves to first non blank on current line
1898 21
1899 norm! $]'
1900 call assert_equal(' 21 b', getline('.'))
1901 call assert_equal(21, line('.'))
1902 call assert_equal(3, col('.'))
1903
1904 " Test for [`
1905 norm! 2[`
1906 call assert_equal(' 15 b', getline('.'))
1907 call assert_equal(15, line('.'))
1908 call assert_equal(8, col('.'))
1909
1910 " Test for ]`
1911 norm! ]`
1912 call assert_equal(' 20 b', getline('.'))
1913 call assert_equal(20, line('.'))
1914 call assert_equal(8, col('.'))
1915
1916 " clean up
1917 bw!
Bram Moolenaar2931f2a2016-09-09 16:59:08 +02001918endfunc
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02001919
Bram Moolenaar1671f442020-03-10 07:48:13 +01001920" Test for ( and ) sentence movements
Bram Moolenaar1bbb6192018-11-10 16:02:01 +01001921func Test_normal28_parenthesis()
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02001922 new
1923 call append(0, ['This is a test. With some sentences!', '', 'Even with a question? And one more. And no sentence here'])
1924
1925 $
1926 norm! d(
1927 call assert_equal(['This is a test. With some sentences!', '', 'Even with a question? And one more. ', ''], getline(1, '$'))
1928 norm! 2d(
1929 call assert_equal(['This is a test. With some sentences!', '', ' ', ''], getline(1, '$'))
1930 1
1931 norm! 0d)
1932 call assert_equal(['With some sentences!', '', ' ', ''], getline(1, '$'))
1933
1934 call append('$', ['This is a long sentence', '', 'spanning', 'over several lines. '])
1935 $
1936 norm! $d(
1937 call assert_equal(['With some sentences!', '', ' ', '', 'This is a long sentence', ''], getline(1, '$'))
1938
Bram Moolenaar224a5f12020-04-28 20:29:07 +02001939 " Move to the next sentence from a paragraph macro
1940 %d
1941 call setline(1, ['.LP', 'blue sky!. blue sky.', 'blue sky. blue sky.'])
1942 call cursor(1, 1)
1943 normal )
1944 call assert_equal([2, 1], [line('.'), col('.')])
1945 normal )
1946 call assert_equal([2, 12], [line('.'), col('.')])
1947 normal ((
1948 call assert_equal([1, 1], [line('.'), col('.')])
1949
Bram Moolenaar1671f442020-03-10 07:48:13 +01001950 " It is an error if a next sentence is not found
1951 %d
1952 call setline(1, '.SH')
1953 call assert_beeps('normal )')
1954
Bram Moolenaar224a5f12020-04-28 20:29:07 +02001955 " If only dot is present, don't treat that as a sentence
1956 call setline(1, '. This is a sentence.')
1957 normal $((
1958 call assert_equal(3, col('.'))
1959
Bram Moolenaar1671f442020-03-10 07:48:13 +01001960 " Jumping to a fold should open the fold
1961 call setline(1, ['', '', 'one', 'two', 'three'])
1962 set foldenable
1963 2,$fold
1964 call feedkeys(')', 'xt')
1965 call assert_equal(3, line('.'))
1966 call assert_equal(1, foldlevel('.'))
1967 call assert_equal(-1, foldclosed('.'))
1968 set foldenable&
1969
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02001970 " clean up
1971 bw!
Bram Moolenaar2931f2a2016-09-09 16:59:08 +02001972endfunc
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02001973
Bram Moolenaar1671f442020-03-10 07:48:13 +01001974" Test for { and } paragraph movements
1975func Test_normal29_brace()
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001976 let text =<< trim [DATA]
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001977 A paragraph begins after each empty line, and also at each of a set of
1978 paragraph macros, specified by the pairs of characters in the 'paragraphs'
1979 option. The default is "IPLPPPQPP TPHPLIPpLpItpplpipbp", which corresponds to
1980 the macros ".IP", ".LP", etc. (These are nroff macros, so the dot must be in
1981 the first column). A section boundary is also a paragraph boundary.
1982 Note that a blank line (only containing white space) is NOT a paragraph
1983 boundary.
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001984
1985
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001986 Also note that this does not include a '{' or '}' in the first column. When
1987 the '{' flag is in 'cpoptions' then '{' in the first column is used as a
1988 paragraph boundary |posix|.
1989 {
1990 This is no paragraph
1991 unless the '{' is set
1992 in 'cpoptions'
1993 }
1994 .IP
1995 The nroff macros IP separates a paragraph
1996 That means, it must be a '.'
1997 followed by IP
1998 .LPIt does not matter, if afterwards some
1999 more characters follow.
2000 .SHAlso section boundaries from the nroff
2001 macros terminate a paragraph. That means
2002 a character like this:
2003 .NH
2004 End of text here
Bram Moolenaarc79745a2019-05-20 22:12:34 +02002005 [DATA]
2006
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002007 new
2008 call append(0, text)
2009 1
2010 norm! 0d2}
Bram Moolenaarc79745a2019-05-20 22:12:34 +02002011
2012 let expected =<< trim [DATA]
Bram Moolenaare7eb9272019-06-24 00:58:07 +02002013 .IP
2014 The nroff macros IP separates a paragraph
2015 That means, it must be a '.'
2016 followed by IP
2017 .LPIt does not matter, if afterwards some
2018 more characters follow.
2019 .SHAlso section boundaries from the nroff
2020 macros terminate a paragraph. That means
2021 a character like this:
2022 .NH
2023 End of text here
Bram Moolenaarc79745a2019-05-20 22:12:34 +02002024
2025 [DATA]
2026 call assert_equal(expected, getline(1, '$'))
2027
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002028 norm! 0d}
Bram Moolenaarc79745a2019-05-20 22:12:34 +02002029
2030 let expected =<< trim [DATA]
Bram Moolenaare7eb9272019-06-24 00:58:07 +02002031 .LPIt does not matter, if afterwards some
2032 more characters follow.
2033 .SHAlso section boundaries from the nroff
2034 macros terminate a paragraph. That means
2035 a character like this:
2036 .NH
2037 End of text here
2038
Bram Moolenaarc79745a2019-05-20 22:12:34 +02002039 [DATA]
2040 call assert_equal(expected, getline(1, '$'))
2041
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002042 $
2043 norm! d{
Bram Moolenaarc79745a2019-05-20 22:12:34 +02002044
2045 let expected =<< trim [DATA]
Bram Moolenaare7eb9272019-06-24 00:58:07 +02002046 .LPIt does not matter, if afterwards some
2047 more characters follow.
2048 .SHAlso section boundaries from the nroff
2049 macros terminate a paragraph. That means
2050 a character like this:
Bram Moolenaarc79745a2019-05-20 22:12:34 +02002051
2052 [DATA]
2053 call assert_equal(expected, getline(1, '$'))
2054
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002055 norm! d{
Bram Moolenaarc79745a2019-05-20 22:12:34 +02002056
2057 let expected =<< trim [DATA]
Bram Moolenaare7eb9272019-06-24 00:58:07 +02002058 .LPIt does not matter, if afterwards some
2059 more characters follow.
Bram Moolenaarc79745a2019-05-20 22:12:34 +02002060
2061 [DATA]
2062 call assert_equal(expected, getline(1, '$'))
2063
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002064 " Test with { in cpooptions
2065 %d
2066 call append(0, text)
2067 set cpo+={
2068 1
2069 norm! 0d2}
Bram Moolenaarc79745a2019-05-20 22:12:34 +02002070
2071 let expected =<< trim [DATA]
Bram Moolenaare7eb9272019-06-24 00:58:07 +02002072 {
2073 This is no paragraph
2074 unless the '{' is set
2075 in 'cpoptions'
2076 }
2077 .IP
2078 The nroff macros IP separates a paragraph
2079 That means, it must be a '.'
2080 followed by IP
2081 .LPIt does not matter, if afterwards some
2082 more characters follow.
2083 .SHAlso section boundaries from the nroff
2084 macros terminate a paragraph. That means
2085 a character like this:
2086 .NH
2087 End of text here
Bram Moolenaarc79745a2019-05-20 22:12:34 +02002088
2089 [DATA]
2090 call assert_equal(expected, getline(1, '$'))
2091
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002092 $
2093 norm! d}
Bram Moolenaarc79745a2019-05-20 22:12:34 +02002094
2095 let expected =<< trim [DATA]
Bram Moolenaare7eb9272019-06-24 00:58:07 +02002096 {
2097 This is no paragraph
2098 unless the '{' is set
2099 in 'cpoptions'
2100 }
2101 .IP
2102 The nroff macros IP separates a paragraph
2103 That means, it must be a '.'
2104 followed by IP
2105 .LPIt does not matter, if afterwards some
2106 more characters follow.
2107 .SHAlso section boundaries from the nroff
2108 macros terminate a paragraph. That means
2109 a character like this:
2110 .NH
2111 End of text here
Bram Moolenaarc79745a2019-05-20 22:12:34 +02002112
2113 [DATA]
2114 call assert_equal(expected, getline(1, '$'))
2115
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002116 norm! gg}
2117 norm! d5}
Bram Moolenaarc79745a2019-05-20 22:12:34 +02002118
2119 let expected =<< trim [DATA]
Bram Moolenaare7eb9272019-06-24 00:58:07 +02002120 {
2121 This is no paragraph
2122 unless the '{' is set
2123 in 'cpoptions'
2124 }
Bram Moolenaarc79745a2019-05-20 22:12:34 +02002125
2126 [DATA]
2127 call assert_equal(expected, getline(1, '$'))
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002128
Bram Moolenaar1671f442020-03-10 07:48:13 +01002129 " Jumping to a fold should open the fold
2130 %d
2131 call setline(1, ['', 'one', 'two', ''])
2132 set foldenable
2133 2,$fold
2134 call feedkeys('}', 'xt')
2135 call assert_equal(4, line('.'))
2136 call assert_equal(1, foldlevel('.'))
2137 call assert_equal(-1, foldclosed('.'))
2138 set foldenable&
2139
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002140 " clean up
2141 set cpo-={
2142 bw!
Bram Moolenaar2931f2a2016-09-09 16:59:08 +02002143endfunc
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002144
Bram Moolenaar8a9bc952020-10-02 18:48:07 +02002145" Test for section movements
2146func Test_normal_section()
2147 new
2148 let lines =<< trim [END]
2149 int foo()
2150 {
2151 if (1)
2152 {
2153 a = 1;
2154 }
2155 }
2156 [END]
2157 call setline(1, lines)
2158
2159 " jumping to a folded line using [[ should open the fold
2160 2,3fold
2161 call cursor(5, 1)
2162 call feedkeys("[[", 'xt')
2163 call assert_equal(2, line('.'))
2164 call assert_equal(-1, foldclosedend(line('.')))
2165
2166 close!
2167endfunc
2168
Bram Moolenaard1ad99b2020-10-04 16:16:54 +02002169" Test for changing case using u, U, gu, gU and ~ (tilde) commands
Bram Moolenaar1671f442020-03-10 07:48:13 +01002170func Test_normal30_changecase()
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002171 new
2172 call append(0, 'This is a simple test: äüöß')
2173 norm! 1ggVu
2174 call assert_equal('this is a simple test: äüöß', getline('.'))
2175 norm! VU
2176 call assert_equal('THIS IS A SIMPLE TEST: ÄÜÖSS', getline('.'))
2177 norm! guu
2178 call assert_equal('this is a simple test: äüöss', getline('.'))
2179 norm! gUgU
2180 call assert_equal('THIS IS A SIMPLE TEST: ÄÜÖSS', getline('.'))
2181 norm! gugu
2182 call assert_equal('this is a simple test: äüöss', getline('.'))
2183 norm! gUU
2184 call assert_equal('THIS IS A SIMPLE TEST: ÄÜÖSS', getline('.'))
2185 norm! 010~
2186 call assert_equal('this is a SIMPLE TEST: ÄÜÖSS', getline('.'))
2187 norm! V~
2188 call assert_equal('THIS IS A simple test: äüöss', getline('.'))
Bram Moolenaard1ad99b2020-10-04 16:16:54 +02002189 call assert_beeps('norm! c~')
2190 %d
2191 call assert_beeps('norm! ~')
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002192
Bram Moolenaar1671f442020-03-10 07:48:13 +01002193 " Test for changing case across lines using 'whichwrap'
2194 call setline(1, ['aaaaaa', 'aaaaaa'])
2195 normal! gg10~
2196 call assert_equal(['AAAAAA', 'aaaaaa'], getline(1, 2))
2197 set whichwrap+=~
2198 normal! gg10~
2199 call assert_equal(['aaaaaa', 'AAAAaa'], getline(1, 2))
2200 set whichwrap&
2201
Bram Moolenaar3e72dca2021-05-29 16:30:12 +02002202 " try changing the case with a double byte encoding (DBCS)
2203 %bw!
2204 let enc = &enc
2205 set encoding=cp932
2206 call setline(1, "\u8470")
2207 normal ~
2208 normal gU$gu$gUgUg~g~gugu
2209 call assert_equal("\u8470", getline(1))
2210 let &encoding = enc
2211
Bram Moolenaar1671f442020-03-10 07:48:13 +01002212 " clean up
2213 bw!
2214endfunc
2215
2216" Turkish ASCII turns to multi-byte. On some systems Turkish locale
2217" is available but toupper()/tolower() don't do the right thing.
2218func Test_normal_changecase_turkish()
2219 new
Bram Moolenaarf1c118b2018-09-03 22:08:10 +02002220 try
2221 lang tr_TR.UTF-8
2222 set casemap=
2223 let iupper = toupper('i')
2224 if iupper == "\u0130"
Bram Moolenaar9f4de1f2017-04-08 19:39:43 +02002225 call setline(1, 'iI')
2226 1normal gUU
2227 call assert_equal("\u0130I", getline(1))
2228 call assert_equal("\u0130I", toupper("iI"))
Bram Moolenaar3317d5e2017-04-08 19:12:06 +02002229
Bram Moolenaar9f4de1f2017-04-08 19:39:43 +02002230 call setline(1, 'iI')
2231 1normal guu
2232 call assert_equal("i\u0131", getline(1))
2233 call assert_equal("i\u0131", tolower("iI"))
Bram Moolenaarf1c118b2018-09-03 22:08:10 +02002234 elseif iupper == "I"
Bram Moolenaar1cc48202017-04-09 13:41:59 +02002235 call setline(1, 'iI')
2236 1normal gUU
2237 call assert_equal("II", getline(1))
2238 call assert_equal("II", toupper("iI"))
2239
2240 call setline(1, 'iI')
2241 1normal guu
2242 call assert_equal("ii", getline(1))
2243 call assert_equal("ii", tolower("iI"))
Bram Moolenaarf1c118b2018-09-03 22:08:10 +02002244 else
2245 call assert_true(false, "expected toupper('i') to be either 'I' or '\u0130'")
2246 endif
2247 set casemap&
2248 call setline(1, 'iI')
2249 1normal gUU
2250 call assert_equal("II", getline(1))
2251 call assert_equal("II", toupper("iI"))
Bram Moolenaar1cc48202017-04-09 13:41:59 +02002252
Bram Moolenaarf1c118b2018-09-03 22:08:10 +02002253 call setline(1, 'iI')
2254 1normal guu
2255 call assert_equal("ii", getline(1))
2256 call assert_equal("ii", tolower("iI"))
2257
2258 lang en_US.UTF-8
2259 catch /E197:/
2260 " can't use Turkish locale
2261 throw 'Skipped: Turkish locale not available'
2262 endtry
Bram Moolenaar1671f442020-03-10 07:48:13 +01002263 close!
Bram Moolenaar2931f2a2016-09-09 16:59:08 +02002264endfunc
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002265
Bram Moolenaar1671f442020-03-10 07:48:13 +01002266" Test for r (replace) command
2267func Test_normal31_r_cmd()
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002268 new
2269 call append(0, 'This is a simple test: abcd')
2270 exe "norm! 1gg$r\<cr>"
2271 call assert_equal(['This is a simple test: abc', '', ''], getline(1,'$'))
2272 exe "norm! 1gg2wlr\<cr>"
2273 call assert_equal(['This is a', 'simple test: abc', '', ''], getline(1,'$'))
2274 exe "norm! 2gg0W5r\<cr>"
2275 call assert_equal(['This is a', 'simple ', ' abc', '', ''], getline('1', '$'))
2276 set autoindent
2277 call setline(2, ['simple test: abc', ''])
2278 exe "norm! 2gg0W5r\<cr>"
2279 call assert_equal(['This is a', 'simple ', 'abc', '', '', ''], getline('1', '$'))
2280 exe "norm! 1ggVr\<cr>"
2281 call assert_equal('^M^M^M^M^M^M^M^M^M', strtrans(getline(1)))
2282 call setline(1, 'This is a')
2283 exe "norm! 1gg05rf"
2284 call assert_equal('fffffis a', getline(1))
2285
Bram Moolenaar1671f442020-03-10 07:48:13 +01002286 " When replacing characters, copy characters from above and below lines
2287 " using CTRL-Y and CTRL-E.
2288 " Different code paths are used for utf-8 and latin1 encodings
2289 set showmatch
2290 for enc in ['latin1', 'utf-8']
2291 enew!
2292 let &encoding = enc
2293 call setline(1, [' {a}', 'xxxxxxxxxx', ' [b]'])
2294 exe "norm! 2gg5r\<C-Y>l5r\<C-E>"
2295 call assert_equal(' {a}x [b]x', getline(2))
2296 endfor
2297 set showmatch&
2298
2299 " r command should fail in operator pending mode
2300 call assert_beeps('normal! cr')
2301
Bram Moolenaar004a6782020-04-11 17:09:31 +02002302 " replace a tab character in visual mode
2303 %d
2304 call setline(1, ["a\tb", "c\td", "e\tf"])
2305 normal gglvjjrx
2306 call assert_equal(['axx', 'xxx', 'xxf'], getline(1, '$'))
2307
Bram Moolenaard7e5e942020-10-07 16:54:52 +02002308 " replace with a multibyte character (with multiple composing characters)
2309 %d
2310 new
2311 call setline(1, 'aaa')
2312 exe "normal $ra\u0328\u0301"
2313 call assert_equal("aaa\u0328\u0301", getline(1))
2314
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002315 " clean up
2316 set noautoindent
2317 bw!
Bram Moolenaar2931f2a2016-09-09 16:59:08 +02002318endfunc
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002319
Bram Moolenaarf5f1e102020-03-08 05:13:15 +01002320" Test for g*, g#
Bram Moolenaar1bbb6192018-11-10 16:02:01 +01002321func Test_normal32_g_cmd1()
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002322 new
2323 call append(0, ['abc.x_foo', 'x_foobar.abc'])
2324 1
2325 norm! $g*
2326 call assert_equal('x_foo', @/)
2327 call assert_equal('x_foobar.abc', getline('.'))
2328 norm! $g#
2329 call assert_equal('abc', @/)
2330 call assert_equal('abc.x_foo', getline('.'))
2331
2332 " clean up
2333 bw!
Bram Moolenaar2931f2a2016-09-09 16:59:08 +02002334endfunc
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002335
Bram Moolenaarf5f1e102020-03-08 05:13:15 +01002336" Test for g`, g;, g,, g&, gv, gk, gj, gJ, g0, g^, g_, gm, g$, gM, g CTRL-G,
2337" gi and gI commands
Bram Moolenaar1671f442020-03-10 07:48:13 +01002338func Test_normal33_g_cmd2()
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002339 call Setup_NewWindow()
2340 " Test for g`
2341 clearjumps
2342 norm! ma10j
2343 let a=execute(':jumps')
2344 " empty jumplist
2345 call assert_equal('>', a[-1:])
2346 norm! g`a
2347 call assert_equal('>', a[-1:])
2348 call assert_equal(1, line('.'))
2349 call assert_equal('1', getline('.'))
Bram Moolenaarf5f1e102020-03-08 05:13:15 +01002350 call cursor(10, 1)
2351 norm! g'a
2352 call assert_equal('>', a[-1:])
2353 call assert_equal(1, line('.'))
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002354
2355 " Test for g; and g,
2356 norm! g;
2357 " there is only one change in the changelist
2358 " currently, when we setup the window
2359 call assert_equal(2, line('.'))
Bram Moolenaare2e40752020-09-04 21:18:46 +02002360 call assert_fails(':norm! g;', 'E662:')
2361 call assert_fails(':norm! g,', 'E663:')
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002362 let &ul=&ul
2363 call append('$', ['a', 'b', 'c', 'd'])
2364 let &ul=&ul
2365 call append('$', ['Z', 'Y', 'X', 'W'])
2366 let a = execute(':changes')
2367 call assert_match('2\s\+0\s\+2', a)
2368 call assert_match('101\s\+0\s\+a', a)
2369 call assert_match('105\s\+0\s\+Z', a)
2370 norm! 3g;
2371 call assert_equal(2, line('.'))
2372 norm! 2g,
2373 call assert_equal(105, line('.'))
2374
2375 " Test for g& - global substitute
2376 %d
2377 call setline(1, range(1,10))
2378 call append('$', ['a', 'b', 'c', 'd'])
2379 $s/\w/&&/g
2380 exe "norm! /[1-8]\<cr>"
2381 norm! g&
2382 call assert_equal(['11', '22', '33', '44', '55', '66', '77', '88', '9', '110', 'a', 'b', 'c', 'dd'], getline(1, '$'))
2383
Bram Moolenaar1671f442020-03-10 07:48:13 +01002384 " Jumping to a fold using gg should open the fold
2385 set foldenable
2386 set foldopen+=jump
2387 5,8fold
2388 call feedkeys('6gg', 'xt')
2389 call assert_equal(1, foldlevel('.'))
2390 call assert_equal(-1, foldclosed('.'))
2391 set foldopen-=jump
2392 set foldenable&
2393
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002394 " Test for gv
2395 %d
2396 call append('$', repeat(['abcdefgh'], 8))
2397 exe "norm! 2gg02l\<c-v>2j2ly"
2398 call assert_equal(['cde', 'cde', 'cde'], getreg(0, 1, 1))
2399 " in visual mode, gv swaps current and last selected region
2400 exe "norm! G0\<c-v>4k4lgvd"
2401 call assert_equal(['', 'abfgh', 'abfgh', 'abfgh', 'abcdefgh', 'abcdefgh', 'abcdefgh', 'abcdefgh', 'abcdefgh'], getline(1,'$'))
2402 exe "norm! G0\<c-v>4k4ly"
2403 exe "norm! gvood"
2404 call assert_equal(['', 'abfgh', 'abfgh', 'abfgh', 'fgh', 'fgh', 'fgh', 'fgh', 'fgh'], getline(1,'$'))
Bram Moolenaarf5f1e102020-03-08 05:13:15 +01002405 " gv cannot be used in operator pending mode
2406 call assert_beeps('normal! cgv')
2407 " gv should beep without a previously selected visual area
2408 new
2409 call assert_beeps('normal! gv')
2410 close
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002411
2412 " Test for gk/gj
2413 %d
2414 15vsp
2415 set wrap listchars= sbr=
Bram Moolenaar74ede802021-05-29 19:18:01 +02002416 let lineA = 'abcdefghijklmnopqrstuvwxyz'
2417 let lineB = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'
2418 let lineC = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz01234567890123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002419 $put =lineA
2420 $put =lineB
2421
2422 norm! 3gg0dgk
2423 call assert_equal(['', 'abcdefghijklmno', '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'], getline(1, '$'))
2424 set nu
2425 norm! 3gg0gjdgj
2426 call assert_equal(['', 'abcdefghijklmno', '0123456789AMNOPQRSTUVWXYZ'], getline(1,'$'))
2427
2428 " Test for gJ
2429 norm! 2gggJ
2430 call assert_equal(['', 'abcdefghijklmno0123456789AMNOPQRSTUVWXYZ'], getline(1,'$'))
2431 call assert_equal(16, col('.'))
2432 " shouldn't do anything
2433 norm! 10gJ
2434 call assert_equal(1, col('.'))
2435
2436 " Test for g0 g^ gm g$
2437 exe "norm! 2gg0gji "
2438 call assert_equal(['', 'abcdefghijk lmno0123456789AMNOPQRSTUVWXYZ'], getline(1,'$'))
2439 norm! g0yl
2440 call assert_equal(12, col('.'))
2441 call assert_equal(' ', getreg(0))
2442 norm! g$yl
2443 call assert_equal(22, col('.'))
2444 call assert_equal('3', getreg(0))
2445 norm! gmyl
2446 call assert_equal(17, col('.'))
2447 call assert_equal('n', getreg(0))
2448 norm! g^yl
2449 call assert_equal(15, col('.'))
2450 call assert_equal('l', getreg(0))
Bram Moolenaarf5f1e102020-03-08 05:13:15 +01002451 call assert_beeps('normal 5g$')
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002452
Bram Moolenaar74ede802021-05-29 19:18:01 +02002453 " Test for g$ with double-width character half displayed
2454 vsplit
2455 9wincmd |
2456 setlocal nowrap nonumber
2457 call setline(2, 'asdfasdfヨ')
2458 2
2459 normal 0g$
2460 call assert_equal(8, col('.'))
2461 10wincmd |
2462 normal 0g$
2463 call assert_equal(9, col('.'))
2464
2465 setlocal signcolumn=yes
2466 11wincmd |
2467 normal 0g$
2468 call assert_equal(8, col('.'))
2469 12wincmd |
2470 normal 0g$
2471 call assert_equal(9, col('.'))
2472
2473 close
2474
Bram Moolenaarf5f1e102020-03-08 05:13:15 +01002475 " Test for g_
2476 call assert_beeps('normal! 100g_')
2477 call setline(2, [' foo ', ' foobar '])
2478 normal! 2ggg_
2479 call assert_equal(5, col('.'))
2480 normal! 2g_
2481 call assert_equal(8, col('.'))
2482
2483 norm! 2ggdG
Bram Moolenaar8b530c12019-10-28 02:13:05 +01002484 $put =lineC
2485
2486 " Test for gM
2487 norm! gMyl
2488 call assert_equal(73, col('.'))
2489 call assert_equal('0', getreg(0))
2490 " Test for 20gM
2491 norm! 20gMyl
2492 call assert_equal(29, col('.'))
2493 call assert_equal('S', getreg(0))
2494 " Test for 60gM
2495 norm! 60gMyl
2496 call assert_equal(87, col('.'))
2497 call assert_equal('E', getreg(0))
2498
2499 " Test for g Ctrl-G
2500 set ff=unix
2501 let a=execute(":norm! g\<c-g>")
2502 call assert_match('Col 87 of 144; Line 2 of 2; Word 1 of 1; Byte 88 of 146', a)
2503
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002504 " Test for gI
2505 norm! gIfoo
Bram Moolenaar8b530c12019-10-28 02:13:05 +01002506 call assert_equal(['', 'foo0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz01234567890123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'], getline(1,'$'))
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002507
2508 " Test for gi
2509 wincmd c
2510 %d
2511 set tw=0
2512 call setline(1, ['foobar', 'new line'])
2513 norm! A next word
2514 $put ='third line'
2515 norm! gi another word
2516 call assert_equal(['foobar next word another word', 'new line', 'third line'], getline(1,'$'))
Bram Moolenaarf5f1e102020-03-08 05:13:15 +01002517 call setline(1, 'foobar')
2518 normal! Ggifirst line
2519 call assert_equal('foobarfirst line', getline(1))
2520 " Test gi in 'virtualedit' mode with cursor after the end of the line
2521 set virtualedit=all
2522 call setline(1, 'foo')
2523 exe "normal! Abar\<Right>\<Right>\<Right>\<Right>"
2524 call setline(1, 'foo')
2525 normal! Ggifirst line
2526 call assert_equal('foo first line', getline(1))
2527 set virtualedit&
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002528
Dominique Pelle923dce22021-11-21 11:36:04 +00002529 " Test for aborting a g command using CTRL-\ CTRL-G
Bram Moolenaar1671f442020-03-10 07:48:13 +01002530 exe "normal! g\<C-\>\<C-G>"
2531 call assert_equal('foo first line', getline('.'))
2532
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002533 " clean up
2534 bw!
Bram Moolenaar2931f2a2016-09-09 16:59:08 +02002535endfunc
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002536
Bram Moolenaarf5f1e102020-03-08 05:13:15 +01002537" Test for g CTRL-G
Bram Moolenaar1bbb6192018-11-10 16:02:01 +01002538func Test_g_ctrl_g()
Bram Moolenaar05295832018-08-24 22:07:58 +02002539 new
2540
2541 let a = execute(":norm! g\<c-g>")
2542 call assert_equal("\n--No lines in buffer--", a)
2543
Bram Moolenaar1671f442020-03-10 07:48:13 +01002544 " Test for CTRL-G (same as :file)
2545 let a = execute(":norm! \<c-g>")
2546 call assert_equal("\n\n\"[No Name]\" --No lines in buffer--", a)
2547
Bram Moolenaar05295832018-08-24 22:07:58 +02002548 call setline(1, ['first line', 'second line'])
2549
2550 " Test g CTRL-g with dos, mac and unix file type.
2551 norm! gojll
2552 set ff=dos
2553 let a = execute(":norm! g\<c-g>")
2554 call assert_equal("\nCol 3 of 11; Line 2 of 2; Word 3 of 4; Byte 15 of 25", a)
2555
2556 set ff=mac
2557 let a = execute(":norm! g\<c-g>")
2558 call assert_equal("\nCol 3 of 11; Line 2 of 2; Word 3 of 4; Byte 14 of 23", a)
2559
2560 set ff=unix
2561 let a = execute(":norm! g\<c-g>")
2562 call assert_equal("\nCol 3 of 11; Line 2 of 2; Word 3 of 4; Byte 14 of 23", a)
2563
2564 " Test g CTRL-g in visual mode (v)
2565 let a = execute(":norm! gojllvlg\<c-g>")
2566 call assert_equal("\nSelected 1 of 2 Lines; 1 of 4 Words; 2 of 23 Bytes", a)
2567
2568 " Test g CTRL-g in visual mode (CTRL-V) with end col > start col
2569 let a = execute(":norm! \<Esc>gojll\<C-V>kllg\<c-g>")
2570 call assert_equal("\nSelected 3 Cols; 2 of 2 Lines; 2 of 4 Words; 6 of 23 Bytes", a)
2571
2572 " Test g_CTRL-g in visual mode (CTRL-V) with end col < start col
2573 let a = execute(":norm! \<Esc>goll\<C-V>jhhg\<c-g>")
2574 call assert_equal("\nSelected 3 Cols; 2 of 2 Lines; 2 of 4 Words; 6 of 23 Bytes", a)
2575
2576 " Test g CTRL-g in visual mode (CTRL-V) with end_vcol being MAXCOL
2577 let a = execute(":norm! \<Esc>gojll\<C-V>k$g\<c-g>")
2578 call assert_equal("\nSelected 2 of 2 Lines; 4 of 4 Words; 17 of 23 Bytes", a)
2579
2580 " There should be one byte less with noeol
2581 set bin noeol
2582 let a = execute(":norm! \<Esc>gog\<c-g>")
2583 call assert_equal("\nCol 1 of 10; Line 1 of 2; Word 1 of 4; Char 1 of 23; Byte 1 of 22", a)
2584 set bin & eol&
2585
Bram Moolenaar30276f22019-01-24 17:59:39 +01002586 call setline(1, ['Français', '日本語'])
Bram Moolenaar05295832018-08-24 22:07:58 +02002587
Bram Moolenaar30276f22019-01-24 17:59:39 +01002588 let a = execute(":norm! \<Esc>gojlg\<c-g>")
2589 call assert_equal("\nCol 4-3 of 9-6; Line 2 of 2; Word 2 of 2; Char 11 of 13; Byte 16 of 20", a)
Bram Moolenaar05295832018-08-24 22:07:58 +02002590
Bram Moolenaar30276f22019-01-24 17:59:39 +01002591 let a = execute(":norm! \<Esc>gojvlg\<c-g>")
2592 call assert_equal("\nSelected 1 of 2 Lines; 1 of 2 Words; 2 of 13 Chars; 6 of 20 Bytes", a)
Bram Moolenaar05295832018-08-24 22:07:58 +02002593
Bram Moolenaar30276f22019-01-24 17:59:39 +01002594 let a = execute(":norm! \<Esc>goll\<c-v>jlg\<c-g>")
2595 call assert_equal("\nSelected 4 Cols; 2 of 2 Lines; 2 of 2 Words; 6 of 13 Chars; 11 of 20 Bytes", a)
Bram Moolenaar05295832018-08-24 22:07:58 +02002596
Bram Moolenaar30276f22019-01-24 17:59:39 +01002597 set fenc=utf8 bomb
2598 let a = execute(":norm! \<Esc>gojlg\<c-g>")
2599 call assert_equal("\nCol 4-3 of 9-6; Line 2 of 2; Word 2 of 2; Char 11 of 13; Byte 16 of 20(+3 for BOM)", a)
Bram Moolenaar05295832018-08-24 22:07:58 +02002600
Bram Moolenaar30276f22019-01-24 17:59:39 +01002601 set fenc=utf16 bomb
2602 let a = execute(":norm! g\<c-g>")
2603 call assert_equal("\nCol 4-3 of 9-6; Line 2 of 2; Word 2 of 2; Char 11 of 13; Byte 16 of 20(+2 for BOM)", a)
Bram Moolenaar05295832018-08-24 22:07:58 +02002604
Bram Moolenaar30276f22019-01-24 17:59:39 +01002605 set fenc=utf32 bomb
2606 let a = execute(":norm! g\<c-g>")
2607 call assert_equal("\nCol 4-3 of 9-6; Line 2 of 2; Word 2 of 2; Char 11 of 13; Byte 16 of 20(+4 for BOM)", a)
Bram Moolenaar05295832018-08-24 22:07:58 +02002608
Bram Moolenaar30276f22019-01-24 17:59:39 +01002609 set fenc& bomb&
Bram Moolenaar05295832018-08-24 22:07:58 +02002610
2611 set ff&
2612 bwipe!
2613endfunc
2614
Bram Moolenaarf5f1e102020-03-08 05:13:15 +01002615" Test for g8
Bram Moolenaar1671f442020-03-10 07:48:13 +01002616func Test_normal34_g_cmd3()
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002617 new
Bram Moolenaar395b6ba2017-04-07 20:09:51 +02002618 let a=execute(':norm! 1G0g8')
2619 call assert_equal("\nNUL", a)
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002620
Bram Moolenaar395b6ba2017-04-07 20:09:51 +02002621 call setline(1, 'abcdefghijklmnopqrstuvwxyzäüö')
2622 let a=execute(':norm! 1G$g8')
2623 call assert_equal("\nc3 b6 ", a)
2624
2625 call setline(1, "a\u0302")
2626 let a=execute(':norm! 1G0g8')
2627 call assert_equal("\n61 + cc 82 ", a)
2628
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002629 " clean up
2630 bw!
Bram Moolenaar2931f2a2016-09-09 16:59:08 +02002631endfunc
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002632
Bram Moolenaarf5f1e102020-03-08 05:13:15 +01002633" Test 8g8 which finds invalid utf8 at or after the cursor.
Bram Moolenaar395b6ba2017-04-07 20:09:51 +02002634func Test_normal_8g8()
Bram Moolenaar395b6ba2017-04-07 20:09:51 +02002635 new
2636
Bram Moolenaar395b6ba2017-04-07 20:09:51 +02002637 " With invalid byte.
2638 call setline(1, "___\xff___")
2639 norm! 1G08g8g
2640 call assert_equal([0, 1, 4, 0, 1], getcurpos())
2641
2642 " With invalid byte before the cursor.
2643 call setline(1, "___\xff___")
2644 norm! 1G$h8g8g
2645 call assert_equal([0, 1, 6, 0, 9], getcurpos())
2646
2647 " With truncated sequence.
2648 call setline(1, "___\xE2\x82___")
2649 norm! 1G08g8g
2650 call assert_equal([0, 1, 4, 0, 1], getcurpos())
2651
2652 " With overlong sequence.
2653 call setline(1, "___\xF0\x82\x82\xAC___")
2654 norm! 1G08g8g
2655 call assert_equal([0, 1, 4, 0, 1], getcurpos())
2656
2657 " With valid utf8.
2658 call setline(1, "café")
2659 norm! 1G08g8
2660 call assert_equal([0, 1, 1, 0, 1], getcurpos())
2661
2662 bw!
2663endfunc
2664
Bram Moolenaarf5f1e102020-03-08 05:13:15 +01002665" Test for g<
Bram Moolenaar1671f442020-03-10 07:48:13 +01002666func Test_normal35_g_cmd4()
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002667 " Cannot capture its output,
2668 " probably a bug, therefore, test disabled:
Bram Moolenaar31845092016-09-05 22:58:31 +02002669 throw "Skipped: output of g< can't be tested currently"
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002670 echo "a\nb\nc\nd"
2671 let b=execute(':norm! g<')
2672 call assert_true(!empty(b), 'failed `execute(g<)`')
Bram Moolenaar2931f2a2016-09-09 16:59:08 +02002673endfunc
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002674
Bram Moolenaarf5f1e102020-03-08 05:13:15 +01002675" Test for gp gP go
Bram Moolenaar1671f442020-03-10 07:48:13 +01002676func Test_normal36_g_cmd5()
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002677 new
2678 call append(0, 'abcdefghijklmnopqrstuvwxyz')
Bram Moolenaar0913a102016-09-03 19:11:59 +02002679 set ff=unix
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002680 " Test for gp gP
2681 call append(1, range(1,10))
2682 1
2683 norm! 1yy
2684 3
2685 norm! gp
2686 call assert_equal([0, 5, 1, 0, 1], getcurpos())
2687 $
2688 norm! gP
2689 call assert_equal([0, 14, 1, 0, 1], getcurpos())
2690
2691 " Test for go
2692 norm! 26go
2693 call assert_equal([0, 1, 26, 0, 26], getcurpos())
2694 norm! 27go
2695 call assert_equal([0, 1, 26, 0, 26], getcurpos())
2696 norm! 28go
2697 call assert_equal([0, 2, 1, 0, 1], getcurpos())
2698 set ff=dos
2699 norm! 29go
2700 call assert_equal([0, 2, 1, 0, 1], getcurpos())
2701 set ff=unix
2702 norm! gg0
2703 norm! 101go
2704 call assert_equal([0, 13, 26, 0, 26], getcurpos())
2705 norm! 103go
2706 call assert_equal([0, 14, 1, 0, 1], getcurpos())
2707 " count > buffer content
2708 norm! 120go
2709 call assert_equal([0, 14, 1, 0, 2147483647], getcurpos())
2710 " clean up
2711 bw!
Bram Moolenaar2931f2a2016-09-09 16:59:08 +02002712endfunc
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002713
Bram Moolenaarf5f1e102020-03-08 05:13:15 +01002714" Test for gt and gT
Bram Moolenaar1671f442020-03-10 07:48:13 +01002715func Test_normal37_g_cmd6()
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002716 tabnew 1.txt
2717 tabnew 2.txt
2718 tabnew 3.txt
2719 norm! 1gt
2720 call assert_equal(1, tabpagenr())
2721 norm! 3gt
2722 call assert_equal(3, tabpagenr())
2723 norm! 1gT
2724 " count gT goes not to the absolute tabpagenumber
2725 " but, but goes to the count previous tabpagenumber
2726 call assert_equal(2, tabpagenr())
2727 " wrap around
2728 norm! 3gT
2729 call assert_equal(3, tabpagenr())
2730 " gt does not wrap around
2731 norm! 5gt
2732 call assert_equal(3, tabpagenr())
2733
2734 for i in range(3)
2735 tabclose
2736 endfor
2737 " clean up
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +01002738 call assert_fails(':tabclose', 'E784:')
Bram Moolenaar2931f2a2016-09-09 16:59:08 +02002739endfunc
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002740
Bram Moolenaarf5f1e102020-03-08 05:13:15 +01002741" Test for <Home> and <C-Home> key
Bram Moolenaar1671f442020-03-10 07:48:13 +01002742func Test_normal38_nvhome()
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002743 new
2744 call setline(1, range(10))
2745 $
2746 setl et sw=2
2747 norm! V10>$
2748 " count is ignored
2749 exe "norm! 10\<home>"
2750 call assert_equal(1, col('.'))
2751 exe "norm! \<home>"
2752 call assert_equal([0, 10, 1, 0, 1], getcurpos())
2753 exe "norm! 5\<c-home>"
2754 call assert_equal([0, 5, 1, 0, 1], getcurpos())
2755 exe "norm! \<c-home>"
2756 call assert_equal([0, 1, 1, 0, 1], getcurpos())
Bram Moolenaarf5f1e102020-03-08 05:13:15 +01002757 exe "norm! G\<c-kHome>"
2758 call assert_equal([0, 1, 1, 0, 1], getcurpos())
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002759
2760 " clean up
2761 bw!
Bram Moolenaar2931f2a2016-09-09 16:59:08 +02002762endfunc
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002763
Bram Moolenaar1671f442020-03-10 07:48:13 +01002764" Test for <End> and <C-End> keys
2765func Test_normal_nvend()
2766 new
2767 call setline(1, map(range(1, 10), '"line" .. v:val'))
2768 exe "normal! \<End>"
2769 call assert_equal(5, col('.'))
2770 exe "normal! 4\<End>"
2771 call assert_equal([4, 5], [line('.'), col('.')])
2772 exe "normal! \<C-End>"
2773 call assert_equal([10, 6], [line('.'), col('.')])
2774 close!
2775endfunc
2776
Bram Moolenaarf5f1e102020-03-08 05:13:15 +01002777" Test for cw cW ce
Bram Moolenaar1671f442020-03-10 07:48:13 +01002778func Test_normal39_cw()
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002779 " Test for cw and cW on whitespace
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002780 new
2781 set tw=0
2782 call append(0, 'here are some words')
2783 norm! 1gg0elcwZZZ
2784 call assert_equal('hereZZZare some words', getline('.'))
2785 norm! 1gg0elcWYYY
2786 call assert_equal('hereZZZareYYYsome words', getline('.'))
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002787 norm! 2gg0cwfoo
2788 call assert_equal('foo', getline('.'))
2789
Bram Moolenaarf5f1e102020-03-08 05:13:15 +01002790 call setline(1, 'one; two')
2791 call cursor(1, 1)
2792 call feedkeys('cwvim', 'xt')
2793 call assert_equal('vim; two', getline(1))
2794 call feedkeys('0cWone', 'xt')
2795 call assert_equal('one two', getline(1))
2796 "When cursor is at the end of a word 'ce' will change until the end of the
2797 "next word, but 'cw' will change only one character
2798 call setline(1, 'one two')
2799 call feedkeys('0ecwce', 'xt')
2800 call assert_equal('once two', getline(1))
2801 call setline(1, 'one two')
2802 call feedkeys('0ecely', 'xt')
2803 call assert_equal('only', getline(1))
2804
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002805 " clean up
2806 bw!
Bram Moolenaar2931f2a2016-09-09 16:59:08 +02002807endfunc
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002808
Bram Moolenaarf5f1e102020-03-08 05:13:15 +01002809" Test for CTRL-\ commands
Bram Moolenaar1671f442020-03-10 07:48:13 +01002810func Test_normal40_ctrl_bsl()
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002811 new
2812 call append(0, 'here are some words')
2813 exe "norm! 1gg0a\<C-\>\<C-N>"
2814 call assert_equal('n', mode())
2815 call assert_equal(1, col('.'))
2816 call assert_equal('', visualmode())
2817 exe "norm! 1gg0viw\<C-\>\<C-N>"
2818 call assert_equal('n', mode())
2819 call assert_equal(4, col('.'))
2820 exe "norm! 1gg0a\<C-\>\<C-G>"
2821 call assert_equal('n', mode())
2822 call assert_equal(1, col('.'))
2823 "imap <buffer> , <c-\><c-n>
2824 set im
2825 exe ":norm! \<c-\>\<c-n>dw"
2826 set noim
2827 call assert_equal('are some words', getline(1))
2828 call assert_false(&insertmode)
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002829 call assert_beeps("normal! \<C-\>\<C-A>")
Bram Moolenaar1671f442020-03-10 07:48:13 +01002830
Bram Moolenaar21829c52021-01-26 22:42:21 +01002831 if has('cmdwin')
2832 " Using CTRL-\ CTRL-N in cmd window should close the window
2833 call feedkeys("q:\<C-\>\<C-N>", 'xt')
2834 call assert_equal('', getcmdwintype())
2835 endif
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002836
2837 " clean up
2838 bw!
Bram Moolenaar2931f2a2016-09-09 16:59:08 +02002839endfunc
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002840
Bram Moolenaarf5f1e102020-03-08 05:13:15 +01002841" Test for <c-r>=, <c-r><c-r>= and <c-r><c-o>= in insert mode
Bram Moolenaar1671f442020-03-10 07:48:13 +01002842func Test_normal41_insert_reg()
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002843 new
2844 set sts=2 sw=2 ts=8 tw=0
2845 call append(0, ["aaa\tbbb\tccc", '', '', ''])
2846 let a=getline(1)
2847 norm! 2gg0
2848 exe "norm! a\<c-r>=a\<cr>"
2849 norm! 3gg0
2850 exe "norm! a\<c-r>\<c-r>=a\<cr>"
2851 norm! 4gg0
2852 exe "norm! a\<c-r>\<c-o>=a\<cr>"
2853 call assert_equal(['aaa bbb ccc', 'aaa bbb ccc', 'aaa bbb ccc', 'aaa bbb ccc', ''], getline(1, '$'))
2854
2855 " clean up
2856 set sts=0 sw=8 ts=8
Bram Moolenaar31845092016-09-05 22:58:31 +02002857 bw!
Bram Moolenaar2931f2a2016-09-09 16:59:08 +02002858endfunc
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002859
Bram Moolenaarf5f1e102020-03-08 05:13:15 +01002860" Test for Ctrl-D and Ctrl-U
Bram Moolenaar1bbb6192018-11-10 16:02:01 +01002861func Test_normal42_halfpage()
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002862 call Setup_NewWindow()
2863 call assert_equal(5, &scroll)
2864 exe "norm! \<c-d>"
2865 call assert_equal('6', getline('.'))
2866 exe "norm! 2\<c-d>"
2867 call assert_equal('8', getline('.'))
2868 call assert_equal(2, &scroll)
2869 set scroll=5
2870 exe "norm! \<c-u>"
2871 call assert_equal('3', getline('.'))
2872 1
2873 set scrolloff=5
2874 exe "norm! \<c-d>"
2875 call assert_equal('10', getline('.'))
2876 exe "norm! \<c-u>"
2877 call assert_equal('5', getline('.'))
2878 1
2879 set scrolloff=99
2880 exe "norm! \<c-d>"
2881 call assert_equal('10', getline('.'))
2882 set scrolloff=0
2883 100
2884 exe "norm! $\<c-u>"
2885 call assert_equal('95', getline('.'))
2886 call assert_equal([0, 95, 1, 0, 1], getcurpos())
2887 100
2888 set nostartofline
2889 exe "norm! $\<c-u>"
2890 call assert_equal('95', getline('.'))
2891 call assert_equal([0, 95, 2, 0, 2147483647], getcurpos())
2892 " cleanup
2893 set startofline
2894 bw!
Bram Moolenaar2931f2a2016-09-09 16:59:08 +02002895endfunc
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002896
Bram Moolenaar1bbb6192018-11-10 16:02:01 +01002897func Test_normal45_drop()
Bram Moolenaar29495952018-02-12 22:49:00 +01002898 if !has('dnd')
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01002899 " The ~ register does not exist
2900 call assert_beeps('norm! "~')
Bram Moolenaar29495952018-02-12 22:49:00 +01002901 return
2902 endif
2903
2904 " basic test for drag-n-drop
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002905 " unfortunately, without a gui, we can't really test much here,
2906 " so simply test that ~p fails (which uses the drop register)
2907 new
Bram Moolenaare2e40752020-09-04 21:18:46 +02002908 call assert_fails(':norm! "~p', 'E353:')
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002909 call assert_equal([], getreg('~', 1, 1))
2910 " the ~ register is read only
Bram Moolenaare2e40752020-09-04 21:18:46 +02002911 call assert_fails(':let @~="1"', 'E354:')
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002912 bw!
Bram Moolenaar2931f2a2016-09-09 16:59:08 +02002913endfunc
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002914
Bram Moolenaar1bbb6192018-11-10 16:02:01 +01002915func Test_normal46_ignore()
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002916 new
2917 " How to test this?
2918 " let's just for now test, that the buffer
2919 " does not change
2920 call feedkeys("\<c-s>", 't')
2921 call assert_equal([''], getline(1,'$'))
2922
Bram Moolenaar2931f2a2016-09-09 16:59:08 +02002923 " no valid commands
2924 exe "norm! \<char-0x100>"
2925 call assert_equal([''], getline(1,'$'))
2926
2927 exe "norm! ä"
2928 call assert_equal([''], getline(1,'$'))
2929
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002930 " clean up
2931 bw!
Bram Moolenaar2931f2a2016-09-09 16:59:08 +02002932endfunc
Bram Moolenaarc4a908e2016-09-08 23:35:30 +02002933
Bram Moolenaar1bbb6192018-11-10 16:02:01 +01002934func Test_normal47_visual_buf_wipe()
Bram Moolenaarc4a908e2016-09-08 23:35:30 +02002935 " This was causing a crash or ml_get error.
2936 enew!
2937 call setline(1,'xxx')
2938 normal $
2939 new
2940 call setline(1, range(1,2))
2941 2
2942 exe "norm \<C-V>$"
2943 bw!
2944 norm yp
2945 set nomodified
Bram Moolenaar2931f2a2016-09-09 16:59:08 +02002946endfunc
2947
Bram Moolenaar1bbb6192018-11-10 16:02:01 +01002948func Test_normal48_wincmd()
Bram Moolenaar2931f2a2016-09-09 16:59:08 +02002949 new
2950 exe "norm! \<c-w>c"
2951 call assert_equal(1, winnr('$'))
Bram Moolenaare2e40752020-09-04 21:18:46 +02002952 call assert_fails(":norm! \<c-w>c", 'E444:')
Bram Moolenaar2931f2a2016-09-09 16:59:08 +02002953endfunc
2954
Bram Moolenaar1bbb6192018-11-10 16:02:01 +01002955func Test_normal49_counts()
Bram Moolenaar2931f2a2016-09-09 16:59:08 +02002956 new
2957 call setline(1, 'one two three four five six seven eight nine ten')
2958 1
2959 norm! 3d2w
2960 call assert_equal('seven eight nine ten', getline(1))
2961 bw!
2962endfunc
2963
Bram Moolenaar1bbb6192018-11-10 16:02:01 +01002964func Test_normal50_commandline()
Bram Moolenaar004a6782020-04-11 17:09:31 +02002965 CheckFeature timers
2966 CheckFeature cmdline_hist
Bram Moolenaar2931f2a2016-09-09 16:59:08 +02002967 func! DoTimerWork(id)
2968 call assert_equal('[Command Line]', bufname(''))
2969 " should fail, with E11, but does fail with E23?
2970 "call feedkeys("\<c-^>", 'tm')
2971
2972 " should also fail with E11
Bram Moolenaare2e40752020-09-04 21:18:46 +02002973 call assert_fails(":wincmd p", 'E11:')
Bram Moolenaar2931f2a2016-09-09 16:59:08 +02002974 " return from commandline window
2975 call feedkeys("\<cr>")
2976 endfunc
2977
2978 let oldlang=v:lang
2979 lang C
2980 set updatetime=20
2981 call timer_start(100, 'DoTimerWork')
2982 try
2983 " throws E23, for whatever reason...
2984 call feedkeys('q:', 'x!')
2985 catch /E23/
2986 " no-op
2987 endtry
2988 " clean up
2989 set updatetime=4000
2990 exe "lang" oldlang
2991 bw!
2992endfunc
2993
Bram Moolenaar1bbb6192018-11-10 16:02:01 +01002994func Test_normal51_FileChangedRO()
Bram Moolenaar004a6782020-04-11 17:09:31 +02002995 CheckFeature autocmd
Bram Moolenaare5f2a072017-02-01 22:31:49 +01002996 " Don't sleep after the warning message.
2997 call test_settime(1)
Bram Moolenaar2931f2a2016-09-09 16:59:08 +02002998 call writefile(['foo'], 'Xreadonly.log')
2999 new Xreadonly.log
3000 setl ro
3001 au FileChangedRO <buffer> :call feedkeys("\<c-^>", 'tix')
Bram Moolenaare2e40752020-09-04 21:18:46 +02003002 call assert_fails(":norm! Af", 'E788:')
Bram Moolenaar2931f2a2016-09-09 16:59:08 +02003003 call assert_equal(['foo'], getline(1,'$'))
3004 call assert_equal('Xreadonly.log', bufname(''))
3005
3006 " cleanup
Bram Moolenaare5f2a072017-02-01 22:31:49 +01003007 call test_settime(0)
Bram Moolenaar2931f2a2016-09-09 16:59:08 +02003008 bw!
3009 call delete("Xreadonly.log")
3010endfunc
3011
Bram Moolenaar1bbb6192018-11-10 16:02:01 +01003012func Test_normal52_rl()
Bram Moolenaar004a6782020-04-11 17:09:31 +02003013 CheckFeature rightleft
Bram Moolenaar2931f2a2016-09-09 16:59:08 +02003014 new
3015 call setline(1, 'abcde fghij klmnopq')
3016 norm! 1gg$
3017 set rl
3018 call assert_equal(19, col('.'))
3019 call feedkeys('l', 'tx')
3020 call assert_equal(18, col('.'))
3021 call feedkeys('h', 'tx')
3022 call assert_equal(19, col('.'))
3023 call feedkeys("\<right>", 'tx')
3024 call assert_equal(18, col('.'))
Bram Moolenaar1671f442020-03-10 07:48:13 +01003025 call feedkeys("\<left>", 'tx')
3026 call assert_equal(19, col('.'))
Bram Moolenaar2931f2a2016-09-09 16:59:08 +02003027 call feedkeys("\<s-right>", 'tx')
3028 call assert_equal(13, col('.'))
3029 call feedkeys("\<c-right>", 'tx')
3030 call assert_equal(7, col('.'))
3031 call feedkeys("\<c-left>", 'tx')
3032 call assert_equal(13, col('.'))
3033 call feedkeys("\<s-left>", 'tx')
3034 call assert_equal(19, col('.'))
3035 call feedkeys("<<", 'tx')
3036 call assert_equal(' abcde fghij klmnopq',getline(1))
3037 call feedkeys(">>", 'tx')
3038 call assert_equal('abcde fghij klmnopq',getline(1))
3039
3040 " cleanup
3041 set norl
3042 bw!
3043endfunc
3044
Bram Moolenaarb1e04fc2017-03-29 13:08:35 +02003045func Test_normal54_Ctrl_bsl()
3046 new
3047 call setline(1, 'abcdefghijklmn')
3048 exe "norm! df\<c-\>\<c-n>"
3049 call assert_equal(['abcdefghijklmn'], getline(1,'$'))
3050 exe "norm! df\<c-\>\<c-g>"
3051 call assert_equal(['abcdefghijklmn'], getline(1,'$'))
3052 exe "norm! df\<c-\>m"
3053 call assert_equal(['abcdefghijklmn'], getline(1,'$'))
Bram Moolenaar30276f22019-01-24 17:59:39 +01003054
Bram Moolenaarb1e04fc2017-03-29 13:08:35 +02003055 call setline(2, 'abcdefghijklmnāf')
3056 norm! 2gg0
3057 exe "norm! df\<Char-0x101>"
3058 call assert_equal(['abcdefghijklmn', 'f'], getline(1,'$'))
3059 norm! 1gg0
3060 exe "norm! df\<esc>"
3061 call assert_equal(['abcdefghijklmn', 'f'], getline(1,'$'))
Bram Moolenaar2931f2a2016-09-09 16:59:08 +02003062
Bram Moolenaarb1e04fc2017-03-29 13:08:35 +02003063 " clean up
3064 bw!
3065endfunc
3066
3067func Test_normal_large_count()
3068 " This may fail with 32bit long, how do we detect that?
3069 new
3070 normal o
3071 normal 6666666666dL
3072 bwipe!
Bram Moolenaar2931f2a2016-09-09 16:59:08 +02003073endfunc
Bram Moolenaarbf3d5802017-03-29 19:48:11 +02003074
3075func Test_delete_until_paragraph()
Bram Moolenaarbf3d5802017-03-29 19:48:11 +02003076 new
3077 normal grádv}
3078 call assert_equal('á', getline(1))
3079 normal grád}
3080 call assert_equal('', getline(1))
3081 bwipe!
3082endfunc
Bram Moolenaarfb094e12017-11-05 20:59:28 +01003083
3084" Test for the gr (virtual replace) command
3085" Test for the bug fixed by 7.4.387
3086func Test_gr_command()
3087 enew!
3088 let save_cpo = &cpo
3089 call append(0, ['First line', 'Second line', 'Third line'])
3090 exe "normal i\<C-G>u"
3091 call cursor(2, 1)
3092 set cpo-=X
3093 normal 4gro
3094 call assert_equal('oooond line', getline(2))
3095 undo
3096 set cpo+=X
3097 normal 4gro
3098 call assert_equal('ooooecond line', getline(2))
3099 let &cpo = save_cpo
Bram Moolenaarf5f1e102020-03-08 05:13:15 +01003100 normal! ggvegrx
3101 call assert_equal('xxxxx line', getline(1))
3102 exe "normal! gggr\<C-V>122"
3103 call assert_equal('zxxxx line', getline(1))
3104 set virtualedit=all
3105 normal! 15|grl
3106 call assert_equal('zxxxx line l', getline(1))
3107 set virtualedit&
3108 set nomodifiable
3109 call assert_fails('normal! grx', 'E21:')
3110 call assert_fails('normal! gRx', 'E21:')
3111 set modifiable&
Bram Moolenaarfb094e12017-11-05 20:59:28 +01003112 enew!
3113endfunc
3114
3115" When splitting a window the changelist position is wrong.
3116" Test the changelist position after splitting a window.
3117" Test for the bug fixed by 7.4.386
3118func Test_changelist()
3119 let save_ul = &ul
3120 enew!
3121 call append('$', ['1', '2'])
3122 exe "normal i\<C-G>u"
3123 exe "normal Gkylpa\<C-G>u"
3124 set ul=100
3125 exe "normal Gylpa\<C-G>u"
3126 set ul=100
3127 normal gg
3128 vsplit
3129 normal g;
3130 call assert_equal([3, 2], [line('.'), col('.')])
3131 normal g;
3132 call assert_equal([2, 2], [line('.'), col('.')])
3133 call assert_fails('normal g;', 'E662:')
Bram Moolenaar1671f442020-03-10 07:48:13 +01003134 new
3135 call assert_fails('normal g;', 'E664:')
Bram Moolenaarfb094e12017-11-05 20:59:28 +01003136 %bwipe!
3137 let &ul = save_ul
3138endfunc
Bram Moolenaar1bbb6192018-11-10 16:02:01 +01003139
3140func Test_nv_hat_count()
3141 %bwipeout!
3142 let l:nr = bufnr('%') + 1
Bram Moolenaare2e40752020-09-04 21:18:46 +02003143 call assert_fails(':execute "normal! ' . l:nr . '\<C-^>"', 'E92:')
Bram Moolenaar1bbb6192018-11-10 16:02:01 +01003144
3145 edit Xfoo
3146 let l:foo_nr = bufnr('Xfoo')
3147
3148 edit Xbar
3149 let l:bar_nr = bufnr('Xbar')
3150
3151 " Make sure we are not just using the alternate file.
3152 edit Xbaz
3153
3154 call feedkeys(l:foo_nr . "\<C-^>", 'tx')
3155 call assert_equal('Xfoo', fnamemodify(bufname('%'), ':t'))
3156
3157 call feedkeys(l:bar_nr . "\<C-^>", 'tx')
3158 call assert_equal('Xbar', fnamemodify(bufname('%'), ':t'))
3159
3160 %bwipeout!
3161endfunc
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01003162
3163func Test_message_when_using_ctrl_c()
Bram Moolenaar553e5a52019-03-25 23:16:34 +01003164 " Make sure no buffers are changed.
3165 %bwipe!
3166
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01003167 exe "normal \<C-C>"
3168 call assert_match("Type :qa and press <Enter> to exit Vim", Screenline(&lines))
Bram Moolenaar553e5a52019-03-25 23:16:34 +01003169
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01003170 new
3171 cal setline(1, 'hi!')
3172 exe "normal \<C-C>"
3173 call assert_match("Type :qa! and press <Enter> to abandon all changes and exit Vim", Screenline(&lines))
Bram Moolenaar553e5a52019-03-25 23:16:34 +01003174
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01003175 bwipe!
3176endfunc
Bram Moolenaarc6b37db2019-04-27 18:00:34 +02003177
3178" Test for '[m', ']m', '[M' and ']M'
3179" Jumping to beginning and end of methods in Java-like languages
3180func Test_java_motion()
3181 new
Bram Moolenaar1671f442020-03-10 07:48:13 +01003182 call assert_beeps('normal! [m')
3183 call assert_beeps('normal! ]m')
3184 call assert_beeps('normal! [M')
3185 call assert_beeps('normal! ]M')
Bram Moolenaar8a9bc952020-10-02 18:48:07 +02003186 let lines =<< trim [CODE]
3187 Piece of Java
3188 {
3189 tt m1 {
3190 t1;
3191 } e1
Bram Moolenaarc6b37db2019-04-27 18:00:34 +02003192
Bram Moolenaar8a9bc952020-10-02 18:48:07 +02003193 tt m2 {
3194 t2;
3195 } e2
Bram Moolenaarc6b37db2019-04-27 18:00:34 +02003196
Bram Moolenaar8a9bc952020-10-02 18:48:07 +02003197 tt m3 {
3198 if (x)
3199 {
3200 t3;
3201 }
3202 } e3
3203 }
3204 [CODE]
3205 call setline(1, lines)
Bram Moolenaarc6b37db2019-04-27 18:00:34 +02003206
3207 normal gg
3208
3209 normal 2]maA
3210 call assert_equal("\ttt m1 {A", getline('.'))
3211 call assert_equal([3, 9, 16], [line('.'), col('.'), virtcol('.')])
3212
3213 normal j]maB
3214 call assert_equal("\ttt m2 {B", getline('.'))
3215 call assert_equal([7, 9, 16], [line('.'), col('.'), virtcol('.')])
3216
3217 normal ]maC
3218 call assert_equal("\ttt m3 {C", getline('.'))
3219 call assert_equal([11, 9, 16], [line('.'), col('.'), virtcol('.')])
3220
3221 normal [maD
3222 call assert_equal("\ttt m3 {DC", getline('.'))
3223 call assert_equal([11, 9, 16], [line('.'), col('.'), virtcol('.')])
3224
3225 normal k2[maE
3226 call assert_equal("\ttt m1 {EA", getline('.'))
3227 call assert_equal([3, 9, 16], [line('.'), col('.'), virtcol('.')])
3228
3229 normal 3[maF
3230 call assert_equal("{F", getline('.'))
3231 call assert_equal([2, 2, 2], [line('.'), col('.'), virtcol('.')])
3232
3233 normal ]MaG
3234 call assert_equal("\t}G e1", getline('.'))
3235 call assert_equal([5, 3, 10], [line('.'), col('.'), virtcol('.')])
3236
3237 normal j2]MaH
3238 call assert_equal("\t}H e3", getline('.'))
3239 call assert_equal([16, 3, 10], [line('.'), col('.'), virtcol('.')])
3240
3241 normal ]M]M
3242 normal aI
3243 call assert_equal("}I", getline('.'))
3244 call assert_equal([17, 2, 2], [line('.'), col('.'), virtcol('.')])
3245
3246 normal 2[MaJ
3247 call assert_equal("\t}JH e3", getline('.'))
3248 call assert_equal([16, 3, 10], [line('.'), col('.'), virtcol('.')])
3249
3250 normal k[MaK
3251 call assert_equal("\t}K e2", getline('.'))
3252 call assert_equal([9, 3, 10], [line('.'), col('.'), virtcol('.')])
3253
3254 normal 3[MaL
3255 call assert_equal("{LF", getline('.'))
3256 call assert_equal([2, 2, 2], [line('.'), col('.'), virtcol('.')])
3257
Bram Moolenaar8a9bc952020-10-02 18:48:07 +02003258 call cursor(2, 1)
3259 call assert_beeps('norm! 5]m')
3260
3261 " jumping to a method in a fold should open the fold
3262 6,10fold
3263 call feedkeys("gg3]m", 'xt')
3264 call assert_equal([7, 8, 15], [line('.'), col('.'), virtcol('.')])
3265 call assert_equal(-1, foldclosedend(7))
3266
Bram Moolenaarc6b37db2019-04-27 18:00:34 +02003267 close!
3268endfunc
Bram Moolenaard5c82342019-07-27 18:44:57 +02003269
Bram Moolenaar004a6782020-04-11 17:09:31 +02003270" Tests for g cmds
Bram Moolenaar1671f442020-03-10 07:48:13 +01003271func Test_normal_gdollar_cmd()
Bram Moolenaard5c82342019-07-27 18:44:57 +02003272 call Setup_NewWindow()
3273 " Make long lines that will wrap
3274 %s/$/\=repeat(' foobar', 10)/
3275 20vsp
3276 set wrap
3277 " Test for g$ with count
3278 norm! gg
3279 norm! 0vg$y
3280 call assert_equal(20, col("'>"))
3281 call assert_equal('1 foobar foobar foob', getreg(0))
3282 norm! gg
3283 norm! 0v4g$y
3284 call assert_equal(72, col("'>"))
3285 call assert_equal('1 foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar'.."\n", getreg(0))
3286 norm! gg
3287 norm! 0v6g$y
3288 call assert_equal(40, col("'>"))
3289 call assert_equal('1 foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar'.. "\n"..
3290 \ '2 foobar foobar foobar foobar foobar foo', getreg(0))
3291 set nowrap
3292 " clean up
3293 norm! gg
3294 norm! 0vg$y
3295 call assert_equal(20, col("'>"))
3296 call assert_equal('1 foobar foobar foob', getreg(0))
3297 norm! gg
3298 norm! 0v4g$y
3299 call assert_equal(20, col("'>"))
3300 call assert_equal('1 foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar'.. "\n"..
3301 \ '2 foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar'.. "\n"..
3302 \ '3 foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar'.. "\n"..
3303 \ '4 foobar foobar foob', getreg(0))
3304 norm! gg
3305 norm! 0v6g$y
3306 call assert_equal(20, col("'>"))
3307 call assert_equal('1 foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar'.. "\n"..
3308 \ '2 foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar'.. "\n"..
3309 \ '3 foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar'.. "\n"..
3310 \ '4 foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar'.. "\n"..
3311 \ '5 foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar'.. "\n"..
3312 \ '6 foobar foobar foob', getreg(0))
3313 " Move to last line, also down movement is not possible, should still move
3314 " the cursor to the last visible char
3315 norm! G
3316 norm! 0v6g$y
3317 call assert_equal(20, col("'>"))
3318 call assert_equal('100 foobar foobar fo', getreg(0))
3319 bw!
3320endfunc
Bram Moolenaar03ac52f2019-09-24 22:47:46 +02003321
Bram Moolenaarf5f1e102020-03-08 05:13:15 +01003322func Test_normal_gk_gj()
Bram Moolenaar03ac52f2019-09-24 22:47:46 +02003323 " needs 80 column new window
3324 new
3325 vert 80new
Bram Moolenaarf5f1e102020-03-08 05:13:15 +01003326 call assert_beeps('normal gk')
Bram Moolenaar03ac52f2019-09-24 22:47:46 +02003327 put =[repeat('x',90)..' {{{1', 'x {{{1']
3328 norm! gk
3329 " In a 80 column wide terminal the window will be only 78 char
3330 " (because Vim will leave space for the other window),
3331 " but if the terminal is larger, it will be 80 chars, so verify the
3332 " cursor column correctly.
3333 call assert_equal(winwidth(0)+1, col('.'))
3334 call assert_equal(winwidth(0)+1, virtcol('.'))
3335 norm! j
3336 call assert_equal(6, col('.'))
3337 call assert_equal(6, virtcol('.'))
3338 norm! gk
3339 call assert_equal(95, col('.'))
3340 call assert_equal(95, virtcol('.'))
Bram Moolenaarf5f1e102020-03-08 05:13:15 +01003341 %bw!
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02003342
3343 " needs 80 column new window
3344 new
3345 vert 80new
Bram Moolenaarf5f1e102020-03-08 05:13:15 +01003346 call assert_beeps('normal gj')
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02003347 set number
3348 set numberwidth=10
3349 set cpoptions+=n
3350 put =[repeat('0',90), repeat('1',90)]
3351 norm! 075l
3352 call assert_equal(76, col('.'))
3353 norm! gk
3354 call assert_equal(1, col('.'))
3355 norm! gk
3356 call assert_equal(76, col('.'))
3357 norm! gk
3358 call assert_equal(1, col('.'))
3359 norm! gj
3360 call assert_equal(76, col('.'))
3361 norm! gj
3362 call assert_equal(1, col('.'))
3363 norm! gj
3364 call assert_equal(76, col('.'))
Bram Moolenaarf5f1e102020-03-08 05:13:15 +01003365 " When 'nowrap' is set, gk and gj behave like k and j
3366 set nowrap
3367 normal! gk
3368 call assert_equal([2, 76], [line('.'), col('.')])
3369 normal! gj
3370 call assert_equal([3, 76], [line('.'), col('.')])
3371 %bw!
3372 set cpoptions& number& numberwidth& wrap&
Bram Moolenaar03ac52f2019-09-24 22:47:46 +02003373endfunc
Bram Moolenaarf0cee192020-02-16 13:33:56 +01003374
Bram Moolenaar818fc9a2020-02-21 17:54:45 +01003375" Test for using : to run a multi-line Ex command in operator pending mode
3376func Test_normal_yank_with_excmd()
3377 new
3378 call setline(1, ['foo', 'bar', 'baz'])
3379 let @a = ''
3380 call feedkeys("\"ay:if v:true\<CR>normal l\<CR>endif\<CR>", 'xt')
3381 call assert_equal('f', @a)
3382 close!
3383endfunc
Bram Moolenaarf5f1e102020-03-08 05:13:15 +01003384
3385" Test for supplying a count to a normal-mode command across a cursorhold call
3386func Test_normal_cursorhold_with_count()
3387 func s:cHold()
3388 let g:cHold_Called += 1
3389 endfunc
3390 new
3391 augroup normalcHoldTest
3392 au!
3393 au CursorHold <buffer> call s:cHold()
3394 augroup END
3395 let g:cHold_Called = 0
3396 call feedkeys("3\<CursorHold>2ix", 'xt')
3397 call assert_equal(1, g:cHold_Called)
3398 call assert_equal(repeat('x', 32), getline(1))
3399 augroup normalcHoldTest
3400 au!
3401 augroup END
3402 au! normalcHoldTest
3403 close!
3404 delfunc s:cHold
3405endfunc
3406
3407" Test for using a count and a command with CTRL-W
3408func Test_wincmd_with_count()
3409 call feedkeys("\<C-W>12n", 'xt')
3410 call assert_equal(12, winheight(0))
3411endfunc
3412
3413" Test for 'b', 'B' 'ge' and 'gE' commands
Bram Moolenaar1671f442020-03-10 07:48:13 +01003414func Test_horiz_motion()
3415 new
Bram Moolenaarf5f1e102020-03-08 05:13:15 +01003416 normal! gg
3417 call assert_beeps('normal! b')
3418 call assert_beeps('normal! B')
3419 call assert_beeps('normal! gE')
3420 call assert_beeps('normal! ge')
Bram Moolenaar1671f442020-03-10 07:48:13 +01003421 " <S-Backspace> moves one word left and <C-Backspace> moves one WORD left
3422 call setline(1, 'one ,two ,three')
3423 exe "normal! $\<S-BS>"
3424 call assert_equal(11, col('.'))
3425 exe "normal! $\<C-BS>"
3426 call assert_equal(10, col('.'))
3427 close!
3428endfunc
3429
3430" Test for using a : command in operator pending mode
3431func Test_normal_colon_op()
3432 new
3433 call setline(1, ['one', 'two'])
3434 call assert_beeps("normal! Gc:d\<CR>")
3435 close!
Bram Moolenaarf5f1e102020-03-08 05:13:15 +01003436endfunc
3437
Bram Moolenaar004a6782020-04-11 17:09:31 +02003438" Test for d and D commands
3439func Test_normal_delete_cmd()
3440 new
3441 " D in an empty line
3442 call setline(1, '')
3443 normal D
3444 call assert_equal('', getline(1))
3445 " D in an empty line in virtualedit mode
3446 set virtualedit=all
3447 normal D
3448 call assert_equal('', getline(1))
3449 set virtualedit&
3450 " delete to a readonly register
3451 call setline(1, ['abcd'])
3452 call assert_beeps('normal ":d2l')
Bram Moolenaar6fd367a2021-03-13 13:14:04 +01003453
3454 " D and d with 'nomodifiable'
3455 call setline(1, ['abcd'])
3456 setlocal nomodifiable
3457 call assert_fails('normal D', 'E21:')
3458 call assert_fails('normal d$', 'E21:')
3459
Bram Moolenaar004a6782020-04-11 17:09:31 +02003460 close!
3461endfunc
3462
Bram Moolenaar8a9bc952020-10-02 18:48:07 +02003463" Test for deleting or changing characters across lines with 'whichwrap'
3464" containing 's'. Should count <EOL> as one character.
3465func Test_normal_op_across_lines()
3466 new
3467 set whichwrap&
3468 call setline(1, ['one two', 'three four'])
3469 exe "norm! $3d\<Space>"
3470 call assert_equal(['one twhree four'], getline(1, '$'))
3471
3472 call setline(1, ['one two', 'three four'])
3473 exe "norm! $3c\<Space>x"
3474 call assert_equal(['one twxhree four'], getline(1, '$'))
3475
3476 set whichwrap+=l
3477 call setline(1, ['one two', 'three four'])
3478 exe "norm! $3x"
3479 call assert_equal(['one twhree four'], getline(1, '$'))
3480 close!
3481 set whichwrap&
3482endfunc
3483
Bram Moolenaar224a5f12020-04-28 20:29:07 +02003484" Test for 'w' and 'b' commands
3485func Test_normal_word_move()
3486 new
3487 call setline(1, ['foo bar a', '', 'foo bar b'])
3488 " copy a single character word at the end of a line
3489 normal 1G$yw
3490 call assert_equal('a', @")
3491 " copy a single character word at the end of a file
3492 normal G$yw
3493 call assert_equal('b', @")
3494 " check for a word movement handling an empty line properly
3495 normal 1G$vwy
3496 call assert_equal("a\n\n", @")
3497
3498 " copy using 'b' command
3499 %d
3500 " non-empty blank line at the start of file
3501 call setline(1, [' ', 'foo bar'])
3502 normal 2Gyb
3503 call assert_equal(" \n", @")
3504 " try to copy backwards from the start of the file
3505 call setline(1, ['one two', 'foo bar'])
3506 call assert_beeps('normal ggyb')
3507 " 'b' command should stop at an empty line
3508 call setline(1, ['one two', '', 'foo bar'])
3509 normal 3Gyb
3510 call assert_equal("\n", @")
3511 normal 3Gy2b
3512 call assert_equal("two\n", @")
3513 " 'b' command should not stop at a non-empty blank line
3514 call setline(1, ['one two', ' ', 'foo bar'])
3515 normal 3Gyb
3516 call assert_equal("two\n ", @")
3517
3518 close!
3519endfunc
3520
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02003521" Test for 'scrolloff' with a long line that doesn't fit in the screen
3522func Test_normal_scroloff()
3523 10new
3524 80vnew
3525 call setline(1, repeat('a', 1000))
3526 set scrolloff=10
3527 normal gg10gj
3528 call assert_equal(8, winline())
3529 normal 10gj
3530 call assert_equal(10, winline())
3531 normal 10gk
3532 call assert_equal(3, winline())
3533 set scrolloff&
3534 close!
3535endfunc
3536
3537" Test for vertical scrolling with CTRL-F and CTRL-B with a long line
3538func Test_normal_vert_scroll_longline()
3539 10new
3540 80vnew
3541 call setline(1, range(1, 10))
3542 call append(5, repeat('a', 1000))
3543 exe "normal gg\<C-F>"
3544 call assert_equal(6, line('.'))
3545 exe "normal \<C-F>\<C-F>"
3546 call assert_equal(11, line('.'))
3547 call assert_equal(1, winline())
3548 exe "normal \<C-B>"
3549 call assert_equal(10, line('.'))
3550 call assert_equal(3, winline())
3551 exe "normal \<C-B>\<C-B>"
3552 call assert_equal(5, line('.'))
3553 call assert_equal(5, winline())
3554 close!
3555endfunc
3556
Bram Moolenaar8a9bc952020-10-02 18:48:07 +02003557" Test for jumping in a file using %
3558func Test_normal_percent_jump()
3559 new
3560 call setline(1, range(1, 100))
3561
3562 " jumping to a folded line should open the fold
3563 25,75fold
3564 call feedkeys('50%', 'xt')
3565 call assert_equal(50, line('.'))
3566 call assert_equal(-1, foldclosedend(50))
3567 close!
3568endfunc
3569
Bram Moolenaar3e72dca2021-05-29 16:30:12 +02003570" Test for << and >> commands to shift text by 'shiftwidth'
3571func Test_normal_shift_rightleft()
3572 new
3573 call setline(1, ['one', '', "\t", ' two', "\tthree", ' four'])
3574 set shiftwidth=2 tabstop=8
3575 normal gg6>>
3576 call assert_equal([' one', '', "\t ", ' two', "\t three", "\tfour"],
3577 \ getline(1, '$'))
3578 normal ggVG2>>
3579 call assert_equal([' one', '', "\t ", "\ttwo",
3580 \ "\t three", "\t four"], getline(1, '$'))
3581 normal gg6<<
3582 call assert_equal([' one', '', "\t ", ' two', "\t three",
3583 \ "\t four"], getline(1, '$'))
3584 normal ggVG2<<
3585 call assert_equal(['one', '', "\t", ' two', "\tthree", ' four'],
3586 \ getline(1, '$'))
3587 set shiftwidth& tabstop&
3588 bw!
3589endfunc
3590
Yegappan Lakshmanan2ac71842021-05-31 19:23:01 +02003591" Some commands like yy, cc, dd, >>, << and !! accept a count after
3592" typing the first letter of the command.
3593func Test_normal_count_after_operator()
3594 new
3595 setlocal shiftwidth=4 tabstop=8 autoindent
3596 call setline(1, ['one', 'two', 'three', 'four', 'five'])
3597 let @a = ''
3598 normal! j"ay4y
3599 call assert_equal("two\nthree\nfour\nfive\n", @a)
3600 normal! 3G>2>
3601 call assert_equal(['one', 'two', ' three', ' four', 'five'],
3602 \ getline(1, '$'))
3603 exe "normal! 3G0c2cred\nblue"
3604 call assert_equal(['one', 'two', ' red', ' blue', 'five'],
3605 \ getline(1, '$'))
3606 exe "normal! gg<8<"
3607 call assert_equal(['one', 'two', 'red', 'blue', 'five'],
3608 \ getline(1, '$'))
3609 exe "normal! ggd3d"
3610 call assert_equal(['blue', 'five'], getline(1, '$'))
3611 call setline(1, range(1, 4))
3612 call feedkeys("gg!3!\<C-B>\"\<CR>", 'xt')
3613 call assert_equal('".,.+2!', @:)
3614 call feedkeys("gg!1!\<C-B>\"\<CR>", 'xt')
3615 call assert_equal('".!', @:)
3616 call feedkeys("gg!9!\<C-B>\"\<CR>", 'xt')
3617 call assert_equal('".,$!', @:)
3618 bw!
3619endfunc
3620
Christian Brabandtaaec1d42021-11-04 13:28:29 +00003621func Test_normal_gj_on_extra_wide_char()
3622 new | 25vsp
3623 let text='1 foooooooo ar e ins‍zwe1 foooooooo ins‍zwei' .
3624 \ ' i drei vier fünf sechs sieben acht un zehn elf zwöfl' .
3625 \ ' dreizehn v ierzehn fünfzehn'
3626 put =text
3627 call cursor(2,1)
3628 norm! gj
3629 call assert_equal([0,2,25,0], getpos('.'))
3630 bw!
3631endfunc
3632
Bram Moolenaar03725c52021-11-24 12:17:53 +00003633func Test_normal_count_out_of_range()
3634 new
3635 call setline(1, 'text')
3636 normal 44444444444|
3637 call assert_equal(999999999, v:count)
3638 normal 444444444444|
3639 call assert_equal(999999999, v:count)
3640 normal 4444444444444|
3641 call assert_equal(999999999, v:count)
3642 normal 4444444444444444444|
3643 call assert_equal(999999999, v:count)
3644
3645 normal 9y99999999|
3646 call assert_equal(899999991, v:count)
3647 normal 10y99999999|
3648 call assert_equal(999999999, v:count)
3649 normal 44444444444y44444444444|
3650 call assert_equal(999999999, v:count)
3651 bwipe!
3652endfunc
3653
Bram Moolenaarf5f1e102020-03-08 05:13:15 +01003654" vim: shiftwidth=2 sts=2 expandtab