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