blob: ea921d6386a1ef90bfa4d2eaef457cb8696a9ce2 [file] [log] [blame]
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02001" Tests for maparg(), mapcheck() and mapset().
Bram Moolenaar292eff02017-07-11 21:46:28 +02002" Also test utf8 map with a 0x80 byte.
Bram Moolenaara1449832019-09-01 20:16:52 +02003" Also test mapcheck()
Bram Moolenaar292eff02017-07-11 21:46:28 +02004
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02005func s:SID()
Bram Moolenaar292eff02017-07-11 21:46:28 +02006 return str2nr(matchstr(expand('<sfile>'), '<SNR>\zs\d\+\ze_SID$'))
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02007endfunc
Bram Moolenaar292eff02017-07-11 21:46:28 +02008
Bram Moolenaarc94c1462020-05-22 20:01:06 +02009func Test_maparg()
Bram Moolenaar292eff02017-07-11 21:46:28 +020010 new
11 set cpo-=<
12 set encoding=utf8
13 " Test maparg() with a string result
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020014 let sid = s:SID()
15 let lnum = expand('<sflnum>')
Bram Moolenaar292eff02017-07-11 21:46:28 +020016 map foo<C-V> is<F4>foo
17 vnoremap <script> <buffer> <expr> <silent> bar isbar
Bram Moolenaar292eff02017-07-11 21:46:28 +020018 call assert_equal("is<F4>foo", maparg('foo<C-V>'))
Bram Moolenaar2da0f0c2020-04-01 19:22:12 +020019 call assert_equal({'silent': 0, 'noremap': 0, 'script': 0, 'lhs': 'foo<C-V>',
Bram Moolenaar9c652532020-05-24 13:10:18 +020020 \ 'lhsraw': "foo\x80\xfc\x04V", 'lhsrawalt': "foo\x16",
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020021 \ 'mode': ' ', 'nowait': 0, 'expr': 0, 'sid': sid, 'lnum': lnum + 1,
Bram Moolenaar9c652532020-05-24 13:10:18 +020022 \ 'rhs': 'is<F4>foo', 'buffer': 0},
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020023 \ maparg('foo<C-V>', '', 0, 1))
Bram Moolenaar9c652532020-05-24 13:10:18 +020024 call assert_equal({'silent': 1, 'noremap': 1, 'script': 1, 'lhs': 'bar',
25 \ 'lhsraw': 'bar', 'mode': 'v',
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020026 \ 'nowait': 0, 'expr': 1, 'sid': sid, 'lnum': lnum + 2,
Bram Moolenaar9c652532020-05-24 13:10:18 +020027 \ 'rhs': 'isbar', 'buffer': 1},
Bram Moolenaara1449832019-09-01 20:16:52 +020028 \ 'bar'->maparg('', 0, 1))
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020029 let lnum = expand('<sflnum>')
Bram Moolenaar292eff02017-07-11 21:46:28 +020030 map <buffer> <nowait> foo bar
Bram Moolenaar9c652532020-05-24 13:10:18 +020031 call assert_equal({'silent': 0, 'noremap': 0, 'script': 0, 'lhs': 'foo',
32 \ 'lhsraw': 'foo', 'mode': ' ',
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020033 \ 'nowait': 1, 'expr': 0, 'sid': sid, 'lnum': lnum + 1, 'rhs': 'bar',
Bram Moolenaar9c652532020-05-24 13:10:18 +020034 \ 'buffer': 1},
Bram Moolenaar292eff02017-07-11 21:46:28 +020035 \ maparg('foo', '', 0, 1))
Bram Moolenaar14371ed2019-07-27 21:05:21 +020036 let lnum = expand('<sflnum>')
37 tmap baz foo
Bram Moolenaar9c652532020-05-24 13:10:18 +020038 call assert_equal({'silent': 0, 'noremap': 0, 'script': 0, 'lhs': 'baz',
39 \ 'lhsraw': 'baz', 'mode': 't',
Bram Moolenaar14371ed2019-07-27 21:05:21 +020040 \ 'nowait': 0, 'expr': 0, 'sid': sid, 'lnum': lnum + 1, 'rhs': 'foo',
Bram Moolenaar9c652532020-05-24 13:10:18 +020041 \ 'buffer': 0},
Bram Moolenaar14371ed2019-07-27 21:05:21 +020042 \ maparg('baz', 't', 0, 1))
Bram Moolenaar292eff02017-07-11 21:46:28 +020043
44 map abc x<char-114>x
Bram Moolenaarf88a5bc2018-05-21 13:28:44 +020045 call assert_equal("xrx", maparg('abc'))
Bram Moolenaar292eff02017-07-11 21:46:28 +020046 map abc y<S-char-114>y
Bram Moolenaarf88a5bc2018-05-21 13:28:44 +020047 call assert_equal("yRy", maparg('abc'))
48
Bram Moolenaar8ba6bb72020-01-20 20:41:42 +010049 omap { w
50 let d = maparg('{', 'o', 0, 1)
51 call assert_equal(['{', 'w', 'o'], [d.lhs, d.rhs, d.mode])
52 ounmap {
53
Bram Moolenaarc2a60ae2020-01-23 16:19:54 +010054 lmap { w
55 let d = maparg('{', 'l', 0, 1)
56 call assert_equal(['{', 'w', 'l'], [d.lhs, d.rhs, d.mode])
57 lunmap {
58
59 nmap { w
60 let d = maparg('{', 'n', 0, 1)
61 call assert_equal(['{', 'w', 'n'], [d.lhs, d.rhs, d.mode])
62 nunmap {
63
64 xmap { w
65 let d = maparg('{', 'x', 0, 1)
66 call assert_equal(['{', 'w', 'x'], [d.lhs, d.rhs, d.mode])
67 xunmap {
68
69 smap { w
70 let d = maparg('{', 's', 0, 1)
71 call assert_equal(['{', 'w', 's'], [d.lhs, d.rhs, d.mode])
72 sunmap {
73
Bram Moolenaarf88a5bc2018-05-21 13:28:44 +020074 map abc <Nop>
75 call assert_equal("<Nop>", maparg('abc'))
76 unmap abc
Bram Moolenaarc2a60ae2020-01-23 16:19:54 +010077
78 call feedkeys(":abbr esc \<C-V>\<C-V>\<C-V>\<C-V>\<C-V>\<Esc>\<CR>", "xt")
79 let d = maparg('esc', 'i', 1, 1)
80 call assert_equal(['esc', "\<C-V>\<C-V>\<Esc>", '!'], [d.lhs, d.rhs, d.mode])
81 abclear
Bram Moolenaar4c9243f2020-05-22 13:10:44 +020082endfunc
Bram Moolenaar292eff02017-07-11 21:46:28 +020083
Bram Moolenaar4a6d1b62020-08-08 17:55:49 +020084def Test_vim9_maparg()
85 nmap { w
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +020086 var one: string = maparg('{')
Bram Moolenaar4a6d1b62020-08-08 17:55:49 +020087 assert_equal('w', one)
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +020088 var two: string = maparg('{', 'n')
Bram Moolenaar4a6d1b62020-08-08 17:55:49 +020089 assert_equal('w', two)
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +020090 var three: string = maparg('{', 'n', 0)
Bram Moolenaar4a6d1b62020-08-08 17:55:49 +020091 assert_equal('w', three)
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +020092 var four: dict<any> = maparg('{', 'n', 0, 1)
Bram Moolenaard2c61702020-09-06 15:58:36 +020093 assert_equal(['{', 'w', 'n'], [four.lhs, four.rhs, four.mode])
Bram Moolenaar4a6d1b62020-08-08 17:55:49 +020094 nunmap {
95enddef
96
Bram Moolenaara1449832019-09-01 20:16:52 +020097func Test_mapcheck()
98 call assert_equal('', mapcheck('a'))
99 call assert_equal('', mapcheck('abc'))
100 call assert_equal('', mapcheck('ax'))
101 call assert_equal('', mapcheck('b'))
102
103 map a something
104 call assert_equal('something', mapcheck('a'))
105 call assert_equal('something', mapcheck('a', 'n'))
106 call assert_equal('', mapcheck('a', 'c'))
107 call assert_equal('', mapcheck('a', 'i'))
108 call assert_equal('something', 'abc'->mapcheck())
109 call assert_equal('something', 'ax'->mapcheck())
110 call assert_equal('', mapcheck('b'))
111 unmap a
112
113 map ab foobar
114 call assert_equal('foobar', mapcheck('a'))
115 call assert_equal('foobar', mapcheck('abc'))
116 call assert_equal('', mapcheck('ax'))
117 call assert_equal('', mapcheck('b'))
118 unmap ab
119
120 map abc barfoo
121 call assert_equal('barfoo', mapcheck('a'))
122 call assert_equal('barfoo', mapcheck('a', 'n', 0))
123 call assert_equal('', mapcheck('a', 'n', 1))
124 call assert_equal('barfoo', mapcheck('abc'))
125 call assert_equal('', mapcheck('ax'))
126 call assert_equal('', mapcheck('b'))
127 unmap abc
128
129 abbr ab abbrev
130 call assert_equal('abbrev', mapcheck('a', 'i', 1))
131 call assert_equal('', mapcheck('a', 'n', 1))
132 call assert_equal('', mapcheck('a', 'i', 0))
133 unabbr ab
134endfunc
135
Bram Moolenaar4c9243f2020-05-22 13:10:44 +0200136func Test_range_map()
Bram Moolenaar292eff02017-07-11 21:46:28 +0200137 new
138 " Outside of the range, minimum
139 inoremap <Char-0x1040> a
140 execute "normal a\u1040\<Esc>"
141 " Inside of the range, minimum
142 inoremap <Char-0x103f> b
143 execute "normal a\u103f\<Esc>"
144 " Inside of the range, maximum
145 inoremap <Char-0xf03f> c
146 execute "normal a\uf03f\<Esc>"
147 " Outside of the range, maximum
148 inoremap <Char-0xf040> d
149 execute "normal a\uf040\<Esc>"
150 call assert_equal("abcd", getline(1))
Bram Moolenaar4c9243f2020-05-22 13:10:44 +0200151endfunc
152
153func One_mapset_test(keys)
154 exe 'nnoremap ' .. a:keys .. ' original<CR>'
155 let orig = maparg(a:keys, 'n', 0, 1)
156 call assert_equal(a:keys, orig.lhs)
157 call assert_equal('original<CR>', orig.rhs)
158 call assert_equal('n', orig.mode)
159
160 exe 'nunmap ' .. a:keys
161 let d = maparg(a:keys, 'n', 0, 1)
162 call assert_equal({}, d)
163
164 call mapset('n', 0, orig)
165 let d = maparg(a:keys, 'n', 0, 1)
166 call assert_equal(a:keys, d.lhs)
167 call assert_equal('original<CR>', d.rhs)
168 call assert_equal('n', d.mode)
169
170 exe 'nunmap ' .. a:keys
171endfunc
172
173func Test_mapset()
174 call One_mapset_test('K')
175 call One_mapset_test('<F3>')
Bram Moolenaarc94c1462020-05-22 20:01:06 +0200176
177 " Check <> key conversion
178 new
179 inoremap K one<Left>x
180 call feedkeys("iK\<Esc>", 'xt')
181 call assert_equal('onxe', getline(1))
182
183 let orig = maparg('K', 'i', 0, 1)
184 call assert_equal('K', orig.lhs)
185 call assert_equal('one<Left>x', orig.rhs)
186 call assert_equal('i', orig.mode)
187
188 iunmap K
189 let d = maparg('K', 'i', 0, 1)
190 call assert_equal({}, d)
191
192 call mapset('i', 0, orig)
193 call feedkeys("SK\<Esc>", 'xt')
194 call assert_equal('onxe', getline(1))
195
196 iunmap K
197
198 " Test literal <CR> using a backslash
199 let cpo_save = &cpo
200 set cpo-=B
201 inoremap K one\<CR>two
202 call feedkeys("SK\<Esc>", 'xt')
203 call assert_equal('one<CR>two', getline(1))
204
205 let orig = maparg('K', 'i', 0, 1)
206 call assert_equal('K', orig.lhs)
207 call assert_equal('one\<CR>two', orig.rhs)
208 call assert_equal('i', orig.mode)
209
210 iunmap K
211 let d = maparg('K', 'i', 0, 1)
212 call assert_equal({}, d)
213
214 call mapset('i', 0, orig)
215 call feedkeys("SK\<Esc>", 'xt')
216 call assert_equal('one<CR>two', getline(1))
217
218 iunmap K
Bram Moolenaarc94c1462020-05-22 20:01:06 +0200219
220 " Test literal <CR> using CTRL-V
221 inoremap K one<CR>two
222 call feedkeys("SK\<Esc>", 'xt')
223 call assert_equal('one<CR>two', getline(1))
224
225 let orig = maparg('K', 'i', 0, 1)
226 call assert_equal('K', orig.lhs)
227 call assert_equal("one\x16<CR>two", orig.rhs)
228 call assert_equal('i', orig.mode)
229
230 iunmap K
231 let d = maparg('K', 'i', 0, 1)
232 call assert_equal({}, d)
233
234 call mapset('i', 0, orig)
235 call feedkeys("SK\<Esc>", 'xt')
236 call assert_equal('one<CR>two', getline(1))
237
238 iunmap K
239 let &cpo = cpo_save
Bram Moolenaarc94c1462020-05-22 20:01:06 +0200240 bwipe!
Bram Moolenaar4c9243f2020-05-22 13:10:44 +0200241endfunc
Bram Moolenaar8ba6bb72020-01-20 20:41:42 +0100242
Bram Moolenaar9c652532020-05-24 13:10:18 +0200243func Check_ctrlb_map(d, check_alt)
244 call assert_equal('<C-B>', a:d.lhs)
245 if a:check_alt
246 call assert_equal("\x80\xfc\x04B", a:d.lhsraw)
247 call assert_equal("\x02", a:d.lhsrawalt)
248 else
249 call assert_equal("\x02", a:d.lhsraw)
250 endif
251endfunc
252
253func Test_map_restore()
254 " Test restoring map with alternate keycode
255 nmap <C-B> back
256 let d = maparg('<C-B>', 'n', 0, 1)
257 call Check_ctrlb_map(d, 1)
258 let dsimp = maparg("\x02", 'n', 0, 1)
259 call Check_ctrlb_map(dsimp, 0)
260 nunmap <C-B>
261 call mapset('n', 0, d)
262 let d = maparg('<C-B>', 'n', 0, 1)
263 call Check_ctrlb_map(d, 1)
264 let dsimp = maparg("\x02", 'n', 0, 1)
265 call Check_ctrlb_map(dsimp, 0)
266
267 nunmap <C-B>
268
269endfunc
270
Bram Moolenaar8ba6bb72020-01-20 20:41:42 +0100271" vim: shiftwidth=2 sts=2 expandtab