Improve mkName, so that it correctly parses the name ^..

This fixes Trac #8633; thanks to aavogt for a first draft.