all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Nik Clayton <nik@ngo.org.uk>
To: emacs-orgmode <emacs-orgmode@gnu.org>
Subject: Org-Babel: Alternate caching behaviour when file does not exist
Date: Wed, 23 Jan 2019 21:45:13 +0100	[thread overview]
Message-ID: <CAKJTzL6Bq3jXfVy+hVdUkPafXjmEdk=2_6hsr8cduhBkgQwMBw@mail.gmail.com> (raw)

[-- 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 --]

                 reply	other threads:[~2019-01-23 20:49 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAKJTzL6Bq3jXfVy+hVdUkPafXjmEdk=2_6hsr8cduhBkgQwMBw@mail.gmail.com' \
    --to=nik@ngo.org.uk \
    --cc=emacs-orgmode@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

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

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.