%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 / 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.

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

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

\unprotect

% TOBIAS

\startmessages  dutch  library: symbols
  title: symbolen
      1: symboolset -- wordt geladen
\stopmessages

\startmessages  english  library: symbols
  title: symbols
      1: loading symbolset --
\stopmessages

\startmessages  german  library: symbols
  title: Symbole
      1: Lade Symboldatei --
\stopmessages

\startmessages  czech  library: symbols
  title: symboly
      1: nacita se soubor symbolu --
\stopmessages

\startmessages  italian  library: symbols
  title: simboli
      1: caricamento gruppo di simboli --
\stopmessages

\startmessages  norwegian  library: symbols
  title: symboler
      1: leser inn symbolsett --
\stopmessages

\startmessages  romanian  library: symbols
  title: simboluri
      1: se incarca setul de simboluri --
\stopmessages

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

% ss:tag     -> symbol
% ss:set:tag -> symbol out of set
% sstag      -> list of symbols in set

% \def\dodefinesymbol[#1][#2]%
%   {\ifx\currentsymboldef\empty
%      \setvalue{\??ss:#1}{#2}%
%    \else
%      \doifundefinedelse{\??ss\currentsymboldef}
%        {\let\currentsymbollist\empty}
%        {\edef\currentsymbollist{\csname\??ss\currentsymboldef\endcsname}}%
%      \addtocommalist{#1}\currentsymbollist
%      \setvalue{\??ss:\currentsymboldef:#1}{#2}%
%      \letvalue{\??ss\currentsymboldef}\currentsymbollist
%    \fi}

\def\dodefinesymbol[#1][#2]%
  {\ifx\currentsymboldef\empty
     \setvalue{\??ss:#1}{#2}%
   \else
     \setvalue{\??ss:\currentsymboldef:#1}{#2}%
     \addvalue{\??ss\currentsymboldef}{#1}%
   \fi}

\def\definesymbol
  {\dodoubleargument\dodefinesymbol}

\unexpanded\def\symbol       % This one always gobbles spaces,
  {\dodoubleempty\dosymbol}  % so never change it again!

%D Since symbols are used frequently in interactive
%D documents, we speed up this one.

\newif\ifnosymbol \newtoks\everysymbol

\def\dodosymbol#1% \relax's prevent lookahead problems
  {\nosymbolfalse{\the\everysymbol\csname\??ss:#1\endcsname\relax}\relax}

\beginTEX

\def\dosymbol[#1][#2]%
  {\nosymboltrue
   \ifsecondargument \@EA\ifx\csname\??ss:#1:#2\endcsname\relax\else
     \dodosymbol{#1:#2}%
   \fi \fi
   \ifnosymbol
     \edef\currentsymbol{#1}%
     \the\symbolsetups
     \ifnosymbol
       \redosymbol\currentsymbol
     \fi
   \fi}

\def\fetchsymbol#1%
  {\ifnosymbol
     \@EA\ifx\csname\??ss:#1:\currentsymbol\endcsname\relax\else
       \dodosymbol{#1:\currentsymbol}%
     \fi
   \fi}

\def\redosymbol#1%
  {\@EA\ifx\csname\??ss:#1\endcsname\relax\else\@EA\dodosymbol\fi{#1}}

\endTEX

\beginETEX \ifcsname

\def\dosymbol[#1][#2]%
  {\nosymboltrue
   \ifsecondargument \ifcsname\??ss:#1:#2\endcsname
     \dodosymbol{#1:#2}%
   \fi \fi
   \ifnosymbol
     \edef\currentsymbol{#1}%
     \the\symbolsetups
     \ifnosymbol
       \redosymbol\currentsymbol
     \fi
   \fi}

\def\fetchsymbol#1%
  {\ifnosymbol
     \ifcsname\??ss:#1:\currentsymbol\endcsname
       \dodosymbol{#1:\currentsymbol}%
     \fi
   \fi}

\def\redosymbol#1%
  {\ifcsname\??ss:#1\endcsname\@EA\dodosymbol\else\fi{#1}}

\endETEX

% % % % %
% this should go in symb-fig, to be loaded after core-fig

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

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

\def\figuresymbol
  {\dodoubleempty\dofiguresymbol}

\def\dofiguresymbol[#1][% #2]%
  {\externalfigure[#1][\c!reset=\v!yes,\c!symbol=\v!yes,\c!height=\defaultsymbolheight,}% #2]}

\appendtoks \resetexternalfigures \to \everysymbol

\def\definefiguresymbol
  {\dotripleempty\dodefinefiguresymbol}

\def\dodefinefiguresymbol[#1][#2][#3]%
  {\ifsecondargument
     \definesymbol[#1][{\dofiguresymbol[#2][#3]}]%
   \fi}

% but for the moment we keep it here
% % % % % %

%\def\objectsymbol[#1]%
%  {\dopresetfieldsymbol{#1}\dogetfieldsymbol{#1}}

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

\beginTEX

\def\xfetchsymbol#1%
  {\ifnosymbol
     \@EA\ifx\csname\??ss:#1:\currentsymbol\endcsname\relax\else\nosymbolfalse\fi
   \fi}

\def\xredosymbol#1%
  {\@EA\ifx\csname\??ss:#1\endcsname\relax\else\nosymbolfalse\fi}

\endTEX

\beginETEX

\def\xfetchsymbol#1%
  {\ifnosymbol
     \ifcsname\??ss:#1:\currentsymbol\endcsname\nosymbolfalse\fi
   \fi}

\def\xredosymbol#1%
  {\ifcsname\??ss:#1\endcsname\nosymbolfalse\fi}

\endETEX

\def\doifsymboldefinedelse#1%
  {\bgroup
   \edef\currentsymbol{#1}%
   \let\fetchsymbol\xfetchsymbol
  %\let\redosymbol \xredosymbol
   \nosymboltrue
   \the\symbolsetups
   \ifnosymbol
    %\redosymbol\currentsymbol
     \xredosymbol\currentsymbol
     \ifnosymbol
       \egroup\@EAEAEA\secondoftwoarguments
     \else
       \egroup\@EAEAEA\firstoftwoarguments
     \fi
   \else
     \egroup\@EA\firstoftwoarguments
   \fi}

%D \macros
%D   {setupsymbolset,startsymbolset}
%D
%D From these macro definitions one can deduce that symbols can
%D be grouped in 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{\y!setupsymbolset}
%D \showsetup{\y!startsymbolset}

\let\currentsymboldef\empty

\def\startsymbolset[#1]
  {\def\currentsymboldef{#1}}

\def\stopsymbolset
  {\let\currentsymboldef\empty}

\newtoks\symbolsetups

\def\setupsymbolset[#1]%
  {\prependtoksonce\fetchsymbol{#1}\to\symbolsetups}

\def\resetsymbolset
  {\symbolsetups\emptytoks}

\def\forcesymbolset[#1]%
  {\symbolsetups{\fetchsymbol{#1}}}

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

\fetchruntimecommand \showsymbolset {\f!symbolprefix\s!run}

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

\def\dousesymbols#1%
  {\makeshortfilename[\f!symbolprefix#1]%
   \showmessage\m!symbols1{#1}%
   \startreadingfile
     \readsysfile\shortfilename\donothing\donothing
   \stopreadingfile}

\def\usesymbols[#1]%
  {\processcommalist[#1]\dousesymbols}

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

\protect \endinput
