The current implementation of the code is unwieldy and needs to be broken up into separate functions. This would also allow modules to add macro processing hooks if desired. An API function such as
add_macro("mymacro",my_function)
would be added to the module API.
Diff: