xref: /vim-8.2.3635/src/testdir/test_normal.vim (revision 2c5c1638)
1" Test for various Normal mode commands
2
3source shared.vim
4source check.vim
5source view_util.vim
6
7func Setup_NewWindow()
8  10new
9  call setline(1, range(1,100))
10endfunc
11
12func MyFormatExpr()
13  " Adds '->$' at lines having numbers followed by trailing whitespace
14  for ln in range(v:lnum, v:lnum+v:count-1)
15    let line = getline(ln)
16    if getline(ln) =~# '\d\s\+$'
17      call setline(ln, substitute(line, '\s\+$', '', '') . '->$')
18    endif
19  endfor
20endfunc
21
22func CountSpaces(type, ...)
23  " for testing operatorfunc
24  " will count the number of spaces
25  " and return the result in g:a
26  let sel_save = &selection
27  let &selection = "inclusive"
28  let reg_save = @@
29
30  if a:0  " Invoked from Visual mode, use gv command.
31    silent exe "normal! gvy"
32  elseif a:type == 'line'
33    silent exe "normal! '[V']y"
34  else
35    silent exe "normal! `[v`]y"
36  endif
37  let g:a=strlen(substitute(@@, '[^ ]', '', 'g'))
38  let &selection = sel_save
39  let @@ = reg_save
40endfunc
41
42func OpfuncDummy(type, ...)
43  " for testing operatorfunc
44  let g:opt=&linebreak
45
46  if a:0  " Invoked from Visual mode, use gv command.
47    silent exe "normal! gvy"
48  elseif a:type == 'line'
49    silent exe "normal! '[V']y"
50  else
51    silent exe "normal! `[v`]y"
52  endif
53  " Create a new dummy window
54  new
55  let g:bufnr=bufnr('%')
56endfunc
57
58func Test_normal00_optrans()
59  new
60  call append(0, ['1 This is a simple test: abcd', '2 This is the second line', '3 this is the third line'])
61  1
62  exe "norm! Sfoobar\<esc>"
63  call assert_equal(['foobar', '2 This is the second line', '3 this is the third line', ''], getline(1,'$'))
64  2
65  exe "norm! $vbsone"
66  call assert_equal(['foobar', '2 This is the second one', '3 this is the third line', ''], getline(1,'$'))
67  norm! VS Second line here
68  call assert_equal(['foobar', ' Second line here', '3 this is the third line', ''], getline(1, '$'))
69  %d
70  call append(0, ['4 This is a simple test: abcd', '5 This is the second line', '6 this is the third line'])
71  call append(0, ['1 This is a simple test: abcd', '2 This is the second line', '3 this is the third line'])
72
73  1
74  norm! 2D
75  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,'$'))
76  set cpo+=#
77  norm! 4D
78  call assert_equal(['', '4 This is a simple test: abcd', '5 This is the second line', '6 this is the third line', ''], getline(1,'$'))
79
80  " clean up
81  set cpo-=#
82  bw!
83endfunc
84
85func Test_normal01_keymodel()
86  call Setup_NewWindow()
87  " Test 1: depending on 'keymodel' <s-down> does something different
88  50
89  call feedkeys("V\<S-Up>y", 'tx')
90  call assert_equal(['47', '48', '49', '50'], getline("'<", "'>"))
91  set keymodel=startsel
92  50
93  call feedkeys("V\<S-Up>y", 'tx')
94  call assert_equal(['49', '50'], getline("'<", "'>"))
95  " Start visual mode when keymodel = startsel
96  50
97  call feedkeys("\<S-Up>y", 'tx')
98  call assert_equal(['49', '5'], getreg(0, 0, 1))
99  " Use the different Shift special keys
100  50
101  call feedkeys("\<S-Right>\<S-Left>\<S-Up>\<S-Down>\<S-Home>\<S-End>y", 'tx')
102  call assert_equal(['50'], getline("'<", "'>"))
103  call assert_equal(['50', ''], getreg(0, 0, 1))
104
105  " Do not start visual mode when keymodel=
106  set keymodel=
107  50
108  call feedkeys("\<S-Up>y$", 'tx')
109  call assert_equal(['42'], getreg(0, 0, 1))
110  " Stop visual mode when keymodel=stopsel
111  set keymodel=stopsel
112  50
113  call feedkeys("Vkk\<Up>yy", 'tx')
114  call assert_equal(['47'], getreg(0, 0, 1))
115
116  set keymodel=
117  50
118  call feedkeys("Vkk\<Up>yy", 'tx')
119  call assert_equal(['47', '48', '49', '50'], getreg(0, 0, 1))
120
121  " clean up
122  bw!
123endfunc
124
125func Test_normal03_join()
126  " basic join test
127  call Setup_NewWindow()
128  50
129  norm! VJ
130  call assert_equal('50 51', getline('.'))
131  $
132  norm! J
133  call assert_equal('100', getline('.'))
134  $
135  norm! V9-gJ
136  call assert_equal('919293949596979899100', getline('.'))
137  call setline(1, range(1,100))
138  $
139  :j 10
140  call assert_equal('100', getline('.'))
141  call assert_beeps('normal GVJ')
142  " clean up
143  bw!
144endfunc
145
146" basic filter test
147func Test_normal04_filter()
148  " only test on non windows platform
149  CheckNotMSWindows
150  call Setup_NewWindow()
151  1
152  call feedkeys("!!sed -e 's/^/|    /'\n", 'tx')
153  call assert_equal('|    1', getline('.'))
154  90
155  :sil :!echo one
156  call feedkeys('.', 'tx')
157  call assert_equal('|    90', getline('.'))
158  95
159  set cpo+=!
160  " 2 <CR>, 1: for executing the command,
161  "         2: clear hit-enter-prompt
162  call feedkeys("!!\n", 'tx')
163  call feedkeys(":!echo one\n\n", 'tx')
164  call feedkeys(".", 'tx')
165  call assert_equal('one', getline('.'))
166  set cpo-=!
167  bw!
168endfunc
169
170func Test_normal05_formatexpr()
171  " basic formatexpr test
172  call Setup_NewWindow()
173  %d_
174  call setline(1, ['here: 1   ', '2', 'here: 3   ', '4', 'not here:   '])
175  1
176  set formatexpr=MyFormatExpr()
177  norm! gqG
178  call assert_equal(['here: 1->$', '2', 'here: 3->$', '4', 'not here:   '], getline(1,'$'))
179  set formatexpr=
180  bw!
181endfunc
182
183func Test_normal05_formatexpr_newbuf()
184  " Edit another buffer in the 'formatexpr' function
185  new
186  func! Format()
187    edit another
188  endfunc
189  set formatexpr=Format()
190  norm gqG
191  bw!
192  set formatexpr=
193endfunc
194
195func Test_normal05_formatexpr_setopt()
196  " Change the 'formatexpr' value in the function
197  new
198  func! Format()
199    set formatexpr=
200  endfunc
201  set formatexpr=Format()
202  norm gqG
203  bw!
204  set formatexpr=
205endfunc
206
207" basic test for formatprg
208func Test_normal06_formatprg()
209  " only test on non windows platform
210  CheckNotMSWindows
211
212  " uses sed to number non-empty lines
213  call writefile(['#!/bin/sh', 'sed ''/./=''|sed ''/./{', 'N', 's/\n/    /', '}'''], 'Xsed_format.sh')
214  call system('chmod +x ./Xsed_format.sh')
215  let text = ['a', '', 'c', '', ' ', 'd', 'e']
216  let expected = ['1    a', '', '3    c', '', '5     ', '6    d', '7    e']
217
218  10new
219  call setline(1, text)
220  set formatprg=./Xsed_format.sh
221  norm! gggqG
222  call assert_equal(expected, getline(1, '$'))
223  %d
224
225  call setline(1, text)
226  set formatprg=donothing
227  setlocal formatprg=./Xsed_format.sh
228  norm! gggqG
229  call assert_equal(expected, getline(1, '$'))
230  %d
231
232  " Check for the command-line ranges added to 'formatprg'
233  set formatprg=cat
234  call setline(1, ['one', 'two', 'three', 'four', 'five'])
235  call feedkeys('gggqG', 'xt')
236  call assert_equal('.,$!cat', @:)
237  call feedkeys('2Ggq2j', 'xt')
238  call assert_equal('.,.+2!cat', @:)
239
240  bw!
241  " clean up
242  set formatprg=
243  setlocal formatprg=
244  call delete('Xsed_format.sh')
245endfunc
246
247func Test_normal07_internalfmt()
248  " basic test for internal formmatter to textwidth of 12
249  let list=range(1,11)
250  call map(list, 'v:val."    "')
251  10new
252  call setline(1, list)
253  set tw=12
254  norm! ggVGgq
255  call assert_equal(['1    2    3', '4    5    6', '7    8    9', '10    11    '], getline(1, '$'))
256  " clean up
257  set tw=0
258  bw!
259endfunc
260
261" basic tests for foldopen/folddelete
262func Test_normal08_fold()
263  CheckFeature folding
264  call Setup_NewWindow()
265  50
266  setl foldenable fdm=marker
267  " First fold
268  norm! V4jzf
269  " check that folds have been created
270  call assert_equal(['50/*{{{*/', '51', '52', '53', '54/*}}}*/'], getline(50,54))
271  " Second fold
272  46
273  norm! V10jzf
274  " check that folds have been created
275  call assert_equal('46/*{{{*/', getline(46))
276  call assert_equal('60/*}}}*/', getline(60))
277  norm! k
278  call assert_equal('45', getline('.'))
279  norm! j
280  call assert_equal('46/*{{{*/', getline('.'))
281  norm! j
282  call assert_equal('61', getline('.'))
283  norm! k
284  " open a fold
285  norm! Vzo
286  norm! k
287  call assert_equal('45', getline('.'))
288  norm! j
289  call assert_equal('46/*{{{*/', getline('.'))
290  norm! j
291  call assert_equal('47', getline('.'))
292  norm! k
293  norm! zcVzO
294  call assert_equal('46/*{{{*/', getline('.'))
295  norm! j
296  call assert_equal('47', getline('.'))
297  norm! j
298  call assert_equal('48', getline('.'))
299  norm! j
300  call assert_equal('49', getline('.'))
301  norm! j
302  call assert_equal('50/*{{{*/', getline('.'))
303  norm! j
304  call assert_equal('51', getline('.'))
305  " delete folds
306  :46
307  " collapse fold
308  norm! V14jzC
309  " delete all folds recursively
310  norm! VzD
311  call assert_equal(['46', '47', '48', '49', '50', '51', '52', '53', '54', '55', '56', '57', '58', '59', '60'], getline(46,60))
312
313  " clean up
314  setl nofoldenable fdm=marker
315  bw!
316endfunc
317
318func Test_normal09_operatorfunc()
319  " Test operatorfunc
320  call Setup_NewWindow()
321  " Add some spaces for counting
322  50,60s/$/  /
323  unlet! g:a
324  let g:a=0
325  nmap <buffer><silent> ,, :set opfunc=CountSpaces<CR>g@
326  vmap <buffer><silent> ,, :<C-U>call CountSpaces(visualmode(), 1)<CR>
327  50
328  norm V2j,,
329  call assert_equal(6, g:a)
330  norm V,,
331  call assert_equal(2, g:a)
332  norm ,,l
333  call assert_equal(0, g:a)
334  50
335  exe "norm 0\<c-v>10j2l,,"
336  call assert_equal(11, g:a)
337  50
338  norm V10j,,
339  call assert_equal(22, g:a)
340
341  " clean up
342  unmap <buffer> ,,
343  set opfunc=
344  unlet! g:a
345  bw!
346endfunc
347
348func Test_normal09a_operatorfunc()
349  " Test operatorfunc
350  call Setup_NewWindow()
351  " Add some spaces for counting
352  50,60s/$/  /
353  unlet! g:opt
354  set linebreak
355  nmap <buffer><silent> ,, :set opfunc=OpfuncDummy<CR>g@
356  50
357  norm ,,j
358  exe "bd!" g:bufnr
359  call assert_true(&linebreak)
360  call assert_equal(g:opt, &linebreak)
361  set nolinebreak
362  norm ,,j
363  exe "bd!" g:bufnr
364  call assert_false(&linebreak)
365  call assert_equal(g:opt, &linebreak)
366
367  " clean up
368  unmap <buffer> ,,
369  set opfunc=
370  call assert_fails('normal Vg@', 'E774:')
371  bw!
372  unlet! g:opt
373endfunc
374
375func Test_normal10_expand()
376  " Test for expand()
377  10new
378  call setline(1, ['1', 'ifooar,,cbar'])
379  2
380  norm! $
381  call assert_equal('cbar', expand('<cword>'))
382  call assert_equal('ifooar,,cbar', expand('<cWORD>'))
383
384  call setline(1, ['prx = list[idx];'])
385  1
386  let expected = ['', 'prx', 'prx', 'prx',
387	\ 'list', 'list', 'list', 'list', 'list', 'list', 'list',
388	\ 'idx', 'idx', 'idx', 'idx',
389	\ 'list[idx]',
390	\ '];',
391	\ ]
392  for i in range(1, 16)
393    exe 'norm ' . i . '|'
394    call assert_equal(expected[i], expand('<cexpr>'), 'i == ' . i)
395  endfor
396
397  if executable('echo')
398    " Test expand(`...`) i.e. backticks command expansion.
399    call assert_equal('abcde', expand('`echo abcde`'))
400  endif
401
402  " Test expand(`=...`) i.e. backticks expression expansion
403  call assert_equal('5', expand('`=2+3`'))
404  call assert_equal('3.14', expand('`=3.14`'))
405
406  " clean up
407  bw!
408endfunc
409
410func Test_normal11_showcmd()
411  " test for 'showcmd'
412  10new
413  exe "norm! ofoobar\<esc>"
414  call assert_equal(2, line('$'))
415  set showcmd
416  exe "norm! ofoobar2\<esc>"
417  call assert_equal(3, line('$'))
418  exe "norm! VAfoobar3\<esc>"
419  call assert_equal(3, line('$'))
420  exe "norm! 0d3\<del>2l"
421  call assert_equal('obar2foobar3', getline('.'))
422  bw!
423endfunc
424
425" Test for nv_error and normal command errors
426func Test_normal12_nv_error()
427  10new
428  call setline(1, range(1,5))
429  " should not do anything, just beep
430  call assert_beeps('exe "norm! <c-k>"')
431  call assert_equal(map(range(1,5), 'string(v:val)'), getline(1,'$'))
432  call assert_beeps('normal! G2dd')
433  call assert_beeps("normal! g\<C-A>")
434  call assert_beeps("normal! g\<C-X>")
435  call assert_beeps("normal! g\<C-B>")
436  call assert_beeps("normal! vQ\<Esc>")
437  call assert_beeps("normal! 2[[")
438  call assert_beeps("normal! 2]]")
439  call assert_beeps("normal! 2[]")
440  call assert_beeps("normal! 2][")
441  call assert_beeps("normal! 4[z")
442  call assert_beeps("normal! 4]z")
443  call assert_beeps("normal! 4[c")
444  call assert_beeps("normal! 4]c")
445  call assert_beeps("normal! 200%")
446  call assert_beeps("normal! %")
447  call assert_beeps("normal! 2{")
448  call assert_beeps("normal! 2}")
449  call assert_beeps("normal! r\<Right>")
450  call assert_beeps("normal! 8ry")
451  call assert_beeps('normal! "@')
452  bw!
453endfunc
454
455func Test_normal13_help()
456  " Test for F1
457  call assert_equal(1, winnr())
458  call feedkeys("\<f1>", 'txi')
459  call assert_match('help\.txt', bufname('%'))
460  call assert_equal(2, winnr('$'))
461  bw!
462endfunc
463
464func Test_normal14_page()
465  " basic test for Ctrl-F and Ctrl-B
466  call Setup_NewWindow()
467  exe "norm! \<c-f>"
468  call assert_equal('9', getline('.'))
469  exe "norm! 2\<c-f>"
470  call assert_equal('25', getline('.'))
471  exe "norm! 2\<c-b>"
472  call assert_equal('18', getline('.'))
473  1
474  set scrolloff=5
475  exe "norm! 2\<c-f>"
476  call assert_equal('21', getline('.'))
477  exe "norm! \<c-b>"
478  call assert_equal('13', getline('.'))
479  1
480  set scrolloff=99
481  exe "norm! \<c-f>"
482  call assert_equal('13', getline('.'))
483  set scrolloff=0
484  100
485  exe "norm! $\<c-b>"
486  call assert_equal('92', getline('.'))
487  call assert_equal([0, 92, 1, 0, 1], getcurpos())
488  100
489  set nostartofline
490  exe "norm! $\<c-b>"
491  call assert_equal('92', getline('.'))
492  call assert_equal([0, 92, 2, 0, 2147483647], getcurpos())
493  " cleanup
494  set startofline
495  bw!
496endfunc
497
498func Test_normal14_page_eol()
499  10new
500  norm oxxxxxxx
501  exe "norm 2\<c-f>"
502  " check with valgrind that cursor is put back in column 1
503  exe "norm 2\<c-b>"
504  bw!
505endfunc
506
507" Test for errors with z command
508func Test_normal_z_error()
509  call assert_beeps('normal! z2p')
510  call assert_beeps('normal! zp')
511endfunc
512
513func Test_normal15_z_scroll_vert()
514  " basic test for z commands that scroll the window
515  call Setup_NewWindow()
516  100
517  norm! >>
518  " Test for z<cr>
519  exe "norm! z\<cr>"
520  call assert_equal('	100', getline('.'))
521  call assert_equal(100, winsaveview()['topline'])
522  call assert_equal([0, 100, 2, 0, 9], getcurpos())
523
524  " Test for zt
525  21
526  norm! >>0zt
527  call assert_equal('	21', getline('.'))
528  call assert_equal(21, winsaveview()['topline'])
529  call assert_equal([0, 21, 1, 0, 8], getcurpos())
530
531  " Test for zb
532  30
533  norm! >>$ztzb
534  call assert_equal('	30', getline('.'))
535  call assert_equal(30, winsaveview()['topline']+winheight(0)-1)
536  call assert_equal([0, 30, 3, 0, 2147483647], getcurpos())
537
538  " Test for z-
539  1
540  30
541  norm! 0z-
542  call assert_equal('	30', getline('.'))
543  call assert_equal(30, winsaveview()['topline']+winheight(0)-1)
544  call assert_equal([0, 30, 2, 0, 9], getcurpos())
545
546  " Test for z{height}<cr>
547  call assert_equal(10, winheight(0))
548  exe "norm! z12\<cr>"
549  call assert_equal(12, winheight(0))
550  exe "norm! z10\<cr>"
551  call assert_equal(10, winheight(0))
552
553  " Test for z.
554  1
555  21
556  norm! 0z.
557  call assert_equal('	21', getline('.'))
558  call assert_equal(17, winsaveview()['topline'])
559  call assert_equal([0, 21, 2, 0, 9], getcurpos())
560
561  " Test for zz
562  1
563  21
564  norm! 0zz
565  call assert_equal('	21', getline('.'))
566  call assert_equal(17, winsaveview()['topline'])
567  call assert_equal([0, 21, 1, 0, 8], getcurpos())
568
569  " Test for z+
570  11
571  norm! zt
572  norm! z+
573  call assert_equal('	21', getline('.'))
574  call assert_equal(21, winsaveview()['topline'])
575  call assert_equal([0, 21, 2, 0, 9], getcurpos())
576
577  " Test for [count]z+
578  1
579  norm! 21z+
580  call assert_equal('	21', getline('.'))
581  call assert_equal(21, winsaveview()['topline'])
582  call assert_equal([0, 21, 2, 0, 9], getcurpos())
583
584  " Test for z^
585  norm! 22z+0
586  norm! z^
587  call assert_equal('	21', getline('.'))
588  call assert_equal(12, winsaveview()['topline'])
589  call assert_equal([0, 21, 2, 0, 9], getcurpos())
590
591  " Test for [count]z^
592  1
593  norm! 30z^
594  call assert_equal('	21', getline('.'))
595  call assert_equal(12, winsaveview()['topline'])
596  call assert_equal([0, 21, 2, 0, 9], getcurpos())
597
598  " cleanup
599  bw!
600endfunc
601
602func Test_normal16_z_scroll_hor()
603  " basic test for z commands that scroll the window
604  10new
605  15vsp
606  set nowrap listchars=
607  let lineA='abcdefghijklmnopqrstuvwxyz'
608  let lineB='0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'
609  $put =lineA
610  $put =lineB
611  1d
612
613  " Test for zl and zh with a count
614  norm! 0z10l
615  call assert_equal([11, 1], [col('.'), wincol()])
616  norm! z4h
617  call assert_equal([11, 5], [col('.'), wincol()])
618  normal! 2gg
619
620  " Test for zl
621  1
622  norm! 5zl
623  call assert_equal(lineA, getline('.'))
624  call assert_equal(6, col('.'))
625  call assert_equal(5, winsaveview()['leftcol'])
626  norm! yl
627  call assert_equal('f', @0)
628
629  " Test for zh
630  norm! 2zh
631  call assert_equal(lineA, getline('.'))
632  call assert_equal(6, col('.'))
633  norm! yl
634  call assert_equal('f', @0)
635  call assert_equal(3, winsaveview()['leftcol'])
636
637  " Test for zL
638  norm! zL
639  call assert_equal(11, col('.'))
640  norm! yl
641  call assert_equal('k', @0)
642  call assert_equal(10, winsaveview()['leftcol'])
643  norm! 2zL
644  call assert_equal(25, col('.'))
645  norm! yl
646  call assert_equal('y', @0)
647  call assert_equal(24, winsaveview()['leftcol'])
648
649  " Test for zH
650  norm! 2zH
651  call assert_equal(25, col('.'))
652  call assert_equal(10, winsaveview()['leftcol'])
653  norm! yl
654  call assert_equal('y', @0)
655
656  " Test for zs
657  norm! $zs
658  call assert_equal(26, col('.'))
659  call assert_equal(25, winsaveview()['leftcol'])
660  norm! yl
661  call assert_equal('z', @0)
662
663  " Test for ze
664  norm! ze
665  call assert_equal(26, col('.'))
666  call assert_equal(11, winsaveview()['leftcol'])
667  norm! yl
668  call assert_equal('z', @0)
669
670  " cleanup
671  set wrap listchars=eol:$
672  bw!
673endfunc
674
675func Test_normal17_z_scroll_hor2()
676  " basic test for z commands that scroll the window
677  " using 'sidescrolloff' setting
678  10new
679  20vsp
680  set nowrap listchars= sidescrolloff=5
681  let lineA='abcdefghijklmnopqrstuvwxyz'
682  let lineB='0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'
683  $put =lineA
684  $put =lineB
685  1d
686
687  " Test for zl
688  1
689  norm! 5zl
690  call assert_equal(lineA, getline('.'))
691  call assert_equal(11, col('.'))
692  call assert_equal(5, winsaveview()['leftcol'])
693  norm! yl
694  call assert_equal('k', @0)
695
696  " Test for zh
697  norm! 2zh
698  call assert_equal(lineA, getline('.'))
699  call assert_equal(11, col('.'))
700  norm! yl
701  call assert_equal('k', @0)
702  call assert_equal(3, winsaveview()['leftcol'])
703
704  " Test for zL
705  norm! 0zL
706  call assert_equal(16, col('.'))
707  norm! yl
708  call assert_equal('p', @0)
709  call assert_equal(10, winsaveview()['leftcol'])
710  norm! 2zL
711  call assert_equal(26, col('.'))
712  norm! yl
713  call assert_equal('z', @0)
714  call assert_equal(15, winsaveview()['leftcol'])
715
716  " Test for zH
717  norm! 2zH
718  call assert_equal(15, col('.'))
719  call assert_equal(0, winsaveview()['leftcol'])
720  norm! yl
721  call assert_equal('o', @0)
722
723  " Test for zs
724  norm! $zs
725  call assert_equal(26, col('.'))
726  call assert_equal(20, winsaveview()['leftcol'])
727  norm! yl
728  call assert_equal('z', @0)
729
730  " Test for ze
731  norm! ze
732  call assert_equal(26, col('.'))
733  call assert_equal(11, winsaveview()['leftcol'])
734  norm! yl
735  call assert_equal('z', @0)
736
737  " cleanup
738  set wrap listchars=eol:$ sidescrolloff=0
739  bw!
740endfunc
741
742" Test for H, M and L commands with folds
743func Test_scroll_cmds()
744  15new
745  call setline(1, range(1, 100))
746  exe "normal! 30ggz\<CR>"
747  set foldenable
748  33,36fold
749  40,43fold
750  46,49fold
751  let h = winheight(0)
752  " Top of the screen = 30
753  " Folded lines = 9
754  " Bottom of the screen = 30 + h + 9 - 1
755  normal! 4L
756  call assert_equal(35 + h, line('.'))
757  normal! 4H
758  call assert_equal(33, line('.'))
759  set foldenable&
760  close!
761endfunc
762
763" basic tests for foldopen/folddelete
764func Test_normal18_z_fold()
765  CheckFeature folding
766  call Setup_NewWindow()
767  50
768  setl foldenable fdm=marker foldlevel=5
769
770  call assert_beeps('normal! zj')
771  call assert_beeps('normal! zk')
772
773  " Test for zF
774  " First fold
775  norm! 4zF
776  " check that folds have been created
777  call assert_equal(['50/*{{{*/', '51', '52', '53/*}}}*/'], getline(50,53))
778
779  " Test for zd
780  51
781  norm! 2zF
782  call assert_equal(2, foldlevel('.'))
783  norm! kzd
784  call assert_equal(['50', '51/*{{{*/', '52/*}}}*/', '53'], getline(50,53))
785  norm! j
786  call assert_equal(1, foldlevel('.'))
787
788  " Test for zD
789  " also deletes partially selected folds recursively
790  51
791  norm! zF
792  call assert_equal(2, foldlevel('.'))
793  norm! kV2jzD
794  call assert_equal(['50', '51', '52', '53'], getline(50,53))
795
796  " Test for zE
797  85
798  norm! 4zF
799  86
800  norm! 2zF
801  90
802  norm! 4zF
803  call assert_equal(['85/*{{{*/', '86/*{{{*/', '87/*}}}*/', '88/*}}}*/', '89', '90/*{{{*/', '91', '92', '93/*}}}*/'], getline(85,93))
804  norm! zE
805  call assert_equal(['85', '86', '87', '88', '89', '90', '91', '92', '93'], getline(85,93))
806
807  " Test for zn
808  50
809  set foldlevel=0
810  norm! 2zF
811  norm! zn
812  norm! k
813  call assert_equal('49', getline('.'))
814  norm! j
815  call assert_equal('50/*{{{*/', getline('.'))
816  norm! j
817  call assert_equal('51/*}}}*/', getline('.'))
818  norm! j
819  call assert_equal('52', getline('.'))
820  call assert_equal(0, &foldenable)
821
822  " Test for zN
823  49
824  norm! zN
825  call assert_equal('49', getline('.'))
826  norm! j
827  call assert_equal('50/*{{{*/', getline('.'))
828  norm! j
829  call assert_equal('52', getline('.'))
830  call assert_equal(1, &foldenable)
831
832  " Test for zi
833  norm! zi
834  call assert_equal(0, &foldenable)
835  norm! zi
836  call assert_equal(1, &foldenable)
837  norm! zi
838  call assert_equal(0, &foldenable)
839  norm! zi
840  call assert_equal(1, &foldenable)
841
842  " Test for za
843  50
844  norm! za
845  norm! k
846  call assert_equal('49', getline('.'))
847  norm! j
848  call assert_equal('50/*{{{*/', getline('.'))
849  norm! j
850  call assert_equal('51/*}}}*/', getline('.'))
851  norm! j
852  call assert_equal('52', getline('.'))
853  50
854  norm! za
855  norm! k
856  call assert_equal('49', getline('.'))
857  norm! j
858  call assert_equal('50/*{{{*/', getline('.'))
859  norm! j
860  call assert_equal('52', getline('.'))
861
862  49
863  norm! 5zF
864  norm! k
865  call assert_equal('48', getline('.'))
866  norm! j
867  call assert_equal('49/*{{{*/', getline('.'))
868  norm! j
869  call assert_equal('55', getline('.'))
870  49
871  norm! za
872  call assert_equal('49/*{{{*/', getline('.'))
873  norm! j
874  call assert_equal('50/*{{{*/', getline('.'))
875  norm! j
876  call assert_equal('52', getline('.'))
877  set nofoldenable
878  " close fold and set foldenable
879  norm! za
880  call assert_equal(1, &foldenable)
881
882  50
883  " have to use {count}za to open all folds and make the cursor visible
884  norm! 2za
885  norm! 2k
886  call assert_equal('48', getline('.'))
887  norm! j
888  call assert_equal('49/*{{{*/', getline('.'))
889  norm! j
890  call assert_equal('50/*{{{*/', getline('.'))
891  norm! j
892  call assert_equal('51/*}}}*/', getline('.'))
893  norm! j
894  call assert_equal('52', getline('.'))
895
896  " Test for zA
897  49
898  set foldlevel=0
899  50
900  norm! zA
901  norm! 2k
902  call assert_equal('48', getline('.'))
903  norm! j
904  call assert_equal('49/*{{{*/', getline('.'))
905  norm! j
906  call assert_equal('50/*{{{*/', getline('.'))
907  norm! j
908  call assert_equal('51/*}}}*/', getline('.'))
909  norm! j
910  call assert_equal('52', getline('.'))
911
912  " zA on a opened fold when foldenable is not set
913  50
914  set nofoldenable
915  norm! zA
916  call assert_equal(1, &foldenable)
917  norm! k
918  call assert_equal('48', getline('.'))
919  norm! j
920  call assert_equal('49/*{{{*/', getline('.'))
921  norm! j
922  call assert_equal('55', getline('.'))
923
924  " Test for zc
925  norm! zE
926  50
927  norm! 2zF
928  49
929  norm! 5zF
930  set nofoldenable
931  50
932  " There most likely is a bug somewhere:
933  " https://groups.google.com/d/msg/vim_dev/v2EkfJ_KQjI/u-Cvv94uCAAJ
934  " TODO: Should this only close the inner most fold or both folds?
935  norm! zc
936  call assert_equal(1, &foldenable)
937  norm! k
938  call assert_equal('48', getline('.'))
939  norm! j
940  call assert_equal('49/*{{{*/', getline('.'))
941  norm! j
942  call assert_equal('55', getline('.'))
943  set nofoldenable
944  50
945  norm! Vjzc
946  norm! k
947  call assert_equal('48', getline('.'))
948  norm! j
949  call assert_equal('49/*{{{*/', getline('.'))
950  norm! j
951  call assert_equal('55', getline('.'))
952
953  " Test for zC
954  set nofoldenable
955  50
956  norm! zCk
957  call assert_equal('48', getline('.'))
958  norm! j
959  call assert_equal('49/*{{{*/', getline('.'))
960  norm! j
961  call assert_equal('55', getline('.'))
962
963  " Test for zx
964  " 1) close folds at line 49-54
965  set nofoldenable
966  48
967  norm! zx
968  call assert_equal(1, &foldenable)
969  norm! j
970  call assert_equal('49/*{{{*/', getline('.'))
971  norm! j
972  call assert_equal('55', getline('.'))
973
974  " 2) do not close fold under cursor
975  51
976  set nofoldenable
977  norm! zx
978  call assert_equal(1, &foldenable)
979  norm! 3k
980  call assert_equal('48', getline('.'))
981  norm! j
982  call assert_equal('49/*{{{*/', getline('.'))
983  norm! j
984  call assert_equal('50/*{{{*/', getline('.'))
985  norm! j
986  call assert_equal('51/*}}}*/', getline('.'))
987  norm! j
988  call assert_equal('52', getline('.'))
989  norm! j
990  call assert_equal('53', getline('.'))
991  norm! j
992  call assert_equal('54/*}}}*/', getline('.'))
993  norm! j
994  call assert_equal('55', getline('.'))
995
996  " 3) close one level of folds
997  48
998  set nofoldenable
999  set foldlevel=1
1000  norm! zx
1001  call assert_equal(1, &foldenable)
1002  call assert_equal('48', getline('.'))
1003  norm! j
1004  call assert_equal('49/*{{{*/', getline('.'))
1005  norm! j
1006  call assert_equal('50/*{{{*/', getline('.'))
1007  norm! j
1008  call assert_equal('52', getline('.'))
1009  norm! j
1010  call assert_equal('53', getline('.'))
1011  norm! j
1012  call assert_equal('54/*}}}*/', getline('.'))
1013  norm! j
1014  call assert_equal('55', getline('.'))
1015
1016  " Test for zX
1017  " Close all folds
1018  set foldlevel=0 nofoldenable
1019  50
1020  norm! zX
1021  call assert_equal(1, &foldenable)
1022  norm! k
1023  call assert_equal('48', getline('.'))
1024  norm! j
1025  call assert_equal('49/*{{{*/', getline('.'))
1026  norm! j
1027  call assert_equal('55', getline('.'))
1028
1029  " Test for zm
1030  50
1031  set nofoldenable foldlevel=2
1032  norm! zm
1033  call assert_equal(1, &foldenable)
1034  call assert_equal(1, &foldlevel)
1035  norm! zm
1036  call assert_equal(0, &foldlevel)
1037  norm! zm
1038  call assert_equal(0, &foldlevel)
1039  norm! k
1040  call assert_equal('48', getline('.'))
1041  norm! j
1042  call assert_equal('49/*{{{*/', getline('.'))
1043  norm! j
1044  call assert_equal('55', getline('.'))
1045
1046  " Test for zM
1047  48
1048  set nofoldenable foldlevel=99
1049  norm! zM
1050  call assert_equal(1, &foldenable)
1051  call assert_equal(0, &foldlevel)
1052  call assert_equal('48', getline('.'))
1053  norm! j
1054  call assert_equal('49/*{{{*/', getline('.'))
1055  norm! j
1056  call assert_equal('55', getline('.'))
1057
1058  " Test for zr
1059  48
1060  set nofoldenable foldlevel=0
1061  norm! zr
1062  call assert_equal(0, &foldenable)
1063  call assert_equal(1, &foldlevel)
1064  set foldlevel=0 foldenable
1065  norm! zr
1066  call assert_equal(1, &foldenable)
1067  call assert_equal(1, &foldlevel)
1068  norm! zr
1069  call assert_equal(2, &foldlevel)
1070  call assert_equal('48', getline('.'))
1071  norm! j
1072  call assert_equal('49/*{{{*/', getline('.'))
1073  norm! j
1074  call assert_equal('50/*{{{*/', getline('.'))
1075  norm! j
1076  call assert_equal('51/*}}}*/', getline('.'))
1077  norm! j
1078  call assert_equal('52', getline('.'))
1079
1080  " Test for zR
1081  48
1082  set nofoldenable foldlevel=0
1083  norm! zR
1084  call assert_equal(0, &foldenable)
1085  call assert_equal(2, &foldlevel)
1086  set foldenable foldlevel=0
1087  norm! zR
1088  call assert_equal(1, &foldenable)
1089  call assert_equal(2, &foldlevel)
1090  call assert_equal('48', getline('.'))
1091  norm! j
1092  call assert_equal('49/*{{{*/', getline('.'))
1093  norm! j
1094  call assert_equal('50/*{{{*/', getline('.'))
1095  norm! j
1096  call assert_equal('51/*}}}*/', getline('.'))
1097  norm! j
1098  call assert_equal('52', getline('.'))
1099  call append(50, ['a /*{{{*/', 'b /*}}}*/'])
1100  48
1101  call assert_equal('48', getline('.'))
1102  norm! j
1103  call assert_equal('49/*{{{*/', getline('.'))
1104  norm! j
1105  call assert_equal('50/*{{{*/', getline('.'))
1106  norm! j
1107  call assert_equal('a /*{{{*/', getline('.'))
1108  norm! j
1109  call assert_equal('51/*}}}*/', getline('.'))
1110  norm! j
1111  call assert_equal('52', getline('.'))
1112  48
1113  norm! zR
1114  call assert_equal(1, &foldenable)
1115  call assert_equal(3, &foldlevel)
1116  call assert_equal('48', getline('.'))
1117  norm! j
1118  call assert_equal('49/*{{{*/', getline('.'))
1119  norm! j
1120  call assert_equal('50/*{{{*/', getline('.'))
1121  norm! j
1122  call assert_equal('a /*{{{*/', getline('.'))
1123  norm! j
1124  call assert_equal('b /*}}}*/', getline('.'))
1125  norm! j
1126  call assert_equal('51/*}}}*/', getline('.'))
1127  norm! j
1128  call assert_equal('52', getline('.'))
1129
1130  " clean up
1131  setl nofoldenable fdm=marker foldlevel=0
1132  bw!
1133endfunc
1134
1135func Test_normal20_exmode()
1136  " Reading from redirected file doesn't work on MS-Windows
1137  CheckNotMSWindows
1138  call writefile(['1a', 'foo', 'bar', '.', 'w! Xfile2', 'q!'], 'Xscript')
1139  call writefile(['1', '2'], 'Xfile')
1140  call system(GetVimCommand() .. ' -e -s < Xscript Xfile')
1141  let a=readfile('Xfile2')
1142  call assert_equal(['1', 'foo', 'bar', '2'], a)
1143
1144  " clean up
1145  for file in ['Xfile', 'Xfile2', 'Xscript']
1146    call delete(file)
1147  endfor
1148  bw!
1149endfunc
1150
1151func Test_normal21_nv_hat()
1152
1153  " Edit a fresh file and wipe the buffer list so that there is no alternate
1154  " file present.  Next, check for the expected command failures.
1155  edit Xfoo | %bw
1156  call assert_fails(':buffer #', 'E86')
1157  call assert_fails(':execute "normal! \<C-^>"', 'E23')
1158
1159  " Test for the expected behavior when switching between two named buffers.
1160  edit Xfoo | edit Xbar
1161  call feedkeys("\<C-^>", 'tx')
1162  call assert_equal('Xfoo', fnamemodify(bufname('%'), ':t'))
1163  call feedkeys("\<C-^>", 'tx')
1164  call assert_equal('Xbar', fnamemodify(bufname('%'), ':t'))
1165
1166  " Test for the expected behavior when only one buffer is named.
1167  enew | let l:nr = bufnr('%')
1168  call feedkeys("\<C-^>", 'tx')
1169  call assert_equal('Xbar', fnamemodify(bufname('%'), ':t'))
1170  call feedkeys("\<C-^>", 'tx')
1171  call assert_equal('', bufname('%'))
1172  call assert_equal(l:nr, bufnr('%'))
1173
1174  " Test that no action is taken by "<C-^>" when an operator is pending.
1175  edit Xfoo
1176  call feedkeys("ci\<C-^>", 'tx')
1177  call assert_equal('Xfoo', fnamemodify(bufname('%'), ':t'))
1178
1179  %bw!
1180endfunc
1181
1182func Test_normal22_zet()
1183  " Test for ZZ
1184  " let shell = &shell
1185  " let &shell = 'sh'
1186  call writefile(['1', '2'], 'Xfile')
1187  let args = ' -N -i NONE --noplugins -X --not-a-term'
1188  call system(GetVimCommand() .. args .. ' -c "%d" -c ":norm! ZZ" Xfile')
1189  let a = readfile('Xfile')
1190  call assert_equal([], a)
1191  " Test for ZQ
1192  call writefile(['1', '2'], 'Xfile')
1193  call system(GetVimCommand() . args . ' -c "%d" -c ":norm! ZQ" Xfile')
1194  let a = readfile('Xfile')
1195  call assert_equal(['1', '2'], a)
1196
1197  " Unsupported Z command
1198  call assert_beeps('normal! ZW')
1199
1200  " clean up
1201  for file in ['Xfile']
1202    call delete(file)
1203  endfor
1204  " let &shell = shell
1205endfunc
1206
1207func Test_normal23_K()
1208  " Test for K command
1209  new
1210  call append(0, ['version8.txt', 'man', 'aa%bb', 'cc|dd'])
1211  let k = &keywordprg
1212  set keywordprg=:help
1213  1
1214  norm! VK
1215  call assert_equal('version8.txt', fnamemodify(bufname('%'), ':t'))
1216  call assert_equal('help', &ft)
1217  call assert_match('\*version8.txt\*', getline('.'))
1218  helpclose
1219  norm! 0K
1220  call assert_equal('version8.txt', fnamemodify(bufname('%'), ':t'))
1221  call assert_equal('help', &ft)
1222  call assert_match('\*version8\.\d\*', getline('.'))
1223  helpclose
1224
1225  set keywordprg=:new
1226  set iskeyword+=%
1227  set iskeyword+=\|
1228  2
1229  norm! K
1230  call assert_equal('man', fnamemodify(bufname('%'), ':t'))
1231  bwipe!
1232  3
1233  norm! K
1234  call assert_equal('aa%bb', fnamemodify(bufname('%'), ':t'))
1235  bwipe!
1236  if !has('win32')
1237    4
1238    norm! K
1239    call assert_equal('cc|dd', fnamemodify(bufname('%'), ':t'))
1240    bwipe!
1241  endif
1242  set iskeyword-=%
1243  set iskeyword-=\|
1244
1245  " Only expect "man" to work on Unix
1246  if !has("unix")
1247    let &keywordprg = k
1248    bw!
1249    return
1250  endif
1251
1252  let not_gnu_man = has('mac') || has('bsd')
1253  if not_gnu_man
1254    " In MacOS and BSD, the option for specifying a pager is different
1255    set keywordprg=man\ -P\ cat
1256  else
1257    set keywordprg=man\ --pager=cat
1258  endif
1259  " Test for using man
1260  2
1261  let a = execute('unsilent norm! K')
1262  if not_gnu_man
1263    call assert_match("man -P cat 'man'", a)
1264  else
1265    call assert_match("man --pager=cat 'man'", a)
1266  endif
1267
1268  " Error cases
1269  call setline(1, '#$#')
1270  call assert_fails('normal! ggK', 'E349:')
1271  call setline(1, '---')
1272  call assert_fails('normal! ggv2lK', 'E349:')
1273  call setline(1, ['abc', 'xyz'])
1274  call assert_fails("normal! gg2lv2h\<C-]>", 'E426:')
1275  call assert_beeps("normal! ggVjK")
1276
1277  " clean up
1278  let &keywordprg = k
1279  bw!
1280endfunc
1281
1282func Test_normal24_rot13()
1283  " Testing for g?? g?g?
1284  new
1285  call append(0, 'abcdefghijklmnopqrstuvwxyzäüö')
1286  1
1287  norm! g??
1288  call assert_equal('nopqrstuvwxyzabcdefghijklmäüö', getline('.'))
1289  norm! g?g?
1290  call assert_equal('abcdefghijklmnopqrstuvwxyzäüö', getline('.'))
1291
1292  " clean up
1293  bw!
1294endfunc
1295
1296func Test_normal25_tag()
1297  CheckFeature quickfix
1298
1299  " Testing for CTRL-] g CTRL-] g]
1300  " CTRL-W g] CTRL-W CTRL-] CTRL-W g CTRL-]
1301  h
1302  " Test for CTRL-]
1303  call search('\<x\>$')
1304  exe "norm! \<c-]>"
1305  call assert_equal("change.txt", fnamemodify(bufname('%'), ':t'))
1306  norm! yiW
1307  call assert_equal("*x*", @0)
1308  exe ":norm \<c-o>"
1309
1310  " Test for g_CTRL-]
1311  call search('\<v_u\>$')
1312  exe "norm! g\<c-]>"
1313  call assert_equal("change.txt", fnamemodify(bufname('%'), ':t'))
1314  norm! yiW
1315  call assert_equal("*v_u*", @0)
1316  exe ":norm \<c-o>"
1317
1318  " Test for g]
1319  call search('\<i_<Esc>$')
1320  let a = execute(":norm! g]")
1321  call assert_match('i_<Esc>.*insert.txt', a)
1322
1323  if !empty(exepath('cscope')) && has('cscope')
1324    " setting cscopetag changes how g] works
1325    set cst
1326    exe "norm! g]"
1327    call assert_equal("insert.txt", fnamemodify(bufname('%'), ':t'))
1328    norm! yiW
1329    call assert_equal("*i_<Esc>*", @0)
1330    exe ":norm \<c-o>"
1331    " Test for CTRL-W g]
1332    exe "norm! \<C-W>g]"
1333    call assert_equal("insert.txt", fnamemodify(bufname('%'), ':t'))
1334    norm! yiW
1335    call assert_equal("*i_<Esc>*", @0)
1336    call assert_equal(3, winnr('$'))
1337    helpclose
1338    set nocst
1339  endif
1340
1341  " Test for CTRL-W g]
1342  let a = execute("norm! \<C-W>g]")
1343  call assert_match('i_<Esc>.*insert.txt', a)
1344
1345  " Test for CTRL-W CTRL-]
1346  exe "norm! \<C-W>\<C-]>"
1347  call assert_equal("insert.txt", fnamemodify(bufname('%'), ':t'))
1348  norm! yiW
1349  call assert_equal("*i_<Esc>*", @0)
1350  call assert_equal(3, winnr('$'))
1351  helpclose
1352
1353  " Test for CTRL-W g CTRL-]
1354  exe "norm! \<C-W>g\<C-]>"
1355  call assert_equal("insert.txt", fnamemodify(bufname('%'), ':t'))
1356  norm! yiW
1357  call assert_equal("*i_<Esc>*", @0)
1358  call assert_equal(3, winnr('$'))
1359  helpclose
1360
1361  " clean up
1362  helpclose
1363endfunc
1364
1365func Test_normal26_put()
1366  " Test for ]p ]P [p and [P
1367  new
1368  call append(0, ['while read LINE', 'do', '  ((count++))', '  if [ $? -ne 0 ]; then', "    echo 'Error writing file'", '  fi', 'done'])
1369  1
1370  /Error/y a
1371  2
1372  norm! "a]pj"a[p
1373  call assert_equal(['do', "echo 'Error writing file'", "  echo 'Error writing file'", '  ((count++))'], getline(2,5))
1374  1
1375  /^\s\{4}/
1376  exe "norm!  \"a]P3Eldt'"
1377  exe "norm! j\"a[P2Eldt'"
1378  call assert_equal(['  if [ $? -ne 0 ]; then', "    echo 'Error writing'", "    echo 'Error'", "    echo 'Error writing file'", '  fi'], getline(6,10))
1379
1380  " clean up
1381  bw!
1382endfunc
1383
1384func Test_normal27_bracket()
1385  " Test for [' [` ]' ]`
1386  call Setup_NewWindow()
1387  1,21s/.\+/  &   b/
1388  1
1389  norm! $ma
1390  5
1391  norm! $mb
1392  10
1393  norm! $mc
1394  15
1395  norm! $md
1396  20
1397  norm! $me
1398
1399  " Test for ['
1400  9
1401  norm! 2['
1402  call assert_equal('  1   b', getline('.'))
1403  call assert_equal(1, line('.'))
1404  call assert_equal(3, col('.'))
1405
1406  " Test for ]'
1407  norm! ]'
1408  call assert_equal('  5   b', getline('.'))
1409  call assert_equal(5, line('.'))
1410  call assert_equal(3, col('.'))
1411
1412  " No mark after line 21, cursor moves to first non blank on current line
1413  21
1414  norm! $]'
1415  call assert_equal('  21   b', getline('.'))
1416  call assert_equal(21, line('.'))
1417  call assert_equal(3, col('.'))
1418
1419  " Test for [`
1420  norm! 2[`
1421  call assert_equal('  15   b', getline('.'))
1422  call assert_equal(15, line('.'))
1423  call assert_equal(8, col('.'))
1424
1425  " Test for ]`
1426  norm! ]`
1427  call assert_equal('  20   b', getline('.'))
1428  call assert_equal(20, line('.'))
1429  call assert_equal(8, col('.'))
1430
1431  " clean up
1432  bw!
1433endfunc
1434
1435" Test for ( and ) sentence movements
1436func Test_normal28_parenthesis()
1437  new
1438  call append(0, ['This is a test. With some sentences!', '', 'Even with a question? And one more. And no sentence here'])
1439
1440  $
1441  norm! d(
1442  call assert_equal(['This is a test. With some sentences!', '', 'Even with a question? And one more. ', ''], getline(1, '$'))
1443  norm! 2d(
1444  call assert_equal(['This is a test. With some sentences!', '', ' ', ''], getline(1, '$'))
1445  1
1446  norm! 0d)
1447  call assert_equal(['With some sentences!', '', ' ', ''], getline(1, '$'))
1448
1449  call append('$', ['This is a long sentence', '', 'spanning', 'over several lines. '])
1450  $
1451  norm! $d(
1452  call assert_equal(['With some sentences!', '', ' ', '', 'This is a long sentence', ''], getline(1, '$'))
1453
1454  " Move to the next sentence from a paragraph macro
1455  %d
1456  call setline(1, ['.LP', 'blue sky!. blue sky.', 'blue sky. blue sky.'])
1457  call cursor(1, 1)
1458  normal )
1459  call assert_equal([2, 1], [line('.'), col('.')])
1460  normal )
1461  call assert_equal([2, 12], [line('.'), col('.')])
1462  normal ((
1463  call assert_equal([1, 1], [line('.'), col('.')])
1464
1465  " It is an error if a next sentence is not found
1466  %d
1467  call setline(1, '.SH')
1468  call assert_beeps('normal )')
1469
1470  " If only dot is present, don't treat that as a sentence
1471  call setline(1, '. This is a sentence.')
1472  normal $((
1473  call assert_equal(3, col('.'))
1474
1475  " Jumping to a fold should open the fold
1476  call setline(1, ['', '', 'one', 'two', 'three'])
1477  set foldenable
1478  2,$fold
1479  call feedkeys(')', 'xt')
1480  call assert_equal(3, line('.'))
1481  call assert_equal(1, foldlevel('.'))
1482  call assert_equal(-1, foldclosed('.'))
1483  set foldenable&
1484
1485  " clean up
1486  bw!
1487endfunc
1488
1489" Test for { and } paragraph movements
1490func Test_normal29_brace()
1491  let text =<< trim [DATA]
1492    A paragraph begins after each empty line, and also at each of a set of
1493    paragraph macros, specified by the pairs of characters in the 'paragraphs'
1494    option.  The default is "IPLPPPQPP TPHPLIPpLpItpplpipbp", which corresponds to
1495    the macros ".IP", ".LP", etc.  (These are nroff macros, so the dot must be in
1496    the first column).  A section boundary is also a paragraph boundary.
1497    Note that a blank line (only containing white space) is NOT a paragraph
1498    boundary.
1499
1500
1501    Also note that this does not include a '{' or '}' in the first column.  When
1502    the '{' flag is in 'cpoptions' then '{' in the first column is used as a
1503    paragraph boundary |posix|.
1504    {
1505    This is no paragraph
1506    unless the '{' is set
1507    in 'cpoptions'
1508    }
1509    .IP
1510    The nroff macros IP separates a paragraph
1511    That means, it must be a '.'
1512    followed by IP
1513    .LPIt does not matter, if afterwards some
1514    more characters follow.
1515    .SHAlso section boundaries from the nroff
1516    macros terminate a paragraph. That means
1517    a character like this:
1518    .NH
1519    End of text here
1520  [DATA]
1521
1522  new
1523  call append(0, text)
1524  1
1525  norm! 0d2}
1526
1527  let expected =<< trim [DATA]
1528    .IP
1529    The nroff macros IP separates a paragraph
1530    That means, it must be a '.'
1531    followed by IP
1532    .LPIt does not matter, if afterwards some
1533    more characters follow.
1534    .SHAlso section boundaries from the nroff
1535    macros terminate a paragraph. That means
1536    a character like this:
1537    .NH
1538    End of text here
1539
1540  [DATA]
1541  call assert_equal(expected, getline(1, '$'))
1542
1543  norm! 0d}
1544
1545  let expected =<< trim [DATA]
1546    .LPIt does not matter, if afterwards some
1547    more characters follow.
1548    .SHAlso section boundaries from the nroff
1549    macros terminate a paragraph. That means
1550    a character like this:
1551    .NH
1552    End of text here
1553
1554  [DATA]
1555  call assert_equal(expected, getline(1, '$'))
1556
1557  $
1558  norm! d{
1559
1560  let expected =<< trim [DATA]
1561    .LPIt does not matter, if afterwards some
1562    more characters follow.
1563    .SHAlso section boundaries from the nroff
1564    macros terminate a paragraph. That means
1565    a character like this:
1566
1567  [DATA]
1568  call assert_equal(expected, getline(1, '$'))
1569
1570  norm! d{
1571
1572  let expected =<< trim [DATA]
1573    .LPIt does not matter, if afterwards some
1574    more characters follow.
1575
1576  [DATA]
1577  call assert_equal(expected, getline(1, '$'))
1578
1579  " Test with { in cpooptions
1580  %d
1581  call append(0, text)
1582  set cpo+={
1583  1
1584  norm! 0d2}
1585
1586  let expected =<< trim [DATA]
1587    {
1588    This is no paragraph
1589    unless the '{' is set
1590    in 'cpoptions'
1591    }
1592    .IP
1593    The nroff macros IP separates a paragraph
1594    That means, it must be a '.'
1595    followed by IP
1596    .LPIt does not matter, if afterwards some
1597    more characters follow.
1598    .SHAlso section boundaries from the nroff
1599    macros terminate a paragraph. That means
1600    a character like this:
1601    .NH
1602    End of text here
1603
1604  [DATA]
1605  call assert_equal(expected, getline(1, '$'))
1606
1607  $
1608  norm! d}
1609
1610  let expected =<< trim [DATA]
1611    {
1612    This is no paragraph
1613    unless the '{' is set
1614    in 'cpoptions'
1615    }
1616    .IP
1617    The nroff macros IP separates a paragraph
1618    That means, it must be a '.'
1619    followed by IP
1620    .LPIt does not matter, if afterwards some
1621    more characters follow.
1622    .SHAlso section boundaries from the nroff
1623    macros terminate a paragraph. That means
1624    a character like this:
1625    .NH
1626    End of text here
1627
1628  [DATA]
1629  call assert_equal(expected, getline(1, '$'))
1630
1631  norm! gg}
1632  norm! d5}
1633
1634  let expected =<< trim [DATA]
1635    {
1636    This is no paragraph
1637    unless the '{' is set
1638    in 'cpoptions'
1639    }
1640
1641  [DATA]
1642  call assert_equal(expected, getline(1, '$'))
1643
1644  " Jumping to a fold should open the fold
1645  %d
1646  call setline(1, ['', 'one', 'two', ''])
1647  set foldenable
1648  2,$fold
1649  call feedkeys('}', 'xt')
1650  call assert_equal(4, line('.'))
1651  call assert_equal(1, foldlevel('.'))
1652  call assert_equal(-1, foldclosed('.'))
1653  set foldenable&
1654
1655  " clean up
1656  set cpo-={
1657  bw!
1658endfunc
1659
1660" Test for ~ command
1661func Test_normal30_changecase()
1662  new
1663  call append(0, 'This is a simple test: äüöß')
1664  norm! 1ggVu
1665  call assert_equal('this is a simple test: äüöß', getline('.'))
1666  norm! VU
1667  call assert_equal('THIS IS A SIMPLE TEST: ÄÜÖSS', getline('.'))
1668  norm! guu
1669  call assert_equal('this is a simple test: äüöss', getline('.'))
1670  norm! gUgU
1671  call assert_equal('THIS IS A SIMPLE TEST: ÄÜÖSS', getline('.'))
1672  norm! gugu
1673  call assert_equal('this is a simple test: äüöss', getline('.'))
1674  norm! gUU
1675  call assert_equal('THIS IS A SIMPLE TEST: ÄÜÖSS', getline('.'))
1676  norm! 010~
1677  call assert_equal('this is a SIMPLE TEST: ÄÜÖSS', getline('.'))
1678  norm! V~
1679  call assert_equal('THIS IS A simple test: äüöss', getline('.'))
1680
1681  " Test for changing case across lines using 'whichwrap'
1682  call setline(1, ['aaaaaa', 'aaaaaa'])
1683  normal! gg10~
1684  call assert_equal(['AAAAAA', 'aaaaaa'], getline(1, 2))
1685  set whichwrap+=~
1686  normal! gg10~
1687  call assert_equal(['aaaaaa', 'AAAAaa'], getline(1, 2))
1688  set whichwrap&
1689
1690  " clean up
1691  bw!
1692endfunc
1693
1694" Turkish ASCII turns to multi-byte.  On some systems Turkish locale
1695" is available but toupper()/tolower() don't do the right thing.
1696func Test_normal_changecase_turkish()
1697  new
1698  try
1699    lang tr_TR.UTF-8
1700    set casemap=
1701    let iupper = toupper('i')
1702    if iupper == "\u0130"
1703      call setline(1, 'iI')
1704      1normal gUU
1705      call assert_equal("\u0130I", getline(1))
1706      call assert_equal("\u0130I", toupper("iI"))
1707
1708      call setline(1, 'iI')
1709      1normal guu
1710      call assert_equal("i\u0131", getline(1))
1711      call assert_equal("i\u0131", tolower("iI"))
1712    elseif iupper == "I"
1713      call setline(1, 'iI')
1714      1normal gUU
1715      call assert_equal("II", getline(1))
1716      call assert_equal("II", toupper("iI"))
1717
1718      call setline(1, 'iI')
1719      1normal guu
1720      call assert_equal("ii", getline(1))
1721      call assert_equal("ii", tolower("iI"))
1722    else
1723      call assert_true(false, "expected toupper('i') to be either 'I' or '\u0130'")
1724    endif
1725    set casemap&
1726    call setline(1, 'iI')
1727    1normal gUU
1728    call assert_equal("II", getline(1))
1729    call assert_equal("II", toupper("iI"))
1730
1731    call setline(1, 'iI')
1732    1normal guu
1733    call assert_equal("ii", getline(1))
1734    call assert_equal("ii", tolower("iI"))
1735
1736    lang en_US.UTF-8
1737  catch /E197:/
1738    " can't use Turkish locale
1739    throw 'Skipped: Turkish locale not available'
1740  endtry
1741  close!
1742endfunc
1743
1744" Test for r (replace) command
1745func Test_normal31_r_cmd()
1746  new
1747  call append(0, 'This is a simple test: abcd')
1748  exe "norm! 1gg$r\<cr>"
1749  call assert_equal(['This is a simple test: abc', '', ''], getline(1,'$'))
1750  exe "norm! 1gg2wlr\<cr>"
1751  call assert_equal(['This is a', 'simple test: abc', '', ''], getline(1,'$'))
1752  exe "norm! 2gg0W5r\<cr>"
1753  call assert_equal(['This is a', 'simple ', ' abc', '', ''], getline('1', '$'))
1754  set autoindent
1755  call setline(2, ['simple test: abc', ''])
1756  exe "norm! 2gg0W5r\<cr>"
1757  call assert_equal(['This is a', 'simple ', 'abc', '', '', ''], getline('1', '$'))
1758  exe "norm! 1ggVr\<cr>"
1759  call assert_equal('^M^M^M^M^M^M^M^M^M', strtrans(getline(1)))
1760  call setline(1, 'This is a')
1761  exe "norm! 1gg05rf"
1762  call assert_equal('fffffis a', getline(1))
1763
1764  " When replacing characters, copy characters from above and below lines
1765  " using CTRL-Y and CTRL-E.
1766  " Different code paths are used for utf-8 and latin1 encodings
1767  set showmatch
1768  for enc in ['latin1', 'utf-8']
1769    enew!
1770    let &encoding = enc
1771    call setline(1, [' {a}', 'xxxxxxxxxx', '      [b]'])
1772    exe "norm! 2gg5r\<C-Y>l5r\<C-E>"
1773    call assert_equal(' {a}x [b]x', getline(2))
1774  endfor
1775  set showmatch&
1776
1777  " r command should fail in operator pending mode
1778  call assert_beeps('normal! cr')
1779
1780  " replace a tab character in visual mode
1781  %d
1782  call setline(1, ["a\tb", "c\td", "e\tf"])
1783  normal gglvjjrx
1784  call assert_equal(['axx', 'xxx', 'xxf'], getline(1, '$'))
1785
1786  " clean up
1787  set noautoindent
1788  bw!
1789endfunc
1790
1791" Test for g*, g#
1792func Test_normal32_g_cmd1()
1793  new
1794  call append(0, ['abc.x_foo', 'x_foobar.abc'])
1795  1
1796  norm! $g*
1797  call assert_equal('x_foo', @/)
1798  call assert_equal('x_foobar.abc', getline('.'))
1799  norm! $g#
1800  call assert_equal('abc', @/)
1801  call assert_equal('abc.x_foo', getline('.'))
1802
1803  " clean up
1804  bw!
1805endfunc
1806
1807" Test for g`, g;, g,, g&, gv, gk, gj, gJ, g0, g^, g_, gm, g$, gM, g CTRL-G,
1808" gi and gI commands
1809func Test_normal33_g_cmd2()
1810  CheckFeature jumplist
1811  call Setup_NewWindow()
1812  " Test for g`
1813  clearjumps
1814  norm! ma10j
1815  let a=execute(':jumps')
1816  " empty jumplist
1817  call assert_equal('>', a[-1:])
1818  norm! g`a
1819  call assert_equal('>', a[-1:])
1820  call assert_equal(1, line('.'))
1821  call assert_equal('1', getline('.'))
1822  call cursor(10, 1)
1823  norm! g'a
1824  call assert_equal('>', a[-1:])
1825  call assert_equal(1, line('.'))
1826
1827  " Test for g; and g,
1828  norm! g;
1829  " there is only one change in the changelist
1830  " currently, when we setup the window
1831  call assert_equal(2, line('.'))
1832  call assert_fails(':norm! g;', 'E662')
1833  call assert_fails(':norm! g,', 'E663')
1834  let &ul=&ul
1835  call append('$', ['a', 'b', 'c', 'd'])
1836  let &ul=&ul
1837  call append('$', ['Z', 'Y', 'X', 'W'])
1838  let a = execute(':changes')
1839  call assert_match('2\s\+0\s\+2', a)
1840  call assert_match('101\s\+0\s\+a', a)
1841  call assert_match('105\s\+0\s\+Z', a)
1842  norm! 3g;
1843  call assert_equal(2, line('.'))
1844  norm! 2g,
1845  call assert_equal(105, line('.'))
1846
1847  " Test for g& - global substitute
1848  %d
1849  call setline(1, range(1,10))
1850  call append('$', ['a', 'b', 'c', 'd'])
1851  $s/\w/&&/g
1852  exe "norm! /[1-8]\<cr>"
1853  norm! g&
1854  call assert_equal(['11', '22', '33', '44', '55', '66', '77', '88', '9', '110', 'a', 'b', 'c', 'dd'], getline(1, '$'))
1855
1856  " Jumping to a fold using gg should open the fold
1857  set foldenable
1858  set foldopen+=jump
1859  5,8fold
1860  call feedkeys('6gg', 'xt')
1861  call assert_equal(1, foldlevel('.'))
1862  call assert_equal(-1, foldclosed('.'))
1863  set foldopen-=jump
1864  set foldenable&
1865
1866  " Test for gv
1867  %d
1868  call append('$', repeat(['abcdefgh'], 8))
1869  exe "norm! 2gg02l\<c-v>2j2ly"
1870  call assert_equal(['cde', 'cde', 'cde'], getreg(0, 1, 1))
1871  " in visual mode, gv swaps current and last selected region
1872  exe "norm! G0\<c-v>4k4lgvd"
1873  call assert_equal(['', 'abfgh', 'abfgh', 'abfgh', 'abcdefgh', 'abcdefgh', 'abcdefgh', 'abcdefgh', 'abcdefgh'], getline(1,'$'))
1874  exe "norm! G0\<c-v>4k4ly"
1875  exe "norm! gvood"
1876  call assert_equal(['', 'abfgh', 'abfgh', 'abfgh', 'fgh', 'fgh', 'fgh', 'fgh', 'fgh'], getline(1,'$'))
1877  " gv cannot be used  in operator pending mode
1878  call assert_beeps('normal! cgv')
1879  " gv should beep without a previously selected visual area
1880  new
1881  call assert_beeps('normal! gv')
1882  close
1883
1884  " Test for gk/gj
1885  %d
1886  15vsp
1887  set wrap listchars= sbr=
1888  let lineA='abcdefghijklmnopqrstuvwxyz'
1889  let lineB='0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'
1890  let lineC='0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz01234567890123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
1891  $put =lineA
1892  $put =lineB
1893
1894  norm! 3gg0dgk
1895  call assert_equal(['', 'abcdefghijklmno', '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'], getline(1, '$'))
1896  set nu
1897  norm! 3gg0gjdgj
1898  call assert_equal(['', 'abcdefghijklmno', '0123456789AMNOPQRSTUVWXYZ'], getline(1,'$'))
1899
1900  " Test for gJ
1901  norm! 2gggJ
1902  call assert_equal(['', 'abcdefghijklmno0123456789AMNOPQRSTUVWXYZ'], getline(1,'$'))
1903  call assert_equal(16, col('.'))
1904  " shouldn't do anything
1905  norm! 10gJ
1906  call assert_equal(1, col('.'))
1907
1908  " Test for g0 g^ gm g$
1909  exe "norm! 2gg0gji   "
1910  call assert_equal(['', 'abcdefghijk   lmno0123456789AMNOPQRSTUVWXYZ'], getline(1,'$'))
1911  norm! g0yl
1912  call assert_equal(12, col('.'))
1913  call assert_equal(' ', getreg(0))
1914  norm! g$yl
1915  call assert_equal(22, col('.'))
1916  call assert_equal('3', getreg(0))
1917  norm! gmyl
1918  call assert_equal(17, col('.'))
1919  call assert_equal('n', getreg(0))
1920  norm! g^yl
1921  call assert_equal(15, col('.'))
1922  call assert_equal('l', getreg(0))
1923  call assert_beeps('normal 5g$')
1924
1925  " Test for g_
1926  call assert_beeps('normal! 100g_')
1927  call setline(2, ['  foo  ', '  foobar  '])
1928  normal! 2ggg_
1929  call assert_equal(5, col('.'))
1930  normal! 2g_
1931  call assert_equal(8, col('.'))
1932
1933  norm! 2ggdG
1934  $put =lineC
1935
1936  " Test for gM
1937  norm! gMyl
1938  call assert_equal(73, col('.'))
1939  call assert_equal('0', getreg(0))
1940  " Test for 20gM
1941  norm! 20gMyl
1942  call assert_equal(29, col('.'))
1943  call assert_equal('S', getreg(0))
1944  " Test for 60gM
1945  norm! 60gMyl
1946  call assert_equal(87, col('.'))
1947  call assert_equal('E', getreg(0))
1948
1949  " Test for g Ctrl-G
1950  set ff=unix
1951  let a=execute(":norm! g\<c-g>")
1952  call assert_match('Col 87 of 144; Line 2 of 2; Word 1 of 1; Byte 88 of 146', a)
1953
1954  " Test for gI
1955  norm! gIfoo
1956  call assert_equal(['', 'foo0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz01234567890123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'], getline(1,'$'))
1957
1958  " Test for gi
1959  wincmd c
1960  %d
1961  set tw=0
1962  call setline(1, ['foobar', 'new line'])
1963  norm! A next word
1964  $put ='third line'
1965  norm! gi another word
1966  call assert_equal(['foobar next word another word', 'new line', 'third line'], getline(1,'$'))
1967  call setline(1, 'foobar')
1968  normal! Ggifirst line
1969  call assert_equal('foobarfirst line', getline(1))
1970  " Test gi in 'virtualedit' mode with cursor after the end of the line
1971  set virtualedit=all
1972  call setline(1, 'foo')
1973  exe "normal! Abar\<Right>\<Right>\<Right>\<Right>"
1974  call setline(1, 'foo')
1975  normal! Ggifirst line
1976  call assert_equal('foo       first line', getline(1))
1977  set virtualedit&
1978
1979  " Test for aboring a g command using CTRL-\ CTRL-G
1980  exe "normal! g\<C-\>\<C-G>"
1981  call assert_equal('foo       first line', getline('.'))
1982
1983  " clean up
1984  bw!
1985endfunc
1986
1987" Test for g CTRL-G
1988func Test_g_ctrl_g()
1989  new
1990
1991  let a = execute(":norm! g\<c-g>")
1992  call assert_equal("\n--No lines in buffer--", a)
1993
1994  " Test for CTRL-G (same as :file)
1995  let a = execute(":norm! \<c-g>")
1996  call assert_equal("\n\n\"[No Name]\" --No lines in buffer--", a)
1997
1998  call setline(1, ['first line', 'second line'])
1999
2000  " Test g CTRL-g with dos, mac and unix file type.
2001  norm! gojll
2002  set ff=dos
2003  let a = execute(":norm! g\<c-g>")
2004  call assert_equal("\nCol 3 of 11; Line 2 of 2; Word 3 of 4; Byte 15 of 25", a)
2005
2006  set ff=mac
2007  let a = execute(":norm! g\<c-g>")
2008  call assert_equal("\nCol 3 of 11; Line 2 of 2; Word 3 of 4; Byte 14 of 23", a)
2009
2010  set ff=unix
2011  let a = execute(":norm! g\<c-g>")
2012  call assert_equal("\nCol 3 of 11; Line 2 of 2; Word 3 of 4; Byte 14 of 23", a)
2013
2014  " Test g CTRL-g in visual mode (v)
2015  let a = execute(":norm! gojllvlg\<c-g>")
2016  call assert_equal("\nSelected 1 of 2 Lines; 1 of 4 Words; 2 of 23 Bytes", a)
2017
2018  " Test g CTRL-g in visual mode (CTRL-V) with end col > start col
2019  let a = execute(":norm! \<Esc>gojll\<C-V>kllg\<c-g>")
2020  call assert_equal("\nSelected 3 Cols; 2 of 2 Lines; 2 of 4 Words; 6 of 23 Bytes", a)
2021
2022  " Test g_CTRL-g in visual mode (CTRL-V) with end col < start col
2023  let a = execute(":norm! \<Esc>goll\<C-V>jhhg\<c-g>")
2024  call assert_equal("\nSelected 3 Cols; 2 of 2 Lines; 2 of 4 Words; 6 of 23 Bytes", a)
2025
2026  " Test g CTRL-g in visual mode (CTRL-V) with end_vcol being MAXCOL
2027  let a = execute(":norm! \<Esc>gojll\<C-V>k$g\<c-g>")
2028  call assert_equal("\nSelected 2 of 2 Lines; 4 of 4 Words; 17 of 23 Bytes", a)
2029
2030  " There should be one byte less with noeol
2031  set bin noeol
2032  let a = execute(":norm! \<Esc>gog\<c-g>")
2033  call assert_equal("\nCol 1 of 10; Line 1 of 2; Word 1 of 4; Char 1 of 23; Byte 1 of 22", a)
2034  set bin & eol&
2035
2036  call setline(1, ['Français', '日本語'])
2037
2038  let a = execute(":norm! \<Esc>gojlg\<c-g>")
2039  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)
2040
2041  let a = execute(":norm! \<Esc>gojvlg\<c-g>")
2042  call assert_equal("\nSelected 1 of 2 Lines; 1 of 2 Words; 2 of 13 Chars; 6 of 20 Bytes", a)
2043
2044  let a = execute(":norm! \<Esc>goll\<c-v>jlg\<c-g>")
2045  call assert_equal("\nSelected 4 Cols; 2 of 2 Lines; 2 of 2 Words; 6 of 13 Chars; 11 of 20 Bytes", a)
2046
2047  set fenc=utf8 bomb
2048  let a = execute(":norm! \<Esc>gojlg\<c-g>")
2049  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)
2050
2051  set fenc=utf16 bomb
2052  let a = execute(":norm! g\<c-g>")
2053  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)
2054
2055  set fenc=utf32 bomb
2056  let a = execute(":norm! g\<c-g>")
2057  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)
2058
2059  set fenc& bomb&
2060
2061  set ff&
2062  bwipe!
2063endfunc
2064
2065" Test for g8
2066func Test_normal34_g_cmd3()
2067  new
2068  let a=execute(':norm! 1G0g8')
2069  call assert_equal("\nNUL", a)
2070
2071  call setline(1, 'abcdefghijklmnopqrstuvwxyzäüö')
2072  let a=execute(':norm! 1G$g8')
2073  call assert_equal("\nc3 b6 ", a)
2074
2075  call setline(1, "a\u0302")
2076  let a=execute(':norm! 1G0g8')
2077  call assert_equal("\n61 + cc 82 ", a)
2078
2079  " clean up
2080  bw!
2081endfunc
2082
2083" Test 8g8 which finds invalid utf8 at or after the cursor.
2084func Test_normal_8g8()
2085  new
2086
2087  " With invalid byte.
2088  call setline(1, "___\xff___")
2089  norm! 1G08g8g
2090  call assert_equal([0, 1, 4, 0, 1], getcurpos())
2091
2092  " With invalid byte before the cursor.
2093  call setline(1, "___\xff___")
2094  norm! 1G$h8g8g
2095  call assert_equal([0, 1, 6, 0, 9], getcurpos())
2096
2097  " With truncated sequence.
2098  call setline(1, "___\xE2\x82___")
2099  norm! 1G08g8g
2100  call assert_equal([0, 1, 4, 0, 1], getcurpos())
2101
2102  " With overlong sequence.
2103  call setline(1, "___\xF0\x82\x82\xAC___")
2104  norm! 1G08g8g
2105  call assert_equal([0, 1, 4, 0, 1], getcurpos())
2106
2107  " With valid utf8.
2108  call setline(1, "café")
2109  norm! 1G08g8
2110  call assert_equal([0, 1, 1, 0, 1], getcurpos())
2111
2112  bw!
2113endfunc
2114
2115" Test for g<
2116func Test_normal35_g_cmd4()
2117  " Cannot capture its output,
2118  " probably a bug, therefore, test disabled:
2119  throw "Skipped: output of g< can't be tested currently"
2120  echo "a\nb\nc\nd"
2121  let b=execute(':norm! g<')
2122  call assert_true(!empty(b), 'failed `execute(g<)`')
2123endfunc
2124
2125" Test for gp gP go
2126func Test_normal36_g_cmd5()
2127  new
2128  call append(0, 'abcdefghijklmnopqrstuvwxyz')
2129  set ff=unix
2130  " Test for gp gP
2131  call append(1, range(1,10))
2132  1
2133  norm! 1yy
2134  3
2135  norm! gp
2136  call assert_equal([0, 5, 1, 0, 1], getcurpos())
2137  $
2138  norm! gP
2139  call assert_equal([0, 14, 1, 0, 1], getcurpos())
2140
2141  " Test for go
2142  norm! 26go
2143  call assert_equal([0, 1, 26, 0, 26], getcurpos())
2144  norm! 27go
2145  call assert_equal([0, 1, 26, 0, 26], getcurpos())
2146  norm! 28go
2147  call assert_equal([0, 2, 1, 0, 1], getcurpos())
2148  set ff=dos
2149  norm! 29go
2150  call assert_equal([0, 2, 1, 0, 1], getcurpos())
2151  set ff=unix
2152  norm! gg0
2153  norm! 101go
2154  call assert_equal([0, 13, 26, 0, 26], getcurpos())
2155  norm! 103go
2156  call assert_equal([0, 14, 1, 0, 1], getcurpos())
2157  " count > buffer content
2158  norm! 120go
2159  call assert_equal([0, 14, 1, 0, 2147483647], getcurpos())
2160  " clean up
2161  bw!
2162endfunc
2163
2164" Test for gt and gT
2165func Test_normal37_g_cmd6()
2166  tabnew 1.txt
2167  tabnew 2.txt
2168  tabnew 3.txt
2169  norm! 1gt
2170  call assert_equal(1, tabpagenr())
2171  norm! 3gt
2172  call assert_equal(3, tabpagenr())
2173  norm! 1gT
2174  " count gT goes not to the absolute tabpagenumber
2175  " but, but goes to the count previous tabpagenumber
2176  call assert_equal(2, tabpagenr())
2177  " wrap around
2178  norm! 3gT
2179  call assert_equal(3, tabpagenr())
2180  " gt does not wrap around
2181  norm! 5gt
2182  call assert_equal(3, tabpagenr())
2183
2184  for i in range(3)
2185    tabclose
2186  endfor
2187  " clean up
2188  call assert_fails(':tabclose', 'E784:')
2189endfunc
2190
2191" Test for <Home> and <C-Home> key
2192func Test_normal38_nvhome()
2193  new
2194  call setline(1, range(10))
2195  $
2196  setl et sw=2
2197  norm! V10>$
2198  " count is ignored
2199  exe "norm! 10\<home>"
2200  call assert_equal(1, col('.'))
2201  exe "norm! \<home>"
2202  call assert_equal([0, 10, 1, 0, 1], getcurpos())
2203  exe "norm! 5\<c-home>"
2204  call assert_equal([0, 5, 1, 0, 1], getcurpos())
2205  exe "norm! \<c-home>"
2206  call assert_equal([0, 1, 1, 0, 1], getcurpos())
2207  exe "norm! G\<c-kHome>"
2208  call assert_equal([0, 1, 1, 0, 1], getcurpos())
2209
2210  " clean up
2211  bw!
2212endfunc
2213
2214" Test for <End> and <C-End> keys
2215func Test_normal_nvend()
2216  new
2217  call setline(1, map(range(1, 10), '"line" .. v:val'))
2218  exe "normal! \<End>"
2219  call assert_equal(5, col('.'))
2220  exe "normal! 4\<End>"
2221  call assert_equal([4, 5], [line('.'), col('.')])
2222  exe "normal! \<C-End>"
2223  call assert_equal([10, 6], [line('.'), col('.')])
2224  close!
2225endfunc
2226
2227" Test for cw cW ce
2228func Test_normal39_cw()
2229  " Test for cw and cW on whitespace
2230  " and cpo+=w setting
2231  new
2232  set tw=0
2233  call append(0, 'here      are   some words')
2234  norm! 1gg0elcwZZZ
2235  call assert_equal('hereZZZare   some words', getline('.'))
2236  norm! 1gg0elcWYYY
2237  call assert_equal('hereZZZareYYYsome words', getline('.'))
2238  set cpo+=w
2239  call setline(1, 'here      are   some words')
2240  norm! 1gg0elcwZZZ
2241  call assert_equal('hereZZZ     are   some words', getline('.'))
2242  norm! 1gg2elcWYYY
2243  call assert_equal('hereZZZ     areYYY  some words', getline('.'))
2244  set cpo-=w
2245  norm! 2gg0cwfoo
2246  call assert_equal('foo', getline('.'))
2247
2248  call setline(1, 'one; two')
2249  call cursor(1, 1)
2250  call feedkeys('cwvim', 'xt')
2251  call assert_equal('vim; two', getline(1))
2252  call feedkeys('0cWone', 'xt')
2253  call assert_equal('one two', getline(1))
2254  "When cursor is at the end of a word 'ce' will change until the end of the
2255  "next word, but 'cw' will change only one character
2256  call setline(1, 'one two')
2257  call feedkeys('0ecwce', 'xt')
2258  call assert_equal('once two', getline(1))
2259  call setline(1, 'one two')
2260  call feedkeys('0ecely', 'xt')
2261  call assert_equal('only', getline(1))
2262
2263  " clean up
2264  bw!
2265endfunc
2266
2267" Test for CTRL-\ commands
2268func Test_normal40_ctrl_bsl()
2269  new
2270  call append(0, 'here      are   some words')
2271  exe "norm! 1gg0a\<C-\>\<C-N>"
2272  call assert_equal('n', mode())
2273  call assert_equal(1, col('.'))
2274  call assert_equal('', visualmode())
2275  exe "norm! 1gg0viw\<C-\>\<C-N>"
2276  call assert_equal('n', mode())
2277  call assert_equal(4, col('.'))
2278  exe "norm! 1gg0a\<C-\>\<C-G>"
2279  call assert_equal('n', mode())
2280  call assert_equal(1, col('.'))
2281  "imap <buffer> , <c-\><c-n>
2282  set im
2283  exe ":norm! \<c-\>\<c-n>dw"
2284  set noim
2285  call assert_equal('are   some words', getline(1))
2286  call assert_false(&insertmode)
2287  call assert_beeps("normal! \<C-\>\<C-A>", 'xt')
2288
2289  " Using CTRL-\ CTRL-N in cmd window should close the window
2290  call feedkeys("q:\<C-\>\<C-N>", 'xt')
2291  call assert_equal('', getcmdwintype())
2292
2293  " clean up
2294  bw!
2295endfunc
2296
2297" Test for <c-r>=, <c-r><c-r>= and <c-r><c-o>= in insert mode
2298func Test_normal41_insert_reg()
2299  new
2300  set sts=2 sw=2 ts=8 tw=0
2301  call append(0, ["aaa\tbbb\tccc", '', '', ''])
2302  let a=getline(1)
2303  norm! 2gg0
2304  exe "norm! a\<c-r>=a\<cr>"
2305  norm! 3gg0
2306  exe "norm! a\<c-r>\<c-r>=a\<cr>"
2307  norm! 4gg0
2308  exe "norm! a\<c-r>\<c-o>=a\<cr>"
2309  call assert_equal(['aaa	bbb	ccc', 'aaa bbb	ccc', 'aaa bbb	ccc', 'aaa	bbb	ccc', ''], getline(1, '$'))
2310
2311  " clean up
2312  set sts=0 sw=8 ts=8
2313  bw!
2314endfunc
2315
2316" Test for Ctrl-D and Ctrl-U
2317func Test_normal42_halfpage()
2318  call Setup_NewWindow()
2319  call assert_equal(5, &scroll)
2320  exe "norm! \<c-d>"
2321  call assert_equal('6', getline('.'))
2322  exe "norm! 2\<c-d>"
2323  call assert_equal('8', getline('.'))
2324  call assert_equal(2, &scroll)
2325  set scroll=5
2326  exe "norm! \<c-u>"
2327  call assert_equal('3', getline('.'))
2328  1
2329  set scrolloff=5
2330  exe "norm! \<c-d>"
2331  call assert_equal('10', getline('.'))
2332  exe "norm! \<c-u>"
2333  call assert_equal('5', getline('.'))
2334  1
2335  set scrolloff=99
2336  exe "norm! \<c-d>"
2337  call assert_equal('10', getline('.'))
2338  set scrolloff=0
2339  100
2340  exe "norm! $\<c-u>"
2341  call assert_equal('95', getline('.'))
2342  call assert_equal([0, 95, 1, 0, 1], getcurpos())
2343  100
2344  set nostartofline
2345  exe "norm! $\<c-u>"
2346  call assert_equal('95', getline('.'))
2347  call assert_equal([0, 95, 2, 0, 2147483647], getcurpos())
2348  " cleanup
2349  set startofline
2350  bw!
2351endfunc
2352
2353func Test_normal45_drop()
2354  if !has('dnd')
2355    " The ~ register does not exist
2356    call assert_beeps('norm! "~')
2357    return
2358  endif
2359
2360  " basic test for drag-n-drop
2361  " unfortunately, without a gui, we can't really test much here,
2362  " so simply test that ~p fails (which uses the drop register)
2363  new
2364  call assert_fails(':norm! "~p', 'E353')
2365  call assert_equal([],  getreg('~', 1, 1))
2366  " the ~ register is read only
2367  call assert_fails(':let @~="1"', 'E354')
2368  bw!
2369endfunc
2370
2371func Test_normal46_ignore()
2372  new
2373  " How to test this?
2374  " let's just for now test, that the buffer
2375  " does not change
2376  call feedkeys("\<c-s>", 't')
2377  call assert_equal([''], getline(1,'$'))
2378
2379  " no valid commands
2380  exe "norm! \<char-0x100>"
2381  call assert_equal([''], getline(1,'$'))
2382
2383  exe "norm! ä"
2384  call assert_equal([''], getline(1,'$'))
2385
2386  " clean up
2387  bw!
2388endfunc
2389
2390func Test_normal47_visual_buf_wipe()
2391  " This was causing a crash or ml_get error.
2392  enew!
2393  call setline(1,'xxx')
2394  normal $
2395  new
2396  call setline(1, range(1,2))
2397  2
2398  exe "norm \<C-V>$"
2399  bw!
2400  norm yp
2401  set nomodified
2402endfunc
2403
2404func Test_normal47_autocmd()
2405  " disabled, does not seem to be possible currently
2406  throw "Skipped: not possible to test cursorhold autocmd while waiting for input in normal_cmd"
2407  new
2408  call append(0, repeat('-',20))
2409  au CursorHold * call feedkeys('2l', '')
2410  1
2411  set updatetime=20
2412  " should delete 12 chars (d12l)
2413  call feedkeys('d1', '!')
2414  call assert_equal('--------', getline(1))
2415
2416  " clean up
2417  au! CursorHold
2418  set updatetime=4000
2419  bw!
2420endfunc
2421
2422func Test_normal48_wincmd()
2423  new
2424  exe "norm! \<c-w>c"
2425  call assert_equal(1, winnr('$'))
2426  call assert_fails(":norm! \<c-w>c", "E444")
2427endfunc
2428
2429func Test_normal49_counts()
2430  new
2431  call setline(1, 'one two three four five six seven eight nine ten')
2432  1
2433  norm! 3d2w
2434  call assert_equal('seven eight nine ten', getline(1))
2435  bw!
2436endfunc
2437
2438func Test_normal50_commandline()
2439  CheckFeature timers
2440  CheckFeature cmdline_hist
2441  func! DoTimerWork(id)
2442    call assert_equal('[Command Line]', bufname(''))
2443    " should fail, with E11, but does fail with E23?
2444    "call feedkeys("\<c-^>", 'tm')
2445
2446    " should also fail with E11
2447    call assert_fails(":wincmd p", 'E11')
2448    " return from commandline window
2449    call feedkeys("\<cr>")
2450  endfunc
2451
2452  let oldlang=v:lang
2453  lang C
2454  set updatetime=20
2455  call timer_start(100, 'DoTimerWork')
2456  try
2457    " throws E23, for whatever reason...
2458    call feedkeys('q:', 'x!')
2459  catch /E23/
2460    " no-op
2461  endtry
2462  " clean up
2463  set updatetime=4000
2464  exe "lang" oldlang
2465  bw!
2466endfunc
2467
2468func Test_normal51_FileChangedRO()
2469  CheckFeature autocmd
2470  " Don't sleep after the warning message.
2471  call test_settime(1)
2472  call writefile(['foo'], 'Xreadonly.log')
2473  new Xreadonly.log
2474  setl ro
2475  au FileChangedRO <buffer> :call feedkeys("\<c-^>", 'tix')
2476  call assert_fails(":norm! Af", 'E788')
2477  call assert_equal(['foo'], getline(1,'$'))
2478  call assert_equal('Xreadonly.log', bufname(''))
2479
2480  " cleanup
2481  call test_settime(0)
2482  bw!
2483  call delete("Xreadonly.log")
2484endfunc
2485
2486func Test_normal52_rl()
2487  CheckFeature rightleft
2488  new
2489  call setline(1, 'abcde fghij klmnopq')
2490  norm! 1gg$
2491  set rl
2492  call assert_equal(19, col('.'))
2493  call feedkeys('l', 'tx')
2494  call assert_equal(18, col('.'))
2495  call feedkeys('h', 'tx')
2496  call assert_equal(19, col('.'))
2497  call feedkeys("\<right>", 'tx')
2498  call assert_equal(18, col('.'))
2499  call feedkeys("\<left>", 'tx')
2500  call assert_equal(19, col('.'))
2501  call feedkeys("\<s-right>", 'tx')
2502  call assert_equal(13, col('.'))
2503  call feedkeys("\<c-right>", 'tx')
2504  call assert_equal(7, col('.'))
2505  call feedkeys("\<c-left>", 'tx')
2506  call assert_equal(13, col('.'))
2507  call feedkeys("\<s-left>", 'tx')
2508  call assert_equal(19, col('.'))
2509  call feedkeys("<<", 'tx')
2510  call assert_equal('	abcde fghij klmnopq',getline(1))
2511  call feedkeys(">>", 'tx')
2512  call assert_equal('abcde fghij klmnopq',getline(1))
2513
2514  " cleanup
2515  set norl
2516  bw!
2517endfunc
2518
2519func Test_normal53_digraph()
2520  CheckFeature digraphs
2521  new
2522  call setline(1, 'abcdefgh|')
2523  exe "norm! 1gg0f\<c-k>!!"
2524  call assert_equal(9, col('.'))
2525  set cpo+=D
2526  exe "norm! 1gg0f\<c-k>!!"
2527  call assert_equal(1, col('.'))
2528
2529  set cpo-=D
2530  bw!
2531endfunc
2532
2533func Test_normal54_Ctrl_bsl()
2534  new
2535  call setline(1, 'abcdefghijklmn')
2536  exe "norm! df\<c-\>\<c-n>"
2537  call assert_equal(['abcdefghijklmn'], getline(1,'$'))
2538  exe "norm! df\<c-\>\<c-g>"
2539  call assert_equal(['abcdefghijklmn'], getline(1,'$'))
2540  exe "norm! df\<c-\>m"
2541  call assert_equal(['abcdefghijklmn'], getline(1,'$'))
2542
2543  call setline(2, 'abcdefghijklmnāf')
2544  norm! 2gg0
2545  exe "norm! df\<Char-0x101>"
2546  call assert_equal(['abcdefghijklmn', 'f'], getline(1,'$'))
2547  norm! 1gg0
2548  exe "norm! df\<esc>"
2549  call assert_equal(['abcdefghijklmn', 'f'], getline(1,'$'))
2550
2551  " clean up
2552  bw!
2553endfunc
2554
2555func Test_normal_large_count()
2556  " This may fail with 32bit long, how do we detect that?
2557  new
2558  normal o
2559  normal 6666666666dL
2560  bwipe!
2561endfunc
2562
2563func Test_delete_until_paragraph()
2564  new
2565  normal grádv}
2566  call assert_equal('á', getline(1))
2567  normal grád}
2568  call assert_equal('', getline(1))
2569  bwipe!
2570endfunc
2571
2572" Test for the gr (virtual replace) command
2573" Test for the bug fixed by 7.4.387
2574func Test_gr_command()
2575  enew!
2576  let save_cpo = &cpo
2577  call append(0, ['First line', 'Second line', 'Third line'])
2578  exe "normal i\<C-G>u"
2579  call cursor(2, 1)
2580  set cpo-=X
2581  normal 4gro
2582  call assert_equal('oooond line', getline(2))
2583  undo
2584  set cpo+=X
2585  normal 4gro
2586  call assert_equal('ooooecond line', getline(2))
2587  let &cpo = save_cpo
2588  normal! ggvegrx
2589  call assert_equal('xxxxx line', getline(1))
2590  exe "normal! gggr\<C-V>122"
2591  call assert_equal('zxxxx line', getline(1))
2592  set virtualedit=all
2593  normal! 15|grl
2594  call assert_equal('zxxxx line    l', getline(1))
2595  set virtualedit&
2596  set nomodifiable
2597  call assert_fails('normal! grx', 'E21:')
2598  call assert_fails('normal! gRx', 'E21:')
2599  set modifiable&
2600  enew!
2601endfunc
2602
2603" When splitting a window the changelist position is wrong.
2604" Test the changelist position after splitting a window.
2605" Test for the bug fixed by 7.4.386
2606func Test_changelist()
2607  let save_ul = &ul
2608  enew!
2609  call append('$', ['1', '2'])
2610  exe "normal i\<C-G>u"
2611  exe "normal Gkylpa\<C-G>u"
2612  set ul=100
2613  exe "normal Gylpa\<C-G>u"
2614  set ul=100
2615  normal gg
2616  vsplit
2617  normal g;
2618  call assert_equal([3, 2], [line('.'), col('.')])
2619  normal g;
2620  call assert_equal([2, 2], [line('.'), col('.')])
2621  call assert_fails('normal g;', 'E662:')
2622  new
2623  call assert_fails('normal g;', 'E664:')
2624  %bwipe!
2625  let &ul = save_ul
2626endfunc
2627
2628func Test_nv_hat_count()
2629  %bwipeout!
2630  let l:nr = bufnr('%') + 1
2631  call assert_fails(':execute "normal! ' . l:nr . '\<C-^>"', 'E92')
2632
2633  edit Xfoo
2634  let l:foo_nr = bufnr('Xfoo')
2635
2636  edit Xbar
2637  let l:bar_nr = bufnr('Xbar')
2638
2639  " Make sure we are not just using the alternate file.
2640  edit Xbaz
2641
2642  call feedkeys(l:foo_nr . "\<C-^>", 'tx')
2643  call assert_equal('Xfoo', fnamemodify(bufname('%'), ':t'))
2644
2645  call feedkeys(l:bar_nr . "\<C-^>", 'tx')
2646  call assert_equal('Xbar', fnamemodify(bufname('%'), ':t'))
2647
2648  %bwipeout!
2649endfunc
2650
2651func Test_message_when_using_ctrl_c()
2652  " Make sure no buffers are changed.
2653  %bwipe!
2654
2655  exe "normal \<C-C>"
2656  call assert_match("Type  :qa  and press <Enter> to exit Vim", Screenline(&lines))
2657
2658  new
2659  cal setline(1, 'hi!')
2660  exe "normal \<C-C>"
2661  call assert_match("Type  :qa!  and press <Enter> to abandon all changes and exit Vim", Screenline(&lines))
2662
2663  bwipe!
2664endfunc
2665
2666" Test for '[m', ']m', '[M' and ']M'
2667" Jumping to beginning and end of methods in Java-like languages
2668func Test_java_motion()
2669  new
2670  call assert_beeps('normal! [m')
2671  call assert_beeps('normal! ]m')
2672  call assert_beeps('normal! [M')
2673  call assert_beeps('normal! ]M')
2674  a
2675Piece of Java
2676{
2677	tt m1 {
2678		t1;
2679	} e1
2680
2681	tt m2 {
2682		t2;
2683	} e2
2684
2685	tt m3 {
2686		if (x)
2687		{
2688			t3;
2689		}
2690	} e3
2691}
2692.
2693
2694  normal gg
2695
2696  normal 2]maA
2697  call assert_equal("\ttt m1 {A", getline('.'))
2698  call assert_equal([3, 9, 16], [line('.'), col('.'), virtcol('.')])
2699
2700  normal j]maB
2701  call assert_equal("\ttt m2 {B", getline('.'))
2702  call assert_equal([7, 9, 16], [line('.'), col('.'), virtcol('.')])
2703
2704  normal ]maC
2705  call assert_equal("\ttt m3 {C", getline('.'))
2706  call assert_equal([11, 9, 16], [line('.'), col('.'), virtcol('.')])
2707
2708  normal [maD
2709  call assert_equal("\ttt m3 {DC", getline('.'))
2710  call assert_equal([11, 9, 16], [line('.'), col('.'), virtcol('.')])
2711
2712  normal k2[maE
2713  call assert_equal("\ttt m1 {EA", getline('.'))
2714  call assert_equal([3, 9, 16], [line('.'), col('.'), virtcol('.')])
2715
2716  normal 3[maF
2717  call assert_equal("{F", getline('.'))
2718  call assert_equal([2, 2, 2], [line('.'), col('.'), virtcol('.')])
2719
2720  normal ]MaG
2721  call assert_equal("\t}G e1", getline('.'))
2722  call assert_equal([5, 3, 10], [line('.'), col('.'), virtcol('.')])
2723
2724  normal j2]MaH
2725  call assert_equal("\t}H e3", getline('.'))
2726  call assert_equal([16, 3, 10], [line('.'), col('.'), virtcol('.')])
2727
2728  normal ]M]M
2729  normal aI
2730  call assert_equal("}I", getline('.'))
2731  call assert_equal([17, 2, 2], [line('.'), col('.'), virtcol('.')])
2732
2733  normal 2[MaJ
2734  call assert_equal("\t}JH e3", getline('.'))
2735  call assert_equal([16, 3, 10], [line('.'), col('.'), virtcol('.')])
2736
2737  normal k[MaK
2738  call assert_equal("\t}K e2", getline('.'))
2739  call assert_equal([9, 3, 10], [line('.'), col('.'), virtcol('.')])
2740
2741  normal 3[MaL
2742  call assert_equal("{LF", getline('.'))
2743  call assert_equal([2, 2, 2], [line('.'), col('.'), virtcol('.')])
2744
2745  close!
2746endfunc
2747
2748" Tests for g cmds
2749func Test_normal_gdollar_cmd()
2750  CheckFeature jumplist
2751  call Setup_NewWindow()
2752  " Make long lines that will wrap
2753  %s/$/\=repeat(' foobar', 10)/
2754  20vsp
2755  set wrap
2756  " Test for g$ with count
2757  norm! gg
2758  norm! 0vg$y
2759  call assert_equal(20, col("'>"))
2760  call assert_equal('1 foobar foobar foob', getreg(0))
2761  norm! gg
2762  norm! 0v4g$y
2763  call assert_equal(72, col("'>"))
2764  call assert_equal('1 foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar'.."\n", getreg(0))
2765  norm! gg
2766  norm! 0v6g$y
2767  call assert_equal(40, col("'>"))
2768  call assert_equal('1 foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar'.. "\n"..
2769		  \ '2 foobar foobar foobar foobar foobar foo', getreg(0))
2770  set nowrap
2771  " clean up
2772  norm! gg
2773  norm! 0vg$y
2774  call assert_equal(20, col("'>"))
2775  call assert_equal('1 foobar foobar foob', getreg(0))
2776  norm! gg
2777  norm! 0v4g$y
2778  call assert_equal(20, col("'>"))
2779  call assert_equal('1 foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar'.. "\n"..
2780                 \  '2 foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar'.. "\n"..
2781                 \  '3 foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar'.. "\n"..
2782                 \  '4 foobar foobar foob', getreg(0))
2783  norm! gg
2784  norm! 0v6g$y
2785  call assert_equal(20, col("'>"))
2786  call assert_equal('1 foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar'.. "\n"..
2787                 \  '2 foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar'.. "\n"..
2788                 \  '3 foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar'.. "\n"..
2789                 \  '4 foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar'.. "\n"..
2790                 \  '5 foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar'.. "\n"..
2791                 \  '6 foobar foobar foob', getreg(0))
2792  " Move to last line, also down movement is not possible, should still move
2793  " the cursor to the last visible char
2794  norm! G
2795  norm! 0v6g$y
2796  call assert_equal(20, col("'>"))
2797  call assert_equal('100 foobar foobar fo', getreg(0))
2798  bw!
2799endfunc
2800
2801func Test_normal_gk_gj()
2802  " needs 80 column new window
2803  new
2804  vert 80new
2805  call assert_beeps('normal gk')
2806  put =[repeat('x',90)..' {{{1', 'x {{{1']
2807  norm! gk
2808  " In a 80 column wide terminal the window will be only 78 char
2809  " (because Vim will leave space for the other window),
2810  " but if the terminal is larger, it will be 80 chars, so verify the
2811  " cursor column correctly.
2812  call assert_equal(winwidth(0)+1, col('.'))
2813  call assert_equal(winwidth(0)+1, virtcol('.'))
2814  norm! j
2815  call assert_equal(6, col('.'))
2816  call assert_equal(6, virtcol('.'))
2817  norm! gk
2818  call assert_equal(95, col('.'))
2819  call assert_equal(95, virtcol('.'))
2820  %bw!
2821
2822  " needs 80 column new window
2823  new
2824  vert 80new
2825  call assert_beeps('normal gj')
2826  set number
2827  set numberwidth=10
2828  set cpoptions+=n
2829  put =[repeat('0',90), repeat('1',90)]
2830  norm! 075l
2831  call assert_equal(76, col('.'))
2832  norm! gk
2833  call assert_equal(1, col('.'))
2834  norm! gk
2835  call assert_equal(76, col('.'))
2836  norm! gk
2837  call assert_equal(1, col('.'))
2838  norm! gj
2839  call assert_equal(76, col('.'))
2840  norm! gj
2841  call assert_equal(1, col('.'))
2842  norm! gj
2843  call assert_equal(76, col('.'))
2844  " When 'nowrap' is set, gk and gj behave like k and j
2845  set nowrap
2846  normal! gk
2847  call assert_equal([2, 76], [line('.'), col('.')])
2848  normal! gj
2849  call assert_equal([3, 76], [line('.'), col('.')])
2850  %bw!
2851  set cpoptions& number& numberwidth& wrap&
2852endfunc
2853
2854" Test for cursor movement with '-' in 'cpoptions'
2855func Test_normal_cpo_minus()
2856  new
2857  call setline(1, ['foo', 'bar', 'baz'])
2858  let save_cpo = &cpo
2859  set cpo+=-
2860  call assert_beeps('normal 10j')
2861  call assert_equal(1, line('.'))
2862  normal G
2863  call assert_beeps('normal 10k')
2864  call assert_equal(3, line('.'))
2865  call assert_fails(10, 'E16:')
2866  let &cpo = save_cpo
2867  close!
2868endfunc
2869
2870" Test for displaying dollar when changing text ('$' flag in 'cpoptions')
2871func Test_normal_cpo_dollar()
2872  new
2873  let g:Line = ''
2874  func SaveFirstLine()
2875    let g:Line = Screenline(1)
2876    return ''
2877  endfunc
2878  inoremap <expr> <buffer> <F2> SaveFirstLine()
2879  call test_override('redraw_flag', 1)
2880  set cpo+=$
2881  call setline(1, 'one two three')
2882  redraw!
2883  exe "normal c2w\<F2>vim"
2884  call assert_equal('one tw$ three', g:Line)
2885  call assert_equal('vim three', getline(1))
2886  set cpo-=$
2887  call test_override('ALL', 0)
2888  delfunc SaveFirstLine
2889  %bw!
2890endfunc
2891
2892" Test for using : to run a multi-line Ex command in operator pending mode
2893func Test_normal_yank_with_excmd()
2894  new
2895  call setline(1, ['foo', 'bar', 'baz'])
2896  let @a = ''
2897  call feedkeys("\"ay:if v:true\<CR>normal l\<CR>endif\<CR>", 'xt')
2898  call assert_equal('f', @a)
2899  close!
2900endfunc
2901
2902" Test for supplying a count to a normal-mode command across a cursorhold call
2903func Test_normal_cursorhold_with_count()
2904  func s:cHold()
2905    let g:cHold_Called += 1
2906  endfunc
2907  new
2908  augroup normalcHoldTest
2909    au!
2910    au CursorHold <buffer> call s:cHold()
2911  augroup END
2912  let g:cHold_Called = 0
2913  call feedkeys("3\<CursorHold>2ix", 'xt')
2914  call assert_equal(1, g:cHold_Called)
2915  call assert_equal(repeat('x', 32), getline(1))
2916  augroup normalcHoldTest
2917    au!
2918  augroup END
2919  au! normalcHoldTest
2920  close!
2921  delfunc s:cHold
2922endfunc
2923
2924" Test for using a count and a command with CTRL-W
2925func Test_wincmd_with_count()
2926  call feedkeys("\<C-W>12n", 'xt')
2927  call assert_equal(12, winheight(0))
2928endfunc
2929
2930" Test for 'b', 'B' 'ge' and 'gE' commands
2931func Test_horiz_motion()
2932  new
2933  normal! gg
2934  call assert_beeps('normal! b')
2935  call assert_beeps('normal! B')
2936  call assert_beeps('normal! gE')
2937  call assert_beeps('normal! ge')
2938  " <S-Backspace> moves one word left and <C-Backspace> moves one WORD left
2939  call setline(1, 'one ,two ,three')
2940  exe "normal! $\<S-BS>"
2941  call assert_equal(11, col('.'))
2942  exe "normal! $\<C-BS>"
2943  call assert_equal(10, col('.'))
2944  close!
2945endfunc
2946
2947" Test for using a : command in operator pending mode
2948func Test_normal_colon_op()
2949  new
2950  call setline(1, ['one', 'two'])
2951  call assert_beeps("normal! Gc:d\<CR>")
2952  close!
2953endfunc
2954
2955" Test for d and D commands
2956func Test_normal_delete_cmd()
2957  new
2958  " D in an empty line
2959  call setline(1, '')
2960  normal D
2961  call assert_equal('', getline(1))
2962  " D in an empty line in virtualedit mode
2963  set virtualedit=all
2964  normal D
2965  call assert_equal('', getline(1))
2966  set virtualedit&
2967  " delete to a readonly register
2968  call setline(1, ['abcd'])
2969  call assert_beeps('normal ":d2l')
2970  close!
2971endfunc
2972
2973" Test for the 'E' flag in 'cpo' with yank, change, delete, etc. operators
2974func Test_empty_region_error()
2975  new
2976  call setline(1, '')
2977  set cpo+=E
2978  " yank an empty line
2979  call assert_beeps('normal "ayl')
2980  " change an empty line
2981  call assert_beeps('normal lcTa')
2982  " delete an empty line
2983  call assert_beeps('normal D')
2984  call assert_beeps('normal dl')
2985  call assert_equal('', getline(1))
2986  " change case of an empty line
2987  call assert_beeps('normal gul')
2988  call assert_beeps('normal gUl')
2989  " replace a character
2990  call assert_beeps('normal vrx')
2991  " increment and decrement
2992  call assert_beeps('exe "normal v\<C-A>"')
2993  call assert_beeps('exe "normal v\<C-X>"')
2994  set cpo-=E
2995  close!
2996endfunc
2997
2998" Test for 'w' and 'b' commands
2999func Test_normal_word_move()
3000  new
3001  call setline(1, ['foo bar a', '', 'foo bar b'])
3002  " copy a single character word at the end of a line
3003  normal 1G$yw
3004  call assert_equal('a', @")
3005  " copy a single character word at the end of a file
3006  normal G$yw
3007  call assert_equal('b', @")
3008  " check for a word movement handling an empty line properly
3009  normal 1G$vwy
3010  call assert_equal("a\n\n", @")
3011
3012  " copy using 'b' command
3013  %d
3014  " non-empty blank line at the start of file
3015  call setline(1, ['  ', 'foo bar'])
3016  normal 2Gyb
3017  call assert_equal("  \n", @")
3018  " try to copy backwards from the start of the file
3019  call setline(1, ['one two', 'foo bar'])
3020  call assert_beeps('normal ggyb')
3021  " 'b' command should stop at an empty line
3022  call setline(1, ['one two', '', 'foo bar'])
3023  normal 3Gyb
3024  call assert_equal("\n", @")
3025  normal 3Gy2b
3026  call assert_equal("two\n", @")
3027  " 'b' command should not stop at a non-empty blank line
3028  call setline(1, ['one two', '  ', 'foo bar'])
3029  normal 3Gyb
3030  call assert_equal("two\n  ", @")
3031
3032  close!
3033endfunc
3034
3035" vim: shiftwidth=2 sts=2 expandtab
3036