%D \module
%D   [       file=core-nav,
%D        version=1995.01.01,
%D          title=\CONTEXT\ Core Macros,
%D       subtitle=New ones,
%D         author=Hans Hagen,
%D           date=\currentdate,
%D      copyright={PRAGMA / Hans Hagen \& Ton Otten}]
%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 Core Macros / New Ones}

\unprotect

% todo : \startsetups[name][XML] \setups[name][XML]

% todo: \doglobal

\let\startsetups\relax % to please dep checker
\let\stopsetups \relax % to please dep checker

\expanded
  {\long\def\@EA\noexpand\csname\e!start\v!setups\endcsname
     {\begingroup\noexpand\doifnextcharelse[%
        {\noexpand\startsetupsA\@EA\noexpand\csname\e!stop\v!setups\endcsname}
        {\noexpand\startsetupsB\@EA\noexpand\csname\e!stop\v!setups\endcsname}}}

\letvalue{\e!stop\v!setups}\relax

\unexpanded \def\setups{\doifnextcharelse\bgroup\dosetupsA\dosetupsB} % {..} or [..]
\unexpanded \def\setup {\doifnextcharelse\bgroup\dosetups \dosetupsC} % {..} or [..]

% \def\dosetupsA  #1{\processcommacommand[#1]\dosetups} % {..}
% \def\dosetupsB[#1]{\processcommacommand[#1]\dosetups} % [..]
% \def\dosetupsC[#1]{\dosetups{#1}} % [..]
%
% better:

\def\dosetupsA  #1{\cleanuplabel{#1}\processcommacommand[\cleanlabel]\dosetups} % {..}
\def\dosetupsB[#1]{\cleanuplabel{#1}\processcommacommand[\cleanlabel]\dosetups} % [..]
\def\dosetupsC[#1]{\cleanuplabel{#1}\dosetups\cleanlabel} % [..]

% \def\dosetups#1% the grid option will be extended to other main modes
%   {\executeifdefined{\??su\ifgridsnapping\v!grid\fi:#1}{\csname\??su:#1\endcsname}\empty}

\def\dosetups#1% the grid option will be extended to other main modes
  {\executeifdefined{\??su\ifgridsnapping\v!grid\fi:#1}
  {\executeifdefined{\??su                         :#1}\empty}}

\let\directsetup\dosetups

\def\doifsetupselse#1% to be done: grid
  {\doifdefinedelse{\??su:#1}}

\chardef\setupseolmode\plusone

\def\startsetups     {\xxstartsetups\plusone  \stopsetups     } \let\stopsetups     \relax
\def\startlocalsetups{\xxstartsetups\plusone  \stoplocalsetups} \let\stoplocalsetups\relax
\def\startrawsetups  {\xxstartsetups\zerocount\stoprawsetups  } \let\stoprawsetups  \relax

\def\xxstartsetups#1#2%
  {\begingroup\chardef\setupseolmode#1\doifnextcharelse[{\startsetupsA#2}{\startsetupsB#2}}

\def\startsetupsA#1% [ ] delimited
  {\ifcase\setupseolmode\or\catcode`\^^M\@@ignore\fi
   \dotripleempty\dostartsetups[#1]}

\def\startsetupsB#1#2 % space delimited
  {\ifcase\setupseolmode\or\catcode`\^^M\@@ignore\fi
   \dodostartsetups#1\empty{#2}}

\def\startsetupsC[#1][#2][#3]{\dodostartsetups#1{#2}{#3}}   % [..] [..]
\def\startsetupsD[#1][#2][#3]{\dodostartsetups#1\empty{#2}} % [..]

\def\dostartsetups
  {\ifthirdargument\@EA\startsetupsC\else\@EA\startsetupsD\fi}

% \long\def\dodostartsetups#1#2#3% watch out: not \grabuntil
%   {\dograbuntil#1{\endgroup\dodoglobal\long\setvalue{\??su#2:#3}}} % \doglobal
%
% better:

\long\def\dodostartsetups#1#2#3% watch out: not \grabuntil
  {\cleanuplabel{\??su#2:#3}\dograbuntil#1{\endgroup\dodoglobal\long\setvalue\cleanlabel}} % \doglobal

\def\systemsetupsprefix{*}

\def\systemsetups#1{\dosetups{\systemsetupsprefix#1}}

\def\resetsetups[#1]% see x-fo for usage
  {\ifundefined{\??su\ifgridsnapping\v!grid\fi:#1}%
     \dodoglobal\letbeundefined{\??su:#1}%
   \else
     \dodoglobal\letbeundefined{\??su\ifgridsnapping\v!grid\fi:#1}%
   \fi}

% or
%
% \def\resetsetups[#1]%
%   {\letbeundefined
%      {\??su:%
%       \ifundefined{\??su\ifgridsnapping\v!grid\fi:#1}#1\else\ifgridsnapping\v!grid\fi%
%       #1}}

%D new and beta

% \def\defineshortcut
%   {\dodoubleargument\dodefineshortcut}
%
% \bgroup
%
%   \catcode`\<=\@@active
%
%   \gdef\dodefineshortcut[#1][#2]%
%     {\ifsecondargument
%        \catcode`\<=\@@active
%        \def<{\ifmmode\expandafter\normalless\else\expandafter\doshortcut\fi}%
%        \getparameters[\??te#1][\c!commands=,\c!command=,\c!style=,\c!color=,#2]%
%      \else
%        \defineshortcut[][#1]%
%      \fi}
%
% \egroup
%
% \def\doshortcut
%   {\bgroup
%    \catcode`\>=\@@other
%    \dodoshortcut}
%
% \def\dodoshortcut#1>%
%   {\def\shortcut{#1}%
%    \dododoshortcut#1:\end}
%
% \def\dododoshortcut#1:#2\end
%   {\doifelsenothing{#2}
%      {\doifundefinedelse{\??te\c!commands}
%         {\shortcut}
%         {\@EA\dodododoshortcut\@EA\??te\@EA:\shortcut:\end}}
%      {\doifundefinedelse{\??te#1\c!commands}
%         {\shortcut}
%         {\dodododoshortcut\??te#1:#2\end}}%
%    \egroup}
%
% \def\dodododoshortcut#1:#2:\end
%   {\getvalue{#1\c!commands}%
%    \doattributes{#1}\c!style\c!color{\getvalue{#1\c!command}{#2}}}

\def\defineshortcut
  {\dotripleargument\dodefineshortcut}

\def\dodefineshortcut[#1][#2][#3]%
  {\ifthirdargument
     \ConvertConstantAfter\doifelse{#1}{}
       {\dododefineshortcut[<>][#2][#3]}
       {\dododefineshortcut[#1][#2][#3]}%
   \else\ifsecondargument
     \dododefineshortcut[<>][#1][#2]%
   \else
     \dododefineshortcut[<>][][#1]%
   \fi\fi}

\def\dododefineshortcut[#1#2][#3][#4]% #1 is the trigger, #2 the delimiter/tag
  {\doifundefined{\??te\??te\string#2}{\letvalue{\??te\??te\string#2}=#1}%
   \defineactivecharacter #1 {\@EA\doshortcut\string#2} %
   \getparameters
     [\??te\string#2#3]
     [\c!commands=,\c!command=,\c!style=,\c!color=,#4]}

\def\doshortcut#1%
  {\ifmmode
     \getvalue{\??te\??te#1}%
   \else
     \bgroup
     \catcode`#1=\@@other
     \def\dodoshortcut##1#1%
       {\def\shorttag{\??te#1}%
        \def\shortcut{##1}%
        \dododoshortcut##1:\end}%
     \@EA\dodoshortcut
   \fi}

\def\dododoshortcut#1:#2\end
  {\doifelsenothing{#2}
     {\doifundefinedelse{\shorttag\c!commands}
        {\shortcut}
        {\@EA\dodododoshortcut\@EA\shorttag\@EA:\shortcut:\end}}
     {\doifundefinedelse{\shorttag#1\c!commands}
        {\shortcut}
        {\dodododoshortcut\shorttag#1:#2\end}}%
   \egroup}

\def\dodododoshortcut#1:#2:\end
  {\getvalue{#1\c!commands}%
   \doattributes{#1}\c!style\c!color{\getvalue{#1\c!command}{#2}}}

%D \defineshortcut     [style=type]
%D \defineshortcut [b] [style=bold]
%D \defineshortcut [e] [style=\em]
%D \defineshortcut [t] [style=type]
%D \defineshortcut [c] [style=cap]
%D \defineshortcut [k] [style=cap]
%D \defineshortcut [u] [style=type,command=\hyphenatedurl]
%D
%D \startlines
%D test <ziezo> test
%D test test <t:ziezo>
%D test test <b:ziezo>
%D test test <w:ziezo>
%D zus<>zo zus<:>zo zus<::>zo
%D test test <t:ziezo> dat (ziezo)
%D test test <t::ziezo> dat (:ziezo)
%D test test <t:ziezo:> dat (ziezo:)
%D test test <t:zi:ezo:> dat (zi:ezo:)
%D well, <u:http://www.pragma-ade.nl> looks fuzzy
%D $10<20$
%D \stoplines
%D
%D \defineshortcut [<>] [i] [style=\it]
%D \defineshortcut [()] [b] [style=\bf]
%D \defineshortcut [++] [s] [style=\sl]
%D \defineshortcut [//] [u] [style=\underbars]
%D \defineshortcut [--] [a] [style=\overstrike]
%D
%D \startlines
%D it seems <i:to work> well
%D it seems (b:to work) well
%D it seems +s:to work+ well
%D it seems /u:to work/ well
%D it seems -a:to work- well
%D \stoplines

% \def\setupenv{\dodoubleargument\rawgetparameters[\??en]}
%
% \def\doifenvelse#1{\doifdefinedelse{\??en#1}} % speed up
% \def\doifenv    #1{\doifdefined    {\??en#1}} % speed up
% \def\doifnotenv #1{\doifundefined  {\??en#1}} % speed up
%
% \def\env#1{\csname\??en#1\endcsname}
%
% \beginTEX
%
% \def\envvar#1#2%
%   {\@EA\ifx\csname\??en#1\endcsname\relax
%      #2\else\csname\??en#1\endcsname
%    \fi}
%
% \endTEX
%
% \beginETEX \ifcsname
%
% \def\envvar#1#2%
%   {\ifcsname\??en#1\endcsname
%      \csname\??en#1\endcsname\else#2%
%    \fi}
%
% \endETEX

% low level change, now also accessible as \getvariable{environment}{...}; the
% next macros will become obsolete some day in favor of normal variables

\def\s!environment{environment}

\def\setupenv   {\dotripleargument\dosetvariables[\getrawparameters][\s!environment]}
\def\doifenvelse{\doifelsevariable  \s!environment}
\def\doifenv    {\doifvariable      \s!environment}
\def\doifnotenv {\doifnotvariable   \s!environment}
\def\env        {\getvariable       \s!environment}
\def\envvar     {\getvariabledefault\s!environment}

\protect \endinput
