aboutsummaryrefslogtreecommitdiff
path: root/neovim/.config/nvim/lua/dyamon/util/map.lua
blob: 4e6608cc55483d07d550a68b2ae58fcaf7aa1949 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
--- Personal lua config files for NeoVim.
--  Last Changed: 2019-01-27
--  Author: Federico Igne <git@federicoigne.com>
--  License: This file is placed in the public domain.

local api = vim.api

--- A collection of helper functions to manipulate mappings
local m = { nore = { }, b = { nore = { } }, un = { b = { } } }

local function map(bufnr, mode, keys, repl, opts)
  local opts = opts or { }
  if bufnr then 
    api.nvim_buf_set_keymap(bufnr, mode, keys, repl, opts)
  else
    api.nvim_set_keymap(mode, keys, repl, opts)
  end
end

local function unmap(bufnr, mode, keys)
  if bufnr then 
    api.nvim_buf_del_keymap(bufnr, mode, keys)
  else
    api.nvim_del_keymap(mode, keys)
  end
end

local function noremap(bufnr, mode, keys, repl, opts)
  local opts = opts or { }
  opts.noremap = true
  map(bufnr, mode, keys, repl, opts)
end

for _, mode in pairs({'n', 'i', 'v', 'x', 't'}) do
  m[mode] = function(keys, repl, opts) map(nil, mode, keys, repl, opts) end
  m.nore[mode] = function(keys, repl, opts) noremap(nil, mode, keys, repl, opts) end
  m.b[mode] = function(bufnr, keys, repl, opts) map(bufnr, mode, keys, repl, opts) end
  m.b.nore[mode] = function(bufnr, keys, repl, opts) noremap(bufnr, mode, keys, repl, opts) end
  m.un[mode] = function(keys) unmap(nil, mode, keys) end
  m.un.b[mode] = function(bufnr, keys) unmap(bufnr, mode, keys) end
end

return m