keymap_spec.lua 6.4 KB

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