%D \module
%D   [       file=page-run,
%D        version=2000.10.20,
%D          title=\CONTEXT\ Page Macros,
%D       subtitle=Runtime Macros,
%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.

\writestatus{loading}{ConTeXt Page Macros / Runtime Macros}

\unprotect

\gdef\showprint
  {\dotripleempty\page_layouts_show_print}

\gdef\page_layouts_show_print[#1][#2][#3]% only english
  {\setbuffer[crap]%
      \unprotect
        \definepapersize[X][\c!width=4em, \c!height=6em]
        \definepapersize[Y][\c!width=12em,\c!height=14em]
        \setuppapersize[X,#1][Y,#2]
        \setuplayout[#3]
        \setuplayout[\v!page]
        \framed
          [\c!offset=\v!overlay,\c!strut=\v!no,
           \c!width=\paperwidth,\c!height=\paperheight]
          {\ss ABC\par DEF}%
      \protect
   \endbuffer
   \framed[\c!offset=\v!overlay,\c!strut=\v!no]{\typesetbuffer[crap]}}

% \switchtobodyfont[8pt]
%
% \startcombination[4*4]
%   {\showprint}                                      {\strut}
%   {\showprint[][][location=middle]}                 {\type{location=middle}}
%   {\showprint[][][location=middle,marking=on]}      {\type{markering=aan}\break\type{location=middle}}
%   {\showprint[][][location=middle,marking=on,nx=2]} {\type{markering=aan}\break\type{location=middle}\break\type{nx=2}}
%   {\showprint[][][location=left]}                   {\type{location=left}}
%   {\showprint[][][location=right]}                  {\type{location=right}}
%   {\showprint[][][location={left,bottom}]}          {\type{location={left,bottom}}}
%   {\showprint[][][location={right,bottom}]}         {\type{location={right,bottom}}}
%   {\showprint[][][nx=2,ny=1]}                       {\type{nx=2,ny=1}}
%   {\showprint[][][nx=1,ny=2]}                       {\type{nx=1,ny=2}}
%   {\showprint[][][nx=2,ny=2]}                       {\type{nx=2,ny=2}}
%   {\showprint[][][nx=2,ny=2,location=middle]}       {\type{nx=2,ny=2}\break\type{location=middle}}
%   {\showprint[][][backoffset=3pt]}                  {\type{rugoffset=.5cm}}
%   {\showprint[][][topoffset=3pt]}                   {\type{kopoffset=.5cm}}
%   {\showprint[][][scale=1.5]}                       {\type{schaal=1.5}}
%   {\showprint[][][scale=0.8]}                       {\type{schaal=0.8}}
% \stopcombination
%
% \startcombination[3*4]
%   {\showprint[landscape][]         [location=middle]} {\type{landscape}}
%   {\showprint[]         [landscape][location=middle]} {\strut\break\type{landscape}}
%   {\showprint[landscape][landscape][location=middle]} {\type{landscape}\break\type{landscape}}
%   {\showprint[90]       []         [location=middle]} {\type{90}}
%   {\showprint[]         [90]       [location=middle]} {\strut\break\type{90}}
%   {\showprint[90]       [90]       [location=middle]} {\type{90}\break\type{90}}
%   {\showprint[180]      []         [location=middle]} {\type{180}}
%   {\showprint[]         [180]      [location=middle]} {\strut\break\type{180}}
%   {\showprint[180]      [180]      [location=middle]} {\type{180}\break\type{180}}
%   {\showprint[mirrored] []         [location=middle]} {\type{mirrored}}
%   {\showprint[]         [mirrored] [location=middle]} {\strut\break\type{mirrored}}
%   {\showprint[mirrored] [mirrored] [location=middle]} {\type{mirrored}\break\type{mirrored}}
% \stopcombination

% maybe we will have page-run.lua

\startluacode
local format, concat = string.format, table.concat
local todimen = number.todimen
local texdimen = tex.dimen

local function asdimen(name,unit)
    return todimen(texdimen[name],unit,"%0.4f") -- 4 is more than enough, even 3 would be okay
end

local function checkedoptions(options)
    if type(options) == "table" then
        return options
    elseif not options or options == "" then
        options = "pt,cm"
    end
    options = utilities.parsers.settings_to_hash(options)
    local n = 4
    for k, v in table.sortedhash(options) do
        local m = tonumber(k)
        if m then
            n = m
        end
    end
    options.n = n
    return options
end

function commands.showlayoutvariables(options)

    options = checkedoptions(options)

    local dimensions = { "pt", "bp", "cm", "mm", "dd", "cc", "pc", "nd", "nc", "sp", "in" }

    local n = 0
    for i=1,#dimensions do
        if options[dimensions[i]] then
            n = n + 1
        end
    end

    if n == 0 then
        options.pt = true
        n = 1
    end

    local function showdimension(name)
        context.NC()
        context.tex(interfaces.interfacedcommand(name))
        context.NC()
        for i=1,#dimensions do
            local d = dimensions[i]
            if options[d] then
                context("%s%s",asdimen(name,d),d)
                context.NC()
            end
        end
        context.NR()
    end

    local function showmacro(name)
        context.NC()
        context.tex(interfaces.interfacedcommand(name))
        context.NC()
        context.getvalue(name)
        context.NC()
        context.NR()
    end

    local function reportdimension(name)
        local result = { }
        for i=1,#dimensions do
            local d = dimensions[i]
            if options[d] then
                result[#result+1] = format("%12s%s",asdimen(name,d),d)
            end
        end
        commands.writestatus("layout",format("%-24s %s",interfaces.interfacedcommand(name),concat(result," ")))
    end

    if tex.count.textlevel == 0 then

        -- especially for Luigi:

        reportdimension("paperheight")
        reportdimension("paperwidth")
        reportdimension("printpaperheight")
        reportdimension("printpaperwidth")
        reportdimension("topspace")
        reportdimension("backspace")
        reportdimension("makeupheight")
        reportdimension("makeupwidth")
        reportdimension("topheight")
        reportdimension("topdistance")
        reportdimension("headerheight")
        reportdimension("headerdistance")
        reportdimension("textheight")
        reportdimension("footerdistance")
        reportdimension("footerheight")
        reportdimension("bottomdistance")
        reportdimension("bottomheight")
        reportdimension("leftedgewidth")
        reportdimension("leftedgedistance")
        reportdimension("leftmarginwidth")
        reportdimension("leftmargindistance")
        reportdimension("textwidth")
        reportdimension("rightmargindistance")
        reportdimension("rightmarginwidth")
        reportdimension("rightedgedistance")
        reportdimension("rightedgewidth")
        reportdimension("bodyfontsize")
        reportdimension("lineheight")

    else

        context.starttabulate { "|l|" .. string.rep("Tr|",n) }

            showdimension("paperheight")
            showdimension("paperwidth")
            showdimension("printpaperheight")
            showdimension("printpaperwidth")
            showdimension("topspace")
            showdimension("backspace")
            showdimension("makeupheight")
            showdimension("makeupwidth")
            showdimension("topheight")
            showdimension("topdistance")
            showdimension("headerheight")
            showdimension("headerdistance")
            showdimension("textheight")
            showdimension("footerdistance")
            showdimension("footerheight")
            showdimension("bottomdistance")
            showdimension("bottomheight")
            showdimension("leftedgewidth")
            showdimension("leftedgedistance")
            showdimension("leftmarginwidth")
            showdimension("leftmargindistance")
            showdimension("textwidth")
            showdimension("rightmargindistance")
            showdimension("rightmarginwidth")
            showdimension("rightedgedistance")
            showdimension("rightedgewidth")
            context.NR()
            showdimension("bodyfontsize")
            showdimension("lineheight")
            context.NR()
            showmacro("strutheightfactor")
            showmacro("strutdepthfactor")
            showmacro("topskipfactor")
            showmacro("maxdepthfactor")

        context.stoptabulate()

    end

end

function commands.showlayout(options)

    options = checkedoptions(options)

    if tex.count.textlevel == 0 then

        commands.showlayoutvariables(options)

    else

        context.page()
        context.bgroup()
        context.showframe()
        context.setuplayout { marking = interfaces.variables.on }
        for i=1,(options.n or 4) do
            commands.showlayoutvariables(options)
            context.page()
        end
        context.egroup()

    end

end
\stopluacode

\gdef\page_layouts_show_frame[#1][#2]%
  {\ifsecondargument
     \setupbackgrounds
       [\v!page]
       [\c!frame=\v!on,
        \c!corner=\v!rectangular,
        \c!frameoffset=\zeropoint,
        \c!framedepth=\zeropoint,
        \c!framecolor=layout:page]
     \setupbackgrounds
       [#1][#2]
       [\c!background=,
        \c!frame=\v!on,
        \c!corner=\v!rectangular,
        \c!frameoffset=\zeropoint,
        \c!framedepth=\zeropoint,
        \c!framecolor=]
   \else\iffirstargument
     \showframe
       [\v!header,\v!text,\v!footer]
       [#1]
   \else
     \showframe
       [\v!top,\v!header,
        \v!text,
        \v!footer,\v!bottom]
       [\v!leftedge,\v!leftmargin,
        \v!text,
        \v!rightmargin,\v!rightedge]
   \fi\fi
   \setupbackgrounds
     [\c!state=\v!repeat]}

\gdef\page_layouts_show_setups[#1]%
  {\ctxcommand{showlayoutvariables("#1")}}

\gdef\page_layouts_show_layout[#1]%
  {\ctxcommand{showlayout("#1")}}

\unexpanded\gdef\showframe {\dodoubleempty\page_layouts_show_frame }
\unexpanded\gdef\showsetups{\dosingleempty\page_layouts_show_setups}
\unexpanded\gdef\showlayout{\dosingleempty\page_layouts_show_layout}

\unexpanded\gdef\showmargins
  {\starttabulate
   \NC asynchrone   \NC \doifelseoddpage     {odd}   {even} \NC \NR
   \NC synchrone    \NC \doifelserightpage   {right} {left} \NC \NR
   \NC right margin \NC \the\rightmarginwidth               \NC \NR
   \NC left margin  \NC \the\leftmarginwidth                \NC \NR
   \NC outer margin \NC \the\outermarginwidth               \NC \NR
   \NC inner margin \NC \the\innermarginwidth               \NC \NR
   \stoptabulate}

\protect \endinput
