diff --git a/README.md b/README.md index 10c8a1c..19d62a0 100644 --- a/README.md +++ b/README.md @@ -159,7 +159,7 @@ require("oil").setup({ -- Selecting a new/moved/renamed file or directory will prompt you to save changes first prompt_save_on_select_new_entry = true, -- Keymaps in oil buffer. Can be any value that `vim.keymap.set` accepts OR a table of keymap - -- options with a `callback` (e.g. { callback = function() ... end, desc = "", nowait = true }) + -- options with a `callback` (e.g. { callback = function() ... end, desc = "", mode = "n" }) -- Additionally, if it is a string that matches "actions.", -- it will use the mapping at require("oil.actions"). -- Set to `false` to remove a keymap diff --git a/doc/oil.txt b/doc/oil.txt index 17071e8..4414ea4 100644 --- a/doc/oil.txt +++ b/doc/oil.txt @@ -50,7 +50,7 @@ OPTIONS *oil-option -- Selecting a new/moved/renamed file or directory will prompt you to save changes first prompt_save_on_select_new_entry = true, -- Keymaps in oil buffer. Can be any value that `vim.keymap.set` accepts OR a table of keymap - -- options with a `callback` (e.g. { callback = function() ... end, desc = "", nowait = true }) + -- options with a `callback` (e.g. { callback = function() ... end, desc = "", mode = "n" }) -- Additionally, if it is a string that matches "actions.", -- it will use the mapping at require("oil.actions"). -- Set to `false` to remove a keymap diff --git a/lua/oil/config.lua b/lua/oil/config.lua index 60fc1b8..61fd4be 100644 --- a/lua/oil/config.lua +++ b/lua/oil/config.lua @@ -35,7 +35,7 @@ local default_config = { -- Selecting a new/moved/renamed file or directory will prompt you to save changes first prompt_save_on_select_new_entry = true, -- Keymaps in oil buffer. Can be any value that `vim.keymap.set` accepts OR a table of keymap - -- options with a `callback` (e.g. { callback = function() ... end, desc = "", nowait = true }) + -- options with a `callback` (e.g. { callback = function() ... end, desc = "", mode = "n" }) -- Additionally, if it is a string that matches "actions.", -- it will use the mapping at require("oil.actions"). -- Set to `false` to remove a keymap diff --git a/lua/oil/init.lua b/lua/oil/init.lua index 9ce7c12..6975b87 100644 --- a/lua/oil/init.lua +++ b/lua/oil/init.lua @@ -780,7 +780,7 @@ local function load_oil_buffer(bufnr) -- (e.g. ssh) because it will set up the filetype keybinds at the *beginning* of the loading -- process. vim.bo[bufnr].filetype = "oil" - keymap_util.set_keymaps("", config.keymaps, bufnr) + keymap_util.set_keymaps(config.keymaps, bufnr) end loading.set_loading(bufnr, true) local winid = vim.api.nvim_get_current_win() diff --git a/lua/oil/keymap_util.lua b/lua/oil/keymap_util.lua index 42d09d7..5f8f321 100644 --- a/lua/oil/keymap_util.lua +++ b/lua/oil/keymap_util.lua @@ -3,26 +3,43 @@ local layout = require("oil.layout") local util = require("oil.util") local M = {} +---@param rhs string|table|fun() +---@return string|fun() rhs +---@return table opts +---@return string|nil mode local function resolve(rhs) if type(rhs) == "string" and vim.startswith(rhs, "actions.") then return resolve(actions[vim.split(rhs, ".", { plain = true })[2]]) elseif type(rhs) == "table" then local opts = vim.deepcopy(rhs) + local callback = opts.callback + local mode = opts.mode + if type(rhs.callback) == "string" then + local action_opts, action_mode + callback, action_opts, action_mode = resolve(rhs.callback) + opts = vim.tbl_extend("keep", opts, action_opts) + mode = mode or action_mode + end opts.callback = nil - return rhs.callback, opts + opts.mode = nil + return callback, opts, mode + else + return rhs, {} end - return rhs, {} end -M.set_keymaps = function(mode, keymaps, bufnr) +---@param keymaps table +---@param bufnr integer +M.set_keymaps = function(keymaps, bufnr) for k, v in pairs(keymaps) do - local rhs, opts = resolve(v) + local rhs, opts, mode = resolve(v) if rhs then - vim.keymap.set(mode, k, rhs, vim.tbl_extend("keep", { buffer = bufnr }, opts)) + vim.keymap.set(mode or "", k, rhs, vim.tbl_extend("keep", { buffer = bufnr }, opts)) end end end +---@param keymaps table M.show_help = function(keymaps) local rhs_to_lhs = {} local lhs_to_all_lhs = {} diff --git a/lua/oil/view.lua b/lua/oil/view.lua index 751fc60..150bd64 100644 --- a/lua/oil/view.lua +++ b/lua/oil/view.lua @@ -357,7 +357,7 @@ M.initialize = function(bufnr) ) end end) - keymap_util.set_keymaps("", config.keymaps, bufnr) + keymap_util.set_keymaps(config.keymaps, bufnr) end ---@param adapter oil.Adapter