Преглед на файлове

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.
Sébastien Miquel преди 2 години
родител
ревизия
72f66ca0b9
променени са 5 файла, в които са добавени 31 реда и са изтрити 8 реда
  1. 4 3
      doc/org-manual.org
  2. 5 0
      etc/ORG-NEWS
  3. 1 1
      lisp/ob-core.el
  4. 6 4
      lisp/ob-ref.el
  5. 15 0
      testing/lisp/test-ob.el

+ 4 - 3
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
 

+ 5 - 0
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

+ 1 - 1
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))

+ 6 - 4
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

+ 15 - 0
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 "