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