%D \module
%D   [     file=t-fancybreak,
%D      version=2013.04.27,
%D        title=\CONTEXT\ User Module,
%D     subtitle=Fancybreak,
%D       author=Wolfgang Schuster,
%D         date=\currentdate,
%D    copyright=Wolfgang Schuster,
%D        email=schuster.wolfgang@googlemail.com,
%D      license=GNU General Public License]

%C Copyright (C) 2010  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 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/>.

%M \usemodule[fancybreak]
%M \loadsetups[t-fancybreak.xml]

%D \subject{Examples}
%D
%D \subsubject{The \letterbackslash fancybreak command}
%D
%D \starttyping
%D The Earth, as a habitat for animal life, is in old age …
%D \fancybreak{$* * *$}
%D The Earth, as a habitat for animal life, is in old age …
%D \stoptyping
%D
%D \input ward
%D \fancybreak{$* * *$}
%D \input ward
%D
%D \subsubject{The “spacebefore” and “spaceafter” keys}
%D
%D \starttyping
%D \setupfancybreak[spacebefore=line,spaceafter=line]
%D
%D The Earth, as a habitat for animal life, is in old age …
%D \fancybreak{$* * *$}
%D The Earth, as a habitat for animal life, is in old age …
%D \stoptyping
%D
%D \start
%D
%D \setupfancybreak[spacebefore=line,spaceafter=line]
%D
%D \input ward
%D \fancybreak{$* * *$}
%D \input ward
%D
%D \stop
%D
%D \subsubject{The argumentless version of the \letterbackslash fancybreak command}
%D
%D \starttyping
%D The Earth, as a habitat for animal life, is in old age …
%D \fancybreak
%D The Earth, as a habitat for animal life, is in old age …
%D \stoptyping
%D
%D \input ward
%D \fancybreak
%D \input ward
%D
%D \subsubject{The “spaceinbetween” key}
%D
%D \starttyping
%D \setupfancybreak[spaceinbetween=3*line]
%D
%D The Earth, as a habitat for animal life, is in old age …
%D \fancybreak
%D The Earth, as a habitat for animal life, is in old age …
%D \stoptyping
%D
%D \start
%D
%D \setupfancybreak[spaceinbetween=3*line]
%D
%D \input ward
%D \fancybreak
%D \input ward
%D
%D \stop
%D
%D \subsubject{The “symbol” key}
%D
%D \starttyping
%D \setupfancybreak[symbol=star]
%D
%D The Earth, as a habitat for animal life, is in old age …
%D \fancybreak
%D The Earth, as a habitat for animal life, is in old age …
%D \stoptyping
%D
%D \start
%D
%D \setupfancybreak[symbol=star]
%D
%D \input ward
%D \fancybreak
%D \input ward
%D
%D \stop
%D
%D \subsubject{How to create new \letterbackslash fancybreak commands}
%D
%D \starttyping
%D \definefancybreak[starbreak]  [symbol=star]
%D \definefancybreak[squarebreak][symbol=square]
%D
%D The Earth, as a habitat for animal life, is in old age …
%D \starbreak
%D The Earth, as a habitat for animal life, is in old age …
%D \squarebreak
%D The Earth, as a habitat for animal life, is in old age …
%D \stoptyping
%D
%D \start
%D
%D \definefancybreak[starbreak]  [symbol=star]
%D \definefancybreak[squarebreak][symbol=square]
%D
%D \input ward
%D \starbreak
%D \input ward
%D \squarebreak
%D \input ward
%D
%D \stop

%D \subject{Implementation}

\writestatus{loading}{ConTeXt User Module / Fancybreak}

\unprotect

\startinterface all
  \setinterfacevariable {fancybreak} {fancybreak}
\stopinterface

\def\????fancybreak{@@@@fancybreak}

\def\fancybreakparameter#1{\csname\????fancybreak\currentfancybreak#1\endcsname}

%D \macros
%D   {setupfancybreak}
%D
%D \subsubject{\letterbackslash setupfancybreak}
%D
%D With the command \type{\setupfancybreak} one can change the settings of
%D a previous created command, the two argument version change only the values
%D of a certain command while the one argument version change the values of
%D all commands.
%D
%D \showsetup{setupfancybreak}

\unexpanded\def\setupfancybreak
  {\dodoubleempty\dosetupfancybreak}

\def\dosetupfancybreak[#1][#2]%
  {\ifsecondargument
     \dodoubleargumentwithset\dodosetupfancybreak[#1][#2]%
   \else
     \dodosetupfancybreak[][#1]%
   \fi}

\def\dodosetupfancybreak[#1][#2]%
  {\getparameters[\????fancybreak#1][#2]}

%D \macros
%D   {definefancybreak}
%D
%D \subsubject{\letterbackslash definefancybreak}
%D
%D The command \type{\fancybreak} is not hardcoded in the module but created with
%D \type{\definefancybreak}. The command takes two arguments, the first is the name
%D of the new command and the second parameter to change the style of the commands
%D content and distances to the surrounding text.
%D
%D In \MKII\ users can only change the spacing before and after the content and
%D the alignment of it while \MKIV\ user can also change the style and color of it.
%D
%D \showsetup{definefancybreak}

\unexpanded\def\definefancybreak
  {\dodoubleempty\dodefinefancybreak}

\def\dodefinefancybreak[#1][#2]%
  {\copyparameters
     [\????fancybreak#1][\????fancybreak]
     [\c!spacebefore,\c!spaceafter,\c!spaceinbetween,\c!align,\c!indentnext,\c!symbol]%
   \getparameters[\????fancybreak#1][#2]%
   \setvalue        {#1}{\dodoubleempty\dofancybreak     [#1]}%
   \setvalue{\e!start#1}{\dodoubleempty\dostartfancybreak[#1]}%
   \setvalue{\e!stop #1}{\dostopfancybreak                   }}

%D \macros
%D   {fancybreak}
%D
%D \subsubject{\letterbackslash fancybreak}
%D
%D The main macro of the module is the \type{\fancybreak} command (or a user defined
%D variant of it), it takes one argument which can be optional.
%D
%D \showsetup{fancybreak}
%D \showsetup{startfancybreak}

\def\dofancybreak[#1][#2]%
  {\par
   \begingroup
   \edef\currentfancybreak{#1}%
   \ifsecondargument
     \doifassignmentelse{#2}
       {\getparameters[\????fancybreak\currentfancybreak][#2]}
       {\setvalue{\????fancybreak\currentfancybreak\c!spaceinbetween}{#2}}%
   \fi
   \doifnextbgroupelse\dodofancybreak\nodofancybreak}

\def\dodofancybreak
  {\forgetall % \forgetparindent
   \dowithnextboxcontent
     {\setupalign[\fancybreakparameter\c!align]}
     {\blank[\fancybreakparameter\c!spacebefore]%
      \flushnextbox
      \blank[\fancybreakparameter\c!spaceafter ]%
      \checknextindentation[\fancybreakparameter\c!indentnext]%
      \endgroup
      \dorechecknextindentation}
     \vbox}

\def\nodofancybreak
  {\doifsymboldefinedelse{\fancybreakparameter\c!symbol}
     {\dodofancybreak{\symbol[\fancybreakparameter\c!symbol]}}
     {\blank[\fancybreakparameter\c!spaceinbetween]%
      \checknextindentation[\fancybreakparameter\c!indentnext]%
      \endgroup
      \dorechecknextindentation}}

\def\dostartfancybreak[#1][#2]%
  {\par
   \begingroup
   \edef\currentfancybreak{#1}%
   \ifsecondargument
     \getparameters[\????fancybreak#1][#2]%
   \fi
   \dodofancybreak\bgroup}

\def\dostopfancybreak
  {\egroup}

\definefancybreak[\v!fancybreak]

\setupfancybreak
  [   \c!spacebefore=,
       \c!spaceafter=\v!nowhite,
   \c!spaceinbetween=\v!line,
            \c!align=\v!middle,
       \c!indentnext=\v!yes,
           \c!symbol=]

\protect \endinput
