From 89bb446a0b68d1ca01f7e9a46335d6d6acb76dd0 Mon Sep 17 00:00:00 2001 From: Eric Schulte Date: Thu, 28 Jun 2012 11:49:47 -0600 Subject: [PATCH] caching in #+call: lines * lisp/ob.el (org-babel-set-current-result-hash): Change the hash of the results for the current code block. (org-babel-current-result-hash): Fix documentation. * lisp/ob-lob.el (org-babel-lob-execute): Don't re-execute the called function if the current call line hash matches that in its results. --- lisp/ob-lob.el | 31 +++++++++++++++++++++---------- lisp/ob.el | 10 +++++++++- 2 files changed, 30 insertions(+), 11 deletions(-) diff --git a/lisp/ob-lob.el b/lisp/ob-lob.el index 7828f1d51..66b100c8a 100644 --- a/lisp/ob-lob.el +++ b/lisp/ob-lob.el @@ -119,16 +119,27 @@ if so then run the appropriate source block from the Library." (defun org-babel-lob-execute (info) "Execute the lob call specified by INFO." - (let ((params (org-babel-process-params - (org-babel-merge-params - org-babel-default-header-args - (org-babel-params-from-properties) - (org-babel-parse-header-arguments - (org-babel-clean-text-properties - (concat ":var results=" - (mapconcat #'identity (butlast info) " ")))))))) - (org-babel-execute-src-block - nil (list "emacs-lisp" "results" params nil nil (nth 2 info))))) + (flet ((mkinfo (p) (list "emacs-lisp" "results" p nil nil (nth 2 info)))) + (let* ((pre-params (org-babel-merge-params + org-babel-default-header-args + (org-babel-params-from-properties) + (org-babel-parse-header-arguments + (org-babel-clean-text-properties + (concat ":var results=" + (mapconcat #'identity (butlast info) " ")))))) + (pre-info (mkinfo pre-params)) + (cache? (and (cdr (assoc :cache pre-params)) + (string= "yes" (cdr (assoc :cache pre-params))))) + (new-hash (when cache? (org-babel-sha1-hash pre-info))) + (old-hash (when cache? (org-babel-current-result-hash)))) + (if (and cache? (equal new-hash old-hash)) + (save-excursion (goto-char (org-babel-where-is-src-block-result)) + (forward-line 1) + (message "%S" (org-babel-read-result))) + (prog1 (org-babel-execute-src-block + nil (mkinfo (org-babel-process-params pre-params))) + ;; update the hash + (org-babel-set-current-result-hash new-hash)))))) (provide 'ob-lob) diff --git a/lisp/ob.el b/lisp/ob.el index 509bd4198..1bd5eb4e6 100644 --- a/lisp/ob.el +++ b/lisp/ob.el @@ -1048,10 +1048,18 @@ the current subtree." (sha1 it)))))) (defun org-babel-current-result-hash () - "Return the in-buffer hash associated with INFO." + "Return the current in-buffer hash." (org-babel-where-is-src-block-result) (org-babel-clean-text-properties (match-string 3))) +(defun org-babel-set-current-result-hash (hash) + "Set the current in-buffer hash to HASH." + (org-babel-where-is-src-block-result) + (save-excursion (goto-char (match-beginning 3)) + ;; (mapc #'delete-overlay (overlays-at (point))) + (replace-match hash nil nil nil 3) + (org-babel-hide-hash))) + (defun org-babel-hide-hash () "Hide the hash in the current results line. Only the initial `org-babel-hash-show' characters of the hash