This part of
etoolbox is rather tricky, but not really complicated, once one gets the idea.
When LaTeX executes
\usepackage, the named file is loaded and
@ is considered as a letter, which makes it possible to use it in macro names.
etoolbox package needs to change category codes in certain parts of it, so it wants to be sure to restore those settings at the end.
Here is the code:
The first instruction stores a list of the characters that potentially change their category code in the package, to be restored at the end.
\do macro is a traditional macro for defining lists, and it can be redefine to execute the list. In this case it is redefined so that, for instance,
\do\& expands to
If the code after the definition of
\do was just
\etb@catcodes, TeX would transform the above code into
which would be useless, because this is the current setting. Instead,
etoolbox wants to save these settings for the end, so it does
which does similar complete expansions for all stored characters. Since
\edef does complete expansion before assigning the meaning to
\etb@catcodes, there's no danger of infinite loop; at the end, the definition of
\etb@catcodes is equivalent to
but with a fundamental difference: if some package has changed the category code of, say,
| to 13, there would be
in the definition of
At the end of the package,
\etb@catcodes is executed, so restoring all category codes to the previous state and the macro is undefined.