* scheme-memory.texi (Memory Blocks): Use {} around types for
[bpt/guile.git] / doc / ref / scheme-utility.texi
1 @page
2 @node Utility Functions
3 @chapter General Utility Functions
4
5 @c FIXME::martin: Review me!
6
7 This chapter contains information about procedures which are not cleanly
8 tied to a specific data type. Because of their wide range of
9 applications, they are collected in a @dfn{utility} chapter.
10
11 @menu
12 * Equality:: When are two values `the same'?
13 * Object Properties:: A modern interface to object properties.
14 * Sorting:: Sort utility procedures.
15 * Copying:: Copying deep structures.
16 * General Conversion:: Converting objects to strings.
17 * Hooks:: User-customizable event lists.
18 @end menu
19
20
21 @node Equality
22 @section Equality
23
24 @c FIXME::martin: Review me!
25
26 @cindex sameness
27 @cindex equality
28
29 Three different kinds of @dfn{sameness} are defined in Scheme.
30
31 @itemize @bullet
32 @item
33 Two values can refer to exactly the same object.
34
35 @item
36 Two objects can have the same @dfn{value}.
37
38 @item
39 Two objects can be structurally equivalent.
40 @end itemize
41
42 The differentiation between these three kinds is important, because
43 determining whether two values are the same objects is very efficient,
44 while determining structural equivalence can be quite expensive
45 (consider comparing two very long lists). Therefore, three different
46 procedures for testing for equality are provided, which correspond to
47 the three kinds of @dfn{sameness} defined above.
48
49 @rnindex eq?
50 @deffn {Scheme Procedure} eq? x y
51 Return @code{#t} iff @var{x} references the same object as @var{y}.
52 @code{eq?} is similar to @code{eqv?} except that in some cases it is
53 capable of discerning distinctions finer than those detectable by
54 @code{eqv?}.
55 @end deffn
56
57 @rnindex eqv?
58 @deffn {Scheme Procedure} eqv? x y
59 The @code{eqv?} procedure defines a useful equivalence relation on objects.
60 Briefly, it returns @code{#t} if @var{x} and @var{y} should normally be
61 regarded as the same object. This relation is left slightly open to
62 interpretation, but works for comparing immediate integers, characters,
63 and inexact numbers.
64 @end deffn
65
66 @rnindex equal?
67 @deffn {Scheme Procedure} equal? x y
68 Return @code{#t} iff @var{x} and @var{y} are recursively @code{eqv?} equivalent.
69 @code{equal?} recursively compares the contents of pairs,
70 vectors, and strings, applying @code{eqv?} on other objects such as
71 numbers and symbols. A rule of thumb is that objects are generally
72 @code{equal?} if they print the same. @code{equal?} may fail to
73 terminate if its arguments are circular data structures.
74 @end deffn
75
76
77 @node Object Properties
78 @section Object Properties
79
80 It's often useful to associate a piece of additional information with a
81 Scheme object even though that object does not have a dedicated slot
82 available in which the additional information could be stored. Object
83 properties allow you to do just that.
84
85 An object property is most commonly used to associate one kind of
86 additional information with each instance of a class of similar Scheme
87 objects. For example, all procedures have a `name' property, which
88 stores the name of the variable in which the procedure was stored by a
89 @code{define} expression, or @code{#f} if the procedure wasn't created
90 by that kind of expression.
91
92 Guile's representation of an object property is a procedure-with-setter
93 (@pxref{Procedures with Setters}) that can be used with the generalized
94 form of @code{set!} (REFFIXME) to set and retrieve that property for any
95 Scheme object. So, setting a property looks like this:
96
97 @lisp
98 (set! (my-property obj1) value-for-obj1)
99 (set! (my-property obj2) value-for-obj2)
100 @end lisp
101
102 @noindent
103 And retrieving values of the same property looks like this:
104
105 @lisp
106 (my-property obj1)
107 @result{}
108 value-for-obj1
109
110 (my-property obj2)
111 @result{}
112 value-for-obj2
113 @end lisp
114
115 To create an object property in the first place, use the
116 @code{make-object-property} procedure:
117
118 @lisp
119 (define my-property (make-object-property))
120 @end lisp
121
122 @deffn {Scheme Procedure} make-object-property
123 Create and return an object property. An object property is a
124 procedure-with-setter that can be called in two ways. @code{(set!
125 (@var{property} @var{obj}) @var{val})} sets @var{obj}'s @var{property}
126 to @var{val}. @code{(@var{property} @var{obj})} returns the current
127 setting of @var{obj}'s @var{property}.
128 @end deffn
129
130 A single object property created by @code{make-object-property} can
131 associate distinct property values with all Scheme values that are
132 distinguishable by @code{eq?} (including, for example, integers).
133
134 Internally, object properties are implemented using a weak key hash
135 table. This means that, as long as a Scheme value with property values
136 is protected from garbage collection, its property values are also
137 protected. When the Scheme value is collected, its entry in the
138 property table is removed and so the (ex-) property values are no longer
139 protected by the table.
140
141 @menu
142 * Property Primitives:: Low level property implementation.
143 * Old-fashioned Properties:: An older approach to properties.
144 @end menu
145
146
147 @node Property Primitives
148 @subsection Low Level Property Implementation.
149
150 @deffn {Scheme Procedure} primitive-make-property not_found_proc
151 @deffnx {C Function} scm_primitive_make_property (not_found_proc)
152 Create a @dfn{property token} that can be used with
153 @code{primitive-property-ref} and @code{primitive-property-set!}.
154 See @code{primitive-property-ref} for the significance of
155 @var{not_found_proc}.
156 @end deffn
157
158 @deffn {Scheme Procedure} primitive-property-ref prop obj
159 @deffnx {C Function} scm_primitive_property_ref (prop, obj)
160 Return the property @var{prop} of @var{obj}. When no value
161 has yet been associated with @var{prop} and @var{obj}, call
162 @var{not-found-proc} instead (see @code{primitive-make-property})
163 and use its return value. That value is also associated with
164 @var{obj} via @code{primitive-property-set!}. When
165 @var{not-found-proc} is @code{#f}, use @code{#f} as the
166 default value of @var{prop}.
167 @end deffn
168
169 @deffn {Scheme Procedure} primitive-property-set! prop obj val
170 @deffnx {C Function} scm_primitive_property_set_x (prop, obj, val)
171 Associate @var{code} with @var{prop} and @var{obj}.
172 @end deffn
173
174 @deffn {Scheme Procedure} primitive-property-del! prop obj
175 @deffnx {C Function} scm_primitive_property_del_x (prop, obj)
176 Remove any value associated with @var{prop} and @var{obj}.
177 @end deffn
178
179
180 @node Old-fashioned Properties
181 @subsection An Older Approach to Properties
182
183 Traditionally, Lisp systems provide a different object property
184 interface to that provided by @code{make-object-property}, in which the
185 object property that is being set or retrieved is indicated by a symbol.
186
187 Guile includes this older kind of interface as well, but it may well be
188 removed in a future release, as it is less powerful than
189 @code{make-object-property} and so increases the size of the Guile
190 library for no benefit. (And it is trivial to write a compatibility
191 layer in Scheme.)
192
193 @deffn {Scheme Procedure} object-properties obj
194 @deffnx {C Function} scm_object_properties (obj)
195 Return @var{obj}'s property list.
196 @end deffn
197
198 @deffn {Scheme Procedure} set-object-properties! obj alist
199 @deffnx {C Function} scm_set_object_properties_x (obj, alist)
200 Set @var{obj}'s property list to @var{alist}.
201 @end deffn
202
203 @deffn {Scheme Procedure} object-property obj key
204 @deffnx {C Function} scm_object_property (obj, key)
205 Return the property of @var{obj} with name @var{key}.
206 @end deffn
207
208 @deffn {Scheme Procedure} set-object-property! obj key value
209 @deffnx {C Function} scm_set_object_property_x (obj, key, value)
210 In @var{obj}'s property list, set the property named @var{key}
211 to @var{value}.
212 @end deffn
213
214
215 @node Sorting
216 @section Sorting
217
218 @c FIXME::martin: Review me!
219
220 @cindex sorting
221 @cindex sorting lists
222 @cindex sorting vectors
223
224 Sorting is very important in computer programs. Therefore, Guile comes
225 with several sorting procedures built-in. As always, procedures with
226 names ending in @code{!} are side-effecting, that means that they may
227 modify their parameters in order to produce their results.
228
229 The first group of procedures can be used to merge two lists (which must
230 be already sorted on their own) and produce sorted lists containing
231 all elements of the input lists.
232
233 @deffn {Scheme Procedure} merge alist blist less
234 @deffnx {C Function} scm_merge (alist, blist, less)
235 Merge two already sorted lists into one.
236 Given two lists @var{alist} and @var{blist}, such that
237 @code{(sorted? alist less?)} and @code{(sorted? blist less?)},
238 return a new list in which the elements of @var{alist} and
239 @var{blist} have been stably interleaved so that
240 @code{(sorted? (merge alist blist less?) less?)}.
241 Note: this does _not_ accept vectors.
242 @end deffn
243
244 @deffn {Scheme Procedure} merge! alist blist less
245 @deffnx {C Function} scm_merge_x (alist, blist, less)
246 Takes two lists @var{alist} and @var{blist} such that
247 @code{(sorted? alist less?)} and @code{(sorted? blist less?)} and
248 returns a new list in which the elements of @var{alist} and
249 @var{blist} have been stably interleaved so that
250 @code{(sorted? (merge alist blist less?) less?)}.
251 This is the destructive variant of @code{merge}
252 Note: this does _not_ accept vectors.
253 @end deffn
254
255 The following procedures can operate on sequences which are either
256 vectors or list. According to the given arguments, they return sorted
257 vectors or lists, respectively. The first of the following procedures
258 determines whether a sequence is already sorted, the other sort a given
259 sequence. The variants with names starting with @code{stable-} are
260 special in that they maintain a special property of the input sequences:
261 If two or more elements are the same according to the comparison
262 predicate, they are left in the same order as they appeared in the
263 input.
264
265 @deffn {Scheme Procedure} sorted? items less
266 @deffnx {C Function} scm_sorted_p (items, less)
267 Return @code{#t} iff @var{items} is a list or a vector such that
268 for all 1 <= i <= m, the predicate @var{less} returns true when
269 applied to all elements i - 1 and i
270 @end deffn
271
272 @deffn {Scheme Procedure} sort items less
273 @deffnx {C Function} scm_sort (items, less)
274 Sort the sequence @var{items}, which may be a list or a
275 vector. @var{less} is used for comparing the sequence
276 elements. This is not a stable sort.
277 @end deffn
278
279 @deffn {Scheme Procedure} sort! items less
280 @deffnx {C Function} scm_sort_x (items, less)
281 Sort the sequence @var{items}, which may be a list or a
282 vector. @var{less} is used for comparing the sequence
283 elements. The sorting is destructive, that means that the
284 input sequence is modified to produce the sorted result.
285 This is not a stable sort.
286 @end deffn
287
288 @deffn {Scheme Procedure} stable-sort items less
289 @deffnx {C Function} scm_stable_sort (items, less)
290 Sort the sequence @var{items}, which may be a list or a
291 vector. @var{less} is used for comparing the sequence elements.
292 This is a stable sort.
293 @end deffn
294
295 @deffn {Scheme Procedure} stable-sort! items less
296 @deffnx {C Function} scm_stable_sort_x (items, less)
297 Sort the sequence @var{items}, which may be a list or a
298 vector. @var{less} is used for comparing the sequence elements.
299 The sorting is destructive, that means that the input sequence
300 is modified to produce the sorted result.
301 This is a stable sort.
302 @end deffn
303
304 The procedures in the last group only accept lists or vectors as input,
305 as their names indicate.
306
307 @deffn {Scheme Procedure} sort-list items less
308 @deffnx {C Function} scm_sort_list (items, less)
309 Sort the list @var{items}, using @var{less} for comparing the
310 list elements. This is a stable sort.
311 @end deffn
312
313 @deffn {Scheme Procedure} sort-list! items less
314 @deffnx {C Function} scm_sort_list_x (items, less)
315 Sort the list @var{items}, using @var{less} for comparing the
316 list elements. The sorting is destructive, that means that the
317 input list is modified to produce the sorted result.
318 This is a stable sort.
319 @end deffn
320
321 @deffn {Scheme Procedure} restricted-vector-sort! vec less startpos endpos
322 @deffnx {C Function} scm_restricted_vector_sort_x (vec, less, startpos, endpos)
323 Sort the vector @var{vec}, using @var{less} for comparing
324 the vector elements. @var{startpos} and @var{endpos} delimit
325 the range of the vector which gets sorted. The return value
326 is not specified.
327 @end deffn
328
329
330 @node Copying
331 @section Copying Deep Structures
332
333 @c FIXME::martin: Review me!
334
335 The procedures for copying lists (@pxref{Lists}) only produce a flat
336 copy of the input list, and currently Guile does not even contain
337 procedures for copying vectors. @code{copy-tree} can be used for these
338 application, as it does not only copy the spine of a list, but also
339 copies any pairs in the cars of the input lists.
340
341 @deffn {Scheme Procedure} copy-tree obj
342 @deffnx {C Function} scm_copy_tree (obj)
343 Recursively copy the data tree that is bound to @var{obj}, and return a
344 pointer to the new data structure. @code{copy-tree} recurses down the
345 contents of both pairs and vectors (since both cons cells and vector
346 cells may point to arbitrary objects), and stops recursing when it hits
347 any other object.
348 @end deffn
349
350
351 @node General Conversion
352 @section General String Conversion
353
354 @c FIXME::martin: Review me!
355
356 When debugging Scheme programs, but also for providing a human-friendly
357 interface, a procedure for converting any Scheme object into string
358 format is very useful. Conversion from/to strings can of course be done
359 with specialized procedures when the data type of the object to convert
360 is known, but with this procedure, it is often more comfortable.
361
362 @code{object->string} converts an object by using a print procedure for
363 writing to a string port, and then returning the resulting string.
364 Converting an object back from the string is only possible if the object
365 type has a read syntax and the read syntax is preserved by the printing
366 procedure.
367
368 @deffn {Scheme Procedure} object->string obj [printer]
369 @deffnx {C Function} scm_object_to_string (obj, printer)
370 Return a Scheme string obtained by printing @var{obj}.
371 Printing function can be specified by the optional second
372 argument @var{printer} (default: @code{write}).
373 @end deffn
374
375
376 @node Hooks
377 @section Hooks
378 @tpindex Hooks
379
380 A hook is a list of procedures to be called at well defined points in
381 time. Typically, an application provides a hook @var{h} and promises
382 its users that it will call all of the procedures in @var{h} at a
383 defined point in the application's processing. By adding its own
384 procedure to @var{h}, an application user can tap into or even influence
385 the progress of the application.
386
387 Guile itself provides several such hooks for debugging and customization
388 purposes: these are listed in a subsection below.
389
390 When an application first creates a hook, it needs to know how many
391 arguments will be passed to the hook's procedures when the hook is run.
392 The chosen number of arguments (which may be none) is declared when the
393 hook is created, and all the procedures that are added to that hook must
394 be capable of accepting that number of arguments.
395
396 A hook is created using @code{make-hook}. A procedure can be added to
397 or removed from a hook using @code{add-hook!} or @code{remove-hook!},
398 and all of a hook's procedures can be removed together using
399 @code{reset-hook!}. When an application wants to run a hook, it does so
400 using @code{run-hook}.
401
402 @menu
403 * Hook Example:: Hook usage by example.
404 * Hook Reference:: Reference of all hook procedures.
405 * C Hooks:: Hooks for use from C code.
406 * Guile Hooks:: Hooks provided by Guile.
407 @end menu
408
409
410 @node Hook Example
411 @subsection Hook Usage by Example
412
413 Hook usage is shown by some examples in this section. First, we will
414 define a hook of arity 2 --- that is, the procedures stored in the hook
415 will have to accept two arguments.
416
417 @lisp
418 (define hook (make-hook 2))
419 hook
420 @result{} #<hook 2 40286c90>
421 @end lisp
422
423 Now we are ready to add some procedures to the newly created hook with
424 @code{add-hook!}. In the following example, two procedures are added,
425 which print different messages and do different things with their
426 arguments.
427
428 @lisp
429 (add-hook! hook (lambda (x y)
430 (display "Foo: ")
431 (display (+ x y))
432 (newline)))
433 (add-hook! hook (lambda (x y)
434 (display "Bar: ")
435 (display (* x y))
436 (newline)))
437 @end lisp
438
439 Once the procedures have been added, we can invoke the hook using
440 @code{run-hook}.
441
442 @lisp
443 (run-hook hook 3 4)
444 @print{} Bar: 12
445 @print{} Foo: 7
446 @end lisp
447
448 Note that the procedures are called in the reverse of the order with
449 which they were added. This is because the default behaviour of
450 @code{add-hook!} is to add its procedure to the @emph{front} of the
451 hook's procedure list. You can force @code{add-hook!} to add its
452 procedure to the @emph{end} of the list instead by providing a third
453 @code{#t} argument on the second call to @code{add-hook!}.
454
455 @lisp
456 (add-hook! hook (lambda (x y)
457 (display "Foo: ")
458 (display (+ x y))
459 (newline)))
460 (add-hook! hook (lambda (x y)
461 (display "Bar: ")
462 (display (* x y))
463 (newline))
464 #t) ; @r{<- Change here!}
465
466 (run-hook hook 3 4)
467 @print{} Foo: 7
468 @print{} Bar: 12
469 @end lisp
470
471
472 @node Hook Reference
473 @subsection Hook Reference
474
475 When you create a hook with @code{make-hook}, you must specify the arity
476 of the procedures which can be added to the hook. If the arity is not
477 given explicitly as an argument to @code{make-hook}, it defaults to
478 zero. All procedures of a given hook must have the same arity, and when
479 the procedures are invoked using @code{run-hook}, the number of
480 arguments passed must match the arity specified at hook creation time.
481
482 The order in which procedures are added to a hook matters. If the third
483 parameter to @code{add-hook!} is omitted or is equal to @code{#f}, the
484 procedure is added in front of the procedures which might already be on
485 that hook, otherwise the procedure is added at the end. The procedures
486 are always called from the front to the end of the list when they are
487 invoked via @code{run-hook}.
488
489 The ordering of the list of procedures returned by @code{hook->list}
490 matches the order in which those procedures would be called if the hook
491 was run using @code{run-hook}.
492
493 Note that the C functions in the following entries are for handling
494 @dfn{Scheme-level} hooks in C. There are also @dfn{C-level} hooks which
495 have their own interface (@pxref{C Hooks}).
496
497 @deffn {Scheme Procedure} make-hook [n_args]
498 @deffnx {C Function} scm_make_hook (n_args)
499 Create a hook for storing procedure of arity @var{n_args}.
500 @var{n_args} defaults to zero. The returned value is a hook
501 object to be used with the other hook procedures.
502 @end deffn
503
504 @deffn {Scheme Procedure} hook? x
505 @deffnx {C Function} scm_hook_p (x)
506 Return @code{#t} if @var{x} is a hook, @code{#f} otherwise.
507 @end deffn
508
509 @deffn {Scheme Procedure} hook-empty? hook
510 @deffnx {C Function} scm_hook_empty_p (hook)
511 Return @code{#t} if @var{hook} is an empty hook, @code{#f}
512 otherwise.
513 @end deffn
514
515 @deffn {Scheme Procedure} add-hook! hook proc [append_p]
516 @deffnx {C Function} scm_add_hook_x (hook, proc, append_p)
517 Add the procedure @var{proc} to the hook @var{hook}. The
518 procedure is added to the end if @var{append_p} is true,
519 otherwise it is added to the front. The return value of this
520 procedure is not specified.
521 @end deffn
522
523 @deffn {Scheme Procedure} remove-hook! hook proc
524 @deffnx {C Function} scm_remove_hook_x (hook, proc)
525 Remove the procedure @var{proc} from the hook @var{hook}. The
526 return value of this procedure is not specified.
527 @end deffn
528
529 @deffn {Scheme Procedure} reset-hook! hook
530 @deffnx {C Function} scm_reset_hook_x (hook)
531 Remove all procedures from the hook @var{hook}. The return
532 value of this procedure is not specified.
533 @end deffn
534
535 @deffn {Scheme Procedure} hook->list hook
536 @deffnx {C Function} scm_hook_to_list (hook)
537 Convert the procedure list of @var{hook} to a list.
538 @end deffn
539
540 @deffn {Scheme Procedure} run-hook hook . args
541 @deffnx {C Function} scm_run_hook (hook, args)
542 Apply all procedures from the hook @var{hook} to the arguments
543 @var{args}. The order of the procedure application is first to
544 last. The return value of this procedure is not specified.
545 @end deffn
546
547 If, in C code, you are certain that you have a hook object and well
548 formed argument list for that hook, you can also use
549 @code{scm_c_run_hook}, which is identical to @code{scm_run_hook} but
550 does no type checking.
551
552 @deftypefn {C Function} void scm_c_run_hook (SCM hook, SCM args)
553 The same as @code{scm_run_hook} but without any type checking to confirm
554 that @var{hook} is actually a hook object and that @var{args} is a
555 well-formed list matching the arity of the hook.
556 @end deftypefn
557
558 For C code, @code{SCM_HOOKP} is a faster alternative to
559 @code{scm_hook_p}:
560
561 @deftypefn {C Macro} int SCM_HOOKP (x)
562 Return 1 if @var{x} is a Scheme-level hook, 0 otherwise.
563 @end deftypefn
564
565
566 @subsection Handling Scheme-level hooks from C code
567
568 Here is an example of how to handle Scheme-level hooks from C code using
569 the above functions.
570
571 @example
572 if (SCM_NFALSEP (scm_hook_p (obj)))
573 /* handle Scheme-level hook using C functions */
574 scm_reset_hook_x (obj);
575 else
576 /* do something else (obj is not a hook) */
577 @end example
578
579
580 @node C Hooks
581 @subsection Hooks For C Code.
582
583 The hooks already described are intended to be populated by Scheme-level
584 procedures. In addition to this, the Guile library provides an
585 independent set of interfaces for the creation and manipulation of hooks
586 that are designed to be populated by functions implemented in C.
587
588 The original motivation here was to provide a kind of hook that could
589 safely be invoked at various points during garbage collection.
590 Scheme-level hooks are unsuitable for this purpose as running them could
591 itself require memory allocation, which would then invoke garbage
592 collection recursively @dots{} However, it is also the case that these
593 hooks are easier to work with than the Scheme-level ones if you only
594 want to register C functions with them. So if that is mainly what your
595 code needs to do, you may prefer to use this interface.
596
597 To create a C hook, you should allocate storage for a structure of type
598 @code{scm_t_c_hook} and then initialize it using @code{scm_c_hook_init}.
599
600 @deftp {C Type} scm_t_c_hook
601 Data type for a C hook. The internals of this type should be treated as
602 opaque.
603 @end deftp
604
605 @deftp {C Enum} scm_t_c_hook_type
606 Enumeration of possible hook types, which are:
607
608 @table @code
609 @item SCM_C_HOOK_NORMAL
610 @vindex SCM_C_HOOK_NORMAL
611 Type of hook for which all the registered functions will always be called.
612 @item SCM_C_HOOK_OR
613 @vindex SCM_C_HOOK_OR
614 Type of hook for which the sequence of registered functions will be
615 called only until one of them returns C true (a non-NULL pointer).
616 @item SCM_C_HOOK_AND
617 @vindex SCM_C_HOOK_AND
618 Type of hook for which the sequence of registered functions will be
619 called only until one of them returns C false (a NULL pointer).
620 @end table
621 @end deftp
622
623 @deftypefn {C Function} void scm_c_hook_init (scm_t_c_hook *hook, void *hook_data, scm_t_c_hook_type type)
624 Initialize the C hook at memory pointed to by @var{hook}. @var{type}
625 should be one of the values of the @code{scm_t_c_hook_type} enumeration,
626 and controls how the hook functions will be called. @var{hook_data} is
627 a closure parameter that will be passed to all registered hook functions
628 when they are called.
629 @end deftypefn
630
631 To add or remove a C function from a C hook, use @code{scm_c_hook_add}
632 or @code{scm_c_hook_remove}. A hook function must expect three
633 @code{void *} parameters which are, respectively:
634
635 @table @var
636 @item hook_data
637 The hook closure data that was specified at the time the hook was
638 initialized by @code{scm_c_hook_init}.
639
640 @item func_data
641 The function closure data that was specified at the time that that
642 function was registered with the hook by @code{scm_c_hook_add}.
643
644 @item data
645 The call closure data specified by the @code{scm_c_hook_run} call that
646 runs the hook.
647 @end table
648
649 @deftp {C Type} scm_t_c_hook_function
650 Function type for a C hook function: takes three @code{void *}
651 parameters and returns a @code{void *} result.
652 @end deftp
653
654 @deftypefn {C Function} void scm_c_hook_add (scm_t_c_hook *hook, scm_t_c_hook_function func, void *func_data, int appendp)
655 Add function @var{func}, with function closure data @var{func_data}, to
656 the C hook @var{hook}. The new function is appended to the hook's list
657 of functions if @var{appendp} is non-zero, otherwise prepended.
658 @end deftypefn
659
660 @deftypefn {C Function} void scm_c_hook_remove (scm_t_c_hook *hook, scm_t_c_hook_function func, void *func_data)
661 Remove function @var{func}, with function closure data @var{func_data},
662 from the C hook @var{hook}. @code{scm_c_hook_remove} checks both
663 @var{func} and @var{func_data} so as to allow for the same @var{func}
664 being registered multiple times with different closure data.
665 @end deftypefn
666
667 Finally, to invoke a C hook, call the @code{scm_c_hook_run} function
668 specifying the hook and the call closure data for this run:
669
670 @deftypefn {C Function} {void *} scm_c_hook_run (scm_t_c_hook *hook, void *data)
671 Run the C hook @var{hook} will call closure data @var{data}. Subject to
672 the variations for hook types @code{SCM_C_HOOK_OR} and
673 @code{SCM_C_HOOK_AND}, @code{scm_c_hook_run} calls @var{hook}'s
674 registered functions in turn, passing them the hook's closure data, each
675 function's closure data, and the call closure data.
676
677 @code{scm_c_hook_run}'s return value is the return value of the last
678 function to be called.
679 @end deftypefn
680
681
682 @node Guile Hooks
683 @subsection Hooks Provided by Guile
684
685 @menu
686 * GC Hooks:: Garbage collection hooks.
687 * REPL Hooks:: Hooks into the Guile REPL.
688 @end menu
689
690
691 @node GC Hooks
692 @subsubsection Hooks for Garbage Collection
693
694 Whenever Guile performs a garbage collection, it calls the following
695 hooks in the order shown.
696
697 @defvr {C Hook} scm_before_gc_c_hook
698 C hook called at the very start of a garbage collection, after setting
699 @code{scm_gc_running_p} to 1, but before entering the GC critical
700 section.
701
702 If garbage collection is blocked because @code{scm_block_gc} is
703 non-zero, GC exits early soon after calling this hook, and no further
704 hooks will be called.
705 @end defvr
706
707 @defvr {C Hook} scm_before_mark_c_hook
708 C hook called before beginning the mark phase of garbage collection,
709 after the GC thread has entered a critical section.
710 @end defvr
711
712 @defvr {C Hook} scm_before_sweep_c_hook
713 C hook called before beginning the sweep phase of garbage collection.
714 This is the same as at the end of the mark phase, since nothing else
715 happens between marking and sweeping.
716 @end defvr
717
718 @defvr {C Hook} scm_after_sweep_c_hook
719 C hook called after the end of the sweep phase of garbage collection,
720 but while the GC thread is still inside its critical section.
721 @end defvr
722
723 @defvr {C Hook} scm_after_gc_c_hook
724 C hook called at the very end of a garbage collection, after the GC
725 thread has left its critical section.
726 @end defvr
727
728 @defvr {Scheme Hook} after-gc-hook
729 @vindex scm_after_gc_hook
730 Scheme hook with arity 0. This hook is run asynchronously
731 (@pxref{Asyncs}) soon after the GC has completed and any other events
732 that were deferred during garbage collection have been processed. (Also
733 accessible from C with the name @code{scm_after_gc_hook}.)
734 @end defvr
735
736 All the C hooks listed here have type @code{SCM_C_HOOK_NORMAL}, are
737 initialized with hook closure data NULL, are are invoked by
738 @code{scm_c_hook_run} with call closure data NULL.
739
740 @cindex guardians, testing for GC'd objects
741 The Scheme hook @code{after-gc-hook} is particularly useful in
742 conjunction with guardians (@pxref{Guardians}). Typically, if you are
743 using a guardian, you want to call the guardian after garbage collection
744 to see if any of the objects added to the guardian have been collected.
745 By adding a thunk that performs this call to @code{after-gc-hook}, you
746 can ensure that your guardian is tested after every garbage collection
747 cycle.
748
749
750 @node REPL Hooks
751 @subsubsection Hooks into the Guile REPL
752
753
754 @c Local Variables:
755 @c TeX-master: "guile.texi"
756 @c End: