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