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