%D \module
%D   [      file=x-e-0302,
%D        version=2003.01.05,
%D          title=\EXAMPLE\ Base Files,
%D       subtitle=Letters,
%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.

%D This is experimental code. Don't change it, don't rely on
%D this interface remaining the same, don't use tricks used
%D here but not described in manuals, so: assume that this
%D file does not exists.

\usemodule[e-0000]
\usemodule[letter]
\usemodule[corres]
\usemodule[contml] \autoXMLnamespace[context] \useXMLfilter[ent]

\usemodule[b][e-0302]

\loadEXArequest{\inputfilename}
\loadEXAmodes  {examodes.tex}

% \doifelsenothing{\mainEXAfilename}{\def\mainEXAfilename{x-corres.xml}}

%D Interface to m-letter

\doifsomething{\getEXAvariable{tex:var:language}}
  {\mainlanguage[\getEXAvariable{tex:var:language}]}

\doifelsenothing{\getEXAvariable{tex:text:date}}
  {\setvariables[letter:data][date=\currentdate]}
  {\setvariables[letter:data][date=\getEXAvariable{tex:text:date}]}

\doifelsenothing{\getEXAvariable{tex:text:reference}}
  {\setvariables[letter:data][reference=\referraldate]}
  {\setvariables[letter:data][reference=\getEXAvariable{tex:text:reference}]}

\setvariables
  [letter:data]
  [   subject=\getEXAvariable{tex:text:subject},
       author=\getEXAvariable{tex:text:author},
     concerns=\getEXAvariable{tex:text:concerns},
    residence=\getEXAvariable{tex:text:residence},
      opening=\getEXAvariable{tex:text:opening},
     greeting=\getEXAvariable{tex:text:greeting},
   appendices=]

\setvariables
  [letter:head]
  [graphic=\getEXAvariable{tex:var:pagegraphic}]

\setvariables
  [letter:style]
  [alternative=\getEXAvariable{tex:var:alternative}]

\setvariables
  [letter:data]
  [   address=\XMLflush{address},
       prefix=\XMLflush{prefix},
       suffix=\XMLflush{suffix},
     initials=\XMLflush{initials},
   formalname=\XMLflush{formalname},
 informalname=\XMLflush{informalname}]

\setvariables
  [letter:data]
  [content=\getEXAvariable{tex:text:content}]

\startmode[contenttype-tex]

  \setvariables
    [letter:data]
    [content=\getEXAvariableTEX{tex:text:content}]

\stopmode

\startmode[contenttype-xml]

  \setvariables
    [letter:data]
    [content=\getEXAvariableXML{tex:text:content}]

\stopmode

\startsetups[standard]

  \setvariables
    [letter:data]
    [address=\getEXAvariableXML{tex:text:address}]

  \setups[letter:place]

\stopsetups

\startsetups[address]

  \setvariables
    [contacts]
    [selection={\getvariable{selection}{label}}]

  \setvariables
    [letter:data]
    [address=\XMLflush{address}]

  \startlocalsetups[contact:handle]
    \setups[letter:place]
  \stoplocalsetups

  \XMLprocesscontacts[\mainEXAfilename]

\stopsetups

\startmode[test-yes]
  \setups[letter:test]
\stopmode

\startmode[grid-yes]
  \showgrid \tracelayerstrue
\stopmode

\usemodule[a][e-0302]


\checkvariables[letter][nofcopies=1]

\starttext

  \dorecurse{\getvariable{letter}{nofcopies}}
    {\doifelsenothing{\mainEXAfilename}
       {\setups[standard]}
       {\doifmodeelse{test-yes}
          {\setups[standard]}
          {\setups[address]}}}

\stoptext
