emacs-orgmode
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[O] Org-Babel: Alternate caching behaviour when file does not exist


From: Nik Clayton
Subject: [O] Org-Babel: Alternate caching behaviour when file does not exist
Date: Wed, 23 Jan 2019 21:45:13 +0100

I discovered that if you have ":cache yes" set on a SRC block, with the results being written to a file with ":results filename.png", Org Babel will not re-evaluate the block if the file doesn't exist and the cache checksum hasn't changed.

This caused me a problem, as the .org files that I'm sharing with colleagues in a VCS have the cache option enabled -- I'd assumed that the first time they tried to export the file the images would be generated and then not regenerated on subsequent exports. That turned out to be wrong, and I can see why Org Babel has decided to go that way.

In case anyone else stumbles on to this and finds this in the archives, here's some advice around the org-babel-current-result-hash that checks to see if the linked file exists, and ignores the cache if it doesn't.

Hope that's helpful to someone -- N


(require 'f)
(defun my/org-babel-current-result-hash ()
  "Alter org-babel caching behaviour.

If you have `:cache yes' set the cached result is used even if
the file doesn't exist.  Fix this by advising the function that
computes the hash of the current content, and check to see if
the file is present."
  (save-excursion
    ;; Find the results block, and go to the first non-whitespace
    ;; content on the following line.
    (goto-char (org-babel-where-is-src-block-result))
    (forward-line)
    (skip-chars-forward " ")
    ;; If the context is a link to a local file check to see if the
    ;; file exists. If it does proceed to org-babel-current-result-hash
    ;; as normal, if it doesn't return nil, and force the file to be
    ;; generated.
    (let ((ctx (org-element-context)))
      (if (and ctx (listp ctx) (eq 'link (car ctx)) (string= "file" (org-element-property :type ctx)))
      (f-exists? (f-join (f-dirname buffer-file-name) (org-element-property :path ctx)))
    t))))

(advice-add #'org-babel-current-result-hash
        :before-while
        #'my/org-babel-current-result-hash)


reply via email to

[Prev in Thread] Current Thread [Next in Thread]