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
77end
88local lfs = require " lfs"
99local fs = require ' vim.fs'
10+ local uv = require ' vim.uv'
1011local 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
1428function M .getcwd ()
15- if vim then
16- return vim .fn .getcwd ()
17- end
1829 return lfs .currentdir ()
1930end
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
2443function 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
2646end
2747
2848--- wrap `vim.fn.mkdir()`
3858--- @param dir string
3959--- @return string
4060function 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
4271end
4372
@@ -54,6 +83,14 @@ function M.strwidth(string)
5483 return # string
5584end
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
0 commit comments