%D \module
%D   [       file=math-dld,
%D        version=2022.07.29,
%D          title=\CONTEXT\ Math Macros,
%D       subtitle=delimiteds,
%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 Math Macros / Delimited}

\unprotect

%D This is a variant on radicals but in the end it was better to just define a new
%D environment for this. This is because we don't have the rule and we want a
%D different control over the spacing. We also don't want the degree.

% Todo: also add the mp plugin here but that more fun stuff for later (when we pick
% up school math).

\installcorenamespace{mathdelimited}
\installcorenamespace{mathdelimitedalternative}

\installcommandhandler \??mathdelimited {mathdelimited} \??mathdelimited

\setupmathdelimited
  [\c!define=\v!yes,
   \c!alternative=\v!normal,
   \c!mpoffset=.25\exheight,
   \c!topoffset=\zeropoint,
   \c!bottomoffset=\zeropoint,
   \c!height=\zeropoint,
   \c!depth=\zeropoint,
   \c!strut=\v!height,
   \c!source=\zerocount,
   \c!size=\zerocount,
   \c!left=\zerocount,
   \c!right=\zerocount,
   \c!leftmargin=\zeropoint,
   \c!rightmargin=\zeropoint]

\appendtoks
    \ifcstok{\mathdelimitedparameter\c!define}\v!yes
      \frozen\protected\instance\edefcsname\currentmathdelimited\endcsname{\math_delimited_handle{\currentmathdelimited}}
    \fi
\to \everydefinemathdelimited

\mutable\lettonothing\currentmathdelimited

\tolerant\protected\def\math_delimited_handle#tag#*[#S#settings]#:#body%
  {\begingroup
   \cdef\currentmathdelimited{#tag}%
   \setupcurrentmathdelimited[#settings]%
   \math_delimited_alternative{%
     \begincsname\??mathwhateverstrut\mathdelimitedparameter\c!strut\endcsname
     \scratchdimen\mathdelimitedparameter\c!leftmargin \relax\ifzeropt\scratchdimen\else\kern\scratchdimen\fi
     #body%
     \scratchdimen\mathdelimitedparameter\c!rightmargin\relax\ifzeropt\scratchdimen\else\kern\scratchdimen\fi
   }%
   \endgroup}

\def\math_delimited_alternative{\csname\??mathdelimitedalternative\mathdelimitedparameter\c!alternative\endcsname}

\protected\def\math_delimited_common
  {\Udelimited
     \s!height\dimexpr\mathdelimitedparameter\c!height+\mathdelimitedparameter\c!topoffset\relax
     \s!depth \dimexpr\mathdelimitedparameter\c!depth+\mathdelimitedparameter\c!bottomoffset\relax
     \s!source\numexpr\namedboxanchor{\mathdelimitedparameter\c!source}\relax
     \s!style \normalmathstyle
     \s!size  \numexpr\mathdelimitedparameter\c!size\relax
     \zerocount \mathdelimitedparameter\c!left
     \zerocount \mathdelimitedparameter\c!right
  \relax}

\defcsname\??mathdelimitedalternative\v!default\endcsname %  #body%
  {\math_delimited_common} % {#body}}

\defcsname\??mathdelimitedalternative\v!normal\endcsname#body%
  {\edef\p_color{\mathdelimitedparameter\c!color}%
   \ifempty\p_color
     \math_delimited_common{#body}% {} really needed as \rootdelimited expands first
   \else
     \pushcolor[\p_color]%
     \math_delimited_common
     {\popcolor#body}%
   \fi}

%integerdef\delimitedrightgraveuc    \privatecharactercode{delimited right grave}
\integerdef\delimitedrightddotuc     \privatecharactercode{delimited right ddot}
\integerdef\delimitedrightbaruc      \privatecharactercode{delimited right bar}
%integerdef\delimitedrightacuteuc    \privatecharactercode{delimited right acute}
\integerdef\delimitedrighthatuc      \privatecharactercode{delimited right hat}
\integerdef\delimitedrightcheckuc    \privatecharactercode{delimited right check}
\integerdef\delimitedrightbreveuc    \privatecharactercode{delimited right breve}
\integerdef\delimitedrightdotuc      \privatecharactercode{delimited right dot}
\integerdef\delimitedrightringuc     \privatecharactercode{delimited right ring}
\integerdef\delimitedrighttildeuc    \privatecharactercode{delimited right tilde}
\integerdef\delimitedrightdddotuc    \privatecharactercode{delimited right dddot}

% \integerdef\delimitedrighthatuc   \privatecharactercode{delimited right hat}
% \integerdef\delimitedrighttildeuc \privatecharactercode{delimited right tilde}
% \integerdef\delimitedrightcheckuc \privatecharactercode{delimited right check}

% todo: some more .. are the offsets okay?
% breve and acute does not make sense

\definemathdelimited
  [marked]
  [\c!define=\v!no,
   \c!topoffset=.2\exheight]

\definemathdelimited
  [ddotmarked]
  [marked]
  [\c!define=\v!yes,
   \c!right=\delimitedrightddotuc]

\definemathdelimited
  [barmarked]
  [marked]
  [\c!define=\v!yes,
   \c!right=\delimitedrightbaruc]

\definemathdelimited
  [hatmarked]
  [marked]
  [\c!define=\v!yes,
   \c!right=\delimitedrighthatuc]

\definemathdelimited
  [checkmarked]
  [marked]
  [\c!define=\v!yes,
   \c!right=\delimitedrightcheckuc]

\definemathdelimited
  [brevemarked]
  [marked]
  [\c!define=\v!yes,
   \c!right=\delimitedrightbreveuc]

\definemathdelimited
  [dotmarked]
  [marked]
  [\c!right=\delimitedrightdotuc]

\definemathdelimited
  [ringmarked]
  [marked]
  [\c!define=\v!yes,
   \c!right=\delimitedrightringuc]

\definemathdelimited
  [tildemarked]
  [marked]
  [\c!define=\v!yes,
   \c!right=\delimitedrighttildeuc]

\definemathdelimited
  [dddotmarked]
  [marked]
  [\c!define=\v!yes,
   \c!right=\delimitedrightdddotuc]

% The following ones are
% not really belonging here,
% but convenient

% If there are more needed we can have:
%
% \c!ight=\privatecharactercode{delimited right dagger \mathdelimitedparameter\c!size}
%
% or even:
%
% \def\privatedelimitedcode#1{\privatecharactercode{delimited #1 \mathdelimitedparameter\c!size}}
%
% \c!right=\privatedelimitedcode{right dagger}
%
% with some glyph installer btu let's for now save some bytes in the format.

\integerdef\delimitedrightdaggeruc        \privatecharactercode{delimited right dagger}
\integerdef\delimitedrightddaggeruc       \privatecharactercode{delimited right ddagger}
\integerdef\delimitedrightastuc           \privatecharactercode{delimited right ast}
\integerdef\delimitedrightstaruc          \privatecharactercode{delimited right star}

\integerdef\delimitedscriptrightdaggeruc  \privatecharactercode{delimited right dagger 1}
\integerdef\delimitedscriptrightddaggeruc \privatecharactercode{delimited right ddagger 1}
\integerdef\delimitedscriptrightastuc     \privatecharactercode{delimited right ast 1}
\integerdef\delimitedscriptrightstaruc    \privatecharactercode{delimited right star 1}

\definemathdelimited
  [daggermarked]
  [marked]
  [\c!define=\v!yes,
   \c!size=1,
   \c!right=\delimitedscriptrightdaggeruc]
  %\c!right=\privatedelimitedcode{right dagger}]

\definemathdelimited
  [ddaggermarked]
  [marked]
  [\c!define=\v!yes,
   \c!size=1,
   \c!right=\delimitedscriptrightddaggeruc]
  %\c!right=\privatedelimitedcode{right ddagger}]

\definemathdelimited
  [astmarked]
  [marked]
  [\c!define=\v!yes,
   \c!size=1,
   \c!right=\delimitedscriptrightastuc]
  %\c!right=\privatedelimitedcode{right ast}]

\definemathdelimited
  [starmarked]
  [marked]
  [\c!define=\v!yes,
   \c!size=1,
   \c!right=\delimitedscriptrightstaruc]
  %\c!right=\privatedelimitedcode{right star}]

% More contextual

\definemathdelimited
  [fourier]
  [\c!topoffset=.2\exheight,
   \c!right=\delimitedrighthatuc]

\definemathdelimited
  [inversefourier]
  [fourier]
  [\c!right=\delimitedrightcheckuc]

\definemathdelimited
  [fuzzyfourier]
  [fourier]
  [\c!right=\delimitedrighttildeuc]

% \definemathdelimited
%   [bannuity]
%   [%topoffset=.2\exheight,
%    \c!strut=no,
%    \c!rightmargin=.05\emwidth,
%    \c!right=\delimitedrightanutityuc]

% $ \autofences \fourier{(z+\frac12)} + \courier{(z+\frac12)} + \xourier{(z+\frac12)} $

\integerdef\delimitedrightprimeuc   \privatecharactercode{delimited right prime}
\integerdef\delimitedrightdprimeuc  \privatecharactercode{delimited right dprime}
\integerdef\delimitedrighttprimeuc  \privatecharactercode{delimited right tprime}
\integerdef\delimitedrightqprimeuc  \privatecharactercode{delimited right qprime}
\integerdef\delimitedrightrprimeuc  \privatecharactercode{delimited right rprime}
\integerdef\delimitedrightrdprimeuc \privatecharactercode{delimited right rdprime}
\integerdef\delimitedrightrtprimeuc \privatecharactercode{delimited right rtprime}

%definemathdelimited [primed]                       [\c!right="2032,\c!size=1,\c!topoffset=.2\exheight]
%definemathdelimited [doubleprimed]        [primed] [\c!right="2033]
%definemathdelimited [tripleprimed]        [primed] [\c!right="2034]
%definemathdelimited [quadrupleprimed]     [primed] [\c!right="2057]
%definemathdelimited [reverseprimed]       [primed] [\c!right="2035]
%definemathdelimited [doublereverseprimed] [primed] [\c!right="2036]
%definemathdelimited [triplereverseprimed] [primed] [\c!right="2037]

\definemathdelimited [primed]                       [\c!right=\delimitedrightprimeuc,\c!topoffset=.2\exheight,\c!size=1]
\definemathdelimited [doubleprimed]        [primed] [\c!right=\delimitedrightdprimeuc]
\definemathdelimited [tripleprimed]        [primed] [\c!right=\delimitedrighttprimeuc]
\definemathdelimited [quadrupleprimed]     [primed] [\c!right=\delimitedrightqprimeuc]
\definemathdelimited [reverseprimed]       [primed] [\c!right=\delimitedrightrprimeuc]
\definemathdelimited [doublereverseprimed] [primed] [\c!right=\delimitedrightrdprimeuc]
\definemathdelimited [triplereverseprimed] [primed] [\c!right=\delimitedrightrtprimeuc]

%

\integerdef\delimitedleftuppercorner \privatecharactercode{delimited left upper corner}
\integerdef\delimitedrightuppercorner\privatecharactercode{delimited right upper corner}
\integerdef\delimitedleftlowercorner \privatecharactercode{delimited left lower corner}
\integerdef\delimitedrightlowercorner\privatecharactercode{delimited right lower corner}

\definemathdelimited
  [leftuppermarked]
  [marked]
  [\c!left=\delimitedleftuppercorner]

\definemathdelimited
  [rightuppermarked]
  [marked]
  [\c!right=\delimitedrightuppercorner]

\definemathdelimited
  [uppermarked]
  [marked]
  [\c!left=\delimitedleftuppercorner,
   \c!right=\delimitedrightuppercorner]

\definemathdelimited
  [leftlowermarked]
  [marked]
  [\c!left=\delimitedleftlowercorner]

\definemathdelimited
  [rightlowermarked]
  [marked]
  [\c!right=\delimitedrightlowercorner]

\definemathdelimited
  [lowermarked]
  [marked]
  [\c!left=\delimitedleftlowercorner,
   \c!right=\delimitedrightlowercorner]

\protect \endinput

