Subject: 27.1; Make remove-hook (interactive

From: Thibault Polge <[email-masked]>
Date: Wed, 23 Dec 2020 14:22:27 +0100

This is a follow-up to a previous discussion on emacs-devel[1].

The attached patch makes `remove-hook` interactive.  A common use case
for remove-hook is to fix your own mistakes when programming Emacs:
renaming a hook function for something more expressive, moving a
function to a different hook or from global to local, and so on.  For
these cases, it seems more natural to use the function interactively
than to have to write throwaway lisp one-liners.

A limitation of this approach is that since completion requires a text
representation of the function to remove, if two hooks have the same
representation under `princ` it will be impossible to distinguish
between them.  In this case, which is probably *extremely* rare (and
only concerns anonymous functions), only the first one will be
removed.

The copyright assignment paperwork was completed on Dec, 11.

[1] See: https://lists.gnu.org/archive/html/emacs-devel/2020-11/msg00860.html

>From 8175c3f1e92a53a6b9bb9e78cfb7d2cb481bf583 Mon Sep 17 00:00:00 2001
From: Thibault Polge <[email-masked]>
Date: Wed, 23 Dec 2020 14:19:15 +0100
Subject: [PATCH] Make `remove-hook` interactive

* lisp/subr.el: modify remove-hook to make it interactive.
---
 lisp/subr.el | 25 ++++++++++++++++++++++++-
 1 file changed, 24 insertions(+), 1 deletion(-)

diff --git a/lisp/subr.el b/lisp/subr.el
index 1fb0f9ab7e..2c8bc6a191 100644
--- a/lisp/subr.el
+++ b/lisp/subr.el
@@ -1742,7 +1742,30 @@ FUNCTION isn't the value of HOOK, or, if FUNCTION doesn't appear in the
 list of hooks to run in HOOK, then nothing is done.  See `add-hook'.
 
 The optional third argument, LOCAL, if non-nil, says to modify
-the hook's buffer-local value rather than its default value."
+the hook's buffer-local value rather than its default value.
+
+Interactively, prompt for the various arguments (skipping local
+unless HOOK has both local and global functions).  If multiple
+functions have the same representation under `princ', the first
+one will be removed."
+  (interactive
+   (let* ((hook (intern (completing-read "Hook variable: " obarray #'boundp t)))
+          (local
+           (and
+            (local-variable-p hook)
+            (symbol-value hook)
+            (or (not (default-value hook)) ; No need to prompt if there's nothing global
+                (y-or-n-p (format "%s has a buffer-local binding, use that? " hook)))))
+          (fn-alist (mapcar
+                     (lambda (x) (cons (with-output-to-string (prin1 x)) x))
+                     (if local (symbol-value hook) (default-value hook))))
+          (function (alist-get (completing-read
+                                (format "%s hook to remove:"
+                                        (if local "Buffer-local" "Global"))
+                                fn-alist
+                                nil t)
+                               fn-alist nil nil 'string=)))
+     (list hook function local)))
   (or (boundp hook) (set hook nil))
   (or (default-boundp hook) (set-default hook nil))
   ;; Do nothing if LOCAL is t but this hook has no local binding.
-- 
2.29.2

Best regards,
Thibault