branch: externals/org
commit 8c24e4bb5378e71141fdcc4278bc005ced00f126
Author: Ignacio Casso <[email protected]>
Commit: Ihor Radchenko <[email protected]>

    org-babel: Fix and extend `org-babel-update-intermediate' variable
    
    * lisp/ob-core.el (org-babel-update-intermediate): Move variable to
    ob-core.el, make it 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.
    
    Link: https://list.orgmode.org/orgmode/[email protected]
---
 etc/ORG-NEWS    |  8 ++++++++
 lisp/ob-core.el | 20 +++++++++++++++++++-
 lisp/ob-ref.el  | 14 ++++++--------
 3 files changed, 33 insertions(+), 9 deletions(-)

diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS
index a638a9ed6d..3b9155090e 100644
--- a/etc/ORG-NEWS
+++ b/etc/ORG-NEWS
@@ -107,6 +107,14 @@ can be also written as:
 ,#+LATEX_CLASS_OPTIONS: a4paper,12pt
 #+END_SRC
 
+*** Updated custom variable ~org-babel-update-intermediate~
+
+The variable is now a custom variable, and it can take a new value:
+~cached~. In that case, only the intermediate blocks with =:cache yes=
+header argument will be updated. This allows for the cache feature to
+still work when a block is evaluated indirectly to resolve a reference
+in another block.
+
 ** New functions and changes in function arguments
 
 # This also includes changes in function behavior from Elisp perspective.
diff --git a/lisp/ob-core.el b/lisp/ob-core.el
index a8ca1ccd08..ea3d416efb 100644
--- a/lisp/ob-core.el
+++ b/lisp/ob-core.el
@@ -184,6 +184,22 @@ This string must include a \"%s\" which will be replaced 
by the results."
   :package-version '(Org . "9.1")
   :safe #'booleanp)
 
+(defcustom org-babel-update-intermediate nil
+  "Whether to update in-buffer results of 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 when resolving references as it relies
+on source block results being printed in the Org buffer."
+  :group 'org-babel
+  :package-version '(Org . "10.0")
+  :type '(choice
+          (const :tag "Never update intermediate results" nil)
+          (const :tag "Always update intermediate results" t)
+          (const :tag "Update results only if they should be cached" cache))
+  :safe (lambda (x) (memq x '(nil t cache))))
+
 (defun org-babel-noweb-wrap (&optional regexp)
   "Return regexp matching a Noweb reference.
 
@@ -953,7 +969,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 14c5ce4a9c..8ce1332bad 100644
--- a/lisp/ob-ref.el
+++ b/lisp/ob-ref.el
@@ -70,9 +70,6 @@
 (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.")
-
 (defun org-babel-ref-parse (assignment)
   "Parse a variable ASSIGNMENT in a header argument.
 
@@ -161,7 +158,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 +183,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 +196,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))

Reply via email to