I'm trying to use window-configuration-change-hook in a package to perform some action when a buffer becomes visible for the first time. It works, however when starting Emacs, desktop-mode calls switch-to-buffer as it is restoring buffers, which triggers window-configuration-change-hook. I don't want that to count because the user doesn't actually see the the buffers as they are restored.

Is there a good way to avoid false positives from desktop-mode? Or maybe a better way to detect when a buffer becomes visible?

My only current idea is to add a timer or something to ignore cases where the buffer is only "visible" for a very short period, but I don't really like that idea.

  • Just a quick-and-dirty thought, without thinking it through: Desktop mode can save variables. You could have it save window-configuration-change-hook and restore it. You might have to find out whether vars are restored before or after the buffer switches and adjust accordingly.
    – Drew
    Jun 22 '19 at 14:53
  • I suggest you file a bug report against desktop.el.
    – Stefan
    Jun 22 '19 at 15:20
  • How about locating the desktop.el functions responsible for the behavior that you wish to change and let-bind the window-configuration-change-hook to nil during those functions?
    – lawlist
    Jun 22 '19 at 19:34
  • I'm not sure there isn't some other user of window-configuration-change-hook that wants it to run when desktop mode restores buffers. I don't think desktop mode is doing anything "wrong". The way it works was just inconvenient for what I was trying to do.
    – Muir
    Jun 22 '19 at 20:04

I found that using a timer of 0 seconds before checking if the buffer is visible was enough to avoid the false positives:

(let ((buffer (current-buffer)))
  (run-with-timer 0 nil (lambda ()
                          (with-current-buffer buffer
                            (when (get-buffer-window nil t)
                              ;; do something now that buffer is visible

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.