Skip to content

Commit

Permalink
feat: keymaps can specify mode (#187)
Browse files Browse the repository at this point in the history
  • Loading branch information
stevearc committed Sep 28, 2023
1 parent 96a334a commit 977da9a
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 10 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.<name>",
-- it will use the mapping at require("oil.actions").<name>
-- Set to `false` to remove a keymap
Expand Down
2 changes: 1 addition & 1 deletion doc/oil.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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.<name>",
-- it will use the mapping at require("oil.actions").<name>
-- Set to `false` to remove a keymap
Expand Down
2 changes: 1 addition & 1 deletion lua/oil/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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.<name>",
-- it will use the mapping at require("oil.actions").<name>
-- Set to `false` to remove a keymap
Expand Down
2 changes: 1 addition & 1 deletion lua/oil/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
27 changes: 22 additions & 5 deletions lua/oil/keymap_util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string|table|fun()>
---@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<string, string|table|fun()>
M.show_help = function(keymaps)
local rhs_to_lhs = {}
local lhs_to_all_lhs = {}
Expand Down
2 changes: 1 addition & 1 deletion lua/oil/view.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 977da9a

Please sign in to comment.