I want to do some custom stuff before a window is deleted?

window-configuration-change-hook seems to be invoked after a window is deleted.

  • 1
    One idea would be to run a pre-command-hook and/or a post-command-hook with a test for a list of functions that would trigger additional custom stuff: (when (memq this-command '(.....)) custom-stuff). If the condition is not met, there is virtually zero overhead. And, these hooks can be buffer local if so desired. The test can be more complicated of so desired, e.g., a particular major-mode, current window of the previous command is the same window as the new command, etc. I cannot recommend advising delete-window or delete-other-windows as they are too fundamental to mess. – lawlist Jan 3 '18 at 19:18

Inspection of delete-window in GNU Emacs (i686-pc-linux-gnu, GTK+ Version 3.10.8) of 2016-04-25 shows that there is no such hook.

I don't right follow the argument of lawlist in his comment that delete-window is too fundamental to be advised.

For an example the following advice would provide a hook for functions with a WINDOW argument that is run before window WINDOW is deleted.

(defvar window-before-delete-functions nil
  "Functions to be run with argument WINDOW before \(delete-window WINDOW).")

(defun delete-window-ad (window)
  "Add `window-before-delete-functions' as before-advice of \(delete-window WINDOW)."
  (run-hook-with-args 'window-before-delete-functions (or window (selected-window))))

(advice-add #'delete-window :before #'delete-window-ad)

Just remove the hook functions in window-before-delete-functions if something goes wrong with them.

| improve this answer | |

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.