\definebuffer[epigraph]

\setupbuffer[epigraph][before=\dostartplaceepigraph,
                       after=\dostopplaceepigraph]

\let\realstartepigraph=\startepigraph

\definesystemvariable{eg} %EpiGraphs

\startconstants all
        separator: separator
     beforesource: beforesource
      sourcestyle: sourcestyle
      sourcecolor: sourcecolor
      authorstyle: authorstyle
      authorcolor: authorcolor
      afterauthor: afterauthor
      beforetitle: beforetitle
       titlestyle: titlestyle
       titlecolor: titlecolor
       aftertitle: aftertitle
       beforenote: beforenote
        notestyle: notestyle
        notecolor: notecolor
             note: note
      aftersource: aftersource
\stopconstants

\unprotect

\long\def\defaultepigraphseparator{\thinrule}
\def\defaultepigraphbefore%
    {\blank[big]%
     \setupnarrower[left=.6\textwidth]%
     \setuptolerance[tolerant]%
     \startnarrower[left]}
\def\defaultepigraphafter%
    {\stopnarrower\blank[medium]}

\def\defaultepigraphsourcebefore%
    {\setupalign[left]}
\long\def\defaultepigraphsourceafter%
    {\par}

\def\defaultepigraphpartsseparator%
    {\penalty1000\hskip1ex}

\def\defaultepigraphsourcestyle{\tfx}

\def\setupepigraph{\dosingleempty\dosetupepigraph}

\def\dosetupepigraph[#1]%
    {\getparameters[\??eg]
        [\c!voor=\defaultepigraphbefore,
         \c!na=\defaultepigraphafter,
         \c!tekstletter=\tfx,
         \c!tekstkleur=,
         \c!separator=\defaultepigraphseparator,
         \c!beforesource=\defaultepigraphsourcebefore,
         \c!aftersource=\defaultepigraphsourceafter,
         \c!sourcestyle=\defaultepigraphsourcestyle,
         \c!sourcecolor=,
         \c!authorstyle=\v!vet,
         \c!authorcolor=,
         \c!afterauthor={,},
         \c!beforetitle=\defaultepigraphpartsseparator,
         \c!titlestyle=\v!italic,
         \c!titlecolor=,
         \c!aftertitle={,},
         \c!beforenote=\defaultepigraphpartsseparator,
         \c!notestyle=,
         \c!notecolor=,
         #1]%
    }

\setupepigraph

\def\startepigraph{\dosingleempty\dostartepigraph}

\def\dostartepigraph[#1]%
    {\getparameters[\??eg][\c!auteur=Anonymous,\c!titel=,\c!note=,#1]%
     \realstartepigraph}

\def\dostartplaceepigraph%
    {\bgroup%
     \getvalue{\??eg\c!voor}%
     \dostartattributes{\??eg}{\c!tekstletter}{\c!tekstkleur}\relax%
     \setupinterlinespace%
     }
\long\def\dostopplaceepigraph%
    {\par\dostopattributes\page[no]%
     \getvalue{\??eg\c!separator}\par\page[no]%
     \getvalue{\??eg\c!beforesource}%
     \dostartattributes{\??eg}{\c!tekstletter}{\c!tekstkleur}\relax%
     \setupinterlinespace%
     \placeepigraphsource{\getvalue{\??eg\c!auteur}}{\getvalue{\??eg\c!titel}}{\getvalue{\??eg\c!note}}%
     \par\dostopattributes%
     \getvalue{\??eg\c!aftersource}%
     \getvalue{\??eg\c!na}%
     \egroup}

\newcount\whattoplaceinepigraphsource

\def\placeepigraphsource#1#2#3%
     {\whattoplaceinepigraphsource=0\relax%
      \edef\theauthor{#1}%
      \edef\thetitle{#2}%
      \edef\thenote{#3}%
      \doifelse{\theauthor}{}{}{\advance\whattoplaceinepigraphsource4}%
      \doifelse{\thetitle}{}{}{\advance\whattoplaceinepigraphsource2}%
      \doifelse{\thenote}{}{}{\advance\whattoplaceinepigraphsource1}%
      \relax%\showthe\whattoplaceinepigraphsource
      \ifcase\whattoplaceinepigraphsource\relax%
          \or\epigraphsourceplacenote%
          \or\epigraphsourceplacetitle%
          \or\epigraphsourceplacetitlenote%
          \or\epigraphsourceplaceauthor%
          \or\epigraphsourceplaceauthornote%
          \or\epigraphsourceplaceauthortitle%
          \or\epigraphsourceplaceauthortitlenote%
      \fi}

\def\epigraphsourceplacenote%
     {\getvalue{\??eg\c!beforenote}%
      \doattributes{\??eg}\c!notestyle\c!notecolor%
                   {\getvalue{\??eg\c!note}}}%

\def\epigraphsourceplacetitle%
     {\getvalue{\??eg\c!beforetitle}%
      \doattributes{\??eg}\c!titlestyle\c!titlecolor%
                   {\getvalue{\??eg\c!titel}}}%

\def\epigraphsourceplacetitlenote%
     {\getvalue{\??eg\c!beforetitle}%
      \doattributes{\??eg}\c!titlestyle\c!titlecolor%
                   {\getvalue{\??eg\c!titel}}%
      \getvalue{\??eg\c!aftertitle}%
      \getvalue{\??eg\c!beforenote}%
      \doattributes{\??eg}\c!notestyle\c!notecolor%
                   {\getvalue{\??eg\c!note}}}%

\def\epigraphsourceplaceauthor%
     {\doattributes{\??eg}\c!authorstyle\c!authorcolor%
                   {\getvalue{\??eg\c!auteur}}}%

\def\epigraphsourceplaceauthornote%
     {\doattributes{\??eg}\c!authorstyle\c!authorcolor%
                   {\getvalue{\??eg\c!auteur}}%
      \getvalue{\??eg\c!afterauthor}%
      \getvalue{\??eg\c!beforenote}%
      \doattributes{\??eg}\c!notestyle\c!notecolor%
                   {\getvalue{\??eg\c!note}}}%

\def\epigraphsourceplaceauthortitle%
     {\doattributes{\??eg}\c!authorstyle\c!authorcolor%
                   {\getvalue{\??eg\c!auteur}}%
      \getvalue{\??eg\c!afterauthor}%
      \getvalue{\??eg\c!beforetitle}%
      \doattributes{\??eg}\c!titlestyle\c!titlecolor%
                   {\getvalue{\??eg\c!titel}}}%

\def\epigraphsourceplaceauthortitlenote%
     {\doattributes{\??eg}\c!authorstyle\c!authorcolor%
                   {\getvalue{\??eg\c!auteur}}%
      \getvalue{\??eg\c!afterauthor}%
      \getvalue{\??eg\c!beforetitle}%
      \doattributes{\??eg}\c!titlestyle\c!titlecolor%
                   {\getvalue{\??eg\c!titel}}%
      \getvalue{\??eg\c!aftertitle}%
      \getvalue{\??eg\c!beforenote}%
      \doattributes{\??eg}\c!notestyle\c!notecolor%
                   {\getvalue{\??eg\c!note}}}%


\protect

\starttext

\setupcolors[state=start]

\startepigraph[author={Hans Hagen},note=2000]
The fact that some installation programs ask you if you want to
install the help info as {\em compiled \kap{html}} or {\em pure
\kap{html}} makes me wonder if a next version of those programs
will be distributed as pure \kap{C++} code.     
\stopepigraph  

\getepigraph

\input tufte

\startepigraph[author={Donald Arsenau},
               title={post on {\tt comp.text.tex}},
               note={May the 15th, 2001}]
\quotation{Acrobat} is so named because of the contortions one must do to
get around its bugs.
\stopepigraph  

\getepigraph

\input douglas

\startepigraph[author={Pink Floyd},
               title={Eclypse},
               note={The~Dark Side of the Moon}]
Everything under the sun is in tune\crlf
But the sun is eclypsed by the moon
\stopepigraph

\getepigraph

\stoptext
