emacsbufferhookkilldired

How do I write a emacs hook to prompt before killing a dired buffer?


I have a buffer of a directory using the dired command. I have some marked files in that buffer and I want emacs to prompt me before I kill the buffer. I understand I have to write a hook but don't know Lisp nor the emacs functions to use. How do I write a hook or can someone provide a hook?

I have looked at other hooks but unable to figure out what the hook would be to make kill-current-buffer prompt me before killing the buffer.


Solution

  • With no prompting:

    (defun my-fun () (not (derived-mode-p 'dired-mode)))
    
    (add-hook 'kill-buffer-query-functions 'my-fun)
    

    With prompting:

    (defun my-fun ()
      (or (not (derived-mode-p 'dired-mode))
          (y-or-n-p (format "Kill Dired buffer `%s'" (current-buffer)))))
    
    (add-hook 'kill-buffer-query-functions 'my-fun)