diff options
| author | TJ DeVries <devries.timothyj@gmail.com> | 2020-09-02 00:06:03 -0400 |
|---|---|---|
| committer | TJ DeVries <devries.timothyj@gmail.com> | 2020-09-02 00:06:07 -0400 |
| commit | 061307233cdff0a90504117dd48e4fec3a10443a (patch) | |
| tree | d71cd84a3eb93fda55284b22be3eb610aaced13a /lua/telescope/mappings.lua | |
| parent | 9f906f03922907b95b71a8f57254e64ba9c76ce8 (diff) | |
feat: add some new items and make mappings easier
Diffstat (limited to 'lua/telescope/mappings.lua')
| -rw-r--r-- | lua/telescope/mappings.lua | 104 |
1 files changed, 69 insertions, 35 deletions
diff --git a/lua/telescope/mappings.lua b/lua/telescope/mappings.lua index ba618d4..6c8641c 100644 --- a/lua/telescope/mappings.lua +++ b/lua/telescope/mappings.lua @@ -29,14 +29,14 @@ end --[[ Usage: -mappings.apply_keymap(42, { +mappings.apply_keymap(42, <function>, { n = { ["<leader>x"] = "just do this string", ["<CR>"] = function(picker, prompt_bufnr) actions.close_prompt() - local filename = ... +> local filename = ... vim.cmd(string.format(":e %s", filename)) end, }, @@ -45,41 +45,75 @@ mappings.apply_keymap(42, { } }) --]] -mappings.apply_keymap = function(prompt_bufnr, buffer_keymap) +local telescope_map = function(prompt_bufnr, mode, key_bind, key_func, opts) + opts = opts or { + silent = true + } + + if type(key_func) == "string" then + a.nvim_buf_set_keymap( + prompt_bufnr, + mode, + key_bind, + key_func, + opts or { + silent = true + } + ) + else + local key_id = assign_function(prompt_bufnr, key_func) + local prefix = "" + + local map_string + if opts.expr then + map_string = string.format( + [[luaeval("require('telescope.mappings').execute_keymap(%s, %s)")]], + prompt_bufnr, + key_id + ) + else + if mode == "i" and not opts.expr then + prefix = "<C-O>" + end + + map_string = string.format( + "%s:lua require('telescope.mappings').execute_keymap(%s, %s)<CR>", + prefix, + prompt_bufnr, + key_id + ) + end + + a.nvim_buf_set_keymap( + prompt_bufnr, + mode, + key_bind, + map_string, + opts + ) + end +end + +mappings.apply_keymap = function(prompt_bufnr, attach_mappings, buffer_keymap) + local applied_mappings = { n = {}, i = {} } + + local map = function(mode, key_bind, key_func, opts) + applied_mappings[mode][key_bind] = true + + telescope_map(prompt_bufnr, mode, key_bind, key_func, opts) + end + + if attach_mappings and not attach_mappings(map) then + return + end + for mode, mode_map in pairs(buffer_keymap) do - for key_bind, key_func in pairs(mode_map) do - if type(key_func) == "string" then - a.nvim_buf_set_keymap( - prompt_bufnr, - mode, - key_bind, - key_func, - { - silent = true - } - ) - else - local key_id = assign_function(prompt_bufnr, key_func) - local prefix = "" - if mode == "i" then - prefix = "<C-O>" - end - - a.nvim_buf_set_keymap( - prompt_bufnr, - mode, - key_bind, - string.format( - "%s:lua require('telescope.mappings').execute_keymap(%s, %s)<CR>", - prefix, - prompt_bufnr, - key_id - ), - { - silent = true - } - ) + -- TODO: Probalby should not overwrite any keymaps + -- local buffer_keymaps + for key_bind, key_func in pairs(mode_map) do + if not applied_mappings[mode][key_bind] then + telescope_map(prompt_bufnr, mode, key_bind, key_func) end end end |
