keymap.lua 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. local keymap = {}
  2. --- Table of |:map-arguments|.
  3. --- Same as |nvim_set_keymap()| {opts}, except:
  4. --- - {replace_keycodes} defaults to `true` if "expr" is `true`.
  5. ---
  6. --- Also accepts:
  7. --- @class vim.keymap.set.Opts : vim.api.keyset.keymap
  8. --- @inlinedoc
  9. ---
  10. --- Creates buffer-local mapping, `0` or `true` for current buffer.
  11. --- @field buffer? integer|boolean
  12. ---
  13. --- Make the mapping recursive. Inverse of {noremap}.
  14. --- (Default: `false`)
  15. --- @field remap? boolean
  16. --- Defines a |mapping| of |keycodes| to a function or keycodes.
  17. ---
  18. --- Examples:
  19. ---
  20. --- ```lua
  21. --- -- Map "x" to a Lua function:
  22. --- vim.keymap.set('n', 'x', function() print("real lua function") end)
  23. --- -- Map "<leader>x" to multiple modes for the current buffer:
  24. --- vim.keymap.set({'n', 'v'}, '<leader>x', vim.lsp.buf.references, { buffer = true })
  25. --- -- Map <Tab> to an expression (|:map-<expr>|):
  26. --- vim.keymap.set('i', '<Tab>', function()
  27. --- return vim.fn.pumvisible() == 1 and "<C-n>" or "<Tab>"
  28. --- end, { expr = true })
  29. --- -- Map "[%%" to a <Plug> mapping:
  30. --- vim.keymap.set('n', '[%%', '<Plug>(MatchitNormalMultiBackward)')
  31. --- ```
  32. ---
  33. ---@param mode string|string[] Mode "short-name" (see |nvim_set_keymap()|), or a list thereof.
  34. ---@param lhs string Left-hand side |{lhs}| of the mapping.
  35. ---@param rhs string|function Right-hand side |{rhs}| of the mapping, can be a Lua function.
  36. ---@param opts? vim.keymap.set.Opts
  37. ---
  38. ---@see |nvim_set_keymap()|
  39. ---@see |maparg()|
  40. ---@see |mapcheck()|
  41. ---@see |mapset()|
  42. function keymap.set(mode, lhs, rhs, opts)
  43. vim.validate('mode', mode, { 'string', 'table' })
  44. vim.validate('lhs', lhs, 'string')
  45. vim.validate('rhs', rhs, { 'string', 'function' })
  46. vim.validate('opts', opts, 'table', true)
  47. opts = vim.deepcopy(opts or {}, true)
  48. ---@cast mode string[]
  49. mode = type(mode) == 'string' and { mode } or mode
  50. if opts.expr and opts.replace_keycodes ~= false then
  51. opts.replace_keycodes = true
  52. end
  53. if opts.remap == nil then
  54. -- default remap value is false
  55. opts.noremap = true
  56. else
  57. -- remaps behavior is opposite of noremap option.
  58. opts.noremap = not opts.remap
  59. opts.remap = nil ---@type boolean?
  60. end
  61. if type(rhs) == 'function' then
  62. opts.callback = rhs
  63. rhs = ''
  64. end
  65. if opts.buffer then
  66. local bufnr = opts.buffer == true and 0 or opts.buffer --[[@as integer]]
  67. opts.buffer = nil ---@type integer?
  68. for _, m in ipairs(mode) do
  69. vim.api.nvim_buf_set_keymap(bufnr, m, lhs, rhs, opts)
  70. end
  71. else
  72. opts.buffer = nil
  73. for _, m in ipairs(mode) do
  74. vim.api.nvim_set_keymap(m, lhs, rhs, opts)
  75. end
  76. end
  77. end
  78. --- @class vim.keymap.del.Opts
  79. --- @inlinedoc
  80. ---
  81. --- Remove a mapping from the given buffer.
  82. --- When `0` or `true`, use the current buffer.
  83. --- @field buffer? integer|boolean
  84. --- Remove an existing mapping.
  85. --- Examples:
  86. ---
  87. --- ```lua
  88. --- vim.keymap.del('n', 'lhs')
  89. ---
  90. --- vim.keymap.del({'n', 'i', 'v'}, '<leader>w', { buffer = 5 })
  91. --- ```
  92. ---
  93. ---@param modes string|string[]
  94. ---@param lhs string
  95. ---@param opts? vim.keymap.del.Opts
  96. ---@see |vim.keymap.set()|
  97. function keymap.del(modes, lhs, opts)
  98. vim.validate('mode', modes, { 'string', 'table' })
  99. vim.validate('lhs', lhs, 'string')
  100. vim.validate('opts', opts, 'table', true)
  101. opts = opts or {}
  102. modes = type(modes) == 'string' and { modes } or modes
  103. --- @cast modes string[]
  104. local buffer = false ---@type false|integer
  105. if opts.buffer ~= nil then
  106. buffer = opts.buffer == true and 0 or opts.buffer --[[@as integer]]
  107. end
  108. if buffer == false then
  109. for _, mode in ipairs(modes) do
  110. vim.api.nvim_del_keymap(mode, lhs)
  111. end
  112. else
  113. for _, mode in ipairs(modes) do
  114. vim.api.nvim_buf_del_keymap(buffer, mode, lhs)
  115. end
  116. end
  117. end
  118. return keymap