emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH] Make remove-hook interactive


From: Thibault Polge
Subject: [PATCH] Make remove-hook interactive
Date: Wed, 18 Nov 2020 22:09:49 +0100

This 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.
---
 lisp/subr.el | 25 ++++++++++++++++++++++++-
 1 file changed, 24 insertions(+), 1 deletion(-)

diff --git a/lisp/subr.el b/lisp/subr.el
index 6e9f66fe97b..ac59ae15465 100644
--- a/lisp/subr.el
+++ b/lisp/subr.el
@@ -1742,7 +1742,30 @@ remove-hook
 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.0



reply via email to

[Prev in Thread] Current Thread [Next in Thread]