blob: 46a475221f6b8089b9e2177df36467ad7d69fcdd [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
327 bw!
328endfunc
329
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +0200330" Test changing indent in replace mode
Bram Moolenaar1e115362019-01-09 23:01:02 +0100331func Test_edit_12()
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100332 new
333 call setline(1, ["\tabc", "\tdef"])
334 call cursor(2, 4)
335 call feedkeys("R^\<c-d>", 'tnix')
336 call assert_equal(["\tabc", "def"], getline(1, '$'))
Bram Moolenaar4c313b12019-08-24 22:58:31 +0200337 call assert_equal([0, 2, 2, 0], '.'->getpos())
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100338 %d
339 call setline(1, ["\tabc", "\t\tdef"])
340 call cursor(2, 2)
341 call feedkeys("R^\<c-d>", 'tnix')
342 call assert_equal(["\tabc", "def"], getline(1, '$'))
343 call assert_equal([0, 2, 1, 0], getpos('.'))
344 %d
345 call setline(1, ["\tabc", "\t\tdef"])
346 call cursor(2, 2)
347 call feedkeys("R\<c-t>", 'tnix')
348 call assert_equal(["\tabc", "\t\t\tdef"], getline(1, '$'))
349 call assert_equal([0, 2, 2, 0], getpos('.'))
350 bw!
351 10vnew
352 call setline(1, ["\tabc", "\t\tdef"])
353 call cursor(2, 2)
354 call feedkeys("R\<c-t>", 'tnix')
355 call assert_equal(["\tabc", "\t\t\tdef"], getline(1, '$'))
356 call assert_equal([0, 2, 2, 0], getpos('.'))
357 %d
358 set sw=4
359 call setline(1, ["\tabc", "\t\tdef"])
360 call cursor(2, 2)
361 call feedkeys("R\<c-t>\<c-t>", 'tnix')
362 call assert_equal(["\tabc", "\t\t\tdef"], getline(1, '$'))
363 call assert_equal([0, 2, 2, 0], getpos('.'))
364 %d
365 call setline(1, ["\tabc", "\t\tdef"])
366 call cursor(2, 2)
367 call feedkeys("R\<c-t>\<c-t>", 'tnix')
368 call assert_equal(["\tabc", "\t\t\tdef"], getline(1, '$'))
369 call assert_equal([0, 2, 2, 0], getpos('.'))
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +0200370 set sw&
371
372 " In replace mode, after hitting enter in a line with tab characters,
373 " pressing backspace should restore the tab characters.
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100374 %d
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +0200375 setlocal autoindent backspace=2
376 call setline(1, "\tone\t\ttwo")
377 exe "normal ggRred\<CR>six" .. repeat("\<BS>", 8)
378 call assert_equal(["\tone\t\ttwo"], getline(1, '$'))
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100379 bw!
380endfunc
381
Bram Moolenaar1e115362019-01-09 23:01:02 +0100382func Test_edit_13()
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100383 " Test smartindenting
384 if exists("+smartindent")
385 new
386 set smartindent autoindent
387 call setline(1, ["\tabc"])
388 call feedkeys("A {\<cr>more\<cr>}\<esc>", 'tnix')
389 call assert_equal(["\tabc {", "\t\tmore", "\t}"], getline(1, '$'))
390 set smartindent& autoindent&
Bram Moolenaar2ba42382019-03-16 18:11:07 +0100391 bwipe!
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100392 endif
Bram Moolenaar2ba42382019-03-16 18:11:07 +0100393
394 " Test autoindent removing indent of blank line.
395 new
396 call setline(1, ' foo bar baz')
397 set autoindent
398 exe "normal 0eea\<CR>\<CR>\<Esc>"
399 call assert_equal(" foo bar", getline(1))
400 call assert_equal("", getline(2))
401 call assert_equal(" baz", getline(3))
402 set autoindent&
Bram Moolenaar845e0ee2020-06-20 16:05:32 +0200403
404 " pressing <C-U> to erase line should keep the indent with 'autoindent'
405 set backspace=2 autoindent
406 %d
407 exe "normal i\tone\<CR>three\<C-U>two"
408 call assert_equal(["\tone", "\ttwo"], getline(1, '$'))
409 set backspace& autoindent&
410
Bram Moolenaar2ba42382019-03-16 18:11:07 +0100411 bwipe!
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100412endfunc
413
Bram Moolenaar1f448d92021-03-22 19:37:06 +0100414" Test for autoindent removing indent when insert mode is stopped. Some parts
415" of the code is exercised only when interactive mode is used. So use Vim in a
416" terminal.
417func Test_autoindent_remove_indent()
418 CheckRunVimInTerminal
419 let buf = RunVimInTerminal('-N Xfile', {'rows': 6, 'cols' : 20})
420 call TermWait(buf)
421 call term_sendkeys(buf, ":set autoindent\n")
422 " leaving insert mode in a new line with indent added by autoindent, should
423 " remove the indent.
424 call term_sendkeys(buf, "i\<Tab>foo\<CR>\<Esc>")
Dominique Pelle923dce22021-11-21 11:36:04 +0000425 " Need to delay for some time, otherwise the code in getchar.c will not be
Bram Moolenaar1f448d92021-03-22 19:37:06 +0100426 " exercised.
427 call TermWait(buf, 50)
428 " when a line is wrapped and the cursor is at the start of the second line,
429 " leaving insert mode, should move the cursor back to the first line.
430 call term_sendkeys(buf, "o" .. repeat('x', 20) .. "\<Esc>")
Dominique Pelle923dce22021-11-21 11:36:04 +0000431 " Need to delay for some time, otherwise the code in getchar.c will not be
Bram Moolenaar1f448d92021-03-22 19:37:06 +0100432 " exercised.
433 call TermWait(buf, 50)
434 call term_sendkeys(buf, ":w\n")
435 call TermWait(buf)
436 call StopVimInTerminal(buf)
437 call assert_equal(["\tfoo", '', repeat('x', 20)], readfile('Xfile'))
438 call delete('Xfile')
439endfunc
440
Bram Moolenaar1e115362019-01-09 23:01:02 +0100441func Test_edit_CR()
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100442 " Test for <CR> in insert mode
Dominique Pelle923dce22021-11-21 11:36:04 +0000443 " basically only in quickfix mode it's tested, the rest
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100444 " has been taken care of by other tests
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200445 CheckFeature quickfix
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100446 botright new
447 call writefile(range(1, 10), 'Xqflist.txt')
448 call setqflist([{'filename': 'Xqflist.txt', 'lnum': 2}])
449 copen
450 set modifiable
451 call feedkeys("A\<cr>", 'tnix')
452 call assert_equal('Xqflist.txt', bufname(''))
453 call assert_equal(2, line('.'))
454 cclose
455 botright new
456 call setloclist(0, [{'filename': 'Xqflist.txt', 'lnum': 10}])
457 lopen
458 set modifiable
459 call feedkeys("A\<cr>", 'tnix')
460 call assert_equal('Xqflist.txt', bufname(''))
461 call assert_equal(10, line('.'))
462 call feedkeys("A\<Enter>", 'tnix')
463 call feedkeys("A\<kEnter>", 'tnix')
464 call feedkeys("A\n", 'tnix')
465 call feedkeys("A\r", 'tnix')
466 call assert_equal(map(range(1, 10), 'string(v:val)') + ['', '', '', ''], getline(1, '$'))
467 bw!
468 lclose
469 call delete('Xqflist.txt')
470endfunc
471
Bram Moolenaar1e115362019-01-09 23:01:02 +0100472func Test_edit_CTRL_()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200473 CheckFeature rightleft
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100474 " disabled for Windows builds, why?
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200475 CheckNotMSWindows
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100476 let _encoding=&encoding
477 set encoding=utf-8
478 " Test for CTRL-_
479 new
480 call setline(1, ['abc'])
481 call cursor(1, 1)
482 call feedkeys("i\<c-_>xyz\<esc>", 'tnix')
483 call assert_equal(["\<C-_>xyzabc"], getline(1, '$'))
484 call assert_false(&revins)
485 set ari
486 call setline(1, ['abc'])
487 call cursor(1, 1)
488 call feedkeys("i\<c-_>xyz\<esc>", 'tnix')
489 call assert_equal(["æèñabc"], getline(1, '$'))
490 call assert_true(&revins)
491 call setline(1, ['abc'])
492 call cursor(1, 1)
493 call feedkeys("i\<c-_>xyz\<esc>", 'tnix')
494 call assert_equal(["xyzabc"], getline(1, '$'))
495 call assert_false(&revins)
496 set noari
497 let &encoding=_encoding
498 bw!
499endfunc
500
501" needs to come first, to have the @. register empty
Bram Moolenaar1e115362019-01-09 23:01:02 +0100502func Test_edit_00a_CTRL_A()
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100503 " Test pressing CTRL-A
504 new
505 call setline(1, repeat([''], 5))
506 call cursor(1, 1)
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100507 try
508 call feedkeys("A\<NUL>", 'tnix')
509 catch /^Vim\%((\a\+)\)\=:E29/
510 call assert_true(1, 'E29 error caught')
511 endtry
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100512 call cursor(1, 1)
513 call feedkeys("Afoobar \<esc>", 'tnix')
514 call cursor(2, 1)
515 call feedkeys("A\<c-a>more\<esc>", 'tnix')
516 call cursor(3, 1)
517 call feedkeys("A\<NUL>and more\<esc>", 'tnix')
518 call assert_equal(['foobar ', 'foobar more', 'foobar morend more', '', ''], getline(1, '$'))
519 bw!
520endfunc
521
Bram Moolenaar1e115362019-01-09 23:01:02 +0100522func Test_edit_CTRL_EY()
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100523 " Ctrl-E/ Ctrl-Y in insert mode completion to scroll
524 10new
525 call setline(1, range(1, 100))
526 call cursor(30, 1)
527 norm! z.
528 call feedkeys("A\<c-x>\<c-e>\<c-e>\<c-e>\<c-e>\<c-e>", 'tnix')
529 call assert_equal(30, winsaveview()['topline'])
530 call assert_equal([0, 30, 2, 0], getpos('.'))
531 call feedkeys("A\<c-x>\<c-e>\<c-e>\<c-e>\<c-e>\<c-e>", 'tnix')
532 call feedkeys("A\<c-x>".repeat("\<c-y>", 10), 'tnix')
533 call assert_equal(21, winsaveview()['topline'])
534 call assert_equal([0, 30, 2, 0], getpos('.'))
535 bw!
536endfunc
537
Bram Moolenaar1e115362019-01-09 23:01:02 +0100538func Test_edit_CTRL_G()
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100539 new
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100540 call setline(1, ['foobar', 'foobar', 'foobar'])
541 call cursor(2, 4)
542 call feedkeys("ioooooooo\<c-g>k\<c-r>.\<esc>", 'tnix')
543 call assert_equal(['foooooooooobar', 'foooooooooobar', 'foobar'], getline(1, '$'))
544 call assert_equal([0, 1, 11, 0], getpos('.'))
545 call feedkeys("i\<c-g>k\<esc>", 'tnix')
546 call assert_equal([0, 1, 10, 0], getpos('.'))
547 call cursor(2, 4)
548 call feedkeys("i\<c-g>jzzzz\<esc>", 'tnix')
549 call assert_equal(['foooooooooobar', 'foooooooooobar', 'foozzzzbar'], getline(1, '$'))
550 call assert_equal([0, 3, 7, 0], getpos('.'))
551 call feedkeys("i\<c-g>j\<esc>", 'tnix')
552 call assert_equal([0, 3, 6, 0], getpos('.'))
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100553 bw!
554endfunc
555
Bram Moolenaar1e115362019-01-09 23:01:02 +0100556func Test_edit_CTRL_I()
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100557 " Tab in completion mode
558 let path=expand("%:p:h")
559 new
Bram Moolenaarca851592018-06-06 21:04:07 +0200560 call setline(1, [path. "/", ''])
Bram Moolenaarc5379472017-03-16 22:38:00 +0100561 call feedkeys("Arunt\<c-x>\<c-f>\<tab>\<cr>\<esc>", 'tnix')
562 call assert_match('runtest\.vim', getline(1))
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100563 %d
564 call writefile(['one', 'two', 'three'], 'Xinclude.txt')
565 let include='#include Xinclude.txt'
566 call setline(1, [include, ''])
567 call cursor(2, 1)
568 call feedkeys("A\<c-x>\<tab>\<cr>\<esc>", 'tnix')
569 call assert_equal([include, 'one', ''], getline(1, '$'))
570 call feedkeys("2ggC\<c-x>\<tab>\<down>\<cr>\<esc>", 'tnix')
571 call assert_equal([include, 'two', ''], getline(1, '$'))
572 call feedkeys("2ggC\<c-x>\<tab>\<down>\<down>\<cr>\<esc>", 'tnix')
573 call assert_equal([include, 'three', ''], getline(1, '$'))
574 call feedkeys("2ggC\<c-x>\<tab>\<down>\<down>\<down>\<cr>\<esc>", 'tnix')
575 call assert_equal([include, '', ''], getline(1, '$'))
576 call delete("Xinclude.txt")
577 bw!
578endfunc
579
Bram Moolenaar1e115362019-01-09 23:01:02 +0100580func Test_edit_CTRL_K()
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100581 " Test pressing CTRL-K (basically only dictionary completion and digraphs
582 " the rest is already covered
583 call writefile(['A', 'AA', 'AAA', 'AAAA'], 'Xdictionary.txt')
584 set dictionary=Xdictionary.txt
585 new
586 call setline(1, 'A')
587 call cursor(1, 1)
588 call feedkeys("A\<c-x>\<c-k>\<cr>\<esc>", 'tnix')
589 call assert_equal(['AA', ''], getline(1, '$'))
590 %d
591 call setline(1, 'A')
592 call cursor(1, 1)
593 call feedkeys("A\<c-x>\<c-k>\<down>\<cr>\<esc>", 'tnix')
594 call assert_equal(['AAA'], getline(1, '$'))
595 %d
596 call setline(1, 'A')
597 call cursor(1, 1)
598 call feedkeys("A\<c-x>\<c-k>\<down>\<down>\<cr>\<esc>", 'tnix')
599 call assert_equal(['AAAA'], getline(1, '$'))
600 %d
601 call setline(1, 'A')
602 call cursor(1, 1)
603 call feedkeys("A\<c-x>\<c-k>\<down>\<down>\<down>\<cr>\<esc>", 'tnix')
604 call assert_equal(['A'], getline(1, '$'))
605 %d
606 call setline(1, 'A')
607 call cursor(1, 1)
608 call feedkeys("A\<c-x>\<c-k>\<down>\<down>\<down>\<down>\<cr>\<esc>", 'tnix')
609 call assert_equal(['AA'], getline(1, '$'))
610
Bram Moolenaar4b96df52020-01-26 22:00:26 +0100611 " press an unexpected key after dictionary completion
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100612 %d
613 call setline(1, 'A')
614 call cursor(1, 1)
615 call feedkeys("A\<c-x>\<c-k>\<c-]>\<cr>\<esc>", 'tnix')
616 call assert_equal(['AA', ''], getline(1, '$'))
617 %d
618 call setline(1, 'A')
619 call cursor(1, 1)
620 call feedkeys("A\<c-x>\<c-k>\<c-s>\<cr>\<esc>", 'tnix')
621 call assert_equal(["AA\<c-s>", ''], getline(1, '$'))
622 %d
623 call setline(1, 'A')
624 call cursor(1, 1)
625 call feedkeys("A\<c-x>\<c-k>\<c-f>\<cr>\<esc>", 'tnix')
626 call assert_equal(["AA\<c-f>", ''], getline(1, '$'))
627
628 set dictionary=
629 %d
630 call setline(1, 'A')
631 call cursor(1, 1)
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100632 let v:testing = 1
633 try
634 call feedkeys("A\<c-x>\<c-k>\<esc>", 'tnix')
635 catch
636 " error sleeps 2 seconds, when v:testing is not set
637 let v:testing = 0
638 endtry
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100639 call delete('Xdictionary.txt')
640
Bram Moolenaar30276f22019-01-24 17:59:39 +0100641 call test_override("char_avail", 1)
642 set showcmd
643 %d
644 call feedkeys("A\<c-k>a:\<esc>", 'tnix')
645 call assert_equal(['ä'], getline(1, '$'))
646 call test_override("char_avail", 0)
647 set noshowcmd
648
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100649 bw!
650endfunc
651
Bram Moolenaar1e115362019-01-09 23:01:02 +0100652func Test_edit_CTRL_L()
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100653 " Test Ctrl-X Ctrl-L (line completion)
654 new
655 set complete=.
656 call setline(1, ['one', 'two', 'three', '', '', '', ''])
657 call cursor(4, 1)
658 call feedkeys("A\<c-x>\<c-l>\<esc>", 'tnix')
659 call assert_equal(['one', 'two', 'three', 'three', '', '', ''], getline(1, '$'))
660 call feedkeys("cct\<c-x>\<c-l>\<c-n>\<esc>", 'tnix')
661 call assert_equal(['one', 'two', 'three', 't', '', '', ''], getline(1, '$'))
662 call feedkeys("cct\<c-x>\<c-l>\<c-n>\<c-n>\<esc>", 'tnix')
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100663 call assert_equal(['one', 'two', 'three', 'two', '', '', ''], getline(1, '$'))
Bram Moolenaar02ae9b42018-02-09 15:06:02 +0100664 call feedkeys("cct\<c-x>\<c-l>\<c-n>\<c-n>\<c-n>\<esc>", 'tnix')
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100665 call assert_equal(['one', 'two', 'three', 'three', '', '', ''], getline(1, '$'))
Bram Moolenaar02ae9b42018-02-09 15:06:02 +0100666 call feedkeys("cct\<c-x>\<c-l>\<c-n>\<c-n>\<c-n>\<c-n>\<esc>", 'tnix')
667 call assert_equal(['one', 'two', 'three', 't', '', '', ''], getline(1, '$'))
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100668 call feedkeys("cct\<c-x>\<c-l>\<c-p>\<esc>", 'tnix')
669 call assert_equal(['one', 'two', 'three', 'two', '', '', ''], getline(1, '$'))
670 call feedkeys("cct\<c-x>\<c-l>\<c-p>\<c-p>\<esc>", 'tnix')
671 call assert_equal(['one', 'two', 'three', 't', '', '', ''], getline(1, '$'))
672 call feedkeys("cct\<c-x>\<c-l>\<c-p>\<c-p>\<c-p>\<esc>", 'tnix')
673 call assert_equal(['one', 'two', 'three', 'three', '', '', ''], getline(1, '$'))
674 set complete=
675 call cursor(5, 1)
676 call feedkeys("A\<c-x>\<c-l>\<c-p>\<c-n>\<esc>", 'tnix')
677 call assert_equal(['one', 'two', 'three', 'three', "\<c-l>\<c-p>\<c-n>", '', ''], getline(1, '$'))
678 set complete&
679 %d
680 if has("conceal") && has("syntax")
681 call setline(1, ['foo', 'bar', 'foobar'])
682 call test_override("char_avail", 1)
683 set conceallevel=2 concealcursor=n
684 syn on
685 syn match ErrorMsg "^bar"
686 call matchadd("Conceal", 'oo', 10, -1, {'conceal': 'X'})
687 func! DoIt()
688 let g:change=1
689 endfunc
690 au! TextChangedI <buffer> :call DoIt()
691
692 call cursor(2, 1)
693 call assert_false(exists("g:change"))
694 call feedkeys("A \<esc>", 'tnix')
695 call assert_equal(['foo', 'bar ', 'foobar'], getline(1, '$'))
696 call assert_equal(1, g:change)
697
698 call test_override("char_avail", 0)
699 call clearmatches()
700 syn off
701 au! TextChangedI
702 delfu DoIt
703 unlet! g:change
704 endif
705 bw!
706endfunc
707
Bram Moolenaar1e115362019-01-09 23:01:02 +0100708func Test_edit_CTRL_N()
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100709 " Check keyword completion
Bram Moolenaara1070ea2021-02-20 19:21:36 +0100710 for e in ['latin1', 'utf-8']
711 exe 'set encoding=' .. e
712 new
713 set complete=.
714 call setline(1, ['INFER', 'loWER', '', '', ])
715 call cursor(3, 1)
716 call feedkeys("Ai\<c-n>\<cr>\<esc>", "tnix")
717 call feedkeys("ILO\<c-n>\<cr>\<esc>", 'tnix')
718 call assert_equal(['INFER', 'loWER', 'i', 'LO', '', ''], getline(1, '$'), e)
719 %d
720 call setline(1, ['INFER', 'loWER', '', '', ])
721 call cursor(3, 1)
722 set ignorecase infercase
723 call feedkeys("Ii\<c-n>\<cr>\<esc>", "tnix")
724 call feedkeys("ILO\<c-n>\<cr>\<esc>", 'tnix')
725 call assert_equal(['INFER', 'loWER', 'infer', 'LOWER', '', ''], getline(1, '$'), e)
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100726
Bram Moolenaara1070ea2021-02-20 19:21:36 +0100727 set noignorecase noinfercase complete&
728 bw!
729 endfor
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100730endfunc
731
Bram Moolenaar1e115362019-01-09 23:01:02 +0100732func Test_edit_CTRL_O()
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100733 " Check for CTRL-O in insert mode
734 new
735 inoreabbr <buffer> h here some more
736 call setline(1, ['abc', 'def'])
737 call cursor(1, 1)
738 " Ctrl-O after an abbreviation
739 exe "norm A h\<c-o>:set nu\<cr> text"
740 call assert_equal(['abc here some more text', 'def'], getline(1, '$'))
741 call assert_true(&nu)
742 set nonu
743 iunabbr <buffer> h
744 " Ctrl-O at end of line with 've'=onemore
745 call cursor(1, 1)
746 call feedkeys("A\<c-o>:let g:a=getpos('.')\<cr>\<esc>", 'tnix')
747 call assert_equal([0, 1, 23, 0], g:a)
748 call cursor(1, 1)
749 set ve=onemore
750 call feedkeys("A\<c-o>:let g:a=getpos('.')\<cr>\<esc>", 'tnix')
751 call assert_equal([0, 1, 24, 0], g:a)
752 set ve=
753 unlet! g:a
754 bw!
755endfunc
756
Bram Moolenaar1e115362019-01-09 23:01:02 +0100757func Test_edit_CTRL_R()
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100758 " Insert Register
759 new
760 call test_override("ALL", 1)
761 set showcmd
762 call feedkeys("AFOOBAR eins zwei\<esc>", 'tnix')
763 call feedkeys("O\<c-r>.", 'tnix')
764 call feedkeys("O\<c-r>=10*500\<cr>\<esc>", 'tnix')
765 call feedkeys("O\<c-r>=getreg('=', 1)\<cr>\<esc>", 'tnix')
766 call assert_equal(["getreg('=', 1)", '5000', "FOOBAR eins zwei", "FOOBAR eins zwei"], getline(1, '$'))
767 call test_override("ALL", 0)
768 set noshowcmd
769 bw!
770endfunc
771
Bram Moolenaar1e115362019-01-09 23:01:02 +0100772func Test_edit_CTRL_S()
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100773 " Test pressing CTRL-S (basically only spellfile completion)
774 " the rest is already covered
775 new
776 if !has("spell")
777 call setline(1, 'vim')
778 call feedkeys("A\<c-x>ss\<cr>\<esc>", 'tnix')
779 call assert_equal(['vims', ''], getline(1, '$'))
780 bw!
781 return
782 endif
783 call setline(1, 'vim')
784 " spell option not yet set
785 try
786 call feedkeys("A\<c-x>\<c-s>\<cr>\<esc>", 'tnix')
787 catch /^Vim\%((\a\+)\)\=:E756/
788 call assert_true(1, 'error caught')
789 endtry
790 call assert_equal(['vim', ''], getline(1, '$'))
791 %d
792 setl spell spelllang=en
793 call setline(1, 'vim')
794 call cursor(1, 1)
795 call feedkeys("A\<c-x>\<c-s>\<cr>\<esc>", 'tnix')
796 call assert_equal(['Vim', ''], getline(1, '$'))
797 %d
798 call setline(1, 'vim')
799 call cursor(1, 1)
800 call feedkeys("A\<c-x>\<c-s>\<down>\<cr>\<esc>", 'tnix')
801 call assert_equal(['Aim'], getline(1, '$'))
802 %d
803 call setline(1, 'vim')
804 call cursor(1, 1)
805 call feedkeys("A\<c-x>\<c-s>\<c-p>\<cr>\<esc>", 'tnix')
806 call assert_equal(['vim', ''], getline(1, '$'))
807 %d
808 " empty buffer
809 call cursor(1, 1)
810 call feedkeys("A\<c-x>\<c-s>\<c-p>\<cr>\<esc>", 'tnix')
811 call assert_equal(['', ''], getline(1, '$'))
812 setl nospell
813 bw!
814endfunc
815
Bram Moolenaar1e115362019-01-09 23:01:02 +0100816func Test_edit_CTRL_T()
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100817 " Check for CTRL-T and CTRL-X CTRL-T in insert mode
818 " 1) increase indent
819 new
820 call setline(1, "abc")
821 call cursor(1, 1)
822 call feedkeys("A\<c-t>xyz", 'tnix')
823 call assert_equal(["\<tab>abcxyz"], getline(1, '$'))
824 " 2) also when paste option is set
825 set paste
826 call setline(1, "abc")
827 call cursor(1, 1)
828 call feedkeys("A\<c-t>xyz", 'tnix')
829 call assert_equal(["\<tab>abcxyz"], getline(1, '$'))
830 set nopaste
831 " CTRL-X CTRL-T (thesaurus complete)
832 call writefile(['angry furious mad enraged'], 'Xthesaurus')
833 set thesaurus=Xthesaurus
834 call setline(1, 'mad')
835 call cursor(1, 1)
836 call feedkeys("A\<c-x>\<c-t>\<cr>\<esc>", 'tnix')
837 call assert_equal(['mad', ''], getline(1, '$'))
838 %d
839 call setline(1, 'mad')
840 call cursor(1, 1)
841 call feedkeys("A\<c-x>\<c-t>\<c-n>\<cr>\<esc>", 'tnix')
842 call assert_equal(['angry', ''], getline(1, '$'))
843 %d
844 call setline(1, 'mad')
845 call cursor(1, 1)
846 call feedkeys("A\<c-x>\<c-t>\<c-n>\<c-n>\<cr>\<esc>", 'tnix')
847 call assert_equal(['furious', ''], getline(1, '$'))
848 %d
849 call setline(1, 'mad')
850 call cursor(1, 1)
851 call feedkeys("A\<c-x>\<c-t>\<c-n>\<c-n>\<c-n>\<cr>\<esc>", 'tnix')
852 call assert_equal(['enraged', ''], getline(1, '$'))
853 %d
854 call setline(1, 'mad')
855 call cursor(1, 1)
856 call feedkeys("A\<c-x>\<c-t>\<c-n>\<c-n>\<c-n>\<c-n>\<cr>\<esc>", 'tnix')
857 call assert_equal(['mad', ''], getline(1, '$'))
858 %d
859 call setline(1, 'mad')
860 call cursor(1, 1)
861 call feedkeys("A\<c-x>\<c-t>\<c-n>\<c-n>\<c-n>\<c-n>\<c-n>\<cr>\<esc>", 'tnix')
862 call assert_equal(['mad', ''], getline(1, '$'))
863 " Using <c-p> <c-n> when 'complete' is empty
864 set complete=
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-p>\<cr>\<esc>", 'tnix')
874 call assert_equal(['mad', ''], getline(1, '$'))
875 set complete&
876
877 set thesaurus=
878 %d
879 call setline(1, 'mad')
880 call cursor(1, 1)
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100881 let v:testing = 1
882 try
883 call feedkeys("A\<c-x>\<c-t>\<esc>", 'tnix')
884 catch
885 " error sleeps 2 seconds, when v:testing is not set
886 let v:testing = 0
887 endtry
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100888 call assert_equal(['mad'], getline(1, '$'))
889 call delete('Xthesaurus')
890 bw!
891endfunc
892
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +0100893" Test 'thesaurusfunc'
894func MyThesaurus(findstart, base)
895 let mythesaurus = [
896 \ #{word: "happy",
897 \ synonyms: "cheerful,blissful,flying high,looking good,peppy"},
898 \ #{word: "kind",
899 \ synonyms: "amiable,bleeding-heart,heart in right place"}]
900 if a:findstart
901 " locate the start of the word
902 let line = getline('.')
903 let start = col('.') - 1
904 while start > 0 && line[start - 1] =~ '\a'
905 let start -= 1
906 endwhile
907 return start
908 else
909 " find strings matching with "a:base"
910 let res = []
911 for w in mythesaurus
912 if w.word =~ '^' . a:base
913 call add(res, w.word)
914 call extend(res, split(w.synonyms, ","))
915 endif
916 endfor
917 return res
918 endif
919endfunc
920
921func Test_thesaurus_func()
922 new
Bram Moolenaarf4d8b762021-10-17 14:13:09 +0100923 set thesaurus=notused
924 set thesaurusfunc=NotUsed
925 setlocal thesaurusfunc=MyThesaurus
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +0100926 call setline(1, "an ki")
927 call cursor(1, 1)
928 call feedkeys("A\<c-x>\<c-t>\<c-n>\<cr>\<esc>", 'tnix')
929 call assert_equal(['an amiable', ''], getline(1, '$'))
Bram Moolenaarf4d8b762021-10-17 14:13:09 +0100930
931 setlocal thesaurusfunc=NonExistingFunc
932 call assert_fails("normal $a\<C-X>\<C-T>", 'E117:')
933
934 setlocal thesaurusfunc=
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +0100935 set thesaurusfunc=NonExistingFunc
936 call assert_fails("normal $a\<C-X>\<C-T>", 'E117:')
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +0100937 %bw!
Bram Moolenaarf4d8b762021-10-17 14:13:09 +0100938
939 set thesaurusfunc=
940 set thesaurus=
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +0100941endfunc
942
Bram Moolenaar1e115362019-01-09 23:01:02 +0100943func Test_edit_CTRL_U()
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100944 " Test 'completefunc'
945 new
946 " -1, -2 and -3 are special return values
947 let g:special=0
948 fun! CompleteMonths(findstart, base)
949 if a:findstart
950 " locate the start of the word
951 return g:special
952 else
953 " find months matching with "a:base"
954 let res = []
955 for m in split("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec")
956 if m =~ '^\c'.a:base
957 call add(res, {'word': m, 'abbr': m.' Month', 'icase': 0})
958 endif
959 endfor
960 return {'words': res, 'refresh': 'always'}
961 endif
962 endfun
963 set completefunc=CompleteMonths
964 call setline(1, ['', ''])
965 call cursor(1, 1)
966 call feedkeys("AX\<c-x>\<c-u>\<cr>\<esc>", 'tnix')
967 call assert_equal(['X', '', ''], getline(1, '$'))
968 %d
969 let g:special=-1
970 call feedkeys("AX\<c-x>\<c-u>\<cr>\<esc>", 'tnix')
971 call assert_equal(['XJan', ''], getline(1, '$'))
972 %d
973 let g:special=-2
974 call feedkeys("AX\<c-x>\<c-u>\<cr>\<esc>", 'tnix')
975 call assert_equal(['X', ''], getline(1, '$'))
976 %d
977 let g:special=-3
978 call feedkeys("AX\<c-x>\<c-u>\<cr>\<esc>", 'tnix')
979 call assert_equal(['X', ''], getline(1, '$'))
980 %d
981 let g:special=0
982 call feedkeys("AM\<c-x>\<c-u>\<cr>\<esc>", 'tnix')
983 call assert_equal(['Mar', ''], getline(1, '$'))
984 %d
985 call feedkeys("AM\<c-x>\<c-u>\<c-n>\<cr>\<esc>", 'tnix')
986 call assert_equal(['May', ''], getline(1, '$'))
987 %d
988 call feedkeys("AM\<c-x>\<c-u>\<c-n>\<c-n>\<cr>\<esc>", 'tnix')
989 call assert_equal(['M', ''], getline(1, '$'))
990 delfu CompleteMonths
991 %d
992 try
993 call feedkeys("A\<c-x>\<c-u>", 'tnix')
994 call assert_fails(1, 'unknown completion function')
995 catch /^Vim\%((\a\+)\)\=:E117/
996 call assert_true(1, 'E117 error caught')
997 endtry
998 set completefunc=
999 bw!
1000endfunc
1001
Bram Moolenaarff06f282020-04-21 22:01:14 +02001002func Test_edit_completefunc_delete()
1003 func CompleteFunc(findstart, base)
1004 if a:findstart == 1
1005 return col('.') - 1
1006 endif
1007 normal dd
1008 return ['a', 'b']
1009 endfunc
1010 new
1011 set completefunc=CompleteFunc
1012 call setline(1, ['', 'abcd', ''])
1013 2d
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02001014 call assert_fails("normal 2G$a\<C-X>\<C-U>", 'E578:')
Bram Moolenaarff06f282020-04-21 22:01:14 +02001015 bwipe!
1016endfunc
1017
1018
Bram Moolenaar1e115362019-01-09 23:01:02 +01001019func Test_edit_CTRL_Z()
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001020 " Ctrl-Z when insertmode is not set inserts it literally
1021 new
1022 call setline(1, 'abc')
1023 call feedkeys("A\<c-z>\<esc>", 'tnix')
1024 call assert_equal(["abc\<c-z>"], getline(1,'$'))
1025 bw!
1026 " TODO: How to Test Ctrl-Z in insert mode, e.g. suspend?
1027endfunc
1028
Bram Moolenaar1e115362019-01-09 23:01:02 +01001029func Test_edit_DROP()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001030 CheckFeature dnd
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001031 new
1032 call setline(1, ['abc def ghi'])
1033 call cursor(1, 1)
1034 try
1035 call feedkeys("i\<Drop>\<Esc>", 'tnix')
1036 call assert_fails(1, 'Invalid register name')
1037 catch /^Vim\%((\a\+)\)\=:E353/
1038 call assert_true(1, 'error caught')
1039 endtry
1040 bw!
1041endfunc
1042
Bram Moolenaar1e115362019-01-09 23:01:02 +01001043func Test_edit_CTRL_V()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001044 CheckFeature ebcdic
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001045 new
1046 call setline(1, ['abc'])
1047 call cursor(2, 1)
1048 " force some redraws
1049 set showmode showcmd
1050 "call test_override_char_avail(1)
1051 call test_override('ALL', 1)
1052 call feedkeys("A\<c-v>\<c-n>\<c-v>\<c-l>\<c-v>\<c-b>\<esc>", 'tnix')
1053 call assert_equal(["abc\x0e\x0c\x02"], getline(1, '$'))
1054
1055 if has("rightleft") && exists("+rl")
1056 set rl
1057 call setline(1, ['abc'])
1058 call cursor(2, 1)
1059 call feedkeys("A\<c-v>\<c-n>\<c-v>\<c-l>\<c-v>\<c-b>\<esc>", 'tnix')
1060 call assert_equal(["abc\x0e\x0c\x02"], getline(1, '$'))
1061 set norl
1062 endif
1063
1064 call test_override('ALL', 0)
1065 set noshowmode showcmd
1066 bw!
1067endfunc
1068
Bram Moolenaar1e115362019-01-09 23:01:02 +01001069func Test_edit_F1()
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01001070 CheckFeature quickfix
1071
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001072 " Pressing <f1>
1073 new
1074 call feedkeys(":set im\<cr>\<f1>\<c-l>", 'tnix')
1075 set noinsertmode
1076 call assert_equal('help', &buftype)
1077 bw
1078 bw
1079endfunc
1080
Bram Moolenaar1e115362019-01-09 23:01:02 +01001081func Test_edit_F21()
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001082 " Pressing <f21>
1083 " sends a netbeans command
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001084 CheckFeature netbeans_intg
1085 new
1086 " I have no idea what this is supposed to do :)
1087 call feedkeys("A\<F21>\<F1>\<esc>", 'tnix')
1088 bw
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001089endfunc
1090
Bram Moolenaar1e115362019-01-09 23:01:02 +01001091func Test_edit_HOME_END()
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001092 " Test Home/End Keys
1093 new
1094 set foldopen+=hor
1095 call setline(1, ['abc', 'def'])
1096 call cursor(1, 1)
1097 call feedkeys("AX\<Home>Y\<esc>", 'tnix')
1098 call cursor(2, 1)
1099 call feedkeys("iZ\<End>Y\<esc>", 'tnix')
1100 call assert_equal(['YabcX', 'ZdefY'], getline(1, '$'))
1101
1102 set foldopen-=hor
1103 bw!
1104endfunc
1105
Bram Moolenaar1e115362019-01-09 23:01:02 +01001106func Test_edit_INS()
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001107 " Test for Pressing <Insert>
1108 new
1109 call setline(1, ['abc', 'def'])
1110 call cursor(1, 1)
1111 call feedkeys("i\<Insert>ZYX>", 'tnix')
1112 call assert_equal(['ZYX>', 'def'], getline(1, '$'))
1113 call setline(1, ['abc', 'def'])
1114 call cursor(1, 1)
1115 call feedkeys("i\<Insert>Z\<Insert>YX>", 'tnix')
1116 call assert_equal(['ZYX>bc', 'def'], getline(1, '$'))
1117 bw!
1118endfunc
1119
Bram Moolenaar1e115362019-01-09 23:01:02 +01001120func Test_edit_LEFT_RIGHT()
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001121 " Left, Shift-Left, Right, Shift-Right
1122 new
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001123 call setline(1, ['abc def ghi', 'ABC DEF GHI', 'ZZZ YYY XXX'])
1124 let _ww=&ww
1125 set ww=
1126 call cursor(2, 1)
1127 call feedkeys("i\<left>\<esc>", 'tnix')
1128 call assert_equal([0, 2, 1, 0], getpos('.'))
1129 " Is this a bug, <s-left> does not respect whichwrap option
1130 call feedkeys("i\<s-left>\<esc>", 'tnix')
1131 call assert_equal([0, 1, 8, 0], getpos('.'))
1132 call feedkeys("i". repeat("\<s-left>", 3). "\<esc>", 'tnix')
1133 call assert_equal([0, 1, 1, 0], getpos('.'))
1134 call feedkeys("i\<right>\<esc>", 'tnix')
1135 call assert_equal([0, 1, 1, 0], getpos('.'))
1136 call feedkeys("i\<right>\<right>\<esc>", 'tnix')
1137 call assert_equal([0, 1, 2, 0], getpos('.'))
1138 call feedkeys("A\<right>\<esc>", 'tnix')
1139 call assert_equal([0, 1, 11, 0], getpos('.'))
1140 call feedkeys("A\<s-right>\<esc>", 'tnix')
1141 call assert_equal([0, 2, 1, 0], getpos('.'))
1142 call feedkeys("i\<s-right>\<esc>", 'tnix')
1143 call assert_equal([0, 2, 4, 0], getpos('.'))
1144 call cursor(3, 11)
1145 call feedkeys("A\<right>\<esc>", 'tnix')
1146 call feedkeys("A\<s-right>\<esc>", 'tnix')
1147 call assert_equal([0, 3, 11, 0], getpos('.'))
1148 call cursor(2, 11)
1149 " <S-Right> does not respect 'whichwrap' option
1150 call feedkeys("A\<s-right>\<esc>", 'tnix')
1151 call assert_equal([0, 3, 1, 0], getpos('.'))
1152 " Check motion when 'whichwrap' contains cursor keys for insert mode
1153 set ww+=[,]
1154 call cursor(2, 1)
1155 call feedkeys("i\<left>\<esc>", 'tnix')
1156 call assert_equal([0, 1, 11, 0], getpos('.'))
1157 call cursor(2, 11)
1158 call feedkeys("A\<right>\<esc>", 'tnix')
1159 call assert_equal([0, 3, 1, 0], getpos('.'))
1160 call cursor(2, 11)
1161 call feedkeys("A\<s-right>\<esc>", 'tnix')
1162 call assert_equal([0, 3, 1, 0], getpos('.'))
1163 let &ww = _ww
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001164 bw!
1165endfunc
1166
Bram Moolenaar1e115362019-01-09 23:01:02 +01001167func Test_edit_MOUSE()
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001168 " This is a simple test, since we not really using the mouse here
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001169 CheckFeature mouse
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001170 10new
1171 call setline(1, range(1, 100))
1172 call cursor(1, 1)
1173 set mouse=a
1174 call feedkeys("A\<ScrollWheelDown>\<esc>", 'tnix')
1175 call assert_equal([0, 4, 1, 0], getpos('.'))
1176 " This should move by one pageDown, but only moves
1177 " by one line when the test is run...
1178 call feedkeys("A\<S-ScrollWheelDown>\<esc>", 'tnix')
1179 call assert_equal([0, 5, 1, 0], getpos('.'))
1180 set nostartofline
1181 call feedkeys("A\<C-ScrollWheelDown>\<esc>", 'tnix')
1182 call assert_equal([0, 6, 1, 0], getpos('.'))
1183 call feedkeys("A\<LeftMouse>\<esc>", 'tnix')
1184 call assert_equal([0, 6, 1, 0], getpos('.'))
1185 call feedkeys("A\<RightMouse>\<esc>", 'tnix')
1186 call assert_equal([0, 6, 1, 0], getpos('.'))
1187 call cursor(1, 100)
1188 norm! zt
1189 " this should move by a screen up, but when the test
1190 " is run, it moves up to the top of the buffer...
1191 call feedkeys("A\<ScrollWheelUp>\<esc>", 'tnix')
1192 call assert_equal([0, 1, 1, 0], getpos('.'))
1193 call cursor(1, 30)
1194 norm! zt
1195 call feedkeys("A\<S-ScrollWheelUp>\<esc>", 'tnix')
1196 call assert_equal([0, 1, 1, 0], getpos('.'))
1197 call cursor(1, 30)
1198 norm! zt
1199 call feedkeys("A\<C-ScrollWheelUp>\<esc>", 'tnix')
1200 call assert_equal([0, 1, 1, 0], getpos('.'))
1201 %d
1202 call setline(1, repeat(["12345678901234567890"], 100))
1203 call cursor(2, 1)
1204 call feedkeys("A\<ScrollWheelRight>\<esc>", 'tnix')
1205 call assert_equal([0, 2, 20, 0], getpos('.'))
1206 call feedkeys("A\<ScrollWheelLeft>\<esc>", 'tnix')
1207 call assert_equal([0, 2, 20, 0], getpos('.'))
1208 call feedkeys("A\<S-ScrollWheelRight>\<esc>", 'tnix')
1209 call assert_equal([0, 2, 20, 0], getpos('.'))
1210 call feedkeys("A\<S-ScrollWheelLeft>\<esc>", 'tnix')
1211 call assert_equal([0, 2, 20, 0], getpos('.'))
1212 call feedkeys("A\<C-ScrollWheelRight>\<esc>", 'tnix')
1213 call assert_equal([0, 2, 20, 0], getpos('.'))
1214 call feedkeys("A\<C-ScrollWheelLeft>\<esc>", 'tnix')
1215 call assert_equal([0, 2, 20, 0], getpos('.'))
1216 set mouse& startofline
1217 bw!
1218endfunc
1219
Bram Moolenaar1e115362019-01-09 23:01:02 +01001220func Test_edit_PAGEUP_PAGEDOWN()
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001221 10new
1222 call setline(1, repeat(['abc def ghi'], 30))
1223 call cursor(1, 1)
1224 call feedkeys("i\<PageDown>\<esc>", 'tnix')
1225 call assert_equal([0, 9, 1, 0], getpos('.'))
1226 call feedkeys("i\<PageDown>\<esc>", 'tnix')
1227 call assert_equal([0, 17, 1, 0], getpos('.'))
1228 call feedkeys("i\<PageDown>\<esc>", 'tnix')
1229 call assert_equal([0, 25, 1, 0], getpos('.'))
1230 call feedkeys("i\<PageDown>\<esc>", 'tnix')
1231 call assert_equal([0, 30, 1, 0], getpos('.'))
1232 call feedkeys("i\<PageDown>\<esc>", 'tnix')
1233 call assert_equal([0, 30, 1, 0], getpos('.'))
1234 call feedkeys("A\<PageUp>\<esc>", 'tnix')
1235 call assert_equal([0, 29, 1, 0], getpos('.'))
1236 call feedkeys("A\<PageUp>\<esc>", 'tnix')
1237 call assert_equal([0, 21, 1, 0], getpos('.'))
1238 call feedkeys("A\<PageUp>\<esc>", 'tnix')
1239 call assert_equal([0, 13, 1, 0], getpos('.'))
1240 call feedkeys("A\<PageUp>\<esc>", 'tnix')
1241 call assert_equal([0, 5, 1, 0], getpos('.'))
1242 call feedkeys("A\<PageUp>\<esc>", 'tnix')
1243 call assert_equal([0, 5, 11, 0], getpos('.'))
1244 " <S-Up> is the same as <PageUp>
1245 " <S-Down> is the same as <PageDown>
1246 call cursor(1, 1)
1247 call feedkeys("i\<S-Down>\<esc>", 'tnix')
1248 call assert_equal([0, 9, 1, 0], getpos('.'))
1249 call feedkeys("i\<S-Down>\<esc>", 'tnix')
1250 call assert_equal([0, 17, 1, 0], getpos('.'))
1251 call feedkeys("i\<S-Down>\<esc>", 'tnix')
1252 call assert_equal([0, 25, 1, 0], getpos('.'))
1253 call feedkeys("i\<S-Down>\<esc>", 'tnix')
1254 call assert_equal([0, 30, 1, 0], getpos('.'))
1255 call feedkeys("i\<S-Down>\<esc>", 'tnix')
1256 call assert_equal([0, 30, 1, 0], getpos('.'))
1257 call feedkeys("A\<S-Up>\<esc>", 'tnix')
1258 call assert_equal([0, 29, 1, 0], getpos('.'))
1259 call feedkeys("A\<S-Up>\<esc>", 'tnix')
1260 call assert_equal([0, 21, 1, 0], getpos('.'))
1261 call feedkeys("A\<S-Up>\<esc>", 'tnix')
1262 call assert_equal([0, 13, 1, 0], getpos('.'))
1263 call feedkeys("A\<S-Up>\<esc>", 'tnix')
1264 call assert_equal([0, 5, 1, 0], getpos('.'))
1265 call feedkeys("A\<S-Up>\<esc>", 'tnix')
1266 call assert_equal([0, 5, 11, 0], getpos('.'))
1267 set nostartofline
1268 call cursor(30, 11)
1269 norm! zt
1270 call feedkeys("A\<PageUp>\<esc>", 'tnix')
1271 call assert_equal([0, 29, 11, 0], getpos('.'))
1272 call feedkeys("A\<PageUp>\<esc>", 'tnix')
1273 call assert_equal([0, 21, 11, 0], getpos('.'))
1274 call feedkeys("A\<PageUp>\<esc>", 'tnix')
1275 call assert_equal([0, 13, 11, 0], getpos('.'))
1276 call feedkeys("A\<PageUp>\<esc>", 'tnix')
1277 call assert_equal([0, 5, 11, 0], getpos('.'))
1278 call feedkeys("A\<PageUp>\<esc>", 'tnix')
1279 call assert_equal([0, 5, 11, 0], getpos('.'))
1280 call cursor(1, 1)
1281 call feedkeys("A\<PageDown>\<esc>", 'tnix')
1282 call assert_equal([0, 9, 11, 0], getpos('.'))
1283 call feedkeys("A\<PageDown>\<esc>", 'tnix')
1284 call assert_equal([0, 17, 11, 0], getpos('.'))
1285 call feedkeys("A\<PageDown>\<esc>", 'tnix')
1286 call assert_equal([0, 25, 11, 0], getpos('.'))
1287 call feedkeys("A\<PageDown>\<esc>", 'tnix')
1288 call assert_equal([0, 30, 11, 0], getpos('.'))
1289 call feedkeys("A\<PageDown>\<esc>", 'tnix')
1290 call assert_equal([0, 30, 11, 0], getpos('.'))
1291 " <S-Up> is the same as <PageUp>
1292 " <S-Down> is the same as <PageDown>
1293 call cursor(30, 11)
1294 norm! zt
1295 call feedkeys("A\<S-Up>\<esc>", 'tnix')
1296 call assert_equal([0, 29, 11, 0], getpos('.'))
1297 call feedkeys("A\<S-Up>\<esc>", 'tnix')
1298 call assert_equal([0, 21, 11, 0], getpos('.'))
1299 call feedkeys("A\<S-Up>\<esc>", 'tnix')
1300 call assert_equal([0, 13, 11, 0], getpos('.'))
1301 call feedkeys("A\<S-Up>\<esc>", 'tnix')
1302 call assert_equal([0, 5, 11, 0], getpos('.'))
1303 call feedkeys("A\<S-Up>\<esc>", 'tnix')
1304 call assert_equal([0, 5, 11, 0], getpos('.'))
1305 call cursor(1, 1)
1306 call feedkeys("A\<S-Down>\<esc>", 'tnix')
1307 call assert_equal([0, 9, 11, 0], getpos('.'))
1308 call feedkeys("A\<S-Down>\<esc>", 'tnix')
1309 call assert_equal([0, 17, 11, 0], getpos('.'))
1310 call feedkeys("A\<S-Down>\<esc>", 'tnix')
1311 call assert_equal([0, 25, 11, 0], getpos('.'))
1312 call feedkeys("A\<S-Down>\<esc>", 'tnix')
1313 call assert_equal([0, 30, 11, 0], getpos('.'))
1314 call feedkeys("A\<S-Down>\<esc>", 'tnix')
1315 call assert_equal([0, 30, 11, 0], getpos('.'))
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001316 bw!
1317endfunc
1318
Bram Moolenaar1e115362019-01-09 23:01:02 +01001319func Test_edit_forbidden()
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001320 new
1321 " 1) edit in the sandbox is not allowed
1322 call setline(1, 'a')
1323 com! Sandbox :sandbox call feedkeys("i\<del>\<esc>", 'tnix')
1324 call assert_fails(':Sandbox', 'E48:')
1325 com! Sandbox :sandbox exe "norm! i\<del>"
1326 call assert_fails(':Sandbox', 'E48:')
1327 delcom Sandbox
1328 call assert_equal(['a'], getline(1,'$'))
Bram Moolenaar52797ba2021-12-16 14:45:13 +00001329
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001330 " 2) edit with textlock set
1331 fu! DoIt()
1332 call feedkeys("i\<del>\<esc>", 'tnix')
1333 endfu
1334 au InsertCharPre <buffer> :call DoIt()
1335 try
1336 call feedkeys("ix\<esc>", 'tnix')
1337 call assert_fails(1, 'textlock')
Bram Moolenaarff06f282020-04-21 22:01:14 +02001338 catch /^Vim\%((\a\+)\)\=:E565/ " catch E565: not allowed here
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001339 endtry
1340 " TODO: Might be a bug: should x really be inserted here
1341 call assert_equal(['xa'], getline(1, '$'))
1342 delfu DoIt
1343 try
1344 call feedkeys("ix\<esc>", 'tnix')
1345 call assert_fails(1, 'unknown function')
1346 catch /^Vim\%((\a\+)\)\=:E117/ " catch E117: unknown function
1347 endtry
1348 au! InsertCharPre
Bram Moolenaar52797ba2021-12-16 14:45:13 +00001349
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001350 " 3) edit when completion is shown
1351 fun! Complete(findstart, base)
1352 if a:findstart
1353 return col('.')
1354 else
1355 call feedkeys("i\<del>\<esc>", 'tnix')
1356 return []
1357 endif
1358 endfun
1359 set completefunc=Complete
1360 try
1361 call feedkeys("i\<c-x>\<c-u>\<esc>", 'tnix')
1362 call assert_fails(1, 'change in complete function')
Bram Moolenaarff06f282020-04-21 22:01:14 +02001363 catch /^Vim\%((\a\+)\)\=:E565/ " catch E565
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001364 endtry
1365 delfu Complete
1366 set completefunc=
Bram Moolenaar52797ba2021-12-16 14:45:13 +00001367
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001368 if has("rightleft") && exists("+fkmap")
1369 " 4) 'R' when 'fkmap' and 'revins' is set.
1370 set revins fkmap
1371 try
1372 normal Ri
1373 call assert_fails(1, "R with 'fkmap' and 'ri' set")
1374 catch
1375 finally
1376 set norevins nofkmap
1377 endtry
1378 endif
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001379 bw!
1380endfunc
1381
Bram Moolenaar1e115362019-01-09 23:01:02 +01001382func Test_edit_rightleft()
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001383 " Cursor in rightleft mode moves differently
Bram Moolenaar845e0ee2020-06-20 16:05:32 +02001384 CheckFeature rightleft
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001385 call NewWindow(10, 20)
1386 call setline(1, ['abc', 'def', 'ghi'])
1387 call cursor(1, 2)
1388 set rightleft
1389 " Screen looks as expected
1390 let lines = ScreenLines([1, 4], winwidth(0))
1391 let expect = [
1392 \" cba",
1393 \" fed",
1394 \" ihg",
1395 \" ~"]
1396 call assert_equal(join(expect, "\n"), join(lines, "\n"))
1397 " 2) right moves to the left
1398 call feedkeys("i\<right>\<esc>x", 'txin')
1399 call assert_equal(['bc', 'def', 'ghi'], getline(1,'$'))
1400 call cursor(1, 2)
1401 call feedkeys("i\<s-right>\<esc>", 'txin')
1402 call cursor(1, 2)
1403 call feedkeys("i\<c-right>\<esc>", 'txin')
1404 " Screen looks as expected
1405 let lines = ScreenLines([1, 4], winwidth(0))
1406 let expect = [
1407 \" cb",
1408 \" fed",
1409 \" ihg",
1410 \" ~"]
1411 call assert_equal(join(expect, "\n"), join(lines, "\n"))
1412 " 2) left moves to the right
1413 call setline(1, ['abc', 'def', 'ghi'])
1414 call cursor(1, 2)
1415 call feedkeys("i\<left>\<esc>x", 'txin')
1416 call assert_equal(['ac', 'def', 'ghi'], getline(1,'$'))
1417 call cursor(1, 2)
1418 call feedkeys("i\<s-left>\<esc>", 'txin')
1419 call cursor(1, 2)
1420 call feedkeys("i\<c-left>\<esc>", 'txin')
1421 " Screen looks as expected
1422 let lines = ScreenLines([1, 4], winwidth(0))
1423 let expect = [
1424 \" ca",
1425 \" fed",
1426 \" ihg",
1427 \" ~"]
1428 call assert_equal(join(expect, "\n"), join(lines, "\n"))
Bram Moolenaar845e0ee2020-06-20 16:05:32 +02001429 %d _
1430 call test_override('redraw_flag', 1)
1431 call test_override('char_avail', 1)
1432 call feedkeys("a\<C-V>x41", "xt")
1433 redraw!
1434 call assert_equal(repeat(' ', 19) .. 'A', Screenline(1))
1435 call test_override('ALL', 0)
Bram Moolenaareb992cb2017-03-09 18:20:16 +01001436 set norightleft
1437 bw!
1438endfunc
Bram Moolenaar658a3a22017-03-31 22:27:12 +02001439
1440func Test_edit_complete_very_long_name()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001441 " Long directory names only work on Unix.
1442 CheckUnix
Bram Moolenaar15ecbd62017-04-07 14:10:48 +02001443
1444 let dirname = getcwd() . "/Xdir"
1445 let longdirname = dirname . repeat('/' . repeat('d', 255), 4)
1446 try
1447 call mkdir(longdirname, 'p')
1448 catch /E739:/
1449 " Long directory name probably not supported.
1450 call delete(dirname, 'rf')
1451 return
1452 endtry
1453
Bram Moolenaarf8191c52019-05-18 17:22:54 +02001454 " Try to get the Vim window position before setting 'columns', so that we can
1455 " move the window back to where it was.
Bram Moolenaarba6ec182017-04-04 22:41:10 +02001456 let winposx = getwinposx()
1457 let winposy = getwinposy()
Bram Moolenaarf8191c52019-05-18 17:22:54 +02001458
1459 if winposx >= 0 && winposy >= 0 && !has('gui_running')
1460 " We did get the window position, but xterm may report the wrong numbers.
1461 " Move the window to the reported position and compute any offset.
1462 exe 'winpos ' . winposx . ' ' . winposy
1463 sleep 100m
1464 let x = getwinposx()
1465 if x >= 0
1466 let winposx += winposx - x
1467 endif
1468 let y = getwinposy()
1469 if y >= 0
1470 let winposy += winposy - y
1471 endif
1472 endif
1473
Bram Moolenaar658a3a22017-03-31 22:27:12 +02001474 let save_columns = &columns
Bram Moolenaar15ecbd62017-04-07 14:10:48 +02001475 " Need at least about 1100 columns to reproduce the problem.
Bram Moolenaarba6ec182017-04-04 22:41:10 +02001476 set columns=2000
Bram Moolenaar658a3a22017-03-31 22:27:12 +02001477 set noswapfile
Bram Moolenaarba6ec182017-04-04 22:41:10 +02001478
Bram Moolenaar658a3a22017-03-31 22:27:12 +02001479 let longfilename = longdirname . '/' . repeat('a', 255)
Bram Moolenaar658a3a22017-03-31 22:27:12 +02001480 call writefile(['Totum', 'Table'], longfilename)
1481 new
1482 exe "next Xfile " . longfilename
1483 exe "normal iT\<C-N>"
1484
1485 bwipe!
1486 exe 'bwipe! ' . longfilename
1487 call delete(dirname, 'rf')
1488 let &columns = save_columns
Bram Moolenaarba6ec182017-04-04 22:41:10 +02001489 if winposx >= 0 && winposy >= 0
1490 exe 'winpos ' . winposx . ' ' . winposy
1491 endif
Bram Moolenaar658a3a22017-03-31 22:27:12 +02001492 set swapfile&
1493endfunc
Bram Moolenaarff930ca2017-10-19 17:12:10 +02001494
Bram Moolenaar2c8c6812018-07-28 17:07:52 +02001495func Test_edit_backtick()
1496 next a\`b c
1497 call assert_equal('a`b', expand('%'))
1498 next
1499 call assert_equal('c', expand('%'))
1500 call assert_equal('a\`b c', expand('##'))
1501endfunc
1502
Bram Moolenaarff930ca2017-10-19 17:12:10 +02001503func Test_edit_quit()
1504 edit foo.txt
1505 split
1506 new
1507 call setline(1, 'hello')
1508 3wincmd w
1509 redraw!
1510 call assert_fails('1q', 'E37:')
1511 bwipe! foo.txt
1512 only
1513endfunc
1514
Bram Moolenaaradb8fbe2018-06-04 20:34:23 +02001515func Test_edit_alt()
1516 " Keeping the cursor line didn't happen when the first line has indent.
1517 new
1518 call setline(1, [' one', 'two', 'three'])
1519 w XAltFile
1520 $
1521 call assert_equal(3, line('.'))
1522 e Xother
1523 e #
1524 call assert_equal(3, line('.'))
1525
1526 bwipe XAltFile
1527 call delete('XAltFile')
1528endfunc
Bram Moolenaar4dbc2622018-11-02 11:59:15 +01001529
Bram Moolenaardb934952020-04-27 20:18:31 +02001530func Test_edit_InsertLeave()
Bram Moolenaar4dbc2622018-11-02 11:59:15 +01001531 new
Bram Moolenaarb53e13a2020-10-21 12:19:53 +02001532 au InsertLeavePre * let g:did_au_pre = 1
Bram Moolenaar4dbc2622018-11-02 11:59:15 +01001533 au InsertLeave * let g:did_au = 1
Bram Moolenaarb53e13a2020-10-21 12:19:53 +02001534 let g:did_au_pre = 0
Bram Moolenaar4dbc2622018-11-02 11:59:15 +01001535 let g:did_au = 0
1536 call feedkeys("afoo\<Esc>", 'tx')
Bram Moolenaarb53e13a2020-10-21 12:19:53 +02001537 call assert_equal(1, g:did_au_pre)
Bram Moolenaar4dbc2622018-11-02 11:59:15 +01001538 call assert_equal(1, g:did_au)
1539 call assert_equal('foo', getline(1))
1540
Bram Moolenaarb53e13a2020-10-21 12:19:53 +02001541 let g:did_au_pre = 0
Bram Moolenaar4dbc2622018-11-02 11:59:15 +01001542 let g:did_au = 0
1543 call feedkeys("Sbar\<C-C>", 'tx')
Bram Moolenaarb53e13a2020-10-21 12:19:53 +02001544 call assert_equal(1, g:did_au_pre)
Bram Moolenaar4dbc2622018-11-02 11:59:15 +01001545 call assert_equal(0, g:did_au)
1546 call assert_equal('bar', getline(1))
1547
1548 inoremap x xx<Esc>
Bram Moolenaarb53e13a2020-10-21 12:19:53 +02001549 let g:did_au_pre = 0
Bram Moolenaar4dbc2622018-11-02 11:59:15 +01001550 let g:did_au = 0
1551 call feedkeys("Saax", 'tx')
Bram Moolenaarb53e13a2020-10-21 12:19:53 +02001552 call assert_equal(1, g:did_au_pre)
Bram Moolenaar4dbc2622018-11-02 11:59:15 +01001553 call assert_equal(1, g:did_au)
1554 call assert_equal('aaxx', getline(1))
1555
1556 inoremap x xx<C-C>
Bram Moolenaarb53e13a2020-10-21 12:19:53 +02001557 let g:did_au_pre = 0
Bram Moolenaar4dbc2622018-11-02 11:59:15 +01001558 let g:did_au = 0
1559 call feedkeys("Sbbx", 'tx')
Bram Moolenaarb53e13a2020-10-21 12:19:53 +02001560 call assert_equal(1, g:did_au_pre)
Bram Moolenaar4dbc2622018-11-02 11:59:15 +01001561 call assert_equal(0, g:did_au)
1562 call assert_equal('bbxx', getline(1))
1563
1564 bwipe!
Bram Moolenaarb53e13a2020-10-21 12:19:53 +02001565 au! InsertLeave InsertLeavePre
Bram Moolenaar4dbc2622018-11-02 11:59:15 +01001566 iunmap x
1567endfunc
Bram Moolenaarc6b37db2019-04-27 18:00:34 +02001568
Bram Moolenaardb934952020-04-27 20:18:31 +02001569func Test_edit_InsertLeave_undo()
1570 new XtestUndo
1571 set undofile
1572 au InsertLeave * wall
1573 exe "normal ofoo\<Esc>"
1574 call assert_equal(2, line('$'))
1575 normal u
1576 call assert_equal(1, line('$'))
1577
1578 bwipe!
1579 au! InsertLeave
1580 call delete('XtestUndo')
Bram Moolenaarb340bae2020-06-15 19:51:56 +02001581 call delete(undofile('XtestUndo'))
Bram Moolenaardb934952020-04-27 20:18:31 +02001582 set undofile&
1583endfunc
1584
Bram Moolenaarc6b37db2019-04-27 18:00:34 +02001585" Test for inserting characters using CTRL-V followed by a number.
1586func Test_edit_special_chars()
1587 new
1588
1589 if has("ebcdic")
1590 let t = "o\<C-V>193\<C-V>xc2\<C-V>o303 \<C-V>90a\<C-V>xfg\<C-V>o578\<Esc>"
1591 else
1592 let t = "o\<C-V>65\<C-V>x42\<C-V>o103 \<C-V>33a\<C-V>xfg\<C-V>o78\<Esc>"
1593 endif
1594
1595 exe "normal " . t
1596 call assert_equal("ABC !a\<C-O>g\<C-G>8", getline(2))
1597
1598 close!
1599endfunc
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02001600
1601func Test_edit_startinsert()
1602 new
1603 set backspace+=start
1604 call setline(1, 'foobar')
1605 call feedkeys("A\<C-U>\<Esc>", 'xt')
1606 call assert_equal('', getline(1))
1607
1608 call setline(1, 'foobar')
1609 call feedkeys(":startinsert!\<CR>\<C-U>\<Esc>", 'xt')
1610 call assert_equal('', getline(1))
1611
1612 set backspace&
1613 bwipe!
1614endfunc
Bram Moolenaar177c9f22019-11-06 13:59:16 +01001615
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +01001616" Test for :startreplace and :startgreplace
1617func Test_edit_startreplace()
1618 new
1619 call setline(1, 'abc')
1620 call feedkeys("l:startreplace\<CR>xyz\e", 'xt')
1621 call assert_equal('axyz', getline(1))
1622 call feedkeys("0:startreplace!\<CR>abc\e", 'xt')
1623 call assert_equal('axyzabc', getline(1))
1624 call setline(1, "a\tb")
1625 call feedkeys("0l:startgreplace\<CR>xyz\e", 'xt')
1626 call assert_equal("axyz\tb", getline(1))
1627 call feedkeys("0i\<C-R>=execute('startreplace')\<CR>12\e", 'xt')
1628 call assert_equal("12axyz\tb", getline(1))
1629 close!
1630endfunc
1631
Bram Moolenaar177c9f22019-11-06 13:59:16 +01001632func Test_edit_noesckeys()
Bram Moolenaar215ba3b2019-11-06 15:07:07 +01001633 CheckNotGui
Bram Moolenaar177c9f22019-11-06 13:59:16 +01001634 new
1635
1636 " <Left> moves cursor when 'esckeys' is set
1637 exe "set t_kl=\<Esc>OD"
1638 set esckeys
1639 call feedkeys("axyz\<Esc>ODX", "xt")
1640 call assert_equal("xyXz", getline(1))
1641
1642 " <Left> exits Insert mode when 'esckeys' is off
1643 set noesckeys
1644 call setline(1, '')
1645 call feedkeys("axyz\<Esc>ODX", "xt")
1646 call assert_equal(["DX", "xyz"], getline(1, 2))
1647
1648 bwipe!
1649 set esckeys
1650endfunc
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +01001651
Bram Moolenaar1671f442020-03-10 07:48:13 +01001652" Test for running an invalid ex command in insert mode using CTRL-O
Bram Moolenaar1671f442020-03-10 07:48:13 +01001653func Test_edit_ctrl_o_invalid_cmd()
1654 new
1655 set showmode showcmd
Bram Moolenaarb340bae2020-06-15 19:51:56 +02001656 " Avoid a sleep of 3 seconds. Zero might have side effects.
1657 call test_override('ui_delay', 50)
Bram Moolenaar1671f442020-03-10 07:48:13 +01001658 let caught_e492 = 0
1659 try
1660 call feedkeys("i\<C-O>:invalid\<CR>abc\<Esc>", "xt")
1661 catch /E492:/
1662 let caught_e492 = 1
1663 endtry
1664 call assert_equal(1, caught_e492)
1665 call assert_equal('abc', getline(1))
1666 set showmode& showcmd&
Bram Moolenaarb340bae2020-06-15 19:51:56 +02001667 call test_override('ui_delay', 0)
Bram Moolenaar1671f442020-03-10 07:48:13 +01001668 close!
1669endfunc
1670
Bram Moolenaarb340bae2020-06-15 19:51:56 +02001671" Test for editing a file with a very long name
1672func Test_edit_illegal_filename()
1673 CheckEnglish
1674 new
1675 redir => msg
1676 exe 'edit ' . repeat('f', 5000)
1677 redir END
1678 call assert_match("Illegal file name$", split(msg, "\n")[0])
1679 close!
1680endfunc
1681
Bram Moolenaarc8fe6452020-10-03 17:04:37 +02001682" Test for editing a directory
1683func Test_edit_is_a_directory()
1684 CheckEnglish
1685 let dirname = getcwd() . "/Xdir"
1686 call mkdir(dirname, 'p')
1687
1688 new
1689 redir => msg
1690 exe 'edit' dirname
1691 redir END
1692 call assert_match("is a directory$", split(msg, "\n")[0])
1693 bwipe!
1694
1695 let dirname .= '/'
1696
1697 new
1698 redir => msg
1699 exe 'edit' dirname
1700 redir END
1701 call assert_match("is a directory$", split(msg, "\n")[0])
1702 bwipe!
1703
1704 call delete(dirname, 'rf')
1705endfunc
1706
Bram Moolenaarb340bae2020-06-15 19:51:56 +02001707" Test for editing a file using invalid file encoding
1708func Test_edit_invalid_encoding()
1709 CheckEnglish
1710 call writefile([], 'Xfile')
1711 redir => msg
1712 new ++enc=axbyc Xfile
1713 redir END
1714 call assert_match('\[NOT converted\]', msg)
1715 call delete('Xfile')
1716 close!
1717endfunc
1718
1719" Test for the "charconvert" option
1720func Test_edit_charconvert()
1721 CheckEnglish
1722 call writefile(['one', 'two'], 'Xfile')
1723
1724 " set 'charconvert' to a non-existing function
1725 set charconvert=NonExitingFunc()
1726 new
1727 let caught_e117 = v:false
1728 try
1729 redir => msg
1730 edit ++enc=axbyc Xfile
1731 catch /E117:/
1732 let caught_e117 = v:true
1733 finally
1734 redir END
1735 endtry
1736 call assert_true(caught_e117)
1737 call assert_equal(['one', 'two'], getline(1, '$'))
1738 call assert_match("Conversion with 'charconvert' failed", msg)
1739 close!
1740 set charconvert&
1741
1742 " 'charconvert' function doesn't create a output file
1743 func Cconv1()
1744 endfunc
1745 set charconvert=Cconv1()
1746 new
1747 redir => msg
1748 edit ++enc=axbyc Xfile
1749 redir END
1750 call assert_equal(['one', 'two'], getline(1, '$'))
1751 call assert_match("can't read output of 'charconvert'", msg)
1752 close!
1753 delfunc Cconv1
1754 set charconvert&
1755
1756 " 'charconvert' function to convert to upper case
1757 func Cconv2()
1758 let data = readfile(v:fname_in)
1759 call map(data, 'toupper(v:val)')
1760 call writefile(data, v:fname_out)
1761 endfunc
1762 set charconvert=Cconv2()
1763 new Xfile
1764 write ++enc=ucase Xfile1
1765 call assert_equal(['ONE', 'TWO'], readfile('Xfile1'))
1766 call delete('Xfile1')
1767 close!
1768 delfunc Cconv2
1769 set charconvert&
1770
1771 " 'charconvert' function removes the input file
1772 func Cconv3()
1773 call delete(v:fname_in)
1774 endfunc
1775 set charconvert=Cconv3()
1776 new
1777 call assert_fails('edit ++enc=lcase Xfile', 'E202:')
1778 call assert_equal([''], getline(1, '$'))
1779 close!
1780 delfunc Cconv3
1781 set charconvert&
1782
1783 call delete('Xfile')
1784endfunc
1785
1786" Test for editing a file without read permission
1787func Test_edit_file_no_read_perm()
1788 CheckUnix
Bram Moolenaar17709e22021-03-19 14:38:12 +01001789 CheckNotRoot
1790
Bram Moolenaarb340bae2020-06-15 19:51:56 +02001791 call writefile(['one', 'two'], 'Xfile')
1792 call setfperm('Xfile', '-w-------')
1793 new
1794 redir => msg
1795 edit Xfile
1796 redir END
1797 call assert_equal(1, &readonly)
1798 call assert_equal([''], getline(1, '$'))
1799 call assert_match('\[Permission Denied\]', msg)
1800 close!
1801 call delete('Xfile')
1802endfunc
1803
Bram Moolenaar845e0ee2020-06-20 16:05:32 +02001804" Pressing escape in 'insertmode' should beep
1805func Test_edit_insertmode_esc_beeps()
1806 new
1807 set insertmode
1808 call assert_beeps("call feedkeys(\"one\<Esc>\", 'xt')")
1809 set insertmode&
1810 " unsupported CTRL-G command should beep in insert mode.
1811 call assert_beeps("normal i\<C-G>l")
1812 close!
1813endfunc
1814
1815" Test for 'hkmap' and 'hkmapp'
1816func Test_edit_hkmap()
1817 CheckFeature rightleft
1818 if has('win32') && !has('gui')
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001819 throw 'Skipped: fails on the MS-Windows terminal version'
Bram Moolenaar845e0ee2020-06-20 16:05:32 +02001820 endif
1821 new
1822
1823 set revins hkmap
1824 let str = 'abcdefghijklmnopqrstuvwxyz'
1825 let str ..= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
1826 let str ..= '`/'',.;'
1827 call feedkeys('i' .. str, 'xt')
1828 let expected = "óõú,.;"
1829 let expected ..= "ZYXWVUTSRQPONMLKJIHGFEDCBA"
1830 let expected ..= "æèñ'äåàãø/ôíîöêìçïéòë÷âáðù"
1831 call assert_equal(expected, getline(1))
1832
1833 %d
1834 set revins hkmap hkmapp
1835 let str = 'abcdefghijklmnopqrstuvwxyz'
1836 let str ..= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
1837 call feedkeys('i' .. str, 'xt')
1838 let expected = "õYXWVUTSRQóOïíLKJIHGFEDêBA"
1839 let expected ..= "öòXùåèúæø'ôñðîì÷çéäâóǟãëáà"
1840 call assert_equal(expected, getline(1))
1841
1842 set revins& hkmap& hkmapp&
1843 close!
1844endfunc
1845
1846" Test for 'allowrevins' and using CTRL-_ in insert mode
1847func Test_edit_allowrevins()
1848 CheckFeature rightleft
1849 new
1850 set allowrevins
1851 call feedkeys("iABC\<C-_>DEF\<C-_>GHI", 'xt')
1852 call assert_equal('ABCFEDGHI', getline(1))
1853 set allowrevins&
1854 close!
1855endfunc
1856
1857" Test for inserting a register in insert mode using CTRL-R
1858func Test_edit_insert_reg()
1859 new
1860 let g:Line = ''
1861 func SaveFirstLine()
1862 let g:Line = Screenline(1)
1863 return 'r'
1864 endfunc
1865 inoremap <expr> <buffer> <F2> SaveFirstLine()
1866 call test_override('redraw_flag', 1)
1867 call test_override('char_avail', 1)
1868 let @r = 'sample'
1869 call feedkeys("a\<C-R>=SaveFirstLine()\<CR>", "xt")
1870 call assert_equal('"', g:Line)
1871 call test_override('ALL', 0)
1872 close!
1873endfunc
1874
1875" When a character is inserted at the last position of the last line in a
1876" window, the window contents should be scrolled one line up. If the top line
1877" is part of a fold, then the entire fold should be scrolled up.
1878func Test_edit_lastline_scroll()
1879 new
1880 let h = winheight(0)
1881 let lines = ['one', 'two', 'three']
1882 let lines += repeat(['vim'], h - 4)
1883 call setline(1, lines)
1884 call setline(h, repeat('x', winwidth(0) - 1))
1885 call feedkeys("GAx", 'xt')
1886 redraw!
1887 call assert_equal(h - 1, winline())
1888 call assert_equal(2, line('w0'))
1889
1890 " scroll with a fold
1891 1,2fold
1892 normal gg
1893 call setline(h + 1, repeat('x', winwidth(0) - 1))
1894 call feedkeys("GAx", 'xt')
1895 redraw!
1896 call assert_equal(h - 1, winline())
1897 call assert_equal(3, line('w0'))
1898
1899 close!
1900endfunc
1901
Bram Moolenaar21cbe172020-10-13 19:08:24 +02001902func Test_edit_browse()
1903 " in the GUI this opens a file picker, we only test the terminal behavior
1904 CheckNotGui
1905
1906 " ":browse xxx" checks for the FileExplorer augroup and assumes editing "."
1907 " works then.
1908 augroup FileExplorer
1909 au!
1910 augroup END
1911
1912 " When the USE_FNAME_CASE is defined this used to cause a crash.
1913 browse enew
1914 bwipe!
1915
1916 browse split
1917 bwipe!
1918endfunc
1919
Bram Moolenaarcaf73dc2020-10-26 21:39:13 +01001920func Test_read_invalid()
1921 set encoding=latin1
1922 " This was not properly checking for going past the end.
1923 call assert_fails('r`=', 'E484')
1924 set encoding=utf-8
1925endfunc
1926
Yegappan Lakshmanan59585492021-06-12 13:46:41 +02001927" Test for the 'revins' option
1928func Test_edit_revins()
1929 CheckFeature rightleft
1930 new
1931 set revins
1932 exe "normal! ione\ttwo three"
1933 call assert_equal("eerht owt\teno", getline(1))
1934 call setline(1, "one\ttwo three")
1935 normal! gg$bi a
1936 call assert_equal("one\ttwo a three", getline(1))
1937 exe "normal! $bi\<BS>\<BS>"
1938 call assert_equal("one\ttwo a ree", getline(1))
1939 exe "normal! 0wi\<C-W>"
1940 call assert_equal("one\t a ree", getline(1))
1941 exe "normal! 0wi\<C-U>"
1942 call assert_equal("one\t ", getline(1))
1943 " newline in insert mode starts at the end of the line
1944 call setline(1, 'one two three')
1945 exe "normal! wi\nfour"
1946 call assert_equal(['one two three', 'ruof'], getline(1, '$'))
1947 set revins&
1948 bw!
1949endfunc
1950
Bram Moolenaar35a9a002021-09-11 21:14:20 +02001951" Test for getting the character of the line below after "p"
1952func Test_edit_put_CTRL_E()
1953 set encoding=latin1
1954 new
1955 let @" = ''
1956 sil! norm orggRx
1957 sil! norm pr
1958 call assert_equal(['r', 'r'], getline(1, 2))
1959 bwipe!
1960 set encoding=utf-8
1961endfunc
1962
=?UTF-8?q?Magnus=20Gro=C3=9F?=f1e88762021-09-12 13:39:55 +02001963" Test for ModeChanged pattern
1964func Test_mode_changes()
=?UTF-8?q?Magnus=20Gro=C3=9F?=f1e88762021-09-12 13:39:55 +02001965 let g:index = 0
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01001966 let g:mode_seq = ['n', 'i', 'n', 'v', 'V', 'i', 'ix', 'i', 'ic', 'i', 'n', 'no', 'n', 'V', 'v', 's', 'n']
=?UTF-8?q?Magnus=20Gro=C3=9F?=f1e88762021-09-12 13:39:55 +02001967 func! TestMode()
1968 call assert_equal(g:mode_seq[g:index], get(v:event, "old_mode"))
1969 call assert_equal(g:mode_seq[g:index + 1], get(v:event, "new_mode"))
Bram Moolenaard85931e2021-10-02 16:55:39 +01001970 call assert_equal(mode(1), get(v:event, "new_mode"))
=?UTF-8?q?Magnus=20Gro=C3=9F?=f1e88762021-09-12 13:39:55 +02001971 let g:index += 1
1972 endfunc
1973
1974 au ModeChanged * :call TestMode()
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01001975 let g:n_to_any = 0
1976 au ModeChanged n:* let g:n_to_any += 1
1977 call feedkeys("i\<esc>vVca\<CR>\<C-X>\<C-L>\<esc>ggdG", 'tnix')
=?UTF-8?q?Magnus=20Gro=C3=9F?=f1e88762021-09-12 13:39:55 +02001978
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01001979 let g:V_to_v = 0
1980 au ModeChanged V:v let g:V_to_v += 1
1981 call feedkeys("Vv\<C-G>\<esc>", 'tnix')
1982 call assert_equal(len(filter(g:mode_seq[1:], {idx, val -> val == 'n'})), g:n_to_any)
1983 call assert_equal(1, g:V_to_v)
Bram Moolenaard85931e2021-10-02 16:55:39 +01001984 call assert_equal(len(g:mode_seq) - 1, g:index)
=?UTF-8?q?Magnus=20Gro=C3=9F?=f1e88762021-09-12 13:39:55 +02001985
Bram Moolenaar72d2fa62021-10-02 21:48:15 +01001986 let g:n_to_i = 0
1987 au ModeChanged n:i let g:n_to_i += 1
1988 let g:n_to_niI = 0
1989 au ModeChanged i:niI let g:n_to_niI += 1
1990 let g:niI_to_i = 0
1991 au ModeChanged niI:i let g:niI_to_i += 1
1992 let g:nany_to_i = 0
1993 au ModeChanged n*:i let g:nany_to_i += 1
1994 let g:i_to_n = 0
1995 au ModeChanged i:n let g:i_to_n += 1
1996 let g:nori_to_any = 0
1997 au ModeChanged [ni]:* let g:nori_to_any += 1
1998 let g:i_to_any = 0
1999 au ModeChanged i:* let g:i_to_any += 1
Bram Moolenaard85931e2021-10-02 16:55:39 +01002000 let g:index = 0
2001 let g:mode_seq = ['n', 'i', 'niI', 'i', 'n']
2002 call feedkeys("a\<C-O>l\<esc>", 'tnix')
=?UTF-8?q?Magnus=20Gro=C3=9F?=f1e88762021-09-12 13:39:55 +02002003 call assert_equal(len(g:mode_seq) - 1, g:index)
Bram Moolenaar72d2fa62021-10-02 21:48:15 +01002004 call assert_equal(1, g:n_to_i)
2005 call assert_equal(1, g:n_to_niI)
2006 call assert_equal(1, g:niI_to_i)
2007 call assert_equal(2, g:nany_to_i)
2008 call assert_equal(1, g:i_to_n)
2009 call assert_equal(2, g:i_to_any)
2010 call assert_equal(3, g:nori_to_any)
=?UTF-8?q?Magnus=20Gro=C3=9F?=f1e88762021-09-12 13:39:55 +02002011
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01002012 if has('terminal')
2013 let g:mode_seq += ['c', 'n', 't', 'nt', 'c', 'nt', 'n']
2014 call feedkeys(":term\<CR>\<C-W>N:bd!\<CR>", 'tnix')
2015 call assert_equal(len(g:mode_seq) - 1, g:index)
2016 call assert_equal(1, g:n_to_i)
2017 call assert_equal(1, g:n_to_niI)
2018 call assert_equal(1, g:niI_to_i)
2019 call assert_equal(2, g:nany_to_i)
2020 call assert_equal(1, g:i_to_n)
2021 call assert_equal(2, g:i_to_any)
2022 call assert_equal(5, g:nori_to_any)
2023 endif
2024
=?UTF-8?q?Magnus=20Gro=C3=9F?=f1e88762021-09-12 13:39:55 +02002025 au! ModeChanged
2026 delfunc TestMode
2027 unlet! g:mode_seq
2028 unlet! g:index
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01002029 unlet! g:n_to_any
2030 unlet! g:V_to_v
2031 unlet! g:n_to_i
2032 unlet! g:n_to_niI
2033 unlet! g:niI_to_i
2034 unlet! g:nany_to_i
2035 unlet! g:i_to_n
2036 unlet! g:nori_to_any
2037 unlet! g:i_to_any
=?UTF-8?q?Magnus=20Gro=C3=9F?=f1e88762021-09-12 13:39:55 +02002038endfunc
2039
Bram Moolenaar3075a452021-11-17 15:51:52 +00002040func Test_recursive_ModeChanged()
2041 au! ModeChanged * norm 0u
2042 sil! norm 
Bram Moolenaara0620062021-11-17 16:52:40 +00002043 au! ModeChanged
2044endfunc
2045
2046func Test_ModeChanged_starts_visual()
2047 " This was triggering ModeChanged before setting VIsual, causing a crash.
2048 au! ModeChanged * norm 0u
2049 sil! norm 
2050
2051 au! ModeChanged
Bram Moolenaar3075a452021-11-17 15:51:52 +00002052endfunc
2053
Dominique Pelle9cd063e2021-10-28 21:06:05 +01002054" Test toggling of input method. See :help i_CTRL-^
2055func Test_edit_CTRL_hat()
2056 CheckFeature xim
Dominique Pelle8753c1d2021-10-31 20:19:17 +00002057
2058 " FIXME: test fails with Athena and Motif GUI.
2059 " test also fails when running in the GUI.
2060 CheckFeature gui_gtk
2061 CheckNotGui
Dominique Pelle9cd063e2021-10-28 21:06:05 +01002062
2063 new
2064
2065 call assert_equal(0, &iminsert)
2066 call feedkeys("i\<C-^>", 'xt')
2067 call assert_equal(2, &iminsert)
2068 call feedkeys("i\<C-^>", 'xt')
2069 call assert_equal(0, &iminsert)
2070
2071 bwipe!
2072endfunc
2073
2074
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +01002075" vim: shiftwidth=2 sts=2 expandtab