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

View file

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