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