steed/stead/xact.lua

132 lines
2.8 KiB
Lua
Raw Normal View History

2010-06-25 12:31:54 +03:00
xact = function(n, f) -- just simple action!
2010-06-07 16:06:00 +03:00
local v = {};
2010-07-02 15:11:21 +03:00
if f == nil and type(n) == 'table' then
f = n[2];
n = n[1];
end
2010-07-11 16:26:47 +03:00
if type(n) ~= 'string' then
2010-06-25 12:31:54 +03:00
error ("Wrong parameter to xact.", 2)
end
2010-06-27 13:02:56 +03:00
v.xaction_type = true
2010-06-25 12:31:54 +03:00
v.nam = n
v.act = f;
2010-06-27 08:37:32 +03:00
v = obj(v);
2010-06-25 12:31:54 +03:00
v.save = function(self, name, h, need)
if need then
2010-06-28 07:48:57 +03:00
local f = self.act;
2010-07-11 16:26:47 +03:00
f = stead.tostring(f);
if f == nil then
error("Can not save xact: "..name);
2010-06-25 12:31:54 +03:00
end
h:write(stead.string.format("%s = xact(%q, %s);\n", name, self.nam, f))
end
savemembers(h, self, name, false);
end
2010-06-07 16:06:00 +03:00
return v
end
2010-06-27 12:43:18 +03:00
__do_xact = function(str, self)
2010-06-25 12:31:54 +03:00
local aarg = {}
local function parg(v)
stead.table.insert(aarg, v);
return ''
end
2010-06-07 16:06:00 +03:00
local xrefrep = function(str)
2010-10-30 15:54:41 +03:00
local s = stead.string.gsub(str,'[\001\002]','');
2010-06-27 12:43:18 +03:00
local o,d,a, oo;
2010-09-16 19:27:21 +03:00
local delim = ':'
if stead.api_version >= "1.2.2" then
delim = stead.delim;
end
2010-11-04 14:46:25 +02:00
s = s:gsub('\\?[\\'..delim..']', { [ delim ] = '\001', [ '\\'..delim ] = delim });
2010-10-30 15:54:41 +03:00
local i = s:find('\001', 1, true);
2010-06-25 12:31:54 +03:00
aarg = {}
if i then
o = s:sub(1, i - 1);
d = s:sub(i + 1);
i = o:find("(", 1, true);
if i then
a = o:sub(i);
o = o:sub(1, i - 1);
a:gsub('[^,()]+', parg);
end
2010-06-27 08:37:32 +03:00
if o == '' then
2010-06-27 12:43:18 +03:00
if isObject(self) then
oo = self
else
error("Empty link: "..s, 3);
end
else
2011-01-12 08:12:46 +02:00
oo = objs():srch(o)
2010-06-27 12:43:18 +03:00
if not oo then
2011-07-27 16:11:29 +03:00
oo = stead.ref(o, true)
2010-06-27 12:43:18 +03:00
end
2010-06-27 08:37:32 +03:00
end
2010-06-27 12:43:18 +03:00
elseif isObject(self) then
oo = self
d = s;
2010-06-25 12:31:54 +03:00
else
2010-06-27 08:51:14 +03:00
error("Wrong link: "..s, 3);
2010-06-25 12:31:54 +03:00
end
2010-10-30 15:54:41 +03:00
d = d:gsub("\001", delim);
2011-07-27 16:11:29 +03:00
return xref(d, stead.ref(oo, true), unpack(aarg));
2010-06-07 16:06:00 +03:00
end
if type(str) ~= 'string' then return end
2010-11-04 14:46:25 +02:00
local s = stead.string.gsub(str, '\\?[\\{}]',
2011-03-24 12:43:52 +02:00
{ ['{'] = '\001', ['}'] = '\002' }):gsub('\001([^\002]+)\002', xrefrep):gsub('[\001\002]', { ['\001'] = '{', ['\002'] = '}' });
2010-06-07 16:06:00 +03:00
return s;
end
2010-06-27 08:37:32 +03:00
stead.fmt = stead.hook(stead.fmt, function(f, ...)
2010-10-30 15:54:41 +03:00
local i, res, s
2011-02-23 12:11:27 +02:00
local a = {...}
for i=1,stead.table.maxn(a) do
if type(a[i]) == 'string' then
s = __do_xact(a[i]);
2010-11-04 14:46:25 +02:00
res = stead.par('', res, s):gsub('\\?[\\{}]', { [ '\\{' ] = '{', [ '\\}' ] = '}' });
2010-10-30 15:54:41 +03:00
end
2010-06-27 08:37:32 +03:00
end
2010-10-30 15:54:41 +03:00
return f(res);
2010-06-25 12:31:54 +03:00
end)
2010-06-27 09:01:36 +03:00
2010-06-27 12:43:18 +03:00
obj = stead.inherit(obj, function(v)
2010-06-27 09:01:36 +03:00
v.xref = function(s, str)
2010-06-27 12:43:18 +03:00
return __do_xact(str, s);
2010-06-27 09:01:36 +03:00
end
return v
end)
2010-06-28 07:48:57 +03:00
function xdsc(n)
local v = {}
v.nam = true
if n == nil then
v.disp = 'xdsc'
elseif type(n) == 'string' then
v.disp = n;
else
error("Wrong parameter to xdsc.", 2);
end
v.dsc = function(s)
2011-07-27 13:21:20 +03:00
return stead.call(here(), s.disp);
2010-06-28 07:48:57 +03:00
end
v.save = function(self, name, h, need)
if need then
h:write(stead.string.format("%s = xdsc(%q);\n", name, self.disp))
end
savemembers(h, self, name, false);
end
return obj(v)
end
2010-06-29 16:18:03 +03:00
2010-06-29 19:23:52 +03:00
xroom = stead.inherit(room, function(v)
v.look = stead.hook(v.look, function(f, s,...)
2011-07-27 13:21:20 +03:00
local xdsc = stead.call(s, 'xdsc');
2011-02-23 12:11:27 +02:00
return par(' ', xdsc, f(s, ...));
2010-06-29 19:23:52 +03:00
end)
return v
2010-06-29 16:18:03 +03:00
end)
2010-07-06 13:15:27 +03:00
-- vim:ts=4