blob: 79e3c3fd22c8a41f032041dea2f0de4a969095a5 [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('.'))
zeertzjq4f026ea2023-02-26 14:47:24 +0000576 call assert_nobeep("normal! i\<c-g>\<esc>")
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100577 bw!
578endfunc
579
Bram Moolenaar1e115362019-01-09 23:01:02 +0100580func Test_edit_CTRL_I()
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100581 " Tab in completion mode
582 let path=expand("%:p:h")
583 new
Bram Moolenaarca851592018-06-06 21:04:07 +0200584 call setline(1, [path. "/", ''])
Bram Moolenaarc5379472017-03-16 22:38:00 +0100585 call feedkeys("Arunt\<c-x>\<c-f>\<tab>\<cr>\<esc>", 'tnix')
586 call assert_match('runtest\.vim', getline(1))
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100587 %d
Bram Moolenaar14f91762022-09-21 15:13:52 +0100588 call writefile(['one', 'two', 'three'], 'Xinclude.txt', 'D')
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100589 let include='#include Xinclude.txt'
590 call setline(1, [include, ''])
591 call cursor(2, 1)
592 call feedkeys("A\<c-x>\<tab>\<cr>\<esc>", 'tnix')
593 call assert_equal([include, 'one', ''], getline(1, '$'))
594 call feedkeys("2ggC\<c-x>\<tab>\<down>\<cr>\<esc>", 'tnix')
595 call assert_equal([include, 'two', ''], getline(1, '$'))
596 call feedkeys("2ggC\<c-x>\<tab>\<down>\<down>\<cr>\<esc>", 'tnix')
597 call assert_equal([include, 'three', ''], getline(1, '$'))
598 call feedkeys("2ggC\<c-x>\<tab>\<down>\<down>\<down>\<cr>\<esc>", 'tnix')
599 call assert_equal([include, '', ''], getline(1, '$'))
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100600 bw!
601endfunc
602
Bram Moolenaar1e115362019-01-09 23:01:02 +0100603func Test_edit_CTRL_K()
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100604 " Test pressing CTRL-K (basically only dictionary completion and digraphs
605 " the rest is already covered
Bram Moolenaar14f91762022-09-21 15:13:52 +0100606 call writefile(['A', 'AA', 'AAA', 'AAAA'], 'Xdictionary.txt', 'D')
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100607 set dictionary=Xdictionary.txt
608 new
609 call setline(1, 'A')
610 call cursor(1, 1)
611 call feedkeys("A\<c-x>\<c-k>\<cr>\<esc>", 'tnix')
612 call assert_equal(['AA', ''], getline(1, '$'))
613 %d
614 call setline(1, 'A')
615 call cursor(1, 1)
616 call feedkeys("A\<c-x>\<c-k>\<down>\<cr>\<esc>", 'tnix')
617 call assert_equal(['AAA'], getline(1, '$'))
618 %d
619 call setline(1, 'A')
620 call cursor(1, 1)
621 call feedkeys("A\<c-x>\<c-k>\<down>\<down>\<cr>\<esc>", 'tnix')
622 call assert_equal(['AAAA'], getline(1, '$'))
623 %d
624 call setline(1, 'A')
625 call cursor(1, 1)
626 call feedkeys("A\<c-x>\<c-k>\<down>\<down>\<down>\<cr>\<esc>", 'tnix')
627 call assert_equal(['A'], getline(1, '$'))
628 %d
629 call setline(1, 'A')
630 call cursor(1, 1)
631 call feedkeys("A\<c-x>\<c-k>\<down>\<down>\<down>\<down>\<cr>\<esc>", 'tnix')
632 call assert_equal(['AA'], getline(1, '$'))
633
Bram Moolenaar4b96df52020-01-26 22:00:26 +0100634 " press an unexpected key after dictionary completion
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100635 %d
636 call setline(1, 'A')
637 call cursor(1, 1)
638 call feedkeys("A\<c-x>\<c-k>\<c-]>\<cr>\<esc>", 'tnix')
639 call assert_equal(['AA', ''], getline(1, '$'))
640 %d
641 call setline(1, 'A')
642 call cursor(1, 1)
643 call feedkeys("A\<c-x>\<c-k>\<c-s>\<cr>\<esc>", 'tnix')
644 call assert_equal(["AA\<c-s>", ''], getline(1, '$'))
645 %d
646 call setline(1, 'A')
647 call cursor(1, 1)
648 call feedkeys("A\<c-x>\<c-k>\<c-f>\<cr>\<esc>", 'tnix')
649 call assert_equal(["AA\<c-f>", ''], getline(1, '$'))
650
651 set dictionary=
652 %d
653 call setline(1, 'A')
654 call cursor(1, 1)
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100655 let v:testing = 1
656 try
657 call feedkeys("A\<c-x>\<c-k>\<esc>", 'tnix')
658 catch
659 " error sleeps 2 seconds, when v:testing is not set
660 let v:testing = 0
661 endtry
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100662
Bram Moolenaar30276f22019-01-24 17:59:39 +0100663 call test_override("char_avail", 1)
664 set showcmd
665 %d
666 call feedkeys("A\<c-k>a:\<esc>", 'tnix')
667 call assert_equal(['ä'], getline(1, '$'))
668 call test_override("char_avail", 0)
669 set noshowcmd
670
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100671 bw!
672endfunc
673
Bram Moolenaar1e115362019-01-09 23:01:02 +0100674func Test_edit_CTRL_L()
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100675 " Test Ctrl-X Ctrl-L (line completion)
676 new
677 set complete=.
678 call setline(1, ['one', 'two', 'three', '', '', '', ''])
679 call cursor(4, 1)
680 call feedkeys("A\<c-x>\<c-l>\<esc>", 'tnix')
681 call assert_equal(['one', 'two', 'three', 'three', '', '', ''], getline(1, '$'))
682 call feedkeys("cct\<c-x>\<c-l>\<c-n>\<esc>", 'tnix')
683 call assert_equal(['one', 'two', 'three', 't', '', '', ''], getline(1, '$'))
684 call feedkeys("cct\<c-x>\<c-l>\<c-n>\<c-n>\<esc>", 'tnix')
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100685 call assert_equal(['one', 'two', 'three', 'two', '', '', ''], getline(1, '$'))
Bram Moolenaar02ae9b42018-02-09 15:06:02 +0100686 call feedkeys("cct\<c-x>\<c-l>\<c-n>\<c-n>\<c-n>\<esc>", 'tnix')
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100687 call assert_equal(['one', 'two', 'three', 'three', '', '', ''], getline(1, '$'))
Bram Moolenaar02ae9b42018-02-09 15:06:02 +0100688 call feedkeys("cct\<c-x>\<c-l>\<c-n>\<c-n>\<c-n>\<c-n>\<esc>", 'tnix')
689 call assert_equal(['one', 'two', 'three', 't', '', '', ''], getline(1, '$'))
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100690 call feedkeys("cct\<c-x>\<c-l>\<c-p>\<esc>", 'tnix')
691 call assert_equal(['one', 'two', 'three', 'two', '', '', ''], getline(1, '$'))
692 call feedkeys("cct\<c-x>\<c-l>\<c-p>\<c-p>\<esc>", 'tnix')
693 call assert_equal(['one', 'two', 'three', 't', '', '', ''], getline(1, '$'))
694 call feedkeys("cct\<c-x>\<c-l>\<c-p>\<c-p>\<c-p>\<esc>", 'tnix')
695 call assert_equal(['one', 'two', 'three', 'three', '', '', ''], getline(1, '$'))
696 set complete=
697 call cursor(5, 1)
698 call feedkeys("A\<c-x>\<c-l>\<c-p>\<c-n>\<esc>", 'tnix')
699 call assert_equal(['one', 'two', 'three', 'three', "\<c-l>\<c-p>\<c-n>", '', ''], getline(1, '$'))
700 set complete&
701 %d
702 if has("conceal") && has("syntax")
703 call setline(1, ['foo', 'bar', 'foobar'])
704 call test_override("char_avail", 1)
705 set conceallevel=2 concealcursor=n
706 syn on
707 syn match ErrorMsg "^bar"
708 call matchadd("Conceal", 'oo', 10, -1, {'conceal': 'X'})
709 func! DoIt()
710 let g:change=1
711 endfunc
712 au! TextChangedI <buffer> :call DoIt()
713
714 call cursor(2, 1)
715 call assert_false(exists("g:change"))
716 call feedkeys("A \<esc>", 'tnix')
717 call assert_equal(['foo', 'bar ', 'foobar'], getline(1, '$'))
718 call assert_equal(1, g:change)
719
720 call test_override("char_avail", 0)
721 call clearmatches()
722 syn off
723 au! TextChangedI
724 delfu DoIt
725 unlet! g:change
726 endif
727 bw!
728endfunc
729
Bram Moolenaar1e115362019-01-09 23:01:02 +0100730func Test_edit_CTRL_N()
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100731 " Check keyword completion
Bram Moolenaara1070ea2021-02-20 19:21:36 +0100732 for e in ['latin1', 'utf-8']
733 exe 'set encoding=' .. e
734 new
735 set complete=.
736 call setline(1, ['INFER', 'loWER', '', '', ])
737 call cursor(3, 1)
738 call feedkeys("Ai\<c-n>\<cr>\<esc>", "tnix")
739 call feedkeys("ILO\<c-n>\<cr>\<esc>", 'tnix')
740 call assert_equal(['INFER', 'loWER', 'i', 'LO', '', ''], getline(1, '$'), e)
741 %d
742 call setline(1, ['INFER', 'loWER', '', '', ])
743 call cursor(3, 1)
744 set ignorecase infercase
745 call feedkeys("Ii\<c-n>\<cr>\<esc>", "tnix")
746 call feedkeys("ILO\<c-n>\<cr>\<esc>", 'tnix')
747 call assert_equal(['INFER', 'loWER', 'infer', 'LOWER', '', ''], getline(1, '$'), e)
Yegappan Lakshmanane9825862022-01-03 11:03:48 +0000748 set noignorecase noinfercase
749 %d
750 call setline(1, ['one word', 'two word'])
751 exe "normal! Goo\<C-P>\<C-X>\<C-P>"
752 call assert_equal('one word', getline(3))
753 %d
754 set complete&
Bram Moolenaara1070ea2021-02-20 19:21:36 +0100755 bw!
756 endfor
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100757endfunc
758
Bram Moolenaar1e115362019-01-09 23:01:02 +0100759func Test_edit_CTRL_O()
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100760 " Check for CTRL-O in insert mode
761 new
762 inoreabbr <buffer> h here some more
763 call setline(1, ['abc', 'def'])
764 call cursor(1, 1)
765 " Ctrl-O after an abbreviation
766 exe "norm A h\<c-o>:set nu\<cr> text"
767 call assert_equal(['abc here some more text', 'def'], getline(1, '$'))
768 call assert_true(&nu)
769 set nonu
770 iunabbr <buffer> h
771 " Ctrl-O at end of line with 've'=onemore
772 call cursor(1, 1)
773 call feedkeys("A\<c-o>:let g:a=getpos('.')\<cr>\<esc>", 'tnix')
774 call assert_equal([0, 1, 23, 0], g:a)
775 call cursor(1, 1)
776 set ve=onemore
777 call feedkeys("A\<c-o>:let g:a=getpos('.')\<cr>\<esc>", 'tnix')
778 call assert_equal([0, 1, 24, 0], g:a)
779 set ve=
780 unlet! g:a
781 bw!
782endfunc
783
Bram Moolenaar1e115362019-01-09 23:01:02 +0100784func Test_edit_CTRL_R()
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100785 " Insert Register
786 new
787 call test_override("ALL", 1)
788 set showcmd
789 call feedkeys("AFOOBAR eins zwei\<esc>", 'tnix')
790 call feedkeys("O\<c-r>.", 'tnix')
791 call feedkeys("O\<c-r>=10*500\<cr>\<esc>", 'tnix')
792 call feedkeys("O\<c-r>=getreg('=', 1)\<cr>\<esc>", 'tnix')
793 call assert_equal(["getreg('=', 1)", '5000', "FOOBAR eins zwei", "FOOBAR eins zwei"], getline(1, '$'))
794 call test_override("ALL", 0)
795 set noshowcmd
796 bw!
797endfunc
798
Bram Moolenaar1e115362019-01-09 23:01:02 +0100799func Test_edit_CTRL_S()
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100800 " Test pressing CTRL-S (basically only spellfile completion)
801 " the rest is already covered
802 new
803 if !has("spell")
804 call setline(1, 'vim')
805 call feedkeys("A\<c-x>ss\<cr>\<esc>", 'tnix')
806 call assert_equal(['vims', ''], getline(1, '$'))
807 bw!
808 return
809 endif
810 call setline(1, 'vim')
811 " spell option not yet set
812 try
813 call feedkeys("A\<c-x>\<c-s>\<cr>\<esc>", 'tnix')
814 catch /^Vim\%((\a\+)\)\=:E756/
815 call assert_true(1, 'error caught')
816 endtry
817 call assert_equal(['vim', ''], getline(1, '$'))
818 %d
819 setl spell spelllang=en
820 call setline(1, 'vim')
821 call cursor(1, 1)
822 call feedkeys("A\<c-x>\<c-s>\<cr>\<esc>", 'tnix')
823 call assert_equal(['Vim', ''], getline(1, '$'))
824 %d
825 call setline(1, 'vim')
826 call cursor(1, 1)
827 call feedkeys("A\<c-x>\<c-s>\<down>\<cr>\<esc>", 'tnix')
828 call assert_equal(['Aim'], getline(1, '$'))
829 %d
830 call setline(1, 'vim')
831 call cursor(1, 1)
832 call feedkeys("A\<c-x>\<c-s>\<c-p>\<cr>\<esc>", 'tnix')
833 call assert_equal(['vim', ''], getline(1, '$'))
834 %d
835 " empty buffer
836 call cursor(1, 1)
837 call feedkeys("A\<c-x>\<c-s>\<c-p>\<cr>\<esc>", 'tnix')
838 call assert_equal(['', ''], getline(1, '$'))
839 setl nospell
840 bw!
841endfunc
842
Bram Moolenaar1e115362019-01-09 23:01:02 +0100843func Test_edit_CTRL_T()
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100844 " Check for CTRL-T and CTRL-X CTRL-T in insert mode
845 " 1) increase indent
846 new
847 call setline(1, "abc")
848 call cursor(1, 1)
849 call feedkeys("A\<c-t>xyz", 'tnix')
850 call assert_equal(["\<tab>abcxyz"], getline(1, '$'))
851 " 2) also when paste option is set
852 set paste
853 call setline(1, "abc")
854 call cursor(1, 1)
855 call feedkeys("A\<c-t>xyz", 'tnix')
856 call assert_equal(["\<tab>abcxyz"], getline(1, '$'))
857 set nopaste
858 " CTRL-X CTRL-T (thesaurus complete)
Bram Moolenaar14f91762022-09-21 15:13:52 +0100859 call writefile(['angry furious mad enraged'], 'Xthesaurus', 'D')
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100860 set thesaurus=Xthesaurus
861 call setline(1, 'mad')
862 call cursor(1, 1)
863 call feedkeys("A\<c-x>\<c-t>\<cr>\<esc>", 'tnix')
864 call assert_equal(['mad', ''], getline(1, '$'))
865 %d
866 call setline(1, 'mad')
867 call cursor(1, 1)
868 call feedkeys("A\<c-x>\<c-t>\<c-n>\<cr>\<esc>", 'tnix')
869 call assert_equal(['angry', ''], getline(1, '$'))
870 %d
871 call setline(1, 'mad')
872 call cursor(1, 1)
873 call feedkeys("A\<c-x>\<c-t>\<c-n>\<c-n>\<cr>\<esc>", 'tnix')
874 call assert_equal(['furious', ''], getline(1, '$'))
875 %d
876 call setline(1, 'mad')
877 call cursor(1, 1)
878 call feedkeys("A\<c-x>\<c-t>\<c-n>\<c-n>\<c-n>\<cr>\<esc>", 'tnix')
879 call assert_equal(['enraged', ''], getline(1, '$'))
880 %d
881 call setline(1, 'mad')
882 call cursor(1, 1)
883 call feedkeys("A\<c-x>\<c-t>\<c-n>\<c-n>\<c-n>\<c-n>\<cr>\<esc>", 'tnix')
884 call assert_equal(['mad', ''], getline(1, '$'))
885 %d
886 call setline(1, 'mad')
887 call cursor(1, 1)
888 call feedkeys("A\<c-x>\<c-t>\<c-n>\<c-n>\<c-n>\<c-n>\<c-n>\<cr>\<esc>", 'tnix')
889 call assert_equal(['mad', ''], getline(1, '$'))
890 " Using <c-p> <c-n> when 'complete' is empty
891 set complete=
892 %d
893 call setline(1, 'mad')
894 call cursor(1, 1)
895 call feedkeys("A\<c-x>\<c-t>\<c-n>\<cr>\<esc>", 'tnix')
896 call assert_equal(['angry', ''], getline(1, '$'))
897 %d
898 call setline(1, 'mad')
899 call cursor(1, 1)
900 call feedkeys("A\<c-x>\<c-t>\<c-p>\<cr>\<esc>", 'tnix')
901 call assert_equal(['mad', ''], getline(1, '$'))
902 set complete&
903
904 set thesaurus=
905 %d
906 call setline(1, 'mad')
907 call cursor(1, 1)
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100908 let v:testing = 1
909 try
910 call feedkeys("A\<c-x>\<c-t>\<esc>", 'tnix')
911 catch
912 " error sleeps 2 seconds, when v:testing is not set
913 let v:testing = 0
914 endtry
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100915 call assert_equal(['mad'], getline(1, '$'))
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100916 bw!
917endfunc
918
Yegappan Lakshmanane9825862022-01-03 11:03:48 +0000919" Test thesaurus completion with different encodings
920func Test_thesaurus_complete_with_encoding()
Bram Moolenaar14f91762022-09-21 15:13:52 +0100921 call writefile(['angry furious mad enraged'], 'Xthesaurus', 'D')
Yegappan Lakshmanane9825862022-01-03 11:03:48 +0000922 set thesaurus=Xthesaurus
923 for e in ['latin1', 'utf-8']
924 exe 'set encoding=' .. e
925 new
926 call setline(1, 'mad')
927 call cursor(1, 1)
928 call feedkeys("A\<c-x>\<c-t>\<cr>\<esc>", 'tnix')
929 call assert_equal(['mad', ''], getline(1, '$'))
930 bw!
931 endfor
932 set thesaurus=
Yegappan Lakshmanane9825862022-01-03 11:03:48 +0000933endfunc
934
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +0100935" Test 'thesaurusfunc'
936func MyThesaurus(findstart, base)
937 let mythesaurus = [
938 \ #{word: "happy",
939 \ synonyms: "cheerful,blissful,flying high,looking good,peppy"},
940 \ #{word: "kind",
941 \ synonyms: "amiable,bleeding-heart,heart in right place"}]
942 if a:findstart
943 " locate the start of the word
944 let line = getline('.')
945 let start = col('.') - 1
946 while start > 0 && line[start - 1] =~ '\a'
947 let start -= 1
948 endwhile
949 return start
950 else
951 " find strings matching with "a:base"
952 let res = []
953 for w in mythesaurus
954 if w.word =~ '^' . a:base
955 call add(res, w.word)
956 call extend(res, split(w.synonyms, ","))
957 endif
958 endfor
959 return res
960 endif
961endfunc
962
963func Test_thesaurus_func()
964 new
Bram Moolenaarf4d8b762021-10-17 14:13:09 +0100965 set thesaurus=notused
966 set thesaurusfunc=NotUsed
967 setlocal thesaurusfunc=MyThesaurus
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +0100968 call setline(1, "an ki")
969 call cursor(1, 1)
970 call feedkeys("A\<c-x>\<c-t>\<c-n>\<cr>\<esc>", 'tnix')
971 call assert_equal(['an amiable', ''], getline(1, '$'))
Bram Moolenaarf4d8b762021-10-17 14:13:09 +0100972
973 setlocal thesaurusfunc=NonExistingFunc
974 call assert_fails("normal $a\<C-X>\<C-T>", 'E117:')
975
976 setlocal thesaurusfunc=
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +0100977 set thesaurusfunc=NonExistingFunc
978 call assert_fails("normal $a\<C-X>\<C-T>", 'E117:')
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +0100979 %bw!
Bram Moolenaarf4d8b762021-10-17 14:13:09 +0100980
981 set thesaurusfunc=
982 set thesaurus=
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +0100983endfunc
984
Bram Moolenaar1e115362019-01-09 23:01:02 +0100985func Test_edit_CTRL_U()
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100986 " Test 'completefunc'
987 new
988 " -1, -2 and -3 are special return values
989 let g:special=0
990 fun! CompleteMonths(findstart, base)
991 if a:findstart
992 " locate the start of the word
993 return g:special
994 else
995 " find months matching with "a:base"
996 let res = []
997 for m in split("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec")
998 if m =~ '^\c'.a:base
999 call add(res, {'word': m, 'abbr': m.' Month', 'icase': 0})
1000 endif
1001 endfor
1002 return {'words': res, 'refresh': 'always'}
1003 endif
1004 endfun
1005 set completefunc=CompleteMonths
1006 call setline(1, ['', ''])
1007 call cursor(1, 1)
1008 call feedkeys("AX\<c-x>\<c-u>\<cr>\<esc>", 'tnix')
1009 call assert_equal(['X', '', ''], getline(1, '$'))
1010 %d
1011 let g:special=-1
1012 call feedkeys("AX\<c-x>\<c-u>\<cr>\<esc>", 'tnix')
1013 call assert_equal(['XJan', ''], getline(1, '$'))
1014 %d
1015 let g:special=-2
1016 call feedkeys("AX\<c-x>\<c-u>\<cr>\<esc>", 'tnix')
1017 call assert_equal(['X', ''], getline(1, '$'))
1018 %d
1019 let g:special=-3
1020 call feedkeys("AX\<c-x>\<c-u>\<cr>\<esc>", 'tnix')
1021 call assert_equal(['X', ''], getline(1, '$'))
1022 %d
1023 let g:special=0
1024 call feedkeys("AM\<c-x>\<c-u>\<cr>\<esc>", 'tnix')
1025 call assert_equal(['Mar', ''], getline(1, '$'))
1026 %d
1027 call feedkeys("AM\<c-x>\<c-u>\<c-n>\<cr>\<esc>", 'tnix')
1028 call assert_equal(['May', ''], getline(1, '$'))
1029 %d
1030 call feedkeys("AM\<c-x>\<c-u>\<c-n>\<c-n>\<cr>\<esc>", 'tnix')
1031 call assert_equal(['M', ''], getline(1, '$'))
1032 delfu CompleteMonths
1033 %d
1034 try
1035 call feedkeys("A\<c-x>\<c-u>", 'tnix')
1036 call assert_fails(1, 'unknown completion function')
1037 catch /^Vim\%((\a\+)\)\=:E117/
1038 call assert_true(1, 'E117 error caught')
1039 endtry
1040 set completefunc=
1041 bw!
1042endfunc
1043
Bram Moolenaarff06f282020-04-21 22:01:14 +02001044func Test_edit_completefunc_delete()
1045 func CompleteFunc(findstart, base)
1046 if a:findstart == 1
1047 return col('.') - 1
1048 endif
1049 normal dd
1050 return ['a', 'b']
1051 endfunc
1052 new
1053 set completefunc=CompleteFunc
1054 call setline(1, ['', 'abcd', ''])
1055 2d
zeertzjqcfe45652022-05-27 17:26:55 +01001056 call assert_fails("normal 2G$a\<C-X>\<C-U>", 'E565:')
Bram Moolenaarff06f282020-04-21 22:01:14 +02001057 bwipe!
1058endfunc
1059
1060
Bram Moolenaar1e115362019-01-09 23:01:02 +01001061func Test_edit_CTRL_Z()
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001062 " Ctrl-Z when insertmode is not set inserts it literally
1063 new
1064 call setline(1, 'abc')
1065 call feedkeys("A\<c-z>\<esc>", 'tnix')
1066 call assert_equal(["abc\<c-z>"], getline(1,'$'))
1067 bw!
1068 " TODO: How to Test Ctrl-Z in insert mode, e.g. suspend?
1069endfunc
1070
Bram Moolenaar1e115362019-01-09 23:01:02 +01001071func Test_edit_DROP()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001072 CheckFeature dnd
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001073 new
1074 call setline(1, ['abc def ghi'])
1075 call cursor(1, 1)
1076 try
1077 call feedkeys("i\<Drop>\<Esc>", 'tnix')
1078 call assert_fails(1, 'Invalid register name')
1079 catch /^Vim\%((\a\+)\)\=:E353/
1080 call assert_true(1, 'error caught')
1081 endtry
1082 bw!
1083endfunc
1084
Bram Moolenaar1e115362019-01-09 23:01:02 +01001085func Test_edit_CTRL_V()
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001086 new
1087 call setline(1, ['abc'])
1088 call cursor(2, 1)
zeertzjq502d8ae2022-01-24 15:27:50 +00001089
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001090 " force some redraws
1091 set showmode showcmd
zeertzjq502d8ae2022-01-24 15:27:50 +00001092 call test_override('char_avail', 1)
1093
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001094 call feedkeys("A\<c-v>\<c-n>\<c-v>\<c-l>\<c-v>\<c-b>\<esc>", 'tnix')
1095 call assert_equal(["abc\x0e\x0c\x02"], getline(1, '$'))
1096
1097 if has("rightleft") && exists("+rl")
1098 set rl
1099 call setline(1, ['abc'])
1100 call cursor(2, 1)
1101 call feedkeys("A\<c-v>\<c-n>\<c-v>\<c-l>\<c-v>\<c-b>\<esc>", 'tnix')
1102 call assert_equal(["abc\x0e\x0c\x02"], getline(1, '$'))
1103 set norl
1104 endif
1105
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001106 set noshowmode showcmd
zeertzjq502d8ae2022-01-24 15:27:50 +00001107 call test_override('char_avail', 0)
1108
1109 " No modifiers should be applied to the char typed using i_CTRL-V_digit.
1110 call feedkeys(":append\<CR>\<C-V>76c\<C-V>76\<C-F2>\<C-V>u3c0j\<C-V>u3c0\<M-F3>\<CR>.\<CR>", 'tnix')
1111 call assert_equal('LcL<C-F2>πjπ<M-F3>', getline(2))
1112
1113 if has('osx')
1114 " A char with a modifier should not be a valid char for i_CTRL-V_digit.
1115 call feedkeys("o\<C-V>\<D-j>\<C-V>\<D-1>\<C-V>\<D-o>\<C-V>\<D-x>\<C-V>\<D-u>", 'tnix')
1116 call assert_equal('<D-j><D-1><D-o><D-x><D-u>', getline(3))
1117 endif
1118
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001119 bw!
1120endfunc
1121
Bram Moolenaar1e115362019-01-09 23:01:02 +01001122func Test_edit_F1()
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01001123 CheckFeature quickfix
1124
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001125 " Pressing <f1>
1126 new
1127 call feedkeys(":set im\<cr>\<f1>\<c-l>", 'tnix')
1128 set noinsertmode
1129 call assert_equal('help', &buftype)
1130 bw
1131 bw
1132endfunc
1133
Bram Moolenaar1e115362019-01-09 23:01:02 +01001134func Test_edit_F21()
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001135 " Pressing <f21>
1136 " sends a netbeans command
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001137 CheckFeature netbeans_intg
1138 new
1139 " I have no idea what this is supposed to do :)
1140 call feedkeys("A\<F21>\<F1>\<esc>", 'tnix')
1141 bw
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001142endfunc
1143
Bram Moolenaar1e115362019-01-09 23:01:02 +01001144func Test_edit_HOME_END()
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001145 " Test Home/End Keys
1146 new
1147 set foldopen+=hor
1148 call setline(1, ['abc', 'def'])
1149 call cursor(1, 1)
1150 call feedkeys("AX\<Home>Y\<esc>", 'tnix')
1151 call cursor(2, 1)
1152 call feedkeys("iZ\<End>Y\<esc>", 'tnix')
1153 call assert_equal(['YabcX', 'ZdefY'], getline(1, '$'))
1154
1155 set foldopen-=hor
1156 bw!
1157endfunc
1158
Bram Moolenaar1e115362019-01-09 23:01:02 +01001159func Test_edit_INS()
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001160 " Test for Pressing <Insert>
1161 new
1162 call setline(1, ['abc', 'def'])
1163 call cursor(1, 1)
1164 call feedkeys("i\<Insert>ZYX>", 'tnix')
1165 call assert_equal(['ZYX>', 'def'], getline(1, '$'))
1166 call setline(1, ['abc', 'def'])
1167 call cursor(1, 1)
1168 call feedkeys("i\<Insert>Z\<Insert>YX>", 'tnix')
1169 call assert_equal(['ZYX>bc', 'def'], getline(1, '$'))
1170 bw!
1171endfunc
1172
Bram Moolenaar1e115362019-01-09 23:01:02 +01001173func Test_edit_LEFT_RIGHT()
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001174 " Left, Shift-Left, Right, Shift-Right
1175 new
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001176 call setline(1, ['abc def ghi', 'ABC DEF GHI', 'ZZZ YYY XXX'])
1177 let _ww=&ww
1178 set ww=
1179 call cursor(2, 1)
1180 call feedkeys("i\<left>\<esc>", 'tnix')
1181 call assert_equal([0, 2, 1, 0], getpos('.'))
1182 " Is this a bug, <s-left> does not respect whichwrap option
1183 call feedkeys("i\<s-left>\<esc>", 'tnix')
1184 call assert_equal([0, 1, 8, 0], getpos('.'))
1185 call feedkeys("i". repeat("\<s-left>", 3). "\<esc>", 'tnix')
1186 call assert_equal([0, 1, 1, 0], getpos('.'))
1187 call feedkeys("i\<right>\<esc>", 'tnix')
1188 call assert_equal([0, 1, 1, 0], getpos('.'))
1189 call feedkeys("i\<right>\<right>\<esc>", 'tnix')
1190 call assert_equal([0, 1, 2, 0], getpos('.'))
1191 call feedkeys("A\<right>\<esc>", 'tnix')
1192 call assert_equal([0, 1, 11, 0], getpos('.'))
1193 call feedkeys("A\<s-right>\<esc>", 'tnix')
1194 call assert_equal([0, 2, 1, 0], getpos('.'))
1195 call feedkeys("i\<s-right>\<esc>", 'tnix')
1196 call assert_equal([0, 2, 4, 0], getpos('.'))
1197 call cursor(3, 11)
1198 call feedkeys("A\<right>\<esc>", 'tnix')
1199 call feedkeys("A\<s-right>\<esc>", 'tnix')
1200 call assert_equal([0, 3, 11, 0], getpos('.'))
1201 call cursor(2, 11)
1202 " <S-Right> does not respect 'whichwrap' option
1203 call feedkeys("A\<s-right>\<esc>", 'tnix')
1204 call assert_equal([0, 3, 1, 0], getpos('.'))
1205 " Check motion when 'whichwrap' contains cursor keys for insert mode
1206 set ww+=[,]
1207 call cursor(2, 1)
1208 call feedkeys("i\<left>\<esc>", 'tnix')
1209 call assert_equal([0, 1, 11, 0], getpos('.'))
1210 call cursor(2, 11)
1211 call feedkeys("A\<right>\<esc>", 'tnix')
1212 call assert_equal([0, 3, 1, 0], getpos('.'))
1213 call cursor(2, 11)
1214 call feedkeys("A\<s-right>\<esc>", 'tnix')
1215 call assert_equal([0, 3, 1, 0], getpos('.'))
1216 let &ww = _ww
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001217 bw!
1218endfunc
1219
Bram Moolenaar1e115362019-01-09 23:01:02 +01001220func Test_edit_MOUSE()
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001221 " This is a simple test, since we not really using the mouse here
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001222 CheckFeature mouse
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001223 10new
1224 call setline(1, range(1, 100))
1225 call cursor(1, 1)
Bram Moolenaar8e8dc9b2022-05-08 20:38:06 +01001226 call assert_equal(1, line('w0'))
1227 call assert_equal(10, line('w$'))
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001228 set mouse=a
Bram Moolenaar8e8dc9b2022-05-08 20:38:06 +01001229 " One scroll event moves three lines.
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001230 call feedkeys("A\<ScrollWheelDown>\<esc>", 'tnix')
Bram Moolenaar8e8dc9b2022-05-08 20:38:06 +01001231 call assert_equal(4, line('w0'))
1232 call assert_equal(13, line('w$'))
1233 " This should move by one page down.
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001234 call feedkeys("A\<S-ScrollWheelDown>\<esc>", 'tnix')
Bram Moolenaar8e8dc9b2022-05-08 20:38:06 +01001235 call assert_equal(14, line('w0'))
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001236 set nostartofline
Bram Moolenaar8e8dc9b2022-05-08 20:38:06 +01001237 " Another page down.
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001238 call feedkeys("A\<C-ScrollWheelDown>\<esc>", 'tnix')
Bram Moolenaar8e8dc9b2022-05-08 20:38:06 +01001239 call assert_equal(24, line('w0'))
1240
1241 call assert_equal([0, 24, 2, 0], getpos('.'))
1242 call test_setmouse(4, 3)
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001243 call feedkeys("A\<LeftMouse>\<esc>", 'tnix')
Bram Moolenaar8e8dc9b2022-05-08 20:38:06 +01001244 call assert_equal([0, 27, 2, 0], getpos('.'))
Bram Moolenaarb3707712022-05-08 22:49:43 +01001245 set mousemodel=extend
Bram Moolenaar8e8dc9b2022-05-08 20:38:06 +01001246 call test_setmouse(5, 3)
1247 call feedkeys("A\<RightMouse>\<esc>\<esc>", 'tnix')
1248 call assert_equal([0, 28, 2, 0], getpos('.'))
Bram Moolenaarb3707712022-05-08 22:49:43 +01001249 set mousemodel&
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001250 call cursor(1, 100)
1251 norm! zt
1252 " this should move by a screen up, but when the test
1253 " is run, it moves up to the top of the buffer...
1254 call feedkeys("A\<ScrollWheelUp>\<esc>", 'tnix')
1255 call assert_equal([0, 1, 1, 0], getpos('.'))
1256 call cursor(1, 30)
1257 norm! zt
1258 call feedkeys("A\<S-ScrollWheelUp>\<esc>", 'tnix')
1259 call assert_equal([0, 1, 1, 0], getpos('.'))
1260 call cursor(1, 30)
1261 norm! zt
1262 call feedkeys("A\<C-ScrollWheelUp>\<esc>", 'tnix')
1263 call assert_equal([0, 1, 1, 0], getpos('.'))
1264 %d
1265 call setline(1, repeat(["12345678901234567890"], 100))
1266 call cursor(2, 1)
1267 call feedkeys("A\<ScrollWheelRight>\<esc>", 'tnix')
1268 call assert_equal([0, 2, 20, 0], getpos('.'))
1269 call feedkeys("A\<ScrollWheelLeft>\<esc>", 'tnix')
1270 call assert_equal([0, 2, 20, 0], getpos('.'))
1271 call feedkeys("A\<S-ScrollWheelRight>\<esc>", 'tnix')
1272 call assert_equal([0, 2, 20, 0], getpos('.'))
1273 call feedkeys("A\<S-ScrollWheelLeft>\<esc>", 'tnix')
1274 call assert_equal([0, 2, 20, 0], getpos('.'))
1275 call feedkeys("A\<C-ScrollWheelRight>\<esc>", 'tnix')
1276 call assert_equal([0, 2, 20, 0], getpos('.'))
1277 call feedkeys("A\<C-ScrollWheelLeft>\<esc>", 'tnix')
1278 call assert_equal([0, 2, 20, 0], getpos('.'))
1279 set mouse& startofline
1280 bw!
1281endfunc
1282
Bram Moolenaar1e115362019-01-09 23:01:02 +01001283func Test_edit_PAGEUP_PAGEDOWN()
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001284 10new
1285 call setline(1, repeat(['abc def ghi'], 30))
1286 call cursor(1, 1)
1287 call feedkeys("i\<PageDown>\<esc>", 'tnix')
1288 call assert_equal([0, 9, 1, 0], getpos('.'))
1289 call feedkeys("i\<PageDown>\<esc>", 'tnix')
1290 call assert_equal([0, 17, 1, 0], getpos('.'))
1291 call feedkeys("i\<PageDown>\<esc>", 'tnix')
1292 call assert_equal([0, 25, 1, 0], getpos('.'))
1293 call feedkeys("i\<PageDown>\<esc>", 'tnix')
1294 call assert_equal([0, 30, 1, 0], getpos('.'))
1295 call feedkeys("i\<PageDown>\<esc>", 'tnix')
1296 call assert_equal([0, 30, 1, 0], getpos('.'))
1297 call feedkeys("A\<PageUp>\<esc>", 'tnix')
1298 call assert_equal([0, 29, 1, 0], getpos('.'))
1299 call feedkeys("A\<PageUp>\<esc>", 'tnix')
1300 call assert_equal([0, 21, 1, 0], getpos('.'))
1301 call feedkeys("A\<PageUp>\<esc>", 'tnix')
1302 call assert_equal([0, 13, 1, 0], getpos('.'))
1303 call feedkeys("A\<PageUp>\<esc>", 'tnix')
1304 call assert_equal([0, 5, 1, 0], getpos('.'))
1305 call feedkeys("A\<PageUp>\<esc>", 'tnix')
1306 call assert_equal([0, 5, 11, 0], getpos('.'))
1307 " <S-Up> is the same as <PageUp>
1308 " <S-Down> is the same as <PageDown>
1309 call cursor(1, 1)
1310 call feedkeys("i\<S-Down>\<esc>", 'tnix')
1311 call assert_equal([0, 9, 1, 0], getpos('.'))
1312 call feedkeys("i\<S-Down>\<esc>", 'tnix')
1313 call assert_equal([0, 17, 1, 0], getpos('.'))
1314 call feedkeys("i\<S-Down>\<esc>", 'tnix')
1315 call assert_equal([0, 25, 1, 0], getpos('.'))
1316 call feedkeys("i\<S-Down>\<esc>", 'tnix')
1317 call assert_equal([0, 30, 1, 0], getpos('.'))
1318 call feedkeys("i\<S-Down>\<esc>", 'tnix')
1319 call assert_equal([0, 30, 1, 0], getpos('.'))
1320 call feedkeys("A\<S-Up>\<esc>", 'tnix')
1321 call assert_equal([0, 29, 1, 0], getpos('.'))
1322 call feedkeys("A\<S-Up>\<esc>", 'tnix')
1323 call assert_equal([0, 21, 1, 0], getpos('.'))
1324 call feedkeys("A\<S-Up>\<esc>", 'tnix')
1325 call assert_equal([0, 13, 1, 0], getpos('.'))
1326 call feedkeys("A\<S-Up>\<esc>", 'tnix')
1327 call assert_equal([0, 5, 1, 0], getpos('.'))
1328 call feedkeys("A\<S-Up>\<esc>", 'tnix')
1329 call assert_equal([0, 5, 11, 0], getpos('.'))
1330 set nostartofline
1331 call cursor(30, 11)
1332 norm! zt
1333 call feedkeys("A\<PageUp>\<esc>", 'tnix')
1334 call assert_equal([0, 29, 11, 0], getpos('.'))
1335 call feedkeys("A\<PageUp>\<esc>", 'tnix')
1336 call assert_equal([0, 21, 11, 0], getpos('.'))
1337 call feedkeys("A\<PageUp>\<esc>", 'tnix')
1338 call assert_equal([0, 13, 11, 0], getpos('.'))
1339 call feedkeys("A\<PageUp>\<esc>", 'tnix')
1340 call assert_equal([0, 5, 11, 0], getpos('.'))
1341 call feedkeys("A\<PageUp>\<esc>", 'tnix')
1342 call assert_equal([0, 5, 11, 0], getpos('.'))
1343 call cursor(1, 1)
1344 call feedkeys("A\<PageDown>\<esc>", 'tnix')
1345 call assert_equal([0, 9, 11, 0], getpos('.'))
1346 call feedkeys("A\<PageDown>\<esc>", 'tnix')
1347 call assert_equal([0, 17, 11, 0], getpos('.'))
1348 call feedkeys("A\<PageDown>\<esc>", 'tnix')
1349 call assert_equal([0, 25, 11, 0], getpos('.'))
1350 call feedkeys("A\<PageDown>\<esc>", 'tnix')
1351 call assert_equal([0, 30, 11, 0], getpos('.'))
1352 call feedkeys("A\<PageDown>\<esc>", 'tnix')
1353 call assert_equal([0, 30, 11, 0], getpos('.'))
1354 " <S-Up> is the same as <PageUp>
1355 " <S-Down> is the same as <PageDown>
1356 call cursor(30, 11)
1357 norm! zt
1358 call feedkeys("A\<S-Up>\<esc>", 'tnix')
1359 call assert_equal([0, 29, 11, 0], getpos('.'))
1360 call feedkeys("A\<S-Up>\<esc>", 'tnix')
1361 call assert_equal([0, 21, 11, 0], getpos('.'))
1362 call feedkeys("A\<S-Up>\<esc>", 'tnix')
1363 call assert_equal([0, 13, 11, 0], getpos('.'))
1364 call feedkeys("A\<S-Up>\<esc>", 'tnix')
1365 call assert_equal([0, 5, 11, 0], getpos('.'))
1366 call feedkeys("A\<S-Up>\<esc>", 'tnix')
1367 call assert_equal([0, 5, 11, 0], getpos('.'))
1368 call cursor(1, 1)
1369 call feedkeys("A\<S-Down>\<esc>", 'tnix')
1370 call assert_equal([0, 9, 11, 0], getpos('.'))
1371 call feedkeys("A\<S-Down>\<esc>", 'tnix')
1372 call assert_equal([0, 17, 11, 0], getpos('.'))
1373 call feedkeys("A\<S-Down>\<esc>", 'tnix')
1374 call assert_equal([0, 25, 11, 0], getpos('.'))
1375 call feedkeys("A\<S-Down>\<esc>", 'tnix')
1376 call assert_equal([0, 30, 11, 0], getpos('.'))
1377 call feedkeys("A\<S-Down>\<esc>", 'tnix')
1378 call assert_equal([0, 30, 11, 0], getpos('.'))
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001379 bw!
1380endfunc
1381
Bram Moolenaar1e115362019-01-09 23:01:02 +01001382func Test_edit_forbidden()
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001383 new
1384 " 1) edit in the sandbox is not allowed
1385 call setline(1, 'a')
1386 com! Sandbox :sandbox call feedkeys("i\<del>\<esc>", 'tnix')
1387 call assert_fails(':Sandbox', 'E48:')
1388 com! Sandbox :sandbox exe "norm! i\<del>"
1389 call assert_fails(':Sandbox', 'E48:')
1390 delcom Sandbox
1391 call assert_equal(['a'], getline(1,'$'))
Bram Moolenaar52797ba2021-12-16 14:45:13 +00001392
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001393 " 2) edit with textlock set
1394 fu! DoIt()
1395 call feedkeys("i\<del>\<esc>", 'tnix')
1396 endfu
1397 au InsertCharPre <buffer> :call DoIt()
1398 try
1399 call feedkeys("ix\<esc>", 'tnix')
1400 call assert_fails(1, 'textlock')
Bram Moolenaarff06f282020-04-21 22:01:14 +02001401 catch /^Vim\%((\a\+)\)\=:E565/ " catch E565: not allowed here
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001402 endtry
1403 " TODO: Might be a bug: should x really be inserted here
1404 call assert_equal(['xa'], getline(1, '$'))
1405 delfu DoIt
1406 try
1407 call feedkeys("ix\<esc>", 'tnix')
1408 call assert_fails(1, 'unknown function')
1409 catch /^Vim\%((\a\+)\)\=:E117/ " catch E117: unknown function
1410 endtry
1411 au! InsertCharPre
Bram Moolenaar52797ba2021-12-16 14:45:13 +00001412
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001413 " 3) edit when completion is shown
1414 fun! Complete(findstart, base)
1415 if a:findstart
1416 return col('.')
1417 else
1418 call feedkeys("i\<del>\<esc>", 'tnix')
1419 return []
1420 endif
1421 endfun
1422 set completefunc=Complete
1423 try
1424 call feedkeys("i\<c-x>\<c-u>\<esc>", 'tnix')
1425 call assert_fails(1, 'change in complete function')
Bram Moolenaarff06f282020-04-21 22:01:14 +02001426 catch /^Vim\%((\a\+)\)\=:E565/ " catch E565
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001427 endtry
1428 delfu Complete
1429 set completefunc=
Bram Moolenaar52797ba2021-12-16 14:45:13 +00001430
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001431 if has("rightleft") && exists("+fkmap")
1432 " 4) 'R' when 'fkmap' and 'revins' is set.
1433 set revins fkmap
1434 try
1435 normal Ri
1436 call assert_fails(1, "R with 'fkmap' and 'ri' set")
1437 catch
1438 finally
1439 set norevins nofkmap
1440 endtry
1441 endif
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001442 bw!
1443endfunc
1444
Bram Moolenaar1e115362019-01-09 23:01:02 +01001445func Test_edit_rightleft()
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001446 " Cursor in rightleft mode moves differently
Bram Moolenaar845e0ee2020-06-20 16:05:32 +02001447 CheckFeature rightleft
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001448 call NewWindow(10, 20)
1449 call setline(1, ['abc', 'def', 'ghi'])
1450 call cursor(1, 2)
1451 set rightleft
1452 " Screen looks as expected
1453 let lines = ScreenLines([1, 4], winwidth(0))
1454 let expect = [
1455 \" cba",
1456 \" fed",
1457 \" ihg",
1458 \" ~"]
1459 call assert_equal(join(expect, "\n"), join(lines, "\n"))
1460 " 2) right moves to the left
1461 call feedkeys("i\<right>\<esc>x", 'txin')
1462 call assert_equal(['bc', 'def', 'ghi'], getline(1,'$'))
1463 call cursor(1, 2)
1464 call feedkeys("i\<s-right>\<esc>", 'txin')
1465 call cursor(1, 2)
1466 call feedkeys("i\<c-right>\<esc>", 'txin')
1467 " Screen looks as expected
1468 let lines = ScreenLines([1, 4], winwidth(0))
1469 let expect = [
1470 \" cb",
1471 \" fed",
1472 \" ihg",
1473 \" ~"]
1474 call assert_equal(join(expect, "\n"), join(lines, "\n"))
1475 " 2) left moves to the right
1476 call setline(1, ['abc', 'def', 'ghi'])
1477 call cursor(1, 2)
1478 call feedkeys("i\<left>\<esc>x", 'txin')
1479 call assert_equal(['ac', 'def', 'ghi'], getline(1,'$'))
1480 call cursor(1, 2)
1481 call feedkeys("i\<s-left>\<esc>", 'txin')
1482 call cursor(1, 2)
1483 call feedkeys("i\<c-left>\<esc>", 'txin')
1484 " Screen looks as expected
1485 let lines = ScreenLines([1, 4], winwidth(0))
1486 let expect = [
1487 \" ca",
1488 \" fed",
1489 \" ihg",
1490 \" ~"]
1491 call assert_equal(join(expect, "\n"), join(lines, "\n"))
Bram Moolenaar845e0ee2020-06-20 16:05:32 +02001492 %d _
1493 call test_override('redraw_flag', 1)
1494 call test_override('char_avail', 1)
1495 call feedkeys("a\<C-V>x41", "xt")
1496 redraw!
1497 call assert_equal(repeat(' ', 19) .. 'A', Screenline(1))
1498 call test_override('ALL', 0)
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001499 set norightleft
1500 bw!
1501endfunc
Bram Moolenaar658a3a22017-03-31 22:27:12 +02001502
1503func Test_edit_complete_very_long_name()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001504 " Long directory names only work on Unix.
1505 CheckUnix
Bram Moolenaar15ecbd62017-04-07 14:10:48 +02001506
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01001507 let dirname = getcwd() . "/Xlongdir"
Bram Moolenaar15ecbd62017-04-07 14:10:48 +02001508 let longdirname = dirname . repeat('/' . repeat('d', 255), 4)
1509 try
Bram Moolenaar14f91762022-09-21 15:13:52 +01001510 call mkdir(longdirname, 'pR')
Bram Moolenaar15ecbd62017-04-07 14:10:48 +02001511 catch /E739:/
1512 " Long directory name probably not supported.
1513 call delete(dirname, 'rf')
1514 return
1515 endtry
1516
Bram Moolenaarf8191c52019-05-18 17:22:54 +02001517 " Try to get the Vim window position before setting 'columns', so that we can
1518 " move the window back to where it was.
Bram Moolenaarba6ec182017-04-04 22:41:10 +02001519 let winposx = getwinposx()
1520 let winposy = getwinposy()
Bram Moolenaarf8191c52019-05-18 17:22:54 +02001521
1522 if winposx >= 0 && winposy >= 0 && !has('gui_running')
1523 " We did get the window position, but xterm may report the wrong numbers.
1524 " Move the window to the reported position and compute any offset.
1525 exe 'winpos ' . winposx . ' ' . winposy
1526 sleep 100m
1527 let x = getwinposx()
1528 if x >= 0
1529 let winposx += winposx - x
1530 endif
1531 let y = getwinposy()
1532 if y >= 0
1533 let winposy += winposy - y
1534 endif
1535 endif
1536
Bram Moolenaar658a3a22017-03-31 22:27:12 +02001537 let save_columns = &columns
Bram Moolenaar15ecbd62017-04-07 14:10:48 +02001538 " Need at least about 1100 columns to reproduce the problem.
Bram Moolenaarba6ec182017-04-04 22:41:10 +02001539 set columns=2000
Bram Moolenaar658a3a22017-03-31 22:27:12 +02001540 set noswapfile
Bram Moolenaarba6ec182017-04-04 22:41:10 +02001541
Bram Moolenaar658a3a22017-03-31 22:27:12 +02001542 let longfilename = longdirname . '/' . repeat('a', 255)
Bram Moolenaar658a3a22017-03-31 22:27:12 +02001543 call writefile(['Totum', 'Table'], longfilename)
1544 new
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01001545 exe "next Xnofile " . longfilename
Bram Moolenaar658a3a22017-03-31 22:27:12 +02001546 exe "normal iT\<C-N>"
1547
1548 bwipe!
1549 exe 'bwipe! ' . longfilename
Bram Moolenaar658a3a22017-03-31 22:27:12 +02001550 let &columns = save_columns
Bram Moolenaarba6ec182017-04-04 22:41:10 +02001551 if winposx >= 0 && winposy >= 0
1552 exe 'winpos ' . winposx . ' ' . winposy
1553 endif
Bram Moolenaar658a3a22017-03-31 22:27:12 +02001554 set swapfile&
1555endfunc
Bram Moolenaarff930ca2017-10-19 17:12:10 +02001556
Bram Moolenaar2c8c6812018-07-28 17:07:52 +02001557func Test_edit_backtick()
1558 next a\`b c
1559 call assert_equal('a`b', expand('%'))
1560 next
1561 call assert_equal('c', expand('%'))
1562 call assert_equal('a\`b c', expand('##'))
1563endfunc
1564
Bram Moolenaarff930ca2017-10-19 17:12:10 +02001565func Test_edit_quit()
1566 edit foo.txt
1567 split
1568 new
1569 call setline(1, 'hello')
1570 3wincmd w
1571 redraw!
1572 call assert_fails('1q', 'E37:')
1573 bwipe! foo.txt
1574 only
1575endfunc
1576
Bram Moolenaaradb8fbe2018-06-04 20:34:23 +02001577func Test_edit_alt()
1578 " Keeping the cursor line didn't happen when the first line has indent.
1579 new
1580 call setline(1, [' one', 'two', 'three'])
1581 w XAltFile
1582 $
1583 call assert_equal(3, line('.'))
1584 e Xother
1585 e #
1586 call assert_equal(3, line('.'))
1587
1588 bwipe XAltFile
1589 call delete('XAltFile')
1590endfunc
Bram Moolenaar4dbc2622018-11-02 11:59:15 +01001591
Bram Moolenaardb934952020-04-27 20:18:31 +02001592func Test_edit_InsertLeave()
Bram Moolenaar4dbc2622018-11-02 11:59:15 +01001593 new
Bram Moolenaarb53e13a2020-10-21 12:19:53 +02001594 au InsertLeavePre * let g:did_au_pre = 1
Bram Moolenaar4dbc2622018-11-02 11:59:15 +01001595 au InsertLeave * let g:did_au = 1
Bram Moolenaarb53e13a2020-10-21 12:19:53 +02001596 let g:did_au_pre = 0
Bram Moolenaar4dbc2622018-11-02 11:59:15 +01001597 let g:did_au = 0
1598 call feedkeys("afoo\<Esc>", 'tx')
Bram Moolenaarb53e13a2020-10-21 12:19:53 +02001599 call assert_equal(1, g:did_au_pre)
Bram Moolenaar4dbc2622018-11-02 11:59:15 +01001600 call assert_equal(1, g:did_au)
1601 call assert_equal('foo', getline(1))
1602
Bram Moolenaarb53e13a2020-10-21 12:19:53 +02001603 let g:did_au_pre = 0
Bram Moolenaar4dbc2622018-11-02 11:59:15 +01001604 let g:did_au = 0
1605 call feedkeys("Sbar\<C-C>", 'tx')
Bram Moolenaarb53e13a2020-10-21 12:19:53 +02001606 call assert_equal(1, g:did_au_pre)
Bram Moolenaar4dbc2622018-11-02 11:59:15 +01001607 call assert_equal(0, g:did_au)
1608 call assert_equal('bar', getline(1))
1609
1610 inoremap x xx<Esc>
Bram Moolenaarb53e13a2020-10-21 12:19:53 +02001611 let g:did_au_pre = 0
Bram Moolenaar4dbc2622018-11-02 11:59:15 +01001612 let g:did_au = 0
1613 call feedkeys("Saax", 'tx')
Bram Moolenaarb53e13a2020-10-21 12:19:53 +02001614 call assert_equal(1, g:did_au_pre)
Bram Moolenaar4dbc2622018-11-02 11:59:15 +01001615 call assert_equal(1, g:did_au)
1616 call assert_equal('aaxx', getline(1))
1617
1618 inoremap x xx<C-C>
Bram Moolenaarb53e13a2020-10-21 12:19:53 +02001619 let g:did_au_pre = 0
Bram Moolenaar4dbc2622018-11-02 11:59:15 +01001620 let g:did_au = 0
1621 call feedkeys("Sbbx", 'tx')
Bram Moolenaarb53e13a2020-10-21 12:19:53 +02001622 call assert_equal(1, g:did_au_pre)
Bram Moolenaar4dbc2622018-11-02 11:59:15 +01001623 call assert_equal(0, g:did_au)
1624 call assert_equal('bbxx', getline(1))
1625
1626 bwipe!
Bram Moolenaarb53e13a2020-10-21 12:19:53 +02001627 au! InsertLeave InsertLeavePre
Bram Moolenaar4dbc2622018-11-02 11:59:15 +01001628 iunmap x
1629endfunc
Bram Moolenaarc6b37db2019-04-27 18:00:34 +02001630
Bram Moolenaardb934952020-04-27 20:18:31 +02001631func Test_edit_InsertLeave_undo()
1632 new XtestUndo
1633 set undofile
1634 au InsertLeave * wall
1635 exe "normal ofoo\<Esc>"
1636 call assert_equal(2, line('$'))
1637 normal u
1638 call assert_equal(1, line('$'))
1639
1640 bwipe!
1641 au! InsertLeave
1642 call delete('XtestUndo')
Bram Moolenaarb340bae2020-06-15 19:51:56 +02001643 call delete(undofile('XtestUndo'))
Bram Moolenaardb934952020-04-27 20:18:31 +02001644 set undofile&
1645endfunc
1646
Bram Moolenaarc6b37db2019-04-27 18:00:34 +02001647" Test for inserting characters using CTRL-V followed by a number.
1648func Test_edit_special_chars()
1649 new
1650
Bram Moolenaar424bcae2022-01-31 14:59:41 +00001651 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 +02001652
1653 exe "normal " . t
1654 call assert_equal("ABC !a\<C-O>g\<C-G>8", getline(2))
1655
1656 close!
1657endfunc
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02001658
1659func Test_edit_startinsert()
1660 new
1661 set backspace+=start
1662 call setline(1, 'foobar')
1663 call feedkeys("A\<C-U>\<Esc>", 'xt')
1664 call assert_equal('', getline(1))
1665
1666 call setline(1, 'foobar')
1667 call feedkeys(":startinsert!\<CR>\<C-U>\<Esc>", 'xt')
1668 call assert_equal('', getline(1))
1669
1670 set backspace&
1671 bwipe!
1672endfunc
Bram Moolenaar177c9f22019-11-06 13:59:16 +01001673
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +01001674" Test for :startreplace and :startgreplace
1675func Test_edit_startreplace()
1676 new
1677 call setline(1, 'abc')
1678 call feedkeys("l:startreplace\<CR>xyz\e", 'xt')
1679 call assert_equal('axyz', getline(1))
1680 call feedkeys("0:startreplace!\<CR>abc\e", 'xt')
1681 call assert_equal('axyzabc', getline(1))
1682 call setline(1, "a\tb")
1683 call feedkeys("0l:startgreplace\<CR>xyz\e", 'xt')
1684 call assert_equal("axyz\tb", getline(1))
1685 call feedkeys("0i\<C-R>=execute('startreplace')\<CR>12\e", 'xt')
1686 call assert_equal("12axyz\tb", getline(1))
1687 close!
1688endfunc
1689
Bram Moolenaar177c9f22019-11-06 13:59:16 +01001690func Test_edit_noesckeys()
Bram Moolenaar215ba3b2019-11-06 15:07:07 +01001691 CheckNotGui
Bram Moolenaar177c9f22019-11-06 13:59:16 +01001692 new
1693
1694 " <Left> moves cursor when 'esckeys' is set
1695 exe "set t_kl=\<Esc>OD"
1696 set esckeys
1697 call feedkeys("axyz\<Esc>ODX", "xt")
1698 call assert_equal("xyXz", getline(1))
1699
1700 " <Left> exits Insert mode when 'esckeys' is off
1701 set noesckeys
1702 call setline(1, '')
1703 call feedkeys("axyz\<Esc>ODX", "xt")
1704 call assert_equal(["DX", "xyz"], getline(1, 2))
1705
1706 bwipe!
1707 set esckeys
1708endfunc
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +01001709
Bram Moolenaar1671f442020-03-10 07:48:13 +01001710" Test for running an invalid ex command in insert mode using CTRL-O
Bram Moolenaar1671f442020-03-10 07:48:13 +01001711func Test_edit_ctrl_o_invalid_cmd()
1712 new
1713 set showmode showcmd
Bram Moolenaarb340bae2020-06-15 19:51:56 +02001714 " Avoid a sleep of 3 seconds. Zero might have side effects.
1715 call test_override('ui_delay', 50)
Bram Moolenaar1671f442020-03-10 07:48:13 +01001716 let caught_e492 = 0
1717 try
1718 call feedkeys("i\<C-O>:invalid\<CR>abc\<Esc>", "xt")
1719 catch /E492:/
1720 let caught_e492 = 1
1721 endtry
1722 call assert_equal(1, caught_e492)
1723 call assert_equal('abc', getline(1))
1724 set showmode& showcmd&
Bram Moolenaarb340bae2020-06-15 19:51:56 +02001725 call test_override('ui_delay', 0)
Bram Moolenaar1671f442020-03-10 07:48:13 +01001726 close!
1727endfunc
1728
Bram Moolenaarb340bae2020-06-15 19:51:56 +02001729" Test for editing a file with a very long name
1730func Test_edit_illegal_filename()
1731 CheckEnglish
1732 new
1733 redir => msg
1734 exe 'edit ' . repeat('f', 5000)
1735 redir END
1736 call assert_match("Illegal file name$", split(msg, "\n")[0])
1737 close!
1738endfunc
1739
Bram Moolenaarc8fe6452020-10-03 17:04:37 +02001740" Test for editing a directory
1741func Test_edit_is_a_directory()
1742 CheckEnglish
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01001743 let dirname = getcwd() . "/Xeditdir"
Bram Moolenaarc8fe6452020-10-03 17:04:37 +02001744 call mkdir(dirname, 'p')
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 let dirname .= '/'
1754
1755 new
1756 redir => msg
1757 exe 'edit' dirname
1758 redir END
1759 call assert_match("is a directory$", split(msg, "\n")[0])
1760 bwipe!
1761
1762 call delete(dirname, 'rf')
1763endfunc
1764
Bram Moolenaarb340bae2020-06-15 19:51:56 +02001765" Test for editing a file using invalid file encoding
1766func Test_edit_invalid_encoding()
1767 CheckEnglish
Bram Moolenaar14f91762022-09-21 15:13:52 +01001768 call writefile([], 'Xinvfile', 'D')
Bram Moolenaarb340bae2020-06-15 19:51:56 +02001769 redir => msg
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01001770 new ++enc=axbyc Xinvfile
Bram Moolenaarb340bae2020-06-15 19:51:56 +02001771 redir END
1772 call assert_match('\[NOT converted\]', msg)
Bram Moolenaarb340bae2020-06-15 19:51:56 +02001773 close!
1774endfunc
1775
1776" Test for the "charconvert" option
1777func Test_edit_charconvert()
1778 CheckEnglish
Bram Moolenaar14f91762022-09-21 15:13:52 +01001779 call writefile(['one', 'two'], 'Xccfile', 'D')
Bram Moolenaarb340bae2020-06-15 19:51:56 +02001780
1781 " set 'charconvert' to a non-existing function
1782 set charconvert=NonExitingFunc()
1783 new
1784 let caught_e117 = v:false
1785 try
1786 redir => msg
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01001787 edit ++enc=axbyc Xccfile
Bram Moolenaarb340bae2020-06-15 19:51:56 +02001788 catch /E117:/
1789 let caught_e117 = v:true
1790 finally
1791 redir END
1792 endtry
1793 call assert_true(caught_e117)
1794 call assert_equal(['one', 'two'], getline(1, '$'))
1795 call assert_match("Conversion with 'charconvert' failed", msg)
1796 close!
1797 set charconvert&
1798
1799 " 'charconvert' function doesn't create a output file
1800 func Cconv1()
1801 endfunc
1802 set charconvert=Cconv1()
1803 new
1804 redir => msg
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01001805 edit ++enc=axbyc Xccfile
Bram Moolenaarb340bae2020-06-15 19:51:56 +02001806 redir END
1807 call assert_equal(['one', 'two'], getline(1, '$'))
1808 call assert_match("can't read output of 'charconvert'", msg)
1809 close!
1810 delfunc Cconv1
1811 set charconvert&
1812
1813 " 'charconvert' function to convert to upper case
1814 func Cconv2()
1815 let data = readfile(v:fname_in)
1816 call map(data, 'toupper(v:val)')
1817 call writefile(data, v:fname_out)
1818 endfunc
1819 set charconvert=Cconv2()
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01001820 new Xccfile
1821 write ++enc=ucase Xccfile1
1822 call assert_equal(['ONE', 'TWO'], readfile('Xccfile1'))
1823 call delete('Xccfile1')
Bram Moolenaarb340bae2020-06-15 19:51:56 +02001824 close!
1825 delfunc Cconv2
1826 set charconvert&
1827
1828 " 'charconvert' function removes the input file
1829 func Cconv3()
1830 call delete(v:fname_in)
1831 endfunc
1832 set charconvert=Cconv3()
1833 new
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01001834 call assert_fails('edit ++enc=lcase Xccfile', 'E202:')
Bram Moolenaarb340bae2020-06-15 19:51:56 +02001835 call assert_equal([''], getline(1, '$'))
1836 close!
1837 delfunc Cconv3
1838 set charconvert&
Bram Moolenaarb340bae2020-06-15 19:51:56 +02001839endfunc
1840
1841" Test for editing a file without read permission
1842func Test_edit_file_no_read_perm()
1843 CheckUnix
Bram Moolenaar17709e22021-03-19 14:38:12 +01001844 CheckNotRoot
1845
Bram Moolenaar14f91762022-09-21 15:13:52 +01001846 call writefile(['one', 'two'], 'Xnrpfile', 'D')
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01001847 call setfperm('Xnrpfile', '-w-------')
Bram Moolenaarb340bae2020-06-15 19:51:56 +02001848 new
1849 redir => msg
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01001850 edit Xnrpfile
Bram Moolenaarb340bae2020-06-15 19:51:56 +02001851 redir END
1852 call assert_equal(1, &readonly)
1853 call assert_equal([''], getline(1, '$'))
1854 call assert_match('\[Permission Denied\]', msg)
1855 close!
Bram Moolenaarb340bae2020-06-15 19:51:56 +02001856endfunc
1857
zeertzjq3a56b6d2022-04-08 11:56:14 +01001858" Using :edit without leaving 'insertmode' should not cause Insert mode to be
1859" re-entered immediately after <C-L>
1860func Test_edit_insertmode_ex_edit()
1861 CheckRunVimInTerminal
1862
1863 let lines =<< trim END
1864 set insertmode noruler
1865 inoremap <C-B> <Cmd>edit Xfoo<CR>
1866 END
Bram Moolenaar14f91762022-09-21 15:13:52 +01001867 call writefile(lines, 'Xtest_edit_insertmode_ex_edit', 'D')
zeertzjq3a56b6d2022-04-08 11:56:14 +01001868
Bram Moolenaar14f91762022-09-21 15:13:52 +01001869 let buf = RunVimInTerminal('-S Xtest_edit_insertmode_ex_edit', #{rows: 6, wait_for_ruler: 0})
1870 " Somehow when using valgrind "INSERT" does not show up unless we send
1871 " something to the terminal.
1872 for i in range(30)
1873 if term_getline(buf, 6) =~ 'INSERT'
1874 break
1875 endif
1876 call term_sendkeys(buf, "-")
1877 sleep 100m
1878 endfor
Bram Moolenaarc5382b62022-06-19 15:22:36 +01001879 call WaitForAssert({-> assert_match('^-- INSERT --\s*$', term_getline(buf, 6))})
zeertzjq3a56b6d2022-04-08 11:56:14 +01001880 call term_sendkeys(buf, "\<C-B>\<C-L>")
Bram Moolenaarc5382b62022-06-19 15:22:36 +01001881 call WaitForAssert({-> assert_notmatch('^-- INSERT --\s*$', term_getline(buf, 6))})
zeertzjq3a56b6d2022-04-08 11:56:14 +01001882
1883 " clean up
1884 call StopVimInTerminal(buf)
zeertzjq3a56b6d2022-04-08 11:56:14 +01001885endfunc
1886
Bram Moolenaar845e0ee2020-06-20 16:05:32 +02001887" Pressing escape in 'insertmode' should beep
Bram Moolenaarc5382b62022-06-19 15:22:36 +01001888" FIXME: Execute this later, when using valgrind it makes the next test
1889" Test_edit_insertmode_ex_edit() fail.
1890func Test_z_edit_insertmode_esc_beeps()
Bram Moolenaar845e0ee2020-06-20 16:05:32 +02001891 new
1892 set insertmode
1893 call assert_beeps("call feedkeys(\"one\<Esc>\", 'xt')")
1894 set insertmode&
Bram Moolenaarc5382b62022-06-19 15:22:36 +01001895 " unsupported "CTRL-G l" command should beep in insert mode.
Bram Moolenaar845e0ee2020-06-20 16:05:32 +02001896 call assert_beeps("normal i\<C-G>l")
Bram Moolenaarc5382b62022-06-19 15:22:36 +01001897 bwipe!
Bram Moolenaar845e0ee2020-06-20 16:05:32 +02001898endfunc
1899
1900" Test for 'hkmap' and 'hkmapp'
1901func Test_edit_hkmap()
1902 CheckFeature rightleft
1903 if has('win32') && !has('gui')
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001904 throw 'Skipped: fails on the MS-Windows terminal version'
Bram Moolenaar845e0ee2020-06-20 16:05:32 +02001905 endif
1906 new
1907
1908 set revins hkmap
1909 let str = 'abcdefghijklmnopqrstuvwxyz'
1910 let str ..= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
1911 let str ..= '`/'',.;'
1912 call feedkeys('i' .. str, 'xt')
1913 let expected = "óõú,.;"
1914 let expected ..= "ZYXWVUTSRQPONMLKJIHGFEDCBA"
1915 let expected ..= "æèñ'äåàãø/ôíîöêìçïéòë÷âáðù"
1916 call assert_equal(expected, getline(1))
1917
1918 %d
1919 set revins hkmap hkmapp
1920 let str = 'abcdefghijklmnopqrstuvwxyz'
1921 let str ..= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
1922 call feedkeys('i' .. str, 'xt')
1923 let expected = "õYXWVUTSRQóOïíLKJIHGFEDêBA"
1924 let expected ..= "öòXùåèúæø'ôñðîì÷çéäâóǟãëáà"
1925 call assert_equal(expected, getline(1))
1926
1927 set revins& hkmap& hkmapp&
1928 close!
1929endfunc
1930
1931" Test for 'allowrevins' and using CTRL-_ in insert mode
1932func Test_edit_allowrevins()
1933 CheckFeature rightleft
1934 new
1935 set allowrevins
1936 call feedkeys("iABC\<C-_>DEF\<C-_>GHI", 'xt')
1937 call assert_equal('ABCFEDGHI', getline(1))
1938 set allowrevins&
1939 close!
1940endfunc
1941
1942" Test for inserting a register in insert mode using CTRL-R
1943func Test_edit_insert_reg()
1944 new
1945 let g:Line = ''
1946 func SaveFirstLine()
1947 let g:Line = Screenline(1)
1948 return 'r'
1949 endfunc
1950 inoremap <expr> <buffer> <F2> SaveFirstLine()
1951 call test_override('redraw_flag', 1)
1952 call test_override('char_avail', 1)
1953 let @r = 'sample'
1954 call feedkeys("a\<C-R>=SaveFirstLine()\<CR>", "xt")
1955 call assert_equal('"', g:Line)
1956 call test_override('ALL', 0)
1957 close!
1958endfunc
1959
1960" When a character is inserted at the last position of the last line in a
1961" window, the window contents should be scrolled one line up. If the top line
1962" is part of a fold, then the entire fold should be scrolled up.
1963func Test_edit_lastline_scroll()
1964 new
1965 let h = winheight(0)
1966 let lines = ['one', 'two', 'three']
1967 let lines += repeat(['vim'], h - 4)
1968 call setline(1, lines)
1969 call setline(h, repeat('x', winwidth(0) - 1))
1970 call feedkeys("GAx", 'xt')
1971 redraw!
1972 call assert_equal(h - 1, winline())
1973 call assert_equal(2, line('w0'))
1974
1975 " scroll with a fold
1976 1,2fold
1977 normal gg
1978 call setline(h + 1, repeat('x', winwidth(0) - 1))
1979 call feedkeys("GAx", 'xt')
1980 redraw!
1981 call assert_equal(h - 1, winline())
1982 call assert_equal(3, line('w0'))
1983
1984 close!
1985endfunc
1986
Bram Moolenaar21cbe172020-10-13 19:08:24 +02001987func Test_edit_browse()
1988 " in the GUI this opens a file picker, we only test the terminal behavior
1989 CheckNotGui
1990
1991 " ":browse xxx" checks for the FileExplorer augroup and assumes editing "."
1992 " works then.
1993 augroup FileExplorer
1994 au!
1995 augroup END
1996
1997 " When the USE_FNAME_CASE is defined this used to cause a crash.
1998 browse enew
1999 bwipe!
2000
2001 browse split
2002 bwipe!
2003endfunc
2004
Bram Moolenaarcaf73dc2020-10-26 21:39:13 +01002005func Test_read_invalid()
2006 set encoding=latin1
2007 " This was not properly checking for going past the end.
2008 call assert_fails('r`=', 'E484')
2009 set encoding=utf-8
2010endfunc
2011
Yegappan Lakshmanan59585492021-06-12 13:46:41 +02002012" Test for the 'revins' option
2013func Test_edit_revins()
2014 CheckFeature rightleft
2015 new
2016 set revins
2017 exe "normal! ione\ttwo three"
2018 call assert_equal("eerht owt\teno", getline(1))
2019 call setline(1, "one\ttwo three")
2020 normal! gg$bi a
2021 call assert_equal("one\ttwo a three", getline(1))
2022 exe "normal! $bi\<BS>\<BS>"
2023 call assert_equal("one\ttwo a ree", getline(1))
2024 exe "normal! 0wi\<C-W>"
2025 call assert_equal("one\t a ree", getline(1))
2026 exe "normal! 0wi\<C-U>"
2027 call assert_equal("one\t ", getline(1))
2028 " newline in insert mode starts at the end of the line
2029 call setline(1, 'one two three')
2030 exe "normal! wi\nfour"
2031 call assert_equal(['one two three', 'ruof'], getline(1, '$'))
2032 set revins&
2033 bw!
2034endfunc
2035
Bram Moolenaar35a9a002021-09-11 21:14:20 +02002036" Test for getting the character of the line below after "p"
2037func Test_edit_put_CTRL_E()
2038 set encoding=latin1
2039 new
2040 let @" = ''
2041 sil! norm orggRx
2042 sil! norm pr
2043 call assert_equal(['r', 'r'], getline(1, 2))
2044 bwipe!
2045 set encoding=utf-8
2046endfunc
2047
Dominique Pelle9cd063e2021-10-28 21:06:05 +01002048" Test toggling of input method. See :help i_CTRL-^
2049func Test_edit_CTRL_hat()
2050 CheckFeature xim
Dominique Pelle8753c1d2021-10-31 20:19:17 +00002051
Bram Moolenaar0b962e52022-04-03 18:02:37 +01002052 " FIXME: test fails with Motif GUI.
Dominique Pelle8753c1d2021-10-31 20:19:17 +00002053 " test also fails when running in the GUI.
2054 CheckFeature gui_gtk
2055 CheckNotGui
Dominique Pelle9cd063e2021-10-28 21:06:05 +01002056
2057 new
2058
2059 call assert_equal(0, &iminsert)
2060 call feedkeys("i\<C-^>", 'xt')
2061 call assert_equal(2, &iminsert)
2062 call feedkeys("i\<C-^>", 'xt')
2063 call assert_equal(0, &iminsert)
2064
2065 bwipe!
2066endfunc
2067
Bram Moolenaarde05bb22022-01-13 13:08:14 +00002068" Weird long file name was going over the end of NameBuff
2069func Test_edit_overlong_file_name()
2070 CheckUnix
2071
2072 file 0000000000000000000000000000
2073 file %%%%%%%%%%%%%%%%%%%%%%%%%%
2074 file %%%%%%
2075 set readonly
Bram Moolenaar94722c52023-01-28 19:19:03 +00002076 set ls=2
Bram Moolenaarde05bb22022-01-13 13:08:14 +00002077
2078 redraw!
2079 set noreadonly ls&
2080 bwipe!
2081endfunc
2082
Christian Brabandtdfbdadc2022-05-05 20:46:47 +01002083func Test_edit_shift_bs()
2084 CheckMSWindows
2085
2086 " FIXME: this works interactively, but the test fails
2087 throw 'Skipped: Shift-Backspace Test not working correctly :('
2088
2089 " Need to run this in Win32 Terminal, do not use CheckRunVimInTerminal
2090 if !has("terminal")
2091 return
2092 endif
2093
2094 " Shift Backspace should work like Backspace in insert mode
2095 let lines =<< trim END
2096 call setline(1, ['abc'])
2097 END
Bram Moolenaar14f91762022-09-21 15:13:52 +01002098 call writefile(lines, 'Xtest_edit_shift_bs', 'D')
Christian Brabandtdfbdadc2022-05-05 20:46:47 +01002099
2100 let buf = RunVimInTerminal('-S Xtest_edit_shift_bs', #{rows: 3})
2101 call term_sendkeys(buf, "A\<S-BS>-\<esc>")
2102 call TermWait(buf, 50)
2103 call assert_equal('ab-', term_getline(buf, 1))
2104
2105 " clean up
2106 call StopVimInTerminal(buf)
Christian Brabandtdfbdadc2022-05-05 20:46:47 +01002107endfunc
Dominique Pelle9cd063e2021-10-28 21:06:05 +01002108
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +01002109" vim: shiftwidth=2 sts=2 expandtab