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