unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: Richard Stallman <rms@gnu.org>
Cc: emacs-devel@gnu.org
Subject: Re: remove-hook.
Date: Wed, 08 Oct 2003 00:52:06 -0400	[thread overview]
Message-ID: <E1A76Iw-0002zT-6l@fencepost.gnu.org> (raw)
In-Reply-To: <sgdsmm5xjqn.fsf@occarina.pna.cwi.nl> (message from Lute Kamstra on Tue, 07 Oct 2003 15:25:36 +0200)

    | The optional third argument, LOCAL, if non-nil, says to modify
    | the hook's buffer-local value rather than its default value.
    | This makes the hook buffer-local if needed.
    `----

    When does remove-hook need to make a hook buffer-local to remove a
    function from it?

I am not sure any more why it does that, or whether it is
really necessary, but that is indeed what the code does.
Perhaps the reason is to prevent it from removing a global
hook when lOCAL is non-nil.

It also deletes buffer-local values when there is no more
need for them.

Anyway, this seems like a cleaner definition overall.
But does it work right?



(defun remove-hook (hook function &optional local)
  "Remove from the value of HOOK the function FUNCTION.
HOOK should be a symbol, and FUNCTION may be any valid function.  If
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."
  (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.
  (unless (and local (not (local-variable-p hook)))
    ;; Detect the case where make-local-variable was used on a hook
    ;; and do what we used to do.
    (when (and (local-variable-p hook)
	       (not (and (consp (symbol-value hook))
			 (memq t (symbol-value hook)))))
      (setq local t))
    (let ((hook-value (if local (symbol-value hook) (default-value hook))))
      ;; Remove the function, for both the list and the non-list cases.
      (if (or (not (listp hook-value)) (eq (car hook-value) 'lambda))
	  (if (equal hook-value function) (setq hook-value nil))
	(setq hook-value (delete function (copy-sequence hook-value))))
      ;; If the function is on the global hook, we need to shadow it locally
      ;;(when (and local (member function (default-value hook))
      ;;	       (not (member (cons 'not function) hook-value)))
      ;;  (push (cons 'not function) hook-value))
      ;; Set the actual variable
      (if (not local)
	  (set-default hook hook-value)
	(if (equal hook-value '(t))
	    (kill-local-variable hook)
	  (set hook hook-value))))))

      parent reply	other threads:[~2003-10-08  4:52 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-10-07 13:25 remove-hook Lute Kamstra
2003-10-07 13:40 ` remove-hook Luc Teirlinck
2003-10-07 13:42 ` remove-hook Stefan Monnier
2003-10-07 14:00   ` remove-hook Lute Kamstra
2003-10-07 14:59     ` remove-hook Stefan Monnier
2003-10-07 15:13       ` remove-hook Lute Kamstra
2003-10-07 15:24         ` remove-hook Stefan Monnier
2003-10-07 22:04         ` remove-hook Luc Teirlinck
2003-10-08  4:52     ` remove-hook Richard Stallman
2003-10-10 14:25       ` remove-hook Lute Kamstra
2003-10-11 17:12         ` remove-hook Richard Stallman
2003-10-08  4:52 ` Richard Stallman [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=E1A76Iw-0002zT-6l@fencepost.gnu.org \
    --to=rms@gnu.org \
    --cc=emacs-devel@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).