%D \module
%D   [       file=math-fen,
%D        version=2012.02.18,
%D          title=\CONTEXT\ Math Macros,
%D       subtitle=Fences,
%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 / Fences}

\unprotect

% todo: mathstyle

% \definemathfence [fancybracket] [bracket] [command=yes,color=blue]
%
% test $|x|$ test \par
% test $||x||$ test (okay) \par
% test $a\left|\frac{1}{b}\right|c$ test \par
% test $a\left||\frac{1}{b}\right||c$ test (not okay) \par
%
% \setupmathfences [color=red]
%
% test $a\fenced[bar]{\frac{1}{b}}c$ test \par
% test $a\fenced[doublebar]{\frac{1}{b}}c$ test \par
% test $a\fenced[bracket]{\frac{1}{b}}c$ test \par
% test $a\fancybracket{\frac{1}{b}}c$ test \par

\installcorenamespace{mathfences}

\installcommandhandler \??mathfences {mathfence} \??mathfences

\let\setupmathfences\setupmathfence

\setupmathfences
  [\c!left=,
   \c!right=,
   \c!middle=,
   \c!mathstyle=,
   \c!color=,
   \c!command=]

\appendtoks
    \edef\p_command{\mathfenceparameter\c!command}%
    \ifx\p_command\v!yes
        \setuevalue\currentmathfence{\math_fenced_fenced[\currentmathfence]}%
    \fi
\to \everydefinemathfence

% we need the direct use of \Udelimiter because of { etc

\def\math_fenced_left  {\edef\p_left{\mathfenceparameter\c!left}%
                        \math_fenced_color_push
                        \normalleft\ifx\p_left\empty.\else\Udelimiter\plusfour\fam\p_left\relax\fi
                        \math_fenced_color_pop}
\def\math_fenced_middle{\edef\p_middle{\mathfenceparameter\c!middle}%
                        \mskip\thinmuskip
                        \math_fenced_color_push
                        \normalmiddle\ifx\p_middle\empty.\else\Udelimiter\plusfour\fam\p_middle\relax\fi
                        \math_fenced_color_pop
                        \mskip\thinmuskip}
\def\math_fenced_right {\edef\p_right{\mathfenceparameter\c!right}%
                        \math_fenced_color_push
                        \normalright\ifx\p_right\empty.\else\Udelimiter\plusfive\fam\p_right\relax\fi
                        \math_fenced_color_pop}

\def\math_fenced_color_do_push{\pushcolor[\p_math_fenced_color]}
\let\math_fenced_color_do_pop  \popcolor

\let\math_fenced_color_push\donothing
\let\math_fenced_color_pop \donothing

\let\fence \relax
\let\fenced\relax

\newcount\c_math_fenced_nesting

\unexpanded\def\math_fenced_fenced_start#1%
  {\advance\c_math_fenced_nesting\plusone
   \begingroup
   \edef\currentmathfence{#1}%
   \startusemathstyleparameter\mathfenceparameter
   \let\fence\math_fenced_middle
   \edef\p_math_fenced_color{\mathfenceparameter\c!color}%
   \ifx\p_math_fenced_color\empty
     \let\math_fenced_color_push\donothing
     \let\math_fenced_color_pop \donothing
   \else
     \let\math_fenced_color_push\math_fenced_color_do_push
     \let\math_fenced_color_pop \math_fenced_color_do_pop
   \fi
   \math_fenced_left}

\unexpanded\def\math_fenced_fenced_stop#1%
  {\edef\currentmathfence{#1}%
   \math_fenced_right
   \stopusemathstyleparameter
   \endgroup
   \advance\c_math_fenced_nesting\minusone}

\unexpanded\def\math_fenced_fenced[#1]#2%
  {\math_fenced_fenced_start{#1}%
   #2%
   \math_fenced_right
   \stopusemathstyleparameter
   \endgroup}

\appendtoks
    \let\fenced\math_fenced_fenced
\to \everymathematics

% todo: | in mathmode letter
%
% \appendtoks
%     \let\bar\letterbar
% \to \everymathematics
%
% but then we don't have it in embedded text too so ...

\definemathfence [parenthesis] [\c!left="0028,\c!right="0029]
\definemathfence [bracket]     [\c!left="005B,\c!right="005D]
\definemathfence [brace]       [\c!left="007B,\c!right="007D]
\definemathfence [bar]         [\c!left="007C,\c!right="007C]
\definemathfence [doublebar]   [\c!left="2016,\c!right="2016]
\definemathfence [triplebar]   [\c!left="2980,\c!right="2980]
\definemathfence [angle]       [\c!left="27E8,\c!right="27E9]
\definemathfence [doubleangle] [\c!left="27EA,\c!right="27EB]
\definemathfence [solidus]     [\c!left="2044,\c!right="2044]
\definemathfence [ceil]        [\c!left="2308,\c!right="2309]
\definemathfence [floor]       [\c!left="230A,\c!right="230B]
\definemathfence [moustache]   [\c!left="23B0,\c!right="23B1]
\definemathfence [uppercorner] [\c!left="231C,\c!right="231D]
\definemathfence [lowercorner] [\c!left="231E,\c!right="231F]
\definemathfence [group]       [\c!left="27EE,\c!right="27EF]
\definemathfence [openbracket] [\c!left="27E6,\c!right="27E7]

\definemathfence [nothing]

\definemathfence [mirrored] % \v!mirrored

\definemathfence [mirroredparenthesis] [mirrored] [\c!right="0028,\c!left="0029]
\definemathfence [mirroredbracket]     [mirrored] [\c!right="005B,\c!left="005D]
\definemathfence [mirroredbrace]       [mirrored] [\c!right="007B,\c!left="007D]
\definemathfence [mirroredbar]         [mirrored] [\c!right="007C,\c!left="007C]
\definemathfence [mirroreddoublebar]   [mirrored] [\c!right="2016,\c!left="2016]
\definemathfence [mirroredtriplebar]   [mirrored] [\c!right="2980,\c!left="2980]
\definemathfence [mirroredangle]       [mirrored] [\c!right="27E8,\c!left="27E9]
\definemathfence [mirroreddoubleangle] [mirrored] [\c!right="27EA,\c!left="27EB]
\definemathfence [mirroredsolidus]     [mirrored] [\c!right="2044,\c!left="2044]
\definemathfence [mirroredceil]        [mirrored] [\c!right="2308,\c!left="2309]
\definemathfence [mirroredfloor]       [mirrored] [\c!right="230A,\c!left="230B]
\definemathfence [mirroredmoustache]   [mirrored] [\c!right="23B0,\c!left="23B1]
\definemathfence [mirroreduppercorner] [mirrored] [\c!right="231C,\c!left="231D]
\definemathfence [mirroredlowercorner] [mirrored] [\c!right="231E,\c!left="231F]
\definemathfence [mirroredgroup]       [mirrored] [\c!right="27EE,\c!left="27EF]
\definemathfence [mirroredopenbracket] [mirrored] [\c!right="27E6,\c!left="27E7]

\definemathfence [mirrorednothing]     [mirrored]

%D A bonus:

\unexpanded\def\Lparent              {\math_fenced_fenced_start{parenthesis}}          \unexpanded\def\Rparent              {\math_fenced_fenced_stop {parenthesis}}
\unexpanded\def\Lbracket             {\math_fenced_fenced_start{bracket}}              \unexpanded\def\Rbracket             {\math_fenced_fenced_stop {bracket}}
\unexpanded\def\Lbrace               {\math_fenced_fenced_start{brace}}                \unexpanded\def\Rbrace               {\math_fenced_fenced_stop {brace}}
\unexpanded\def\Langle               {\math_fenced_fenced_start{angle}}                \unexpanded\def\Rangle               {\math_fenced_fenced_stop {angle}}
\unexpanded\def\Ldoubleangle         {\math_fenced_fenced_start{doubleangle}}          \unexpanded\def\Rdoubleangle         {\math_fenced_fenced_stop {doubleangle}}
\unexpanded\def\Lbar                 {\math_fenced_fenced_start{bar}}                  \unexpanded\def\Rbar                 {\math_fenced_fenced_stop {bar}}
\unexpanded\def\Ldoublebar           {\math_fenced_fenced_start{doublebar}}            \unexpanded\def\Rdoublebar           {\math_fenced_fenced_stop {doublebar}}
\unexpanded\def\Ltriplebar           {\math_fenced_fenced_start{triplebar}}            \unexpanded\def\Rtriplebar           {\math_fenced_fenced_stop {triplebar}}
\unexpanded\def\Lsolidus             {\math_fenced_fenced_start{solidus}}              \unexpanded\def\Rsolidus             {\math_fenced_fenced_stop {solidus}}
\unexpanded\def\Lfloor               {\math_fenced_fenced_start{floor}}                \unexpanded\def\Rfloor               {\math_fenced_fenced_stop {floor}}
\unexpanded\def\Lceil                {\math_fenced_fenced_start{ceil}}                 \unexpanded\def\Rceil                {\math_fenced_fenced_stop {ceil}}
\unexpanded\def\Lmoustache           {\math_fenced_fenced_start{moustache}}            \unexpanded\def\Rmoustache           {\math_fenced_fenced_stop {moustache}}
\unexpanded\def\Luppercorner         {\math_fenced_fenced_start{uppercorner}}          \unexpanded\def\Ruppercorner         {\math_fenced_fenced_stop {uppercorner}}
\unexpanded\def\Llowercorner         {\math_fenced_fenced_start{lowercorner}}          \unexpanded\def\Rlowercorner         {\math_fenced_fenced_stop {lowercorner}}
\unexpanded\def\Lgroup               {\math_fenced_fenced_start{group}}                \unexpanded\def\Rgroup               {\math_fenced_fenced_stop {group}}
\unexpanded\def\Lopenbracket         {\math_fenced_fenced_start{openbracket}}          \unexpanded\def\Ropenbracket         {\math_fenced_fenced_stop {openbracket}}
\unexpanded\def\Lnothing             {\math_fenced_fenced_start{nothing}}              \unexpanded\def\Rnothing             {\math_fenced_fenced_stop {nothing}}

\unexpanded\def\Lparentmirrored      {\math_fenced_fenced_stop {mirroredparenthesis}}  \unexpanded\def\Rparentmirrored      {\math_fenced_fenced_start{mirroredparenthesis}}
\unexpanded\def\Lbracketmirrored     {\math_fenced_fenced_stop {mirroredbracket}}      \unexpanded\def\Rbracketmirrored     {\math_fenced_fenced_start{mirroredbracket}}
\unexpanded\def\Lbracemirrored       {\math_fenced_fenced_stop {mirroredbrace}}        \unexpanded\def\Rbracemirrored       {\math_fenced_fenced_start{mirroredbrace}}
\unexpanded\def\Langlemirrored       {\math_fenced_fenced_stop {mirroredangle}}        \unexpanded\def\Ranglemirrored       {\math_fenced_fenced_start{mirroredangle}}
\unexpanded\def\Ldoubleanglemirrored {\math_fenced_fenced_stop {mirroreddoubleangle}}  \unexpanded\def\Rdoubleanglemirrored {\math_fenced_fenced_start{mirroreddoubleangle}}
\unexpanded\def\Lbarmirrored         {\math_fenced_fenced_stop {mirroredbar}}          \unexpanded\def\Rbarmirrored         {\math_fenced_fenced_start{mirroredbar}}
\unexpanded\def\Ldoublebarmirrored   {\math_fenced_fenced_stop {mirroreddoublebar}}    \unexpanded\def\Rdoublebarmirrored   {\math_fenced_fenced_start{mirroreddoublebar}}
\unexpanded\def\Ltriplebarmirrored   {\math_fenced_fenced_stop {mirroredtriplebar}}    \unexpanded\def\Rtriplebarmirrored   {\math_fenced_fenced_start{mirroredtriplebar}}
\unexpanded\def\Lsolidusmirrored     {\math_fenced_fenced_stop {mirroredsolidus}}      \unexpanded\def\Rsolidusmirrored     {\math_fenced_fenced_start{mirroredsolidus}}
\unexpanded\def\Lfloormirrored       {\math_fenced_fenced_stop {mirroredfloor}}        \unexpanded\def\Rfloormirrored       {\math_fenced_fenced_start{mirroredfloor}}
\unexpanded\def\Lceilmirrored        {\math_fenced_fenced_stop {mirroredceil}}         \unexpanded\def\Rceilmirrored        {\math_fenced_fenced_start{mirroredceil}}
\unexpanded\def\Lmoustachemirrored   {\math_fenced_fenced_stop {mirroredmoustache}}    \unexpanded\def\Rmoustachemirrored   {\math_fenced_fenced_start{mirroredmoustache}}
\unexpanded\def\Luppercornermirrored {\math_fenced_fenced_stop {mirroreduppercorner}}  \unexpanded\def\Ruppercornermirrored {\math_fenced_fenced_start{mirroreduppercorner}}
\unexpanded\def\Llowercornermirrored {\math_fenced_fenced_stop {mirroredlowercorner}}  \unexpanded\def\Rlowercornermirrored {\math_fenced_fenced_start{mirroredlowercorner}}
\unexpanded\def\Lgroupmirrored       {\math_fenced_fenced_stop {mirroredgroup}}        \unexpanded\def\Rgroupmirrored       {\math_fenced_fenced_start{mirroredgroup}}
\unexpanded\def\Lopenbracketmirrored {\math_fenced_fenced_stop {mirroredopenbracket}}  \unexpanded\def\Ropenbracketmirrored {\math_fenced_fenced_start{mirroredopenbracket}}
\unexpanded\def\Lnothingmirrored     {\math_fenced_fenced_stop {mirrorednothing}}      \unexpanded\def\Rnothingmirrored     {\math_fenced_fenced_start{mirrorednothing}}

% \startformula
% \left{ \frac{1}{a} \right}
% \left[ \frac{1}{b} \right]
% \left( \frac{1}{c} \right)
% \left< \frac{1}{d} \right>
% \left| \frac{1}{e} \right|
% \stopformula

\installcorenamespace{mathleft}
\installcorenamespace{mathright}
\installcorenamespace{mathmiddle}

\unexpanded\def\left  {\afterassignment\math_left  \let\nexttoken}
\unexpanded\def\right {\afterassignment\math_right \let\nexttoken}
\unexpanded\def\middle{\afterassignment\math_middle\let\nexttoken}

\newconditional\c_math_fenced_done
\newconditional\c_math_fenced_unknown  \settrue\c_math_fenced_unknown

% maybe use \detokenize ...

\def\math_left
  {\settrue\c_math_fenced_done
   \edef\m_math_left{\meaning\nexttoken}%
   \csname\??mathleft\ifcsname\??mathleft\m_math_left\endcsname\m_math_left\else\s!unknown\fi\endcsname}

\def\math_right
  {\settrue\c_math_fenced_done
   \edef\m_math_right{\meaning\nexttoken}%
   \csname\??mathright\ifcsname\??mathright\m_math_right\endcsname\m_math_right\else\s!unknown\fi\endcsname}

\def\math_middle
  {\settrue\c_math_fenced_done
   \edef\m_math_middle{\meaning\nexttoken}%
   \csname\??mathmiddle\ifcsname\??mathmiddle\m_math_middle\endcsname\m_math_middle\else\s!unknown\fi\endcsname}

\unexpanded\def\lfence#1%
  {\settrue\c_math_fenced_done
   \edef\m_math_left{\meaning#1}%
   \csname\??mathleft\ifcsname\??mathleft\m_math_left\endcsname\m_math_left\else\s!unknown\fi\endcsname}

\unexpanded\def\rfence#1%
  {\settrue\c_math_fenced_done
   \edef\m_math_right{\meaning#1}%
   \csname\??mathright\ifcsname\??mathright\m_math_right\endcsname\m_math_right\else\s!unknown\fi\endcsname}

\unexpanded\def\mfence#1%
  {\settrue\c_math_fenced_done
   \edef\m_math_middle{\meaning#1}%
   \csname\??mathmiddle\ifcsname\??mathmiddle\m_math_middle\endcsname\m_math_middle\else\s!unknown\fi\endcsname}

\setvalue{\??mathleft  \s!unknown}{\setfalse\c_math_fenced_done\ifconditional\c_math_fenced_unknown\normalleft  \nexttoken\fi}
\setvalue{\??mathright \s!unknown}{\setfalse\c_math_fenced_done\ifconditional\c_math_fenced_unknown\normalright \nexttoken\fi}
\setvalue{\??mathmiddle\s!unknown}{\setfalse\c_math_fenced_done\ifconditional\c_math_fenced_unknown\normalmiddle\nexttoken\fi}

\unexpanded\def\installmathfencepair#1#2#3#4%
  {\expandafter\let\csname\??mathleft \meaning#1\endcsname#2%
   \expandafter\let\csname\??mathright\meaning#3\endcsname#4}

\normalexpanded{\installmathfencepair {|\detokenize {|}} \Ldoublebar {|\detokenize {|}} \Rdoublebar}
\normalexpanded{\installmathfencepair {|\detokenize{||}} \Ltriplebar {|\detokenize{||}} \Rtriplebar}

\installmathfencepair \bgroup \Lbrace         \egroup \Rbrace
\installmathfencepair \egroup \Rbracemirrored \bgroup \Lbracemirrored

\installmathfencepair . \Lnothing            . \Rnothing
\installmathfencepair . \Rnothingmirrored    . \Lnothingmirrored

\installmathfencepair [ \Lbracket            ] \Rbracket
\installmathfencepair ] \Rbracketmirrored    [ \Lbracketmirrored

\installmathfencepair ( \Lparent             ) \Rparent
\installmathfencepair ) \Rparentmirrored     ( \Lparentmirrored

\installmathfencepair < \Langle              > \Rangle
\installmathfencepair > \Ranglemirrored      < \Langlemirrored

\installmathfencepair / \Lsolidus            / \Rsolidus
%installmathfencepair / \Rsolidusmirrored    / \Lsolidusmirrored

\installmathfencepair | \Lbar                | \Rbar
%installmathfencepair | \Rbarmirrored        | \Lbarmirrored

\installmathfencepair ⌊ \Lfloor              ⌋ \Rfloor
\installmathfencepair ⌋ \Rfloormirrored      ⌊ \Lfloormirrored
\installmathfencepair ⌈ \Lceil               ⌉ \Rceil
\installmathfencepair ⌉ \Rceilmirrored       ⌈ \Lceilmirrored

\installmathfencepair ⟨ \Langle              ⟩ \Rangle
\installmathfencepair ⟩ \Ranglemirrored      ⟨ \Langlemirrored

\installmathfencepair ⟪ \Ldoubleangle         ⟫ \Rdoubleangle
\installmathfencepair ⟫ \Rdoubleanglemirrored ⟪ \Ldoubleanglemirrored

\installmathfencepair ‖ \Ldoublebar           ‖ \Rdoublebar
%installmathfencepair ‖ \Rdoublebarmirrored   ‖ \Ldoublebarmirrored

\installmathfencepair ⦀ \Ltriplebar           ⦀ \Rtriplebar
%installmathfencepair ⦀ \Rtriplebarmirrored   ⦀ \Ltriplebarmirrored

% \installmathfencepair { \Lbrace          } \Rbrace
% \installmathfencepair } \Rbracemirrored  { \Lbracemirrored

\appendtoks
    \ignorediscretionaries % so $\mtext{a|b}$ works, this is ok because it's an \hbox
\to \everymathematics

% todo paren parent

\let\lbrack\lbracket
\let\rbrack\rbracket

\installmathfencepair \lbrace     \Lbrace       \rbrace     \Rbrace
\installmathfencepair \lbracket   \Lbracket     \rbracket   \Rbracket
\installmathfencepair \lparen     \Lparen       \rparen     \Rparen
\installmathfencepair \lparent    \Lparent      \rparent    \Rparent
\installmathfencepair \langle     \Langle       \rangle     \Rangle
\installmathfencepair \lrangle    \Ldoubleangle \rrangle    \Rdoubleangle
\installmathfencepair \lbar       \Lbar         \rbar       \Rbar
\installmathfencepair \lVert      \Ldoublebar   \rVert      \Rdoublebar
\installmathfencepair \vert       \Lbar         \vert       \Rbar
\installmathfencepair \solidus    \Lsolidus     \solidus    \Rsolidus
\installmathfencepair \lfloor     \Lfloor       \rfloor     \Rfloor
\installmathfencepair \lceil      \Lceil        \rceil      \Rceil

\installmathfencepair \ulcorner   \Luppercorner \ulcorner   \Ruppercorner
\installmathfencepair \llcorner   \Llowercorner \llcorner   \Rlowercorner
\installmathfencepair \lmoustache \Lmoustache   \lmoustache \Rmoustache
\installmathfencepair \llbracket  \Lopenbracket \llbracket  \Ropenbracket
\installmathfencepair \lgroup     \Lgroup       \lgroup     \Rgroup

% \setupmathfences[color=darkgreen]

\unexpanded\def\{{\mathortext\lbrace  \letterleftbrace   } % or maybe a chardef
\unexpanded\def\}{\mathortext\rbrace  \letterrightbrace  } % or maybe a chardef
\unexpanded\def\[{\mathortext\lbracket\letterleftbracket } % or maybe a chardef
\unexpanded\def\]{\mathortext\rbracket\letterrightbracket} % or maybe a chardef
\unexpanded\def\({\mathortext\lparent \letterleftparent  } % or maybe a chardef
\unexpanded\def\){\mathortext\rparent \letterrightparent } % or maybe a chardef
\unexpanded\def\|{\mathortext\vert    \letterbar         } % or maybe a chardef
%unexpanded\def\/{\mathortext\solidus \letterslash       } % or maybe a chardef

\installmathfencepair \{ \Lbrace   \} \Rbrace
\installmathfencepair \[ \Lbracket \] \Rbracket
\installmathfencepair \( \Lparent  \) \Rparent
\installmathfencepair \< \Langle   \> \Rangle
\installmathfencepair \| \Lbar     \| \Rbar

%D As we have overloaded \type {\left} and \type {\right} we also need a more
%D clever version of the following:

% methods:
%
% 1: none
% 2: lua
% 3: tex

% variants:
%
% 1: step 1
% 2: step 2
% 3: htdp * 1.33^n
% 4: size * 1.33^n

\setnewconstant\bigmathdelimitermethod \plusone
\setnewconstant\bigmathdelimitervariant\plusthree

\unexpanded\def\plainbigdelimiters % traditional method
  {\bigmathdelimitermethod\plustwo}

\plainbigdelimiters % is default for the moment but not so nice

% \setconstant\bigmathdelimitermethod\plusone

\installcorenamespace{mathbig}

\unexpanded\def\choosemathbig#1#2% so we accept \big{||} as well
  {{\hbox\bgroup
      \startimath
        \ifcase\bigmathdelimitermethod
          \math_fenced_step#2\relax
        \or
          \attribute\mathsizeattribute\numexpr\bigmathdelimitervariant*\plushundred+#1\relax
          \math_fenced_step#2\relax
        \else
          \math_fenced_step#2{\vbox to\getvalue{\??mathbig\number#1}\bodyfontsize{}}%
        \fi
        \nulldelimiterspace\zeropoint\relax
        \mathsurround\zeropoint
      \stopimath
    \egroup}}

\def\math_fenced_step#1#2%
  {\setfalse\c_math_fenced_unknown
   \setfalse\c_math_fenced_done
   \left#1\relax
   \ifconditional\c_math_fenced_done
     #2%
     \right.\relax
   \else
     \left.\relax
     #2%
     \setfalse\c_math_fenced_done
     \right#1\relax
     \ifconditional\c_math_fenced_done
     \else
       \right.\relax
     \fi
   \fi}

\unexpanded\def\mathdelimiterstep#1#2%
  {\begingroup
   \attribute\mathsizeattribute\numexpr\plushundred+#1\relax
   \math_fenced_step#2\relax
   \endgroup}

\setvalue{\??mathbig1}{0.85}
\setvalue{\??mathbig2}{1.15}
\setvalue{\??mathbig3}{1.45}
\setvalue{\??mathbig4}{1.75}

\definemathcommand [big]  {\choosemathbig\plusone  }
\definemathcommand [Big]  {\choosemathbig\plustwo  }
\definemathcommand [bigg] {\choosemathbig\plusthree}
\definemathcommand [Bigg] {\choosemathbig\plusfour }

\definemathcommand [bigl]  [open]  [one] {\big}
\definemathcommand [bigm]  [rel]   [one] {\big}
\definemathcommand [bigr]  [close] [one] {\big}
\definemathcommand [Bigl]  [open]  [one] {\Big}
\definemathcommand [Bigm]  [rel]   [one] {\Big}
\definemathcommand [Bigr]  [close] [one] {\Big}
\definemathcommand [biggl] [open]  [one] {\bigg}
\definemathcommand [biggm] [rel]   [one] {\bigg}
\definemathcommand [biggr] [close] [one] {\bigg}
\definemathcommand [Biggl] [open]  [one] {\Bigg}
\definemathcommand [Biggm] [rel]   [one] {\Bigg}
\definemathcommand [Biggr] [close] [one] {\Bigg}

%definemathfence [fancybracket] [bracket] [command=yes,color=red]

% experimental accents:
%
% \definemathoverextensible [top] [hoed]  ["FE302]
% \definemathoverextensible [top] [slang] ["FE303]

%D This is needed for mathml (used in mrow, so it gets reset):

\let\math_fences_saved_left  \left
\let\math_fences_saved_middle\middle
\let\math_fences_saved_right \right

% \def\math_fences_traced#1{\ruledhbox{\ttx#1\low{\the\c_math_fenced_nesting}}}

\unexpanded\def\math_fences_checked_left
  {%\math_fences_traced L%
   \math_fences_saved_left}

\unexpanded\def\math_fences_checked_middle
  {%\math_fences_traced M%
   \ifcase\c_math_fenced_nesting
     \expandafter\math_fences_saved_middle
   \else
     \expandafter\firstofoneargument
   \fi}

\unexpanded\def\math_fences_checked_right
  {%\math_fences_traced R%
   \ifcase\c_math_fenced_nesting
     \expandafter\firstofoneargument
   \else
     \expandafter\math_fences_saved_right
   \fi}

\newconditional\c_math_checked_done % only bars

\unexpanded\def\math_fences_checked_left_or_right
  {%\math_fences_traced B%
   \ifcase\c_math_fenced_nesting
     \settrue\c_math_checked_done
     \expandafter\math_fences_saved_left
   \else\ifconditional\c_math_checked_done
     \setfalse\c_math_checked_done
     \doubleexpandafter\math_fences_saved_right
   \else
     \doubleexpandafter\math_fences_saved_middle
   \fi\fi}

\unexpanded\def\math_fences_checked_start
  {\c_math_fenced_nesting\zerocount}

\unexpanded\def\math_fences_checked_stop
  {\ifcase\c_math_fenced_nesting\else
     \right.\relax % error, todo: nil spacing
     \expandafter\math_fences_checked_stop
   \fi}

\unexpanded\def\startcheckedfences
  {\begingroup
   \let\left  \math_fences_checked_left
   \let\middle\math_fences_checked_middle
   \let\right \math_fences_checked_right
   \math_fences_checked_start}

\unexpanded\def\stopcheckedfences
  {\math_fences_checked_stop
   \endgroup}

\let\leftorright\math_fences_checked_left_or_right % for bars

%D The next characters were used for constructing nicer extensibles but
%D nowadays we have real characters.

\Umathchardef\braceld=0 \defaultmathfamily "FF07A
\Umathchardef\bracerd=0 \defaultmathfamily "FF07B
\Umathchardef\bracelu=0 \defaultmathfamily "FF07C
\Umathchardef\braceru=0 \defaultmathfamily "FF07D

\protect
