Skip to content

Commit 72f66ca

Browse files
sebmiqyantar92
authored andcommitted
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.
1 parent 06373a6 commit 72f66ca

File tree

5 files changed

+31
-8
lines changed

5 files changed

+31
-8
lines changed

doc/org-manual.org

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17505,9 +17505,10 @@ a colon, for example: =:var table=other-file.org:example-table=.
1750517505
: 4
1750617506
#+end_example
1750717507

17508-
- literal example ::
17508+
- literal example, or code block contents ::
1750917509

17510-
A literal example block named with a =NAME= keyword.
17510+
A code block or literal example block named with a =NAME= keyword,
17511+
followed by brackets (optional for example blocks).
1751117512

1751217513
#+begin_example
1751317514
,#+NAME: literal-example
@@ -17517,7 +17518,7 @@ a colon, for example: =:var table=other-file.org:example-table=.
1751717518
,#+END_EXAMPLE
1751817519

1751917520
,#+NAME: read-literal-example
17520-
,#+BEGIN_SRC emacs-lisp :var x=literal-example
17521+
,#+BEGIN_SRC emacs-lisp :var x=literal-example[]
1752117522
(concatenate #'string x " for you.")
1752217523
,#+END_SRC
1752317524

etc/ORG-NEWS

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,11 @@ The =org-md-toplevel-hlevel= customization variable sets the heading
288288
level used for top level headings, much like how
289289
=org-html-toplevel-hlevel= sets the heading level used for top level
290290
headings in HTML export.
291+
*** Babel: new syntax to pass the contents of a src block as argument
292+
293+
Use the header argument =:var x=code-block[]= or
294+
: #+CALL: fn(x=code-block[])
295+
to pass the contents of a named code block as a string argument.
291296

292297
** New options
293298
*** A new custom setting =org-hide-drawer-startup= to control initial folding state of drawers

lisp/ob-core.el

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2156,7 +2156,7 @@ Return nil if ELEMENT cannot be read."
21562156
(or (org-babel--string-to-number v) v)))
21572157
(`table (org-babel-read-table))
21582158
(`plain-list (org-babel-read-list))
2159-
(`example-block
2159+
((or `example-block `src-block)
21602160
(let ((v (org-element-property :value element)))
21612161
(if (or org-src-preserve-indentation
21622162
(org-element-property :preserve-indent element))

lisp/ob-ref.el

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,12 +124,14 @@ Emacs Lisp representation of the value of the variable."
124124
(save-excursion
125125
(let ((case-fold-search t)
126126
args new-refere new-header-args new-referent split-file split-ref
127-
index)
127+
index contents)
128128
;; if ref is indexed grab the indices -- beware nested indices
129-
(when (and (string-match "\\[\\([^\\[]+\\)\\]$" ref)
129+
(when (and (string-match "\\[\\([^\\[]*\\)\\]$" ref)
130130
(let ((str (substring ref 0 (match-beginning 0))))
131131
(= (cl-count ?\( str) (cl-count ?\) str))))
132-
(setq index (match-string 1 ref))
132+
(if (> (length (match-string 1 ref)) 0)
133+
(setq index (match-string 1 ref))
134+
(setq contents t))
133135
(setq ref (substring ref 0 (match-beginning 0))))
134136
;; assign any arguments to pass to source block
135137
(when (string-match
@@ -171,7 +173,7 @@ Emacs Lisp representation of the value of the variable."
171173
(throw :found
172174
(org-babel-execute-src-block
173175
nil (org-babel-lob-get-info e) params)))
174-
(`src-block
176+
((and `src-block (guard (not contents)))
175177
(throw :found
176178
(org-babel-execute-src-block
177179
nil nil

testing/lisp/test-ob.el

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,21 @@ should still return the link."
178178
(point-at-bol)
179179
(point-at-eol))))))
180180

181+
(ert-deftest test-ob/block-content-resolution ()
182+
"Test block content resolution."
183+
(org-test-with-temp-text-in-file "
184+
185+
#+name: four
186+
#+begin_src emacs-lisp
187+
(list 1 2 3 4)
188+
#+end_src
189+
190+
#+begin_src emacs-lisp :var four=four[]
191+
(length (eval (car (read-from-string four))))
192+
#+end_src"
193+
(org-babel-next-src-block 2)
194+
(should (= 4 (org-babel-execute-src-block)))))
195+
181196
(ert-deftest test-ob/cons-cell-as-variable ()
182197
"Test that cons cell can be assigned as variable."
183198
(org-test-with-temp-text "

0 commit comments

Comments
 (0)