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