%% FILE: tokmac.tex
%% AUTHOR: Wai WONG
%% DATE: 26 April 1993
%%
%% Copyright (C) 1992, 1993 Wai Wong
%%
%% This program is free software; you can redistribute it and/or modify
%% it under the terms of the GNU General Public License as published by
%% the Free Software Foundation; either version 2, or (at your option)
%% any later version.
%%
%% This program is distributed in the hope that it will be useful,
%% but WITHOUT ANY WARRANTY; without even the implied warranty of
%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
%% GNU General Public License for more details.
%%
%% You should have received a copy of the GNU General Public License
%% along with this program; if not, write to the Free Software
%% Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
%%
%%
%% token name macros
%% 
%% Token name macros provide a convenient way of formatting and indexing
%% tokens. For example, if a class of tokens, such as identifiers in a
%% programming language, need to be typeset in a
%% special font and automatically indexed, one can define a macro, say
%% \TOKEN for this purpose. When this macro is used,
%% e.g., \TOKEN{foo}, `foo' will be set in a pre-defined font and an
%% index entry is generated automatically. This definition can be
%% automated. The macro
%%	\newtokmac{<name>}{<font>}
%% defines a new token macro whose name is <name>. This new macro
%% takes a single argument. It will be type set in the font specified
%% by <font>. An index entry is generated for every use of this
%% macro, and it will be set in the same font. The automatic
%% generation of index can be disabled by the command \indexfalse. It
%% can also be re-enable by the command \indextrue. A star variant of the
%% new macro is defined at the same time, i.e., \<name>* can be used.
%% When the satr variant is use, no index is generated no matter what
%% is the current setting of the index generation feature. The
%% argument of the token name macro can contain any ordinary character
%% plus underline(_).

% check to see if this macro file has already been loaded
\ifx\undefined\tokmac\def\tokmac{}\else\endinput\fi

\makeatletter
%% 
%% The macro \makeulother changes the catcode of the underline
%% character. It is called just before calling \dotoken or
%% \dotokenidx. It can be redefined to do other things which may
%% affect how TeX processes the token word.

\def\makeulother{\catcode`\_=12\relax}
%%
%% The token name macro  is implemented the macros: \dotoken,
%% \dotokenidx and \idxname. \dotaken is called when no index is
%% generated, while \dotokenidx is called when index is required. When
%% these macros are called, TeX is in a group in which the catcode of
%% the underline character has been changed to `other', but the actual
%% argument to the token name macro has not been read. These two
%% macros take two arguments: the first if a command for changing
%% font, and the second is the token word. the macro is called when
%% the index is processed.

\def\dotoken#1#2{\mbox{#1#2}\endgroup}
\def\idxname#1{\begingroup\makeulother\dotoken#1}
\def\dotokenidx#1#2{\mbox{#1#2}\index{#2@\string\idxname{#1}{#2}}\endgroup}
%%
%% The automatic generation of index is controlled by the conditional
%% \ifindex. The default is TRUE, i.e., the feature is enabled.
%%
\newif\ifindex \indextrue
%%
%% 
\def\@nametok{\begingroup\makeulother\dotoken}
\def\@nametokidx{\begingroup\makeulother\dotokenidx}
\def\@nametokidxtest{%
 \ifindex\let\doidx=\@nametokidx\else\let\doidx=\@nametok\fi\doidx}

\def\newtokmac#1#2{%
 \expandafter\def\csname#1\endcsname{\@ifstar{\@nametok{#2}}{\@nametokidxtest{#2}}}}

\makeatother
