2

I am trying to make graphical tooltips appear over some words in Emacs. Here is the code I have developed:

(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))))

The kiwi.png and other images are stored on disk, and they work fine (you can see it here https://www.youtube.com/watch?v=uX_hAPb9NOc) It works fine to put an image in the minibuffer, but the tooltip itself just says Look in minibuffer, and I thought it would instead show the image. Am I missing something in here?

1

You should return the propertized string in your image-tooltip code:

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

The code works fine in my Emacs(version 24.5, Arch Linux).

|improve this answer|||||
  • hm. I tried that with Emacs 24.1 and 25.1 on MacOSX, and it doesn't work , although in the minibuffer it does work with the image. Interestingly enough, it works on Windows with Emacs 24.5. I guess it is a Mac thing then. Thanks! – John Kitchin Mar 18 '16 at 22:06
  • This does look like a platform specific problem. In Linux, image tooltip is only shown when disabling the system tooltip by (setq x-gtk-use-system-tooltips nil), though I believe in OSX it should be something different. BTW, dired+ can show image tooltips when the mouse moves over an image name, which looks like something similar to what you want to achieve. – cutejumper Mar 19 '16 at 3:33
  • I was looking at dired+ when I wrote this example ;) unfortunately it also doesn't show tooltips in OSX. I guess this has to do with gtk support on osx. Thanks for the tip though. – John Kitchin Mar 19 '16 at 12:12
  • It turns out if I rebuild Emacs with gtk3 support, the image tooltips work! I had previously used a cocoa option to brew to install it. – John Kitchin Mar 19 '16 at 20:02
  • Great! Good to hear your problem is solved! – cutejumper Mar 20 '16 at 2:19

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.