Skip to content

Instantly share code, notes, and snippets.

@shadmansaleh
Last active July 9, 2021 19:03
Show Gist options
  • Save shadmansaleh/4f7eba765b413502fcfea893f4cf00b0 to your computer and use it in GitHub Desktop.
Save shadmansaleh/4f7eba765b413502fcfea893f4cf00b0 to your computer and use it in GitHub Desktop.
-- Uses:
-- Semantic
-- require'keymap'.[?mode][?nore]map[?!]('key', 'command string | lua callback', 'patams' ...)
-- require'keymap'.nnoremap('<leader>a', function() print 'hello' end, 'silent')
-- require'keymap'.imap('<C-Space>', 'compe#complete()', 'silent', 'expr')
-- To unmap <C-Space> key
-- require'keymap'.imap('<C-Space>')
local ok, callbacks = pcall(require, 'lua_callbacks')
if not ok then
callbacks = setmetatable({register = {}}, {
__index = function(self, k) return self.register[k] end
})
package.loaded.lua_callbacks = callbacks
end
function callbacks.new(cb)
local len = #callbacks.register
table.insert(callbacks.register, cb)
return len + 1
end
local map = setmetatable({}, {
__index = function(self, index)
if not vim.endswith(index, 'map') and not vim.endswith(index, 'map!') then
return nil
end
local x = rawget(self, index)
if x ~= nil then return x end
local mode = index:sub(1,1)
local noremap = vim.endswith(index, 'noremap')
if index == 'map' or index == 'noremap' then mode = '' end
if index == 'map!' or index == 'noremap!' then mode = '!'; noremap = true end
local f = function(key, cmd, ...)
if not cmd or cmd == '' then
vim.api,nvim_del_keymap(mode, key)
retutn
end
local args = {...}
local opt = {}
for _, v in pairs(args) do opt[v] = true end
opt.noremap = (opt.noremap ~= nil)and opt.noremap or noremap
if type(cmd) == 'function' then
local id = callbacks.new(cmd)
cmd = opt.expr and
'luaeval("require(\'lua_callbacks\')['..id..']")()' or
'<cmd>lua require("lua_callbacks")['..id..']()<cr>'
end
vim.api.nvim_set_keymap(mode, key, cmd or '', opt)
end
rawset(self, index, f)
return f
end
})
return map
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment