From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp2 ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms11 with LMTPS id IGTKONjDT1//WgAA0tVLHw (envelope-from ) for ; Wed, 02 Sep 2020 16:10:00 +0000 Received: from aspmx1.migadu.com ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp2 with LMTPS id OLeVNNjDT1/6dwAAB5/wlQ (envelope-from ) for ; Wed, 02 Sep 2020 16:10:00 +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 3C0FC9402A0 for ; Wed, 2 Sep 2020 16:10:00 +0000 (UTC) Received: from localhost ([::1]:60156 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kDVKh-00057L-18 for larch@yhetil.org; Wed, 02 Sep 2020 12:09:59 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:60124) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kDVKA-00055P-N0 for emacs-orgmode@gnu.org; Wed, 02 Sep 2020 12:09:26 -0400 Received: from mail-pf1-x430.google.com ([2607:f8b0:4864:20::430]:40901) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1kDVK8-0000YQ-W1 for emacs-orgmode@gnu.org; Wed, 02 Sep 2020 12:09:26 -0400 Received: by mail-pf1-x430.google.com with SMTP id c142so3089428pfb.7 for ; Wed, 02 Sep 2020 09:09:23 -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=GHBYwvJLZDweJfKlkMlJ09SV0AIQWJvFf1RR4eQRryQ=; b=I9wNG1Q0VAfAVrmdrT8IlDJ2CPapYzrNYvL7qH2kRewPrrty6bbqg6AgAgJnpfmOcz uIrhJI5zHDydl+YPp2PPEExOIVySW+zHqpT8ndzTngIt+naTiyz75agilIXWNxJguozk 4mmVQw+tmQg/5PiE5YVeOF+f6JhtYoiud+VDjSilM+MEDtniz0rybzErxoCxCGcdQgd7 rCJaOkag+6PHfaQB18bFxCwmtluFT3RcnWafkYPT4n4Vd7j2Gn+BTMJwhlKLUKhYHmic pEAAUcRo/9w9tcLUzio1SxNTiOzA2VAHe3etZu7YB7xbdCNBrKK+l7RDNA1cMGkzhTvU 2FKA== 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=GHBYwvJLZDweJfKlkMlJ09SV0AIQWJvFf1RR4eQRryQ=; b=pzg1m4EdoPOk9Xx85gnG3NFPLemZAx0GHpqZ75k+KwaqT9TVUKynHm4DNy7V+EHJas /8mhFYxoZSqI8hWuYlwJ8F3EYKtyZ8GgQuSqs5KiX3BDue2D1D+kUecqqjtB40hVbdi4 sETTy6peiyob+E7ewSo2OKk4jTjFAQ0KzWXfvPKR+4bQPYr3DK9lRoppHp7wHtS8Qh59 iPYuKZd0LGm/G69kCXwcf4CMZkRaMVyI65fm1/Ohe2gsacsFIYrqHiWaIzK8piAG72Nt 2o87JQ8AO0QKXQ4Mmo2jgbSwzpodM3l2We8TpgeFXncrpwz4obUACmpxZG0CfythxRFj DheQ== X-Gm-Message-State: AOAM530u8HvnIR8mtQHPLTW4d6gc2LZPEC1LiMcxhhi993hnVOa8KNIZ k9cQ6kG/lxddFRPCJ1Ok6oJbmy2mj5sGkA== X-Google-Smtp-Source: ABdhPJxaYXmxATXDCou+VCFENfavNsfQAMzXszVFz/oryoB9zRAroVwXBB57CI9z3+sCRoPNqXAOGg== X-Received: by 2002:a63:4746:: with SMTP id w6mr2493126pgk.412.1599062962119; Wed, 02 Sep 2020 09:09:22 -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 kf10sm4969350pjb.2.2020.09.02.09.09.20 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Sep 2020 09:09:20 -0700 (PDT) From: Matt Huszagh To: "emacs-orgmode@gnu.org" Cc: Subject: Re: babel default header args as functions In-Reply-To: <871rjqprdu.fsf@gmail.com> References: <87ftflikkc.fsf@gmail.com> <871rjqprdu.fsf@gmail.com> Date: Wed, 02 Sep 2020 09:09:16 -0700 Message-ID: <875z8wxis3.fsf@gmail.com> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Received-SPF: pass client-ip=2607:f8b0:4864:20::430; envelope-from=huszaghmatt@gmail.com; helo=mail-pf1-x430.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=I9wNG1Q0; 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: 2KkRgufeHJti --=-=-= Content-Type: text/plain Matt Huszagh writes: > I've generated a patch for this. Please let me know your thoughts. I > believe this adds valuable flexibility to default header > arguments. I've added an additional fix that makes this work during export too. --=-=-= Content-Type: text/x-patch Content-Disposition: inline; filename=0001-ob-core.el-Add-ability-to-use-closures-as-default-he.patch >From aec4e905d5d72f9a124adfde877835a783bd637b 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. * lisp/ob-exp.el (org-babel-exp-src-block): Must use new eval-default-headers when exporting as well. The closures are evaluated at runtime. --- lisp/ob-core.el | 32 ++++++++++++++++++++++++++++++-- lisp/ob-exp.el | 2 +- 2 files changed, 31 insertions(+), 3 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 diff --git a/lisp/ob-exp.el b/lisp/ob-exp.el index 34caf9546..13277f64f 100644 --- a/lisp/ob-exp.el +++ b/lisp/ob-exp.el @@ -103,7 +103,7 @@ Assume point is at block opening line." (apply #'org-babel-merge-params org-babel-default-header-args (and (boundp lang-headers) - (symbol-value lang-headers)) + (eval-default-headers lang-headers)) (append (org-babel-params-from-properties lang) (list raw-params))))))) (setf hash (org-babel-sha1-hash info :export))) -- 2.28.0 --=-=-=--