emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Tom Gillespie <tgbugs@gmail.com>
To: emacs-orgmode@gnu.org
Subject: [PATCH] lisp/ob-core.el: pass expanded body to org-confirm-babel-evaluate
Date: Mon, 13 Jul 2020 12:53:48 -0700	[thread overview]
Message-ID: <CA+G3_PNi3uMvBiWgBdKuC3C6VJt1T1j-RKH43LRqYbr+4NS8ZA@mail.gmail.com> (raw)

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

Hi all,
   This is a patch to improve the behavior of
org-babel-check-confirm-evaluate and the usefulness of
org-confirm-babel-evaluate when a function is provided. It is
currently made against master, but it might also be possible to make
it against maint. The rest is in the commit message. Best!
Tom

[-- Attachment #2: 0001-lisp-ob-core.el-pass-expanded-body-to-org-confirm-ba.patch --]
[-- Type: text/x-patch, Size: 3983 bytes --]

From 6d069f9532f44ee9fbc1a0ebdaadcc2eb807f8ec Mon Sep 17 00:00:00 2001
From: Tom Gillespie <tgbugs@gmail.com>
Date: Mon, 13 Jul 2020 12:04:18 -0700
Subject: [PATCH] lisp/ob-core.el: pass expanded body to
 org-confirm-babel-evaluate

* lisp/ob-core.el (org-babel-get-body-to-eval): New function extracted
from org-babel-execute-src-block so that it can be reused.
(org-babel-execute-src-block): Use org-babel-get-body-to-eval
(org-babel-check-confirm-evaluate): Use org-babel-get-body-to-eval to
pass the expanded body of the code that will actually be evaluated to
org-confirm-babel-evaluate if it is a function.

This commit changes the behavior of org-babel-check-confirm-evaluate
so that org-confirm-babel-evaluate receives the fully expanded code to
be evaluated. This is important because there is no easy way to expand
noweb references inside org-confirm-babel-evaluate without calling
org-babel-get-src-block-info a second time. It is also important
because the current behavior makes it possible for code lurking behind
noweb references to change without the user being able to detect those
changes if they trust org-confirm-babel-evaluate is receiving the
actual body of the code that will be evalulated. I was bitten by this
when trying to hash the body passed to org-confirm-babel-evaluate and
could not figure out why the hash was not changing when a nowebbed
block changed.

These changes were made in such a way as to minimize changes to the
existing functions, however they come at the cost of making two calls
to org-babel-get-body-to-eval since passing the expanded body through
to org-confirm-babel-evaluate would either require appending it to the
info list or changing the function signatures of
org-babel-confirm-evaluate and org-babel-check-confirm-evaluate which
is undesireable. Furthermore, to compute the expanded body only once
would require switching from using cond in org-babel-execute-src-block
to using a series of nested ifs. This change can be made, but starting
from the current implementation will provide a working reference for
comparison rather than trying to make all the changes at the same time.
---
 lisp/ob-core.el | 23 +++++++++++++----------
 1 file changed, 13 insertions(+), 10 deletions(-)

diff --git a/lisp/ob-core.el b/lisp/ob-core.el
index e798595bd..4f2413579 100644
--- a/lisp/ob-core.el
+++ b/lisp/ob-core.el
@@ -238,7 +238,7 @@ should be asked whether to allow evaluation."
 		    (if (functionp org-confirm-babel-evaluate)
 			(funcall org-confirm-babel-evaluate
 				 ;; Language, code block body.
-				 (nth 0 info) (nth 1 info))
+				 (nth 0 info) (org-babel-get-body-to-eval info))
 		      org-confirm-babel-evaluate))))
     (cond
      (noeval nil)
@@ -632,6 +632,17 @@ a list with the following pattern:
 	(setf (nth 2 info) (org-babel-generate-file-param name (nth 2 info)))
 	info))))
 
+(defun org-babel-get-body-to-eval (info)
+  "Expand noweb references in BODY and remove any coderef."
+  (let ((coderef (nth 6 info))
+	(expand
+	 (if (org-babel-noweb-p (nth 2 info) :eval)
+	     (org-babel-expand-noweb-references info)
+	   (nth 1 info))))
+    (if (not coderef) expand
+      (replace-regexp-in-string
+       (org-src-coderef-regexp coderef) "" expand nil nil 1))))
+
 ;;;###autoload
 (defun org-babel-execute-src-block (&optional arg info params)
   "Execute the current source code block.
@@ -679,15 +690,7 @@ block."
 		 (result-params (cdr (assq :result-params params)))
 		 ;; Expand noweb references in BODY and remove any
 		 ;; coderef.
-		 (body
-		  (let ((coderef (nth 6 info))
-			(expand
-			 (if (org-babel-noweb-p params :eval)
-			     (org-babel-expand-noweb-references info)
-			   (nth 1 info))))
-		    (if (not coderef) expand
-		      (replace-regexp-in-string
-		       (org-src-coderef-regexp coderef) "" expand nil nil 1))))
+		 (body (org-babel-get-body-to-eval info))
 		 (dir (cdr (assq :dir params)))
 		 (mkdirp (cdr (assq :mkdirp params)))
 		 (default-directory
-- 
2.26.2


             reply	other threads:[~2020-07-13 19:54 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-07-13 19:53 Tom Gillespie [this message]
2020-07-19 21:13 ` Kyle Meyer
2020-07-20 20:27   ` Tom Gillespie
2020-07-22  4:19     ` Kyle Meyer
2020-08-02  6:03       ` Tom Gillespie
2020-08-03  3:05         ` Kyle Meyer
2020-09-05  3:54           ` Tom Gillespie
2020-09-06  3:45             ` Kyle Meyer
2020-09-06  9:39               ` Tom Gillespie

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=CA+G3_PNi3uMvBiWgBdKuC3C6VJt1T1j-RKH43LRqYbr+4NS8ZA@mail.gmail.com \
    --to=tgbugs@gmail.com \
    --cc=emacs-orgmode@gnu.org \
    --subject='Re: [PATCH] lisp/ob-core.el: pass expanded body to org-confirm-babel-evaluate' \
    /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

Code repositories for project(s) associated with this 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 NNTP newsgroup(s).