emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Diego Zamboni <diego@zzamboni.org>
To: Org-mode <emacs-orgmode@gnu.org>
Subject: Re: How to expand macros in tangled code blocks?
Date: Wed, 26 Aug 2020 00:52:30 +0200	[thread overview]
Message-ID: <CAGY83EfBuCJ+3-ZsmefsYsbhG=QBW3b9cfkx_sHCJDLj-C360g@mail.gmail.com> (raw)
In-Reply-To: <CAGY83EeV_xV1NKDZv3WpaP0T9q2tR33+6eK_qD8fchJ2MKf03w@mail.gmail.com>

[-- 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.


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 --]

      reply	other threads:[~2020-08-25 22:53 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-08-25  7:59 How to expand macros in tangled code blocks? Diego Zamboni
2020-08-25 22:52 ` Diego Zamboni [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAGY83EfBuCJ+3-ZsmefsYsbhG=QBW3b9cfkx_sHCJDLj-C360g@mail.gmail.com' \
    --to=diego@zzamboni.org \
    --cc=emacs-orgmode@gnu.org \


* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox


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).