xref: /vim-8.2.3635/src/testdir/test_mapping.vim (revision 01a6c216)
1" Tests for mappings and abbreviations
2
3source shared.vim
4
5func Test_abbreviation()
6  " abbreviation with 0x80 should work
7  inoreab чкпр   vim
8  call feedkeys("Goчкпр \<Esc>", "xt")
9  call assert_equal('vim ', getline('$'))
10  iunab чкпр
11  set nomodified
12endfunc
13
14func Test_map_ctrl_c_insert()
15  " mapping of ctrl-c in Insert mode
16  set cpo-=< cpo-=k
17  inoremap <c-c> <ctrl-c>
18  cnoremap <c-c> dummy
19  cunmap <c-c>
20  call feedkeys("GoTEST2: CTRL-C |\<C-C>A|\<Esc>", "xt")
21  call assert_equal('TEST2: CTRL-C |<ctrl-c>A|', getline('$'))
22  unmap! <c-c>
23  set nomodified
24endfunc
25
26func Test_map_ctrl_c_visual()
27  " mapping of ctrl-c in Visual mode
28  vnoremap <c-c> :<C-u>$put ='vmap works'
29  call feedkeys("GV\<C-C>\<CR>", "xt")
30  call assert_equal('vmap works', getline('$'))
31  vunmap <c-c>
32  set nomodified
33endfunc
34
35func Test_map_langmap()
36  if !has('langmap')
37    return
38  endif
39
40  " check langmap applies in normal mode
41  set langmap=+- nolangremap
42  new
43  call setline(1, ['a', 'b', 'c'])
44  2
45  call assert_equal('b', getline('.'))
46  call feedkeys("+", "xt")
47  call assert_equal('a', getline('.'))
48
49  " check no remapping
50  map x +
51  2
52  call feedkeys("x", "xt")
53  call assert_equal('c', getline('.'))
54
55  " check with remapping
56  set langremap
57  2
58  call feedkeys("x", "xt")
59  call assert_equal('a', getline('.'))
60
61  unmap x
62  bwipe!
63
64  " 'langnoremap' follows 'langremap' and vise versa
65  set langremap
66  set langnoremap
67  call assert_equal(0, &langremap)
68  set langremap
69  call assert_equal(0, &langnoremap)
70  set nolangremap
71  call assert_equal(1, &langnoremap)
72
73  " check default values
74  set langnoremap&
75  call assert_equal(0, &langnoremap)
76  call assert_equal(1, &langremap)
77  set langremap&
78  call assert_equal(0, &langnoremap)
79  call assert_equal(1, &langremap)
80
81  " langmap should not apply in insert mode, 'langremap' doesn't matter
82  set langmap=+{ nolangremap
83  call feedkeys("Go+\<Esc>", "xt")
84  call assert_equal('+', getline('$'))
85  set langmap=+{ langremap
86  call feedkeys("Go+\<Esc>", "xt")
87  call assert_equal('+', getline('$'))
88
89  " langmap used for register name in insert mode.
90  call setreg('a', 'aaaa')
91  call setreg('b', 'bbbb')
92  call setreg('c', 'cccc')
93  set langmap=ab langremap
94  call feedkeys("Go\<C-R>a\<Esc>", "xt")
95  call assert_equal('bbbb', getline('$'))
96  call feedkeys("Go\<C-R>\<C-R>a\<Esc>", "xt")
97  call assert_equal('bbbb', getline('$'))
98  " mapping does not apply
99  imap c a
100  call feedkeys("Go\<C-R>c\<Esc>", "xt")
101  call assert_equal('cccc', getline('$'))
102  imap a c
103  call feedkeys("Go\<C-R>a\<Esc>", "xt")
104  call assert_equal('bbbb', getline('$'))
105
106  " langmap should not apply in Command-line mode
107  set langmap=+{ nolangremap
108  call feedkeys(":call append(line('$'), '+')\<CR>", "xt")
109  call assert_equal('+', getline('$'))
110
111  iunmap a
112  iunmap c
113  set nomodified
114endfunc
115
116func Test_map_feedkeys()
117  " issue #212 (feedkeys insert mapping at current position)
118  nnoremap . :call feedkeys(".", "in")<cr>
119  call setline('$', ['a b c d', 'a b c d'])
120  $-1
121  call feedkeys("0qqdw.ifoo\<Esc>qj0@q\<Esc>", "xt")
122  call assert_equal(['fooc d', 'fooc d'], getline(line('$') - 1, line('$')))
123  nunmap .
124  set nomodified
125endfunc
126
127func Test_map_cursor()
128  " <c-g>U<cursor> works only within a single line
129  imapclear
130  imap ( ()<c-g>U<left>
131  call feedkeys("G2o\<Esc>ki\<CR>Test1: text with a (here some more text\<Esc>k.", "xt")
132  call assert_equal('Test1: text with a (here some more text)', getline(line('$') - 2))
133  call assert_equal('Test1: text with a (here some more text)', getline(line('$') - 1))
134
135  " test undo
136  call feedkeys("G2o\<Esc>ki\<CR>Test2: text wit a (here some more text [und undo]\<C-G>u\<Esc>k.u", "xt")
137  call assert_equal('', getline(line('$') - 2))
138  call assert_equal('Test2: text wit a (here some more text [und undo])', getline(line('$') - 1))
139  set nomodified
140  imapclear
141endfunc
142
143" This isn't actually testing a mapping, but similar use of CTRL-G U as above.
144func Test_break_undo()
145  :set whichwrap=<,>,[,]
146  call feedkeys("G4o2k", "xt")
147  exe ":norm! iTest3: text with a (parenthesis here\<C-G>U\<Right>new line here\<esc>\<up>\<up>."
148  call assert_equal('new line here', getline(line('$') - 3))
149  call assert_equal('Test3: text with a (parenthesis here', getline(line('$') - 2))
150  call assert_equal('new line here', getline(line('$') - 1))
151  set nomodified
152endfunc
153
154func Test_map_meta_quotes()
155  imap <M-"> foo
156  call feedkeys("Go-\<M-\">-\<Esc>", "xt")
157  call assert_equal("-foo-", getline('$'))
158  set nomodified
159  iunmap <M-">
160endfunc
161
162func Test_abbr_after_line_join()
163  new
164  abbr foo bar
165  set backspace=indent,eol,start
166  exe "normal o\<BS>foo "
167  call assert_equal("bar ", getline(1))
168  bwipe!
169  unabbr foo
170  set backspace&
171endfunc
172
173func Test_map_timeout()
174  if !has('timers')
175    return
176  endif
177  nnoremap aaaa :let got_aaaa = 1<CR>
178  nnoremap bb :let got_bb = 1<CR>
179  nmap b aaa
180  new
181  func ExitInsert(timer)
182    let g:line = getline(1)
183    call feedkeys("\<Esc>", "t")
184  endfunc
185  set timeout timeoutlen=200
186  let timer = timer_start(300, 'ExitInsert')
187  " After the 'b' Vim waits for another character to see if it matches 'bb'.
188  " When it times out it is expanded to "aaa", but there is no wait for
189  " "aaaa".  Can't check that reliably though.
190  call feedkeys("b", "xt!")
191  call assert_equal("aa", g:line)
192  call assert_false(exists('got_aaa'))
193  call assert_false(exists('got_bb'))
194
195  bwipe!
196  nunmap aaaa
197  nunmap bb
198  nunmap b
199  set timeoutlen&
200  delfunc ExitInsert
201  call timer_stop(timer)
202endfunc
203
204func Test_map_timeout_with_timer_interrupt()
205  if !has('job') || !has('timers')
206    return
207  endif
208
209  " Confirm the timer invoked in exit_cb of the job doesn't disturb mapped key
210  " sequence.
211  new
212  let g:val = 0
213  nnoremap \12 :let g:val = 1<CR>
214  nnoremap \123 :let g:val = 2<CR>
215  set timeout timeoutlen=1000
216
217  func ExitCb(job, status)
218    let g:timer = timer_start(1, {-> feedkeys("3\<Esc>", 't')})
219  endfunc
220
221  call job_start([&shell, &shellcmdflag, 'echo'], {'exit_cb': 'ExitCb'})
222  call feedkeys('\12', 'xt!')
223  call assert_equal(2, g:val)
224
225  bwipe!
226  nunmap \12
227  nunmap \123
228  set timeoutlen&
229  call WaitFor({-> exists('g:timer')})
230  call timer_stop(g:timer)
231  unlet g:timer
232  unlet g:val
233  delfunc ExitCb
234endfunc
235
236func Test_abbreviation_CR()
237  new
238  func Eatchar(pat)
239    let c = nr2char(getchar(0))
240    return (c =~ a:pat) ? '' : c
241  endfunc
242  iabbrev <buffer><silent> ~~7 <c-r>=repeat('~', 7)<CR><c-r>=Eatchar('\s')<cr>
243  call feedkeys("GA~~7 \<esc>", 'xt')
244  call assert_equal('~~~~~~~', getline('$'))
245  %d
246  call feedkeys("GA~~7\<cr>\<esc>", 'xt')
247  call assert_equal(['~~~~~~~', ''], getline(1,'$'))
248  delfunc Eatchar
249  bw!
250endfunc
251
252func Test_cabbr_visual_mode()
253  cabbr s su
254  call feedkeys(":s \<c-B>\"\<CR>", 'itx')
255  call assert_equal('"su ', getreg(':'))
256  call feedkeys(":'<,'>s \<c-B>\"\<CR>", 'itx')
257  let expected = '"'. "'<,'>su "
258  call assert_equal(expected, getreg(':'))
259  call feedkeys(":  '<,'>s \<c-B>\"\<CR>", 'itx')
260  let expected = '"  '. "'<,'>su "
261  call assert_equal(expected, getreg(':'))
262  call feedkeys(":'a,'bs \<c-B>\"\<CR>", 'itx')
263  let expected = '"'. "'a,'bsu "
264  call assert_equal(expected, getreg(':'))
265  cunabbr s
266endfunc
267
268func Test_motionforce_omap()
269  func GetCommand()
270    let g:m=mode(1)
271    let [g:lnum1, g:col1] = searchpos('-', 'Wb')
272    if g:lnum1 == 0
273        return "\<Esc>"
274    endif
275    let [g:lnum2, g:col2] = searchpos('-', 'W')
276    if g:lnum2 == 0
277        return "\<Esc>"
278    endif
279    return ":call Select()\<CR>"
280  endfunc
281  func Select()
282    call cursor([g:lnum1, g:col1])
283    exe "normal! 1 ". (strlen(g:m) == 2 ? 'v' : g:m[2])
284    call cursor([g:lnum2, g:col2])
285    execute "normal! \<BS>"
286  endfunc
287  new
288  onoremap <buffer><expr> i- GetCommand()
289  " 1) default omap mapping
290  %d_
291  call setline(1, ['aaa - bbb', 'x', 'ddd - eee'])
292  call cursor(2, 1)
293  norm di-
294  call assert_equal('no', g:m)
295  call assert_equal(['aaa -- eee'], getline(1, '$'))
296  " 2) forced characterwise operation
297  %d_
298  call setline(1, ['aaa - bbb', 'x', 'ddd - eee'])
299  call cursor(2, 1)
300  norm dvi-
301  call assert_equal('nov', g:m)
302  call assert_equal(['aaa -- eee'], getline(1, '$'))
303  " 3) forced linewise operation
304  %d_
305  call setline(1, ['aaa - bbb', 'x', 'ddd - eee'])
306  call cursor(2, 1)
307  norm dVi-
308  call assert_equal('noV', g:m)
309  call assert_equal([''], getline(1, '$'))
310  " 4) forced blockwise operation
311  %d_
312  call setline(1, ['aaa - bbb', 'x', 'ddd - eee'])
313  call cursor(2, 1)
314  exe "norm d\<C-V>i-"
315  call assert_equal("no\<C-V>", g:m)
316  call assert_equal(['aaabbb', 'x', 'dddeee'], getline(1, '$'))
317  bwipe!
318  delfunc Select
319  delfunc GetCommand
320endfunc
321