xref: /vim-8.2.3635/src/testdir/test_edit.vim (revision cd5c8f82)
1" Test for edit functions
2"
3if exists("+t_kD")
4  let &t_kD="[3;*~"
5endif
6set belloff=
7
8" Needed for testing basic rightleft: Test_edit_rightleft
9source view_util.vim
10
11" Needs to come first until the bug in getchar() is
12" fixed: https://groups.google.com/d/msg/vim_dev/fXL9yme4H4c/bOR-U6_bAQAJ
13func! Test_edit_00b()
14  new
15  call setline(1, ['abc '])
16  inoreabbr <buffer> h here some more
17  call cursor(1, 4)
18  " <c-l> expands the abbreviation and ends insertmode
19  call feedkeys(":set im\<cr> h\<c-l>:set noim\<cr>", 'tix')
20  call assert_equal(['abc here some more '], getline(1,'$'))
21  iunabbr <buffer> h
22  bw!
23endfunc
24
25func! Test_edit_01()
26  " set for Travis CI?
27  "  set nocp noesckeys
28  new
29  set belloff=backspace
30  " 1) empty buffer
31  call assert_equal([''], getline(1,'$'))
32  " 2) delete in an empty line
33  call feedkeys("i\<del>\<esc>", 'tnix')
34  call assert_equal([''], getline(1,'$'))
35  %d
36  " 3) delete one character
37  call setline(1, 'a')
38  call feedkeys("i\<del>\<esc>", 'tnix')
39  call assert_equal([''], getline(1,'$'))
40  %d
41  " 4) delete a multibyte character
42  if has("multi_byte")
43    call setline(1, "\u0401")
44    call feedkeys("i\<del>\<esc>", 'tnix')
45    call assert_equal([''], getline(1,'$'))
46    %d
47  endif
48  " 5.1) delete linebreak with 'bs' option containing eol
49  let _bs=&bs
50  set bs=eol
51  call setline(1, ["abc def", "ghi jkl"])
52  call cursor(1, 1)
53  call feedkeys("A\<del>\<esc>", 'tnix')
54  call assert_equal(['abc defghi jkl'], getline(1, 2))
55  %d
56  " 5.2) delete linebreak with backspace option w/out eol
57  set bs=
58  call setline(1, ["abc def", "ghi jkl"])
59  call cursor(1, 1)
60  call feedkeys("A\<del>\<esc>", 'tnix')
61  call assert_equal(["abc def", "ghi jkl"], getline(1, 2))
62  set belloff=
63  let &bs=_bs
64  bw!
65endfunc
66
67func! Test_edit_02()
68  " Change cursor position in InsertCharPre command
69  new
70  call setline(1, 'abc')
71  call cursor(1, 1)
72  fu! DoIt(...)
73    call cursor(1, 4)
74    if len(a:000)
75      let v:char=a:1
76    endif
77  endfu
78  au InsertCharPre <buffer> :call DoIt('y')
79  call feedkeys("ix\<esc>", 'tnix')
80  call assert_equal(['abcy'], getline(1, '$'))
81  " Setting <Enter> in InsertCharPre
82  au! InsertCharPre <buffer> :call DoIt("\n")
83  call setline(1, 'abc')
84  call cursor(1, 1)
85  call feedkeys("ix\<esc>", 'tnix')
86  call assert_equal(['abc', ''], getline(1, '$'))
87  %d
88  au! InsertCharPre
89  " Change cursor position in InsertEnter command
90  " 1) when setting v:char, keeps changed cursor position
91  au! InsertEnter <buffer> :call DoIt('y')
92  call setline(1, 'abc')
93  call cursor(1, 1)
94  call feedkeys("ix\<esc>", 'tnix')
95  call assert_equal(['abxc'], getline(1, '$'))
96  " 2) when not setting v:char, restores changed cursor position
97  au! InsertEnter <buffer> :call DoIt()
98  call setline(1, 'abc')
99  call cursor(1, 1)
100  call feedkeys("ix\<esc>", 'tnix')
101  call assert_equal(['xabc'], getline(1, '$'))
102  au! InsertEnter
103  delfu DoIt
104  bw!
105endfunc
106
107func! Test_edit_03()
108  " Change cursor after <c-o> command to end of line
109  new
110  call setline(1, 'abc')
111  call cursor(1, 1)
112  call feedkeys("i\<c-o>$y\<esc>", 'tnix')
113  call assert_equal(['abcy'], getline(1, '$'))
114  %d
115  call setline(1, 'abc')
116  call cursor(1, 1)
117  call feedkeys("i\<c-o>80|y\<esc>", 'tnix')
118  call assert_equal(['abcy'], getline(1, '$'))
119  %d
120  call setline(1, 'abc')
121  call feedkeys("Ad\<c-o>:s/$/efg/\<cr>hij", 'tnix')
122  call assert_equal(['hijabcdefg'], getline(1, '$'))
123  bw!
124endfunc
125
126func! Test_edit_04()
127  " test for :stopinsert
128  new
129  call setline(1, 'abc')
130  call cursor(1, 1)
131  call feedkeys("i\<c-o>:stopinsert\<cr>$", 'tnix')
132  call feedkeys("aX\<esc>", 'tnix')
133  call assert_equal(['abcX'], getline(1, '$'))
134  %d
135  bw!
136endfunc
137
138func! Test_edit_05()
139  " test for folds being opened
140  new
141  call setline(1, ['abcX', 'abcX', 'zzzZ'])
142  call cursor(1, 1)
143  set foldmethod=manual foldopen+=insert
144  " create fold for those two lines
145  norm! Vjzf
146  call feedkeys("$ay\<esc>", 'tnix')
147  call assert_equal(['abcXy', 'abcX', 'zzzZ'], getline(1, '$'))
148  %d
149  call setline(1, ['abcX', 'abcX', 'zzzZ'])
150  call cursor(1, 1)
151  set foldmethod=manual foldopen-=insert
152  " create fold for those two lines
153  norm! Vjzf
154  call feedkeys("$ay\<esc>", 'tnix')
155  call assert_equal(['abcXy', 'abcX', 'zzzZ'], getline(1, '$'))
156  %d
157  bw!
158endfunc
159
160func! Test_edit_06()
161  " Test in diff mode
162  if !has("diff") || !executable("diff")
163    return
164  endif
165  new
166  call setline(1, ['abc', 'xxx', 'yyy'])
167  vnew
168  call setline(1, ['abc', 'zzz', 'xxx', 'yyy'])
169  wincmd p
170  diffthis
171  wincmd p
172  diffthis
173  wincmd p
174  call cursor(2, 1)
175  norm! zt
176  call feedkeys("Ozzz\<esc>", 'tnix')
177  call assert_equal(['abc', 'zzz', 'xxx', 'yyy'], getline(1,'$'))
178  bw!
179  bw!
180endfunc
181
182func! Test_edit_07()
183  " 1) Test with completion <c-l> when popupmenu is visible
184  new
185  call setline(1, 'J')
186
187  func! ListMonths()
188    call complete(col('.')-1, ['January', 'February', 'March',
189    \ 'April', 'May', 'June', 'July', 'August', 'September',
190    \ 'October', 'November', 'December'])
191    return ''
192  endfunc
193  inoremap <buffer> <F5> <C-R>=ListMonths()<CR>
194
195  call feedkeys("A\<f5>\<c-p>". repeat("\<down>", 6)."\<c-l>\<down>\<c-l>\<cr>", 'tx')
196  call assert_equal(['July'], getline(1,'$'))
197  " 1) Test completion when InsertCharPre kicks in
198  %d
199  call setline(1, 'J')
200  fu! DoIt()
201    if v:char=='u'
202      let v:char='an'
203    endif
204  endfu
205  au InsertCharPre <buffer> :call DoIt()
206  call feedkeys("A\<f5>\<c-p>u\<cr>\<c-l>\<cr>", 'tx')
207  call assert_equal(["Jan\<c-l>",''], getline(1,'$'))
208  %d
209  call setline(1, 'J')
210  call feedkeys("A\<f5>\<c-p>u\<down>\<c-l>\<cr>", 'tx')
211  call assert_equal(["January"], getline(1,'$'))
212
213  delfu ListMonths
214  delfu DoIt
215  iunmap <buffer> <f5>
216  bw!
217endfunc
218
219func! Test_edit_08()
220  " reset insertmode from i_ctrl-r_=
221  new
222  call setline(1, ['abc'])
223  call cursor(1, 4)
224  call feedkeys(":set im\<cr>ZZZ\<c-r>=setbufvar(1,'&im', 0)\<cr>",'tnix')
225  call assert_equal(['abZZZc'], getline(1,'$'))
226  call assert_equal([0, 1, 1, 0], getpos('.'))
227  call assert_false(0, '&im')
228  bw!
229endfunc
230
231func! Test_edit_09()
232  " test i_CTRL-\ combinations
233  new
234  call setline(1, ['abc', 'def', 'ghi'])
235  call cursor(1, 1)
236  " 1) CTRL-\ CTLR-N
237  call feedkeys(":set im\<cr>\<c-\>\<c-n>ccABC\<c-l>", 'txin')
238  call assert_equal(['ABC', 'def', 'ghi'], getline(1,'$'))
239  call setline(1, ['ABC', 'def', 'ghi'])
240  " 2) CTRL-\ CTLR-G
241  call feedkeys("j0\<c-\>\<c-g>ZZZ\<cr>\<c-l>", 'txin')
242  call assert_equal(['ABC', 'ZZZ', 'def', 'ghi'], getline(1,'$'))
243  call feedkeys("I\<c-\>\<c-g>YYY\<c-l>", 'txin')
244  call assert_equal(['ABC', 'ZZZ', 'YYYdef', 'ghi'], getline(1,'$'))
245  set noinsertmode
246  " 3) CTRL-\ CTRL-O
247  call setline(1, ['ABC', 'ZZZ', 'def', 'ghi'])
248  call cursor(1, 1)
249  call feedkeys("A\<c-o>ix", 'txin')
250  call assert_equal(['ABxC', 'ZZZ', 'def', 'ghi'], getline(1,'$'))
251  call feedkeys("A\<c-\>\<c-o>ix", 'txin')
252  call assert_equal(['ABxCx', 'ZZZ', 'def', 'ghi'], getline(1,'$'))
253  " 4) CTRL-\ a (should be inserted literally, not special after <c-\>
254  call setline(1, ['ABC', 'ZZZ', 'def', 'ghi'])
255  call cursor(1, 1)
256  call feedkeys("A\<c-\>a", 'txin')
257  call assert_equal(["ABC\<c-\>a", 'ZZZ', 'def', 'ghi'], getline(1, '$'))
258  bw!
259endfunc
260
261func! Test_edit_10()
262  " Test for starting selectmode
263  new
264  set selectmode=key keymodel=startsel
265  call setline(1, ['abc', 'def', 'ghi'])
266  call cursor(1, 4)
267  call feedkeys("A\<s-home>start\<esc>", 'txin')
268  call assert_equal(['startdef', 'ghi'], getline(1, '$'))
269  set selectmode= keymodel=
270  bw!
271endfunc
272
273func! Test_edit_11()
274  " Test that indenting kicks in
275  new
276  set cindent
277  call setline(1, ['{', '', ''])
278  call cursor(2, 1)
279  call feedkeys("i\<c-f>int c;\<esc>", 'tnix')
280  call cursor(3, 1)
281  call feedkeys("i/* comment */", 'tnix')
282  call assert_equal(['{', "\<tab>int c;", "/* comment */"], getline(1, '$'))
283  " added changed cindentkeys slightly
284  set cindent cinkeys+=*/
285  call setline(1, ['{', '', ''])
286  call cursor(2, 1)
287  call feedkeys("i\<c-f>int c;\<esc>", 'tnix')
288  call cursor(3, 1)
289  call feedkeys("i/* comment */", 'tnix')
290  call assert_equal(['{', "\<tab>int c;", "\<tab>/* comment */"], getline(1, '$'))
291  set cindent cinkeys+==end
292  call feedkeys("oend\<cr>\<esc>", 'tnix')
293  call assert_equal(['{', "\<tab>int c;", "\<tab>/* comment */", "\tend", ''], getline(1, '$'))
294  set cinkeys-==end
295  %d
296  " Use indentexpr instead of cindenting
297  func! Do_Indent()
298    if v:lnum == 3
299      return 3*shiftwidth()
300    else
301      return 2*shiftwidth()
302    endif
303  endfunc
304  setl indentexpr=Do_Indent() indentkeys+=*/
305  call setline(1, ['{', '', ''])
306  call cursor(2, 1)
307  call feedkeys("i\<c-f>int c;\<esc>", 'tnix')
308  call cursor(3, 1)
309  call feedkeys("i/* comment */", 'tnix')
310  call assert_equal(['{', "\<tab>\<tab>int c;", "\<tab>\<tab>\<tab>/* comment */"], getline(1, '$'))
311  set cinkeys&vim indentkeys&vim
312  set nocindent indentexpr=
313  delfu Do_Indent
314  bw!
315endfunc
316
317func! Test_edit_12()
318  " Test changing indent in replace mode
319  new
320  call setline(1, ["\tabc", "\tdef"])
321  call cursor(2, 4)
322  call feedkeys("R^\<c-d>", 'tnix')
323  call assert_equal(["\tabc", "def"], getline(1, '$'))
324  call assert_equal([0, 2, 2, 0], getpos('.'))
325  %d
326  call setline(1, ["\tabc", "\t\tdef"])
327  call cursor(2, 2)
328  call feedkeys("R^\<c-d>", 'tnix')
329  call assert_equal(["\tabc", "def"], getline(1, '$'))
330  call assert_equal([0, 2, 1, 0], getpos('.'))
331  %d
332  call setline(1, ["\tabc", "\t\tdef"])
333  call cursor(2, 2)
334  call feedkeys("R\<c-t>", 'tnix')
335  call assert_equal(["\tabc", "\t\t\tdef"], getline(1, '$'))
336  call assert_equal([0, 2, 2, 0], getpos('.'))
337  bw!
338  10vnew
339  call setline(1, ["\tabc", "\t\tdef"])
340  call cursor(2, 2)
341  call feedkeys("R\<c-t>", 'tnix')
342  call assert_equal(["\tabc", "\t\t\tdef"], getline(1, '$'))
343  call assert_equal([0, 2, 2, 0], getpos('.'))
344  %d
345  set sw=4
346  call setline(1, ["\tabc", "\t\tdef"])
347  call cursor(2, 2)
348  call feedkeys("R\<c-t>\<c-t>", 'tnix')
349  call assert_equal(["\tabc", "\t\t\tdef"], getline(1, '$'))
350  call assert_equal([0, 2, 2, 0], getpos('.'))
351  %d
352  call setline(1, ["\tabc", "\t\tdef"])
353  call cursor(2, 2)
354  call feedkeys("R\<c-t>\<c-t>", 'tnix')
355  call assert_equal(["\tabc", "\t\t\tdef"], getline(1, '$'))
356  call assert_equal([0, 2, 2, 0], getpos('.'))
357  set et
358  set sw& et&
359  %d
360  call setline(1, ["\t/*"])
361  set formatoptions=croql
362  call cursor(1, 3)
363  call feedkeys("A\<cr>\<cr>/", 'tnix')
364  call assert_equal(["\t/*", " *", " */"], getline(1, '$'))
365  set formatoptions&
366  bw!
367endfunc
368
369func! Test_edit_13()
370  " Test smartindenting
371  if exists("+smartindent")
372    new
373    set smartindent autoindent
374    call setline(1, ["\tabc"])
375    call feedkeys("A {\<cr>more\<cr>}\<esc>", 'tnix')
376    call assert_equal(["\tabc {", "\t\tmore", "\t}"], getline(1, '$'))
377    set smartindent& autoindent&
378    bw!
379  endif
380endfunc
381
382func! Test_edit_CR()
383  " Test for <CR> in insert mode
384  " basically only in quickfix mode ist tested, the rest
385  " has been taken care of by other tests
386  if !has("quickfix")
387    return
388  endif
389  botright new
390  call writefile(range(1, 10), 'Xqflist.txt')
391  call setqflist([{'filename': 'Xqflist.txt', 'lnum': 2}])
392  copen
393  set modifiable
394  call feedkeys("A\<cr>", 'tnix')
395  call assert_equal('Xqflist.txt', bufname(''))
396  call assert_equal(2, line('.'))
397  cclose
398  botright new
399  call setloclist(0, [{'filename': 'Xqflist.txt', 'lnum': 10}])
400  lopen
401  set modifiable
402  call feedkeys("A\<cr>", 'tnix')
403  call assert_equal('Xqflist.txt', bufname(''))
404  call assert_equal(10, line('.'))
405  call feedkeys("A\<Enter>", 'tnix')
406  call feedkeys("A\<kEnter>", 'tnix')
407  call feedkeys("A\n", 'tnix')
408  call feedkeys("A\r", 'tnix')
409  call assert_equal(map(range(1, 10), 'string(v:val)') + ['', '', '', ''], getline(1, '$'))
410  bw!
411  lclose
412  call delete('Xqflist.txt')
413endfunc
414
415func! Test_edit_CTRL_()
416  " disabled for Windows builds, why?
417  if !has("multi_byte") || !has("rightleft") || has("win32")
418    return
419  endif
420  let _encoding=&encoding
421  set encoding=utf-8
422  " Test for CTRL-_
423  new
424  call setline(1, ['abc'])
425  call cursor(1, 1)
426  call feedkeys("i\<c-_>xyz\<esc>", 'tnix')
427  call assert_equal(["\<C-_>xyzabc"], getline(1, '$'))
428  call assert_false(&revins)
429  set ari
430  call setline(1, ['abc'])
431  call cursor(1, 1)
432  call feedkeys("i\<c-_>xyz\<esc>", 'tnix')
433  call assert_equal(["æèñabc"], getline(1, '$'))
434  call assert_true(&revins)
435  call setline(1, ['abc'])
436  call cursor(1, 1)
437  call feedkeys("i\<c-_>xyz\<esc>", 'tnix')
438  call assert_equal(["xyzabc"], getline(1, '$'))
439  call assert_false(&revins)
440  set noari
441  let &encoding=_encoding
442  bw!
443endfunc
444
445" needs to come first, to have the @. register empty
446func! Test_edit_00a_CTRL_A()
447  " Test pressing CTRL-A
448  new
449  call setline(1, repeat([''], 5))
450  call cursor(1, 1)
451  set belloff=all
452  try
453    call feedkeys("A\<NUL>", 'tnix')
454  catch /^Vim\%((\a\+)\)\=:E29/
455    call assert_true(1, 'E29 error caught')
456  endtry
457  set belloff=
458  call cursor(1, 1)
459  call feedkeys("Afoobar \<esc>", 'tnix')
460  call cursor(2, 1)
461  call feedkeys("A\<c-a>more\<esc>", 'tnix')
462  call cursor(3, 1)
463  call feedkeys("A\<NUL>and more\<esc>", 'tnix')
464  call assert_equal(['foobar ', 'foobar more', 'foobar morend more', '', ''], getline(1, '$'))
465  bw!
466endfunc
467
468func! Test_edit_CTRL_EY()
469  " Ctrl-E/ Ctrl-Y in insert mode completion to scroll
470  10new
471  call setline(1, range(1, 100))
472  call cursor(30, 1)
473  norm! z.
474  call feedkeys("A\<c-x>\<c-e>\<c-e>\<c-e>\<c-e>\<c-e>", 'tnix')
475  call assert_equal(30, winsaveview()['topline'])
476  call assert_equal([0, 30, 2, 0], getpos('.'))
477  call feedkeys("A\<c-x>\<c-e>\<c-e>\<c-e>\<c-e>\<c-e>", 'tnix')
478  call feedkeys("A\<c-x>".repeat("\<c-y>", 10), 'tnix')
479  call assert_equal(21, winsaveview()['topline'])
480  call assert_equal([0, 30, 2, 0], getpos('.'))
481  bw!
482endfunc
483
484func! Test_edit_CTRL_G()
485  new
486  set belloff=all
487  call setline(1, ['foobar', 'foobar', 'foobar'])
488  call cursor(2, 4)
489  call feedkeys("ioooooooo\<c-g>k\<c-r>.\<esc>", 'tnix')
490  call assert_equal(['foooooooooobar', 'foooooooooobar', 'foobar'], getline(1, '$'))
491  call assert_equal([0, 1, 11, 0], getpos('.'))
492  call feedkeys("i\<c-g>k\<esc>", 'tnix')
493  call assert_equal([0, 1, 10, 0], getpos('.'))
494  call cursor(2, 4)
495  call feedkeys("i\<c-g>jzzzz\<esc>", 'tnix')
496  call assert_equal(['foooooooooobar', 'foooooooooobar', 'foozzzzbar'], getline(1, '$'))
497  call assert_equal([0, 3, 7, 0], getpos('.'))
498  call feedkeys("i\<c-g>j\<esc>", 'tnix')
499  call assert_equal([0, 3, 6, 0], getpos('.'))
500  set belloff=
501  bw!
502endfunc
503
504func! Test_edit_CTRL_I()
505  " Tab in completion mode
506  let path=expand("%:p:h")
507  new
508  call setline(1, [path."/", ''])
509  call feedkeys("Arunt\<c-x>\<c-f>\<tab>\<cr>\<esc>", 'tnix')
510  call assert_match('runtest\.vim', getline(1))
511  %d
512  call writefile(['one', 'two', 'three'], 'Xinclude.txt')
513  let include='#include Xinclude.txt'
514  call setline(1, [include, ''])
515  call cursor(2, 1)
516  call feedkeys("A\<c-x>\<tab>\<cr>\<esc>", 'tnix')
517  call assert_equal([include, 'one', ''], getline(1, '$'))
518  call feedkeys("2ggC\<c-x>\<tab>\<down>\<cr>\<esc>", 'tnix')
519  call assert_equal([include, 'two', ''], getline(1, '$'))
520  call feedkeys("2ggC\<c-x>\<tab>\<down>\<down>\<cr>\<esc>", 'tnix')
521  call assert_equal([include, 'three', ''], getline(1, '$'))
522  call feedkeys("2ggC\<c-x>\<tab>\<down>\<down>\<down>\<cr>\<esc>", 'tnix')
523  call assert_equal([include, '', ''], getline(1, '$'))
524  call delete("Xinclude.txt")
525  bw!
526endfunc
527
528func! Test_edit_CTRL_K()
529  " Test pressing CTRL-K (basically only dictionary completion and digraphs
530  " the rest is already covered
531  call writefile(['A', 'AA', 'AAA', 'AAAA'], 'Xdictionary.txt')
532  set dictionary=Xdictionary.txt
533  new
534  call setline(1, 'A')
535  call cursor(1, 1)
536  call feedkeys("A\<c-x>\<c-k>\<cr>\<esc>", 'tnix')
537  call assert_equal(['AA', ''], getline(1, '$'))
538  %d
539  call setline(1, 'A')
540  call cursor(1, 1)
541  call feedkeys("A\<c-x>\<c-k>\<down>\<cr>\<esc>", 'tnix')
542  call assert_equal(['AAA'], getline(1, '$'))
543  %d
544  call setline(1, 'A')
545  call cursor(1, 1)
546  call feedkeys("A\<c-x>\<c-k>\<down>\<down>\<cr>\<esc>", 'tnix')
547  call assert_equal(['AAAA'], getline(1, '$'))
548  %d
549  call setline(1, 'A')
550  call cursor(1, 1)
551  call feedkeys("A\<c-x>\<c-k>\<down>\<down>\<down>\<cr>\<esc>", 'tnix')
552  call assert_equal(['A'], getline(1, '$'))
553  %d
554  call setline(1, 'A')
555  call cursor(1, 1)
556  call feedkeys("A\<c-x>\<c-k>\<down>\<down>\<down>\<down>\<cr>\<esc>", 'tnix')
557  call assert_equal(['AA'], getline(1, '$'))
558
559  " press an unexecpted key after dictionary completion
560  %d
561  call setline(1, 'A')
562  call cursor(1, 1)
563  call feedkeys("A\<c-x>\<c-k>\<c-]>\<cr>\<esc>", 'tnix')
564  call assert_equal(['AA', ''], getline(1, '$'))
565  %d
566  call setline(1, 'A')
567  call cursor(1, 1)
568  call feedkeys("A\<c-x>\<c-k>\<c-s>\<cr>\<esc>", 'tnix')
569  call assert_equal(["AA\<c-s>", ''], getline(1, '$'))
570  %d
571  call setline(1, 'A')
572  call cursor(1, 1)
573  call feedkeys("A\<c-x>\<c-k>\<c-f>\<cr>\<esc>", 'tnix')
574  call assert_equal(["AA\<c-f>", ''], getline(1, '$'))
575
576  set dictionary=
577  %d
578  call setline(1, 'A')
579  call cursor(1, 1)
580  set belloff=all
581  let v:testing = 1
582  try
583    call feedkeys("A\<c-x>\<c-k>\<esc>", 'tnix')
584  catch
585    " error sleeps 2 seconds, when v:testing is not set
586    let v:testing = 0
587  endtry
588  set belloff=
589  call delete('Xdictionary.txt')
590
591  if has("multi_byte")
592    call test_override("char_avail", 1)
593    set showcmd
594    %d
595    call feedkeys("A\<c-k>a:\<esc>", 'tnix')
596    call assert_equal(['ä'], getline(1, '$'))
597    call test_override("char_avail", 0)
598    set noshowcmd
599  endif
600  bw!
601endfunc
602
603func! Test_edit_CTRL_L()
604  " Test Ctrl-X Ctrl-L (line completion)
605  new
606  set complete=.
607  call setline(1, ['one', 'two', 'three', '', '', '', ''])
608  call cursor(4, 1)
609  call feedkeys("A\<c-x>\<c-l>\<esc>", 'tnix')
610  call assert_equal(['one', 'two', 'three', 'three', '', '', ''], getline(1, '$'))
611  call feedkeys("cct\<c-x>\<c-l>\<c-n>\<esc>", 'tnix')
612  call assert_equal(['one', 'two', 'three', 't', '', '', ''], getline(1, '$'))
613  call feedkeys("cct\<c-x>\<c-l>\<c-n>\<c-n>\<esc>", 'tnix')
614  call assert_equal(['one', 'two', 'three', 't', '', '', ''], getline(1, '$'))
615  call feedkeys("cct\<c-x>\<c-l>\<c-n>\<c-n>\<c-n>\<esc>", 'tnix')
616  call assert_equal(['one', 'two', 'three', 'two', '', '', ''], getline(1, '$'))
617  call feedkeys("cct\<c-x>\<c-l>\<c-n>\<c-n>\<c-n>\<c-n>\<esc>", 'tnix')
618  call assert_equal(['one', 'two', 'three', 'three', '', '', ''], getline(1, '$'))
619  call feedkeys("cct\<c-x>\<c-l>\<c-p>\<esc>", 'tnix')
620  call assert_equal(['one', 'two', 'three', 'two', '', '', ''], getline(1, '$'))
621  call feedkeys("cct\<c-x>\<c-l>\<c-p>\<c-p>\<esc>", 'tnix')
622  call assert_equal(['one', 'two', 'three', 't', '', '', ''], getline(1, '$'))
623  call feedkeys("cct\<c-x>\<c-l>\<c-p>\<c-p>\<c-p>\<esc>", 'tnix')
624  call assert_equal(['one', 'two', 'three', 'three', '', '', ''], getline(1, '$'))
625  set complete=
626  call cursor(5, 1)
627  call feedkeys("A\<c-x>\<c-l>\<c-p>\<c-n>\<esc>", 'tnix')
628  call assert_equal(['one', 'two', 'three', 'three', "\<c-l>\<c-p>\<c-n>", '', ''], getline(1, '$'))
629  set complete&
630  %d
631  if has("conceal") && has("syntax")
632    call setline(1, ['foo', 'bar', 'foobar'])
633    call test_override("char_avail", 1)
634    set conceallevel=2 concealcursor=n
635    syn on
636    syn match ErrorMsg "^bar"
637    call matchadd("Conceal", 'oo', 10, -1, {'conceal': 'X'})
638    func! DoIt()
639      let g:change=1
640    endfunc
641    au! TextChangedI <buffer> :call DoIt()
642
643    call cursor(2, 1)
644    call assert_false(exists("g:change"))
645    call feedkeys("A \<esc>", 'tnix')
646    call assert_equal(['foo', 'bar ', 'foobar'], getline(1, '$'))
647    call assert_equal(1, g:change)
648
649    call test_override("char_avail", 0)
650    call clearmatches()
651    syn off
652    au! TextChangedI
653    delfu DoIt
654    unlet! g:change
655  endif
656  bw!
657endfunc
658
659func! Test_edit_CTRL_N()
660  " Check keyword completion
661  new
662  set complete=.
663  call setline(1, ['INFER', 'loWER', '', '', ])
664  call cursor(3, 1)
665  call feedkeys("Ai\<c-n>\<cr>\<esc>", "tnix")
666  call feedkeys("ILO\<c-n>\<cr>\<esc>", 'tnix')
667  call assert_equal(['INFER', 'loWER', 'i', 'LO', '', ''], getline(1, '$'))
668  %d
669  call setline(1, ['INFER', 'loWER', '', '', ])
670  call cursor(3, 1)
671  set ignorecase infercase
672  call feedkeys("Ii\<c-n>\<cr>\<esc>", "tnix")
673  call feedkeys("ILO\<c-n>\<cr>\<esc>", 'tnix')
674  call assert_equal(['INFER', 'loWER', 'infer', 'LOWER', '', ''], getline(1, '$'))
675
676  set noignorecase noinfercase complete&
677  bw!
678endfunc
679
680func! Test_edit_CTRL_O()
681  " Check for CTRL-O in insert mode
682  new
683  inoreabbr <buffer> h here some more
684  call setline(1, ['abc', 'def'])
685  call cursor(1, 1)
686  " Ctrl-O after an abbreviation
687  exe "norm A h\<c-o>:set nu\<cr> text"
688  call assert_equal(['abc here some more text', 'def'], getline(1, '$'))
689  call assert_true(&nu)
690  set nonu
691  iunabbr <buffer> h
692  " Ctrl-O at end of line with 've'=onemore
693  call cursor(1, 1)
694  call feedkeys("A\<c-o>:let g:a=getpos('.')\<cr>\<esc>", 'tnix')
695  call assert_equal([0, 1, 23, 0], g:a)
696  call cursor(1, 1)
697  set ve=onemore
698  call feedkeys("A\<c-o>:let g:a=getpos('.')\<cr>\<esc>", 'tnix')
699  call assert_equal([0, 1, 24, 0], g:a)
700  set ve=
701  unlet! g:a
702  bw!
703endfunc
704
705func! Test_edit_CTRL_R()
706  " Insert Register
707  new
708  call test_override("ALL", 1)
709  set showcmd
710  call feedkeys("AFOOBAR eins zwei\<esc>", 'tnix')
711  call feedkeys("O\<c-r>.", 'tnix')
712  call feedkeys("O\<c-r>=10*500\<cr>\<esc>", 'tnix')
713  call feedkeys("O\<c-r>=getreg('=', 1)\<cr>\<esc>", 'tnix')
714  call assert_equal(["getreg('=', 1)", '5000', "FOOBAR eins zwei", "FOOBAR eins zwei"], getline(1, '$'))
715  call test_override("ALL", 0)
716  set noshowcmd
717  bw!
718endfunc
719
720func! Test_edit_CTRL_S()
721  " Test pressing CTRL-S (basically only spellfile completion)
722  " the rest is already covered
723  new
724  if !has("spell")
725    call setline(1, 'vim')
726    call feedkeys("A\<c-x>ss\<cr>\<esc>", 'tnix')
727    call assert_equal(['vims', ''], getline(1, '$'))
728    bw!
729    return
730  endif
731  call setline(1, 'vim')
732  " spell option not yet set
733  try
734    call feedkeys("A\<c-x>\<c-s>\<cr>\<esc>", 'tnix')
735  catch /^Vim\%((\a\+)\)\=:E756/
736    call assert_true(1, 'error caught')
737  endtry
738  call assert_equal(['vim', ''], getline(1, '$'))
739  %d
740  setl spell spelllang=en
741  call setline(1, 'vim')
742  call cursor(1, 1)
743  call feedkeys("A\<c-x>\<c-s>\<cr>\<esc>", 'tnix')
744  call assert_equal(['Vim', ''], getline(1, '$'))
745  %d
746  call setline(1, 'vim')
747  call cursor(1, 1)
748  call feedkeys("A\<c-x>\<c-s>\<down>\<cr>\<esc>", 'tnix')
749  call assert_equal(['Aim'], getline(1, '$'))
750  %d
751  call setline(1, 'vim')
752  call cursor(1, 1)
753  call feedkeys("A\<c-x>\<c-s>\<c-p>\<cr>\<esc>", 'tnix')
754  call assert_equal(['vim', ''], getline(1, '$'))
755  %d
756  " empty buffer
757  call cursor(1, 1)
758  call feedkeys("A\<c-x>\<c-s>\<c-p>\<cr>\<esc>", 'tnix')
759  call assert_equal(['', ''], getline(1, '$'))
760  setl nospell
761  bw!
762endfunc
763
764func! Test_edit_CTRL_T()
765  " Check for CTRL-T and CTRL-X CTRL-T in insert mode
766  " 1) increase indent
767  new
768  call setline(1, "abc")
769  call cursor(1, 1)
770  call feedkeys("A\<c-t>xyz", 'tnix')
771  call assert_equal(["\<tab>abcxyz"], getline(1, '$'))
772  " 2) also when paste option is set
773  set paste
774  call setline(1, "abc")
775  call cursor(1, 1)
776  call feedkeys("A\<c-t>xyz", 'tnix')
777  call assert_equal(["\<tab>abcxyz"], getline(1, '$'))
778  set nopaste
779  " CTRL-X CTRL-T (thesaurus complete)
780  call writefile(['angry furious mad enraged'], 'Xthesaurus')
781  set thesaurus=Xthesaurus
782  call setline(1, 'mad')
783  call cursor(1, 1)
784  call feedkeys("A\<c-x>\<c-t>\<cr>\<esc>", 'tnix')
785  call assert_equal(['mad', ''], getline(1, '$'))
786  %d
787  call setline(1, 'mad')
788  call cursor(1, 1)
789  call feedkeys("A\<c-x>\<c-t>\<c-n>\<cr>\<esc>", 'tnix')
790  call assert_equal(['angry', ''], getline(1, '$'))
791  %d
792  call setline(1, 'mad')
793  call cursor(1, 1)
794  call feedkeys("A\<c-x>\<c-t>\<c-n>\<c-n>\<cr>\<esc>", 'tnix')
795  call assert_equal(['furious', ''], getline(1, '$'))
796  %d
797  call setline(1, 'mad')
798  call cursor(1, 1)
799  call feedkeys("A\<c-x>\<c-t>\<c-n>\<c-n>\<c-n>\<cr>\<esc>", 'tnix')
800  call assert_equal(['enraged', ''], getline(1, '$'))
801  %d
802  call setline(1, 'mad')
803  call cursor(1, 1)
804  call feedkeys("A\<c-x>\<c-t>\<c-n>\<c-n>\<c-n>\<c-n>\<cr>\<esc>", 'tnix')
805  call assert_equal(['mad', ''], getline(1, '$'))
806  %d
807  call setline(1, 'mad')
808  call cursor(1, 1)
809  call feedkeys("A\<c-x>\<c-t>\<c-n>\<c-n>\<c-n>\<c-n>\<c-n>\<cr>\<esc>", 'tnix')
810  call assert_equal(['mad', ''], getline(1, '$'))
811  " Using <c-p> <c-n> when 'complete' is empty
812  set complete=
813  %d
814  call setline(1, 'mad')
815  call cursor(1, 1)
816  call feedkeys("A\<c-x>\<c-t>\<c-n>\<cr>\<esc>", 'tnix')
817  call assert_equal(['angry', ''], getline(1, '$'))
818  %d
819  call setline(1, 'mad')
820  call cursor(1, 1)
821  call feedkeys("A\<c-x>\<c-t>\<c-p>\<cr>\<esc>", 'tnix')
822  call assert_equal(['mad', ''], getline(1, '$'))
823  set complete&
824
825  set thesaurus=
826  %d
827  call setline(1, 'mad')
828  call cursor(1, 1)
829  set belloff=all
830  let v:testing = 1
831  try
832    call feedkeys("A\<c-x>\<c-t>\<esc>", 'tnix')
833  catch
834    " error sleeps 2 seconds, when v:testing is not set
835    let v:testing = 0
836  endtry
837  set belloff=
838  call assert_equal(['mad'], getline(1, '$'))
839  call delete('Xthesaurus')
840  bw!
841endfunc
842
843func! Test_edit_CTRL_U()
844  " Test 'completefunc'
845  new
846  " -1, -2 and -3 are special return values
847  let g:special=0
848  fun! CompleteMonths(findstart, base)
849    if a:findstart
850      " locate the start of the word
851      return g:special
852    else
853      " find months matching with "a:base"
854      let res = []
855      for m in split("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec")
856        if m =~ '^\c'.a:base
857          call add(res, {'word': m, 'abbr': m.' Month', 'icase': 0})
858        endif
859      endfor
860      return {'words': res, 'refresh': 'always'}
861    endif
862  endfun
863  set completefunc=CompleteMonths
864  call setline(1, ['', ''])
865  call cursor(1, 1)
866  call feedkeys("AX\<c-x>\<c-u>\<cr>\<esc>", 'tnix')
867  call assert_equal(['X', '', ''], getline(1, '$'))
868  %d
869  let g:special=-1
870  call feedkeys("AX\<c-x>\<c-u>\<cr>\<esc>", 'tnix')
871  call assert_equal(['XJan', ''], getline(1, '$'))
872  %d
873  let g:special=-2
874  call feedkeys("AX\<c-x>\<c-u>\<cr>\<esc>", 'tnix')
875  call assert_equal(['X', ''], getline(1, '$'))
876  %d
877  let g:special=-3
878  call feedkeys("AX\<c-x>\<c-u>\<cr>\<esc>", 'tnix')
879  call assert_equal(['X', ''], getline(1, '$'))
880  %d
881  let g:special=0
882  call feedkeys("AM\<c-x>\<c-u>\<cr>\<esc>", 'tnix')
883  call assert_equal(['Mar', ''], getline(1, '$'))
884  %d
885  call feedkeys("AM\<c-x>\<c-u>\<c-n>\<cr>\<esc>", 'tnix')
886  call assert_equal(['May', ''], getline(1, '$'))
887  %d
888  call feedkeys("AM\<c-x>\<c-u>\<c-n>\<c-n>\<cr>\<esc>", 'tnix')
889  call assert_equal(['M', ''], getline(1, '$'))
890  delfu CompleteMonths
891  %d
892  try
893    call feedkeys("A\<c-x>\<c-u>", 'tnix')
894    call assert_fails(1, 'unknown completion function')
895  catch /^Vim\%((\a\+)\)\=:E117/
896    call assert_true(1, 'E117 error caught')
897  endtry
898  set completefunc=
899  bw!
900endfunc
901
902func! Test_edit_CTRL_Z()
903  " Ctrl-Z when insertmode is not set inserts it literally
904  new
905  call setline(1, 'abc')
906  call feedkeys("A\<c-z>\<esc>", 'tnix')
907  call assert_equal(["abc\<c-z>"], getline(1,'$'))
908  bw!
909  " TODO: How to Test Ctrl-Z in insert mode, e.g. suspend?
910endfunc
911
912func! Test_edit_DROP()
913  if !has("dnd")
914    return
915  endif
916  new
917  call setline(1, ['abc def ghi'])
918  call cursor(1, 1)
919  try
920    call feedkeys("i\<Drop>\<Esc>", 'tnix')
921    call assert_fails(1, 'Invalid register name')
922  catch /^Vim\%((\a\+)\)\=:E353/
923    call assert_true(1, 'error caught')
924  endtry
925  bw!
926endfunc
927
928func! Test_edit_CTRL_V()
929  if has("ebcdic")
930    return
931  endif
932  new
933  call setline(1, ['abc'])
934  call cursor(2, 1)
935  " force some redraws
936  set showmode showcmd
937  "call test_override_char_avail(1)
938  call test_override('ALL', 1)
939  call feedkeys("A\<c-v>\<c-n>\<c-v>\<c-l>\<c-v>\<c-b>\<esc>", 'tnix')
940  call assert_equal(["abc\x0e\x0c\x02"], getline(1, '$'))
941
942  if has("rightleft") && exists("+rl")
943    set rl
944    call setline(1, ['abc'])
945    call cursor(2, 1)
946    call feedkeys("A\<c-v>\<c-n>\<c-v>\<c-l>\<c-v>\<c-b>\<esc>", 'tnix')
947    call assert_equal(["abc\x0e\x0c\x02"], getline(1, '$'))
948    set norl
949  endif
950
951  call test_override('ALL', 0)
952  set noshowmode showcmd
953  bw!
954endfunc
955
956func! Test_edit_F1()
957  " Pressing <f1>
958  new
959  call feedkeys(":set im\<cr>\<f1>\<c-l>", 'tnix')
960  set noinsertmode
961  call assert_equal('help', &buftype)
962  bw
963  bw
964endfunc
965
966func! Test_edit_F21()
967  " Pressing <f21>
968  " sends a netbeans command
969  if has("netbeans_intg")
970    new
971    " I have no idea what this is supposed to do :)
972    call feedkeys("A\<F21>\<F1>\<esc>", 'tnix')
973    bw
974  endif
975endfunc
976
977func! Test_edit_HOME_END()
978  " Test Home/End Keys
979  new
980  set foldopen+=hor
981  call setline(1, ['abc', 'def'])
982  call cursor(1, 1)
983  call feedkeys("AX\<Home>Y\<esc>", 'tnix')
984  call cursor(2, 1)
985  call feedkeys("iZ\<End>Y\<esc>", 'tnix')
986  call assert_equal(['YabcX', 'ZdefY'], getline(1, '$'))
987
988  set foldopen-=hor
989  bw!
990endfunc
991
992func! Test_edit_INS()
993  " Test for Pressing <Insert>
994  new
995  call setline(1, ['abc', 'def'])
996  call cursor(1, 1)
997  call feedkeys("i\<Insert>ZYX>", 'tnix')
998  call assert_equal(['ZYX>', 'def'], getline(1, '$'))
999  call setline(1, ['abc', 'def'])
1000  call cursor(1, 1)
1001  call feedkeys("i\<Insert>Z\<Insert>YX>", 'tnix')
1002  call assert_equal(['ZYX>bc', 'def'], getline(1, '$'))
1003  bw!
1004endfunc
1005
1006func! Test_edit_LEFT_RIGHT()
1007  " Left, Shift-Left, Right, Shift-Right
1008  new
1009  set belloff=all
1010  call setline(1, ['abc def ghi', 'ABC DEF GHI', 'ZZZ YYY XXX'])
1011  let _ww=&ww
1012  set ww=
1013  call cursor(2, 1)
1014  call feedkeys("i\<left>\<esc>", 'tnix')
1015  call assert_equal([0, 2, 1, 0], getpos('.'))
1016  " Is this a bug, <s-left> does not respect whichwrap option
1017  call feedkeys("i\<s-left>\<esc>", 'tnix')
1018  call assert_equal([0, 1, 8, 0], getpos('.'))
1019  call feedkeys("i". repeat("\<s-left>", 3). "\<esc>", 'tnix')
1020  call assert_equal([0, 1, 1, 0], getpos('.'))
1021  call feedkeys("i\<right>\<esc>", 'tnix')
1022  call assert_equal([0, 1, 1, 0], getpos('.'))
1023  call feedkeys("i\<right>\<right>\<esc>", 'tnix')
1024  call assert_equal([0, 1, 2, 0], getpos('.'))
1025  call feedkeys("A\<right>\<esc>", 'tnix')
1026  call assert_equal([0, 1, 11, 0], getpos('.'))
1027  call feedkeys("A\<s-right>\<esc>", 'tnix')
1028  call assert_equal([0, 2, 1, 0], getpos('.'))
1029  call feedkeys("i\<s-right>\<esc>", 'tnix')
1030  call assert_equal([0, 2, 4, 0], getpos('.'))
1031  call cursor(3, 11)
1032  call feedkeys("A\<right>\<esc>", 'tnix')
1033  call feedkeys("A\<s-right>\<esc>", 'tnix')
1034  call assert_equal([0, 3, 11, 0], getpos('.'))
1035  call cursor(2, 11)
1036  " <S-Right> does not respect 'whichwrap' option
1037  call feedkeys("A\<s-right>\<esc>", 'tnix')
1038  call assert_equal([0, 3, 1, 0], getpos('.'))
1039  " Check motion when 'whichwrap' contains cursor keys for insert mode
1040  set ww+=[,]
1041  call cursor(2, 1)
1042  call feedkeys("i\<left>\<esc>", 'tnix')
1043  call assert_equal([0, 1, 11, 0], getpos('.'))
1044  call cursor(2, 11)
1045  call feedkeys("A\<right>\<esc>", 'tnix')
1046  call assert_equal([0, 3, 1, 0], getpos('.'))
1047  call cursor(2, 11)
1048  call feedkeys("A\<s-right>\<esc>", 'tnix')
1049  call assert_equal([0, 3, 1, 0], getpos('.'))
1050  let &ww = _ww
1051  set belloff=
1052  bw!
1053endfunc
1054
1055func! Test_edit_MOUSE()
1056  " This is a simple test, since we not really using the mouse here
1057  if !has("mouse")
1058    return
1059  endif
1060  10new
1061  call setline(1, range(1, 100))
1062  call cursor(1, 1)
1063  set mouse=a
1064  call feedkeys("A\<ScrollWheelDown>\<esc>", 'tnix')
1065  call assert_equal([0, 4, 1, 0], getpos('.'))
1066  " This should move by one pageDown, but only moves
1067  " by one line when the test is run...
1068  call feedkeys("A\<S-ScrollWheelDown>\<esc>", 'tnix')
1069  call assert_equal([0, 5, 1, 0], getpos('.'))
1070  set nostartofline
1071  call feedkeys("A\<C-ScrollWheelDown>\<esc>", 'tnix')
1072  call assert_equal([0, 6, 1, 0], getpos('.'))
1073  call feedkeys("A\<LeftMouse>\<esc>", 'tnix')
1074  call assert_equal([0, 6, 1, 0], getpos('.'))
1075  call feedkeys("A\<RightMouse>\<esc>", 'tnix')
1076  call assert_equal([0, 6, 1, 0], getpos('.'))
1077  call cursor(1, 100)
1078  norm! zt
1079  " this should move by a screen up, but when the test
1080  " is run, it moves up to the top of the buffer...
1081  call feedkeys("A\<ScrollWheelUp>\<esc>", 'tnix')
1082  call assert_equal([0, 1, 1, 0], getpos('.'))
1083  call cursor(1, 30)
1084  norm! zt
1085  call feedkeys("A\<S-ScrollWheelUp>\<esc>", 'tnix')
1086  call assert_equal([0, 1, 1, 0], getpos('.'))
1087  call cursor(1, 30)
1088  norm! zt
1089  call feedkeys("A\<C-ScrollWheelUp>\<esc>", 'tnix')
1090  call assert_equal([0, 1, 1, 0], getpos('.'))
1091  %d
1092  call setline(1, repeat(["12345678901234567890"], 100))
1093  call cursor(2, 1)
1094  call feedkeys("A\<ScrollWheelRight>\<esc>", 'tnix')
1095  call assert_equal([0, 2, 20, 0], getpos('.'))
1096  call feedkeys("A\<ScrollWheelLeft>\<esc>", 'tnix')
1097  call assert_equal([0, 2, 20, 0], getpos('.'))
1098  call feedkeys("A\<S-ScrollWheelRight>\<esc>", 'tnix')
1099  call assert_equal([0, 2, 20, 0], getpos('.'))
1100  call feedkeys("A\<S-ScrollWheelLeft>\<esc>", 'tnix')
1101  call assert_equal([0, 2, 20, 0], getpos('.'))
1102  call feedkeys("A\<C-ScrollWheelRight>\<esc>", 'tnix')
1103  call assert_equal([0, 2, 20, 0], getpos('.'))
1104  call feedkeys("A\<C-ScrollWheelLeft>\<esc>", 'tnix')
1105  call assert_equal([0, 2, 20, 0], getpos('.'))
1106  set mouse& startofline
1107  bw!
1108endfunc
1109
1110func! Test_edit_PAGEUP_PAGEDOWN()
1111  set belloff=all
1112  10new
1113  call setline(1, repeat(['abc def ghi'], 30))
1114  call cursor(1, 1)
1115  call feedkeys("i\<PageDown>\<esc>", 'tnix')
1116  call assert_equal([0, 9, 1, 0], getpos('.'))
1117  call feedkeys("i\<PageDown>\<esc>", 'tnix')
1118  call assert_equal([0, 17, 1, 0], getpos('.'))
1119  call feedkeys("i\<PageDown>\<esc>", 'tnix')
1120  call assert_equal([0, 25, 1, 0], getpos('.'))
1121  call feedkeys("i\<PageDown>\<esc>", 'tnix')
1122  call assert_equal([0, 30, 1, 0], getpos('.'))
1123  call feedkeys("i\<PageDown>\<esc>", 'tnix')
1124  call assert_equal([0, 30, 1, 0], getpos('.'))
1125  call feedkeys("A\<PageUp>\<esc>", 'tnix')
1126  call assert_equal([0, 29, 1, 0], getpos('.'))
1127  call feedkeys("A\<PageUp>\<esc>", 'tnix')
1128  call assert_equal([0, 21, 1, 0], getpos('.'))
1129  call feedkeys("A\<PageUp>\<esc>", 'tnix')
1130  call assert_equal([0, 13, 1, 0], getpos('.'))
1131  call feedkeys("A\<PageUp>\<esc>", 'tnix')
1132  call assert_equal([0, 5, 1, 0], getpos('.'))
1133  call feedkeys("A\<PageUp>\<esc>", 'tnix')
1134  call assert_equal([0, 5, 11, 0], getpos('.'))
1135  " <S-Up> is the same as <PageUp>
1136  " <S-Down> is the same as <PageDown>
1137  call cursor(1, 1)
1138  call feedkeys("i\<S-Down>\<esc>", 'tnix')
1139  call assert_equal([0, 9, 1, 0], getpos('.'))
1140  call feedkeys("i\<S-Down>\<esc>", 'tnix')
1141  call assert_equal([0, 17, 1, 0], getpos('.'))
1142  call feedkeys("i\<S-Down>\<esc>", 'tnix')
1143  call assert_equal([0, 25, 1, 0], getpos('.'))
1144  call feedkeys("i\<S-Down>\<esc>", 'tnix')
1145  call assert_equal([0, 30, 1, 0], getpos('.'))
1146  call feedkeys("i\<S-Down>\<esc>", 'tnix')
1147  call assert_equal([0, 30, 1, 0], getpos('.'))
1148  call feedkeys("A\<S-Up>\<esc>", 'tnix')
1149  call assert_equal([0, 29, 1, 0], getpos('.'))
1150  call feedkeys("A\<S-Up>\<esc>", 'tnix')
1151  call assert_equal([0, 21, 1, 0], getpos('.'))
1152  call feedkeys("A\<S-Up>\<esc>", 'tnix')
1153  call assert_equal([0, 13, 1, 0], getpos('.'))
1154  call feedkeys("A\<S-Up>\<esc>", 'tnix')
1155  call assert_equal([0, 5, 1, 0], getpos('.'))
1156  call feedkeys("A\<S-Up>\<esc>", 'tnix')
1157  call assert_equal([0, 5, 11, 0], getpos('.'))
1158  set nostartofline
1159  call cursor(30, 11)
1160  norm! zt
1161  call feedkeys("A\<PageUp>\<esc>", 'tnix')
1162  call assert_equal([0, 29, 11, 0], getpos('.'))
1163  call feedkeys("A\<PageUp>\<esc>", 'tnix')
1164  call assert_equal([0, 21, 11, 0], getpos('.'))
1165  call feedkeys("A\<PageUp>\<esc>", 'tnix')
1166  call assert_equal([0, 13, 11, 0], getpos('.'))
1167  call feedkeys("A\<PageUp>\<esc>", 'tnix')
1168  call assert_equal([0, 5, 11, 0], getpos('.'))
1169  call feedkeys("A\<PageUp>\<esc>", 'tnix')
1170  call assert_equal([0, 5, 11, 0], getpos('.'))
1171  call cursor(1, 1)
1172  call feedkeys("A\<PageDown>\<esc>", 'tnix')
1173  call assert_equal([0, 9, 11, 0], getpos('.'))
1174  call feedkeys("A\<PageDown>\<esc>", 'tnix')
1175  call assert_equal([0, 17, 11, 0], getpos('.'))
1176  call feedkeys("A\<PageDown>\<esc>", 'tnix')
1177  call assert_equal([0, 25, 11, 0], getpos('.'))
1178  call feedkeys("A\<PageDown>\<esc>", 'tnix')
1179  call assert_equal([0, 30, 11, 0], getpos('.'))
1180  call feedkeys("A\<PageDown>\<esc>", 'tnix')
1181  call assert_equal([0, 30, 11, 0], getpos('.'))
1182  " <S-Up> is the same as <PageUp>
1183  " <S-Down> is the same as <PageDown>
1184  call cursor(30, 11)
1185  norm! zt
1186  call feedkeys("A\<S-Up>\<esc>", 'tnix')
1187  call assert_equal([0, 29, 11, 0], getpos('.'))
1188  call feedkeys("A\<S-Up>\<esc>", 'tnix')
1189  call assert_equal([0, 21, 11, 0], getpos('.'))
1190  call feedkeys("A\<S-Up>\<esc>", 'tnix')
1191  call assert_equal([0, 13, 11, 0], getpos('.'))
1192  call feedkeys("A\<S-Up>\<esc>", 'tnix')
1193  call assert_equal([0, 5, 11, 0], getpos('.'))
1194  call feedkeys("A\<S-Up>\<esc>", 'tnix')
1195  call assert_equal([0, 5, 11, 0], getpos('.'))
1196  call cursor(1, 1)
1197  call feedkeys("A\<S-Down>\<esc>", 'tnix')
1198  call assert_equal([0, 9, 11, 0], getpos('.'))
1199  call feedkeys("A\<S-Down>\<esc>", 'tnix')
1200  call assert_equal([0, 17, 11, 0], getpos('.'))
1201  call feedkeys("A\<S-Down>\<esc>", 'tnix')
1202  call assert_equal([0, 25, 11, 0], getpos('.'))
1203  call feedkeys("A\<S-Down>\<esc>", 'tnix')
1204  call assert_equal([0, 30, 11, 0], getpos('.'))
1205  call feedkeys("A\<S-Down>\<esc>", 'tnix')
1206  call assert_equal([0, 30, 11, 0], getpos('.'))
1207  set startofline belloff=
1208  bw!
1209endfunc
1210
1211func! Test_edit_forbidden()
1212  set belloff=error,esc
1213  new
1214  " 1) edit in the sandbox is not allowed
1215  call setline(1, 'a')
1216  com! Sandbox :sandbox call feedkeys("i\<del>\<esc>", 'tnix')
1217  call assert_fails(':Sandbox', 'E48:')
1218  com! Sandbox :sandbox exe "norm! i\<del>"
1219  call assert_fails(':Sandbox', 'E48:')
1220  delcom Sandbox
1221  call assert_equal(['a'], getline(1,'$'))
1222  " 2) edit with textlock set
1223  fu! DoIt()
1224    call feedkeys("i\<del>\<esc>", 'tnix')
1225  endfu
1226  au InsertCharPre <buffer> :call DoIt()
1227  try
1228    call feedkeys("ix\<esc>", 'tnix')
1229    call assert_fails(1, 'textlock')
1230  catch /^Vim\%((\a\+)\)\=:E523/ " catch E523: not allowed here
1231  endtry
1232  " TODO: Might be a bug: should x really be inserted here
1233  call assert_equal(['xa'], getline(1, '$'))
1234  delfu DoIt
1235  try
1236    call feedkeys("ix\<esc>", 'tnix')
1237    call assert_fails(1, 'unknown function')
1238  catch /^Vim\%((\a\+)\)\=:E117/ " catch E117: unknown function
1239  endtry
1240  au! InsertCharPre
1241  " 3) edit when completion is shown
1242  fun! Complete(findstart, base)
1243    if a:findstart
1244      return col('.')
1245    else
1246      call feedkeys("i\<del>\<esc>", 'tnix')
1247      return []
1248    endif
1249  endfun
1250  set completefunc=Complete
1251  try
1252    call feedkeys("i\<c-x>\<c-u>\<esc>", 'tnix')
1253    call assert_fails(1, 'change in complete function')
1254  catch /^Vim\%((\a\+)\)\=:E523/ " catch E523
1255  endtry
1256  delfu Complete
1257  set completefunc=
1258  if has("rightleft") && exists("+fkmap")
1259    " 4) 'R' when 'fkmap' and 'revins' is set.
1260    set revins fkmap
1261    try
1262      normal Ri
1263      call assert_fails(1, "R with 'fkmap' and 'ri' set")
1264    catch
1265    finally
1266      set norevins nofkmap
1267    endtry
1268  endif
1269  set belloff=
1270  bw!
1271endfunc
1272
1273func! Test_edit_rightleft()
1274  " Cursor in rightleft mode moves differently
1275  if !exists("+rightleft")
1276    return
1277  endif
1278  call NewWindow(10, 20)
1279  call setline(1, ['abc', 'def', 'ghi'])
1280  call cursor(1, 2)
1281  set rightleft
1282  " Screen looks as expected
1283  let lines = ScreenLines([1, 4], winwidth(0))
1284  let expect = [
1285        \"                 cba",
1286        \"                 fed",
1287        \"                 ihg",
1288        \"                   ~"]
1289  call assert_equal(join(expect, "\n"), join(lines, "\n"))
1290  " 2) right moves to the left
1291  call feedkeys("i\<right>\<esc>x", 'txin')
1292  call assert_equal(['bc', 'def', 'ghi'], getline(1,'$'))
1293  call cursor(1, 2)
1294  call feedkeys("i\<s-right>\<esc>", 'txin')
1295  call cursor(1, 2)
1296  call feedkeys("i\<c-right>\<esc>", 'txin')
1297  " Screen looks as expected
1298  let lines = ScreenLines([1, 4], winwidth(0))
1299  let expect = [
1300        \"                  cb",
1301        \"                 fed",
1302        \"                 ihg",
1303        \"                   ~"]
1304  call assert_equal(join(expect, "\n"), join(lines, "\n"))
1305  " 2) left moves to the right
1306  call setline(1, ['abc', 'def', 'ghi'])
1307  call cursor(1, 2)
1308  call feedkeys("i\<left>\<esc>x", 'txin')
1309  call assert_equal(['ac', 'def', 'ghi'], getline(1,'$'))
1310  call cursor(1, 2)
1311  call feedkeys("i\<s-left>\<esc>", 'txin')
1312  call cursor(1, 2)
1313  call feedkeys("i\<c-left>\<esc>", 'txin')
1314  " Screen looks as expected
1315  let lines = ScreenLines([1, 4], winwidth(0))
1316  let expect = [
1317        \"                  ca",
1318        \"                 fed",
1319        \"                 ihg",
1320        \"                   ~"]
1321  call assert_equal(join(expect, "\n"), join(lines, "\n"))
1322  set norightleft
1323  bw!
1324endfunc
1325
1326func Test_edit_complete_very_long_name()
1327  if !has('unix')
1328    " Long directory names only work on Unix.
1329    return
1330  endif
1331
1332  let dirname = getcwd() . "/Xdir"
1333  let longdirname = dirname . repeat('/' . repeat('d', 255), 4)
1334  try
1335    call mkdir(longdirname, 'p')
1336  catch /E739:/
1337    " Long directory name probably not supported.
1338    call delete(dirname, 'rf')
1339    return
1340  endtry
1341
1342  " Try to get the Vim window position before setting 'columns'.
1343  let winposx = getwinposx()
1344  let winposy = getwinposy()
1345  let save_columns = &columns
1346  " Need at least about 1100 columns to reproduce the problem.
1347  set columns=2000
1348  call assert_equal(2000, &columns)
1349  set noswapfile
1350
1351  let longfilename = longdirname . '/' . repeat('a', 255)
1352  call writefile(['Totum', 'Table'], longfilename)
1353  new
1354  exe "next Xfile " . longfilename
1355  exe "normal iT\<C-N>"
1356
1357  bwipe!
1358  exe 'bwipe! ' . longfilename
1359  call delete(dirname, 'rf')
1360  let &columns = save_columns
1361  if winposx >= 0 && winposy >= 0
1362    exe 'winpos ' . winposx . ' ' . winposy
1363  endif
1364  set swapfile&
1365endfunc
1366