The 'big picture' aim here is to make sure that no ligatures are applied: for example,
-- is converted to an en-dash in 'normal' circumstances as it's a ligature. The way this is done is to make the potential ligature characters 'safe' inside the verbatim environment by inserting a kern between them.
The detail you ask about is as follows. The macro
\do@noligs receives one escaped character at a time, for example
\-. This will have category code 'other', but to make it 'safe' the code needs to make it active. That's what the lower case business is for: much of this is a 'standard trick'. What happens is that
\lowercase can be used to change the character code of a token but leave the category code unchanged. So we need some 'ready made' active character to 'transform': that's where
~ comes in. Taking the example
\- The code does the following:
- an active character (
- Sets up a group so the change to lower case behaviour is local
- the lower case equivalent of
- Inserts a
\lowercase, which reads and therefore tokenizes its argument and substitutes the
-, but does not execute it at this stage
- Ends the group so that the case changing is returned to normal and so the definition for an active
- does not need to be made globally.
- Sets up the definition of
In the definition for
\kern\z@ is the key part as it will prevent for example
-- being a ligature, as the two characters have an invisible 'barrier' between them.
\leavevmode is a safety precaution, making sure that we will not be 'between paragraphs'. The latter happens if you just insert a kern directly in vertical mode (and we'd end up with a conceptually vertical kern).
The net result is that within the group created by the
verbatim environment, the standard TeX ligatures do not act and the output is as expected.