I updated the patch:
>From 40bddcbc0bedd0d728ed066d94f61f7f9d7f27fb Mon Sep 17 00:00:00 2001 From: Ignacio Casso <[email protected]> Date: Mon, 9 Mar 2026 20:02:39 +0100 Subject: [PATCH] org-babel: 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 | 24 +++++++++++++++++------- 2 files changed, 20 insertions(+), 8 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..ffee10edf 100644 --- a/lisp/ob-ref.el +++ b/lisp/ob-ref.el @@ -70,8 +70,17 @@ (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 + :package-version '(Org . "9.7") + :type 'boolean) (defun org-babel-ref-parse (assignment) "Parse a variable ASSIGNMENT in a header argument. @@ -161,7 +170,10 @@ Emacs Lisp representation of the value of the variable." (setq ref split-ref))) (org-with-wide-buffer (goto-char (point-min)) - (let* ((params (append args '((:results . "none")))) + (let* ((params (if (and org-babel-update-intermediate + (not (eq 'cached org-babel-update-intermediate))) + args + (append args '((:results . "none"))))) (regexp (org-babel-named-data-regexp-for-name ref)) (result (catch :found @@ -183,9 +195,7 @@ 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)))) + params))) ((and (let v (org-babel-read-element e)) (guard v)) (throw :found v)) @@ -198,7 +208,7 @@ Emacs Lisp representation of the value of the variable." org-babel-library-of-babel)))) (when info (throw :found - (org-babel-execute-src-block nil info params)))) + (org-babel-execute-src-block nil info (append args '((:results . "none"))))))) (error "Reference `%s' not found in this buffer" ref)))) (cond ((and result (symbolp result)) (format "%S" result)) -- 2.43.0
> This is minor, but could you please follow > https://orgmode.org/worg/org-contribute.html#commit-messages format? I fixed the variable quotes and double spaces. I prefixed the first line of the commit message with "org-babel:", since the commit didn't touch a single file so I couldn't use "lisp/ob-core.el:" or "lisp/ob-ref.el:" > What if there is :results silent? org-babel-insert-result is the one that handles the insertion and takes the silent param into account. If you are talking about a block being both cached and silent, that doesn't make sense to begin with so I think we can ignore that case (the same way I'm ignoring the case of a cached block with results = none) > Please add :package-version and :type. Also, use `cached' to quote > symbol. :type was already there, I added :package-version with version 9.7 > What about babel calls? Sorry, I thought that the only way to evaluate blocks indirectly to resolve a reference was with org-babel-execute-src-block, but you are right that it can be done with babel calls too, it's right there in the code. I wasn't aware of that was even possible, but I tried to reproduce and indeed it is: #+name: babell-call-ref #+CALL: random(max=5)[:cache yes] #+begin_src emacs-lisp :var x=babell-call-ref :results replace x #+end_src This case was not handled by the original org-babel-update-intermediate variable to begin with, so I didn't realize I had to handle it too. I've updated the patch to take it into account. There is another case which is not handled, the library of Babel, but I guess the user never wants to update it and it never has cached blocks, so it should be fine.
