blob: 8383dde395919d8b0962e9d8ca2a84a2b6800724 [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
447 func MyopFunc(val, type)
448 let g:OpFuncArgs = [a:val, a:type]
449 endfunc
450
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000451 let lines =<< trim END
452 #" Test for using a function()
453 set opfunc=function('g:MyopFunc',\ [10])
454 LET g:OpFuncArgs = []
455 normal! g@l
456 call assert_equal([10, 'char'], g:OpFuncArgs)
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +0000457
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000458 #" Using a funcref variable to set 'operatorfunc'
459 VAR Fn = function('g:MyopFunc', [11])
460 LET &opfunc = Fn
461 LET g:OpFuncArgs = []
462 normal! g@l
463 call assert_equal([11, 'char'], g:OpFuncArgs)
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +0000464
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000465 #" Using a string(funcref_variable) to set 'operatorfunc'
466 LET Fn = function('g:MyopFunc', [12])
467 LET &operatorfunc = string(Fn)
468 LET g:OpFuncArgs = []
469 normal! g@l
470 call assert_equal([12, 'char'], g:OpFuncArgs)
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +0000471
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000472 #" Test for using a funcref()
473 set operatorfunc=funcref('g:MyopFunc',\ [13])
474 LET g:OpFuncArgs = []
475 normal! g@l
476 call assert_equal([13, 'char'], g:OpFuncArgs)
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +0000477
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000478 #" Using a funcref variable to set 'operatorfunc'
479 LET Fn = funcref('g:MyopFunc', [14])
480 LET &opfunc = Fn
481 LET g:OpFuncArgs = []
482 normal! g@l
483 call assert_equal([14, 'char'], g:OpFuncArgs)
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +0000484
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000485 #" Using a string(funcref_variable) to set 'operatorfunc'
486 LET Fn = funcref('g:MyopFunc', [15])
487 LET &opfunc = string(Fn)
488 LET g:OpFuncArgs = []
489 normal! g@l
490 call assert_equal([15, 'char'], g:OpFuncArgs)
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +0000491
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000492 #" Test for using a lambda function using set
493 VAR optval = "LSTART a LMIDDLE MyopFunc(16, a) LEND"
494 LET optval = substitute(optval, ' ', '\\ ', 'g')
495 exe "set opfunc=" .. optval
496 LET g:OpFuncArgs = []
497 normal! g@l
498 call assert_equal([16, 'char'], g:OpFuncArgs)
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +0000499
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000500 #" Test for using a lambda function using LET
501 LET &opfunc = LSTART a LMIDDLE MyopFunc(17, a) LEND
502 LET g:OpFuncArgs = []
503 normal! g@l
504 call assert_equal([17, 'char'], g:OpFuncArgs)
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +0000505
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000506 #" Set 'operatorfunc' to a string(lambda expression)
507 LET &opfunc = 'LSTART a LMIDDLE MyopFunc(18, a) LEND'
508 LET g:OpFuncArgs = []
509 normal! g@l
510 call assert_equal([18, 'char'], g:OpFuncArgs)
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +0000511
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000512 #" Set 'operatorfunc' to a variable with a lambda expression
513 VAR Lambda = LSTART a LMIDDLE MyopFunc(19, a) LEND
514 LET &opfunc = Lambda
515 LET g:OpFuncArgs = []
516 normal! g@l
517 call assert_equal([19, 'char'], g:OpFuncArgs)
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +0000518
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000519 #" Set 'operatorfunc' to a string(variable with a lambda expression)
520 LET Lambda = LSTART a LMIDDLE MyopFunc(20, a) LEND
521 LET &opfunc = string(Lambda)
522 LET g:OpFuncArgs = []
523 normal! g@l
524 call assert_equal([20, 'char'], g:OpFuncArgs)
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +0000525
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000526 #" Try to use 'operatorfunc' after the function is deleted
527 func g:TmpOpFunc(type)
528 LET g:OpFuncArgs = [21, a:type]
529 endfunc
530 LET &opfunc = function('g:TmpOpFunc')
531 delfunc g:TmpOpFunc
532 call test_garbagecollect_now()
533 LET g:OpFuncArgs = []
534 call assert_fails('normal! g@l', 'E117:')
535 call assert_equal([], g:OpFuncArgs)
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +0000536
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000537 #" Try to use a function with two arguments for 'operatorfunc'
538 func MyopFunc2(x, y)
539 LET g:OpFuncArgs = [a:x, a:y]
540 endfunc
541 set opfunc=MyopFunc2
542 LET g:OpFuncArgs = []
543 call assert_fails('normal! g@l', 'E119:')
544 call assert_equal([], g:OpFuncArgs)
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +0000545
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000546 #" Try to use a lambda function with two arguments for 'operatorfunc'
547 LET &opfunc = LSTART a, b LMIDDLE MyopFunc(22, b) LEND
548 LET g:OpFuncArgs = []
549 call assert_fails('normal! g@l', 'E119:')
550 call assert_equal([], g:OpFuncArgs)
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +0000551
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000552 #" Test for clearing the 'operatorfunc' option
553 set opfunc=''
554 set opfunc&
555 call assert_fails("set opfunc=function('abc')", "E700:")
556 call assert_fails("set opfunc=funcref('abc')", "E700:")
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +0000557
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000558 #" set 'operatorfunc' to a non-existing function
559 LET &opfunc = function('g:MyopFunc', [23])
560 call assert_fails("set opfunc=function('NonExistingFunc')", 'E700:')
561 call assert_fails("LET &opfunc = function('NonExistingFunc')", 'E700:')
562 LET g:OpFuncArgs = []
563 normal! g@l
564 call assert_equal([23, 'char'], g:OpFuncArgs)
565 END
566 call CheckTransLegacySuccess(lines)
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +0000567
568 " Using Vim9 lambda expression in legacy context should fail
569 set opfunc=(a)\ =>\ MyopFunc(24,\ a)
570 let g:OpFuncArgs = []
571 call assert_fails('normal! g@l', 'E117:')
572 call assert_equal([], g:OpFuncArgs)
573
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000574 " set 'operatorfunc' to a partial with dict. This used to cause a crash.
575 func SetOpFunc()
576 let operator = {'execute': function('OperatorExecute')}
577 let &opfunc = operator.execute
578 endfunc
579 func OperatorExecute(_) dict
580 endfunc
581 call SetOpFunc()
582 call test_garbagecollect_now()
583 set operatorfunc=
584 delfunc SetOpFunc
585 delfunc OperatorExecute
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +0000586
587 " Vim9 tests
588 let lines =<< trim END
589 vim9script
590
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000591 # Test for using a def function with opfunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +0000592 def g:Vim9opFunc(val: number, type: string): void
593 g:OpFuncArgs = [val, type]
594 enddef
595 set opfunc=function('g:Vim9opFunc',\ [60])
596 g:OpFuncArgs = []
597 normal! g@l
598 assert_equal([60, 'char'], g:OpFuncArgs)
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +0000599 END
600 call CheckScriptSuccess(lines)
601
602 " cleanup
603 set opfunc&
604 delfunc MyopFunc
605 delfunc MyopFunc2
606 unlet g:OpFuncArgs
607 %bw!
608endfunc
609
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100610func Test_normal10_expand()
Bram Moolenaar87bc3f72016-09-03 17:33:54 +0200611 " Test for expand()
612 10new
613 call setline(1, ['1', 'ifooar,,cbar'])
614 2
615 norm! $
Bram Moolenaar65f08472017-09-10 18:16:20 +0200616 call assert_equal('cbar', expand('<cword>'))
617 call assert_equal('ifooar,,cbar', expand('<cWORD>'))
618
619 call setline(1, ['prx = list[idx];'])
620 1
621 let expected = ['', 'prx', 'prx', 'prx',
622 \ 'list', 'list', 'list', 'list', 'list', 'list', 'list',
623 \ 'idx', 'idx', 'idx', 'idx',
624 \ 'list[idx]',
625 \ '];',
626 \ ]
627 for i in range(1, 16)
628 exe 'norm ' . i . '|'
629 call assert_equal(expected[i], expand('<cexpr>'), 'i == ' . i)
630 endfor
631
Bram Moolenaard7e5e942020-10-07 16:54:52 +0200632 " Test for <cexpr> in state.val and ptr->val
633 call setline(1, 'x = state.val;')
634 call cursor(1, 10)
635 call assert_equal('state.val', expand('<cexpr>'))
636 call setline(1, 'x = ptr->val;')
637 call cursor(1, 9)
638 call assert_equal('ptr->val', expand('<cexpr>'))
639
Bram Moolenaarae6f8652017-12-20 22:32:20 +0100640 if executable('echo')
641 " Test expand(`...`) i.e. backticks command expansion.
Bram Moolenaar077ff432019-10-28 00:42:21 +0100642 call assert_equal('abcde', expand('`echo abcde`'))
Bram Moolenaarae6f8652017-12-20 22:32:20 +0100643 endif
644
645 " Test expand(`=...`) i.e. backticks expression expansion
646 call assert_equal('5', expand('`=2+3`'))
Bram Moolenaar8b633132020-03-20 18:20:51 +0100647 call assert_equal('3.14', expand('`=3.14`'))
Bram Moolenaarae6f8652017-12-20 22:32:20 +0100648
Bram Moolenaar87bc3f72016-09-03 17:33:54 +0200649 " clean up
650 bw!
Bram Moolenaar2931f2a2016-09-09 16:59:08 +0200651endfunc
Bram Moolenaar87bc3f72016-09-03 17:33:54 +0200652
Bram Moolenaard7e5e942020-10-07 16:54:52 +0200653" Test for expand() in latin1 encoding
654func Test_normal_expand_latin1()
655 new
656 let save_enc = &encoding
657 set encoding=latin1
658 call setline(1, 'val = item->color;')
659 call cursor(1, 11)
660 call assert_equal('color', expand("<cword>"))
661 call assert_equal('item->color', expand("<cexpr>"))
662 let &encoding = save_enc
663 bw!
664endfunc
665
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100666func Test_normal11_showcmd()
Bram Moolenaar87bc3f72016-09-03 17:33:54 +0200667 " test for 'showcmd'
668 10new
669 exe "norm! ofoobar\<esc>"
670 call assert_equal(2, line('$'))
671 set showcmd
672 exe "norm! ofoobar2\<esc>"
673 call assert_equal(3, line('$'))
674 exe "norm! VAfoobar3\<esc>"
675 call assert_equal(3, line('$'))
676 exe "norm! 0d3\<del>2l"
677 call assert_equal('obar2foobar3', getline('.'))
Bram Moolenaard1ad99b2020-10-04 16:16:54 +0200678 " test for the visual block size displayed in the status line
679 call setline(1, ['aaaaa', 'bbbbb', 'ccccc'])
680 call feedkeys("ggl\<C-V>lljj", 'xt')
681 redraw!
682 call assert_match('3x3$', Screenline(&lines))
683 call feedkeys("\<C-V>", 'xt')
684 " test for visually selecting a multi-byte character
685 call setline(1, ["\U2206"])
686 call feedkeys("ggv", 'xt')
687 redraw!
688 call assert_match('1-3$', Screenline(&lines))
689 call feedkeys("v", 'xt')
Bram Moolenaard7e5e942020-10-07 16:54:52 +0200690 " test for visually selecting the end of line
691 call setline(1, ["foobar"])
692 call feedkeys("$vl", 'xt')
693 redraw!
694 call assert_match('2$', Screenline(&lines))
695 call feedkeys("y", 'xt')
696 call assert_equal("r\n", @")
Bram Moolenaar87bc3f72016-09-03 17:33:54 +0200697 bw!
Bram Moolenaar2931f2a2016-09-09 16:59:08 +0200698endfunc
Bram Moolenaar87bc3f72016-09-03 17:33:54 +0200699
Bram Moolenaar1671f442020-03-10 07:48:13 +0100700" Test for nv_error and normal command errors
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100701func Test_normal12_nv_error()
Bram Moolenaar87bc3f72016-09-03 17:33:54 +0200702 10new
703 call setline(1, range(1,5))
704 " should not do anything, just beep
Bram Moolenaarf5f1e102020-03-08 05:13:15 +0100705 call assert_beeps('exe "norm! <c-k>"')
Bram Moolenaar87bc3f72016-09-03 17:33:54 +0200706 call assert_equal(map(range(1,5), 'string(v:val)'), getline(1,'$'))
Bram Moolenaarf5f1e102020-03-08 05:13:15 +0100707 call assert_beeps('normal! G2dd')
708 call assert_beeps("normal! g\<C-A>")
709 call assert_beeps("normal! g\<C-X>")
710 call assert_beeps("normal! g\<C-B>")
Bram Moolenaar1671f442020-03-10 07:48:13 +0100711 call assert_beeps("normal! vQ\<Esc>")
712 call assert_beeps("normal! 2[[")
713 call assert_beeps("normal! 2]]")
714 call assert_beeps("normal! 2[]")
715 call assert_beeps("normal! 2][")
716 call assert_beeps("normal! 4[z")
717 call assert_beeps("normal! 4]z")
718 call assert_beeps("normal! 4[c")
719 call assert_beeps("normal! 4]c")
720 call assert_beeps("normal! 200%")
721 call assert_beeps("normal! %")
722 call assert_beeps("normal! 2{")
723 call assert_beeps("normal! 2}")
724 call assert_beeps("normal! r\<Right>")
725 call assert_beeps("normal! 8ry")
726 call assert_beeps('normal! "@')
Bram Moolenaar87bc3f72016-09-03 17:33:54 +0200727 bw!
Bram Moolenaar2931f2a2016-09-09 16:59:08 +0200728endfunc
Bram Moolenaar87bc3f72016-09-03 17:33:54 +0200729
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100730func Test_normal13_help()
Bram Moolenaar87bc3f72016-09-03 17:33:54 +0200731 " Test for F1
732 call assert_equal(1, winnr())
733 call feedkeys("\<f1>", 'txi')
734 call assert_match('help\.txt', bufname('%'))
735 call assert_equal(2, winnr('$'))
736 bw!
Bram Moolenaar2931f2a2016-09-09 16:59:08 +0200737endfunc
Bram Moolenaar87bc3f72016-09-03 17:33:54 +0200738
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100739func Test_normal14_page()
Bram Moolenaar87bc3f72016-09-03 17:33:54 +0200740 " basic test for Ctrl-F and Ctrl-B
741 call Setup_NewWindow()
742 exe "norm! \<c-f>"
743 call assert_equal('9', getline('.'))
744 exe "norm! 2\<c-f>"
745 call assert_equal('25', getline('.'))
746 exe "norm! 2\<c-b>"
747 call assert_equal('18', getline('.'))
748 1
749 set scrolloff=5
750 exe "norm! 2\<c-f>"
751 call assert_equal('21', getline('.'))
752 exe "norm! \<c-b>"
753 call assert_equal('13', getline('.'))
754 1
755 set scrolloff=99
756 exe "norm! \<c-f>"
757 call assert_equal('13', getline('.'))
758 set scrolloff=0
759 100
760 exe "norm! $\<c-b>"
761 call assert_equal('92', getline('.'))
762 call assert_equal([0, 92, 1, 0, 1], getcurpos())
763 100
764 set nostartofline
765 exe "norm! $\<c-b>"
766 call assert_equal('92', getline('.'))
767 call assert_equal([0, 92, 2, 0, 2147483647], getcurpos())
768 " cleanup
769 set startofline
770 bw!
Bram Moolenaar2931f2a2016-09-09 16:59:08 +0200771endfunc
Bram Moolenaar87bc3f72016-09-03 17:33:54 +0200772
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100773func Test_normal14_page_eol()
Bram Moolenaarbc54f3f2016-09-04 14:34:28 +0200774 10new
775 norm oxxxxxxx
776 exe "norm 2\<c-f>"
777 " check with valgrind that cursor is put back in column 1
778 exe "norm 2\<c-b>"
779 bw!
780endfunc
781
Bram Moolenaar1671f442020-03-10 07:48:13 +0100782" Test for errors with z command
783func Test_normal_z_error()
784 call assert_beeps('normal! z2p')
Christian Brabandt2fa93842021-05-30 22:17:25 +0200785 call assert_beeps('normal! zq')
Bram Moolenaar1671f442020-03-10 07:48:13 +0100786endfunc
787
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100788func Test_normal15_z_scroll_vert()
Bram Moolenaar87bc3f72016-09-03 17:33:54 +0200789 " basic test for z commands that scroll the window
790 call Setup_NewWindow()
791 100
792 norm! >>
793 " Test for z<cr>
794 exe "norm! z\<cr>"
795 call assert_equal(' 100', getline('.'))
796 call assert_equal(100, winsaveview()['topline'])
797 call assert_equal([0, 100, 2, 0, 9], getcurpos())
798
799 " Test for zt
800 21
801 norm! >>0zt
802 call assert_equal(' 21', getline('.'))
803 call assert_equal(21, winsaveview()['topline'])
804 call assert_equal([0, 21, 1, 0, 8], getcurpos())
805
806 " Test for zb
807 30
808 norm! >>$ztzb
809 call assert_equal(' 30', getline('.'))
810 call assert_equal(30, winsaveview()['topline']+winheight(0)-1)
811 call assert_equal([0, 30, 3, 0, 2147483647], getcurpos())
812
813 " Test for z-
814 1
815 30
816 norm! 0z-
817 call assert_equal(' 30', getline('.'))
818 call assert_equal(30, winsaveview()['topline']+winheight(0)-1)
819 call assert_equal([0, 30, 2, 0, 9], getcurpos())
820
821 " Test for z{height}<cr>
822 call assert_equal(10, winheight(0))
823 exe "norm! z12\<cr>"
824 call assert_equal(12, winheight(0))
825 exe "norm! z10\<cr>"
826 call assert_equal(10, winheight(0))
827
828 " Test for z.
829 1
830 21
831 norm! 0z.
832 call assert_equal(' 21', getline('.'))
833 call assert_equal(17, winsaveview()['topline'])
834 call assert_equal([0, 21, 2, 0, 9], getcurpos())
835
836 " Test for zz
837 1
838 21
839 norm! 0zz
840 call assert_equal(' 21', getline('.'))
841 call assert_equal(17, winsaveview()['topline'])
842 call assert_equal([0, 21, 1, 0, 8], getcurpos())
843
844 " Test for z+
845 11
846 norm! zt
847 norm! z+
848 call assert_equal(' 21', getline('.'))
849 call assert_equal(21, winsaveview()['topline'])
850 call assert_equal([0, 21, 2, 0, 9], getcurpos())
851
852 " Test for [count]z+
853 1
854 norm! 21z+
855 call assert_equal(' 21', getline('.'))
856 call assert_equal(21, winsaveview()['topline'])
857 call assert_equal([0, 21, 2, 0, 9], getcurpos())
858
Bram Moolenaar8a9bc952020-10-02 18:48:07 +0200859 " Test for z+ with [count] greater than buffer size
860 1
861 norm! 1000z+
862 call assert_equal(' 100', getline('.'))
863 call assert_equal(100, winsaveview()['topline'])
864 call assert_equal([0, 100, 2, 0, 9], getcurpos())
865
866 " Test for z+ from the last buffer line
867 norm! Gz.z+
868 call assert_equal(' 100', getline('.'))
869 call assert_equal(100, winsaveview()['topline'])
870 call assert_equal([0, 100, 2, 0, 9], getcurpos())
871
Bram Moolenaar87bc3f72016-09-03 17:33:54 +0200872 " Test for z^
873 norm! 22z+0
874 norm! z^
875 call assert_equal(' 21', getline('.'))
876 call assert_equal(12, winsaveview()['topline'])
877 call assert_equal([0, 21, 2, 0, 9], getcurpos())
878
Bram Moolenaar8a9bc952020-10-02 18:48:07 +0200879 " Test for z^ from first buffer line
880 norm! ggz^
881 call assert_equal('1', getline('.'))
882 call assert_equal(1, winsaveview()['topline'])
883 call assert_equal([0, 1, 1, 0, 1], getcurpos())
884
Bram Moolenaar87bc3f72016-09-03 17:33:54 +0200885 " Test for [count]z^
886 1
887 norm! 30z^
888 call assert_equal(' 21', getline('.'))
889 call assert_equal(12, winsaveview()['topline'])
890 call assert_equal([0, 21, 2, 0, 9], getcurpos())
891
892 " cleanup
893 bw!
Bram Moolenaar2931f2a2016-09-09 16:59:08 +0200894endfunc
Bram Moolenaar87bc3f72016-09-03 17:33:54 +0200895
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100896func Test_normal16_z_scroll_hor()
Bram Moolenaar87bc3f72016-09-03 17:33:54 +0200897 " basic test for z commands that scroll the window
898 10new
899 15vsp
900 set nowrap listchars=
901 let lineA='abcdefghijklmnopqrstuvwxyz'
902 let lineB='0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'
903 $put =lineA
904 $put =lineB
905 1d
906
Bram Moolenaar1671f442020-03-10 07:48:13 +0100907 " Test for zl and zh with a count
908 norm! 0z10l
909 call assert_equal([11, 1], [col('.'), wincol()])
910 norm! z4h
911 call assert_equal([11, 5], [col('.'), wincol()])
912 normal! 2gg
913
Bram Moolenaar87bc3f72016-09-03 17:33:54 +0200914 " Test for zl
915 1
916 norm! 5zl
917 call assert_equal(lineA, getline('.'))
918 call assert_equal(6, col('.'))
919 call assert_equal(5, winsaveview()['leftcol'])
920 norm! yl
921 call assert_equal('f', @0)
922
923 " Test for zh
924 norm! 2zh
925 call assert_equal(lineA, getline('.'))
926 call assert_equal(6, col('.'))
927 norm! yl
928 call assert_equal('f', @0)
929 call assert_equal(3, winsaveview()['leftcol'])
930
931 " Test for zL
932 norm! zL
933 call assert_equal(11, col('.'))
934 norm! yl
935 call assert_equal('k', @0)
936 call assert_equal(10, winsaveview()['leftcol'])
937 norm! 2zL
938 call assert_equal(25, col('.'))
939 norm! yl
940 call assert_equal('y', @0)
941 call assert_equal(24, winsaveview()['leftcol'])
942
943 " Test for zH
944 norm! 2zH
945 call assert_equal(25, col('.'))
946 call assert_equal(10, winsaveview()['leftcol'])
947 norm! yl
948 call assert_equal('y', @0)
949
950 " Test for zs
951 norm! $zs
952 call assert_equal(26, col('.'))
953 call assert_equal(25, winsaveview()['leftcol'])
954 norm! yl
955 call assert_equal('z', @0)
956
957 " Test for ze
958 norm! ze
959 call assert_equal(26, col('.'))
960 call assert_equal(11, winsaveview()['leftcol'])
961 norm! yl
962 call assert_equal('z', @0)
963
Bram Moolenaar8a9bc952020-10-02 18:48:07 +0200964 " Test for zs and ze with folds
965 %fold
966 norm! $zs
967 call assert_equal(26, col('.'))
968 call assert_equal(0, winsaveview()['leftcol'])
969 norm! yl
970 call assert_equal('z', @0)
971 norm! ze
972 call assert_equal(26, col('.'))
973 call assert_equal(0, winsaveview()['leftcol'])
974 norm! yl
975 call assert_equal('z', @0)
976
Bram Moolenaar87bc3f72016-09-03 17:33:54 +0200977 " cleanup
978 set wrap listchars=eol:$
979 bw!
Bram Moolenaar2931f2a2016-09-09 16:59:08 +0200980endfunc
Bram Moolenaar87bc3f72016-09-03 17:33:54 +0200981
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100982func Test_normal17_z_scroll_hor2()
Bram Moolenaar87bc3f72016-09-03 17:33:54 +0200983 " basic test for z commands that scroll the window
984 " using 'sidescrolloff' setting
985 10new
986 20vsp
987 set nowrap listchars= sidescrolloff=5
988 let lineA='abcdefghijklmnopqrstuvwxyz'
989 let lineB='0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'
990 $put =lineA
991 $put =lineB
992 1d
993
994 " Test for zl
995 1
996 norm! 5zl
997 call assert_equal(lineA, getline('.'))
998 call assert_equal(11, col('.'))
999 call assert_equal(5, winsaveview()['leftcol'])
1000 norm! yl
1001 call assert_equal('k', @0)
1002
1003 " Test for zh
1004 norm! 2zh
1005 call assert_equal(lineA, getline('.'))
1006 call assert_equal(11, col('.'))
1007 norm! yl
1008 call assert_equal('k', @0)
1009 call assert_equal(3, winsaveview()['leftcol'])
1010
1011 " Test for zL
1012 norm! 0zL
1013 call assert_equal(16, col('.'))
1014 norm! yl
1015 call assert_equal('p', @0)
1016 call assert_equal(10, winsaveview()['leftcol'])
1017 norm! 2zL
1018 call assert_equal(26, col('.'))
1019 norm! yl
1020 call assert_equal('z', @0)
1021 call assert_equal(15, winsaveview()['leftcol'])
1022
1023 " Test for zH
1024 norm! 2zH
1025 call assert_equal(15, col('.'))
1026 call assert_equal(0, winsaveview()['leftcol'])
1027 norm! yl
1028 call assert_equal('o', @0)
1029
1030 " Test for zs
1031 norm! $zs
1032 call assert_equal(26, col('.'))
1033 call assert_equal(20, winsaveview()['leftcol'])
1034 norm! yl
1035 call assert_equal('z', @0)
1036
1037 " Test for ze
1038 norm! ze
1039 call assert_equal(26, col('.'))
1040 call assert_equal(11, winsaveview()['leftcol'])
1041 norm! yl
1042 call assert_equal('z', @0)
1043
1044 " cleanup
1045 set wrap listchars=eol:$ sidescrolloff=0
1046 bw!
Bram Moolenaar2931f2a2016-09-09 16:59:08 +02001047endfunc
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02001048
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02001049" Test for commands that scroll the window horizontally. Test with folds.
1050" H, M, L, CTRL-E, CTRL-Y, CTRL-U, CTRL-D, PageUp, PageDown commands
1051func Test_vert_scroll_cmds()
Bram Moolenaar1671f442020-03-10 07:48:13 +01001052 15new
1053 call setline(1, range(1, 100))
1054 exe "normal! 30ggz\<CR>"
1055 set foldenable
1056 33,36fold
1057 40,43fold
1058 46,49fold
1059 let h = winheight(0)
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02001060
1061 " Test for H, M and L commands
Bram Moolenaar1671f442020-03-10 07:48:13 +01001062 " Top of the screen = 30
1063 " Folded lines = 9
1064 " Bottom of the screen = 30 + h + 9 - 1
1065 normal! 4L
1066 call assert_equal(35 + h, line('.'))
1067 normal! 4H
1068 call assert_equal(33, line('.'))
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02001069
Bram Moolenaar8a9bc952020-10-02 18:48:07 +02001070 " Test for using a large count value
1071 %d
1072 call setline(1, range(1, 4))
1073 norm! 6H
1074 call assert_equal(4, line('.'))
1075
1076 " Test for 'M' with folded lines
1077 %d
1078 call setline(1, range(1, 20))
1079 1,5fold
1080 norm! LM
1081 call assert_equal(12, line('.'))
1082
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02001083 " Test for the CTRL-E and CTRL-Y commands with folds
1084 %d
1085 call setline(1, range(1, 10))
1086 3,5fold
1087 exe "normal 6G3\<C-E>"
1088 call assert_equal(6, line('w0'))
1089 exe "normal 2\<C-Y>"
1090 call assert_equal(2, line('w0'))
1091
1092 " Test for CTRL-Y on a folded line
1093 %d
1094 call setline(1, range(1, 100))
1095 exe (h + 2) .. "," .. (h + 4) .. "fold"
1096 exe h + 5
1097 normal z-
1098 exe "normal \<C-Y>\<C-Y>"
1099 call assert_equal(h + 1, line('w$'))
1100
Bram Moolenaard1ad99b2020-10-04 16:16:54 +02001101 " Test for CTRL-Y from the first line and CTRL-E from the last line
1102 %d
1103 set scrolloff=2
1104 call setline(1, range(1, 4))
1105 exe "normal gg\<C-Y>"
1106 call assert_equal(1, line('w0'))
1107 call assert_equal(1, line('.'))
1108 exe "normal G4\<C-E>\<C-E>"
1109 call assert_equal(4, line('w$'))
1110 call assert_equal(4, line('.'))
1111 set scrolloff&
1112
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02001113 " Using <PageUp> and <PageDown> in an empty buffer should beep
1114 %d
1115 call assert_beeps('exe "normal \<PageUp>"')
1116 call assert_beeps('exe "normal \<C-B>"')
1117 call assert_beeps('exe "normal \<PageDown>"')
1118 call assert_beeps('exe "normal \<C-F>"')
1119
1120 " Test for <C-U> and <C-D> with fold
1121 %d
1122 call setline(1, range(1, 100))
1123 10,35fold
1124 set scroll=10
1125 exe "normal \<C-D>"
1126 call assert_equal(36, line('.'))
1127 exe "normal \<C-D>"
1128 call assert_equal(46, line('.'))
1129 exe "normal \<C-U>"
1130 call assert_equal(36, line('.'))
1131 exe "normal \<C-U>"
1132 call assert_equal(10, line('.'))
1133 exe "normal \<C-U>"
1134 call assert_equal(1, line('.'))
1135 set scroll&
1136
1137 " Test for scrolling to the top of the file with <C-U> and a fold
1138 10
1139 normal ztL
1140 exe "normal \<C-U>\<C-U>"
1141 call assert_equal(1, line('w0'))
1142
1143 " Test for CTRL-D on a folded line
1144 %d
1145 call setline(1, range(1, 100))
1146 50,100fold
1147 75
1148 normal z-
1149 exe "normal \<C-D>"
1150 call assert_equal(50, line('.'))
1151 call assert_equal(100, line('w$'))
1152 normal z.
1153 let lnum = winline()
1154 exe "normal \<C-D>"
1155 call assert_equal(lnum, winline())
1156 call assert_equal(50, line('.'))
1157 normal zt
1158 exe "normal \<C-D>"
1159 call assert_equal(50, line('w0'))
1160
Bram Moolenaard1ad99b2020-10-04 16:16:54 +02001161 " Test for <S-CR>. Page down.
1162 %d
1163 call setline(1, range(1, 100))
1164 call feedkeys("\<S-CR>", 'xt')
1165 call assert_equal(14, line('w0'))
1166 call assert_equal(28, line('w$'))
1167
1168 " Test for <S-->. Page up.
1169 call feedkeys("\<S-->", 'xt')
1170 call assert_equal(1, line('w0'))
1171 call assert_equal(15, line('w$'))
1172
Bram Moolenaar1671f442020-03-10 07:48:13 +01001173 set foldenable&
1174 close!
1175endfunc
1176
Bram Moolenaar777e7c22021-10-25 17:07:04 +01001177func Test_scroll_in_ex_mode()
1178 " This was using invalid memory because w_botline was invalid.
1179 let lines =<< trim END
1180 diffsplit
1181 norm os00(
1182 call writefile(['done'], 'Xdone')
1183 qa!
1184 END
1185 call writefile(lines, 'Xscript')
1186 call assert_equal(1, RunVim([], [], '--clean -X -Z -e -s -S Xscript'))
1187 call assert_equal(['done'], readfile('Xdone'))
1188
1189 call delete('Xscript')
1190 call delete('Xdone')
1191endfunc
1192
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02001193" Test for the 'sidescroll' option
1194func Test_sidescroll_opt()
1195 new
1196 20vnew
1197
1198 " scroll by 2 characters horizontally
1199 set sidescroll=2 nowrap
1200 call setline(1, repeat('a', 40))
1201 normal g$l
1202 call assert_equal(19, screenpos(0, 1, 21).col)
1203 normal l
1204 call assert_equal(20, screenpos(0, 1, 22).col)
1205 normal g0h
1206 call assert_equal(2, screenpos(0, 1, 2).col)
1207 call assert_equal(20, screenpos(0, 1, 20).col)
1208
1209 " when 'sidescroll' is 0, cursor positioned at the center
1210 set sidescroll=0
1211 normal g$l
1212 call assert_equal(11, screenpos(0, 1, 21).col)
1213 normal g0h
1214 call assert_equal(10, screenpos(0, 1, 10).col)
1215
1216 %bw!
1217 set wrap& sidescroll&
1218endfunc
1219
Bram Moolenaar004a6782020-04-11 17:09:31 +02001220" basic tests for foldopen/folddelete
Bram Moolenaar1bbb6192018-11-10 16:02:01 +01001221func Test_normal18_z_fold()
Bram Moolenaar004a6782020-04-11 17:09:31 +02001222 CheckFeature folding
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02001223 call Setup_NewWindow()
1224 50
1225 setl foldenable fdm=marker foldlevel=5
1226
Bram Moolenaar1671f442020-03-10 07:48:13 +01001227 call assert_beeps('normal! zj')
1228 call assert_beeps('normal! zk')
1229
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02001230 " Test for zF
1231 " First fold
1232 norm! 4zF
1233 " check that folds have been created
1234 call assert_equal(['50/*{{{*/', '51', '52', '53/*}}}*/'], getline(50,53))
1235
1236 " Test for zd
1237 51
1238 norm! 2zF
1239 call assert_equal(2, foldlevel('.'))
1240 norm! kzd
1241 call assert_equal(['50', '51/*{{{*/', '52/*}}}*/', '53'], getline(50,53))
1242 norm! j
1243 call assert_equal(1, foldlevel('.'))
1244
1245 " Test for zD
1246 " also deletes partially selected folds recursively
1247 51
1248 norm! zF
1249 call assert_equal(2, foldlevel('.'))
1250 norm! kV2jzD
1251 call assert_equal(['50', '51', '52', '53'], getline(50,53))
1252
1253 " Test for zE
1254 85
1255 norm! 4zF
1256 86
1257 norm! 2zF
1258 90
1259 norm! 4zF
1260 call assert_equal(['85/*{{{*/', '86/*{{{*/', '87/*}}}*/', '88/*}}}*/', '89', '90/*{{{*/', '91', '92', '93/*}}}*/'], getline(85,93))
1261 norm! zE
1262 call assert_equal(['85', '86', '87', '88', '89', '90', '91', '92', '93'], getline(85,93))
1263
1264 " Test for zn
1265 50
1266 set foldlevel=0
1267 norm! 2zF
1268 norm! zn
1269 norm! k
1270 call assert_equal('49', getline('.'))
1271 norm! j
1272 call assert_equal('50/*{{{*/', getline('.'))
1273 norm! j
1274 call assert_equal('51/*}}}*/', getline('.'))
1275 norm! j
1276 call assert_equal('52', getline('.'))
1277 call assert_equal(0, &foldenable)
1278
1279 " Test for zN
1280 49
1281 norm! zN
1282 call assert_equal('49', getline('.'))
1283 norm! j
1284 call assert_equal('50/*{{{*/', getline('.'))
1285 norm! j
1286 call assert_equal('52', getline('.'))
1287 call assert_equal(1, &foldenable)
1288
1289 " Test for zi
1290 norm! zi
1291 call assert_equal(0, &foldenable)
1292 norm! zi
1293 call assert_equal(1, &foldenable)
1294 norm! zi
1295 call assert_equal(0, &foldenable)
1296 norm! zi
1297 call assert_equal(1, &foldenable)
1298
1299 " Test for za
1300 50
1301 norm! za
1302 norm! k
1303 call assert_equal('49', getline('.'))
1304 norm! j
1305 call assert_equal('50/*{{{*/', getline('.'))
1306 norm! j
1307 call assert_equal('51/*}}}*/', getline('.'))
1308 norm! j
1309 call assert_equal('52', getline('.'))
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('52', getline('.'))
1318
1319 49
1320 norm! 5zF
1321 norm! k
1322 call assert_equal('48', getline('.'))
1323 norm! j
1324 call assert_equal('49/*{{{*/', getline('.'))
1325 norm! j
1326 call assert_equal('55', getline('.'))
1327 49
1328 norm! za
1329 call assert_equal('49/*{{{*/', getline('.'))
1330 norm! j
1331 call assert_equal('50/*{{{*/', getline('.'))
1332 norm! j
1333 call assert_equal('52', getline('.'))
1334 set nofoldenable
1335 " close fold and set foldenable
1336 norm! za
1337 call assert_equal(1, &foldenable)
1338
1339 50
1340 " have to use {count}za to open all folds and make the cursor visible
1341 norm! 2za
1342 norm! 2k
1343 call assert_equal('48', getline('.'))
1344 norm! j
1345 call assert_equal('49/*{{{*/', getline('.'))
1346 norm! j
1347 call assert_equal('50/*{{{*/', getline('.'))
1348 norm! j
1349 call assert_equal('51/*}}}*/', getline('.'))
1350 norm! j
1351 call assert_equal('52', getline('.'))
1352
1353 " Test for zA
1354 49
1355 set foldlevel=0
1356 50
1357 norm! zA
1358 norm! 2k
1359 call assert_equal('48', getline('.'))
1360 norm! j
1361 call assert_equal('49/*{{{*/', getline('.'))
1362 norm! j
1363 call assert_equal('50/*{{{*/', getline('.'))
1364 norm! j
1365 call assert_equal('51/*}}}*/', getline('.'))
1366 norm! j
1367 call assert_equal('52', getline('.'))
1368
Dominique Pelle923dce22021-11-21 11:36:04 +00001369 " zA on an opened fold when foldenable is not set
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02001370 50
1371 set nofoldenable
1372 norm! zA
1373 call assert_equal(1, &foldenable)
1374 norm! k
1375 call assert_equal('48', getline('.'))
1376 norm! j
1377 call assert_equal('49/*{{{*/', getline('.'))
1378 norm! j
1379 call assert_equal('55', getline('.'))
1380
1381 " Test for zc
1382 norm! zE
1383 50
1384 norm! 2zF
1385 49
1386 norm! 5zF
1387 set nofoldenable
1388 50
1389 " There most likely is a bug somewhere:
1390 " https://groups.google.com/d/msg/vim_dev/v2EkfJ_KQjI/u-Cvv94uCAAJ
1391 " TODO: Should this only close the inner most fold or both folds?
1392 norm! zc
1393 call assert_equal(1, &foldenable)
1394 norm! k
1395 call assert_equal('48', getline('.'))
1396 norm! j
1397 call assert_equal('49/*{{{*/', getline('.'))
1398 norm! j
1399 call assert_equal('55', getline('.'))
1400 set nofoldenable
1401 50
1402 norm! Vjzc
1403 norm! k
1404 call assert_equal('48', getline('.'))
1405 norm! j
1406 call assert_equal('49/*{{{*/', getline('.'))
1407 norm! j
1408 call assert_equal('55', getline('.'))
1409
1410 " Test for zC
1411 set nofoldenable
1412 50
1413 norm! zCk
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 zx
1421 " 1) close folds at line 49-54
1422 set nofoldenable
1423 48
1424 norm! zx
1425 call assert_equal(1, &foldenable)
1426 norm! j
1427 call assert_equal('49/*{{{*/', getline('.'))
1428 norm! j
1429 call assert_equal('55', getline('.'))
1430
Bram Moolenaar395b6ba2017-04-07 20:09:51 +02001431 " 2) do not close fold under cursor
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02001432 51
1433 set nofoldenable
1434 norm! zx
1435 call assert_equal(1, &foldenable)
1436 norm! 3k
1437 call assert_equal('48', getline('.'))
1438 norm! j
1439 call assert_equal('49/*{{{*/', getline('.'))
1440 norm! j
1441 call assert_equal('50/*{{{*/', getline('.'))
1442 norm! j
1443 call assert_equal('51/*}}}*/', getline('.'))
1444 norm! j
1445 call assert_equal('52', getline('.'))
1446 norm! j
1447 call assert_equal('53', getline('.'))
1448 norm! j
1449 call assert_equal('54/*}}}*/', getline('.'))
1450 norm! j
1451 call assert_equal('55', getline('.'))
1452
1453 " 3) close one level of folds
1454 48
1455 set nofoldenable
1456 set foldlevel=1
1457 norm! zx
1458 call assert_equal(1, &foldenable)
1459 call assert_equal('48', getline('.'))
1460 norm! j
1461 call assert_equal('49/*{{{*/', getline('.'))
1462 norm! j
1463 call assert_equal('50/*{{{*/', getline('.'))
1464 norm! j
1465 call assert_equal('52', getline('.'))
1466 norm! j
1467 call assert_equal('53', getline('.'))
1468 norm! j
1469 call assert_equal('54/*}}}*/', getline('.'))
1470 norm! j
1471 call assert_equal('55', getline('.'))
1472
1473 " Test for zX
1474 " Close all folds
1475 set foldlevel=0 nofoldenable
1476 50
1477 norm! zX
1478 call assert_equal(1, &foldenable)
1479 norm! k
1480 call assert_equal('48', getline('.'))
1481 norm! j
1482 call assert_equal('49/*{{{*/', getline('.'))
1483 norm! j
1484 call assert_equal('55', getline('.'))
1485
1486 " Test for zm
1487 50
1488 set nofoldenable foldlevel=2
1489 norm! zm
1490 call assert_equal(1, &foldenable)
1491 call assert_equal(1, &foldlevel)
1492 norm! zm
1493 call assert_equal(0, &foldlevel)
1494 norm! zm
1495 call assert_equal(0, &foldlevel)
1496 norm! k
1497 call assert_equal('48', getline('.'))
1498 norm! j
1499 call assert_equal('49/*{{{*/', getline('.'))
1500 norm! j
1501 call assert_equal('55', getline('.'))
1502
Bram Moolenaar8a9bc952020-10-02 18:48:07 +02001503 " Test for zm with a count
1504 50
1505 set foldlevel=2
1506 norm! 3zm
1507 call assert_equal(0, &foldlevel)
1508 call assert_equal(49, foldclosed(line('.')))
1509
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02001510 " Test for zM
1511 48
1512 set nofoldenable foldlevel=99
1513 norm! zM
1514 call assert_equal(1, &foldenable)
1515 call assert_equal(0, &foldlevel)
1516 call assert_equal('48', getline('.'))
1517 norm! j
1518 call assert_equal('49/*{{{*/', getline('.'))
1519 norm! j
1520 call assert_equal('55', getline('.'))
1521
1522 " Test for zr
1523 48
1524 set nofoldenable foldlevel=0
1525 norm! zr
1526 call assert_equal(0, &foldenable)
1527 call assert_equal(1, &foldlevel)
1528 set foldlevel=0 foldenable
1529 norm! zr
1530 call assert_equal(1, &foldenable)
1531 call assert_equal(1, &foldlevel)
1532 norm! zr
1533 call assert_equal(2, &foldlevel)
1534 call assert_equal('48', getline('.'))
1535 norm! j
1536 call assert_equal('49/*{{{*/', getline('.'))
1537 norm! j
1538 call assert_equal('50/*{{{*/', getline('.'))
1539 norm! j
1540 call assert_equal('51/*}}}*/', getline('.'))
1541 norm! j
1542 call assert_equal('52', getline('.'))
1543
1544 " Test for zR
1545 48
1546 set nofoldenable foldlevel=0
1547 norm! zR
1548 call assert_equal(0, &foldenable)
1549 call assert_equal(2, &foldlevel)
1550 set foldenable foldlevel=0
1551 norm! zR
1552 call assert_equal(1, &foldenable)
1553 call assert_equal(2, &foldlevel)
1554 call assert_equal('48', getline('.'))
1555 norm! j
1556 call assert_equal('49/*{{{*/', getline('.'))
1557 norm! j
1558 call assert_equal('50/*{{{*/', getline('.'))
1559 norm! j
1560 call assert_equal('51/*}}}*/', getline('.'))
1561 norm! j
1562 call assert_equal('52', getline('.'))
1563 call append(50, ['a /*{{{*/', 'b /*}}}*/'])
1564 48
1565 call assert_equal('48', getline('.'))
1566 norm! j
1567 call assert_equal('49/*{{{*/', getline('.'))
1568 norm! j
1569 call assert_equal('50/*{{{*/', getline('.'))
1570 norm! j
1571 call assert_equal('a /*{{{*/', getline('.'))
1572 norm! j
1573 call assert_equal('51/*}}}*/', getline('.'))
1574 norm! j
1575 call assert_equal('52', getline('.'))
1576 48
1577 norm! zR
1578 call assert_equal(1, &foldenable)
1579 call assert_equal(3, &foldlevel)
1580 call assert_equal('48', getline('.'))
1581 norm! j
1582 call assert_equal('49/*{{{*/', getline('.'))
1583 norm! j
1584 call assert_equal('50/*{{{*/', getline('.'))
1585 norm! j
1586 call assert_equal('a /*{{{*/', getline('.'))
1587 norm! j
1588 call assert_equal('b /*}}}*/', getline('.'))
1589 norm! j
1590 call assert_equal('51/*}}}*/', getline('.'))
1591 norm! j
1592 call assert_equal('52', getline('.'))
1593
1594 " clean up
1595 setl nofoldenable fdm=marker foldlevel=0
1596 bw!
Bram Moolenaar2931f2a2016-09-09 16:59:08 +02001597endfunc
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02001598
Bram Moolenaar1bbb6192018-11-10 16:02:01 +01001599func Test_normal20_exmode()
Bram Moolenaar004a6782020-04-11 17:09:31 +02001600 " Reading from redirected file doesn't work on MS-Windows
1601 CheckNotMSWindows
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02001602 call writefile(['1a', 'foo', 'bar', '.', 'w! Xfile2', 'q!'], 'Xscript')
1603 call writefile(['1', '2'], 'Xfile')
Bram Moolenaar93344c22019-08-14 21:12:05 +02001604 call system(GetVimCommand() .. ' -e -s < Xscript Xfile')
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02001605 let a=readfile('Xfile2')
1606 call assert_equal(['1', 'foo', 'bar', '2'], a)
1607
1608 " clean up
1609 for file in ['Xfile', 'Xfile2', 'Xscript']
1610 call delete(file)
1611 endfor
1612 bw!
Bram Moolenaar2931f2a2016-09-09 16:59:08 +02001613endfunc
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02001614
Bram Moolenaar1bbb6192018-11-10 16:02:01 +01001615func Test_normal21_nv_hat()
1616
1617 " Edit a fresh file and wipe the buffer list so that there is no alternate
1618 " file present. Next, check for the expected command failures.
1619 edit Xfoo | %bw
Bram Moolenaare2e40752020-09-04 21:18:46 +02001620 call assert_fails(':buffer #', 'E86:')
1621 call assert_fails(':execute "normal! \<C-^>"', 'E23:')
Bram Moolenaarb7e24832020-06-24 13:37:35 +02001622 call assert_fails("normal i\<C-R>#", 'E23:')
Bram Moolenaar1bbb6192018-11-10 16:02:01 +01001623
1624 " Test for the expected behavior when switching between two named buffers.
1625 edit Xfoo | edit Xbar
1626 call feedkeys("\<C-^>", 'tx')
1627 call assert_equal('Xfoo', fnamemodify(bufname('%'), ':t'))
1628 call feedkeys("\<C-^>", 'tx')
1629 call assert_equal('Xbar', fnamemodify(bufname('%'), ':t'))
1630
1631 " Test for the expected behavior when only one buffer is named.
1632 enew | let l:nr = bufnr('%')
1633 call feedkeys("\<C-^>", 'tx')
1634 call assert_equal('Xbar', fnamemodify(bufname('%'), ':t'))
1635 call feedkeys("\<C-^>", 'tx')
1636 call assert_equal('', bufname('%'))
1637 call assert_equal(l:nr, bufnr('%'))
1638
1639 " Test that no action is taken by "<C-^>" when an operator is pending.
1640 edit Xfoo
1641 call feedkeys("ci\<C-^>", 'tx')
1642 call assert_equal('Xfoo', fnamemodify(bufname('%'), ':t'))
1643
1644 %bw!
Bram Moolenaar2931f2a2016-09-09 16:59:08 +02001645endfunc
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02001646
Bram Moolenaar1bbb6192018-11-10 16:02:01 +01001647func Test_normal22_zet()
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02001648 " Test for ZZ
Bram Moolenaar0913a102016-09-03 19:11:59 +02001649 " let shell = &shell
1650 " let &shell = 'sh'
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02001651 call writefile(['1', '2'], 'Xfile')
Bram Moolenaar93344c22019-08-14 21:12:05 +02001652 let args = ' -N -i NONE --noplugins -X --not-a-term'
1653 call system(GetVimCommand() .. args .. ' -c "%d" -c ":norm! ZZ" Xfile')
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02001654 let a = readfile('Xfile')
1655 call assert_equal([], a)
1656 " Test for ZQ
1657 call writefile(['1', '2'], 'Xfile')
Bram Moolenaar93344c22019-08-14 21:12:05 +02001658 call system(GetVimCommand() . args . ' -c "%d" -c ":norm! ZQ" Xfile')
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02001659 let a = readfile('Xfile')
1660 call assert_equal(['1', '2'], a)
1661
Bram Moolenaar1671f442020-03-10 07:48:13 +01001662 " Unsupported Z command
1663 call assert_beeps('normal! ZW')
1664
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02001665 " clean up
1666 for file in ['Xfile']
1667 call delete(file)
1668 endfor
Bram Moolenaar0913a102016-09-03 19:11:59 +02001669 " let &shell = shell
Bram Moolenaar2931f2a2016-09-09 16:59:08 +02001670endfunc
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02001671
Bram Moolenaar1bbb6192018-11-10 16:02:01 +01001672func Test_normal23_K()
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02001673 " Test for K command
1674 new
Bram Moolenaar426f3752016-11-04 21:22:37 +01001675 call append(0, ['version8.txt', 'man', 'aa%bb', 'cc|dd'])
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02001676 let k = &keywordprg
1677 set keywordprg=:help
1678 1
1679 norm! VK
1680 call assert_equal('version8.txt', fnamemodify(bufname('%'), ':t'))
1681 call assert_equal('help', &ft)
1682 call assert_match('\*version8.txt\*', getline('.'))
1683 helpclose
1684 norm! 0K
1685 call assert_equal('version8.txt', fnamemodify(bufname('%'), ':t'))
1686 call assert_equal('help', &ft)
Bram Moolenaarb1c91982018-05-17 17:04:55 +02001687 call assert_match('\*version8\.\d\*', getline('.'))
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02001688 helpclose
1689
Bram Moolenaar426f3752016-11-04 21:22:37 +01001690 set keywordprg=:new
1691 set iskeyword+=%
1692 set iskeyword+=\|
1693 2
1694 norm! K
1695 call assert_equal('man', fnamemodify(bufname('%'), ':t'))
1696 bwipe!
1697 3
1698 norm! K
1699 call assert_equal('aa%bb', fnamemodify(bufname('%'), ':t'))
1700 bwipe!
Bram Moolenaareb828d02016-11-05 19:54:01 +01001701 if !has('win32')
1702 4
1703 norm! K
1704 call assert_equal('cc|dd', fnamemodify(bufname('%'), ':t'))
1705 bwipe!
1706 endif
Bram Moolenaar426f3752016-11-04 21:22:37 +01001707 set iskeyword-=%
1708 set iskeyword-=\|
1709
Bram Moolenaar8a9bc952020-10-02 18:48:07 +02001710 " Test for specifying a count to K
1711 1
1712 com! -nargs=* Kprog let g:Kprog_Args = <q-args>
1713 set keywordprg=:Kprog
1714 norm! 3K
1715 call assert_equal('3 version8', g:Kprog_Args)
1716 delcom Kprog
1717
Bram Moolenaar0913a102016-09-03 19:11:59 +02001718 " Only expect "man" to work on Unix
1719 if !has("unix")
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02001720 let &keywordprg = k
1721 bw!
1722 return
1723 endif
Bram Moolenaarc6b37db2019-04-27 18:00:34 +02001724
Bram Moolenaar9134f1e2019-11-29 20:26:13 +01001725 let not_gnu_man = has('mac') || has('bsd')
1726 if not_gnu_man
Dominique Pelle923dce22021-11-21 11:36:04 +00001727 " In macOS and BSD, the option for specifying a pager is different
Bram Moolenaarc6b37db2019-04-27 18:00:34 +02001728 set keywordprg=man\ -P\ cat
1729 else
1730 set keywordprg=man\ --pager=cat
1731 endif
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02001732 " Test for using man
1733 2
1734 let a = execute('unsilent norm! K')
Bram Moolenaar9134f1e2019-11-29 20:26:13 +01001735 if not_gnu_man
Bram Moolenaarc6b37db2019-04-27 18:00:34 +02001736 call assert_match("man -P cat 'man'", a)
1737 else
1738 call assert_match("man --pager=cat 'man'", a)
1739 endif
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02001740
Bram Moolenaar1671f442020-03-10 07:48:13 +01001741 " Error cases
1742 call setline(1, '#$#')
1743 call assert_fails('normal! ggK', 'E349:')
1744 call setline(1, '---')
1745 call assert_fails('normal! ggv2lK', 'E349:')
1746 call setline(1, ['abc', 'xyz'])
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001747 call assert_fails("normal! gg2lv2h\<C-]>", 'E433:')
Bram Moolenaar1671f442020-03-10 07:48:13 +01001748 call assert_beeps("normal! ggVjK")
Bram Moolenaar8a9bc952020-10-02 18:48:07 +02001749 norm! V
1750 call assert_beeps("norm! cK")
Bram Moolenaar1671f442020-03-10 07:48:13 +01001751
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02001752 " clean up
1753 let &keywordprg = k
1754 bw!
Bram Moolenaar2931f2a2016-09-09 16:59:08 +02001755endfunc
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02001756
Bram Moolenaar1bbb6192018-11-10 16:02:01 +01001757func Test_normal24_rot13()
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02001758 " Testing for g?? g?g?
1759 new
1760 call append(0, 'abcdefghijklmnopqrstuvwxyzäüö')
1761 1
1762 norm! g??
1763 call assert_equal('nopqrstuvwxyzabcdefghijklmäüö', getline('.'))
1764 norm! g?g?
1765 call assert_equal('abcdefghijklmnopqrstuvwxyzäüö', getline('.'))
1766
1767 " clean up
1768 bw!
Bram Moolenaar2931f2a2016-09-09 16:59:08 +02001769endfunc
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02001770
Bram Moolenaar1bbb6192018-11-10 16:02:01 +01001771func Test_normal25_tag()
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01001772 CheckFeature quickfix
1773
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02001774 " Testing for CTRL-] g CTRL-] g]
1775 " CTRL-W g] CTRL-W CTRL-] CTRL-W g CTRL-]
1776 h
1777 " Test for CTRL-]
1778 call search('\<x\>$')
1779 exe "norm! \<c-]>"
1780 call assert_equal("change.txt", fnamemodify(bufname('%'), ':t'))
1781 norm! yiW
1782 call assert_equal("*x*", @0)
1783 exe ":norm \<c-o>"
1784
1785 " Test for g_CTRL-]
1786 call search('\<v_u\>$')
1787 exe "norm! g\<c-]>"
1788 call assert_equal("change.txt", fnamemodify(bufname('%'), ':t'))
1789 norm! yiW
1790 call assert_equal("*v_u*", @0)
1791 exe ":norm \<c-o>"
1792
1793 " Test for g]
1794 call search('\<i_<Esc>$')
1795 let a = execute(":norm! g]")
1796 call assert_match('i_<Esc>.*insert.txt', a)
1797
1798 if !empty(exepath('cscope')) && has('cscope')
1799 " setting cscopetag changes how g] works
1800 set cst
1801 exe "norm! g]"
1802 call assert_equal("insert.txt", fnamemodify(bufname('%'), ':t'))
1803 norm! yiW
1804 call assert_equal("*i_<Esc>*", @0)
1805 exe ":norm \<c-o>"
1806 " Test for CTRL-W g]
1807 exe "norm! \<C-W>g]"
1808 call assert_equal("insert.txt", fnamemodify(bufname('%'), ':t'))
1809 norm! yiW
1810 call assert_equal("*i_<Esc>*", @0)
1811 call assert_equal(3, winnr('$'))
1812 helpclose
1813 set nocst
1814 endif
1815
1816 " Test for CTRL-W g]
1817 let a = execute("norm! \<C-W>g]")
1818 call assert_match('i_<Esc>.*insert.txt', a)
1819
1820 " Test for CTRL-W CTRL-]
1821 exe "norm! \<C-W>\<C-]>"
1822 call assert_equal("insert.txt", fnamemodify(bufname('%'), ':t'))
1823 norm! yiW
1824 call assert_equal("*i_<Esc>*", @0)
1825 call assert_equal(3, winnr('$'))
1826 helpclose
1827
1828 " Test for CTRL-W g CTRL-]
1829 exe "norm! \<C-W>g\<C-]>"
1830 call assert_equal("insert.txt", fnamemodify(bufname('%'), ':t'))
1831 norm! yiW
1832 call assert_equal("*i_<Esc>*", @0)
1833 call assert_equal(3, winnr('$'))
1834 helpclose
1835
1836 " clean up
1837 helpclose
Bram Moolenaar2931f2a2016-09-09 16:59:08 +02001838endfunc
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02001839
Bram Moolenaar1bbb6192018-11-10 16:02:01 +01001840func Test_normal26_put()
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02001841 " Test for ]p ]P [p and [P
1842 new
1843 call append(0, ['while read LINE', 'do', ' ((count++))', ' if [ $? -ne 0 ]; then', " echo 'Error writing file'", ' fi', 'done'])
1844 1
1845 /Error/y a
1846 2
1847 norm! "a]pj"a[p
1848 call assert_equal(['do', "echo 'Error writing file'", " echo 'Error writing file'", ' ((count++))'], getline(2,5))
1849 1
1850 /^\s\{4}/
1851 exe "norm! \"a]P3Eldt'"
1852 exe "norm! j\"a[P2Eldt'"
1853 call assert_equal([' if [ $? -ne 0 ]; then', " echo 'Error writing'", " echo 'Error'", " echo 'Error writing file'", ' fi'], getline(6,10))
1854
1855 " clean up
1856 bw!
Bram Moolenaar2931f2a2016-09-09 16:59:08 +02001857endfunc
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02001858
Bram Moolenaar1bbb6192018-11-10 16:02:01 +01001859func Test_normal27_bracket()
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02001860 " Test for [' [` ]' ]`
1861 call Setup_NewWindow()
1862 1,21s/.\+/ & b/
1863 1
1864 norm! $ma
1865 5
1866 norm! $mb
1867 10
1868 norm! $mc
1869 15
1870 norm! $md
1871 20
1872 norm! $me
1873
1874 " Test for ['
1875 9
1876 norm! 2['
1877 call assert_equal(' 1 b', getline('.'))
1878 call assert_equal(1, line('.'))
1879 call assert_equal(3, col('.'))
1880
1881 " Test for ]'
1882 norm! ]'
1883 call assert_equal(' 5 b', getline('.'))
1884 call assert_equal(5, line('.'))
1885 call assert_equal(3, col('.'))
1886
1887 " No mark after line 21, cursor moves to first non blank on current line
1888 21
1889 norm! $]'
1890 call assert_equal(' 21 b', getline('.'))
1891 call assert_equal(21, line('.'))
1892 call assert_equal(3, col('.'))
1893
1894 " Test for [`
1895 norm! 2[`
1896 call assert_equal(' 15 b', getline('.'))
1897 call assert_equal(15, line('.'))
1898 call assert_equal(8, col('.'))
1899
1900 " Test for ]`
1901 norm! ]`
1902 call assert_equal(' 20 b', getline('.'))
1903 call assert_equal(20, line('.'))
1904 call assert_equal(8, col('.'))
1905
1906 " clean up
1907 bw!
Bram Moolenaar2931f2a2016-09-09 16:59:08 +02001908endfunc
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02001909
Bram Moolenaar1671f442020-03-10 07:48:13 +01001910" Test for ( and ) sentence movements
Bram Moolenaar1bbb6192018-11-10 16:02:01 +01001911func Test_normal28_parenthesis()
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02001912 new
1913 call append(0, ['This is a test. With some sentences!', '', 'Even with a question? And one more. And no sentence here'])
1914
1915 $
1916 norm! d(
1917 call assert_equal(['This is a test. With some sentences!', '', 'Even with a question? And one more. ', ''], getline(1, '$'))
1918 norm! 2d(
1919 call assert_equal(['This is a test. With some sentences!', '', ' ', ''], getline(1, '$'))
1920 1
1921 norm! 0d)
1922 call assert_equal(['With some sentences!', '', ' ', ''], getline(1, '$'))
1923
1924 call append('$', ['This is a long sentence', '', 'spanning', 'over several lines. '])
1925 $
1926 norm! $d(
1927 call assert_equal(['With some sentences!', '', ' ', '', 'This is a long sentence', ''], getline(1, '$'))
1928
Bram Moolenaar224a5f12020-04-28 20:29:07 +02001929 " Move to the next sentence from a paragraph macro
1930 %d
1931 call setline(1, ['.LP', 'blue sky!. blue sky.', 'blue sky. blue sky.'])
1932 call cursor(1, 1)
1933 normal )
1934 call assert_equal([2, 1], [line('.'), col('.')])
1935 normal )
1936 call assert_equal([2, 12], [line('.'), col('.')])
1937 normal ((
1938 call assert_equal([1, 1], [line('.'), col('.')])
1939
Bram Moolenaar1671f442020-03-10 07:48:13 +01001940 " It is an error if a next sentence is not found
1941 %d
1942 call setline(1, '.SH')
1943 call assert_beeps('normal )')
1944
Bram Moolenaar224a5f12020-04-28 20:29:07 +02001945 " If only dot is present, don't treat that as a sentence
1946 call setline(1, '. This is a sentence.')
1947 normal $((
1948 call assert_equal(3, col('.'))
1949
Bram Moolenaar1671f442020-03-10 07:48:13 +01001950 " Jumping to a fold should open the fold
1951 call setline(1, ['', '', 'one', 'two', 'three'])
1952 set foldenable
1953 2,$fold
1954 call feedkeys(')', 'xt')
1955 call assert_equal(3, line('.'))
1956 call assert_equal(1, foldlevel('.'))
1957 call assert_equal(-1, foldclosed('.'))
1958 set foldenable&
1959
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02001960 " clean up
1961 bw!
Bram Moolenaar2931f2a2016-09-09 16:59:08 +02001962endfunc
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02001963
Bram Moolenaar1671f442020-03-10 07:48:13 +01001964" Test for { and } paragraph movements
1965func Test_normal29_brace()
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001966 let text =<< trim [DATA]
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001967 A paragraph begins after each empty line, and also at each of a set of
1968 paragraph macros, specified by the pairs of characters in the 'paragraphs'
1969 option. The default is "IPLPPPQPP TPHPLIPpLpItpplpipbp", which corresponds to
1970 the macros ".IP", ".LP", etc. (These are nroff macros, so the dot must be in
1971 the first column). A section boundary is also a paragraph boundary.
1972 Note that a blank line (only containing white space) is NOT a paragraph
1973 boundary.
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001974
1975
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001976 Also note that this does not include a '{' or '}' in the first column. When
1977 the '{' flag is in 'cpoptions' then '{' in the first column is used as a
1978 paragraph boundary |posix|.
1979 {
1980 This is no paragraph
1981 unless the '{' is set
1982 in 'cpoptions'
1983 }
1984 .IP
1985 The nroff macros IP separates a paragraph
1986 That means, it must be a '.'
1987 followed by IP
1988 .LPIt does not matter, if afterwards some
1989 more characters follow.
1990 .SHAlso section boundaries from the nroff
1991 macros terminate a paragraph. That means
1992 a character like this:
1993 .NH
1994 End of text here
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001995 [DATA]
1996
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02001997 new
1998 call append(0, text)
1999 1
2000 norm! 0d2}
Bram Moolenaarc79745a2019-05-20 22:12:34 +02002001
2002 let expected =<< trim [DATA]
Bram Moolenaare7eb9272019-06-24 00:58:07 +02002003 .IP
2004 The nroff macros IP separates a paragraph
2005 That means, it must be a '.'
2006 followed by IP
2007 .LPIt does not matter, if afterwards some
2008 more characters follow.
2009 .SHAlso section boundaries from the nroff
2010 macros terminate a paragraph. That means
2011 a character like this:
2012 .NH
2013 End of text here
Bram Moolenaarc79745a2019-05-20 22:12:34 +02002014
2015 [DATA]
2016 call assert_equal(expected, getline(1, '$'))
2017
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002018 norm! 0d}
Bram Moolenaarc79745a2019-05-20 22:12:34 +02002019
2020 let expected =<< trim [DATA]
Bram Moolenaare7eb9272019-06-24 00:58:07 +02002021 .LPIt does not matter, if afterwards some
2022 more characters follow.
2023 .SHAlso section boundaries from the nroff
2024 macros terminate a paragraph. That means
2025 a character like this:
2026 .NH
2027 End of text here
2028
Bram Moolenaarc79745a2019-05-20 22:12:34 +02002029 [DATA]
2030 call assert_equal(expected, getline(1, '$'))
2031
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002032 $
2033 norm! d{
Bram Moolenaarc79745a2019-05-20 22:12:34 +02002034
2035 let expected =<< trim [DATA]
Bram Moolenaare7eb9272019-06-24 00:58:07 +02002036 .LPIt does not matter, if afterwards some
2037 more characters follow.
2038 .SHAlso section boundaries from the nroff
2039 macros terminate a paragraph. That means
2040 a character like this:
Bram Moolenaarc79745a2019-05-20 22:12:34 +02002041
2042 [DATA]
2043 call assert_equal(expected, getline(1, '$'))
2044
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002045 norm! d{
Bram Moolenaarc79745a2019-05-20 22:12:34 +02002046
2047 let expected =<< trim [DATA]
Bram Moolenaare7eb9272019-06-24 00:58:07 +02002048 .LPIt does not matter, if afterwards some
2049 more characters follow.
Bram Moolenaarc79745a2019-05-20 22:12:34 +02002050
2051 [DATA]
2052 call assert_equal(expected, getline(1, '$'))
2053
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002054 " Test with { in cpooptions
2055 %d
2056 call append(0, text)
2057 set cpo+={
2058 1
2059 norm! 0d2}
Bram Moolenaarc79745a2019-05-20 22:12:34 +02002060
2061 let expected =<< trim [DATA]
Bram Moolenaare7eb9272019-06-24 00:58:07 +02002062 {
2063 This is no paragraph
2064 unless the '{' is set
2065 in 'cpoptions'
2066 }
2067 .IP
2068 The nroff macros IP separates a paragraph
2069 That means, it must be a '.'
2070 followed by IP
2071 .LPIt does not matter, if afterwards some
2072 more characters follow.
2073 .SHAlso section boundaries from the nroff
2074 macros terminate a paragraph. That means
2075 a character like this:
2076 .NH
2077 End of text here
Bram Moolenaarc79745a2019-05-20 22:12:34 +02002078
2079 [DATA]
2080 call assert_equal(expected, getline(1, '$'))
2081
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002082 $
2083 norm! d}
Bram Moolenaarc79745a2019-05-20 22:12:34 +02002084
2085 let expected =<< trim [DATA]
Bram Moolenaare7eb9272019-06-24 00:58:07 +02002086 {
2087 This is no paragraph
2088 unless the '{' is set
2089 in 'cpoptions'
2090 }
2091 .IP
2092 The nroff macros IP separates a paragraph
2093 That means, it must be a '.'
2094 followed by IP
2095 .LPIt does not matter, if afterwards some
2096 more characters follow.
2097 .SHAlso section boundaries from the nroff
2098 macros terminate a paragraph. That means
2099 a character like this:
2100 .NH
2101 End of text here
Bram Moolenaarc79745a2019-05-20 22:12:34 +02002102
2103 [DATA]
2104 call assert_equal(expected, getline(1, '$'))
2105
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002106 norm! gg}
2107 norm! d5}
Bram Moolenaarc79745a2019-05-20 22:12:34 +02002108
2109 let expected =<< trim [DATA]
Bram Moolenaare7eb9272019-06-24 00:58:07 +02002110 {
2111 This is no paragraph
2112 unless the '{' is set
2113 in 'cpoptions'
2114 }
Bram Moolenaarc79745a2019-05-20 22:12:34 +02002115
2116 [DATA]
2117 call assert_equal(expected, getline(1, '$'))
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002118
Bram Moolenaar1671f442020-03-10 07:48:13 +01002119 " Jumping to a fold should open the fold
2120 %d
2121 call setline(1, ['', 'one', 'two', ''])
2122 set foldenable
2123 2,$fold
2124 call feedkeys('}', 'xt')
2125 call assert_equal(4, line('.'))
2126 call assert_equal(1, foldlevel('.'))
2127 call assert_equal(-1, foldclosed('.'))
2128 set foldenable&
2129
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002130 " clean up
2131 set cpo-={
2132 bw!
Bram Moolenaar2931f2a2016-09-09 16:59:08 +02002133endfunc
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002134
Bram Moolenaar8a9bc952020-10-02 18:48:07 +02002135" Test for section movements
2136func Test_normal_section()
2137 new
2138 let lines =<< trim [END]
2139 int foo()
2140 {
2141 if (1)
2142 {
2143 a = 1;
2144 }
2145 }
2146 [END]
2147 call setline(1, lines)
2148
2149 " jumping to a folded line using [[ should open the fold
2150 2,3fold
2151 call cursor(5, 1)
2152 call feedkeys("[[", 'xt')
2153 call assert_equal(2, line('.'))
2154 call assert_equal(-1, foldclosedend(line('.')))
2155
2156 close!
2157endfunc
2158
Bram Moolenaard1ad99b2020-10-04 16:16:54 +02002159" Test for changing case using u, U, gu, gU and ~ (tilde) commands
Bram Moolenaar1671f442020-03-10 07:48:13 +01002160func Test_normal30_changecase()
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002161 new
2162 call append(0, 'This is a simple test: äüöß')
2163 norm! 1ggVu
2164 call assert_equal('this is a simple test: äüöß', getline('.'))
2165 norm! VU
2166 call assert_equal('THIS IS A SIMPLE TEST: ÄÜÖSS', getline('.'))
2167 norm! guu
2168 call assert_equal('this is a simple test: äüöss', getline('.'))
2169 norm! gUgU
2170 call assert_equal('THIS IS A SIMPLE TEST: ÄÜÖSS', getline('.'))
2171 norm! gugu
2172 call assert_equal('this is a simple test: äüöss', getline('.'))
2173 norm! gUU
2174 call assert_equal('THIS IS A SIMPLE TEST: ÄÜÖSS', getline('.'))
2175 norm! 010~
2176 call assert_equal('this is a SIMPLE TEST: ÄÜÖSS', getline('.'))
2177 norm! V~
2178 call assert_equal('THIS IS A simple test: äüöss', getline('.'))
Bram Moolenaard1ad99b2020-10-04 16:16:54 +02002179 call assert_beeps('norm! c~')
2180 %d
2181 call assert_beeps('norm! ~')
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002182
Bram Moolenaar1671f442020-03-10 07:48:13 +01002183 " Test for changing case across lines using 'whichwrap'
2184 call setline(1, ['aaaaaa', 'aaaaaa'])
2185 normal! gg10~
2186 call assert_equal(['AAAAAA', 'aaaaaa'], getline(1, 2))
2187 set whichwrap+=~
2188 normal! gg10~
2189 call assert_equal(['aaaaaa', 'AAAAaa'], getline(1, 2))
2190 set whichwrap&
2191
Bram Moolenaar3e72dca2021-05-29 16:30:12 +02002192 " try changing the case with a double byte encoding (DBCS)
2193 %bw!
2194 let enc = &enc
2195 set encoding=cp932
2196 call setline(1, "\u8470")
2197 normal ~
2198 normal gU$gu$gUgUg~g~gugu
2199 call assert_equal("\u8470", getline(1))
2200 let &encoding = enc
2201
Bram Moolenaar1671f442020-03-10 07:48:13 +01002202 " clean up
2203 bw!
2204endfunc
2205
2206" Turkish ASCII turns to multi-byte. On some systems Turkish locale
2207" is available but toupper()/tolower() don't do the right thing.
2208func Test_normal_changecase_turkish()
2209 new
Bram Moolenaarf1c118b2018-09-03 22:08:10 +02002210 try
2211 lang tr_TR.UTF-8
2212 set casemap=
2213 let iupper = toupper('i')
2214 if iupper == "\u0130"
Bram Moolenaar9f4de1f2017-04-08 19:39:43 +02002215 call setline(1, 'iI')
2216 1normal gUU
2217 call assert_equal("\u0130I", getline(1))
2218 call assert_equal("\u0130I", toupper("iI"))
Bram Moolenaar3317d5e2017-04-08 19:12:06 +02002219
Bram Moolenaar9f4de1f2017-04-08 19:39:43 +02002220 call setline(1, 'iI')
2221 1normal guu
2222 call assert_equal("i\u0131", getline(1))
2223 call assert_equal("i\u0131", tolower("iI"))
Bram Moolenaarf1c118b2018-09-03 22:08:10 +02002224 elseif iupper == "I"
Bram Moolenaar1cc48202017-04-09 13:41:59 +02002225 call setline(1, 'iI')
2226 1normal gUU
2227 call assert_equal("II", getline(1))
2228 call assert_equal("II", toupper("iI"))
2229
2230 call setline(1, 'iI')
2231 1normal guu
2232 call assert_equal("ii", getline(1))
2233 call assert_equal("ii", tolower("iI"))
Bram Moolenaarf1c118b2018-09-03 22:08:10 +02002234 else
2235 call assert_true(false, "expected toupper('i') to be either 'I' or '\u0130'")
2236 endif
2237 set casemap&
2238 call setline(1, 'iI')
2239 1normal gUU
2240 call assert_equal("II", getline(1))
2241 call assert_equal("II", toupper("iI"))
Bram Moolenaar1cc48202017-04-09 13:41:59 +02002242
Bram Moolenaarf1c118b2018-09-03 22:08:10 +02002243 call setline(1, 'iI')
2244 1normal guu
2245 call assert_equal("ii", getline(1))
2246 call assert_equal("ii", tolower("iI"))
2247
2248 lang en_US.UTF-8
2249 catch /E197:/
2250 " can't use Turkish locale
2251 throw 'Skipped: Turkish locale not available'
2252 endtry
Bram Moolenaar1671f442020-03-10 07:48:13 +01002253 close!
Bram Moolenaar2931f2a2016-09-09 16:59:08 +02002254endfunc
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002255
Bram Moolenaar1671f442020-03-10 07:48:13 +01002256" Test for r (replace) command
2257func Test_normal31_r_cmd()
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002258 new
2259 call append(0, 'This is a simple test: abcd')
2260 exe "norm! 1gg$r\<cr>"
2261 call assert_equal(['This is a simple test: abc', '', ''], getline(1,'$'))
2262 exe "norm! 1gg2wlr\<cr>"
2263 call assert_equal(['This is a', 'simple test: abc', '', ''], getline(1,'$'))
2264 exe "norm! 2gg0W5r\<cr>"
2265 call assert_equal(['This is a', 'simple ', ' abc', '', ''], getline('1', '$'))
2266 set autoindent
2267 call setline(2, ['simple test: abc', ''])
2268 exe "norm! 2gg0W5r\<cr>"
2269 call assert_equal(['This is a', 'simple ', 'abc', '', '', ''], getline('1', '$'))
2270 exe "norm! 1ggVr\<cr>"
2271 call assert_equal('^M^M^M^M^M^M^M^M^M', strtrans(getline(1)))
2272 call setline(1, 'This is a')
2273 exe "norm! 1gg05rf"
2274 call assert_equal('fffffis a', getline(1))
2275
Bram Moolenaar1671f442020-03-10 07:48:13 +01002276 " When replacing characters, copy characters from above and below lines
2277 " using CTRL-Y and CTRL-E.
2278 " Different code paths are used for utf-8 and latin1 encodings
2279 set showmatch
2280 for enc in ['latin1', 'utf-8']
2281 enew!
2282 let &encoding = enc
2283 call setline(1, [' {a}', 'xxxxxxxxxx', ' [b]'])
2284 exe "norm! 2gg5r\<C-Y>l5r\<C-E>"
2285 call assert_equal(' {a}x [b]x', getline(2))
2286 endfor
2287 set showmatch&
2288
2289 " r command should fail in operator pending mode
2290 call assert_beeps('normal! cr')
2291
Bram Moolenaar004a6782020-04-11 17:09:31 +02002292 " replace a tab character in visual mode
2293 %d
2294 call setline(1, ["a\tb", "c\td", "e\tf"])
2295 normal gglvjjrx
2296 call assert_equal(['axx', 'xxx', 'xxf'], getline(1, '$'))
2297
Bram Moolenaard7e5e942020-10-07 16:54:52 +02002298 " replace with a multibyte character (with multiple composing characters)
2299 %d
2300 new
2301 call setline(1, 'aaa')
2302 exe "normal $ra\u0328\u0301"
2303 call assert_equal("aaa\u0328\u0301", getline(1))
2304
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002305 " clean up
2306 set noautoindent
2307 bw!
Bram Moolenaar2931f2a2016-09-09 16:59:08 +02002308endfunc
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002309
Bram Moolenaarf5f1e102020-03-08 05:13:15 +01002310" Test for g*, g#
Bram Moolenaar1bbb6192018-11-10 16:02:01 +01002311func Test_normal32_g_cmd1()
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002312 new
2313 call append(0, ['abc.x_foo', 'x_foobar.abc'])
2314 1
2315 norm! $g*
2316 call assert_equal('x_foo', @/)
2317 call assert_equal('x_foobar.abc', getline('.'))
2318 norm! $g#
2319 call assert_equal('abc', @/)
2320 call assert_equal('abc.x_foo', getline('.'))
2321
2322 " clean up
2323 bw!
Bram Moolenaar2931f2a2016-09-09 16:59:08 +02002324endfunc
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002325
Bram Moolenaarf5f1e102020-03-08 05:13:15 +01002326" Test for g`, g;, g,, g&, gv, gk, gj, gJ, g0, g^, g_, gm, g$, gM, g CTRL-G,
2327" gi and gI commands
Bram Moolenaar1671f442020-03-10 07:48:13 +01002328func Test_normal33_g_cmd2()
Bram Moolenaar004a6782020-04-11 17:09:31 +02002329 CheckFeature jumplist
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002330 call Setup_NewWindow()
2331 " Test for g`
2332 clearjumps
2333 norm! ma10j
2334 let a=execute(':jumps')
2335 " empty jumplist
2336 call assert_equal('>', a[-1:])
2337 norm! g`a
2338 call assert_equal('>', a[-1:])
2339 call assert_equal(1, line('.'))
2340 call assert_equal('1', getline('.'))
Bram Moolenaarf5f1e102020-03-08 05:13:15 +01002341 call cursor(10, 1)
2342 norm! g'a
2343 call assert_equal('>', a[-1:])
2344 call assert_equal(1, line('.'))
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002345
2346 " Test for g; and g,
2347 norm! g;
2348 " there is only one change in the changelist
2349 " currently, when we setup the window
2350 call assert_equal(2, line('.'))
Bram Moolenaare2e40752020-09-04 21:18:46 +02002351 call assert_fails(':norm! g;', 'E662:')
2352 call assert_fails(':norm! g,', 'E663:')
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002353 let &ul=&ul
2354 call append('$', ['a', 'b', 'c', 'd'])
2355 let &ul=&ul
2356 call append('$', ['Z', 'Y', 'X', 'W'])
2357 let a = execute(':changes')
2358 call assert_match('2\s\+0\s\+2', a)
2359 call assert_match('101\s\+0\s\+a', a)
2360 call assert_match('105\s\+0\s\+Z', a)
2361 norm! 3g;
2362 call assert_equal(2, line('.'))
2363 norm! 2g,
2364 call assert_equal(105, line('.'))
2365
2366 " Test for g& - global substitute
2367 %d
2368 call setline(1, range(1,10))
2369 call append('$', ['a', 'b', 'c', 'd'])
2370 $s/\w/&&/g
2371 exe "norm! /[1-8]\<cr>"
2372 norm! g&
2373 call assert_equal(['11', '22', '33', '44', '55', '66', '77', '88', '9', '110', 'a', 'b', 'c', 'dd'], getline(1, '$'))
2374
Bram Moolenaar1671f442020-03-10 07:48:13 +01002375 " Jumping to a fold using gg should open the fold
2376 set foldenable
2377 set foldopen+=jump
2378 5,8fold
2379 call feedkeys('6gg', 'xt')
2380 call assert_equal(1, foldlevel('.'))
2381 call assert_equal(-1, foldclosed('.'))
2382 set foldopen-=jump
2383 set foldenable&
2384
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002385 " Test for gv
2386 %d
2387 call append('$', repeat(['abcdefgh'], 8))
2388 exe "norm! 2gg02l\<c-v>2j2ly"
2389 call assert_equal(['cde', 'cde', 'cde'], getreg(0, 1, 1))
2390 " in visual mode, gv swaps current and last selected region
2391 exe "norm! G0\<c-v>4k4lgvd"
2392 call assert_equal(['', 'abfgh', 'abfgh', 'abfgh', 'abcdefgh', 'abcdefgh', 'abcdefgh', 'abcdefgh', 'abcdefgh'], getline(1,'$'))
2393 exe "norm! G0\<c-v>4k4ly"
2394 exe "norm! gvood"
2395 call assert_equal(['', 'abfgh', 'abfgh', 'abfgh', 'fgh', 'fgh', 'fgh', 'fgh', 'fgh'], getline(1,'$'))
Bram Moolenaarf5f1e102020-03-08 05:13:15 +01002396 " gv cannot be used in operator pending mode
2397 call assert_beeps('normal! cgv')
2398 " gv should beep without a previously selected visual area
2399 new
2400 call assert_beeps('normal! gv')
2401 close
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002402
2403 " Test for gk/gj
2404 %d
2405 15vsp
2406 set wrap listchars= sbr=
Bram Moolenaar74ede802021-05-29 19:18:01 +02002407 let lineA = 'abcdefghijklmnopqrstuvwxyz'
2408 let lineB = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'
2409 let lineC = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz01234567890123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002410 $put =lineA
2411 $put =lineB
2412
2413 norm! 3gg0dgk
2414 call assert_equal(['', 'abcdefghijklmno', '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'], getline(1, '$'))
2415 set nu
2416 norm! 3gg0gjdgj
2417 call assert_equal(['', 'abcdefghijklmno', '0123456789AMNOPQRSTUVWXYZ'], getline(1,'$'))
2418
2419 " Test for gJ
2420 norm! 2gggJ
2421 call assert_equal(['', 'abcdefghijklmno0123456789AMNOPQRSTUVWXYZ'], getline(1,'$'))
2422 call assert_equal(16, col('.'))
2423 " shouldn't do anything
2424 norm! 10gJ
2425 call assert_equal(1, col('.'))
2426
2427 " Test for g0 g^ gm g$
2428 exe "norm! 2gg0gji "
2429 call assert_equal(['', 'abcdefghijk lmno0123456789AMNOPQRSTUVWXYZ'], getline(1,'$'))
2430 norm! g0yl
2431 call assert_equal(12, col('.'))
2432 call assert_equal(' ', getreg(0))
2433 norm! g$yl
2434 call assert_equal(22, col('.'))
2435 call assert_equal('3', getreg(0))
2436 norm! gmyl
2437 call assert_equal(17, col('.'))
2438 call assert_equal('n', getreg(0))
2439 norm! g^yl
2440 call assert_equal(15, col('.'))
2441 call assert_equal('l', getreg(0))
Bram Moolenaarf5f1e102020-03-08 05:13:15 +01002442 call assert_beeps('normal 5g$')
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002443
Bram Moolenaar74ede802021-05-29 19:18:01 +02002444 " Test for g$ with double-width character half displayed
2445 vsplit
2446 9wincmd |
2447 setlocal nowrap nonumber
2448 call setline(2, 'asdfasdfヨ')
2449 2
2450 normal 0g$
2451 call assert_equal(8, col('.'))
2452 10wincmd |
2453 normal 0g$
2454 call assert_equal(9, col('.'))
2455
2456 setlocal signcolumn=yes
2457 11wincmd |
2458 normal 0g$
2459 call assert_equal(8, col('.'))
2460 12wincmd |
2461 normal 0g$
2462 call assert_equal(9, col('.'))
2463
2464 close
2465
Bram Moolenaarf5f1e102020-03-08 05:13:15 +01002466 " Test for g_
2467 call assert_beeps('normal! 100g_')
2468 call setline(2, [' foo ', ' foobar '])
2469 normal! 2ggg_
2470 call assert_equal(5, col('.'))
2471 normal! 2g_
2472 call assert_equal(8, col('.'))
2473
2474 norm! 2ggdG
Bram Moolenaar8b530c12019-10-28 02:13:05 +01002475 $put =lineC
2476
2477 " Test for gM
2478 norm! gMyl
2479 call assert_equal(73, col('.'))
2480 call assert_equal('0', getreg(0))
2481 " Test for 20gM
2482 norm! 20gMyl
2483 call assert_equal(29, col('.'))
2484 call assert_equal('S', getreg(0))
2485 " Test for 60gM
2486 norm! 60gMyl
2487 call assert_equal(87, col('.'))
2488 call assert_equal('E', getreg(0))
2489
2490 " Test for g Ctrl-G
2491 set ff=unix
2492 let a=execute(":norm! g\<c-g>")
2493 call assert_match('Col 87 of 144; Line 2 of 2; Word 1 of 1; Byte 88 of 146', a)
2494
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002495 " Test for gI
2496 norm! gIfoo
Bram Moolenaar8b530c12019-10-28 02:13:05 +01002497 call assert_equal(['', 'foo0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz01234567890123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'], getline(1,'$'))
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002498
2499 " Test for gi
2500 wincmd c
2501 %d
2502 set tw=0
2503 call setline(1, ['foobar', 'new line'])
2504 norm! A next word
2505 $put ='third line'
2506 norm! gi another word
2507 call assert_equal(['foobar next word another word', 'new line', 'third line'], getline(1,'$'))
Bram Moolenaarf5f1e102020-03-08 05:13:15 +01002508 call setline(1, 'foobar')
2509 normal! Ggifirst line
2510 call assert_equal('foobarfirst line', getline(1))
2511 " Test gi in 'virtualedit' mode with cursor after the end of the line
2512 set virtualedit=all
2513 call setline(1, 'foo')
2514 exe "normal! Abar\<Right>\<Right>\<Right>\<Right>"
2515 call setline(1, 'foo')
2516 normal! Ggifirst line
2517 call assert_equal('foo first line', getline(1))
2518 set virtualedit&
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002519
Dominique Pelle923dce22021-11-21 11:36:04 +00002520 " Test for aborting a g command using CTRL-\ CTRL-G
Bram Moolenaar1671f442020-03-10 07:48:13 +01002521 exe "normal! g\<C-\>\<C-G>"
2522 call assert_equal('foo first line', getline('.'))
2523
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002524 " clean up
2525 bw!
Bram Moolenaar2931f2a2016-09-09 16:59:08 +02002526endfunc
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002527
Bram Moolenaarf5f1e102020-03-08 05:13:15 +01002528" Test for g CTRL-G
Bram Moolenaar1bbb6192018-11-10 16:02:01 +01002529func Test_g_ctrl_g()
Bram Moolenaar05295832018-08-24 22:07:58 +02002530 new
2531
2532 let a = execute(":norm! g\<c-g>")
2533 call assert_equal("\n--No lines in buffer--", a)
2534
Bram Moolenaar1671f442020-03-10 07:48:13 +01002535 " Test for CTRL-G (same as :file)
2536 let a = execute(":norm! \<c-g>")
2537 call assert_equal("\n\n\"[No Name]\" --No lines in buffer--", a)
2538
Bram Moolenaar05295832018-08-24 22:07:58 +02002539 call setline(1, ['first line', 'second line'])
2540
2541 " Test g CTRL-g with dos, mac and unix file type.
2542 norm! gojll
2543 set ff=dos
2544 let a = execute(":norm! g\<c-g>")
2545 call assert_equal("\nCol 3 of 11; Line 2 of 2; Word 3 of 4; Byte 15 of 25", a)
2546
2547 set ff=mac
2548 let a = execute(":norm! g\<c-g>")
2549 call assert_equal("\nCol 3 of 11; Line 2 of 2; Word 3 of 4; Byte 14 of 23", a)
2550
2551 set ff=unix
2552 let a = execute(":norm! g\<c-g>")
2553 call assert_equal("\nCol 3 of 11; Line 2 of 2; Word 3 of 4; Byte 14 of 23", a)
2554
2555 " Test g CTRL-g in visual mode (v)
2556 let a = execute(":norm! gojllvlg\<c-g>")
2557 call assert_equal("\nSelected 1 of 2 Lines; 1 of 4 Words; 2 of 23 Bytes", a)
2558
2559 " Test g CTRL-g in visual mode (CTRL-V) with end col > start col
2560 let a = execute(":norm! \<Esc>gojll\<C-V>kllg\<c-g>")
2561 call assert_equal("\nSelected 3 Cols; 2 of 2 Lines; 2 of 4 Words; 6 of 23 Bytes", a)
2562
2563 " Test g_CTRL-g in visual mode (CTRL-V) with end col < start col
2564 let a = execute(":norm! \<Esc>goll\<C-V>jhhg\<c-g>")
2565 call assert_equal("\nSelected 3 Cols; 2 of 2 Lines; 2 of 4 Words; 6 of 23 Bytes", a)
2566
2567 " Test g CTRL-g in visual mode (CTRL-V) with end_vcol being MAXCOL
2568 let a = execute(":norm! \<Esc>gojll\<C-V>k$g\<c-g>")
2569 call assert_equal("\nSelected 2 of 2 Lines; 4 of 4 Words; 17 of 23 Bytes", a)
2570
2571 " There should be one byte less with noeol
2572 set bin noeol
2573 let a = execute(":norm! \<Esc>gog\<c-g>")
2574 call assert_equal("\nCol 1 of 10; Line 1 of 2; Word 1 of 4; Char 1 of 23; Byte 1 of 22", a)
2575 set bin & eol&
2576
Bram Moolenaar30276f22019-01-24 17:59:39 +01002577 call setline(1, ['Français', '日本語'])
Bram Moolenaar05295832018-08-24 22:07:58 +02002578
Bram Moolenaar30276f22019-01-24 17:59:39 +01002579 let a = execute(":norm! \<Esc>gojlg\<c-g>")
2580 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 +02002581
Bram Moolenaar30276f22019-01-24 17:59:39 +01002582 let a = execute(":norm! \<Esc>gojvlg\<c-g>")
2583 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 +02002584
Bram Moolenaar30276f22019-01-24 17:59:39 +01002585 let a = execute(":norm! \<Esc>goll\<c-v>jlg\<c-g>")
2586 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 +02002587
Bram Moolenaar30276f22019-01-24 17:59:39 +01002588 set fenc=utf8 bomb
2589 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(+3 for BOM)", a)
Bram Moolenaar05295832018-08-24 22:07:58 +02002591
Bram Moolenaar30276f22019-01-24 17:59:39 +01002592 set fenc=utf16 bomb
2593 let a = execute(":norm! g\<c-g>")
2594 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 +02002595
Bram Moolenaar30276f22019-01-24 17:59:39 +01002596 set fenc=utf32 bomb
2597 let a = execute(":norm! g\<c-g>")
2598 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 +02002599
Bram Moolenaar30276f22019-01-24 17:59:39 +01002600 set fenc& bomb&
Bram Moolenaar05295832018-08-24 22:07:58 +02002601
2602 set ff&
2603 bwipe!
2604endfunc
2605
Bram Moolenaarf5f1e102020-03-08 05:13:15 +01002606" Test for g8
Bram Moolenaar1671f442020-03-10 07:48:13 +01002607func Test_normal34_g_cmd3()
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002608 new
Bram Moolenaar395b6ba2017-04-07 20:09:51 +02002609 let a=execute(':norm! 1G0g8')
2610 call assert_equal("\nNUL", a)
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002611
Bram Moolenaar395b6ba2017-04-07 20:09:51 +02002612 call setline(1, 'abcdefghijklmnopqrstuvwxyzäüö')
2613 let a=execute(':norm! 1G$g8')
2614 call assert_equal("\nc3 b6 ", a)
2615
2616 call setline(1, "a\u0302")
2617 let a=execute(':norm! 1G0g8')
2618 call assert_equal("\n61 + cc 82 ", a)
2619
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002620 " clean up
2621 bw!
Bram Moolenaar2931f2a2016-09-09 16:59:08 +02002622endfunc
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002623
Bram Moolenaarf5f1e102020-03-08 05:13:15 +01002624" Test 8g8 which finds invalid utf8 at or after the cursor.
Bram Moolenaar395b6ba2017-04-07 20:09:51 +02002625func Test_normal_8g8()
Bram Moolenaar395b6ba2017-04-07 20:09:51 +02002626 new
2627
Bram Moolenaar395b6ba2017-04-07 20:09:51 +02002628 " With invalid byte.
2629 call setline(1, "___\xff___")
2630 norm! 1G08g8g
2631 call assert_equal([0, 1, 4, 0, 1], getcurpos())
2632
2633 " With invalid byte before the cursor.
2634 call setline(1, "___\xff___")
2635 norm! 1G$h8g8g
2636 call assert_equal([0, 1, 6, 0, 9], getcurpos())
2637
2638 " With truncated sequence.
2639 call setline(1, "___\xE2\x82___")
2640 norm! 1G08g8g
2641 call assert_equal([0, 1, 4, 0, 1], getcurpos())
2642
2643 " With overlong sequence.
2644 call setline(1, "___\xF0\x82\x82\xAC___")
2645 norm! 1G08g8g
2646 call assert_equal([0, 1, 4, 0, 1], getcurpos())
2647
2648 " With valid utf8.
2649 call setline(1, "café")
2650 norm! 1G08g8
2651 call assert_equal([0, 1, 1, 0, 1], getcurpos())
2652
2653 bw!
2654endfunc
2655
Bram Moolenaarf5f1e102020-03-08 05:13:15 +01002656" Test for g<
Bram Moolenaar1671f442020-03-10 07:48:13 +01002657func Test_normal35_g_cmd4()
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002658 " Cannot capture its output,
2659 " probably a bug, therefore, test disabled:
Bram Moolenaar31845092016-09-05 22:58:31 +02002660 throw "Skipped: output of g< can't be tested currently"
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002661 echo "a\nb\nc\nd"
2662 let b=execute(':norm! g<')
2663 call assert_true(!empty(b), 'failed `execute(g<)`')
Bram Moolenaar2931f2a2016-09-09 16:59:08 +02002664endfunc
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002665
Bram Moolenaarf5f1e102020-03-08 05:13:15 +01002666" Test for gp gP go
Bram Moolenaar1671f442020-03-10 07:48:13 +01002667func Test_normal36_g_cmd5()
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002668 new
2669 call append(0, 'abcdefghijklmnopqrstuvwxyz')
Bram Moolenaar0913a102016-09-03 19:11:59 +02002670 set ff=unix
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002671 " Test for gp gP
2672 call append(1, range(1,10))
2673 1
2674 norm! 1yy
2675 3
2676 norm! gp
2677 call assert_equal([0, 5, 1, 0, 1], getcurpos())
2678 $
2679 norm! gP
2680 call assert_equal([0, 14, 1, 0, 1], getcurpos())
2681
2682 " Test for go
2683 norm! 26go
2684 call assert_equal([0, 1, 26, 0, 26], getcurpos())
2685 norm! 27go
2686 call assert_equal([0, 1, 26, 0, 26], getcurpos())
2687 norm! 28go
2688 call assert_equal([0, 2, 1, 0, 1], getcurpos())
2689 set ff=dos
2690 norm! 29go
2691 call assert_equal([0, 2, 1, 0, 1], getcurpos())
2692 set ff=unix
2693 norm! gg0
2694 norm! 101go
2695 call assert_equal([0, 13, 26, 0, 26], getcurpos())
2696 norm! 103go
2697 call assert_equal([0, 14, 1, 0, 1], getcurpos())
2698 " count > buffer content
2699 norm! 120go
2700 call assert_equal([0, 14, 1, 0, 2147483647], getcurpos())
2701 " clean up
2702 bw!
Bram Moolenaar2931f2a2016-09-09 16:59:08 +02002703endfunc
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002704
Bram Moolenaarf5f1e102020-03-08 05:13:15 +01002705" Test for gt and gT
Bram Moolenaar1671f442020-03-10 07:48:13 +01002706func Test_normal37_g_cmd6()
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002707 tabnew 1.txt
2708 tabnew 2.txt
2709 tabnew 3.txt
2710 norm! 1gt
2711 call assert_equal(1, tabpagenr())
2712 norm! 3gt
2713 call assert_equal(3, tabpagenr())
2714 norm! 1gT
2715 " count gT goes not to the absolute tabpagenumber
2716 " but, but goes to the count previous tabpagenumber
2717 call assert_equal(2, tabpagenr())
2718 " wrap around
2719 norm! 3gT
2720 call assert_equal(3, tabpagenr())
2721 " gt does not wrap around
2722 norm! 5gt
2723 call assert_equal(3, tabpagenr())
2724
2725 for i in range(3)
2726 tabclose
2727 endfor
2728 " clean up
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +01002729 call assert_fails(':tabclose', 'E784:')
Bram Moolenaar2931f2a2016-09-09 16:59:08 +02002730endfunc
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002731
Bram Moolenaarf5f1e102020-03-08 05:13:15 +01002732" Test for <Home> and <C-Home> key
Bram Moolenaar1671f442020-03-10 07:48:13 +01002733func Test_normal38_nvhome()
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002734 new
2735 call setline(1, range(10))
2736 $
2737 setl et sw=2
2738 norm! V10>$
2739 " count is ignored
2740 exe "norm! 10\<home>"
2741 call assert_equal(1, col('.'))
2742 exe "norm! \<home>"
2743 call assert_equal([0, 10, 1, 0, 1], getcurpos())
2744 exe "norm! 5\<c-home>"
2745 call assert_equal([0, 5, 1, 0, 1], getcurpos())
2746 exe "norm! \<c-home>"
2747 call assert_equal([0, 1, 1, 0, 1], getcurpos())
Bram Moolenaarf5f1e102020-03-08 05:13:15 +01002748 exe "norm! G\<c-kHome>"
2749 call assert_equal([0, 1, 1, 0, 1], getcurpos())
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002750
2751 " clean up
2752 bw!
Bram Moolenaar2931f2a2016-09-09 16:59:08 +02002753endfunc
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002754
Bram Moolenaar1671f442020-03-10 07:48:13 +01002755" Test for <End> and <C-End> keys
2756func Test_normal_nvend()
2757 new
2758 call setline(1, map(range(1, 10), '"line" .. v:val'))
2759 exe "normal! \<End>"
2760 call assert_equal(5, col('.'))
2761 exe "normal! 4\<End>"
2762 call assert_equal([4, 5], [line('.'), col('.')])
2763 exe "normal! \<C-End>"
2764 call assert_equal([10, 6], [line('.'), col('.')])
2765 close!
2766endfunc
2767
Bram Moolenaarf5f1e102020-03-08 05:13:15 +01002768" Test for cw cW ce
Bram Moolenaar1671f442020-03-10 07:48:13 +01002769func Test_normal39_cw()
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002770 " Test for cw and cW on whitespace
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002771 new
2772 set tw=0
2773 call append(0, 'here are some words')
2774 norm! 1gg0elcwZZZ
2775 call assert_equal('hereZZZare some words', getline('.'))
2776 norm! 1gg0elcWYYY
2777 call assert_equal('hereZZZareYYYsome words', getline('.'))
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002778 norm! 2gg0cwfoo
2779 call assert_equal('foo', getline('.'))
2780
Bram Moolenaarf5f1e102020-03-08 05:13:15 +01002781 call setline(1, 'one; two')
2782 call cursor(1, 1)
2783 call feedkeys('cwvim', 'xt')
2784 call assert_equal('vim; two', getline(1))
2785 call feedkeys('0cWone', 'xt')
2786 call assert_equal('one two', getline(1))
2787 "When cursor is at the end of a word 'ce' will change until the end of the
2788 "next word, but 'cw' will change only one character
2789 call setline(1, 'one two')
2790 call feedkeys('0ecwce', 'xt')
2791 call assert_equal('once two', getline(1))
2792 call setline(1, 'one two')
2793 call feedkeys('0ecely', 'xt')
2794 call assert_equal('only', getline(1))
2795
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002796 " clean up
2797 bw!
Bram Moolenaar2931f2a2016-09-09 16:59:08 +02002798endfunc
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002799
Bram Moolenaarf5f1e102020-03-08 05:13:15 +01002800" Test for CTRL-\ commands
Bram Moolenaar1671f442020-03-10 07:48:13 +01002801func Test_normal40_ctrl_bsl()
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002802 new
2803 call append(0, 'here are some words')
2804 exe "norm! 1gg0a\<C-\>\<C-N>"
2805 call assert_equal('n', mode())
2806 call assert_equal(1, col('.'))
2807 call assert_equal('', visualmode())
2808 exe "norm! 1gg0viw\<C-\>\<C-N>"
2809 call assert_equal('n', mode())
2810 call assert_equal(4, col('.'))
2811 exe "norm! 1gg0a\<C-\>\<C-G>"
2812 call assert_equal('n', mode())
2813 call assert_equal(1, col('.'))
2814 "imap <buffer> , <c-\><c-n>
2815 set im
2816 exe ":norm! \<c-\>\<c-n>dw"
2817 set noim
2818 call assert_equal('are some words', getline(1))
2819 call assert_false(&insertmode)
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002820 call assert_beeps("normal! \<C-\>\<C-A>")
Bram Moolenaar1671f442020-03-10 07:48:13 +01002821
Bram Moolenaar21829c52021-01-26 22:42:21 +01002822 if has('cmdwin')
2823 " Using CTRL-\ CTRL-N in cmd window should close the window
2824 call feedkeys("q:\<C-\>\<C-N>", 'xt')
2825 call assert_equal('', getcmdwintype())
2826 endif
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002827
2828 " clean up
2829 bw!
Bram Moolenaar2931f2a2016-09-09 16:59:08 +02002830endfunc
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002831
Bram Moolenaarf5f1e102020-03-08 05:13:15 +01002832" Test for <c-r>=, <c-r><c-r>= and <c-r><c-o>= in insert mode
Bram Moolenaar1671f442020-03-10 07:48:13 +01002833func Test_normal41_insert_reg()
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002834 new
2835 set sts=2 sw=2 ts=8 tw=0
2836 call append(0, ["aaa\tbbb\tccc", '', '', ''])
2837 let a=getline(1)
2838 norm! 2gg0
2839 exe "norm! a\<c-r>=a\<cr>"
2840 norm! 3gg0
2841 exe "norm! a\<c-r>\<c-r>=a\<cr>"
2842 norm! 4gg0
2843 exe "norm! a\<c-r>\<c-o>=a\<cr>"
2844 call assert_equal(['aaa bbb ccc', 'aaa bbb ccc', 'aaa bbb ccc', 'aaa bbb ccc', ''], getline(1, '$'))
2845
2846 " clean up
2847 set sts=0 sw=8 ts=8
Bram Moolenaar31845092016-09-05 22:58:31 +02002848 bw!
Bram Moolenaar2931f2a2016-09-09 16:59:08 +02002849endfunc
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002850
Bram Moolenaarf5f1e102020-03-08 05:13:15 +01002851" Test for Ctrl-D and Ctrl-U
Bram Moolenaar1bbb6192018-11-10 16:02:01 +01002852func Test_normal42_halfpage()
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002853 call Setup_NewWindow()
2854 call assert_equal(5, &scroll)
2855 exe "norm! \<c-d>"
2856 call assert_equal('6', getline('.'))
2857 exe "norm! 2\<c-d>"
2858 call assert_equal('8', getline('.'))
2859 call assert_equal(2, &scroll)
2860 set scroll=5
2861 exe "norm! \<c-u>"
2862 call assert_equal('3', getline('.'))
2863 1
2864 set scrolloff=5
2865 exe "norm! \<c-d>"
2866 call assert_equal('10', getline('.'))
2867 exe "norm! \<c-u>"
2868 call assert_equal('5', getline('.'))
2869 1
2870 set scrolloff=99
2871 exe "norm! \<c-d>"
2872 call assert_equal('10', getline('.'))
2873 set scrolloff=0
2874 100
2875 exe "norm! $\<c-u>"
2876 call assert_equal('95', getline('.'))
2877 call assert_equal([0, 95, 1, 0, 1], getcurpos())
2878 100
2879 set nostartofline
2880 exe "norm! $\<c-u>"
2881 call assert_equal('95', getline('.'))
2882 call assert_equal([0, 95, 2, 0, 2147483647], getcurpos())
2883 " cleanup
2884 set startofline
2885 bw!
Bram Moolenaar2931f2a2016-09-09 16:59:08 +02002886endfunc
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002887
Bram Moolenaar1bbb6192018-11-10 16:02:01 +01002888func Test_normal45_drop()
Bram Moolenaar29495952018-02-12 22:49:00 +01002889 if !has('dnd')
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01002890 " The ~ register does not exist
2891 call assert_beeps('norm! "~')
Bram Moolenaar29495952018-02-12 22:49:00 +01002892 return
2893 endif
2894
2895 " basic test for drag-n-drop
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002896 " unfortunately, without a gui, we can't really test much here,
2897 " so simply test that ~p fails (which uses the drop register)
2898 new
Bram Moolenaare2e40752020-09-04 21:18:46 +02002899 call assert_fails(':norm! "~p', 'E353:')
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002900 call assert_equal([], getreg('~', 1, 1))
2901 " the ~ register is read only
Bram Moolenaare2e40752020-09-04 21:18:46 +02002902 call assert_fails(':let @~="1"', 'E354:')
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002903 bw!
Bram Moolenaar2931f2a2016-09-09 16:59:08 +02002904endfunc
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002905
Bram Moolenaar1bbb6192018-11-10 16:02:01 +01002906func Test_normal46_ignore()
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002907 new
2908 " How to test this?
2909 " let's just for now test, that the buffer
2910 " does not change
2911 call feedkeys("\<c-s>", 't')
2912 call assert_equal([''], getline(1,'$'))
2913
Bram Moolenaar2931f2a2016-09-09 16:59:08 +02002914 " no valid commands
2915 exe "norm! \<char-0x100>"
2916 call assert_equal([''], getline(1,'$'))
2917
2918 exe "norm! ä"
2919 call assert_equal([''], getline(1,'$'))
2920
Bram Moolenaar87bc3f72016-09-03 17:33:54 +02002921 " clean up
2922 bw!
Bram Moolenaar2931f2a2016-09-09 16:59:08 +02002923endfunc
Bram Moolenaarc4a908e2016-09-08 23:35:30 +02002924
Bram Moolenaar1bbb6192018-11-10 16:02:01 +01002925func Test_normal47_visual_buf_wipe()
Bram Moolenaarc4a908e2016-09-08 23:35:30 +02002926 " This was causing a crash or ml_get error.
2927 enew!
2928 call setline(1,'xxx')
2929 normal $
2930 new
2931 call setline(1, range(1,2))
2932 2
2933 exe "norm \<C-V>$"
2934 bw!
2935 norm yp
2936 set nomodified
Bram Moolenaar2931f2a2016-09-09 16:59:08 +02002937endfunc
2938
Bram Moolenaar1bbb6192018-11-10 16:02:01 +01002939func Test_normal48_wincmd()
Bram Moolenaar2931f2a2016-09-09 16:59:08 +02002940 new
2941 exe "norm! \<c-w>c"
2942 call assert_equal(1, winnr('$'))
Bram Moolenaare2e40752020-09-04 21:18:46 +02002943 call assert_fails(":norm! \<c-w>c", 'E444:')
Bram Moolenaar2931f2a2016-09-09 16:59:08 +02002944endfunc
2945
Bram Moolenaar1bbb6192018-11-10 16:02:01 +01002946func Test_normal49_counts()
Bram Moolenaar2931f2a2016-09-09 16:59:08 +02002947 new
2948 call setline(1, 'one two three four five six seven eight nine ten')
2949 1
2950 norm! 3d2w
2951 call assert_equal('seven eight nine ten', getline(1))
2952 bw!
2953endfunc
2954
Bram Moolenaar1bbb6192018-11-10 16:02:01 +01002955func Test_normal50_commandline()
Bram Moolenaar004a6782020-04-11 17:09:31 +02002956 CheckFeature timers
2957 CheckFeature cmdline_hist
Bram Moolenaar2931f2a2016-09-09 16:59:08 +02002958 func! DoTimerWork(id)
2959 call assert_equal('[Command Line]', bufname(''))
2960 " should fail, with E11, but does fail with E23?
2961 "call feedkeys("\<c-^>", 'tm')
2962
2963 " should also fail with E11
Bram Moolenaare2e40752020-09-04 21:18:46 +02002964 call assert_fails(":wincmd p", 'E11:')
Bram Moolenaar2931f2a2016-09-09 16:59:08 +02002965 " return from commandline window
2966 call feedkeys("\<cr>")
2967 endfunc
2968
2969 let oldlang=v:lang
2970 lang C
2971 set updatetime=20
2972 call timer_start(100, 'DoTimerWork')
2973 try
2974 " throws E23, for whatever reason...
2975 call feedkeys('q:', 'x!')
2976 catch /E23/
2977 " no-op
2978 endtry
2979 " clean up
2980 set updatetime=4000
2981 exe "lang" oldlang
2982 bw!
2983endfunc
2984
Bram Moolenaar1bbb6192018-11-10 16:02:01 +01002985func Test_normal51_FileChangedRO()
Bram Moolenaar004a6782020-04-11 17:09:31 +02002986 CheckFeature autocmd
Bram Moolenaare5f2a072017-02-01 22:31:49 +01002987 " Don't sleep after the warning message.
2988 call test_settime(1)
Bram Moolenaar2931f2a2016-09-09 16:59:08 +02002989 call writefile(['foo'], 'Xreadonly.log')
2990 new Xreadonly.log
2991 setl ro
2992 au FileChangedRO <buffer> :call feedkeys("\<c-^>", 'tix')
Bram Moolenaare2e40752020-09-04 21:18:46 +02002993 call assert_fails(":norm! Af", 'E788:')
Bram Moolenaar2931f2a2016-09-09 16:59:08 +02002994 call assert_equal(['foo'], getline(1,'$'))
2995 call assert_equal('Xreadonly.log', bufname(''))
2996
2997 " cleanup
Bram Moolenaare5f2a072017-02-01 22:31:49 +01002998 call test_settime(0)
Bram Moolenaar2931f2a2016-09-09 16:59:08 +02002999 bw!
3000 call delete("Xreadonly.log")
3001endfunc
3002
Bram Moolenaar1bbb6192018-11-10 16:02:01 +01003003func Test_normal52_rl()
Bram Moolenaar004a6782020-04-11 17:09:31 +02003004 CheckFeature rightleft
Bram Moolenaar2931f2a2016-09-09 16:59:08 +02003005 new
3006 call setline(1, 'abcde fghij klmnopq')
3007 norm! 1gg$
3008 set rl
3009 call assert_equal(19, col('.'))
3010 call feedkeys('l', 'tx')
3011 call assert_equal(18, col('.'))
3012 call feedkeys('h', 'tx')
3013 call assert_equal(19, col('.'))
3014 call feedkeys("\<right>", 'tx')
3015 call assert_equal(18, col('.'))
Bram Moolenaar1671f442020-03-10 07:48:13 +01003016 call feedkeys("\<left>", 'tx')
3017 call assert_equal(19, col('.'))
Bram Moolenaar2931f2a2016-09-09 16:59:08 +02003018 call feedkeys("\<s-right>", 'tx')
3019 call assert_equal(13, col('.'))
3020 call feedkeys("\<c-right>", 'tx')
3021 call assert_equal(7, col('.'))
3022 call feedkeys("\<c-left>", 'tx')
3023 call assert_equal(13, col('.'))
3024 call feedkeys("\<s-left>", 'tx')
3025 call assert_equal(19, col('.'))
3026 call feedkeys("<<", 'tx')
3027 call assert_equal(' abcde fghij klmnopq',getline(1))
3028 call feedkeys(">>", 'tx')
3029 call assert_equal('abcde fghij klmnopq',getline(1))
3030
3031 " cleanup
3032 set norl
3033 bw!
3034endfunc
3035
Bram Moolenaarb1e04fc2017-03-29 13:08:35 +02003036func Test_normal54_Ctrl_bsl()
3037 new
3038 call setline(1, 'abcdefghijklmn')
3039 exe "norm! df\<c-\>\<c-n>"
3040 call assert_equal(['abcdefghijklmn'], getline(1,'$'))
3041 exe "norm! df\<c-\>\<c-g>"
3042 call assert_equal(['abcdefghijklmn'], getline(1,'$'))
3043 exe "norm! df\<c-\>m"
3044 call assert_equal(['abcdefghijklmn'], getline(1,'$'))
Bram Moolenaar30276f22019-01-24 17:59:39 +01003045
Bram Moolenaarb1e04fc2017-03-29 13:08:35 +02003046 call setline(2, 'abcdefghijklmnāf')
3047 norm! 2gg0
3048 exe "norm! df\<Char-0x101>"
3049 call assert_equal(['abcdefghijklmn', 'f'], getline(1,'$'))
3050 norm! 1gg0
3051 exe "norm! df\<esc>"
3052 call assert_equal(['abcdefghijklmn', 'f'], getline(1,'$'))
Bram Moolenaar2931f2a2016-09-09 16:59:08 +02003053
Bram Moolenaarb1e04fc2017-03-29 13:08:35 +02003054 " clean up
3055 bw!
3056endfunc
3057
3058func Test_normal_large_count()
3059 " This may fail with 32bit long, how do we detect that?
3060 new
3061 normal o
3062 normal 6666666666dL
3063 bwipe!
Bram Moolenaar2931f2a2016-09-09 16:59:08 +02003064endfunc
Bram Moolenaarbf3d5802017-03-29 19:48:11 +02003065
3066func Test_delete_until_paragraph()
Bram Moolenaarbf3d5802017-03-29 19:48:11 +02003067 new
3068 normal grádv}
3069 call assert_equal('á', getline(1))
3070 normal grád}
3071 call assert_equal('', getline(1))
3072 bwipe!
3073endfunc
Bram Moolenaarfb094e12017-11-05 20:59:28 +01003074
3075" Test for the gr (virtual replace) command
3076" Test for the bug fixed by 7.4.387
3077func Test_gr_command()
3078 enew!
3079 let save_cpo = &cpo
3080 call append(0, ['First line', 'Second line', 'Third line'])
3081 exe "normal i\<C-G>u"
3082 call cursor(2, 1)
3083 set cpo-=X
3084 normal 4gro
3085 call assert_equal('oooond line', getline(2))
3086 undo
3087 set cpo+=X
3088 normal 4gro
3089 call assert_equal('ooooecond line', getline(2))
3090 let &cpo = save_cpo
Bram Moolenaarf5f1e102020-03-08 05:13:15 +01003091 normal! ggvegrx
3092 call assert_equal('xxxxx line', getline(1))
3093 exe "normal! gggr\<C-V>122"
3094 call assert_equal('zxxxx line', getline(1))
3095 set virtualedit=all
3096 normal! 15|grl
3097 call assert_equal('zxxxx line l', getline(1))
3098 set virtualedit&
3099 set nomodifiable
3100 call assert_fails('normal! grx', 'E21:')
3101 call assert_fails('normal! gRx', 'E21:')
3102 set modifiable&
Bram Moolenaarfb094e12017-11-05 20:59:28 +01003103 enew!
3104endfunc
3105
3106" When splitting a window the changelist position is wrong.
3107" Test the changelist position after splitting a window.
3108" Test for the bug fixed by 7.4.386
3109func Test_changelist()
3110 let save_ul = &ul
3111 enew!
3112 call append('$', ['1', '2'])
3113 exe "normal i\<C-G>u"
3114 exe "normal Gkylpa\<C-G>u"
3115 set ul=100
3116 exe "normal Gylpa\<C-G>u"
3117 set ul=100
3118 normal gg
3119 vsplit
3120 normal g;
3121 call assert_equal([3, 2], [line('.'), col('.')])
3122 normal g;
3123 call assert_equal([2, 2], [line('.'), col('.')])
3124 call assert_fails('normal g;', 'E662:')
Bram Moolenaar1671f442020-03-10 07:48:13 +01003125 new
3126 call assert_fails('normal g;', 'E664:')
Bram Moolenaarfb094e12017-11-05 20:59:28 +01003127 %bwipe!
3128 let &ul = save_ul
3129endfunc
Bram Moolenaar1bbb6192018-11-10 16:02:01 +01003130
3131func Test_nv_hat_count()
3132 %bwipeout!
3133 let l:nr = bufnr('%') + 1
Bram Moolenaare2e40752020-09-04 21:18:46 +02003134 call assert_fails(':execute "normal! ' . l:nr . '\<C-^>"', 'E92:')
Bram Moolenaar1bbb6192018-11-10 16:02:01 +01003135
3136 edit Xfoo
3137 let l:foo_nr = bufnr('Xfoo')
3138
3139 edit Xbar
3140 let l:bar_nr = bufnr('Xbar')
3141
3142 " Make sure we are not just using the alternate file.
3143 edit Xbaz
3144
3145 call feedkeys(l:foo_nr . "\<C-^>", 'tx')
3146 call assert_equal('Xfoo', fnamemodify(bufname('%'), ':t'))
3147
3148 call feedkeys(l:bar_nr . "\<C-^>", 'tx')
3149 call assert_equal('Xbar', fnamemodify(bufname('%'), ':t'))
3150
3151 %bwipeout!
3152endfunc
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01003153
3154func Test_message_when_using_ctrl_c()
Bram Moolenaar553e5a52019-03-25 23:16:34 +01003155 " Make sure no buffers are changed.
3156 %bwipe!
3157
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01003158 exe "normal \<C-C>"
3159 call assert_match("Type :qa and press <Enter> to exit Vim", Screenline(&lines))
Bram Moolenaar553e5a52019-03-25 23:16:34 +01003160
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01003161 new
3162 cal setline(1, 'hi!')
3163 exe "normal \<C-C>"
3164 call assert_match("Type :qa! and press <Enter> to abandon all changes and exit Vim", Screenline(&lines))
Bram Moolenaar553e5a52019-03-25 23:16:34 +01003165
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01003166 bwipe!
3167endfunc
Bram Moolenaarc6b37db2019-04-27 18:00:34 +02003168
3169" Test for '[m', ']m', '[M' and ']M'
3170" Jumping to beginning and end of methods in Java-like languages
3171func Test_java_motion()
3172 new
Bram Moolenaar1671f442020-03-10 07:48:13 +01003173 call assert_beeps('normal! [m')
3174 call assert_beeps('normal! ]m')
3175 call assert_beeps('normal! [M')
3176 call assert_beeps('normal! ]M')
Bram Moolenaar8a9bc952020-10-02 18:48:07 +02003177 let lines =<< trim [CODE]
3178 Piece of Java
3179 {
3180 tt m1 {
3181 t1;
3182 } e1
Bram Moolenaarc6b37db2019-04-27 18:00:34 +02003183
Bram Moolenaar8a9bc952020-10-02 18:48:07 +02003184 tt m2 {
3185 t2;
3186 } e2
Bram Moolenaarc6b37db2019-04-27 18:00:34 +02003187
Bram Moolenaar8a9bc952020-10-02 18:48:07 +02003188 tt m3 {
3189 if (x)
3190 {
3191 t3;
3192 }
3193 } e3
3194 }
3195 [CODE]
3196 call setline(1, lines)
Bram Moolenaarc6b37db2019-04-27 18:00:34 +02003197
3198 normal gg
3199
3200 normal 2]maA
3201 call assert_equal("\ttt m1 {A", getline('.'))
3202 call assert_equal([3, 9, 16], [line('.'), col('.'), virtcol('.')])
3203
3204 normal j]maB
3205 call assert_equal("\ttt m2 {B", getline('.'))
3206 call assert_equal([7, 9, 16], [line('.'), col('.'), virtcol('.')])
3207
3208 normal ]maC
3209 call assert_equal("\ttt m3 {C", getline('.'))
3210 call assert_equal([11, 9, 16], [line('.'), col('.'), virtcol('.')])
3211
3212 normal [maD
3213 call assert_equal("\ttt m3 {DC", getline('.'))
3214 call assert_equal([11, 9, 16], [line('.'), col('.'), virtcol('.')])
3215
3216 normal k2[maE
3217 call assert_equal("\ttt m1 {EA", getline('.'))
3218 call assert_equal([3, 9, 16], [line('.'), col('.'), virtcol('.')])
3219
3220 normal 3[maF
3221 call assert_equal("{F", getline('.'))
3222 call assert_equal([2, 2, 2], [line('.'), col('.'), virtcol('.')])
3223
3224 normal ]MaG
3225 call assert_equal("\t}G e1", getline('.'))
3226 call assert_equal([5, 3, 10], [line('.'), col('.'), virtcol('.')])
3227
3228 normal j2]MaH
3229 call assert_equal("\t}H e3", getline('.'))
3230 call assert_equal([16, 3, 10], [line('.'), col('.'), virtcol('.')])
3231
3232 normal ]M]M
3233 normal aI
3234 call assert_equal("}I", getline('.'))
3235 call assert_equal([17, 2, 2], [line('.'), col('.'), virtcol('.')])
3236
3237 normal 2[MaJ
3238 call assert_equal("\t}JH e3", getline('.'))
3239 call assert_equal([16, 3, 10], [line('.'), col('.'), virtcol('.')])
3240
3241 normal k[MaK
3242 call assert_equal("\t}K e2", getline('.'))
3243 call assert_equal([9, 3, 10], [line('.'), col('.'), virtcol('.')])
3244
3245 normal 3[MaL
3246 call assert_equal("{LF", getline('.'))
3247 call assert_equal([2, 2, 2], [line('.'), col('.'), virtcol('.')])
3248
Bram Moolenaar8a9bc952020-10-02 18:48:07 +02003249 call cursor(2, 1)
3250 call assert_beeps('norm! 5]m')
3251
3252 " jumping to a method in a fold should open the fold
3253 6,10fold
3254 call feedkeys("gg3]m", 'xt')
3255 call assert_equal([7, 8, 15], [line('.'), col('.'), virtcol('.')])
3256 call assert_equal(-1, foldclosedend(7))
3257
Bram Moolenaarc6b37db2019-04-27 18:00:34 +02003258 close!
3259endfunc
Bram Moolenaard5c82342019-07-27 18:44:57 +02003260
Bram Moolenaar004a6782020-04-11 17:09:31 +02003261" Tests for g cmds
Bram Moolenaar1671f442020-03-10 07:48:13 +01003262func Test_normal_gdollar_cmd()
Bram Moolenaar004a6782020-04-11 17:09:31 +02003263 CheckFeature jumplist
Bram Moolenaard5c82342019-07-27 18:44:57 +02003264 call Setup_NewWindow()
3265 " Make long lines that will wrap
3266 %s/$/\=repeat(' foobar', 10)/
3267 20vsp
3268 set wrap
3269 " Test for g$ with count
3270 norm! gg
3271 norm! 0vg$y
3272 call assert_equal(20, col("'>"))
3273 call assert_equal('1 foobar foobar foob', getreg(0))
3274 norm! gg
3275 norm! 0v4g$y
3276 call assert_equal(72, col("'>"))
3277 call assert_equal('1 foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar'.."\n", getreg(0))
3278 norm! gg
3279 norm! 0v6g$y
3280 call assert_equal(40, col("'>"))
3281 call assert_equal('1 foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar'.. "\n"..
3282 \ '2 foobar foobar foobar foobar foobar foo', getreg(0))
3283 set nowrap
3284 " clean up
3285 norm! gg
3286 norm! 0vg$y
3287 call assert_equal(20, col("'>"))
3288 call assert_equal('1 foobar foobar foob', getreg(0))
3289 norm! gg
3290 norm! 0v4g$y
3291 call assert_equal(20, col("'>"))
3292 call assert_equal('1 foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar'.. "\n"..
3293 \ '2 foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar'.. "\n"..
3294 \ '3 foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar'.. "\n"..
3295 \ '4 foobar foobar foob', getreg(0))
3296 norm! gg
3297 norm! 0v6g$y
3298 call assert_equal(20, col("'>"))
3299 call assert_equal('1 foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar'.. "\n"..
3300 \ '2 foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar'.. "\n"..
3301 \ '3 foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar'.. "\n"..
3302 \ '4 foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar'.. "\n"..
3303 \ '5 foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar'.. "\n"..
3304 \ '6 foobar foobar foob', getreg(0))
3305 " Move to last line, also down movement is not possible, should still move
3306 " the cursor to the last visible char
3307 norm! G
3308 norm! 0v6g$y
3309 call assert_equal(20, col("'>"))
3310 call assert_equal('100 foobar foobar fo', getreg(0))
3311 bw!
3312endfunc
Bram Moolenaar03ac52f2019-09-24 22:47:46 +02003313
Bram Moolenaarf5f1e102020-03-08 05:13:15 +01003314func Test_normal_gk_gj()
Bram Moolenaar03ac52f2019-09-24 22:47:46 +02003315 " needs 80 column new window
3316 new
3317 vert 80new
Bram Moolenaarf5f1e102020-03-08 05:13:15 +01003318 call assert_beeps('normal gk')
Bram Moolenaar03ac52f2019-09-24 22:47:46 +02003319 put =[repeat('x',90)..' {{{1', 'x {{{1']
3320 norm! gk
3321 " In a 80 column wide terminal the window will be only 78 char
3322 " (because Vim will leave space for the other window),
3323 " but if the terminal is larger, it will be 80 chars, so verify the
3324 " cursor column correctly.
3325 call assert_equal(winwidth(0)+1, col('.'))
3326 call assert_equal(winwidth(0)+1, virtcol('.'))
3327 norm! j
3328 call assert_equal(6, col('.'))
3329 call assert_equal(6, virtcol('.'))
3330 norm! gk
3331 call assert_equal(95, col('.'))
3332 call assert_equal(95, virtcol('.'))
Bram Moolenaarf5f1e102020-03-08 05:13:15 +01003333 %bw!
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02003334
3335 " needs 80 column new window
3336 new
3337 vert 80new
Bram Moolenaarf5f1e102020-03-08 05:13:15 +01003338 call assert_beeps('normal gj')
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02003339 set number
3340 set numberwidth=10
3341 set cpoptions+=n
3342 put =[repeat('0',90), repeat('1',90)]
3343 norm! 075l
3344 call assert_equal(76, col('.'))
3345 norm! gk
3346 call assert_equal(1, col('.'))
3347 norm! gk
3348 call assert_equal(76, col('.'))
3349 norm! gk
3350 call assert_equal(1, col('.'))
3351 norm! gj
3352 call assert_equal(76, col('.'))
3353 norm! gj
3354 call assert_equal(1, col('.'))
3355 norm! gj
3356 call assert_equal(76, col('.'))
Bram Moolenaarf5f1e102020-03-08 05:13:15 +01003357 " When 'nowrap' is set, gk and gj behave like k and j
3358 set nowrap
3359 normal! gk
3360 call assert_equal([2, 76], [line('.'), col('.')])
3361 normal! gj
3362 call assert_equal([3, 76], [line('.'), col('.')])
3363 %bw!
3364 set cpoptions& number& numberwidth& wrap&
Bram Moolenaar03ac52f2019-09-24 22:47:46 +02003365endfunc
Bram Moolenaarf0cee192020-02-16 13:33:56 +01003366
Bram Moolenaar818fc9a2020-02-21 17:54:45 +01003367" Test for using : to run a multi-line Ex command in operator pending mode
3368func Test_normal_yank_with_excmd()
3369 new
3370 call setline(1, ['foo', 'bar', 'baz'])
3371 let @a = ''
3372 call feedkeys("\"ay:if v:true\<CR>normal l\<CR>endif\<CR>", 'xt')
3373 call assert_equal('f', @a)
3374 close!
3375endfunc
Bram Moolenaarf5f1e102020-03-08 05:13:15 +01003376
3377" Test for supplying a count to a normal-mode command across a cursorhold call
3378func Test_normal_cursorhold_with_count()
3379 func s:cHold()
3380 let g:cHold_Called += 1
3381 endfunc
3382 new
3383 augroup normalcHoldTest
3384 au!
3385 au CursorHold <buffer> call s:cHold()
3386 augroup END
3387 let g:cHold_Called = 0
3388 call feedkeys("3\<CursorHold>2ix", 'xt')
3389 call assert_equal(1, g:cHold_Called)
3390 call assert_equal(repeat('x', 32), getline(1))
3391 augroup normalcHoldTest
3392 au!
3393 augroup END
3394 au! normalcHoldTest
3395 close!
3396 delfunc s:cHold
3397endfunc
3398
3399" Test for using a count and a command with CTRL-W
3400func Test_wincmd_with_count()
3401 call feedkeys("\<C-W>12n", 'xt')
3402 call assert_equal(12, winheight(0))
3403endfunc
3404
3405" Test for 'b', 'B' 'ge' and 'gE' commands
Bram Moolenaar1671f442020-03-10 07:48:13 +01003406func Test_horiz_motion()
3407 new
Bram Moolenaarf5f1e102020-03-08 05:13:15 +01003408 normal! gg
3409 call assert_beeps('normal! b')
3410 call assert_beeps('normal! B')
3411 call assert_beeps('normal! gE')
3412 call assert_beeps('normal! ge')
Bram Moolenaar1671f442020-03-10 07:48:13 +01003413 " <S-Backspace> moves one word left and <C-Backspace> moves one WORD left
3414 call setline(1, 'one ,two ,three')
3415 exe "normal! $\<S-BS>"
3416 call assert_equal(11, col('.'))
3417 exe "normal! $\<C-BS>"
3418 call assert_equal(10, col('.'))
3419 close!
3420endfunc
3421
3422" Test for using a : command in operator pending mode
3423func Test_normal_colon_op()
3424 new
3425 call setline(1, ['one', 'two'])
3426 call assert_beeps("normal! Gc:d\<CR>")
3427 close!
Bram Moolenaarf5f1e102020-03-08 05:13:15 +01003428endfunc
3429
Bram Moolenaar004a6782020-04-11 17:09:31 +02003430" Test for d and D commands
3431func Test_normal_delete_cmd()
3432 new
3433 " D in an empty line
3434 call setline(1, '')
3435 normal D
3436 call assert_equal('', getline(1))
3437 " D in an empty line in virtualedit mode
3438 set virtualedit=all
3439 normal D
3440 call assert_equal('', getline(1))
3441 set virtualedit&
3442 " delete to a readonly register
3443 call setline(1, ['abcd'])
3444 call assert_beeps('normal ":d2l')
Bram Moolenaar6fd367a2021-03-13 13:14:04 +01003445
3446 " D and d with 'nomodifiable'
3447 call setline(1, ['abcd'])
3448 setlocal nomodifiable
3449 call assert_fails('normal D', 'E21:')
3450 call assert_fails('normal d$', 'E21:')
3451
Bram Moolenaar004a6782020-04-11 17:09:31 +02003452 close!
3453endfunc
3454
Bram Moolenaar8a9bc952020-10-02 18:48:07 +02003455" Test for deleting or changing characters across lines with 'whichwrap'
3456" containing 's'. Should count <EOL> as one character.
3457func Test_normal_op_across_lines()
3458 new
3459 set whichwrap&
3460 call setline(1, ['one two', 'three four'])
3461 exe "norm! $3d\<Space>"
3462 call assert_equal(['one twhree four'], getline(1, '$'))
3463
3464 call setline(1, ['one two', 'three four'])
3465 exe "norm! $3c\<Space>x"
3466 call assert_equal(['one twxhree four'], getline(1, '$'))
3467
3468 set whichwrap+=l
3469 call setline(1, ['one two', 'three four'])
3470 exe "norm! $3x"
3471 call assert_equal(['one twhree four'], getline(1, '$'))
3472 close!
3473 set whichwrap&
3474endfunc
3475
Bram Moolenaar224a5f12020-04-28 20:29:07 +02003476" Test for 'w' and 'b' commands
3477func Test_normal_word_move()
3478 new
3479 call setline(1, ['foo bar a', '', 'foo bar b'])
3480 " copy a single character word at the end of a line
3481 normal 1G$yw
3482 call assert_equal('a', @")
3483 " copy a single character word at the end of a file
3484 normal G$yw
3485 call assert_equal('b', @")
3486 " check for a word movement handling an empty line properly
3487 normal 1G$vwy
3488 call assert_equal("a\n\n", @")
3489
3490 " copy using 'b' command
3491 %d
3492 " non-empty blank line at the start of file
3493 call setline(1, [' ', 'foo bar'])
3494 normal 2Gyb
3495 call assert_equal(" \n", @")
3496 " try to copy backwards from the start of the file
3497 call setline(1, ['one two', 'foo bar'])
3498 call assert_beeps('normal ggyb')
3499 " 'b' command should stop at an empty line
3500 call setline(1, ['one two', '', 'foo bar'])
3501 normal 3Gyb
3502 call assert_equal("\n", @")
3503 normal 3Gy2b
3504 call assert_equal("two\n", @")
3505 " 'b' command should not stop at a non-empty blank line
3506 call setline(1, ['one two', ' ', 'foo bar'])
3507 normal 3Gyb
3508 call assert_equal("two\n ", @")
3509
3510 close!
3511endfunc
3512
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02003513" Test for 'scrolloff' with a long line that doesn't fit in the screen
3514func Test_normal_scroloff()
3515 10new
3516 80vnew
3517 call setline(1, repeat('a', 1000))
3518 set scrolloff=10
3519 normal gg10gj
3520 call assert_equal(8, winline())
3521 normal 10gj
3522 call assert_equal(10, winline())
3523 normal 10gk
3524 call assert_equal(3, winline())
3525 set scrolloff&
3526 close!
3527endfunc
3528
3529" Test for vertical scrolling with CTRL-F and CTRL-B with a long line
3530func Test_normal_vert_scroll_longline()
3531 10new
3532 80vnew
3533 call setline(1, range(1, 10))
3534 call append(5, repeat('a', 1000))
3535 exe "normal gg\<C-F>"
3536 call assert_equal(6, line('.'))
3537 exe "normal \<C-F>\<C-F>"
3538 call assert_equal(11, line('.'))
3539 call assert_equal(1, winline())
3540 exe "normal \<C-B>"
3541 call assert_equal(10, line('.'))
3542 call assert_equal(3, winline())
3543 exe "normal \<C-B>\<C-B>"
3544 call assert_equal(5, line('.'))
3545 call assert_equal(5, winline())
3546 close!
3547endfunc
3548
Bram Moolenaar8a9bc952020-10-02 18:48:07 +02003549" Test for jumping in a file using %
3550func Test_normal_percent_jump()
3551 new
3552 call setline(1, range(1, 100))
3553
3554 " jumping to a folded line should open the fold
3555 25,75fold
3556 call feedkeys('50%', 'xt')
3557 call assert_equal(50, line('.'))
3558 call assert_equal(-1, foldclosedend(50))
3559 close!
3560endfunc
3561
Bram Moolenaar3e72dca2021-05-29 16:30:12 +02003562" Test for << and >> commands to shift text by 'shiftwidth'
3563func Test_normal_shift_rightleft()
3564 new
3565 call setline(1, ['one', '', "\t", ' two', "\tthree", ' four'])
3566 set shiftwidth=2 tabstop=8
3567 normal gg6>>
3568 call assert_equal([' one', '', "\t ", ' two', "\t three", "\tfour"],
3569 \ getline(1, '$'))
3570 normal ggVG2>>
3571 call assert_equal([' one', '', "\t ", "\ttwo",
3572 \ "\t three", "\t four"], getline(1, '$'))
3573 normal gg6<<
3574 call assert_equal([' one', '', "\t ", ' two', "\t three",
3575 \ "\t four"], getline(1, '$'))
3576 normal ggVG2<<
3577 call assert_equal(['one', '', "\t", ' two', "\tthree", ' four'],
3578 \ getline(1, '$'))
3579 set shiftwidth& tabstop&
3580 bw!
3581endfunc
3582
Yegappan Lakshmanan2ac71842021-05-31 19:23:01 +02003583" Some commands like yy, cc, dd, >>, << and !! accept a count after
3584" typing the first letter of the command.
3585func Test_normal_count_after_operator()
3586 new
3587 setlocal shiftwidth=4 tabstop=8 autoindent
3588 call setline(1, ['one', 'two', 'three', 'four', 'five'])
3589 let @a = ''
3590 normal! j"ay4y
3591 call assert_equal("two\nthree\nfour\nfive\n", @a)
3592 normal! 3G>2>
3593 call assert_equal(['one', 'two', ' three', ' four', 'five'],
3594 \ getline(1, '$'))
3595 exe "normal! 3G0c2cred\nblue"
3596 call assert_equal(['one', 'two', ' red', ' blue', 'five'],
3597 \ getline(1, '$'))
3598 exe "normal! gg<8<"
3599 call assert_equal(['one', 'two', 'red', 'blue', 'five'],
3600 \ getline(1, '$'))
3601 exe "normal! ggd3d"
3602 call assert_equal(['blue', 'five'], getline(1, '$'))
3603 call setline(1, range(1, 4))
3604 call feedkeys("gg!3!\<C-B>\"\<CR>", 'xt')
3605 call assert_equal('".,.+2!', @:)
3606 call feedkeys("gg!1!\<C-B>\"\<CR>", 'xt')
3607 call assert_equal('".!', @:)
3608 call feedkeys("gg!9!\<C-B>\"\<CR>", 'xt')
3609 call assert_equal('".,$!', @:)
3610 bw!
3611endfunc
3612
Christian Brabandtaaec1d42021-11-04 13:28:29 +00003613func Test_normal_gj_on_extra_wide_char()
3614 new | 25vsp
3615 let text='1 foooooooo ar e ins‍zwe1 foooooooo ins‍zwei' .
3616 \ ' i drei vier fünf sechs sieben acht un zehn elf zwöfl' .
3617 \ ' dreizehn v ierzehn fünfzehn'
3618 put =text
3619 call cursor(2,1)
3620 norm! gj
3621 call assert_equal([0,2,25,0], getpos('.'))
3622 bw!
3623endfunc
3624
Bram Moolenaar03725c52021-11-24 12:17:53 +00003625func Test_normal_count_out_of_range()
3626 new
3627 call setline(1, 'text')
3628 normal 44444444444|
3629 call assert_equal(999999999, v:count)
3630 normal 444444444444|
3631 call assert_equal(999999999, v:count)
3632 normal 4444444444444|
3633 call assert_equal(999999999, v:count)
3634 normal 4444444444444444444|
3635 call assert_equal(999999999, v:count)
3636
3637 normal 9y99999999|
3638 call assert_equal(899999991, v:count)
3639 normal 10y99999999|
3640 call assert_equal(999999999, v:count)
3641 normal 44444444444y44444444444|
3642 call assert_equal(999999999, v:count)
3643 bwipe!
3644endfunc
3645
Bram Moolenaarf5f1e102020-03-08 05:13:15 +01003646" vim: shiftwidth=2 sts=2 expandtab