blob: f0dd446adaba4e9595b95ed899cba6039736ac5b [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
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200159 CheckFeature diff
160 CheckExecutable diff
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100161 new
162 call setline(1, ['abc', 'xxx', 'yyy'])
163 vnew
164 call setline(1, ['abc', 'zzz', 'xxx', 'yyy'])
165 wincmd p
166 diffthis
167 wincmd p
168 diffthis
169 wincmd p
170 call cursor(2, 1)
171 norm! zt
172 call feedkeys("Ozzz\<esc>", 'tnix')
173 call assert_equal(['abc', 'zzz', 'xxx', 'yyy'], getline(1,'$'))
174 bw!
175 bw!
176endfunc
177
Bram Moolenaar1e115362019-01-09 23:01:02 +0100178func Test_edit_07()
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100179 " 1) Test with completion <c-l> when popupmenu is visible
180 new
181 call setline(1, 'J')
182
183 func! ListMonths()
184 call complete(col('.')-1, ['January', 'February', 'March',
185 \ 'April', 'May', 'June', 'July', 'August', 'September',
186 \ 'October', 'November', 'December'])
187 return ''
188 endfunc
189 inoremap <buffer> <F5> <C-R>=ListMonths()<CR>
190
191 call feedkeys("A\<f5>\<c-p>". repeat("\<down>", 6)."\<c-l>\<down>\<c-l>\<cr>", 'tx')
192 call assert_equal(['July'], getline(1,'$'))
193 " 1) Test completion when InsertCharPre kicks in
194 %d
195 call setline(1, 'J')
196 fu! DoIt()
197 if v:char=='u'
198 let v:char='an'
199 endif
200 endfu
201 au InsertCharPre <buffer> :call DoIt()
202 call feedkeys("A\<f5>\<c-p>u\<cr>\<c-l>\<cr>", 'tx')
Bram Moolenaar4c313b12019-08-24 22:58:31 +0200203 call assert_equal(["Jan\<c-l>",''], 1->getline('$'))
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100204 %d
205 call setline(1, 'J')
206 call feedkeys("A\<f5>\<c-p>u\<down>\<c-l>\<cr>", 'tx')
Bram Moolenaar4c313b12019-08-24 22:58:31 +0200207 call assert_equal(["January"], 1->getline('$'))
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100208
209 delfu ListMonths
210 delfu DoIt
211 iunmap <buffer> <f5>
212 bw!
213endfunc
214
Bram Moolenaar1e115362019-01-09 23:01:02 +0100215func Test_edit_08()
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100216 " reset insertmode from i_ctrl-r_=
Bram Moolenaar2a45d642017-10-27 01:35:00 +0200217 let g:bufnr = bufnr('%')
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100218 new
219 call setline(1, ['abc'])
220 call cursor(1, 4)
Bram Moolenaar2a45d642017-10-27 01:35:00 +0200221 call feedkeys(":set im\<cr>ZZZ\<c-r>=setbufvar(g:bufnr,'&im', 0)\<cr>",'tnix')
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100222 call assert_equal(['abZZZc'], getline(1,'$'))
223 call assert_equal([0, 1, 1, 0], getpos('.'))
224 call assert_false(0, '&im')
225 bw!
Bram Moolenaar2a45d642017-10-27 01:35:00 +0200226 unlet g:bufnr
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100227endfunc
228
Bram Moolenaar1e115362019-01-09 23:01:02 +0100229func Test_edit_09()
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100230 " test i_CTRL-\ combinations
231 new
232 call setline(1, ['abc', 'def', 'ghi'])
233 call cursor(1, 1)
234 " 1) CTRL-\ CTLR-N
235 call feedkeys(":set im\<cr>\<c-\>\<c-n>ccABC\<c-l>", 'txin')
236 call assert_equal(['ABC', 'def', 'ghi'], getline(1,'$'))
237 call setline(1, ['ABC', 'def', 'ghi'])
238 " 2) CTRL-\ CTLR-G
239 call feedkeys("j0\<c-\>\<c-g>ZZZ\<cr>\<c-l>", 'txin')
240 call assert_equal(['ABC', 'ZZZ', 'def', 'ghi'], getline(1,'$'))
241 call feedkeys("I\<c-\>\<c-g>YYY\<c-l>", 'txin')
242 call assert_equal(['ABC', 'ZZZ', 'YYYdef', 'ghi'], getline(1,'$'))
243 set noinsertmode
244 " 3) CTRL-\ CTRL-O
245 call setline(1, ['ABC', 'ZZZ', 'def', 'ghi'])
246 call cursor(1, 1)
247 call feedkeys("A\<c-o>ix", 'txin')
248 call assert_equal(['ABxC', 'ZZZ', 'def', 'ghi'], getline(1,'$'))
249 call feedkeys("A\<c-\>\<c-o>ix", 'txin')
250 call assert_equal(['ABxCx', 'ZZZ', 'def', 'ghi'], getline(1,'$'))
251 " 4) CTRL-\ a (should be inserted literally, not special after <c-\>
252 call setline(1, ['ABC', 'ZZZ', 'def', 'ghi'])
253 call cursor(1, 1)
254 call feedkeys("A\<c-\>a", 'txin')
255 call assert_equal(["ABC\<c-\>a", 'ZZZ', 'def', 'ghi'], getline(1, '$'))
256 bw!
257endfunc
258
Bram Moolenaar1e115362019-01-09 23:01:02 +0100259func Test_edit_11()
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100260 " Test that indenting kicks in
261 new
262 set cindent
263 call setline(1, ['{', '', ''])
264 call cursor(2, 1)
265 call feedkeys("i\<c-f>int c;\<esc>", 'tnix')
266 call cursor(3, 1)
Bram Moolenaar1671f442020-03-10 07:48:13 +0100267 call feedkeys("\<Insert>/* comment */", 'tnix')
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100268 call assert_equal(['{', "\<tab>int c;", "/* comment */"], getline(1, '$'))
269 " added changed cindentkeys slightly
270 set cindent cinkeys+=*/
271 call setline(1, ['{', '', ''])
272 call cursor(2, 1)
273 call feedkeys("i\<c-f>int c;\<esc>", 'tnix')
274 call cursor(3, 1)
275 call feedkeys("i/* comment */", 'tnix')
276 call assert_equal(['{', "\<tab>int c;", "\<tab>/* comment */"], getline(1, '$'))
277 set cindent cinkeys+==end
278 call feedkeys("oend\<cr>\<esc>", 'tnix')
279 call assert_equal(['{', "\<tab>int c;", "\<tab>/* comment */", "\tend", ''], getline(1, '$'))
280 set cinkeys-==end
281 %d
282 " Use indentexpr instead of cindenting
283 func! Do_Indent()
284 if v:lnum == 3
285 return 3*shiftwidth()
286 else
287 return 2*shiftwidth()
288 endif
289 endfunc
290 setl indentexpr=Do_Indent() indentkeys+=*/
291 call setline(1, ['{', '', ''])
292 call cursor(2, 1)
293 call feedkeys("i\<c-f>int c;\<esc>", 'tnix')
294 call cursor(3, 1)
295 call feedkeys("i/* comment */", 'tnix')
296 call assert_equal(['{', "\<tab>\<tab>int c;", "\<tab>\<tab>\<tab>/* comment */"], getline(1, '$'))
297 set cinkeys&vim indentkeys&vim
298 set nocindent indentexpr=
299 delfu Do_Indent
300 bw!
301endfunc
302
Bram Moolenaar1e115362019-01-09 23:01:02 +0100303func Test_edit_11_indentexpr()
Bram Moolenaar1b383442017-09-26 20:04:54 +0200304 " Test that indenting kicks in
305 new
306 " Use indentexpr instead of cindenting
307 func! Do_Indent()
308 let pline=prevnonblank(v:lnum)
309 if empty(getline(v:lnum))
310 if getline(pline) =~ 'if\|then'
311 return shiftwidth()
312 else
313 return 0
314 endif
315 else
316 return 0
317 endif
318 endfunc
319 setl indentexpr=Do_Indent() indentkeys+=0=then,0=fi
320 call setline(1, ['if [ $this ]'])
321 call cursor(1, 1)
322 call feedkeys("othen\<cr>that\<cr>fi", 'tnix')
323 call assert_equal(['if [ $this ]', "then", "\<tab>that", "fi"], getline(1, '$'))
324 set cinkeys&vim indentkeys&vim
325 set nocindent indentexpr=
326 delfu Do_Indent
Yegappan Lakshmanan8bb65f22021-12-26 10:51:39 +0000327
328 " Using a script-local function
329 func s:NewIndentExpr()
330 endfunc
331 set indentexpr=s:NewIndentExpr()
332 call assert_equal(expand('<SID>') .. 'NewIndentExpr()', &indentexpr)
333 set indentexpr=<SID>NewIndentExpr()
334 call assert_equal(expand('<SID>') .. 'NewIndentExpr()', &indentexpr)
335 set indentexpr&
336
Bram Moolenaar1b383442017-09-26 20:04:54 +0200337 bw!
338endfunc
339
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +0200340" Test changing indent in replace mode
Bram Moolenaar1e115362019-01-09 23:01:02 +0100341func Test_edit_12()
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100342 new
343 call setline(1, ["\tabc", "\tdef"])
344 call cursor(2, 4)
345 call feedkeys("R^\<c-d>", 'tnix')
346 call assert_equal(["\tabc", "def"], getline(1, '$'))
Bram Moolenaar4c313b12019-08-24 22:58:31 +0200347 call assert_equal([0, 2, 2, 0], '.'->getpos())
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100348 %d
349 call setline(1, ["\tabc", "\t\tdef"])
350 call cursor(2, 2)
351 call feedkeys("R^\<c-d>", 'tnix')
352 call assert_equal(["\tabc", "def"], getline(1, '$'))
353 call assert_equal([0, 2, 1, 0], getpos('.'))
354 %d
355 call setline(1, ["\tabc", "\t\tdef"])
356 call cursor(2, 2)
357 call feedkeys("R\<c-t>", 'tnix')
358 call assert_equal(["\tabc", "\t\t\tdef"], getline(1, '$'))
359 call assert_equal([0, 2, 2, 0], getpos('.'))
360 bw!
361 10vnew
362 call setline(1, ["\tabc", "\t\tdef"])
363 call cursor(2, 2)
364 call feedkeys("R\<c-t>", 'tnix')
365 call assert_equal(["\tabc", "\t\t\tdef"], getline(1, '$'))
366 call assert_equal([0, 2, 2, 0], getpos('.'))
367 %d
368 set sw=4
369 call setline(1, ["\tabc", "\t\tdef"])
370 call cursor(2, 2)
371 call feedkeys("R\<c-t>\<c-t>", 'tnix')
372 call assert_equal(["\tabc", "\t\t\tdef"], getline(1, '$'))
373 call assert_equal([0, 2, 2, 0], getpos('.'))
374 %d
375 call setline(1, ["\tabc", "\t\tdef"])
376 call cursor(2, 2)
377 call feedkeys("R\<c-t>\<c-t>", 'tnix')
378 call assert_equal(["\tabc", "\t\t\tdef"], getline(1, '$'))
379 call assert_equal([0, 2, 2, 0], getpos('.'))
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +0200380 set sw&
381
382 " In replace mode, after hitting enter in a line with tab characters,
383 " pressing backspace should restore the tab characters.
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100384 %d
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +0200385 setlocal autoindent backspace=2
386 call setline(1, "\tone\t\ttwo")
387 exe "normal ggRred\<CR>six" .. repeat("\<BS>", 8)
388 call assert_equal(["\tone\t\ttwo"], getline(1, '$'))
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100389 bw!
390endfunc
391
Bram Moolenaar1e115362019-01-09 23:01:02 +0100392func Test_edit_13()
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100393 " Test smartindenting
394 if exists("+smartindent")
395 new
396 set smartindent autoindent
397 call setline(1, ["\tabc"])
398 call feedkeys("A {\<cr>more\<cr>}\<esc>", 'tnix')
399 call assert_equal(["\tabc {", "\t\tmore", "\t}"], getline(1, '$'))
400 set smartindent& autoindent&
Bram Moolenaar2ba42382019-03-16 18:11:07 +0100401 bwipe!
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100402 endif
Bram Moolenaar2ba42382019-03-16 18:11:07 +0100403
404 " Test autoindent removing indent of blank line.
405 new
406 call setline(1, ' foo bar baz')
407 set autoindent
408 exe "normal 0eea\<CR>\<CR>\<Esc>"
409 call assert_equal(" foo bar", getline(1))
410 call assert_equal("", getline(2))
411 call assert_equal(" baz", getline(3))
412 set autoindent&
Bram Moolenaar845e0ee2020-06-20 16:05:32 +0200413
414 " pressing <C-U> to erase line should keep the indent with 'autoindent'
415 set backspace=2 autoindent
416 %d
417 exe "normal i\tone\<CR>three\<C-U>two"
418 call assert_equal(["\tone", "\ttwo"], getline(1, '$'))
419 set backspace& autoindent&
420
Bram Moolenaar2ba42382019-03-16 18:11:07 +0100421 bwipe!
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100422endfunc
423
Bram Moolenaar1f448d92021-03-22 19:37:06 +0100424" Test for autoindent removing indent when insert mode is stopped. Some parts
425" of the code is exercised only when interactive mode is used. So use Vim in a
426" terminal.
427func Test_autoindent_remove_indent()
428 CheckRunVimInTerminal
429 let buf = RunVimInTerminal('-N Xfile', {'rows': 6, 'cols' : 20})
430 call TermWait(buf)
431 call term_sendkeys(buf, ":set autoindent\n")
432 " leaving insert mode in a new line with indent added by autoindent, should
433 " remove the indent.
434 call term_sendkeys(buf, "i\<Tab>foo\<CR>\<Esc>")
Dominique Pelle923dce22021-11-21 11:36:04 +0000435 " Need to delay for some time, otherwise the code in getchar.c will not be
Bram Moolenaar1f448d92021-03-22 19:37:06 +0100436 " exercised.
437 call TermWait(buf, 50)
438 " when a line is wrapped and the cursor is at the start of the second line,
439 " leaving insert mode, should move the cursor back to the first line.
440 call term_sendkeys(buf, "o" .. repeat('x', 20) .. "\<Esc>")
Dominique Pelle923dce22021-11-21 11:36:04 +0000441 " Need to delay for some time, otherwise the code in getchar.c will not be
Bram Moolenaar1f448d92021-03-22 19:37:06 +0100442 " exercised.
443 call TermWait(buf, 50)
444 call term_sendkeys(buf, ":w\n")
445 call TermWait(buf)
446 call StopVimInTerminal(buf)
447 call assert_equal(["\tfoo", '', repeat('x', 20)], readfile('Xfile'))
448 call delete('Xfile')
449endfunc
450
Bram Moolenaar1e115362019-01-09 23:01:02 +0100451func Test_edit_CR()
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100452 " Test for <CR> in insert mode
Dominique Pelle923dce22021-11-21 11:36:04 +0000453 " basically only in quickfix mode it's tested, the rest
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100454 " has been taken care of by other tests
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200455 CheckFeature quickfix
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100456 botright new
457 call writefile(range(1, 10), 'Xqflist.txt')
458 call setqflist([{'filename': 'Xqflist.txt', 'lnum': 2}])
459 copen
460 set modifiable
461 call feedkeys("A\<cr>", 'tnix')
462 call assert_equal('Xqflist.txt', bufname(''))
463 call assert_equal(2, line('.'))
464 cclose
465 botright new
466 call setloclist(0, [{'filename': 'Xqflist.txt', 'lnum': 10}])
467 lopen
468 set modifiable
469 call feedkeys("A\<cr>", 'tnix')
470 call assert_equal('Xqflist.txt', bufname(''))
471 call assert_equal(10, line('.'))
472 call feedkeys("A\<Enter>", 'tnix')
473 call feedkeys("A\<kEnter>", 'tnix')
474 call feedkeys("A\n", 'tnix')
475 call feedkeys("A\r", 'tnix')
476 call assert_equal(map(range(1, 10), 'string(v:val)') + ['', '', '', ''], getline(1, '$'))
477 bw!
478 lclose
479 call delete('Xqflist.txt')
480endfunc
481
Bram Moolenaar1e115362019-01-09 23:01:02 +0100482func Test_edit_CTRL_()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200483 CheckFeature rightleft
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100484 " disabled for Windows builds, why?
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200485 CheckNotMSWindows
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100486 let _encoding=&encoding
487 set encoding=utf-8
488 " Test for CTRL-_
489 new
490 call setline(1, ['abc'])
491 call cursor(1, 1)
492 call feedkeys("i\<c-_>xyz\<esc>", 'tnix')
493 call assert_equal(["\<C-_>xyzabc"], getline(1, '$'))
494 call assert_false(&revins)
495 set ari
496 call setline(1, ['abc'])
497 call cursor(1, 1)
498 call feedkeys("i\<c-_>xyz\<esc>", 'tnix')
499 call assert_equal(["æèñabc"], getline(1, '$'))
500 call assert_true(&revins)
501 call setline(1, ['abc'])
502 call cursor(1, 1)
503 call feedkeys("i\<c-_>xyz\<esc>", 'tnix')
504 call assert_equal(["xyzabc"], getline(1, '$'))
505 call assert_false(&revins)
506 set noari
507 let &encoding=_encoding
508 bw!
509endfunc
510
511" needs to come first, to have the @. register empty
Bram Moolenaar1e115362019-01-09 23:01:02 +0100512func Test_edit_00a_CTRL_A()
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100513 " Test pressing CTRL-A
514 new
515 call setline(1, repeat([''], 5))
516 call cursor(1, 1)
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100517 try
518 call feedkeys("A\<NUL>", 'tnix')
519 catch /^Vim\%((\a\+)\)\=:E29/
520 call assert_true(1, 'E29 error caught')
521 endtry
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100522 call cursor(1, 1)
523 call feedkeys("Afoobar \<esc>", 'tnix')
524 call cursor(2, 1)
525 call feedkeys("A\<c-a>more\<esc>", 'tnix')
526 call cursor(3, 1)
527 call feedkeys("A\<NUL>and more\<esc>", 'tnix')
528 call assert_equal(['foobar ', 'foobar more', 'foobar morend more', '', ''], getline(1, '$'))
529 bw!
530endfunc
531
Bram Moolenaar1e115362019-01-09 23:01:02 +0100532func Test_edit_CTRL_EY()
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100533 " Ctrl-E/ Ctrl-Y in insert mode completion to scroll
534 10new
535 call setline(1, range(1, 100))
536 call cursor(30, 1)
537 norm! z.
538 call feedkeys("A\<c-x>\<c-e>\<c-e>\<c-e>\<c-e>\<c-e>", 'tnix')
539 call assert_equal(30, winsaveview()['topline'])
540 call assert_equal([0, 30, 2, 0], getpos('.'))
541 call feedkeys("A\<c-x>\<c-e>\<c-e>\<c-e>\<c-e>\<c-e>", 'tnix')
542 call feedkeys("A\<c-x>".repeat("\<c-y>", 10), 'tnix')
543 call assert_equal(21, winsaveview()['topline'])
544 call assert_equal([0, 30, 2, 0], getpos('.'))
545 bw!
546endfunc
547
Bram Moolenaar1e115362019-01-09 23:01:02 +0100548func Test_edit_CTRL_G()
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100549 new
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100550 call setline(1, ['foobar', 'foobar', 'foobar'])
551 call cursor(2, 4)
552 call feedkeys("ioooooooo\<c-g>k\<c-r>.\<esc>", 'tnix')
553 call assert_equal(['foooooooooobar', 'foooooooooobar', 'foobar'], getline(1, '$'))
554 call assert_equal([0, 1, 11, 0], getpos('.'))
555 call feedkeys("i\<c-g>k\<esc>", 'tnix')
556 call assert_equal([0, 1, 10, 0], getpos('.'))
557 call cursor(2, 4)
558 call feedkeys("i\<c-g>jzzzz\<esc>", 'tnix')
559 call assert_equal(['foooooooooobar', 'foooooooooobar', 'foozzzzbar'], getline(1, '$'))
560 call assert_equal([0, 3, 7, 0], getpos('.'))
561 call feedkeys("i\<c-g>j\<esc>", 'tnix')
562 call assert_equal([0, 3, 6, 0], getpos('.'))
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100563 bw!
564endfunc
565
Bram Moolenaar1e115362019-01-09 23:01:02 +0100566func Test_edit_CTRL_I()
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100567 " Tab in completion mode
568 let path=expand("%:p:h")
569 new
Bram Moolenaarca851592018-06-06 21:04:07 +0200570 call setline(1, [path. "/", ''])
Bram Moolenaarc5379472017-03-16 22:38:00 +0100571 call feedkeys("Arunt\<c-x>\<c-f>\<tab>\<cr>\<esc>", 'tnix')
572 call assert_match('runtest\.vim', getline(1))
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100573 %d
574 call writefile(['one', 'two', 'three'], 'Xinclude.txt')
575 let include='#include Xinclude.txt'
576 call setline(1, [include, ''])
577 call cursor(2, 1)
578 call feedkeys("A\<c-x>\<tab>\<cr>\<esc>", 'tnix')
579 call assert_equal([include, 'one', ''], getline(1, '$'))
580 call feedkeys("2ggC\<c-x>\<tab>\<down>\<cr>\<esc>", 'tnix')
581 call assert_equal([include, 'two', ''], getline(1, '$'))
582 call feedkeys("2ggC\<c-x>\<tab>\<down>\<down>\<cr>\<esc>", 'tnix')
583 call assert_equal([include, 'three', ''], getline(1, '$'))
584 call feedkeys("2ggC\<c-x>\<tab>\<down>\<down>\<down>\<cr>\<esc>", 'tnix')
585 call assert_equal([include, '', ''], getline(1, '$'))
586 call delete("Xinclude.txt")
587 bw!
588endfunc
589
Bram Moolenaar1e115362019-01-09 23:01:02 +0100590func Test_edit_CTRL_K()
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100591 " Test pressing CTRL-K (basically only dictionary completion and digraphs
592 " the rest is already covered
593 call writefile(['A', 'AA', 'AAA', 'AAAA'], 'Xdictionary.txt')
594 set dictionary=Xdictionary.txt
595 new
596 call setline(1, 'A')
597 call cursor(1, 1)
598 call feedkeys("A\<c-x>\<c-k>\<cr>\<esc>", 'tnix')
599 call assert_equal(['AA', ''], getline(1, '$'))
600 %d
601 call setline(1, 'A')
602 call cursor(1, 1)
603 call feedkeys("A\<c-x>\<c-k>\<down>\<cr>\<esc>", 'tnix')
604 call assert_equal(['AAA'], getline(1, '$'))
605 %d
606 call setline(1, 'A')
607 call cursor(1, 1)
608 call feedkeys("A\<c-x>\<c-k>\<down>\<down>\<cr>\<esc>", 'tnix')
609 call assert_equal(['AAAA'], getline(1, '$'))
610 %d
611 call setline(1, 'A')
612 call cursor(1, 1)
613 call feedkeys("A\<c-x>\<c-k>\<down>\<down>\<down>\<cr>\<esc>", 'tnix')
614 call assert_equal(['A'], getline(1, '$'))
615 %d
616 call setline(1, 'A')
617 call cursor(1, 1)
618 call feedkeys("A\<c-x>\<c-k>\<down>\<down>\<down>\<down>\<cr>\<esc>", 'tnix')
619 call assert_equal(['AA'], getline(1, '$'))
620
Bram Moolenaar4b96df52020-01-26 22:00:26 +0100621 " press an unexpected key after dictionary completion
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100622 %d
623 call setline(1, 'A')
624 call cursor(1, 1)
625 call feedkeys("A\<c-x>\<c-k>\<c-]>\<cr>\<esc>", 'tnix')
626 call assert_equal(['AA', ''], getline(1, '$'))
627 %d
628 call setline(1, 'A')
629 call cursor(1, 1)
630 call feedkeys("A\<c-x>\<c-k>\<c-s>\<cr>\<esc>", 'tnix')
631 call assert_equal(["AA\<c-s>", ''], getline(1, '$'))
632 %d
633 call setline(1, 'A')
634 call cursor(1, 1)
635 call feedkeys("A\<c-x>\<c-k>\<c-f>\<cr>\<esc>", 'tnix')
636 call assert_equal(["AA\<c-f>", ''], getline(1, '$'))
637
638 set dictionary=
639 %d
640 call setline(1, 'A')
641 call cursor(1, 1)
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100642 let v:testing = 1
643 try
644 call feedkeys("A\<c-x>\<c-k>\<esc>", 'tnix')
645 catch
646 " error sleeps 2 seconds, when v:testing is not set
647 let v:testing = 0
648 endtry
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100649 call delete('Xdictionary.txt')
650
Bram Moolenaar30276f22019-01-24 17:59:39 +0100651 call test_override("char_avail", 1)
652 set showcmd
653 %d
654 call feedkeys("A\<c-k>a:\<esc>", 'tnix')
655 call assert_equal(['ä'], getline(1, '$'))
656 call test_override("char_avail", 0)
657 set noshowcmd
658
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100659 bw!
660endfunc
661
Bram Moolenaar1e115362019-01-09 23:01:02 +0100662func Test_edit_CTRL_L()
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100663 " Test Ctrl-X Ctrl-L (line completion)
664 new
665 set complete=.
666 call setline(1, ['one', 'two', 'three', '', '', '', ''])
667 call cursor(4, 1)
668 call feedkeys("A\<c-x>\<c-l>\<esc>", 'tnix')
669 call assert_equal(['one', 'two', 'three', 'three', '', '', ''], getline(1, '$'))
670 call feedkeys("cct\<c-x>\<c-l>\<c-n>\<esc>", 'tnix')
671 call assert_equal(['one', 'two', 'three', 't', '', '', ''], getline(1, '$'))
672 call feedkeys("cct\<c-x>\<c-l>\<c-n>\<c-n>\<esc>", 'tnix')
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100673 call assert_equal(['one', 'two', 'three', 'two', '', '', ''], getline(1, '$'))
Bram Moolenaar02ae9b42018-02-09 15:06:02 +0100674 call feedkeys("cct\<c-x>\<c-l>\<c-n>\<c-n>\<c-n>\<esc>", 'tnix')
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100675 call assert_equal(['one', 'two', 'three', 'three', '', '', ''], getline(1, '$'))
Bram Moolenaar02ae9b42018-02-09 15:06:02 +0100676 call feedkeys("cct\<c-x>\<c-l>\<c-n>\<c-n>\<c-n>\<c-n>\<esc>", 'tnix')
677 call assert_equal(['one', 'two', 'three', 't', '', '', ''], getline(1, '$'))
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100678 call feedkeys("cct\<c-x>\<c-l>\<c-p>\<esc>", 'tnix')
679 call assert_equal(['one', 'two', 'three', 'two', '', '', ''], getline(1, '$'))
680 call feedkeys("cct\<c-x>\<c-l>\<c-p>\<c-p>\<esc>", 'tnix')
681 call assert_equal(['one', 'two', 'three', 't', '', '', ''], getline(1, '$'))
682 call feedkeys("cct\<c-x>\<c-l>\<c-p>\<c-p>\<c-p>\<esc>", 'tnix')
683 call assert_equal(['one', 'two', 'three', 'three', '', '', ''], getline(1, '$'))
684 set complete=
685 call cursor(5, 1)
686 call feedkeys("A\<c-x>\<c-l>\<c-p>\<c-n>\<esc>", 'tnix')
687 call assert_equal(['one', 'two', 'three', 'three', "\<c-l>\<c-p>\<c-n>", '', ''], getline(1, '$'))
688 set complete&
689 %d
690 if has("conceal") && has("syntax")
691 call setline(1, ['foo', 'bar', 'foobar'])
692 call test_override("char_avail", 1)
693 set conceallevel=2 concealcursor=n
694 syn on
695 syn match ErrorMsg "^bar"
696 call matchadd("Conceal", 'oo', 10, -1, {'conceal': 'X'})
697 func! DoIt()
698 let g:change=1
699 endfunc
700 au! TextChangedI <buffer> :call DoIt()
701
702 call cursor(2, 1)
703 call assert_false(exists("g:change"))
704 call feedkeys("A \<esc>", 'tnix')
705 call assert_equal(['foo', 'bar ', 'foobar'], getline(1, '$'))
706 call assert_equal(1, g:change)
707
708 call test_override("char_avail", 0)
709 call clearmatches()
710 syn off
711 au! TextChangedI
712 delfu DoIt
713 unlet! g:change
714 endif
715 bw!
716endfunc
717
Bram Moolenaar1e115362019-01-09 23:01:02 +0100718func Test_edit_CTRL_N()
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100719 " Check keyword completion
Bram Moolenaara1070ea2021-02-20 19:21:36 +0100720 for e in ['latin1', 'utf-8']
721 exe 'set encoding=' .. e
722 new
723 set complete=.
724 call setline(1, ['INFER', 'loWER', '', '', ])
725 call cursor(3, 1)
726 call feedkeys("Ai\<c-n>\<cr>\<esc>", "tnix")
727 call feedkeys("ILO\<c-n>\<cr>\<esc>", 'tnix')
728 call assert_equal(['INFER', 'loWER', 'i', 'LO', '', ''], getline(1, '$'), e)
729 %d
730 call setline(1, ['INFER', 'loWER', '', '', ])
731 call cursor(3, 1)
732 set ignorecase infercase
733 call feedkeys("Ii\<c-n>\<cr>\<esc>", "tnix")
734 call feedkeys("ILO\<c-n>\<cr>\<esc>", 'tnix')
735 call assert_equal(['INFER', 'loWER', 'infer', 'LOWER', '', ''], getline(1, '$'), e)
Yegappan Lakshmanane9825862022-01-03 11:03:48 +0000736 set noignorecase noinfercase
737 %d
738 call setline(1, ['one word', 'two word'])
739 exe "normal! Goo\<C-P>\<C-X>\<C-P>"
740 call assert_equal('one word', getline(3))
741 %d
742 set complete&
Bram Moolenaara1070ea2021-02-20 19:21:36 +0100743 bw!
744 endfor
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100745endfunc
746
Bram Moolenaar1e115362019-01-09 23:01:02 +0100747func Test_edit_CTRL_O()
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100748 " Check for CTRL-O in insert mode
749 new
750 inoreabbr <buffer> h here some more
751 call setline(1, ['abc', 'def'])
752 call cursor(1, 1)
753 " Ctrl-O after an abbreviation
754 exe "norm A h\<c-o>:set nu\<cr> text"
755 call assert_equal(['abc here some more text', 'def'], getline(1, '$'))
756 call assert_true(&nu)
757 set nonu
758 iunabbr <buffer> h
759 " Ctrl-O at end of line with 've'=onemore
760 call cursor(1, 1)
761 call feedkeys("A\<c-o>:let g:a=getpos('.')\<cr>\<esc>", 'tnix')
762 call assert_equal([0, 1, 23, 0], g:a)
763 call cursor(1, 1)
764 set ve=onemore
765 call feedkeys("A\<c-o>:let g:a=getpos('.')\<cr>\<esc>", 'tnix')
766 call assert_equal([0, 1, 24, 0], g:a)
767 set ve=
768 unlet! g:a
769 bw!
770endfunc
771
Bram Moolenaar1e115362019-01-09 23:01:02 +0100772func Test_edit_CTRL_R()
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100773 " Insert Register
774 new
775 call test_override("ALL", 1)
776 set showcmd
777 call feedkeys("AFOOBAR eins zwei\<esc>", 'tnix')
778 call feedkeys("O\<c-r>.", 'tnix')
779 call feedkeys("O\<c-r>=10*500\<cr>\<esc>", 'tnix')
780 call feedkeys("O\<c-r>=getreg('=', 1)\<cr>\<esc>", 'tnix')
781 call assert_equal(["getreg('=', 1)", '5000', "FOOBAR eins zwei", "FOOBAR eins zwei"], getline(1, '$'))
782 call test_override("ALL", 0)
783 set noshowcmd
784 bw!
785endfunc
786
Bram Moolenaar1e115362019-01-09 23:01:02 +0100787func Test_edit_CTRL_S()
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100788 " Test pressing CTRL-S (basically only spellfile completion)
789 " the rest is already covered
790 new
791 if !has("spell")
792 call setline(1, 'vim')
793 call feedkeys("A\<c-x>ss\<cr>\<esc>", 'tnix')
794 call assert_equal(['vims', ''], getline(1, '$'))
795 bw!
796 return
797 endif
798 call setline(1, 'vim')
799 " spell option not yet set
800 try
801 call feedkeys("A\<c-x>\<c-s>\<cr>\<esc>", 'tnix')
802 catch /^Vim\%((\a\+)\)\=:E756/
803 call assert_true(1, 'error caught')
804 endtry
805 call assert_equal(['vim', ''], getline(1, '$'))
806 %d
807 setl spell spelllang=en
808 call setline(1, 'vim')
809 call cursor(1, 1)
810 call feedkeys("A\<c-x>\<c-s>\<cr>\<esc>", 'tnix')
811 call assert_equal(['Vim', ''], getline(1, '$'))
812 %d
813 call setline(1, 'vim')
814 call cursor(1, 1)
815 call feedkeys("A\<c-x>\<c-s>\<down>\<cr>\<esc>", 'tnix')
816 call assert_equal(['Aim'], getline(1, '$'))
817 %d
818 call setline(1, 'vim')
819 call cursor(1, 1)
820 call feedkeys("A\<c-x>\<c-s>\<c-p>\<cr>\<esc>", 'tnix')
821 call assert_equal(['vim', ''], getline(1, '$'))
822 %d
823 " empty buffer
824 call cursor(1, 1)
825 call feedkeys("A\<c-x>\<c-s>\<c-p>\<cr>\<esc>", 'tnix')
826 call assert_equal(['', ''], getline(1, '$'))
827 setl nospell
828 bw!
829endfunc
830
Bram Moolenaar1e115362019-01-09 23:01:02 +0100831func Test_edit_CTRL_T()
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100832 " Check for CTRL-T and CTRL-X CTRL-T in insert mode
833 " 1) increase indent
834 new
835 call setline(1, "abc")
836 call cursor(1, 1)
837 call feedkeys("A\<c-t>xyz", 'tnix')
838 call assert_equal(["\<tab>abcxyz"], getline(1, '$'))
839 " 2) also when paste option is set
840 set paste
841 call setline(1, "abc")
842 call cursor(1, 1)
843 call feedkeys("A\<c-t>xyz", 'tnix')
844 call assert_equal(["\<tab>abcxyz"], getline(1, '$'))
845 set nopaste
846 " CTRL-X CTRL-T (thesaurus complete)
847 call writefile(['angry furious mad enraged'], 'Xthesaurus')
848 set thesaurus=Xthesaurus
849 call setline(1, 'mad')
850 call cursor(1, 1)
851 call feedkeys("A\<c-x>\<c-t>\<cr>\<esc>", 'tnix')
852 call assert_equal(['mad', ''], getline(1, '$'))
853 %d
854 call setline(1, 'mad')
855 call cursor(1, 1)
856 call feedkeys("A\<c-x>\<c-t>\<c-n>\<cr>\<esc>", 'tnix')
857 call assert_equal(['angry', ''], getline(1, '$'))
858 %d
859 call setline(1, 'mad')
860 call cursor(1, 1)
861 call feedkeys("A\<c-x>\<c-t>\<c-n>\<c-n>\<cr>\<esc>", 'tnix')
862 call assert_equal(['furious', ''], getline(1, '$'))
863 %d
864 call setline(1, 'mad')
865 call cursor(1, 1)
866 call feedkeys("A\<c-x>\<c-t>\<c-n>\<c-n>\<c-n>\<cr>\<esc>", 'tnix')
867 call assert_equal(['enraged', ''], getline(1, '$'))
868 %d
869 call setline(1, 'mad')
870 call cursor(1, 1)
871 call feedkeys("A\<c-x>\<c-t>\<c-n>\<c-n>\<c-n>\<c-n>\<cr>\<esc>", 'tnix')
872 call assert_equal(['mad', ''], getline(1, '$'))
873 %d
874 call setline(1, 'mad')
875 call cursor(1, 1)
876 call feedkeys("A\<c-x>\<c-t>\<c-n>\<c-n>\<c-n>\<c-n>\<c-n>\<cr>\<esc>", 'tnix')
877 call assert_equal(['mad', ''], getline(1, '$'))
878 " Using <c-p> <c-n> when 'complete' is empty
879 set complete=
880 %d
881 call setline(1, 'mad')
882 call cursor(1, 1)
883 call feedkeys("A\<c-x>\<c-t>\<c-n>\<cr>\<esc>", 'tnix')
884 call assert_equal(['angry', ''], getline(1, '$'))
885 %d
886 call setline(1, 'mad')
887 call cursor(1, 1)
888 call feedkeys("A\<c-x>\<c-t>\<c-p>\<cr>\<esc>", 'tnix')
889 call assert_equal(['mad', ''], getline(1, '$'))
890 set complete&
891
892 set thesaurus=
893 %d
894 call setline(1, 'mad')
895 call cursor(1, 1)
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100896 let v:testing = 1
897 try
898 call feedkeys("A\<c-x>\<c-t>\<esc>", 'tnix')
899 catch
900 " error sleeps 2 seconds, when v:testing is not set
901 let v:testing = 0
902 endtry
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100903 call assert_equal(['mad'], getline(1, '$'))
904 call delete('Xthesaurus')
905 bw!
906endfunc
907
Yegappan Lakshmanane9825862022-01-03 11:03:48 +0000908" Test thesaurus completion with different encodings
909func Test_thesaurus_complete_with_encoding()
910 call writefile(['angry furious mad enraged'], 'Xthesaurus')
911 set thesaurus=Xthesaurus
912 for e in ['latin1', 'utf-8']
913 exe 'set encoding=' .. e
914 new
915 call setline(1, 'mad')
916 call cursor(1, 1)
917 call feedkeys("A\<c-x>\<c-t>\<cr>\<esc>", 'tnix')
918 call assert_equal(['mad', ''], getline(1, '$'))
919 bw!
920 endfor
921 set thesaurus=
922 call delete('Xthesaurus')
923endfunc
924
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +0100925" Test 'thesaurusfunc'
926func MyThesaurus(findstart, base)
927 let mythesaurus = [
928 \ #{word: "happy",
929 \ synonyms: "cheerful,blissful,flying high,looking good,peppy"},
930 \ #{word: "kind",
931 \ synonyms: "amiable,bleeding-heart,heart in right place"}]
932 if a:findstart
933 " locate the start of the word
934 let line = getline('.')
935 let start = col('.') - 1
936 while start > 0 && line[start - 1] =~ '\a'
937 let start -= 1
938 endwhile
939 return start
940 else
941 " find strings matching with "a:base"
942 let res = []
943 for w in mythesaurus
944 if w.word =~ '^' . a:base
945 call add(res, w.word)
946 call extend(res, split(w.synonyms, ","))
947 endif
948 endfor
949 return res
950 endif
951endfunc
952
953func Test_thesaurus_func()
954 new
Bram Moolenaarf4d8b762021-10-17 14:13:09 +0100955 set thesaurus=notused
956 set thesaurusfunc=NotUsed
957 setlocal thesaurusfunc=MyThesaurus
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +0100958 call setline(1, "an ki")
959 call cursor(1, 1)
960 call feedkeys("A\<c-x>\<c-t>\<c-n>\<cr>\<esc>", 'tnix')
961 call assert_equal(['an amiable', ''], getline(1, '$'))
Bram Moolenaarf4d8b762021-10-17 14:13:09 +0100962
963 setlocal thesaurusfunc=NonExistingFunc
964 call assert_fails("normal $a\<C-X>\<C-T>", 'E117:')
965
966 setlocal thesaurusfunc=
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +0100967 set thesaurusfunc=NonExistingFunc
968 call assert_fails("normal $a\<C-X>\<C-T>", 'E117:')
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +0100969 %bw!
Bram Moolenaarf4d8b762021-10-17 14:13:09 +0100970
971 set thesaurusfunc=
972 set thesaurus=
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +0100973endfunc
974
Bram Moolenaar1e115362019-01-09 23:01:02 +0100975func Test_edit_CTRL_U()
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100976 " Test 'completefunc'
977 new
978 " -1, -2 and -3 are special return values
979 let g:special=0
980 fun! CompleteMonths(findstart, base)
981 if a:findstart
982 " locate the start of the word
983 return g:special
984 else
985 " find months matching with "a:base"
986 let res = []
987 for m in split("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec")
988 if m =~ '^\c'.a:base
989 call add(res, {'word': m, 'abbr': m.' Month', 'icase': 0})
990 endif
991 endfor
992 return {'words': res, 'refresh': 'always'}
993 endif
994 endfun
995 set completefunc=CompleteMonths
996 call setline(1, ['', ''])
997 call cursor(1, 1)
998 call feedkeys("AX\<c-x>\<c-u>\<cr>\<esc>", 'tnix')
999 call assert_equal(['X', '', ''], getline(1, '$'))
1000 %d
1001 let g:special=-1
1002 call feedkeys("AX\<c-x>\<c-u>\<cr>\<esc>", 'tnix')
1003 call assert_equal(['XJan', ''], getline(1, '$'))
1004 %d
1005 let g:special=-2
1006 call feedkeys("AX\<c-x>\<c-u>\<cr>\<esc>", 'tnix')
1007 call assert_equal(['X', ''], getline(1, '$'))
1008 %d
1009 let g:special=-3
1010 call feedkeys("AX\<c-x>\<c-u>\<cr>\<esc>", 'tnix')
1011 call assert_equal(['X', ''], getline(1, '$'))
1012 %d
1013 let g:special=0
1014 call feedkeys("AM\<c-x>\<c-u>\<cr>\<esc>", 'tnix')
1015 call assert_equal(['Mar', ''], getline(1, '$'))
1016 %d
1017 call feedkeys("AM\<c-x>\<c-u>\<c-n>\<cr>\<esc>", 'tnix')
1018 call assert_equal(['May', ''], getline(1, '$'))
1019 %d
1020 call feedkeys("AM\<c-x>\<c-u>\<c-n>\<c-n>\<cr>\<esc>", 'tnix')
1021 call assert_equal(['M', ''], getline(1, '$'))
1022 delfu CompleteMonths
1023 %d
1024 try
1025 call feedkeys("A\<c-x>\<c-u>", 'tnix')
1026 call assert_fails(1, 'unknown completion function')
1027 catch /^Vim\%((\a\+)\)\=:E117/
1028 call assert_true(1, 'E117 error caught')
1029 endtry
1030 set completefunc=
1031 bw!
1032endfunc
1033
Bram Moolenaarff06f282020-04-21 22:01:14 +02001034func Test_edit_completefunc_delete()
1035 func CompleteFunc(findstart, base)
1036 if a:findstart == 1
1037 return col('.') - 1
1038 endif
1039 normal dd
1040 return ['a', 'b']
1041 endfunc
1042 new
1043 set completefunc=CompleteFunc
1044 call setline(1, ['', 'abcd', ''])
1045 2d
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02001046 call assert_fails("normal 2G$a\<C-X>\<C-U>", 'E578:')
Bram Moolenaarff06f282020-04-21 22:01:14 +02001047 bwipe!
1048endfunc
1049
1050
Bram Moolenaar1e115362019-01-09 23:01:02 +01001051func Test_edit_CTRL_Z()
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001052 " Ctrl-Z when insertmode is not set inserts it literally
1053 new
1054 call setline(1, 'abc')
1055 call feedkeys("A\<c-z>\<esc>", 'tnix')
1056 call assert_equal(["abc\<c-z>"], getline(1,'$'))
1057 bw!
1058 " TODO: How to Test Ctrl-Z in insert mode, e.g. suspend?
1059endfunc
1060
Bram Moolenaar1e115362019-01-09 23:01:02 +01001061func Test_edit_DROP()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001062 CheckFeature dnd
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001063 new
1064 call setline(1, ['abc def ghi'])
1065 call cursor(1, 1)
1066 try
1067 call feedkeys("i\<Drop>\<Esc>", 'tnix')
1068 call assert_fails(1, 'Invalid register name')
1069 catch /^Vim\%((\a\+)\)\=:E353/
1070 call assert_true(1, 'error caught')
1071 endtry
1072 bw!
1073endfunc
1074
Bram Moolenaar1e115362019-01-09 23:01:02 +01001075func Test_edit_CTRL_V()
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001076 new
1077 call setline(1, ['abc'])
1078 call cursor(2, 1)
zeertzjq502d8ae2022-01-24 15:27:50 +00001079
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001080 " force some redraws
1081 set showmode showcmd
zeertzjq502d8ae2022-01-24 15:27:50 +00001082 call test_override('char_avail', 1)
1083
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001084 call feedkeys("A\<c-v>\<c-n>\<c-v>\<c-l>\<c-v>\<c-b>\<esc>", 'tnix')
1085 call assert_equal(["abc\x0e\x0c\x02"], getline(1, '$'))
1086
1087 if has("rightleft") && exists("+rl")
1088 set rl
1089 call setline(1, ['abc'])
1090 call cursor(2, 1)
1091 call feedkeys("A\<c-v>\<c-n>\<c-v>\<c-l>\<c-v>\<c-b>\<esc>", 'tnix')
1092 call assert_equal(["abc\x0e\x0c\x02"], getline(1, '$'))
1093 set norl
1094 endif
1095
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001096 set noshowmode showcmd
zeertzjq502d8ae2022-01-24 15:27:50 +00001097 call test_override('char_avail', 0)
1098
1099 " No modifiers should be applied to the char typed using i_CTRL-V_digit.
1100 call feedkeys(":append\<CR>\<C-V>76c\<C-V>76\<C-F2>\<C-V>u3c0j\<C-V>u3c0\<M-F3>\<CR>.\<CR>", 'tnix')
1101 call assert_equal('LcL<C-F2>πjπ<M-F3>', getline(2))
1102
1103 if has('osx')
1104 " A char with a modifier should not be a valid char for i_CTRL-V_digit.
1105 call feedkeys("o\<C-V>\<D-j>\<C-V>\<D-1>\<C-V>\<D-o>\<C-V>\<D-x>\<C-V>\<D-u>", 'tnix')
1106 call assert_equal('<D-j><D-1><D-o><D-x><D-u>', getline(3))
1107 endif
1108
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001109 bw!
1110endfunc
1111
Bram Moolenaar1e115362019-01-09 23:01:02 +01001112func Test_edit_F1()
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01001113 CheckFeature quickfix
1114
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001115 " Pressing <f1>
1116 new
1117 call feedkeys(":set im\<cr>\<f1>\<c-l>", 'tnix')
1118 set noinsertmode
1119 call assert_equal('help', &buftype)
1120 bw
1121 bw
1122endfunc
1123
Bram Moolenaar1e115362019-01-09 23:01:02 +01001124func Test_edit_F21()
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001125 " Pressing <f21>
1126 " sends a netbeans command
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001127 CheckFeature netbeans_intg
1128 new
1129 " I have no idea what this is supposed to do :)
1130 call feedkeys("A\<F21>\<F1>\<esc>", 'tnix')
1131 bw
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001132endfunc
1133
Bram Moolenaar1e115362019-01-09 23:01:02 +01001134func Test_edit_HOME_END()
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001135 " Test Home/End Keys
1136 new
1137 set foldopen+=hor
1138 call setline(1, ['abc', 'def'])
1139 call cursor(1, 1)
1140 call feedkeys("AX\<Home>Y\<esc>", 'tnix')
1141 call cursor(2, 1)
1142 call feedkeys("iZ\<End>Y\<esc>", 'tnix')
1143 call assert_equal(['YabcX', 'ZdefY'], getline(1, '$'))
1144
1145 set foldopen-=hor
1146 bw!
1147endfunc
1148
Bram Moolenaar1e115362019-01-09 23:01:02 +01001149func Test_edit_INS()
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001150 " Test for Pressing <Insert>
1151 new
1152 call setline(1, ['abc', 'def'])
1153 call cursor(1, 1)
1154 call feedkeys("i\<Insert>ZYX>", 'tnix')
1155 call assert_equal(['ZYX>', 'def'], getline(1, '$'))
1156 call setline(1, ['abc', 'def'])
1157 call cursor(1, 1)
1158 call feedkeys("i\<Insert>Z\<Insert>YX>", 'tnix')
1159 call assert_equal(['ZYX>bc', 'def'], getline(1, '$'))
1160 bw!
1161endfunc
1162
Bram Moolenaar1e115362019-01-09 23:01:02 +01001163func Test_edit_LEFT_RIGHT()
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001164 " Left, Shift-Left, Right, Shift-Right
1165 new
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001166 call setline(1, ['abc def ghi', 'ABC DEF GHI', 'ZZZ YYY XXX'])
1167 let _ww=&ww
1168 set ww=
1169 call cursor(2, 1)
1170 call feedkeys("i\<left>\<esc>", 'tnix')
1171 call assert_equal([0, 2, 1, 0], getpos('.'))
1172 " Is this a bug, <s-left> does not respect whichwrap option
1173 call feedkeys("i\<s-left>\<esc>", 'tnix')
1174 call assert_equal([0, 1, 8, 0], getpos('.'))
1175 call feedkeys("i". repeat("\<s-left>", 3). "\<esc>", 'tnix')
1176 call assert_equal([0, 1, 1, 0], getpos('.'))
1177 call feedkeys("i\<right>\<esc>", 'tnix')
1178 call assert_equal([0, 1, 1, 0], getpos('.'))
1179 call feedkeys("i\<right>\<right>\<esc>", 'tnix')
1180 call assert_equal([0, 1, 2, 0], getpos('.'))
1181 call feedkeys("A\<right>\<esc>", 'tnix')
1182 call assert_equal([0, 1, 11, 0], getpos('.'))
1183 call feedkeys("A\<s-right>\<esc>", 'tnix')
1184 call assert_equal([0, 2, 1, 0], getpos('.'))
1185 call feedkeys("i\<s-right>\<esc>", 'tnix')
1186 call assert_equal([0, 2, 4, 0], getpos('.'))
1187 call cursor(3, 11)
1188 call feedkeys("A\<right>\<esc>", 'tnix')
1189 call feedkeys("A\<s-right>\<esc>", 'tnix')
1190 call assert_equal([0, 3, 11, 0], getpos('.'))
1191 call cursor(2, 11)
1192 " <S-Right> does not respect 'whichwrap' option
1193 call feedkeys("A\<s-right>\<esc>", 'tnix')
1194 call assert_equal([0, 3, 1, 0], getpos('.'))
1195 " Check motion when 'whichwrap' contains cursor keys for insert mode
1196 set ww+=[,]
1197 call cursor(2, 1)
1198 call feedkeys("i\<left>\<esc>", 'tnix')
1199 call assert_equal([0, 1, 11, 0], getpos('.'))
1200 call cursor(2, 11)
1201 call feedkeys("A\<right>\<esc>", 'tnix')
1202 call assert_equal([0, 3, 1, 0], getpos('.'))
1203 call cursor(2, 11)
1204 call feedkeys("A\<s-right>\<esc>", 'tnix')
1205 call assert_equal([0, 3, 1, 0], getpos('.'))
1206 let &ww = _ww
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001207 bw!
1208endfunc
1209
Bram Moolenaar1e115362019-01-09 23:01:02 +01001210func Test_edit_MOUSE()
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001211 " This is a simple test, since we not really using the mouse here
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001212 CheckFeature mouse
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001213 10new
1214 call setline(1, range(1, 100))
1215 call cursor(1, 1)
Bram Moolenaar8e8dc9b2022-05-08 20:38:06 +01001216 call assert_equal(1, line('w0'))
1217 call assert_equal(10, line('w$'))
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001218 set mouse=a
Bram Moolenaar8e8dc9b2022-05-08 20:38:06 +01001219 " One scroll event moves three lines.
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001220 call feedkeys("A\<ScrollWheelDown>\<esc>", 'tnix')
Bram Moolenaar8e8dc9b2022-05-08 20:38:06 +01001221 call assert_equal(4, line('w0'))
1222 call assert_equal(13, line('w$'))
1223 " This should move by one page down.
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001224 call feedkeys("A\<S-ScrollWheelDown>\<esc>", 'tnix')
Bram Moolenaar8e8dc9b2022-05-08 20:38:06 +01001225 call assert_equal(14, line('w0'))
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001226 set nostartofline
Bram Moolenaar8e8dc9b2022-05-08 20:38:06 +01001227 " Another page down.
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001228 call feedkeys("A\<C-ScrollWheelDown>\<esc>", 'tnix')
Bram Moolenaar8e8dc9b2022-05-08 20:38:06 +01001229 call assert_equal(24, line('w0'))
1230
1231 call assert_equal([0, 24, 2, 0], getpos('.'))
1232 call test_setmouse(4, 3)
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001233 call feedkeys("A\<LeftMouse>\<esc>", 'tnix')
Bram Moolenaar8e8dc9b2022-05-08 20:38:06 +01001234 call assert_equal([0, 27, 2, 0], getpos('.'))
Bram Moolenaarb3707712022-05-08 22:49:43 +01001235 set mousemodel=extend
Bram Moolenaar8e8dc9b2022-05-08 20:38:06 +01001236 call test_setmouse(5, 3)
1237 call feedkeys("A\<RightMouse>\<esc>\<esc>", 'tnix')
1238 call assert_equal([0, 28, 2, 0], getpos('.'))
Bram Moolenaarb3707712022-05-08 22:49:43 +01001239 set mousemodel&
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001240 call cursor(1, 100)
1241 norm! zt
1242 " this should move by a screen up, but when the test
1243 " is run, it moves up to the top of the buffer...
1244 call feedkeys("A\<ScrollWheelUp>\<esc>", 'tnix')
1245 call assert_equal([0, 1, 1, 0], getpos('.'))
1246 call cursor(1, 30)
1247 norm! zt
1248 call feedkeys("A\<S-ScrollWheelUp>\<esc>", 'tnix')
1249 call assert_equal([0, 1, 1, 0], getpos('.'))
1250 call cursor(1, 30)
1251 norm! zt
1252 call feedkeys("A\<C-ScrollWheelUp>\<esc>", 'tnix')
1253 call assert_equal([0, 1, 1, 0], getpos('.'))
1254 %d
1255 call setline(1, repeat(["12345678901234567890"], 100))
1256 call cursor(2, 1)
1257 call feedkeys("A\<ScrollWheelRight>\<esc>", 'tnix')
1258 call assert_equal([0, 2, 20, 0], getpos('.'))
1259 call feedkeys("A\<ScrollWheelLeft>\<esc>", 'tnix')
1260 call assert_equal([0, 2, 20, 0], getpos('.'))
1261 call feedkeys("A\<S-ScrollWheelRight>\<esc>", 'tnix')
1262 call assert_equal([0, 2, 20, 0], getpos('.'))
1263 call feedkeys("A\<S-ScrollWheelLeft>\<esc>", 'tnix')
1264 call assert_equal([0, 2, 20, 0], getpos('.'))
1265 call feedkeys("A\<C-ScrollWheelRight>\<esc>", 'tnix')
1266 call assert_equal([0, 2, 20, 0], getpos('.'))
1267 call feedkeys("A\<C-ScrollWheelLeft>\<esc>", 'tnix')
1268 call assert_equal([0, 2, 20, 0], getpos('.'))
1269 set mouse& startofline
1270 bw!
1271endfunc
1272
Bram Moolenaar1e115362019-01-09 23:01:02 +01001273func Test_edit_PAGEUP_PAGEDOWN()
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001274 10new
1275 call setline(1, repeat(['abc def ghi'], 30))
1276 call cursor(1, 1)
1277 call feedkeys("i\<PageDown>\<esc>", 'tnix')
1278 call assert_equal([0, 9, 1, 0], getpos('.'))
1279 call feedkeys("i\<PageDown>\<esc>", 'tnix')
1280 call assert_equal([0, 17, 1, 0], getpos('.'))
1281 call feedkeys("i\<PageDown>\<esc>", 'tnix')
1282 call assert_equal([0, 25, 1, 0], getpos('.'))
1283 call feedkeys("i\<PageDown>\<esc>", 'tnix')
1284 call assert_equal([0, 30, 1, 0], getpos('.'))
1285 call feedkeys("i\<PageDown>\<esc>", 'tnix')
1286 call assert_equal([0, 30, 1, 0], getpos('.'))
1287 call feedkeys("A\<PageUp>\<esc>", 'tnix')
1288 call assert_equal([0, 29, 1, 0], getpos('.'))
1289 call feedkeys("A\<PageUp>\<esc>", 'tnix')
1290 call assert_equal([0, 21, 1, 0], getpos('.'))
1291 call feedkeys("A\<PageUp>\<esc>", 'tnix')
1292 call assert_equal([0, 13, 1, 0], getpos('.'))
1293 call feedkeys("A\<PageUp>\<esc>", 'tnix')
1294 call assert_equal([0, 5, 1, 0], getpos('.'))
1295 call feedkeys("A\<PageUp>\<esc>", 'tnix')
1296 call assert_equal([0, 5, 11, 0], getpos('.'))
1297 " <S-Up> is the same as <PageUp>
1298 " <S-Down> is the same as <PageDown>
1299 call cursor(1, 1)
1300 call feedkeys("i\<S-Down>\<esc>", 'tnix')
1301 call assert_equal([0, 9, 1, 0], getpos('.'))
1302 call feedkeys("i\<S-Down>\<esc>", 'tnix')
1303 call assert_equal([0, 17, 1, 0], getpos('.'))
1304 call feedkeys("i\<S-Down>\<esc>", 'tnix')
1305 call assert_equal([0, 25, 1, 0], getpos('.'))
1306 call feedkeys("i\<S-Down>\<esc>", 'tnix')
1307 call assert_equal([0, 30, 1, 0], getpos('.'))
1308 call feedkeys("i\<S-Down>\<esc>", 'tnix')
1309 call assert_equal([0, 30, 1, 0], getpos('.'))
1310 call feedkeys("A\<S-Up>\<esc>", 'tnix')
1311 call assert_equal([0, 29, 1, 0], getpos('.'))
1312 call feedkeys("A\<S-Up>\<esc>", 'tnix')
1313 call assert_equal([0, 21, 1, 0], getpos('.'))
1314 call feedkeys("A\<S-Up>\<esc>", 'tnix')
1315 call assert_equal([0, 13, 1, 0], getpos('.'))
1316 call feedkeys("A\<S-Up>\<esc>", 'tnix')
1317 call assert_equal([0, 5, 1, 0], getpos('.'))
1318 call feedkeys("A\<S-Up>\<esc>", 'tnix')
1319 call assert_equal([0, 5, 11, 0], getpos('.'))
1320 set nostartofline
1321 call cursor(30, 11)
1322 norm! zt
1323 call feedkeys("A\<PageUp>\<esc>", 'tnix')
1324 call assert_equal([0, 29, 11, 0], getpos('.'))
1325 call feedkeys("A\<PageUp>\<esc>", 'tnix')
1326 call assert_equal([0, 21, 11, 0], getpos('.'))
1327 call feedkeys("A\<PageUp>\<esc>", 'tnix')
1328 call assert_equal([0, 13, 11, 0], getpos('.'))
1329 call feedkeys("A\<PageUp>\<esc>", 'tnix')
1330 call assert_equal([0, 5, 11, 0], getpos('.'))
1331 call feedkeys("A\<PageUp>\<esc>", 'tnix')
1332 call assert_equal([0, 5, 11, 0], getpos('.'))
1333 call cursor(1, 1)
1334 call feedkeys("A\<PageDown>\<esc>", 'tnix')
1335 call assert_equal([0, 9, 11, 0], getpos('.'))
1336 call feedkeys("A\<PageDown>\<esc>", 'tnix')
1337 call assert_equal([0, 17, 11, 0], getpos('.'))
1338 call feedkeys("A\<PageDown>\<esc>", 'tnix')
1339 call assert_equal([0, 25, 11, 0], getpos('.'))
1340 call feedkeys("A\<PageDown>\<esc>", 'tnix')
1341 call assert_equal([0, 30, 11, 0], getpos('.'))
1342 call feedkeys("A\<PageDown>\<esc>", 'tnix')
1343 call assert_equal([0, 30, 11, 0], getpos('.'))
1344 " <S-Up> is the same as <PageUp>
1345 " <S-Down> is the same as <PageDown>
1346 call cursor(30, 11)
1347 norm! zt
1348 call feedkeys("A\<S-Up>\<esc>", 'tnix')
1349 call assert_equal([0, 29, 11, 0], getpos('.'))
1350 call feedkeys("A\<S-Up>\<esc>", 'tnix')
1351 call assert_equal([0, 21, 11, 0], getpos('.'))
1352 call feedkeys("A\<S-Up>\<esc>", 'tnix')
1353 call assert_equal([0, 13, 11, 0], getpos('.'))
1354 call feedkeys("A\<S-Up>\<esc>", 'tnix')
1355 call assert_equal([0, 5, 11, 0], getpos('.'))
1356 call feedkeys("A\<S-Up>\<esc>", 'tnix')
1357 call assert_equal([0, 5, 11, 0], getpos('.'))
1358 call cursor(1, 1)
1359 call feedkeys("A\<S-Down>\<esc>", 'tnix')
1360 call assert_equal([0, 9, 11, 0], getpos('.'))
1361 call feedkeys("A\<S-Down>\<esc>", 'tnix')
1362 call assert_equal([0, 17, 11, 0], getpos('.'))
1363 call feedkeys("A\<S-Down>\<esc>", 'tnix')
1364 call assert_equal([0, 25, 11, 0], getpos('.'))
1365 call feedkeys("A\<S-Down>\<esc>", 'tnix')
1366 call assert_equal([0, 30, 11, 0], getpos('.'))
1367 call feedkeys("A\<S-Down>\<esc>", 'tnix')
1368 call assert_equal([0, 30, 11, 0], getpos('.'))
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001369 bw!
1370endfunc
1371
Bram Moolenaar1e115362019-01-09 23:01:02 +01001372func Test_edit_forbidden()
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001373 new
1374 " 1) edit in the sandbox is not allowed
1375 call setline(1, 'a')
1376 com! Sandbox :sandbox call feedkeys("i\<del>\<esc>", 'tnix')
1377 call assert_fails(':Sandbox', 'E48:')
1378 com! Sandbox :sandbox exe "norm! i\<del>"
1379 call assert_fails(':Sandbox', 'E48:')
1380 delcom Sandbox
1381 call assert_equal(['a'], getline(1,'$'))
Bram Moolenaar52797ba2021-12-16 14:45:13 +00001382
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001383 " 2) edit with textlock set
1384 fu! DoIt()
1385 call feedkeys("i\<del>\<esc>", 'tnix')
1386 endfu
1387 au InsertCharPre <buffer> :call DoIt()
1388 try
1389 call feedkeys("ix\<esc>", 'tnix')
1390 call assert_fails(1, 'textlock')
Bram Moolenaarff06f282020-04-21 22:01:14 +02001391 catch /^Vim\%((\a\+)\)\=:E565/ " catch E565: not allowed here
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001392 endtry
1393 " TODO: Might be a bug: should x really be inserted here
1394 call assert_equal(['xa'], getline(1, '$'))
1395 delfu DoIt
1396 try
1397 call feedkeys("ix\<esc>", 'tnix')
1398 call assert_fails(1, 'unknown function')
1399 catch /^Vim\%((\a\+)\)\=:E117/ " catch E117: unknown function
1400 endtry
1401 au! InsertCharPre
Bram Moolenaar52797ba2021-12-16 14:45:13 +00001402
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001403 " 3) edit when completion is shown
1404 fun! Complete(findstart, base)
1405 if a:findstart
1406 return col('.')
1407 else
1408 call feedkeys("i\<del>\<esc>", 'tnix')
1409 return []
1410 endif
1411 endfun
1412 set completefunc=Complete
1413 try
1414 call feedkeys("i\<c-x>\<c-u>\<esc>", 'tnix')
1415 call assert_fails(1, 'change in complete function')
Bram Moolenaarff06f282020-04-21 22:01:14 +02001416 catch /^Vim\%((\a\+)\)\=:E565/ " catch E565
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001417 endtry
1418 delfu Complete
1419 set completefunc=
Bram Moolenaar52797ba2021-12-16 14:45:13 +00001420
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001421 if has("rightleft") && exists("+fkmap")
1422 " 4) 'R' when 'fkmap' and 'revins' is set.
1423 set revins fkmap
1424 try
1425 normal Ri
1426 call assert_fails(1, "R with 'fkmap' and 'ri' set")
1427 catch
1428 finally
1429 set norevins nofkmap
1430 endtry
1431 endif
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001432 bw!
1433endfunc
1434
Bram Moolenaar1e115362019-01-09 23:01:02 +01001435func Test_edit_rightleft()
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001436 " Cursor in rightleft mode moves differently
Bram Moolenaar845e0ee2020-06-20 16:05:32 +02001437 CheckFeature rightleft
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001438 call NewWindow(10, 20)
1439 call setline(1, ['abc', 'def', 'ghi'])
1440 call cursor(1, 2)
1441 set rightleft
1442 " Screen looks as expected
1443 let lines = ScreenLines([1, 4], winwidth(0))
1444 let expect = [
1445 \" cba",
1446 \" fed",
1447 \" ihg",
1448 \" ~"]
1449 call assert_equal(join(expect, "\n"), join(lines, "\n"))
1450 " 2) right moves to the left
1451 call feedkeys("i\<right>\<esc>x", 'txin')
1452 call assert_equal(['bc', 'def', 'ghi'], getline(1,'$'))
1453 call cursor(1, 2)
1454 call feedkeys("i\<s-right>\<esc>", 'txin')
1455 call cursor(1, 2)
1456 call feedkeys("i\<c-right>\<esc>", 'txin')
1457 " Screen looks as expected
1458 let lines = ScreenLines([1, 4], winwidth(0))
1459 let expect = [
1460 \" cb",
1461 \" fed",
1462 \" ihg",
1463 \" ~"]
1464 call assert_equal(join(expect, "\n"), join(lines, "\n"))
1465 " 2) left moves to the right
1466 call setline(1, ['abc', 'def', 'ghi'])
1467 call cursor(1, 2)
1468 call feedkeys("i\<left>\<esc>x", 'txin')
1469 call assert_equal(['ac', 'def', 'ghi'], getline(1,'$'))
1470 call cursor(1, 2)
1471 call feedkeys("i\<s-left>\<esc>", 'txin')
1472 call cursor(1, 2)
1473 call feedkeys("i\<c-left>\<esc>", 'txin')
1474 " Screen looks as expected
1475 let lines = ScreenLines([1, 4], winwidth(0))
1476 let expect = [
1477 \" ca",
1478 \" fed",
1479 \" ihg",
1480 \" ~"]
1481 call assert_equal(join(expect, "\n"), join(lines, "\n"))
Bram Moolenaar845e0ee2020-06-20 16:05:32 +02001482 %d _
1483 call test_override('redraw_flag', 1)
1484 call test_override('char_avail', 1)
1485 call feedkeys("a\<C-V>x41", "xt")
1486 redraw!
1487 call assert_equal(repeat(' ', 19) .. 'A', Screenline(1))
1488 call test_override('ALL', 0)
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001489 set norightleft
1490 bw!
1491endfunc
Bram Moolenaar658a3a22017-03-31 22:27:12 +02001492
1493func Test_edit_complete_very_long_name()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001494 " Long directory names only work on Unix.
1495 CheckUnix
Bram Moolenaar15ecbd62017-04-07 14:10:48 +02001496
1497 let dirname = getcwd() . "/Xdir"
1498 let longdirname = dirname . repeat('/' . repeat('d', 255), 4)
1499 try
1500 call mkdir(longdirname, 'p')
1501 catch /E739:/
1502 " Long directory name probably not supported.
1503 call delete(dirname, 'rf')
1504 return
1505 endtry
1506
Bram Moolenaarf8191c52019-05-18 17:22:54 +02001507 " Try to get the Vim window position before setting 'columns', so that we can
1508 " move the window back to where it was.
Bram Moolenaarba6ec182017-04-04 22:41:10 +02001509 let winposx = getwinposx()
1510 let winposy = getwinposy()
Bram Moolenaarf8191c52019-05-18 17:22:54 +02001511
1512 if winposx >= 0 && winposy >= 0 && !has('gui_running')
1513 " We did get the window position, but xterm may report the wrong numbers.
1514 " Move the window to the reported position and compute any offset.
1515 exe 'winpos ' . winposx . ' ' . winposy
1516 sleep 100m
1517 let x = getwinposx()
1518 if x >= 0
1519 let winposx += winposx - x
1520 endif
1521 let y = getwinposy()
1522 if y >= 0
1523 let winposy += winposy - y
1524 endif
1525 endif
1526
Bram Moolenaar658a3a22017-03-31 22:27:12 +02001527 let save_columns = &columns
Bram Moolenaar15ecbd62017-04-07 14:10:48 +02001528 " Need at least about 1100 columns to reproduce the problem.
Bram Moolenaarba6ec182017-04-04 22:41:10 +02001529 set columns=2000
Bram Moolenaar658a3a22017-03-31 22:27:12 +02001530 set noswapfile
Bram Moolenaarba6ec182017-04-04 22:41:10 +02001531
Bram Moolenaar658a3a22017-03-31 22:27:12 +02001532 let longfilename = longdirname . '/' . repeat('a', 255)
Bram Moolenaar658a3a22017-03-31 22:27:12 +02001533 call writefile(['Totum', 'Table'], longfilename)
1534 new
1535 exe "next Xfile " . longfilename
1536 exe "normal iT\<C-N>"
1537
1538 bwipe!
1539 exe 'bwipe! ' . longfilename
1540 call delete(dirname, 'rf')
1541 let &columns = save_columns
Bram Moolenaarba6ec182017-04-04 22:41:10 +02001542 if winposx >= 0 && winposy >= 0
1543 exe 'winpos ' . winposx . ' ' . winposy
1544 endif
Bram Moolenaar658a3a22017-03-31 22:27:12 +02001545 set swapfile&
1546endfunc
Bram Moolenaarff930ca2017-10-19 17:12:10 +02001547
Bram Moolenaar2c8c6812018-07-28 17:07:52 +02001548func Test_edit_backtick()
1549 next a\`b c
1550 call assert_equal('a`b', expand('%'))
1551 next
1552 call assert_equal('c', expand('%'))
1553 call assert_equal('a\`b c', expand('##'))
1554endfunc
1555
Bram Moolenaarff930ca2017-10-19 17:12:10 +02001556func Test_edit_quit()
1557 edit foo.txt
1558 split
1559 new
1560 call setline(1, 'hello')
1561 3wincmd w
1562 redraw!
1563 call assert_fails('1q', 'E37:')
1564 bwipe! foo.txt
1565 only
1566endfunc
1567
Bram Moolenaaradb8fbe2018-06-04 20:34:23 +02001568func Test_edit_alt()
1569 " Keeping the cursor line didn't happen when the first line has indent.
1570 new
1571 call setline(1, [' one', 'two', 'three'])
1572 w XAltFile
1573 $
1574 call assert_equal(3, line('.'))
1575 e Xother
1576 e #
1577 call assert_equal(3, line('.'))
1578
1579 bwipe XAltFile
1580 call delete('XAltFile')
1581endfunc
Bram Moolenaar4dbc2622018-11-02 11:59:15 +01001582
Bram Moolenaardb934952020-04-27 20:18:31 +02001583func Test_edit_InsertLeave()
Bram Moolenaar4dbc2622018-11-02 11:59:15 +01001584 new
Bram Moolenaarb53e13a2020-10-21 12:19:53 +02001585 au InsertLeavePre * let g:did_au_pre = 1
Bram Moolenaar4dbc2622018-11-02 11:59:15 +01001586 au InsertLeave * let g:did_au = 1
Bram Moolenaarb53e13a2020-10-21 12:19:53 +02001587 let g:did_au_pre = 0
Bram Moolenaar4dbc2622018-11-02 11:59:15 +01001588 let g:did_au = 0
1589 call feedkeys("afoo\<Esc>", 'tx')
Bram Moolenaarb53e13a2020-10-21 12:19:53 +02001590 call assert_equal(1, g:did_au_pre)
Bram Moolenaar4dbc2622018-11-02 11:59:15 +01001591 call assert_equal(1, g:did_au)
1592 call assert_equal('foo', getline(1))
1593
Bram Moolenaarb53e13a2020-10-21 12:19:53 +02001594 let g:did_au_pre = 0
Bram Moolenaar4dbc2622018-11-02 11:59:15 +01001595 let g:did_au = 0
1596 call feedkeys("Sbar\<C-C>", 'tx')
Bram Moolenaarb53e13a2020-10-21 12:19:53 +02001597 call assert_equal(1, g:did_au_pre)
Bram Moolenaar4dbc2622018-11-02 11:59:15 +01001598 call assert_equal(0, g:did_au)
1599 call assert_equal('bar', getline(1))
1600
1601 inoremap x xx<Esc>
Bram Moolenaarb53e13a2020-10-21 12:19:53 +02001602 let g:did_au_pre = 0
Bram Moolenaar4dbc2622018-11-02 11:59:15 +01001603 let g:did_au = 0
1604 call feedkeys("Saax", 'tx')
Bram Moolenaarb53e13a2020-10-21 12:19:53 +02001605 call assert_equal(1, g:did_au_pre)
Bram Moolenaar4dbc2622018-11-02 11:59:15 +01001606 call assert_equal(1, g:did_au)
1607 call assert_equal('aaxx', getline(1))
1608
1609 inoremap x xx<C-C>
Bram Moolenaarb53e13a2020-10-21 12:19:53 +02001610 let g:did_au_pre = 0
Bram Moolenaar4dbc2622018-11-02 11:59:15 +01001611 let g:did_au = 0
1612 call feedkeys("Sbbx", 'tx')
Bram Moolenaarb53e13a2020-10-21 12:19:53 +02001613 call assert_equal(1, g:did_au_pre)
Bram Moolenaar4dbc2622018-11-02 11:59:15 +01001614 call assert_equal(0, g:did_au)
1615 call assert_equal('bbxx', getline(1))
1616
1617 bwipe!
Bram Moolenaarb53e13a2020-10-21 12:19:53 +02001618 au! InsertLeave InsertLeavePre
Bram Moolenaar4dbc2622018-11-02 11:59:15 +01001619 iunmap x
1620endfunc
Bram Moolenaarc6b37db2019-04-27 18:00:34 +02001621
Bram Moolenaardb934952020-04-27 20:18:31 +02001622func Test_edit_InsertLeave_undo()
1623 new XtestUndo
1624 set undofile
1625 au InsertLeave * wall
1626 exe "normal ofoo\<Esc>"
1627 call assert_equal(2, line('$'))
1628 normal u
1629 call assert_equal(1, line('$'))
1630
1631 bwipe!
1632 au! InsertLeave
1633 call delete('XtestUndo')
Bram Moolenaarb340bae2020-06-15 19:51:56 +02001634 call delete(undofile('XtestUndo'))
Bram Moolenaardb934952020-04-27 20:18:31 +02001635 set undofile&
1636endfunc
1637
Bram Moolenaarc6b37db2019-04-27 18:00:34 +02001638" Test for inserting characters using CTRL-V followed by a number.
1639func Test_edit_special_chars()
1640 new
1641
Bram Moolenaar424bcae2022-01-31 14:59:41 +00001642 let t = "o\<C-V>65\<C-V>x42\<C-V>o103 \<C-V>33a\<C-V>xfg\<C-V>o78\<Esc>"
Bram Moolenaarc6b37db2019-04-27 18:00:34 +02001643
1644 exe "normal " . t
1645 call assert_equal("ABC !a\<C-O>g\<C-G>8", getline(2))
1646
1647 close!
1648endfunc
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02001649
1650func Test_edit_startinsert()
1651 new
1652 set backspace+=start
1653 call setline(1, 'foobar')
1654 call feedkeys("A\<C-U>\<Esc>", 'xt')
1655 call assert_equal('', getline(1))
1656
1657 call setline(1, 'foobar')
1658 call feedkeys(":startinsert!\<CR>\<C-U>\<Esc>", 'xt')
1659 call assert_equal('', getline(1))
1660
1661 set backspace&
1662 bwipe!
1663endfunc
Bram Moolenaar177c9f22019-11-06 13:59:16 +01001664
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +01001665" Test for :startreplace and :startgreplace
1666func Test_edit_startreplace()
1667 new
1668 call setline(1, 'abc')
1669 call feedkeys("l:startreplace\<CR>xyz\e", 'xt')
1670 call assert_equal('axyz', getline(1))
1671 call feedkeys("0:startreplace!\<CR>abc\e", 'xt')
1672 call assert_equal('axyzabc', getline(1))
1673 call setline(1, "a\tb")
1674 call feedkeys("0l:startgreplace\<CR>xyz\e", 'xt')
1675 call assert_equal("axyz\tb", getline(1))
1676 call feedkeys("0i\<C-R>=execute('startreplace')\<CR>12\e", 'xt')
1677 call assert_equal("12axyz\tb", getline(1))
1678 close!
1679endfunc
1680
Bram Moolenaar177c9f22019-11-06 13:59:16 +01001681func Test_edit_noesckeys()
Bram Moolenaar215ba3b2019-11-06 15:07:07 +01001682 CheckNotGui
Bram Moolenaar177c9f22019-11-06 13:59:16 +01001683 new
1684
1685 " <Left> moves cursor when 'esckeys' is set
1686 exe "set t_kl=\<Esc>OD"
1687 set esckeys
1688 call feedkeys("axyz\<Esc>ODX", "xt")
1689 call assert_equal("xyXz", getline(1))
1690
1691 " <Left> exits Insert mode when 'esckeys' is off
1692 set noesckeys
1693 call setline(1, '')
1694 call feedkeys("axyz\<Esc>ODX", "xt")
1695 call assert_equal(["DX", "xyz"], getline(1, 2))
1696
1697 bwipe!
1698 set esckeys
1699endfunc
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +01001700
Bram Moolenaar1671f442020-03-10 07:48:13 +01001701" Test for running an invalid ex command in insert mode using CTRL-O
Bram Moolenaar1671f442020-03-10 07:48:13 +01001702func Test_edit_ctrl_o_invalid_cmd()
1703 new
1704 set showmode showcmd
Bram Moolenaarb340bae2020-06-15 19:51:56 +02001705 " Avoid a sleep of 3 seconds. Zero might have side effects.
1706 call test_override('ui_delay', 50)
Bram Moolenaar1671f442020-03-10 07:48:13 +01001707 let caught_e492 = 0
1708 try
1709 call feedkeys("i\<C-O>:invalid\<CR>abc\<Esc>", "xt")
1710 catch /E492:/
1711 let caught_e492 = 1
1712 endtry
1713 call assert_equal(1, caught_e492)
1714 call assert_equal('abc', getline(1))
1715 set showmode& showcmd&
Bram Moolenaarb340bae2020-06-15 19:51:56 +02001716 call test_override('ui_delay', 0)
Bram Moolenaar1671f442020-03-10 07:48:13 +01001717 close!
1718endfunc
1719
Bram Moolenaarb340bae2020-06-15 19:51:56 +02001720" Test for editing a file with a very long name
1721func Test_edit_illegal_filename()
1722 CheckEnglish
1723 new
1724 redir => msg
1725 exe 'edit ' . repeat('f', 5000)
1726 redir END
1727 call assert_match("Illegal file name$", split(msg, "\n")[0])
1728 close!
1729endfunc
1730
Bram Moolenaarc8fe6452020-10-03 17:04:37 +02001731" Test for editing a directory
1732func Test_edit_is_a_directory()
1733 CheckEnglish
1734 let dirname = getcwd() . "/Xdir"
1735 call mkdir(dirname, 'p')
1736
1737 new
1738 redir => msg
1739 exe 'edit' dirname
1740 redir END
1741 call assert_match("is a directory$", split(msg, "\n")[0])
1742 bwipe!
1743
1744 let dirname .= '/'
1745
1746 new
1747 redir => msg
1748 exe 'edit' dirname
1749 redir END
1750 call assert_match("is a directory$", split(msg, "\n")[0])
1751 bwipe!
1752
1753 call delete(dirname, 'rf')
1754endfunc
1755
Bram Moolenaarb340bae2020-06-15 19:51:56 +02001756" Test for editing a file using invalid file encoding
1757func Test_edit_invalid_encoding()
1758 CheckEnglish
1759 call writefile([], 'Xfile')
1760 redir => msg
1761 new ++enc=axbyc Xfile
1762 redir END
1763 call assert_match('\[NOT converted\]', msg)
1764 call delete('Xfile')
1765 close!
1766endfunc
1767
1768" Test for the "charconvert" option
1769func Test_edit_charconvert()
1770 CheckEnglish
1771 call writefile(['one', 'two'], 'Xfile')
1772
1773 " set 'charconvert' to a non-existing function
1774 set charconvert=NonExitingFunc()
1775 new
1776 let caught_e117 = v:false
1777 try
1778 redir => msg
1779 edit ++enc=axbyc Xfile
1780 catch /E117:/
1781 let caught_e117 = v:true
1782 finally
1783 redir END
1784 endtry
1785 call assert_true(caught_e117)
1786 call assert_equal(['one', 'two'], getline(1, '$'))
1787 call assert_match("Conversion with 'charconvert' failed", msg)
1788 close!
1789 set charconvert&
1790
1791 " 'charconvert' function doesn't create a output file
1792 func Cconv1()
1793 endfunc
1794 set charconvert=Cconv1()
1795 new
1796 redir => msg
1797 edit ++enc=axbyc Xfile
1798 redir END
1799 call assert_equal(['one', 'two'], getline(1, '$'))
1800 call assert_match("can't read output of 'charconvert'", msg)
1801 close!
1802 delfunc Cconv1
1803 set charconvert&
1804
1805 " 'charconvert' function to convert to upper case
1806 func Cconv2()
1807 let data = readfile(v:fname_in)
1808 call map(data, 'toupper(v:val)')
1809 call writefile(data, v:fname_out)
1810 endfunc
1811 set charconvert=Cconv2()
1812 new Xfile
1813 write ++enc=ucase Xfile1
1814 call assert_equal(['ONE', 'TWO'], readfile('Xfile1'))
1815 call delete('Xfile1')
1816 close!
1817 delfunc Cconv2
1818 set charconvert&
1819
1820 " 'charconvert' function removes the input file
1821 func Cconv3()
1822 call delete(v:fname_in)
1823 endfunc
1824 set charconvert=Cconv3()
1825 new
1826 call assert_fails('edit ++enc=lcase Xfile', 'E202:')
1827 call assert_equal([''], getline(1, '$'))
1828 close!
1829 delfunc Cconv3
1830 set charconvert&
1831
1832 call delete('Xfile')
1833endfunc
1834
1835" Test for editing a file without read permission
1836func Test_edit_file_no_read_perm()
1837 CheckUnix
Bram Moolenaar17709e22021-03-19 14:38:12 +01001838 CheckNotRoot
1839
Bram Moolenaarb340bae2020-06-15 19:51:56 +02001840 call writefile(['one', 'two'], 'Xfile')
1841 call setfperm('Xfile', '-w-------')
1842 new
1843 redir => msg
1844 edit Xfile
1845 redir END
1846 call assert_equal(1, &readonly)
1847 call assert_equal([''], getline(1, '$'))
1848 call assert_match('\[Permission Denied\]', msg)
1849 close!
1850 call delete('Xfile')
1851endfunc
1852
zeertzjq3a56b6d2022-04-08 11:56:14 +01001853" Using :edit without leaving 'insertmode' should not cause Insert mode to be
1854" re-entered immediately after <C-L>
1855func Test_edit_insertmode_ex_edit()
1856 CheckRunVimInTerminal
1857
1858 let lines =<< trim END
1859 set insertmode noruler
1860 inoremap <C-B> <Cmd>edit Xfoo<CR>
1861 END
1862 call writefile(lines, 'Xtest_edit_insertmode_ex_edit')
1863
1864 let buf = RunVimInTerminal('-S Xtest_edit_insertmode_ex_edit', #{rows: 6})
1865 call TermWait(buf, 50)
1866 call assert_match('^-- INSERT --\s*$', term_getline(buf, 6))
1867 call term_sendkeys(buf, "\<C-B>\<C-L>")
1868 call TermWait(buf, 50)
1869 call assert_notmatch('^-- INSERT --\s*$', term_getline(buf, 6))
1870
1871 " clean up
1872 call StopVimInTerminal(buf)
1873 call delete('Xtest_edit_insertmode_ex_edit')
1874endfunc
1875
Bram Moolenaar845e0ee2020-06-20 16:05:32 +02001876" Pressing escape in 'insertmode' should beep
1877func Test_edit_insertmode_esc_beeps()
1878 new
1879 set insertmode
1880 call assert_beeps("call feedkeys(\"one\<Esc>\", 'xt')")
1881 set insertmode&
1882 " unsupported CTRL-G command should beep in insert mode.
1883 call assert_beeps("normal i\<C-G>l")
1884 close!
1885endfunc
1886
1887" Test for 'hkmap' and 'hkmapp'
1888func Test_edit_hkmap()
1889 CheckFeature rightleft
1890 if has('win32') && !has('gui')
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001891 throw 'Skipped: fails on the MS-Windows terminal version'
Bram Moolenaar845e0ee2020-06-20 16:05:32 +02001892 endif
1893 new
1894
1895 set revins hkmap
1896 let str = 'abcdefghijklmnopqrstuvwxyz'
1897 let str ..= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
1898 let str ..= '`/'',.;'
1899 call feedkeys('i' .. str, 'xt')
1900 let expected = "óõú,.;"
1901 let expected ..= "ZYXWVUTSRQPONMLKJIHGFEDCBA"
1902 let expected ..= "æèñ'äåàãø/ôíîöêìçïéòë÷âáðù"
1903 call assert_equal(expected, getline(1))
1904
1905 %d
1906 set revins hkmap hkmapp
1907 let str = 'abcdefghijklmnopqrstuvwxyz'
1908 let str ..= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
1909 call feedkeys('i' .. str, 'xt')
1910 let expected = "õYXWVUTSRQóOïíLKJIHGFEDêBA"
1911 let expected ..= "öòXùåèúæø'ôñðîì÷çéäâóǟãëáà"
1912 call assert_equal(expected, getline(1))
1913
1914 set revins& hkmap& hkmapp&
1915 close!
1916endfunc
1917
1918" Test for 'allowrevins' and using CTRL-_ in insert mode
1919func Test_edit_allowrevins()
1920 CheckFeature rightleft
1921 new
1922 set allowrevins
1923 call feedkeys("iABC\<C-_>DEF\<C-_>GHI", 'xt')
1924 call assert_equal('ABCFEDGHI', getline(1))
1925 set allowrevins&
1926 close!
1927endfunc
1928
1929" Test for inserting a register in insert mode using CTRL-R
1930func Test_edit_insert_reg()
1931 new
1932 let g:Line = ''
1933 func SaveFirstLine()
1934 let g:Line = Screenline(1)
1935 return 'r'
1936 endfunc
1937 inoremap <expr> <buffer> <F2> SaveFirstLine()
1938 call test_override('redraw_flag', 1)
1939 call test_override('char_avail', 1)
1940 let @r = 'sample'
1941 call feedkeys("a\<C-R>=SaveFirstLine()\<CR>", "xt")
1942 call assert_equal('"', g:Line)
1943 call test_override('ALL', 0)
1944 close!
1945endfunc
1946
1947" When a character is inserted at the last position of the last line in a
1948" window, the window contents should be scrolled one line up. If the top line
1949" is part of a fold, then the entire fold should be scrolled up.
1950func Test_edit_lastline_scroll()
1951 new
1952 let h = winheight(0)
1953 let lines = ['one', 'two', 'three']
1954 let lines += repeat(['vim'], h - 4)
1955 call setline(1, lines)
1956 call setline(h, repeat('x', winwidth(0) - 1))
1957 call feedkeys("GAx", 'xt')
1958 redraw!
1959 call assert_equal(h - 1, winline())
1960 call assert_equal(2, line('w0'))
1961
1962 " scroll with a fold
1963 1,2fold
1964 normal gg
1965 call setline(h + 1, repeat('x', winwidth(0) - 1))
1966 call feedkeys("GAx", 'xt')
1967 redraw!
1968 call assert_equal(h - 1, winline())
1969 call assert_equal(3, line('w0'))
1970
1971 close!
1972endfunc
1973
Bram Moolenaar21cbe172020-10-13 19:08:24 +02001974func Test_edit_browse()
1975 " in the GUI this opens a file picker, we only test the terminal behavior
1976 CheckNotGui
1977
1978 " ":browse xxx" checks for the FileExplorer augroup and assumes editing "."
1979 " works then.
1980 augroup FileExplorer
1981 au!
1982 augroup END
1983
1984 " When the USE_FNAME_CASE is defined this used to cause a crash.
1985 browse enew
1986 bwipe!
1987
1988 browse split
1989 bwipe!
1990endfunc
1991
Bram Moolenaarcaf73dc2020-10-26 21:39:13 +01001992func Test_read_invalid()
1993 set encoding=latin1
1994 " This was not properly checking for going past the end.
1995 call assert_fails('r`=', 'E484')
1996 set encoding=utf-8
1997endfunc
1998
Yegappan Lakshmanan59585492021-06-12 13:46:41 +02001999" Test for the 'revins' option
2000func Test_edit_revins()
2001 CheckFeature rightleft
2002 new
2003 set revins
2004 exe "normal! ione\ttwo three"
2005 call assert_equal("eerht owt\teno", getline(1))
2006 call setline(1, "one\ttwo three")
2007 normal! gg$bi a
2008 call assert_equal("one\ttwo a three", getline(1))
2009 exe "normal! $bi\<BS>\<BS>"
2010 call assert_equal("one\ttwo a ree", getline(1))
2011 exe "normal! 0wi\<C-W>"
2012 call assert_equal("one\t a ree", getline(1))
2013 exe "normal! 0wi\<C-U>"
2014 call assert_equal("one\t ", getline(1))
2015 " newline in insert mode starts at the end of the line
2016 call setline(1, 'one two three')
2017 exe "normal! wi\nfour"
2018 call assert_equal(['one two three', 'ruof'], getline(1, '$'))
2019 set revins&
2020 bw!
2021endfunc
2022
Bram Moolenaar35a9a002021-09-11 21:14:20 +02002023" Test for getting the character of the line below after "p"
2024func Test_edit_put_CTRL_E()
2025 set encoding=latin1
2026 new
2027 let @" = ''
2028 sil! norm orggRx
2029 sil! norm pr
2030 call assert_equal(['r', 'r'], getline(1, 2))
2031 bwipe!
2032 set encoding=utf-8
2033endfunc
2034
=?UTF-8?q?Magnus=20Gro=C3=9F?=f1e88762021-09-12 13:39:55 +02002035" Test for ModeChanged pattern
2036func Test_mode_changes()
=?UTF-8?q?Magnus=20Gro=C3=9F?=f1e88762021-09-12 13:39:55 +02002037 let g:index = 0
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01002038 let g:mode_seq = ['n', 'i', 'n', 'v', 'V', 'i', 'ix', 'i', 'ic', 'i', 'n', 'no', 'n', 'V', 'v', 's', 'n']
=?UTF-8?q?Magnus=20Gro=C3=9F?=f1e88762021-09-12 13:39:55 +02002039 func! TestMode()
2040 call assert_equal(g:mode_seq[g:index], get(v:event, "old_mode"))
2041 call assert_equal(g:mode_seq[g:index + 1], get(v:event, "new_mode"))
Bram Moolenaard85931e2021-10-02 16:55:39 +01002042 call assert_equal(mode(1), get(v:event, "new_mode"))
=?UTF-8?q?Magnus=20Gro=C3=9F?=f1e88762021-09-12 13:39:55 +02002043 let g:index += 1
2044 endfunc
2045
2046 au ModeChanged * :call TestMode()
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01002047 let g:n_to_any = 0
2048 au ModeChanged n:* let g:n_to_any += 1
2049 call feedkeys("i\<esc>vVca\<CR>\<C-X>\<C-L>\<esc>ggdG", 'tnix')
=?UTF-8?q?Magnus=20Gro=C3=9F?=f1e88762021-09-12 13:39:55 +02002050
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01002051 let g:V_to_v = 0
2052 au ModeChanged V:v let g:V_to_v += 1
2053 call feedkeys("Vv\<C-G>\<esc>", 'tnix')
2054 call assert_equal(len(filter(g:mode_seq[1:], {idx, val -> val == 'n'})), g:n_to_any)
2055 call assert_equal(1, g:V_to_v)
Bram Moolenaard85931e2021-10-02 16:55:39 +01002056 call assert_equal(len(g:mode_seq) - 1, g:index)
=?UTF-8?q?Magnus=20Gro=C3=9F?=f1e88762021-09-12 13:39:55 +02002057
Bram Moolenaar72d2fa62021-10-02 21:48:15 +01002058 let g:n_to_i = 0
2059 au ModeChanged n:i let g:n_to_i += 1
2060 let g:n_to_niI = 0
2061 au ModeChanged i:niI let g:n_to_niI += 1
2062 let g:niI_to_i = 0
2063 au ModeChanged niI:i let g:niI_to_i += 1
2064 let g:nany_to_i = 0
2065 au ModeChanged n*:i let g:nany_to_i += 1
2066 let g:i_to_n = 0
2067 au ModeChanged i:n let g:i_to_n += 1
2068 let g:nori_to_any = 0
2069 au ModeChanged [ni]:* let g:nori_to_any += 1
2070 let g:i_to_any = 0
2071 au ModeChanged i:* let g:i_to_any += 1
Bram Moolenaard85931e2021-10-02 16:55:39 +01002072 let g:index = 0
2073 let g:mode_seq = ['n', 'i', 'niI', 'i', 'n']
2074 call feedkeys("a\<C-O>l\<esc>", 'tnix')
=?UTF-8?q?Magnus=20Gro=C3=9F?=f1e88762021-09-12 13:39:55 +02002075 call assert_equal(len(g:mode_seq) - 1, g:index)
Bram Moolenaar72d2fa62021-10-02 21:48:15 +01002076 call assert_equal(1, g:n_to_i)
2077 call assert_equal(1, g:n_to_niI)
2078 call assert_equal(1, g:niI_to_i)
2079 call assert_equal(2, g:nany_to_i)
2080 call assert_equal(1, g:i_to_n)
2081 call assert_equal(2, g:i_to_any)
2082 call assert_equal(3, g:nori_to_any)
=?UTF-8?q?Magnus=20Gro=C3=9F?=f1e88762021-09-12 13:39:55 +02002083
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01002084 if has('terminal')
2085 let g:mode_seq += ['c', 'n', 't', 'nt', 'c', 'nt', 'n']
2086 call feedkeys(":term\<CR>\<C-W>N:bd!\<CR>", 'tnix')
2087 call assert_equal(len(g:mode_seq) - 1, g:index)
2088 call assert_equal(1, g:n_to_i)
2089 call assert_equal(1, g:n_to_niI)
2090 call assert_equal(1, g:niI_to_i)
2091 call assert_equal(2, g:nany_to_i)
2092 call assert_equal(1, g:i_to_n)
2093 call assert_equal(2, g:i_to_any)
2094 call assert_equal(5, g:nori_to_any)
2095 endif
2096
=?UTF-8?q?Magnus=20Gro=C3=9F?=f1e88762021-09-12 13:39:55 +02002097 au! ModeChanged
2098 delfunc TestMode
2099 unlet! g:mode_seq
2100 unlet! g:index
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01002101 unlet! g:n_to_any
2102 unlet! g:V_to_v
2103 unlet! g:n_to_i
2104 unlet! g:n_to_niI
2105 unlet! g:niI_to_i
2106 unlet! g:nany_to_i
2107 unlet! g:i_to_n
2108 unlet! g:nori_to_any
2109 unlet! g:i_to_any
=?UTF-8?q?Magnus=20Gro=C3=9F?=f1e88762021-09-12 13:39:55 +02002110endfunc
2111
Bram Moolenaar3075a452021-11-17 15:51:52 +00002112func Test_recursive_ModeChanged()
2113 au! ModeChanged * norm 0u
2114 sil! norm 
Bram Moolenaara0620062021-11-17 16:52:40 +00002115 au! ModeChanged
2116endfunc
2117
2118func Test_ModeChanged_starts_visual()
2119 " This was triggering ModeChanged before setting VIsual, causing a crash.
2120 au! ModeChanged * norm 0u
2121 sil! norm 
2122
2123 au! ModeChanged
Bram Moolenaar3075a452021-11-17 15:51:52 +00002124endfunc
2125
Dominique Pelle9cd063e2021-10-28 21:06:05 +01002126" Test toggling of input method. See :help i_CTRL-^
2127func Test_edit_CTRL_hat()
2128 CheckFeature xim
Dominique Pelle8753c1d2021-10-31 20:19:17 +00002129
Bram Moolenaar0b962e52022-04-03 18:02:37 +01002130 " FIXME: test fails with Motif GUI.
Dominique Pelle8753c1d2021-10-31 20:19:17 +00002131 " test also fails when running in the GUI.
2132 CheckFeature gui_gtk
2133 CheckNotGui
Dominique Pelle9cd063e2021-10-28 21:06:05 +01002134
2135 new
2136
2137 call assert_equal(0, &iminsert)
2138 call feedkeys("i\<C-^>", 'xt')
2139 call assert_equal(2, &iminsert)
2140 call feedkeys("i\<C-^>", 'xt')
2141 call assert_equal(0, &iminsert)
2142
2143 bwipe!
2144endfunc
2145
Bram Moolenaarde05bb22022-01-13 13:08:14 +00002146" Weird long file name was going over the end of NameBuff
2147func Test_edit_overlong_file_name()
2148 CheckUnix
2149
2150 file 0000000000000000000000000000
2151 file %%%%%%%%%%%%%%%%%%%%%%%%%%
2152 file %%%%%%
2153 set readonly
2154 set ls=2
2155
2156 redraw!
2157 set noreadonly ls&
2158 bwipe!
2159endfunc
2160
Christian Brabandtdfbdadc2022-05-05 20:46:47 +01002161func Test_edit_shift_bs()
2162 CheckMSWindows
2163
2164 " FIXME: this works interactively, but the test fails
2165 throw 'Skipped: Shift-Backspace Test not working correctly :('
2166
2167 " Need to run this in Win32 Terminal, do not use CheckRunVimInTerminal
2168 if !has("terminal")
2169 return
2170 endif
2171
2172 " Shift Backspace should work like Backspace in insert mode
2173 let lines =<< trim END
2174 call setline(1, ['abc'])
2175 END
2176 call writefile(lines, 'Xtest_edit_shift_bs')
2177
2178 let buf = RunVimInTerminal('-S Xtest_edit_shift_bs', #{rows: 3})
2179 call term_sendkeys(buf, "A\<S-BS>-\<esc>")
2180 call TermWait(buf, 50)
2181 call assert_equal('ab-', term_getline(buf, 1))
2182
2183 " clean up
2184 call StopVimInTerminal(buf)
2185 call delete('Xtest_edit_shift_bs')
2186endfunc
Dominique Pelle9cd063e2021-10-28 21:06:05 +01002187
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +01002188" vim: shiftwidth=2 sts=2 expandtab