xref: /vim-8.2.3635/src/testdir/test_normal.vim (revision 577fadfc)
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  set keywordprg=man\ --pager=cat
1362  " Test for using man
1363  2
1364  let a = execute('unsilent norm! K')
1365  call assert_match("man --pager=cat 'man'", a)
1366
1367  " clean up
1368  let &keywordprg = k
1369  bw!
1370endfunc
1371
1372func Test_normal24_rot13()
1373  " Testing for g?? g?g?
1374  new
1375  call append(0, 'abcdefghijklmnopqrstuvwxyzäüö')
1376  1
1377  norm! g??
1378  call assert_equal('nopqrstuvwxyzabcdefghijklmäüö', getline('.'))
1379  norm! g?g?
1380  call assert_equal('abcdefghijklmnopqrstuvwxyzäüö', getline('.'))
1381
1382  " clean up
1383  bw!
1384endfunc
1385
1386func Test_normal25_tag()
1387  " Testing for CTRL-] g CTRL-] g]
1388  " CTRL-W g] CTRL-W CTRL-] CTRL-W g CTRL-]
1389  h
1390  " Test for CTRL-]
1391  call search('\<x\>$')
1392  exe "norm! \<c-]>"
1393  call assert_equal("change.txt", fnamemodify(bufname('%'), ':t'))
1394  norm! yiW
1395  call assert_equal("*x*", @0)
1396  exe ":norm \<c-o>"
1397
1398  " Test for g_CTRL-]
1399  call search('\<v_u\>$')
1400  exe "norm! g\<c-]>"
1401  call assert_equal("change.txt", fnamemodify(bufname('%'), ':t'))
1402  norm! yiW
1403  call assert_equal("*v_u*", @0)
1404  exe ":norm \<c-o>"
1405
1406  " Test for g]
1407  call search('\<i_<Esc>$')
1408  let a = execute(":norm! g]")
1409  call assert_match('i_<Esc>.*insert.txt', a)
1410
1411  if !empty(exepath('cscope')) && has('cscope')
1412    " setting cscopetag changes how g] works
1413    set cst
1414    exe "norm! g]"
1415    call assert_equal("insert.txt", fnamemodify(bufname('%'), ':t'))
1416    norm! yiW
1417    call assert_equal("*i_<Esc>*", @0)
1418    exe ":norm \<c-o>"
1419    " Test for CTRL-W g]
1420    exe "norm! \<C-W>g]"
1421    call assert_equal("insert.txt", fnamemodify(bufname('%'), ':t'))
1422    norm! yiW
1423    call assert_equal("*i_<Esc>*", @0)
1424    call assert_equal(3, winnr('$'))
1425    helpclose
1426    set nocst
1427  endif
1428
1429  " Test for CTRL-W g]
1430  let a = execute("norm! \<C-W>g]")
1431  call assert_match('i_<Esc>.*insert.txt', a)
1432
1433  " Test for CTRL-W CTRL-]
1434  exe "norm! \<C-W>\<C-]>"
1435  call assert_equal("insert.txt", fnamemodify(bufname('%'), ':t'))
1436  norm! yiW
1437  call assert_equal("*i_<Esc>*", @0)
1438  call assert_equal(3, winnr('$'))
1439  helpclose
1440
1441  " Test for CTRL-W g CTRL-]
1442  exe "norm! \<C-W>g\<C-]>"
1443  call assert_equal("insert.txt", fnamemodify(bufname('%'), ':t'))
1444  norm! yiW
1445  call assert_equal("*i_<Esc>*", @0)
1446  call assert_equal(3, winnr('$'))
1447  helpclose
1448
1449  " clean up
1450  helpclose
1451endfunc
1452
1453func Test_normal26_put()
1454  " Test for ]p ]P [p and [P
1455  new
1456  call append(0, ['while read LINE', 'do', '  ((count++))', '  if [ $? -ne 0 ]; then', "    echo 'Error writing file'", '  fi', 'done'])
1457  1
1458  /Error/y a
1459  2
1460  norm! "a]pj"a[p
1461  call assert_equal(['do', "echo 'Error writing file'", "  echo 'Error writing file'", '  ((count++))'], getline(2,5))
1462  1
1463  /^\s\{4}/
1464  exe "norm!  \"a]P3Eldt'"
1465  exe "norm! j\"a[P2Eldt'"
1466  call assert_equal(['  if [ $? -ne 0 ]; then', "    echo 'Error writing'", "    echo 'Error'", "    echo 'Error writing file'", '  fi'], getline(6,10))
1467
1468  " clean up
1469  bw!
1470endfunc
1471
1472func Test_normal27_bracket()
1473  " Test for [' [` ]' ]`
1474  call Setup_NewWindow()
1475  1,21s/.\+/  &   b/
1476  1
1477  norm! $ma
1478  5
1479  norm! $mb
1480  10
1481  norm! $mc
1482  15
1483  norm! $md
1484  20
1485  norm! $me
1486
1487  " Test for ['
1488  9
1489  norm! 2['
1490  call assert_equal('  1   b', getline('.'))
1491  call assert_equal(1, line('.'))
1492  call assert_equal(3, col('.'))
1493
1494  " Test for ]'
1495  norm! ]'
1496  call assert_equal('  5   b', getline('.'))
1497  call assert_equal(5, line('.'))
1498  call assert_equal(3, col('.'))
1499
1500  " No mark after line 21, cursor moves to first non blank on current line
1501  21
1502  norm! $]'
1503  call assert_equal('  21   b', getline('.'))
1504  call assert_equal(21, line('.'))
1505  call assert_equal(3, col('.'))
1506
1507  " Test for [`
1508  norm! 2[`
1509  call assert_equal('  15   b', getline('.'))
1510  call assert_equal(15, line('.'))
1511  call assert_equal(8, col('.'))
1512
1513  " Test for ]`
1514  norm! ]`
1515  call assert_equal('  20   b', getline('.'))
1516  call assert_equal(20, line('.'))
1517  call assert_equal(8, col('.'))
1518
1519  " clean up
1520  bw!
1521endfunc
1522
1523func Test_normal28_parenthesis()
1524  " basic testing for ( and )
1525  new
1526  call append(0, ['This is a test. With some sentences!', '', 'Even with a question? And one more. And no sentence here'])
1527
1528  $
1529  norm! d(
1530  call assert_equal(['This is a test. With some sentences!', '', 'Even with a question? And one more. ', ''], getline(1, '$'))
1531  norm! 2d(
1532  call assert_equal(['This is a test. With some sentences!', '', ' ', ''], getline(1, '$'))
1533  1
1534  norm! 0d)
1535  call assert_equal(['With some sentences!', '', ' ', ''], getline(1, '$'))
1536
1537  call append('$', ['This is a long sentence', '', 'spanning', 'over several lines. '])
1538  $
1539  norm! $d(
1540  call assert_equal(['With some sentences!', '', ' ', '', 'This is a long sentence', ''], getline(1, '$'))
1541
1542  " clean up
1543  bw!
1544endfunc
1545
1546fun! Test_normal29_brace()
1547  " basic test for { and } movements
1548  let text= ['A paragraph begins after each empty line, and also at each of a set of',
1549  \ 'paragraph macros, specified by the pairs of characters in the ''paragraphs''',
1550  \ 'option.  The default is "IPLPPPQPP TPHPLIPpLpItpplpipbp", which corresponds to',
1551  \ 'the macros ".IP", ".LP", etc.  (These are nroff macros, so the dot must be in',
1552  \ 'the first column).  A section boundary is also a paragraph boundary.',
1553  \ 'Note that a blank line (only containing white space) is NOT a paragraph',
1554  \ 'boundary.',
1555  \ '',
1556  \ '',
1557  \ 'Also note that this does not include a ''{'' or ''}'' in the first column.  When',
1558  \ 'the ''{'' flag is in ''cpoptions'' then ''{'' in the first column is used as a',
1559  \ 'paragraph boundary |posix|.',
1560  \ '{',
1561  \ 'This is no paragraph',
1562  \ 'unless the ''{'' is set',
1563  \ 'in ''cpoptions''',
1564  \ '}',
1565  \ '.IP',
1566  \ 'The nroff macros IP separates a paragraph',
1567  \ 'That means, it must be a ''.''',
1568  \ 'followed by IP',
1569  \ '.LPIt does not matter, if afterwards some',
1570  \ 'more characters follow.',
1571  \ '.SHAlso section boundaries from the nroff',
1572  \ 'macros terminate a paragraph. That means',
1573  \ 'a character like this:',
1574  \ '.NH',
1575  \ 'End of text here']
1576  new
1577  call append(0, text)
1578  1
1579  norm! 0d2}
1580  call assert_equal(['.IP',
1581    \  'The nroff macros IP separates a paragraph', 'That means, it must be a ''.''', 'followed by IP',
1582    \ '.LPIt does not matter, if afterwards some', 'more characters follow.', '.SHAlso section boundaries from the nroff',
1583    \  'macros terminate a paragraph. That means', 'a character like this:', '.NH', 'End of text here', ''], getline(1,'$'))
1584  norm! 0d}
1585  call assert_equal(['.LPIt does not matter, if afterwards some', 'more characters follow.',
1586    \ '.SHAlso section boundaries from the nroff', 'macros terminate a paragraph. That means',
1587    \ 'a character like this:', '.NH', 'End of text here', ''], getline(1, '$'))
1588  $
1589  norm! d{
1590  call assert_equal(['.LPIt does not matter, if afterwards some', 'more characters follow.',
1591	\ '.SHAlso section boundaries from the nroff', 'macros terminate a paragraph. That means', 'a character like this:', ''], getline(1, '$'))
1592  norm! d{
1593  call assert_equal(['.LPIt does not matter, if afterwards some', 'more characters follow.', ''], getline(1,'$'))
1594  " Test with { in cpooptions
1595  %d
1596  call append(0, text)
1597  set cpo+={
1598  1
1599  norm! 0d2}
1600  call assert_equal(['{', 'This is no paragraph', 'unless the ''{'' is set', 'in ''cpoptions''', '}',
1601    \ '.IP', 'The nroff macros IP separates a paragraph', 'That means, it must be a ''.''',
1602    \ 'followed by IP', '.LPIt does not matter, if afterwards some', 'more characters follow.',
1603    \ '.SHAlso section boundaries from the nroff', 'macros terminate a paragraph. That means',
1604    \ 'a character like this:', '.NH', 'End of text here', ''], getline(1,'$'))
1605  $
1606  norm! d}
1607  call assert_equal(['{', 'This is no paragraph', 'unless the ''{'' is set', 'in ''cpoptions''', '}',
1608    \ '.IP', 'The nroff macros IP separates a paragraph', 'That means, it must be a ''.''',
1609    \ 'followed by IP', '.LPIt does not matter, if afterwards some', 'more characters follow.',
1610    \ '.SHAlso section boundaries from the nroff', 'macros terminate a paragraph. That means',
1611    \ 'a character like this:', '.NH', 'End of text here', ''], getline(1,'$'))
1612  norm! gg}
1613  norm! d5}
1614  call assert_equal(['{', 'This is no paragraph', 'unless the ''{'' is set', 'in ''cpoptions''', '}', ''], getline(1,'$'))
1615
1616  " clean up
1617  set cpo-={
1618  bw!
1619endfunc
1620
1621fun! Test_normal30_changecase()
1622  new
1623  call append(0, 'This is a simple test: äüöß')
1624  norm! 1ggVu
1625  call assert_equal('this is a simple test: äüöß', getline('.'))
1626  norm! VU
1627  call assert_equal('THIS IS A SIMPLE TEST: ÄÜÖSS', getline('.'))
1628  norm! guu
1629  call assert_equal('this is a simple test: äüöss', getline('.'))
1630  norm! gUgU
1631  call assert_equal('THIS IS A SIMPLE TEST: ÄÜÖSS', getline('.'))
1632  norm! gugu
1633  call assert_equal('this is a simple test: äüöss', getline('.'))
1634  norm! gUU
1635  call assert_equal('THIS IS A SIMPLE TEST: ÄÜÖSS', getline('.'))
1636  norm! 010~
1637  call assert_equal('this is a SIMPLE TEST: ÄÜÖSS', getline('.'))
1638  norm! V~
1639  call assert_equal('THIS IS A simple test: äüöss', getline('.'))
1640
1641  " Turkish ASCII turns to multi-byte.  On some systems Turkish locale
1642  " is available but toupper()/tolower() don't do the right thing.
1643  try
1644    lang tr_TR.UTF-8
1645    set casemap=
1646    let iupper = toupper('i')
1647    if iupper == "\u0130"
1648      call setline(1, 'iI')
1649      1normal gUU
1650      call assert_equal("\u0130I", getline(1))
1651      call assert_equal("\u0130I", toupper("iI"))
1652
1653      call setline(1, 'iI')
1654      1normal guu
1655      call assert_equal("i\u0131", getline(1))
1656      call assert_equal("i\u0131", tolower("iI"))
1657    elseif iupper == "I"
1658      call setline(1, 'iI')
1659      1normal gUU
1660      call assert_equal("II", getline(1))
1661      call assert_equal("II", toupper("iI"))
1662
1663      call setline(1, 'iI')
1664      1normal guu
1665      call assert_equal("ii", getline(1))
1666      call assert_equal("ii", tolower("iI"))
1667    else
1668      call assert_true(false, "expected toupper('i') to be either 'I' or '\u0130'")
1669    endif
1670    set casemap&
1671    call setline(1, 'iI')
1672    1normal gUU
1673    call assert_equal("II", getline(1))
1674    call assert_equal("II", toupper("iI"))
1675
1676    call setline(1, 'iI')
1677    1normal guu
1678    call assert_equal("ii", getline(1))
1679    call assert_equal("ii", tolower("iI"))
1680
1681    lang en_US.UTF-8
1682  catch /E197:/
1683    " can't use Turkish locale
1684    throw 'Skipped: Turkish locale not available'
1685  endtry
1686
1687  " clean up
1688  bw!
1689endfunc
1690
1691fun! Test_normal31_r_cmd()
1692  " Test for r command
1693  new
1694  call append(0, 'This is a simple test: abcd')
1695  exe "norm! 1gg$r\<cr>"
1696  call assert_equal(['This is a simple test: abc', '', ''], getline(1,'$'))
1697  exe "norm! 1gg2wlr\<cr>"
1698  call assert_equal(['This is a', 'simple test: abc', '', ''], getline(1,'$'))
1699  exe "norm! 2gg0W5r\<cr>"
1700  call assert_equal(['This is a', 'simple ', ' abc', '', ''], getline('1', '$'))
1701  set autoindent
1702  call setline(2, ['simple test: abc', ''])
1703  exe "norm! 2gg0W5r\<cr>"
1704  call assert_equal(['This is a', 'simple ', 'abc', '', '', ''], getline('1', '$'))
1705  exe "norm! 1ggVr\<cr>"
1706  call assert_equal('^M^M^M^M^M^M^M^M^M', strtrans(getline(1)))
1707  call setline(1, 'This is a')
1708  exe "norm! 1gg05rf"
1709  call assert_equal('fffffis a', getline(1))
1710
1711  " clean up
1712  set noautoindent
1713  bw!
1714endfunc
1715
1716func Test_normal32_g_cmd1()
1717  " Test for g*, g#
1718  new
1719  call append(0, ['abc.x_foo', 'x_foobar.abc'])
1720  1
1721  norm! $g*
1722  call assert_equal('x_foo', @/)
1723  call assert_equal('x_foobar.abc', getline('.'))
1724  norm! $g#
1725  call assert_equal('abc', @/)
1726  call assert_equal('abc.x_foo', getline('.'))
1727
1728  " clean up
1729  bw!
1730endfunc
1731
1732fun! Test_normal33_g_cmd2()
1733  if !has("jumplist")
1734    return
1735  endif
1736  " Tests for g cmds
1737  call Setup_NewWindow()
1738  " Test for g`
1739  clearjumps
1740  norm! ma10j
1741  let a=execute(':jumps')
1742  " empty jumplist
1743  call assert_equal('>', a[-1:])
1744  norm! g`a
1745  call assert_equal('>', a[-1:])
1746  call assert_equal(1, line('.'))
1747  call assert_equal('1', getline('.'))
1748
1749  " Test for g; and g,
1750  norm! g;
1751  " there is only one change in the changelist
1752  " currently, when we setup the window
1753  call assert_equal(2, line('.'))
1754  call assert_fails(':norm! g;', 'E662')
1755  call assert_fails(':norm! g,', 'E663')
1756  let &ul=&ul
1757  call append('$', ['a', 'b', 'c', 'd'])
1758  let &ul=&ul
1759  call append('$', ['Z', 'Y', 'X', 'W'])
1760  let a = execute(':changes')
1761  call assert_match('2\s\+0\s\+2', a)
1762  call assert_match('101\s\+0\s\+a', a)
1763  call assert_match('105\s\+0\s\+Z', a)
1764  norm! 3g;
1765  call assert_equal(2, line('.'))
1766  norm! 2g,
1767  call assert_equal(105, line('.'))
1768
1769  " Test for g& - global substitute
1770  %d
1771  call setline(1, range(1,10))
1772  call append('$', ['a', 'b', 'c', 'd'])
1773  $s/\w/&&/g
1774  exe "norm! /[1-8]\<cr>"
1775  norm! g&
1776  call assert_equal(['11', '22', '33', '44', '55', '66', '77', '88', '9', '110', 'a', 'b', 'c', 'dd'], getline(1, '$'))
1777
1778  " Test for gv
1779  %d
1780  call append('$', repeat(['abcdefgh'], 8))
1781  exe "norm! 2gg02l\<c-v>2j2ly"
1782  call assert_equal(['cde', 'cde', 'cde'], getreg(0, 1, 1))
1783  " in visual mode, gv swaps current and last selected region
1784  exe "norm! G0\<c-v>4k4lgvd"
1785  call assert_equal(['', 'abfgh', 'abfgh', 'abfgh', 'abcdefgh', 'abcdefgh', 'abcdefgh', 'abcdefgh', 'abcdefgh'], getline(1,'$'))
1786  exe "norm! G0\<c-v>4k4ly"
1787  exe "norm! gvood"
1788  call assert_equal(['', 'abfgh', 'abfgh', 'abfgh', 'fgh', 'fgh', 'fgh', 'fgh', 'fgh'], getline(1,'$'))
1789
1790  " Test for gk/gj
1791  %d
1792  15vsp
1793  set wrap listchars= sbr=
1794  let lineA='abcdefghijklmnopqrstuvwxyz'
1795  let lineB='0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'
1796  $put =lineA
1797  $put =lineB
1798
1799  norm! 3gg0dgk
1800  call assert_equal(['', 'abcdefghijklmno', '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'], getline(1, '$'))
1801  set nu
1802  norm! 3gg0gjdgj
1803  call assert_equal(['', 'abcdefghijklmno', '0123456789AMNOPQRSTUVWXYZ'], getline(1,'$'))
1804
1805  " Test for gJ
1806  norm! 2gggJ
1807  call assert_equal(['', 'abcdefghijklmno0123456789AMNOPQRSTUVWXYZ'], getline(1,'$'))
1808  call assert_equal(16, col('.'))
1809  " shouldn't do anything
1810  norm! 10gJ
1811  call assert_equal(1, col('.'))
1812
1813  " Test for g0 g^ gm g$
1814  exe "norm! 2gg0gji   "
1815  call assert_equal(['', 'abcdefghijk   lmno0123456789AMNOPQRSTUVWXYZ'], getline(1,'$'))
1816  norm! g0yl
1817  call assert_equal(12, col('.'))
1818  call assert_equal(' ', getreg(0))
1819  norm! g$yl
1820  call assert_equal(22, col('.'))
1821  call assert_equal('3', getreg(0))
1822  norm! gmyl
1823  call assert_equal(17, col('.'))
1824  call assert_equal('n', getreg(0))
1825  norm! g^yl
1826  call assert_equal(15, col('.'))
1827  call assert_equal('l', getreg(0))
1828
1829  " Test for gI
1830  norm! gIfoo
1831  call assert_equal(['', 'fooabcdefghijk   lmno0123456789AMNOPQRSTUVWXYZ'], getline(1,'$'))
1832
1833  " Test for gi
1834  wincmd c
1835  %d
1836  set tw=0
1837  call setline(1, ['foobar', 'new line'])
1838  norm! A next word
1839  $put ='third line'
1840  norm! gi another word
1841  call assert_equal(['foobar next word another word', 'new line', 'third line'], getline(1,'$'))
1842
1843  " clean up
1844  bw!
1845endfunc
1846
1847func Test_g_ctrl_g()
1848  new
1849
1850  let a = execute(":norm! g\<c-g>")
1851  call assert_equal("\n--No lines in buffer--", a)
1852
1853  call setline(1, ['first line', 'second line'])
1854
1855  " Test g CTRL-g with dos, mac and unix file type.
1856  norm! gojll
1857  set ff=dos
1858  let a = execute(":norm! g\<c-g>")
1859  call assert_equal("\nCol 3 of 11; Line 2 of 2; Word 3 of 4; Byte 15 of 25", a)
1860
1861  set ff=mac
1862  let a = execute(":norm! g\<c-g>")
1863  call assert_equal("\nCol 3 of 11; Line 2 of 2; Word 3 of 4; Byte 14 of 23", a)
1864
1865  set ff=unix
1866  let a = execute(":norm! g\<c-g>")
1867  call assert_equal("\nCol 3 of 11; Line 2 of 2; Word 3 of 4; Byte 14 of 23", a)
1868
1869  " Test g CTRL-g in visual mode (v)
1870  let a = execute(":norm! gojllvlg\<c-g>")
1871  call assert_equal("\nSelected 1 of 2 Lines; 1 of 4 Words; 2 of 23 Bytes", a)
1872
1873  " Test g CTRL-g in visual mode (CTRL-V) with end col > start col
1874  let a = execute(":norm! \<Esc>gojll\<C-V>kllg\<c-g>")
1875  call assert_equal("\nSelected 3 Cols; 2 of 2 Lines; 2 of 4 Words; 6 of 23 Bytes", a)
1876
1877  " Test g_CTRL-g in visual mode (CTRL-V) with end col < start col
1878  let a = execute(":norm! \<Esc>goll\<C-V>jhhg\<c-g>")
1879  call assert_equal("\nSelected 3 Cols; 2 of 2 Lines; 2 of 4 Words; 6 of 23 Bytes", a)
1880
1881  " Test g CTRL-g in visual mode (CTRL-V) with end_vcol being MAXCOL
1882  let a = execute(":norm! \<Esc>gojll\<C-V>k$g\<c-g>")
1883  call assert_equal("\nSelected 2 of 2 Lines; 4 of 4 Words; 17 of 23 Bytes", a)
1884
1885  " There should be one byte less with noeol
1886  set bin noeol
1887  let a = execute(":norm! \<Esc>gog\<c-g>")
1888  call assert_equal("\nCol 1 of 10; Line 1 of 2; Word 1 of 4; Char 1 of 23; Byte 1 of 22", a)
1889  set bin & eol&
1890
1891  call setline(1, ['Français', '日本語'])
1892
1893  let a = execute(":norm! \<Esc>gojlg\<c-g>")
1894  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)
1895
1896  let a = execute(":norm! \<Esc>gojvlg\<c-g>")
1897  call assert_equal("\nSelected 1 of 2 Lines; 1 of 2 Words; 2 of 13 Chars; 6 of 20 Bytes", a)
1898
1899  let a = execute(":norm! \<Esc>goll\<c-v>jlg\<c-g>")
1900  call assert_equal("\nSelected 4 Cols; 2 of 2 Lines; 2 of 2 Words; 6 of 13 Chars; 11 of 20 Bytes", a)
1901
1902  set fenc=utf8 bomb
1903  let a = execute(":norm! \<Esc>gojlg\<c-g>")
1904  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)
1905
1906  set fenc=utf16 bomb
1907  let a = execute(":norm! g\<c-g>")
1908  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)
1909
1910  set fenc=utf32 bomb
1911  let a = execute(":norm! g\<c-g>")
1912  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)
1913
1914  set fenc& bomb&
1915
1916  set ff&
1917  bwipe!
1918endfunc
1919
1920fun! Test_normal34_g_cmd3()
1921  " Test for g8
1922  new
1923  let a=execute(':norm! 1G0g8')
1924  call assert_equal("\nNUL", a)
1925
1926  call setline(1, 'abcdefghijklmnopqrstuvwxyzäüö')
1927  let a=execute(':norm! 1G$g8')
1928  call assert_equal("\nc3 b6 ", a)
1929
1930  call setline(1, "a\u0302")
1931  let a=execute(':norm! 1G0g8')
1932  call assert_equal("\n61 + cc 82 ", a)
1933
1934  " clean up
1935  bw!
1936endfunc
1937
1938func Test_normal_8g8()
1939  new
1940
1941  " Test 8g8 which finds invalid utf8 at or after the cursor.
1942
1943  " With invalid byte.
1944  call setline(1, "___\xff___")
1945  norm! 1G08g8g
1946  call assert_equal([0, 1, 4, 0, 1], getcurpos())
1947
1948  " With invalid byte before the cursor.
1949  call setline(1, "___\xff___")
1950  norm! 1G$h8g8g
1951  call assert_equal([0, 1, 6, 0, 9], getcurpos())
1952
1953  " With truncated sequence.
1954  call setline(1, "___\xE2\x82___")
1955  norm! 1G08g8g
1956  call assert_equal([0, 1, 4, 0, 1], getcurpos())
1957
1958  " With overlong sequence.
1959  call setline(1, "___\xF0\x82\x82\xAC___")
1960  norm! 1G08g8g
1961  call assert_equal([0, 1, 4, 0, 1], getcurpos())
1962
1963  " With valid utf8.
1964  call setline(1, "café")
1965  norm! 1G08g8
1966  call assert_equal([0, 1, 1, 0, 1], getcurpos())
1967
1968  bw!
1969endfunc
1970
1971fun! Test_normal35_g_cmd4()
1972  " Test for g<
1973  " Cannot capture its output,
1974  " probably a bug, therefore, test disabled:
1975  throw "Skipped: output of g< can't be tested currently"
1976  echo "a\nb\nc\nd"
1977  let b=execute(':norm! g<')
1978  call assert_true(!empty(b), 'failed `execute(g<)`')
1979endfunc
1980
1981fun! Test_normal36_g_cmd5()
1982  new
1983  call append(0, 'abcdefghijklmnopqrstuvwxyz')
1984  set ff=unix
1985  " Test for gp gP
1986  call append(1, range(1,10))
1987  1
1988  norm! 1yy
1989  3
1990  norm! gp
1991  call assert_equal([0, 5, 1, 0, 1], getcurpos())
1992  $
1993  norm! gP
1994  call assert_equal([0, 14, 1, 0, 1], getcurpos())
1995
1996  " Test for go
1997  norm! 26go
1998  call assert_equal([0, 1, 26, 0, 26], getcurpos())
1999  norm! 27go
2000  call assert_equal([0, 1, 26, 0, 26], getcurpos())
2001  norm! 28go
2002  call assert_equal([0, 2, 1, 0, 1], getcurpos())
2003  set ff=dos
2004  norm! 29go
2005  call assert_equal([0, 2, 1, 0, 1], getcurpos())
2006  set ff=unix
2007  norm! gg0
2008  norm! 101go
2009  call assert_equal([0, 13, 26, 0, 26], getcurpos())
2010  norm! 103go
2011  call assert_equal([0, 14, 1, 0, 1], getcurpos())
2012  " count > buffer content
2013  norm! 120go
2014  call assert_equal([0, 14, 1, 0, 2147483647], getcurpos())
2015  " clean up
2016  bw!
2017endfunc
2018
2019fun! Test_normal37_g_cmd6()
2020  " basic test for gt and gT
2021  tabnew 1.txt
2022  tabnew 2.txt
2023  tabnew 3.txt
2024  norm! 1gt
2025  call assert_equal(1, tabpagenr())
2026  norm! 3gt
2027  call assert_equal(3, tabpagenr())
2028  norm! 1gT
2029  " count gT goes not to the absolute tabpagenumber
2030  " but, but goes to the count previous tabpagenumber
2031  call assert_equal(2, tabpagenr())
2032  " wrap around
2033  norm! 3gT
2034  call assert_equal(3, tabpagenr())
2035  " gt does not wrap around
2036  norm! 5gt
2037  call assert_equal(3, tabpagenr())
2038
2039  for i in range(3)
2040    tabclose
2041  endfor
2042  " clean up
2043  call assert_fails(':tabclose', 'E784')
2044endfunc
2045
2046fun! Test_normal38_nvhome()
2047  " Test for <Home> and <C-Home> key
2048  new
2049  call setline(1, range(10))
2050  $
2051  setl et sw=2
2052  norm! V10>$
2053  " count is ignored
2054  exe "norm! 10\<home>"
2055  call assert_equal(1, col('.'))
2056  exe "norm! \<home>"
2057  call assert_equal([0, 10, 1, 0, 1], getcurpos())
2058  exe "norm! 5\<c-home>"
2059  call assert_equal([0, 5, 1, 0, 1], getcurpos())
2060  exe "norm! \<c-home>"
2061  call assert_equal([0, 1, 1, 0, 1], getcurpos())
2062
2063  " clean up
2064  bw!
2065endfunc
2066
2067fun! Test_normal39_cw()
2068  " Test for cw and cW on whitespace
2069  " and cpo+=w setting
2070  new
2071  set tw=0
2072  call append(0, 'here      are   some words')
2073  norm! 1gg0elcwZZZ
2074  call assert_equal('hereZZZare   some words', getline('.'))
2075  norm! 1gg0elcWYYY
2076  call assert_equal('hereZZZareYYYsome words', getline('.'))
2077  set cpo+=w
2078  call setline(1, 'here      are   some words')
2079  norm! 1gg0elcwZZZ
2080  call assert_equal('hereZZZ     are   some words', getline('.'))
2081  norm! 1gg2elcWYYY
2082  call assert_equal('hereZZZ     areYYY  some words', getline('.'))
2083  set cpo-=w
2084  norm! 2gg0cwfoo
2085  call assert_equal('foo', getline('.'))
2086
2087  " clean up
2088  bw!
2089endfunc
2090
2091fun! Test_normal40_ctrl_bsl()
2092  " Basic test for CTRL-\ commands
2093  new
2094  call append(0, 'here      are   some words')
2095  exe "norm! 1gg0a\<C-\>\<C-N>"
2096  call assert_equal('n', mode())
2097  call assert_equal(1, col('.'))
2098  call assert_equal('', visualmode())
2099  exe "norm! 1gg0viw\<C-\>\<C-N>"
2100  call assert_equal('n', mode())
2101  call assert_equal(4, col('.'))
2102  exe "norm! 1gg0a\<C-\>\<C-G>"
2103  call assert_equal('n', mode())
2104  call assert_equal(1, col('.'))
2105  "imap <buffer> , <c-\><c-n>
2106  set im
2107  exe ":norm! \<c-\>\<c-n>dw"
2108  set noim
2109  call assert_equal('are   some words', getline(1))
2110  call assert_false(&insertmode)
2111
2112  " clean up
2113  bw!
2114endfunc
2115
2116fun! Test_normal41_insert_reg()
2117  " Test for <c-r>=, <c-r><c-r>= and <c-r><c-o>=
2118  " in insert mode
2119  new
2120  set sts=2 sw=2 ts=8 tw=0
2121  call append(0, ["aaa\tbbb\tccc", '', '', ''])
2122  let a=getline(1)
2123  norm! 2gg0
2124  exe "norm! a\<c-r>=a\<cr>"
2125  norm! 3gg0
2126  exe "norm! a\<c-r>\<c-r>=a\<cr>"
2127  norm! 4gg0
2128  exe "norm! a\<c-r>\<c-o>=a\<cr>"
2129  call assert_equal(['aaa	bbb	ccc', 'aaa bbb	ccc', 'aaa bbb	ccc', 'aaa	bbb	ccc', ''], getline(1, '$'))
2130
2131  " clean up
2132  set sts=0 sw=8 ts=8
2133  bw!
2134endfunc
2135
2136func Test_normal42_halfpage()
2137  " basic test for Ctrl-D and Ctrl-U
2138  call Setup_NewWindow()
2139  call assert_equal(5, &scroll)
2140  exe "norm! \<c-d>"
2141  call assert_equal('6', getline('.'))
2142  exe "norm! 2\<c-d>"
2143  call assert_equal('8', getline('.'))
2144  call assert_equal(2, &scroll)
2145  set scroll=5
2146  exe "norm! \<c-u>"
2147  call assert_equal('3', getline('.'))
2148  1
2149  set scrolloff=5
2150  exe "norm! \<c-d>"
2151  call assert_equal('10', getline('.'))
2152  exe "norm! \<c-u>"
2153  call assert_equal('5', getline('.'))
2154  1
2155  set scrolloff=99
2156  exe "norm! \<c-d>"
2157  call assert_equal('10', getline('.'))
2158  set scrolloff=0
2159  100
2160  exe "norm! $\<c-u>"
2161  call assert_equal('95', getline('.'))
2162  call assert_equal([0, 95, 1, 0, 1], getcurpos())
2163  100
2164  set nostartofline
2165  exe "norm! $\<c-u>"
2166  call assert_equal('95', getline('.'))
2167  call assert_equal([0, 95, 2, 0, 2147483647], getcurpos())
2168  " cleanup
2169  set startofline
2170  bw!
2171endfunc
2172
2173fun! Test_normal43_textobject1()
2174  " basic tests for text object aw
2175  new
2176  call append(0, ['foobar,eins,foobar', 'foo,zwei,foo    '])
2177  " diw
2178  norm! 1gg0diw
2179  call assert_equal([',eins,foobar', 'foo,zwei,foo    ', ''], getline(1,'$'))
2180  " daw
2181  norm! 2ggEdaw
2182  call assert_equal([',eins,foobar', 'foo,zwei,', ''], getline(1, '$'))
2183  %d
2184  call append(0, ["foo\teins\tfoobar", "foo\tzwei\tfoo   "])
2185  " diW
2186  norm! 2ggwd2iW
2187  call assert_equal(['foo	eins	foobar', 'foo	foo   ', ''], getline(1,'$'))
2188  " daW
2189  norm! 1ggd2aW
2190  call assert_equal(['foobar', 'foo	foo   ', ''], getline(1,'$'))
2191
2192  %d
2193  call append(0, ["foo\teins\tfoobar", "foo\tzwei\tfoo   "])
2194  " aw in visual line mode switches to characterwise mode
2195  norm! 2gg$Vawd
2196  call assert_equal(['foo	eins	foobar', 'foo	zwei	foo'], getline(1,'$'))
2197  norm! 1gg$Viwd
2198  call assert_equal(['foo	eins	', 'foo	zwei	foo'], getline(1,'$'))
2199
2200  " clean up
2201  bw!
2202endfunc
2203
2204func Test_normal44_textobjects2()
2205  " basic testing for is and as text objects
2206  new
2207  call append(0, ['This is a test. With some sentences!', '', 'Even with a question? And one more. And no sentence here'])
2208  " Test for dis - does not remove trailing whitespace
2209  norm! 1gg0dis
2210  call assert_equal([' With some sentences!', '', 'Even with a question? And one more. And no sentence here', ''], getline(1,'$'))
2211  " Test for das - removes leading whitespace
2212  norm! 3ggf?ldas
2213  call assert_equal([' With some sentences!', '', 'Even with a question? And no sentence here', ''], getline(1,'$'))
2214  " when used in visual mode, is made characterwise
2215  norm! 3gg$Visy
2216  call assert_equal('v', visualmode())
2217  " reset visualmode()
2218  norm! 3ggVy
2219  norm! 3gg$Vasy
2220  call assert_equal('v', visualmode())
2221  " basic testing for textobjects a< and at
2222  %d
2223  call setline(1, ['<div> ','<a href="foobar" class="foo">xyz</a>','    </div>', ' '])
2224  " a<
2225  norm! 1gg0da<
2226  call assert_equal([' ', '<a href="foobar" class="foo">xyz</a>', '    </div>', ' '], getline(1,'$'))
2227  norm! 1pj
2228  call assert_equal([' <div>', '<a href="foobar" class="foo">xyz</a>', '    </div>', ' '], getline(1,'$'))
2229  " at
2230  norm! d2at
2231  call assert_equal([' '], getline(1,'$'))
2232  %d
2233  call setline(1, ['<div> ','<a href="foobar" class="foo">xyz</a>','    </div>', ' '])
2234  " i<
2235  norm! 1gg0di<
2236  call assert_equal(['<> ', '<a href="foobar" class="foo">xyz</a>', '    </div>', ' '], getline(1,'$'))
2237  norm! 1Pj
2238  call assert_equal(['<div> ', '<a href="foobar" class="foo">xyz</a>', '    </div>', ' '], getline(1,'$'))
2239  norm! d2it
2240  call assert_equal(['<div></div>',' '], getline(1,'$'))
2241  " basic testing for a[ and i[ text object
2242  %d
2243  call setline(1, [' ', '[', 'one [two]', 'thre', ']'])
2244  norm! 3gg0di[
2245  call assert_equal([' ', '[', ']'], getline(1,'$'))
2246  call setline(1, [' ', '[', 'one [two]', 'thre', ']'])
2247  norm! 3gg0ftd2a[
2248  call assert_equal([' '], getline(1,'$'))
2249  %d
2250  " Test for i" when cursor is in front of a quoted object
2251  call append(0, 'foo "bar"')
2252  norm! 1gg0di"
2253  call assert_equal(['foo ""', ''], getline(1,'$'))
2254
2255  " clean up
2256  bw!
2257endfunc
2258
2259func Test_normal45_drop()
2260  if !has('dnd')
2261    " The ~ register does not exist
2262    call assert_beeps('norm! "~')
2263    return
2264  endif
2265
2266  " basic test for drag-n-drop
2267  " unfortunately, without a gui, we can't really test much here,
2268  " so simply test that ~p fails (which uses the drop register)
2269  new
2270  call assert_fails(':norm! "~p', 'E353')
2271  call assert_equal([],  getreg('~', 1, 1))
2272  " the ~ register is read only
2273  call assert_fails(':let @~="1"', 'E354')
2274  bw!
2275endfunc
2276
2277func Test_normal46_ignore()
2278  new
2279  " How to test this?
2280  " let's just for now test, that the buffer
2281  " does not change
2282  call feedkeys("\<c-s>", 't')
2283  call assert_equal([''], getline(1,'$'))
2284
2285  " no valid commands
2286  exe "norm! \<char-0x100>"
2287  call assert_equal([''], getline(1,'$'))
2288
2289  exe "norm! ä"
2290  call assert_equal([''], getline(1,'$'))
2291
2292  " clean up
2293  bw!
2294endfunc
2295
2296func Test_normal47_visual_buf_wipe()
2297  " This was causing a crash or ml_get error.
2298  enew!
2299  call setline(1,'xxx')
2300  normal $
2301  new
2302  call setline(1, range(1,2))
2303  2
2304  exe "norm \<C-V>$"
2305  bw!
2306  norm yp
2307  set nomodified
2308endfunc
2309
2310func Test_normal47_autocmd()
2311  " disabled, does not seem to be possible currently
2312  throw "Skipped: not possible to test cursorhold autocmd while waiting for input in normal_cmd"
2313  new
2314  call append(0, repeat('-',20))
2315  au CursorHold * call feedkeys('2l', '')
2316  1
2317  set updatetime=20
2318  " should delete 12 chars (d12l)
2319  call feedkeys('d1', '!')
2320  call assert_equal('--------', getline(1))
2321
2322  " clean up
2323  au! CursorHold
2324  set updatetime=4000
2325  bw!
2326endfunc
2327
2328func Test_normal48_wincmd()
2329  new
2330  exe "norm! \<c-w>c"
2331  call assert_equal(1, winnr('$'))
2332  call assert_fails(":norm! \<c-w>c", "E444")
2333endfunc
2334
2335func Test_normal49_counts()
2336  new
2337  call setline(1, 'one two three four five six seven eight nine ten')
2338  1
2339  norm! 3d2w
2340  call assert_equal('seven eight nine ten', getline(1))
2341  bw!
2342endfunc
2343
2344func Test_normal50_commandline()
2345  if !has("timers") || !has("cmdline_hist")
2346    return
2347  endif
2348  func! DoTimerWork(id)
2349    call assert_equal('[Command Line]', bufname(''))
2350    " should fail, with E11, but does fail with E23?
2351    "call feedkeys("\<c-^>", 'tm')
2352
2353    " should also fail with E11
2354    call assert_fails(":wincmd p", 'E11')
2355    " return from commandline window
2356    call feedkeys("\<cr>")
2357  endfunc
2358
2359  let oldlang=v:lang
2360  lang C
2361  set updatetime=20
2362  call timer_start(100, 'DoTimerWork')
2363  try
2364    " throws E23, for whatever reason...
2365    call feedkeys('q:', 'x!')
2366  catch /E23/
2367    " no-op
2368  endtry
2369  " clean up
2370  set updatetime=4000
2371  exe "lang" oldlang
2372  bw!
2373endfunc
2374
2375func Test_normal51_FileChangedRO()
2376  if !has("autocmd")
2377    return
2378  endif
2379  " Don't sleep after the warning message.
2380  call test_settime(1)
2381  call writefile(['foo'], 'Xreadonly.log')
2382  new Xreadonly.log
2383  setl ro
2384  au FileChangedRO <buffer> :call feedkeys("\<c-^>", 'tix')
2385  call assert_fails(":norm! Af", 'E788')
2386  call assert_equal(['foo'], getline(1,'$'))
2387  call assert_equal('Xreadonly.log', bufname(''))
2388
2389  " cleanup
2390  call test_settime(0)
2391  bw!
2392  call delete("Xreadonly.log")
2393endfunc
2394
2395func Test_normal52_rl()
2396  if !has("rightleft")
2397    return
2398  endif
2399  new
2400  call setline(1, 'abcde fghij klmnopq')
2401  norm! 1gg$
2402  set rl
2403  call assert_equal(19, col('.'))
2404  call feedkeys('l', 'tx')
2405  call assert_equal(18, col('.'))
2406  call feedkeys('h', 'tx')
2407  call assert_equal(19, col('.'))
2408  call feedkeys("\<right>", 'tx')
2409  call assert_equal(18, col('.'))
2410  call feedkeys("\<s-right>", 'tx')
2411  call assert_equal(13, col('.'))
2412  call feedkeys("\<c-right>", 'tx')
2413  call assert_equal(7, col('.'))
2414  call feedkeys("\<c-left>", 'tx')
2415  call assert_equal(13, col('.'))
2416  call feedkeys("\<s-left>", 'tx')
2417  call assert_equal(19, col('.'))
2418  call feedkeys("<<", 'tx')
2419  call assert_equal('	abcde fghij klmnopq',getline(1))
2420  call feedkeys(">>", 'tx')
2421  call assert_equal('abcde fghij klmnopq',getline(1))
2422
2423  " cleanup
2424  set norl
2425  bw!
2426endfunc
2427
2428func Test_normal53_digraph()
2429  if !has('digraphs')
2430    return
2431  endif
2432  new
2433  call setline(1, 'abcdefgh|')
2434  exe "norm! 1gg0f\<c-k>!!"
2435  call assert_equal(9, col('.'))
2436  set cpo+=D
2437  exe "norm! 1gg0f\<c-k>!!"
2438  call assert_equal(1, col('.'))
2439
2440  set cpo-=D
2441  bw!
2442endfunc
2443
2444func Test_normal54_Ctrl_bsl()
2445  new
2446  call setline(1, 'abcdefghijklmn')
2447  exe "norm! df\<c-\>\<c-n>"
2448  call assert_equal(['abcdefghijklmn'], getline(1,'$'))
2449  exe "norm! df\<c-\>\<c-g>"
2450  call assert_equal(['abcdefghijklmn'], getline(1,'$'))
2451  exe "norm! df\<c-\>m"
2452  call assert_equal(['abcdefghijklmn'], getline(1,'$'))
2453
2454  call setline(2, 'abcdefghijklmnāf')
2455  norm! 2gg0
2456  exe "norm! df\<Char-0x101>"
2457  call assert_equal(['abcdefghijklmn', 'f'], getline(1,'$'))
2458  norm! 1gg0
2459  exe "norm! df\<esc>"
2460  call assert_equal(['abcdefghijklmn', 'f'], getline(1,'$'))
2461
2462  " clean up
2463  bw!
2464endfunc
2465
2466func Test_normal_large_count()
2467  " This may fail with 32bit long, how do we detect that?
2468  new
2469  normal o
2470  normal 6666666666dL
2471  bwipe!
2472endfunc
2473
2474func Test_delete_until_paragraph()
2475  new
2476  normal grádv}
2477  call assert_equal('á', getline(1))
2478  normal grád}
2479  call assert_equal('', getline(1))
2480  bwipe!
2481endfunc
2482
2483" Test for the gr (virtual replace) command
2484" Test for the bug fixed by 7.4.387
2485func Test_gr_command()
2486  enew!
2487  let save_cpo = &cpo
2488  call append(0, ['First line', 'Second line', 'Third line'])
2489  exe "normal i\<C-G>u"
2490  call cursor(2, 1)
2491  set cpo-=X
2492  normal 4gro
2493  call assert_equal('oooond line', getline(2))
2494  undo
2495  set cpo+=X
2496  normal 4gro
2497  call assert_equal('ooooecond line', getline(2))
2498  let &cpo = save_cpo
2499  enew!
2500endfunc
2501
2502" When splitting a window the changelist position is wrong.
2503" Test the changelist position after splitting a window.
2504" Test for the bug fixed by 7.4.386
2505func Test_changelist()
2506  let save_ul = &ul
2507  enew!
2508  call append('$', ['1', '2'])
2509  exe "normal i\<C-G>u"
2510  exe "normal Gkylpa\<C-G>u"
2511  set ul=100
2512  exe "normal Gylpa\<C-G>u"
2513  set ul=100
2514  normal gg
2515  vsplit
2516  normal g;
2517  call assert_equal([3, 2], [line('.'), col('.')])
2518  normal g;
2519  call assert_equal([2, 2], [line('.'), col('.')])
2520  call assert_fails('normal g;', 'E662:')
2521  %bwipe!
2522  let &ul = save_ul
2523endfunc
2524
2525func Test_nv_hat_count()
2526  %bwipeout!
2527  let l:nr = bufnr('%') + 1
2528  call assert_fails(':execute "normal! ' . l:nr . '\<C-^>"', 'E92')
2529
2530  edit Xfoo
2531  let l:foo_nr = bufnr('Xfoo')
2532
2533  edit Xbar
2534  let l:bar_nr = bufnr('Xbar')
2535
2536  " Make sure we are not just using the alternate file.
2537  edit Xbaz
2538
2539  call feedkeys(l:foo_nr . "\<C-^>", 'tx')
2540  call assert_equal('Xfoo', fnamemodify(bufname('%'), ':t'))
2541
2542  call feedkeys(l:bar_nr . "\<C-^>", 'tx')
2543  call assert_equal('Xbar', fnamemodify(bufname('%'), ':t'))
2544
2545  %bwipeout!
2546endfunc
2547
2548func Test_message_when_using_ctrl_c()
2549  " Make sure no buffers are changed.
2550  %bwipe!
2551
2552  exe "normal \<C-C>"
2553  call assert_match("Type  :qa  and press <Enter> to exit Vim", Screenline(&lines))
2554
2555  new
2556  cal setline(1, 'hi!')
2557  exe "normal \<C-C>"
2558  call assert_match("Type  :qa!  and press <Enter> to abandon all changes and exit Vim", Screenline(&lines))
2559
2560  bwipe!
2561endfunc
2562