forked from torch/paths
-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.lua
118 lines (108 loc) · 3.03 KB
/
init.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
require 'libpaths'
local assert = assert
local debug = debug
local pcall = pcall
local type = type
local ipairs = ipairs
local os = os
local g = _G
module('paths')
function is_win()
return uname():match('Windows')
end
function is_mac()
return uname():match('Darwin')
end
if is_win() then
home = os.getenv('HOMEDRIVE') or 'C:'
home = home .. ( os.getenv('HOMEPATH') or '\\' )
else
home = os.getenv('HOME') or '.'
end
function files(s)
local d = dir(s)
local n = 0
return function()
n = n + 1
if (d and n <= #d) then
return d[n]
else
return nil
end
end
end
function thisfile(arg, depth)
local s = debug.getinfo(depth or 2).source
if type(s) ~= "string" then
s = nil
elseif s:match("^@") then -- when called from a file
s = concat(s:sub(2))
elseif s:match("^qt[.]") then -- when called from a qtide editor
local function z(s) return g.qt[s].fileName:tostring() end
local b, f = pcall(z, s:sub(4));
if b and f and f ~= "" then s = f else s = nil end
end
if type(arg) == "string" then
if s then s = concat(dirname(s), arg) else s = arg end
end
return s
end
function dofile(f, depth)
local s = thisfile(nil, 1 + (depth or 2))
if s and s ~= "" then
f = concat(dirname(s),f)
end
return g.dofile(f)
end
function rmall(d, more)
if more ~= 'yes' then
return nil, "missing second argument ('yes')"
elseif filep(d) then
return os.remove(d)
elseif dirp(d) then
for f in files(d) do
if f ~= '.' and f ~= '..' then
local ff = concat(d, f)
local r0,r1,r2 = rmall(ff, more)
if not r0 then
return r0,r1,ff
end
end
end
return rmdir(d)
else
return nil, "not a file or directory", d
end
end
function findprogram(...)
for _,exe in ipairs{...} do
if is_win() then
if not exe:match('[.]exe$') then
exe = exe .. '.exe'
end
local path, k, x = os.getenv("PATH") or "."
for dir in path:gmatch('[^;]+') do
x = concat(dir, exe)
if filep(x) then return x end
end
local function clean(s)
if s:match('^"') then return s:match('[^"]+') else return s end
end
k = 'SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\' .. exe
x = getregistryvalue('HKEY_CURRENT_USER', k, '')
if type(x) == 'string' then return clean(x) end
x = getregistryvalue('HKEY_LOCAL_MACHINE', k, '')
if type(x) == 'string' then return clean(x) end
k = 'Applications\\' .. exe .. '\\shell\\open\\command'
x = getregistryvalue('HKEY_CLASSES_ROOT', k, '')
if type(x) == 'string' then return clean(x) end
else
local path = os.getenv("PATH") or "."
for dir in path:gmatch('[^:]+') do
local x = concat(dir, exe)
if filep(x) then return x end
end
end
end
return nil
end