Warning: this is an htmlized version!
The original is here, and the conversion rules are here. |
#!/usr/bin/env lua51 -- defwithmanyargs.lua -- Eduardo Ochs, 2011jan17 -- This file: (find-angg "LUA/defwithmanyargs.lua") -- http://angg.twu.net/LUA/defwithmanyargs.lua -- http://angg.twu.net/LUA/defwithmanyargs.lua.html -- See: -- (find-es "tex" "more-than-9-args") -- (find-angg ".emacs" "tex-def-many") -- «.DefWithManyArgs-class» (to "DefWithManyArgs-class") -- «.defwithmanyargs» (to "defwithmanyargs") -- «.DefWithManyArgs-tests» (to "DefWithManyArgs-tests") -- «.SetManyArgs» (to "SetManyArgs") -- «.SetManyArgs-tests» (to "SetManyArgs-tests") -- «.Bottle» (to "Bottle") -- «.run» (to "run") -- «.test-arg» (to "test-arg") -- «.test-run» (to "test-run") --[[ * (eepitch-lua51) * (eepitch-kill) * (eepitch-lua51) --]] -- Old: name = "foo" body = "% body\n" ha = function (hn) return format("#%d", hn) end han = function (np) return mapconcat(ha, seq(1, np)) end sa = function (myn, hn) return format("\\sa{%d}{#%d}", myn, hn) end saf = function (n) return function (hn) return sa(5*n + hn, hn) end end san = function (n, np) return mapconcat(saf(n), seq(1, np)) end na = function (n) return format("\\%s%s", name, ("@"):rep(n)) end bg = function (n) return n==0 and "\\begingroup" or "" end -- ga = function (myn) return format("\\ga{%d}", myn) end da = function (n) return format("\\def%s%s{%s%%\n", na(n), han(5), bg(n)) .. format(" %s%s}\n", san(n, 5), na(n + 1)) end dal = function (n, np) return format("\\def%s%s{%%\n", na(n), han(np)) .. format(" %s%%\n", san(n, np)) .. body .. " \\endgroup}\n" end fulldef_ = function (np) local T = {} for n=0,10000 do if np > 5 then tinsert(T, da(n)); np = np - 5 else tinsert(T, dal(n, np)); break end end return table.concat(T, "") end -- «DefWithManyArgs-class» (to ".DefWithManyArgs-class") -- Written in 2021mar16. -- DefWithManyArgs = Class { type = "DefWithManyArgs", fromnames = function (name, argnames) local n = #split(argnames) return DefWithManyArgs {name=name, argnames=argnames, remaining=n} end, __index = { subst = function (dma, str) local f = function (method, arg) return dma[method](dma, arg) end return (str:gsub("<(.-):(.-)>", f)) end, mapn = function (dma, method, n) return mapconcat(function (k) return dma[method](dma, k) end, seq(1, n)) end, advance = function (dma) dma.ats = dma.ats .. "@" dma.delta = dma.delta + 5 dma.remaining = dma.remaining - 5 return "" end, mapremaining = function (dma, method) local A = {} while dma.remaining > 0 do table.insert(A, dma[method](dma, dma:N())) dma:advance() end return table.concat(A, "\n") end, -- ats = "", delta = 0, remaining = 7, N = function (dma) return min(dma.remaining, 5) end, v = function (dma, fieldname) return dma[fieldname] end, arg = function (dma, n) return "#"..n end, args = function (dma, n) return dma:mapn("arg", n) end, argsN = function (dma, _) return dma:args(dma:N()) end, setarg = function (dma, n) return "\\sa{"..dma.delta+n.."}{#"..n.."}" end, setargs = function (dma, n) return dma:mapn("setarg", n) end, setargsN = function (dma, _) return dma:setargs(dma:N()) end, defN = function (dma) return dma:subst("\\def\\<v:name><v:ats><argsN:>{%\n".. " <setargsN:>%\n".. " \\<v:name>@<v:ats>}\n") end, defNs = function (dma) local bigstr = "" while dma.remaining > 0 do bigstr = bigstr .. dma:defN() dma.ats = dma.ats .. "@" dma.delta = dma.delta + 5 dma.remaining = dma.remaining - 5 end return bigstr end, defbody = function (dma) return dma:subst("\\def\\<v:name><v:ats>{<v:body>}") end, deffull = function (dma) return dma:subst("\\makeatletter%\n".. "<defNs:><defbody:>%\n".. "\\makeatother") end, }, } -- «defwithmanyargs» (to ".defwithmanyargs") defwithmanyargs = function (name, nargs, body) return DefWithManyArgs {name=name, remaining=nargs, body=body}:deffull() end -- «DefWithManyArgs-tests» (to ".DefWithManyArgs-tests") --[==[ * (eepitch-lua51) * (eepitch-kill) * (eepitch-lua51) dofile "defwithmanyargs.lua" dma = DefWithManyArgs {delta=0, name="foo", ats="@@", argnames="aa bb cc dd ee ff gg hh"} dma = DefWithManyArgs.fromnames("foo", "aa bb cc dd ee ff gg hh") = dma:subst [[<argname:2>]] = dma:subst [[<setargname:2>]] = dma:subst [[<setargnames0:3>]] = dma:subst [[<setargnames0:3>]] = dma:subst [[<setargnames1:>]] = dma:subst "<setargnames1:><advance:>\n<setargnames1:>" dma = DefWithManyArgs.fromnames("foo", "aa bb cc dd ee ff gg hh") = dma:subst "<setargnames1:><advance:>\n<setargnames1:>" dma = DefWithManyArgs.fromnames("foo", "aa bb cc dd ee ff gg hh") = dma:subst "<mapremaining:setargnames1>" dma = DefWithManyArgs.fromnames("foo", "aa bb cc dd ee ff gg hh") = dma:setargnames2() = dma:subst("\\def\\<v:name><v:ats><argsN:>{%\n".. " <setargsN:>%\n".. " \\<v:name>@<v:ats>}\n") --]==] --[==[ * (eepitch-lua51) * (eepitch-kill) * (eepitch-lua51) dofile "defwithmanyargs.lua" dma = DefWithManyArgs {delta=0} dma = DefWithManyArgs {delta=0, name="foo", ats="@@"} = dma:subst "foo <setarg:3> plic" = dma:subst "foo <setargs:5> plic" = dma:subst "foo <args:5><setargs:5> plic" = dma:subst "foo <args:5><v:remaining> plic" = dma:subst [[\def\<v:name><v:ats><args:5>{% <setargs:5> }]] = dma:subst [[\def\<v:name><v:ats><argsN:>{% <setargsN:>% \<v:name>@<v:ats>}]] dma = DefWithManyArgs {name="Foo", remaining=23, body="BLA"} = dma:defNs() dma = DefWithManyArgs {name="Foo", remaining=23, body="BLA"} = dma:deffull() dma = DefWithManyArgs {name="Foo", remaining=10, body="BLA"} = dma:deffull() = defwithmanyargs("Boo", 12, "BAR") --]==] -- «Bottle» (to ".Bottle") -- (find-es "tex" "more-than-9-args") --[[ * (eepitch-lua51) * (eepitch-kill) * (eepitch-lua51) dofile "defwithmanyargs.lua" = defwithmanyargs("Bottle", 10, "\\BottleCore") = defwithmanyargs("WideBottle", 14, "\\WideBottleCore") --]] -- «SetManyArgs» (to ".SetManyArgs") SetManyArgs = Class { type = "SetManyArgs", from = function (name, argnames) local n = #split(argnames) return SetManyArgs {name=name, argnames=argnames, remaining=n} end, __index = { subst = function (sma, str) local f = function (method, arg) return sma[method](sma, arg) end return (str:gsub("<(.-):(.-)>", f)) end, advanceat = function (sma) sma.ats = sma.ats.."@"; return "" end, advance = function (sma) sma.ats = sma.ats .. "@" sma.delta = sma.delta + 5 sma.remaining = sma.remaining - 5 return "" end, mapn = function (sma, method, n) return mapconcat(function (k) return sma[method](sma, k) end, seq(1, n)) end, mapremaining = function (sma, method) local A = {} while sma.remaining > 0 do table.insert(A, sma[method](sma, sma:N())) sma:advance() end return table.concat(A, "\n") end, -- ats = "", delta = 0, remaining = 7, N = function (sma) return min(sma.remaining, 5) end, v = function (sma, fieldname) return sma[fieldname] end, N = function (sma) return min(sma.remaining, 5) end, v = function (sma, fieldname) return sma[fieldname] end, arg = function (sma, n) return "#"..n end, args = function (sma, n) return sma:mapn("arg", n) end, argsN = function (sma, _) return sma:args(sma:N()) end, setarg = function (sma, n) return "\\sa{"..sma.delta+n.."}{#"..n.."}" end, setargs = function (sma, n) return sma:mapn("setarg", n) end, setargsN = function (sma, _) return sma:setargs(sma:N()) end, -- argname = function (sma, n) return split(sma.argnames)[n + sma.delta] end, setargname = function (sma, n) return sma:subst("\\sa{<argname:"..n..">}{#"..n.."}") end, setargnames0 = function (sma) return sma:mapn("setargname", sma:N()) end, vnameats = function (sma) return sma:subst "\\<v:name><v:ats>" end, vnameats1 = function (sma) return sma:subst "\\<v:name>@<v:ats>" end, vnameats1opt = function (sma) return sma.remaining > 5 and sma:vnameats1() or "" end, setargnames1 = function (sma) return sma:subst("\\def<vnameats:><argsN:>{%\n".. " <setargnames0:>%\n".. " <vnameats1opt:>}") end, setargnames2 = function (sma) return sma:subst "<mapremaining:setargnames1>" end, setargnames3 = function (sma) return sma:subst("\\makeatletter\n".. "\\def<vnameats:>#1{<vnameats1:>#1}<advanceat:>\n".. "<setargnames2:>\n".. "\\makeatother") end, }, } setmanyargs = function (name, argnames) return SetManyArgs.from(name.."SetArgs", argnames):setargnames3() end -- «SetManyArgs-tests» (to ".SetManyArgs-tests") -- --[==[ * (eepitch-lua51) * (eepitch-kill) * (eepitch-lua51) dofile "defwithmanyargs.lua" sma = SetManyArgs.from("FooSetArgs", "aa bb cc dd ee ff gg hh") = sma:setargnames3() = setmanyargs( "Bottle", "32 20 21 22 10 11 12 00 01 02") = setmanyargs("WideBottle", "32 33 20 21 22 23 10 11 12 13 00 01 02 03") = setmanyargs("OArtDecoN", "32 33 20 21 22 23 10 11 12 13 00 01 02 03") = setmanyargs("ArtNouveauN", "3_ _3 2_ _2 1_ _1") --]==] --[[ texdef = function (Sh, lower) local out = "" local printf = function (...) out = out..format(...) end local w, h = Sh.width, Sh.height local pw = 6 * w + 2 local ph = 12 * h local name = Sh.name local args = texargs[Sh.nvertices] local plower = (lower or 0) * 12 printf("\\def\\dag%s%s{%%\n", name, args) printf(" \\dagpicture(%d,%d)(-4,0)[%d]{\n", pw, ph, plower) for i,xy in ipairs(Sh.coords) do local x, y = xy[1], xy[2] local px, py = (x-1)*6, (h-y)*12 printf(" \\dagput(%3d,%3d){$#%d$}\n", px, py, i) end printf(" }}\n") return out end, --]] -- «run» (to ".run") -- (find-angg ".emacs" "tex-def-many") if arg and arg[1] == "--longdef" then name = arg[2] np = tonumber(arg[3]) body = arg[4] or "% body\n" print(fulldef_(np)) end --[[ -- Tests. * (eepitch-lua51) * (eepitch-kill) * (eepitch-lua51) ee_dofile "~/LUA/defwithmanyargs.lua" = fulldef_(16) = fulldef_(20) = mapconcat(ha, seq(1, 5)) = mapconcat(saf(3), seq(1, 5)) = da(0)..da(1)..da(2) = dal(3, 1) = dal(3, 5) -- «test-arg» (to ".test-arg") * (eepitch-lua51) * (eepitch-kill) * (eepitch-lua51) arg = {"--longdef", "sixt", "16"} ee_dofile "~/LUA/defwithmanyargs.lua" arg = {"--longdef", "sixt", "20", "% bbooddyy\n% bboo\n"} ee_dofile "~/LUA/defwithmanyargs.lua" -- «test-run» (to ".test-run") -- (find-sh "~/LUA/defwithmanyargs.lua -run rect 12") -- (find-sh "~/LUA/defwithmanyargs.lua -run rect 12 '% body\n% body\n'") -- (find-angg ".emacs" "tex-def-many") --]] -- Local Variables: -- coding: raw-text-unix -- modes: (fundamental-mode lua-mode) -- End: