From 6d069f9532f44ee9fbc1a0ebdaadcc2eb807f8ec Mon Sep 17 00:00:00 2001 From: Tom Gillespie 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