WIP: properly escape paths in Windows

Still not working with $ (try a file named a$PATH.txt).
:edit a$PATH.txt will expand the variable
:edit a\$PATH.txt will treat it as a path separator, so a/$PATH.txt
This commit is contained in:
Steven Arcangeli 2023-11-04 08:43:03 -07:00
parent 2e6996b075
commit 0cff2447b5
2 changed files with 32 additions and 22 deletions

View file

@ -423,6 +423,7 @@ end
M.select = function(opts, callback)
local cache = require("oil.cache")
local config = require("oil.config")
local fs = require("oil.fs")
opts = vim.tbl_extend("keep", opts or {}, {})
local function finish(err)
if err then
@ -558,9 +559,11 @@ M.select = function(opts, callback)
end
end
print("url", url, "entry", entry.name)
-- Normalize the url before opening to prevent needing to rename them inside the BufReadCmd
-- Renaming buffers during opening can lead to missed autocmds
get_edit_path(function(normalized_url)
print("Normalized url", normalized_url)
local mods = {
vertical = opts.vertical,
horizontal = opts.horizontal,
@ -569,11 +572,14 @@ M.select = function(opts, callback)
emsg_silent = true,
}
-- If we're editing a file on disk, shorten the path prior to :edit so the
-- display name will show up shortened
if adapter.name == "files" and not util.parse_url(normalized_url) then
-- display name will show up shortened.
-- Don't do this on Windows, where ~/home/path.txt doesn't work
if adapter.name == "files" and not util.parse_url(normalized_url) and not fs.is_windows then
normalized_url = require("oil.fs").shorten_path(normalized_url)
print("Shortened", normalized_url)
end
local filename = util.escape_filename(normalized_url)
print("Escaped", filename)
-- If we're previewing a file that hasn't been opened yet, make sure it gets deleted after we
-- close the window

View file

@ -1,5 +1,6 @@
local config = require("oil.config")
local constants = require("oil.constants")
local fs = require("oil.fs")
local M = {}
@ -19,8 +20,11 @@ end
---@param filename string
---@return string
M.escape_filename = function(filename)
local ret = filename:gsub("([%%#$])", "\\%1")
return ret
if fs.is_windows then
filename = filename:gsub("\\", "/")
filename = filename:gsub("([$])", "\\%1")
end
return vim.fn.fnameescape(filename)
end
local _url_escape_chars = {