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