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