emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* How to expand macros in tangled code blocks?
@ 2020-08-25  7:59 Diego Zamboni
  2020-08-25 22:52 ` Diego Zamboni
  0 siblings, 1 reply; 2+ messages in thread
From: Diego Zamboni @ 2020-08-25  7:59 UTC (permalink / raw)
  To: Org-mode

[-- Attachment #1: Type: text/plain, Size: 286 bytes --]

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

[-- Attachment #2: Type: text/html, Size: 495 bytes --]

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: How to expand macros in tangled code blocks?
  2020-08-25  7:59 How to expand macros in tangled code blocks? Diego Zamboni
@ 2020-08-25 22:52 ` Diego Zamboni
  0 siblings, 0 replies; 2+ messages in thread
From: Diego Zamboni @ 2020-08-25 22:52 UTC (permalink / raw)
  To: Org-mode

[-- Attachment #1: Type: text/plain, Size: 1769 bytes --]

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 <diego@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
>
>

[-- Attachment #2: Type: text/html, Size: 2548 bytes --]

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2020-08-25 22:53 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-08-25  7:59 How to expand macros in tangled code blocks? Diego Zamboni
2020-08-25 22:52 ` Diego Zamboni

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).