emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: "Sébastien Miquel" <sebastien.miquel@posteo.eu>
To: Ihor Radchenko <yantar92@gmail.com>
Cc: emacs-orgmode <emacs-orgmode@gnu.org>
Subject: Re: [PATCH] Make :var foo=name-of-src-block assign the source block code instead of currently assigned result of evaluation (was: [PATCH] Add :noweb-prefix and :noweb-trans babel header arguments)
Date: Mon, 29 Aug 2022 17:39:56 +0000	[thread overview]
Message-ID: <884a86ae-8637-a9c8-326d-a0725e5ac142@posteo.eu> (raw)
In-Reply-To: <87sfmsv3b7.fsf@localhost>

[-- Attachment #1: Type: text/plain, Size: 114 bytes --]

Hi Ihor,

I've implemented this proposal in the patch attached.

Does it look good to you ?

-- 
Sébastien Miquel

[-- Attachment #2: 0001-New-babel-syntax-to-pass-src-block-contents-as-argum.patch --]
[-- Type: text/x-patch, Size: 5096 bytes --]

From b1b783dc80821b07937ac4211ec28df8726fff1c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Miquel?= <sebastien.miquel@posteo.eu>
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


  reply	other threads:[~2022-08-29 17:45 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-01-15 10:42 [PATCH] Add :noweb-prefix and :noweb-trans babel header arguments Sébastien Miquel
2022-04-28 13:45 ` Ihor Radchenko
2022-04-29 14:27   ` Sébastien Miquel
2022-04-30  6:05     ` Ihor Radchenko
2022-04-30  7:35       ` Sébastien Miquel
2022-04-30  8:41         ` Ihor Radchenko
2022-05-30 19:51       ` Sébastien Miquel
2022-05-31  5:14         ` Ihor Radchenko
2022-05-31  6:48           ` Sébastien Miquel
2022-07-16  8:57             ` [FR] Make :var foo=name-of-src-block assign the source block code instead of currently assigned result of evaluation (was: [PATCH] Add :noweb-prefix and :noweb-trans babel header arguments) Ihor Radchenko
2022-07-16 10:17               ` Greg Minshall
2022-07-16 10:45                 ` Ihor Radchenko
2022-07-16 14:31                   ` Greg Minshall
2022-07-17 17:39                   ` Sébastien Miquel
2022-07-23  4:52                     ` Ihor Radchenko
2022-08-29 17:39                       ` Sébastien Miquel [this message]
2022-08-31  4:04                         ` [PATCH] " Ihor Radchenko
2022-07-21 11:13             ` [PATCH] org-babel: Do not echo output of resolved noweb references Ihor Radchenko
2022-07-21 13:41               ` Sébastien Miquel
2022-08-22 12:01               ` Ihor Radchenko

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=884a86ae-8637-a9c8-326d-a0725e5ac142@posteo.eu \
    --to=sebastien.miquel@posteo.eu \
    --cc=emacs-orgmode@gnu.org \
    --cc=yantar92@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).