keymap_spec.lua (6527B)
1 local t = require('test.testutil') 2 local n = require('test.functional.testnvim')() 3 4 local clear, feed, eq = n.clear, n.feed, t.eq 5 local expect, command, eval = n.expect, n.command, n.eval 6 local insert, call = n.insert, n.call 7 local exec_capture, dedent = n.exec_capture, t.dedent 8 9 -- First test it's implemented using the :lmap and :lnoremap commands, then 10 -- check those mappings behave as expected. 11 describe("'keymap' / :lmap", function() 12 before_each(function() 13 clear() 14 insert('lllaaa') 15 command('set iminsert=1') 16 command('set imsearch=1') 17 command('lmap l a') 18 feed('gg0') 19 end) 20 21 describe("'keymap' as :lmap", function() 22 -- Shows that 'keymap' sets language mappings that allows remapping. 23 -- This equivalence allows us to only test :lmap commands and assert they 24 -- behave the same as 'keymap' settings. 25 -- It does rely on the absence of special code that implements 'keymap' 26 -- and :lmap differently but shows mappings from the 'keymap' after 27 -- typing :lmap. 28 -- At the moment this is the case. 29 it("'keymap' mappings are shown with :lmap", function() 30 command('lmapclear') 31 command('lmapclear <buffer>') 32 command('set keymap=dvorak') 33 command('set nomore') 34 local bindings = exec_capture('lmap') 35 eq( 36 dedent([[ 37 38 l " @_ 39 l ' @- 40 l + @} 41 l , @w 42 l - @[ 43 l . @v 44 l / @z 45 l : @S 46 l ; @s 47 l < @W 48 l = @] 49 l > @V 50 l ? @Z 51 l A @A 52 l B @X 53 l C @J 54 l D @E 55 l E @> 56 l F @U 57 l G @I 58 l H @D 59 l I @C 60 l J @H 61 l K @T 62 l L @N 63 l M @M 64 l N @B 65 l O @R 66 l P @L 67 l Q @" 68 l R @P 69 l S @O 70 l T @Y 71 l U @G 72 l V @K 73 l W @< 74 l X @Q 75 l Y @F 76 l Z @: 77 l [ @/ 78 l \ @\ 79 l ] @= 80 l _ @{ 81 l a @a 82 l b @x 83 l c @j 84 l d @e 85 l e @. 86 l f @u 87 l g @i 88 l h @d 89 l i @c 90 l j @h 91 l k @t 92 l l @n 93 l m @m 94 l n @b 95 l o @r 96 l p @l 97 l q @' 98 l r @p 99 l s @o 100 l t @y 101 l u @g 102 l v @k 103 l w @, 104 l x @q 105 l y @f 106 l z @; 107 l { @? 108 l | @| 109 l } @+]]), 110 bindings 111 ) 112 end) 113 end) 114 describe("'iminsert' option", function() 115 it('Uses :lmap in insert mode when ON', function() 116 feed('il<esc>') 117 expect('alllaaa') 118 end) 119 it('Ignores :lmap in insert mode when OFF', function() 120 command('set iminsert=0') 121 feed('il<esc>') 122 expect('llllaaa') 123 end) 124 it('Can be toggled with <C-^> in insert mode', function() 125 feed('i<C-^>l<C-^>l<esc>') 126 expect('lalllaaa') 127 eq(1, eval('&iminsert')) 128 feed('i<C-^><esc>') 129 eq(0, eval('&iminsert')) 130 end) 131 end) 132 describe("'imsearch' option", function() 133 it('Uses :lmap at search prompt when ON', function() 134 feed('/lll<cr>3x') 135 expect('lll') 136 end) 137 it('Ignores :lmap at search prompt when OFF', function() 138 command('set imsearch=0') 139 feed('gg/lll<cr>3x') 140 expect('aaa') 141 end) 142 it('Can be toggled with C-^', function() 143 eq(1, eval('&imsearch')) 144 feed('/<C-^>lll<cr>3x') 145 expect('aaa') 146 eq(0, eval('&imsearch')) 147 feed('u0/<C-^>lll<cr>3x') 148 expect('lll') 149 eq(1, eval('&imsearch')) 150 end) 151 it("can follow 'iminsert'", function() 152 command('set imsearch=-1') 153 feed('/lll<cr>3x') 154 expect('lll') 155 eq(-1, eval('&imsearch')) 156 eq(1, eval('&iminsert')) 157 feed('u/<C-^>lll<cr>3x') 158 expect('aaa') 159 eq(-1, eval('&imsearch')) 160 eq(0, eval('&iminsert')) 161 end) 162 end) 163 it(':lmap not applied to macros', function() 164 command("call setreg('a', 'il')") 165 feed('@a') 166 expect('llllaaa') 167 eq('il', call('getreg', 'a')) 168 end) 169 it(':lmap applied to macro recording', function() 170 feed('qail<esc>q@a') 171 expect('aalllaaa') 172 eq('ia', call('getreg', 'a')) 173 end) 174 it(':lmap not applied to mappings', function() 175 command('imap t l') 176 feed('it<esc>') 177 expect('llllaaa') 178 end) 179 it('mappings applied to keys created with :lmap', function() 180 command('imap a x') 181 feed('il<esc>') 182 expect('xlllaaa') 183 end) 184 it('mappings not applied to keys gotten with :lnoremap', function() 185 command('lmapclear') 186 command('lnoremap l a') 187 command('imap a x') 188 feed('il<esc>') 189 expect('alllaaa') 190 end) 191 -- This is a problem introduced when introducing :lmap and macro 192 -- compatibility. There are no plans to fix this as the complexity involved 193 -- seems too great. 194 pending('mappings not applied to macro replay of :lnoremap', function() 195 command('lmapclear') 196 command('lnoremap l a') 197 command('imap a x') 198 feed('qail<esc>q') 199 expect('alllaaa') 200 feed('@a') 201 expect('aalllaaa') 202 end) 203 it('is applied when using f/F t/T', function() 204 feed('flx') 205 expect('lllaa') 206 feed('0ia<esc>4lFlx') 207 expect('lllaa') 208 feed('tllx') 209 expect('llla') 210 feed('0ia<esc>4lTlhx') 211 expect('llla') 212 end) 213 it('takes priority over :imap mappings', function() 214 command('imap l x') 215 feed('il<esc>') 216 expect('alllaaa') 217 command('lmapclear') 218 command('lmap l a') 219 feed('il') 220 expect('aalllaaa') 221 end) 222 it('does not cause recursive mappings', function() 223 command('lmap a l') 224 feed('qaila<esc>q') 225 expect('allllaaa') 226 feed('u@a') 227 expect('allllaaa') 228 end) 229 it('can handle multicharacter mappings', function() 230 command("lmap 'a x") 231 command("lmap '' '") 232 feed("qai'a''a<esc>q") 233 expect("x'alllaaa") 234 feed('u@a') 235 expect("x'alllaaa") 236 end) 237 end)