diff --git a/lua/plenary/curl.lua b/lua/plenary/curl.lua index 7f4eef8c..2a052b77 100644 --- a/lua/plenary/curl.lua +++ b/lua/plenary/curl.lua @@ -32,6 +32,7 @@ local util, parse, request = {}, {}, nil local F = require "plenary.functional" local J = require "plenary.job" local P = require "plenary.path" +local S = require "plenary.system" -- Utils ---------------------------------------------------- ------------------------------------------------------------- @@ -70,7 +71,7 @@ util.gen_dump_path = function() local v = (l == "x") and math.random(0, 0xf) or math.random(0, 0xb) return string.format("%x", v) end) - if P.path.sep == "\\" then + if S.is_windows() then path = string.format("%s\\AppData\\Local\\Temp\\plenary_curl_%s.headers", os.getenv "USERPROFILE", id) else path = "/tmp/plenary_curl_" .. id .. ".headers" diff --git a/lua/plenary/path.lua b/lua/plenary/path.lua index f92b26e7..a54692db 100644 --- a/lua/plenary/path.lua +++ b/lua/plenary/path.lua @@ -7,6 +7,7 @@ local bit = require "plenary.bit" local uv = vim.loop local F = require "plenary.functional" +local S = require "plenary.system" local S_IF = { -- S_IFDIR = 0o040000 # directory @@ -20,8 +21,7 @@ path.home = vim.loop.os_homedir() path.sep = (function() if jit then - local os = string.lower(jit.os) - if os ~= "windows" then + if not S.is_windows() or S.uses_shellslash() then return "/" else return "\\" @@ -32,7 +32,7 @@ path.sep = (function() end)() path.root = (function() - if path.sep == "/" then + if not S.is_windows() or S.uses_shellslash then return function() return "/" end @@ -55,7 +55,7 @@ local concat_paths = function(...) end local function is_root(pathname) - if path.sep == "\\" then + if S.is_windows() then return string.match(pathname, "^[A-Z]:\\?$") end return pathname == "/" @@ -77,7 +77,7 @@ local is_uri = function(filename) end local is_absolute = function(filename, sep) - if sep == "\\" then + if S.is_windows() then return string.match(filename, "^[%a]:\\.*$") ~= nil end return string.sub(filename, 1, 1) == sep @@ -103,7 +103,7 @@ local function _normalize_path(filename, cwd) local split_without_disk_name = function(filename_local) local parts = _split_by_separator(filename_local) -- Remove disk name part on Windows - if path.sep == "\\" and is_abs then + if S.is_windows() and is_abs then table.remove(parts, 1) end return parts @@ -431,7 +431,7 @@ local function shorten_len(filename, len, exclude) end local shorten = (function() - if jit and path.sep ~= "\\" then + if jit and not S.is_windows() then local ffi = require "ffi" ffi.cdef [[ typedef unsigned char char_u; diff --git a/lua/plenary/scandir.lua b/lua/plenary/scandir.lua index 16e4ed54..80850408 100644 --- a/lua/plenary/scandir.lua +++ b/lua/plenary/scandir.lua @@ -1,6 +1,7 @@ local Path = require "plenary.path" local os_sep = Path.path.sep local F = require "plenary.functional" +local S = require "plenary.system" local uv = vim.loop @@ -319,7 +320,7 @@ local gen_date = (function() end)() local get_username = (function() - if jit and os_sep ~= "\\" then + if jit and not S.is_windows() then local ffi = require "ffi" ffi.cdef [[ typedef unsigned int __uid_t; @@ -369,7 +370,7 @@ local get_username = (function() end)() local get_groupname = (function() - if jit and os_sep ~= "\\" then + if jit and not S.is_windows() then local ffi = require "ffi" ffi.cdef [[ typedef unsigned int __gid_t; @@ -446,8 +447,8 @@ local gen_ls = function(data, path, opts) local results, sections = {}, {} - local users_tbl = os_sep ~= "\\" and {} or nil - local groups_tbl = os_sep ~= "\\" and {} or nil + local users_tbl = not S.is_windows() and {} or nil + local groups_tbl = not S.is_windows() and {} or nil local stats, permissions_cache = {}, {} for _, v in ipairs(data) do diff --git a/lua/plenary/strings.lua b/lua/plenary/strings.lua index bc374739..3b1828c2 100644 --- a/lua/plenary/strings.lua +++ b/lua/plenary/strings.lua @@ -1,9 +1,10 @@ local path = require("plenary.path").path +local S = require "plenary.system" local M = {} M.strdisplaywidth = (function() - if jit and path.sep ~= [[\]] then + if jit and not S.is_windows() then local ffi = require "ffi" ffi.cdef [[ typedef unsigned char char_u; @@ -29,7 +30,7 @@ M.strdisplaywidth = (function() end)() M.strcharpart = (function() - if jit and path.sep ~= [[\]] then + if jit and not S.is_windows() then local ffi = require "ffi" ffi.cdef [[ typedef unsigned char char_u; diff --git a/lua/plenary/system.lua b/lua/plenary/system.lua new file mode 100644 index 00000000..e04b0cf2 --- /dev/null +++ b/lua/plenary/system.lua @@ -0,0 +1,22 @@ +local system = {} + +function system.is_windows() + -- The shellslash option is not able to change this variable + if package.config:sub(1,1) == "\\" then + return true + end + + return false +end + +function system.uses_shellslash() + local shellslash_exists = vim.fn.exists("+shellslash") ~= 0 + + if shellslash_exists then + return vim.o.shellslash + end + + return false +end + +return system diff --git a/tests/plenary/path_spec.lua b/tests/plenary/path_spec.lua index f5a90972..f64cab61 100644 --- a/tests/plenary/path_spec.lua +++ b/tests/plenary/path_spec.lua @@ -1,5 +1,6 @@ local Path = require "plenary.path" local path = Path.path +local System = require "plenary.system" describe("Path", function() it("should find valid files", function() @@ -104,7 +105,7 @@ describe("Path", function() end) it("can take absolute paths and make them relative to a given path", function() - local root = path.sep == "\\" and "c:\\" or "/" + local root = System.is_windows() and "c:\\" or "/" local r = Path:new { root, "home", "prime" } local p = Path:new { "aoeu", "agen.lua" } local absolute = r.filename .. path.sep .. p.filename @@ -120,7 +121,7 @@ describe("Path", function() end) it("can take double separator absolute paths and make them relative to a given path", function() - local root = path.sep == "\\" and "c:\\" or "/" + local root = System.is_windows() and "c:\\" or "/" local r = Path:new { root, "home", "prime" } local p = Path:new { "aoeu", "agen.lua" } local absolute = r.filename .. path.sep .. path.sep .. p.filename @@ -129,7 +130,7 @@ describe("Path", function() end) it("can take absolute paths and make them relative to a given path with trailing separator", function() - local root = path.sep == "\\" and "c:\\" or "/" + local root = System.is_windows() and "c:\\" or "/" local r = Path:new { root, "home", "prime" } local p = Path:new { "aoeu", "agen.lua" } local absolute = r.filename .. path.sep .. p.filename @@ -138,7 +139,7 @@ describe("Path", function() end) it("can take absolute paths and make them relative to the root directory", function() - local root = path.sep == "\\" and "c:\\" or "/" + local root = System.is_windows() and "c:\\" or "/" local p = Path:new { "home", "prime", "aoeu", "agen.lua" } local absolute = root .. p.filename local relative = Path:new(absolute):make_relative(root) @@ -146,7 +147,7 @@ describe("Path", function() end) it("can take absolute paths and make them relative to themselves", function() - local root = path.sep == "\\" and "c:\\" or "/" + local root = System.is_windows() and "c:\\" or "/" local p = Path:new { root, "home", "prime", "aoeu", "agen.lua" } local relative = Path:new(p.filename):make_relative(p.filename) assert.are.same(relative, ".")