Fix Trac #5263: bug in chooseExternalIds
An identifier used in an unfolding wasn't getting marked as an external Id, which caused subsequent chaos. In understanding and fixing this I refactored some of chooseExternalIds. As a side benefit, the order in which it enumerates the free variables of the IdInfo is now deterministic (this was a to-do before).
Showing with 105 additions and 74 deletions