Skip to content

Commit 67ed2bd

Browse files
committed
allow to open external files in new tab
1 parent 59775af commit 67ed2bd

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

lua/neorg/modules/core/dirman/utils/module.lua

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,34 @@ module.public = {
7878
---Call attempt to edit a file, catches and suppresses the error caused by a swap file being
7979
---present. Re-raises other errors via log.error
8080
---@param path string
81-
edit_file = function(path)
82-
local ok, err = pcall(vim.cmd.edit, path)
81+
---@param opts table? Optional parameters
82+
---@param opts.mode string? The vim command to use for opening the file (default: "edit")
83+
--- - "edit" (default) - open in current window
84+
--- - "tabedit" - open in new tab
85+
--- - "vsplit" - open in vertical split
86+
--- - "split" - open in horizontal split
87+
--- - "tab-drop" - open in new tab or switch to existing tab
88+
--- - "drop" - switch to existing buffer or open in current window
89+
edit_file = function(path, opts)
90+
opts = opts or {}
91+
local mode = opts.mode or "edit"
92+
93+
local ok, err
94+
-- Special handling for tab-drop and drop commands
95+
if mode == "tab-drop" or mode == "drop" then
96+
local cmd = mode == "tab-drop" and "tab drop" or "drop"
97+
ok, err = pcall(vim.cmd, cmd .. " " .. vim.fn.fnameescape(path))
98+
else
99+
-- For edit, tabedit, vsplit, split
100+
ok, err = pcall(vim.cmd[mode], path)
101+
end
102+
83103
if not ok then
84104
-- Vim:E325 is the swap file error, in which case, a lengthy message already shows to
85105
-- the user, and we don't have to crash out of this function (which creates a long and
86106
-- misleading error message).
87107
if err and not err:match("Vim:E325") then
88-
log.error("Failed to edit file %s. Error:\n%s"):format(path, err)
108+
log.error(string.format("Failed to edit file %s. Error:\n%s", path, err))
89109
end
90110
end
91111
end,

lua/neorg/modules/core/esupports/hop/module.lua

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,9 +212,17 @@ module.public = {
212212

213213
-- If we're dealing with an external file, open it up in another Neovim buffer (unless otherwise applicable)
214214
external_file = function()
215-
open_split()
215+
-- Map open_mode to vim command mode
216+
local mode_map = {
217+
vsplit = "vsplit",
218+
split = "split",
219+
tab = "tabedit",
220+
["tab-drop"] = "tab-drop",
221+
drop = "drop",
222+
}
223+
local mode = open_mode and mode_map[open_mode] or "edit"
216224

217-
dirman_utils.edit_file(located_link_information.path)
225+
dirman_utils.edit_file(located_link_information.path, { mode = mode })
218226

219227
if located_link_information.line then
220228
jump_to_line(located_link_information.line)

0 commit comments

Comments
 (0)