-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathmake_ple.lua
More file actions
111 lines (86 loc) · 2.14 KB
/
make_ple.lua
File metadata and controls
111 lines (86 loc) · 2.14 KB
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
#!/usr/bin/env lua
--- make_ple.lua
--
-- combine ple.lua and the modules it uses
-- into one standalone Lua script 'ple'
--
-- it must be run in the same directory as buffer.lua,
-- ple.lua and plterm.lua
--
-- usage: lua make_ple.lua
local function fget(fname)
-- return content of file 'fname' or nil, msg in case of error
-- if fname is '-', then read from stdin
local f, msg, s
if fname == "-" then
s, msg = io.read("*a")
if not s then return nil, msg end
return s
end
f, msg = io.open(fname, 'rb')
if not f then return nil, msg end
s, msg = f:read("*a")
f:close()
if not s then return nil, msg end
return s
end
local function fput(fname, content)
-- write 'content' to file 'fname'
-- if fname is '-', then write to stdout
-- return true in case of success, or nil, msg in case of error
local f, msg, r
if fname == "-" then
r, msg = io.write(content)
if not r then return nil, msg else return true end
end
f, msg = io.open(fname, 'wb')
if not f then return nil, msg end
r, msg = f:write(content)
f:flush(); f:close()
if not r then return nil, msg else return true end
end
-- list of modules
local ml = {
"plterm",
"buffer",
}
local fmtmod =
[[
------------------------------------------------------------------------
-- preload module: %s
package.preload[%q] = function()
%s
end --module: %s
]]
local st = {
[[#!/usr/bin/env lua
-- DO NOT EDIT THIS FILE"
]] }
-- append modules
local m, mf, name, main
for i, name in ipairs(ml) do
m = assert(fget(name .. ".lua"))
-- do not repeat the copyright for each module
m = m:gsub("^-- Copyright (.-\n", "")
mf = string.format(fmtmod, name, name, m, name)
table.insert(st, mf)
end
-- append main program
local fmtmain = [[
------------------------------------------------------------------------
-- main program: ple.lua
%s
]]
local main = fget("ple.lua")
main = string.format(fmtmain, main)
table.insert(st, main)
local s = table.concat(st, "\n\n")
-- save the stanalone editor in one file 'ple'
fput("ple", s)
-- make 'ple' executable
os.execute("chmod +x ple")
-- ple can now be invoked directly from the command line:
--
-- $ ./ple
-- or $ ./ple some_file
--