blob: a70bc45d9cc196a9d439e4233d658e5a19fbec8f [file] [log] [blame]
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001" Test for edit functions
Bram Moolenaar215ba3b2019-11-06 15:07:07 +01002
Bram Moolenaareb992cb2017-03-09 18:20:16 +01003if exists("+t_kD")
4 let &t_kD="[3;*~"
5endif
Bram Moolenaareb992cb2017-03-09 18:20:16 +01006
Bram Moolenaar215ba3b2019-11-06 15:07:07 +01007source check.vim
8
Bram Moolenaareb992cb2017-03-09 18:20:16 +01009" Needed for testing basic rightleft: Test_edit_rightleft
10source view_util.vim
11
12" Needs to come first until the bug in getchar() is
13" fixed: https://groups.google.com/d/msg/vim_dev/fXL9yme4H4c/bOR-U6_bAQAJ
Bram Moolenaar1e115362019-01-09 23:01:02 +010014func Test_edit_00b()
Bram Moolenaareb992cb2017-03-09 18:20:16 +010015 new
16 call setline(1, ['abc '])
17 inoreabbr <buffer> h here some more
18 call cursor(1, 4)
19 " <c-l> expands the abbreviation and ends insertmode
20 call feedkeys(":set im\<cr> h\<c-l>:set noim\<cr>", 'tix')
21 call assert_equal(['abc here some more '], getline(1,'$'))
22 iunabbr <buffer> h
23 bw!
24endfunc
25
Bram Moolenaar1e115362019-01-09 23:01:02 +010026func Test_edit_01()
Bram Moolenaareb992cb2017-03-09 18:20:16 +010027 " set for Travis CI?
28 " set nocp noesckeys
29 new
Bram Moolenaareb992cb2017-03-09 18:20:16 +010030 " 1) empty buffer
31 call assert_equal([''], getline(1,'$'))
32 " 2) delete in an empty line
33 call feedkeys("i\<del>\<esc>", 'tnix')
34 call assert_equal([''], getline(1,'$'))
35 %d
36 " 3) delete one character
37 call setline(1, 'a')
38 call feedkeys("i\<del>\<esc>", 'tnix')
39 call assert_equal([''], getline(1,'$'))
40 %d
41 " 4) delete a multibyte character
Bram Moolenaar30276f22019-01-24 17:59:39 +010042 call setline(1, "\u0401")
43 call feedkeys("i\<del>\<esc>", 'tnix')
44 call assert_equal([''], getline(1,'$'))
45 %d
Bram Moolenaareb992cb2017-03-09 18:20:16 +010046 " 5.1) delete linebreak with 'bs' option containing eol
47 let _bs=&bs
48 set bs=eol
49 call setline(1, ["abc def", "ghi jkl"])
50 call cursor(1, 1)
51 call feedkeys("A\<del>\<esc>", 'tnix')
52 call assert_equal(['abc defghi jkl'], getline(1, 2))
53 %d
54 " 5.2) delete linebreak with backspace option w/out eol
55 set bs=
56 call setline(1, ["abc def", "ghi jkl"])
57 call cursor(1, 1)
58 call feedkeys("A\<del>\<esc>", 'tnix')
59 call assert_equal(["abc def", "ghi jkl"], getline(1, 2))
Bram Moolenaareb992cb2017-03-09 18:20:16 +010060 let &bs=_bs
61 bw!
62endfunc
63
Bram Moolenaar1e115362019-01-09 23:01:02 +010064func Test_edit_02()
Bram Moolenaareb992cb2017-03-09 18:20:16 +010065 " Change cursor position in InsertCharPre command
66 new
67 call setline(1, 'abc')
68 call cursor(1, 1)
69 fu! DoIt(...)
70 call cursor(1, 4)
71 if len(a:000)
72 let v:char=a:1
73 endif
74 endfu
75 au InsertCharPre <buffer> :call DoIt('y')
76 call feedkeys("ix\<esc>", 'tnix')
77 call assert_equal(['abcy'], getline(1, '$'))
78 " Setting <Enter> in InsertCharPre
79 au! InsertCharPre <buffer> :call DoIt("\n")
80 call setline(1, 'abc')
81 call cursor(1, 1)
82 call feedkeys("ix\<esc>", 'tnix')
83 call assert_equal(['abc', ''], getline(1, '$'))
84 %d
85 au! InsertCharPre
86 " Change cursor position in InsertEnter command
87 " 1) when setting v:char, keeps changed cursor position
88 au! InsertEnter <buffer> :call DoIt('y')
89 call setline(1, 'abc')
90 call cursor(1, 1)
91 call feedkeys("ix\<esc>", 'tnix')
92 call assert_equal(['abxc'], getline(1, '$'))
93 " 2) when not setting v:char, restores changed cursor position
94 au! InsertEnter <buffer> :call DoIt()
95 call setline(1, 'abc')
96 call cursor(1, 1)
97 call feedkeys("ix\<esc>", 'tnix')
98 call assert_equal(['xabc'], getline(1, '$'))
99 au! InsertEnter
100 delfu DoIt
101 bw!
102endfunc
103
Bram Moolenaar1e115362019-01-09 23:01:02 +0100104func Test_edit_03()
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100105 " Change cursor after <c-o> command to end of line
106 new
107 call setline(1, 'abc')
108 call cursor(1, 1)
109 call feedkeys("i\<c-o>$y\<esc>", 'tnix')
110 call assert_equal(['abcy'], getline(1, '$'))
111 %d
112 call setline(1, 'abc')
113 call cursor(1, 1)
114 call feedkeys("i\<c-o>80|y\<esc>", 'tnix')
115 call assert_equal(['abcy'], getline(1, '$'))
116 %d
117 call setline(1, 'abc')
118 call feedkeys("Ad\<c-o>:s/$/efg/\<cr>hij", 'tnix')
119 call assert_equal(['hijabcdefg'], getline(1, '$'))
120 bw!
121endfunc
122
Bram Moolenaar1e115362019-01-09 23:01:02 +0100123func Test_edit_04()
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100124 " test for :stopinsert
125 new
126 call setline(1, 'abc')
127 call cursor(1, 1)
128 call feedkeys("i\<c-o>:stopinsert\<cr>$", 'tnix')
129 call feedkeys("aX\<esc>", 'tnix')
130 call assert_equal(['abcX'], getline(1, '$'))
131 %d
132 bw!
133endfunc
134
Bram Moolenaar1e115362019-01-09 23:01:02 +0100135func Test_edit_05()
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100136 " test for folds being opened
137 new
138 call setline(1, ['abcX', 'abcX', 'zzzZ'])
139 call cursor(1, 1)
140 set foldmethod=manual foldopen+=insert
141 " create fold for those two lines
142 norm! Vjzf
143 call feedkeys("$ay\<esc>", 'tnix')
144 call assert_equal(['abcXy', 'abcX', 'zzzZ'], getline(1, '$'))
145 %d
146 call setline(1, ['abcX', 'abcX', 'zzzZ'])
147 call cursor(1, 1)
148 set foldmethod=manual foldopen-=insert
149 " create fold for those two lines
150 norm! Vjzf
151 call feedkeys("$ay\<esc>", 'tnix')
152 call assert_equal(['abcXy', 'abcX', 'zzzZ'], getline(1, '$'))
153 %d
154 bw!
155endfunc
156
Bram Moolenaar1e115362019-01-09 23:01:02 +0100157func Test_edit_06()
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100158 " Test in diff mode
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200159 CheckFeature diff
160 CheckExecutable diff
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100161 new
162 call setline(1, ['abc', 'xxx', 'yyy'])
163 vnew
164 call setline(1, ['abc', 'zzz', 'xxx', 'yyy'])
165 wincmd p
166 diffthis
167 wincmd p
168 diffthis
169 wincmd p
170 call cursor(2, 1)
171 norm! zt
172 call feedkeys("Ozzz\<esc>", 'tnix')
173 call assert_equal(['abc', 'zzz', 'xxx', 'yyy'], getline(1,'$'))
174 bw!
175 bw!
176endfunc
177
Bram Moolenaar1e115362019-01-09 23:01:02 +0100178func Test_edit_07()
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100179 " 1) Test with completion <c-l> when popupmenu is visible
180 new
181 call setline(1, 'J')
182
183 func! ListMonths()
184 call complete(col('.')-1, ['January', 'February', 'March',
185 \ 'April', 'May', 'June', 'July', 'August', 'September',
186 \ 'October', 'November', 'December'])
187 return ''
188 endfunc
189 inoremap <buffer> <F5> <C-R>=ListMonths()<CR>
190
191 call feedkeys("A\<f5>\<c-p>". repeat("\<down>", 6)."\<c-l>\<down>\<c-l>\<cr>", 'tx')
192 call assert_equal(['July'], getline(1,'$'))
193 " 1) Test completion when InsertCharPre kicks in
194 %d
195 call setline(1, 'J')
196 fu! DoIt()
197 if v:char=='u'
198 let v:char='an'
199 endif
200 endfu
201 au InsertCharPre <buffer> :call DoIt()
202 call feedkeys("A\<f5>\<c-p>u\<cr>\<c-l>\<cr>", 'tx')
Bram Moolenaar4c313b12019-08-24 22:58:31 +0200203 call assert_equal(["Jan\<c-l>",''], 1->getline('$'))
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100204 %d
205 call setline(1, 'J')
206 call feedkeys("A\<f5>\<c-p>u\<down>\<c-l>\<cr>", 'tx')
Bram Moolenaar4c313b12019-08-24 22:58:31 +0200207 call assert_equal(["January"], 1->getline('$'))
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100208
209 delfu ListMonths
210 delfu DoIt
211 iunmap <buffer> <f5>
212 bw!
213endfunc
214
Bram Moolenaar1e115362019-01-09 23:01:02 +0100215func Test_edit_08()
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100216 " reset insertmode from i_ctrl-r_=
Bram Moolenaar2a45d642017-10-27 01:35:00 +0200217 let g:bufnr = bufnr('%')
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100218 new
219 call setline(1, ['abc'])
220 call cursor(1, 4)
Bram Moolenaar2a45d642017-10-27 01:35:00 +0200221 call feedkeys(":set im\<cr>ZZZ\<c-r>=setbufvar(g:bufnr,'&im', 0)\<cr>",'tnix')
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100222 call assert_equal(['abZZZc'], getline(1,'$'))
223 call assert_equal([0, 1, 1, 0], getpos('.'))
224 call assert_false(0, '&im')
225 bw!
Bram Moolenaar2a45d642017-10-27 01:35:00 +0200226 unlet g:bufnr
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100227endfunc
228
Bram Moolenaar1e115362019-01-09 23:01:02 +0100229func Test_edit_09()
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100230 " test i_CTRL-\ combinations
231 new
232 call setline(1, ['abc', 'def', 'ghi'])
233 call cursor(1, 1)
234 " 1) CTRL-\ CTLR-N
235 call feedkeys(":set im\<cr>\<c-\>\<c-n>ccABC\<c-l>", 'txin')
236 call assert_equal(['ABC', 'def', 'ghi'], getline(1,'$'))
237 call setline(1, ['ABC', 'def', 'ghi'])
238 " 2) CTRL-\ CTLR-G
239 call feedkeys("j0\<c-\>\<c-g>ZZZ\<cr>\<c-l>", 'txin')
240 call assert_equal(['ABC', 'ZZZ', 'def', 'ghi'], getline(1,'$'))
241 call feedkeys("I\<c-\>\<c-g>YYY\<c-l>", 'txin')
242 call assert_equal(['ABC', 'ZZZ', 'YYYdef', 'ghi'], getline(1,'$'))
243 set noinsertmode
244 " 3) CTRL-\ CTRL-O
245 call setline(1, ['ABC', 'ZZZ', 'def', 'ghi'])
246 call cursor(1, 1)
247 call feedkeys("A\<c-o>ix", 'txin')
248 call assert_equal(['ABxC', 'ZZZ', 'def', 'ghi'], getline(1,'$'))
249 call feedkeys("A\<c-\>\<c-o>ix", 'txin')
250 call assert_equal(['ABxCx', 'ZZZ', 'def', 'ghi'], getline(1,'$'))
251 " 4) CTRL-\ a (should be inserted literally, not special after <c-\>
252 call setline(1, ['ABC', 'ZZZ', 'def', 'ghi'])
253 call cursor(1, 1)
254 call feedkeys("A\<c-\>a", 'txin')
255 call assert_equal(["ABC\<c-\>a", 'ZZZ', 'def', 'ghi'], getline(1, '$'))
256 bw!
257endfunc
258
Bram Moolenaar1e115362019-01-09 23:01:02 +0100259func Test_edit_11()
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100260 " Test that indenting kicks in
261 new
262 set cindent
263 call setline(1, ['{', '', ''])
264 call cursor(2, 1)
265 call feedkeys("i\<c-f>int c;\<esc>", 'tnix')
266 call cursor(3, 1)
Bram Moolenaar1671f442020-03-10 07:48:13 +0100267 call feedkeys("\<Insert>/* comment */", 'tnix')
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100268 call assert_equal(['{', "\<tab>int c;", "/* comment */"], getline(1, '$'))
269 " added changed cindentkeys slightly
270 set cindent cinkeys+=*/
271 call setline(1, ['{', '', ''])
272 call cursor(2, 1)
273 call feedkeys("i\<c-f>int c;\<esc>", 'tnix')
274 call cursor(3, 1)
275 call feedkeys("i/* comment */", 'tnix')
276 call assert_equal(['{', "\<tab>int c;", "\<tab>/* comment */"], getline(1, '$'))
277 set cindent cinkeys+==end
278 call feedkeys("oend\<cr>\<esc>", 'tnix')
279 call assert_equal(['{', "\<tab>int c;", "\<tab>/* comment */", "\tend", ''], getline(1, '$'))
280 set cinkeys-==end
281 %d
282 " Use indentexpr instead of cindenting
283 func! Do_Indent()
284 if v:lnum == 3
285 return 3*shiftwidth()
286 else
287 return 2*shiftwidth()
288 endif
289 endfunc
290 setl indentexpr=Do_Indent() indentkeys+=*/
291 call setline(1, ['{', '', ''])
292 call cursor(2, 1)
293 call feedkeys("i\<c-f>int c;\<esc>", 'tnix')
294 call cursor(3, 1)
295 call feedkeys("i/* comment */", 'tnix')
296 call assert_equal(['{', "\<tab>\<tab>int c;", "\<tab>\<tab>\<tab>/* comment */"], getline(1, '$'))
297 set cinkeys&vim indentkeys&vim
298 set nocindent indentexpr=
299 delfu Do_Indent
300 bw!
301endfunc
302
Bram Moolenaar1e115362019-01-09 23:01:02 +0100303func Test_edit_11_indentexpr()
Bram Moolenaar1b383442017-09-26 20:04:54 +0200304 " Test that indenting kicks in
305 new
306 " Use indentexpr instead of cindenting
307 func! Do_Indent()
308 let pline=prevnonblank(v:lnum)
309 if empty(getline(v:lnum))
310 if getline(pline) =~ 'if\|then'
311 return shiftwidth()
312 else
313 return 0
314 endif
315 else
316 return 0
317 endif
318 endfunc
319 setl indentexpr=Do_Indent() indentkeys+=0=then,0=fi
320 call setline(1, ['if [ $this ]'])
321 call cursor(1, 1)
322 call feedkeys("othen\<cr>that\<cr>fi", 'tnix')
323 call assert_equal(['if [ $this ]', "then", "\<tab>that", "fi"], getline(1, '$'))
324 set cinkeys&vim indentkeys&vim
325 set nocindent indentexpr=
326 delfu Do_Indent
Yegappan Lakshmanan8bb65f22021-12-26 10:51:39 +0000327
328 " Using a script-local function
329 func s:NewIndentExpr()
330 endfunc
331 set indentexpr=s:NewIndentExpr()
332 call assert_equal(expand('<SID>') .. 'NewIndentExpr()', &indentexpr)
333 set indentexpr=<SID>NewIndentExpr()
334 call assert_equal(expand('<SID>') .. 'NewIndentExpr()', &indentexpr)
335 set indentexpr&
336
Bram Moolenaar1b383442017-09-26 20:04:54 +0200337 bw!
338endfunc
339
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +0200340" Test changing indent in replace mode
Bram Moolenaar1e115362019-01-09 23:01:02 +0100341func Test_edit_12()
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100342 new
343 call setline(1, ["\tabc", "\tdef"])
344 call cursor(2, 4)
345 call feedkeys("R^\<c-d>", 'tnix')
346 call assert_equal(["\tabc", "def"], getline(1, '$'))
Bram Moolenaar4c313b12019-08-24 22:58:31 +0200347 call assert_equal([0, 2, 2, 0], '.'->getpos())
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100348 %d
349 call setline(1, ["\tabc", "\t\tdef"])
350 call cursor(2, 2)
351 call feedkeys("R^\<c-d>", 'tnix')
352 call assert_equal(["\tabc", "def"], getline(1, '$'))
353 call assert_equal([0, 2, 1, 0], getpos('.'))
354 %d
355 call setline(1, ["\tabc", "\t\tdef"])
356 call cursor(2, 2)
357 call feedkeys("R\<c-t>", 'tnix')
358 call assert_equal(["\tabc", "\t\t\tdef"], getline(1, '$'))
359 call assert_equal([0, 2, 2, 0], getpos('.'))
360 bw!
361 10vnew
362 call setline(1, ["\tabc", "\t\tdef"])
363 call cursor(2, 2)
364 call feedkeys("R\<c-t>", 'tnix')
365 call assert_equal(["\tabc", "\t\t\tdef"], getline(1, '$'))
366 call assert_equal([0, 2, 2, 0], getpos('.'))
367 %d
368 set sw=4
369 call setline(1, ["\tabc", "\t\tdef"])
370 call cursor(2, 2)
371 call feedkeys("R\<c-t>\<c-t>", 'tnix')
372 call assert_equal(["\tabc", "\t\t\tdef"], getline(1, '$'))
373 call assert_equal([0, 2, 2, 0], getpos('.'))
374 %d
375 call setline(1, ["\tabc", "\t\tdef"])
376 call cursor(2, 2)
377 call feedkeys("R\<c-t>\<c-t>", 'tnix')
378 call assert_equal(["\tabc", "\t\t\tdef"], getline(1, '$'))
379 call assert_equal([0, 2, 2, 0], getpos('.'))
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +0200380 set sw&
381
382 " In replace mode, after hitting enter in a line with tab characters,
383 " pressing backspace should restore the tab characters.
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100384 %d
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +0200385 setlocal autoindent backspace=2
386 call setline(1, "\tone\t\ttwo")
387 exe "normal ggRred\<CR>six" .. repeat("\<BS>", 8)
388 call assert_equal(["\tone\t\ttwo"], getline(1, '$'))
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100389 bw!
390endfunc
391
Bram Moolenaar1e115362019-01-09 23:01:02 +0100392func Test_edit_13()
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100393 " Test smartindenting
Bram Moolenaar8e145b82022-05-21 20:17:31 +0100394 new
395 set smartindent autoindent
396 call setline(1, ["\tabc"])
397 call feedkeys("A {\<cr>more\<cr>}\<esc>", 'tnix')
398 call assert_equal(["\tabc {", "\t\tmore", "\t}"], getline(1, '$'))
399 set smartindent& autoindent&
400 bwipe!
Bram Moolenaar2ba42382019-03-16 18:11:07 +0100401
402 " Test autoindent removing indent of blank line.
403 new
404 call setline(1, ' foo bar baz')
405 set autoindent
406 exe "normal 0eea\<CR>\<CR>\<Esc>"
407 call assert_equal(" foo bar", getline(1))
408 call assert_equal("", getline(2))
409 call assert_equal(" baz", getline(3))
410 set autoindent&
Bram Moolenaar845e0ee2020-06-20 16:05:32 +0200411
412 " pressing <C-U> to erase line should keep the indent with 'autoindent'
413 set backspace=2 autoindent
414 %d
415 exe "normal i\tone\<CR>three\<C-U>two"
416 call assert_equal(["\tone", "\ttwo"], getline(1, '$'))
417 set backspace& autoindent&
418
Bram Moolenaar2ba42382019-03-16 18:11:07 +0100419 bwipe!
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100420endfunc
421
Bram Moolenaar1f448d92021-03-22 19:37:06 +0100422" Test for autoindent removing indent when insert mode is stopped. Some parts
423" of the code is exercised only when interactive mode is used. So use Vim in a
424" terminal.
425func Test_autoindent_remove_indent()
426 CheckRunVimInTerminal
Bram Moolenaar61abe7d2022-08-30 21:46:08 +0100427 let buf = RunVimInTerminal('-N Xarifile', {'rows': 6, 'cols' : 20})
Bram Moolenaar1f448d92021-03-22 19:37:06 +0100428 call TermWait(buf)
429 call term_sendkeys(buf, ":set autoindent\n")
430 " leaving insert mode in a new line with indent added by autoindent, should
431 " remove the indent.
432 call term_sendkeys(buf, "i\<Tab>foo\<CR>\<Esc>")
Dominique Pelle923dce22021-11-21 11:36:04 +0000433 " Need to delay for some time, otherwise the code in getchar.c will not be
Bram Moolenaar1f448d92021-03-22 19:37:06 +0100434 " exercised.
435 call TermWait(buf, 50)
436 " when a line is wrapped and the cursor is at the start of the second line,
437 " leaving insert mode, should move the cursor back to the first line.
438 call term_sendkeys(buf, "o" .. repeat('x', 20) .. "\<Esc>")
Dominique Pelle923dce22021-11-21 11:36:04 +0000439 " Need to delay for some time, otherwise the code in getchar.c will not be
Bram Moolenaar1f448d92021-03-22 19:37:06 +0100440 " exercised.
441 call TermWait(buf, 50)
442 call term_sendkeys(buf, ":w\n")
443 call TermWait(buf)
444 call StopVimInTerminal(buf)
Bram Moolenaar61abe7d2022-08-30 21:46:08 +0100445 call assert_equal(["\tfoo", '', repeat('x', 20)], readfile('Xarifile'))
446 call delete('Xarifile')
Bram Moolenaar1f448d92021-03-22 19:37:06 +0100447endfunc
448
Bram Moolenaar1e115362019-01-09 23:01:02 +0100449func Test_edit_CR()
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100450 " Test for <CR> in insert mode
Dominique Pelle923dce22021-11-21 11:36:04 +0000451 " basically only in quickfix mode it's tested, the rest
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100452 " has been taken care of by other tests
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200453 CheckFeature quickfix
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100454 botright new
Bram Moolenaar14f91762022-09-21 15:13:52 +0100455 call writefile(range(1, 10), 'Xqflist.txt', 'D')
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100456 call setqflist([{'filename': 'Xqflist.txt', 'lnum': 2}])
457 copen
458 set modifiable
459 call feedkeys("A\<cr>", 'tnix')
460 call assert_equal('Xqflist.txt', bufname(''))
461 call assert_equal(2, line('.'))
462 cclose
463 botright new
464 call setloclist(0, [{'filename': 'Xqflist.txt', 'lnum': 10}])
465 lopen
466 set modifiable
467 call feedkeys("A\<cr>", 'tnix')
468 call assert_equal('Xqflist.txt', bufname(''))
469 call assert_equal(10, line('.'))
470 call feedkeys("A\<Enter>", 'tnix')
471 call feedkeys("A\<kEnter>", 'tnix')
472 call feedkeys("A\n", 'tnix')
473 call feedkeys("A\r", 'tnix')
474 call assert_equal(map(range(1, 10), 'string(v:val)') + ['', '', '', ''], getline(1, '$'))
Bram Moolenaar14f91762022-09-21 15:13:52 +0100475
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100476 bw!
477 lclose
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100478endfunc
479
Bram Moolenaar1e115362019-01-09 23:01:02 +0100480func Test_edit_CTRL_()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200481 CheckFeature rightleft
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100482 " disabled for Windows builds, why?
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200483 CheckNotMSWindows
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100484 let _encoding=&encoding
485 set encoding=utf-8
486 " Test for CTRL-_
487 new
488 call setline(1, ['abc'])
489 call cursor(1, 1)
490 call feedkeys("i\<c-_>xyz\<esc>", 'tnix')
491 call assert_equal(["\<C-_>xyzabc"], getline(1, '$'))
492 call assert_false(&revins)
493 set ari
494 call setline(1, ['abc'])
495 call cursor(1, 1)
496 call feedkeys("i\<c-_>xyz\<esc>", 'tnix')
497 call assert_equal(["æèñabc"], getline(1, '$'))
498 call assert_true(&revins)
499 call setline(1, ['abc'])
500 call cursor(1, 1)
501 call feedkeys("i\<c-_>xyz\<esc>", 'tnix')
502 call assert_equal(["xyzabc"], getline(1, '$'))
503 call assert_false(&revins)
504 set noari
505 let &encoding=_encoding
506 bw!
507endfunc
508
509" needs to come first, to have the @. register empty
Bram Moolenaar1e115362019-01-09 23:01:02 +0100510func Test_edit_00a_CTRL_A()
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100511 " Test pressing CTRL-A
512 new
513 call setline(1, repeat([''], 5))
514 call cursor(1, 1)
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100515 try
516 call feedkeys("A\<NUL>", 'tnix')
517 catch /^Vim\%((\a\+)\)\=:E29/
518 call assert_true(1, 'E29 error caught')
519 endtry
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100520 call cursor(1, 1)
521 call feedkeys("Afoobar \<esc>", 'tnix')
522 call cursor(2, 1)
523 call feedkeys("A\<c-a>more\<esc>", 'tnix')
524 call cursor(3, 1)
525 call feedkeys("A\<NUL>and more\<esc>", 'tnix')
526 call assert_equal(['foobar ', 'foobar more', 'foobar morend more', '', ''], getline(1, '$'))
527 bw!
528endfunc
529
Bram Moolenaar1e115362019-01-09 23:01:02 +0100530func Test_edit_CTRL_EY()
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100531 " Ctrl-E/ Ctrl-Y in insert mode completion to scroll
532 10new
533 call setline(1, range(1, 100))
534 call cursor(30, 1)
535 norm! z.
536 call feedkeys("A\<c-x>\<c-e>\<c-e>\<c-e>\<c-e>\<c-e>", 'tnix')
537 call assert_equal(30, winsaveview()['topline'])
538 call assert_equal([0, 30, 2, 0], getpos('.'))
539 call feedkeys("A\<c-x>\<c-e>\<c-e>\<c-e>\<c-e>\<c-e>", 'tnix')
540 call feedkeys("A\<c-x>".repeat("\<c-y>", 10), 'tnix')
541 call assert_equal(21, winsaveview()['topline'])
542 call assert_equal([0, 30, 2, 0], getpos('.'))
543 bw!
544endfunc
545
Bram Moolenaar1e115362019-01-09 23:01:02 +0100546func Test_edit_CTRL_G()
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100547 new
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100548 call setline(1, ['foobar', 'foobar', 'foobar'])
549 call cursor(2, 4)
550 call feedkeys("ioooooooo\<c-g>k\<c-r>.\<esc>", 'tnix')
551 call assert_equal(['foooooooooobar', 'foooooooooobar', 'foobar'], getline(1, '$'))
552 call assert_equal([0, 1, 11, 0], getpos('.'))
553 call feedkeys("i\<c-g>k\<esc>", 'tnix')
554 call assert_equal([0, 1, 10, 0], getpos('.'))
555 call cursor(2, 4)
556 call feedkeys("i\<c-g>jzzzz\<esc>", 'tnix')
557 call assert_equal(['foooooooooobar', 'foooooooooobar', 'foozzzzbar'], getline(1, '$'))
558 call assert_equal([0, 3, 7, 0], getpos('.'))
559 call feedkeys("i\<c-g>j\<esc>", 'tnix')
560 call assert_equal([0, 3, 6, 0], getpos('.'))
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100561 bw!
562endfunc
563
Bram Moolenaar1e115362019-01-09 23:01:02 +0100564func Test_edit_CTRL_I()
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100565 " Tab in completion mode
566 let path=expand("%:p:h")
567 new
Bram Moolenaarca851592018-06-06 21:04:07 +0200568 call setline(1, [path. "/", ''])
Bram Moolenaarc5379472017-03-16 22:38:00 +0100569 call feedkeys("Arunt\<c-x>\<c-f>\<tab>\<cr>\<esc>", 'tnix')
570 call assert_match('runtest\.vim', getline(1))
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100571 %d
Bram Moolenaar14f91762022-09-21 15:13:52 +0100572 call writefile(['one', 'two', 'three'], 'Xinclude.txt', 'D')
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100573 let include='#include Xinclude.txt'
574 call setline(1, [include, ''])
575 call cursor(2, 1)
576 call feedkeys("A\<c-x>\<tab>\<cr>\<esc>", 'tnix')
577 call assert_equal([include, 'one', ''], getline(1, '$'))
578 call feedkeys("2ggC\<c-x>\<tab>\<down>\<cr>\<esc>", 'tnix')
579 call assert_equal([include, 'two', ''], getline(1, '$'))
580 call feedkeys("2ggC\<c-x>\<tab>\<down>\<down>\<cr>\<esc>", 'tnix')
581 call assert_equal([include, 'three', ''], getline(1, '$'))
582 call feedkeys("2ggC\<c-x>\<tab>\<down>\<down>\<down>\<cr>\<esc>", 'tnix')
583 call assert_equal([include, '', ''], getline(1, '$'))
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100584 bw!
585endfunc
586
Bram Moolenaar1e115362019-01-09 23:01:02 +0100587func Test_edit_CTRL_K()
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100588 " Test pressing CTRL-K (basically only dictionary completion and digraphs
589 " the rest is already covered
Bram Moolenaar14f91762022-09-21 15:13:52 +0100590 call writefile(['A', 'AA', 'AAA', 'AAAA'], 'Xdictionary.txt', 'D')
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100591 set dictionary=Xdictionary.txt
592 new
593 call setline(1, 'A')
594 call cursor(1, 1)
595 call feedkeys("A\<c-x>\<c-k>\<cr>\<esc>", 'tnix')
596 call assert_equal(['AA', ''], getline(1, '$'))
597 %d
598 call setline(1, 'A')
599 call cursor(1, 1)
600 call feedkeys("A\<c-x>\<c-k>\<down>\<cr>\<esc>", 'tnix')
601 call assert_equal(['AAA'], getline(1, '$'))
602 %d
603 call setline(1, 'A')
604 call cursor(1, 1)
605 call feedkeys("A\<c-x>\<c-k>\<down>\<down>\<cr>\<esc>", 'tnix')
606 call assert_equal(['AAAA'], getline(1, '$'))
607 %d
608 call setline(1, 'A')
609 call cursor(1, 1)
610 call feedkeys("A\<c-x>\<c-k>\<down>\<down>\<down>\<cr>\<esc>", 'tnix')
611 call assert_equal(['A'], getline(1, '$'))
612 %d
613 call setline(1, 'A')
614 call cursor(1, 1)
615 call feedkeys("A\<c-x>\<c-k>\<down>\<down>\<down>\<down>\<cr>\<esc>", 'tnix')
616 call assert_equal(['AA'], getline(1, '$'))
617
Bram Moolenaar4b96df52020-01-26 22:00:26 +0100618 " press an unexpected key after dictionary completion
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100619 %d
620 call setline(1, 'A')
621 call cursor(1, 1)
622 call feedkeys("A\<c-x>\<c-k>\<c-]>\<cr>\<esc>", 'tnix')
623 call assert_equal(['AA', ''], getline(1, '$'))
624 %d
625 call setline(1, 'A')
626 call cursor(1, 1)
627 call feedkeys("A\<c-x>\<c-k>\<c-s>\<cr>\<esc>", 'tnix')
628 call assert_equal(["AA\<c-s>", ''], getline(1, '$'))
629 %d
630 call setline(1, 'A')
631 call cursor(1, 1)
632 call feedkeys("A\<c-x>\<c-k>\<c-f>\<cr>\<esc>", 'tnix')
633 call assert_equal(["AA\<c-f>", ''], getline(1, '$'))
634
635 set dictionary=
636 %d
637 call setline(1, 'A')
638 call cursor(1, 1)
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100639 let v:testing = 1
640 try
641 call feedkeys("A\<c-x>\<c-k>\<esc>", 'tnix')
642 catch
643 " error sleeps 2 seconds, when v:testing is not set
644 let v:testing = 0
645 endtry
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100646
Bram Moolenaar30276f22019-01-24 17:59:39 +0100647 call test_override("char_avail", 1)
648 set showcmd
649 %d
650 call feedkeys("A\<c-k>a:\<esc>", 'tnix')
651 call assert_equal(['ä'], getline(1, '$'))
652 call test_override("char_avail", 0)
653 set noshowcmd
654
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100655 bw!
656endfunc
657
Bram Moolenaar1e115362019-01-09 23:01:02 +0100658func Test_edit_CTRL_L()
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100659 " Test Ctrl-X Ctrl-L (line completion)
660 new
661 set complete=.
662 call setline(1, ['one', 'two', 'three', '', '', '', ''])
663 call cursor(4, 1)
664 call feedkeys("A\<c-x>\<c-l>\<esc>", 'tnix')
665 call assert_equal(['one', 'two', 'three', 'three', '', '', ''], getline(1, '$'))
666 call feedkeys("cct\<c-x>\<c-l>\<c-n>\<esc>", 'tnix')
667 call assert_equal(['one', 'two', 'three', 't', '', '', ''], getline(1, '$'))
668 call feedkeys("cct\<c-x>\<c-l>\<c-n>\<c-n>\<esc>", 'tnix')
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100669 call assert_equal(['one', 'two', 'three', 'two', '', '', ''], getline(1, '$'))
Bram Moolenaar02ae9b42018-02-09 15:06:02 +0100670 call feedkeys("cct\<c-x>\<c-l>\<c-n>\<c-n>\<c-n>\<esc>", 'tnix')
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100671 call assert_equal(['one', 'two', 'three', 'three', '', '', ''], getline(1, '$'))
Bram Moolenaar02ae9b42018-02-09 15:06:02 +0100672 call feedkeys("cct\<c-x>\<c-l>\<c-n>\<c-n>\<c-n>\<c-n>\<esc>", 'tnix')
673 call assert_equal(['one', 'two', 'three', 't', '', '', ''], getline(1, '$'))
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100674 call feedkeys("cct\<c-x>\<c-l>\<c-p>\<esc>", 'tnix')
675 call assert_equal(['one', 'two', 'three', 'two', '', '', ''], getline(1, '$'))
676 call feedkeys("cct\<c-x>\<c-l>\<c-p>\<c-p>\<esc>", 'tnix')
677 call assert_equal(['one', 'two', 'three', 't', '', '', ''], getline(1, '$'))
678 call feedkeys("cct\<c-x>\<c-l>\<c-p>\<c-p>\<c-p>\<esc>", 'tnix')
679 call assert_equal(['one', 'two', 'three', 'three', '', '', ''], getline(1, '$'))
680 set complete=
681 call cursor(5, 1)
682 call feedkeys("A\<c-x>\<c-l>\<c-p>\<c-n>\<esc>", 'tnix')
683 call assert_equal(['one', 'two', 'three', 'three', "\<c-l>\<c-p>\<c-n>", '', ''], getline(1, '$'))
684 set complete&
685 %d
686 if has("conceal") && has("syntax")
687 call setline(1, ['foo', 'bar', 'foobar'])
688 call test_override("char_avail", 1)
689 set conceallevel=2 concealcursor=n
690 syn on
691 syn match ErrorMsg "^bar"
692 call matchadd("Conceal", 'oo', 10, -1, {'conceal': 'X'})
693 func! DoIt()
694 let g:change=1
695 endfunc
696 au! TextChangedI <buffer> :call DoIt()
697
698 call cursor(2, 1)
699 call assert_false(exists("g:change"))
700 call feedkeys("A \<esc>", 'tnix')
701 call assert_equal(['foo', 'bar ', 'foobar'], getline(1, '$'))
702 call assert_equal(1, g:change)
703
704 call test_override("char_avail", 0)
705 call clearmatches()
706 syn off
707 au! TextChangedI
708 delfu DoIt
709 unlet! g:change
710 endif
711 bw!
712endfunc
713
Bram Moolenaar1e115362019-01-09 23:01:02 +0100714func Test_edit_CTRL_N()
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100715 " Check keyword completion
Bram Moolenaara1070ea2021-02-20 19:21:36 +0100716 for e in ['latin1', 'utf-8']
717 exe 'set encoding=' .. e
718 new
719 set complete=.
720 call setline(1, ['INFER', 'loWER', '', '', ])
721 call cursor(3, 1)
722 call feedkeys("Ai\<c-n>\<cr>\<esc>", "tnix")
723 call feedkeys("ILO\<c-n>\<cr>\<esc>", 'tnix')
724 call assert_equal(['INFER', 'loWER', 'i', 'LO', '', ''], getline(1, '$'), e)
725 %d
726 call setline(1, ['INFER', 'loWER', '', '', ])
727 call cursor(3, 1)
728 set ignorecase infercase
729 call feedkeys("Ii\<c-n>\<cr>\<esc>", "tnix")
730 call feedkeys("ILO\<c-n>\<cr>\<esc>", 'tnix')
731 call assert_equal(['INFER', 'loWER', 'infer', 'LOWER', '', ''], getline(1, '$'), e)
Yegappan Lakshmanane9825862022-01-03 11:03:48 +0000732 set noignorecase noinfercase
733 %d
734 call setline(1, ['one word', 'two word'])
735 exe "normal! Goo\<C-P>\<C-X>\<C-P>"
736 call assert_equal('one word', getline(3))
737 %d
738 set complete&
Bram Moolenaara1070ea2021-02-20 19:21:36 +0100739 bw!
740 endfor
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100741endfunc
742
Bram Moolenaar1e115362019-01-09 23:01:02 +0100743func Test_edit_CTRL_O()
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100744 " Check for CTRL-O in insert mode
745 new
746 inoreabbr <buffer> h here some more
747 call setline(1, ['abc', 'def'])
748 call cursor(1, 1)
749 " Ctrl-O after an abbreviation
750 exe "norm A h\<c-o>:set nu\<cr> text"
751 call assert_equal(['abc here some more text', 'def'], getline(1, '$'))
752 call assert_true(&nu)
753 set nonu
754 iunabbr <buffer> h
755 " Ctrl-O at end of line with 've'=onemore
756 call cursor(1, 1)
757 call feedkeys("A\<c-o>:let g:a=getpos('.')\<cr>\<esc>", 'tnix')
758 call assert_equal([0, 1, 23, 0], g:a)
759 call cursor(1, 1)
760 set ve=onemore
761 call feedkeys("A\<c-o>:let g:a=getpos('.')\<cr>\<esc>", 'tnix')
762 call assert_equal([0, 1, 24, 0], g:a)
763 set ve=
764 unlet! g:a
765 bw!
766endfunc
767
Bram Moolenaar1e115362019-01-09 23:01:02 +0100768func Test_edit_CTRL_R()
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100769 " Insert Register
770 new
771 call test_override("ALL", 1)
772 set showcmd
773 call feedkeys("AFOOBAR eins zwei\<esc>", 'tnix')
774 call feedkeys("O\<c-r>.", 'tnix')
775 call feedkeys("O\<c-r>=10*500\<cr>\<esc>", 'tnix')
776 call feedkeys("O\<c-r>=getreg('=', 1)\<cr>\<esc>", 'tnix')
777 call assert_equal(["getreg('=', 1)", '5000', "FOOBAR eins zwei", "FOOBAR eins zwei"], getline(1, '$'))
778 call test_override("ALL", 0)
779 set noshowcmd
780 bw!
781endfunc
782
Bram Moolenaar1e115362019-01-09 23:01:02 +0100783func Test_edit_CTRL_S()
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100784 " Test pressing CTRL-S (basically only spellfile completion)
785 " the rest is already covered
786 new
787 if !has("spell")
788 call setline(1, 'vim')
789 call feedkeys("A\<c-x>ss\<cr>\<esc>", 'tnix')
790 call assert_equal(['vims', ''], getline(1, '$'))
791 bw!
792 return
793 endif
794 call setline(1, 'vim')
795 " spell option not yet set
796 try
797 call feedkeys("A\<c-x>\<c-s>\<cr>\<esc>", 'tnix')
798 catch /^Vim\%((\a\+)\)\=:E756/
799 call assert_true(1, 'error caught')
800 endtry
801 call assert_equal(['vim', ''], getline(1, '$'))
802 %d
803 setl spell spelllang=en
804 call setline(1, 'vim')
805 call cursor(1, 1)
806 call feedkeys("A\<c-x>\<c-s>\<cr>\<esc>", 'tnix')
807 call assert_equal(['Vim', ''], getline(1, '$'))
808 %d
809 call setline(1, 'vim')
810 call cursor(1, 1)
811 call feedkeys("A\<c-x>\<c-s>\<down>\<cr>\<esc>", 'tnix')
812 call assert_equal(['Aim'], getline(1, '$'))
813 %d
814 call setline(1, 'vim')
815 call cursor(1, 1)
816 call feedkeys("A\<c-x>\<c-s>\<c-p>\<cr>\<esc>", 'tnix')
817 call assert_equal(['vim', ''], getline(1, '$'))
818 %d
819 " empty buffer
820 call cursor(1, 1)
821 call feedkeys("A\<c-x>\<c-s>\<c-p>\<cr>\<esc>", 'tnix')
822 call assert_equal(['', ''], getline(1, '$'))
823 setl nospell
824 bw!
825endfunc
826
Bram Moolenaar1e115362019-01-09 23:01:02 +0100827func Test_edit_CTRL_T()
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100828 " Check for CTRL-T and CTRL-X CTRL-T in insert mode
829 " 1) increase indent
830 new
831 call setline(1, "abc")
832 call cursor(1, 1)
833 call feedkeys("A\<c-t>xyz", 'tnix')
834 call assert_equal(["\<tab>abcxyz"], getline(1, '$'))
835 " 2) also when paste option is set
836 set paste
837 call setline(1, "abc")
838 call cursor(1, 1)
839 call feedkeys("A\<c-t>xyz", 'tnix')
840 call assert_equal(["\<tab>abcxyz"], getline(1, '$'))
841 set nopaste
842 " CTRL-X CTRL-T (thesaurus complete)
Bram Moolenaar14f91762022-09-21 15:13:52 +0100843 call writefile(['angry furious mad enraged'], 'Xthesaurus', 'D')
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100844 set thesaurus=Xthesaurus
845 call setline(1, 'mad')
846 call cursor(1, 1)
847 call feedkeys("A\<c-x>\<c-t>\<cr>\<esc>", 'tnix')
848 call assert_equal(['mad', ''], getline(1, '$'))
849 %d
850 call setline(1, 'mad')
851 call cursor(1, 1)
852 call feedkeys("A\<c-x>\<c-t>\<c-n>\<cr>\<esc>", 'tnix')
853 call assert_equal(['angry', ''], getline(1, '$'))
854 %d
855 call setline(1, 'mad')
856 call cursor(1, 1)
857 call feedkeys("A\<c-x>\<c-t>\<c-n>\<c-n>\<cr>\<esc>", 'tnix')
858 call assert_equal(['furious', ''], getline(1, '$'))
859 %d
860 call setline(1, 'mad')
861 call cursor(1, 1)
862 call feedkeys("A\<c-x>\<c-t>\<c-n>\<c-n>\<c-n>\<cr>\<esc>", 'tnix')
863 call assert_equal(['enraged', ''], getline(1, '$'))
864 %d
865 call setline(1, 'mad')
866 call cursor(1, 1)
867 call feedkeys("A\<c-x>\<c-t>\<c-n>\<c-n>\<c-n>\<c-n>\<cr>\<esc>", 'tnix')
868 call assert_equal(['mad', ''], 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>\<c-n>\<c-n>\<c-n>\<cr>\<esc>", 'tnix')
873 call assert_equal(['mad', ''], getline(1, '$'))
874 " Using <c-p> <c-n> when 'complete' is empty
875 set complete=
876 %d
877 call setline(1, 'mad')
878 call cursor(1, 1)
879 call feedkeys("A\<c-x>\<c-t>\<c-n>\<cr>\<esc>", 'tnix')
880 call assert_equal(['angry', ''], getline(1, '$'))
881 %d
882 call setline(1, 'mad')
883 call cursor(1, 1)
884 call feedkeys("A\<c-x>\<c-t>\<c-p>\<cr>\<esc>", 'tnix')
885 call assert_equal(['mad', ''], getline(1, '$'))
886 set complete&
887
888 set thesaurus=
889 %d
890 call setline(1, 'mad')
891 call cursor(1, 1)
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100892 let v:testing = 1
893 try
894 call feedkeys("A\<c-x>\<c-t>\<esc>", 'tnix')
895 catch
896 " error sleeps 2 seconds, when v:testing is not set
897 let v:testing = 0
898 endtry
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100899 call assert_equal(['mad'], getline(1, '$'))
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100900 bw!
901endfunc
902
Yegappan Lakshmanane9825862022-01-03 11:03:48 +0000903" Test thesaurus completion with different encodings
904func Test_thesaurus_complete_with_encoding()
Bram Moolenaar14f91762022-09-21 15:13:52 +0100905 call writefile(['angry furious mad enraged'], 'Xthesaurus', 'D')
Yegappan Lakshmanane9825862022-01-03 11:03:48 +0000906 set thesaurus=Xthesaurus
907 for e in ['latin1', 'utf-8']
908 exe 'set encoding=' .. e
909 new
910 call setline(1, 'mad')
911 call cursor(1, 1)
912 call feedkeys("A\<c-x>\<c-t>\<cr>\<esc>", 'tnix')
913 call assert_equal(['mad', ''], getline(1, '$'))
914 bw!
915 endfor
916 set thesaurus=
Yegappan Lakshmanane9825862022-01-03 11:03:48 +0000917endfunc
918
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +0100919" Test 'thesaurusfunc'
920func MyThesaurus(findstart, base)
921 let mythesaurus = [
922 \ #{word: "happy",
923 \ synonyms: "cheerful,blissful,flying high,looking good,peppy"},
924 \ #{word: "kind",
925 \ synonyms: "amiable,bleeding-heart,heart in right place"}]
926 if a:findstart
927 " locate the start of the word
928 let line = getline('.')
929 let start = col('.') - 1
930 while start > 0 && line[start - 1] =~ '\a'
931 let start -= 1
932 endwhile
933 return start
934 else
935 " find strings matching with "a:base"
936 let res = []
937 for w in mythesaurus
938 if w.word =~ '^' . a:base
939 call add(res, w.word)
940 call extend(res, split(w.synonyms, ","))
941 endif
942 endfor
943 return res
944 endif
945endfunc
946
947func Test_thesaurus_func()
948 new
Bram Moolenaarf4d8b762021-10-17 14:13:09 +0100949 set thesaurus=notused
950 set thesaurusfunc=NotUsed
951 setlocal thesaurusfunc=MyThesaurus
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +0100952 call setline(1, "an ki")
953 call cursor(1, 1)
954 call feedkeys("A\<c-x>\<c-t>\<c-n>\<cr>\<esc>", 'tnix')
955 call assert_equal(['an amiable', ''], getline(1, '$'))
Bram Moolenaarf4d8b762021-10-17 14:13:09 +0100956
957 setlocal thesaurusfunc=NonExistingFunc
958 call assert_fails("normal $a\<C-X>\<C-T>", 'E117:')
959
960 setlocal thesaurusfunc=
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +0100961 set thesaurusfunc=NonExistingFunc
962 call assert_fails("normal $a\<C-X>\<C-T>", 'E117:')
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +0100963 %bw!
Bram Moolenaarf4d8b762021-10-17 14:13:09 +0100964
965 set thesaurusfunc=
966 set thesaurus=
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +0100967endfunc
968
Bram Moolenaar1e115362019-01-09 23:01:02 +0100969func Test_edit_CTRL_U()
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100970 " Test 'completefunc'
971 new
972 " -1, -2 and -3 are special return values
973 let g:special=0
974 fun! CompleteMonths(findstart, base)
975 if a:findstart
976 " locate the start of the word
977 return g:special
978 else
979 " find months matching with "a:base"
980 let res = []
981 for m in split("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec")
982 if m =~ '^\c'.a:base
983 call add(res, {'word': m, 'abbr': m.' Month', 'icase': 0})
984 endif
985 endfor
986 return {'words': res, 'refresh': 'always'}
987 endif
988 endfun
989 set completefunc=CompleteMonths
990 call setline(1, ['', ''])
991 call cursor(1, 1)
992 call feedkeys("AX\<c-x>\<c-u>\<cr>\<esc>", 'tnix')
993 call assert_equal(['X', '', ''], getline(1, '$'))
994 %d
995 let g:special=-1
996 call feedkeys("AX\<c-x>\<c-u>\<cr>\<esc>", 'tnix')
997 call assert_equal(['XJan', ''], getline(1, '$'))
998 %d
999 let g:special=-2
1000 call feedkeys("AX\<c-x>\<c-u>\<cr>\<esc>", 'tnix')
1001 call assert_equal(['X', ''], getline(1, '$'))
1002 %d
1003 let g:special=-3
1004 call feedkeys("AX\<c-x>\<c-u>\<cr>\<esc>", 'tnix')
1005 call assert_equal(['X', ''], getline(1, '$'))
1006 %d
1007 let g:special=0
1008 call feedkeys("AM\<c-x>\<c-u>\<cr>\<esc>", 'tnix')
1009 call assert_equal(['Mar', ''], getline(1, '$'))
1010 %d
1011 call feedkeys("AM\<c-x>\<c-u>\<c-n>\<cr>\<esc>", 'tnix')
1012 call assert_equal(['May', ''], getline(1, '$'))
1013 %d
1014 call feedkeys("AM\<c-x>\<c-u>\<c-n>\<c-n>\<cr>\<esc>", 'tnix')
1015 call assert_equal(['M', ''], getline(1, '$'))
1016 delfu CompleteMonths
1017 %d
1018 try
1019 call feedkeys("A\<c-x>\<c-u>", 'tnix')
1020 call assert_fails(1, 'unknown completion function')
1021 catch /^Vim\%((\a\+)\)\=:E117/
1022 call assert_true(1, 'E117 error caught')
1023 endtry
1024 set completefunc=
1025 bw!
1026endfunc
1027
Bram Moolenaarff06f282020-04-21 22:01:14 +02001028func Test_edit_completefunc_delete()
1029 func CompleteFunc(findstart, base)
1030 if a:findstart == 1
1031 return col('.') - 1
1032 endif
1033 normal dd
1034 return ['a', 'b']
1035 endfunc
1036 new
1037 set completefunc=CompleteFunc
1038 call setline(1, ['', 'abcd', ''])
1039 2d
zeertzjqcfe45652022-05-27 17:26:55 +01001040 call assert_fails("normal 2G$a\<C-X>\<C-U>", 'E565:')
Bram Moolenaarff06f282020-04-21 22:01:14 +02001041 bwipe!
1042endfunc
1043
1044
Bram Moolenaar1e115362019-01-09 23:01:02 +01001045func Test_edit_CTRL_Z()
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001046 " Ctrl-Z when insertmode is not set inserts it literally
1047 new
1048 call setline(1, 'abc')
1049 call feedkeys("A\<c-z>\<esc>", 'tnix')
1050 call assert_equal(["abc\<c-z>"], getline(1,'$'))
1051 bw!
1052 " TODO: How to Test Ctrl-Z in insert mode, e.g. suspend?
1053endfunc
1054
Bram Moolenaar1e115362019-01-09 23:01:02 +01001055func Test_edit_DROP()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001056 CheckFeature dnd
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001057 new
1058 call setline(1, ['abc def ghi'])
1059 call cursor(1, 1)
1060 try
1061 call feedkeys("i\<Drop>\<Esc>", 'tnix')
1062 call assert_fails(1, 'Invalid register name')
1063 catch /^Vim\%((\a\+)\)\=:E353/
1064 call assert_true(1, 'error caught')
1065 endtry
1066 bw!
1067endfunc
1068
Bram Moolenaar1e115362019-01-09 23:01:02 +01001069func Test_edit_CTRL_V()
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001070 new
1071 call setline(1, ['abc'])
1072 call cursor(2, 1)
zeertzjq502d8ae2022-01-24 15:27:50 +00001073
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001074 " force some redraws
1075 set showmode showcmd
zeertzjq502d8ae2022-01-24 15:27:50 +00001076 call test_override('char_avail', 1)
1077
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001078 call feedkeys("A\<c-v>\<c-n>\<c-v>\<c-l>\<c-v>\<c-b>\<esc>", 'tnix')
1079 call assert_equal(["abc\x0e\x0c\x02"], getline(1, '$'))
1080
1081 if has("rightleft") && exists("+rl")
1082 set rl
1083 call setline(1, ['abc'])
1084 call cursor(2, 1)
1085 call feedkeys("A\<c-v>\<c-n>\<c-v>\<c-l>\<c-v>\<c-b>\<esc>", 'tnix')
1086 call assert_equal(["abc\x0e\x0c\x02"], getline(1, '$'))
1087 set norl
1088 endif
1089
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001090 set noshowmode showcmd
zeertzjq502d8ae2022-01-24 15:27:50 +00001091 call test_override('char_avail', 0)
1092
1093 " No modifiers should be applied to the char typed using i_CTRL-V_digit.
1094 call feedkeys(":append\<CR>\<C-V>76c\<C-V>76\<C-F2>\<C-V>u3c0j\<C-V>u3c0\<M-F3>\<CR>.\<CR>", 'tnix')
1095 call assert_equal('LcL<C-F2>πjπ<M-F3>', getline(2))
1096
1097 if has('osx')
1098 " A char with a modifier should not be a valid char for i_CTRL-V_digit.
1099 call feedkeys("o\<C-V>\<D-j>\<C-V>\<D-1>\<C-V>\<D-o>\<C-V>\<D-x>\<C-V>\<D-u>", 'tnix')
1100 call assert_equal('<D-j><D-1><D-o><D-x><D-u>', getline(3))
1101 endif
1102
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001103 bw!
1104endfunc
1105
Bram Moolenaar1e115362019-01-09 23:01:02 +01001106func Test_edit_F1()
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01001107 CheckFeature quickfix
1108
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001109 " Pressing <f1>
1110 new
1111 call feedkeys(":set im\<cr>\<f1>\<c-l>", 'tnix')
1112 set noinsertmode
1113 call assert_equal('help', &buftype)
1114 bw
1115 bw
1116endfunc
1117
Bram Moolenaar1e115362019-01-09 23:01:02 +01001118func Test_edit_F21()
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001119 " Pressing <f21>
1120 " sends a netbeans command
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001121 CheckFeature netbeans_intg
1122 new
1123 " I have no idea what this is supposed to do :)
1124 call feedkeys("A\<F21>\<F1>\<esc>", 'tnix')
1125 bw
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001126endfunc
1127
Bram Moolenaar1e115362019-01-09 23:01:02 +01001128func Test_edit_HOME_END()
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001129 " Test Home/End Keys
1130 new
1131 set foldopen+=hor
1132 call setline(1, ['abc', 'def'])
1133 call cursor(1, 1)
1134 call feedkeys("AX\<Home>Y\<esc>", 'tnix')
1135 call cursor(2, 1)
1136 call feedkeys("iZ\<End>Y\<esc>", 'tnix')
1137 call assert_equal(['YabcX', 'ZdefY'], getline(1, '$'))
1138
1139 set foldopen-=hor
1140 bw!
1141endfunc
1142
Bram Moolenaar1e115362019-01-09 23:01:02 +01001143func Test_edit_INS()
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001144 " Test for Pressing <Insert>
1145 new
1146 call setline(1, ['abc', 'def'])
1147 call cursor(1, 1)
1148 call feedkeys("i\<Insert>ZYX>", 'tnix')
1149 call assert_equal(['ZYX>', 'def'], getline(1, '$'))
1150 call setline(1, ['abc', 'def'])
1151 call cursor(1, 1)
1152 call feedkeys("i\<Insert>Z\<Insert>YX>", 'tnix')
1153 call assert_equal(['ZYX>bc', 'def'], getline(1, '$'))
1154 bw!
1155endfunc
1156
Bram Moolenaar1e115362019-01-09 23:01:02 +01001157func Test_edit_LEFT_RIGHT()
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001158 " Left, Shift-Left, Right, Shift-Right
1159 new
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001160 call setline(1, ['abc def ghi', 'ABC DEF GHI', 'ZZZ YYY XXX'])
1161 let _ww=&ww
1162 set ww=
1163 call cursor(2, 1)
1164 call feedkeys("i\<left>\<esc>", 'tnix')
1165 call assert_equal([0, 2, 1, 0], getpos('.'))
1166 " Is this a bug, <s-left> does not respect whichwrap option
1167 call feedkeys("i\<s-left>\<esc>", 'tnix')
1168 call assert_equal([0, 1, 8, 0], getpos('.'))
1169 call feedkeys("i". repeat("\<s-left>", 3). "\<esc>", 'tnix')
1170 call assert_equal([0, 1, 1, 0], getpos('.'))
1171 call feedkeys("i\<right>\<esc>", 'tnix')
1172 call assert_equal([0, 1, 1, 0], getpos('.'))
1173 call feedkeys("i\<right>\<right>\<esc>", 'tnix')
1174 call assert_equal([0, 1, 2, 0], getpos('.'))
1175 call feedkeys("A\<right>\<esc>", 'tnix')
1176 call assert_equal([0, 1, 11, 0], getpos('.'))
1177 call feedkeys("A\<s-right>\<esc>", 'tnix')
1178 call assert_equal([0, 2, 1, 0], getpos('.'))
1179 call feedkeys("i\<s-right>\<esc>", 'tnix')
1180 call assert_equal([0, 2, 4, 0], getpos('.'))
1181 call cursor(3, 11)
1182 call feedkeys("A\<right>\<esc>", 'tnix')
1183 call feedkeys("A\<s-right>\<esc>", 'tnix')
1184 call assert_equal([0, 3, 11, 0], getpos('.'))
1185 call cursor(2, 11)
1186 " <S-Right> does not respect 'whichwrap' option
1187 call feedkeys("A\<s-right>\<esc>", 'tnix')
1188 call assert_equal([0, 3, 1, 0], getpos('.'))
1189 " Check motion when 'whichwrap' contains cursor keys for insert mode
1190 set ww+=[,]
1191 call cursor(2, 1)
1192 call feedkeys("i\<left>\<esc>", 'tnix')
1193 call assert_equal([0, 1, 11, 0], getpos('.'))
1194 call cursor(2, 11)
1195 call feedkeys("A\<right>\<esc>", 'tnix')
1196 call assert_equal([0, 3, 1, 0], getpos('.'))
1197 call cursor(2, 11)
1198 call feedkeys("A\<s-right>\<esc>", 'tnix')
1199 call assert_equal([0, 3, 1, 0], getpos('.'))
1200 let &ww = _ww
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001201 bw!
1202endfunc
1203
Bram Moolenaar1e115362019-01-09 23:01:02 +01001204func Test_edit_MOUSE()
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001205 " This is a simple test, since we not really using the mouse here
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001206 CheckFeature mouse
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001207 10new
1208 call setline(1, range(1, 100))
1209 call cursor(1, 1)
Bram Moolenaar8e8dc9b2022-05-08 20:38:06 +01001210 call assert_equal(1, line('w0'))
1211 call assert_equal(10, line('w$'))
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001212 set mouse=a
Bram Moolenaar8e8dc9b2022-05-08 20:38:06 +01001213 " One scroll event moves three lines.
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001214 call feedkeys("A\<ScrollWheelDown>\<esc>", 'tnix')
Bram Moolenaar8e8dc9b2022-05-08 20:38:06 +01001215 call assert_equal(4, line('w0'))
1216 call assert_equal(13, line('w$'))
1217 " This should move by one page down.
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001218 call feedkeys("A\<S-ScrollWheelDown>\<esc>", 'tnix')
Bram Moolenaar8e8dc9b2022-05-08 20:38:06 +01001219 call assert_equal(14, line('w0'))
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001220 set nostartofline
Bram Moolenaar8e8dc9b2022-05-08 20:38:06 +01001221 " Another page down.
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001222 call feedkeys("A\<C-ScrollWheelDown>\<esc>", 'tnix')
Bram Moolenaar8e8dc9b2022-05-08 20:38:06 +01001223 call assert_equal(24, line('w0'))
1224
1225 call assert_equal([0, 24, 2, 0], getpos('.'))
1226 call test_setmouse(4, 3)
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001227 call feedkeys("A\<LeftMouse>\<esc>", 'tnix')
Bram Moolenaar8e8dc9b2022-05-08 20:38:06 +01001228 call assert_equal([0, 27, 2, 0], getpos('.'))
Bram Moolenaarb3707712022-05-08 22:49:43 +01001229 set mousemodel=extend
Bram Moolenaar8e8dc9b2022-05-08 20:38:06 +01001230 call test_setmouse(5, 3)
1231 call feedkeys("A\<RightMouse>\<esc>\<esc>", 'tnix')
1232 call assert_equal([0, 28, 2, 0], getpos('.'))
Bram Moolenaarb3707712022-05-08 22:49:43 +01001233 set mousemodel&
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001234 call cursor(1, 100)
1235 norm! zt
1236 " this should move by a screen up, but when the test
1237 " is run, it moves up to the top of the buffer...
1238 call feedkeys("A\<ScrollWheelUp>\<esc>", 'tnix')
1239 call assert_equal([0, 1, 1, 0], getpos('.'))
1240 call cursor(1, 30)
1241 norm! zt
1242 call feedkeys("A\<S-ScrollWheelUp>\<esc>", 'tnix')
1243 call assert_equal([0, 1, 1, 0], getpos('.'))
1244 call cursor(1, 30)
1245 norm! zt
1246 call feedkeys("A\<C-ScrollWheelUp>\<esc>", 'tnix')
1247 call assert_equal([0, 1, 1, 0], getpos('.'))
1248 %d
1249 call setline(1, repeat(["12345678901234567890"], 100))
1250 call cursor(2, 1)
1251 call feedkeys("A\<ScrollWheelRight>\<esc>", 'tnix')
1252 call assert_equal([0, 2, 20, 0], getpos('.'))
1253 call feedkeys("A\<ScrollWheelLeft>\<esc>", 'tnix')
1254 call assert_equal([0, 2, 20, 0], getpos('.'))
1255 call feedkeys("A\<S-ScrollWheelRight>\<esc>", 'tnix')
1256 call assert_equal([0, 2, 20, 0], getpos('.'))
1257 call feedkeys("A\<S-ScrollWheelLeft>\<esc>", 'tnix')
1258 call assert_equal([0, 2, 20, 0], getpos('.'))
1259 call feedkeys("A\<C-ScrollWheelRight>\<esc>", 'tnix')
1260 call assert_equal([0, 2, 20, 0], getpos('.'))
1261 call feedkeys("A\<C-ScrollWheelLeft>\<esc>", 'tnix')
1262 call assert_equal([0, 2, 20, 0], getpos('.'))
1263 set mouse& startofline
1264 bw!
1265endfunc
1266
Bram Moolenaar1e115362019-01-09 23:01:02 +01001267func Test_edit_PAGEUP_PAGEDOWN()
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001268 10new
1269 call setline(1, repeat(['abc def ghi'], 30))
1270 call cursor(1, 1)
1271 call feedkeys("i\<PageDown>\<esc>", 'tnix')
1272 call assert_equal([0, 9, 1, 0], getpos('.'))
1273 call feedkeys("i\<PageDown>\<esc>", 'tnix')
1274 call assert_equal([0, 17, 1, 0], getpos('.'))
1275 call feedkeys("i\<PageDown>\<esc>", 'tnix')
1276 call assert_equal([0, 25, 1, 0], getpos('.'))
1277 call feedkeys("i\<PageDown>\<esc>", 'tnix')
1278 call assert_equal([0, 30, 1, 0], getpos('.'))
1279 call feedkeys("i\<PageDown>\<esc>", 'tnix')
1280 call assert_equal([0, 30, 1, 0], getpos('.'))
1281 call feedkeys("A\<PageUp>\<esc>", 'tnix')
1282 call assert_equal([0, 29, 1, 0], getpos('.'))
1283 call feedkeys("A\<PageUp>\<esc>", 'tnix')
1284 call assert_equal([0, 21, 1, 0], getpos('.'))
1285 call feedkeys("A\<PageUp>\<esc>", 'tnix')
1286 call assert_equal([0, 13, 1, 0], getpos('.'))
1287 call feedkeys("A\<PageUp>\<esc>", 'tnix')
1288 call assert_equal([0, 5, 1, 0], getpos('.'))
1289 call feedkeys("A\<PageUp>\<esc>", 'tnix')
1290 call assert_equal([0, 5, 11, 0], getpos('.'))
1291 " <S-Up> is the same as <PageUp>
1292 " <S-Down> is the same as <PageDown>
1293 call cursor(1, 1)
1294 call feedkeys("i\<S-Down>\<esc>", 'tnix')
1295 call assert_equal([0, 9, 1, 0], getpos('.'))
1296 call feedkeys("i\<S-Down>\<esc>", 'tnix')
1297 call assert_equal([0, 17, 1, 0], getpos('.'))
1298 call feedkeys("i\<S-Down>\<esc>", 'tnix')
1299 call assert_equal([0, 25, 1, 0], getpos('.'))
1300 call feedkeys("i\<S-Down>\<esc>", 'tnix')
1301 call assert_equal([0, 30, 1, 0], getpos('.'))
1302 call feedkeys("i\<S-Down>\<esc>", 'tnix')
1303 call assert_equal([0, 30, 1, 0], getpos('.'))
1304 call feedkeys("A\<S-Up>\<esc>", 'tnix')
1305 call assert_equal([0, 29, 1, 0], getpos('.'))
1306 call feedkeys("A\<S-Up>\<esc>", 'tnix')
1307 call assert_equal([0, 21, 1, 0], getpos('.'))
1308 call feedkeys("A\<S-Up>\<esc>", 'tnix')
1309 call assert_equal([0, 13, 1, 0], getpos('.'))
1310 call feedkeys("A\<S-Up>\<esc>", 'tnix')
1311 call assert_equal([0, 5, 1, 0], getpos('.'))
1312 call feedkeys("A\<S-Up>\<esc>", 'tnix')
1313 call assert_equal([0, 5, 11, 0], getpos('.'))
1314 set nostartofline
1315 call cursor(30, 11)
1316 norm! zt
1317 call feedkeys("A\<PageUp>\<esc>", 'tnix')
1318 call assert_equal([0, 29, 11, 0], getpos('.'))
1319 call feedkeys("A\<PageUp>\<esc>", 'tnix')
1320 call assert_equal([0, 21, 11, 0], getpos('.'))
1321 call feedkeys("A\<PageUp>\<esc>", 'tnix')
1322 call assert_equal([0, 13, 11, 0], getpos('.'))
1323 call feedkeys("A\<PageUp>\<esc>", 'tnix')
1324 call assert_equal([0, 5, 11, 0], getpos('.'))
1325 call feedkeys("A\<PageUp>\<esc>", 'tnix')
1326 call assert_equal([0, 5, 11, 0], getpos('.'))
1327 call cursor(1, 1)
1328 call feedkeys("A\<PageDown>\<esc>", 'tnix')
1329 call assert_equal([0, 9, 11, 0], getpos('.'))
1330 call feedkeys("A\<PageDown>\<esc>", 'tnix')
1331 call assert_equal([0, 17, 11, 0], getpos('.'))
1332 call feedkeys("A\<PageDown>\<esc>", 'tnix')
1333 call assert_equal([0, 25, 11, 0], getpos('.'))
1334 call feedkeys("A\<PageDown>\<esc>", 'tnix')
1335 call assert_equal([0, 30, 11, 0], getpos('.'))
1336 call feedkeys("A\<PageDown>\<esc>", 'tnix')
1337 call assert_equal([0, 30, 11, 0], getpos('.'))
1338 " <S-Up> is the same as <PageUp>
1339 " <S-Down> is the same as <PageDown>
1340 call cursor(30, 11)
1341 norm! zt
1342 call feedkeys("A\<S-Up>\<esc>", 'tnix')
1343 call assert_equal([0, 29, 11, 0], getpos('.'))
1344 call feedkeys("A\<S-Up>\<esc>", 'tnix')
1345 call assert_equal([0, 21, 11, 0], getpos('.'))
1346 call feedkeys("A\<S-Up>\<esc>", 'tnix')
1347 call assert_equal([0, 13, 11, 0], getpos('.'))
1348 call feedkeys("A\<S-Up>\<esc>", 'tnix')
1349 call assert_equal([0, 5, 11, 0], getpos('.'))
1350 call feedkeys("A\<S-Up>\<esc>", 'tnix')
1351 call assert_equal([0, 5, 11, 0], getpos('.'))
1352 call cursor(1, 1)
1353 call feedkeys("A\<S-Down>\<esc>", 'tnix')
1354 call assert_equal([0, 9, 11, 0], getpos('.'))
1355 call feedkeys("A\<S-Down>\<esc>", 'tnix')
1356 call assert_equal([0, 17, 11, 0], getpos('.'))
1357 call feedkeys("A\<S-Down>\<esc>", 'tnix')
1358 call assert_equal([0, 25, 11, 0], getpos('.'))
1359 call feedkeys("A\<S-Down>\<esc>", 'tnix')
1360 call assert_equal([0, 30, 11, 0], getpos('.'))
1361 call feedkeys("A\<S-Down>\<esc>", 'tnix')
1362 call assert_equal([0, 30, 11, 0], getpos('.'))
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001363 bw!
1364endfunc
1365
Bram Moolenaar1e115362019-01-09 23:01:02 +01001366func Test_edit_forbidden()
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001367 new
1368 " 1) edit in the sandbox is not allowed
1369 call setline(1, 'a')
1370 com! Sandbox :sandbox call feedkeys("i\<del>\<esc>", 'tnix')
1371 call assert_fails(':Sandbox', 'E48:')
1372 com! Sandbox :sandbox exe "norm! i\<del>"
1373 call assert_fails(':Sandbox', 'E48:')
1374 delcom Sandbox
1375 call assert_equal(['a'], getline(1,'$'))
Bram Moolenaar52797ba2021-12-16 14:45:13 +00001376
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001377 " 2) edit with textlock set
1378 fu! DoIt()
1379 call feedkeys("i\<del>\<esc>", 'tnix')
1380 endfu
1381 au InsertCharPre <buffer> :call DoIt()
1382 try
1383 call feedkeys("ix\<esc>", 'tnix')
1384 call assert_fails(1, 'textlock')
Bram Moolenaarff06f282020-04-21 22:01:14 +02001385 catch /^Vim\%((\a\+)\)\=:E565/ " catch E565: not allowed here
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001386 endtry
1387 " TODO: Might be a bug: should x really be inserted here
1388 call assert_equal(['xa'], getline(1, '$'))
1389 delfu DoIt
1390 try
1391 call feedkeys("ix\<esc>", 'tnix')
1392 call assert_fails(1, 'unknown function')
1393 catch /^Vim\%((\a\+)\)\=:E117/ " catch E117: unknown function
1394 endtry
1395 au! InsertCharPre
Bram Moolenaar52797ba2021-12-16 14:45:13 +00001396
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001397 " 3) edit when completion is shown
1398 fun! Complete(findstart, base)
1399 if a:findstart
1400 return col('.')
1401 else
1402 call feedkeys("i\<del>\<esc>", 'tnix')
1403 return []
1404 endif
1405 endfun
1406 set completefunc=Complete
1407 try
1408 call feedkeys("i\<c-x>\<c-u>\<esc>", 'tnix')
1409 call assert_fails(1, 'change in complete function')
Bram Moolenaarff06f282020-04-21 22:01:14 +02001410 catch /^Vim\%((\a\+)\)\=:E565/ " catch E565
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001411 endtry
1412 delfu Complete
1413 set completefunc=
Bram Moolenaar52797ba2021-12-16 14:45:13 +00001414
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001415 if has("rightleft") && exists("+fkmap")
1416 " 4) 'R' when 'fkmap' and 'revins' is set.
1417 set revins fkmap
1418 try
1419 normal Ri
1420 call assert_fails(1, "R with 'fkmap' and 'ri' set")
1421 catch
1422 finally
1423 set norevins nofkmap
1424 endtry
1425 endif
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001426 bw!
1427endfunc
1428
Bram Moolenaar1e115362019-01-09 23:01:02 +01001429func Test_edit_rightleft()
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001430 " Cursor in rightleft mode moves differently
Bram Moolenaar845e0ee2020-06-20 16:05:32 +02001431 CheckFeature rightleft
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001432 call NewWindow(10, 20)
1433 call setline(1, ['abc', 'def', 'ghi'])
1434 call cursor(1, 2)
1435 set rightleft
1436 " Screen looks as expected
1437 let lines = ScreenLines([1, 4], winwidth(0))
1438 let expect = [
1439 \" cba",
1440 \" fed",
1441 \" ihg",
1442 \" ~"]
1443 call assert_equal(join(expect, "\n"), join(lines, "\n"))
1444 " 2) right moves to the left
1445 call feedkeys("i\<right>\<esc>x", 'txin')
1446 call assert_equal(['bc', 'def', 'ghi'], getline(1,'$'))
1447 call cursor(1, 2)
1448 call feedkeys("i\<s-right>\<esc>", 'txin')
1449 call cursor(1, 2)
1450 call feedkeys("i\<c-right>\<esc>", 'txin')
1451 " Screen looks as expected
1452 let lines = ScreenLines([1, 4], winwidth(0))
1453 let expect = [
1454 \" cb",
1455 \" fed",
1456 \" ihg",
1457 \" ~"]
1458 call assert_equal(join(expect, "\n"), join(lines, "\n"))
1459 " 2) left moves to the right
1460 call setline(1, ['abc', 'def', 'ghi'])
1461 call cursor(1, 2)
1462 call feedkeys("i\<left>\<esc>x", 'txin')
1463 call assert_equal(['ac', 'def', 'ghi'], getline(1,'$'))
1464 call cursor(1, 2)
1465 call feedkeys("i\<s-left>\<esc>", 'txin')
1466 call cursor(1, 2)
1467 call feedkeys("i\<c-left>\<esc>", 'txin')
1468 " Screen looks as expected
1469 let lines = ScreenLines([1, 4], winwidth(0))
1470 let expect = [
1471 \" ca",
1472 \" fed",
1473 \" ihg",
1474 \" ~"]
1475 call assert_equal(join(expect, "\n"), join(lines, "\n"))
Bram Moolenaar845e0ee2020-06-20 16:05:32 +02001476 %d _
1477 call test_override('redraw_flag', 1)
1478 call test_override('char_avail', 1)
1479 call feedkeys("a\<C-V>x41", "xt")
1480 redraw!
1481 call assert_equal(repeat(' ', 19) .. 'A', Screenline(1))
1482 call test_override('ALL', 0)
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001483 set norightleft
1484 bw!
1485endfunc
Bram Moolenaar658a3a22017-03-31 22:27:12 +02001486
1487func Test_edit_complete_very_long_name()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001488 " Long directory names only work on Unix.
1489 CheckUnix
Bram Moolenaar15ecbd62017-04-07 14:10:48 +02001490
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01001491 let dirname = getcwd() . "/Xlongdir"
Bram Moolenaar15ecbd62017-04-07 14:10:48 +02001492 let longdirname = dirname . repeat('/' . repeat('d', 255), 4)
1493 try
Bram Moolenaar14f91762022-09-21 15:13:52 +01001494 call mkdir(longdirname, 'pR')
Bram Moolenaar15ecbd62017-04-07 14:10:48 +02001495 catch /E739:/
1496 " Long directory name probably not supported.
1497 call delete(dirname, 'rf')
1498 return
1499 endtry
1500
Bram Moolenaarf8191c52019-05-18 17:22:54 +02001501 " Try to get the Vim window position before setting 'columns', so that we can
1502 " move the window back to where it was.
Bram Moolenaarba6ec182017-04-04 22:41:10 +02001503 let winposx = getwinposx()
1504 let winposy = getwinposy()
Bram Moolenaarf8191c52019-05-18 17:22:54 +02001505
1506 if winposx >= 0 && winposy >= 0 && !has('gui_running')
1507 " We did get the window position, but xterm may report the wrong numbers.
1508 " Move the window to the reported position and compute any offset.
1509 exe 'winpos ' . winposx . ' ' . winposy
1510 sleep 100m
1511 let x = getwinposx()
1512 if x >= 0
1513 let winposx += winposx - x
1514 endif
1515 let y = getwinposy()
1516 if y >= 0
1517 let winposy += winposy - y
1518 endif
1519 endif
1520
Bram Moolenaar658a3a22017-03-31 22:27:12 +02001521 let save_columns = &columns
Bram Moolenaar15ecbd62017-04-07 14:10:48 +02001522 " Need at least about 1100 columns to reproduce the problem.
Bram Moolenaarba6ec182017-04-04 22:41:10 +02001523 set columns=2000
Bram Moolenaar658a3a22017-03-31 22:27:12 +02001524 set noswapfile
Bram Moolenaarba6ec182017-04-04 22:41:10 +02001525
Bram Moolenaar658a3a22017-03-31 22:27:12 +02001526 let longfilename = longdirname . '/' . repeat('a', 255)
Bram Moolenaar658a3a22017-03-31 22:27:12 +02001527 call writefile(['Totum', 'Table'], longfilename)
1528 new
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01001529 exe "next Xnofile " . longfilename
Bram Moolenaar658a3a22017-03-31 22:27:12 +02001530 exe "normal iT\<C-N>"
1531
1532 bwipe!
1533 exe 'bwipe! ' . longfilename
Bram Moolenaar658a3a22017-03-31 22:27:12 +02001534 let &columns = save_columns
Bram Moolenaarba6ec182017-04-04 22:41:10 +02001535 if winposx >= 0 && winposy >= 0
1536 exe 'winpos ' . winposx . ' ' . winposy
1537 endif
Bram Moolenaar658a3a22017-03-31 22:27:12 +02001538 set swapfile&
1539endfunc
Bram Moolenaarff930ca2017-10-19 17:12:10 +02001540
Bram Moolenaar2c8c6812018-07-28 17:07:52 +02001541func Test_edit_backtick()
1542 next a\`b c
1543 call assert_equal('a`b', expand('%'))
1544 next
1545 call assert_equal('c', expand('%'))
1546 call assert_equal('a\`b c', expand('##'))
1547endfunc
1548
Bram Moolenaarff930ca2017-10-19 17:12:10 +02001549func Test_edit_quit()
1550 edit foo.txt
1551 split
1552 new
1553 call setline(1, 'hello')
1554 3wincmd w
1555 redraw!
1556 call assert_fails('1q', 'E37:')
1557 bwipe! foo.txt
1558 only
1559endfunc
1560
Bram Moolenaaradb8fbe2018-06-04 20:34:23 +02001561func Test_edit_alt()
1562 " Keeping the cursor line didn't happen when the first line has indent.
1563 new
1564 call setline(1, [' one', 'two', 'three'])
1565 w XAltFile
1566 $
1567 call assert_equal(3, line('.'))
1568 e Xother
1569 e #
1570 call assert_equal(3, line('.'))
1571
1572 bwipe XAltFile
1573 call delete('XAltFile')
1574endfunc
Bram Moolenaar4dbc2622018-11-02 11:59:15 +01001575
Bram Moolenaardb934952020-04-27 20:18:31 +02001576func Test_edit_InsertLeave()
Bram Moolenaar4dbc2622018-11-02 11:59:15 +01001577 new
Bram Moolenaarb53e13a2020-10-21 12:19:53 +02001578 au InsertLeavePre * let g:did_au_pre = 1
Bram Moolenaar4dbc2622018-11-02 11:59:15 +01001579 au InsertLeave * let g:did_au = 1
Bram Moolenaarb53e13a2020-10-21 12:19:53 +02001580 let g:did_au_pre = 0
Bram Moolenaar4dbc2622018-11-02 11:59:15 +01001581 let g:did_au = 0
1582 call feedkeys("afoo\<Esc>", 'tx')
Bram Moolenaarb53e13a2020-10-21 12:19:53 +02001583 call assert_equal(1, g:did_au_pre)
Bram Moolenaar4dbc2622018-11-02 11:59:15 +01001584 call assert_equal(1, g:did_au)
1585 call assert_equal('foo', getline(1))
1586
Bram Moolenaarb53e13a2020-10-21 12:19:53 +02001587 let g:did_au_pre = 0
Bram Moolenaar4dbc2622018-11-02 11:59:15 +01001588 let g:did_au = 0
1589 call feedkeys("Sbar\<C-C>", 'tx')
Bram Moolenaarb53e13a2020-10-21 12:19:53 +02001590 call assert_equal(1, g:did_au_pre)
Bram Moolenaar4dbc2622018-11-02 11:59:15 +01001591 call assert_equal(0, g:did_au)
1592 call assert_equal('bar', getline(1))
1593
1594 inoremap x xx<Esc>
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("Saax", '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('aaxx', getline(1))
1601
1602 inoremap x xx<C-C>
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("Sbbx", '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('bbxx', getline(1))
1609
1610 bwipe!
Bram Moolenaarb53e13a2020-10-21 12:19:53 +02001611 au! InsertLeave InsertLeavePre
Bram Moolenaar4dbc2622018-11-02 11:59:15 +01001612 iunmap x
1613endfunc
Bram Moolenaarc6b37db2019-04-27 18:00:34 +02001614
Bram Moolenaardb934952020-04-27 20:18:31 +02001615func Test_edit_InsertLeave_undo()
1616 new XtestUndo
1617 set undofile
1618 au InsertLeave * wall
1619 exe "normal ofoo\<Esc>"
1620 call assert_equal(2, line('$'))
1621 normal u
1622 call assert_equal(1, line('$'))
1623
1624 bwipe!
1625 au! InsertLeave
1626 call delete('XtestUndo')
Bram Moolenaarb340bae2020-06-15 19:51:56 +02001627 call delete(undofile('XtestUndo'))
Bram Moolenaardb934952020-04-27 20:18:31 +02001628 set undofile&
1629endfunc
1630
Bram Moolenaarc6b37db2019-04-27 18:00:34 +02001631" Test for inserting characters using CTRL-V followed by a number.
1632func Test_edit_special_chars()
1633 new
1634
Bram Moolenaar424bcae2022-01-31 14:59:41 +00001635 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 +02001636
1637 exe "normal " . t
1638 call assert_equal("ABC !a\<C-O>g\<C-G>8", getline(2))
1639
1640 close!
1641endfunc
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02001642
1643func Test_edit_startinsert()
1644 new
1645 set backspace+=start
1646 call setline(1, 'foobar')
1647 call feedkeys("A\<C-U>\<Esc>", 'xt')
1648 call assert_equal('', getline(1))
1649
1650 call setline(1, 'foobar')
1651 call feedkeys(":startinsert!\<CR>\<C-U>\<Esc>", 'xt')
1652 call assert_equal('', getline(1))
1653
1654 set backspace&
1655 bwipe!
1656endfunc
Bram Moolenaar177c9f22019-11-06 13:59:16 +01001657
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +01001658" Test for :startreplace and :startgreplace
1659func Test_edit_startreplace()
1660 new
1661 call setline(1, 'abc')
1662 call feedkeys("l:startreplace\<CR>xyz\e", 'xt')
1663 call assert_equal('axyz', getline(1))
1664 call feedkeys("0:startreplace!\<CR>abc\e", 'xt')
1665 call assert_equal('axyzabc', getline(1))
1666 call setline(1, "a\tb")
1667 call feedkeys("0l:startgreplace\<CR>xyz\e", 'xt')
1668 call assert_equal("axyz\tb", getline(1))
1669 call feedkeys("0i\<C-R>=execute('startreplace')\<CR>12\e", 'xt')
1670 call assert_equal("12axyz\tb", getline(1))
1671 close!
1672endfunc
1673
Bram Moolenaar177c9f22019-11-06 13:59:16 +01001674func Test_edit_noesckeys()
Bram Moolenaar215ba3b2019-11-06 15:07:07 +01001675 CheckNotGui
Bram Moolenaar177c9f22019-11-06 13:59:16 +01001676 new
1677
1678 " <Left> moves cursor when 'esckeys' is set
1679 exe "set t_kl=\<Esc>OD"
1680 set esckeys
1681 call feedkeys("axyz\<Esc>ODX", "xt")
1682 call assert_equal("xyXz", getline(1))
1683
1684 " <Left> exits Insert mode when 'esckeys' is off
1685 set noesckeys
1686 call setline(1, '')
1687 call feedkeys("axyz\<Esc>ODX", "xt")
1688 call assert_equal(["DX", "xyz"], getline(1, 2))
1689
1690 bwipe!
1691 set esckeys
1692endfunc
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +01001693
Bram Moolenaar1671f442020-03-10 07:48:13 +01001694" Test for running an invalid ex command in insert mode using CTRL-O
Bram Moolenaar1671f442020-03-10 07:48:13 +01001695func Test_edit_ctrl_o_invalid_cmd()
1696 new
1697 set showmode showcmd
Bram Moolenaarb340bae2020-06-15 19:51:56 +02001698 " Avoid a sleep of 3 seconds. Zero might have side effects.
1699 call test_override('ui_delay', 50)
Bram Moolenaar1671f442020-03-10 07:48:13 +01001700 let caught_e492 = 0
1701 try
1702 call feedkeys("i\<C-O>:invalid\<CR>abc\<Esc>", "xt")
1703 catch /E492:/
1704 let caught_e492 = 1
1705 endtry
1706 call assert_equal(1, caught_e492)
1707 call assert_equal('abc', getline(1))
1708 set showmode& showcmd&
Bram Moolenaarb340bae2020-06-15 19:51:56 +02001709 call test_override('ui_delay', 0)
Bram Moolenaar1671f442020-03-10 07:48:13 +01001710 close!
1711endfunc
1712
Bram Moolenaarb340bae2020-06-15 19:51:56 +02001713" Test for editing a file with a very long name
1714func Test_edit_illegal_filename()
1715 CheckEnglish
1716 new
1717 redir => msg
1718 exe 'edit ' . repeat('f', 5000)
1719 redir END
1720 call assert_match("Illegal file name$", split(msg, "\n")[0])
1721 close!
1722endfunc
1723
Bram Moolenaarc8fe6452020-10-03 17:04:37 +02001724" Test for editing a directory
1725func Test_edit_is_a_directory()
1726 CheckEnglish
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01001727 let dirname = getcwd() . "/Xeditdir"
Bram Moolenaarc8fe6452020-10-03 17:04:37 +02001728 call mkdir(dirname, 'p')
1729
1730 new
1731 redir => msg
1732 exe 'edit' dirname
1733 redir END
1734 call assert_match("is a directory$", split(msg, "\n")[0])
1735 bwipe!
1736
1737 let dirname .= '/'
1738
1739 new
1740 redir => msg
1741 exe 'edit' dirname
1742 redir END
1743 call assert_match("is a directory$", split(msg, "\n")[0])
1744 bwipe!
1745
1746 call delete(dirname, 'rf')
1747endfunc
1748
Bram Moolenaarb340bae2020-06-15 19:51:56 +02001749" Test for editing a file using invalid file encoding
1750func Test_edit_invalid_encoding()
1751 CheckEnglish
Bram Moolenaar14f91762022-09-21 15:13:52 +01001752 call writefile([], 'Xinvfile', 'D')
Bram Moolenaarb340bae2020-06-15 19:51:56 +02001753 redir => msg
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01001754 new ++enc=axbyc Xinvfile
Bram Moolenaarb340bae2020-06-15 19:51:56 +02001755 redir END
1756 call assert_match('\[NOT converted\]', msg)
Bram Moolenaarb340bae2020-06-15 19:51:56 +02001757 close!
1758endfunc
1759
1760" Test for the "charconvert" option
1761func Test_edit_charconvert()
1762 CheckEnglish
Bram Moolenaar14f91762022-09-21 15:13:52 +01001763 call writefile(['one', 'two'], 'Xccfile', 'D')
Bram Moolenaarb340bae2020-06-15 19:51:56 +02001764
1765 " set 'charconvert' to a non-existing function
1766 set charconvert=NonExitingFunc()
1767 new
1768 let caught_e117 = v:false
1769 try
1770 redir => msg
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01001771 edit ++enc=axbyc Xccfile
Bram Moolenaarb340bae2020-06-15 19:51:56 +02001772 catch /E117:/
1773 let caught_e117 = v:true
1774 finally
1775 redir END
1776 endtry
1777 call assert_true(caught_e117)
1778 call assert_equal(['one', 'two'], getline(1, '$'))
1779 call assert_match("Conversion with 'charconvert' failed", msg)
1780 close!
1781 set charconvert&
1782
1783 " 'charconvert' function doesn't create a output file
1784 func Cconv1()
1785 endfunc
1786 set charconvert=Cconv1()
1787 new
1788 redir => msg
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01001789 edit ++enc=axbyc Xccfile
Bram Moolenaarb340bae2020-06-15 19:51:56 +02001790 redir END
1791 call assert_equal(['one', 'two'], getline(1, '$'))
1792 call assert_match("can't read output of 'charconvert'", msg)
1793 close!
1794 delfunc Cconv1
1795 set charconvert&
1796
1797 " 'charconvert' function to convert to upper case
1798 func Cconv2()
1799 let data = readfile(v:fname_in)
1800 call map(data, 'toupper(v:val)')
1801 call writefile(data, v:fname_out)
1802 endfunc
1803 set charconvert=Cconv2()
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01001804 new Xccfile
1805 write ++enc=ucase Xccfile1
1806 call assert_equal(['ONE', 'TWO'], readfile('Xccfile1'))
1807 call delete('Xccfile1')
Bram Moolenaarb340bae2020-06-15 19:51:56 +02001808 close!
1809 delfunc Cconv2
1810 set charconvert&
1811
1812 " 'charconvert' function removes the input file
1813 func Cconv3()
1814 call delete(v:fname_in)
1815 endfunc
1816 set charconvert=Cconv3()
1817 new
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01001818 call assert_fails('edit ++enc=lcase Xccfile', 'E202:')
Bram Moolenaarb340bae2020-06-15 19:51:56 +02001819 call assert_equal([''], getline(1, '$'))
1820 close!
1821 delfunc Cconv3
1822 set charconvert&
Bram Moolenaarb340bae2020-06-15 19:51:56 +02001823endfunc
1824
1825" Test for editing a file without read permission
1826func Test_edit_file_no_read_perm()
1827 CheckUnix
Bram Moolenaar17709e22021-03-19 14:38:12 +01001828 CheckNotRoot
1829
Bram Moolenaar14f91762022-09-21 15:13:52 +01001830 call writefile(['one', 'two'], 'Xnrpfile', 'D')
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01001831 call setfperm('Xnrpfile', '-w-------')
Bram Moolenaarb340bae2020-06-15 19:51:56 +02001832 new
1833 redir => msg
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01001834 edit Xnrpfile
Bram Moolenaarb340bae2020-06-15 19:51:56 +02001835 redir END
1836 call assert_equal(1, &readonly)
1837 call assert_equal([''], getline(1, '$'))
1838 call assert_match('\[Permission Denied\]', msg)
1839 close!
Bram Moolenaarb340bae2020-06-15 19:51:56 +02001840endfunc
1841
zeertzjq3a56b6d2022-04-08 11:56:14 +01001842" Using :edit without leaving 'insertmode' should not cause Insert mode to be
1843" re-entered immediately after <C-L>
1844func Test_edit_insertmode_ex_edit()
1845 CheckRunVimInTerminal
1846
1847 let lines =<< trim END
1848 set insertmode noruler
1849 inoremap <C-B> <Cmd>edit Xfoo<CR>
1850 END
Bram Moolenaar14f91762022-09-21 15:13:52 +01001851 call writefile(lines, 'Xtest_edit_insertmode_ex_edit', 'D')
zeertzjq3a56b6d2022-04-08 11:56:14 +01001852
Bram Moolenaar14f91762022-09-21 15:13:52 +01001853 let buf = RunVimInTerminal('-S Xtest_edit_insertmode_ex_edit', #{rows: 6, wait_for_ruler: 0})
1854 " Somehow when using valgrind "INSERT" does not show up unless we send
1855 " something to the terminal.
1856 for i in range(30)
1857 if term_getline(buf, 6) =~ 'INSERT'
1858 break
1859 endif
1860 call term_sendkeys(buf, "-")
1861 sleep 100m
1862 endfor
Bram Moolenaarc5382b62022-06-19 15:22:36 +01001863 call WaitForAssert({-> assert_match('^-- INSERT --\s*$', term_getline(buf, 6))})
zeertzjq3a56b6d2022-04-08 11:56:14 +01001864 call term_sendkeys(buf, "\<C-B>\<C-L>")
Bram Moolenaarc5382b62022-06-19 15:22:36 +01001865 call WaitForAssert({-> assert_notmatch('^-- INSERT --\s*$', term_getline(buf, 6))})
zeertzjq3a56b6d2022-04-08 11:56:14 +01001866
1867 " clean up
1868 call StopVimInTerminal(buf)
zeertzjq3a56b6d2022-04-08 11:56:14 +01001869endfunc
1870
Bram Moolenaar845e0ee2020-06-20 16:05:32 +02001871" Pressing escape in 'insertmode' should beep
Bram Moolenaarc5382b62022-06-19 15:22:36 +01001872" FIXME: Execute this later, when using valgrind it makes the next test
1873" Test_edit_insertmode_ex_edit() fail.
1874func Test_z_edit_insertmode_esc_beeps()
Bram Moolenaar845e0ee2020-06-20 16:05:32 +02001875 new
1876 set insertmode
1877 call assert_beeps("call feedkeys(\"one\<Esc>\", 'xt')")
1878 set insertmode&
Bram Moolenaarc5382b62022-06-19 15:22:36 +01001879 " unsupported "CTRL-G l" command should beep in insert mode.
Bram Moolenaar845e0ee2020-06-20 16:05:32 +02001880 call assert_beeps("normal i\<C-G>l")
Bram Moolenaarc5382b62022-06-19 15:22:36 +01001881 bwipe!
Bram Moolenaar845e0ee2020-06-20 16:05:32 +02001882endfunc
1883
1884" Test for 'hkmap' and 'hkmapp'
1885func Test_edit_hkmap()
1886 CheckFeature rightleft
1887 if has('win32') && !has('gui')
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001888 throw 'Skipped: fails on the MS-Windows terminal version'
Bram Moolenaar845e0ee2020-06-20 16:05:32 +02001889 endif
1890 new
1891
1892 set revins hkmap
1893 let str = 'abcdefghijklmnopqrstuvwxyz'
1894 let str ..= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
1895 let str ..= '`/'',.;'
1896 call feedkeys('i' .. str, 'xt')
1897 let expected = "óõú,.;"
1898 let expected ..= "ZYXWVUTSRQPONMLKJIHGFEDCBA"
1899 let expected ..= "æèñ'äåàãø/ôíîöêìçïéòë÷âáðù"
1900 call assert_equal(expected, getline(1))
1901
1902 %d
1903 set revins hkmap hkmapp
1904 let str = 'abcdefghijklmnopqrstuvwxyz'
1905 let str ..= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
1906 call feedkeys('i' .. str, 'xt')
1907 let expected = "õYXWVUTSRQóOïíLKJIHGFEDêBA"
1908 let expected ..= "öòXùåèúæø'ôñðîì÷çéäâóǟãëáà"
1909 call assert_equal(expected, getline(1))
1910
1911 set revins& hkmap& hkmapp&
1912 close!
1913endfunc
1914
1915" Test for 'allowrevins' and using CTRL-_ in insert mode
1916func Test_edit_allowrevins()
1917 CheckFeature rightleft
1918 new
1919 set allowrevins
1920 call feedkeys("iABC\<C-_>DEF\<C-_>GHI", 'xt')
1921 call assert_equal('ABCFEDGHI', getline(1))
1922 set allowrevins&
1923 close!
1924endfunc
1925
1926" Test for inserting a register in insert mode using CTRL-R
1927func Test_edit_insert_reg()
1928 new
1929 let g:Line = ''
1930 func SaveFirstLine()
1931 let g:Line = Screenline(1)
1932 return 'r'
1933 endfunc
1934 inoremap <expr> <buffer> <F2> SaveFirstLine()
1935 call test_override('redraw_flag', 1)
1936 call test_override('char_avail', 1)
1937 let @r = 'sample'
1938 call feedkeys("a\<C-R>=SaveFirstLine()\<CR>", "xt")
1939 call assert_equal('"', g:Line)
1940 call test_override('ALL', 0)
1941 close!
1942endfunc
1943
1944" When a character is inserted at the last position of the last line in a
1945" window, the window contents should be scrolled one line up. If the top line
1946" is part of a fold, then the entire fold should be scrolled up.
1947func Test_edit_lastline_scroll()
1948 new
1949 let h = winheight(0)
1950 let lines = ['one', 'two', 'three']
1951 let lines += repeat(['vim'], h - 4)
1952 call setline(1, lines)
1953 call setline(h, repeat('x', winwidth(0) - 1))
1954 call feedkeys("GAx", 'xt')
1955 redraw!
1956 call assert_equal(h - 1, winline())
1957 call assert_equal(2, line('w0'))
1958
1959 " scroll with a fold
1960 1,2fold
1961 normal gg
1962 call setline(h + 1, repeat('x', winwidth(0) - 1))
1963 call feedkeys("GAx", 'xt')
1964 redraw!
1965 call assert_equal(h - 1, winline())
1966 call assert_equal(3, line('w0'))
1967
1968 close!
1969endfunc
1970
Bram Moolenaar21cbe172020-10-13 19:08:24 +02001971func Test_edit_browse()
1972 " in the GUI this opens a file picker, we only test the terminal behavior
1973 CheckNotGui
1974
1975 " ":browse xxx" checks for the FileExplorer augroup and assumes editing "."
1976 " works then.
1977 augroup FileExplorer
1978 au!
1979 augroup END
1980
1981 " When the USE_FNAME_CASE is defined this used to cause a crash.
1982 browse enew
1983 bwipe!
1984
1985 browse split
1986 bwipe!
1987endfunc
1988
Bram Moolenaarcaf73dc2020-10-26 21:39:13 +01001989func Test_read_invalid()
1990 set encoding=latin1
1991 " This was not properly checking for going past the end.
1992 call assert_fails('r`=', 'E484')
1993 set encoding=utf-8
1994endfunc
1995
Yegappan Lakshmanan59585492021-06-12 13:46:41 +02001996" Test for the 'revins' option
1997func Test_edit_revins()
1998 CheckFeature rightleft
1999 new
2000 set revins
2001 exe "normal! ione\ttwo three"
2002 call assert_equal("eerht owt\teno", getline(1))
2003 call setline(1, "one\ttwo three")
2004 normal! gg$bi a
2005 call assert_equal("one\ttwo a three", getline(1))
2006 exe "normal! $bi\<BS>\<BS>"
2007 call assert_equal("one\ttwo a ree", getline(1))
2008 exe "normal! 0wi\<C-W>"
2009 call assert_equal("one\t a ree", getline(1))
2010 exe "normal! 0wi\<C-U>"
2011 call assert_equal("one\t ", getline(1))
2012 " newline in insert mode starts at the end of the line
2013 call setline(1, 'one two three')
2014 exe "normal! wi\nfour"
2015 call assert_equal(['one two three', 'ruof'], getline(1, '$'))
2016 set revins&
2017 bw!
2018endfunc
2019
Bram Moolenaar35a9a002021-09-11 21:14:20 +02002020" Test for getting the character of the line below after "p"
2021func Test_edit_put_CTRL_E()
2022 set encoding=latin1
2023 new
2024 let @" = ''
2025 sil! norm orggRx
2026 sil! norm pr
2027 call assert_equal(['r', 'r'], getline(1, 2))
2028 bwipe!
2029 set encoding=utf-8
2030endfunc
2031
Dominique Pelle9cd063e2021-10-28 21:06:05 +01002032" Test toggling of input method. See :help i_CTRL-^
2033func Test_edit_CTRL_hat()
2034 CheckFeature xim
Dominique Pelle8753c1d2021-10-31 20:19:17 +00002035
Bram Moolenaar0b962e52022-04-03 18:02:37 +01002036 " FIXME: test fails with Motif GUI.
Dominique Pelle8753c1d2021-10-31 20:19:17 +00002037 " test also fails when running in the GUI.
2038 CheckFeature gui_gtk
2039 CheckNotGui
Dominique Pelle9cd063e2021-10-28 21:06:05 +01002040
2041 new
2042
2043 call assert_equal(0, &iminsert)
2044 call feedkeys("i\<C-^>", 'xt')
2045 call assert_equal(2, &iminsert)
2046 call feedkeys("i\<C-^>", 'xt')
2047 call assert_equal(0, &iminsert)
2048
2049 bwipe!
2050endfunc
2051
Bram Moolenaarde05bb22022-01-13 13:08:14 +00002052" Weird long file name was going over the end of NameBuff
2053func Test_edit_overlong_file_name()
2054 CheckUnix
2055
2056 file 0000000000000000000000000000
2057 file %%%%%%%%%%%%%%%%%%%%%%%%%%
2058 file %%%%%%
2059 set readonly
2060 set ls=2
2061
2062 redraw!
2063 set noreadonly ls&
2064 bwipe!
2065endfunc
2066
Christian Brabandtdfbdadc2022-05-05 20:46:47 +01002067func Test_edit_shift_bs()
2068 CheckMSWindows
2069
2070 " FIXME: this works interactively, but the test fails
2071 throw 'Skipped: Shift-Backspace Test not working correctly :('
2072
2073 " Need to run this in Win32 Terminal, do not use CheckRunVimInTerminal
2074 if !has("terminal")
2075 return
2076 endif
2077
2078 " Shift Backspace should work like Backspace in insert mode
2079 let lines =<< trim END
2080 call setline(1, ['abc'])
2081 END
Bram Moolenaar14f91762022-09-21 15:13:52 +01002082 call writefile(lines, 'Xtest_edit_shift_bs', 'D')
Christian Brabandtdfbdadc2022-05-05 20:46:47 +01002083
2084 let buf = RunVimInTerminal('-S Xtest_edit_shift_bs', #{rows: 3})
2085 call term_sendkeys(buf, "A\<S-BS>-\<esc>")
2086 call TermWait(buf, 50)
2087 call assert_equal('ab-', term_getline(buf, 1))
2088
2089 " clean up
2090 call StopVimInTerminal(buf)
Christian Brabandtdfbdadc2022-05-05 20:46:47 +01002091endfunc
Dominique Pelle9cd063e2021-10-28 21:06:05 +01002092
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +01002093" vim: shiftwidth=2 sts=2 expandtab