% engine=luatex

%D \module
%D   [       file=mtx-context-listing,
%D        version=2008.11.10, % about that time i started playing with this
%D          title=\CONTEXT\ Extra Trickry,
%D       subtitle=Listing Files,
%D         author=Hans Hagen,
%D           date=\currentdate,
%D      copyright=\PRAGMA]
%C
%C This module is part of the \CONTEXT\ macro||package and is
%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
%C details.

%D This is a \TEXEXEC\ features that has been moved to \MKIV.

% begin help
%
% usage: context --extra=listing [options] list-of-files
%
% --sort                : sort filenames first
% --topspace=dimension  : distance above first line
% --backspace=dimension : distance before left margin
% --pretty              : pretty print comform suffix (temporarily disabled)
%
% end help

\setupbodyfont
  [11pt,tt]

\setuplayout
  [header=0cm,
   footer=1.5cm,
   width=middle,
   height=middle]

% todo: use \arguments{topspace}

\startluacode
    local topspace = document.arguments["topspace"] or 0
    if dimen(topspace) > dimen(0) then
        tex.sprint(string.format("\\setuplayout[topspace=%s]",dimen(topspace)))
    end
    local backspace = document.arguments["backspace"] or 0
    if dimen(topspace) > dimen(0) then
        tex.sprint(string.format("\\setuplayout[backspace=%s]",dimen(backspace)))
    end
\stopluacode

\setuptyping
  [lines=yes]

\setuptyping
  [option=color]

\starttext

\startluacode
    if #document.files > 0 then
        if document.arguments["sort"] then
            table.sort(document.files)
        end
        for _, filename in ipairs(document.files) do
            if not string.find(filename,"^mtx%-context%-") then
                tex.sprint("\\page\n")
                tex.sprint(string.format("\\setupfootertexts[\\detokenize{%s}][\\pagenumber]\n",file.basename(filename)))
                tex.sprint(string.format("\\typefile{%s}",filename))
            end
        end
    else
        tex.sprint(tex.ctxcatcodes,"no files given")
    end
\stopluacode

\stoptext
