%D \module
%D   [       file=x-ldx,
%D        version=2008.06.03,
%D          title=\CONTEXT\ Modules,
%D       subtitle=Lua Source Pretty Printing,
%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.

% this will become an extra

\setupxml[default=hidden]

\usemodule[x][mathml]
\usemodule[abr-02]

\xmlregistersetup{xml:mml:define}
\xmlregistersetup{xml:ldx:define}

\xmlregisterns{ldx}{ldx}

\startxmlsetups xml:ldx:define
    \xmlsetsetup {#1} {ldx:*} {ldx:*}
\stopxmlsetups

% % %

\startxmlsetups ldx:p
    \xmlflush{#1}\par
\stopxmlsetups

\startxmlsetups ldx:source
    \source{\xmlflush{#1}}
\stopxmlsetups

\startxmlsetups ldx:key
    \dontleavehmode{\bf\xmlflush{#1}}
\stopxmlsetups

\startxmlsetups ldx:variable
    \xmlflush{#1}
%     \expanded{\variable{\xmlflush{#1}}}
\stopxmlsetups

\startxmlsetups ldx:function
    \dontleavehmode{\bf function}\space\xmlflush{#1}
%     \expanded{\function{\xmlflush{#1}}}
\stopxmlsetups

\startxmlsetups ldx:com
    \dontleavehmode{\tt--\xmlflush{#1}}
\stopxmlsetups

\startxmlsetups ldx:document
    \page
    \xmlflush{#1}
    \determineregistercharacteristics[function]
    \startmode[*register]
        \testpage[4]
        \extra{Functions}
        \placeregister[function]
    \stopmode
    \determineregistercharacteristics[variable]
    \startmode[*register]
        \testpage[4]
        \extra{Variables}
        \placeregister[variable]
    \stopmode
\stopxmlsetups

\newcounter\CommentCounter

\startxmlsetups ldx:comment
    \blank
    \doglobal\increment\CommentCounter
    \margintitle{\bf\CommentCounter}
    \xmlflush{#1}
    \blank
\stopxmlsetups

\startxmlsetups ldx:dqs
    \dontleavehmode\bgroup\tt"\xmlflush{#1}"\egroup
\stopxmlsetups

\startxmlsetups ldx:sqs
    \dontleavehmode\bgroup\tt'\xmlflush{#1}'\egroup
\stopxmlsetups

\startxmlsetups ldx:code
    \startpacked
    \xmlflush{#1}\relax
    \stoppacked
\stopxmlsetups

\startluacode
    function xml.finalizers.tex.cdatatobuffer(c,name)
        buffers.assign(name,xml.cdata(c[1]))
    end
\stopluacode

\startxmlsetups ldx:luacode
    \blank
    \begingroup
    \switchtobodyfont[dejavu-condensed]
    \xmlfilter{#1}{./cdatatobuffer('name')}
    \scitebuffer[lua][name]
    \endgroup
    \blank
\stopxmlsetups

\startxmlsetups ldx:lines
    \startpacked
    \xmlflush{#1}
    \stoppacked
\stopxmlsetups

\startxmlsetups ldx:line
    \doifelsenothing {\xmlflush{#1}} {
        \xmlflush{#1}\crlf
    } {
        \dontleavehmode \hbox to \hsize \bgroup
            \strut
            \hskip.25\dimexpr\xmlattdef{#1}{n}{0}em\relax\relax % extra relax needed !
            \doif {\xmlatt{#1}{comment}} {yes} {\tt}
            \xmlflush{#1}
            \hss
       \egroup
       \endgraf
    }
\stopxmlsetups

\startxmlsetups ldx:logo
    \uppercasestring\xmlatt{#1}{label}\xmlatt{#1}{name}\to\ascii
    \ifx\ascii\empty\else\getvalue{\ascii}\fi
\stopxmlsetups

\startxmlsetups ldx:l
    \uppercasestring\xmlatt{#1}{l}\xmlatt{#1}{n}\to\ascii
    \ifx\ascii\empty\else\getvalue{\ascii}\fi
\stopxmlsetups

\startxmlsetups ldx:typing
    \blank
    \startpacked \tt
    \xmlverbatim{#1}
    \stoppacked
    \blank
\stopxmlsetups

\startxmlsetups ldx:type
    \dontleavehmode{\tt\xmlflush{#1}}
\stopxmlsetups

\startxmlsetups ldx:t
    \dontleavehmode{\tt\xmlflush{#1}}
\stopxmlsetups

% key     -> kw
% dqs     -> dq
% sqs     -> sq
% line    -> ln
% code    -> cd
% comment -> tx (text)

\usemodule[scite]

\switchtobodyfont
  [dejavu-condensed,10pt] % preload

\setupbodyfont
  [dejavu,10pt] % main font

\mainlanguage
  [en]

\setupwhitespace
  [big]

\defineregister[function]
\defineregister[variable]

\definehead[source][subject]
\definehead[extra] [subsubject]
\definehead[topic] [subsubsubject]

\setuphead
  [source]
  [style=\bfb]

\setuphead
  [extra]
  [style=\bfa]

\setuphead
  [topic]
  [style=\bf]

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

\doifmode {nocolor} {

  \setupcolors
    [conversion=always]

}
\endinput

