As I am working on improving my answer to Different approach to literate programming for LaTeX, I have created a macro that automatically processes two arguments and formats indexes accordingly. The arguments are strings (not control sequences), with #1 being the type of macro and #2 being the name of the macro.
Here is a sample of the macro I am using. It relies on etoolbox and xstring.
\newcommand{\set@macro@type}[2]{
\StrSubstitute{#2}{@}{"@}[\entryname]
% Default definitions
\def\macro@type{#1}
\def\macro@format{#2}
\def\macro@index{#2@\string\texttt{#2}}
% Specific type definitions
\ifstrequal{#1}{m}
{\def\macro@type{}
\def\macro@format{\textbackslash#2}
\def\macro@index{%
\entryname @\string\texttt{{\string\textbackslash}\entryname}}}{}
\ifstrequal{#1}{l}
{\def\macro@type{length}
\def\macro@format{%
\entryname @\string\texttt{{\string\textbackslash}\entryname}}}{}
\ifstrequal{#1}{e}
{\def\macro@type{environment}
\def\macro@format{\textbackslash#2\par\textbackslash end#2}
\def\macro@index{%
\entryname @\string\texttt{{\string\textbackslash}\entryname}}}{}}
However, I would like to make it so that the user can add new types of macros with an interface such as:
\addmacro{<name>}{<type>}{<format>}{<index>}
which should append the following code to my existing macro:
\ifstrequal{#1}{<name>}
{\def\macro@type{<type>}
\def\macro@format{<format>}
\def\macro@index{<index>}}
Ideally, the \addmacro command could be starred, too, as I have an additional switch to toggle depending on the user's wishes.
So, my question is: how can I implement a macro that appends some code at the end of the main macro, and that can be used repeatedly. I would prefer to keep using etoolbox if possible (I have seen \apptocmd in the documentation but can't get it to work). The main difficulty which I am encountering is with the arguments #1 and #2 of \set@macro@style, which the user must be able refer to in some way. How could this be implemented? An explanation would be very welcome too.