%D \module
%D   [       file=x-set-11,
%D        version=2004.10.31,
%D         remark=setupx.tex: 1998.07.20 and later,
%D          title=\CONTEXT\ Setup Definitions,
%D       subtitle=Macro Definitions,
%D         author=Hans Hagen,
%D           date=\currentdate,
%D      copyright={PRAGMA / Hans Hagen \& Ton Otten}]
%C
%C This module is part of the \CONTEXT\ macro||package and is
%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
%C details.

\loadmarkfile{x-set-11}

\endinput

% Old code, a mixture of loading in mkiv and processing in mkii.

\startluacode
    do
        document = document or { }

        document.setups = {
            loaded = { },
            root   = nil,
            used   = { },
        }

        function document.setups.load(filename)
            filename = resolvers.find_file(filename) or ""
            if filename ~= "" and not document.setups.loaded[filename] then
                local loaded = xml.load(filename)
                if loaded then
                    if document.setups.root then
                        xml.inject(document.setups.root,"/",loaded)
                    else
                        document.setups.root = loaded
                    end
                    document.setups.loaded[filename] = true
                end
            end
        end
        function document.setups.name(ek)
            local at = ek.at
            local name = at.name
            if at.type == 'environment' then
                name = "start" .. name
            end
            if at.variant then
                name = name .. ":" .. at.variant
            end
            if at.generated == "yes" then
                name = name .. "*"
            end
            return name:lower()
        end
        function document.setups.show(name)
            if document.setups.root then
                local name = name:gsub("[<>]","")
                local setup = xml.first(document.setups.root,"cd:command[@name='" .. name .. "']")
                document.setups.used[#document.setups.used+1] = setup
                xml.sprint(setup)
            end
        end
        function document.setups.showused()
            if document.setups.root and next(document.setups.used) then
                for k,v in ipairs(table.sortedkeys(document.setups.used)) do
                    xml.sprint(document.setups.used[v])
                end
            end
        end
        function document.setups.showall()
            if document.setups.root then
                local list = { }
                xml.each_element(document.setups.root,"cd:command", function(r,d,t)
                    local ek = d[t]
                    list[document.setups.name(ek)] = ek
                end )
                for k,v in ipairs(table.sortedkeys(list)) do
                    xml.sprint(list[v])
                end
            end
        end
        function document.setups.resolve(name)
            if document.setups.root then
                local e = xml.filter(document.setups.root,string.format("cd:define[@name='%s']/text()",name))
                if e then
                    xml.sprint(e)
                end
            end
        end
    end
\stopluacode

\def\mkloadsetups    #1{\ctxlua{document.setups.load("#1")}}
\def\mkshowsetup     #1{\ctxlua{document.setups.show("#1")}}
\def\mkshowusedsetups  {\bgroup\enableXML\ctxlua{document.setups.showused()}\egroup}
\def\mkshowallsetups   {\bgroup\enableXML\ctxlua{document.setups.showall ()}\egroup}

\def\mkshowsetup     #1{\bgroup\enableXML\ctxlua{document.setups.show("#1")}\egroup}

\defineXMLenvironmentsave [cd:define] [name=]
  {}

\defineXMLsingular [cd:resolve] [name=]
  {\enableXML\ignorespaces\ctxlua{document.setups.resolve("\XMLop{name}")}}

\endinput
