-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRadLine.lua
329 lines (286 loc) · 8.61 KB
/
RadLine.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
-- TODO
-- Need a find reg values also
local win32 = require "lrwin32"
--win32.OutputDebugString("package.path: " .. package.path .. "\n")
--win32.OutputDebugString("package.cpath: " .. package.cpath .. "\n")
INVALID_HANDLE_VALUE = -1 -- TODO add to win32 somewhere
-- function FindEnv(s, enclose)
-- function FindRegKey(s)
function DebugOutLn(s)
win32.OutputDebugString(s .. "\n")
end
function split(s, sep)
local fields = {}
local sep = sep or " "
local pattern = string.format("([^%s]+)", sep)
s:gsub(pattern, function(c) fields[#fields + 1] = c end)
return fields
end
function concat(t, ...)
for i,v in ipairs({...}) do
for ii,vv in ipairs(v) do
t[#t+1] = vv
end
end
return t
end
-- TODO can I make beginswith a parameter?
function concat_if(t, s, ...)
for i,v in ipairs({...}) do
for ii,vv in ipairs(v) do
if beginswith(vv:lower(), s) then
t[#t+1] = vv
end
end
end
return t
end
local escape
do
local matches =
{
["^"] = "%^";
["$"] = "%$";
["("] = "%(";
[")"] = "%)";
["%"] = "%%";
["."] = "%.";
["["] = "%[";
["]"] = "%]";
["*"] = "%*";
["+"] = "%+";
["-"] = "%-";
["?"] = "%?";
["\0"] = "%z";
}
escape = function(s)
return s:gsub(".", matches)
end
end
function beginswith(s, t)
-- TODO better? s:sub(1, #t) == t
return s:find("^"..escape(t)) ~= nil
end
function table.contains(table, element)
for _, value in pairs(table) do
if value == element then
return true
end
end
return false
end
function CaseInsensitiveLess(s1, s2)
return s1:lower() < s2:lower()
end
DebugOutLn("RadLine ".._VERSION)
internal = {
"assoc", "call", "cd", "chdir", "cls", "color", "copy", "date", "del", "dir", "echo", "endlocal", "erase", "exit", "for",
"ftype", "goto", "if", "md", "mkdir", "mklink", "move", "path", "popd", "prompt", "pushd", "rem", "ren", "rd", "rmdir",
"set", "setlocal", "shift", "start", "time", "title", "type", "ver", "verify", "vol"
}
function FileNameIsDir(s)
return s:sub(-1) == "\\";
end
function FileNameLess(s1, s2)
if FileNameIsDir(s1) and not FileNameIsDir(s2) then
return true;
elseif not FileNameIsDir(s1) and FileNameIsDir(s2) then
return false;
else
return CaseInsensitiveLess(s1, s2)
end
end
FindFilesE = { All = 0, DirOnly = 1, FileOnly = 2 }
function FindFiles(s, e)
assert(table.contains(FindFilesE, e), "e=" .. tostring(e) .. " is not in FindFilesE")
s = s:gsub('"', '')
if beginswith(s, "~\\") and tonumber(win32.GetEnvironmentVariable("RADLINE_TILDE") or 0) ~= 0 then
s = "%USERPROFILE%" .. s:sub(2, -1)
end
win32.SetEnvironmentVariable("CD", win32.GetCurrentDirectory())
s = win32.ExpandEnvironmentStrings(s)
win32.SetEnvironmentVariable("CD", nil)
local files = {}
local FindFileData = {}
local hFind = win32.FindFirstFile(s, FindFileData)
if hFind ~= INVALID_HANDLE_VALUE then
repeat
if FindFileData.cFileName ~= "." and FindFileData.cFileName ~= ".." then
--if FindFileData.cFileName ~= "." and FindFileData.cFileName ~= ".." and (FindFileData.dwFileAttributes & win32.FILE_ATTRIBUTE.HIDDEN) == 0 then
local add = true;
if e == FindFilesE.FileOnly and (FindFileData.dwFileAttributes & win32.FILE_ATTRIBUTE.DIRECTORY) ~= 0 then add = false;
elseif e == FindFilesE.DirOnly and (FindFileData.dwFileAttributes & win32.FILE_ATTRIBUTE.DIRECTORY) == 0 then add = false;
end
if add then
if (FindFileData.dwFileAttributes & win32.FILE_ATTRIBUTE.DIRECTORY) ~= 0 then
FindFileData.cFileName = FindFileData.cFileName.."\\"
end
files[#files+1] = FindFileData.cFileName
end
end
until not win32.FindNextFile(hFind, FindFileData)
win32.FindClose(hFind);
end
table.sort(files, FileNameLess)
return files;
end
function FindExeFiles(s)
local pathext = split(win32.GetEnvironmentVariable("PATHEXT"), ";")
local dot = s:match'^.*()%.' -- Find last of '.'
local slash = s:match'^.*()\\' or 0 -- Find last of '\'
local ext = (dot and dot > slash) and s:sub(dot):upper() or nil
local f = {}
if not ext then
for _,i in ipairs(pathext) do
concat(f, FindFiles(s.."*"..i, FindFilesE.FileOnly))
end
else
for _,i in ipairs(pathext) do
local dot = i:match'^.*()%.' -- Find last of '.'
local iext = (dot and dot > 1) and i:sub(dot) or nil
if beginswith(i:upper(), ext) then
concat(f, FindFiles(s..i:sub(#ext + 1), FindFilesE.FileOnly))
end
if iext and beginswith(iext:upper(), ext) then
concat(f, FindFiles(s..i:sub(dot + #ext), FindFilesE.FileOnly))
end
--concat(f, FindFiles(s.."*"..i, FindFilesE.FileOnly))
end
end
return f
end
function FindPathExeFiles(s)
local path = split(win32.GetEnvironmentVariable("PATH"), ";")
local f = {}
for _,i in ipairs(path) do
concat(f, FindExeFiles(i.."\\"..s))
end
return f
end
function FindEnv(s, enclose)
s = s:upper()
local e = {}
local env = win32.GetEnvironmentStrings();
for k,v in pairs(env) do
if beginswith(k:upper(), s) then
if enclose then
e[#e+1] = "%"..k.."%"
else
e[#e+1] = k
end
end
end
return e;
end
function FindEnvBegin(s)
local count = 0
for c in s:gmatch("%%") do
count = count + 1
end
if count%2 == 1 then
return s:match'^.*()%%' -- Find last of '%'
else
return nil
end
end
command_sep = {
["&"] = true;
["|"] = true;
["&&"] = true;
["||"] = true;
}
redirect_sep = {
[">"] = true;
[">>"] = true;
["<"] = true;
["2>"] = true;
}
function GetParam(params, p)
local s = params[p] or ""
local i = s:match'^.*()[\\/]' -- Find last of '\' or '/'
i = i and i + 1 or 1
if #s > 0 and s:sub(1, 1) == '"' then
s = s:sub(2)
if i == 1 then
i = i + 1
end
end
if #s > 0 and s:sub(#s, #s) == '"' then
s = s:sub(1, -2)
end
return s, i
end
function FindPotentialDefault(params, p)
local s,i = GetParam(params, p)
return FindFiles(s.."*", FindFilesE.All), i
end
function FindPotentialDirs(params, p)
local s,i = GetParam(params, p)
return FindFiles(s.."*", FindFilesE.DirOnly), i
end
command_fn = {
_default = FindPotentialDefault,
cd = FindPotentialDirs,
chdir = FindPotentialDirs,
md = FindPotentialDirs,
mkdir = FindPotentialDirs,
pushd = FindPotentialDirs,
}
function LookUpExt(t, key)
local pathext = split(win32.GetEnvironmentVariable("PATHEXT"), ";")
for _,i in ipairs(pathext) do
local f = rawget(t, (key..i):lower())
if f then
return f
end
end
return rawget(t, "_default")
end
setmetatable(command_fn, {
__index = LookUpExt
})
other_env = {
"%CD%", "%CMDCMDLINE%", "%CMDEXTVERSION%", "%DATE%", "%ERRORLEVEL%", "%RANDOM%", "%TIME%",
"%FIRMWARE_TYPE%", "%HighestNumaNodeNumber%",
"%__APPDIR__%", "%__CD__%", "%__PID__%", "%__TICK__%", "%RAWPROMPT%", "%RADLINE_LOADED%", "%RADLINE_DIR%",
"%=ExitCode%", "%=ExitCodeAscii%", "%=C:%", "%=D:%", "%=E:%", "%=F:%"
}
function GetCmdOutput(cmd)
local d = {}
local f1 = io.popen(cmd, 'r')
for line in f1:lines() do
d[#d+1] = line
end
f1:close()
return d
end
function FindPotential(params, p)
local s,i = GetParam(params, p)
local e = FindEnvBegin(s)
if e then
local f = {}
concat(f, FindEnv(s:sub(e + 1), true))
concat_if(f, s:sub(e):lower(), other_env)
return f, (e + i - 1)
elseif p == 1 or command_sep[params[p - 1]] then
local f = {}
if i == 1 then
concat_if(f, s:lower(), internal)
concat(f, FindFiles(s.."*", FindFilesE.DirOnly))
concat(f, FindExeFiles(s))
concat(f, FindPathExeFiles(s))
concat(f, FindAlias(s))
else
concat(f, FindFiles(s.."*", FindFilesE.DirOnly))
concat(f, FindExeFiles(s))
end
return f, i
elseif p > 1 and redirect_sep[params[p - 1]] then
return FindFiles(s.."*", FindFilesE.All), i
else
local command = params[1]
-- TODO Unquote and remove path ???
local fn = command_fn[command]
return fn(params, p), i
end
end