Add product information.
[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 ;; beware of circular dependency.
63 (require 'product)
64 (product-provide (provide 'localhook) (require 'apel-ver))
65
66 (require 'poe)                          ; this file is loaded from poe.el.
67
68 ;; These two functions are not complete, but work enough for our purpose.
69 ;;
70 ;; (defun local-variable-p (variable &optional buffer)
71 ;;   "Non-nil if VARIABLE has a local binding in buffer BUFFER.
72 ;; BUFFER defaults to the current buffer."
73 ;;   (and (or (assq variable (buffer-local-variables buffer)) ; local and bound.
74 ;;         (memq variable (buffer-local-variables buffer))); local but void.
75 ;;        ;; docstring is ambiguous; 20.3 returns bool value.
76 ;;        t))
77 ;;
78 ;; (defun local-variable-if-set-p (variable &optional buffer)
79 ;;   "Non-nil if VARIABLE will be local in buffer BUFFER if it is set there.
80 ;; BUFFER defaults to the current buffer."
81 ;;   (and (or (assq variable (buffer-local-variables buffer)) ; local and bound.
82 ;;         (memq variable (buffer-local-variables buffer))); local but void.
83 ;;        ;; docstring is ambiguous; 20.3 returns bool value.
84 ;;        t))
85 \f
86 ;;; Hook manipulation functions.
87
88 ;; The following three functions are imported from emacs-20.3/lisp/subr.el.
89 ;; (local-variable-p, and local-variable-if-set-p are expanded.)
90 (defun make-local-hook (hook)
91   "Make the hook HOOK local to the current buffer.
92 The return value is HOOK.
93
94 When a hook is local, its local and global values
95 work in concert: running the hook actually runs all the hook
96 functions listed in *either* the local value *or* the global value
97 of the hook variable.
98
99 This function works by making `t' a member of the buffer-local value,
100 which acts as a flag to run the hook functions in the default value as
101 well.  This works for all normal hooks, but does not work for most
102 non-normal hooks yet.  We will be changing the callers of non-normal
103 hooks so that they can handle localness; this has to be done one by
104 one.
105
106 This function does nothing if HOOK is already local in the current
107 buffer.
108
109 Do not use `make-local-variable' to make a hook variable buffer-local."
110   (if ;; (local-variable-p hook)
111       (or (assq hook (buffer-local-variables)) ; local and bound.
112           (memq hook (buffer-local-variables))); local but void.
113       nil
114     (or (boundp hook) (set hook nil))
115     (make-local-variable hook)
116     (set hook (list t)))
117   hook)
118
119 (defun add-hook (hook function &optional append local)
120   "Add to the value of HOOK the function FUNCTION.
121 FUNCTION is not added if already present.
122 FUNCTION is added (if necessary) at the beginning of the hook list
123 unless the optional argument APPEND is non-nil, in which case
124 FUNCTION is added at the end.
125
126 The optional fourth argument, LOCAL, if non-nil, says to modify
127 the hook's buffer-local value rather than its default value.
128 This makes no difference if the hook is not buffer-local.
129 To make a hook variable buffer-local, always use
130 `make-local-hook', not `make-local-variable'.
131
132 HOOK should be a symbol, and FUNCTION may be any valid function.  If
133 HOOK is void, it is first set to nil.  If HOOK's value is a single
134 function, it is changed to a list of functions."
135   (or (boundp hook) (set hook nil))
136   (or (default-boundp hook) (set-default hook nil))
137   ;; If the hook value is a single function, turn it into a list.
138   (let ((old (symbol-value hook)))
139     (if (or (not (listp old)) (eq (car old) 'lambda))
140         (set hook (list old))))
141   (if (or local
142           ;; Detect the case where make-local-variable was used on a hook
143           ;; and do what we used to do.
144           (and ;; (local-variable-if-set-p hook)
145            (or (assq hook (buffer-local-variables)) ; local and bound.
146                (memq hook (buffer-local-variables))); local but void.
147            (not (memq t (symbol-value hook)))))
148       ;; Alter the local value only.
149       (or (if (or (consp function) (byte-code-function-p function))
150               (member function (symbol-value hook))
151             (memq function (symbol-value hook)))
152           (set hook
153                (if append
154                    (append (symbol-value hook) (list function))
155                  (cons function (symbol-value hook)))))
156     ;; Alter the global value (which is also the only value,
157     ;; if the hook doesn't have a local value).
158     (or (if (or (consp function) (byte-code-function-p function))
159             (member function (default-value hook))
160           (memq function (default-value hook)))
161         (set-default hook
162                      (if append
163                          (append (default-value hook) (list function))
164                        (cons function (default-value hook)))))))
165
166 (defun remove-hook (hook function &optional local)
167   "Remove from the value of HOOK the function FUNCTION.
168 HOOK should be a symbol, and FUNCTION may be any valid function.  If
169 FUNCTION isn't the value of HOOK, or, if FUNCTION doesn't appear in the
170 list of hooks to run in HOOK, then nothing is done.  See `add-hook'.
171
172 The optional third argument, LOCAL, if non-nil, says to modify
173 the hook's buffer-local value rather than its default value.
174 This makes no difference if the hook is not buffer-local.
175 To make a hook variable buffer-local, always use
176 `make-local-hook', not `make-local-variable'."
177   (if (or (not (boundp hook))           ;unbound symbol, or
178           (not (default-boundp hook))
179           (null (symbol-value hook))    ;value is nil, or
180           (null function))              ;function is nil, then
181       nil                               ;Do nothing.
182     (if (or local
183             ;; Detect the case where make-local-variable was used on a hook
184             ;; and do what we used to do.
185             (and ;; (local-variable-p hook)
186              (or (assq hook (buffer-local-variables)) ; local and bound.
187                  (memq hook (buffer-local-variables))); local but void.
188              (consp (symbol-value hook))
189              (not (memq t (symbol-value hook)))))
190         (let ((hook-value (symbol-value hook)))
191           (if (consp hook-value)
192               (if (member function hook-value)
193                   (setq hook-value (delete function (copy-sequence hook-value))))
194             (if (equal hook-value function)
195                 (setq hook-value nil)))
196           (set hook hook-value))
197       (let ((hook-value (default-value hook)))
198         (if (and (consp hook-value) (not (functionp hook-value)))
199             (if (member function hook-value)
200                 (setq hook-value (delete function (copy-sequence hook-value))))
201           (if (equal hook-value function)
202               (setq hook-value nil)))
203         (set-default hook hook-value)))))
204 \f
205 ;;; Hook execution functions.
206
207 (defun run-hook-with-args-internal (hook args cond)
208   "Run HOOK with the specified arguments ARGS.
209 HOOK should be a symbol, a hook variable.  Its value should be a list of
210 functions.  We call those functions, one by one, passing arguments ARGS
211 to each of them, until specified COND is satisfied.  If COND is nil, we
212 call those functions until one of them returns a non-nil value, and then
213 we return that value.  If COND is t, we call those functions until one
214 of them returns nil, and then we return nil.  If COND is not nil and not
215 t, we call all the functions."
216   (if (not (boundp hook))
217       ;; hook is void.
218       (not cond)
219     (let* ((functions (symbol-value hook))
220            (ret (eq cond t))
221            (all (and cond (not ret)))
222            function)
223       (if (functionp functions)
224           ;; hook is just a function.
225           (apply functions args)
226         ;; hook is nil or a list of functions.
227         (while (and functions
228                     (or all             ; to-completion
229                         (if cond
230                             ret         ; until-failure
231                           (null ret)))) ; until-success
232           (setq function (car functions)
233                 functions(cdr functions))
234           (if (eq function t)
235               ;; this hook has a local binding.
236               ;; we must run the global binding too.
237               (let ((globals (default-value hook))
238                     global)
239                 (if (functionp globals)
240                     (setq ret (apply globals args))
241                   (while (and globals
242                               (or all
243                                   (if cond
244                                       ret
245                                     (null ret))))
246                     (setq global (car globals)
247                           globals(cdr globals))
248                     (or (eq global t)   ; t should not occur.
249                         (setq ret (apply global args))))))
250             (setq ret (apply function args))))
251         ret))))
252
253 ;; The following four functions are direct translation of their
254 ;; C definitions in emacs-20.3/src/eval.c.
255 (defun run-hooks (&rest hooks)
256   "Run each hook in HOOKS.  Major mode functions use this.
257 Each argument should be a symbol, a hook variable.
258 These symbols are processed in the order specified.
259 If a hook symbol has a non-nil value, that value may be a function
260 or a list of functions to be called to run the hook.
261 If the value is a function, it is called with no arguments.
262 If it is a list, the elements are called, in order, with no arguments.
263
264 To make a hook variable buffer-local, use `make-local-hook',
265 not `make-local-variable'."
266   (while hooks
267     (run-hook-with-args-internal (car hooks) nil 'to-completion)
268     (setq hooks (cdr hooks))))
269
270 (defun run-hook-with-args (hook &rest args)
271   "Run HOOK with the specified arguments ARGS.
272 HOOK should be a symbol, a hook variable.  If HOOK has a non-nil
273 value, that value may be a function or a list of functions to be
274 called to run the hook.  If the value is a function, it is called with
275 the given arguments and its return value is returned.  If it is a list
276 of functions, those functions are called, in order,
277 with the given arguments ARGS.
278 It is best not to depend on the value return by `run-hook-with-args',
279 as that may change.
280
281 To make a hook variable buffer-local, use `make-local-hook',
282 not `make-local-variable'."
283   (run-hook-with-args-internal hook args 'to-completion))
284
285 (defun run-hook-with-args-until-success (hook &rest args)
286   "Run HOOK with the specified arguments ARGS.
287 HOOK should be a symbol, a hook variable.  Its value should
288 be a list of functions.  We call those functions, one by one,
289 passing arguments ARGS to each of them, until one of them
290 returns a non-nil value.  Then we return that value.
291 If all the functions return nil, we return nil.
292
293 To make a hook variable buffer-local, use `make-local-hook',
294 not `make-local-variable'."
295   (run-hook-with-args-internal hook args nil))
296
297 (defun run-hook-with-args-until-failure (hook &rest args)
298   "Run HOOK with the specified arguments ARGS.
299 HOOK should be a symbol, a hook variable.  Its value should
300 be a list of functions.  We call those functions, one by one,
301 passing arguments ARGS to each of them, until one of them
302 returns nil.  Then we return nil.
303 If all the functions return non-nil, we return non-nil.
304
305 To make a hook variable buffer-local, use `make-local-hook',
306 not `make-local-variable'."
307   (run-hook-with-args-internal hook args t))
308
309 ;;; localhook.el ends here