|
Warning: this is an htmlized version!
The original is here, and the conversion rules are here. |
-- This file:
-- http://angg.twu.net/LATEX/2021excuse.lua.html
-- http://angg.twu.net/LATEX/2021excuse.lua
-- (find-angg "LATEX/2021excuse.lua")
-- Author: Eduardo Ochs <eduardoochs@gmail.com>
--
-- (defun e (interactive) (find-angg "LATEX/2021excuse.tex"))
-- (defun l (interactive) (find-angg "LATEX/2021excuse.lua"))
-- «.myverbatim» (to "myverbatim")
-- «.at» (to "at")
-- «.errors» (to "errors")
-- «myverbatim» (to ".myverbatim")
-- From: (find-dednat6 "myverbatim.lua")
verbapat = "[\n #$%%&\\^_{}~]"
verbadict = {["\n"]="\\\\\n"}
verbaset_b = function (c) verbadict[c] = "\\"..c end
verbaset_c = function (c) verbadict[c] = "\\char"..string.byte(c).." " end
verbaexpand = function (str) return (string.gsub(str, verbapat, verbadict)) end
gchars = function (str) return string.gmatch(str, ".") end
for c in gchars(" \\%&^_{}~") do verbaset_c(c) end
for c in gchars("#$") do verbaset_b(c) end
verbalast = {} -- contents of the last verbatim block
verbact = function (str)
local doe = function (A) return map(verbaexpand, A) end
local doh1 = function (s) return format("\\verbahbox{%s}", s) end
local doh = function (A) return map(doh1, A) end
local doc = function (A) return table.concat(A, "%\n") end
local dov = function (s) return format("\\vbox{%%\n%s%%\n}", s) end
local dobg = function (s) return format("\\bgbox{%s}", s) end
local dodef = function (s, action)
local name = action:match":(.*)"
return format("\\def\\%s{%s}", name, s)
end
--
for _,action in ipairs(split(str)) do
if action == "e" then verbalast = doe(verbalast)
elseif action == "h" then verbalast = doh(verbalast)
elseif action == "c" then verbalast = doc(verbalast)
elseif action == "v" then verbalast = dov(verbalast)
elseif action == "bg" then verbalast = dobg(verbalast)
elseif action:match"^def:" then verbalast = dodef(verbalast, action)
elseif action == "o" then output(verbalast)
elseif action == "P" then PP(verbalast)
else error("Unrecognized action: "..action)
end
end
end
verbdef = function (name) verbact("e h c v bg def:"..name.." o") end
registerhead "%V" {
name = "myverbatim",
action = function ()
local i,j,origlines = tf:getblock()
verbalast = origlines
PPV(origlines)
end,
}
-- «at» (to ".at")
-- From: (find-es "dednat" "at:")
Node = Class {
type = "Node",
__tostring = function (node) return mytostring(node) end,
__index = {
v = function (node) return v(node.x,node.y) end,
setv = function (node,v) node.x=v[1]; node.y=v[2]; return node end,
},
}
storenode = function (node)
node = Node(node)
table.insert(nodes, node)
node.noden = #nodes -- nodes[node.noden] == node
if node.tag then -- was: "and not nodes[node.tag]"...
nodes[node.tag] = node -- nodes[node.tag] == node
end
return node
end
tow = function (vv, ww, a, b)
local diff = ww-vv
local diffrot90 = v(diff[2], -diff[1])
return vv + (a or 0.5)*diff + (b or 0)*diffrot90
end
ats_to_vs = function (str)
return (str:gsub("@(%w+)", "nodes[\"%1\"]:v()"))
end
forths["newnode:"] = function ()
local tag = getword()
ds:push(storenode({tag=tag, TeX=phantomnode}))
end
forths["at:"] = function ()
local node = ds:pick(0)
local vexpr = getword()
node:setv(expr(ats_to_vs(vexpr)))
end
-- «errors» (to ".errors")
-- (find-dn6 "parse.lua" "getword-with-errmsgs")
-- (find-dn6 "errors.lua")
-- (find-LATEX "2020dn6-error-handling.lua")
-- (find-LATEX "2020dn6-error-handling.lua" "arrow-modifiers")
forths[".plabel="] = function ()
local p, label = getword_2(".plabel=", "first argument (placement)",
"second argument (label)")
ds:pick(0).placement, ds:pick(0).label = p, label
end
forths[".slide="] = function ()
ds:pick(0).slide = getword_1(".slide=")
end
forths[".curve="] = function ()
ds:pick(0).curve = getword_1(".curve=")
end
forths["xy+="] = function ()
local dx,dy = getword_lualua(".plabel=", "first argument (dx)",
"second argument (dy)")
ds:pick(0).x = ds:pick(0).x + dx
ds:pick(0).y = ds:pick(0).y + dy
end
-- (find-LATEX "2020dn6-error-handling.lua" "dxyrun")
dxyrun_errmsg = function (errmsg)
local fname = tf.name
local b = lastheadblock
local blockinfo = format("%s-block in lines %d-%d", b.head, b.i, b.j)
local where = format("In %s, in the %s, in line %d",
fname, blockinfo, dxyrun_linenum)
local whereinsubj = (" "):rep(startcol - 1)..("^"):rep(endcol - startcol)
local errmsg0 = errmsg:gsub("^[^ ]* ", "")
return errmsg0 .. "\n" ..
where ..":\n\n"..
subj .. "\n" ..
whereinsubj.. "\n\n"..
"Dednat6 error - aborting!"
end
dxyrun0 = function (str, pos)
local restorevars = savevars(function (...)
subj,pos,startcol,endcol = ... end,
subj,pos,startcol,endcol)
setsubj(str, pos or 1)
while getword() do
-- PP(word)
if forths[word] then forths[word]()
elseif nodes[word] then ds:push(nodes[word])
else Error("Unknown word: "..word)
end
end
restorevars()
end
-- (find-LATEX "2020dn6-error-handling.lua" "dxyrun")
-- (find-angg "edrxrepl/edrxrepl.lua" "MyXpcall-class")
dxyrun = function (str, pos, linenum)
dxyrun_linenum = linenum or "??"
local myx = MyXpcall.new()
myx.errhandler = function (myx)
return function (...)
myx.eh_args = pack(...)
myx.tb = debug.traceback(myx:tbargs())
print("GAAAAAAAAAH")
print(dxyrun_errmsg(myx:errmsg()))
-- print(myx:shortertraceback())
return "eh22", "eh33", "eh44" -- only the first is used
end
end,
myx:call0(dxyrun0, str, pos)
PP(myx:ret())
PPV(keys(myx))
-- local f = function () dxyrun0(str, pos) end
-- local status = xpcall(f, dxyrun_errhandler)
-- if status == false then os.exit(1) end
end
-- dxyrun_errhandler = function (errmsg) print(dxyrun_errmsg(errmgs)) end
--[==[
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
loaddednat6()
dofile "2021excuse.lua"
dxyrun [[ diagram ?? ]]
dxyrun [[ 2Dx 100 +40 ]]
dxyrun [[ 2D 100 A --> B ]]
dxyrun [[ 2D | ]]
dxyrun [[ 2D +30 C ]]
dxyrun [[ 2D ]]
dxyrun [[ (( A B -> .plabel= aafoo ]]
dxyrun [[ A W -> .plabel= aafoo ]]
dxyrun [[ )) ]]
dxyrun [[ # enddiagram ]]
--]==]
--[[
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
dofile "2021excuse.lua"
--]]