4

I'd like to add some navigation context to the modeline. I can figure out where I am just fine and give the name of my function, but I don't know how to run this every time point moves.

post-command-hook is the only thing I've found, but it sounds like it's overkill. Is there anything more appropriate?

  • 5
    Why is 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 a save-excursion, but still … (I don't know if code can decide whether save-excursion is in effect or not.) – Harald Hanche-Olsen Feb 25 '16 at 20:12
  • @Harald I suppose it might not be. The documentation isn't very clear on when this hook is run (i.e. what constitutes a command). For the record, I'm 100% willing to accept 'that is in fact the right thing to use' as an answer. – Sean Allred Feb 25 '16 at 20:42
  • M-x elisp-index-search RET command. A command is a function with an interactive form, or a keyboard macro (string or vector format). – phils Feb 25 '16 at 21:30
6

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

  • 7
    Don't explicitly make hook variables buffer-local. Instead use the LOCAL argument when calling add-hook. – phils Feb 25 '16 at 21:25
  • @phils Excellent point. I'll update the answer. – Harald Hanche-Olsen Feb 26 '16 at 9:16

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.