map_spec.lua 7.1 KB

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