map_spec.lua 7.7 KB

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