Subject: Re: bug#5293: 23.1; unload-feature on buffer-local hooks

From: Lars Ingebrigtsen <[email-masked]>
Date: Wed, 30 Sep 2020 20:44:04 +0200

Štěpán Němec <[email-masked]> writes:

> So I guess my defeatism will not prevail after all. Updated [2/3] attached.

The patch no longer applied to the trunk, and it referred to a variable
called `removables' that I couldn't find.  So I've respun it (included
below), but not tested.

Any comments?

diff --git a/etc/NEWS b/etc/NEWS
index b4f29ab783..993cb3ca85 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -215,6 +215,9 @@ preserving markers, properties and overlays.  The new variable
 number of seconds that 'revert-buffer-with-fine-grain' should spend
 trying to be non-destructive.
 
+---
+** 'unload-feature' now also tries to undo additions to buffer-local hooks.
+
 
 * Changes in Specialized Modes and Packages in Emacs 28.1
 
diff --git a/lisp/loadhist.el b/lisp/loadhist.el
index a1ff2f6270..6f2e313f71 100644
--- a/lisp/loadhist.el
+++ b/lisp/loadhist.el
@@ -297,6 +297,16 @@ unload-feature
 	       (when (and (eq (car-safe y) 'defun)
 			  (not (get (cdr y) 'autoload)))
 		 (remove-hook x (cdr y)))))))
+        ;; Handle buffer-local hooks.
+        (save-current-buffer
+          (dolist (buffer (buffer-list))
+            (pcase-dolist (`(,sym . ,val) (buffer-local-variables buffer))
+              (when (or (and (consp val)
+                             (string-match "-hooks?\\'" (symbol-name sym)))
+                        (memq sym unload-feature-special-hooks))
+                (set-buffer buffer)
+                (dolist (func unload-function-defs-list)
+                  (remove-hook sym func t))))))
 	;; Remove any feature-symbols from auto-mode-alist as well.
 	(dolist (y unload-function-defs-list)
 	  (when (and (eq (car-safe y) 'defun)

-- 
(domestic pets only, the antidote for overdose, milk.)
   bloggy blog: http://lars.ingebrigtsen.no