I think post-command-hook
is what you wish to use.
It is run every time a command has been run. And a command is basically any interactive action, such as clicking a mouse button, typing a letter (which runs self-insert-command
, or running a command using M-x
.
You may wish to make sure your hook is not run while you are typing in the minibuffer. I think wrapping the body in (unless (window-minibuffer-p) …)
will care of that for you.
And possibly, you want your hook only to be active when you are editing certain files. In which case, you might consider making post-command-hook
buffer local: Using
(add-hook 'post-command-hook #'your-hook nil :local)
will take care of that for you. (Edited to add this last point.)
post-command-hook
overkill? You may not want your hook to run every time point moves, if it is done by some elisp code. There is a lot of code that moves point in order to do stuff. Granted, it is typically done within asave-excursion
, but still … (I don't know if code can decide whethersave-excursion
is in effect or not.)M-x elisp-index-search RET command
. A command is a function with aninteractive
form, or a keyboard macro (string or vector format).