map_spec.lua (7309B)
1 local t = require('test.testutil') 2 local n = require('test.functional.testnvim')() 3 local Screen = require('test.functional.ui.screen') 4 5 local eq = t.eq 6 local exec = n.exec 7 local exec_capture = n.exec_capture 8 local feed = n.feed 9 local api = n.api 10 local clear = n.clear 11 local command = n.command 12 local expect = n.expect 13 local insert = n.insert 14 local eval = n.eval 15 16 describe(':*map', function() 17 before_each(clear) 18 19 it('are not affected by &isident', function() 20 api.nvim_set_var('counter', 0) 21 command('nnoremap <C-x> :let counter+=1<CR>') 22 api.nvim_set_option_value('isident', ('%u'):format(('>'):byte()), {}) 23 command('nnoremap <C-y> :let counter+=1<CR>') 24 -- &isident used to disable keycode parsing here as well 25 feed('\24\25<C-x><C-y>') 26 eq(4, api.nvim_get_var('counter')) 27 end) 28 29 it(':imap <M-">', function() 30 command('imap <M-"> foo') 31 feed('i-<M-">-') 32 expect('-foo-') 33 end) 34 35 it('shows <Nop> as mapping rhs', function() 36 command('nmap asdf <Nop>') 37 eq( 38 [[ 39 40 n asdf <Nop>]], 41 exec_capture('nmap asdf') 42 ) 43 end) 44 45 it('mappings with description can be filtered', function() 46 api.nvim_set_keymap('n', 'asdf1', 'qwert', { desc = 'do the one thing' }) 47 api.nvim_set_keymap('n', 'asdf2', 'qwert', { desc = 'doesnot really do anything' }) 48 api.nvim_set_keymap('n', 'asdf3', 'qwert', { desc = 'do the other thing' }) 49 eq( 50 [[ 51 52 n asdf3 qwert 53 do the other thing 54 n asdf1 qwert 55 do the one thing]], 56 exec_capture('filter the nmap') 57 ) 58 end) 59 60 it('<Plug> mappings ignore nore', function() 61 command('let x = 0') 62 eq(0, api.nvim_eval('x')) 63 command [[ 64 nnoremap <Plug>(Increase_x) <cmd>let x+=1<cr> 65 nmap increase_x_remap <Plug>(Increase_x) 66 nnoremap increase_x_noremap <Plug>(Increase_x) 67 ]] 68 feed('increase_x_remap') 69 eq(1, api.nvim_eval('x')) 70 feed('increase_x_noremap') 71 eq(2, api.nvim_eval('x')) 72 end) 73 74 it("Doesn't auto ignore nore for keys before or after <Plug> mapping", function() 75 command('let x = 0') 76 eq(0, api.nvim_eval('x')) 77 command [[ 78 nnoremap x <nop> 79 nnoremap <Plug>(Increase_x) <cmd>let x+=1<cr> 80 nmap increase_x_remap x<Plug>(Increase_x)x 81 nnoremap increase_x_noremap x<Plug>(Increase_x)x 82 ]] 83 insert('Some text') 84 eq('Some text', eval("getline('.')")) 85 86 feed('increase_x_remap') 87 eq(1, api.nvim_eval('x')) 88 eq('Some text', eval("getline('.')")) 89 feed('increase_x_noremap') 90 eq(2, api.nvim_eval('x')) 91 eq('Some te', eval("getline('.')")) 92 end) 93 94 it(':unmap with rhs works when lhs is in another bucket #21530', function() 95 command('map F <Plug>Foo') 96 command('unmap <Plug>Foo') 97 eq('\nNo mapping found', exec_capture('map F')) 98 end) 99 end) 100 101 describe('Screen', function() 102 local screen 103 before_each(function() 104 clear() 105 screen = Screen.new(20, 5) 106 end) 107 108 it('cursor is restored after :map <expr> which calls input()', function() 109 command('map <expr> x input("> ")') 110 screen:expect([[ 111 ^ | 112 {1:~ }|*3 113 | 114 ]]) 115 feed('x') 116 screen:expect([[ 117 | 118 {1:~ }|*3 119 > ^ | 120 ]]) 121 feed('\n') 122 screen:expect([[ 123 ^ | 124 {1:~ }|*3 125 > | 126 ]]) 127 end) 128 129 it('cursor is restored after :imap <expr> which calls input()', function() 130 command('imap <expr> x input("> ")') 131 feed('i') 132 screen:expect([[ 133 ^ | 134 {1:~ }|*3 135 {5:-- INSERT --} | 136 ]]) 137 feed('x') 138 screen:expect([[ 139 | 140 {1:~ }|*3 141 > ^ | 142 ]]) 143 feed('\n') 144 screen:expect([[ 145 ^ | 146 {1:~ }|*3 147 {5:-- INSERT --} | 148 ]]) 149 end) 150 151 it('cursor position does not move after empty-string :cmap <expr> #19046', function() 152 command([[cnoremap <expr> <F2> '']]) 153 feed(':<F2>') 154 screen:expect([[ 155 | 156 {1:~ }|*3 157 :^ | 158 ]]) 159 end) 160 161 -- oldtest: Test_expr_map_restore_cursor() 162 it('cursor is restored after :map <expr> which redraws statusline vim-patch:8.1.2336', function() 163 exec([[ 164 call setline(1, ['one', 'two', 'three']) 165 2 166 set ls=2 167 hi! link StatusLine ErrorMsg 168 noremap <expr> <C-B> Func() 169 func Func() 170 let g:on = !get(g:, 'on', 0) 171 redraws 172 return '' 173 endfunc 174 func Status() 175 return get(g:, 'on', 0) ? '[on]' : '' 176 endfunc 177 set stl=%{Status()} 178 ]]) 179 feed('<C-B>') 180 screen:expect([[ 181 one | 182 ^two | 183 three | 184 {9:[on] }| 185 | 186 ]]) 187 end) 188 189 it('error in :nmap <expr> does not mess up display vim-patch:4.2.4338', function() 190 screen:try_resize(40, 5) 191 command('nmap <expr> <F2> execute("throw 42")') 192 feed('<F2>') 193 screen:expect([[ 194 | 195 {3: }| 196 {9:Error in :} | 197 {9:E605: Exception not caught: 42} | 198 {6:Press ENTER or type command to continue}^ | 199 ]]) 200 feed('<CR>') 201 screen:expect([[ 202 ^ | 203 {1:~ }|*3 204 | 205 ]]) 206 end) 207 208 it('error in :cmap <expr> handled correctly vim-patch:4.2.4338', function() 209 screen:try_resize(40, 5) 210 command('cmap <expr> <F2> execute("throw 42")') 211 feed(':echo "foo') 212 screen:expect([[ 213 | 214 {1:~ }|*3 215 :echo "foo^ | 216 ]]) 217 feed('<F2>') 218 screen:expect([[ 219 {3: }| 220 :echo "foo | 221 {9:Error in :} | 222 {9:E605: Exception not caught: 42} | 223 :echo "foo^ | 224 ]]) 225 feed('"') 226 screen:expect([[ 227 {3: }| 228 :echo "foo | 229 {9:Error in :} | 230 {9:E605: Exception not caught: 42} | 231 :echo "foo"^ | 232 ]]) 233 feed('\n') 234 screen:expect([[ 235 :echo "foo | 236 {9:Error in :} | 237 {9:E605: Exception not caught: 42} | 238 foo | 239 {6:Press ENTER or type command to continue}^ | 240 ]]) 241 end) 242 243 -- oldtest: Test_map_listing() 244 it('listing mappings clears command line vim-patch:8.2.4401', function() 245 screen:try_resize(40, 5) 246 command('nmap a b') 247 feed(': nmap a<CR>') 248 screen:expect([[ 249 ^ | 250 {1:~ }|*3 251 n a b | 252 ]]) 253 end) 254 end)