diff options
author | Taco Hoekwater <taco@elvenkind.com> | 2010-05-24 14:05:02 +0000 |
---|---|---|
committer | Taco Hoekwater <taco@elvenkind.com> | 2010-05-24 14:05:02 +0000 |
commit | 57ea7dad48fbf2541c04e434c31bde655ada3ac4 (patch) | |
tree | 1f8b43bc7cb92939271e1f5bec610710be69097f /Master/texmf-dist/tex/context/base/luat-cbk.lua | |
parent | 6ee41e1f1822657f7f23231ec56c0272de3855e3 (diff) |
here is context 2010.05.24 13:05
git-svn-id: svn://tug.org/texlive/trunk@18445 c570f23f-e606-0410-a88d-b1316a301751
Diffstat (limited to 'Master/texmf-dist/tex/context/base/luat-cbk.lua')
-rw-r--r-- | Master/texmf-dist/tex/context/base/luat-cbk.lua | 128 |
1 files changed, 117 insertions, 11 deletions
diff --git a/Master/texmf-dist/tex/context/base/luat-cbk.lua b/Master/texmf-dist/tex/context/base/luat-cbk.lua index d8b508c13d6..3cb63ad6ec5 100644 --- a/Master/texmf-dist/tex/context/base/luat-cbk.lua +++ b/Master/texmf-dist/tex/context/base/luat-cbk.lua @@ -1,11 +1,15 @@ if not modules then modules = { } end modules ['luat-cbk'] = { version = 1.001, - comment = "companion to luat-lib.tex", + comment = "companion to luat-lib.mkiv", author = "Hans Hagen, PRAGMA-ADE, Hasselt NL", copyright = "PRAGMA ADE / ConTeXt Development Team", license = "see context related readme files" } +local insert, remove, find = table.insert, table.remove, string.find +local collectgarbage, type, next = collectgarbage, type, next +local round = math.round + local trace_checking = false trackers.register("memory.checking", function(v) trace_checking = v end) --[[ldx-- @@ -14,8 +18,7 @@ your own code into the <l n='tex'/> engine. Here we implement a few handy auxiliary functions.</p> --ldx]]-- -callbacks = { } -callbacks.stack = { } +callbacks = callbacks or { } --[[ldx-- <p>When you (temporarily) want to install a callback function, and after a @@ -23,20 +26,124 @@ while wants to revert to the original one, you can use the following two functions.</p> --ldx]]-- +local trace_callbacks = false trackers.register("system.callbacks", function(v) trace_callbacks = v end) + +local register_callback, find_callback = callback.register, callback.find +local frozen, stack = { }, { } + +callback.original_register_callback = register_callback + +local function frozen_message(what,name) + logs.report("callbacks","not %s frozen '%s' (%s)",what,name,frozen[name]) +end + +local function frozen_callback(name) + return nil, format("callback '%s' is frozen (%s)",name,frozen[name]) +end + +local function state(name) + local f = find_callback(name) + if f == false then + return "disabled" + elseif f then + return "enabled" + else + return "undefined" + end +end + +function callbacks.report() + local list = callback.list() + for name, func in table.sortedhash(list) do + local str = frozen[name] + if str then + logs.report("callbacks","%s: %s -> %s",state(name),name,str) + else + logs.report("callbacks","%s: %s",state(name),name) + end + end +end + +function callbacks.table() + local NC, NR, verbatim = context.NC, context.NR, context.type + context.starttabulate { "|l|l|p|" } + for name, func in table.sortedhash(callback.list()) do + NC() verbatim(name) NC() verbatim(state(name)) NC() context(frozen[name] or "") NC() NR() + end + context.stoptabulate() +end + +function callbacks.freeze(name,freeze) + freeze = type(freeze) == "string" and freeze +--~ print(name) + if find(name,"%*") then + local pattern = name -- string.simpleesc(name) + local list = callback.list() + for name, func in next, list do + if find(name,pattern) then + frozen[name] = freeze or frozen[name] or "frozen" + end + end + else + frozen[name] = freeze or frozen[name] or "frozen" + end +end + +function callbacks.register(name,func,freeze) + if frozen[name] then + if trace_callbacks then + frozen_message("registering",name) + end + return frozen_callback(name) + elseif freeze then + frozen[name] = (type(freeze) == "string" and freeze) or "registered" + end + return register_callback(name,func) +end + +function callback.register(name,func) -- original + if not frozen[name] then + return register_callback(name,func) + elseif trace_callbacks then + frozen_message("registering",name) + end + return frozen_callback(name) +end + function callbacks.push(name, func) - if not callbacks.stack[name] then - callbacks.stack[name] = { } + if not frozen[name] then + local sn = stack[name] + if not sn then + sn = { } + stack[name] = sn + end + insert(sn,find_callback(name)) + register_callback(name, func) + elseif trace_callbacks then + frozen_message("pushing",name) end - table.insert(callbacks.stack[name],callback.find(name)) - callback.register(name, func) end function callbacks.pop(name) --- this fails: callback.register(name, table.remove(callbacks.stack[name])) - local func = table.remove(callbacks.stack[name]) - callback.register(name, func) + if not frozen[name] then + local sn = stack[name] + if not sn or #sn == 0 then + -- some error + register_callback(name, nil) -- ! really needed + else + -- this fails: register_callback(name, remove(stack[name])) + local func = remove(sn) + register_callback(name, func) + end + end end +--~ -- somehow crashes later on +--~ +--~ callbacks.freeze("find_.*_file","finding file") +--~ callbacks.freeze("read_.*_file","reading file") +--~ callbacks.freeze("open_.*_file","opening file") + --[[ldx-- <p>The simple case is to remove the callback:</p> @@ -128,7 +235,6 @@ function garbagecollector.check(size,criterium) criterium = criterium or garbagecollector.criterium if not size or (criterium and criterium > 0 and size > criterium) then if trace_checking then - local round = math.round or math.floor local b = collectgarbage("count") collectgarbage("collect") local a = collectgarbage("count") |