Skip to content

Show images in Racket's REPL

gcr requested to merge gcr:master into master

This patch adds support for displaying (convertible?) values as images right in the Geiser REPL.

This is still pretty rough, so feedback welcome!

Screenshot

This works for many types:

  • Bitmaps
(require racket/draw)
(make-object bitmap% "filename.png")
  • Picts
(require slideshow/pict)
(disk 100)
  • Plots
#lang racket
(require plot)
(plot (function sin 0 pi))
  • Any convertible? object

Implementation

From the racket side, a custom current-print handler determines whether evaluated values are convertible? and, if so, prints them like: #<Image: /var/tmp/filename.png>

From the Geiser side, a comint output hook:

  • Scans for lines containing that regexp
  • Parses the filename
  • Loads the given image into memory
  • Deletes the temporary image from disk
  • Displays the image in the REPL

For non-TTY emacs users, geiser remembers the previously seen image. You can invoke the system image viewer on a temporary image with M-x geiser-view-last-image.

Settings and commands

  • geiser-repl-inline-images: If t, displays inline images
  • geiser-system-image-viewer: Which program to invoke (default: "display", from the imagemagick package)
  • M-x geiser-view-last-image: Writes the last seen image's contents to a temporary file, runs geiser-system-image-viewer, and registers a sentinel that deletes the temp file when the user exits the viewer.

Patch shortcomings:

  • My elisp isn't great; beware of bugs!
  • There's no documentation
  • geiser-eval and friends don't display temporary images; this only affects the REPL
  • The following should work but doesn't because the "outermost" returned value is not convertible to a picture:
#lang racket
(require slideshow/pict)
(list (disk 10) (disk 20) (disk 30))
  • Right now, emacs keeps the last displayed image data in memory so it can create a new temporary file to invoke external viewers. This may cause problematic memory usage.
  • Untested on anything but Linux

Feedback is more than welcome.

I hereby assign copyright of this patch and its contents to the Geiser developers.

Merge request reports