keymap.lua 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. local keymap = {}
  2. --- Add a new |mapping|.
  3. --- Examples:
  4. --- <pre>
  5. --- -- Can add mapping to Lua functions
  6. --- vim.keymap.set('n', 'lhs', function() print("real lua function") end)
  7. ---
  8. --- -- Can use it to map multiple modes
  9. --- vim.keymap.set({'n', 'v'}, '<leader>lr', vim.lsp.buf.references, { buffer=true })
  10. ---
  11. --- -- Can add mapping for specific buffer
  12. --- vim.keymap.set('n', '<leader>w', "<cmd>w<cr>", { silent = true, buffer = 5 })
  13. ---
  14. --- -- Expr mappings
  15. --- vim.keymap.set('i', '<Tab>', function()
  16. --- return vim.fn.pumvisible() == 1 and "<C-n>" or "<Tab>"
  17. --- end, { expr = true })
  18. --- -- <Plug> mappings
  19. --- vim.keymap.set('n', '[%%', '<Plug>(MatchitNormalMultiBackward)')
  20. --- </pre>
  21. ---
  22. --- Note that in a mapping like:
  23. --- <pre>
  24. --- vim.keymap.set('n', 'asdf', require('jkl').my_fun)
  25. --- </pre>
  26. ---
  27. --- the ``require('jkl')`` gets evaluated during this call in order to access the function.
  28. --- If you want to avoid this cost at startup you can wrap it in a function, for example:
  29. --- <pre>
  30. --- vim.keymap.set('n', 'asdf', function() return require('jkl').my_fun() end)
  31. --- </pre>
  32. ---
  33. ---@param mode string|table Same mode short names as |nvim_set_keymap()|.
  34. --- Can also be list of modes to create mapping on multiple modes.
  35. ---@param lhs string Left-hand side |{lhs}| of the mapping.
  36. ---@param rhs string|function Right-hand side |{rhs}| of the mapping. Can also be a Lua function.
  37. --
  38. ---@param opts table|nil A table of |:map-arguments|.
  39. --- + Accepts options accepted by the {opts} parameter in |nvim_set_keymap()|,
  40. --- with the following notable differences:
  41. --- - replace_keycodes: Defaults to `true` if "expr" is `true`.
  42. --- - noremap: Always overridden with the inverse of "remap" (see below).
  43. --- + In addition to those options, the table accepts the following keys:
  44. --- - buffer: (number or boolean) Add a mapping to the given buffer.
  45. --- When `0` or `true`, use the current buffer.
  46. --- - remap: (boolean) Make the mapping recursive.
  47. --- This is the inverse of the "noremap" option from |nvim_set_keymap()|.
  48. --- Defaults to `false`.
  49. ---@see |nvim_set_keymap()|
  50. function keymap.set(mode, lhs, rhs, opts)
  51. vim.validate({
  52. mode = { mode, { 's', 't' } },
  53. lhs = { lhs, 's' },
  54. rhs = { rhs, { 's', 'f' } },
  55. opts = { opts, 't', true },
  56. })
  57. opts = vim.deepcopy(opts) or {}
  58. mode = type(mode) == 'string' and { mode } or mode
  59. if opts.expr and opts.replace_keycodes ~= false then
  60. opts.replace_keycodes = true
  61. end
  62. if opts.remap == nil then
  63. -- default remap value is false
  64. opts.noremap = true
  65. else
  66. -- remaps behavior is opposite of noremap option.
  67. opts.noremap = not opts.remap
  68. opts.remap = nil
  69. end
  70. if type(rhs) == 'function' then
  71. opts.callback = rhs
  72. rhs = ''
  73. end
  74. if opts.buffer then
  75. local bufnr = opts.buffer == true and 0 or opts.buffer
  76. opts.buffer = nil
  77. for _, m in ipairs(mode) do
  78. vim.api.nvim_buf_set_keymap(bufnr, m, lhs, rhs, opts)
  79. end
  80. else
  81. opts.buffer = nil
  82. for _, m in ipairs(mode) do
  83. vim.api.nvim_set_keymap(m, lhs, rhs, opts)
  84. end
  85. end
  86. end
  87. --- Remove an existing mapping.
  88. --- Examples:
  89. --- <pre>
  90. --- vim.keymap.del('n', 'lhs')
  91. ---
  92. --- vim.keymap.del({'n', 'i', 'v'}, '<leader>w', { buffer = 5 })
  93. --- </pre>
  94. ---@param opts table|nil A table of optional arguments:
  95. --- - buffer: (number or boolean) Remove a mapping from the given buffer.
  96. --- When "true" or 0, use the current buffer.
  97. ---@see |vim.keymap.set()|
  98. ---
  99. function keymap.del(modes, lhs, opts)
  100. vim.validate({
  101. mode = { modes, { 's', 't' } },
  102. lhs = { lhs, 's' },
  103. opts = { opts, 't', true },
  104. })
  105. opts = opts or {}
  106. modes = type(modes) == 'string' and { modes } or modes
  107. local buffer = false
  108. if opts.buffer ~= nil then
  109. buffer = opts.buffer == true and 0 or opts.buffer
  110. end
  111. if buffer == false then
  112. for _, mode in ipairs(modes) do
  113. vim.api.nvim_del_keymap(mode, lhs)
  114. end
  115. else
  116. for _, mode in ipairs(modes) do
  117. vim.api.nvim_buf_del_keymap(buffer, mode, lhs)
  118. end
  119. end
  120. end
  121. return keymap