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 | |
| 186 | func One_mapset_test(keys) |
| 187 | exe 'nnoremap ' .. a:keys .. ' original<CR>' |
| 188 | let orig = maparg(a:keys, 'n', 0, 1) |
| 189 | call assert_equal(a:keys, orig.lhs) |
| 190 | call assert_equal('original<CR>', orig.rhs) |
| 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) |
| 200 | call assert_equal('original<CR>', d.rhs) |
| 201 | call assert_equal('n', d.mode) |
| 202 | |
| 203 | exe 'nunmap ' .. a:keys |
| 204 | endfunc |
| 205 | |
| 206 | func Test_mapset() |
| 207 | call One_mapset_test('K') |
| 208 | call One_mapset_test('<F3>') |
Bram Moolenaar | c94c146 | 2020-05-22 20:01:06 +0200 | [diff] [blame] | 209 | |
| 210 | " Check <> key conversion |
| 211 | new |
| 212 | inoremap K one<Left>x |
| 213 | call feedkeys("iK\<Esc>", 'xt') |
| 214 | call assert_equal('onxe', getline(1)) |
| 215 | |
| 216 | let orig = maparg('K', 'i', 0, 1) |
| 217 | call assert_equal('K', orig.lhs) |
| 218 | call assert_equal('one<Left>x', orig.rhs) |
| 219 | call assert_equal('i', orig.mode) |
| 220 | |
| 221 | iunmap K |
| 222 | let d = maparg('K', 'i', 0, 1) |
| 223 | call assert_equal({}, d) |
| 224 | |
| 225 | call mapset('i', 0, orig) |
| 226 | call feedkeys("SK\<Esc>", 'xt') |
| 227 | call assert_equal('onxe', getline(1)) |
| 228 | |
| 229 | iunmap K |
| 230 | |
| 231 | " Test literal <CR> using a backslash |
| 232 | let cpo_save = &cpo |
| 233 | set cpo-=B |
| 234 | inoremap K one\<CR>two |
| 235 | call feedkeys("SK\<Esc>", 'xt') |
| 236 | call assert_equal('one<CR>two', getline(1)) |
| 237 | |
| 238 | let orig = maparg('K', 'i', 0, 1) |
| 239 | call assert_equal('K', orig.lhs) |
| 240 | call assert_equal('one\<CR>two', orig.rhs) |
| 241 | call assert_equal('i', orig.mode) |
| 242 | |
| 243 | iunmap K |
| 244 | let d = maparg('K', 'i', 0, 1) |
| 245 | call assert_equal({}, d) |
| 246 | |
| 247 | call mapset('i', 0, orig) |
| 248 | call feedkeys("SK\<Esc>", 'xt') |
| 249 | call assert_equal('one<CR>two', getline(1)) |
| 250 | |
| 251 | iunmap K |
Bram Moolenaar | c94c146 | 2020-05-22 20:01:06 +0200 | [diff] [blame] | 252 | |
| 253 | " Test literal <CR> using CTRL-V |
| 254 | inoremap K one<CR>two |
| 255 | call feedkeys("SK\<Esc>", 'xt') |
| 256 | call assert_equal('one<CR>two', getline(1)) |
| 257 | |
| 258 | let orig = maparg('K', 'i', 0, 1) |
| 259 | call assert_equal('K', orig.lhs) |
| 260 | call assert_equal("one\x16<CR>two", orig.rhs) |
| 261 | call assert_equal('i', orig.mode) |
| 262 | |
| 263 | iunmap K |
| 264 | let d = maparg('K', 'i', 0, 1) |
| 265 | call assert_equal({}, d) |
| 266 | |
| 267 | call mapset('i', 0, orig) |
| 268 | call feedkeys("SK\<Esc>", 'xt') |
| 269 | call assert_equal('one<CR>two', getline(1)) |
| 270 | |
| 271 | iunmap K |
| 272 | let &cpo = cpo_save |
Bram Moolenaar | c94c146 | 2020-05-22 20:01:06 +0200 | [diff] [blame] | 273 | bwipe! |
Bram Moolenaar | 1b91298 | 2020-09-29 21:45:41 +0200 | [diff] [blame] | 274 | |
| 275 | call assert_fails('call mapset([], v:false, {})', 'E730:') |
Ernie Rael | 51d04d1 | 2022-05-04 15:40:22 +0100 | [diff] [blame] | 276 | call assert_fails('call mapset("i", 0, "")', 'E715:') |
Bram Moolenaar | a1070ea | 2021-02-20 19:21:36 +0100 | [diff] [blame] | 277 | call assert_fails('call mapset("i", 0, {})', 'E460:') |
Bram Moolenaar | 4c9243f | 2020-05-22 13:10:44 +0200 | [diff] [blame] | 278 | endfunc |
Bram Moolenaar | 8ba6bb7 | 2020-01-20 20:41:42 +0100 | [diff] [blame] | 279 | |
Ernie Rael | 51d04d1 | 2022-05-04 15:40:22 +0100 | [diff] [blame] | 280 | def Test_mapset_arg1_dir() |
| 281 | # This test is mostly about get_map_mode_string. |
| 282 | # Once the code gets past that, it's common with the 3 arg mapset. |
| 283 | |
| 284 | # GetModes() return list of modes for 'XZ' lhs using maplist. |
| 285 | # There is one list item per mapping |
| 286 | def GetModes(abbr: bool = false): list<string> |
| 287 | return maplist(abbr)->filter((_, m) => m.lhs == 'XZ') |
| 288 | ->mapnew((_, m) => m.mode) |
| 289 | enddef |
| 290 | |
| 291 | const unmap_cmds = [ 'unmap', 'unmap!', 'tunmap', 'lunmap' ] |
| 292 | def UnmapAll(lhs: string) |
| 293 | for cmd in unmap_cmds |
| 294 | try | execute(cmd .. ' ' .. lhs) | catch /E31/ | endtry |
| 295 | endfor |
| 296 | enddef |
| 297 | |
| 298 | var tmap: dict<any> |
| 299 | |
| 300 | # some mapset(mode, abbr, dict) tests using get_map_mode_str |
| 301 | map XZ x |
| 302 | tmap = maplist()->filter((_, m) => m.lhs == 'XZ')[0]->copy() |
| 303 | # this splits the mapping into 2 mappings |
| 304 | mapset('ox', false, tmap) |
| 305 | assert_equal(2, len(GetModes())) |
| 306 | mapset('o', false, tmap) |
| 307 | assert_equal(3, len(GetModes())) |
| 308 | # test that '' acts like ' ', and that the 3 mappings become 1 |
| 309 | mapset('', false, tmap) |
| 310 | assert_equal([' '], GetModes()) |
| 311 | # dict's mode/abbr are ignored |
| 312 | UnmapAll('XZ') |
| 313 | tmap.mode = '!' |
| 314 | tmap.abbr = true |
| 315 | mapset('o', false, tmap) |
| 316 | assert_equal(['o'], GetModes()) |
| 317 | |
| 318 | # test the 3 arg version handles bad mode string, dict not used |
| 319 | assert_fails("mapset('vi', false, {})", 'E1276:') |
| 320 | |
| 321 | |
| 322 | # get the abbreviations out of the way |
| 323 | abbreviate XZ ZX |
| 324 | tmap = maplist(true)->filter((_, m) => m.lhs == 'XZ')[0]->copy() |
| 325 | |
| 326 | abclear |
| 327 | # 'ic' is the default ab command, shows up as '!' |
| 328 | tmap.mode = 'ic' |
| 329 | mapset(tmap) |
| 330 | assert_equal(['!'], GetModes(true)) |
| 331 | |
| 332 | abclear |
| 333 | tmap.mode = 'i' |
| 334 | mapset(tmap) |
| 335 | assert_equal(['i'], GetModes(true)) |
| 336 | |
| 337 | abclear |
| 338 | tmap.mode = 'c' |
| 339 | mapset(tmap) |
| 340 | assert_equal(['c'], GetModes(true)) |
| 341 | |
| 342 | abclear |
| 343 | tmap.mode = '!' |
| 344 | mapset(tmap) |
| 345 | assert_equal(['!'], GetModes(true)) |
| 346 | |
| 347 | assert_fails("mapset({mode: ' !', abbr: 1})", 'E1276:') |
| 348 | assert_fails("mapset({mode: 'cl', abbr: 1})", 'E1276:') |
| 349 | assert_fails("mapset({mode: 'in', abbr: 1})", 'E1276:') |
| 350 | |
| 351 | # the map commands |
| 352 | map XZ x |
| 353 | tmap = maplist()->filter((_, m) => m.lhs == 'XZ')[0]->copy() |
| 354 | |
| 355 | # try the combos |
| 356 | UnmapAll('XZ') |
| 357 | # 'nxso' is ' ', the unadorned :map |
| 358 | tmap.mode = 'nxso' |
| 359 | mapset(tmap) |
| 360 | assert_equal([' '], GetModes()) |
| 361 | |
| 362 | UnmapAll('XZ') |
| 363 | # 'ic' is '!' |
| 364 | tmap.mode = 'ic' |
| 365 | mapset(tmap) |
| 366 | assert_equal(['!'], GetModes()) |
| 367 | |
| 368 | UnmapAll('XZ') |
| 369 | # 'xs' is really 'v' |
| 370 | tmap.mode = 'xs' |
| 371 | mapset(tmap) |
| 372 | assert_equal(['v'], GetModes()) |
| 373 | |
| 374 | # try the individual modes |
| 375 | UnmapAll('XZ') |
| 376 | tmap.mode = 'n' |
| 377 | mapset(tmap) |
| 378 | assert_equal(['n'], GetModes()) |
| 379 | |
| 380 | UnmapAll('XZ') |
| 381 | tmap.mode = 'x' |
| 382 | mapset(tmap) |
| 383 | assert_equal(['x'], GetModes()) |
| 384 | |
| 385 | UnmapAll('XZ') |
| 386 | tmap.mode = 's' |
| 387 | mapset(tmap) |
| 388 | assert_equal(['s'], GetModes()) |
| 389 | |
| 390 | UnmapAll('XZ') |
| 391 | tmap.mode = 'o' |
| 392 | mapset(tmap) |
| 393 | assert_equal(['o'], GetModes()) |
| 394 | |
| 395 | UnmapAll('XZ') |
| 396 | tmap.mode = 'i' |
| 397 | mapset(tmap) |
| 398 | assert_equal(['i'], GetModes()) |
| 399 | |
| 400 | UnmapAll('XZ') |
| 401 | tmap.mode = 'c' |
| 402 | mapset(tmap) |
| 403 | assert_equal(['c'], GetModes()) |
| 404 | |
| 405 | UnmapAll('XZ') |
| 406 | tmap.mode = 't' |
| 407 | mapset(tmap) |
| 408 | assert_equal(['t'], GetModes()) |
| 409 | |
| 410 | UnmapAll('XZ') |
| 411 | tmap.mode = 'l' |
| 412 | mapset(tmap) |
| 413 | assert_equal(['l'], GetModes()) |
| 414 | |
| 415 | UnmapAll('XZ') |
| 416 | |
| 417 | # get errors for modes that can't be in one mapping |
| 418 | assert_fails("mapset({mode: 'nxsoi', abbr: 0})", 'E1276:') |
| 419 | assert_fails("mapset({mode: ' !', abbr: 0})", 'E1276:') |
| 420 | assert_fails("mapset({mode: 'ix', abbr: 0})", 'E1276:') |
| 421 | assert_fails("mapset({mode: 'tl', abbr: 0})", 'E1276:') |
| 422 | assert_fails("mapset({mode: ' l', abbr: 0})", 'E1276:') |
| 423 | assert_fails("mapset({mode: ' t', abbr: 0})", 'E1276:') |
| 424 | enddef |
| 425 | |
Bram Moolenaar | 9c65253 | 2020-05-24 13:10:18 +0200 | [diff] [blame] | 426 | func Check_ctrlb_map(d, check_alt) |
| 427 | call assert_equal('<C-B>', a:d.lhs) |
| 428 | if a:check_alt |
| 429 | call assert_equal("\x80\xfc\x04B", a:d.lhsraw) |
| 430 | call assert_equal("\x02", a:d.lhsrawalt) |
| 431 | else |
| 432 | call assert_equal("\x02", a:d.lhsraw) |
| 433 | endif |
| 434 | endfunc |
| 435 | |
Bram Moolenaar | 7ba1e4d | 2021-04-24 13:12:38 +0200 | [diff] [blame] | 436 | func Test_map_local() |
| 437 | nmap a global |
| 438 | nmap <buffer>a local |
| 439 | |
| 440 | let prev_map_list = split(execute('nmap a'), "\n") |
| 441 | call assert_match('n\s*a\s*@local', prev_map_list[0]) |
| 442 | call assert_match('n\s*a\s*global', prev_map_list[1]) |
| 443 | |
| 444 | let mapping = maparg('a', 'n', 0, 1) |
| 445 | call assert_equal(1, mapping.buffer) |
| 446 | let mapping.rhs = 'new_local' |
| 447 | call mapset('n', 0, mapping) |
| 448 | |
| 449 | " Check that the global mapping is left untouched. |
| 450 | let map_list = split(execute('nmap a'), "\n") |
| 451 | call assert_match('n\s*a\s*@new_local', map_list[0]) |
| 452 | call assert_match('n\s*a\s*global', map_list[1]) |
| 453 | |
| 454 | nunmap a |
| 455 | endfunc |
| 456 | |
Bram Moolenaar | 9c65253 | 2020-05-24 13:10:18 +0200 | [diff] [blame] | 457 | func Test_map_restore() |
| 458 | " Test restoring map with alternate keycode |
| 459 | nmap <C-B> back |
| 460 | let d = maparg('<C-B>', 'n', 0, 1) |
| 461 | call Check_ctrlb_map(d, 1) |
| 462 | let dsimp = maparg("\x02", 'n', 0, 1) |
| 463 | call Check_ctrlb_map(dsimp, 0) |
| 464 | nunmap <C-B> |
| 465 | call mapset('n', 0, d) |
| 466 | let d = maparg('<C-B>', 'n', 0, 1) |
| 467 | call Check_ctrlb_map(d, 1) |
| 468 | let dsimp = maparg("\x02", 'n', 0, 1) |
| 469 | call Check_ctrlb_map(dsimp, 0) |
| 470 | |
| 471 | nunmap <C-B> |
| 472 | |
| 473 | endfunc |
| 474 | |
Ernie Rael | 0966120 | 2022-04-25 14:40:44 +0100 | [diff] [blame] | 475 | def Test_maplist() |
Ernie Rael | 659c240 | 2022-04-24 18:40:28 +0100 | [diff] [blame] | 476 | new |
Ernie Rael | 0966120 | 2022-04-25 14:40:44 +0100 | [diff] [blame] | 477 | def ClearMappingsAbbreviations() |
Ernie Rael | 659c240 | 2022-04-24 18:40:28 +0100 | [diff] [blame] | 478 | mapclear | nmapclear | vmapclear | xmapclear | smapclear | omapclear |
| 479 | mapclear! | imapclear | lmapclear | cmapclear | tmapclear |
| 480 | mapclear <buffer> | nmapclear <buffer> | vmapclear <buffer> |
| 481 | xmapclear <buffer> | smapclear <buffer> | omapclear <buffer> |
| 482 | mapclear! <buffer> | imapclear <buffer> | lmapclear <buffer> |
| 483 | cmapclear <buffer> | tmapclear <buffer> |
Ernie Rael | 0966120 | 2022-04-25 14:40:44 +0100 | [diff] [blame] | 484 | abclear | abclear <buffer> |
Ernie Rael | 659c240 | 2022-04-24 18:40:28 +0100 | [diff] [blame] | 485 | enddef |
| 486 | |
| 487 | def AddMaps(new: list<string>, accum: list<string>) |
| 488 | if len(new) > 0 && new[0] != "No mapping found" |
| 489 | accum->extend(new) |
| 490 | endif |
| 491 | enddef |
| 492 | |
Ernie Rael | 0966120 | 2022-04-25 14:40:44 +0100 | [diff] [blame] | 493 | ClearMappingsAbbreviations() |
| 494 | assert_equal(0, len(maplist())) |
| 495 | assert_equal(0, len(maplist(true))) |
Ernie Rael | 659c240 | 2022-04-24 18:40:28 +0100 | [diff] [blame] | 496 | |
| 497 | # Set up some mappings. |
| 498 | map dup bar |
| 499 | map <buffer> dup bufbar |
| 500 | map foo<C-V> is<F4>foo |
| 501 | vnoremap <script> <buffer> <expr> <silent> bar isbar |
| 502 | tmap baz foo |
| 503 | omap h w |
| 504 | lmap i w |
| 505 | nmap j w |
| 506 | xmap k w |
| 507 | smap l w |
| 508 | map abc <Nop> |
| 509 | nmap <M-j> x |
| 510 | nmap <M-Space> y |
Ernie Rael | 0966120 | 2022-04-25 14:40:44 +0100 | [diff] [blame] | 511 | # And abbreviations |
| 512 | abbreviate xy he |
| 513 | abbreviate xx she |
| 514 | abbreviate <buffer> x they |
Ernie Rael | 659c240 | 2022-04-24 18:40:28 +0100 | [diff] [blame] | 515 | |
| 516 | # Get a list of the mappings with the ':map' commands. |
Ernie Rael | 0966120 | 2022-04-25 14:40:44 +0100 | [diff] [blame] | 517 | # Check maplist() return a list of the same size. |
| 518 | assert_equal(13, len(maplist())) |
| 519 | assert_equal(3, len(maplist(true))) |
| 520 | assert_equal(13, len(maplist(false))) |
Ernie Rael | 659c240 | 2022-04-24 18:40:28 +0100 | [diff] [blame] | 521 | |
| 522 | # collect all the current maps using :map commands |
| 523 | var maps_command: list<string> |
| 524 | AddMaps(split(execute('map'), '\n'), maps_command) |
| 525 | AddMaps(split(execute('map!'), '\n'), maps_command) |
| 526 | AddMaps(split(execute('tmap'), '\n'), maps_command) |
| 527 | AddMaps(split(execute('lmap'), '\n'), maps_command) |
| 528 | |
Ernie Rael | 0966120 | 2022-04-25 14:40:44 +0100 | [diff] [blame] | 529 | # Use maplist to get all the maps |
| 530 | var maps_maplist = maplist() |
| 531 | assert_equal(len(maps_command), len(maps_maplist)) |
Ernie Rael | 659c240 | 2022-04-24 18:40:28 +0100 | [diff] [blame] | 532 | |
| 533 | # make sure all the mode-lhs are unique, no duplicates |
| 534 | var map_set: dict<number> |
Ernie Rael | 0966120 | 2022-04-25 14:40:44 +0100 | [diff] [blame] | 535 | for d in maps_maplist |
Ernie Rael | 659c240 | 2022-04-24 18:40:28 +0100 | [diff] [blame] | 536 | map_set[d.mode .. "-" .. d.lhs .. "-" .. d.buffer] = 0 |
| 537 | endfor |
Ernie Rael | 0966120 | 2022-04-25 14:40:44 +0100 | [diff] [blame] | 538 | assert_equal(len(maps_maplist), len(map_set)) |
Ernie Rael | 659c240 | 2022-04-24 18:40:28 +0100 | [diff] [blame] | 539 | |
Ernie Rael | 0966120 | 2022-04-25 14:40:44 +0100 | [diff] [blame] | 540 | # For everything returned by maplist, should be the same as from maparg. |
Ernie Rael | 659c240 | 2022-04-24 18:40:28 +0100 | [diff] [blame] | 541 | # Except for "map dup", bacause maparg returns the <buffer> version |
Ernie Rael | 0966120 | 2022-04-25 14:40:44 +0100 | [diff] [blame] | 542 | for d in maps_maplist |
Ernie Rael | 659c240 | 2022-04-24 18:40:28 +0100 | [diff] [blame] | 543 | if d.lhs == 'dup' && d.buffer == 0 |
| 544 | continue |
| 545 | endif |
| 546 | var d_maparg = maparg(d.lhs, d.mode, false, true) |
| 547 | assert_equal(d_maparg, d) |
| 548 | endfor |
| 549 | |
Ernie Rael | 0966120 | 2022-04-25 14:40:44 +0100 | [diff] [blame] | 550 | # Check abbr matches maparg |
| 551 | for d in maplist(true) |
| 552 | # Note, d.mode is '!', but can't use that with maparg |
| 553 | var d_maparg = maparg(d.lhs, 'i', true, true) |
| 554 | assert_equal(d_maparg, d) |
| 555 | endfor |
| 556 | |
| 557 | ClearMappingsAbbreviations() |
| 558 | assert_equal(0, len(maplist())) |
| 559 | assert_equal(0, len(maplist(true))) |
Ernie Rael | 659c240 | 2022-04-24 18:40:28 +0100 | [diff] [blame] | 560 | enddef |
| 561 | |
| 562 | |
Bram Moolenaar | 8ba6bb7 | 2020-01-20 20:41:42 +0100 | [diff] [blame] | 563 | " vim: shiftwidth=2 sts=2 expandtab |