0

I'd like to get a tooltip that says "meow", whenever my mouse is on the word "Cat" in Emacs. I tried this snippet I put together, but didn't work. What's wrong with it?

(defun my-hi-lock ()
(interactive)
(highlight-regexp "Cat"  '(help-echo "meow!"))
)

I'd like to put the associated tooltip message inside the highlight-regexp statement rather than define a font face at a different place because I won't reuse each tooltip message that I'm going to assign to different keywords.

3
  • You don't need highlight-regexp for this (and you don't need font-lock either). Just put text-property help-echo on the buffer text for which you want the mouseover tooltip.
    – Drew
    Sep 15 '16 at 13:49
  • @Drew, is that permanent? How do you do that? Does it write something in your init file?
    – stacko
    Sep 15 '16 at 20:07
  • The property is not persisted in the file, no. You just define a function that you invoke in whatever buffer (e.g. on a mode hook, for buffers of a given mode), and which searches for a pattern and applies the property. Or have the searching be done automatically by font-lock-mode, as @Lindydancer suggests. My point was only that it is text-property help-echo (not font-lock, for example) that is responsible for the tooltip. See functions put-text-property, add-text-properties, and set-text-properties.
    – Drew
    Sep 15 '16 at 20:23
1

highlight-regexp seems to assume that the value is a symbol which is defined as a face, it doesn't work with lists. However, the function is just a wrapper around font-lock-add-keywords, fortunately, you can call this function directly, for example:

(font-lock-add-keywords
 nil
 '(("Cat" (0 '(face font-lock-warning-face help-echo "meow!")))))

This will set the face property to font-lock-warning-face (feel free to pick a better one, or use nil) and the help-echo property to "meow!". Note that the symbol face must be the first entry in the list.

Note: If you do this, you must also put help-echo into font-lock-extra-managed-props, see the documentation of the variable font-lock-keywords for details.

For a concrete example, see emacs-lisp-mode.

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.