Ignacio Casso <[email protected]> writes:

> Here it is, let me know what you think.

Sorry, I just realized I had an uncommited change. The correct patch is
the following:

>From b2f9c3aebef8768e9618e81848aaae3330cf3c7d Mon Sep 17 00:00:00 2001
From: Ignacio Casso <[email protected]>
Date: Mon, 9 Mar 2026 20:02:39 +0100
Subject: [PATCH] fix and extend org-babel-update-intermediate variable

* lisp/ob-ref.el (org-babel-update-intermediate): make variable custom
and add `cached` as new possible value to only update intermediate
blocks when they have the :cache yes header argument

* lisp/ob-ref.el (org-babel-ref-resolve): Fix usage of
`org-babel-update-intermediate` so that it only affects the :results
"none" header argument (before it was also affecting other header
arguments like parameter values, which resulted in a bug)

* lisp/ob-core.el (org-babel-execute-src-block): Use the cache header
argument and `org-babel-update-intermediate` to decide whether
:results "none" header argument should be ignored. This allows results
of cached blocks to be actually cached also when the block is
evaluated indirectly as a dependency of another block.

See https://list.orgmode.org/orgmode/[email protected]
---
 lisp/ob-core.el |  4 +++-
 lisp/ob-ref.el  | 19 ++++++++++++++-----
 2 files changed, 17 insertions(+), 6 deletions(-)

diff --git a/lisp/ob-core.el b/lisp/ob-core.el
index a8ca1ccd0..0ec4e8381 100644
--- a/lisp/ob-core.el
+++ b/lisp/ob-core.el
@@ -953,7 +953,9 @@ guess will be made."
 		    (setq result (org-babel-ref-resolve post))
 		    (when file
 		      (setq result-params (remove "file" result-params))))))
-	      (unless (member "none" result-params)
+	      (unless (and (member "none" result-params)
+                           (not (and cache
+                                     (eq 'cached org-babel-update-intermediate))))
 	        (org-babel-insert-result
 	         result result-params info
                  ;; append/prepend cannot handle hash as we accumulate
diff --git a/lisp/ob-ref.el b/lisp/ob-ref.el
index 14c5ce4a9..15906f2bb 100644
--- a/lisp/ob-ref.el
+++ b/lisp/ob-ref.el
@@ -70,8 +70,16 @@
 (declare-function org-narrow-to-subtree "org" (&optional element))
 (declare-function org-fold-show-context "org-fold" (&optional key))
 
-(defvar org-babel-update-intermediate nil
-  "Update the in-buffer results of code blocks executed to resolve references.")
+(defcustom org-babel-update-intermediate nil
+  "Update the in-buffer results of code blocks executed to resolve references.
+
+If value is nil they will never be updated.  If value is non-nil they
+will always be updated.  A value of ‘cached’ means to only update them if
+the block has the cache header argument set to yes.  This is needed
+for the cache feature to work properly, as it relies on source block
+results being printed in the Org buffer."
+  :group 'org-babel
+  :type 'boolean)
 
 (defun org-babel-ref-parse (assignment)
   "Parse a variable ASSIGNMENT in a header argument.
@@ -183,9 +191,10 @@ Emacs Lisp representation of the value of the variable."
 				(throw :found
 				       (org-babel-execute-src-block
 					nil nil
-					(and
-					 (not org-babel-update-intermediate)
-					 params))))
+                                        (if (and org-babel-update-intermediate
+                                                 (not (eq 'cached org-babel-update-intermediate)))
+                                            args
+                                          params))))
 			       ((and (let v (org-babel-read-element e))
 				     (guard v))
 				(throw :found v))
-- 
2.43.0

Reply via email to