emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* Org-Babel: Alternate caching behaviour when file does not exist
@ 2019-01-23 20:45 Nik Clayton
  0 siblings, 0 replies; only message in thread
From: Nik Clayton @ 2019-01-23 20:45 UTC (permalink / raw)
  To: emacs-orgmode

[-- Attachment #1: Type: text/plain, Size: 1949 bytes --]

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)

[-- Attachment #2: Type: text/html, Size: 2374 bytes --]

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2019-01-23 20:49 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2019-01-23 20:45 Org-Babel: Alternate caching behaviour when file does not exist Nik Clayton

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).