Ernie Rael | 0966120 | 2022-04-25 14:40:44 +0100 | [diff] [blame] | 1 | " Tests for maparg(), mapcheck(), mapset(), maplist() |
Bram Moolenaar | 292eff0 | 2017-07-11 21:46:28 +0200 | [diff] [blame] | 2 | " Also test utf8 map with a 0x80 byte. |
Bram Moolenaar | 292eff0 | 2017-07-11 21:46:28 +0200 | [diff] [blame] | 3 | |
Bram Moolenaar | 4c9243f | 2020-05-22 13:10:44 +0200 | [diff] [blame] | 4 | func s:SID() |
Bram Moolenaar | 292eff0 | 2017-07-11 21:46:28 +0200 | [diff] [blame] | 5 | return str2nr(matchstr(expand('<sfile>'), '<SNR>\zs\d\+\ze_SID$')) |
Bram Moolenaar | 4c9243f | 2020-05-22 13:10:44 +0200 | [diff] [blame] | 6 | endfunc |
Bram Moolenaar | 292eff0 | 2017-07-11 21:46:28 +0200 | [diff] [blame] | 7 | |
Bram Moolenaar | c94c146 | 2020-05-22 20:01:06 +0200 | [diff] [blame] | 8 | func Test_maparg() |
Bram Moolenaar | 292eff0 | 2017-07-11 21:46:28 +0200 | [diff] [blame] | 9 | new |
| 10 | set cpo-=< |
| 11 | set encoding=utf8 |
| 12 | " Test maparg() with a string result |
Bram Moolenaar | f29c1c6 | 2018-09-10 21:05:02 +0200 | [diff] [blame] | 13 | let sid = s:SID() |
| 14 | let lnum = expand('<sflnum>') |
Bram Moolenaar | 292eff0 | 2017-07-11 21:46:28 +0200 | [diff] [blame] | 15 | map foo<C-V> is<F4>foo |
| 16 | vnoremap <script> <buffer> <expr> <silent> bar isbar |
Bram Moolenaar | 292eff0 | 2017-07-11 21:46:28 +0200 | [diff] [blame] | 17 | call assert_equal("is<F4>foo", maparg('foo<C-V>')) |
Bram Moolenaar | 2da0f0c | 2020-04-01 19:22:12 +0200 | [diff] [blame] | 18 | call assert_equal({'silent': 0, 'noremap': 0, 'script': 0, 'lhs': 'foo<C-V>', |
Bram Moolenaar | 9c65253 | 2020-05-24 13:10:18 +0200 | [diff] [blame] | 19 | \ 'lhsraw': "foo\x80\xfc\x04V", 'lhsrawalt': "foo\x16", |
Bram Moolenaar | a9528b3 | 2022-01-18 20:51:35 +0000 | [diff] [blame] | 20 | \ 'mode': ' ', 'nowait': 0, 'expr': 0, 'sid': sid, 'scriptversion': 1, |
| 21 | \ 'lnum': lnum + 1, |
Ernie Rael | d8f5f76 | 2022-05-10 17:50:39 +0100 | [diff] [blame] | 22 | \ 'rhs': 'is<F4>foo', 'buffer': 0, 'abbr': 0, 'mode_bits': 0x47}, |
Bram Moolenaar | f29c1c6 | 2018-09-10 21:05:02 +0200 | [diff] [blame] | 23 | \ maparg('foo<C-V>', '', 0, 1)) |
Bram Moolenaar | 9c65253 | 2020-05-24 13:10:18 +0200 | [diff] [blame] | 24 | call assert_equal({'silent': 1, 'noremap': 1, 'script': 1, 'lhs': 'bar', |
| 25 | \ 'lhsraw': 'bar', 'mode': 'v', |
Bram Moolenaar | a9528b3 | 2022-01-18 20:51:35 +0000 | [diff] [blame] | 26 | \ 'nowait': 0, 'expr': 1, 'sid': sid, 'scriptversion': 1, |
| 27 | \ 'lnum': lnum + 2, |
Ernie Rael | d8f5f76 | 2022-05-10 17:50:39 +0100 | [diff] [blame] | 28 | \ 'rhs': 'isbar', 'buffer': 1, 'abbr': 0, 'mode_bits': 0x42}, |
Bram Moolenaar | a144983 | 2019-09-01 20:16:52 +0200 | [diff] [blame] | 29 | \ 'bar'->maparg('', 0, 1)) |
Bram Moolenaar | f29c1c6 | 2018-09-10 21:05:02 +0200 | [diff] [blame] | 30 | let lnum = expand('<sflnum>') |
Bram Moolenaar | 292eff0 | 2017-07-11 21:46:28 +0200 | [diff] [blame] | 31 | map <buffer> <nowait> foo bar |
Bram Moolenaar | 9c65253 | 2020-05-24 13:10:18 +0200 | [diff] [blame] | 32 | call assert_equal({'silent': 0, 'noremap': 0, 'script': 0, 'lhs': 'foo', |
| 33 | \ 'lhsraw': 'foo', 'mode': ' ', |
Bram Moolenaar | a9528b3 | 2022-01-18 20:51:35 +0000 | [diff] [blame] | 34 | \ 'nowait': 1, 'expr': 0, 'sid': sid, 'scriptversion': 1, |
| 35 | \ 'lnum': lnum + 1, 'rhs': 'bar', |
Ernie Rael | d8f5f76 | 2022-05-10 17:50:39 +0100 | [diff] [blame] | 36 | \ 'buffer': 1, 'abbr': 0, 'mode_bits': 0x47}, |
Bram Moolenaar | 292eff0 | 2017-07-11 21:46:28 +0200 | [diff] [blame] | 37 | \ maparg('foo', '', 0, 1)) |
Bram Moolenaar | 14371ed | 2019-07-27 21:05:21 +0200 | [diff] [blame] | 38 | let lnum = expand('<sflnum>') |
| 39 | tmap baz foo |
Bram Moolenaar | 9c65253 | 2020-05-24 13:10:18 +0200 | [diff] [blame] | 40 | call assert_equal({'silent': 0, 'noremap': 0, 'script': 0, 'lhs': 'baz', |
| 41 | \ 'lhsraw': 'baz', 'mode': 't', |
Bram Moolenaar | a9528b3 | 2022-01-18 20:51:35 +0000 | [diff] [blame] | 42 | \ 'nowait': 0, 'expr': 0, 'sid': sid, 'scriptversion': 1, |
| 43 | \ 'lnum': lnum + 1, 'rhs': 'foo', |
Ernie Rael | d8f5f76 | 2022-05-10 17:50:39 +0100 | [diff] [blame] | 44 | \ 'buffer': 0, 'abbr': 0, 'mode_bits': 0x80}, |
Bram Moolenaar | 14371ed | 2019-07-27 21:05:21 +0200 | [diff] [blame] | 45 | \ maparg('baz', 't', 0, 1)) |
Ernie Rael | 51d04d1 | 2022-05-04 15:40:22 +0100 | [diff] [blame] | 46 | let lnum = expand('<sflnum>') |
| 47 | iab A B |
| 48 | call assert_equal({'silent': 0, 'noremap': 0, 'script': 0, 'lhs': 'A', |
| 49 | \ 'lhsraw': 'A', 'mode': 'i', |
| 50 | \ 'nowait': 0, 'expr': 0, 'sid': sid, 'scriptversion': 1, |
| 51 | \ 'lnum': lnum + 1, 'rhs': 'B', |
Ernie Rael | d8f5f76 | 2022-05-10 17:50:39 +0100 | [diff] [blame] | 52 | \ 'buffer': 0, 'abbr': 1, 'mode_bits': 0x0010}, |
Ernie Rael | 51d04d1 | 2022-05-04 15:40:22 +0100 | [diff] [blame] | 53 | \ maparg('A', 'i', 1, 1)) |
| 54 | iuna A |
Bram Moolenaar | 292eff0 | 2017-07-11 21:46:28 +0200 | [diff] [blame] | 55 | |
| 56 | map abc x<char-114>x |
Bram Moolenaar | f88a5bc | 2018-05-21 13:28:44 +0200 | [diff] [blame] | 57 | call assert_equal("xrx", maparg('abc')) |
Bram Moolenaar | 292eff0 | 2017-07-11 21:46:28 +0200 | [diff] [blame] | 58 | map abc y<S-char-114>y |
Bram Moolenaar | f88a5bc | 2018-05-21 13:28:44 +0200 | [diff] [blame] | 59 | call assert_equal("yRy", maparg('abc')) |
| 60 | |
zeertzjq | 0519ce0 | 2022-05-09 12:16:19 +0100 | [diff] [blame] | 61 | " character with K_SPECIAL byte |
| 62 | nmap abc … |
| 63 | call assert_equal('…', maparg('abc')) |
| 64 | |
| 65 | " modified character with K_SPECIAL byte |
| 66 | nmap abc <M-…> |
| 67 | call assert_equal('<M-…>', maparg('abc')) |
| 68 | |
| 69 | " illegal bytes |
| 70 | let str = ":\x7f:\x80:\x90:\xd0:" |
| 71 | exe 'nmap abc ' .. str |
| 72 | call assert_equal(str, maparg('abc')) |
| 73 | unlet str |
| 74 | |
Bram Moolenaar | 8ba6bb7 | 2020-01-20 20:41:42 +0100 | [diff] [blame] | 75 | omap { w |
| 76 | let d = maparg('{', 'o', 0, 1) |
| 77 | call assert_equal(['{', 'w', 'o'], [d.lhs, d.rhs, d.mode]) |
| 78 | ounmap { |
| 79 | |
Bram Moolenaar | c2a60ae | 2020-01-23 16:19:54 +0100 | [diff] [blame] | 80 | lmap { w |
| 81 | let d = maparg('{', 'l', 0, 1) |
| 82 | call assert_equal(['{', 'w', 'l'], [d.lhs, d.rhs, d.mode]) |
| 83 | lunmap { |
| 84 | |
| 85 | nmap { w |
| 86 | let d = maparg('{', 'n', 0, 1) |
| 87 | call assert_equal(['{', 'w', 'n'], [d.lhs, d.rhs, d.mode]) |
| 88 | nunmap { |
| 89 | |
| 90 | xmap { w |
| 91 | let d = maparg('{', 'x', 0, 1) |
| 92 | call assert_equal(['{', 'w', 'x'], [d.lhs, d.rhs, d.mode]) |
| 93 | xunmap { |
| 94 | |
| 95 | smap { w |
| 96 | let d = maparg('{', 's', 0, 1) |
| 97 | call assert_equal(['{', 'w', 's'], [d.lhs, d.rhs, d.mode]) |
| 98 | sunmap { |
| 99 | |
zeertzjq | 2c8a7eb | 2022-04-26 21:36:21 +0100 | [diff] [blame] | 100 | map <C-I> foo |
| 101 | unmap <Tab> |
| 102 | " This used to cause a segfault |
| 103 | call maparg('<C-I>', '', 0, 1) |
| 104 | unmap <C-I> |
| 105 | |
Bram Moolenaar | f88a5bc | 2018-05-21 13:28:44 +0200 | [diff] [blame] | 106 | map abc <Nop> |
| 107 | call assert_equal("<Nop>", maparg('abc')) |
| 108 | unmap abc |
Bram Moolenaar | c2a60ae | 2020-01-23 16:19:54 +0100 | [diff] [blame] | 109 | |
| 110 | call feedkeys(":abbr esc \<C-V>\<C-V>\<C-V>\<C-V>\<C-V>\<Esc>\<CR>", "xt") |
| 111 | let d = maparg('esc', 'i', 1, 1) |
| 112 | call assert_equal(['esc', "\<C-V>\<C-V>\<Esc>", '!'], [d.lhs, d.rhs, d.mode]) |
| 113 | abclear |
zeertzjq | 2c8a7eb | 2022-04-26 21:36:21 +0100 | [diff] [blame] | 114 | unlet d |
Bram Moolenaar | 4c9243f | 2020-05-22 13:10:44 +0200 | [diff] [blame] | 115 | endfunc |
Bram Moolenaar | 292eff0 | 2017-07-11 21:46:28 +0200 | [diff] [blame] | 116 | |
Bram Moolenaar | 4a6d1b6 | 2020-08-08 17:55:49 +0200 | [diff] [blame] | 117 | def Test_vim9_maparg() |
| 118 | nmap { w |
Bram Moolenaar | 7a9cbca | 2020-09-27 22:47:05 +0200 | [diff] [blame] | 119 | var one: string = maparg('{') |
Bram Moolenaar | 4a6d1b6 | 2020-08-08 17:55:49 +0200 | [diff] [blame] | 120 | assert_equal('w', one) |
Bram Moolenaar | 7a9cbca | 2020-09-27 22:47:05 +0200 | [diff] [blame] | 121 | var two: string = maparg('{', 'n') |
Bram Moolenaar | 4a6d1b6 | 2020-08-08 17:55:49 +0200 | [diff] [blame] | 122 | assert_equal('w', two) |
Bram Moolenaar | 7a9cbca | 2020-09-27 22:47:05 +0200 | [diff] [blame] | 123 | var three: string = maparg('{', 'n', 0) |
Bram Moolenaar | 4a6d1b6 | 2020-08-08 17:55:49 +0200 | [diff] [blame] | 124 | assert_equal('w', three) |
Bram Moolenaar | 7a9cbca | 2020-09-27 22:47:05 +0200 | [diff] [blame] | 125 | var four: dict<any> = maparg('{', 'n', 0, 1) |
Bram Moolenaar | d2c6170 | 2020-09-06 15:58:36 +0200 | [diff] [blame] | 126 | assert_equal(['{', 'w', 'n'], [four.lhs, four.rhs, four.mode]) |
Bram Moolenaar | 4a6d1b6 | 2020-08-08 17:55:49 +0200 | [diff] [blame] | 127 | nunmap { |
| 128 | enddef |
| 129 | |
Bram Moolenaar | a144983 | 2019-09-01 20:16:52 +0200 | [diff] [blame] | 130 | func Test_mapcheck() |
| 131 | call assert_equal('', mapcheck('a')) |
| 132 | call assert_equal('', mapcheck('abc')) |
| 133 | call assert_equal('', mapcheck('ax')) |
| 134 | call assert_equal('', mapcheck('b')) |
| 135 | |
| 136 | map a something |
| 137 | call assert_equal('something', mapcheck('a')) |
| 138 | call assert_equal('something', mapcheck('a', 'n')) |
| 139 | call assert_equal('', mapcheck('a', 'c')) |
| 140 | call assert_equal('', mapcheck('a', 'i')) |
| 141 | call assert_equal('something', 'abc'->mapcheck()) |
| 142 | call assert_equal('something', 'ax'->mapcheck()) |
| 143 | call assert_equal('', mapcheck('b')) |
| 144 | unmap a |
| 145 | |
| 146 | map ab foobar |
| 147 | call assert_equal('foobar', mapcheck('a')) |
| 148 | call assert_equal('foobar', mapcheck('abc')) |
| 149 | call assert_equal('', mapcheck('ax')) |
| 150 | call assert_equal('', mapcheck('b')) |
| 151 | unmap ab |
| 152 | |
| 153 | map abc barfoo |
| 154 | call assert_equal('barfoo', mapcheck('a')) |
| 155 | call assert_equal('barfoo', mapcheck('a', 'n', 0)) |
| 156 | call assert_equal('', mapcheck('a', 'n', 1)) |
| 157 | call assert_equal('barfoo', mapcheck('abc')) |
| 158 | call assert_equal('', mapcheck('ax')) |
| 159 | call assert_equal('', mapcheck('b')) |
| 160 | unmap abc |
| 161 | |
| 162 | abbr ab abbrev |
| 163 | call assert_equal('abbrev', mapcheck('a', 'i', 1)) |
| 164 | call assert_equal('', mapcheck('a', 'n', 1)) |
| 165 | call assert_equal('', mapcheck('a', 'i', 0)) |
| 166 | unabbr ab |
| 167 | endfunc |
| 168 | |
Bram Moolenaar | 4c9243f | 2020-05-22 13:10:44 +0200 | [diff] [blame] | 169 | func Test_range_map() |
Bram Moolenaar | 292eff0 | 2017-07-11 21:46:28 +0200 | [diff] [blame] | 170 | new |
| 171 | " Outside of the range, minimum |
| 172 | inoremap <Char-0x1040> a |
| 173 | execute "normal a\u1040\<Esc>" |
| 174 | " Inside of the range, minimum |
| 175 | inoremap <Char-0x103f> b |
| 176 | execute "normal a\u103f\<Esc>" |
| 177 | " Inside of the range, maximum |
| 178 | inoremap <Char-0xf03f> c |
| 179 | execute "normal a\uf03f\<Esc>" |
| 180 | " Outside of the range, maximum |
| 181 | inoremap <Char-0xf040> d |
| 182 | execute "normal a\uf040\<Esc>" |
| 183 | call assert_equal("abcd", getline(1)) |
Bram Moolenaar | 4c9243f | 2020-05-22 13:10:44 +0200 | [diff] [blame] | 184 | endfunc |
| 185 | |
zeertzjq | 92a3d20 | 2022-08-31 16:40:17 +0100 | [diff] [blame] | 186 | func One_mapset_test(keys, rhs) |
| 187 | exe 'nnoremap ' .. a:keys .. ' ' .. a:rhs |
Bram Moolenaar | 4c9243f | 2020-05-22 13:10:44 +0200 | [diff] [blame] | 188 | let orig = maparg(a:keys, 'n', 0, 1) |
| 189 | call assert_equal(a:keys, orig.lhs) |
zeertzjq | 92a3d20 | 2022-08-31 16:40:17 +0100 | [diff] [blame] | 190 | call assert_equal(a:rhs, orig.rhs) |
Bram Moolenaar | 4c9243f | 2020-05-22 13:10:44 +0200 | [diff] [blame] | 191 | call assert_equal('n', orig.mode) |
| 192 | |
| 193 | exe 'nunmap ' .. a:keys |
| 194 | let d = maparg(a:keys, 'n', 0, 1) |
| 195 | call assert_equal({}, d) |
| 196 | |
| 197 | call mapset('n', 0, orig) |
| 198 | let d = maparg(a:keys, 'n', 0, 1) |
| 199 | call assert_equal(a:keys, d.lhs) |
zeertzjq | 92a3d20 | 2022-08-31 16:40:17 +0100 | [diff] [blame] | 200 | call assert_equal(a:rhs, d.rhs) |
Bram Moolenaar | 4c9243f | 2020-05-22 13:10:44 +0200 | [diff] [blame] | 201 | call assert_equal('n', d.mode) |
| 202 | |
| 203 | exe 'nunmap ' .. a:keys |
| 204 | endfunc |
| 205 | |
| 206 | func Test_mapset() |
zeertzjq | 92a3d20 | 2022-08-31 16:40:17 +0100 | [diff] [blame] | 207 | call One_mapset_test('K', 'original<CR>') |
| 208 | call One_mapset_test('<F3>', 'original<CR>') |
| 209 | call One_mapset_test('<F3>', '<lt>Nop>') |
Bram Moolenaar | c94c146 | 2020-05-22 20:01:06 +0200 | [diff] [blame] | 210 | |
| 211 | " Check <> key conversion |
| 212 | new |
| 213 | inoremap K one<Left>x |
| 214 | call feedkeys("iK\<Esc>", 'xt') |
| 215 | call assert_equal('onxe', getline(1)) |
| 216 | |
| 217 | let orig = maparg('K', 'i', 0, 1) |
| 218 | call assert_equal('K', orig.lhs) |
| 219 | call assert_equal('one<Left>x', orig.rhs) |
| 220 | call assert_equal('i', orig.mode) |
| 221 | |
| 222 | iunmap K |
| 223 | let d = maparg('K', 'i', 0, 1) |
| 224 | call assert_equal({}, d) |
| 225 | |
| 226 | call mapset('i', 0, orig) |
| 227 | call feedkeys("SK\<Esc>", 'xt') |
| 228 | call assert_equal('onxe', getline(1)) |
| 229 | |
| 230 | iunmap K |
| 231 | |
zeertzjq | 92a3d20 | 2022-08-31 16:40:17 +0100 | [diff] [blame] | 232 | " Test that <Nop> is restored properly |
| 233 | inoremap K <Nop> |
| 234 | call feedkeys("SK\<Esc>", 'xt') |
| 235 | call assert_equal('', getline(1)) |
| 236 | |
| 237 | let orig = maparg('K', 'i', 0, 1) |
| 238 | call assert_equal('K', orig.lhs) |
| 239 | call assert_equal('<Nop>', orig.rhs) |
| 240 | call assert_equal('i', orig.mode) |
| 241 | |
| 242 | inoremap K foo |
| 243 | call feedkeys("SK\<Esc>", 'xt') |
| 244 | call assert_equal('foo', getline(1)) |
| 245 | |
| 246 | call mapset('i', 0, orig) |
| 247 | call feedkeys("SK\<Esc>", 'xt') |
| 248 | call assert_equal('', getline(1)) |
| 249 | |
| 250 | iunmap K |
| 251 | |
Bram Moolenaar | c94c146 | 2020-05-22 20:01:06 +0200 | [diff] [blame] | 252 | " Test literal <CR> using a backslash |
| 253 | let cpo_save = &cpo |
| 254 | set cpo-=B |
| 255 | inoremap K one\<CR>two |
| 256 | call feedkeys("SK\<Esc>", 'xt') |
| 257 | call assert_equal('one<CR>two', getline(1)) |
| 258 | |
| 259 | let orig = maparg('K', 'i', 0, 1) |
| 260 | call assert_equal('K', orig.lhs) |
| 261 | call assert_equal('one\<CR>two', orig.rhs) |
| 262 | call assert_equal('i', orig.mode) |
| 263 | |
| 264 | iunmap K |
| 265 | let d = maparg('K', 'i', 0, 1) |
| 266 | call assert_equal({}, d) |
| 267 | |
| 268 | call mapset('i', 0, orig) |
| 269 | call feedkeys("SK\<Esc>", 'xt') |
| 270 | call assert_equal('one<CR>two', getline(1)) |
| 271 | |
| 272 | iunmap K |
Bram Moolenaar | c94c146 | 2020-05-22 20:01:06 +0200 | [diff] [blame] | 273 | |
| 274 | " Test literal <CR> using CTRL-V |
| 275 | inoremap K one<CR>two |
| 276 | call feedkeys("SK\<Esc>", 'xt') |
| 277 | call assert_equal('one<CR>two', getline(1)) |
| 278 | |
| 279 | let orig = maparg('K', 'i', 0, 1) |
| 280 | call assert_equal('K', orig.lhs) |
| 281 | call assert_equal("one\x16<CR>two", orig.rhs) |
| 282 | call assert_equal('i', orig.mode) |
| 283 | |
| 284 | iunmap K |
| 285 | let d = maparg('K', 'i', 0, 1) |
| 286 | call assert_equal({}, d) |
| 287 | |
| 288 | call mapset('i', 0, orig) |
| 289 | call feedkeys("SK\<Esc>", 'xt') |
| 290 | call assert_equal('one<CR>two', getline(1)) |
| 291 | |
| 292 | iunmap K |
| 293 | let &cpo = cpo_save |
Bram Moolenaar | c94c146 | 2020-05-22 20:01:06 +0200 | [diff] [blame] | 294 | bwipe! |
Bram Moolenaar | 1b91298 | 2020-09-29 21:45:41 +0200 | [diff] [blame] | 295 | |
| 296 | call assert_fails('call mapset([], v:false, {})', 'E730:') |
Yegappan Lakshmanan | 04c4c57 | 2022-08-30 19:48:24 +0100 | [diff] [blame] | 297 | call assert_fails('call mapset("i", 0, "")', 'E1206:') |
Bram Moolenaar | a1070ea | 2021-02-20 19:21:36 +0100 | [diff] [blame] | 298 | call assert_fails('call mapset("i", 0, {})', 'E460:') |
Bram Moolenaar | 4c9243f | 2020-05-22 13:10:44 +0200 | [diff] [blame] | 299 | endfunc |
Bram Moolenaar | 8ba6bb7 | 2020-01-20 20:41:42 +0100 | [diff] [blame] | 300 | |
Ernie Rael | 51d04d1 | 2022-05-04 15:40:22 +0100 | [diff] [blame] | 301 | def Test_mapset_arg1_dir() |
| 302 | # This test is mostly about get_map_mode_string. |
| 303 | # Once the code gets past that, it's common with the 3 arg mapset. |
| 304 | |
| 305 | # GetModes() return list of modes for 'XZ' lhs using maplist. |
| 306 | # There is one list item per mapping |
| 307 | def GetModes(abbr: bool = false): list<string> |
| 308 | return maplist(abbr)->filter((_, m) => m.lhs == 'XZ') |
| 309 | ->mapnew((_, m) => m.mode) |
| 310 | enddef |
| 311 | |
| 312 | const unmap_cmds = [ 'unmap', 'unmap!', 'tunmap', 'lunmap' ] |
| 313 | def UnmapAll(lhs: string) |
| 314 | for cmd in unmap_cmds |
| 315 | try | execute(cmd .. ' ' .. lhs) | catch /E31/ | endtry |
| 316 | endfor |
| 317 | enddef |
| 318 | |
| 319 | var tmap: dict<any> |
| 320 | |
| 321 | # some mapset(mode, abbr, dict) tests using get_map_mode_str |
| 322 | map XZ x |
| 323 | tmap = maplist()->filter((_, m) => m.lhs == 'XZ')[0]->copy() |
| 324 | # this splits the mapping into 2 mappings |
| 325 | mapset('ox', false, tmap) |
| 326 | assert_equal(2, len(GetModes())) |
| 327 | mapset('o', false, tmap) |
| 328 | assert_equal(3, len(GetModes())) |
| 329 | # test that '' acts like ' ', and that the 3 mappings become 1 |
| 330 | mapset('', false, tmap) |
| 331 | assert_equal([' '], GetModes()) |
| 332 | # dict's mode/abbr are ignored |
| 333 | UnmapAll('XZ') |
| 334 | tmap.mode = '!' |
| 335 | tmap.abbr = true |
| 336 | mapset('o', false, tmap) |
| 337 | assert_equal(['o'], GetModes()) |
| 338 | |
| 339 | # test the 3 arg version handles bad mode string, dict not used |
| 340 | assert_fails("mapset('vi', false, {})", 'E1276:') |
| 341 | |
| 342 | |
| 343 | # get the abbreviations out of the way |
| 344 | abbreviate XZ ZX |
| 345 | tmap = maplist(true)->filter((_, m) => m.lhs == 'XZ')[0]->copy() |
| 346 | |
| 347 | abclear |
| 348 | # 'ic' is the default ab command, shows up as '!' |
| 349 | tmap.mode = 'ic' |
| 350 | mapset(tmap) |
| 351 | assert_equal(['!'], GetModes(true)) |
| 352 | |
| 353 | abclear |
| 354 | tmap.mode = 'i' |
| 355 | mapset(tmap) |
| 356 | assert_equal(['i'], GetModes(true)) |
| 357 | |
| 358 | abclear |
| 359 | tmap.mode = 'c' |
| 360 | mapset(tmap) |
| 361 | assert_equal(['c'], GetModes(true)) |
| 362 | |
| 363 | abclear |
| 364 | tmap.mode = '!' |
| 365 | mapset(tmap) |
| 366 | assert_equal(['!'], GetModes(true)) |
| 367 | |
| 368 | assert_fails("mapset({mode: ' !', abbr: 1})", 'E1276:') |
| 369 | assert_fails("mapset({mode: 'cl', abbr: 1})", 'E1276:') |
| 370 | assert_fails("mapset({mode: 'in', abbr: 1})", 'E1276:') |
| 371 | |
| 372 | # the map commands |
| 373 | map XZ x |
| 374 | tmap = maplist()->filter((_, m) => m.lhs == 'XZ')[0]->copy() |
| 375 | |
| 376 | # try the combos |
| 377 | UnmapAll('XZ') |
| 378 | # 'nxso' is ' ', the unadorned :map |
| 379 | tmap.mode = 'nxso' |
| 380 | mapset(tmap) |
| 381 | assert_equal([' '], GetModes()) |
| 382 | |
| 383 | UnmapAll('XZ') |
| 384 | # 'ic' is '!' |
| 385 | tmap.mode = 'ic' |
| 386 | mapset(tmap) |
| 387 | assert_equal(['!'], GetModes()) |
| 388 | |
| 389 | UnmapAll('XZ') |
| 390 | # 'xs' is really 'v' |
| 391 | tmap.mode = 'xs' |
| 392 | mapset(tmap) |
| 393 | assert_equal(['v'], GetModes()) |
| 394 | |
| 395 | # try the individual modes |
| 396 | UnmapAll('XZ') |
| 397 | tmap.mode = 'n' |
| 398 | mapset(tmap) |
| 399 | assert_equal(['n'], GetModes()) |
| 400 | |
| 401 | UnmapAll('XZ') |
| 402 | tmap.mode = 'x' |
| 403 | mapset(tmap) |
| 404 | assert_equal(['x'], GetModes()) |
| 405 | |
| 406 | UnmapAll('XZ') |
| 407 | tmap.mode = 's' |
| 408 | mapset(tmap) |
| 409 | assert_equal(['s'], GetModes()) |
| 410 | |
| 411 | UnmapAll('XZ') |
| 412 | tmap.mode = 'o' |
| 413 | mapset(tmap) |
| 414 | assert_equal(['o'], GetModes()) |
| 415 | |
| 416 | UnmapAll('XZ') |
| 417 | tmap.mode = 'i' |
| 418 | mapset(tmap) |
| 419 | assert_equal(['i'], GetModes()) |
| 420 | |
| 421 | UnmapAll('XZ') |
| 422 | tmap.mode = 'c' |
| 423 | mapset(tmap) |
| 424 | assert_equal(['c'], GetModes()) |
| 425 | |
| 426 | UnmapAll('XZ') |
| 427 | tmap.mode = 't' |
| 428 | mapset(tmap) |
| 429 | assert_equal(['t'], GetModes()) |
| 430 | |
| 431 | UnmapAll('XZ') |
| 432 | tmap.mode = 'l' |
| 433 | mapset(tmap) |
| 434 | assert_equal(['l'], GetModes()) |
| 435 | |
| 436 | UnmapAll('XZ') |
| 437 | |
| 438 | # get errors for modes that can't be in one mapping |
| 439 | assert_fails("mapset({mode: 'nxsoi', abbr: 0})", 'E1276:') |
| 440 | assert_fails("mapset({mode: ' !', abbr: 0})", 'E1276:') |
| 441 | assert_fails("mapset({mode: 'ix', abbr: 0})", 'E1276:') |
| 442 | assert_fails("mapset({mode: 'tl', abbr: 0})", 'E1276:') |
| 443 | assert_fails("mapset({mode: ' l', abbr: 0})", 'E1276:') |
| 444 | assert_fails("mapset({mode: ' t', abbr: 0})", 'E1276:') |
| 445 | enddef |
| 446 | |
Bram Moolenaar | 9c65253 | 2020-05-24 13:10:18 +0200 | [diff] [blame] | 447 | func Check_ctrlb_map(d, check_alt) |
| 448 | call assert_equal('<C-B>', a:d.lhs) |
| 449 | if a:check_alt |
| 450 | call assert_equal("\x80\xfc\x04B", a:d.lhsraw) |
| 451 | call assert_equal("\x02", a:d.lhsrawalt) |
| 452 | else |
| 453 | call assert_equal("\x02", a:d.lhsraw) |
| 454 | endif |
| 455 | endfunc |
| 456 | |
Bram Moolenaar | 7ba1e4d | 2021-04-24 13:12:38 +0200 | [diff] [blame] | 457 | func Test_map_local() |
| 458 | nmap a global |
| 459 | nmap <buffer>a local |
| 460 | |
| 461 | let prev_map_list = split(execute('nmap a'), "\n") |
| 462 | call assert_match('n\s*a\s*@local', prev_map_list[0]) |
| 463 | call assert_match('n\s*a\s*global', prev_map_list[1]) |
| 464 | |
| 465 | let mapping = maparg('a', 'n', 0, 1) |
| 466 | call assert_equal(1, mapping.buffer) |
| 467 | let mapping.rhs = 'new_local' |
| 468 | call mapset('n', 0, mapping) |
| 469 | |
| 470 | " Check that the global mapping is left untouched. |
| 471 | let map_list = split(execute('nmap a'), "\n") |
| 472 | call assert_match('n\s*a\s*@new_local', map_list[0]) |
| 473 | call assert_match('n\s*a\s*global', map_list[1]) |
| 474 | |
| 475 | nunmap a |
| 476 | endfunc |
| 477 | |
Bram Moolenaar | 9c65253 | 2020-05-24 13:10:18 +0200 | [diff] [blame] | 478 | func Test_map_restore() |
| 479 | " Test restoring map with alternate keycode |
| 480 | nmap <C-B> back |
| 481 | let d = maparg('<C-B>', 'n', 0, 1) |
| 482 | call Check_ctrlb_map(d, 1) |
| 483 | let dsimp = maparg("\x02", 'n', 0, 1) |
| 484 | call Check_ctrlb_map(dsimp, 0) |
| 485 | nunmap <C-B> |
| 486 | call mapset('n', 0, d) |
| 487 | let d = maparg('<C-B>', 'n', 0, 1) |
| 488 | call Check_ctrlb_map(d, 1) |
| 489 | let dsimp = maparg("\x02", 'n', 0, 1) |
| 490 | call Check_ctrlb_map(dsimp, 0) |
| 491 | |
| 492 | nunmap <C-B> |
| 493 | |
| 494 | endfunc |
| 495 | |
Ernie Rael | 0966120 | 2022-04-25 14:40:44 +0100 | [diff] [blame] | 496 | def Test_maplist() |
Ernie Rael | 659c240 | 2022-04-24 18:40:28 +0100 | [diff] [blame] | 497 | new |
Ernie Rael | 0966120 | 2022-04-25 14:40:44 +0100 | [diff] [blame] | 498 | def ClearMappingsAbbreviations() |
Ernie Rael | 659c240 | 2022-04-24 18:40:28 +0100 | [diff] [blame] | 499 | mapclear | nmapclear | vmapclear | xmapclear | smapclear | omapclear |
| 500 | mapclear! | imapclear | lmapclear | cmapclear | tmapclear |
| 501 | mapclear <buffer> | nmapclear <buffer> | vmapclear <buffer> |
| 502 | xmapclear <buffer> | smapclear <buffer> | omapclear <buffer> |
| 503 | mapclear! <buffer> | imapclear <buffer> | lmapclear <buffer> |
| 504 | cmapclear <buffer> | tmapclear <buffer> |
Ernie Rael | 0966120 | 2022-04-25 14:40:44 +0100 | [diff] [blame] | 505 | abclear | abclear <buffer> |
Ernie Rael | 659c240 | 2022-04-24 18:40:28 +0100 | [diff] [blame] | 506 | enddef |
| 507 | |
| 508 | def AddMaps(new: list<string>, accum: list<string>) |
| 509 | if len(new) > 0 && new[0] != "No mapping found" |
| 510 | accum->extend(new) |
| 511 | endif |
| 512 | enddef |
| 513 | |
Ernie Rael | 0966120 | 2022-04-25 14:40:44 +0100 | [diff] [blame] | 514 | ClearMappingsAbbreviations() |
| 515 | assert_equal(0, len(maplist())) |
| 516 | assert_equal(0, len(maplist(true))) |
Ernie Rael | 659c240 | 2022-04-24 18:40:28 +0100 | [diff] [blame] | 517 | |
| 518 | # Set up some mappings. |
| 519 | map dup bar |
| 520 | map <buffer> dup bufbar |
| 521 | map foo<C-V> is<F4>foo |
| 522 | vnoremap <script> <buffer> <expr> <silent> bar isbar |
| 523 | tmap baz foo |
| 524 | omap h w |
| 525 | lmap i w |
| 526 | nmap j w |
| 527 | xmap k w |
| 528 | smap l w |
| 529 | map abc <Nop> |
| 530 | nmap <M-j> x |
| 531 | nmap <M-Space> y |
Ernie Rael | 0966120 | 2022-04-25 14:40:44 +0100 | [diff] [blame] | 532 | # And abbreviations |
| 533 | abbreviate xy he |
| 534 | abbreviate xx she |
| 535 | abbreviate <buffer> x they |
Ernie Rael | 659c240 | 2022-04-24 18:40:28 +0100 | [diff] [blame] | 536 | |
| 537 | # Get a list of the mappings with the ':map' commands. |
Ernie Rael | 0966120 | 2022-04-25 14:40:44 +0100 | [diff] [blame] | 538 | # Check maplist() return a list of the same size. |
| 539 | assert_equal(13, len(maplist())) |
| 540 | assert_equal(3, len(maplist(true))) |
| 541 | assert_equal(13, len(maplist(false))) |
Ernie Rael | 659c240 | 2022-04-24 18:40:28 +0100 | [diff] [blame] | 542 | |
| 543 | # collect all the current maps using :map commands |
| 544 | var maps_command: list<string> |
| 545 | AddMaps(split(execute('map'), '\n'), maps_command) |
| 546 | AddMaps(split(execute('map!'), '\n'), maps_command) |
| 547 | AddMaps(split(execute('tmap'), '\n'), maps_command) |
| 548 | AddMaps(split(execute('lmap'), '\n'), maps_command) |
| 549 | |
Ernie Rael | 0966120 | 2022-04-25 14:40:44 +0100 | [diff] [blame] | 550 | # Use maplist to get all the maps |
| 551 | var maps_maplist = maplist() |
| 552 | assert_equal(len(maps_command), len(maps_maplist)) |
Ernie Rael | 659c240 | 2022-04-24 18:40:28 +0100 | [diff] [blame] | 553 | |
| 554 | # make sure all the mode-lhs are unique, no duplicates |
| 555 | var map_set: dict<number> |
Ernie Rael | 0966120 | 2022-04-25 14:40:44 +0100 | [diff] [blame] | 556 | for d in maps_maplist |
Ernie Rael | 659c240 | 2022-04-24 18:40:28 +0100 | [diff] [blame] | 557 | map_set[d.mode .. "-" .. d.lhs .. "-" .. d.buffer] = 0 |
| 558 | endfor |
Ernie Rael | 0966120 | 2022-04-25 14:40:44 +0100 | [diff] [blame] | 559 | assert_equal(len(maps_maplist), len(map_set)) |
Ernie Rael | 659c240 | 2022-04-24 18:40:28 +0100 | [diff] [blame] | 560 | |
Ernie Rael | 0966120 | 2022-04-25 14:40:44 +0100 | [diff] [blame] | 561 | # For everything returned by maplist, should be the same as from maparg. |
dundargoc | c57b5bc | 2022-11-02 13:30:51 +0000 | [diff] [blame^] | 562 | # Except for "map dup", because maparg returns the <buffer> version |
Ernie Rael | 0966120 | 2022-04-25 14:40:44 +0100 | [diff] [blame] | 563 | for d in maps_maplist |
Ernie Rael | 659c240 | 2022-04-24 18:40:28 +0100 | [diff] [blame] | 564 | if d.lhs == 'dup' && d.buffer == 0 |
| 565 | continue |
| 566 | endif |
| 567 | var d_maparg = maparg(d.lhs, d.mode, false, true) |
| 568 | assert_equal(d_maparg, d) |
| 569 | endfor |
| 570 | |
Ernie Rael | 0966120 | 2022-04-25 14:40:44 +0100 | [diff] [blame] | 571 | # Check abbr matches maparg |
| 572 | for d in maplist(true) |
| 573 | # Note, d.mode is '!', but can't use that with maparg |
| 574 | var d_maparg = maparg(d.lhs, 'i', true, true) |
| 575 | assert_equal(d_maparg, d) |
| 576 | endfor |
| 577 | |
| 578 | ClearMappingsAbbreviations() |
| 579 | assert_equal(0, len(maplist())) |
| 580 | assert_equal(0, len(maplist(true))) |
Ernie Rael | 659c240 | 2022-04-24 18:40:28 +0100 | [diff] [blame] | 581 | enddef |
| 582 | |
| 583 | |
Bram Moolenaar | 8ba6bb7 | 2020-01-20 20:41:42 +0100 | [diff] [blame] | 584 | " vim: shiftwidth=2 sts=2 expandtab |