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