From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp0 ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms11 with LMTPS id oAYuA1iWRV/EJQAA0tVLHw (envelope-from ) for ; Tue, 25 Aug 2020 22:53:12 +0000 Received: from aspmx1.migadu.com ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp0 with LMTPS id cOiKOleWRV8XeQAA1q6Kng (envelope-from ) for ; Tue, 25 Aug 2020 22:53:11 +0000 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id BEC519404E1 for ; Tue, 25 Aug 2020 22:53:10 +0000 (UTC) Received: from localhost ([::1]:33818 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kAhoS-0003E8-QD for larch@yhetil.org; Tue, 25 Aug 2020 18:53:08 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:60528) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kAho5-0003E0-9q for emacs-orgmode@gnu.org; Tue, 25 Aug 2020 18:52:45 -0400 Received: from mail-ed1-x533.google.com ([2a00:1450:4864:20::533]:42462) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1kAho3-00076S-8v for emacs-orgmode@gnu.org; Tue, 25 Aug 2020 18:52:44 -0400 Received: by mail-ed1-x533.google.com with SMTP id l63so37770edl.9 for ; Tue, 25 Aug 2020 15:52:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=zzamboni-org.20150623.gappssmtp.com; s=20150623; h=mime-version:references:in-reply-to:from:date:message-id:subject:to; bh=Rs6TceISAUEV+iXYuccSwGv68E9rgboB2fCaaNS24yo=; b=aWkqo+P3J2+7T3Uwt3J59aI8qafGc83DIG06kWUZ95h9I0dFG55CbycOAQueBI1WL7 7ZhWUP9bU6v87ake71S5VKxR1exoj8dKjAMOCAEmHO6eAzHrZSiuH8KZ9pN0ueRK/vJb tBlQaZygCl5Aw4oejEcjHSMp568g8ZhcfHxqgXFcqEsG4EmlIJdqEEk5VY4g0oP4gCc3 FHXAmH2hMO7bpDeJ0O7ofAG5ajwk1RfanUaN+2ulrf6T7Hr6pXGeYUpH9zXeNPbMFdx/ QZY/mAF7iMs8P7ey3x3wxZqDsJCgrIBzhY9WEKg6M/m9ejx6komZZj78ecMR2WyT3Xz6 jrjA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to; bh=Rs6TceISAUEV+iXYuccSwGv68E9rgboB2fCaaNS24yo=; b=CYj2DvlSEenzMjIUqLpRyEn9xMQKGN4QTXrig6Kx1q8J/t+gb+53sL9po+hG+uvLnC ewfBufi2HCWKCyZI62oUtvZbX4KUR3kT6IDm4BR+gZ2GfrWIOjObQ7WMgp0n0HoidPYz zsAUJhliEpM1NC/+UyOQAO6yvd0CKPMGMkmJi5G7UdXsLMZJ8Pa+jwurUqPx9ye6yKPL kqcY8fHPFH0wg3BDd0SsWSJ5a5myUDPkTARmkPMhvzKD1fY/cqLNxO6fv8k91OTeDv3m jM+xXxjSV0xtjuHkRTd2OvFChuGVXZoXnchooM17i2tIhIaB2BvUS1GU4DD9RHpO1S+8 fewg== X-Gm-Message-State: AOAM533hRkSCfBJ4fW9tgGzpjRegB88/nQ8gg/Ggq95CUOiIwHf1444u rBe+16ufqafwiCrnLU0sjbx37X+FINK2kJHhyj8lOUsGG64i9xEQ X-Google-Smtp-Source: ABdhPJy8Usp60sQrJ7LNwy/Prw8nEQpifbVxVD9q3rcZVP2tMZgA9B/WqXzZ99T1wZLd7Urx1lacOhOnxlmvUjYSRvo= X-Received: by 2002:a05:6402:2062:: with SMTP id bd2mr12925966edb.101.1598395960801; Tue, 25 Aug 2020 15:52:40 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: Diego Zamboni Date: Wed, 26 Aug 2020 00:52:30 +0200 Message-ID: Subject: Re: How to expand macros in tangled code blocks? To: Org-mode Content-Type: multipart/alternative; boundary="00000000000074506e05adbb8f31" Received-SPF: none client-ip=2a00:1450:4864:20::533; envelope-from=diego@zzamboni.org; helo=mail-ed1-x533.google.com X-detected-operating-system: by eggs.gnu.org: No matching host in p0f cache. That's all we know. X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, HTML_MESSAGE=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_NONE=0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: emacs-orgmode@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+larch=yhetil.org@gnu.org Sender: "Emacs-orgmode" X-Scanner: scn0 Authentication-Results: aspmx1.migadu.com; dkim=pass header.d=zzamboni-org.20150623.gappssmtp.com header.s=20150623 header.b=aWkqo+P3; dmarc=none; spf=pass (aspmx1.migadu.com: domain of emacs-orgmode-bounces@gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=emacs-orgmode-bounces@gnu.org X-Spam-Score: -0.21 X-TUID: ye9smm4XGuVo --00000000000074506e05adbb8f31 Content-Type: text/plain; charset="UTF-8" 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 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 > > --00000000000074506e05adbb8f31 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
To foll= ow up on my own question, I figured out a solution. I added three hooks wit= h the following local variables:

# Local Variab= les:
# eval: (add-hook 'org-babel-pre-tangle-hook (lambda () = (setq zz/saved-macro-templates org-macro-templates)) :append :local)
<= div># eval: (add-hook 'org-babel-post-tangle-hook (lambda () (makunboun= d 'zz/saved-macro-templates)) :append :local)
# eval: (add-ho= ok 'org-babel-tangle-body-hook (lambda () (when (boundp 'zz/saved-m= acro-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 start= ing the tangle, which gets unbound by the second hook, and the third one us= es it on each code block's body to expand the macros.

This feels a bit clunky, because of the need to define multiple hoo= ks, but also because the third hook (and the temporary variable) cannot be = made buffer-local, because the operation happens across different buffers (= each =3Dorg-babel-tangle-body-hook=3D runs in a new temporary buffer with t= hat block's body in it). So the third hook will remain in effect for an= y other org buffers I edit in the same session. Since the first two hooks a= re 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.

Cheer= s,
--Diego


On Tue, Aug 25, = 2020 at 9:59 AM Diego Zamboni <die= go@zzamboni.org> wrote:
Hi everyone,

Is it possible to expand Org macros in tangled code blocks? I found t= his old message: https://lists.gnu.org/archive/ht= ml/emacs-orgmode/2011-03/msg00857.html, but the mentioned org-export-pr= eprocess-apply-macros doesn't seem to exist anymore.

=
Thanks for any tips,
--Diego

--00000000000074506e05adbb8f31--