Emacs, Overlays, and Point-Motion

One feature that’s missing from Emacs is the ability to run a hook when the point moves into an overlay. Now, there are several use cases for this; the most obvious one being displaying messages when you are looking at some specific part of the code. For example, flymake displaying the specific syntax error when the point is in the highlighted part. This could also be used for completions.

Now, there are ways to do similar things in emacs. You can assign functions to the ‘point-entered and ‘point-left text properties, and they are run when the point enters the function. However, in some cases it would be easier to use an overlay; for example, js2-mode uses an overlay for the error checking, but must set this property on the actual text. This is clearly suboptimal; b oth setting up and cleaning up overlays requires remembering about this text property. Help-at-pt.el can also help, but it can only display messages, and only display arbitrary actions.

This isn’t a problem that can be solved on the emacs-lisp side of things; a behavior like this needs to be added at the C level of emacs. I decided to delve in and investigate how you’d accomplish this; I ended up finding the correct places to add this functionality - a function called command_loop_1. There was additional helper code in other places, but the code that actually called the functions was here.

However, after discussing it on the emacs development mailing list, the interface for defining a hook got uglier to make it more general. Currently, the hook takes five arguments; the last point position, the current point position, the overlay/text property that contains the hook, the previous buffer, and the previous window. This is to support being able to support executing when the point leaves because it went to a different window, etc. The hook is called on every point motion; entering or leaving the overlay/text property, including by way of buffer or window switching, and motion inside the property.

Combined with the ugliness, there was no real use case I could think of other than the display, where there are the above-mentioned workarounds. The patch is currently languishing on the bug tracker; if you want it, please comment on the bug report at http://emacsbugs.donarmstrong.com/cgi-bin/bugreport.cgi?bug=5397. My patch is also located at point-motion.patch, if you wish to apply it and try it on your emacs system. To use it once you’ve patched your emacs, just add functions to the ‘point-motion text or overlay property.

Leave a Reply