Sync up with README.en.
[elisp/apel.git] / localhook.el
1 ;;; localhook.el --- local hook variable support in emacs-lisp.
2
3 ;; Copyright (C) 1985,86,92,94,95,1999 Free Software Foundation, Inc.
4
5 ;; Author: Shuhei KOBAYASHI <shuhei@aqua.ocn.ne.jp>
6 ;; Keywords: compatibility
7
8 ;; This file is part of APEL (A Portable Emacs Library).
9
10 ;; This program is free software; you can redistribute it and/or
11 ;; modify it under the terms of the GNU General Public License as
12 ;; published by the Free Software Foundation; either version 2, or (at
13 ;; your option) any later version.
14
15 ;; This program is distributed in the hope that it will be useful, but
16 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 ;; General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with this program; see the file COPYING.  If not, write to
22 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;; This file (re)defines the following functions.
28 ;; These functions support local hook feature in emacs-lisp level.
29 ;;
30 ;;      add-hook, remove-hook, make-local-hook,
31 ;;      run-hooks, run-hook-with-args,
32 ;;      run-hook-with-args-until-success, and
33 ;;      run-hook-with-args-until-failure.
34
35 ;; The following functions which do not exist in 19.28 are used in the
36 ;; original definitions of add-hook, remove-hook, and make-local-hook.
37 ;;
38 ;;      local-variable-p, and local-variable-if-set-p.
39 ;;
40 ;; In this file, these functions are replaced with mock versions.
41
42 ;; In addition, the following functions which do not exist in v18 are used.
43 ;;
44 ;;      default-boundp, byte-code-function-p, functionp, member, and delete.
45 ;;
46 ;; These functions are provided by poe-18.el.
47
48 ;; For historians:
49 ;;
50 ;;      `add-hook' and `remove-hook' were introduced in v19.
51 ;;
52 ;;      Local hook feature and `make-local-hook' were introduced in 19.29.
53 ;;
54 ;;      `run-hooks' exists in v17.
55 ;;      `run-hook-with-args' was introduced in 19.23 as a lisp function.
56 ;;      Two variants of `run-hook-with-args' were introduced in 19.29 as
57 ;;      lisp functions.  `run-hook' family became C primitives in 19.30.
58
59 ;;; Code:
60
61 (provide 'localhook)                    ; beware of circular dependency.
62 (require 'poe)                          ; this file is loaded from poe.el.
63
64 ;; These two functions are not complete, but work enough for our purpose.
65 ;;
66 ;; (defun local-variable-p (variable &optional buffer)
67 ;;   "Non-nil if VARIABLE has a local binding in buffer BUFFER.
68 ;; BUFFER defaults to the current buffer."
69 ;;   (and (or (assq variable (buffer-local-variables buffer)) ; local and bound.
70 ;;         (memq variable (buffer-local-variables buffer))); local but void.
71 ;;        ;; docstring is ambiguous; 20.3 returns bool value.
72 ;;        t))
73 ;;
74 ;; (defun local-variable-if-set-p (variable &optional buffer)
75 ;;   "Non-nil if VARIABLE will be local in buffer BUFFER if it is set there.
76 ;; BUFFER defaults to the current buffer."
77 ;;   (and (or (assq variable (buffer-local-variables buffer)) ; local and bound.
78 ;;         (memq variable (buffer-local-variables buffer))); local but void.
79 ;;        ;; docstring is ambiguous; 20.3 returns bool value.
80 ;;        t))
81 \f
82 ;;; Hook manipulation functions.
83
84 ;; The following three functions are imported from emacs-20.3/lisp/subr.el.
85 ;; (local-variable-p, and local-variable-if-set-p are expanded.)
86 (defun make-local-hook (hook)
87   "Make the hook HOOK local to the current buffer.
88 The return value is HOOK.
89
90 When a hook is local, its local and global values
91 work in concert: running the hook actually runs all the hook
92 functions listed in *either* the local value *or* the global value
93 of the hook variable.
94
95 This function works by making `t' a member of the buffer-local value,
96 which acts as a flag to run the hook functions in the default value as
97 well.  This works for all normal hooks, but does not work for most
98 non-normal hooks yet.  We will be changing the callers of non-normal
99 hooks so that they can handle localness; this has to be done one by
100 one.
101
102 This function does nothing if HOOK is already local in the current
103 buffer.
104
105 Do not use `make-local-variable' to make a hook variable buffer-local."
106   (if ;; (local-variable-p hook)
107       (or (assq hook (buffer-local-variables)) ; local and bound.
108           (memq hook (buffer-local-variables))); local but void.
109       nil
110     (or (boundp hook) (set hook nil))
111     (make-local-variable hook)
112     (set hook (list t)))
113   hook)
114
115 (defun add-hook (hook function &optional append local)
116   "Add to the value of HOOK the function FUNCTION.
117 FUNCTION is not added if already present.
118 FUNCTION is added (if necessary) at the beginning of the hook list
119 unless the optional argument APPEND is non-nil, in which case
120 FUNCTION is added at the end.
121
122 The optional fourth argument, LOCAL, if non-nil, says to modify
123 the hook's buffer-local value rather than its default value.
124 This makes no difference if the hook is not buffer-local.
125 To make a hook variable buffer-local, always use
126 `make-local-hook', not `make-local-variable'.
127
128 HOOK should be a symbol, and FUNCTION may be any valid function.  If
129 HOOK is void, it is first set to nil.  If HOOK's value is a single
130 function, it is changed to a list of functions."
131   (or (boundp hook) (set hook nil))
132   (or (default-boundp hook) (set-default hook nil))
133   ;; If the hook value is a single function, turn it into a list.
134   (let ((old (symbol-value hook)))
135     (if (or (not (listp old)) (eq (car old) 'lambda))
136         (set hook (list old))))
137   (if (or local
138           ;; Detect the case where make-local-variable was used on a hook
139           ;; and do what we used to do.
140           (and ;; (local-variable-if-set-p hook)
141            (or (assq hook (buffer-local-variables)) ; local and bound.
142                (memq hook (buffer-local-variables))); local but void.
143            (not (memq t (symbol-value hook)))))
144       ;; Alter the local value only.
145       (or (if (or (consp function) (byte-code-function-p function))
146               (member function (symbol-value hook))
147             (memq function (symbol-value hook)))
148           (set hook
149                (if append
150                    (append (symbol-value hook) (list function))
151                  (cons function (symbol-value hook)))))
152     ;; Alter the global value (which is also the only value,
153     ;; if the hook doesn't have a local value).
154     (or (if (or (consp function) (byte-code-function-p function))
155             (member function (default-value hook))
156           (memq function (default-value hook)))
157         (set-default hook
158                      (if append
159                          (append (default-value hook) (list function))
160                        (cons function (default-value hook)))))))
161
162 (defun remove-hook (hook function &optional local)
163   "Remove from the value of HOOK the function FUNCTION.
164 HOOK should be a symbol, and FUNCTION may be any valid function.  If
165 FUNCTION isn't the value of HOOK, or, if FUNCTION doesn't appear in the
166 list of hooks to run in HOOK, then nothing is done.  See `add-hook'.
167
168 The optional third argument, LOCAL, if non-nil, says to modify
169 the hook's buffer-local value rather than its default value.
170 This makes no difference if the hook is not buffer-local.
171 To make a hook variable buffer-local, always use
172 `make-local-hook', not `make-local-variable'."
173   (if (or (not (boundp hook))           ;unbound symbol, or
174           (not (default-boundp hook))
175           (null (symbol-value hook))    ;value is nil, or
176           (null function))              ;function is nil, then
177       nil                               ;Do nothing.
178     (if (or local
179             ;; Detect the case where make-local-variable was used on a hook
180             ;; and do what we used to do.
181             (and ;; (local-variable-p hook)
182              (or (assq hook (buffer-local-variables)) ; local and bound.
183                  (memq hook (buffer-local-variables))); local but void.
184              (consp (symbol-value hook))
185              (not (memq t (symbol-value hook)))))
186         (let ((hook-value (symbol-value hook)))
187           (if (consp hook-value)
188               (if (member function hook-value)
189                   (setq hook-value (delete function (copy-sequence hook-value))))
190             (if (equal hook-value function)
191                 (setq hook-value nil)))
192           (set hook hook-value))
193       (let ((hook-value (default-value hook)))
194         (if (and (consp hook-value) (not (functionp hook-value)))
195             (if (member function hook-value)
196                 (setq hook-value (delete function (copy-sequence hook-value))))
197           (if (equal hook-value function)
198               (setq hook-value nil)))
199         (set-default hook hook-value)))))
200 \f
201 ;;; Hook execution functions.
202
203 (defun run-hook-with-args-internal (hook args cond)
204   "Run HOOK with the specified arguments ARGS.
205 HOOK should be a symbol, a hook variable.  Its value should be a list of
206 functions.  We call those functions, one by one, passing arguments ARGS
207 to each of them, until specified COND is satisfied.  If COND is nil, we
208 call those functions until one of them returns a non-nil value, and then
209 we return that value.  If COND is t, we call those functions until one
210 of them returns nil, and then we return nil.  If COND is not nil and not
211 t, we call all the functions."
212   (if (not (boundp hook))
213       ;; hook is void.
214       (not cond)
215     (let* ((functions (symbol-value hook))
216            (ret (eq cond t))
217            (all (and cond (not ret)))
218            function)
219       (if (functionp functions)
220           ;; hook is just a function.
221           (apply functions args)
222         ;; hook is nil or a list of functions.
223         (while (and functions
224                     (or all             ; to-completion
225                         (if cond
226                             ret         ; until-failure
227                           (null ret)))) ; until-success
228           (setq function (car functions)
229                 functions(cdr functions))
230           (if (eq function t)
231               ;; this hook has a local binding.
232               ;; we must run the global binding too.
233               (let ((globals (default-value hook))
234                     global)
235                 (if (functionp globals)
236                     (setq ret (apply globals args))
237                   (while (and globals
238                               (or all
239                                   (if cond
240                                       ret
241                                     (null ret))))
242                     (setq global (car globals)
243                           globals(cdr globals))
244                     (or (eq global t)   ; t should not occur.
245                         (setq ret (apply global args))))))
246             (setq ret (apply function args))))
247         ret))))
248
249 ;; The following four functions are direct translation of their
250 ;; C definitions in emacs-20.3/src/eval.c.
251 (defun run-hooks (&rest hooks)
252   "Run each hook in HOOKS.  Major mode functions use this.
253 Each argument should be a symbol, a hook variable.
254 These symbols are processed in the order specified.
255 If a hook symbol has a non-nil value, that value may be a function
256 or a list of functions to be called to run the hook.
257 If the value is a function, it is called with no arguments.
258 If it is a list, the elements are called, in order, with no arguments.
259
260 To make a hook variable buffer-local, use `make-local-hook',
261 not `make-local-variable'."
262   (while hooks
263     (run-hook-with-args-internal (car hooks) nil 'to-completion)
264     (setq hooks (cdr hooks))))
265
266 (defun run-hook-with-args (hook &rest args)
267   "Run HOOK with the specified arguments ARGS.
268 HOOK should be a symbol, a hook variable.  If HOOK has a non-nil
269 value, that value may be a function or a list of functions to be
270 called to run the hook.  If the value is a function, it is called with
271 the given arguments and its return value is returned.  If it is a list
272 of functions, those functions are called, in order,
273 with the given arguments ARGS.
274 It is best not to depend on the value return by `run-hook-with-args',
275 as that may change.
276
277 To make a hook variable buffer-local, use `make-local-hook',
278 not `make-local-variable'."
279   (run-hook-with-args-internal hook args 'to-completion))
280
281 (defun run-hook-with-args-until-success (hook &rest args)
282   "Run HOOK with the specified arguments ARGS.
283 HOOK should be a symbol, a hook variable.  Its value should
284 be a list of functions.  We call those functions, one by one,
285 passing arguments ARGS to each of them, until one of them
286 returns a non-nil value.  Then we return that value.
287 If all the functions return nil, we return nil.
288
289 To make a hook variable buffer-local, use `make-local-hook',
290 not `make-local-variable'."
291   (run-hook-with-args-internal hook args nil))
292
293 (defun run-hook-with-args-until-failure (hook &rest args)
294   "Run HOOK with the specified arguments ARGS.
295 HOOK should be a symbol, a hook variable.  Its value should
296 be a list of functions.  We call those functions, one by one,
297 passing arguments ARGS to each of them, until one of them
298 returns nil.  Then we return nil.
299 If all the functions return non-nil, we return non-nil.
300
301 To make a hook variable buffer-local, use `make-local-hook',
302 not `make-local-variable'."
303   (run-hook-with-args-internal hook args t))
304
305 ;;; localhook.el ends here