%D \module
%D   [     file=s-cor-02,
%D      version=2012.06.10,
%D        title=\CONTEXT\ User Module,
%D     subtitle=Memos,
%D       author=Wolfgang Schuster,
%D         date=\currentdate,
%D    copyright=Wolfgang Schuster,
%D      license=GNU General Public License]

%C Copyright (C) 2011  Wolfgang Schuster
%C
%C This program is free software: you can redistribute it and/or modify
%C it under the terms of the GNU General Public License as published by
%C the Free Software Foundation, either version 3 of the License, or
%C (at your option) any later version.
%C
%C This program is distributed in the hope that it will be useful,
%C but WITHOUT ANY WARRANTY; without even the implied warranty of
%C MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
%C GNU General Public License for more details.
%C
%C You should have received a copy of the GNU General Public License
%C along with this program.  If not, see <http://www.gnu.org/licenses/>.

\unprotect

\startmodule[memo]

\usemodule[cor-00]

\setupmodule
  [\c!style=memo]

% Commands

\definecorrespondence[\v!memo]

\def\definememolayer       {\dodoubleargument\definecorrespondencelayer       [\v!memo]}
\def\definememosection     {\dodoubleargument\definecorrespondencesection     [\v!memo]}
\def\definememodescription {\dodoubleargument\definecorrespondencedescription [\v!memo]}

\def\definememoelements    {\dotripleargument\definecorrespondenceelements    [\v!memo]}
\def\setupmemoelements     {\dotripleargument\setupcorrespondenceelements     [\v!memo]}

\def\setupmemostyle        {\dotripleargument\setupcorrespondencestyle        [\v!memo]}
\def\setupmemolayer        {\dotripleargument\setupcorrespondencelayer        [\v!memo]}
\def\setupmemoframe        {\dotripleargument\setupcorrespondenceframe        [\v!memo]}
\def\setupmemolayout       {\dotripleargument\setupcorrespondencelayout       [\v!memo]}
\def\setupmemosection      {\dotripleargument\setupcorrespondencesection      [\v!memo]}
\def\setupmemodescription  {\dotripleargument\setupcorrespondencedescription  [\v!memo]}
\def\setupmemooptions      {\dodoubleargument\setupcorrespondenceoption       [\v!memo]}
\def\setupmemo             {\dodoubleargument\setupcorrespondence             [\v!memo]}

\def\usememostyle          {\dodoubleargument\loadcorrespondencefile          [\v!memo]}

\def\definememoelement     {\doquadrupleargument\definecorrespondenceelement  [\v!memo]}
\def\memoelement           {\doquadrupleargument\placecorrespondenceelement   [\v!memo]}

\def\namedmemolayerparameter  #1{\namedcorrespondencelayerparameter  {\v!memo:#1}}
\def\namedmemoframeparameter  #1{\namedcorrespondenceframeparameter  {\v!memo:#1}}
\def\namedmemosectionparameter#1{\namedcorrespondencesectionparameter{\v!memo:#1}}

% Layers

\definememolayer [\v!head]
\definememolayer [\v!nexthead]
\definememolayer [\v!lefthead]
\definememolayer [\v!righthead]

\definememolayer [\v!foot]
\definememolayer [\v!nextfoot]
\definememolayer [\v!leftfoot]
\definememolayer [\v!rightfoot]

\definememolayer [\v!topmark]
\definememolayer [\v!botmark]
\definememolayer [\v!cutmark]
\definememolayer [\v!endmark]
\definememolayer [\v!usermark]

\definememolayer [\v!memomain]
\definememolayer [\v!memonext]

% Section

\definememosection [\v!head]
\definememosection [\v!date]
\definememosection [\v!reference]
\definememosection [\v!specialnotation]
\definememosection [\v!address]
\definememosection [\v!title]
\definememosection [\v!opening]
\definememosection [\v!subject]
\definememosection [\v!content]
\definememosection [\v!closing]
\definememosection [\v!appendices]

% Descriptions

\definememodescription [\v!copy]
\definememodescription [\v!enclosure]
\definememodescription [\v!postscript]

% Setups

% layer: head

\definememoelement[\v!layer][\v!head][\v!setups]
  {\texsetup{\v!memo:\v!layer:\v!head}}

% layer: nexthead

\definememoelement[\v!layer][\v!nexthead][\v!setups]
  {\texsetup{\v!memo:\v!layer:\v!nexthead}}

% layer: lefthead

\definememoelement[\v!layer][\v!lefthead][\v!setups]
  {\texsetup{\v!memo:\v!layer:\v!lefthead}}

% layer: righthead

\definememoelement[\v!layer][\v!righthead][\v!setups]
  {\texsetup{\v!memo:\v!layer:\v!righthead}}

% layer: foot

\definememoelement[\v!layer][\v!foot][\v!pagenumber]
  {\centerbox{\hbox{\begstrut\leftmemotext\c!pagenumber\subpagenumber\rightmemotext\c!pagenumber\lastsubpagenumber\endstrut}}}

% layer: topmark

\definememoelement[\v!layer][\v!topmark][\v!setups]
  {\texsetup{\v!memo:\v!layer:\v!topmark}}

% layer: botmark

\definememoelement[\v!layer][\v!botmark][\v!setups]
  {\texsetup{\v!memo:\v!layer:\v!botmark}}

% layer: cutmark

\definememoelement[\v!layer][\v!cutmark][\v!setups]
  {\texsetup{\v!memo:\v!layer:\v!cutmark}}

% layer: endmark

\definememoelement[\v!layer][\v!endmark][\v!setups]
  {\texsetup{\v!memo:\v!layer:\v!endmark}}

% layer: usermark

\definememoelement[\v!layer][\v!usermark][\v!setups]
  {\texsetup{\v!memo:\v!layer:\v!usermark}}

% layer: memomain

\definememoelement[\v!layer][\v!memomain][\v!setups]
  {\texsetup{\v!memo:\v!layer:\v!memomain}}

% layer: memonext

\definememoelement[\v!layer][\v!memonext][\v!setups]
  {\texsetup{\v!memo:\v!layer:\v!memonext}}

% section: head

\definememoelement[\v!section][\v!head][\s!default]
  {\def\\{\correspondencesectionparameter\c!separator}%
   \correspondenceparameter\c!fromaddress}

\definememoelement[\v!section][\v!head][\v!memo]
  {\def\\{\correspondencesectionparameter\c!separator}%
   \startpacked
   \doifsomething{\correspondenceparameter\c!fromname}{\memotext\c!fromname \correspondenceparameter\c!fromname \par}
   \doifsomething{\correspondenceparameter\c!date    }{\memotext\c!date     \correspondenceparameter\c!date     \par}
   \doifsomething{\correspondenceparameter\c!toname  }{\memotext\c!toname   \correspondenceparameter\c!toname   \par}
   \doifsomething{\correspondenceparameter\c!subject }{\memotext\c!subject  \correspondenceparameter\c!subject  \par}
   \stoppacked}

\unexpanded\def\doformatmemoheadTABLE#1%
  {\doifsomething{\correspondenceparameter#1}
     {\NC % label
        \def\currentcorrespondencestyle{\v!memo:#1}%
        \usecorrespondencestylestyleandcolor\c!titlestyle\c!titlecolor
        \memotext#1
      \EQ % text
        \def\currentcorrespondencestyle{\v!memo:#1}%
        \usecorrespondencestylestyleandcolor\c!textstyle\c!textcolor
        \correspondenceparameter#1
      \NC\NR}}

\definememoelement[\v!section][\v!head][\v!table]
  {\def\\{\correspondencesectionparameter\c!separator}%
   \starttabulate[|l|p|]
   \doformatmemoheadTABLE\v!fromname
   \doformatmemoheadTABLE\v!date
   \doformatmemoheadTABLE\v!toname
   \doformatmemoheadTABLE\v!subject
   \stoptabulate}

\unexpanded\def\doformatmemoheadMARGIN#1%
  {\doifsomething{\correspondenceparameter#1}
     {\def\currentcorrespondencestyle{\v!memo:#1}%
      \dontleavehmode\llap\bgroup % label
         \usecorrespondencestylestyleandcolor\c!titlestyle\c!titlecolor
         \memotext#1
         \hskip\leftmargindistance
      \egroup
      \bgroup
        \usecorrespondencestylestyleandcolor\c!textstyle\c!textcolor
        \correspondenceparameter#1
      \egroup
      \par}}

\definememoelement[\v!section][\v!head][\v!margin]
  {\def\\{\correspondencesectionparameter\c!separator}%
   \startpacked
   \doformatmemoheadMARGIN\c!fromname
   \doformatmemoheadMARGIN\c!date
   \doformatmemoheadMARGIN\c!toname
   \doformatmemoheadMARGIN\c!subject
   \stoppacked}

\definememoelement[\v!section][\v!head][\v!setups]
  {\texsetup{\v!memo:\v!section:\v!head}}

% section: date

\definememoelement[\v!section][\v!date][\s!default]
  {\correspondenceparameter\c!date}

\definememoelement[\v!section][\v!date][\v!setups]
  {\texsetup{\v!memo:\v!section:\v!date}}

% section: reference

\definememoelement[\v!section][\v!reference][\s!default]
  {\def\\{\correspondencesectionparameter\c!separator}%
   \correspondenceparameter\c!reference}

\definememoelement[\v!section][\v!reference][\v!setups]
  {\texsetup{\v!memo:\v!section:\v!reference}}

% section: specialnotation

\definememoelement[\v!section][\v!specialnotation][\s!default]
  {\def\\{\correspondencesectionparameter\c!separator}%
   \correspondenceparameter\c!dispatch}

\definememoelement[\v!section][\v!specialnotation][\v!setups]
  {\texsetup{\v!memo:\v!section:\v!specialnotation}}

% section: address

\definememoelement[\v!section][\v!address][\s!default]
  {\def\\{\correspondencesectionparameter\c!separator}%
   \correspondenceparameter\c!toname
   \doifsomething{\correspondenceparameter\c!toname}\\
   \correspondenceparameter\c!toaddress}

\definememoelement[\v!section][\v!address][\v!setups]
  {\texsetup{\v!memo:\v!section:\v!address}}

% section: title

\definememoelement[\v!section][\v!title][\s!default]
  {\def\\{\correspondencesectionparameter\c!separator}%
   \correspondenceparameter\c!title}

\definememoelement[\v!section][\v!title][\v!setups]
  {\texsetup{\v!memo:\v!section:\v!title}}

% section: opening

\definememoelement[\v!section][\v!opening][\s!default]
  {\def\\{\correspondencesectionparameter\c!separator}%
   \correspondenceparameter\c!opening}

\definememoelement[\v!section][\v!opening][\v!setups]
  {\texsetup{\v!memo:\v!section:\v!opening}}

% section: subject

\definememoelement[\v!section][\v!subject][\s!default]
  {\def\\{\correspondencesectionparameter\c!separator}%
   \memotext\c!subject\correspondenceparameter\c!subject}

\definememoelement[\v!section][\v!subject][\v!setups]
  {\texsetup{\v!memo:\v!section:\v!subject}}

% section: content

\definememoelement[\v!section][\v!content][\s!default]
  {\setupbuffer[\c!before=,\c!after=]%
   \getbuffer  [\????correspondencebuffer\v!memo]}

\definememoelement[\v!section][\v!content][\v!setups]
  {\texsetup{\v!memo:\v!section:\v!content}}

% section: closing

\definememoelement[\v!section][\v!closing][\s!default]
  {\def\\{\correspondencesectionparameter\c!separator}%
   \correspondenceparameter\c!closing
   \doifsomething{\correspondenceparameter\c!signature}
     {\blank[\correspondencesectionparameter\c!spaceinbetween]%
      \correspondenceparameter\c!signature}}

\definememoelement[\v!section][\v!closing][\v!setups]
  {\texsetup{\v!memo:\v!section:\v!closing}}

% section: appendices

\definememoelement[\v!section][\v!appendices][\s!default]
  {\def\\{\correspondencesectionparameter\c!separator}%
   \startpacked
   \expanded{\processcommalistwithparameters[\accesscorrespondenceelements\v!memo\v!description]}{\placecorrespondencedescription[\v!memo]}%
   \stoppacked}

\startsetups[\v!memo:\v!section:\v!appendices]
\def\\{\correspondencesectionparameter\c!separator}
\memoelement[\v!section][\v!appendices][\correspondencesectionparameter\c!alternative]
\stopsetups

\startsetups[\v!memo:\v!place]
\placecorrespondence[\v!memo]
\stopsetups

% Extras

\appendtoks
  \ifx\currentcorrespondence\v!memo
    \processallactionsinset
      [\correspondenceoptionparameter\c!option]
      [  \v!test=>{\usememostyle[test]},
       \v!setups=>{\usememostyle[setups]}]%
  \fi
\to \everybeforecorrespondence

\appendtoks
  \ifx\currentcorrespondence\v!memo
    \doif{\correspondenceoptionparameter\c!marking}\v!no{\setupmemolayer[\v!topmark,\v!botmark,\v!cutmark,\v!endmark,\v!usermark][\c!state=\v!stop]}%
  \fi
\to \everybeforecorrespondence

% Labels

\def\????memotext{@@@@memotext}

\def\setupmemotext
  {\dodoubleempty\dosetupmemotext}

\def\dosetupmemotext[#1][#2]%
  {\ifsecondargument
     \def\dodosetupmemotext##1{\setuplabeltext[#1][\????memotext##1]}%
     \processcommalist[#2]\dodosetupmemotext
   \else
     \def\dodosetupmemotext##1{\setuplabeltext    [\????memotext##1]}%
     \processcommalist[#1]\dodosetupmemotext
   \fi}

\def\memotext     #1{\labeltext     {\????memotext#1}}
\def\leftmemotext #1{\leftlabeltext {\????memotext#1}}
\def\rightmemotext#1{\rightlabeltext{\????memotext#1}}

% pagenumber:

\setupmemotext[\s!en][\c!pagenumber={Page , of }]
\setupmemotext[\s!nl][\c!pagenumber=]
\setupmemotext[\s!de][\c!pagenumber={Seite , von }]
\setupmemotext[\s!fr][\c!pagenumber=]
\setupmemotext[\s!it][\c!pagenumber=]
\setupmemotext[\s!es][\c!pagenumber=]

% date:

\setupmemotext[\s!en][\c!date=Date: ]
\setupmemotext[\s!nl][\c!date=Datum: ]
\setupmemotext[\s!de][\c!date=Datum: ]
\setupmemotext[\s!fr][\c!date=Date\thinspace: ]
\setupmemotext[\s!it][\c!date=Data: ]
\setupmemotext[\s!es][\c!date=Fecha: ]

% fromname:

\setupmemotext[\s!en][\c!fromname=From: ]
\setupmemotext[\s!nl][\c!fromname=Van: ]
\setupmemotext[\s!de][\c!fromname=Von: ]
\setupmemotext[\s!fr][\c!fromname=De\thinspace: ]
\setupmemotext[\s!it][\c!fromname=Da: ]
\setupmemotext[\s!es][\c!fromname=De: ]

% toname:

\setupmemotext[\s!en][\c!toname=To: ]
\setupmemotext[\s!nl][\c!toname=Aan: ]
\setupmemotext[\s!de][\c!toname=An: ]
\setupmemotext[\s!fr][\c!toname=\Agrave\thinspace: ]
\setupmemotext[\s!it][\c!toname=A: ]
\setupmemotext[\s!es][\c!toname=A: ]

% subject:

\setupmemotext[\s!en][\c!subject=Subject: ]
\setupmemotext[\s!nl][\c!subject=Onderwerp: ]
\setupmemotext[\s!de][\c!subject=Betrifft: ]
\setupmemotext[\s!fr][\c!subject=Concernant\thinspace: ]
\setupmemotext[\s!it][\c!subject=Oggetto: ]
\setupmemotext[\s!es][\c!subject=Asunto: ]

% address:

\setupmemotext[\s!en][\c!address=Address]
\setupmemotext[\s!nl][\c!address=]
\setupmemotext[\s!de][\c!address=Adresse]
\setupmemotext[\s!fr][\c!address=]
\setupmemotext[\s!it][\c!address=]
\setupmemotext[\s!es][\c!address=]

% Files

\usememostyle[default,\currentmoduleparameter\c!style]

\stopmodule

\protect \endinput
