Getting graphical feedback as tooltips in Emacs

| categories: emacs | tags:

In a continued exploration of Emacs as a user interface today we consider how to look at alternative representations of text. The long term idea is you might have a link to a datafile, say a Raman spectrum. You could click on the link to open it, perhaps even in analysis software. But, it might be nice to have a thumbnail type image that shows the data in graphical form. That might be sufficient for some purposes to identify which file to open.

You need to see the video to see the tooltips actually working in Emacs: https://www.youtube.com/watch?v=uX_hAPb9NOc

To illustrate the idea here we will have E1macs display an image when you mouse over some words that represent fruit, specifically grapes, kiwi and strawberry. We have in this directory images of those fruit:

ls
grapes.png
image-tooltips.org
kiwi.png
strawberry.png

We will use font-lock to add a tooltip to those words that displays the image in the minibuffer. I thought the image would show in the tooltip here, but for some reason it doesn't. Maybe that is ok, since it doesn't clutter the text with big images. Font lock also makes the words stand out a bit so you know there is something there. Here is our tooltip code, and the font-lock-add-keywords that activates it.

(defun image-tooltip (window object position)
  (save-excursion
    (goto-char position)
    (let* ((img-file (format "%s.png" (thing-at-point 'word)))
           (s (propertize "Look in the minbuffer"
                          'display (create-image (expand-file-name img-file)))))
      (message "%s" s))))

(font-lock-add-keywords
 nil
 '(("\\<kiwi\\>\\|\\<grapes\\>\\|strawberry" 0 '(face font-lock-keyword-face
                                                      help-echo image-tooltip))))

Some examples of fruit are the kiwi, the strawberry and grapes. That is a little example to illustrate the concept. Now, imagine something more sophisticated, e.g. a link to a molecular simulation that generates a thumbnail of the atomic geometry, and a summary of the energy. Or a Raman spectrum that shows a thumbnail of the spectrum.

Copyright (C) 2016 by John Kitchin. See the License for information about copying.

org-mode source

Org-mode version = 8.2.10

Discuss on Twitter