Implement support for using custom variables as macros.

Fixes #5364