%D \module
%D   [       file=symb-ini,
%D        version=1998.07.20,
%D          title=\CONTEXT\ Symbol Libraries,
%D       subtitle=Basic Symbols Commands,
%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.

%D The macros described here used to be part of the \type {core-con} module. I
%D decided to move them here when symbolsets saw the light. Let their light shine.

\writestatus{loading}{ConTeXt Symbol Libraries / Initialization}

\registerctxluafile{symb-ini}{autosuffix}

\unprotect

%D \macros
%D   {definesymbol, symbol}
%D
%D Converting numbers or levels into a character, romannumeral, symbol or something
%D else, is supported by many \CONTEXT\ commands. Therefore we need a mechanism for
%D linking such numbers to their counterparts.
%D
%D First we take care of symbols. These are for instance used in enumerations and
%D itemizations. We have:
%D
%D \showsetup{definesymbol}
%D \showsetup{symbol}
%D
%D Symbols are simply linked to a tag. Such tags can be numbers or strings.
%D
%D \starttyping
%D \definesymbol [1]       [$\bullet$]
%D \definesymbol [level 5] [$\star$]
%D \stoptyping

\installcorenamespace{symbol}
\installcorenamespace{symbols}
\installcorenamespace{symbolset}
\installcorenamespace{symboldefault}

%D For now we only have one option.

\installparameterhandler\??symbols {symbols}
\installsetuphandler    \??symbols {symbols}

\appendtoks
    \ifcstok{\symbolsparameter\c!stylealternative}\v!math
      \prefermathovertextchar\conditionaltrue
    \else
      \prefermathovertextchar\conditionalfalse
    \fi
\to \everysetupsymbols

\setupsymbols
  [\c!stylealternative=\v!text]

\mutable\lettonothing\currentsymbol
\mutable\lettonothing\currentsymbolset

\newtoks\t_symb_setups

\lettonothing\m_symb_current_set

\newconditional\c_symb_found

\newtoks\everysymbol

%D We don't use the commandhandler as symbols have their own subsystem for resolving
%D values.

% \permanent\tolerant\protected\def\definesymbol[#1]#*[#2]#*[#3]%
%   {\ifparameter#3\or
%      \defcsname\??symbol#1:#2\endcsname{#3}%
%      \ifempty{#1}\else\addvalue{\??symbolset#1}{#2}\fi
%    \else
%      \defcsname\??symbol\m_symb_current_set:#1\endcsname{#2}%
%      \addvalue{\??symbolset\m_symb_current_set}{#1}%
%    \fi}

\permanent\tolerant\protected\def\definesymbol[#1]#*[#2]#*[#3]%
  {\ifparameter#3\or
     \defcsname\??symbol#1:#2\endcsname{#3}%
     \ifempty{#1}\else\symb_register{#1}{#2}\fi
   \else
     \defcsname\??symbol\m_symb_current_set:#1\endcsname{#2}%
     \symb_register{\m_symb_current_set}{#1}%
   \fi}

\permanent\tolerant\protected\def\symb_register#1#2%
  {\ifcsname\??symbolset#1\endcsname
     \expanded{\addtocommalist{#2}\expandafter\noexpand\lastnamedcs}%
   \else
     \edefcsname\??symbolset#1\endcsname{#2}%
   \fi}

\permanent\protected\def\doifelseinsymbolset#1#2{\ifcsname\??symbol#1:#2\endcsname\expandafter\firstoftwoarguments\else\expandafter\secondoftwoarguments\fi}
\permanent\protected\def\doifinsymbolset    #1#2{\ifcsname\??symbol#1:#2\endcsname\expandafter\firstofoneargument \else\expandafter\gobbleoneargument   \fi}
\permanent\protected\def\doifelsesymbolset    #1{\ifcsname\??symbolset#1\endcsname\expandafter\firstoftwoarguments\else\expandafter\secondoftwoarguments\fi}

\aliased\let\doifinsymbolsetelse\doifelseinsymbolset
\aliased\let\doifsymbolsetelse  \doifelsesymbolset

\letcsname\??symbolset\endcsname\empty

%def\symbolset#1{\csname\??symbolset\ifcsname\??symbolset#1\endcsname#1\fi\endcsname} % no [#1], to be used in commalists etc

\permanent\def\symbolset#1{\begincsname\??symbolset#1\endcsname} % no [#1], to be used in commalists etc

%D Since symbols are used frequently in interactive documents, we speed up this one.
%D Well, that was history, since now we simplified things a bit, because the low
%D level macros have been sped up every now and then.

% We support both:
%
% Test test \symbol[whatever]\ test \symbol[whatever].
% Test test \symbol{whatever}  test \symbol{whatever}.

\permanent\tolerant\protected\def\symbol[#1]#*[#2]%
  {\ifarguments
     \expandafter\symb_place_argument
   \or
     \symb_place_argument{#1}%
   \or
     \dontleavehmode
     \edef\currentsymbol{#2}%
     \ifcsname\??symbol#1:#2\endcsname
       \symb_place_indeed{#1:#2}% maybe use \lastnamescs
     \orelse\ifcsname\??symboldefault#1\endcsname
       \symb_place_named{#1}% maybe use \lastnamescs
     \else
       \symb_place_normal
     \fi
   \fi}

\def\symb_place_argument#1%
  {\dontleavehmode
   \edef\currentsymbol{#1}%
   \ifcsname\??symbol\currentsymbolset:#1\endcsname
     \symb_place_indeed{\currentsymbolset:#1}%
   \else
    \symb_place_normal
   \fi}

\def\symb_place_normal
  {\c_symb_found\conditionalfalse
   \expand\t_symb_setups
   \ifconditional\c_symb_found \else
     \symb_place_retry\currentsymbol % hm, isn't this redundant?
   \fi}

\def\symb_place_indeed#1% \relax's prevent lookahead problems
  {\c_symb_found\conditionaltrue
   \begingroup
   \expand\everysymbol
   \csname\??symbol#1\endcsname\relax
   \endgroup}

\letcsname\??symbol\endcsname\firstofoneargument

\permanent\def\directsymbol#1#2% no \relax, there can be an argument, see lists
  {\begincsname\??symbol#1:#2\endcsname}

\protected\def\symb_fetch
  {\ifconditional\c_symb_found
     \expandafter\gobbleoneargument
   \else
     \expandafter\symb_fetch_indeed
   \fi}

\def\symb_fetch_indeed#1%
  {\ifcsname\??symbol#1:\currentsymbol\endcsname
     \symb_place_indeed{#1:\currentsymbol}%
   \orelse\ifcsname\??symboldefault#1\endcsname
     \symb_place_named{#1}%
   \fi}

\def\symb_place_named#1% \relax's prevent lookahead problems
  {\begingroup
   \setbox\scratchbox\hbox\bgroup
     \expand\everysymbol
     \getglyphstyled
       {\csname\??symboldefault#1\endcsname}%
       {\tochar{n:\currentsymbol}}%
     \relax
   \egroup
   \ifdim\wd\scratchbox>\zeropoint
     \unhbox\scratchbox
     \endgroup
     \xdefcsname\??symbol#1:\currentsymbol\endcsname{\symb_place_named_indeed{#1}{\currentsymbol}}%
     \c_symb_found\conditionaltrue
   \else
     \endgroup
   \fi}

\protected\def\symb_place_named_indeed#1#2% \relax's prevent lookahead problems
  {\c_symb_found\conditionaltrue
   \begingroup
   \expand\everysymbol
   \getglyphstyled
     {\csname\??symboldefault#1\endcsname}%
     {\tochar{n:#2}}%
   \relax
   \endgroup}

\def\symb_place_retry#1%
  {\ifcsname\??symbol:#1\endcsname
     \symb_place_indeed{:#1}%
   \else
     #1%
   \fi}

%D \macros
%D   {definefiguresymbol}
%D
%D To simplify defining figure symbols, we offer:
%D
%D \showsetup{definefiguresymbol}
%D
%D By default, such symbols scale along the current bodyfont size or running font
%D size (which is better).

\ifdefined\externalfigure \else \def\externalfigure[#1][#2]{#1} \fi

\mutable\def\defaultsymbolfactor{10}
\mutable\def\defaultsymbolheight{1.25ex}

\permanent\tolerant\protected\def\figuresymbol[#1]#,[#S#2]%
  {\externalfigure[#1][\c!reset=\v!yes,\c!symbol=\v!yes,\c!height=\defaultsymbolheight,#2]}

\permanent\tolerant\protected\def\definefiguresymbol[#1]#*[#2]#*[#S#3]%
  {\ifarguments\or\else
     \definesymbol[#1][{\figuresymbol[#2][#3]}]%
   \fi}

%D \macros
%D   {doifsymboldefinedelse}
%D
%D A handy private one:

\def\symb_fetch_first
  {\ifconditional\c_symb_found
     \expandafter\gobbleoneargument
   \else
     \expandafter\symb_fetch_first_indeed
   \fi}

\def\symb_fetch_first_indeed#1%
  {\doifinsymbolset{#1}\currentsymbol{\c_symb_found\conditionaltrue}}

\def\symb_fetch_second#1%
  {\doifinsymbolset\empty\currentsymbol{\c_symb_found\conditionaltrue}}

\permanent\protected\def\doifelsesymboldefined#1%
  {\begingroup
   \edef\currentsymbol{#1}%
   \let\symb_fetch\symb_fetch_first
   \c_symb_found\conditionalfalse
   \expand\t_symb_setups
   \ifconditional\c_symb_found
     \endgroup\expandafter\firstoftwoarguments
   \else
     \symb_fetch_second\currentsymbol
     \ifconditional\c_symb_found
       \endgroup\doubleexpandafter\firstoftwoarguments
     \else
       \endgroup\doubleexpandafter\secondoftwoarguments
     \fi
   \fi}

\aliased\let\doifsymboldefinedelse\doifelsesymboldefined

%D \macros
%D   {setupsymbolset,startsymbolset}
%D
%D From these macro definitions one can deduce that symbols can be grouped in
%D symbol sets:
%D
%D \starttyping
%D \startsymbolset [navigation 1]
%D   \definefiguresymbol [Next] [mp-symb.1]
%D   \definefiguresymbol [Prev] [mp-symb.2]
%D \stopsymbolset
%D \stoptyping
%D
%D Such a symbol can be typeset with:
%D
%D \starttyping
%D \setupsymbolset[navigation 1]\symbol[Next]
%D \stoptyping
%D
%D or simply:
%D
%D \starttyping
%D \symbol[navigation 1][Next]
%D \stoptyping
%D
%D Formally:
%D
%D \showsetup{setupsymbolset}
%D \showsetup{startsymbolset}

\installcorenamespace{symbolsets}

\permanent\tolerant\protected\def\startsymbolset[#1]#*[#S#2]%
  {\pushmacro\m_symb_current_set
   \def\m_symb_current_set{#1}%
   \ifarguments\or\or
     \getdummyparameters[\s!font=,#2]%
     \edef\p_font{\dummyparameter\s!font}%
     \ifempty\p_font\else
       \letcsname\??symboldefault#1\endcsname\p_font
     \fi
   \fi}

\permanent\protected\def\stopsymbolset
  {\popmacro\m_symb_current_set}

\permanent\protected\def\setupsymbolset[#1]% should have been \setsymbolset
  {\cdef\currentsymbolset{#1}%
   \ifcsname\??symbolsets\currentsymbolset\endcsname \else
     \symb_setup_symbol_set
   \fi}

\def\symb_setup_symbol_set
  {\expanded{\t_symb_setups{\symb_fetch{\currentsymbolset}\expand\t_symb_setups}}%
   \letcsname\??symbolsets\currentsymbolset\endcsname\empty} % speedup

\permanent\protected\def\resetsymbolset
  {\t_symb_setups\emptytoks}

\permanent\protected\def\forcesymbolset[#1]% still needed? why not just usesymbolset
  {\expanded{\t_symb_setups{\symb_fetch{#1}}}}

%D \macros
%D   {showsymbolset}
%D
%D \showsetup{showsymbolset}

\fetchmodulecommand \showsymbolset \f!symb_run

%D \macros
%D   {usesymbols}
%D
%D \showsetup{usesymbols}

\permanent\protected\def\usesymbols[#1]{\clf_usesymbols{#1}}

%D As longs as symbols are linked to levels or numbers, we can also use the
%D conversion mechanism, but in for instance the itemization macros, we prefer
%D symbols because they can more easier be (partially) redefined.

\protect \endinput
