blob: 71548edda0cc4e83238d549c179372f180a1013f [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 Moolenaar26d98212019-01-27 22:32:55 +01005
Bram Moolenaar2d1a2482016-08-14 15:32:11 +02006func Test_abbreviation()
7 " abbreviation with 0x80 should work
8 inoreab чкпр vim
9 call feedkeys("Goчкпр \<Esc>", "xt")
10 call assert_equal('vim ', getline('$'))
11 iunab чкпр
12 set nomodified
13endfunc
14
Bram Moolenaar8485be42019-04-23 16:36:05 +020015func Test_abclear()
16 abbrev foo foobar
17 iabbrev fooi foobari
18 cabbrev fooc foobarc
19 call assert_equal("\n\n"
20 \ .. "c fooc foobarc\n"
21 \ .. "i fooi foobari\n"
22 \ .. "! foo foobar", execute('abbrev'))
23
24 iabclear
25 call assert_equal("\n\n"
26 \ .. "c fooc foobarc\n"
27 \ .. "c foo foobar", execute('abbrev'))
28 abbrev foo foobar
29 iabbrev fooi foobari
30
31 cabclear
32 call assert_equal("\n\n"
33 \ .. "i fooi foobari\n"
34 \ .. "i foo foobar", execute('abbrev'))
35 abbrev foo foobar
36 cabbrev fooc foobarc
37
38 abclear
39 call assert_equal("\n\nNo abbreviation found", execute('abbrev'))
40endfunc
41
42func Test_abclear_buffer()
43 abbrev foo foobar
44 new X1
45 abbrev <buffer> foo1 foobar1
46 new X2
47 abbrev <buffer> foo2 foobar2
48
49 call assert_equal("\n\n"
50 \ .. "! foo2 @foobar2\n"
51 \ .. "! foo foobar", execute('abbrev'))
52
53 abclear <buffer>
54 call assert_equal("\n\n"
55 \ .. "! foo foobar", execute('abbrev'))
56
57 b X1
58 call assert_equal("\n\n"
59 \ .. "! foo1 @foobar1\n"
60 \ .. "! foo foobar", execute('abbrev'))
61 abclear <buffer>
62 call assert_equal("\n\n"
63 \ .. "! foo foobar", execute('abbrev'))
64
65 abclear
66 call assert_equal("\n\nNo abbreviation found", execute('abbrev'))
67
68 %bwipe
69endfunc
70
Bram Moolenaar2d1a2482016-08-14 15:32:11 +020071func Test_map_ctrl_c_insert()
72 " mapping of ctrl-c in Insert mode
73 set cpo-=< cpo-=k
74 inoremap <c-c> <ctrl-c>
75 cnoremap <c-c> dummy
76 cunmap <c-c>
77 call feedkeys("GoTEST2: CTRL-C |\<C-C>A|\<Esc>", "xt")
78 call assert_equal('TEST2: CTRL-C |<ctrl-c>A|', getline('$'))
79 unmap! <c-c>
80 set nomodified
81endfunc
82
83func Test_map_ctrl_c_visual()
84 " mapping of ctrl-c in Visual mode
85 vnoremap <c-c> :<C-u>$put ='vmap works'
86 call feedkeys("GV\<C-C>\<CR>", "xt")
87 call assert_equal('vmap works', getline('$'))
88 vunmap <c-c>
89 set nomodified
90endfunc
91
92func Test_map_langmap()
Bram Moolenaar920694c2016-08-21 17:45:02 +020093 if !has('langmap')
94 return
95 endif
96
97 " check langmap applies in normal mode
98 set langmap=+- nolangremap
99 new
100 call setline(1, ['a', 'b', 'c'])
101 2
102 call assert_equal('b', getline('.'))
103 call feedkeys("+", "xt")
104 call assert_equal('a', getline('.'))
105
106 " check no remapping
107 map x +
108 2
109 call feedkeys("x", "xt")
110 call assert_equal('c', getline('.'))
111
112 " check with remapping
113 set langremap
114 2
115 call feedkeys("x", "xt")
116 call assert_equal('a', getline('.'))
117
118 unmap x
119 bwipe!
120
121 " 'langnoremap' follows 'langremap' and vise versa
122 set langremap
123 set langnoremap
124 call assert_equal(0, &langremap)
125 set langremap
126 call assert_equal(0, &langnoremap)
127 set nolangremap
128 call assert_equal(1, &langnoremap)
129
Bram Moolenaarda9ce2c2016-09-02 19:34:10 +0200130 " check default values
131 set langnoremap&
132 call assert_equal(0, &langnoremap)
133 call assert_equal(1, &langremap)
134 set langremap&
135 call assert_equal(0, &langnoremap)
136 call assert_equal(1, &langremap)
137
Bram Moolenaar920694c2016-08-21 17:45:02 +0200138 " langmap should not apply in insert mode, 'langremap' doesn't matter
139 set langmap=+{ nolangremap
140 call feedkeys("Go+\<Esc>", "xt")
141 call assert_equal('+', getline('$'))
142 set langmap=+{ langremap
Bram Moolenaar2d1a2482016-08-14 15:32:11 +0200143 call feedkeys("Go+\<Esc>", "xt")
144 call assert_equal('+', getline('$'))
145
Bram Moolenaar920694c2016-08-21 17:45:02 +0200146 " langmap used for register name in insert mode.
147 call setreg('a', 'aaaa')
148 call setreg('b', 'bbbb')
149 call setreg('c', 'cccc')
150 set langmap=ab langremap
151 call feedkeys("Go\<C-R>a\<Esc>", "xt")
152 call assert_equal('bbbb', getline('$'))
153 call feedkeys("Go\<C-R>\<C-R>a\<Esc>", "xt")
154 call assert_equal('bbbb', getline('$'))
155 " mapping does not apply
156 imap c a
157 call feedkeys("Go\<C-R>c\<Esc>", "xt")
158 call assert_equal('cccc', getline('$'))
159 imap a c
160 call feedkeys("Go\<C-R>a\<Esc>", "xt")
161 call assert_equal('bbbb', getline('$'))
162
163 " langmap should not apply in Command-line mode
164 set langmap=+{ nolangremap
Bram Moolenaar2d1a2482016-08-14 15:32:11 +0200165 call feedkeys(":call append(line('$'), '+')\<CR>", "xt")
166 call assert_equal('+', getline('$'))
Bram Moolenaar2d1a2482016-08-14 15:32:11 +0200167
Bram Moolenaare90858d2017-02-01 17:24:34 +0100168 iunmap a
169 iunmap c
Bram Moolenaar2d1a2482016-08-14 15:32:11 +0200170 set nomodified
171endfunc
172
173func Test_map_feedkeys()
174 " issue #212 (feedkeys insert mapping at current position)
175 nnoremap . :call feedkeys(".", "in")<cr>
176 call setline('$', ['a b c d', 'a b c d'])
177 $-1
178 call feedkeys("0qqdw.ifoo\<Esc>qj0@q\<Esc>", "xt")
179 call assert_equal(['fooc d', 'fooc d'], getline(line('$') - 1, line('$')))
Bram Moolenaare90858d2017-02-01 17:24:34 +0100180 nunmap .
Bram Moolenaar2d1a2482016-08-14 15:32:11 +0200181 set nomodified
182endfunc
183
184func Test_map_cursor()
185 " <c-g>U<cursor> works only within a single line
186 imapclear
187 imap ( ()<c-g>U<left>
188 call feedkeys("G2o\<Esc>ki\<CR>Test1: text with a (here some more text\<Esc>k.", "xt")
189 call assert_equal('Test1: text with a (here some more text)', getline(line('$') - 2))
190 call assert_equal('Test1: text with a (here some more text)', getline(line('$') - 1))
191
192 " test undo
193 call feedkeys("G2o\<Esc>ki\<CR>Test2: text wit a (here some more text [und undo]\<C-G>u\<Esc>k.u", "xt")
194 call assert_equal('', getline(line('$') - 2))
195 call assert_equal('Test2: text wit a (here some more text [und undo])', getline(line('$') - 1))
196 set nomodified
197 imapclear
198endfunc
199
Bram Moolenaar75bf3d22019-03-26 22:46:05 +0100200func Test_map_cursor_ctrl_gU()
201 " <c-g>U<cursor> works only within a single line
202 nnoremap c<* *Ncgn<C-r>"<C-G>U<S-Left>
203 call setline(1, ['foo', 'foobar', '', 'foo'])
204 call cursor(1,2)
205 call feedkeys("c<*PREFIX\<esc>.", 'xt')
206 call assert_equal(['PREFIXfoo', 'foobar', '', 'PREFIXfoo'], getline(1,'$'))
207 " break undo manually
208 set ul=1000
209 exe ":norm! uu"
210 call assert_equal(['foo', 'foobar', '', 'foo'], getline(1,'$'))
211
212 " Test that it does not work if the cursor moves to the previous line
213 " 2 times <S-Left> move to the previous line
214 nnoremap c<* *Ncgn<C-r>"<C-G>U<S-Left><C-G>U<S-Left>
215 call setline(1, ['', ' foo', 'foobar', '', 'foo'])
216 call cursor(2,3)
217 call feedkeys("c<*PREFIX\<esc>.", 'xt')
218 call assert_equal(['PREFIXPREFIX', ' foo', 'foobar', '', 'foo'], getline(1,'$'))
219 nmapclear
220endfunc
221
222
Bram Moolenaar2d1a2482016-08-14 15:32:11 +0200223" This isn't actually testing a mapping, but similar use of CTRL-G U as above.
224func Test_break_undo()
Bram Moolenaar75bf3d22019-03-26 22:46:05 +0100225 set whichwrap=<,>,[,]
Bram Moolenaar2d1a2482016-08-14 15:32:11 +0200226 call feedkeys("G4o2k", "xt")
227 exe ":norm! iTest3: text with a (parenthesis here\<C-G>U\<Right>new line here\<esc>\<up>\<up>."
228 call assert_equal('new line here', getline(line('$') - 3))
229 call assert_equal('Test3: text with a (parenthesis here', getline(line('$') - 2))
230 call assert_equal('new line here', getline(line('$') - 1))
231 set nomodified
232endfunc
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +0200233
234func Test_map_meta_quotes()
235 imap <M-"> foo
236 call feedkeys("Go-\<M-\">-\<Esc>", "xt")
237 call assert_equal("-foo-", getline('$'))
238 set nomodified
239 iunmap <M-">
240endfunc
Bram Moolenaar878c2632017-04-01 15:15:52 +0200241
242func Test_abbr_after_line_join()
243 new
244 abbr foo bar
245 set backspace=indent,eol,start
246 exe "normal o\<BS>foo "
247 call assert_equal("bar ", getline(1))
248 bwipe!
249 unabbr foo
250 set backspace&
251endfunc
Bram Moolenaarb7637c42017-04-23 18:49:36 +0200252
253func Test_map_timeout()
Bram Moolenaar26d98212019-01-27 22:32:55 +0100254 if !has('timers')
255 return
256 endif
Bram Moolenaarb7637c42017-04-23 18:49:36 +0200257 nnoremap aaaa :let got_aaaa = 1<CR>
258 nnoremap bb :let got_bb = 1<CR>
259 nmap b aaa
260 new
261 func ExitInsert(timer)
262 let g:line = getline(1)
263 call feedkeys("\<Esc>", "t")
264 endfunc
265 set timeout timeoutlen=200
Bram Moolenaar26d98212019-01-27 22:32:55 +0100266 let timer = timer_start(300, 'ExitInsert')
Bram Moolenaarb7637c42017-04-23 18:49:36 +0200267 " After the 'b' Vim waits for another character to see if it matches 'bb'.
268 " When it times out it is expanded to "aaa", but there is no wait for
269 " "aaaa". Can't check that reliably though.
270 call feedkeys("b", "xt!")
271 call assert_equal("aa", g:line)
272 call assert_false(exists('got_aaa'))
273 call assert_false(exists('got_bb'))
274
275 bwipe!
276 nunmap aaaa
277 nunmap bb
278 nunmap b
279 set timeoutlen&
280 delfunc ExitInsert
Bram Moolenaar26d98212019-01-27 22:32:55 +0100281 call timer_stop(timer)
282endfunc
283
284func Test_map_timeout_with_timer_interrupt()
285 if !has('job') || !has('timers')
286 return
287 endif
288
289 " Confirm the timer invoked in exit_cb of the job doesn't disturb mapped key
290 " sequence.
291 new
292 let g:val = 0
293 nnoremap \12 :let g:val = 1<CR>
294 nnoremap \123 :let g:val = 2<CR>
295 set timeout timeoutlen=1000
296
297 func ExitCb(job, status)
Bram Moolenaar8d4ce562019-01-30 22:01:40 +0100298 let g:timer = timer_start(1, {-> feedkeys("3\<Esc>", 't')})
Bram Moolenaar26d98212019-01-27 22:32:55 +0100299 endfunc
300
301 call job_start([&shell, &shellcmdflag, 'echo'], {'exit_cb': 'ExitCb'})
302 call feedkeys('\12', 'xt!')
303 call assert_equal(2, g:val)
304
305 bwipe!
306 nunmap \12
307 nunmap \123
308 set timeoutlen&
309 call WaitFor({-> exists('g:timer')})
310 call timer_stop(g:timer)
311 unlet g:timer
312 unlet g:val
313 delfunc ExitCb
Bram Moolenaarb7637c42017-04-23 18:49:36 +0200314endfunc
Bram Moolenaarc3c3e692018-04-26 22:30:33 +0200315
316func Test_abbreviation_CR()
317 new
318 func Eatchar(pat)
319 let c = nr2char(getchar(0))
320 return (c =~ a:pat) ? '' : c
321 endfunc
322 iabbrev <buffer><silent> ~~7 <c-r>=repeat('~', 7)<CR><c-r>=Eatchar('\s')<cr>
323 call feedkeys("GA~~7 \<esc>", 'xt')
324 call assert_equal('~~~~~~~', getline('$'))
325 %d
326 call feedkeys("GA~~7\<cr>\<esc>", 'xt')
327 call assert_equal(['~~~~~~~', ''], getline(1,'$'))
328 delfunc Eatchar
329 bw!
330endfunc
Bram Moolenaar5e3423d2018-05-13 18:36:27 +0200331
332func Test_cabbr_visual_mode()
333 cabbr s su
334 call feedkeys(":s \<c-B>\"\<CR>", 'itx')
335 call assert_equal('"su ', getreg(':'))
336 call feedkeys(":'<,'>s \<c-B>\"\<CR>", 'itx')
337 let expected = '"'. "'<,'>su "
338 call assert_equal(expected, getreg(':'))
339 call feedkeys(": '<,'>s \<c-B>\"\<CR>", 'itx')
340 let expected = '" '. "'<,'>su "
341 call assert_equal(expected, getreg(':'))
342 call feedkeys(":'a,'bs \<c-B>\"\<CR>", 'itx')
343 let expected = '"'. "'a,'bsu "
344 call assert_equal(expected, getreg(':'))
345 cunabbr s
346endfunc
Bram Moolenaar5976f8f2018-12-27 23:44:44 +0100347
348func Test_motionforce_omap()
349 func GetCommand()
350 let g:m=mode(1)
351 let [g:lnum1, g:col1] = searchpos('-', 'Wb')
352 if g:lnum1 == 0
353 return "\<Esc>"
354 endif
355 let [g:lnum2, g:col2] = searchpos('-', 'W')
356 if g:lnum2 == 0
357 return "\<Esc>"
358 endif
359 return ":call Select()\<CR>"
360 endfunc
361 func Select()
362 call cursor([g:lnum1, g:col1])
363 exe "normal! 1 ". (strlen(g:m) == 2 ? 'v' : g:m[2])
364 call cursor([g:lnum2, g:col2])
365 execute "normal! \<BS>"
366 endfunc
367 new
368 onoremap <buffer><expr> i- GetCommand()
369 " 1) default omap mapping
370 %d_
371 call setline(1, ['aaa - bbb', 'x', 'ddd - eee'])
372 call cursor(2, 1)
373 norm di-
374 call assert_equal('no', g:m)
375 call assert_equal(['aaa -- eee'], getline(1, '$'))
376 " 2) forced characterwise operation
377 %d_
378 call setline(1, ['aaa - bbb', 'x', 'ddd - eee'])
379 call cursor(2, 1)
380 norm dvi-
381 call assert_equal('nov', g:m)
382 call assert_equal(['aaa -- eee'], getline(1, '$'))
383 " 3) forced linewise operation
384 %d_
385 call setline(1, ['aaa - bbb', 'x', 'ddd - eee'])
386 call cursor(2, 1)
387 norm dVi-
388 call assert_equal('noV', g:m)
389 call assert_equal([''], getline(1, '$'))
390 " 4) forced blockwise operation
391 %d_
392 call setline(1, ['aaa - bbb', 'x', 'ddd - eee'])
393 call cursor(2, 1)
394 exe "norm d\<C-V>i-"
395 call assert_equal("no\<C-V>", g:m)
396 call assert_equal(['aaabbb', 'x', 'dddeee'], getline(1, '$'))
397 bwipe!
398 delfunc Select
399 delfunc GetCommand
400endfunc
Bram Moolenaar7d491c42019-06-25 06:28:02 +0200401
402func Test_error_in_map_expr()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +0200403 " Unlike CheckRunVimInTerminal this does work in a win32 console
404 CheckFeature terminal
405 if has('win32') && has('gui_running')
Bram Moolenaar7d491c42019-06-25 06:28:02 +0200406 throw 'Skipped: cannot run Vim in a terminal window'
407 endif
408
409 let lines =<< trim [CODE]
410 func Func()
411 " fail to create list
412 let x = [
413 endfunc
414 nmap <expr> ! Func()
415 set updatetime=50
416 [CODE]
417 call writefile(lines, 'Xtest.vim')
418
Bram Moolenaar0d702022019-07-04 14:20:41 +0200419 let buf = term_start(GetVimCommandCleanTerm() .. ' -S Xtest.vim', {'term_rows': 8})
Bram Moolenaar7d491c42019-06-25 06:28:02 +0200420 let job = term_getjob(buf)
421 call WaitForAssert({-> assert_notequal('', term_getline(buf, 8))})
422
423 " GC must not run during map-expr processing, which can make Vim crash.
424 call term_sendkeys(buf, '!')
425 call term_wait(buf, 100)
426 call term_sendkeys(buf, "\<CR>")
427 call term_wait(buf, 100)
428 call assert_equal('run', job_status(job))
429
430 call term_sendkeys(buf, ":qall!\<CR>")
431 call WaitFor({-> job_status(job) ==# 'dead'})
432 if has('unix')
433 call assert_equal('', job_info(job).termsig)
434 endif
435
436 call delete('Xtest.vim')
437 exe buf .. 'bwipe!'
438endfunc