robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

robinmerge: 1dc9728daa1d8a2702

master
75.3 KiB2601 linesraw
1" Test for edit functions
2
3if exists("+t_kD")
4  let &t_kD="[3;*~"
5endif
6
7source check.vim
8source screendump.vim
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  " <esc> expands the abbreviation and ends insert mode
19  " call feedkeys(":set im\<cr> h\<c-l>:set noim\<cr>", 'tix')
20  call feedkeys("i h\<esc>", '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\<C-Y>\<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  throw 'skipped: moved to test/functional/legacy/edit_spec.lua'
218  " reset insertmode from i_ctrl-r_=
219  let g:bufnr = bufnr('%')
220  new
221  call setline(1, ['abc'])
222  call cursor(1, 4)
223  call feedkeys(":set im\<cr>ZZZ\<c-r>=setbufvar(g:bufnr,'&im', 0)\<cr>",'tnix')
224  call assert_equal(['abZZZc'], getline(1,'$'))
225  call assert_equal([0, 1, 1, 0], getpos('.'))
226  call assert_false(0, '&im')
227  bw!
228  unlet g:bufnr
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 feedkeys("i\<c-\>\<c-n>ccABC\<esc>", 'txin')
239  call assert_equal(['ABC', 'def', 'ghi'], getline(1,'$'))
240  call setline(1, ['ABC', 'def', 'ghi'])
241  " 2) CTRL-\ CTLR-G
242  " CTRL-\ CTRL-G goes to Insert mode when 'insertmode' is set, but
243  " 'insertmode' is now removed so skip this test
244  " call feedkeys("j0\<c-\>\<c-g>ZZZ\<cr>\<esc>", 'txin')
245  " call assert_equal(['ABC', 'ZZZ', 'def', 'ghi'], getline(1,'$'))
246  " call feedkeys("I\<c-\>\<c-g>YYY\<c-l>", 'txin')
247  " call assert_equal(['ABC', 'ZZZ', 'YYYdef', 'ghi'], getline(1,'$'))
248  " set noinsertmode
249  " 3) CTRL-\ CTRL-O
250  call setline(1, ['ABC', 'ZZZ', 'def', 'ghi'])
251  call cursor(1, 1)
252  call feedkeys("A\<c-o>ix", 'txin')
253  call assert_equal(['ABxC', 'ZZZ', 'def', 'ghi'], getline(1,'$'))
254  call feedkeys("A\<c-\>\<c-o>ix", 'txin')
255  call assert_equal(['ABxCx', 'ZZZ', 'def', 'ghi'], getline(1,'$'))
256  " 4) CTRL-\ a (should be inserted literally, not special after <c-\>
257  call setline(1, ['ABC', 'ZZZ', 'def', 'ghi'])
258  call cursor(1, 1)
259  call feedkeys("A\<c-\>a", 'txin')
260  call assert_equal(["ABC\<c-\>a", 'ZZZ', 'def', 'ghi'], getline(1, '$'))
261  bw!
262endfunc
263
264func Test_edit_11()
265  " Test that indenting kicks in
266  new
267  set cindent
268  call setline(1, ['{', '', ''])
269  call cursor(2, 1)
270  call feedkeys("i\<c-f>int c;\<esc>", 'tnix')
271  call cursor(3, 1)
272  call feedkeys("\<Insert>/* comment */", 'tnix')
273  call assert_equal(['{', "\<tab>int c;", "/* comment */"], getline(1, '$'))
274  " added changed cindentkeys slightly
275  set cindent cinkeys+=*/
276  call setline(1, ['{', '', ''])
277  call cursor(2, 1)
278  call feedkeys("i\<c-f>int c;\<esc>", 'tnix')
279  call cursor(3, 1)
280  call feedkeys("i/* comment */", 'tnix')
281  call assert_equal(['{', "\<tab>int c;", "\<tab>/* comment */"], getline(1, '$'))
282  set cindent cinkeys+==end
283  call feedkeys("oend\<cr>\<esc>", 'tnix')
284  call assert_equal(['{', "\<tab>int c;", "\<tab>/* comment */", "\tend", ''], getline(1, '$'))
285  set cinkeys-==end
286  %d
287  " Use indentexpr instead of cindenting
288  func! Do_Indent()
289    if v:lnum == 3
290      return 3*shiftwidth()
291    else
292      return 2*shiftwidth()
293    endif
294  endfunc
295  setl indentexpr=Do_Indent() indentkeys+=*/
296  call setline(1, ['{', '', ''])
297  call cursor(2, 1)
298  call feedkeys("i\<c-f>int c;\<esc>", 'tnix')
299  call cursor(3, 1)
300  call feedkeys("i/* comment */", 'tnix')
301  call assert_equal(['{', "\<tab>\<tab>int c;", "\<tab>\<tab>\<tab>/* comment */"], getline(1, '$'))
302  set cinkeys&vim indentkeys&vim
303  set nocindent indentexpr=
304  delfu Do_Indent
305  bw!
306endfunc
307
308func Test_edit_11_indentexpr()
309  " Test that indenting kicks in
310  new
311  " Use indentexpr instead of cindenting
312  func! Do_Indent()
313    let pline=prevnonblank(v:lnum)
314    if empty(getline(v:lnum))
315      if getline(pline) =~ 'if\|then'
316        return shiftwidth()
317      else
318        return 0
319      endif
320    else
321        return 0
322    endif
323  endfunc
324  setl indentexpr=Do_Indent() indentkeys+=0=then,0=fi
325  call setline(1, ['if [ $this ]'])
326  call cursor(1, 1)
327  call feedkeys("othen\<cr>that\<cr>fi", 'tnix')
328  call assert_equal(['if [ $this ]', "then", "\<tab>that", "fi"], getline(1, '$'))
329  set cinkeys&vim indentkeys&vim
330  set nocindent indentexpr=
331  delfu Do_Indent
332
333  " Using a script-local function
334  func s:NewIndentExpr()
335  endfunc
336  set indentexpr=s:NewIndentExpr()
337  call assert_equal(expand('<SID>') .. 'NewIndentExpr()', &indentexpr)
338  call assert_equal(expand('<SID>') .. 'NewIndentExpr()', &g:indentexpr)
339  set indentexpr=<SID>NewIndentExpr()
340  call assert_equal(expand('<SID>') .. 'NewIndentExpr()', &indentexpr)
341  call assert_equal(expand('<SID>') .. 'NewIndentExpr()', &g:indentexpr)
342  setlocal indentexpr=
343  setglobal indentexpr=s:NewIndentExpr()
344  call assert_equal(expand('<SID>') .. 'NewIndentExpr()', &g:indentexpr)
345  call assert_equal('', &indentexpr)
346  new
347  call assert_equal(expand('<SID>') .. 'NewIndentExpr()', &indentexpr)
348  bw!
349  setglobal indentexpr=<SID>NewIndentExpr()
350  call assert_equal(expand('<SID>') .. 'NewIndentExpr()', &g:indentexpr)
351  call assert_equal('', &indentexpr)
352  new
353  call assert_equal(expand('<SID>') .. 'NewIndentExpr()', &indentexpr)
354  bw!
355  set indentexpr&
356
357  bw!
358endfunc
359
360" Test changing indent in replace mode
361func Test_edit_12()
362  new
363  call setline(1, ["\tabc", "\tdef"])
364  call cursor(2, 4)
365  call feedkeys("R^\<c-d>", 'tnix')
366  call assert_equal(["\tabc", "def"], getline(1, '$'))
367  call assert_equal([0, 2, 2, 0], '.'->getpos())
368  %d
369  call setline(1, ["\tabc", "\t\tdef"])
370  call cursor(2, 2)
371  call feedkeys("R^\<c-d>", 'tnix')
372  call assert_equal(["\tabc", "def"], getline(1, '$'))
373  call assert_equal([0, 2, 1, 0], getpos('.'))
374  %d
375  call setline(1, ["\tabc", "\t\tdef"])
376  call cursor(2, 2)
377  call feedkeys("R\<c-t>", 'tnix')
378  call assert_equal(["\tabc", "\t\t\tdef"], getline(1, '$'))
379  call assert_equal([0, 2, 2, 0], getpos('.'))
380  bw!
381  10vnew
382  call setline(1, ["\tabc", "\t\tdef"])
383  call cursor(2, 2)
384  call feedkeys("R\<c-t>", 'tnix')
385  call assert_equal(["\tabc", "\t\t\tdef"], getline(1, '$'))
386  call assert_equal([0, 2, 2, 0], getpos('.'))
387  %d
388  set sw=4
389  call setline(1, ["\tabc", "\t\tdef"])
390  call cursor(2, 2)
391  call feedkeys("R\<c-t>\<c-t>", 'tnix')
392  call assert_equal(["\tabc", "\t\t\tdef"], getline(1, '$'))
393  call assert_equal([0, 2, 2, 0], getpos('.'))
394  %d
395  call setline(1, ["\tabc", "\t\tdef"])
396  call cursor(2, 2)
397  call feedkeys("R\<c-t>\<c-t>", 'tnix')
398  call assert_equal(["\tabc", "\t\t\tdef"], getline(1, '$'))
399  call assert_equal([0, 2, 2, 0], getpos('.'))
400  set sw&
401
402  " In replace mode, after hitting enter in a line with tab characters,
403  " pressing backspace should restore the tab characters.
404  %d
405  setlocal autoindent backspace=2
406  call setline(1, "\tone\t\ttwo")
407  exe "normal ggRred\<CR>six" .. repeat("\<BS>", 8)
408  call assert_equal(["\tone\t\ttwo"], getline(1, '$'))
409  bw!
410endfunc
411
412func Test_edit_13()
413  " Test smartindenting
414  new
415  set smartindent autoindent
416  call setline(1, ["\tabc"])
417  call feedkeys("A {\<cr>more\<cr>}\<esc>", 'tnix')
418  call assert_equal(["\tabc {", "\t\tmore", "\t}"], getline(1, '$'))
419  set smartindent& autoindent&
420  bwipe!
421
422  " Test autoindent removing indent of blank line.
423  new
424  call setline(1, '    foo bar baz')
425  set autoindent
426  exe "normal 0eea\<CR>\<CR>\<Esc>"
427  call assert_equal("    foo bar", getline(1))
428  call assert_equal("", getline(2))
429  call assert_equal("    baz", getline(3))
430  set autoindent&
431
432  " pressing <C-U> to erase line should keep the indent with 'autoindent'
433  set backspace=2 autoindent
434  %d
435  exe "normal i\tone\<CR>three\<C-U>two"
436  call assert_equal(["\tone", "\ttwo"], getline(1, '$'))
437  set backspace& autoindent&
438
439  bwipe!
440endfunc
441
442" Test for autoindent removing indent when insert mode is stopped.  Some parts
443" of the code is exercised only when interactive mode is used. So use Vim in a
444" terminal.
445func Test_autoindent_remove_indent()
446  CheckRunVimInTerminal
447  let buf = RunVimInTerminal('-N Xarifile', {'rows': 6, 'cols' : 20})
448  call TermWait(buf)
449  call term_sendkeys(buf, ":set autoindent\n")
450  " leaving insert mode in a new line with indent added by autoindent, should
451  " remove the indent.
452  call term_sendkeys(buf, "i\<Tab>foo\<CR>\<Esc>")
453  " Need to delay for some time, otherwise the code in getchar.c will not be
454  " exercised.
455  call TermWait(buf, 50)
456  " when a line is wrapped and the cursor is at the start of the second line,
457  " leaving insert mode, should move the cursor back to the first line.
458  call term_sendkeys(buf, "o" .. repeat('x', 20) .. "\<Esc>")
459  " Need to delay for some time, otherwise the code in getchar.c will not be
460  " exercised.
461  call TermWait(buf, 50)
462  call term_sendkeys(buf, ":w\n")
463  call TermWait(buf)
464  call StopVimInTerminal(buf)
465  call assert_equal(["\tfoo", '', repeat('x', 20)], readfile('Xarifile'))
466  call delete('Xarifile')
467endfunc
468
469" Test for autoindent removing indent when insert mode is stopped and
470" autocomplete is enabled.  Some parts of the code is exercised only when
471" interactive mode is used. So use Vim in a terminal.
472func Test_autoindent_remove_indent_with_autocomplete()
473  CheckRunVimInTerminal
474  let buf = RunVimInTerminal('-N Xarifile', {'rows': 6, 'cols' : 20})
475  call TermWait(buf)
476  call term_sendkeys(buf, ":set autoindent autocomplete\n")
477  " leaving insert mode in a new line with indent added by autoindent, should
478  " remove the indent.
479  call term_sendkeys(buf, "i\<Tab>foo\<CR>\<Esc>")
480  " Need to delay for some time, otherwise the code in getchar.c will not be
481  " exercised.
482  call TermWait(buf, 50)
483  " when a line is wrapped and the cursor is at the start of the second line,
484  " leaving insert mode, should move the cursor back to the first line.
485  call term_sendkeys(buf, "o" .. repeat('x', 20) .. "\<Esc>")
486  " Need to delay for some time, otherwise the code in getchar.c will not be
487  " exercised.
488  call TermWait(buf, 50)
489  call term_sendkeys(buf, ":w\n")
490  call TermWait(buf)
491  call StopVimInTerminal(buf)
492  call assert_equal(["\tfoo", '', repeat('x', 20)], readfile('Xarifile'))
493  call delete('Xarifile')
494endfunc
495
496func Test_edit_esc_after_CR_autoindent()
497  new
498  setlocal autoindent
499  autocmd InsertLeavePre * let g:prev_cursor = getpos('.')
500
501  call setline(1, 'foobar')
502  exe "normal! $hi\<CR>\<Esc>"
503  call assert_equal(['foob', 'ar'], getline(1, '$'))
504  call assert_equal([0, 2, 1, 0], getpos('.'))
505  call assert_equal([0, 2, 1, 0], getpos("'^"))
506  call assert_equal([0, 2, 1, 0], g:prev_cursor)
507  %d
508
509  call setline(1, 'foobar')
510  exe "normal! $i\<CR>\<Esc>"
511  call assert_equal(['fooba', 'r'], getline(1, '$'))
512  call assert_equal([0, 2, 1, 0], getpos('.'))
513  call assert_equal([0, 2, 1, 0], getpos("'^"))
514  call assert_equal([0, 2, 1, 0], g:prev_cursor)
515  %d
516
517  call setline(1, 'foobar')
518  exe "normal! A\<CR>\<Esc>"
519  call assert_equal(['foobar', ''], getline(1, '$'))
520  call assert_equal([0, 2, 1, 0], getpos('.'))
521  call assert_equal([0, 2, 1, 0], getpos("'^"))
522  call assert_equal([0, 2, 1, 0], g:prev_cursor)
523  %d
524
525  call setline(1, '  foobar')
526  exe "normal! $hi\<CR>\<Esc>"
527  call assert_equal(['  foob', '  ar'], getline(1, '$'))
528  call assert_equal([0, 2, 2, 0], getpos('.'))
529  call assert_equal([0, 2, 3, 0], getpos("'^"))
530  call assert_equal([0, 2, 3, 0], g:prev_cursor)
531  %d
532
533  call setline(1, '  foobar')
534  exe "normal! $i\<CR>\<Esc>"
535  call assert_equal(['  fooba', '  r'], getline(1, '$'))
536  call assert_equal([0, 2, 2, 0], getpos('.'))
537  call assert_equal([0, 2, 3, 0], getpos("'^"))
538  call assert_equal([0, 2, 3, 0], g:prev_cursor)
539  %d
540
541  call setline(1, '  foobar')
542  exe "normal! A\<CR>\<Esc>"
543  call assert_equal(['  foobar', ''], getline(1, '$'))
544  call assert_equal([0, 2, 1, 0], getpos('.'))
545  call assert_equal([0, 2, 1, 0], getpos("'^"))
546  call assert_equal([0, 2, 1, 0], g:prev_cursor)
547  %d
548
549  autocmd! InsertLeavePre
550  unlet g:prev_cursor
551  bwipe!
552endfunc
553
554func Test_edit_CR()
555  " Test for <CR> in insert mode
556  " basically only in quickfix mode it's tested, the rest
557  " has been taken care of by other tests
558  CheckFeature quickfix
559  botright new
560  call writefile(range(1, 10), 'Xqflist.txt', 'D')
561  call setqflist([{'filename': 'Xqflist.txt', 'lnum': 2}])
562  copen
563  set modifiable
564  call feedkeys("A\<cr>", 'tnix')
565  call assert_equal('Xqflist.txt', bufname(''))
566  call assert_equal(2, line('.'))
567  cclose
568  botright new
569  call setloclist(0, [{'filename': 'Xqflist.txt', 'lnum': 10}])
570  lopen
571  set modifiable
572  call feedkeys("A\<cr>", 'tnix')
573  call assert_equal('Xqflist.txt', bufname(''))
574  call assert_equal(10, line('.'))
575  call feedkeys("A\<Enter>", 'tnix')
576  call feedkeys("A\<kEnter>", 'tnix')
577  call feedkeys("A\n", 'tnix')
578  call feedkeys("A\r", 'tnix')
579  call assert_equal(map(range(1, 10), 'string(v:val)') + ['', '', '', ''], getline(1, '$'))
580
581  bw!
582  lclose
583endfunc
584
585func Test_edit_CTRL_()
586  CheckFeature rightleft
587  " disabled for Windows builds, why?
588  CheckNotMSWindows
589  let _encoding=&encoding
590  set encoding=utf-8
591  " Test for CTRL-_
592  new
593  call setline(1, ['abc'])
594  call cursor(1, 1)
595  call feedkeys("i\<c-_>xyz\<esc>", 'tnix')
596  call assert_equal(["\<C-_>xyzabc"], getline(1, '$'))
597  call assert_false(&revins)
598  set ari
599  call setline(1, ['abc'])
600  call cursor(1, 1)
601  call feedkeys("i\<c-_>xyz\<esc>", 'tnix')
602  " call assert_equal(["æèñabc"], getline(1, '$'))
603  call assert_equal(["zyxabc"], getline(1, '$'))
604  call assert_true(&revins)
605  call setline(1, ['abc'])
606  call cursor(1, 1)
607  call feedkeys("i\<c-_>xyz\<esc>", 'tnix')
608  call assert_equal(["xyzabc"], getline(1, '$'))
609  call assert_false(&revins)
610  set noari
611  let &encoding=_encoding
612  bw!
613endfunc
614
615" needs to come first, to have the @. register empty
616func Test_edit_00a_CTRL_A()
617  " Test pressing CTRL-A
618  new
619  call setline(1, repeat([''], 5))
620  call cursor(1, 1)
621  try
622    call feedkeys("A\<NUL>", 'tnix')
623  catch /^Vim\%((\a\+)\)\=:E29/
624    call assert_true(1, 'E29 error caught')
625  endtry
626  call cursor(1, 1)
627  call feedkeys("Afoobar \<esc>", 'tnix')
628  call cursor(2, 1)
629  call feedkeys("A\<c-a>more\<esc>", 'tnix')
630  call cursor(3, 1)
631  call feedkeys("A\<NUL>and more\<esc>", 'tnix')
632  call assert_equal(['foobar ', 'foobar more', 'foobar morend more', '', ''], getline(1, '$'))
633  bw!
634endfunc
635
636func Test_edit_CTRL_EY()
637  " Ctrl-E/ Ctrl-Y in insert mode completion to scroll
638  10new
639  call setline(1, range(1, 100))
640  call cursor(30, 1)
641  norm! z.
642  call feedkeys("A\<c-x>\<c-e>\<c-e>\<c-e>\<c-e>\<c-e>", 'tnix')
643  call assert_equal(30, winsaveview()['topline'])
644  call assert_equal([0, 30, 2, 0], getpos('.'))
645  call feedkeys("A\<c-x>\<c-e>\<c-e>\<c-e>\<c-e>\<c-e>", 'tnix')
646  call feedkeys("A\<c-x>".repeat("\<c-y>", 10), 'tnix')
647  call assert_equal(21, winsaveview()['topline'])
648  call assert_equal([0, 30, 2, 0], getpos('.'))
649  bw!
650endfunc
651
652func Test_edit_CTRL_G()
653  new
654  call setline(1, ['foobar', 'foobar', 'foobar'])
655  call cursor(2, 4)
656  call feedkeys("ioooooooo\<c-g>k\<c-r>.\<esc>", 'tnix')
657  call assert_equal(['foooooooooobar', 'foooooooooobar', 'foobar'], getline(1, '$'))
658  call assert_equal([0, 1, 11, 0], getpos('.'))
659  call feedkeys("i\<c-g>k\<esc>", 'tnix')
660  call assert_equal([0, 1, 10, 0], getpos('.'))
661  call cursor(2, 4)
662  call feedkeys("i\<c-g>jzzzz\<esc>", 'tnix')
663  call assert_equal(['foooooooooobar', 'foooooooooobar', 'foozzzzbar'], getline(1, '$'))
664  call assert_equal([0, 3, 7, 0], getpos('.'))
665  call feedkeys("i\<c-g>j\<esc>", 'tnix')
666  call assert_equal([0, 3, 6, 0], getpos('.'))
667  call assert_nobeep("normal! i\<c-g>\<esc>")
668  bw!
669endfunc
670
671func Test_edit_CTRL_I()
672  " Tab in completion mode
673  let path=expand("%:p:h")
674  new
675  call setline(1, [path. "/", ''])
676  call feedkeys("Arunt\<c-x>\<c-f>\<tab>\<cr>\<esc>", 'tnix')
677  call assert_match('runtest\.vim', getline(1))
678  %d
679  call writefile(['one', 'two', 'three'], 'Xinclude.txt', 'D')
680  let include='#include Xinclude.txt'
681  call setline(1, [include, ''])
682  call cursor(2, 1)
683  call feedkeys("A\<c-x>\<tab>\<cr>\<esc>", 'tnix')
684  call assert_equal([include, 'one', ''], getline(1, '$'))
685  call feedkeys("2ggC\<c-x>\<tab>\<down>\<cr>\<esc>", 'tnix')
686  call assert_equal([include, 'two', ''], getline(1, '$'))
687  call feedkeys("2ggC\<c-x>\<tab>\<down>\<down>\<cr>\<esc>", 'tnix')
688  call assert_equal([include, 'three', ''], getline(1, '$'))
689  call feedkeys("2ggC\<c-x>\<tab>\<down>\<down>\<down>\<cr>\<esc>", 'tnix')
690  call assert_equal([include, '', ''], getline(1, '$'))
691  bw!
692endfunc
693
694func Test_edit_CTRL_K()
695  " Test pressing CTRL-K (basically only dictionary completion and digraphs
696  " the rest is already covered
697  call writefile(['A', 'AA', 'AAA', 'AAAA'], 'Xdictionary.txt', 'D')
698  set dictionary=Xdictionary.txt
699  new
700  call setline(1, 'A')
701  call cursor(1, 1)
702  call feedkeys("A\<c-x>\<c-k>\<cr>\<esc>", 'tnix')
703  call assert_equal(['AA', ''], getline(1, '$'))
704  %d
705  call setline(1, 'A')
706  call cursor(1, 1)
707  call feedkeys("A\<c-x>\<c-k>\<down>\<cr>\<esc>", 'tnix')
708  call assert_equal(['AAA'], getline(1, '$'))
709  %d
710  call setline(1, 'A')
711  call cursor(1, 1)
712  call feedkeys("A\<c-x>\<c-k>\<down>\<down>\<cr>\<esc>", 'tnix')
713  call assert_equal(['AAAA'], getline(1, '$'))
714  %d
715  call setline(1, 'A')
716  call cursor(1, 1)
717  call feedkeys("A\<c-x>\<c-k>\<down>\<down>\<down>\<cr>\<esc>", 'tnix')
718  call assert_equal(['A'], getline(1, '$'))
719  %d
720  call setline(1, 'A')
721  call cursor(1, 1)
722  call feedkeys("A\<c-x>\<c-k>\<down>\<down>\<down>\<down>\<cr>\<esc>", 'tnix')
723  call assert_equal(['AA'], getline(1, '$'))
724
725  " press an unexpected key after dictionary completion
726  %d
727  call setline(1, 'A')
728  call cursor(1, 1)
729  call feedkeys("A\<c-x>\<c-k>\<c-]>\<cr>\<esc>", 'tnix')
730  call assert_equal(['AA', ''], getline(1, '$'))
731  %d
732  call setline(1, 'A')
733  call cursor(1, 1)
734  call feedkeys("A\<c-x>\<c-k>\<c-s>\<cr>\<esc>", 'tnix')
735  call assert_equal(["AA\<c-s>", ''], getline(1, '$'))
736  %d
737  call setline(1, 'A')
738  call cursor(1, 1)
739  call feedkeys("A\<c-x>\<c-k>\<c-f>\<cr>\<esc>", 'tnix')
740  call assert_equal(["AA\<c-f>", ''], getline(1, '$'))
741
742  set dictionary=
743  %d
744  call setline(1, 'A')
745  call cursor(1, 1)
746  try
747    call feedkeys("A\<c-x>\<c-k>\<esc>", 'tnix')
748  catch
749  endtry
750
751  call Ntest_override("char_avail", 1)
752  set showcmd
753  %d
754  call feedkeys("A\<c-k>a:\<esc>", 'tnix')
755  call assert_equal(['ä'], getline(1, '$'))
756  call Ntest_override("char_avail", 0)
757  set noshowcmd
758
759  bw!
760endfunc
761
762func Test_edit_CTRL_L()
763  " Test Ctrl-X Ctrl-L (line completion)
764  new
765  set complete=.
766  call setline(1, ['one', 'two', 'three', '', '', '', ''])
767  call cursor(4, 1)
768  call feedkeys("A\<c-x>\<c-l>\<esc>", 'tnix')
769  call assert_equal(['one', 'two', 'three', 'three', '', '', ''], getline(1, '$'))
770  call feedkeys("cct\<c-x>\<c-l>\<c-n>\<esc>", 'tnix')
771  call assert_equal(['one', 'two', 'three', 't', '', '', ''], getline(1, '$'))
772  call feedkeys("cct\<c-x>\<c-l>\<c-n>\<c-n>\<esc>", 'tnix')
773  call assert_equal(['one', 'two', 'three', 'two', '', '', ''], getline(1, '$'))
774  call feedkeys("cct\<c-x>\<c-l>\<c-n>\<c-n>\<c-n>\<esc>", 'tnix')
775  call assert_equal(['one', 'two', 'three', 'three', '', '', ''], getline(1, '$'))
776  call feedkeys("cct\<c-x>\<c-l>\<c-n>\<c-n>\<c-n>\<c-n>\<esc>", 'tnix')
777  call assert_equal(['one', 'two', 'three', 't', '', '', ''], getline(1, '$'))
778  call feedkeys("cct\<c-x>\<c-l>\<c-p>\<esc>", 'tnix')
779  call assert_equal(['one', 'two', 'three', 'two', '', '', ''], getline(1, '$'))
780  call feedkeys("cct\<c-x>\<c-l>\<c-p>\<c-p>\<esc>", 'tnix')
781  call assert_equal(['one', 'two', 'three', 't', '', '', ''], getline(1, '$'))
782  call feedkeys("cct\<c-x>\<c-l>\<c-p>\<c-p>\<c-p>\<esc>", 'tnix')
783  call assert_equal(['one', 'two', 'three', 'three', '', '', ''], getline(1, '$'))
784  set complete=
785  call cursor(5, 1)
786  call feedkeys("A\<c-x>\<c-l>\<c-p>\<c-n>\<esc>", 'tnix')
787  call assert_equal(['one', 'two', 'three', 'three', "\<c-l>\<c-p>\<c-n>", '', ''], getline(1, '$'))
788  set complete&
789  %d
790  if has("conceal") && has("syntax")
791    call setline(1, ['foo', 'bar', 'foobar'])
792    call Ntest_override("char_avail", 1)
793    set conceallevel=2 concealcursor=n
794    syn on
795    syn match ErrorMsg "^bar"
796    call matchadd("Conceal", 'oo', 10, -1, {'conceal': 'X'})
797    func! DoIt()
798      let g:change=1
799    endfunc
800    au! TextChangedI <buffer> :call DoIt()
801
802    call cursor(2, 1)
803    call assert_false(exists("g:change"))
804    call feedkeys("A \<esc>", 'tnix')
805    call assert_equal(['foo', 'bar ', 'foobar'], getline(1, '$'))
806    call assert_equal(1, g:change)
807
808    call Ntest_override("char_avail", 0)
809    call clearmatches()
810    syn off
811    au! TextChangedI
812    delfu DoIt
813    unlet! g:change
814  endif
815  bw!
816endfunc
817
818func Test_edit_CTRL_N()
819  " Check keyword completion
820  " for e in ['latin1', 'utf-8']
821  for e in ['utf-8']
822    exe 'set encoding=' .. e
823    new
824    set complete=.
825    call setline(1, ['INFER', 'loWER', '', '', ])
826    call cursor(3, 1)
827    call feedkeys("Ai\<c-n>\<cr>\<esc>", "tnix")
828    call feedkeys("ILO\<c-n>\<cr>\<esc>", 'tnix')
829    call assert_equal(['INFER', 'loWER', 'i', 'LO', '', ''], getline(1, '$'), e)
830    %d
831    call setline(1, ['INFER', 'loWER', '', '', ])
832    call cursor(3, 1)
833    set ignorecase infercase
834    call feedkeys("Ii\<c-n>\<cr>\<esc>", "tnix")
835    call feedkeys("ILO\<c-n>\<cr>\<esc>", 'tnix')
836    call assert_equal(['INFER', 'loWER', 'infer', 'LOWER', '', ''], getline(1, '$'), e)
837    set noignorecase noinfercase
838    %d
839    call setline(1, ['one word', 'two word'])
840    exe "normal! Goo\<C-P>\<C-X>\<C-P>"
841    call assert_equal('one word', getline(3))
842    %d
843    set complete&
844    bw!
845  endfor
846endfunc
847
848func Test_edit_CTRL_O()
849  " Check for CTRL-O in insert mode
850  new
851  inoreabbr <buffer> h here some more
852  call setline(1, ['abc', 'def'])
853  call cursor(1, 1)
854  " Ctrl-O after an abbreviation
855  exe "norm A h\<c-o>:set nu\<cr> text"
856  call assert_equal(['abc here some more text', 'def'], getline(1, '$'))
857  call assert_true(&nu)
858  set nonu
859  iunabbr <buffer> h
860  " Ctrl-O at end of line with 've'=onemore
861  call cursor(1, 1)
862  call feedkeys("A\<c-o>:let g:a=getpos('.')\<cr>\<esc>", 'tnix')
863  call assert_equal([0, 1, 23, 0], g:a)
864  call cursor(1, 1)
865  set ve=onemore
866  call feedkeys("A\<c-o>:let g:a=getpos('.')\<cr>\<esc>", 'tnix')
867  call assert_equal([0, 1, 24, 0], g:a)
868  set ve=
869  unlet! g:a
870  bw!
871endfunc
872
873func Test_edit_CTRL_R()
874  " Insert Register
875  new
876  " call test_override("ALL", 1)
877  set showcmd
878  call feedkeys("AFOOBAR eins zwei\<esc>", 'tnix')
879  call feedkeys("O\<c-r>.", 'tnix')
880  call feedkeys("O\<c-r>=10*500\<cr>\<esc>", 'tnix')
881  call feedkeys("O\<c-r>=getreg('=', 1)\<cr>\<esc>", 'tnix')
882  call assert_equal(["getreg('=', 1)", '5000', "FOOBAR eins zwei", "FOOBAR eins zwei"], getline(1, '$'))
883  " call test_override("ALL", 0)
884  set noshowcmd
885  bw!
886endfunc
887
888func Test_edit_CTRL_S()
889  " Test pressing CTRL-S (basically only spellfile completion)
890  " the rest is already covered
891  new
892  if !has("spell")
893    call setline(1, 'vim')
894    call feedkeys("A\<c-x>ss\<cr>\<esc>", 'tnix')
895    call assert_equal(['vims', ''], getline(1, '$'))
896    bw!
897    return
898  endif
899  call setline(1, 'vim')
900  " spell option not yet set
901  try
902    call feedkeys("A\<c-x>\<c-s>\<cr>\<esc>", 'tnix')
903  catch /^Vim\%((\a\+)\)\=:E756/
904    call assert_true(1, 'error caught')
905  endtry
906  call assert_equal(['vim', ''], getline(1, '$'))
907  %d
908  setl spell spelllang=en
909  call setline(1, 'vim')
910  call cursor(1, 1)
911  call feedkeys("A\<c-x>\<c-s>\<cr>\<esc>", 'tnix')
912  call assert_equal(['Vim', ''], getline(1, '$'))
913  %d
914  call setline(1, 'vim')
915  call cursor(1, 1)
916  call feedkeys("A\<c-x>\<c-s>\<down>\<cr>\<esc>", 'tnix')
917  call assert_equal(['Aim'], getline(1, '$'))
918  %d
919  call setline(1, 'vim')
920  call cursor(1, 1)
921  call feedkeys("A\<c-x>\<c-s>\<c-p>\<cr>\<esc>", 'tnix')
922  call assert_equal(['vim', ''], getline(1, '$'))
923  %d
924  " empty buffer
925  call cursor(1, 1)
926  call feedkeys("A\<c-x>\<c-s>\<c-p>\<cr>\<esc>", 'tnix')
927  call assert_equal(['', ''], getline(1, '$'))
928  setl nospell
929  bw!
930endfunc
931
932func Edit_CTRL_T()
933  " Check for CTRL-T and CTRL-X CTRL-T in insert mode
934  " 1) increase indent
935  new
936  call setline(1, "abc")
937  call cursor(1, 1)
938  call feedkeys("A\<c-t>xyz", 'tnix')
939  call assert_equal(["\<tab>abcxyz"], getline(1, '$'))
940  " 2) also when paste option is set
941  set paste
942  call setline(1, "abc")
943  call cursor(1, 1)
944  call feedkeys("A\<c-t>xyz", 'tnix')
945  call assert_equal(["\<tab>abcxyz"], getline(1, '$'))
946  set nopaste
947  " CTRL-X CTRL-T (thesaurus complete)
948  call writefile(['angry furious mad enraged'], 'Xthesaurus', 'D')
949  set thesaurus=Xthesaurus
950  call setline(1, 'mad')
951  call cursor(1, 1)
952  call feedkeys("A\<c-x>\<c-t>\<cr>\<esc>", 'tnix')
953  call assert_equal(['mad', ''], getline(1, '$'))
954  %d
955  call setline(1, 'mad')
956  call cursor(1, 1)
957  call feedkeys("A\<c-x>\<c-t>\<c-n>\<cr>\<esc>", 'tnix')
958  call assert_equal(['angry', ''], getline(1, '$'))
959  %d
960  call setline(1, 'mad')
961  call cursor(1, 1)
962  call feedkeys("A\<c-x>\<c-t>\<c-n>\<c-n>\<cr>\<esc>", 'tnix')
963  call assert_equal(['furious', ''], getline(1, '$'))
964  %d
965  call setline(1, 'mad')
966  call cursor(1, 1)
967  call feedkeys("A\<c-x>\<c-t>\<c-n>\<c-n>\<c-n>\<cr>\<esc>", 'tnix')
968  call assert_equal(['enraged', ''], getline(1, '$'))
969  %d
970  call setline(1, 'mad')
971  call cursor(1, 1)
972  call feedkeys("A\<c-x>\<c-t>\<c-n>\<c-n>\<c-n>\<c-n>\<cr>\<esc>", 'tnix')
973  call assert_equal(['mad', ''], getline(1, '$'))
974  %d
975  call setline(1, 'mad')
976  call cursor(1, 1)
977  call feedkeys("A\<c-x>\<c-t>\<c-n>\<c-n>\<c-n>\<c-n>\<c-n>\<cr>\<esc>", 'tnix')
978  call assert_equal(['mad', ''], getline(1, '$'))
979  " Using <c-p> <c-n> when 'complete' is empty
980  set complete=
981  %d
982  call setline(1, 'mad')
983  call cursor(1, 1)
984  call feedkeys("A\<c-x>\<c-t>\<c-n>\<cr>\<esc>", 'tnix')
985  call assert_equal(['angry', ''], getline(1, '$'))
986  %d
987  call setline(1, 'mad')
988  call cursor(1, 1)
989  call feedkeys("A\<c-x>\<c-t>\<c-p>\<cr>\<esc>", 'tnix')
990  call assert_equal(['mad', ''], getline(1, '$'))
991  set complete&
992
993  set thesaurus=
994  %d
995  call setline(1, 'mad')
996  call cursor(1, 1)
997  try
998    call feedkeys("A\<c-x>\<c-t>\<esc>", 'tnix')
999  catch
1000  endtry
1001  call assert_equal(['mad'], getline(1, '$'))
1002  bw!
1003endfunc
1004
1005func Test_edit_CTRL_T()
1006  call Edit_CTRL_T()
1007  set completeopt+=fuzzy
1008  call Edit_CTRL_T()
1009  set completeopt&
1010endfunc
1011
1012func Test_edit_CTRL_T_longest()
1013  " CTRL-X CTRL-T (thesaurus complete) with 'longest' should not insert
1014  " longest match
1015  set completeopt+=longest
1016  new
1017  call writefile(['angry furious mad madder maddest'], 'Xthesaurus', 'D')
1018  set thesaurus=Xthesaurus
1019  call setline(1, 'mad')
1020  call cursor(1, 1)
1021  call feedkeys("A\<c-x>\<c-t>\<cr>\<esc>", 'tnix')
1022  call assert_equal(['mad', ''], getline(1, '$'))
1023  bw!
1024  set thesaurus=
1025  set completeopt&
1026endfunc
1027
1028" Test thesaurus completion with different encodings
1029func Test_thesaurus_complete_with_encoding()
1030  call writefile(['angry furious mad enraged'], 'Xthesaurus', 'D')
1031  set thesaurus=Xthesaurus
1032  " for e in ['latin1', 'utf-8']
1033  for e in ['utf-8']
1034    exe 'set encoding=' .. e
1035    new
1036    call setline(1, 'mad')
1037    call cursor(1, 1)
1038    call feedkeys("A\<c-x>\<c-t>\<cr>\<esc>", 'tnix')
1039    call assert_equal(['mad', ''], getline(1, '$'))
1040    bw!
1041  endfor
1042  set thesaurus=
1043endfunc
1044
1045" Test 'thesaurusfunc'
1046func MyThesaurus(findstart, base)
1047  let mythesaurus = [
1048        \ #{word: "happy",
1049        \   synonyms: "cheerful,blissful,flying high,looking good,peppy"},
1050        \ #{word: "kind",
1051        \   synonyms: "amiable,bleeding-heart,heart in right place"}]
1052  if a:findstart
1053    " locate the start of the word
1054    let line = getline('.')
1055    let start = col('.') - 1
1056    while start > 0 && line[start - 1] =~ '\a'
1057      let start -= 1
1058    endwhile
1059    return start
1060  else
1061    " find strings matching with "a:base"
1062    let res = []
1063    for w in mythesaurus
1064      if w.word =~ '^' . a:base
1065        call add(res, w.word)
1066        call extend(res, split(w.synonyms, ","))
1067      endif
1068    endfor
1069    return res
1070  endif
1071endfunc
1072
1073func Test_thesaurus_func()
1074  new
1075  set thesaurus=notused
1076  set thesaurusfunc=NotUsed
1077  setlocal thesaurusfunc=MyThesaurus
1078  call setline(1, "an ki")
1079  call cursor(1, 1)
1080  call feedkeys("A\<c-x>\<c-t>\<c-n>\<cr>\<esc>", 'tnix')
1081  call assert_equal(['an amiable', ''], getline(1, '$'))
1082
1083  setlocal thesaurusfunc=NonExistingFunc
1084  call assert_fails("normal $a\<C-X>\<C-T>", 'E117:')
1085
1086  setlocal thesaurusfunc=
1087  set thesaurusfunc=NonExistingFunc
1088  call assert_fails("normal $a\<C-X>\<C-T>", 'E117:')
1089  %bw!
1090
1091  set thesaurusfunc=
1092  set thesaurus=
1093endfunc
1094
1095func Test_edit_CTRL_U()
1096  " Test 'completefunc'
1097  new
1098  " -1, -2 and -3 are special return values
1099  let g:special=0
1100  fun! CompleteMonths(findstart, base)
1101    if a:findstart
1102      " locate the start of the word
1103      return g:special
1104    else
1105      " find months matching with "a:base"
1106      let res = []
1107      for m in split("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec")
1108        if m =~ '^\c'.a:base
1109          call add(res, {'word': m, 'abbr': m.' Month', 'icase': 0})
1110        endif
1111      endfor
1112      return {'words': res, 'refresh': 'always'}
1113    endif
1114  endfun
1115  set completefunc=CompleteMonths
1116  call setline(1, ['', ''])
1117  call cursor(1, 1)
1118  call feedkeys("AX\<c-x>\<c-u>\<cr>\<esc>", 'tnix')
1119  call assert_equal(['X', '', ''], getline(1, '$'))
1120  %d
1121  let g:special=-1
1122  call feedkeys("AX\<c-x>\<c-u>\<cr>\<esc>", 'tnix')
1123  call assert_equal(['XJan', ''], getline(1, '$'))
1124  %d
1125  let g:special=-2
1126  call feedkeys("AX\<c-x>\<c-u>\<cr>\<esc>", 'tnix')
1127  call assert_equal(['X', ''], getline(1, '$'))
1128  %d
1129  let g:special=-3
1130  call feedkeys("AX\<c-x>\<c-u>\<cr>\<esc>", 'tnix')
1131  call assert_equal(['X', ''], getline(1, '$'))
1132  %d
1133  let g:special=0
1134  call feedkeys("AM\<c-x>\<c-u>\<cr>\<esc>", 'tnix')
1135  call assert_equal(['Mar', ''], getline(1, '$'))
1136  %d
1137  call feedkeys("AM\<c-x>\<c-u>\<c-n>\<cr>\<esc>", 'tnix')
1138  call assert_equal(['May', ''], getline(1, '$'))
1139  %d
1140  call feedkeys("AM\<c-x>\<c-u>\<c-n>\<c-n>\<cr>\<esc>", 'tnix')
1141  call assert_equal(['M', ''], getline(1, '$'))
1142  delfu CompleteMonths
1143  %d
1144  try
1145    call feedkeys("A\<c-x>\<c-u>", 'tnix')
1146    call assert_fails(1, 'unknown completion function')
1147  catch /^Vim\%((\a\+)\)\=:E117/
1148    call assert_true(1, 'E117 error caught')
1149  endtry
1150  set completefunc=
1151  bw!
1152endfunc
1153
1154func Test_edit_completefunc_delete()
1155  func CompleteFunc(findstart, base)
1156    if a:findstart == 1
1157      return col('.') - 1
1158    endif
1159    normal dd
1160    return ['a', 'b']
1161  endfunc
1162  new
1163  set completefunc=CompleteFunc
1164  call setline(1, ['', 'abcd', ''])
1165  2d
1166  call assert_fails("normal 2G$a\<C-X>\<C-U>", 'E565:')
1167  bwipe!
1168endfunc
1169
1170func Test_edit_CTRL_Z()
1171  " Ctrl-Z when insertmode is not set inserts it literally
1172  new
1173  call setline(1, 'abc')
1174  call feedkeys("A\<c-z>\<esc>", 'tnix')
1175  call assert_equal(["abc\<c-z>"], getline(1,'$'))
1176  bw!
1177  " TODO: How to Test Ctrl-Z in insert mode, e.g. suspend?
1178endfunc
1179
1180func Test_edit_DROP()
1181  if !has("dnd")
1182    return
1183  endif
1184  new
1185  call setline(1, ['abc def ghi'])
1186  call cursor(1, 1)
1187  try
1188    call feedkeys("i\<Drop>\<Esc>", 'tnix')
1189    call assert_fails(1, 'Invalid register name')
1190  catch /^Vim\%((\a\+)\)\=:E353/
1191    call assert_true(1, 'error caught')
1192  endtry
1193  bw!
1194endfunc
1195
1196func Test_edit_CTRL_V()
1197  new
1198  call setline(1, ['abc'])
1199  call cursor(2, 1)
1200
1201  " force some redraws
1202  set showmode showcmd
1203  call Ntest_override('char_avail', 1)
1204
1205  call feedkeys("A\<c-v>\<c-n>\<c-v>\<c-l>\<c-v>\<c-b>\<esc>", 'tnix')
1206  call assert_equal(["abc\x0e\x0c\x02"], getline(1, '$'))
1207
1208  if has("rightleft") && exists("+rl")
1209    set rl
1210    call setline(1, ['abc'])
1211    call cursor(2, 1)
1212    call feedkeys("A\<c-v>\<c-n>\<c-v>\<c-l>\<c-v>\<c-b>\<esc>", 'tnix')
1213    call assert_equal(["abc\x0e\x0c\x02"], getline(1, '$'))
1214    set norl
1215  endif
1216
1217  set noshowmode showcmd
1218  call Ntest_override('char_avail', 0)
1219
1220  " No modifiers should be applied to the char typed using i_CTRL-V_digit.
1221  call feedkeys(":append\<CR>\<C-V>76c\<C-V>76\<C-F2>\<C-V>u3c0j\<C-V>u3c0\<M-F3>\<CR>.\<CR>", 'tnix')
1222  call assert_equal('LcL<C-F2>πjπ<M-F3>', getline(2))
1223
1224  if has('osx')
1225    " A char with a modifier should not be a valid char for i_CTRL-V_digit.
1226    call feedkeys("o\<C-V>\<D-j>\<C-V>\<D-1>\<C-V>\<D-o>\<C-V>\<D-x>\<C-V>\<D-u>", 'tnix')
1227    call assert_equal('<D-j><D-1><D-o><D-x><D-u>', getline(3))
1228  endif
1229
1230  bw!
1231endfunc
1232
1233func Test_edit_F1()
1234  CheckFeature quickfix
1235
1236  " Pressing <f1>
1237  new
1238  " call feedkeys(":set im\<cr>\<f1>\<c-l>", 'tnix')
1239  call feedkeys("i\<f1>\<esc>", 'tnix')
1240  set noinsertmode
1241  call assert_equal('help', &buftype)
1242  bw
1243  bw
1244endfunc
1245
1246func Test_edit_F21()
1247  " Pressing <f21>
1248  " sends a netbeans command
1249  if has("netbeans_intg")
1250    new
1251    " I have no idea what this is supposed to do :)
1252    call feedkeys("A\<F21>\<F1>\<esc>", 'tnix')
1253    bw
1254  endif
1255endfunc
1256
1257func Test_edit_HOME_END()
1258  " Test Home/End Keys
1259  new
1260  set foldopen+=hor
1261  call setline(1, ['abc', 'def'])
1262  call cursor(1, 1)
1263  call feedkeys("AX\<Home>Y\<esc>", 'tnix')
1264  call cursor(2, 1)
1265  call feedkeys("iZ\<End>Y\<esc>", 'tnix')
1266  call assert_equal(['YabcX', 'ZdefY'], getline(1, '$'))
1267
1268  set foldopen-=hor
1269  bw!
1270endfunc
1271
1272func Test_edit_INS()
1273  " Test for Pressing <Insert>
1274  new
1275  call setline(1, ['abc', 'def'])
1276  call cursor(1, 1)
1277  call feedkeys("i\<Insert>ZYX>", 'tnix')
1278  call assert_equal(['ZYX>', 'def'], getline(1, '$'))
1279  call setline(1, ['abc', 'def'])
1280  call cursor(1, 1)
1281  call feedkeys("i\<Insert>Z\<Insert>YX>", 'tnix')
1282  call assert_equal(['ZYX>bc', 'def'], getline(1, '$'))
1283  bw!
1284endfunc
1285
1286func Test_edit_LEFT_RIGHT()
1287  " Left, Shift-Left, Right, Shift-Right
1288  new
1289  call setline(1, ['abc def ghi', 'ABC DEF GHI', 'ZZZ YYY XXX'])
1290  let _ww=&ww
1291  set ww=
1292  call cursor(2, 1)
1293  call feedkeys("i\<left>\<esc>", 'tnix')
1294  call assert_equal([0, 2, 1, 0], getpos('.'))
1295  " Is this a bug, <s-left> does not respect whichwrap option
1296  call feedkeys("i\<s-left>\<esc>", 'tnix')
1297  call assert_equal([0, 1, 8, 0], getpos('.'))
1298  call feedkeys("i". repeat("\<s-left>", 3). "\<esc>", 'tnix')
1299  call assert_equal([0, 1, 1, 0], getpos('.'))
1300  call feedkeys("i\<right>\<esc>", 'tnix')
1301  call assert_equal([0, 1, 1, 0], getpos('.'))
1302  call feedkeys("i\<right>\<right>\<esc>", 'tnix')
1303  call assert_equal([0, 1, 2, 0], getpos('.'))
1304  call feedkeys("A\<right>\<esc>", 'tnix')
1305  call assert_equal([0, 1, 11, 0], getpos('.'))
1306  call feedkeys("A\<s-right>\<esc>", 'tnix')
1307  call assert_equal([0, 2, 1, 0], getpos('.'))
1308  call feedkeys("i\<s-right>\<esc>", 'tnix')
1309  call assert_equal([0, 2, 4, 0], getpos('.'))
1310  call cursor(3, 11)
1311  call feedkeys("A\<right>\<esc>", 'tnix')
1312  call feedkeys("A\<s-right>\<esc>", 'tnix')
1313  call assert_equal([0, 3, 11, 0], getpos('.'))
1314  call cursor(2, 11)
1315  " <S-Right> does not respect 'whichwrap' option
1316  call feedkeys("A\<s-right>\<esc>", 'tnix')
1317  call assert_equal([0, 3, 1, 0], getpos('.'))
1318  " Check motion when 'whichwrap' contains cursor keys for insert mode
1319  set ww+=[,]
1320  call cursor(2, 1)
1321  call feedkeys("i\<left>\<esc>", 'tnix')
1322  call assert_equal([0, 1, 11, 0], getpos('.'))
1323  call cursor(2, 11)
1324  call feedkeys("A\<right>\<esc>", 'tnix')
1325  call assert_equal([0, 3, 1, 0], getpos('.'))
1326  call cursor(2, 11)
1327  call feedkeys("A\<s-right>\<esc>", 'tnix')
1328  call assert_equal([0, 3, 1, 0], getpos('.'))
1329  let &ww = _ww
1330  bw!
1331endfunc
1332
1333func Test_edit_MOUSE()
1334  " This is a simple test, since we're not really using the mouse here
1335  CheckFeature mouse
1336  10new
1337  call setline(1, range(1, 100))
1338  call cursor(1, 1)
1339  call assert_equal(1, line('w0'))
1340  call assert_equal(10, line('w$'))
1341  set mouse=a
1342  " One scroll event moves three lines.
1343  call feedkeys("A\<ScrollWheelDown>\<esc>", 'tnix')
1344  call assert_equal(4, line('w0'))
1345  call assert_equal(13, line('w$'))
1346  " This should move by one page down.
1347  call feedkeys("A\<S-ScrollWheelDown>\<esc>", 'tnix')
1348  call assert_equal(14, line('w0'))
1349  set nostartofline
1350  " Another page down.
1351  call feedkeys("A\<C-ScrollWheelDown>\<esc>", 'tnix')
1352  call assert_equal(24, line('w0'))
1353
1354  call assert_equal([0, 24, 2, 0], getpos('.'))
1355  call Ntest_setmouse(4, 3)
1356  call feedkeys("A\<LeftMouse>\<esc>", 'tnix')
1357  call assert_equal([0, 27, 2, 0], getpos('.'))
1358  set mousemodel=extend
1359  call Ntest_setmouse(5, 3)
1360  call feedkeys("A\<RightMouse>\<esc>\<esc>", 'tnix')
1361  call assert_equal([0, 28, 2, 0], getpos('.'))
1362  set mousemodel&
1363  call cursor(1, 100)
1364  norm! zt
1365  " this should move by a screen up, but when the test
1366  " is run, it moves up to the top of the buffer...
1367  call feedkeys("A\<ScrollWheelUp>\<esc>", 'tnix')
1368  call assert_equal([0, 1, 1, 0], getpos('.'))
1369  call cursor(1, 30)
1370  norm! zt
1371  call feedkeys("A\<S-ScrollWheelUp>\<esc>", 'tnix')
1372  call assert_equal([0, 1, 1, 0], getpos('.'))
1373  call cursor(1, 30)
1374  norm! zt
1375  call feedkeys("A\<C-ScrollWheelUp>\<esc>", 'tnix')
1376  call assert_equal([0, 1, 1, 0], getpos('.'))
1377  %d
1378  call setline(1, repeat(["12345678901234567890"], 100))
1379  call cursor(2, 1)
1380  call feedkeys("A\<ScrollWheelRight>\<esc>", 'tnix')
1381  call assert_equal([0, 2, 20, 0], getpos('.'))
1382  call feedkeys("A\<ScrollWheelLeft>\<esc>", 'tnix')
1383  call assert_equal([0, 2, 20, 0], getpos('.'))
1384  call feedkeys("A\<S-ScrollWheelRight>\<esc>", 'tnix')
1385  call assert_equal([0, 2, 20, 0], getpos('.'))
1386  call feedkeys("A\<S-ScrollWheelLeft>\<esc>", 'tnix')
1387  call assert_equal([0, 2, 20, 0], getpos('.'))
1388  call feedkeys("A\<C-ScrollWheelRight>\<esc>", 'tnix')
1389  call assert_equal([0, 2, 20, 0], getpos('.'))
1390  call feedkeys("A\<C-ScrollWheelLeft>\<esc>", 'tnix')
1391  call assert_equal([0, 2, 20, 0], getpos('.'))
1392  set mouse& startofline
1393  bw!
1394endfunc
1395
1396func Test_edit_PAGEUP_PAGEDOWN()
1397  10new
1398  call setline(1, repeat(['abc def ghi'], 30))
1399  call cursor(1, 1)
1400  call assert_nobeep('call feedkeys("i\<PageDown>\<esc>", "tnix")')
1401  call assert_equal([0, 9, 1, 0], getpos('.'))
1402  call assert_nobeep('call feedkeys("i\<PageDown>\<esc>", "tnix")')
1403  call assert_equal([0, 17, 1, 0], getpos('.'))
1404  call assert_nobeep('call feedkeys("i\<PageDown>\<esc>", "tnix")')
1405  call assert_equal([0, 25, 1, 0], getpos('.'))
1406  call assert_nobeep('call feedkeys("i\<PageDown>\<esc>", "tnix")')
1407  call assert_equal([0, 30, 1, 0], getpos('.'))
1408  call assert_beeps('call feedkeys("i\<PageDown>\<esc>", "tnix")')
1409  call assert_equal([0, 30, 1, 0], getpos('.'))
1410  call assert_nobeep('call feedkeys("A\<PageUp>\<esc>", "tnix")')
1411  call assert_equal([0, 29, 1, 0], getpos('.'))
1412  call assert_nobeep('call feedkeys("A\<PageUp>\<esc>", "tnix")')
1413  call assert_equal([0, 21, 1, 0], getpos('.'))
1414  call assert_nobeep('call feedkeys("A\<PageUp>\<esc>", "tnix")')
1415  call assert_equal([0, 13, 1, 0], getpos('.'))
1416  call assert_nobeep('call feedkeys("A\<PageUp>\<esc>", "tnix")')
1417  call assert_equal([0, 10, 1, 0], getpos('.'))
1418  call assert_beeps('call feedkeys("A\<PageUp>\<esc>", "tnix")')
1419  call assert_equal([0, 10, 11, 0], getpos('.'))
1420  " <S-Up> is the same as <PageUp>
1421  " <S-Down> is the same as <PageDown>
1422  call cursor(1, 1)
1423  call assert_nobeep('call feedkeys("i\<S-Down>\<esc>", "tnix")')
1424  call assert_equal([0, 9, 1, 0], getpos('.'))
1425  call assert_nobeep('call feedkeys("i\<S-Down>\<esc>", "tnix")')
1426  call assert_equal([0, 17, 1, 0], getpos('.'))
1427  call assert_nobeep('call feedkeys("i\<S-Down>\<esc>", "tnix")')
1428  call assert_equal([0, 25, 1, 0], getpos('.'))
1429  call assert_nobeep('call feedkeys("i\<S-Down>\<esc>", "tnix")')
1430  call assert_equal([0, 30, 1, 0], getpos('.'))
1431  call assert_beeps('call feedkeys("i\<S-Down>\<esc>", "tnix")')
1432  call assert_equal([0, 30, 1, 0], getpos('.'))
1433  call assert_nobeep('call feedkeys("A\<S-Up>\<esc>", "tnix")')
1434  call assert_equal([0, 29, 1, 0], getpos('.'))
1435  call assert_nobeep('call feedkeys("A\<S-Up>\<esc>", "tnix")')
1436  call assert_equal([0, 21, 1, 0], getpos('.'))
1437  call assert_nobeep('call feedkeys("A\<S-Up>\<esc>", "tnix")')
1438  call assert_equal([0, 13, 1, 0], getpos('.'))
1439  call assert_nobeep('call feedkeys("A\<S-Up>\<esc>", "tnix")')
1440  call assert_equal([0, 10, 1, 0], getpos('.'))
1441  call assert_beeps('call feedkeys("A\<S-Up>\<esc>", "tnix")')
1442  call assert_equal([0, 10, 11, 0], getpos('.'))
1443  set nostartofline
1444  call cursor(30, 11)
1445  norm! zt
1446  call assert_nobeep('call feedkeys("A\<PageUp>\<esc>", "tnix")')
1447  call assert_equal([0, 29, 11, 0], getpos('.'))
1448  call assert_nobeep('call feedkeys("A\<PageUp>\<esc>", "tnix")')
1449  call assert_equal([0, 21, 11, 0], getpos('.'))
1450  call assert_nobeep('call feedkeys("A\<PageUp>\<esc>", "tnix")')
1451  call assert_equal([0, 13, 11, 0], getpos('.'))
1452  call assert_nobeep('call feedkeys("A\<PageUp>\<esc>", "tnix")')
1453  call assert_equal([0, 10, 11, 0], getpos('.'))
1454  call assert_beeps('call feedkeys("A\<PageUp>\<esc>", "tnix")')
1455  call assert_equal([0, 10, 11, 0], getpos('.'))
1456  call cursor(1, 1)
1457  call assert_nobeep('call feedkeys("A\<PageDown>\<esc>", "tnix")')
1458  call assert_equal([0, 9, 11, 0], getpos('.'))
1459  call assert_nobeep('call feedkeys("A\<PageDown>\<esc>", "tnix")')
1460  call assert_equal([0, 17, 11, 0], getpos('.'))
1461  call assert_nobeep('call feedkeys("A\<PageDown>\<esc>", "tnix")')
1462  call assert_equal([0, 25, 11, 0], getpos('.'))
1463  call assert_nobeep('call feedkeys("A\<PageDown>\<esc>", "tnix")')
1464  call assert_equal([0, 30, 11, 0], getpos('.'))
1465  call assert_beeps('call feedkeys("A\<PageDown>\<esc>", "tnix")')
1466  call assert_equal([0, 30, 11, 0], getpos('.'))
1467  " <S-Up> is the same as <PageUp>
1468  " <S-Down> is the same as <PageDown>
1469  call cursor(30, 11)
1470  norm! zt
1471  call assert_nobeep('call feedkeys("A\<S-Up>\<esc>", "tnix")')
1472  call assert_equal([0, 29, 11, 0], getpos('.'))
1473  call assert_nobeep('call feedkeys("A\<S-Up>\<esc>", "tnix")')
1474  call assert_equal([0, 21, 11, 0], getpos('.'))
1475  call assert_nobeep('call feedkeys("A\<S-Up>\<esc>", "tnix")')
1476  call assert_equal([0, 13, 11, 0], getpos('.'))
1477  call assert_nobeep('call feedkeys("A\<S-Up>\<esc>", "tnix")')
1478  call assert_equal([0, 10, 11, 0], getpos('.'))
1479  call assert_beeps('call feedkeys("A\<S-Up>\<esc>", "tnix")')
1480  call assert_equal([0, 10, 11, 0], getpos('.'))
1481  call cursor(1, 1)
1482  call assert_nobeep('call feedkeys("A\<S-Down>\<esc>", "tnix")')
1483  call assert_equal([0, 9, 11, 0], getpos('.'))
1484  call assert_nobeep('call feedkeys("A\<S-Down>\<esc>", "tnix")')
1485  call assert_equal([0, 17, 11, 0], getpos('.'))
1486  call assert_nobeep('call feedkeys("A\<S-Down>\<esc>", "tnix")')
1487  call assert_equal([0, 25, 11, 0], getpos('.'))
1488  call assert_nobeep('call feedkeys("A\<S-Down>\<esc>", "tnix")')
1489  call assert_equal([0, 30, 11, 0], getpos('.'))
1490  call assert_beeps('call feedkeys("A\<S-Down>\<esc>", "tnix")')
1491  call assert_equal([0, 30, 11, 0], getpos('.'))
1492  bw!
1493endfunc
1494
1495func Test_edit_forbidden()
1496  new
1497  " 1) edit in the sandbox is not allowed
1498  call setline(1, 'a')
1499  com! Sandbox :sandbox call feedkeys("i\<del>\<esc>", 'tnix')
1500  call assert_fails(':Sandbox', 'E48:')
1501  com! Sandbox :sandbox exe "norm! i\<del>"
1502  call assert_fails(':Sandbox', 'E48:')
1503  delcom Sandbox
1504  call assert_equal(['a'], getline(1,'$'))
1505
1506  " 2) edit with textlock set
1507  fu! DoIt()
1508    call feedkeys("i\<del>\<esc>", 'tnix')
1509  endfu
1510  au InsertCharPre <buffer> :call DoIt()
1511  try
1512    call feedkeys("ix\<esc>", 'tnix')
1513    call assert_fails(1, 'textlock')
1514  catch /^Vim\%((\a\+)\)\=:E565/ " catch E565: not allowed here
1515  endtry
1516  " TODO: Might be a bug: should x really be inserted here
1517  call assert_equal(['xa'], getline(1, '$'))
1518  delfu DoIt
1519  try
1520    call feedkeys("ix\<esc>", 'tnix')
1521    call assert_fails(1, 'unknown function')
1522  catch /^Vim\%((\a\+)\)\=:E117/ " catch E117: unknown function
1523  endtry
1524  au! InsertCharPre
1525
1526  " 3) edit when completion is shown
1527  fun! Complete(findstart, base)
1528    if a:findstart
1529      return col('.')
1530    else
1531      call feedkeys("i\<del>\<esc>", 'tnix')
1532      return []
1533    endif
1534  endfun
1535  set completefunc=Complete
1536  try
1537    call feedkeys("i\<c-x>\<c-u>\<esc>", 'tnix')
1538    call assert_fails(1, 'change in complete function')
1539  catch /^Vim\%((\a\+)\)\=:E565/ " catch E565
1540  endtry
1541  delfu Complete
1542  set completefunc=
1543
1544  if has("rightleft") && exists("+fkmap")
1545    " 4) 'R' when 'fkmap' and 'revins' is set.
1546    set revins fkmap
1547    try
1548      normal Ri
1549      call assert_fails(1, "R with 'fkmap' and 'ri' set")
1550    catch
1551    finally
1552      set norevins nofkmap
1553    endtry
1554  endif
1555  bw!
1556endfunc
1557
1558func Test_edit_rightleft()
1559  " Cursor in rightleft mode moves differently
1560  CheckFeature rightleft
1561  call NewWindow(10, 20)
1562  call setline(1, ['abc', 'def', 'ghi'])
1563  call cursor(1, 2)
1564  set rightleft
1565  " Screen looks as expected
1566  let lines = ScreenLines([1, 4], winwidth(0))
1567  let expect = [
1568        \"                 cba",
1569        \"                 fed",
1570        \"                 ihg",
1571        \"                   ~"]
1572  call assert_equal(join(expect, "\n"), join(lines, "\n"))
1573  " 2) right moves to the left
1574  call feedkeys("i\<right>\<esc>x", 'txin')
1575  call assert_equal(['bc', 'def', 'ghi'], getline(1,'$'))
1576  call cursor(1, 2)
1577  call feedkeys("i\<s-right>\<esc>", 'txin')
1578  call cursor(1, 2)
1579  call feedkeys("i\<c-right>\<esc>", 'txin')
1580  " Screen looks as expected
1581  let lines = ScreenLines([1, 4], winwidth(0))
1582  let expect = [
1583        \"                  cb",
1584        \"                 fed",
1585        \"                 ihg",
1586        \"                   ~"]
1587  call assert_equal(join(expect, "\n"), join(lines, "\n"))
1588  " 2) left moves to the right
1589  call setline(1, ['abc', 'def', 'ghi'])
1590  call cursor(1, 2)
1591  call feedkeys("i\<left>\<esc>x", 'txin')
1592  call assert_equal(['ac', 'def', 'ghi'], getline(1,'$'))
1593  call cursor(1, 2)
1594  call feedkeys("i\<s-left>\<esc>", 'txin')
1595  call cursor(1, 2)
1596  call feedkeys("i\<c-left>\<esc>", 'txin')
1597  " Screen looks as expected
1598  let lines = ScreenLines([1, 4], winwidth(0))
1599  let expect = [
1600        \"                  ca",
1601        \"                 fed",
1602        \"                 ihg",
1603        \"                   ~"]
1604  call assert_equal(join(expect, "\n"), join(lines, "\n"))
1605  %d _
1606  " call test_override('redraw_flag', 1)
1607  " call test_override('char_avail', 1)
1608  call feedkeys("a\<C-V>x41", "xt")
1609  redraw!
1610  call assert_equal(repeat(' ', 19) .. 'A', Screenline(1))
1611  " call test_override('ALL', 0)
1612  set norightleft
1613  bw!
1614endfunc
1615
1616func Test_edit_complete_very_long_name()
1617  " Long directory names only work on Unix.
1618  CheckUnix
1619
1620  let dirname = getcwd() . "/Xlongdir"
1621  let longdirname = dirname . repeat('/' . repeat('d', 255), 4)
1622  try
1623    call mkdir(longdirname, 'pR')
1624  catch /E739:/
1625    " Long directory name probably not supported.
1626    call delete(dirname, 'rf')
1627    return
1628  endtry
1629
1630  " Try to get the Vim window position before setting 'columns', so that we can
1631  " move the window back to where it was.
1632  let winposx = getwinposx()
1633  let winposy = getwinposy()
1634
1635  if winposx >= 0 && winposy >= 0 && !has('gui_running')
1636    " We did get the window position, but xterm may report the wrong numbers.
1637    " Move the window to the reported position and compute any offset.
1638    exe 'winpos ' . winposx . ' ' . winposy
1639    sleep 100m
1640    let x = getwinposx()
1641    if x >= 0
1642      let winposx += winposx - x
1643    endif
1644    let y = getwinposy()
1645    if y >= 0
1646      let winposy += winposy - y
1647    endif
1648  endif
1649
1650  let save_columns = &columns
1651  " Need at least about 1100 columns to reproduce the problem.
1652  set columns=2000
1653  set noswapfile
1654
1655  let longfilename = longdirname . '/' . repeat('a', 255)
1656  call writefile(['Totum', 'Table'], longfilename)
1657  new
1658  exe "next Xnofile " . longfilename
1659  exe "normal iT\<C-N>"
1660
1661  bwipe!
1662  exe 'bwipe! ' . longfilename
1663  let &columns = save_columns
1664  if winposx >= 0 && winposy >= 0
1665    exe 'winpos ' . winposx . ' ' . winposy
1666  endif
1667  set swapfile&
1668endfunc
1669
1670func Test_edit_backtick()
1671  next a\`b c
1672  call assert_equal('a`b', expand('%'))
1673  next
1674  call assert_equal('c', expand('%'))
1675  call assert_equal('a\`b c', expand('##'))
1676endfunc
1677
1678func Test_edit_quit()
1679  edit foo.txt
1680  split
1681  new
1682  call setline(1, 'hello')
1683  3wincmd w
1684  redraw!
1685  call assert_fails('1q', 'E37:')
1686  bwipe! foo.txt
1687  only
1688endfunc
1689
1690func Test_edit_alt()
1691  " Keeping the cursor line didn't happen when the first line has indent.
1692  new
1693  call setline(1, ['  one', 'two', 'three'])
1694  w XAltFile
1695  $
1696  call assert_equal(3, line('.'))
1697  e Xother
1698  e #
1699  call assert_equal(3, line('.'))
1700
1701  bwipe XAltFile
1702  call delete('XAltFile')
1703endfunc
1704
1705func Test_edit_InsertLeave()
1706  new
1707  au InsertLeavePre * let g:did_au_pre = 1
1708  au InsertLeave * let g:did_au = 1
1709  let g:did_au_pre = 0
1710  let g:did_au = 0
1711  call feedkeys("afoo\<Esc>", 'tx')
1712  call assert_equal(1, g:did_au_pre)
1713  call assert_equal(1, g:did_au)
1714  call assert_equal('foo', getline(1))
1715
1716  let g:did_au_pre = 0
1717  let g:did_au = 0
1718  call feedkeys("Sbar\<C-C>", 'tx')
1719  call assert_equal(1, g:did_au_pre)
1720  call assert_equal(0, g:did_au)
1721  call assert_equal('bar', getline(1))
1722
1723  inoremap x xx<Esc>
1724  let g:did_au_pre = 0
1725  let g:did_au = 0
1726  call feedkeys("Saax", 'tx')
1727  call assert_equal(1, g:did_au_pre)
1728  call assert_equal(1, g:did_au)
1729  call assert_equal('aaxx', getline(1))
1730
1731  inoremap x xx<C-C>
1732  let g:did_au_pre = 0
1733  let g:did_au = 0
1734  call feedkeys("Sbbx", 'tx')
1735  call assert_equal(1, g:did_au_pre)
1736  call assert_equal(0, g:did_au)
1737  call assert_equal('bbxx', getline(1))
1738
1739  bwipe!
1740  au! InsertLeave InsertLeavePre
1741  iunmap x
1742endfunc
1743
1744func Test_edit_InsertLeave_undo()
1745  new XtestUndo
1746  set undofile
1747  au InsertLeave * wall
1748  exe "normal ofoo\<Esc>"
1749  call assert_equal(2, line('$'))
1750  normal u
1751  call assert_equal(1, line('$'))
1752
1753  bwipe!
1754  au! InsertLeave
1755  call delete('XtestUndo')
1756  call delete(undofile('XtestUndo'))
1757  set undofile&
1758endfunc
1759
1760" Test for inserting characters using CTRL-V followed by a number.
1761func Test_edit_special_chars()
1762  new
1763
1764  let t = "o\<C-V>65\<C-V>x42\<C-V>o103 \<C-V>33a\<C-V>xfg\<C-V>o78\<Esc>"
1765
1766  exe "normal " . t
1767  call assert_equal("ABC !a\<C-O>g\<C-G>8", getline(2))
1768
1769  bw!
1770endfunc
1771
1772func Test_edit_startinsert()
1773  new
1774  set backspace+=start
1775  call setline(1, 'foobar')
1776  call feedkeys("A\<C-U>\<Esc>", 'xt')
1777  call assert_equal('', getline(1))
1778
1779  call setline(1, 'foobar')
1780  call feedkeys(":startinsert!\<CR>\<C-U>\<Esc>", 'xt')
1781  call assert_equal('', getline(1))
1782
1783  call setline(1, 'foobar')
1784  setl nomodifiable
1785  call assert_fails('startinsert', 'E21:')
1786
1787  call cursor(1, 1)
1788  call assert_fails('startinsert!', 'E21:')
1789  call assert_equal(1, col('.'))
1790
1791  set backspace&
1792  bwipe!
1793endfunc
1794
1795" ":startinsert" is ineffective in a terminal window: it must not give an
1796" error and with "!" it must not move the cursor.
1797func Test_edit_startinsert_in_terminal()
1798  CheckFeature terminal
1799
1800  let buf = Run_shell_in_terminal({})
1801
1802  " Fill the terminal with text.
1803  if has('win32')
1804    call feedkeys("dir\<CR>", 'xt')
1805  else
1806    call feedkeys("ls\<CR>", 'xt')
1807  endif
1808  call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
1809
1810  " Go to Terminal-Normal mode and put the cursor on a line with text.
1811  call feedkeys("\<C-W>N", 'xt')
1812  call assert_notequal(0, search('\S', 'w'))
1813  call cursor(line('.'), 1)
1814
1815  startinsert
1816  startinsert!
1817  call assert_equal(1, col('.'))
1818
1819  " Clear "restart_edit" in case the commands were not ignored.
1820  stopinsert
1821
1822  call feedkeys("i", 'xt')
1823  call StopShellInTerminal(buf)
1824  bwipe!
1825endfunc
1826
1827" Backspacing in Virtual Replace mode over a wide character that replaced
1828" several narrower ones must not eat the padding that follows.
1829func Test_edit_vreplace_bs_multibyte()
1830  new
1831  call setline(1, 'aé    xyz')
1832  call feedkeys("gR\u4e00\<BS>\e", 'xt')
1833  call assert_equal('aé    xyz', getline(1))
1834
1835  call setline(1, 'a€  xyz')
1836  call feedkeys("gR\u4e00\<BS>\e", 'xt')
1837  call assert_equal('a€  xyz', getline(1))
1838  bwipe!
1839endfunc
1840
1841" Test for :startreplace and :startgreplace
1842func Test_edit_startreplace()
1843  new
1844  call setline(1, 'abc')
1845  call feedkeys("l:startreplace\<CR>xyz\e", 'xt')
1846  call assert_equal('axyz', getline(1))
1847  call feedkeys("0:startreplace!\<CR>abc\e", 'xt')
1848  call assert_equal('axyzabc', getline(1))
1849  call setline(1, "a\tb")
1850  call feedkeys("0l:startgreplace\<CR>xyz\e", 'xt')
1851  call assert_equal("axyz\tb", getline(1))
1852  call feedkeys("0i\<C-R>=execute('startreplace')\<CR>12\e", 'xt')
1853  call assert_equal("12axyz\tb", getline(1))
1854
1855  call setline(1, 'abc')
1856  setl nomodifiable
1857  call assert_fails('startreplace', 'E21:')
1858  call assert_fails('startgreplace', 'E21:')
1859
1860  call cursor(1, 1)
1861  call assert_fails('startreplace!', 'E21:')
1862  call assert_equal(1, col('.'))
1863  call cursor(1, 1)
1864  call assert_fails('startgreplace!', 'E21:')
1865  call assert_equal(1, col('.'))
1866
1867  bw!
1868endfunc
1869
1870func Test_edit_noesckeys()
1871  CheckNotGui
1872  new
1873
1874  " <Left> moves cursor when 'esckeys' is set
1875  exe "set t_kl=\<Esc>OD"
1876  " set esckeys
1877  call feedkeys("axyz\<Esc>ODX", "xt")
1878  " call assert_equal("xyXz", getline(1))
1879
1880  " <Left> exits Insert mode when 'esckeys' is off
1881  " set noesckeys
1882  call setline(1, '')
1883  call feedkeys("axyz\<Esc>ODX", "xt")
1884  call assert_equal(["DX", "xyz"], getline(1, 2))
1885
1886  bwipe!
1887  " set esckeys
1888endfunc
1889
1890" Test for running an invalid ex command in insert mode using CTRL-O
1891func Test_edit_ctrl_o_invalid_cmd()
1892  new
1893  set showmode showcmd
1894  " Avoid a sleep of 3 seconds. Zero might have side effects.
1895  " call test_override('ui_delay', 50)
1896  let caught_e492 = 0
1897  try
1898    call feedkeys("i\<C-O>:invalid\<CR>abc\<Esc>", "xt")
1899  catch /E492:/
1900    let caught_e492 = 1
1901  endtry
1902  call assert_equal(1, caught_e492)
1903  call assert_equal('abc', getline(1))
1904  set showmode& showcmd&
1905  " call test_override('ui_delay', 0)
1906  bw!
1907endfunc
1908
1909" Test for editing a file with a very long name
1910func Test_edit_illegal_filename()
1911  CheckEnglish
1912  new
1913  redir => msg
1914  exe 'edit ' . repeat('f', 5000)
1915  redir END
1916  call assert_match("Illegal file name$", split(msg, "\n")[0])
1917  close!
1918endfunc
1919
1920" Test for editing a directory
1921func Test_edit_is_a_directory()
1922  CheckEnglish
1923  let dirname = getcwd() . "/Xeditdir"
1924  call mkdir(dirname, 'p')
1925
1926  new
1927  redir => msg
1928  exe 'edit' dirname
1929  redir END
1930  call assert_match("is a directory$", split(msg, "\n")[0])
1931  bwipe!
1932
1933  let dirname .= '/'
1934
1935  new
1936  redir => msg
1937  exe 'edit' dirname
1938  redir END
1939  call assert_match("is a directory$", split(msg, "\n")[0])
1940  bwipe!
1941
1942  call delete(dirname, 'rf')
1943endfunc
1944
1945" Test for editing a file using invalid file encoding
1946func Test_edit_invalid_encoding()
1947  CheckEnglish
1948  call writefile([], 'Xinvfile', 'D')
1949  redir => msg
1950  new ++enc=axbyc Xinvfile
1951  redir END
1952  call assert_match('\[NOT converted\]', msg)
1953  close!
1954endfunc
1955
1956" Test for the "charconvert" option
1957func Test_edit_charconvert()
1958  CheckEnglish
1959  call writefile(['one', 'two'], 'Xccfile', 'D')
1960
1961  " set 'charconvert' to a non-existing function
1962  set charconvert=NonExitingFunc()
1963  new
1964  let caught_e117 = v:false
1965  try
1966    redir => msg
1967    edit ++enc=axbyc Xccfile
1968  catch /E117:/
1969    let caught_e117 = v:true
1970  finally
1971    redir END
1972  endtry
1973  call assert_true(caught_e117)
1974  call assert_equal(['one', 'two'], getline(1, '$'))
1975  call assert_match("Conversion with 'charconvert' failed", msg)
1976  close!
1977  set charconvert&
1978
1979  " 'charconvert' function doesn't create an output file
1980  func Cconv1()
1981  endfunc
1982  set charconvert=Cconv1()
1983  new
1984  redir => msg
1985  edit ++enc=axbyc Xccfile
1986  redir END
1987  call assert_equal(['one', 'two'], getline(1, '$'))
1988  call assert_match("can't read output of 'charconvert'", msg)
1989  close!
1990  delfunc Cconv1
1991  set charconvert&
1992
1993  " 'charconvert' function to convert to upper case
1994  func Cconv2()
1995    let data = readfile(v:fname_in)
1996    call map(data, 'toupper(v:val)')
1997    call writefile(data, v:fname_out)
1998  endfunc
1999  set charconvert=Cconv2()
2000  new Xccfile
2001  write ++enc=ucase Xccfile1
2002  call assert_equal(['ONE', 'TWO'], readfile('Xccfile1'))
2003  call delete('Xccfile1')
2004  close!
2005  delfunc Cconv2
2006  set charconvert&
2007
2008  " 'charconvert' function removes the input file
2009  func Cconv3()
2010    call delete(v:fname_in)
2011  endfunc
2012  set charconvert=Cconv3()
2013  new
2014  call assert_fails('edit ++enc=lcase Xccfile', 'E202:')
2015  call assert_equal([''], getline(1, '$'))
2016  close!
2017  delfunc Cconv3
2018  set charconvert&
2019endfunc
2020
2021" Test for editing a file without read permission
2022func Test_edit_file_no_read_perm()
2023  CheckUnix
2024  CheckNotRoot
2025
2026  call writefile(['one', 'two'], 'Xnrpfile', 'D')
2027  call setfperm('Xnrpfile', '-w-------')
2028  new
2029  redir => msg
2030  edit Xnrpfile
2031  redir END
2032  call assert_equal(1, &readonly)
2033  call assert_equal([''], getline(1, '$'))
2034  call assert_match('\[Permission Denied\]', msg)
2035  close!
2036endfunc
2037
2038" Using :edit without leaving 'insertmode' should not cause Insert mode to be
2039" re-entered immediately after <C-L>
2040func Test_edit_insertmode_ex_edit()
2041  CheckRunVimInTerminal
2042
2043  let lines =<< trim END
2044    set insertmode noruler
2045    inoremap <C-B> <Cmd>edit Xfoo<CR>
2046  END
2047  call writefile(lines, 'Xtest_edit_insertmode_ex_edit', 'D')
2048
2049  let buf = RunVimInTerminal('-S Xtest_edit_insertmode_ex_edit', #{rows: 6, wait_for_ruler: 0})
2050  " Somehow when using valgrind "INSERT" does not show up unless we send
2051  " something to the terminal.
2052  for i in range(30)
2053    if term_getline(buf, 6) =~ 'INSERT'
2054      break
2055    endif
2056    call term_sendkeys(buf, "-")
2057    sleep 100m
2058  endfor
2059  call WaitForAssert({-> assert_match('^-- INSERT --\s*$', term_getline(buf, 6))})
2060  call term_sendkeys(buf, "\<C-B>\<C-L>")
2061  call WaitForAssert({-> assert_notmatch('^-- INSERT --\s*$', term_getline(buf, 6))})
2062
2063  " clean up
2064  call StopVimInTerminal(buf)
2065endfunc
2066
2067" Pressing escape in 'insertmode' should beep
2068" FIXME: Execute this later, when using valgrind it makes the next test
2069" Test_edit_insertmode_ex_edit() fail.
2070func Test_z_edit_insertmode_esc_beeps()
2071  new
2072  " set insertmode
2073  " call assert_beeps("call feedkeys(\"one\<Esc>\", 'xt')")
2074  set insertmode&
2075  " unsupported "CTRL-G l" command should beep in insert mode.
2076  call assert_beeps("normal i\<C-G>l")
2077  bwipe!
2078endfunc
2079
2080" Test for 'hkmap' and 'hkmapp'
2081func Test_edit_hkmap()
2082  throw "Skipped: Nvim does not support 'hkmap'"
2083  CheckFeature rightleft
2084  if has('win32') && !has('gui')
2085    " Test fails on the MS-Windows terminal version
2086    return
2087  endif
2088  new
2089
2090  set revins hkmap
2091  let str = 'abcdefghijklmnopqrstuvwxyz'
2092  let str ..= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
2093  let str ..= '`/'',.;'
2094  call feedkeys('i' .. str, 'xt')
2095  let expected = "óõú,.;"
2096  let expected ..= "ZYXWVUTSRQPONMLKJIHGFEDCBA"
2097  let expected ..= "æèñ'äåàãø/ôíîöêìçïéòë÷âáðù"
2098  call assert_equal(expected, getline(1))
2099
2100  %d
2101  set revins hkmap hkmapp
2102  let str = 'abcdefghijklmnopqrstuvwxyz'
2103  let str ..= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
2104  call feedkeys('i' .. str, 'xt')
2105  let expected = "õYXWVUTSRQóOïíLKJIHGFEDêBA"
2106  let expected ..= "öòXùåèúæø'ôñðîì÷çéäâóǟãëáà"
2107  call assert_equal(expected, getline(1))
2108
2109  set revins& hkmap& hkmapp&
2110  bw!
2111endfunc
2112
2113" Test for 'allowrevins' and using CTRL-_ in insert mode
2114func Test_edit_allowrevins()
2115  CheckFeature rightleft
2116  new
2117  set allowrevins
2118  call feedkeys("iABC\<C-_>DEF\<C-_>GHI", 'xt')
2119  call assert_equal('ABCFEDGHI', getline(1))
2120  set allowrevins&
2121  bw!
2122endfunc
2123
2124" Test for inserting a register in insert mode using CTRL-R
2125func Test_edit_insert_reg()
2126  throw 'Skipped: use test/functional/legacy/edit_spec.lua'
2127  new
2128  let g:Line = ''
2129  func SaveFirstLine()
2130    let g:Line = Screenline(1)
2131    return 'r'
2132  endfunc
2133  inoremap <expr> <buffer> <F2> SaveFirstLine()
2134  call test_override('redraw_flag', 1)
2135  call test_override('char_avail', 1)
2136  let @r = 'sample'
2137  call feedkeys("a\<C-R>=SaveFirstLine()\<CR>", "xt")
2138  call assert_equal('"', g:Line)
2139
2140  " Test for inserting an null and an empty list
2141  call feedkeys("a\<C-R>=test_null_list()\<CR>", "xt")
2142  call feedkeys("a\<C-R>=[]\<CR>", "xt")
2143  call assert_equal(['r'], getbufline('', 1, '$'))
2144  call test_override('ALL', 0)
2145  bw!
2146endfunc
2147
2148" Test for positioning cursor after CTRL-R expression failed
2149func Test_edit_ctrl_r_failed()
2150  CheckScreendump
2151  CheckRunVimInTerminal
2152
2153  let buf = RunVimInTerminal('', #{rows: 6, cols: 60})
2154
2155  " trying to insert a blob produces an error
2156  call term_sendkeys(buf, "i\<C-R>=0z\<CR>")
2157  call WaitForAssert({-> assert_match('^E976:', term_getline(buf, 5))}, 1000)
2158
2159  " ending Insert mode should put the cursor back on the ':'
2160  call term_sendkeys(buf, ":\<Esc>")
2161  call VerifyScreenDump(buf, 'Test_edit_ctlr_r_failed_1', {})
2162
2163  call StopVimInTerminal(buf)
2164endfunc
2165
2166" When a character is inserted at the last position of the last line in a
2167" window, the window contents should be scrolled one line up. If the top line
2168" is part of a fold, then the entire fold should be scrolled up.
2169func Test_edit_lastline_scroll()
2170  if has('linux')
2171    " TODO: For unknown reasons, this test fails on CI when run in Gui mode
2172    CheckNotGui
2173  endif
2174  new
2175  let h = winheight(0)
2176  let lines = ['one', 'two', 'three']
2177  let lines += repeat(['vim'], h - 4)
2178  call setline(1, lines)
2179  call setline(h, repeat('x', winwidth(0) - 1))
2180  call feedkeys("GAx", 'xt')
2181  redraw!
2182  call assert_equal(h - 1, winline())
2183  call assert_equal(2, line('w0'))
2184
2185  " scroll with a fold
2186  1,2fold
2187  normal gg
2188  call setline(h + 1, repeat('x', winwidth(0) - 1))
2189  call feedkeys("GAx", 'xt')
2190  redraw!
2191  call assert_equal(h - 1, winline())
2192  call assert_equal(3, line('w0'))
2193
2194  bw!
2195endfunc
2196
2197func Test_edit_browse()
2198  " in the GUI this opens a file picker, we only test the terminal behavior
2199  CheckNotGui
2200
2201  " ":browse xxx" checks for the FileExplorer augroup and assumes editing "."
2202  " works then.
2203  augroup FileExplorer
2204    au!
2205  augroup END
2206
2207  " When the CASE_INSENSITIVE_FILENAME is defined this used to cause a crash.
2208  browse enew
2209  bwipe!
2210
2211  browse split
2212  bwipe!
2213endfunc
2214
2215func Test_read_invalid()
2216  " set encoding=latin1
2217  " This was not properly checking for going past the end.
2218  call assert_fails('r`=', 'E484:')
2219  set encoding=utf-8
2220endfunc
2221
2222" Test for the 'revins' option
2223func Test_edit_revins()
2224  CheckFeature rightleft
2225  new
2226  set revins
2227  exe "normal! ione\ttwo three"
2228  call assert_equal("eerht owt\teno", getline(1))
2229  call setline(1, "one\ttwo three")
2230  normal! gg$bi a
2231  call assert_equal("one\ttwo a three", getline(1))
2232  exe "normal! $bi\<BS>\<BS>"
2233  call assert_equal("one\ttwo a ree", getline(1))
2234  exe "normal! 0wi\<C-W>"
2235  call assert_equal("one\t a ree", getline(1))
2236  exe "normal! 0wi\<C-U>"
2237  call assert_equal("one\t ", getline(1))
2238  " newline in insert mode starts at the end of the line
2239  call setline(1, 'one two three')
2240  exe "normal! wi\nfour"
2241  call assert_equal(['one two three', 'ruof'], getline(1, '$'))
2242  set backspace=indent,eol,start
2243  exe "normal! ggA\<BS>:"
2244  call assert_equal(['one two three:ruof'], getline(1, '$'))
2245  set revins& backspace&
2246  bw!
2247endfunc
2248
2249" Test for getting the character of the line below after "p"
2250func Test_edit_put_CTRL_E()
2251  " set encoding=latin1
2252  new
2253  let @" = ''
2254  sil! norm orggRx
2255  sil! norm pr
2256  call assert_equal(['r', 'r'], getline(1, 2))
2257  bwipe!
2258  set encoding=utf-8
2259endfunc
2260
2261" Test toggling of input method. See :help i_CTRL-^
2262func Test_edit_CTRL_hat()
2263  CheckFeature xim
2264
2265  " FIXME: test fails with Motif GUI.
2266  "        test also fails when running in the GUI.
2267  CheckFeature gui_gtk
2268  CheckNotGui
2269
2270  new
2271
2272  call assert_equal(0, &iminsert)
2273  call feedkeys("i\<C-^>", 'xt')
2274  call assert_equal(2, &iminsert)
2275  call feedkeys("i\<C-^>", 'xt')
2276  call assert_equal(0, &iminsert)
2277
2278  bwipe!
2279endfunc
2280
2281" Weird long file name was going over the end of NameBuff
2282func Test_edit_overlong_file_name()
2283  CheckUnix
2284
2285  file 0000000000000000000000000000
2286  file %%%%%%%%%%%%%%%%%%%%%%%%%%
2287  file %%%%%%
2288  set readonly
2289  set ls=2
2290
2291  redraw!
2292  set noreadonly ls&
2293  bwipe!
2294endfunc
2295
2296func Test_edit_shift_bs()
2297  CheckMSWindows
2298
2299  " FIXME: this works interactively, but the test fails
2300  throw 'Skipped: Shift-Backspace Test not working correctly :('
2301
2302  " Need to run this in Win32 Terminal, do not use CheckRunVimInTerminal
2303  if !has("terminal")
2304    return
2305  endif
2306
2307  " Shift Backspace should work like Backspace in insert mode
2308  let lines =<< trim END
2309    call setline(1, ['abc'])
2310  END
2311  call writefile(lines, 'Xtest_edit_shift_bs', 'D')
2312
2313  let buf = RunVimInTerminal('-S Xtest_edit_shift_bs', #{rows: 3})
2314  call term_sendkeys(buf, "A\<S-BS>-\<esc>")
2315  call TermWait(buf, 50)
2316  call assert_equal('ab-', term_getline(buf, 1))
2317
2318  " clean up
2319  call StopVimInTerminal(buf)
2320endfunc
2321
2322func Test_edit_Ctrl_RSB()
2323  new
2324  let g:triggered = []
2325  autocmd InsertCharPre <buffer> let g:triggered += [v:char]
2326
2327  " i_CTRL-] should not trigger InsertCharPre
2328  exe "normal! A\<C-]>"
2329  call assert_equal([], g:triggered)
2330
2331  " i_CTRL-] should expand abbreviations but not trigger InsertCharPre
2332  inoreabbr <buffer> f foo
2333  exe "normal! Af\<C-]>a"
2334  call assert_equal(['f', 'f', 'o', 'o', 'a'], g:triggered)
2335  call assert_equal('fooa', getline(1))
2336
2337  " CTRL-] followed by i_CTRL-V should not expand abbreviations
2338  " i_CTRL-V doesn't trigger InsertCharPre
2339  call setline(1, '')
2340  exe "normal! Af\<C-V>\<C-]>"
2341  call assert_equal("f\<C-]>", getline(1))
2342
2343  let g:triggered = []
2344  call setline(1, '')
2345
2346  " Also test assigning to v:char
2347  autocmd InsertCharPre <buffer> let v:char = 'f'
2348  exe "normal! Ag\<C-]>h"
2349  call assert_equal(['g', 'f', 'o', 'o', 'h'], g:triggered)
2350  call assert_equal('ffff', getline(1))
2351
2352  autocmd! InsertCharPre
2353  unlet g:triggered
2354  bwipe!
2355endfunc
2356
2357func s:check_backspace(expected)
2358  let g:actual = []
2359  inoremap <buffer> <F2> <Cmd>let g:actual += [getline('.')]<CR>
2360  set backspace=indent,eol,start
2361
2362  exe "normal i" .. repeat("\<BS>\<F2>", len(a:expected))
2363  call assert_equal(a:expected, g:actual)
2364
2365  set backspace&
2366  iunmap <buffer> <F2>
2367  unlet g:actual
2368endfunc
2369
2370" Test that backspace works with 'smarttab' and mixed Tabs and spaces.
2371func Test_edit_backspace_smarttab_mixed()
2372  set smarttab
2373  call NewWindow(1, 30)
2374  setlocal tabstop=4 shiftwidth=4
2375
2376  call setline(1, "\t    \t         \t a")
2377  normal! $
2378  call s:check_backspace([
2379        \ "\t    \t         \ta",
2380        \ "\t    \t        a",
2381        \ "\t    \t    a",
2382        \ "\t    \ta",
2383        \ "\t    a",
2384        \ "\ta",
2385        \ "a",
2386        \ ])
2387
2388  call CloseWindow()
2389  set smarttab&
2390endfunc
2391
2392" Test that backspace works with 'smarttab' and 'varsofttabstop'.
2393func Test_edit_backspace_smarttab_varsofttabstop()
2394  CheckFeature vartabs
2395
2396  set smarttab
2397  call NewWindow(1, 30)
2398  setlocal tabstop=8 varsofttabstop=6,2,5,3
2399
2400  call setline(1, "a\t    \t a")
2401  normal! $
2402  call s:check_backspace([
2403        \ "a\t    \ta",
2404        \ "a\t     a",
2405        \ "a\ta",
2406        \ "a     a",
2407        \ "aa",
2408        \ "a",
2409        \ ])
2410
2411  call CloseWindow()
2412  set smarttab&
2413endfunc
2414
2415" Test that backspace works with 'smarttab' when a Tab is shown as "^I".
2416func Test_edit_backspace_smarttab_list()
2417  set smarttab
2418  call NewWindow(1, 30)
2419  setlocal tabstop=4 shiftwidth=4 list listchars=
2420
2421  call setline(1, "\t    \t         \t a")
2422  normal! $
2423  call s:check_backspace([
2424        \ "\t    \t        a",
2425        \ "\t    \t    a",
2426        \ "\t    \ta",
2427        \ "\t  a",
2428        \ "a",
2429        \ ])
2430
2431  call CloseWindow()
2432  set smarttab&
2433endfunc
2434
2435" Test that backspace works with 'smarttab' and 'breakindent'.
2436func Test_edit_backspace_smarttab_breakindent()
2437  CheckFeature linebreak
2438
2439  set smarttab
2440  call NewWindow(3, 17)
2441  setlocal tabstop=4 shiftwidth=4 breakindent breakindentopt=min:5
2442
2443  call setline(1, "\t    \t         \t a")
2444  normal! $
2445  call s:check_backspace([
2446        \ "\t    \t         \ta",
2447        \ "\t    \t        a",
2448        \ "\t    \t    a",
2449        \ "\t    \ta",
2450        \ "\t    a",
2451        \ "\ta",
2452        \ "a",
2453        \ ])
2454
2455  call CloseWindow()
2456  set smarttab&
2457endfunc
2458
2459" Test that backspace works with 'smarttab' and virtual text.
2460func Test_edit_backspace_smarttab_virtual_text()
2461  CheckFeature textprop
2462
2463  set smarttab
2464  call NewWindow(1, 50)
2465  setlocal tabstop=4 shiftwidth=4
2466
2467  call setline(1, "\t    \t         \t a")
2468  call prop_type_add('theprop', {})
2469  call prop_add(1, 3, {'type': 'theprop', 'text': 'text'})
2470  normal! $
2471  call s:check_backspace([
2472        \ "\t    \t         \ta",
2473        \ "\t    \t        a",
2474        \ "\t    \t    a",
2475        \ "\t    \ta",
2476        \ "\t    a",
2477        \ "\ta",
2478        \ "a",
2479        \ ])
2480
2481  call CloseWindow()
2482  call prop_type_delete('theprop')
2483  set smarttab&
2484endfunc
2485
2486func Test_edit_CAR_with_completion()
2487  new
2488
2489  " With "noselect", behavior is the same with and without "noinsert":
2490  " Enter inserts a new line when no selection is done or after selecting with
2491  " Ctrl-N/P, but does not insert a new line when selecting with cursor keys.
2492  for cot in ['menu,menuone,noselect', 'menu,menuone,noselect,noinsert']
2493    let &cot = cot
2494    %delete
2495    call feedkeys("Shello hero\<CR>h\<C-X>\<C-N>e\<CR>", 'tx')
2496    call assert_equal(['hello hero', 'he', ''], getline(1, '$'))
2497    %delete
2498    call feedkeys("Shello hero\<CR>h\<C-X>\<C-N>\<CR>", 'tx')
2499    call assert_equal(['hello hero', 'h', ''], getline(1, '$'))
2500    %delete
2501    call feedkeys("Shello hero\<CR>h\<C-X>\<C-N>\<C-N>\<CR>", 'tx')
2502    call assert_equal(['hello hero', 'hello', ''], getline(1, '$'))
2503    %delete
2504    call feedkeys("Shello hero\<CR>h\<C-X>\<C-N>\<C-N>\<C-N>\<CR>", 'tx')
2505    call assert_equal(['hello hero', 'hero', ''], getline(1, '$'))
2506    %delete
2507    call feedkeys("Shello hero\<CR>h\<C-X>\<C-N>\<C-N>\<C-P>\<CR>", 'tx')
2508    call assert_equal(['hello hero', 'h', ''], getline(1, '$'))
2509    %delete
2510    call feedkeys("Shello hero\<CR>h\<C-X>\<C-N>\<Down>\<CR>", 'tx')
2511    call assert_equal(['hello hero', 'hello'], getline(1, '$'))
2512    %delete
2513    call feedkeys("Shello hero\<CR>h\<C-X>\<C-N>\<Down>\<Down>\<CR>", 'tx')
2514    call assert_equal(['hello hero', 'hero'], getline(1, '$'))
2515    %delete
2516    call feedkeys("Shello hero\<CR>h\<C-X>\<C-N>\<Down>\<Up>\<CR>", 'tx')
2517    call assert_equal(['hello hero', 'h'], getline(1, '$'))
2518  endfor
2519
2520  " With "noinsert" but not "noselect": like pressing <Down> after "noselect".
2521  set cot=menu,menuone,noinsert
2522  %delete
2523  call feedkeys("Shello hero\<CR>h\<C-X>\<C-N>e\<CR>", 'tx')
2524  call assert_equal(['hello hero', 'hello'], getline(1, '$'))
2525  %delete
2526  call feedkeys("Shello hero\<CR>h\<C-X>\<C-N>\<CR>", 'tx')
2527  call assert_equal(['hello hero', 'hello'], getline(1, '$'))
2528  %delete
2529  call feedkeys("Shello hero\<CR>h\<C-X>\<C-N>\<Down>\<CR>", 'tx')
2530  call assert_equal(['hello hero', 'hero'], getline(1, '$'))
2531  %delete
2532  call feedkeys("Shello hero\<CR>h\<C-X>\<C-N>\<Up>\<CR>", 'tx')
2533  call assert_equal(['hello hero', 'h'], getline(1, '$'))
2534  %delete
2535  call feedkeys("Shello hero\<CR>h\<C-X>\<C-N>\<C-N>\<CR>", 'tx')
2536  call assert_equal(['hello hero', 'hero', ''], getline(1, '$'))
2537  %delete
2538  call feedkeys("Shello hero\<CR>h\<C-X>\<C-N>\<C-P>\<CR>", 'tx')
2539  call assert_equal(['hello hero', 'h', ''], getline(1, '$'))
2540
2541  set cot&
2542  bw!
2543endfunc
2544
2545func Test_autoindent_no_strip_after_cmd_setline()
2546  new
2547  setlocal autoindent
2548  inoremap <buffer> <F2> <Cmd>call setline('.', 'v  v')<CR><Cmd>call cursor(line('.'), 2)<CR>
2549  call feedkeys("Go\<F2>\<Esc>", 'tx')
2550  call assert_equal('v  v', getline(2))
2551  bwipe!
2552endfunc
2553
2554func Test_autoindent_no_strip_after_cursorholdi()
2555  CheckFeature timers
2556  new
2557  setlocal autoindent
2558  set updatetime=50
2559  au CursorHoldI <buffer> call setline('.', 'v v')
2560  call setline(1, ' x')
2561  call cursor(1, 2)
2562  call timer_start(120, {-> feedkeys("\<Esc>", 't')})
2563  call feedkeys("o", 'tx!')
2564  call assert_equal('v v', getline(2))
2565  set updatetime&
2566  bwipe!
2567endfunc
2568
2569" Issue #20130: '[ must mark the start of the paste after CTRL-R CTRL-P + edit.
2570func Test_open_square_mark_after_ctrl_r_ctrl_p_paste()
2571  new
2572  call setline(1, ['a', 'b', 'c', 'd'])
2573  call cursor(4, 1)
2574
2575  call feedkeys("Vggyjo\<C-r>\<C-p>\"\<BS>\<Esc>", 'xt')
2576
2577  call assert_equal(['a', 'b', 'a', 'b', 'c', 'd', 'c', 'd'],
2578        \ getline(1, '$'))
2579  call assert_equal([0, 3, 1, 0], getpos("'["))
2580  bwipe!
2581endfunc
2582
2583func Test_autoindent_no_strip_cross_line()
2584  new
2585  setlocal autoindent
2586  inoremap <buffer> <F3> {}<Left><CR><Cmd>normal! ==<CR><Up><End><CR>
2587
2588  call setline(1, '')
2589  call feedkeys("i\<F3>\<Esc>", 'tx')
2590
2591  call assert_equal('{', getline(1))
2592  call assert_equal('', getline(2))
2593  call assert_equal('}', getline(3))
2594  call assert_equal([0, 2, 1, 0], getpos('.'))
2595
2596  " Overwrite @. register with simple content to avoid affecting later tests.
2597  call feedkeys("Go\<Esc>", 'tnix')
2598  bwipe!
2599endfunc
2600
2601" vim: shiftwidth=2 sts=2 expandtab