unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#45393: 27.1; Make remove-hook (interactive
@ 2020-12-23 13:22 Thibault Polge
  2020-12-23 16:55 ` Drew Adams
  2020-12-25  5:45 ` Lars Ingebrigtsen
  0 siblings, 2 replies; 5+ messages in thread
From: Thibault Polge @ 2020-12-23 13:22 UTC (permalink / raw)
  To: 45393

[-- Attachment #1: Type: text/plain, Size: 947 bytes --]

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


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Make-remove-hook-interactive.patch --]
[-- Type: text/x-patch, Size: 2145 bytes --]

From 8175c3f1e92a53a6b9bb9e78cfb7d2cb481bf583 Mon Sep 17 00:00:00 2001
From: Thibault Polge <thibault@thb.lt>
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


[-- Attachment #3: Type: text/plain, Size: 23 bytes --]

Best regards,
Thibault

^ permalink raw reply related	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2021-01-05  8:35 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-12-23 13:22 bug#45393: 27.1; Make remove-hook (interactive Thibault Polge
2020-12-23 16:55 ` Drew Adams
2020-12-25  5:45 ` Lars Ingebrigtsen
2021-01-04 17:39   ` Juri Linkov
2021-01-05  8:35     ` Lars Ingebrigtsen

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).