xref: /vim-8.2.3635/src/testdir/test_edit.vim (revision 73f4439c)
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_11_indentexpr()
318  " Test that indenting kicks in
319  new
320  " Use indentexpr instead of cindenting
321  func! Do_Indent()
322    let pline=prevnonblank(v:lnum)
323    if empty(getline(v:lnum))
324      if getline(pline) =~ 'if\|then'
325        return shiftwidth()
326      else
327        return 0
328      endif
329    else
330        return 0
331    endif
332  endfunc
333  setl indentexpr=Do_Indent() indentkeys+=0=then,0=fi
334  call setline(1, ['if [ $this ]'])
335  call cursor(1, 1)
336  call feedkeys("othen\<cr>that\<cr>fi", 'tnix')
337  call assert_equal(['if [ $this ]', "then", "\<tab>that", "fi"], getline(1, '$'))
338  set cinkeys&vim indentkeys&vim
339  set nocindent indentexpr=
340  delfu Do_Indent
341  bw!
342endfunc
343
344func! Test_edit_12()
345  " Test changing indent in replace mode
346  new
347  call setline(1, ["\tabc", "\tdef"])
348  call cursor(2, 4)
349  call feedkeys("R^\<c-d>", 'tnix')
350  call assert_equal(["\tabc", "def"], getline(1, '$'))
351  call assert_equal([0, 2, 2, 0], getpos('.'))
352  %d
353  call setline(1, ["\tabc", "\t\tdef"])
354  call cursor(2, 2)
355  call feedkeys("R^\<c-d>", 'tnix')
356  call assert_equal(["\tabc", "def"], getline(1, '$'))
357  call assert_equal([0, 2, 1, 0], getpos('.'))
358  %d
359  call setline(1, ["\tabc", "\t\tdef"])
360  call cursor(2, 2)
361  call feedkeys("R\<c-t>", 'tnix')
362  call assert_equal(["\tabc", "\t\t\tdef"], getline(1, '$'))
363  call assert_equal([0, 2, 2, 0], getpos('.'))
364  bw!
365  10vnew
366  call setline(1, ["\tabc", "\t\tdef"])
367  call cursor(2, 2)
368  call feedkeys("R\<c-t>", 'tnix')
369  call assert_equal(["\tabc", "\t\t\tdef"], getline(1, '$'))
370  call assert_equal([0, 2, 2, 0], getpos('.'))
371  %d
372  set sw=4
373  call setline(1, ["\tabc", "\t\tdef"])
374  call cursor(2, 2)
375  call feedkeys("R\<c-t>\<c-t>", 'tnix')
376  call assert_equal(["\tabc", "\t\t\tdef"], getline(1, '$'))
377  call assert_equal([0, 2, 2, 0], getpos('.'))
378  %d
379  call setline(1, ["\tabc", "\t\tdef"])
380  call cursor(2, 2)
381  call feedkeys("R\<c-t>\<c-t>", 'tnix')
382  call assert_equal(["\tabc", "\t\t\tdef"], getline(1, '$'))
383  call assert_equal([0, 2, 2, 0], getpos('.'))
384  set et
385  set sw& et&
386  %d
387  call setline(1, ["\t/*"])
388  set formatoptions=croql
389  call cursor(1, 3)
390  call feedkeys("A\<cr>\<cr>/", 'tnix')
391  call assert_equal(["\t/*", " *", " */"], getline(1, '$'))
392  set formatoptions&
393  bw!
394endfunc
395
396func! Test_edit_13()
397  " Test smartindenting
398  if exists("+smartindent")
399    new
400    set smartindent autoindent
401    call setline(1, ["\tabc"])
402    call feedkeys("A {\<cr>more\<cr>}\<esc>", 'tnix')
403    call assert_equal(["\tabc {", "\t\tmore", "\t}"], getline(1, '$'))
404    set smartindent& autoindent&
405    bw!
406  endif
407endfunc
408
409func! Test_edit_CR()
410  " Test for <CR> in insert mode
411  " basically only in quickfix mode ist tested, the rest
412  " has been taken care of by other tests
413  if !has("quickfix")
414    return
415  endif
416  botright new
417  call writefile(range(1, 10), 'Xqflist.txt')
418  call setqflist([{'filename': 'Xqflist.txt', 'lnum': 2}])
419  copen
420  set modifiable
421  call feedkeys("A\<cr>", 'tnix')
422  call assert_equal('Xqflist.txt', bufname(''))
423  call assert_equal(2, line('.'))
424  cclose
425  botright new
426  call setloclist(0, [{'filename': 'Xqflist.txt', 'lnum': 10}])
427  lopen
428  set modifiable
429  call feedkeys("A\<cr>", 'tnix')
430  call assert_equal('Xqflist.txt', bufname(''))
431  call assert_equal(10, line('.'))
432  call feedkeys("A\<Enter>", 'tnix')
433  call feedkeys("A\<kEnter>", 'tnix')
434  call feedkeys("A\n", 'tnix')
435  call feedkeys("A\r", 'tnix')
436  call assert_equal(map(range(1, 10), 'string(v:val)') + ['', '', '', ''], getline(1, '$'))
437  bw!
438  lclose
439  call delete('Xqflist.txt')
440endfunc
441
442func! Test_edit_CTRL_()
443  " disabled for Windows builds, why?
444  if !has("multi_byte") || !has("rightleft") || has("win32")
445    return
446  endif
447  let _encoding=&encoding
448  set encoding=utf-8
449  " Test for CTRL-_
450  new
451  call setline(1, ['abc'])
452  call cursor(1, 1)
453  call feedkeys("i\<c-_>xyz\<esc>", 'tnix')
454  call assert_equal(["\<C-_>xyzabc"], getline(1, '$'))
455  call assert_false(&revins)
456  set ari
457  call setline(1, ['abc'])
458  call cursor(1, 1)
459  call feedkeys("i\<c-_>xyz\<esc>", 'tnix')
460  call assert_equal(["æèñabc"], getline(1, '$'))
461  call assert_true(&revins)
462  call setline(1, ['abc'])
463  call cursor(1, 1)
464  call feedkeys("i\<c-_>xyz\<esc>", 'tnix')
465  call assert_equal(["xyzabc"], getline(1, '$'))
466  call assert_false(&revins)
467  set noari
468  let &encoding=_encoding
469  bw!
470endfunc
471
472" needs to come first, to have the @. register empty
473func! Test_edit_00a_CTRL_A()
474  " Test pressing CTRL-A
475  new
476  call setline(1, repeat([''], 5))
477  call cursor(1, 1)
478  set belloff=all
479  try
480    call feedkeys("A\<NUL>", 'tnix')
481  catch /^Vim\%((\a\+)\)\=:E29/
482    call assert_true(1, 'E29 error caught')
483  endtry
484  set belloff=
485  call cursor(1, 1)
486  call feedkeys("Afoobar \<esc>", 'tnix')
487  call cursor(2, 1)
488  call feedkeys("A\<c-a>more\<esc>", 'tnix')
489  call cursor(3, 1)
490  call feedkeys("A\<NUL>and more\<esc>", 'tnix')
491  call assert_equal(['foobar ', 'foobar more', 'foobar morend more', '', ''], getline(1, '$'))
492  bw!
493endfunc
494
495func! Test_edit_CTRL_EY()
496  " Ctrl-E/ Ctrl-Y in insert mode completion to scroll
497  10new
498  call setline(1, range(1, 100))
499  call cursor(30, 1)
500  norm! z.
501  call feedkeys("A\<c-x>\<c-e>\<c-e>\<c-e>\<c-e>\<c-e>", 'tnix')
502  call assert_equal(30, winsaveview()['topline'])
503  call assert_equal([0, 30, 2, 0], getpos('.'))
504  call feedkeys("A\<c-x>\<c-e>\<c-e>\<c-e>\<c-e>\<c-e>", 'tnix')
505  call feedkeys("A\<c-x>".repeat("\<c-y>", 10), 'tnix')
506  call assert_equal(21, winsaveview()['topline'])
507  call assert_equal([0, 30, 2, 0], getpos('.'))
508  bw!
509endfunc
510
511func! Test_edit_CTRL_G()
512  new
513  set belloff=all
514  call setline(1, ['foobar', 'foobar', 'foobar'])
515  call cursor(2, 4)
516  call feedkeys("ioooooooo\<c-g>k\<c-r>.\<esc>", 'tnix')
517  call assert_equal(['foooooooooobar', 'foooooooooobar', 'foobar'], getline(1, '$'))
518  call assert_equal([0, 1, 11, 0], getpos('.'))
519  call feedkeys("i\<c-g>k\<esc>", 'tnix')
520  call assert_equal([0, 1, 10, 0], getpos('.'))
521  call cursor(2, 4)
522  call feedkeys("i\<c-g>jzzzz\<esc>", 'tnix')
523  call assert_equal(['foooooooooobar', 'foooooooooobar', 'foozzzzbar'], getline(1, '$'))
524  call assert_equal([0, 3, 7, 0], getpos('.'))
525  call feedkeys("i\<c-g>j\<esc>", 'tnix')
526  call assert_equal([0, 3, 6, 0], getpos('.'))
527  set belloff=
528  bw!
529endfunc
530
531func! Test_edit_CTRL_I()
532  " Tab in completion mode
533  let path=expand("%:p:h")
534  new
535  call setline(1, [path."/", ''])
536  call feedkeys("Arunt\<c-x>\<c-f>\<tab>\<cr>\<esc>", 'tnix')
537  call assert_match('runtest\.vim', getline(1))
538  %d
539  call writefile(['one', 'two', 'three'], 'Xinclude.txt')
540  let include='#include Xinclude.txt'
541  call setline(1, [include, ''])
542  call cursor(2, 1)
543  call feedkeys("A\<c-x>\<tab>\<cr>\<esc>", 'tnix')
544  call assert_equal([include, 'one', ''], getline(1, '$'))
545  call feedkeys("2ggC\<c-x>\<tab>\<down>\<cr>\<esc>", 'tnix')
546  call assert_equal([include, 'two', ''], getline(1, '$'))
547  call feedkeys("2ggC\<c-x>\<tab>\<down>\<down>\<cr>\<esc>", 'tnix')
548  call assert_equal([include, 'three', ''], getline(1, '$'))
549  call feedkeys("2ggC\<c-x>\<tab>\<down>\<down>\<down>\<cr>\<esc>", 'tnix')
550  call assert_equal([include, '', ''], getline(1, '$'))
551  call delete("Xinclude.txt")
552  bw!
553endfunc
554
555func! Test_edit_CTRL_K()
556  " Test pressing CTRL-K (basically only dictionary completion and digraphs
557  " the rest is already covered
558  call writefile(['A', 'AA', 'AAA', 'AAAA'], 'Xdictionary.txt')
559  set dictionary=Xdictionary.txt
560  new
561  call setline(1, 'A')
562  call cursor(1, 1)
563  call feedkeys("A\<c-x>\<c-k>\<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>\<down>\<cr>\<esc>", 'tnix')
569  call assert_equal(['AAA'], getline(1, '$'))
570  %d
571  call setline(1, 'A')
572  call cursor(1, 1)
573  call feedkeys("A\<c-x>\<c-k>\<down>\<down>\<cr>\<esc>", 'tnix')
574  call assert_equal(['AAAA'], getline(1, '$'))
575  %d
576  call setline(1, 'A')
577  call cursor(1, 1)
578  call feedkeys("A\<c-x>\<c-k>\<down>\<down>\<down>\<cr>\<esc>", 'tnix')
579  call assert_equal(['A'], getline(1, '$'))
580  %d
581  call setline(1, 'A')
582  call cursor(1, 1)
583  call feedkeys("A\<c-x>\<c-k>\<down>\<down>\<down>\<down>\<cr>\<esc>", 'tnix')
584  call assert_equal(['AA'], getline(1, '$'))
585
586  " press an unexecpted key after dictionary completion
587  %d
588  call setline(1, 'A')
589  call cursor(1, 1)
590  call feedkeys("A\<c-x>\<c-k>\<c-]>\<cr>\<esc>", 'tnix')
591  call assert_equal(['AA', ''], getline(1, '$'))
592  %d
593  call setline(1, 'A')
594  call cursor(1, 1)
595  call feedkeys("A\<c-x>\<c-k>\<c-s>\<cr>\<esc>", 'tnix')
596  call assert_equal(["AA\<c-s>", ''], getline(1, '$'))
597  %d
598  call setline(1, 'A')
599  call cursor(1, 1)
600  call feedkeys("A\<c-x>\<c-k>\<c-f>\<cr>\<esc>", 'tnix')
601  call assert_equal(["AA\<c-f>", ''], getline(1, '$'))
602
603  set dictionary=
604  %d
605  call setline(1, 'A')
606  call cursor(1, 1)
607  set belloff=all
608  let v:testing = 1
609  try
610    call feedkeys("A\<c-x>\<c-k>\<esc>", 'tnix')
611  catch
612    " error sleeps 2 seconds, when v:testing is not set
613    let v:testing = 0
614  endtry
615  set belloff=
616  call delete('Xdictionary.txt')
617
618  if has("multi_byte")
619    call test_override("char_avail", 1)
620    set showcmd
621    %d
622    call feedkeys("A\<c-k>a:\<esc>", 'tnix')
623    call assert_equal(['ä'], getline(1, '$'))
624    call test_override("char_avail", 0)
625    set noshowcmd
626  endif
627  bw!
628endfunc
629
630func! Test_edit_CTRL_L()
631  " Test Ctrl-X Ctrl-L (line completion)
632  new
633  set complete=.
634  call setline(1, ['one', 'two', 'three', '', '', '', ''])
635  call cursor(4, 1)
636  call feedkeys("A\<c-x>\<c-l>\<esc>", 'tnix')
637  call assert_equal(['one', 'two', 'three', 'three', '', '', ''], getline(1, '$'))
638  call feedkeys("cct\<c-x>\<c-l>\<c-n>\<esc>", 'tnix')
639  call assert_equal(['one', 'two', 'three', 't', '', '', ''], getline(1, '$'))
640  call feedkeys("cct\<c-x>\<c-l>\<c-n>\<c-n>\<esc>", 'tnix')
641  call assert_equal(['one', 'two', 'three', 't', '', '', ''], getline(1, '$'))
642  call feedkeys("cct\<c-x>\<c-l>\<c-n>\<c-n>\<c-n>\<esc>", 'tnix')
643  call assert_equal(['one', 'two', 'three', 'two', '', '', ''], getline(1, '$'))
644  call feedkeys("cct\<c-x>\<c-l>\<c-n>\<c-n>\<c-n>\<c-n>\<esc>", 'tnix')
645  call assert_equal(['one', 'two', 'three', 'three', '', '', ''], getline(1, '$'))
646  call feedkeys("cct\<c-x>\<c-l>\<c-p>\<esc>", 'tnix')
647  call assert_equal(['one', 'two', 'three', 'two', '', '', ''], getline(1, '$'))
648  call feedkeys("cct\<c-x>\<c-l>\<c-p>\<c-p>\<esc>", 'tnix')
649  call assert_equal(['one', 'two', 'three', 't', '', '', ''], getline(1, '$'))
650  call feedkeys("cct\<c-x>\<c-l>\<c-p>\<c-p>\<c-p>\<esc>", 'tnix')
651  call assert_equal(['one', 'two', 'three', 'three', '', '', ''], getline(1, '$'))
652  set complete=
653  call cursor(5, 1)
654  call feedkeys("A\<c-x>\<c-l>\<c-p>\<c-n>\<esc>", 'tnix')
655  call assert_equal(['one', 'two', 'three', 'three', "\<c-l>\<c-p>\<c-n>", '', ''], getline(1, '$'))
656  set complete&
657  %d
658  if has("conceal") && has("syntax")
659    call setline(1, ['foo', 'bar', 'foobar'])
660    call test_override("char_avail", 1)
661    set conceallevel=2 concealcursor=n
662    syn on
663    syn match ErrorMsg "^bar"
664    call matchadd("Conceal", 'oo', 10, -1, {'conceal': 'X'})
665    func! DoIt()
666      let g:change=1
667    endfunc
668    au! TextChangedI <buffer> :call DoIt()
669
670    call cursor(2, 1)
671    call assert_false(exists("g:change"))
672    call feedkeys("A \<esc>", 'tnix')
673    call assert_equal(['foo', 'bar ', 'foobar'], getline(1, '$'))
674    call assert_equal(1, g:change)
675
676    call test_override("char_avail", 0)
677    call clearmatches()
678    syn off
679    au! TextChangedI
680    delfu DoIt
681    unlet! g:change
682  endif
683  bw!
684endfunc
685
686func! Test_edit_CTRL_N()
687  " Check keyword completion
688  new
689  set complete=.
690  call setline(1, ['INFER', 'loWER', '', '', ])
691  call cursor(3, 1)
692  call feedkeys("Ai\<c-n>\<cr>\<esc>", "tnix")
693  call feedkeys("ILO\<c-n>\<cr>\<esc>", 'tnix')
694  call assert_equal(['INFER', 'loWER', 'i', 'LO', '', ''], getline(1, '$'))
695  %d
696  call setline(1, ['INFER', 'loWER', '', '', ])
697  call cursor(3, 1)
698  set ignorecase infercase
699  call feedkeys("Ii\<c-n>\<cr>\<esc>", "tnix")
700  call feedkeys("ILO\<c-n>\<cr>\<esc>", 'tnix')
701  call assert_equal(['INFER', 'loWER', 'infer', 'LOWER', '', ''], getline(1, '$'))
702
703  set noignorecase noinfercase complete&
704  bw!
705endfunc
706
707func! Test_edit_CTRL_O()
708  " Check for CTRL-O in insert mode
709  new
710  inoreabbr <buffer> h here some more
711  call setline(1, ['abc', 'def'])
712  call cursor(1, 1)
713  " Ctrl-O after an abbreviation
714  exe "norm A h\<c-o>:set nu\<cr> text"
715  call assert_equal(['abc here some more text', 'def'], getline(1, '$'))
716  call assert_true(&nu)
717  set nonu
718  iunabbr <buffer> h
719  " Ctrl-O at end of line with 've'=onemore
720  call cursor(1, 1)
721  call feedkeys("A\<c-o>:let g:a=getpos('.')\<cr>\<esc>", 'tnix')
722  call assert_equal([0, 1, 23, 0], g:a)
723  call cursor(1, 1)
724  set ve=onemore
725  call feedkeys("A\<c-o>:let g:a=getpos('.')\<cr>\<esc>", 'tnix')
726  call assert_equal([0, 1, 24, 0], g:a)
727  set ve=
728  unlet! g:a
729  bw!
730endfunc
731
732func! Test_edit_CTRL_R()
733  " Insert Register
734  new
735  call test_override("ALL", 1)
736  set showcmd
737  call feedkeys("AFOOBAR eins zwei\<esc>", 'tnix')
738  call feedkeys("O\<c-r>.", 'tnix')
739  call feedkeys("O\<c-r>=10*500\<cr>\<esc>", 'tnix')
740  call feedkeys("O\<c-r>=getreg('=', 1)\<cr>\<esc>", 'tnix')
741  call assert_equal(["getreg('=', 1)", '5000', "FOOBAR eins zwei", "FOOBAR eins zwei"], getline(1, '$'))
742  call test_override("ALL", 0)
743  set noshowcmd
744  bw!
745endfunc
746
747func! Test_edit_CTRL_S()
748  " Test pressing CTRL-S (basically only spellfile completion)
749  " the rest is already covered
750  new
751  if !has("spell")
752    call setline(1, 'vim')
753    call feedkeys("A\<c-x>ss\<cr>\<esc>", 'tnix')
754    call assert_equal(['vims', ''], getline(1, '$'))
755    bw!
756    return
757  endif
758  call setline(1, 'vim')
759  " spell option not yet set
760  try
761    call feedkeys("A\<c-x>\<c-s>\<cr>\<esc>", 'tnix')
762  catch /^Vim\%((\a\+)\)\=:E756/
763    call assert_true(1, 'error caught')
764  endtry
765  call assert_equal(['vim', ''], getline(1, '$'))
766  %d
767  setl spell spelllang=en
768  call setline(1, 'vim')
769  call cursor(1, 1)
770  call feedkeys("A\<c-x>\<c-s>\<cr>\<esc>", 'tnix')
771  call assert_equal(['Vim', ''], getline(1, '$'))
772  %d
773  call setline(1, 'vim')
774  call cursor(1, 1)
775  call feedkeys("A\<c-x>\<c-s>\<down>\<cr>\<esc>", 'tnix')
776  call assert_equal(['Aim'], getline(1, '$'))
777  %d
778  call setline(1, 'vim')
779  call cursor(1, 1)
780  call feedkeys("A\<c-x>\<c-s>\<c-p>\<cr>\<esc>", 'tnix')
781  call assert_equal(['vim', ''], getline(1, '$'))
782  %d
783  " empty buffer
784  call cursor(1, 1)
785  call feedkeys("A\<c-x>\<c-s>\<c-p>\<cr>\<esc>", 'tnix')
786  call assert_equal(['', ''], getline(1, '$'))
787  setl nospell
788  bw!
789endfunc
790
791func! Test_edit_CTRL_T()
792  " Check for CTRL-T and CTRL-X CTRL-T in insert mode
793  " 1) increase indent
794  new
795  call setline(1, "abc")
796  call cursor(1, 1)
797  call feedkeys("A\<c-t>xyz", 'tnix')
798  call assert_equal(["\<tab>abcxyz"], getline(1, '$'))
799  " 2) also when paste option is set
800  set paste
801  call setline(1, "abc")
802  call cursor(1, 1)
803  call feedkeys("A\<c-t>xyz", 'tnix')
804  call assert_equal(["\<tab>abcxyz"], getline(1, '$'))
805  set nopaste
806  " CTRL-X CTRL-T (thesaurus complete)
807  call writefile(['angry furious mad enraged'], 'Xthesaurus')
808  set thesaurus=Xthesaurus
809  call setline(1, 'mad')
810  call cursor(1, 1)
811  call feedkeys("A\<c-x>\<c-t>\<cr>\<esc>", 'tnix')
812  call assert_equal(['mad', ''], getline(1, '$'))
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-n>\<c-n>\<cr>\<esc>", 'tnix')
822  call assert_equal(['furious', ''], getline(1, '$'))
823  %d
824  call setline(1, 'mad')
825  call cursor(1, 1)
826  call feedkeys("A\<c-x>\<c-t>\<c-n>\<c-n>\<c-n>\<cr>\<esc>", 'tnix')
827  call assert_equal(['enraged', ''], getline(1, '$'))
828  %d
829  call setline(1, 'mad')
830  call cursor(1, 1)
831  call feedkeys("A\<c-x>\<c-t>\<c-n>\<c-n>\<c-n>\<c-n>\<cr>\<esc>", 'tnix')
832  call assert_equal(['mad', ''], getline(1, '$'))
833  %d
834  call setline(1, 'mad')
835  call cursor(1, 1)
836  call feedkeys("A\<c-x>\<c-t>\<c-n>\<c-n>\<c-n>\<c-n>\<c-n>\<cr>\<esc>", 'tnix')
837  call assert_equal(['mad', ''], getline(1, '$'))
838  " Using <c-p> <c-n> when 'complete' is empty
839  set complete=
840  %d
841  call setline(1, 'mad')
842  call cursor(1, 1)
843  call feedkeys("A\<c-x>\<c-t>\<c-n>\<cr>\<esc>", 'tnix')
844  call assert_equal(['angry', ''], getline(1, '$'))
845  %d
846  call setline(1, 'mad')
847  call cursor(1, 1)
848  call feedkeys("A\<c-x>\<c-t>\<c-p>\<cr>\<esc>", 'tnix')
849  call assert_equal(['mad', ''], getline(1, '$'))
850  set complete&
851
852  set thesaurus=
853  %d
854  call setline(1, 'mad')
855  call cursor(1, 1)
856  set belloff=all
857  let v:testing = 1
858  try
859    call feedkeys("A\<c-x>\<c-t>\<esc>", 'tnix')
860  catch
861    " error sleeps 2 seconds, when v:testing is not set
862    let v:testing = 0
863  endtry
864  set belloff=
865  call assert_equal(['mad'], getline(1, '$'))
866  call delete('Xthesaurus')
867  bw!
868endfunc
869
870func! Test_edit_CTRL_U()
871  " Test 'completefunc'
872  new
873  " -1, -2 and -3 are special return values
874  let g:special=0
875  fun! CompleteMonths(findstart, base)
876    if a:findstart
877      " locate the start of the word
878      return g:special
879    else
880      " find months matching with "a:base"
881      let res = []
882      for m in split("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec")
883        if m =~ '^\c'.a:base
884          call add(res, {'word': m, 'abbr': m.' Month', 'icase': 0})
885        endif
886      endfor
887      return {'words': res, 'refresh': 'always'}
888    endif
889  endfun
890  set completefunc=CompleteMonths
891  call setline(1, ['', ''])
892  call cursor(1, 1)
893  call feedkeys("AX\<c-x>\<c-u>\<cr>\<esc>", 'tnix')
894  call assert_equal(['X', '', ''], getline(1, '$'))
895  %d
896  let g:special=-1
897  call feedkeys("AX\<c-x>\<c-u>\<cr>\<esc>", 'tnix')
898  call assert_equal(['XJan', ''], getline(1, '$'))
899  %d
900  let g:special=-2
901  call feedkeys("AX\<c-x>\<c-u>\<cr>\<esc>", 'tnix')
902  call assert_equal(['X', ''], getline(1, '$'))
903  %d
904  let g:special=-3
905  call feedkeys("AX\<c-x>\<c-u>\<cr>\<esc>", 'tnix')
906  call assert_equal(['X', ''], getline(1, '$'))
907  %d
908  let g:special=0
909  call feedkeys("AM\<c-x>\<c-u>\<cr>\<esc>", 'tnix')
910  call assert_equal(['Mar', ''], getline(1, '$'))
911  %d
912  call feedkeys("AM\<c-x>\<c-u>\<c-n>\<cr>\<esc>", 'tnix')
913  call assert_equal(['May', ''], getline(1, '$'))
914  %d
915  call feedkeys("AM\<c-x>\<c-u>\<c-n>\<c-n>\<cr>\<esc>", 'tnix')
916  call assert_equal(['M', ''], getline(1, '$'))
917  delfu CompleteMonths
918  %d
919  try
920    call feedkeys("A\<c-x>\<c-u>", 'tnix')
921    call assert_fails(1, 'unknown completion function')
922  catch /^Vim\%((\a\+)\)\=:E117/
923    call assert_true(1, 'E117 error caught')
924  endtry
925  set completefunc=
926  bw!
927endfunc
928
929func! Test_edit_CTRL_Z()
930  " Ctrl-Z when insertmode is not set inserts it literally
931  new
932  call setline(1, 'abc')
933  call feedkeys("A\<c-z>\<esc>", 'tnix')
934  call assert_equal(["abc\<c-z>"], getline(1,'$'))
935  bw!
936  " TODO: How to Test Ctrl-Z in insert mode, e.g. suspend?
937endfunc
938
939func! Test_edit_DROP()
940  if !has("dnd")
941    return
942  endif
943  new
944  call setline(1, ['abc def ghi'])
945  call cursor(1, 1)
946  try
947    call feedkeys("i\<Drop>\<Esc>", 'tnix')
948    call assert_fails(1, 'Invalid register name')
949  catch /^Vim\%((\a\+)\)\=:E353/
950    call assert_true(1, 'error caught')
951  endtry
952  bw!
953endfunc
954
955func! Test_edit_CTRL_V()
956  if has("ebcdic")
957    return
958  endif
959  new
960  call setline(1, ['abc'])
961  call cursor(2, 1)
962  " force some redraws
963  set showmode showcmd
964  "call test_override_char_avail(1)
965  call test_override('ALL', 1)
966  call feedkeys("A\<c-v>\<c-n>\<c-v>\<c-l>\<c-v>\<c-b>\<esc>", 'tnix')
967  call assert_equal(["abc\x0e\x0c\x02"], getline(1, '$'))
968
969  if has("rightleft") && exists("+rl")
970    set rl
971    call setline(1, ['abc'])
972    call cursor(2, 1)
973    call feedkeys("A\<c-v>\<c-n>\<c-v>\<c-l>\<c-v>\<c-b>\<esc>", 'tnix')
974    call assert_equal(["abc\x0e\x0c\x02"], getline(1, '$'))
975    set norl
976  endif
977
978  call test_override('ALL', 0)
979  set noshowmode showcmd
980  bw!
981endfunc
982
983func! Test_edit_F1()
984  " Pressing <f1>
985  new
986  call feedkeys(":set im\<cr>\<f1>\<c-l>", 'tnix')
987  set noinsertmode
988  call assert_equal('help', &buftype)
989  bw
990  bw
991endfunc
992
993func! Test_edit_F21()
994  " Pressing <f21>
995  " sends a netbeans command
996  if has("netbeans_intg")
997    new
998    " I have no idea what this is supposed to do :)
999    call feedkeys("A\<F21>\<F1>\<esc>", 'tnix')
1000    bw
1001  endif
1002endfunc
1003
1004func! Test_edit_HOME_END()
1005  " Test Home/End Keys
1006  new
1007  set foldopen+=hor
1008  call setline(1, ['abc', 'def'])
1009  call cursor(1, 1)
1010  call feedkeys("AX\<Home>Y\<esc>", 'tnix')
1011  call cursor(2, 1)
1012  call feedkeys("iZ\<End>Y\<esc>", 'tnix')
1013  call assert_equal(['YabcX', 'ZdefY'], getline(1, '$'))
1014
1015  set foldopen-=hor
1016  bw!
1017endfunc
1018
1019func! Test_edit_INS()
1020  " Test for Pressing <Insert>
1021  new
1022  call setline(1, ['abc', 'def'])
1023  call cursor(1, 1)
1024  call feedkeys("i\<Insert>ZYX>", 'tnix')
1025  call assert_equal(['ZYX>', 'def'], getline(1, '$'))
1026  call setline(1, ['abc', 'def'])
1027  call cursor(1, 1)
1028  call feedkeys("i\<Insert>Z\<Insert>YX>", 'tnix')
1029  call assert_equal(['ZYX>bc', 'def'], getline(1, '$'))
1030  bw!
1031endfunc
1032
1033func! Test_edit_LEFT_RIGHT()
1034  " Left, Shift-Left, Right, Shift-Right
1035  new
1036  set belloff=all
1037  call setline(1, ['abc def ghi', 'ABC DEF GHI', 'ZZZ YYY XXX'])
1038  let _ww=&ww
1039  set ww=
1040  call cursor(2, 1)
1041  call feedkeys("i\<left>\<esc>", 'tnix')
1042  call assert_equal([0, 2, 1, 0], getpos('.'))
1043  " Is this a bug, <s-left> does not respect whichwrap option
1044  call feedkeys("i\<s-left>\<esc>", 'tnix')
1045  call assert_equal([0, 1, 8, 0], getpos('.'))
1046  call feedkeys("i". repeat("\<s-left>", 3). "\<esc>", 'tnix')
1047  call assert_equal([0, 1, 1, 0], getpos('.'))
1048  call feedkeys("i\<right>\<esc>", 'tnix')
1049  call assert_equal([0, 1, 1, 0], getpos('.'))
1050  call feedkeys("i\<right>\<right>\<esc>", 'tnix')
1051  call assert_equal([0, 1, 2, 0], getpos('.'))
1052  call feedkeys("A\<right>\<esc>", 'tnix')
1053  call assert_equal([0, 1, 11, 0], getpos('.'))
1054  call feedkeys("A\<s-right>\<esc>", 'tnix')
1055  call assert_equal([0, 2, 1, 0], getpos('.'))
1056  call feedkeys("i\<s-right>\<esc>", 'tnix')
1057  call assert_equal([0, 2, 4, 0], getpos('.'))
1058  call cursor(3, 11)
1059  call feedkeys("A\<right>\<esc>", 'tnix')
1060  call feedkeys("A\<s-right>\<esc>", 'tnix')
1061  call assert_equal([0, 3, 11, 0], getpos('.'))
1062  call cursor(2, 11)
1063  " <S-Right> does not respect 'whichwrap' option
1064  call feedkeys("A\<s-right>\<esc>", 'tnix')
1065  call assert_equal([0, 3, 1, 0], getpos('.'))
1066  " Check motion when 'whichwrap' contains cursor keys for insert mode
1067  set ww+=[,]
1068  call cursor(2, 1)
1069  call feedkeys("i\<left>\<esc>", 'tnix')
1070  call assert_equal([0, 1, 11, 0], getpos('.'))
1071  call cursor(2, 11)
1072  call feedkeys("A\<right>\<esc>", 'tnix')
1073  call assert_equal([0, 3, 1, 0], getpos('.'))
1074  call cursor(2, 11)
1075  call feedkeys("A\<s-right>\<esc>", 'tnix')
1076  call assert_equal([0, 3, 1, 0], getpos('.'))
1077  let &ww = _ww
1078  set belloff=
1079  bw!
1080endfunc
1081
1082func! Test_edit_MOUSE()
1083  " This is a simple test, since we not really using the mouse here
1084  if !has("mouse")
1085    return
1086  endif
1087  10new
1088  call setline(1, range(1, 100))
1089  call cursor(1, 1)
1090  set mouse=a
1091  call feedkeys("A\<ScrollWheelDown>\<esc>", 'tnix')
1092  call assert_equal([0, 4, 1, 0], getpos('.'))
1093  " This should move by one pageDown, but only moves
1094  " by one line when the test is run...
1095  call feedkeys("A\<S-ScrollWheelDown>\<esc>", 'tnix')
1096  call assert_equal([0, 5, 1, 0], getpos('.'))
1097  set nostartofline
1098  call feedkeys("A\<C-ScrollWheelDown>\<esc>", 'tnix')
1099  call assert_equal([0, 6, 1, 0], getpos('.'))
1100  call feedkeys("A\<LeftMouse>\<esc>", 'tnix')
1101  call assert_equal([0, 6, 1, 0], getpos('.'))
1102  call feedkeys("A\<RightMouse>\<esc>", 'tnix')
1103  call assert_equal([0, 6, 1, 0], getpos('.'))
1104  call cursor(1, 100)
1105  norm! zt
1106  " this should move by a screen up, but when the test
1107  " is run, it moves up to the top of the buffer...
1108  call feedkeys("A\<ScrollWheelUp>\<esc>", 'tnix')
1109  call assert_equal([0, 1, 1, 0], getpos('.'))
1110  call cursor(1, 30)
1111  norm! zt
1112  call feedkeys("A\<S-ScrollWheelUp>\<esc>", 'tnix')
1113  call assert_equal([0, 1, 1, 0], getpos('.'))
1114  call cursor(1, 30)
1115  norm! zt
1116  call feedkeys("A\<C-ScrollWheelUp>\<esc>", 'tnix')
1117  call assert_equal([0, 1, 1, 0], getpos('.'))
1118  %d
1119  call setline(1, repeat(["12345678901234567890"], 100))
1120  call cursor(2, 1)
1121  call feedkeys("A\<ScrollWheelRight>\<esc>", 'tnix')
1122  call assert_equal([0, 2, 20, 0], getpos('.'))
1123  call feedkeys("A\<ScrollWheelLeft>\<esc>", 'tnix')
1124  call assert_equal([0, 2, 20, 0], getpos('.'))
1125  call feedkeys("A\<S-ScrollWheelRight>\<esc>", 'tnix')
1126  call assert_equal([0, 2, 20, 0], getpos('.'))
1127  call feedkeys("A\<S-ScrollWheelLeft>\<esc>", 'tnix')
1128  call assert_equal([0, 2, 20, 0], getpos('.'))
1129  call feedkeys("A\<C-ScrollWheelRight>\<esc>", 'tnix')
1130  call assert_equal([0, 2, 20, 0], getpos('.'))
1131  call feedkeys("A\<C-ScrollWheelLeft>\<esc>", 'tnix')
1132  call assert_equal([0, 2, 20, 0], getpos('.'))
1133  set mouse& startofline
1134  bw!
1135endfunc
1136
1137func! Test_edit_PAGEUP_PAGEDOWN()
1138  set belloff=all
1139  10new
1140  call setline(1, repeat(['abc def ghi'], 30))
1141  call cursor(1, 1)
1142  call feedkeys("i\<PageDown>\<esc>", 'tnix')
1143  call assert_equal([0, 9, 1, 0], getpos('.'))
1144  call feedkeys("i\<PageDown>\<esc>", 'tnix')
1145  call assert_equal([0, 17, 1, 0], getpos('.'))
1146  call feedkeys("i\<PageDown>\<esc>", 'tnix')
1147  call assert_equal([0, 25, 1, 0], getpos('.'))
1148  call feedkeys("i\<PageDown>\<esc>", 'tnix')
1149  call assert_equal([0, 30, 1, 0], getpos('.'))
1150  call feedkeys("i\<PageDown>\<esc>", 'tnix')
1151  call assert_equal([0, 30, 1, 0], getpos('.'))
1152  call feedkeys("A\<PageUp>\<esc>", 'tnix')
1153  call assert_equal([0, 29, 1, 0], getpos('.'))
1154  call feedkeys("A\<PageUp>\<esc>", 'tnix')
1155  call assert_equal([0, 21, 1, 0], getpos('.'))
1156  call feedkeys("A\<PageUp>\<esc>", 'tnix')
1157  call assert_equal([0, 13, 1, 0], getpos('.'))
1158  call feedkeys("A\<PageUp>\<esc>", 'tnix')
1159  call assert_equal([0, 5, 1, 0], getpos('.'))
1160  call feedkeys("A\<PageUp>\<esc>", 'tnix')
1161  call assert_equal([0, 5, 11, 0], getpos('.'))
1162  " <S-Up> is the same as <PageUp>
1163  " <S-Down> is the same as <PageDown>
1164  call cursor(1, 1)
1165  call feedkeys("i\<S-Down>\<esc>", 'tnix')
1166  call assert_equal([0, 9, 1, 0], getpos('.'))
1167  call feedkeys("i\<S-Down>\<esc>", 'tnix')
1168  call assert_equal([0, 17, 1, 0], getpos('.'))
1169  call feedkeys("i\<S-Down>\<esc>", 'tnix')
1170  call assert_equal([0, 25, 1, 0], getpos('.'))
1171  call feedkeys("i\<S-Down>\<esc>", 'tnix')
1172  call assert_equal([0, 30, 1, 0], getpos('.'))
1173  call feedkeys("i\<S-Down>\<esc>", 'tnix')
1174  call assert_equal([0, 30, 1, 0], getpos('.'))
1175  call feedkeys("A\<S-Up>\<esc>", 'tnix')
1176  call assert_equal([0, 29, 1, 0], getpos('.'))
1177  call feedkeys("A\<S-Up>\<esc>", 'tnix')
1178  call assert_equal([0, 21, 1, 0], getpos('.'))
1179  call feedkeys("A\<S-Up>\<esc>", 'tnix')
1180  call assert_equal([0, 13, 1, 0], getpos('.'))
1181  call feedkeys("A\<S-Up>\<esc>", 'tnix')
1182  call assert_equal([0, 5, 1, 0], getpos('.'))
1183  call feedkeys("A\<S-Up>\<esc>", 'tnix')
1184  call assert_equal([0, 5, 11, 0], getpos('.'))
1185  set nostartofline
1186  call cursor(30, 11)
1187  norm! zt
1188  call feedkeys("A\<PageUp>\<esc>", 'tnix')
1189  call assert_equal([0, 29, 11, 0], getpos('.'))
1190  call feedkeys("A\<PageUp>\<esc>", 'tnix')
1191  call assert_equal([0, 21, 11, 0], getpos('.'))
1192  call feedkeys("A\<PageUp>\<esc>", 'tnix')
1193  call assert_equal([0, 13, 11, 0], getpos('.'))
1194  call feedkeys("A\<PageUp>\<esc>", 'tnix')
1195  call assert_equal([0, 5, 11, 0], getpos('.'))
1196  call feedkeys("A\<PageUp>\<esc>", 'tnix')
1197  call assert_equal([0, 5, 11, 0], getpos('.'))
1198  call cursor(1, 1)
1199  call feedkeys("A\<PageDown>\<esc>", 'tnix')
1200  call assert_equal([0, 9, 11, 0], getpos('.'))
1201  call feedkeys("A\<PageDown>\<esc>", 'tnix')
1202  call assert_equal([0, 17, 11, 0], getpos('.'))
1203  call feedkeys("A\<PageDown>\<esc>", 'tnix')
1204  call assert_equal([0, 25, 11, 0], getpos('.'))
1205  call feedkeys("A\<PageDown>\<esc>", 'tnix')
1206  call assert_equal([0, 30, 11, 0], getpos('.'))
1207  call feedkeys("A\<PageDown>\<esc>", 'tnix')
1208  call assert_equal([0, 30, 11, 0], getpos('.'))
1209  " <S-Up> is the same as <PageUp>
1210  " <S-Down> is the same as <PageDown>
1211  call cursor(30, 11)
1212  norm! zt
1213  call feedkeys("A\<S-Up>\<esc>", 'tnix')
1214  call assert_equal([0, 29, 11, 0], getpos('.'))
1215  call feedkeys("A\<S-Up>\<esc>", 'tnix')
1216  call assert_equal([0, 21, 11, 0], getpos('.'))
1217  call feedkeys("A\<S-Up>\<esc>", 'tnix')
1218  call assert_equal([0, 13, 11, 0], getpos('.'))
1219  call feedkeys("A\<S-Up>\<esc>", 'tnix')
1220  call assert_equal([0, 5, 11, 0], getpos('.'))
1221  call feedkeys("A\<S-Up>\<esc>", 'tnix')
1222  call assert_equal([0, 5, 11, 0], getpos('.'))
1223  call cursor(1, 1)
1224  call feedkeys("A\<S-Down>\<esc>", 'tnix')
1225  call assert_equal([0, 9, 11, 0], getpos('.'))
1226  call feedkeys("A\<S-Down>\<esc>", 'tnix')
1227  call assert_equal([0, 17, 11, 0], getpos('.'))
1228  call feedkeys("A\<S-Down>\<esc>", 'tnix')
1229  call assert_equal([0, 25, 11, 0], getpos('.'))
1230  call feedkeys("A\<S-Down>\<esc>", 'tnix')
1231  call assert_equal([0, 30, 11, 0], getpos('.'))
1232  call feedkeys("A\<S-Down>\<esc>", 'tnix')
1233  call assert_equal([0, 30, 11, 0], getpos('.'))
1234  set startofline belloff=
1235  bw!
1236endfunc
1237
1238func! Test_edit_forbidden()
1239  set belloff=error,esc
1240  new
1241  " 1) edit in the sandbox is not allowed
1242  call setline(1, 'a')
1243  com! Sandbox :sandbox call feedkeys("i\<del>\<esc>", 'tnix')
1244  call assert_fails(':Sandbox', 'E48:')
1245  com! Sandbox :sandbox exe "norm! i\<del>"
1246  call assert_fails(':Sandbox', 'E48:')
1247  delcom Sandbox
1248  call assert_equal(['a'], getline(1,'$'))
1249  " 2) edit with textlock set
1250  fu! DoIt()
1251    call feedkeys("i\<del>\<esc>", 'tnix')
1252  endfu
1253  au InsertCharPre <buffer> :call DoIt()
1254  try
1255    call feedkeys("ix\<esc>", 'tnix')
1256    call assert_fails(1, 'textlock')
1257  catch /^Vim\%((\a\+)\)\=:E523/ " catch E523: not allowed here
1258  endtry
1259  " TODO: Might be a bug: should x really be inserted here
1260  call assert_equal(['xa'], getline(1, '$'))
1261  delfu DoIt
1262  try
1263    call feedkeys("ix\<esc>", 'tnix')
1264    call assert_fails(1, 'unknown function')
1265  catch /^Vim\%((\a\+)\)\=:E117/ " catch E117: unknown function
1266  endtry
1267  au! InsertCharPre
1268  " 3) edit when completion is shown
1269  fun! Complete(findstart, base)
1270    if a:findstart
1271      return col('.')
1272    else
1273      call feedkeys("i\<del>\<esc>", 'tnix')
1274      return []
1275    endif
1276  endfun
1277  set completefunc=Complete
1278  try
1279    call feedkeys("i\<c-x>\<c-u>\<esc>", 'tnix')
1280    call assert_fails(1, 'change in complete function')
1281  catch /^Vim\%((\a\+)\)\=:E523/ " catch E523
1282  endtry
1283  delfu Complete
1284  set completefunc=
1285  if has("rightleft") && exists("+fkmap")
1286    " 4) 'R' when 'fkmap' and 'revins' is set.
1287    set revins fkmap
1288    try
1289      normal Ri
1290      call assert_fails(1, "R with 'fkmap' and 'ri' set")
1291    catch
1292    finally
1293      set norevins nofkmap
1294    endtry
1295  endif
1296  set belloff=
1297  bw!
1298endfunc
1299
1300func! Test_edit_rightleft()
1301  " Cursor in rightleft mode moves differently
1302  if !exists("+rightleft")
1303    return
1304  endif
1305  call NewWindow(10, 20)
1306  call setline(1, ['abc', 'def', 'ghi'])
1307  call cursor(1, 2)
1308  set rightleft
1309  " Screen looks as expected
1310  let lines = ScreenLines([1, 4], winwidth(0))
1311  let expect = [
1312        \"                 cba",
1313        \"                 fed",
1314        \"                 ihg",
1315        \"                   ~"]
1316  call assert_equal(join(expect, "\n"), join(lines, "\n"))
1317  " 2) right moves to the left
1318  call feedkeys("i\<right>\<esc>x", 'txin')
1319  call assert_equal(['bc', 'def', 'ghi'], getline(1,'$'))
1320  call cursor(1, 2)
1321  call feedkeys("i\<s-right>\<esc>", 'txin')
1322  call cursor(1, 2)
1323  call feedkeys("i\<c-right>\<esc>", 'txin')
1324  " Screen looks as expected
1325  let lines = ScreenLines([1, 4], winwidth(0))
1326  let expect = [
1327        \"                  cb",
1328        \"                 fed",
1329        \"                 ihg",
1330        \"                   ~"]
1331  call assert_equal(join(expect, "\n"), join(lines, "\n"))
1332  " 2) left moves to the right
1333  call setline(1, ['abc', 'def', 'ghi'])
1334  call cursor(1, 2)
1335  call feedkeys("i\<left>\<esc>x", 'txin')
1336  call assert_equal(['ac', 'def', 'ghi'], getline(1,'$'))
1337  call cursor(1, 2)
1338  call feedkeys("i\<s-left>\<esc>", 'txin')
1339  call cursor(1, 2)
1340  call feedkeys("i\<c-left>\<esc>", 'txin')
1341  " Screen looks as expected
1342  let lines = ScreenLines([1, 4], winwidth(0))
1343  let expect = [
1344        \"                  ca",
1345        \"                 fed",
1346        \"                 ihg",
1347        \"                   ~"]
1348  call assert_equal(join(expect, "\n"), join(lines, "\n"))
1349  set norightleft
1350  bw!
1351endfunc
1352
1353func Test_edit_complete_very_long_name()
1354  if !has('unix')
1355    " Long directory names only work on Unix.
1356    return
1357  endif
1358
1359  let dirname = getcwd() . "/Xdir"
1360  let longdirname = dirname . repeat('/' . repeat('d', 255), 4)
1361  try
1362    call mkdir(longdirname, 'p')
1363  catch /E739:/
1364    " Long directory name probably not supported.
1365    call delete(dirname, 'rf')
1366    return
1367  endtry
1368
1369  " Try to get the Vim window position before setting 'columns'.
1370  let winposx = getwinposx()
1371  let winposy = getwinposy()
1372  let save_columns = &columns
1373  " Need at least about 1100 columns to reproduce the problem.
1374  set columns=2000
1375  call assert_equal(2000, &columns)
1376  set noswapfile
1377
1378  let longfilename = longdirname . '/' . repeat('a', 255)
1379  call writefile(['Totum', 'Table'], longfilename)
1380  new
1381  exe "next Xfile " . longfilename
1382  exe "normal iT\<C-N>"
1383
1384  bwipe!
1385  exe 'bwipe! ' . longfilename
1386  call delete(dirname, 'rf')
1387  let &columns = save_columns
1388  if winposx >= 0 && winposy >= 0
1389    exe 'winpos ' . winposx . ' ' . winposy
1390  endif
1391  set swapfile&
1392endfunc
1393