aboutsummaryrefslogtreecommitdiff
path: root/runtime
diff options
context:
space:
mode:
Diffstat (limited to 'runtime')
-rw-r--r--runtime/doc/plugins.txt14
-rw-r--r--runtime/lua/nvim/dir.lua48
-rw-r--r--runtime/lua/nvim/dir/fs.lua9
3 files changed, 47 insertions, 24 deletions
diff --git a/runtime/doc/plugins.txt b/runtime/doc/plugins.txt
index 9f10625251..5a68e732a6 100644
--- a/runtime/doc/plugins.txt
+++ b/runtime/doc/plugins.txt
@@ -70,13 +70,19 @@ GLOBAL MAPPINGS *dir-mappings*
BUFFER-LOCAL MAPPINGS *dir-buffer-mappings*
• <CR> opens the file or directory under the cursor.
+• a opens the file or directory under the cursor in a vertical split.
+• o opens the file or directory under the cursor in a horizontal split.
• - opens the parent directory.
• R reloads the directory listing.
-These keys map to <Plug>(nvim-dir-open), <Plug>(nvim-dir-up), and
-<Plug>(nvim-dir-reload); map those to use different keys. Default mappings are
-skipped when the target <Plug> mapping is already mapped. The default global and
-directory-buffer "-" mappings are also skipped when "-" is already mapped.
+a and o are available for local directories and keep focus in the listing.
+'splitright' and 'splitbelow' control where the new window appears.
+
+These keys map to <Plug>(nvim-dir-open), <Plug>(nvim-dir-vsplit),
+<Plug>(nvim-dir-split), <Plug>(nvim-dir-up), and <Plug>(nvim-dir-reload); map those
+to use different keys. Default mappings are skipped when the target <Plug>
+mapping is already mapped. The default "-", "a", and "o" mappings are also
+skipped when their keys are already mapped.
*dir-config*
Directory buffers follow the global 'hidden' option by default. To delete them
diff --git a/runtime/lua/nvim/dir.lua b/runtime/lua/nvim/dir.lua
index ccce98e98c..c72d4d3b27 100644
--- a/runtime/lua/nvim/dir.lua
+++ b/runtime/lua/nvim/dir.lua
@@ -167,24 +167,15 @@ local function reload(buf, provider)
end
---@param buf integer
-local function set_maps(buf)
- ---@param lhs string
- ---@param plug string
- local function map(lhs, plug)
- if vim.fn.hasmapto(plug, 'n') == 0 then
- vim.keymap.set('n', lhs, plug, { buffer = buf, silent = true })
- end
- end
- ---@param lhs string
- ---@param plug string
- local function default_map(lhs, plug)
- if vim.fn.mapcheck(lhs, 'n') == '' and vim.fn.hasmapto(plug, 'n') == 0 then
+---@param lhs string
+---@param plug string
+---@param force? boolean
+function M._map(buf, lhs, plug, force)
+ api.nvim_buf_call(buf, function()
+ if (force or vim.fn.mapcheck(lhs, 'n') == '') and vim.fn.hasmapto(plug, 'n') == 0 then
vim.keymap.set('n', lhs, plug, { buffer = buf, silent = true })
end
- end
- map('<CR>', '<Plug>(nvim-dir-open)')
- default_map('-', '<Plug>(nvim-dir-up)')
- map('R', '<Plug>(nvim-dir-reload)')
+ end)
end
--- Let handlers reshape the rendered listing. Unlocks the buffer for the duration and
@@ -286,7 +277,9 @@ function load(buf, name, provider, restore_view, setup, select)
end
setup_render_autocmds(buf)
- set_maps(buf)
+ M._map(buf, '<CR>', '<Plug>(nvim-dir-open)', true)
+ M._map(buf, '-', '<Plug>(nvim-dir-up)')
+ M._map(buf, 'R', '<Plug>(nvim-dir-reload)', true)
if provider.init then
provider.init(buf, name)
end
@@ -326,12 +319,27 @@ local function get_provider(buf)
return state and state.provider or nil
end
-function M._open_entry()
+---@param cmd? 'split'|'vsplit'
+function M._open_entry(cmd)
local buf = api.nvim_get_current_buf()
local provider = get_provider(buf)
local entry = current_entry(buf)
- if provider and entry then
- provider.open(buf, api.nvim_buf_get_name(buf), entry)
+ if not provider or not entry then
+ return
+ end
+ local name = api.nvim_buf_get_name(buf)
+ local win = cmd and api.nvim_get_current_win()
+ local ok, err = pcall(function()
+ if cmd then
+ api.nvim_cmd({ cmd = cmd }, {})
+ end
+ provider.open(buf, name, entry)
+ end)
+ if win and api.nvim_win_is_valid(win) then
+ api.nvim_set_current_win(win)
+ end
+ if not ok then
+ error(err, 0)
end
end
diff --git a/runtime/lua/nvim/dir/fs.lua b/runtime/lua/nvim/dir/fs.lua
index 0af9d3229f..9ff95ce8ad 100644
--- a/runtime/lua/nvim/dir/fs.lua
+++ b/runtime/lua/nvim/dir/fs.lua
@@ -102,6 +102,15 @@ function M.init(buf, path)
if api.nvim_get_option_value('filetype', { buf = buf }) ~= 'directory' then
api.nvim_set_option_value('filetype', 'directory', { buf = buf })
end
+ local dir = require('nvim.dir')
+ vim.keymap.set('n', '<Plug>(nvim-dir-vsplit)', function()
+ dir._open_entry('vsplit')
+ end, { buffer = buf, silent = true, desc = 'Open directory entry in a vertical split' })
+ vim.keymap.set('n', '<Plug>(nvim-dir-split)', function()
+ dir._open_entry('split')
+ end, { buffer = buf, silent = true, desc = 'Open directory entry in a horizontal split' })
+ dir._map(buf, 'a', '<Plug>(nvim-dir-vsplit)')
+ dir._map(buf, 'o', '<Plug>(nvim-dir-split)')
api.nvim_buf_call(buf, function()
pcall(api.nvim_cmd, {
cmd = 'bcd',