Display images in Emacs from Common Lisp

The slime repl is used to interact with a Common Lisp process, and while printing textual information is good enough most of the time, being able to display an image can be really useful.

I used to simply call an external process, the image viewer feh, but the following method is more interesting.

You first need to load the slime-media module which is provided with slime, for example by adding it to the list of modules to load with slime:

(slime-setup '(slime-repl

You then have to authorize Slime to evaluate arbitrary lisp forms coming from the Common Lisp process:

(setq slime-enable-evaluate-in-emacs t)

Let’s display an image:

It looks pretty good, but I really wanted to have the return value printed below the image, and not on its right.

The following function is a modification of slime-media-insert-image; an end-of-line character is inserted just after the image. This way, the result value is printed below the image. Furthermore, when more than one image is displayed during the same call, it won’t be crammed on the same line.

(defun nm-slime-media-insert-image (image string)
  (with-current-buffer (slime-output-buffer)
    (let ((marker (slime-output-target-marker :repl-result)))
      (goto-char marker)
      (slime-propertize-region `(face slime-repl-result-face
                                      rear-nonsticky (face))
        (insert-image image string)
        (insert ?\n))
      ;; Move the input-start marker after the REPL result.
      (set-marker marker (point)))