aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorStefan Monnier2000-05-10 22:40:17 +0000
committerStefan Monnier2000-05-10 22:40:17 +0000
commit8947a5e23255b77ff0dae73675d3a2e77122c066 (patch)
tree2ddaed5799ad6f9809c4645bc78112489e173c8f
parentb15b5618c8e291d687d36dbfcce1081a30683c3d (diff)
downloademacs-8947a5e23255b77ff0dae73675d3a2e77122c066.tar.gz
emacs-8947a5e23255b77ff0dae73675d3a2e77122c066.zip
(add-hook, remove-hook): Make hook buffer-local if needed..
(add-minor-mode): Don't make the variable buffer-local and add a reference to define-minor-mode in the docstring.
-rw-r--r--lisp/subr.el97
1 files changed, 41 insertions, 56 deletions
diff --git a/lisp/subr.el b/lisp/subr.el
index 9f8418f671e..1d37402f22d 100644
--- a/lisp/subr.el
+++ b/lisp/subr.el
@@ -680,7 +680,7 @@ FUNCTION is added at the end.
680 680
681The optional fourth argument, LOCAL, if non-nil, says to modify 681The optional fourth argument, LOCAL, if non-nil, says to modify
682the hook's buffer-local value rather than its default value. 682the hook's buffer-local value rather than its default value.
683This makes no difference if the hook is not buffer-local. 683This makes the hook buffer-local if needed.
684To make a hook variable buffer-local, always use 684To make a hook variable buffer-local, always use
685`make-local-hook', not `make-local-variable'. 685`make-local-hook', not `make-local-variable'.
686 686
@@ -689,32 +689,23 @@ HOOK is void, it is first set to nil. If HOOK's value is a single
689function, it is changed to a list of functions." 689function, it is changed to a list of functions."
690 (or (boundp hook) (set hook nil)) 690 (or (boundp hook) (set hook nil))
691 (or (default-boundp hook) (set-default hook nil)) 691 (or (default-boundp hook) (set-default hook nil))
692 ;; If the hook value is a single function, turn it into a list. 692 (if local (make-local-hook hook)
693 (let ((old (symbol-value hook))) 693 ;; Detect the case where make-local-variable was used on a hook
694 (if (or (not (listp old)) (eq (car old) 'lambda)) 694 ;; and do what we used to do.
695 (set hook (list old)))) 695 (unless (and (consp (symbol-value hook)) (memq t (symbol-value hook)))
696 (if (or local 696 (setq local t)))
697 ;; Detect the case where make-local-variable was used on a hook 697 (let ((hook-value (if local (symbol-value hook) (default-value hook))))
698 ;; and do what we used to do. 698 ;; If the hook value is a single function, turn it into a list.
699 (and (local-variable-if-set-p hook) 699 (when (or (not (listp hook-value)) (eq (car hook-value) 'lambda))
700 (not (memq t (symbol-value hook))))) 700 (set hook-value (list hook-value)))
701 ;; Alter the local value only. 701 ;; Do the actual addition if necessary
702 (or (if (or (consp function) (byte-code-function-p function)) 702 (unless (member function hook-value)
703 (member function (symbol-value hook)) 703 (setq hook-value
704 (memq function (symbol-value hook))) 704 (if append
705 (set hook 705 (append hook-value (list function))
706 (if append 706 (cons function hook-value))))
707 (append (symbol-value hook) (list function)) 707 ;; Set the actual variable
708 (cons function (symbol-value hook))))) 708 (if local (set hook hook-value) (set-default hook hook-value))))
709 ;; Alter the global value (which is also the only value,
710 ;; if the hook doesn't have a local value).
711 (or (if (or (consp function) (byte-code-function-p function))
712 (member function (default-value hook))
713 (memq function (default-value hook)))
714 (set-default hook
715 (if append
716 (append (default-value hook) (list function))
717 (cons function (default-value hook)))))))
718 709
719(defun remove-hook (hook function &optional local) 710(defun remove-hook (hook function &optional local)
720 "Remove from the value of HOOK the function FUNCTION. 711 "Remove from the value of HOOK the function FUNCTION.
@@ -724,34 +715,28 @@ list of hooks to run in HOOK, then nothing is done. See `add-hook'.
724 715
725The optional third argument, LOCAL, if non-nil, says to modify 716The optional third argument, LOCAL, if non-nil, says to modify
726the hook's buffer-local value rather than its default value. 717the hook's buffer-local value rather than its default value.
727This makes no difference if the hook is not buffer-local. 718This makes the hook buffer-local if needed.
728To make a hook variable buffer-local, always use 719To make a hook variable buffer-local, always use
729`make-local-hook', not `make-local-variable'." 720`make-local-hook', not `make-local-variable'."
730 (if (or (not (boundp hook)) ;unbound symbol, or 721 (or (boundp hook) (set hook nil))
731 (not (default-boundp hook)) 722 (or (default-boundp hook) (set-default hook nil))
732 (null (symbol-value hook)) ;value is nil, or 723 (if local (make-local-hook hook)
733 (null function)) ;function is nil, then 724 ;; Detect the case where make-local-variable was used on a hook
734 nil ;Do nothing. 725 ;; and do what we used to do.
735 (if (or local 726 (unless (and (consp (symbol-value hook)) (memq t (symbol-value hook)))
736 ;; Detect the case where make-local-variable was used on a hook 727 (setq local t)))
737 ;; and do what we used to do. 728 (let ((hook-value (if local (symbol-value hook) (default-value hook))))
738 (and (local-variable-p hook) 729 ;; If the hook value is a single function, turn it into a list.
739 (consp (symbol-value hook)) 730 (when (or (not (listp hook-value)) (eq (car hook-value) 'lambda))
740 (not (memq t (symbol-value hook))))) 731 (set hook-value (list hook-value)))
741 (let ((hook-value (symbol-value hook))) 732 ;; Do the actual removal if necessary
742 (if (consp hook-value) 733 (setq hook-value (delete function (copy-sequence hook-value)))
743 (if (member function hook-value) 734 ;; If the function is on the global hook, we need to shadow it locally
744 (setq hook-value (delete function (copy-sequence hook-value)))) 735 ;;(when (and local (member function (default-value hook))
745 (if (equal hook-value function) 736 ;; (not (member (cons 'not function) hook-value)))
746 (setq hook-value nil))) 737 ;; (push (cons 'not function) hook-value))
747 (set hook hook-value)) 738 ;; Set the actual variable
748 (let ((hook-value (default-value hook))) 739 (if local (set hook hook-value) (set-default hook hook-value))))
749 (if (and (consp hook-value) (not (functionp hook-value)))
750 (if (member function hook-value)
751 (setq hook-value (delete function (copy-sequence hook-value))))
752 (if (equal hook-value function)
753 (setq hook-value nil)))
754 (set-default hook hook-value)))))
755 740
756(defun add-to-list (list-var element) 741(defun add-to-list (list-var element)
757 "Add to the value of LIST-VAR the element ELEMENT if it isn't there yet. 742 "Add to the value of LIST-VAR the element ELEMENT if it isn't there yet.
@@ -1503,9 +1488,9 @@ Optional AFTER specifies that TOGGLE should be added after AFTER
1503in `minor-mode-alist'. 1488in `minor-mode-alist'.
1504 1489
1505Optional TOGGLE-FUN is there for compatiblity with other Emacsen. 1490Optional TOGGLE-FUN is there for compatiblity with other Emacsen.
1506It is currently not used." 1491It is currently not used.
1507 (make-local-variable toggle) 1492
1508 1493In most cases, `define-minor-mode' should be used instead."
1509 (when name 1494 (when name
1510 (let ((existing (assq toggle minor-mode-alist)) 1495 (let ((existing (assq toggle minor-mode-alist))
1511 (name (if (symbolp name) (symbol-value name) name))) 1496 (name (if (symbolp name) (symbol-value name) name)))