blob: e886424fb1bdc85a2a332ded6626aad04277380f [file] [log] [blame]
Bram Moolenaar2d1a2482016-08-14 15:32:11 +02001" Tests for mappings and abbreviations
2
Bram Moolenaar26d98212019-01-27 22:32:55 +01003source shared.vim
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02004source check.vim
Bram Moolenaar4ebe0e62019-11-22 20:55:40 +01005source screendump.vim
Bram Moolenaar957cf672020-11-12 14:21:06 +01006source term_util.vim
Bram Moolenaar62aec932022-01-29 21:45:34 +00007import './vim9.vim' as v9
Bram Moolenaar26d98212019-01-27 22:32:55 +01008
Bram Moolenaar2d1a2482016-08-14 15:32:11 +02009func Test_abbreviation()
10 " abbreviation with 0x80 should work
11 inoreab чкпр vim
12 call feedkeys("Goчкпр \<Esc>", "xt")
13 call assert_equal('vim ', getline('$'))
14 iunab чкпр
15 set nomodified
16endfunc
17
Bram Moolenaar8485be42019-04-23 16:36:05 +020018func Test_abclear()
19 abbrev foo foobar
20 iabbrev fooi foobari
21 cabbrev fooc foobarc
22 call assert_equal("\n\n"
23 \ .. "c fooc foobarc\n"
24 \ .. "i fooi foobari\n"
25 \ .. "! foo foobar", execute('abbrev'))
26
27 iabclear
28 call assert_equal("\n\n"
29 \ .. "c fooc foobarc\n"
30 \ .. "c foo foobar", execute('abbrev'))
31 abbrev foo foobar
32 iabbrev fooi foobari
33
34 cabclear
35 call assert_equal("\n\n"
36 \ .. "i fooi foobari\n"
37 \ .. "i foo foobar", execute('abbrev'))
38 abbrev foo foobar
39 cabbrev fooc foobarc
40
41 abclear
42 call assert_equal("\n\nNo abbreviation found", execute('abbrev'))
Bram Moolenaarf0cee192020-02-16 13:33:56 +010043 call assert_fails('%abclear', 'E481:')
Bram Moolenaar8485be42019-04-23 16:36:05 +020044endfunc
45
46func Test_abclear_buffer()
47 abbrev foo foobar
48 new X1
49 abbrev <buffer> foo1 foobar1
50 new X2
51 abbrev <buffer> foo2 foobar2
52
53 call assert_equal("\n\n"
54 \ .. "! foo2 @foobar2\n"
55 \ .. "! foo foobar", execute('abbrev'))
56
57 abclear <buffer>
58 call assert_equal("\n\n"
59 \ .. "! foo foobar", execute('abbrev'))
60
61 b X1
62 call assert_equal("\n\n"
63 \ .. "! foo1 @foobar1\n"
64 \ .. "! foo foobar", execute('abbrev'))
65 abclear <buffer>
66 call assert_equal("\n\n"
67 \ .. "! foo foobar", execute('abbrev'))
68
69 abclear
70 call assert_equal("\n\nNo abbreviation found", execute('abbrev'))
71
72 %bwipe
73endfunc
74
Bram Moolenaar2d1a2482016-08-14 15:32:11 +020075func Test_map_ctrl_c_insert()
76 " mapping of ctrl-c in Insert mode
77 set cpo-=< cpo-=k
78 inoremap <c-c> <ctrl-c>
79 cnoremap <c-c> dummy
80 cunmap <c-c>
Bram Moolenaarfccd93f2020-05-31 22:06:51 +020081 call feedkeys("GoTEST2: CTRL-C |\<*C-C>A|\<Esc>", "xt")
Bram Moolenaar2d1a2482016-08-14 15:32:11 +020082 call assert_equal('TEST2: CTRL-C |<ctrl-c>A|', getline('$'))
83 unmap! <c-c>
84 set nomodified
85endfunc
86
87func Test_map_ctrl_c_visual()
88 " mapping of ctrl-c in Visual mode
89 vnoremap <c-c> :<C-u>$put ='vmap works'
Bram Moolenaarfccd93f2020-05-31 22:06:51 +020090 call feedkeys("GV\<*C-C>\<CR>", "xt")
Bram Moolenaar2d1a2482016-08-14 15:32:11 +020091 call assert_equal('vmap works', getline('$'))
92 vunmap <c-c>
93 set nomodified
94endfunc
95
96func Test_map_langmap()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +020097 CheckFeature langmap
Bram Moolenaar920694c2016-08-21 17:45:02 +020098
99 " check langmap applies in normal mode
100 set langmap=+- nolangremap
101 new
102 call setline(1, ['a', 'b', 'c'])
103 2
104 call assert_equal('b', getline('.'))
105 call feedkeys("+", "xt")
106 call assert_equal('a', getline('.'))
107
108 " check no remapping
109 map x +
110 2
111 call feedkeys("x", "xt")
112 call assert_equal('c', getline('.'))
113
114 " check with remapping
115 set langremap
116 2
117 call feedkeys("x", "xt")
118 call assert_equal('a', getline('.'))
119
120 unmap x
121 bwipe!
122
123 " 'langnoremap' follows 'langremap' and vise versa
124 set langremap
125 set langnoremap
126 call assert_equal(0, &langremap)
127 set langremap
128 call assert_equal(0, &langnoremap)
129 set nolangremap
130 call assert_equal(1, &langnoremap)
131
Bram Moolenaarda9ce2c2016-09-02 19:34:10 +0200132 " check default values
133 set langnoremap&
134 call assert_equal(0, &langnoremap)
135 call assert_equal(1, &langremap)
136 set langremap&
137 call assert_equal(0, &langnoremap)
138 call assert_equal(1, &langremap)
139
Bram Moolenaar920694c2016-08-21 17:45:02 +0200140 " langmap should not apply in insert mode, 'langremap' doesn't matter
141 set langmap=+{ nolangremap
142 call feedkeys("Go+\<Esc>", "xt")
143 call assert_equal('+', getline('$'))
144 set langmap=+{ langremap
Bram Moolenaar2d1a2482016-08-14 15:32:11 +0200145 call feedkeys("Go+\<Esc>", "xt")
146 call assert_equal('+', getline('$'))
147
Bram Moolenaar920694c2016-08-21 17:45:02 +0200148 " langmap used for register name in insert mode.
149 call setreg('a', 'aaaa')
150 call setreg('b', 'bbbb')
151 call setreg('c', 'cccc')
152 set langmap=ab langremap
153 call feedkeys("Go\<C-R>a\<Esc>", "xt")
154 call assert_equal('bbbb', getline('$'))
155 call feedkeys("Go\<C-R>\<C-R>a\<Esc>", "xt")
156 call assert_equal('bbbb', getline('$'))
157 " mapping does not apply
158 imap c a
159 call feedkeys("Go\<C-R>c\<Esc>", "xt")
160 call assert_equal('cccc', getline('$'))
161 imap a c
162 call feedkeys("Go\<C-R>a\<Esc>", "xt")
163 call assert_equal('bbbb', getline('$'))
164
165 " langmap should not apply in Command-line mode
166 set langmap=+{ nolangremap
Bram Moolenaar2d1a2482016-08-14 15:32:11 +0200167 call feedkeys(":call append(line('$'), '+')\<CR>", "xt")
168 call assert_equal('+', getline('$'))
Bram Moolenaar2d1a2482016-08-14 15:32:11 +0200169
Bram Moolenaare90858d2017-02-01 17:24:34 +0100170 iunmap a
171 iunmap c
Bram Moolenaar2d1a2482016-08-14 15:32:11 +0200172 set nomodified
173endfunc
174
175func Test_map_feedkeys()
176 " issue #212 (feedkeys insert mapping at current position)
177 nnoremap . :call feedkeys(".", "in")<cr>
178 call setline('$', ['a b c d', 'a b c d'])
179 $-1
180 call feedkeys("0qqdw.ifoo\<Esc>qj0@q\<Esc>", "xt")
181 call assert_equal(['fooc d', 'fooc d'], getline(line('$') - 1, line('$')))
Bram Moolenaare90858d2017-02-01 17:24:34 +0100182 nunmap .
Bram Moolenaar2d1a2482016-08-14 15:32:11 +0200183 set nomodified
184endfunc
185
186func Test_map_cursor()
187 " <c-g>U<cursor> works only within a single line
188 imapclear
189 imap ( ()<c-g>U<left>
190 call feedkeys("G2o\<Esc>ki\<CR>Test1: text with a (here some more text\<Esc>k.", "xt")
191 call assert_equal('Test1: text with a (here some more text)', getline(line('$') - 2))
192 call assert_equal('Test1: text with a (here some more text)', getline(line('$') - 1))
193
194 " test undo
195 call feedkeys("G2o\<Esc>ki\<CR>Test2: text wit a (here some more text [und undo]\<C-G>u\<Esc>k.u", "xt")
196 call assert_equal('', getline(line('$') - 2))
197 call assert_equal('Test2: text wit a (here some more text [und undo])', getline(line('$') - 1))
198 set nomodified
199 imapclear
200endfunc
201
Bram Moolenaar75bf3d22019-03-26 22:46:05 +0100202func Test_map_cursor_ctrl_gU()
203 " <c-g>U<cursor> works only within a single line
204 nnoremap c<* *Ncgn<C-r>"<C-G>U<S-Left>
205 call setline(1, ['foo', 'foobar', '', 'foo'])
206 call cursor(1,2)
207 call feedkeys("c<*PREFIX\<esc>.", 'xt')
208 call assert_equal(['PREFIXfoo', 'foobar', '', 'PREFIXfoo'], getline(1,'$'))
209 " break undo manually
210 set ul=1000
211 exe ":norm! uu"
212 call assert_equal(['foo', 'foobar', '', 'foo'], getline(1,'$'))
213
214 " Test that it does not work if the cursor moves to the previous line
215 " 2 times <S-Left> move to the previous line
216 nnoremap c<* *Ncgn<C-r>"<C-G>U<S-Left><C-G>U<S-Left>
217 call setline(1, ['', ' foo', 'foobar', '', 'foo'])
218 call cursor(2,3)
219 call feedkeys("c<*PREFIX\<esc>.", 'xt')
220 call assert_equal(['PREFIXPREFIX', ' foo', 'foobar', '', 'foo'], getline(1,'$'))
221 nmapclear
222endfunc
223
224
Bram Moolenaar2d1a2482016-08-14 15:32:11 +0200225" This isn't actually testing a mapping, but similar use of CTRL-G U as above.
226func Test_break_undo()
Bram Moolenaar75bf3d22019-03-26 22:46:05 +0100227 set whichwrap=<,>,[,]
Bram Moolenaar2d1a2482016-08-14 15:32:11 +0200228 call feedkeys("G4o2k", "xt")
229 exe ":norm! iTest3: text with a (parenthesis here\<C-G>U\<Right>new line here\<esc>\<up>\<up>."
230 call assert_equal('new line here', getline(line('$') - 3))
231 call assert_equal('Test3: text with a (parenthesis here', getline(line('$') - 2))
232 call assert_equal('new line here', getline(line('$') - 1))
233 set nomodified
234endfunc
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +0200235
236func Test_map_meta_quotes()
237 imap <M-"> foo
Bram Moolenaarfccd93f2020-05-31 22:06:51 +0200238 call feedkeys("Go-\<*M-\">-\<Esc>", "xt")
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +0200239 call assert_equal("-foo-", getline('$'))
240 set nomodified
241 iunmap <M-">
242endfunc
Bram Moolenaar878c2632017-04-01 15:15:52 +0200243
Bram Moolenaarc8fd33d2019-08-16 20:33:05 +0200244func Test_map_meta_multibyte()
245 imap <M-á> foo
Bram Moolenaar2f710af2019-08-16 20:56:03 +0200246 call assert_match('i <M-á>\s*foo', execute('imap'))
Bram Moolenaarc8fd33d2019-08-16 20:33:05 +0200247 iunmap <M-á>
248endfunc
249
Bram Moolenaar878c2632017-04-01 15:15:52 +0200250func Test_abbr_after_line_join()
251 new
252 abbr foo bar
253 set backspace=indent,eol,start
254 exe "normal o\<BS>foo "
255 call assert_equal("bar ", getline(1))
256 bwipe!
257 unabbr foo
258 set backspace&
259endfunc
Bram Moolenaarb7637c42017-04-23 18:49:36 +0200260
261func Test_map_timeout()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200262 CheckFeature timers
Bram Moolenaarb7637c42017-04-23 18:49:36 +0200263 nnoremap aaaa :let got_aaaa = 1<CR>
264 nnoremap bb :let got_bb = 1<CR>
265 nmap b aaa
266 new
267 func ExitInsert(timer)
268 let g:line = getline(1)
269 call feedkeys("\<Esc>", "t")
270 endfunc
271 set timeout timeoutlen=200
Bram Moolenaar26d98212019-01-27 22:32:55 +0100272 let timer = timer_start(300, 'ExitInsert')
Bram Moolenaarb7637c42017-04-23 18:49:36 +0200273 " After the 'b' Vim waits for another character to see if it matches 'bb'.
274 " When it times out it is expanded to "aaa", but there is no wait for
275 " "aaaa". Can't check that reliably though.
276 call feedkeys("b", "xt!")
277 call assert_equal("aa", g:line)
278 call assert_false(exists('got_aaa'))
279 call assert_false(exists('got_bb'))
280
281 bwipe!
282 nunmap aaaa
283 nunmap bb
284 nunmap b
285 set timeoutlen&
286 delfunc ExitInsert
Bram Moolenaar26d98212019-01-27 22:32:55 +0100287 call timer_stop(timer)
288endfunc
289
290func Test_map_timeout_with_timer_interrupt()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200291 CheckFeature job
292 CheckFeature timers
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +0100293 let g:test_is_flaky = 1
Bram Moolenaar26d98212019-01-27 22:32:55 +0100294
295 " Confirm the timer invoked in exit_cb of the job doesn't disturb mapped key
296 " sequence.
297 new
298 let g:val = 0
299 nnoremap \12 :let g:val = 1<CR>
300 nnoremap \123 :let g:val = 2<CR>
Bram Moolenaarea94c852019-08-16 21:47:27 +0200301 set timeout timeoutlen=200
Bram Moolenaar26d98212019-01-27 22:32:55 +0100302
303 func ExitCb(job, status)
Bram Moolenaar8d4ce562019-01-30 22:01:40 +0100304 let g:timer = timer_start(1, {-> feedkeys("3\<Esc>", 't')})
Bram Moolenaar26d98212019-01-27 22:32:55 +0100305 endfunc
306
307 call job_start([&shell, &shellcmdflag, 'echo'], {'exit_cb': 'ExitCb'})
308 call feedkeys('\12', 'xt!')
309 call assert_equal(2, g:val)
310
311 bwipe!
312 nunmap \12
313 nunmap \123
314 set timeoutlen&
315 call WaitFor({-> exists('g:timer')})
316 call timer_stop(g:timer)
317 unlet g:timer
318 unlet g:val
319 delfunc ExitCb
Bram Moolenaarb7637c42017-04-23 18:49:36 +0200320endfunc
Bram Moolenaarc3c3e692018-04-26 22:30:33 +0200321
322func Test_abbreviation_CR()
323 new
324 func Eatchar(pat)
325 let c = nr2char(getchar(0))
326 return (c =~ a:pat) ? '' : c
327 endfunc
328 iabbrev <buffer><silent> ~~7 <c-r>=repeat('~', 7)<CR><c-r>=Eatchar('\s')<cr>
329 call feedkeys("GA~~7 \<esc>", 'xt')
330 call assert_equal('~~~~~~~', getline('$'))
331 %d
332 call feedkeys("GA~~7\<cr>\<esc>", 'xt')
333 call assert_equal(['~~~~~~~', ''], getline(1,'$'))
334 delfunc Eatchar
335 bw!
336endfunc
Bram Moolenaar5e3423d2018-05-13 18:36:27 +0200337
338func Test_cabbr_visual_mode()
339 cabbr s su
340 call feedkeys(":s \<c-B>\"\<CR>", 'itx')
341 call assert_equal('"su ', getreg(':'))
342 call feedkeys(":'<,'>s \<c-B>\"\<CR>", 'itx')
343 let expected = '"'. "'<,'>su "
344 call assert_equal(expected, getreg(':'))
345 call feedkeys(": '<,'>s \<c-B>\"\<CR>", 'itx')
346 let expected = '" '. "'<,'>su "
347 call assert_equal(expected, getreg(':'))
348 call feedkeys(":'a,'bs \<c-B>\"\<CR>", 'itx')
349 let expected = '"'. "'a,'bsu "
350 call assert_equal(expected, getreg(':'))
351 cunabbr s
352endfunc
Bram Moolenaar5976f8f2018-12-27 23:44:44 +0100353
354func Test_motionforce_omap()
355 func GetCommand()
356 let g:m=mode(1)
357 let [g:lnum1, g:col1] = searchpos('-', 'Wb')
358 if g:lnum1 == 0
359 return "\<Esc>"
360 endif
361 let [g:lnum2, g:col2] = searchpos('-', 'W')
362 if g:lnum2 == 0
363 return "\<Esc>"
364 endif
365 return ":call Select()\<CR>"
366 endfunc
367 func Select()
368 call cursor([g:lnum1, g:col1])
369 exe "normal! 1 ". (strlen(g:m) == 2 ? 'v' : g:m[2])
370 call cursor([g:lnum2, g:col2])
371 execute "normal! \<BS>"
372 endfunc
373 new
374 onoremap <buffer><expr> i- GetCommand()
375 " 1) default omap mapping
376 %d_
377 call setline(1, ['aaa - bbb', 'x', 'ddd - eee'])
378 call cursor(2, 1)
379 norm di-
380 call assert_equal('no', g:m)
381 call assert_equal(['aaa -- eee'], getline(1, '$'))
382 " 2) forced characterwise operation
383 %d_
384 call setline(1, ['aaa - bbb', 'x', 'ddd - eee'])
385 call cursor(2, 1)
386 norm dvi-
387 call assert_equal('nov', g:m)
388 call assert_equal(['aaa -- eee'], getline(1, '$'))
389 " 3) forced linewise operation
390 %d_
391 call setline(1, ['aaa - bbb', 'x', 'ddd - eee'])
392 call cursor(2, 1)
393 norm dVi-
394 call assert_equal('noV', g:m)
395 call assert_equal([''], getline(1, '$'))
396 " 4) forced blockwise operation
397 %d_
398 call setline(1, ['aaa - bbb', 'x', 'ddd - eee'])
399 call cursor(2, 1)
400 exe "norm d\<C-V>i-"
401 call assert_equal("no\<C-V>", g:m)
402 call assert_equal(['aaabbb', 'x', 'dddeee'], getline(1, '$'))
403 bwipe!
404 delfunc Select
405 delfunc GetCommand
406endfunc
Bram Moolenaar7d491c42019-06-25 06:28:02 +0200407
408func Test_error_in_map_expr()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +0200409 " Unlike CheckRunVimInTerminal this does work in a win32 console
410 CheckFeature terminal
411 if has('win32') && has('gui_running')
Bram Moolenaar7d491c42019-06-25 06:28:02 +0200412 throw 'Skipped: cannot run Vim in a terminal window'
413 endif
414
415 let lines =<< trim [CODE]
416 func Func()
417 " fail to create list
418 let x = [
419 endfunc
420 nmap <expr> ! Func()
421 set updatetime=50
422 [CODE]
423 call writefile(lines, 'Xtest.vim')
424
Bram Moolenaar0d702022019-07-04 14:20:41 +0200425 let buf = term_start(GetVimCommandCleanTerm() .. ' -S Xtest.vim', {'term_rows': 8})
Bram Moolenaar7d491c42019-06-25 06:28:02 +0200426 let job = term_getjob(buf)
427 call WaitForAssert({-> assert_notequal('', term_getline(buf, 8))})
428
429 " GC must not run during map-expr processing, which can make Vim crash.
430 call term_sendkeys(buf, '!')
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200431 call TermWait(buf, 50)
Bram Moolenaar7d491c42019-06-25 06:28:02 +0200432 call term_sendkeys(buf, "\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200433 call TermWait(buf, 50)
Bram Moolenaar7d491c42019-06-25 06:28:02 +0200434 call assert_equal('run', job_status(job))
435
436 call term_sendkeys(buf, ":qall!\<CR>")
437 call WaitFor({-> job_status(job) ==# 'dead'})
438 if has('unix')
439 call assert_equal('', job_info(job).termsig)
440 endif
441
442 call delete('Xtest.vim')
443 exe buf .. 'bwipe!'
444endfunc
Bram Moolenaarfafb4b12019-10-16 18:34:57 +0200445
446func Test_list_mappings()
Bram Moolenaar2559a472019-10-16 23:33:12 +0200447 " Remove default mappings
448 imapclear
Bram Moolenaar4f2f61a2019-10-16 22:27:49 +0200449
Bram Moolenaare3d1f4c2021-04-06 20:21:59 +0200450 " reset 'isident' to check it isn't used
451 set isident=
452 inoremap <C-m> CtrlM
Bram Moolenaarfafb4b12019-10-16 18:34:57 +0200453 inoremap <A-S> AltS
454 inoremap <S-/> ShiftSlash
Bram Moolenaare3d1f4c2021-04-06 20:21:59 +0200455 set isident&
Bram Moolenaarfafb4b12019-10-16 18:34:57 +0200456 call assert_equal([
457 \ 'i <S-/> * ShiftSlash',
458 \ 'i <M-S> * AltS',
459 \ 'i <C-M> * CtrlM',
460 \], execute('imap')->trim()->split("\n"))
461 iunmap <C-M>
462 iunmap <A-S>
463 call assert_equal(['i <S-/> * ShiftSlash'], execute('imap')->trim()->split("\n"))
464 iunmap <S-/>
465 call assert_equal(['No mapping found'], execute('imap')->trim()->split("\n"))
Bram Moolenaarc2a60ae2020-01-23 16:19:54 +0100466
467 " List global, buffer local and script local mappings
468 nmap ,f /^\k\+ (<CR>
469 nmap <buffer> ,f /^\k\+ (<CR>
470 nmap <script> ,fs /^\k\+ (<CR>
471 call assert_equal(['n ,f @/^\k\+ (<CR>',
472 \ 'n ,fs & /^\k\+ (<CR>',
473 \ 'n ,f /^\k\+ (<CR>'],
474 \ execute('nmap ,f')->trim()->split("\n"))
475
476 " List <Nop> mapping
477 nmap ,n <Nop>
478 call assert_equal(['n ,n <Nop>'],
479 \ execute('nmap ,n')->trim()->split("\n"))
480
Bram Moolenaar7f51bbe2020-01-24 20:21:19 +0100481 " verbose map
482 call assert_match("\tLast set from .*/test_mapping.vim line \\d\\+$",
483 \ execute('verbose map ,n')->trim()->split("\n")[1])
484
zeertzjqac402f42022-05-04 18:51:43 +0100485 " character with K_SPECIAL byte in rhs
486 nmap foo
487 call assert_equal(['n foo …'],
488 \ execute('nmap foo')->trim()->split("\n"))
489
490 " modified character with K_SPECIAL byte in rhs
491 nmap foo <M-…>
492 call assert_equal(['n foo <M-…>'],
493 \ execute('nmap foo')->trim()->split("\n"))
494
495 " character with K_SPECIAL byte in lhs
496 nmap foo
497 call assert_equal(['n … foo'],
498 \ execute('nmap …')->trim()->split("\n"))
499
500 " modified character with K_SPECIAL byte in lhs
501 nmap <M-…> foo
502 call assert_equal(['n <M-…> foo'],
503 \ execute('nmap <M-…>')->trim()->split("\n"))
504
Bram Moolenaar7f51bbe2020-01-24 20:21:19 +0100505 " map to CTRL-V
506 exe "nmap ,k \<C-V>"
507 call assert_equal(['n ,k <Nop>'],
508 \ execute('nmap ,k')->trim()->split("\n"))
509
Yegappan Lakshmanan2d6d7182021-06-13 21:52:48 +0200510 " map with space at the beginning
511 exe "nmap \<C-V> w <Nop>"
512 call assert_equal(['n <Space>w <Nop>'],
513 \ execute("nmap \<C-V> w")->trim()->split("\n"))
514
Bram Moolenaarc2a60ae2020-01-23 16:19:54 +0100515 nmapclear
Bram Moolenaarfafb4b12019-10-16 18:34:57 +0200516endfunc
Bram Moolenaar4ebe0e62019-11-22 20:55:40 +0100517
Bram Moolenaar18b7d862021-03-17 13:28:05 +0100518func Test_expr_map_gets_cursor()
519 new
520 call setline(1, ['one', 'some w!rd'])
521 func StoreColumn()
522 let g:exprLine = line('.')
523 let g:exprCol = col('.')
524 return 'x'
525 endfunc
526 nnoremap <expr> x StoreColumn()
527 2
528 nmap ! f!<Ignore>x
529 call feedkeys("!", 'xt')
530 call assert_equal('some wrd', getline(2))
531 call assert_equal(2, g:exprLine)
532 call assert_equal(7, g:exprCol)
533
534 bwipe!
535 unlet g:exprLine
536 unlet g:exprCol
Bram Moolenaar6ccfd992021-03-17 13:39:33 +0100537 delfunc StoreColumn
Bram Moolenaar18b7d862021-03-17 13:28:05 +0100538 nunmap x
539 nunmap !
540endfunc
541
Bram Moolenaar4ebe0e62019-11-22 20:55:40 +0100542func Test_expr_map_restore_cursor()
543 CheckScreendump
544
545 let lines =<< trim END
546 call setline(1, ['one', 'two', 'three'])
547 2
548 set ls=2
549 hi! link StatusLine ErrorMsg
550 noremap <expr> <C-B> Func()
551 func Func()
552 let g:on = !get(g:, 'on', 0)
553 redraws
554 return ''
555 endfunc
556 func Status()
557 return get(g:, 'on', 0) ? '[on]' : ''
558 endfunc
559 set stl=%{Status()}
560 END
561 call writefile(lines, 'XtestExprMap')
562 let buf = RunVimInTerminal('-S XtestExprMap', #{rows: 10})
Bram Moolenaar4ebe0e62019-11-22 20:55:40 +0100563 call term_sendkeys(buf, "\<C-B>")
564 call VerifyScreenDump(buf, 'Test_map_expr_1', {})
565
566 " clean up
567 call StopVimInTerminal(buf)
568 call delete('XtestExprMap')
569endfunc
Bram Moolenaar8ba6bb72020-01-20 20:41:42 +0100570
Bram Moolenaard288eaa2022-02-16 18:27:55 +0000571func Test_map_listing()
572 CheckScreendump
573
574 let lines =<< trim END
575 nmap a b
576 END
577 call writefile(lines, 'XtestMapList')
578 let buf = RunVimInTerminal('-S XtestMapList', #{rows: 6})
579 call term_sendkeys(buf, ": nmap a\<CR>")
580 call VerifyScreenDump(buf, 'Test_map_list_1', {})
581
582 " clean up
583 call StopVimInTerminal(buf)
584 call delete('XtestMapList')
585endfunc
586
Bram Moolenaar74a0a5b2022-02-10 14:07:41 +0000587func Test_expr_map_error()
588 CheckScreendump
589
590 let lines =<< trim END
591 func Func()
592 throw 'test'
593 return ''
594 endfunc
595
596 nnoremap <expr> <F2> Func()
597 cnoremap <expr> <F2> Func()
598
599 call test_override('ui_delay', 10)
600 END
601 call writefile(lines, 'XtestExprMap')
602 let buf = RunVimInTerminal('-S XtestExprMap', #{rows: 10})
Bram Moolenaar74a0a5b2022-02-10 14:07:41 +0000603 call term_sendkeys(buf, "\<F2>")
604 call TermWait(buf)
605 call term_sendkeys(buf, "\<CR>")
606 call VerifyScreenDump(buf, 'Test_map_expr_2', {})
607
608 call term_sendkeys(buf, ":abc\<F2>")
609 call VerifyScreenDump(buf, 'Test_map_expr_3', {})
610 call term_sendkeys(buf, "\<Esc>0")
611 call VerifyScreenDump(buf, 'Test_map_expr_4', {})
612
613 " clean up
614 call StopVimInTerminal(buf)
615 call delete('XtestExprMap')
616endfunc
617
Bram Moolenaar8ba6bb72020-01-20 20:41:42 +0100618" Test for mapping errors
619func Test_map_error()
620 call assert_fails('unmap', 'E474:')
621 call assert_fails("exe 'map ' .. repeat('a', 51) .. ' :ls'", 'E474:')
622 call assert_fails('unmap abc', 'E31:')
623 call assert_fails('unabbr abc', 'E24:')
624 call assert_equal('', maparg(''))
625 call assert_fails('echo maparg("abc", [])', 'E730:')
626
627 " unique map
628 map ,w /[#&!]<CR>
629 call assert_fails("map <unique> ,w /[#&!]<CR>", 'E227:')
630 " unique buffer-local map
631 call assert_fails("map <buffer> <unique> ,w /[.,;]<CR>", 'E225:')
632 unmap ,w
633
634 " unique abbreviation
635 abbr SP special
636 call assert_fails("abbr <unique> SP special", 'E226:')
637 " unique buffer-local map
638 call assert_fails("abbr <buffer> <unique> SP special", 'E224:')
639 unabbr SP
640
641 call assert_fails('mapclear abc', 'E474:')
642 call assert_fails('abclear abc', 'E474:')
Bram Moolenaarc2a60ae2020-01-23 16:19:54 +0100643 call assert_fails('abbr $xyz abc', 'E474:')
644
645 " space character in an abbreviation
646 call assert_fails('abbr ab<space> ABC', 'E474:')
647
648 " invalid <expr> map
649 map <expr> ,f abc
650 call assert_fails('normal ,f', 'E121:')
651 unmap <expr> ,f
Bram Moolenaar9f6277b2020-02-11 22:04:02 +0100652
653 " Recursive use of :normal in a map
654 set maxmapdepth=100
655 map gq :normal gq<CR>
656 call assert_fails('normal gq', 'E192:')
657 unmap gq
658 set maxmapdepth&
Bram Moolenaar8ba6bb72020-01-20 20:41:42 +0100659endfunc
660
661" Test for <special> key mapping
662func Test_map_special()
663 new
664 let old_cpo = &cpo
665 set cpo+=<
666 imap <F12> Blue
667 call feedkeys("i\<F12>", "x")
668 call assert_equal("<F12>", getline(1))
669 call feedkeys("ddi<F12>", "x")
670 call assert_equal("Blue", getline(1))
671 iunmap <F12>
672 imap <special> <F12> Green
673 call feedkeys("ddi\<F12>", "x")
674 call assert_equal("Green", getline(1))
675 call feedkeys("ddi<F12>", "x")
676 call assert_equal("<F12>", getline(1))
677 iunmap <special> <F12>
678 let &cpo = old_cpo
679 %bwipe!
680endfunc
681
682" Test for hasmapto()
683func Test_hasmapto()
684 call assert_equal(0, hasmapto('/^\k\+ ('))
Bram Moolenaarc2a60ae2020-01-23 16:19:54 +0100685 map ,f /^\k\+ (<CR>
686 call assert_equal(1, hasmapto('/^\k\+ ('))
687 unmap ,f
688
689 " Insert mode mapping
690 call assert_equal(0, hasmapto('/^\k\+ (', 'i'))
691 imap ,f /^\k\+ (<CR>
692 call assert_equal(1, hasmapto('/^\k\+ (', 'i'))
693 iunmap ,f
694
695 " Normal mode mapping
Bram Moolenaar8ba6bb72020-01-20 20:41:42 +0100696 call assert_equal(0, hasmapto('/^\k\+ (', 'n'))
697 nmap ,f /^\k\+ (<CR>
698 call assert_equal(1, hasmapto('/^\k\+ ('))
699 call assert_equal(1, hasmapto('/^\k\+ (', 'n'))
Bram Moolenaarc2a60ae2020-01-23 16:19:54 +0100700 nunmap ,f
701
702 " Visual and Select mode mapping
Bram Moolenaar8ba6bb72020-01-20 20:41:42 +0100703 call assert_equal(0, hasmapto('/^\k\+ (', 'v'))
Bram Moolenaarc2a60ae2020-01-23 16:19:54 +0100704 call assert_equal(0, hasmapto('/^\k\+ (', 'x'))
705 call assert_equal(0, hasmapto('/^\k\+ (', 's'))
706 vmap ,f /^\k\+ (<CR>
707 call assert_equal(1, hasmapto('/^\k\+ (', 'v'))
708 call assert_equal(1, hasmapto('/^\k\+ (', 'x'))
709 call assert_equal(1, hasmapto('/^\k\+ (', 's'))
710 vunmap ,f
711
712 " Visual mode mapping
713 call assert_equal(0, hasmapto('/^\k\+ (', 'x'))
714 xmap ,f /^\k\+ (<CR>
715 call assert_equal(1, hasmapto('/^\k\+ (', 'v'))
716 call assert_equal(1, hasmapto('/^\k\+ (', 'x'))
717 call assert_equal(0, hasmapto('/^\k\+ (', 's'))
718 xunmap ,f
719
720 " Select mode mapping
721 call assert_equal(0, hasmapto('/^\k\+ (', 's'))
722 smap ,f /^\k\+ (<CR>
723 call assert_equal(1, hasmapto('/^\k\+ (', 'v'))
724 call assert_equal(0, hasmapto('/^\k\+ (', 'x'))
725 call assert_equal(1, hasmapto('/^\k\+ (', 's'))
726 sunmap ,f
727
728 " Operator-pending mode mapping
729 call assert_equal(0, hasmapto('/^\k\+ (', 'o'))
730 omap ,f /^\k\+ (<CR>
731 call assert_equal(1, hasmapto('/^\k\+ (', 'o'))
732 ounmap ,f
733
734 " Language mapping
735 call assert_equal(0, hasmapto('/^\k\+ (', 'l'))
736 lmap ,f /^\k\+ (<CR>
737 call assert_equal(1, hasmapto('/^\k\+ (', 'l'))
738 lunmap ,f
739
740 " Cmdline mode mapping
741 call assert_equal(0, hasmapto('/^\k\+ (', 'c'))
742 cmap ,f /^\k\+ (<CR>
743 call assert_equal(1, hasmapto('/^\k\+ (', 'c'))
744 cunmap ,f
Bram Moolenaar8ba6bb72020-01-20 20:41:42 +0100745
746 call assert_equal(0, hasmapto('/^\k\+ (', 'n', 1))
747endfunc
748
749" Test for command-line completion of maps
750func Test_mapcomplete()
751 call assert_equal(['<buffer>', '<expr>', '<nowait>', '<script>',
752 \ '<silent>', '<special>', '<unique>'],
753 \ getcompletion('', 'mapping'))
754 call assert_equal([], getcompletion(',d', 'mapping'))
755
Bram Moolenaarc2a60ae2020-01-23 16:19:54 +0100756 call feedkeys(":unmap <buf\<C-A>\<C-B>\"\<CR>", 'tx')
757 call assert_equal('"unmap <buffer>', @:)
758
759 call feedkeys(":unabbr <buf\<C-A>\<C-B>\"\<CR>", 'tx')
760 call assert_equal('"unabbr <buffer>', @:)
761
Bram Moolenaar8ba6bb72020-01-20 20:41:42 +0100762 call feedkeys(":abbr! \<C-A>\<C-B>\"\<CR>", 'tx')
Bram Moolenaarc2a60ae2020-01-23 16:19:54 +0100763 call assert_equal("\"abbr! \x01", @:)
764
765 " Multiple matches for a map
766 nmap ,f /H<CR>
767 omap ,f /H<CR>
768 call feedkeys(":map ,\<C-A>\<C-B>\"\<CR>", 'tx')
769 call assert_equal('"map ,f', @:)
770 mapclear
771endfunc
772
Bram Moolenaar94075b22022-01-18 20:30:39 +0000773func GetAbbrText()
774 unabbr hola
775 return 'hello'
776endfunc
777
Bram Moolenaarc2a60ae2020-01-23 16:19:54 +0100778" Test for <expr> in abbreviation
779func Test_expr_abbr()
780 new
781 iabbr <expr> teh "the"
782 call feedkeys("iteh ", "tx")
783 call assert_equal('the ', getline(1))
784 iabclear
785 call setline(1, '')
786
787 " invalid <expr> abbreviation
788 abbr <expr> hte GetAbbr()
789 call assert_fails('normal ihte ', 'E117:')
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100790 call assert_equal('', getline(1))
Bram Moolenaarc2a60ae2020-01-23 16:19:54 +0100791 unabbr <expr> hte
792
Bram Moolenaar94075b22022-01-18 20:30:39 +0000793 " evaluating the expression deletes the abbreviation
794 abbr <expr> hola GetAbbrText()
795 call assert_equal('GetAbbrText()', maparg('hola', 'i', '1'))
796 call feedkeys("ahola \<Esc>", 'xt')
797 call assert_equal('hello ', getline('.'))
798 call assert_equal('', maparg('hola', 'i', '1'))
799
800 bwipe!
Bram Moolenaarc2a60ae2020-01-23 16:19:54 +0100801endfunc
802
803" Test for storing mappings in different modes in a vimrc file
804func Test_mkvimrc_mapmodes()
805 map a1 /a1
806 nmap a2 /a2
807 vmap a3 /a3
808 smap a4 /a4
809 xmap a5 /a5
810 omap a6 /a6
811 map! a7 /a7
812 imap a8 /a8
813 lmap a9 /a9
814 cmap a10 /a10
815 tmap a11 /a11
816 " Normal + Visual map
817 map a12 /a12
818 sunmap a12
819 ounmap a12
820 " Normal + Selectmode map
821 map a13 /a13
822 xunmap a13
823 ounmap a13
824 " Normal + OpPending map
825 map a14 /a14
826 vunmap a14
827 " Visual + Selectmode map
828 map a15 /a15
829 nunmap a15
830 ounmap a15
831 " Visual + OpPending map
832 map a16 /a16
833 nunmap a16
834 sunmap a16
835 " Selectmode + OpPending map
836 map a17 /a17
837 nunmap a17
838 xunmap a17
839 " Normal + Visual + Selectmode map
840 map a18 /a18
841 ounmap a18
842 " Normal + Visual + OpPending map
843 map a19 /a19
844 sunmap a19
845 " Normal + Selectmode + OpPending map
846 map a20 /a20
847 xunmap a20
848 " Visual + Selectmode + OpPending map
849 map a21 /a21
850 nunmap a21
851 " Mapping to Nop
852 map a22 <Nop>
853 " Script local mapping
854 map <script> a23 /a23
855
856 " Newline in {lhs} and {rhs} of a map
857 exe "map a24\<C-V>\<C-J> ia24\<C-V>\<C-J><Esc>"
858
859 " Abbreviation
860 abbr a25 A25
861 cabbr a26 A26
862 iabbr a27 A27
863
864 mkvimrc! Xvimrc
865 let l = readfile('Xvimrc')
866 call assert_equal(['map a1 /a1'], filter(copy(l), 'v:val =~ " a1 "'))
867 call assert_equal(['nmap a2 /a2'], filter(copy(l), 'v:val =~ " a2 "'))
868 call assert_equal(['vmap a3 /a3'], filter(copy(l), 'v:val =~ " a3 "'))
869 call assert_equal(['smap a4 /a4'], filter(copy(l), 'v:val =~ " a4 "'))
870 call assert_equal(['xmap a5 /a5'], filter(copy(l), 'v:val =~ " a5 "'))
871 call assert_equal(['omap a6 /a6'], filter(copy(l), 'v:val =~ " a6 "'))
872 call assert_equal(['map! a7 /a7'], filter(copy(l), 'v:val =~ " a7 "'))
873 call assert_equal(['imap a8 /a8'], filter(copy(l), 'v:val =~ " a8 "'))
874 call assert_equal(['lmap a9 /a9'], filter(copy(l), 'v:val =~ " a9 "'))
875 call assert_equal(['cmap a10 /a10'], filter(copy(l), 'v:val =~ " a10 "'))
876 call assert_equal(['tmap a11 /a11'], filter(copy(l), 'v:val =~ " a11 "'))
877 call assert_equal(['nmap a12 /a12', 'xmap a12 /a12'],
878 \ filter(copy(l), 'v:val =~ " a12 "'))
879 call assert_equal(['nmap a13 /a13', 'smap a13 /a13'],
880 \ filter(copy(l), 'v:val =~ " a13 "'))
881 call assert_equal(['nmap a14 /a14', 'omap a14 /a14'],
882 \ filter(copy(l), 'v:val =~ " a14 "'))
883 call assert_equal(['vmap a15 /a15'], filter(copy(l), 'v:val =~ " a15 "'))
884 call assert_equal(['xmap a16 /a16', 'omap a16 /a16'],
885 \ filter(copy(l), 'v:val =~ " a16 "'))
886 call assert_equal(['smap a17 /a17', 'omap a17 /a17'],
887 \ filter(copy(l), 'v:val =~ " a17 "'))
888 call assert_equal(['nmap a18 /a18', 'vmap a18 /a18'],
889 \ filter(copy(l), 'v:val =~ " a18 "'))
890 call assert_equal(['nmap a19 /a19', 'xmap a19 /a19', 'omap a19 /a19'],
891 \ filter(copy(l), 'v:val =~ " a19 "'))
892 call assert_equal(['nmap a20 /a20', 'smap a20 /a20', 'omap a20 /a20'],
893 \ filter(copy(l), 'v:val =~ " a20 "'))
894 call assert_equal(['vmap a21 /a21', 'omap a21 /a21'],
895 \ filter(copy(l), 'v:val =~ " a21 "'))
896 call assert_equal(['map a22 <Nop>'], filter(copy(l), 'v:val =~ " a22 "'))
897 call assert_equal([], filter(copy(l), 'v:val =~ " a23 "'))
898 call assert_equal(["map a24<NL> ia24<NL>\x16\e"],
899 \ filter(copy(l), 'v:val =~ " a24"'))
900
901 call assert_equal(['abbr a25 A25'], filter(copy(l), 'v:val =~ " a25 "'))
902 call assert_equal(['cabbr a26 A26'], filter(copy(l), 'v:val =~ " a26 "'))
903 call assert_equal(['iabbr a27 A27'], filter(copy(l), 'v:val =~ " a27 "'))
904 call delete('Xvimrc')
905
906 mapclear
907 nmapclear
908 vmapclear
909 xmapclear
910 smapclear
911 omapclear
912 imapclear
913 lmapclear
914 cmapclear
915 tmapclear
916endfunc
917
918" Test for recursive mapping ('maxmapdepth')
919func Test_map_recursive()
920 map x y
921 map y x
922 call assert_fails('normal x', 'E223:')
923 unmap x
924 unmap y
925endfunc
926
927" Test for removing an abbreviation using {rhs} and with space after {lhs}
928func Test_abbr_remove()
929 abbr foo bar
930 let d = maparg('foo', 'i', 1, 1)
931 call assert_equal(['foo', 'bar', '!'], [d.lhs, d.rhs, d.mode])
932 unabbr bar
933 call assert_equal({}, maparg('foo', 'i', 1, 1))
934
935 abbr foo bar
936 unabbr foo<space><tab>
937 call assert_equal({}, maparg('foo', 'i', 1, 1))
Bram Moolenaar8ba6bb72020-01-20 20:41:42 +0100938endfunc
939
Bram Moolenaar7f51bbe2020-01-24 20:21:19 +0100940" Trigger an abbreviation using a special key
941func Test_abbr_trigger_special()
942 new
943 iabbr teh the
944 call feedkeys("iteh\<F2>\<Esc>", 'xt')
945 call assert_equal('the<F2>', getline(1))
946 iunab teh
947 close!
948endfunc
949
950" Test for '<' in 'cpoptions'
951func Test_map_cpo_special_keycode()
952 set cpo-=<
953 imap x<Bslash>k Test
954 let d = maparg('x<Bslash>k', 'i', 0, 1)
955 call assert_equal(['x\k', 'Test', 'i'], [d.lhs, d.rhs, d.mode])
956 call feedkeys(":imap x\<C-A>\<C-B>\"\<CR>", 'tx')
957 call assert_equal('"imap x\k', @:)
958 iunmap x<Bslash>k
959 set cpo+=<
960 imap x<Bslash>k Test
961 let d = maparg('x<Bslash>k', 'i', 0, 1)
962 call assert_equal(['x<Bslash>k', 'Test', 'i'], [d.lhs, d.rhs, d.mode])
963 call feedkeys(":imap x\<C-A>\<C-B>\"\<CR>", 'tx')
964 call assert_equal('"imap x<Bslash>k', @:)
965 iunmap x<Bslash>k
966 set cpo-=<
967 " Modifying 'cpo' above adds some default mappings, remove them
968 mapclear
969 mapclear!
970endfunc
971
Bram Moolenaar957cf672020-11-12 14:21:06 +0100972" Test for <Cmd> key in maps to execute commands
973func Test_map_cmdkey()
974 new
975
976 " Error cases
977 let x = 0
978 noremap <F3> <Cmd><Cmd>let x = 1<CR>
979 call assert_fails('call feedkeys("\<F3>", "xt")', 'E1136:')
980 call assert_equal(0, x)
981
982 noremap <F3> <Cmd><F3>let x = 2<CR>
983 call assert_fails('call feedkeys("\<F3>", "xt")', 'E1137:')
984 call assert_equal(0, x)
985
986 noremap <F3> <Cmd>let x = 3
Bram Moolenaar806da512021-12-24 19:54:52 +0000987 call assert_fails('call feedkeys("\<F3>", "xt!")', 'E1255:')
Bram Moolenaar957cf672020-11-12 14:21:06 +0100988 call assert_equal(0, x)
989
990 " works in various modes and sees the correct mode()
991 noremap <F3> <Cmd>let m = mode(1)<CR>
992 noremap! <F3> <Cmd>let m = mode(1)<CR>
993
994 " normal mode
995 call feedkeys("\<F3>", 'xt')
996 call assert_equal('n', m)
997
998 " visual mode
999 call feedkeys("v\<F3>", 'xt!')
1000 call assert_equal('v', m)
1001 " shouldn't leave the visual mode
1002 call assert_equal('v', mode(1))
1003 call feedkeys("\<Esc>", 'xt')
1004 call assert_equal('n', mode(1))
1005
1006 " visual mapping in select mode
1007 call feedkeys("gh\<F3>", 'xt!')
1008 call assert_equal('v', m)
1009 " shouldn't leave select mode
1010 call assert_equal('s', mode(1))
1011 call feedkeys("\<Esc>", 'xt')
1012 call assert_equal('n', mode(1))
1013
1014 " select mode mapping
1015 snoremap <F3> <Cmd>let m = mode(1)<cr>
1016 call feedkeys("gh\<F3>", 'xt!')
1017 call assert_equal('s', m)
1018 " shouldn't leave select mode
1019 call assert_equal('s', mode(1))
1020 call feedkeys("\<Esc>", 'xt')
1021 call assert_equal('n', mode(1))
1022
1023 " operator-pending mode
1024 call feedkeys("d\<F3>", 'xt!')
1025 call assert_equal('no', m)
1026 " leaves the operator-pending mode
1027 call assert_equal('n', mode(1))
1028
1029 " insert mode
1030 call feedkeys("i\<F3>abc", 'xt')
1031 call assert_equal('i', m)
1032 call assert_equal('abc', getline('.'))
1033
1034 " replace mode
1035 call feedkeys("0R\<F3>two", 'xt')
1036 call assert_equal('R', m)
1037 call assert_equal('two', getline('.'))
1038
1039 " virtual replace mode
1040 call setline('.', "one\ttwo")
1041 call feedkeys("4|gR\<F3>xxx", 'xt')
1042 call assert_equal('Rv', m)
1043 call assert_equal("onexxx\ttwo", getline('.'))
1044
1045 " cmdline mode
1046 call feedkeys(":\<F3>\"xxx\<CR>", 'xt!')
1047 call assert_equal('c', m)
1048 call assert_equal('"xxx', @:)
1049
1050 " terminal mode
1051 if CanRunVimInTerminal()
1052 tnoremap <F3> <Cmd>let m = mode(1)<CR>
1053 let buf = Run_shell_in_terminal({})
1054 call feedkeys("\<F3>", 'xt')
1055 call assert_equal('t', m)
1056 call assert_equal('t', mode(1))
1057 call StopShellInTerminal(buf)
Bram Moolenaar957cf672020-11-12 14:21:06 +01001058 close!
1059 tunmap <F3>
1060 endif
1061
1062 " invoke cmdline mode recursively
1063 noremap! <F2> <Cmd>norm! :foo<CR>
1064 %d
1065 call setline(1, ['some short lines', 'of test text'])
1066 call feedkeys(":bar\<F2>x\<C-B>\"\r", 'xt')
1067 call assert_equal('"barx', @:)
1068 unmap! <F2>
1069
1070 " test for calling a <SID> function
1071 let lines =<< trim END
1072 map <F2> <Cmd>call <SID>do_it()<CR>
1073 func s:do_it()
1074 let g:x = 32
1075 endfunc
1076 END
1077 call writefile(lines, 'Xscript')
1078 source Xscript
1079 call feedkeys("\<F2>", 'xt')
1080 call assert_equal(32, g:x)
1081 call delete('Xscript')
1082
1083 unmap <F3>
1084 unmap! <F3>
1085 %bw!
Bram Moolenaar4a441202020-11-28 14:43:26 +01001086
1087 " command line ending in "0" is handled without errors
1088 onoremap ix <cmd>eval 0<cr>
1089 call feedkeys('dix.', 'xt')
1090 ounmap ix
Bram Moolenaar957cf672020-11-12 14:21:06 +01001091endfunc
1092
1093" text object enters visual mode
1094func TextObj()
1095 if mode() !=# "v"
1096 normal! v
1097 end
1098 call cursor(1, 3)
1099 normal! o
1100 call cursor(2, 4)
1101endfunc
1102
1103func s:cmdmap(lhs, rhs)
1104 exe 'noremap ' .. a:lhs .. ' <Cmd>' .. a:rhs .. '<CR>'
1105 exe 'noremap! ' .. a:lhs .. ' <Cmd>' .. a:rhs .. '<CR>'
1106endfunc
1107
1108func s:cmdunmap(lhs)
1109 exe 'unmap ' .. a:lhs
1110 exe 'unmap! ' .. a:lhs
1111endfunc
1112
1113" Map various <Fx> keys used by the <Cmd> key tests
1114func s:setupMaps()
1115 call s:cmdmap('<F3>', 'let m = mode(1)')
1116 call s:cmdmap('<F4>', 'normal! ww')
1117 call s:cmdmap('<F5>', 'normal! "ay')
1118 call s:cmdmap('<F6>', 'throw "very error"')
1119 call s:cmdmap('<F7>', 'call TextObj()')
1120 call s:cmdmap('<F8>', 'startinsert')
1121 call s:cmdmap('<F9>', 'stopinsert')
1122endfunc
1123
1124" Remove the mappings setup by setupMaps()
1125func s:cleanupMaps()
1126 call s:cmdunmap('<F3>')
1127 call s:cmdunmap('<F4>')
1128 call s:cmdunmap('<F5>')
1129 call s:cmdunmap('<F6>')
1130 call s:cmdunmap('<F7>')
1131 call s:cmdunmap('<F8>')
1132 call s:cmdunmap('<F9>')
1133endfunc
1134
1135" Test for <Cmd> mapping in normal mode
1136func Test_map_cmdkey_normal_mode()
1137 new
1138 call s:setupMaps()
1139
1140 " check v:count and v:register works
1141 call s:cmdmap('<F2>', 'let s = [mode(1), v:count, v:register]')
1142 call feedkeys("\<F2>", 'xt')
1143 call assert_equal(['n', 0, '"'], s)
1144 call feedkeys("7\<F2>", 'xt')
1145 call assert_equal(['n', 7, '"'], s)
1146 call feedkeys("\"e\<F2>", 'xt')
1147 call assert_equal(['n', 0, 'e'], s)
1148 call feedkeys("5\"k\<F2>", 'xt')
1149 call assert_equal(['n', 5, 'k'], s)
1150 call s:cmdunmap('<F2>')
1151
1152 call setline(1, ['some short lines', 'of test text'])
1153 call feedkeys("\<F7>y", 'xt')
1154 call assert_equal("me short lines\nof t", @")
1155 call assert_equal('v', getregtype('"'))
1156 call assert_equal([0, 1, 3, 0], getpos("'<"))
1157 call assert_equal([0, 2, 4, 0], getpos("'>"))
1158
1159 " startinsert
1160 %d
1161 call feedkeys("\<F8>abc", 'xt')
1162 call assert_equal('abc', getline(1))
1163
1164 " feedkeys are not executed immediately
1165 noremap ,a <Cmd>call feedkeys("aalpha") \| let g:a = getline(2)<CR>
1166 %d
1167 call setline(1, ['some short lines', 'of test text'])
1168 call cursor(2, 3)
1169 call feedkeys(",a\<F3>", 'xt')
1170 call assert_equal('of test text', g:a)
1171 call assert_equal('n', m)
1172 call assert_equal(['some short lines', 'of alphatest text'], getline(1, '$'))
1173 nunmap ,a
1174
1175 " feedkeys(..., 'x') is executed immediately, but insert mode is aborted
1176 noremap ,b <Cmd>call feedkeys("abeta", 'x') \| let g:b = getline(2)<CR>
1177 call feedkeys(",b\<F3>", 'xt')
1178 call assert_equal('n', m)
1179 call assert_equal('of alphabetatest text', g:b)
1180 nunmap ,b
1181
1182 call s:cleanupMaps()
1183 %bw!
1184endfunc
1185
1186" Test for <Cmd> mapping with the :normal command
1187func Test_map_cmdkey_normal_cmd()
1188 new
1189 noremap ,x <Cmd>call append(1, "xx") \| call append(1, "aa")<CR>
1190 noremap ,f <Cmd>nosuchcommand<CR>
1191 noremap ,e <Cmd>throw "very error" \| call append(1, "yy")<CR>
1192 noremap ,m <Cmd>echoerr "The message." \| call append(1, "zz")<CR>
1193 noremap ,w <Cmd>for i in range(5) \| if i==1 \| echoerr "Err" \| endif \| call append(1, i) \| endfor<CR>
1194
1195 call setline(1, ['some short lines', 'of test text'])
1196 exe "norm ,x\r"
1197 call assert_equal(['some short lines', 'aa', 'xx', 'of test text'], getline(1, '$'))
1198
1199 call assert_fails('norm ,f', 'E492:')
1200 call assert_fails('norm ,e', 'very error')
1201 call assert_fails('norm ,m', 'The message.')
1202 call assert_equal(['some short lines', 'aa', 'xx', 'of test text'], getline(1, '$'))
1203
1204 %d
1205 let caught_err = 0
1206 try
1207 exe "normal ,w"
1208 catch /Vim(echoerr):Err/
1209 let caught_err = 1
1210 endtry
1211 call assert_equal(1, caught_err)
1212 call assert_equal(['', '0'], getline(1, '$'))
1213
1214 %d
1215 call assert_fails('normal ,w', 'Err')
1216 call assert_equal(['', '4', '3', '2' ,'1', '0'], getline(1, '$'))
1217 call assert_equal(1, line('.'))
1218
1219 nunmap ,x
1220 nunmap ,f
1221 nunmap ,e
1222 nunmap ,m
1223 nunmap ,w
1224 %bw!
1225endfunc
1226
1227" Test for <Cmd> mapping in visual mode
1228func Test_map_cmdkey_visual_mode()
1229 new
1230 set showmode
1231 call s:setupMaps()
1232
1233 call setline(1, ['some short lines', 'of test text'])
1234 call feedkeys("v\<F4>", 'xt!')
1235 call assert_equal(['v', 1, 12], [mode(1), col('v'), col('.')])
1236
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +01001237 " can invoke an operator, ending the visual mode
Bram Moolenaar957cf672020-11-12 14:21:06 +01001238 let @a = ''
1239 call feedkeys("\<F5>", 'xt!')
1240 call assert_equal('n', mode(1))
1241 call assert_equal('some short l', @a)
1242
1243 " error doesn't interrupt visual mode
1244 call assert_fails('call feedkeys("ggvw\<F6>", "xt!")', 'E605:')
1245 call assert_equal(['v', 1, 6], [mode(1), col('v'), col('.')])
1246 call feedkeys("\<F7>", 'xt!')
1247 call assert_equal(['v', 1, 3, 2, 4], [mode(1), line('v'), col('v'), line('.'), col('.')])
1248
1249 " startinsert gives "-- (insert) VISUAL --" mode
1250 call feedkeys("\<F8>", 'xt!')
1251 call assert_equal(['v', 1, 3, 2, 4], [mode(1), line('v'), col('v'), line('.'), col('.')])
1252 redraw!
1253 call assert_match('^-- (insert) VISUAL --', Screenline(&lines))
1254 call feedkeys("\<Esc>new ", 'x')
1255 call assert_equal(['some short lines', 'of new test text'], getline(1, '$'))
1256
1257 call s:cleanupMaps()
1258 set showmode&
1259 %bw!
1260endfunc
1261
1262" Test for <Cmd> mapping in select mode
1263func Test_map_cmdkey_select_mode()
1264 new
1265 set showmode
1266 call s:setupMaps()
1267
1268 snoremap <F1> <cmd>throw "very error"<CR>
1269 snoremap <F2> <cmd>normal! <c-g>"by<CR>
1270 call setline(1, ['some short lines', 'of test text'])
1271
1272 call feedkeys("gh\<F4>", "xt!")
1273 call assert_equal(['s', 1, 12], [mode(1), col('v'), col('.')])
1274 redraw!
1275 call assert_match('^-- SELECT --', Screenline(&lines))
1276
1277 " visual mapping in select mode restarts select mode after operator
1278 let @a = ''
1279 call feedkeys("\<F5>", 'xt!')
1280 call assert_equal('s', mode(1))
1281 call assert_equal('some short l', @a)
1282
1283 " select mode mapping works, and does not restart select mode
1284 let @b = ''
1285 call feedkeys("\<F2>", 'xt!')
1286 call assert_equal('n', mode(1))
1287 call assert_equal('some short l', @b)
1288
1289 " error doesn't interrupt temporary visual mode
1290 call assert_fails('call feedkeys("\<Esc>ggvw\<C-G>\<F6>", "xt!")', 'E605:')
1291 redraw!
1292 call assert_match('^-- VISUAL --', Screenline(&lines))
1293 " quirk: restoration of select mode is not performed
1294 call assert_equal(['v', 1, 6], [mode(1), col('v'), col('.')])
1295
1296 " error doesn't interrupt select mode
1297 call assert_fails('call feedkeys("\<Esc>ggvw\<C-G>\<F1>", "xt!")', 'E605:')
1298 redraw!
1299 call assert_match('^-- SELECT --', Screenline(&lines))
1300 call assert_equal(['s', 1, 6], [mode(1), col('v'), col('.')])
1301
1302 call feedkeys("\<F7>", 'xt!')
1303 redraw!
1304 call assert_match('^-- SELECT --', Screenline(&lines))
1305 call assert_equal(['s', 1, 3, 2, 4], [mode(1), line('v'), col('v'), line('.'), col('.')])
1306
1307 " startinsert gives "-- SELECT (insert) --" mode
1308 call feedkeys("\<F8>", 'xt!')
1309 redraw!
1310 call assert_match('^-- (insert) SELECT --', Screenline(&lines))
1311 call assert_equal(['s', 1, 3, 2, 4], [mode(1), line('v'), col('v'), line('.'), col('.')])
1312 call feedkeys("\<Esc>new ", 'x')
1313 call assert_equal(['some short lines', 'of new test text'], getline(1, '$'))
1314
1315 sunmap <F1>
1316 sunmap <F2>
1317 call s:cleanupMaps()
1318 set showmode&
1319 %bw!
1320endfunc
1321
1322" Test for <Cmd> mapping in operator-pending mode
1323func Test_map_cmdkey_op_pending_mode()
1324 new
1325 call s:setupMaps()
1326
1327 call setline(1, ['some short lines', 'of test text'])
1328 call feedkeys("d\<F4>", 'xt')
1329 call assert_equal(['lines', 'of test text'], getline(1, '$'))
1330 call assert_equal(['some short '], getreg('"', 1, 1))
1331 " create a new undo point
1332 let &undolevels = &undolevels
1333
1334 call feedkeys(".", 'xt')
1335 call assert_equal(['test text'], getline(1, '$'))
1336 call assert_equal(['lines', 'of '], getreg('"', 1, 1))
1337 " create a new undo point
1338 let &undolevels = &undolevels
1339
1340 call feedkeys("uu", 'xt')
1341 call assert_equal(['some short lines', 'of test text'], getline(1, '$'))
1342
1343 " error aborts operator-pending, operator not performed
1344 call assert_fails('call feedkeys("d\<F6>", "xt")', 'E605:')
1345 call assert_equal(['some short lines', 'of test text'], getline(1, '$'))
1346
1347 call feedkeys("\"bd\<F7>", 'xt')
1348 call assert_equal(['soest text'], getline(1, '$'))
1349 call assert_equal(['me short lines', 'of t'], getreg('b', 1, 1))
1350
1351 " startinsert aborts operator
1352 call feedkeys("d\<F8>cc", 'xt')
1353 call assert_equal(['soccest text'], getline(1, '$'))
1354
1355 call s:cleanupMaps()
1356 %bw!
1357endfunc
1358
1359" Test for <Cmd> mapping in insert mode
1360func Test_map_cmdkey_insert_mode()
1361 new
1362 call s:setupMaps()
1363
1364 call setline(1, ['some short lines', 'of test text'])
1365 " works the same as <C-O>w<C-O>w
1366 call feedkeys("iindeed \<F4>little ", 'xt')
1367 call assert_equal(['indeed some short little lines', 'of test text'], getline(1, '$'))
1368 call assert_fails('call feedkeys("i\<F6> 2", "xt")', 'E605:')
1369 call assert_equal(['indeed some short little 2 lines', 'of test text'], getline(1, '$'))
1370
1371 " Note when entering visual mode from InsertEnter autocmd, an async event,
1372 " or a <Cmd> mapping, vim ends up in undocumented "INSERT VISUAL" mode.
1373 call feedkeys("i\<F7>stuff ", 'xt')
1374 call assert_equal(['indeed some short little 2 lines', 'of stuff test text'], getline(1, '$'))
1375 call assert_equal(['v', 1, 3, 2, 9], [mode(1), line('v'), col('v'), line('.'), col('.')])
1376
1377 call feedkeys("\<F5>", 'xt')
1378 call assert_equal(['deed some short little 2 lines', 'of stuff '], getreg('a', 1, 1))
1379
1380 " also works as part of abbreviation
1381 abbr foo <Cmd>let g:y = 17<CR>bar
1382 exe "normal i\<space>foo "
1383 call assert_equal(17, g:y)
1384 call assert_equal('in bar deed some short little 2 lines', getline(1))
1385 unabbr foo
1386
1387 " :startinsert does nothing
1388 call setline(1, 'foo bar')
1389 call feedkeys("ggi\<F8>vim", 'xt')
1390 call assert_equal('vimfoo bar', getline(1))
1391
1392 " :stopinsert works
1393 call feedkeys("ggi\<F9>Abc", 'xt')
1394 call assert_equal('vimfoo barbc', getline(1))
1395
1396 call s:cleanupMaps()
1397 %bw!
1398endfunc
1399
1400" Test for <Cmd> mapping in insert-completion mode
1401func Test_map_cmdkey_insert_complete_mode()
1402 new
1403 call s:setupMaps()
1404
1405 call setline(1, 'some short lines')
1406 call feedkeys("os\<C-X>\<C-N>\<F3>\<C-N> ", 'xt')
1407 call assert_equal('ic', m)
1408 call assert_equal(['some short lines', 'short '], getline(1, '$'))
1409
1410 call s:cleanupMaps()
1411 %bw!
1412endfunc
1413
1414" Test for <Cmd> mapping in cmdline mode
1415func Test_map_cmdkey_cmdline_mode()
1416 new
1417 call s:setupMaps()
1418
1419 call setline(1, ['some short lines', 'of test text'])
1420 let x = 0
1421 call feedkeys(":let x\<F3>= 10\r", 'xt')
1422 call assert_equal('c', m)
1423 call assert_equal(10, x)
1424
1425 " exception doesn't leave cmdline mode
1426 call assert_fails('call feedkeys(":let x\<F6>= 20\r", "xt")', 'E605:')
1427 call assert_equal(20, x)
1428
1429 " move cursor in the buffer from cmdline mode
1430 call feedkeys(":let x\<F4>= 30\r", 'xt')
1431 call assert_equal(30, x)
1432 call assert_equal(12, col('.'))
1433
1434 " :startinsert takes effect after leaving cmdline mode
1435 call feedkeys(":let x\<F8>= 40\rnew ", 'xt')
1436 call assert_equal(40, x)
1437 call assert_equal('some short new lines', getline(1))
1438
1439 call s:cleanupMaps()
1440 %bw!
1441endfunc
1442
Bram Moolenaarc77534c2020-11-18 11:34:37 +01001443func Test_map_cmdkey_redo()
1444 func SelectDash()
1445 call search('^---\n\zs', 'bcW')
1446 norm! V
1447 call search('\n\ze---$', 'W')
1448 endfunc
1449
1450 let text =<< trim END
1451 ---
1452 aaa
1453 ---
1454 bbb
1455 bbb
1456 ---
1457 ccc
1458 ccc
1459 ccc
1460 ---
1461 END
1462 new Xcmdtext
1463 call setline(1, text)
1464
1465 onoremap <silent> i- <Cmd>call SelectDash()<CR>
1466 call feedkeys('2Gdi-', 'xt')
1467 call assert_equal(['---', '---'], getline(1, 2))
1468 call feedkeys('j.', 'xt')
1469 call assert_equal(['---', '---', '---'], getline(1, 3))
1470 call feedkeys('j.', 'xt')
1471 call assert_equal(['---', '---', '---', '---'], getline(1, 4))
1472
1473 bwipe!
1474 call delete('Xcmdtext')
1475 delfunc SelectDash
1476 ounmap i-
1477endfunc
1478
Bram Moolenaara9725222022-01-16 13:30:33 +00001479func Test_map_script_cmd_restore()
1480 let lines =<< trim END
1481 vim9script
1482 nnoremap <F3> <ScriptCmd>eval 1 + 2<CR>
1483 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001484 call v9.CheckScriptSuccess(lines)
Bram Moolenaara9725222022-01-16 13:30:33 +00001485 call feedkeys("\<F3>:let g:result = 3+4\<CR>", 'xtc')
1486 call assert_equal(7, g:result)
1487
1488 nunmap <F3>
1489 unlet g:result
1490endfunc
1491
Bram Moolenaardc987762022-01-16 15:52:35 +00001492func Test_map_script_cmd_finds_func()
1493 let lines =<< trim END
1494 vim9script
1495 onoremap <F3> <ScriptCmd>Func()<CR>
1496 def Func()
1497 g:func_called = 'yes'
1498 enddef
1499 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001500 call v9.CheckScriptSuccess(lines)
Bram Moolenaardc987762022-01-16 15:52:35 +00001501 call feedkeys("y\<F3>\<Esc>", 'xtc')
1502 call assert_equal('yes', g:func_called)
1503
1504 ounmap <F3>
1505 unlet g:func_called
1506endfunc
1507
Bram Moolenaarf61c89d2022-01-19 22:51:48 +00001508func Test_map_script_cmd_survives_unmap()
1509 let lines =<< trim END
1510 vim9script
1511 var n = 123
1512 nnoremap <F4> <ScriptCmd><CR>
1513 autocmd CmdlineEnter * silent! nunmap <F4>
1514 nnoremap <F3> :<ScriptCmd>eval setbufvar(bufnr(), "result", n)<CR>
1515 feedkeys("\<F3>\<CR>", 'xct')
1516 assert_equal(123, b:result)
1517 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001518 call v9.CheckScriptSuccess(lines)
Bram Moolenaarf61c89d2022-01-19 22:51:48 +00001519
1520 nunmap <F3>
1521 unlet b:result
Bram Moolenaarca34db32022-01-20 11:17:18 +00001522 autocmd! CmdlineEnter
Bram Moolenaarf61c89d2022-01-19 22:51:48 +00001523endfunc
1524
Bram Moolenaar1f448d92021-03-22 19:37:06 +01001525" Test for using <script> with a map to remap characters in rhs
1526func Test_script_local_remap()
1527 new
1528 inoremap <buffer> <SID>xyz mno
1529 inoremap <buffer> <script> abc st<SID>xyzre
1530 normal iabc
1531 call assert_equal('stmnore', getline(1))
1532 bwipe!
1533endfunc
1534
Bram Moolenaar4934ed32021-04-30 19:43:11 +02001535func Test_abbreviate_multi_byte()
1536 new
1537 iabbrev foo bar
1538 call feedkeys("ifoo…\<Esc>", 'xt')
1539 call assert_equal("bar…", getline(1))
1540 iunabbrev foo
1541 bwipe!
1542endfunc
1543
Yegappan Lakshmanan2d6d7182021-06-13 21:52:48 +02001544" Test for abbreviations with 'latin1' encoding
1545func Test_abbreviate_latin1_encoding()
1546 set encoding=latin1
1547 call assert_fails('abbr ab#$c ABC', 'E474:')
1548 new
1549 iabbr <buffer> #i #include
1550 iabbr <buffer> ## #enddef
1551 exe "normal i#i\<C-]>"
1552 call assert_equal('#include', getline(1))
1553 exe "normal 0Di##\<C-]>"
1554 call assert_equal('#enddef', getline(1))
1555 %bw!
1556 set encoding=utf-8
1557endfunc
1558
Bram Moolenaar1fc34222022-03-03 13:56:24 +00001559" Test for <Plug> always being mapped, even when used with "noremap".
1560func Test_plug_remap()
1561 let g:foo = 0
1562 nnoremap <Plug>(Increase_x) <Cmd>let g:foo += 1<CR>
1563 nmap <F2> <Plug>(Increase_x)
1564 nnoremap <F3> <Plug>(Increase_x)
1565 call feedkeys("\<F2>", 'xt')
1566 call assert_equal(1, g:foo)
1567 call feedkeys("\<F3>", 'xt')
1568 call assert_equal(2, g:foo)
1569 nnoremap x <Nop>
1570 nmap <F4> x<Plug>(Increase_x)x
1571 nnoremap <F5> x<Plug>(Increase_x)x
1572 call setline(1, 'Some text')
1573 normal! gg$
1574 call feedkeys("\<F4>", 'xt')
1575 call assert_equal(3, g:foo)
1576 call assert_equal('Some text', getline(1))
1577 call feedkeys("\<F5>", 'xt')
1578 call assert_equal(4, g:foo)
1579 call assert_equal('Some te', getline(1))
1580 nunmap <Plug>(Increase_x)
1581 nunmap <F2>
1582 nunmap <F3>
1583 nunmap <F4>
1584 nunmap <F5>
1585 unlet g:foo
1586 %bw!
1587endfunc
1588
zeertzjqac92ab72022-04-24 15:58:30 +01001589func Test_mouse_drag_mapped_start_select()
1590 set mouse=a
1591 set selectmode=key,mouse
1592 func ClickExpr()
1593 call test_setmouse(1, 1)
1594 return "\<LeftMouse>"
1595 endfunc
1596 func DragExpr()
1597 call test_setmouse(1, 2)
1598 return "\<LeftDrag>"
1599 endfunc
1600 nnoremap <expr> <F2> ClickExpr()
1601 nmap <expr> <F3> DragExpr()
1602
1603 nnoremap <LeftDrag> <LeftDrag><Cmd><CR>
1604 exe "normal \<F2>\<F3>"
1605 call assert_equal('s', mode())
1606 exe "normal! \<C-\>\<C-N>"
1607
1608 nunmap <LeftDrag>
1609 nunmap <F2>
1610 nunmap <F3>
1611 delfunc ClickExpr
1612 delfunc DragExpr
1613 set selectmode&
1614 set mouse&
1615endfunc
1616
zeertzjq0f68e6c2022-04-05 13:17:01 +01001617" Test for mapping <LeftDrag> in Insert mode
1618func Test_mouse_drag_insert_map()
1619 set mouse=a
1620 func ClickExpr()
1621 call test_setmouse(1, 1)
1622 return "\<LeftMouse>"
1623 endfunc
1624 func DragExpr()
1625 call test_setmouse(1, 2)
1626 return "\<LeftDrag>"
1627 endfunc
1628 inoremap <expr> <F2> ClickExpr()
1629 imap <expr> <F3> DragExpr()
1630
1631 inoremap <LeftDrag> <LeftDrag><Cmd>let g:dragged = 1<CR>
1632 exe "normal i\<F2>\<F3>"
1633 call assert_equal(1, g:dragged)
1634 call assert_equal('v', mode())
1635 exe "normal! \<C-\>\<C-N>"
1636 unlet g:dragged
1637
1638 inoremap <LeftDrag> <LeftDrag><C-\><C-N>
1639 exe "normal i\<F2>\<F3>"
1640 call assert_equal('n', mode())
1641
1642 iunmap <LeftDrag>
1643 iunmap <F2>
1644 iunmap <F3>
1645 delfunc ClickExpr
1646 delfunc DragExpr
1647 set mouse&
1648endfunc
1649
zeertzjqabeb09b2022-04-26 12:29:43 +01001650func Test_unmap_simplifiable()
zeertzjqa4e33322022-04-24 17:07:53 +01001651 map <C-I> foo
1652 map <Tab> bar
1653 call assert_equal('foo', maparg('<C-I>'))
1654 call assert_equal('bar', maparg('<Tab>'))
1655 unmap <C-I>
1656 call assert_equal('', maparg('<C-I>'))
1657 call assert_equal('bar', maparg('<Tab>'))
1658 unmap <Tab>
zeertzjqabeb09b2022-04-26 12:29:43 +01001659
1660 map <C-I> foo
1661 unmap <Tab>
1662 " This should not error
1663 unmap <C-I>
zeertzjqa4e33322022-04-24 17:07:53 +01001664endfunc
1665
zeertzjqdb088872022-05-02 22:53:45 +01001666func Test_expr_map_escape_special()
1667 nnoremap … <Cmd>let g:got_ellipsis += 1<CR>
1668 func Func()
1669 return '…'
1670 endfunc
1671 nmap <expr> <F2> Func()
1672 let g:got_ellipsis = 0
1673 call feedkeys("\<F2>", 'xt')
1674 call assert_equal(1, g:got_ellipsis)
1675 delfunc Func
1676 nunmap <F2>
1677 unlet g:got_ellipsis
1678 nunmap …
1679endfunc
1680
Bram Moolenaar8ba6bb72020-01-20 20:41:42 +01001681" vim: shiftwidth=2 sts=2 expandtab