%D \module
%D \module
%D   [       file=strc-mat,
%D        version=2008.10.20,
%D          title=\CONTEXT\ Structure Macros,
%D       subtitle=Math Numbering,
%D         author=Hans Hagen,
%D           date=\currentdate,
%D      copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
%C
%C This module is part of the \CONTEXT\ macro||package and is
%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
%C details.

\writestatus{loading}{ConTeXt Structure Macros / Math Numbering}

\registerctxluafile{strc-mat}{1.001}

% -- we have potential for captions
% -- this module will use the commandhandler
% -- key/value pairs will be added (I have no time now)

\unprotect

\setupformulas
  [%\c!way=,
   %\c!blockway=,
   %\c!sectionnumber=,
   %\c!conversion=\v!numbers,
   %\c!numberstyle=,
   %\c!numbercolor=,
   %\c!numbercommand=,
   %\c!margin=,
   %\c!align=,
   %\c!separator=,
   %\c!grid=,
   \c!location=\v!right,
   \c!left=(,
   \c!right=),
   \c!expansion=\v!yes, % maybe automatically
   \c!spacebefore=\v!big,
   \c!spaceafter=\formulaparameter\c!spacebefore,
   \c!leftmargin=\zeropoint,
   \c!rightmargin=\zeropoint,
   \c!indentnext=\v!no,
   \c!alternative=\s!default,
   \c!strut=\v!no,
   \c!distance=2\emwidth]

\ifdefined\matheqnogapstep
    % we're ok, now we have that quad in the distance which is
    % more consistent and not depending on the text font in math
    \matheqnogapstep\zerocount
\else
    % we will keep this for a while
    \setupformulas[\c!distance=\emwidth]
\fi

\setupsubformulas % subformulas could be last in chain
  [\c!indentnext=\formulaparameter\c!indentnext]

\definecounter % one ?
  [\v!formula]

\defineconversionset
  [\v!formula]
  [numbers,characters] % no \v! ?

\installcounterassociation{formula} \registerformulacounter\v!formula % currently we only have one

\appendtoks
    \synchronizeformulacounters
\to \everysetupformula

% \appendtoks
%     \synchronizeformulacounters
% \to \everydefineformula

\setupformulas
  [\c!numberconversionset=\v!formula] % why forgotten

\appendtoks
    \normalexpanded{\definelist[\currentformula]}% is expansion needed?
    \setuevalue{\e!start\currentformula\v!formula}{\strc_formulas_start_formula{\currentformula}}%
    \setuevalue{\e!stop \currentformula\v!formula}{\strc_formulas_stop_formula}%
\to \everydefineformula

\definelist[\v!formula]

\setuvalue{\e!start\v!formula}{\strc_formulas_start_formula{}}
\setuvalue{\e!stop \v!formula}{\strc_formulas_stop_formula}

\let\strc_formulas_start_formula\relax % defined later
\let\strc_formulas_stop_formula \relax % defined later

\unexpanded\def\defineformulaalternative               % this might change ... start and stop can become keys
  {\dotripleargument\strc_formulas_define_alternative} % to the general define .. s!startcommand

\def\strc_formulas_define_alternative[#1][#2][#3]%
  {\setvalue{\e!start#1\v!formula}{#2}%
   \setvalue{\e!stop #1\v!formula}{#3}}

% sp = single line paragraph  sd = single line display
% mp = multi  line paragraph  md = multy  line display

\defineformulaalternative[\s!default][\startdisplaymath][\stopdisplaymath]
\defineformulaalternative[\s!single] [\startdisplaymath][\stopdisplaymath]
\defineformulaalternative[\s!multi]  [\startdisplaymath][\stopdisplaymath]

\defineformula
  [sp]
  [\c!spacebefore=\v!none,
   \c!spaceafter=\v!none,
   \c!indentnext=\v!no,
   \c!alternative=\s!single]

\defineformula
  [sd]
  [\c!spacebefore=\v!none,
   \c!spaceafter=\v!none,
   \c!indentnext=\v!yes,
   \c!alternative=\s!single]

\defineformula
  [mp]
  [\c!indentnext=\v!no,
   \c!alternative=\s!multi]

\defineformula
  [md]
  [\c!indentnext=\v!yes,
   \c!alternative=\s!multi]

\newtoks\everyresetformulas

\appendtoks
    \let\currentformula\empty % to be checked:
\to \everyresetformulas

% implementation

\unexpanded\def\strc_formulas_store_number#1#2#3#4#5% ref, todo:str, \sync % todo: title etc (like float)
  {\settrue\c_strc_formulas_handle_number
   \strc_counters_register_component
     {formula}%
     \setupcurrentformula \formulaparameter \detokenizedformulaparameter
     \relax \relax \relax
     [\c!name=\v!formula,\s!counter=\v!formula,%
      \s!hascaption=\v!yes,\s!hastitle=\v!yes,\s!hasnumber=\v!yes,%\s!haslevel=#6,%
      \c!reference=#1,\c!title=\namedformulaentry,\c!bookmark=]%
     [#2]%
   \glet\namedformulaentry\empty % \relax
   \globallet#3\m_strc_counters_last_registered_index
   \globallet#4\m_strc_counters_last_registered_synchronize
   \globallet#5\m_strc_counters_last_registered_attribute}

% modes: 0=unset, 1=forced, 2=none, 3=reference

\newconstant\c_strc_formulas_place_number_mode
\newconstant\c_strc_formulas_number_mode
\newconstant\c_strc_formulas_sub_number_mode
\newconstant\c_strc_formulas_nested_number_mode

\appendtoks
    \c_strc_formulas_place_number_mode \zerocount
    \c_strc_formulas_number_mode       \zerocount
    \c_strc_formulas_sub_number_mode   \zerocount
    \c_strc_formulas_nested_number_mode\zerocount
\to \everyresetformulas

\newconditional\c_strc_formulas_handle_number
\newconditional\c_strc_formulas_increment
\newconditional\c_strc_formulas_inside_place
\newconditional\c_strc_formulas_inside_place_sub
\newconditional\c_strc_formulas_inside_formulas
\newconditional\c_strc_formulas_inside_formulas_sub

\appendtoks
    \global\setfalse\c_strc_formulas_inside_place
    \global\setfalse\c_strc_formulas_inside_place_sub
\to \everyresetformulas

% \def\strc_formulas_place_numbering % place formula
%   {\settrue\c_strc_formulas_handle_number
%    \strc_formulas_check_reference\c_strc_formulas_place_number_mode\currentplaceformulareference
%    \glet\strc_formulas_place_number\strc_formulas_place_number_indeed
%    \glet\strc_formulas_place_number_nested\strc_formulas_place_number_nested_indeed}

\def\strc_formulas_place_numbering % place formula
  {\settrue\c_strc_formulas_handle_number
   \strc_formulas_check_reference\c_strc_formulas_place_number_mode\currentplaceformulareference
   \ifnum\c_strc_formulas_place_number_mode=\plustwo
     \glet\strc_formulas_place_number\relax
   \else
     \glet\strc_formulas_place_number\strc_formulas_place_number_indeed
   \fi
   \glet\strc_formulas_place_number_nested\strc_formulas_place_number_nested_indeed}

\def\strc_formulas_handle_number % formulas
  {\strc_formulas_check_reference\c_strc_formulas_number_mode\currentformulareference}

\def\strc_formulas_handle_sub_number_indeed % sub formulas
  {\strc_formulas_check_reference\c_strc_formulas_sub_number_mode\currentsubformulasreference
   \strc_counters_increment\v!formula
   \strc_formulas_store_number
     \currentsubformulasreference
     \empty
     \currentsubformulasnumber
     \currentsubformulassynchronize
     \currentsubformulasattribute}

\def\strc_formulas_handle_sub_number % sub formulas
  {\iftrialtypesetting
     \strc_counters_save\v!formula
     \strc_formulas_handle_sub_number_indeed
     \strc_counters_restore\v!formula
   \else
     \strc_formulas_handle_sub_number_indeed
   \fi}

\let\strc_formulas_reference_trace\relax
\let\strc_formulas_reference_show \relax

% \def\strc_formulas_reference_trace
%   {\rlap{\hbox{\quad\tt\txx[%
%       \number\c_strc_formulas_place_number_mode,%
%       \number\c_strc_formulas_number_mode,%
%       \number\c_strc_formulas_sub_number_mode,%
%       \number\c_strc_formulas_nested_number_mode
%    ]}}}

% \def\strc_formulas_reference_show
%   {\writestatus{\v!formula}%
%      {place:      \number\c_strc_formulas_place_number_mode,\space
%       formula:    \number\c_strc_formulas_number_mode,\space
%       subformula: \number\c_strc_formulas_sub_number_mode,\space
%       nested:     \number\c_strc_formulas_nested_number_mode]}}

\unexpanded\def\placecurrentformulanumber
  {\begingroup
   \rm % determines the distance and main font
   \edef\p_location{\formulaparameter\c!location}%
   \ifx\p_location\v!right
      \hskip\formulaparameter\c!distance
   \fi
   \begingroup
   \useformulastyleandcolor\c!numberstyle\c!numbercolor
   \formulaparameter\c!numbercommand
     {\strut
      \formulaparameter\c!left
      \namedtaggedlabeltexts
        \t!formulalabel \v!formula
        \t!formulanumber\v!formula
        {\ignorespaces\strc_formulas_place_current_number\removeunwantedspaces}%
      \formulaparameter\c!right}%
   \endgroup
   \ifx\p_location\v!left
     \hskip\formulaparameter\c!distance
   \fi
   \endgroup}

\unexpanded\def\strc_formulas_place_current_number
  {\strc_formulas_handle_current_references
   \labeltexts\currentformula{\convertedcounter[\v!formula][]}}

% \def\theboxdestinationattribute#1{\iflocation\ifx#1\relax\else\ifx#1\empty\else attr      \destinationattribute#1\fi\fi\fi}
% \def\thedestinationattribute   #1{\iflocation\ifx#1\relax\else\ifx#1\empty\else \attribute\destinationattribute#1\fi\fi\fi}

\def\theformuladestinationattribute#1%
  {\iflocation\ifx#1\relax\else\ifx#1\empty\else
     \attribute\destinationattribute#1%
     \glet#1\relax
   \fi\fi\fi}

\let\currentplaceformulaattribute\relax
\let\currentformulaattribute     \relax
\let\currentsubformulaattribute  \relax
\let\currentformulasattribute    \relax

\let\currentplaceformulanumber\relax
\let\currentformulanumber     \relax
\let\currentsubformulanumber  \relax
\let\currentformulasnumber    \relax

\let\currentformulasreference     \empty
\let\currentformulareference      \empty
\let\currentsubformulareference   \empty
\let\currentnestedformulareference\empty

\appendtoks
    \glet\currentformulasreference     \empty
    \glet\currentformulareference      \empty
    \glet\currentsubformulareference   \empty
    \glet\currentnestedformulareference\empty
\to \everyresetformulas

\let\currentformulassuffix     \empty
\let\currentformulasuffix      \empty
\let\currentsubformulasuffix   \empty
\let\currentnestedformulasuffix\empty

\appendtoks
    \glet\currentformulassuffix     \empty
    \glet\currentformulasuffix      \empty
    \glet\currentsubformulasuffix   \empty
    \glet\currentnestedformulasuffix\empty
\to \everyresetformulas

\let\currentplaceformulasynchronize\relax
\let\currentformulasynchronize     \relax
\let\currentsubformulasynchronize  \relax
\let\currentformulassynchronize    \relax

\appendtoks
    \glet\currentplaceformulasynchronize \relax
    \glet\currentformulassynchronize     \relax
    \glet\currentsubformulassynchronize  \relax
    \glet\currentnestedformulasynchronize\relax
\to \everyresetformulas

% currently we do the number, some day we will do the (sub) formula

\def\strc_formulas_handle_current_references
  {\strc_formulas_reference_show
   \ifnum\c_strc_formulas_place_number_mode=\plusthree
     \strc_formulas_store_number
       \currentplaceformulareference
       \empty
       \currentplaceformulanumber
       \currentplaceformulasynchronize
       \currentplaceformulaattribute
     \currentplaceformulasynchronize
     \glet\currentplaceformulasynchronize\relax
     \theformuladestinationattribute\currentplaceformulaattribute
   \fi
   \ifnum\c_strc_formulas_number_mode=\plusthree
     \strc_formulas_store_number
       \currentformulasreference
       \empty
       \currentformulasnumber
       \currentformulassynchronize
       \currentformulasattribute
     \currentformulassynchronize
     \glet\currentformulassynchronize\relax
     \theformuladestinationattribute\currentformulasattribute
   \fi
   \ifnum\c_strc_formulas_sub_number_mode=\plusthree
     \currentsubformulassynchronize
     \glet\currentsubformulassynchronize\relax
   \fi
   \ifnum\c_strc_formulas_nested_number_mode=\plusthree
     \strc_formulas_store_number
       \currentnestedformulareference
       \empty
       \currentnestedformulanumber
       \currentnestedformulasynchronize
       \currentnestedformulaattribute
     \currentnestedformulasynchronize
     \glet\currentnestedformulasynchronize\relax
     \theformuladestinationattribute\currentnestedformulaattribute
   \fi}

% needs checking ... too many:

\def\strc_formulas_handle_numbering_indeed
  {\strc_counters_increment\v!formula
   \doiftext\currentplaceformulasuffix{\strc_counters_setown_sub\v!formula\plustwo\currentplaceformulasuffix}%
   \placecurrentformulanumber}

\def\strc_formulas_handle_numbering
  {\iftrialtypesetting
     \strc_counters_save\v!formula
     \strc_formulas_handle_numbering_indeed
     \strc_counters_restore\v!formula
   \else
     \strc_formulas_handle_numbering_indeed
   \fi}

\def\strc_formulas_handle_sub_numbering_indeed
  {\let\strc_formulas_handle_sub_numbering\relax % else error: see math/numbering-001.tex
   \doifelsetext\currentsubformulasuffix
     {\strc_counters_setown_sub\v!formula\plustwo\currentsubformulasuffix}
     {\strc_counters_increment_sub\v!formula\plustwo}%
   \placecurrentformulanumber}

\def\strc_formulas_handle_sub_numbering
  {\iftrialtypesetting
     \strc_counters_save\v!formula
     \strc_formulas_handle_sub_numbering_indeed
     \strc_counters_restore\v!formula
   \else
     \strc_formulas_handle_sub_numbering_indeed
   \fi}

\def\strc_formulas_number_indeed
  {\ifconditional\c_strc_formulas_handle_number
     \hbox\bgroup
       % main counter
       \ifconditional\c_strc_formulas_inside_formulas_sub
         % nothing
       \else
         \ifcase\c_strc_formulas_number_mode
           \ifcase\c_strc_formulas_place_number_mode
             \strc_formulas_handle_numbering
           \or
             \strc_formulas_handle_numbering
           \or
             % nothing
           \or
             \strc_formulas_handle_numbering
           \fi
         \or
           \strc_formulas_handle_numbering
         \or
           % nothing
         \or
           \strc_formulas_handle_numbering
         \fi
       \fi
       % subcounter
       \ifconditional\c_strc_formulas_inside_formulas_sub
         \ifcase\c_strc_formulas_sub_number_mode
           % nothing
         \or
           \strc_formulas_handle_sub_numbering
         \or
           % nothing
         \or
           \strc_formulas_handle_sub_numbering
         \fi
       \fi
      \strc_formulas_reference_trace
     \egroup
   \fi}

\installstructurelistprocessor\v!formula % to be checked ...
  {\let\currentlistentrynumber    \structurelistgenericnumber
   \let\currentlistentrytitle     \structurelistgenerictitle
   \let\currentlistentrypagenumber\structurelistpagenumber
   \strc_lists_apply_renderingsetup}

\newif\ifinformula

%D We need a hook into the plain math alignment macros
%D
%D \starttyping
%D \displaylines
%D \eqalignno
%D \eqalignno
%D \stoptyping
%D
%D Otherwise we get a missing \type {$$} error reported.

\let\reqno\eqno

\unexpanded\def\resetdisplaymatheq % when used?
  {\let\normalleqno\gobbleoneargument \let\leqno\gobbleoneargument
   \let\normalreqno\gobbleoneargument \let\eqno \gobbleoneargument
   \let\strc_formulas_place_number\relax}

%D \macros
%D   {startsubformulas}

% \placeformula
% \startsubformulas[Maxwell]
%     \startformulas
%      \startformula \startalign
%        \NC \nabla\cdot\bf E  \NC = \frac{\rho}{\varepsilon_0} \NR[Maxwell 1]
%        \NC \nabla\times\bf E \NC = - \frac{\partial\bf B}{\partial t} \NR[Maxwell II]
%        \stopalign \stopformula
%        \startformula \startalign
%          \NC \nabla\cdot \bf B \NC = 0 \NR[Maxwell III]
%          \NC \nabla\times\bf B \NC = \mu_0{\bf j}+\varepsilon_0\mu_0\frac{\partial\bf E}{\partial t} \NR[Maxwell IV]
%        \stopalign \stopformula
%    \stopformulas
% \stopsubformulas
%
% Maxwell : \in [Maxwell] and II : \in [Maxwell II]

%D Tricky stuff:

\newdimen\lastlinewidth

\abovedisplayskip      \zeropoint
\abovedisplayshortskip \zeropoint % evt. 0pt minus 3pt
\belowdisplayskip      \zeropoint
\belowdisplayshortskip \zeropoint % evt. 0pt minus 3pt

\predisplaypenalty     \zerocount
\postdisplaypenalty    \zerocount % -5000 gaat mis, zie penalty bij \paragraaf

% we don't use the skip's

\unexpanded\def\strc_formulas_forget_display_skips
  {\abovedisplayskip     \zeropoint
   \belowdisplayskip     \zeropoint
   \abovedisplayshortskip\zeropoint
   \belowdisplayshortskip\zeropoint}

% \def\predisplaysizethreshhold{2\emwidth} % was 3\emwidth

\newdimen\d_strc_formulas_display_skip_left
\newdimen\d_strc_formulas_display_skip_right
\newdimen\d_strc_formulas_display_margin_left
\newdimen\d_strc_formulas_display_margin_right
\newdimen\d_strc_formulas_display_pre_threshold
\newskip \d_strc_formulas_display_skip_par

\unexpanded\def\beforedisplayspace
  {\edef\p_spacebefore{\formulaparameter\c!spacebefore}%
   \ifx\p_spacebefore\v!none \else
     \blank[\p_spacebefore]%
   \fi}

\unexpanded\def\afterdisplayspace
  {\edef\p_spaceafter{\formulaparameter\c!spaceafter}%
   \ifx\p_spaceafter\v!none \else
     \blank[\p_spaceafter]%
   \fi}

\unexpanded\def\setdisplaydimensions
  {\displayindent\d_strc_formulas_display_skip_left
   \advance\displayindent\d_strc_formulas_display_margin_left
   \displaywidth\hsize
  %\setlocalhsize
  %\displaywidth\localhsize
   \ifdim\hangindent>\zeropoint
     \advance\displayindent\hangindent
   \else
     \advance\displaywidth\hangindent
   \fi
   \advance\displaywidth\dimexpr-\displayindent-\d_strc_formulas_display_skip_right-\d_strc_formulas_display_margin_right\relax
   \hsize\displaywidth} % new, else overfull in itemize

\unexpanded\def\strc_formulas_start_formula#1%
  {\dodoubleempty\strc_formulas_start_formula_indeed[#1]}

% \newskip\formulastrutht
% \newskip\formulastrutdp

%D \startbuffer
%D \startformula[9pt] x = 1 \stopformula
%D \startformula[7pt] x = 1 \stopformula
%D \stopbuffer
%D
%D \typebuffer \getbuffer

\unexpanded\def\strc_formulas_start_formula_indeed[#1][#2]% setting leftskip adaption is slow !
  {\bgroup % HERE
   \def\currentformula{#1}%
   \dostarttaggedchained\t!formula\currentformula\??formula
   \the\everybeforedisplayformula
   \d_strc_formulas_display_skip_par\parskip\relax
  %\formulastrutdp\strutdepth
  %\formulastrutht\strutheight
   \edef\p_option  {\formulaparameter\c!option}%
   \edef\p_margin  {\formulaparameter\c!margin}%
   \edef\p_bodyfont{#2}%
  %\ifx\p_bodyfont\empty
  %  \edef\p_bodyfont{\formulaparameter\c!bodyfont}%
  %\fi
   \ifx\p_bodyfont\empty \else
     \switchtoformulabodyfont[#2]%
   \fi
   \parskip\d_strc_formulas_display_skip_par\relax
   \ifx\p_option\v!middle
     \d_strc_formulas_display_skip_left \zeropoint
     \d_strc_formulas_display_skip_right\zeropoint
   \else
     \d_strc_formulas_display_skip_left \leftskip
     \d_strc_formulas_display_skip_right\rightskip
   \fi
   \d_strc_formulas_display_margin_left \formulaparameter\c!leftmargin \relax
   \d_strc_formulas_display_margin_right\formulaparameter\c!rightmargin\relax
   \ifx\p_margin\empty \else
     \dosetleftskipadaption\p_margin
     \d_strc_formulas_display_margin_left\leftskipadaption
   \fi
   \let\strc_formulas_start_formula\strc_formulas_start_formula_nested
  %\freezedimenmacro\predisplaysizethreshhold
   \strc_formulas_forget_display_skips
   \getvalue{\e!start\formulaparameter\c!alternative\v!formula}}

\unexpanded\def\strc_formulas_start_formula_nested#1%
  {\bgroup
   \let\strc_formulas_stop_formula\strc_formulas_stop_formula_nested
   \dostarttagged\t!subformula\empty}

\unexpanded\def\strc_formulas_stop_formula_nested
  {\dostoptagged
   \egroup}

% tagging of formulanumbers is not ok (we get two display maths blobs)

\unexpanded\def\strc_formulas_stop_formula
  {\dostarttagged\t!formulacaption\empty
   \strc_formulas_place_number
   \dostoptagged
   \dostarttagged\t!formulacontent\empty
   \getvalue{\e!stop\formulaparameter\c!alternative\v!formula}%
   \dostoptagged
   \dostoptagged
   \nonoindentation
   \useindentnextparameter\formulaparameter
   \egroup
   \hangafter\minusone   % added for side floats
   \hangindent\zeropoint % added for side floats
   \setfalse\c_strc_formulas_handle_number
   \the\everyresetformulas
   \dorechecknextindentation} % here ?

% experiment:

\appendtoks
   \edef\p_grid{\formulaparameter\c!grid}%
   \ifx\p_grid\empty \else
     \spac_grids_snap_value_auto\p_grid
   \fi
\to \everybeforedisplayformula

\unexpanded\def\switchtoformulabodyfont
  {\switchtobodyfont}

\setuvalue{\v!formula}{\dosingleempty\strc_formulas_formula}

\def\strc_formulas_formula[#1]#2% todo: tagged
  {\begingroup
   \edef\p_bodyfont{#1}%
   \ifx\p_bodyfont\empty \else
     \switchtoformulabodyfont[\p_bodyfont]%
   \fi
   % not : \def\strc_formulas_formula[##1]##2{\mathematics{##2}}%
   \mathematics{#2}%
   \endgroup}

%D \starttyping
%D % test \par       % no preceding hlist
%D % $$x$$           % preceding hlist
%D % \noindent $$x$$ % no preceding hlist
%D \startformula x \stopformula % now has \noindent (in mkii we messed with baselineskip)
%D \stoptyping

% \unexpanded\def\startdisplaymath
%   {\bgroup
%    \par
%    \informulatrue
%    \beforedisplayspace
%    \par
%    \ifvmode
%      \prevdepth-\maxdimen % texbook pagina 79-80
%    \fi
%    \noindent % else funny hlist with funny baselineskip
%    $$% \Ucheckedstartdisplaymath
%    \setdisplaydimensions
%    \startinnermath}
%
% \unexpanded\def\stopdisplaymath
%   {\stopinnermath
%    $$% \Ucheckedstopdisplaymath
%    \par
%    \afterdisplayspace
%    \par
%    \egroup}

\newconstant\c_strc_formulas_space_model

\c_strc_formulas_space_model\plusone
%c_strc_formulas_space_model\plustwo % needs chdcking with spac-ver

\unexpanded\def\startdisplaymath
  {\bgroup
   \par
   \informulatrue
   \beforedisplayspace
   \par
   \ifvmode
     \ifcase\c_strc_formulas_space_model
       % nothing
     \or
       % nothing yet
     \or
       \prevdepth-\maxdimen % texbook pagina 79-80
     \fi
   \fi
   \noindent % else funny hlist with funny baselineskip
   \Ucheckedstartdisplaymath
   \setdisplaydimensions
   \startinnermath}

\unexpanded\def\stopdisplaymath
  {\stopinnermath
   \Ucheckedstopdisplaymath
   \par
   \ifvmode
     \ifcase\c_strc_formulas_space_model
       % nothing
     \or
       \prevdepth .5\strutdp
     \or
       \prevdepth\lineheight
     \fi
   \fi
   \afterdisplayspace
   \par
   \egroup}

% already defined
%
% \let\startinnermath\empty
% \let\stopinnermath \empty

% \defineformulaalternative[multi][\begindmath][\enddmath]
%
% \fakewords{20}{40}\epar
% \placeformula {a} $$              \fakespacingformula $$
% \fakewords{20}{40}\epar
% \placeformula {b} \startformule   \fakespacingformula \stopformule
% \placeformula {b} \startformule   \fakespacingformula \stopformule
% \fakewords{20}{40}\epar
% \placeformula {c} \startmdformule \fakespacingformula \stopmdformule
% \placeformula {c} \startmdformule \fakespacingformula \stopmdformule
% \fakewords{20}{40}\epar
% \placeformula {d} \startmpformule \fakespacingformula \stopmpformule
% \placeformula {d} \startmpformule \fakespacingformula \stopmpformule
% \fakewords{20}{40}\epar
% \placeformula {e} \startsdformule \fakespacingformula \stopsdformule
% \placeformula {e} \startsdformule \fakespacingformula \stopsdformule
% \fakewords{20}{40}\epar
% \placeformula {f} \startspformule \fakespacingformula \stopspformule
% \placeformula {f} \startspformule \fakespacingformula \stopspformule
% \fakewords{20}{40}

\unexpanded\def\startsubformulas
  {\dosingleempty\strc_formulas_start_sub_formulas}

\def\strc_formulas_start_sub_formulas[#1]%
  {\edef\currentsubformulasreference{#1}%
   \global\settrue\c_strc_formulas_inside_formulas_sub
   \strc_formulas_handle_sub_number}

\unexpanded\def\stopsubformulas
  {\nonoindentation
   \useindentnextparameter\subformulaparameter
   \the\everyresetformulas % to be checked
   \global\setfalse\c_strc_formulas_inside_formulas_sub
   \dorechecknextindentation} % here ?

%D Named subformulas (to be redone)

\unexpanded\def\startnamedsubformulas
  {\dosingleempty\strc_formulas_start_named_sub_formulas}

\def\strc_formulas_start_named_sub_formulas[#1]#2%
  {\setformulalistentry{#2}%
   \startsubformulas[#1]}

\unexpanded\def\stopnamedsubformulas
  {\stopsubformulas}

%D Experimental goodie:
%D
%D \startbuffer
%D \placelist[formula][criterium=text] \blank[2*big]
%D \placenamedformula[one]{first}  \startformula a = 1 \stopformula \endgraf
%D \placeformula                   \startformula a = 2 \stopformula \endgraf
%D \placenamedformula     {second} \startformula a = 3 \stopformula \endgraf
%D \stopbuffer
%D
%D \typebuffer \getbuffer

\unexpanded\def\startformulas
  {\dosingleempty\strc_formulas_start_formulas}

\def\strc_formulas_start_formulas[#1]#2\stopformulas % new / to be internationalized
  {\bgroup
   \dostarttagged\t!formulaset\empty
   \global\settrue\c_strc_formulas_inside_formulas
   \edef\currentformulasreference{#1}%
   \strc_formulas_handle_number
   \let\currentformula\empty
   \strc_formulas_forget_display_skips
   \startdisplaymath
   \setlocalhsize
   \unexpanded\def\startformula##1\stopformula
     {\advance\scratchcounter\plusone}%
   \scratchcounter\zerocount
   #2% preroll
   \ifcase\scratchcounter\else
     \divide \hsize \scratchcounter
   \fi
   \hbox to \localhsize \bgroup
     \hss
     \let\startformula\strc_formulas_nested_formula_start
     \let\stopformula \strc_formulas_nested_formula_stop
     #2%
     \egroup
   \stopdisplaymath
   \global\setfalse\c_strc_formulas_inside_formulas
   \dostoptagged
   \egroup
   \the\everyresetformulas
   \hangafter\minusone    % added for side floats
   \hangindent\zeropoint} % added for side floats

\unexpanded\def\strc_formulas_nested_formula_start
  {\Ustartmath
   \vcenter\bgroup
   \vskip-\strutdepth
   \Ustartdisplaymath}

\unexpanded\def\strc_formulas_nested_formula_stop
  {\Ustopdisplaymath
   \egroup
   \Ustopmath
   \hss}

% place

\def\m_strc_formulas_flag_inhibit{-}
\def\m_strc_formulas_flag_force  {+}

\def\strc_formulas_check_reference#1#2%
  {#1\unless\ifx\namedformulaentry\empty % \relax % new 29/8/2010
     \plusthree
   \else\ifx#2\empty
     \zerocount
   \else\ifx#2\m_strc_formulas_flag_force
     \plusone
   \else\ifx#2\m_strc_formulas_flag_inhibit
     \plustwo
   \else
     \plusthree
   \fi\fi\fi\fi}

\unexpanded\def\formulanumber
 {\strc_formulas_number} % for the moment

\def\strc_formulas_number
  {\dosingleempty\strc_formulas_number_again}

\def\strc_formulas_number_again[#1]%
  {\def\currentformulareference{#1}%
   \dosinglegroupempty\strc_formulas_number_indeed}

\unexpanded\def\placeformula
  {\global\settrue\c_strc_formulas_inside_place
   \settrue\c_strc_formulas_increment
   \dosingleempty\strc_formulas_place}

\unexpanded\def\placesubformula
  {\global\settrue\c_strc_formulas_inside_place_sub
   \setfalse\c_strc_formulas_increment
   \dosingleempty\strc_formulas_place}

\def\strc_formulas_place[#1]%
  {\def\currentplaceformulareference{#1}%
   \let\currentplaceformulasuffix\empty
   \doifelsenextbgroup\strc_formulas_place_yes\strc_formulas_place_nop\strc_formulas_place_nop} % [ref]{}

\def\strc_formulas_place_yes#1%
  {\def\currentplaceformulasuffix{#1}%
   \strc_formulas_place_nop}

\def\strc_formulas_place_nop
  {\doifelsenextchar$\strc_formulas_place_pickup\strc_formulas_place_indeed} % [ref]$$ [ref]\start

\def\strc_formulas_place_indeed
  {\strc_formulas_place_numbering}

\def\strc_formulas_place_pickup$$#1$$%
  {\strc_formulas_place_numbering
   \strc_formulas_start_formula{}#1\strc_formulas_stop_formula}

\let\startplaceformula\placeformula
\let\stopplaceformula \relax

% to be checked

\let\strc_formulas_place_number       \relax
\let\strc_formulas_place_number_nested\gobbletwoarguments

\def\strc_formulas_place_number_nested_indeed#1#2%
  {\def\currentnestedformulareference{#1}%
   \def\currentnestedformulasuffix{#2}%
   \glet\strc_formulas_place_number\relax
   \strc_formulas_check_reference\c_strc_formulas_nested_number_mode\currentnestedformulareference
   \ifcase\c_strc_formulas_nested_number_mode
     % nothing
   \or
     \strc_formulas_number % hm, looks ahead for []
   \or
     % nothing
   \or
     \strc_formulas_number % hm, looks ahead for []
   \fi}

\def\strc_formulas_place_number_indeed
  {\glet\strc_formulas_place_number\relax
   \doifelse{\formulaparameter\c!location}\v!left
     {\normalleqno{\strc_formulas_number_indeed}}
     {\normalreqno{\strc_formulas_number_indeed}}}

% todo

\unexpanded\def\placenamedformula
  {\dosingleempty\strc_formulase_place_named}

\def\strc_formulase_place_named
  {\iffirstargument
     \expandafter\strc_formulase_place_named_yes
   \else
     \expandafter\strc_formulase_place_named_nop
   \fi}

\def\strc_formulase_place_named_yes[#1]#2%
  {\setformulalistentry{#2}%
   \placeformula[#1]}

\def\strc_formulase_place_named_nop[#1]#2%
  {\setformulalistentry{#2}%
   \placeformula}

\let\namedformulaentry\empty % \relax % this will become a key/value so that we can do bookmarks

\unexpanded\def\setformulalistentry#1%
  {\gdef\namedformulaentry{#1}}

\protect \endinput

% \abovedisplayshortskip0pt \belowdisplayshortskip0pt \abovedisplayskip0pt \belowdisplayskip0pt \forgetall
%
% test       \par                      $$xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx$$ \par test \par
% test       \par                      $$xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx$$ \par test \par
% test plus  \par \prevdepth \maxdimen $$xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx$$ \par test \par
% test minus \par \prevdepth-\maxdimen $$xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx$$ \par test \par
%
% \parskip\baselineskip
%
% test       \par                      $$xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx$$ \par test \par
% test       \par                      $$xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx$$ \par test \par
% test plus  \par \prevdepth \maxdimen $$xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx$$ \par test \par
% test minus \par \prevdepth-\maxdimen $$xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx$$ \par test \par
