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