robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1local 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` for current buffer. 13--- @field buf? integer 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. If `lhs` is a list, defines 20--- a mapping for each (mode, lhs) pair. 21--- 22--- Examples: 23--- 24--- ```lua 25--- -- Map "x" to a Lua function: 26--- vim.keymap.set('n', 'x', function() print('real lua function') end) 27--- -- Map "<leader>x" to multiple modes for the current buffer: 28--- vim.keymap.set({'n', 'v'}, '<leader>x', vim.lsp.buf.references, { buf = 0 }) 29--- -- Map <Tab> to an expression (|:map-<expr>|): 30--- vim.keymap.set('i', '<Tab>', function() 31--- return vim.fn.pumvisible() == 1 and '<C-n>' or '<Tab>' 32--- end, { expr = true }) 33--- -- Map "[%%" to a <Plug> mapping: 34--- vim.keymap.set('n', '[%%', '<Plug>(MatchitNormalMultiBackward)') 35--- 36--- -- Use `getregionpos(getpos('v'))` to get the "current visual selection": 37--- vim.keymap.set('x', 'M', function() 38--- local region = vim.fn.getregionpos(vim.fn.getpos('v'), vim.fn.getpos('.'), { 39--- type = 'v', 40--- exclusive = false, 41--- eol = false, 42--- bounds = true, 43--- }) 44--- local line1 = region[1][1][2] 45--- local line2 = region[1][2][2] 46--- vim.print({ line1, line2 }) 47--- end) 48--- 49--- vim.keymap.set({ 'n', 'i' }, { 'a', 'b' }, '<cmd>echom localtime()<cr>') 50--- -- ... is the same as: 51--- vim.keymap.set('n', 'a', '<cmd>echom localtime()<cr>') 52--- vim.keymap.set('i', 'a', '<cmd>echom localtime()<cr>') 53--- vim.keymap.set('n', 'b', '<cmd>echom localtime()<cr>') 54--- vim.keymap.set('i', 'b', '<cmd>echom localtime()<cr>') 55--- ``` 56--- 57---@param modes string|string[] Mode "short-name" (see |nvim_set_keymap()|), or a list thereof. 58---@param lhs string|string[] Left-hand side |{lhs}| of the mapping, or a list thereof. 59---@param rhs string|function Right-hand side |{rhs}| of the mapping, can be a Lua function. 60---@param opts? vim.keymap.set.Opts 61--- 62---@see |nvim_set_keymap()| 63---@see |maparg()| 64---@see |mapcheck()| 65---@see |mapset()| 66function keymap . set ( modes , lhs , rhs , opts ) 67vim . validate ( 'modes' , modes , { 'string' , 'table' } ) 68vim . validate ( 'lhs' , lhs , { 'string' , 'table' } ) 69vim . validate ( 'rhs' , rhs , { 'string' , 'function' } ) 70vim . validate ( 'opts' , opts , 'table' , true ) 71 72opts =vim . deepcopy ( opts or {} , true ) 73 74modes =type ( modes ) == 'string' and { modes } or modes 75---@cast modes string[] 76lhs =type ( lhs ) == 'string' and { lhs } or lhs 77---@cast lhs string[] 78 79if opts . expr and opts . replace_keycodes ~= false then 80opts . replace_keycodes =true 81end 82 83if opts . remap == nil then 84-- default remap value is false 85opts . noremap =true 86else 87-- remaps behavior is opposite of noremap option. 88opts . noremap =not opts . remap 89opts . remap =nil ---@type boolean? 90end 91 92if type ( rhs ) == 'function' then 93opts . callback =rhs 94rhs ='' 95end 96 97local buf =opts . buf 98opts . buf =nil 99--- @cast opts +{buffer?:integer|boolean} 100if opts . buffer ~= nil then 101-- TODO(skewb1k): soft-deprecate `buffer` option in 0.13, remove in 0.15. 102assert ( buf == nil , "Conflict: 'buf' not allowed with 'buffer'" ) 103buf =opts . buffer == true and 0 or opts . buffer --[[@as integer?]] 104opts . buffer =nil 105end 106 107for _ , m in ipairs ( modes ) do 108for _ , l in ipairs ( lhs ) do 109if buf then 110vim . api . nvim_buf_set_keymap ( buf , m , l , rhs , opts ) 111else 112vim . api . nvim_set_keymap ( m , l , rhs , opts ) 113end 114end 115end 116end 117 118--- @class vim.keymap.del.Opts 119--- @inlinedoc 120--- 121--- Remove a mapping from the given buffer. `0` for current. 122--- @field buf? integer 123--- When true, only match {lhs}, not {rhs}. 124--- @field lhs? boolean 125 126--- Removes a mapping, or removes each (mode, lhs) pair if `lhs` is a list. 127--- 128--- Examples: 129--- 130--- ```lua 131--- vim.keymap.del('n', 'lhs') 132--- vim.keymap.del({'n', 'i', 'v'}, '<leader>w', { buf = 5 }) 133--- ``` 134--- 135---@param modes string|string[] 136---@param lhs string|string[] 137---@param opts? vim.keymap.del.Opts 138---@see |vim.keymap.set()| 139function keymap . del ( modes , lhs , opts ) 140vim . validate ( 'modes' , modes , { 'string' , 'table' } ) 141vim . validate ( 'lhs' , lhs , { 'string' , 'table' } ) 142vim . validate ( 'opts' , opts , 'table' , true ) 143 144opts =opts or {} 145 146modes =type ( modes ) == 'string' and { modes } or modes 147--- @cast modes string[] 148lhs =type ( lhs ) == 'string' and { lhs } or lhs 149---@cast lhs string[] 150 151local buf =opts . buf 152--- @cast opts +{buffer?:integer|boolean} 153if opts . buffer ~= nil then 154-- TODO(skewb1k): soft-deprecate `buffer` option in 0.13, remove in 0.15. 155assert ( opts . buf == nil , "Conflict: 'buf' not allowed with 'buffer'" ) 156buf =opts . buffer == true and 0 or opts . buffer --[[@as integer?]] 157end 158 159local api_opts ={ lhs =opts . lhs } 160 161for _ , m in ipairs ( modes ) do 162for _ , l in ipairs ( lhs ) do 163if buf then 164vim . api . nvim_buf_del_keymap ( buf , m , l , api_opts ) 165else 166vim . api . nvim_del_keymap ( m , l , api_opts ) 167end 168end 169end 170end 171 172return keymap