Skip to content

Commit fcc4c3e

Browse files
committed
✨ Add some vim.fn.*()
1 parent 5ec2cc6 commit fcc4c3e

2 files changed

Lines changed: 51 additions & 8 deletions

File tree

packages/vim/lua/vim/fn.lua

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
---wrap `vim.fn`
1+
---wrap `vim.fn`. `vim.fn` doesn't use `boolean` rather than `0 | 1`.
22
---@module vim.fn
33
---@diagnostic disable: undefined-global
44
-- luacheck: ignore 111 113 212
@@ -7,22 +7,42 @@ if vim and vim.fn then
77
end
88
local lfs = require "lfs"
99
local fs = require 'vim.fs'
10+
local uv = require 'vim.uv'
1011
local M = {}
1112

13+
---wrap `vim.fn.has()`
14+
---@param feature string
15+
---@return 0 | 1
16+
function M.has(feature)
17+
local ret
18+
if feature == 'win32' then
19+
ret = uv.os_uname().sysname == "windows"
20+
elseif feature == 'nvim' then
21+
ret = true
22+
end
23+
return ret and 1 or 0
24+
end
25+
1226
---wrap `vim.fn.getcwd()`
1327
---@return string cwd
1428
function M.getcwd()
15-
if vim then
16-
return vim.fn.getcwd()
17-
end
1829
return lfs.currentdir()
1930
end
2031

32+
---wrap `vim.fn.executable()`
33+
---@param expr string
34+
---@return 0 | 1
35+
function M.executable(expr)
36+
local attr = lfs.attributes(expr)
37+
return attr and attr.mode ~= "directory" and attr.permissions:match 'x' and 1 or 0
38+
end
39+
2140
---wrap `vim.fn.isdirectory()`
2241
---@param dir string
23-
---@return boolean
42+
---@return 0 | 1
2443
function M.isdirectory(dir)
25-
return lfs.attributes(dir) and lfs.attributes(dir).mode == "directory"
44+
local attr = lfs.attributes(dir)
45+
return attr and attr.mode == "directory" and 1 or 0
2646
end
2747

2848
---wrap `vim.fn.mkdir()`
@@ -38,6 +58,15 @@ end
3858
---@param dir string
3959
---@return string
4060
function M.expand(dir)
61+
if dir == "~" then
62+
dir = uv.os_homedir() or "/"
63+
elseif dir:match "^~/" then
64+
dir = dir:gsub("^~", uv.os_homedir() or "")
65+
end
66+
local path = dir
67+
for m in path:gmatch('%$[a-zA-Z_]+') do
68+
dir = dir:gsub('%' .. m, os.getenv(m:sub(2)) or '')
69+
end
4170
return dir
4271
end
4372

@@ -54,6 +83,14 @@ function M.strwidth(string)
5483
return #string
5584
end
5685

86+
---wrap `vim.fn.fnameescape()`
87+
---@param string string
88+
---@return string
89+
function M.fnameescape(string)
90+
string = string:gsub("[+| %%]", "\\%1")
91+
return string
92+
end
93+
5794
---wrap `vim.fn.fnamemodify()`
5895
---@param fname string
5996
---@param mods string
@@ -62,11 +99,17 @@ function M.fnamemodify(fname, mods)
6299
for mod in mods:gmatch(':(.)') do
63100
if mod == 'p' then
64101
fname = fs.abspath(fname)
65-
if M.isdirectory(fname) then
102+
if M.isdirectory(fname) == 1 then
66103
fname = fs.joinpath(fname, '')
67104
end
68105
elseif mod == 'h' then
69106
fname = fs.dirname(fname)
107+
elseif mod == 't' then
108+
fname = fs.basename(fname)
109+
elseif mod == 'e' then
110+
fname = fname:match('%.[^./]+$') or ''
111+
elseif mod == 'r' then
112+
fname = fname:match('(.*)%.[^./]+$') or fname
70113
end
71114
end
72115
return fname

packages/vim/lua/vim/uv.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ end
8585
---wrap `vim.uv.os_getenv()`
8686
---@return string?
8787
function M.os_homedir()
88-
return os.getenv("HOME")
88+
return os.getenv("HOME") or os.getenv("USERPROFILE")
8989
end
9090

9191
return M

0 commit comments

Comments
 (0)