%D \module
%D   [       file=m-letter,
%D        version=2002.01.05, % was docs-bri: 1995.10.10,
%D          title=\CONTEXT\ Extra Modules,
%D       subtitle=Framework for Letters,
%D         author=J. Hagen,
%D           date=\currentdate,
%D      copyright={PRAGMA / Hans Hagen \& Ton Otten}]
%C
%C This module is part of the \CONTEXT\ macro||package and
%C is therefore copyrighted by \PRAGMA. See mreadme.pdf for
%C details.

%D This is a preliminary module.

\usemodule[b][letter]

%D Layout:

\setuplayout
  [grid=yes,
   topspace=2cm,
   bottomspace=2cm,
   backspace=2cm,
   header=0pt,
   footer=1cm,
   width=middle,
   height=middle]

\startmode[*letter]

  \setuplayout
    [topspace=1in,
     bottomspace=1in,
     backspace=1in,
     footer=1in]

\stopmode

\setupwhitespace
  [big]

%D Color

\setupcolors
  [state=start]

%D Components - Address

\setvariables
  [letter:address]
  [line=3,
   hoffset=0pt,
   width=25em,
   noflines=10]

\definelayer
  [address]
  [method=text,
   location=grid,
   width=\textwidth,
   height=\getvariable{letter:address}{noflines}\lineheight]

\startsetups [letter:place:address]

  \setlayer
    [address]
    [line=\getvariable{letter:address}{line},
     hoffset=\getvariable{letter:address}{hoffset}]
    {\vtop
       {\hsize\getvariable{letter:address}{width}
        \setups[letter:address]}}

  % we need to use the [page] directive because layers
  % (in grid mode) compensate for the topskip, as does
  % the page key in the snapper

  \snaptogrid[page]\vbox{\placelayer[address]}

\stopsetups

%D Components - Reference

% todo: noflines=3 when no reference

\setvariables
  [letter:reference]
  [line=1,
   noflines-min=2,
   noflines-max=5,
   noflines=\getvariable{letter:reference}{noflines-max}]

\definelayer
  [reference]
  [method=text,
   location=grid,
   width=\textwidth,
   height=\getvariable{letter:reference}{noflines}\lineheight]

\startsetups [letter:place:reference]

  \doifelsenothing{\getvariable{letter:data}{concerns}
                   \getvariable{letter:data}{subject}}
    {\setvariables[letter:reference][noflines=\getvariable{letter:reference}{noflines-min}]}
    {\setvariables[letter:reference][noflines=\getvariable{letter:reference}{noflines-max}]}

  \setlayer
    [reference]
    [line=\getvariable{letter:reference}{line}]
    {\vtop
       {\hsize\textwidth
        \setups[letter:reference]}}

  % we need to use the [page] directive because layers
  % (in grid mode) compensate for the topskip, as does
  % the page key in the snapper

   \snaptogrid[page]\vbox{\placelayer[reference]}

\stopsetups

%D Components - Opening

\startsetups [letter:place:opening]
  \endgraf \setups[letter:opening] \endgraf
\stopsetups

%D Components - Content

\startsetups[letter:place:content]
  \bgroup \disableXML \endgraf \setups[letter:content] \endgraf \egroup
\stopsetups

% or, in case of xml:
%
% \startsetups[letter:place:content]
%   \endgraf \setups[letter:content] \endgraf
% \stopsetups

%D Components - Closing

\startsetups [letter:place:closing]
  \endgraf \setups[letter:closing] \endgraf
\stopsetups

%D Components - Appendices

\startsetups[letter:place:appendices]
  \endgraf \setups[letter:appendices] \endgraf
\stopsetups

%D Components - Head

\setvariables
  [letter:head]
  [graphic=] % [graphic=koe.pdf]

\setupbackgrounds
  [page]
  [background={graphic,letternext,lettermain}] % next before main, permits common ornaments

\definelayer
  [lettermain]
  [width=\paperwidth,
   height=\paperheight]

\definelayer
  [letternext]
  [state=continue,
   width=\paperwidth,
   height=\paperheight]

\defineoverlay
  [graphic]
  [\doifsomething{\getvariable{letter:head}{graphic}}
     {\overlayfigure{\getvariable{letter:head}{graphic}}}]

%D Dummies

\startsetups[letter:test]

  \startlocalsetups [letter:head]
    % nothing
  \stoplocalsetups

  \startlocalsetups[letter:address]
    A Fancy Name \endgraf
    A Nice Address \endgraf
    The Place To Go
  \stoplocalsetups

  \startlocalsetups[letter:reference]
    \rightaligned{\currentdate}
  \stoplocalsetups

  \startlocalsetups[letter:opening]
    Hi There,
  \stoplocalsetups

  \startlocalsetups[letter:content]
    \dorecurse{2}{\input tufte \blank}
  \stoplocalsetups

  \startlocalsetups[letter:closing]
    That Was Me
  \stoplocalsetups

  \startlocalsetups[letter:appendices]
    % nothing
  \stoplocalsetups

\stopsetups

%D Initialization

\startsetups [letter:initialize]
  \noheaderandfooterlines
  \setuppagenumber[number=1]
\stopsetups

\startsetups [letter:finish]
  \page
\stopsetups

%D Plug in

\setvariables
  [letter:style]
  [opening=informal, % formal unknown (ls)
   closing=informal, % formal
   alternative=]     % just a key

%D The Letter

\startsetups [letter:place]

  \resetlayer[lettermain] \resetlayer[letternext] \setuplayer[letternext][state=continue]

  \setups [letter:initialize]
  \setups [letter:alternative:\getvariable{letter:style}{alternative}]
  \setups [letter:sequence]
  \setups [letter:finish]

\stopsetups

\startsetups [letter:sequence]

  \dontcomplain \blank[synchronize]

  \setups [letter:place:head]       \endgraf
  \setups [letter:place:address]    \endgraf
  \setups [letter:place:reference]  \endgraf
  \setups [letter:place:opening]    \endgraf
  \setups [letter:place:content]    \page[no]
  \setups [letter:place:closing]    \endgraf
  \setups [letter:place:appendices] \endgraf

\stopsetups

%D Test

% \starttext \setups [letter:place] \stoptext \endinput

%D Interface

\startsetups [letter:head]
  % nothing
\stopsetups

\setvariables
  [letter:data]
  [address=]

\startsetups[letter:address]
  \def\\{\endgraf}\getvariable{letter:data}{address}
\stopsetups

\setvariables
  [letter:data]
  [subject=,
   date=,
   concerns=,
   residence=,
   reference=]

\def\SomeTableEntry#1%
  {\doifsomething{\getvariable{letter:data}{#1}}
      {\doiftext{\labeltext{letter:table:#1}}
         {\NC\labeltext{letter:table:#1}\EQ\getvariable{letter:data}{#1}\NC\NR}}}

\startsetups[letter:reference:table]

  \setuptabulate[before=,after=] \blank[synchronize]

  \starttabulate[|l|p|]
    \SomeTableEntry {concerns}
    \SomeTableEntry {date}
    \SomeTableEntry {subject}
    \SomeTableEntry {reference}
  \stoptabulate

\stopsetups

\startsetups[letter:reference]

  \doifsomething{\getvariable{letter:data}{concerns}
                 \getvariable{letter:data}{subject}}
    {\bgroup
     \doifsomething{\getvariable{letter:data}{residence}}{\setvariables[letter:data][date=]}
     \setups[letter:reference:table]
     \egroup
     \blank}

  \doifsomething{\getvariable{letter:data}{residence}}
    {\noindent
     \hbox to \hsize
        {\hss
         \getvariable{letter:data}{residence},\space
         \getvariable{letter:data}{date}}}

\stopsetups

\setvariables
  [letter:data]
  [opening=,
   initials=,
   prefix=,
   suffix=,
   informalname=,
   formalname=]

\startsetups[letter:opening:unknown]

  \labeltext{letter:opening:unknown},

\stopsetups

\startsetups [letter:opening:informal]

 \doifelsenothing{\getvariable{letter:data}{informalname}}
   {\labeltext{letter:opening:unknown}}
   {\labeltext{letter:opening:informal}\space
    \getvariable{letter:data}{informalname}},

\stopsetups

\startsetups [letter:opening:formal]

 \doifelsenothing{\getvariable{letter:data}{formalname}}
   {\labeltext{letter:opening:unknown}}
   {\labeltext{letter:opening:formal}\space
    \doifsomething{\getvariable{letter:data}{prefix}}
      {\getvariable{letter:data}{prefix}\space}%
    \getvariable{letter:data}{formalname}},

\stopsetups

\startsetups[letter:opening]
  \setups[letter:opening:\getvariable{letter:style}{opening}]
\stopsetups

\setvariables
  [letter:data]
  [content={\getbuffer[texletter]}]

\def\startlettercontent
  {\startbuffer[texletter][startlettercontent][stoplettercontent]}

\startsetups[letter:content]
  \getvariable{letter:data}{content}
\stopsetups

\setvariables
  [letter:data]
  [author=,
   greeting=]

\startsetups[letter:closing:informal]

  \blank

  \doifelsenothing{\getvariable{letter:data}{greeting}}
    {\noindent\labeltext{letter:greeting:informal}}
    {\noindent\getvariable{letter:data}{greeting}},

  \blank

  \noindent\getvariable{letter:data}{author}

\stopsetups

\startsetups[letter:closing:formal]

  \blank

  \doifelsenothing{\getvariable{letter:data}{greeting}}
    {\noindent\labeltext{letter:greeting:formal}}
    {\noindent\getvariable{letter:data}{greeting}},

  \blank

  \noindent\getvariable{letter:data}{author}

\stopsetups

\startsetups[letter:closing]
  \setups[letter:closing:\getvariable{letter:style}{closing}]
\stopsetups

\setvariables
  [letter:data]
  [appendices=]

\startsetups[letter:appendices]
  \getvariable{letter:data}{appendices}
\stopsetups

%D Labels

\setuplabeltext
  [nl]
  [%letter:appendices=bijlage(n) : ,
   letter:subject=het onderwerp,
   letter:opening:formal=Geachte,
   letter:opening:informal=Beste,
   letter:opening:unknown=LS,
   letter:name:unknown=lezer,
   letter:greeting:informal=Een vriendelijke groet,
   letter:greeting:formal=Hoogachtend]

\setuplabeltext
  [en]
  [%letter:appendices=appendices : ,
   letter:subject=the topic,
   letter:opening:formal=Dear,
   letter:opening:informal=Dear,
   letter:opening:unknown=LS,
   letter:name:unknown=reader,
   letter:greeting:informal=Regards,
   letter:greeting:formal=Sincerely yours]

\setuplabeltext
  [de]
  [%letter:appendices=Beilagen : ,
   letter:subject=das Kennzeichen,
   letter:opening:formal=Sehr Geehrte,
   letter:opening:informal=Hallo,
   letter:opening:unknown=LS,
   letter:name:unknown=Leser,
   letter:greeting:informal=Mit freundlichen Gr{\SS}en,
   letter:greeting:formal=Hochachtungsvoll]

%D A few more:

\setuplabeltext
  [nl]
  [letter:table:concerns=betreft,
   letter:table:date=datum,
   letter:table:subject=kenmerk,
   letter:table:reference=referentie]

\setuplabeltext
  [en]
  [letter:table:concerns=concerns,
   letter:table:date=date,
   letter:table:subject=subject,
   letter:table:reference=reference]

%D Test

\usemodule[a][letter]

\endinput
 