I've tried to link an image in org-mode ([[image.jpg]] and [[file:image.jpg]]), then C-xC-cC-v to toggle displaying it. That works fine for normal images.

Then I encrypt the same image with epa-dired-do-encrypt, the file is encrypted fine and re-toggling doesn't display it.

Even though it's rather expected, is there any hook or other way to intercept all of the images and if there is an encrypted one (assumed from the epa-file or .gpg extensions) attempt to decrypt it?


What the keyboard binding triggers is (org-toggle-inline-images), that continues to (org-display-inline-images) which contains:

(when (and path (string-match-p file-extension-re path)) ...)

The file-extension-re is created by (file-extension-re (image-file-name-regexp)) that's a part of emacs/image-file.el.

Fortunately that's always called and isn't cached, which is good, because then I can utilize org-mode-hook to call my stuff and patch it in a sane way that doesn't corrupt everything around. However that's only the first part of the story and quite an easy one.

The problematic part is that the (create-image) function is expecting a path or data, so, another patch. Here is helpful the (advice-add) which has quite a clever way of injecting a custom function before/after/around/etc states, which allowed me to capture the path prior to calling (create-image), thus decrypt into some temp folder first and return back the temporary path.

Everything loads, we're done, right?


The decrypted files are still present on the system, thus allowing anyone who can look into temp to read them (well, "current user", but that's not enough for a restriction), so removal is in order. I tried to remove it right after decrypting and noticed the return value of (create-image) is used in org-mode and due to Lisp being Lisp I couldn't just store it in a temp variable, remove it and then return from the temp variable unless I wanted to start pulling in random packages or misuse throw as return. So I assembled a set of other hooks where the behavior would be kind of moving away from the editor and in there I injected a function removing the files.

Obviously it's not error proof and a malicious program could just rename files to something different, but until I figure out how to make in less silly, it's the way it works.

Quite a funny experience to code it over night. You can find the source here as well as the instructions how to install.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.