blob: e6c392df0f2675a0e67b10652b82fc8aac7a0ad3 [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 Moolenaar004a6782020-04-11 17:09:31 +02002339 CheckFeature jumplist
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002340 call Setup_NewWindow()
2341 " Test for g`
2342 clearjumps
2343 norm! ma10j
2344 let a=execute(':jumps')
2345 " empty jumplist
2346 call assert_equal('>', a[-1:])
2347 norm! g`a
2348 call assert_equal('>', a[-1:])
2349 call assert_equal(1, line('.'))
2350 call assert_equal('1', getline('.'))
Bram Moolenaarf5f1e102020-03-08 05:13:15 +01002351 call cursor(10, 1)
2352 norm! g'a
2353 call assert_equal('>', a[-1:])
2354 call assert_equal(1, line('.'))
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002355
2356 " Test for g; and g,
2357 norm! g;
2358 " there is only one change in the changelist
2359 " currently, when we setup the window
2360 call assert_equal(2, line('.'))
Bram Moolenaare2e40752020-09-04 21:18:46 +02002361 call assert_fails(':norm! g;', 'E662:')
2362 call assert_fails(':norm! g,', 'E663:')
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002363 let &ul=&ul
2364 call append('$', ['a', 'b', 'c', 'd'])
2365 let &ul=&ul
2366 call append('$', ['Z', 'Y', 'X', 'W'])
2367 let a = execute(':changes')
2368 call assert_match('2\s\+0\s\+2', a)
2369 call assert_match('101\s\+0\s\+a', a)
2370 call assert_match('105\s\+0\s\+Z', a)
2371 norm! 3g;
2372 call assert_equal(2, line('.'))
2373 norm! 2g,
2374 call assert_equal(105, line('.'))
2375
2376 " Test for g& - global substitute
2377 %d
2378 call setline(1, range(1,10))
2379 call append('$', ['a', 'b', 'c', 'd'])
2380 $s/\w/&&/g
2381 exe "norm! /[1-8]\<cr>"
2382 norm! g&
2383 call assert_equal(['11', '22', '33', '44', '55', '66', '77', '88', '9', '110', 'a', 'b', 'c', 'dd'], getline(1, '$'))
2384
Bram Moolenaar1671f442020-03-10 07:48:13 +01002385 " Jumping to a fold using gg should open the fold
2386 set foldenable
2387 set foldopen+=jump
2388 5,8fold
2389 call feedkeys('6gg', 'xt')
2390 call assert_equal(1, foldlevel('.'))
2391 call assert_equal(-1, foldclosed('.'))
2392 set foldopen-=jump
2393 set foldenable&
2394
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002395 " Test for gv
2396 %d
2397 call append('$', repeat(['abcdefgh'], 8))
2398 exe "norm! 2gg02l\<c-v>2j2ly"
2399 call assert_equal(['cde', 'cde', 'cde'], getreg(0, 1, 1))
2400 " in visual mode, gv swaps current and last selected region
2401 exe "norm! G0\<c-v>4k4lgvd"
2402 call assert_equal(['', 'abfgh', 'abfgh', 'abfgh', 'abcdefgh', 'abcdefgh', 'abcdefgh', 'abcdefgh', 'abcdefgh'], getline(1,'$'))
2403 exe "norm! G0\<c-v>4k4ly"
2404 exe "norm! gvood"
2405 call assert_equal(['', 'abfgh', 'abfgh', 'abfgh', 'fgh', 'fgh', 'fgh', 'fgh', 'fgh'], getline(1,'$'))
Bram Moolenaarf5f1e102020-03-08 05:13:15 +01002406 " gv cannot be used in operator pending mode
2407 call assert_beeps('normal! cgv')
2408 " gv should beep without a previously selected visual area
2409 new
2410 call assert_beeps('normal! gv')
2411 close
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002412
2413 " Test for gk/gj
2414 %d
2415 15vsp
2416 set wrap listchars= sbr=
Bram Moolenaar74ede802021-05-29 19:18:01 +02002417 let lineA = 'abcdefghijklmnopqrstuvwxyz'
2418 let lineB = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'
2419 let lineC = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz01234567890123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002420 $put =lineA
2421 $put =lineB
2422
2423 norm! 3gg0dgk
2424 call assert_equal(['', 'abcdefghijklmno', '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'], getline(1, '$'))
2425 set nu
2426 norm! 3gg0gjdgj
2427 call assert_equal(['', 'abcdefghijklmno', '0123456789AMNOPQRSTUVWXYZ'], getline(1,'$'))
2428
2429 " Test for gJ
2430 norm! 2gggJ
2431 call assert_equal(['', 'abcdefghijklmno0123456789AMNOPQRSTUVWXYZ'], getline(1,'$'))
2432 call assert_equal(16, col('.'))
2433 " shouldn't do anything
2434 norm! 10gJ
2435 call assert_equal(1, col('.'))
2436
2437 " Test for g0 g^ gm g$
2438 exe "norm! 2gg0gji "
2439 call assert_equal(['', 'abcdefghijk lmno0123456789AMNOPQRSTUVWXYZ'], getline(1,'$'))
2440 norm! g0yl
2441 call assert_equal(12, col('.'))
2442 call assert_equal(' ', getreg(0))
2443 norm! g$yl
2444 call assert_equal(22, col('.'))
2445 call assert_equal('3', getreg(0))
2446 norm! gmyl
2447 call assert_equal(17, col('.'))
2448 call assert_equal('n', getreg(0))
2449 norm! g^yl
2450 call assert_equal(15, col('.'))
2451 call assert_equal('l', getreg(0))
Bram Moolenaarf5f1e102020-03-08 05:13:15 +01002452 call assert_beeps('normal 5g$')
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002453
Bram Moolenaar74ede802021-05-29 19:18:01 +02002454 " Test for g$ with double-width character half displayed
2455 vsplit
2456 9wincmd |
2457 setlocal nowrap nonumber
2458 call setline(2, 'asdfasdfヨ')
2459 2
2460 normal 0g$
2461 call assert_equal(8, col('.'))
2462 10wincmd |
2463 normal 0g$
2464 call assert_equal(9, col('.'))
2465
2466 setlocal signcolumn=yes
2467 11wincmd |
2468 normal 0g$
2469 call assert_equal(8, col('.'))
2470 12wincmd |
2471 normal 0g$
2472 call assert_equal(9, col('.'))
2473
2474 close
2475
Bram Moolenaarf5f1e102020-03-08 05:13:15 +01002476 " Test for g_
2477 call assert_beeps('normal! 100g_')
2478 call setline(2, [' foo ', ' foobar '])
2479 normal! 2ggg_
2480 call assert_equal(5, col('.'))
2481 normal! 2g_
2482 call assert_equal(8, col('.'))
2483
2484 norm! 2ggdG
Bram Moolenaar8b530c12019-10-28 02:13:05 +01002485 $put =lineC
2486
2487 " Test for gM
2488 norm! gMyl
2489 call assert_equal(73, col('.'))
2490 call assert_equal('0', getreg(0))
2491 " Test for 20gM
2492 norm! 20gMyl
2493 call assert_equal(29, col('.'))
2494 call assert_equal('S', getreg(0))
2495 " Test for 60gM
2496 norm! 60gMyl
2497 call assert_equal(87, col('.'))
2498 call assert_equal('E', getreg(0))
2499
2500 " Test for g Ctrl-G
2501 set ff=unix
2502 let a=execute(":norm! g\<c-g>")
2503 call assert_match('Col 87 of 144; Line 2 of 2; Word 1 of 1; Byte 88 of 146', a)
2504
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002505 " Test for gI
2506 norm! gIfoo
Bram Moolenaar8b530c12019-10-28 02:13:05 +01002507 call assert_equal(['', 'foo0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz01234567890123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'], getline(1,'$'))
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002508
2509 " Test for gi
2510 wincmd c
2511 %d
2512 set tw=0
2513 call setline(1, ['foobar', 'new line'])
2514 norm! A next word
2515 $put ='third line'
2516 norm! gi another word
2517 call assert_equal(['foobar next word another word', 'new line', 'third line'], getline(1,'$'))
Bram Moolenaarf5f1e102020-03-08 05:13:15 +01002518 call setline(1, 'foobar')
2519 normal! Ggifirst line
2520 call assert_equal('foobarfirst line', getline(1))
2521 " Test gi in 'virtualedit' mode with cursor after the end of the line
2522 set virtualedit=all
2523 call setline(1, 'foo')
2524 exe "normal! Abar\<Right>\<Right>\<Right>\<Right>"
2525 call setline(1, 'foo')
2526 normal! Ggifirst line
2527 call assert_equal('foo first line', getline(1))
2528 set virtualedit&
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002529
Dominique Pelle923dce22021-11-21 11:36:04 +00002530 " Test for aborting a g command using CTRL-\ CTRL-G
Bram Moolenaar1671f442020-03-10 07:48:13 +01002531 exe "normal! g\<C-\>\<C-G>"
2532 call assert_equal('foo first line', getline('.'))
2533
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002534 " clean up
2535 bw!
Bram Moolenaar2931f2a2016-09-09 16:59:08 +02002536endfunc
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002537
Bram Moolenaarf5f1e102020-03-08 05:13:15 +01002538" Test for g CTRL-G
Bram Moolenaar1bbb6192018-11-10 16:02:01 +01002539func Test_g_ctrl_g()
Bram Moolenaar05295832018-08-24 22:07:58 +02002540 new
2541
2542 let a = execute(":norm! g\<c-g>")
2543 call assert_equal("\n--No lines in buffer--", a)
2544
Bram Moolenaar1671f442020-03-10 07:48:13 +01002545 " Test for CTRL-G (same as :file)
2546 let a = execute(":norm! \<c-g>")
2547 call assert_equal("\n\n\"[No Name]\" --No lines in buffer--", a)
2548
Bram Moolenaar05295832018-08-24 22:07:58 +02002549 call setline(1, ['first line', 'second line'])
2550
2551 " Test g CTRL-g with dos, mac and unix file type.
2552 norm! gojll
2553 set ff=dos
2554 let a = execute(":norm! g\<c-g>")
2555 call assert_equal("\nCol 3 of 11; Line 2 of 2; Word 3 of 4; Byte 15 of 25", a)
2556
2557 set ff=mac
2558 let a = execute(":norm! g\<c-g>")
2559 call assert_equal("\nCol 3 of 11; Line 2 of 2; Word 3 of 4; Byte 14 of 23", a)
2560
2561 set ff=unix
2562 let a = execute(":norm! g\<c-g>")
2563 call assert_equal("\nCol 3 of 11; Line 2 of 2; Word 3 of 4; Byte 14 of 23", a)
2564
2565 " Test g CTRL-g in visual mode (v)
2566 let a = execute(":norm! gojllvlg\<c-g>")
2567 call assert_equal("\nSelected 1 of 2 Lines; 1 of 4 Words; 2 of 23 Bytes", a)
2568
2569 " Test g CTRL-g in visual mode (CTRL-V) with end col > start col
2570 let a = execute(":norm! \<Esc>gojll\<C-V>kllg\<c-g>")
2571 call assert_equal("\nSelected 3 Cols; 2 of 2 Lines; 2 of 4 Words; 6 of 23 Bytes", a)
2572
2573 " Test g_CTRL-g in visual mode (CTRL-V) with end col < start col
2574 let a = execute(":norm! \<Esc>goll\<C-V>jhhg\<c-g>")
2575 call assert_equal("\nSelected 3 Cols; 2 of 2 Lines; 2 of 4 Words; 6 of 23 Bytes", a)
2576
2577 " Test g CTRL-g in visual mode (CTRL-V) with end_vcol being MAXCOL
2578 let a = execute(":norm! \<Esc>gojll\<C-V>k$g\<c-g>")
2579 call assert_equal("\nSelected 2 of 2 Lines; 4 of 4 Words; 17 of 23 Bytes", a)
2580
2581 " There should be one byte less with noeol
2582 set bin noeol
2583 let a = execute(":norm! \<Esc>gog\<c-g>")
2584 call assert_equal("\nCol 1 of 10; Line 1 of 2; Word 1 of 4; Char 1 of 23; Byte 1 of 22", a)
2585 set bin & eol&
2586
Bram Moolenaar30276f22019-01-24 17:59:39 +01002587 call setline(1, ['Français', '日本語'])
Bram Moolenaar05295832018-08-24 22:07:58 +02002588
Bram Moolenaar30276f22019-01-24 17:59:39 +01002589 let a = execute(":norm! \<Esc>gojlg\<c-g>")
2590 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 +02002591
Bram Moolenaar30276f22019-01-24 17:59:39 +01002592 let a = execute(":norm! \<Esc>gojvlg\<c-g>")
2593 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 +02002594
Bram Moolenaar30276f22019-01-24 17:59:39 +01002595 let a = execute(":norm! \<Esc>goll\<c-v>jlg\<c-g>")
2596 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 +02002597
Bram Moolenaar30276f22019-01-24 17:59:39 +01002598 set fenc=utf8 bomb
2599 let a = execute(":norm! \<Esc>gojlg\<c-g>")
2600 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 +02002601
Bram Moolenaar30276f22019-01-24 17:59:39 +01002602 set fenc=utf16 bomb
2603 let a = execute(":norm! g\<c-g>")
2604 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 +02002605
Bram Moolenaar30276f22019-01-24 17:59:39 +01002606 set fenc=utf32 bomb
2607 let a = execute(":norm! g\<c-g>")
2608 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 +02002609
Bram Moolenaar30276f22019-01-24 17:59:39 +01002610 set fenc& bomb&
Bram Moolenaar05295832018-08-24 22:07:58 +02002611
2612 set ff&
2613 bwipe!
2614endfunc
2615
Bram Moolenaarf5f1e102020-03-08 05:13:15 +01002616" Test for g8
Bram Moolenaar1671f442020-03-10 07:48:13 +01002617func Test_normal34_g_cmd3()
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002618 new
Bram Moolenaar395b6ba2017-04-07 20:09:51 +02002619 let a=execute(':norm! 1G0g8')
2620 call assert_equal("\nNUL", a)
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002621
Bram Moolenaar395b6ba2017-04-07 20:09:51 +02002622 call setline(1, 'abcdefghijklmnopqrstuvwxyzäüö')
2623 let a=execute(':norm! 1G$g8')
2624 call assert_equal("\nc3 b6 ", a)
2625
2626 call setline(1, "a\u0302")
2627 let a=execute(':norm! 1G0g8')
2628 call assert_equal("\n61 + cc 82 ", a)
2629
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002630 " clean up
2631 bw!
Bram Moolenaar2931f2a2016-09-09 16:59:08 +02002632endfunc
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002633
Bram Moolenaarf5f1e102020-03-08 05:13:15 +01002634" Test 8g8 which finds invalid utf8 at or after the cursor.
Bram Moolenaar395b6ba2017-04-07 20:09:51 +02002635func Test_normal_8g8()
Bram Moolenaar395b6ba2017-04-07 20:09:51 +02002636 new
2637
Bram Moolenaar395b6ba2017-04-07 20:09:51 +02002638 " With invalid byte.
2639 call setline(1, "___\xff___")
2640 norm! 1G08g8g
2641 call assert_equal([0, 1, 4, 0, 1], getcurpos())
2642
2643 " With invalid byte before the cursor.
2644 call setline(1, "___\xff___")
2645 norm! 1G$h8g8g
2646 call assert_equal([0, 1, 6, 0, 9], getcurpos())
2647
2648 " With truncated sequence.
2649 call setline(1, "___\xE2\x82___")
2650 norm! 1G08g8g
2651 call assert_equal([0, 1, 4, 0, 1], getcurpos())
2652
2653 " With overlong sequence.
2654 call setline(1, "___\xF0\x82\x82\xAC___")
2655 norm! 1G08g8g
2656 call assert_equal([0, 1, 4, 0, 1], getcurpos())
2657
2658 " With valid utf8.
2659 call setline(1, "café")
2660 norm! 1G08g8
2661 call assert_equal([0, 1, 1, 0, 1], getcurpos())
2662
2663 bw!
2664endfunc
2665
Bram Moolenaarf5f1e102020-03-08 05:13:15 +01002666" Test for g<
Bram Moolenaar1671f442020-03-10 07:48:13 +01002667func Test_normal35_g_cmd4()
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002668 " Cannot capture its output,
2669 " probably a bug, therefore, test disabled:
Bram Moolenaar31845092016-09-05 22:58:31 +02002670 throw "Skipped: output of g< can't be tested currently"
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002671 echo "a\nb\nc\nd"
2672 let b=execute(':norm! g<')
2673 call assert_true(!empty(b), 'failed `execute(g<)`')
Bram Moolenaar2931f2a2016-09-09 16:59:08 +02002674endfunc
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002675
Bram Moolenaarf5f1e102020-03-08 05:13:15 +01002676" Test for gp gP go
Bram Moolenaar1671f442020-03-10 07:48:13 +01002677func Test_normal36_g_cmd5()
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002678 new
2679 call append(0, 'abcdefghijklmnopqrstuvwxyz')
Bram Moolenaar0913a102016-09-03 19:11:59 +02002680 set ff=unix
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002681 " Test for gp gP
2682 call append(1, range(1,10))
2683 1
2684 norm! 1yy
2685 3
2686 norm! gp
2687 call assert_equal([0, 5, 1, 0, 1], getcurpos())
2688 $
2689 norm! gP
2690 call assert_equal([0, 14, 1, 0, 1], getcurpos())
2691
2692 " Test for go
2693 norm! 26go
2694 call assert_equal([0, 1, 26, 0, 26], getcurpos())
2695 norm! 27go
2696 call assert_equal([0, 1, 26, 0, 26], getcurpos())
2697 norm! 28go
2698 call assert_equal([0, 2, 1, 0, 1], getcurpos())
2699 set ff=dos
2700 norm! 29go
2701 call assert_equal([0, 2, 1, 0, 1], getcurpos())
2702 set ff=unix
2703 norm! gg0
2704 norm! 101go
2705 call assert_equal([0, 13, 26, 0, 26], getcurpos())
2706 norm! 103go
2707 call assert_equal([0, 14, 1, 0, 1], getcurpos())
2708 " count > buffer content
2709 norm! 120go
2710 call assert_equal([0, 14, 1, 0, 2147483647], getcurpos())
2711 " clean up
2712 bw!
Bram Moolenaar2931f2a2016-09-09 16:59:08 +02002713endfunc
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002714
Bram Moolenaarf5f1e102020-03-08 05:13:15 +01002715" Test for gt and gT
Bram Moolenaar1671f442020-03-10 07:48:13 +01002716func Test_normal37_g_cmd6()
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002717 tabnew 1.txt
2718 tabnew 2.txt
2719 tabnew 3.txt
2720 norm! 1gt
2721 call assert_equal(1, tabpagenr())
2722 norm! 3gt
2723 call assert_equal(3, tabpagenr())
2724 norm! 1gT
2725 " count gT goes not to the absolute tabpagenumber
2726 " but, but goes to the count previous tabpagenumber
2727 call assert_equal(2, tabpagenr())
2728 " wrap around
2729 norm! 3gT
2730 call assert_equal(3, tabpagenr())
2731 " gt does not wrap around
2732 norm! 5gt
2733 call assert_equal(3, tabpagenr())
2734
2735 for i in range(3)
2736 tabclose
2737 endfor
2738 " clean up
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +01002739 call assert_fails(':tabclose', 'E784:')
Bram Moolenaar2931f2a2016-09-09 16:59:08 +02002740endfunc
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002741
Bram Moolenaarf5f1e102020-03-08 05:13:15 +01002742" Test for <Home> and <C-Home> key
Bram Moolenaar1671f442020-03-10 07:48:13 +01002743func Test_normal38_nvhome()
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002744 new
2745 call setline(1, range(10))
2746 $
2747 setl et sw=2
2748 norm! V10>$
2749 " count is ignored
2750 exe "norm! 10\<home>"
2751 call assert_equal(1, col('.'))
2752 exe "norm! \<home>"
2753 call assert_equal([0, 10, 1, 0, 1], getcurpos())
2754 exe "norm! 5\<c-home>"
2755 call assert_equal([0, 5, 1, 0, 1], getcurpos())
2756 exe "norm! \<c-home>"
2757 call assert_equal([0, 1, 1, 0, 1], getcurpos())
Bram Moolenaarf5f1e102020-03-08 05:13:15 +01002758 exe "norm! G\<c-kHome>"
2759 call assert_equal([0, 1, 1, 0, 1], getcurpos())
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002760
2761 " clean up
2762 bw!
Bram Moolenaar2931f2a2016-09-09 16:59:08 +02002763endfunc
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002764
Bram Moolenaar1671f442020-03-10 07:48:13 +01002765" Test for <End> and <C-End> keys
2766func Test_normal_nvend()
2767 new
2768 call setline(1, map(range(1, 10), '"line" .. v:val'))
2769 exe "normal! \<End>"
2770 call assert_equal(5, col('.'))
2771 exe "normal! 4\<End>"
2772 call assert_equal([4, 5], [line('.'), col('.')])
2773 exe "normal! \<C-End>"
2774 call assert_equal([10, 6], [line('.'), col('.')])
2775 close!
2776endfunc
2777
Bram Moolenaarf5f1e102020-03-08 05:13:15 +01002778" Test for cw cW ce
Bram Moolenaar1671f442020-03-10 07:48:13 +01002779func Test_normal39_cw()
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002780 " Test for cw and cW on whitespace
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002781 new
2782 set tw=0
2783 call append(0, 'here are some words')
2784 norm! 1gg0elcwZZZ
2785 call assert_equal('hereZZZare some words', getline('.'))
2786 norm! 1gg0elcWYYY
2787 call assert_equal('hereZZZareYYYsome words', getline('.'))
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002788 norm! 2gg0cwfoo
2789 call assert_equal('foo', getline('.'))
2790
Bram Moolenaarf5f1e102020-03-08 05:13:15 +01002791 call setline(1, 'one; two')
2792 call cursor(1, 1)
2793 call feedkeys('cwvim', 'xt')
2794 call assert_equal('vim; two', getline(1))
2795 call feedkeys('0cWone', 'xt')
2796 call assert_equal('one two', getline(1))
2797 "When cursor is at the end of a word 'ce' will change until the end of the
2798 "next word, but 'cw' will change only one character
2799 call setline(1, 'one two')
2800 call feedkeys('0ecwce', 'xt')
2801 call assert_equal('once two', getline(1))
2802 call setline(1, 'one two')
2803 call feedkeys('0ecely', 'xt')
2804 call assert_equal('only', getline(1))
2805
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002806 " clean up
2807 bw!
Bram Moolenaar2931f2a2016-09-09 16:59:08 +02002808endfunc
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002809
Bram Moolenaarf5f1e102020-03-08 05:13:15 +01002810" Test for CTRL-\ commands
Bram Moolenaar1671f442020-03-10 07:48:13 +01002811func Test_normal40_ctrl_bsl()
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002812 new
2813 call append(0, 'here are some words')
2814 exe "norm! 1gg0a\<C-\>\<C-N>"
2815 call assert_equal('n', mode())
2816 call assert_equal(1, col('.'))
2817 call assert_equal('', visualmode())
2818 exe "norm! 1gg0viw\<C-\>\<C-N>"
2819 call assert_equal('n', mode())
2820 call assert_equal(4, col('.'))
2821 exe "norm! 1gg0a\<C-\>\<C-G>"
2822 call assert_equal('n', mode())
2823 call assert_equal(1, col('.'))
2824 "imap <buffer> , <c-\><c-n>
2825 set im
2826 exe ":norm! \<c-\>\<c-n>dw"
2827 set noim
2828 call assert_equal('are some words', getline(1))
2829 call assert_false(&insertmode)
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002830 call assert_beeps("normal! \<C-\>\<C-A>")
Bram Moolenaar1671f442020-03-10 07:48:13 +01002831
Bram Moolenaar21829c52021-01-26 22:42:21 +01002832 if has('cmdwin')
2833 " Using CTRL-\ CTRL-N in cmd window should close the window
2834 call feedkeys("q:\<C-\>\<C-N>", 'xt')
2835 call assert_equal('', getcmdwintype())
2836 endif
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002837
2838 " clean up
2839 bw!
Bram Moolenaar2931f2a2016-09-09 16:59:08 +02002840endfunc
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002841
Bram Moolenaarf5f1e102020-03-08 05:13:15 +01002842" Test for <c-r>=, <c-r><c-r>= and <c-r><c-o>= in insert mode
Bram Moolenaar1671f442020-03-10 07:48:13 +01002843func Test_normal41_insert_reg()
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002844 new
2845 set sts=2 sw=2 ts=8 tw=0
2846 call append(0, ["aaa\tbbb\tccc", '', '', ''])
2847 let a=getline(1)
2848 norm! 2gg0
2849 exe "norm! a\<c-r>=a\<cr>"
2850 norm! 3gg0
2851 exe "norm! a\<c-r>\<c-r>=a\<cr>"
2852 norm! 4gg0
2853 exe "norm! a\<c-r>\<c-o>=a\<cr>"
2854 call assert_equal(['aaa bbb ccc', 'aaa bbb ccc', 'aaa bbb ccc', 'aaa bbb ccc', ''], getline(1, '$'))
2855
2856 " clean up
2857 set sts=0 sw=8 ts=8
Bram Moolenaar31845092016-09-05 22:58:31 +02002858 bw!
Bram Moolenaar2931f2a2016-09-09 16:59:08 +02002859endfunc
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002860
Bram Moolenaarf5f1e102020-03-08 05:13:15 +01002861" Test for Ctrl-D and Ctrl-U
Bram Moolenaar1bbb6192018-11-10 16:02:01 +01002862func Test_normal42_halfpage()
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002863 call Setup_NewWindow()
2864 call assert_equal(5, &scroll)
2865 exe "norm! \<c-d>"
2866 call assert_equal('6', getline('.'))
2867 exe "norm! 2\<c-d>"
2868 call assert_equal('8', getline('.'))
2869 call assert_equal(2, &scroll)
2870 set scroll=5
2871 exe "norm! \<c-u>"
2872 call assert_equal('3', getline('.'))
2873 1
2874 set scrolloff=5
2875 exe "norm! \<c-d>"
2876 call assert_equal('10', getline('.'))
2877 exe "norm! \<c-u>"
2878 call assert_equal('5', getline('.'))
2879 1
2880 set scrolloff=99
2881 exe "norm! \<c-d>"
2882 call assert_equal('10', getline('.'))
2883 set scrolloff=0
2884 100
2885 exe "norm! $\<c-u>"
2886 call assert_equal('95', getline('.'))
2887 call assert_equal([0, 95, 1, 0, 1], getcurpos())
2888 100
2889 set nostartofline
2890 exe "norm! $\<c-u>"
2891 call assert_equal('95', getline('.'))
2892 call assert_equal([0, 95, 2, 0, 2147483647], getcurpos())
2893 " cleanup
2894 set startofline
2895 bw!
Bram Moolenaar2931f2a2016-09-09 16:59:08 +02002896endfunc
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002897
Bram Moolenaar1bbb6192018-11-10 16:02:01 +01002898func Test_normal45_drop()
Bram Moolenaar29495952018-02-12 22:49:00 +01002899 if !has('dnd')
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01002900 " The ~ register does not exist
2901 call assert_beeps('norm! "~')
Bram Moolenaar29495952018-02-12 22:49:00 +01002902 return
2903 endif
2904
2905 " basic test for drag-n-drop
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002906 " unfortunately, without a gui, we can't really test much here,
2907 " so simply test that ~p fails (which uses the drop register)
2908 new
Bram Moolenaare2e40752020-09-04 21:18:46 +02002909 call assert_fails(':norm! "~p', 'E353:')
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002910 call assert_equal([], getreg('~', 1, 1))
2911 " the ~ register is read only
Bram Moolenaare2e40752020-09-04 21:18:46 +02002912 call assert_fails(':let @~="1"', 'E354:')
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002913 bw!
Bram Moolenaar2931f2a2016-09-09 16:59:08 +02002914endfunc
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002915
Bram Moolenaar1bbb6192018-11-10 16:02:01 +01002916func Test_normal46_ignore()
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002917 new
2918 " How to test this?
2919 " let's just for now test, that the buffer
2920 " does not change
2921 call feedkeys("\<c-s>", 't')
2922 call assert_equal([''], getline(1,'$'))
2923
Bram Moolenaar2931f2a2016-09-09 16:59:08 +02002924 " no valid commands
2925 exe "norm! \<char-0x100>"
2926 call assert_equal([''], getline(1,'$'))
2927
2928 exe "norm! ä"
2929 call assert_equal([''], getline(1,'$'))
2930
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002931 " clean up
2932 bw!
Bram Moolenaar2931f2a2016-09-09 16:59:08 +02002933endfunc
Bram Moolenaarc4a908e2016-09-08 23:35:30 +02002934
Bram Moolenaar1bbb6192018-11-10 16:02:01 +01002935func Test_normal47_visual_buf_wipe()
Bram Moolenaarc4a908e2016-09-08 23:35:30 +02002936 " This was causing a crash or ml_get error.
2937 enew!
2938 call setline(1,'xxx')
2939 normal $
2940 new
2941 call setline(1, range(1,2))
2942 2
2943 exe "norm \<C-V>$"
2944 bw!
2945 norm yp
2946 set nomodified
Bram Moolenaar2931f2a2016-09-09 16:59:08 +02002947endfunc
2948
Bram Moolenaar1bbb6192018-11-10 16:02:01 +01002949func Test_normal48_wincmd()
Bram Moolenaar2931f2a2016-09-09 16:59:08 +02002950 new
2951 exe "norm! \<c-w>c"
2952 call assert_equal(1, winnr('$'))
Bram Moolenaare2e40752020-09-04 21:18:46 +02002953 call assert_fails(":norm! \<c-w>c", 'E444:')
Bram Moolenaar2931f2a2016-09-09 16:59:08 +02002954endfunc
2955
Bram Moolenaar1bbb6192018-11-10 16:02:01 +01002956func Test_normal49_counts()
Bram Moolenaar2931f2a2016-09-09 16:59:08 +02002957 new
2958 call setline(1, 'one two three four five six seven eight nine ten')
2959 1
2960 norm! 3d2w
2961 call assert_equal('seven eight nine ten', getline(1))
2962 bw!
2963endfunc
2964
Bram Moolenaar1bbb6192018-11-10 16:02:01 +01002965func Test_normal50_commandline()
Bram Moolenaar004a6782020-04-11 17:09:31 +02002966 CheckFeature timers
2967 CheckFeature cmdline_hist
Bram Moolenaar2931f2a2016-09-09 16:59:08 +02002968 func! DoTimerWork(id)
2969 call assert_equal('[Command Line]', bufname(''))
2970 " should fail, with E11, but does fail with E23?
2971 "call feedkeys("\<c-^>", 'tm')
2972
2973 " should also fail with E11
Bram Moolenaare2e40752020-09-04 21:18:46 +02002974 call assert_fails(":wincmd p", 'E11:')
Bram Moolenaar2931f2a2016-09-09 16:59:08 +02002975 " return from commandline window
2976 call feedkeys("\<cr>")
2977 endfunc
2978
2979 let oldlang=v:lang
2980 lang C
2981 set updatetime=20
2982 call timer_start(100, 'DoTimerWork')
2983 try
2984 " throws E23, for whatever reason...
2985 call feedkeys('q:', 'x!')
2986 catch /E23/
2987 " no-op
2988 endtry
2989 " clean up
2990 set updatetime=4000
2991 exe "lang" oldlang
2992 bw!
2993endfunc
2994
Bram Moolenaar1bbb6192018-11-10 16:02:01 +01002995func Test_normal51_FileChangedRO()
Bram Moolenaar004a6782020-04-11 17:09:31 +02002996 CheckFeature autocmd
Bram Moolenaare5f2a072017-02-01 22:31:49 +01002997 " Don't sleep after the warning message.
2998 call test_settime(1)
Bram Moolenaar2931f2a2016-09-09 16:59:08 +02002999 call writefile(['foo'], 'Xreadonly.log')
3000 new Xreadonly.log
3001 setl ro
3002 au FileChangedRO <buffer> :call feedkeys("\<c-^>", 'tix')
Bram Moolenaare2e40752020-09-04 21:18:46 +02003003 call assert_fails(":norm! Af", 'E788:')
Bram Moolenaar2931f2a2016-09-09 16:59:08 +02003004 call assert_equal(['foo'], getline(1,'$'))
3005 call assert_equal('Xreadonly.log', bufname(''))
3006
3007 " cleanup
Bram Moolenaare5f2a072017-02-01 22:31:49 +01003008 call test_settime(0)
Bram Moolenaar2931f2a2016-09-09 16:59:08 +02003009 bw!
3010 call delete("Xreadonly.log")
3011endfunc
3012
Bram Moolenaar1bbb6192018-11-10 16:02:01 +01003013func Test_normal52_rl()
Bram Moolenaar004a6782020-04-11 17:09:31 +02003014 CheckFeature rightleft
Bram Moolenaar2931f2a2016-09-09 16:59:08 +02003015 new
3016 call setline(1, 'abcde fghij klmnopq')
3017 norm! 1gg$
3018 set rl
3019 call assert_equal(19, col('.'))
3020 call feedkeys('l', 'tx')
3021 call assert_equal(18, col('.'))
3022 call feedkeys('h', 'tx')
3023 call assert_equal(19, col('.'))
3024 call feedkeys("\<right>", 'tx')
3025 call assert_equal(18, col('.'))
Bram Moolenaar1671f442020-03-10 07:48:13 +01003026 call feedkeys("\<left>", 'tx')
3027 call assert_equal(19, col('.'))
Bram Moolenaar2931f2a2016-09-09 16:59:08 +02003028 call feedkeys("\<s-right>", 'tx')
3029 call assert_equal(13, col('.'))
3030 call feedkeys("\<c-right>", 'tx')
3031 call assert_equal(7, col('.'))
3032 call feedkeys("\<c-left>", 'tx')
3033 call assert_equal(13, col('.'))
3034 call feedkeys("\<s-left>", 'tx')
3035 call assert_equal(19, col('.'))
3036 call feedkeys("<<", 'tx')
3037 call assert_equal(' abcde fghij klmnopq',getline(1))
3038 call feedkeys(">>", 'tx')
3039 call assert_equal('abcde fghij klmnopq',getline(1))
3040
3041 " cleanup
3042 set norl
3043 bw!
3044endfunc
3045
Bram Moolenaarb1e04fc2017-03-29 13:08:35 +02003046func Test_normal54_Ctrl_bsl()
3047 new
3048 call setline(1, 'abcdefghijklmn')
3049 exe "norm! df\<c-\>\<c-n>"
3050 call assert_equal(['abcdefghijklmn'], getline(1,'$'))
3051 exe "norm! df\<c-\>\<c-g>"
3052 call assert_equal(['abcdefghijklmn'], getline(1,'$'))
3053 exe "norm! df\<c-\>m"
3054 call assert_equal(['abcdefghijklmn'], getline(1,'$'))
Bram Moolenaar30276f22019-01-24 17:59:39 +01003055
Bram Moolenaarb1e04fc2017-03-29 13:08:35 +02003056 call setline(2, 'abcdefghijklmnāf')
3057 norm! 2gg0
3058 exe "norm! df\<Char-0x101>"
3059 call assert_equal(['abcdefghijklmn', 'f'], getline(1,'$'))
3060 norm! 1gg0
3061 exe "norm! df\<esc>"
3062 call assert_equal(['abcdefghijklmn', 'f'], getline(1,'$'))
Bram Moolenaar2931f2a2016-09-09 16:59:08 +02003063
Bram Moolenaarb1e04fc2017-03-29 13:08:35 +02003064 " clean up
3065 bw!
3066endfunc
3067
3068func Test_normal_large_count()
3069 " This may fail with 32bit long, how do we detect that?
3070 new
3071 normal o
3072 normal 6666666666dL
3073 bwipe!
Bram Moolenaar2931f2a2016-09-09 16:59:08 +02003074endfunc
Bram Moolenaarbf3d5802017-03-29 19:48:11 +02003075
3076func Test_delete_until_paragraph()
Bram Moolenaarbf3d5802017-03-29 19:48:11 +02003077 new
3078 normal grádv}
3079 call assert_equal('á', getline(1))
3080 normal grád}
3081 call assert_equal('', getline(1))
3082 bwipe!
3083endfunc
Bram Moolenaarfb094e12017-11-05 20:59:28 +01003084
3085" Test for the gr (virtual replace) command
3086" Test for the bug fixed by 7.4.387
3087func Test_gr_command()
3088 enew!
3089 let save_cpo = &cpo
3090 call append(0, ['First line', 'Second line', 'Third line'])
3091 exe "normal i\<C-G>u"
3092 call cursor(2, 1)
3093 set cpo-=X
3094 normal 4gro
3095 call assert_equal('oooond line', getline(2))
3096 undo
3097 set cpo+=X
3098 normal 4gro
3099 call assert_equal('ooooecond line', getline(2))
3100 let &cpo = save_cpo
Bram Moolenaarf5f1e102020-03-08 05:13:15 +01003101 normal! ggvegrx
3102 call assert_equal('xxxxx line', getline(1))
3103 exe "normal! gggr\<C-V>122"
3104 call assert_equal('zxxxx line', getline(1))
3105 set virtualedit=all
3106 normal! 15|grl
3107 call assert_equal('zxxxx line l', getline(1))
3108 set virtualedit&
3109 set nomodifiable
3110 call assert_fails('normal! grx', 'E21:')
3111 call assert_fails('normal! gRx', 'E21:')
3112 set modifiable&
Bram Moolenaarfb094e12017-11-05 20:59:28 +01003113 enew!
3114endfunc
3115
3116" When splitting a window the changelist position is wrong.
3117" Test the changelist position after splitting a window.
3118" Test for the bug fixed by 7.4.386
3119func Test_changelist()
3120 let save_ul = &ul
3121 enew!
3122 call append('$', ['1', '2'])
3123 exe "normal i\<C-G>u"
3124 exe "normal Gkylpa\<C-G>u"
3125 set ul=100
3126 exe "normal Gylpa\<C-G>u"
3127 set ul=100
3128 normal gg
3129 vsplit
3130 normal g;
3131 call assert_equal([3, 2], [line('.'), col('.')])
3132 normal g;
3133 call assert_equal([2, 2], [line('.'), col('.')])
3134 call assert_fails('normal g;', 'E662:')
Bram Moolenaar1671f442020-03-10 07:48:13 +01003135 new
3136 call assert_fails('normal g;', 'E664:')
Bram Moolenaarfb094e12017-11-05 20:59:28 +01003137 %bwipe!
3138 let &ul = save_ul
3139endfunc
Bram Moolenaar1bbb6192018-11-10 16:02:01 +01003140
3141func Test_nv_hat_count()
3142 %bwipeout!
3143 let l:nr = bufnr('%') + 1
Bram Moolenaare2e40752020-09-04 21:18:46 +02003144 call assert_fails(':execute "normal! ' . l:nr . '\<C-^>"', 'E92:')
Bram Moolenaar1bbb6192018-11-10 16:02:01 +01003145
3146 edit Xfoo
3147 let l:foo_nr = bufnr('Xfoo')
3148
3149 edit Xbar
3150 let l:bar_nr = bufnr('Xbar')
3151
3152 " Make sure we are not just using the alternate file.
3153 edit Xbaz
3154
3155 call feedkeys(l:foo_nr . "\<C-^>", 'tx')
3156 call assert_equal('Xfoo', fnamemodify(bufname('%'), ':t'))
3157
3158 call feedkeys(l:bar_nr . "\<C-^>", 'tx')
3159 call assert_equal('Xbar', fnamemodify(bufname('%'), ':t'))
3160
3161 %bwipeout!
3162endfunc
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01003163
3164func Test_message_when_using_ctrl_c()
Bram Moolenaar553e5a52019-03-25 23:16:34 +01003165 " Make sure no buffers are changed.
3166 %bwipe!
3167
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01003168 exe "normal \<C-C>"
3169 call assert_match("Type :qa and press <Enter> to exit Vim", Screenline(&lines))
Bram Moolenaar553e5a52019-03-25 23:16:34 +01003170
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01003171 new
3172 cal setline(1, 'hi!')
3173 exe "normal \<C-C>"
3174 call assert_match("Type :qa! and press <Enter> to abandon all changes and exit Vim", Screenline(&lines))
Bram Moolenaar553e5a52019-03-25 23:16:34 +01003175
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01003176 bwipe!
3177endfunc
Bram Moolenaarc6b37db2019-04-27 18:00:34 +02003178
3179" Test for '[m', ']m', '[M' and ']M'
3180" Jumping to beginning and end of methods in Java-like languages
3181func Test_java_motion()
3182 new
Bram Moolenaar1671f442020-03-10 07:48:13 +01003183 call assert_beeps('normal! [m')
3184 call assert_beeps('normal! ]m')
3185 call assert_beeps('normal! [M')
3186 call assert_beeps('normal! ]M')
Bram Moolenaar8a9bc952020-10-02 18:48:07 +02003187 let lines =<< trim [CODE]
3188 Piece of Java
3189 {
3190 tt m1 {
3191 t1;
3192 } e1
Bram Moolenaarc6b37db2019-04-27 18:00:34 +02003193
Bram Moolenaar8a9bc952020-10-02 18:48:07 +02003194 tt m2 {
3195 t2;
3196 } e2
Bram Moolenaarc6b37db2019-04-27 18:00:34 +02003197
Bram Moolenaar8a9bc952020-10-02 18:48:07 +02003198 tt m3 {
3199 if (x)
3200 {
3201 t3;
3202 }
3203 } e3
3204 }
3205 [CODE]
3206 call setline(1, lines)
Bram Moolenaarc6b37db2019-04-27 18:00:34 +02003207
3208 normal gg
3209
3210 normal 2]maA
3211 call assert_equal("\ttt m1 {A", getline('.'))
3212 call assert_equal([3, 9, 16], [line('.'), col('.'), virtcol('.')])
3213
3214 normal j]maB
3215 call assert_equal("\ttt m2 {B", getline('.'))
3216 call assert_equal([7, 9, 16], [line('.'), col('.'), virtcol('.')])
3217
3218 normal ]maC
3219 call assert_equal("\ttt m3 {C", getline('.'))
3220 call assert_equal([11, 9, 16], [line('.'), col('.'), virtcol('.')])
3221
3222 normal [maD
3223 call assert_equal("\ttt m3 {DC", getline('.'))
3224 call assert_equal([11, 9, 16], [line('.'), col('.'), virtcol('.')])
3225
3226 normal k2[maE
3227 call assert_equal("\ttt m1 {EA", getline('.'))
3228 call assert_equal([3, 9, 16], [line('.'), col('.'), virtcol('.')])
3229
3230 normal 3[maF
3231 call assert_equal("{F", getline('.'))
3232 call assert_equal([2, 2, 2], [line('.'), col('.'), virtcol('.')])
3233
3234 normal ]MaG
3235 call assert_equal("\t}G e1", getline('.'))
3236 call assert_equal([5, 3, 10], [line('.'), col('.'), virtcol('.')])
3237
3238 normal j2]MaH
3239 call assert_equal("\t}H e3", getline('.'))
3240 call assert_equal([16, 3, 10], [line('.'), col('.'), virtcol('.')])
3241
3242 normal ]M]M
3243 normal aI
3244 call assert_equal("}I", getline('.'))
3245 call assert_equal([17, 2, 2], [line('.'), col('.'), virtcol('.')])
3246
3247 normal 2[MaJ
3248 call assert_equal("\t}JH e3", getline('.'))
3249 call assert_equal([16, 3, 10], [line('.'), col('.'), virtcol('.')])
3250
3251 normal k[MaK
3252 call assert_equal("\t}K e2", getline('.'))
3253 call assert_equal([9, 3, 10], [line('.'), col('.'), virtcol('.')])
3254
3255 normal 3[MaL
3256 call assert_equal("{LF", getline('.'))
3257 call assert_equal([2, 2, 2], [line('.'), col('.'), virtcol('.')])
3258
Bram Moolenaar8a9bc952020-10-02 18:48:07 +02003259 call cursor(2, 1)
3260 call assert_beeps('norm! 5]m')
3261
3262 " jumping to a method in a fold should open the fold
3263 6,10fold
3264 call feedkeys("gg3]m", 'xt')
3265 call assert_equal([7, 8, 15], [line('.'), col('.'), virtcol('.')])
3266 call assert_equal(-1, foldclosedend(7))
3267
Bram Moolenaarc6b37db2019-04-27 18:00:34 +02003268 close!
3269endfunc
Bram Moolenaard5c82342019-07-27 18:44:57 +02003270
Bram Moolenaar004a6782020-04-11 17:09:31 +02003271" Tests for g cmds
Bram Moolenaar1671f442020-03-10 07:48:13 +01003272func Test_normal_gdollar_cmd()
Bram Moolenaar004a6782020-04-11 17:09:31 +02003273 CheckFeature jumplist
Bram Moolenaard5c82342019-07-27 18:44:57 +02003274 call Setup_NewWindow()
3275 " Make long lines that will wrap
3276 %s/$/\=repeat(' foobar', 10)/
3277 20vsp
3278 set wrap
3279 " Test for g$ with count
3280 norm! gg
3281 norm! 0vg$y
3282 call assert_equal(20, col("'>"))
3283 call assert_equal('1 foobar foobar foob', getreg(0))
3284 norm! gg
3285 norm! 0v4g$y
3286 call assert_equal(72, col("'>"))
3287 call assert_equal('1 foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar'.."\n", getreg(0))
3288 norm! gg
3289 norm! 0v6g$y
3290 call assert_equal(40, col("'>"))
3291 call assert_equal('1 foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar'.. "\n"..
3292 \ '2 foobar foobar foobar foobar foobar foo', getreg(0))
3293 set nowrap
3294 " clean up
3295 norm! gg
3296 norm! 0vg$y
3297 call assert_equal(20, col("'>"))
3298 call assert_equal('1 foobar foobar foob', getreg(0))
3299 norm! gg
3300 norm! 0v4g$y
3301 call assert_equal(20, col("'>"))
3302 call assert_equal('1 foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar'.. "\n"..
3303 \ '2 foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar'.. "\n"..
3304 \ '3 foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar'.. "\n"..
3305 \ '4 foobar foobar foob', getreg(0))
3306 norm! gg
3307 norm! 0v6g$y
3308 call assert_equal(20, col("'>"))
3309 call assert_equal('1 foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar'.. "\n"..
3310 \ '2 foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar'.. "\n"..
3311 \ '3 foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar'.. "\n"..
3312 \ '4 foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar'.. "\n"..
3313 \ '5 foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar'.. "\n"..
3314 \ '6 foobar foobar foob', getreg(0))
3315 " Move to last line, also down movement is not possible, should still move
3316 " the cursor to the last visible char
3317 norm! G
3318 norm! 0v6g$y
3319 call assert_equal(20, col("'>"))
3320 call assert_equal('100 foobar foobar fo', getreg(0))
3321 bw!
3322endfunc
Bram Moolenaar03ac52f2019-09-24 22:47:46 +02003323
Bram Moolenaarf5f1e102020-03-08 05:13:15 +01003324func Test_normal_gk_gj()
Bram Moolenaar03ac52f2019-09-24 22:47:46 +02003325 " needs 80 column new window
3326 new
3327 vert 80new
Bram Moolenaarf5f1e102020-03-08 05:13:15 +01003328 call assert_beeps('normal gk')
Bram Moolenaar03ac52f2019-09-24 22:47:46 +02003329 put =[repeat('x',90)..' {{{1', 'x {{{1']
3330 norm! gk
3331 " In a 80 column wide terminal the window will be only 78 char
3332 " (because Vim will leave space for the other window),
3333 " but if the terminal is larger, it will be 80 chars, so verify the
3334 " cursor column correctly.
3335 call assert_equal(winwidth(0)+1, col('.'))
3336 call assert_equal(winwidth(0)+1, virtcol('.'))
3337 norm! j
3338 call assert_equal(6, col('.'))
3339 call assert_equal(6, virtcol('.'))
3340 norm! gk
3341 call assert_equal(95, col('.'))
3342 call assert_equal(95, virtcol('.'))
Bram Moolenaarf5f1e102020-03-08 05:13:15 +01003343 %bw!
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02003344
3345 " needs 80 column new window
3346 new
3347 vert 80new
Bram Moolenaarf5f1e102020-03-08 05:13:15 +01003348 call assert_beeps('normal gj')
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02003349 set number
3350 set numberwidth=10
3351 set cpoptions+=n
3352 put =[repeat('0',90), repeat('1',90)]
3353 norm! 075l
3354 call assert_equal(76, col('.'))
3355 norm! gk
3356 call assert_equal(1, col('.'))
3357 norm! gk
3358 call assert_equal(76, col('.'))
3359 norm! gk
3360 call assert_equal(1, col('.'))
3361 norm! gj
3362 call assert_equal(76, col('.'))
3363 norm! gj
3364 call assert_equal(1, col('.'))
3365 norm! gj
3366 call assert_equal(76, col('.'))
Bram Moolenaarf5f1e102020-03-08 05:13:15 +01003367 " When 'nowrap' is set, gk and gj behave like k and j
3368 set nowrap
3369 normal! gk
3370 call assert_equal([2, 76], [line('.'), col('.')])
3371 normal! gj
3372 call assert_equal([3, 76], [line('.'), col('.')])
3373 %bw!
3374 set cpoptions& number& numberwidth& wrap&
Bram Moolenaar03ac52f2019-09-24 22:47:46 +02003375endfunc
Bram Moolenaarf0cee192020-02-16 13:33:56 +01003376
Bram Moolenaar818fc9a2020-02-21 17:54:45 +01003377" Test for using : to run a multi-line Ex command in operator pending mode
3378func Test_normal_yank_with_excmd()
3379 new
3380 call setline(1, ['foo', 'bar', 'baz'])
3381 let @a = ''
3382 call feedkeys("\"ay:if v:true\<CR>normal l\<CR>endif\<CR>", 'xt')
3383 call assert_equal('f', @a)
3384 close!
3385endfunc
Bram Moolenaarf5f1e102020-03-08 05:13:15 +01003386
3387" Test for supplying a count to a normal-mode command across a cursorhold call
3388func Test_normal_cursorhold_with_count()
3389 func s:cHold()
3390 let g:cHold_Called += 1
3391 endfunc
3392 new
3393 augroup normalcHoldTest
3394 au!
3395 au CursorHold <buffer> call s:cHold()
3396 augroup END
3397 let g:cHold_Called = 0
3398 call feedkeys("3\<CursorHold>2ix", 'xt')
3399 call assert_equal(1, g:cHold_Called)
3400 call assert_equal(repeat('x', 32), getline(1))
3401 augroup normalcHoldTest
3402 au!
3403 augroup END
3404 au! normalcHoldTest
3405 close!
3406 delfunc s:cHold
3407endfunc
3408
3409" Test for using a count and a command with CTRL-W
3410func Test_wincmd_with_count()
3411 call feedkeys("\<C-W>12n", 'xt')
3412 call assert_equal(12, winheight(0))
3413endfunc
3414
3415" Test for 'b', 'B' 'ge' and 'gE' commands
Bram Moolenaar1671f442020-03-10 07:48:13 +01003416func Test_horiz_motion()
3417 new
Bram Moolenaarf5f1e102020-03-08 05:13:15 +01003418 normal! gg
3419 call assert_beeps('normal! b')
3420 call assert_beeps('normal! B')
3421 call assert_beeps('normal! gE')
3422 call assert_beeps('normal! ge')
Bram Moolenaar1671f442020-03-10 07:48:13 +01003423 " <S-Backspace> moves one word left and <C-Backspace> moves one WORD left
3424 call setline(1, 'one ,two ,three')
3425 exe "normal! $\<S-BS>"
3426 call assert_equal(11, col('.'))
3427 exe "normal! $\<C-BS>"
3428 call assert_equal(10, col('.'))
3429 close!
3430endfunc
3431
3432" Test for using a : command in operator pending mode
3433func Test_normal_colon_op()
3434 new
3435 call setline(1, ['one', 'two'])
3436 call assert_beeps("normal! Gc:d\<CR>")
3437 close!
Bram Moolenaarf5f1e102020-03-08 05:13:15 +01003438endfunc
3439
Bram Moolenaar004a6782020-04-11 17:09:31 +02003440" Test for d and D commands
3441func Test_normal_delete_cmd()
3442 new
3443 " D in an empty line
3444 call setline(1, '')
3445 normal D
3446 call assert_equal('', getline(1))
3447 " D in an empty line in virtualedit mode
3448 set virtualedit=all
3449 normal D
3450 call assert_equal('', getline(1))
3451 set virtualedit&
3452 " delete to a readonly register
3453 call setline(1, ['abcd'])
3454 call assert_beeps('normal ":d2l')
Bram Moolenaar6fd367a2021-03-13 13:14:04 +01003455
3456 " D and d with 'nomodifiable'
3457 call setline(1, ['abcd'])
3458 setlocal nomodifiable
3459 call assert_fails('normal D', 'E21:')
3460 call assert_fails('normal d$', 'E21:')
3461
Bram Moolenaar004a6782020-04-11 17:09:31 +02003462 close!
3463endfunc
3464
Bram Moolenaar8a9bc952020-10-02 18:48:07 +02003465" Test for deleting or changing characters across lines with 'whichwrap'
3466" containing 's'. Should count <EOL> as one character.
3467func Test_normal_op_across_lines()
3468 new
3469 set whichwrap&
3470 call setline(1, ['one two', 'three four'])
3471 exe "norm! $3d\<Space>"
3472 call assert_equal(['one twhree four'], getline(1, '$'))
3473
3474 call setline(1, ['one two', 'three four'])
3475 exe "norm! $3c\<Space>x"
3476 call assert_equal(['one twxhree four'], getline(1, '$'))
3477
3478 set whichwrap+=l
3479 call setline(1, ['one two', 'three four'])
3480 exe "norm! $3x"
3481 call assert_equal(['one twhree four'], getline(1, '$'))
3482 close!
3483 set whichwrap&
3484endfunc
3485
Bram Moolenaar224a5f12020-04-28 20:29:07 +02003486" Test for 'w' and 'b' commands
3487func Test_normal_word_move()
3488 new
3489 call setline(1, ['foo bar a', '', 'foo bar b'])
3490 " copy a single character word at the end of a line
3491 normal 1G$yw
3492 call assert_equal('a', @")
3493 " copy a single character word at the end of a file
3494 normal G$yw
3495 call assert_equal('b', @")
3496 " check for a word movement handling an empty line properly
3497 normal 1G$vwy
3498 call assert_equal("a\n\n", @")
3499
3500 " copy using 'b' command
3501 %d
3502 " non-empty blank line at the start of file
3503 call setline(1, [' ', 'foo bar'])
3504 normal 2Gyb
3505 call assert_equal(" \n", @")
3506 " try to copy backwards from the start of the file
3507 call setline(1, ['one two', 'foo bar'])
3508 call assert_beeps('normal ggyb')
3509 " 'b' command should stop at an empty line
3510 call setline(1, ['one two', '', 'foo bar'])
3511 normal 3Gyb
3512 call assert_equal("\n", @")
3513 normal 3Gy2b
3514 call assert_equal("two\n", @")
3515 " 'b' command should not stop at a non-empty blank line
3516 call setline(1, ['one two', ' ', 'foo bar'])
3517 normal 3Gyb
3518 call assert_equal("two\n ", @")
3519
3520 close!
3521endfunc
3522
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02003523" Test for 'scrolloff' with a long line that doesn't fit in the screen
3524func Test_normal_scroloff()
3525 10new
3526 80vnew
3527 call setline(1, repeat('a', 1000))
3528 set scrolloff=10
3529 normal gg10gj
3530 call assert_equal(8, winline())
3531 normal 10gj
3532 call assert_equal(10, winline())
3533 normal 10gk
3534 call assert_equal(3, winline())
3535 set scrolloff&
3536 close!
3537endfunc
3538
3539" Test for vertical scrolling with CTRL-F and CTRL-B with a long line
3540func Test_normal_vert_scroll_longline()
3541 10new
3542 80vnew
3543 call setline(1, range(1, 10))
3544 call append(5, repeat('a', 1000))
3545 exe "normal gg\<C-F>"
3546 call assert_equal(6, line('.'))
3547 exe "normal \<C-F>\<C-F>"
3548 call assert_equal(11, line('.'))
3549 call assert_equal(1, winline())
3550 exe "normal \<C-B>"
3551 call assert_equal(10, line('.'))
3552 call assert_equal(3, winline())
3553 exe "normal \<C-B>\<C-B>"
3554 call assert_equal(5, line('.'))
3555 call assert_equal(5, winline())
3556 close!
3557endfunc
3558
Bram Moolenaar8a9bc952020-10-02 18:48:07 +02003559" Test for jumping in a file using %
3560func Test_normal_percent_jump()
3561 new
3562 call setline(1, range(1, 100))
3563
3564 " jumping to a folded line should open the fold
3565 25,75fold
3566 call feedkeys('50%', 'xt')
3567 call assert_equal(50, line('.'))
3568 call assert_equal(-1, foldclosedend(50))
3569 close!
3570endfunc
3571
Bram Moolenaar3e72dca2021-05-29 16:30:12 +02003572" Test for << and >> commands to shift text by 'shiftwidth'
3573func Test_normal_shift_rightleft()
3574 new
3575 call setline(1, ['one', '', "\t", ' two', "\tthree", ' four'])
3576 set shiftwidth=2 tabstop=8
3577 normal gg6>>
3578 call assert_equal([' one', '', "\t ", ' two', "\t three", "\tfour"],
3579 \ getline(1, '$'))
3580 normal ggVG2>>
3581 call assert_equal([' one', '', "\t ", "\ttwo",
3582 \ "\t three", "\t four"], getline(1, '$'))
3583 normal gg6<<
3584 call assert_equal([' one', '', "\t ", ' two', "\t three",
3585 \ "\t four"], getline(1, '$'))
3586 normal ggVG2<<
3587 call assert_equal(['one', '', "\t", ' two', "\tthree", ' four'],
3588 \ getline(1, '$'))
3589 set shiftwidth& tabstop&
3590 bw!
3591endfunc
3592
Yegappan Lakshmanan2ac71842021-05-31 19:23:01 +02003593" Some commands like yy, cc, dd, >>, << and !! accept a count after
3594" typing the first letter of the command.
3595func Test_normal_count_after_operator()
3596 new
3597 setlocal shiftwidth=4 tabstop=8 autoindent
3598 call setline(1, ['one', 'two', 'three', 'four', 'five'])
3599 let @a = ''
3600 normal! j"ay4y
3601 call assert_equal("two\nthree\nfour\nfive\n", @a)
3602 normal! 3G>2>
3603 call assert_equal(['one', 'two', ' three', ' four', 'five'],
3604 \ getline(1, '$'))
3605 exe "normal! 3G0c2cred\nblue"
3606 call assert_equal(['one', 'two', ' red', ' blue', 'five'],
3607 \ getline(1, '$'))
3608 exe "normal! gg<8<"
3609 call assert_equal(['one', 'two', 'red', 'blue', 'five'],
3610 \ getline(1, '$'))
3611 exe "normal! ggd3d"
3612 call assert_equal(['blue', 'five'], getline(1, '$'))
3613 call setline(1, range(1, 4))
3614 call feedkeys("gg!3!\<C-B>\"\<CR>", 'xt')
3615 call assert_equal('".,.+2!', @:)
3616 call feedkeys("gg!1!\<C-B>\"\<CR>", 'xt')
3617 call assert_equal('".!', @:)
3618 call feedkeys("gg!9!\<C-B>\"\<CR>", 'xt')
3619 call assert_equal('".,$!', @:)
3620 bw!
3621endfunc
3622
Christian Brabandtaaec1d42021-11-04 13:28:29 +00003623func Test_normal_gj_on_extra_wide_char()
3624 new | 25vsp
3625 let text='1 foooooooo ar e ins‍zwe1 foooooooo ins‍zwei' .
3626 \ ' i drei vier fünf sechs sieben acht un zehn elf zwöfl' .
3627 \ ' dreizehn v ierzehn fünfzehn'
3628 put =text
3629 call cursor(2,1)
3630 norm! gj
3631 call assert_equal([0,2,25,0], getpos('.'))
3632 bw!
3633endfunc
3634
Bram Moolenaar03725c52021-11-24 12:17:53 +00003635func Test_normal_count_out_of_range()
3636 new
3637 call setline(1, 'text')
3638 normal 44444444444|
3639 call assert_equal(999999999, v:count)
3640 normal 444444444444|
3641 call assert_equal(999999999, v:count)
3642 normal 4444444444444|
3643 call assert_equal(999999999, v:count)
3644 normal 4444444444444444444|
3645 call assert_equal(999999999, v:count)
3646
3647 normal 9y99999999|
3648 call assert_equal(899999991, v:count)
3649 normal 10y99999999|
3650 call assert_equal(999999999, v:count)
3651 normal 44444444444y44444444444|
3652 call assert_equal(999999999, v:count)
3653 bwipe!
3654endfunc
3655
Bram Moolenaarf5f1e102020-03-08 05:13:15 +01003656" vim: shiftwidth=2 sts=2 expandtab