From b1b783dc80821b07937ac4211ec28df8726fff1c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Miquel?= Date: Sat, 13 Aug 2022 20:49:27 +0200 Subject: [PATCH] New babel syntax to pass src block contents as argument * lisp/ob-ref.el (org-babel-ref-resolve): Add support for `named-block[]' syntax, resolving to the contents of a named-block. * lisp/ob-core.el (org-babel-read-element): Read a code block into its contents, like other blocks. * testing/listp/test-ob.el (test-ob/block-content-resolution): Test block content resolution. * doc/org-manual.org: Document syntax. * etc/ORG-NEWS: Document syntax. --- doc/org-manual.org | 7 ++++--- etc/ORG-NEWS | 5 +++++ lisp/ob-core.el | 2 +- lisp/ob-ref.el | 10 ++++++---- testing/lisp/test-ob.el | 15 +++++++++++++++ 5 files changed, 31 insertions(+), 8 deletions(-) diff --git a/doc/org-manual.org b/doc/org-manual.org index 57a57a6fe..794682b49 100644 --- a/doc/org-manual.org +++ b/doc/org-manual.org @@ -17505,9 +17505,10 @@ a colon, for example: =:var table=other-file.org:example-table=. : 4 #+end_example -- literal example :: +- literal example, or code block contents :: - A literal example block named with a =NAME= keyword. + A code block or literal example block named with a =NAME= keyword, + followed by brackets (optional for example blocks). #+begin_example ,#+NAME: literal-example @@ -17517,7 +17518,7 @@ a colon, for example: =:var table=other-file.org:example-table=. ,#+END_EXAMPLE ,#+NAME: read-literal-example - ,#+BEGIN_SRC emacs-lisp :var x=literal-example + ,#+BEGIN_SRC emacs-lisp :var x=literal-example[] (concatenate #'string x " for you.") ,#+END_SRC diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS index 7dae03dc6..d6d99a64b 100644 --- a/etc/ORG-NEWS +++ b/etc/ORG-NEWS @@ -288,6 +288,11 @@ The =org-md-toplevel-hlevel= customization variable sets the heading level used for top level headings, much like how =org-html-toplevel-hlevel= sets the heading level used for top level headings in HTML export. +*** Babel: new syntax to pass the contents of a src block as argument + +Use the header argument =:var x=code-block[]= or +: #+CALL: fn(x=code-block[]) +to pass the contents of a named code block as a string argument. ** New options *** A new custom setting =org-hide-drawer-startup= to control initial folding state of drawers diff --git a/lisp/ob-core.el b/lisp/ob-core.el index 68dd5557c..c52ef9ed6 100644 --- a/lisp/ob-core.el +++ b/lisp/ob-core.el @@ -2156,7 +2156,7 @@ Return nil if ELEMENT cannot be read." (or (org-babel--string-to-number v) v))) (`table (org-babel-read-table)) (`plain-list (org-babel-read-list)) - (`example-block + ((or `example-block `src-block) (let ((v (org-element-property :value element))) (if (or org-src-preserve-indentation (org-element-property :preserve-indent element)) diff --git a/lisp/ob-ref.el b/lisp/ob-ref.el index 87a7ccf63..ee2745e09 100644 --- a/lisp/ob-ref.el +++ b/lisp/ob-ref.el @@ -124,12 +124,14 @@ Emacs Lisp representation of the value of the variable." (save-excursion (let ((case-fold-search t) args new-refere new-header-args new-referent split-file split-ref - index) + index contents) ;; if ref is indexed grab the indices -- beware nested indices - (when (and (string-match "\\[\\([^\\[]+\\)\\]$" ref) + (when (and (string-match "\\[\\([^\\[]*\\)\\]$" ref) (let ((str (substring ref 0 (match-beginning 0)))) (= (cl-count ?\( str) (cl-count ?\) str)))) - (setq index (match-string 1 ref)) + (if (> (length (match-string 1 ref)) 0) + (setq index (match-string 1 ref)) + (setq contents t)) (setq ref (substring ref 0 (match-beginning 0)))) ;; assign any arguments to pass to source block (when (string-match @@ -171,7 +173,7 @@ Emacs Lisp representation of the value of the variable." (throw :found (org-babel-execute-src-block nil (org-babel-lob-get-info e) params))) - (`src-block + ((and `src-block (guard (not contents))) (throw :found (org-babel-execute-src-block nil nil diff --git a/testing/lisp/test-ob.el b/testing/lisp/test-ob.el index a62bd56bf..c944ccd39 100644 --- a/testing/lisp/test-ob.el +++ b/testing/lisp/test-ob.el @@ -178,6 +178,21 @@ should still return the link." (point-at-bol) (point-at-eol)))))) +(ert-deftest test-ob/block-content-resolution () + "Test block content resolution." + (org-test-with-temp-text-in-file " + +#+name: four +#+begin_src emacs-lisp + (list 1 2 3 4) +#+end_src + +#+begin_src emacs-lisp :var four=four[] + (length (eval (car (read-from-string four)))) +#+end_src" + (org-babel-next-src-block 2) + (should (= 4 (org-babel-execute-src-block))))) + (ert-deftest test-ob/cons-cell-as-variable () "Test that cons cell can be assigned as variable." (org-test-with-temp-text " -- 2.37.2