blob: f0896d2c5cbb3d1c4359cf5a7eb6fcd60a47c3a6 [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)
zeertzjq01d4efe2023-01-25 15:31:28 +0000333 call assert_equal(expand('<SID>') .. 'NewIndentExpr()', &g:indentexpr)
Yegappan Lakshmanan8bb65f22021-12-26 10:51:39 +0000334 set indentexpr=<SID>NewIndentExpr()
335 call assert_equal(expand('<SID>') .. 'NewIndentExpr()', &indentexpr)
zeertzjq01d4efe2023-01-25 15:31:28 +0000336 call assert_equal(expand('<SID>') .. 'NewIndentExpr()', &g:indentexpr)
337 setlocal indentexpr=
338 setglobal indentexpr=s:NewIndentExpr()
339 call assert_equal(expand('<SID>') .. 'NewIndentExpr()', &g:indentexpr)
340 call assert_equal('', &indentexpr)
341 new
342 call assert_equal(expand('<SID>') .. 'NewIndentExpr()', &indentexpr)
343 bw!
344 setglobal indentexpr=<SID>NewIndentExpr()
345 call assert_equal(expand('<SID>') .. 'NewIndentExpr()', &g:indentexpr)
346 call assert_equal('', &indentexpr)
347 new
348 call assert_equal(expand('<SID>') .. 'NewIndentExpr()', &indentexpr)
349 bw!
Yegappan Lakshmanan8bb65f22021-12-26 10:51:39 +0000350 set indentexpr&
351
Bram Moolenaar1b383442017-09-26 20:04:54 +0200352 bw!
353endfunc
354
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +0200355" Test changing indent in replace mode
Bram Moolenaar1e115362019-01-09 23:01:02 +0100356func Test_edit_12()
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100357 new
358 call setline(1, ["\tabc", "\tdef"])
359 call cursor(2, 4)
360 call feedkeys("R^\<c-d>", 'tnix')
361 call assert_equal(["\tabc", "def"], getline(1, '$'))
Bram Moolenaar4c313b12019-08-24 22:58:31 +0200362 call assert_equal([0, 2, 2, 0], '.'->getpos())
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100363 %d
364 call setline(1, ["\tabc", "\t\tdef"])
365 call cursor(2, 2)
366 call feedkeys("R^\<c-d>", 'tnix')
367 call assert_equal(["\tabc", "def"], getline(1, '$'))
368 call assert_equal([0, 2, 1, 0], getpos('.'))
369 %d
370 call setline(1, ["\tabc", "\t\tdef"])
371 call cursor(2, 2)
372 call feedkeys("R\<c-t>", 'tnix')
373 call assert_equal(["\tabc", "\t\t\tdef"], getline(1, '$'))
374 call assert_equal([0, 2, 2, 0], getpos('.'))
375 bw!
376 10vnew
377 call setline(1, ["\tabc", "\t\tdef"])
378 call cursor(2, 2)
379 call feedkeys("R\<c-t>", 'tnix')
380 call assert_equal(["\tabc", "\t\t\tdef"], getline(1, '$'))
381 call assert_equal([0, 2, 2, 0], getpos('.'))
382 %d
383 set sw=4
384 call setline(1, ["\tabc", "\t\tdef"])
385 call cursor(2, 2)
386 call feedkeys("R\<c-t>\<c-t>", 'tnix')
387 call assert_equal(["\tabc", "\t\t\tdef"], getline(1, '$'))
388 call assert_equal([0, 2, 2, 0], getpos('.'))
389 %d
390 call setline(1, ["\tabc", "\t\tdef"])
391 call cursor(2, 2)
392 call feedkeys("R\<c-t>\<c-t>", 'tnix')
393 call assert_equal(["\tabc", "\t\t\tdef"], getline(1, '$'))
394 call assert_equal([0, 2, 2, 0], getpos('.'))
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +0200395 set sw&
396
397 " In replace mode, after hitting enter in a line with tab characters,
398 " pressing backspace should restore the tab characters.
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100399 %d
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +0200400 setlocal autoindent backspace=2
401 call setline(1, "\tone\t\ttwo")
402 exe "normal ggRred\<CR>six" .. repeat("\<BS>", 8)
403 call assert_equal(["\tone\t\ttwo"], getline(1, '$'))
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100404 bw!
405endfunc
406
Bram Moolenaar1e115362019-01-09 23:01:02 +0100407func Test_edit_13()
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100408 " Test smartindenting
Bram Moolenaar8e145b82022-05-21 20:17:31 +0100409 new
410 set smartindent autoindent
411 call setline(1, ["\tabc"])
412 call feedkeys("A {\<cr>more\<cr>}\<esc>", 'tnix')
413 call assert_equal(["\tabc {", "\t\tmore", "\t}"], getline(1, '$'))
414 set smartindent& autoindent&
415 bwipe!
Bram Moolenaar2ba42382019-03-16 18:11:07 +0100416
417 " Test autoindent removing indent of blank line.
418 new
419 call setline(1, ' foo bar baz')
420 set autoindent
421 exe "normal 0eea\<CR>\<CR>\<Esc>"
422 call assert_equal(" foo bar", getline(1))
423 call assert_equal("", getline(2))
424 call assert_equal(" baz", getline(3))
425 set autoindent&
Bram Moolenaar845e0ee2020-06-20 16:05:32 +0200426
427 " pressing <C-U> to erase line should keep the indent with 'autoindent'
428 set backspace=2 autoindent
429 %d
430 exe "normal i\tone\<CR>three\<C-U>two"
431 call assert_equal(["\tone", "\ttwo"], getline(1, '$'))
432 set backspace& autoindent&
433
Bram Moolenaar2ba42382019-03-16 18:11:07 +0100434 bwipe!
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100435endfunc
436
Bram Moolenaar1f448d92021-03-22 19:37:06 +0100437" Test for autoindent removing indent when insert mode is stopped. Some parts
438" of the code is exercised only when interactive mode is used. So use Vim in a
439" terminal.
440func Test_autoindent_remove_indent()
441 CheckRunVimInTerminal
Bram Moolenaar61abe7d2022-08-30 21:46:08 +0100442 let buf = RunVimInTerminal('-N Xarifile', {'rows': 6, 'cols' : 20})
Bram Moolenaar1f448d92021-03-22 19:37:06 +0100443 call TermWait(buf)
444 call term_sendkeys(buf, ":set autoindent\n")
445 " leaving insert mode in a new line with indent added by autoindent, should
446 " remove the indent.
447 call term_sendkeys(buf, "i\<Tab>foo\<CR>\<Esc>")
Dominique Pelle923dce22021-11-21 11:36:04 +0000448 " Need to delay for some time, otherwise the code in getchar.c will not be
Bram Moolenaar1f448d92021-03-22 19:37:06 +0100449 " exercised.
450 call TermWait(buf, 50)
451 " when a line is wrapped and the cursor is at the start of the second line,
452 " leaving insert mode, should move the cursor back to the first line.
453 call term_sendkeys(buf, "o" .. repeat('x', 20) .. "\<Esc>")
Dominique Pelle923dce22021-11-21 11:36:04 +0000454 " Need to delay for some time, otherwise the code in getchar.c will not be
Bram Moolenaar1f448d92021-03-22 19:37:06 +0100455 " exercised.
456 call TermWait(buf, 50)
457 call term_sendkeys(buf, ":w\n")
458 call TermWait(buf)
459 call StopVimInTerminal(buf)
Bram Moolenaar61abe7d2022-08-30 21:46:08 +0100460 call assert_equal(["\tfoo", '', repeat('x', 20)], readfile('Xarifile'))
461 call delete('Xarifile')
Bram Moolenaar1f448d92021-03-22 19:37:06 +0100462endfunc
463
Bram Moolenaar1e115362019-01-09 23:01:02 +0100464func Test_edit_CR()
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100465 " Test for <CR> in insert mode
Dominique Pelle923dce22021-11-21 11:36:04 +0000466 " basically only in quickfix mode it's tested, the rest
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100467 " has been taken care of by other tests
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200468 CheckFeature quickfix
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100469 botright new
Bram Moolenaar14f91762022-09-21 15:13:52 +0100470 call writefile(range(1, 10), 'Xqflist.txt', 'D')
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100471 call setqflist([{'filename': 'Xqflist.txt', 'lnum': 2}])
472 copen
473 set modifiable
474 call feedkeys("A\<cr>", 'tnix')
475 call assert_equal('Xqflist.txt', bufname(''))
476 call assert_equal(2, line('.'))
477 cclose
478 botright new
479 call setloclist(0, [{'filename': 'Xqflist.txt', 'lnum': 10}])
480 lopen
481 set modifiable
482 call feedkeys("A\<cr>", 'tnix')
483 call assert_equal('Xqflist.txt', bufname(''))
484 call assert_equal(10, line('.'))
485 call feedkeys("A\<Enter>", 'tnix')
486 call feedkeys("A\<kEnter>", 'tnix')
487 call feedkeys("A\n", 'tnix')
488 call feedkeys("A\r", 'tnix')
489 call assert_equal(map(range(1, 10), 'string(v:val)') + ['', '', '', ''], getline(1, '$'))
Bram Moolenaar14f91762022-09-21 15:13:52 +0100490
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100491 bw!
492 lclose
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100493endfunc
494
Bram Moolenaar1e115362019-01-09 23:01:02 +0100495func Test_edit_CTRL_()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200496 CheckFeature rightleft
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100497 " disabled for Windows builds, why?
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200498 CheckNotMSWindows
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100499 let _encoding=&encoding
500 set encoding=utf-8
501 " Test for CTRL-_
502 new
503 call setline(1, ['abc'])
504 call cursor(1, 1)
505 call feedkeys("i\<c-_>xyz\<esc>", 'tnix')
506 call assert_equal(["\<C-_>xyzabc"], getline(1, '$'))
507 call assert_false(&revins)
508 set ari
509 call setline(1, ['abc'])
510 call cursor(1, 1)
511 call feedkeys("i\<c-_>xyz\<esc>", 'tnix')
512 call assert_equal(["æèñabc"], getline(1, '$'))
513 call assert_true(&revins)
514 call setline(1, ['abc'])
515 call cursor(1, 1)
516 call feedkeys("i\<c-_>xyz\<esc>", 'tnix')
517 call assert_equal(["xyzabc"], getline(1, '$'))
518 call assert_false(&revins)
519 set noari
520 let &encoding=_encoding
521 bw!
522endfunc
523
524" needs to come first, to have the @. register empty
Bram Moolenaar1e115362019-01-09 23:01:02 +0100525func Test_edit_00a_CTRL_A()
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100526 " Test pressing CTRL-A
527 new
528 call setline(1, repeat([''], 5))
529 call cursor(1, 1)
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100530 try
531 call feedkeys("A\<NUL>", 'tnix')
532 catch /^Vim\%((\a\+)\)\=:E29/
533 call assert_true(1, 'E29 error caught')
534 endtry
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100535 call cursor(1, 1)
536 call feedkeys("Afoobar \<esc>", 'tnix')
537 call cursor(2, 1)
538 call feedkeys("A\<c-a>more\<esc>", 'tnix')
539 call cursor(3, 1)
540 call feedkeys("A\<NUL>and more\<esc>", 'tnix')
541 call assert_equal(['foobar ', 'foobar more', 'foobar morend more', '', ''], getline(1, '$'))
542 bw!
543endfunc
544
Bram Moolenaar1e115362019-01-09 23:01:02 +0100545func Test_edit_CTRL_EY()
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100546 " Ctrl-E/ Ctrl-Y in insert mode completion to scroll
547 10new
548 call setline(1, range(1, 100))
549 call cursor(30, 1)
550 norm! z.
551 call feedkeys("A\<c-x>\<c-e>\<c-e>\<c-e>\<c-e>\<c-e>", 'tnix')
552 call assert_equal(30, winsaveview()['topline'])
553 call assert_equal([0, 30, 2, 0], getpos('.'))
554 call feedkeys("A\<c-x>\<c-e>\<c-e>\<c-e>\<c-e>\<c-e>", 'tnix')
555 call feedkeys("A\<c-x>".repeat("\<c-y>", 10), 'tnix')
556 call assert_equal(21, winsaveview()['topline'])
557 call assert_equal([0, 30, 2, 0], getpos('.'))
558 bw!
559endfunc
560
Bram Moolenaar1e115362019-01-09 23:01:02 +0100561func Test_edit_CTRL_G()
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100562 new
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100563 call setline(1, ['foobar', 'foobar', 'foobar'])
564 call cursor(2, 4)
565 call feedkeys("ioooooooo\<c-g>k\<c-r>.\<esc>", 'tnix')
566 call assert_equal(['foooooooooobar', 'foooooooooobar', 'foobar'], getline(1, '$'))
567 call assert_equal([0, 1, 11, 0], getpos('.'))
568 call feedkeys("i\<c-g>k\<esc>", 'tnix')
569 call assert_equal([0, 1, 10, 0], getpos('.'))
570 call cursor(2, 4)
571 call feedkeys("i\<c-g>jzzzz\<esc>", 'tnix')
572 call assert_equal(['foooooooooobar', 'foooooooooobar', 'foozzzzbar'], getline(1, '$'))
573 call assert_equal([0, 3, 7, 0], getpos('.'))
574 call feedkeys("i\<c-g>j\<esc>", 'tnix')
575 call assert_equal([0, 3, 6, 0], getpos('.'))
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100576 bw!
577endfunc
578
Bram Moolenaar1e115362019-01-09 23:01:02 +0100579func Test_edit_CTRL_I()
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100580 " Tab in completion mode
581 let path=expand("%:p:h")
582 new
Bram Moolenaarca851592018-06-06 21:04:07 +0200583 call setline(1, [path. "/", ''])
Bram Moolenaarc5379472017-03-16 22:38:00 +0100584 call feedkeys("Arunt\<c-x>\<c-f>\<tab>\<cr>\<esc>", 'tnix')
585 call assert_match('runtest\.vim', getline(1))
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100586 %d
Bram Moolenaar14f91762022-09-21 15:13:52 +0100587 call writefile(['one', 'two', 'three'], 'Xinclude.txt', 'D')
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100588 let include='#include Xinclude.txt'
589 call setline(1, [include, ''])
590 call cursor(2, 1)
591 call feedkeys("A\<c-x>\<tab>\<cr>\<esc>", 'tnix')
592 call assert_equal([include, 'one', ''], getline(1, '$'))
593 call feedkeys("2ggC\<c-x>\<tab>\<down>\<cr>\<esc>", 'tnix')
594 call assert_equal([include, 'two', ''], getline(1, '$'))
595 call feedkeys("2ggC\<c-x>\<tab>\<down>\<down>\<cr>\<esc>", 'tnix')
596 call assert_equal([include, 'three', ''], getline(1, '$'))
597 call feedkeys("2ggC\<c-x>\<tab>\<down>\<down>\<down>\<cr>\<esc>", 'tnix')
598 call assert_equal([include, '', ''], getline(1, '$'))
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100599 bw!
600endfunc
601
Bram Moolenaar1e115362019-01-09 23:01:02 +0100602func Test_edit_CTRL_K()
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100603 " Test pressing CTRL-K (basically only dictionary completion and digraphs
604 " the rest is already covered
Bram Moolenaar14f91762022-09-21 15:13:52 +0100605 call writefile(['A', 'AA', 'AAA', 'AAAA'], 'Xdictionary.txt', 'D')
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100606 set dictionary=Xdictionary.txt
607 new
608 call setline(1, 'A')
609 call cursor(1, 1)
610 call feedkeys("A\<c-x>\<c-k>\<cr>\<esc>", 'tnix')
611 call assert_equal(['AA', ''], getline(1, '$'))
612 %d
613 call setline(1, 'A')
614 call cursor(1, 1)
615 call feedkeys("A\<c-x>\<c-k>\<down>\<cr>\<esc>", 'tnix')
616 call assert_equal(['AAA'], getline(1, '$'))
617 %d
618 call setline(1, 'A')
619 call cursor(1, 1)
620 call feedkeys("A\<c-x>\<c-k>\<down>\<down>\<cr>\<esc>", 'tnix')
621 call assert_equal(['AAAA'], getline(1, '$'))
622 %d
623 call setline(1, 'A')
624 call cursor(1, 1)
625 call feedkeys("A\<c-x>\<c-k>\<down>\<down>\<down>\<cr>\<esc>", 'tnix')
626 call assert_equal(['A'], getline(1, '$'))
627 %d
628 call setline(1, 'A')
629 call cursor(1, 1)
630 call feedkeys("A\<c-x>\<c-k>\<down>\<down>\<down>\<down>\<cr>\<esc>", 'tnix')
631 call assert_equal(['AA'], getline(1, '$'))
632
Bram Moolenaar4b96df52020-01-26 22:00:26 +0100633 " press an unexpected key after dictionary completion
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100634 %d
635 call setline(1, 'A')
636 call cursor(1, 1)
637 call feedkeys("A\<c-x>\<c-k>\<c-]>\<cr>\<esc>", 'tnix')
638 call assert_equal(['AA', ''], getline(1, '$'))
639 %d
640 call setline(1, 'A')
641 call cursor(1, 1)
642 call feedkeys("A\<c-x>\<c-k>\<c-s>\<cr>\<esc>", 'tnix')
643 call assert_equal(["AA\<c-s>", ''], getline(1, '$'))
644 %d
645 call setline(1, 'A')
646 call cursor(1, 1)
647 call feedkeys("A\<c-x>\<c-k>\<c-f>\<cr>\<esc>", 'tnix')
648 call assert_equal(["AA\<c-f>", ''], getline(1, '$'))
649
650 set dictionary=
651 %d
652 call setline(1, 'A')
653 call cursor(1, 1)
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100654 let v:testing = 1
655 try
656 call feedkeys("A\<c-x>\<c-k>\<esc>", 'tnix')
657 catch
658 " error sleeps 2 seconds, when v:testing is not set
659 let v:testing = 0
660 endtry
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100661
Bram Moolenaar30276f22019-01-24 17:59:39 +0100662 call test_override("char_avail", 1)
663 set showcmd
664 %d
665 call feedkeys("A\<c-k>a:\<esc>", 'tnix')
666 call assert_equal(['ä'], getline(1, '$'))
667 call test_override("char_avail", 0)
668 set noshowcmd
669
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100670 bw!
671endfunc
672
Bram Moolenaar1e115362019-01-09 23:01:02 +0100673func Test_edit_CTRL_L()
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100674 " Test Ctrl-X Ctrl-L (line completion)
675 new
676 set complete=.
677 call setline(1, ['one', 'two', 'three', '', '', '', ''])
678 call cursor(4, 1)
679 call feedkeys("A\<c-x>\<c-l>\<esc>", 'tnix')
680 call assert_equal(['one', 'two', 'three', 'three', '', '', ''], getline(1, '$'))
681 call feedkeys("cct\<c-x>\<c-l>\<c-n>\<esc>", 'tnix')
682 call assert_equal(['one', 'two', 'three', 't', '', '', ''], getline(1, '$'))
683 call feedkeys("cct\<c-x>\<c-l>\<c-n>\<c-n>\<esc>", 'tnix')
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100684 call assert_equal(['one', 'two', 'three', 'two', '', '', ''], getline(1, '$'))
Bram Moolenaar02ae9b42018-02-09 15:06:02 +0100685 call feedkeys("cct\<c-x>\<c-l>\<c-n>\<c-n>\<c-n>\<esc>", 'tnix')
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100686 call assert_equal(['one', 'two', 'three', 'three', '', '', ''], getline(1, '$'))
Bram Moolenaar02ae9b42018-02-09 15:06:02 +0100687 call feedkeys("cct\<c-x>\<c-l>\<c-n>\<c-n>\<c-n>\<c-n>\<esc>", 'tnix')
688 call assert_equal(['one', 'two', 'three', 't', '', '', ''], getline(1, '$'))
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100689 call feedkeys("cct\<c-x>\<c-l>\<c-p>\<esc>", 'tnix')
690 call assert_equal(['one', 'two', 'three', 'two', '', '', ''], getline(1, '$'))
691 call feedkeys("cct\<c-x>\<c-l>\<c-p>\<c-p>\<esc>", 'tnix')
692 call assert_equal(['one', 'two', 'three', 't', '', '', ''], getline(1, '$'))
693 call feedkeys("cct\<c-x>\<c-l>\<c-p>\<c-p>\<c-p>\<esc>", 'tnix')
694 call assert_equal(['one', 'two', 'three', 'three', '', '', ''], getline(1, '$'))
695 set complete=
696 call cursor(5, 1)
697 call feedkeys("A\<c-x>\<c-l>\<c-p>\<c-n>\<esc>", 'tnix')
698 call assert_equal(['one', 'two', 'three', 'three', "\<c-l>\<c-p>\<c-n>", '', ''], getline(1, '$'))
699 set complete&
700 %d
701 if has("conceal") && has("syntax")
702 call setline(1, ['foo', 'bar', 'foobar'])
703 call test_override("char_avail", 1)
704 set conceallevel=2 concealcursor=n
705 syn on
706 syn match ErrorMsg "^bar"
707 call matchadd("Conceal", 'oo', 10, -1, {'conceal': 'X'})
708 func! DoIt()
709 let g:change=1
710 endfunc
711 au! TextChangedI <buffer> :call DoIt()
712
713 call cursor(2, 1)
714 call assert_false(exists("g:change"))
715 call feedkeys("A \<esc>", 'tnix')
716 call assert_equal(['foo', 'bar ', 'foobar'], getline(1, '$'))
717 call assert_equal(1, g:change)
718
719 call test_override("char_avail", 0)
720 call clearmatches()
721 syn off
722 au! TextChangedI
723 delfu DoIt
724 unlet! g:change
725 endif
726 bw!
727endfunc
728
Bram Moolenaar1e115362019-01-09 23:01:02 +0100729func Test_edit_CTRL_N()
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100730 " Check keyword completion
Bram Moolenaara1070ea2021-02-20 19:21:36 +0100731 for e in ['latin1', 'utf-8']
732 exe 'set encoding=' .. e
733 new
734 set complete=.
735 call setline(1, ['INFER', 'loWER', '', '', ])
736 call cursor(3, 1)
737 call feedkeys("Ai\<c-n>\<cr>\<esc>", "tnix")
738 call feedkeys("ILO\<c-n>\<cr>\<esc>", 'tnix')
739 call assert_equal(['INFER', 'loWER', 'i', 'LO', '', ''], getline(1, '$'), e)
740 %d
741 call setline(1, ['INFER', 'loWER', '', '', ])
742 call cursor(3, 1)
743 set ignorecase infercase
744 call feedkeys("Ii\<c-n>\<cr>\<esc>", "tnix")
745 call feedkeys("ILO\<c-n>\<cr>\<esc>", 'tnix')
746 call assert_equal(['INFER', 'loWER', 'infer', 'LOWER', '', ''], getline(1, '$'), e)
Yegappan Lakshmanane9825862022-01-03 11:03:48 +0000747 set noignorecase noinfercase
748 %d
749 call setline(1, ['one word', 'two word'])
750 exe "normal! Goo\<C-P>\<C-X>\<C-P>"
751 call assert_equal('one word', getline(3))
752 %d
753 set complete&
Bram Moolenaara1070ea2021-02-20 19:21:36 +0100754 bw!
755 endfor
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100756endfunc
757
Bram Moolenaar1e115362019-01-09 23:01:02 +0100758func Test_edit_CTRL_O()
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100759 " Check for CTRL-O in insert mode
760 new
761 inoreabbr <buffer> h here some more
762 call setline(1, ['abc', 'def'])
763 call cursor(1, 1)
764 " Ctrl-O after an abbreviation
765 exe "norm A h\<c-o>:set nu\<cr> text"
766 call assert_equal(['abc here some more text', 'def'], getline(1, '$'))
767 call assert_true(&nu)
768 set nonu
769 iunabbr <buffer> h
770 " Ctrl-O at end of line with 've'=onemore
771 call cursor(1, 1)
772 call feedkeys("A\<c-o>:let g:a=getpos('.')\<cr>\<esc>", 'tnix')
773 call assert_equal([0, 1, 23, 0], g:a)
774 call cursor(1, 1)
775 set ve=onemore
776 call feedkeys("A\<c-o>:let g:a=getpos('.')\<cr>\<esc>", 'tnix')
777 call assert_equal([0, 1, 24, 0], g:a)
778 set ve=
779 unlet! g:a
780 bw!
781endfunc
782
Bram Moolenaar1e115362019-01-09 23:01:02 +0100783func Test_edit_CTRL_R()
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100784 " Insert Register
785 new
786 call test_override("ALL", 1)
787 set showcmd
788 call feedkeys("AFOOBAR eins zwei\<esc>", 'tnix')
789 call feedkeys("O\<c-r>.", 'tnix')
790 call feedkeys("O\<c-r>=10*500\<cr>\<esc>", 'tnix')
791 call feedkeys("O\<c-r>=getreg('=', 1)\<cr>\<esc>", 'tnix')
792 call assert_equal(["getreg('=', 1)", '5000', "FOOBAR eins zwei", "FOOBAR eins zwei"], getline(1, '$'))
793 call test_override("ALL", 0)
794 set noshowcmd
795 bw!
796endfunc
797
Bram Moolenaar1e115362019-01-09 23:01:02 +0100798func Test_edit_CTRL_S()
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100799 " Test pressing CTRL-S (basically only spellfile completion)
800 " the rest is already covered
801 new
802 if !has("spell")
803 call setline(1, 'vim')
804 call feedkeys("A\<c-x>ss\<cr>\<esc>", 'tnix')
805 call assert_equal(['vims', ''], getline(1, '$'))
806 bw!
807 return
808 endif
809 call setline(1, 'vim')
810 " spell option not yet set
811 try
812 call feedkeys("A\<c-x>\<c-s>\<cr>\<esc>", 'tnix')
813 catch /^Vim\%((\a\+)\)\=:E756/
814 call assert_true(1, 'error caught')
815 endtry
816 call assert_equal(['vim', ''], getline(1, '$'))
817 %d
818 setl spell spelllang=en
819 call setline(1, 'vim')
820 call cursor(1, 1)
821 call feedkeys("A\<c-x>\<c-s>\<cr>\<esc>", 'tnix')
822 call assert_equal(['Vim', ''], getline(1, '$'))
823 %d
824 call setline(1, 'vim')
825 call cursor(1, 1)
826 call feedkeys("A\<c-x>\<c-s>\<down>\<cr>\<esc>", 'tnix')
827 call assert_equal(['Aim'], getline(1, '$'))
828 %d
829 call setline(1, 'vim')
830 call cursor(1, 1)
831 call feedkeys("A\<c-x>\<c-s>\<c-p>\<cr>\<esc>", 'tnix')
832 call assert_equal(['vim', ''], getline(1, '$'))
833 %d
834 " empty buffer
835 call cursor(1, 1)
836 call feedkeys("A\<c-x>\<c-s>\<c-p>\<cr>\<esc>", 'tnix')
837 call assert_equal(['', ''], getline(1, '$'))
838 setl nospell
839 bw!
840endfunc
841
Bram Moolenaar1e115362019-01-09 23:01:02 +0100842func Test_edit_CTRL_T()
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100843 " Check for CTRL-T and CTRL-X CTRL-T in insert mode
844 " 1) increase indent
845 new
846 call setline(1, "abc")
847 call cursor(1, 1)
848 call feedkeys("A\<c-t>xyz", 'tnix')
849 call assert_equal(["\<tab>abcxyz"], getline(1, '$'))
850 " 2) also when paste option is set
851 set paste
852 call setline(1, "abc")
853 call cursor(1, 1)
854 call feedkeys("A\<c-t>xyz", 'tnix')
855 call assert_equal(["\<tab>abcxyz"], getline(1, '$'))
856 set nopaste
857 " CTRL-X CTRL-T (thesaurus complete)
Bram Moolenaar14f91762022-09-21 15:13:52 +0100858 call writefile(['angry furious mad enraged'], 'Xthesaurus', 'D')
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100859 set thesaurus=Xthesaurus
860 call setline(1, 'mad')
861 call cursor(1, 1)
862 call feedkeys("A\<c-x>\<c-t>\<cr>\<esc>", 'tnix')
863 call assert_equal(['mad', ''], getline(1, '$'))
864 %d
865 call setline(1, 'mad')
866 call cursor(1, 1)
867 call feedkeys("A\<c-x>\<c-t>\<c-n>\<cr>\<esc>", 'tnix')
868 call assert_equal(['angry', ''], getline(1, '$'))
869 %d
870 call setline(1, 'mad')
871 call cursor(1, 1)
872 call feedkeys("A\<c-x>\<c-t>\<c-n>\<c-n>\<cr>\<esc>", 'tnix')
873 call assert_equal(['furious', ''], getline(1, '$'))
874 %d
875 call setline(1, 'mad')
876 call cursor(1, 1)
877 call feedkeys("A\<c-x>\<c-t>\<c-n>\<c-n>\<c-n>\<cr>\<esc>", 'tnix')
878 call assert_equal(['enraged', ''], getline(1, '$'))
879 %d
880 call setline(1, 'mad')
881 call cursor(1, 1)
882 call feedkeys("A\<c-x>\<c-t>\<c-n>\<c-n>\<c-n>\<c-n>\<cr>\<esc>", 'tnix')
883 call assert_equal(['mad', ''], getline(1, '$'))
884 %d
885 call setline(1, 'mad')
886 call cursor(1, 1)
887 call feedkeys("A\<c-x>\<c-t>\<c-n>\<c-n>\<c-n>\<c-n>\<c-n>\<cr>\<esc>", 'tnix')
888 call assert_equal(['mad', ''], getline(1, '$'))
889 " Using <c-p> <c-n> when 'complete' is empty
890 set complete=
891 %d
892 call setline(1, 'mad')
893 call cursor(1, 1)
894 call feedkeys("A\<c-x>\<c-t>\<c-n>\<cr>\<esc>", 'tnix')
895 call assert_equal(['angry', ''], getline(1, '$'))
896 %d
897 call setline(1, 'mad')
898 call cursor(1, 1)
899 call feedkeys("A\<c-x>\<c-t>\<c-p>\<cr>\<esc>", 'tnix')
900 call assert_equal(['mad', ''], getline(1, '$'))
901 set complete&
902
903 set thesaurus=
904 %d
905 call setline(1, 'mad')
906 call cursor(1, 1)
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100907 let v:testing = 1
908 try
909 call feedkeys("A\<c-x>\<c-t>\<esc>", 'tnix')
910 catch
911 " error sleeps 2 seconds, when v:testing is not set
912 let v:testing = 0
913 endtry
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100914 call assert_equal(['mad'], getline(1, '$'))
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100915 bw!
916endfunc
917
Yegappan Lakshmanane9825862022-01-03 11:03:48 +0000918" Test thesaurus completion with different encodings
919func Test_thesaurus_complete_with_encoding()
Bram Moolenaar14f91762022-09-21 15:13:52 +0100920 call writefile(['angry furious mad enraged'], 'Xthesaurus', 'D')
Yegappan Lakshmanane9825862022-01-03 11:03:48 +0000921 set thesaurus=Xthesaurus
922 for e in ['latin1', 'utf-8']
923 exe 'set encoding=' .. e
924 new
925 call setline(1, 'mad')
926 call cursor(1, 1)
927 call feedkeys("A\<c-x>\<c-t>\<cr>\<esc>", 'tnix')
928 call assert_equal(['mad', ''], getline(1, '$'))
929 bw!
930 endfor
931 set thesaurus=
Yegappan Lakshmanane9825862022-01-03 11:03:48 +0000932endfunc
933
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +0100934" Test 'thesaurusfunc'
935func MyThesaurus(findstart, base)
936 let mythesaurus = [
937 \ #{word: "happy",
938 \ synonyms: "cheerful,blissful,flying high,looking good,peppy"},
939 \ #{word: "kind",
940 \ synonyms: "amiable,bleeding-heart,heart in right place"}]
941 if a:findstart
942 " locate the start of the word
943 let line = getline('.')
944 let start = col('.') - 1
945 while start > 0 && line[start - 1] =~ '\a'
946 let start -= 1
947 endwhile
948 return start
949 else
950 " find strings matching with "a:base"
951 let res = []
952 for w in mythesaurus
953 if w.word =~ '^' . a:base
954 call add(res, w.word)
955 call extend(res, split(w.synonyms, ","))
956 endif
957 endfor
958 return res
959 endif
960endfunc
961
962func Test_thesaurus_func()
963 new
Bram Moolenaarf4d8b762021-10-17 14:13:09 +0100964 set thesaurus=notused
965 set thesaurusfunc=NotUsed
966 setlocal thesaurusfunc=MyThesaurus
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +0100967 call setline(1, "an ki")
968 call cursor(1, 1)
969 call feedkeys("A\<c-x>\<c-t>\<c-n>\<cr>\<esc>", 'tnix')
970 call assert_equal(['an amiable', ''], getline(1, '$'))
Bram Moolenaarf4d8b762021-10-17 14:13:09 +0100971
972 setlocal thesaurusfunc=NonExistingFunc
973 call assert_fails("normal $a\<C-X>\<C-T>", 'E117:')
974
975 setlocal thesaurusfunc=
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +0100976 set thesaurusfunc=NonExistingFunc
977 call assert_fails("normal $a\<C-X>\<C-T>", 'E117:')
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +0100978 %bw!
Bram Moolenaarf4d8b762021-10-17 14:13:09 +0100979
980 set thesaurusfunc=
981 set thesaurus=
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +0100982endfunc
983
Bram Moolenaar1e115362019-01-09 23:01:02 +0100984func Test_edit_CTRL_U()
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100985 " Test 'completefunc'
986 new
987 " -1, -2 and -3 are special return values
988 let g:special=0
989 fun! CompleteMonths(findstart, base)
990 if a:findstart
991 " locate the start of the word
992 return g:special
993 else
994 " find months matching with "a:base"
995 let res = []
996 for m in split("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec")
997 if m =~ '^\c'.a:base
998 call add(res, {'word': m, 'abbr': m.' Month', 'icase': 0})
999 endif
1000 endfor
1001 return {'words': res, 'refresh': 'always'}
1002 endif
1003 endfun
1004 set completefunc=CompleteMonths
1005 call setline(1, ['', ''])
1006 call cursor(1, 1)
1007 call feedkeys("AX\<c-x>\<c-u>\<cr>\<esc>", 'tnix')
1008 call assert_equal(['X', '', ''], getline(1, '$'))
1009 %d
1010 let g:special=-1
1011 call feedkeys("AX\<c-x>\<c-u>\<cr>\<esc>", 'tnix')
1012 call assert_equal(['XJan', ''], getline(1, '$'))
1013 %d
1014 let g:special=-2
1015 call feedkeys("AX\<c-x>\<c-u>\<cr>\<esc>", 'tnix')
1016 call assert_equal(['X', ''], getline(1, '$'))
1017 %d
1018 let g:special=-3
1019 call feedkeys("AX\<c-x>\<c-u>\<cr>\<esc>", 'tnix')
1020 call assert_equal(['X', ''], getline(1, '$'))
1021 %d
1022 let g:special=0
1023 call feedkeys("AM\<c-x>\<c-u>\<cr>\<esc>", 'tnix')
1024 call assert_equal(['Mar', ''], getline(1, '$'))
1025 %d
1026 call feedkeys("AM\<c-x>\<c-u>\<c-n>\<cr>\<esc>", 'tnix')
1027 call assert_equal(['May', ''], getline(1, '$'))
1028 %d
1029 call feedkeys("AM\<c-x>\<c-u>\<c-n>\<c-n>\<cr>\<esc>", 'tnix')
1030 call assert_equal(['M', ''], getline(1, '$'))
1031 delfu CompleteMonths
1032 %d
1033 try
1034 call feedkeys("A\<c-x>\<c-u>", 'tnix')
1035 call assert_fails(1, 'unknown completion function')
1036 catch /^Vim\%((\a\+)\)\=:E117/
1037 call assert_true(1, 'E117 error caught')
1038 endtry
1039 set completefunc=
1040 bw!
1041endfunc
1042
Bram Moolenaarff06f282020-04-21 22:01:14 +02001043func Test_edit_completefunc_delete()
1044 func CompleteFunc(findstart, base)
1045 if a:findstart == 1
1046 return col('.') - 1
1047 endif
1048 normal dd
1049 return ['a', 'b']
1050 endfunc
1051 new
1052 set completefunc=CompleteFunc
1053 call setline(1, ['', 'abcd', ''])
1054 2d
zeertzjqcfe45652022-05-27 17:26:55 +01001055 call assert_fails("normal 2G$a\<C-X>\<C-U>", 'E565:')
Bram Moolenaarff06f282020-04-21 22:01:14 +02001056 bwipe!
1057endfunc
1058
1059
Bram Moolenaar1e115362019-01-09 23:01:02 +01001060func Test_edit_CTRL_Z()
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001061 " Ctrl-Z when insertmode is not set inserts it literally
1062 new
1063 call setline(1, 'abc')
1064 call feedkeys("A\<c-z>\<esc>", 'tnix')
1065 call assert_equal(["abc\<c-z>"], getline(1,'$'))
1066 bw!
1067 " TODO: How to Test Ctrl-Z in insert mode, e.g. suspend?
1068endfunc
1069
Bram Moolenaar1e115362019-01-09 23:01:02 +01001070func Test_edit_DROP()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001071 CheckFeature dnd
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001072 new
1073 call setline(1, ['abc def ghi'])
1074 call cursor(1, 1)
1075 try
1076 call feedkeys("i\<Drop>\<Esc>", 'tnix')
1077 call assert_fails(1, 'Invalid register name')
1078 catch /^Vim\%((\a\+)\)\=:E353/
1079 call assert_true(1, 'error caught')
1080 endtry
1081 bw!
1082endfunc
1083
Bram Moolenaar1e115362019-01-09 23:01:02 +01001084func Test_edit_CTRL_V()
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001085 new
1086 call setline(1, ['abc'])
1087 call cursor(2, 1)
zeertzjq502d8ae2022-01-24 15:27:50 +00001088
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001089 " force some redraws
1090 set showmode showcmd
zeertzjq502d8ae2022-01-24 15:27:50 +00001091 call test_override('char_avail', 1)
1092
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001093 call feedkeys("A\<c-v>\<c-n>\<c-v>\<c-l>\<c-v>\<c-b>\<esc>", 'tnix')
1094 call assert_equal(["abc\x0e\x0c\x02"], getline(1, '$'))
1095
1096 if has("rightleft") && exists("+rl")
1097 set rl
1098 call setline(1, ['abc'])
1099 call cursor(2, 1)
1100 call feedkeys("A\<c-v>\<c-n>\<c-v>\<c-l>\<c-v>\<c-b>\<esc>", 'tnix')
1101 call assert_equal(["abc\x0e\x0c\x02"], getline(1, '$'))
1102 set norl
1103 endif
1104
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001105 set noshowmode showcmd
zeertzjq502d8ae2022-01-24 15:27:50 +00001106 call test_override('char_avail', 0)
1107
1108 " No modifiers should be applied to the char typed using i_CTRL-V_digit.
1109 call feedkeys(":append\<CR>\<C-V>76c\<C-V>76\<C-F2>\<C-V>u3c0j\<C-V>u3c0\<M-F3>\<CR>.\<CR>", 'tnix')
1110 call assert_equal('LcL<C-F2>πjπ<M-F3>', getline(2))
1111
1112 if has('osx')
1113 " A char with a modifier should not be a valid char for i_CTRL-V_digit.
1114 call feedkeys("o\<C-V>\<D-j>\<C-V>\<D-1>\<C-V>\<D-o>\<C-V>\<D-x>\<C-V>\<D-u>", 'tnix')
1115 call assert_equal('<D-j><D-1><D-o><D-x><D-u>', getline(3))
1116 endif
1117
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001118 bw!
1119endfunc
1120
Bram Moolenaar1e115362019-01-09 23:01:02 +01001121func Test_edit_F1()
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01001122 CheckFeature quickfix
1123
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001124 " Pressing <f1>
1125 new
1126 call feedkeys(":set im\<cr>\<f1>\<c-l>", 'tnix')
1127 set noinsertmode
1128 call assert_equal('help', &buftype)
1129 bw
1130 bw
1131endfunc
1132
Bram Moolenaar1e115362019-01-09 23:01:02 +01001133func Test_edit_F21()
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001134 " Pressing <f21>
1135 " sends a netbeans command
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001136 CheckFeature netbeans_intg
1137 new
1138 " I have no idea what this is supposed to do :)
1139 call feedkeys("A\<F21>\<F1>\<esc>", 'tnix')
1140 bw
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001141endfunc
1142
Bram Moolenaar1e115362019-01-09 23:01:02 +01001143func Test_edit_HOME_END()
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001144 " Test Home/End Keys
1145 new
1146 set foldopen+=hor
1147 call setline(1, ['abc', 'def'])
1148 call cursor(1, 1)
1149 call feedkeys("AX\<Home>Y\<esc>", 'tnix')
1150 call cursor(2, 1)
1151 call feedkeys("iZ\<End>Y\<esc>", 'tnix')
1152 call assert_equal(['YabcX', 'ZdefY'], getline(1, '$'))
1153
1154 set foldopen-=hor
1155 bw!
1156endfunc
1157
Bram Moolenaar1e115362019-01-09 23:01:02 +01001158func Test_edit_INS()
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001159 " Test for Pressing <Insert>
1160 new
1161 call setline(1, ['abc', 'def'])
1162 call cursor(1, 1)
1163 call feedkeys("i\<Insert>ZYX>", 'tnix')
1164 call assert_equal(['ZYX>', 'def'], getline(1, '$'))
1165 call setline(1, ['abc', 'def'])
1166 call cursor(1, 1)
1167 call feedkeys("i\<Insert>Z\<Insert>YX>", 'tnix')
1168 call assert_equal(['ZYX>bc', 'def'], getline(1, '$'))
1169 bw!
1170endfunc
1171
Bram Moolenaar1e115362019-01-09 23:01:02 +01001172func Test_edit_LEFT_RIGHT()
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001173 " Left, Shift-Left, Right, Shift-Right
1174 new
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001175 call setline(1, ['abc def ghi', 'ABC DEF GHI', 'ZZZ YYY XXX'])
1176 let _ww=&ww
1177 set ww=
1178 call cursor(2, 1)
1179 call feedkeys("i\<left>\<esc>", 'tnix')
1180 call assert_equal([0, 2, 1, 0], getpos('.'))
1181 " Is this a bug, <s-left> does not respect whichwrap option
1182 call feedkeys("i\<s-left>\<esc>", 'tnix')
1183 call assert_equal([0, 1, 8, 0], getpos('.'))
1184 call feedkeys("i". repeat("\<s-left>", 3). "\<esc>", 'tnix')
1185 call assert_equal([0, 1, 1, 0], getpos('.'))
1186 call feedkeys("i\<right>\<esc>", 'tnix')
1187 call assert_equal([0, 1, 1, 0], getpos('.'))
1188 call feedkeys("i\<right>\<right>\<esc>", 'tnix')
1189 call assert_equal([0, 1, 2, 0], getpos('.'))
1190 call feedkeys("A\<right>\<esc>", 'tnix')
1191 call assert_equal([0, 1, 11, 0], getpos('.'))
1192 call feedkeys("A\<s-right>\<esc>", 'tnix')
1193 call assert_equal([0, 2, 1, 0], getpos('.'))
1194 call feedkeys("i\<s-right>\<esc>", 'tnix')
1195 call assert_equal([0, 2, 4, 0], getpos('.'))
1196 call cursor(3, 11)
1197 call feedkeys("A\<right>\<esc>", 'tnix')
1198 call feedkeys("A\<s-right>\<esc>", 'tnix')
1199 call assert_equal([0, 3, 11, 0], getpos('.'))
1200 call cursor(2, 11)
1201 " <S-Right> does not respect 'whichwrap' option
1202 call feedkeys("A\<s-right>\<esc>", 'tnix')
1203 call assert_equal([0, 3, 1, 0], getpos('.'))
1204 " Check motion when 'whichwrap' contains cursor keys for insert mode
1205 set ww+=[,]
1206 call cursor(2, 1)
1207 call feedkeys("i\<left>\<esc>", 'tnix')
1208 call assert_equal([0, 1, 11, 0], getpos('.'))
1209 call cursor(2, 11)
1210 call feedkeys("A\<right>\<esc>", 'tnix')
1211 call assert_equal([0, 3, 1, 0], getpos('.'))
1212 call cursor(2, 11)
1213 call feedkeys("A\<s-right>\<esc>", 'tnix')
1214 call assert_equal([0, 3, 1, 0], getpos('.'))
1215 let &ww = _ww
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001216 bw!
1217endfunc
1218
Bram Moolenaar1e115362019-01-09 23:01:02 +01001219func Test_edit_MOUSE()
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001220 " This is a simple test, since we not really using the mouse here
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001221 CheckFeature mouse
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001222 10new
1223 call setline(1, range(1, 100))
1224 call cursor(1, 1)
Bram Moolenaar8e8dc9b2022-05-08 20:38:06 +01001225 call assert_equal(1, line('w0'))
1226 call assert_equal(10, line('w$'))
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001227 set mouse=a
Bram Moolenaar8e8dc9b2022-05-08 20:38:06 +01001228 " One scroll event moves three lines.
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001229 call feedkeys("A\<ScrollWheelDown>\<esc>", 'tnix')
Bram Moolenaar8e8dc9b2022-05-08 20:38:06 +01001230 call assert_equal(4, line('w0'))
1231 call assert_equal(13, line('w$'))
1232 " This should move by one page down.
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001233 call feedkeys("A\<S-ScrollWheelDown>\<esc>", 'tnix')
Bram Moolenaar8e8dc9b2022-05-08 20:38:06 +01001234 call assert_equal(14, line('w0'))
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001235 set nostartofline
Bram Moolenaar8e8dc9b2022-05-08 20:38:06 +01001236 " Another page down.
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001237 call feedkeys("A\<C-ScrollWheelDown>\<esc>", 'tnix')
Bram Moolenaar8e8dc9b2022-05-08 20:38:06 +01001238 call assert_equal(24, line('w0'))
1239
1240 call assert_equal([0, 24, 2, 0], getpos('.'))
1241 call test_setmouse(4, 3)
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001242 call feedkeys("A\<LeftMouse>\<esc>", 'tnix')
Bram Moolenaar8e8dc9b2022-05-08 20:38:06 +01001243 call assert_equal([0, 27, 2, 0], getpos('.'))
Bram Moolenaarb3707712022-05-08 22:49:43 +01001244 set mousemodel=extend
Bram Moolenaar8e8dc9b2022-05-08 20:38:06 +01001245 call test_setmouse(5, 3)
1246 call feedkeys("A\<RightMouse>\<esc>\<esc>", 'tnix')
1247 call assert_equal([0, 28, 2, 0], getpos('.'))
Bram Moolenaarb3707712022-05-08 22:49:43 +01001248 set mousemodel&
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001249 call cursor(1, 100)
1250 norm! zt
1251 " this should move by a screen up, but when the test
1252 " is run, it moves up to the top of the buffer...
1253 call feedkeys("A\<ScrollWheelUp>\<esc>", 'tnix')
1254 call assert_equal([0, 1, 1, 0], getpos('.'))
1255 call cursor(1, 30)
1256 norm! zt
1257 call feedkeys("A\<S-ScrollWheelUp>\<esc>", 'tnix')
1258 call assert_equal([0, 1, 1, 0], getpos('.'))
1259 call cursor(1, 30)
1260 norm! zt
1261 call feedkeys("A\<C-ScrollWheelUp>\<esc>", 'tnix')
1262 call assert_equal([0, 1, 1, 0], getpos('.'))
1263 %d
1264 call setline(1, repeat(["12345678901234567890"], 100))
1265 call cursor(2, 1)
1266 call feedkeys("A\<ScrollWheelRight>\<esc>", 'tnix')
1267 call assert_equal([0, 2, 20, 0], getpos('.'))
1268 call feedkeys("A\<ScrollWheelLeft>\<esc>", 'tnix')
1269 call assert_equal([0, 2, 20, 0], getpos('.'))
1270 call feedkeys("A\<S-ScrollWheelRight>\<esc>", 'tnix')
1271 call assert_equal([0, 2, 20, 0], getpos('.'))
1272 call feedkeys("A\<S-ScrollWheelLeft>\<esc>", 'tnix')
1273 call assert_equal([0, 2, 20, 0], getpos('.'))
1274 call feedkeys("A\<C-ScrollWheelRight>\<esc>", 'tnix')
1275 call assert_equal([0, 2, 20, 0], getpos('.'))
1276 call feedkeys("A\<C-ScrollWheelLeft>\<esc>", 'tnix')
1277 call assert_equal([0, 2, 20, 0], getpos('.'))
1278 set mouse& startofline
1279 bw!
1280endfunc
1281
Bram Moolenaar1e115362019-01-09 23:01:02 +01001282func Test_edit_PAGEUP_PAGEDOWN()
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001283 10new
1284 call setline(1, repeat(['abc def ghi'], 30))
1285 call cursor(1, 1)
1286 call feedkeys("i\<PageDown>\<esc>", 'tnix')
1287 call assert_equal([0, 9, 1, 0], getpos('.'))
1288 call feedkeys("i\<PageDown>\<esc>", 'tnix')
1289 call assert_equal([0, 17, 1, 0], getpos('.'))
1290 call feedkeys("i\<PageDown>\<esc>", 'tnix')
1291 call assert_equal([0, 25, 1, 0], getpos('.'))
1292 call feedkeys("i\<PageDown>\<esc>", 'tnix')
1293 call assert_equal([0, 30, 1, 0], getpos('.'))
1294 call feedkeys("i\<PageDown>\<esc>", 'tnix')
1295 call assert_equal([0, 30, 1, 0], getpos('.'))
1296 call feedkeys("A\<PageUp>\<esc>", 'tnix')
1297 call assert_equal([0, 29, 1, 0], getpos('.'))
1298 call feedkeys("A\<PageUp>\<esc>", 'tnix')
1299 call assert_equal([0, 21, 1, 0], getpos('.'))
1300 call feedkeys("A\<PageUp>\<esc>", 'tnix')
1301 call assert_equal([0, 13, 1, 0], getpos('.'))
1302 call feedkeys("A\<PageUp>\<esc>", 'tnix')
1303 call assert_equal([0, 5, 1, 0], getpos('.'))
1304 call feedkeys("A\<PageUp>\<esc>", 'tnix')
1305 call assert_equal([0, 5, 11, 0], getpos('.'))
1306 " <S-Up> is the same as <PageUp>
1307 " <S-Down> is the same as <PageDown>
1308 call cursor(1, 1)
1309 call feedkeys("i\<S-Down>\<esc>", 'tnix')
1310 call assert_equal([0, 9, 1, 0], getpos('.'))
1311 call feedkeys("i\<S-Down>\<esc>", 'tnix')
1312 call assert_equal([0, 17, 1, 0], getpos('.'))
1313 call feedkeys("i\<S-Down>\<esc>", 'tnix')
1314 call assert_equal([0, 25, 1, 0], getpos('.'))
1315 call feedkeys("i\<S-Down>\<esc>", 'tnix')
1316 call assert_equal([0, 30, 1, 0], getpos('.'))
1317 call feedkeys("i\<S-Down>\<esc>", 'tnix')
1318 call assert_equal([0, 30, 1, 0], getpos('.'))
1319 call feedkeys("A\<S-Up>\<esc>", 'tnix')
1320 call assert_equal([0, 29, 1, 0], getpos('.'))
1321 call feedkeys("A\<S-Up>\<esc>", 'tnix')
1322 call assert_equal([0, 21, 1, 0], getpos('.'))
1323 call feedkeys("A\<S-Up>\<esc>", 'tnix')
1324 call assert_equal([0, 13, 1, 0], getpos('.'))
1325 call feedkeys("A\<S-Up>\<esc>", 'tnix')
1326 call assert_equal([0, 5, 1, 0], getpos('.'))
1327 call feedkeys("A\<S-Up>\<esc>", 'tnix')
1328 call assert_equal([0, 5, 11, 0], getpos('.'))
1329 set nostartofline
1330 call cursor(30, 11)
1331 norm! zt
1332 call feedkeys("A\<PageUp>\<esc>", 'tnix')
1333 call assert_equal([0, 29, 11, 0], getpos('.'))
1334 call feedkeys("A\<PageUp>\<esc>", 'tnix')
1335 call assert_equal([0, 21, 11, 0], getpos('.'))
1336 call feedkeys("A\<PageUp>\<esc>", 'tnix')
1337 call assert_equal([0, 13, 11, 0], getpos('.'))
1338 call feedkeys("A\<PageUp>\<esc>", 'tnix')
1339 call assert_equal([0, 5, 11, 0], getpos('.'))
1340 call feedkeys("A\<PageUp>\<esc>", 'tnix')
1341 call assert_equal([0, 5, 11, 0], getpos('.'))
1342 call cursor(1, 1)
1343 call feedkeys("A\<PageDown>\<esc>", 'tnix')
1344 call assert_equal([0, 9, 11, 0], getpos('.'))
1345 call feedkeys("A\<PageDown>\<esc>", 'tnix')
1346 call assert_equal([0, 17, 11, 0], getpos('.'))
1347 call feedkeys("A\<PageDown>\<esc>", 'tnix')
1348 call assert_equal([0, 25, 11, 0], getpos('.'))
1349 call feedkeys("A\<PageDown>\<esc>", 'tnix')
1350 call assert_equal([0, 30, 11, 0], getpos('.'))
1351 call feedkeys("A\<PageDown>\<esc>", 'tnix')
1352 call assert_equal([0, 30, 11, 0], getpos('.'))
1353 " <S-Up> is the same as <PageUp>
1354 " <S-Down> is the same as <PageDown>
1355 call cursor(30, 11)
1356 norm! zt
1357 call feedkeys("A\<S-Up>\<esc>", 'tnix')
1358 call assert_equal([0, 29, 11, 0], getpos('.'))
1359 call feedkeys("A\<S-Up>\<esc>", 'tnix')
1360 call assert_equal([0, 21, 11, 0], getpos('.'))
1361 call feedkeys("A\<S-Up>\<esc>", 'tnix')
1362 call assert_equal([0, 13, 11, 0], getpos('.'))
1363 call feedkeys("A\<S-Up>\<esc>", 'tnix')
1364 call assert_equal([0, 5, 11, 0], getpos('.'))
1365 call feedkeys("A\<S-Up>\<esc>", 'tnix')
1366 call assert_equal([0, 5, 11, 0], getpos('.'))
1367 call cursor(1, 1)
1368 call feedkeys("A\<S-Down>\<esc>", 'tnix')
1369 call assert_equal([0, 9, 11, 0], getpos('.'))
1370 call feedkeys("A\<S-Down>\<esc>", 'tnix')
1371 call assert_equal([0, 17, 11, 0], getpos('.'))
1372 call feedkeys("A\<S-Down>\<esc>", 'tnix')
1373 call assert_equal([0, 25, 11, 0], getpos('.'))
1374 call feedkeys("A\<S-Down>\<esc>", 'tnix')
1375 call assert_equal([0, 30, 11, 0], getpos('.'))
1376 call feedkeys("A\<S-Down>\<esc>", 'tnix')
1377 call assert_equal([0, 30, 11, 0], getpos('.'))
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001378 bw!
1379endfunc
1380
Bram Moolenaar1e115362019-01-09 23:01:02 +01001381func Test_edit_forbidden()
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001382 new
1383 " 1) edit in the sandbox is not allowed
1384 call setline(1, 'a')
1385 com! Sandbox :sandbox call feedkeys("i\<del>\<esc>", 'tnix')
1386 call assert_fails(':Sandbox', 'E48:')
1387 com! Sandbox :sandbox exe "norm! i\<del>"
1388 call assert_fails(':Sandbox', 'E48:')
1389 delcom Sandbox
1390 call assert_equal(['a'], getline(1,'$'))
Bram Moolenaar52797ba2021-12-16 14:45:13 +00001391
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001392 " 2) edit with textlock set
1393 fu! DoIt()
1394 call feedkeys("i\<del>\<esc>", 'tnix')
1395 endfu
1396 au InsertCharPre <buffer> :call DoIt()
1397 try
1398 call feedkeys("ix\<esc>", 'tnix')
1399 call assert_fails(1, 'textlock')
Bram Moolenaarff06f282020-04-21 22:01:14 +02001400 catch /^Vim\%((\a\+)\)\=:E565/ " catch E565: not allowed here
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001401 endtry
1402 " TODO: Might be a bug: should x really be inserted here
1403 call assert_equal(['xa'], getline(1, '$'))
1404 delfu DoIt
1405 try
1406 call feedkeys("ix\<esc>", 'tnix')
1407 call assert_fails(1, 'unknown function')
1408 catch /^Vim\%((\a\+)\)\=:E117/ " catch E117: unknown function
1409 endtry
1410 au! InsertCharPre
Bram Moolenaar52797ba2021-12-16 14:45:13 +00001411
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001412 " 3) edit when completion is shown
1413 fun! Complete(findstart, base)
1414 if a:findstart
1415 return col('.')
1416 else
1417 call feedkeys("i\<del>\<esc>", 'tnix')
1418 return []
1419 endif
1420 endfun
1421 set completefunc=Complete
1422 try
1423 call feedkeys("i\<c-x>\<c-u>\<esc>", 'tnix')
1424 call assert_fails(1, 'change in complete function')
Bram Moolenaarff06f282020-04-21 22:01:14 +02001425 catch /^Vim\%((\a\+)\)\=:E565/ " catch E565
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001426 endtry
1427 delfu Complete
1428 set completefunc=
Bram Moolenaar52797ba2021-12-16 14:45:13 +00001429
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001430 if has("rightleft") && exists("+fkmap")
1431 " 4) 'R' when 'fkmap' and 'revins' is set.
1432 set revins fkmap
1433 try
1434 normal Ri
1435 call assert_fails(1, "R with 'fkmap' and 'ri' set")
1436 catch
1437 finally
1438 set norevins nofkmap
1439 endtry
1440 endif
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001441 bw!
1442endfunc
1443
Bram Moolenaar1e115362019-01-09 23:01:02 +01001444func Test_edit_rightleft()
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001445 " Cursor in rightleft mode moves differently
Bram Moolenaar845e0ee2020-06-20 16:05:32 +02001446 CheckFeature rightleft
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001447 call NewWindow(10, 20)
1448 call setline(1, ['abc', 'def', 'ghi'])
1449 call cursor(1, 2)
1450 set rightleft
1451 " Screen looks as expected
1452 let lines = ScreenLines([1, 4], winwidth(0))
1453 let expect = [
1454 \" cba",
1455 \" fed",
1456 \" ihg",
1457 \" ~"]
1458 call assert_equal(join(expect, "\n"), join(lines, "\n"))
1459 " 2) right moves to the left
1460 call feedkeys("i\<right>\<esc>x", 'txin')
1461 call assert_equal(['bc', 'def', 'ghi'], getline(1,'$'))
1462 call cursor(1, 2)
1463 call feedkeys("i\<s-right>\<esc>", 'txin')
1464 call cursor(1, 2)
1465 call feedkeys("i\<c-right>\<esc>", 'txin')
1466 " Screen looks as expected
1467 let lines = ScreenLines([1, 4], winwidth(0))
1468 let expect = [
1469 \" cb",
1470 \" fed",
1471 \" ihg",
1472 \" ~"]
1473 call assert_equal(join(expect, "\n"), join(lines, "\n"))
1474 " 2) left moves to the right
1475 call setline(1, ['abc', 'def', 'ghi'])
1476 call cursor(1, 2)
1477 call feedkeys("i\<left>\<esc>x", 'txin')
1478 call assert_equal(['ac', 'def', 'ghi'], getline(1,'$'))
1479 call cursor(1, 2)
1480 call feedkeys("i\<s-left>\<esc>", 'txin')
1481 call cursor(1, 2)
1482 call feedkeys("i\<c-left>\<esc>", 'txin')
1483 " Screen looks as expected
1484 let lines = ScreenLines([1, 4], winwidth(0))
1485 let expect = [
1486 \" ca",
1487 \" fed",
1488 \" ihg",
1489 \" ~"]
1490 call assert_equal(join(expect, "\n"), join(lines, "\n"))
Bram Moolenaar845e0ee2020-06-20 16:05:32 +02001491 %d _
1492 call test_override('redraw_flag', 1)
1493 call test_override('char_avail', 1)
1494 call feedkeys("a\<C-V>x41", "xt")
1495 redraw!
1496 call assert_equal(repeat(' ', 19) .. 'A', Screenline(1))
1497 call test_override('ALL', 0)
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001498 set norightleft
1499 bw!
1500endfunc
Bram Moolenaar658a3a22017-03-31 22:27:12 +02001501
1502func Test_edit_complete_very_long_name()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001503 " Long directory names only work on Unix.
1504 CheckUnix
Bram Moolenaar15ecbd62017-04-07 14:10:48 +02001505
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01001506 let dirname = getcwd() . "/Xlongdir"
Bram Moolenaar15ecbd62017-04-07 14:10:48 +02001507 let longdirname = dirname . repeat('/' . repeat('d', 255), 4)
1508 try
Bram Moolenaar14f91762022-09-21 15:13:52 +01001509 call mkdir(longdirname, 'pR')
Bram Moolenaar15ecbd62017-04-07 14:10:48 +02001510 catch /E739:/
1511 " Long directory name probably not supported.
1512 call delete(dirname, 'rf')
1513 return
1514 endtry
1515
Bram Moolenaarf8191c52019-05-18 17:22:54 +02001516 " Try to get the Vim window position before setting 'columns', so that we can
1517 " move the window back to where it was.
Bram Moolenaarba6ec182017-04-04 22:41:10 +02001518 let winposx = getwinposx()
1519 let winposy = getwinposy()
Bram Moolenaarf8191c52019-05-18 17:22:54 +02001520
1521 if winposx >= 0 && winposy >= 0 && !has('gui_running')
1522 " We did get the window position, but xterm may report the wrong numbers.
1523 " Move the window to the reported position and compute any offset.
1524 exe 'winpos ' . winposx . ' ' . winposy
1525 sleep 100m
1526 let x = getwinposx()
1527 if x >= 0
1528 let winposx += winposx - x
1529 endif
1530 let y = getwinposy()
1531 if y >= 0
1532 let winposy += winposy - y
1533 endif
1534 endif
1535
Bram Moolenaar658a3a22017-03-31 22:27:12 +02001536 let save_columns = &columns
Bram Moolenaar15ecbd62017-04-07 14:10:48 +02001537 " Need at least about 1100 columns to reproduce the problem.
Bram Moolenaarba6ec182017-04-04 22:41:10 +02001538 set columns=2000
Bram Moolenaar658a3a22017-03-31 22:27:12 +02001539 set noswapfile
Bram Moolenaarba6ec182017-04-04 22:41:10 +02001540
Bram Moolenaar658a3a22017-03-31 22:27:12 +02001541 let longfilename = longdirname . '/' . repeat('a', 255)
Bram Moolenaar658a3a22017-03-31 22:27:12 +02001542 call writefile(['Totum', 'Table'], longfilename)
1543 new
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01001544 exe "next Xnofile " . longfilename
Bram Moolenaar658a3a22017-03-31 22:27:12 +02001545 exe "normal iT\<C-N>"
1546
1547 bwipe!
1548 exe 'bwipe! ' . longfilename
Bram Moolenaar658a3a22017-03-31 22:27:12 +02001549 let &columns = save_columns
Bram Moolenaarba6ec182017-04-04 22:41:10 +02001550 if winposx >= 0 && winposy >= 0
1551 exe 'winpos ' . winposx . ' ' . winposy
1552 endif
Bram Moolenaar658a3a22017-03-31 22:27:12 +02001553 set swapfile&
1554endfunc
Bram Moolenaarff930ca2017-10-19 17:12:10 +02001555
Bram Moolenaar2c8c6812018-07-28 17:07:52 +02001556func Test_edit_backtick()
1557 next a\`b c
1558 call assert_equal('a`b', expand('%'))
1559 next
1560 call assert_equal('c', expand('%'))
1561 call assert_equal('a\`b c', expand('##'))
1562endfunc
1563
Bram Moolenaarff930ca2017-10-19 17:12:10 +02001564func Test_edit_quit()
1565 edit foo.txt
1566 split
1567 new
1568 call setline(1, 'hello')
1569 3wincmd w
1570 redraw!
1571 call assert_fails('1q', 'E37:')
1572 bwipe! foo.txt
1573 only
1574endfunc
1575
Bram Moolenaaradb8fbe2018-06-04 20:34:23 +02001576func Test_edit_alt()
1577 " Keeping the cursor line didn't happen when the first line has indent.
1578 new
1579 call setline(1, [' one', 'two', 'three'])
1580 w XAltFile
1581 $
1582 call assert_equal(3, line('.'))
1583 e Xother
1584 e #
1585 call assert_equal(3, line('.'))
1586
1587 bwipe XAltFile
1588 call delete('XAltFile')
1589endfunc
Bram Moolenaar4dbc2622018-11-02 11:59:15 +01001590
Bram Moolenaardb934952020-04-27 20:18:31 +02001591func Test_edit_InsertLeave()
Bram Moolenaar4dbc2622018-11-02 11:59:15 +01001592 new
Bram Moolenaarb53e13a2020-10-21 12:19:53 +02001593 au InsertLeavePre * let g:did_au_pre = 1
Bram Moolenaar4dbc2622018-11-02 11:59:15 +01001594 au InsertLeave * let g:did_au = 1
Bram Moolenaarb53e13a2020-10-21 12:19:53 +02001595 let g:did_au_pre = 0
Bram Moolenaar4dbc2622018-11-02 11:59:15 +01001596 let g:did_au = 0
1597 call feedkeys("afoo\<Esc>", 'tx')
Bram Moolenaarb53e13a2020-10-21 12:19:53 +02001598 call assert_equal(1, g:did_au_pre)
Bram Moolenaar4dbc2622018-11-02 11:59:15 +01001599 call assert_equal(1, g:did_au)
1600 call assert_equal('foo', getline(1))
1601
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("Sbar\<C-C>", '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(0, g:did_au)
1607 call assert_equal('bar', getline(1))
1608
1609 inoremap x xx<Esc>
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("Saax", '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(1, g:did_au)
1615 call assert_equal('aaxx', getline(1))
1616
1617 inoremap x xx<C-C>
Bram Moolenaarb53e13a2020-10-21 12:19:53 +02001618 let g:did_au_pre = 0
Bram Moolenaar4dbc2622018-11-02 11:59:15 +01001619 let g:did_au = 0
1620 call feedkeys("Sbbx", 'tx')
Bram Moolenaarb53e13a2020-10-21 12:19:53 +02001621 call assert_equal(1, g:did_au_pre)
Bram Moolenaar4dbc2622018-11-02 11:59:15 +01001622 call assert_equal(0, g:did_au)
1623 call assert_equal('bbxx', getline(1))
1624
1625 bwipe!
Bram Moolenaarb53e13a2020-10-21 12:19:53 +02001626 au! InsertLeave InsertLeavePre
Bram Moolenaar4dbc2622018-11-02 11:59:15 +01001627 iunmap x
1628endfunc
Bram Moolenaarc6b37db2019-04-27 18:00:34 +02001629
Bram Moolenaardb934952020-04-27 20:18:31 +02001630func Test_edit_InsertLeave_undo()
1631 new XtestUndo
1632 set undofile
1633 au InsertLeave * wall
1634 exe "normal ofoo\<Esc>"
1635 call assert_equal(2, line('$'))
1636 normal u
1637 call assert_equal(1, line('$'))
1638
1639 bwipe!
1640 au! InsertLeave
1641 call delete('XtestUndo')
Bram Moolenaarb340bae2020-06-15 19:51:56 +02001642 call delete(undofile('XtestUndo'))
Bram Moolenaardb934952020-04-27 20:18:31 +02001643 set undofile&
1644endfunc
1645
Bram Moolenaarc6b37db2019-04-27 18:00:34 +02001646" Test for inserting characters using CTRL-V followed by a number.
1647func Test_edit_special_chars()
1648 new
1649
Bram Moolenaar424bcae2022-01-31 14:59:41 +00001650 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 +02001651
1652 exe "normal " . t
1653 call assert_equal("ABC !a\<C-O>g\<C-G>8", getline(2))
1654
1655 close!
1656endfunc
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02001657
1658func Test_edit_startinsert()
1659 new
1660 set backspace+=start
1661 call setline(1, 'foobar')
1662 call feedkeys("A\<C-U>\<Esc>", 'xt')
1663 call assert_equal('', getline(1))
1664
1665 call setline(1, 'foobar')
1666 call feedkeys(":startinsert!\<CR>\<C-U>\<Esc>", 'xt')
1667 call assert_equal('', getline(1))
1668
1669 set backspace&
1670 bwipe!
1671endfunc
Bram Moolenaar177c9f22019-11-06 13:59:16 +01001672
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +01001673" Test for :startreplace and :startgreplace
1674func Test_edit_startreplace()
1675 new
1676 call setline(1, 'abc')
1677 call feedkeys("l:startreplace\<CR>xyz\e", 'xt')
1678 call assert_equal('axyz', getline(1))
1679 call feedkeys("0:startreplace!\<CR>abc\e", 'xt')
1680 call assert_equal('axyzabc', getline(1))
1681 call setline(1, "a\tb")
1682 call feedkeys("0l:startgreplace\<CR>xyz\e", 'xt')
1683 call assert_equal("axyz\tb", getline(1))
1684 call feedkeys("0i\<C-R>=execute('startreplace')\<CR>12\e", 'xt')
1685 call assert_equal("12axyz\tb", getline(1))
1686 close!
1687endfunc
1688
Bram Moolenaar177c9f22019-11-06 13:59:16 +01001689func Test_edit_noesckeys()
Bram Moolenaar215ba3b2019-11-06 15:07:07 +01001690 CheckNotGui
Bram Moolenaar177c9f22019-11-06 13:59:16 +01001691 new
1692
1693 " <Left> moves cursor when 'esckeys' is set
1694 exe "set t_kl=\<Esc>OD"
1695 set esckeys
1696 call feedkeys("axyz\<Esc>ODX", "xt")
1697 call assert_equal("xyXz", getline(1))
1698
1699 " <Left> exits Insert mode when 'esckeys' is off
1700 set noesckeys
1701 call setline(1, '')
1702 call feedkeys("axyz\<Esc>ODX", "xt")
1703 call assert_equal(["DX", "xyz"], getline(1, 2))
1704
1705 bwipe!
1706 set esckeys
1707endfunc
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +01001708
Bram Moolenaar1671f442020-03-10 07:48:13 +01001709" Test for running an invalid ex command in insert mode using CTRL-O
Bram Moolenaar1671f442020-03-10 07:48:13 +01001710func Test_edit_ctrl_o_invalid_cmd()
1711 new
1712 set showmode showcmd
Bram Moolenaarb340bae2020-06-15 19:51:56 +02001713 " Avoid a sleep of 3 seconds. Zero might have side effects.
1714 call test_override('ui_delay', 50)
Bram Moolenaar1671f442020-03-10 07:48:13 +01001715 let caught_e492 = 0
1716 try
1717 call feedkeys("i\<C-O>:invalid\<CR>abc\<Esc>", "xt")
1718 catch /E492:/
1719 let caught_e492 = 1
1720 endtry
1721 call assert_equal(1, caught_e492)
1722 call assert_equal('abc', getline(1))
1723 set showmode& showcmd&
Bram Moolenaarb340bae2020-06-15 19:51:56 +02001724 call test_override('ui_delay', 0)
Bram Moolenaar1671f442020-03-10 07:48:13 +01001725 close!
1726endfunc
1727
Bram Moolenaarb340bae2020-06-15 19:51:56 +02001728" Test for editing a file with a very long name
1729func Test_edit_illegal_filename()
1730 CheckEnglish
1731 new
1732 redir => msg
1733 exe 'edit ' . repeat('f', 5000)
1734 redir END
1735 call assert_match("Illegal file name$", split(msg, "\n")[0])
1736 close!
1737endfunc
1738
Bram Moolenaarc8fe6452020-10-03 17:04:37 +02001739" Test for editing a directory
1740func Test_edit_is_a_directory()
1741 CheckEnglish
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01001742 let dirname = getcwd() . "/Xeditdir"
Bram Moolenaarc8fe6452020-10-03 17:04:37 +02001743 call mkdir(dirname, 'p')
1744
1745 new
1746 redir => msg
1747 exe 'edit' dirname
1748 redir END
1749 call assert_match("is a directory$", split(msg, "\n")[0])
1750 bwipe!
1751
1752 let dirname .= '/'
1753
1754 new
1755 redir => msg
1756 exe 'edit' dirname
1757 redir END
1758 call assert_match("is a directory$", split(msg, "\n")[0])
1759 bwipe!
1760
1761 call delete(dirname, 'rf')
1762endfunc
1763
Bram Moolenaarb340bae2020-06-15 19:51:56 +02001764" Test for editing a file using invalid file encoding
1765func Test_edit_invalid_encoding()
1766 CheckEnglish
Bram Moolenaar14f91762022-09-21 15:13:52 +01001767 call writefile([], 'Xinvfile', 'D')
Bram Moolenaarb340bae2020-06-15 19:51:56 +02001768 redir => msg
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01001769 new ++enc=axbyc Xinvfile
Bram Moolenaarb340bae2020-06-15 19:51:56 +02001770 redir END
1771 call assert_match('\[NOT converted\]', msg)
Bram Moolenaarb340bae2020-06-15 19:51:56 +02001772 close!
1773endfunc
1774
1775" Test for the "charconvert" option
1776func Test_edit_charconvert()
1777 CheckEnglish
Bram Moolenaar14f91762022-09-21 15:13:52 +01001778 call writefile(['one', 'two'], 'Xccfile', 'D')
Bram Moolenaarb340bae2020-06-15 19:51:56 +02001779
1780 " set 'charconvert' to a non-existing function
1781 set charconvert=NonExitingFunc()
1782 new
1783 let caught_e117 = v:false
1784 try
1785 redir => msg
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01001786 edit ++enc=axbyc Xccfile
Bram Moolenaarb340bae2020-06-15 19:51:56 +02001787 catch /E117:/
1788 let caught_e117 = v:true
1789 finally
1790 redir END
1791 endtry
1792 call assert_true(caught_e117)
1793 call assert_equal(['one', 'two'], getline(1, '$'))
1794 call assert_match("Conversion with 'charconvert' failed", msg)
1795 close!
1796 set charconvert&
1797
1798 " 'charconvert' function doesn't create a output file
1799 func Cconv1()
1800 endfunc
1801 set charconvert=Cconv1()
1802 new
1803 redir => msg
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01001804 edit ++enc=axbyc Xccfile
Bram Moolenaarb340bae2020-06-15 19:51:56 +02001805 redir END
1806 call assert_equal(['one', 'two'], getline(1, '$'))
1807 call assert_match("can't read output of 'charconvert'", msg)
1808 close!
1809 delfunc Cconv1
1810 set charconvert&
1811
1812 " 'charconvert' function to convert to upper case
1813 func Cconv2()
1814 let data = readfile(v:fname_in)
1815 call map(data, 'toupper(v:val)')
1816 call writefile(data, v:fname_out)
1817 endfunc
1818 set charconvert=Cconv2()
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01001819 new Xccfile
1820 write ++enc=ucase Xccfile1
1821 call assert_equal(['ONE', 'TWO'], readfile('Xccfile1'))
1822 call delete('Xccfile1')
Bram Moolenaarb340bae2020-06-15 19:51:56 +02001823 close!
1824 delfunc Cconv2
1825 set charconvert&
1826
1827 " 'charconvert' function removes the input file
1828 func Cconv3()
1829 call delete(v:fname_in)
1830 endfunc
1831 set charconvert=Cconv3()
1832 new
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01001833 call assert_fails('edit ++enc=lcase Xccfile', 'E202:')
Bram Moolenaarb340bae2020-06-15 19:51:56 +02001834 call assert_equal([''], getline(1, '$'))
1835 close!
1836 delfunc Cconv3
1837 set charconvert&
Bram Moolenaarb340bae2020-06-15 19:51:56 +02001838endfunc
1839
1840" Test for editing a file without read permission
1841func Test_edit_file_no_read_perm()
1842 CheckUnix
Bram Moolenaar17709e22021-03-19 14:38:12 +01001843 CheckNotRoot
1844
Bram Moolenaar14f91762022-09-21 15:13:52 +01001845 call writefile(['one', 'two'], 'Xnrpfile', 'D')
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01001846 call setfperm('Xnrpfile', '-w-------')
Bram Moolenaarb340bae2020-06-15 19:51:56 +02001847 new
1848 redir => msg
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01001849 edit Xnrpfile
Bram Moolenaarb340bae2020-06-15 19:51:56 +02001850 redir END
1851 call assert_equal(1, &readonly)
1852 call assert_equal([''], getline(1, '$'))
1853 call assert_match('\[Permission Denied\]', msg)
1854 close!
Bram Moolenaarb340bae2020-06-15 19:51:56 +02001855endfunc
1856
zeertzjq3a56b6d2022-04-08 11:56:14 +01001857" Using :edit without leaving 'insertmode' should not cause Insert mode to be
1858" re-entered immediately after <C-L>
1859func Test_edit_insertmode_ex_edit()
1860 CheckRunVimInTerminal
1861
1862 let lines =<< trim END
1863 set insertmode noruler
1864 inoremap <C-B> <Cmd>edit Xfoo<CR>
1865 END
Bram Moolenaar14f91762022-09-21 15:13:52 +01001866 call writefile(lines, 'Xtest_edit_insertmode_ex_edit', 'D')
zeertzjq3a56b6d2022-04-08 11:56:14 +01001867
Bram Moolenaar14f91762022-09-21 15:13:52 +01001868 let buf = RunVimInTerminal('-S Xtest_edit_insertmode_ex_edit', #{rows: 6, wait_for_ruler: 0})
1869 " Somehow when using valgrind "INSERT" does not show up unless we send
1870 " something to the terminal.
1871 for i in range(30)
1872 if term_getline(buf, 6) =~ 'INSERT'
1873 break
1874 endif
1875 call term_sendkeys(buf, "-")
1876 sleep 100m
1877 endfor
Bram Moolenaarc5382b62022-06-19 15:22:36 +01001878 call WaitForAssert({-> assert_match('^-- INSERT --\s*$', term_getline(buf, 6))})
zeertzjq3a56b6d2022-04-08 11:56:14 +01001879 call term_sendkeys(buf, "\<C-B>\<C-L>")
Bram Moolenaarc5382b62022-06-19 15:22:36 +01001880 call WaitForAssert({-> assert_notmatch('^-- INSERT --\s*$', term_getline(buf, 6))})
zeertzjq3a56b6d2022-04-08 11:56:14 +01001881
1882 " clean up
1883 call StopVimInTerminal(buf)
zeertzjq3a56b6d2022-04-08 11:56:14 +01001884endfunc
1885
Bram Moolenaar845e0ee2020-06-20 16:05:32 +02001886" Pressing escape in 'insertmode' should beep
Bram Moolenaarc5382b62022-06-19 15:22:36 +01001887" FIXME: Execute this later, when using valgrind it makes the next test
1888" Test_edit_insertmode_ex_edit() fail.
1889func Test_z_edit_insertmode_esc_beeps()
Bram Moolenaar845e0ee2020-06-20 16:05:32 +02001890 new
1891 set insertmode
1892 call assert_beeps("call feedkeys(\"one\<Esc>\", 'xt')")
1893 set insertmode&
Bram Moolenaarc5382b62022-06-19 15:22:36 +01001894 " unsupported "CTRL-G l" command should beep in insert mode.
Bram Moolenaar845e0ee2020-06-20 16:05:32 +02001895 call assert_beeps("normal i\<C-G>l")
Bram Moolenaarc5382b62022-06-19 15:22:36 +01001896 bwipe!
Bram Moolenaar845e0ee2020-06-20 16:05:32 +02001897endfunc
1898
1899" Test for 'hkmap' and 'hkmapp'
1900func Test_edit_hkmap()
1901 CheckFeature rightleft
1902 if has('win32') && !has('gui')
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001903 throw 'Skipped: fails on the MS-Windows terminal version'
Bram Moolenaar845e0ee2020-06-20 16:05:32 +02001904 endif
1905 new
1906
1907 set revins hkmap
1908 let str = 'abcdefghijklmnopqrstuvwxyz'
1909 let str ..= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
1910 let str ..= '`/'',.;'
1911 call feedkeys('i' .. str, 'xt')
1912 let expected = "óõú,.;"
1913 let expected ..= "ZYXWVUTSRQPONMLKJIHGFEDCBA"
1914 let expected ..= "æèñ'äåàãø/ôíîöêìçïéòë÷âáðù"
1915 call assert_equal(expected, getline(1))
1916
1917 %d
1918 set revins hkmap hkmapp
1919 let str = 'abcdefghijklmnopqrstuvwxyz'
1920 let str ..= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
1921 call feedkeys('i' .. str, 'xt')
1922 let expected = "õYXWVUTSRQóOïíLKJIHGFEDêBA"
1923 let expected ..= "öòXùåèúæø'ôñðîì÷çéäâóǟãëáà"
1924 call assert_equal(expected, getline(1))
1925
1926 set revins& hkmap& hkmapp&
1927 close!
1928endfunc
1929
1930" Test for 'allowrevins' and using CTRL-_ in insert mode
1931func Test_edit_allowrevins()
1932 CheckFeature rightleft
1933 new
1934 set allowrevins
1935 call feedkeys("iABC\<C-_>DEF\<C-_>GHI", 'xt')
1936 call assert_equal('ABCFEDGHI', getline(1))
1937 set allowrevins&
1938 close!
1939endfunc
1940
1941" Test for inserting a register in insert mode using CTRL-R
1942func Test_edit_insert_reg()
1943 new
1944 let g:Line = ''
1945 func SaveFirstLine()
1946 let g:Line = Screenline(1)
1947 return 'r'
1948 endfunc
1949 inoremap <expr> <buffer> <F2> SaveFirstLine()
1950 call test_override('redraw_flag', 1)
1951 call test_override('char_avail', 1)
1952 let @r = 'sample'
1953 call feedkeys("a\<C-R>=SaveFirstLine()\<CR>", "xt")
1954 call assert_equal('"', g:Line)
1955 call test_override('ALL', 0)
1956 close!
1957endfunc
1958
1959" When a character is inserted at the last position of the last line in a
1960" window, the window contents should be scrolled one line up. If the top line
1961" is part of a fold, then the entire fold should be scrolled up.
1962func Test_edit_lastline_scroll()
1963 new
1964 let h = winheight(0)
1965 let lines = ['one', 'two', 'three']
1966 let lines += repeat(['vim'], h - 4)
1967 call setline(1, lines)
1968 call setline(h, repeat('x', winwidth(0) - 1))
1969 call feedkeys("GAx", 'xt')
1970 redraw!
1971 call assert_equal(h - 1, winline())
1972 call assert_equal(2, line('w0'))
1973
1974 " scroll with a fold
1975 1,2fold
1976 normal gg
1977 call setline(h + 1, repeat('x', winwidth(0) - 1))
1978 call feedkeys("GAx", 'xt')
1979 redraw!
1980 call assert_equal(h - 1, winline())
1981 call assert_equal(3, line('w0'))
1982
1983 close!
1984endfunc
1985
Bram Moolenaar21cbe172020-10-13 19:08:24 +02001986func Test_edit_browse()
1987 " in the GUI this opens a file picker, we only test the terminal behavior
1988 CheckNotGui
1989
1990 " ":browse xxx" checks for the FileExplorer augroup and assumes editing "."
1991 " works then.
1992 augroup FileExplorer
1993 au!
1994 augroup END
1995
1996 " When the USE_FNAME_CASE is defined this used to cause a crash.
1997 browse enew
1998 bwipe!
1999
2000 browse split
2001 bwipe!
2002endfunc
2003
Bram Moolenaarcaf73dc2020-10-26 21:39:13 +01002004func Test_read_invalid()
2005 set encoding=latin1
2006 " This was not properly checking for going past the end.
2007 call assert_fails('r`=', 'E484')
2008 set encoding=utf-8
2009endfunc
2010
Yegappan Lakshmanan59585492021-06-12 13:46:41 +02002011" Test for the 'revins' option
2012func Test_edit_revins()
2013 CheckFeature rightleft
2014 new
2015 set revins
2016 exe "normal! ione\ttwo three"
2017 call assert_equal("eerht owt\teno", getline(1))
2018 call setline(1, "one\ttwo three")
2019 normal! gg$bi a
2020 call assert_equal("one\ttwo a three", getline(1))
2021 exe "normal! $bi\<BS>\<BS>"
2022 call assert_equal("one\ttwo a ree", getline(1))
2023 exe "normal! 0wi\<C-W>"
2024 call assert_equal("one\t a ree", getline(1))
2025 exe "normal! 0wi\<C-U>"
2026 call assert_equal("one\t ", getline(1))
2027 " newline in insert mode starts at the end of the line
2028 call setline(1, 'one two three')
2029 exe "normal! wi\nfour"
2030 call assert_equal(['one two three', 'ruof'], getline(1, '$'))
2031 set revins&
2032 bw!
2033endfunc
2034
Bram Moolenaar35a9a002021-09-11 21:14:20 +02002035" Test for getting the character of the line below after "p"
2036func Test_edit_put_CTRL_E()
2037 set encoding=latin1
2038 new
2039 let @" = ''
2040 sil! norm orggRx
2041 sil! norm pr
2042 call assert_equal(['r', 'r'], getline(1, 2))
2043 bwipe!
2044 set encoding=utf-8
2045endfunc
2046
Dominique Pelle9cd063e2021-10-28 21:06:05 +01002047" Test toggling of input method. See :help i_CTRL-^
2048func Test_edit_CTRL_hat()
2049 CheckFeature xim
Dominique Pelle8753c1d2021-10-31 20:19:17 +00002050
Bram Moolenaar0b962e52022-04-03 18:02:37 +01002051 " FIXME: test fails with Motif GUI.
Dominique Pelle8753c1d2021-10-31 20:19:17 +00002052 " test also fails when running in the GUI.
2053 CheckFeature gui_gtk
2054 CheckNotGui
Dominique Pelle9cd063e2021-10-28 21:06:05 +01002055
2056 new
2057
2058 call assert_equal(0, &iminsert)
2059 call feedkeys("i\<C-^>", 'xt')
2060 call assert_equal(2, &iminsert)
2061 call feedkeys("i\<C-^>", 'xt')
2062 call assert_equal(0, &iminsert)
2063
2064 bwipe!
2065endfunc
2066
Bram Moolenaar2824d1e2023-02-23 20:13:04 +00002067" Test "gr" followed by an Insert mode command does get out of Insert mode.
2068func Test_edit_gr_special()
2069 enew
2070 call setline(1, ['abcdef', 'xxxxxx'])
Bram Moolenaard6a4ea32023-02-25 14:24:44 +00002071 exe "normal! gr\<C-O>lx"
2072 call assert_equal("\<C-O>def", getline(1))
2073
2074 call setline(1, 'abcdef')
2075 exe "normal! 0gr\<C-G>lx"
2076 call assert_equal("\<C-G>def", getline(1))
Bram Moolenaar2824d1e2023-02-23 20:13:04 +00002077
2078 bwipe!
2079endfunc
2080
Bram Moolenaarde05bb22022-01-13 13:08:14 +00002081" Weird long file name was going over the end of NameBuff
2082func Test_edit_overlong_file_name()
2083 CheckUnix
2084
2085 file 0000000000000000000000000000
2086 file %%%%%%%%%%%%%%%%%%%%%%%%%%
2087 file %%%%%%
2088 set readonly
Bram Moolenaar94722c52023-01-28 19:19:03 +00002089 set ls=2
Bram Moolenaarde05bb22022-01-13 13:08:14 +00002090
2091 redraw!
2092 set noreadonly ls&
2093 bwipe!
2094endfunc
2095
Christian Brabandtdfbdadc2022-05-05 20:46:47 +01002096func Test_edit_shift_bs()
2097 CheckMSWindows
2098
2099 " FIXME: this works interactively, but the test fails
2100 throw 'Skipped: Shift-Backspace Test not working correctly :('
2101
2102 " Need to run this in Win32 Terminal, do not use CheckRunVimInTerminal
2103 if !has("terminal")
2104 return
2105 endif
2106
2107 " Shift Backspace should work like Backspace in insert mode
2108 let lines =<< trim END
2109 call setline(1, ['abc'])
2110 END
Bram Moolenaar14f91762022-09-21 15:13:52 +01002111 call writefile(lines, 'Xtest_edit_shift_bs', 'D')
Christian Brabandtdfbdadc2022-05-05 20:46:47 +01002112
2113 let buf = RunVimInTerminal('-S Xtest_edit_shift_bs', #{rows: 3})
2114 call term_sendkeys(buf, "A\<S-BS>-\<esc>")
2115 call TermWait(buf, 50)
2116 call assert_equal('ab-', term_getline(buf, 1))
2117
2118 " clean up
2119 call StopVimInTerminal(buf)
Christian Brabandtdfbdadc2022-05-05 20:46:47 +01002120endfunc
Dominique Pelle9cd063e2021-10-28 21:06:05 +01002121
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +01002122" vim: shiftwidth=2 sts=2 expandtab