Skip to content

Instantly share code, notes, and snippets.

@adam-james-v
Created July 18, 2022 20:25
Show Gist options
  • Save adam-james-v/908e461ecccc11dc00882906cddf199d to your computer and use it in GitHub Desktop.
Save adam-james-v/908e461ecccc11dc00882906cddf199d to your computer and use it in GitHub Desktop.
Ways to Show Images in Emacs during Clojure Dev.

CIDER-show

Cider-show depends on CIDER’s ability to display inline images. You have to enable this feature:

M-x cider-repl-toggle-content-types

Run that command in a CIDER REPL buffer to enable the feature.

Or, permanently enable the setting by placing this somewhere in your emacs init config:

(setq cider-repl-content-types t)

Then, you can define a cider-show function that you can use during development. Here is an example of saving and viewing an SVG file, but you should be able to also create versions of this that can save other image formats such as PNG.

The ‘trick’ of the function is to rely on the fact that CIDER can display images if they are loaded with clojure.java.io/file, so all you have to do is save the file to some known location (a tmp directory, for example), and immediately load it.

I’ve also provided a show function that will open up your browser to view an html file. This can be useful to view SVG files in your default browser. You can probably extend this to work with other data by wrapping things in appropriate hiccup and compiling to a valid html page.

(defn cider-show
  "Show hiccup-style `svg-data` in a CIDER REPL. Creates `_tmp.svg` in the project's root folder."
  [svg-data]
  (let [fname "_tmp.svg"]
    ;; for other data, you'll want some different 'compilation' function here
    ;; perhaps the clojure2d library can be used for PNG generation, for example.
    (save-svg svg-data fname)
    (clojure.java.io/file fname)))

(defn show
  "Show hiccup-style `svg-data` in the browser. Creates `_tmp.svg.html` in the project's root folder."
  [svg-data]
  (let [fname "_tmp.svg.html"]
    (save-svg svg-data fname)
    (clojure.java.io/file fname)))

Inline Images in Org Files

Org Mode can actually render images inline. This can be done by writing a link in an org file to an image resource:

[[./tmp/image-to-display.png]] (without the opening/closing ‘~’).

Then, to actually cause the render, you can do

M-x org-display-inline-images

If you want this to happen for you automatically, you can do something like this in your emacs init:

(defun display-images-on-save-org-mode-file ()
  (when (and (string-match-p
              (regexp-quote ".org") (message "%s" (current-buffer)))
             (not (string-match-p
                   (regexp-quote "[") (message "%s" (current-buffer)))))
    (org-display-inline-images)))

(add-hook 'after-save-hook 'display-images-on-save-org-mode-file)
(add-hook 'org-babel-after-execute-hook  'display-images-on-save-org-mode-file)

So, any time you save an org file, the Images will load and display inline. As well, if you have a code block that results in an Image (or SVG, in my use case), you can have that display automatically as well. This way, you can write a code block, immediately evaluate it, and see the result inline (assuming your org setup displays results). This can give you a quick feedback loop where you edit, save, view all in the same .org file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment