From c2c4626c3d3816586014b505c10b249cf2514005 Mon Sep 17 00:00:00 2001 From: TJ DeVries Date: Thu, 8 Oct 2020 21:39:43 -0400 Subject: feat: Add more easily customizable mappings. Closes: #131 --- lua/telescope/config.lua | 60 +++++++++++----------------------------- lua/telescope/mappings.lua | 69 ++++++++++++++++++++++++++++++++++++++++++---- lua/telescope/pickers.lua | 2 +- 3 files changed, 81 insertions(+), 50 deletions(-) (limited to 'lua') diff --git a/lua/telescope/config.lua b/lua/telescope/config.lua index 9e1ba25..82cd158 100644 --- a/lua/telescope/config.lua +++ b/lua/telescope/config.lua @@ -59,52 +59,24 @@ function config.set_defaults(defaults) -- Last argument will be the search term (passed in during execution) set("vimgrep_arguments", {'rg', '--color=never', '--no-heading', '--with-filename', '--line-number', '--column', '--smart-case'}) - -- TODO: Shortenpath - -- Decide how to propagate that to all the opts everywhere. - -- TODO: Add motions to keybindings - -- TODO: Add relative line numbers? - set("default_mappings", { - i = { - [""] = actions.move_selection_next, - [""] = actions.move_selection_previous, - - [""] = actions.close, - - [""] = actions.move_selection_next, - [""] = actions.move_selection_previous, - - [""] = actions.goto_file_selection_edit, - [""] = actions.goto_file_selection_split, - [""] = actions.goto_file_selection_vsplit, - [""] = actions.goto_file_selection_tabedit, - - [""] = actions.preview_scrolling_up, - [""] = actions.preview_scrolling_down, - - -- TODO: When we implement multi-select, you can turn this back on :) - -- [""] = actions.add_selection, - }, - - n = { - [""] = actions.close, - [""] = actions.goto_file_selection_edit, - [""] = actions.goto_file_selection_split, - [""] = actions.goto_file_selection_vsplit, - [""] = actions.goto_file_selection_tabedit, - - -- TODO: This would be weird if we switch the ordering. - ["j"] = actions.move_selection_next, - ["k"] = actions.move_selection_previous, - - [""] = actions.move_selection_next, - [""] = actions.move_selection_previous, - - [""] = actions.preview_scrolling_up, - [""] = actions.preview_scrolling_down, - }, - }) + -- To disable a keymap, put [map] = false + -- So, to not map "", just put + -- + -- ..., + -- [""] = false, + -- ..., + -- + -- Into your config. + -- + -- Otherwise, just set the mapping to the function that you want it to be. + -- + -- ..., + -- [""] = actions.goto_file_selection_split + -- ..., + -- + set("mappings", {}) -- NOT STABLE. DO NOT USE set("horizontal_config", { diff --git a/lua/telescope/mappings.lua b/lua/telescope/mappings.lua index 8d9dd65..5abe098 100644 --- a/lua/telescope/mappings.lua +++ b/lua/telescope/mappings.lua @@ -1,8 +1,51 @@ -- TODO: Customize keymap local a = vim.api +local actions = require('telescope.actions') + local mappings = {} +mappings.default_mappings = { + i = { + [""] = actions.move_selection_next, + [""] = actions.move_selection_previous, + + [""] = actions.close, + + [""] = actions.move_selection_next, + [""] = actions.move_selection_previous, + + [""] = actions.goto_file_selection_edit, + [""] = actions.goto_file_selection_split, + [""] = actions.goto_file_selection_vsplit, + [""] = actions.goto_file_selection_tabedit, + + [""] = actions.preview_scrolling_up, + [""] = actions.preview_scrolling_down, + + -- TODO: When we implement multi-select, you can turn this back on :) + -- [""] = actions.add_selection, + }, + + n = { + [""] = actions.close, + [""] = actions.goto_file_selection_edit, + [""] = actions.goto_file_selection_split, + [""] = actions.goto_file_selection_vsplit, + [""] = actions.goto_file_selection_tabedit, + + -- TODO: This would be weird if we switch the ordering. + ["j"] = actions.move_selection_next, + ["k"] = actions.move_selection_previous, + + [""] = actions.move_selection_next, + [""] = actions.move_selection_previous, + + [""] = actions.preview_scrolling_up, + [""] = actions.preview_scrolling_down, + }, + } + local keymap_store = setmetatable({}, { __index = function(t, k) rawset(t, k, {}) @@ -46,6 +89,10 @@ mappings.apply_keymap(42, , { }) --]] local telescope_map = function(prompt_bufnr, mode, key_bind, key_func, opts) + if not key_func then + return + end + opts = opts or { silent = true } @@ -98,7 +145,7 @@ mappings.apply_keymap = function(prompt_bufnr, attach_mappings, buffer_keymap) local applied_mappings = { n = {}, i = {} } local map = function(mode, key_bind, key_func, opts) - local mode = string.lower(mode) + mode = string.lower(mode) local key_bind_internal = a.nvim_replace_termcodes(key_bind, true, true, true) applied_mappings[mode][key_bind_internal] = true @@ -109,14 +156,26 @@ mappings.apply_keymap = function(prompt_bufnr, attach_mappings, buffer_keymap) return end - for mode, mode_map in pairs(buffer_keymap) do - local mode = string.lower(mode) - -- TODO: Probalby should not overwrite any keymaps - -- local buffer_keymaps + for mode, mode_map in pairs(buffer_keymap or {}) do + mode = string.lower(mode) + + for key_bind, key_func in pairs(mode_map) do + local key_bind_internal = a.nvim_replace_termcodes(key_bind, true, true, true) + if not applied_mappings[mode][key_bind_internal] then + applied_mappings[mode][key_bind_internal] = true + telescope_map(prompt_bufnr, mode, key_bind, key_func) + end + end + end + + -- TODO: Probalby should not overwrite any keymaps + for mode, mode_map in pairs(mappings.default_mappings) do + mode = string.lower(mode) for key_bind, key_func in pairs(mode_map) do local key_bind_internal = a.nvim_replace_termcodes(key_bind, true, true, true) if not applied_mappings[mode][key_bind_internal] then + applied_mappings[mode][key_bind_internal] = true telescope_map(prompt_bufnr, mode, key_bind, key_func) end end diff --git a/lua/telescope/pickers.lua b/lua/telescope/pickers.lua index 9c55e1b..f28ae6a 100644 --- a/lua/telescope/pickers.lua +++ b/lua/telescope/pickers.lua @@ -526,7 +526,7 @@ function Picker:find() picker = self, }, { __mode = 'kv' })) - mappings.apply_keymap(prompt_bufnr, self.attach_mappings, config.values.default_mappings) + mappings.apply_keymap(prompt_bufnr, self.attach_mappings, config.values.mappings) -- Do filetype last, so that users can register at the last second. pcall(a.nvim_buf_set_option, prompt_bufnr, 'filetype', 'TelescopePrompt') -- cgit v1.2.3