Let's say I want to write a mode which updates the display (e.g. adds some annotation) when the user edits the buffer or just moves around.

What is the recommended way to implement this?

Using post-command-hook with sit-for, so my function does its work when the user becomes idle after a command:

(defun my-post-command ()
  (when (sit-for 0.3)

Or an idle timer:

(run-with-idle-timer 0.3 t 'my-post-command)

The drawback of post-command-hook is that it holds up the hook with the delay if some other hook function comes after it.

The drawback of idle timer is that it's global, so there can be no buffer local timer, so if one implements some minor mode then the timer has to be deactivated/activated when the user switches buffers.

Is there a better/recommended method for this?


Do you not want the action taken, if the user switches buffer? If so, just use this in the timer:

(when (derived-mode-p 'THE-MODE-WHERE-IT-SHOULD-BE-DONE)
  <your code to do it>)


(when (eq (current-buffer) <the buffer where it should be done>)
  <your code to do it>)

or something else similar (e.g. conditional on a given file or whatever).

If you expect the action to be taken when the user comes back to the buffer or whatever, then you'll need some more elaborate handling.

Are you sure you need/want to do this kind of thing? E.g., what's the real problem you're trying to solve?

  • "It's also the case that as soon as the user does anything the sit-for is quit and the following code invoked immediately. " sit-for returns t if it waited the full time otherwise nil, so using it like (when (sit-for 0.3) ...) the body is only executed if the delay elapsed otherwise it's skipped. That's why it can be used in post-command-hook: when the user keeps typing, moving, the delayed code is not executed. it only executes if the user stops for a while. – Tom Jan 4 at 22:10
  • Oh, right. Thanks for the correction. I've removed that part of my answer. – Drew Jan 5 at 2:27

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.