xref: /vim-8.2.3635/src/testdir/test_normal.vim (revision bc93cebb)
1" Test for various Normal mode commands
2
3source shared.vim
4source check.vim
5
6func Setup_NewWindow()
7  10new
8  call setline(1, range(1,100))
9endfunc
10
11func MyFormatExpr()
12  " Adds '->$' at lines having numbers followed by trailing whitespace
13  for ln in range(v:lnum, v:lnum+v:count-1)
14    let line = getline(ln)
15    if getline(ln) =~# '\d\s\+$'
16      call setline(ln, substitute(line, '\s\+$', '', '') . '->$')
17    endif
18  endfor
19endfunc
20
21func CountSpaces(type, ...)
22  " for testing operatorfunc
23  " will count the number of spaces
24  " and return the result in g:a
25  let sel_save = &selection
26  let &selection = "inclusive"
27  let reg_save = @@
28
29  if a:0  " Invoked from Visual mode, use gv command.
30    silent exe "normal! gvy"
31  elseif a:type == 'line'
32    silent exe "normal! '[V']y"
33  else
34    silent exe "normal! `[v`]y"
35  endif
36  let g:a=strlen(substitute(@@, '[^ ]', '', 'g'))
37  let &selection = sel_save
38  let @@ = reg_save
39endfunc
40
41func OpfuncDummy(type, ...)
42  " for testing operatorfunc
43  let g:opt=&linebreak
44
45  if a:0  " Invoked from Visual mode, use gv command.
46    silent exe "normal! gvy"
47  elseif a:type == 'line'
48    silent exe "normal! '[V']y"
49  else
50    silent exe "normal! `[v`]y"
51  endif
52  " Create a new dummy window
53  new
54  let g:bufnr=bufnr('%')
55endfunc
56
57fun! Test_normal00_optrans()
58  new
59  call append(0, ['1 This is a simple test: abcd', '2 This is the second line', '3 this is the third line'])
60  1
61  exe "norm! Sfoobar\<esc>"
62  call assert_equal(['foobar', '2 This is the second line', '3 this is the third line', ''], getline(1,'$'))
63  2
64  exe "norm! $vbsone"
65  call assert_equal(['foobar', '2 This is the second one', '3 this is the third line', ''], getline(1,'$'))
66  norm! VS Second line here
67  call assert_equal(['foobar', ' Second line here', '3 this is the third line', ''], getline(1, '$'))
68  %d
69  call append(0, ['4 This is a simple test: abcd', '5 This is the second line', '6 this is the third line'])
70  call append(0, ['1 This is a simple test: abcd', '2 This is the second line', '3 this is the third line'])
71
72  1
73  norm! 2D
74  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,'$'))
75  set cpo+=#
76  norm! 4D
77  call assert_equal(['', '4 This is a simple test: abcd', '5 This is the second line', '6 this is the third line', ''], getline(1,'$'))
78
79  " clean up
80  set cpo-=#
81  bw!
82endfunc
83
84func Test_normal01_keymodel()
85  call Setup_NewWindow()
86  " Test 1: depending on 'keymodel' <s-down> does something different
87  50
88  call feedkeys("V\<S-Up>y", 'tx')
89  call assert_equal(['47', '48', '49', '50'], getline("'<", "'>"))
90  set keymodel=startsel
91  50
92  call feedkeys("V\<S-Up>y", 'tx')
93  call assert_equal(['49', '50'], getline("'<", "'>"))
94  " Start visual mode when keymodel = startsel
95  50
96  call feedkeys("\<S-Up>y", 'tx')
97  call assert_equal(['49', '5'], getreg(0, 0, 1))
98  " Do not start visual mode when keymodel=
99  set keymodel=
100  50
101  call feedkeys("\<S-Up>y$", 'tx')
102  call assert_equal(['42'], getreg(0, 0, 1))
103  " Stop visual mode when keymodel=stopsel
104  set keymodel=stopsel
105  50
106  call feedkeys("Vkk\<Up>yy", 'tx')
107  call assert_equal(['47'], getreg(0, 0, 1))
108
109  set keymodel=
110  50
111  call feedkeys("Vkk\<Up>yy", 'tx')
112  call assert_equal(['47', '48', '49', '50'], getreg(0, 0, 1))
113
114  " clean up
115  bw!
116endfunc
117
118func Test_normal02_selectmode()
119  " some basic select mode tests
120  call Setup_NewWindow()
121  50
122  norm! gHy
123  call assert_equal('y51', getline('.'))
124  call setline(1, range(1,100))
125  50
126  exe ":norm! V9jo\<c-g>y"
127  call assert_equal('y60', getline('.'))
128  " clean up
129  bw!
130endfunc
131
132func Test_normal02_selectmode2()
133  " some basic select mode tests
134  call Setup_NewWindow()
135  50
136  call feedkeys(":set im\n\<c-o>gHc\<c-o>:set noim\n", 'tx')
137  call assert_equal('c51', getline('.'))
138  " clean up
139  bw!
140endfunc
141
142func Test_normal03_join()
143  " basic join test
144  call Setup_NewWindow()
145  50
146  norm! VJ
147  call assert_equal('50 51', getline('.'))
148  $
149  norm! J
150  call assert_equal('100', getline('.'))
151  $
152  norm! V9-gJ
153  call assert_equal('919293949596979899100', getline('.'))
154  call setline(1, range(1,100))
155  $
156  :j 10
157  call assert_equal('100', getline('.'))
158  " clean up
159  bw!
160endfunc
161
162func Test_normal04_filter()
163  " basic filter test
164  " only test on non windows platform
165  if has('win32')
166    return
167  endif
168  call Setup_NewWindow()
169  1
170  call feedkeys("!!sed -e 's/^/|    /'\n", 'tx')
171  call assert_equal('|    1', getline('.'))
172  90
173  :sil :!echo one
174  call feedkeys('.', 'tx')
175  call assert_equal('|    90', getline('.'))
176  95
177  set cpo+=!
178  " 2 <CR>, 1: for executing the command,
179  "         2: clear hit-enter-prompt
180  call feedkeys("!!\n", 'tx')
181  call feedkeys(":!echo one\n\n", 'tx')
182  call feedkeys(".", 'tx')
183  call assert_equal('one', getline('.'))
184  set cpo-=!
185  bw!
186endfunc
187
188func Test_normal05_formatexpr()
189  " basic formatexpr test
190  call Setup_NewWindow()
191  %d_
192  call setline(1, ['here: 1   ', '2', 'here: 3   ', '4', 'not here:   '])
193  1
194  set formatexpr=MyFormatExpr()
195  norm! gqG
196  call assert_equal(['here: 1->$', '2', 'here: 3->$', '4', 'not here:   '], getline(1,'$'))
197  set formatexpr=
198  bw!
199endfunc
200
201func Test_normal05_formatexpr_newbuf()
202  " Edit another buffer in the 'formatexpr' function
203  new
204  func! Format()
205    edit another
206  endfunc
207  set formatexpr=Format()
208  norm gqG
209  bw!
210  set formatexpr=
211endfunc
212
213func Test_normal05_formatexpr_setopt()
214  " Change the 'formatexpr' value in the function
215  new
216  func! Format()
217    set formatexpr=
218  endfunc
219  set formatexpr=Format()
220  norm gqG
221  bw!
222  set formatexpr=
223endfunc
224
225func Test_normal06_formatprg()
226  " basic test for formatprg
227  " only test on non windows platform
228  if has('win32')
229    return
230  endif
231
232  " uses sed to number non-empty lines
233  call writefile(['#!/bin/sh', 'sed ''/./=''|sed ''/./{', 'N', 's/\n/    /', '}'''], 'Xsed_format.sh')
234  call system('chmod +x ./Xsed_format.sh')
235  let text = ['a', '', 'c', '', ' ', 'd', 'e']
236  let expected = ['1    a', '', '3    c', '', '5     ', '6    d', '7    e']
237
238  10new
239  call setline(1, text)
240  set formatprg=./Xsed_format.sh
241  norm! gggqG
242  call assert_equal(expected, getline(1, '$'))
243  bw!
244
245  10new
246  call setline(1, text)
247  set formatprg=donothing
248  setlocal formatprg=./Xsed_format.sh
249  norm! gggqG
250  call assert_equal(expected, getline(1, '$'))
251  bw!
252
253  " clean up
254  set formatprg=
255  setlocal formatprg=
256  call delete('Xsed_format.sh')
257endfunc
258
259func Test_normal07_internalfmt()
260  " basic test for internal formmatter to textwidth of 12
261  let list=range(1,11)
262  call map(list, 'v:val."    "')
263  10new
264  call setline(1, list)
265  set tw=12
266  norm! gggqG
267  call assert_equal(['1    2    3', '4    5    6', '7    8    9', '10    11    '], getline(1, '$'))
268  " clean up
269  set tw=0
270  bw!
271endfunc
272
273func Test_normal08_fold()
274  " basic tests for foldopen/folddelete
275  if !has("folding")
276    return
277  endif
278  call Setup_NewWindow()
279  50
280  setl foldenable fdm=marker
281  " First fold
282  norm! V4jzf
283  " check that folds have been created
284  call assert_equal(['50/*{{{*/', '51', '52', '53', '54/*}}}*/'], getline(50,54))
285  " Second fold
286  46
287  norm! V10jzf
288  " check that folds have been created
289  call assert_equal('46/*{{{*/', getline(46))
290  call assert_equal('60/*}}}*/', getline(60))
291  norm! k
292  call assert_equal('45', getline('.'))
293  norm! j
294  call assert_equal('46/*{{{*/', getline('.'))
295  norm! j
296  call assert_equal('61', getline('.'))
297  norm! k
298  " open a fold
299  norm! Vzo
300  norm! k
301  call assert_equal('45', getline('.'))
302  norm! j
303  call assert_equal('46/*{{{*/', getline('.'))
304  norm! j
305  call assert_equal('47', getline('.'))
306  norm! k
307  norm! zcVzO
308  call assert_equal('46/*{{{*/', getline('.'))
309  norm! j
310  call assert_equal('47', getline('.'))
311  norm! j
312  call assert_equal('48', getline('.'))
313  norm! j
314  call assert_equal('49', getline('.'))
315  norm! j
316  call assert_equal('50/*{{{*/', getline('.'))
317  norm! j
318  call assert_equal('51', getline('.'))
319  " delete folds
320  :46
321  " collapse fold
322  norm! V14jzC
323  " delete all folds recursively
324  norm! VzD
325  call assert_equal(['46', '47', '48', '49', '50', '51', '52', '53', '54', '55', '56', '57', '58', '59', '60'], getline(46,60))
326
327  " clean up
328  setl nofoldenable fdm=marker
329  bw!
330endfunc
331
332func Test_normal09_operatorfunc()
333  " Test operatorfunc
334  call Setup_NewWindow()
335  " Add some spaces for counting
336  50,60s/$/  /
337  unlet! g:a
338  let g:a=0
339  nmap <buffer><silent> ,, :set opfunc=CountSpaces<CR>g@
340  vmap <buffer><silent> ,, :<C-U>call CountSpaces(visualmode(), 1)<CR>
341  50
342  norm V2j,,
343  call assert_equal(6, g:a)
344  norm V,,
345  call assert_equal(2, g:a)
346  norm ,,l
347  call assert_equal(0, g:a)
348  50
349  exe "norm 0\<c-v>10j2l,,"
350  call assert_equal(11, g:a)
351  50
352  norm V10j,,
353  call assert_equal(22, g:a)
354
355  " clean up
356  unmap <buffer> ,,
357  set opfunc=
358  unlet! g:a
359  bw!
360endfunc
361
362func Test_normal09a_operatorfunc()
363  " Test operatorfunc
364  call Setup_NewWindow()
365  " Add some spaces for counting
366  50,60s/$/  /
367  unlet! g:opt
368  set linebreak
369  nmap <buffer><silent> ,, :set opfunc=OpfuncDummy<CR>g@
370  50
371  norm ,,j
372  exe "bd!" g:bufnr
373  call assert_true(&linebreak)
374  call assert_equal(g:opt, &linebreak)
375  set nolinebreak
376  norm ,,j
377  exe "bd!" g:bufnr
378  call assert_false(&linebreak)
379  call assert_equal(g:opt, &linebreak)
380
381  " clean up
382  unmap <buffer> ,,
383  set opfunc=
384  bw!
385  unlet! g:opt
386endfunc
387
388func Test_normal10_expand()
389  " Test for expand()
390  10new
391  call setline(1, ['1', 'ifooar,,cbar'])
392  2
393  norm! $
394  call assert_equal('cbar', expand('<cword>'))
395  call assert_equal('ifooar,,cbar', expand('<cWORD>'))
396
397  call setline(1, ['prx = list[idx];'])
398  1
399  let expected = ['', 'prx', 'prx', 'prx',
400	\ 'list', 'list', 'list', 'list', 'list', 'list', 'list',
401	\ 'idx', 'idx', 'idx', 'idx',
402	\ 'list[idx]',
403	\ '];',
404	\ ]
405  for i in range(1, 16)
406    exe 'norm ' . i . '|'
407    call assert_equal(expected[i], expand('<cexpr>'), 'i == ' . i)
408  endfor
409
410  if executable('echo')
411    " Test expand(`...`) i.e. backticks command expansion.
412    call assert_equal('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_normal20_exmode()
1093  if !has("unix")
1094    " Reading from redirected file doesn't work on MS-Windows
1095    return
1096  endif
1097  call writefile(['1a', 'foo', 'bar', '.', 'w! Xfile2', 'q!'], 'Xscript')
1098  call writefile(['1', '2'], 'Xfile')
1099  call system(GetVimCommand() .. ' -e -s < Xscript Xfile')
1100  let a=readfile('Xfile2')
1101  call assert_equal(['1', 'foo', 'bar', '2'], a)
1102
1103  " clean up
1104  for file in ['Xfile', 'Xfile2', 'Xscript']
1105    call delete(file)
1106  endfor
1107  bw!
1108endfunc
1109
1110func Test_normal21_nv_hat()
1111
1112  " Edit a fresh file and wipe the buffer list so that there is no alternate
1113  " file present.  Next, check for the expected command failures.
1114  edit Xfoo | %bw
1115  call assert_fails(':buffer #', 'E86')
1116  call assert_fails(':execute "normal! \<C-^>"', 'E23')
1117
1118  " Test for the expected behavior when switching between two named buffers.
1119  edit Xfoo | edit Xbar
1120  call feedkeys("\<C-^>", 'tx')
1121  call assert_equal('Xfoo', fnamemodify(bufname('%'), ':t'))
1122  call feedkeys("\<C-^>", 'tx')
1123  call assert_equal('Xbar', fnamemodify(bufname('%'), ':t'))
1124
1125  " Test for the expected behavior when only one buffer is named.
1126  enew | let l:nr = bufnr('%')
1127  call feedkeys("\<C-^>", 'tx')
1128  call assert_equal('Xbar', fnamemodify(bufname('%'), ':t'))
1129  call feedkeys("\<C-^>", 'tx')
1130  call assert_equal('', bufname('%'))
1131  call assert_equal(l:nr, bufnr('%'))
1132
1133  " Test that no action is taken by "<C-^>" when an operator is pending.
1134  edit Xfoo
1135  call feedkeys("ci\<C-^>", 'tx')
1136  call assert_equal('Xfoo', fnamemodify(bufname('%'), ':t'))
1137
1138  %bw!
1139endfunc
1140
1141func Test_normal22_zet()
1142  " Test for ZZ
1143  " let shell = &shell
1144  " let &shell = 'sh'
1145  call writefile(['1', '2'], 'Xfile')
1146  let args = ' -N -i NONE --noplugins -X --not-a-term'
1147  call system(GetVimCommand() .. args .. ' -c "%d" -c ":norm! ZZ" Xfile')
1148  let a = readfile('Xfile')
1149  call assert_equal([], a)
1150  " Test for ZQ
1151  call writefile(['1', '2'], 'Xfile')
1152  call system(GetVimCommand() . args . ' -c "%d" -c ":norm! ZQ" Xfile')
1153  let a = readfile('Xfile')
1154  call assert_equal(['1', '2'], a)
1155
1156  " clean up
1157  for file in ['Xfile']
1158    call delete(file)
1159  endfor
1160  " let &shell = shell
1161endfunc
1162
1163func Test_normal23_K()
1164  " Test for K command
1165  new
1166  call append(0, ['version8.txt', 'man', 'aa%bb', 'cc|dd'])
1167  let k = &keywordprg
1168  set keywordprg=:help
1169  1
1170  norm! VK
1171  call assert_equal('version8.txt', fnamemodify(bufname('%'), ':t'))
1172  call assert_equal('help', &ft)
1173  call assert_match('\*version8.txt\*', getline('.'))
1174  helpclose
1175  norm! 0K
1176  call assert_equal('version8.txt', fnamemodify(bufname('%'), ':t'))
1177  call assert_equal('help', &ft)
1178  call assert_match('\*version8\.\d\*', getline('.'))
1179  helpclose
1180
1181  set keywordprg=:new
1182  set iskeyword+=%
1183  set iskeyword+=\|
1184  2
1185  norm! K
1186  call assert_equal('man', fnamemodify(bufname('%'), ':t'))
1187  bwipe!
1188  3
1189  norm! K
1190  call assert_equal('aa%bb', fnamemodify(bufname('%'), ':t'))
1191  bwipe!
1192  if !has('win32')
1193    4
1194    norm! K
1195    call assert_equal('cc|dd', fnamemodify(bufname('%'), ':t'))
1196    bwipe!
1197  endif
1198  set iskeyword-=%
1199  set iskeyword-=\|
1200
1201  " Only expect "man" to work on Unix
1202  if !has("unix")
1203    let &keywordprg = k
1204    bw!
1205    return
1206  endif
1207
1208  let not_gnu_man = has('mac') || has('bsd')
1209  if not_gnu_man
1210    " In MacOS and BSD, the option for specifying a pager is different
1211    set keywordprg=man\ -P\ cat
1212  else
1213    set keywordprg=man\ --pager=cat
1214  endif
1215  " Test for using man
1216  2
1217  let a = execute('unsilent norm! K')
1218  if not_gnu_man
1219    call assert_match("man -P cat 'man'", a)
1220  else
1221    call assert_match("man --pager=cat 'man'", a)
1222  endif
1223
1224  " clean up
1225  let &keywordprg = k
1226  bw!
1227endfunc
1228
1229func Test_normal24_rot13()
1230  " Testing for g?? g?g?
1231  new
1232  call append(0, 'abcdefghijklmnopqrstuvwxyzäüö')
1233  1
1234  norm! g??
1235  call assert_equal('nopqrstuvwxyzabcdefghijklmäüö', getline('.'))
1236  norm! g?g?
1237  call assert_equal('abcdefghijklmnopqrstuvwxyzäüö', getline('.'))
1238
1239  " clean up
1240  bw!
1241endfunc
1242
1243func Test_normal25_tag()
1244  CheckFeature quickfix
1245
1246  " Testing for CTRL-] g CTRL-] g]
1247  " CTRL-W g] CTRL-W CTRL-] CTRL-W g CTRL-]
1248  h
1249  " Test for CTRL-]
1250  call search('\<x\>$')
1251  exe "norm! \<c-]>"
1252  call assert_equal("change.txt", fnamemodify(bufname('%'), ':t'))
1253  norm! yiW
1254  call assert_equal("*x*", @0)
1255  exe ":norm \<c-o>"
1256
1257  " Test for g_CTRL-]
1258  call search('\<v_u\>$')
1259  exe "norm! g\<c-]>"
1260  call assert_equal("change.txt", fnamemodify(bufname('%'), ':t'))
1261  norm! yiW
1262  call assert_equal("*v_u*", @0)
1263  exe ":norm \<c-o>"
1264
1265  " Test for g]
1266  call search('\<i_<Esc>$')
1267  let a = execute(":norm! g]")
1268  call assert_match('i_<Esc>.*insert.txt', a)
1269
1270  if !empty(exepath('cscope')) && has('cscope')
1271    " setting cscopetag changes how g] works
1272    set cst
1273    exe "norm! g]"
1274    call assert_equal("insert.txt", fnamemodify(bufname('%'), ':t'))
1275    norm! yiW
1276    call assert_equal("*i_<Esc>*", @0)
1277    exe ":norm \<c-o>"
1278    " Test for CTRL-W g]
1279    exe "norm! \<C-W>g]"
1280    call assert_equal("insert.txt", fnamemodify(bufname('%'), ':t'))
1281    norm! yiW
1282    call assert_equal("*i_<Esc>*", @0)
1283    call assert_equal(3, winnr('$'))
1284    helpclose
1285    set nocst
1286  endif
1287
1288  " Test for CTRL-W g]
1289  let a = execute("norm! \<C-W>g]")
1290  call assert_match('i_<Esc>.*insert.txt', a)
1291
1292  " Test for CTRL-W CTRL-]
1293  exe "norm! \<C-W>\<C-]>"
1294  call assert_equal("insert.txt", fnamemodify(bufname('%'), ':t'))
1295  norm! yiW
1296  call assert_equal("*i_<Esc>*", @0)
1297  call assert_equal(3, winnr('$'))
1298  helpclose
1299
1300  " Test for CTRL-W g CTRL-]
1301  exe "norm! \<C-W>g\<C-]>"
1302  call assert_equal("insert.txt", fnamemodify(bufname('%'), ':t'))
1303  norm! yiW
1304  call assert_equal("*i_<Esc>*", @0)
1305  call assert_equal(3, winnr('$'))
1306  helpclose
1307
1308  " clean up
1309  helpclose
1310endfunc
1311
1312func Test_normal26_put()
1313  " Test for ]p ]P [p and [P
1314  new
1315  call append(0, ['while read LINE', 'do', '  ((count++))', '  if [ $? -ne 0 ]; then', "    echo 'Error writing file'", '  fi', 'done'])
1316  1
1317  /Error/y a
1318  2
1319  norm! "a]pj"a[p
1320  call assert_equal(['do', "echo 'Error writing file'", "  echo 'Error writing file'", '  ((count++))'], getline(2,5))
1321  1
1322  /^\s\{4}/
1323  exe "norm!  \"a]P3Eldt'"
1324  exe "norm! j\"a[P2Eldt'"
1325  call assert_equal(['  if [ $? -ne 0 ]; then', "    echo 'Error writing'", "    echo 'Error'", "    echo 'Error writing file'", '  fi'], getline(6,10))
1326
1327  " clean up
1328  bw!
1329endfunc
1330
1331func Test_normal27_bracket()
1332  " Test for [' [` ]' ]`
1333  call Setup_NewWindow()
1334  1,21s/.\+/  &   b/
1335  1
1336  norm! $ma
1337  5
1338  norm! $mb
1339  10
1340  norm! $mc
1341  15
1342  norm! $md
1343  20
1344  norm! $me
1345
1346  " Test for ['
1347  9
1348  norm! 2['
1349  call assert_equal('  1   b', getline('.'))
1350  call assert_equal(1, line('.'))
1351  call assert_equal(3, col('.'))
1352
1353  " Test for ]'
1354  norm! ]'
1355  call assert_equal('  5   b', getline('.'))
1356  call assert_equal(5, line('.'))
1357  call assert_equal(3, col('.'))
1358
1359  " No mark after line 21, cursor moves to first non blank on current line
1360  21
1361  norm! $]'
1362  call assert_equal('  21   b', getline('.'))
1363  call assert_equal(21, line('.'))
1364  call assert_equal(3, col('.'))
1365
1366  " Test for [`
1367  norm! 2[`
1368  call assert_equal('  15   b', getline('.'))
1369  call assert_equal(15, line('.'))
1370  call assert_equal(8, col('.'))
1371
1372  " Test for ]`
1373  norm! ]`
1374  call assert_equal('  20   b', getline('.'))
1375  call assert_equal(20, line('.'))
1376  call assert_equal(8, col('.'))
1377
1378  " clean up
1379  bw!
1380endfunc
1381
1382func Test_normal28_parenthesis()
1383  " basic testing for ( and )
1384  new
1385  call append(0, ['This is a test. With some sentences!', '', 'Even with a question? And one more. And no sentence here'])
1386
1387  $
1388  norm! d(
1389  call assert_equal(['This is a test. With some sentences!', '', 'Even with a question? And one more. ', ''], getline(1, '$'))
1390  norm! 2d(
1391  call assert_equal(['This is a test. With some sentences!', '', ' ', ''], getline(1, '$'))
1392  1
1393  norm! 0d)
1394  call assert_equal(['With some sentences!', '', ' ', ''], getline(1, '$'))
1395
1396  call append('$', ['This is a long sentence', '', 'spanning', 'over several lines. '])
1397  $
1398  norm! $d(
1399  call assert_equal(['With some sentences!', '', ' ', '', 'This is a long sentence', ''], getline(1, '$'))
1400
1401  " clean up
1402  bw!
1403endfunc
1404
1405fun! Test_normal29_brace()
1406  " basic test for { and } movements
1407  let text =<< trim [DATA]
1408    A paragraph begins after each empty line, and also at each of a set of
1409    paragraph macros, specified by the pairs of characters in the 'paragraphs'
1410    option.  The default is "IPLPPPQPP TPHPLIPpLpItpplpipbp", which corresponds to
1411    the macros ".IP", ".LP", etc.  (These are nroff macros, so the dot must be in
1412    the first column).  A section boundary is also a paragraph boundary.
1413    Note that a blank line (only containing white space) is NOT a paragraph
1414    boundary.
1415
1416
1417    Also note that this does not include a '{' or '}' in the first column.  When
1418    the '{' flag is in 'cpoptions' then '{' in the first column is used as a
1419    paragraph boundary |posix|.
1420    {
1421    This is no paragraph
1422    unless the '{' is set
1423    in 'cpoptions'
1424    }
1425    .IP
1426    The nroff macros IP separates a paragraph
1427    That means, it must be a '.'
1428    followed by IP
1429    .LPIt does not matter, if afterwards some
1430    more characters follow.
1431    .SHAlso section boundaries from the nroff
1432    macros terminate a paragraph. That means
1433    a character like this:
1434    .NH
1435    End of text here
1436  [DATA]
1437
1438  new
1439  call append(0, text)
1440  1
1441  norm! 0d2}
1442
1443  let expected =<< trim [DATA]
1444    .IP
1445    The nroff macros IP separates a paragraph
1446    That means, it must be a '.'
1447    followed by IP
1448    .LPIt does not matter, if afterwards some
1449    more characters follow.
1450    .SHAlso section boundaries from the nroff
1451    macros terminate a paragraph. That means
1452    a character like this:
1453    .NH
1454    End of text here
1455
1456  [DATA]
1457  call assert_equal(expected, getline(1, '$'))
1458
1459  norm! 0d}
1460
1461  let expected =<< trim [DATA]
1462    .LPIt does not matter, if afterwards some
1463    more characters follow.
1464    .SHAlso section boundaries from the nroff
1465    macros terminate a paragraph. That means
1466    a character like this:
1467    .NH
1468    End of text here
1469
1470  [DATA]
1471  call assert_equal(expected, getline(1, '$'))
1472
1473  $
1474  norm! d{
1475
1476  let expected =<< trim [DATA]
1477    .LPIt does not matter, if afterwards some
1478    more characters follow.
1479    .SHAlso section boundaries from the nroff
1480    macros terminate a paragraph. That means
1481    a character like this:
1482
1483  [DATA]
1484  call assert_equal(expected, getline(1, '$'))
1485
1486  norm! d{
1487
1488  let expected =<< trim [DATA]
1489    .LPIt does not matter, if afterwards some
1490    more characters follow.
1491
1492  [DATA]
1493  call assert_equal(expected, getline(1, '$'))
1494
1495  " Test with { in cpooptions
1496  %d
1497  call append(0, text)
1498  set cpo+={
1499  1
1500  norm! 0d2}
1501
1502  let expected =<< trim [DATA]
1503    {
1504    This is no paragraph
1505    unless the '{' is set
1506    in 'cpoptions'
1507    }
1508    .IP
1509    The nroff macros IP separates a paragraph
1510    That means, it must be a '.'
1511    followed by IP
1512    .LPIt does not matter, if afterwards some
1513    more characters follow.
1514    .SHAlso section boundaries from the nroff
1515    macros terminate a paragraph. That means
1516    a character like this:
1517    .NH
1518    End of text here
1519
1520  [DATA]
1521  call assert_equal(expected, getline(1, '$'))
1522
1523  $
1524  norm! d}
1525
1526  let expected =<< trim [DATA]
1527    {
1528    This is no paragraph
1529    unless the '{' is set
1530    in 'cpoptions'
1531    }
1532    .IP
1533    The nroff macros IP separates a paragraph
1534    That means, it must be a '.'
1535    followed by IP
1536    .LPIt does not matter, if afterwards some
1537    more characters follow.
1538    .SHAlso section boundaries from the nroff
1539    macros terminate a paragraph. That means
1540    a character like this:
1541    .NH
1542    End of text here
1543
1544  [DATA]
1545  call assert_equal(expected, getline(1, '$'))
1546
1547  norm! gg}
1548  norm! d5}
1549
1550  let expected =<< trim [DATA]
1551    {
1552    This is no paragraph
1553    unless the '{' is set
1554    in 'cpoptions'
1555    }
1556
1557  [DATA]
1558  call assert_equal(expected, getline(1, '$'))
1559
1560  " clean up
1561  set cpo-={
1562  bw!
1563endfunc
1564
1565fun! Test_normal30_changecase()
1566  new
1567  call append(0, 'This is a simple test: äüöß')
1568  norm! 1ggVu
1569  call assert_equal('this is a simple test: äüöß', getline('.'))
1570  norm! VU
1571  call assert_equal('THIS IS A SIMPLE TEST: ÄÜÖSS', getline('.'))
1572  norm! guu
1573  call assert_equal('this is a simple test: äüöss', getline('.'))
1574  norm! gUgU
1575  call assert_equal('THIS IS A SIMPLE TEST: ÄÜÖSS', getline('.'))
1576  norm! gugu
1577  call assert_equal('this is a simple test: äüöss', getline('.'))
1578  norm! gUU
1579  call assert_equal('THIS IS A SIMPLE TEST: ÄÜÖSS', getline('.'))
1580  norm! 010~
1581  call assert_equal('this is a SIMPLE TEST: ÄÜÖSS', getline('.'))
1582  norm! V~
1583  call assert_equal('THIS IS A simple test: äüöss', getline('.'))
1584
1585  " Turkish ASCII turns to multi-byte.  On some systems Turkish locale
1586  " is available but toupper()/tolower() don't do the right thing.
1587  try
1588    lang tr_TR.UTF-8
1589    set casemap=
1590    let iupper = toupper('i')
1591    if iupper == "\u0130"
1592      call setline(1, 'iI')
1593      1normal gUU
1594      call assert_equal("\u0130I", getline(1))
1595      call assert_equal("\u0130I", toupper("iI"))
1596
1597      call setline(1, 'iI')
1598      1normal guu
1599      call assert_equal("i\u0131", getline(1))
1600      call assert_equal("i\u0131", tolower("iI"))
1601    elseif iupper == "I"
1602      call setline(1, 'iI')
1603      1normal gUU
1604      call assert_equal("II", getline(1))
1605      call assert_equal("II", toupper("iI"))
1606
1607      call setline(1, 'iI')
1608      1normal guu
1609      call assert_equal("ii", getline(1))
1610      call assert_equal("ii", tolower("iI"))
1611    else
1612      call assert_true(false, "expected toupper('i') to be either 'I' or '\u0130'")
1613    endif
1614    set casemap&
1615    call setline(1, 'iI')
1616    1normal gUU
1617    call assert_equal("II", getline(1))
1618    call assert_equal("II", toupper("iI"))
1619
1620    call setline(1, 'iI')
1621    1normal guu
1622    call assert_equal("ii", getline(1))
1623    call assert_equal("ii", tolower("iI"))
1624
1625    lang en_US.UTF-8
1626  catch /E197:/
1627    " can't use Turkish locale
1628    throw 'Skipped: Turkish locale not available'
1629  endtry
1630
1631  " clean up
1632  bw!
1633endfunc
1634
1635fun! Test_normal31_r_cmd()
1636  " Test for r command
1637  new
1638  call append(0, 'This is a simple test: abcd')
1639  exe "norm! 1gg$r\<cr>"
1640  call assert_equal(['This is a simple test: abc', '', ''], getline(1,'$'))
1641  exe "norm! 1gg2wlr\<cr>"
1642  call assert_equal(['This is a', 'simple test: abc', '', ''], getline(1,'$'))
1643  exe "norm! 2gg0W5r\<cr>"
1644  call assert_equal(['This is a', 'simple ', ' abc', '', ''], getline('1', '$'))
1645  set autoindent
1646  call setline(2, ['simple test: abc', ''])
1647  exe "norm! 2gg0W5r\<cr>"
1648  call assert_equal(['This is a', 'simple ', 'abc', '', '', ''], getline('1', '$'))
1649  exe "norm! 1ggVr\<cr>"
1650  call assert_equal('^M^M^M^M^M^M^M^M^M', strtrans(getline(1)))
1651  call setline(1, 'This is a')
1652  exe "norm! 1gg05rf"
1653  call assert_equal('fffffis a', getline(1))
1654
1655  " clean up
1656  set noautoindent
1657  bw!
1658endfunc
1659
1660func Test_normal32_g_cmd1()
1661  " Test for g*, g#
1662  new
1663  call append(0, ['abc.x_foo', 'x_foobar.abc'])
1664  1
1665  norm! $g*
1666  call assert_equal('x_foo', @/)
1667  call assert_equal('x_foobar.abc', getline('.'))
1668  norm! $g#
1669  call assert_equal('abc', @/)
1670  call assert_equal('abc.x_foo', getline('.'))
1671
1672  " clean up
1673  bw!
1674endfunc
1675
1676fun! Test_normal33_g_cmd2()
1677  if !has("jumplist")
1678    return
1679  endif
1680  " Tests for g cmds
1681  call Setup_NewWindow()
1682  " Test for g`
1683  clearjumps
1684  norm! ma10j
1685  let a=execute(':jumps')
1686  " empty jumplist
1687  call assert_equal('>', a[-1:])
1688  norm! g`a
1689  call assert_equal('>', a[-1:])
1690  call assert_equal(1, line('.'))
1691  call assert_equal('1', getline('.'))
1692
1693  " Test for g; and g,
1694  norm! g;
1695  " there is only one change in the changelist
1696  " currently, when we setup the window
1697  call assert_equal(2, line('.'))
1698  call assert_fails(':norm! g;', 'E662')
1699  call assert_fails(':norm! g,', 'E663')
1700  let &ul=&ul
1701  call append('$', ['a', 'b', 'c', 'd'])
1702  let &ul=&ul
1703  call append('$', ['Z', 'Y', 'X', 'W'])
1704  let a = execute(':changes')
1705  call assert_match('2\s\+0\s\+2', a)
1706  call assert_match('101\s\+0\s\+a', a)
1707  call assert_match('105\s\+0\s\+Z', a)
1708  norm! 3g;
1709  call assert_equal(2, line('.'))
1710  norm! 2g,
1711  call assert_equal(105, line('.'))
1712
1713  " Test for g& - global substitute
1714  %d
1715  call setline(1, range(1,10))
1716  call append('$', ['a', 'b', 'c', 'd'])
1717  $s/\w/&&/g
1718  exe "norm! /[1-8]\<cr>"
1719  norm! g&
1720  call assert_equal(['11', '22', '33', '44', '55', '66', '77', '88', '9', '110', 'a', 'b', 'c', 'dd'], getline(1, '$'))
1721
1722  " Test for gv
1723  %d
1724  call append('$', repeat(['abcdefgh'], 8))
1725  exe "norm! 2gg02l\<c-v>2j2ly"
1726  call assert_equal(['cde', 'cde', 'cde'], getreg(0, 1, 1))
1727  " in visual mode, gv swaps current and last selected region
1728  exe "norm! G0\<c-v>4k4lgvd"
1729  call assert_equal(['', 'abfgh', 'abfgh', 'abfgh', 'abcdefgh', 'abcdefgh', 'abcdefgh', 'abcdefgh', 'abcdefgh'], getline(1,'$'))
1730  exe "norm! G0\<c-v>4k4ly"
1731  exe "norm! gvood"
1732  call assert_equal(['', 'abfgh', 'abfgh', 'abfgh', 'fgh', 'fgh', 'fgh', 'fgh', 'fgh'], getline(1,'$'))
1733
1734  " Test for gk/gj
1735  %d
1736  15vsp
1737  set wrap listchars= sbr=
1738  let lineA='abcdefghijklmnopqrstuvwxyz'
1739  let lineB='0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'
1740  let lineC='0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz01234567890123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
1741  $put =lineA
1742  $put =lineB
1743
1744  norm! 3gg0dgk
1745  call assert_equal(['', 'abcdefghijklmno', '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'], getline(1, '$'))
1746  set nu
1747  norm! 3gg0gjdgj
1748  call assert_equal(['', 'abcdefghijklmno', '0123456789AMNOPQRSTUVWXYZ'], getline(1,'$'))
1749
1750  " Test for gJ
1751  norm! 2gggJ
1752  call assert_equal(['', 'abcdefghijklmno0123456789AMNOPQRSTUVWXYZ'], getline(1,'$'))
1753  call assert_equal(16, col('.'))
1754  " shouldn't do anything
1755  norm! 10gJ
1756  call assert_equal(1, col('.'))
1757
1758  " Test for g0 g^ gm g$
1759  exe "norm! 2gg0gji   "
1760  call assert_equal(['', 'abcdefghijk   lmno0123456789AMNOPQRSTUVWXYZ'], getline(1,'$'))
1761  norm! g0yl
1762  call assert_equal(12, col('.'))
1763  call assert_equal(' ', getreg(0))
1764  norm! g$yl
1765  call assert_equal(22, col('.'))
1766  call assert_equal('3', getreg(0))
1767  norm! gmyl
1768  call assert_equal(17, col('.'))
1769  call assert_equal('n', getreg(0))
1770  norm! g^yl
1771  call assert_equal(15, col('.'))
1772  call assert_equal('l', getreg(0))
1773
1774  norm! 2ggdd
1775  $put =lineC
1776
1777  " Test for gM
1778  norm! gMyl
1779  call assert_equal(73, col('.'))
1780  call assert_equal('0', getreg(0))
1781  " Test for 20gM
1782  norm! 20gMyl
1783  call assert_equal(29, col('.'))
1784  call assert_equal('S', getreg(0))
1785  " Test for 60gM
1786  norm! 60gMyl
1787  call assert_equal(87, col('.'))
1788  call assert_equal('E', getreg(0))
1789
1790  " Test for g Ctrl-G
1791  set ff=unix
1792  let a=execute(":norm! g\<c-g>")
1793  call assert_match('Col 87 of 144; Line 2 of 2; Word 1 of 1; Byte 88 of 146', a)
1794
1795  " Test for gI
1796  norm! gIfoo
1797  call assert_equal(['', 'foo0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz01234567890123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'], getline(1,'$'))
1798
1799  " Test for gi
1800  wincmd c
1801  %d
1802  set tw=0
1803  call setline(1, ['foobar', 'new line'])
1804  norm! A next word
1805  $put ='third line'
1806  norm! gi another word
1807  call assert_equal(['foobar next word another word', 'new line', 'third line'], getline(1,'$'))
1808
1809  " clean up
1810  bw!
1811endfunc
1812
1813func Test_g_ctrl_g()
1814  new
1815
1816  let a = execute(":norm! g\<c-g>")
1817  call assert_equal("\n--No lines in buffer--", a)
1818
1819  call setline(1, ['first line', 'second line'])
1820
1821  " Test g CTRL-g with dos, mac and unix file type.
1822  norm! gojll
1823  set ff=dos
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 15 of 25", a)
1826
1827  set ff=mac
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  set ff=unix
1832  let a = execute(":norm! g\<c-g>")
1833  call assert_equal("\nCol 3 of 11; Line 2 of 2; Word 3 of 4; Byte 14 of 23", a)
1834
1835  " Test g CTRL-g in visual mode (v)
1836  let a = execute(":norm! gojllvlg\<c-g>")
1837  call assert_equal("\nSelected 1 of 2 Lines; 1 of 4 Words; 2 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>gojll\<C-V>kllg\<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 col < start col
1844  let a = execute(":norm! \<Esc>goll\<C-V>jhhg\<c-g>")
1845  call assert_equal("\nSelected 3 Cols; 2 of 2 Lines; 2 of 4 Words; 6 of 23 Bytes", a)
1846
1847  " Test g CTRL-g in visual mode (CTRL-V) with end_vcol being MAXCOL
1848  let a = execute(":norm! \<Esc>gojll\<C-V>k$g\<c-g>")
1849  call assert_equal("\nSelected 2 of 2 Lines; 4 of 4 Words; 17 of 23 Bytes", a)
1850
1851  " There should be one byte less with noeol
1852  set bin noeol
1853  let a = execute(":norm! \<Esc>gog\<c-g>")
1854  call assert_equal("\nCol 1 of 10; Line 1 of 2; Word 1 of 4; Char 1 of 23; Byte 1 of 22", a)
1855  set bin & eol&
1856
1857  call setline(1, ['Français', '日本語'])
1858
1859  let a = execute(":norm! \<Esc>gojlg\<c-g>")
1860  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)
1861
1862  let a = execute(":norm! \<Esc>gojvlg\<c-g>")
1863  call assert_equal("\nSelected 1 of 2 Lines; 1 of 2 Words; 2 of 13 Chars; 6 of 20 Bytes", a)
1864
1865  let a = execute(":norm! \<Esc>goll\<c-v>jlg\<c-g>")
1866  call assert_equal("\nSelected 4 Cols; 2 of 2 Lines; 2 of 2 Words; 6 of 13 Chars; 11 of 20 Bytes", a)
1867
1868  set fenc=utf8 bomb
1869  let a = execute(":norm! \<Esc>gojlg\<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(+3 for BOM)", a)
1871
1872  set fenc=utf16 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(+2 for BOM)", a)
1875
1876  set fenc=utf32 bomb
1877  let a = execute(":norm! g\<c-g>")
1878  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)
1879
1880  set fenc& bomb&
1881
1882  set ff&
1883  bwipe!
1884endfunc
1885
1886fun! Test_normal34_g_cmd3()
1887  " Test for g8
1888  new
1889  let a=execute(':norm! 1G0g8')
1890  call assert_equal("\nNUL", a)
1891
1892  call setline(1, 'abcdefghijklmnopqrstuvwxyzäüö')
1893  let a=execute(':norm! 1G$g8')
1894  call assert_equal("\nc3 b6 ", a)
1895
1896  call setline(1, "a\u0302")
1897  let a=execute(':norm! 1G0g8')
1898  call assert_equal("\n61 + cc 82 ", a)
1899
1900  " clean up
1901  bw!
1902endfunc
1903
1904func Test_normal_8g8()
1905  new
1906
1907  " Test 8g8 which finds invalid utf8 at or after the cursor.
1908
1909  " With invalid byte.
1910  call setline(1, "___\xff___")
1911  norm! 1G08g8g
1912  call assert_equal([0, 1, 4, 0, 1], getcurpos())
1913
1914  " With invalid byte before the cursor.
1915  call setline(1, "___\xff___")
1916  norm! 1G$h8g8g
1917  call assert_equal([0, 1, 6, 0, 9], getcurpos())
1918
1919  " With truncated sequence.
1920  call setline(1, "___\xE2\x82___")
1921  norm! 1G08g8g
1922  call assert_equal([0, 1, 4, 0, 1], getcurpos())
1923
1924  " With overlong sequence.
1925  call setline(1, "___\xF0\x82\x82\xAC___")
1926  norm! 1G08g8g
1927  call assert_equal([0, 1, 4, 0, 1], getcurpos())
1928
1929  " With valid utf8.
1930  call setline(1, "café")
1931  norm! 1G08g8
1932  call assert_equal([0, 1, 1, 0, 1], getcurpos())
1933
1934  bw!
1935endfunc
1936
1937fun! Test_normal35_g_cmd4()
1938  " Test for g<
1939  " Cannot capture its output,
1940  " probably a bug, therefore, test disabled:
1941  throw "Skipped: output of g< can't be tested currently"
1942  echo "a\nb\nc\nd"
1943  let b=execute(':norm! g<')
1944  call assert_true(!empty(b), 'failed `execute(g<)`')
1945endfunc
1946
1947fun! Test_normal36_g_cmd5()
1948  new
1949  call append(0, 'abcdefghijklmnopqrstuvwxyz')
1950  set ff=unix
1951  " Test for gp gP
1952  call append(1, range(1,10))
1953  1
1954  norm! 1yy
1955  3
1956  norm! gp
1957  call assert_equal([0, 5, 1, 0, 1], getcurpos())
1958  $
1959  norm! gP
1960  call assert_equal([0, 14, 1, 0, 1], getcurpos())
1961
1962  " Test for go
1963  norm! 26go
1964  call assert_equal([0, 1, 26, 0, 26], getcurpos())
1965  norm! 27go
1966  call assert_equal([0, 1, 26, 0, 26], getcurpos())
1967  norm! 28go
1968  call assert_equal([0, 2, 1, 0, 1], getcurpos())
1969  set ff=dos
1970  norm! 29go
1971  call assert_equal([0, 2, 1, 0, 1], getcurpos())
1972  set ff=unix
1973  norm! gg0
1974  norm! 101go
1975  call assert_equal([0, 13, 26, 0, 26], getcurpos())
1976  norm! 103go
1977  call assert_equal([0, 14, 1, 0, 1], getcurpos())
1978  " count > buffer content
1979  norm! 120go
1980  call assert_equal([0, 14, 1, 0, 2147483647], getcurpos())
1981  " clean up
1982  bw!
1983endfunc
1984
1985fun! Test_normal37_g_cmd6()
1986  " basic test for gt and gT
1987  tabnew 1.txt
1988  tabnew 2.txt
1989  tabnew 3.txt
1990  norm! 1gt
1991  call assert_equal(1, tabpagenr())
1992  norm! 3gt
1993  call assert_equal(3, tabpagenr())
1994  norm! 1gT
1995  " count gT goes not to the absolute tabpagenumber
1996  " but, but goes to the count previous tabpagenumber
1997  call assert_equal(2, tabpagenr())
1998  " wrap around
1999  norm! 3gT
2000  call assert_equal(3, tabpagenr())
2001  " gt does not wrap around
2002  norm! 5gt
2003  call assert_equal(3, tabpagenr())
2004
2005  for i in range(3)
2006    tabclose
2007  endfor
2008  " clean up
2009  call assert_fails(':tabclose', 'E784:')
2010endfunc
2011
2012fun! Test_normal38_nvhome()
2013  " Test for <Home> and <C-Home> key
2014  new
2015  call setline(1, range(10))
2016  $
2017  setl et sw=2
2018  norm! V10>$
2019  " count is ignored
2020  exe "norm! 10\<home>"
2021  call assert_equal(1, col('.'))
2022  exe "norm! \<home>"
2023  call assert_equal([0, 10, 1, 0, 1], getcurpos())
2024  exe "norm! 5\<c-home>"
2025  call assert_equal([0, 5, 1, 0, 1], getcurpos())
2026  exe "norm! \<c-home>"
2027  call assert_equal([0, 1, 1, 0, 1], getcurpos())
2028
2029  " clean up
2030  bw!
2031endfunc
2032
2033fun! Test_normal39_cw()
2034  " Test for cw and cW on whitespace
2035  " and cpo+=w setting
2036  new
2037  set tw=0
2038  call append(0, 'here      are   some words')
2039  norm! 1gg0elcwZZZ
2040  call assert_equal('hereZZZare   some words', getline('.'))
2041  norm! 1gg0elcWYYY
2042  call assert_equal('hereZZZareYYYsome words', getline('.'))
2043  set cpo+=w
2044  call setline(1, 'here      are   some words')
2045  norm! 1gg0elcwZZZ
2046  call assert_equal('hereZZZ     are   some words', getline('.'))
2047  norm! 1gg2elcWYYY
2048  call assert_equal('hereZZZ     areYYY  some words', getline('.'))
2049  set cpo-=w
2050  norm! 2gg0cwfoo
2051  call assert_equal('foo', getline('.'))
2052
2053  " clean up
2054  bw!
2055endfunc
2056
2057fun! Test_normal40_ctrl_bsl()
2058  " Basic test for CTRL-\ commands
2059  new
2060  call append(0, 'here      are   some words')
2061  exe "norm! 1gg0a\<C-\>\<C-N>"
2062  call assert_equal('n', mode())
2063  call assert_equal(1, col('.'))
2064  call assert_equal('', visualmode())
2065  exe "norm! 1gg0viw\<C-\>\<C-N>"
2066  call assert_equal('n', mode())
2067  call assert_equal(4, col('.'))
2068  exe "norm! 1gg0a\<C-\>\<C-G>"
2069  call assert_equal('n', mode())
2070  call assert_equal(1, col('.'))
2071  "imap <buffer> , <c-\><c-n>
2072  set im
2073  exe ":norm! \<c-\>\<c-n>dw"
2074  set noim
2075  call assert_equal('are   some words', getline(1))
2076  call assert_false(&insertmode)
2077
2078  " clean up
2079  bw!
2080endfunc
2081
2082fun! Test_normal41_insert_reg()
2083  " Test for <c-r>=, <c-r><c-r>= and <c-r><c-o>=
2084  " in insert mode
2085  new
2086  set sts=2 sw=2 ts=8 tw=0
2087  call append(0, ["aaa\tbbb\tccc", '', '', ''])
2088  let a=getline(1)
2089  norm! 2gg0
2090  exe "norm! a\<c-r>=a\<cr>"
2091  norm! 3gg0
2092  exe "norm! a\<c-r>\<c-r>=a\<cr>"
2093  norm! 4gg0
2094  exe "norm! a\<c-r>\<c-o>=a\<cr>"
2095  call assert_equal(['aaa	bbb	ccc', 'aaa bbb	ccc', 'aaa bbb	ccc', 'aaa	bbb	ccc', ''], getline(1, '$'))
2096
2097  " clean up
2098  set sts=0 sw=8 ts=8
2099  bw!
2100endfunc
2101
2102func Test_normal42_halfpage()
2103  " basic test for Ctrl-D and Ctrl-U
2104  call Setup_NewWindow()
2105  call assert_equal(5, &scroll)
2106  exe "norm! \<c-d>"
2107  call assert_equal('6', getline('.'))
2108  exe "norm! 2\<c-d>"
2109  call assert_equal('8', getline('.'))
2110  call assert_equal(2, &scroll)
2111  set scroll=5
2112  exe "norm! \<c-u>"
2113  call assert_equal('3', getline('.'))
2114  1
2115  set scrolloff=5
2116  exe "norm! \<c-d>"
2117  call assert_equal('10', getline('.'))
2118  exe "norm! \<c-u>"
2119  call assert_equal('5', getline('.'))
2120  1
2121  set scrolloff=99
2122  exe "norm! \<c-d>"
2123  call assert_equal('10', getline('.'))
2124  set scrolloff=0
2125  100
2126  exe "norm! $\<c-u>"
2127  call assert_equal('95', getline('.'))
2128  call assert_equal([0, 95, 1, 0, 1], getcurpos())
2129  100
2130  set nostartofline
2131  exe "norm! $\<c-u>"
2132  call assert_equal('95', getline('.'))
2133  call assert_equal([0, 95, 2, 0, 2147483647], getcurpos())
2134  " cleanup
2135  set startofline
2136  bw!
2137endfunc
2138
2139fun! Test_normal43_textobject1()
2140  " basic tests for text object aw
2141  new
2142  call append(0, ['foobar,eins,foobar', 'foo,zwei,foo    '])
2143  " diw
2144  norm! 1gg0diw
2145  call assert_equal([',eins,foobar', 'foo,zwei,foo    ', ''], getline(1,'$'))
2146  " daw
2147  norm! 2ggEdaw
2148  call assert_equal([',eins,foobar', 'foo,zwei,', ''], getline(1, '$'))
2149  %d
2150  call append(0, ["foo\teins\tfoobar", "foo\tzwei\tfoo   "])
2151  " diW
2152  norm! 2ggwd2iW
2153  call assert_equal(['foo	eins	foobar', 'foo	foo   ', ''], getline(1,'$'))
2154  " daW
2155  norm! 1ggd2aW
2156  call assert_equal(['foobar', 'foo	foo   ', ''], getline(1,'$'))
2157
2158  %d
2159  call append(0, ["foo\teins\tfoobar", "foo\tzwei\tfoo   "])
2160  " aw in visual line mode switches to characterwise mode
2161  norm! 2gg$Vawd
2162  call assert_equal(['foo	eins	foobar', 'foo	zwei	foo'], getline(1,'$'))
2163  norm! 1gg$Viwd
2164  call assert_equal(['foo	eins	', 'foo	zwei	foo'], getline(1,'$'))
2165
2166  " clean up
2167  bw!
2168endfunc
2169
2170func Test_normal44_textobjects2()
2171  " basic testing for is and as text objects
2172  new
2173  call append(0, ['This is a test. With some sentences!', '', 'Even with a question? And one more. And no sentence here'])
2174  " Test for dis - does not remove trailing whitespace
2175  norm! 1gg0dis
2176  call assert_equal([' With some sentences!', '', 'Even with a question? And one more. And no sentence here', ''], getline(1,'$'))
2177  " Test for das - removes leading whitespace
2178  norm! 3ggf?ldas
2179  call assert_equal([' With some sentences!', '', 'Even with a question? And no sentence here', ''], getline(1,'$'))
2180  " when used in visual mode, is made characterwise
2181  norm! 3gg$Visy
2182  call assert_equal('v', visualmode())
2183  " reset visualmode()
2184  norm! 3ggVy
2185  norm! 3gg$Vasy
2186  call assert_equal('v', visualmode())
2187  " basic testing for textobjects a< and at
2188  %d
2189  call setline(1, ['<div> ','<a href="foobar" class="foo">xyz</a>','    </div>', ' '])
2190  " a<
2191  norm! 1gg0da<
2192  call assert_equal([' ', '<a href="foobar" class="foo">xyz</a>', '    </div>', ' '], getline(1,'$'))
2193  norm! 1pj
2194  call assert_equal([' <div>', '<a href="foobar" class="foo">xyz</a>', '    </div>', ' '], getline(1,'$'))
2195  " at
2196  norm! d2at
2197  call assert_equal([' '], getline(1,'$'))
2198  %d
2199  call setline(1, ['<div> ','<a href="foobar" class="foo">xyz</a>','    </div>', ' '])
2200  " i<
2201  norm! 1gg0di<
2202  call assert_equal(['<> ', '<a href="foobar" class="foo">xyz</a>', '    </div>', ' '], getline(1,'$'))
2203  norm! 1Pj
2204  call assert_equal(['<div> ', '<a href="foobar" class="foo">xyz</a>', '    </div>', ' '], getline(1,'$'))
2205  norm! d2it
2206  call assert_equal(['<div></div>',' '], getline(1,'$'))
2207  " basic testing for a[ and i[ text object
2208  %d
2209  call setline(1, [' ', '[', 'one [two]', 'thre', ']'])
2210  norm! 3gg0di[
2211  call assert_equal([' ', '[', ']'], getline(1,'$'))
2212  call setline(1, [' ', '[', 'one [two]', 'thre', ']'])
2213  norm! 3gg0ftd2a[
2214  call assert_equal([' '], getline(1,'$'))
2215  %d
2216  " Test for i" when cursor is in front of a quoted object
2217  call append(0, 'foo "bar"')
2218  norm! 1gg0di"
2219  call assert_equal(['foo ""', ''], getline(1,'$'))
2220
2221  " clean up
2222  bw!
2223endfunc
2224
2225func Test_normal45_drop()
2226  if !has('dnd')
2227    " The ~ register does not exist
2228    call assert_beeps('norm! "~')
2229    return
2230  endif
2231
2232  " basic test for drag-n-drop
2233  " unfortunately, without a gui, we can't really test much here,
2234  " so simply test that ~p fails (which uses the drop register)
2235  new
2236  call assert_fails(':norm! "~p', 'E353')
2237  call assert_equal([],  getreg('~', 1, 1))
2238  " the ~ register is read only
2239  call assert_fails(':let @~="1"', 'E354')
2240  bw!
2241endfunc
2242
2243func Test_normal46_ignore()
2244  new
2245  " How to test this?
2246  " let's just for now test, that the buffer
2247  " does not change
2248  call feedkeys("\<c-s>", 't')
2249  call assert_equal([''], getline(1,'$'))
2250
2251  " no valid commands
2252  exe "norm! \<char-0x100>"
2253  call assert_equal([''], getline(1,'$'))
2254
2255  exe "norm! ä"
2256  call assert_equal([''], getline(1,'$'))
2257
2258  " clean up
2259  bw!
2260endfunc
2261
2262func Test_normal47_visual_buf_wipe()
2263  " This was causing a crash or ml_get error.
2264  enew!
2265  call setline(1,'xxx')
2266  normal $
2267  new
2268  call setline(1, range(1,2))
2269  2
2270  exe "norm \<C-V>$"
2271  bw!
2272  norm yp
2273  set nomodified
2274endfunc
2275
2276func Test_normal47_autocmd()
2277  " disabled, does not seem to be possible currently
2278  throw "Skipped: not possible to test cursorhold autocmd while waiting for input in normal_cmd"
2279  new
2280  call append(0, repeat('-',20))
2281  au CursorHold * call feedkeys('2l', '')
2282  1
2283  set updatetime=20
2284  " should delete 12 chars (d12l)
2285  call feedkeys('d1', '!')
2286  call assert_equal('--------', getline(1))
2287
2288  " clean up
2289  au! CursorHold
2290  set updatetime=4000
2291  bw!
2292endfunc
2293
2294func Test_normal48_wincmd()
2295  new
2296  exe "norm! \<c-w>c"
2297  call assert_equal(1, winnr('$'))
2298  call assert_fails(":norm! \<c-w>c", "E444")
2299endfunc
2300
2301func Test_normal49_counts()
2302  new
2303  call setline(1, 'one two three four five six seven eight nine ten')
2304  1
2305  norm! 3d2w
2306  call assert_equal('seven eight nine ten', getline(1))
2307  bw!
2308endfunc
2309
2310func Test_normal50_commandline()
2311  if !has("timers") || !has("cmdline_hist")
2312    return
2313  endif
2314  func! DoTimerWork(id)
2315    call assert_equal('[Command Line]', bufname(''))
2316    " should fail, with E11, but does fail with E23?
2317    "call feedkeys("\<c-^>", 'tm')
2318
2319    " should also fail with E11
2320    call assert_fails(":wincmd p", 'E11')
2321    " return from commandline window
2322    call feedkeys("\<cr>")
2323  endfunc
2324
2325  let oldlang=v:lang
2326  lang C
2327  set updatetime=20
2328  call timer_start(100, 'DoTimerWork')
2329  try
2330    " throws E23, for whatever reason...
2331    call feedkeys('q:', 'x!')
2332  catch /E23/
2333    " no-op
2334  endtry
2335  " clean up
2336  set updatetime=4000
2337  exe "lang" oldlang
2338  bw!
2339endfunc
2340
2341func Test_normal51_FileChangedRO()
2342  if !has("autocmd")
2343    return
2344  endif
2345  " Don't sleep after the warning message.
2346  call test_settime(1)
2347  call writefile(['foo'], 'Xreadonly.log')
2348  new Xreadonly.log
2349  setl ro
2350  au FileChangedRO <buffer> :call feedkeys("\<c-^>", 'tix')
2351  call assert_fails(":norm! Af", 'E788')
2352  call assert_equal(['foo'], getline(1,'$'))
2353  call assert_equal('Xreadonly.log', bufname(''))
2354
2355  " cleanup
2356  call test_settime(0)
2357  bw!
2358  call delete("Xreadonly.log")
2359endfunc
2360
2361func Test_normal52_rl()
2362  if !has("rightleft")
2363    return
2364  endif
2365  new
2366  call setline(1, 'abcde fghij klmnopq')
2367  norm! 1gg$
2368  set rl
2369  call assert_equal(19, col('.'))
2370  call feedkeys('l', 'tx')
2371  call assert_equal(18, col('.'))
2372  call feedkeys('h', 'tx')
2373  call assert_equal(19, col('.'))
2374  call feedkeys("\<right>", 'tx')
2375  call assert_equal(18, col('.'))
2376  call feedkeys("\<s-right>", 'tx')
2377  call assert_equal(13, col('.'))
2378  call feedkeys("\<c-right>", 'tx')
2379  call assert_equal(7, col('.'))
2380  call feedkeys("\<c-left>", 'tx')
2381  call assert_equal(13, col('.'))
2382  call feedkeys("\<s-left>", 'tx')
2383  call assert_equal(19, col('.'))
2384  call feedkeys("<<", 'tx')
2385  call assert_equal('	abcde fghij klmnopq',getline(1))
2386  call feedkeys(">>", 'tx')
2387  call assert_equal('abcde fghij klmnopq',getline(1))
2388
2389  " cleanup
2390  set norl
2391  bw!
2392endfunc
2393
2394func Test_normal53_digraph()
2395  if !has('digraphs')
2396    return
2397  endif
2398  new
2399  call setline(1, 'abcdefgh|')
2400  exe "norm! 1gg0f\<c-k>!!"
2401  call assert_equal(9, col('.'))
2402  set cpo+=D
2403  exe "norm! 1gg0f\<c-k>!!"
2404  call assert_equal(1, col('.'))
2405
2406  set cpo-=D
2407  bw!
2408endfunc
2409
2410func Test_normal54_Ctrl_bsl()
2411  new
2412  call setline(1, 'abcdefghijklmn')
2413  exe "norm! df\<c-\>\<c-n>"
2414  call assert_equal(['abcdefghijklmn'], getline(1,'$'))
2415  exe "norm! df\<c-\>\<c-g>"
2416  call assert_equal(['abcdefghijklmn'], getline(1,'$'))
2417  exe "norm! df\<c-\>m"
2418  call assert_equal(['abcdefghijklmn'], getline(1,'$'))
2419
2420  call setline(2, 'abcdefghijklmnāf')
2421  norm! 2gg0
2422  exe "norm! df\<Char-0x101>"
2423  call assert_equal(['abcdefghijklmn', 'f'], getline(1,'$'))
2424  norm! 1gg0
2425  exe "norm! df\<esc>"
2426  call assert_equal(['abcdefghijklmn', 'f'], getline(1,'$'))
2427
2428  " clean up
2429  bw!
2430endfunc
2431
2432func Test_normal_large_count()
2433  " This may fail with 32bit long, how do we detect that?
2434  new
2435  normal o
2436  normal 6666666666dL
2437  bwipe!
2438endfunc
2439
2440func Test_delete_until_paragraph()
2441  new
2442  normal grádv}
2443  call assert_equal('á', getline(1))
2444  normal grád}
2445  call assert_equal('', getline(1))
2446  bwipe!
2447endfunc
2448
2449" Test for the gr (virtual replace) command
2450" Test for the bug fixed by 7.4.387
2451func Test_gr_command()
2452  enew!
2453  let save_cpo = &cpo
2454  call append(0, ['First line', 'Second line', 'Third line'])
2455  exe "normal i\<C-G>u"
2456  call cursor(2, 1)
2457  set cpo-=X
2458  normal 4gro
2459  call assert_equal('oooond line', getline(2))
2460  undo
2461  set cpo+=X
2462  normal 4gro
2463  call assert_equal('ooooecond line', getline(2))
2464  let &cpo = save_cpo
2465  enew!
2466endfunc
2467
2468" When splitting a window the changelist position is wrong.
2469" Test the changelist position after splitting a window.
2470" Test for the bug fixed by 7.4.386
2471func Test_changelist()
2472  let save_ul = &ul
2473  enew!
2474  call append('$', ['1', '2'])
2475  exe "normal i\<C-G>u"
2476  exe "normal Gkylpa\<C-G>u"
2477  set ul=100
2478  exe "normal Gylpa\<C-G>u"
2479  set ul=100
2480  normal gg
2481  vsplit
2482  normal g;
2483  call assert_equal([3, 2], [line('.'), col('.')])
2484  normal g;
2485  call assert_equal([2, 2], [line('.'), col('.')])
2486  call assert_fails('normal g;', 'E662:')
2487  %bwipe!
2488  let &ul = save_ul
2489endfunc
2490
2491func Test_nv_hat_count()
2492  %bwipeout!
2493  let l:nr = bufnr('%') + 1
2494  call assert_fails(':execute "normal! ' . l:nr . '\<C-^>"', 'E92')
2495
2496  edit Xfoo
2497  let l:foo_nr = bufnr('Xfoo')
2498
2499  edit Xbar
2500  let l:bar_nr = bufnr('Xbar')
2501
2502  " Make sure we are not just using the alternate file.
2503  edit Xbaz
2504
2505  call feedkeys(l:foo_nr . "\<C-^>", 'tx')
2506  call assert_equal('Xfoo', fnamemodify(bufname('%'), ':t'))
2507
2508  call feedkeys(l:bar_nr . "\<C-^>", 'tx')
2509  call assert_equal('Xbar', fnamemodify(bufname('%'), ':t'))
2510
2511  %bwipeout!
2512endfunc
2513
2514func Test_message_when_using_ctrl_c()
2515  " Make sure no buffers are changed.
2516  %bwipe!
2517
2518  exe "normal \<C-C>"
2519  call assert_match("Type  :qa  and press <Enter> to exit Vim", Screenline(&lines))
2520
2521  new
2522  cal setline(1, 'hi!')
2523  exe "normal \<C-C>"
2524  call assert_match("Type  :qa!  and press <Enter> to abandon all changes and exit Vim", Screenline(&lines))
2525
2526  bwipe!
2527endfunc
2528
2529" Test for '[m', ']m', '[M' and ']M'
2530" Jumping to beginning and end of methods in Java-like languages
2531func Test_java_motion()
2532  new
2533  a
2534Piece of Java
2535{
2536	tt m1 {
2537		t1;
2538	} e1
2539
2540	tt m2 {
2541		t2;
2542	} e2
2543
2544	tt m3 {
2545		if (x)
2546		{
2547			t3;
2548		}
2549	} e3
2550}
2551.
2552
2553  normal gg
2554
2555  normal 2]maA
2556  call assert_equal("\ttt m1 {A", getline('.'))
2557  call assert_equal([3, 9, 16], [line('.'), col('.'), virtcol('.')])
2558
2559  normal j]maB
2560  call assert_equal("\ttt m2 {B", getline('.'))
2561  call assert_equal([7, 9, 16], [line('.'), col('.'), virtcol('.')])
2562
2563  normal ]maC
2564  call assert_equal("\ttt m3 {C", getline('.'))
2565  call assert_equal([11, 9, 16], [line('.'), col('.'), virtcol('.')])
2566
2567  normal [maD
2568  call assert_equal("\ttt m3 {DC", getline('.'))
2569  call assert_equal([11, 9, 16], [line('.'), col('.'), virtcol('.')])
2570
2571  normal k2[maE
2572  call assert_equal("\ttt m1 {EA", getline('.'))
2573  call assert_equal([3, 9, 16], [line('.'), col('.'), virtcol('.')])
2574
2575  normal 3[maF
2576  call assert_equal("{F", getline('.'))
2577  call assert_equal([2, 2, 2], [line('.'), col('.'), virtcol('.')])
2578
2579  normal ]MaG
2580  call assert_equal("\t}G e1", getline('.'))
2581  call assert_equal([5, 3, 10], [line('.'), col('.'), virtcol('.')])
2582
2583  normal j2]MaH
2584  call assert_equal("\t}H e3", getline('.'))
2585  call assert_equal([16, 3, 10], [line('.'), col('.'), virtcol('.')])
2586
2587  normal ]M]M
2588  normal aI
2589  call assert_equal("}I", getline('.'))
2590  call assert_equal([17, 2, 2], [line('.'), col('.'), virtcol('.')])
2591
2592  normal 2[MaJ
2593  call assert_equal("\t}JH e3", getline('.'))
2594  call assert_equal([16, 3, 10], [line('.'), col('.'), virtcol('.')])
2595
2596  normal k[MaK
2597  call assert_equal("\t}K e2", getline('.'))
2598  call assert_equal([9, 3, 10], [line('.'), col('.'), virtcol('.')])
2599
2600  normal 3[MaL
2601  call assert_equal("{LF", getline('.'))
2602  call assert_equal([2, 2, 2], [line('.'), col('.'), virtcol('.')])
2603
2604  close!
2605endfunc
2606
2607fun! Test_normal_gdollar_cmd()
2608  if !has("jumplist")
2609    return
2610  endif
2611  " Tests for g cmds
2612  call Setup_NewWindow()
2613  " Make long lines that will wrap
2614  %s/$/\=repeat(' foobar', 10)/
2615  20vsp
2616  set wrap
2617  " Test for g$ with count
2618  norm! gg
2619  norm! 0vg$y
2620  call assert_equal(20, col("'>"))
2621  call assert_equal('1 foobar foobar foob', getreg(0))
2622  norm! gg
2623  norm! 0v4g$y
2624  call assert_equal(72, col("'>"))
2625  call assert_equal('1 foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar'.."\n", getreg(0))
2626  norm! gg
2627  norm! 0v6g$y
2628  call assert_equal(40, col("'>"))
2629  call assert_equal('1 foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar'.. "\n"..
2630		  \ '2 foobar foobar foobar foobar foobar foo', getreg(0))
2631  set nowrap
2632  " clean up
2633  norm! gg
2634  norm! 0vg$y
2635  call assert_equal(20, col("'>"))
2636  call assert_equal('1 foobar foobar foob', getreg(0))
2637  norm! gg
2638  norm! 0v4g$y
2639  call assert_equal(20, col("'>"))
2640  call assert_equal('1 foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar'.. "\n"..
2641                 \  '2 foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar'.. "\n"..
2642                 \  '3 foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar'.. "\n"..
2643                 \  '4 foobar foobar foob', getreg(0))
2644  norm! gg
2645  norm! 0v6g$y
2646  call assert_equal(20, col("'>"))
2647  call assert_equal('1 foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar'.. "\n"..
2648                 \  '2 foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar'.. "\n"..
2649                 \  '3 foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar'.. "\n"..
2650                 \  '4 foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar'.. "\n"..
2651                 \  '5 foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar'.. "\n"..
2652                 \  '6 foobar foobar foob', getreg(0))
2653  " Move to last line, also down movement is not possible, should still move
2654  " the cursor to the last visible char
2655  norm! G
2656  norm! 0v6g$y
2657  call assert_equal(20, col("'>"))
2658  call assert_equal('100 foobar foobar fo', getreg(0))
2659  bw!
2660endfunc
2661
2662func Test_normal_gk()
2663  " needs 80 column new window
2664  new
2665  vert 80new
2666  put =[repeat('x',90)..' {{{1', 'x {{{1']
2667  norm! gk
2668  " In a 80 column wide terminal the window will be only 78 char
2669  " (because Vim will leave space for the other window),
2670  " but if the terminal is larger, it will be 80 chars, so verify the
2671  " cursor column correctly.
2672  call assert_equal(winwidth(0)+1, col('.'))
2673  call assert_equal(winwidth(0)+1, virtcol('.'))
2674  norm! j
2675  call assert_equal(6, col('.'))
2676  call assert_equal(6, virtcol('.'))
2677  norm! gk
2678  call assert_equal(95, col('.'))
2679  call assert_equal(95, virtcol('.'))
2680  bw!
2681  bw!
2682
2683  " needs 80 column new window
2684  new
2685  vert 80new
2686  set number
2687  set numberwidth=10
2688  set cpoptions+=n
2689  put =[repeat('0',90), repeat('1',90)]
2690  norm! 075l
2691  call assert_equal(76, col('.'))
2692  norm! gk
2693  call assert_equal(1, col('.'))
2694  norm! gk
2695  call assert_equal(76, col('.'))
2696  norm! gk
2697  call assert_equal(1, col('.'))
2698  norm! gj
2699  call assert_equal(76, col('.'))
2700  norm! gj
2701  call assert_equal(1, col('.'))
2702  norm! gj
2703  call assert_equal(76, col('.'))
2704  bw!
2705  bw!
2706  set cpoptions& number& numberwidth&
2707endfunc
2708
2709" Test for cursor movement with '-' in 'cpoptions'
2710func Test_normal_cpo_minus()
2711  new
2712  call setline(1, ['foo', 'bar', 'baz'])
2713  let save_cpo = &cpo
2714  set cpo+=-
2715  call assert_beeps('normal 10j')
2716  call assert_equal(1, line('.'))
2717  normal G
2718  call assert_beeps('normal 10k')
2719  call assert_equal(3, line('.'))
2720  call assert_fails(10, 'E16:')
2721  let &cpo = save_cpo
2722  close!
2723endfunc
2724
2725" Test for using : to run a multi-line Ex command in operator pending mode
2726func Test_normal_yank_with_excmd()
2727  new
2728  call setline(1, ['foo', 'bar', 'baz'])
2729  let @a = ''
2730  call feedkeys("\"ay:if v:true\<CR>normal l\<CR>endif\<CR>", 'xt')
2731  call assert_equal('f', @a)
2732  close!
2733endfunc
2734