blob: f0bf5cc5f6c32b1bd41aaa9d15d8ebbe095680aa [file] [log] [blame]
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001" Test for edit functions
Bram Moolenaar215ba3b2019-11-06 15:07:07 +01002
Bram Moolenaareb992cb2017-03-09 18:20:16 +01003if exists("+t_kD")
4 let &t_kD="[3;*~"
5endif
Bram Moolenaareb992cb2017-03-09 18:20:16 +01006
Bram Moolenaar215ba3b2019-11-06 15:07:07 +01007source check.vim
8
Bram Moolenaareb992cb2017-03-09 18:20:16 +01009" 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
Bram Moolenaar1e115362019-01-09 23:01:02 +010014func Test_edit_00b()
Bram Moolenaareb992cb2017-03-09 18:20:16 +010015 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
Bram Moolenaar1e115362019-01-09 23:01:02 +010026func Test_edit_01()
Bram Moolenaareb992cb2017-03-09 18:20:16 +010027 " set for Travis CI?
28 " set nocp noesckeys
29 new
Bram Moolenaareb992cb2017-03-09 18:20:16 +010030 " 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
Bram Moolenaar30276f22019-01-24 17:59:39 +010042 call setline(1, "\u0401")
43 call feedkeys("i\<del>\<esc>", 'tnix')
44 call assert_equal([''], getline(1,'$'))
45 %d
Bram Moolenaareb992cb2017-03-09 18:20:16 +010046 " 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))
Bram Moolenaareb992cb2017-03-09 18:20:16 +010060 let &bs=_bs
61 bw!
62endfunc
63
Bram Moolenaar1e115362019-01-09 23:01:02 +010064func Test_edit_02()
Bram Moolenaareb992cb2017-03-09 18:20:16 +010065 " 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
Bram Moolenaar1e115362019-01-09 23:01:02 +0100104func Test_edit_03()
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100105 " 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
Bram Moolenaar1e115362019-01-09 23:01:02 +0100123func Test_edit_04()
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100124 " 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
Bram Moolenaar1e115362019-01-09 23:01:02 +0100135func Test_edit_05()
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100136 " 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
Bram Moolenaar1e115362019-01-09 23:01:02 +0100157func Test_edit_06()
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100158 " 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
Bram Moolenaar1e115362019-01-09 23:01:02 +0100179func Test_edit_07()
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100180 " 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')
Bram Moolenaar4c313b12019-08-24 22:58:31 +0200204 call assert_equal(["Jan\<c-l>",''], 1->getline('$'))
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100205 %d
206 call setline(1, 'J')
207 call feedkeys("A\<f5>\<c-p>u\<down>\<c-l>\<cr>", 'tx')
Bram Moolenaar4c313b12019-08-24 22:58:31 +0200208 call assert_equal(["January"], 1->getline('$'))
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100209
210 delfu ListMonths
211 delfu DoIt
212 iunmap <buffer> <f5>
213 bw!
214endfunc
215
Bram Moolenaar1e115362019-01-09 23:01:02 +0100216func Test_edit_08()
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100217 " reset insertmode from i_ctrl-r_=
Bram Moolenaar2a45d642017-10-27 01:35:00 +0200218 let g:bufnr = bufnr('%')
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100219 new
220 call setline(1, ['abc'])
221 call cursor(1, 4)
Bram Moolenaar2a45d642017-10-27 01:35:00 +0200222 call feedkeys(":set im\<cr>ZZZ\<c-r>=setbufvar(g:bufnr,'&im', 0)\<cr>",'tnix')
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100223 call assert_equal(['abZZZc'], getline(1,'$'))
224 call assert_equal([0, 1, 1, 0], getpos('.'))
225 call assert_false(0, '&im')
226 bw!
Bram Moolenaar2a45d642017-10-27 01:35:00 +0200227 unlet g:bufnr
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100228endfunc
229
Bram Moolenaar1e115362019-01-09 23:01:02 +0100230func Test_edit_09()
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100231 " 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
Bram Moolenaar1e115362019-01-09 23:01:02 +0100260func Test_edit_11()
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100261 " Test that indenting kicks in
262 new
263 set cindent
264 call setline(1, ['{', '', ''])
265 call cursor(2, 1)
266 call feedkeys("i\<c-f>int c;\<esc>", 'tnix')
267 call cursor(3, 1)
Bram Moolenaar1671f442020-03-10 07:48:13 +0100268 call feedkeys("\<Insert>/* comment */", 'tnix')
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100269 call assert_equal(['{', "\<tab>int c;", "/* comment */"], getline(1, '$'))
270 " added changed cindentkeys slightly
271 set cindent cinkeys+=*/
272 call setline(1, ['{', '', ''])
273 call cursor(2, 1)
274 call feedkeys("i\<c-f>int c;\<esc>", 'tnix')
275 call cursor(3, 1)
276 call feedkeys("i/* comment */", 'tnix')
277 call assert_equal(['{', "\<tab>int c;", "\<tab>/* comment */"], getline(1, '$'))
278 set cindent cinkeys+==end
279 call feedkeys("oend\<cr>\<esc>", 'tnix')
280 call assert_equal(['{', "\<tab>int c;", "\<tab>/* comment */", "\tend", ''], getline(1, '$'))
281 set cinkeys-==end
282 %d
283 " Use indentexpr instead of cindenting
284 func! Do_Indent()
285 if v:lnum == 3
286 return 3*shiftwidth()
287 else
288 return 2*shiftwidth()
289 endif
290 endfunc
291 setl indentexpr=Do_Indent() indentkeys+=*/
292 call setline(1, ['{', '', ''])
293 call cursor(2, 1)
294 call feedkeys("i\<c-f>int c;\<esc>", 'tnix')
295 call cursor(3, 1)
296 call feedkeys("i/* comment */", 'tnix')
297 call assert_equal(['{', "\<tab>\<tab>int c;", "\<tab>\<tab>\<tab>/* comment */"], getline(1, '$'))
298 set cinkeys&vim indentkeys&vim
299 set nocindent indentexpr=
300 delfu Do_Indent
301 bw!
302endfunc
303
Bram Moolenaar1e115362019-01-09 23:01:02 +0100304func Test_edit_11_indentexpr()
Bram Moolenaar1b383442017-09-26 20:04:54 +0200305 " Test that indenting kicks in
306 new
307 " Use indentexpr instead of cindenting
308 func! Do_Indent()
309 let pline=prevnonblank(v:lnum)
310 if empty(getline(v:lnum))
311 if getline(pline) =~ 'if\|then'
312 return shiftwidth()
313 else
314 return 0
315 endif
316 else
317 return 0
318 endif
319 endfunc
320 setl indentexpr=Do_Indent() indentkeys+=0=then,0=fi
321 call setline(1, ['if [ $this ]'])
322 call cursor(1, 1)
323 call feedkeys("othen\<cr>that\<cr>fi", 'tnix')
324 call assert_equal(['if [ $this ]', "then", "\<tab>that", "fi"], getline(1, '$'))
325 set cinkeys&vim indentkeys&vim
326 set nocindent indentexpr=
327 delfu Do_Indent
328 bw!
329endfunc
330
Bram Moolenaar1e115362019-01-09 23:01:02 +0100331func Test_edit_12()
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100332 " Test changing indent in replace mode
333 new
334 call setline(1, ["\tabc", "\tdef"])
335 call cursor(2, 4)
336 call feedkeys("R^\<c-d>", 'tnix')
337 call assert_equal(["\tabc", "def"], getline(1, '$'))
Bram Moolenaar4c313b12019-08-24 22:58:31 +0200338 call assert_equal([0, 2, 2, 0], '.'->getpos())
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100339 %d
340 call setline(1, ["\tabc", "\t\tdef"])
341 call cursor(2, 2)
342 call feedkeys("R^\<c-d>", 'tnix')
343 call assert_equal(["\tabc", "def"], getline(1, '$'))
344 call assert_equal([0, 2, 1, 0], getpos('.'))
345 %d
346 call setline(1, ["\tabc", "\t\tdef"])
347 call cursor(2, 2)
348 call feedkeys("R\<c-t>", 'tnix')
349 call assert_equal(["\tabc", "\t\t\tdef"], getline(1, '$'))
350 call assert_equal([0, 2, 2, 0], getpos('.'))
351 bw!
352 10vnew
353 call setline(1, ["\tabc", "\t\tdef"])
354 call cursor(2, 2)
355 call feedkeys("R\<c-t>", 'tnix')
356 call assert_equal(["\tabc", "\t\t\tdef"], getline(1, '$'))
357 call assert_equal([0, 2, 2, 0], getpos('.'))
358 %d
359 set sw=4
360 call setline(1, ["\tabc", "\t\tdef"])
361 call cursor(2, 2)
362 call feedkeys("R\<c-t>\<c-t>", 'tnix')
363 call assert_equal(["\tabc", "\t\t\tdef"], getline(1, '$'))
364 call assert_equal([0, 2, 2, 0], getpos('.'))
365 %d
366 call setline(1, ["\tabc", "\t\tdef"])
367 call cursor(2, 2)
368 call feedkeys("R\<c-t>\<c-t>", 'tnix')
369 call assert_equal(["\tabc", "\t\t\tdef"], getline(1, '$'))
370 call assert_equal([0, 2, 2, 0], getpos('.'))
371 set et
372 set sw& et&
373 %d
374 call setline(1, ["\t/*"])
375 set formatoptions=croql
376 call cursor(1, 3)
377 call feedkeys("A\<cr>\<cr>/", 'tnix')
378 call assert_equal(["\t/*", " *", " */"], getline(1, '$'))
379 set formatoptions&
380 bw!
381endfunc
382
Bram Moolenaar1e115362019-01-09 23:01:02 +0100383func Test_edit_13()
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100384 " Test smartindenting
385 if exists("+smartindent")
386 new
387 set smartindent autoindent
388 call setline(1, ["\tabc"])
389 call feedkeys("A {\<cr>more\<cr>}\<esc>", 'tnix')
390 call assert_equal(["\tabc {", "\t\tmore", "\t}"], getline(1, '$'))
391 set smartindent& autoindent&
Bram Moolenaar2ba42382019-03-16 18:11:07 +0100392 bwipe!
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100393 endif
Bram Moolenaar2ba42382019-03-16 18:11:07 +0100394
395 " Test autoindent removing indent of blank line.
396 new
397 call setline(1, ' foo bar baz')
398 set autoindent
399 exe "normal 0eea\<CR>\<CR>\<Esc>"
400 call assert_equal(" foo bar", getline(1))
401 call assert_equal("", getline(2))
402 call assert_equal(" baz", getline(3))
403 set autoindent&
404 bwipe!
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100405endfunc
406
Bram Moolenaar1e115362019-01-09 23:01:02 +0100407func Test_edit_CR()
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100408 " Test for <CR> in insert mode
409 " basically only in quickfix mode ist tested, the rest
410 " has been taken care of by other tests
411 if !has("quickfix")
412 return
413 endif
414 botright new
415 call writefile(range(1, 10), 'Xqflist.txt')
416 call setqflist([{'filename': 'Xqflist.txt', 'lnum': 2}])
417 copen
418 set modifiable
419 call feedkeys("A\<cr>", 'tnix')
420 call assert_equal('Xqflist.txt', bufname(''))
421 call assert_equal(2, line('.'))
422 cclose
423 botright new
424 call setloclist(0, [{'filename': 'Xqflist.txt', 'lnum': 10}])
425 lopen
426 set modifiable
427 call feedkeys("A\<cr>", 'tnix')
428 call assert_equal('Xqflist.txt', bufname(''))
429 call assert_equal(10, line('.'))
430 call feedkeys("A\<Enter>", 'tnix')
431 call feedkeys("A\<kEnter>", 'tnix')
432 call feedkeys("A\n", 'tnix')
433 call feedkeys("A\r", 'tnix')
434 call assert_equal(map(range(1, 10), 'string(v:val)') + ['', '', '', ''], getline(1, '$'))
435 bw!
436 lclose
437 call delete('Xqflist.txt')
438endfunc
439
Bram Moolenaar1e115362019-01-09 23:01:02 +0100440func Test_edit_CTRL_()
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100441 " disabled for Windows builds, why?
Bram Moolenaar30276f22019-01-24 17:59:39 +0100442 if !has("rightleft") || has("win32")
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100443 return
444 endif
445 let _encoding=&encoding
446 set encoding=utf-8
447 " Test for CTRL-_
448 new
449 call setline(1, ['abc'])
450 call cursor(1, 1)
451 call feedkeys("i\<c-_>xyz\<esc>", 'tnix')
452 call assert_equal(["\<C-_>xyzabc"], getline(1, '$'))
453 call assert_false(&revins)
454 set ari
455 call setline(1, ['abc'])
456 call cursor(1, 1)
457 call feedkeys("i\<c-_>xyz\<esc>", 'tnix')
458 call assert_equal(["æèñabc"], getline(1, '$'))
459 call assert_true(&revins)
460 call setline(1, ['abc'])
461 call cursor(1, 1)
462 call feedkeys("i\<c-_>xyz\<esc>", 'tnix')
463 call assert_equal(["xyzabc"], getline(1, '$'))
464 call assert_false(&revins)
465 set noari
466 let &encoding=_encoding
467 bw!
468endfunc
469
470" needs to come first, to have the @. register empty
Bram Moolenaar1e115362019-01-09 23:01:02 +0100471func Test_edit_00a_CTRL_A()
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100472 " Test pressing CTRL-A
473 new
474 call setline(1, repeat([''], 5))
475 call cursor(1, 1)
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100476 try
477 call feedkeys("A\<NUL>", 'tnix')
478 catch /^Vim\%((\a\+)\)\=:E29/
479 call assert_true(1, 'E29 error caught')
480 endtry
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100481 call cursor(1, 1)
482 call feedkeys("Afoobar \<esc>", 'tnix')
483 call cursor(2, 1)
484 call feedkeys("A\<c-a>more\<esc>", 'tnix')
485 call cursor(3, 1)
486 call feedkeys("A\<NUL>and more\<esc>", 'tnix')
487 call assert_equal(['foobar ', 'foobar more', 'foobar morend more', '', ''], getline(1, '$'))
488 bw!
489endfunc
490
Bram Moolenaar1e115362019-01-09 23:01:02 +0100491func Test_edit_CTRL_EY()
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100492 " Ctrl-E/ Ctrl-Y in insert mode completion to scroll
493 10new
494 call setline(1, range(1, 100))
495 call cursor(30, 1)
496 norm! z.
497 call feedkeys("A\<c-x>\<c-e>\<c-e>\<c-e>\<c-e>\<c-e>", 'tnix')
498 call assert_equal(30, winsaveview()['topline'])
499 call assert_equal([0, 30, 2, 0], getpos('.'))
500 call feedkeys("A\<c-x>\<c-e>\<c-e>\<c-e>\<c-e>\<c-e>", 'tnix')
501 call feedkeys("A\<c-x>".repeat("\<c-y>", 10), 'tnix')
502 call assert_equal(21, winsaveview()['topline'])
503 call assert_equal([0, 30, 2, 0], getpos('.'))
504 bw!
505endfunc
506
Bram Moolenaar1e115362019-01-09 23:01:02 +0100507func Test_edit_CTRL_G()
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100508 new
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100509 call setline(1, ['foobar', 'foobar', 'foobar'])
510 call cursor(2, 4)
511 call feedkeys("ioooooooo\<c-g>k\<c-r>.\<esc>", 'tnix')
512 call assert_equal(['foooooooooobar', 'foooooooooobar', 'foobar'], getline(1, '$'))
513 call assert_equal([0, 1, 11, 0], getpos('.'))
514 call feedkeys("i\<c-g>k\<esc>", 'tnix')
515 call assert_equal([0, 1, 10, 0], getpos('.'))
516 call cursor(2, 4)
517 call feedkeys("i\<c-g>jzzzz\<esc>", 'tnix')
518 call assert_equal(['foooooooooobar', 'foooooooooobar', 'foozzzzbar'], getline(1, '$'))
519 call assert_equal([0, 3, 7, 0], getpos('.'))
520 call feedkeys("i\<c-g>j\<esc>", 'tnix')
521 call assert_equal([0, 3, 6, 0], getpos('.'))
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100522 bw!
523endfunc
524
Bram Moolenaar1e115362019-01-09 23:01:02 +0100525func Test_edit_CTRL_I()
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100526 " Tab in completion mode
527 let path=expand("%:p:h")
528 new
Bram Moolenaarca851592018-06-06 21:04:07 +0200529 call setline(1, [path. "/", ''])
Bram Moolenaarc5379472017-03-16 22:38:00 +0100530 call feedkeys("Arunt\<c-x>\<c-f>\<tab>\<cr>\<esc>", 'tnix')
531 call assert_match('runtest\.vim', getline(1))
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100532 %d
533 call writefile(['one', 'two', 'three'], 'Xinclude.txt')
534 let include='#include Xinclude.txt'
535 call setline(1, [include, ''])
536 call cursor(2, 1)
537 call feedkeys("A\<c-x>\<tab>\<cr>\<esc>", 'tnix')
538 call assert_equal([include, 'one', ''], getline(1, '$'))
539 call feedkeys("2ggC\<c-x>\<tab>\<down>\<cr>\<esc>", 'tnix')
540 call assert_equal([include, 'two', ''], getline(1, '$'))
541 call feedkeys("2ggC\<c-x>\<tab>\<down>\<down>\<cr>\<esc>", 'tnix')
542 call assert_equal([include, 'three', ''], getline(1, '$'))
543 call feedkeys("2ggC\<c-x>\<tab>\<down>\<down>\<down>\<cr>\<esc>", 'tnix')
544 call assert_equal([include, '', ''], getline(1, '$'))
545 call delete("Xinclude.txt")
546 bw!
547endfunc
548
Bram Moolenaar1e115362019-01-09 23:01:02 +0100549func Test_edit_CTRL_K()
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100550 " Test pressing CTRL-K (basically only dictionary completion and digraphs
551 " the rest is already covered
552 call writefile(['A', 'AA', 'AAA', 'AAAA'], 'Xdictionary.txt')
553 set dictionary=Xdictionary.txt
554 new
555 call setline(1, 'A')
556 call cursor(1, 1)
557 call feedkeys("A\<c-x>\<c-k>\<cr>\<esc>", 'tnix')
558 call assert_equal(['AA', ''], getline(1, '$'))
559 %d
560 call setline(1, 'A')
561 call cursor(1, 1)
562 call feedkeys("A\<c-x>\<c-k>\<down>\<cr>\<esc>", 'tnix')
563 call assert_equal(['AAA'], getline(1, '$'))
564 %d
565 call setline(1, 'A')
566 call cursor(1, 1)
567 call feedkeys("A\<c-x>\<c-k>\<down>\<down>\<cr>\<esc>", 'tnix')
568 call assert_equal(['AAAA'], getline(1, '$'))
569 %d
570 call setline(1, 'A')
571 call cursor(1, 1)
572 call feedkeys("A\<c-x>\<c-k>\<down>\<down>\<down>\<cr>\<esc>", 'tnix')
573 call assert_equal(['A'], getline(1, '$'))
574 %d
575 call setline(1, 'A')
576 call cursor(1, 1)
577 call feedkeys("A\<c-x>\<c-k>\<down>\<down>\<down>\<down>\<cr>\<esc>", 'tnix')
578 call assert_equal(['AA'], getline(1, '$'))
579
Bram Moolenaar4b96df52020-01-26 22:00:26 +0100580 " press an unexpected key after dictionary completion
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100581 %d
582 call setline(1, 'A')
583 call cursor(1, 1)
584 call feedkeys("A\<c-x>\<c-k>\<c-]>\<cr>\<esc>", 'tnix')
585 call assert_equal(['AA', ''], getline(1, '$'))
586 %d
587 call setline(1, 'A')
588 call cursor(1, 1)
589 call feedkeys("A\<c-x>\<c-k>\<c-s>\<cr>\<esc>", 'tnix')
590 call assert_equal(["AA\<c-s>", ''], getline(1, '$'))
591 %d
592 call setline(1, 'A')
593 call cursor(1, 1)
594 call feedkeys("A\<c-x>\<c-k>\<c-f>\<cr>\<esc>", 'tnix')
595 call assert_equal(["AA\<c-f>", ''], getline(1, '$'))
596
597 set dictionary=
598 %d
599 call setline(1, 'A')
600 call cursor(1, 1)
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100601 let v:testing = 1
602 try
603 call feedkeys("A\<c-x>\<c-k>\<esc>", 'tnix')
604 catch
605 " error sleeps 2 seconds, when v:testing is not set
606 let v:testing = 0
607 endtry
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100608 call delete('Xdictionary.txt')
609
Bram Moolenaar30276f22019-01-24 17:59:39 +0100610 call test_override("char_avail", 1)
611 set showcmd
612 %d
613 call feedkeys("A\<c-k>a:\<esc>", 'tnix')
614 call assert_equal(['ä'], getline(1, '$'))
615 call test_override("char_avail", 0)
616 set noshowcmd
617
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100618 bw!
619endfunc
620
Bram Moolenaar1e115362019-01-09 23:01:02 +0100621func Test_edit_CTRL_L()
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100622 " Test Ctrl-X Ctrl-L (line completion)
623 new
624 set complete=.
625 call setline(1, ['one', 'two', 'three', '', '', '', ''])
626 call cursor(4, 1)
627 call feedkeys("A\<c-x>\<c-l>\<esc>", 'tnix')
628 call assert_equal(['one', 'two', 'three', 'three', '', '', ''], getline(1, '$'))
629 call feedkeys("cct\<c-x>\<c-l>\<c-n>\<esc>", 'tnix')
630 call assert_equal(['one', 'two', 'three', 't', '', '', ''], getline(1, '$'))
631 call feedkeys("cct\<c-x>\<c-l>\<c-n>\<c-n>\<esc>", 'tnix')
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100632 call assert_equal(['one', 'two', 'three', 'two', '', '', ''], getline(1, '$'))
Bram Moolenaar02ae9b42018-02-09 15:06:02 +0100633 call feedkeys("cct\<c-x>\<c-l>\<c-n>\<c-n>\<c-n>\<esc>", 'tnix')
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100634 call assert_equal(['one', 'two', 'three', 'three', '', '', ''], getline(1, '$'))
Bram Moolenaar02ae9b42018-02-09 15:06:02 +0100635 call feedkeys("cct\<c-x>\<c-l>\<c-n>\<c-n>\<c-n>\<c-n>\<esc>", 'tnix')
636 call assert_equal(['one', 'two', 'three', 't', '', '', ''], getline(1, '$'))
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100637 call feedkeys("cct\<c-x>\<c-l>\<c-p>\<esc>", 'tnix')
638 call assert_equal(['one', 'two', 'three', 'two', '', '', ''], getline(1, '$'))
639 call feedkeys("cct\<c-x>\<c-l>\<c-p>\<c-p>\<esc>", 'tnix')
640 call assert_equal(['one', 'two', 'three', 't', '', '', ''], getline(1, '$'))
641 call feedkeys("cct\<c-x>\<c-l>\<c-p>\<c-p>\<c-p>\<esc>", 'tnix')
642 call assert_equal(['one', 'two', 'three', 'three', '', '', ''], getline(1, '$'))
643 set complete=
644 call cursor(5, 1)
645 call feedkeys("A\<c-x>\<c-l>\<c-p>\<c-n>\<esc>", 'tnix')
646 call assert_equal(['one', 'two', 'three', 'three', "\<c-l>\<c-p>\<c-n>", '', ''], getline(1, '$'))
647 set complete&
648 %d
649 if has("conceal") && has("syntax")
650 call setline(1, ['foo', 'bar', 'foobar'])
651 call test_override("char_avail", 1)
652 set conceallevel=2 concealcursor=n
653 syn on
654 syn match ErrorMsg "^bar"
655 call matchadd("Conceal", 'oo', 10, -1, {'conceal': 'X'})
656 func! DoIt()
657 let g:change=1
658 endfunc
659 au! TextChangedI <buffer> :call DoIt()
660
661 call cursor(2, 1)
662 call assert_false(exists("g:change"))
663 call feedkeys("A \<esc>", 'tnix')
664 call assert_equal(['foo', 'bar ', 'foobar'], getline(1, '$'))
665 call assert_equal(1, g:change)
666
667 call test_override("char_avail", 0)
668 call clearmatches()
669 syn off
670 au! TextChangedI
671 delfu DoIt
672 unlet! g:change
673 endif
674 bw!
675endfunc
676
Bram Moolenaar1e115362019-01-09 23:01:02 +0100677func Test_edit_CTRL_N()
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100678 " Check keyword completion
679 new
680 set complete=.
681 call setline(1, ['INFER', 'loWER', '', '', ])
682 call cursor(3, 1)
683 call feedkeys("Ai\<c-n>\<cr>\<esc>", "tnix")
684 call feedkeys("ILO\<c-n>\<cr>\<esc>", 'tnix')
685 call assert_equal(['INFER', 'loWER', 'i', 'LO', '', ''], getline(1, '$'))
686 %d
687 call setline(1, ['INFER', 'loWER', '', '', ])
688 call cursor(3, 1)
689 set ignorecase infercase
690 call feedkeys("Ii\<c-n>\<cr>\<esc>", "tnix")
691 call feedkeys("ILO\<c-n>\<cr>\<esc>", 'tnix')
692 call assert_equal(['INFER', 'loWER', 'infer', 'LOWER', '', ''], getline(1, '$'))
693
694 set noignorecase noinfercase complete&
695 bw!
696endfunc
697
Bram Moolenaar1e115362019-01-09 23:01:02 +0100698func Test_edit_CTRL_O()
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100699 " Check for CTRL-O in insert mode
700 new
701 inoreabbr <buffer> h here some more
702 call setline(1, ['abc', 'def'])
703 call cursor(1, 1)
704 " Ctrl-O after an abbreviation
705 exe "norm A h\<c-o>:set nu\<cr> text"
706 call assert_equal(['abc here some more text', 'def'], getline(1, '$'))
707 call assert_true(&nu)
708 set nonu
709 iunabbr <buffer> h
710 " Ctrl-O at end of line with 've'=onemore
711 call cursor(1, 1)
712 call feedkeys("A\<c-o>:let g:a=getpos('.')\<cr>\<esc>", 'tnix')
713 call assert_equal([0, 1, 23, 0], g:a)
714 call cursor(1, 1)
715 set ve=onemore
716 call feedkeys("A\<c-o>:let g:a=getpos('.')\<cr>\<esc>", 'tnix')
717 call assert_equal([0, 1, 24, 0], g:a)
718 set ve=
719 unlet! g:a
720 bw!
721endfunc
722
Bram Moolenaar1e115362019-01-09 23:01:02 +0100723func Test_edit_CTRL_R()
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100724 " Insert Register
725 new
726 call test_override("ALL", 1)
727 set showcmd
728 call feedkeys("AFOOBAR eins zwei\<esc>", 'tnix')
729 call feedkeys("O\<c-r>.", 'tnix')
730 call feedkeys("O\<c-r>=10*500\<cr>\<esc>", 'tnix')
731 call feedkeys("O\<c-r>=getreg('=', 1)\<cr>\<esc>", 'tnix')
732 call assert_equal(["getreg('=', 1)", '5000', "FOOBAR eins zwei", "FOOBAR eins zwei"], getline(1, '$'))
733 call test_override("ALL", 0)
734 set noshowcmd
735 bw!
736endfunc
737
Bram Moolenaar1e115362019-01-09 23:01:02 +0100738func Test_edit_CTRL_S()
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100739 " Test pressing CTRL-S (basically only spellfile completion)
740 " the rest is already covered
741 new
742 if !has("spell")
743 call setline(1, 'vim')
744 call feedkeys("A\<c-x>ss\<cr>\<esc>", 'tnix')
745 call assert_equal(['vims', ''], getline(1, '$'))
746 bw!
747 return
748 endif
749 call setline(1, 'vim')
750 " spell option not yet set
751 try
752 call feedkeys("A\<c-x>\<c-s>\<cr>\<esc>", 'tnix')
753 catch /^Vim\%((\a\+)\)\=:E756/
754 call assert_true(1, 'error caught')
755 endtry
756 call assert_equal(['vim', ''], getline(1, '$'))
757 %d
758 setl spell spelllang=en
759 call setline(1, 'vim')
760 call cursor(1, 1)
761 call feedkeys("A\<c-x>\<c-s>\<cr>\<esc>", 'tnix')
762 call assert_equal(['Vim', ''], getline(1, '$'))
763 %d
764 call setline(1, 'vim')
765 call cursor(1, 1)
766 call feedkeys("A\<c-x>\<c-s>\<down>\<cr>\<esc>", 'tnix')
767 call assert_equal(['Aim'], getline(1, '$'))
768 %d
769 call setline(1, 'vim')
770 call cursor(1, 1)
771 call feedkeys("A\<c-x>\<c-s>\<c-p>\<cr>\<esc>", 'tnix')
772 call assert_equal(['vim', ''], getline(1, '$'))
773 %d
774 " empty buffer
775 call cursor(1, 1)
776 call feedkeys("A\<c-x>\<c-s>\<c-p>\<cr>\<esc>", 'tnix')
777 call assert_equal(['', ''], getline(1, '$'))
778 setl nospell
779 bw!
780endfunc
781
Bram Moolenaar1e115362019-01-09 23:01:02 +0100782func Test_edit_CTRL_T()
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100783 " Check for CTRL-T and CTRL-X CTRL-T in insert mode
784 " 1) increase indent
785 new
786 call setline(1, "abc")
787 call cursor(1, 1)
788 call feedkeys("A\<c-t>xyz", 'tnix')
789 call assert_equal(["\<tab>abcxyz"], getline(1, '$'))
790 " 2) also when paste option is set
791 set paste
792 call setline(1, "abc")
793 call cursor(1, 1)
794 call feedkeys("A\<c-t>xyz", 'tnix')
795 call assert_equal(["\<tab>abcxyz"], getline(1, '$'))
796 set nopaste
797 " CTRL-X CTRL-T (thesaurus complete)
798 call writefile(['angry furious mad enraged'], 'Xthesaurus')
799 set thesaurus=Xthesaurus
800 call setline(1, 'mad')
801 call cursor(1, 1)
802 call feedkeys("A\<c-x>\<c-t>\<cr>\<esc>", 'tnix')
803 call assert_equal(['mad', ''], getline(1, '$'))
804 %d
805 call setline(1, 'mad')
806 call cursor(1, 1)
807 call feedkeys("A\<c-x>\<c-t>\<c-n>\<cr>\<esc>", 'tnix')
808 call assert_equal(['angry', ''], getline(1, '$'))
809 %d
810 call setline(1, 'mad')
811 call cursor(1, 1)
812 call feedkeys("A\<c-x>\<c-t>\<c-n>\<c-n>\<cr>\<esc>", 'tnix')
813 call assert_equal(['furious', ''], getline(1, '$'))
814 %d
815 call setline(1, 'mad')
816 call cursor(1, 1)
817 call feedkeys("A\<c-x>\<c-t>\<c-n>\<c-n>\<c-n>\<cr>\<esc>", 'tnix')
818 call assert_equal(['enraged', ''], getline(1, '$'))
819 %d
820 call setline(1, 'mad')
821 call cursor(1, 1)
822 call feedkeys("A\<c-x>\<c-t>\<c-n>\<c-n>\<c-n>\<c-n>\<cr>\<esc>", 'tnix')
823 call assert_equal(['mad', ''], getline(1, '$'))
824 %d
825 call setline(1, 'mad')
826 call cursor(1, 1)
827 call feedkeys("A\<c-x>\<c-t>\<c-n>\<c-n>\<c-n>\<c-n>\<c-n>\<cr>\<esc>", 'tnix')
828 call assert_equal(['mad', ''], getline(1, '$'))
829 " Using <c-p> <c-n> when 'complete' is empty
830 set complete=
831 %d
832 call setline(1, 'mad')
833 call cursor(1, 1)
834 call feedkeys("A\<c-x>\<c-t>\<c-n>\<cr>\<esc>", 'tnix')
835 call assert_equal(['angry', ''], getline(1, '$'))
836 %d
837 call setline(1, 'mad')
838 call cursor(1, 1)
839 call feedkeys("A\<c-x>\<c-t>\<c-p>\<cr>\<esc>", 'tnix')
840 call assert_equal(['mad', ''], getline(1, '$'))
841 set complete&
842
843 set thesaurus=
844 %d
845 call setline(1, 'mad')
846 call cursor(1, 1)
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100847 let v:testing = 1
848 try
849 call feedkeys("A\<c-x>\<c-t>\<esc>", 'tnix')
850 catch
851 " error sleeps 2 seconds, when v:testing is not set
852 let v:testing = 0
853 endtry
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100854 call assert_equal(['mad'], getline(1, '$'))
855 call delete('Xthesaurus')
856 bw!
857endfunc
858
Bram Moolenaar1e115362019-01-09 23:01:02 +0100859func Test_edit_CTRL_U()
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100860 " Test 'completefunc'
861 new
862 " -1, -2 and -3 are special return values
863 let g:special=0
864 fun! CompleteMonths(findstart, base)
865 if a:findstart
866 " locate the start of the word
867 return g:special
868 else
869 " find months matching with "a:base"
870 let res = []
871 for m in split("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec")
872 if m =~ '^\c'.a:base
873 call add(res, {'word': m, 'abbr': m.' Month', 'icase': 0})
874 endif
875 endfor
876 return {'words': res, 'refresh': 'always'}
877 endif
878 endfun
879 set completefunc=CompleteMonths
880 call setline(1, ['', ''])
881 call cursor(1, 1)
882 call feedkeys("AX\<c-x>\<c-u>\<cr>\<esc>", 'tnix')
883 call assert_equal(['X', '', ''], getline(1, '$'))
884 %d
885 let g:special=-1
886 call feedkeys("AX\<c-x>\<c-u>\<cr>\<esc>", 'tnix')
887 call assert_equal(['XJan', ''], getline(1, '$'))
888 %d
889 let g:special=-2
890 call feedkeys("AX\<c-x>\<c-u>\<cr>\<esc>", 'tnix')
891 call assert_equal(['X', ''], getline(1, '$'))
892 %d
893 let g:special=-3
894 call feedkeys("AX\<c-x>\<c-u>\<cr>\<esc>", 'tnix')
895 call assert_equal(['X', ''], getline(1, '$'))
896 %d
897 let g:special=0
898 call feedkeys("AM\<c-x>\<c-u>\<cr>\<esc>", 'tnix')
899 call assert_equal(['Mar', ''], getline(1, '$'))
900 %d
901 call feedkeys("AM\<c-x>\<c-u>\<c-n>\<cr>\<esc>", 'tnix')
902 call assert_equal(['May', ''], getline(1, '$'))
903 %d
904 call feedkeys("AM\<c-x>\<c-u>\<c-n>\<c-n>\<cr>\<esc>", 'tnix')
905 call assert_equal(['M', ''], getline(1, '$'))
906 delfu CompleteMonths
907 %d
908 try
909 call feedkeys("A\<c-x>\<c-u>", 'tnix')
910 call assert_fails(1, 'unknown completion function')
911 catch /^Vim\%((\a\+)\)\=:E117/
912 call assert_true(1, 'E117 error caught')
913 endtry
914 set completefunc=
915 bw!
916endfunc
917
Bram Moolenaarff06f282020-04-21 22:01:14 +0200918func Test_edit_completefunc_delete()
919 func CompleteFunc(findstart, base)
920 if a:findstart == 1
921 return col('.') - 1
922 endif
923 normal dd
924 return ['a', 'b']
925 endfunc
926 new
927 set completefunc=CompleteFunc
928 call setline(1, ['', 'abcd', ''])
929 2d
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200930 call assert_fails("normal 2G$a\<C-X>\<C-U>", 'E578:')
Bram Moolenaarff06f282020-04-21 22:01:14 +0200931 bwipe!
932endfunc
933
934
Bram Moolenaar1e115362019-01-09 23:01:02 +0100935func Test_edit_CTRL_Z()
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100936 " Ctrl-Z when insertmode is not set inserts it literally
937 new
938 call setline(1, 'abc')
939 call feedkeys("A\<c-z>\<esc>", 'tnix')
940 call assert_equal(["abc\<c-z>"], getline(1,'$'))
941 bw!
942 " TODO: How to Test Ctrl-Z in insert mode, e.g. suspend?
943endfunc
944
Bram Moolenaar1e115362019-01-09 23:01:02 +0100945func Test_edit_DROP()
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100946 if !has("dnd")
947 return
948 endif
949 new
950 call setline(1, ['abc def ghi'])
951 call cursor(1, 1)
952 try
953 call feedkeys("i\<Drop>\<Esc>", 'tnix')
954 call assert_fails(1, 'Invalid register name')
955 catch /^Vim\%((\a\+)\)\=:E353/
956 call assert_true(1, 'error caught')
957 endtry
958 bw!
959endfunc
960
Bram Moolenaar1e115362019-01-09 23:01:02 +0100961func Test_edit_CTRL_V()
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100962 if has("ebcdic")
963 return
964 endif
965 new
966 call setline(1, ['abc'])
967 call cursor(2, 1)
968 " force some redraws
969 set showmode showcmd
970 "call test_override_char_avail(1)
971 call test_override('ALL', 1)
972 call feedkeys("A\<c-v>\<c-n>\<c-v>\<c-l>\<c-v>\<c-b>\<esc>", 'tnix')
973 call assert_equal(["abc\x0e\x0c\x02"], getline(1, '$'))
974
975 if has("rightleft") && exists("+rl")
976 set rl
977 call setline(1, ['abc'])
978 call cursor(2, 1)
979 call feedkeys("A\<c-v>\<c-n>\<c-v>\<c-l>\<c-v>\<c-b>\<esc>", 'tnix')
980 call assert_equal(["abc\x0e\x0c\x02"], getline(1, '$'))
981 set norl
982 endif
983
984 call test_override('ALL', 0)
985 set noshowmode showcmd
986 bw!
987endfunc
988
Bram Moolenaar1e115362019-01-09 23:01:02 +0100989func Test_edit_F1()
Bram Moolenaar5a4c3082019-12-01 15:23:11 +0100990 CheckFeature quickfix
991
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100992 " Pressing <f1>
993 new
994 call feedkeys(":set im\<cr>\<f1>\<c-l>", 'tnix')
995 set noinsertmode
996 call assert_equal('help', &buftype)
997 bw
998 bw
999endfunc
1000
Bram Moolenaar1e115362019-01-09 23:01:02 +01001001func Test_edit_F21()
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001002 " Pressing <f21>
1003 " sends a netbeans command
1004 if has("netbeans_intg")
1005 new
1006 " I have no idea what this is supposed to do :)
1007 call feedkeys("A\<F21>\<F1>\<esc>", 'tnix')
1008 bw
1009 endif
1010endfunc
1011
Bram Moolenaar1e115362019-01-09 23:01:02 +01001012func Test_edit_HOME_END()
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001013 " Test Home/End Keys
1014 new
1015 set foldopen+=hor
1016 call setline(1, ['abc', 'def'])
1017 call cursor(1, 1)
1018 call feedkeys("AX\<Home>Y\<esc>", 'tnix')
1019 call cursor(2, 1)
1020 call feedkeys("iZ\<End>Y\<esc>", 'tnix')
1021 call assert_equal(['YabcX', 'ZdefY'], getline(1, '$'))
1022
1023 set foldopen-=hor
1024 bw!
1025endfunc
1026
Bram Moolenaar1e115362019-01-09 23:01:02 +01001027func Test_edit_INS()
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001028 " Test for Pressing <Insert>
1029 new
1030 call setline(1, ['abc', 'def'])
1031 call cursor(1, 1)
1032 call feedkeys("i\<Insert>ZYX>", 'tnix')
1033 call assert_equal(['ZYX>', 'def'], getline(1, '$'))
1034 call setline(1, ['abc', 'def'])
1035 call cursor(1, 1)
1036 call feedkeys("i\<Insert>Z\<Insert>YX>", 'tnix')
1037 call assert_equal(['ZYX>bc', 'def'], getline(1, '$'))
1038 bw!
1039endfunc
1040
Bram Moolenaar1e115362019-01-09 23:01:02 +01001041func Test_edit_LEFT_RIGHT()
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001042 " Left, Shift-Left, Right, Shift-Right
1043 new
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001044 call setline(1, ['abc def ghi', 'ABC DEF GHI', 'ZZZ YYY XXX'])
1045 let _ww=&ww
1046 set ww=
1047 call cursor(2, 1)
1048 call feedkeys("i\<left>\<esc>", 'tnix')
1049 call assert_equal([0, 2, 1, 0], getpos('.'))
1050 " Is this a bug, <s-left> does not respect whichwrap option
1051 call feedkeys("i\<s-left>\<esc>", 'tnix')
1052 call assert_equal([0, 1, 8, 0], getpos('.'))
1053 call feedkeys("i". repeat("\<s-left>", 3). "\<esc>", 'tnix')
1054 call assert_equal([0, 1, 1, 0], getpos('.'))
1055 call feedkeys("i\<right>\<esc>", 'tnix')
1056 call assert_equal([0, 1, 1, 0], getpos('.'))
1057 call feedkeys("i\<right>\<right>\<esc>", 'tnix')
1058 call assert_equal([0, 1, 2, 0], getpos('.'))
1059 call feedkeys("A\<right>\<esc>", 'tnix')
1060 call assert_equal([0, 1, 11, 0], getpos('.'))
1061 call feedkeys("A\<s-right>\<esc>", 'tnix')
1062 call assert_equal([0, 2, 1, 0], getpos('.'))
1063 call feedkeys("i\<s-right>\<esc>", 'tnix')
1064 call assert_equal([0, 2, 4, 0], getpos('.'))
1065 call cursor(3, 11)
1066 call feedkeys("A\<right>\<esc>", 'tnix')
1067 call feedkeys("A\<s-right>\<esc>", 'tnix')
1068 call assert_equal([0, 3, 11, 0], getpos('.'))
1069 call cursor(2, 11)
1070 " <S-Right> does not respect 'whichwrap' option
1071 call feedkeys("A\<s-right>\<esc>", 'tnix')
1072 call assert_equal([0, 3, 1, 0], getpos('.'))
1073 " Check motion when 'whichwrap' contains cursor keys for insert mode
1074 set ww+=[,]
1075 call cursor(2, 1)
1076 call feedkeys("i\<left>\<esc>", 'tnix')
1077 call assert_equal([0, 1, 11, 0], getpos('.'))
1078 call cursor(2, 11)
1079 call feedkeys("A\<right>\<esc>", 'tnix')
1080 call assert_equal([0, 3, 1, 0], getpos('.'))
1081 call cursor(2, 11)
1082 call feedkeys("A\<s-right>\<esc>", 'tnix')
1083 call assert_equal([0, 3, 1, 0], getpos('.'))
1084 let &ww = _ww
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001085 bw!
1086endfunc
1087
Bram Moolenaar1e115362019-01-09 23:01:02 +01001088func Test_edit_MOUSE()
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001089 " This is a simple test, since we not really using the mouse here
1090 if !has("mouse")
1091 return
1092 endif
1093 10new
1094 call setline(1, range(1, 100))
1095 call cursor(1, 1)
1096 set mouse=a
1097 call feedkeys("A\<ScrollWheelDown>\<esc>", 'tnix')
1098 call assert_equal([0, 4, 1, 0], getpos('.'))
1099 " This should move by one pageDown, but only moves
1100 " by one line when the test is run...
1101 call feedkeys("A\<S-ScrollWheelDown>\<esc>", 'tnix')
1102 call assert_equal([0, 5, 1, 0], getpos('.'))
1103 set nostartofline
1104 call feedkeys("A\<C-ScrollWheelDown>\<esc>", 'tnix')
1105 call assert_equal([0, 6, 1, 0], getpos('.'))
1106 call feedkeys("A\<LeftMouse>\<esc>", 'tnix')
1107 call assert_equal([0, 6, 1, 0], getpos('.'))
1108 call feedkeys("A\<RightMouse>\<esc>", 'tnix')
1109 call assert_equal([0, 6, 1, 0], getpos('.'))
1110 call cursor(1, 100)
1111 norm! zt
1112 " this should move by a screen up, but when the test
1113 " is run, it moves up to the top of the buffer...
1114 call feedkeys("A\<ScrollWheelUp>\<esc>", 'tnix')
1115 call assert_equal([0, 1, 1, 0], getpos('.'))
1116 call cursor(1, 30)
1117 norm! zt
1118 call feedkeys("A\<S-ScrollWheelUp>\<esc>", 'tnix')
1119 call assert_equal([0, 1, 1, 0], getpos('.'))
1120 call cursor(1, 30)
1121 norm! zt
1122 call feedkeys("A\<C-ScrollWheelUp>\<esc>", 'tnix')
1123 call assert_equal([0, 1, 1, 0], getpos('.'))
1124 %d
1125 call setline(1, repeat(["12345678901234567890"], 100))
1126 call cursor(2, 1)
1127 call feedkeys("A\<ScrollWheelRight>\<esc>", 'tnix')
1128 call assert_equal([0, 2, 20, 0], getpos('.'))
1129 call feedkeys("A\<ScrollWheelLeft>\<esc>", 'tnix')
1130 call assert_equal([0, 2, 20, 0], getpos('.'))
1131 call feedkeys("A\<S-ScrollWheelRight>\<esc>", 'tnix')
1132 call assert_equal([0, 2, 20, 0], getpos('.'))
1133 call feedkeys("A\<S-ScrollWheelLeft>\<esc>", 'tnix')
1134 call assert_equal([0, 2, 20, 0], getpos('.'))
1135 call feedkeys("A\<C-ScrollWheelRight>\<esc>", 'tnix')
1136 call assert_equal([0, 2, 20, 0], getpos('.'))
1137 call feedkeys("A\<C-ScrollWheelLeft>\<esc>", 'tnix')
1138 call assert_equal([0, 2, 20, 0], getpos('.'))
1139 set mouse& startofline
1140 bw!
1141endfunc
1142
Bram Moolenaar1e115362019-01-09 23:01:02 +01001143func Test_edit_PAGEUP_PAGEDOWN()
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001144 10new
1145 call setline(1, repeat(['abc def ghi'], 30))
1146 call cursor(1, 1)
1147 call feedkeys("i\<PageDown>\<esc>", 'tnix')
1148 call assert_equal([0, 9, 1, 0], getpos('.'))
1149 call feedkeys("i\<PageDown>\<esc>", 'tnix')
1150 call assert_equal([0, 17, 1, 0], getpos('.'))
1151 call feedkeys("i\<PageDown>\<esc>", 'tnix')
1152 call assert_equal([0, 25, 1, 0], getpos('.'))
1153 call feedkeys("i\<PageDown>\<esc>", 'tnix')
1154 call assert_equal([0, 30, 1, 0], getpos('.'))
1155 call feedkeys("i\<PageDown>\<esc>", 'tnix')
1156 call assert_equal([0, 30, 1, 0], getpos('.'))
1157 call feedkeys("A\<PageUp>\<esc>", 'tnix')
1158 call assert_equal([0, 29, 1, 0], getpos('.'))
1159 call feedkeys("A\<PageUp>\<esc>", 'tnix')
1160 call assert_equal([0, 21, 1, 0], getpos('.'))
1161 call feedkeys("A\<PageUp>\<esc>", 'tnix')
1162 call assert_equal([0, 13, 1, 0], getpos('.'))
1163 call feedkeys("A\<PageUp>\<esc>", 'tnix')
1164 call assert_equal([0, 5, 1, 0], getpos('.'))
1165 call feedkeys("A\<PageUp>\<esc>", 'tnix')
1166 call assert_equal([0, 5, 11, 0], getpos('.'))
1167 " <S-Up> is the same as <PageUp>
1168 " <S-Down> is the same as <PageDown>
1169 call cursor(1, 1)
1170 call feedkeys("i\<S-Down>\<esc>", 'tnix')
1171 call assert_equal([0, 9, 1, 0], getpos('.'))
1172 call feedkeys("i\<S-Down>\<esc>", 'tnix')
1173 call assert_equal([0, 17, 1, 0], getpos('.'))
1174 call feedkeys("i\<S-Down>\<esc>", 'tnix')
1175 call assert_equal([0, 25, 1, 0], getpos('.'))
1176 call feedkeys("i\<S-Down>\<esc>", 'tnix')
1177 call assert_equal([0, 30, 1, 0], getpos('.'))
1178 call feedkeys("i\<S-Down>\<esc>", 'tnix')
1179 call assert_equal([0, 30, 1, 0], getpos('.'))
1180 call feedkeys("A\<S-Up>\<esc>", 'tnix')
1181 call assert_equal([0, 29, 1, 0], getpos('.'))
1182 call feedkeys("A\<S-Up>\<esc>", 'tnix')
1183 call assert_equal([0, 21, 1, 0], getpos('.'))
1184 call feedkeys("A\<S-Up>\<esc>", 'tnix')
1185 call assert_equal([0, 13, 1, 0], getpos('.'))
1186 call feedkeys("A\<S-Up>\<esc>", 'tnix')
1187 call assert_equal([0, 5, 1, 0], getpos('.'))
1188 call feedkeys("A\<S-Up>\<esc>", 'tnix')
1189 call assert_equal([0, 5, 11, 0], getpos('.'))
1190 set nostartofline
1191 call cursor(30, 11)
1192 norm! zt
1193 call feedkeys("A\<PageUp>\<esc>", 'tnix')
1194 call assert_equal([0, 29, 11, 0], getpos('.'))
1195 call feedkeys("A\<PageUp>\<esc>", 'tnix')
1196 call assert_equal([0, 21, 11, 0], getpos('.'))
1197 call feedkeys("A\<PageUp>\<esc>", 'tnix')
1198 call assert_equal([0, 13, 11, 0], getpos('.'))
1199 call feedkeys("A\<PageUp>\<esc>", 'tnix')
1200 call assert_equal([0, 5, 11, 0], getpos('.'))
1201 call feedkeys("A\<PageUp>\<esc>", 'tnix')
1202 call assert_equal([0, 5, 11, 0], getpos('.'))
1203 call cursor(1, 1)
1204 call feedkeys("A\<PageDown>\<esc>", 'tnix')
1205 call assert_equal([0, 9, 11, 0], getpos('.'))
1206 call feedkeys("A\<PageDown>\<esc>", 'tnix')
1207 call assert_equal([0, 17, 11, 0], getpos('.'))
1208 call feedkeys("A\<PageDown>\<esc>", 'tnix')
1209 call assert_equal([0, 25, 11, 0], getpos('.'))
1210 call feedkeys("A\<PageDown>\<esc>", 'tnix')
1211 call assert_equal([0, 30, 11, 0], getpos('.'))
1212 call feedkeys("A\<PageDown>\<esc>", 'tnix')
1213 call assert_equal([0, 30, 11, 0], getpos('.'))
1214 " <S-Up> is the same as <PageUp>
1215 " <S-Down> is the same as <PageDown>
1216 call cursor(30, 11)
1217 norm! zt
1218 call feedkeys("A\<S-Up>\<esc>", 'tnix')
1219 call assert_equal([0, 29, 11, 0], getpos('.'))
1220 call feedkeys("A\<S-Up>\<esc>", 'tnix')
1221 call assert_equal([0, 21, 11, 0], getpos('.'))
1222 call feedkeys("A\<S-Up>\<esc>", 'tnix')
1223 call assert_equal([0, 13, 11, 0], getpos('.'))
1224 call feedkeys("A\<S-Up>\<esc>", 'tnix')
1225 call assert_equal([0, 5, 11, 0], getpos('.'))
1226 call feedkeys("A\<S-Up>\<esc>", 'tnix')
1227 call assert_equal([0, 5, 11, 0], getpos('.'))
1228 call cursor(1, 1)
1229 call feedkeys("A\<S-Down>\<esc>", 'tnix')
1230 call assert_equal([0, 9, 11, 0], getpos('.'))
1231 call feedkeys("A\<S-Down>\<esc>", 'tnix')
1232 call assert_equal([0, 17, 11, 0], getpos('.'))
1233 call feedkeys("A\<S-Down>\<esc>", 'tnix')
1234 call assert_equal([0, 25, 11, 0], getpos('.'))
1235 call feedkeys("A\<S-Down>\<esc>", 'tnix')
1236 call assert_equal([0, 30, 11, 0], getpos('.'))
1237 call feedkeys("A\<S-Down>\<esc>", 'tnix')
1238 call assert_equal([0, 30, 11, 0], getpos('.'))
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001239 bw!
1240endfunc
1241
Bram Moolenaar1e115362019-01-09 23:01:02 +01001242func Test_edit_forbidden()
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001243 new
1244 " 1) edit in the sandbox is not allowed
1245 call setline(1, 'a')
1246 com! Sandbox :sandbox call feedkeys("i\<del>\<esc>", 'tnix')
1247 call assert_fails(':Sandbox', 'E48:')
1248 com! Sandbox :sandbox exe "norm! i\<del>"
1249 call assert_fails(':Sandbox', 'E48:')
1250 delcom Sandbox
1251 call assert_equal(['a'], getline(1,'$'))
1252 " 2) edit with textlock set
1253 fu! DoIt()
1254 call feedkeys("i\<del>\<esc>", 'tnix')
1255 endfu
1256 au InsertCharPre <buffer> :call DoIt()
1257 try
1258 call feedkeys("ix\<esc>", 'tnix')
1259 call assert_fails(1, 'textlock')
Bram Moolenaarff06f282020-04-21 22:01:14 +02001260 catch /^Vim\%((\a\+)\)\=:E565/ " catch E565: not allowed here
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001261 endtry
1262 " TODO: Might be a bug: should x really be inserted here
1263 call assert_equal(['xa'], getline(1, '$'))
1264 delfu DoIt
1265 try
1266 call feedkeys("ix\<esc>", 'tnix')
1267 call assert_fails(1, 'unknown function')
1268 catch /^Vim\%((\a\+)\)\=:E117/ " catch E117: unknown function
1269 endtry
1270 au! InsertCharPre
1271 " 3) edit when completion is shown
1272 fun! Complete(findstart, base)
1273 if a:findstart
1274 return col('.')
1275 else
1276 call feedkeys("i\<del>\<esc>", 'tnix')
1277 return []
1278 endif
1279 endfun
1280 set completefunc=Complete
1281 try
1282 call feedkeys("i\<c-x>\<c-u>\<esc>", 'tnix')
1283 call assert_fails(1, 'change in complete function')
Bram Moolenaarff06f282020-04-21 22:01:14 +02001284 catch /^Vim\%((\a\+)\)\=:E565/ " catch E565
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001285 endtry
1286 delfu Complete
1287 set completefunc=
1288 if has("rightleft") && exists("+fkmap")
1289 " 4) 'R' when 'fkmap' and 'revins' is set.
1290 set revins fkmap
1291 try
1292 normal Ri
1293 call assert_fails(1, "R with 'fkmap' and 'ri' set")
1294 catch
1295 finally
1296 set norevins nofkmap
1297 endtry
1298 endif
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001299 bw!
1300endfunc
1301
Bram Moolenaar1e115362019-01-09 23:01:02 +01001302func Test_edit_rightleft()
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001303 " Cursor in rightleft mode moves differently
1304 if !exists("+rightleft")
1305 return
1306 endif
1307 call NewWindow(10, 20)
1308 call setline(1, ['abc', 'def', 'ghi'])
1309 call cursor(1, 2)
1310 set rightleft
1311 " Screen looks as expected
1312 let lines = ScreenLines([1, 4], winwidth(0))
1313 let expect = [
1314 \" cba",
1315 \" fed",
1316 \" ihg",
1317 \" ~"]
1318 call assert_equal(join(expect, "\n"), join(lines, "\n"))
1319 " 2) right moves to the left
1320 call feedkeys("i\<right>\<esc>x", 'txin')
1321 call assert_equal(['bc', 'def', 'ghi'], getline(1,'$'))
1322 call cursor(1, 2)
1323 call feedkeys("i\<s-right>\<esc>", 'txin')
1324 call cursor(1, 2)
1325 call feedkeys("i\<c-right>\<esc>", 'txin')
1326 " Screen looks as expected
1327 let lines = ScreenLines([1, 4], winwidth(0))
1328 let expect = [
1329 \" cb",
1330 \" fed",
1331 \" ihg",
1332 \" ~"]
1333 call assert_equal(join(expect, "\n"), join(lines, "\n"))
1334 " 2) left moves to the right
1335 call setline(1, ['abc', 'def', 'ghi'])
1336 call cursor(1, 2)
1337 call feedkeys("i\<left>\<esc>x", 'txin')
1338 call assert_equal(['ac', 'def', 'ghi'], getline(1,'$'))
1339 call cursor(1, 2)
1340 call feedkeys("i\<s-left>\<esc>", 'txin')
1341 call cursor(1, 2)
1342 call feedkeys("i\<c-left>\<esc>", 'txin')
1343 " Screen looks as expected
1344 let lines = ScreenLines([1, 4], winwidth(0))
1345 let expect = [
1346 \" ca",
1347 \" fed",
1348 \" ihg",
1349 \" ~"]
1350 call assert_equal(join(expect, "\n"), join(lines, "\n"))
1351 set norightleft
1352 bw!
1353endfunc
Bram Moolenaar658a3a22017-03-31 22:27:12 +02001354
1355func Test_edit_complete_very_long_name()
Bram Moolenaar15ecbd62017-04-07 14:10:48 +02001356 if !has('unix')
Bram Moolenaar9b810792017-03-31 23:32:53 +02001357 " Long directory names only work on Unix.
1358 return
1359 endif
Bram Moolenaar15ecbd62017-04-07 14:10:48 +02001360
1361 let dirname = getcwd() . "/Xdir"
1362 let longdirname = dirname . repeat('/' . repeat('d', 255), 4)
1363 try
1364 call mkdir(longdirname, 'p')
1365 catch /E739:/
1366 " Long directory name probably not supported.
1367 call delete(dirname, 'rf')
1368 return
1369 endtry
1370
Bram Moolenaarf8191c52019-05-18 17:22:54 +02001371 " Try to get the Vim window position before setting 'columns', so that we can
1372 " move the window back to where it was.
Bram Moolenaarba6ec182017-04-04 22:41:10 +02001373 let winposx = getwinposx()
1374 let winposy = getwinposy()
Bram Moolenaarf8191c52019-05-18 17:22:54 +02001375
1376 if winposx >= 0 && winposy >= 0 && !has('gui_running')
1377 " We did get the window position, but xterm may report the wrong numbers.
1378 " Move the window to the reported position and compute any offset.
1379 exe 'winpos ' . winposx . ' ' . winposy
1380 sleep 100m
1381 let x = getwinposx()
1382 if x >= 0
1383 let winposx += winposx - x
1384 endif
1385 let y = getwinposy()
1386 if y >= 0
1387 let winposy += winposy - y
1388 endif
1389 endif
1390
Bram Moolenaar658a3a22017-03-31 22:27:12 +02001391 let save_columns = &columns
Bram Moolenaar15ecbd62017-04-07 14:10:48 +02001392 " Need at least about 1100 columns to reproduce the problem.
Bram Moolenaarba6ec182017-04-04 22:41:10 +02001393 set columns=2000
Bram Moolenaar658a3a22017-03-31 22:27:12 +02001394 set noswapfile
Bram Moolenaarba6ec182017-04-04 22:41:10 +02001395
Bram Moolenaar658a3a22017-03-31 22:27:12 +02001396 let longfilename = longdirname . '/' . repeat('a', 255)
Bram Moolenaar658a3a22017-03-31 22:27:12 +02001397 call writefile(['Totum', 'Table'], longfilename)
1398 new
1399 exe "next Xfile " . longfilename
1400 exe "normal iT\<C-N>"
1401
1402 bwipe!
1403 exe 'bwipe! ' . longfilename
1404 call delete(dirname, 'rf')
1405 let &columns = save_columns
Bram Moolenaarba6ec182017-04-04 22:41:10 +02001406 if winposx >= 0 && winposy >= 0
1407 exe 'winpos ' . winposx . ' ' . winposy
1408 endif
Bram Moolenaar658a3a22017-03-31 22:27:12 +02001409 set swapfile&
1410endfunc
Bram Moolenaarff930ca2017-10-19 17:12:10 +02001411
Bram Moolenaar2c8c6812018-07-28 17:07:52 +02001412func Test_edit_backtick()
1413 next a\`b c
1414 call assert_equal('a`b', expand('%'))
1415 next
1416 call assert_equal('c', expand('%'))
1417 call assert_equal('a\`b c', expand('##'))
1418endfunc
1419
Bram Moolenaarff930ca2017-10-19 17:12:10 +02001420func Test_edit_quit()
1421 edit foo.txt
1422 split
1423 new
1424 call setline(1, 'hello')
1425 3wincmd w
1426 redraw!
1427 call assert_fails('1q', 'E37:')
1428 bwipe! foo.txt
1429 only
1430endfunc
1431
Bram Moolenaaradb8fbe2018-06-04 20:34:23 +02001432func Test_edit_alt()
1433 " Keeping the cursor line didn't happen when the first line has indent.
1434 new
1435 call setline(1, [' one', 'two', 'three'])
1436 w XAltFile
1437 $
1438 call assert_equal(3, line('.'))
1439 e Xother
1440 e #
1441 call assert_equal(3, line('.'))
1442
1443 bwipe XAltFile
1444 call delete('XAltFile')
1445endfunc
Bram Moolenaar4dbc2622018-11-02 11:59:15 +01001446
Bram Moolenaardb934952020-04-27 20:18:31 +02001447func Test_edit_InsertLeave()
Bram Moolenaar4dbc2622018-11-02 11:59:15 +01001448 new
1449 au InsertLeave * let g:did_au = 1
1450 let g:did_au = 0
1451 call feedkeys("afoo\<Esc>", 'tx')
1452 call assert_equal(1, g:did_au)
1453 call assert_equal('foo', getline(1))
1454
1455 let g:did_au = 0
1456 call feedkeys("Sbar\<C-C>", 'tx')
1457 call assert_equal(0, g:did_au)
1458 call assert_equal('bar', getline(1))
1459
1460 inoremap x xx<Esc>
1461 let g:did_au = 0
1462 call feedkeys("Saax", 'tx')
1463 call assert_equal(1, g:did_au)
1464 call assert_equal('aaxx', getline(1))
1465
1466 inoremap x xx<C-C>
1467 let g:did_au = 0
1468 call feedkeys("Sbbx", 'tx')
1469 call assert_equal(0, g:did_au)
1470 call assert_equal('bbxx', getline(1))
1471
1472 bwipe!
1473 au! InsertLeave
1474 iunmap x
1475endfunc
Bram Moolenaarc6b37db2019-04-27 18:00:34 +02001476
Bram Moolenaardb934952020-04-27 20:18:31 +02001477func Test_edit_InsertLeave_undo()
1478 new XtestUndo
1479 set undofile
1480 au InsertLeave * wall
1481 exe "normal ofoo\<Esc>"
1482 call assert_equal(2, line('$'))
1483 normal u
1484 call assert_equal(1, line('$'))
1485
1486 bwipe!
1487 au! InsertLeave
1488 call delete('XtestUndo')
1489 set undofile&
1490endfunc
1491
Bram Moolenaarc6b37db2019-04-27 18:00:34 +02001492" Test for inserting characters using CTRL-V followed by a number.
1493func Test_edit_special_chars()
1494 new
1495
1496 if has("ebcdic")
1497 let t = "o\<C-V>193\<C-V>xc2\<C-V>o303 \<C-V>90a\<C-V>xfg\<C-V>o578\<Esc>"
1498 else
1499 let t = "o\<C-V>65\<C-V>x42\<C-V>o103 \<C-V>33a\<C-V>xfg\<C-V>o78\<Esc>"
1500 endif
1501
1502 exe "normal " . t
1503 call assert_equal("ABC !a\<C-O>g\<C-G>8", getline(2))
1504
1505 close!
1506endfunc
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02001507
1508func Test_edit_startinsert()
1509 new
1510 set backspace+=start
1511 call setline(1, 'foobar')
1512 call feedkeys("A\<C-U>\<Esc>", 'xt')
1513 call assert_equal('', getline(1))
1514
1515 call setline(1, 'foobar')
1516 call feedkeys(":startinsert!\<CR>\<C-U>\<Esc>", 'xt')
1517 call assert_equal('', getline(1))
1518
1519 set backspace&
1520 bwipe!
1521endfunc
Bram Moolenaar177c9f22019-11-06 13:59:16 +01001522
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +01001523" Test for :startreplace and :startgreplace
1524func Test_edit_startreplace()
1525 new
1526 call setline(1, 'abc')
1527 call feedkeys("l:startreplace\<CR>xyz\e", 'xt')
1528 call assert_equal('axyz', getline(1))
1529 call feedkeys("0:startreplace!\<CR>abc\e", 'xt')
1530 call assert_equal('axyzabc', getline(1))
1531 call setline(1, "a\tb")
1532 call feedkeys("0l:startgreplace\<CR>xyz\e", 'xt')
1533 call assert_equal("axyz\tb", getline(1))
1534 call feedkeys("0i\<C-R>=execute('startreplace')\<CR>12\e", 'xt')
1535 call assert_equal("12axyz\tb", getline(1))
1536 close!
1537endfunc
1538
Bram Moolenaar177c9f22019-11-06 13:59:16 +01001539func Test_edit_noesckeys()
Bram Moolenaar215ba3b2019-11-06 15:07:07 +01001540 CheckNotGui
Bram Moolenaar177c9f22019-11-06 13:59:16 +01001541 new
1542
1543 " <Left> moves cursor when 'esckeys' is set
1544 exe "set t_kl=\<Esc>OD"
1545 set esckeys
1546 call feedkeys("axyz\<Esc>ODX", "xt")
1547 call assert_equal("xyXz", getline(1))
1548
1549 " <Left> exits Insert mode when 'esckeys' is off
1550 set noesckeys
1551 call setline(1, '')
1552 call feedkeys("axyz\<Esc>ODX", "xt")
1553 call assert_equal(["DX", "xyz"], getline(1, 2))
1554
1555 bwipe!
1556 set esckeys
1557endfunc
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +01001558
Bram Moolenaar1671f442020-03-10 07:48:13 +01001559" Test for running an invalid ex command in insert mode using CTRL-O
1560" Note that vim has a hard-coded sleep of 3 seconds. So this test will take
1561" more than 3 seconds to complete.
1562func Test_edit_ctrl_o_invalid_cmd()
1563 new
1564 set showmode showcmd
1565 let caught_e492 = 0
1566 try
1567 call feedkeys("i\<C-O>:invalid\<CR>abc\<Esc>", "xt")
1568 catch /E492:/
1569 let caught_e492 = 1
1570 endtry
1571 call assert_equal(1, caught_e492)
1572 call assert_equal('abc', getline(1))
1573 set showmode& showcmd&
1574 close!
1575endfunc
1576
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001577" Test for inserting text in a line with only spaces ('H' flag in 'cpoptions')
1578func Test_edit_cpo_H()
Bram Moolenaar1671f442020-03-10 07:48:13 +01001579 new
1580 call setline(1, ' ')
1581 normal! Ia
1582 call assert_equal(' a', getline(1))
1583 set cpo+=H
1584 call setline(1, ' ')
1585 normal! Ia
1586 call assert_equal(' a ', getline(1))
1587 set cpo-=H
1588 close!
1589endfunc
1590
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001591" Test for inserting tab in virtual replace mode ('L' flag in 'cpoptions')
1592func Test_edit_cpo_L()
1593 new
1594 call setline(1, 'abcdefghijklmnopqr')
1595 exe "normal 0gR\<Tab>"
1596 call assert_equal("\<Tab>ijklmnopqr", getline(1))
1597 set cpo+=L
1598 set list
1599 call setline(1, 'abcdefghijklmnopqr')
1600 exe "normal 0gR\<Tab>"
1601 call assert_equal("\<Tab>cdefghijklmnopqr", getline(1))
1602 set nolist
1603 call setline(1, 'abcdefghijklmnopqr')
1604 exe "normal 0gR\<Tab>"
1605 call assert_equal("\<Tab>ijklmnopqr", getline(1))
1606 set cpo-=L
1607 %bw!
1608endfunc
1609
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +01001610" vim: shiftwidth=2 sts=2 expandtab