fix: escape special characters when editing buffer (#96)

This commit is contained in:
Steven Arcangeli 2023-05-05 08:32:44 -07:00
parent 37cb6be6f6
commit 339ade9dc3
2 changed files with 13 additions and 4 deletions

View file

@ -331,7 +331,7 @@ M.open_float = function(dir)
) )
end end
vim.cmd.edit({ args = { parent_url }, mods = { keepalt = true } }) vim.cmd.edit({ args = { util.escape_filename(parent_url) }, mods = { keepalt = true } })
if vim.fn.has("nvim-0.9") == 0 then if vim.fn.has("nvim-0.9") == 0 then
util.add_title_to_win(winid) util.add_title_to_win(winid)
@ -352,6 +352,7 @@ end
---Open oil browser for a directory ---Open oil browser for a directory
---@param dir nil|string When nil, open the parent of the current buffer, or the cwd if current buffer is not a file ---@param dir nil|string When nil, open the parent of the current buffer, or the cwd if current buffer is not a file
M.open = function(dir) M.open = function(dir)
local util = require("oil.util")
local view = require("oil.view") local view = require("oil.view")
local parent_url, basename = M.get_url_for_path(dir) local parent_url, basename = M.get_url_for_path(dir)
if not parent_url then if not parent_url then
@ -360,7 +361,7 @@ M.open = function(dir)
if basename then if basename then
view.set_last_cursor(parent_url, basename) view.set_last_cursor(parent_url, basename)
end end
vim.cmd.edit({ args = { parent_url }, mods = { keepalt = true } }) vim.cmd.edit({ args = { util.escape_filename(parent_url) }, mods = { keepalt = true } })
end end
---Restore the buffer that was present when oil was opened ---Restore the buffer that was present when oil was opened
@ -491,7 +492,7 @@ M.select = function(opts)
} }
if opts.preview and preview_win then if opts.preview and preview_win then
vim.api.nvim_set_current_win(preview_win) vim.api.nvim_set_current_win(preview_win)
vim.cmd.edit({ args = { url }, mods = mods }) vim.cmd.edit({ args = { util.escape_filename(url) }, mods = mods })
else else
if vim.tbl_isempty(mods) then if vim.tbl_isempty(mods) then
mods = nil mods = nil
@ -506,7 +507,7 @@ M.select = function(opts)
end end
vim.cmd({ vim.cmd({
cmd = cmd, cmd = cmd,
args = { url }, args = { util.escape_filename(url) },
mods = mods, mods = mods,
}) })
end end

View file

@ -8,6 +8,14 @@ M.parse_url = function(url)
return url:match("^(.*://)(.*)$") return url:match("^(.*://)(.*)$")
end end
---Escapes a filename for use in :edit
---@param filename string
---@return string
M.escape_filename = function(filename)
local ret = filename:gsub("([%%#])", "\\%1")
return ret
end
---@param bufnr integer ---@param bufnr integer
---@return nil|oil.Adapter ---@return nil|oil.Adapter
M.get_adapter = function(bufnr) M.get_adapter = function(bufnr)