Search code examples
emacselisp

When is buffer-quit-function executed?


I want to hook into change-buffer, so tried to use buffer-quit-function. I wrote the following code and evaluated (using C-x C-e):

(add-hook 'buffer-quit-function    (lambda () (message "Buffer quit!")))

After that, I tried to change buffer with using C-x o (other-window), C-x b (switch-to-buffer.) But I can not see string Buffer quit!.

Why is my buffer-quit-function not executed?


Solution

  • Two things wrong:

    1. buffer-quit-function is not a hook. It's a variable, so set it like

      (set (make-local-variable 'buffer-quit-function)
           (lambda () (message "Buffer quit!")))
      
    2. It is not called when changing buffers; it's used when the user invokes keyboard-escape-quit, which is normally bound to ESC ESC ESC.

    I don't think there's any mechanism provided to do what you're trying to do.