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