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 sBd+GVdKSV+XUAAA0tVLHw (envelope-from ) for ; Fri, 28 Aug 2020 18:17:59 +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 uBJmFVdKSV/ENAAA1q6Kng (envelope-from ) for ; Fri, 28 Aug 2020 18:17:59 +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 86456940AFB for ; Fri, 28 Aug 2020 18:17:58 +0000 (UTC) Received: from localhost ([::1]:56224 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kBiwm-0007um-PO for larch@yhetil.org; Fri, 28 Aug 2020 14:17:56 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:54382) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kBiw6-0007uP-9m for emacs-orgmode@gnu.org; Fri, 28 Aug 2020 14:17:14 -0400 Received: from mail-pj1-x1030.google.com ([2607:f8b0:4864:20::1030]:50259) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1kBiw0-00044n-S3 for emacs-orgmode@gnu.org; Fri, 28 Aug 2020 14:17:14 -0400 Received: by mail-pj1-x1030.google.com with SMTP id i13so110265pjv.0 for ; Fri, 28 Aug 2020 11:17:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:in-reply-to:references:date:message-id :mime-version; bh=DzYvwwTXubh1S8jo9YXB0nKVQC779MSS7ob9xrIuBjQ=; b=J/gkxIG3ACB/Af8hmuHp4mMgpupb/2iG7j+pI+yQ82TiZemNrmjNEdZERcSL0Y4QF6 uI8xUtR3mLNNOigtBu5Fm7QFrqyff9D+qUiq8YLBTWOXvvDQgAhSVpIRccFiUU4XEoE7 ihyazLGJfYLcN1X0mYO2Lt3HzQhZwEeSGM5oBDiUhVCdKdgt+pUP/DZXZMeS4uGLuS+i Zbza8QPOOUlmi1zmvKC/tk6xGYga5ZAXoGMR1yvKk7pcPtn/bFUeiVLl+r4l2NqA3r/X gTnU6qqOcw1VKB0AqXc4YvAijHnrLlRWAmzUJMBkFpUjiGv0LKM0k2rvhrQKRYbMhrD1 rlBg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:in-reply-to:references:date :message-id:mime-version; bh=DzYvwwTXubh1S8jo9YXB0nKVQC779MSS7ob9xrIuBjQ=; b=k4mVmBMyDy9M4AfcKKdPBLpVK/u2kuuPg8cErOJqIeU+PPZ/dpd+hkLlD2Sux3kAJV nCGFaDesiIaCGtRAbvr05hd/7EMh16NprFiC6a3d7w8H/CEQ28COstohy2RvfrQscvBd FgfW9nXKyU8bZCyq7YBYhcJmQXjcAP3o6ynWLN4a6XzTxAKZGZCa2SueIV8lXmXIXRos vpDrOPz62xj6zEWoDg9bhLjIbbr58XWbPNpbyH1LMpBUJULJPG/p3ZVTeei8s8UuT4U2 2HEMgROVzZPQf/OBDuTimo9VrtOR64xnhXSsWIDd3efuykUIoO6rPIdf1H8dTgzi1Hlp qxIQ== X-Gm-Message-State: AOAM530yFODR/pPA/92vUV0K5V1FskQcJ4/79qMbyt4l4NkRuocdCVPC 0lNTxCM9XqZoah7RoCW2lZiy1lFeR7I= X-Google-Smtp-Source: ABdhPJyKh8nkuHTbCGAilub6k/r3n8/FlOrd72JgodTi2/ApzoiQCOisFaHWjK4SAshOccjILDk/yg== X-Received: by 2002:a17:90a:4382:: with SMTP id r2mr243664pjg.144.1598638626045; Fri, 28 Aug 2020 11:17:06 -0700 (PDT) Received: from ryzen3950 (c-73-71-89-135.hsd1.ca.comcast.net. [73.71.89.135]) by smtp.gmail.com with ESMTPSA id a6sm139548pgt.70.2020.08.28.11.17.04 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 28 Aug 2020 11:17:05 -0700 (PDT) From: Matt Huszagh To: "emacs-orgmode@gnu.org" Cc: Subject: Re: babel default header args as functions In-Reply-To: <87ftflikkc.fsf@gmail.com> References: <87ftflikkc.fsf@gmail.com> Date: Fri, 28 Aug 2020 11:17:01 -0700 Message-ID: <871rjqprdu.fsf@gmail.com> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Received-SPF: pass client-ip=2607:f8b0:4864:20::1030; envelope-from=huszaghmatt@gmail.com; helo=mail-pj1-x1030.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: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-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=gmail.com header.s=20161025 header.b=J/gkxIG3; dmarc=pass (policy=none) header.from=gmail.com; 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: -1.71 X-TUID: ONcirWLe529s --=-=-= Content-Type: text/plain Matt Huszagh writes: > I've added the ability in my own configuration to use lambda functions > that evaluate to a string as babel default header arguments, instead of > just the plain strings currently allowed. Would anyone else be > interested in this feature? Shall I prepare a patch? > > There are a number of use cases for this, but to give you an idea, > here's one I'm using myself. > > (setq org-babel-default-header-args:latex > `((:file . (lambda () > (concat "img/" > (sha1 (org-element-property :value (org-element-at-point))) > (by-backend '((html . "-html") (t . "-org"))) > ".svg"))))) > > This computes a filename based on the hash of the block contents. I've generated a patch for this. Please let me know your thoughts. I believe this adds valuable flexibility to default header arguments. Thanks! Matt --=-=-= Content-Type: text/x-patch Content-Disposition: attachment; filename=0001-ob-core.el-Add-ability-to-use-closures-as-default-he.patch >From 3dfb1066b211fdcc5e3ea1da8d36aa115dde9f9b Mon Sep 17 00:00:00 2001 From: Matt Huszagh Date: Fri, 28 Aug 2020 11:05:59 -0700 Subject: [PATCH] ob-core.el: Add ability to use closures as default header arguments * lisp/ob-core.el (org-babel-default-header-args): Document ability to use functions. (eval-default-headers): New function to generate default header arguments, which adds the ability to evaluate function arguments at runtime. (org-babel-get-src-block-info): Use new header argument evaluate function when retreiving src block info. The closures are evaluated at runtime. --- lisp/ob-core.el | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/lisp/ob-core.el b/lisp/ob-core.el index 578622232..4a22f17e7 100644 --- a/lisp/ob-core.el +++ b/lisp/ob-core.el @@ -473,7 +473,23 @@ For the format of SAFE-LIST, see `org-babel-safe-header-args'." (defvar org-babel-default-header-args '((:session . "none") (:results . "replace") (:exports . "code") (:cache . "no") (:noweb . "no") (:hlines . "no") (:tangle . "no")) - "Default arguments to use when evaluating a source block.") + "Default arguments to use when evaluating a source block. + +This is a list in which each element is an alist. Each key +corresponds to a header argument, and each value to that header's +value. The value can either be a string or a closure that +evaluates to a string at runtime. For instance, imagine you'd +like to set the file name output of a latex source block to a +sha1 of its contents. We could achieve this with: + +(defun org-src-sha () + (let ((elem (org-element-at-point))) + (concat (sha1 (org-element-property :value elem)) \".svg\"))) + +(setq org-babel-default-header-args:latex + `((:results . \"file link replace\") + (:file . (lambda () (org-src-sha)))))") + (put 'org-babel-default-header-args 'safe-local-variable (org-babel-header-args-safe-fn org-babel-safe-header-args)) @@ -584,6 +600,18 @@ the outer-most code block.") (defvar *this*) +(defun eval-default-headers (headers) + "Compute default header list set with HEADERS. + + Evaluate all default header arguments set to functions prior to + returning the list of header arguments." + (let ((lst nil)) + (dolist (elem (eval headers t)) + (if (listp (cdr elem)) + (push `(,(car elem) . ,(funcall (cdr elem))) lst) + (push elem lst))) + lst)) + (defun org-babel-get-src-block-info (&optional light datum) "Extract information from a source block or inline source block. @@ -615,7 +643,7 @@ a list with the following pattern: (apply #'org-babel-merge-params (if inline org-babel-default-inline-header-args org-babel-default-header-args) - (and (boundp lang-headers) (eval lang-headers t)) + (and (boundp lang-headers) (eval-default-headers lang-headers)) (append ;; If DATUM is provided, make sure we get node ;; properties applicable to its location within -- 2.28.0 --=-=-=--