keymap_spec.lua 6.4 KB

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