mapping_spec.lua 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. -- Test for mappings and abbreviations
  2. local t = require('test.testutil')
  3. local n = require('test.functional.testnvim')()
  4. local Screen = require('test.functional.ui.screen')
  5. local clear, feed, insert = n.clear, n.feed, n.insert
  6. local expect, poke_eventloop = n.expect, n.poke_eventloop
  7. local command, eq, eval, api = n.command, t.eq, n.eval, n.api
  8. local exec = n.exec
  9. local sleep = vim.uv.sleep
  10. describe('mapping', function()
  11. before_each(clear)
  12. it('abbreviations with р (0x80)', function()
  13. insert([[
  14. test starts here:
  15. ]])
  16. -- Abbreviations with р (0x80) should work.
  17. command('inoreab чкпр vim')
  18. feed('GAчкпр <esc>')
  19. expect([[
  20. test starts here:
  21. vim ]])
  22. end)
  23. -- oldtest: Test_map_ctrl_c_insert()
  24. it('Ctrl-c works in Insert mode', function()
  25. -- Mapping of ctrl-c in insert mode
  26. command('set cpo-=< cpo-=k')
  27. command('inoremap <c-c> <ctrl-c>')
  28. command('cnoremap <c-c> dummy')
  29. command('cunmap <c-c>')
  30. feed('GA<cr>')
  31. -- XXX: editor must be in Insert mode before <C-C> is put into input buffer
  32. poke_eventloop()
  33. feed('TEST2: CTRL-C |<c-c>A|<cr><esc>')
  34. command('unmap! <c-c>')
  35. expect([[
  36. TEST2: CTRL-C |<ctrl-c>A|
  37. ]])
  38. end)
  39. -- oldtest: Test_map_ctrl_c_visual()
  40. it('Ctrl-c works in Visual mode', function()
  41. command([[vnoremap <c-c> :<C-u>$put ='vmap works'<cr>]])
  42. feed('GV')
  43. -- XXX: editor must be in Visual mode before <C-C> is put into input buffer
  44. poke_eventloop()
  45. feed('vV<c-c>')
  46. command('vunmap <c-c>')
  47. expect([[
  48. vmap works]])
  49. end)
  50. it('langmap', function()
  51. -- langmap should not get remapped in insert mode.
  52. command('inoremap { FAIL_ilangmap')
  53. command('set langmap=+{ langnoremap')
  54. feed('o+<esc>')
  55. -- Insert mode expr mapping with langmap.
  56. command('inoremap <expr> { "FAIL_iexplangmap"')
  57. feed('o+<esc>')
  58. -- langmap should not get remapped in cmdline mode.
  59. command('cnoremap { FAIL_clangmap')
  60. feed('o+<esc>')
  61. command('cunmap {')
  62. -- cmdline mode expr mapping with langmap.
  63. command('cnoremap <expr> { "FAIL_cexplangmap"')
  64. feed('o+<esc>')
  65. command('cunmap {')
  66. -- Assert buffer contents.
  67. expect([[
  68. +
  69. +
  70. +
  71. +]])
  72. end)
  73. -- oldtest: Test_map_feedkeys()
  74. it('feedkeys', function()
  75. insert([[
  76. a b c d
  77. a b c d
  78. ]])
  79. -- Vim's issue #212 (feedkeys insert mapping at current position)
  80. command('nnoremap . :call feedkeys(".", "in")<cr>')
  81. feed('/^a b<cr>')
  82. feed('0qqdw.ifoo<esc>qj0@q<esc>')
  83. command('unmap .')
  84. expect([[
  85. fooc d
  86. fooc d
  87. ]])
  88. end)
  89. -- oldtest: Test_map_cursor()
  90. it('i_CTRL-G_U', function()
  91. -- <c-g>U<cursor> works only within a single line
  92. command('imapclear')
  93. command('imap ( ()<c-g>U<left>')
  94. feed('G2o<esc>ki<cr>Test1: text with a (here some more text<esc>k.')
  95. -- test undo
  96. feed('G2o<esc>ki<cr>Test2: text wit a (here some more text [und undo]<c-g>u<esc>k.u')
  97. command('imapclear')
  98. command('set whichwrap=<,>,[,]')
  99. feed('G3o<esc>2k')
  100. command(
  101. [[:exe ":norm! iTest3: text with a (parenthesis here\<C-G>U\<Right>new line here\<esc>\<up>\<up>."]]
  102. )
  103. expect([[
  104. Test1: text with a (here some more text)
  105. Test1: text with a (here some more text)
  106. Test2: text wit a (here some more text [und undo])
  107. new line here
  108. Test3: text with a (parenthesis here
  109. new line here
  110. ]])
  111. end)
  112. -- oldtest: Test_mouse_drag_mapped_start_select()
  113. it('dragging starts Select mode even if coming from mapping', function()
  114. command('set mouse=a')
  115. command('set selectmode=mouse')
  116. command('nnoremap <LeftDrag> <LeftDrag><Cmd><CR>')
  117. poke_eventloop()
  118. api.nvim_input_mouse('left', 'press', '', 0, 0, 0)
  119. poke_eventloop()
  120. api.nvim_input_mouse('left', 'drag', '', 0, 0, 1)
  121. poke_eventloop()
  122. eq('s', eval('mode()'))
  123. end)
  124. -- oldtest: Test_mouse_drag_insert_map()
  125. it('<LeftDrag> mapping in Insert mode works correctly', function()
  126. command('set mouse=a')
  127. command('inoremap <LeftDrag> <LeftDrag><Cmd>let g:dragged = 1<CR>')
  128. feed('i')
  129. poke_eventloop()
  130. api.nvim_input_mouse('left', 'press', '', 0, 0, 0)
  131. poke_eventloop()
  132. api.nvim_input_mouse('left', 'drag', '', 0, 0, 1)
  133. poke_eventloop()
  134. eq(1, eval('g:dragged'))
  135. eq('v', eval('mode()'))
  136. feed([[<C-\><C-N>]])
  137. command([[inoremap <LeftDrag> <LeftDrag><C-\><C-N>]])
  138. feed('i')
  139. poke_eventloop()
  140. api.nvim_input_mouse('left', 'press', '', 0, 0, 0)
  141. poke_eventloop()
  142. api.nvim_input_mouse('left', 'drag', '', 0, 0, 1)
  143. poke_eventloop()
  144. eq('n', eval('mode()'))
  145. end)
  146. -- oldtest: Test_map_after_timed_out_nop()
  147. it('timeout works after an <Nop> mapping is triggered on timeout', function()
  148. command('set timeout timeoutlen=400')
  149. command('inoremap ab TEST')
  150. command('inoremap a <Nop>')
  151. -- Enter Insert mode
  152. feed('i')
  153. -- Wait for the "a" mapping to time out
  154. feed('a')
  155. sleep(500)
  156. -- Send "a" and wait for a period shorter than 'timeoutlen'
  157. feed('a')
  158. sleep(100)
  159. -- Send "b", should trigger the "ab" mapping
  160. feed('b')
  161. expect('TEST')
  162. end)
  163. -- oldtest: Test_showcmd_part_map()
  164. it("'showcmd' with a partial mapping", function()
  165. local screen = Screen.new(60, 6)
  166. exec([[
  167. set notimeout showcmd
  168. nnoremap ,a <Ignore>
  169. nnoremap ;a <Ignore>
  170. nnoremap Àa <Ignore>
  171. nnoremap Ëa <Ignore>
  172. nnoremap βa <Ignore>
  173. nnoremap ωa <Ignore>
  174. nnoremap …a <Ignore>
  175. nnoremap <C-W>a <Ignore>
  176. ]])
  177. for _, c in ipairs({ ',', ';', 'À', 'Ë', 'β', 'ω', '…' }) do
  178. feed(c)
  179. screen:expect(([[
  180. ^ |
  181. {1:~ }|*4
  182. %s |
  183. ]]):format(c))
  184. feed('a')
  185. screen:expect([[
  186. ^ |
  187. {1:~ }|*4
  188. |
  189. ]])
  190. end
  191. feed('\23')
  192. screen:expect([[
  193. ^ |
  194. {1:~ }|*4
  195. ^W |
  196. ]])
  197. feed('a')
  198. screen:expect([[
  199. ^ |
  200. {1:~ }|*4
  201. |
  202. ]])
  203. feed('<C-W>')
  204. screen:expect([[
  205. ^ |
  206. {1:~ }|*4
  207. ^W |
  208. ]])
  209. feed('a')
  210. screen:expect([[
  211. ^ |
  212. {1:~ }|*4
  213. |
  214. ]])
  215. end)
  216. end)