To follow up on my own question, I figured out a solution. I added three
hooks with the following local variables:

# Local Variables:
# eval: (add-hook 'org-babel-pre-tangle-hook (lambda () (setq
zz/saved-macro-templates org-macro-templates)) :append :local)
# eval: (add-hook 'org-babel-post-tangle-hook (lambda () (makunbound
'zz/saved-macro-templates)) :append :local)
# eval: (add-hook 'org-babel-tangle-body-hook (lambda () (when (boundp
'zz/saved-macro-templates) (org-macro-replace-all
zz/saved-macro-templates))) :append)
# End:

This works! The first hook saves the current buffer's macros to a temporary
variable before starting the tangle, which gets unbound by the second hook,
and the third one uses it on each code block's body to expand the macros.

This feels a bit clunky, because of the need to define multiple hooks, but
also because the third hook (and the temporary variable) cannot be made
buffer-local, because the operation happens across different buffers (each
=org-babel-tangle-body-hook= runs in a new temporary buffer with that
block's body in it). So the third hook will remain in effect for any other
org buffers I edit in the same session. Since the first two hooks are
buffer-local, it will have no effect, but it still feels hacky.

If anyone has ideas for how this could be done in a better/cleaner way, I'd
love to hear about it.

Cheers,
--Diego


On Tue, Aug 25, 2020 at 9:59 AM Diego Zamboni <di...@zzamboni.org> wrote:

> Hi everyone,
>
> Is it possible to expand Org macros in tangled code blocks? I found this
> old message:
> https://lists.gnu.org/archive/html/emacs-orgmode/2011-03/msg00857.html,
> but the mentioned org-export-preprocess-apply-macros doesn't seem to exist
> anymore.
>
> Thanks for any tips,
> --Diego
>
>

Reply via email to