Skip to content

Commit

Permalink
fix: escape special characters when editing buffer (#96)
Browse files Browse the repository at this point in the history
  • Loading branch information
stevearc committed May 5, 2023
1 parent 37cb6be commit 339ade9
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
9 changes: 5 additions & 4 deletions lua/oil/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ M.open_float = function(dir)
)
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
util.add_title_to_win(winid)
Expand All @@ -352,6 +352,7 @@ end
---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
M.open = function(dir)
local util = require("oil.util")
local view = require("oil.view")
local parent_url, basename = M.get_url_for_path(dir)
if not parent_url then
Expand All @@ -360,7 +361,7 @@ M.open = function(dir)
if basename then
view.set_last_cursor(parent_url, basename)
end
vim.cmd.edit({ args = { parent_url }, mods = { keepalt = true } })
vim.cmd.edit({ args = { util.escape_filename(parent_url) }, mods = { keepalt = true } })
end

---Restore the buffer that was present when oil was opened
Expand Down Expand Up @@ -491,7 +492,7 @@ M.select = function(opts)
}
if opts.preview and preview_win then
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
if vim.tbl_isempty(mods) then
mods = nil
Expand All @@ -506,7 +507,7 @@ M.select = function(opts)
end
vim.cmd({
cmd = cmd,
args = { url },
args = { util.escape_filename(url) },
mods = mods,
})
end
Expand Down
8 changes: 8 additions & 0 deletions lua/oil/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ M.parse_url = function(url)
return url:match("^(.*://)(.*)$")
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
---@return nil|oil.Adapter
M.get_adapter = function(bufnr)
Expand Down

0 comments on commit 339ade9

Please sign in to comment.