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