%D \module
%D   [      file=s-references-show.mkiv,
%D        version=2015.04.13,
%D          title=\CONTEXT\ Style File,
%D       subtitle=Reference Checking,
%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.

\startmodule[references-show]

\unprotect

% \enabletrackers[nodes.references,nodes.destinations] % also shows areas

\enabletrackers[nodes.references.show,nodes.destinations.show]

\enablehiddenbackground % trick

\edef\hiddenbackgroundlist{\hiddenbackgroundlist,trace-references}

\defineoverlay
  [trace-references]
  [\directsetup{trace:references:onpage}]

\startluacode
    local pagelist = structures.references.tracedpages

    function commands.getreferencesonpage(n)
        n = tonumber(n)
        if n  then
            local pagedata = pagelist[n]
            if pagedata then
                context("%s references",#pagedata)
                context.blank()
                for i=1,#pagedata do
                    local details   = pagedata[i]
                    local prefix    = details[1]
                    local reference = details[2]
                 -- local internal  = details[3]
                 -- context("%04i = %s : %s",internal,prefix == "" and "-" or prefix,reference)
                    context("%s : %s",prefix == "" and "-" or prefix,reference)
                    context.par()
                end
            else
                context("no references")
            end
        else
            context("no valid page")
        end
    end

\stopluacode

\definecolor
  [trace:references:onpage]
  [b=.5,a=1,t=.25]

\defineframed
  [trace:references:onpage]
  [\c!offset=2\exheight,
  %\c!foregroundstyle=\infofont,
   \c!frame=\v!off,
   \c!background=\v!color,
   \c!backgroundcolor=trace:references:onpage,
   \c!align=\v!normal]

\startsetups trace:references:onpage
    \vbox to \vsize \bgroup
        \infofont
        \vskip\dimexpr-\topspace-\headerheight+2\exheight\relax
        \hbox to \hsize \bgroup
            \doifoddpageelse\hss{\hskip\dimexpr-\cutspace+2\exheight\relax}%
            \directlocalframed [
                trace:references:onpage
            ] {
                \ctxcommand{getreferencesonpage(\the\realpageno)}
            }
            \doifoddpageelse{\hskip\dimexpr-\cutspace+2\exheight\relax}\hss
        \egroup
        \vss
    \egroup
\stopsetups

\protect

\continueifinputfile{s-references-show.mkiv}

\usemodule[art-01]

\setupinteraction
  [state=start]

\setuppagenumbering
  [alternative=doublesided]

\starttext

\title {Contents}

\placelist[chapter]

\setupreferenceprefix[zero]

\chapter[crap]{foo}

\setupreferenceprefix[one]

test \pagereference[whatever]

\dorecurse{5}{
  \placefigure
    [here][bar 1.#1]
    {xx}{\framed{xx #1}}
}

\setupreferenceprefix[two]

\dorecurse{5}{
  \placefigure
    [here][bar 2.#1]
    {xx}{\framed{xx #1}}
}

\in{checked}[bar 1.1]
\in{checked}[bar 2.1]

\stoptext
