Bram Moolenaar | 4c9243f | 2020-05-22 13:10:44 +0200 | [diff] [blame] | 1 | " Tests for maparg(), mapcheck() and mapset(). |
Bram Moolenaar | 292eff0 | 2017-07-11 21:46:28 +0200 | [diff] [blame] | 2 | " Also test utf8 map with a 0x80 byte. |
Bram Moolenaar | a144983 | 2019-09-01 20:16:52 +0200 | [diff] [blame] | 3 | " Also test mapcheck() |
Bram Moolenaar | 292eff0 | 2017-07-11 21:46:28 +0200 | [diff] [blame] | 4 | |
Bram Moolenaar | 4c9243f | 2020-05-22 13:10:44 +0200 | [diff] [blame] | 5 | func s:SID() |
Bram Moolenaar | 292eff0 | 2017-07-11 21:46:28 +0200 | [diff] [blame] | 6 | return str2nr(matchstr(expand('<sfile>'), '<SNR>\zs\d\+\ze_SID$')) |
Bram Moolenaar | 4c9243f | 2020-05-22 13:10:44 +0200 | [diff] [blame] | 7 | endfunc |
Bram Moolenaar | 292eff0 | 2017-07-11 21:46:28 +0200 | [diff] [blame] | 8 | |
Bram Moolenaar | c94c146 | 2020-05-22 20:01:06 +0200 | [diff] [blame] | 9 | func Test_maparg() |
Bram Moolenaar | 292eff0 | 2017-07-11 21:46:28 +0200 | [diff] [blame] | 10 | new |
| 11 | set cpo-=< |
| 12 | set encoding=utf8 |
| 13 | " Test maparg() with a string result |
Bram Moolenaar | f29c1c6 | 2018-09-10 21:05:02 +0200 | [diff] [blame] | 14 | let sid = s:SID() |
| 15 | let lnum = expand('<sflnum>') |
Bram Moolenaar | 292eff0 | 2017-07-11 21:46:28 +0200 | [diff] [blame] | 16 | map foo<C-V> is<F4>foo |
| 17 | vnoremap <script> <buffer> <expr> <silent> bar isbar |
Bram Moolenaar | 292eff0 | 2017-07-11 21:46:28 +0200 | [diff] [blame] | 18 | call assert_equal("is<F4>foo", maparg('foo<C-V>')) |
Bram Moolenaar | 2da0f0c | 2020-04-01 19:22:12 +0200 | [diff] [blame] | 19 | 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] | 20 | \ 'lhsraw': "foo\x80\xfc\x04V", 'lhsrawalt': "foo\x16", |
Bram Moolenaar | f29c1c6 | 2018-09-10 21:05:02 +0200 | [diff] [blame] | 21 | \ 'mode': ' ', 'nowait': 0, 'expr': 0, 'sid': sid, 'lnum': lnum + 1, |
Bram Moolenaar | 9c65253 | 2020-05-24 13:10:18 +0200 | [diff] [blame] | 22 | \ 'rhs': 'is<F4>foo', 'buffer': 0}, |
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 | f29c1c6 | 2018-09-10 21:05:02 +0200 | [diff] [blame] | 26 | \ 'nowait': 0, 'expr': 1, 'sid': sid, 'lnum': lnum + 2, |
Bram Moolenaar | 9c65253 | 2020-05-24 13:10:18 +0200 | [diff] [blame] | 27 | \ 'rhs': 'isbar', 'buffer': 1}, |
Bram Moolenaar | a144983 | 2019-09-01 20:16:52 +0200 | [diff] [blame] | 28 | \ 'bar'->maparg('', 0, 1)) |
Bram Moolenaar | f29c1c6 | 2018-09-10 21:05:02 +0200 | [diff] [blame] | 29 | let lnum = expand('<sflnum>') |
Bram Moolenaar | 292eff0 | 2017-07-11 21:46:28 +0200 | [diff] [blame] | 30 | map <buffer> <nowait> foo bar |
Bram Moolenaar | 9c65253 | 2020-05-24 13:10:18 +0200 | [diff] [blame] | 31 | call assert_equal({'silent': 0, 'noremap': 0, 'script': 0, 'lhs': 'foo', |
| 32 | \ 'lhsraw': 'foo', 'mode': ' ', |
Bram Moolenaar | f29c1c6 | 2018-09-10 21:05:02 +0200 | [diff] [blame] | 33 | \ 'nowait': 1, 'expr': 0, 'sid': sid, 'lnum': lnum + 1, 'rhs': 'bar', |
Bram Moolenaar | 9c65253 | 2020-05-24 13:10:18 +0200 | [diff] [blame] | 34 | \ 'buffer': 1}, |
Bram Moolenaar | 292eff0 | 2017-07-11 21:46:28 +0200 | [diff] [blame] | 35 | \ maparg('foo', '', 0, 1)) |
Bram Moolenaar | 14371ed | 2019-07-27 21:05:21 +0200 | [diff] [blame] | 36 | let lnum = expand('<sflnum>') |
| 37 | tmap baz foo |
Bram Moolenaar | 9c65253 | 2020-05-24 13:10:18 +0200 | [diff] [blame] | 38 | call assert_equal({'silent': 0, 'noremap': 0, 'script': 0, 'lhs': 'baz', |
| 39 | \ 'lhsraw': 'baz', 'mode': 't', |
Bram Moolenaar | 14371ed | 2019-07-27 21:05:21 +0200 | [diff] [blame] | 40 | \ 'nowait': 0, 'expr': 0, 'sid': sid, 'lnum': lnum + 1, 'rhs': 'foo', |
Bram Moolenaar | 9c65253 | 2020-05-24 13:10:18 +0200 | [diff] [blame] | 41 | \ 'buffer': 0}, |
Bram Moolenaar | 14371ed | 2019-07-27 21:05:21 +0200 | [diff] [blame] | 42 | \ maparg('baz', 't', 0, 1)) |
Bram Moolenaar | 292eff0 | 2017-07-11 21:46:28 +0200 | [diff] [blame] | 43 | |
| 44 | map abc x<char-114>x |
Bram Moolenaar | f88a5bc | 2018-05-21 13:28:44 +0200 | [diff] [blame] | 45 | call assert_equal("xrx", maparg('abc')) |
Bram Moolenaar | 292eff0 | 2017-07-11 21:46:28 +0200 | [diff] [blame] | 46 | map abc y<S-char-114>y |
Bram Moolenaar | f88a5bc | 2018-05-21 13:28:44 +0200 | [diff] [blame] | 47 | call assert_equal("yRy", maparg('abc')) |
| 48 | |
Bram Moolenaar | 8ba6bb7 | 2020-01-20 20:41:42 +0100 | [diff] [blame] | 49 | 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 Moolenaar | c2a60ae | 2020-01-23 16:19:54 +0100 | [diff] [blame] | 54 | 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 Moolenaar | f88a5bc | 2018-05-21 13:28:44 +0200 | [diff] [blame] | 74 | map abc <Nop> |
| 75 | call assert_equal("<Nop>", maparg('abc')) |
| 76 | unmap abc |
Bram Moolenaar | c2a60ae | 2020-01-23 16:19:54 +0100 | [diff] [blame] | 77 | |
| 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 Moolenaar | 4c9243f | 2020-05-22 13:10:44 +0200 | [diff] [blame] | 82 | endfunc |
Bram Moolenaar | 292eff0 | 2017-07-11 21:46:28 +0200 | [diff] [blame] | 83 | |
Bram Moolenaar | a144983 | 2019-09-01 20:16:52 +0200 | [diff] [blame] | 84 | func Test_mapcheck() |
| 85 | call assert_equal('', mapcheck('a')) |
| 86 | call assert_equal('', mapcheck('abc')) |
| 87 | call assert_equal('', mapcheck('ax')) |
| 88 | call assert_equal('', mapcheck('b')) |
| 89 | |
| 90 | map a something |
| 91 | call assert_equal('something', mapcheck('a')) |
| 92 | call assert_equal('something', mapcheck('a', 'n')) |
| 93 | call assert_equal('', mapcheck('a', 'c')) |
| 94 | call assert_equal('', mapcheck('a', 'i')) |
| 95 | call assert_equal('something', 'abc'->mapcheck()) |
| 96 | call assert_equal('something', 'ax'->mapcheck()) |
| 97 | call assert_equal('', mapcheck('b')) |
| 98 | unmap a |
| 99 | |
| 100 | map ab foobar |
| 101 | call assert_equal('foobar', mapcheck('a')) |
| 102 | call assert_equal('foobar', mapcheck('abc')) |
| 103 | call assert_equal('', mapcheck('ax')) |
| 104 | call assert_equal('', mapcheck('b')) |
| 105 | unmap ab |
| 106 | |
| 107 | map abc barfoo |
| 108 | call assert_equal('barfoo', mapcheck('a')) |
| 109 | call assert_equal('barfoo', mapcheck('a', 'n', 0)) |
| 110 | call assert_equal('', mapcheck('a', 'n', 1)) |
| 111 | call assert_equal('barfoo', mapcheck('abc')) |
| 112 | call assert_equal('', mapcheck('ax')) |
| 113 | call assert_equal('', mapcheck('b')) |
| 114 | unmap abc |
| 115 | |
| 116 | abbr ab abbrev |
| 117 | call assert_equal('abbrev', mapcheck('a', 'i', 1)) |
| 118 | call assert_equal('', mapcheck('a', 'n', 1)) |
| 119 | call assert_equal('', mapcheck('a', 'i', 0)) |
| 120 | unabbr ab |
| 121 | endfunc |
| 122 | |
Bram Moolenaar | 4c9243f | 2020-05-22 13:10:44 +0200 | [diff] [blame] | 123 | func Test_range_map() |
Bram Moolenaar | 292eff0 | 2017-07-11 21:46:28 +0200 | [diff] [blame] | 124 | new |
| 125 | " Outside of the range, minimum |
| 126 | inoremap <Char-0x1040> a |
| 127 | execute "normal a\u1040\<Esc>" |
| 128 | " Inside of the range, minimum |
| 129 | inoremap <Char-0x103f> b |
| 130 | execute "normal a\u103f\<Esc>" |
| 131 | " Inside of the range, maximum |
| 132 | inoremap <Char-0xf03f> c |
| 133 | execute "normal a\uf03f\<Esc>" |
| 134 | " Outside of the range, maximum |
| 135 | inoremap <Char-0xf040> d |
| 136 | execute "normal a\uf040\<Esc>" |
| 137 | call assert_equal("abcd", getline(1)) |
Bram Moolenaar | 4c9243f | 2020-05-22 13:10:44 +0200 | [diff] [blame] | 138 | endfunc |
| 139 | |
| 140 | func One_mapset_test(keys) |
| 141 | exe 'nnoremap ' .. a:keys .. ' original<CR>' |
| 142 | let orig = maparg(a:keys, 'n', 0, 1) |
| 143 | call assert_equal(a:keys, orig.lhs) |
| 144 | call assert_equal('original<CR>', orig.rhs) |
| 145 | call assert_equal('n', orig.mode) |
| 146 | |
| 147 | exe 'nunmap ' .. a:keys |
| 148 | let d = maparg(a:keys, 'n', 0, 1) |
| 149 | call assert_equal({}, d) |
| 150 | |
| 151 | call mapset('n', 0, orig) |
| 152 | let d = maparg(a:keys, 'n', 0, 1) |
| 153 | call assert_equal(a:keys, d.lhs) |
| 154 | call assert_equal('original<CR>', d.rhs) |
| 155 | call assert_equal('n', d.mode) |
| 156 | |
| 157 | exe 'nunmap ' .. a:keys |
| 158 | endfunc |
| 159 | |
| 160 | func Test_mapset() |
| 161 | call One_mapset_test('K') |
| 162 | call One_mapset_test('<F3>') |
Bram Moolenaar | c94c146 | 2020-05-22 20:01:06 +0200 | [diff] [blame] | 163 | |
| 164 | " Check <> key conversion |
| 165 | new |
| 166 | inoremap K one<Left>x |
| 167 | call feedkeys("iK\<Esc>", 'xt') |
| 168 | call assert_equal('onxe', getline(1)) |
| 169 | |
| 170 | let orig = maparg('K', 'i', 0, 1) |
| 171 | call assert_equal('K', orig.lhs) |
| 172 | call assert_equal('one<Left>x', orig.rhs) |
| 173 | call assert_equal('i', orig.mode) |
| 174 | |
| 175 | iunmap K |
| 176 | let d = maparg('K', 'i', 0, 1) |
| 177 | call assert_equal({}, d) |
| 178 | |
| 179 | call mapset('i', 0, orig) |
| 180 | call feedkeys("SK\<Esc>", 'xt') |
| 181 | call assert_equal('onxe', getline(1)) |
| 182 | |
| 183 | iunmap K |
| 184 | |
| 185 | " Test literal <CR> using a backslash |
| 186 | let cpo_save = &cpo |
| 187 | set cpo-=B |
| 188 | inoremap K one\<CR>two |
| 189 | call feedkeys("SK\<Esc>", 'xt') |
| 190 | call assert_equal('one<CR>two', getline(1)) |
| 191 | |
| 192 | let orig = maparg('K', 'i', 0, 1) |
| 193 | call assert_equal('K', orig.lhs) |
| 194 | call assert_equal('one\<CR>two', orig.rhs) |
| 195 | call assert_equal('i', orig.mode) |
| 196 | |
| 197 | iunmap K |
| 198 | let d = maparg('K', 'i', 0, 1) |
| 199 | call assert_equal({}, d) |
| 200 | |
| 201 | call mapset('i', 0, orig) |
| 202 | call feedkeys("SK\<Esc>", 'xt') |
| 203 | call assert_equal('one<CR>two', getline(1)) |
| 204 | |
| 205 | iunmap K |
Bram Moolenaar | c94c146 | 2020-05-22 20:01:06 +0200 | [diff] [blame] | 206 | |
| 207 | " Test literal <CR> using CTRL-V |
| 208 | inoremap K one<CR>two |
| 209 | call feedkeys("SK\<Esc>", 'xt') |
| 210 | call assert_equal('one<CR>two', getline(1)) |
| 211 | |
| 212 | let orig = maparg('K', 'i', 0, 1) |
| 213 | call assert_equal('K', orig.lhs) |
| 214 | call assert_equal("one\x16<CR>two", orig.rhs) |
| 215 | call assert_equal('i', orig.mode) |
| 216 | |
| 217 | iunmap K |
| 218 | let d = maparg('K', 'i', 0, 1) |
| 219 | call assert_equal({}, d) |
| 220 | |
| 221 | call mapset('i', 0, orig) |
| 222 | call feedkeys("SK\<Esc>", 'xt') |
| 223 | call assert_equal('one<CR>two', getline(1)) |
| 224 | |
| 225 | iunmap K |
| 226 | let &cpo = cpo_save |
Bram Moolenaar | c94c146 | 2020-05-22 20:01:06 +0200 | [diff] [blame] | 227 | bwipe! |
Bram Moolenaar | 4c9243f | 2020-05-22 13:10:44 +0200 | [diff] [blame] | 228 | endfunc |
Bram Moolenaar | 8ba6bb7 | 2020-01-20 20:41:42 +0100 | [diff] [blame] | 229 | |
Bram Moolenaar | 9c65253 | 2020-05-24 13:10:18 +0200 | [diff] [blame] | 230 | func Check_ctrlb_map(d, check_alt) |
| 231 | call assert_equal('<C-B>', a:d.lhs) |
| 232 | if a:check_alt |
| 233 | call assert_equal("\x80\xfc\x04B", a:d.lhsraw) |
| 234 | call assert_equal("\x02", a:d.lhsrawalt) |
| 235 | else |
| 236 | call assert_equal("\x02", a:d.lhsraw) |
| 237 | endif |
| 238 | endfunc |
| 239 | |
| 240 | func Test_map_restore() |
| 241 | " Test restoring map with alternate keycode |
| 242 | nmap <C-B> back |
| 243 | let d = maparg('<C-B>', 'n', 0, 1) |
| 244 | call Check_ctrlb_map(d, 1) |
| 245 | let dsimp = maparg("\x02", 'n', 0, 1) |
| 246 | call Check_ctrlb_map(dsimp, 0) |
| 247 | nunmap <C-B> |
| 248 | call mapset('n', 0, d) |
| 249 | let d = maparg('<C-B>', 'n', 0, 1) |
| 250 | call Check_ctrlb_map(d, 1) |
| 251 | let dsimp = maparg("\x02", 'n', 0, 1) |
| 252 | call Check_ctrlb_map(dsimp, 0) |
| 253 | |
| 254 | nunmap <C-B> |
| 255 | |
| 256 | endfunc |
| 257 | |
Bram Moolenaar | 8ba6bb7 | 2020-01-20 20:41:42 +0100 | [diff] [blame] | 258 | " vim: shiftwidth=2 sts=2 expandtab |