···11-local function tbl_filter(func, t)
22- vim.validate{func={func,'c'},t={t,'t'}}
33-44- local rettab = {}
55- for key, entry in pairs(t) do
66- if func(key, entry) then
77- rettab[key] = entry
88- end
99- end
1010- return rettab
1111-end
1212-1313-local function normalise_map(rhs, opts)
1414- -- If it is command line map, then automaticall add <C-u> for cleaning
1515- -- selection and <CR> at the end, to fire it up
1616- if vim.startswith(rhs, ':') and rhs ~= ':' then
1717- if not vim.startswith(rhs, ':<C-u>') and options["selection"] then
1818- rhs = '<cmd>' .. rhs:sub(2)
1919- end
2020- if not vim.endswith(rhs, '<CR>') and options["cr"] then
2121- rhs = rhs .. '<CR>'
2222- end
2323- end
2424-2525- return rhs
2626-end
2727-2828-local function do_map(cb)
2929- return function(modes, lhs, rhs, opts)
3030- -- Defaults to non recursive mappings
3131- options = vim.tbl_extend('force', {noremap = true, selection = true, cr = true}, opts or {})
3232-3333- if modes == '' then
3434- error('Modes must not be empty')
3535- end
3636-3737- local f = function(k, _)
3838- return not (k == "selection" or k == "cr")
3939- end
4040- local map_opts = tbl_filter(f, options)
4141- local normalised = normalise_map(rhs, options)
4242-4343- for mode in modes:gmatch('.') do
4444- cb(mode, lhs, normalised, map_opts)
4545- end
4646- end
4747-end
4848-4949-return {
5050- map = do_map(vim.api.nvim_set_keymap),
5151- buf_map = do_map(function(...) vim.api.nvim_buf_set_keymap(0, ...) end)
5252-}