%D \module
%D   [      file=s-system-callbacks.mkxl,
%D        version=2020.06.02,
%D          title=\CONTEXT\ Style File,
%D       subtitle=System Callback States,
%D         author=Hans Hagen,
%D           date=\currentdate,
%D      copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
%C
%C This module is part of the \CONTEXT\ macro||package and is
%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
%C details.

\startluacode

moduledata.callbacks = moduledata.callbacks or { }

local list       = callback.names()
local states     = callback.getstatevalues()

local getstates  = callbacks.getstates
local getcomment = callbacks.getcomment

local context = context
local BC, NC, NR = context.BC, context.NC, context.NR

function moduledata.callbacks.showstates()
    local statekeys = table.sortedkeys(states)
    context.starttabulate { "|T|" .. string.rep("Tc|",#statekeys) }
        context.FL()
        BC() context("name")
        for i=1,#statekeys do
            BC()
            local k = statekeys[i]
            context(states[k])
        end
        NC() NR()
        context.ML()
        for i=1,#list do
            local state = callback.getstate(i)
            NC() context(list[i])
            for i=1,#statekeys do
                NC()
                local k = statekeys[i]
                if (state & k) == k then
                    context("+")
                end
            end
            NC() NR()
        end
        context.LL()
    context.stoptabulate()
end

function moduledata.callbacks.showdetails()
    context.starttabulate { "|rT|lT|lT|" }
        context.FL()
        BC() context("id")
        BC() context("name")
        BC() context("[state] comment")
        NC() NR()
        context.ML()
        for index, name in table.sortedhash(list) do
            local states  = getstates(name)  or {}
            local comment = getcomment(name) or ""
            NC() context(index)
            NC() context(name)
            if #states > 0 then
                NC() context("[% t]",states)
                if comment ~= "" then
                    NR() NC() NC() NC()
                end
            end
            if comment ~= "" then
                context(comment)
            end
            NC() NR()
        end
        context.LL()
    context.stoptabulate()
end

commands.showcallbacks = moduledata.callbacks.showdetails -- mkiv compatible name

\stopluacode

\continueifinputfile{s-system-callbacks.mkxl}

\usemodule[article-basic]

\setuplayout[tight]

\starttext
    \ctxlua{moduledata.callbacks.showstates ()} \page
    \ctxlua{moduledata.callbacks.showdetails()} \page
\stoptext
