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